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

36 questions
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
  • 9,718
  • 4
  • 38
  • 80
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)…
Gere
  • 12,075
  • 18
  • 62
  • 94
1
vote
1 answer

Link error when installing grako in Python

I'm trying to install the package grako from PyPI which apparently compiles stuff with Cython. I don't know what this process means and it breaks with an unresolved symbol. Maybe someone can give me a hint what to check for to make this work? Here…
Gere
  • 12,075
  • 18
  • 62
  • 94
1
vote
1 answer

How can I express this format in EBNF?

I have the following data: dbCon= { main = { database = "db1", hostname = "db1.serv.com", maxConnCount = "5", port = "3306", slaves = [ { charset = "utf8", …
MrDuk
  • 16,578
  • 18
  • 74
  • 133
1
vote
0 answers

grako ebnf tags at new line; ingoring spaces

Trying to make a parser with grako to parse something like tag1: line1 line 2 ... tag2: line1 line2 ... such that whitespaces before tag1 should be ignored; but tags should start at new line. Small ebnf (x.ebnf) START = DATA $; DATA =…
user3177400
  • 71
  • 1
  • 2
1
vote
1 answer

Breaking lexical elements into pieces

My grammar file test.ebnf looks like, start = identifier ; identifier = /[a-z]*/ rest; rest = /[0-9]*/ ; When I run this grammar in the input "test1234", I want it to yield "test1234" as a single lexeme, but instead the AST looks…
Charles
  • 953
  • 1
  • 8
  • 19
0
votes
1 answer

How to implement the priority of expressions Bakus-Naur Form

There is a grammar of this kind described in the documentation: grammar = | ['()'] ['$'] {'#' &'#'} '#' | ['()'] {'#' &'#'} '#%' | ['()'] ['$'] {'0' &'0'} '0' | ['()'] {'0' &'0%'} '0%' | ['()'] ['$'] {'#' &'0'} {'0' &'0'}…
MadInc
  • 1
  • 3
0
votes
0 answers

Grako's ordered match

@@grammar::tester @@comments :: /\(\*((?:.|\n)*?)\*\)/ @@eol_comments :: /(#([^\n]*?)$|\/\/([^\n]*?)$)/ start = pattern $; pattern = | number | anything ; anything = ?'\S*'; number =…
user40129
  • 763
  • 1
  • 5
  • 15
0
votes
1 answer

Grako left recursion

I'm trying to use grako to describe a simple left-recursive grammar but I have trouble to do so. Right-recursion does work without any problem : symbol = /[a-z]/ ; condition = symbol "AND" condition | symbol ; start = condition $ ; According to all…
Jbb
  • 483
  • 3
  • 15
0
votes
1 answer

Context sensitve code generation with grako

I'm in a situation where I've built an abstract syntax tree (AST) with grako's model builder semantics. Now I need to generate javascript code from that AST. I have defined several templates, but I realized not all cases can be handled with simple…
G.G
  • 899
  • 6
  • 15
0
votes
1 answer

Avoiding nested objects using ModelBuilderSemantics in Grako

If you take a look at the grammar below, you can see a primary rule, expression, which gets parsed into more specific expression types. expression::Expression = or_ex:and_expr {'||' or_ex:and_expr}+ | andex:and_expr ; and_expr::AndExpression = …
pgebhard
  • 59
  • 1
  • 10
0
votes
1 answer

Ignoring empty elements matched by {} in recursive rule

I would like to describe a nestable condition. Here is what I'm working with : expr = ( /[_a-zA-Z][a-zA-Z0-9_-]*/ ) ; condop = ( "AND" | "OR" ) ; condition = expr { condop condition } ; start = condition ; I can generate an AST with lines like…
Jbb
  • 483
  • 3
  • 15
0
votes
1 answer

Improving errors output by Grako-generated parser

I'm trying to figure out the best approach to improving the errors displayed to a user of a Grako-generated parser. It seems like the default parse errors displayed by the Grako-generated parser when it hits some parsing issue in the input file are…
pgebhard
  • 59
  • 1
  • 10
0
votes
1 answer

node label/key in XText when translating from grako

In grako one can use the following name:e to add the result of e to the AST using name as key. For example var_def = var+:ID {',' var+:ID}* What would be a good translation of this to Xtext? I tried var_def: var=ID (','…
Quantico
  • 2,398
  • 7
  • 35
  • 59
0
votes
1 answer

Augmented Abstract Syntax Tree

Here is a simple grammar: START = DECL DECL $ ; DECL = TYPE NAME '=' VAL ; TYPE = 'int' | 'float' ; NAME = 'a' | 'b' ; VAL = '4' ; I parse this input stream with Grako: int a = 4 float b = 4 and I retrieve this abstract syntax tree (JSON): [ …
LD_FLO
  • 23
  • 4