1
votes

I'm having trouble figuring out how to write a parser with bison.

In order to insert variables into my symbol table so that I can do some type-checking and other nonsense I need the variable name and type.

I'm particularly focusing on these lines:

%union {tokentype token;
        char *type;
        sPtr names; //stack pointer
       }
<%token definitions>
%token <token> ID ICONST
%type <type> stype type
%type <names> IDlist

vardcl          : IDlist ':' type
                ;

IDlist          : IDlist ',' ID
                | ID
                ;

type            : ARRAY '[' integer_constant RANGE integer_constant ']' OF stype { $$ = $8 }
                | stype { $$ = $1 }
                ;

stype           : INT { $$ = "INT" }
                | BOOLEAN { $$ = "BOOLEAN" }
                ;

If my grammar were like this:

vardcl          : ID ':' type
                ;

I could just do something like:

vardcl          : ID ':' type { SymbolTableInsert($1, $3); }
                ;

But instead my grammar looks like this:

vardcl          : IDlist ':' type
                ;

IDlist          : IDlist ',' ID
                | ID
                ;

So I'm trying to put each ID into a data structure (I already use a stack for the symbol table so I figured I may as well use that) but I keep getting incompatibility errors about the type of the $arguments and I'm not sure I'm even pushing everything I need either:

IDlist          : IDlist ',' ID { $$ = SCreate(CompareStrings); SPush($$, $3); }
                | ID { $$ = SCreate(CompareStrings); SPush($$, $1);
                ;

I get the error "incompatible type for argument 2 of SPush, expected void * not tokentype". I get the same error when trying to insert things into my symbol table as well.

What's a good way to do this?

Thanks in advance for any help.

1

1 Answers

1
votes

On the grammar side, everything looks ok. It looks like your problem is actually a problem with C: check the signature of your SPush function, it seems to be a function whose second argument is a void*, not tokentype (which is the declared type for ID).