lec18 - CS415 Compilers Syntax-Directed Translation Schemes...

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

View Full Document Right Arrow Icon
CS415 Compilers Syntax-Directed Translation Schemes, type systems / type checking, simple code generation These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University
Image of page 1

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

View Full Document Right Arrow Icon
Lecture 18 2 cs415, spring 10 Example — Building an Abstract Syntax Tree Assume constructors for each node Assume stack holds pointers to nodes Assume yacc syntax Goal Expr $$ = $1; Expr Expr + Term $$ = MakeAddNode($1,$3); | Expr Term $$ = MakeSubNode($1,$3); | Term $$ = $1; Term Term * Factor $$ = MakeMulNode($1,$3); | Term / Factor $$ = MakeDivNode($1,$3); | Factor $$ = $1; Factor ( Expr ) $$ = $2; | number $$ = MakeNumNode(token); | id $$ = MakeIdNode(token);
Image of page 2
Lecture 18 3 cs415, spring 10 Making Ad-hoc SDT Work How do we fit this into an LR(1) parser? Need a place to store the attribute and their values Stash them in the stack, along with state and symbol Push three items each time, pop 3 x | β | symbols Need a naming scheme to access them $n translates into stack location: top - 3 x (| β | - n) Need to sequence rule applications On every reduce action, perform the action rule What about a rule that must work in mid-production? Can transform the grammar Split it into two parts at the point where rule must go and apply the rule on reduction to the appropriate part Introduce marker productions M ε with appropriate action
Image of page 3

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

View Full Document Right Arrow Icon
Lecture 18 4 cs415, spring 10 Reality Most parsers are based on this ad-hoc style of context- sensitive analysis Advantages Addresses the shortcomings of the AG paradigm Efficient, flexible Disadvantages Must write the code with little assistance Programmer deals directly with the details Most parser generators support a yacc-like notation
Image of page 4
Lecture 18 5 cs415, spring 10 Typical Uses (Semantic Analysis) Building a symbol table Enter declaration information as processed At end of declaration syntax, do some post processing Use table to check errors as parsing progresses Simple error checking/type checking Define before use lookup on reference Dimension, type, ... check as encountered Type conformability of expression bottom-up walk Procedure interfaces are harder Build a representation for parameter list & types Check actual vs. formal parameter list Positional or keyword associations assumes table is global
Image of page 5

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

View Full Document Right Arrow Icon
Lecture 18 6 cs415, spring 10 Is This Really “Ad-hoc” ? Relationship between practice and attribute grammars Similarities Both rules & actions associated with productions Application order determined by tools (Somewhat) abstract names for symbols Differences Actions applied as a unit; not true for AG rules Anything goes in ad-hoc actions; AG rules are (purely) functional AG rules are higher level than ad-hoc actions
Image of page 6