Lecture-05-6up - Semantic actions for declarations and expressions Monday October 4 2010 Semantic actions • Semantic actions are routines called

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Semantic actions for declarations and expressions Monday, October 4, 2010 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 Monday, October 4, 2010 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 Monday, October 4, 2010 Example of semantic stack • Consider a := b + 1; • Sequence of semantic actions invoked: process_id, process_id, process_op, process_lit, gen_in¡x, gen_assign Monday, October 4, 2010 How do we manipulate stack? • Action-controlled : actions directly manipulate stack (call push and pop) • Parser-controlled : parser automatically manipulates stack Monday, October 4, 2010 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 Monday, October 4, 2010 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) Monday, October 4, 2010 Overview of declarations • Symbol tables • Action routines for simple declarations • Action routines for advanced features • Constants • Enumerations • Arrays • Structs • Pointers Monday, October 4, 2010 Symbol Tables • Table of declarations, associated with each scope • One entry for each variable declared • Store declaration attributes (e.g., name and type) – will discuss this in a few slides • Table must be dynamic (why?) • Possible implementations • Linear list (easy to implement, only good for small programs) • Binary search trees (better for large programs, but can still be slow)...
View Full Document

This note was uploaded on 02/19/2012 for the course ECE 468 taught by Professor Test during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 7

Lecture-05-6up - Semantic actions for declarations and expressions Monday October 4 2010 Semantic actions • Semantic actions are routines called

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online