Content deleted Content added
→Advantages: Actually write about advantages (as opposed to underhandedly warn about memory requirements, which is covered in another section) |
→Indirect left recursion: Sum and Product should have + instead of * for repetition. With that change, it became obvious that the ordered choice should start with the lowest priority, not the highest. |
||
Line 190:
A PEG is called ''well-formed''<ref name="For04"/> if it contains no ''[[left recursion|left-recursive]]'' rules, i.e., rules that allow a nonterminal to expand to an expression in which the same nonterminal occurs as the leftmost symbol. For a left-to-right top-down parser, such rules cause infinite regress: parsing will continually expand the same nonterminal without moving forward in the string.
Therefore, to allow packrat parsing, left recursion must be eliminated. For example, in the arithmetic grammar above, it could seem tempting to express operator precedence as a matter of ordered choice — <code>
<syntaxhighlight lang="peg">
Value ← [0-9.]+ / '(' Expr ')'
Product ← Expr (('*' / '/') Expr)
Sum ← Expr (('+' / '-') Expr)
Expr ←
</syntaxhighlight>
Unfortunately matching an <code>Expr</code>
<syntaxhighlight lang="peg">
string-of-a ← string-of-a 'a' | 'a'
|