简体   繁体   中英

Yacc union on struct type

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM