Package: grako
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 theCprogramming language and ofUnixGrakoNOTE:DEVELOPMENT OF GRAKO HAS STOPPEDIf you’re interested in the technology, or in future developments such as proper support for left recursion, then please follow theTatsuproject.Grako(forgrammar compiler) is a tool that takes grammars in a variation ofEBNFas input, and outputsmemoizing(Packrat)PEGparsers inPython.Grakocan also compile a grammar stored in a string into aGrammarobject that can be used to parse any given input, much like theremodule does with regular expressions.Grakoisdifferentfrom otherPEGparser generators:Generated parsers usePython’s very efficient exception-handling system to backtrack.Grakogenerated parsers simply assert what must be parsed. There are no complicatedif-then-elsesequences for decision making or backtracking. Memoization allows going over the same input sequence several times in linear time.Positive and negative lookaheads, and thecutelement (with its cleaning of the memoization cache) allow for additional, hand-crafted optimizations at the grammar level.Delegation toPython’sremodule forlexemesallows for (Perl-like) powerful and efficient lexical analysis.The use ofPython’scontext managersconsiderably reduces the size of the generated parsers for code clarity, and enhanced CPU-cache hits.Include files, rule inheritance, and rule inclusion giveGrakogrammars considerable expressive power.Automatic generation ofAbstract Syntax Treesand Object Models, along withModel WalkersandCode Generatorsmake analysis and translation approachableThe parser generator, the run-time support, and the generated parsers have measurably lowCyclomatic complexity.  At around 5KLOCofPython, it is possible to study all its source code in a single session.The only dependencies are on thePythonstandard library, yet theregexlibrary will be used if installed, andcoloramawill be used on trace output if available.pygraphvizis required for generating diagrams.Grakois 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.RationaleGrakowas created to address some recurring problems encountered over decades of working with parser generation tools:Some programming languages allow the use ofkeywordsas 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.PEGparsers 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 actionsdo notbelong 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 likePython, but help is hard to find for working with complex grammar-description languages.Grakogrammars are in the spirit of aTranslators and Interpreters 101course (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.Pythonis a great language for working with language parsing and translation.DocumentationThecomplete documentationis available atGrako’shome page.LicenseCopyright (C) 2017      by Juancarlo Añez
Copyright (C) 2012-2016 by Juancarlo Añez and Thomas BraggYou may useGrakounder the terms of theBSD-style license described in the enclosedLICENSE.txtfile.If your project requires different licensingpleaseemail.ChangesSee theCHANGELOGfor details.