0
 %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 没有导出任何句子

4

2 回答 2

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必须包含 a CODE,其中必须包含 a command(直接或通过 a listcommand),其中必须包含 a boucle,而后者又必须包含另一个listcommand。所以你最终会得到一个无限的扩展链listcommands-> command-> boucle-> listcommands

问题可能是你的规则

listcommands: command | listcommands ;

它完全匹配一个命令,加上该命令的无用(和模棱两可)无界 noop 扩展。你可能想要

listcommands: /*epsilon*/ | listcommands command ;

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

要跟踪和修复 shift/reduce 冲突,请使用bison -v生成.output详细列出语法、状态和冲突的文件。你的大部分来自没有优先级 for NOT,另外两个来自于模棱两可dectypeCODE规则。

于 2014-11-29T00:49:11.147 回答
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;
 }    
于 2014-12-05T17:39:52.330 回答