0

Hello I need help with antlr4 grammar. I have been trying to create a parser for Datalog grammar. This is just a small snippet of the whole code. Whatever I try to parse its being recognized as Uppercase or Lowercase. The predicate token is not being recognized

For example the following Code should parse

abc as abc-> predicate

But its being parsed as

a-> Lrr

b-> Lrr

c-> Lrr

Its being parsed similarly for the rest of my code. How do I fix it?

grammar D;



predicate           : Lrr | predicate varChars ;

varChars            : Lrr | Urr;

Lrr : LOWERCASE;

Urr: UPPERCASE;

fragment LOWERCASE  : [a-z] ;

fragment UPPERCASE  : [A-Z] ;

Where am I going wrong. Please help

Ajay Santhosh
  • 19
  • 1
  • 1
  • 5
  • You'll probably have an easier time if you define your token rules, so that "abc", "as" and "abc" are the tokens in your example. At the token level ambiguities are resolved by always preferring the longest match, which seems like what you want here. – sepp2k Nov 24 '17 at 15:34
  • How do I define token rules? – Ajay Santhosh Nov 24 '17 at 17:08
  • Token rules are the ones whose names start with capital letters (`Lrr` and `Urr` in your code). I'm saying you should define those to match more than a single letter. – sepp2k Nov 24 '17 at 17:26
  • About the same grammar as [here](https://stackoverflow.com/questions/47153634/antlr-grammar-mutually-left-recursive). `predicate` has no sense, it does the same as `predicate : LOWERCASE ( LOWERCASE | UPPERCASE )?`. Is predicate just any word starting with a lower letter ? – BernardK Nov 24 '17 at 21:13

0 Answers0