[antlr-interest] Function Expressions

Jim Idle jimi at temporal-wave.com
Wed May 4 10:05:13 PDT 2011


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


More information about the antlr-interest mailing list