EBNF unable to get at sub-expression
Consider this grammar part:
function_call
: IDENTIFIER "(" (expression ("," expression)* )? ")"
{
console.log($1);
console.log($3);
try {
if ($5) {
console.log($5);
}
} catch (e) {
console.log("exception");
}
}
This code executes the "exception" log statement. In this case, $5 can be undefined due to the asterisk-operator. However, I cannot test for this, because the $5 is translated by JISON into an array subscript. Am I doing something wrong or does JISON maybe need a way to discover whether $x is present?
I also have this issue. for now,I just change the grammar like this
function_call:
IDENTIFIER '(' ')'
|IDENTIFIER '(' param ')'
;
param:
expression
|param ',' expression
;
Is there any other solution?
Exactly the same problem with %ebnf:
CreateVertex
: CREATE VERTEX Literal? (SET SetColumnsList | CONTENT ExprList | Select)?
{
$$ = new yy.CreateVertex({class:$3, action:$4});
$$.expr = $5; // this should be or SetColumnList or ExprList or undefined
}
;
translates to:
this.$ = new yy.CreateVertex({class:$$[$0-1], action:$$[$0]});
this.$.expr = $$[$01]; // wrong parameter, because parser has only 3 values in stack
@brianZeng Thank you for the idea of solution. I used the same approach:
CreateVertex
: CREATE VERTEX Literal? CreateVertexSet
{ $$ = new yy.CreateVertex({class:$3}); yy.extend($$,$4); }
;
CreateVertexSet
: SET SetColumnsList { $$ = {sets:$2}; }
| CONTENT ExprList { $$ = {content:$2}; }
| Select { $$ = {select:$1}; }
;
After some work on the EBNF stuff lately, I added a bit of docu in the wiki that might be useful, if only sideways today (as EBNF in latest jison is a tad skewered, while I must say that my own fork isn't super-duper either.
Here's the docs: https://github.com/zaach/jison/wiki/Deviations-From-Flex-Bison#extended-bnf
The TL;DR take-away is that you MUST NOT EVER attempt to reference any term inside the outermost wildcarded EBNF group/operator. The difference between vanilla and GerHobbelt today is that vanilla discards the terms beyond $1 in there, while GerHobbelt produces a nested set of arrays carrying the terms' values.
:+1: Thanl you!