Homework 2

Part 1

Part 1 is about the language of set expressions defined as follows:

  1. A hash-separated list of zero or more countries enclosed in curly braces is a set expression.

  2. If S1 and S2 are both set expressions, then so are each of the following: S1 ∪ S2
    S1 ∩ S2
    S1 + S2
    ( S1 )

In a set expression, parenthesis has the highest precedence; intersection and union have the same, second highest precedence; addition has the lowest precedence. Intersection, union and addition are all left associative.

Valid examples:

Question:

Write an unambiguous CFG for the language of set expressions so that parse trees correctly reflect the precedences and associativities of the operators. The terminals used are already for tokens. Use lower-case names for nonterminals and use the following terminals:

COUNTRY   // one country in a set
∪     // union
∩     // intersection
+      // plus
(      // left paren
)      // right paren
{      // left curly brace
}      // right curly brace
#      // hash

Part 2

For Part 2 you will define a syntax-directed translation for the CFG given below, which defines a very simple programming language.

program → MAIN LPAREN RPAREN LCURLY list RCURLY
list → list oneItem
     | epsilon

oneItem → decl
        | stmt

decl → BOOL ID SEMICOLON
     | INT ID SEMICOLON

stmt → ID ASSIGN exp SEMICOLON
     | IF LPAREN exp RPAREN stmt
     | LCURLY list RCURLY
exp → exp PLUS exp
    | exp LESS exp
    | exp EQUALS exp
    | ID
    | BOOLLITERAL
    | INTLITERAL

Question:

Write a syntax-directed translation for the CFG given above, so that the translation of an input program is the set of variables used somewhere in the program. Note: here the term use is in contrast to declaration; any appearance of a variable that is not a variable declaration counts as a use of that variable. For the example code in Question 2, the translation should be { x, a, b }.

Your translation rules should use the following notation:

Note that you should not try to use something like "{ a, b }" to mean a set with two elements; instead, use set union to combine two sets that each contain one element.

Use the notation that was used in class and in the on-line readings; i.e., use nonterminal.trans to mean the translation of a nonterminal, and terminal.value to mean the value of a terminal. Assume that ID.value is a String (the name of the identifier). Use subscripts for translation rules that include the same nonterminal or the same terminal more than once.