This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: COP4020 Programming Assignment 2 Spring 2011 Consider our familiar augmented LL(1) grammar for an expression language (see Syn- tax lecture notes on the LL(1) expression grammar): GRAMMAR PRODUCTION SEMANTIC RULES <expr>-> <term> <term_tail> term_tail.subtotal := term.value; expr.value := term_tail.value <term>-> <factor> <factor_tail> factor_tail.subtotal := factor.value; term.value := factor_tail.value <term_tail1>-> + <term> <term_tail2> term_tail2.subtotal := term_tail1.subtotal+term.value; term_tail1.value := term_tail2.value | - <term> <term_tail2> term_tail2.subtotal := term_tail1.subtotal-term.value; term_tail1.value := term_tail2.value | empty term_tail1.value := term_tail1.subtotal <factor1>-> ( <expr> ) factor1.value := expr.value | - <factor2> factor1.value := -factor2.value | num factor1.value := num.value | id factor1.value := lookup(id.name) <factor_tail1> -> * <factor> <factor_tail2> factor_tail2.subtotal := factor_tail1.subtotal * factor.value; factor_tail1.value := factor_tail2.value | / <factor> <factor_tail2> factor_tail2.subtotal := factor_tail1.subtotal/factor.value; factor_tail1.value := factor_tail2.value | empty factor_tail1.value := factor_tail1.subtotal where lookup(id.name) returns the value of the identifier (assuming it is a variable with a value) from a name-value store, which could be an associative array or hashmap etc. in the implementation of an interpreter that uses this grammar to parse input. Note that we use nonterminals with indexes, so factor1 and factor2 are just the same factor nonterminal, but indexed so we can distinguish the nonterminals in the pro- ductions in the semantic rules. The empty in the productions shown above denotes . To goal of this project is to implement an interpreter that evaluates arithmetic expres- sions and that supports the use of variables in local scopes. The local scope is similar to the let construct in functional languages: let x = 2 in x * x end which evaluates to 4, 3 * ( let x = 2 in x * x end ) 1 which evaluates to 12, and 3 * ( let x = 2 in ( let y = x * x+1 in y/2 end ) + 1 end ) evaluates to 9 (integer division rounds towards zero)....
View Full Document
This note was uploaded on 02/01/2012 for the course COP 4020 taught by Professor Engelen during the Spring '11 term at FSU.
- Spring '11