0
votes

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.

1
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
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 alternativeSusha Suresh
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

1 Answers

0
votes

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).