簡體   English   中英

Grako基本示例給出IndexError

[英]Basic Grako example gives IndexError

我想開始使用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的基本示例嗎?

Grako不能正確看到語法中的"\\n" ,因為令牌中不允許換行,並且在外部三重引號( """ )字符串的上下文中對\\n進行了評估。您使用/\\n/代替。

還要注意,如果\\n將成為語言的一部分,那么您可能應該編寫一個@@whitespace子句,以便解析器不會跳過該字符:

@@whitespace :: /[\t ]+/

這是您所用語言的正確語法:

grammar = """
@@whitespace :: /[\t ]+/
table = { row }+ ;
row = (cell1:cell "|" cell2:cell) "\\n";
cell = /[a-z]+/ ;
"""

我目前正在修補Grako,以檢測並報告語法中的錯誤。 更改已在Bitbucket存儲庫中。 完成測試后,我將發布一個版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM