jison icon indicating copy to clipboard operation
jison copied to clipboard

EBNF unable to get at sub-expression

Open rogierschouten opened this issue 11 years ago • 5 comments

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?

rogierschouten avatar Oct 13 '14 13:10 rogierschouten

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?

brianZeng avatar Oct 13 '14 15:10 brianZeng

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

agershun avatar Apr 22 '15 04:04 agershun

@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}; }
    ;

agershun avatar Apr 22 '15 05:04 agershun

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.

GerHobbelt avatar Oct 26 '15 21:10 GerHobbelt

:+1: Thanl you!

agershun avatar Oct 27 '15 08:10 agershun