Documente Academic
Documente Profesional
Documente Cultură
Objectives to be able to define syntax directed translation and explain the mechanism on how to specify and implement it. to show how syntax-directed definitions can be used to specify the construction of syntax trees and other graphical representations of language constructs. to discuss how to implement a translator using bottom up evaluation. to elaborate how a recursive evaluation works.
a method of translating a string into a sequence of actions by attaining one such action to each rule of a grammar. It uses a grammar to direct the translation which defines the syntax of the input language.
Input String Parse Tree Dependency Graph Evaluation order for semantic rules
01-Feb-2012
Synthesized Attributes
Furthermore, syntax directed translation refers to two basic techniques: Syntax-directed definitions (SDDs) Syntax-directed translation schemes (SDTs)
A syntax directed definition that uses synthesized attributes exclusively is said to be an Sattributed definition. A parse tree for an Sattributed definition can always be annotated by evaluating the semantic rules for the attributes at each node bottom up, from the leaves to the root.
Example:
The figure below contains an annotated parse tree for the input 3*5+4n. The output printed at the root of the tree, is the value of E.val at the first child of the root.
a generalization of a context free grammar in which each grammar symbol has an associated set of attributes, partitioned into two subsets called the synthesized and inherited attributes of that grammar symbol.
01-Feb-2012
Inherited Attributes
An inherited attribute is one whose value at a node in a parse tree is defined in terms of attributes at the parent and/or siblings of that node.
Example
A declaration generated by the nonterminal D in the syntax directed definition in figure below, consists of the keyword int or real, followed by a list of identifiers.
Dependency Graph
The interdependencies among the inherited and synthesized attributes at the nodes in a parse tree can be depicted by a directed graph called a dependency graph.
01-Feb-2012
Dependency Graphs
Example: whenever the ff. production is used in a parse tree, we add the edges to the dependency graph. PRODUCTION EE1+E2 E1.val+E2.val SEMANTIC RULE E.val :=
Construction
01-Feb-2012
15
Evaluation Order
Syntax Tree
A topological sort of a directed acyclic graph is any ordering m1, m2,.mk of the node of the graph such that edges go from nodes earlier in the ordering to later nodes.
condensed form of parse tree useful for representing language constructs syntax-directed translation can be based on syntax trees as well as on parse trees
01-Feb-2012
16
01-Feb-2012
similar to the translation of the expression(infix) into postfix form construct sub-trees for the subexpressions by creating a node for each operator and operand children of an operator node are the roots of the nodes representing the sub-expresions constituting the operands of that operator
01-Feb-2012
there are a number of functions defined to create the nodes of syntax trees
returns a pointer to a newly created node
17
19
Example: a - 4 + c
Postfix: a4-c+ each node in a syntax tree can be implemented as a record with several fields
one field identifies the operator and the remaining fields contain pointers to the nodes for the operands p1 p2 p3 p4 p5 := := := := := mkleaf(id , entrya) mkleaf(num , 4) mknode(' - ', p1 , p2) mkleaf(id , entryc) mknode('+' , p3 , p4)
01-Feb-2012
18
01-Feb-2012
01-Feb-2012
Example: a * b + f * (a * b)
Postfix: ab*fab**+
p1 p2 p3 p4 p5 p6 p7 p8 p9 := := := := := := := := := mkleaf(id , entrya) mkleaf(id , entryb) mknode('*, p1 , p2) mkleaf(id , entryf) mkleaf(id , entrya) mkleaf(id , entryb) mknode('*, p5 , p6) mknode(*' , p4 , p7) mknode('+' , p3 , p8)
uses the underlying productions of the grammar to schedule the calls of the functions mknode() and mkleaf() to construct the tree
01-Feb-2012
21
01-Feb-2012
23
Example: a * b + f * (a * b)
p1 p2 p3 p4 p5 := := := := := mkleaf(id , entrya) mkleaf(id , entryb) mknode('*, p1 , p2) mkleaf(id , entryf) mkleaf(id , entrya) p6 p7 p8 p9 := := := := mkleaf(id , entryb) mknode('*, p5 , p6) mknode(*' , p4 , p7) mknode('+' , p3 , p8)
synthesized attribute nptr for E and T keeps track of the pointers returned by the function calls
01-Feb-2012
01-Feb-2012
24
01-Feb-2012
Example: a * b + f * (a * b)
Postfix: ab*fab**+ directed acyclic graph (dag) for an expression identifies the common sub-expressions in the expression difference is that a node in a dag representing a common subexpression has more than one "parent
p1 p2 p3 p4 p5 p6 p7 p8 p9 := := := := := := := := := mkleaf(id , entrya) Representation mkleaf(id , entryb) 1 id entrya mknode('*, p1 , p2) 2 id entryb mkleaf(id , entryf) mkleaf(id , entrya) 3 * 1 2 mkleaf(id , entryb) 4 id entryf mknode('*, p5 , p6) * 3 4 mknode(*' , p4 , p7) 5 + 3 5 mknode('+' , p3 , p8) 6
01-Feb-2012
25
01-Feb-2012
27
1 2 3 4 5 6
id id * id * +
entrya 1 3 3 2
entryb entryf 4 5
+ * a b * f
26
01-Feb-2012
28
01-Feb-2012
01-Feb-2012
01-Feb-2012