[英]How can I create a method that generates Strings that match for this regular expression?
I have these regular expression: 我有以下正则表达式:
M := (M)
M := n
These expressions can generate strings such as: ((((n)))), n, (n), etc. 这些表达式可以生成字符串,例如:[[[[[n)))),n,[n)等。
I have to create a method that can check if a string is part of the grammar, it doesn't necessarily has to be a code, it is more like the step before you start coding 我必须创建一个方法来检查字符串是否是语法的一部分,它不一定必须是代码,它更像是开始编码之前的步骤
class Sintax {
Lexic l = new Lexic();
int p = 0;
void M(){
if(lexicalSImbol == 'n')
{
if(p != 0)
checkParentesis();
else
checkEndOfString();
}
else if(lexicalSimbol == '(')
{
p++;
M();
}
else
error();
}
void checkParentesis(){
int i = 1;
while(i <= p)
{
if(l.nextSimbol() == ')')
i++;
else
error();
}
}
}
But I am not sure if the method is able to deliver all of the strings that the grammar can generate, or if I can change it in some way to make it more efficient. 但是我不确定该方法是否能够传递语法可以生成的所有字符串,或者我是否可以通过某种方式对其进行更改以提高其效率。
checkParentesis
has to end with checkEndOfString();
checkParentesis
必须以checkEndOfString();
结尾checkEndOfString();
for an erroneous input "(n))"
. 错误输入"(n))"
。 So the code can be reduced: 因此可以减少代码:
void M(){
if(lexicalSImbol == 'n')
{
//checkParentesis();
while (p > 0 && l.nextSimbol() == ')')
--p;
checkEndOfString();
}
else if(lexicalSimbol == '(')
{
p++;
M();
}
else
error();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.