comparison examples/fc/fc4b.syn @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /*
2 AnaGram Programming Examples
3 FC4B: Multiple Fahrenheit-Celsius conversion with signed numbers,
4 floating point arithmetic, optional white space, and Kelvin
5 temperature input.
6
7 This grammar resolves the conflicts found in FC4.
8
9
10 Copyright 1993 Parsifal Software. All Rights Reserved.
11
12 This software is provided 'as-is', without any express or implied
13 warranty. In no event will the authors be held liable for any damages
14 arising from the use of this software.
15
16 Permission is granted to anyone to use this software for any purpose,
17 including commercial applications, and to alter it and redistribute it
18 freely, subject to the following restrictions:
19
20 1. The origin of this software must not be misrepresented; you must not
21 claim that you wrote the original software. If you use this software
22 in a product, an acknowledgment in the product documentation would be
23 appreciated but is not required.
24 2. Altered source versions must be plainly marked as such, and must not be
25 misrepresented as being the original software.
26 3. This notice may not be removed or altered from any source distribution.
27 */
28
29 [
30 test file mask = "*.fc4" // C1
31 traditional engine /* turn this off for production use */ // C2
32 default token type = double // C3
33 disregard white space // C4
34 lexeme {unsigned number, end of line} // C5
35 ]
36
37 eof = -1
38
39 (void) grammar
40 -> [temperature?, end of line]..., eof // P1
41
42 (void) temperature
43 -> number:c, 'c' + 'C' ={ /* P2 */
44 double f = 9*c/5 + 32;
45 printf("%.6g\370F = %.6g\370C = %.6g\370K\n",f,c,c+273.16);
46 }
47 -> number:f, 'f' + 'F' = { /* P3 */
48 double c = 5*(f-32)/9;
49 printf("%.6g\370F = %.6g\370C = %.6g\370K\n",f,c,c+273.16);
50 }
51 -> '+'?, unsigned number:k, 'k' + 'K' ={ /* P3a */
52 double c = k - 273.16;
53 double f = 9*c/5 + 32;
54 printf("%.6g\370F = %.6g\370C = %.6g\370K\n", f, c, k);
55 }
56 -> error // P4
57
58 number
59 -> '-', unsigned number:n =-n; // P5
60 -> '+'?, unsigned number:n =n; // P6
61
62 unsigned number
63 -> integer, '.'? // P6a
64 -> integer:i, '.', fraction:f =i+f; // P6b
65 -> '.', fraction:f =f; // P6c
66
67 integer
68 -> '0-9':d =d-'0'; // P7
69 -> integer:n, '0-9':d =10*n+d-'0'; // P8
70
71 fraction
72 -> '0-9':d =(d-'0')/10.; // P9
73 -> '0-9':d, fraction:f =(d-'0' + f)/10.; // P10
74
75 white space
76 -> ' ' + '\t' // P11
77 -> "/*", ~eof?..., "*/" // P12
78
79 end of line
80 -> ["//", ~(eof + '\n')?...], '\n' // P13