-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanual.tex
executable file
·1958 lines (1501 loc) · 51.5 KB
/
manual.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[10pt]{article}
\usepackage{code,url}
\topmargin=0in
\headheight=0in
\headsep=0in
\textheight=9in
\oddsidemargin=0in
\evensidemargin=0in
\textwidth=6.5in
\parindent=0in
\parskip=10pt
\tolerance=10000
\title{CM-Lex and CM-Yacc User's Manual \\ \Large
(Version 2.1)}
\author{Karl Crary \\ Carnegie Mellon University}
\date{\today}
\newcommand{\nonterm}[1]{\mbox{$\langle${\it{}#1}$\rangle$}}
\newcommand{\lb}{$[$}
\newcommand{\rb}{$]$}
\newcommand{\OR}{{\it{}or}}
\begin{document}
\maketitle
\section{CM-Lex}
CM-Lex is a lexer generator for Standard ML, OCaml, and Haskell. It converts
a specification file into an SML, OCaml, or Haskell source-code file that
implements a lexer functor. The programmer may then instantiate that
functor with appropriate code for lexing actions to obtain a lexer.
\subsection{The specification file}
A CM-Lex specification consists of a series of directives:
\begin{itemize}
\item
\cd{sml} {\it or\/} \cd{ocaml} {\it or\/} \cd{haskell}
Specifies the language in which the generated lexer should be
implemented. This is a required directive, and it must appear first.
\item
\cd{name \nonterm{identifier}}
Specifies the name of the generated functor for SML/Ocaml lexers,
and the name of the generated module for Haskell lexers. This is
a required directive.
In Haskell and OCaml specifications, the name must be a valid module
identifier, so it must begin with a capital letter. In Haskell, a
hierarchical name is permitted. In SML specifications the name is not
required to be capitalized, but it is standard practice in SML code to
capitalize functor names nonetheless.
\item
\cd{alphabet \nonterm{alphabet size}}
Specifies the size of the input alphabet, which must be a positive
integer. The generated lexer will not recognize any symbols whose
ordinal is greater or equal to the given value. This is a
required directive and it must appear before any {\tt set}, {\tt regexp},
or {\tt function} directives.
\item
\cd{set \nonterm{name} = \nonterm{charset}}
Declares a character set \nonterm{name} with the definition \nonterm{charset}.
\item
\cd{regexp \nonterm{name} = \nonterm{regexp}}
Declares a regular expression \nonterm{name} with the definition \nonterm{regexp}.
\item
\cd{function \nonterm{function name} : \nonterm{type name} = \nonterm{clauses}}
Specifies a lexing function \nonterm{function name} that has type
\nonterm{type name} and is defined by \nonterm{clauses}, where each
clause has the form:
\begin{code}
\nonterm{regexp} => \nonterm{action name}
\end{code}
\item
{\tt monadic}
{\em Haskell specifications only:}
Specifies that CM-Lex should generate a monadic lexer.
\end{itemize}
Comments are written ``\cd{/* {\it{}comment text} */}'' and may appear
anywhere in the specification. Comments may be nested.
\subsubsection{Character sets and regular expressions}
For the purposes of CM-Lex, symbols are identified with nonnegative
integers. Typically the number is the ASCII code of a character, but
establishing the correspondence is left to the programmer.
\begin{tabbing}
\quad \= \cd{(range \nonterm{range-spec} \dots)} \quad\= \kill
\nonterm{symbol} ::=
\\ \>
\nonterm{number} \>
the indicated number
\\ \>
\cd{'} \nonterm{printable character} \>
the ASCII code of the indicated character
\end{tabbing}
Character sets and regular expressions are specified using the Scheme
Shell's SRE notation:
\begin{tabbing}
\quad \= \cd{(range \nonterm{range-spec} \dots)} \quad\= \kill
\nonterm{charset} ::=
\\[1ex] \>
\nonterm{identifier} \>
the indicated character set
\\[1ex] \>
\nonterm{symbol} \>
the singleton set containing \nonterm{symbol}
\\[1ex] \>
\cd{empty} \>
the empty set
\\[1ex] \>
\cd{any} \>
the universal set (all symbols up to $N-1$, where $N$ is the alphabet size)
\\[1ex] \>
\cd{(or \nonterm{charset} \dots)} \>
set union
\\ \>
\cd{(| \nonterm{charset} \dots)}
\\[1ex] \>
\cd{(and \nonterm{charset} \dots)} \>
set intersection
\\ \>
\cd{(& \nonterm{charset} \dots)}
\\[1ex] \>
\cd{(- \nonterm{charset} \dots)} \>
set difference: the first set minus all the following sets
\\[1ex] \>
\cd{(\char"7E{} \nonterm{charset} \dots)} \>
set complement: equivalent to (- any (or \nonterm{charset} \dots))
\\[1ex] \>
\cd{(range \nonterm{range-spec} \dots)} \>
union of the specified ranges
\\ \>
\cd{(/ \nonterm{range-spec} \dots)}
\end{tabbing}
\begin{tabbing}
\quad \= \cd{(range \nonterm{range-spec} \dots)} \quad\= \kill
\nonterm{range-spec} ::=
\\ \>
\nonterm{symbol1} \nonterm{symbol2} \>
the range of symbols from \nonterm{symbol1} to \nonterm{symbol2} (inclusive)
\end{tabbing}
\begin{tabbing}
\quad \= \cd{(** \nonterm{number1} \nonterm{number2} \nonterm{regexp})} \quad\= \kill
\nonterm{regexp} ::=
\\[1ex] \>
\nonterm{identifier} \>
the indicated character set or regular expression
\\[1ex] \>
\nonterm{symbol} \>
match the indicated symbol
\\[1ex] \>
\cd{any} \>
match any character
\\[1ex] \>
\cd{epsilon} \>
match the empty string
\\[1ex] \>
\cd{empty} \>
match nothing
\\[1ex] \>
\cd{eos} \>
match end-of-stream
\\[1ex] \>
\cd{" \nonterm{string} "} \>
\parbox[t]{4in}{match the indicated string: that is, the sequence of symbols given by
the ASCII codes of the characters of \nonterm{string} (the string can \\
contain any printable character other than a quotation mark)}
\\[1ex] \>
\cd{(seq \nonterm{regexp} \dots)} \>
concatenation
\\ \>
\cd{(: \nonterm{regexp} \dots)}
\\[1ex] \>
\cd{(or \nonterm{regexp} \dots)} \>
choice
\\ \>
\cd{(| \nonterm{regexp} \dots)}
\\[1ex] \>
\cd{(? \nonterm{regexp})} \>
zero or one matches
\\[1ex] \>
\cd{(* \nonterm{regexp})} \>
zero or more matches
\\[1ex] \>
\cd{(+ \nonterm{regexp})} \>
one or more matches
\\[1ex] \>
\cd{(= \nonterm{number} \nonterm{regexp})} \>
exactly \nonterm{number} matches
\\[1ex] \>
\cd{(>= \nonterm{number} \nonterm{regexp})} \>
at least \nonterm{number} matches
\\[1ex] \>
\cd{(** \nonterm{number1} \nonterm{number2} \nonterm{regexp})} \>
between \nonterm{number1} and \nonterm{number2} matches (inclusive)
\end{tabbing}
\subsubsection{Function specifications}
A lexing function is specified by the directive:
\begin{code}
function \nonterm{function name} : \nonterm{type name} =
\nonterm{regexp} => \nonterm{action}
\dots
\end{code}
This generates a function named \nonterm{function name} with return type
\nonterm{type name}. That function finds the longest prefix of its input
stream that matches any of the given regular expressions and activates
the corresponding action. If the longest possible match matches two
different regular expressions, CM-Lex prefers the one that appears
earlier.
If a function is inexhaustive ({\em i.e.,} if it is possible for an input
stream to satisfy none of the regular expressions), CM-Lex adds a
default action that raises an exception. The programmer can add an
explicit default action by adding a clause \cd{epsilon => my_default}
to the end of the clause list. This ensures that the function is
exhaustive, because it always can at least match the empty prefix.
An action name can be used more than once, either in the same function
specification, or even in different function specifications, provided
that the functions have the same return type.
\subsection{Invocation}
CM-Lex is invoked from the command line with the specification file's
name as its argument. The desired output file name can be specified
with the \cd{-o} flag. Otherwise, the output file name is derived from from
the input file name by appending the appropriate suffix.
\begin{code}
cmlex foo.cmlex # generates foo.cmlex.sml or foo.cmlex.hs
# or foo.cmlex.ml
cmlex foo.cmlex -o bar.sml # generates bar.sml
cmlex foo.cmlex -o bar.ml # generates bar.ml
cmlex foo.cmlex -o bar.hs # generates bar.hs
\end{code}
\subsection{Streams (Standard ML)}
\label{sec:stream-sml}
The generated lexer takes its input in the form of a lazy stream,
which is a possibly-infinite list in which each element is computed
the first time it is needed, and then memoized for future uses.
A stream type is included in CM-Lib, a library packaged with CM-Lex
and CM-Yacc. The module {\tt Stream} implements the signature {\tt STREAM}, an
abbreviated version of which appears below:
\begin{code}
signature STREAM =
sig
type 'a stream
datatype 'a front = Nil | Cons of 'a * 'a stream
val front : 'a stream -> 'a front
val lazy : (unit -> 'a front) -> 'a stream
end
\end{code}
The {\tt front} function forces the computation of the next element of the
stream, if any. It returns a front, which exposes whether the stream
is empty, and, if not, gives its next element and the remainder of the
stream. The {\tt lazy} function builds a stream from a thunk returning
the stream's front. A thunk is used so that the front need not be
computed until the stream is forced.
The {\tt STREAM} signature also includes a variety of other functions for
building and manipulating streams. For example, {\tt fromString} and
{\tt fromTextInstream} build character streams that read input from a string
and from an SML IO stream, respectively:
\begin{code}
val fromString : string -> char stream
val fromTextInstream : TextIO.instream -> char stream
\end{code}
A lexer need not use streams, it may use any type that implements the
type class {\tt STREAMABLE}, which specifies types that are compatible with
the stream forcing operation, {\tt front}:
\begin{code}
signature STREAMABLE =
sig
type 'a t
datatype 'a front = Nil | Cons of 'a * 'a t
val front : 'a t -> 'a front
end
\end{code}
The canonical implementation of {\tt STREAMABLE} is {\tt
StreamStreamable}, which implements {\tt t} as simply {\tt
Stream.stream}. However, the programmer may also use {\tt
ListStreamable}, which implements {\tt t} as {\tt list}, or any other
way he or she chooses.
\subsection{The generated functor (Standard ML)}
\label{sec:lex-functor-sml}
CM-Lex generates a functor from the given specification file. For example,
consider the specification:
\begin{code}
sml
name LexerFun
alphabet 128
function foo : t =
(* 'a) => astar
function bar : u =
(* 'b) => bstar
\end{code}
CM-Lex generates the functor:
\begin{strictcode}
functor LexerFun
(structure Streamable : STREAMABLE
structure Arg :
sig
type symbol
val ord : symbol -> int
type t
type u
type self = { bar : symbol Streamable.t -> u,
foo : symbol Streamable.t -> t }
type info = { match : symbol list,
len : int,
start : symbol Streamable.t,
follow : symbol Streamable.t,
self : self }
val astar : info -> t
val bstar : info -> u
end)
:>
sig
val bar : Arg.symbol Streamable.t -> Arg.u
val foo : Arg.symbol Streamable.t -> Arg.t
end
= itdots implementation dots
endstrictcode
The functor takes in two arguments. The first structure argument is
an implementation of of the {\tt STREAMABLE} type class for the lexer to
use. This is typically {\tt StreamStreamable} (so {\tt Streamable.t} =
{\tt Stream.stream}), but need not be.
The second structure argument, {\tt Arg}, implements the lexer's
actions. First, the programmer defines the type of the symbols that
the lexer processes. This is typically {\tt char}, but need not be.
Whatever symbol type is chosen, {\tt ord} gives the coercion from
symbols to the numbers that CM-Lex uses to identify them.
Next, the programmer implements the types used for the functions'
result types. The specification uses two type names, {\tt t} and {\tt u}, so
the functor demands {\tt t} and {\tt u} as type arguments.
Next are two type definitions we will return to in a moment, and
finally the programmer implements the lexing actions. The
specification uses two action names, {\tt astar} and {\tt bstar}, so the
functor demands {\tt astar} and {\tt bstar} as value arguments.
Since {\tt astar} was used as an action for {\tt foo}, which was declared to
return type {\tt t}, the {\tt astar} action must return type {\tt t}. Similarly,
{\tt bstar} must return type {\tt u}.
Once given the two structure arguments, the generated functor returns
a structure containing the two lexing functions, {\tt foo} and {\tt bar}.
Each one takes in an {\tt Arg.symbol Streamable.t} (typically a {\tt char
stream}), and returns the appropriate type.
\subsubsection{The match info}
\label{sec:match-info}
When a lexing action is invoked by the lexer, it is passed a record
containing various matching information:
\begin{itemize}
\item
{\tt match}:
the actual symbols that were matched.
\item
{\tt len}:
the length of match.
\item
{\tt follow}:
the remaining stream after the matched symbols.
\item
{\tt start}:
the stream on which the lexer was called ({\em i.e.,} beginning with the
matched symbols). This is useful for re-lexing some or all of the
stream, as in Lex's {\tt yyless} directive.
\item
{\tt self}:
a record containing the lexing functions being defined, so that
actions can reinvoke the lexer.
\end{itemize}
Note that the lexing functor is {\em stateless}. Nowhere does it maintain
the current input stream. Rather, it is the job of the programmer,
through the lexing actions, to manage the input stream.
Typically, an action calls the lexer (through the {\tt self} argument) on
the remaining stream ({\em i.e.,} the {\tt follow} argument). However, the
programmer may manipulate the stream as he or she desires. For
example, one may push additional symbols onto the stream (as in Lex's
{\tt yymore} directive), simply by doing so before calling the lexer.
\subsection{Streams (OCaml)}
We do not use the stream type in the OCaml standard library, which is
imperative. Instead we employ a functional stream type similar to the
streams we employ for Standard ML (Section~\ref{sec:stream-sml}). The
module {\tt FStream} implements streams; its interface is abbreviated
below:
\begin{code}
type 'a stream
type 'a front = Nil | Cons of 'a * 'a stream
type 'a t = 'a stream
val front : 'a stream -> 'a front
val eager : 'a front -> 'a stream
val laz : 'a front Lazy.t -> 'a stream
\end{code}
For lazy stream construction, we use OCaml's {\tt Lazy} module,
instead of a thunk. One can construct a lazy expression using the
{\tt lazy} keyword. Thus, a lazy {\tt Cons} can be written \cd{laz
(lazy (Cons (...)))}.
As in Standard ML, a lexer need not use a stream, it may use any type
that implements the type class \cd{Streamable.S}:
\begin{code}
module type S =
sig
type 'a t
type 'a front = Nil | Cons of 'a * 'a t
val front : 'a t -> 'a front
end
\end{code}
The canonical implementation of \cd{Streamable.S} is
\cd{Streamable.StreamStreamable}, which implements {\tt t} as simply
{\tt FStream.stream}. However, the programmer may also use
\cd{Streamable.ListStreamable}, which implements {\tt t} as {\tt
list}, or any other way he or she chooses.
\subsection{The generated functor (OCaml)}
Consider the specification:
\begin{code}
ocaml
name LexerFun
alphabet 128
function foo : t =
(* 'a) => astar
function bar : u =
(* 'b) => bstar
\end{code}
CM-Lex generates the functor:
\begin{strictcode}
module LexerFun
(Strm : Streamable.S)
(Arg :
sig
type symbol
val ord : symbol -> int
type t
type u
type self = { bar : symbol Strm.t -> u;
foo : symbol Strm.t -> t }
type info = (symbol list, symbol Strm.t, self) LexInfo.t
val astar : info -> t
val bstar : info -> u
end)
:
sig
val bar : Arg.symbol Strm.t -> Arg.u
val foo : Arg.symbol Strm.t -> Arg.t
end
= itdots implementation dots
endstrictcode
This works in a fashion identical to Standard ML lexers
(Section~\ref{sec:lex-functor-sml}). Each action is passed a
\cd{LexInfo.t}, which is defined as follows:
\begin{strictcode}
type ('a, 'b, 'c) t =
{ matc : 'a;
len : int;
start : 'b;
follow : 'b;
self : 'c }
endstrictcode
In particular, \cd{(symbol list, symbol Strm.t, self) LexInfo.t} (as
it is used in the functor) works out to:
\begin{strictcode}
{ matc : symbol list;
len : int;
start : symbol Strm.t;
follow : symbol Strm.t;
self : self }
endstrictcode
The information in the \cd{LexInfo.t} is identical to that in
Section~\ref{sec:match-info}. (The first field is named {\tt matc}
because {\tt match} is a reserved word in OCaml.) Lexing functions
may reinvoke the lexer using the {\tt self} argument or (with a bit
more effort) they can use a recursive module.
\subsection{Streams (Haskell)}
As in Standard ML, Haskell lexers use a type class {\tt Streamable} that
specifies types compatible with the stream forcing operation {\tt front}:
\begin{code}
data Front a b =
Nil
| Cons a b
class Monad m => Streamable s m where
front :: s a -> m (Front a (s a))
\end{code}
A type constructor {\tt s} is streamable (with an associated monad
{\tt m}) if it provides a {\tt front} operation that takes an {\tt s
a} and then --- in the monad {\tt m} --- returns a stream front with head {\tt
a} and tail {\tt s a}. This differs from the {\tt STREAMABLE} class
in Standard ML, which makes no explicit mention of a monad since SML
permits side-effects.
One important instance is lists. Lists are purely functional
streamables, and thus belong to {\tt Streamable} with any monad (but
particularly with {\tt Identity}).
For effectful streams, the canonical instance is the {\tt Stream} type:
\begin{code}
newtype Stream m a =
Stream (m (Front a (Stream m a)))
\end{code}
For example, streams that result from reading input from a file will
typically have the type \cd{Stream Char IO}. Forcing such a stream
will result in an IO effect and return a \cd{Front} containing a
character and the rest of the stream. The effect occurs because it
might be necessary to read the file to obtain the next character.
A {\tt Stream} can be formed from a {\tt Front} in two ways:
\begin{code}
eager :: Monad m => Front a (Stream m a) -> Stream m a
lazy :: MonadMemo m => m (Front a (Stream m a)) -> m (Stream m a)
\end{code}
When a front is held outright, \cd{eager} coerces it to a stream.
However, when one has a front under a monad (the typical case for
monadic lexers), one uses \cd{lazy}, which memoizes the monadic front
before coercing it to a stream. The memoization means that if the
stream is forced more than once, the monadic operation is not
repeated; instead the prior result is recalled. This requires that
the monad belong to the \cd{MonadMemo} class, which includes
\cd{Identity}, \cd{IO}, and \cd{ST}, as well as every monad belonging
to \cd{MonadIO}.
The \cd{Stream} module contains a variety of other functions for
building and manipulating streams. For example, fromList and
fromHandle build character streams that read input from a list (or
string) and from an IO handle, respectively:
\begin{code}
fromList :: Monad m => [a] -> Stream m a
fromHandle :: Handle -> IO (Stream IO Char)
\end{code}
\subsection{The generated functor (Haskell)}
\label{sec:lex-functor-hs}
Instantiating the generated code for Haskell is conceptually similar
to the process for Standard ML. However, Haskell, unlike Standard ML,
does not support functors. Instead, the tool generates an ersatz
functor using polymorphic functions. For example, again consider the
specification from Section~\ref{sec:lex-functor-sml} (now as a Haskell
specification):
\begin{code}
haskell
name LexerFun
alphabet 128
function foo : t =
(* 'a) => astar
function bar : u =
(* 'b) => bstar
\end{code}
CM-Lex generates a module exporting:\footnote{For readability, we
use unqualified names here.}
\begin{strictcode}
data Arg stream symbol t u =
Arg { ord :: symbol -> Int,
{- type arguments -}
t :: Proxy t,
u :: Proxy u,
{- action arguments -}
astar :: LexInfo stream symbol -> t,
bstar :: LexInfo stream symbol -> u }
foo :: Streamable stream Identity
=> Arg stream symbol t u -> stream symbol -> t
bar :: Streamable stream Identity
=> Arg stream symbol t u -> stream symbol -> u
endstrictcode
To use the functor, one constructs an inhabitant of the {\tt Arg}
type. As in the Standard ML version, an argument contains an {\tt
ord} function that converts symbols to integers, implementations of
the type arguments ({\tt t} and {\tt u}) in this example, and
implementations of the actions ({\tt astar} and {\tt bstar} in this
example). One then calls the functions ({\tt foo} and {\tt
bar}) with the argument to obtain the lexing functions.
The type arguments are given by proxy fields using the
\cd{Data.Proxy} module in the Haskell standard library. The terms put into the
proxy fields are not used; their purpose is solely to specify the
types. They are filled in with \cd{Proxy :: Proxy T} for
the desired {\tt T}.
Each action is passed a {\tt LexInfo}, which is defined as follows:
\begin{strictcode}
data LexInfo stream symbol =
LexInfo
{ match :: [symbol],
len :: Int,
start :: stream symbol,
follow :: stream symbol }
endstrictcode
The information in the {\tt LexInfo} is similar to that from
Section~\ref{sec:match-info}. However, note that {\tt LexInfo} does
not include a {\tt self} field. In Haskell a {\tt self} field is not
necessary: since anything can call anything else, no special mechanism
is necessary for an action to reinvoke the lexer.
\subsubsection{Monadic lexers}
If the {\tt monadic} directive is added to the specification, CM-Lex
generates a monadic lexer. In a monadic lexer, the module contains:
\begin{strictcode}
data Arg stream monad symbol t u =
Arg { ord :: symbol -> Int,
{- type arguments -}
monad :: Proxy monad,
t :: Proxy t,
u :: Proxy u,
{- action arguments -}
astar :: LexInfo stream symbol -> monad t,
bstar :: LexInfo stream symbol -> monad u }
foo :: Streamable stream monad
=> Arg stream monad symbol t u -> stream symbol -> monad t
bar :: Streamable stream monad
=> Arg stream monad symbol t u -> stream symbol -> monad u
endstrictcode
Observe that the types of the actions {\tt astar} and {\tt bstar}, and
the lexing functions {\tt foo} and {\tt bar}, now end in \cd{monad
t} and \cd{monad u}, intead of {\tt t} and {\tt u}.
A worked example of how to use this appears in Section~\ref{sec:example-hs}.
\subsection{The automaton listing}
In the generated file, embedded as a comment immediately after the
functor's interface specification, is a listing of each lexing
function's state machine. For each lexing function, the listing gives
the initial state and the total number of states, and for each state,
it gives the state that results from each possible input symbol.
Symbols that are not listed, implicitly transition to the error
state.
\section{CM-Yacc}
CM-Yacc is an LALR(1) parser generator for Standard ML, OCaml, and Haskell.
It converts a specification file into an SML, OCaml, or Haskell source-code
file that implements a parser functor. The programmer may then
instantiate that functor with appropriate code for lexing actions to
obtain a parser.
\subsection{The specification file}
A CM-Yacc specification consists of a series of directives:
\begin{itemize}
\item
\cd{sml} {\it or\/} \cd{ocaml} {\it or\/} \cd{haskell}
Specifies the language in which the generated parser should be
implemented. This is a required directive, and it must appear first.
\item
\cd{name \nonterm{identifier}}
Specifies the name of the generated functor for SML/Ocaml parser,
and the name of the generated module for Haskell parsers. This is
a required directive.
In Haskell and OCaml specifications, the name must be a valid module
identifier, so it must begin with a capital letter. In Haskell, a
hierarchical name is permitted. In SML specifications the name is not
required to be capitalized, but it is standard practice in SML code to
capitalize functor names nonetheless.
\item
\cd{terminal \nonterm{name} \lb of \nonterm{type name} \rb \lb \nonterm{precedence} \rb}
Declares a terminal. If the ``\cd{of \nonterm{type name}}'' form is
used, then the terminal carries a value of type \nonterm{type name}.
Otherwise it carries no value. If the \nonterm{precedence} form is
used, the terminal is marked with a precedence, which is used to
resolve shift-reduce conflicts.
\item
\cd{nonterminal \nonterm{name} : \nonterm{type name} =
\nonterm{production} \dots{} \nonterm{production}}
Declares a nonterminal carrying a value of type \nonterm{type name}.
The nonterminal expands using the productions in \nonterm{production
list}.
\item
\cd{start \nonterm{name}}
Indicates that the nonterminal \nonterm{name} is the start symbol.
This is a required directive.
\item
\cd{follower \nonterm{name}}
Indicates that the terminal \nonterm{name} is permitted to follow
the start symbol. (In other words, it is permitted to be the first
symbol after a complete parse.) If no followers are given, only the
end of the stream is permitted to follow the start symbol.
\item
\cd{data \nonterm{identifier}}
{\em Haskell specifications only:}
Specifies the name that the tool should give to the datatype of
terminals. This is a required directive. (In Standard ML, the
datatype of terminals is always named \cd{terminal}.)
\item
{\tt monadic}
{\em Haskell specifications only:}
Specifies that CM-Lex should generate a monadic parser.
\end{itemize}
Comments are written ``\cd{/* {\it{}comment text} */}'' and may appear
anywhere in the specification. Comments may be nested.
\subsubsection{Productions}
Each production has the form:
\begin{tabbing}
\quad \= \kill
\nonterm{production} ::=
\\ \>
\cd{\nonterm{right-hand-side} \cd{=>} \nonterm{action} \lb \nonterm{precedence} \rb}
\end{tabbing}
\begin{tabbing}
\quad \= \kill
\nonterm{right-hand-side} ::=
\\ \>
\cd{\nonterm{constituent} \ldots \nonterm{constituent}}
\end{tabbing}
\begin{tabbing}
\quad \= \kill
\nonterm{constituent} ::=
\\ \>
\nonterm{terminal-or-nonterminal}
\\ \>
\cd{\nonterm{label} : \nonterm{terminal-or-nonterminal}}
\\ \>
\cd{( \nonterm{constituent} )}
\end{tabbing}
\begin{tabbing}
\quad \= \kill
\nonterm{label} ::=
\\ \>
\nonterm{positive integer}
\\ \>
\nonterm{identifier}
\end{tabbing}
Each production gives a right-hand-side (made up of terminals and
nonterminals) into which the nonterminal being defined can expand.
The right-hand-side may be empty. When a production is used, the
action is called to convert the data carried by the right-hand-side's
constituents into the data carried by the nonterminal.
The labels on constituents determine which constituents' data are
passed to the action. If a constituent omits the label, it passes no
data to the action. If no constituents pass data to the action, the
domain type of the action function will be {\tt unit} ({\tt ()} in
Haskell). Otherwise, the labels determine the action's domain type.
A label is either a positive integer or an identifier. In a single
production, all the labels must be of the same sort ({\em i.e.,} all
numbers or all identifiers). If the labels are identifiers, the
constituents' data are passed in a record, and the labels are used as
the names of the record's fields. If the labels are numbers, all the
numbers from $1$ to $n$ must be used, and the action's domain is an
$n$-tuple. The numbers indicate the order in which the constituents'
data appear in the tuple. {\em Except:} If only the label $1$ is
used, the datum is passed bare, not in a $1$-tuple.
For example, with the directive:
\begin{code}
nonterminal Production : production =
2:Constituents ARROW 1:Ident 3:Precedence => single_production
\end{code}
\noindent
the action \cd{single_production} has type \cd{ident * constitutents *
precedence -> production} (assuming that \cd{Ident}, \cd{Constituents}, and
\cd{Precedence} are declared to carry types \cd{ident},
\cd{constitutents}, and \cd{precedence}, respectively).
\subsubsection{Precedence}
If precedences are used, the tool consults them to resolve
shift-reduce conflicts. They have the form:
\begin{tabbing}
\nonterm{precedence} ::=
\\
\quad \=
{\tt precl} \nonterm{number} \quad
\= left associative with precedence \nonterm{number}
\\ \>
{\tt precr} \nonterm{number} \>
right associative with precedence \nonterm{number}
\\ \>
{\tt noprec} \>
no precedence
\end{tabbing}
Precedences range from 0 to 100, with higher numbers indicating higher
precedence ({\em i.e.,} binding more tightly). For a terminal, a {\tt
noprec} precedence is equivalent to omitting the precedence. For a
production, if no precedence is given, the production's precedence is
inferred from the precedence of its last terminal. A production has no precedence
if it is given precedence {\tt noprec}, if it contains no terminals,
or if its last terminal does not have a precedence.
\subsection{Invocation}
CM-Yacc is invoked from the command line with the specification file's
name as its argument. The desired output file name can be specified
with the \cd{-o} flag. Otherwise, the output file name is derived from from
the input file name by appending the appropriate suffix.
\begin{code}
cmyacc foo.cmyacc # generates foo.cmyacc.sml or foo.cmyacc.hs
# or foo.cmyacc.ml
cmyacc foo.cmyacc -o bar.sml # generates bar.sml
cmyacc foo.cmyacc -o bar.ml # generates bar.ml
cmyacc foo.cmyacc -o bar.hs # generates bar.hs
\end{code}
\subsection{The generated functor (Standard ML)}
\label{sec:yacc-functor-sml}
CM-Yacc generates a functor from the given specification file. For example,
consider the specification:
\begin{code}
sml
name ParserFun
terminal NUMBER of t
terminal PLUS
terminal TIMES
nonterminal Factor : t =
1:NUMBER => number_factor
1:NUMBER TIMES 2:Factor => times_factor
nonterminal Term : t =
1:Factor => factor_term
1:Factor PLUS 2:Term => plus_term
start Term
\end{code}
CM-Yacc generates the functor:
\begin{strictcode}
functor ParserFun