FILE yyin set before calling yylex int yylex call once per token char yytext chars matched by yylex int yywrap endoffile handler ID: 629263
Download Presentation The PPT/PDF document "lex (1) and flex(1) Lex public interfac..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
lex(1) and flex(1)Slide2
Lex public interfaceFILE *yyin
; /* set before calling
yylex
() */
int
yylex
(); /* call once per token */
char
yytext
[]; /* chars matched by
yylex
() */
int
yywrap
(); /* end-of-file handler */Slide3
.l file formatheader
%%
body
%%
helper functionsSlide4
Lex headerC code inside %{ … %}prototypes for helper functions
#
include’s
that #define integer token categories
Macro definitions, e.g.
letter [a-
zA
-Z]
digit [0-9]
ident
{letter}({letter}|{digit})*
Warning: macros are fraught with perilSlide5
Lex bodyRegular expressions with semantic actions
“ “ { /* discard */ }
{
ident
} { return IDENT; }
“*” { return ASTERISK; }
“.” { return PERIOD; }
Match the longest
r.e
. possible
Break ties with whichever appears first
If it fails to match: copy unmatched to
stdoutSlide6
Lex helper functionsFollows rules of ordinary C codeCompute lexical attributes
Do stuff the regular expressions can’t do
Write a
yywrap
() to switch files on EOFSlide7
Lex regular expressions\c escapes for most operators“s” match C string as-is (
superescape
)
r{
m,n
} match r between m and n times
r/s match r when s follows
^r match r when at beginning of line
r$ match r when at end of lineSlide8
struct tokenstruct token {
int
category;
char *text;
int
linenumber
;
int
column;
char *filename;
union literal value;
}Slide9
“string removal tool”%%“zap me”Slide10
whitespace trimmer%%[ \t]+ putchar
(‘ ‘);
[ \t]+ /* drop entirely */Slide11
string replacement%%username printf
(“%s”,
getlogin
() );Slide12
Line/word counter int
lines=0, chars=0;
%%
\n ++lines; ++chars;
. ++chars;
%%
main() {
yylex
();
printf
(“lines: %d chars: %d\n”, lines, chars);
}Slide13
Example: C realsIs it: [0-9]*.[0-9]*Is it: ([0-9]+.[0-9]* | [0-9]*.[0-9]+)