w5Color - Now the Specifics of Semantic Action Routines 1 A...

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

View Full Document Right Arrow Icon
1 Now the Specifics of Semantic Action Routines
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 A Common Compiler Structure: Semantic Actions Generate ASTs • In many compilers, the sequence of semantic actions generated by the parser build an abstract syntax tree ( AST , or simply syntax tree .) • After this step, many compiler passes operate on the syntax tree.
Background image of page 2
3 Tree Traversals After the AST has been built, it is traversed several times, for • testing attributes of the tree (e.g., type checking) • testing structural information (e.g., number of subroutine parameters) • optimizations • output generation.
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Semantic Actions and LL/LR Parsers • Actions are called either by parsing routines or by the parser driver. Both need provisions for semantic record parameter passing Example: <if-stmt> IF <expr> #start-if THEN <stmt-list> ENDIF #finish-if • For LL parsers, semantic actions are a perfect fit, thanks to their predictive nature • In LR parsers, productions are only recognized at their end. It may be necessary to split a production, generating “semantics hooks” <if-stmt> <begin-if> THEN <stmt-list> ENDIF #finish-if <begin-if> IF <expr> #start-if passing semantic record
Background image of page 4
5 Semantic Records • Associated with both nonterminals and terminals. • Can also be empty (e.g., for “;” or <stmt-list>). • Control statements often have 2 or more actions. • Typically semantic record information is generated by actions at symbols and is passed to actions at the end of productions. A good organization of the semantic records is the semantic stack.
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Semantic Stack Example • consider a:=b+1 • sequence of parse actions invoked: process_id, process_id, process_op, process_lit, gen_infix, gen_assign process_id a process_id a b process_op a b + process_lit a b + 1 gen_infix a b+1 gen_assign
Background image of page 6
7 Action-Controlled Semantic Stack • Action routines can push/pop semantic records directly onto/from the stack. This is called action-controlled stack. – Disadvantage: stack management has to be implemented in action routines.
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 LR Parser-Controlled Stack • Every shift operation pushes a semantic record onto the semantic stack, describing the token. • At a reduce operation, the production produces a semantic record and replaces all RHS records on the stack with it. The action procedures don’t see the stack. They see the semantic records as parameters. Example: YACC (use $1, $2, etc. as args)
Background image of page 8
9 LL Parser-Controlled Stack Remember: the parse stack contains predicted symbols, not the symbols already parsed. Entries for all RHS symbols (left-to-right) are also pushed onto the semantic stack and gradually filled in. When a production is matched: the RHS symbols are popped, the LHS symbol remains. Keep pointers to left,right,current symbol,and top for
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.

Page1 / 27

w5Color - Now the Specifics of Semantic Action Routines 1 A...

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