Bison中的致命错误:启动符号不能得出任何句子

时间:2014-11-28 23:41:22

标签: bison semantic-analysis

 %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:

致命错误:开始符号S不会导出任何句子

2 个答案:

答案 0 :(得分:1)

输入中的

bison -d告诉我:

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

它可以准确地告诉您问题所在 - 您使用CODEDEC而不是定义它们。将它们添加到其中一个%token行,它可以正常工作......

修改

错误'开始符号S不会导出任何句子'告诉你你的语法中有无限递归,所以没有(有限)输入可以匹配起始符号。在您的情况下,S必须包含CODE,其中必须包含command(直接或通过listcommand),其中必须包含boucle,必须反过来包含另一个listcommand。所以你最终得到了listcommands - >的无限扩展链。 command - > boucle - > listcommands

问题可能是你的规则

listcommands: command | listcommands ;

只匹配一个命令,加上该命令的无用(且模糊)无界扩展。你可能想要

listcommands: /*epsilon*/ | listcommands command ;

按顺序匹配0个或更多command个。进行此更改可修复致命错误,但仍会留下一堆shift-reduce冲突以及无用的规则dectype: dectype

要跟踪并修复班次/减少冲突,请使用bison -v生成.output文件,详细列出语法,状态和冲突。您的大多数人来自NOT没有优先权,而其他两个来自不明确的dectypeCODE规则。

答案 1 :(得分:0)

这是我的代码:

 %{#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;
 }