I'm finally getting around to sending this patch as requested on IRC...
dang commit bits are rare around here ;)
This contains the Makefile, README, .pg grammar, a -harness.pir that
executes the parser on a sample string and dumps the parse tree and a
-stress.pir that runs 50,000 trial runs to see how fast PGE is (not too
shabby is the answer, as it comes in at about 1/2 the time of a P::RD
version for the simple example, and gets a bigger lead the more complex
the input expression).
What I didn't understand was why -j didn't run any faster than -b in my
trials... that seemed odd.
Index: examples/pge/wikipedia/wptest-harness.pir
===================================================================
--- examples/pge/wikipedia/wptest-harness.pir (revision 0)
+++ examples/pge/wikipedia/wptest-harness.pir (revision 0)
@@ -0,0 +1,44 @@
+# This is a simple invoker for the example parser whose grammar is
+# used in the Wikipedia article:
+# http://en.wikipedia.org/wiki/Parser_Grammar_Engine
+#
+# This simply runs the parser against a static expression.
+.sub main :main
+ load_bytecode 'dumper.pir'
+ load_bytecode '../../../compilers/pge/PGE.pir'
+ load_bytecode 'PGE/Dumper.pir'
+ load_bytecode 'wptest.pir'
+
+ .local pmc match
+ .local pmc code
+ .local pmc start_rule
+ code = new .String
+ code = '1+(1+1)' # target string
+ print "Parsing simple expression: "
+ print code
+ print "\n"
+ # Find the "expr" node in the parser:
+ start_rule = get_root_global [ 'parrot'; 'Wikipedia::Test'], 'expr'
+ if start_rule goto start_rule_ok
+ printerr "Cannot find valid start rule in Wikipedia::Test"
+ exit 1
+
+ # And execute it on our text:
+ start_rule_ok:
+ match = start_rule(code)
+
+ # Now dump the results
+ say "Match results begin:"
+ match_loop:
+ unless match goto match_done
+ _dumper(match)
+ match = match.'next'() # find the next match
+ goto match_loop
+
+ match_done:
+ say "match complete"
+
+ end
+
+.end
+
Index: examples/pge/wikipedia/wptest-stress.pir
===================================================================
--- examples/pge/wikipedia/wptest-stress.pir (revision 0)
+++ examples/pge/wikipedia/wptest-stress.pir (revision 0)
@@ -0,0 +1,55 @@
+# This is a simple invoker for the example parser whose grammar is
+# used in the Wikipedia article:
+# http://en.wikipedia.org/wiki/Parser_Grammar_Engine
+#
+# This example has been turned into a stress-test to measure
+# parsing speed.
+.sub main :main
+ load_bytecode 'dumper.pir'
+ load_bytecode '../../../compilers/pge/PGE.pir'
+ load_bytecode 'PGE/Dumper.pir'
+ load_bytecode 'wptest.pir'
+
+ .local pmc match
+ .local pmc code
+ .local pmc start_rule
+ .local int loops
+ .local int i
+ loops = 50000
+ code = new .String
+ code = '1+(1+1+1+1+(1+1+1))+1+1' # target string
+ print "Parsing simple expression: '"
+ print code
+ print "' "
+ print loops
+ print "times\n"
+ i = 0
+ loop_start:
+ if i >= loops goto loop_end
+ inc i
+ # Find the "expr" node in the parser:
+ start_rule = get_root_global [ 'parrot'; 'Wikipedia::Test'], 'expr'
+ if start_rule goto start_rule_ok
+ printerr "Cannot find valid start rule in Wikipedia::Test"
+ exit 1
+
+ # And execute it on our text:
+ start_rule_ok:
+ match = start_rule(code)
+
+ unless match goto match_fail
+ goto loop_start
+
+ loop_end:
+ say "Looping complete"
+ _dumper(match)
+ exit 0
+
+ match_fail:
+ print "Failed to match simple grammar\n"
+ exit 1
+
+ end
+
+.end
+
Index: examples/pge/wikipedia/wptest.pg
===================================================================
--- examples/pge/wikipedia/wptest.pg (revision 0)
+++ examples/pge/wikipedia/wptest.pg (revision 0)
@@ -0,0 +1,12 @@
+#
+# A simple test grammar that is used in a Wikipedia article about
+# PGE. See wptest-harness.pir for more detail.
+#
+grammar Wikipedia::Test
+
+rule expr is optable { ... }
+rule term { <number> | \( <expr> \) }
+rule number { \d+ }
+proto term: is precedence('=')
+ is parsed(&term) {...}
+proto infix:+ is looser('term:') {...}
Index: examples/pge/wikipedia/Makefile
===================================================================
--- examples/pge/wikipedia/Makefile (revision 0)
+++ examples/pge/wikipedia/Makefile (revision 0)
@@ -0,0 +1,13 @@
+PARROT=../../../parrot
+PGC=../../../compilers/pge/pgc.pir
+RM=rm -f
+PIR=wptest.pir
+
+all: $(PIR)
+
+$(PIR): wptest.pg
+ $(PARROT) -o $@ $(PGC) $< || $(RM) $@
+
+clean::
+ $(RM) $(PIR)
+
Index: examples/pge/wikipedia/README
===================================================================
--- examples/pge/wikipedia/README (revision 0)
+++ examples/pge/wikipedia/README (revision 0)
@@ -0,0 +1,13 @@
+This directory contains a very crude example parser that reads a fixed string
+"1+(1+1)" and produces a parse tree. The parser itself is used as an example
+in the Wikipedia article:
+
+ http://en.wikipedia.org/wiki/Parser_Grammar_Engine
+
+This code is here so that others can benefit from a simple example,
+and so that anyone who updates PGE can see if it affects the ability
+to handle the example given in that article.
+
+Written in 2006 by Aaron Sherman, and distrbuted under the same terms
+as the rest of the Parrot distribution that this should have come
+with.