English 中文(简体)
What s wrong with this yacc file?
原标题:
  • 时间:2009-11-16 01:07:04
  •  标签:
  • yacc

When I run yacc -d parser.y on the following file I get the following errors:

parser.y:23.3-24.4: warning: unused value: $4
15 rules never reduced
parser.y: warning: 7 useless nonterminals and 15 useless rules
parser.y:16.1-14: fatal error: start symbol statement_list does not derive any sentence
make: *** [y.tab.c] Error 1

I m particularly concerned about how to get rid of the fatal error.

%{
#include "parser.h"
#include <string.h>
%}
%union {
double dval;
struct symtab *symp;
}
%token <symp> NAME
%token <dval> NUMBER
%type <dval> expression
%type <dval> term
%type <dval> factor
%%

statement_list: statement  
 
| statement_list statement  
 
;
statement: NAME  =  expression { $1->value = $3; }
| expression { printf("= %g
", $1); }
;
expression: expression  +  term { $$ = $1 + $3; }
| expression  -  term { $$ = $1 - $3; }
term
;

term: term  *  factor { $$ = $1 * $3; }
| term  /  factor { if($3 == 0.0)
yyerror("divide by zero");
else
$$ = $1 / $3;
}
| factor
;
factor:  (  expression  )  { $$ = $2; }
|  -  factor { $$ = -$2; }
| NUMBER
| NAME { $$ = $1->value; }
;
%%
/* look up a symbol table entry, add if not present */
struct symtab *symlook(char *s) {
char *p;
struct symtab *sp;
for(sp = symtab; sp < &symtab[NSYMS]; sp++) {
/* is it already here? */
if(sp->name && !strcmp(sp->name, s))
return sp;
if(!sp->name) { /* is it free */
sp->name = strdup(s);
return sp;
}
/* otherwise continue to next */
}
yyerror("Too many symbols");
exit(1); /* cannot continue */
} /* symlook */
yyerror(char *s)
{
printf( "yyerror: %s
", s);
}
最佳回答

All those warnings and errors are caused by the missing | before term in your expression rule. The hint is the unused $4 in a snippet that s plainly should only have 3 arguments. That problem cascades into all the others.

Change:

expression: expression  +  term { $$ = $1 + $3; }
| expression  -  term { $$ = $1 - $3; }
term
;

into:

expression: expression  +  term { $$ = $1 + $3; }
| expression  -  term { $$ = $1 - $3; }
| term
;

and try again.

问题回答

you forget the or | here

expression: expression  +  term { $$ = $1 + $3; }
| expression  -  term { $$ = $1 - $3; }
term
;

the last rule should be |term {};





相关问题
How to turn this into a parser

If I just add on to the following yacc file, will it turn into a parser? /* C-Minus BNF Grammar */ %token ELSE %token IF %token INT %token RETURN %token VOID %token WHILE %token ID %token NUM %...

Bison/Yacc, make literal token return its own value?

Below is my rule, when i replace $2 with = my code works. I know by default all literal tokens uses their ascii value (hence why multi character token require a definition) The below doesnt work. ...

What s wrong with this yacc file?

When I run yacc -d parser.y on the following file I get the following errors: parser.y:23.3-24.4: warning: unused value: $4 15 rules never reduced parser.y: warning: 7 useless nonterminals and 15 ...

How to find shift/reduce conflict in this yacc file?

When I try to use yacc on the following file I get the error conflicts: 1 shift/reduce How can I find and fix the conflict? /* C-Minus BNF Grammar */ %token ELSE %token IF %token INT %token RETURN %...

Deriving a state machine from a BNF grammar

I am trying to put together a proof of concept of an XSS-safe string interpolation scheme. Given a string with substitutions, "Hello <b>$planetoid</b>!" I want break it into literal ...

热门标签