Exam 1 - Practice Test - Programming Language | CSCI 4430, Exams of Programming Languages

Material Type: Exam; Class: PROGRAMMING LANGUAGES; Subject: Computer Science; University: Rensselaer Polytechnic Institute; Term: Fall 2008;

Typology: Exams

2011/2012

Uploaded on 02/17/2012

koofers-user-7ag-1
koofers-user-7ag-1 🇺🇸

5

(1)

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Exam 1 Practice Problems
Posted Monday, October 13, 2008 (Exam on Thursday, October 23)
III LL and LR Grammars.
P2. Consider the following grammar:
S[S Y ]|a
Y²|+S Z |Z b
Z²| S Y c
S, Y and Zare nonterminals and a, b, c, +,,[ and ] are terminals.
(i) Compute the FIRST and FOLLOW sets for S,Yand Z.
(ii) Construct the LL(1) parsing table for this grammar.
(iii) Is this grammar LL(1)? Why or why not?
P3. Consider the following grammar over the terminals +, - (negation operator) and id:
SE
EE+E| E|id
(i) Construct the DFA (i.e., CFSM) for this grammar. You need to (a) show all states,
(b) show all items in a state, including the computation of closure, (c) show all transi-
tions with labels, and (d) write all necessary ”reduce by...” labels on states.
1
pf3
pf4
pf5

Partial preview of the text

Download Exam 1 - Practice Test - Programming Language | CSCI 4430 and more Exams Programming Languages in PDF only on Docsity!

Exam 1 Practice Problems

Posted Monday, October 13, 2008 (Exam on Thursday, October 23)

III LL and LR Grammars.

P2. Consider the following grammar: S → [S Y ] | a Y → ≤ | + S Z | Z b Z → ≤ | − S Y c

S, Y and Z are nonterminals and a, b, c, +, −, [ and ] are terminals.

  • (i) Compute the FIRST and FOLLOW sets for S, Y and Z.
  • (ii) Construct the LL(1) parsing table for this grammar.
  • (iii) Is this grammar LL(1)? Why or why not?

P3. Consider the following grammar over the terminals +, - (negation operator) and id : S → E E → E + E | − E | id

  • (i) Construct the DFA (i.e., CFSM) for this grammar. You need to (a) show all states, (b) show all items in a state, including the computation of closure, (c) show all transi- tions with labels, and (d) write all necessary ”reduce by...” labels on states.

1

  • (ii) For each state with a conflict list the state, the lookahead token and the type of conflict (i.e., shift-reduce, or reduce-reduce).
  • (iii) Draw the parse tree(s) for this expression: id + −id + id
  • (viii) Rewrite the grammar into an equivalent unambiguous grammar.

IV Attribute Grammars.

P1. Consider the grammar for postfix expressions (here E and Op are nonterminals and id , +,−,* and / are terminals):

E → E E Op | id Op → + | − | ∗ | /

Give an attribute grammar which translates postfix expressions into parenthesized infix ex- pressions. For example, expression AAB + / will be translated into (A/(A + B)).

P2. Consider this subset of the grammar for parenthesized arithmetic expressions:

E → E + T E → T T → T ∗ F T → F F → id F → (E)

Write an attribute grammar which constructs an abstract syntax tree for a parse tree. Your attribute grammar should get rid of redundant enclosing parentheses: that is, if an expression e is enclosed by multiple sets of parentheses, the redundant sets of parentheses should be removed. For example, the abstract syntax tree for expression (((a*b))+(((c+d)))) should be as follows:

(+) /
(*) (+) / \ /
a b c d

Note that only redundant sets of enclosing parentheses are removed (i.e., ((ab)) becomes (ab) and (((c+d))) becomes (c+d)). Parentheses that become redundant due to associativity and precedence are not removed (e.g., (a*b) remains parenthesized even though the parentheses are redundant due to the higher precedence of ∗ over +). You may look at slides 29 and 30 in Lecture 11 which describe the attribute grammar for the construction of the abstract syntax tree.

P3. The attribute flow graph is constructed for a parse tree given the attribute grammar asso- ciated to the underlying context-free grammar. It shows attribute dependencies. For example, recall the LL(1) version of the context-free grammar for attribute expressions and the attribute grammar which computes the value of arithmetic expressions (this grammar is described in Chapter 4 of textbook and repeatedly in lectures). Consider expression 3+5. Its corresponding parse tree is as follows (T1, F1, T’1, E’1 are the indexed nonterminals T 1 , F 1 , T 1 ′, E 1 ′):

E /
T E’ / \ /|
F T’ + T1 E’ | | | \ | num:3 eps F1 T’1 eps | | num1:5 eps

The attribute flow graph for this parse tree is as follow:

num.val → F.val → T ′.sub → T ′.val → T.val → E′.sub → E 1 ′.sub

num 1 .val → F 1 .val → T 1 ′.sub → T 1 ′.val → T 1 .val → E′ 1 .sub

E′ 1 .sub → E′ 1 .val → E′.val → E.val

Draw the parse tree for expression 3*5+7+8 and the corresponding attribute flow graph. Is it possible to construct an expression which will result in a cyclic attribute flow graph?