2013-02-06 27 views
0

帕爾和親愛的社區,ANTLR4:差績效調用getInterpreter()adaptivePredict

當起初,我要感謝你們的驚人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 
; 

這裏所生成的代碼(I加入的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''adaptivePredict(_input, 68,_ctx);第二次調用_alt = getInterpreter()時小於1'。adaptivePredict(_input,68,_ctx);第一次。 什麼改變當然是參數_input和_ctx。

問題可能出在我的語法上,但我在我的智慧結局;-)。 1.您可以告訴我我可能在哪裏尋找解決方案。 2.無論如何,adaptivePredict中發生了什麼;-)

謝謝你的幫助!

親切的問候,沃爾夫岡·錘

+0

對不起,親愛的帕爾先生和尊敬的社區 –

回答

0

這聽起來像你有你的語法決定它需要非常大的量先行的和/或不明確或上下文敏感的。不幸的是,我們無法在沒有看到完整語法的情況下告訴你哪一個。你可以做的是以下內容將打印出一些關於歧義的詳細信息給控制檯。

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

嗨Helotes, 感謝你回答!我有三種錯誤: 1. reportAttemptingFullContext d = nnn,input ='xxx' 2. reportContextSensitivity d = nnn,input ='xxx' 3. reportAmbiguityd = nnn:ambigAlts = {1,n, ...},input ='xxx' 我查看了代碼:d是DFA中的決定,並且輸入是顯而易見的。我如何從d(決定)到我的語法中的錯誤規則?它甚至有可能嗎?類「ParserATNSimulator」中的代碼非常複雜。 謝謝你的幫助! 親切的問候,WolfgangHämmer –