How to convert a simple grammar to something that works in PEG.js (expected "a", but "a" found)

I just started playing with PEG.js and have a grammar problem (greatly simplified for debugging):

start = presingle single / preplural plural presingle = "a" / "b" preplural = "b" / "c" single = "d" / "e" plural = "dd" / "ee" 

I am using http://pegjs.majda.cz/online

This grammar cannot bdd .

 Line 1, column 3: Expected "a" but "d" found. 

Is this what PEGs cannot do, or can I turn my grammar into something that will analyze it?

PS If I try to parse (erroneously recommended?) bda , I get a meaningless error:

 Line 1, column 3: Expected "a" but "a" found. 
+1
source share
2 answers

This grammar only changes the order of the articles at the beginning and works for bdd

 start = preplural plural / presingle single presingle = "a" / "b" preplural = "b" / "c" single = "d" / "e" plural = "dd" / "ee" 

and for bda it shows row 1, column 3: the expected "dd" or "ee", but "a" is found.

+1
source

The good news is that in modern versions of pegjs an error message appears: "Row 1, column 3: expected end of input, but" d "found." when setting input bdd.

This is what you would expect, since it matches the only match in the first place, and since the coincidence of “b” and “d” suggests that it is good. Inverting the order, as suggested by @HBP, forces it to match plurals first, and only if they don't exist, try a single that gives the expected result.

0
source

Source: https://habr.com/ru/post/972297/


All Articles