0

I'm trying to use Irony to parse C99, and I found a grammar online to guide me.

I'm having difficulty with conflicts on declaration versus statement. The following rule fails to detect a pointer declaration with initializer.

blockItemList.Rule = MakePlusRule(blockItemList, blockItem);
blockItem.Rule = declaration | statement;

The type of line it's failing on would be:

MyType *x = foo();

When I remove labeledStatement and expressionStatement from statement's rule (both may start with identifier), this type of declaration is recognized correctly.

What's the best way to force Irony to exhaust the declaration rule before trying statement? Or, can I add to the grammar as Irony parses so that it can register MyType as a terminal rather than an identifier?

xtravar
  • 1,321
  • 11
  • 24

1 Answers1

1

I remember having similiar problems with function calls and identifiers. Don't think you've done something particularily wrong, it's just the way grammars work. You need to "fine-tune" it for Irony. As far I know, Irony is LALR(1) parser, eg looking only one symbol forward when doing decisions. This might mean that you need to do more work than just define the given grammar.

I had case where I had conflicts in my grammar and I fixed it by lowering the "precision" of grammar. The actual precision was later restored through AST nodes.

Ps, you can also:

Use Irony GrammarExplorer and see what conflicts your grammar has. You can sometimes fix the conflicts with PreferSHiftHere() or ReduceHere()

And few links that I think are interesting to read:

http://irony.codeplex.com/discussions/400830

http://irony.codeplex.com/discussions/80134

https://irony.codeplex.com/discussions/551074

Context-free grammar understanding is not enough - you have to know smth about parsing methods like LR, LALR(1), LL, etc. Irony is LALR(1), while Antlr is LL. Grammar rules should be fine-tuned for a specific method. Irony 'is insisting' on something 'wrong' means that it took one of two equally possible alternatives resulting from ambiguities (conflicts!) that it reports. So no point trying to parse smth before you fix the conflicts. To do this - read more about LALR grammars.

Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78