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: Semantic actions for declarations and expressions Sunday, September 11, 2011 Semantic actions Semantic actions are routines called as productions (or parts of productions) are recognized Actions work together to build up intermediate representations <if-stmt> ! IF <expr> #startif THEN <stmts> END #endif Semantic action for #startif needs to pass a semantic record to #endif For LL parsers, semantic actions work easily, because they are predictive For LR parsers, do not know which production is used until reduce step; need to place semantic actions at end of production <if-stmt> ! <begin-if> THEN <stmts> END #endif <begin-if> ! IF <expr> #startif Sunday, September 11, 2011 Semantic Records Data structures produced by semantic actions Associated with both non-terminals (code structures) and terminals (tokens/symbols) Do not have to exist (e.g., no action associated with ;) Control statements often require multiple actions (see <if- stmt> example on previous slide) Typically: semantic records are produced by actions associated with terminals, and are passed to actions associated with non-terminals Standard organization: semantic stack Sunday, September 11, 2011 Example of semantic stack Consider following grammar: And now annotated with semantic actions: assign ! ID := expr expr ! term addop term term ! ID | LIT addop ! + | assign ! ID #process_id := expr #gen_assign expr ! term addop term #gen_infix term ! ID #process_id | LIT #process_id addop ! + #process_p | #process_m Sunday, September 11, 2011 Example of semantic stack Consider a := b + 1; Sequence of semantic actions invoked: process_id, process_id, process_op, process_lit, gen_inx, gen_assign Sunday, September 11, 2011 How do we manipulate stack? Action-controlled : actions directly manipulate stack (call push and pop) Parser-controlled : parser automatically manipulates stack Sunday, September 11, 2011 LR-parser controlled Shift operations push semantic records onto stack (describing the token) Reduce operations pop semantic records associated with symbols off stack, replace with semantic record associated with production Action routines do not see stack. Can refer to popped off records using handles e.g., in yacc/bison, use $1, $2 etc. to refer to popped off records Sunday, September 11, 2011 LL-controlled Parse stack contains predicted productions, not matched productions Push empty semantic records onto stack when production is predicted Fill in records as symbols are matched When non-terminal is matched, pop off records associated with RHS, use to ll in the record associated with LHS (leave LHS record on stack) Sunday, September 11, 2011 Overview of declarations Symbol tables Action routines for simple declarations Action routines for advanced features Constants Enumerations Arrays Structs Pointers Sunday, September 11, 2011 Symbol Tables...
View Full Document
- Fall '08