Docsity

midterm solution for compiler design, Exams of Compiler Design

Toggle sidebar
bg1
1
Introduction to Compiler Design 5/9 2007
Midterm Examination Solution
Name: _____________ Reg-No: __Solution_____ score: _____________
1. (20 pts) [CFG ] Consider the grammar G: S Æ S S + | S S # | a
and the string: a a + a #
(a) Give a rightmost derivation for the string. (5pts)
Ans: S ÆSS# ÆSa# Æ SS+a# Æ Sa+a# Æaa+a#.
(b) Give a parse tree for the string. (5 pts)
Ans: S( S( S(a), S(a), + ), S(a), # ).
Note: we use S(t1,t2,,,,tn) to represent a tree with root S and subtrees t1,t2,…,tn.
(c) Is the grammar ambiguous or unambiguous ? why? (5pts)
Ans: No, since it can be shown that for every input, there is at most one parse tree for it.
(d) Describe the language generated by this grammar? ( 5 pts)
Ans: It is the set of the postfix forms of all expressions which are composable by two binary
operators (+ and #) and one constant a.
2. [5 pts] Consider the grammar : S Æ S a | a
Which class of grammar does this grammar belong to?
(a) LL(1) (b) LL(2) (c) LR(0) (d) LR(1) (e) LALR(1)
Ans: ____cde______
3. (30 pts) [LL(1) parsing] Given the following grammar G:
S Æ E $ E Æ T E' E' Æ + T E' | ε
T Æ F T' T' Æ * F T' | ε F Æ (E) | d
(a) Find all nonterminals in G that are nullable. (4pts)
pf3
pf4
pf5

Partial preview of the text

Download midterm solution for compiler design and more Exams Compiler Design in PDF only on Docsity!

Introduction to Compiler Design 5/9 2007

Midterm Examination Solution

Name: _____________ Reg-No: __Solution_____ score: _____________

  1. (20 pts) [CFG ] Consider the grammar G: S Æ S S + | S S # | a and the string: a a + a # (a) Give a rightmost derivation for the string. (5pts) Ans: S ÆSS# ÆSa# Æ SS+a# Æ Sa+a# Æaa+a#.

(b) Give a parse tree for the string. (5 pts) Ans: S( S( S(a), S(a), + ), S(a), # ). Note: we use S(t1,t2,,,,tn) to represent a tree with root S and subtrees t1,t2,…,tn.

(c) Is the grammar ambiguous or unambiguous? why? (5pts)

Ans: No, since it can be shown that for every input, there is at most one parse tree for it.

(d) Describe the language generated by this grammar? ( 5 pts)

Ans: It is the set of the postfix forms of all expressions which are composable by two binary operators (+ and #) and one constant a.

  1. [5 pts] Consider the grammar : S Æ S a | a Which class of grammar does this grammar belong to? (a) LL(1) (b) LL(2) (c) LR(0) (d) LR(1) (e) LALR(1) Ans: ____cde______
  2. (30 pts) [LL(1) parsing] Given the following grammar G: S Æ E $ E Æ T E' E' Æ + T E' | ε T Æ F T' T' Æ * F T' | ε F Æ (E) | d (a) Find all nonterminals in G that are nullable. (4pts)

Ans: ______________T',E'__________ (b) Fill in the following table with FIRST and FOLLOW sets for the grammar: (10 pts)

N FIRST(N) FOLLOW(N) S (,d {} E (,d ),$ E' + ),$ T (,d +,),$ T' * +,),$ F (, d *,+,),$

(c) Complete the following LL(1) table for the grammar: (10 pts)

N \ Σ d^ +^ *^ (^ )^ $ S E$ err err E$ err err E TE' err err TE' err err E' err +TE' err err ε ε T FT' err err FT' err err T' err ε *FT' err ε ε F FÆ d error error FÆ(E) error error

(d) Is this grammar LL(1)? why? (4 pts) Ans: __Yes, since there is at most one rule in every cell of the LL(1) table.


  1. (10pts) Let G = (N,Σ, S,P) be a CFG where N is the set of non-terminal symbols, S is the set of terminal symbols and P the set of production rules. Define the relation First ⊆ N x Σ as the set of all pairs (A,a) such that a can appear in the first position of some sentential forms derived from A, i.e., First = { (A, a) ∈ N X Σ | A Æ* a β for some β ∈ (N U Σ)* }. The relation First can be defined inductively as follows: (a) Basis: (A, a) ∈ First for all A Æ B 1 B 2 …B n a … ∈ P, where n ≥ 0 and B 1 ,…,B n are all nullable. (b) Closure: If (C,c)∈First and B 1 ,..,B n are nullable and A ÆB 1 B 2 …B n C… ∈ P, then (A,c) ∈First.

Now let Last = { (A, a) ∈ N X Σ | ∃β∈(N U Σ)* s.t. A Æ* β a }.

1 2 x 3 x0 x 4 x1 x1 x 5 - x1 x0 x 6 x1 x1 x0 - x 7 x2 x1 x0 x1 x2 x 8 x1 - x0 x1 x1 x1 x 1 2 3 4 5 6 7 8

(b) Draw the minimized finite automata according the result of (a).

  1. (15 pts) [From Regular Expression to DFA] Consider the regular expression α given below: (a+b)* ba(a+b) (a) Write an NFA which recognizes the language represented by α and contains no more than 4 states. Remember to label each state. [5 pts]

(b) Convert the NFA you got at (a) to an equivalent DFA by using the subset construction. Note: you must identify each state of your DFA with the set of old NFA states it corresponds to and make sure all unused states are removed. [10pts]

[15]

[28]

[[3]]

[46] [7]

  1. (25 pts)[LR parsing] Consider the grammar
    1. S Æ E$
    2. E Æ E E + 2. E Æ E E! 3. E Æ a

(a) Build the LR(0) NFA for this grammar [5pts]

a,b

b a a,b

b a a

a b

b

b

a

b

a

(a) : Ans :

(b) : Ans :

(d) Is this an SLR(1) grammar? Give evidence. [5pts] Yes, since it is already an LR(0) grammar and every LR(0) grammar is a SLR(1) grammar.