C struct and a bit of yacc

This is a discussion on C struct and a bit of yacc within the C Programming forums, part of the General Programming Boards category; I have the following code in yacc: Code: typedef struct data { int type; union { float f; char *str; ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    C struct and a bit of yacc

    I have the following code in yacc:

    Code:
    typedef struct data {
         int type;
         union {
            float f;
            char *str;
            int v;
         } d;
    } data;
    
    %}
    
    %union
    { 
       data *typ;  ==> error occurs here in the y.tab.h
       char* stringcon;
       char charcon;
       char *id;
    }
    
    %type <typ> expr
    however it gives me an error :

    y.tab.h:24: error: expected specifier-qualifier-list before ‚data‚

    Can someone tell me why?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    How about listening to and heeding to the words of your compiler. I see an error on line 22 that causes line 24 to become confused. Please just copy and paste code directly next time.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    1
    you have the same name for the typedef and the struct, thats name must be different.
    example
    Code:
      typedef struct strdata {...} data;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,639
    Quote Originally Posted by evaristoyoka
    you have the same name for the typedef and the struct, thats name must be different.
    No, that is fine. The typedef defines data as an alias for struct data.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21