Debian Codegraph @

lib/mllpt-lib/ml-lpt/doc/example.tex

raw
1 %!TEX root = manual.tex
2 %
3 \chapter{A complete example}
4 \label{ch:example}
5
6 This chapter gives a complete example of a simple calculator
7 language implemented using both \texttt{ml-ulex} and \texttt{ml-antlr}.
8 The language has the following syntax:
9 \begin{displaymath}
10 \begin{array}{rcl}
11 E & ::= & \mathtt{let}\;\mathrm{id}\;\mathtt{in}\;E \\
12 & \mid & E \;\mathtt{+}\; E \\
13 & \mid & E \;\mathtt{*}\; E \\
14 & \mid & \mathtt{-}\;E \\
15 & \mid & \mathrm{id} \\
16 & \mid & \mathrm{num}
17 \end{array}%
18 \end{displaymath}%
19 The lexical conventions allow arbitrary whitespace between tokens.
20 Numbers ($\mathrm{num}$) numbers are unsigned decimal numbers
21 and identifiers ($\mathrm{id}$) begin with a letter followed by letters and digits.
22 The expression forms are listed in order of increasing precedence:
23 let-expressions have the lowest precedence; then addition, multiplication, and negation.
24 The calculator will compute and return the value of the expression.
25
26 Figure~\ref{fig:calc-sources} gives the CM file for the project.
27 %Note that we are assuming that the \texttt{ml-ulex} and \texttt{ml-antlr} tools have been run by hand.
28 \begin{figure}[h]
29 \begin{lstlisting}[language=CM]
30 Library
31
32 structure CalcLexer
33 functor CalcParseFn
34 structure CalcTest
35
36 is
37 $/basis.cm
38 $/smlnj-lib.cm
39 $/ml-lpt-lib.cm
40
41 calc.grm : ml-antlr
42 calc.lex : ml-ulex
43 calc-test.sml
44 \end{lstlisting}%
45 \caption{The CM file: {\tt sources.cm}}
46 \label{fig:calc-sources}
47 \end{figure}%
48
49 \begin{figure}
50
51 \begin{lstlisting}[language=MLULex]
52 %name CalcLexer;
53
54 %let digit = [0-9];
55 %let int = {digit}+;
56 %let alpha = [a-zA-Z];
57 %let id = {alpha}({alpha} | {digit})*;
58
59 %defs (
60 structure T = CalcTokens
61 type lex_result = T.token
62 fun eof() = T.EOF
63 );
64
65 let => ( T.KW_let );
66 in => ( T.KW_in );
67 {id} => ( T.ID yytext );
68 {int} => ( T.NUM (valOf (Int.fromString yytext)) );
69 "=" => ( T.EQ );
70 "+" => ( T.PLUS );
71 "-" => ( T.MINUS );
72 "*" => ( T.TIMES );
73 "(" => ( T.LP );
74 ")" => ( T.RP );
75 " " | \n | \t
76 => ( skip() );
77 . => ( (* handle error *) );
78 \end{lstlisting}
79 \caption{The ml-ulex specification: \texttt{calc.lex}}
80 \label{fig:calc-lex}
81 \end{figure}%
82
83 \begin{figure}[p]
84 \begin{lstlisting}[language=SML]
85 %name Calc;
86
87 %tokens
88 : KW_let ("let") | KW_in ("in")
89 | ID of string | NUM of Int.int
90 | EQ ("=") | PLUS ("+")
91 | TIMES ("*") | MINUS ("-")
92 | LP ("(") | RP (")")
93 ;
94
95 exp(env)
96 : "let" ID "=" exp@(env)
97 "in" exp@(AtomMap.insert(env, Atom.atom ID, exp1))
98 => ( exp2 )
99 | addExp@(env)
100 ;
101
102 addExp(env)
103 : multExp@(env) ("+" multExp@(env))*
104 => ( List.foldr op+ 0 (multExp::SR) )
105 ;
106
107 multExp(env)
108 : prefixExp@(env) ("*" prefixExp@(env))*
109 => ( List.foldr op* 1 (prefixExp::SR) )
110 ;
111
112 prefixExp(env)
113 : atomicExp@(env)
114 | "-" prefixExp@(env)
115 => ( ~prefixExp )
116 ;
117
118 atomicExp(env)
119 : ID
120 => ( valOf(AtomMap.find (env, Atom.atom ID)) )
121 | NUM
122 | "(" exp@(env) ")"
123 ;
124 \end{lstlisting}
125 \caption{The ml-antlr specification: \texttt{calc.grm}}
126 \label{fig:calc-grm}
127 \end{figure}%
128
129 \begin{figure}
130 \begin{lstlisting}[language=SML]
131 structure CalcTest =
132 struct
133
134 structure CP = CalcParseFn(CalcLexer)
135
136 fun tok2s (ID s) = s
137 | tok2s (NUM n) = Int.toString n
138 | tok2s tok = CalcTokens.toString tok
139
140 (* val calc : TextIO.instream -> Int.int *)
141 fun calc instrm = let
142 val sm = AntlrStreamPos.mkSourcemap()
143 val lex = CalcLexer.lex sm
144 val strm = CalcLexer.streamifyInstream instrm
145 val (r, strm', errs) = CP.parse lex AtomMap.empty strm
146 in
147 print (String.concatWith "\n"
148 (List.map (AntlrRepair.repairToString tok2s sm)
149 errs));
150 r
151 end
152
153 end
154 \end{lstlisting}
155 \caption{The driver: {\tt calc-test.sml}}
156 \label{fig:calc-test}
157 \end{figure}%