简体   繁体   English

为什么简单的野牛语法规则不起作用?

[英]why simple grammar rule in bison not working?

I am learning flex & bison and I am stuck here and cannot figure out how such a simple grammar rule does not work as I expected, below is the lexer code: 我正在学习flex&bison,我被困在这里,无法弄清楚这样一个简单的语法规则如何不符合我的预期,下面是lexer代码:

%{

#include <stdio.h>
#include "zparser.tab.h"

%}

%%

[\t\n ]+        //ignore white space

FROM|from           { return FROM;   }
select|SELECT       { return SELECT; }
update|UPDATE       { return UPDATE; }
insert|INSERT       { return INSERT; }
delete|DELETE       { return DELETE; }
[a-zA-Z].*          { return IDENTIFIER; }
\*                  { return STAR;   }

%%

And below is the parser code: 下面是解析器代码:

%{
#include<stdio.h>
#include<iostream>
#include<vector>
#include<string>
using namespace std;

extern int yyerror(const char* str);
extern int yylex();


%}

%%

%token SELECT UPDATE INSERT DELETE STAR IDENTIFIER FROM;


ZQL     : SELECT STAR FROM  IDENTIFIER { cout<<"Done"<<endl; return 0;}
        ;

%%

Can any one tell me why it shows error if I try to put "select * from something" 任何人都可以告诉我,如果我尝试输入“从某事中选择*”,为什么它会显示错误?

[a-zA-Z].* will match an alphabetic character followed by any number of arbitrary characters except newline. [a-zA-Z].*将匹配字母字符,后跟除换行符之外的任意数量的任意字符。 In other words, it will match from an alphabetic character to the end of the line. 换句话说,它将从字母字符匹配到行尾。

Since flex always accepts the longest match, the line select * from ... will appear to have only one token, IDENTIFIER , and that is a syntax error. 由于flex始终接受最长的匹配,因此select * from ...行将似乎只有一个令牌IDENTIFIER ,这是语法错误。

[a-zA-Z].* { return IDENTIFIER; }

The problem is here. 问题在这里。 It allows any junk to follow an initial alpha character and be returned as IDENTIFIER, including in this case the entire rest of the line after the initial ''s. 它允许所有垃圾内容跟随初始的字母字符并作为IDENTIFIER,返回IDENTIFIER,在这种情况下IDENTIFIER,包括在初始''之后的整个行的其余部分。

It should be: 它应该是:

[a-zA-Z]+          { return IDENTIFIER; }

or possibly 或可能

[a-zA-Z][a-zA-Z0-9]*          { return IDENTIFIER; }

or whatever else you want to allow to follow an initial alpha character in your identifiers. 或您希望允许标识符后面跟随字母首字母字符的任何其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM