11

I previously posted about my first attempt at using ANTLR when I was having issues with left recursion.

Now that I have resolved those issues, I am getting the following error when I try to use org.antlr.v4.Tool to generate the code:

error(99): C:test.g4::: grammar 'test' has no rules

What are the possible reasons for this error? Using ANTLRWorks I can certainly see rules in the Parse Tree so why can't it see them? Is it because it cannot find a suitable START rule?

Felix Bembrick
  • 233
  • 2
  • 7
  • Well, if you resolved your earlier problem, could you either post the answer/solution there, or remove the question? – Bart Kiers Jun 13 '13 at 06:19
  • Can you post the grammar, and post exactly how you're trying to generate the parser/lexer classes using the `Tool`? – Bart Kiers Jun 13 '13 at 06:20
  • You may have a syntax error in the file which is causing ANTLR to stop parsing. Can you post your grammar up through (and including) the first rule? – Sam Harwell Jun 28 '13 at 17:40

7 Answers7

10

I think Antlr expects the first rule name to be in small case. I was getting the same error with my grammar

grammar ee;
Condition : LogicalExpression ;
LogicalExpression : BooleanLiteral ; 
BooleanLiteral :  True ;
True : 'true' ;

By changing the first production rule in the grammar to lower case it solved the issue i.e. the below grammar worked for me.

grammar ee;
condition : LogicalExpression ;
LogicalExpression : BooleanLiteral ; 
BooleanLiteral :  True ;
True : 'true' ;

Note: It is my personal interpretation, I couldn't find this reasoning in the online documentation.

Edit: The production rules should begin with lower case letters as specified in the latest docs [1]

[1] https://github.com/antlr/antlr4/blob/master/doc/lexicon.md#identifiers

vdua
  • 1,281
  • 1
  • 14
  • 24
  • 1
    I met the same issue. I think the names of grammar rules (aka the non-terminal rules) must be in all lower-case. – smwikipedia Dec 31 '16 at 07:02
2

I'm not sure if you've found the solution for this, but I had the same problem and fixed it by changing my start symbol to 'prog'. So for example, the first two lines of your .g4 file would be:

    grammar test;
    prog : <...> ;

Where <...> will be your first derivation.

Markoorn
  • 2,477
  • 1
  • 16
  • 31
1

I just got that error as well (antlworks 2.1). switching from RULE : THIS | THAT ; to rule : this | that ; for parser rules (i.e. from uppercase to lowercase) solved the problem!

EDIT The above correction holds only for RULE , what follows after the : can be any combination of lexer/parser rules

Dio
  • 348
  • 2
  • 12
0

The most likely cause is just what the error message suggests. And the most likely reason for that is that you have not saved your grammar to the file--or if you're using ANTLRWorks2--ANTLRWorks hasn't saved your work to the file. I have no idea why ANTLRWorks doesn't save reliably.

Roy
  • 51
  • 5
  • With your remark *"I have no idea why ANTLRWorks doesn't save reliably"*, you're assuming it's ANTLWorks that is malfunctioning and not the OP. I bet it's the other way around. – Bart Kiers Jun 28 '13 at 17:38
  • I said ANTLRWorks doesn't save reliably because it does it to me, which is how I knew a likely answer to the OP's question. – Roy Jun 29 '13 at 07:09
  • Your reasoning is incorrect. It _is_ **an** answer, and had I seen it when I first encountered error(99) it would have been _the_ answer. Given that the file save problem is easily reproducible in ANTLRWorks2 it is even--as I said--a likely answer. BTW, only I have mentioned ANTLRWorks2. Are we even talking about the same software? Clarify. – Roy Jun 30 '13 at 08:02
0

I also got the same error but could not fix it. I downloaded antlrworks-1.4.jar and it's working perfectly.

Download >> antlrworks-1.4.jar

Cjo
  • 1,265
  • 13
  • 19
0

Changing the first rule to start with a lower case character worked for me.

Chet
  • 1
  • 1
  • 1
0

I had the same problem, and this means that your grammar has no Syntactic rules. So in order to avoid this error, you need to write at least one Syntactic rule.