2013-10-23 75 views
0

我已經構建了一個使用javafx和swing的GUI,並且當我添加一個動作偵聽器來解析文本字段中的表達式時出現錯誤,我不確定問題出在哪裏。在javafx和swing應用程序中運行antlr

的錯誤是:

Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError 
at functionparsergui.Test.parseFunction(Test.java:110) 
at functionparsergui.Test.access$000(Test.java:38) 
at functionparsergui.Test$2.actionPerformed(Test.java:88) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) 
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) 
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 
at java.awt.Component.processMouseEvent(Component.java:6505) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) 
at java.awt.Component.processEvent(Component.java:6270) 
at java.awt.Container.processEvent(Container.java:2229) 
at java.awt.Component.dispatchEventImpl(Component.java:4861) 
at java.awt.Container.dispatchEventImpl(Container.java:2287) 
at java.awt.Component.dispatchEvent(Component.java:4687) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) 
at java.awt.Container.dispatchEventImpl(Container.java:2273) 
at java.awt.Window.dispatchEventImpl(Window.java:2719) 
at java.awt.Component.dispatchEvent(Component.java:4687) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729) 
at java.awt.EventQueue.access$200(EventQueue.java:103) 
at java.awt.EventQueue$3.run(EventQueue.java:688) 
at java.awt.EventQueue$3.run(EventQueue.java:686) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 
at java.awt.EventQueue$4.run(EventQueue.java:702) 
at java.awt.EventQueue$4.run(EventQueue.java:700) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 
Caused by: java.lang.UnsupportedOperationException: java.io.InvalidClassException: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with version 3 (expected 2). 
at org.antlr.v4.runtime.atn.ATNSimulator.deserialize(ATNSimulator.java:114) 
at edu.chrr.util.function.FunctionLexer.<clinit>(FunctionLexer.java:504) 
... 39 more 
Caused by: java.io.InvalidClassException: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with version 3 (expected 2). 
... 41 more 

我的代碼從聲明的ActionListener開始如下:

ActionListener clearField = new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      exprField.setText(""); 
      JOptionPane.showMessageDialog(frame, "Input Cleared"); 
     } 
    }; 

    clearButton.addActionListener(clearField); 


    ActionListener parserButton; 
    parserButton = new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      String expression = exprField.getText(); 
      String nowhiteExpr = expression.replaceAll("\\s+", ""); 
      parseFunction(nowhiteExpr, frame); 


     } 
    }; 

    parseButton.addActionListener(parserButton); 


    Platform.runLater(new Runnable() { 
     @Override 
     public void run() { 
      initFX(fxPanel); 
     } 
    }); 
} 

private static void parseFunction(final String function, final JFrame frame) { 

    try { 
       ANTLRInputStream input = new ANTLRInputStream(function); 
       FunctionLexer lexer = new FunctionLexer(input); 
       CommonTokenStream tokens = new CommonTokenStream((TokenSource) lexer); 
       FunctionParser parser = new FunctionParser(tokens); 
       parser.start(); 
       int errorsCount = parser.getNumberOfSyntaxErrors(); 
       if (errorsCount == 0) { 
        JOptionPane.showMessageDialog(frame, "Syntax is Correct"); 

       } else { 

        Token t = parser.getCurrentToken(); 
        String msg = "Syntax Incorrect: Missing " + t.getText(); 
        JOptionPane.showMessageDialog(frame, msg); 

       } 

      } catch (RecognitionException ex) { 

       JOptionPane.showMessageDialog(frame, "Syntax is Incorrect"); 
      } 
} 





private static void initFX(JFXPanel fxPanel) { 
    // This method is invoked on the JavaFX thread 
    Scene scene = createScene(); 
    fxPanel.setScene(scene); 
} 

private static Scene createScene() { 
    Group root = new Group(); 
    Scene scene = new Scene(root, Color.ALICEBLUE); 
      return (scene); 
} 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      initAndShowGUI(); 
     } 
    }); 
} 
} 

回答

1

ANTLR 4.1不符合ANTLR 4.0兼容。您正在使用ANTLR 4.1生成代碼,但試圖使用ANTLR 4.0運行時庫運行它。

相關問題