*This preview shows
pages
1–3. Sign up to
view the full content.*

1
Prof. Aiken
CS 143
Lecture 7
1
Top-Down Parsing
and
Intro to Bottom-Up Parsing
Lecture 7
Prof. Aiken
CS 143
Lecture 7
2
Predictive Parsers
• Like recursive-descent but parser can
“predict” which production to use
– By looking at the next few tokens
– No backtracking
• Predictive parsers accept LL(k) grammars
– L means “left-to-right” scan of input
– L means “leftmost derivation”
– k means “predict based on k tokens of lookahead”
– In practice, LL(1) is used
Prof. Aiken
CS 143
Lecture 7
3
LL(1) vs. Recursive Descent
• In recursive-descent,
– At each step, many choices of production to use
– Backtracking used to undo bad choices
• In LL(1),
– At each step, only one choice of production
– That is
• When a non-terminal
A
is leftmost in a derivation
• The next input symbol is
t
• There is a unique production
A
to use
– Or no production to use (an error state)
• LL(1) is a recursive descent variant without backtracking
Prof. Aiken
CS 143
Lecture 7
4
Predictive Parsing and Left Factoring
• Recall the grammar
E
T + E | T
T
int
| int * T | ( E )
• Hard to predict because
– For
T
two productions start with
int
– For
E
it is not clear how to predict
• We need to left-factor
the grammar
Prof. Aiken
CS 143
Lecture 7
5
Left-Factoring Example
• Recall the grammar
E
T + E | T
T
int
| int * T | ( E )
• Factor out common prefixes of productions
E
T X
X
+ E |
T
( E ) | int Y
Y
* T |
Prof. Aiken
CS 143
Lecture 7
6
LL(1) Parsing Table Example
• Left-factored grammar
E
T X
X
+ E |
T
( E ) | int Y
Y
* T |
• The LL(1) parsing table:
int
*
+
(
)
$
E
T X
T X
X
+ E
T
int Y
( E )
Y
* T
leftmost non-terminal
next input token
rhs of production to use

This ** preview**
has intentionally

**sections.**

*blurred***to view the full version.**

*Sign up*2
Prof. Aiken
CS 143
Lecture 7
7
LL(1) Parsing Table Example (Cont.)
• Consider the
[E, int]
entry
– “When current non-terminal is
E
and next input is
int
, use production
E
T X
”
– This can generate an
int
in the first position
• Consider the
[Y,+]
entry
– “When current non-terminal is
Y
and current token
is
+
, get rid of
Y
”
– Y
can be followed by
+
only if
Y
Prof. Aiken
CS 143
Lecture 7
8
LL(1) Parsing Tables. Errors
• Blank entries indicate error situations
• Consider the
[E,*]
entry
– “There is no way to derive a string starting with
*
from non-terminal
E
”
Prof. Aiken
CS 143
Lecture 7
9
Using Parsing Tables
• Method similar to recursive descent, except
– For the leftmost non-terminal
S
– We look at the next input token
a
– And choose the production shown at
[S,a]
• A stack records frontier of parse tree
– Non-terminals that have yet to be expanded
– Terminals that have yet to matched against the input
– Top of stack = leftmost pending terminal or non-terminal
• Reject on reaching error state
• Accept on end of input & empty stack
Prof. Aiken
CS 143
Lecture 7
10
LL(1) Parsing Algorithm
initialize stack = <S $> and next

This is the end of the preview. Sign up
to
access the rest of the document.