lecture5-2x2

# Make the nonterminal functions return semantic values

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: #5 3 Last modified: Mon Sep 17 17:06:09 2012 CS164: Lecture #5 4 Expression Recognizer with Actions • Can make the nonterminal functions return semantic values. • Assume lexer somehow supplies semantic values for tokens, if elist : ǫ | sexp elist { \$\$ = emptyList; } { \$\$ = cons(\$1, \$2); } def elist (): if next() in [SYM, NUM, STRING, ’(’, ’\’’]: v1 = sexp(); v2 = elist(); return cons(v1,v2) else: return emptyList Last modified: Mon Sep 17 17:06:09 2012 CS164: L Grammar Problems I Grammar Problems II In a recursive-descent parser, what goes wrong here? p : e ’⊣’ e:t | e ’/’ t | e ’*’ t Well then: What goes wrong here? p : e ’⊣’ e:t | t ’/’ e | t ’*’ e { \$\$ = \$1; } { \$\$ = makeTree(DIV, \$1, \$3); } { \$\$ = makeTree(MULT, \$1, \$3); } { \$\$ = \$1; } { \$\$ = makeTree(DIV, \$1, \$3); } { \$\$ = makeTree(MULT, \$1, \$3); } If we choose the second of third alternative for e, we’ll get an infinite recursion. If we choose the first, we’ll miss ’/’ and ’*’ cases. No infinite recursion, but we still don’t know which right-hand side to choose for e. Last modified: Mon Sep 17 17:06:09 2012 Last modified: Mon Sep 17 17:06:09 2012 CS164: Lecture #5 5 FIRST and FOLLOW string. For example: : : : : 6 Using FIRST and FOLLOW • If α is any string of terminals and nonterminals (like the right side of a production) then FIRST(α) is the set of terminal symbols that start some string that α produces, plus ǫ if α can produce the empty p e s t CS164: Lecture #5 • In a r...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online