1

I'm trying to add support for arrays in my programming language and am having trouble

Array
  : '[' Expr ("," Expr)* ']'
     {{ $$  = ['ArrayList', $1]; }}
  | '[' Expr ']'
     {{ $$ = ['Array', $2]; }}
  | '[' ']'
     {{ $$ = ['Empty']; }}
  ;

This however, will not parse "[1,2,3,4]." Jison tells me that it expects "]" but it got ",". Any ideas?

2 Answers2

3

The recursion isn't interpreted or rejected. You have to split it into 2 elements to make it work:

Array
  : '[' Element ']'
     {{ $$  = ['ArrayList', $2]; }}
  ;

Element
  : Element "," Expr
     {{ $$ = $1 + ',' + $3 }}
  | Expr
     {{ $$ = $1 }};

This returns an Array as expected:

["ArrayList","1,2,3,4"]
Alberto
  • 880
  • 5
  • 15
1

jison does not accept EBNF. (It also doesn't reject it either, apparently.) So your rule:

Array
  : '[' Expr ("," Expr)* ']'

is interpreted as though it were:

Array
  : '[' Expr "," Expr ']'

You need to create an ExprList production:

Array   : '[' ExprList ']'
        | '[' ']'
        ;
ExprList: Expr
        | ExprList ',' Expr
        ;
rici
  • 234,347
  • 28
  • 237
  • 341
  • Jison now supports EBNF syntax via the `%ebnf` declaration. See: https://gist.github.com/zaach/1659274 – Clark Apr 05 '15 at 08:29