所以我试图了解 Bison 和 Flex(以及两者如何结合)。我给出的示例语法很简单,
e → e plus t
e → t
t → t TIMES f
t → f
f → LPAREN e RPAREN
f → ID
我的测试输入只是“x”,我期望输出是:
"(e (t (f (ID x))))"
我得到的实际输出是:
ID x f t
我想知道为什么我的输出是向后的(我还没有添加括号)。这是我的 flex 和 bison 文件。
野牛:
%{
#include "expr-parse-defs.h"
#include <iostream>
std::string AST;
%}
%union {
char *sval;
}
%token <sval> ID PLUS TIMES LPAREN RPAREN
%%
e :
| e PLUS t { AST += std::string("e ") + $2 + "t "; }
| t { AST += "t "; }
;
t :
| t TIMES f { AST += std::string("t ") + $2 + "f "; }
| f { AST += "f "; }
;
f :
| LPAREN e RPAREN { AST += $1 + std::string("e ") + $3; }
| ID { AST += std::string("ID ") + $1 + " " ; }
;
%%
int main() {
yyparse();
std::cout << AST;
return 0;
}
柔性:
%{
#include <cstring>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include "expr-parse.tab.h"
#include "expr-parse-defs.h"
using namespace std;
int tokenpos = 0;
char * process_token(const char* token){
// we have to copy because we can't rely on yytext not changing underneath us:
char *res = new char[strlen(yytext) + 3];
strcpy(res, yytext);
yylval.sval = res;
}
%}
ID [a-zA-Z][a-zA-Z0-9_]*
%%
"+" { yylval.sval = process_token("PLUS"); return PLUS; }
"*" { yylval.sval = process_token("TIMES"); return TIMES; }
"(" { yylval.sval = process_token("LPAREN"); return LPAREN; }
")" { yylval.sval = process_token("RPAREN"); return RPAREN; }
{ID} { yylval.sval = process_token("ID"); return ID; }
[\n]
%%
int yyerror(const char *s) {
cerr << "this is a bad error message and needs to be changed eventually" << endl;
return 0;
}