$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); $Table = $NDA->export(); // Output classes // $commentData = array('author' => 'Tim Whitlock', 'category' => 'PLUG', 'package' => 'parsing', 'subpackage' => 'bnf', 'version' => '$Id' . '$'); @ob_end_flush(); ob_start(); $Lex->class_export('BNFLex', $commentData); file_put_contents('../BNFLex.php', "<?php\n" . ob_get_contents()); ob_clean(); $Grammar->class_export('BNFGrammar', $commentData); file_put_contents('../BNFGrammar.php', "<?php\n" . ob_get_contents()); ob_clean();
import('PLUG.parsing.bnf.*'); $lexname = 'JLexBase'; $grammarname = 'JGrammar'; $tablename = 'JParseTable'; $commentData = array('author' => 'Tim Whitlock', 'category' => 'PLUG', 'package' => 'JavaScript', 'version' => '$Id' . '$'); $Timer = new Timer(); $classdir = PLUG_HOST_DIR . '/PLUG/JavaScript'; $bnfpath = $classdir . '/dev/ecma_262.bnf'; $Tree = BNFParser::parse_file($bnfpath); // Make Lex object $Lex = $Tree->make_lex(); $Lex->dump(); echo "----------\n"; // Make Grammar object $rawgrammar = $Tree->evaluate(); $Grammar = GrammarBuilder::make($rawgrammar); // Load special rules $Grammar->exclude_terminal(J_EXPR_STATEMENT, '{'); $Grammar->exclude_terminal(J_EXPR_STATEMENT, J_FUNCTION); // trash dummy rules that we used just to get redundant terminals into the Lex $Grammar->remove_rules(J_RESERVED); $Grammar->remove_rules(J_IGNORE); $Grammar->dump($Lex); echo "----------\n"; printf("Ready to build automaton after %f milliseconds\n", $Timer->reset()); // Make Parse Table object $NDA = new LRNDA($Grammar, 1); printf("NDA built in %f milliseconds\n", $Timer->reset()); $Table = $NDA->export(); printf("Table exported in %f milliseconds\n", $Timer->reset()); /**