fatal error in Bison : start symbole can't derive any sentence

697 views Asked by At
 %token ENTIER REEL VECTOR INTEGER FLOAT CONST READ DISPLAY IF ELSE FOR END AND OR NOT G L GE LE EQ DI ACOUV AT
 %token ACFER AFFEC PLUS MOIN MUL DIV CROOUV CROFER PAROUV PARFER SEP VERG PVERG DEUXPT ESPACE ID CAR CHCAR STRING

 %start S 
 %%
S: ID ACOUV DEC CODE ACFER;    

J'ai ce message qui apparait lorse que je fait bison -d prog.y :

fatal error: start symbol S does not derive any sentence

2

There are 2 answers

3
Chris Dodd On

bison -d on your input gives me:

test.y:6.17-20: symbol CODE is used, but is not defined as a token and has no rules
test.y:6.13-15: symbol DEC is used, but is not defined as a token and has no rules

which tells you exactly what the problem is -- you're using CODE and DEC and not defining them. Add them to one of the %token lines and it works fine...

edit

The error 'start symbol S does not derive any sentence' is telling you that you have unbounded recursion in your grammar, so that no (finite) input can match the start symbol. In you case, S must include a CODE, which must include a command (directly or via a listcommand), which must contain a boucle, which must in turn contain another listcommand. So you end up with an infinite expansion chain of listcommands -> command -> boucle -> listcommands.

The problem is probably your rule

listcommands: command | listcommands ;

which matches exactly one command, plus a useless (and ambiguous) unbounded noop-expansion of that command. You probably want

listcommands: /*epsilon*/ | listcommands command ;

which matches 0 or more commands in sequence. Making this change fixes the fatal error, but still leaves a bunch of shift-reduce conflicts, along with the useless rule dectype: dectype.

To track down and fix shift/reduce conflicts, use bison -v to produce a .output file listing the grammar, states and conflicts in detail. Most of yours come from not having a precedence for NOT, and the other two come from the ambiguous dectype and CODE rules.

0
Abderrahmane BECHIKH On

this is my code :

 %{#include<stdio.h>
extern FILE* yyin;    
extern tab;    
%}
%union
{char *name;
int num;
 char car;
 }
 %token ENTIER REEL VECTOR INTEGER FLOAT CONST CHAR READ DISPLAY IF ELSE FOR END AND OR NOT G L GE LE EQ DI ACOUV AT
 %token ACFER AFFEC PLUS MOIN MUL DIV CROOUV CROFER PAROUV PARFER SEP VERG PVERG DEUXPT ESPACE ID CAR CHCAR STRING
 %left OR 
 %left AND
 %left G GE EQ DI LE L 
 %left PLUS MOIN 
 %left MUL DIV 
 %start S
 %%
S: ID ACOUV DEC CODE ACFER {printf("Chaine correcte !");YYACCEPT;};
DEC: ACOUV dectype ACFER;
dectype: dectype | type DEUXPT listidf PVERG | VECTOR DEUXPT type DEUXPT ID CROOUV ENTIER VERG     ENTIER CROFER PVERG;
listidf: ID SEP listidf | ID;
type: INTEGER STRING CHAR FLOAT CONST ; 
CODE:ACOUV command ACFER | ACOUV listcommands ACFER;
listcommands: command | listcommands;
command: affichage lecture affectation condition boucle;
affichage: DISPLAY PAROUV CHCAR DEUXPT ID PARFER PVERG;
lecture: READ PAROUV CHCAR DEUXPT AT ID PARFER PVERG;
affectation: ID AFFEC expression PVERG;
expression: expression1 | expressioncond | ID |CONST | PAROUV expression PARFER; 
expressioncond: expression G expression | expression L expression | expression GE expression | expression EQ expression | expression LE expression | expression DI expression |NOT expression | expression OR expression | expression AND expression;
expression1: expression MOIN expression | expression PLUS expression | expression DIV expression | expression MUL expression ;
condition: IF PAROUV expressioncond PARFER listcommands ELSE listcommands END  | IF PAROUV     expressioncond PARFER listcommands END | IF PAROUV expressioncond PARFER condition END;
boucle: FOR PAROUV ID DEUXPT ENTIER conditiondarret PARFER listcommands END;
conditiondarret:ID;
 %%
 int yyerror(char* msg)
 {printf("%s",msg);
  return 1;}
 int main(int argc,char *argv[]) {
     yyin=fopen(argv[1],"r");
     yypase();
     affiche();
     return 0;
 }