[antlr-interest] Problems with syntactic predicates(?)

Mark Volkmann r.mark.volkmann at gmail.com
Sat Jan 26 18:21:08 PST 2008


On Jan 26, 2008 7:06 PM, Jim Idle <jimi at temporal-wave.com> wrote:
> Look at the example grammars for how to set up for parsing expressions
> with precedence, you don't need those predicates but you do need to
> chain your rules correctly. Start by coding the primitives (the thigns
> that cannot break down further). You want something like
>
> equations
>         : equation* EOF
>         ;
>
> equation
>   : primary ('=' primary)?
>   ;
>
> primary
>   : IDENT ('(' expression (',' expression)* ')')?
>   | INT
>   ;
>
> INT     :       ( '0'..'9' )+ ;
> IDENT   :       ( 'a'..'z' )+ ;
> WS      :       (' ' | '\t' )+ { $channel = HIDDEN; } ;
> NL      :       '\r'? '\n'      { $channel = HIDDEN; } ;
>
> You are trying to construct context/semantics in the parser, but it's
> job is to parse correct syntactical constructs only. After the parser
> you can apply context and semantics. For your grammar then you want to
> combine everything with common roots into one rule/alt and branch when
> tokens tell you that you must.

I like this approach much better.
One minor change is needed to parse the original input which contains
IDENT values containing underscores.

IDENT: ('a'..'z' | '_')+;

-- 
R. Mark Volkmann
Object Computing, Inc.


More information about the antlr-interest mailing list