Date post: | 28-Mar-2015 |
Category: |
Documents |
Upload: | abdul-wahid-khan |
View: | 874 times |
Download: | 2 times |
1
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing Bottom-up parsing is more
general than top-down parsing
Bottom-up parsers handle a large class of grammars.
Preferred method in practice
2
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing Bottom-up parsing is more
general than top-down parsing
Bottom-up parsers handle a large class of grammars.
Preferred method in practice
3
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing Bottom-up parsing is more
general than top-down parsing
Bottom-up parsers handle a large class of grammars.
Preferred method in practice
4
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up ParsingAlso called LR parsing L means that tokens are
read left to right R means that the parser
constructs a rightmost derivation.
5
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up ParsingAlso called LR parsing L means that tokens are
read left to right R means that the parser
constructs a rightmost derivation.
6
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up ParsingAlso called LR parsing L means that tokens are
read left to right R means that the parser
constructs a rightmost derivation.
7
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing LR parsers donot need
left-factored grammars LR parsers can handle
left-recursive grammars
8
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing LR parsers donot need
left-factored grammars LR parsers can handle
left-recursive grammars
9
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing
LR parsing reduces a string to the start symbol by inverting productions.
10
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing A derivation consists of a
series of rewrite steps
S 0 1 ... n-1 n sentence
11
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up ParsingS 0 ... n sentence
Each i is a sentential form if contains only terminals,
is a sentence in L(G) If contains 1
nonterminals, is a sentential form
12
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up ParsingS 0 ... n sentence
Each i is a sentential form if contains only terminals,
is a sentence in L(G) If contains 1
nonterminals, is a sentential form
13
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up ParsingS 0 ... n sentence
Each i is a sentential form if contains only terminals,
is a sentence in L(G) If contains 1
nonterminals, is a sentential form
14
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing A bottom-up parser builds a
derivation by working from input sentence back towards the start symbol S
S 0 ... n sentence
15
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing Consider the grammar
S → aABeA → Abc | bB → d
16
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing The sentence abbcde can be
reduced to S:abbcdeaAbcdeaAdeaABeS
17
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing The sentence abbcde can be
reduced to S:abbcdeaAbcdeaAdeaABeS
18
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing The sentence abbcde can be
reduced to S:abbcdeaAbcdeaAdeaABeS
19
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing The sentence abbcde can be
reduced to S:abbcdeaAbcdeaAdeaABeS
20
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing The sentence abbcde can be
reduced to S:abbcdeaAbcdeaAdeaABeS
21
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing These reductions, in fact,
trace out the following right-most derivation in reverse: S aABe
aAde aAbcde abbcde
22
S By y xy rmrm rm
xx y
B
S
rule: B →
Terminals only
23
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing
Consider the grammar
1. E → E + (E) 2. | int
24
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing
Consider bottom-up parse of the string
int + (int) + (int)
25
int + (int) + (int)
int+ ( int )int + ( )
26
int + (int) + (int)
E + (int) + (int)
int+ ( int )int + ( )
E
27
int + (int) + (int)
E + (int) + (int)
E + (E) + (int)
int+ ( int )int + ( )
E E
28
int + (int) + (int)
E + (int) + (int)
E + (E) + (int)E + (int)
int+ ( int )int + ( )
E E
E
29
int + (int) + (int)
E + (int) + (int)E + (E) + (int)E + (int)E + (E)
int+ ( int )int + ( )
E E E
E
30
int + (int) + (int)
E + (int) + (int)E + (E) + (int)E + (int)E + (E)E
int+ ( int )int + ( )
E E E
E
EA rightmost derivation in reverse
31
Bottom-up ParsingBottom-up ParsingBottom-up ParsingBottom-up Parsing
An LR parser traces a rightmost derivation in reverse
32
ConsequenceConsequenceConsequenceConsequence Let be a step of a
bottom-up parse Assume that next reduction
is A → Then is a string of
terminals!
33
ConsequenceConsequenceConsequenceConsequence Let be a step of a
bottom-up parse Assume that next reduction
is A → Then is a string of
terminals!
34
ConsequenceConsequenceConsequenceConsequence Let be a step of a
bottom-up parse Assume that next reduction
is A → Then is a string of
terminals!
35
ConsequenceConsequenceConsequenceConsequenceWhy? Because A → is a
step in a rightmost derivation
36
NotationNotationNotationNotation
Idea: Split the string into two
substrings
37
NotationNotationNotationNotation Right substring (a string of
terminals) is as yet unexamined by parser
Left substring has terminals and non-terminals
38
NotationNotationNotationNotation Right substring (a string of
terminals) is as yet unexamined by parser
Left substring has terminals and non-terminals
39
NotationNotationNotationNotation The dividing point is marked
by a ►
• The ► is not part of the string
Initially, all input is unexamined: ►x1 x1 . . . xn
40
Shift-Reduce ParsingShift-Reduce ParsingShift-Reduce ParsingShift-Reduce Parsing
Bottom-up parsing uses only two kinds of actions:
1. Shift2. Reduce
41
ShiftShiftShiftShiftMove ► one place to the rightshifts a terminal to the left string
E + (► int) E + (int ►)
42
ReduceReduceReduceReduceApply an inverse production at the right end of the left string. If E → E + (E) is a production, then E + ( E+(E)►) E + ( E ►)
43
Shift-Reduce ExampleShift-Reduce ExampleShift-Reduce ExampleShift-Reduce Example
►int + (int) + (int) $
shift
int ► + (int) + (int) $
reduce E → int
E ► + (int) + (int) $
shift 3 times
E + (int ►) + (int) $
reduce E → int
E + (E ►) + (int) $ shift
E + (E) ► + (int) $ reduce E → E+(E)
44
Shift-Reduce ExampleShift-Reduce ExampleShift-Reduce ExampleShift-Reduce Example
►int + (int) + (int) $
shift
int ► + (int) + (int) $
reduce E → int
E ► + (int) + (int) $
shift 3 times
E + (int ►) + (int) $
reduce E → int
E + (E ►) + (int) $ shift
E + (E) ► + (int) $ reduce E → E+(E)
45
Shift-Reduce ExampleShift-Reduce ExampleShift-Reduce ExampleShift-Reduce Example
►int + (int) + (int) $
shift
int ► + (int) + (int) $
reduce E → int
E ► + (int) + (int) $
shift 3 times
E + (int ►) + (int) $
reduce E → int
E + (E ►) + (int) $ shift
E + (E) ► + (int) $ reduce E → E+(E)
46
Shift-Reduce ExampleShift-Reduce ExampleShift-Reduce ExampleShift-Reduce Example
►int + (int) + (int) $
shift
int ► + (int) + (int) $
reduce E → int
E ► + (int) + (int) $
shift 3 times
E + (int ►) + (int) $
reduce E → int
E + (E ►) + (int) $ shift
E + (E) ► + (int) $ reduce E → E+(E)
47
Shift-Reduce ExampleShift-Reduce ExampleShift-Reduce ExampleShift-Reduce Example
►int + (int) + (int) $
shift
int ► + (int) + (int) $
reduce E → int
E ► + (int) + (int) $
shift 3 times
E + (int ►) + (int) $
reduce E → int
E + (E ►) + (int) $ shift
E + (E) ► + (int) $ reduce E → E+(E)
48
Shift-Reduce ExampleShift-Reduce ExampleShift-Reduce ExampleShift-Reduce Example
►int + (int) + (int) $
shift
int ► + (int) + (int) $
reduce E → int
E ► + (int) + (int) $
shift 3 times
E + (int ►) + (int) $
reduce E → int
E + (E ►) + (int) $ shift
E + (E) ► + (int) $ reduce E → E+(E)
49
Shift-Reduce ExampleShift-Reduce ExampleShift-Reduce ExampleShift-Reduce Example
►int + (int) + (int) $
shift
int ► + (int) + (int) $
reduce E → int
E ► + (int) + (int) $
shift 3 times
E + (int ►) + (int) $
reduce E → int
E + (E ►) + (int) $ shift
E + (E) ► + (int) $ reduce E → E+(E)
50
Shift-Reduce ExampleShift-Reduce ExampleShift-Reduce ExampleShift-Reduce Example
►int + (int) + (int) $
shift
int ► + (int) + (int) $
reduce E → int
E ► + (int) + (int) $
shift 3 times
E + (int ►) + (int) $
reduce E → int
E + (E ►) + (int) $ shift
E + (E) ► + (int) $ reduce E → E+(E)
51
Shift-Reduce ExampleShift-Reduce ExampleShift-Reduce ExampleShift-Reduce Example
►int + (int) + (int) $
shift
int ► + (int) + (int) $
reduce E → int
E ► + (int) + (int) $
shift 3 times
E + (int ►) + (int) $
reduce E → int
E + (E ►) + (int) $ shift
E + (E) ► + (int) $ reduce E → E+(E)
52
Shift-Reduce ExampleShift-Reduce ExampleShift-Reduce ExampleShift-Reduce Example
►int + (int) + (int) $
shift
int ► + (int) + (int) $
reduce E → int
E ► + (int) + (int) $
shift 3 times
E + (int ►) + (int) $
reduce E → int
E + (E ►) + (int) $ shift
E + (E) ► + (int) $ reduce E → E+(E)
53
Shift-Reduce ExampleShift-Reduce ExampleShift-Reduce ExampleShift-Reduce Example
E ► + (int) $ shift 3 times
E + (int ►) $ reduce E → int
E + (E ►) $ shift
E + (E) ► $ red E → E+(E)E ► $ accept