roganjoshua

roganjoshua

Lecc and Yecc - trying to take some plain text and turn it into a model

I am trying to take some plain text and turn it into a model.
More specifically take a chordpro file and turn it into a struct.

I thought I could maybe use lecc and yecc to do this:

For example,

  @songexample """
  {title:Take It Easy}
  {st:Eagles}

  Well I'm a [G]runnin' down the road try'n to loosen my load

  I've got seven women [D]on my [C]mind

  """

And turn it into something like this:


%{
      title: "Take it Easy",
      by: "Eagles",
      chords: ["G"],
      lines: [
        %{
          line: [
            %{
              linetype: "lyric",
              content: "Well I'm a"
            },
            %{
              linetype: "chord",
              content: "C"
            }
          ]
        }
      ]
    }

OR...

    %{
      "title" => "Take It Easy",
      "by" => "Eagles",
      "chords" => [
        %{
          "name" => "A"
        }
      ],
      "lines" => [
        %{
          "line" => [
            %{
              "type" => "lyric",
              "content" => "Well I'm a"
            },
            %{
              "type" => "chord",
              "content" => "[G]"
            },

perhaps.

It struck me that perhaps a I could use lecc and yecc.

So I have this:

Definitions.

START_CURLY     = {
END_CURLY       = }
DIRECTIVE_SEP   = :
CHORD           = \[[a-zA-Z\s,.'\-+_#0-9]+\]
TEXT            = [a-zA-Z\s,.'\-+_#]*
WHITESPACE      = [\t\n\r]+

Rules.

{START_CURLY}   : {token, {start_directive, TokenLine}}.
{DIRECTIVE_SEP} : {token, {directive_sep, TokenLine}}.
{END_CURLY}     : {token, {end_directive, TokenLine}}.
{CHORD}         : {token, {chord, TokenLine, TokenChars}}.
{TEXT}          : {token, {text, TokenLine, TokenChars}}.
{WHITESPACE}    : skip_token.

Erlang code.

Which produces this:

[
  {:start_directive, 1},
  {:text, 1, ~c"title"},
  {:directive_sep, 1},
  {:text, 1, ~c"Take It Easy"},
  {:end_directive, 1},
  {:start_directive, 2},
  {:text, 2, ~c"st"},
  {:directive_sep, 2},
  {:text, 2, ~c"Eagles"},
  {:end_directive, 2},
  {:text, 4, ~c"Well I'm a "},
  {:chord, 4, ~c"[G]"},
  {:text, 4, ~c"runnin' down the road try'n to loosen my load"},
  {:text, 6, ~c"I've got seven women "},
  {:chord, 6, ~c"[D]"},
  {:text, 6, ~c"on my "},
  {:chord, 6, ~c"[C]"},
  {:text, 6, ~c"mind"}
]

Which seems reasonable…
and the yecc parser

Nonterminals
    song
    lines
    line
    elements
    element
    directive.

Terminals
    start_directive
    end_directive
    directive_sep
    chord
    text.

Rootsymbol song.

song -> lines : {song, '$1'}.

lines -> line : '$1'.
% lines -> line lines : ['$1'|'$2'].

line -> elements : {properties, '$1'}.

elements -> element : ['$1'].
elements -> element elements : ['$1'|'$2'].

element -> chord : {chord, unwrap('$1')}.
element -> text : {text, unwrap('$1')}.
element -> directive : '$1'.

directive -> start_directive text directive_sep text end_directive : {list_to_atom(unwrap('$2')), unwrap('$4')}.

Erlang code.

unwrap({_, _, Content}) -> Content.

Which is not quite there…

{:ok,
 {:song,
  {:properties,
   [
     title: ~c"Take It Easy",
     st: ~c"Eagles",
     text: ~c"Well I'm a ",
     chord: ~c"[G]",
     text: ~c"runnin' down the road try'n to loosen my load",
     text: ~c"I've got seven women ",
     chord: ~c"[D]",
     text: ~c"on my ",
     chord: ~c"[C]",
     text: ~c"mind"
   ]}}}

I think I have the xrl file more or less understood.

But I have been struggling with with the yrl part where I have arrived at it through fumbling and intuition and a little AI but I haven’t really figured out how to get to

%{
      title: "Take it Easy",
      by: "Eagles",
      chords: ["G"],
      lines: [
        %{
          line: [
            %{
              linetype: "lyric",
              content: "Well I'm a"
            },
            %{
              linetype: "chord",
              content: "C"
            }
          ]
        }
      ]
    }

I am not quite understanding how the yecc part reads the tokens and how the left part and right part work :confused:

Most Liked

thiagomajesk

thiagomajesk

Have you seen this post before? Using Leex and Yecc in Elixir

I haven’t used Leex and Yecc myself yet, but when I was doing some research I found this to be an extremely useful resource.

roganjoshua

roganjoshua

That is extremely helpful, I haven’t seen that, best explanation for me so far!

Thank you

Where Next?

Popular in Questions Top

ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement