[antlr-interest] Newbie Issues: MismatchedTokenExceptions andFailedSyntacticPredicates

Micheal J open.zone at virgin.net
Thu Jul 12 13:59:10 PDT 2007


Hi,

> Hi. I'm new to this list, and new to ANTLR in general, though
> I have quite a bit of experience on a previous project 
> implementing a DSL compiler with JavaCC (I'm switching to 
> ANTLR primarily because of support for C#).

If JavaCC-with-C#-output represents a better choice for you at the moment
(given your JavaCC experience) then this may be interesting:

http://sourceforge.net/projects/csharpcc

> I'm currently developing a grammar for stock-market
> indicators. For example, when complete it'll allow a user to 
> define an indicator like
> this:
> 
> <code>
>    // In technical analysis, the "MACD" symbol represents a
> "Moving Average
>    // Convergence Divergence" indicator. This indicator is a 
> simple arithmetic
>    // function over a "slow" exponential moving average 
> (EMA), and a "fast"
>    // exponential moving average.
> 
>    indicator MACD[m, n] : EMA[m](CLOSE) - EMA[n](CLOSE) </code>
> 
> But I'm getting ahead of myself. For now, I'm just trying to
> get the left-hand side of the assignment expression to parse 
> correctly. The attached grammar file (VTExpression.g) was 
> written to parse syntax like "indicator MACD[m, n]", as well 
> as "indicator EMA[3]" or "indicator XYZ[true, 5, 3 + (n % 2) ]".
> 
> But even on the simplest stuff ("indicator EMA[3]"), I get a
> MismatchedTokenException from the ANTLRWorks interpreter at 
> the top level of the expression.

VTExpression.g works fine for me in the ANTLRWorks *debugger* given *all* of
your sample input snippets. Note the interpreter is *documented* as not
supporting/executing actions, semantic predicates, backtrack=true etc.

You may want to add the EOF virtual token to the end of
'indicatorDeclaration' to handle EOF.

> 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?)
> 
> Now, using the tokenless grammar (VTExpression2.g), the
> interpreter gets inside the left-bracket, but it fails to 
> recognize the "3" as an IntegerLiteral, instead throwing a 
> FailedPredicateException (between primaryExpression and synpred19).
> 
> When I do a google search on syntactic predicates, I can see
> that they're defined like this:
> 
>    (...)=>
> 
> But I'm not doing that. Whenever I've seen these failed
> predicate problems, it's from ordinary stuff like this:
> 
>    A : B ( op_a B)* ;
>    B : C ( op_b C)* ;
> 
> 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.
> 
> 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?

No stranger to stirring a hornet's nest I see  ;-)

> Anyhow, thanks for whatever help you can offer. I appreciate it.
> 
> --benji smith

Micheal

-----------------------
The best way to contact me is via the list/forum. My time is very limited.



More information about the antlr-interest mailing list