0
votes

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;
}
2
It's always good to indicate which line the error is on, since bison tells you (and even tells you which column position).rici
@rici romans.y:9.9-21: syntax error, unexpected string, expecting =Eoin Dowling

2 Answers

2
votes

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.

0
votes

Note that updating Bison on macOS can be tricky. The default system Bison in the Xcode toolchain (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/bison) is at 2.3 as of 10.14 Mojave, which, as @rici explained, does not support the %output "brackets.c" syntax (it expects %output="roman.tab.c", thus the = reference in the error message).

In order to update Bison in a way that it is both in your path and in your compiler's path, you need to forcefully symlink it after installing it via Homebrew (Homebrew requires Java 8 specifically in order to install Bison):

brew cask install homebrew/cask-versions/adoptopenjdk8 # Homebrew Bison requires Java8
brew install bazel bison flex

# So that the system can find the new brew Bison instead of the old system Bison.
brew link bison --force
echo 'export PATH="/usr/local/opt/bison/bin:$PATH"' >> ~/.bash_profile
export LDFLAGS="-L/usr/local/opt/bison/lib"