Compilers and Interpreters: Assignment 5
October 17, 2008
Make an ANTLR grammar for Mini-Triangle, as described in pages 8-9
of our textbook. Feel free to modify the BNF as you feel suited.
Your grammar should implement the following actions:
- When there is a constant declaration, put the ID and the value in
the hash table.
- When there is a variable declaration, put the ID and the default value
zero in the hash table.
- Whenever there is an expression in the program, output its value based
on the current contents of the hash table.
If some ID is not in the hash table,
you choose what to do:
either consider it to be zero, or issue an error message.
Consider the result of an
operation that yields a boolean as 0 or 1 for false and true, respectively.
The backslash means 'not'. 'not' applied to a non-zero is zero, and
to zero is 1. Implement the following precedence in your grammar.
- Unary operators: first.
- * and /: second.
- + and -: third.
- Comparisons: fourth.
- Whenever there is an assignment, update the value in the hash table.
For now, don't worry about scopes. A compiler does not in general evaluate
expressions, it generates code to evaluate expressions. We're including actions
here to get familiar with the concept. So what our compiler does for now is
rather meaningless. It's not really an interpreter either.
Due Wednesday, October 29, befire the start of class.
E-mail to the instructor (firstname.lastname@example.org)
and to the teaching assistant (email@example.com):
- the grammar.
- a sample Mini-Triangle program that causes the parser to execute
- the output generated by the parser on your program.