Search code examples
parsingcompiler-constructionbisonflex-lexer

GNU Bison outputting error "syntax error, unexpected string, expecting ="


I've been trying to compile my Bison code and it seems to be that something is wrong with my code and yet I just can't figure out why or where.

Here is my bison code, I am running GNU Bison 2.3 on OSX. The error I am receiving is:

romans.y:9.9-21: syntax error, unexpected string, expecting =

This is an error I do not appear to be receiving on my Linux machine but on the OSX machine

%{
// file created via echo
#  include <stdio.h>
#  include <stdlib.h>
int yyerror(char *s);
int yylex();
int yyparse();
%}
%output "roman.tab.c"
%token ARABIC_NUMERAL;
%token EOL
%%

calclist: /* nothing */ {}
| calclist arabic_numerals EOL { printf("%d\n", $2);  }
;

arabic_numerals: ARABIC_NUMERAL
    | ARABIC_NUMERAL { $$ = $$ + $2; }
    ;  

/* ones:
    |   ONE {$$ = 1;}
    |   ONE ONE {$$ = 2;}
    |   ONE ONE ONE {$$ = 3;}
    ;
fives:
    |   FOUR {$$ = 4;}
    |   FIVE {$$ = 5;}
    |   FIVE ones { $$ = 5 +$2;}
    ;
tens:
    |   TEN {$$ = 10;}
    |   TEN TEN { $$ = 20;}
    |   TEN TEN TEN { $$ = 30;}
    |   TEN fives { $$ = 10 + $2}
    |   NINE { $$ = 9}
    ;
fifties:
    |   FIFTY { $$ = 50;}
    |
    :*/

%% 

void yyerror(char *s)
{
  printf("error: %s\n", s);
  exit(0);
}

int
main()
{
//  yydebug = 1;
  yyparse();
  return 0;
}

I have based my code off a program given to me by my professor, which is the following. When I attempted to compile it myself, I have the exact same issue. Is it a problem with the version of bison on my system?

%{ 
#  include <stdio.h>
#  include <stdlib.h>
void yyerror(char *s);
int yylex();
int yyparse();
%}
%output "brackets.c"

%token OP CP N EOL
%%

calclist: /* nothing */ {}
| calclist expr EOL { printf("Input conforms to grammar\n");  }
;

//expr: N N N { }
//;

expr: OP expr CP 
 | N
 ;
%%
void yyerror(char *s)
{
  printf("error: %s\n", s);
}


int
main()
{
//  yydebug = 1;
  yyparse();
  return 0;
}

Solution

  • You should update your bison version. The one which comes by default on OS X is ancient and lacking many features.

    In that version (but not 2.4 or later) the syntax of the %output directive had an equals sign:

    %output="roman.tab.c"
    

    You could make that change but then your file won't work on your other machine, or on anybody else's machine, such as the ones at your school. You can also set the output filename when you run the bison command:

    bison -d -o roman.tab.c roman.y
    

    which avoids the need for the %output directive and will work on all versions of bison.

    But on the whole, upgrading is probably your best option.