Bison 似乎无法正确识别 C 字符串文字

发布于 2024-07-17 01:22:41 字数 848 浏览 4 评论 0原文

我的问题是我正在尝试运行使用 flex-bison 扫描仪解析器编码的问题。 我的程序应该做的是获取用户输入(在我的例子中,查询我正在设计的数据库系统),进行 lex 和解析,然后执行相应的操作。 实际发生的情况是我的解析器代码没有正确解释我提供给它的字符串文字。

这是我的代码:

130 insertexpr :  "INSERT" expr '(' expr ')'
131 
132                  {
133                         $$ = new QLInsert( $2, $4 );
134                          }
135                         ;

以及我的输入,遵循“查询:”提示:

查询:INSERT abc(5);
输入:1.0-5:语法错误,意外的字符串,期望文件结尾或行结尾或 INSERT 或“;”

现在,如果我从第 130 行的 parser.yy 代码中删除“INSERT”字符串文字,则程序运行得很好。 事实上,在存储输入数据(即“abc”和整数 5)后,它会正确返回给我。

起初,我认为这是字符编码的问题。 Bison 代码需要使用相同的编码进行编译和运行,这不应该是一个问题,因为我是从同一终端编译和运行的。

我的系统详细信息:

Ubuntu 8.10 (Linux 2.6.24-16-generic)
弹性 2.5.34
野牛2.3
gcc 4.2.4

如果您需要更多信息或代码,请告诉我!

My problem is that I am trying to run a problem that I coded using a flex-bison scanner-parser. What my program is supposed to do is take user input (in my case, queries for a database system I'm designing), lex and parse, and then execute the corresponding actions. What actually happens is that my parser code is not correctly interpreting the string literals that I feed it.

Here's my code:

130 insertexpr :  "INSERT" expr '(' expr ')'
131 
132                  {
133                         $ = new QLInsert( $2, $4 );
134                          }
135                         ;

And my input, following the "Query: " prompt:

Query: INSERT abc(5);
input:1.0-5: syntax error, unexpected string, expecting end of file or end of line or INSERT or ';'

Now, if I remove the "INSERT" string literal from my parser.yy code on line 130, the program runs just fine. In fact, after storing the input data (namely, "abc" and the integer 5), it's returned right back to me correctly.

At first, I thought this was an issue with character encodings. Bison code needs to be compiled and run using the same encodings, which should not be an issue seeing as I am compiling and running from the same terminal.

My system details:

Ubuntu 8.10 (Linux 2.6.24-16-generic)
flex 2.5.34
bison 2.3
gcc 4.2.4

If you need any more info or code from, let me know!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心旳酸楚 2024-07-24 01:22:41

这是一个典型的错误,如果您使用 flex 将输入词法为标记,则不能将解析器中的文字字符串称为文字字符串,而是使用标记来表示它们。

有关详细信息,请参阅此类似问题

This is a classic error, if you use flex to lex your input into tokens, you must not refer to the literal strings in the parser as literal strings, but rather use tokens for them.

For details, see this similar question

习惯成性 2024-07-24 01:22:41

谢谢,谢谢,谢谢!

只是为了澄清一下,以下是我如何根据 jpalecek 的评论实施我的解决方案。 首先,我在 bison 代码 (parser.yy) 中声明了一个 INSERT 标记:

71 %token INSERT

接下来,我在 Flex 代码 (scanner.ll) 中定义了该标记:

79 "INSERT INTO" { return token::INSERT; }

最后,我在语法规则中使用了 INSERT 标记:

132 insertexpr :  INSERT expr '(' expr ')'
133 
134                  {
135                         $ = new QLInsert( $2, $4 );
136                          }
137                         ;

瞧! 我过度的头痛终于结束了!

谢谢,jpalecek:)。

Thankee, thankee, thankee!

Just to clarify, here is how I implemented my solution, based on the comments from jpalecek. First, I declared an INSERT token in the bison code (parser.yy):

71 %token INSERT

Next, I defined that token in the flex code (scanner.ll):

79 "INSERT INTO" { return token::INSERT; }

Finally, I used the token INSERT in my grammar rule:

132 insertexpr :  INSERT expr '(' expr ')'
133 
134                  {
135                         $ = new QLInsert( $2, $4 );
136                          }
137                         ;

And voila! my over-extended headache is finally over!

Thanks, jpalecek :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文