PGE is a parser generator. It's part of parrot.
This tutorial shows three grammars for simple calculator and parsers generated from them. Parsers generated from he second grammar and the third one can parse more complex expression than the first one. And the output of the third one is more structural than the second one.
# PGE grammar for simple calculator grammar SC::Grammar; token TOP { <expression> } rule expression { <term> <addop> <term> } token term { \d+ } token addop { '+' | '-' }
$ parrot runtime/parrot/library/PGE/Perl6Grammar.pir sc.pg >sc-grammar.pir
.namespace [ 'SC' ] .sub '__onload' :load :init load_bytecode 'PGE.pbc' load_bytecode 'Parrot/HLLCompiler.pbc' $P0 = new [ 'HLLCompiler' ] $P0.'language'('SC') $P0.'parsegrammar'('SC::Grammar') .end .sub 'main' :main .param pmc args $P0 = compreg 'SC' .return $P0.'command_line'(args) .end .include 'sc-grammar.pir'
$ parrot -o main.pbc main.pir $ parrot main.pbc --target=parse > 2+3 "parse" => PMC 'SC::Grammar' => "2+3\n" @ 0 { <expression> => PMC 'SC::Grammar' => "2+3\n" @ 0 { <term> => ResizablePMCArray (size:2) [ PMC 'SC::Grammar' => "2" @ 0, PMC 'SC::Grammar' => "3" @ 2 ] <addop> => ResizablePMCArray (size:1) [ PMC 'SC::Grammar' => "+" @ 1 ] } }
grammar SC::Grammar; token TOP { <expression> } rule expression { <term> [ <addop> <term> ]* } token term { \d+ } token addop { '+' | '-' }
PARROT=$(HOME)/tasks/parrot/parrot PGE=$(PARROT) $(HOME)/tasks/parrot/runtime/parrot/library/PGE/Perl6Grammar.pir .PHONY: all all: main.pbc main.pbc: main.pir sc-grammar.pir $(PARROT) -o $@ $< sc-grammar.pir: sc.pg $(PGE) -o $@ $<
$ make $ parrot main.pbc --target=parse > 2+3-2 "parse" => PMC 'SC::Grammar' => "2+3-2\n" @ 0 { <expression> => PMC 'SC::Grammar' => "2+3-2\n" @ 0 { <term> => ResizablePMCArray (size:3) [ PMC 'SC::Grammar' => "2" @ 0, PMC 'SC::Grammar' => "3" @ 2, PMC 'SC::Grammar' => "2" @ 4 ] <addop> => ResizablePMCArray (size:2) [ PMC 'SC::Grammar' => "+" @ 1, PMC 'SC::Grammar' => "-" @ 3 ] } }
grammar SC::Grammar; token TOP { <expression> } rule expression { <term> <postterm>* } rule postterm { <addop> <term> } token term { \d+ } token addop { '+' | '-' }
$ make $ parrot main.pbc --target=parse > 2+3-4 "parse" => PMC 'SC::Grammar' => "2+3-4\n" @ 0 { <expression> => PMC 'SC::Grammar' => "2+3-4\n" @ 0 { <term> => PMC 'SC::Grammar' => "2" @ 0 <postterm> => ResizablePMCArray (size:2) [ PMC 'SC::Grammar' => "+3" @ 1 { <addop> => PMC 'SC::Grammar' => "+" @ 1 <term> => PMC 'SC::Grammar' => "3" @ 2 }, PMC 'SC::Grammar' => "-4\n" @ 3 { <addop> => PMC 'SC::Grammar' => "-" @ 3 <term> => PMC 'SC::Grammar' => "4" @ 4 } ] } }