user571138
user571138

Reputation:

Strange behaviour for comments in Antlr4 grammar

When adding a comment line under ID is ok, however adding one under WS, causes an error to be raised. Entire file Hello.g4 listed below.

/**
 * Define a grammar called Hello
 */
grammar Hello;
r  : 'hello' ID ;         // match keyword hello followed by an identifier

ID : [a-z]+ ;             // match lower-case identifiers
/**********************************************************************************************/

WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines

/**********************************************************************************************/

The output i get in the console is as below:

ANTLR Tool v4.4 (/tmp/antlr-4.4-complete.jar)
Hello.g4 -o /home/me/workspace/TestComment/target/generated-sources/antlr4 -listener -no-visitor -encoding UTF-8
error(50): Hello.g4:13:0: syntax error: '<EOF>' came as a complete surprise to me
1 error(s)

BUILD FAIL
Total time: 168 millisecond(s)

Running Eclipse Version: Neon.3 Release (4.6.3), Default ANTLR4 project.

Why should ANTLR4, care about a trailing comment line ?

Upvotes: 0

Views: 105

Answers (1)

GRosenberg
GRosenberg

Reputation: 6001

The ANTLR 4 grammar defines JavaDoc comments as optionally allowed as a header and on each rule. No rule follows the last 'comment line', so it is interpreted an invalid beginning of a rule.

Change your comment line to /*----*/ to avoid the error.

Upvotes: 0

Related Questions