0

I do know that this question has been asked a lot of times. I am trying to build a grammar using ANTLR.

Predicate           : LOWERCASE | Predicate VarChars ;

VarChars            : LOWERCASE | UPPERCASE;

fragment LOWERCASE  : [a-z] ;   

fragment UPPERCASE  : [A-Z] ;

I am getting the following error :"The following sets of rules are mutually left-recursive [Predicate]"

Please show me how this is fixed. How to remove the mutual left recursion in my antlr grammar.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
Susha Suresh
  • 65
  • 1
  • 1
  • 8
  • what are you trying to achieve? is your intention that a predicate must always start with a small letter? could you give an example of some valid/invalid tokens? the technical reason of the error message is of course that Predicate occurs as its own rule alternative. – Cee McSharpface Nov 07 '17 at 09:02
  • So This is a part of Datalog Grammar. pA would be valid PA would be invalid I got the mutually left recursive error for many cases. How do I fix the error? How can I use Predicate in its own rule alternative – Susha Suresh Nov 07 '17 at 18:51
  • Be aware that rules starting with a capital letter are lexer rules. To avoid confusion, the practice is to give an all uppercase name. What you probably want is : `PREDICATE : LOWERCASE ( LOWERCASE | UPPERCASE )* ;`. And you must provide another rule for input like `PA`, or you'll have token recognition errors. – BernardK Nov 07 '17 at 19:53

1 Answers1

0

Get rid of the recursive occurrence of "Predicate" altogether. VarChars alone is sufficient to mean lowercase or uppercase. Use the + suffix to indicate "one or more instances":

fragment LOWERCASE : [a-z];   
fragment UPPERCASE : [A-Z];

VARCHARS  : LOWERCASE | UPPERCASE;
PREDICATE : LOWERCASE VARCHARS+;

Applied to your examples, this would disqualify "p" and "PA" as predicates, and qualify "pA".

Observe how PREDICATE and VARCHARS are still lexer rules (as opposed to syntactical rules) as they describe how a lexeme is formed. Therefore it follows the all-uppercase naming convention (Antlr does not care, but it improves readability).

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77