Mercurial > ~dholland > hg > ag > index.cgi
comparison examples/fc/fc5.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 FC5: 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 Event driven version of FC4B. | |
9 | |
10 | |
11 Copyright 1993 Parsifal Software. All Rights Reserved. | |
12 | |
13 This software is provided 'as-is', without any express or implied | |
14 warranty. In no event will the authors be held liable for any damages | |
15 arising from the use of this software. | |
16 | |
17 Permission is granted to anyone to use this software for any purpose, | |
18 including commercial applications, and to alter it and redistribute it | |
19 freely, subject to the following restrictions: | |
20 | |
21 1. The origin of this software must not be misrepresented; you must not | |
22 claim that you wrote the original software. If you use this software | |
23 in a product, an acknowledgment in the product documentation would be | |
24 appreciated but is not required. | |
25 2. Altered source versions must be plainly marked as such, and must not be | |
26 misrepresented as being the original software. | |
27 3. This notice may not be removed or altered from any source distribution. | |
28 */ | |
29 | |
30 [ | |
31 test file mask = "*.fc4" // C1 | |
32 traditional engine /* turn this off for production use */ // C2 | |
33 default token type = double // C3 | |
34 disregard white space // C4 | |
35 lexeme {unsigned number, end of line} // C5 | |
36 event driven // C6 | |
37 ] | |
38 | |
39 eof = -1 | |
40 | |
41 (void) grammar | |
42 -> [temperature?, end of line]..., eof // P1 | |
43 | |
44 (void) temperature | |
45 -> number:c, 'c' + 'C' ={ /* P2 */ | |
46 double f = 9*c/5 + 32; | |
47 printf("%.6g\370F = %.6g\370C = %.6g\370K\n",f,c,c+zero); | |
48 } | |
49 -> number:f, 'f' + 'F' ={ /* P3 */ | |
50 double c = 5*(f-32)/9; | |
51 printf("%.6g\370F = %.6g\370C = %.6g\370K\n",f,c,c+zero); | |
52 } | |
53 -> '+'?, unsigned number:k, 'k' + 'K' ={ /* P3a */ | |
54 double c = k - zero; | |
55 double f = 9*c/5 + 32; | |
56 printf("%.6g\370F = %.6g\370C = %.6g\370K\n", f, c, k); | |
57 } | |
58 -> error // P4 | |
59 | |
60 number | |
61 -> '-', unsigned number:n =-n; // P5 | |
62 -> '+'?, unsigned number:n =n; // P6 | |
63 | |
64 unsigned number | |
65 -> integer, '.'? // P6a | |
66 -> integer:i, '.', fraction:f =i+f; // P6b | |
67 -> '.', fraction:f =f; // P6c | |
68 | |
69 integer | |
70 -> '0-9':d =d-'0'; // P7 | |
71 -> integer:n, '0-9':d =10*n+d-'0'; // P8 | |
72 | |
73 fraction | |
74 -> '0-9':d =(d-'0')/10.; // P9 | |
75 -> '0-9':d, fraction:f =(d-'0' + f)/10.; // P10 | |
76 | |
77 white space | |
78 -> ' ' + '\t' // P11 | |
79 -> "/*", ~eof?..., "*/" // P12 | |
80 | |
81 end of line | |
82 -> ["//", ~(eof + '\n')?...], '\n' // P13 | |
83 | |
84 { /* Embedded C */ | |
85 | |
86 #include <stdlib.h> | |
87 #include <stdio.h> | |
88 | |
89 const double zero = 273.16; | |
90 | |
91 int main(void) { | |
92 init_fc5(); /* Initialize parser */ | |
93 while (fc5_pcb.exit_flag == AG_RUNNING_CODE) { /* Until done */ | |
94 fc5_pcb.input_code = getchar(); /* Hand char to parser */ | |
95 fc5(); /* Process character */ | |
96 } | |
97 return (fc5_pcb.exit_flag - AG_SUCCESS_CODE); /* Return error level */ | |
98 } | |
99 | |
100 } // End of Embedded C |