forked from orangeduck/mpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c2f936b
Showing
9 changed files
with
1,884 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*~ | ||
*.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
--------- | ||
Oops, something went wrong.