0
votes

I have this problem where my YACC file doesn't seem to be able to access types defined in my header file.

If I replace my header file with %code requires{ } it does recognize it but that is not really what I want.

My st.h header file:

struct node {
    int item;
    int identifier;
    struct node *left;
    struct node *middle;
    struct node *right;
};

typedef struct node NODE;
typedef NODE *TREE;

My parser.y file:

%{
#include <stdio.h>
#include <stdlib.h>
#include "st.h"
%}

%union {
    int value;
    TREE token;
}

Yacc (or C) gives me this error:

error: unknown type name ‘TREE’

I understand that this is most likely a mistake on my end and I would greatly appreciate any help.

1
Note: never ever typedef a pointer! And read How to Ask, provide all required information. It should be no problem to specify the exact error message, which it originates from and where it arises.too honest for this site
Is it yacc or C which gives the error? (Hint: which command was running when the error was produced?) And if it's C, does the error pertain to the file which yacc/bison generated or the file which (f)lex generated? (Hint: the error message will have a filename beside it.)rici
Also: minimal reproducible example. The code as presented compiles correctly.rici
Well, which is it? Yacc? Or C?user207421
You stated 'YACC (or C) gives me this error'. I am asking you which it is. Not what file the code is in. An error message is produced by a program, and that program is either yacc(1) or the C compiler. Which is it?user207421

1 Answers

4
votes

You're (likely) getting this error when trying to compile some other source file (NOT your parser file) that has an #include "y.tab.h" in it. The problem being that since your %union uses types defined in st.h, you need to always #include "st.h" BEFORE #include "y.tab.h" in every file that wants to include the latter.