1

I'm struggling with a small parser using PetitParser2. Here is a fragment of some of the rules I'm defining but their behaviour is not as I could expect:

andKeyword := 'and' asPParser trim, #letter asPParser not. notKeyword := 'not' asPParser trim, #letter asPParser not. keyword := andKeyword / notKeyword. nonKeywordIdentifier := keyword not, #letter asPParser, #word asPParser star. word := nonKeywordIdentifier, #blank asPParser star. words := word star. 

The behaviour of the "nonKeywordIdentifier" is the expected:

identifier end parse: 'hello'. "It parses successfully" identifier end parse: 'and'. "It fails as 'and' is a keyword" 

However the "words" rule's behavior is the following:

words end parse: 'hello Pharo'. "It parses successfully" words end parse: 'hello and bye'. "It parses, when I would expect it fails because 'and' is a keyword" 

I don't understand why the "words" rule works this way, but I'm starting with PetitParser. Any suggestions?

PS: Sorry for the cross-posting in the Pharo users mailing list.

Thank you.

1 Answer 1

0

words end parse: 'hello and bye'. "It parses, when I would expect it fails because 'and' is a keyword"

nonKeywordIdentifier := keyword not, #letter asPParser, #word asPParser star. word := nonKeywordIdentifier, #blank asPParser star. words := word star. 

It parses because you have created a loop. word calling nonKeywordIdentifier and the other way around. Even thou you have only #letter asPParser, but that will be matched #word asPParser star times (notice the star at the end).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.