jison

how to perform if and else statement in jison parser?


I need to perform math and conditional statement by jison,but problem is i am new to jison here so i attached my lex code below without conditional statment work good but when i attach conditional lex i got an error, i attached error too.

/* description: Parses end executes mathematical expressions. */

/* lexical grammar */
%lex
%%

\s+                   /* skip whitespace */
[0-9]+("."[0-9]+)?\b  return 'NUMBER'
"*"                   return '*'
"/"                   return '/'
"-"                   return '-'
"+"                   return '+'
"^"                   return '^'
"("                   return '('
")"                   return ')'
"PI"                  return 'PI'
"E"                   return 'E'
<<EOF>>               return 'EOF'
.                     return 'INVALID'

/lex

/* operator associations and precedence */

%left '+' '-'
%left '*' '/'
%left '^'
%left UMINUS

%start expressions

%% /* language grammar */

expressions
    : e EOF
        {return $1;}
    | IfExpression
    ;

e

    : e '+' e
        {$$ = $1+$3;}
    | e '-' e
        {$$ = $1-$3;}
    | e '*' e
        {$$ = $1*$3;}
    | e '/' e
        {$$ = $1/$3;}
    | e '^' e
        {$$ = Math.pow($1, $3);}
    | '-' e %prec UMINUS
        {$$ = -$2;}
    | '(' e ')'
        {$$ = $2;}
    | NUMBER
        {$$ = Number(yytext);}
    | E
        {$$ = Math.E;}
    | PI
        {$$ = Math.PI;}
;
IfExpression
: "IF" "(" Expression ")" "THEN" "(" Expression ")"
    {
        $$ = new IfExpressionNode(/* pass arguments as desired */);
    }
| "IF" "(" Expression ")" "THEN" "(" Expression ")" "ELSE" "(" Expression ")"
    {
        $$ = new IfExpressionNode(/* arguments */);
    }
;

i got error

Parse error on line 1:
IF(43)THEN(5)
^
Expecting '-', '(', 'NUMBER', 'E', 'PI', 'IF', got 'INVALID'

i want to perform following operators too '<','>,'=='


Solution

  • Finally, I found I missed terminal symbol in my lex grammer IF,THEN,ELSE after added it started works