Bison似乎无法正确识别C字符串文字 [英] Bison does not appear to recognize C string literals appropriately

查看:235
本文介绍了Bison似乎无法正确识别C字符串文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的代码:

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

然后在查询:"提示下输入我的信息:

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

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

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

现在,如果我从第130行的parser.yy代码中删除了"INSERT"字符串文字,则程序运行正常.实际上,在存储输入数据(即"abc"和整数5)之后,它会正确地正确返回给我.

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.

我的系统详细信息:

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

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!

推荐答案

这是一个经典错误,如果您使用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

这篇关于Bison似乎无法正确识别C字符串文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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