Questions tagged [grako]

Grako (for grammar compiler) is a tool that takes grammars in a variation of EBNF as input, and outputs memoizing (Packrat) PEG parsers in Python.

Grako (for grammar compiler) is a tool that takes grammars in a variation of EBNF as input, and outputs memoizing (Packrat) PEG parsers in Python.

Features:

  • Generated parsers use Python's exception-handling system to backtrack.

  • Positive and negative lookaheads, and the cut element allow for hand-crafted optimizations at the grammar level.

  • Delegation to Python's re module for lexemes allows for Perl-like lexical analysis.

  • Python's context managers reduce the size of the generated parsers.

  • Include files, rule inheritance, and rule inclusion provide expressive power

  • Support for direct and indirect left recursion allows more intuitive grammars.

See also

35 questions
5
votes
1 answer

Grako "code" generation

I am trying to understand how one can re-create a document parsed by a parser generated by grako. After burying myself deep in the grako source code, I believe I have finally understood how one returns from the AST to generated document. Could…
ARF
  • 6,320
  • 5
  • 34
  • 62
5
votes
1 answer

Parsing of optionals with PEG (Grako) falling short?

My colleague PaulS asked me the following: I'm writing a parser for an existing language (SystemVerilog - an IEEE standard), and the specification has a rule in it that is similar in structure to this: cover_point = [[data_type]…
Apalala
  • 8,159
  • 3
  • 26
  • 47
3
votes
1 answer

Do I have a bug in my grammar, or the parser-generation tool?

The following is an EBNF-format (mostly - the actual syntax is documented here) grammar that I am attempting to generate a parser for: expr = lambda_expr_list $; lambda_expr_list = [ lambda_expr_list "," ] lambda_expr; lambda_expr =…
3
votes
1 answer

Source code generation

I am using grako utility of python to parse my OIL file to AST. but i want to re generate the source code back from the AST after modifying the AST. Is grako is having feature to do this or else any other utility in python is available for this…
3
votes
1 answer

How to process the stuctured language file in python

I have a big structured language file like this: TASK SchM_Task { TYPE = AUTO; SCHEDULE = NON; PRIORITY = 160; ACTIVATION = 1; TIMING_PROTECTION = FALSE; AUTOSTART = FALSE; EVENT = SchM_Event; RESOURCE =…
3
votes
2 answers

Rule precedence issue with grako

I'm redoing a minilanguage I originally built on Perl (see Chessa# on github), but I'm running into a number of issues when I go to apply semantics. Here is the grammar: (* integers *) DEC = /([1-9][0-9]*|0+)/; int =…
Aerdan
  • 43
  • 7
2
votes
1 answer

Why does the Grako parsing process fail if my grammar contains an expression that consists of many or-concatenated subexpressions?

I am using Grako. In my EBNF grammar, I have an expression that consists of a lot of subexpressions that are concatenated using the OR-operator, like so: expression = subexpressionA | subexpressionB | ... | subexpressionZ; The parsing process…
Matthias
  • 408
  • 3
  • 14
2
votes
1 answer

Parse one or more expressions with helpful errors

I'm using grako (a PEG parser generator library for python) to parse a simple declarative language where a document can contain one or more protocols. Originally, I had the root rule for document written as: document = {protocol}+ ; This…
shader
  • 676
  • 5
  • 23
2
votes
1 answer

How to describe scopes in EBNF?

I'm trying to write a parser for Cisco IOS and ASA configurations, using Grako and Python. I'm trying to figure out how to represent 'scoped' keywords in EBNF - for example, the 'description' keyword must appear inside an interface scope, but there…
AnotherHowie
  • 687
  • 9
  • 23
2
votes
1 answer

Is it possible to use a with grako generated parser without grako?

see the title. For a small tool I am writing I wanted to introduce a simple boolean filter language and decided to do that "properly" and use a parser-generator. After playing around with grako a bit I found I like it and got the filter-language…
mageta
  • 597
  • 2
  • 6
  • 12
2
votes
1 answer

How to implement combination of rules without repeatings in ebnf grammar?

I am using Grako EBNF/PEG parser. I know that EBNF syntax allows to grab expressions that satisfy one of given options: (a | b | c) Is there a similar part of syntax or a workaround that allows to grab expessions that are any combinations of a, b…
Sashko Lykhenko
  • 1,262
  • 3
  • 15
  • 33
1
vote
1 answer

Cannot define rule priority in grako grammar for handling special tokens

I am trying to analyze some documents by a grammar generated via Grako that should parse simple sentences for further analysis but face some difficulties with some special tokens. The (Grako-style) EBNF looks like: abbr::str = "etc." |…
1
vote
1 answer

Whitespace handling in grako when regular expressions are involved

I'm trying to write a grako flavored ebnf grammar. I noticed the generated parser does not seem to advance over whitespaces or comments, when trying to parse a regular expression. The documentation says the following on that topic Unlike other…
G.G
  • 615
  • 4
  • 12
1
vote
1 answer

"FailedParse: [...] Expecting end of text" when trying to parse parenthesized expressions in grako

In search_query.ebnf, I have the following grammar definition for grako 3.14.0: @@grammar :: SearchQuery start = search_query $; search_query = parenthesized_query | combined_query | search_term; parenthesized_query = '(' search_query…
das-g
  • 8,581
  • 3
  • 33
  • 72
1
vote
1 answer

Basic Grako example gives IndexError

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)…
Gerenuk
  • 10,513
  • 16
  • 48
  • 83
1
2 3