I'm trying to compile scanner and parser for the toy language using g++. Here is the code for every file which I use (if you want, I can post it to pastebin
or anywhere else).
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
When I try to compile it using make
, I see couple of errors:
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
Could you help me, please? Thanks!
UPDATE: I've already fixed error with incorrect function type.
First fix the obvious errors -- add declarations to the top of caesar.yy:
int yylex(void);
void yyerror(const char *);
and return types for main
and yyerror
(note -- I also added const
to the argument to yyerror to silence warnings about string literals being passed to it).
You need similar trivial fixes to caesar.ll:
#include "caesar.tab.hh"
using namespace std;
Now you can see the real errors:
caesar.yy: conflicts: 3 shift/reduce
caesar.ll: In function ‘int yylex()’:
caesar.ll:79:10: error: ‘RANGE’ was not declared in this scope
The second one first -- your scanner is trying to return a token RANGE which isn't defined.
You can add %token RANGE
to caesaer.yy
to define it, though as you don't use it (or various other tokens like BSTREAM
or USTREAM
) in your grammar it will just cause a syntax error.
That brings us to the grammar conflicts. These aren't really errors (more like warnings) but you do want to pay attention to them. Add a -v flag to the bison
command in the Makefile
and you'll get a caesaer.output
file with info about the conflicts.
The 3 conflicts all come from state 16, which you can see in the .output file:
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)
This is telling you that all 3 conflicts come from your exp: exp ADD exp
rule. Having a rule that is both left- and right- recursive is always ambiguous, but in this case the fix is obvious -- change it to exp: exp ADD factor
, matching the rest of your rules.