Bison似乎没有恰当地识别C字符串文字

时间:2023-02-01 09:40:58

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.

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

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 ';'

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

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.

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

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.

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

My system details:

我的系统详情:

Ubuntu 8.10 (Linux 2.6.24-16-generic)
flex 2.5.34
bison 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!

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

2 个解决方案

#1


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.

这是一个经典的错误,如果你使用flex来将你的输入放到标记中,你不能将解析器中的文字字符串称为文字字符串,而是使用它们的标记。

For details, see this similar question

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

#2


Thankee, thankee, thankee!

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):

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

71 %token INSERT

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

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

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

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

最后,我在语法规则中使用了令牌INSERT:

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 :).

谢谢,jpalecek :)。

#1


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.

这是一个经典的错误,如果你使用flex来将你的输入放到标记中,你不能将解析器中的文字字符串称为文字字符串,而是使用它们的标记。

For details, see this similar question

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

#2


Thankee, thankee, thankee!

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):

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

71 %token INSERT

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

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

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

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

最后,我在语法规则中使用了令牌INSERT:

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 :).

谢谢,jpalecek :)。