gpt4 book ai didi

JavaCC 问题 - 生成的代码没有找到所有解析错误

转载 作者:行者123 更新时间:2023-11-29 08:19:10 26 4
gpt4 key购买 nike

刚开始接触 JavaCC。但是我有一个奇怪的行为。我想验证输入 int 形式的标记(字母和数字),它们与符号(+、-、/)连接并且可以包含括号。我希望这是可以理解的:)

在 main 方法中是一个字符串,应该会产生错误,因为它有一个左括号和两个右括号,但我没有得到解析异常 --> 为什么?

有人知道为什么我没有得到异常吗?

我在最初的尝试中一直在左递归和选择冲突方面苦苦挣扎,但设法克服了它们。也许我在那里介绍了问题?!

哦 - 也许我的解决方案不是很好 - 忽略这个事实......或者更好的是,提供一些建议;-)

文件:代码解析器.jj

 options {
STATIC=false;
}

PARSER_BEGIN(CodeParser)

package com.testing;

import java.io.StringReader;
import java.io.Reader;

public class CodeParser {

public CodeParser(String s)
{
this((Reader)(new StringReader(s)));

}

public static void main(String args[])
{
try
{
/** String has one open, but two closing parenthesis --> should produce parse error */
String s = "A+BC+-(2XXL+A/-B))";
CodeParser parser = new CodeParser(s);
parser.expression();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
PARSER_END(CodeParser)

TOKEN:
{
<code : ("-")?(["A"-"Z", "0"-"9"])+ >
| <op : ("+"|"/") >
| <not : ("-") >
| <lparenthesis : ("(") >
| <rparenthesis : (")") >
}

void expression() :
{
}
{
negated_expression() | parenthesis_expression() | LOOKAHEAD(2) operator_expression() | <code>
}

void negated_expression() :
{
}
{
<not>parenthesis_expression()
}

void parenthesis_expression() :
{
}
{
<lparenthesis>expression()<rparenthesis>
}

void operator_expression() :
{
}
{
<code><op>expression()
}

编辑 - 2009 年 11 月 16 日

现在我尝试了 ANTLR。

我更改了一些术语以更好地匹配我的问题域。我想出了以下代码(使用本网站上的答案),它现在似乎可以完成工作:

grammar Code;

CODE : ('A'..'Z'|'0'..'9')+;
OP : '+'|'/';

start : terms EOF;
terms : term (OP term)*;
term : '-'? CODE
| '-'? '(' terms ')';

顺便说一句...ANTLRWORKS 是一个很棒的调试/可视化工具!对我帮助很大。

附加信息
上面的代码匹配如下内容:

(-Z19+-Z07+((FV+((M005+(M272/M276))/((M278/M273/M642)+-M005)))/(FW+(M005+(M273/M278/M642)))))+(-Z19+-Z07+((FV+((M005+(M272/M276))/((M278/M273/M642/M651)+-M005)))/(FW+(M0))))

最佳答案

kgregory 说的是正确答案。如果您使用 DEBUG_PARSER 选项构建语法然后运行它,您可以看到这一点:

$ javacc -debug_parser -output_directory=com/testing/ CodeParser.jj && javac com/testing/*.java && java -cp . com.testing.CodeParserJava Compiler Compiler Version 5.0 (Parser Generator)(type "javacc" with no arguments for help)Reading from file CodeParser.jj . . .File "TokenMgrError.java" is being rebuilt.File "ParseException.java" is being rebuilt.File "Token.java" is being rebuilt.File "SimpleCharStream.java" is being rebuilt.Parser generated successfully.Call:   expression  Call:   operator_expression    Consumed token: <<code>: "A" at line 1 column 1>    Consumed token: <<op>: "+" at line 1 column 2>    Call:   expression      Call:   operator_expression        Consumed token: <<code>: "BC" at line 1 column 3>        Consumed token: <<op>: "+" at line 1 column 5>        Call:   expression          Call:   negated_expression            Consumed token: <"-" at line 1 column 6>            Call:   parenthesis_expression              Consumed token: <"(" at line 1 column 7>              Call:   expression                Call:   operator_expression                  Consumed token: <<code>: "2XXL" at line 1 column 8>                  Consumed token: <<op>: "+" at line 1 column 12>                  Call:   expression                    Call:   operator_expression                      Consumed token: <<code>: "A" at line 1 column 13>                      Consumed token: <<op>: "/" at line 1 column 14>                      Call:   expression                        Consumed token: <<code>: "-B" at line 1 column 15>                      Return: expression                    Return: operator_expression                  Return: expression                Return: operator_expression              Return: expression              Consumed token: <")" at line 1 column 17>            Return: parenthesis_expression          Return: negated_expression        Return: expression      Return: operator_expression    Return: expression  Return: operator_expressionReturn: expression

看到了吗?最后消耗的 token 是倒数第二个字符 - 倒数第二个右括号。

如果您想要异常(exception),再次像 kgregory 所说的那样,您可以添加一个名为"file"或“数据”或其他名称的新顶级产品,并以 token 结尾。这样任何像这样的悬空括号都会导致错误。这是执行此操作的语法:

options {  STATIC=false;}PARSER_BEGIN(CodeParser)package com.testing;import java.io.StringReader;import java.io.Reader;public class CodeParser {    public CodeParser(String s)     {        this((Reader)(new StringReader(s)));     }    public static void main(String args[])    {        try        {              /** String has one open, but two closing parenthesis --> should produce parse error */              String s = "A+BC+-(2XXL+A/-B))";              CodeParser parser = new CodeParser(s);              parser.file();        }        catch(Exception e)        {              e.printStackTrace();        }    }}PARSER_END(CodeParser)TOKEN:{        <code : ("-")?(["A"-"Z", "0"-"9"])+ >        | <op : ("+"|"/") >        | <not : ("-") >        | <lparenthesis : ("(") >        | <rparenthesis : (")") >}void file() : {} {  expression() <EOF>}void expression() :{}{        negated_expression() | parenthesis_expression() | LOOKAHEAD(2) operator_expression() | <code>}void negated_expression() :{}{      <not>parenthesis_expression()}void parenthesis_expression() :{}{       <lparenthesis>expression()<rparenthesis>}void operator_expression() :{}{      <code><op>expression()}

和一个样本运行:

$ javacc -debug_parser -output_directory=com/testing/ CodeParser.jj && javac com/testing/*.java && java -cp . com.testing.CodeParserJava Compiler Compiler Version 5.0 (Parser Generator)(type "javacc" with no arguments for help)Reading from file CodeParser.jj . . .File "TokenMgrError.java" is being rebuilt.File "ParseException.java" is being rebuilt.File "Token.java" is being rebuilt.File "SimpleCharStream.java" is being rebuilt.Parser generated successfully.Call:   file  Call:   expression    Call:   operator_expression      Consumed token: <<code>: "A" at line 1 column 1>      Consumed token: <<op>: "+" at line 1 column 2>      Call:   expression        Call:   operator_expression          Consumed token: <<code>: "BC" at line 1 column 3>          Consumed token: <<op>: "+" at line 1 column 5>          Call:   expression            Call:   negated_expression              Consumed token: <"-" at line 1 column 6>              Call:   parenthesis_expression                Consumed token: <"(" at line 1 column 7>                Call:   expression                  Call:   operator_expression                    Consumed token: <<code>: "2XXL" at line 1 column 8>                    Consumed token: <<op>: "+" at line 1 column 12>                    Call:   expression                      Call:   operator_expression                        Consumed token: <<code>: "A" at line 1 column 13>                        Consumed token: <<op>: "/" at line 1 column 14>                        Call:   expression                          Consumed token: <<code>: "-B" at line 1 column 15>                        Return: expression                      Return: operator_expression                    Return: expression                  Return: operator_expression                Return: expression                Consumed token: <")" at line 1 column 17>              Return: parenthesis_expression            Return: negated_expression          Return: expression        Return: operator_expression      Return: expression    Return: operator_expression  Return: expressionReturn: filecom.testing.ParseException: Encountered " ")" ") "" at line 1, column 18.Was expecting:    <EOF>   at com.testing.CodeParser.generateParseException(CodeParser.java:354)  at com.testing.CodeParser.jj_consume_token(CodeParser.java:238)  at com.testing.CodeParser.file(CodeParser.java:34)  at com.testing.CodeParser.main(CodeParser.java:22)

瞧!一个异常(exception)。

关于JavaCC 问题 - 生成的代码没有找到所有解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1721534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com