Professional Documents
Culture Documents
1
TABLE DRIVEN PREDICTIVE PARSING
2
PREDICTIVE PARSING
3
PREDICTIVE PARSING
4
PREDICTIVE PARSING
5
PREDICTIVE PARSING
6
PREDICTIVE PARSING
7
PREDICTIVE PARSING
8
PREDICTIVE PARSING
9
PREDICTIVE PARSING
10
PREDICTIVE PARSING
11
PREDICTIVE PARSING
12
PREDICTIVE PARSING
13
PREDICTIVE PARSING
14
PREDICTIVE PARSING
15
PREDICTIVE PARSING
16
PREDICTIVE PARSING
17
PREDICTIVE PARSING
18
PREDICTIVE PARSING
19
PREDICTIVE PARSING
20
PREDICTIVE PARSING
21
PREDICTIVE PARSING
22
PREDICTIVE PARSING
23
PREDICTIVE PARSING
24
PREDICTIVE PARSING
25
PREDICTIVE PARSING
26
PREDICTIVE PARSING
27
PREDICTIVE PARSING
28
PREDICTIVE PARSING
29
PREDICTIVE PARSING
30
PREDICTIVE PARSING
31
PREDICTIVE PARSING
32
PREDICTIVE PARSING
33
PREDICTIVE PARSING
34
PREDICTIVE PARSING
35
PREDICTIVE PARSING
36
PREDICTIVE PARSING
37
PREDICTIVE PARSING
38
PREDICTIVE PARSING
39
PREDICTIVE PARSING
40
PREDICTIVE PARSING
41
PREDICTIVE PARSING
42
PREDICTIVE PARSING
43
PREDICTIVE PARSING
44
PREDICTIVE PARSING
45
PREDICTIVE PARSING
46
PREDICTIVE PARSING
47
PREDICTIVE PARSING
48
PREDICTIVE PARSING
49
RECONSTRUCTING THE PARSE
TREE
50
RECONSTRUCTING THE PARSE
TREE
51
RECONSTRUCTING THE PARSE
TREE
52
Example (Stack Moves)
Input string: id + id * id
53
EXAMPLE: THE “DANGLING ELSE”
GRAMMAR
54
EXAMPLE: THE “DANGLING ELSE”
GRAMMAR
55
EXAMPLE: THE “DANGLING ELSE”
GRAMMAR
EXAMPLE: THE “DANGLING ELSE”
GRAMMAR
EXAMPLE: THE “DANGLING ELSE”
GRAMMAR
EXAMPLE: THE “DANGLING ELSE”
GRAMMAR
EXAMPLE: THE “DANGLING ELSE”
GRAMMAR
EXAMPLE: THE “DANGLING ELSE”
GRAMMAR
EXAMPLE: THE “DANGLING ELSE”
GRAMMAR
EXAMPLE: THE “DANGLING ELSE”
GRAMMAR
EXAMPLE: THE “DANGLING ELSE”
GRAMMAR
LL(1) GRAMMAR LL(1) Grammar
• LL(1) grammars
• Are never ambiguous.
• Will never have left recursion.
• Furthermore...
• If we are looking for a n “A” and the next symbol is
“b”,
• Then only one production mu s t be possible
66
PROPERTIES OF LL(1) GRAMMAR
67
Examples
S -> A a A b | B b B a
A -> ε
B -> ε
Rule 2. (A a A b) ε
( B b B a) ε
Grammar is LL(1)
Examples
S -> 1 A B | ε
A -> 1 A C | 0 C
B -> 0 S
C - >1
Rule 2. FIRST (1 A B) ε
FIRST (ε) ε
Grammar is LL(1)
ERROR RECOVERY
When Do Errors Occur? Recall Predictive Parser Function:
a + b $ Input
$E’T’ + id $
$E’ + id $
$E’T+ + id $
$E’T id $
$E’T’F id $
$E’T’id id $
$E’T’ $
$E’ $
$ $
Bottom-Up Parsing
• Shift-reduce parsing is a general style of bottom-up
parsing.
S lm a A B e a AbcB e a b b c B e a b b c d e
Example
78
Handles (cont.)
If S * Aw * w, then A in the position following is
a handle of w.
Note:
1. The string w to the right of a handle contains only terminal
symbols.
2. If a grammar is unambiguous, then every right-sentential
form of the grammar has exactly one handle; otherwise,
some right-sentential forms may have more than one handle.
79
Example
Consider the following ambiguous grammar
EE+E | E*E | (E) | id
Two rightmost derivation of id1+id2*id3
1. E E+E 2. E E*E
E+ E*E E*id3
E+ E*id3 E+ E*id3
E+ id2*id3 E+ id2*id3
id1+ id2*id3 id1+ id2*id3
two possible handles for Right Sentential Form
E + E * id3
80
Handle Pruning
• A right-most derivation in reverse can be obtained by
handle-pruning.
Disadvantages:
We use $ to mark the end of the stack and the input buffer
STACK INPUT
$ w$
• The parser shifts input symbols onto the stack until a handle is on
top of stack.
• Repeats this cycle until it has an error or the stack contains S and
the input buffer is empty.
STACK INPUT
$S $
83
Shift-Reduce Parsing
The handle always appears at the top of the stack just before it is
identified as the handle.
STACK INPUT
….if expr then stmt else….$
2. LR-Parsers
– covers wide range of grammars.
• SLR – simple LR parser
• CLR – Canonical LR -most general LR parser
• LALR – intermediate LR parser (lookhead LR parser)
– SLR, LR and LALR work same, only their parsing tables are different.
Operator Precedence Parser
Operator grammars are used for implementation of operator
precedence parser.
Operator Grammar :
E -> E + E | E – E | E * E | E / E | -E | ( E ) | id
Precedence relation
• Scan input string left to right, try to detect .> and put a
pointer on its location.
E -> E + E | E * E | id
Precedence Function
1. Create functions fa and ga for each grammar terminal a and for the
end of string symbol.
2. Partition the symbols in groups so that fa and gb are in the same
group if a =· b (there can be symbols in the same group even if they
are not connected by this relation).
3. Create a directed graph whose nodes are in the groups, next for each
symbols a and b do: place an edge from the group of gb to the group
of fa if a <· b, otherwise if a ·> b place an edge from the group of fa
to that of gb.
4. If the constructed graph has a cycle then no precedence functions
exist. When there are no cycles collect the length of the longest
paths from the groups of fa and gb respectively.
Consider the following table:
Resulting graph:
gid fid
f* g*
Precedence Table:
id + * $
g+ f+
f 4 2 4 0
g 5 1 3 0
f$ g$
LR PARSERS
LR parsing is attractive because:
Drawback of LR method:
• Sets of LR(0) items will be the states of action and goto table of
the SLR parser.
– States represent sets of “items”
• Special Case:
Rule: A yields only one item
A
CONSTRUCTING SLR PARSING TABLES
• A collection of sets of LR(0) items (the canonical LR(0)
collection) is the basis for constructing SLR parsers.
E' E
closure({E' . E}) =
Kernel item: includes the initial item S’->.S and all items whose
dots are not at the left end.
Non-Kernel item: all items whose dots are at the left end.
GOTO OPERATION
• Algorithm:
C is { closure({S' S}) }
repeat the followings until no more set of LR(0) items
can be added to C.
for each I in C and each grammar symbol X
if GOTO(I,X) is not empty and not in C
add GOTO(I,X) to C
closure (,*E’•E]})=I0: E’ •E
E •E+T
E •T
T •T*F
T •F
F • (E)
F • id
goto(I0, E)=I1: E’ E •
E E•+T
113
Example (cont.) I0: E’ .E
E .E+T
E .T
T .T*F
T .F
goto(I0, ( )= I4: F (• E)
E •E+T
E •T
T •T*F
F • (E)
F • id
115
Example (cont.)
goto(I4, E)= I8 : F (E•)
E E•+T
goto(I4, T)= I2
goto(I4, F)= I3
goto(I4, ( )= I4
goto(I4, id)= I5
goto(I6, T)= I9 : E E + T •
T T•*F
116
Example (cont.)
goto(I6, F)= I3
goto(I6, ( )= I4
goto(I6, id)= I5
goto(I7, F )= I10 : T T * F •
goto(I7, ( )= I4
goto(I7, id )= I5
goto(I8, ) )= I11 : F (E) •
goto(I8, + )= I6
goto(I9, * )= I7
117
The Transition Diagram
The goto functions for the canonical collection of sets of items
be shown as a transition diagram.
118
The Transition Diagram
The goto functions for the canonical collection of sets of items
be shown as a transition diagram.
119
The Parsing Table
State i is construction form Ii.
1) The parsing actions for state i are determinated as follows:
a) If [A•a] is in Ii and goto(Ii, a)=Ij, set action[i,a] to
“shift j”. Here a must be a terminal.
b) If [A•+ is in Ii, set action *i, a+ to “reduce A” for all a
in FOLLOW (A). Here A must not S’.
c) If * S’S•+ in Ii, set action*i,$+ to “accept”.
2) The goto transactions for state i are constructed for all
nonterminals A using the rule:
• If goto(Ii, A)=Ij, the goto[i,A]= j.
3) All entries not defined by 1) and 2) are set “error”
4) The start state of the parser is the one constructed from the set
of items containing *S’• S]
120
PARSING TABLES OF EXPRESSION
GRAMMAR
Action Table GotoTable
state id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
Key to Notation 3 r4 r4 r4 r4
4 s5 s4 8 2 3
S4=“Shift input symbol
and push state 4” 5 r6 r6 r6 r6
R5= “Reduce by rule 5” 6 s5 s4 9 3
Acc=Accept
(blank)=Syntax Error 7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
LR PARSING ALGORITHM
input a1 ... ai ... an $
stack
Sm
Xm
Sm-1 LR Parsing Algorithm output
Xm-1
.
.
S1 Action Table Goto Table
X1 terminals and $ non-terminal
S0 s four different s
t actions t each item is
a a a state number t
t e
e s
s
A CONFIGURATION OF LR PARSING
ALGORITHM
1. shift s -- shifts the next input symbol and the state s onto the stack
( So X1 S1 ... Xm Sm, ai ai+1 ... an $ ) € ( So X1 S1 ... Xm Sm ai s, ai+1 ... an $ )
3. Error -- Parser detected an error (an empty entry in the action table)
ACTIONS OF A LR-PARSER
Reduce Action
state id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
Key to Notation 4 s5 s4 8 2 3
S4=“Shift input symbol 5 r6 r6 r6 r6
and push state 4” 6 s5 s4 9 3
R5= “Reduce by rule 5”
Acc=Accept 7 s5 s4 10
(blank)=Syntax Error 8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
ACTIONS OF A (S)LR-PARSER -- state
0
id
s5
+ * (
s4
) $ E T
1 2
F
EXAMPLE 1
2
s6
r2 s7 r2
acc
r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
Input : id * id + id 8
9
s6
r1 s7
s11
r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
S L=R
S R
L *R
L id
R L
LALR Parsing Tables
• LALR stands for LookAhead LR.
14
8