2015-02-07 29 views
0

在YACC当我这样做添加定义类型%工会YACC

%union {char *str;int num;} it work (yytypes) 

的probleme是,当我加入

%union{EXPR_ARBRE tree;int num;} 

注:EXP_ARBRE是type.h 声明的指针类型,包括在yac.y

type.h但我编译时得到我的EXPR_ARBRE类型未知

0错误

为什么?任何人都可以给我一些解释吗?

这是type.h

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 

typedef struct _EXPR_ARBRE { 
enum EXPR_TYPE { 
     UNAIRE,BINAIRE,VARIABLE,CONSTANTE,TERNAIRE 
    }type; 
    union { 
    int val ; 
    char *nom; 
    struct { 
     char op; 
     struct _EXPR_ARBRE *u; 
    }un; 
    struct{ 
     char op; 
     struct _EXPR_ARBRE *opg; 
     struct _EXPR_ARBRE *opd; 
    }bin; 
    struct{ 
     struct _EXPR_ARBRE *test; 
     struct _EXPR_ARBRE *alternv; 
     struct _EXPR_ARBRE *alternf; 
    }ter; 
}forme; 
}*EXPR_ARBRE; 

////////////////////////////////// //////////// 我YACC文件(注:我使用lex和我没有删除我的个人原因,主要功能)

%token NUMBRE IDENTIF SI ALORS SINON POUR FAIRE APPEL TANTQUE RETOUR INFEG SUPEG EGAL DIFFERENT PLUS SUB MULT DIV OPAR CPAR AFFECT OACOL CACOL OBR CBR PNT PV VERG INF SUP 
%token ENTIER TABLEAU FONCTION OU ET ECRIRE LIRE 
%{ 
    void yyerror(char *); 
    #include<stdio.h> 
    #include<stdlib.h> 
    #include"type.h" 
%} 
%union {EXP_ARBRE arbre; int entier; char caractere; char *chaine;} 
%% 
programme: declarationVariable declarationTableau declarationFonction PNT   { printf("\t |\n\t |\n ____PROG____\n"); } 
     | declarationVariable PNT   { printf("\t |\n\t |\n ____PROG____\n"); } 
     | declarationTableau PNT   { printf("\t |\n\t |\n ____PROG____\n"); } 
     | declarationFonction PNT   { printf("\t |\n\t |\n ____PROG____\n"); } 
     | declarationVariable declarationTableau PNT   { printf("\t |\n\t |\n ____PROG____\n"); } 
     | declarationVariable declarationFonction PNT   { printf("\t |\n\t |\n ____PROG____\n"); } 
     | declarationTableau declarationFonction PNT   { printf("\t |\n\t |\n ____PROG____\n"); } 
     ; 

declarationVariable: declarationVariable ENTIER IDENTIF PV { printf("\t |\n\t |\n Decl-Variable\n"); } 
      | ENTIER IDENTIF PV { printf("\t |\n\t |\n Decl-Variable\n"); } 
      ; 



declarationTableau: declarationTableau TABLEAU IDENTIF OBR NUMBRE CBR PV { printf("\t |\n\t |\n Decl-Tab\n"); } 
      | TABLEAU IDENTIF OBR NUMBRE CBR PV { printf("\t |\n\t |\n Decl-Tab\n"); } 
      ; 

declarationFonction: declarationFonction FONCTION IDENTIF OPAR param CPAR declarationVariable instructionBloc { printf("\t |\n\t |\n Decl-FONC\n"); } 
      | FONCTION IDENTIF OPAR param CPAR declarationVariable instructionBloc { printf("\t |\n\t |\n Decl-FONC\n"); } 
      ; 

param: param VERG IDENTIF 
    | IDENTIF 
     | 
    ; 

instructionBloc:OACOL instr CACOL { printf("\t |\n\t |\n inst-Block\n"); } 
      | OACOL CACOL { printf("\t |\n\t |\n inst-Block\n"); } 
     ; 

instr : instr instruction 
    | instruction 
; 


instruction : instructionAffect 
     | instructionBloc 
     | instructionSi 
     | instructionTantque 
     | instructionAppel 
     | instructionRetour 
     | instructionEcriture 
     | instructionVide 
     ; 


instructionAffect : IDENTIF AFFECT expression PV { printf("\t |\n\t |\n inst-Affect\n"); } 
      | IDENTIF OBR expression CBR AFFECT expression PV { printf("\t |\n\t |\n inst-Affect\n"); } 
      ; 


instructionSi : SI expression ALORS instruction { printf("\t |\n\t |\n inst-Si\n"); } 
      ; 

instructionTantque : TANTQUE expression FAIRE instruction { printf("\t |\n\t |\n inst-TantQue\n"); } 
      ; 

instructionAppel : APPEL IDENTIF OPAR finAppelFonction CPAR PV { printf("\t |\n\t |\n inst-Appel\n"); } 
     ; 

finAppelFonction : expression 
     | finAppelFonction VERG expression 
     ; 

instructionRetour: RETOUR expression PV { printf("\t |\n\t |\n inst-Retour\n"); } 
     ; 

instructionEcriture : ECRIRE OPAR expression CPAR PV { printf("\t |\n\t |\n inst-Ecriture\n"); } 
     ; 

instructionVide : PV { printf("\t |\n\t |\n inst-Vide\n"); }; 

expression : expression OU conjonction 
     | conjonction 
     ; 

conjonction : conjonction ET comparaison 
     | comparaison 
     ; 

comparaison : expArith 
     | expArith INF expArith 
     | expArith SUP expArith 
     | expArith EGAL expArith 
     | expArith DIFFERENT expArith 
     | expArith INFEG expArith 
     | expArith SUPEG expArith 
     ; 

expArith : expArith PLUS terme 
    | expArith SUB terme 
    | terme 
     ; 

terme : terme MULT facteur 
     | terme DIV facteur 
     | facteur 
    ; 

facteur : OPAR expression CPAR 
    | NUMBRE 
    | LIRE OPAR facteur CPAR 
    | IDENTIF OPAR finAppelFonction CPAR 
    | IDENTIF OBR expression CBR 
    | IDENTIF 


%% 



extern int yylex(); 
extern int yyparse(); 
extern FILE *yyin; 

void yyerror(char *s) { 
    printf("%s\n", s); 
} 
+0

我复制了你的'type.h',并制作了一个包含它的小型yacc文件,并在'%union'中使用'EXPR_ARBRE',就像你展示的那样,它工作正常。也许你正在从其他地方捡起一个不同的'type.h'?如果你也显示你的yacc文件,它也许会有帮助。 – 2015-02-07 22:13:15

+0

我确实添加了我的yacc文件,可以让我看看你的例子 – Okba 2015-02-07 22:36:46

回答

1

的类型被定义为EXPR_ARBRE但你%union定义改为使用EXP_ARBRE