用于将字符串解析为复杂布尔值的递归逻辑? [英] Recursive logic for parsing string into complex boolean?

查看:80
本文介绍了用于将字符串解析为复杂布尔值的递归逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定以前已经做过,只是找不到.

I'm sure this has been done before, I just can't find it.

我需要将(((A OR B)AND C)OR D"之类的东西变成数据库中的属性查询.具体来说,我正在使用Ruby Sequel.谁能给我指出一个例子或实用程序,或使我无法重新发明轮子的东西?

I need to turn something like, "((A OR B) AND C) OR D" into a database query for an attribute. Specifically I'm using Ruby Sequel. Can anyone point me at an example or utility or something that will keep me from reinventing the wheel?

推荐答案

您可以使用 ANTLR 定义语法并自动为这些类型的字符串生成一个Ruby解析器. ANTLR是解析器生成器,它使您可以为一种语言(例如您描述的布尔语言)定义语法.

You can define a grammar using ANTLR and automatically generate a Ruby parser for those type of strings. ANTLR is a parser generator and it allows you to define a grammar for a language (such as a the boolean language that you described).

解析后,您可以指定需要采取哪些操作来构建所需的数据结构(在您的情况下为捕获查询结构的树数据结构).

After parsing, you can specify what actions need to be taken to build the desired data structure (in your case a tree data structure that captures the structure of the query).

这不是一个特别的Ruby问题,因为ANTLR还可以为其他语言生成解析器.在您的情况下,它将生成一个Ruby解析器,您可以将其集成到应用程序中以解析字符串并生成所需的数据结构.

This is not particularly a Ruby problem as ANTLR can also generate parsers for other languages. In your case it would produce a Ruby parser that you can integrate into your application to parse the strings and to produce the data structure that you need.

这篇关于用于将字符串解析为复杂布尔值的递归逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆