我想开始使用Grako (3.6.6),作为对解析器的第一次体验,我希望通过自定义语法生成一个HTML表。以下基本测试
import grako
grammar = """table = { row }+ ;
row = (cell1:cell "|" cell2:cell) "\n";
cell = /[a-z]+/ ;
"""
model = grako.genmodel("model", grammar)
ast = model.parse(
"""a | b
c | d
""", "table")
print(ast)导致一个错误
File "test.py", line 13, in <module>
""", "table")
File "grako\grammars.py", line 790, in grako.grammars.Grammar.parse (grako\grammars.c:27773)
File "grako\grammars.py", line 97, in grako.grammars.GrakoContext.parse (grako\grammars.c:4391)
File "grako\contexts.py", line 180, in grako.contexts.ParseContext.parse (grako\contexts.c:4313)
File "grako\grammars.py", line 594, in grako.grammars.Rule.parse (grako\grammars.c:22253)
File "grako\grammars.py", line 597, in grako.grammars.Rule._parse_rhs (grako\grammars.c:22435)
File "grako\contexts.py", line 399, in grako.contexts.ParseContext._call (grako\contexts.c:10088)
File "grako\contexts.py", line 433, in grako.contexts.ParseContext._invoke_rule (grako\contexts.c:11135)
File "grako\grammars.py", line 435, in grako.grammars.PositiveClosure.parse (grako\grammars.c:17285)
File "grako\contexts.py", line 695, in grako.contexts.ParseContext._positive_closure (grako\contexts.c:19286)
File "grako\contexts.py", line 696, in grako.contexts.ParseContext._positive_closure (grako\contexts.c:19240)
File "grako\grammars.py", line 435, in grako.grammars.PositiveClosure.parse.lambda10 (grako\grammars.c:17195)
File "grako\grammars.py", line 547, in grako.grammars.RuleRef.parse (grako\grammars.c:20774)
File "grako\grammars.py", line 594, in grako.grammars.Rule.parse (grako\grammars.c:22253)
File "grako\grammars.py", line 597, in grako.grammars.Rule._parse_rhs (grako\grammars.c:22435)
File "grako\contexts.py", line 399, in grako.contexts.ParseContext._call (grako\contexts.c:10088)
File "grako\contexts.py", line 433, in grako.contexts.ParseContext._invoke_rule (grako\contexts.c:11135)
File "grako\grammars.py", line 326, in grako.grammars.Sequence.parse (grako\grammars.c:11582)
File "grako\grammars.py", line 268, in grako.grammars.Token.parse (grako\grammars.c:9463)
File "grako\contexts.py", line 543, in grako.contexts.ParseContext._token (grako\contexts.c:13772)
File "grako\buffering.py", line 301, in grako.buffering.Buffer.match (grako\buffering.c:9168)
IndexError: string index out of range恰巧是partial_match = (token[0].isalpha() and token.isalnum() and self.is_name_char(self.current()) )
尽管我对解析器还不熟悉,也有点缺乏文档,但我还是想继续使用Grako。
你能帮我建立一个基本的例子来输出表格的HTML吗?
发布于 2016-01-15 13:21:37
Grako在语法中没有正确地看到"\n",因为在令牌中不允许换行符,并且\n是在外部的三元引号(""")字符串的上下文中计算的。如果您使用/\n/,那么一切都很好。
还请注意,如果\n将是语言的一部分,那么您可能应该编写一个@@whitespace子句,这样解析器就不会跳过字符:
@@whitespace :: /[\t ]+/这是你的语言的正确语法:
grammar = """
@@whitespace :: /[\t ]+/
table = { row }+ ;
row = (cell1:cell "|" cell2:cell) "\\n";
cell = /[a-z]+/ ;
"""我目前正在修补Grako,以检测和报告语法中的错误。这些更改已经在比特桶存储库中了。测试结束后我会发布一个版本。
https://stackoverflow.com/questions/34787992
复制相似问题