lecture-05 - Semantic actions for declarations and...

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

View Full Document Right Arrow Icon
Semantic actions for declarations and expressions Monday, October 4, 2010
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
Example of semantic stack Consider a := b + 1; Sequence of semantic actions invoked: process_id, process_id, process_op, process_lit, gen_inFx, gen_assign Monday, October 4, 2010
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
Overview of declarations Symbol tables Action routines for simple declarations Action routines for advanced features Constants Enumerations Arrays Structs Pointers Monday, October 4, 2010
Background image of page 8
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) Hash tables (best solution) BSTs and Hash tables can be difFcult to implement, but languages like C++ and Java provide implementations for you Monday, October 4, 2010
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the 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.

Page1 / 40

lecture-05 - Semantic actions for declarations and...

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

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