# Strange bison 'parse error'

This topic is 4601 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I don't know whether or not this is the right section for such a question, but I couldn't see any better. I have been working on a Bison (yacc) grammar for my gcc front and and I'm not sure what I did, but a short while ago I added some (seemingly unrelated) things, and started getting a parse error on every construct. I stripped down the test file to just one simple 'import' construct, and still got "parse error at import". So I began removing things from the grammar until I was left with just about nothing, and then moved the import construct up into the very top of the parse tree, and removed the name list that usually goes long with it so that the 'import' keyword should've made it printf IMPORT!:

/* General Stuff */

name_list:		tok_ident
|		name_list '.' tok_ident
;

/* Top of the tree */

treetop:	tok_import 	{printf("IMPORT!");}
;


But I still got the parse error! So out of desperation I took out the seemingly useless name_list rule, and it worked. This was very strange so I tried pasing it back in in the reverse order and then putting the name_list back into the grammar to be sure it wasn't somehow flawed:
	/* Top of the tree */

treetop:	tok_import name_list	{printf("IMPORT!");}
;

/* General Stuff */

name_list:		tok_ident
|		name_list '.' tok_ident
;


And now it works perfectly. I have no idea what's going on here, anyone else have one? Thanks, ~SPH NOTE: I'm still getting parse errors when I went back to my original grammar, even when I tried moving the name_list rules around a bit :-p. Seems like there's something that I'm missing here.

##### Share on other sites
Quote:
 Original post by ShmeeBegekI don't know whether or not this is the right section for such a question, but I couldn't see any better.

Depending on how your grammar file is laid out, how you have your tokens set up, and how the tree of the sub rules (if you have any) is constructed chances are even though 'import' is defined in the grammar the parse has no idea what to do with it. From what i've read in the draft of the language imports look something like:

import module.module.module.*;

In which case you'll need to create a rule tree similar to:

treetop: import ;import: tok_import import_ident ';' ;import_ident: ident | import_ident '.' '*' | ident '.' import_ident ;

The problem with Bison is that if you haven't already laid the grammer out in BNF or EBNF you're likely to run into problems. another thing that you'll need to look out for is getting your sub trees arranged in such a way that you start encountering one or more nondeterminism situations. This is a very very unholy beast to deal with.

On a side note, I recently switched to using ANTLR which is far superior to Bison in SO many ways. Not only does it automatically (and optionally) build an abstract syntax tree but you can specify rules that don't generate nodes, rewrite nodes, or generate custom node subtrees. When I switched to ANTLR it took 13 days to create an entire grammar and another 3-4 days to do adjustments and cleanup.

##### Share on other sites

As I said I had stripped away most of the grammar, which indeed had complex constructs for this. I was just feeding it the 'input' token. I think that I've resolved the problem though, which had to do with a naming conflict and I think the layout of the grammar.

Thanks though, I'll check out ANTLR, ~SPH

##### Share on other sites
Quote:
 Original post by ShmeeBegekAs I said I had stripped away most of the grammar, which indeed had complex constructs for this. I was just feeding it the 'input' token. I think that I've resolved the problem though, which had to do with a naming conflict and I think the layout of the grammar.

ANTLR is an excellent package but if you've gotten yourself familiar wiht Bison the additional functionality can take some getting used to. When I went back and started cleaning up the grammar I found a lot of places where I was doing something totally unnecessary because I was still in YACC mode. If you have specific questions about ANTLR feel free to pm me.