Context Free Grammars and Parse Trees
- Write a grammar rule for parsing an ifStatement. Specifically,
you should consider the following: an ifStatement should
- Start with the ‘if’
keyword,
- Require parenthesis around the condition,
- Can have any expression for the condition (you do not need to
write a rule for expressions, you can assume one exists named
expression),
- The condition is followed by the ‘then’
keyword,
- Which is followed by zero or more statements (you do not need
to write a rule for statements, you can assume one exists named
statements)
- Followed by the ‘else’
keyword,
- Followed by zero or more statements,
- Followed by the ‘end’
keyword.
Example ifStatement:
if ( x * y + 10 )
then
x = 1
y = 2
else
x = 0
y = 1
end