- Parser Grammar Engine
The Parser Grammar Engine (originally Parrot Grammar Engine) or PGE is a
compiler andruntime for aPerl 6 rules for theParrot virtual machine . [cite web | url=http://search.cpan.org/~ltoetsch/parrot-0.2.2/compilers/pge/README | title=Parrot Grammar Engine (PGE) | author=Michaud, Patrick R. | date=2004-11-22] PGE uses these "rules" to convert aparsing expression grammar into Parrotbytecode . It is therefore compiling rules into a program, unlike most virtual machines and runtimes, which store regular expressions in a secondary internal format that is then interpreted at runtime by a regular expression engine. The rules format used by PGE can express anyregular expression and mostformal grammar s, and as such it forms the first link in the compiler chain for all of Parrot's front-end languages.When executed, the bytecode generated by PGE will parse text as described in the input rules, generating a parse tree. The parse tree can be manipulated directly, or fed into the next stage of the
Parrot compiler toolchain in order to generate an AST from which code generation can occur (if the grammar describes a programming language).History
Originally named "P6GE" and written in C, PGE was translated to native Parrot and re-named not long after its initial release in November 2004. Its author is Patrick R. Michaud. [cite web | url=http://www.nntp.perl.org/group/perl.perl6.compiler/96 | title=First public release of grammar engine | author=Michaud, Patrick R. | date=2004-11-08] PGE was written in order to reduce the amount of work required to implement a compiler on top of Parrot. It was also written to allow Perl 6 to easily self-host, though current
Pugs development no longer uses PGE as its primary rules back-end in favor of a native engine called PCR. [cite web | url=http://pugs.blogs.com/pugs/2006/09/pcr_replaces_pg.html | title=PCR replaces PGE in Pugs | author="Agent Zhang" | date=2006-09-17]Internals
PGE combines three styles of parsing:
* Perl 6 rules
* an operator precedence parser
* custom parse subroutinesThe primary form is Perl 6 rules, so a PGE rule might look like this for an addition-only grammar: rule term {| ( ) } rule number { d+ } rule expr { ( '+' )* }The operator precedence parser allows an operator table to be built and used directly in a Perl 6 rule style parser like so: rule expr is optable { ... } rule term { | ( ) } rule number { d+ } proto term: is precedence('=') is parsed(&term) {...} proto infix:+ is looser('term:') {...}This accomplishes the same goal of defining a simple, addition-only grammar, but does so using a combination of a Perl 6 style regex/rules for term
andnumber
and a shift-reduce optable for everything else.Code generation
Though PGE outputs code which will parse the grammar described by a rule, and can be used at
runtime to handle simple grammars and regular expressions found in code, its primary purpose is for the parsing ofhigh level language s.The Parrot compiler toolchain is broken into several parts, of which PGE is the first. PGE converts source code to
parse tree s. TheTree Grammar Engine (TGE) then converts these into Parrot Abstract Syntax Trees (PAST). A second TGE pass then converts a PAST intoParrot Opcode Syntax Trees (POST) which can be directly transformed into executable bytecode.References
External links
*cite web | url=http://www.pmichaud.com/2006/pres/yapc-parsers/start.html | title=Parsers, Perl 6 Rules, and the Parrot Grammar Engine | date=2006-06-28
*
Wikimedia Foundation. 2010.