03-03syntaxTrees - 1 CSE 450 Compilers K Stirewalt Semantic...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 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…...
View Full Document

This note was uploaded on 07/25/2008 for the course CSE 450 taught by Professor Stirewalt during the Spring '08 term at Michigan State University.

Page1 / 7

03-03syntaxTrees - 1 CSE 450 Compilers K Stirewalt Semantic...

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

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