Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/ml-yacc/lib/parser1.sml
ViewVC logotype

Annotation of /sml/trunk/src/ml-yacc/lib/parser1.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9 - (view) (download)

1 : monnier 2 (* ML-Yacc Parser Generator (c) 1989 Andrew W. Appel, David R. Tarditi
2 :     *
3 :     * $Log$
4 : monnier 8 * Revision 1.1.1.2 1998/01/18 01:00:08 monnier
5 :     * *** empty log message ***
6 : monnier 2 *
7 : monnier 8 * Revision 1.2 1997/09/10 18:34:22 jhr
8 :     * Changed "abstraction" to ":>".
9 :     *
10 : monnier 2 # Revision 1.1.1.1 1997/01/14 01:38:04 george
11 :     # Version 109.24
12 :     #
13 :     * Revision 1.1.1.1 1996/01/31 16:01:42 george
14 :     * Version 109
15 :     *
16 :     *)
17 :    
18 :     (* drt (12/15/89) -- the functor should be used during development work,
19 :     but it is wastes space in the release version.
20 :    
21 :     functor ParserGen(structure LrTable : LR_TABLE
22 :     structure Stream : STREAM) : LR_PARSER =
23 :     *)
24 :    
25 : monnier 8 structure LrParser :> LR_PARSER =
26 : monnier 2 struct
27 :     val print = fn s => output(std_out,s)
28 :     val println = fn s => (print s; print "\n")
29 :     structure LrTable = LrTable
30 :     structure Stream = Stream
31 :     structure Token : TOKEN =
32 :     struct
33 :     structure LrTable = LrTable
34 :     datatype ('a,'b) token = TOKEN of LrTable.term * ('a * 'b * 'b)
35 :     val sameToken = fn (TOKEN (t,_),TOKEN(t',_)) => t=t'
36 :     end
37 :    
38 :    
39 :     open LrTable
40 :     open Token
41 :    
42 :     val DEBUG = false
43 :     exception ParseError
44 :    
45 :     type ('a,'b) elem = (state * ('a * 'b * 'b))
46 :     type ('a,'b) stack = ('a,'b) elem list
47 :    
48 :     val showState = fn (STATE s) => ("STATE " ^ (makestring s))
49 :    
50 :     fun printStack(stack: ('a,'b) elem list, n: int) =
51 :     case stack
52 :     of (state, _) :: rest =>
53 :     (print(" " ^ makestring n ^ ": ");
54 :     println(showState state);
55 :     printStack(rest, n+1)
56 :     )
57 :     | nil => ()
58 :    
59 :     val parse = fn {arg : 'a,
60 :     table : LrTable.table,
61 :     lexer : ('_b,'_c) token Stream.stream,
62 :     saction : int * '_c * ('_b,'_c) stack * 'a ->
63 :     nonterm * ('_b * '_c * '_c) * ('_b,'_c) stack,
64 :     void : '_b,
65 :     ec = {is_keyword,preferred_change,
66 :     errtermvalue,showTerminal,
67 :     error,terms,noShift},
68 :     lookahead} =>
69 :     let fun prAction(stack as (state, _) :: _,
70 :     next as (TOKEN (term,_),_), action) =
71 :     (println "Parse: state stack:";
72 :     printStack(stack, 0);
73 :     print(" state="
74 :     ^ showState state
75 :     ^ " next="
76 :     ^ showTerminal term
77 :     ^ " action="
78 :     );
79 :     case action
80 :     of SHIFT s => println ("SHIFT " ^ showState s)
81 :     | REDUCE i => println ("REDUCE " ^ (makestring i))
82 :     | ERROR => println "ERROR"
83 :     | ACCEPT => println "ACCEPT";
84 :     action)
85 :     | prAction (_,_,action) = action
86 :    
87 :     val action = LrTable.action table
88 :     val goto = LrTable.goto table
89 :    
90 :     fun parseStep(next as (TOKEN (terminal, value as (_,leftPos,_)),lexer) :
91 :     ('_b,'_c) token * ('_b,'_c) token Stream.stream,
92 :     stack as (state,_) :: _ : ('_b ,'_c) stack) =
93 :     case (if DEBUG then prAction(stack, next,action(state, terminal))
94 :     else action(state, terminal))
95 :     of SHIFT s => parseStep(Stream.get lexer, (s,value) :: stack)
96 :     | REDUCE i =>
97 :     let val (nonterm,value,stack as (state,_) :: _ ) =
98 :     saction(i,leftPos,stack,arg)
99 :     in parseStep(next,(goto(state,nonterm),value)::stack)
100 :     end
101 :     | ERROR => let val (_,leftPos,rightPos) = value
102 :     in error("syntax error\n",leftPos,rightPos);
103 :     raise ParseError
104 :     end
105 :     | ACCEPT => let val (_,(topvalue,_,_)) :: _ = stack
106 :     val (token,restLexer) = next
107 :     in (topvalue,Stream.cons(token,lexer))
108 :     end
109 :     val next as (TOKEN (terminal,(_,leftPos,_)),_) = Stream.get lexer
110 :     in parseStep(next,[(initialState table,(void,leftPos,leftPos))])
111 :     end
112 :     end;
113 :    
114 :     (* drt (12/15/89) -- this needs to be used only when the parsing engine
115 :     (the code above) is functorized.
116 :    
117 :     structure LrParser = ParserGen(structure LrTable = LrTable
118 :     structure Stream = Stream);
119 :     *)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0