03-03syntaxTrees - Recall Syntax-directed translation...

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

View Full Document Right Arrow Icon
1 CSE 450: Compilers K. Stirewalt Semantic analysis Part 3: Building syntax trees Topics: – syntax trees – embedded actions in yacc – inherited attributes in yacc Readings: Sections 5.3 of the Dragon Book CSE 450: Compilers K. Stirewalt Recall: Syntax-directed translation Method of translation accomplished by attaching program fragments to productions in a grammar Based on two concepts: – Associate data attributes (or values) with symbols in a grammar – Augment grammar productions with code fragments Fragments executed when a production is used Combined execution of fragments, in order induced by the parse, effects a translation of the program being compiled CSE 450: Compilers K. Stirewalt L-attributed syntax-directed defns An SDD is S-attributed if every attribute is synthesized The expression grammar is an example Easily implemented in a bottom-up parse An SDD is L-attributed if each attribute is either: synthesized, or inherited, but with limitations on how the attribute may be used in rules Limitations on L-attributed SDDs: Dependency graph of any parse tree must be traversible from top to bottom and left-to-right---never right to left L-attributed SDDs useful for building abstract syntax trees CSE 450: Compilers K. Stirewalt From an SDD to an SDT The rules for turning an L-attributed SDD into an SDT are as follows: 1. Embed the action that computes the inherited attributes for a non-terminal A before that occurrence of A in the body of the production. If several inherited attributes for A depend on one another in acyclic fashion, order them appropriately. 2. Place the actions that compute a synthesized attribute for the head of a production at the end of the body of that production CSE 450: Compilers K. Stirewalt Recall: Symbol values in yacc Every symbol in a yacc parser has a value – gives additional information about the symbol – e.g., if symbol is a terminal that represents a numeric literal, the value might be the numeric value of the literal when it was scanned Values may be referenced and defined inside actions in a yacc grammar – E.g., A: X Y Z { $$ = f($1, $2, $3); } – Here, $$ refers to the value of the symbol A – It is computed in terms of the values of symbols X ($1), Y ($2), and Z ($3). CSE 450: Compilers K. Stirewalt Embedded actions in yacc Yacc’s parsing technique allows actions only at the end of a rule It simulates embedded actions by: – Making up a new rule with new non-terminal at the head and with an empty body – Moving the embedded action to the end of this new rule – Replacing action in original rule with the newly generated non-terminal symbol
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 CSE 450: Compilers K. Stirewalt Example embedded action Consider the following snippet: thing: A { cout << “Seen an A”; } B ; CSE 450: Compilers K. Stirewalt Example embedded action Embedded action… thing: A { cout << “Seen an A”; } B ; CSE 450: Compilers K. Stirewalt Example embedded action Embedded action is replaced with new symbol… thing: A fakename B ; CSE 450: Compilers K. Stirewalt Example embedded action
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