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 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 ...

  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. (I believe everyone's lexer was good enough to use.)
  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.