我正在尝试使用 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 */
%%
caesar.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;
}
Makefile
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
请问你能帮帮我吗?谢谢!
更新:我已经修复了函数类型不正确的错误。