Flex / Bison中的Lisp解释器

时间:2018-03-05 16:15:28

标签: bison flex-lexer context-free-grammar

我有以下Bison语法:

%%
input:
    expression { cout << $1 << endl;}
    ;

expression:
    INT
    | OP PLUS expression expression CP {$$ = $3 + $4;}
    | OP MINUS expression expression CP {$$ = $3 - $4;}
    | OP MULT expression expression CP {$$ = $3 * $4;}
    | OP DIV expression expression CP {$$ = $3 / $4;}
    | OP PLUS expression expression exprseries CP {$$ = $3 + $4 + $5;}
    ;

exprseries:
    expression
    | expression exprseries {$$ = $1 + $2;}
    ;
%%

OPCP分别是开括号和近括号。

我希望它识别(+ <exp1> <exp2> ... <expn>)并计算总和。不幸的是,它只识别(+ INT INT ... INT)(+ INT <exp1> <exp2> <exp3>)

0 个答案:

没有答案