1

I have a program where users want to be able to filter out certain String criteria using the format

(someType != 'a' AND someType != 'b') OR (anotherType = 'abc' AND somethingElse = 'cns')

We are looking into using ANTLR 4 for parsing this out. Each group will always be separated by an OR and each inner group will always be separated by ANDs.

I am a junior developer and I will learn ANTLR4 by reading the book if this is the route we want to go in. I just want to make sure ANTLR4 will take care of this.

We essentially want to know if the expression will evaluate to true or false based on this grammar.

Envin
  • 1,463
  • 8
  • 32
  • 69
  • As mentioned by Ira, ANTLR does not evaluate this for you. In your case, it might be safer/faster to use some Java integrated scripting instead (like Groovy: http://groovy.codehaus.org/JSR+223+Scripting+with+Groovy). How to evaluate with ANTLR, see this Q&A: http://stackoverflow.com/questions/15610183/if-else-statements-in-antlr-using-listeners – Bart Kiers Oct 15 '13 at 21:32

1 Answers1

3

Antlr doesn't evaluate expressions. It parses them.

"Evaluation" of the parsed result is up to you. Generally you attach node-building actions to grammar rules; with that, ANTLR will help you build a tree, and then you walk to the tree to evaluate it.

If you are really sneaky, you can likely do expression evaluation in the semantic actions. Passing values up is somewhat like passing created nodes up. Passing values down takes more effort, and I'm not the guy to describe how to do this with ANTLR.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • I suppose I do not understand the purpose then. What scenarios does parsing benefit from? Creating a compiler? – Envin Oct 15 '13 at 22:08
  • Any time one needs to process a complex structure of information, you have to first *read* that information. This is essentially what parsing does. Parser generators like ANTLR (and many others like it) are focused on parsing computer-program like texts (as opposed to English or CSV files) in which breaking the source into tokens, and recognizing how tokens composed into more complex structures [repeatedly]. This is really handy for calculator like things all the way up to C++. ... – Ira Baxter Oct 15 '13 at 23:44
  • .... Known well to people that do this professionally, building trees to represent the composed structure of the program is an really effective way to capture the parsed result in a form in which it can processed/analyzed/compiled (or in your case, "evaluated"). If you build such a tool, you will appreciate this a lot more. You can try to *avoid* building such a tool this way for your particular task, and you'll rediscover why people do it this way e.g., it is likely that your filters will get processed repeatedly; speed will matter, and then you'll find out this standard scheme works great. – Ira Baxter Oct 15 '13 at 23:46
  • ... the thing to understand is that ANTLR only solves the pure parsing part of problem. You have to add help ("create trees") to solve the *capture* part of the problem, so you can have the benefit of processing the captured results. A few other parser generators go a lot further. See my bio. – Ira Baxter Oct 15 '13 at 23:50
  • Thank you for that, I have a much better understanding of ANTLR now and will continue researching it. – Envin Oct 16 '13 at 18:45