pharo-users@lists.pharo.org

Any question about pharo is welcome

View all threads

Help with PetitParser2 rules

RL
Rafael Luque
Fri, Jul 5, 2024 9:39 AM

Hi all,

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 of 'and' is a keyword"

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

Thank  you.

Hi all, 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 of 'and' is a keyword" I don't understand why the "words" rule works this way, but I'm starting with PetitParser. Any suggestions? Thank you.
V
vfclists
Sun, Jul 7, 2024 4:18 AM

On Fri, 5 Jul 2024 at 10:39, Rafael Luque rafael.luque.leiva@gmail.com
wrote:

Hi all,

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 of 'and' is a keyword"

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

Thank  you.

You might consider asking on stackoverflow.

A PetitParser2 question has been answered there recently.

Frank Church

=======================

On Fri, 5 Jul 2024 at 10:39, Rafael Luque <rafael.luque.leiva@gmail.com> wrote: > Hi all, > > 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 of 'and' is a keyword" > > I don't understand why the "words" rule works this way, but I'm starting > with PetitParser. Any suggestions? > > Thank you. > You might consider asking on stackoverflow. A PetitParser2 question has been answered there recently. -- Frank Church =======================
RL
Rafael Luque
Sun, Jul 7, 2024 4:16 PM

Thank you, Frank. I'll try your suggestion.

El dom, 7 jul 2024 a las 6:19, vfclists (vfclists@gmail.com) escribió:

On Fri, 5 Jul 2024 at 10:39, Rafael Luque rafael.luque.leiva@gmail.com
wrote:

Hi all,

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 of 'and' is a keyword"

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

Thank  you.

You might consider asking on stackoverflow.

A PetitParser2 question has been answered there recently.

Frank Church

=======================

Thank you, Frank. I'll try your suggestion. El dom, 7 jul 2024 a las 6:19, vfclists (<vfclists@gmail.com>) escribió: > > > On Fri, 5 Jul 2024 at 10:39, Rafael Luque <rafael.luque.leiva@gmail.com> > wrote: > >> Hi all, >> >> 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 of 'and' is a keyword" >> >> I don't understand why the "words" rule works this way, but I'm starting >> with PetitParser. Any suggestions? >> >> Thank you. >> > > You might consider asking on stackoverflow. > > A PetitParser2 question has been answered there recently. > -- > Frank Church > > ======================= > >