Title: Test 1 Post Mortem
1Test 1 Post Mortem
CSCE 531 Compiler Construction
- Topics
- Questions and points
- Grade Distribution
- Answers
February 20, 2006
2Questions and Point Evaluations
- 1a. Reg expr?NFA 14 pts
- 2. Subset Construction 14 pts
- Grammars, derivations
- Flex/Lex patterns 4 pts
- L( (ab)aa(ab) ) 5 pts
- Reg expr as before bs 5pts
- Left-recusion
- Left recursion 4 pts
- Left factor 4 pts
- LL(1) 2pts
- MA,a 4pts
- 5 Recursive Descent parsing
- Transition diagram 7pts
- recursive parsing 7pts
- LL(1) table construction pts
- Modifications 1 pt
- FIRST 4 pts
- FOLLOW 4 pts
- Table 5 pts
- LR(0) sets of items 14 pts
3Grade Distribution
41. Thompson Construction Reg Expr ? NFA
- (a b)(ab)a solution build from innermost out
e
a
e
e
a
e
b
e
a
e
e
e
e
b
e
e
52. Subset Construction NFA ? DFA
Accepting states those that contain s6
63. Regular Languages
- 3a. What do the following Lex/Flex patterns
match? - x? --- zero or one xs (spaces on the
dynamically bound question) - abc --- any single character not a, b or
c - ab --- a single a followed by one or more
bs - ab3,5 --- a single a followed by 3, 4, or 5
bs - 3b. Describe the language denoted by the regular
expression (ab)aa(ab) - Any string of as and bs such that the third
and second to last characters are as - Or strings of as and bs that end in aaa or aab
- 3c. Regular expression for strings from a,b,c
all as before any b - (a c) (b c)
73a. Mistakes imprecise answers, etc.
- x? --- zero or one xs (spaces )
- An optional match
- Matches anything that is not an a, b or c
- abc --- any single character not a, b or
c - Negated character class any thing other than
abc - Any string other than abc
- Any string that does not contain the characters
a, b and c - ab --- a single a followed by one or more
bs - ab3,5 --- a single a followed by 3, 4, or 5
bs - Matches 3 to 5 bs
8- 3b. Mistakes
- A string of as and bs which contain two
consecutive as (ab)aa(ab) - 3c. Mistakes, etc. Notations
- L3c the language specified in 3c
- La the language specified by the students
answer - Mistakes
- abc c a b a c b --- acacb is
not in La . - (a c) b c -- close but acbcb is not in La
. - ((a c) b c) -- close but abcabc is in La .
94. Points(4/4/2/4) Left Recursion
- S? Sa bL
- L ? ScL Sd a
- 4a. Equivalent non left recursive grammar
- The only left recursive production is S? Sa
- The problem would have been more interesting if
S?Lb were to replace S?bL because then we would
have some non-direct recursion to eliminate - To handle S?Sa ß (aa, ßbL) replace with S?
ßS and S? aS ?, so - S ? bLS
- S ? aS ?
- L ? ScL Sd a
10Common error rewrite Eliminate L
- S? Sa bL
- L ? ScL Sd a
- The idea some students used is to substitute for
L the right habd sides of the L productions to
obtain - S? Sa bScL bSd ba
- But whats wrong with this?
- We still have an L!!!
- This is because the L productions are recursive
so no matter how many times you rewrite it you
can never get rid of the L.
114b Left Factor this resulting grammar
- S ? bLS
- S ? aS ?
- L ? ScL Sd a
- The only portion needing factoring is L?ScL
Sd - So
- L ? SL a
- L ? cL d
- With the S and S productions from above
- S ? bLS
- S ? aS ?
124. continued
- 4c. (2pts) a grammar is LL(1) if in constructing
the predictive parse table there are no multiply
defined entries - There is an alternate definition in the text
page 192. - G is LL(1) if and only if if A?a ß are two
productions then the following conditions hold - FIRST(a) n FIRST(ß) F
- At most one of a and ß can derive the empty
string ? - If ß ? ? then a does not derive any string
beginning with a token in FOLLOW(A) - 4d. (4 pts) Pop A off the stack and push X Y
and Z in reverse order
135 Recursive Descent Parsing
- 5a. Construct a transition diagram for X given
the productions X ? aAB bB ?
?
B
A
a
b
B
145b. Recursive Descent Parsing
- 5b. Construct a recursive parsing routine for X
assuming routines for A and B. - Reference Transition diagrams in fig 4.12 used to
generate parsing routine in Chapter 2, page 75. - ParseX ()
- if (current_token a)
- parseA()
- parseB()
- else if (current_token b)
- parseB()
- else return / epsilon/
156 FIRST, FOLLOW Predictive Parsing
6a. Non required 6b. FIRST - FIRST (ta) t
for any token t
- Considering productions
- Z?d add d to FIRST(Z)
- Y?c add c to FIRST(Y)
- Y?? add ? to FIRST(Y)
- X?a add a to FIRST(X)
- X?Y add FIRST(Y) to FIRST(X)
- Then considering
- Z?XYZ add FIRST(X) to FIRST(Z)
- Since X?Y?? add to FIRST(Y) to FIRST(Z)
- And since X?? and Y?? add FIRST(Z) to FIRST(Z)
duh!
166c. FOLLOW
6c. Add to FOLLOW(Z) Here we will use FIRST(N)
to denote FIRST(N) ?
- Considering productions
- Z?XYZ add
- FIRST(Y) to FOLLOW(X)
- FIRST(Z) to FOLLOW(Y)
- Since Z? XYZ ? X?Z
- FIRST(Z) to FOLLOW(X)
- And considering
- X?Y add FOLLOW(X) to FOLLOW(Y)
176d. Predictive Parse Table
186d. Predictive Parse Table Analysis
- Consider Productions in order and add them to
MN,t according to algorithm 4.4 - Z?XYZ
- Using rule 2 add Z?XYZ for each a in FIRST(XYZ)
- But since ? is in FIRST(X) and FIRST(Y)
- FIRST(XYZ) FIRST(X) U FIRST(Y) U FIRST(Z)
a, c, d - Rule 3 does not apply as XYZ does not derive ?
- Z ? d
- Using rule 2 again add Z?d for MZ, d ? double
entry ? not LL(1) - Y?c similarly adds Y?c to MY, c
- And X? a adds X? a to MX, a
- Y??, using rule3 add to MY, b foreach token b
in FOLLOW(Y)a,c,d - X?Y using rule 2 add to a to MX, a for each a
in FIRST(Y)c - X?Y, using rule3 add to MX, b for each token
b in FOLLOW(X)a,c,d
197. LR(0) Sets of Items
- D ? T L ? Tokens , id, ,,
int, float - L ? L , id id
- T ? int float
- Augment with S ? D
- J0 closure(S ? ? D)
- S ? ? D, D? ? T L , D??, T??int,
T? ?float - Now consider GOTO(J0, X) for each X just to right
of dot - J1 GOTO(J0, D) closure(S ? D ? S ?
D ? - J2 GOTO(J0, T) closure(D? T ? L
- D? T ? L , L ? ? L , id, L ? ? id
207. LR(0) Sets of Items
- J3 GOTO(J0, int) closure(T? int ?
T? int ? - J4 GOTO(J0, float) closure(T? float ?
T?float ? - J5 GOTO(J2, L) closure(D? T L ? , L ? L
? , id) - (D? T L ? , L ? L ? , id)
- J6 GOTO(J2, id) closure(L ? id ? ) L
? id ? - J7 GOTO(J5, ) closure( L ? T L ? )
L ? T L ? - J8 GOTO(J5, , ) closure( L ? L , ? id)
L ? L , ? id - J9 GOTO(J8, id ) closure( L ? L , id ?)
L ? L , id?
21What I should have also asked .
- Leftmost derivations, parse trees
- Ambiguous grammars