Bison grammar warnings

2019-07-31 03:50发布

问题:

I am writing a parser with Bison and I am getting the following warnings.

fol.y:42 parser name defined to default :"parse"
fol.y:61:  warning:  type clash ('' 'pred') on default action

I have been using Google to search for a way to get rid of them, but have pretty much come up empty handed on what they mean (much less how to fix them) since every post I found with them has a compilation error and the warnings them selves aren't addressed. Could someone tell me what they mean and how to fix them? The relevant code is below. Line 61 is the last semicolon. I cut out the rest of the grammar since it is incredibly verbose.

%union {
    char* var;
    char* name;
    char* pred;
}


%token <var> VARIABLE
%token <name> NAME
%token <pred> PRED

%%


fol:
        declines clauses {cout << "Done parsing with file" << endl;}
        ;
declines:
        declines decline
        |decline
        ;
decline:
        PRED decs
        ;

回答1:

The first message is likely just a warning that you didn't include %start parse in the grammar specification.

The second means that somewhere you have rule that is supposed to return a value but you haven't properly specified which type of value it is to return. The PRED returns the pred element of your union; the problem might be that you've not created %type entries for decline and declines. If you have a union, you have to specify the type for most, if not all, rules — or maybe just rules that don't have an explicit action (so as to override the default $$ = $1; action).

I'm not convinced that the problem is in the line you specify, and because we don't have a complete, minimal reproduction of your problem, we can't investigate for you to validate it. The specification for decs may be relevant (I'm not convinced it is, but it might be).

You may get more information from the output of bison -v, which is the y.output file (or something similar).



回答2:

Finally found it.

To fix this:

fol.y:42 parser name defined to default :"parse"

Add %name parse before %token

Eg:

%name parse
%token NUM

(From: https://bdhacker.wordpress.com/2012/05/05/flex-bison-in-ubuntu/#comment-2669)



标签: parsing bison