Menu

#20 state transition stack for syntax parser

open
nobody
None
5
2012-12-12
2003-12-06
Max
No

It would be useful to have a stack for state transition
and an abitily to return to the "previous" state
("caller") from the current one, whatever previous
state is.

This would allow simplify implementation of nested
syntax constructions that do not depend on where they
are embedded.

As an example consider Pascal comments syntax. Comments
can present in main program as well as in
asm-instructions blocks.
Currently we have to handle each case separately like

h_state 0 { 'Normal' }
h_trans { 8, '<', 'asm', 'Command' }
# ...
h_trans { 11, '<', '//', 'Comment' }
# ...

h_state 8 { 'Command' }
h_trans { 18, '<', '//', 'Comment' }
# ...

h_state 11 { 'Comment' }
h_trans { 0, '$', '', 'Comment' }

# ...

h_state 18 { 'Comment' }
h_trans { 8, '$', '', 'Comment' }

Here states 11 and 18 are almost identical. The only
difference is where they pass flow control when done.

Suppose now that we have a state transition stack and
can return flow control to the caller which state I
will denote ``-1''. The same example will look like

h_state 0 { 'Normal' }
h_trans { 8, '<', 'asm', 'Command' }
# ...
h_trans { 11, '<', '//', 'Comment' }
# ...

h_state 8 { 'Command' }
h_trans { 11, '<', '//', 'Comment' }
# ...

h_state 11 { 'Comment' }
h_trans { -1, '$', '', 'Comment' }

Both main program and asm-block pass flow control to
the same state and later get it back.

Discussion


Log in to post a comment.