Action.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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: Action.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. * Every shift or reduce operation is stored as one of the following objects.
  51. *
  52. * @category PHP
  53. * @package PHP_ParserGenerator
  54. * @author Gregory Beaver <cellog@php.net>
  55. * @copyright 2006 Gregory Beaver
  56. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  57. * @version Release: @package_version@
  58. * @link http://pear.php.net/package/PHP_ParserGenerator
  59. * @since Class available since Release 0.1.0
  60. */
  61. class PHP_ParserGenerator_Action
  62. {
  63. const SHIFT = 1,
  64. ACCEPT = 2,
  65. REDUCE = 3,
  66. ERROR = 4,
  67. /**
  68. * Was a reduce, but part of a conflict
  69. */
  70. CONFLICT = 5,
  71. /**
  72. * Was a shift. Precedence resolved conflict
  73. */
  74. SH_RESOLVED = 6,
  75. /**
  76. * Was a reduce. Precedence resolved conflict
  77. */
  78. RD_RESOLVED = 7,
  79. /**
  80. * Deleted by compression
  81. * @see PHP_ParserGenerator::CompressTables()
  82. */
  83. NOT_USED = 8;
  84. /**
  85. * The look-ahead symbol that triggers this action
  86. * @var PHP_ParserGenerator_Symbol
  87. */
  88. public $sp; /* The look-ahead symbol */
  89. /**
  90. * This defines the kind of action, and must be one
  91. * of the class constants.
  92. *
  93. * - {@link PHP_ParserGenerator_Action::SHIFT}
  94. * - {@link PHP_ParserGenerator_Action::ACCEPT}
  95. * - {@link PHP_ParserGenerator_Action::REDUCE}
  96. * - {@link PHP_ParserGenerator_Action::ERROR}
  97. * - {@link PHP_ParserGenerator_Action::CONFLICT}
  98. * - {@link PHP_ParserGenerator_Action::SH_RESOLVED}
  99. * - {@link PHP_ParserGenerator_Action:: RD_RESOLVED}
  100. * - {@link PHP_ParserGenerator_Action::NOT_USED}
  101. */
  102. public $type;
  103. /**
  104. * The new state, if this is a shift,
  105. * the parser rule index, if this is a reduce.
  106. *
  107. * @var PHP_ParserGenerator_State|PHP_ParserGenerator_Rule
  108. */
  109. public $x;
  110. /**
  111. * The next action for this state.
  112. * @var PHP_ParserGenerator_Action
  113. */
  114. public $next;
  115. /**
  116. * Compare two actions
  117. *
  118. * This is used by {@link Action_sort()} to compare actions
  119. */
  120. static function actioncmp(PHP_ParserGenerator_Action $ap1, PHP_ParserGenerator_Action $ap2)
  121. {
  122. $rc = $ap1->sp->index - $ap2->sp->index;
  123. if ($rc === 0) {
  124. $rc = $ap1->type - $ap2->type;
  125. }
  126. if ($rc === 0) {
  127. if ($ap1->type == self::SHIFT) {
  128. if ($ap1->x->statenum != $ap2->x->statenum) {
  129. throw new Exception('Shift conflict: ' . $ap1->sp->name .
  130. ' shifts both to state ' . $ap1->x->statenum . ' (rule ' .
  131. $ap1->x->cfp->rp->lhs->name . ' on line ' .
  132. $ap1->x->cfp->rp->ruleline . ') and to state ' .
  133. $ap2->x->statenum . ' (rule ' .
  134. $ap2->x->cfp->rp->lhs->name . ' on line ' .
  135. $ap2->x->cfp->rp->ruleline . ')');
  136. }
  137. }
  138. if ($ap1->type != self::REDUCE
  139. && $ap1->type != self::RD_RESOLVED
  140. && $ap1->type != self::CONFLICT
  141. ) {
  142. throw new Exception('action has not been processed: ' .
  143. $ap1->sp->name . ' on line ' . $ap1->x->cfp->rp->ruleline .
  144. ', rule ' . $ap1->x->cfp->rp->lhs->name);
  145. }
  146. if ($ap2->type != self::REDUCE
  147. && $ap2->type != self::RD_RESOLVED
  148. && $ap2->type != self::CONFLICT
  149. ) {
  150. throw new Exception('action has not been processed: ' .
  151. $ap2->sp->name . ' on line ' . $ap2->x->cfp->rp->ruleline .
  152. ', rule ' . $ap2->x->cfp->rp->lhs->name);
  153. }
  154. $rc = $ap1->x->index - $ap2->x->index;
  155. }
  156. return $rc;
  157. }
  158. function display($processed = false)
  159. {
  160. $map = array(
  161. self::ACCEPT => 'ACCEPT',
  162. self::CONFLICT => 'CONFLICT',
  163. self::REDUCE => 'REDUCE',
  164. self::SHIFT => 'SHIFT'
  165. );
  166. echo $map[$this->type] . ' for ' . $this->sp->name;
  167. if ($this->type == self::REDUCE) {
  168. echo ' - rule ' . $this->x->lhs->name . "\n";
  169. } elseif ($this->type == self::SHIFT) {
  170. echo ' - state ' . $this->x->statenum . ', basis ' . $this->x->cfp->rp->lhs->name . "\n";
  171. } else {
  172. echo "\n";
  173. }
  174. }
  175. /**
  176. * create linked list of PHP_ParserGenerator_Actions
  177. *
  178. * @param PHP_ParserGenerator_Action|null $app
  179. * @param int $type one of the class constants from PHP_ParserGenerator_Action
  180. * @param PHP_ParserGenerator_Symbol $sp
  181. * @param PHP_ParserGenerator_State|PHP_ParserGenerator_Rule $arg
  182. */
  183. static function Action_add(&$app, $type, PHP_ParserGenerator_Symbol $sp, $arg)
  184. {
  185. $new = new PHP_ParserGenerator_Action;
  186. $new->next = $app;
  187. $app = $new;
  188. $new->type = $type;
  189. $new->sp = $sp;
  190. $new->x = $arg;
  191. echo ' Adding ';
  192. $new->display();
  193. }
  194. /**
  195. * Sort parser actions
  196. *
  197. * @param PHP_ParserGenerator_Action $ap a parser action
  198. *
  199. * @see PHP_ParserGenerator_Data::FindActions()
  200. *
  201. * @return PHP_ParserGenerator_Action
  202. */
  203. static function Action_sort(PHP_ParserGenerator_Action $ap)
  204. {
  205. $ap = PHP_ParserGenerator::msort($ap, 'next', array('PHP_ParserGenerator_Action', 'actioncmp'));
  206. return $ap;
  207. }
  208. /**
  209. * Print an action to the given file descriptor. Return FALSE if
  210. * nothing was actually printed.
  211. *
  212. * @param resource $fp File descriptor to print on
  213. * @param integer $indent Number of indents
  214. *
  215. * @see PHP_ParserGenerator_Data::ReportOutput()
  216. *
  217. * @return int|false
  218. */
  219. function PrintAction($fp, $indent)
  220. {
  221. if (!$fp) {
  222. $fp = STDOUT;
  223. }
  224. $result = 1;
  225. switch ($this->type)
  226. {
  227. case self::SHIFT:
  228. fprintf($fp, "%${indent}s shift %d", $this->sp->name, $this->x->statenum);
  229. break;
  230. case self::REDUCE:
  231. fprintf($fp, "%${indent}s reduce %d", $this->sp->name, $this->x->index);
  232. break;
  233. case self::ACCEPT:
  234. fprintf($fp, "%${indent}s accept", $this->sp->name);
  235. break;
  236. case self::ERROR:
  237. fprintf($fp, "%${indent}s error", $this->sp->name);
  238. break;
  239. case self::CONFLICT:
  240. fprintf($fp, "%${indent}s reduce %-3d ** Parsing conflict **", $this->sp->name, $this->x->index);
  241. break;
  242. case self::SH_RESOLVED:
  243. case self::RD_RESOLVED:
  244. case self::NOT_USED:
  245. $result = 0;
  246. break;
  247. }
  248. return $result;
  249. }
  250. }
  251. ?>