yacctabl.pas 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. function lookup(k : Integer) : String;{$ifndef fpc}far;{$endif}
  349. (* print name of symbol no. k *)
  350. begin
  351. with sym_table^[k] do
  352. if pname=nil then
  353. lookup := ''
  354. else
  355. lookup := pname^
  356. end(*lookup*);
  357. procedure entry(k : Integer; symbol : String);{$ifndef fpc}far;{$endif}
  358. (* enter symbol into table *)
  359. begin
  360. sym_table^[k].pname := newStr(symbol);
  361. end(*entry*);
  362. function get_key ( symbol : String ) : Integer;
  363. begin
  364. get_key := key(symbol, max_keys,{$ifdef fpc}@{$endif}lookup, {$ifdef fpc}@{$endif}entry);
  365. end(*get_key*);
  366. procedure def_key ( k : Integer; sym : Integer );
  367. begin
  368. sym_key^[sym] := k;
  369. sym_table^[k].deff := true;
  370. sym_table^[k].sym := sym;
  371. end(*def_key*);
  372. function is_def_key ( k : Integer; var sym : Integer ) : Boolean;
  373. begin
  374. if sym_table^[k].deff then
  375. begin
  376. sym := sym_table^[k].sym;
  377. is_def_key := true;
  378. end
  379. else
  380. is_def_key := false
  381. end(*is_def_key*);
  382. function pname ( sym : Integer ) : String;
  383. begin
  384. case sym of
  385. 1..255 : pname := singleQuoteStr(chr(sym));
  386. 0 : pname := '$end';
  387. -1 : pname := '$accept';
  388. else begin
  389. if sym_table^[sym_key^[sym]].pname^[1]=''''
  390. then begin
  391. pname := singleQuoteStr(
  392. copy( sym_table^[sym_key^[sym]].pname^,
  393. 2,
  394. length(sym_table^[sym_key^[sym]].pname^)-2)
  395. )
  396. end
  397. else begin
  398. pname := sym_table^[sym_key^[sym]].pname^;
  399. end;
  400. end;
  401. end;
  402. end(*pname*);
  403. (* Rule table: *)
  404. function newRuleRec ( r : RuleRec ) : RuleRecPtr;
  405. var rp : RuleRecPtr;
  406. begin
  407. getmem(rp, 2*sizeOf(Integer)+r.rhs_len*sizeOf(Integer));
  408. move(r, rp^, 2*sizeOf(Integer)+r.rhs_len*sizeOf(Integer));
  409. newRuleRec := rp;
  410. end(*newRuleRec*);
  411. procedure add_rule ( r : RuleRecPtr );
  412. begin
  413. inc(n_rules);
  414. if n_rules>max_rules then fatal(rule_table_overflow);
  415. rule_table^[n_rules] := r;
  416. end(*add_rule*);
  417. function rule_less ( i, j : Integer ) : Boolean;{$ifndef fpc}far;{$endif}
  418. begin
  419. if rule_table^[rule_no^[i]]^.lhs_sym =
  420. rule_table^[rule_no^[j]]^.lhs_sym then
  421. rule_less := rule_no^[i] < rule_no^[j]
  422. else
  423. rule_less := rule_table^[rule_no^[i]]^.lhs_sym >
  424. rule_table^[rule_no^[j]]^.lhs_sym
  425. end(*rule_less*);
  426. procedure rule_swap ( i, j : Integer );{$ifndef fpc}far;{$endif}
  427. var x : Integer;
  428. begin
  429. x := rule_no^[i]; rule_no^[i] := rule_no^[j]; rule_no^[j] := x;
  430. end(*rule_swap*);
  431. procedure sort_rules;
  432. var i : Integer;
  433. begin
  434. for i := 1 to n_rules do rule_no^[i] := i;
  435. quicksort ( 1, n_rules, {$ifdef fpc}@{$endif}rule_less, {$ifdef fpc}@{$endif}rule_swap );
  436. end(*sort_rules*);
  437. procedure rule_offsets;
  438. var i, sym : Integer;
  439. begin
  440. for sym := 1 to n_nts do with rule_offs^[sym] do
  441. begin
  442. rule_lo := 1; rule_hi := 0;
  443. end;
  444. i := 1;
  445. while (i<=n_rules) do
  446. begin
  447. sym := rule_table^[rule_no^[i]]^.lhs_sym;
  448. rule_offs^[-sym].rule_lo := i;
  449. inc(i);
  450. while (i<=n_rules) and
  451. (rule_table^[rule_no^[i]]^.lhs_sym=sym) do
  452. inc(i);
  453. rule_offs^[-sym].rule_hi := i-1;
  454. end;
  455. end(*rule_offsets*);
  456. function n_nt_rules ( sym : Integer ) : Integer;
  457. begin
  458. with rule_offs^[-sym] do
  459. n_nt_rules := rule_hi-rule_lo+1
  460. end(*n_nt_rules*);
  461. (* Type Table routines: *)
  462. procedure add_type ( k : Integer );
  463. begin
  464. inc(n_types);
  465. if n_types>max_types then fatal(type_table_overflow);
  466. type_table^[n_types] := k;
  467. end(*add_type*);
  468. (* Routines to sort type identifiers alphabetically: *)
  469. function type_less ( i, j : Integer ) : Boolean;{$ifndef fpc}far;{$endif}
  470. begin
  471. type_less := sym_table^[type_table^[i]].pname^<
  472. sym_table^[type_table^[j]].pname^
  473. end(*type_less*);
  474. procedure type_swap ( i, j : Integer );{$ifndef fpc}far;{$endif}
  475. var x : Integer;
  476. begin
  477. x := type_table^[i];
  478. type_table^[i] := type_table^[j];
  479. type_table^[j] := x;
  480. end(*type_swap*);
  481. procedure sort_types;
  482. var i, j, count : Integer;
  483. begin
  484. (* sort: *)
  485. quicksort(1, n_types, {$ifdef fpc}@{$endif}type_less, {$ifdef fpc}@{$endif}type_swap);
  486. (* eliminate dups: *)
  487. i := 1; j := 1; count := 0;
  488. while i<=n_types do
  489. begin
  490. if i<>j then type_table^[j] := type_table^[i];
  491. while (i<n_types) and (type_table^[i+1]=type_table^[i]) do
  492. begin
  493. inc(i); inc(count);
  494. end;
  495. inc(i); inc(j);
  496. end;
  497. dec(n_types, count);
  498. end(*sort_types*);
  499. function search_type ( symbol : String ) : Boolean;
  500. var l, r, k : Integer;
  501. begin
  502. (* binary search: *)
  503. l := 1; r := n_types;
  504. k := l + (r-l) div 2;
  505. while (l<r) and (sym_table^[type_table^[k]].pname^<>symbol) do
  506. begin
  507. if sym_table^[type_table^[k]].pname^<symbol then
  508. l := succ(k)
  509. else
  510. r := pred(k);
  511. k := l + (r-l) div 2;
  512. end;
  513. search_type := (k<=n_types) and (sym_table^[type_table^[k]].pname^=symbol);
  514. end(*search_type*);
  515. (* Precedence table routines: *)
  516. function new_prec_level ( prec_type : PrecType ) : Integer;
  517. begin
  518. inc(n_prec);
  519. if n_prec>max_prec then fatal(prec_table_overflow);
  520. prec_table^[n_prec] := prec_type;
  521. new_prec_level := n_prec;
  522. end(*new_prec_level*);
  523. (* State table: *)
  524. procedure new_state;
  525. begin
  526. inc(n_states);
  527. if n_states>max_states then fatal(state_table_overflow);
  528. state_table^[n_states-1].item_lo := n_items+1;
  529. end(*new_state*);
  530. procedure add_item ( rule_no, pos_no : Integer );
  531. begin
  532. inc(n_items);
  533. if n_items>max_items then fatal(item_table_overflow);
  534. item_table^[n_items].rule_no := rule_no;
  535. item_table^[n_items].pos_no := pos_no;
  536. item_table^[n_items].next := 0;
  537. end(*add_item*);
  538. function add_state : Integer;
  539. function state_key ( s : Integer ) : Integer;
  540. (* determines a hash key for state s *)
  541. const max_key = 4001;
  542. (* should be prime number s.t. hash keys are distributed
  543. evenly *)
  544. var i, k : Integer;
  545. begin
  546. with state_table^[s] do
  547. begin
  548. k := 0;
  549. for i := item_lo to item_hi do
  550. with item_table^[i] do
  551. inc(k, rule_no+pos_no);
  552. state_key := k mod max_key;
  553. end;
  554. end(*state_key*);
  555. function search_state ( s, lo, hi : Integer; var t : Integer ) : Boolean;
  556. (* searches the range lo..hi in the state table for a state with the
  557. same kernel items as s; returns true if found, and then the
  558. corresponding state number in t *)
  559. function eq_items(s, t : Integer) : Boolean;
  560. (* compares kernel item sets of states s and t *)
  561. var i, i_s, i_t : Integer;
  562. begin
  563. if n_state_items(s)<>n_state_items(t) then
  564. eq_items := false
  565. else
  566. begin
  567. i_s := state_table^[s].item_lo;
  568. i_t := state_table^[t].item_lo;
  569. for i := 0 to n_state_items(s)-1 do
  570. if (item_table^[i_s+i].rule_no<>item_table^[i_t+i].rule_no) or
  571. (item_table^[i_s+i].pos_no<>item_table^[i_t+i].pos_no) then
  572. begin
  573. eq_items := false;
  574. exit;
  575. end;
  576. eq_items := true;
  577. end
  578. end(*eq_items*);
  579. var t1 : Integer;
  580. begin
  581. with state_table^[s] do
  582. for t1 := lo to hi do
  583. if (key=state_table^[t1].key) and
  584. eq_items(s, t1) then
  585. begin
  586. search_state := true;
  587. t := t1;
  588. exit;
  589. end;
  590. search_state := false;
  591. end(*search_state*);
  592. var s : Integer;
  593. begin
  594. with state_table^[n_states-1] do
  595. begin
  596. item_hi := n_items;
  597. key := state_key(n_states-1);
  598. if search_state(n_states-1, 0, n_states-2, s) then
  599. begin
  600. n_items := item_lo;
  601. dec(n_states);
  602. add_state := s;
  603. end
  604. else
  605. add_state := n_states-1;
  606. end;
  607. end(*add_state*);
  608. procedure start_trans;
  609. begin
  610. state_table^[act_state].trans_lo := n_trans+1;
  611. end(*start_trans*);
  612. procedure add_trans ( sym, next_state : Integer );
  613. begin
  614. inc(n_trans);
  615. if n_trans>max_trans then fatal(trans_table_overflow);
  616. trans_table^[n_trans].sym := sym;
  617. trans_table^[n_trans].next_state := next_state;
  618. end(*add_trans*);
  619. procedure end_trans;
  620. begin
  621. state_table^[act_state].trans_hi := n_trans;
  622. end(*end_trans*);
  623. procedure start_redns;
  624. begin
  625. state_table^[act_state].redns_lo := n_redns+1;
  626. end(*start_redns*);
  627. procedure add_redn ( symset : IntSetPtr; rule_no : Integer );
  628. begin
  629. inc(n_redns);
  630. if n_redns>max_redns then fatal(redn_table_overflow);
  631. redn_table^[n_redns].symset := symset;
  632. redn_table^[n_redns].rule_no := rule_no;
  633. end(*add_redn*);
  634. procedure end_redns;
  635. begin
  636. state_table^[act_state].redns_hi := n_redns;
  637. end(*end_redns*);
  638. function n_state_items ( s : Integer ) : Integer;
  639. begin
  640. with state_table^[s] do
  641. n_state_items := item_hi-item_lo+1
  642. end(*n_state_items*);
  643. function n_state_trans ( s : Integer ) : Integer;
  644. begin
  645. with state_table^[s] do
  646. n_state_trans := trans_hi-trans_lo+1
  647. end(*n_state_trans*);
  648. function n_state_redns ( s : Integer ) : Integer;
  649. begin
  650. with state_table^[s] do
  651. n_state_redns := redns_hi-redns_lo+1
  652. end(*n_state_redns*);
  653. function find_item( s : Integer; rule_no, pos_no : Integer ) : Integer;
  654. var i : Integer;
  655. begin
  656. with state_table^[s] do
  657. for i := item_lo to item_hi do
  658. if (item_table^[i].rule_no=rule_no) and
  659. (item_table^[i].pos_no=pos_no) then
  660. begin
  661. find_item := i;
  662. exit;
  663. end;
  664. find_item := 0;
  665. end(*find_item*);
  666. (* Item set routines: *)
  667. procedure empty_item_set ( var item_set : ItemSet );
  668. begin
  669. item_set.n_items := 0;
  670. end(*empty_item_set*);
  671. procedure include_item_set ( var item_set : ItemSet;
  672. rule_no, pos_no : Integer);
  673. begin
  674. with item_set do
  675. begin
  676. inc(n_items);
  677. if n_items>max_set_items then fatal(item_table_overflow);
  678. item[n_items].rule_no := rule_no;
  679. item[n_items].pos_no := pos_no;
  680. end;
  681. end(*include_item_set*);
  682. procedure get_item_set ( s : Integer; var item_set : ItemSet);
  683. begin
  684. with state_table^[s], item_set do
  685. begin
  686. n_items := n_state_items(s);
  687. move(item_table^[item_lo], item, n_items*sizeOf(ItemRec));
  688. end
  689. end(*get_item_set*);
  690. procedure closure ( var item_set : ItemSet );
  691. var i, j : Integer;
  692. nt_syms0, nt_syms : IntSet;
  693. begin
  694. with item_set do
  695. begin
  696. (* get the nonterminals at current positions in items: *)
  697. empty(nt_syms0);
  698. for i := 1 to n_items do
  699. with item[i], rule_table^[rule_no]^ do
  700. if (pos_no<=rhs_len) and (rhs_sym[pos_no]<0) then
  701. include(nt_syms0, rhs_sym[pos_no]);
  702. nt_syms := nt_syms0;
  703. (* add closure symbols: *)
  704. for i := 1 to size(nt_syms0) do
  705. setunion(nt_syms, closure_table^[-nt_syms0[i]]^);
  706. (* add the nonkernel items for the nonterminal symbols: *)
  707. for i := 1 to size(nt_syms) do
  708. with rule_offs^[-nt_syms[i]] do
  709. for j := rule_lo to rule_hi do
  710. include_item_set(item_set, rule_no^[j], 1);
  711. end;
  712. end(*closure*);
  713. var sort_items : ItemSet;
  714. (* comparison and swap routines for sort_item_set: *)
  715. function items_less ( i, j : Integer ) : Boolean;{$ifndef fpc}far;{$endif}
  716. begin
  717. with sort_items do
  718. if item[i].pos_no=item[j].pos_no then
  719. items_less := item[i].rule_no<item[j].rule_no
  720. else
  721. items_less := item[i].pos_no>item[j].pos_no
  722. end(*items_less*);
  723. procedure items_swap ( i, j : Integer );{$ifndef fpc}far;{$endif}
  724. var x : ItemRec;
  725. begin
  726. with sort_items do
  727. begin
  728. x := item[i]; item[i] := item[j]; item[j] := x;
  729. end
  730. end(*items_swap*);
  731. procedure sort_item_set ( var item_set : ItemSet );
  732. begin
  733. sort_items := item_set;
  734. quicksort(1, sort_items.n_items, {$ifdef fpc}@{$endif}items_less, {$ifdef fpc}@{$endif}items_swap);
  735. item_set := sort_items;
  736. end(*sort_item_set*);
  737. var i : Integer;
  738. begin
  739. verbose := false;
  740. debug := false;
  741. startnt := 0;
  742. {$IFNDEF Win32}
  743. max_bytes := memAvail;
  744. {$ENDIF}
  745. n_nts := 1;
  746. n_lits := 257;
  747. n_rules := 0;
  748. n_types := 0;
  749. n_prec := 0;
  750. n_states := 0;
  751. n_items := 0;
  752. n_trans := 0;
  753. n_redns := 0;
  754. (* allocate tables: *)
  755. new(sym_table);
  756. new(sym_key);
  757. new(rule_table);
  758. new(rule_no);
  759. new(rule_offs);
  760. new(type_table);
  761. new(sym_type);
  762. new(prec_table);
  763. new(sym_prec);
  764. new(rule_prec);
  765. new(closure_table);
  766. new(first_set_table);
  767. new(nullable);
  768. new(state_table);
  769. new(item_table);
  770. new(trans_table);
  771. new(redn_table);
  772. new(lookahead_table);
  773. new(prop_table);
  774. (* initialize symbol table: *)
  775. for i := 1 to max_keys do
  776. with sym_table^[i] do
  777. begin
  778. pname := nil;
  779. deff := false;
  780. end;
  781. (* enter predefined error symbol into symbol table: *)
  782. def_key(get_key('error'), 256);
  783. (* initialize type and precedence tables: *)
  784. for i := -max_nts to max_lits-1 do sym_type^[i] := 0;
  785. for i := 0 to max_lits-1 do sym_prec^[i] := 0;
  786. for i := 1 to max_rules do rule_prec^[i] := 0;
  787. end(*YaccTables*).