Docsity

Top-Down Parsing: Predictive Parsing & Parsing Tables in EECS 483 at UMich, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Top-Down Parsing: Predictive Parsing & Parsing Tables in EECS 483 at UMich and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Syntax Analysis –

Part III

Top-Down Parsers^ EECS 483 – Lecture 6^ University of Michigan^ Monday, September 27, 2004

  • 1 -

Predictive Parsing^ Y^ LL(1) grammar:^ » For a given non-terminal, the lookaheadsymbol uniquely determines the production toapply^ » Top-down parsing = predictive parsing^ » Driven by predictive parsing table of^ y^ non-terminals x terminals

Æ^ productions

  • 3 -

How to Implement This?^ • Table can be converted easily into a recursive^ descent parser^ • 3 procedures: parse_S(), parse_S’(), and parse_E()^ num^

+^ (^

)^ $

S^ Æ^ ES’^

Æ^ ES’

S’^

Æ^ +S^

Æ^ ε^ Æ^ ε E^ Æ^ num^

Æ^ (S)

  • 4 -

Recursive-Descent Parser^ void parse_S() {switch (token) {case num: parse_E(); parse_S’(); return;case ‘(‘: parse_E(); parse_S’(); return;default: ParseError();} }

lookahead token num^ +^

(^ )^

S^ Æ^ ES’^

Æ^ ES’

S’^

Æ^ +S^

Æ^ ε^ Æ^ ε E^ Æ^ num^

Æ^ (S)

  • 6 -

Recursive-Descent Parser (3)^ void parse_E() {^ switch (token) {^ case number: token = input.read(); return;^ case ‘(‘: token = input.read(); parse_S();^ if (token != ‘)’) ParseError();^ token = input.read(); return;^ default: ParseError();^ }^ }^ num^

+^ (^

)^ $

S^ Æ^ ES’^

Æ^ ES’

S’^

Æ^ +S^

Æ^ ε^ Æ^ ε E^ Æ^ num^

Æ^ (S)

  • 7 -

Call Tree = Parse Tree

S E +^ S ( S ) E E + SE + S 1 2 E ( S )E + SE 3 4

parse_Sparse_Eparse_S’parse_Sparse_Sparse_E parse_S’parse_Sparse_E^ parse_S’parse_Sparse_Eparse_S’parse_S

  • 9 -

Constructing Parse Tables^ Y^ Can construct predictive parser if:» For every non-terminal, every lookahead symbol canbe handled by at most 1 production^ Y^ FIRST(β) for an arbitrary string of terminals andnon-terminals^

β^ is: » Set of symbols that might begin the fully expandedversion of β Y FOLLOW(X) for a non-terminal X is:» Set of symbols that might follow the derivation of Xin the input stream^ FIRST^

X FOLLOW

  • 10 -

Parse Table Entries^ Y^ Consider a production X

Æ^ β Y^ Add^ Æ^ β^ to the X row for each symbol inFIRST(β) Y^ If^ β^ can derive

ε^ (β^ is nullable), add

Æ^ β for each symbol in FOLLOW(X) Y Grammar is LL(1) if no conflicting entries

num^ +^ (^

)^ $

S^ ES’^

ES’

S’^ +S^

ε^ ε E^ num^

(S)

S^ Æ^ ES’ S’^ Æ^ ε^ | +S E^ Æ^ number | (S)

  • 12 -

Computing FIRST^ Y^ Determining FIRST(X)^ 1.^ if X is a terminal, then add X to FIRST(X)^ 2.^ if XÆ^ ε^ then add

ε^ to FIRST(X)

3.^ if X is a nonterminal and X

Æ^ Y1Y2...Yk then a is in FIRST(X) if a is in FIRST(Yi) and

ε^ is in FIRST(Yj) for j = 1...i-1 (i.e., its possible to have anempty prefix Y1 ... Yi-1 4. if^ ε^ is in FIRST(Y1Y2...Yk) then

ε^ is in FIRST(X)

  • 13 -

FIRST Example

S^ Æ^ ES’ S’^ Æ^ ε^ | +S E^ Æ^ number | (S) Apply rule 1: FIRST(num) = {num}, FIRST(+) = {+}, etc. Apply rule 2:^ FIRST(S’) = {

ε} Apply rule 3: FIRST(S) = FIRST(E) = {}^ FIRST(S’) = FIRST(‘+’) + {

ε} = {^ ε, + } FIRST(E) = FIRST(num) + FIRST(‘(‘) = {num, ( } Rule 3 again: FIRST(S) = FIRST(E) = {num, ( } FIRST(S’) = {ε, + } FIRST(E) = {num, ( }

  • 15 -

FOLLOW Example

FIRST(S) = {num, ( } FIRST(S’) = {ε, + } FIRST(E) = { num, ( } S^ Æ^ ES’ S’^ Æ^ ε^ | +S E^ Æ^ number | (S) Apply rule 1: FOL(S) = {$} Apply rule 2:^ S^ Æ^ ES’^

FOL(E) += {FIRST(S’) -

ε} = {+} S’^ Æ^ ε^ | +S^ - E^ Æ^ num | (S) FOL(S) += {FIRST(‘)’) -

ε} = {$,) } Apply rule 3:^ S^ Æ^ ES’

FOL(E) += FOL(S) = {+,$,)}^ (because S’ is nullable)^ FOL(S’) += FOL(S) = {$,)}

  • 16 -

Putting it all Together

FOLLOW(S) = { $, ) } FOLLOW(S’) = { $, ) } FOLLOW(E) = { +, ), $ }

FIRST(S) = {num, ( } FIRST(S’) = {ε, + } FIRST(E) = { num, ( }^ YConsider a production X

Æ^ β YAdd^ Æ^ β^ to the X row for each symbol in FIRST(

β) YIf^ β^ can derive^ ε^ (β

is nullable), add^ Æ^

β^ for each symbol in FOLLOW(X)

num^ +^ (^

)^ $

S^ ES’^

ES’

S’^ +S^

ε^ ε E^ num^

(S)

S^ Æ^ ES’ S’^ Æ^ ε^ | +S E^ Æ^ number | (S)

  • 18 -

Class Problem^ E^ Æ^ E + T | T^ T^ Æ^ T * F | F^ F^ Æ^ (E) | num |^ ε

  1. Compute FIRST and FOLLOW sets for this G 2. Compute parse table entries
  • 19 -

Top-Down Parsing Up to This Point^ Y^ Now we know^ » How to build parsing table for an LL(1)grammar (ie FIRST/FOLLOW)^ » How to construct recursive-descent parserfrom parsing table^ » Call tree = parse tree^ Y^ Open question – Can we generate theAST?