-
Notifications
You must be signed in to change notification settings - Fork 0
/
fun-with-instaparse.html
39 lines (39 loc) · 4.42 KB
/
fun-with-instaparse.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<head><title>Fun with Instaparse</title><link href="tufte-css/tufte.css" rel="stylesheet" /><script src="http:https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script></head><body><h1>Fun with Instaparse</h1><p class="subtitle"><a href="about-me.html">John Jacobsen</a></p><div><p><span>One of my favorite talks from this month’s excellent <a href="http:https://clojure-conj.org/">Clojure/conj</a> was <a href="http:https://gigasquid.github.io/">Carin Meier</a>'s presentation, which combined storytelling, live coding, philosophy,
the history of computing, and flying robotic drones. She used the
relatively new <a href="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/Engelberg/instaparse">Instaparse library</a> to <a href="http:https://gigasquidsoftware.com/wordpress/?p=689">create her
own language</a> to explore something called “Speech Acts” (which I won’t
go into here, but do catch the video of her talk when it goes up).</span></p><p><span>My university work was in physics (and art) rather than CS, but I
have long been interested in the implementation of programming languages,
even going so far as to write a simple parser for Lisp-style math
expressions in Pascal many years ago. Last year I had the opportunity to
take the first <a href="http:https://dabeaz.blogspot.com/2012/01/compiler-experiment-begins.html">“write a compiler in Python” class</a> offered by <a href="http:https://dabeaz.com">David Beazley</a> here in Chicago, in which we implemented a subset of the Go
language. His <a href="http:https://www.dabeaz.com/ply/index.html">PLY</a> library is a great way
to get started with implementing language parsers in Python, and the
relative ease of doing so, compared with classic C implementations
described in the infamous <a href="http:https://www.amazon.comCompilers-Principles-Techniques-Alfred-Aho/dp/0201100886">Dragon Book</a>, inspired me to do some further <a href="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/eigenhombre/PyClojure">experimentation of my own</a>.</span></p><p>With this background, and inspired by Carin’s talk, I have been waiting
for an opportunity to try out Instaparse, which is getting great press in
the Clojure world. Instaparse takes a grammar as input (in the form of a
string), and gives you a parser in the language specified by that
grammar. It will also let you specify rules for transforming the
resulting tree into something your Clojure program can use more directly
(for example, by converting data types or removing unneeded elements from
the parse tree).</p><p><span>When the need arose this weekend to read in Python configuration
files into a Clojure program, I decided the time was ripe. I also wanted
to document the journey using some form of <a href="http:https://en.wikipedia.org/wiki/Literate_programming">literate programming</a>. A library called <a href="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/gdeer81/marginalia">Marginalia</a> (Michael Fogus <em>et. al.</em>) made this pretty easy.</span></p><p><span>The results are <a href="http:https://eigenhombre.com/semi-literate-programming/parsepy.html">here</a>, as well as on <a href="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/eigenhombre/parsepy">GitHub</a>.</span></p><p>My impressions, after doing this project in just a few hours, are that
(1) literate programming is great fun; and (2) Instaparse sets a new
standard for power and expressiveness when converting structured text
into abstract syntax trees. If you have a DSL or some other text-based,
formal language you want to parse, and you are either literate in Clojure
or interested in becoming so, Instaparse would be a great tool to check
out.</p></div><div><p><a href="about-me.html">about</a>|<a href="content.html">all posts</a></p><p>© 2016 <a href="about-me.html">John Jacobsen</a>. Created with <a href="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/eigenhombre/unmark">unmark</a>. CSS by <a href="https://edwardtufte.github.io/tufte-css/">Tufte-CSS</a>.</p></div><script type="text/javascript">var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-40279882-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http:https://www')
+ '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();</script></body>