Okba
Okba

Reputation: 3

Adding defined type to %union in yacc

in yacc when i do

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

the probleme is when i add

%union{EXPR_ARBRE tree;int num;}

Note : EXP_ARBRE is a pointer type declared in type.h and type.h included in yac.y

but when i compile i get the error that my EXPR_ARBRE is an unknown type

why ? can any one give me some explanation ?

this is 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;

////////////////////////////////////////////// my yacc file (Note : i use lex and i did delete my main function for personal reason )

%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);
}

Upvotes: 0

Views: 644

Answers (1)

Steve Vinoski
Steve Vinoski

Reputation: 20024

The type is defined as EXPR_ARBRE but your %union definition uses EXP_ARBRE instead.

Upvotes: 1

Related Questions