Nick B. Nick B. - 11 months ago 71
C# Question

antlr grammar avoiding angle brackets

In this question I asked about extracting tags from arbitrary text. The solution provided worked well, but there's one edge case I'd like to handle. To recap, I'm parsing arbitrary user-entered text and would like to have any occurrence of

to conform to valid tag syntax. Where an angle bracket isn't part of a valid tag, it should be escaped as
. The syntax I'm looking for is
is text from a fixed list of entries and
is a number
. The parser:

parser grammar TagsParser;

options {
tokenVocab = TagsLexer;

parse: (tag | text)* EOF;
text: NOANGLE;
fixedlist: FOO | BAR | BAZ;

The lexer:

lexer grammar TagsLexer;

LANGLE: '<' -> pushMode(tag);
NOANGLE: ~[<>]+;

mode tag:

RANGLE: '>' -> popMode;
FOO: 'foo';
BAR: 'bar';
BAZ: 'baz';
ID: [0-9]+;

This works well and successfully parses text such as:

Hi <bar#987>!
if 1 &lt; 2

It also successfully fails the following when I use the

if 1 < 2

The last one successfully fails because
enters the
mode and it doesn't match a supported tag format. However, I would also like to avoid instances of
in the text as well, so the following should fail as well:

if 2 > 1

That text should be specified as
if 2 &gt; 1
instead of having the raw angle bracket.

How can I modify the grammar so that occurrences of
which aren't part of a valid tag fail to parse?

Answer Source

As your grammar stands now, it will fail > outside of a tag with token recognition error, because > doesn't appear in the lexer grammar outside of the tag mode. That's a failure all right as it is. But if you insist on failing during parse, then just add right angle to the lexer's default mode:

lexer grammar TagsLexer;

LANGLE: '<' -> pushMode(tag);
NOANGLE: ~[<>]+;

mode tag;

RANGLE: '>' -> popMode;

Then > outside of a tag will fail during parse.