File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed
misc/perl6advent-2010/articles Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change
1
+ =head1 Operator precedence
2
+
3
+ Today we get a wondrous gift: operator precedence parsing. L<Last
4
+ year|http://perl6advent.wordpress.com/2009/12/21/day-21-grammars-and-actions/>
5
+ we got parsing, and OPP makes grammars even better.
6
+
7
+ Ordinarily, when you call a subrule in your grammar, Perl 6 will find a match
8
+ in a I<top-down> manner. That is, it will recursively try to match the various
9
+ regexes for that rule, until it finds literals to match against the string.
10
+ Most of the time this is an intuitive way for a parser to work, but sometimes
11
+ it makes things harder; the classical example is parsing mathematical
12
+ expressions. For extra credit, try writing a grammar that gets the precedence
13
+ right in all cases without using operator precedence. It's not as easy as you
14
+ think.
15
+
16
+ This is obviously not a fun, or productive, way to solve this problem. A
17
+ better way is to use operator precedence. There is a special rule, C<EXPR>,
18
+ which invokes Perl 6's operator precedence parser. This rule reads in
19
+ operators and terms, and finds the right parse in an efficient manner.
20
+
21
+ # OPP four-function grammar goes here.
You can’t perform that action at this time.
0 commit comments