Recursive Decent Parsing:Functions to parse the Toy Grammar II
Here are two more of the four functions needed to parse the Toy Grammar. Note the use of a while() loop to match possibly repeated elements in a grammar production, based on whether the look ahead token indicates that a repeated element is present:
<> => <> ( (+|-) <> )*
term(); /* non-terminal is function call */
while( tokentype == PLUS || tokentype == MINUS ){
scanner(); /* get next lookahead token */
term(); /* non-terminal is function call */
<> => <> ( (*|/) <> )*
factor(); /* non-terminal is function call */
while( tokentype == MULT || tokentype == DIV ){
scanner(); /* get next lookahead token */
factor();/* non-terminal is function call */