How to remove left recursion

I would like to make a grammar that will allow you to make calls in curry.

I.e:

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

My first blow was as follows:

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

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

expr    :   ID|fncall;

But this fails due to left recursion.

+5
source share
1 answer

Assuming that (a)()it will also be valid, here you can solve the following:

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'|'_')*;

A parser generated from the above grammar parses input:

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

and build the following AST:

enter image description here

Without rules for rewriting the tree, the grammar will 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'|'_')*;
+3
source

All Articles