yylex()之后如何执行代码;命令 [英] how to execute code after yylex(); command

查看:206
本文介绍了yylex()之后如何执行代码;命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的flex源代码,它跳过了/* */中的注释,应该获取找到的注释数:

I have a simple flex source code which skips the comments in /* */ and should get the count of comments found:

%{
  int in_comment = 0;
  int count = 0;
%}

%%
\/\* { in_comment = 1; count++; }
\*\/ { in_comment = 0; }
.    { if (!in_comment) ECHO; }
%%

int main(void)
{
  yylex();
  printf("Comments found %d\n", count); // never executed
  return 0;
}

上半部分效果很好-确实跳过了注释,但不计算在内...执行printf行我该怎么办?

First half works fine - it really skips the comments, but they are not counted... what can I do to execute printf line?

推荐答案

我自己尝试过.所以我将您的源代码复制到"x.l",并做了一个make x ld然后抱怨缺少yywrap()函数.添加后

I just tried it myself. So I copied your source code to "x.l" and did a make x ld then complained about the missing yywrap() function. After adding

%option noyywrap

编译成功,并且测试显示:

The compile succeeded and a test showed:

ronald@cheetah:~/tmp$ ./x < cribbage.c
... lots of output ...
Comments found 15

更新:

如果未从文件(只是./x)加载文本,则必须通过CTRL + D

If the text is not loaded from a file (just ./x), you have to end your manual input by CTRL + D

这篇关于yylex()之后如何执行代码;命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆