CHAP 4 SEMENTIC ANALYZER Semantic Analysis Semantic analysis

  • Slides: 12
Download presentation
CHAP 4 SEMENTIC ANALYZER

CHAP 4 SEMENTIC ANALYZER

Semantic Analysis • Semantic analysis is a process in compiler construction usually after parsing

Semantic Analysis • Semantic analysis is a process in compiler construction usually after parsing to gather necessary semantic information from the source code. it usually includes type checking or makes sure a variable is declared before use which is impossible to be described called semantic analysis.

Role of semantic analyzer • Compilers use semantic analysis to enforce the static semantic

Role of semantic analyzer • Compilers use semantic analysis to enforce the static semantic rules of language • The text focuses on an organization where the parser creates a syntax tree (and no full parse tree), and semantic analysis is done over a separate traversal of the syntax tree.

Continue • For instance, a completely separated compiler could have a well-defined lexical analysis

Continue • For instance, a completely separated compiler could have a well-defined lexical analysis and parsing stage generating a parse tree, which is passed wholesale to a semantic analyzer, which could then create a syntax tree and populate a symbol table, and then pass it all on to a code generator. • Or a completely interleaved compiler could intermix all of these stages, literally generating final code as part of the parsing engine.

Static Analysis • In general, compile-time algorithms that predict run-time behavior are known as

Static Analysis • In general, compile-time algorithms that predict run-time behavior are known as static analysis. “ • In Ada, ML, and Haskell, type checking is static. (An amusing, related paper: Dynamic Typing in Haskell).

Dynamic analysis • Many compilers that generate code for dynamic checks provide the option

Dynamic analysis • Many compilers that generate code for dynamic checks provide the option of disabling them if desired. It is customary in some organizations to enable dynamic checks during program development and testing, and then disable them for production use, to increase execution speed. The wisdom of this practice is questionable

Assertions • Assertions, invariants, preconditions, and post-conditions: let the programmer express logical requirements for

Assertions • Assertions, invariants, preconditions, and post-conditions: let the programmer express logical requirements for values. (If you continue along this route, you see run into the various ideas about formalizing semantics, such as operational semantics, axiomatic semantics, and denotational semantics. )

Attribute grammars • An attribute grammar is a formal way to define attribute for

Attribute grammars • An attribute grammar is a formal way to define attribute for the productions of a formal grammar, associating these attributes with values. The evaluation occurs in the nodes of the abstract syntax tree, when the language is processed by some parser or compiler. • The attributes are divided into two groups: synthesized attributes and inherited attributes.

Synthesized attributes • A synthesized attribute is computed from the values of attributes of

Synthesized attributes • A synthesized attribute is computed from the values of attributes of the children. Since the values of the children must be computed first, To formally define a synthesized attribute • G=(Vn , Vt , P, S) be a formal grammer where • Vn is set of non terminal symbols • Vt is a set of terminal symbols • P is a set of productions • S is the start symbol

Inherited attributes • An inherited attribute at a node in parse tree is defined

Inherited attributes • An inherited attribute at a node in parse tree is defined using the attribute values at the parent • S → ABC

Special types of attribute grammars • L-attributed grammar inherited attributes can be evaluated in

Special types of attribute grammars • L-attributed grammar inherited attributes can be evaluated in one left-to-right traversal of the abstract syntax tree LR-attributed grammar an L-attributed grammar whose inherited attributes can also be evaluated in bottom-up parsing ECLR-attributed grammar a subset of LR-attributed grammars where equivalence classes can be used to optimize the evaluation of inherited attributes.

continue • S-attributed grammar a simple type of attribute grammar, using only synthesized attributes,

continue • S-attributed grammar a simple type of attribute grammar, using only synthesized attributes, but no inherited attributes