【问题标题】:Throw error for unclosed comment block javacc未关闭的注释块 javacc 引发错误
【发布时间】:2015-10-23 01:37:17
【问题描述】:

我正在 javacc 中创建一个词法分析器,它会跳过以 /* 开头并以 */ 结尾的块 cmets。我让它对有效的块 cmets 正常工作,但我试图找出一种在块注释未关闭时引发错误的方法......

例子:

/* this is not a valid block comment
/* this is a valid block comment*/

这是我必须跳过有效块 cmets 的内容:

MORE: { <"/*"> : BLC_CMNT_ST}
<BLC_CMNT_ST> SKIP: { <"*/">: DEFAULT >
<BLC_CMNT_ST> MORE: { <~[]>}

目前,当我运行词法分析器时,如果有未关闭的块注释,则会引发 TokenMgrError。我想捕捉这个错误和/或抛出我自己的错误,显示matchedToken.image。我尝试了几种不同的方法,但都遇到了问题,因此非常感谢您的帮助

【问题讨论】:

    标签: comments lexer javacc


    【解决方案1】:

    怎么样

    SKIP: { <"/*"> : BLC_CMNT_ST}
    <BLC_CMNT_ST> SKIP: { "*/" : DEFAULT  }
    <BLC_CMNT_ST> SKIP: { < ~[] > }
    
    <*> TOKEN : { <EOF>
        { System.out.println("Lexical state is " + curLexState ) ; 
        if(curLexState==BLC_CMNT_ST) throw new Error("Unmatched comment at end of file.") ; } }
    

    由于我不完全理解的原因,我不得不使用 SKIP 而不是 MORE。

    如果你想在块 cmets 中禁止“/*”,你可以添加这个产生式

    <BLC_CMNT_ST> TOKEN: { < "/*" > 
        { if(true) throw new Error("Unmatched comment at line "
                       + matchedToken.beginLine
                       + ", column "
                       + matchedToken.beginColumn + ".") ; } }
    

    很遗憾,此解决方案无法让您访问评论的图像。

    【讨论】:

      猜你喜欢
      • 2015-10-22
      • 1970-01-01
      • 2018-05-22
      • 2015-10-25
      • 1970-01-01
      • 2022-12-22
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      相关资源
      最近更新 更多