Search code examples
cstructtypedefdefinition

Why is this struct unknown at compile time?


I am using Ox, bison and lex to write an attribute grammar. In my grammar, I use a symbol table to keep track of identifiers that are in scope.

The symbol table struct is defined in a parser.h header:

struct st_;
typedef struct st_
{
    char** names;
    void** vals;
    char** types;
    int len;
    int maxlen;
} ST;

Ox produces the files oxout.tab.h and oxout.tab.c which are the outputs of bison after checking the grammar.

This is the head part of my ox file:

%{
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "parser.h"

int yylex(void);
void yyerror(char *);
extern int line_number;

struct test {
    ST s;
};

ST add(ST table, char* name, void* val, char* type);
ST addMaybe(ST table, char* name, void* val, char* type);
ST addAll(ST old, ST new);
ST init_st();
bool containsT(ST table, char* name, char* type);
bool contains(ST table, char* name);

void oxerrorf(char* f, char* s);
%}

%start Start

%token INTERFACE END CLASS IMPLEMENTS VAR METHOD INT RETURN IF THEN ELSE
%token WHILE DO NOT AND NEW THIS _NULL EOC SCOL COL BRACL BRACR DOT COMMA 
%token ASSIGNMENT PLUS ASTERISK MINUS LT EQ DEC HEX ID NEWLINE

@attributes { ST st; } Program Interface Class MaybeIDs InterfaceContents
@attributes { ST st; } Types Members Stats Negations Term Exprs Expr
@attributes { ST st; } Additions Ands ArithOps MaybeElse
@attributes { char* yystrval; } ID
@attributes { ST st; char* name; } AbstractMethod Type Member Par Stat
@attributes { ST st; ST stUp; } Pars 


@traversal @preorder LRpre
@traversal PV

%%
// definitions...

this is transformed to the following oxout.tab.c:

#include <stdbool.h>
#include <stdlib.h>
#if defined(__sun) && defined(__SVR4)
#include <strings.h>
#else
#include <string.h>
#endif

/* Identify Ox output. */
#define YYYOX 1

#line 1 "littleparser.y"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "parser.h"

int yylex(void);
void yyerror(char *);
extern int line_number;

struct test {
    ST s;
};

ST add(ST table, char* name, void* val, char* type);
ST addMaybe(ST table, char* name, void* val, char* type);
ST addAll(ST old, ST new);
ST init_st();
bool containsT(ST table, char* name, char* type);
bool contains(ST table, char* name);

void oxerrorf(char* f, char* s);
#line 93 "oxout.y"

struct yyyT1
#line 42 "littleparser.y"
 { ST st; }
#line 98 "oxout.y"
;

struct yyyT2
#line 43 "littleparser.y"
 { ST st; }
#line 104 "oxout.y"
;

struct yyyT3
#line 44 "littleparser.y"
 { ST st; }
#line 110 "oxout.y"
;

struct yyyT4
#line 45 "littleparser.y"
 { char* yystrval; }
#line 116 "oxout.y"
;

struct yyyT5
#line 46 "littleparser.y"
 { ST st; char* name; }
#line 122 "oxout.y"
;

struct yyyT6
#line 47 "littleparser.y"
 { ST st; ST stUp; }
#line 128 "oxout.y"
;

Now when I compile this using

> gcc  parser.h oxout.tab.h lex.yy.c oxout.tab.c -o ag

gcc throws me the most curious error:

littleparser.y:42:4: error: unknown type name ‘ST’
   42 | @attributes { ST st; } Program Interface Class MaybeIDs InterfaceContents
      |    ^~
littleparser.y:43:4: error: unknown type name ‘ST’
   43 | @attributes { ST st; } Types Members Stats Negations Term Exprs Expr
      |    ^~
littleparser.y:44:4: error: unknown type name ‘ST’
   44 | @attributes { ST st; } Additions Ands ArithOps MaybeElse
      |    ^~
littleparser.y:46:4: error: unknown type name ‘ST’
   46 | @attributes { ST st; char* name; } AbstractMethod Type Member Par Stat
      |    ^~
littleparser.y:47:4: error: unknown type name ‘ST’
   47 | @attributes { ST st; ST stUp; } Pars
      |    ^~
littleparser.y:47:11: error: unknown type name ‘ST’
   47 | @attributes { ST st; ST stUp; } Pars
      |           ^~

The interesting this is this:

struct test {
    ST s;
};

ST add(ST table, char* name, void* val, char* type);
// and the other declarations

seem to compile fine;

however

struct yyyT1
#line 42 "littleparser.y"
 { ST st; }
#line 98 "oxout.y"
;

throws the unknown type name error.

Why is that? All these definitions seem to be in scope/defined at the time it is compiled. (Also when I look at the output of the preprocessor the contents of parser.h are inlined before their first usage.)

I am not extremely savvy with C so I have probably missed something quite obvious I guess. Sadly C Typedef - Incomplete Type did not help me finding the error;

I tried to define the struct inside the c file, move the typedef somewhere else, keeping it all in the h file, but nothing seems to work.

Where am I going wrong here?


Solution

  • As @Paul Hankin suggested, this wasn't an error coming from the file littleparser.y.

    After removing all #line .* definitions in all the generated c files and compiling it again, the following output

    lex.yy.c:574:4: error: unknown type name ‘ST’
      574 |  { ST st; }
          |    ^~
    lex.yy.c:580:4: error: unknown type name ‘ST’
      580 |  { ST st; }
          |    ^~
    lex.yy.c:586:4: error: unknown type name ‘ST’
      586 |  { ST st; }
          |    ^~
    lex.yy.c:598:4: error: unknown type name ‘ST’
      598 |  { ST st; char* name; }
          |    ^~
    lex.yy.c:604:4: error: unknown type name ‘ST’
      604 |  { ST st; ST stUp; }
          |    ^~
    lex.yy.c:604:11: error: unknown type name ‘ST’
      604 |  { ST st; ST stUp; }
          |           ^~
    

    suggested that the problem was linked to the lexer.y instead of the litteparser.y. After adding #include "parser.h" in lexer.y and recompiling it, everything worked fine.