Topdown parsing - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Topdown parsing

Description:

Removes any terminals from the top of the analysis stack and from the matched input. ... Non-terminal on top of the analysis stack: Pops it and pushes it back onto the ... – PowerPoint PPT presentation

Number of Views:93
Avg rating:3.0/5.0
Slides: 28
Provided by: miriamk
Category:
Tags: of | parsing | pops | the | top | topdown

less

Transcript and Presenter's Notes

Title: Topdown parsing


1
Top-down parsing
  • Janina Kopp
  • Miriam Käshammer
  • CL2 Parsing Course
  • 21 November 2007

2
Overview
  • The basic top-down approach
  • Pushdown automata
  • Breadth-first top-down parsing
  • Depth-first top-down parsing

3
The 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

4
The basic approach (2)
  • Example
  • S ? aB bA
  • A ? a aS bAA
  • B ? b bS aBB
  • Input aabb

Derivation S ? aB ? aaBB ? aabB ? aabb
5
The basic approach (3)
Taken from Dick Grune and Ceriel Jacobs. Parsing
Techniques. Ellis Horwood Limited, 1990. Page 121.
6
Pushdown automata
  • Construction of a PDA for a CFG
  • Presentation on PDAs (5 November 2007)
  • JFLAP Convert to PDA (LL)
  • adopts Slide 13 (top down)

7
PDA 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

8
Breadth-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.

9
Breadth-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

10
Breadth-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

11
Breadth-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
12
Breadth-first Example (2)
1
2
4
3
13
Breadth-first Example (2)
5
4
6
7
14
Breadth-first Example (2)
8
7
9
10
15
Breadth-first Example (2)
11
10
12
16
Depth-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.

17
Depth-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)

18
Depth-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.

?
19
Depth-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

?
20
Depth-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

21
Depth-first (backtracking) parser (6)
Taken from Dick Grune and Ceriel Jacobs. Parsing
Techniques. Ellis Horwood Limited, 1990.
22
Depth-first (backtracking) parser (7)
23
Depth-first (backtracking) parser (8)
24
Depth-first (backtracking) parser (9)
25
Depth-first (backtracking) parser (10)
26
The 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 ß)

27
Breadth-first vs. Depth-first
Write a Comment
User Comments (0)
About PowerShow.com