Package: grakopp
Work in progress!Grako++Grako++ is a packrat parser runtime for Grako, written in C++. The aim
is to provide a Grako-compatible parser generator, but with improved
performance over Python.Differences to GrakoThe output is always AST/JSON.The semantics class can be implemented in C++ or Cython (pure Python
through Cython is planned).The regular expression syntax isECMAScript,
not Python.Some features of Grako are missing, see below in the TODO section.State types must implement operator< for std::map (for future
flexibility, consider implementing the hash trait, too).BuildThe library is header-files only currently, so you can just copy the
files in include/ to a convenient location, or build from there. More
formally, you can use cmake.Build with clang by invoking: cmake -DCMAKE_CXX_COMPILER=/path/to/clang++Building with g++ < 4.9 requires linking with the boost_regex library,
because the std::regex implementation is broken. You can rely on the
exported cmake library file to configure the right settings.Other useful option: -DCMAKE_INSTALL_PREFIX:PATH=/path/to/installUsageThe grakopp program is used like grako to compile PEG files to source
code. There are four different source code output formats that can be
specified with the -f/–format option:–format–outputPurposehppname.hppC++ declarationcppname.cppC++ implementationpxdname.pxdCython declarationpyxname.pyxCython implementationFor pure C++ parsers, generating the hpp and cpp files is sufficient.
For Python integration, the pxd and pyx files are also needed. For
“name” you should subsitute the actual name of the parser (either the
base name of the PEG file or the argument to the –name option). The
filenames are, for now, hard-coded into the source files (the underscore
protects the C++ implementation from Cython-generated source files).Here is an example how to build a parser:$./grakopp-fhpp-o_basic.hpptests/basic/basic.peg$./grakopp--whitespace=""--no-nameguard-fcpp-o_basic.cpptests/basic/basic.peg$g++-DGRAKOPP_MAIN-std=c++11-Iinclude-O4-obasic_basic.cpp-lboost_regexYou can invoke the parser like a Python parser generated with grako
(currently, no options are supported, though, except for an internal
option –test that compares the AST with a text fixture file):$echo-ne1e2|./basic/dev/stdinsequence["e1","e2"]C++ InterfaceThe following header files exist:HeaderPurposegrakopp/exception.hppParser exceptionsgrakopp/buffer.hppBuffer for I/Ograkopp/ast.hppAST implementationgrakopp/parser.hppParser base classgrakopp/grakopp/hppInclude all abovegrakopp/ast-io.hppOptional AST stream I/OPython IntegrationUntil the Python package is prepared properly, build the Cython
extensions manually, for example like this:$cdpython/grakopp$cython--cplusbuffer.pyx$cython--cplusast.pyx$g++-std=c++11-I../../include-shared-pthread-fPIC-fwrapv-O2-Wall-fno-strict-aliasing-I/usr/include/python2.7-obuffer.sobuffer.cpp$g++-std=c++11-I../../include-shared-pthread-fPIC-fwrapv-O2-Wall-fno-strict-aliasing-I/usr/include/python2.7-oast.soast.cpp$cd../..To continue the above example:$./grakopp-fpxd-obasic.pxdtests/basic/basic.peg$./grakopp-fpyx-obasic.pyxtests/basic/basic.peg$cython-Ipython--cplusbasic.pyx$g++-std=c++11-Iinclude-shared-pthread-fPIC-fwrapv-O2-Wall-fno-strict-aliasing-I/usr/include/python2.7-obasic.sobasic.cpp_basic.cpp-lboost_regexYou can then use it from Python:$ PYTHONPATH=python python
>>> from grakopp import buffer
>>> b = buffer.PyBuffer()
>>> b.from_string("e1e2")
>>> import basic
>>> p = basic.basicPyParser()
>>> p.set_buffer(b)
>>> a = p._sequence_()
>>> a.to_python()
['e1', 'e2']
>>> b.pos
4
>>> a = p._sequence_()
>>> a.to_python()
FailedToken('e1')TODOdynamic Ast objects (so you can pass through Python or XML objects)python/distutils integrationautomatic compilation a la pyximportadd namespaceunicode support?more support and tests for stateful parsingregex syntax tests (make sure generated C strings are always proper)profile and optimizeGrako features missing:ignorecase (buffer match, matchre)comments skippingbuffer line parsing and trace output (also in exceptions)ParseInforules with argumentsleft recursionsemantic action “_default”AuthorsCopyright (C) 2014 semantics Kommunikationsmanagement GmbH
Written by Marcus Brinkmann <m.brinkmann@semantics.de>
See LICENSE.txt for details.