简体   繁体   English

我可以改进这个GOLD Parser Grammar吗?

[英]Can I improve this GOLD Parser Grammar?

I have to parse a file that looks like this: 我必须解析一个看起来像这样的文件:

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)"
        }
    }
}

I have a parser written from scratch, but it has a few bugs that I can't track down and I imagine it'll be difficult to maintain if the format changes in the future. 我有一个从头开始编写的解析器,但它有一些我无法追踪的错误,我想如果格式在将来发生变化,将难以维护。 I decided to use the GOLD Parsing System to generate a parser, instead. 我决定使用GOLD Parsing System来生成解析器。 My grammar looks like this: 我的语法看起来像这样:

"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

There are no errors and it parses my 2000-line test file just fine. 没有错误,它解析我的2000行测试文件就好了。 However, this is my first time writing a custom grammar, so I'm not sure if I'm doing it correctly. 但是,这是我第一次编写自定义语法,所以我不确定我是否正确使用它。

Are there any improvements I could make to the grammar above? 我可以对上面的语法做出哪些改进?

below are some changes i would request to change for better performance 以下是我要求更改以获得更好性能的一些更改

1) make the grammar left recursive rules. 1)使语法保持递归规则。 this is better in terms of making shift reduce operations as gold parser is a shift reduce LR parser. 这在制作移位减少操作方面更好,因为黄金解析器是一个移位减少LR解析器。

SectionList ::= Section SectionList :: = Section

           |   SectionList Section

PropertyList ::= Property PropertyList :: = Property

            | PropertyList Property

2) third rule in below section forces you to have propertylist only before sectionlist but not between different 's. 2)以下部分中的第三条规则强制您仅在分区列表之前具有属性列表,而不是在区分列表之间。 make sure its as per requirement 根据要求确定它

SectionBody ::= PropertyList SectionBody :: = PropertyList

           |  SectionList

           |  PropertyList SectionList

i can help you better if required and if you let me know the language saying " it should accept this , shouldn't accept this" rather than a sample input which will not give 100% picture of your language. 如果需要,我可以帮助你更好,如果你让我知道“它应该接受这个,不应该接受这个”的语言,而不是一个不能100%描述你的语言的样本输入。 or let me know the bugs you felt from which we can define the language description also. 或者让我知道您认为我们可以定义语言描述的错误。

Regards, VM Rakesh (rakesh.vm@gmail.com) 此致,VM Rakesh(rakesh.vm@gmail.com)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM