Semantic Analysis: Syntax Directed Translation CSE 420 Lecture 11
Inherited and Synthesized Attributes • A synthesized attribute for a nonterminal A at a parse- tree node N is defined by ▫ a semantic rule associated with the production at N. Note that the production must have A as its head. • A synthesized attribute at node N is defined only in terms of attribute values at the children of N and at N itself. • An inherited attribute for a nonterminal B at a parse- tree node N is defined by ▫ A semantic rule associated with the production at the parent of N. Note that the production must have B as a symbol in its body. • An inherited attribute at node N is defined only in terms of attribute values at N's parent, N itself, and N's siblings.
Inherited and Synthesized Attributes • Terminals can have synthesized attributes, which are given to it by the lexer (not the parser). • There are no rules in an SDD giving values to attributes for terminals. • Terminals do not have inherited attributes. • A nonterminal can have both inherited and synthesized attributes.
Evaluating an SDD at the Nodes of a Parse Tree • Parse tree helps us to visualize the translation specified by SDD. • The rules of an SDD are applied by first constructing a parse tree ▫ then using the rules to evaluate all of the attributes at each of the nodes of the parse tree. • A parse tree, showing the value(s) of its attribute(s) is called an annotated parse tree.
Evaluating an SDD at the Nodes of a Parse Tree Annotated parse tree: 3*5 + 4 n With synthesized attributes, we can evaluate attributes in any bottom-up order, such as that of a postorder traversal of the parse tree. val and lexval are synthesized attributes
• Inherited attributes are useful when the structure of a parse tree does not match the abstract syntax of the source code. • They can be used to overcome the mismatch due to grammar designed for parsing rather than translation. Evaluating an SDD at the Nodes of a Parse Tree
Evaluating an SDD at the Nodes of a Parse Tree An SDD with both inherited and synthesized attributes does not ensure any guaranteed order; even it may not have an order at all. Annotated parse tree: 3*5
Evaluating an SDD at the Nodes of a Parse Tree
Evaluation Orders for SDD's • "Dependency graphs" are a useful tool for determining an evaluation order for the attribute instances in a given parse tree. • While an annotated parse tree shows the values of attributes ▫ A dependency graph helps us determine how those values can be computed.
Evaluation Orders for SDD's • Each attribute is associated to a node. • If a semantic rule associated with a production p defines the value of synthesized attribute A.b in terms of the value of X.c, then graph has an edge from X.c to A.b • If a semantic rule associated with a production p defines the value of inherited attribute B.c in terms of value of X.a, then graph has an edge from X.a to B.c
Evaluation Orders for SDD's • At every node N labeled E with children correspond to the body of production, ▫ The synthesized attribute val at N is computed using the values of val at the two childr.en, labeled E and T
Evaluation Orders for SDD's Dependency graph for the annotated parse tree for 3*5
S-Attributed Definitions • An SDD is S-attributed if every attribute is synthesized. • Attributes of an S-attributed SDD can be evaluated in bottom-up order of the nodes of parse tree. • Evaluation is simple using post-order traversal. postorder(N) { for (each child C of N, from the left) postorder(C); evaluate attributes associated with node N; } • S-attributed definitions can be implemented during bottom- up parsing as ▫ bottom-up parse corresponds to a postorder traversal ▫ postorder corresponds to the order in which an LR parser reduces a production body to its head
L-Attributed Definitions • Each attribute must be either ▫ Synthesized, or ▫ Inherited, but with the rules limited as follows. Suppose that there is a production A X1 X2 • • • Xn, there is an inherited attribute Xi.a computed by a rule associated with this production. Then the rule may use only: • 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 only in such a way that there are no cycles in a dependency graph formed by the attributes of this Xi.
L-Attributed Definitions-Example
SDD For Simple Type Declarations • The purpose of L.inh is to pass the declared type down the list of identifiers, so that it can be the appropriate symbol-table entries. • Productions 2 and 3 each evaluate the synthesized attribute T.type, giving it the appropriate value, integer or float. • Productions 4 and 5 also have a rule in which a function addType is called with two arguments: 1. id.entry, a lexical value that points to a symbol-table object, and 2. L.inh, the type being assigned to every identifier on the list. • The function addType properly installs the type L.inh as the type of the represented identifier.
Dependency Graph For Simple Type Declarations A dependency graph for the input string float id1 , id 2, id3
Construction of Syntax Trees • SDDs are useful for is construction of syntax trees. • A syntax tree is a condensed form of parse tree. • Syntax trees are useful for representing programming language constructs like expressions and statements. • They help compiler design by decoupling parsing from translation. • Each node of a syntax tree represents a construct; ▫ The children of the node represent the meaningful components of the construct.
Construction of Syntax Trees e.g. a syntax-tree node representing an expression E1 + E2 has label + and two children representing the sub expressions E1 and E2 • Each node is implemented by objects with suitable number of fields; each object will have an op field that is the label of the node with additional fields as follows: ▫ If the node is a leaf, an additional field holds the lexical value for the leaf . This is created by function Leaf(op, val) ▫ If the node is an interior node, there are as many fields as the node has children in the syntax tree. This is created by function Node(op, c1, c2,...,ck) .
Construction of Syntax Trees
Constructing Syntax Trees during Top- Down Parsing • With a grammar designed for top-down parsing, ▫ the same syntax trees are constructed, ▫ using the same sequence of steps, ▫ even though the structure of the parse trees differs significantly from that of syntax trees.
Constructing Syntax Trees during Top- Down Parsing
Constructing Syntax Trees during Top- Down Parsing
The structure of a TYPE In C, the type int [2][3] can be read as, "array of 2 arrays of 3 integers." The corresponding type expression array(2, array(3, integer)) is represented by the tree as shown below.
The structure of a TYPE An annotated parse tree for the input string int [2][3] is shown below.
SDTs with Actions inside Productions • Action can be placed at any position in the production body. • Action is performed immediately after all symbols left to it are processed. • Given B —> X { a } Y , an action a is done after ▫ we have recognized X (if X is a terminal), or ▫ all terminals derived from X (if X is a nonterminal). • If bottom-up parser is used, then action a is performed as soon as X appears on top of the stack. • If top-down parser is used, then action a is performed ▫ just before Y is expanded (if Y is nonterminal), or ▫ check Y on input (if Y is a terminal).
SDTs with Actions inside Productions • Any SDT can be implemented as follows: ▫ Ignoring actions, parse input and produce parse tree. ▫ Add additional children to node N for action in α, where A-> α . ▫ Perform preorder traversal of the tree, and as soon as a node labeled by an action is visited, perform that action.
SDTs with Actions inside Productions
Any Question?

Lecture 11 semantic analysis 2

  • 1.
    Semantic Analysis: Syntax DirectedTranslation CSE 420 Lecture 11
  • 2.
    Inherited and SynthesizedAttributes • A synthesized attribute for a nonterminal A at a parse- tree node N is defined by ▫ a semantic rule associated with the production at N. Note that the production must have A as its head. • A synthesized attribute at node N is defined only in terms of attribute values at the children of N and at N itself. • An inherited attribute for a nonterminal B at a parse- tree node N is defined by ▫ A semantic rule associated with the production at the parent of N. Note that the production must have B as a symbol in its body. • An inherited attribute at node N is defined only in terms of attribute values at N's parent, N itself, and N's siblings.
  • 3.
    Inherited and SynthesizedAttributes • Terminals can have synthesized attributes, which are given to it by the lexer (not the parser). • There are no rules in an SDD giving values to attributes for terminals. • Terminals do not have inherited attributes. • A nonterminal can have both inherited and synthesized attributes.
  • 4.
    Evaluating an SDDat the Nodes of a Parse Tree • Parse tree helps us to visualize the translation specified by SDD. • The rules of an SDD are applied by first constructing a parse tree ▫ then using the rules to evaluate all of the attributes at each of the nodes of the parse tree. • A parse tree, showing the value(s) of its attribute(s) is called an annotated parse tree.
  • 5.
    Evaluating an SDDat the Nodes of a Parse Tree Annotated parse tree: 3*5 + 4 n With synthesized attributes, we can evaluate attributes in any bottom-up order, such as that of a postorder traversal of the parse tree. val and lexval are synthesized attributes
  • 6.
    • Inherited attributesare useful when the structure of a parse tree does not match the abstract syntax of the source code. • They can be used to overcome the mismatch due to grammar designed for parsing rather than translation. Evaluating an SDD at the Nodes of a Parse Tree
  • 7.
    Evaluating an SDDat the Nodes of a Parse Tree An SDD with both inherited and synthesized attributes does not ensure any guaranteed order; even it may not have an order at all. Annotated parse tree: 3*5
  • 8.
    Evaluating an SDDat the Nodes of a Parse Tree
  • 9.
    Evaluation Orders forSDD's • "Dependency graphs" are a useful tool for determining an evaluation order for the attribute instances in a given parse tree. • While an annotated parse tree shows the values of attributes ▫ A dependency graph helps us determine how those values can be computed.
  • 10.
    Evaluation Orders forSDD's • Each attribute is associated to a node. • If a semantic rule associated with a production p defines the value of synthesized attribute A.b in terms of the value of X.c, then graph has an edge from X.c to A.b • If a semantic rule associated with a production p defines the value of inherited attribute B.c in terms of value of X.a, then graph has an edge from X.a to B.c
  • 11.
    Evaluation Orders forSDD's • At every node N labeled E with children correspond to the body of production, ▫ The synthesized attribute val at N is computed using the values of val at the two childr.en, labeled E and T
  • 12.
    Evaluation Orders forSDD's Dependency graph for the annotated parse tree for 3*5
  • 13.
    S-Attributed Definitions • AnSDD is S-attributed if every attribute is synthesized. • Attributes of an S-attributed SDD can be evaluated in bottom-up order of the nodes of parse tree. • Evaluation is simple using post-order traversal. postorder(N) { for (each child C of N, from the left) postorder(C); evaluate attributes associated with node N; } • S-attributed definitions can be implemented during bottom- up parsing as ▫ bottom-up parse corresponds to a postorder traversal ▫ postorder corresponds to the order in which an LR parser reduces a production body to its head
  • 14.
    L-Attributed Definitions • Eachattribute must be either ▫ Synthesized, or ▫ Inherited, but with the rules limited as follows. Suppose that there is a production A X1 X2 • • • Xn, there is an inherited attribute Xi.a computed by a rule associated with this production. Then the rule may use only: • 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 only in such a way that there are no cycles in a dependency graph formed by the attributes of this Xi.
  • 15.
  • 16.
    SDD For SimpleType Declarations • The purpose of L.inh is to pass the declared type down the list of identifiers, so that it can be the appropriate symbol-table entries. • Productions 2 and 3 each evaluate the synthesized attribute T.type, giving it the appropriate value, integer or float. • Productions 4 and 5 also have a rule in which a function addType is called with two arguments: 1. id.entry, a lexical value that points to a symbol-table object, and 2. L.inh, the type being assigned to every identifier on the list. • The function addType properly installs the type L.inh as the type of the represented identifier.
  • 17.
    Dependency Graph ForSimple Type Declarations A dependency graph for the input string float id1 , id 2, id3
  • 18.
    Construction of SyntaxTrees • SDDs are useful for is construction of syntax trees. • A syntax tree is a condensed form of parse tree. • Syntax trees are useful for representing programming language constructs like expressions and statements. • They help compiler design by decoupling parsing from translation. • Each node of a syntax tree represents a construct; ▫ The children of the node represent the meaningful components of the construct.
  • 19.
    Construction of SyntaxTrees e.g. a syntax-tree node representing an expression E1 + E2 has label + and two children representing the sub expressions E1 and E2 • Each node is implemented by objects with suitable number of fields; each object will have an op field that is the label of the node with additional fields as follows: ▫ If the node is a leaf, an additional field holds the lexical value for the leaf . This is created by function Leaf(op, val) ▫ If the node is an interior node, there are as many fields as the node has children in the syntax tree. This is created by function Node(op, c1, c2,...,ck) .
  • 20.
  • 21.
    Constructing Syntax Treesduring Top- Down Parsing • With a grammar designed for top-down parsing, ▫ the same syntax trees are constructed, ▫ using the same sequence of steps, ▫ even though the structure of the parse trees differs significantly from that of syntax trees.
  • 22.
    Constructing Syntax Treesduring Top- Down Parsing
  • 23.
    Constructing Syntax Treesduring Top- Down Parsing
  • 24.
    The structure ofa TYPE In C, the type int [2][3] can be read as, "array of 2 arrays of 3 integers." The corresponding type expression array(2, array(3, integer)) is represented by the tree as shown below.
  • 25.
    The structure ofa TYPE An annotated parse tree for the input string int [2][3] is shown below.
  • 26.
    SDTs with Actionsinside Productions • Action can be placed at any position in the production body. • Action is performed immediately after all symbols left to it are processed. • Given B —> X { a } Y , an action a is done after ▫ we have recognized X (if X is a terminal), or ▫ all terminals derived from X (if X is a nonterminal). • If bottom-up parser is used, then action a is performed as soon as X appears on top of the stack. • If top-down parser is used, then action a is performed ▫ just before Y is expanded (if Y is nonterminal), or ▫ check Y on input (if Y is a terminal).
  • 27.
    SDTs with Actionsinside Productions • Any SDT can be implemented as follows: ▫ Ignoring actions, parse input and produce parse tree. ▫ Add additional children to node N for action in α, where A-> α . ▫ Perform preorder traversal of the tree, and as soon as a node labeled by an action is visited, perform that action.
  • 28.
    SDTs with Actionsinside Productions
  • 29.