Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Parser being greedy #296

Closed
lmrcarneiro opened this issue Feb 20, 2023 · 3 comments
Closed

Parser being greedy #296

lmrcarneiro opened this issue Feb 20, 2023 · 3 comments

Comments

@lmrcarneiro
Copy link
Contributor

lmrcarneiro commented Feb 20, 2023

Hey, Alec Thomas.
I'm trying to parse the input "aa" which should be valid accordding to the grammar, but the example program returns an error.
It seems like the parser is greedy and does not "give back", so the second expression of the Text property of the Example struct never consumes anything.

Grammar:

Example = *( ALPHA / DIGIT / "-" ) Let-dig
Let-dig = ALPHA / DIGIT

Code:

var iniLexer = lexer.MustSimple([]lexer.SimpleRule{
	{Name: `ALPHA`, Pattern: `[A-Za-z]`},
	{Name: `DIGIT`, Pattern: `[0-9]`},
	{Name: `Others`, Pattern: `-`},
})

//	Example = *( ALPHA / DIGIT / "-" ) Let-dig
//	Let-dig = ALPHA / DIGIT
type Example struct {
	Text string `@(ALPHA | DIGIT | "-")* @(ALPHA | DIGIT)`
}

func main() {
	parser := participle.MustBuild[Example](
		participle.Lexer(iniLexer))
	_, err := parser.ParseString("", "aa")
	if err != nil {
		panic(err)
	}
}

Error:
panic: 1:3: unexpected token "<EOF>" (expected (<alpha> | <digit>))

I have tried to created the "Let-dig" symbol as indicated by the grammar but as you can expect it had no effect.
I have also tried to rewrite this grammar somehow to eliminate the ambiguity but I couldn't come up with anything.
Is this my fault? Should I use some kind of regex expression to prevent the first expression from matching the last occurrence? I'm kind of lost here...
Cheers

@alecthomas
Copy link
Owner

Participle is greedy yes.

@lmrcarneiro
Copy link
Contributor Author

Okay, thanks.
Fow now I just added an extra check that returns an error when converting to string after parsing.

Out of curiosity, recursion is not yet supported right?

@alecthomas
Copy link
Owner

Recursion into structs is supported, yes.

Repository owner locked and limited conversation to collaborators Feb 21, 2023
@alecthomas alecthomas converted this issue into discussion #297 Feb 21, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants