GOLD Parser评论语法

时间:2012-07-17 01:32:30

标签: grammar bnf dfa gold-parser

我的语法中的注释块有些麻烦。语法很好,但是第3步DFA扫描仪抱怨我的方式。

我试图解析的语言如下:

{statement} {statement}等。

在每个陈述中可以有几种不同类型的评论:

{% This is a comment.
It can contain multiple lines
and continues until the statement end}

{statement  REM This is a comment.  
It can contain multiple lines  
and continues until the statement end}

这是一个简化的语法,显示我遇到的问题:

"Start Symbol" = <Program>

{String Chars} = {Printable} + {HT} - ["\]
StringLiteral = '"' ( {String Chars} | '\' {Printable} )* '"'

Comment Start = '{%'
Comment End = '}'
Comment Block @= { Ending = Closed }  ! Eat the } and produce an empty statement
!Comment @= { Type = Noise }  !Implied by GOLD

Remark Start = 'REM'
Remark End = '}'
Remark Block @= { Ending = Open }  ! Don't eat the }, the statements expects it
Remark @= { Type = Noise }

<Program> ::= <Statements>
<Statements> ::= '{' <Statement> '}' <Statements> |  <>
<Statement> ::= StringLiteral

第3步是抱怨}&lt;语句&gt;和}为词汇组的结尾。

任何人都知道如何完成我的需要吗?

[编辑]
我让REM部分使用以下内容:

{Remark Chars} = {Printable} + {WhiteSpace} - [}]
Remark = 'REM' {Remark Chars}* '}'
<Statements> ::= <Statements> '{' <Statement> '}'
              |  <Statements> '{' <Statement> <Remark Stmt>
              |  <>
<Remark Stmt> ::= Remark

这实际上是理想的,因为备注对我来说不一定是噪音。

评论词汇组仍有问题。我会以同样的方式看待解决方案。

1 个答案:

答案 0 :(得分:1)

我不认为可以使用词汇组捕获REM评论。

我认为你需要定义一个像这样的新终端:

Remark = 'REM' ({Printable} - '}')*

但这意味着您需要能够在制作中处理这个新终端......

EG。 从:

<CurlyStatement> ::= '{' <Statement> '}'

要:

<CurlyStatement> ::= '{' <Statement> '}'
                   | '{' <Statement> Remark '}'

我没有检查上面例子中的语法,但我希望你能得到我的想法