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
私を手伝ってくれますか?ありがとう!
更新:関数の型が正しくない場合のエラーはすでに修正されています。