<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Joel&apos;s Blog</title>
    <description>A blog about whatever strikes my fancy.
</description>
    <link>http://www.joelwilliamson.ca/</link>
    <atom:link href="http://www.joelwilliamson.ca/feed.xml" rel="self" type="application/rss+xml" />
    <pubDate>Mon, 30 Aug 2021 23:50:00 -0400</pubDate>
    <lastBuildDate>Mon, 30 Aug 2021 23:50:00 -0400</lastBuildDate>
    <generator>Jekyll v3.8.7</generator>
    
      <item>
        <title>On Liberty - Introduction</title>
        <description>&lt;p&gt;I started reading J.S. Mill&amp;#39;s &lt;em&gt;&lt;a href=&quot;https://gutenberg.org/ebooks/34901.epub.noimages&quot;&gt;On Liberty&lt;/a&gt;&lt;/em&gt; today.
I thought I would write up my thoughts after each chapter.&lt;/p&gt;

&lt;p&gt;In the introduction, Mill states the goal of the essay is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;the nature and limits of the power which can be legitimately excercised by society over the individual.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;He then goes on to provide a brief historical overview. It used to be, (he says) that the struggle between Authority
and Liberty was the struggle between Ruler and Ruled.
When there is a clear division between the ruler and his subjects, the subjects gain Liberty by restricting the power of the ruler.
This is a clear pattern that he sees throughout European history.
Sometimes the ruler has more constraints and sometimes he has less, but always those who seek liberty seek to restrain his power.&lt;/p&gt;

&lt;p&gt;With the rise of constitutional forms of government, things become murkier.
Now those who seek liberty will sometimes try to transfer power from the ruler to some other body that represents society as a whole.
Once enough power has been transferred to bodies that claim to act in the interests of the people,
there is an argument that restrictions on their power reduce the liberty of the people:
Liberty consists of the ability of the people to do what they want.
One main avenue by which the people (as a whole) exercise their power is through their representatives.
Therefore restricting the power of the representatives reduces their ability to achieve the goals of the people,
and thus reduces liberty.&lt;/p&gt;

&lt;p&gt;Mill raises three objections to this argument.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The interests of the representatives might not coincide with that of the people.&lt;/li&gt;
&lt;li&gt;If each person is subject to the will of all the others, each person will have much less liberty.&lt;/li&gt;
&lt;li&gt;The &amp;quot;will of the people&amp;quot; often means the will of a particular section of the people (maybe the most numerous or the most vocal),
and they may oppress the rest of the people.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;He also notes that when society is acting against an individual, it can act through political avenues, but it does not need to.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Protection agains the tyranny of the magistrate is not enough, there needs protection also against the tyranny of the prevailing
opinion and feeling; against the tendency of society to impose, by other means than civil penalties, its own ideas and practices
as rules of conduct on those who dissent from them; to fetter the development and prevent the formation of any individuality not
in harmony with its ways, and compel all characters to fashion themselves upon the model of its own.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This quote brings two things to my mind. First, in today&amp;#39;s society it seems that the tyranny of the magistrate is far more constrained
than the tyranny of prevailing opinion and feeling.
Secondly, (as was pointed out recently in &lt;a href=&quot;https://palladiummag.com/2021/02/16/yan-fus-lessons-on-the-true-mission-of-liberalism/&quot;&gt;Blake Smith&amp;#39;s article on Yan Fu&amp;#39;s translation of Mill in Palladium&lt;/a&gt;) Mill is concerned with the creation of individuality, not merely allowing people to do as they please.&lt;/p&gt;

&lt;p&gt;How then should we determine the ways society may impose upon the individual?
Mill rejects custom as merely a way of expressing the interests of the currently dominant class.
Instead, society may impose on an individual to prevent harm to another, but may not prevent self-harm.&lt;/p&gt;

&lt;p&gt;Finally, he identifies three key liberties that must be protected:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Liberty of Conscience. The thoughts and feelings of one individual cannot harm another, and must be free. He considers the liberty of expressing and publishing opinions to be a key element of this.&lt;/li&gt;
&lt;li&gt;Liberty of Tastes and Pursuits. Each should be allowed to plot his own course through life, provided it doesn&amp;#39;t harm others.&lt;/li&gt;
&lt;li&gt;Liberty of Association. It follows that two or more people should be able to come together for any reason that doesn&amp;#39;t harm other.&lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Sat, 27 Feb 2021 00:00:00 -0500</pubDate>
        <link>http://www.joelwilliamson.ca/books/2021/02/27/on-liberty-introduction.html</link>
        <guid isPermaLink="true">http://www.joelwilliamson.ca/books/2021/02/27/on-liberty-introduction.html</guid>
        
        
        <category>books</category>
        
      </item>
    
      <item>
        <title>New Job</title>
        <description>&lt;p&gt;I started a new job in September just after the last post.
I was hired by Octopuz, a small company that makes offline robot programming software.
OLRP is a technique used to streamline the use of the industrial robots used in many factories.&lt;/p&gt;

&lt;p&gt;Users start by creating a model of their existing &amp;quot;robot cell.&amp;quot;
This is one or more six-axis robots, rails that the robots can be repositioned on, rotary tables the work pieces can be mounted on,
tools (such as welding torches or cutters) attached to the robot, and obstacles like tables or walls.
Our software ships with CAD models of most of the mechanical components our users have, and more can be added easily.&lt;/p&gt;

&lt;p&gt;Once the cell has been defined, a CAD model of the part they want to work on is imported.
They then create one or more &amp;quot;paths&amp;quot; the robot will follow around the part.
The path contains the points to travel between, where to turn the tool on or off, how fast to travel, the angle the tool should be at, &amp;amp;c.
A single path is then often duplicated several times to take advantage of translational, rotational or flip symmetries of the part.
For instance, there might be a bar held onto a plate by 8 identical brackets on each side.
The user would manually lay out the path to weld one bracket, then translate the path to each bracket on that side,
and then flip them all to the other side.&lt;/p&gt;

&lt;p&gt;We then analyze the paths to find how to best move each robot axis and any external positioners.
Users can see a simulation of how the robot will follow the path they defined, and any problems we identified with it.&lt;/p&gt;

&lt;p&gt;Finally, we generate an output program that can be provided to the robot to make it follow the paths the user created.&lt;/p&gt;

&lt;p&gt;The alternative approach is to use a controller attached to the robot to steer it through the paths to follow,
recording the points as you go.
OLRP allows a new program to be created for the robot while it is working on a different part, 
improving utilization. It can also be much faster for complicated paths, as the software can provide shortcuts to
quickly visit many points.&lt;/p&gt;
</description>
        <pubDate>Sat, 27 Feb 2021 00:00:00 -0500</pubDate>
        <link>http://www.joelwilliamson.ca/work/2021/02/27/new-job.html</link>
        <guid isPermaLink="true">http://www.joelwilliamson.ca/work/2021/02/27/new-job.html</guid>
        
        
        <category>work</category>
        
      </item>
    
      <item>
        <title>Three Stage Iron Casting</title>
        <description>&lt;p&gt;(This is the second entry in the series. &lt;a href=&quot;/factorio/pyanodon/2020/09/08/pyanodon-aar-intro.html&quot;&gt;Part 1&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;I mentioned briefly in the intro that more advanced smelting and casting provides higher yields.
In this post, I&amp;#39;ll walk through a basic iron setup.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/three-stage-iron.jpg&quot; alt=&quot;Three Stage Iron&quot;&gt;&lt;/p&gt;

&lt;p&gt;The main flow of this setup is iron ore enters at the bottom, then is progressively refined before iron plates come out at the top.&lt;/p&gt;

&lt;p&gt;The first processing stage is crushing.
This uses Jaw crushers to separate the raw Iron ore into Processed iron ore and Stone.
Most of the stone is consumed by the foundries on the second row to create bricks.
The bricks are used to create Hot Air to improve the yield farther up.&lt;/p&gt;

&lt;p&gt;The processed ore then moves up to the Basic oxygen furnaces.
In this chain, the Basic oxygen furnaces seem to play the role of a &lt;a href=&quot;https://en.wikipedia.org/wiki/Blast_furnace&quot;&gt;blast furnace&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;(A real-life blast furnace works as follows: iron ore (which is primarily iron oxide) is mixed with coal and a flux (typically limestone).
The furnace is loaded with this mixture.
Hot oxygen-rich air is blown into the bottom of the furnace, and the coal burns in the oxygen to create heat and carbon monoxide.
The carbon monoxide is a strong reducing agent, and will react with the iron oxides to create carbon dioxide and elemental iron.
The intense heat from the combustion will also melt the iron, allowing it to flow out the bottom of the furnace.
The limestone (CaCO&lt;sub&gt;3&lt;/sub&gt;) also reacts with the carbon monoxide to form quicklime (CaO) and carbon dioxide.
The quicklime then reacts with various impurities in the iron and forms a slag layer near the bottom of the furnace.
The molten iron and slag can be drawn out and used for casting or steelmaking.)&lt;/p&gt;

&lt;p&gt;In the process I&amp;#39;m currently using, the BOF uses processed ore, oxygen and a borax flux, as well as any combustible fuel.
It creates molten iron, which is piped up to the final stage, the casting unit.&lt;/p&gt;

&lt;p&gt;The penultimate stage creates sand castings.
It uses some of the stone from the crushers to make sand, and that is mixed with creosote made from tar to make the casts.&lt;/p&gt;

&lt;p&gt;Finally, the casts, molten iron and more borax flux as well as hot air from the bricks in the second stage are used to cast iron plates.&lt;/p&gt;
</description>
        <pubDate>Tue, 08 Sep 2020 00:00:00 -0400</pubDate>
        <link>http://www.joelwilliamson.ca/factorio/pyanodon/2020/09/08/pyanodon-simple-iron.html</link>
        <guid isPermaLink="true">http://www.joelwilliamson.ca/factorio/pyanodon/2020/09/08/pyanodon-simple-iron.html</guid>
        
        
        <category>factorio</category>
        
        <category>pyanodon</category>
        
      </item>
    
      <item>
        <title>Introduction to Pyanodon</title>
        <description>&lt;p&gt;I recently started a game of &lt;a href=&quot;https://factorio.com/&quot;&gt;Factorio&lt;/a&gt; using &lt;a href=&quot;https://mods.factorio.com/user/pyanodon&quot;&gt;Pyanodon&amp;#39;s mods&lt;/a&gt;.
This is a set of mods intended to make the production chains in Factorio more complex and realistic.
For example, in vanilla Factorio Electronic circuits are made by
combining copper wires and iron plates in an assembler.
This requires a total of 2 buildings to go from plates to final product.
In Pyanodons, Simple circuits (the equivalent item) require Capacitors, Inductors, Resistors, Vacuum Tubes and PCBs.
Each of the electronic components requires at least a metal plate or wire as well as Ceramic or Glass that needs to be assembled from simpler ingredients.
The PCBs have their own production chain, going from wood -&amp;gt; treated wood -&amp;gt; fiberboard -&amp;gt; formica -&amp;gt; PCB, pulling in at least one other ingredient at each step.
By my count, 27 different recipes are used, not including smelting ore into plates.&lt;/p&gt;

&lt;p&gt;Someone on Reddit commented that there are lots of posts showing Simple Circuits, but it seems most people give up shortly after.
I thought I would chronicle my progress so people interested in the mod can see what&amp;#39;s involved.&lt;/p&gt;

&lt;p&gt;The aforementioned simple circuit assembly:
&lt;img src=&quot;/images/simple-circuit-assembly.jpg&quot; alt=&quot;Simple Circuit Assembly&quot;&gt;&lt;/p&gt;

&lt;p&gt;Raw coal goes through a two stage coking process to reduce it to coke, as well as creating some tar and coal gas.
&lt;img src=&quot;/images/small-coking.jpg&quot; alt=&quot;Coking&quot;&gt;&lt;/p&gt;

&lt;p&gt;Quartz crystals are refined into molten glass before being formed into glass plates or glass beakers for science.
&lt;img src=&quot;/images/small-glass.jpg&quot; alt=&quot;Glass&quot;&gt;&lt;/p&gt;

&lt;p&gt;Alien lifeforms are grown to provide urea and methane to create melamine.
&lt;img src=&quot;/images/single-melamine.jpg&quot; alt=&quot;Melamine&quot;&gt;&lt;/p&gt;

&lt;p&gt;Most metals can be smelted in a stone furnace like in vanilla, but this is very expensive, typically using around 10 ore/plate. Here I have a three-stage copper smelter that screens and crushes the ore before casting to improve yield.
&lt;img src=&quot;/images/three-stage-copper.jpg&quot; alt=&quot;Three Stage Copper&quot;&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/factorio/pyanodon/2020/09/08/pyanodon-simple-iron.html&quot;&gt;Iron Smelting&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I&amp;#39;ll post more as the factory continues to grow.&lt;/p&gt;
</description>
        <pubDate>Tue, 08 Sep 2020 00:00:00 -0400</pubDate>
        <link>http://www.joelwilliamson.ca/factorio/pyanodon/2020/09/08/pyanodon-aar-intro.html</link>
        <guid isPermaLink="true">http://www.joelwilliamson.ca/factorio/pyanodon/2020/09/08/pyanodon-aar-intro.html</guid>
        
        
        <category>factorio</category>
        
        <category>pyanodon</category>
        
      </item>
    
      <item>
        <title>Review of Mother of Learning</title>
        <description>&lt;p&gt;&lt;a href=&quot;https://www.fictionpress.com/s/2961893/1/Mother-of-Learning&quot;&gt;Mother of Learning&lt;/a&gt; is a story I&amp;#39;d seen recommended on &lt;a href=&quot;reddit.com/r/fantasy&quot;&gt;/r/fantasy&lt;/a&gt; a few times.
In the first chapter, we&amp;#39;re introduced to the protagonist, Zorian Kazinski.
He&amp;#39;s a budding mage who is returning to the mage academy for his third year of schooling.
He is a diligent student, but is held back by poor &amp;quot;mana reserves&amp;quot; and a somewhat prickly personality.&lt;/p&gt;

&lt;p&gt;Around a month into the school year, the city hosting the school is attacked by a force of monsters
and Zorian witnesses a fellow student (Zach) fighting an incredibly powerful lich leading the monsters.
Zach makes some cryptic comments and the lich casts a mysterious spell,
and then Zorian wakes up in his bed at the beginning of the month.&lt;/p&gt;

&lt;p&gt;Zorian quickly figures out he is caught in a Groundhog Day-style time loop, living through the same month over and over,
and only he, Zach and a mysterious enemy appear to remember what is happening from month-to-month.
He sets out to learn as much magic as he can, both to figure out what is happening with the time loop and to stop the invasion of the city.&lt;/p&gt;

&lt;p&gt;This story is an interesting story of Zorian learning to be both a better mage and a better person in general.
It is somewhat long-winded (my e-reader app estimates it at 2700 pages),
but while I was reading it it always felt like he was progressing towards a goal.
It also felt like the setting was designed for Dungeons &amp;amp; Dragons,
and in the author&amp;#39;s notes at the end he confirmed that the story started out as a way to flesh out a D&amp;amp;D setting.&lt;/p&gt;

&lt;p&gt;I enjoyed it, but I&amp;#39;d be hesitant to recommend it given how long it is.&lt;/p&gt;
</description>
        <pubDate>Sat, 30 May 2020 00:00:00 -0400</pubDate>
        <link>http://www.joelwilliamson.ca/books/2020/05/30/mother-of-learning-review.html</link>
        <guid isPermaLink="true">http://www.joelwilliamson.ca/books/2020/05/30/mother-of-learning-review.html</guid>
        
        
        <category>books</category>
        
      </item>
    
      <item>
        <title>Green peppers stuffed with sausage</title>
        <description>&lt;p&gt;This one is fairly self-explanatory.&lt;/p&gt;

&lt;p&gt;Heat the oven to 350°F (175°C)&lt;/p&gt;

&lt;p&gt;Remove the casings from several sausages.
Fry the meat, as well as anything you think goes well with pork. Finely chopped apples work well.&lt;/p&gt;

&lt;p&gt;Cut the tops off of some green peppers (one per person) and remove the ribs and seeds.
Stuff the peppers with the meat.&lt;/p&gt;

&lt;p&gt;Bake for 20 minutes.&lt;/p&gt;
</description>
        <pubDate>Fri, 21 Aug 2015 00:00:00 -0400</pubDate>
        <link>http://www.joelwilliamson.ca/cooking/2015/08/21/green-peppers-sausage.html</link>
        <guid isPermaLink="true">http://www.joelwilliamson.ca/cooking/2015/08/21/green-peppers-sausage.html</guid>
        
        
        <category>cooking</category>
        
      </item>
    
      <item>
        <title>Stir-Fried Shrimp</title>
        <description>&lt;p&gt;Gwen&amp;#39;s dad sent us a recipe for stir-fried shrimp with rice wine, and it turned out quite well.&lt;/p&gt;

&lt;p&gt;Shell a pound of shrimp.&lt;/p&gt;

&lt;p&gt;Heat ¼ cup of oil in a wok on high. Add 4 thinly sliced scallions and some chopped ginger. Cook until nicely fragrant.&lt;/p&gt;

&lt;p&gt;Add the shrimp and cook fully.&lt;/p&gt;

&lt;p&gt;Add 1 tablespoon of brown sugar and 2 tablespoons of rice wine. He recommended shàoxīng wine, but the LCBO near us only had saké, which worked fine. Simmer 2 or 3 minutes, then serve.&lt;/p&gt;
</description>
        <pubDate>Sat, 04 Jul 2015 00:00:00 -0400</pubDate>
        <link>http://www.joelwilliamson.ca/cooking/2015/07/04/stir-fried-shrimp.html</link>
        <guid isPermaLink="true">http://www.joelwilliamson.ca/cooking/2015/07/04/stir-fried-shrimp.html</guid>
        
        
        <category>cooking</category>
        
      </item>
    
      <item>
        <title>Seeding the PRNG in a C program</title>
        <description>&lt;p&gt;The POSIX specification provides a pseudo-random number generator via the
&lt;code&gt;random(3)&lt;/code&gt; and &lt;code&gt;srandom(3)&lt;/code&gt; functions. &lt;code&gt;srandom()&lt;/code&gt; should be called at program
start to seed the generator. (If no seed is provided, &lt;code&gt;random()&lt;/code&gt; acts as though
&lt;code&gt;srandom(1);&lt;/code&gt; was called. This provides reproducible behaviour for testing, but
an explicit seed might be preferred.) How should we get a random seed for the
call to &lt;code&gt;srandom()&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;On x86_64 Linux, the loader for ELF files places a collection of values known as
the auxilary vector just below the bottom of the stack. These values can be
accessed through the &lt;code&gt;getauxval(3)&lt;/code&gt; function, found in &lt;code&gt;sys/auxv.h&lt;/code&gt;.
One of the entries in this vector is &lt;code&gt;AT_RANDOM&lt;/code&gt;, a pointer to 16 bytes of
random data.&lt;/p&gt;

&lt;p&gt;We can therefore seed the generator via&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdlib.h&amp;gt; // random(), srandom()
#include &amp;lt;sys/auxv.h&amp;gt; // getauxval(), AT_RANDOM
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;random_seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getauxval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AT_RANDOM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;srandom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This should not be used in a situation where the seed must be hidden from the
user, as the auxilary vector can be displayed by setting the &lt;code&gt;LD_SHOW_AUXV&lt;/code&gt;
environment variable prior to calling the program.&lt;/p&gt;
</description>
        <pubDate>Fri, 20 Feb 2015 00:00:00 -0500</pubDate>
        <link>http://www.joelwilliamson.ca/programming/c/linux/2015/02/20/seeding-the-prng.html</link>
        <guid isPermaLink="true">http://www.joelwilliamson.ca/programming/c/linux/2015/02/20/seeding-the-prng.html</guid>
        
        
        <category>programming</category>
        
        <category>C</category>
        
        <category>linux</category>
        
      </item>
    
      <item>
        <title>Parsec and Left Recursive Grammars</title>
        <description>&lt;p&gt;The traditional way to specify the syntax of a programming language is to write
a Context Free Grammar (CFG) in Extended Backus-Naur Form (EBNF). Briefly, this
form consists of a series of rules, each describing the form of some syntax
element. For example, we might have a rule for postfix expression in C:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;postfix-expression:
   primary-expression
   | postfix-expression &apos;[&apos; expression &apos;]&apos;
   | postfix-expression &apos;(&apos; argument-expression-list &apos;)&apos;
   | postfix-expression &apos;.&apos; identifier
   ⋮
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This says that a postfix expression can be a primary expression (constant,
identifier, or expression in parentheses) or it can be a postfix expression
followed by some operator. An expression like&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;n&quot;&gt;rectangle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bottomLeft&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;is thus parsed as&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;((primary-expression) &apos;.&apos; identifier) &apos;.&apos; identifier
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The rule for &lt;code&gt;postfix-expression&lt;/code&gt; is known as &lt;strong&gt;left recursive&lt;/strong&gt; because
&lt;code&gt;postfix-expression&lt;/code&gt; appears as the leftmost symbol in some of the productions.
This causes a problem for most left-to-right parsers. When they attempt to parse
a postfix expression, they might first attempt to parse it as a
&lt;code&gt;primary-expression&lt;/code&gt;. If that fails, they might attempt to parse it as an array
subscript. To do this, they first need to parse a &lt;code&gt;postfix-expression&lt;/code&gt;. This
leads to an infinite recursion, where the parser continues to nest deeper and
deeper, while never consuming any input.&lt;/p&gt;

&lt;p&gt;The traditional solution is to rewrite our rule into a &lt;strong&gt;right recursive&lt;/strong&gt; form.
In this case, we notice that every &lt;code&gt;postfix-expression&lt;/code&gt; begins with a
&lt;code&gt;primary-expression&lt;/code&gt;, followed by a (possibly empty) chain of subscripts, member accesses and
function calls.
We can thus rewrite the grammar as&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;postfix-expression: primary-expression postfix-expression&apos;

postfix-expression&apos;: empty
                   | &apos;[&apos; expression &apos;]&apos; postfix-expression&apos;
                   | &apos;(&apos; argument-expression-list &apos;)&apos; postfix-expression&apos;
                   | &apos;.&apos; identifier postfix-expression&apos;
                   ⋮
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Unfortunately, this changes the associativity of the operations. Instead of
being left associative, they are now right associative, and our example would be
parsed as&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;primary-expression (&apos;.&apos; identifier (&apos;.&apos; identifier))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The solution to this is somewhat dependent on the particulars of your parser.
I have been using &lt;a href=&quot;https://wiki.haskell.org/Parsec&quot;&gt;Parsec&lt;/a&gt; for parsing. The
left recursive grammar would be written as&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;kr&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostfixExpression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Primary&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PrimaryExpression&lt;/span&gt;
                       &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ArraySubscript&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostfixExpression&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Expression&lt;/span&gt;
                       &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;FunctionCall&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostfixExpression&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;ArgumentExpression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
                       &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Member&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostfixExpression&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Identifier&lt;/span&gt;
                       &lt;span class=&quot;err&quot;&gt;⋮&lt;/span&gt;
                       
&lt;span class=&quot;n&quot;&gt;postfixExpression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;liftM2&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ArraySubscript&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;postfixExpression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brackets&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;&amp;lt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;liftM2&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;FunctionCall&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;postfixExpressioon&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parens&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;many&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argumentExpression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;&amp;lt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;liftM2&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Member&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;postfixExpression&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;identifier&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;err&quot;&gt;⋮&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;&amp;lt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;liftM&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Primary&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;primaryExpression&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This builds an abstract syntax tree and nicely matches the structure of the
grammar. Unfortunately, it doesn&amp;#39;t complete (as we have seen). The advice on most
blogs I have seen for dealing with left recursion is to use the &lt;code&gt;chainl1&lt;/code&gt;
combinator. This would be a nice solution, but it assumes all the subexpressions
have the same type as the result of combining them. &lt;code&gt;chainl1&lt;/code&gt; can be adapted to
handle the case of subexpressions of type &lt;code&gt;a&lt;/code&gt; and a result of type &lt;code&gt;b&lt;/code&gt;. This was
useful for handling arithmetic expressions.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;n&quot;&gt;chainl1&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Parser&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Parser&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Parser&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;chainl1&apos;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;op&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baseConstructor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;baseConstructor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
                             &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;op&lt;/span&gt;
                             &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt;
                             &lt;span class=&quot;n&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;
                         &lt;span class=&quot;o&quot;&gt;&amp;lt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AdditiveExpression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;MultiplicativeExpression&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;MultiplicativeExpression&lt;/span&gt;
                        &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Add&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AdditiveExpression&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;MultiplicativeExpression&lt;/span&gt;
                        &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Subtract&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;AdditiveExpression&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;MultiplicativeExpression&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;parsePlusMinus&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reservedOp&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;+&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reservedOp&lt;/span&gt; &lt;span class=&quot;sc&quot;&gt;&apos;-&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Subtract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;additiveExpression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chainl1&apos;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;multiplicativeExpression&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parsePlusMinus&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;MultiplicativeExpression&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This works beautifully for defining the arithmetic of a language, which is often
many levels deep and each level has similar structure to all the others. Postfix
expressions lack this regular structure, so we need a slightly more involved
approach. It might be possible to make an even more general chain combinator,
which associates each operator parser with an expression parser, but it would
only be used in this location, so it is simpler to just handle this case
independently. The structure is similar to chainl1&amp;#39;, but with several
alternatives&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;n&quot;&gt;postfixExpression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Primary&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;primaryExpression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rest&lt;/span&gt;
    &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
                         &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;brackets&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ArraySubscript&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                         &lt;span class=&quot;o&quot;&gt;&amp;lt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parens&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;expression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;FunctionCall&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                         &lt;span class=&quot;o&quot;&gt;&amp;lt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;identifier&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Member&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                         &lt;span class=&quot;o&quot;&gt;&amp;lt;|&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This runs nicely and still preserves clarity.&lt;/p&gt;

&lt;p&gt;Coming up with this solution made showed just how valuable having the source for
a solution that does something similar to what you want is, and how valuable
having a package management system (Hackage) that can show source is.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edit:&lt;/strong&gt; I messed up the final version of &lt;code&gt;postfixExpression&lt;/code&gt;.
The post was written before I had completed the loop inherent in the C expression type,
and consequently couldn&amp;#39;t test the parser.
This is a case where implicit typing bit me.
I knew &lt;code&gt;rest&lt;/code&gt; should have been&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;n&quot;&gt;rest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostfixExpression&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;ParsecT&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Identity&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PostfixExpression&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;but hadn&amp;#39;t annotated it. My original version was taking a &lt;code&gt;PasecT&lt;/code&gt; as its argument,
which lead back to the looping problem.&lt;/p&gt;
</description>
        <pubDate>Wed, 04 Feb 2015 00:00:00 -0500</pubDate>
        <link>http://www.joelwilliamson.ca/programming/parsing/haskell/2015/02/04/Parsec-Left-Recursion.html</link>
        <guid isPermaLink="true">http://www.joelwilliamson.ca/programming/parsing/haskell/2015/02/04/Parsec-Left-Recursion.html</guid>
        
        
        <category>programming</category>
        
        <category>parsing</category>
        
        <category>haskell</category>
        
      </item>
    
      <item>
        <title>C Initialization Designators</title>
        <description>&lt;p&gt;I have been working on a C parser over the last few days, and because of this I
have been reading the section of C99 relating to syntax much more carefully than
I have in the past. In particular, §6.7.8 Initialization had a construct I had
never seen before.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;initializer:
    assignment-expression
    { initializer-list }
    { initializer-list , }

initializer-list:
    designation_opt initializer
    initializer-list , designation_opt initializer

designation:
    designator-list =

designator-list:
    designator
    designator-list designator

designator:
    [ constant-expression ]
    . identifier
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;It starts very conventionally. If we are initializing a scalar (or copying an
aggregate or union), we use an assignment expression. This is the familiar&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;aggregate_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;some_function_returning_aggregate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We can have a list of initializers enclosed in braces. This is the well known
construct for initializing arrays:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We can initialize based on the members of a structure (this was added in C99, so
anyone using compilers that are based on 25+ year old standards won&amp;#39;t have this):&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;point&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This is all basic stuff, that I see in common use. What caught my eye, largely
because I couldn&amp;#39;t see how the EBNF translated to C syntax, was the concept of
designator-lists and the &lt;code&gt;[ constant-expression ]&lt;/code&gt; form of designators. This
lets us initialize particular elements of an array, and do precise initialization
of nested aggregates.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Point&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;points&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Point&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;otherPoints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;otherPoints&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Since this could leave a minefield of partially initialized structures, it is
easy to understand why the are so infrequently used. I am not sure I have ever
seen this syntax in a codebase, and I have certainly never seen it in teaching
materials. It also wasn&amp;#39;t carried over into C++, so it could prevent a program
from being compiled by both C and C++ compilers. Nevertheless, if you want to
pick up on interesting (and potentially useful) language features, reading the
standard (or your compiler&amp;#39;s manual) is invaluable.&lt;/p&gt;
</description>
        <pubDate>Tue, 03 Feb 2015 00:00:00 -0500</pubDate>
        <link>http://www.joelwilliamson.ca/programming/c/2015/02/03/C-Initialization-Designators.html</link>
        <guid isPermaLink="true">http://www.joelwilliamson.ca/programming/c/2015/02/03/C-Initialization-Designators.html</guid>
        
        
        <category>programming</category>
        
        <category>C</category>
        
      </item>
    
  </channel>
</rss>
