/** * parse input string * @param string input source * @return ParseNode */ static function parse_string($s) { if ($s == '') { throw new Exception('Cannot parse empty string'); } // instantiate self $Parser = new BNFParser(); // perform external tokenizing on string input $tokens = bnf_tokenize($s); return $Parser->parse($tokens); }
/** * Build JParser */ require dirname($argv[0]) . '/../../plugcli.php'; import('PLUG.time.Timer'); import('PLUG.parsing.*'); import('PLUG.parsing.LR.*'); 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";