免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3571 | 回复: 9
打印 上一主题 下一主题

我发现lex&&yacc上的代码在flex下编译出错 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-30 23:28 |只看该作者 |倒序浏览
第二章的ch2-05.l:


  1. %{
  2. #undef input
  3. #undef unput
  4. int input(void);
  5. void unput(int ch);
  6. unsigned verbose;
  7. char *progName;
  8. %}

  9. %%

  10. -h        |
  11. "-?"        |
  12. -help        { printf("usage is: %s [-help | -h | -? ] [-verbose | -v]"
  13.          " [(-file| -f) filename]\n", progName);
  14.         }
  15. -v        |
  16. -verbose { printf("verbose mode is on\n"); verbose = 1; }

  17. %%
  18. char **targv;        /* remembers arguments */
  19. char **arglim;        /* end of arguments */

  20. main(int argc, char **argv)
  21. {
  22.         progName = *argv;
  23.         targv = argv+1;
  24.         arglim = argv+argc;
  25.         yylex();
  26. }

  27. static unsigned offset = 0;

  28. int
  29. input(void)
  30. {
  31.         char c;

  32.         if (targv >= arglim)
  33.                 return(0);        /* EOF */
  34.         /* end of argument, move to the next */
  35.         if ((c = targv[0][offset++]) != '\0')
  36.                 return(c);
  37.         targv++;
  38.         offset = 0;
  39.         return(' ');
  40. }

  41. /* simple unput only backs up, doesn't allow you to */
  42. /* put back different text */
  43. void
  44. unput(int ch)
  45. {
  46.         /* AT&T lex sometimes puts back the EOF ! */
  47.         if(ch == 0)
  48.                 return;        /* ignore, can't put back EOF */
  49.         if (offset) {        /* back up in current arg */
  50.                 offset--;
  51.                 return;
  52.         }

  53.         targv--;        /* back to previous arg */
  54.         offset = strlen(*targv);
  55. }
复制代码

$ flex ch2-05.l  

Lichuang@LICHUANG /e/source/lex&&yacc
$ gcc lex.yy.c  -lfl
lex.yy.c:514: warning: static declaration of 'input' follows non-static declaration
ch2-05.l:4: warning: previous declaration of 'input' was here
ch2-05.l:37: error: redefinition of 'input'
lex.yy.c:1096: error: previous definition of 'input' was here


环境是MSYS下的flex.
难道是lex和flex的有兼容问题?

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
2 [报告]
发表于 2008-03-31 13:24 |只看该作者
用lex ch2-05.l
或者flex -l ch2-05.l
试下

论坛徽章:
0
3 [报告]
发表于 2008-03-31 17:10 |只看该作者

回复 #2 cjaizss 的帖子

还是不行:

Lichuang@LICHUANG /e/source/lex&&yacc
$ flex -l ch2-05.l  

Lichuang@LICHUANG /e/source/lex&&yacc
$ gcc lex.yy.c  -lfl
lex.yy.c:559: warning: static declaration of 'input' follows non-static declaration
ch2-05.l:4: warning: previous declaration of 'input' was here
ch2-05.l:37: error: redefinition of 'input'
lex.yy.c:1128: error: previous definition of 'input' was here

Lichuang@LICHUANG /e/source/lex&&yacc
$

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
4 [报告]
发表于 2008-04-01 17:29 |只看该作者
最前面加两句

  1. %{
  2. #define YY_NO_INPUT
  3. #define YY_NO_UNPUT
  4. #undef input
  5. #undef unput
  6. int input(void);
  7. void unput(int ch);
  8. unsigned verbose;
  9. char *progName;

  10. %}
复制代码

论坛徽章:
0
5 [报告]
发表于 2008-04-02 22:32 |只看该作者

回复 #4 cjaizss 的帖子

感谢,这次可以编译过去了,我找找lex的说明,回头在这里给一个解释.

论坛徽章:
0
6 [报告]
发表于 2008-06-25 21:13 |只看该作者

回复 #4 cjaizss 的帖子

版主怎么想到这种解决方案?因为
#ifndef YY_NO_INPUT
#ifdef __cplusplus
static int yyinput YY_PROTO(( void ));
#else
static int input YY_PROTO(( void ));
#endif
#endif
吗?添加之后是可以正常编译,但为什么可以这样处理?难道lex生成的c中这些代码是冗余的?

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
7 [报告]
发表于 2008-06-25 21:44 |只看该作者

回复 #6 Lion King 的帖子

呵呵,确实是因为这段代码

论坛徽章:
0
8 [报告]
发表于 2008-06-25 23:41 |只看该作者
这么快就回复了, 不过你没完全回答我的问题啊-_-#
btw, 你的头像是名子组成的?!

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
9 [报告]
发表于 2008-06-26 00:53 |只看该作者
呵呵,你的观察力让我佩服.
那里有一堆宏,让你可以定制,不能说冗余的

论坛徽章:
0
10 [报告]
发表于 2008-06-26 01:07 |只看该作者
^^过奖~
多谢指教
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP