pacc logo
pacc v0.3: Operators

Next: , Previous: , Up: Top   [Contents][Index]


9 Operators

So now we can concisely parse a single decimal digit: we match the digit with a character class, and use a reference to the input to extract its value. To extend this to decimal integers, we can use a repetition operator. Pacc supports three repetition operators: ‘?’, ‘*’, and ‘+’. They are postfix operators, that is to say the operator follows its operand. The operand can be a matcher, or a more complicated expression in brackets.

The repetition operators in pacc have the same meanings as they do in regular expressions.

?

Matches zero or one occurrences of its operand, or to put it another way, it makes its operand optional.

*

Matches zero or more occurrences of its operand. Matches as many times as possible.

+

Matches one or more occurrences of its operand. Matches as many times as possible.

A decimal number consists of one or more digits, so the correct choice of repetiton operator for this case is ‘+’.

Decimal <- [0-9]+ -> { atoi(ref_str()) }

We are using the ‘ref_str()’ function which returns a newly allocated, NUL terminated string containing a copy of everything that the current rule matches. And ‘atoi()’ is from ‘<stdlib.h>’ of course. (One snag with this example is that pacc will happily match an arbitrarily long integer, whereas ‘atoi()’ is limited to integers that will fit in an ‘int’, but solving that issue is outside the scope of this tutorial. Possible options might include the error-checking available in ‘strtol()’, or to use an arbitrary-precision integer package which provides its own conversion function, for example ‘gmp_sscanf’.)


Next: , Previous: , Up: Top   [Contents][Index]

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