CH 1
CH 1
CH 1
Code Optimizer
Code Generator
Target Program
oldval 12
Syntax Analyzer versus Lexical
Analyzer
• Which constructs of a program should be recognized by
the lexical analyzer, and which ones by the syntax
analyzer?
• Both of them do similar things; But the lexical analyzer
deals with simple non-recursive constructs of the
language.
• The syntax analyzer deals with recursive constructs of
the language.
• The lexical analyzer simplifies the job of the syntax
analyzer.
• The lexical analyzer recognizes the smallest meaningful
units (tokens) in a source program.
• The syntax analyzer works on the smallest meaningful
units (tokens) in a source program to recognize
meaningful structures in our programming language.
Semantic Analyzer
• A semantic analyzer checks the source program for
semantic errors and collects the type information for the
code generation.
• Type-checking is an important part of semantic
analyzer.
• Normally semantic information cannot be represented
by a context-free language used in syntax analyzers.
• Context-free grammars used in the syntax analysis are
integrated with attributes (semantic rules)
• the result is a syntax-directed translation,
• Attribute grammars
• Ex: newval:= oldval+ 12
• The type of the identifier newval must match with type
of the expression (oldval+12)
Intermediate Code Generation
• A compiler may produce an explicit intermediate codes
representing the source program.
• These intermediate codes are generally machine
(architecture independent). But the level of intermediate
codes is close to the level of machine codes.
• Ex: newval:= oldval* fact + 1
Code Generator
[Intermediate Code Generator]
Code Optimizer
Parser
[Syntax Analyzer] Optimized Intermediate Code
Parse tree
Code Optimizer
Semantic Process
[Semantic analyzer] Target machine code
%%
/* lex patterns and actions */
{INT} {sscanf (yytext, “%d”, &i);
printf (“INTEGER\n”);}
%%
/* C functions called by the above actions */
{ yylex(): }
Yacc Parser Generator
General Information:
•Input is specification of a language
•Output is a compiler for that language
•yacc generates C function stored in y.tab.c
•Public domain version avalable Bison
using yacc:
1)Generates a C function called yyparse()
2)yyparse() may include calls to yylex()
3)Compile this function to obtain the compiler
Yacc Parser Generator