Compiler Design 2
Compiler Design 2
Lexical Analysis
Tokens
Lexemes
Source Lexical Syntax
Program Analyzer Analyzer
Request for Tokens
Figure: Interaction between the Lexical analyzer and the Syntax analyzer
2
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
1
07/23/2020
b) Lexical analysis proper is the more complex portion, where the scanner
produces the sequence of tokens as output.
3
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
4
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
2
07/23/2020
Recognizing of constants
5
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
Pattern:
A pattern is a description of the form or rule that describes the set of strings.
Token:
A token is a set of strings over source alphabets. Also a token is a pair consisting
of a token name and an optional attribute value.
Typical tokens are,
1) Identifiers 2) keywords 3) operators 4) special symbols 5)constants
6
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
3
07/23/2020
7
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
8
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
4
07/23/2020
Prefix
Suffix
Substring
Subsequence
9
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
Operations on Languages
Union of two languages L and M is written as
L U M = {s | s is in L or s is in M}
LM = {st | s is in L and t is in M}
10
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
5
07/23/2020
Regular Expressions
11
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
If r and s are regular expressions denoting the languages L(r) and L(s), then
12
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
6
07/23/2020
1. int x1;
x=23;
7
07/23/2020
Lexical Error
Transition Diagrams
As an intermediate step in the construction of a
lexical analyzer, patterns are converted into
stylized flowcharts, called “transition diagrams”.
8
07/23/2020
Finite Automata
Finite Automata(FA) is the simplest machine to recognize patterns.
(b) Deterministic finite automata (DFA) have, for each state, and for each
symbol of its input alphabet exactly one edge with that symbol leaving that
state.
17
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU