@venezia is on PowPing!

PowPing is a place where you can earn Bitcoin simply by socializing, for FREE.
Never tried Bitcoin? It's OK! Just come, socialize, and earn Bitcoin.
Check out venezia's activities
Total Economy: 0.03 USD

Play with PEG.js

PEG.js is a great tool for parsers.

I used it a few days ago to write a parser that can parse the MiniForth language. While using it today, I found a bug. The parser I wrote cannot distinguish between numbers and words that start with numbers.

For example, words like "2dup" will be parsed into "2" and "dup". I need a way to describe "can contain numbers or letters, and must contain at least one letter".

This requirement can be achieved with regular expressions. But PEG.js cannot use all regular expressions. On the contrary, it recommends us to use "*" "+" "!" "?" These symbols to mean "repeat 0~n times", "repeat 1~n times", "not exist", and "may exist".

So, first I defined the numbers and letters separately.

digit =

letter =

Then I realized that "can contain numbers or letters, and must contain at least one letter" can be converted into "First there may be any numbers at the beginning, then there must be a letter, and then it may be any number or letter". So I solved the problem with this statement:

word =
    ${digit* letter (digit / letter)*}

The ${} will join all the chars into a string.

powered by powpress
link Tip
musiq tipped:
0.02 USD
1 year ago
adonsats tipped:
0.01 USD
1 year ago
Is there a reason you use "/" instead of "|"? The "|" feels closer to regular expression. I first thought you were dividing digit by letter. Also, how about using $ prefix for when you're using variables? maybe like: word = $digit* $letter ($digit|$letter)*
venezia replied:
Make sense. It's just the syntax of peg.js. Here has more info https://pegjs.org/documentation
musiq tipped:
0.02 USD
1 year ago
fix: The ${} in last paragraph should be $() .