简体   繁体   English

ANTLR 4是否允许我为布尔表达式创建解析器?

[英]Would ANTLR 4 allow me to create a parser for a boolean expression?

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') (someType!='a'和someType!='b')或(anotherType ='abc'AND somethingElse ='cns')

We are looking into using ANTLR 4 for parsing this out. 我们正在研究使用ANTLR 4来解析它。 Each group will always be separated by an OR and each inner group will always be separated by ANDs. 每个组将始终用OR分隔,每个内部组将始终用AND分隔。

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. 我是一名初级开发人员,如果这是我们想要进入的路线,我将通过阅读本书来学习ANTLR4。我只是想确保ANTLR4能够解决这个问题。

We essentially want to know if the expression will evaluate to true or false based on this grammar. 我们基本上想要知道表达式是否会根据此语法计算为true或false。

Antlr doesn't evaluate expressions. Antlr不评估表达式。 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. 有了它,ANTLR将帮助您构建一棵树,然后您走到树上进行评估。

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. 将值传递下来需要付出更多努力,而我不是用ANTLR来描述如何做到这一点的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM