marcuslankenau

marcuslankenau

ExLR - LR Parser library

Today I published a toy project, that I worked on in the last couple of weeks. It is an LR Parser generator. I normally use yecc in my projects for parsing. It works ok, but I am not happy with usability. Besides that it seems to have issues with special (utf-8) characters. I also tried other libraries (like xpet), but struggling to use them for my purpose (probably my fault understanding the concepts). I created an LR parser years ago in c# and always dreamed of redoing it in elixir.

The parser is defined using a DSL:

  use ExLR

  lr skip_whitespaces: true do
    S <- S + "+" + :integer = fn ([a, _, b]) -> a + b end
    S <- :integer           = fn ([a]) -> a end
  end

It provides a parse function that can be used like

# parse("3+4")
{:ok, 7}

It contains a scanner, but it can also be called with a list of symbols (using a different scanner). Each symbol is a tuple of 3 containing the symbol, the data and the position:

parse([{:integer, 3, {0, 0}}, {"+", nil, {0, 1}}, {:integer, 4, {0, 2}}, {:"$", nil, {0, 3}}])

The scanner is initialised by going through the rules. It accepts:

  • Strings
  • :integer
  • :text
  • :quoted_text
  • :ws

Special terminals can also be defined:

  terminal :zip_code, chars: ?0..?9, min: 4, max: 5

  lr skip_whitespaces: true do
    Address <- Zip + City = fn [zip, city] -> %{zip: zip, city: city} end
    Zip <- :zip_code
    City <- :text
  end

You can find the repository here: GitHub - mlankenau/exlr · GitHub
I put a couple of examples in the test folder. I think they are a good reference.

I hope it is adding some values for other devs and highly appreciate any feedback.

Happy easter :slight_smile:

Where Next?

Popular in Announcing Top

Crowdhailer
Experimenting with this code. OK.try do user &lt;- fetch_user(1) cart &lt;- fetch_cart(1) order = checkout(cart, user) save_orde...
New
josevalim
Yes, yet another parser combinator library! Most of the parser combinators in the ecosystem are either compile-time, often using AST tra...
159 19870 141
New
wfgilman
I’ve cleaned up and open sourced three financial libraries I was using for my company. They are bindings for the APIs of these three comp...
New
josevalim
EDIT: since Ecto 3.0 final version is out, this post was amended to use the final versions in the instructions below. Hi everyone, We a...
New
devonestes
Introducing assertions, the library that helps you write really great test assertions! GitHub: GitHub - devonestes/assertions: Helpful a...
New
tmbb
PhoenixWS - Websockets over Phoenix Channels Source code on Github here: GitHub - tmbb/phoenix_ws: Websockets implemented over Phoenix Ch...
New
nikokozak
Hello all, I’ve been working on Svonix - a library for quickly integrating Svelte components into Phoenix views. It’s a much-needed succ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

We're in Beta

About us Mission Statement