Title: Homework 1
1Homework 1
- CSC3120 Tutorial 4
- Yangfan Zhou
- 16th, 17th February 2009
2Problem 1
3Problem 1
- Purposes and hints
- How to perform left-factoring
- Observe Same string a at the left-hand side
- Extract them to the left-hand side A ? a (b g)
- Use a new symbol (say A) to denote the things
inside () - A ? a A
- A ? b g
4Problem 1
5Problem 1
- Purposes and hints (cont)
- How to eliminate left-recursion
- Since ß always appears as the first symbol A ? ß
A - A can contain arbitrary number of a, so A aA
e - Another Example A ? Acd e f
- e or f can appear as the first symbol A ? eA
fA - A can contain arbitrary number of cd, so A
cdAe
6Problem 1
7Problem 1
- Purposes and hints (cont)
- The above two steps are prerequisites for
top-down parsing!! - Why (Important for you to understand top-down
parsing) - To avoid many backtracks
- To avoid dead loop
- Read Slides 24-34 for eliminating left recursion
and Slides 39-41 for left-factoring in Lecture 2
(presentation) - Sections 4.3.3 and 4.3.4 in the purple dragon book
Do you have this book?
8Problem 2
9Problem 2
- Purposes and hints
- How to find the FIRSTk set
- FIRSTk (X) is the possible first k (or less than
k, if X produces a terminal string with size less
than k) terminal symbols in what X can produce - A - bA a c e.g., its FIRST2 bb, ba, bc,
a, c - How to find the FOLLOWk set
- FOLLOWk (A) is the possible first k (or less than
k, if A is followed by a terminal string with
size less than k) terminal symbols that follow A - Read Slides 44-62 (esp Slides 54-56 for the
FOLLOW set) in Lecture 2 (presentation) - Important for solving homework questions and
10Problem 2
Think about which terminal symbols can follow S
By observation
You may need to figure out First1, First2 in
order to work out First 3 Follow3 is based on
First3
11Problem 3
12Problem 3
- Purposes and hints
- The extended context free grammar used in this
question - There is Kleene stars
- What is a Kleene closure
- There are something in between ( and )
- (aaa) means e, aaa, aaaaaa, aaaaaaaaa
- You need to remove the above two things
- Based on your CSC3130 knowledge!
- How to find the SELECT set
- So if you know how to compute the FIRST and
FOLLOW sets, you know how to solve this problem - Read Slides 63-68 (esp Slide for )in Lecture 2
(presentation)
B ? A ? B ? AB e
13Problem 3
- Purposes and hints (cont)
- How to find out whether it is LL(1)
- A non-terminal A in G is LL(1), then SELECT1 sets
for all productions like A? XXX should be
disjoint - If G is LL(1), all non-terminal in G should be
LL(1) - Based on the above
- Dont forget the new non-terminals youve
introduced, if any
14Problem 3
- Purposes and hints (cont)
- Show the moves
- You need to construct a parse table
- Read Slides 69-86 in Lecture 2 (presentation)
- The predictive parsing table
15Problem 3
- Purposes and hints (cont)
- Table-driven LL parse
- Start with a stack containing S and a table
constructed in previous step - Read aaabbba from left-to-right
- Check the table (based on the stack and the
leftmost symbol in the input string) to see which
production we should select - You need to identify it (for each input symbol)
in your solution - Remove terminal symbols from the input if it
exists in the stack - Done when the input symbols are all removed and
the stack is null - Read Slides 69-86 in Lecture 2 (presentation)
illustrate the above idea clearly -
16Problem 4
17Problem 4
- Purposes and hints
- Understand how modern CC does to implement the
bottom-up parse - From the lecture notes
-
Then, why asking us to solve this question!???
18Problem 4
- Purposes and hints
- The LALR can be easily derived from the
Canonical LR - Read Slides 38-46 in Lecture 4 (presentation)
- So lets focus on the Canonical LR
- Step 0, augment the grammar, make it more
readable - S ? S
- S ? SS
- S ? SS
- S ?a
- Step 1, construct a finite state machine
19Problem 4
- Purposes and hints
- State 0 (Q0)
- CLOSURE (S ? .S, )
- S ? .S,
- S ? .SS,
-
- S ? .SS,
- S ?.a,
-
- Q1 Goto(Q0, S)
- Q2 Goto(Q0, a)
S ? S S ? SS S ? SS S ? a
How to determine the terminal symbol Say we are
finding the closure of (S?yyy.xyyy, a) The symbol
is each element in First(Follow(x)a)
How to determine the symbol Generally speaking,
Goto(Q, x) if x is a symbol in Q that follows a .
20Problem 4
- Purposes and hints
- How to calculate Goto(Q, x)
- Say in Q, we have A?yyy.xyyy, a
- Goto(Q, x)
- A?yyyx.yyy, a ( . ????) (This
is simple) - And then find the closure of A?yyyx.yyy, a (This
is tedious) - Construct the finite state machine
- Link two states Q1 to Q2 with a label a if
Goto(Q1, a) Q2 - Remember the links may form cycles
- Goto(Q5, S) Q5
- Goto(State1, symbol1) State2, Goto(State2,
symbol2) State1
S ? S S ? SS S ? SS S ? a
21Problem 4
- Purposes and hints
- Step 2 Construct the parsing table
22Problem 4
EXAMPLE
- Construct the parsing table based on the FSM
graph
0. ?acc 1. Based on the links, ??goto??shiftstat
e 2. ??rproduction
There is a . at the end
23Problem 5
24Problem 5
- Hints
- First construct the parse tree for aabbcbcabbccc
- In this question, every non-terminal in the tree
have two attributes - One is inherited
- The other is synthesized
- Be aware of the confusing naming scheme w, x, y,
z - You need to work out a dependency graph based on
the parse tree - Each non-terminal node in the tree can be mapped
to two nodes in the graph since each non-terminal
has two attributes - And then it is pretty easy to work out a
topological order - Evaluate the values according to the topological
order - The results are simple and it is related to
aabbbcbcabbccc
25Problem 5
Make the CFG more readable
G?A A?BA A?cC B?aBbC B?b C-c
26Problem 5
Make the CFG more readable
G?A A?BA A?cC B?aBbC B?b C-c
Each non-terminal symbol A, B, or C has a
inherited attribute
27Problem 5
Make the CFG more readable
G?A A?BA A?cC B?aBbC B?b C-c
Each non-terminal symbol A, B, or C has a
synthesized attribute
28Problem 5
For this production, the synthesized attribute of
A is denoted by y, it will be inherited by
non-terminal B.
29Problem 5
For this production, the inherited attribute of
the right-hand side A (the child in the parse
tree) is denoted by x, it is inherited directly
from the inherited attribute of the left-hand
side A (the parent in the parse tree).
30Problem 5
For this production, the synthesized attribute
of A is from the synthesized attribute of B.
31Problem 5
For this production, the inherited attribute of
C, denoted by x, is from the inherited attribute
of A.
32Problem 5
For this production, the synthesized attribute of
A, denote by z, is from the synthesized
attribute y of C, based on the equation z
10y3
33Problem 5
For this production, the synthesized attribute of
C, denote by y, is from the inherited attribute
x of C, based on the equation y 10x3
34Problem 5
For different productions, the attributes can
have the same name, but they are not the same guy
35Problem 5
This equation is for the product A?cC only, not
for the production A?BA
36Attribute Evaluation
- Practice 1
- Consider this context-free grammar used to
define the context-sensitive language
anbncnn1 - This CFG generates the language
akbmcnk1,m1,n1 -
- (a) Design an attribute grammar so that only the
correct sentences will be accepted - (b) Annotate the parse tree for the input
sentences aaabbbccc, aaabbcc
S - A B C A - A a a B - B b
b C - C c c
37 S - A B C if(Size(A) Size(B)
Size (C)) A - A2 a Size(A) ? Size(A2) 1
a Size(A) ? 1 B - B2 b Size(B) ? Size(B2)
1 b Size(B) ? 1 C - C2 c Size(C) ?
Size(C2) 1 c Size(C) ? 1
38- Practice 2
- Consider the context-free grammar defining
fixed-point binary numbers
(a) Design an attribute grammar so that the
numerical value of the binary number can be
computed. (b) Annotate the parse tree for the
input sentences 1 1 . 1 101
39(No Transcript)
40(No Transcript)
41Tree for 11.1101
42SDT
- Practice 3
- Eliminate left-recursion in the following SDT