Mismatched input error

47 views
Skip to first unread message

RG

unread,
Aug 6, 2021, 4:58:11 PM8/6/21
to antlr-discussion
Hi there,

I defined the following grammar:

grammar Hello;

hello:  '{' pair '}';
pair: '"name"' ':' STRING;

STRING: '"' (~ ["\\\u0000-\u001F])* '"';
WS: [ \t\n\r] + -> skip;

It works when tested with the input { "name": "hello" }. It does not when parsing { "name": "name" }. The error is mismatched input '"name"' expecting STRING

What am doing wrong?

Thanks in advance

Mike Lischke

unread,
Aug 7, 2021, 12:47:52 PM8/7/21
to antlr-discussion
> I defined the following grammar:
>
> grammar Hello;
>
> hello: '{' pair '}';
> pair: '"name"' ':' STRING;

This implicitly defines "name" (including the double quotes) as a keyword.

>
> STRING: '"' (~ ["\\\u0000-\u001F])* '"';

And that keyword comes before the STRING rule in the grammar, so it is matched first.

> WS: [ \t\n\r] + -> skip;
>
> It works when tested with the input { "name": "hello" }. It does not when parsing { "name": "name" }. The error is mismatched input '"name"' expecting STRING


The input "name" (including the double quotes) will never be matched by the STRING rule and hence will never appear as string in the pair rule.

Print your token stream to the terminal to see which token values you get, when you have problems with tokens.

Mike
--
www.soft-gems.net

Reply all
Reply to author
Forward
0 new messages