slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Compiler PowerPoint Presentation


158 Views Download Presentation
Download Presentation


- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Compiler Chapter# 4 Semantic Analysis

  2. 2 The Compiler So Far • Lexical analysis • Detects inputs with illegal tokens • e.g.: main$ (); • Syntax analysis • Detects inputs with ill-formed parse trees • e.g.: missing semicolons • Semantic analysis • Last “front end” analysis phase • Catches all remaining errors

  3. 3 Typical Semantic Errors • Multiple declarations:a variable should be declared (in the same scope) at most once • Undeclared variable:a variable should not be used before being declared • Type mismatch:type of the LHS of an assignment should match the type of the RHS • Wrong arguments:methods should be called with the right number and types of arguments

  4. Introduction • We can associate information with a language construct by attaching attributes to the grammar symbols. • A syntax directed definition specifies the values of attributes by associating semantic rules with the grammar productions. Semantic Rule Production E.code=E1.code||T.code||’+’ E->E1+T • We may alternatively insert the semantic actions inside the grammar E -> E1+T {print ‘+’}

  5. Syntax Directed Definitions • A SDD is a context free grammar with attributes and rules. • Attributes are associated with grammar symbols and rules with productions. • Attributes may be of many kinds: numbers, types, table references, strings, etc. • Synthesized attributes • A synthesized attribute at node N is defined only in terms of attribute values of children of N. • Inherited attributes • An inherited attribute at node N is defined only in terms of attribute values at N’s parent.

  6. 6 Syntax-Directed Definition -- Example ProductionSemantic Rules L → E n L.val = E.val E → E1 + T E.val = E1.val + T.val E → T E.val = T.val T → T1 * F T.val = T1.val * F.val T → F T.val = F.val F → ( E ) F.val = E.val F → digit F.val = digit.lexval 1. Symbols E, T, and F are associated with a synthesized attribute val. 2. The token digit has a synthesized attribute lexval (it is assumed that it is evaluated by the lexical analyzer). 3. Terminals are assumed to have synthesized attributes only. Values for attributes of terminals are usually supplied by the lexical analyzer. 4. The start symbol does not have any inherited attribute unless otherwise stated.

  7. 7 Annotated Parse Tree -- Example L Input: 5+3*4 E.val=17 n E.val=5 + T.val=12 T.val=5 T.val=3 * F.val=4 F.val=5 F.val=3 digit.lexval=4 digit.lexval=5 digit.lexval=3

  8. Evaluation orders for SDD’s • A dependency graph is used to determine the order of computation of attributes. • Dependency graph • For each parse tree node, the parse tree has a node for each attribute associated with that node • If a semantic rule defines the value of synthesized attribute A.b in terms of the value of X.c then the dependency graph has an edge from X.c to A.b • If a semantic rule defines the value of inherited attribute B.c in terms of the value of X.a then the dependency graph has an edge from X.c to B.c

  9. Ordering the evaluation of attributes • If dependency graph has an edge from M to N then M must be evaluated before the attribute of N • Thus the only allowable orders of evaluation are those sequence of nodes N1,N2,…,Nk such that if there is an edge from Ni to Nj then i<j • Such an ordering is called a topological sort of a graph.

  10. Dependency Graph 10 L Input: 5+3*4 E.val=17 n E.val=5 + T.val=12 T.val=5 T.val=3 * F.val=4 F.val=5 F.val=3 digit.lexval=4 digit.lexval=5 digit.lexval=3

  11. S-Attributed definitions • A SDD is S-attributed if every attribute is synthesized • We can have a post-order traversal of parse-tree to evaluate attributes in S-attributed definitions. • S-Attributed definitions can be implemented during bottom-up parsing without the need to explicitly create parse trees.

  12. L-Attributed definitions • A SDD is L-Attributed if the edges in dependency graph goes from Left to Right but not from Right to Left. • More precisely, each attribute must be either • Synthesized. • Inherited, but if there is a production A->X1X2…Xn and there is an inherited attribute Xi.a computed by a rule associated with this production, then the rule may only use: • Inherited attributes associated with the head A • Either inherited or synthesized attributes associated with the occurrences of symbols X1,X2,…,Xi-1 located to the left of Xi • Inherited or synthesized attributes associated with this occurrence of Xi itself, but in such a way that there is no cycle in the graph.

  13. Applications of syntax-directed translation: • The syntax directed translation techniques in this chapter will be applied in next chapter that is intermediate-code generation. Here, we consider selected examples to illustrate some representative SDD’s. • The main application in this section is the construction of syntax trees. • We consider two SDD’s for constructing syntax trees for expressions. • The first, an S-attributed, is suitable for use during bottom-up parsing. • The second, L-attributed, is suitable for use during top-down parsing.

  14. Construction of syntax trees: • Each node in a syntax tree represents a construct. • The children of the node represent the meaningful components of the construct. • A syntax-tree node representing an expression E1+E2 has label + and two children representing the sub-expressions E1 and E2. • We shall implement the nodes of a syntax tree by objects with a suitable number of fields. • Each object will have an op field that is the label of the node.

  15. Construction of syntax trees: Continue…….. • The objects will have additional fields as follows: • If the node is a leaf, an additional field holds the lexical value for the leaf. • If the node is an interior node, there are as many additional fields as the node has children in the syntax tree.

  16. 16 Constructing Syntax Tree for Expressions • Each node can be implemented as a record with several fields. • Operator node: one field identifies the operator (called label of the node) and remaining fields contain pointers to operands. • The nodes may also contain fields to hold the values (pointers to values) of attributes attached to the nodes. • Functions used to create nodes of syntax tree for expressions with binary operator are given below. • mknode(op,left,right) • mkleaf(id,entry) • mkleaf(num,val)

  17. Constructing Syntax Tree for Expressions: Example: a-4+c • p1:=mkleaf(id,entrya); • p2:=mkleaf(num,4); 3. p3:=mknode(-,p1,p2) • p4:=mkleaf(id,entryc); • p5:= mknode(+,p3,p4); • The tree is constructed bottom up. to entry for c to entry for a

  18. A syntax Directed Definition for Constructing Syntax Tree • It uses underlying productions of the grammar to schedule the calls of the functions mkleafand mknode to construct the syntax tree • Employment of the synthesized attribute nptr(pointer) for E and T to keep track of the pointers returned by the function calls. PRODUCTION SEMANTIC RULE E  E1+ T E.nptr = mknode(“+”,E1.nptr ,T.nptr) E  E1- T E.nptr = mknode(“-”,E1.nptr ,T.nptr) E  T E.nptr = T.nptr T  (E)T.nptr = E.nptr T  idT.nptr = mkleaf(id, id.lexval) T  numT.nptr = mkleaf(num, num.val)

  19. Annotated parse tree depicting construction of syntax tree for the expression a-4+c E.nptr E.nptr + T.nptr E.nptr - T.nptr id T.nptr num id Entry for c Entry for a

  20. Syntax directed translation schemes • A SDT is a Context Free grammar with program fragments embedded within production bodies • Those program fragments are called semantic actions. • They can appear at any position within production body. • Any SDT can be implemented by first building a parse tree and then performing the actions in a left-to-right depth first order • Typically SDT’s are implemented during parsing without building a parse tree

  21. Postfix translation schemes • Simplest SDDs are those that we can parse the grammar bottom-up and the SDD is s-attributed • For such cases we can construct SDT where each action is placed at the end of the production and is executed along with the reduction of the body to the head of that production. • SDT’s with all actions at the right ends of the production bodies are called postfix SDT’s

  22. SDT’s with actions inside productions L • Any SDT can be implemented as follows • Ignore the actions and produce a parse tree • Examine each interior node N and add actions as new children at the correct position. • Perform a post-order traversal and execute actions when their nodes are visited. E {print(‘+’);} E + T T F {print(4);} {print(‘*’);} T * F digit {print(5);} F digit {print(3);} digit

  23. The end