kach / nearley

📜🔜🌲 Simple, fast, powerful parser toolkit for JavaScript.
https://nearley.js.org
MIT License
3.57k stars 232 forks source link

Can I specify precedence? #508

Open mspoulsen opened 4 years ago

mspoulsen commented 4 years ago

Say I have an ambiguous grammar like:

main -> (ab | a | b):*
ab -> "ab"
a -> "a"
b -> "b"

Here "ab" can be interpreted as both "ab" or an "a" followed by a "b".

Is there a way to specify precedence in those situations? For example tell nearley that it should prefer "ab" over "a" followed by "b".

Or do I need to use Moo for that?

Thanks!

kach commented 4 years ago

This is a situation where you should use moo to tokenize and disambiguate.