4

please help a newbie to understand this warning in ANTLWorks:

[11:10:15] warning(138): BooleanExpr.g:0:1: grammar BooleanExpr: no start rule (no rule can obviously be followed by EOF)

This is how definition of my grammar looks like:

grammar BooleanExpr;
booleanExpr
    :   andExpr ('OR' andExpr)*;
andExpr :   notExpr (('AND' | ' ' ) notExpr)*;
notExpr :   kppExpr ('NOT' kppExpr);
kppExpr :   keywordExpr|phraseExpr|proximityExpr|'(' booleanExpr ')';
keywordExpr
    :CHAR+;
phraseExpr
    :   '"' keywordExpr '"';
proximityExpr
    :   keywordExpr|phraseExpr '~' INT;
CHAR    :   ('A'..'Z') | ('a'..'z');
INT :   '0'..'9'+;

Thanks a lot!

Seki
  • 11,135
  • 7
  • 46
  • 70
Ihor M.
  • 2,728
  • 3
  • 44
  • 70

2 Answers2

3

Any grammar needs so-called start rule. Start rule is a rule that is not referenced by another rule. If your grammar does not have such rule, ANTLR generator will issue a warning:

no start rule (no rule can obviously be followed by EOF)

To avoid it, add a dummy start rule to your grammar:

start_rule: someOtherRule; 
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
Sanjiv
  • 1,795
  • 1
  • 29
  • 45
2

Normally parsers are written to parse an entire input (from the beginning to the end of a file). Grammars used for this task generally include a rule like the following.

compilationUnit : someElement* EOF;

This rule says a compilation unit is a sequence of zero-or-more elements ending at the end of the file. If the EOF reference is omitted, you could have a rule like the following.

compilationUnit : someElement*;

The problem with this form lies in its error handling ability. If the input contains a syntax error, then rather than trying to correct and/or recover from it, the rule will simply return (an empty sequence is a valid interpretation of this compilationUnit rule, so that is preferred to a longer sequence containing an invalid element).

ANTLRWorks is informing you that the grammar does not contain a rule ending with an explicit EOF reference, which can be problematic if you are intending to parse complete files.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • I do not plan to parse files, my intent here is validate boolean expressions that will be stored as a String objects. It doesn't make sense for me to append EOF in the end of the rule. How error handling could be supported in this case? – Ihor M. Jun 11 '13 at 16:25
  • If you plan you parse an entire string, then treat it like a file and add `EOF` to the end of the start rule. – Sam Harwell Jun 11 '13 at 18:22
  • Alright, should i append EOF symbol to the end of the String as well? – Ihor M. Jun 11 '13 at 19:50
  • @IhorM. No. `EOF` simply means the end of the input, which in your case is the end of the string. – Sam Harwell Jun 11 '13 at 22:26