ch5.1 cse 4100 chapter 5: syntax directed translation prof. steven a. demurjian computer science...
Post on 22-Dec-2015
231 views
TRANSCRIPT
CH5.1
CSE4100
Chapter 5: Syntax Directed TranslationChapter 5: Syntax Directed Translation
Prof. Steven A. Demurjian Computer Science & Engineering Department
The University of Connecticut371 Fairfield Way, Unit 2155
Storrs, CT [email protected]
http://www.engr.uconn.edu/~steve(860) 486 - 4818
Material for course thanks to:Laurent MichelAggelos KiayiasRobert LeBarre
CH5.2
CSE4100
OverviewOverview Review Supporting ConceptsReview Supporting Concepts
(Extended) Backus Naur Form Parse Tree and Schedule
Explore Basic Concepts/Examples of Attribute Explore Basic Concepts/Examples of Attribute GrammarsGrammars Synthesized and Inherited Attributes Actions as Direct Effect of Parsing
Examine more Complex ExamplesExamine more Complex Examples Attribute Grammars and Yacc – Jump to Slide SetAttribute Grammars and Yacc – Jump to Slide Set Constructing Syntax Trees During Parsing Translation Constructing Syntax Trees During Parsing Translation
is Two-Pass:is Two-Pass: First Pass: Construct Tree using Attribute Grammar Second Pass: Evaluate Tree (Perform Translation)
Concluding RemarksConcluding Remarks
CH5.3
CSE4100
BNF and EBNFBNF and EBNF Essentially Backus Naur Form for Regular Expressions Essentially Backus Naur Form for Regular Expressions
that we have Utilized to Date that we have Utilized to Date Extension - Reminiscent of regular expressionsExtension - Reminiscent of regular expressions EBNFEBNF
Extended Backus Naur Form
What is it?What is it? A way to specify a high-level grammar Grammar is
Independent of parsing algorithm Richer than “plain grammars” Human friendly [highly readable]
CH5.4
CSE4100
Optional and Alternative SectionsOptional and Alternative Sections
E → id ( A ) → id
A → integer→ id
E → id [ ( A ) ] A → integer
→ id
Optional Part!
E → id [ ( A ) ] A → { integer | id }
E → id [ ( A ) ] A → integer
→ id
Simplifying for Alternatives
CH5.5
CSE4100
Kleene ClosureKleene Closure Simplifies Grammar by Eliminating Epsilon RulesSimplifies Grammar by Eliminating Epsilon Rules
E → id [ ( [Args] ) ] Args → E [ , Args ]*
E → id [ ( Args ) ] Args → E Rest
→ εRest → , E Rest
→ εfoo()foo(x)foo(x,y)foo(x,y,z)foo(w,x,y,z)
CH5.6
CSE4100
Positive ClosurePositive Closure For having at least 1 occurrenceFor having at least 1 occurrence
L → S+S → if .... → while .... → repeat ....
L → S L’L’ → S L’ → εS → if .... → while .... → repeat ....
CH5.7
CSE4100
C-- EBNF StyleC-- EBNF Style
CH5.8
CSE4100
C-- EBNF StyleC-- EBNF Style
CH5.9
CSE4100
Parse Trees - The ProblemParse Trees - The Problem In TDP or BUP, the Token Stream (from Lex) is Supplied to ParserIn TDP or BUP, the Token Stream (from Lex) is Supplied to Parser
Parser Produces Yes/No Answer if Successful
However, this is Not Sufficient for Code Generation, Optimization, However, this is Not Sufficient for Code Generation, Optimization, etc.etc. Desired Outcome from Parsing is:
CH5.10
CSE4100
What is a Parse Tree ?What is a Parse Tree ? Two Options for a Parse Two Options for a Parse
Tree:Tree:A true physical parse tree
that contains the program structure and associated relevant tokens
A schedule of operations that must be performed
Base exampleBase example
y := 5;x := 10 + 3 * yy := 5;x := 10 + 3 * y
CH5.11
CSE4100
Physical TreePhysical Tree PositivePositive
Depicts the grammatical structure Should be easy to create while parsing Unambiguous Easy to manipulate
NegativeNegative Not “Operational” Not closer to final product (code) Compilation requires multiple passes
CH5.12
CSE4100
What is a Schedule?What is a Schedule? Schedule is aSchedule is a
Sequence of OperationsSequence of Operations
Not only StructureNot only Structure(Parse Tree), but(Parse Tree), butway to Evaluate itway to Evaluate it
Sequence of StepsSequence of StepsLeading to “Code”Leading to “Code”
Ability to “Evaluate”Ability to “Evaluate”Tokens as Parsed Tokens as Parsed
Result: Result: Value or “Code”Value or “Code”
y := 5;x := 10 + 3 * yy := 5;x := 10 + 3 * y
CH5.13
CSE4100
Schedule [a.k.a. Dependency Graph]Schedule [a.k.a. Dependency Graph] PositivePositive
This is almost runnable code ! It give the sequence of step to follow We bypassed the parse tree altogether (so this is
lightweight) Compilation doable in a single pass
NegativeNegative Harder to manipulate Can it always be created ? What is the connection with the
grammar ?
CH5.14
CSE4100
What is the Trade-Off ?What is the Trade-Off ? Physical Parse TreePhysical Parse Tree
Requires multiple pass for compilation Very flexible This is what we will use
Schedule [Dependency Graph]Schedule [Dependency Graph] Requires a single pass for compilation Less flexible
Bottom-line Bottom-line The construction of both rely on the same technique
Attributed Grammars
CH5.15
CSE4100
What is the Desired Goal?What is the Desired Goal? Change the parser or the grammarChange the parser or the grammar
To automatically build the parse tree FactsFacts
We have three parsing techniquesRecursive DescentLL(k)LR(k) (and LALR(1))
CorollaryCorollary Find a way to instrument each technique to get the
tree Pre-requisitePre-requisite
You must understand what the trees look like.
CH5.16
CSE4100
Examples of TreesExamples of Trees
a.b
a + b * c
a.b(x)
a.b(x)[y]
x = a + b
CH5.17
CSE4100
Tree for a Code SegmentTree for a Code Segment
while x<n { x = x + 1;
b.foo(x);}
CH5.18
CSE4100
E E + T Id + T Id + Id
Key IssueKey Issue How to build the tree while parsing ?How to build the tree while parsing ? IdeaIdea
Use the grammar
E → E + T
→ T T → Id
T Id Sites wherewe must
Take an action
CH5.19
CSE4100
ActionAction What is the nature of the action?What is the nature of the action? AnswerAnswer
It depends on the production!
E → E + T
Here we know thatOn top of the stack we must have two operandsSo....
Action = a = pop();b = pop();c = new Addition(a,b);push(c);
CH5.20
CSE4100
What is Going OnWhat is Going On We We synthesizesynthesize the tree the tree
While parsing In a bottom-up fashion
What we needWhat we need A stack to hold the synthesized “values” Actions inserted in the grammar
Issues to approachIssues to approach Where do we attach the actions in productions ? How do we attach the actions ? How can we automate the process ? It this always bottom-up ?
CH5.21
CSE4100
Attribute GrammarsAttribute Grammars A Language Specification Technique for TranslationA Language Specification Technique for Translation Attribute Grammar Contains:Attribute Grammar Contains:
Attributes (for Each NT in Grammar) Evaluation (Action) Rules (AKA: Semantic Rules) Conditions (Optional) for Evaluation
Main Concepts:Main Concepts: Each Attributed Define with Set of Values Values Augment Syntax/Parse Tree of Input String Attributes Associated with Non-Terminals Evaluation Rules Associated with Grammar Rules Conditions Constrain Attribute Values
Objective: 1. Compute attributes automatically andObjective: 1. Compute attributes automatically and2. Trigger rules when the production is used2. Trigger rules when the production is used
CH5.22
CSE4100
A First ExampleA First Example Consider Grammar for Unsigned IntegersConsider Grammar for Unsigned Integers
Objective:Objective: Develop Attribute Grammar that Generates Actual
Unsigned Integers from 0 to 32,767 Recall Tokens for Lexical Analyzer are Strings,
Namely “2” and “7” Begin by Augmenting Grammar with U → NU → N
N → DN → DN → N DN → N DD → 0 | 1 | …. | 8 | 9D → 0 | 1 | …. | 8 | 9
N ND DD 2D 27
N
DN
D
2
7
CH5.23
CSE4100
Define AttributeDefine Attribute Attribute “val” Tracks Actual Value of Unsigned Attribute “val” Tracks Actual Value of Unsigned
Integer as Input is Scanned and ParsedInteger as Input is Scanned and Parsed
How is 27 Evaluated?How is 27 Evaluated?
Production RulesProduction RulesU → N U → N N → NN → N1 D D
N → D N → D D → D → digitdigit
Evaluation/Semantic RulesEvaluation/Semantic RulesPrint(N.val)Print(N.val)N.val 10 * NN.val 10 * N1.val + D.val.val + D.val
N.val D.valN.val D.valD.val D.val digit.lexemedigit.lexeme
→→→→→→
N
DN1
D
2
7
CH5.24
CSE4100
Evaluation/Semantic Rules into GrammarEvaluation/Semantic Rules into Grammar
U → N U → N { U.val := N.val }{ U.val := N.val }
N → NN → N1 D D
{ N.val := 10 * N{ N.val := 10 * N1.val + D.val .val + D.val
Condition: N.val Condition: N.val ≤ 32,767 ≤ 32,767 }}N → DN → D
{ N.val := D.val }{ N.val := D.val }D → D → digitdigit
{D.val := {D.val := digit.lexeme digit.lexeme }}
N
DN1
D
2
3
D
1
N1
CH5.25
CSE4100
Two Types of AttributesTwo Types of Attributes Synthesized AttributesSynthesized Attributes
Information (Values) move Up Tree from Leaves towards Root
Value (Node) is Synthesized (Calculated) form Subset of its Children
Previous Example had “val” as Synthesized
val1 val2 val3
CH5.26
CSE4100
Second Example of Synthesized AttributesSecond Example of Synthesized Attributes
L → E nL → E n { print (E.val)}{ print (E.val)}E → EE → E1 + T + T { E.val := E{ E.val := E1 + T.val} + T.val}
E → TE → T { E.val := T.val }{ E.val := T.val }T → TT → T1 * F * F { T.val := T{ T.val := T1 * F.val} * F.val}
T → FT → F { T.val := F.val }{ T.val := F.val }F → (E)F → (E) { F.val := E.val }{ F.val := E.val }F → UF → U {F.val := U.val}{F.val := U.val}
CH5.27
CSE4100
Combining First Two ExamplesCombining First Two Examples
L → E nL → E n { print (E.val)}{ print (E.val)}E → EE → E1 + T + T { E.val := E{ E.val := E1 + T.val} + T.val}
E → TE → T { E.val := T.val }{ E.val := T.val }T → TT → T1 * F * F { T.val := T{ T.val := T1 * F.val} * F.val}
T → FT → F { T.val := F.val }{ T.val := F.val }F → (E)F → (E) { F.val := E.val }{ F.val := E.val }F → F → digitdigit {F.val := {F.val := digit.lexeme digit.lexeme }}U → N U → N { U.val := N.val }{ U.val := N.val }N → NN → N1 D D { N.val := 10 * N{ N.val := 10 * N1.val + D.val .val + D.val
Condition: N.val Condition: N.val ≤ 32,767 ≤ 32,767 }}N → DN → D { N.val := D.val }{ N.val := D.val }D → D → digitdigit {D.val := {D.val := digit.lexeme digit.lexeme }}
CH5.28
CSE4100
Two Types of AttributesTwo Types of Attributes Inherited AttributesInherited Attributes
Information for Node Obtained from Node’s Parent and/or Siblings
Used to Keep Track of Context Dependencies Location of Identifier on RHS vs. LHS of Assignment Type Information for Expression
These are Context Sensitive Issues!
val
CH5.29
CSE4100
Example of Inherited AttributesExample of Inherited Attributes
Production RulesProduction RulesD → T L D → T L T → T → intint T → T → realreal L → L L → L , id, idL → L → idid
D TL intint L int int L , id , id int int L , id , id , id , id int id , id , idint id , id , id
D
LT
realreal idid
“int”
D
T L
idid
idid,,L
Where is Type InformationWith respect to Identifiers?
CH5.30
CSE4100
Example of Inherited AttributesExample of Inherited Attributes
D → T LD → T L { L.in := T.type } { L.in := T.type } T → T → intint {T.type := integer }{T.type := integer }T → T → realreal {T.type := real }{T.type := real }L → LL → L1 , id, id {L{L1.in := L.in ; addtype (id.entry, L.in)}.in := L.in ; addtype (id.entry, L.in)}
L → L → idid {addtype (id.entry, L.in)}{addtype (id.entry, L.in)}
D
T.type = real L.in = real
idid11
idid22
,,
realrealL.in = real
type is a synthesized attributein is an inherited attribute
CH5.31
CSE4100
Formal Definitions of AttributesFormal Definitions of Attributes Given a production Given a production A → αA → α We can write a We can write a semanticsemantic rulerule b := f(cb := f(c11,c,c22,...,c,...,ckk)) There are Two possibilitiesThere are Two possibilities
Synthesisb is a synthesized attribute for A ci are attributes from non-terminals appearing in αInformation flows up – hence Bottom-up computation
Inheritanceb is an inherited attribute for a non-terminal appearing in αci are attributes from non-terminals appearing in α or an
attribute of AInformation flows down - hence Top-down computation
CH5.32
CSE4100
Inherited AttributesInherited Attributes
SummarySummary These attributes are computed while going down The same could be achieved with post-processing
FactFact Inherited attributes exist for one reason only
A FASTER compilation– Avoid a “pass” over the tree to decorate
– Everything happens during the parsing
» Parse
» Construct the tree
» Decorate the tree
This is an OPTIMIZATION of the compilation processThe truly important bit is synthesized attributes
CH5.33
CSE4100
Other Attribute Grammar ConceptsOther Attribute Grammar Concepts L-Attributed Definitions: Attribute Grammars that can L-Attributed Definitions: Attribute Grammars that can
always be Evaluated in a Depth-First Fashionalways be Evaluated in a Depth-First Fashion Consider the Rule: Consider the Rule: A → XX11 X X2 … 2 … XXnn
A Syntax-Directed Definition (AG) is L-Attributed if A Syntax-Directed Definition (AG) is L-Attributed if Every Inherited Attribute XEvery Inherited Attribute Xjj in Rule Depends on: in Rule Depends on: Attributes of X1 X2 … Xj-1 which are to the Left of Xj
in the Parse Tree The Inherited Attributes of A
Every Synthesized Attribute Grammar is L-AttributedEvery Synthesized Attribute Grammar is L-Attributed L-Attributed Definitions are True for each Production L-Attributed Definitions are True for each Production
Rule and the Entire GrammarRule and the Entire Grammar
CH5.34
CSE4100
Translation SchemesTranslation Schemes Combining Attribute Grammars and Grammar Rules to Combining Attribute Grammars and Grammar Rules to
Translate During the Parse (One-Pass)Translate During the Parse (One-Pass) Evaluating Attribute Grammar for an Input String as Evaluating Attribute Grammar for an Input String as
We’re ParsingWe’re Parsing Translations can Take Many Different FormsTranslations can Take Many Different Forms What is the Grammar Below For?What is the Grammar Below For? What Can we Do as Scan Input?What Can we Do as Scan Input?
Convert Infix to Postfix!
E → T R E → T R R → R → addop addop T R T R R → R → εε T → T → numnum
CH5.35
CSE4100
Infix to Postfix Translation SchemeInfix to Postfix Translation Scheme A Translation Scheme Embeds Actions (Semantic A Translation Scheme Embeds Actions (Semantic
Rules) into Right Hand Side of Production RulesRules) into Right Hand Side of Production Rules
E → T R E → T R R → R → addop addop T {print(addop.lexeme)} RT {print(addop.lexeme)} R11
R → R → εε T → T → num num {print(num.val)} {print(num.val)}
E
T R
55
print(‘-’)print(‘-’)99T--
print(‘5’)print(‘5’)
print(‘9’)print(‘9’)
εε
print(‘+’)print(‘+’)T ++
print(‘2’)print(‘2’)22
Input: 9-5+2Input: 9-5+2
R1
R1
Why is print(addop)embedded within rule?
CH5.36
CSE4100
What’s Key Issue with Translation Schemes?What’s Key Issue with Translation Schemes? Placement!Placement! Consider:Consider:
Where is Semantic Rule Placed in Production Rule?Where is Semantic Rule Placed in Production Rule? What about:What about:
Is this OK?Is this OK? What is the Correct Placement?What is the Correct Placement?
T → TT → T11 * * FF T.val = TT.val = T11.val * F.val.val * F.val
T → TT → T11 * * {T.val = T{T.val = T11.val * F.val}.val * F.val} FF
CH5.37
CSE4100
Placement RulesPlacement Rules An Inherited Attribute for Symbol on Right Hand Side An Inherited Attribute for Symbol on Right Hand Side
of a Production Rule Must be Computed in an Action of a Production Rule Must be Computed in an Action BEFORE the SymbolBEFORE the Symbol This Implies that the Evaluation/Semantic Rule is
Placed at Differing Positions in the Right Hand Side of a Production Rule
An Action Can’t Refer to a Synthesized Attribute of a An Action Can’t Refer to a Synthesized Attribute of a Symbol to the Right of an Action in a Production RuleSymbol to the Right of an Action in a Production Rule
A Synthesized Attribute of a Non-Terminal on the A Synthesized Attribute of a Non-Terminal on the Left-Hand Side of a Production Rule can Only be Left-Hand Side of a Production Rule can Only be Computed After ALL Attributes it References has Computed After ALL Attributes it References has Been Computed:Been Computed: This Implies that the Evaluation/Semantic Rule is
Placed (Usually) at the End of the Right Hand Side of a Production Rule
CH5.38
CSE4100
Consider a More Complex ExampleConsider a More Complex Example Consider a Grammar for Subscripts: E sub 1 means EConsider a Grammar for Subscripts: E sub 1 means E1 1 Focus on Relationship Between E and 1Focus on Relationship Between E and 1
Point Size – ps (Inherited)– Size of Characters Displacement – disp – Up/Down Offset
S → B S → B B.ps = 10B.ps = 10S.ht = B.htS.ht = B.ht
B → BB → B11 B B22 BB11.ps = B.ps.ps = B.ps
BB22.ps = B.ps.ps = B.ps
B.ht = max(BB.ht = max(B11.ht, B.ht, B22.ht).ht)
B → BB → B11 subsub B B22 BB11.ps = B.ps.ps = B.ps
BB22.ps = shrink (B.ps).ps = shrink (B.ps)
B.ht = disp(BB.ht = disp(B11.ht, B.ht, B22.ht).ht)
T → T → text text B.ht = text.h * B.psB.ht = text.h * B.ps
CH5.39
CSE4100
Where are Semantic Rules Placed?Where are Semantic Rules Placed? Placement Across Multiple Lines Clearly Identifies Placement Across Multiple Lines Clearly Identifies
Evaluations/Actions that are Performed and When they Evaluations/Actions that are Performed and When they are Performed!are Performed!
S → S → {B.ps = 10 }{B.ps = 10 } BB {S.ht = B.ht}{S.ht = B.ht}B → B → {B{B11.ps = B.ps}.ps = B.ps}
BB11 {B{B22.ps = B.ps}.ps = B.ps}
BB22 {B.ht = max(B{B.ht = max(B11.ht, B.ht, B22.ht)}.ht)}
B → B → {B{B11.ps = B.ps}.ps = B.ps}
BB11
subsub {B{B22.ps = shrink (B.ps)}.ps = shrink (B.ps)}
BB22 {B.ht = disp(B{B.ht = disp(B11.ht, B.ht, B22.ht)}.ht)}
T → T → text text {B.ht = text.h * B.ps}{B.ht = text.h * B.ps}
CH5.40
CSE4100
Another Example: Pascal to C ConversionAnother Example: Pascal to C Conversion Consider Pascal Grammar for Declarations, Example, Consider Pascal Grammar for Declarations, Example,
and C Equivalentand C Equivalent
V → V → varvar D; D; D → D D → D ;; D D D → D → idid T TT → T → integerintegerT → T → realrealT → T → charcharT → T → array[num .. num] of array[num .. num] of TT
Pascal: Pascal: var var i: integer;i: integer;
x: real;x: real;y: array[2..10] of char; y: array[2..10] of char;
C: C: int int i;i;floatfloat x;x;charchar y[9]; y[9];
Let’s Constructthe Parse Tree
and Attribute Grammar
CH5.41
CSE4100
Consider Sample Parse TreeConsider Sample Parse Tree
CH5.42
CSE4100
Grammar and RulesGrammar and Rules
V → V → varvar D; D; {V.decl = D.decl}{V.decl = D.decl}
D → DD → D11 ;; D D22 {D.decl = D{D.decl = D11.decl || D.decl || D22.decl}.decl}
D → D → idid T T
{D.decl = T.type || ‘b’ || id.lexeme || T.array || ‘;’}{D.decl = T.type || ‘b’ || id.lexeme || T.array || ‘;’}
T → T → integerinteger { T.type = “int” ; T.array = “” }{ T.type = “int” ; T.array = “” }
T → T → realreal { T.type = “float” ; T.array = “” }{ T.type = “float” ; T.array = “” }
T → T → charchar { T.type = “char” ; T.array = “” }{ T.type = “char” ; T.array = “” }
T → T → array[numarray[num11 .. num .. num22] of ] of TT
{ T.type = “char” ; { T.type = “char” ;
T.array = ‘[’ || string(numT.array = ‘[’ || string(num22 – num – num11 + 1) || ‘]’ } + 1) || ‘]’ }
CH5.43
CSE4100
Consider Database Language TranslationConsider Database Language Translation SQL:SQL:
ABDLABDL
SELECTSELECT column-name-listcolumn-name-listFROMFROM relation-listrelation-list[WHERE [WHERE boolean-expressionboolean-expression]][ORDER BY[ORDER BY column-namecolumn-name] ]
RETRIEVERETRIEVE boolean-expression (target-list) boolean-expression (target-list)[BY[BY column-namecolumn-name] ]
CH5.44
CSE4100
Consider Database Language TranslationConsider Database Language Translation SQL:SQL:
ABDLABDL
Note: Similarities and Differences …Note: Similarities and Differences … Very Straightforward to Translate!Very Straightforward to Translate!
SELECTSELECT Course#, PCourse#Course#, PCourse#FROMFROM PrereqPrereqWHERE WHERE Course#=CSE4100Course#=CSE4100ORDER BY ORDER BY PCourse#PCourse#
RETRIEVERETRIEVE ((File = Prereq) and (Course# =CSE4100)) ((File = Prereq) and (Course# =CSE4100)) (Course#, PCourse#)(Course#, PCourse#) BY BY PCourse#PCourse#
CH5.45
CSE4100
Syntax Tree Construction/EvaluationSyntax Tree Construction/Evaluation Recall: Parse Tree Contains Non-Terminals and Recall: Parse Tree Contains Non-Terminals and
Terminals that Corresponds to DerivationTerminals that Corresponds to Derivation For Simplistic Grammars and Input Streams, the Parse For Simplistic Grammars and Input Streams, the Parse
Tree can be Very LargeTree can be Very Large Solution:Solution:
Replace “Parse Tree” with Syntax Tree which is an Abridged Version
Two-Fold Objective:Two-Fold Objective: Construction of Syntax Tree via Attribute Grammar
as a Side Effect of Parsing Process Evaluating Syntax Trees
CH5.46
CSE4100
Typical ExampleTypical Example Parse Tree for a – 4 + cParse Tree for a – 4 + c
Syntax Tree:Syntax Tree:
E → E + T | E – T | TE → E + T | E – T | TT → ( E ) | id | numT → ( E ) | id | num
E
TE
T
T
E
num=4
id=a
id=c-
+
-
+
-
id
id
num 4
to entry for a
to entry for c
Where does this go?
CH5.47
CSE4100
How is Syntax Tree Constructed?How is Syntax Tree Constructed? Introduce a Number of Functions:Introduce a Number of Functions:
mknode (op, left, right) mkleaf (id, entry) mkleaf (num, entry)All Functions Return Pointers to Syntax Tree Nodes
For Syntax Tree on Prior Slide:For Syntax Tree on Prior Slide: p1 := mkleaf (id, entry a) p2 := mkleaf (num, 4) p3 := mknode (‘-’, p1, p2) p4 := mkleaf (id, entry b) p5 := mknode (‘+’, p3, p4)
What are Semantic Rules for this?What are Semantic Rules for this?
CH5.48
CSE4100
Attribute Grammar for Syntax TreeAttribute Grammar for Syntax Tree The Attribute nptr is SynthesizedThe Attribute nptr is Synthesized All Semantic Rules Occur after Right Hand Side of All Semantic Rules Occur after Right Hand Side of
Grammar RuleGrammar Rule What Does this Attribute Grammar Assume?What Does this Attribute Grammar Assume?
Lexical Analysis is Inserting ids into Symbol Table
Approach is Generalizable!Approach is Generalizable!
E → E1 + TE → E1 - TE → TT → ( E )T → idT → num
E.nptr := mknode(‘+’, E1.nptr,T.nptr)E.nptr := mknode(‘-’, E1.nptr,T.nptr)E.nptr := T.nptrT.nptr := E.nptrT.nptr := mkleaf(id, id.entry)T.nptr := mkleaf(num, num.val)
CH5.49
CSE4100
AAbstract bstract SSyntax yntax TTree [AST]ree [AST] An instance of the Composite Design PatternAn instance of the Composite Design Pattern
Abstract Node Concrete Node Combined in a class hierarchy
CH5.50
CSE4100
An AST InstanceAn AST Instance ExampleExample
x + y * 3
CH5.51
CSE4100
Building Physical Syntax Trees Building Physical Syntax Trees StraightforwardStraightforward
Write adequate semantic rules! Semantic attribute (val) is a pointer to a tree node
S → E $E → E + TE → TT → T * FT → FF → ( E )F → integer
print(E.val)E.val := new ASTAdd(E1.val,T.val)E.val := T.valT.val := new ASTMul(T1.val,F.val)T.val := F.valF.val := E.valF.val := new ASTInt(integer.val)
CH5.52
CSE4100
Concluding Remarks/Looking AheadConcluding Remarks/Looking Ahead Attribute Grammars are a Powerful Tool for Attribute Grammars are a Powerful Tool for
Specifying Translation SchemesSpecifying Translation Schemes Parse-Translator one of the Most Practical Compiler Parse-Translator one of the Most Practical Compiler
ApplicationsApplications Remainder of the Semester Highlights Other Critical Remainder of the Semester Highlights Other Critical
Issues in CompilersIssues in Compilers Typing and Type Checking Runtime Environment Optimization Code Generation