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
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…
Gerenuk
  • 10,513
  • 16
  • 48
  • 83
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
  • 12,340
  • 13
  • 55
  • 113
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
  • 733
  • 6
  • 16
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
  • 3
  • 3
0
votes
1 answer

How to implement this kind of EBNF grammar (lookahead)?

I am trying to parse the string "###" using an EBNF grammar in TatSu (grako) of this kind: grammar = """mask = | ['()'] ['$'] {'#'} '#' | ['()'] {'#'} '#%' | ['()'] ['$'] {'#'} {'0'} '0' '.#'…
MadInc
  • 3
  • 3
0
votes
1 answer

How to get concise syntax error messages from grako/TatSu

If the input to a grako/tatsu generated parser has a syntax error, such as 3 + / 3 to the calc.py examples, one gets a long list of Python calling sequences in addition to the relevant 3 + / 3 ^ I could use try - except constructions but then…
koskenni
  • 53
  • 4
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
  • 645
  • 5
  • 14
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…
J1bz
  • 191
  • 1
  • 11
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
  • 615
  • 4
  • 12
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
  • 39
  • 1
  • 9
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…
J1bz
  • 191
  • 1
  • 11
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
  • 39
  • 1
  • 9
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,224
  • 5
  • 28
  • 55
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
  • 3