Config.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. <?php
  2. /**
  3. * PHP_ParserGenerator, a php 5 parser generator.
  4. *
  5. * This is a direct port of the Lemon parser generator, found at
  6. * {@link http://www.hwaci.com/sw/lemon/}
  7. *
  8. * PHP version 5
  9. *
  10. * LICENSE:
  11. *
  12. * Copyright (c) 2006, Gregory Beaver <cellog@php.net>
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. *
  19. * * Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * * Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in
  23. * the documentation and/or other materials provided with the distribution.
  24. * * Neither the name of the PHP_ParserGenerator nor the names of its
  25. * contributors may be used to endorse or promote products derived
  26. * from this software without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  29. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  30. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  31. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  32. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  36. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  37. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * @category PHP
  41. * @package PHP_ParserGenerator
  42. * @author Gregory Beaver <cellog@php.net>
  43. * @copyright 2006 Gregory Beaver
  44. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  45. * @version CVS: $Id: Config.php 302382 2010-08-17 06:08:09Z jespino $
  46. * @link http://pear.php.net/package/PHP_ParserGenerator
  47. * @since File available since Release 0.1.0
  48. */
  49. /**
  50. /** A configuration is a production rule of the grammar together with
  51. * a mark (dot) showing how much of that rule has been processed so far.
  52. *
  53. * Configurations also contain a follow-set which is a list of terminal
  54. * symbols which are allowed to immediately follow the end of the rule.
  55. * Every configuration is recorded as an instance of the following class.
  56. *
  57. * @category PHP
  58. * @package PHP_ParserGenerator
  59. * @author Gregory Beaver <cellog@php.net>
  60. * @copyright 2006 Gregory Beaver
  61. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  62. * @version Release: @package_version@
  63. * @link http://pear.php.net/package/PHP_ParserGenerator
  64. * @since Class available since Release 0.1.0
  65. */
  66. class PHP_ParserGenerator_Config
  67. {
  68. const COMPLETE = 1;
  69. const INCOMPLETE = 2;
  70. /**
  71. * The parser rule upon with the configuration is based.
  72. *
  73. * A parser rule is something like:
  74. * <pre>
  75. * blah ::= FOO bar.
  76. * </pre>
  77. * @var PHP_ParserGenerator_Rule
  78. */
  79. public $rp;
  80. /**
  81. * The parse point.
  82. *
  83. * This is the index into the right-hand side of a rule that is
  84. * represented by this configuration. In other words, possible
  85. * dots for this rule:
  86. *
  87. * <pre>
  88. * blah ::= FOO bar.
  89. * </pre>
  90. *
  91. * are (represented by "[here]"):
  92. *
  93. * <pre>
  94. * blah ::= [here] FOO bar.
  95. * blah ::= FOO [here] bar.
  96. * blah ::= FOO bar [here].
  97. * </pre>
  98. * @var int
  99. */
  100. public $dot;
  101. /**
  102. * Follow-set for this configuration only
  103. *
  104. * This is the list of terminals and non-terminals that
  105. * can follow this configuration.
  106. * @var array
  107. */
  108. public $fws;
  109. /**
  110. * Follow-set forward propagation links.
  111. * @var PHP_ParserGenerator_PropagationLink
  112. */
  113. public $fplp;
  114. /**
  115. * Follow-set backwards propagation links
  116. * @var PHP_ParserGenerator_PropagationLink
  117. */
  118. public $bplp;
  119. /**
  120. * State that contains this configuration
  121. * @var PHP_ParserGenerator_State
  122. */
  123. public $stp;
  124. /* enum {
  125. COMPLETE, /* The status is used during followset and
  126. INCOMPLETE /* shift computations
  127. } */
  128. /**
  129. * Status during followset and shift computations.
  130. *
  131. * One of PHP_ParserGenerator_Config::COMPLETE or
  132. * PHP_ParserGenerator_Config::INCOMPLETE.
  133. * @var int
  134. */
  135. public $status;
  136. /**
  137. * Next configuration in the state.
  138. *
  139. * Index of next PHP_ParserGenerator_Config object.
  140. * @var int
  141. */
  142. public $next;
  143. /**
  144. * Index of the next basis configuration PHP_ParserGenerator_Config object
  145. * @var int
  146. */
  147. public $bp;
  148. /**
  149. * Top of the list of configurations for the current state.
  150. * @var PHP_ParserGenerator_Config
  151. */
  152. static public $current;
  153. /**
  154. * Last on the list of configurations for the current state.
  155. * @var PHP_ParserGenerator_Config
  156. */
  157. static public $currentend;
  158. /**
  159. * Top of the list of basis configurations for the current state.
  160. * @var PHP_ParserGenerator_Config
  161. */
  162. static public $basis;
  163. /**
  164. * Last on the list of basis configurations for the current state.
  165. * @var PHP_ParserGenerator_Config
  166. */
  167. static public $basisend;
  168. /**
  169. * Associative array representation of the linked list of configurations
  170. * found in {@link $current}
  171. *
  172. * @var array
  173. */
  174. static public $x4a = array();
  175. /**
  176. * Return a pointer to a new configuration
  177. * @return PHP_ParserGenerator_Config
  178. */
  179. private static function newconfig()
  180. {
  181. return new PHP_ParserGenerator_Config;
  182. }
  183. /**
  184. * Display the current configuration for the .out file
  185. *
  186. * @param PHP_ParserGenerator_Config $cfp
  187. * @see PHP_ParserGenerator_Data::ReportOutput()
  188. */
  189. static function Configshow(PHP_ParserGenerator_Config $cfp)
  190. {
  191. $fp = fopen('php://output', 'w');
  192. while ($cfp) {
  193. if ($cfp->dot == $cfp->rp->nrhs) {
  194. $buf = sprintf('(%d)', $cfp->rp->index);
  195. fprintf($fp, ' %5s ', $buf);
  196. } else {
  197. fwrite($fp,' ');
  198. }
  199. $cfp->ConfigPrint($fp);
  200. fwrite($fp, "\n");
  201. if (0) {
  202. //SetPrint(fp,cfp->fws,$this);
  203. //PlinkPrint(fp,cfp->fplp,"To ");
  204. //PlinkPrint(fp,cfp->bplp,"From");
  205. }
  206. $cfp = $cfp->next;
  207. }
  208. fwrite($fp, "\n");
  209. fclose($fp);
  210. }
  211. /**
  212. * Initialize the configuration list builder for a new state.
  213. */
  214. static function Configlist_init()
  215. {
  216. self::$current = 0;
  217. self::$currentend = &self::$current;
  218. self::$basis = 0;
  219. self::$basisend = &self::$basis;
  220. self::$x4a = array();
  221. }
  222. /**
  223. * Remove all data from the table.
  224. *
  225. * Pass each data to the function $f as it is removed if
  226. * $f is a valid callback.
  227. * @param callback|null
  228. * @see Configtable_clear()
  229. */
  230. static function Configtable_reset($f)
  231. {
  232. self::$current = 0;
  233. self::$currentend = &self::$current;
  234. self::$basis = 0;
  235. self::$basisend = &self::$basis;
  236. self::Configtable_clear(0);
  237. }
  238. /**
  239. * Remove all data from the associative array representation
  240. * of configurations.
  241. *
  242. * Pass each data to the function $f as it is removed if
  243. * $f is a valid callback.
  244. * @param callback|null
  245. */
  246. static function Configtable_clear($f)
  247. {
  248. if (!count(self::$x4a)) {
  249. return;
  250. }
  251. if ($f) {
  252. for ($i = 0; $i < count(self::$x4a); $i++) {
  253. call_user_func($f, self::$x4a[$i]->data);
  254. }
  255. }
  256. self::$x4a = array();
  257. }
  258. /**
  259. * Reset the configuration list builder for a new state.
  260. * @see Configtable_clear()
  261. */
  262. static function Configlist_reset()
  263. {
  264. self::Configtable_clear(0);
  265. }
  266. /**
  267. * Add another configuration to the configuration list for this parser state.
  268. * @param PHP_ParserGenerator_Rule the rule
  269. * @param int Index into the right-hand side of the rule where the dot goes
  270. * @return PHP_ParserGenerator_Config
  271. */
  272. static function Configlist_add($rp, $dot)
  273. {
  274. $model = new PHP_ParserGenerator_Config;
  275. $model->rp = $rp;
  276. $model->dot = $dot;
  277. $cfp = self::Configtable_find($model);
  278. if ($cfp === 0) {
  279. $cfp = self::newconfig();
  280. $cfp->rp = $rp;
  281. $cfp->dot = $dot;
  282. $cfp->fws = array();
  283. $cfp->stp = 0;
  284. $cfp->fplp = $cfp->bplp = 0;
  285. $cfp->next = 0;
  286. $cfp->bp = 0;
  287. self::$currentend = $cfp;
  288. self::$currentend = &$cfp->next;
  289. self::Configtable_insert($cfp);
  290. }
  291. return $cfp;
  292. }
  293. /**
  294. * Add a basis configuration to the configuration list for this parser state.
  295. *
  296. * Basis configurations are the root for a configuration. This method also
  297. * inserts the configuration into the regular list of configurations for this
  298. * reason.
  299. * @param PHP_ParserGenerator_Rule the rule
  300. * @param int Index into the right-hand side of the rule where the dot goes
  301. * @return PHP_ParserGenerator_Config
  302. */
  303. static function Configlist_addbasis($rp, $dot)
  304. {
  305. $model = new PHP_ParserGenerator_Config;
  306. $model->rp = $rp;
  307. $model->dot = $dot;
  308. $cfp = self::Configtable_find($model);
  309. if ($cfp === 0) {
  310. $cfp = self::newconfig();
  311. $cfp->rp = $rp;
  312. $cfp->dot = $dot;
  313. $cfp->fws = array();
  314. $cfp->stp = 0;
  315. $cfp->fplp = $cfp->bplp = 0;
  316. $cfp->next = 0;
  317. $cfp->bp = 0;
  318. self::$currentend = $cfp;
  319. self::$currentend = &$cfp->next;
  320. self::$basisend = $cfp;
  321. self::$basisend = &$cfp->bp;
  322. self::Configtable_insert($cfp);
  323. }
  324. return $cfp;
  325. }
  326. /**
  327. * Compute the closure of the configuration list.
  328. *
  329. * This calculates all of the possible continuations of
  330. * each configuration, ensuring that each state accounts
  331. * for every configuration that could arrive at that state.
  332. */
  333. static function Configlist_closure(PHP_ParserGenerator_Data $lemp)
  334. {
  335. for ($cfp = self::$current; $cfp; $cfp = $cfp->next) {
  336. $rp = $cfp->rp;
  337. $dot = $cfp->dot;
  338. if ($dot >= $rp->nrhs) {
  339. continue;
  340. }
  341. $sp = $rp->rhs[$dot];
  342. if ($sp->type == PHP_ParserGenerator_Symbol::NONTERMINAL) {
  343. if ($sp->rule === 0 && $sp !== $lemp->errsym) {
  344. PHP_ParserGenerator::ErrorMsg($lemp->filename, $rp->line,
  345. "Nonterminal \"%s\" has no rules.", $sp->name);
  346. $lemp->errorcnt++;
  347. }
  348. for ($newrp = $sp->rule; $newrp; $newrp = $newrp->nextlhs) {
  349. $newcfp = self::Configlist_add($newrp, 0);
  350. for ($i = $dot + 1; $i < $rp->nrhs; $i++) {
  351. $xsp = $rp->rhs[$i];
  352. if ($xsp->type == PHP_ParserGenerator_Symbol::TERMINAL) {
  353. $newcfp->fws[$xsp->index] = 1;
  354. break;
  355. } elseif ($xsp->type == PHP_ParserGenerator_Symbol::MULTITERMINAL) {
  356. for ($k = 0; $k < $xsp->nsubsym; $k++) {
  357. $newcfp->fws[$xsp->subsym[$k]->index] = 1;
  358. }
  359. break;
  360. } else {
  361. $a = array_diff_key($xsp->firstset, $newcfp->fws);
  362. $newcfp->fws += $a;
  363. if ($xsp->lambda === false) {
  364. break;
  365. }
  366. }
  367. }
  368. if ($i == $rp->nrhs) {
  369. PHP_ParserGenerator_PropagationLink::Plink_add($cfp->fplp, $newcfp);
  370. }
  371. }
  372. }
  373. }
  374. }
  375. /**
  376. * Sort the configuration list
  377. * @uses Configcmp()
  378. */
  379. static function Configlist_sort()
  380. {
  381. $a = 0;
  382. //self::Configshow(self::$current);
  383. self::$current = PHP_ParserGenerator::msort(self::$current,'next', array('PHP_ParserGenerator_Config', 'Configcmp'));
  384. //self::Configshow(self::$current);
  385. self::$currentend = &$a;
  386. self::$currentend = 0;
  387. }
  388. /**
  389. * Sort the configuration list
  390. * @uses Configcmp
  391. */
  392. static function Configlist_sortbasis()
  393. {
  394. $a = 0;
  395. self::$basis = PHP_ParserGenerator::msort(self::$current,'bp', array('PHP_ParserGenerator_Config', 'Configcmp'));
  396. self::$basisend = &$a;
  397. self::$basisend = 0;
  398. }
  399. /**
  400. * Return a pointer to the head of the configuration list and
  401. * reset the list
  402. * @see $current
  403. * @return PHP_ParserGenerator_Config
  404. */
  405. static function Configlist_return()
  406. {
  407. $old = self::$current;
  408. self::$current = 0;
  409. self::$currentend = &self::$current;
  410. return $old;
  411. }
  412. /**
  413. * Return a pointer to the head of the basis list and
  414. * reset the list
  415. * @see $basis
  416. * @return PHP_ParserGenerator_Config
  417. */
  418. static function Configlist_basis()
  419. {
  420. $old = self::$basis;
  421. self::$basis = 0;
  422. self::$basisend = &self::$basis;
  423. return $old;
  424. }
  425. /**
  426. * Free all elements of the given configuration list
  427. * @param PHP_ParserGenerator_Config
  428. */
  429. static function Configlist_eat($cfp)
  430. {
  431. for (; $cfp; $cfp = $nextcfp) {
  432. $nextcfp = $cfp->next;
  433. if ($cfp->fplp !=0) {
  434. throw new Exception('fplp of configuration non-zero?');
  435. }
  436. if ($cfp->bplp !=0) {
  437. throw new Exception('bplp of configuration non-zero?');
  438. }
  439. if ($cfp->fws) {
  440. $cfp->fws = array();
  441. }
  442. }
  443. }
  444. /**
  445. * Compare two configurations for sorting purposes.
  446. *
  447. * Configurations based on higher precedence rules
  448. * (those earlier in the file) are chosen first. Two
  449. * configurations that are the same rule are sorted by
  450. * dot (see {@link $dot}), and those configurations
  451. * with a dot closer to the left-hand side are chosen first.
  452. * @param unknown_type $a
  453. * @param unknown_type $b
  454. * @return unknown
  455. */
  456. static function Configcmp($a, $b)
  457. {
  458. $x = $a->rp->index - $b->rp->index;
  459. if (!$x) {
  460. $x = $a->dot - $b->dot;
  461. }
  462. return $x;
  463. }
  464. /**
  465. * Print out information on this configuration.
  466. *
  467. * @param resource $fp
  468. * @see PHP_ParserGenerator_Data::ReportOutput()
  469. */
  470. function ConfigPrint($fp)
  471. {
  472. $rp = $this->rp;
  473. fprintf($fp, "%s ::=", $rp->lhs->name);
  474. for ($i = 0; $i <= $rp->nrhs; $i++) {
  475. if ($i === $this->dot) {
  476. fwrite($fp,' *');
  477. }
  478. if ($i === $rp->nrhs) {
  479. break;
  480. }
  481. $sp = $rp->rhs[$i];
  482. fprintf($fp,' %s', $sp->name);
  483. if ($sp->type == PHP_ParserGenerator_Symbol::MULTITERMINAL) {
  484. for ($j = 1; $j < $sp->nsubsym; $j++) {
  485. fprintf($fp, '|%s', $sp->subsym[$j]->name);
  486. }
  487. }
  488. }
  489. }
  490. /**
  491. * Hash a configuration for the associative array {@link $x4a}
  492. */
  493. private static function confighash(PHP_ParserGenerator_Config $a)
  494. {
  495. $h = 0;
  496. $h = $h * 571 + $a->rp->index * 37 + $a->dot;
  497. return $h;
  498. }
  499. /**
  500. * Insert a new record into the array. Return TRUE if successful.
  501. * Prior data with the same key is NOT overwritten
  502. */
  503. static function Configtable_insert(PHP_ParserGenerator_Config $data)
  504. {
  505. $h = self::confighash($data);
  506. if (isset(self::$x4a[$h])) {
  507. $np = self::$x4a[$h];
  508. } else {
  509. $np = 0;
  510. }
  511. while ($np) {
  512. if (self::Configcmp($np->data, $data) == 0) {
  513. /* An existing entry with the same key is found. */
  514. /* Fail because overwrite is not allows. */
  515. return 0;
  516. }
  517. $np = $np->next;
  518. }
  519. /* Insert the new data */
  520. $np = array('data' => $data, 'next' => 0, 'from' => 0);
  521. $np = new PHP_ParserGenerator_StateNode;
  522. $np->data = $data;
  523. if (isset(self::$x4a[$h])) {
  524. self::$x4a[$h]->from = $np->next;
  525. $np->next = self::$x4a[$h];
  526. }
  527. $np->from = $np;
  528. self::$x4a[$h] = $np;
  529. return 1;
  530. }
  531. /**
  532. * Return a pointer to data assigned to the given key. Return NULL
  533. * if no such key.
  534. * @return PHP_ParserGenerator_Config|0
  535. */
  536. static function Configtable_find(PHP_ParserGenerator_Config $key)
  537. {
  538. $h = self::confighash($key);
  539. if (!isset(self::$x4a[$h])) {
  540. return 0;
  541. }
  542. $np = self::$x4a[$h];
  543. while ($np) {
  544. if (self::Configcmp($np->data, $key) == 0) {
  545. break;
  546. }
  547. $np = $np->next;
  548. }
  549. return $np ? $np->data : 0;
  550. }
  551. }
  552. ?>