ANTLR4:调用getInterpreter()时的性能不佳.adaptivePredict

时间:2013-02-06 10:03:55

标签: performance antlr4

帕尔和亲爱的社区,

首先,我要感谢你们惊人的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()时失去了60''。adaptive adaptiveinput,_input,68,_ctx);第二次但是在调用_alt = getInterpreter()时小于1'.adaptivePredict(_input,68,_ctx);第一次。 更改的当然是参数_input和_ctx。

问题很可能在于我的语法,但我在我的智慧结束;-)。 你可以告诉我在哪里寻找解决方案。 2.无论如何,adaptivePredict正在发生什么; - )

感谢您的帮助!

亲切的问候,WolfgangHämmer

1 个答案:

答案 0 :(得分:0)

听起来你的语法决定需要大量的前瞻和/或模糊或上下文敏感。不幸的是,如果没有看到完整的语法,我们将无法告诉你哪一个。您可以做的是以下内容,它将向控制台打印出有关歧义的详细信息。

parser.addErrorListener(new DiagnosticErrorListener());
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);