我正在尝试使用 g++ 为玩具语言编译扫描器和解析器。这是我使用的每个文件的代码(如果您愿意,我可以将其发布到 pastebin
或其他任何地方)。
caesar.ll
/* Simple scanner for a Caesar language */
%{
#include "caesar.tab.h"
#include <iostream>
#include <string>
int chars = 0;
int words = 0;
int lines = 0;
%}
/* Define constants */
OWS [" "\t]*
COMMA {OWS}","{OWS}
ID [A-Za-z_][A-Za-z0-9_]*
INT ([0-9]+)|("0x"[A-Ha-h0-9]+)
FLOAT [0-9]+"."[0-9]+
BSTREAM b[\'\"].*[\'\"]
USTREAM u?[\'\"].*[\'\"]
ARRAY {LBRACE}({INT}|{FLOAT})({COMMA}({INT}|{FLOAT})){RBRACE}
LIST {LBRACKET}.*({COMMA}.*){RBRACKET}
RANGE {LBRACE}{INT}":"{INT}(":"{INT})?{RBRACE}
ARGS {ID}({COMMA}{ID})*
LPARENTHESIS "("{OWS}
RPARENTHESIS {OWS}")"
LBRACE "{"{OWS}
RBRACE {OWS}"}"
LBRACKET "["{OWS}
RBRACKET {OWS}"]"
%%
%{
/*============================================================================*/
/* Define types */
/*============================================================================*/
%}
{INT} {
cout << "int: " << yytext << endl;
yylval = atoi(yytext);
return INT;
} /* int type */
{FLOAT} {
cout << "float: " << yytext << endl;
yylval = atof(yytext);
return FLOAT;
} /* float type */
{BSTREAM} {
cout << "bstream: " << yytext << endl;
return BSTREAM;
} /* bstream type */
{USTREAM} {
cout << "ustream: " << yytext << endl;
return USTREAM;
} /* ustream type */
%{
/*============================================================================*/
/* Define operators */
/*============================================================================*/
%}
"+" { return ADD; }
"-" { return SUB; }
"*" { return MUL; }
"/" { return DIV; }
"//" { return FDIV; }
"|" { return ABS; }
"\n" { return EOL; }
%{
/*============================================================================*/
/* Define statements */
/*============================================================================*/
%}
{RANGE} {
cout << "range: " << yytext << endl;
return RANGE;
} /* range function */
%%
凯撒.yy
/* Simple parser for a Caesar language */
%{
#include <iostream>
using namespace std;
%}
/* Define built-in types */
%token INT FLOAT BSTREAM USTREAM
%token ADD SUB MUL DIV FDIV ABS
%token EOL
%%
calclist: /* nothing */
| calclist exp EOL {
cout << $2 << endl;
}
| calclist EOL {
cout << ">>> ";
}
;
exp: factor
| exp ADD exp { $$ = $1 + $3; }
| exp SUB factor { $$ = $1 - $3; }
| exp ABS factor { $$ = $1 | $3; }
;
factor: term
| factor MUL term { $$ = $1 * $3; }
| factor DIV term { $$ = $1 / $3; }
;
term: INT
| ABS term { $$ = $2 >= 0? $2 : - $2; }
;
%%
main()
{
cout << ">>> ";
yyparse();
}
yyerror(char *error)
{
cerr << error;
}
生成文件
caesar: caesar.ll caesar.yy
bison -d caesar.yy
flex caesar.ll
g++ -o $@ caesar.tab.cc lex.yy.c -lfl
当我尝试使用 make
编译它时,我看到了几个错误:
bison -d caesar.yy
caesar.yy: conflicts: 3 shift/reduce
flex caesar.ll
g++ -o caesar caesar.tab.cc lex.yy.c -lfl
caesar.tab.cc: In function 'int yyparse()':
caesar.tab.cc:1281:16: error: 'yylex' was not declared in this scope
caesar.tab.cc:1470:35: error: 'yyerror' was not declared in this scope
caesar.tab.cc:1612:35: error: 'yyerror' was not declared in this scope
caesar.yy: At global scope:
caesar.yy:46:20: error: ISO C++ forbids declaration of 'yyerror' with no type [-fpermissive]
caesar.ll:3:24: fatal error: caesar.tab.h: No such file or directory
compilation terminated.
make: *** [caesar] Error 1
你能帮帮我吗?谢谢!
更新:我已经修复了函数类型不正确的错误。
最佳答案
首先修复明显的错误——在 caesar.yy 的顶部添加声明:
int yylex(void);
void yyerror(const char *);
并为 main
和 yyerror
返回类型(注意——我还在 yyerror 的参数中添加了 const
以消除有关字符串文字的警告被传递给它)。
您需要对 caesar.ll 进行类似的简单修复:
#include "caesar.tab.hh"
using namespace std;
现在您可以看到真正的错误:
caesar.yy: conflicts: 3 shift/reduce
caesar.ll: In function ‘int yylex()’:
caesar.ll:79:10: error: ‘RANGE’ was not declared in this scope
第二个在前——您的扫描器正试图返回一个未定义的 token 范围。
您可以将 %token RANGE
添加到 caesaer.yy
来定义它,尽管您不使用它(或各种其他标记,如 BSTREAM
或 USTREAM
) 在你的语法中它只会导致语法错误。
这给我们带来了语法冲突。这些并不是真正的错误(更像是警告),但您确实需要注意它们。向 Makefile
中的 bison
命令添加一个 -v 标志,您将获得一个包含有关冲突信息的 caesaer.output
文件。
这 3 个冲突都来自状态 16,您可以在 .output 文件中看到:
state 16
5 exp: exp . ADD exp
5 | exp ADD exp .
6 | exp . SUB factor
7 | exp . ABS factor
ADD shift, and go to state 10
SUB shift, and go to state 11
ABS shift, and go to state 12
ADD [reduce using rule 5 (exp)]
SUB [reduce using rule 5 (exp)]
ABS [reduce using rule 5 (exp)]
$default reduce using rule 5 (exp)
这告诉您所有 3 个冲突都来自您的 exp: exp ADD exp
规则。拥有一个既是左递归又是右递归的规则总是模棱两可的,但在这种情况下,解决方法很明显——将其更改为 exp: exp ADD factor
,匹配您的其余规则。
关于c++ - 弹性和 Bison : g++ compiling error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13020631/