野牛语义谓词语法错误,流浪“#" [英] Bison semantic predicate syntax error, stray '#'

查看:123
本文介绍了野牛语义谓词语法错误,流浪“#"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试使用野牛的语义谓词功能,但是我一直在尝试使它起作用的过程中遇到了一些问题.

So I am trying to use bison's semantic predicate feature, but I've been running into a few issues trying to have it work.

当我尝试使用gcc编译生成的.tab.c文件时出现问题.我正在使用gcc 7.1.0和bison 3.0.4.这是编译错误的片段:

The problem comes when I try to compile the generated .tab.c file with gcc. I am using gcc 7.1.0 and bison 3.0.4. Here's a snippet of the compile error:

test2.tab.c: In function ‘yyuserAction’:
test2.tab.c:811:12: error: stray ‘#’ in program
     if (! (#line 19 "test2.y" /* glr.c:816  */
            ^
test2.tab.c:811:13: error: ‘line’ undeclared (first use in this function); did you mean ‘uint’?
     if (! (#line 19 "test2.y" /* glr.c:816  */
             ^~~~
             uint

因此,我以bison的语义谓词示例为例,并使其成为可行的示例:

So I've taken bison's example for semantic predicate, and made it a working example:

%{

int new_syntax = 0;
int yyerror(const char* msg) { /* some error handling */ }

%}

%token id

%glr-parser

%%

prog: %empty
    | prog widget
    | prog flip
    ;

widget: %?{  new_syntax } "widget" id old_arg
      | %?{ !new_syntax } "widget" id new_arg
      ;

flip: "flip" { new_syntax = !new_syntax; }
    ;

old_arg: /* something here */
       ;
new_arg: /* something else here */
       ;

%%

在处理了选项卡文件之后,我意识到在#line指令之前添加换行符可以解决语法错误,(但是直接修改生成的文件感觉有些棘手.此外,您必须对齐一些空格以便gcc计算代码的右列位置.

After playing around with the tab file, I realized that adding a newline before the #line directives resolves the syntax error, (but it feels kinda hacky directly modifying the generated file. Plus, you would have to align with some spaces in order for gcc to compute the right column position of the code).

我想知道这是否是bison本身的错误,或者我使用的语义谓词有误,还是这种语法在gcc的早期版本中是正确的,还是其他?

I wonder if is this a bug with bison itself, or is it that I'm using semantic predicates wrong, or if this syntax was correct in an earlier version of gcc, or something else.

我也曾尝试在网络上搜索此问题或已经与野牛一起提交的错误,但没有找到. (最新的野牛版本似乎已经3岁了.如果这个问题没有在任何地方得到解决,我会感到惊讶).有人可以启发我这个问题吗?谢谢.

I've also tried searching the web for this issue, or for a bug already filed with bison, but I found none. (The latest bison version seems to be 3-ish years old. I would be surprised if this issue has not been addressed anywhere at all). Can someone enlighten me about this issue? Thanks.

如果有必要,我可以尝试向野牛提出错误(需要弄清楚该怎么做),但是我不确定这是我自己的问题还是什么原因.

If necessary, I could try filing a bug with bison (need to figure out how to do that), but I'm not sure if it's my own issue or what not.

推荐答案

据我所知,此错误已经存在一段时间了(可能是因为引入了语义谓词功能,尽管似乎有人应该进行了测试)它在某种程度上与野牛的版本有关.)

As far as I can see, this bug has been present for some time (possibly since the semantic predicate feature was introduced, although it seems like someone should have tested it at some point with some version of bison.)

最简单的解决方法是关闭#line指令的生成,您可以通过在野牛命令行中简单地添加-l(或--no-lines)来完成.但是,这将使得解释错误消息更加困难,否则错误消息将引用您的野牛语法文件中的行号.

The simplest workaround is to turn off the generation of #line directives, which you can do by simply adding -l (or --no-lines) to the bison command line. However, that will make it harder to interpret error messages which would otherwise refer to line numbers in your bison grammar file.

作为第二种解决方法,您可以找到文件c.m4,该文件可能位于/usr/share/bison/c.m4中(当然,这取决于您的分发).该文件的第462行(在野牛3.0.4中)显示为:

As a second workaround, you could find the file c.m4, which is probably in /usr/share/bison/c.m4 (of course, that depends on your distribution). Line 462 of that file (in bison 3.0.4) reads:

    if (! ($2)) YYERROR;

如果您在$2之前添加换行符,使其显示为

If you add a newline just before $2, so that it reads

    if (! (
           $2)) YYERROR;

然后一切正常. (至少在我测试时确实如此.)

then everything should work out fine. (At least, it did when I tested it.)

这篇关于野牛语义谓词语法错误,流浪“#"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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