Trouble with Warnings While Writing a Parser in Yacc and Bison

0
11
Asked By CodeGuru123 On

I'm working on a parser using Yacc and Bison for a C-like language, and I've run into a strange warning: "rule useless in parser due to conflicts" for my empty rule in the `globaldecarray`. Here's the code for the relevant rules:

```yacc
globaldec: EXTERN basictype globaldecarray ID SEMICOLON
{ $$ = ASTglobaldec($3, $2,$4); } ;

globaldecarray: SQUARE_BRACKET_L ID ids SQUARE_BRACKET_R
{ $$ = ASTids($3, $2); }
|
{ $$ = NULL; };
```

Interestingly, I have other rules defined like the following that work fine without any warnings:

```yacc
fundef: funheader CURLY_BRACKET_L funbody CURLY_BRACKET_R
{ $$ = ASTfundef($1, $3, true, false); }
| EXPORT funheader CURLY_BRACKET_L funbody CURLY_BRACKET_R
{ $$ = ASTfundef($2, $4, true, true); } ;

funbody: fundef
{ $$ = ASTfundef($1, NULL, true, false); }
| vardecs fundefs stmts
{ $$ = ASTfunbody($1, ASTfundefs(NULL, $2, true), $3); }
|
{ $$ = ASTfunbody(NULL, NULL, NULL); };
```

I'm unsure why I'm getting this warning and how to address it, especially since similar structures don't trigger the same message.

4 Answers

Answered By ParserPro99 On

Check your definition of `globaldecarray`; it sounds like the line with `$$ = NULL` could be throwing things off. Are you sure it should be optional? If a `globaldec` can either be a regular type or an array, that might be the source of the conflict!

SkepticalCoder -

Yeah, I think the optional part might be causing confusion in how the rules are being evaluated.

Answered By CodeNerd42 On

It's tough to diagnose without more context. What's defined as `basictype`? That might create a conflict with `globaldecarray`. Any specific error messages alongside the warning?

Answered By YaccNewbie On

I've not used Yacc or Bison extensively, but I believe the warning hints at shift/reduce conflicts. These occur when the parser has ambiguity between rules. Do you think `globaldecarray` might conflict with other definitions?

Answered By DebuggingDude On

You might see more warnings if you address the existing conflicts. If you're on Bison, try using the `-Wcounterexamples` option; it'll generate a token sequence for ambiguous parses and could help you troubleshoot this issue further.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.