Title: Topdown parsing
1Top-down parsing
- Janina Kopp
- Miriam Käshammer
- CL2 Parsing Course
- 21 November 2007
2Overview
- The basic top-down approach
- Pushdown automata
- Breadth-first top-down parsing
- Depth-first top-down parsing
3The basic approach (1)
- Start with the start symbol on the prediction
stack. - Always process the topmost symbol of the stack
- If it is a terminal, match it with the current
input symbol or reject the parse. - If it is a non-terminal, replace it with one of
its right-hand sides. - ? Left-most derivation
4The basic approach (2)
- Example
- S ? aB bA
- A ? a aS bAA
- B ? b bS aBB
- Input aabb
Derivation S ? aB ? aaBB ? aabB ? aabb
5The basic approach (3)
Taken from Dick Grune and Ceriel Jacobs. Parsing
Techniques. Ellis Horwood Limited, 1990. Page 121.
6Pushdown automata
- Construction of a PDA for a CFG
- Presentation on PDAs (5 November 2007)
- JFLAP Convert to PDA (LL)
- adopts Slide 13 (top down)
7PDA Shortcomings
- Non-determinism
- ? try all possibilities
- No record of used rules
- ? analysis stack
- For every prediction step, push the non-terminal
being replaced (with index). - For every match, push the matched terminal.
- ? instantaneous description
8Breadth-first top-down parser (1)
- Intuition
- Consider all possible predictions of a given
grammar, but follow a prediction only until it
contradicts the input string.
9Breadth-first top-down parser (2)
- Technique
- Maintain a list of all possible predictions.
- non-terminal in front
- remove this prediction and
- add as many predictions as there are right hand
sides for this non-terminal - each with the non-terminal replaced by one of
these right hand sides. - -gt repeat this until you have only predictions
that start with a terminal
10Breadth-first top-down parser (3)
- Technique (continued)
- Take the next symbol of your input sentence and
compare it to the terminals of your predictions - Discard all predictions that do not match your
current input symbol - Continue until you reach the end of your input
sentence. - the end is marked by appending to the input
string - a rule S ? S is added
- Illustration of the principle
11Breadth-first Example (1)
- Example Grammar
- S ? DC AB
- A ? a aA
- B ? bc bBc
- D ? ab aDb
- C ? c cC
- termination rule
- S ? S
Input aabc
12Breadth-first Example (2)
1
2
4
3
13Breadth-first Example (2)
5
4
6
7
14Breadth-first Example (2)
8
7
9
10
15Breadth-first Example (2)
11
10
12
16Depth-first (backtracking) parser (1)
- Intuition
- When faced with a number of possibilities, choose
one and leave the others for later. - If this choice turns out to be a failure (or even
a success, but one needs all solutions), roll
back the actions until the point of decision and
continue with the other possibilities.
17Depth-first (backtracking) parser (2)
- Technique
- Same as the breadth-first parser until the
topmost symbol on the prediction stack has more
than one right-hand side - Chooses the first right-hand side A1
- Continues until
- the prediction stack is empty and there is no
input left (? a parse is found) - a match fails (? backtracking)
18Depth-first (backtracking) parser (3)
- Backtracking
- Removes any terminals from the top of the
analysis stack and from the matched input. - Pushes them back onto the prediction stack and
the rest of the input.
?
19Depth-first (backtracking) parser (4)
- Backtracking
- Two possibilities
- Empty analysis stack parsing stops
- Non-terminal on top of the analysis stack Pops
it and pushes it back onto the prediction stack
replacing its right-hand side
?
20Depth-first (backtracking) parser (5)
- Two possibilities
- No other right-hand side of this non-terminal
- ? backtracks further
- Another right-hand side of this non-terminal left
- ? starts parsing again Replaces the
non-terminal with the next right-hand side
21Depth-first (backtracking) parser (6)
Taken from Dick Grune and Ceriel Jacobs. Parsing
Techniques. Ellis Horwood Limited, 1990.
22Depth-first (backtracking) parser (7)
23Depth-first (backtracking) parser (8)
24Depth-first (backtracking) parser (9)
25Depth-first (backtracking) parser (10)
26The problem of left-recursion
- Assume we have a grammar
- S ? Sb a
- ? we do not get to a state where a match with the
input can be attempted - problem non-terminal derives an infinite
sequence of sentential forms, all starting with
non-terminals
- left-recursion
- immediate (A ? A a)
- indirect (A ? B a, B ? A ß)
27Breadth-first vs. Depth-first