SCM Repository
Annotation of /sml/trunk/src/ml-yacc/lib/lrtable.sml
Parent Directory
|
Revision Log
Revision 250 - (view) (download)
1 : | monnier | 249 | (* ML-Yacc Parser Generator (c) 1989 Andrew W. Appel, David R. Tarditi |
2 : | * | ||
3 : | * $Log$ | ||
4 : | * Revision 1.1.1.10 1999/04/17 18:56:10 monnier | ||
5 : | * version 110.16 | ||
6 : | * | ||
7 : | * Revision 1.1.1.1 1997/01/14 01:38:04 george | ||
8 : | * Version 109.24 | ||
9 : | * | ||
10 : | * Revision 1.1.1.1 1996/01/31 16:01:42 george | ||
11 : | * Version 109 | ||
12 : | * | ||
13 : | *) | ||
14 : | |||
15 : | structure LrTable : LR_TABLE = | ||
16 : | struct | ||
17 : | open Array List | ||
18 : | infix 9 sub | ||
19 : | datatype ('a,'b) pairlist = EMPTY | ||
20 : | | PAIR of 'a * 'b * ('a,'b) pairlist | ||
21 : | datatype term = T of int | ||
22 : | datatype nonterm = NT of int | ||
23 : | datatype state = STATE of int | ||
24 : | datatype action = SHIFT of state | ||
25 : | | REDUCE of int (* rulenum from grammar *) | ||
26 : | | ACCEPT | ||
27 : | | ERROR | ||
28 : | exception Goto of state * nonterm | ||
29 : | type table = {states: int, rules : int,initialState: state, | ||
30 : | action: ((term,action) pairlist * action) array, | ||
31 : | goto : (nonterm,state) pairlist array} | ||
32 : | val numStates = fn ({states,...} : table) => states | ||
33 : | val numRules = fn ({rules,...} : table) => rules | ||
34 : | val describeActions = | ||
35 : | fn ({action,...} : table) => | ||
36 : | fn (STATE s) => action sub s | ||
37 : | val describeGoto = | ||
38 : | fn ({goto,...} : table) => | ||
39 : | fn (STATE s) => goto sub s | ||
40 : | fun findTerm (T term,row,default) = | ||
41 : | let fun find (PAIR (T key,data,r)) = | ||
42 : | if key < term then find r | ||
43 : | else if key=term then data | ||
44 : | else default | ||
45 : | | find EMPTY = default | ||
46 : | in find row | ||
47 : | end | ||
48 : | fun findNonterm (NT nt,row) = | ||
49 : | let fun find (PAIR (NT key,data,r)) = | ||
50 : | if key < nt then find r | ||
51 : | else if key=nt then SOME data | ||
52 : | else NONE | ||
53 : | | find EMPTY = NONE | ||
54 : | in find row | ||
55 : | end | ||
56 : | val action = fn ({action,...} : table) => | ||
57 : | fn (STATE state,term) => | ||
58 : | let val (row,default) = action sub state | ||
59 : | in findTerm(term,row,default) | ||
60 : | end | ||
61 : | val goto = fn ({goto,...} : table) => | ||
62 : | fn (a as (STATE state,nonterm)) => | ||
63 : | case findNonterm(nonterm,goto sub state) | ||
64 : | of SOME state => state | ||
65 : | | NONE => raise (Goto a) | ||
66 : | val initialState = fn ({initialState,...} : table) => initialState | ||
67 : | val mkLrTable = fn {actions,gotos,initialState,numStates,numRules} => | ||
68 : | ({action=actions,goto=gotos, | ||
69 : | states=numStates, | ||
70 : | rules=numRules, | ||
71 : | initialState=initialState} : table) | ||
72 : | end; |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |