编译器设计 实践
LEX
LEX 最简分词 示例, 类似cat命令1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23LEX 代码
chunli@ubuntu:~/lab/yacc/lex$ cat 001.l
%%
.|\n ECHO;
%%
chunli@ubuntu:~/lab/yacc/lex$
编译 && 运行
chunli@ubuntu:~/lab/yacc/lex$ lex 001.l
chunli@ubuntu:~/lab/yacc/lex$ gcc lex.yy.c -ll && ./a.out
3213213
3213213
412312
412312
534534
534534
52423
52423
^C
chunli@ubuntu:~/lab/yacc/lex$
LEX 分词器 识别动词方法
1 |
|
添加更多词性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
91chunli@ubuntu:~/lab/yacc/lex$ cat 001.l
%{
/*
* 演示识别 动词/非动词
*
*/
%}
%%
[ \t]+ ; // 忽略空白
is |
am |
are |
were |
was |
be |
being |
been |
do |
dose |
did |
will |
would |
shold |
could |
can |
has |
have |
had |
go {printf("%s is verb\n", yytext);}
very |
simply |
gently |
quietly |
camly |
angrily {printf("%s is adverb\n", yytext);}
to |
from |
behind |
below |
between |
above {printf("%s is preposition\n", yytext);}
if |
then |
and |
but |
or {printf("%s is conjunction\n", yytext);}
I |
you |
he |
she |
we |
theya {printf("%s is pronoun\n", yytext);}
[a-zA-Z]+ {printf("%s is not verb\n", yytext);}
.|\n ECHO;
%%
chunli@ubuntu:~/lab/yacc/lex$
编译 && 运行
chunli@ubuntu:~/lab/yacc/lex$ lex 001.l
chunli@ubuntu:~/lab/yacc/lex$ gcc lex.yy.c -l l && ./a.out
did you if very nice?
did is verb
you is pronoun
if is conjunction
very is adverb
nice is not verb
?
if I have simply idea
if is conjunction
I is pronoun
have is verb
simply is adverb
idea is not verb
^C
chunli@ubuntu:~/lab/yacc/lex$
一种自动识别 单词词性的分词器
1 |
|