Professional Documents
Culture Documents
Class 07 B
Class 07 B
Class 07 B
de
Lexical analysis II
1. From NFA to DFA
2. How lex works
3. From RE to DFA
lex
lex.l lex.yy.c
compiler
C
lex.yy.c a.out
compiler
input token
a.out
stream sequence
Source: Aho, Lam, Seti, Ullman; Compiler construction – principles, techniques, and tools
translation rules:
Pattern { Action }
Read input until the longest input prefix that matches an RE pattern
(several matching REs: use the first one); execute actions, until
one returns control to parser; lexer returns token name to parser;
global variable yylval stores additional information
TECHNISCHE HOCHSCHULE DEGGENDORF
4
www.th-deg.de
Lex
• Lookahead operator
– usually, only read 1 character at a time
– sometimes, it is necessary to postpone recognition until
AFTER the sequence to be matched is read
• Lookahead operator "/" separates pattern to be matched from
additional pattern
• Example:
In FORTRAN, keywords are not reserved
=>
IF(i,j)=3
is an array assignment – not a condition!
Lex rule:
IF / \( .* \) {letter}