Пример #1
0
 protected function createBlock()
 {
     $parser = PEG::ref($this->blockRef);
     return $parser;
 }
Пример #2
0
 * メモ化の有無での処理時間の差を見るサンプル
 * ここではメモ化するのとしないのとでは著しく違いが出る文法規則を元にパーサを組み立てている
 */
$t = new Benchmark_Timer();
$str = '((((((((1))))))))';
$t->start();
// メモ化していないパーサ
$a = PEG::ref($a_ref);
$p = PEG::ref($p_ref);
$a_ref = PEG::choice(PEG::seq($p, '+', $a), PEG::seq($p, '-', $a), $p);
$p_ref = PEG::choice(PEG::seq('(', $a, ')'), '1');
$a->parse(PEG::context($str));
$t->setMarker('no memoize');
// メモ化しているパーサ
$a = PEG::ref($a_ref);
$p = PEG::ref($p_ref);
$a_ref = PEG::memo(PEG::choice(PEG::seq($p, '+', $a), PEG::seq($p, '-', $a), $p));
$p_ref = PEG::memo(PEG::choice(PEG::seq('(', $a, ')'), '1'));
$a->parse($c = PEG::context($str));
$t->setMarker('memoize');
$t->stop();
$t->display();
/* 結果
---------------------------------------------------------
marker       time index            ex time         perct   
---------------------------------------------------------
Start        1242400475.10093900   -                0.00%
---------------------------------------------------------
no memoize   1242400476.30805000   1.207111        99.74%
---------------------------------------------------------
memoize      1242400476.31115500   0.003105         0.26%
Пример #3
0
<?php

include_once dirname(__FILE__) . '/t/t.php';
$t = new lime_test();
$p = PEG::seq('a', 'a');
$c = PEG::context(array('a', 'a'));
$t->is($p->parse($c), array('a', 'a'));
$p = PEG::seq('(', PEG::choice(PEG::ref($ref), PEG::anything()), ')');
$ref = $p;
$c = PEG::context(array('(', 3, ')'));
$t->is($p->parse($c), array('(', 3, ')'));
$c = PEG::context(array('(', '(', 3, ')', ')'));
$t->is($p->parse($c), array('(', array('(', 3, ')'), ')'));
Пример #4
0
<?php

include_once dirname(__FILE__) . '/t/t.php';
$t = new lime_test();
$p = PEG::ref($p_ref);
$p_ref = PEG::token('hoge');
$t->is($p->parse(PEG::context('hoge')), 'hoge');
$p = PEG::ref($_);
$ref =& $p->getRef();
$ref = PEG::token('hoge');
$t->is($p->parse(PEG::context('hoge')), 'hoge');
$p = PEG::ref($_);
$p->is(PEG::token('hoge'));
$t->is($p->parse(PEG::context('hoge')), 'hoge');
Пример #5
0
<?php

include_once dirname(__FILE__) . '/../code/PEG.php';
/**
 * 括弧の対応をとる再帰的なパーサのサンプル。
 * 認識した括弧を用いて文字列を階層化する。
 * 
 * パーサのEBNFはこんな感じ
 * item       := paren | anything
 * paren_item := (?! ")") item
 * paren      := "(" paren_item* ")"
 * parser     := item*
 */
$paren = PEG::ref($paren_ref);
$item = PEG::choice($paren, PEG::anything());
$paren_item = PEG::andalso(PEG::not(')'), $item);
$paren_ref = PEG::pack('(', PEG::many($paren_item), ')');
$parser = PEG::many($item);
$str = 'abc(def(ghi)(jkl(mno)))pq';
var_dump($parser->parse(PEG::context($str)));
/* 結果
array(6) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  array(5) {
    [0]=>