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

Info icon This 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
Image of page 1

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

View Full Document Right 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.
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.
Image of page 3

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

View Full Document Right 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
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.
Image of page 5

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

View Full Document Right 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
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.
Image of page 7

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

View Full Document Right 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)
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.
Image of page 9

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

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern