antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
10.09k stars 3.69k forks source link

Parsing error in the PDDL grammar #1347

Open JoanEspasa opened 5 years ago

JoanEspasa commented 5 years ago

Hello, I am relatively new to ANTLR so please bear with me.

I'm trying to parse a valid PDDL file and it seems the given grammar has some kind of error I cannot seem to find

line 3:13 mismatched input 'at' expecting NAME
line 8:18 mismatched input 'at' expecting NAME
line 8:25 mismatched input '?a' expecting {'(', NAME, NUMBER}

A minimal input that reproduces the error:

(define (domain foo)
(:types car place)
(:functions (at ?x - car) - place)

(:action move 
 :parameters (?a - place ?c - car)
 :precondition ()
 :effect (assign (at ?c) ?a)))

If it is of any use, the "official" BNF can be found here: https://helios.hud.ac.uk/scommv/IPC-14/repository/kovacs-pddl-3.1-2011.pdf

I'm using the latest stable antlr4 (4.7.2). I have tried generating Java and Python code, but it outputs the same error.

JoanEspasa commented 5 years ago

Thanks to a user in stackoverflow, I found why this happens: https://stackoverflow.com/questions/54163775/parsing-error-with-the-default-antlr4-grammar-for-the-pddl-language For all the literal keywords inside parser rules (like 'at', 'begin', 'end', ...) ANTLR will create tokens for behind the scenes. So the input at will always be tokenised as an AT token, never as a NAME token.

But there should be no restriction about the predicates or functions name, so this could be fixed by adding the literal explicitly like this:

functionSymbol
   : NAME
   | 'at'
   ;

I'm not sure though if the keywords should be added explicitly to the grammar.