Professional Documents
Culture Documents
02 Lexicalanalysis 2013 2014
02 Lexicalanalysis 2013 2014
Lexical analysis
Lexical analysis 44
Outline
1. Principle
2. Regular expressions
Lexical analysis 45
Structure of a compiler
character stream
Lexical analysis
token stream
Syntax analysis
syntax tree
Semantic analysis
syntax tree
Code generation
machine code
Code optimization
machine code
Lexical analysis 46
Lexical analysis or scanning
Goals of the lexical analysis
I Divide the character stream into meaningful sequences called lexemes.
I Label each lexeme with a token that is passed to the parser (syntax
analysis)
I Remove non-significant blanks and comments
I Optional: update the symbol tables with all identifiers (and numbers)
Provide the interface between the source program and the parser
(Dragonbook)
Lexical analysis 47
Example
ip z ip
w h i l e ( i p < z ) \n \t + + i p ;
Lexical analysis 48
Example
While
< ++
ip z ip
w h i l e ( i p < z ) \n \t + + i p ;
Lexical analysis 49
Lexical versus syntax analysis
Lexical analysis 50
Tokens, patterns, and lexemes
Lexical analysis 51
Defining a lexical analysis
Lexical analysis 52
Choosing the tokens
Lexical analysis 53
Describing the patterns
Lexical analysis 54
Reminder: languages
Lexical analysis 55
Reminder: regular expressions
Any character a 2 ⌃ is a regular expression L = {a}
✏ is a regular expression L = {✏}
If R1 and R2 are regular expressions, then
I R1 R2 is a regular expression
S L(R1 R2 ) is the concatenation of L(R1) and L(R2)
I R1 |R2 (= R1 R2 ) is a regular expression S
L(R1 |R2 ) = L(R1 ) L(R2 )
I R1⇤ is a regular expression
L(R1⇤ ) is the Kleene closure of L(R1 )
I (R1 ) is a regular expression
L((R1 )) = L(R1 )
Lexical analysis 56
Notational conveniences
Regular definitions:
letter ! A|B|...|Z|a|b|...|z
digit ! 0|1|...|9
id ! letter (letter |digit)⇤
Lexical analysis 57
Examples
Keywords:
if, while, for, . . .
Identifiers:
[a-zA-Z ][a-zA-Z 0-9]⇤
Integers:
[+ ]?[0-9]+
Floats:
[+ ]?(([0-9]+ (.[0-9]⇤ )?|.[0-9]+ )([eE][+ ]?[0-9]+ )?)
String constants:
“([a-zA-Z0-9]|\[a-zA-Z])⇤ ”
Lexical analysis 58
Algorithms for lexical analysis
Lexical analysis 59
ed by the following sequence of transitions:
Reminder:
by non-deterministic automata (NFA)
a
A non-deterministic automaton is a five-tuple M = (Q, ⌃, , s0 , F )
where:
a Q is a finite set of states,
b
⌃ is an alphabet,
he input we are in state
S
3, which
⇢ (Q ⇥ (⌃ {✏})is⇥accepting.
Q) is theHence, the string
transition is
relation,
e NFA. You can check this by placing a coin at the starting state and
s 2 Q is
sitions by moving thethe
coin.initial state,
we sometimes
F ✓have
Q aischoice
the setof several transitions.
of accepting If we are in state
states
Example:
2
a Transition table
a State a b ✏
1 ; {3} {2}
b 2 {1,3} ; ;
1 3
3 ; ; ;
(Mogensen)
Figure 2.3: Example of an NFA
Lexical analysis 60
Reminder: fromregular
Reminder: from regular expression
expression to NFA
to NFA
Reminder: from regular expression to NFA
AAregular
regular expression
expressioncancan
be be
transformed into aninto
transformed
st
equivalent NFA
an equivalent NFA
A regular expression
Reminder: can be
from regular st
transformed
expression to into
NFAan equivalent N
✏
✏ s|t
Aa regular expression can be transformed
s|t into an equivalent NFA
✏a
a
s⇤
s⇤
st Lexical analysis
st Lexical analysis
st
s|t
s|t
s|t
(Dragonbook)
Lexical analysis 61
Reminder:20 from regular expressionCHAPTER
to NFA2. LEXICAL ANALYSIS
Example: (a|b)⇤ ac (Mogensen)
a
6
5 8
7 b
a c
1 2 3 4
(Dragonbook)
Lexical analysis 63
Lexical analysis
Lexical analysis 64
Illustration
i! f!
1 2 3 IF#
=
4 5 EQ#
[0-9]!
[0-9]!
6 7 NUM#
[a-z0-9]!
[a-z]!
8 9 ID#
Lexical analysis 65
Illustration: ambiguities
i! f!
1 2 3 IF#
=
4 5 EQ#
[0-9]!
[0-9]!
6 7 NUM#
[a-z0-9]!
[a-z]!
8 9 ID#
Lexical analysis 67
Other source of ambiguity
Two solutions:
I Report an error (such conflict is not allowed in the language)
I Let the user decide on a priority order on the tokens (eg., keywords
have priority over identifiers)
Lexical analysis 68
What if nothing matches
What if we can not reach any accepting states given the current
input?
Add a “catch-all” rule that matches any character and reports an
error
i! f!
1 2 3 IF#
=
4 5 EQ#
[0-9]!
[0-9]!
6 7 NUM#
[a-z0-9]!
[a-z]!
8 9 ID#
10! 11!
Lexical analysis 69
Merging all automata into a single NFA
i! f!
1 2 3 IF#
=
4 5 EQ#
[0-9]!
[0-9]!
0 6 7 NUM#
[a-z0-9]!
[a-z]!
8 9 ID#
10! 11!
Lexical analysis 70
Lexical analysis with an NFA: summary
Lexical analysis 71
Computational efficiency
(Dragonbook)
In the worst case, an NFA with |Q| states takes O(|S||Q|2 ) time to
match a string of length |S|
Complexity thus depends on the number of states
It is possible to reduce complexity of matching to O(|S|) by
transforming the NFA into an equivalent deterministic finite
automaton (DFA)
Lexical analysis 72
Reminder: deterministic finite automaton
S
Like an NFA but the transition relation ⇢ (Q ⇥ (⌃ {✏}) ⇥ Q) is
such that:
I Transitions based on ✏ are not allowed
I Each state has at most one outgoing transition defined for every letter
Transition relation is replaced by a transition function
:Q ⇥⌃!Q
22 CHAPTER 2. LEXICAL ANALYSIS
Example of a DFA
a 2
b
a
b
1 3
(Mogensen)
Figure 2.8: Example of a DFA
Lexical analysis 73
Reminder: from NFA to DFA
DFA and NFA (and regular expressions) have the same expressive
power
An NFA can be converted into a DFA by the subset construction
method
Main idea: mimic the simulation of the NFA with a DFA
I Every state of the resulting DFA corresponds to a set of states of the
NFA. First state is ✏-closure(s0 ).
I Transitions between states of DFA correspond to transitions between
set of states in the NFA:
Lexical analysis 74
Reminder:
20 from NFA to DFA
CHAPTER 2. LEXICAL ANALYSIS
NFA
Figure 2.5: NFA for the ⇤
6 regular expression (a|b) ac
a
5 8
ted NFA. Note that even though
7
we allow an NFA to have several ac-
b
tes, an NFA constructed using this method will have only one: the one
e end of the construction.
a c
A 4 ⇤ ac is shown in
7. constructed
SIZE VERSUSthis way for1 the regular2 expression (a|b)
3
SPEED 29
We have numbered the states for future reference.
a
DFA Figure 2.5: NFA for the regular expression (a|b)⇤ ac
ptimisations
s00 {1, 2, 5, 6, 7}
the completed
e the construction inNFA.
figureNote2.4
a thatfor
even though
s1 any we allow an NFA to have byseveral ac-
cepting states, an NFA constructed c regular
using
expression
this method will have only one:
expanding
s01the {3,
one 8, 1, 2, 5, 6, 7}
rthand, e.g. converting ⇤
s construction.
+ to ssa , [0-9] to s0|1|2| · · · |9 and s? to s| , etc.
added at the end ofsthe b
0 3
his will result
AninNFAvery large NFAs
constructed this wayfor some
for the expressions,
regular expression (a|b)so wes02use{8,
⇤ ac is shown
ainfew
1, 2, 5, 6, 7}
figure 2.5. We have numbered
b the states for future reference.
constructions for the shorthands.s2 Additionally, we show an alternative
n for the 2.4.1
regular expression . This construction does not s03 follow
quite {4}
Optimisations
a used in figure 2.4, as it does not have two half-transitions. Rather,
We can use the construction in figure
b 2.4 for any regular expression by expanding
gment notation is intended
out all shorthand, to indicate
e.g. converting s+ to ssthat theto NFA
⇤ , [0-9] 0|1|2| · ·fragment
· |9 and s? to for
s| , etc.just
he half-transitions
However, thisofwillthe NFA
result in veryfragments
large NFAs forthat someitexpressions,
is combined with.
so we use a few In (Mogensen)
optimised constructions for the shorthands. Additionally, we show an alternative
uction foranalysis
Lexical Figure
[0-9],2.9:the
DFAvertical
constructedellipsis
from theisNFA meant to 2.5
in figure indicate that there is 75
Simulating a DFA
Lexical analysis 76
Lexical analysis with a DFA: summary
Lexical analysis 77
Example: combined NFA for several tokens
i f IF
2 3 4
[a-zA-Z 0-9]
[a-zA-Z ]
5 6
ID
[+-] [0-9]
1 7 8 9
NUM
[0-9] [0-9]
[+-] [0-9] .
10 11 12 13
FLOAT FLOAT
. [eE] [eE]
[0-9] [eE] [+-] [0-9]
14 15 16 17 18
FLOAT FLOAT
(Mogensen)
Lexical analysis Figure 2.12: Combined NFA for several tokens 78
Example: combined DFA for several tokens
IF
C [a-zA-Z 0-9]
f [a-zA-Z 0-9]
ID [a-eg-zA-Z 0-9]
B D
ID
i [a-hj-zA-Z ]
.
A E
[0-9]
J K
FLOAT
Lexical analysis 79
Speed versus memory
Note:
It is possible to minimise the number of states of a DFA in
O(n log n) (Hopcroft’s algorithm1 )
I Theory says that any regular language has a unique minimal DFA
I However, the number of states may remain exponential in the size of
the regular expression after minimization
1
http://en.wikipedia.org/wiki/DFA_minimization
Lexical analysis 80
Keywords and identifiers
Lexical analysis 81
Summary
Kleene(
construc5on(
Token(
pa?erns( Analyzer(
minimiza5on(
Regular(
expressions( DFA(
Thompson’s(
NFA( determiniza5on(
construc5on(
Lexical analysis 82
Some langage specificities
Language specificities that make lexical analysis hard:
Whitespaces are irrelevant in Fortran.
DO 5 I = 1,25
DO5I = 1.25
PL/1: keywords can be used as identifiers:
IF THEN THEN THEN = ELSE; ELSE ELSE = IF
Python block defined by indentation:
if w == z:
a = b
else:
e = f
g = h
(the lexical analyser needs to record current identation and output a
token for each increase/decrease in indentation)
(Keith Schwarz)
Lexical analysis 83
Some langage specificities
NB: How could you test if your compiler accepts nested comments
without generating a compilation error?
int nest = /*/*/0*/**/1;
Lexical analysis 84
Implementing a lexical analyzer
Lexical analysis 85
The output is a list of tokens and lexemes from the source program. The follow
Example of an shows
program fragment ad-hoc lexical
a skeletal analyser of a simple loop and switch scan
implementation
The main
(source: program calls InitScanner and loops calling ScanOneToken until
http://dragonbook.stanford.edu/lecture-notes.html)
ScanOneToken reads the next character from the file and switches off that char to de
how to handle
Definition what
of the is coming
token classesup(through
next in the file. The return values from the sca
constants)
can be passed on to the parser in the next phase.
#define T_SEMICOLON ';' // use ASCII values for single char tokens
#define T_LPAREN '('
#define T_RPAREN ')'
#define T_ASSIGN '='
#define T_DIVIDE '/'
...
Lexical analysis 86
Example of an ad-hoc lexical analyser
Structure for tokens 3
struct token_t {
int type; // one of the token codes from above
union {struct token_t {
char stringValue[256];
int type; // holds lexeme
// one value if token
of the string/identifier
codes from abov
int intValue;
union { // holds lexeme value if integer
double doubleValue; // holds lexeme
char stringValue[256]; value
// holds if double
lexeme value if string/ide
} val; int intValue; // holds lexeme value if integer
}; double doubleValue; // holds lexeme value if double
} val;
Main function
int main(int
}; argc, char *argv[])
{
struct token_t token;
int main(int argc, char *argv[])
{
InitScanner();
struct token_t token;
while (ScanOneToken(stdin, &token) != T_END)
; // this is where you would process each token
return 0;InitScanner();
} while (ScanOneToken(stdin, &token) != T_END)
; // this is where you would process each token
static voidreturn 0;
InitScanner()
{ }
create_reserved_table(); // table maps reserved words to token type
static void InitScanner()
insert_reserved("WHILE", T_WHILE)
{
insert_reserved("IF", T_IF)
Lexical analysis create_reserved_table(); // table maps reserved words to toke
87
} val;
};
Example of an ad-hoc lexical analyser
int main(int argc, char *argv[])
{
struct token_t token;
InitScanner();
while (ScanOneToken(stdin, &token) != T_END)
; // this is where you would process each token
return 0;
Initialization
}
switch(ch) {
case '/': // could either begin comment or T_DIVIDE op
nextch = getc(fp);
Lexical analysis 88
static void InitScanner()
{
Example of an ad-hoc lexical analyser
create_reserved_table(); // table maps reserved words to token type
insert_reserved("WHILE", T_WHILE)
insert_reserved("IF", T_IF)
insert_reserved("RETURN", T_RETURN)
....
Scanning (single-char tokens)
}
switch(ch) {
case '/': // could either begin comment or T_DIVIDE op
nextch = getc(fp);
if (nextch == '/' || nextch == '*')
; // here you would skip over the comment
else
ungetc(nextch, fp); // fall-through to single-char token case
case ';': case ',': case '=': // ... and other single char tokens
token->type = ch; // ASCII value is used as token type
return ch; // ASCII value used as token type
case 'A': case 'B': case 'C': // ... and other upper letters
token->val.stringValue[0] = ch;
for (i = 1; isupper(ch = getc(fp)); i++) // gather uppercase
token->val.stringValue[i] = ch;
Lexical analysis
ungetc(ch, fp); 89
;ungetc(nextch,
// here you wouldfp); skip
// fall-through to single-char token case
over the comment
else
Example of an ad-hoc lexical analyser
caseungetc(nextch,
';': case ',':fp); case//'=': // ... and
fall-through to other singletoken
single-char char case
tokens
token->type = ch; // ASCII value is used as token type
Scanning:
return
case keywords
';': ch;
case ',': // case
ASCII'=':
value//used
... as
andtoken
othertype
single char tokens
token->type = ch; // ASCII value is used as token type
case 'A':ch;
return case 'B': case 'C':
// ASCII // ...
value used as and
tokenother
typeupper letters
token->val.stringValue[0] = ch;
for'A':
case (i =case
1; isupper(ch = getc(fp));
'B': case 'C': // ... i++) // gather
and other upperuppercase
letters
token->val.stringValue[i]
token->val.stringValue[0] = ch;
= ch;
ungetc(ch,
for (i = 1; fp);
isupper(ch = getc(fp)); i++) // gather uppercase
token->val.stringValue[i]
token->val.stringValue[i]= ='\0'; ch; // lookup reserved word
token->typefp);
ungetc(ch, = lookup_reserved(token->val.stringValue);
token->val.stringValue[i]
return token->type; = '\0'; // lookup reserved word
token->type = lookup_reserved(token->val.stringValue);
return
case 'a':token->type;
Scanning: identifier
case 'b': case 'c': // ... and other lower letters
token->type = T_IDENTIFIER;
case 'a': case 'b': case 'c':= ch;
token->val.stringValue[0] // ... and other lower letters
token->type
for (i = 1; =islower(ch
T_IDENTIFIER;
= getc(fp)); i++)
token->val.stringValue[0] = ch; 4
for (i = 1; islower(ch = getc(fp)); i++)
token->val.stringValue[i] = ch; // gather lowercase
ungetc(ch, fp);
token->val.stringValue[i] = '\0';
if (lookup_symtab(token->val.stringValue) == NULL)
add_symtab(token->val.stringValue); // get symbol for ident
return T_IDENTIFIER;
case '0': case '1': case '2': case '3': //.... and other digits
Lexical analysis 90
token->val.stringValue[i] = ch; // gather lowercase
Example of fp);
ungetc(ch, an ad-hoc lexical
token->val.stringValue[i]
token->val.stringValue[i]
analyser
= ch;
= '\0'; // gather lowercase
ungetc(ch,
if fp);
(lookup_symtab(token->val.stringValue) == NULL)
token->val.stringValue[i] = '\0';
add_symtab(token->val.stringValue); // get symbol for ident
if (lookup_symtab(token->val.stringValue)
return T_IDENTIFIER; == NULL)
Scanning: number
add_symtab(token->val.stringValue); // get symbol for ident
return
case '0': T_IDENTIFIER;
case '1': case '2': case '3': //.... and other digits
token->type = T_INTEGER;
case '0': case '1': case
token->val.intValue = ch '2': case '3': //.... and other digits
- '0';
token->type
while = T_INTEGER;
(isdigit(ch = getc(fp))) // convert digit char to number
token->val.intValue
token->val.intValue == ch - '0';
token->val.intValue * 10 + ch - '0';
while (isdigit(ch
ungetc(ch, fp); = getc(fp))) // convert digit char to number
token->val.intValue
return T_INTEGER; = token->val.intValue * 10 + ch - '0';
ungetc(ch, fp);
case EOF: T_INTEGER;
return
Scanning: EOF and default
return T_END;
case EOF:
default: // anything else is not recognized
return T_END;
token->val.intValue = ch;
token->type//= anything
default: T_UNKNOWN;else is not recognized
return T_UNKNOWN;
token->val.intValue = ch;
} token->type = T_UNKNOWN;
return T_UNKNOWN;
}
e mythical source language tokenized by the above scanner requires that res
ords be in all upper case and identifiers in all lower case. This convenient fe
e mythical
akes easy source
it analysis
Lexical for the language
scanner totokenized by the
choose which above
path scanner
to pursue requires
after readingthat
91jus
Flex
Lexical analysis 92
Input file
Lexical analysis 93
Rules
In the form:
pattern1 action1
pattern2 action2
...
Lexical analysis 94
Regular expressions
Lexical analysis 95
Interacting with the scanner
User subroutines and action may interact with the generated scanner
through global variables:
I yylex: scan tokens from the global input file yyin (defaults to
stdin). Continues until it reaches the end of the file or one of its
actions executes a return statement.
I yytext: a null-terminated string (of length yyleng) containing the
text of the lexeme just recognized.
I yylval: store the attributes of the token
I yylloc: location of the tokens in the input file (line and column)
I ...
Lexical analysis 96
Example 1: hiding numbers
hide-digits.l:
%%
[0-9]+ printf("?");
. ECHO;
Lexical analysis 97
Example 2: wc
count.l:
%{
int numChars = 0, numWords = 0, numLines = 0;
%}
%%
\n {numLines++; numChars++;}
[^ \t\n]+ {numWords++; numChars += yyleng;}
. {numChars++;}
%%
int main() {
yylex();
printf("%d\t%d\t%d\n", numChars, numWords, numLines);
}
Lexical analysis 98
Example 3: typical compiler
Lexical analysis 99
Example 3: typical compiler