Next: Pacc basics, Previous: Parsing basics, Up: Top [Contents][Index]
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 mean 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. The output of pacc is valid C99, but not C89, so you will probably need to explicitly invoke a C99 compiler. (This is definitely an area that needs further study. While I’m not greatly swayed by the argument that pacc should be compatible with a 25 year old standard, it is unfortunate that currently if you hand its output to gcc you get an obscure error message.)
c99 -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!
Next: Pacc basics, Previous: Parsing basics, Up: Top [Contents][Index]
Last updated: 2016-08-03 21:39:50 UTC
One thing pacc needs is more users. And, perhaps, one way to get more users is to reduce the friction in getting started with pacc. An obvious lubricant is packaging. Read More...
Looking at _pacc_coords(), I noticed that it seemed to have the same realloc() bug that I'd just fixed in _pacc_result(). However, the "list of arrays" trick really wasn't going to work here. Read More...