I am relatively new to ANTLR so bear with me pls.
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: '''
line 1:42 token recognition error at: '''
No method for rule r or it has arguments
What is the meaning of rule 'r'? How can I understand where the problem is coming for? Any help will be much appreciated!
'''
, i.e. a single quote in your input. You have nothing in your lexer/parser rules to handle a quoted string, i.e.'red'
. This would be an answer except I don't know enough about ANTLR4 to tell you how to solve the problem. You could change the input toExists node in GraphA -> node.color = red
without the quotes around "red" and that should parse OK.red
but not'red'
. The quotes in the rule are ANTLR metacharacters.=red
then I end up only withNo method for rule r or it has arguments