Code Monkey home page Code Monkey logo

comb's Introduction

Combinators in C
================

This is a sketch of parser combinators in C (plus some hacks).

The idea is that we build up a function that parses a given grammar by putting "combinators" together.

We only define a few combinators here:

* And( comb1, comb2 ) : constructs the combinator which parses comb1 followed by comb2

* Or ( comb1, comb2 ) : constructs the combinators which parses comb1 or comb2

* Lloop ( comb1, comb2 ) : constructs the combinator which parses a list of comb1's separated by comb2's; this combinator is for use when comb2 parses a left associative operator (such as '+')

We also have some functions for parsing literals:

* Integer() : constructs a combinator which parses an integer

* Match( str ) : constructs the combinator which accepts the string str

* Expect( str, err ) : constructs the combinator which accepts the string, str,  if present, else it prints the given error mesage, err, and aborts

The combinators are themselves defined using closures, i.e. structs which contain a function to call, plus data which is to be passed to that function (in this case, other combinators).

Warning: there are many things missing in this code. For example there is no garbage collection, so the code leaks memory. Also combinators for options and lists are not present.

Calc
====

The Calc program is a very simple example of how to use the combinators.

To build it, simply type make. To run it, type ./calc.

You can evaluate simple expressions involving:

* Integers (unsigned at present)

* +, -, *, /

* parentheses ()

CTRL-D to quit.

E.g.

> 1 + (2*3-4+5)*7;
50

comb's People

Contributors

wbhart avatar

Watchers

 avatar  avatar  avatar

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.