Presentation is loading. Please wait.

Presentation is loading. Please wait.

Predictive Parsing Program

Similar presentations


Presentation on theme: "Predictive Parsing Program"— Presentation transcript:

1 Predictive Parsing Program
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) =  a + b $ Input Predictive Parsing Program Parsing Table M Output X Y Z Stack

2 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.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)

4 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)

5 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)

6 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 lower-level 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 …

7 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 lower-level construct the symbol that begin higher-level constructs, e.g., add keywords that begin statements to the sync set for the nonterminals generating expressions

8 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

9 Parsing table with error recovery
Non terminal Input id + * ( ) $ E E  TE  synch E  E   +TE  E    T T  FT  Synch T  T    T   *FT  F F  id F  (E)

10 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

11 + belongs to the sync set of F, pop F
Stack Input Output $E +id*+id$ skip+ id*+id$ id belongs to FIRST(E) $E T $E T F $E T  id $E T  *+id$ $E T F* +id$ + belongs to the sync set of F, pop F

12 $E  +id$ $E T+ $E T id$ $E T F $E T id $E T  $


Download ppt "Predictive Parsing Program"

Similar presentations


Ads by Google