Rules

Here's where we've got to, french1.pacc:

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

We can improve this by splitting things up. Here's french2.pacc:

Start ← Yes → 1 / No → 0
Yes :: void ← "yes" / "oui"
No ← "non" / "no"

Don't, at the moment, worry about :: void on the second line—we'll explain what this means shortly. The important point to notice is that a grammar can have more than one rule. The first rule is always the start rule, where parsing begins.

Each rule has a name, in this grammar there are 3 rules named Start, Yes, and No. Rule names are formed in the same way as C identifiers: they must start with a letter or underscore, and continue with letters, underscores, or digits. I often follow the convention of starting rule names with capital letters as it helps them to stand out, but you don't have to.

One rule can call another simply by naming it. The definition of the Start rule contains two call matchers: the first calls the rule Yes and the second calls the rule No. As you might expect, when you call a rule by name, that matches whatever the called rule matches. You can call a rule before it has been defined, so long as the definition occurs in the same pacc grammar.

Let's look at that in a little more detail. A literal string like "yes" is one kind of matcher. A matcher occurs on the right hand side of a rule, and it matches something in the input. In the case of a literal string, it matches just that one string. So a call to another rule is another kind of matcher. There's only a couple more, which we'll get to soon.

Donate

Support the development of pacc with a donation! We accept donations in BitCoin or via PayPal who handle almost any other form of payment.

News

  • Release: pacc-0.1 (wōkòu)

    I'm delighted to announce a new release of pacc, the packrat PEG parser generator for C. See the download page. Read More...

  • Tying the loop tighter

    If it seems to have gone quiet around here recently, that's only partly because it's summer, and the lure of the outdoors is stronger than staying in with my head in a chunk of code. I have been gnawing away at a major subproject: converting a real language based on yacc over to pacc. This has been quite an eye-opener! Read More...

See more news articles

feed