Git Product home page Git Product logo

parsegen's Introduction

Parsegen

** This project is super old, and hasn't been supported for many years. **

Parsegen is an automatic parser generator. It reads in definitions in BNF and creates a top-down parser with one-symbol lookahead. Currently it supports outputting parsers in c and Ruby, although the ruby support is in it's early days.

Parsegen reads in files that describe LL(1) grammars and outputs a top-down hand-editable automaton that parses them.

The Grammar File

Parsegen grammar files are written in a dialect of BNF. There are three main sections in each file, separated by %%.

Header

The first section contains declarations and options. Declarations have the form TERMINAL = Terminal_Value. These specify the terminal symbols that are used in the grammar. The left hand side is the way they are referred to in the grammar file and the right hand side is the value to use in the output file (such as an enum value or constant). Options begin with % and look something like %option = value. Options are used to specify things like the function to call in the lexer to get a token, the prefix to use on function names and the language to use for the output automaton. Options specified in the file can be overridden from the command line.

Body

The second section contains a list of grammar rules. Each line that contains a rule begins with a non-terminal followed by the := symbol. The right hand side of the rule is made up of a mixture of terminals and non-terminals.

expr := NUMBER expr_prime
expr_prime := ADD expr_prime
expr_prime := SUB expr_prime
expr_prime :=

Lambda transitions are denoted by an empty expansion.

User Code

The final section contains user code that is written to the output file without any modification. This can be used to provide entry points to the parser or could include a main function to make the whole parser standalone.

Comments

In the first two sections the # character can be used as a line comment character. Any text after the hash is ignored. Within the user code section you are free to use whatever comment system you like, just make sure that it is supported by the target language.

Example Grammar

An example grammar which parses a properly formatted chain of integers added together:

NUMBER Tok_NUMBER
PLUS   Tok_PLUS
%%
addition_list := NUMBER addition_list_prime
addition_list_prime := PLUS NUMBER addition_list_prime
addition_list_prime := 
%%
int main(int argc, const char* argv[])
{
    printf("%d", addition_list());
}

Prerequisites

Parsegen is written in Python 3. To get it up and running you will need a python3 installation and the pystache module. If you want to be able to run the tests you will need to have nose installed as well. For development you will probably want to use Snooper or nosy to run the test suites automatically.

Gimme Gimme Gimme!

Parsegen can be installed with just one command in a Python 3 environment:

$ pip3 install --pre parsegen

To check that the package installed ok run the following command:

$ parsegen --version

parsegen's People

Contributors

iwillspeak avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

parsegen's Issues

Add Functional Testing

Add tests that check output in a functional manner.

This would be a collection of input files and the expected output context. A group of YAML files and a simple test runner similar to the way the Mustache spec is defined might be a good way to do this.

Add Introspection for Mustache Output

Look into using package introspection to discover the output formatters/contexts for the mustache output style. Not sure how config options for this will be described. Either compulsory .py file as at the moment or YAML front matter (a la Jekyll).

Reduce Filtering in Output Contexts

Some form of redesign of the model is required to ensure that it provides all the attributes required for the output Output objects. The Output objects themselves should be as thin and light as possible.

Use Mustache to Generate Parsers

This might be a good idea to allow the app to be more data-driven. A mustache template would be created for each different language supported and would allow easy generation of parsers in many different languages. This would hopefully remove a lot of string constants from the script and allow
easy refactoring.

Add proper command line parsing

Use the optparse.OptParse class and parse command line options. Also allow options to be set in the filewith %option statements.

Rename Output Contexts

OutputContext objects should probably be renamed to something more like OutputFormatter when they have been transitioned to hold almost no state.

Complete Manpages

The current page covering the use of the page can be made more complete.

A second page covering the components of a new file format would also be useful:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.