{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture-05-6up

lecture-05-6up - Semantic actions Semantic actions for...

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

View Full Document Right Arrow Icon
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_infix, 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
Image of page 1

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

View Full Document Right Arrow Icon
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 fill 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) Hash tables (best solution) BSTs and Hash tables can be difficult to implement, but languages like
Image of page 2
Image of page 3
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