Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"a|a*b" should match "aa" but it doesn't #456

Open
zeng-y-l opened this issue Dec 21, 2024 · 6 comments
Open

"a|a*b" should match "aa" but it doesn't #456

zeng-y-l opened this issue Dec 21, 2024 · 6 comments
Labels
bug Something isn't working T-backtracking Triage: issue related to backtracking

Comments

@zeng-y-l
Copy link

Example:

use logos::Logos;

#[derive(Debug, PartialEq, Logos)]
enum Tok {
    #[regex("a|a*b")]
    T,
}

fn main() {
    let lex = Tok::lexer("aa");
    assert_eq!(lex.collect::<Vec<_>>(), [Ok(Tok::T), Ok(Tok::T)]); // [Err(())]
}

Maybe it's related to #160. Is this a bug? Thank you so much!

@jeertmans
Copy link
Collaborator

Hi @zeng-y-l, I didn’t verify but I think you need to quote your patterns around the or operator; otherwise you current pattern needs to match a « b » at the end.

@zeng-y-l
Copy link
Author

I want it to match a or a*b. I have tried a|(a*b), and the result is the same.

@zeng-y-l
Copy link
Author

Graph:

{
    1: ::T,
    3: b ⇒ 1,
    4: [
        a ⇒ 4,
        _ ⇒ 3,
    ],
    5: {
        a ⇒ 4,
        b ⇒ 1,
        _ ⇒ 1,
    },
    8: {
        a ⇒ 5,
        b ⇒ 1,
    },
}

@minirop
Copy link

minirop commented Dec 30, 2024

Seems to be an issue with the * operator, because something like #[regex("a|ab|aab|aaab|aaaab")] works fine yet #[regex("a|aaa*b")] doesn't.

@maciejhirsz
Copy link
Owner

This is 99% an issue with backtracking, once the lexer sees the second a it will attempt to match the loop and if it doesn't find b it errors instead of falling back to last potentially correct match.

@jeertmans
Copy link
Collaborator

Thanks for the update, I didn't see the notification!

I will create a new label (that we should also add to older similar issues) to track issues related to backtracking.

@jeertmans jeertmans added bug Something isn't working T-backtracking Triage: issue related to backtracking labels Dec 30, 2024
@jeertmans jeertmans changed the title "a|a*b" should match "aa" but it doesn't a|a*b" should match "aa" but it doesn't Dec 30, 2024
@jeertmans jeertmans changed the title a|a*b" should match "aa" but it doesn't "a|a*b" should match "aa" but it doesn't Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working T-backtracking Triage: issue related to backtracking
Projects
None yet
Development

No branches or pull requests

4 participants