jehrhardt

jehrhardt

I want parse some text based format and I found two solutions for building a parser:

  • Use Erlang’s built in leex and yecc, which are easy to use in Elixir.
  • Use parser combinators like Nimble_parsec.

What is preferred way to parse text in Elixir? And why?

Showing Posts 1 to 4

Fl4m3Ph03n1x

Fl4m3Ph03n1x

Afaik, there is no preferred way to parse text in Elixir.

I would say you should pick the tool that is both:

  1. most mature and stable
  2. better fits your use case

Erlang and Elixir go hand in hand. You can’t really be good at Elixir without knowing some Erlang basics - it’s everywhere.

So when the time comes to pick a tool, I’d suggest you to not focus on the language it is made in, but instead on the features and usability the tools provide.

Hope it helps!

david_ex

david_ex

In my (pretty limited) experience, I would go with:

  • leex and yecc if your text is rigidly structured and follows some sore of grammar (e.g. it’s a DSL, config file, source code, etc.)
  • NimbleParsec if your text is semi-structured (e.g. records where the schema wasn’t enforced and there are deviations) and you want to extract structured information from it

Basically, if your “rules” have a bunch of “that’s not always true” cases (e.g. "an email address usually follows the person’s name, but sometimes there’s a phone number instead’) I would go with NimbleParsec because it’s easier to manage the complexity (by combining sub-parsers).

Of course, you can also handle the variety of corner cases in the grammar given to leex and yecc, but in my experience the grammar size explodes pretty quickly and makes it challenging to keep in your head.

Also, if your case is simple you could get away with using just Elixir’s pattern matching on strings ("foo " <> rest_of_string"), here’s a description of the idea Redirect

jehrhardt

jehrhardt OP

In my case, I want to parse some Python code to annotate it. Since there is a complete Python grammar available, I will go for leex and yecc.

Thanks for the explanation.

Qqwy

Qqwy

TypeCheck Core Team

@david_ex’s answer is a great and simple ‘Tl;Dr’ one. For parsing Python code, going the route of Leex/Yecc definitely makes sense.

If you want a more in-depth question/anwer about when to use Parser Combinators and when to use Parser Generators, then this was a question I dwelled on ~2.5 years ago for quite a significant amount of time. You might enjoy reading my StackOverflow Question + Answer on this matter.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews