I am trying to figure out a JSF*uck grammar. I read the actual JavaScript grammar here and only took the grammar rules that were relevant to JSF*ck and got this:
Expression: AdditiveExpression ;
AdditiveExpression: UnaryExpression
| AdditiveExpression '+' UnaryExpression
;
UnaryExpression: UpdateExpression
| '+' UnaryExpression
| '!' UnaryExpression
;
UpdateExpression: MemberExpression
| UnaryExpression
;
MemberExpression: PrimaryExpression
| MemberExpression '[' Expression ']'
;
PrimaryExpression: ArrayLiteral
| ParenthesizedExpression
;
ParenthesizedExpression: '(' Expression ')' ;
ArrayLiteral: '[' ']' ;
Since I'm trying to implement a recursive descent parser, I tried removing left recursion by changing the grammar to this:
Expression: AdditiveExpression ;
AdditiveExpression: MemberExpression ('+' AdditiveExpression)? ;
MemberExpression: ('+'|'!') Expression
| Atom ('[' Expression ']')?
;
Atom: '[' Expression? ']'
| '(' Expression ')'
;
However, my AST doesn't get built correctly. For example, ![]+[]
should be interpreted as addition(unary_not(array), array)
but I get unary_not(addition(array, array))
instead. I thought that the unary operators would have a higher precendence since I've defined them further down from the topmost rule compared to addition ?
I've also read this answer and this one, but the answers were not really self-explanatory. Any guidance would be greatly appreciated.