[antlr-interest] Function Expressions

Jeff Hair knifed at gmail.com
Wed May 4 10:37:46 PDT 2011


Right. That's basically what I'm doing right now. The problem is that I
can't call do stuff like anonymous function calls (which can be generated by
an expression). So I'm trying to figure out how to do enable that without
getting the recursion errors.

JavaScript allows you to do stuff like (function() { ... })(); in order to
call the function. You can even do it with (1 + 1)(), which of course
returns an error. But the point is that it's possible. That's what I'm
trying to enable.

On Wed, May 4, 2011 at 1:05 PM, Jim Idle <jimi at temporal-wave.com> wrote:

> Generally when parsing you do this:
>
>
> expr
> ...
>
>
> atom
>  : i=ID
>     (
>          LPAREN e=exprList RPAREN  ->^(FUNC $i $e)
>           | -> ^(IDENT $i)
>     )
>
> ...
>
>
> Jim
>
> > -----Original Message-----
> > From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> > bounces at antlr.org] On Behalf Of Jeff Hair
> > Sent: Wednesday, May 04, 2011 9:27 AM
> > To: antlr-interest at antlr.org
> > Subject: [antlr-interest] Function Expressions
> >
> > Hello all,
> >
> > I have a simple C/JavaScript-style grammar for my interpreter project.
> > Right now, functions can be called via identifier(), or
> > identifier(param1, param2). This works fine for simple cases, but in my
> > language functions are first-class objects. I'm in the process of
> > redoing my identifier logic for properties, arrays, and function calls.
> > I've gotten the first two working.
> >
> > I'm trying to allow expressions to be callable as functions, so I can
> > do stuff like createFunction()(), where createFunction would be a
> > function that returns a function. Another example would be (1 + 1)().
> > Obviously that should throw an error, but it should be a permissible
> > language construct.
> > JavaScript allows this.
> >
> > The func.g file in my gist is a pared down version of the language
> > grammar, with only the relevant rules in it. Understandably, it fails
> > with the following errors:
> >
> > [java] error(210):  The following sets of rules are mutually left-
> > recursive [boolNegation, unary, add, mult, relation, term, expression]
> > [java] error(206): /home/user/955488/func.g:66:2: Alternative 1: after
> > matching input such as IDENT '(' decision cannot predict what comes
> > next due to recursion overflow to relation from expression [java]
> > error(201): /home/user/955488/func.g:66:2: The following alternatives
> > can never be matched: 2
> >
> > https://gist.github.com/955488 demonstrates the issue. I've stripped
> > out everything except the expression rules. The gist can be cloned as a
> > git repo and then built via Ant + Ivy.
> >
> > I understand why it's failing. There's a conflict between the IDENT
> > expression and IDENT '(' ')' for function calls. What I'm trying to
> > figure out is how to allow both identifiers and function calls. If I
> > figure that out, it should give me the rest of what I need. Any help
> > would be appreciated.
> >
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> > email-address
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list