1
votes

I am trying to parse the following grammar, where Value can be any character up to the semicolon, but I cannot get it to work correctly:

grammar Test;

pragmaDirective : 'pragma' Identifier Value ';' ;

Identifier : [a-z]+ ;

Value : ~';'* ;

WS : [ \t\r\n\u000C]+ -> skip ;

When I test it with pragma foo bar;, I get the following error:

line 1:6 extraneous input ' ' expecting Identifier
line 1:11 extraneous input 'bar' expecting ';'
2

2 Answers

1
votes

Try this:

pragmaDirective : 'pragma' Identifier .*? ';' ;

and remove the Value rule. That should do the job.

And a recommendation: define lexer rules for your literals (like 'pragma') instead of defining them directly in the parser rules.

0
votes

The Value rule is much too greedy. Lexer rules try to match as much as possible, so for input like this: pragma mu foo;, the Value rule would match pragma mu foo. After all, that's zero or more characters other than a semicolon.

Value is not well suited to be used as a lexer rule. I suggest you rethink your approach. Perhaps create a parser rule value that matches an Identifier and perhaps other lexer rules. Hard to make a suggestion without seeing much of the "real" grammar (you probably posted a dumbed down version of the grammar you're working on).