Cobol 的语法帮助

Syntax Help for Cobol

我目前正在为我的 Cobol class 编写一个程序来计算学生的学费。但是,我不断收到语法错误:

   jdoodle.cobc: in Paragraph '100-MAIN-Module':
   jdoodle.cobc:33: error: syntax error, unexpected END
   jdoodle.cobc:36: error: syntax error, unexpected END
   jdoodle.cobc:40: error: syntax error, unexpected END-PERFORM

我试过将其删除并移到别处,检查了拼写,但似乎无法消除这些错误。

   IDENTIFICATION DIVISION.
   PROGRAM-ID. Tuition.
   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
       SELECT IN-STUDENT-FILE 
           ASSIGN TO 'name.dat'
           ORGANIZATION IS LINE SEQUENTIAL.    
       SELECT OUT-STUDENT-FILE 
           ASSIGN TO 'tuition.dat'
           ORGANIZATION IS LINE SEQUENTIAL.
   DATA DIVISION.
   FILE SECTION.
   FD IN-STUDENT-FILE.
   01 IN-STUDENT-REC.
       05 STUDENTNAME-IN  PICTURE X(20).
       05 NUMCRED-IN      PICTURE 99.
       05                 PICTURE X(58).
   FD OUT-STUDENT-FILE.
   01 OUT-STUDENT-REC.
       05 STUDENTNAME-OUT PICTURE X(20).
       05                 PICTURE X(20).
       05 NUMCRED-OUT     PICTURE 99.
       05                 PICTURE X(20).
       05 TUITION-OUT     PICTURE X(4).
   WORKING-STORAGE SECTION.
   01 TUITION             PICTURE 9(4).
   01 EOF                 PICTURE X.
   PROCEDURE DIVISION.
   100-MAIN-MODULE.
       OPEN INPUT IN-STUDENT-FILE
            OUTPUT OUT-STUDENT-FILE
        PERFORM UNTIL END OF FILE = 'YES'
           READ IN-STUDENT-FILE
               AT END
                   MOVE 'YES' TO END OF FILE
               NOT AT END
                   PERFORM 200-PROCESS-RTN
           END-READ.
        END-PERFORM.
         CLOSE IN-STUDENT-FILE
               OUT-STUDENT-FILE
         STOP RUN.
   200-PROCESS-RTN.
       MOVE STUDENTNAME-IN TO STUDENTNAME-OUT
       MOVE NUMCRED-IN TO NUMCRED-OUT
       IF NUMCRED-IN < 12 THEN
           MULTIPLY NUMCRED-IN BY 525 GIVING TUITION
       ELSE
           SET TUITION TO 6300
       END-IF
       MOVE TUITION TO TUITION-OUT
       DISPLAY OUT-STUDENT-REC
       WRITE OUT-STUDENT-REC.

我在 class 的第三周,除了给我们的示例程序外,这是我第一次尝试编写。

您使用一个名为 END OF FILE 的 "variable"。
对于 COBOL,这是一个无效的名称,因为它包含保留字 END,请改用 END-OF-FILE,您会遇到另一个错误,并有一条更好的消息告诉您 END-OF-FILE 未定义;这样做,你应该可以走得更远。

两件事。首先,如前所述,字段名称中不能有空格。第二:句号结束一个陈述而不是一个动词。除非必须,否则不要使用它们,这只是在段落的末尾;范围终止符本身就很好,除非它在段落的最后一个语句中。

  PERFORM UNTIL END-OF-FILE = 'YES'
     READ IN-STUDENT-FILE
        AT END
           MOVE 'YES' TO END-OF-FILE
        NOT AT END
           PERFORM 200-PROCESS-RTN
     END-READ. <- This period ends the statement not just the read
  END-PERFORM.  <- leaving this hanging

您可以更改此代码的多个不同方面。首先,对资本化没有具体要求。可以使用小写字母,我个人认为这样会容易很多。

你也不需要使用'picture',这可以缩写为'pic',节省开发时间。

不需要使用名为 'END OF FILE' 的变量,简单地制作一个标志并将其初始化为 false 开始要容易得多,然后才能在需要时对其进行操作,88 级变量非常适合这个。

同样,这完全取决于个人喜好,但我不喜欢使用段落并坚持使用部分,我觉得这样可以更轻松地管理程序的结构。

在您的代码中,您在读取结束时使用了句号,句号本身将被视为 'end-read' 因此,由于您已经有了句号,编译器正在读取第一个完整的句号-stop as 'end-perform' 并为此抛出错误。我使用句号的方式是在每个部分的末尾都有一个句号,清晰可见,这样我就知道它在那里。

我已经按照我编写代码的方式重构了您的代码。请看看这个,看看你的想法。

identification division.
program-id. Tuition.
environment division.
input-output section.
file-control.
   select in-student-file
       assign to 'name.dat'
       organization is line sequential.    
   select out-student-file 
       assign to 'tuition.dat'
       organization is line sequential.

data division.    
file section.    
   fd in-student-file.    
      01 in-student-rec.
         05 studentname-in  pic x(20).
         05 numcred-in      pic 99.
         05                 pic X(58).    

   fd out-section-file.    
      01 out-student-rec.
         05 studentname-out pic X(20).
         05                 pic X(20).
         05 numcred-out     pic 99.
         05                 pic X(20).
         05 tuition-out     pic X(4).    
working-storage section.    
   01  tuition              pic 9(4).   
   01  ws-file-at-end       pic x.
       88 fl-eof            value "Y" false "N". 

procedure division.

*************************************************
*********Structure of programme******************
*************************************************    
main section.
   perform startup
   perform main-process until fl-eof-true
   perform closedown
   go-back
   .

*************************************************
***********Open necessary files******************
*************************************************
startup section.
   open input in-student-file
   open output out-student-file
   set fl-eof to false
   .

*************************************************
********Reads until flag end of file*************
*************************************************
main-process section.
   read in-student-file
      at end 
        set fl-eof to true
      not at end
        perform process-and-return
   end-read
   .

*************************************************
******Process the student tuition****************
*************************************************
process-and-return section.
   move studentname-in to studentname-out
   move numcred-in to numcred-out

   if numcred-in < 12 then
      multiply numcred-in by 525 giving tuition
   else
      set tuition to 6300
   end-if

   move tuition to tuition-out
   display out-student-rec
   write out-student-rec
   .

*************************************************
******Close all necessary files******************
*************************************************
closedown section.
   close in-student-file
   close out-student-file
   .