Bison does not appear to recognize C string literals appropriately

杀马特。学长 韩版系。学妹 提交于 2019-12-23 02:46:09

问题


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!


回答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.

For details, see this similar question




回答2:


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



来源:https://stackoverflow.com/questions/699054/bison-does-not-appear-to-recognize-c-string-literals-appropriately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!