Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
orangeduck committed Sep 19, 2013
0 parents commit c2f936b
Show file tree
Hide file tree
Showing 9 changed files with 1,884 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*~
*.exe
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CC = gcc
CFLAGS = -ansi -Wall -Werror -Wno-unused -g

TESTS = $(wildcard tests/*.c)

all: check

check: $(TESTS) mpc.c
$(CC) $(CFLAGS) $^ -o test
./test

clean:
rm test.exe
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
Micro Parser Combinators
========================

_mpc_ is a lightweight Parser Combinator library for C.

The current main alternative is a branch of (https://github.com/wbhart/Cesium3)[Cesium3].

Features & Advantages
---------------------

* Error Message Support
* Regular Expression Support
* Parser Grammar Support
* Works for Generic Types
* AST Extension
* Single source & header files
* Written in clean ANSI C
* Doesn't rely on Boehm-Demers-Weiser Garbage Collection
* Doesn't use `setjmp` and `longjmp` for errors
* Doesn't pollute namespace

Example
-------

```c

mpc_val_t* combine_maths(int n, mpc_val_t** xs) {

int** vs = (int**)xs;

if (*vs[1] == '*') { *vs[0] *= *vs[2]; }
if (*vs[1] == '/') { *vs[0] /= *vs[2]; }
if (*vs[1] == '+') { *vs[0] += *vs[2]; }
if (*vs[1] == '-') { *vs[0] -= *vs[2]; }

free(vs[1]);
free(vs[2]);

return vs[0];
}

int main(int argc, char** argv) {

mpc_parser_t* Expr = mpc_new();
mpc_parser_t* Factor = mpc_new();
mpc_parser_t* Term = mpc_new();
mpc_parser_t* Maths = mpc_new();

mpc_define(Expr,
mpc_pc("cmaths ( fact ['*' | '/'] fact ) | fact",
combine_maths, Factor, free, Factor, free, Factor),
);

mpc_define(Factor,
mpc_pc("cmaths ( term ['+' | '-'] term ) | term",
combine_maths, Term, free, Term, free, Term),
);

mpc_define(Term,
mpc_pc("num | snd ('(' expr ')')",
mpc_int(), mpcf_asnd_free, Expr, free)
);

mpc_define(Maths, mpc_ends(Expr, free));

mpc_delete(Expr);
mpc_delete(Factor);
mpc_delete(Term);
mpc_delete(Maths);

}
```
Parsers
-------
Combinators
-----------
Regular Expressions
-------------------
Combinator Grammars
-------------------
Abstract Syntax Tree
--------------------
Reference
---------
Loading

0 comments on commit c2f936b

Please sign in to comment.