簡體   English   中英

使用Javacc處理COBOL語法中的注釋和行/列號

[英]Handling comments and Line/Column numbers in COBOL grammar using Javacc

我正在使用JavaCC開發 COBOL解析器。 COBOL文件通常將第1至6列作為行/列號。 如果行/列號不存在,它將有空格。

我需要知道如何處理COBOL文件中的注釋和序列區域,並且僅解析主區域。

我嘗試了許多表達式,但是沒有一個起作用。 我創建了一個特殊的令牌,該令牌將檢查新行,然后檢查六次出現的空格或除空格和回車符之外的任何字符,然后第七個字符將是"*"表示注釋,而" "表示正常行。

我正在使用http://java.net/downloads/javacc/contrib/grammars/cobol.jj上的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 :
{
 < ((~[])(~[])(~[])(~[])(~[])(~[])) (" ") >
}

由於解析器從行的開頭開始,因此您應使用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.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM