【问题标题】:lex using flex -gettin output from lex.yy.c filelex 使用来自 lex.yy.c 文件的 flex -gettin 输出
【发布时间】:2011-11-06 19:43:48
【问题描述】:

我为行数和字符数编写了一个 lex 程序(.l 文件) 程序:

%{
  int charcount=0,linecount=0;
%}
%%
.charcount++
\n linecount++,charcount++;
%%
main()
{
yylex();
printf(“lines  %d”,linecount);
printf(“characters %d”,charcount);
}
int yywrap()
{
return 1;
}

我使用 flex bison 和代码块 编写程序后 我使用命令 flex lccc.l 执行它(lccc 是文件名) 现在我有 lex.yy.c 文件 请告诉我如何获得输出 编译 lex.yy.c 是 igivng 和错误.. 但是这个程序在我大学的 linux 上运行良好,在家里我在 windows 上使用上述调整.. 请帮助!

这是错误:

 J:\> gcc lex.yy.c
 lccc.l:  In function 'main':
 lccc.l:13: error:  stray'\223' in program
 lccc.l:13: error:  'lines' undeclared (first use in this function )
 lccc.l:13: error:  (Each undeclared identifier is reported only once
 lccc.l:13: error:  for each function it appears in. )
 lccc.l:13: error:  stray'\224' in program
 lccc.l:13: error:  'd' undeclared (first use in this function )
 lccc.l:14: error:  stray'\223' in program
 lccc.l:14: error:  'characters' undeclared (first use in this function )
 lccc.l:14: error:  stray'\224' in program

【问题讨论】:

  • 出现什么错误?你不能从你的问题中忽略重要信息,并合理地期望得到答案。
  • 试试看这篇文章:stackoverflow.com/questions/5456011/…
  • @Dr Beco 我从你的建议开始先生......否则我根本没有弹性......谢谢你的帖子

标签: compiler-construction compiler-errors lex lexical-analysis


【解决方案1】:

您需要将自定义代码括在大括号中。而且后面没有分号:charcount++并且你不能用逗号分隔语句,所以linecount++, charcount++;应该是linecount++; charcount++;

试试这个:

%{
  int charcount=0, linecount=0;
%}
%%
.  {charcount++;}
\n {linecount++; charcount++;}
%%
main()
{
  yylex();
  printf("lines  %d", linecount);
  printf("characters %d", charcount);
}
// ...

【讨论】:

  • 它确实有效,先生!我为愚蠢的错误感到抱歉..我们有一个非常糟糕的先生,他对这个主题一无所知..但是没有 yy wrap 的先生说有一个未定义的引用 yywrap()..然后当我包含它时..它起作用了。 .但输出并不像deired...我只是继续输入文本,它一直在接受..它没有电话号码。行数和字符数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多