lec18 - CS415 Compilers Syntax-Directed Translation...

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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 + Term $$ = MakeAddNode($1,$3); | $$ = MakeSubNode($1,$3); | $$ = $1; Term * Factor $$ = MakeMulNode($1,$3); | / $$ = MakeDivNode($1,$3); | $$ = $1; ( ) $$ = $2; |n u m b e r $$ = MakeNumNode(token); |i d $$ = MakeIdNode(token);
Background 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
Background image of page 3

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

View Full DocumentRight 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
Background 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
Background image of page 5

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

View Full DocumentRight 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
Background image of page 6
Lecture 18 7 cs415, spring 10 Types and Type Systems Type : A set of values and meaningful operations on them Types provide semantic “sanity checks” (consistency checks) and determine efficient implementations for data objects Types help identify errors, if an operator is applied to an incompatible operand ± dereferencing of a non-pointer ±
Background image of page 7

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

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

This note was uploaded on 10/29/2010 for the course CS 198:415 taught by Professor Kremer,u. during the Spring '10 term at Rutgers.

Page1 / 35

lec18 - CS415 Compilers Syntax-Directed Translation...

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

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