0

パーと親愛なるコミュニティへ

最初に、素晴らしい Antlr4 (および antlr 全体 :-) ) に感謝したいと思います。私は過去 6 か月間 Antlr 3 を使用してきました (すでに非常に満足しています) が、antlr4 にはさらに満足しています。Java をターゲット言語として使用することで、文法の単純さと生成時間の大幅な改善に気付きました。残念ながら、antlr3 にはなかったランタイム パフォーマンスについて懸念があります。

ここに私の文法の抜粋があります:

declare_specs
:
DECLARE? declare_spec+
|
DECLARE
;

declare_spec
:
constant_declaration
| variable_declaration
| exception_declaration
| procedure_body
| function_body
;

生成されたコード (トレース用に System.out.println を追加しました):

    public final Declare_specsContext declare_specs() throws RecognitionException {
            System.out.println("TIME: " + timestamp() + " - declare_specs - 1");
        Declare_specsContext _localctx = new Declare_specsContext(_ctx, getState());
        System.out.println("TIME: " + timestamp() + " - declare_specs - 2");
        enterRule(_localctx, 118, RULE_declare_specs);
        System.out.println("TIME: " + timestamp() + " - declare_specs - 3");
        int _la;
        try {
            int _alt;
        System.out.println("TIME: " + timestamp() + " - declare_specs - 4");
        setState(826);
        System.out.println("TIME: " + timestamp() + " - declare_specs - 5");
        switch ( getInterpreter().adaptivePredict(_input,69,_ctx) ) {
            case 1:
        System.out.println("TIME: " + timestamp() + " - declare_specs - 6");
                enterOuterAlt(_localctx, 1);
        System.out.println("TIME: " + timestamp() + " - declare_specs - 7");
                {

                        if (f_trace >= f_trace_low) {
                            System.out.println("TIME: " + timestamp() + " - DECLARE_SPECS - FIRST ALT");
                        };

                setState(817);
        System.out.println("TIME: " + timestamp() + " - declare_specs - 8");
                _la = _input.LA(1);
        System.out.println("TIME: " + timestamp() + " - declare_specs - 9");
                if (_la==DECLARE) {
                    {
                    setState(816); match(DECLARE);
                    }
                }

        System.out.println("TIME: " + timestamp() + " - declare_specs - 10");
                setState(820); 
        System.out.println("TIME: " + timestamp() + " - declare_specs - 11");
                _errHandler.sync(this);
        System.out.println("TIME: " + timestamp() + " - declare_specs - 12");
                _alt = getInterpreter().adaptivePredict(_input,68,_ctx);
        System.out.println("TIME: " + timestamp() + " - declare_specs - 13");
                do {
                    switch (_alt) {
                    case 1:
                        {
                        {
        System.out.println("TIME: " + timestamp() + " - declare_specs - 14");
                        setState(819);
        System.out.println("TIME: " + timestamp() + " - declare_specs - 15");
                        declare_spec();
        System.out.println("TIME: " + timestamp() + " - declare_specs - 16");
                        }
                        }
                        break;
                    default:
                        throw new NoViableAltException(this);
                    }
        System.out.println("TIME: " + timestamp() + " - declare_specs - 17");
                    setState(822);
        System.out.println("TIME: " + timestamp() + " - declare_specs - 18");
                    _errHandler.sync(this);
        System.out.println("TIME: " + timestamp() + " - declare_specs - 19");
                    _alt = getInterpreter().adaptivePredict(_input,68,_ctx);
        System.out.println("TIME: " + timestamp() + " - declare_specs - 20");
                } while ( _alt!=2 && _alt!=-1 );
                }
                break;

            case 2:
                enterOuterAlt(_localctx, 2);
                {

                        if (f_trace >= f_trace_low) {
                            System.out.println("TIME: " + timestamp() + " - DECLARE_SPECS - SECOND ALT");
                        };

                setState(825); match(DECLARE);
                }
                break;
            }
        }
        catch (RecognitionException re) {
            _localctx.exception = re;
            _errHandler.reportError(this, re);
            _errHandler.recover(this, re);
        }
        finally {
            exitRule();
        }
        return _localctx;
    }

ここにトレース:

................
TIME: 2013-02-06 09:47:10.417 - declare_specs - 12
TIME: 2013-02-06 09:47:11.023 - declare_specs - 13
.................
TIME: 2013-02-06 09:51:38.915 - DECLARE_SPEC - AFTER
.................
TIME: 2013-02-06 09:51:38.916 - declare_specs - 19
TIME: 2013-02-06 09:52:31.435 - declare_specs - 20
...................
TIME: 2013-02-06 09:52:31.435 - DECLARE_SPEC - INIT

_alt = getInterpreter().adaptivePredict(_input,68,_ctx); を呼び出すと、60 インチが失われます。2 回目ですが、_alt = getInterpreter().adaptivePredict(_input,68,_ctx); を呼び出すときは 1' 未満です。初めて。変更されたのは、もちろんパラメーター _input と _ctx です。

問題はおそらく私の文法にありますが、私は頭がおかしいです;-)。1. 解決策を探す場所を教えていただけないでしょうか。2.とにかくadaptivePredictで何が起こっているのか;-)

ご協力ありがとうございました!

敬具、ヴォルフガング・ハマー

4

1 に答える 1

1

非常に大量の先読みを必要とする、および/またはあいまいまたは文脈依存の文法で決定を下しているようです。残念ながら、完全な文法を確認しないと、どれがどれであるかを判断することはできません。できることは次のとおりです。これにより、あいまいさに関する詳細情報がコンソールに出力されます。

parser.addErrorListener(new DiagnosticErrorListener());
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
于 2013-02-06T14:10:36.033 に答える