简体   繁体   中英

Bison warning: Empty rule for typed nonterminal

I'm getting a warning I don't really understand from Bison.

warning: empty rule for typed non-terminal, and no action

it's for each of my non-terminal characters. The part I don't understand is that If I don't give them a type then I get compilation errors stating that all of the $ns are undefined. Here's the grammar section of my bison file.

%union {
  char *sval;
}

%token <sval> PLUS TIMES LPAREN RPAREN ID
%type  <sval> s e t f 
%%

s : e                   { cout << GetNonConstCharStar(std::string("(e ") + $1 + ")") << endl; }

e :                                     
    | e PLUS t          { $$ = GetNonConstCharStar(std::string("(e ") + $1 + ")" + " (PLUS " + $2 + ") " + "(t " + $3 + ")" ); }
    | t                 { $$ = GetNonConstCharStar(std::string("(t ") + $1 + ")"); }
    ;
t : 
    | t TIMES f         { $$ = GetNonConstCharStar(std::string("(t ") + $1 + ")" + " (TIMES " + $2 + ") " + "(f " + $3 + ")"); }  
    | f                 { $$ = GetNonConstCharStar(std::string("(f ") + $1 + ")"); }
    ;

f :  
    | LPAREN e RPAREN   { $$ = GetNonConstCharStar(std::string("(LPAREN \\")+ $1 + ") (e " + $2 + ") (RPAREN \\" + $3 + ")") ; }
    | ID                { $$ = GetNonConstCharStar(std::string("(ID ") + $1 + ")") ; }
    ;

%%
e :                                     
    | e PLUS t
    | t

Is e: | e PLUS t | t e: | e PLUS t | t e: | e PLUS t | t , that is, nothing or e PLUS t or t . Remove the first | .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM