Compilers: Programming Assignment 2

Syntactic analysis for php-- using SableCC

Due date: Monday, March 3, 2008

The goal of this programming assignment is to implement the parser for php-- and, in so doing, gain experience using context-free grammars and more experience with SableCC and Eclipse. The most challenging aspect of the assignment will be designing an accurate and unambiguous context-free grammar for php-- and having the patience to cover all the cases required to recursively walk over the parse tree in order to "pretty print" it effectively.


What you need to do

Read this assignment in its entirety. Review the description of idempotence and its relevance to compilers, the php-- reference manual, and Chapter 5 ("Parser") in Gagnon's "SableCC, an Object-Oriented Compiler Framework" available from the SableCC Web site before starting to implement the parser.

Download the starter project, try compiling and running it using SableCC and your Java compiler. As it stands, the parser is not good for much: it can only successfully parse simple assignment statements.

Important: Due to a slight quirk (a.k.a. bug, a.k.a. feature) you will need to manually edit the SableCC-generated Parser.java file every time you run SableCC (can you figure out why?). To do so - go to the Parser.java file and replace the two occurrences of

  :*/
with
  :* /
(That's right - you are just adding one space, in two places. That's it.)

  1. I recommend writing an initial grammar for php-- on paper, or, at least, not worrying about getting the SableCC code right until after you have a good idea what the context-free grammar should look like.

  2. Implement a portion of the grammar for statements and expressions that allow you to try out fragments such as:

      $n = 10;
      $sum = 0;
      $i = 0;
      while ($i < $n) {
        $sum = $sum + $i;
        $i = $i + 1;
      }
      // the following is not php-- type correct, but no matter
      echo "the sum is: ", $sum; 
    
    Remember that all php-- programs must be enclosed in begin (<?php) and end (?>) tags.

  3. Slowly add compound statements to your grammar. Each time you add a kind of statement, run SableCC to see if your grammar encounters any shift-reduce conflicts. The "dangling else" may be of particular concern. Keep adding statements until you have completed this portion of the grammar.

  4. Add productions for parsing functions. Remember to handle reference parameters as in:

      function swap(/*:string[]:*/ &$a, /*:int:*/ $i, /*:int:*/ $j) /*:void:*/ {
        if ($i != $j) {
          /*:string:*/ $temp = $a[$i];
          $a[$i] = $a[$j];
          $a[$j] = $temp;
        }
      }
    
    and global variable declarations inside functions such as:
      function setOdd(/*:int:*/ $i) /*:void:*/ {
        global /*:bool:*/ $isOdd;
        $isOdd = ($i % 2 == 0);
      }
    

  5. Finish the grammar rules for expressions. The most challenging part of this is to "factor" the grammar to avoid ambiguity caused by binary-operator expressions, unary-operator expressions, and array-subcript expressions. Study the operator-precedence rules for php-- carefully.

  6. You should now be finished with grammar and, therefore, your phpmm.sable file. But you are not yet done with the assignment!

    Comment and uncomment the appropriate portions of the main method of the ParserTest class so you can begin work on the pretty printer.

  7. Add pretty-printing capabilities for assignment statements and try some simple examples.

  8. Add pretty-printing capabilities for functions.

  9. Add cases for all kinds of statements.

  10. Add cases for all kinds of expressions.

  11. You can test your pretty printer in three ways: visual inspection, idempotence, and by running the output through a php interpreter. In particular, php run on the output of your pretty printer run on the selection-sort example should produce:

      10 9 8 7 6 5 4 3 2 1
       1 2 3 4 5 6 7 8 9 10
    


Files

All the files you need are included in this Eclipse-project archive.


Submission

Upload just the two files (phpmm.sable and Printer.java>) for your project here.