Dotfile Maintenance

Here is a party I’m a bit late in joining. And it is one of those ideas that makes you smack your head wondering why you didn’t think of it.

Config file tweaks

For years, I’ve maintained a tar.Z bundle that contains a .profile, .cshrc, .bashrc, .vimrc etc. You can tell how old it is by the fact that I still maintain it with compress rather than gzip. When I started it, gzip did not exist. And even after it did, I could not be sure that a system would have it installed. There are a few zillion copies of it lying around on various drives and removable media. Needless to say, each one is slightly different and it may be hard to figure out which is the “latest”.

But with the advent of github, why not use that to store and version control those files?

Github has a nice round up of various systems to do just that.

The idea is rather simple. The “actual” dotfiles live in a git repository that can be cloned and updated. What lives in your home directory is a symlink.

It can get fancier with some of the frameworks allowing you store the files as fragments that get built into a single unit. That allows you to group all the settings for e.g. fzf in one place rather than having to go hunt them down in all the various place they might reside (.vimrc, .bash_profile, etc).

Or, if you don’t like all the symlinking, you can make your entire home directory the repository.

Note that you might still need need to render the repository down to a tarball to get it on a system if the company admin does not allow connection to github – a not unusual security stance to take, mostly to keep things from moving from the system to github.

Getting Started

For something simple to get your feet wet, I would suggest Jeff Coffler’s skeleton.

Jeff has organized things by system (*nix/Win/Mac) and then by subsystem (bash, git, vim), though that second level can actually be organized anyway you want.

Fork his repository into one of your own, clone it to your system. Copy your original dotfiles into the working directory (remember to rename them to remove the dot) and run the Commit; push; you’re done.

One particular file to watch out for is nix/git/gitconfig. It has Jeff’s name and email and some other stuff that you will probably not want. So, be sure to copy your config over top.

Dot directories

Edit: This fix has been added to the upstream version now.

There is a minor flaw in the script that makes it play rough if you have a dot directory (e.g. .vim) that is a symlink. The script will delete the symlink as “stale” since it assumes all such dot things should be files. You can grab the version from my dotfiles as a fix. I will be working to get Jeff to make the change in his copy as well.

Vim packages as submodules

This is a good tutorial on how to manage you vim packages as submodules of your dotfiles repo. I really don’t have anything to add. I am using this method to handle NERDtree and lightline

Sponsored Post Learn from the experts: Create a successful blog with our brand new courseThe Blog

Are you new to blogging, and do you want step-by-step guidance on how to publish and grow your blog? Learn more about our new Blogging for Beginners course and get 50% off through December 10th. is excited to announce our newest offering: a course just for beginning bloggers where you’ll learn everything you need to know about blogging from the most trusted experts in the industry. We have helped millions of blogs get up and running, we know what works, and we want you to to know everything we know. This course provides all the fundamental skills and inspiration you need to get your blog started, an interactive community forum, and content updated annually.

Dairying in Mongolia

This will be a bit off from the normal fodder for this blog, but I thought it was interesting.

Lets start with a bit of biology.

Babies naturally produce an enzyme called lactase that allows them to digest the main sugar component of milk – lactose. Many (globally it would be most) people lose that ability near puberty. This leads to lactose intolerance and the GI issues it can cause. Many people of European heritage, however, are lactase persistent and maintain the ability to digest lactose – to the delight of the dairy industry.

The standard story in archaeology about dairy husbandry is that lactase persistence will naturally follow along the path of the spread of dairy farming. After all, it would be a great advantage to be able to consume the additional calories and proteins – as an adult – that milk and other dairy products represent. So those that are lactase persistent would be better able to spread their genes and the trait would come to dominate the population.

And when we look at the spread of dairy animals in the Africa and the Levant, that is pretty much what we see.

And then, there are the Mongols.

According to this article from HeritageDaily, the Mongols have been drinking milk as adults for 3,000 years without gaining a majority of lactase persistence in the population.

So, the question is, why?

Has there been continuous influxes of people that are not lactase persistent – essentially swamping any shifts in allele frequency? Have they come up with some other genetic variant that mutes the normal response to lactose?

Hopefully somebody will look into that.

Musical Sensor

Vibrating cantilevers has a long history of being used as sensors, but almost always in the micro-domain. The cantilever is frequently etched into silicon or other substrate. The weight of even single molecules can be measured or detected.

The idea is fairly simple. Change the distribution of weight on the cantilever, and the frequency of vibration will change.

It can even be used as a motion detector since acceleration in the same plane as the natural vibration will either start the cantilever vibrating or will change the frequency of the vibration.

A research group at the University of California, Riverside was attempting to find a cheap, easy way to detect counterfeit or adulterated medicines.

Adulterated medicines will almost always have a difference density to the “real” product. So, if a fixed volume is tested, then the weight will be different. Cantilevers based sensors can be very sensitive to such changes.

But what could be used to create the cantilevers?

The mbira is musical instrument that uses cantilevers to produce tones (similar to a music box). This became the inspiration for the paper Musical Instruments as Sensors

Of course, there is also the need to capture the frequency and compare to a standard. Since the mbira produces tones in the audible range, why not use the recording capabilities of a smartphone?

The researchers created a site which allows a user to upload recordings and have them analyzed. According to the paper, the analysis software is written (at least in part) in python.


Another writeup

Github for the Grover Lab

Spirit X3 – Separate Lexer Part II

Last time, we looked at the lexer and supporting staff. This time, we will look at the primitive parser and final usage.

The full code is in GitHub.


The tok parser is quite simple. Give it the TokenType to look for and it returns true if that is indeed the next token in the stream.

This is the beauty of the separate lexer. The lexer is responsible for the hard work of classifying the characters and splitting them up into logical chucks. Parsing can concentrate on a little higher level of syntactical analysis – how those chunks are organized.

One subtlety in tok’s code. We must be sure to consume the token (advance the iterator) if and only if we actually match.  Since we are only assuming a ForwardIterator, we can’t depend on a decrement operation in order to “unconsume”. So we, make sure the increment is only done in the true leg of the match logic.

There is a bit of a flaw in the interface of tok. Currently it makes an undocumented assumption that the iterator’s value type has a istype operation. Once Concepts are standardize (hopefully in C++2020), we would be able to document this. As it is, passing, say, a char iterator would cause a vary hard to debug instantiation error.

In parting, we define two specializations for operator>>. These will allow use to simplify the parser expression we write. If we were doing this for real, we would also specialize operator| at the very least.


There should be no surprises here. The grammar is straight-forward. Those helper specializations come in handy letting us string together token type rather than having to explicitly wrap everything into a tok().

auto vardef = tok(tokVar) >> tokIdent >> tokSemi ;

rather than

auto vardef = tok(tokVar) >> tok(tokIdent) >> tok(tokSemi);

And we’re done.

But we can do a bit better.


The current way tok()is specified, it exposes a string as its attribute. So, the syntensized attribute for, e.g. vardef would be something like array. But this is less than desirable. Normally, we would not be concerned about an attribute of say, the var keyword. If we DID want to know, we could capture the difference as seaparate rules.

There will certainly be exceptions. For instance, if we had two keywords for a type (say int, and float), we would definitely care which of these we parsed, but not want to have a separate rule for each.

The solution is to define two different parsers – one which  returns an attribute and one which exposes the attribute type used. And in v2/parser.hpp, that is what has been done.


This is the version that returns the string. It is a renamed copy of the original tok.


This is the new one that does not return an attribute. There are only two changes from the original.

The attribute is specified as unused:

using attribute_type = x3::unused_type;

and the assignment to attr has been removed.

Now the v2/main can say…

auto vardef = tok_kw(tokVar) >> tokIdent >> tok_kw(tokSemi);

And now, the synthesized attribute for vardef is a simple string.

Other Improvement

We could make other improvements.
We could get keywords to automatically use tok_kw by using two different enum classes.
We could provide a third variety that returns the TokenType as the attribute (to solve the int, float problem.
I’ll leave those as exercises for reader.

Spirit X3 – Separate Lexer Part I

Back in this post, I said about Spirit ..

…it would be very feasible to write a lexical analyzer that makes a token object stream available via a ForwardIterator and write your grammar rules based on that.

But is it ? really?

The short answer is – Yes its feasible, but probably not a good idea.

The long answer is the journey we’ll take on the next two posts.

Overall Design

The first part will be a stand-alone lexer (named – of course – Lexer), that will take a pair of iterators to a character stream and turn it into a stream of Tokens. The token stream will be available through an iterator interface. We’ll look at in more detail in a moment.

The Spirit framework can be thought of as having 4 categories of classes/objects:

  • rules
  • combinators (“|”, “>>”, etc)
  • directives (lexeme and friends)
  • primitive parsers (char_, etc)

Only the primitive parsers truly care about the type you get when dereferencing the iterators. Unfortunately, they really care (and rightly so). So, that means we will need to write replacements for them. Fortunately  we do not have to replace any of the rule or combinator infrastructure or this would be undoable – even on a dare.

To recap – we will be writing the following classes:

  • Lexer – the tokenizer
  • Token – the class that represents the lexed tokens.
  • tok – a primitive Token parser.

We will look at Token and Lexer in this post and tok in the next.

All code can be found in the GitHub repository

Token Class

Looking at lexer.hpp, the first thing we see is the enum TokenType. No surprises here except possibly the fact that we need a special tokEOF to signal the end of the end input. This will also act as the marker for the end iterator.

struct token is also fairly simple. It will hold the TokenType, iterators to where in the input it was found and the actual lexeme. The lexeme won’t be of much use except in the case of tokIdent.

I intentionally made these small so that we could pass tokens around by value most of the time.  The embedded iterators are not really necessary for this project, but would be if this were fleshed out more with good parse error reporting.

The most important things are the istype member function and mkend()istype() will be what the parser uses to decide if there is a match. mkend() is a static helper to generate an EOF token.

Lexer Class

Lets start off in the header file – lexer.hpp.

To keep this simple, I decided to hardcode the fact that we are using std::string::const_iterators as input.

The lexer class itself is simply a shell. It holds on to the input iterators and uses them to create it’s own iterators as requested. begin(), end() are the only reason the outer class exists.


Lets look at this in some detail.

using self_type = iterator;
using value_type = token;
using reference = value_type &;
using pointer = value_type *;
using iterator_category = std::forward_iterator_tag;
using difference_type = int;

These types are require to allow our iterator to play nice with STL algorithm. The STL templates consult these typedefs to know what types to instantiate for temporary values, etc. We could use these to make the lexer class hyper-general and match any value type for which operator== is defined.

But lets not.

self_type operator++();
self_type operator++(int junk);
reference operator*();
pointer operator->();
bool operator==(const self_type& rhs) const { return m_curr_tok == rhs.m_curr_tok; };
bool operator!=(const self_type& rhs) const { return !(m_curr_tok == rhs.m_curr_tok); };

These are the operators that are needed to make it a ForwardIterator – increment and dereference and equality.

Note, that in general, you will also want to supply a const_iterator as well. The only difference would be that operator* and operator-> would return const versions.

Now lets head over to the implementation – lexer.cpp


This is a utility that – as the name on the box says – skips spaces. It also helpfully returns an indication if the end of input was reached. In an effort to be somewhat standard, isspace is used to decide whether a character needs to be skipped.


Here is the heart of the lexer. get_next_token returns by value the next token that it can get out of the input or return tokEOF if it reaches the end of input or can make a valid token out of the current position.

After skipping spaces, it checks to see if the current character is a “punctuation” token – in this case a semicolon or a parenthesis.

If not, it gathers up the next batch of consecutive alphanumeric characters and checks to see if they are a keyword. If not, it brands it an identifier.

And that’s about it for the lexer.

Next time, we’ll look at the parsing primitive and put it all together.

Static Exceptions

Dynamic Exceptions have their flaws. Herb Sutter has proposed a replacement known as Static Exceptions. Lets look at it a bit.

Before we do, we need to look at the C+11 feature std::error_code

std::error_code and Friends

Anyone who has done any coding in C knows about good old errno, the global int that many system functions will set to signal a problem.  This, of course, has many problems, not the least of which is that different platforms could and did use different integer values to represent the same error.

To bring some order to the chaos, std::error_code was added along with its friend std::error_category.

An error code is actually two numbers – an integer saying which exact error and a “category” or domain for that error. Thus the math related errors and the filesystem errors could have the same integer value, but different domains. A domain or category is nothing but a pointer to a singleton object.

For a bit more, go look at the writeup as well as a tutorial on creating your own error codes from the folks behind Outcome.  And here is another writeup on a use of custom error codes.

For our purposes, std::error_code has four really nice properties:

  • It is small – the size two pointers. It could in theory be passed around in cpu registers.
  • Creating one cannot possibly throw an exception.
  • Copying and/or moving can be done with a memcpy or just two memory read/writes.
  • It does not require any RTTI – no dynamic casting is required – only (possibly) a static_cast between integer types.

Dynamic Exceptions considered harmful

Sutter does a much better job than I can of enumerating the problems with the current exception system. So go read the paper.

And error returns schemes such as Expected or Outcome aren’t much better.

Static Exceptions

Sutters proposal is to do something like the following.

Introduce a new keyword throws.

IF you define a function as :

T my_function() throws;

Then behind the scenes the compiler will act as if the function was defined.

variant<T, std::error_code> my_function();

In the body of the function anything that looks like:

throw e;

get translated to a simple

return e;

And at the call site

try {
    x = my_function();
} catch (e) {
    /* try to recover */

Will get translated into something like:

x = my_function();
if (compiler_magic::is_error(x)) {
     /* try to recover */

This eliminates the hand-rolled “if checks” that have to be written to use something like Outcome. And it propagates. If you don’t handle the call there will still be the check, but it will have a simple return to move the exception outward.

The paper is filled with more details about the interplay between the proposed mechanism and the current exception system, noexcept, and other details the language lawyers need to care about.


I have decided to make this the standard of exception handling in Onyx. There are details to be worked out. In particular in the early stages, I will literally have to rewrite the return types in order to “reduce” Onyx to C++.

But it will be fun to try out.


Pivots (turning a column’s values into actual columns) is a very common activity. Spreadsheet programs have robust support for it. But Standard SQL? Not so much.

The Problem

Create Table orders (
  orderNumber int,
  sku char(3),
  quantity int,
  salesPerson varchar(10)

insert into orders values
( 1, 'ZR34', 2, 'Mary'),
( 1, 'AS99', 1, 'Mary'),
( 2, 'ZR34', 1, 'Jim'),
( 2, 'MB01', 1, 'Jim');

The ubiquitous order table with the SKU, quantity and sales person. To keep this simple, I did not normalize. If that bothers you, then think of the orders table as the results of joining between the all the bits.

The ask, is to product a report that shows, for each sales persons, how many of each SKU they sold.

 sku	Mary	Jim	Kiki
AS99	1	0       0
MB01	0       1	0
ZR34	2	1	0

(Kiki was apparently on vacation.)

Now if your most people, you download the data to a spreadsheet and call it a day. But we’re not most people. We have a hammer (SQL) so we are going to hammer this flat. Plus, we know that we’ll get the same request next week, and the next, etc. And who has time for that?

Standard SQL

If we were to do this in standard SQL, it would look like:

select sku, sum([Mary]) as "Mary", sum([Jim]) as "Jim", 
        sum([Kiki]) as "Kiki"
from (
      case when salesPerson = 'Jim' then quantity else 0 end as [Jim],
      case when salesPerson = 'Mary' then quantity else 0 end as [Mary],
      case when salesPerson = 'Kiki' then quantity else 0 end as [Kiki]
   from orders
   ) as bySP
group by bySP.sku

A new case statement is required for every salesperson. That’s no fun.


MSSQL has a pivot  statement that makes this a bit less painful

select sku, [Mary], [Jim], [Kiki]
from (select sku, quantity, salesPerson from orders) s
pivot (sum(quantity) for salesPerson in ( [Mary], [Jim], [Kiki])) pvt

Some notes about the syntax:

  • The alias for the pivot (eg pvt) is required.
  • The alias for the subselect is also required, even though  it isn’t used.
  • The values that form the in-list are not strings – they are column names.
  • You can use * in the outer select’s return list.

You can use a bare table in the from clause, but be careful. Any column (like sku) that is not aggregated or used as the pivot column becomes a defacto group-by. In our example orderNumber becomes another row label

select *
from orders
pivot (sum(quantity) for salesPerson in ( [Mary], [Jim], [Kiki])) pvt

Leading to:

orderNumber	sku	Mary	Jim	Kiki
1	        AS99	1	NULL	NULL
2	        MB01	NULL	1	NULL
1	        ZR34	2	NULL	NULL
2	        ZR34	NULL	1	NULL

It would be nice if we could do something like this:

select *
from (select sku, quantity, salesPerson from orders) s
pivot sum(quantity) 
for salesPerson in ( select distinct salesPerson from orders ) as pt

But unfortunately, the list of values needs to be given explicitly. The one good thing about this is that you will have a column for a value, even if there is no rows that match (think of poor Kiki).

Dynamic SQL

But this can be done using Dynamic SQL and exec. I’ve built dynamic queries such as this for the standard sql case and it is no fun. Doing so for the pivot operator is a piece of cake.

First create a salesForce table so Kiki will make an appearance.

create table salesForce(
	name varchar(10)

insert into salesForce values ('Kiki'), ('Mary'), ('Jim')

Then use a cursor to build our column list and presto!

declare sp cursor for select [name] from salesForce
declare @list varchar(500) = ''
declare @query varchar(1000)
declare @aName varchar(10)

open sp
Fetch next from sp into @aName
while @@FETCH_STATUS = 0
	if @list != ''
		set @list = @list + ', '

	set @list = @list + '[' + @aName + ']'
	fetch next from sp into @aName

close sp
deallocate sp

set @query = 'select sku, ' + @list + 
	' from (select sku, quantity, salesPerson from orders) s ' +
	'pivot (sum(quantity) for salesPerson in (' + @list + ')) pvt'

print @query

Unfortunately, to get rid of the nulls means keeping two parallel list – one for the select that has the isnull and one for the pivot value list.

I hope this helps you use the pivot statement effectively.

Identifier Parsing in Boost.Spirit X3 – custom parser

This time around, we will use a custom parser to handle the keywords.

I really hadn’t planned on making this a series, but there you go. This will be the last – I think.


I started from the code from the last post, but did make a minor adjustment. I made underbar (‘_’) a valid character in an identifier.

auto const ualnum = alnum | char_('_');
auto const reserved = lexeme[symtab >> !ualnum];
auto const ident = lexeme[ *char_('_') >> alpha >> *ualnum ] - reserved;

Custom Parser

Parsers in X3 are classes that have a parse template function with a specific signature.

template<typename Iterator, typename Context, typename RContext, typename Attribute>
    bool parse(Iterator &first, Iterator const& last, Context const& context, 
               RContext const& rcontext, Attribute& attr) const

first and last are input iterators that contain the stream of characters (or whatever the iterators are iterating) to match.

context and rcontext contain various client and system supplied information.

attr is the attribute – the value that the parser will pass back on success. In our case, this will just be the keyword itself.

And that really is it. The code itself is straightforward and pretty much mirrors what the “standard” version does – match the given string, then check that next character (if it exists) is not a letter, number, or underbar.

After that, it is just a matter of using the new parser keyword in the mkkw lambda.

Now that wasn’t bad, was it?

Here is the finished code.

* Copyright 2018 Mark Hollomon
#include <iostream>
#include <string>
#include <cctype>
#include <boost/spirit/home/x3.hpp>
namespace x3 = boost::spirit::x3;
// Custom parser
struct keyword: x3::parser<keyword> {
using attribute_type = std::string;
keyword(std::string s) : m_kw(s) {};
template<typename Iterator, typename Context, typename RContext, typename Attribute>
bool parse(Iterator &first, Iterator const& last, Context const& context,
RContext const& rcontext, Attribute& attr) const
Iterator save = first;
auto kbeg = m_kw.begin();
auto const kend = m_kw.end();
while ((first != last) & (kbeg != kend)) {
if (*first != *kbeg) break;
++first; ++ kbeg;
bool okay = true;
if (kbeg == kend) {
// Matched our target, but now must make sure
// nothing interesting comes afterward.
if (first != last) {
if (isalnum(*first)) okay = false;
else if (*first == '_') okay = false;
else okay = true;
} else {
okay = false;
if (okay) {
attr = m_kw;
} else {
first = save;
return okay;
std::string m_kw;
struct x3::get_info<keyword>
typedef std::string result_type;
std::string operator()( keyword const& kw) const
return "keyword " + kw.m_kw;
// The gramar
using x3::lexeme;
using x3::alnum;
using x3::alpha;
using x3::ascii::char_;
x3::symbols<int> symtab;
auto const ualnum = alnum | char_('_');
auto mkkw = [](std::string kw) {
return keyword(kw);
auto const kw_var = mkkw("var");
auto const kw_func = mkkw("func");
auto const reserved = lexeme[symtab >> !ualnum];
auto const ident = lexeme[ *char_('_') >> alpha >> *ualnum ] - reserved;
auto const stmt = kw_var >> ident;
auto const program = +stmt;
// Main
int main(int argc, char**argv)
if (argc < 2) {
std::cout << "Need something to parse\n";
std::string input(argv[1]);
auto iter = input.cbegin();
auto end_iter = input.cend();
std::cout << "parsing : " << input << "\n";
bool r = x3::phrase_parse(iter, end_iter, program, x3::ascii::space);
if (iter != end_iter) {
auto distance = end_iter - iter;
std::cout << "Failed: didn't parse everything\n";
std::cout << "stopped " << distance << " characters from the end "
<< "( '" << *iter << "' )\n";
return 1;
} else if (r) {
std::cout << "Good input\n";
return 0;
} else {
std::cout << "Parse failed\n";
return 1;
view raw custom_parser.cpp hosted with ❤ by GitHub


Identifier Parsing Redux

The ink hadn’t dried[1] on my Identifier Parsing post when I realized that there was indeed a better way to handle multiple keywords.

In that post I stated that a symbols<T> parser would not help because it suffered the same problem as lit(). Which is true.

What I missed was that, of course, you could use the same trick with symbols as you did with lit() to make it work.

Like this:

auto const reserved = lexeme[symtab >> !alnum];
auto const ident = lexeme[  +alnum - reserved ]

That does what we need.

AND, we can fix up our lambda to automatically register new keywords.

auto mkkw = [](std::string kw) {
    return lexeme[x3::lit(kw) >> !alnum];

Now, we can happily make up keywords and keep the rest of the parser in sync.

I will place a V4 in the Github repository.


[1] Yea, I know. Work with me.