Marcio Barroso
Marcio Barroso

Reputation: 783

COBOL Parser using javacc + PMD

I am using this grammar to generate a COBOL parser.

I use javacc to generate the parser and Java Tree Builder to generate the AST from this grammar.

I am customizing a eclipse plugin called PMD to read a COBOL file and generate the AST. There is a UI called Designer that allow you to fill a AST from a source code.

When I try using the source code bellow, I have a error message:

ERROR MESSAGE

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 CODE

   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.

I am not a COBOL programmer and because of that, I did not understand what is wrong in this grammar.

Can any one help me?

Upvotes: 0

Views: 1183

Answers (2)

NealB
NealB

Reputation: 16928

Many under estimate the complexity of building a conforming COBOL compiler or parser. This is really hard stuff, much harder than for most other languages.

I think the basic problem here is that the JavaCC grammar you are using is only appropriate for parsing COBOL source in post "text manipulation" phase.

A typical COBOL compiler has a pre-step where comments and compiler directing statements are processed to generate a source text suitable for subsequent parsing. The "text manipulation" phase drops comment lines, processes continuation lines, processes COPY directives and builds compiler directives to guide subsequent compilation.

The program you are trying to parse still contains comment lines (the lines with '*' in column 7) and a couple of COPY directives. Until you either remove these or build a pre-compiler to complete the text manipulation phase of compilation, the grammar you have will not be very useful - except on the most trivial of programs.

The really bad news is that I have seen many COBOL grammars suitable for post text manipulation phase parsing, but virtually nothing for conducting the text manipulation needed to make them useful!

Note: The JavaCC grammer contains CommentLine() but these are for COBOL Comment Entries, which are obsolete coding constructs found in some older COBOL programs. This does not parse a COBOL comment line (bad choice of naming by the author or this grammar). As noted earlier COBOL comment lines would have been removed during text manipulation.

Upvotes: 3

crowne
crowne

Reputation: 8534

It seems to be struggling with the comments.
COBOL statements are "Column Sensitive", although I can't see that the grammar is going to be able to recognise column alignment.

  • comments should be an * in column 7
  • division and section headers start in column 8 e.g "IDENTIFICATION DIVISION"
  • other statements start in column 12

Its strange that it is reporting an error on "[no filename].cbl", is that because you haven't saved the file that is being parsed in Eclipse?

Try aligning the columns and removing the comments.

Upvotes: 1

Related Questions