Skip to main content

fury license pyversions travis landscape


At least for the people who send me mail about a new language that they’re designing, the general advice is: do it to learn about how to write a compiler. Don’t have any expectations that anyone will use it, unless you hook up with some sort of organization in a position to push it hard. It’s a lottery, and some can buy a lot of the tickets. There are plenty of beautiful languages (more beautiful than C) that didn’t catch on. But someone does win the lottery, and doing a language at least teaches you something.
Dennis Ritchie (1941-2011)

Creator of the C programming language and of Unix

Grako

NOTE: DEVELOPMENT OF GRAKO HAS STOPPED

If you’re interested in the technology, or in future developments such as proper support for left recursion, then please follow the Tatsu project.


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 can also compile a grammar stored in a string into a Grammar object that can be used to parse any given input, much like the re module does with regular expressions.

Grako is different from other PEG parser generators:

  • Generated parsers use Python’s very efficient exception-handling system to backtrack. Grako generated parsers simply assert what must be parsed. There are no complicated if-then-else sequences for decision making or backtracking. Memoization allows going over the same input sequence several times in linear time.

  • Positive and negative lookaheads, and the cut element (with its cleaning of the memoization cache) allow for additional, hand-crafted optimizations at the grammar level.

  • Delegation to Python’s re module for lexemes allows for (Perl-like) powerful and efficient lexical analysis.

  • The use of Python’s context managers considerably reduces the size of the generated parsers for code clarity, and enhanced CPU-cache hits.

  • Include files, rule inheritance, and rule inclusion give Grako grammars considerable expressive power.

  • Automatic generation of Abstract Syntax Trees and Object Models, along with Model Walkers and Code Generators make analysis and translation approachable

The parser generator, the run-time support, and the generated parsers have measurably low Cyclomatic complexity. At around 5 KLOC of Python, it is possible to study all its source code in a single session.

The only dependencies are on the Python standard library, yet the regex library will be used if installed, and colorama will be used on trace output if available. pygraphviz is required for generating diagrams.

Grako is feature-complete and currently being used with complex grammars to parse, analyze, and translate hundreds of thousands of lines of input text, including source code in several programming languages.

Rationale

Grako was created to address some recurring problems encountered over decades of working with parser generation tools:

  • Some programming languages allow the use of keywords as identifiers, or have different meanings for symbols depending on context (Ruby). A parser needs control of lexical analysis to be able to handle those languages.

  • LL and LR grammars become contaminated with myriads of lookahead statements to deal with ambiguous constructs in the source language. PEG parsers address ambiguity from the onset.

  • Separating the grammar from the code that implements the semantics, and using a variation of a well-known grammar syntax (EBNF) allows for full declarative power in language descriptions. General-purpose programming languages are not up to the task.

  • Semantic actions do not belong in a grammar. They create yet another programming language to deal with when doing parsing and translation: the source language, the grammar language, the semantics language, the generated parser’s language, and the translation’s target language. Most grammar parsers do not check the syntax of embedded semantic actions, so errors get reported at awkward moments, and against the generated code, not against the grammar.

  • Preprocessing (like dealing with includes, fixed column formats, or structure-through-indentation) belongs in well-designed program code; not in the grammar.

  • It is easy to recruit help with knowledge about a mainstream programming language like Python, but help is hard to find for working with complex grammar-description languages. Grako grammars are in the spirit of a Translators and Interpreters 101 course (if something is hard to explain to a college student, it’s probably too complicated, or not well understood).

  • Generated parsers should be easy to read and debug by humans. Looking at the generated source code is sometimes the only way to find problems in a grammar, the semantic actions, or in the parser generator itself. It’s inconvenient to trust generated code that one cannot understand.

  • Python is a great language for working with language parsing and translation.

Documentation

The complete documentation is available at Grako’s home page.

License

Copyright (C) 2017      by Juancarlo Añez
Copyright (C) 2012-2016 by Juancarlo Añez and Thomas Bragg

You may use Grako under the terms of the BSD-style license described in the enclosed LICENSE.txt file. If your project requires different licensing please email.

Changes

See the CHANGELOG for details.

Metadata

Release files for grako 3.99.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for grako 3.99.9
File Size Uploaded
grako-3.99.9.zip 159.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for grako 3.99.9
File Interpreter ABI Platform
grako-3.99.9-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 241.4 kB

Release files / grako-3.99.9.zip

Download URL grako-3.99.9.zip
Size 159.3 kB
Tags Source
SHA-256 checksum
How to use checksums
fcc37309eab7cd0cbbb26cfd6a54303fbb80a00a58ab295d1e665bc69189c364
BLAKE2b-256 checksum
How to use checksums
330d6db911c7f6458974745c91c1e71841e347364798a5cc01e8149e84352c77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / grako-3.99.9-py2.py3-none-any.whl

Download URL grako-3.99.9-py2.py3-none-any.whl
Size 82.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
28813fd09e31edf26761e2f814ef65adc073d432f0f1e4311ae5e5b1519978c2
BLAKE2b-256 checksum
How to use checksums
c91dc10ddc7ccf060695d1cefda9726a2923d45e7a4d39ac08ede3b014e9b33f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

3.99.9 This release

2 release files

3.22.0

2 release files

3.21.1

2 release files

3.21.0

2 release files

3.20.1

2 release files

3.18.1

3 release files

3.16.3

3 release files

3.16.2

3 release files

3.16.1

3 release files

3.15.1

3 release files

3.15.0

3 release files

3.14.0

3 release files

3.13.0

3 release files

3.10.1

3 release files

3.10.0

3 release files

3.9.3

3 release files

3.9.2

3 release files

3.9.1

3 release files

3.9.0

3 release files

3.8.2

1 release file

3.8.1

1 release file

3.8.0

1 release file

3.7.0

1 release file

3.6.8

1 release file

3.6.7

1 release file

3.6.6

1 release file

3.6.5

1 release file

3.6.4

3.6.3

1 release file

3.6.2

1 release file

3.6.1

1 release file

3.6.0

1 release file

3.5.1

1 release file

3.4.3

1 release file

3.4.2

1 release file

3.4.1

1 release file

3.4.0

1 release file

3.3.0

1 release file

3.2.1

1 release file

3.2.0

1 release file

3.1.2

1 release file

3.1.1

1 release file

3.1.0

1 release file

3.0.4

1 release file

3.0.3

1 release file

3.0.1

1 release file

3.0.0

1 release file

2.4.3

1 release file

2.4.2

1 release file

2.4.1

1 release file

2.4.0

1 release file

2.3.0

1 release file

2.2.2

1 release file

2.2.1

1 release file

2.2.0

1 release file

2.1.0

1 release file

2.0.4

1 release file

2.0.3

1 release file

2.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page