yacctabl.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. {
  2. This module collects the various tables used by the Yacc program:
  3. - the symbol table
  4. - the rule table
  5. - the precedence table
  6. - the closure table
  7. - the LALR state, item, transition and reduction table
  8. Note: All tables are allocated dynamically (at initialization time)
  9. because of the 64KB static data limit. *)
  10. Copyright (c) 1990-92 Albert Graef <[email protected]>
  11. Copyright (C) 1996 Berend de Boer <[email protected]>
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. $Revision: 1.3 $
  24. $Modtime: 96-07-31 21:15 $
  25. $History: YACCTABL.PAS $
  26. *
  27. * ***************** Version 2 *****************
  28. * User: Berend Date: 96-10-10 Time: 21:16
  29. * Updated in $/Lex and Yacc/tply
  30. * Updated for protected mode, windows and Delphi 1.X and 2.X.
  31. }
  32. unit YaccTabl;
  33. interface
  34. uses
  35. YaccBase;
  36. const
  37. (* Maximum table sizes: *)
  38. max_keys = 997; (* size of hash symbol table (prime number!) *)
  39. {$IFDEF MsDos}
  40. max_nts = 300; (* maximum number of nonterminals *)
  41. max_lits = 556; (* number of literals (300+256) *)
  42. max_rules = 301; (* number of rules (300+1) *)
  43. max_types = 100; (* number of type tags *)
  44. max_prec = 50; (* maximum precedence level *)
  45. max_states = 600; (* number of LR(0) states *)
  46. max_items = 2400; (* number of items *)
  47. max_trans = 2400; (* number of transitions *)
  48. max_redns = 1200; (* number of reductions *)
  49. {$ELSE}
  50. max_nts = 900; (* maximum number of nonterminals *)
  51. max_lits = max_nts+256; (* number of literals (300+256) *)
  52. max_rules = max_nts+1; (* number of rules (300+1) *)
  53. max_types = 100; (* number of type tags *)
  54. max_prec = 50; (* maximum precedence level *)
  55. {$IFDEF Windows}
  56. max_states = 3000; (* number of LR(0) states *)
  57. {$ELSE}
  58. max_states = 3000; (* number of LR(0) states *)
  59. {$ENDIF}
  60. max_items = 40000; (* number of items *)
  61. max_trans = 40000; (* number of transitions *)
  62. max_redns = 9600; (* number of reductions *)
  63. {$ENDIF}
  64. {$IFDEF MsDos}
  65. max_rule_len = 64; (* maximum length of rules *)
  66. max_set_items = 64; (* maximum number of items in an item set *)
  67. {$ELSE}
  68. max_rule_len = 64; (* maximum length of rules *)
  69. max_set_items = 9600; (* maximum number of items in an item set *)
  70. {$ENDIF}
  71. var
  72. (* Actual table sizes: *)
  73. n_nts : Integer;
  74. n_lits : Integer;
  75. n_rules : Integer;
  76. n_types : Integer;
  77. n_prec : Integer;
  78. n_states : Integer;
  79. n_items : Integer;
  80. n_trans : Integer;
  81. n_redns : Integer;
  82. type
  83. (* Table data structures: *)
  84. (* Symbol table: The symbol table consists of a hash table which stores
  85. print names and internal symbol numbers, and a key table which stores,
  86. for each internal symbol number, the corresponding hash key. *)
  87. SymRec = record
  88. pname : StrPtr;
  89. (* print name; empty entries are denoted by pname=nil *)
  90. deff : Boolean;
  91. (* flag denoting whether symbol is already defined *)
  92. sym : Integer;
  93. (* internal symbol number (0 or positive: literal symbols
  94. (literal characters have symbol numbers 1 thru 255);
  95. negative: nonterminal symbols; 0 denotes endmarker,
  96. -1 augmented start nonterminal, 256 is reserved for
  97. error token; note that the predefined symbols except
  98. the error literal are not actually stored in the symbol
  99. table; the error symbol is entered at initialization
  100. s.t. it always has number 256) *)
  101. end;
  102. SymTable = array [1..max_keys] of SymRec;
  103. SymKeyTable = array [-max_nts..max_lits-1] of Integer;
  104. (* hash keys for nonterminal and literal symbols *)
  105. (* Rule table: the rule table consists of an array storing the rules
  106. sequentially in the order in which they appear in the source grammar;
  107. a rule no.s table which is used to sort rules w.r.t. left-hand side
  108. nonterminals (after the rule table has been constructed and sorted, all
  109. references to rules are done indirectly via the rule_no array s.t. the
  110. rules for each nonterminal can be accessed easily); and an offset table
  111. which stores, for each nonterminal, the corresponding first and last
  112. index in the rule no.s table. *)
  113. RuleRec = record
  114. lhs_sym : Integer; (* lhs nonterminal *)
  115. rhs_len : Integer; (* length of rhs *)
  116. rhs_sym : array [1..max_rule_len] of Integer;
  117. (* rhs symbols *)
  118. end;
  119. RuleRecPtr = ^RuleRec;
  120. RuleTable = array [1..max_rules] of RuleRecPtr;
  121. RuleNoTable = array [1..max_rules] of Integer;
  122. RuleOffsRec = record
  123. rule_lo, rule_hi : Integer;
  124. end;
  125. RuleOffsTable = array [1..max_nts] of RuleOffsRec;
  126. (* Symbol type table: The symbol type table stores the types associated
  127. with the nonterminal and terminal grammar symbols (0 if none). *)
  128. TypeTable = array [1..max_types] of Integer;
  129. (* types declared in the definitions section *)
  130. SymTypeTable = array [-max_nts..max_lits-1] of Integer;
  131. (* symbol types *)
  132. (* Precedence table: The precedence table stores the type of each
  133. precedence level (left, right, nonassoc) and, for each literal
  134. symbol and grammar rule, the assigned precedence level (precedence
  135. level 0 if none). *)
  136. PrecType = ( left, right, nonassoc );
  137. PrecTable = array [1..max_prec] of PrecType;
  138. SymPrecTable = array [0..max_lits-1] of Integer;
  139. RulePrecTable = array [1..max_rules] of Integer;
  140. (* Closure and first symbols table: The closure table stores, for each
  141. nonterminal X, the set of those nonterminals Y for which there is a
  142. rightmost derivation X =>+ Y ... . Similarly, the first set table
  143. stores, for each nonterminal X, the set of literals a for which there
  144. is a derivation X =>+ a ... . Both tables are of type SymSetTable.
  145. The nullable table stores, for each nonterminal, a flag denoting whether
  146. the nonterminal is nullable (i.e. may be derived to the empty string).
  147. These tables are constructed by the routines in the YaccClosure unit,
  148. and are used by the LALR parser construction algorithms in YaccLR0 and
  149. YaccLookaheads. *)
  150. SymSetTable = array [1..max_nts] of IntSetPtr;
  151. NullableTable = array [1..max_nts] of Boolean;
  152. (* State table:
  153. Each state stores the first and last index of the kernel items,
  154. transitions and reductions belonging to it, and a hash key determined
  155. from the kernel items which is used to speed up searches for existing
  156. states.
  157. The items table stores the individual kernel items in the LR(0) set.
  158. Each entry consists of a rule number together with the item position,
  159. and a "next" field indicating the associated item in the successor state
  160. (0 if there is none). The ItemSet type is used to retrieve and manipulate
  161. individual item sets from the item table.
  162. The transition table stores the shift and goto transitions in each state
  163. (each transition is denoted by a (symbol, next_state) pair). Similarly,
  164. the reductions table stores the reductions in each state, where each
  165. action is denoted by a (symbolset, ruleno) pair. *)
  166. StateRec = record
  167. item_lo, item_hi : Integer;
  168. trans_lo, trans_hi : Integer;
  169. redns_lo, redns_hi : Integer;
  170. key : Integer;
  171. end;
  172. StateTable = array [0..max_states-1] of StateRec;
  173. ItemRec = record
  174. rule_no, pos_no : Integer;
  175. next : Integer;
  176. end;
  177. ItemSet = record
  178. n_items : Integer;
  179. item : array [1..max_set_items] of ItemRec;
  180. end;
  181. ItemTable = array [1..max_items] of ItemRec;
  182. TransRec = record
  183. sym, next_state : Integer;
  184. end;
  185. TransTable = array [1..max_trans] of TransRec;
  186. RednRec = record
  187. symset : IntSetPtr;
  188. rule_no : Integer;
  189. end;
  190. RednTable = array [1..max_redns] of RednRec;
  191. (* Lookaheads table: This table stores, for each kernel item, the
  192. corresponding LALR(1) lookahead symbol sets. *)
  193. LookaheadTable = array [1..max_items] of IntSetPtr;
  194. (* The propagation table is used to keep track of how lookaheads are
  195. propagated from kernel items to other lookahead sets. *)
  196. PropList = ^PropEntry;
  197. PropEntry = record
  198. symset : IntSetPtr;
  199. next : PropList;
  200. end;
  201. PropTable = array [1..max_items] of PropList;
  202. var
  203. verbose : Boolean; (* status of the verbose option *)
  204. debug : Boolean; (* status of the debug option *)
  205. startnt : Integer; (* start nonterminal of grammar
  206. (0 if undefined) *)
  207. sym_table : ^SymTable; (* symbol table *)
  208. sym_key : ^SymKeyTable; (* symbol keys *)
  209. rule_table : ^RuleTable; (* rule table *)
  210. type_table : ^TypeTable; (* type table *)
  211. sym_type : ^SymTypeTable; (* symbol types *)
  212. prec_table : ^PrecTable; (* precedence table *)
  213. sym_prec : ^SymPrecTable; (* literal symbols precedence *)
  214. rule_prec : ^RulePrecTable; (* rules precedence *)
  215. rule_no : ^RuleNoTable; (* rule no table *)
  216. rule_offs : ^RuleOffsTable; (* rule offset table *)
  217. closure_table : ^SymSetTable; (* closure table *)
  218. first_set_table : ^SymSetTable; (* first set table *)
  219. nullable : ^NullableTable; (* nullable flags table *)
  220. state_table : ^StateTable; (* LR(0) state table *)
  221. item_table : ^ItemTable; (* LR(0) kernel item table *)
  222. trans_table : ^TransTable; (* transition table *)
  223. redn_table : ^RednTable; (* reduction table *)
  224. lookahead_table : ^LookaheadTable; (* LALR lookaheads table *)
  225. prop_table : ^PropTable; (* lookahead propagation table *)
  226. (* Operations: *)
  227. (* Symbol table routines: *)
  228. function new_nt : Integer;
  229. (* returns a new nonterminal number (<-1) *)
  230. function new_lit : Integer;
  231. (* returns a new literal number above 256 *)
  232. procedure add_lit ( sym : Integer );
  233. (* this routine allows to add a user-defined literal symbol;
  234. the current literal symbols count is adjusted accordingly *)
  235. function get_key ( symbol : String ) : Integer;
  236. (* returns a hash key for symbol *)
  237. procedure def_key ( k : Integer; sym : Integer );
  238. (* defines k to be a new symbol with internal symbol number sym *)
  239. function is_def_key ( k : Integer; var sym : Integer ) : Boolean;
  240. (* checks whether symbol denoted by symbol table key k is already
  241. defined; if so, returns the corresponding symbol number *)
  242. function pname ( sym : Integer ) : String;
  243. (* returns the print name of an internal symbol (`$end' for
  244. symbol 0, `$accept' for nonterminal -1, and a single quoted
  245. character for literals 1..255) *)
  246. (* Rule table routines: *)
  247. function newRuleRec ( r : RuleRec ) : RuleRecPtr;
  248. (* obtains a dynamic copy of r (only the number of bytes actually
  249. needed is allocated) *)
  250. procedure add_rule ( r : RuleRecPtr );
  251. (* add a rule to the rule table *)
  252. procedure sort_rules;
  253. (* sorts rules w.r.t. left-hand sides into the rule no table *)
  254. procedure rule_offsets;
  255. (* computes rule offsets after rules have been sorted *)
  256. function n_nt_rules ( sym : Integer ) : Integer;
  257. (* returns number of rules for nonterminal sym *)
  258. (* Type Table routines: *)
  259. procedure add_type ( k : Integer );
  260. (* add a type identifier to the table *)
  261. procedure sort_types;
  262. (* sort the type table alphabetically, eliminate dups *)
  263. function search_type ( symbol : String ) : Boolean;
  264. (* search the sorted types table for the given type symbol *)
  265. (* Precedence table routines: *)
  266. function new_prec_level ( prec_type : PrecType ) : Integer;
  267. (* adds a new precedence level of the denoted type; returns: the new
  268. level *)
  269. (* State table routines: *)
  270. var act_state : Integer; (* state currently considered *)
  271. procedure new_state;
  272. (* build a new state *)
  273. procedure add_item ( rule_no, pos_no : Integer );
  274. (* add an item to the new state (initialize its next field to 0) *)
  275. function add_state : Integer;
  276. (* add the new state to the state table; if an equivalent state is already
  277. in the table, dispose the new state, and return the existing state
  278. number, otherwise return the new state number *)
  279. procedure start_trans;
  280. (* starts building transitions of the active state *)
  281. procedure add_trans ( sym, next_state : Integer );
  282. (* adds a transition to the active state *)
  283. procedure end_trans;
  284. (* ends transitions of the active state *)
  285. procedure start_redns;
  286. (* starts building reduction actions of the active state *)
  287. procedure add_redn ( symset : IntSetPtr; rule_no : Integer );
  288. (* adds a reduction to the active state *)
  289. procedure end_redns;
  290. (* ends reduction actions of the active state *)
  291. function n_state_items ( s : Integer ) : Integer;
  292. function n_state_trans ( s : Integer ) : Integer;
  293. function n_state_redns ( s : Integer ) : Integer;
  294. (* return the number of kernel items, transitions and reductions in state
  295. s, respectively *)
  296. function find_item( s : Integer; rule_no, pos_no : Integer ) : Integer;
  297. (* find item (rule_no, pos_no) in state s; returns: the item number *)
  298. (* Item set routines: *)
  299. procedure empty_item_set ( var item_set : ItemSet );
  300. (* initializes an empty item set *)
  301. procedure include_item_set ( var item_set : ItemSet;
  302. rule_no, pos_no : Integer);
  303. (* add the denoted item to the given item set *)
  304. procedure get_item_set ( s : Integer; var item_set : ItemSet);
  305. (* obtain the item set of state s from the item table *)
  306. procedure closure ( var item_set : ItemSet );
  307. (* compute the closure of item_set (using the closure table) *)
  308. procedure sort_item_set ( var item_set : ItemSet );
  309. (* sorts an item set w.r.t. position and rule numbers (higher positions,
  310. lower rules first) *)
  311. implementation
  312. uses YaccMsgs;
  313. (* Symbol table routines: *)
  314. function new_nt : Integer;
  315. begin
  316. inc(n_nts);
  317. if n_nts>max_nts then fatal(nt_table_overflow);
  318. sym_type^[-n_nts] := 0;
  319. new_nt := -n_nts;
  320. end(*new_nt*);
  321. function new_lit : Integer;
  322. begin
  323. inc(n_lits);
  324. if n_lits>max_lits then fatal(lit_table_overflow);
  325. sym_type^[n_lits-1] := 0;
  326. sym_prec^[n_lits-1] := 0;
  327. new_lit := n_lits-1;
  328. end(*new_lit*);
  329. procedure add_lit ( sym : Integer );
  330. begin
  331. if sym>n_lits then n_lits := sym;
  332. if n_lits>max_lits then fatal(lit_table_overflow);
  333. sym_type^[sym] := 0;
  334. sym_prec^[sym] := 0;
  335. end(*add_lit*);
  336. {$ifndef fpc}{$F+}{$endif}
  337. function lookup(k : Integer) : String;
  338. {$ifndef fpc}{$F-}{$endif}
  339. (* print name of symbol no. k *)
  340. begin
  341. with sym_table^[k] do
  342. if pname=nil then
  343. lookup := ''
  344. else
  345. lookup := pname^
  346. end(*lookup*);
  347. {$ifndef fpc}{$F+}{$endif}
  348. procedure entry(k : Integer; symbol : String);
  349. {$ifndef fpc}{$F-}{$endif}
  350. (* enter symbol into table *)
  351. begin
  352. sym_table^[k].pname := newStr(symbol);
  353. end(*entry*);
  354. function get_key ( symbol : String ) : Integer;
  355. begin
  356. get_key := key(symbol, max_keys, {$ifdef fpc}@{$endif}lookup,
  357. {$ifdef fpc}@{$endif}entry);
  358. end(*get_key*);
  359. procedure def_key ( k : Integer; sym : Integer );
  360. begin
  361. sym_key^[sym] := k;
  362. sym_table^[k].deff := true;
  363. sym_table^[k].sym := sym;
  364. end(*def_key*);
  365. function is_def_key ( k : Integer; var sym : Integer ) : Boolean;
  366. begin
  367. if sym_table^[k].deff then
  368. begin
  369. sym := sym_table^[k].sym;
  370. is_def_key := true;
  371. end
  372. else
  373. is_def_key := false
  374. end(*is_def_key*);
  375. function pname ( sym : Integer ) : String;
  376. begin
  377. case sym of
  378. 1..255 : pname := singleQuoteStr(chr(sym));
  379. 0 : pname := '$end';
  380. -1 : pname := '$accept';
  381. else begin
  382. if sym_table^[sym_key^[sym]].pname^[1]=''''
  383. then begin
  384. pname := singleQuoteStr(
  385. copy( sym_table^[sym_key^[sym]].pname^,
  386. 2,
  387. length(sym_table^[sym_key^[sym]].pname^)-2)
  388. )
  389. end
  390. else begin
  391. pname := sym_table^[sym_key^[sym]].pname^;
  392. end;
  393. end;
  394. end;
  395. end(*pname*);
  396. (* Rule table: *)
  397. function newRuleRec ( r : RuleRec ) : RuleRecPtr;
  398. var rp : RuleRecPtr;
  399. begin
  400. getmem(rp, 2*sizeOf(Integer)+r.rhs_len*sizeOf(Integer));
  401. move(r, rp^, 2*sizeOf(Integer)+r.rhs_len*sizeOf(Integer));
  402. newRuleRec := rp;
  403. end(*newRuleRec*);
  404. procedure add_rule ( r : RuleRecPtr );
  405. begin
  406. inc(n_rules);
  407. if n_rules>max_rules then fatal(rule_table_overflow);
  408. rule_table^[n_rules] := r;
  409. end(*add_rule*);
  410. {$ifndef fpc}{$F+}{$endif}
  411. function rule_less ( i, j : Integer ) : Boolean;
  412. {$ifndef fpc}{$F-}{$endif}
  413. begin
  414. if rule_table^[rule_no^[i]]^.lhs_sym =
  415. rule_table^[rule_no^[j]]^.lhs_sym then
  416. rule_less := rule_no^[i] < rule_no^[j]
  417. else
  418. rule_less := rule_table^[rule_no^[i]]^.lhs_sym >
  419. rule_table^[rule_no^[j]]^.lhs_sym
  420. end(*rule_less*);
  421. {$ifndef fpc}{$F+}{$endif}
  422. procedure rule_swap ( i, j : Integer );
  423. {$ifndef fpc}{$F-}{$endif}
  424. var x : Integer;
  425. begin
  426. x := rule_no^[i]; rule_no^[i] := rule_no^[j]; rule_no^[j] := x;
  427. end(*rule_swap*);
  428. procedure sort_rules;
  429. var i : Integer;
  430. begin
  431. for i := 1 to n_rules do rule_no^[i] := i;
  432. quicksort ( 1, n_rules, {$ifdef fpc}@{$endif}rule_less,
  433. {$ifdef fpc}@{$endif}rule_swap );
  434. end(*sort_rules*);
  435. procedure rule_offsets;
  436. var i, sym : Integer;
  437. begin
  438. for sym := 1 to n_nts do with rule_offs^[sym] do
  439. begin
  440. rule_lo := 1; rule_hi := 0;
  441. end;
  442. i := 1;
  443. while (i<=n_rules) do
  444. begin
  445. sym := rule_table^[rule_no^[i]]^.lhs_sym;
  446. rule_offs^[-sym].rule_lo := i;
  447. inc(i);
  448. while (i<=n_rules) and
  449. (rule_table^[rule_no^[i]]^.lhs_sym=sym) do
  450. inc(i);
  451. rule_offs^[-sym].rule_hi := i-1;
  452. end;
  453. end(*rule_offsets*);
  454. function n_nt_rules ( sym : Integer ) : Integer;
  455. begin
  456. with rule_offs^[-sym] do
  457. n_nt_rules := rule_hi-rule_lo+1
  458. end(*n_nt_rules*);
  459. (* Type Table routines: *)
  460. procedure add_type ( k : Integer );
  461. begin
  462. inc(n_types);
  463. if n_types>max_types then fatal(type_table_overflow);
  464. type_table^[n_types] := k;
  465. end(*add_type*);
  466. (* Routines to sort type identifiers alphabetically: *)
  467. {$ifndef fpc}{$F+}{$endif}
  468. function type_less ( i, j : Integer ) : Boolean;
  469. {$ifndef fpc}{$F-}{$endif}
  470. begin
  471. type_less := sym_table^[type_table^[i]].pname^<
  472. sym_table^[type_table^[j]].pname^
  473. end(*type_less*);
  474. {$ifndef fpc}{$F+}{$endif}
  475. procedure type_swap ( i, j : Integer );
  476. {$ifndef fpc}{$F-}{$endif}
  477. var x : Integer;
  478. begin
  479. x := type_table^[i];
  480. type_table^[i] := type_table^[j];
  481. type_table^[j] := x;
  482. end(*type_swap*);
  483. procedure sort_types;
  484. var i, j, count : Integer;
  485. begin
  486. (* sort: *)
  487. quicksort(1, n_types, {$ifdef fpc}@{$endif}type_less,
  488. {$ifdef fpc}@{$endif}type_swap);
  489. (* eliminate dups: *)
  490. i := 1; j := 1; count := 0;
  491. while i<=n_types do
  492. begin
  493. if i<>j then type_table^[j] := type_table^[i];
  494. while (i<n_types) and (type_table^[i+1]=type_table^[i]) do
  495. begin
  496. inc(i); inc(count);
  497. end;
  498. inc(i); inc(j);
  499. end;
  500. dec(n_types, count);
  501. end(*sort_types*);
  502. function search_type ( symbol : String ) : Boolean;
  503. var l, r, k : Integer;
  504. begin
  505. (* binary search: *)
  506. l := 1; r := n_types;
  507. k := l + (r-l) div 2;
  508. while (l<r) and (sym_table^[type_table^[k]].pname^<>symbol) do
  509. begin
  510. if sym_table^[type_table^[k]].pname^<symbol then
  511. l := succ(k)
  512. else
  513. r := pred(k);
  514. k := l + (r-l) div 2;
  515. end;
  516. search_type := (k<=n_types) and (sym_table^[type_table^[k]].pname^=symbol);
  517. end(*search_type*);
  518. (* Precedence table routines: *)
  519. function new_prec_level ( prec_type : PrecType ) : Integer;
  520. begin
  521. inc(n_prec);
  522. if n_prec>max_prec then fatal(prec_table_overflow);
  523. prec_table^[n_prec] := prec_type;
  524. new_prec_level := n_prec;
  525. end(*new_prec_level*);
  526. (* State table: *)
  527. procedure new_state;
  528. begin
  529. inc(n_states);
  530. if n_states>max_states then fatal(state_table_overflow);
  531. state_table^[n_states-1].item_lo := n_items+1;
  532. end(*new_state*);
  533. procedure add_item ( rule_no, pos_no : Integer );
  534. begin
  535. inc(n_items);
  536. if n_items>max_items then fatal(item_table_overflow);
  537. item_table^[n_items].rule_no := rule_no;
  538. item_table^[n_items].pos_no := pos_no;
  539. item_table^[n_items].next := 0;
  540. end(*add_item*);
  541. function add_state : Integer;
  542. function state_key ( s : Integer ) : Integer;
  543. (* determines a hash key for state s *)
  544. const max_key = 4001;
  545. (* should be prime number s.t. hash keys are distributed
  546. evenly *)
  547. var i, k : Integer;
  548. begin
  549. with state_table^[s] do
  550. begin
  551. k := 0;
  552. for i := item_lo to item_hi do
  553. with item_table^[i] do
  554. inc(k, rule_no+pos_no);
  555. state_key := k mod max_key;
  556. end;
  557. end(*state_key*);
  558. function search_state ( s, lo, hi : Integer; var t : Integer ) : Boolean;
  559. (* searches the range lo..hi in the state table for a state with the
  560. same kernel items as s; returns true if found, and then the
  561. corresponding state number in t *)
  562. function eq_items(s, t : Integer) : Boolean;
  563. (* compares kernel item sets of states s and t *)
  564. var i, i_s, i_t : Integer;
  565. begin
  566. if n_state_items(s)<>n_state_items(t) then
  567. eq_items := false
  568. else
  569. begin
  570. i_s := state_table^[s].item_lo;
  571. i_t := state_table^[t].item_lo;
  572. for i := 0 to n_state_items(s)-1 do
  573. if (item_table^[i_s+i].rule_no<>item_table^[i_t+i].rule_no) or
  574. (item_table^[i_s+i].pos_no<>item_table^[i_t+i].pos_no) then
  575. begin
  576. eq_items := false;
  577. exit;
  578. end;
  579. eq_items := true;
  580. end
  581. end(*eq_items*);
  582. var t1 : Integer;
  583. begin
  584. with state_table^[s] do
  585. for t1 := lo to hi do
  586. if (key=state_table^[t1].key) and
  587. eq_items(s, t1) then
  588. begin
  589. search_state := true;
  590. t := t1;
  591. exit;
  592. end;
  593. search_state := false;
  594. end(*search_state*);
  595. var s : Integer;
  596. begin
  597. with state_table^[n_states-1] do
  598. begin
  599. item_hi := n_items;
  600. key := state_key(n_states-1);
  601. if search_state(n_states-1, 0, n_states-2, s) then
  602. begin
  603. n_items := item_lo;
  604. dec(n_states);
  605. add_state := s;
  606. end
  607. else
  608. add_state := n_states-1;
  609. end;
  610. end(*add_state*);
  611. procedure start_trans;
  612. begin
  613. state_table^[act_state].trans_lo := n_trans+1;
  614. end(*start_trans*);
  615. procedure add_trans ( sym, next_state : Integer );
  616. begin
  617. inc(n_trans);
  618. if n_trans>max_trans then fatal(trans_table_overflow);
  619. trans_table^[n_trans].sym := sym;
  620. trans_table^[n_trans].next_state := next_state;
  621. end(*add_trans*);
  622. procedure end_trans;
  623. begin
  624. state_table^[act_state].trans_hi := n_trans;
  625. end(*end_trans*);
  626. procedure start_redns;
  627. begin
  628. state_table^[act_state].redns_lo := n_redns+1;
  629. end(*start_redns*);
  630. procedure add_redn ( symset : IntSetPtr; rule_no : Integer );
  631. begin
  632. inc(n_redns);
  633. if n_redns>max_redns then fatal(redn_table_overflow);
  634. redn_table^[n_redns].symset := symset;
  635. redn_table^[n_redns].rule_no := rule_no;
  636. end(*add_redn*);
  637. procedure end_redns;
  638. begin
  639. state_table^[act_state].redns_hi := n_redns;
  640. end(*end_redns*);
  641. function n_state_items ( s : Integer ) : Integer;
  642. begin
  643. with state_table^[s] do
  644. n_state_items := item_hi-item_lo+1
  645. end(*n_state_items*);
  646. function n_state_trans ( s : Integer ) : Integer;
  647. begin
  648. with state_table^[s] do
  649. n_state_trans := trans_hi-trans_lo+1
  650. end(*n_state_trans*);
  651. function n_state_redns ( s : Integer ) : Integer;
  652. begin
  653. with state_table^[s] do
  654. n_state_redns := redns_hi-redns_lo+1
  655. end(*n_state_redns*);
  656. function find_item( s : Integer; rule_no, pos_no : Integer ) : Integer;
  657. var i : Integer;
  658. begin
  659. with state_table^[s] do
  660. for i := item_lo to item_hi do
  661. if (item_table^[i].rule_no=rule_no) and
  662. (item_table^[i].pos_no=pos_no) then
  663. begin
  664. find_item := i;
  665. exit;
  666. end;
  667. find_item := 0;
  668. end(*find_item*);
  669. (* Item set routines: *)
  670. procedure empty_item_set ( var item_set : ItemSet );
  671. begin
  672. item_set.n_items := 0;
  673. end(*empty_item_set*);
  674. procedure include_item_set ( var item_set : ItemSet;
  675. rule_no, pos_no : Integer);
  676. begin
  677. with item_set do
  678. begin
  679. inc(n_items);
  680. if n_items>max_set_items then fatal(item_table_overflow);
  681. item[n_items].rule_no := rule_no;
  682. item[n_items].pos_no := pos_no;
  683. end;
  684. end(*include_item_set*);
  685. procedure get_item_set ( s : Integer; var item_set : ItemSet);
  686. begin
  687. with state_table^[s], item_set do
  688. begin
  689. n_items := n_state_items(s);
  690. move(item_table^[item_lo], item, n_items*sizeOf(ItemRec));
  691. end
  692. end(*get_item_set*);
  693. procedure closure ( var item_set : ItemSet );
  694. var i, j : Integer;
  695. nt_syms0, nt_syms : IntSet;
  696. begin
  697. with item_set do
  698. begin
  699. (* get the nonterminals at current positions in items: *)
  700. empty(nt_syms0);
  701. for i := 1 to n_items do
  702. with item[i], rule_table^[rule_no]^ do
  703. if (pos_no<=rhs_len) and (rhs_sym[pos_no]<0) then
  704. include(nt_syms0, rhs_sym[pos_no]);
  705. nt_syms := nt_syms0;
  706. (* add closure symbols: *)
  707. for i := 1 to size(nt_syms0) do
  708. setunion(nt_syms, closure_table^[-nt_syms0[i]]^);
  709. (* add the nonkernel items for the nonterminal symbols: *)
  710. for i := 1 to size(nt_syms) do
  711. with rule_offs^[-nt_syms[i]] do
  712. for j := rule_lo to rule_hi do
  713. include_item_set(item_set, rule_no^[j], 1);
  714. end;
  715. end(*closure*);
  716. var sort_items : ItemSet;
  717. (* comparison and swap routines for sort_item_set: *)
  718. {$ifndef fpc}{$F+}{$endif}
  719. function items_less ( i, j : Integer ) : Boolean;
  720. {$ifndef fpc}{$F-}{$endif}
  721. begin
  722. with sort_items do
  723. if item[i].pos_no=item[j].pos_no then
  724. items_less := item[i].rule_no<item[j].rule_no
  725. else
  726. items_less := item[i].pos_no>item[j].pos_no
  727. end(*items_less*);
  728. {$ifndef fpc}{$F+}{$endif}
  729. procedure items_swap ( i, j : Integer );
  730. {$ifndef fpc}{$F-}{$endif}
  731. var x : ItemRec;
  732. begin
  733. with sort_items do
  734. begin
  735. x := item[i]; item[i] := item[j]; item[j] := x;
  736. end
  737. end(*items_swap*);
  738. procedure sort_item_set ( var item_set : ItemSet );
  739. begin
  740. sort_items := item_set;
  741. quicksort(1, sort_items.n_items, {$ifdef fpc}@{$endif}items_less,
  742. {$ifdef fpc}@{$endif}items_swap);
  743. item_set := sort_items;
  744. end(*sort_item_set*);
  745. var i : Integer;
  746. begin
  747. verbose := false;
  748. debug := false;
  749. startnt := 0;
  750. n_nts := 1;
  751. n_lits := 257;
  752. n_rules := 0;
  753. n_types := 0;
  754. n_prec := 0;
  755. n_states := 0;
  756. n_items := 0;
  757. n_trans := 0;
  758. n_redns := 0;
  759. (* allocate tables: *)
  760. new(sym_table);
  761. new(sym_key);
  762. new(rule_table);
  763. new(rule_no);
  764. new(rule_offs);
  765. new(type_table);
  766. new(sym_type);
  767. new(prec_table);
  768. new(sym_prec);
  769. new(rule_prec);
  770. new(closure_table);
  771. new(first_set_table);
  772. new(nullable);
  773. new(state_table);
  774. new(item_table);
  775. new(trans_table);
  776. new(redn_table);
  777. new(lookahead_table);
  778. new(prop_table);
  779. (* initialize symbol table: *)
  780. for i := 1 to max_keys do
  781. with sym_table^[i] do
  782. begin
  783. pname := nil;
  784. deff := false;
  785. end;
  786. (* enter predefined error symbol into symbol table: *)
  787. def_key(get_key('error'), 256);
  788. (* initialize type and precedence tables: *)
  789. for i := -max_nts to max_lits-1 do sym_type^[i] := 0;
  790. for i := 0 to max_lits-1 do sym_prec^[i] := 0;
  791. for i := 1 to max_rules do rule_prec^[i] := 0;
  792. end(*YaccTables*).