[英]ANTLR4 grammar parser issue
I am relatively new to ANTLR so bear with me pls. 我对ANTLR来说还比较陌生,所以请多多包涵。
I have the following imitation of a grammar for parsing very simple first-order logic formulas: 我对语法进行了以下模仿,以解析非常简单的一阶逻辑公式:
grammar graph;
/*------------------------------------------------------------------
* PARSER RULES
*------------------------------------------------------------------*/
input
:
TRUE | FALSE | formula | EOF
;
formula
:
(element)+ ST condition
;
element
:
quantifier IN domain
;
condition
:
atom EQUALS (assignment | atom)
;
atom
:
variable DOT property
;
quantifier
:
(FOREACH | EXISTS) variable
;
domain
:
(GRAPH_A | GRAPH_B)
;
variable
:
(NODE | EDGE)
;
property
:
(COLOR | VALUE)
;
assignment
:
(COLORTYPE | NUMBER)
;
/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
COLORTYPE : ('a'..'z')+ ;
NUMBER : ('0'..'9')+ (DOT ('0'..'9')+)? ;
WS : [ \t\r\n]+ -> skip ;
EXISTS : 'Exists' ;
FOREACH : 'Foreach' ;
TRUE : 'True' ;
FALSE : 'False' ;
ST : '->' ;
NODE : 'node' ;
EDGE : 'edge' ;
IN : 'in' ;
GRAPH_A : 'GraphA' ;
GRAPH_B : 'GraphB' ;
COLOR : 'color' ;
VALUE : 'value' ;
EQUALS : '=' ;
DOT : '.' ;
The grammar is pretty straightforward. 语法非常简单。 I was able to generate the lexer and parser classes with
我能够用生成词法分析器和解析器类
java org.antlr.v4.Tool graph.g4
but when I try to parse the following expression 但是当我尝试解析以下表达式时
Exists node in GraphA -> node.color = 'red'
I end up with the following error: 我最终遇到以下错误:
line 1:38 token recognition error at: ''' 1:38行令牌识别错误:“''
line 1:42 token recognition error at: ''' 第1:42行令牌识别错误,位于:“''
No method for rule r or it has arguments 没有规则r的方法或它具有参数
What is the meaning of rule 'r'? 规则“ r”是什么意思? How can I understand where the problem is coming for?
我怎么知道问题出在哪里? Any help will be much appreciated!
任何帮助都感激不尽!
Move COLORTYPE last; 最后移动COLORTYPE; it also matches the keywords.
它也与关键字匹配。 ANTLR resolves ambiguities to the rule mentioned first.
ANTLR解决了前面提到的规则的歧义。
The problem is COLORTYPE
matches the input red
, but you actually specified 'red'
. 问题是
COLORTYPE
匹配输入red
,但是您实际上指定了'red'
。 You need to do one of the following: 您需要执行以下操作之一:
red
in your input. red
周围的引号。 Add quotes to your COLORTYPE
rule: 在您的
COLORTYPE
规则中添加引号:
COLORTYPE : '\\'' [az]+ '\\'';
When I make the following changes to your grammar, it works for me: 当我对您的语法进行以下更改时,它对我有用:
Move COLORTYPE to the end, since as others have mentioned, it matches before your keywords. 将COLORTYPE移到末尾,因为正如其他人提到的那样,它在关键字之前匹配。
Change your 'condition' rule to: 将您的“条件”规则更改为:
atom EQUALS QUOTE? 原子等于报价? (assignment | atom) QUOTE?
(分配|原子)QUOTE?
Add this at the end: 最后添加:
QUOTE : '\\'' ; 引用:“ \\”;
i am a bit late i think, but 我想我有点晚了,但是
"No method for rule r or it has arguments"
“规则r没有方法或它具有参数”
this is produced because you are calling something like this 这是因为您正在调用类似这样的东西
C:\>grun graph r -gui
you should instead use 你应该改用
C:\>grun graph input -gui
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.