2010-07-25 69 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]

相關問題