2010-07-25 72 views
1

我要解析看起来像这样的文件:我可以改进这个GOLD解析器语法吗?

versioninfo 
{ 
    "editorversion" "400" 
    "editorbuild" "4715" 
} 
visgroups 
{ 
} 
world 
{ 
    "id" "1" 
    "mapversion" "525" 
    "classname" "worldspawn" 
    solid 
    { 
     "id" "2" 
     side 
     { 
      "id" "1" 
      "plane" "(-544 -400 0) (-544 -240 0) (-272 -240 0)" 
     } 
     side 
     { 
      "id" "2" 
      "plane" "(-544 -240 -16) (-544 -400 -16) (-272 -400 -16)" 
     } 
    } 
} 

我必须从头开始编写的解析器,但它有我不能追查一些错误,我想这将是困难的如果格式在未来发生变化,请维持。我决定使用GOLD Parsing System来生成解析器。我的语法如下:

"Start Symbol" = <SectionList> 

! SETS 

{Section Chars} = {AlphaNumeric} + [_] 
{Property Chars} = {Printable} - ["] 

! TERMINALS 

SectionName = {Section Chars}+ 
PropertyPart = '"' {Property Chars}* '"' 

! RULES 

<SectionList> ::= <Section> 
       | <Section> <SectionList> 

<SectionBody> ::= <PropertyList> 
       | <SectionList> 
       | <PropertyList> <SectionList> 

<Section> ::= SectionName '{' '}' 
      | SectionName '{' <SectionBody> '}' 

<PropertyList> ::= <Property> 
       | <Property> <PropertyList> 

<Property> ::= PropertyPart PropertyPart 

没有错误,它解析我的2000行测试文件就好了。然而,这是我第一次编写自定义语法,所以我不确定我是否正确地做了。

我可以对上面的语法做任何改进吗?下面

回答

4

有一些变化,我会要求更改为更好的性能

1)让文法左递归规则。由于黄金分析器是一种减少LR分析器的转换,因此在减少操作方面做得更好。

SectionList :: =第

  | SectionList Section 

对propertyList :: =房产

  | PropertyList Property 

2)在下面的部分,迫使你必须对propertyList只有sectionlist之前,但没有什么不同的之间的第三规则。确保其按规定

SectionBody :: =对propertyList

  | SectionList 

      | PropertyList SectionList 

我可以更好地帮助你,如果需要的,如果你让我知道的语言说:“它应该接受这一点,不应该接受这种”而不是一个不会给出你的语言100%图片的示例输入。或者让我知道我们可以从中定义语言描述的错误。

Regards, V M Rakesh([email protected]

相关问题