{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture5-2x2

# and if a right hand sides first set contains take

This preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: ecursive-descent compiler where we have a choice of righthand sides to produce for non-terminal, X , look at the FIRST of each choice and take it if the next input symbol is in it. . . • . . . and if a right-hand side’s FIRST set contains ǫ, take it if the next input symbol is in FOLLOW(X ). e ’⊣’ st ǫ | ’+’ | ’-’ ID | ’(’ e ’)’ Since e ⇒ s t ⇒ ( e ) ⇒ ..., we know that ‘(’ ∈ FIRST(e). Since s ⇒ ǫ, we know that ǫ ∈ FIRST(s). • If X is a non-terminal symbol in some grammar, G, then FOLLOW(X ) is the set of terminal symbols that can come immediately after X in some sentential form that G can produce. For example, since p ⇒ e ⊣ ⇒ s t ⊣ ⇒ s ’(’ e ’)’ ⊣ ⇒ ..., we know that ‘(’ ∈ FOLLOW(s). Last modified: Mon Sep 17 17:06:09 2012 CS164: Lecture #5 7 Last modified: Mon Sep 17 17:06:09 2012 CS164: Lecture #5 8 Using Loops to Roll Up Recursion Grammar Problems III Grammar • There are ways to deal with problem in last slide within the pure What actions? p: e: et: | | t: e ’⊣’ t et ǫ ’/’ e ’*’ e I { { { { { ?1 ?2 ?3 ?4 \$\$ } } } } = \$1; } Here, we don’t have the previous problems, but how do we build a tree that associates properly (left to right), so that we don’t interpret I/I/I as if it were I/(I/I)? What are FIRST and FOLLOW? FIRST(p) = FIRST(e) = FIRST(t) = FIRST(et) = { ǫ, ’/’, ’*’ } FIRST(’/’ e) = { ’/’ } (when FIRST(’*’ e) = { ’*’ } (when FOLLOW(e) = { ’⊣’ } FOLLOW(et) = FOLLOW(e) (when FOLLOW(t) = { ’⊣’, ’/’, ’*’ } Last modified: Mon Sep 17 17:06:09 2012 {I} to use ?3) to use ?4) framework, but why bother? • Implement e procedure with a loop, instead: def e(): r = t() while next() in [’/’, ’*’]: if next() == ’/’: scan(’/’); t1 = t() r = makeTree (DIV, r, t1) else: scan(’*’); t1 = t() r = makeTree (MULT, r, t1) return r to use ?2) CS164: Lecture #5 9 Last modified: Mon Sep 17 17:06:09 2012 CS164: Lecture #5 10...
View Full Document

{[ snackBarMessage ]}