2

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!

4
  • Note the errors point to ''', 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 to Exists node in GraphA -> node.color = red without the quotes around "red" and that should parse OK. Commented Jan 31, 2014 at 0:24
  • well i have COLORTYPE : ('a'..'z')+ ; which should be able to parse 'red'
    – Wosh
    Commented Jan 31, 2014 at 0:28
  • Not true. It will parse red but not 'red'. The quotes in the rule are ANTLR metacharacters. Commented Jan 31, 2014 at 0:29
  • well if I try =red then I end up only with No method for rule r or it has arguments
    – Wosh
    Commented Jan 31, 2014 at 0:30

4 Answers 4

2

The problem is COLORTYPE matches the input red, but you actually specified 'red'. You need to do one of the following:

  1. Remove the quotes around red in your input.
  2. Add quotes to your COLORTYPE rule:

    COLORTYPE : '\'' [a-z]+ '\'';
    
2

Move COLORTYPE last; it also matches the keywords. ANTLR resolves ambiguities to the rule mentioned first.

1
  • well, you need to move it but you also need to realize you are not matching a single quote anywhere. Probably will want this no? See Sam's entry. Commented Feb 1, 2014 at 2:25
1

When I make the following changes to your grammar, it works for me:

  1. Move COLORTYPE to the end, since as others have mentioned, it matches before your keywords.

  2. Change your 'condition' rule to:

    atom EQUALS QUOTE? (assignment | atom) QUOTE?

  3. Add this at the end:

    QUOTE : '\'' ;

0

i am a bit late i think, but

"No method for rule r or it has arguments"

this is produced because you are calling something like this

C:\>grun graph r -gui

you should instead use

C:\>grun graph input -gui

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.