I am using Bison on a mac and i seem to be getting the error below. I cant seem to solve this problem. Any idea as to why I am getting a syntax error from the code displayed below?
Terminal output when ran:
syntax error, unexpected identifier, expecting string
bison -d parser.y
parser.y
%{
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define YYDEBUG 1
extern FILE * yyin;
FILE * outputFile;
int yyerror(const char* s);
int yylex(void);
%}
%define parse.error verbose
%token PROGRAM BEGIN_WORD END VAR PRINT INTEGER STRING NUM ID
%%
start : PROGRAM pname ';' VAR decList ';' BEGIN_WORD statList END { YYACCEPT; }
;
pname : ID { fprintf(outputFile, "#include <iostream>\nUsing namespace std;\nint main()\n{\n"); }
;
decList : dec ':' type
;
dec : ID ',' dec
| ID
;
statList : stat ';'
| stat ';' statList
;
stat : print
| assign
;
print : PRINT '(' output ')'
;
output : STRING ',' ID
| ID
;
assign : ID '=' expr
;
expr : term
| expr '+' term
| expr '-' term
;
term : term '*' factor
| term '/' factor
| factor
;
factor : ID
| NUM
| '(' expr ')'
;
type : INTEGER
;
%%
int main (void)
{
yyin=fopen("input.txt","r+");
if(yyin==NULL)
{
return(printf("error: failed to open file\n"));
}
else
{
outputFile = fopen("abc13.cpp","w");
return(yyparse());
}
return 0;
}
int yyerror(const char * s)
{
return(fprintf(stderr, "%s\n", s));
}
Not sure if the problem is the version of Bison I have, or if its because Im on a mac.
Version:
bison 3.0.4_1
define.error
to do? Bison on Mac is fairly old. – Jonathan Leffler