Reputation: 133
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?
Upvotes: 1
Views: 424
Reputation: 878
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"]
Upvotes: 3
Reputation: 241861
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
;
Upvotes: 1