Pergunta

I'd like to make a grammar that will allow curried function calls.

That is:

a() /// good
a()() /// good
a()()() /// good
a(a) /// good
a(a()()) /// good
/// etc

My first stab was this:

ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;

fncall  :   expr '(' (expr (',' expr)*)? ')';

expr    :   ID|fncall;

But that fails due to left recursion.

Foi útil?

Solução

Assuming (a)() would also be valid, here's a way to solve this:

grammar T;

options {
  output=AST;
}

tokens {
  EXPR_LIST;
  CALL;
  INDEX;
  LOOKUP;
}

parse
 : expr EOF -> expr
 ;

expr
 : add_expr
 ;

add_expr
 : mul_exp (('+' | '-')^ mul_exp)*
 ;

mul_exp 
 : atom (('*' | '/')^ atom)*
 ;

atom
 : fncall
 | NUM
 ;

fncall
 : (fncall_start -> fncall_start) ( '(' expr_list ')' -> ^(CALL $fncall expr_list)
                                  | '[' expr ']'      -> ^(INDEX $fncall expr)
                                  | '.' ID            -> ^(LOOKUP $fncall ID)
                                  )* 
 ;

fncall_start
 : ID
 | '(' expr ')' -> expr
 ;

expr_list
 : (expr (',' expr)*)? -> ^(EXPR_LIST expr*)
 ;

NUM : '0'..'9'+;
ID  :  ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;

The parser generated from the grammar above would parse the input:

(foo.bar().array[i*2])(42)(1,2,3)

and construct the following AST:

enter image description here

Without the tree rewrite rules, the grammar would look like this:

grammar T;

parse
 : expr EOF
 ;

expr
 : add_expr
 ;

add_expr
 : mul_exp (('+' | '-') mul_exp)*
 ;

mul_exp 
 : atom (('*' | '/') atom)*
 ;

atom
 : fncall
 | NUM
 ;

fncall
 : fncall_start ( '(' expr_list ')' | '[' expr ']' | '.' ID )* 
 ;

fncall_start
 : ID
 | '(' expr ')'
 ;

expr_list
 : (expr (',' expr)*)?
 ;

NUM : '0'..'9'+;
ID  :  ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top