[antlr-interest] Newbie Issues: MismatchedTokenExceptions and FailedSyntacticPredicates

Richard Clark rdclark at gmail.com
Fri Jul 13 00:29:06 PDT 2007


On 7/12/07, Benji Smith <benji at benjismith.net> wrote:

> But even on the simplest stuff ("indicator EMA[3]"), I get a
> MismatchedTokenException from the ANTLRWorks interpreter at the top
> level of the expression. I tried rewriting the grammar
> (VTExpression2.g) to use string literals in the rule definitions
> instead of tokens, and the error went away. (Why? Shouldn't the lexer
> & parser be identical at this point, whether the grammar uses tokens
> or string literals in the rules?)

I saw your tokens declared after the parser rules and beflre the lexer
rules. Folks normally define the tokens at the top of the file. That
might have been the problem, but I haven't tried digging through your
grammar and/or the generated code that deeply to see the real issue.

> I don't understand where all of these synpred objects come from. As
> far as I can tell, I'm never actually using a syntactic predicate.

You enabled backtracking which automatically inserts a syntatic
predicate in front of each choice. It's good for rapid prototyping,
but makes tracking down errors more difficult.

I think the emerging best practice is to try developing w/
backtracking off and without specifying a fixed lookahead, then
turning on backtracking and/or setting lookahead on a rule by rule
basis. (There's a wonderfully detailed discussion of this in the
book.)

> I feel a little silly grappling with errors parsing such simple little
> expressions. The stuff I used to do in JavaCC was significantly more
> complex than this. Is it just me, or do many other people find ANTLR a
> lot more difficult to use?

I struggled at first too, but less than with other compiler-building
tools. An artist friend of mine told me that when you pick up a new
tool, expect yourself to lose about half of your skill while using it.
But once you master it you gain new proficiency with _all_ of your
tools.

If I can make a suggestion, break your grammar down to a small kernel
and get that working in ANTLRWorks. Then start extending it and you'll
have a working grammar in no time.

 ...Richard


More information about the antlr-interest mailing list