CD 1
CD 1
UNIT-1
Language Processors
PHASES OF COMPILER DESIGN
All these phases convert the source code
by dividing into tokens, creating parse
trees, and optimizing the source code by
different phases.
(a+b)*c
Working of Compiler Phases with
Example
Front End and Back end of Compiler
Lexical Analyzer
Introduction
• Lexical analysis is the starting phase of the compiler.
It gathers modified source code that is written in
the form of sentences from the language
preprocessor.
• The lexical analyzer is responsible for breaking
these syntaxes into a series of tokens, by removing
whitespace in the source code.
• If the lexical analyzer gets any invalid token, it
generates an error.
• The stream of character is read by it and it seeks the
legal tokens, and then the data is passed to the
syntax analyzer, when it is asked for.
Roles and Responsibility of Lexical
Analyzer
The lexical analyzer performs the following tasks-
• The lexical analyzer is responsible for removing
the white spaces and comments from the source
program.
• It corresponds to the error messages with the
source program.
• It helps to identify the tokens.
• The input characters are read by the lexical
analyzer from the source code.
Example
Count number of tokens:
int max(int i);
• Lexical analyzer first read int and finds it to be
valid and accepts as token.
• max is read by it and found to be a valid
function name after reading (
• int is also a token , then again i as another
token and finally ;
• Answer:
Total number of tokens 7:
int, max, ( ,int, i, ), ;
• We can represent in the form of lexemes and
tokens as under
Input Buffering
s = abc t = def
• where the
terminals if, then, else, relop, id and num generates
sets of strings given by following regular definitions.
Lexical-Analyzer Generator
• LEX is a tool that generates a lexical
analyzer program for a given input string. It
processes the given input string/file and
transforms them into tokens. It is used by
YACC programs to generate complete parsers.
• There are many versions available for LEX, but
the most popular is flex which is readily
available on Linux systems as a part of the
compiler package.
The function of Lex is as follows: