Compiler 2
Compiler 2
Chapter 2
Lexical Analysis
By Diriba Regasa (MSc)
Lexical analysis
3
…
4
…
Let us understand how the language theory undertakes the following terms:
6
…
7
…
8
…
9
…
10
…
11
…
12
Finite Automaton
13
…
14
Finite automata model
Finite control 15
Representation of Finite Automata
Transition diagram
• The transition diagram is also called a transition graph; it is
represented by a diagraph. A transition graph consists of three
things
• Arrow: The initial state in the transition diagram is marked
with an arrow.
• Circle: Each circle represents the state.
• Double circle: Double circle indicates the final state or
accepting state.
16
…
Transition Table
– It is the tabular representation of the behavior of the
transition function that takes two arguments, the first is a
state, and the other is input, and it returns a value, which is
the new state of the automata.
– It represents all the moves of finite-state function based on
the current state and input.
– In the transition table, the initial state is represented with an
arrow, and the final state is represented by a single circle.
17
…
18
Types of finite automata
• Deterministic
– On each input there is one and only one state to which the automaton
can transition from its current state
• Nondeterministic
– An automaton can be in several states at once.
19
Deterministic Finite Automaton (DFA)
20
…
21
Non-Deterministic Finite Automaton (NDFA)
22
…
23
DNA vs NDFA
DFA NDFA
The transition from a state is to a single The transition from a state can be to
next state for each input symbol. Hence it multiple next states for each input symbol.
is called deterministic. Hence it is called non-deterministic.
Empty string transitions are not seen in NDFA permits empty string transitions.
DFA.
Backtracking is allowed in DFA In NDFA, backtracking is not always
possible.
Requires more space. Requires less space.
A string is accepted by a DFA, if it transits A string is accepted by a NDFA, if at least
to a final state. one of all possible transitions ends in a
final state.
24
Error recover
25
…
26
…
28
Run-time error
• These are errors that occur when a user enters improper syntax
into a code or enters code that a typical compiler cannot run.
29
Logical error
30
Mode of Error recovery
31
Examples
32
…
33
…
34
…
35
…
36
…
37
The End