


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Item Sets and DFA Construction: LR(0), LR(1), and LALR(1)
S ---> A B A ---> S y | x B ---> y S
Is the grammar SLR? Why or why not? Reason out your answer without constructing the entire parsing table!
S ---> d D d S ---> e D e S ---> d E e S ---> e E d D ---> f E ---> f
Is the grammar SLR? Why or why not? Reason out your answer without constructing the entire parsing table!
Attribute Grammars
Expressions in the Lisp functional programming language are formed using nested parentheses and operators which apply to exactly two operands. Below are some example Lisp expressions with their infix and postfix equivalents:
(= x (+ a (* b 5))) x = (a+(b5)) b 5 * a + x = (= y (- ( d (+ a 3)) (/ 8 d))) y = (((a+3)*d)-(8/d)) a 3 + d * 8 d / - y =
For Lisp expressions, assume that multiplication and division have equal precedence, which is higher than addition and subtraction (which also have equal precedence).
E ---> E + T E ---> E - T E ---> T T ---> T * F T ---> T / F T ---> F F ---> ( E ) F ---> id F ---> int
Write an attribute grammar the converts expressions to their Lisp equivalents. For a + b * c, the Lisp equivalent is ( + a ( * b c ) ), where there is one operator and two operands within each (...). Show that your attribute grammar works for the expression (a + b) * d - (c / d). Clearly name your attributes and identify each attribute as either inherited or synthesized.
E --> ( = id L) L --> ( A L1 L1) L --> ( M L1 L1) L1 --> int | id | L A --> + | - M --> * | /
a. Write an attribute grammar that converts Lisp expressions into their infix equivalents. That is, while parsing a Lisp expression, the grammar should generate the infix equivalent shown above. Demonstrate that your attribute grammar works by evaluating the grammar for the Lisp expression: ( = y (- ( * d ( + a 3 )) ( / 8 d ))). (Hint: Notice that for the infix expression, I've included a complete set of parentheses to indicate the grouping of the operands. This should make the translation easier than only having to put in parentheses some of the time.)
b. Write an attribute grammar that converts Lisp expressions into their infix equivalents. That is, while parsing a Lisp expression, the grammar should generate the infix equivalent shown above. Demonstrate that your attribute grammar works by evaluating the grammar for the Lisp expression: ( = y (- ( * d ( + a 3 )) ( / 8 d ))). (Hint: Notice that for the infix expression, I've included a complete set of parentheses to indicate the grouping of the operands. This should make the translation easier than only having to put in parentheses some of the time.)