<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title>pacc - a compiler compiler</title><link href="https://paccrat.org/feed" rel="self"/><link href="https://paccrat.org/"/><updated>2016-08-03T21:31:34-00:00</updated><id>https://paccrat.org/</id><author><name>Toby Goodwin</name></author><entry><id>https://paccrat.org/entry/2016-07-29</id><link href="https://paccrat.org/entry/2016-07-29"/><updated>2016-08-03T21:31:34-00:00</updated><title>Porting and packaging</title><content type="html">&lt;div class="document" id="porting-and-packaging"&gt;
&lt;h1 class="title"&gt;Porting and packaging&lt;/h1&gt;

&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;I've recently become enamoured of &lt;a class="reference external" href="https://copr.fedorainfracloud.org/"&gt;COPRs&lt;/a&gt; (Cool Other Package Repos).
This is some Fedora infrastructure that makes it easy for anyone to
build binary RPMs for various architectures and versions of Fedora and
EPEL, and easy for end users to install them. EPEL, by the way, is the
“extra packages for enterprise linux” system, which you already have
installed on CentOS / RHEL unless you're &lt;em&gt;very&lt;/em&gt; strange.&lt;/p&gt;
&lt;p&gt;According to my current understanding, a COPR is pretty much equivalent
to a PPA (Private Package Archive) in the Ubuntu world. I am primarily a
Fedora guy, although professionally I support Debian, Ubuntu, and CentOS
systems. I'll have a bash at creating a PPA soon, but for now I'm
concentrating on COPR.&lt;/p&gt;
&lt;p&gt;It didn't take me long to write an RPM &lt;tt class="docutils literal"&gt;.spec&lt;/tt&gt; file. (Heck, I once had
a consultancy gig writing RPMs for money!) It worked just fine on my
primary development platform, currently Fedora-24 on &lt;tt class="docutils literal"&gt;x86_64&lt;/tt&gt;. Nor
did it take long to create a COPR account and upload my source RPM to
the build system. Then I ticked the boxes to choose a bunch of the
available versions and architectures, and went to make a coffee.&lt;/p&gt;
&lt;p&gt;Imagine my surprise to return to a long list of failures! It took a fair
while to fix them all, but the good news is that pacc is in fact pretty
portable. The failures were of three sorts.&lt;/p&gt;
&lt;ol class="arabic simple"&gt;
&lt;li&gt;Not quite a failure, but on a modern 32-bit system, most of the trace
lines in a pacc-generated file (and there are over 2000 in pacc.c)
produced a warning. They need to print a &lt;tt class="docutils literal"&gt;size_t&lt;/tt&gt; value, and I had
used &lt;tt class="docutils literal"&gt;%ld&lt;/tt&gt;. It turns out that C99 added the &lt;tt class="docutils literal"&gt;z&lt;/tt&gt; modifier for just
this case, so all the formats were replaced with &lt;tt class="docutils literal"&gt;%zu&lt;/tt&gt;.&lt;/li&gt;
&lt;li&gt;There were a few bugs in the test suite; use of uninitialized values,
writing past end of buffer, that sort of thing. These had worked by
coincidence on my development system, but caused failures on the ppc
architecture.&lt;/li&gt;
&lt;li&gt;Some of the test cases involve errors produced by the C compiler.
These have changed between different versions of gcc (I'm not
currently pretending that you can build pacc with any other C
compiler). For example, reporting the column where the error
occurred, as well as the line, is a relatively recent innovation. The
test suite is now more flexible about what it will accept.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;With some tweaking, I managed to get pacc building, and passing its test
suite, on the three current releases of Fedora (22 - 24), three
architectures each (&lt;tt class="docutils literal"&gt;i386&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;x86_64&lt;/tt&gt;, and &lt;tt class="docutils literal"&gt;ppc64le&lt;/tt&gt;). And 2
versions of EPEL: EPEL 6 comes in &lt;tt class="docutils literal"&gt;i386&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;x86_64&lt;/tt&gt; flavours;
under the fairly recent EPEL 7, only &lt;tt class="docutils literal"&gt;x86_64&lt;/tt&gt; is supported.&lt;/p&gt;
&lt;p&gt;I abandoned trying to get it going under EPEL 5. I forget exactly what
the problems were, but it looked like it was going to be pretty tedious,
and EPEL 5 is in the last year of “security fixes only” support. Nobody
is going to be developing new software for it. Similarly, the Fedora 22
ppc build failed in ways that looked uninteresting, and F22 reached
end-of-life a couple of weeks ago.&lt;/p&gt;
&lt;p&gt;Anyway, the upshot of all this, is that if you run Fedora, you can run
these commands:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
dnf copr enable tobygoodwin/pacc
dnf install pacc
&lt;/pre&gt;
&lt;p&gt;And that's it! Pacc is now installed as &lt;tt class="docutils literal"&gt;/usr/bin/pacc&lt;/tt&gt;, and you can
read the documentation locally with &lt;tt class="docutils literal"&gt;info pacc&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;For CentOS and RHEL, you can enable the COPR with &lt;tt class="docutils literal"&gt;yum copr&lt;/tt&gt;, or
download an appropriate &lt;tt class="docutils literal"&gt;.repo&lt;/tt&gt; file from the &lt;a class="reference external" href="https://copr.fedorainfracloud.org/coprs/tobygoodwin/pacc/"&gt;pacc copr&lt;/a&gt; which has
all the links and details you need.&lt;/p&gt;
&lt;p&gt;Hopefully this makes pacc more readily available to some of the people
who might be interested in using it. Perhaps more importantly, the
exercise has made pacc, and particularly its test suite, &lt;em&gt;considerably&lt;/em&gt;
more portable.&lt;/p&gt;
&lt;p&gt;If you are not covered by the COPR, then you will be interested in this
&lt;a class="reference external" href="https://paccrat.org/article/download"&gt;new source release&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2015-07-31</id><link href="https://paccrat.org/entry/2015-07-31"/><updated>2015-07-31T20:50:44-00:00</updated><title>Release relief</title><content type="html">&lt;div class="document" id="release-relief"&gt;
&lt;h1 class="title"&gt;Release relief&lt;/h1&gt;

&lt;p&gt;Looking at &lt;tt class="docutils literal"&gt;_pacc_coords()&lt;/tt&gt;, I noticed that it seemed to have the same
&lt;tt class="docutils literal"&gt;realloc()&lt;/tt&gt; bug that I'd just fixed in &lt;tt class="docutils literal"&gt;_pacc_result()&lt;/tt&gt;. However,
the &amp;quot;list of arrays&amp;quot; trick really wasn't going to work here.&lt;/p&gt;
&lt;p&gt;The problem is that we do a binary search on the collection of
coordinates we've amassed, which means it &lt;em&gt;must&lt;/em&gt; be a single array.
Also, since the list is sorted we sometimes need to insert a new element
in the middle. That would be incredibly hard to do with a list of
arrays.&lt;/p&gt;
&lt;p&gt;So after a bit of fretting I hit on a different approach: use a &lt;tt class="docutils literal"&gt;struct
return&lt;/tt&gt; to pass both values back from the function without involving
pointers, then provide a couple of macros to extract the &lt;tt class="docutils literal"&gt;int&lt;/tt&gt;s from
the &lt;tt class="docutils literal"&gt;struct&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Stupidly, I implemented this without first writing a test case. Having
got used to the safety harness of test-driven development, I fretted
about this: was the bug I thought I'd seen really there? And if so, had
I really fixed it? I had to go back and write one. The bug &lt;em&gt;was&lt;/em&gt; there,
although it proved surprisingly difficult to exercise. It depends on
evaluating a coordinate, then holding onto that value as another
coordinate (which causes the &lt;tt class="docutils literal"&gt;realloc()&lt;/tt&gt;) is calculated.&lt;/p&gt;
&lt;p&gt;My first attempts gave some odd results, and I realised there was
another bug in &lt;tt class="docutils literal"&gt;_pacc_coords()&lt;/tt&gt;. Due to complete silliness on my part,
it never calculated the right column value for the second coordinate
evaluated on a line. So that became a test case for a bug I previously
hadn't noticed.&lt;/p&gt;
&lt;p&gt;Next time round I finally managed to get a test case to exercise the
&lt;tt class="docutils literal"&gt;realloc()&lt;/tt&gt; bug. As with exercising &lt;tt class="docutils literal"&gt;memmove()&lt;/tt&gt;, it requires
contrived use of semantic guards to cause things to be evaluated in an
unusual order.&lt;/p&gt;
&lt;p&gt;At first, I'd convinced myself that I had a correct test case, but
nothing failed (obviously I'd reverted to the version of the code that I
thought had the bug). However, with careful use of first &lt;tt class="docutils literal"&gt;gdb&lt;/tt&gt; and
then &lt;tt class="docutils literal"&gt;valgrind&lt;/tt&gt; I was able to see that there was a use of freed memory
(which happened to still have the right values in it). How to handle
this? I thought about setting up a new class of test case that grepped
the output of &lt;tt class="docutils literal"&gt;valgrind&lt;/tt&gt; for text like &lt;tt class="docutils literal"&gt;Invalid &lt;span class="pre"&gt;read...&lt;/span&gt;&lt;/tt&gt;. But I
hadn't gone far dawn that road when...&lt;/p&gt;
&lt;p&gt;I remembered &lt;tt class="docutils literal"&gt;MALLOC_PERTURB_&lt;/tt&gt;! This little-advertised feature of
&lt;tt class="docutils literal"&gt;glibc&lt;/tt&gt; was introduced to me by Jeff Johnson. Details in
&lt;tt class="docutils literal"&gt;mallopt(3)&lt;/tt&gt;. Having once thought of it, I immediately realised that
it should be enabled for &lt;em&gt;all&lt;/em&gt; tests, which is easy to do. The good news
is that this didn't reveal any new bugs in the parser engine, although
it did show up a use-after-free bug that I'd introduced into the cooking
code. For now, this has reverted to a memory leak. It really doesn't
matter too much if we leak memory in the compiler, and I need to get a
new release out.&lt;/p&gt;
&lt;p&gt;Which I have &lt;a class="reference external" href="/article/download"&gt;now done&lt;/a&gt;. Whew!&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2015-07-18</id><link href="https://paccrat.org/entry/2015-07-18"/><updated>2015-07-19T07:30:55-00:00</updated><title>Lists of arrays</title><content type="html">&lt;div class="document" id="lists-of-arrays"&gt;
&lt;h1 class="title"&gt;Lists of arrays&lt;/h1&gt;

&lt;p&gt;In the &lt;a class="reference external" href="/entry/2015-07-15"&gt;previous post&lt;/a&gt; I talked about my ideas for avoiding a
&lt;tt class="docutils literal"&gt;realloc&lt;/tt&gt;-induced heisenbug. I've now implemented this, let me
describe it.&lt;/p&gt;
&lt;p&gt;Recall that the data structure we are implementing is nominally a table
with as many columns as there are characters in the input, and as many
rows as there are rules in the grammer. Each cell of the table is
currently called a &lt;tt class="docutils literal"&gt;struct pacc_mid&lt;/tt&gt;, although I can't now remember
where that name comes from and will probably change it to &lt;tt class="docutils literal"&gt;pacc_cell&lt;/tt&gt;.
Because the table is sparsely populated, we implement it as a hash
table, using the (column, row) pair as the key.&lt;/p&gt;
&lt;p&gt;The central &lt;tt class="docutils literal"&gt;struct pacc_parser&lt;/tt&gt; include these members:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
struct pacc_bkt *m_bkt;
unsigned int m_bkt_cnt;
&lt;/pre&gt;
&lt;p&gt;During initialization in &lt;tt class="docutils literal"&gt;pacc_input()&lt;/tt&gt;, we pick a value for
&lt;tt class="docutils literal"&gt;m_bkt_cnt&lt;/tt&gt; (more on this later), then allocate an array of that many
buckets.&lt;/p&gt;
&lt;p&gt;A bucket is defined like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
struct pacc_bkt {
    struct pacc_blk *blk;
    unsigned int valid;
};
&lt;/pre&gt;
&lt;p&gt;It is a pointer to the first &lt;tt class="docutils literal"&gt;struct pacc_blk&lt;/tt&gt; &lt;em&gt;block&lt;/em&gt;, and a counter
which specifies how many cells there are in the bucket.  And what is a
block? That's defined like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
struct pacc_blk {
    struct pacc_blk *next;
    struct pacc_mid mid[_pacc_MIDS_PER_BLK];
};
&lt;/pre&gt;
&lt;p&gt;It is simply a bog standard linked list of blocks. Each block contains
an array of cell structures, the number being a compile time constant.&lt;/p&gt;
&lt;p&gt;There's nothing too tricky in the code to manipulate the structures.
Fortunately knowledge of the hash table structure was (almost) confined
to a single place:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
static struct pacc_mid *_pacc_result(struct pacc_parser *p,
        size_t col, long rule);
&lt;/pre&gt;
&lt;p&gt;(In passing, I wonder why I used &lt;tt class="docutils literal"&gt;long&lt;/tt&gt; for rules, when &lt;tt class="docutils literal"&gt;int&lt;/tt&gt; seems
like a more sensible choice? Or even &lt;tt class="docutils literal"&gt;short&lt;/tt&gt;! Not that &lt;tt class="docutils literal"&gt;long&lt;/tt&gt; and
&lt;tt class="docutils literal"&gt;int&lt;/tt&gt; are actually different sizes on any common architecture.)&lt;/p&gt;
&lt;p&gt;The other place that needs to understand the structure is
&lt;tt class="docutils literal"&gt;pacc_destroy&lt;/tt&gt;. Correctly freeing the table is annoying, especially
since each &lt;tt class="docutils literal"&gt;pacc_mid&lt;/tt&gt; contains a pointer to a list which must itself
be freed, but it's only a few lines of code.&lt;/p&gt;
&lt;p&gt;Now, there are two interacting knobs that we can twiddle to get various
tradeoffs. First there is &lt;tt class="docutils literal"&gt;MIDS_PER_BLK&lt;/tt&gt; which is a compile time
constant. If this is set to &lt;tt class="docutils literal"&gt;1&lt;/tt&gt;, then the bucket structure degenerates
to a standard linked list. Naturally, I have confirmed that all tests
pass in this scenario.&lt;/p&gt;
&lt;p&gt;With &lt;tt class="docutils literal"&gt;MIDS_PER_BLK == 1&lt;/tt&gt; we never allocate a cell structure that we
don't use. But they are scattered arbitrarily through memory, and my
assumption is that following the list will prove expensive in terms of
page faults and CPU cache misses. (It would be nice to test this
assumption at some point.) With more cells in a block, we hope to reduce
this cost, at the expense of sometimes allocating cells that we don't
use.&lt;/p&gt;
&lt;p&gt;How far should we twist the knob? These days a Linux VM page is 4k, so a
plausible definition might be this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
#define _pacc_MIDS_PER_BLK (4096 / sizeof(struct pacc_mid) - 1)
&lt;/pre&gt;
&lt;p&gt;The idea of the &lt;tt class="docutils literal"&gt;- 1&lt;/tt&gt; is to ensure that &lt;tt class="docutils literal"&gt;sizeof (struct pacc_blk) &amp;lt;
4096&lt;/tt&gt;. Although we have no reason to think that &lt;tt class="docutils literal"&gt;malloc()&lt;/tt&gt; will align
the structure so that it lies within a single page, we know that we can
examine an entire block with at most two page faults.&lt;/p&gt;
&lt;p&gt;It so happens that &lt;tt class="docutils literal"&gt;sizeof (struct pacc_mid) == 64&lt;/tt&gt; (currently), so
we get 63 cells per block. Is that a reasonable number?&lt;/p&gt;
&lt;p&gt;Unfortunately, my corpus of pacc grammars and inputs is small. Still,
let's look at &lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt; for a start. The longest hash bucket created
when parsing it is 17 cells. That means we end up using only 12% of the
cells we allocate!&lt;/p&gt;
&lt;p&gt;This brings us to the other knob. In &lt;tt class="docutils literal"&gt;_pacc_set_bkt_cnt&lt;/tt&gt;, we choose
the largest of a preset selection of primes which is smaller than:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
unsigned long max = n_rules * (p-&amp;gt;input_length + 1) / 100;
&lt;/pre&gt;
&lt;p&gt;For the case of &lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt;, the numbers are 91 rules, 4981
characters, a maximum of 4533, and a selected prime of 2999. The hash
function does a decent job of spreading the 23087 cells among the
2999 buckets: a perfect distribution would be 7.7 cells per bucket.&lt;/p&gt;
&lt;p&gt;So it would seem that a more reasonable setting of &lt;tt class="docutils literal"&gt;MIDS_PER_BLK&lt;/tt&gt;
might be:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
#define _pacc_MIDS_PER_BLK (1024 / sizeof(struct pacc_mid) - 1)
&lt;/pre&gt;
&lt;p&gt;For the case of &lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt;, this results in us using 51% of all the
cells we allocate, and according to valgrind actually using less memory
than the previous table structure. (On reflection, the previous scheme
is certain to allocate a lot of unused cells too, but it's not worth
worrying about that now.)&lt;/p&gt;
&lt;p&gt;In addition, crude timing tests suggest a tiny improvement in
performance, of around 5%. So I declare this a win.  Without more data
points, I don't think further knob twiddling is worthwhile.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2015-07-15</id><link href="https://paccrat.org/entry/2015-07-15"/><updated>2015-07-19T07:17:18-00:00</updated><title>Heisenbug</title><content type="html">&lt;div class="document" id="heisenbug"&gt;
&lt;h1 class="title"&gt;Heisenbug&lt;/h1&gt;

&lt;p&gt;You might expect, from the &lt;a class="reference external" href="/entry/2015-06-03"&gt;previous post&lt;/a&gt;, that I've been beavering
away at converting an existing grammar to pacc. But you'd be wrong.&lt;/p&gt;
&lt;p&gt;What I have been doing is cooking up a brand new grammar from scratch.
Well, almost from scratch. As it happens, I already had the beginnings
of a PEG parser for the language in question, written for Ian Piumarta's
&lt;a class="reference external" href="http://piumarta.com/software/peg/"&gt;leg&lt;/a&gt; (a generator of recursive descent parsers from PEGs, and the direct
inspiration for pacc). Yes, I am returning to The Project from which
pacc is essentially a very long tangent. The Project has recently been
named quux. (I do think pacc is more important than quux, although at
present quux is at that delicious stage where the low-level stuff is in
place, but none of the high-level stuff is set in stone, and it can be
endlessly tweaked.)&lt;/p&gt;
&lt;p&gt;Anyway, as usual trying to do new things with pacc reveals new bugs. And
then (just before I went away for a week on holiday) I found what seemed
to be a very odd heisenbug. Pacc appeared to be trying to allocate an
absolutely enormous amount of memory. That was because a &lt;tt class="docutils literal"&gt;ref_t&lt;/tt&gt; value
had ended up with its start after its end! Running under gdb though, to
try and find the problem, the bug vanished. It's a long time since I've
had a proper heisenbug.&lt;/p&gt;
&lt;p&gt;Fortunately, we have valgrind, and it didn't take me too long at all to
find that I was using memory that had been &lt;tt class="docutils literal"&gt;realloc&lt;/tt&gt;'d. And a little
bit more playing led me to this test case:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
P ← a:Number b:Number { a * b }
  / w:Word c:Number { c * strlen(ref_dup(w)) }

Number ← &amp;quot;five&amp;quot;:Word { 5 } / &amp;quot;six&amp;quot;:Word { 6 }

Word :: ref_t ← w:[a-z]+ &amp;quot; &amp;quot;* -&amp;gt; w
&lt;/pre&gt;
&lt;p&gt;Remember that bound literals are implemented using semantic guards, so
the &lt;tt class="docutils literal"&gt;Number&lt;/tt&gt; rule is desugared to something like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Number ← _pacc_bl127:Word &amp;amp;{ ref_streq(_pacc_bl127,&amp;quot;five&amp;quot;) } { 5 } / ...
&lt;/pre&gt;
&lt;p&gt;Consider what happens with an input like &lt;tt class="docutils literal"&gt;&amp;quot;eight six&amp;quot;&lt;/tt&gt;. We
successfully match &lt;tt class="docutils literal"&gt;&amp;quot;eight&amp;quot;&lt;/tt&gt; against &lt;tt class="docutils literal"&gt;Word&lt;/tt&gt;. Then the semantic guard
kicks in, and we switch from parsing to evaluating long enough to bind
&lt;tt class="docutils literal"&gt;_pacc_bl127&lt;/tt&gt;. Since this is a &lt;tt class="docutils literal"&gt;ref_t&lt;/tt&gt;, it's actually a pointer into
the appropriate &lt;tt class="docutils literal"&gt;pacc_mid&lt;/tt&gt; structure.&lt;/p&gt;
&lt;p&gt;The semantic guard fails, and we're back to parsing. This will
inevitably involve allocating more &lt;tt class="docutils literal"&gt;pacc_mid&lt;/tt&gt; structures, by extending
a hash bucket. Since hash buckets are &lt;tt class="docutils literal"&gt;malloc&lt;/tt&gt;'d arrays, we need to
&lt;tt class="docutils literal"&gt;realloc&lt;/tt&gt;. This moves the pair of the values that our &lt;tt class="docutils literal"&gt;ref_t&lt;/tt&gt; is
pointing to, but of course the pointer doesn't get updated, and chaos
ensues.&lt;/p&gt;
&lt;p&gt;So how are we going to fix this? I've had several ideas. Let's start
with a silly one. We keep track of every &lt;tt class="docutils literal"&gt;ref_t&lt;/tt&gt; value. Every time
we're about to reallocate a block, we scan each &lt;tt class="docutils literal"&gt;ref_t&lt;/tt&gt; and see if it
points into this block. If it does, update it after the block has moved.
This could work, but it's far too much mechanism.&lt;/p&gt;
&lt;p&gt;Here's a much more reasonable idea. Instead of &lt;tt class="docutils literal"&gt;ref()&lt;/tt&gt; simply
returning a pointer into the current &lt;tt class="docutils literal"&gt;pacc_mid&lt;/tt&gt; structure, it could
copy the 2 &lt;tt class="docutils literal"&gt;size_t&lt;/tt&gt; values into a freshly allocated array, and return
(a pointer to) that instead. This should work fine, and would be easy to
implement. I have two minor misgivings. First, it makes &lt;tt class="docutils literal"&gt;ref()&lt;/tt&gt; &lt;em&gt;much&lt;/em&gt;
more expensive than it currently is:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
#define ref() (&amp;amp;cur-&amp;gt;col)
&lt;/pre&gt;
&lt;p&gt;The other snag is that (in an ideal world) something or other would
eventually free that memory. Now, I'm aware that I have a general
problem with freeing memory already, and this doesn't make it much
worse. Still, I'm not happy with having pacc itself allocating memory
that it doesn't know how to free. (I suppose I could keep a list of it
all and do it.)&lt;/p&gt;
&lt;p&gt;Taking a step back, why is this memory being moved anyway? Were this a
university project, hash buckets would be implemented as a linked list,
and the issue simply would not arise. The reason it's not is that I
believe linked lists are disastrous for performance: every single
element of the list is likely to involve a CPU cache miss and page
fault. I don't think preferring vectors to lists is premature
optimization.&lt;/p&gt;
&lt;p&gt;But, what about a list of vectors? We could allocate a block of
&lt;tt class="docutils literal"&gt;pacc_mid&lt;/tt&gt; structures. When they've all been used, allocate a new
block and link it to the last one. Nothing ever moves, and we avoid both
the problem of invalid pointers, and the copying overhead. (In an ideal,
ideal world, each block would be aligned on something that helps with
caches, such as a 4k boundary. There are probably glibc-specific ways to
do this, or perhaps it could be done with &lt;tt class="docutils literal"&gt;mmap()&lt;/tt&gt;, but that's
something to tackle another day.)&lt;/p&gt;
&lt;p&gt;Let's give it a go. The &lt;a class="reference external" href="/entry/2015-07-18"&gt;next post&lt;/a&gt; describes the new implementation.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2015-06-03</id><link href="https://paccrat.org/entry/2015-06-03"/><updated>2015-07-15T22:05:44-00:00</updated><title>Users</title><content type="html">&lt;div class="document" id="users"&gt;
&lt;h1 class="title"&gt;Users&lt;/h1&gt;

&lt;p&gt;One thing that pacc has lacked all the way along is users. It turns out
to be really hard to write decent code without those darned users
probing the dark corners.&lt;/p&gt;
&lt;p&gt;Now, of course, there's always been one primary user of pacc: and that's
pacc! (There's a reason for the recursive acronym.) And of course, the
fact that pacc can compile &lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt; is a good sign that a lot of it
fundamentally works. But still, it's all a bit parochial.&lt;/p&gt;
&lt;p&gt;I have tried to widen the net. On two occasions, I've started converting
grammars written for other parser generators to pacc. The first was
Bryan Ford's pappy parser for Java. Some of pacc's features were added
specifically for this project, such as &lt;tt class="docutils literal"&gt;|&lt;/tt&gt; as a separator for
alternatives, and bound literals. And several bugs were uncovered.&lt;/p&gt;
&lt;p&gt;I also began converting the yacc parser for rc, which is the Plan 9
shell designed by Tom Duff and reimplemented for Unix by Byron Rakitzis.&lt;/p&gt;
&lt;p&gt;In both cases, I have (so far at least!) failed to complete the project.
Obviously this is largely due to a lack of stamina and moral fibre.
Another problem is that it's hard to work on the conversion in an
incremental fashion, since a grammar is a tightly knitted whole.
Especially when all the expressions are utterly wrong (because they are
designed for a different language), &lt;em&gt;and&lt;/em&gt; some of the details of the
parser language itself are different, &lt;em&gt;and&lt;/em&gt; the whole point of the
exercise is that pacc itself may have bugs or infelicities lurking.&lt;/p&gt;
&lt;p&gt;Anyway, enough excuses. I've just made two changes that could ameliorate
the situation somewhat. The first is that I've finally got round to
implementing &lt;em&gt;value inheritance&lt;/em&gt; (which I've also called &lt;em&gt;default
expressions&lt;/em&gt;, but I really should pick one name and stick to it).&lt;/p&gt;
&lt;p&gt;The idea is simple enough, and has been on &lt;a class="reference external" href="/article/todo"&gt;the todo list&lt;/a&gt; for ages.
Suppose you have an alternative in a rule (or even a whole rule) that
simply calls another rule and returns its value:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
S ← t:That → t / ...
&lt;/pre&gt;
&lt;p&gt;You can now omit the excess verbiage, and write simply:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
S ← That / ...
&lt;/pre&gt;
&lt;p&gt;As well as the saving in keystrokes, in the latter case pacc ensures
that &lt;tt class="docutils literal"&gt;S&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;That&lt;/tt&gt; have the same type, which leads to much nicer
error messages than letting the C compiler pick up on type errors. (In
theory pacc could detect a type mismatch in the former case too, but I
think that would involve a lot of code that doesn't exist yet.)&lt;/p&gt;
&lt;p&gt;How does this help the conversion case? For one thing, pappy supports
this abbreviation. For another, it makes it simpler to change the start
rule to alias some low-level rule, so you can test the grammar from the
bottom up. In &lt;tt class="docutils literal"&gt;java.pappy&lt;/tt&gt;, the start rule is &lt;tt class="docutils literal"&gt;CompilationUnit&lt;/tt&gt;.
With value inheritance, you could say:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Start :: int ← HexNumeral
&lt;/pre&gt;
&lt;p&gt;and test the tiny part of the grammar that deals with &lt;tt class="docutils literal"&gt;HexNumeral&lt;/tt&gt;s.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Except&lt;/em&gt;: that will give you a bunch of &lt;tt class="docutils literal"&gt;rule not reached&lt;/tt&gt; errors.
No, strike that, it will give you a &lt;em&gt;single&lt;/em&gt; &lt;tt class="docutils literal"&gt;rule not reached&lt;/tt&gt; error,
since that is a fatal error. I think the only reason I made it a fatal
error is that I had code to hand that would report an error and die, but
nothing just to report an error. So the second change was to tidy that
up a bit and make &lt;tt class="docutils literal"&gt;rule not reached&lt;/tt&gt; a warning.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2015-05-26</id><link href="https://paccrat.org/entry/2015-05-26"/><updated>2015-05-27T06:34:11-00:00</updated><title>Default expressions</title><content type="html">&lt;div class="document" id="default-expressions"&gt;
&lt;h1 class="title"&gt;Default expressions&lt;/h1&gt;

&lt;p&gt;I finally got round to implementing a simple but highly useful feature in
the language, which I'm calling &lt;em&gt;default expressions&lt;/em&gt;. It's probably simplest
to explain by showing the test case:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Answer &amp;lt;- Digit

Digit &amp;lt;- Four / f:Five -&amp;gt; f / Six

Four &amp;lt;- &amp;quot;four&amp;quot; -&amp;gt; 4
Five &amp;lt;- &amp;quot;five&amp;quot; -&amp;gt; 5
Six &amp;lt;- &amp;quot;six&amp;quot; -&amp;gt; 6
&lt;/pre&gt;
&lt;p&gt;In both &lt;tt class="docutils literal"&gt;Answer&lt;/tt&gt; and two of the three alternatives of &lt;tt class="docutils literal"&gt;Digit&lt;/tt&gt; there is no
result expression. Previously this was never allowed in a non-&lt;tt class="docutils literal"&gt;void&lt;/tt&gt; rule.
With the new feature, if a sequence consists of a single call, a missing result
expression is equivalent to a binding and expression that returns the result of
that call.&lt;/p&gt;
&lt;p&gt;I did consider making the feature more general. For example, a rule like
&lt;tt class="docutils literal"&gt;Number ← &lt;span class="pre"&gt;n:[0-9]+&lt;/span&gt; → n&lt;/tt&gt; could benefit from default expressions. However, it
strikes me that a rule like this would be quite unusual. More likely would be
&lt;tt class="docutils literal"&gt;Number ← &lt;span class="pre"&gt;n:[0-9]+&lt;/span&gt; _ → n&lt;/tt&gt;, following the usual convention that &lt;tt class="docutils literal"&gt;_&lt;/tt&gt; is a
rule that matches whitespace. It is not clear that a generalized default
expression feature would help in this case. Were it fully generalized, the
meaning of &lt;tt class="docutils literal"&gt;Number ← &lt;span class="pre"&gt;[0-9]+&lt;/span&gt; _&lt;/tt&gt; would most obviously be &lt;tt class="docutils literal"&gt;Number ← &lt;span class="pre"&gt;n:([0-9]+&lt;/span&gt;
_) → n&lt;/tt&gt;. That's probably not what you wanted. (And, if it is, you'd probably
be better off writing &lt;tt class="docutils literal"&gt;Number ← &lt;span class="pre"&gt;[0-9]+&lt;/span&gt; _ → { ref() }&lt;/tt&gt; which is longer but
clearer.)&lt;/p&gt;
&lt;p&gt;The default expressions feature is implemented in &lt;tt class="docutils literal"&gt;sugar.c&lt;/tt&gt; using the
standard &lt;tt class="docutils literal"&gt;walk()&lt;/tt&gt; function. However, this is not a particularly good fit for
the job: &lt;tt class="docutils literal"&gt;walk()&lt;/tt&gt; is great at desugaring expressions that may occur anywhere
in the AST. But the default expressions feature only applies to sequences that
are either at the top-level of a rule, or are children of a top-level alt.
Something that simply looped over the rules would be simpler and clearer.&lt;/p&gt;
&lt;p&gt;I came across this interesting &lt;a class="reference external" href="http://jozefg.bitbucket.org/posts/2015-05-19-compiling.html"&gt;1000 word introduction to compilers&lt;/a&gt; the other
day. Of course, I disagree with his assessment that parsing is “kinda stuffy” 🙂
But it did get me thinking more about simplifications / optimizations. These
have received scant attention in pacc, simply stuffed into &lt;tt class="docutils literal"&gt;sugar.c&lt;/tt&gt;,
definitely one of the uglier and least polished parts of the compiler. Of
course, pacc is a tiny language, and it is right that there is relatively
little between the parser and the emitter. Still, it's a nice place to work.&lt;/p&gt;
&lt;p&gt;There are a couple of snags with the implementation of default expressions as
it stands. One is that we generate new &lt;tt class="docutils literal"&gt;expr&lt;/tt&gt; nodes that don't contain
&lt;tt class="docutils literal"&gt;coords&lt;/tt&gt; nodes. There's currently no obvious place we could get the
coordinates from.&lt;/p&gt;
&lt;p&gt;Another snag is that it fails to check that the types line up.&lt;/p&gt;
&lt;p&gt;Unfortunately, these two snags interact particularly badly. Suppose we change
the last rule to be &lt;tt class="docutils literal"&gt;Six :: char * &amp;lt;- &amp;quot;six&amp;quot; &lt;span class="pre"&gt;-&amp;gt;&lt;/span&gt; { &amp;quot;string&amp;quot; }&lt;/tt&gt; the only hint
that anything is wrong is a C compiler warning:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
test/parse.c: In function ‘pacc_parse’:
test/parse.c:762:24: warning: assignment makes integer from pointer without a cast
           cur = _pacc_p;
&lt;/pre&gt;
&lt;p&gt;The warning is accurate, but there's no way to see what error in the code
produced it.&lt;/p&gt;
&lt;p&gt;Obviously pacc has enough type information available to it to produce a proper
error at this point. It's just a small matter of coding...&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2015-05-07</id><link href="https://paccrat.org/entry/2015-05-07"/><updated>2015-05-24T19:51:22-00:00</updated><title>A new bug</title><content type="html">&lt;div class="document" id="a-new-bug"&gt;
&lt;h1 class="title"&gt;A new bug&lt;/h1&gt;

&lt;p&gt;Unfortunately, there are too many projects in this world, and pacc has
been neglected for too long. However, I have been looking at one of
those other projects recently, in fact, the one that convinced me I
needed to write pacc in the first place! A new bug has turned up.
(Hooray?)&lt;/p&gt;
&lt;p&gt;The code in &lt;tt class="docutils literal"&gt;dexpr()&lt;/tt&gt; that deletes &lt;tt class="docutils literal"&gt;expr&lt;/tt&gt; nodes from a feeding
grammar assumes (indeed, asserts) that an &lt;tt class="docutils literal"&gt;expr&lt;/tt&gt; node will have an
older sibling.  But that may not be the case. Consider this rule:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
List ← i:Item l:List → { cell_cons(i, l) }
     / ε → cell_nil
&lt;/pre&gt;
&lt;p&gt;It produces this AST:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
rule 168: List
  type 167: Cell *
  alt 166:
    seq 161:
      bind 153: i
        call 152: Item
      bind 155: l
        call 154: List
      expr 160:  cell_cons(i, l)
        coords 159: 15 25
        ident 158: cell_cons
        ident 157: i
        ident 156: l
    seq 165:
      expr 164: cell_nil
        coords 163: 16 12
        ident 162: cell_nil
&lt;/pre&gt;
&lt;p&gt;So &lt;tt class="docutils literal"&gt;dexpr()&lt;/tt&gt; can eliminate the node &lt;tt class="docutils literal"&gt;expr 160&lt;/tt&gt; by updating the
&lt;tt class="docutils literal"&gt;next&lt;/tt&gt; pointer in node &lt;tt class="docutils literal"&gt;bind 155&lt;/tt&gt;. But to eliminate &lt;tt class="docutils literal"&gt;expr 164&lt;/tt&gt;, we
need to update the &lt;tt class="docutils literal"&gt;first&lt;/tt&gt; pointer in &lt;tt class="docutils literal"&gt;seq 165&lt;/tt&gt;. That's easy enough
to fix, just keep a pointer to the parent in &lt;tt class="docutils literal"&gt;dexpr&lt;/tt&gt; too, and use that
if there is no elder sibling.&lt;/p&gt;
&lt;p&gt;Oh darn, I just implemented the fix, but I'm supposed to make a test
case first. Sorted.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-07-12</id><link href="https://paccrat.org/entry/2013-07-12"/><updated>2015-05-24T19:45:22-00:00</updated><title>Tying the loop tighter</title><content type="html">&lt;div class="document" id="tying-the-loop-tighter"&gt;
&lt;h1 class="title"&gt;Tying the loop tighter&lt;/h1&gt;

&lt;p&gt;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!&lt;/p&gt;
&lt;p&gt;I've been painfully aware of the lack of real clients for pacc for a
long time. Apart from &lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt; itself, I have just a bunch of test
cases. I've &lt;em&gt;thought&lt;/em&gt; about various things, but never got round to it
till now. And converting an existing project has shown up a number of
deficiencies, or “bugs” as you might call them. One surprising bug was
that result expressions could not contain newline characters! In 172
test files, it had never once occurred to me to type something like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
S ←
  &amp;quot;yes&amp;quot; {
    1
  } /
  &amp;quot;no&amp;quot; {
    0
  }
&lt;/pre&gt;
&lt;p&gt;That was an easy (and urgent) fix. Others will have to wait for the next
release. For example, for certain classes of error, pacc goes to great
lengths to ensure that they are reported with their location, and this
works very well with edit-compile-fix tools, such as &lt;tt class="docutils literal"&gt;:make&lt;/tt&gt; in vim.
But other error reports, those that pacc makes while looking at the
parse tree, are not so helpful. For example, “rule not found” error is
reported with the name of the calling rule, but not its location. That
will have to wait for the next release.&lt;/p&gt;
&lt;p&gt;If you omit the closing brace from a result expression, you get an error
at the end of the file. We will need to work out some way that pacc can
report the location of the opening brace. Again, that's for the next
release, at the earliest!&lt;/p&gt;
&lt;p&gt;A more urgent, and highly surprising omission was that braceless
expressions don't record where they are. For a simple example:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
S ← &amp;quot;yes&amp;quot; → q
&lt;/pre&gt;
&lt;p&gt;Obviously the C compiler is going to complain that &lt;tt class="docutils literal"&gt;q&lt;/tt&gt; is not in
scope, but since we haven't generated a &lt;tt class="docutils literal"&gt;coords&lt;/tt&gt; node for that
braceless expression, no &lt;tt class="docutils literal"&gt;#line&lt;/tt&gt; directive is emitted, and the error
ends up being at line 456 of &lt;tt class="docutils literal"&gt;parse.c&lt;/tt&gt;, instead of line 1 of
&lt;tt class="docutils literal"&gt;parse.pacc&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;(Ac-tu-al-ly.... pacc could detect that error itself, since it knows
what identifiers are in scope. But even if we some day implement that,
it's still Just Right for the coords node to be created here.)&lt;/p&gt;
&lt;p&gt;The fix was a relatively minor change to the grammar, although it took
me a while to get it right, due to an unfortunate wrong turn. I tried to
use &lt;tt class="docutils literal"&gt;lBrace ← &amp;quot;{&amp;quot; _&lt;/tt&gt; to match the start of a braced expression. But
since &lt;tt class="docutils literal"&gt;_&lt;/tt&gt; matches pacc comments, which include &lt;tt class="docutils literal"&gt;# to end of line&lt;/tt&gt;,
using &lt;tt class="docutils literal"&gt;lBrace&lt;/tt&gt; swallows any &lt;tt class="docutils literal"&gt;#include&lt;/tt&gt; lines. It took me quite a
while to spot that.&lt;/p&gt;
&lt;p&gt;All substantial changes to &lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt; must be reflected back into
&lt;tt class="docutils literal"&gt;pacc0.c&lt;/tt&gt;: the code that hand crafts the first pacc AST. (That first AST we
walk with the emitter to build a parser that can parse &lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt; to
build the second AST.) Of necessity, &lt;tt class="docutils literal"&gt;pacc0.c&lt;/tt&gt; was originally hand
written, and I have manually maintained it ever since.&lt;/p&gt;
&lt;p&gt;Every time I've begun the tedious process of tweaking &lt;tt class="docutils literal"&gt;pacc0.c&lt;/tt&gt;, I've
wondered if it could be automated. I've always held back though. Partly
because each set of changes was quite small (and “the last”—it's
surprised me how often &lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt; has changed) so it barely seemed
worthwhile.  Partly out of a nostalgia for that early piece of
scaffolding (I am intensely proud of pacc, and don't want to throw away
its baby clothes). And partly because the way I was envisaging
automating it was to run an (awk? perl? Haskell?) program over the
output of a tree dump made with &lt;tt class="docutils literal"&gt;pacc &lt;span class="pre"&gt;-D0&lt;/span&gt; pacc.pacc&lt;/tt&gt;, and I didn't
relish writing that.&lt;/p&gt;
&lt;p&gt;This time, because of that summer thing we seem to be having at the
moment, there was quite a gap between changing &lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt; and
updating &lt;tt class="docutils literal"&gt;pacc0.c&lt;/tt&gt; so I had a good chance to think over whether and
how to do it (see also &lt;a class="reference external" href="http://tobold.org/entry/2009-06-09"&gt;Stop &amp;amp; Think&lt;/a&gt;). I was just thinking about how
to use the indentation in the tree dump to build the nesting structure
when it finally dawned on me that it would be absolutely insane to
&lt;em&gt;re-parse&lt;/em&gt; a dump of a parse tree! Absolutely the right way to program
this is in &lt;strong&gt;C&lt;/strong&gt;: it's just an alternate backend to the compiler!&lt;/p&gt;
&lt;p&gt;Now that seemed like a project I could get enthusiastic about! The new
backend is almost trivial, in fact, here's the tree walker in its
entirety:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
void walk(struct s_node *p, int d) {
    int kid = p-&amp;gt;first != 0;
    int sib = p-&amp;gt;next != 0;
    char *t = decode_type(p-&amp;gt;type);

    if (sib) walk(p-&amp;gt;next, d);
    if (kid) walk(p-&amp;gt;first, d + 1);
    printf(&amp;quot;n = &amp;quot;);
    if (sib) printf(&amp;quot;cons(&amp;quot;);
    if (kid) printf(&amp;quot;s_kid(%s, p%d)&amp;quot;, t, d + 1);
    else printf(&amp;quot;s_new(%s)&amp;quot;, t);
    if (sib) printf(&amp;quot;, p%d)&amp;quot;, d);
    printf(&amp;quot;; &amp;quot;);
    if (s_is_number(p-&amp;gt;type))
        printf(&amp;quot;n-&amp;gt;number = %d; &amp;quot;, p-&amp;gt;number);
    if (s_is_pair(p-&amp;gt;type))
        printf(&amp;quot;n-&amp;gt;pair[0] = %d; n-&amp;gt;pair[1] = %d; &amp;quot;, p-&amp;gt;pair[0], p-&amp;gt;pair[1]);
    if (s_is_text(p-&amp;gt;type) &amp;amp;&amp;amp; p-&amp;gt;text) {
        char *s;
        printf(&amp;quot;n-&amp;gt;text = \&amp;quot;&amp;quot;);
        for (s = p-&amp;gt;text; *s; ++s) {
            switch (*s) {
                case '\n': printf(&amp;quot;\\n&amp;quot;); break;
                case '&amp;quot;': printf(&amp;quot;\\\&amp;quot;&amp;quot;); break;
                case '\\': printf(&amp;quot;\\\\&amp;quot;); break;
                default: printf(&amp;quot;%c&amp;quot;, *s);
            }
        }
        printf(&amp;quot;\&amp;quot;; &amp;quot;);
    }
    printf(&amp;quot;p%d = n;\n&amp;quot;, d);
}
&lt;/pre&gt;
&lt;p&gt;So what does all this mean? Pacc will still ship with a &lt;tt class="docutils literal"&gt;pacc0.c&lt;/tt&gt;
which begins the build process. But in the future, when &lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt;
changes, those changes can quickly be reflected back with:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
make boot
./boot &amp;gt; pacc0.c
&lt;/pre&gt;
&lt;p&gt;Pacc is slowly growing up. And if it's possible, I'm now even prouder of
it than I was before.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-06-11</id><link href="https://paccrat.org/entry/2013-06-11"/><updated>2015-05-24T19:45:30-00:00</updated><title>Feeding finale</title><content type="html">&lt;div class="document" id="feeding-finale"&gt;
&lt;h1 class="title"&gt;Feeding finale&lt;/h1&gt;

&lt;p&gt;That was almost too easy. It took just a few minutes to implement &lt;tt class="docutils literal"&gt;$&lt;/tt&gt;
as new syntax to indicate feeding points (the new node type in the AST
is called &lt;tt class="docutils literal"&gt;cafe&lt;/tt&gt;), then update the cooker, eat a few test cases, and
wash up by removing the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-r&lt;/span&gt;&lt;/tt&gt; option.&lt;/p&gt;
&lt;p&gt;A particularly pleasing moment was when the calculator test worked first
time; a nice indication that this is at last the right approach. That
example now looks like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Sum :: int ← _ a:Additive → a
Additive ← m:Multitive $ Plus $ a:Additive { m + a } / m:Multitive → m
Multitive ← p:Primary $ Times $ m:Multitive { p * m } / p:Primary → p
Primary ← Left $ a:Additive $ Right → a / d:Decimal → d
Decimal ← [0-9]+ _ { atoi(ref_ptr(ref())) }

Left :: void ← &amp;quot;(&amp;quot; _
Right ← &amp;quot;)&amp;quot; _
Plus ← &amp;quot;+&amp;quot; _
Times ← &amp;quot;*&amp;quot; _
_ ← [ \t\n] *
&lt;/pre&gt;
&lt;p&gt;If you want to play with it yourself, please clone the git repo:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
git clone git://paccrat.org/pacc
&lt;/pre&gt;
&lt;p&gt;A new release is now a step more imminent than it was. Watch this space!&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-06-10</id><link href="https://paccrat.org/entry/2013-06-10"/><updated>2015-05-24T19:45:23-00:00</updated><title>Rethinking feeding</title><content type="html">&lt;div class="document" id="rethinking-feeding"&gt;
&lt;h1 class="title"&gt;Rethinking feeding&lt;/h1&gt;

&lt;p&gt;I thought I'd cracked it. Following Bryan Ford, I have always written my
spacing at the end of “lexical” rules, with an additional call at the
beginning of the start rule. For example, picking up again a slightly
improved version of our trivial digit adder, and again ignoring feeding
for the moment I would have written:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Sum ← _ a:Digit Plus b:Digit → { a + b }
Digit ← [0-9] _  → { *ref_ptr(ref()) - '0' }
Plus :: void ← &amp;quot;+&amp;quot; _
_ ← [ \t\n] *
&lt;/pre&gt;
&lt;p&gt;As previously discussed, to turn this into a feeder, we have to do more
than declare &lt;tt class="docutils literal"&gt;_&lt;/tt&gt; as our feed rule. But consider this equivalent
grammar:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Sum ← a:Digit Plus b:Digit _ → { a + b }
Digit ← _ d:[0-9] → { *ref_ptr(d) - '0' }
Plus :: void ← _ &amp;quot;+&amp;quot;
_ ← [ \t\n] *
&lt;/pre&gt;
&lt;p&gt;All that has happened is that the lexical elements pick up space
&lt;em&gt;before&lt;/em&gt; their element. Of course, that means the start rule doesn't
need to match initial space, but it does need to pick up final space
instead. Now with this grammar, if we simply declare &lt;tt class="docutils literal"&gt;_&lt;/tt&gt; to be the
feed rule, it all works! Well, apart from the call at the end of the
start rule, but we can easily make an exception for the first rule.&lt;/p&gt;
&lt;p&gt;So I implemented all that, and it worked fine. I even got as far as
changing the default name for the feed rule to a single underscore. Then
I tried it on a slighlty more realistic example. Specifically this one:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Sum :: int ← a:Additive _ → a
Additive ← m:Multitive Plus a:Additive { m + a } / m:Multitive -&amp;gt; m
Multitive ← p:Primary Times m:Multitive { p * m } / p:Primary -&amp;gt; p
Primary ← Left a:Additive Right -&amp;gt; a / d:Decimal -&amp;gt; d
Decimal ← _ [0-9]+ { atoi(ref_ptr(ref())) }
Left :: void ← _ &amp;quot;(&amp;quot;
Right ← _ &amp;quot;)&amp;quot;
Plus ← _ &amp;quot;+&amp;quot;
Times ← _ &amp;quot;*&amp;quot;
_ ← [ \t\n] *
&lt;/pre&gt;
&lt;p&gt;The critical difference here is that the rules form a loop. If we
declare &lt;tt class="docutils literal"&gt;_&lt;/tt&gt; as the feed rule (recall that cooking replaces &lt;tt class="docutils literal"&gt;... _
foo&lt;/tt&gt; with &lt;tt class="docutils literal"&gt;... _ (foo / !.)&lt;/tt&gt;) then we end up with left recursions. In
the grammar above, the recursion is in &lt;tt class="docutils literal"&gt;Additive&lt;/tt&gt;, since we end up at
&lt;tt class="docutils literal"&gt;Primary&lt;/tt&gt;, the cooked alternative in &lt;tt class="docutils literal"&gt;Left&lt;/tt&gt; doesn't consume
anything, and we're back to &lt;tt class="docutils literal"&gt;Additive&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;That's not quite true. When I first tried it, the grammar actually
compiled, and then went into an infinite loop. It turns out that cooked
grammars aren't subject to preening. And then even when I fixed that, I
hadn't implemented the left recursion test properly for predicates. So
if nothing else this little excursion has improved the code base a
little.&lt;/p&gt;
&lt;p&gt;At first I thought that I could get away with it by removing the &lt;tt class="docutils literal"&gt;_&lt;/tt&gt;
call from &lt;tt class="docutils literal"&gt;Left&lt;/tt&gt; (and hoping I could fix that up somewhere else
later). But no, that just shifts the left recursion to &lt;tt class="docutils literal"&gt;Multitive&lt;/tt&gt;
(exercise for the reader).&lt;/p&gt;
&lt;p&gt;So this idea won't fly. I'm slightly glad, because I really like the
idea of using &lt;tt class="docutils literal"&gt;$&lt;/tt&gt; to mark feed points, even though it will be more
work.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-06-05</id><link href="https://paccrat.org/entry/2013-06-05"/><updated>2015-05-24T19:45:29-00:00</updated><title>Documenting feeding</title><content type="html">&lt;div class="document" id="documenting-feeding"&gt;
&lt;h1 class="title"&gt;Documenting feeding&lt;/h1&gt;

&lt;p&gt;I've always been very proud of feeding. It solves a problem which &lt;a class="reference external" href="/article/glossary#ford"&gt;Bryan
Ford&lt;/a&gt; highlights in his original thesis, and as far as I know nobody
else has solved this problem. (If anybody would like to award me a PhD
for this, please get in touch at the usual address.) Life has taught me
that ideas are cheap. To &lt;em&gt;implement&lt;/em&gt; an idea, well, that's...&lt;/p&gt;
&lt;p&gt;... scarcely anything! I'd definitely thought of feeding by November
2009, I think rather sooner than that. (I came to it very early, since I
decided to write pacc because the Unix shell I was working on had got
too complicated for yacc, and feeding is basic to shells: think &lt;tt class="docutils literal"&gt;PS1&lt;/tt&gt;
and &lt;tt class="docutils literal"&gt;PS2&lt;/tt&gt; in the Bourne shell.) I seem to have implemented feeding
only just before the first release, in about May 2012.&lt;/p&gt;
&lt;p&gt;Slightly better than implementing an idea is to publish it. Feeding was
in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pacc-0.0&lt;/span&gt;&lt;/tt&gt; of course, and at that time there was a very brief
description of it on the website. But it is only now that I have sat
down and tried to explain it. As usual, writing documentation throws up
ideas about how the implementation could perhaps be better.&lt;/p&gt;
&lt;p&gt;I'm reminded of a paper &lt;a class="reference external" href="http://www.win.tue.nl/~evink/education/avp/pdf/critique-of-java.pdf"&gt;A critique of Java&lt;/a&gt; by one of my favourite
university lecturers, Harold Thimbleby. Sadly not the hilarious lame
ducks, but the part where he discusses Java's &lt;tt class="docutils literal"&gt;import&lt;/tt&gt; statement which
is restricted to occur only near the top of a source file. This
restriction keeps rearing its head in the documentation. “But as the
restriction is arbitrary, why not design the language without the
restriction, for the description of the restricted language including
providing examples is unnecessarily tedious and error prone.”&lt;/p&gt;
&lt;p&gt;Anyway, in my tutorial document I am expanding the venerable calculator
example to allow feeding. And describing it is harder than expected.  As
the implementation stands, there is a distinguished &lt;em&gt;feed rule&lt;/em&gt; whose
name defaults to &lt;tt class="docutils literal"&gt;__&lt;/tt&gt; (two underscores). At first I thought this could
simply replace the whitespace rule &lt;tt class="docutils literal"&gt;_ ← [ \t\n]*&lt;/tt&gt;. I wrote an example
on this basis, but when I tried to run it I hit an assert!&lt;/p&gt;
&lt;p&gt;On investigation, it turned out that the assert was essentially right;
what I was trying to do was daft. So the first step was to turn that
into a sensible error message (along with a test case of course). How is
it daft? Rewind to a trivial grammar, which sums two digits, possibly
separated by whitespace, and initially without feeding:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Sum ← _ a:Digit b:Digit → { a + b }
Digit ← [0-9] _  → { *ref_ptr(ref()) - '0' }
_ :: void ← [ \t\n] *
&lt;/pre&gt;
&lt;p&gt;If we want to use this for feeding, we will need to “cook” it, by
invoking pacc with the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-f&lt;/span&gt;&lt;/tt&gt; option. Cooking a grammar produces a
second grammar in which each occurence of the distinguished rule is
followed by either whatever follows it normally, or the end of the
input. Then if the input fails to be matched by the full grammar, but
can be matched by the cooked grammar, it is clear that we can read and
feed another line.&lt;/p&gt;
&lt;p&gt;Now, if we simply replace &lt;tt class="docutils literal"&gt;_&lt;/tt&gt; with &lt;tt class="docutils literal"&gt;__&lt;/tt&gt; (or invoke &lt;tt class="docutils literal"&gt;pacc &lt;span class="pre"&gt;-r_&lt;/span&gt;&lt;/tt&gt; to
choose a non-standard name for the distinguished rule), it can't
possibly do what we need. In the above example, for one thing, the only
occurence of &lt;tt class="docutils literal"&gt;_&lt;/tt&gt; is already not followed by anything, so cooking can't
work out how to modify it (that was what threw the assert). But more
significantly, since there is no &lt;tt class="docutils literal"&gt;_&lt;/tt&gt; between the two &lt;tt class="docutils literal"&gt;Digit&lt;/tt&gt;s, how
can cooking allow them to appear in separate bites of the input?&lt;/p&gt;
&lt;p&gt;So we need to lift &lt;tt class="docutils literal"&gt;__&lt;/tt&gt; above the lexical elements, and specify it in
the highest level rules where it makes sense. Then the first two rules
of trivial grammar will look like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Sum ← _ a:Digit __ b:Digit → { a + b }
Digit ← [0-9] _  → { *ref_ptr(ref()) - '0' }
&lt;/pre&gt;
&lt;p&gt;(I may return later to the question of whether the initial &lt;tt class="docutils literal"&gt;_&lt;/tt&gt; should
in fact be &lt;tt class="docutils literal"&gt;__&lt;/tt&gt; or even &lt;tt class="docutils literal"&gt;_ __&lt;/tt&gt;!) That is fine as far as it goes, but
what should &lt;tt class="docutils literal"&gt;_&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;__&lt;/tt&gt; match? It seems obvious that &lt;tt class="docutils literal"&gt;__&lt;/tt&gt; should
match &lt;tt class="docutils literal"&gt;&amp;quot;\n&amp;quot;*&lt;/tt&gt;, so in my second attempt at describing feeding, I
suggested this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
_ :: void ← [ \t]*
__ ← &amp;quot;\n&amp;quot;*
&lt;/pre&gt;
&lt;p&gt;But this is wrong on at least 2 counts. It doesn't match this input:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
2
 3
&lt;/pre&gt;
&lt;p&gt;because there's nothing to match the space on the second line. Possibly
we could fix that with &lt;tt class="docutils literal"&gt;__ ← &lt;span class="pre"&gt;(&amp;quot;\n&amp;quot;&lt;/span&gt; _)*&lt;/tt&gt;. But the test harness that I
have includes the terminating &lt;tt class="docutils literal"&gt;\n&lt;/tt&gt; character at the end of the input,
so it still doesn't work. What &lt;em&gt;does&lt;/em&gt; work is this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
_ :: void ← [ \t\n]*
__ ← ε
&lt;/pre&gt;
&lt;p&gt;Recall that the epsilon &lt;tt class="docutils literal"&gt;ε&lt;/tt&gt; is an optional placeholder: the &lt;tt class="docutils literal"&gt;__&lt;/tt&gt;
rule now matches nothing! All of which makes me think that I've taken a
wrong turn. If the most useful thing for &lt;tt class="docutils literal"&gt;__&lt;/tt&gt; to match is nothing,
then it makes no sense for it to be a rule at all. Should it not be an
operator in the grammar? The &lt;tt class="docutils literal"&gt;$&lt;/tt&gt; character is still available, so it
could look like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Sum ← _ a:Digit $ b:Digit → { a + b }
Digit ← [0-9] _  → { *ref_ptr(ref()) - '0' }
_ :: void ← [ \t]*
&lt;/pre&gt;
&lt;p&gt;I need to play with some more examples to see if this is a genuine
improvement, but it looks plausible at the moment.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-05-28</id><link href="https://paccrat.org/entry/2013-05-28"/><updated>2015-05-24T19:45:23-00:00</updated><title>Back it up</title><content type="html">&lt;div class="document" id="back-it-up"&gt;
&lt;h1 class="title"&gt;Back it up&lt;/h1&gt;

&lt;p&gt;This has got out of hand. I really like the work that I've been doing,
and I'm certain that I will incorporate it into pacc. But not &lt;em&gt;this&lt;/em&gt;
release.&lt;/p&gt;
&lt;p&gt;I'd finished every scheduled item on the todo list, except completing
the documentation, and had considered the code frozen. It is clear from
my experiments that implementing “new rep” (or “rep rules” or “for loops”
or whatever they end up being called) will be a &lt;em&gt;significant&lt;/em&gt;
perturbation of the code. Dearly though I would love to implement and
include it soon, the world will be a better place if I release first.&lt;/p&gt;
&lt;p&gt;So, I'm back to putting the finishing touches to the documentation. The
subtraction example can simply describe how left recursion is very hard
to do in pacc at present, with a teaser that a decent solution is in the
works. And with some exciting coding in prospect, I should actually be
able to muster the discipline to get the release out there.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-05-27</id><link href="https://paccrat.org/entry/2013-05-27"/><updated>2015-05-24T19:45:28-00:00</updated><title>Rep-rules again</title><content type="html">&lt;div class="document" id="rep-rules-again"&gt;
&lt;h1 class="title"&gt;Rep-rules again&lt;/h1&gt;

&lt;p&gt;It's so long since I've done any serious work on the parser engine that
I've more or less completely forgotten how it works. So I've begun a
side-project of documenting the internals. I've covered a significant
amount of ground, which has refreshed my memory. (And also given me a
few things to tidy up at a later date.)&lt;/p&gt;
&lt;p&gt;With my recovered knowledge in hand, I've written a “rep-rule” version
of the unary parser that actually gives the right answers. It's not
quite right though—the current version doesn't handle bindings, and
can't be made to do so.&lt;/p&gt;
&lt;p&gt;The first part seems reasonably clear: to match a rep-rule, we have a
&lt;tt class="docutils literal"&gt;for&lt;/tt&gt; loop that looks something like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
for (n = -1; _status == parsed; ++n) { ...
&lt;/pre&gt;
&lt;p&gt;That allows us to count how many matches we can make of the repeated
thing (let's call it the &lt;em&gt;repetand&lt;/em&gt;) at the current parsing point. And
then we test &lt;tt class="docutils literal"&gt;n&lt;/tt&gt; against the bounds of the particular repetition
operator to see if we have an overall match at all. That's all fine and
good.&lt;/p&gt;
&lt;p&gt;The trickier part is how to evaluate it. At present, I simply evaluate
the associated expression &lt;tt class="docutils literal"&gt;n&lt;/tt&gt; times. In the case of the unary example,
the expression is &lt;tt class="docutils literal"&gt;a + 1&lt;/tt&gt;, so we have the moral equivalent of:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
for (i = 0; i &amp;lt; n; ++i) a = a + 1;
&lt;/pre&gt;
&lt;p&gt;But that's definitely not going to work in the presence of bindings. To
make those work, consider an input of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;3-2-1&lt;/span&gt;&lt;/tt&gt;. The repetand will be
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;&amp;quot;-&amp;quot;&lt;/span&gt; d:decimal&lt;/tt&gt;, and we will need to record on evlis that we must call
&lt;tt class="docutils literal"&gt;decimal&lt;/tt&gt; when &lt;tt class="docutils literal"&gt;_x&lt;/tt&gt; is 2 (returns 2) and 4 (returns 1). To evaluate
that, first of all we'll have to notice that the repetand contains a
single binding. So we'll need to step through evlis 1 item at a time,
evaluating a single call to &lt;tt class="docutils literal"&gt;decimal&lt;/tt&gt; and binding its value to &lt;tt class="docutils literal"&gt;d&lt;/tt&gt;,
then evaluating &lt;tt class="docutils literal"&gt;a = a - d&lt;/tt&gt;. If the repetand were to make 2 bindings,
then we would need to step through evlist 2 items at a time.&lt;/p&gt;
&lt;p&gt;I'm inclined to say that the repetand may &lt;em&gt;not&lt;/em&gt; contain alternatives
(directly; it can do so by calling a rule of course). If we allow a
repetand like &lt;tt class="docutils literal"&gt;&amp;quot;+&amp;quot; d { a + d } / &lt;span class="pre"&gt;&amp;quot;-&amp;quot;&lt;/span&gt; d { a - d }&lt;/tt&gt; then we'll have to
record somewhere which of the alternative expressions we need to
evaluate. In this particular case, there is no problem to forbidding the
alternative: we can simply define:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
sumTail &amp;lt;- &amp;quot;+&amp;quot; d:decimal { d } / &amp;quot;-&amp;quot; d:decimal { -d }
&lt;/pre&gt;
&lt;p&gt;and make the repetand &lt;tt class="docutils literal"&gt;s:sumTail { a + s }&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;The other most likely case I can think of is that we would be building a
list, and one of the alternatives would push something on to the list
&lt;tt class="docutils literal"&gt;{ push(a, d) }&lt;/tt&gt; and the other wouldn't &lt;tt class="docutils literal"&gt;{ a }&lt;/tt&gt;. It seems to me that
in most realistic cases we could handle this with a &lt;tt class="docutils literal"&gt;maybe_push&lt;/tt&gt;
function that pushes only if &lt;tt class="docutils literal"&gt;d != 0&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;A second advantage to outlawing alternatives within the repetand is that
it makes the user syntax much easier to conceive of. I think I'd very
much like to base it on the &lt;tt class="docutils literal"&gt;for&lt;/tt&gt; loop, so for example we might
write:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
sum ← v:decimal (v; w:sumTail; a + w)*
sumTail ← &amp;quot;+&amp;quot; d:decimal { d } / &amp;quot;-&amp;quot; d:decimal { -d }
&lt;/pre&gt;
&lt;p&gt;It looks a little odd to have expressions without &lt;tt class="docutils literal"&gt;{&lt;/tt&gt; braces &lt;tt class="docutils literal"&gt;}&lt;/tt&gt;,
but it's succinct, and eminently parseable. Just to complete the other
two examples, we have a decimal parsed by pacc:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
decimal ← (0; d:digit; 10 * a + d)+
&lt;/pre&gt;
&lt;p&gt;And parsing a unary number:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
unary ← (0; &amp;quot;1&amp;quot;; a + 1)+
&lt;/pre&gt;
&lt;p&gt;I very much like the look of all this. Now, can I implement it?&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-05-19</id><link href="https://paccrat.org/entry/2013-05-19"/><updated>2015-05-24T19:45:29-00:00</updated><title>New-rep details</title><content type="html">&lt;div class="document" id="new-rep-details"&gt;
&lt;h1 class="title"&gt;New-rep details&lt;/h1&gt;

&lt;p&gt;I'm starting to implement what I'm calling “new-rep”. To begin with, we
can gloss over the details of exactly how it will be expressed in pacc,
but we will need to decide how it will look in the AST. My first test
case will be the unary counting grammar, which might look a bit like
this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
S ← &amp;quot;x&amp;quot; { 0 } (&amp;quot;y&amp;quot; { a + 1 })* &amp;quot;x&amp;quot; → a
&lt;/pre&gt;
&lt;p&gt;And in the AST, looks like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
grammar 114: (null)
  preamble 113: (null)
  rule 112: S
    type 111: int
    seq 110:
      lit 109: x
      rep 108: (null)
        expr 107: 0
        seq 106:
          lit 105: y
          expr 104: a+1
            ident 103: a
      lit 102: x
      expr 101: a
        ident 100: a
&lt;/pre&gt;
&lt;p&gt;However, there is already a major snag. What is the type of &lt;tt class="docutils literal"&gt;a&lt;/tt&gt;?
Obviously it will often, as this case, be the same as the enclosing
rule, but in general it need not be. I suppose it might be possible to
add a type-annotation in there somewhere; perhaps the base case could be
&lt;tt class="docutils literal"&gt;{ 0 :: int }&lt;/tt&gt;, but that's not exactly appealing. The obvious thing to
do is to say that a new-rep must always be a complete rule. That may
make the syntax more appealing too. Let's see what our examples might
look like:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
S ← &amp;quot;x&amp;quot; u:Unary &amp;quot;x&amp;quot; → u
Unary ← { 0 } (&amp;quot;y&amp;quot; { a + 1 })*
&lt;/pre&gt;
&lt;p&gt;In this case, the value of the new-rep rule defaults to the final value
of the accumulator, although we may need an explicit &lt;tt class="docutils literal"&gt;→ a&lt;/tt&gt; for the
time being. Or how about this?:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Unary* ← { 0 } &amp;quot;y&amp;quot; { a + 1 }
&lt;/pre&gt;
&lt;p&gt;This is rather appealing. In the earlier examples, I've felt that the
all-important repetition operator has got a bit lost.  What about that
tricky sum example? I actually sketched that out &lt;a class="reference external" href="2013-05-18"&gt;yesterday&lt;/a&gt; and rejected
it, but hadn't thought about types at that stage. And the decimal
example is already a single rule.&lt;/p&gt;
&lt;p&gt;Another advantage of restricting new-reps to separate rules is that we
don't have to worry about them nesting, which would present problems
with the binding of &lt;tt class="docutils literal"&gt;a&lt;/tt&gt;. Well, perhaps not. Clearly a rep-rule can
call another one, and we'll have to keep things straight.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-05-18</id><link href="https://paccrat.org/entry/2013-05-18"/><updated>2015-05-24T19:45:22-00:00</updated><title>Finally... left recursion</title><content type="html">&lt;div class="document" id="finally-left-recursion"&gt;
&lt;h1 class="title"&gt;Finally... left recursion&lt;/h1&gt;

&lt;p&gt;With the help of &lt;a class="reference external" href="http://www.dalnefre.com/wp/2011/05/parsing-expression-grammars-part-4/"&gt;Dale Schumacher&lt;/a&gt;, I have cracked left recursion. At
least, I have a paper design that I believe I can implement. I think I
will attempt to do this before the next release, as really without it
pacc is very hard to use for a number of important cases. It should also
make it possible to implement arbitrary counted repetitions, such as
&lt;tt class="docutils literal"&gt;{2,4}&lt;/tt&gt;. As a bonus, this should make certain error messages less
opaque. Finally, I believe it to be an original piece of work.&lt;/p&gt;
&lt;p&gt;At present, pacc implements &lt;tt class="docutils literal"&gt;?&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;*&lt;/tt&gt;, and &lt;tt class="docutils literal"&gt;+&lt;/tt&gt; by rewriting them
into rules. I had vague plans to someday implement arbitrary
repetitions, presumably using the &lt;tt class="docutils literal"&gt;{n,m}&lt;/tt&gt; syntax of Perl-compatible
regular expressions. The good news is that, if I can do so, then &lt;tt class="docutils literal"&gt;?&lt;/tt&gt;
is simply sugar for &lt;tt class="docutils literal"&gt;{0,1}&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;*&lt;/tt&gt; for &lt;tt class="docutils literal"&gt;{,}&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;+&lt;/tt&gt; for
&lt;tt class="docutils literal"&gt;{1,}&lt;/tt&gt;. The AST for pacc already reflects this.&lt;/p&gt;
&lt;p&gt;Schumacher's important observation is that left recursion is not
important for defining which inputs a grammar recognises. The following
three rules all express exactly the same set of valid inputs:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
left ← left &amp;quot;-&amp;quot; decimal / decimal
right ← decimal &amp;quot;-&amp;quot; right / decimal
tail ← decimal (&amp;quot;-&amp;quot; decimal)*
&lt;/pre&gt;
&lt;p&gt;Well, strictly speaking the first doesn't express anything at all; it's
not a valid PEG. The second does match the right set of inputs, but
unfortunately if we add the obvious expressions it makes the &lt;tt class="docutils literal"&gt;-&lt;/tt&gt;
operator right associative.&lt;/p&gt;
&lt;p&gt;That leaves the third possibility. At present, pacc can only return a
&lt;tt class="docutils literal"&gt;ref_t&lt;/tt&gt; for a repetition operator. The basic idea of this proposal is
that pacc will evaluate the repetition operator from left to right. This
is rather similar to the &lt;tt class="docutils literal"&gt;foldl&lt;/tt&gt; function; we have a base value, and a
way to fold each new value that we match onto it. So here's what it
could look like for the subtraction example:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
sub ← v:decimal { v } (&amp;quot;-&amp;quot; d:decimal { a - d })*
&lt;/pre&gt;
&lt;p&gt;The first thing that is a bit strange here is the identifier &lt;tt class="docutils literal"&gt;a&lt;/tt&gt;. This
is the accumulator. We could use some special syntax for this (&lt;tt class="docutils literal"&gt;$$&lt;/tt&gt;
anyone?) but I'm inclined to simply say that it is always called &lt;tt class="docutils literal"&gt;a&lt;/tt&gt;.
Now, the syntax here is a bit convoluted, but I'm not quite sure how to
improve it. Because I think we need to be able to handle a mixed sum,
like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
additive ← v:decimal { v }
             ( &amp;quot;+&amp;quot; d:decimal { a + d }
             / &amp;quot;-&amp;quot; d:decimal { a - d } )*
&lt;/pre&gt;
&lt;p&gt;In any case, since we can certainly include a (parenthesized) &lt;tt class="docutils literal"&gt;alt&lt;/tt&gt;
inside a &lt;tt class="docutils literal"&gt;rep&lt;/tt&gt;, it would be nice to allow a case like this. One thing
that isn't quite clear to me at the moment is whether we have to have
another &lt;tt class="docutils literal"&gt;expr&lt;/tt&gt; to give the overall value of the rule.&lt;/p&gt;
&lt;p&gt;Would it work better to say that this special processing can only apply
to an entire rule? And the final value of the accumulator yields the
value of the rule. So the last example could look something like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
additive ← v:decimal w:additivetail { v + w }
additivetail ← { 0 }
                 ( &amp;quot;+&amp;quot; d:decimal { a + d }
                 / &amp;quot;-&amp;quot; d:decimal { a - d } )*
&lt;/pre&gt;
&lt;p&gt;That's really not an improvement over simply saying that &lt;tt class="docutils literal"&gt;a&lt;/tt&gt; remains
in scope till the end of the rule (and the previous example needs a
final &lt;tt class="docutils literal"&gt;→ a&lt;/tt&gt; to give its overall value).&lt;/p&gt;
&lt;p&gt;Let's look at a couple more examples. I struggled with trying to
implement &lt;tt class="docutils literal"&gt;atoi()&lt;/tt&gt; in pacc——again it demands left association. Here's
how to do it:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
decimal ← { 0 } ( d:digit { a * 10 + d } )+ → a
&lt;/pre&gt;
&lt;p&gt;And for a minimal example, what about simply counting the number of
repetitions?&lt;/p&gt;
&lt;pre class="literal-block"&gt;
unary ← { 0 } ( &amp;quot;1&amp;quot; { a + 1 } )+ → a
&lt;/pre&gt;
&lt;p&gt;That's about it. These examples aren't fantastically pretty, but
I think they can be made to work. If I can get the backend sorted, I can
perhaps tweak the syntax; it strikes me that &lt;tt class="docutils literal"&gt;;&lt;/tt&gt; is currently unused.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-05-12</id><link href="https://paccrat.org/entry/2013-05-12"/><updated>2015-05-24T19:45:24-00:00</updated><title>Darn</title><content type="html">&lt;div class="document" id="darn"&gt;
&lt;h1 class="title"&gt;Darn&lt;/h1&gt;

&lt;p&gt;Just had one of those “oh darn” experiences. I expect it'll all work out
ok in the end, but it does seem to be a major flaw that has gone
previously unnoticed.&lt;/p&gt;
&lt;p&gt;I've been playing around with left recursion, which is in itself a very
hard problem. It's also an important one: at present, it is &lt;em&gt;impossible&lt;/em&gt;
to write a pacc parser that can subtract 3 numbers correctly. Is that
too strong a word? I wondered if I could follow Bryan Ford, perhaps even
manually at first. The transformation performed by pappy is a pragmatic
approach, but it involves higher order functions and partial
application, so is scarcely amenable to implementing in C. (As far as I
can tell, Rats! performs an equivalent transformation using some nasty
Java libraries.)&lt;/p&gt;
&lt;p&gt;I'd rather follow &lt;a class="reference external" href="http://www.dalnefre.com/wp/2011/05/parsing-expression-grammars-part-4/"&gt;Dale Schumacher&lt;/a&gt;, and I'm going to study his idea
further. I think it might be doable by adding special notation that
allows the pacc grammar to specify how the result of &lt;strong&gt;*&lt;/strong&gt; (and &lt;strong&gt;+&lt;/strong&gt;)
operators will be assembled. The special trick will be to allow a
binding from each iteration to be in scope for the next one, which is
why it is more than can be done simply by manually expanding the
repetition operator.&lt;/p&gt;
&lt;p&gt;But before I got onto that, I thought that  there might just be a way to
do it, at least for the subtraction example, by converting to reverse
Łukasiewicz notation. (As one of my college lecturers explained, we call
it “reverse Polish” notation only because no-one is confident of how to
pronounce his name. The first letter of course has a “w” sound.)
However, whilst playing around with this idea, I started writing a very
modest grammar that happened to have two function definitions in the
preamble. Bang! Pacc cannot parse such a thing.&lt;/p&gt;
&lt;p&gt;After some considerably head scratching, I realised that the problem is
in this definition:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
CodeNames
  ← n:Name ns:CodeNames { s_set_cons(s_text(ident, n), ns) }
  / StringLit ns:CodeNames → ns
  / CharLit ns:CodeNames → ns
  / C_Comment ns:CodeNames → ns
  / [^{}] ns:CodeNames → ns
  / lBrace ns:CodeNames rBrace → ns
  / ε { 0 }
&lt;/pre&gt;
&lt;p&gt;This handles as many nested braces as required, &lt;tt class="docutils literal"&gt;{ { { } } }&lt;/tt&gt;, but
cannot handle even a single pair in sequence, &lt;tt class="docutils literal"&gt;{ { } { } }&lt;/tt&gt;. The
penultimate alternative needs to be something like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
/ lBrace ns:CodeNames* rBrace → ns
&lt;/pre&gt;
&lt;p&gt;But that's not going to work, because we have no control over how
the result of a repetition operator is assembled. (Is this starting to
sound familiar?) At present, you just get a &lt;tt class="docutils literal"&gt;ref_t()&lt;/tt&gt;. Anyway, after
peering at this for quite a while, the lack of symmetry finally jumped
out. That line should read:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
/ lBrace ns:CodeNames rBrace ps:CodeNames → { append(ns, ps) }
&lt;/pre&gt;
&lt;p&gt;I still feel that there's a &lt;tt class="docutils literal"&gt;*&lt;/tt&gt; operator trying to escape there, but
it will do for now. At least the bug is fixed: preambles work as
expected, and can contain (multiple!) function definitions.&lt;/p&gt;
&lt;p&gt;So back to left recursion. With considerable effort, I have implemented
a trivial grammar that can evaluate &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;3-2-1&lt;/span&gt;&lt;/tt&gt; and get &lt;tt class="docutils literal"&gt;0&lt;/tt&gt;. It's pretty
grody though: the code has to maintain its own stack to ensure that the
subtractions are evaluated in the right order. There are already so many
stacks in pacc, I would have thought one of them could do the job.&lt;/p&gt;
&lt;p&gt;Not quite sure where to go with this at the moment. I really don't want
to frob with the code much more before the release if I can help it.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-03-18</id><link href="https://paccrat.org/entry/2013-03-18"/><updated>2016-08-03T21:31:33-00:00</updated><title>More on documentation</title><content type="html">&lt;div class="document" id="more-on-documentation"&gt;
&lt;h1 class="title"&gt;More on documentation&lt;/h1&gt;

&lt;p&gt;Fantastic! I was nervous about trying to upload the new texinfo-based
documentation to the website, fearing first that I'd have to rebuild the
yesod app, and secondly that there would be painful stylesheet clashes.&lt;/p&gt;
&lt;p&gt;I can cope with either of those, and no doubt will in the fulness of
time, but just at the moment my time budget is tight, and I'd really
like to get another release out without engaging on another tedious
doc-twiddling exercise. So I was delighted with &lt;a class="reference external" href="https://paccrat.org/article/index"&gt;this&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;It's simply an upload of the texinfo output, with one simple change:
texinfo likes to add the &lt;tt class="docutils literal"&gt;.html&lt;/tt&gt; filename suffix to every url, whereas
yesod is more sensible. But a swift &lt;tt class="docutils literal"&gt;sed &lt;span class="pre"&gt;-i&lt;/span&gt; &lt;span class="pre"&gt;'s/\.html//g'&lt;/span&gt; *.html&lt;/tt&gt;
sorts that out. It's not perfect (I will tweak the styles eventually),
but it's perfectly usable, for almost zero effort. Another point to
texinfo.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-02-27</id><link href="https://paccrat.org/entry/2013-02-27"/><updated>2015-05-24T19:45:26-00:00</updated><title>Documentation</title><content type="html">&lt;div class="document" id="documentation"&gt;
&lt;h1 class="title"&gt;Documentation&lt;/h1&gt;

&lt;p&gt;Sigh. Unix documentation, balkanization. Clearly we have to ship with a
&lt;strong&gt;man&lt;/strong&gt; page. It's a &lt;em&gt;very&lt;/em&gt; long time since I've written anything in
&lt;strong&gt;troff&lt;/strong&gt;. For some recent projects, I've used &lt;strong&gt;pod&lt;/strong&gt; to produce
&lt;strong&gt;man&lt;/strong&gt; pages. For one I wrote the documentation in XHTML and wrote a
Haskell monadic combinator parser to output &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-man&lt;/span&gt;&lt;/tt&gt; &lt;strong&gt;troff&lt;/strong&gt; from
that (OK, so that was insane).&lt;/p&gt;
&lt;p&gt;These days, I am an enormous fan of &lt;strong&gt;reStructuredText&lt;/strong&gt; (although not the
language that spawned it), and I am on a one-person crusade to stop
&lt;strong&gt;reST&lt;/strong&gt; becoming the Betamax to &lt;strong&gt;markdown&lt;/strong&gt;'s VHS. So if all I want is
a &lt;strong&gt;man&lt;/strong&gt; page, then &lt;strong&gt;rst2man&lt;/strong&gt; is the way to go.&lt;/p&gt;
&lt;p&gt;But I am intending &lt;strong&gt;pacc&lt;/strong&gt; eventually to be a &lt;strong&gt;GNU&lt;/strong&gt; project. So I
&lt;em&gt;suppose&lt;/em&gt; I should write a &lt;strong&gt;texinfo&lt;/strong&gt; manual, and use &lt;strong&gt;help2man&lt;/strong&gt; to
generate a minimal &lt;strong&gt;man&lt;/strong&gt; page. I suppose I could quickly try the
second half... And, y'know, it actually doesn't do too bad a job, if you
can stand that sort of manual page. Hmm...&lt;/p&gt;
&lt;p&gt;So here's one possibility: &lt;strong&gt;rst2texinfo&lt;/strong&gt; exists. Come to that, the
wonderful &lt;strong&gt;pandoc&lt;/strong&gt; can do the job too (although I have found a few
shortcomings in its &lt;strong&gt;reST&lt;/strong&gt; parser (yes, I should fix them and submit
patches)). But that's not going to work out well. For example, my
&lt;strong&gt;reST&lt;/strong&gt; markup uses a fixed-width font for command text and filenames,
whereas &lt;strong&gt;texinfo&lt;/strong&gt; distinguishes those.&lt;/p&gt;
&lt;p&gt;I guess the Right Thing is to write a proper &lt;strong&gt;texinfo&lt;/strong&gt; manual. I
dislike the &lt;strong&gt;texinfo&lt;/strong&gt; HTML output, but that can probably be smartened
up with a modicum of XSLT and a sprinkling of CSS. And it does look nice
on paper, of course. OK. I've avoided &lt;strong&gt;texinfo&lt;/strong&gt; as far as possible for
all these years, but let's give it a go.&lt;/p&gt;
&lt;p&gt;Some random notes as I convert the &lt;strong&gt;reST&lt;/strong&gt; documentation to
&lt;strong&gt;texinfo&lt;/strong&gt;.&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;texinfo is about as annoying to write as pod, but of course it does
more.&lt;/li&gt;
&lt;li&gt;the TeX backend doesn't support Unicode, which is a pain as we really
need to talk about the Unicode characters that pacc can use. I'm not
going to worry about this too much, as it rather looks like there's a
fix in Fedora rawhide already.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I've now converted all the documentation I have into texinfo, and I'm
reasonably pleased with the result. Of course, more documentation is
needed, and the texinfo source can also be spruced up with nodes and
index entries.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-02-25</id><link href="https://paccrat.org/entry/2013-02-25"/><updated>2015-05-24T19:45:21-00:00</updated><title>Cleanups</title><content type="html">&lt;div class="document" id="cleanups"&gt;
&lt;h1 class="title"&gt;Cleanups&lt;/h1&gt;

&lt;p&gt;Knocking off the last few items on the &lt;a class="reference external" href="/article/todo"&gt;todo list&lt;/a&gt;. One was:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;command line option to enable tracing&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I haven't quite done this. Instead, there is a new macro
&lt;tt class="docutils literal"&gt;PACC_CAN_TRACE&lt;/tt&gt;, either in the &lt;tt class="docutils literal"&gt;.h&lt;/tt&gt; file if &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-d&lt;/span&gt;&lt;/tt&gt; was specified,
otherwise at the top of the &lt;tt class="docutils literal"&gt;.c&lt;/tt&gt; file. Tracing code is prefixed with
&lt;tt class="docutils literal"&gt;PACC_TRACE&lt;/tt&gt; which expands to:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
if (PACC_CAN_TRACE &amp;amp;&amp;amp; PACC_SYM(trace))
&lt;/pre&gt;
&lt;p&gt;Thus if &lt;tt class="docutils literal"&gt;PACC_CAN_TRACE&lt;/tt&gt; is &lt;tt class="docutils literal"&gt;0&lt;/tt&gt;, the conditional is a compile-time
constant zero, and the compiler will (hopefully!) optimize away the test
and the code it protects. If &lt;tt class="docutils literal"&gt;PACC_CAN_TRACE&lt;/tt&gt; is &lt;tt class="docutils literal"&gt;1&lt;/tt&gt;, then the trace
code will be executed if &lt;tt class="docutils literal"&gt;pacc_trace&lt;/tt&gt; is also &lt;tt class="docutils literal"&gt;1&lt;/tt&gt;: this can be set
by the code calling the parser in whatever way it wants.&lt;/p&gt;
&lt;p&gt;I also changed the &lt;tt class="docutils literal"&gt;Instr&lt;/tt&gt; code (which reports on hash chain
statistics) to use the &lt;tt class="docutils literal"&gt;PACC_TRACE&lt;/tt&gt; macro instead, as there was really
just one stage of development where it was helpful to separate the two.&lt;/p&gt;
&lt;p&gt;Another item on the list is to move all names into the namespaces I am
claiming: &lt;tt class="docutils literal"&gt;pacc&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;_pacc&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;PACC&lt;/tt&gt;, and &lt;tt class="docutils literal"&gt;ref&lt;/tt&gt;. (Originally
&lt;tt class="docutils literal"&gt;pacc&lt;/tt&gt; was to be used for names that the &lt;strong&gt;pacc&lt;/strong&gt; user might have
occasion to use, and &lt;tt class="docutils literal"&gt;_pacc&lt;/tt&gt; was for purely internal names, although I
haven't been entirly consistent about this.) This includes both
external names, and names which are in scope when user code copied from
the &lt;strong&gt;pacc&lt;/strong&gt; grammar is being executed, which makes this quite
burdensome. For example, &lt;tt class="docutils literal"&gt;col&lt;/tt&gt; which identifies the current character
in the input is used in almost every line of the generated parser. Not
only is &lt;tt class="docutils literal"&gt;pacc_col&lt;/tt&gt; verbose, but by the time every identifier starts
with &lt;tt class="docutils literal"&gt;pacc_&lt;/tt&gt; the code will be extremely hard for a human to read.&lt;/p&gt;
&lt;p&gt;So I'm thinking that, whilst it is right to use the &lt;tt class="docutils literal"&gt;pacc&lt;/tt&gt; prefix for
external names, perhaps I could just use an underscore prefix for names
static to the parser.&lt;/p&gt;
&lt;p&gt;That's now completed. Some variable names could be better chosen, and I
think one or two could be eliminated, or have their scope tightened (for
example, I'm almost &lt;tt class="docutils literal"&gt;_pacc_any_i&lt;/tt&gt; could be scoped inside the couple of
loops that use it). But those can wait for another release.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-02-19</id><link href="https://paccrat.org/entry/2013-02-19"/><updated>2015-05-24T19:45:22-00:00</updated><title>Tracing</title><content type="html">&lt;div class="document" id="tracing"&gt;
&lt;h1 class="title"&gt;Tracing&lt;/h1&gt;

&lt;p&gt;One of the few &lt;em&gt;blockers&lt;/em&gt; left on the &lt;a class="reference external" href="/article/todo"&gt;todo list&lt;/a&gt; is “command line
option to enable tracing”. That's clear enough, but to make that work,
we really want to define a macro that will switch tracing on or off in
&lt;tt class="docutils literal"&gt;pacc.h&lt;/tt&gt;. And that's fine, except... &lt;tt class="docutils literal"&gt;pacc.c&lt;/tt&gt; doesn't actually
include &lt;tt class="docutils literal"&gt;pacc.h&lt;/tt&gt;! My argument was that, since both files are
automatically created, we don't need the customary consistency check
between &lt;tt class="docutils literal"&gt;.c&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;.h&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;I was never very convinced by that argument, but it is a trifle awkward
to include the header file, as its name is not constant, so it will need
to be patched into the template output.&lt;/p&gt;
&lt;p&gt;Another blocker is “expose ability to alter prefix”, which is obviously
related, since currently the prefix is the only non-constant item in the
template output. The prefix is represented by &lt;tt class="docutils literal"&gt;YY&lt;/tt&gt; in the template,
and currently is always &lt;tt class="docutils literal"&gt;pacc&lt;/tt&gt; or &lt;tt class="docutils literal"&gt;pacc_feed&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Stage one is to add a new command-line argument: &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-n&lt;/span&gt; NAME&lt;/tt&gt; (or
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;--name=NAME&lt;/span&gt;&lt;/tt&gt;). This defaults to the input filename less its &lt;tt class="docutils literal"&gt;.pacc&lt;/tt&gt;
suffix. Now the output filename defaults to &lt;tt class="docutils literal"&gt;NAME.c&lt;/tt&gt; (but can of
course be specified with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-o&lt;/span&gt;&lt;/tt&gt;), and the defines filename defaults to
&lt;tt class="docutils literal"&gt;NAME.h&lt;/tt&gt; (if enabled with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-d&lt;/span&gt;&lt;/tt&gt; and not explicitly given). This name
is now pasted into the root node of the AST, but I haven't done anything
with it yet.&lt;/p&gt;
&lt;p&gt;Hmm. But there's an awful snag here: &lt;tt class="docutils literal"&gt;NAME&lt;/tt&gt; must be a valid &lt;strong&gt;C&lt;/strong&gt;
identifier, so what do we do if the input filename is &lt;tt class="docutils literal"&gt;test/one.pacc&lt;/tt&gt;
or &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;one-two.pacc&lt;/span&gt;&lt;/tt&gt;? Answer, nothing: the idea of deriving &lt;tt class="docutils literal"&gt;NAME&lt;/tt&gt; from
the input filename is simply bogus. &lt;tt class="docutils literal"&gt;NAME&lt;/tt&gt; can simply be &lt;tt class="docutils literal"&gt;pacc&lt;/tt&gt;; in
the rare cases where the user wants more than one parser in a program,
she will have to devise distinct names for the parsers.&lt;/p&gt;
&lt;p&gt;A thought: why don't we simply have a single macro like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
#define PACC_NAME whatever
&lt;/pre&gt;
&lt;p&gt;and use token-pasting? That's at least worth experimenting with. And it
works very nicely indeed. I was rather proud of the &lt;tt class="docutils literal"&gt;sed&lt;/tt&gt; script that
turned &lt;tt class="docutils literal"&gt;pacc.tmpl&lt;/tt&gt; into the &lt;strong&gt;C&lt;/strong&gt; code to write a parser, but the
tricky part of that has now gone, to be replaced by some trick &lt;strong&gt;C&lt;/strong&gt;
preprocessor macros.&lt;/p&gt;
&lt;p&gt;So the first thing is to tidy up so that &lt;tt class="docutils literal"&gt;pacc.c&lt;/tt&gt; actually includes
a &lt;strong&gt;pacc&lt;/strong&gt;-created &lt;tt class="docutils literal"&gt;pacc.h&lt;/tt&gt;. One thing that strikes me is that
currently we include the preamble in both &lt;tt class="docutils literal"&gt;pacc.c&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;pacc.h&lt;/tt&gt;. We
need this in &lt;strong&gt;pacc&lt;/strong&gt; itself, because it is the preamble that includes
&lt;tt class="docutils literal"&gt;syntax.h&lt;/tt&gt;, which we need for the types of &lt;tt class="docutils literal"&gt;yy_wrap()&lt;/tt&gt; etc. However,
in general, this probably isn't right. I think we will need eventually
to have &lt;em&gt;two&lt;/em&gt; preambles, which is going to be a complicated change.&lt;/p&gt;
&lt;p&gt;In the end, though, I have gone the other way. The problem is that,
since &lt;strong&gt;pacc&lt;/strong&gt; compiles itself, any dependency on &lt;tt class="docutils literal"&gt;pacc.h&lt;/tt&gt; ends up
being circular. So it's simpler just to stuff an &lt;tt class="docutils literal"&gt;extern&lt;/tt&gt; declaration
of &lt;tt class="docutils literal"&gt;pacc_wrap()&lt;/tt&gt; at the top of &lt;tt class="docutils literal"&gt;main.c&lt;/tt&gt;. However, this does mean
that &lt;tt class="docutils literal"&gt;pacc &lt;span class="pre"&gt;-d&lt;/span&gt;&lt;/tt&gt; isn't tested any more. I think I need to cook up some
more realistic examples.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-02-06</id><link href="https://paccrat.org/entry/2013-02-06"/><updated>2015-05-24T19:45:27-00:00</updated><title>More on Errors</title><content type="html">&lt;div class="document" id="more-on-errors"&gt;
&lt;h1 class="title"&gt;More on Errors&lt;/h1&gt;

&lt;p&gt;In the last article, I was looking at how to handle “semantic errors”,
such as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[z-a]&lt;/span&gt;&lt;/tt&gt;. As a simplified test case, I'd been playing with this
grammar:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
S ← a:Letter b:Letter ( &amp;amp;{ a &amp;lt; b } / &amp;amp;{ fail(&amp;quot;invalid range&amp;quot;) } )  → { &amp;quot;yes&amp;quot; }
Letter :: char ← [a-z] { ref_0(ref()) }
&lt;/pre&gt;
&lt;p&gt;I have a proof-of-concept implementation of &lt;tt class="docutils literal"&gt;fail()&lt;/tt&gt; that makes this
work, and I'd suggested that we might add some syntactic sugar to make
this cleaner. Well, I think this is rather sweet:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
S ← a:Letter b:Letter &amp;amp;{ a &amp;lt; b || fail(&amp;quot;invalid range&amp;quot;) } → { &amp;quot;yes&amp;quot; }
&lt;/pre&gt;
&lt;p&gt;I don't know why it took me quite so long to hit upon that. Anyway, so
what's really needed is to expose &lt;tt class="docutils literal"&gt;fail()&lt;/tt&gt; properly. Hmm... this is
harder than I thought. The proof-of-concept implementation calls
&lt;tt class="docutils literal"&gt;exit()&lt;/tt&gt;, but that's not what we want to do, we just want to return
early from the parser. But &lt;tt class="docutils literal"&gt;return&lt;/tt&gt; and even &lt;tt class="docutils literal"&gt;goto&lt;/tt&gt; are statements,
not expressions, so even doing nasty tricks with macros I don't see how
&lt;tt class="docutils literal"&gt;fail()&lt;/tt&gt; can easily cause an early exit from the parser.&lt;/p&gt;
&lt;p&gt;I suppose &lt;tt class="docutils literal"&gt;fail()&lt;/tt&gt; could set a flag in the parser object, that we test
often in the engine. So I did a proof-of-concept implementation of that.&lt;/p&gt;
&lt;p&gt;And on further reflection... &lt;strong&gt;yeuch!&lt;/strong&gt; This whole tangent is misgiven.
The “problem” I'm trying to solve is that if I use a semantic guard to
reject &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[z-a]&lt;/span&gt;&lt;/tt&gt; as a character range, my grammar will instead parse it
as a 3-character class, the same as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[-az]&lt;/span&gt;&lt;/tt&gt;. But this is a bug in the
grammar: the alternative interpretation is simply wrong. And if fixing
that bug also means that we &lt;em&gt;don't&lt;/em&gt; interpret &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[a-m-n-z]&lt;/span&gt;&lt;/tt&gt; as the same
range as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[-a-z]&lt;/span&gt;&lt;/tt&gt;, well, at least that will match regular expressions.&lt;/p&gt;
&lt;p&gt;This is all completed now, and available from the git repo:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
git clone git://paccrat.org/pacc
&lt;/pre&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-02-02</id><link href="https://paccrat.org/entry/2013-02-02"/><updated>2015-05-24T19:45:29-00:00</updated><title>New Error Interface</title><content type="html">&lt;div class="document" id="new-error-interface"&gt;
&lt;h1 class="title"&gt;New Error Interface&lt;/h1&gt;

&lt;p&gt;I have been looking at how user-supplied code can report “semantic
errors” in the input. For example, when &lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt; sees a purported
character class such as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[z-a]&lt;/span&gt;&lt;/tt&gt; we want to test that in &lt;tt class="docutils literal"&gt;s_range2()&lt;/tt&gt;
and report it as an invalid range, along with the location of the error.&lt;/p&gt;
&lt;p&gt;The first phase was to split the old error reporting function
&lt;tt class="docutils literal"&gt;YY_error()&lt;/tt&gt; into two.  The replacement &lt;tt class="docutils literal"&gt;YY_error()&lt;/tt&gt; function
generates the specific error we encountered (such as &lt;tt class="docutils literal"&gt;expected Defns,
or &lt;span class="pre"&gt;end-of-input&lt;/span&gt;&lt;/tt&gt;) and hands that to a new &lt;tt class="docutils literal"&gt;YY_pos()&lt;/tt&gt; function which
prepends the position of the error (such as &lt;tt class="docutils literal"&gt;parse.pacc:5:7&lt;/tt&gt;). The
error is now returned as a &lt;tt class="docutils literal"&gt;char *&lt;/tt&gt;, for the outer program to use how
it wishes (such as outputting to &lt;tt class="docutils literal"&gt;stderr&lt;/tt&gt;).&lt;/p&gt;
&lt;p&gt;So now we can use &lt;tt class="docutils literal"&gt;YY_pos()&lt;/tt&gt; to output a semantic error and its
location. Except... darn, we can't. Following our usual object-like
style, the first argument to &lt;tt class="docutils literal"&gt;YY_pos()&lt;/tt&gt; is the parser object, but
that's not known to &lt;tt class="docutils literal"&gt;s_range2()&lt;/tt&gt;. In code spliced in from the grammar,
it &lt;em&gt;is&lt;/em&gt; in scope as &lt;tt class="docutils literal"&gt;_pacc&lt;/tt&gt;, but so far we've avoided mentioning that.
Still, at present I don't see a better way to do it than to pass
&lt;tt class="docutils literal"&gt;_pacc&lt;/tt&gt; in as an extra argument to &lt;tt class="docutils literal"&gt;s_range2()&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Just now though, I've got into a confusing mess where &lt;tt class="docutils literal"&gt;pacc.h&lt;/tt&gt; and
&lt;tt class="docutils literal"&gt;syntax.h&lt;/tt&gt; depend on each other, and I'm too tired to sort it out.&lt;/p&gt;
&lt;p&gt;OK, fixed that one by judicious use of &lt;tt class="docutils literal"&gt;void *&lt;/tt&gt;, but I've run into a
minor hitch and a major snag. The minor hitch is that just now we call
&lt;tt class="docutils literal"&gt;fatal()&lt;/tt&gt; and so output &lt;tt class="docutils literal"&gt;pacc: fatal: parse.pacc:x:y: invalid
range&lt;/tt&gt;. We don't need that much decoration!&lt;/p&gt;
&lt;p&gt;The major snag, though, is that &lt;tt class="docutils literal"&gt;x:y&lt;/tt&gt; always references the end of the
file! Of course, because we don't evaluate anything, except semantic
guards, till the entire grammar is parsed.&lt;/p&gt;
&lt;p&gt;So after some thought, I have devised a way that we can handle this in
semantic guards. Something like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
CRange ← a:CChar &amp;quot;-&amp;quot; b:CChar ( &amp;amp;{ a &amp;lt; b } / &amp;amp;{ fail(&amp;quot;invalid range&amp;quot;) } )
&lt;/pre&gt;
&lt;p&gt;Perhaps later we can add syntactic sugar to this, but first let's see if
it works at all. Yes, that's fine. Needs some tidying up, but I think we
have a goer!&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2013-01-10</id><link href="https://paccrat.org/entry/2013-01-10"/><updated>2015-05-24T19:45:26-00:00</updated><title>Happy New Year!</title><content type="html">&lt;div class="document" id="happy-new-year"&gt;
&lt;h1 class="title"&gt;Happy New Year!&lt;/h1&gt;

&lt;p&gt;Unfortunately I've been incredibly busy recently, first moving home yet
again (hopefully stable for a little bit now) and then dealing with a
family bereavement. However, that's largely dealt with now, and &lt;strong&gt;2013&lt;/strong&gt;
promises to be a fantastic year for &lt;strong&gt;pacc&lt;/strong&gt;!&lt;/p&gt;
&lt;p&gt;The last thing I was working on before the hiatus was a new error
interface. When there is a syntax error detected by &lt;strong&gt;pacc&lt;/strong&gt; itself, it
reports the line and column number where it found the error, as well as
a clue to what the error was. However, sometimes we might not detect an
error in the grammar itself, but in a semantic action.&lt;/p&gt;
&lt;p&gt;For example, consider character classes. A range such as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[a-z]&lt;/span&gt;&lt;/tt&gt; is
valid, whereas a range such as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[z-a]&lt;/span&gt;&lt;/tt&gt; is not, but the difference is
not one that can be expressed in the PEG itself. (In this particular
case, one could argue that the second range has the same meaning as the
first. I don't think that's a good argument in any case, and anyway the
general idea of &lt;em&gt;nearly&lt;/em&gt; syntactic errors is valid.)&lt;/p&gt;
&lt;p&gt;So we need an interface to report errors with line and column numbers.
Or do we? If semantic guards were a bit cleverer, and in particular
allowed us to specify messages, maybe we don't need this feature. The
current definition of a character range looks more or less like this
(I've omitted some casts that keep the compiler quiet):&lt;/p&gt;
&lt;pre class="literal-block"&gt;
CRange
    ← a:CChar &amp;quot;-&amp;quot; b:CChar { s_range2(a, b) }
    / a:CChar { s_range1(a) }
&lt;/pre&gt;
&lt;p&gt;Hmmm... now there's another problem here. If we add a semantic guard to
the first alternative, I was thinking along these lines:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
... a:CChar &amp;quot;-&amp;quot; b:CChar &amp;amp;{ b &amp;gt; a } { s_range2(a, b) } / ...
&lt;/pre&gt;
&lt;p&gt;then a class like &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[z-a]&lt;/span&gt;&lt;/tt&gt; will parse the same as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[-az]&lt;/span&gt;&lt;/tt&gt;, in other
words a class that matches 3 characters. That's definitely not what we
want. There might be ways around that, but I rather like the simplicity
of the PEG definition of a class: a hyphen means a hyphen wherever it
can't be part of a range. This does mean that &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[a-m-z]&lt;/span&gt;&lt;/tt&gt; means the same
as &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[a-mz-]&lt;/span&gt;&lt;/tt&gt;, whereas regular expressions treat it as an error.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-08-29</id><link href="https://paccrat.org/entry/2012-08-29"/><updated>2015-05-24T19:45:26-00:00</updated><title>Odds and Ends</title><content type="html">&lt;div class="document" id="odds-and-ends"&gt;
&lt;h1 class="title"&gt;Odds and Ends&lt;/h1&gt;

&lt;p&gt;I just implemented &lt;tt class="docutils literal"&gt;→ 5&lt;/tt&gt; to go with &lt;tt class="docutils literal"&gt;→ n&lt;/tt&gt;. I had thought to make the
braces optional around any sequence of non-space characters, but the
need to pick out identifiers makes that unappealing: essentially we'd
have to duplicate &lt;tt class="docutils literal"&gt;CodeAndNames&lt;/tt&gt; but without any rules that match
space. Even if you're using Unicode arrows, it's only one more character
to include the braces!&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-08-27</id><link href="https://paccrat.org/entry/2012-08-27"/><updated>2015-05-24T19:45:25-00:00</updated><title>Character escapes in classes</title><content type="html">&lt;div class="document" id="character-escapes-in-classes"&gt;
&lt;h1 class="title"&gt;Character escapes in classes&lt;/h1&gt;

&lt;p&gt;Thanks again to Richard &lt;a class="reference external" href="/article/glossary#smith"&gt;Smith&lt;/a&gt; for pointing out that character escapes
in classes don't work. Here's a new test case inspired by his bug
report; it's slightly more realistic than many of our test cases:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
/*
chars
parse foo foo
parse 'foo bar' 'foo,bar'
parse 'foo  bar' 'foo,bar'
parse 'foo  bar' 'foo,bar'
parse 'foo   456    qux' 'foo,bar,qux'
*/

{
#include &amp;lt;string.h&amp;gt;

char *allocat(char *a, char *b) {
    char *x = malloc(strlen(a) + strlen(b) + 2);
    if (!x) nomem();
    strcpy(x, a); strcat(x, &amp;quot;,&amp;quot;); strcat(x, b);
    return x;
}
}

Words :: char * ← w:Word Space* ws:Words { allocat(w, ws) } / w:Word → w
Word ← (!Space .)+ → { ref_str() }
Space :: void ← [ \t\v\r\n]
&lt;/pre&gt;
&lt;p&gt;It may not be obvious, but the inputs include some tab characters, which
should be picked up as a &lt;tt class="docutils literal"&gt;Space&lt;/tt&gt; by the &lt;tt class="docutils literal"&gt;\t&lt;/tt&gt; in the character class.
Currently, however, &lt;strong&gt;pacc&lt;/strong&gt; treats the tabs as &lt;tt class="docutils literal"&gt;Word&lt;/tt&gt; characters. I
found this surprising, as I was sure that I'd included code to interpret
the traditional character escapes (I already knew we don't support the
fancy new escapes in &lt;strong&gt;C99&lt;/strong&gt;).&lt;/p&gt;
&lt;p&gt;However, on looking at that code again, I realised that it was wrong
(and unfortunately I hadn't written any test cases for it): although we
correctly recognise that &lt;tt class="docutils literal"&gt;\t&lt;/tt&gt; means something special, it ends up
meaning lower-case &lt;strong&gt;t&lt;/strong&gt;!&lt;/p&gt;
&lt;p&gt;Thinking about it some more, I've taken rather a wrong turn in handling
character escapes. I've started building my own compiler for them: in
&lt;tt class="docutils literal"&gt;s_range()&lt;/tt&gt; we put a &lt;tt class="docutils literal"&gt;number&lt;/tt&gt; into the parse tree. While this could
work, it's a lot of effort which we can instead hand off to the &lt;strong&gt;C&lt;/strong&gt;
compiler! Even better, this should mean that to get all the fancy
escapes, we only have to recognise them.&lt;/p&gt;
&lt;p&gt;Well, that's easy, and works beautifully. &lt;em&gt;Almost&lt;/em&gt;. Unfortunately,
although turning &lt;tt class="docutils literal"&gt;[\t]&lt;/tt&gt; into &lt;tt class="docutils literal"&gt;'\t'&lt;/tt&gt; is a great idea, turning &lt;tt class="docutils literal"&gt;[€]&lt;/tt&gt;
into &lt;tt class="docutils literal"&gt;'€'&lt;/tt&gt; isn't so good: it elicits a warning from &lt;strong&gt;gcc&lt;/strong&gt; about
multi-byte character constants.&lt;/p&gt;
&lt;p&gt;So, back to making &lt;tt class="docutils literal"&gt;s_range()&lt;/tt&gt; work. In fact, the task of interpreting
&lt;strong&gt;C&lt;/strong&gt; character escapes is not at all hard, even including the exotic
new ones. I added about 20 lines to &lt;tt class="docutils literal"&gt;s_range()&lt;/tt&gt;, and there are some
extra rules in &lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt; which were largely copied directly from the
syntax in the standard. As usual, one advantage of PEGs shines through:
the &lt;strong&gt;C&lt;/strong&gt; standard has a disambiguating “semantic” rule:&lt;/p&gt;
&lt;blockquote&gt;
Each octal or hexadecimal escape sequence is the longest sequence of
characters that can constitute the escape sequence.&lt;/blockquote&gt;
&lt;p&gt;PEGs avoid ambiguity, though, and the &lt;em&gt;maximal munch&lt;/em&gt; rule is expressed
in the grammar itself:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
OctalEscape ← &amp;quot;\\&amp;quot; [0-7][0-7]?[0-7]?
&lt;/pre&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-08-23</id><link href="https://paccrat.org/entry/2012-08-23"/><updated>2015-05-24T19:45:30-00:00</updated><title>A binding corner case</title><content type="html">&lt;div class="document" id="a-binding-corner-case"&gt;
&lt;h1 class="title"&gt;A binding corner case&lt;/h1&gt;

&lt;p&gt;I am delighted to report that Richard &lt;a class="reference external" href="/article/glossary#smith"&gt;Smith&lt;/a&gt; has been using &lt;strong&gt;pacc&lt;/strong&gt;,
giving me useful feedback, and asking awkward questions (which is
great!).&lt;/p&gt;
&lt;p&gt;I will write some documentation very soon about bindings, but one thing
that came out of our discussion is that in &lt;tt class="docutils literal"&gt;n:Rule&lt;/tt&gt; the type of &lt;tt class="docutils literal"&gt;n&lt;/tt&gt;
is the same as the type of &lt;tt class="docutils literal"&gt;Rule&lt;/tt&gt;. In all other cases, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;n:(not&lt;/span&gt; a
rule)&lt;/tt&gt;, the type of &lt;tt class="docutils literal"&gt;n&lt;/tt&gt; is &lt;tt class="docutils literal"&gt;ref_t&lt;/tt&gt;. I was rather surprised to
discover that it is legal to write &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;n:&amp;quot;foo&amp;quot;&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;So, awkward question coming up: what is the type of &lt;tt class="docutils literal"&gt;n&lt;/tt&gt; in
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;n:(Rule)&lt;/span&gt;&lt;/tt&gt;? One could argue that parentheses are only used for
grouping things (differently from the usual precedence) and therefore
shouldn't change the type of anything. I suspect that this is how
&lt;strong&gt;pacc&lt;/strong&gt; currently interprets it, although I haven't peered at
&lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt; or tested it yet.&lt;/p&gt;
&lt;p&gt;On the other hand, since there's no reason to write redundant
parentheses, we could make &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;n:(Rule)&lt;/span&gt;&lt;/tt&gt; mean that &lt;tt class="docutils literal"&gt;n&lt;/tt&gt; has type
&lt;tt class="docutils literal"&gt;ref_t&lt;/tt&gt;, and is bound to whatever &lt;tt class="docutils literal"&gt;Rule&lt;/tt&gt; matches. Another way to
express this would be &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;n:(Rule&lt;/span&gt; ε)&lt;/tt&gt;. This might occasionally be useful,
so let's make it so.&lt;/p&gt;
&lt;p&gt;That wasn't even trivial to implement: it was already what &lt;strong&gt;pacc&lt;/strong&gt;
does. (Honestly, I really did think about it and choose this option
&lt;em&gt;before&lt;/em&gt; trying it!) Peering at &lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt;, the parentheses rule
looks unpromising:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Rule6 ← lParen r:Rule0 rParen → r
&lt;/pre&gt;
&lt;p&gt;But &lt;tt class="docutils literal"&gt;Rule1&lt;/tt&gt; wraps a &lt;strong&gt;seq&lt;/strong&gt; node around the &lt;strong&gt;call&lt;/strong&gt;, and that's all
we need to trigger the &lt;tt class="docutils literal"&gt;debind()&lt;/tt&gt; code in &lt;tt class="docutils literal"&gt;sugar.c&lt;/tt&gt; that will
achieve what we wanted. There is a new test case to verify that this
works as intended.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-08-06</id><link href="https://paccrat.org/entry/2012-08-06"/><updated>2015-05-24T19:45:26-00:00</updated><title>Supporting void types</title><content type="html">&lt;div class="document" id="supporting-void-types"&gt;
&lt;h1 class="title"&gt;Supporting void types&lt;/h1&gt;

&lt;p&gt;It says &lt;a class="reference external" href="/todo"&gt;here&lt;/a&gt; to “complete” (meaning finish) support for &lt;strong&gt;void&lt;/strong&gt;, but
that's a bit optimistic: at the moment, &lt;strong&gt;void&lt;/strong&gt; is just a funny
spelling for &lt;strong&gt;int&lt;/strong&gt;.  One item that I think falls under this heading,
and I've been planning for quite a while, is to ensure that all paths
through a rule include exactly one &lt;strong&gt;expr&lt;/strong&gt; for non-&lt;strong&gt;void&lt;/strong&gt; rules,
and exactly none for &lt;strong&gt;void&lt;/strong&gt; rules.&lt;/p&gt;
&lt;p&gt;Here's a quote from the &lt;em&gt;Journal&lt;/em&gt; file, that's over 2 years old.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;What about this idea of analysing the tree to find out the maximum
possible &lt;tt class="docutils literal"&gt;evlis&lt;/tt&gt; for each rule? I had imagined walking the tree at
the optimization stage, and couldn't see how to communicate the
information to &lt;tt class="docutils literal"&gt;emit()&lt;/tt&gt;. But actually we could do it in
&lt;tt class="docutils literal"&gt;rule_pre()&lt;/tt&gt; as we are about to emit each rule.&lt;/p&gt;
&lt;p&gt;Insight! If we make this a little more abstract, so it can count the
maximum of any type of node, then we can raise an error if
&lt;tt class="docutils literal"&gt;pathmax(expr) &amp;gt; 1&lt;/tt&gt; (or rule type is &lt;strong&gt;void&lt;/strong&gt; and &lt;tt class="docutils literal"&gt;pathmax(expr) &amp;gt;
0&lt;/tt&gt;):&lt;/p&gt;
&lt;pre class="literal-block"&gt;
int pathmax(struct node *n, enum s_type t) {
    int r = 0;
    struct s_node *p;
    assert(!s_has_children(t));
    if (n-&amp;gt;type == alt)
        for (p = n-&amp;gt;first; p; p = p-&amp;gt;next) {
            int m = pathmax(p, t);
            if (m &amp;gt; r) r = m;
        }
    else if (s_has_children(n-&amp;gt;type))
        for (p = n-&amp;gt;first; p; p = p-&amp;gt;next)
            r += pathmax(p, t);
    else if (n-&amp;gt;type == t)
        r = 1;
    return r;
}
&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;So, let's see if that code actually comes anywhere near working! Pretty
much, with a couple of minor fixes. However, it doesn't go far enough:
we also need a &lt;tt class="docutils literal"&gt;path_min()&lt;/tt&gt;, so we can verify that for non-&lt;strong&gt;void&lt;/strong&gt;
rules, both &lt;tt class="docutils literal"&gt;path_min()&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;path_max()&lt;/tt&gt; are &lt;strong&gt;1&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;That's all done. Very many of the test cases had taken advantage of the
fact that &lt;strong&gt;pacc&lt;/strong&gt; was previously slack about enforcing the &lt;tt class="docutils literal"&gt;void&lt;/tt&gt;
type on rules with no expressions. (With several dozen test cases to
patch up, it did cross my mind briefly to wonder if it is necessary to
be so rigid. But this is so obviously the Right Thing to do, and goes a
long way towards compensating the dangers inherent in type defaulting.)&lt;/p&gt;
&lt;p&gt;Also a few test cases still had &lt;tt class="docutils literal"&gt;int&lt;/tt&gt; instead of &lt;tt class="docutils literal"&gt;void&lt;/tt&gt;, even though
I made the latter a synonym for the former a long time ago. That all
took a little while to clear up, but everything's much neater now.&lt;/p&gt;
&lt;p&gt;Best of all, though, this enhancement fixes one of the (previously two)
expected failures. The test case was this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
P ← a:A { 5 }
A ← &amp;quot;5&amp;quot;
&lt;/pre&gt;
&lt;p&gt;which in very early development actually failed to terminate, hence the
test case. Due to this change, it now fails because the rule &lt;tt class="docutils literal"&gt;A&lt;/tt&gt;
contains no expressions, yet has &lt;tt class="docutils literal"&gt;int&lt;/tt&gt; type. If we change it to &lt;tt class="docutils literal"&gt;A ::
void&lt;/tt&gt;, then the current version of &lt;strong&gt;pacc&lt;/strong&gt; attempts to declare &lt;tt class="docutils literal"&gt;void
a&lt;/tt&gt;, which is of course invalid &lt;strong&gt;C&lt;/strong&gt;. Fixing this will be the last (I
hope) piece of the puzzle in “complete support for &lt;tt class="docutils literal"&gt;void&lt;/tt&gt; types”.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-08-04</id><link href="https://paccrat.org/entry/2012-08-04"/><updated>2015-05-24T19:45:24-00:00</updated><title>UTF-8 support in pacc_coords()</title><content type="html">&lt;div class="document" id="utf-8-support-in-pacc-coords"&gt;
&lt;h1 class="title"&gt;UTF-8 support in pacc_coords()&lt;/h1&gt;

&lt;p&gt;Finally the last missing bit of UTF-8 support is added, to the
&lt;tt class="docutils literal"&gt;pacc_coords()&lt;/tt&gt; function.&lt;/p&gt;
&lt;p&gt;That was straightforward enough, although I realised after writing the
code that I had forgotten to follow the test-based development strategy.
Fortunately, &lt;strong&gt;git&lt;/strong&gt; makes it pretty easy to do things out of sequence:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
git commit -a          # commit the new code
git branch tbd HEAD^   # make a new branch off previous commit
git checkout tbd       # and switch to it
hack...                # implement the test case
test...                # and verify that it fails
git commit -a          # commit the test case
git checkout master    # back to the mainline
git merge tbd          # merge in the test case
&lt;/pre&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-07-21</id><link href="https://paccrat.org/entry/2012-07-21"/><updated>2015-05-24T19:45:25-00:00</updated><title>Fixing coords()</title><content type="html">&lt;div class="document" id="fixing-coords"&gt;
&lt;h1 class="title"&gt;Fixing coords()&lt;/h1&gt;

&lt;p&gt;A major problem with the rōnin release was that the &lt;tt class="docutils literal"&gt;pacc_coords()&lt;/tt&gt;
function performed a linear search on the input each time it was called.
Although I don't actually have a way to measure it, this must have
destroyed the parser's claimed linearity.&lt;/p&gt;
&lt;p&gt;That is now fixed. The &lt;tt class="docutils literal"&gt;pacc_coords()&lt;/tt&gt; function memoizes each result
it returns. When it needs to find a new coordinate pair, it starts from
the closest previous one. Hmm... this could pathologically still be
linear (thus making the entire parser worse than linear), if it is
called with always decreasing &lt;tt class="docutils literal"&gt;col&lt;/tt&gt; values. That's desperately
unlikely though; &lt;strong&gt;pacc&lt;/strong&gt; itself apparently never calls
&lt;tt class="docutils literal"&gt;pacc_coords()&lt;/tt&gt; with a value for &lt;tt class="docutils literal"&gt;col&lt;/tt&gt; that is smaller than the
largest we've already seen.&lt;/p&gt;
&lt;p&gt;Potentially, this could just about be fixed; we could count backwards
from the closest next position if it's closer, or the previous position
is non-existent. But I seriously doubt that's worth it.&lt;/p&gt;
&lt;p&gt;I managed to cook up a contrived test case, that exercises the
&lt;tt class="docutils literal"&gt;memmove()&lt;/tt&gt; case, when a smaller &lt;tt class="docutils literal"&gt;col&lt;/tt&gt; value shows up after a larger
one.&lt;/p&gt;
&lt;p&gt;On further reflection, the already dubious notion of counting lines
backwards is almost entirely scuppered by the need to handle UTF-8
encoding. However, I believe that the value of &lt;tt class="docutils literal"&gt;col&lt;/tt&gt; can only decrease
if a &lt;strong&gt;seq&lt;/strong&gt; has started matching, calling &lt;tt class="docutils literal"&gt;pacc_coords()&lt;/tt&gt; for a
&lt;strong&gt;semantic guard&lt;/strong&gt;, and then failed, and a shorter &lt;strong&gt;seq&lt;/strong&gt; has matched.
(This is how the contrived test case works.) Bear in mind that value
expressions aren't evaluated till a path through the grammar has already
been plotted. So the number of times that &lt;tt class="docutils literal"&gt;col&lt;/tt&gt; can decrease is bound
by the grammar itself. Maybe not, but anyway, I'm definitely going to
stop worry about it now!&lt;/p&gt;
&lt;p&gt;In other news, a gentle hacking session yesterday made a couple of
modest improvements to the emitted code.&lt;/p&gt;
&lt;p&gt;For a start, parsing a &lt;strong&gt;seq&lt;/strong&gt; proceeds from a default assumption of
&lt;tt class="docutils literal"&gt;parsed&lt;/tt&gt;, so there is no need for the &lt;strong&gt;any&lt;/strong&gt; matcher to set this
explicitly; it just needs to set &lt;tt class="docutils literal"&gt;no_parse&lt;/tt&gt; when it fails. (Other
matchers may well have the same redundancy, I haven't checked.) This
fact, incidentally, is why all action must take place inside a &lt;strong&gt;seq&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;I also noticed that on returning from a &lt;strong&gt;call&lt;/strong&gt;, we take a copy of
&lt;tt class="docutils literal"&gt;cur&lt;/tt&gt;, but this is never used. Eliminating it reduces the size of
&lt;tt class="docutils literal"&gt;pacc2.c&lt;/tt&gt; from 9125 to 8982 lines; a modest, but useful improvement.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-07-09</id><link href="https://paccrat.org/entry/2012-07-09"/><updated>2015-05-24T19:45:27-00:00</updated><title>Unicode character classes</title><content type="html">&lt;div class="document" id="unicode-character-classes"&gt;
&lt;h1 class="title"&gt;Unicode character classes&lt;/h1&gt;

&lt;p&gt;The development version of &lt;strong&gt;pacc&lt;/strong&gt; now supports Unicode characters in
character classes. (As always, UTF-8 is currently the only supported
encoding.)&lt;/p&gt;
&lt;p&gt;Not much to say about it, really. The fact that characters in classes
were (ultimately) matched by &lt;tt class="docutils literal"&gt;.&lt;/tt&gt; meant that we were already reading
Unicode characters correctly. Emitting the right code meant adding a
decoder whilst building the AST, and adding support for numeric nodes,
which was actually trivial.&lt;/p&gt;
&lt;p&gt;Previously we had a single node type, &lt;tt class="docutils literal"&gt;crange&lt;/tt&gt; which held either a
character class equality, range start, or range end, distinguished on
their first character. This was actually a rather ugly scheme. Replacing
&lt;tt class="docutils literal"&gt;crange&lt;/tt&gt; with &lt;tt class="docutils literal"&gt;cceq&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;ccge&lt;/tt&gt;, and &lt;tt class="docutils literal"&gt;ccle&lt;/tt&gt; seemed extravagant, but
in fact considerably tidied up the code in &lt;tt class="docutils literal"&gt;emit.c&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;One task remains to be done: we output the exact same code to read the
next Unicode character from the input for every &lt;tt class="docutils literal"&gt;any&lt;/tt&gt; matcher and
every &lt;tt class="docutils literal"&gt;cclass&lt;/tt&gt; matcher. This is obviously silly, and putting this into
a function will tidy things up considerably, and also make one day
supporting encodings other than UTF-8 more feasible.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-07-02</id><link href="https://paccrat.org/entry/2012-07-02"/><updated>2015-05-24T19:45:30-00:00</updated><title>Between releases</title><content type="html">&lt;div class="document" id="between-releases"&gt;
&lt;h1 class="title"&gt;Between releases&lt;/h1&gt;

&lt;p&gt;The first &lt;a class="reference external" href="/article/download"&gt;release&lt;/a&gt; of &lt;strong&gt;pacc&lt;/strong&gt; certainly had some rough edges. Now
begins the work of smoothing some of those off.&lt;/p&gt;
&lt;p&gt;For a start, I've updated the &lt;a class="reference external" href="/article/todo"&gt;todo&lt;/a&gt; list to reflect targets for the next
release. The first one I've tackled is character classes in
&lt;tt class="docutils literal"&gt;pacc0.c.&lt;/tt&gt;. As a result of this work, these three rules:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Name :: char *
    ← n:(NameStart NameCont*) _ { ref_dup(n) }
NameStart :: void
    ← c:. &amp;amp;{ isalpha(ref_0(c)) || ref_0(c) == '_' }
NameCont
    ← c:. &amp;amp;{ isalnum(ref_0(c)) || ref_0(c) == '_' }
&lt;/pre&gt;
&lt;p&gt;have now been replaced by the rather more succinct:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Name :: char *
    ← n:([_A-Za-z] [_A-Za-z0-9]*) _ { ref_dup(n) }
&lt;/pre&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-06-17</id><link href="https://paccrat.org/entry/2012-06-17"/><updated>2015-05-24T19:45:27-00:00</updated><title>Release!</title><content type="html">&lt;div class="document" id="release"&gt;
&lt;h1 class="title"&gt;Release!&lt;/h1&gt;

&lt;p&gt;See the &lt;a class="reference external" href="/article/download"&gt;download page&lt;/a&gt; for the details of the first ever release of
&lt;strong&gt;pacc&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Today's plan is actually to make some sort of release. Of course it will
have some serious holes in it, but at least it will be out the door. I'm
just having one last tweak of the &lt;tt class="docutils literal"&gt;Makefile&lt;/tt&gt;s.&lt;/p&gt;
&lt;p&gt;Which is lucky, because I've discovered that I wasn't running the &lt;em&gt;feed&lt;/em&gt;
tests at all. And the last change I made to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-f&lt;/span&gt;&lt;/tt&gt; was entirely bogus
(what was I thinking?) so I've reversed that. I've also been puzzling
over why &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;arg.c::munge()&lt;/span&gt;&lt;/tt&gt; takes the basename of its input. It doesn't
any more, and I also renamed it to &lt;tt class="docutils literal"&gt;repsuf()&lt;/tt&gt;, for replace suffix
since that's now all that it does.&lt;/p&gt;
&lt;p&gt;As always, the final packaging tweaks take longer than expected, but
it's all done! Err, except that the test harness relies on function
exports, which aren't available in &lt;strong&gt;dash&lt;/strong&gt; for example. Fixed.&lt;/p&gt;
&lt;p&gt;This release is dedicated to HR.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-06-09</id><link href="https://paccrat.org/entry/2012-06-09"/><updated>2015-05-24T19:45:24-00:00</updated><title>Darn</title><content type="html">&lt;div class="document" id="darn"&gt;
&lt;h1 class="title"&gt;Darn&lt;/h1&gt;

&lt;p&gt;Writing documentation again, and spotting holes as I do so. I've a
feeling I've been here before, but the interaction between alternation
and expressions is wrong.&lt;/p&gt;
&lt;p&gt;For example, currently this is a valid &lt;strong&gt;pacc&lt;/strong&gt; grammar:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
S ← &amp;quot;a&amp;quot; (&amp;quot;b&amp;quot; → {5}) &amp;quot;c&amp;quot; → {7}
foo
&lt;/pre&gt;
&lt;p&gt;This is permitted because parentheses contain a &lt;tt class="docutils literal"&gt;Rule0&lt;/tt&gt;, which is
where alternation is defined. However, the conclusion last time was that
this is the right way to describe the syntax, and I've reached the same
conclusion again. The current syntax exactly matches &lt;a class="reference external" href="/article/glossary#ford"&gt;Ford&lt;/a&gt; (I'm not sure
how, or even if, he handles the case of a rule with too many values).
And I definitely like this grammar (which just became
&lt;tt class="docutils literal"&gt;test/pacc/alt2.pacc&lt;/tt&gt;):&lt;/p&gt;
&lt;pre class="literal-block"&gt;
FortySomething ← &amp;quot;forty-&amp;quot; (&amp;quot;four&amp;quot; → { 44 } / &amp;quot;five&amp;quot; → { 45 })
foo
&lt;/pre&gt;
&lt;p&gt;To prevent nonsensical grammars like the first one above, we will
introduce a separate semantic pass over the AST. Similar to the check
for left recursion, this pass will ensure that every path through a rule
produces exactly one value (or no values, for &lt;tt class="docutils literal"&gt;void&lt;/tt&gt; rules).&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-06-06</id><link href="https://paccrat.org/entry/2012-06-06"/><updated>2015-05-24T19:45:28-00:00</updated><title>Right Anchored</title><content type="html">&lt;div class="document" id="right-anchored"&gt;
&lt;h1 class="title"&gt;Right Anchored&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;pacc&lt;/strong&gt; grammars are now anchored on the right. That is, to be
considered a successful parse, the start rule must match the entire
input, and not just a prefix of it.&lt;/p&gt;
&lt;p&gt;I contemplated this change a while ago, and decided against it on
the grounds that other PEG parsers don't anchor on the right. However,
we're not endeavouring to be compatible with any other PEG parser, so in
this case I think it's more important to be Right than Popular. Why do I
think right-anchoring is Right?&lt;/p&gt;
&lt;dl class="docutils"&gt;
&lt;dt&gt;Symmetry&lt;/dt&gt;
&lt;dd&gt;&lt;strong&gt;pacc&lt;/strong&gt; — and every other PEG parser — anchors to the left (the start
rule must start matching the first character of input), so why not to
the right too?&lt;/dd&gt;
&lt;dt&gt;Utility&lt;/dt&gt;
&lt;dd&gt;Every practical &lt;strong&gt;pacc&lt;/strong&gt; grammar, including the “trivial” evaluators
after Bryan Ford, ends up explicitly anchoring to the right, with a
&lt;tt class="docutils literal"&gt;!.&lt;/tt&gt; rule. Since that's &lt;em&gt;at least&lt;/em&gt; 50% of parsers, it should be the
default.&lt;/dd&gt;
&lt;dt&gt;Paltry&lt;/dt&gt;
&lt;dd&gt;Whichever way the default goes, the other option is just two
characters away: &lt;tt class="docutils literal"&gt;!.&lt;/tt&gt; anchors, &lt;tt class="docutils literal"&gt;.*&lt;/tt&gt; unanchors.&lt;/dd&gt;
&lt;dt&gt;Pedagogy&lt;/dt&gt;
&lt;dd&gt;This was the clincher for me. The first example language in the
&lt;a class="reference external" href="/chapter/tutorial/02"&gt;tutorial&lt;/a&gt; comprises just two utterances: &lt;strong&gt;yes&lt;/strong&gt; and &lt;strong&gt;no&lt;/strong&gt;. But then
we have to explain why the parser also accepts &lt;strong&gt;yesterday&lt;/strong&gt;,
&lt;strong&gt;nothing&lt;/strong&gt;, and an infinite variety of other non-utterances, and how
to &lt;em&gt;fix&lt;/em&gt; this problem.&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;So it's done. Unfortunately, this introduces 43 new FAILs: they're
almost all of the “different error message” variety, but they'll have to
be looked at... Actually, although some are to do with right-anchoring,
but mostly the differences are caused by changing an occurrence of
&lt;tt class="docutils literal"&gt;col&lt;/tt&gt; to &lt;tt class="docutils literal"&gt;rule_col&lt;/tt&gt; when an error is flagged for a rule that has
made no progress. For example, the test &lt;tt class="docutils literal"&gt;bad/rep0.pacc&lt;/tt&gt; consists of
the “rule” &lt;tt class="docutils literal"&gt;S ← .**&lt;/tt&gt;. This used to generate the error &lt;tt class="docutils literal"&gt;expected
NameStart:c:0&lt;/tt&gt;, which is fairly meaningless, desugared, and was one
character past where it should have been to boot!&lt;/p&gt;
&lt;p&gt;Now it points to the second &lt;tt class="docutils literal"&gt;*&lt;/tt&gt; with the error &lt;tt class="docutils literal"&gt;expected Defns, or
&lt;span class="pre"&gt;end-of-input&lt;/span&gt;&lt;/tt&gt;, which is definitely an improvement. (This stuff still
isn't right, though: there's a whole bunch of other things — any
matcher, or &lt;tt class="docutils literal"&gt;→&lt;/tt&gt; for example — that could come after &lt;tt class="docutils literal"&gt;S ← .*&lt;/tt&gt;. Still,
we're making progress.)&lt;/p&gt;
&lt;p&gt;All fixed up, back to the expected 4 FAILs.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-06-04</id><link href="https://paccrat.org/entry/2012-06-04"/><updated>2015-05-24T19:45:22-00:00</updated><title>UTF-8 character classes</title><content type="html">&lt;div class="document" id="utf-8-character-classes"&gt;
&lt;h1 class="title"&gt;UTF-8 character classes&lt;/h1&gt;

&lt;p&gt;So I missed the “end of May” release date (I've been too busy enjoying
myself!). I think that I should make a release as soon as possible.
Remaining &amp;quot;blockers&amp;quot; are UTF-8 support in character classes, and
documentation. I'm afraid that documentation will be minimal.&lt;/p&gt;
&lt;p&gt;As for UTF-8 character classes, there should be an almost reasonable
workaround for these, as follows. Where you'd like to write:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Currency ← [$£€]
foo
&lt;/pre&gt;
&lt;p&gt;instead write:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Currency ← &amp;quot;$&amp;quot;:. / &amp;quot;£&amp;quot;:. / &amp;quot;€&amp;quot;:.
bar
&lt;/pre&gt;
&lt;p&gt;Let me just check if that actually works. Yes (although it gives some
more examples of error messages that use desugared syntax). So that is
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;test/pacc/utf8-2.pacc&lt;/span&gt;&lt;/tt&gt;, and the character class version (which
currently adds another two FAILs to the test suite) is
&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;test/pacc/utf8-3.pacc&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;So I think I'm going to make a release with no more code changes. This
won't be the &lt;em&gt;ninja&lt;/em&gt; release after all, it will be &lt;em&gt;rōnin&lt;/em&gt;. Except that
in writing up the command line interface, I realise that I've got in a
muddle with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-f&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-r&lt;/span&gt;&lt;/tt&gt;. Oh, and I really really am going to
anchor grammars on the right.&lt;/p&gt;
&lt;p&gt;Sigh. Writing documentation is such a sobering experience. I was just
describing a &lt;strong&gt;pacc&lt;/strong&gt; grammar when I realised that we currently admit a
grammar with no rules at all. Needless to say, if you actually specify
such a thing, &lt;strong&gt;pacc&lt;/strong&gt; dumps core. Even more hilariously, if &lt;strong&gt;pacc&lt;/strong&gt;'s
input grammar is &lt;em&gt;empty&lt;/em&gt;, we get a failure from &lt;tt class="docutils literal"&gt;mmap()&lt;/tt&gt;! Well, they
shouldn't be too tricky to fix... done the &lt;tt class="docutils literal"&gt;mmap()&lt;/tt&gt; failure. The empty
grammar failure was completely trivial to fix, change this, which
matches zero or more &lt;tt class="docutils literal"&gt;Defn&lt;/tt&gt;s:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Defns
    ← d:Defn ds:Defns { cons(d, ds) }
    / ε { 0 }
foo
&lt;/pre&gt;
&lt;p&gt;for this, which matches one or more:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Defns
    ← d:Defn ds:Defns { cons(d, ds) }
    / d:Defn → d
foo
&lt;/pre&gt;
&lt;p&gt;It would, of course, be even nicer to do this with &lt;tt class="docutils literal"&gt;+&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;*&lt;/tt&gt;, and
maybe this shows the way to do it: when &lt;strong&gt;pacc&lt;/strong&gt; needs to build a list,
it will simply call &lt;tt class="docutils literal"&gt;cons()&lt;/tt&gt;, and it is up to the user to have a
suitable definition in scope. (We could support an option to call a
different name instead.) That might even work...&lt;/p&gt;
&lt;p&gt;Hmm... and another hitch. The support for UTF-8 in &lt;strong&gt;any&lt;/strong&gt; matchers has
changed the error output from the bound literal tests. In one sense this
really doesn't matter, but I don't understand it. (And actually the
previous errors were rather better.) Ah! I think I get it: the new
&lt;strong&gt;any&lt;/strong&gt; matcher doesn't reset &lt;tt class="docutils literal"&gt;col&lt;/tt&gt; if it fails to match. Of course,
it can only fail to match at the end of the input, but still it should
rewind &lt;tt class="docutils literal"&gt;col&lt;/tt&gt; to its starting point. OK, let's fix &lt;em&gt;that&lt;/em&gt;. Done.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-05-29</id><link href="https://paccrat.org/entry/2012-05-29"/><updated>2015-05-24T19:45:29-00:00</updated><title>UTF-8 Support</title><content type="html">&lt;div class="document" id="utf-8-support"&gt;
&lt;h1 class="title"&gt;UTF-8 Support&lt;/h1&gt;

&lt;p&gt;Just about the last thing left on the &lt;a class="reference external" href="/article/todo"&gt;list&lt;/a&gt;. Of course, for the most
part &lt;strong&gt;pacc&lt;/strong&gt; &lt;em&gt;already&lt;/em&gt; supports UTF-8: its own grammar uses characters
like ← and ε with no problems. So long as they occur in literal strings,
they work just fine (and on the assumption that &lt;em&gt;all the world's UTF-8&lt;/em&gt;).&lt;/p&gt;
&lt;p&gt;It's not my intention to do anything about &lt;em&gt;all the world's UTF-8&lt;/em&gt; yet.
I'll need to take advice from that on the userbase, when there is one!
as I am no expert.&lt;/p&gt;
&lt;p&gt;So the infelicity that needs to be fixed for the first release is the
&lt;strong&gt;Any&lt;/strong&gt; matcher (which is supposed to match a single character, but in
fact matches a single byte), and &lt;strong&gt;Character Classes&lt;/strong&gt;. I don't actually
know how to approach the latter yet, but I think I can do &lt;strong&gt;Any&lt;/strong&gt;, so
let's knock that off first, starting with a test case, of course...&lt;/p&gt;
&lt;p&gt;Also, we get the wrong answers for column counts in &lt;tt class="docutils literal"&gt;coords&lt;/tt&gt; due to
encoding issues.&lt;/p&gt;
&lt;p&gt;Right, so &lt;strong&gt;Any&lt;/strong&gt; matching works, thanks to the &lt;a class="reference external" href="http://bjoern.hoehrmann.de/utf-8/decoder/dfa/"&gt;genius decoder&lt;/a&gt; by
Björn Höhrmann.&lt;/p&gt;
&lt;p&gt;And I'm pretty sure I can see how to apply that to &lt;strong&gt;Character
Class&lt;/strong&gt;es too, but I'm too tired to start on that now.&lt;/p&gt;
&lt;p&gt;On another note, whilst writing the tutorial, I fairly early hit the
problem of explaining to the punters why the first &lt;strong&gt;pacc&lt;/strong&gt; parser they
write gives answers for &lt;tt class="docutils literal"&gt;yessir&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;noway&lt;/tt&gt; as well as the expected
inputs. I thought I had some notes from ages ago about the possibility
of anchoring the parse to the end of the string by default, but I can't
find them now. I think I threw it out on grounds of compatability with
other PEG parsers.&lt;/p&gt;
&lt;p&gt;But I'm going to bring it back in again on the grounds of: it's the
right thing to do. Sure, it's easy enough to anchor explicitly, with
&lt;tt class="docutils literal"&gt;End ← !.&lt;/tt&gt;. But if we anchor by default, it's just as easy to unanchor
with &lt;tt class="docutils literal"&gt;End ← .*&lt;/tt&gt;. And every real world grammar wants to be anchored; we
anchor at the left end, don't we? So I'm going to do that, before the
release. Sure, that may not be how any other PEG parser in the world
works, but they're wrong. &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;:-)&lt;/span&gt;&lt;/tt&gt;&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-05-21</id><link href="https://paccrat.org/entry/2012-05-21"/><updated>2015-05-24T19:45:28-00:00</updated><title>Command-line interface</title><content type="html">&lt;div class="document" id="command-line-interface"&gt;
&lt;h1 class="title"&gt;Command-line interface&lt;/h1&gt;

&lt;p&gt;Just a few blockers left to knock down. Completing the command-line
interface is one of them. I think we need the following additions.&lt;/p&gt;
&lt;dl class="docutils"&gt;
&lt;dt&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;--help&lt;/span&gt;&lt;/tt&gt; / &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-h&lt;/span&gt;&lt;/tt&gt;&lt;/dt&gt;
&lt;dd&gt;Basic help text, of course.&lt;/dd&gt;
&lt;dt&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;--version&lt;/span&gt;&lt;/tt&gt; / &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-v&lt;/span&gt;&lt;/tt&gt;&lt;/dt&gt;
&lt;dd&gt;Version information, in GNUtastic style.&lt;/dd&gt;
&lt;dt&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;--defines&lt;/span&gt;&lt;/tt&gt; / &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-d&lt;/span&gt;&lt;/tt&gt;&lt;/dt&gt;
&lt;dd&gt;Write a &lt;tt class="docutils literal"&gt;.h&lt;/tt&gt; file. (Unfortunately &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-d&lt;/span&gt;&lt;/tt&gt; currently means &amp;quot;debug / dump
AST&amp;quot;; that will change to... oh, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-D&lt;/span&gt;&lt;/tt&gt;, I'm unimaginative.)&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;Hmm. I can't think of anything much else at the moment! Let's get those
going then. Done. &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;-d&lt;/span&gt;&lt;/tt&gt; was a tiny bit fiddly, but it seems to work OK.
We still have to distribute &lt;tt class="docutils literal"&gt;pacc.h&lt;/tt&gt; for bootstrap purposes, but it
was in fact built from &lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt;.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-05-19</id><link href="https://paccrat.org/entry/2012-05-19"/><updated>2015-05-24T19:45:23-00:00</updated><title>C interface</title><content type="html">&lt;div class="document" id="c-interface"&gt;
&lt;h1 class="title"&gt;C interface&lt;/h1&gt;

&lt;p&gt;There's a near-OO interface in &lt;tt class="docutils literal"&gt;pacc.tmpl&lt;/tt&gt;, but it's currently
actually hidden from the user with names that start &lt;tt class="docutils literal"&gt;_pacc_&lt;/tt&gt;. The
wrapper function &lt;tt class="docutils literal"&gt;parse()&lt;/tt&gt; is all right, but we need to offer more
control, and perhaps a variety of different wrappers.&lt;/p&gt;
&lt;p&gt;The thing about &lt;tt class="docutils literal"&gt;_pacc_parser_new()&lt;/tt&gt; is that it has the grammar rules
embedded in it. So it would have to be &lt;tt class="docutils literal"&gt;YY_new()&lt;/tt&gt; (where &lt;tt class="docutils literal"&gt;YY&lt;/tt&gt; is the
user-supplied string). That could work. Or, could we truly have a
&lt;tt class="docutils literal"&gt;pacc_parser_new()&lt;/tt&gt; function that returns a generic parser object, a
later method call setting the grammar to use with it? Thing is, &amp;quot;a
grammar&amp;quot; carries quite a lot of baggage: a pointer to the &lt;tt class="docutils literal"&gt;engine()&lt;/tt&gt;
function, the return type, the number of rules...&lt;/p&gt;
&lt;p&gt;So let's spin out the first idea.&lt;/p&gt;
&lt;dl class="docutils"&gt;
&lt;dt&gt;&lt;tt class="docutils literal"&gt;p = YY_new(void)&lt;/tt&gt;&lt;/dt&gt;
&lt;dd&gt;Returns a new pacc parser object for the &lt;tt class="docutils literal"&gt;YY&lt;/tt&gt; grammar. We don't need
the &lt;tt class="docutils literal"&gt;name&lt;/tt&gt; parameter that the current &lt;tt class="docutils literal"&gt;_pacc_parser_new()&lt;/tt&gt; has:
that's &lt;tt class="docutils literal"&gt;YY&lt;/tt&gt;. No it's not, it's the filename that the input came
from, and &lt;tt class="docutils literal"&gt;YY_error()&lt;/tt&gt; would very much like to have it still. This
function won't actually be able to do all that much, since it doesn't
know how long the input is.&lt;/dd&gt;
&lt;dt&gt;&lt;tt class="docutils literal"&gt;void YY_input(p, char *name, char *addr, off_t length)&lt;/tt&gt;&lt;/dt&gt;
&lt;dd&gt;Sets the text to be parsed.&lt;/dd&gt;
&lt;dt&gt;&lt;tt class="docutils literal"&gt;int YY_parse(p)&lt;/tt&gt;&lt;/dt&gt;
&lt;dd&gt;Actually performs the parse, returning 1 if it was sucessful.&lt;/dd&gt;
&lt;dt&gt;&lt;tt class="docutils literal"&gt;YY_type YY_result(p)&lt;/tt&gt;&lt;/dt&gt;
&lt;dd&gt;If the parse was successful, returns the result. Ooh. Do we want to
defer the actual evaluation till this point? Hmm... think about that.
&lt;a class="footnote-reference" href="#id2" id="id1"&gt;[1]&lt;/a&gt;&lt;/dd&gt;
&lt;dt&gt;&lt;tt class="docutils literal"&gt;YY_error(p)&lt;/tt&gt;&lt;/dt&gt;
&lt;dd&gt;If the parse was unsuccessful, report an error.&lt;/dd&gt;
&lt;dt&gt;&lt;tt class="docutils literal"&gt;YY_destroy(p)&lt;/tt&gt;&lt;/dt&gt;
&lt;dd&gt;Destructor.&lt;/dd&gt;
&lt;/dl&gt;
&lt;p&gt;That looks like it could work out pretty well. Let's give it a go...
Yup, seems to be sorted. Now to build a feeder that uses it. Done.
Fiddly, as anticipated, but it works! As far as I know this is a world
first (apart from my previous proof-of-concept). If anybody's got a
rooftop, I'd like to shout from it.&lt;/p&gt;
&lt;table class="docutils footnote" frame="void" id="id2" rules="none"&gt;
&lt;colgroup&gt;&lt;col class="label" /&gt;&lt;col /&gt;&lt;/colgroup&gt;
&lt;tbody valign="top"&gt;
&lt;tr&gt;&lt;td class="label"&gt;&lt;a class="fn-backref" href="#id1"&gt;[1]&lt;/a&gt;&lt;/td&gt;&lt;td&gt;Unfortunately, it's not so easy to move the evaluation code out into a
separate function, as it uses the same explicit control
infrastructure (&lt;tt class="docutils literal"&gt;goto contin;&lt;/tt&gt; and such like) as the actual
parser. Since I've claimed I'm getting the first release out in the
next 10 days, I really don't have time for this at the moment! I've
added it to the &lt;a class="reference external" href="/article/todo"&gt;To Do&lt;/a&gt; list, though, as I think it's a great idea.&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-05-15</id><link href="https://paccrat.org/entry/2012-05-15"/><updated>2015-05-24T19:45:24-00:00</updated><title>Feeding Again</title><content type="html">&lt;div class="document" id="feeding-again"&gt;
&lt;h1 class="title"&gt;Feeding Again&lt;/h1&gt;

&lt;p&gt;So I think the &lt;tt class="docutils literal"&gt;cook()&lt;/tt&gt; function, that produces the secondary grammar,
is pretty much there. Now comes the tricky part. Somehow, I have to
arrange for these two grammars to co-exist in a single file (shouldn't
be too hard; that was part of the point of the &lt;tt class="docutils literal"&gt;_pacc&lt;/tt&gt; object), with
different boilerplate that calls each of them...&lt;/p&gt;
&lt;p&gt;Or, how about simply writing a second &lt;tt class="docutils literal"&gt;.c&lt;/tt&gt; file, and punting to the
user to tie it all together? After all, we don't currently provide any
support for getting the input in the first place, so it seems a bit odd
to support extending it. And that way I could probably have it finished
tonight, which is very appealing!&lt;/p&gt;
&lt;p&gt;As expected, that produces a few &amp;quot;multiple definition&amp;quot; errors, although
most of them are missing &lt;tt class="docutils literal"&gt;static&lt;/tt&gt;s I think. Yup, which brings it down
to just &lt;tt class="docutils literal"&gt;parse()&lt;/tt&gt; itself. And with a bit of judicious sed, we're at
the stage where whatever ends up in the &lt;tt class="docutils literal"&gt;text&lt;/tt&gt; of the grammar node
gets prepended to that name. But at the moment that's hardwired to
&lt;tt class="docutils literal"&gt;&amp;quot;yy&amp;quot;&lt;/tt&gt;, and it's too late at night to fix that.&lt;/p&gt;
&lt;p&gt;Of course, another thing that's necessary for this to work is for the
first (full) grammar not to report any errors.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-05-14</id><link href="https://paccrat.org/entry/2012-05-14"/><updated>2015-05-24T19:45:25-00:00</updated><title>Feeding</title><content type="html">&lt;div class="document" id="feeding"&gt;
&lt;h1 class="title"&gt;Feeding&lt;/h1&gt;

&lt;p&gt;So, it's time I tackled &lt;em&gt;feeding&lt;/em&gt;. This is, I believe, a unique feature
of &lt;strong&gt;pacc&lt;/strong&gt;, and makes it possible to use packrat parsing on the command
line. I'm a bit hesitant to dive into this one: I had a proof-of-concept
for &lt;strong&gt;peg / leg&lt;/strong&gt; a couple of years ago, but I actually don't have that
to hand at the moment. The concept isn't a tricky one. But the details
will inevitably be tedious.&lt;/p&gt;
&lt;p&gt;Let's start with the command line. &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;--feed&lt;/span&gt; foo&lt;/tt&gt; turns on feeding, and
&lt;tt class="docutils literal"&gt;foo()&lt;/tt&gt; is the feeder function (I don't think we'll allow a default
feeder). &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;--feed-rule&lt;/span&gt; x&lt;/tt&gt; gives the name of the rule that appears at
feed points; it defaults to &lt;tt class="docutils literal"&gt;__&lt;/tt&gt; (two underscores). That's easy
enough.&lt;/p&gt;
&lt;p&gt;I think I want to make another proof-of-concept in &lt;strong&gt;pacc&lt;/strong&gt; this time.
Let's take &lt;tt class="docutils literal"&gt;test/pacc/baf4.pacc&lt;/tt&gt; as a starting point, and add feed
markers. Hmm. Consider this pair of rules:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
__ ← (&amp;quot;\n&amp;quot; / _)*
_ ← [ \t]*
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;pacc&lt;/strong&gt; says &lt;tt class="docutils literal"&gt;left recursion in rule &lt;span class="pre"&gt;`__:*:0'&lt;/span&gt;&lt;/tt&gt;. And it's right, up to
a point (future work: report the error more meaningfully in terms of the
user's sugar). The derived rules read:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
__:*:0  ←  __:1:0  __:*:0  /  ε
__:1:0  ←  &amp;quot;\n&amp;quot; / _
&lt;/pre&gt;
&lt;p&gt;So the desugared rule &lt;tt class="docutils literal"&gt;__:1:0&lt;/tt&gt;, which matches a single instance of the
subject of the &lt;tt class="docutils literal"&gt;*&lt;/tt&gt; operator, may make no progress, because &lt;tt class="docutils literal"&gt;_&lt;/tt&gt; may
make no progress. And so we may indeed left-recurse into &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;__:*:0&lt;/span&gt;&lt;/tt&gt;.
Yeah, OK, &lt;strong&gt;pacc&lt;/strong&gt; is right, I'm wrong. I love it when that happens!
What we want here is:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
__ ← (&amp;quot;\n&amp;quot; _)*
&lt;/pre&gt;
&lt;p&gt;Hmm again. This grammar, or even the unmodified one, is apparently happy
to parse &lt;tt class="docutils literal"&gt;2 +&lt;/tt&gt; (with a value of 2). How can that be: it includes an
end-of-input marker? There's a much simpler eof test in
&lt;tt class="docutils literal"&gt;test/pacc/not1.pacc&lt;/tt&gt;, and that works as expected. It's unfortunately
not all that easy to see &lt;em&gt;how&lt;/em&gt; &lt;strong&gt;pacc&lt;/strong&gt; parsed an input unless it built
a syntax tree. Better try and get a smaller example that fails in the
same way. Aha! Once again, &lt;strong&gt;pacc&lt;/strong&gt; is right, and I'm wrong. We had this
rule:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
Decimal ← Digit* Space { atoi(ref_ptr(ref())) }
&lt;/pre&gt;
&lt;p&gt;which allows an &amp;quot;empty&amp;quot; &lt;tt class="docutils literal"&gt;Decimal&lt;/tt&gt;. Obviously it should be &lt;tt class="docutils literal"&gt;Digit+&lt;/tt&gt;,
and with that change it behaves as expected.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-05-13</id><link href="https://paccrat.org/entry/2012-05-13"/><updated>2015-05-24T19:45:25-00:00</updated><title>State of the union</title><content type="html">&lt;div class="document" id="state-of-the-union"&gt;
&lt;h1 class="title"&gt;State of the union&lt;/h1&gt;

&lt;p&gt;So the next &amp;quot;blocker&amp;quot; I want to tackle on &lt;a class="reference external" href="/entry/2012-04-18"&gt;the list&lt;/a&gt; is to tidy up the
union so it contains only one instance of each type--it would be nice to
support &lt;tt class="docutils literal"&gt;void&lt;/tt&gt; at this point, but we'll see.&lt;/p&gt;
&lt;p&gt;I suppose you could argue that this isn't &lt;em&gt;really&lt;/em&gt; a blocker, since the
current code works, and doesn't seem to run into any compiler
limitations. It's just really ugly.&lt;/p&gt;
&lt;p&gt;This is one of those cases where &amp;quot;&lt;em&gt;n&lt;/em&gt; is usually small&amp;quot; - for example,
&lt;tt class="docutils literal"&gt;pacc.pacc&lt;/tt&gt; has just 5 different types (and will eventually add
&lt;tt class="docutils literal"&gt;void&lt;/tt&gt;, but that doesn't come into the union anyway). It's hard to
imagine a grammar that would use many more. So we don't need any clever
data structures--a simple vector with linear search will be perfectly
adequate.&lt;/p&gt;
&lt;p&gt;Done. What's next? I was thinking this wouldn't be a bad time to do
&lt;tt class="docutils literal"&gt;void&lt;/tt&gt;, since we're playing with types, but that's a &amp;quot;later&amp;quot; rather
than a &amp;quot;blocker&amp;quot;. I suppose detecting left recursion would be a good
one. I've read a lot about left-recursion, and have decided to pin my
colours to the masts of Dale Schumacher, who wrote &lt;a class="reference external" href="http://www.dalnefre.com/wp/2011/05/parsing-expression-grammars-part-4/"&gt;Left Recursion
Considered Harmful&lt;/a&gt; (he didn't &lt;em&gt;quite&lt;/em&gt; call it that) and Ian Piumarta,
who's &lt;a class="reference external" href="http://piumarta.com/software/peg/"&gt;peg / leg&lt;/a&gt; recursive-descent PEG parsers for C inspired me to
start &lt;tt class="docutils literal"&gt;pacc&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Obviously, there's a huge advantage in not trying to handle
left-recursion (other than to detect it), which is that I don't have to
implement anything!  Anyway, I'm supposed to be using test-based
development, so let's start with a left-recursive test-case or two.
Easy, and either direct or indirect left-recursion loops. (In fact, they
run out of memory &lt;em&gt;very&lt;/em&gt; quickly, which is a bit disturbing. Hmm...
let's quickly look at that with a debugger. OK, well one thing that's
going wrong is in &lt;tt class="docutils literal"&gt;int l = 2 * &lt;span class="pre"&gt;(p-&amp;gt;stack_alloc&lt;/span&gt; - &lt;span class="pre"&gt;p-&amp;gt;stack)&lt;/span&gt; + s;&lt;/tt&gt; we
only have 31 bits to play with. Obviously that should be a &lt;tt class="docutils literal"&gt;size_t&lt;/tt&gt;.
That's better! We now run for about a minute and grow to over 10G before
the inevitable grisly death.)&lt;/p&gt;
&lt;p&gt;OK so that's all done. Huge thanks again to Ian for idea liberation.&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-05-09</id><link href="https://paccrat.org/entry/2012-05-09"/><updated>2015-05-24T19:45:26-00:00</updated><title>Implementing #line directives</title><content type="html">&lt;div class="document" id="implementing-line-directives"&gt;
&lt;h1 class="title"&gt;Implementing #line directives&lt;/h1&gt;

&lt;div class="section" id="id1"&gt;
&lt;h2&gt;2012-04-28&lt;/h2&gt;
&lt;p&gt;So what about implementing #line directives? First thing is that we need
to get line numbers into the AST. And I'm afraid to say that I really
suspect the easiest (and possibly best) way to do this is to make an
&lt;tt class="docutils literal"&gt;expr&lt;/tt&gt; node hold a value like &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;6:&amp;quot;yes&amp;quot;&lt;/span&gt;&lt;/tt&gt; which will eventually turn
into:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
something_or_other = (
#line 6 &amp;quot;parse.pacc&amp;quot;
&amp;quot;yes&amp;quot;
#line 943 &amp;quot;parse.c&amp;quot;
);
foo
&lt;/pre&gt;
&lt;p&gt;These are both fiddly - it's irritating that the C pre-processor doesn't
have a &lt;tt class="docutils literal"&gt;#unline&lt;/tt&gt; directive. The second &lt;tt class="docutils literal"&gt;#line&lt;/tt&gt; will need to be on
line 942 of file &lt;tt class="docutils literal"&gt;parse.c&lt;/tt&gt; to tell it that the next line is 943 in
&lt;tt class="docutils literal"&gt;parse.c&lt;/tt&gt;!&lt;/p&gt;
&lt;p&gt;To implement this, I'm going to expunge all the random &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;printf()``s&lt;/span&gt; in
``emit.c&lt;/tt&gt;. This will make removing redundant types from the union much
simpler, too.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="id2"&gt;
&lt;h2&gt;2012-04-30&lt;/h2&gt;
&lt;p&gt;So this is a major rewrite of emit.c, and I've got to &lt;tt class="docutils literal"&gt;error()&lt;/tt&gt;, which
is the major cause of bloat in the emitted code. For example, pacc2.c is
currently 11937 lines. I suppose now would be as good a time as any to
fix that up. Well, it drops to 9331 lines, which is a &amp;gt; 20% reduction,
so that's actually not bad.&lt;/p&gt;
&lt;p&gt;Reworking everything to go through &lt;tt class="docutils literal"&gt;c_str()&lt;/tt&gt; and friends is complete.
I can now add the &lt;tt class="docutils literal"&gt;#unline&lt;/tt&gt; directives that go back to where we were,
which was the whole point of that rewrite.&lt;/p&gt;
&lt;p&gt;Argh, so that's full of hideousnesses. First, #line is supposed to be
left-justified, but that doesn't match the way indents work at the
moment. Second, we need to know how long the template is. Thirdly, it
means that the &amp;quot;diff&amp;quot; to check pacc2.c versus pacc3.c fails miserably.
All solved.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="id3"&gt;
&lt;h2&gt;2012-05-02&lt;/h2&gt;
&lt;p&gt;So, onto how to emit sensible #lines for the user-supplied text. At the
moment, we have nodes in the parse tree like &lt;tt class="docutils literal"&gt;expr 102: 5&lt;/tt&gt;. Either we
change those to include the line number where that expr came from, so we
might have &lt;tt class="docutils literal"&gt;expr 102: 7 5&lt;/tt&gt;, or we add another node type.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="id4"&gt;
&lt;h2&gt;2012-05-05&lt;/h2&gt;
&lt;p&gt;When we report an error, we calculate the line number (and column
number) in the input by counting &lt;tt class="docutils literal"&gt;\\n&lt;/tt&gt;s. Which is all right as far as
it goes, but we don't want to keep doing that every time we encounter an
expression.&lt;/p&gt;
&lt;p&gt;OK, I don't know why I've been holding off on this (except perhaps that
there's this vast body of code that I barely understand). We need
&lt;tt class="docutils literal"&gt;_pacc_line_number(_pacc_parser *, off_t)&lt;/tt&gt;. Initially use the code
we've got, counting &lt;tt class="docutils literal"&gt;\\n&lt;/tt&gt;s. Later we can memoize it.&lt;/p&gt;
&lt;p&gt;(I'm worrying about off_t again; wondering if we wouldn't be better off
with intmax_t or intptr_t or int_fast64_t. The problem is, I need to
write some code that will convert a line number into a string. Oh, but
hang on, they're &lt;tt class="docutils literal"&gt;long&lt;/tt&gt;s anyway. OK, no problem.)&lt;/p&gt;
&lt;p&gt;Right, well, that all seems to work &lt;em&gt;except&lt;/em&gt; there's discrepancies
between pacc2.c and pacc3.c on node numbers. Right, because pacc1
doesn't build line numbers into the tree. So let's fix that. Done.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="id5"&gt;
&lt;h2&gt;2012-05-06&lt;/h2&gt;
&lt;p&gt;Guards! Guards! They don't have #line numbers yet.&lt;/p&gt;
&lt;p&gt;Well that's something I wouldn't have anticipated. First #line
implementation produces code that looks like this:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
cur-&amp;gt;value.u106= (
#line 7 &amp;quot;bad/line.pacc&amp;quot;
         ++
#line 332 &amp;quot;parse.c&amp;quot;
        );
foo
&lt;/pre&gt;
&lt;p&gt;But in this case, gcc doesn't spot the syntax error till it sees the
closing brace, so we need to put that on the same line as the
expression. Hmm... and now it says&lt;/p&gt;
&lt;p&gt;bad/line.pacc:7:15: error: expected expression before ‘)’ token&lt;/p&gt;
&lt;p&gt;which is a touch confusing, but I don't think there's much I can do
about it. The column numbers are wrong, too, which will be fiddly to
fix, sigh. Oh, here's a thought. Instead of tediously using snprintf
(twice) to turn a line number into a &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;n-&amp;gt;text&lt;/span&gt;&lt;/tt&gt; node, if the AST
actually supported numeric nodes, we could carry the line and column
numbers through as numbers. Then the line numbers are trivial to turn
into a string (because we're writing to a stream, rather than memory),
and the column number can be used as a number to indent appropriately.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="id6"&gt;
&lt;h2&gt;2012-05-09&lt;/h2&gt;
&lt;p&gt;Couple of things we can do here. We could have a node type &lt;tt class="docutils literal"&gt;coord&lt;/tt&gt;
that holds &lt;tt class="docutils literal"&gt;int[2]&lt;/tt&gt;. We could have node types of &lt;tt class="docutils literal"&gt;line&lt;/tt&gt; and
&lt;tt class="docutils literal"&gt;column&lt;/tt&gt; that both hold an int. We could have a single node type of
type &lt;tt class="docutils literal"&gt;num&lt;/tt&gt; that holds an int, and distinguish them by their position.
On the whole, the first seems easiest.  Especially since I've decided
that it's OK to use an anonymous union (supported in C11 apparently as
well as ganuck and others).&lt;/p&gt;
&lt;p&gt;Almost there, but still some minor hitch with positioning, and one of
those darned pacc2 vs pacc3 errors... which was easily fixed.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2012-04-18</id><link href="https://paccrat.org/entry/2012-04-18"/><updated>2015-05-24T19:45:28-00:00</updated><title>To Do List</title><content type="html">&lt;div class="document" id="to-do-list"&gt;
&lt;h1 class="title"&gt;To Do List&lt;/h1&gt;

&lt;p&gt;In another project, I divided the todo list into &amp;quot;blockers&amp;quot; and
&amp;quot;laters&amp;quot;. Let's try that here.&lt;/p&gt;
&lt;p&gt;Blockers:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;add #line directives&lt;/li&gt;
&lt;li&gt;implement --feed / partialization (this is my unique selling point)&lt;/li&gt;
&lt;li&gt;complete command line interface&lt;/li&gt;
&lt;li&gt;detect left recursion&lt;/li&gt;
&lt;li&gt;one instance of each type per union&lt;/li&gt;
&lt;li&gt;dynamically allocate everything&lt;/li&gt;
&lt;li&gt;UTF-8 support&lt;/li&gt;
&lt;li&gt;basic documentation&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Laters:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;void types&lt;/li&gt;
&lt;li&gt;autoconfiscation, because I really want to make at least one public
release with my beautiful pristine makefile&lt;/li&gt;
&lt;li&gt;parser optimization&lt;/li&gt;
&lt;li&gt;value inheritance&lt;/li&gt;
&lt;li&gt;XXX cleanups&lt;/li&gt;
&lt;li&gt;review&lt;/li&gt;
&lt;li&gt;more research&lt;/li&gt;
&lt;li&gt;permissions (since I won't actually distribute the Java parser yet)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now done:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p class="first"&gt;Scoping&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Names in pacc grammars are supposed to scope statically over
sequences, but that clearly isn't the case at the moment, otherwise
test/pacc/scope1.pacc would compile!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;First up would seem to be #line directives. As far as I can see, we
include user-supplied in emit.c::emit_expr(), ::guard_post(), and that's
all. However, where do we get the information from to go in the #line
directive? As far as I can see, we're not storing that anywhere at
present, so we'll need to fix up the Result rule to store that somehow
(a new node type?)&lt;/p&gt;
&lt;/div&gt;
</content></entry><entry><id>https://paccrat.org/entry/2010-11-30</id><link href="https://paccrat.org/entry/2010-11-30"/><updated>2015-05-24T19:47:21-00:00</updated><title>Character Classes</title><content type="html">&lt;div class="document" id="character-classes"&gt;
&lt;h1 class="title"&gt;Character Classes&lt;/h1&gt;

&lt;div class="section" id="id1"&gt;
&lt;h2&gt;2010-11-22&lt;/h2&gt;
&lt;p&gt;At the moment I'm thinking about character classes. It seems to me that
we must add at least two node types to the grammar: cclass introduces a
class; its children of type crange are a list that give the various
ranges within the class. The value of the cclass node itself is null for
a normal character class; anything (&lt;tt class="docutils literal"&gt;^&lt;/tt&gt;, say) for an inverted
character class.&lt;/p&gt;
&lt;p&gt;The crange node's value consists of a type byte, which is &lt;tt class="docutils literal"&gt;&amp;gt;&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;=&lt;/tt&gt;, or
&lt;tt class="docutils literal"&gt;&amp;lt;&lt;/tt&gt;, followed by the character in question. So for example the character
class &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[A-Z_]&lt;/span&gt;&lt;/tt&gt; would appear in the tree as:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
cclass: (null)
  crange: &amp;quot;&amp;gt;A&amp;quot;
  crange: &amp;quot;&amp;lt;Z&amp;quot;
  crange: &amp;quot;=_&amp;quot;
&lt;/pre&gt;
&lt;p&gt;Code emission is fairly obvious I think.&lt;/p&gt;
&lt;p&gt;Also, I'm now more or less determined that we will support different
character encodings. The important ones I know of are byte (no character
encoding, each byte in the input represents itself), UTF-8, and UTF-16.
Mainly I just need 2 functions. One function returns the length in bytes of
the next character on the input (obviously this is always 1 for the byte
noncoding; more complex for UTF-8 / UTF-16), which is used by the any
matcher. The other function returns the next character on the input
decoded into an int (or UTF-32), for use in the character class matcher.&lt;/p&gt;
&lt;p&gt;Supporting various combinations like &amp;quot;grammar is written in UTF-8 but
input is in UTF-16&amp;quot; will be a little more complicated, but I think I can
do it using function pointers in the struct _pacc_parser object. But
more of that later.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="id2"&gt;
&lt;h2&gt;2010-11-24&lt;/h2&gt;
&lt;p&gt;So basic character classes work, all the way from pacc.pacc to the
compiler! Still missing are inverted character classes, and proper
handling of character escapes.&lt;/p&gt;
&lt;p&gt;(Inverted character classes are technically unnecessary, since &lt;tt class="docutils literal"&gt;[^x]&lt;/tt&gt; is
equivalent to &lt;tt class="docutils literal"&gt;![x].&lt;/tt&gt;. Hmm... we could actually implement them that way
in pacc.pacc. But on further reflection I think it's easier -- actually
less code -- to carry the inversion down to emit, as I'd originally
envisaged.)&lt;/p&gt;
&lt;p&gt;But first, some tidy up. I made a bit of a hash of syntax.c in the last
throes of making character classes work, and thinking about it I suspect
snoc is actually append (essentially, we don't distinguish atoms from
lists). Let's see... that seems fine. And negated character classes
work. Cool.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="id3"&gt;
&lt;h2&gt;2010-11-30&lt;/h2&gt;
&lt;p&gt;What next? Fix character classes to support C escapes, and also the
special escape &lt;tt class="docutils literal"&gt;\]&lt;/tt&gt;. (Note that we don't need to escape &lt;tt class="docutils literal"&gt;-&lt;/tt&gt;, since
it should work at the beginning, or end of a class, and also after any
range, for example &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[A-Za-z-_]&lt;/span&gt;&lt;/tt&gt;. Need tests for all these cases, of
course.) Rewrite pacc.pacc to use character classes. Copy new rules into
pacc0.c.&lt;/p&gt;
&lt;p&gt;Hmm... making &lt;tt class="docutils literal"&gt;-&lt;/tt&gt; work as expected was more effort than I'd expected. I
was bitten by the usual problem that PEGs never backtrack, even when you
expect that they might, and also that some of the low-level rules that
match escaped characters were of type void -- they were normally glued
together by a higher-level rule that used refs to get at the actual
string value. But for character classes we need to know the characters.
(Along the way, I think I spotted some problems with &lt;tt class="docutils literal"&gt;#&lt;/tt&gt; comments
occurring inside rules that I should pursue.)&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</content></entry></feed>