gpt4 book ai didi

ANTLR4 Parser and Lexer Separatly(ANTLR4解析器和词法分析器分别)

转载 作者:bug小助手 更新时间:2023-10-24 22:14:01 25 4
gpt4 key购买 nike



Im working on a simple program using ANTLR4, but now I'm trying to have the lexer and the parser in different files, but when I run it, i got a bunch of errors :(

我正在使用ANTLR4编写一个简单的程序,但现在我试图将词法分析器和解析器放在不同的文件中,但当我运行它时,我得到了一堆错误:(


Errors:

错误:


warning(125): ExprParser.g4:6:12: implicit definition of token PLUS in parser
warning(125): ExprParser.g4:7:12: implicit definition of token TIMES in parser
warning(125): ExprParser.g4:8:9: implicit definition of token L_PAR in parser
warning(125): ExprParser.g4:8:17: implicit definition of token R_PAR in parser
warning(125): ExprParser.g4:8:25: implicit definition of token ID in parser
Exception in thread "main" java.lang.IllegalStateException: A lexer interpreter can only be created for a lexer or combined grammar.
at org.antlr.v4.tool.Grammar.createLexerInterpreter(Grammar.java:1319)
at org.antlr.v4.gui.Interpreter.interp(Interpreter.java:167)
at org.antlr.v4.gui.Interpreter.main(Interpreter.java:277)

My ExprParser.g4:

我的ExprParser.g4:


parser grammar ExprParser;
options { tokenVocab=ExprLexer; }
prog: e EOF ;
e: t eprime ;
eprime:
'+' t eprime
|
;
t: f tprime ;
tprime:
'*' f tprime
|
;
f:
'(' e ')'
| ID
;

My ExprLexer.g4:

我的ExprLexfor.g4:


lexer grammar ExprLexer;

PLUS : '+';
TIMES : '*';
L_PAR : '(';
R_PAR : ')';
NEWLINE: [\r\n]+ -> skip;
ID : [a-zA-Z_][a-zA-Z_0-9]* ;
WS : [ \t]+ -> skip;

Command: antlr4-parse ExprParser.g4 prog -gui
Input: a+b*c
Thanks for your help!

命令:antlr4-parse ExprParser.g4prog-gui输入:a+b*c谢谢你的帮助!


更多回答

The problem is that you are not calling the tool correctly. You must also include the lexer grammar on the command line because it is a split grammar. echo 'a+b*c' | antlr4-parse.exe ExprLexer.g4 ExprParser.g4 prog -gui. The answer below is wrong: you can use string literals in the parser grammar. But you can do that as long as there is a corresponding rule for that literal in the lexer grammar. You don't need to use PLUS in place of the literal. But, it's best if you be consistent (use PLUS, or use the literal '+').

问题是您没有正确调用该工具。您还必须在命令行中包括词法分析器语法,因为它是一种拆分语法。ECHO‘a+b*c’|antlr4-parse.exe ExprLexfor.g4ExprParser.g4prog-gui。下面的答案是错误的:您可以在解析器语法中使用字符串。但是,只要在词法分析器语法中存在与该文字相对应的规则,您就可以这样做。你不需要用加号来代替原文。但是,最好是保持一致(使用加号,或使用字面上的‘+’)。

优秀答案推荐

Well, the warnings are because for example, your parser rule for eprime includes the literal token '+'. But in your lexer, you've correctly create a token for PLUS. You just need to use it. So use PLUS in your parser grammar instead of the literal plus sign, and repeat for other explicit tokens in the parser grammar. All the "implicit definition" warnings will disappear when you've done this.

好的,警告是因为,例如,您对eprime的解析器规则包括文字标记‘+’。但是在您的词法分析器中,您已经正确地为plus创建了一个标记。你只需要用它就行了。因此,在解析器语法中使用加号而不是文字加号,并对解析器语法中的其他显式标记重复使用。当你这样做的时候,所有的“隐式定义”警告都会消失。


The idea is to have NO explicit tokens in the parser grammar if you're going to use separate grammars.

我们的想法是,如果您要使用单独的语法,那么解析器语法中不应该有显式标记。


I'll have to look at the error later...as I don't have ANTLR loaded here right now.

我将不得不在稍后查看错误...因为我现在没有在这里加载ANTLR。


更多回答

Thanks for your answer! Unfortunately, I still get errors: warning(125): ExprParser.g4:6:4: implicit definition of token PLUS in parser warning(125): ExprParser.g4:11:4: implicit definition of token TIMES in parser warning(125): ExprParser.g4:15:4: implicit definition of token L_PAR in parser warning(125): ExprParser.g4:15:12: implicit definition of token R_PAR in parser warning(125): ExprParser.g4:16:6: implicit definition of token ID in parser Exception in thread "main" java.lang.IllegalStateException: A lexer interpreter can only be created for a lexer or combined grammar.

谢谢你的回答!不幸的是,我仍然收到错误:警告(125):ExprParser.g4:6:4:解析器中令牌加的隐式定义警告(125):ExprParser.g4:11:4:解析器中令牌时间的隐式定义(125):ExprParser.g4:15:4:解析器中令牌L_PAR的隐式定义(125):ExprParser.g4:15:12:解析器中令牌R_PAR的隐式定义(125):ExprParser.g4:16:6:线程“main”中的解析器异常中令牌ID的隐式定义:只能为词法分析器或组合语法创建词法分析器解释器。

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