Docsity

Syntax Directed Definition, Schemes and Mind Maps of Compilers

Partial preview of the text

Download Syntax Directed Definition and more Schemes and Mind Maps Compilers in PDF only on Docsity!

SEMANTIC ANALYSIS

Recap

Lexical Analyzer

Token, R.E, FA etc

Syntax Analyzer

CFG , Parse Tree etc

Semantic Analyzer

Actual Meaning even if grammar is true

int x= 3.5 ;

Intermediate code generation

Semantic Error !!!!

We have mentioned some of the semantics errors that the semantic analyzer is expected to recognize:

  1. Type mismatch
  2. Undeclared variable
  3. Reserved identifier misuse.
  4. Multiple declaration of variable in a scope.
  5. Accessing an out of scope variable.
  6. Actual and formal parameter mismatch.

SDD

Semantic attributes

Semantic attributes may be assigned to their values

from their domain at the time of parsing and evaluated

at the time of assignment or conditions.

Based on the way the attributes get their values, they

can be broadly divided into two categories :

1. synthesized attributes

2. inherited attributes.

Inherited attributes

In contrast to synthesized attributes, inherited attributes

can take values from parent and/or siblings. As in the

following production,

S โ†’ ABC{ C.X = S.x+A.x+B.x}

Expansion Vs Reduction

Expansion : When a non-terminal is expanded to

terminals as per a grammatical rule

S-attributed SDT(Syntax directed Translation) ๏‚ž SDT = Output using rules in SDD by making Tree ๏‚ž Two Types

  1. S-attributed SDT
  2. L-attributed SDT ๏‚ž If an SDT uses only synthesized attributes, it is called as S-attributed SDT ๏‚ž Attributes in S-attributed SDTs are evaluated in bottom- up parsing, as the values of the parent nodes depend upon the values of the child nodes.

L-attributed SDT

L-attributed SDT uses both synthesized and inherited attributes with restriction of not taking values from right siblings. Non-terminal can get values from its parent, child, and sibling nodes.

Example : S โ†’ ABC

S can take values from A, B, and C (synthesized). A can take values from S only. B can take values from S and A. C can get values from S, A, and B. No non-terminal can get values from the sibling to its right. Attributes in L-attributed SDTs are evaluated by depth-first and left-to-right parsing manner.

Find S-attributed SDT for below SDD using string 2+3*

Find S-attributed SDT for below SDD using string 2+3*

Find S-attributed SDT for below SDD using string 2+3*

Find S-attributed SDT for below SDD using string 35+*