0

我正在使用这个语法来生成 COBOL 解析器。

我使用 javacc 来生成解析器和 Java Tree Builder 来从这个语法生成 AST。

我正在自定义一个名为PMD的 Eclipse 插件来读取 COBOL 文件并生成 AST。有一个名为 Designer 的 UI,允许您从源代码中填充 AST。

当我尝试使用下面的源代码时,我收到一条错误消息:

错误信息

net.sourceforge.pmd.PMDException: Error while processing [no filename].cbl
    at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:80)
    at net.sourceforge.pmd.util.designer.Designer$DFAListener.actionPerformed(Designer.java:502)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: net.sourceforge.pmd.lang.ast.ParseException: net.sourceforge.pmd.lang.cpd.cobol.ast.ParseException: Encountered "" at line 0, column 0.
Was expecting one of:

    at net.sourceforge.pmd.lang.cobol.CobolParser.parse(CobolParser.java:35)
    at net.sourceforge.pmd.SourceCodeProcessor.parse(SourceCodeProcessor.java:90)
    at net.sourceforge.pmd.SourceCodeProcessor.processSource(SourceCodeProcessor.java:136)
    at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:75)
    ... 37 more
Caused by: net.sourceforge.pmd.lang.cpd.cobol.ast.ParseException: Encountered "" at line 0, column 0.
Was expecting one of:

    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.generateParseException(CobolParser.java:43571)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.jj_consume_token(CobolParser.java:43405)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.IdentificationDivision(CobolParser.java:3734)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.ProgramUnit(CobolParser.java:3527)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.CompilationUnit(CobolParser.java:3450)
    at net.sourceforge.pmd.lang.cobol.CobolParser.parse(CobolParser.java:33)
    ... 40 more
Exception in thread "AWT-EventQueue-0" net.sourceforge.pmd.lang.ast.ParseException: net.sourceforge.pmd.lang.cpd.cobol.ast.ParseException: Encountered "" at line 0, column 0.
Was expecting one of:

    at net.sourceforge.pmd.lang.cobol.CobolParser.parse(CobolParser.java:35)
    at net.sourceforge.pmd.util.designer.Designer.getCompilationUnit(Designer.java:135)
    at net.sourceforge.pmd.util.designer.Designer.access$1(Designer.java:132)
    at net.sourceforge.pmd.util.designer.Designer$ShowListener.actionPerformed(Designer.java:475)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: net.sourceforge.pmd.lang.cpd.cobol.ast.ParseException: Encountered "" at line 0, column 0.
Was expecting one of:

    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.generateParseException(CobolParser.java:43571)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.jj_consume_token(CobolParser.java:43405)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.IdentificationDivision(CobolParser.java:3734)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.ProgramUnit(CobolParser.java:3527)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.CompilationUnit(CobolParser.java:3450)
    at net.sourceforge.pmd.lang.cobol.CobolParser.parse(CobolParser.java:33)
    ... 39 more
net.sourceforge.pmd.PMDException: Error while processing [no filename].cbl
    at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:80)
    at net.sourceforge.pmd.util.designer.Designer$DFAListener.actionPerformed(Designer.java:502)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: net.sourceforge.pmd.lang.ast.ParseException: net.sourceforge.pmd.lang.cpd.cobol.ast.ParseException: Encountered " "*" "* "" at line 2, column 7.
Was expecting:
    "program-id" ...

    at net.sourceforge.pmd.lang.cobol.CobolParser.parse(CobolParser.java:35)
    at net.sourceforge.pmd.SourceCodeProcessor.parse(SourceCodeProcessor.java:90)
    at net.sourceforge.pmd.SourceCodeProcessor.processSource(SourceCodeProcessor.java:136)
    at net.sourceforge.pmd.SourceCodeProcessor.processSourceCode(SourceCodeProcessor.java:75)
    ... 37 more
Caused by: net.sourceforge.pmd.lang.cpd.cobol.ast.ParseException: Encountered " "*" "* "" at line 2, column 7.
Was expecting:
    "program-id" ...

    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.generateParseException(CobolParser.java:43571)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.jj_consume_token(CobolParser.java:43405)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.ProgramIdParagraph(CobolParser.java:3925)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.IdentificationDivision(CobolParser.java:3741)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.ProgramUnit(CobolParser.java:3527)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.CompilationUnit(CobolParser.java:3450)
    at net.sourceforge.pmd.lang.cobol.CobolParser.parse(CobolParser.java:33)
    ... 40 more
Exception in thread "AWT-EventQueue-0" net.sourceforge.pmd.lang.ast.ParseException: net.sourceforge.pmd.lang.cpd.cobol.ast.ParseException: Encountered " "*" "* "" at line 2, column 7.
Was expecting:
    "program-id" ...

    at net.sourceforge.pmd.lang.cobol.CobolParser.parse(CobolParser.java:35)
    at net.sourceforge.pmd.util.designer.Designer.getCompilationUnit(Designer.java:135)
    at net.sourceforge.pmd.util.designer.Designer.access$1(Designer.java:132)
    at net.sourceforge.pmd.util.designer.Designer$ShowListener.actionPerformed(Designer.java:475)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: net.sourceforge.pmd.lang.cpd.cobol.ast.ParseException: Encountered " "*" "* "" at line 2, column 7.
Was expecting:
    "program-id" ...

    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.generateParseException(CobolParser.java:43571)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.jj_consume_token(CobolParser.java:43405)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.ProgramIdParagraph(CobolParser.java:3925)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.IdentificationDivision(CobolParser.java:3741)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.ProgramUnit(CobolParser.java:3527)
    at net.sourceforge.pmd.lang.cpd.cobol.ast.CobolParser.CompilationUnit(CobolParser.java:3450)
    at net.sourceforge.pmd.lang.cobol.CobolParser.parse(CobolParser.java:33)
    ... 39 more

COBOL 代码

   IDENTIFICATION DIVISION.
  *
   PROGRAM-ID.    GETNUMT.
  *
   DATE-WRITTEN.  04/04/90.
  *
  *    MODIFIED   10/18/97.
  *
  *        *******************************
  *        *                             *
  *        *     Judson D. McClendon     *
  *        *     Sun Valley Systems      *
  *        *     329 37th Court N.E.     *
  *        *     Birmingham, AL 35215    *
  *        *        205-853-8440         *
  *        *                             *
  *        *******************************
  *
   ENVIRONMENT DIVISION.
  *
   CONFIGURATION SECTION.
  *
   INPUT-OUTPUT SECTION.
  *
   FILE-CONTROL.
  *
   I-O-CONTROL.
  *
   DATA DIVISION.
  *
   FILE SECTION.
  *
  *
   WORKING-STORAGE SECTION.
  *
   77  WS-ESCAPE-FLAG              PIC  9(01)     COMP VALUE 0.
   77  WS-ERR-MSG                  PIC  X(30)          VALUE SPACES.
  *
   COPY GETNUMW.COB.
  *
   SCREEN SECTION.
  *
  *
  *                     I N P U T   S C R E E N
  *
   01  INPUT-SCREEN
           FOREGROUND-COLOR 7
           BACKGROUND-COLOR 0.
       03  BLANK SCREEN
           LINE 01  COLUMN 31  VALUE "G E T   N U M B E R".
  *
       03  LINE 03  COLUMN 01  VALUE "Enter Number: ".
       03  PIC  X(25)          USING NW-WORK-NBR.
  *
  *
  *                    O U T P U T   S C R E E N
  *
   01  OUTPUT-SCREEN
           FOREGROUND-COLOR 7
           BACKGROUND-COLOR 0.
  *
       03  LINE 05  COLUMN 01  VALUE "      Result: ".
       03  PIC  -(13).9(06)    FROM  NW-EXTRACTED-NBR.
       03           COLUMN 41
           PIC  X(30)          FROM  WS-ERR-MSG.
  /
   PROCEDURE DIVISION.
  *
  *
  *             C O N T R O L   S E C T I O N
  *
   000000-CONTROL.
  *
       MOVE SPACES TO NW-WORK-NBR.
       DISPLAY INPUT-SCREEN.
  *
       PERFORM 000100-PROCESS
          THRU 000100-EXIT
           UNTIL (WS-ESCAPE-FLAG = 1).
  *
   000000-EXIT.
       STOP RUN.
  *
  *                    P R O C E S S
  *
   000100-PROCESS.
  *
       ACCEPT INPUT-SCREEN
           ON ESCAPE
               MOVE 1 TO WS-ESCAPE-FLAG
               GO TO 000100-EXIT.
  *
       PERFORM 003000-GET-NBR
          THRU 003000-EXIT.
  *
       IF (NW-NBR-ERROR-FLAG = 1)
           MOVE "NUMBER INVALID" TO WS-ERR-MSG
       ELSE
           MOVE SPACES           TO WS-ERR-MSG.
  *
       DISPLAY OUTPUT-SCREEN.
  *
   000100-EXIT.
       EXIT.
  *
   COPY GETNUMP.COB.

我不是 COBOL 程序员,因此,我不明白这个语法有什么问题。

谁能帮我?

4

2 回答 2

3

许多人低估了构建符合标准的 COBOL 编译器或解析器的复杂性。这真的很难,比大多数其他语言都要难。

我认为这里的基本问题是您使用的 JavaCC 语法仅适用于在后期“文本操作”阶段解析 COBOL 源。

典型的 COBOL 编译器有一个预处理步骤,其中处理注释和编译器指令语句以生成适合后续解析的源文本。“文本操作”阶段删除注释行、处理续行、处理COPY指令并构建编译器指令以指导后续编译。

您尝试解析的程序仍然包含注释行(第 7 列中带有“*”的行)和几个COPY指令。除非您删除这些或构建一个预编译器来完成编译的文本操作阶段,否则您所拥有的语法不会很有用——除了最琐碎的程序。

真正的坏消息是,我已经看到许多适用于后期文本操作阶段解析的 COBOL 语法,但几乎没有任何内容可用于执行使它们有用所需的文本操作!

注意: JavaCC 语法包含CommentLine()但这些是针对 COBOL 注释条目的,它们是在一些较旧的 COBOL 程序中发现的过时的编码结构。这不会解析 COBOL 注释行(作者或此语法的错误命名选择)。如前所述,COBOL 注释行将在文本操作期间被删除。

于 2012-11-13T21:58:30.183 回答
1

它似乎在为评论而苦苦挣扎。
COBOL 语句是“列敏感的”,尽管我看不到语法将能够识别列对齐。

  • 注释应该是第 7 列中的 *
  • 分部和节标题从第 8 列开始,例如“IDENTIFICATION DIVISION”
  • 其他陈述从第 12 列开始

奇怪的是它在“[no filename].cbl”上报告错误,是因为您没有保存在 Eclipse 中正在解析的文件吗?

尝试对齐列并删除注释。

于 2012-11-13T19:15:34.807 回答