
In compiler construction , Regular Expressions (RE) and Finite Automata (FA) are used in the lexical analyzer to define and recognize tokens .
In compiler construction, Regular Expressions (RE) and Finite Automata (FA) are used in the lexical analyzer to define and recognize tokens.
π Relationship (Very Important):
Regular Expression β Finite Automaton β Token Recognition
A Regular Expression is a pattern used to describe a set of strings.
π It defines how tokens (like identifiers, numbers) are formed.
| Symbol | Meaning | |
|---|---|---|
a |
single character | |
| `a | b` | OR |
ab |
concatenation | |
a* |
zero or more a | |
a+ |
one or more a | |
( ) |
grouping |
letter (letter | digit)*
π Matches:
x, var1, total99
digit+
π Matches:
10, 245, 999
if | else | while
π RE describes WHAT to match
A Finite Automaton is a machine used to recognize strings that follow a pattern.
π It checks whether input belongs to a language or not.
β (q0) --a--> (q1) --b--> (q2*)
An automaton where:
(q0) --a--> (q1)
(q0) --a--> (q2)
An automaton where:
(q0) --a--> (q1)
(q1) --b--> (q2*)
| Feature | DFA | NFA |
|---|---|---|
| Transitions | One per input | Multiple possible |
| Ξ΅-moves | Not allowed | Allowed |
| Speed | Fast | Slower |
| Implementation | Easy | Complex |
| Use | Practical systems | Theory/design |
Regular Expression β NFA β DFA β Token Recognition
π βEvery Regular Expression can be converted into DFAβ
a*
Ξ΅, a, aa, aaa, ...
(q0*) --a--> (q0*)
letter (letter | digit)*
(q0) --letter--> (q1*)
(q1) --letter/digit--> (q1*)
π Frequently asked:
RE β NFA β DFA β Token Recognition
| Aspect | Regular Expression | Finite Automata |
|---|---|---|
| Definition | Pattern description | Pattern recognition machine |
| Purpose | Specify tokens | Recognize tokens |
| Nature | Declarative | Operational |
| Types | β | DFA, NFA |
| Usage | Before scanning | During scanning |
| Example | digit+ | DFA for numbers |
| Conversion | β NFA/DFA | From RE |
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.