kokolegorille
Hello everyone,
I am having some trouble understanding leex behaviour. To give some context, I would like to create a parser for pgn files.
Here is some code to explain my problem.
I created the lexer file pgn_lexer.xrl with
% -- Definitions.
Definitions.
TAG = (\[[^\]]*\])+
COMMENT = ({[^}]*})+
WHITESPACE = [\s\t\n\r]
Rules.
{TAG} : {token, {tag, TokenLine, TokenChars}}.
{COMMENT} : {token, {comment, TokenLine, TokenChars}}.
{WHITESPACE}+ : skip_token.
Erlang code.
The important rule is for TAG
( \[ [^\]] * \] )+
I cannot understand why
iex> :pgn_lexer.string '[Tag1 "Value1"] [Tag2 "Value2"]{comment}'
{:ok,
[
{:tag, 1, '[Tag1 "Value1"]'},
{:tag, 2, '[Tag2 "Value2"]'},
{:comment, 2, '{comment}'}
], 2}
# This works has expected, it detects 2 tags
iex> :pgn_lexer.string '[Tag1 "Value1"][Tag2 "Value2"]{comment}'
{:ok, [{:tag, 1, '[Tag1 "Value1"][Tag2 "Value2"]'}, {:comment, 1, '{comment}'}],
1}
# This does not work, tag1 and tag2 are merged
Why do the tags need to be separated by a space?
Thanks for enlightments.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
I think I’ve found a small improvement I could contribute to <%= web_namespace %>.CoreComponents (installer/templates/phx_web/compo...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kip
The reason is that regexes in Leex are greedy. Your regex in this case matches the longest string possible. Here’s a example using your regex and data in IEx:
In general I find these issues become prevalent when you’re using Leex to parse instead of just tokenize. (I know this because I’ve spent hours on similar cases myself). My learning is to use Leex to tokenise and Yecc to parse.
Or look at nimble_parsec or ex_spirit
kokolegorille
Thank You for the answer, now I know why, and I know why I am spending hours too
NobbZ
Yes, they are greedy, but thats not the cause here.
TAGis defined as(\[[^\]]*\])+and thus we demand at least one but allow many repitions of square-bracket-pairs-with-stuff-inbetween and consolidate them into one token.If the second example is expected to spit out 2
tagtokens, thenTAGshould be just(\[[^\]]*\])(without the+), now we should even be able to remove the grouping parens without changing anything.kokolegorille
That did the trick, thank You very much.
Here is the modified version…
And as mentionned, the short version works as well
kip
Arrrggghhhhh sorry for my misleading reply.