Hello.
I'm currently learning how lexers and parsers work, and i have following question about state machine. For example, i need to colorize text by following rule: For this rule simple state transition table will look like this:
current event next action
IDLE $ COLOR -
COLOR any - OnColor()
COLOR \n IDLE -
This will call OnColor() action for every character that is between '$' and line end so i can colorize it. Of course same can be automatically generated from regexp, but i really want to know how it works before heavy magic usage :). Next goes problem: if i have a rule: (want to color any line of text that ends with dollar, the state transition table is not very clear:
current event next action
IDLE any - -
IDLE $ DOUND_DOLLAR -
FOUND_DOLLAR \n IDLE OnDollar()
FOUND_DOLLAR any IDLE -
I can teach my state machine to call OnDollar() if it founds a '$' sign at end of line, but what i can do in order to colorize text that was BEFORE dollar sign encounter? What are common patterns to solve such problems? Of course it will be 1 line with regexp, but i'm really interested to know how such parser can be implemented via state machine and is it possible at all.