22
Syntax Directed Translation

Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC assembly code/mc Cmm subexpression,……

Embed Size (px)

Citation preview

Syntax Directed Translation

Tokens

Parser

Semantic checking

TAC

Peephole, pipeline, ……

TAC assembly code/mc

Cmm subexpression ,……

Syntax Directed Translation

• The parsing process and parse trees are used to direct semantic analysis and the translation of the source program.

• This can be a separate phase of a compiler or we can augment our conventional grammar with information to control the semantic analysis and translation. Such grammars are called attribute grammars (P & Attribute Equation)

The syntax analyzer directs the whole process

1. Calls the lexical analyzer whenever syntax analyzer wants another token.

2. Perform the actions of semantic analyzer. 3. Perform the actions of the intermediate code

generator.

Syntax-Directed Definitions

• Generalization of a CFG 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.

Form of a Syntax Directed Definition

• For each grammar production A α has associated with it a set of semantic rules of the form b:= f(c1, c2,……..,ck), where f is a function, and either

• b is a synthesized attribute of A and c1, c2,……..,ck are attributes belonging to the grammar symbols of the production, or,

• b is an inherited attribute of one of the grammar symbols on the right side of the production, and c1, c2,……..,ck are attributes belonging to the grammar symbols of the production.

Synthesized & Inherited Attributes

• An attribute can represent anything we choose: a string, a number, a type, a memory location, or whatever.

• The value of a synthesized attribute at a node is computed from the values of attributes at the children of that node in the parse tree.

• The value of an inherited attribute is computed from the values of attributes at the siblings and parent of that node.

Synthesized & Inherited Attributes

Types of inherited dependencies1. Inheritance from parent to siblings 2. Inheritance from sibling to sibling (b)

3. Sibling inheritance via sibling pointers (c)

Cont…

• There are two notations for associating semantic rules with productions

1. Syntax directed definitions - CFG in which each grammar symbol has an associated set of attributes, (synthesized inherited attributes)

2. Translation schemes. The scheme associates to each production rule of the

source context free language a production rule of the target context free language

A → α,β where α is called the source element of the rule. β is called the translation element of the rule

Conceptual views of syntax directed definitions

I/P P.TreeE.Order for Semantic

rulesD.Graph

Dependency Graph

• It is a Directed graph representing dependencies of several objects towards each other.

• It is possible to derive an evaluation order or the absence of an evaluation order that respects the given dependencies from the dependency graph.

Definition of a dependency graph

• Given a set of objects S and a Transitive relation R= SxS with modeling a dependency “a needs b evaluated first”, the dependency graph is a graph G=(S,T).

Example A = B+C; B = 5+D; C=4; D=2;

then S = A,B,C,D and R = (A,B),(A,C),(B,D).• You can derive this relation directly: A depends on B and C,

because you can add two variables if and only if you know the values of both variables.

Syntax Directed Semantics (semantics directed syntax)

• Attributes are associated directly with the grammar symbols of the language.(T & N)

• If X is a G & a is an attribute associated to X, then we can write X.a

• Given collection of attributes a1,a2,..ak, the principle of syntax directed semantics implies that for each grammar rule X0X1…Xn(X0 is a Non terminal & Xi are arbitrary symbols) . Each relationship is specified by an attribute equation or semantic rule of the form:

Xi.Aj =fij(X0.a1,...X0.ak,X1.a1,…..X1.ak,….Xn.ak,…..Xn.ak)

Cont…

• An attribute grammar for the attributes a1,….ak is the collection of all such equations, for all the grammar rules of the language. Attribute grammars are written in tabular form in which each grammar rule listed with the set of attribute equations or semantic rules associated to that rule.

Grammar Rule Semantic rules

Rule 1 Associated attribute equations

, ,

, ,

Rule n Associated attribute equations

ExampleNumber number digit|digit Digit 0|1|2|3|4|5|6|7|8|9 G• (ex) digit 0 implies that digit has a value 0 , this can be expressed by an

attribute equation digit.val=0 and we associate this equation with the rule digit 0.

• (ex) number digit (A.EQN) number.val = digit.val (for a single digit)• number number digit• number1number2 digit (distinguishing the numbers) • Consider the number 34, LMD of this number is: • Number number digit digit digit 3 digit 34• We can also multiply the value of number2 by 10 and add the value of the digit

(34=3*10+4)• Then the attribute equation will be :Number1.val=number2.val *number3.val

+number4.val

cont…Grammar Attribute Equations

Number1number2 digit (34)

Number1.val=number2.val *number3.val +number4.val

Numberdigit Number.val=digit.val

Digit0 Digit.val=0

Digit1 Digit.val=1

Digit2 Digit.val=2

Digit3 Digit.val=3

Digit4 Digit.val=4

Digit5 digit.val=5

Digit6 Digit.val=6

Digit7 Digit.val=7

Digit8 Digit.val=8

Digit9 Digit.val=9

Synthesized Attributes

• A syntax directed definition that uses synthesized attributes exclusively is said to be an S-attributed definition.

• A parse tree for an S-attributed definition can always be annotated by evaluating the semantic rules for the attributes at each node bottom up, from the leaves to the root.

L-Attribute

Annotate Parse Tree