CS 364 - Parser for Clite

In this assignment you are going to complete the parser for Clite that we began in class. The project is due in two parts. Part one is due before you leave for spring break and covers the subset of the gramar that includes declarations and assignment statements. Part 2 will be due Tuesday April 7. Make sure you turn in a stapled printout. Watch for bad line breaks, split lines of code and comments across lines nicely.

  1. Implement the complete Clite grammar from pages 37-38 of the book except for the array related syntax. Also you do not have to implement comma separated variable declarations. The simplified grammar is below.
      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 )
    
  2. Your parser should use your lexer you developed in the previous assignment.
  3. Your parser should parse a Clite program and reprint the program on the console with "good" formatting. For example the test file isprime.c might get reprinted as
    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.

  4. The reprinted program should be syntactically correct. That is, I should be able to cut-and-paste it from the console window into a file and parse it and get the same program reprinted.
  5. When you have a parse error your program should issue a user friendly error message with a line number.
  6. Your program should be able to read any number of files on the command line (just like your lexer did).
  7. I should be able to run your parser from the command line. See me if you need help in figuring this out. The command line argument should be the name of the Clite file being parsed.