1 (* ML
-Yacc Parser
Generator (c
) 1989 Andrew W
. Appel
, David R
. Tarditi
*)
3 (* functor Join creates a user parser by putting together a Lexer
structure,
4 an LrValues
structure, and a polymorphic parser
structure. Note that
5 the Lexer
and LrValues
structure must share the
type pos (i
.e
. the
type
6 of line numbers
), the
type svalues for semantic values
, and the
type
10 functor Join(structure Lex
: LEXER
11 structure ParserData
: PARSER_DATA
12 structure LrParser
: LR_PARSER
13 sharing ParserData
.LrTable
= LrParser
.LrTable
14 sharing ParserData
.Token
= LrParser
.Token
15 sharing type Lex
.UserDeclarations
.svalue
= ParserData
.svalue
16 sharing type Lex
.UserDeclarations
.pos
= ParserData
.pos
17 sharing type Lex
.UserDeclarations
.token
= ParserData
.Token
.token
)
20 structure Token
= ParserData
.Token
21 structure Stream
= LrParser
.Stream
23 exception ParseError
= LrParser
.ParseError
25 type arg
= ParserData
.arg
26 type pos
= ParserData
.pos
27 type result
= ParserData
.result
28 type svalue
= ParserData
.svalue
29 val makeLexer
= LrParser
.Stream
.streamify
o Lex
.makeLexer
30 val parse
= fn (lookahead
,lexer
,error
,arg
) =>
31 (fn (a
,b
) => (ParserData
.Actions
.extract a
,b
))
32 (LrParser
.parse
{table
= ParserData
.table
,
35 saction
= ParserData
.Actions
.actions
,
37 void
= ParserData
.Actions
.void
,
38 ec
= {is_keyword
= ParserData
.EC
.is_keyword
,
39 noShift
= ParserData
.EC
.noShift
,
40 preferred_change
= ParserData
.EC
.preferred_change
,
41 errtermvalue
= ParserData
.EC
.errtermvalue
,
43 showTerminal
= ParserData
.EC
.showTerminal
,
44 terms
= ParserData
.EC
.terms
}}
46 val sameToken
= Token
.sameToken
49 (* functor JoinWithArg creates a variant
of the parser
structure produced
50 above
. In this
case, the makeLexer take an additional argument
before
51 yielding a value
of type unit
-> (svalue
,pos
) token
54 functor JoinWithArg(structure Lex
: ARG_LEXER
55 structure ParserData
: PARSER_DATA
56 structure LrParser
: LR_PARSER
57 sharing ParserData
.LrTable
= LrParser
.LrTable
58 sharing ParserData
.Token
= LrParser
.Token
59 sharing type Lex
.UserDeclarations
.svalue
= ParserData
.svalue
60 sharing type Lex
.UserDeclarations
.pos
= ParserData
.pos
61 sharing type Lex
.UserDeclarations
.token
= ParserData
.Token
.token
)
64 structure Token
= ParserData
.Token
65 structure Stream
= LrParser
.Stream
67 exception ParseError
= LrParser
.ParseError
69 type arg
= ParserData
.arg
70 type lexarg
= Lex
.UserDeclarations
.arg
71 type pos
= ParserData
.pos
72 type result
= ParserData
.result
73 type svalue
= ParserData
.svalue
75 val makeLexer
= fn s
=> fn arg
=>
76 LrParser
.Stream
.streamify (Lex
.makeLexer s arg
)
77 val parse
= fn (lookahead
,lexer
,error
,arg
) =>
78 (fn (a
,b
) => (ParserData
.Actions
.extract a
,b
))
79 (LrParser
.parse
{table
= ParserData
.table
,
82 saction
= ParserData
.Actions
.actions
,
84 void
= ParserData
.Actions
.void
,
85 ec
= {is_keyword
= ParserData
.EC
.is_keyword
,
86 noShift
= ParserData
.EC
.noShift
,
87 preferred_change
= ParserData
.EC
.preferred_change
,
88 errtermvalue
= ParserData
.EC
.errtermvalue
,
90 showTerminal
= ParserData
.EC
.showTerminal
,
91 terms
= ParserData
.EC
.terms
}}
93 val sameToken
= Token
.sameToken