compiler-construction - lex 使用来自 lex.yy.c 文件的 flex

我为行数和字符数编写了一个 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

最佳答案

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

试试这个:

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

关于compiler-construction - lex 使用来自 lex.yy.c 文件的 flex -gettin 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8029855/

相关文章:

java - 编译Java项目时出错

visual-studio-2010 - 未注册库时,无法使用引用的TLB编译.NET应用程序

php - 使用PHP的exec()函数(或类似函数),如何显示终端错误输出?

java - Java编译时错误: reached end of file while parsin

android - 在 Android 中初始化一个大的常量数据结构

objective-c - 尽管导入了正确的 header ,但使用自定义类的编译器错误

iphone - 编译我的应用程序没有错误,但是在设备上编译时挂起

compiler-errors - nant 构建文件使用 nant 0.86 而不是 0.91 a

java - 复选框编译错误

asp.net - 加载共享类库时出错