Skip to content

Compiler-As-2

Question 1

Given the set of tokens {σ,,×,r,,=,id,lit,(,)} (pay attention to the underline "_ ") and the following CFG grammar:

  1. Eσ_P(E)
  2. Eσ_P(E)×E
  3. Er×E
  4. Er
  5. PPA
  6. PA
  7. AI=I
  8. Iid
  9. Ilit

and answer the following questions. Show the detail of each step.

1. Use the grammar to do left-most and right-most derivation on the input "σ_id=lit(r)" and draw the parse tree. (12 pt)

Left-most derivation:

  1. E
  2. σ_P(E)
  3. σ_A(E)
  4. σ_I=I(E)
  5. σ_id=I(E)
  6. σ_id=lit(E)
  7. σ_id=lit(r)

Right-most derivation:

  1. E
  2. σ_P(E)
  3. σ_P(r)
  4. σ_A(r)
  5. σ_I=I(r)
  6. σ_I=lit(r)
  7. σ_id=lit(r)

2. Eliminate the left recursions. (6 pt)

There is only one production rule that has left recursion, which is PPA. We can eliminate it by introducing a new non-terminal symbol P and rewrite the grammar as follows:

  • PPA|A

Eliminate the left recursions:

  • PAP
  • PAP|ϵ

Due to Pϵ ; Therefore,

  • PAP
  • PAP|ϵ
  • PA

can be simplified as follows:

PAPPAP|ϵ

Rewrite the grammar as follows:

  1. Eσ_P(E)
  2. Eσ_P(E)×E
  3. Er×E
  4. Er
  5. PAP
  6. PAP|ϵ
  7. AI=I
  8. Iid
  9. Ilit

3. Base on the grammar without left recursions from Q2, left factorize the grammar. (6 pt)

There are several productions that have common prefixes:

  • Eσ_P(E)|σ_P(E)×E
  • Er×E|r

We can left factorize the grammar as follows:

  • Eσ_P(E)E

  • Eϵ|×E

  • ErE

  • E×E|ϵ

And it can be simplified as follows:

  • Eσ_P(E)E|rE
  • Eϵ|×E

Rewrite the grammar as follows:

  1. Eσ_P(E)E|rE
  2. Eϵ|×E
  3. PAP
  4. PAP|ϵ
  5. AI=I
  6. Iid|lit

4. Base on the grammar from Q3, find the First set and the Follow set. (10 pt)

First set

For all terminals:
Xσ_×r=idlit()
First(X){σ}{_}{×}{r}{}{=}{id}{lit}{(}{)}
For Eσ_P(E)E|rE
XEEPPAI
First(X){σ,r}{}{}{}{}{}
For Eϵ|×E
XEEPPAI
First(X){σ,r}{×,ϵ}{}{}{}{}
For PAP
XEEPPAI
First(X){σ,r}{×,ϵ}{FIRST(A)}{}{}{}
For PAP|ϵ
XEEPPAI
First(X){σ,r}{×,ϵ}{FIRST(A)}{,ϵ}{}{}
For AI=I
XEEPPAI
First(X){σ,r}{×,ϵ}{FIRST(A)}{,ϵ}{FIRST(I)}{}
For Iid|lit
XEEPPAI
First(X){σ,r}{×,ϵ}{FIRST(A)}{,ϵ}{FIRST(I)}{id,lit}
Final First set:
Xσ_×r=idlit()
First(X){σ}{_}{×}{r}{}{=}{id}{lit}{(}{)}
XEEPPAI
First(X){σ,r}{×,ϵ}{id,lit}{,ϵ}{id,lit}{id,lit}

Follow set:

XEEPPAI
First(X){σ,r}{×,ϵ}{id,lit}{,ϵ}{id,lit}{id,lit}
E is the start symbol, FOLLOW(E){$}
XEEPPAI
Follow(X){$}{}{}{}{}{}
For Eσ_P(E)E|rE
XEEPPAI
Follow(X){$,FIRST())/ϵ}{FOLLOW(E)}{FIRST(()/ϵ}{}{}{}
For Eϵ|×E
XEEPPAI
Follow(X){$,FIRST())/ϵ,FOLLOW(E))}{FOLLOW(E)}{FIRST(()/ϵ}{}{}{}
For PAP
XEEPPAI
Follow(X){$,FIRST())/ϵ,FOLLOW(E))}{FOLLOW(E)}{FIRST(()/ϵ}{FOLLOW(P)}{FIRST(P)/ϵ,FOLLOW(P)}{}
For PAP|ϵ
XEEPPAI
Follow(X){$,FIRST())/ϵ,FOLLOW(E))}{FOLLOW(E)}{FIRST(()/ϵ}{FOLLOW(P)}{FIRST(P)/ϵ,FOLLOW(P)}{}
For AI=I
XEEPPAI
Follow(X){$,FIRST())/ϵ,FOLLOW(E))}{FOLLOW(E)}{FIRST(()/ϵ}{FOLLOW(P)/ϵ}{FIRST(P)/ϵ,FOLLOW(P)}{FOLLOW(A),=}
For Iid|lit

These are terminal symbols. So, Follow(I) is only influenced by the productions involving A.

Final Follow set:
XEEPPAI
Follow(X){$,)}{$,)}{(}{(}{,(}{,=,(}

5. Base on the grammar from Q3, construct the LL(1) parsing table. (6 pt)

LL(1) parsing table

  1. Eσ_P(E)E|rE
  2. Eϵ|×E
  3. PAP
  4. PAP|ϵ
  5. AI=I
  6. Iid|lit
σ_×r=idlit()$
EEσ_P(E)EErE
EE×EEϵEϵ
PPAPPAP
PPAPPϵ
AAI=IAI=I
IIidIlit

6. Base on the original grammar construct the DFA from the set of LR(0) items (18 pt)

  1. Eσ_P(E)|σ_P(E)×E|r×E|r
  2. PPA|A
  3. AI=I
  4. Iid|lit

Construct the corresponding augmented grammar

  1. EE
  2. Eσ_P(E)|σ_P(E)×E|r×E|r
  3. PPA|A
  4. AI=I
  5. Iid|lit

Construct the LR(0) items:

I0=closure(EE)={EE,Eσ_P(E),Eσ_P(E)×E,Er×E,Er}

  • goto(I0,E)=closure({EE})={EE}=I1
  • goto(I0,σ)=closure({Eσ_P(E),Eσ_P(E)×E})={Eσ_P(E),Eσ_P(E)×E}=I2
  • goto(I0,r)=closure({Er×E,Er})={Er×E,Er}=I3

For I1={EE} nothing can be done

For I2={Eσ_P(E),Eσ_P(E)×E}

  • goto(I2,_)=closure({Eσ_P(E),Eσ_P(E)×E})={Eσ_P(E),Eσ_P(E)×E,PPA,PA,AI=I,Iid,Ilit}=I4

For I3={Er×E,Er}

  • goto(I3,×)=closure({Er×E})={Er×E,Eσ_P(E),Eσ_P(E)×E,Er×E,Er}=I5

For I4={Eσ_P(E),Eσ_P(E)×E,PPA,PA,AI=I,Iid,Ilit}

  • goto(I4,P)=closure({Eσ_P(E),Eσ_P(E)×E,PPA})={Eσ_P(E),Eσ_P(E)×E,PPA}=I6
  • goto(I4,A)=closure({PA})={PA}=I7
  • goto(I4,I)=closure({AI=I})={AI=I}=I8
  • goto(I4,id)=closure({Iid})={Iid}=I9
  • goto(I4,lit)=closure({Ilit})={Ilit}=I10

For I5={Er×E,Eσ_P(E),Eσ_P(E)×E,Er×E,Er}

  • goto(I5,E)=closure({Er×E})={Er×E}=I11
  • goto(I5,σ)=closure({Eσ_P(E),Eσ_P(E)×E})={Eσ_P(E),Eσ_P(E)×E}=I2
  • goto(I5,r)=closure({Er×E,Er})={Er×E,Er}=I3

For I6={Eσ_P(E),Eσ_P(E)×E,PPA}

  • goto(I6,()=closure({Eσ_P(E),Eσ_P(E)×E})={Eσ_P(E),Eσ_P(E)×E,Eσ_P(E),Eσ_P(E)×E,Er×E,Er}=I12
  • goto(I6,)=closure({PPA})={PPA,AI=I,Iid,Ilit}=I13

For I7={PA} nothing can be done

For I8={AI=I}

  • goto(I8,=)=closure({AI=I})={AI=I,I=id,Ilit}=I14

For I9={Iid} nothing can be done For I10={Ilit} nothing can be done For I11={Er×E} nothing can be done

For I12={Eσ_P(E),Eσ_P(E)×E,Eσ_P(E),Eσ_P(E)×E,Er×E,Er}

  • goto(I12,E)=closure({Eσ_P(E),Eσ_P(E)×E})={Eσ_P(E),Eσ_P(E)×E}=I15

  • goto(I12,σ)=closure({Eσ_P(E),Eσ_P(E)×E})={Eσ_P(E),Eσ_P(E)×E,PPA,PA,AI=I,Iid,Ilit}=I4

  • goto(I12,r)=closure({Er×E,Er})={Er×E,Er}=I3

For I13={PPA,AI=I,Iid,Ilit}

  • goto(I13,A)=closure({PPA})={PPA}=I16
  • goto(I13,I)=closure({AI=I})={AI=I}=I8
  • goto(I13,id)=closure({Iid})={Iid}=I9
  • goto(I13,lit)=closure({Ilit})={Ilit}=I10

For I14={AI=I,I=id,Ilit}

  • goto(I14,I)=closure({AI=I})={AI=I}=I17
  • goto(I14,id)=closure({Iid})={Iid}=I9
  • goto(I14,lit)=closure({Ilit})={Ilit}=I10

For I15={Eσ_P(E),Eσ_P(E)×E}

  • goto(I15,))=closure({Eσ_P(E),Eσ_P(E)×E})={Eσ_P(E),Eσ_P(E)×E}=I18

For I16={PPA} nothing can be done For I17={AI=I} nothing can be done

For I18={Eσ_P(E),Eσ_P(E)×E}

  • goto(I18,×)=closure({Eσ_P(E)×E})={Eσ_P(E)×E,Eσ_P(E),Eσ_P(E)×E,Er×E,Er}=I19

For I19={Eσ_P(E)×E,Eσ_P(E),Eσ_P(E)×E,Er×E,Er}

  • goto(I19,E)=closure({Eσ_P(E)×E})={Eσ_P(E)×E}=I20
  • goto(I19,σ)=closure({Eσ_P(E),Eσ_P(E)×E})={Eσ_P(E),Eσ_P(E)×E}=I2
  • goto(I19,r)=closure({Er×E,Er})={Er×E,Er}=I3

For I20={Eσ_P(E)×E} nothing can be done

Here is the list of sets of items.

iIi
0{EE,Eσ_P(E),Eσ_P(E)×E,Er×E,Er}
1{EE}
2{Eσ_P(E),Eσ_P(E)×E}
3{Er×E,Er}
4{Eσ_P(E),Eσ_P(E)×E,PPA,PA,AI=I,Iid,Ilit}
5{Er×E,Eσ_P(E),Eσ_P(E)×E,Er×E,Er}
6{Eσ_P(E),Eσ_P(E)×E,PPA}
7{PA}
8{AI=I}
9{Iid}
10{Ilit}
11{Er×E}
12{Eσ_P(E),Eσ_P(E)×E,Eσ_P(E),Eσ_P(E)×E,Er×E,Er}
13{PPA,AI=I,Iid,Ilit}
14{AI=I,Iid,Ilit}
15{Eσ_P(E),Eσ_P(E)×E}
16{PPA}
17{AI=I}
18{Eσ_P(E),Eσ_P(E)×E}
19{Eσ_P(E)×E,Eσ_P(E),Eσ_P(E)×E,Er×E,Er}
20{Eσ_P(E)×E}

7. Convert the DFA from Q6 to an SLR(1) parsing table. (15 pt)

  1. EE
  2. Eσ_P(E)
  3. Eσ_P(E)×E
  4. Er×E
  5. Er
  6. PPA
  7. PA
  8. AI=I
  9. Iid
  10. Ilit

SLR(1) parsing table

XEEPAI
FIRST(X)FIRST(E){σ,r}FIRST(A)FIRST(I){id,lit}
XEEPAI
FIRST(X){σ,r}{σ,r}{id,lit}{id,lit}{id,lit}
XEEPAI
FOLLOW(X)$),$,(,(,(,=
STATEσ_×r=idlit()$EPAI
0S2S31
1acc
2S4
3S5R5R5
4S9S10678
5S2S311
6S13S12
7R7R7
8S14
9R9R9R9
10R10R10R10
11R4R4
12S4S315
13S9S10168
14S9S1017
15S18
16R6R6
17R8R8R8
18S19R2R2
19S2S320
20R3R3

8. Use the SLR(1) parsing table to parse "σ_id=lit(r)". Show the configuration and the output for each step. (10 pt)

StackInputOutput
0σ_id=lit(r)$
0σ2_id=lit(r)$Shift 2
0σ2 _4id=lit(r)$Shift 4
0σ2 _4 id9=lit(r)$Shift 9
0σ2 _4 I8=lit(r)$Reduce 9. Iid
0σ2 _4 I8 =14lit(r)$Shift 14
0σ2 _4 I8 =14 lit10(r)$Shift 10
0σ2 _4 I8 =14 I17(r)$Reduce 10. Ilit
0σ2 _4 A7(r)$Reduce 8. AI=I
0σ2 _4 P6(r)$Reduce 7. PA
0σ2 _4 P6 (12r)$Shift12
0σ2 _4 P6 (12 r3)$Shift 3
0σ2 _4 P6 (12 E15)$Reduce 5. Er
0σ2 _4 P6 (12 E15 )18$Shift 18
0E1$Reduce 2. Eσ_P(E)
0E1$Accept

Question 2

Given the following grammar:

  1. EE+E
  2. Eid

9. Show that the grammar is ambiguous. (6 pt)

The grammar G is ambiguous if there is a sentence in L(G) from which it is possible to construct multiple parse trees (using any type of derivation).

For example the sentence id+id+id can be derived in two different ways:

  1. EE+EE+E+Eid+E+Eid+id+Eid+id+id
  2. EE+Eid+Eid+E+Eid+id+Eid+id+id

Obviously, the sentence id+id+id can be derived in two different ways, which means the grammar is ambiguous.

10. Find a sentence in the language such that the sentence is ambiguous but left-most derivation and right-most derivation can produce a same parse tree. You need to prove your answer. (9 pt)

For example the sentence id+id+id can be derived in two different ways:

Left-Most Derivation:

  1. E
  2. E+E
  3. E+E+E
  4. id+E+E
  5. id+id+E
  6. xid+id+id

Right-Most Derivation:

  1. E
  2. E+E
  3. E+id
  4. E+E+id
  5. E+id+id
  6. id+id+id