我可以改进这个 GOLD Parser Grammar 吗?
我必须解析一个如下所示的文件:
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 解析系统来生成解析器。我的语法如下所示:
"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 行测试文件。然而,这是我第一次编写自定义语法,所以我不确定我是否做得正确。
我可以对上面的语法进行任何改进吗?
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. 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. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是我要求更改以获得更好性能的一些更改
1)使语法左递归规则。这在进行移位归约操作方面更好,因为黄金解析器是移位归约 LR 解析器。
SectionList ::= Section
PropertyList ::= Property
2) 下面部分中的第三条规则强制您仅在sectionlist之前拥有propertylist,但不在不同的之间拥有propertylist。确保它符合要求
SectionBody ::= PropertyList
如果需要的话我可以更好地帮助你,如果你让我知道语言说“它应该接受这个,不应该接受这个”而不是一个不会给出100%的示例输入你的语言的图片。或者让我知道您感受到的错误,我们也可以从中定义语言描述。
问候,
VM Rakesh([电子邮件受保护])
below are some changes i would request to change for better performance
1) make the grammar left recursive rules. this is better in terms of making shift reduce operations as gold parser is a shift reduce LR parser.
SectionList ::= Section
PropertyList ::= Property
2) third rule in below section forces you to have propertylist only before sectionlist but not between different 's. make sure its as per requirement
SectionBody ::= PropertyList
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. or let me know the bugs you felt from which we can define the language description also.
Regards,
V M Rakesh ([email protected])