yacctabl.pas 29 KB

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