Lesson 7 - OSCAR Lab

Revision Context Free Grammar
Top Down
自下而上
Recursive Descent
LL(1)
Non-recursive
2 Functions
For any A  |
1、FIRST( )  FIRST( )
=
2、If  *  , then
FIRST()  FOLLOW(A) =

Input
Stack
X
Y
Z
$
a +b $
Predictive Parsing
Program
Parsing Table M
Output
3.3 Top Down Parsing
3.3.6 Error Recovery
Error examples :
Lexical: misspell ID/keywords
Syntax: Parenthesis mismatch
Semantic: Incompatible operator
Logic: Infinite recursion
3.3 Top Down Parsing
When will non-recursive predictive
parsing detect errors?
When the terminal on top of the stack does
not match the next input symbol
When nonterminal A is on top of the stack,
a is the
next input symbol, and M[A, a]
is error (empty parsing table entry)
3.3 Top Down Parsing
Panic Mode
Panic mode error recovery is based
on the idea of skipping over symbols on
the input, until a token in a selected
set of synchronizing tokens appears.
Synchronizing
The syntactic structure that could
be constructed by the current token
stream (what does the parser expect)
3.3 Top Down Parsing
Choice of Synchronizing set
Place all symbols in FOLLOW(A) into the
synchronizing set of A
if expr then
(then belongs to the synchronizing
set of expr)
3.3 Top Down Parsing
Choice of Synchronizing set
Place all symbols in FOLLOW(A) into the
synchronizing set of nonterminal A
Add to the synchronizing set of a lowerlevel construct the symbol that begin
higher-level constructs, e.g., add
keywords that begin statements to the sync
set for the nonterminals generating
expressions
a := expr ; if …
3.3 Top Down Parsing
Choice of Synchronizing set
Place all symbols in FOLLOW(A) into the
synchronizing set of nonterminal A
Add to the synchronizing set of a lowerlevel construct the symbol that begin
higher-level constructs, e.g., add
keywords that begin statements to the sync
set for the nonterminals generating
expressions
3.3 Top Down Parsing
Choice of Synchronizing set
Put symbols in FIRST(A) to the
synchronizing set for nonterminal A
If a nonterminal can generate the empty
string, then the production deriving
epsilon can be used as a default
If a terminal on top of the stack cannot
be matched, pop the terminal
Parsing table with error recovery
Input
Non
terminal
id
E
E  TE 
E
T
*
(
E  TE 
E   +TE 
T  FT 
T
F
+
F  id
T  FT 
Synch
T
T   *FT 
synch
synch
F  (E)
)
$
synch
synch
E
E
synch
synch
T
T
synch
synch
3.3 Top Down Parsing
1 Look up the parsing table, if blank entry,
the terminal is skipped
2. If the entry is “synch”, pop the
nonterminal on top of the stack
3. If nonterminal on top of the stack does
not match the input table, pop the
nonterminal
1. stack unchanged; 2,3 input unchanged
Stack
$E
Input
+id*+id$
Output
skip+
$E
id*+id$
id*+id$
id belongs to FIRST(E)
$E T
$E T F
$E T  id
$E T 
$E T F*
$E T F
$E T 
id*+id$
id*+id$
*+id$
*+id$
+id$
+id$
+ belongs to the sync set of
F, pop F
$E 
+id$
$E T+
+id$
$E T
id$
$E T F
id$
$E T id
id$
$E T 
$
$E 
$
$
$