pacc logo

pacc basics

A pacc grammar consists of one or more rules. Let's look at our first example again.

Start <- "yes" {1} / "no" {0}

There's just one rule in this tiny grammar. Every rule starts with a name, and the name of this rule is Start. The names of rules follow the same conventions as identifiers in C.

After the name, a left arrow introduces the definition of the rule. In this case, we've made a left arrow out of two old-fashioned ASCII symbols, < and -, but it's also permitted to use the Unicode “leftwards arrow” character, U+2190: ← and all future examples will do this. You can also use a plain old equals sign = if you prefer.

A pacc parser works by matching the input to the rules in the grammar. The process starts with the first rule (in this case it's the only rule), and the first thing in the definition of that first rule. In the case of Start, the first thing in the definition is "yes". The quote marks mean that this is a literal string. For a successful match, the input must be exactly the same characters as are in the literal.

Let's suppose that on this occasion the input is no. So the parser can't match the input against "yes" in the rule. But in this rule there is an alternative, and the parser will try to match that next. The / character separates alternatives, so the next alternative starts with another literal string: "no". This does match our example input.

Alternatives

An absolutely crucial point to understand about pacc grammars is that alternatives are tried in order. The first alternative which matches successfully is the one that “wins”, and later alternatives aren't considered at all.

So having matched "no" the parser then finds {0}. The braces indicate that this is a (tiny!) hunk of C code. Because it is in the alternative that matched, it will be evaluated to give a value to the rule. And the value of the start rule is the overall value of the parse.

Let's go multi-lingual. (There are more elegant ways to write this, which we'll come to soon.)

Start ← "yes" {1} / "oui" {1} / "no" {0} / "non" {0}

That may look like it should work, but this is what happens when we try it:

$ french0 oui
parsed with value 1
$ french0 no
parsed with value 0
$ french0 non
arg:1:3: expected end-of-input

What went wrong there? The problem is that the third alternative "no" matches the first two characters of the input non, and so that becomes the “winning” alternative, even though that leads to an overall failure. pacc parsers never “back up” once an alternative has completely matched.

For this simple grammar, the obvious fix is simply to swap the order of these two alternatives. We'll see similar problems, and other fixes, later on.

Start ← "yes" {1} / "oui" {1} / "non" {0} / "no" {0}

This grammar works as intended:

$ french1 no
parsed with value 0
$ french1 non
parsed with value 0

This behaviour is the fundamental difference between PEGs (the style of grammars that pacc uses) and CFGs (the more traditional alternative), so it may take you a while to get used to it.