c++ - why simple grammar rule in bison not working? -
i learning flex & bison , stuck here , cannot figure out how such simple grammar rule not work expected, below lexer code:
%{ #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 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 identifier { cout<<"done"<<endl; return 0;} ; %%
can 1 tell me why shows error if try put "select * something"
[a-za-z].* { return identifier; }
the problem here. allows junk follow initial alpha character , returned identifier,
including in case entire rest of line after initial ''s.
it should be:
[a-za-z]+ { return identifier; }
or possibly
[a-za-z][a-za-z0-9]* { return identifier; }
or whatever else want allow follow initial alpha character in identifiers.
Comments
Post a Comment