代码之家  ›  专栏  ›  技术社区  ›  David Brown Muad'Dib

我能改进这个语法吗?

  •  1
  • David Brown Muad'Dib  · 技术社区  · 14 年前

    我必须解析一个如下所示的文件:

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

    "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行测试文件。然而,这是我第一次写一个自定义语法,所以我不确定我做的是否正确。

    1 回复  |  直到 12 年前
        1
  •  4
  •   V M Rakesh    14 年前

    下面是一些为了更好的表现我要求改变的地方

    节列表::=节

               |   SectionList Section
    

                | PropertyList Property
    

    SectionBody::=属性列表

               |  SectionList
    
               |  PropertyList SectionList
    

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

    当做,

    推荐文章