带占位符的简单java递归下降解析库 [英] Simple java recursive descent parsing library with placeholders

查看:196
本文介绍了带占位符的简单java递归下降解析库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个应用程序,我想用算术表达式和变量解析一个String。想象一下这个字符串:

For an application I want to parse a String with arithmetic expressions and variables. Just imagine this string:

((A + B) * C) / (D - (E * F))

所以我这里有占位符,没有实际的整数/双精度值。我正在搜索一个库,它允许我获得第一个占位符,将(通过数据库查询)一个值放入占位符并继续下一个占位符。

So I have placeholders here and no actual integer/double values. I am searching for a library which allows me to get the first placeholder, put (via a database query for example) a value into the placeholder and proceed with the next placeholder.

所以我基本上想要做的是允许用户用他们的域语言编写一个字符串,而不知道变量的实际值。因此,应用程序将根据某些上下文逻辑提供数值,并输出计算结果。

So what I essentially want to do is to allow users to write a string in their domain language without knowing the actual values of the variables. So the application would provide numeric values depending on some "contextual logic" and would output the result of the calculation.

我用Google搜索并找不到任何合适的库。我找到了ANTLR,但我认为它对我的用例来说非常重量级。有什么建议吗?

I googled and did not find any suitable library. I found ANTLR, but I think it would be very "heavyweight" for my usecase. Any suggestions?

推荐答案

你是对的,ANTLR有点矫枉过正。但是,使用中缀表示法解析算术表达式并不难,请参阅:

You are right that ANTLR is a bit of an overkill. However parsing arithmetic expressions in infix notation isn't that hard, see:

  • Operator-precedence parser
  • Shunting-yard algorithm
  • Algorithms for Parsing Arithmetic Expressions

还有你应该考虑使用一些脚本语言,如Groovy或JRuby。此外,JDK 6还提供内置的JavaScript支持。请在此处查看我的答案:使用Java创建元语言

Also you should consider using some scripting languages like Groovy or JRuby. Also JDK 6 onwards provides built-in JavaScript support. See my answer here: Creating meta language with Java.

这篇关于带占位符的简单java递归下降解析库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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