Homework Assignment 5


  1. Explain the difference between the task performed by a lexical analyzer and the task performed by a parser.

  2. Consider the following grammar discussed in class:
    Exp -> Exp + Exp | Exp * Exp | ( Exp ) 
        |  - Exp | id | num
        
    Using this grammar (a) write a left derivation for the string "(A + C) * (8 + B + -(B + C))", and (b) show the parse tree for your derivation. Assume that * has precedence over + and that all operators are left associative.

  3. Write a context free grammar that specifies the syntax for C functions. For the statements that go into the function body it is fine to go no deeper than individual statements. In other words, you may assume that stmt is a terminal symbol that does not have to be expanded further into assignment statements, function calls, control constructs, etc. You do have to specify that it is ok to have zero or more statements in the function body and that a statement may be a block of statements enclosed in {}'s. For this assignment allowable return and parameter types are int, char, float, pointer versions of these three types, struct id, and struct id *. C functions have the form:
    type name (type param1, type param2, ..., type param n) body
    

  4. Write an Antlr specification for the following grammar. An ID is any string of one or more upper/lowercase letters and a NUM is a floating point number that may be either an integer with 1 or more digits from 0-9 or a floating point number with zero or more digits before the decimal point, a decimal point '.', and one or more digits after the decimal point. A number may have an optional '-' sign before it. Examples of valid numbers include 0, 012, 003, -88, 3.45, .5, and .0005. You should recognize this grammar as roughly corresponding to the expressions recognized by the expression tree problem in previous homework assignments:
         
         pgm -> (stmt | exp)+
         
         stmt -> = ID exp
    
         exp -> + exp exp | - exp exp | * exp exp | / exp exp
                ID | NUM
        
    Your productions should perform the following actions:
    1. pgm -> exp: print the value of the expression.
    2. pgm -> stmt: no action required.
    3. stmt production: assign the value of the expression to id and print the value of the expression,
    4. exp productions: calculate the value of the expression.

    Each value should be printed on a separate line. If you have a question about whether or not an expression is valid or what value should be printed for an expression or statement, you can run the antlr expression evaluator that I have written which is in /home/bvz/cs365/hw/hw5. You will need to copy the .class and .tokens files to your directory, and then type:

        java -cp .:..:/usr/share/java/antlr3.jar ExprEvaluator < inputfile
        
    where inputfile is the name of the file containing your expressions.

    Name your antlr specification Prefix.g and your driver file ExprEvaluator.java (you can use an appropriately modified version of the Test.java file discussed in class).


What to Submit

Submit the following files (do not jar them--the submit script will pack them into a tar file):

  1. either an ascii file that contains the answers to questions 1-3 or a pdf file that contains the answers to questions 1-3. Use either ascii art or a drawing editor to draw your parse tree. A scanned drawing of the parse tree is also okay, if it is included as a clearly identified pdf file.
  2. Prefix.g
  3. ExprEvaluator.java
The TAs will test your grammar by compiling it themselves and then running it with the antlr3.jar file. The reason you are not being asked to submit a jar file is that you would need to include the antlr3.jar file in your jar file, and the java virtual machine cannot easily access class files that are bundled in a jar file that is bundled inside another jar file.