Sie sind auf Seite 1von 8

01-Feb-2012

CMSC 129 Report Cacha Montero Palencia Simplicio


01-Feb-2012

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.

Syntax Directed Translation

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

Conceptual view of a syntax directed translation.

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.

Syntax Directed Definition

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.

Parse tree with inherited attribute in at each node labeled L.

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

Construction of Syntax Trees for Expressions

Construction of Syntax Trees for Expressions

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

In this section we use the following functions:


i. mknode(op, left, right) ii. mkleaf(id, entry) iii. mkleaf(id, val)
01-Feb-2012

17

19

Construction of Syntax Trees for Expressions

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)

Syntax-Directed Definition for Constructing Syntax Trees

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)

Syntax-Directed Definition for Constructing Syntax Trees

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

Directed Acyclic Graphs for Expressions

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

Directed Acyclic Graphs for Expressions

Example: a * b + f * (a * b) Representation DAG:

Algorithm: Value-number method for constructing a node in a dag.


Input: Label op, node l, and node r. Output: A node with signature <op,l,r>. Method: Search the array for a node m with label op, left child l, and right child r. If there is such a node, return m; otherwise, create a new node n with label op, left child l, right child r, and return n.

1 2 3 4 5 6

id id * id * +

entrya 1 3 3 2

entryb entryf 4 5

+ * a b * f

mknode() and mkleaf() create new nodes only when necessary


01-Feb-2012

26

01-Feb-2012

28

01-Feb-2012

01-Feb-2012

Activity: Syntax Trees

Given: a + a * (b c) + (b c) * d a. Postfix b. Function calls c. DAG and its representation(table)

01-Feb-2012

Das könnte Ihnen auch gefallen