-
Notifications
You must be signed in to change notification settings - Fork 0
/
flex_notes.txt
91 lines (61 loc) · 1.51 KB
/
flex_notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Flex is a scanner
input
scanner specification,
suffix: l
output
c/c++ source file "lex.yy.c" containing yylex()
non-terminals:
parser looks for non-terminals by combining tokens
returned by yylex.
Scanner specification:
definitions
%%
rules
%%
user code
generated scanner action:
yylex(): take input from yyin, parse, return matched token
yytext: contains matched token (global variable)
yyleng: length of matched token
yyout: where the ECHO output goes. Default:stdout,
can be reassigend to some other FILE pointer.
Common options
-------------
%option noyywrap nodefault yylineno
noyywrap: no default main in flex
nodefault: don't generate default rule ". ECHO"
yylineno: maintain the current line number in a variable called yylineno
Simple example:
--------------
Remove "zap me" from input text file
scanner.l:
---------
%option noyywrap
%%
"zap me"
main.c:
------
#include "scanner.lex.h"
main(int argc, char **argv)
{
if(argc > 1) {
if(!(yyin = fopen(argv[1], "r"))) {
perror(argv[1]);
return 1;
}
yylex();
}else {
printf("no input file given\n\n");
return 1;
}
return 0;
}
Makefile
--------
flextest: scanner.lex.o main.o
cc -o $@ $^
scanner.lex.c:
flex -o $@ --header-file=scanner.lex.h scanner.l
clean:
rm -f scanner.lex.* main.o
f