Source for file Config.php
Documentation is available at Config.php
* PHP_ParserGenerator, a php 5 parser generator.
* This is a direct port of the Lemon parser generator, found at
* {@link http://www.hwaci.com/sw/lemon/}
* Copyright (c) 2006, Gregory Beaver <cellog@php.net>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
* * Neither the name of the PHP_ParserGenerator nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* @package PHP_ParserGenerator
* @author Gregory Beaver <cellog@php.net>
* @copyright 2006 Gregory Beaver
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @since File available since Release 0.1.0
/** A configuration is a production rule of the grammar together with
* a mark (dot) showing how much of that rule has been processed so far.
* Configurations also contain a follow-set which is a list of terminal
* symbols which are allowed to immediately follow the end of the rule.
* Every configuration is recorded as an instance of the following class.
* @package PHP_ParserGenerator
* @author Gregory Beaver <cellog@php.net>
* @copyright 2006 Gregory Beaver
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version @package_version@
* @since Class available since Release 0.1.0
* The parser rule upon with the configuration is based.
* A parser rule is something like:
* @var PHP_ParserGenerator_Rule
* This is the index into the right-hand side of a rule that is
* represented by this configuration. In other words, possible
* are (represented by "[here]"):
* blah ::= [here] FOO bar.
* blah ::= FOO [here] bar.
* blah ::= FOO bar [here].
* Follow-set for this configuration only
* This is the list of terminals and non-terminals that
* can follow this configuration.
* Follow-set forward propagation links.
* @var PHP_ParserGenerator_PropagationLink
* Follow-set backwards propagation links
* @var PHP_ParserGenerator_PropagationLink
* State that contains this configuration
* @var PHP_ParserGenerator_State
COMPLETE, /* The status is used during followset and
INCOMPLETE /* shift computations
* Status during followset and shift computations.
* One of PHP_ParserGenerator_Config::COMPLETE or
* PHP_ParserGenerator_Config::INCOMPLETE.
* Next configuration in the state.
* Index of next PHP_ParserGenerator_Config object.
* Index of the next basis configuration PHP_ParserGenerator_Config object
* Top of the list of configurations for the current state.
* @var PHP_ParserGenerator_Config
* Last on the list of configurations for the current state.
* @var PHP_ParserGenerator_Config
* Top of the list of basis configurations for the current state.
* @var PHP_ParserGenerator_Config
* Last on the list of basis configurations for the current state.
* @var PHP_ParserGenerator_Config
* Associative array representation of the linked list of configurations
* found in {@link $current}
static public $x4a = array();
* Return a pointer to a new configuration
* @return PHP_ParserGenerator_Config
private static function newconfig()
* Display the current configuration for the .out file
* @param PHP_ParserGenerator_Config $cfp
* @see PHP_ParserGenerator_Data::ReportOutput()
static function Configshow(PHP_ParserGenerator_Config $cfp)
$fp = fopen('php://output', 'w');
if ($cfp->dot == $cfp->rp->nrhs) {
$buf = sprintf('(%d)', $cfp->rp->index);
//SetPrint(fp,cfp->fws,$this);
//PlinkPrint(fp,cfp->fplp,"To ");
//PlinkPrint(fp,cfp->bplp,"From");
* Initialize the configuration list builder for a new state.
self::$currentend = &self::$current;
self::$basisend = &self::$basis;
* Remove all data from the table.
* Pass each data to the function $f as it is removed if
* $f is a valid callback.
* @see Configtable_clear()
self::$currentend = &self::$current;
self::$basisend = &self::$basis;
self::Configtable_clear(0);
* Remove all data from the associative array representation
* Pass each data to the function $f as it is removed if
* $f is a valid callback.
if (!count(self::$x4a)) {
for ($i = 0; $i < count(self::$x4a); $i++ ) {
call_user_func($f, self::$x4a[$i]->data);
* Reset the configuration list builder for a new state.
* @see Configtable_clear()
self::Configtable_clear(0);
* Add another configuration to the configuration list for this parser state.
* @param PHP_ParserGenerator_Rule the rule
* @param int Index into the right-hand side of the rule where the dot goes
* @return PHP_ParserGenerator_Config
$cfp = self::Configtable_find($model);
$cfp = self::newconfig();
$cfp->fplp = $cfp->bplp = 0;
self::$currentend = $cfp;
self::$currentend = &$cfp->next;
self::Configtable_insert($cfp);
* Add a basis configuration to the configuration list for this parser state.
* Basis configurations are the root for a configuration. This method also
* inserts the configuration into the regular list of configurations for this
* @param PHP_ParserGenerator_Rule the rule
* @param int Index into the right-hand side of the rule where the dot goes
* @return PHP_ParserGenerator_Config
$cfp = self::Configtable_find($model);
$cfp = self::newconfig();
$cfp->fplp = $cfp->bplp = 0;
self::$currentend = $cfp;
self::$currentend = &$cfp->next;
self::$basisend = &$cfp->bp;
self::Configtable_insert($cfp);
* Compute the closure of the configuration list.
* This calculates all of the possible continuations of
* each configuration, ensuring that each state accounts
* for every configuration that could arrive at that state.
for ($cfp = self::$current; $cfp; $cfp = $cfp->next) {
if ($sp->type == PHP_ParserGenerator_Symbol::NONTERMINAL) {
if ($sp->rule === 0 && $sp !== $lemp->errsym) {
"Nonterminal \"%s\" has no rules.", $sp->name);
for ($newrp = $sp->rule; $newrp; $newrp = $newrp->nextlhs) {
$newcfp = self::Configlist_add($newrp, 0);
for ($i = $dot + 1; $i < $rp->nrhs; $i++ ) {
$newcfp->fws[$xsp->index] = 1;
for ($k = 0; $k < $xsp->nsubsym; $k++ ) {
$newcfp->fws[$xsp->subsym[$k]->index] = 1;
if ($xsp->lambda === false) {
* Sort the configuration list
//self::Configshow(self::$current);
self::$current = PHP_ParserGenerator::msort(self::$current,'next', array('PHP_ParserGenerator_Config', 'Configcmp'));
//self::Configshow(self::$current);
* Sort the configuration list
self::$basis = PHP_ParserGenerator::msort(self::$current,'bp', array('PHP_ParserGenerator_Config', 'Configcmp'));
* Return a pointer to the head of the configuration list and
* @return PHP_ParserGenerator_Config
self::$currentend = &self::$current;
* Return a pointer to the head of the basis list and
* @return PHP_ParserGenerator_Config
self::$basisend = &self::$basis;
* Free all elements of the given configuration list
* @param PHP_ParserGenerator_Config
for(; $cfp; $cfp = $nextcfp){
throw new Exception('fplp of configuration non-zero?');
throw new Exception('bplp of configuration non-zero?');
* Compare two configurations for sorting purposes.
* Configurations based on higher precedence rules
* (those earlier in the file) are chosen first. Two
* configurations that are the same rule are sorted by
* dot (see {@link $dot}), and those configurations
* with a dot closer to the left-hand side are chosen first.
$x = $a->rp->index - $b->rp->index;
* Print out information on this configuration.
* @see PHP_ParserGenerator_Data::ReportOutput()
fprintf($fp, "%s ::=", $rp->lhs->name);
for ($i = 0; $i <= $rp->nrhs; $i++ ) {
for ($j = 1; $j < $sp->nsubsym; $j++ ) {
fprintf($fp, '|%s', $sp->subsym[$j]->name);
* Hash a configuration for the associative array {@link $x4a}
private static function confighash(PHP_ParserGenerator_Config $a)
$h = $h * 571 + $a->rp->index * 37 + $a->dot;
* Insert a new record into the array. Return TRUE if successful.
* Prior data with the same key is NOT overwritten
$h = self::confighash($data);
if (isset (self::$x4a[$h])) {
if (self::Configcmp($np->data, $data) == 0) {
/* An existing entry with the same key is found. */
/* Fail because overwrite is not allows. */
/* Insert the new data */
$np = array('data' => $data, 'next' => 0, 'from' => 0);
if (isset (self::$x4a[$h])) {
self::$x4a[$h]->from = $np->next;
$np->next = self::$x4a[$h];
* Return a pointer to data assigned to the given key. Return NULL
* @return PHP_ParserGenerator_Config|0
$h = self::confighash($key);
if (!isset (self::$x4a[$h])) {
if (self::Configcmp($np->data, $key) == 0) {
return $np ? $np->data : 0;
|