Lecture 17
This presentation is the property of its rightful owner.
Sponsored Links
1 / 11

Lecture # 17 PowerPoint PPT Presentation


  • 52 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

Lecture # 17

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Lecture 17

Lecture # 17

Syntax Directed Definition


Translation schemes

Translation Schemes

  • A translation scheme is a CF grammar embedded with semantic actions

rest +term { print(“+”) } rest

Embeddedsemantic action

rest

+

term

{ print(“+”) }

rest


Example translation scheme

Example Translation Scheme

expr expr+termexpr  expr-termexpr  termterm  0term  1…term  9

{ print(“+”) }{ print(“-”) }{ print(“0”) }{ print(“1”) }…{ print(“9”) }


Example translation scheme cont d

Example Translation Scheme (cont’d)

expr

{ print(“+”) }

+

expr

term

{ print(“2”) }

{ print(“-”) }

-

2

expr

term

{ print(“5”) }

5

term

{ print(“9”) }

9

Translates 9-5+2 into postfix 95-2+


Attributes

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


Synthesized versus inherited attributes

Synthesized Versus Inherited Attributes

  • Given a productionAthen each semantic rule is of the formb := f(c1,c2,…,ck)where f is a function and ci 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 


Synthesized versus inherited attributes cont d

Synthesized Versus Inherited Attributes (cont’d)

inherited

Production

Semantic Rule

D T LT  int…L  id

L.in:= T.typeT.type:=‘integer’…… := L.in

synthesized


S attributed definitions

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


Example attribute grammar with synthesized inherited attributes

Example Attribute Grammar with Synthesized+Inherited Attributes

Production

Semantic Rule

L.in:= T.typeT.type:= ‘integer’T.type:= ‘real’ L1.in:= L.in; addtype(id.entry, L.in)addtype(id.entry, L.in)

D T LT  intT realL  L1,idL id

Synthesized:T.type, id.entryInherited:L.in


Example

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


Example contd

Example (contd)

Productions

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 *2pos

A.val=1 A.pos=S.pos

A.decval= A.val *2pos

S.pos =S.pos

  • S A S

  • A 0

  • A 1

  • S €


  • Login