骰子表示法(递归下降解析实现):没有分隔符的扫描器
如果可能的话,我想使用 java Scanner 构建 骰子符号 的递归下降解析实现。我之前曾提出过有关它的问题,但我的要求似乎过于简单化了。所以我在这里提出这个全球性的请求。
我真的希望这对于 java.util.Scanner 类是可行的,但如果需要,我将编写自己的扫描仪。我现在想避免这种情况。
expression = { whitespace } , [ plusminus ] , roll , { plusminus , ( roll | number , { whitespace } ) } ;
roll = [ number ] , ( "d" | "D" ) , ( number | "%") , [ "-" ( "L" | "H" ) ] , { whitespace } ;
plusminus = ( "+" | "-" ) , { whitespace } ;
number = nonzerodigit , { digit } ;
digit = nonzero digit | "0" ;
nonzerodigit = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
whitespace = ? Java definition of a whitespace ? ;
所以事实上,我尝试编写以下代码:
Scanner s = new Scanner("1d6");
if (s.hasNextInt()) {
s.nextInt();
} else {
throw new java.text.ParseException();
}
但显然它一直失败。
另外,正如上一个问题中所建议的,我尝试了 findWithinHorizon 方法,但它确实找到了下一个模式,并且不会从我所在的位置进行检查。因此,如果不是我需要的字符串,我就无法“推回”该字符串...
因此,关于如何在这种情况下使用 java.util.Scanner 有什么建议吗?或者也许我必须编写自己的扫描仪?
I would like to build a recursive descent parsing implementation of the dice notation using the java Scanner if possible. I've previously opened a question about it , but it seemed that I had my requirements way too simplified. So I'm presenting here the request in its globality.
I really hope that this is feasible with the class java.util.Scanner, but if needed I will write my own scanner. I would like to avoid this one right now.
expression = { whitespace } , [ plusminus ] , roll , { plusminus , ( roll | number , { whitespace } ) } ;
roll = [ number ] , ( "d" | "D" ) , ( number | "%") , [ "-" ( "L" | "H" ) ] , { whitespace } ;
plusminus = ( "+" | "-" ) , { whitespace } ;
number = nonzerodigit , { digit } ;
digit = nonzero digit | "0" ;
nonzerodigit = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
whitespace = ? Java definition of a whitespace ? ;
So in fact, I've tried to write the following code:
Scanner s = new Scanner("1d6");
if (s.hasNextInt()) {
s.nextInt();
} else {
throw new java.text.ParseException();
}
But obviously it keeps failing.
Also as suggested in the previous question, I've tried the findWithinHorizon methods but it really finds the next pattern and doesn't check from where I am. So I can't "pushback" the string if it is not what I need...
So any suggestion on how I should use java.util.Scanner in this context? Or maybe I have to write my own scanner?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该使用 java.util.Scanner。看一下 JParsec 的设计。
You should not use java.util.Scanner. Take a look at the design of JParsec.
要使用语法,您应该使用 ANTLR。它是一个解析器生成器。根据您的语法,您将能够轻松生成解析器,并使用此类来解析您的表达式。
For working with grammars, you should use ANTLR. It's a parser generator. From your grammar, you'll be able to generate easily a parser, and use this class to parse your expression.