4 Semantic Analysis Abstract Syntax Trees Objectives To
– 4– Semantic Analysis ( Abstract Syntax Trees)
Objectives • To Understand 1. The concept of Static Semantic Analysis 2. Syntax Directed Definitions & Construction of Syntax Trees 3. Bottom up Evaluation of S-attributed Definitions 4. L-attributed Definitions 5. Top Down Translation 6. Bottom up Evaluation of Inherited Attributes www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
Where are we now ? characters Scanner tokens Parser Source Parse Tree Semantic Abstract Actions Syntax Tre ( IR ) www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
• • Semantic Actions – Why? While Parsing checked the syntactical correctness of the program; There is a level of correctness that is not captured by a grammar 1. 2. 3. 4. Has a variable been declared? Are types consistent in an expression? In the assignment x=y, is y assignable to x? Does a method call have the right number and types of parameters? 5. In a selector p. q, is q a method or field of class p? 6. Is variable x guaranteed to be initialized before it is used? etc. www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
• • Semantic Actions – What? While Parsing checked the syntactical correctness of the program; Semantic Actions include: 1. Symbol table handling • • • Maintaining information about declared names Maintaining information about types Maintaining scopes 2. Checking context conditions • • Scoping rules Type checking 3. Invocation of code generation routines www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
Semantic Actions – when ? • Key Idea: At significant points during parsing perform some semantic action – Typically when a production is reduced (LR) or – at a convenient point in the parse (LL) • Semantic actions that can be performed during Parsing – Build (and return) a representation of the parsed chunk(large) of the input (compiler) – Perform some sort of computation and return result (interpreter) • Also Known. Static as Semantic Analysis www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
• Static Semantic Analysis – How ? Done by a technique called Syntax Directed Definition – A generalization (or Extension ) of a context free grammar in which each grammar symbol has an associated set of attributes 1. Associate information with a programming language construct by attaching attributes to the grammar symbols representing the constructs. 2. Values for attributes are computed by “ semantic rules” associated with the grammar production. – A node in grammar is a record with fields for holding information, www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
1. Attributes in Syntax Directed Definition • Each nonterminal ( an intermediate node in the parse tree) has associated properties or attributes • Example attributes could be: – Name – Memory Location – Type, – Font-size, etc • For each separate instance of the grammar symbol, there will be separate instance of the attribute. • A node in the parse tree for the grammar symbol will be a record holding these attributes as fields • Value of the attribute is defined by Semantic Rule associated with the production used at that node www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
1. 1 Types of Attributes E. val 2 + E 1. val num. val 1. Synthesized Attributes – An attribute of a grammar symbol is synthesized if it is computed from the attributes of its children in the parse tree corresponding to the rightmost derivation – Example: E --> E + num | num – E 2. val = E 1. val + num. val ( val of E 2 computed based on the VAL of E 1 and num) – Used Extensively in LR parsing practice – A syntax directed Definition that uses Synthesized Attributes is known as “S – attributed Definition” (S – attributed Definitions are based on LR Grammar) www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
D. type 1. 2 Types of Attributes 2. Inherited Attributes T. type L. type – An attribute of a grammar symbol is inherited, if it is computed from the attributes of its parents and siblings L. type – D → T {$2. type: =$1. type} L – L → id , {$3. type: =$1. type} L , L. type – Typical for LL parsing (top down). id – Convenient for expressing the dependence of a programming language construct on the context in which it appears – More natural to use with Syntax Directed Definition (Although it is possible to write a syntax directed Definition using only synthesized attributes) www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
2. Semantic Rules in Syntax Directed Defn • Define the value of an attribute associated with a grammar symbol • Associated with the grammar rule used at that node Producti on L→En E → E 1 + T E→T T → T 1 *F T→F Semantic Rule n Notes: n Associates an integer valued synthesized Print (E: Val) attribute called val E. Val = E 1. val + with each nonterminal T. val E, T, & F E. val = T. val n The token digit has a T. Val = T 1. val x synthesized attribute lexval whose value F. val www. Bookspar. com | Website for Students VTU - Notes - Question Paperswill be supplied by the T. val = |F. val
2. Semantic Rules in Syntax Directed Defn , y l l • Define the value of an attribute associated nwith ta a e zed d i grammar symbol c esi n i , h r t • Associated with the grammar ruletused at that node o n y a l S u es c l s : u Producti Semantic Rule k can Notes t i s e Associates e y c l d on n an integer n e i e v l ns i p s u valued synthesized l o L→En Print s(E: im Val) i c t ex i a n i attribute called val f f s o = d. Ee 1. valte+ E → E 1 + E. Val n u with each nonterminal o d b i i e t i r t T. val T t n u t i E, T, & F f b i A e r E. val = T. val t d t E→ T is S-a n The token digit has a h T an T→ T. Val = T 1. val x synthesized attribute is T 1 *F T→F lexval whose value F. val www. Bookspar. com | Website for Students VTU - Notes - Question Paperswill be supplied by the T. val = |F. val
S-attributed definition Example • The Parse Tree for 3* 5+ 4 Using Production rules L below is: E Productio n L→En E E → E 1 + T T E→T T → T 1 *F T→F F→(E) T F Digit * + T F F Digit
S-attributed definition Example (Contd. ) • The annotated parse tree with attribute grammer and synthesized attributes is: Semantic Rule E. Val E= 19 Print (E: Val) E. Val = E 1. val + T. val E. val = T. val L E. Val E = 15 + T. Val. T= 15 T. Val = T 1. val x F. val T. Val. T= 3 T. val = F. val F. Val. F= 3 F. val = E. val F. Val = digit. lex. Val Digit = 3 digit. lexval * T. Val. T= 4 F. Val F =4 digit. lex. Val Digit = 4 F. Val F= 5 digit. lex. Val Digit = 5
Attribute grammar Example • Consider the following CFG for signed binary numbers: – (1) Number →Sign List , – (3) List → List Bit | Bit , (2) Sign → + | – (4) Bit → 0 | 1 n Parse tree for string “ -1” n Parse tree for string “ -101” is: 1. 2. 3. 4. Number → Sign List → Sign Bit → Sign 1 →– 1 1. Number → Sign List 2. → Sign List Bit 3. → Sign List 1 4. → Sign List Bit 1 5. → Sign List 0 1 6. → Sign Bit 0 1 7. → Sign 1 0 1
Attribute Grammar Example (Contd. ) n Attribute Grammar for the CFG of signed binary numbers is :
Attribute Grammar Example (Contd. ) www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
Attribute Grammar Example (Contd. ) Val =– 5 Neg =True Pos =1 Val =4 Pos =2 4 Val = Pos =0 Val =5 Pos =0 Val =1 Pos =1 Val =0 Pos =2 Val =4 www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
Attribute Grammar Example ( Contd. ) Val =– 5 Neg =True Pos =1 Val =4 Pos =2 4 Val = Pos =2 Val =4 Pos =0 Val =5 Pos =0 Val =1 Pos =1 Val =0 • The example on the side Shows the decorated parse tree with attribute dependencies shown with arrows. • If the parse tree is taken out, • What is left is a DEPENDENCY GRAPH
Dependency Graph • A directed graph that depicts the interdependencies among the inherited and synthesized attributes at the nodes in a parse tree • Example: – Consider the grammar production E → E 1 + E 2 and the Corresponding semantic rule E. val : = E 1. val+ E 2. val – The parse tree would be: Val – The Dependency Graph ● E would be shown separately as E 1 ● + Val www. Bookspar. com | Website for Students | VTU - Notes - Question Papers E 2 ● Val 20
Dependency Graph • A directed graph that depicts the interdependencies among the inherited and synthesized attributes at the nodes in a parse tree • The order in which the semantic rules are evaluated is decided by the earlier node to the later node. • The dependency graph is constructed topologically; – Grammar is used to construct the parse tree from the input www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
Algorithm For constructing Dependency graph for each node in the parse tree do for each attribute a of the grammar symbol at n do Construct a node in the dependency Graph for a; for each node n in the parse tree do for each semantic rule b: = f(c 1, c 2, …. ck) associated with the production used at n do for i : = 1 to k do www. Bookspar. com | Website for Students | VTU - Notes - Question Papers 22
Abstract Syntax Tree (AST) • A condensed form of a parse tree useful in representing language constructs of a programming language • An intermediate representation that allows translation to be decoupled from parsing • For Example: The production: S → if B then S 1 else S 2 § Similarly, 3 * 5 + If-then+ as: 4 will have a syntax tree else will result in 4 * S 2 B S 1 3 www. Bookspar. com | Website for Students | VTU - Notes - Question Papers 5
ASTtree versus • While a parse keeps Parse track of Tree all productions used to build the parse tree, an AST is a condensed form of this with just the information needed in later stages of compilation or interpretation. www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
AST Construction for Expressions • Similar to the translation of expression into postfix form • Sub-trees are constructed for the sub-expressions by creating a node for each operator & operand • The children of the Operator node are the nodes representing the sub-expressions constituting the operands of the operator • In a node for operator, ( often called the Label of the node) One field identifies the operator & the rest contain pointer to the nodes of operands • Each node in the syntax tree can be represented as a record with several fields www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
• Constructing AST (Contd. ) Following functions are used to create the nodes of AST. ( Each function returns pointer to the newly created node) 1. mknode (op, left, right) node with label op and F Creates an operator two fields containing pointers left and right. F Creates an identifier node with label id and a 2. mkleaf (id, entry) field containing entry , a pointer to the symbol table entry. 3. mkleaf (num, entry) F Creates a number node with label num and a field containing val, the value of the number www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
Constructing AST – An Example + | | § Sentence a – 4 + c - | | id | to entry for a id | num| 4 to entry for c p 1 : = mkleaf (id, entry a p 2 : = mkleaf (num, 4) § The tree is built bottom-up p 3 : = mknode (‘-’, p 1, using the function call p 2) p 4: = mkleaf (id, entry c) sequence → p 5 : = mknode (‘+’, p 3, p www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
A SDD for Constructing AST PRODUCTI ON SEMANTIC RULE E. nptr : = mknode ( ‘+’ E 1. nptr, E → E 1 + T T. nptr ) E. nptr : = mknode ( ‘–’ E 1. nptr, E → E 1 – T T. nptr ) E→T E. nptr : = T. nptr ) T→(E) T. nptr : = E. nptr ) T → id T. nptr : = mkleaf ( id, entry ) • Uses underlying productions of the grammar to T → num T. nptr mkleaf ( num, and val) schedule calls : = to the functions mknode NOTE: mkleaf www. Bookspar. com | Website for Students • The synthesized attribute nptr for E and T keeps | VTU - Notes - Question Papers
A SDD for Constructing AST + | | E. Nptr - E. Nptr + T. nptr - | | num. nptr E. nptr T. nptr id | to entry for a num| 4 PRODUCTI ON T. nptr id | to entry for c SEMANTIC RULE E. nptr : = mknode ( ‘+’ E 1. nptr, T. nptr ) E. nptr : = mknode ( ‘–’ E 1. nptr, E → E 1 – T T. nptr ) E → E 1 + T E→T E. nptr : = T. nptr ) T→(E) T. nptr : = E. nptr ) T → id T. nptr : = mkleaf ( id, entry )
Directed A-cyclic graph (DAG) • Identifies common sub-expressions • Like syntax tree, – it has an interior node representing an operator and two children as operands; left & right. – The only difference is that the node in a DAG representing a common sub-expression has more than one parent in the syntax tree. • The common sub-expression would be represented as a duplicated sub-tree • Can also be used to represent a set of www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
DAG Usage – An Example • Consider the expression : a: = b*-c + b*-c n • The corresponding Syntax Tree would be: assign a + a * * uminus b c t 1 : = - c; c t 2 : = b * t 1; t 3 : = -c; t 4 : = b* t 3; t 5 : = t 2 + t 4; a : =| Website t 5; for Students www. Bookspar. com | VTU - Notes - Question Papers The corresponding DAG would be: assign + * b uminus c t 1 : = - c; t 2 : = b * t 1; t 3 : = t 2 + t 2; a : = t 3;
• • Translators for SDD A translator for an arbitrary SDD can be difficult to build However: 1. Large classes of syntax directed Definitions exist for which it s easier to build a translator 2. SDD’s with synthesized attributes ( A. k. a. S– attributed definitions) can be evaluated using Bottom-up Evaluation. Technique 3. SDD’s with Inherited attributes ( A. k. a. L– Attributed definitions ) are evaluated using top – down method known as “depth–first order” www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
Bottom-Up Evaluation of SDD’s • A translator for an S-attributed definition can often be implemented with the help of an LR -Parser generator. • From the S-attributed definition, the parser generator can construct a translator that can evaluate attributes as it parses the input. • One can have extra fields in the parser stack to hold the values of the synthesized attributes value. www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
Thank you www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
1. Syntax Directed Definition Summary SDD is a generalization of the Context Free Grammar. 2. Each symbol can have 2 kinds of attributes i. synthetic & inherited 3. The value of an attribute is defined by the semantic rule associated with the production 4. Synthetics computed from the children at the node and inherited from the siblings and parents of the node. 5. Semantic. Dependency rules set up the dependency of the Graph 35 attributes that will be represented as………. a graph www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
6. Syntax Directed Definition Summary The Dependency Graph deciding the order of evaluation of the semantic rules. . 7. The evaluation defines the values of the attribute at the nodes in the parse tree 8. A parse tree showing the attributes at each node is called an Annotated Parse Tree and the process of evaluation is called decorating the tree. 9. An SDD with only synthesized attributes exclusively is called S-attributed definition and can always be implemented bottom up 36 www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
Syntax Directed Definition – Another example • In this example, an inherited attribute distributes type information to the various identifiers in the declaration: Semantic Rule n Notes Productio n D→TL L. in: = T. type T → int T. Type : = integer T → real T. Type : = real L → L 1, id n The nonterminal T has a synthesized attribute type whose value is determined by the type declaration n Associated with the productions for L call L 1. in : = L. in procedure addtype to addtype (id. entry, add the type of each www. Bookspar. com | Website for Students | VTU - Notes - Question Papers identifier to its 37 entry in L. in)
Syntax Directed Definition – Another example • The corresponding Decorated Parser tree for the sentence • real id 1, id 2, id 3 D Production Semantic Rule T. type = real L. in = real D→TL L. in: = T. type T → int T. Type : = integer T → real T. Type : = real L → L 1, id L → id real L. in = real L 1. in : = L. in Addtype (id. entry, L. in) Id 1 www. Bookspar. com | Website for Students | VTU - Notes - Question Papers Addtype (id. entry, , , Id 2 38 Id 3
Dependency Graph Example Dependency Graph for the construct: D Semantic Rule real id 1, i L. in: = T. type T. Type : = integer T. Type : = real L 1. in : = L. in addtype (id. entry, L. in) 4 type T. type = real in 5 L. in = real In 6 in 7 L. in = real in 9 L. in = real in 8 , In 10 id 1 1 entry www. Bookspar. com | Website for Students | VTU - Notes - Question Papers , 3 entry id 3 id 22 entry 1, 2, 3 entry nodes 4, 5, 6, 7, 9 are inter 8, 10 are 39 link node
Input a * b+ cn b+ cn + cn cn n n Desk Calculator SDD with LR Stack Val parser. Production Rule a F T T* T*b T*F T E E+ E+c E+F E+T E En Input a F digit a a*b+c a T F a L→En a, b E → E 1 + T a, b F digit ab T T*F E→T ab E T T → T 1 *F ab , c T→F ab , c F digit F→(E) ab , c T F ab+c E E + T F → digit www. Bookspar. com | Website for Students | VTU ab+c - Notes - Question Papers 40
L – Attributed Definitions • A syntax directed definition where each inherited attribute of Xi for 1 <= i <= n, on the right side of A X 1, X 2, Xn, depends only on – The attribute symbols X 1, X 2, Xi-1 to the left of Xi in the production & – The inherited attributes of A • Note : Every S-attribute definition is L-attributed since the restrictions 1 and 2 above applies only to inherited attributes. • The order of evaluation of the attributes is the order in which the nodes of the parse tree are “created” by the parsing method ( Depth-first 41 order ) www. Bookspar. com | Website for Students | VTU - Notes - Question Papers
DEPTH – FIRST ORDER • Depth first evaluation order for attributes • procedure dfvisit (n : node ); • begin – for each child m of n, from left to right – do begin • evaluate inherited attributes of m; • dfvisit ( m ); – end; – evaluate synthesized attributes of n; • end www. Bookspar. com | Website for Students | VTU - Notes - Question Papers 42
Parse Tree for 9 – 5 + 2 GRAMMAR E TR R addtop T {print ( addtop. lexeme) } | R | T num { print ( num, val ) } E T 9 print(‘ 9’) R 5 T print(‘-’) print(‘ 5’) www. Bookspar. com | Website for Students | VTU - Notes - Question Papers R + T 2 print((‘ 2’) print(‘ 2’) 43 R
Example 9 • Production E E 1 + T E E 1 - T E T T 0 |1 |…. . |9 - 5 + 2 • Semantic rule E. t E 1. t || T. t ||’+’ E. t E 1. t || T. t ||’-’ E. t T. t T ‘ 0’ |’ 1’ |…. . |’ 9’ E. t =95 -2+ E. t = 95 E. t= 9 T. t =5 T. t = 2 T. t= 9 9 - 5 www. Bookspar. com | Website + for Students | VTU - Notes - Question Papers 2 44
Problem Consider the problem of translating decimal numbers between 0 to 99 into their English words / phrases. Number word / phrase 0 zero 1 One 10 Ten 11…. . Eleven 19 Nineteen 20 Twenty 30 Thirty 31 Thirty one www. Bookspar. com | Website for Students | VTU - Notes - Question Papers 45
Grammar 1. N : = D | D P 2. P : = D 3. D : = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 PARSE TREE N D P D 6 8 ANNOTATED PARSE TREE N. trans D. val . in P. trans D 1. val 6 8 www. Bookspar. com | Website for Students | VTU - Notes - Question Papers 46
Grammar 1. N : = D | D P 2. P : = D 3. D : = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 Production Semantic rule N : = D N. trans : = spell (D. val) N : = D P P. in : = D. val N. trans : = P. trans P : = D P. trans : = if D 1. val = 0 then decade (P. in) else if P. in <= 1 then spell (10*p. in + D 1. val) else decade (P. in) || spell (D 1. val) D : = 0 D. val : = 0 spell ( 1 ) = One …. ----- decade (1) = Ten ……. . www. Bookspar. com | Website for Students decade ( 9 ) = ninet | VTU - Notes - Question Papers 47 47
- Slides: 47