Write an Attribute Grammar



next up previous
Next: Evaluate the Use Up: Assignment 4: Attribute Grammars Previous: Assignment 4: Attribute Grammars

Write an Attribute Grammar

Write a context-free grammar for parenthesized arithmetic expressions that contains the operators `+', `-', `*', `/', and , where is right-associative and has highest precedence, the other operators are left associative and have their usual precedences. Parentheses are used to override the built-in associativities and precedences. Use the simple ambiguous grammar and assume that the bison-like parser ``does the right thing'' in building a parse tree.

Write an attribute grammar that prints out the expression in infix form and that checks each of the following properties, generating an error message if the property is not satisfied. You may invent the side-effect-free attribution language, or take it from one of the papers. Be sure to `declare' the attributes, indicating for each whether it is inherited or synthesized. To clarify your answer, explain how each of the checks is carried out (i.e. provide some intuition).

  1. The maximum depth of nested parentheses is 3.
  2. The maximum number of operators used in an expression is 10.
  3. If an expression has more divisions than multiplications, then it may not have any subtractions. (Don't ask me why!)
  4. All the parentheses are necessary; i.e., the associativity or precedence would be different if they were omitted.
Note: the first 3 conditions are taken from a homework exercise in a textbook by Slonneger and Kurtz.

Reader Cs
Mon Oct 30 17:51:33 PST 1995