-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notes
27 lines (13 loc) · 921 Bytes
/
Notes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
If you put grammar clauses which match something or nothing at the beginning of a rule, e.g.:
ws = ([ ]+ | Nothing);
assignment = ws [a-z]+ ws '=' ws [0-9+];
Then given a string like
" x = 5;"
will have a match at position 0, and then again at the 'x'. This is inefficient, because it doubles the number of memo entries per assignment.
=> solution: put optional whitespace consuming matches at the end of the rule
...and actually that makes a lot of sense, since PEG parsing is about greedily consuming input from a fixed start position.
In fact any rule (X | Nothing) will always match in every position -- the question is just how much input is matched.
--
"Nothing" (or anything that can match 0 characters, e.g. (X | Nothing)) is inefficient if used in the first position, since it seeds a lot of parent clause evaluations.
It's more efficient to rewrite, e.g.:
(X | Nothing) Y => (X Y) | Y