2
votes

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

1
What do you expect define.error to do? Bison on Mac is fairly old.Jonathan Leffler
If you suspect the problem has to do with your bison version, the least you could do is inclyde the version number in your question, no? It would also be cool to not remove the line number corresponding to the error.rici
Are you getting this running Bison on your grammar, or running your generated parser?user207421
I added the current version of Bison i am using.CoderChick

1 Answers

5
votes

Diagnosis

I've installed Bison 3.0.4-1 via HomeBrew (https://brew.sh/), and when I run it on your code, I get no warnings or errors. When I use /usr/bin/bison (the Apple-provided Bison 2.3), I get:

so-5022-1650.y:13.13-23: syntax error, unexpected identifier, expecting string.

I diagnose that you are accidentally using the system-provided Bison and not the Bison 3.0.4 from Brew (or wherever you got it from). Check your PATH settings.

I added a one-line comment before your code, so my line 13 corresponds to your line 12, which is the %define line. When I changed it to read

%define "parse.error" "verbose"

the Bison 2.3 compiled the code without error. It might even work, therefore. When I compile that older notation with Bison 3.0.4, I get the warning:

so-5022-1650.y:13.13-25: warning: %define variable 'parse.error' requires keyword values [-Wdeprecated]
     %define "parse.error" "verbose"
             ^^^^^^^^^^^^^

There is a %error-verbose directive that is nominally deprecated but it is still usable. Officially, it was replaced by %define parse.error verbose in 3.0.x. However, using %error-verbose works without warning with both Bison 2.3 and Bison 3.0.4.

Steps towards an answer

What do you expect %define parse.error verbose to do?

Bison on Mac is fairly old — bison (GNU Bison) 2.3. There were versions within the 2.x family up to 2.7.1, and the current version is 3.0.4.

In the Bison 3.0.4 documentation on %define, it notes that:

There are many features of Bison’s behavior that can be controlled by assigning the feature a single value. For historical reasons, some such features are assigned values by dedicated directives, such as %start, which assigns the start symbol. However, newer such features are associated with variables, which are assigned by the %define directive:

and continues to discuss what they are.

If you manage to find Bison 2.3 documentation, %define is not part of it. (My O'Reilly ebook on "Flex and Bison" doesn't cover it, for example — it describes Bison 2.4.1 and Flex 2.5.35.)

So, you'll probably do best to get hold of the Bison 3.0.4 and compile and install it (or download it, pre-built, from one of the Mac download sites). Alternatively, you'll need to revise the code to work with older versions of Bison, which will mean not using %define.


Poking around the NEWS for Bison 2.3, it appears that some %define options may have been supported; they were 'new in 2.2', or at least the notes for 2.2 mentions %define "global_tokens_and_yystype" "1" as a possibility. That is different syntax from what you are using. Looking at the Bison 3.0.4 NEWS file, it appears that parse.error was added in Bison 3.0 (not in Bison 2.7) — which is why you're running into trouble with Bison 2.3. The NEWS file uses the quote-less notation for this too.