0

Here is my top of yacc file.y

%code requires {

    struct Id {
        char *var;
    };

    struct Commds;

    struct Commd {
        struct Id lhs;
    };

    struct Commds {
        struct Commd commd;
        struct Commds *next;
    };    
}

I used this code in my %union to defined new types for parser.

%union {
    char *id;    
    long long integer;  
    struct Id Identifier;
    struct Commd Command;
    struct Commds *Commands;         
}
....
%type <Command> command
%type <Commands> commands

I have no problem in using it with $$-dollars attributes while parse tree is building up while evaluating tokens from my lexer. Unfortunately I would like to use my structures which are defined in the beginning of the file in other methods in %{ codes %} section. Unfortunately whenever I define function like this:

void add(struct Commd cmd) {...}; 

I am getting an error: unknown type! I would be grateful for telling me how to make this structs visible to my entire parser.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Dago
  • 788
  • 2
  • 9
  • 24
  • There seems to be one closing brace too many in your block of struct definitions. You may want to look into that first. – Ruud Helderman Jan 17 '16 at 22:10
  • checked it it is not the case. I think the problem is connected with y.tab.h – Dago Jan 17 '16 at 22:33
  • I can't reproduce this. Can you just insert a minimal example (less than 20 lines, if possible) which exhibits the problem? (Just the code blocks would be ideal.) – rici Jan 17 '16 at 22:51

1 Answers1

0

This error occurs usually because you have your union, tokens and types:

%union {
    char *id;    
    long long integer;  
    struct Id Identifier;
    struct Commd Command;
    struct Commds *Commands;         
}
....
%type <Command> command
%type <Commands> commands

before C code in {% %} braces. You need to put this after {% %}. Basically you're saying that your terminal or non terminal has type struct Commd, but yacc doesn't know what struct Commd is because you included it underneath union code.

You didn't write whole code so I can only assume you've done this. If that is the case, this is your answer.

Aleksandar Makragić
  • 1,957
  • 17
  • 32