Compiler Design Part 2
Compiler Design Part 2
INTRODUCTION
Finite automata and lexical Analysis:
⚫A lexical analyzer automatically by specifying the
lexeme patterns to a lexical-analyzer generator and
compiling those patterns into code that functions as a
lexical analyzer.
⚫It also speeds up the process of implementing the
lexical analyzer, since the programmer species the
software at the very high level of patterns and relies on
the generator to produce the detailed code.
⚫A lexical-analyzer generator called Lex.
The Role of the Lexical Analyzer:
⚫As the first phase of a compiler, the main task of the lexical
analyzer is to read the input characters of the source program,
group them into lexemes, and produce as output a sequence
of tokens for each lexeme in the source program. The stream
of tokens is sent to the parser for syntax analysis.
⚫Commonly, the interaction is implemented by having the
parser call the lexical analyzer.
⚫The call, suggested by the getNextToken command, causes
the lexical analyzer to read characters from its input until it
can identify the next lexeme and produce for it the next
token, which it returns to the parser.
The Role of the Lexical Analyzer:
⚫ Since the lexical analyzer is the part of the compiler that reads the source
text, it may perform certain other tasks besides identification of lexemes.
⚫ One such task is stripping out comments and whitespace (blank, newline,
tab, and perhaps other characters that are used to separate tokens in the
input).
lexical analyzers are divided into a cascade of two
processes:
E = M * C**2
1. Empirical Method
2. Formal Method
Empirical Method
⚫ This method minimized the number of states
and transitions according to the following
steps:-
❑ We merge two states in DFA into one state if they
have the same important states. The important
state is the state with no ε-transition
❑ We merge two states if they either both include or
both exclude accepting states of the NFA.
Example: Minimize the DFA to the Empirical
method.
2- Formal Method
⚫ In this method we don’t need an experience for deciding
which of the state that are legal for merging, instead of
that we must use the following algorithm:-
⚫ Algorithm: - Minimizing the number of states of a DFA
⚫ Input: - A DFA M with set of states S, inputs Σ, transitions
defined for all states and inputs, initial state S0, and set
the final states F.
⚫ Output: - A DFA M' accepting the same language as M and
having as few states as possible.
THANK YOU