In this assignment you are going to complete the parser for Clite that we began in class. The project is due on Monday April 7 by class time. Put your assignment in a folder on the T: drive named CliteParser
. Make sure you turn in a stapled printout. Watch for bad line breaks, split lines of code and comments across lines nicely.
This program will take a while to complete and will be worth twenty points (your other assignments have been worth ten). Here are the requirements you will be graded on. Your parser should ...
Program ⇒ int main ( ) { Declarations Statements } Declarations ⇒ Declaration Declarations | epsilon Declaration ⇒ Type Identifier ; Type ⇒ int | bool | float | char Statements ⇒ Statement Statements | epsilon Statement ⇒ ; | Block | Assignment | IfStatement | WhileStatement Block ⇒ { Statements } Assignment ⇒ Identifier = Expression ; IfStatement ⇒ if ( Expression ) Statement [ else Statement ] WhileStatement ⇒ while ( Expression ) Statement Expression ⇒ Conjunction { || Conjunction } Conjunction ⇒ Equality { && Equality } Equality ⇒ Relation [ EquOp Relation ] EquOp ⇒ == | != Relation ⇒ Addition [ RelOp Addition ] RelOp ⇒ < | <= | > | >= Addition ⇒ Term { AddOp Term } AddOp ⇒ + | - Term ⇒ Factor { MulOp Factor } MulOp ⇒ * | / | % Factor ⇒ [ UnaryOp ] Primary UnaryOp ⇒ - | ! Primary ⇒ Identifier | IntLit | FloatLit | ( Expression )
int main() { bool prime; int i; int n; i = 2; n = 1234567; prime = (((!false) && (!false)) || (true && (!true))); while ((prime && (i < n/2))) { if (((n % i) == 0))) prime = false; i = (i + 1); } }
Since your lexer tosses comments and whitespace (at least they should) you don't need to worry about reprinting those as they appeared in the file. You need to print the parentheses to show that you parsed the expressions properly. You also need
to get the indentation right. Hint: To get indentation, as you traverse the abstract syntax
tree pass down a variable named indent_level
that is incremented each time
you need to increase the indent. The use indent_level
to
print an appropriate number of leading spaces.