7

I need to use flex and bison to parse some code.

The default type of YYSTYPE is int, even though I never declared it that way. Is that a default from bison?

It would help me a lot to pass strings back. I read this: How to solve Bison warning "... has no declared type" This looks like a good approach. (I don't need the full power of a union yet, just the char* part, but I might as well use the union because it might be helpful later.)

It's not working for me. I get these errors:

y:111.37-38: $1 of `ConstExpression' has no declared type
y:113.34-35: $1 of `ConstFactor' has no declared type
y:114.35-36: $1 of `ConstFactor' has no declared type
y:119.34-35: $1 of `Type' has no declared type
y:109.23-48: warning: type clash on default action: <str> != <>
y:115.23-27: warning: type clash on default action: <str> != <>
[...more of the same snipped...]

Here are the declarations from my y grammar file:

%union {
     char *str;
 }

%type<str> ConstExpression ConstFactor Type

Here is one line from my .l file:

[a-zA-Z]+[a-zA-Z0-9]*   { yylval.str = strdup(yytext); return yident;}

What else do I need to do to resolve the errors?

Community
  • 1
  • 1
naokoken
  • 71
  • 1
  • 2
  • 1
    Do you seriously think people can debug your grammar file without seeing it? –  Nov 22 '09 at 11:25
  • 8
    @Kinopiko, did you seriously just downvote this because he didn't just paste his whole homework in and say "Show me teh codez"? Would it have killed you to just have said "I need more information; please post the whole grammar file" or some such? – steveha Nov 22 '09 at 20:01
  • 1
    @Kinopiko, when he posts the information you have requested, will you undo your downvote? That just seems harsh. – steveha Nov 22 '09 at 20:01
  • steveha: Do you seriously think we can debug the grammar file without seeing it? I don't. I think the downvote for this question is justified. If you want to discuss this, perhaps we can bring it up on meta.stackoverflow.com. –  Nov 23 '09 at 03:19
  • 3
    @Kinopiko, full disclosure here: this is a friend of mine, and I encouraged him to come here for help with this problem. So, I happen to know that he was trying to edit down the amount of information posted in order to be courteous, and not just dump the whole contents of his homework directory here and say "do my homework for me". Okay, so it's not enough information, so he needs to post some more. I still think you were needlessly harsh. If you really want to talk on meta, I guess we could. I don't have an account there but I'll make one if you like. – steveha Nov 23 '09 at 07:05
  • 1
    A good way to reference the complete source without ballooning the question is to click the entire file into http://pastebin.com. or http://pastie.org/ or some other such service, and then include the URL you get. – DigitalRoss Nov 25 '09 at 02:30
  • 1
    At the very least, you need to include the lines with the errors (so lines 111, 113, 114, 119, 109, and 115) of the .y file if you expect anyone to be able to help. Ideally, post a [SSCCE](http://sscce.org) that demonstrates the problem – Chris Dodd Mar 17 '13 at 04:13

2 Answers2

6

Actually, I'm pretty sure I know what's wrong without seeing the grammar.

It's necessary to declare the types of terminal symbols as well, as they can and often do return semantic value. Think about things like ID and NUMBER, which have yyval data. But yacc doesn't have any way to know whether it's just an 'x' or something more, and the default action in a rule is: $$ = $1

For example, the following grammar makes it through yacc just fine, but try removing one more more of the symbols from the %type below:

%union {
  char *s;
}

%type <s> r1 r2 'x'

%%

r1: r2;

r2: 'x'   { printf("%s\n", $1); };
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
3

you probably just need to define types for your tokens in the yacc file

%token<str> yident
Chris Dodd
  • 2,920
  • 15
  • 10
  • I think this is right - If your return a char* in the lex file, you don't use %type, which is for bison types... – alternative Aug 19 '10 at 18:30