首页 最近更新 网站讯息 页面索引 这是什么?

PgeSimpleTutorial

内容

  1. Overview
  2. First Simple Calculator
    1. Simple Calculator Grammar
    2. Generate SC Grammar Parser
    3. main
    4. Demo
  3. Second Simple Calculator
    1. Grammar
    2. Makefile
    3. Demo
  4. Third Simple Calculator
    1. Grammar
    2. Demo
  5. Reference

Overview

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.

First Simple Calculator

Simple Calculator Grammar

# PGE grammar for simple calculator
grammar SC::Grammar;

token TOP { <expression> }

rule expression { <term> <addop> <term> }

token term { \d+ }
token addop { '+' | '-' }

Generate SC Grammar Parser

  $ parrot runtime/parrot/library/PGE/Perl6Grammar.pir sc.pg >sc-grammar.pir

main

.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'

Demo

  $ 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
          ]
      }
  }

Second Simple Calculator

Grammar

grammar SC::Grammar;

token TOP { <expression> }

rule expression { <term> [ <addop> <term> ]* }

token term { \d+ }
token addop { '+' | '-' }

Makefile

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 $@ $<

Demo

$ 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
        ]
    }
}

Third Simple Calculator

Grammar

grammar SC::Grammar;

token TOP { <expression> }

rule expression { <term> <postterm>* }
rule postterm { <addop> <term> }

token term { \d+ }
token addop { '+' | '-' }

Demo

$ 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
            }
        ]
    }
}

Reference

  1. Parrot Compiler Tools 2007, by Patrick R. Michaud
  2. Parsers, Perl 6 Rules, and the Parrot Grammar Engine 2006, by Patrick Michaud