初学者野牛flex [英] Beginner bison flex

查看:136
本文介绍了初学者野牛flex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打印发生错误的行号.我尝试在yyerror()函数中使用yylineno并在.l文件中写入%option yylineno,但是在编译后给了我一个错误"yylineno undeclared (first use in this function)",如果我将yylineno初始化为1,就给了我这个错误:

How can I print the line number on which an error occurred. I tried using yylineno in the yyerror() function and writing %option yylineno in the .l file but after compiling it gives me an error "yylineno undeclared (first use in this function)" and if I initialize yylineno as 1 it gives me this error:

error: redefinition of yylineno
lex.yy.c:273: note: previous definition of yylineno was here

推荐答案

还有第二种方法要求flex提供全局变量yylineno:命令行开关-l.如果那对您不起作用,您可以像Levine等人一样管理自己的全局对象.建议.在与换行符匹配的任何规则中将其递增.

There is a second way to ask flex to provide the global variable yylineno: the command line switch -l. If that doesn't work for you, you can manage your own global, as Levine et al. suggests. Increment it in any rule that matches a newline.

%{
    int mylineno = 1;
%}
%%
* * * 
\n  { mylineno++; }

这篇关于初学者野牛flex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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