我这里有一个JISON计算器示例的略微修改的版本:
/* 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 '%'
"(" return '('
")" return ')'
"PI" return 'PI'
"E" return 'E'
<<EOF>> return 'EOF'
. return 'INVALID'
/lex
/* operator associations and precedence */
%left '+' '-'
%left '*' '/'
%left '^'
%right '!'
%right '%'
%left UMINUS
%start expressions
%% /* language grammar */
expressions
: e EOF
{ typeof console !== 'undefined' ? console.log($1) : print($1);
return $1; }
;
e
: e '+' e
{$$ = $1+$3;}
| e '-' e
{$$ = $1-$3;}
| e '*' e
{$$ = $1*$3;}
| e '/' e
{$$ = $1/$3;}
| e '^' e
{$$ = Math.pow($1, $3);}
| e '!'
{{
$$ = fact($1);
}}
| e '%'
{$$ = $1/100;}
| '-' e %prec UMINUS
{$$ = -$2;}
| '(' e ')'
{$$ = $2;}
| NUMBER
{$$ = Number(yytext);}
| E
{$$ = Math.E;}
| PI
{$$ = Math.PI;}
;
%%
/*why doesn't this work at runtime?
I see other examples defining declarations this way but I must be doing something wrong
I couldn't find a syntactically valid way of putting this declaration anywhere but here,
which is probably the issue*/
function fact(n) {
var tot=1;
for(var i=2;i<=n;++i) {
tot*=i;
}
return tot;
}请注意!运算符定义中的细微差别。我尝试在外部定义fact函数,而不是内联定义。
到目前为止,它在运行时告诉我fact is not defined。我该如何解决这个问题呢?另外,为什么计算器示例在阶乘定义{{ /*like so*/ }}周围使用了两个大括号
发布于 2013-01-28 04:55:01
要调用在产品(如fact)下定义的函数,您可以使用大括号表示法%{和%}执行多行语义操作:
e
: e '+' e
...
| e '!'
%{
// the %{ tells jison this is a multi-line js eval statement
$$ = fact($1);
%}
;作为最终的解决方案,尝试这样做:
/* lexical grammar */
%lex
%%
\s+ /* skip whitespace */
[0-9]+("."[0-9]+)?\b return 'NUMBER'
"*" return '*'
"/" return '/'
"-" 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 '^'
%right '!'
%right '%'
%left UMINUS
%start expressions
%% /* language grammar */
expressions
: e EOF
%{
typeof console !== 'undefined' ? console.log($1) : print($1);
return $1;
%}
;
e
: e '+' e
{$$ = $1+$3;}
| e '-' e
{$$ = $1-$3;}
| e '*' e
{$$ = $1*$3;}
| e '/' e
{$$ = $1/$3;}
| e '^' e
{$$ = Math.pow($1, $3);}
| e '!'
%{
$$ = fact($1);
%}
| e '%'
{$$ = $1/100;}
| '-' e %prec UMINUS
{$$ = -$2;}
| '(' e ')'
{$$ = $2;}
| NUMBER
{$$ = Number(yytext);}
| E
{$$ = Math.E;}
| PI
{$$ = Math.PI;}
;
%%
function fact(n) {
var tot=1;
for(var i=2;i<=n;++i) {
tot*=i;
}
return tot;
}发布于 2015-03-16 07:31:42
正如@Nucleon和@chaosbohne所指出的那样,公认的答案是不正确的。
直接从Jison wiki (https://github.com/zaach/jison/wiki/Deviations-From-Flex-Bison)
在词法分析器操作中,如果要使用块样式的语句,请使用%{ ... %}分隔符,例如:
.* %{
if (true) {
console.log('test');
}
// ...
%}在解析器操作中,使用{{ ..}}个分隔符用于相同的目的。
因此,如果您在lexer中,对于多行操作,请使用%{ action %}。在解析器中,对于多行操作,使用{{ action }}。如果您正在编写单行操作,则单花括号{}在词法分析器和解析器中可以很好地工作。
https://stackoverflow.com/questions/14528648
复制相似问题