alecthomas / participle

A parser library for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parser being greedy

lmrcarneiro opened this issue · comments

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

Participle is greedy yes.

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?

Recursion into structs is supported, yes.