lex 与yacc ch1-02.l 识别单词

240 篇文章 3 订阅


D:\UnxUtils>a
did I have fun?
did: is a verb
I: is not a verb
have: is a verb
fun: is not a verb
?
^Z

D:\UnxUtils>

//example.txt

did I have fun? 
If our time is usefully employed, it will either turn out some useful and important piece of work which will fetch its price in the market, or it will add to our experience and increase our capacities so as to enable us to earn money when the proper opportunity comes. Let those, who think nothing of wasting time, remember this.

lex 与yacc  ch1-02.l 识别单词 正确代码如下:

%{
/*
 * 例1-1 单词识别程序 ch1-02.l
 * 识别单词是否是动词
 */
%}
%%
[\t ]+          /*意味着空格的正闭包,忽略空白*/; 
is |
am |
are |
were |
was |
be |
being |
been |
do |
does |
did |
will |
would |
should |
can |
could |
has |
have |
had |
go		{printf("%s: is a verb\n",yytext);}
[a-zA-Z]+		{printf("%s: is not a verb\n",yytext);}

.|\n {ECHO;/*通常的默认状态:输出匹配模式,复制标点或其它字符*/}
%%

int main()
{
	yyin = fopen("example.txt","r");
		yylex();
	fclose(yyin);
}
int yywrap()
{
	return 1;
}

D:\UnxUtils>a
did I have fun?
did: is a verb
I: is not a verb
have: is a verb
fun: is not a verb
?
^Z

D:\UnxUtils>a
did: is a verb
I: is not a verb
have: is a verb
fun: is not a verb
?
If: is not a verb
our: is not a verb
time: is not a verb
is: is a verb
usefully: is not a verb
employed: is not a verb
,it: is not a verb
will: is a verb
either: is not a verb
turn: is not a verb
out: is not a verb
some: is not a verb
useful: is not a verb
and: is not a verb
important: is not a verb
piece: is not a verb
of: is not a verb
work: is not a verb
which: is not a verb
will: is a verb
fetch: is not a verb
its: is not a verb
price: is not a verb
in: is not a verb
the: is not a verb
market: is not a verb
,or: is not a verb
it: is not a verb
will: is a verb
add: is not a verb
to: is not a verb
our: is not a verb
experience: is not a verb
and: is not a verb
increase: is not a verb
our: is not a verb
capacities: is not a verb
so: is not a verb
as: is not a verb
to: is not a verb
enable: is not a verb
us: is not a verb
to: is not a verb
earn: is not a verb
money: is not a verb
when: is not a verb
the: is not a verb
proper: is not a verb
opportunity: is not a verb
comes: is not a verb
.Let: is not a verb
those: is not a verb
,who: is not a verb
think: is not a verb
nothing: is not a verb
of: is not a verb
wasting: is not a verb
time: is not a verb
,remember: is not a verb
this: is not a verb
.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值