5

ここでは、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。どうすればこれを修正できますか? また、電卓の例で階乗定義を 2 つの中括弧で囲んでいるのはなぜ{{ /*like so*/ }}ですか?

4

2 に答える 2

3

プロダクションで定義された関数 ( などfact) を呼び出すには、mod-brace 表記%{を使用%}して複数行のセマンティック アクションを実行できます。

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;
}
于 2013-01-27T20:55:01.217 に答える
2

@Nucleon と @chaosbohne が指摘したように、受け入れられた回答は正しくありません。

Jison wiki から直接 ( https://github.com/zaach/jison/wiki/Deviations-From-Flex-Bison )

ブロックスタイルのステートメントを使用する場合は、レクサーアクション内で%{ ... %} 区切り文字を使用します。たとえば、次のようになります。

.*  %{
  if (true) {
    console.log('test');
  }
  // ...
%}

パーサー アクション内では、同じ目的で {{ .. }} 区切り文字を使用します。

したがって、レクサーを使用している場合、複数行のアクションには%{ action %}. パーサーでは、複数行のアクションの場合、 を使用します{{ action }}。単一行のアクションを作成している場合{}、レクサーとパーサーでは単一のブレースが正常に機能します。

于 2015-03-15T23:31:41.740 に答える