1

I'd like to get started with Grako (3.6.6) and as a first experience with parsers I wanted to generate an HTML table from a custom syntax. The following basic test

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)

results in an error

  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

which happens to be partial_match = (token[0].isalpha() and token.isalnum() and self.is_name_char(self.current()) )

Despite me being new to parsers and a little lack of documentation, I'd like to stick to Grako.

Can you help me set up a basic example which outputs the HTML for a table?

Gere
  • 12,075
  • 18
  • 62
  • 94

1 Answers1

2

Grako is not seeing the "\n" in the grammar correctly because newlines are not allowed in tokens, and the \n is being evaluated in the context of the outer, triple-quote ("""), string. Things work fine if you use /\n/ instead.

Also note that if \n will be part of the language, then you should probably write a @@whitespace clause so the parser doesn't skip over the character:

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

This is the correct grammar for your language:

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

I'm currently patching Grako to detect and report errors like the one in your grammar. The changes are already in the Bitbucket repository. I'll make a release after I finish testing.

Apalala
  • 9,017
  • 3
  • 30
  • 48
  • Oh, I see. Thanks! Could you point me to an example (with code line) where the AST is translated into some string output (Renderer, right?)? I've looked through the linked examples, but it's hard to extract the relevant part without having much experience with parsing. – Gere Jan 15 '16 at 22:23
  • For complex translations I use ``grako.model``. But the ASTs produced by **Grako** are easy to use in several different ways, so I'd suggest you _role your own_ for your simple language. That will let you learn what you want as support for translation in a library, and thus let you approach ``grako.model`` more easily. – Apalala Jan 20 '16 at 02:55