Predictive parsers Predictive parser can be implemented by
• Predictive parsers • Predictive parser can be implemented by maintaining an external stack
Predictive parsing Non Recursive Descent Parser • Predictive parser can be implemented by maintaining an external stack input stack parser output Parse table is a two dimensional array M[X, a] where “X” is a non terminal and “a” is a terminal of the grammar Parse table 2
FIRST and FOLLOW • Given a grammar G, we may define the functions FIRST and FOLLOW on the strings of symbols of G. – FIRST( ) is the set of all terminals that may appear as the first symbol in a replacement string of . – FOLLOW( ) is the set of all terminals that may follow in a derivation.
Example: FIRST • Let the grammar be E T E' E' + T E' | . T F T' T' * F T' | . F (E) | id | num
Example: FIRST • Find FIRST(F). – FIRST(F) = {(, id, num}. • Therefore, – FIRST(E) = {(, id, num}. – FIRST(T) = {(, id, num}.
Example: FIRST • Find FIRST(E'). – FIRST(E') = {+, €}. • Find FIRST(T'). – FIRST(T') = {*, €}.
Summary Nonterminal E E' T T' F Nullable No Yes No FIRST {(, id, num} {+, €} {(, id, num} {*, €} {(, id, num}
FOLLOW • We are concerned about FOLLOW(X) only for the nonterminals of the grammar. • According to the definition, to determine FOLLOW(A), we must inspect all productions that have A on the right.
Example: FOLLOW • Let the grammar be E T E' E' + T E' | . T F T' T' * F T' | . F (E) | id | num
Example: FOLLOW • Find FOLLOW(E). – E is the start symbol, therefore $ FOLLOW(E). – E occurs on the right in only one production. F (E). – Therefore FOLLOW(E) = {$, )}.
Example: FOLLOW • Find FOLLOW(E'). – E' occurs on the right in two productions. E T E' E' + T E'. – Therefore, FOLLOW(E') = FOLLOW(E) = {$, )}.
Example: FOLLOW • Find FOLLOW(T). – T occurs on the right in two productions. E T E' E' + T E'. – Therefore, FOLLOW(T) contains FIRST(E') = {+}. – However, E' is nullable, therefore it also contains FOLLOW(E) = {$, )} and FOLLOW(E') = {$, )}. – Therefore, FOLLOW(T) = {+, $, )}.
Example: FOLLOW • Find FOLLOW(T'). – T' occurs on the right in two productions. T F T' T' * F T'. – Therefore, FOLLOW(T') = FOLLOW(T) = {$, ), +}.
Example: FOLLOW • Find FOLLOW(F). – F occurs on the right in two productions. T F T' T' * F T'. – Therefore, FOLLOW(F) contains FIRST(T') = {*}. – However, T' is nullable, therefore it also contains FOLLOW(T) = {+, $, )} and FOLLOW(T') = {$, ), +}. – Therefore, FOLLOW(F) = {*, $, ), +}.
Summary Nonterminal E E' T T' F Nullable No Yes No FIRST {(, id, num} {+} {(, id, num} {*} {(, id, num} FOLLOW {$, )} {$, ), +} {*, $, ), +}
Parse table for the grammar id E E’ T T’ F + * E TE’ ( $ E’ Є T’ Є E T E’ E’ +TE’ T FT’ T F T’ T’ Є F id ) T’ *FT’ F (E) Blank entries are error states. For example E can not derive a string starting with ‘+’ 17
Parsing algorithm • The parser considers 'X' the symbol on top of stack, and 'a' the current input symbol • These two symbols determine the action to be taken by the parser • Assume that '$' is a special token that is at the bottom of the stack and terminates the input string if X = a = $ then halt if X = a ≠ $ then pop(x) and ip++ if X is a non terminal then if M[X, a] = {X UVW} then begin pop(X); push(W, V, U) end else error 18
Example Stack input action $E id + id * id $ expand by E TE’ $E’T id + id * id $ expand by T FT’ $E’T’F id + id * id $ expand by F id $E’T’id id + id * id $ $E’T’ + id * id $ expand by T’ Є $E’ + id * id $ expand by E’ +TE’ $E’T+ + id * id $ pop + and ip++ $E’T id * id $ pop id and ip++ expand by T FT’ 19
Example … Stack input action $E’T’F id * id $ $E’T’id id * id $ $E’T’ * id $ expand by T’ *FT’ $E’T’F* * id $ pop * and ip++ $E’T’F id $ expand by F id $E’T’id id $ pop id and ip++ $E’T’ $ expand by T’ Є $E’ $ expand by E’ Є $ $ halt expand by F id pop id and ip++ 20
• A δ 1/ δ 2/ δ 3/……. / δn – First(δ 1), First(δ 2), First(δ 3)… First(δn) gives multiple entries in a cell if δ 1, δ 2, δ 3… δn have common element in first. • A δ/€ – First(δ)˄follow(A) != NULL multiple entries in a cell
- Slides: 21