Week 2 Lec 4 CC
Week 2 Lec 4 CC
Understanding related
Compiler phases in detail
The character sequence forming a token is called the lexemes for the
token.
Lexical analyzer represents lexemes in the form of tokens as
<token_name, attribute_value>
More appropriate way
<Token_type, Word>
void main( ) {
int x;
x = 3;
}
The tree has an interior node labeled * with ( id, 3 ) as its left child and
the integer 10 as its right child. The node (id, 3) represents the
identifier rate. The node labeled * makes it explicit that we must first
multiply the value of rate by 10. The node labeled + indicates that we
must add the result of this multiplication to the value of count. The root
of the tree, labeled =, indicates that we must store the result of this
addition into the location for the identifier total.
For building such type of syntax tree the production rules are to be
designed.
The rules are usually expressed by context free grammar.
This phase checks the source program for semantic errors and gathers type information
for the subsequent code generation phase.
It uses the hierarchical structure determined by the syntax-analysis phase to identify
the operators and operands of expressions and statements
An important component of semantic analysis is type checking where the compiler
checks that each operator has matching operands. For example, many programming
language definitions require an array index to be an integer; the compiler must report
an error if a floating-point number is used to index an array
It checks whether the parse tree constructed follows the rules of language.
It only checks errors
It also gathers type information and saves it in either the syntax tree or the symbol
table, for subsequent use during intermediate-code generation
Suppose that total, count, and rate have been declared to be floating-
point numbers, and that the lexeme 10 by itself forms an integer. The
type checker in the semantic analyzer discovers that the operator * is
applied to a floating-point number rate and an integer 10. In this case,
the integer may be converted into a floating-point number.
notice that the output of the semantic analyzer has an extra node for
the operator inttofloat , which explicitly converts its integer argument
into a floating-point number
LDF R2 , id3
MULF R2 , R2 , 60 . 0
LDF R1 , id2
ADDF R1 , R 1 , R2
STF id1 , R1
Q&A