Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture # 17 Syntax Directed Definition. 2 Translation Schemes A translation scheme is a CF grammar embedded with semantic actions rest  + term { print(“+”)

Similar presentations


Presentation on theme: "Lecture # 17 Syntax Directed Definition. 2 Translation Schemes A translation scheme is a CF grammar embedded with semantic actions rest  + term { print(“+”)"— Presentation transcript:

1 Lecture # 17 Syntax Directed Definition

2 2 Translation Schemes A translation scheme is a CF grammar embedded with semantic actions rest  + term { print(“+”) } rest Embedded semantic action rest termrest+{ print(“+”) }

3 3 Example Translation Scheme expr  expr + term expr  expr - term expr  term term  0 term  1 … term  9 { print(“+”) } { print(“-”) } { print(“0”) } { print(“1”) } … { print(“9”) }

4 4 Example Translation Scheme (cont’d) expr term 9 - 5 + 2 expr term { print(“-”) } { print(“+”) } { print(“9”) } { print(“5”) } { print(“2”) } Translates 9-5+2 into postfix 95-2+

5 5 Attributes Attribute values may represent – Numbers (literal constants) – Strings (literal constants) – Memory locations, such as a frame index of a local variable or function argument – A data type for type checking of expressions – Scoping information for local declarations – Intermediate program representations

6 6 Synthesized Versus Inherited Attributes Given a production A   then each semantic rule is of the form b := f(c 1,c 2,…,c k ) where f is a function and c i are attributes of A and , and either – b is a synthesized attribute of A – b is an inherited attribute of one of the grammar symbols in 

7 7 Synthesized Versus Inherited Attributes (cont’d) D  T L T  int … L  id L.in := T.type T.type := ‘integer’ … … := L.in ProductionSemantic Rule inherited synthesized

8 8 S-Attributed Definitions A syntax-directed definition that uses synthesized attributes exclusively is called an S-attributed definition (or S-attributed grammar) A parse tree of an S-attributed definition is annotated with a single bottom-up traversal

9 9 Example Attribute Grammar with Synthesized+Inherited Attributes D  T L T  int T  real L  L 1, id L  id L.in := T.type T.type := ‘integer’ T.type := ‘real’ L 1.in := L.in; addtype(id.entry, L.in) addtype(id.entry, L.in) ProductionSemantic Rule Synthesized:T.type, id.entry Inherited:L.in

10 Example Write Syntax Directed Definitions to convert a binary string to decimal value Solution: – First we would think of synthesized and inherited attributes required. We identified three variables namely “var” for holding binary vale, “decval” for holding decimal value and “pos” for the place value

11 Example (contd) Productions S  A S A  0 A  1 S  € Semantic Rules S.pos = 0 S.pos=S.pos+1 S.decval= A.decval+S.decval A.val=0 A.pos=S.pos A.decval= A.val *2 pos A.val=1 A.pos=S.pos A.decval= A.val *2 pos S.pos =S.pos


Download ppt "Lecture # 17 Syntax Directed Definition. 2 Translation Schemes A translation scheme is a CF grammar embedded with semantic actions rest  + term { print(“+”)"

Similar presentations


Ads by Google