pacc logo
pacc v0.3: Grammar

21.2 Grammar

The overall structure of a pacc grammar consists of an optional preamble enclosed in braces: ‘{}’ followed by one or more rules. The preamble is C source code, and is copied verbatim near the beginning of the output file. (If you use the ‘--defines’ option, the preamble is also copied into the .h file that it creates.)

21.2.1 Rules

A rule consists of a name, an optional type, and a definition. The first rule is the start rule of the grammar: valid utterances of the language defined by the grammar match the start rule.

The name follows the same lexical rules as C identifier names: it must start with an alphabetic character or underscore, and remaining characters are alphabetic, numeric, or underscores. The namespace of pacc rule names is entirely separate from any C program, however, and there are no reserved words.

The type, if present, is introduced by a double colon ‘::’, and can be any C type name that is in scope, provided that name can be constructed with identifiers and the ‘*’ character. (Function pointers can be used via a ‘typedef’ in the preamble.) If the type is omitted, the rule has the same type as the previous rule. (Caution: this means that rearranging the rules in a grammar can change the meaning of the grammar!) If the first rule has no explicit type, its type is ‘int’.

The definition is introduced with a left arrow ‘<-’, which is followed by one or more matchers, combined with operators.

21.2.2 Matchers

Matchers are the fundamental building blocks of definitions.

Literal: ‘"string"’

A string enclosed in double quotes matches exactly that string. The usual C character escapes are supported, so ‘\n’, ‘\012’, ‘\xA’, and ‘\u000A’ all represent the ASCII line feed character.

Character class: ‘[a-z0-9]’

A series of characters in square brackets matches any single one of those characters, which are written as in C, e.g. ‘[\t\n]’ matches a tab or a newline. If the first character is caret ‘^’, the class is negated; that is, it matches any single character except those in the class. A character range is expressed using two characters separated by a hyphen ‘-’, and it matches any single character with a Unicode code point between those of the two characters inclusive, e.g. ‘[a-z]’ matches any lower case English letter. The character to the left of the hyphen must be strictly less than the character to the right of the hyphen. To include a literal hyphen, make it the first or last character in the class. For a literal right square bracket, make it first. For a literal caret, make it anything other than the first character.

Call: ‘rule_name’

A bare word names another rule—it is an error if there is no such rule defined in this pacc grammar. It matches anything that the named rule matches. Call matchers are not allowed in a left recursive position.

Any: ‘.’

The period character matches any single character. (UTF-8 coding is assumed.)

21.2.3 Operators

Pacc has the following operators, which are shown with their precedence, in order from 5 to 0. In the descriptions that follow, ‘e’ and ‘f’ are arbitrary pacc expressions; ‘c’ is an arbitrary C expression.

Parentheses: ‘(e)’ (5)

Parentheses are used for grouping.

Repetition: ‘e?’ ‘e*’ ‘e+’ (4)

‘e?’ matches ‘e’ optionally (zero or one occurrences); ‘e*’ matches zero or more repetitions of ‘e’; and ‘e+’ matches one or more repetitions of ‘e’.

Guards: ‘&e’ ‘!e’ ‘&{c}’ (3)

‘&e’ requires that the input matches ‘e’ at this point, but it does not consume any input. ‘!e’ requires that the input does not match ‘e’, and again does not consume anything. ‘&{ ... }’ is a semantic guard: the braces hold an arbitrary C expression of type ‘int’. The expression is evaluated; the guard succeeds if its value is non-zero.

Binding: ‘name:e’ (3)

Matches whatever ‘e’ matches, and binds the value of ‘e’ to ‘name’, which is then in scope till the end of this alternative, for use in semantic guards and expressions. See Binding.

Bound literal: ‘"string":e’ (3)

Matches if ‘e’ matches and the value of ‘e’ (which must be of type char * or ref_t) is equal to ‘string’.

Sequencing: ‘e f’ (2)

Matches ‘e’ followed by ‘f’.

Empty: ‘%’ (2)

Epsilon ‘%’ represents an empty sequence; it can always be omitted, but its use can improve readability.

Value: ‘-> {c}’ (1)

Defines the value of a sequence; ‘c’ is an arbitrary C expression which must have the same type as the rule. If ‘c’ is a name (C identifier rules) or a decimal integer, the braces are optional. Otherwise, the arrow is optional.

Alternation: ‘e / f’ (0)

If ‘e’ matches, the alternation matches; otherwise, if ‘f’ matches, the alternation matches. Priority is important: ‘e’ is always tried before ‘f’.


Last updated: 2016-08-03 21:39:50 UTC

News

Porting and packaging

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...

Release relief

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...

See more news articles

feed