Mismatched input

29 views
Skip to first unread message

Joey Ezechiëls

unread,
Nov 14, 2016, 10:15:39 AM11/14/16
to antlr-discussion
Hi everyone, 

I have a grammar that just won't work, so I have made a simple (hollowed out) version of it, see the attachment. 
Even that version won't work, on input 'xquery version" 3.0"' (without the single quotes)  it tells me:
Line 1:0 mismatched input 'xquery version' expecting 'xquery'. 

The error as it is does not make any sense to me, can anyone clarify what it is trying to tell me and how do I fix it? 
DSC_0004.JPG

Kevin Cummings

unread,
Nov 14, 2016, 4:09:08 PM11/14/16
to antlr-di...@googlegroups.com
On 11/14/16 10:15, Joey Ezechiëls wrote:
> Hi everyone,
>
> I have a grammar that just won't work, so I have made a simple (hollowed
> out) version of it, see the attachment.
> Even that version won't work, on input 'xquery version" 3.0"' (without
> the single quotes) it tells me:
> Line 1:0 mismatched input 'xquery version' expecting 'xquery'.

You have no whitespace rule. Certainly whitespace should be a token
separator in your grammar? So the lexer interprets the string 'xquery
version' as a single token instead of 2 separate tokens as you intend.

try adding a WS rule like the following:

WS : " " -> skip;

You can add more characters to this is necessary (\n, \r, \t, etc). I
think you will find that you get further into your parsing with this rule.

> The error as it is does not make any sense to me, can anyone clarify
> what it is trying to tell me and how do I fix it?
>
> --
> You received this message because you are subscribed to the Google
> Groups "antlr-discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to antlr-discussi...@googlegroups.com
> <mailto:antlr-discussi...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

--
Kevin J. Cummings
kjc...@verizon.net
cumm...@kjchome.homeip.net
cumm...@kjc386.framingham.ma.us
Registered Linux User #1232 (http://www.linuxcounter.net/)

Joey Ezechiëls

unread,
Nov 15, 2016, 1:29:29 AM11/15/16
to antlr-discussion
I see, indeed I have seen this around in examples on the web. But what is never made clear is how to inform ANTLR that that is the whitespace rule, surely including the WS rule in the grammar alone is not enough?

I wonder about this because I would prefer to not litter other rules with WS nonterminals, assuming I even have a choice there.

Kevin Cummings

unread,
Nov 15, 2016, 2:02:54 PM11/15/16
to antlr-di...@googlegroups.com
On 11/15/16 01:29, Joey Ezechiëls wrote:
> I see, indeed I have seen this around in examples on the web. But what is never made clear is how to inform ANTLR that that is the whitespace rule, surely including the WS rule in the grammar alone is not enough?

No need to "inform ANTLR". The fact that the tokens are being "skip"ed
should be sufficient. ANTLR tries to match the longest token it can
when tokenizing. The WS rule will cause an "end of token" when
appropriate. What you put in your WS rule depends on what is required
for your grammar. For example, you would not put \n or \r in it if
end-of-lines are significant in your grammar.... I have found that
skipping all whitespace in my grammars is appropriate (except in the
lexer when I'm doing pre-processor type stuff; Ter once had an option in
lexer rules to ignore whitespace "options {ignore=WS;}", but he removed
it in later versions of ANTLR).

> I wonder about this because I would prefer to not litter other rules with WS nonterminals, assuming I even have a choice there.

In this case, WS becomes a terminal rule, and it is skipped, it is not
emitted into the token stream, so you do not have to have it in your
parser rules. It is used to only separate tokens. What complicates
things is when your grammar NEEDS to recognize WS, then it becomes a
pain to deal with.
Reply all
Reply to author
Forward
0 new messages