cosmicrose

cosmicrose

I’m having trouble implementing a parser. Either it enters an infinite recursive loop, or it only parses the first part of the input and drops the rest.

For context, I’m writing a parser for a query language, and it contains the ability to nest boolean expressions. Here’s a stripped-down version of what I’m working with:

defmodule CESQL.Parsec do
  import NimbleParsec

  value_identifier = ascii_string([?a..?z], min: 1, max: 20)

  boolean_operation =
    parsec(:expr)
    |> ignore(string(" "))
    |> choice([
      string("AND"),
      string("OR"),
      string("XOR")
    ])
    |> ignore(string(" "))
    |> parsec(:expr)

  expr =
    choice([
      value_identifier,
      boolean_operation
    ])

  defparsec :expr, expr
end

My end goal is to parse a boolean expression, and I intend to allow for complex nested expressions like (a AND b) OR c. But for starters, this is what I want out of the above code:

iex> CESQL.Parsec.expr("a AND b")
{:ok, ["a", "AND", "b"], "", _, _, _}

However, when I place value_identifier first in expr’s argument to choice/2, the parser takes the first value and drops the rest of the string, which looks like this:

iex> CESQL.Parsec.expr("a AND b")
{:ok, ["a"], " AND b", _, _, _}

Alternatively, when I place boolean_operation as the first choice, I believe the parser enters an infinite loop trying to find the start of an expression, because my test times out.

How can I get this working the way I want it to? I’ve tried using NimbleParsec.lookahead/2 every way I can think of, but I might be misunderstanding how it works, because I’ve had no luck.

Showing Posts 1 to 7

jakemorrison

jakemorrison

cosmicrose

cosmicrose OP

I think I’m actually going to try leex and yecc instead of NimbleParsec. NimbleParsec is awesome, but since I’m trying to implement a query language (the CloudEvents query language), I think it’s more suited to the job. And I only recently discovered this additional feature of Erlang and I’m really excited to use it! For anyone else who wants to parse a language using traditional language-parsing tools, check out Tokenizing and parsing in Elixir with yecc and leex – Andrea Leopardi

jakemorrison

jakemorrison

Since there are ABNF grammars for SQL around, you might also look at ex_abnf

kip

kip

ex_cldr Core Team

Using Leex and Yecc is very workable (and what I used for the fundamentals of ex_cldr). But because parsers are fun, here’s a reasonable attempt at parsing your logical expressions in nimble_parsec using a common approach to de-structuring such parsers:

defmodule CESQL.Parsec do
  @moduledoc """
  Based upon the simple grammar of:

    Expression ⇒ Term {AND Term}
    Term ⇒ Factor {OR Factor}
    Factor ⇒ Item | "-" Factor
    Item ⇒ Identifier | "(" Expression ")"

  """
  import NimbleParsec

  whitespace = times(ascii_char([?\s, ?\t]), min: 1)

  # An expression
  defparsec(
    :expr,
    ignore(optional(whitespace))
    |> choice([
      parsec(:term) |> parsec(:op_and) |> parsec(:expr) |> reduce(:postfix),
      parsec(:term)
    ])
  )

  # A term
  defparsec(
    :term,
    choice([
      parsec(:factor) |> parsec(:op_or) |> parsec(:term) |> reduce(:postfix),
      parsec(:factor)
    ])
  )

  # A factor
  defparsec(
    :factor,
    choice([
      parsec(:identifier),
      ignore(ascii_char([?(]))
      |> ignore(optional(whitespace))
      |> parsec(:expr)
      |> ignore(optional(whitespace))
      |> ignore(ascii_char([?)]))
    ])
  )

  # OR operation
  defparsec(
    :op_or,
    ignore(whitespace) |> string("OR") |> ignore(whitespace)
  )

  # AND operation
  defparsec(
    :op_and,
    ignore(whitespace) |> string("AND") |> ignore(whitespace)
  )

  # An identifier (lower case letters)
  defparsec(
    :identifier,
    times(ascii_char([?a..?z]), min: 1) |> reduce({List, :to_string, []})
  )

  # Convert infix list to postfix for more regular "AST"
  def postfix([term_1, op, term_2]) do
    [op, term_1, term_2]
  end

  # Just pattern matching some examples
  def test do
    {:ok, [["AND", "a", "b"]], "", %{}, _, _} = expr("a AND b")
    {:ok, [["OR", "a", "b"]], "", %{}, _, _} = expr("a OR b")

    # precedence
    {:ok, [["AND", "a", ["OR", "b", "c"]]], "", %{}, _, _} = expr("a AND b OR c")
    {:ok, [["AND", ["OR", "a", "b"], "c"]], "", %{}, _, _} = expr("a OR b AND c")

    # nesting
    {:ok, [["OR", "a", ["AND", "b", "c"]]], "", %{}, _, _} = expr("a OR (b AND c)")

    :ok
  end
end
ityonemo

ityonemo

/Self-promotion but I also have pegasus: Pegasus — pegasus v1.0.0 if you like grammars that actually look like grammars

kip

kip

ex_cldr Core Team

@ityonemo wow, that’s very cool. Will definitely be taking that for a spin!

TwistingTwists

TwistingTwists

This post was so useful !
Writing recursive parsers here!
https://github.com/TwistingTwists/swift_class/blob/master/lib/bracket_attributes.ex#L50
Thanks for such insight!

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews