UNIT-III Intermediate Code Generation: Syntax Directed Definitions, Evaluation Orders for Syntax Directed Definitions-Intermediate Languages: Syntax Tree, Three Address Code, Types and Declarations-Translation of Expressions, Type Checking
Syntax-Directed Definition Inherited and Synthesized Attributes Evaluating an SDD at the nodes of a parse tree
Semantic Analysis The third phase of the compiler called semantic analysis. The main goal of the semantic analysis is to check the correctness of program and enable proper execution The job of the parser is only to verify that the input program consists of tokens arranged on syntactically valid combination In semantic analysis we check whether they form a sensible set of instructions in the programming language
Semantic Analysis Definition: Semantic Analysis is the third phase of the compiler which acts as an interface between syntax analysis phase and code generation phase It accepts the parse tree from the syntax analysis phase and adds the semantic information to the parse tree and performs certain checks based on this information It also helps constructing the symbol table with appropriate information
Semantic Analysis The semantics of a language can be described very easily using two notations namely: Syntax Directed Definition (SDD) Syntax Directed Translation (SDT) Note: Consider the production E E+T . To distinguish E on LHS of the production and E on RHS of the production, we use E1 on RHS of the production as shown below: E E1 + T
Syntax Directed Definition (SDD) Definition: A Syntax Directed Definition (SDD) is a context free grammar with attributes and semantic rules . The attributes are associated with grammar symbols whereas the semantic rules are associated with productions. The semantic rules are used to compute the attribute values.
Example A Simple Syntax Directed Definition ( SDD) for the production E E1 + T can be written as shown below:
Types of attributes There are two types of attributes: Synthesized Attributes Inherited Attributes Synthesized Attributes – These are those attributes which derive their values from their children nodes i.e. value of synthesized attribute at node is computed from the values of attributes at children nodes in parse tree.
Example: Let us assume an input string 4 * 5 + 6 for computing synthesized attributes. The annotated parse tree for the input string is
Contd.. 2. Inherited Attributes – These are the attributes which derive their values from their parent or sibling nodes i.e. value of inherited attributes are computed by value of parent or sibling nodes.
Example: Let us assume an input string int a, c for computing inherited attributes. The annotated parse tree for the input string is