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

[ab]* 'a' couldn't derive 'aaa' #105

Closed
curvedmark opened this issue Sep 11, 2012 · 7 comments
Closed

[ab]* 'a' couldn't derive 'aaa' #105

curvedmark opened this issue Sep 11, 2012 · 7 comments

Comments

@curvedmark
Copy link

I don't really know how pegjs works, but I guess the reason might be that the three 'a's are matched first and when EOF is encountered, the last 'a' in the grammar is unmatched and thus causing the failure?

If trying alternatives is expensive and won't be implemented, is there any way to workaround it when such grammar is needed?

@michaelficarra
Copy link

Related: #57.

'a'? ('b'* 'a')+

@curvedmark
Copy link
Author

The grammar actually means greedy matching in my case.

pegjs will have to try alternative matches in order to derive the input, much like how regexp works: when [ab]* has consumed the whole string and the rule doesn't match, it should reduce the consumption and try again, which might be expensive.

@michaelficarra
Copy link

@gravof: or you can rewrite it like I showed in my first comment.

@curvedmark
Copy link
Author

Oh, thanks. went directly to the related issue, didn't pay much attention to the grammar you suggested.

It works. However, will nested repetition cause trouble? After seeing #26, I try to stay away from such pattern.

@curvedmark
Copy link
Author

Probably related:

start
  = 'a'? 'a'

couldn't derive a

@prophile
Copy link

Greedy matching is how PEGs work. They are not regexes. :)

@dmajda
Copy link
Contributor

dmajda commented Oct 31, 2012

Contrary to regular expressions, * and + modifiers in PEGs do not backtrack and just match greedily. This is how it is defined in the formalism. I do not plan to deviate from this behavior in PEG.js, nor do I think about introducing a special syntax for backtracking variants.

Closing this issue since the described behavior is not a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants