Semantic Analysis
Semantic Analysis
• Semantic analysis is a pass by a compiler that adds semantic
information to the parse tree and performs certain checks based on
this information. It logically follows the parsing phase, in which the
parse tree is generated, and logically precedes the code generation
phase, in which executable code is generated.
Semantic analyzer
• Semantic Errors
• Variable used without declaration
• Declaration of same variables in a scope
• Scope violation
• Mismatch of actual and formal parameters
• Type mismatch
• Array out of bound
Attaching semantic rules
• There are two notations for attaching semantic rules:
1. Syntax Directed Definitions. High-level specification hiding many
implementation details (also called Attribute Grammars).
2. Translation Schemes. More implementation oriented: Indicate the order in
which semantic rules are to be evaluated.
Attribute Grammar
• Attribute grammar is a medium to provide semantics to the context-
free grammar and it can help specify the syntax and semantics of a
programming language.
Syntax Directed Translations (cont.)
• S-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.
• E->E+T
• L-attributed SDT
• This form of SDT uses both synthesized and inherited attributes with restriction of not taking
values from right siblings. 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.
Synthesized Attribute
Inherited attribute example
Comments

Semantic Analysis.pptx

  • 1.
  • 2.
    Semantic Analysis • Semanticanalysis is a pass by a compiler that adds semantic information to the parse tree and performs certain checks based on this information. It logically follows the parsing phase, in which the parse tree is generated, and logically precedes the code generation phase, in which executable code is generated.
  • 3.
    Semantic analyzer • SemanticErrors • Variable used without declaration • Declaration of same variables in a scope • Scope violation • Mismatch of actual and formal parameters • Type mismatch • Array out of bound
  • 4.
    Attaching semantic rules •There are two notations for attaching semantic rules: 1. Syntax Directed Definitions. High-level specification hiding many implementation details (also called Attribute Grammars). 2. Translation Schemes. More implementation oriented: Indicate the order in which semantic rules are to be evaluated.
  • 5.
    Attribute Grammar • Attributegrammar is a medium to provide semantics to the context- free grammar and it can help specify the syntax and semantics of a programming language.
  • 6.
    Syntax Directed Translations(cont.) • S-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. • E->E+T • L-attributed SDT • This form of SDT uses both synthesized and inherited attributes with restriction of not taking values from right siblings. 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.
  • 7.
  • 8.
  • 14.