0

我正在使用JavaCC开发 COBOL 解析器。COBOL 文件通常将第 1 到第 6 列作为行/列编号。如果行/列号不存在,它将有空格。

我需要知道如何处理 COBOL 文件中的注释和序列区域并仅解析主区域。

我尝试了很多表达,但没有一个有效。我创建了一个特殊标记,它将检查新行,然后检查出现的六次空格或除空格和回车之外的任何字符,之后第七个字符将"*"用于注释和" "普通行。

我正在使用此处提供的 Cobol.jj 文件http://java.net/downloads/javacc/contrib/grammars/cobol.jj

谁能建议我应该使用什么语法?

我的语法文件示例:

    PARSER_END(CblParser)

////////////////////////////////////////////////////////////////////////////////
// Lexical structure
////////////////////////////////////////////////////////////////////////////////

SPECIAL_TOKEN :
{
  < EOL: "\n" > : LINE_START 
| < SPACECHAR: ( " " | "\t" | "\f" | ";" | "\r" )+ >
}

SPECIAL_TOKEN :
{
  < COMMENT: ( ~["\n","\r"," "] ~["\n","\r"," "] ~["\n","\r"," "] ~["\n","\r"," "] ~["\n","\r"," "] ~["\n","\r"," "] ) ( "*" | "|" ) (~["\n","\r"])* >
| < PREPROC_COMMENT: "*|" (~["\n","\r"])* >
| < SPACE_SEPARATOR : ( <SPACECHAR> | <EOL> )+ >
| < COMMA_SEPARATOR : "," <SPACE_SEPARATOR> >
}

<LINE_START> SKIP :
{
 < ((~[])(~[])(~[])(~[])(~[])(~[])) (" ") >
}
4

1 回答 1

1

由于解析器从行首开始,您应该使用 DEFAULT 状态来表示行首。我会做类似以下的事情[未经测试的代码如下]。

// At the start of each line, the first 6 characters are ignored and the 7th is used
// to determine whether this is a code line or a comment line.
// (Continuation lines are handled elsewhere.)
// If there are fewer than 7 characters on the line, it is ignored.
// Note that there will be a TokenManagerError if a line has at least 7 characters and
// the 7th character is other than a "*", a "/", or a space.
<DEFAULT> SKIP :
{
   < (~[]){0,6} ("\n" | "\r" | "\r\n") > :DEFAULT
|
   < (~[]){6} (" ") > :CODE
|
   < (~[]){6} ("*"|"/")  :COMMENT
}

<COMMENT> SKIP :
{   // At the end of a comment line, return to the DEFAULT state.
    < "\n" | "\r" | "\r\n" > : DEFAULT
|   // All non-end-of-line characters on a comment line are ignored.
    < ~["\n","\r"] > : COMMENT
}
<CODE> SKIP :
{   // At the end of a code line, return to the DEFAULT state.
    < "\n" | "\r" | "\r\n" > : DEFAULT
|   // White space is skipped, as are semicolons.
    < ( " " | "\t" | "\f" | ";" )+ >
}
<CODE> TOKEN :
{  
    < ACCEPT: "accept" >
|  
    ... // all rules for tokens should be in the CODE state.
}
于 2013-03-14T12:02:33.047 回答