Let's actually make a pacc parser. We'll start with about the simplest possible language I can conceive of. In this language, there are just two valid utterances, yes and no. The “meaning” of yes will be 1, no will be 0. This can be expressed in a pacc grammar like this.
Start <- "yes" {1} / "no" {0}
We'll worry about the details of exactly how that expresses the language we described later on (although you can probably get the general idea). For now, we want to turn this into an actual program, so the first thing to do is to put that grammar into a file named bool.pacc.
Next, we'll need to supply the rest of the program. We'll keep it very simple. Here's main.c.
#include <stdio.h> #include <string.h> #include "bool.h" int main(int argc, char **argv) { int result; if (argc != 2) { fprintf(stderr, "one argument please\n"); return 1; } if (pacc_wrap("arg", argv[1], strlen(argv[1]), &result)) printf("parsed with value %d\n", result); else return 1; return 0; }
To build the program, we first need to invoke pacc on the grammar definition. This will output bool.c. We specify the -d switch to pacc so that it will also output bool.h which we included in our program, and supplies the declaration of pacc_wrap():
pacc -d bool.pacc
Then simply compile as normal:
gcc -o bool main.c bool.c
And here's what the program looks like in action:
$ bool yes parsed with value 1 $ bool no parsed with value 0 $ bool foo arg:1:1: expected Start
Which is what we wanted!