ActionTable.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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: ActionTable.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. * The state of the yy_action table under construction is an instance of
  51. * the following structure
  52. *
  53. * @category PHP
  54. * @package PHP_ParserGenerator
  55. * @author Gregory Beaver <cellog@php.net>
  56. * @copyright 2006 Gregory Beaver
  57. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  58. * @version Release: @package_version@
  59. * @link http://pear.php.net/package/PHP_ParserGenerator
  60. * @since Class available since Release 0.1.0
  61. */
  62. class PHP_ParserGenerator_ActionTable
  63. {
  64. /**
  65. * Number of used slots in {@link $aAction}
  66. * @var int
  67. */
  68. public $nAction = 0;
  69. /**
  70. * The $yy_action table under construction.
  71. *
  72. * Each entry is of format:
  73. * <code>
  74. * array(
  75. * 'lookahead' => -1, // Value of the lookahead token (symbol index)
  76. * 'action' => -1 // Action to take on the given lookahead (action index)
  77. * );
  78. * </code>
  79. * @see PHP_ParserGenerator_Data::compute_action()
  80. * @var array
  81. */
  82. public $aAction = array(
  83. array(
  84. 'lookahead' => -1,
  85. 'action' => -1
  86. )
  87. );
  88. /**
  89. * A single new transaction set.
  90. *
  91. * @see $aAction format of the internal array is described here
  92. * @var array
  93. */
  94. public $aLookahead = array(
  95. array(
  96. 'lookahead' => 0,
  97. 'action' => 0
  98. )
  99. );
  100. /**
  101. * The smallest (minimum) value of any lookahead token in {@link $aLookahead}
  102. *
  103. * The lowest non-terminal is always introduced earlier in the parser file,
  104. * and is therefore a more significant token.
  105. * @var int
  106. */
  107. public $mnLookahead = 0;
  108. /**
  109. * The action associated with the smallest lookahead token.
  110. * @see $mnLookahead
  111. * @var int
  112. */
  113. public $mnAction = 0;
  114. /**
  115. * The largest (maximum) value of any lookahead token in {@link $aLookahead}
  116. * @var int
  117. */
  118. public $mxLookahead = 0;
  119. /**
  120. * The number of slots used in {@link $aLookahead}.
  121. *
  122. * This is the same as count($aLookahead), but there was no pressing reason
  123. * to change this when porting from C.
  124. * @see $mnLookahead
  125. * @var int
  126. */
  127. public $nLookahead = 0;
  128. /**
  129. * Add a new action to the current transaction set
  130. *
  131. * @param int $lookahead
  132. * @param int $action
  133. *
  134. * @return void
  135. */
  136. function acttab_action($lookahead, $action)
  137. {
  138. if ($this->nLookahead === 0) {
  139. $this->aLookahead = array();
  140. $this->mxLookahead = $lookahead;
  141. $this->mnLookahead = $lookahead;
  142. $this->mnAction = $action;
  143. } else {
  144. if ($this->mxLookahead < $lookahead) {
  145. $this->mxLookahead = $lookahead;
  146. }
  147. if ($this->mnLookahead > $lookahead) {
  148. $this->mnLookahead = $lookahead;
  149. $this->mnAction = $action;
  150. }
  151. }
  152. $this->aLookahead[$this->nLookahead] = array(
  153. 'lookahead' => $lookahead,
  154. 'action' => $action);
  155. $this->nLookahead++;
  156. }
  157. /**
  158. * Add the transaction set built up with prior calls to acttab_action()
  159. * into the current action table. Then reset the transaction set back
  160. * to an empty set in preparation for a new round of acttab_action() calls.
  161. *
  162. * Return the offset into the action table of the new transaction.
  163. *
  164. * @return int Return the offset that should be added to the lookahead in
  165. * order to get the index into $yy_action of the action. This will be used
  166. * in generation of $yy_ofst tables (reduce and shift)
  167. * @throws Exception
  168. */
  169. function acttab_insert()
  170. {
  171. if ($this->nLookahead <= 0) {
  172. throw new Exception('nLookahead is not set up?');
  173. }
  174. /* Scan the existing action table looking for an offset where we can
  175. ** insert the current transaction set. Fall out of the loop when that
  176. ** offset is found. In the worst case, we fall out of the loop when
  177. ** i reaches $this->nAction, which means we append the new transaction set.
  178. **
  179. ** i is the index in $this->aAction[] where $this->mnLookahead is inserted.
  180. */
  181. for ($i = 0; $i < $this->nAction + $this->mnLookahead; $i++) {
  182. if (!isset($this->aAction[$i])) {
  183. $this->aAction[$i] = array(
  184. 'lookahead' => -1,
  185. 'action' => -1,
  186. );
  187. }
  188. if ($this->aAction[$i]['lookahead'] < 0) {
  189. for ($j = 0; $j < $this->nLookahead; $j++) {
  190. if (!isset($this->aLookahead[$j])) {
  191. $this->aLookahead[$j] = array(
  192. 'lookahead' => 0,
  193. 'action' => 0,
  194. );
  195. }
  196. $k = $this->aLookahead[$j]['lookahead'] -
  197. $this->mnLookahead + $i;
  198. if ($k < 0) {
  199. break;
  200. }
  201. if (!isset($this->aAction[$k])) {
  202. $this->aAction[$k] = array(
  203. 'lookahead' => -1,
  204. 'action' => -1,
  205. );
  206. }
  207. if ($this->aAction[$k]['lookahead'] >= 0) {
  208. break;
  209. }
  210. }
  211. if ($j < $this->nLookahead ) {
  212. continue;
  213. }
  214. for ($j = 0; $j < $this->nAction; $j++) {
  215. if (!isset($this->aAction[$j])) {
  216. $this->aAction[$j] = array(
  217. 'lookahead' => -1,
  218. 'action' => -1,
  219. );
  220. }
  221. if ($this->aAction[$j]['lookahead'] == $j + $this->mnLookahead - $i) {
  222. break;
  223. }
  224. }
  225. if ($j == $this->nAction) {
  226. break; /* Fits in empty slots */
  227. }
  228. } elseif ($this->aAction[$i]['lookahead'] == $this->mnLookahead) {
  229. if ($this->aAction[$i]['action'] != $this->mnAction) {
  230. continue;
  231. }
  232. for ($j = 0; $j < $this->nLookahead; $j++) {
  233. $k = $this->aLookahead[$j]['lookahead'] -
  234. $this->mnLookahead + $i;
  235. if ($k < 0 || $k >= $this->nAction) {
  236. break;
  237. }
  238. if (!isset($this->aAction[$k])) {
  239. $this->aAction[$k] = array(
  240. 'lookahead' => -1,
  241. 'action' => -1,
  242. );
  243. }
  244. if ($this->aLookahead[$j]['lookahead'] != $this->aAction[$k]['lookahead']) {
  245. break;
  246. }
  247. if ($this->aLookahead[$j]['action'] != $this->aAction[$k]['action']) {
  248. break;
  249. }
  250. }
  251. if ($j < $this->nLookahead) {
  252. continue;
  253. }
  254. $n = 0;
  255. for ($j = 0; $j < $this->nAction; $j++) {
  256. if (!isset($this->aAction[$j])) {
  257. $this->aAction[$j] = array(
  258. 'lookahead' => -1,
  259. 'action' => -1,
  260. );
  261. }
  262. if ($this->aAction[$j]['lookahead'] < 0) {
  263. continue;
  264. }
  265. if ($this->aAction[$j]['lookahead'] == $j + $this->mnLookahead - $i) {
  266. $n++;
  267. }
  268. }
  269. if ($n == $this->nLookahead) {
  270. break; /* Same as a prior transaction set */
  271. }
  272. }
  273. }
  274. /* Insert transaction set at index i. */
  275. for ($j = 0; $j < $this->nLookahead; $j++) {
  276. if (!isset($this->aLookahead[$j])) {
  277. $this->aLookahead[$j] = array(
  278. 'lookahead' => 0,
  279. 'action' => 0,
  280. );
  281. }
  282. $k = $this->aLookahead[$j]['lookahead'] - $this->mnLookahead + $i;
  283. $this->aAction[$k] = $this->aLookahead[$j];
  284. if ($k >= $this->nAction) {
  285. $this->nAction = $k + 1;
  286. }
  287. }
  288. $this->nLookahead = 0;
  289. $this->aLookahead = array();
  290. /* Return the offset that is added to the lookahead in order to get the
  291. ** index into yy_action of the action */
  292. return $i - $this->mnLookahead;
  293. }
  294. }
  295. ?>