我需要由jison执行数学和条件语句,但问题是我对这里的jison是新手,所以我在下面附加了我的lex代码,没有条件语句,效果很好,但当我附加条件lex时,我得到了一个错误,我也附加了错误。
/* 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 */);
}
;
我收到错误
Parse error on line 1:
IF(43)THEN(5)
^
Expecting '-', '(', 'NUMBER', 'E', 'PI', 'IF', got 'INVALID'
我也想执行以下运算符'<','>,'=='
最后,我发现我在添加后的lex语法IF、THEN、ELSE中遗漏了终端符号,它开始工作