예제 #1
0
#!/usr/bin/php
<?php 
require dirname(__FILE__) . '/../../../plugcli.php';
import('PLUG.parsing.LexBuilder');
import('PLUG.parsing.GrammarBuilder');
import('PLUG.parsing.LR.*');
// Manually construct the BNF Lexicon
//
$Lex = new LexBuilder();
// non terminals
$Lex->define('BNF_RULES');
$Lex->define('BNF_RULE');
$Lex->define('BNF_EMPTY_RULE');
$Lex->define('BNF_EXPRESSION');
$Lex->define('BNF_LIST');
$Lex->define('BNF_TERM');
// terminals
$Lex->define('BNF_RULE_END');
$Lex->define('BNF_TEXT');
$Lex->define('BNF_LITERAL');
$Lex->define_literal(':');
$Lex->define_literal('<');
$Lex->define_literal('>');
$Lex->define_literal('|');
// Manually construct the BNF Grammar in its raw form
//
$raw = array(BNF_RULES => array(array(BNF_RULES, BNF_RULE), array(BNF_RULE), array(BNF_RULES, BNF_EMPTY_RULE), array(BNF_EMPTY_RULE)), BNF_RULE => array(array('<', BNF_TEXT, '>', ':', BNF_EXPRESSION, BNF_RULE_END)), BNF_EMPTY_RULE => array(array(BNF_RULE_END)), BNF_EXPRESSION => array(array(BNF_LIST), array(BNF_EXPRESSION, '|', BNF_LIST)), BNF_LIST => array(array(BNF_TERM), array(BNF_LIST, BNF_TERM)), BNF_TERM => array(array(BNF_TEXT), array(BNF_LITERAL), array('<', BNF_TEXT, '>')));
$Grammar = GrammarBuilder::make($raw);
// Construct LR(1) Parse Table
//
$NDA = new LRNDA($Grammar, 1);
예제 #2
0
 /**
  * Create a Lex instance fro symbols
  * @param int Lowest value for new token constant definitions, defaults to 0 
  */
 function make_lex($i = 0)
 {
     $Lex = new LexBuilder($i);
     foreach ($this->collect_symbols() as $symbols) {
         foreach ($symbols as $t => $s) {
             if (preg_match('/^\\W/', $s, $r)) {
                 $Lex->define_literal((string) $t);
             } else {
                 if ($Lex->defined($s)) {
                 } else {
                     if (defined($s)) {
                         $Lex->redefine($s);
                     } else {
                         $Lex->define($t);
                     }
                 }
             }
         }
     }
     return $Lex;
 }