Let us use an example, given this grammar:
ex = ex op term | term
op = + | -
term = ( ex ) | id
which yields the following sentences:
id + id
id + ( id + ... ) + ... )))
( id + id ) - id
ex = term {op term}
op = + | -
term = ( ex ) | id
op()
if tok == + match( + )
else if tok == - match( - )
else error
term()
if tok == (
match( ( )
ex()
match( ) )
else if tok == id
match( id )
else
error
ex()
term()
if tok == + || tok == -
op()
term()
push(arg)
pushop(op)
arg = pop()
op = popop()
newatom()
cons()
op()
if tok == +
match( + )
pushop( + )
else if tok == -
match( - )
pushop( - )
else error
term()
if tok == (
match( ( )
ex()
match( ) )
makebintree()
else if tok == id
match( id )
push( id )
else
error
ex()
term()
if tok == + || tok == -
op()
term()
makebintree()
b = pop()
a = pop()
op = popop()
c = cons(op,cons(a,cons(b,NIL)))
push(c)
last update 19 Nov 2018