[antlr-interest] Function Expressions
Jeff Hair
knifed at gmail.com
Wed May 4 16:08:55 PDT 2011
Oh, and thanks. I've put your code through a bunch of crazy scenarios after
adapting it to my full needs and it works perfectly. I can now move forward
with changes to the tree parser and interpreter. Thanks again.
On Wed, May 4, 2011 at 6:19 PM, John B. Brodie <jbb at acm.org> wrote:
> I think that basically you want Application (I come from the lambda
> calculus and function invocation is called Application therein) to be a
> post-fix operator.
>
> If you look at grammars for Java or C or other C-like languages you will
> see (I believe) that indexing into an array and/or projecting a field
> out of a tuple (record) and probably others-like-them are post-fix
> operators.
>
> So I would recommend the following (tested, but not using your
> incomplete gist framework) --- replacing your term rule with:
>
> //Expressions
> primary
> : IDENT
> | '('! expression ')'!
> | INTEGER
> | DOUBLE
> | BOOLEAN
> ;
>
> term
> : (primary -> primary) ( suffix[$term.tree] -> suffix )*
> ;
>
> suffix [CommonTree term] :
> ( x='(' modifiers? ')' -> ^(APPLICATION[$x,"A"] {$term} modifiers?) )
> | ( x='[' modifiers ']' -> ^(INDEXING[$x,"I"] {$term} modifiers) )
> | ( x='.' (p=IDENT|p=INTEGER) -> ^(PROJECTION[$x,"P"] {$term} $p) )
> ;
>
> modifiers : expression (','! expression)* ;
>
>
>
> with an appropriate tokens{} section defining the APPLICATION;
> INDEXING; and PROJECTION imaginary tokens.
>
> the above is a little complicated in order to get the imaginary token
> representing the suffix operator to be a tree root.
>
> note that the stuff in the []'s after the imaginary token name is for
> error reporting and/or tree pretty printing. keep the $x but change the
> string to suite your need.
>
> hope this helps
> -jbb
>
>
>
>
More information about the antlr-interest
mailing list