Mercurial > ~dholland > hg > ag > index.cgi
comparison tests/agcl/ffcalc/ffc-re.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 {/* FOUR FUNCTION CALCULATOR: FFCALC.SYN */} | |
2 | |
3 // -- CONFIGURATION SECTION ---------------------------- | |
4 [ | |
5 default token type = double | |
6 disregard white space | |
7 lexeme { real} | |
8 event driven | |
9 reentrant parser | |
10 mutex type = HANDLE | |
11 ] | |
12 | |
13 // -- FOUR FUNCTION CALCULATOR ------------------------- | |
14 (void) calculator $ | |
15 -> [calculation?, '\n']..., eof | |
16 | |
17 (void) calculation | |
18 -> expression:x =printf("%g\n",x); | |
19 -> name:n, '=', expression:x ={ | |
20 printf("%c = %g\n",n+'A',value[n]=x);} | |
21 -> error | |
22 | |
23 expression | |
24 -> term | |
25 -> expression:x, '+', term:t = x+t; | |
26 -> expression:x, '-', term:t = x-t; | |
27 | |
28 term | |
29 -> factor | |
30 -> term:t, '*', factor:f = t*f; | |
31 -> term:t, '/', factor:f = t/f; | |
32 | |
33 factor | |
34 -> name:n = value[n]; | |
35 -> real | |
36 -> '(', expression:x, ')' = x; | |
37 -> '-', factor:f = -f; | |
38 | |
39 // -- LEXICAL UNITS ------------------------------------ | |
40 digit = '0-9' | |
41 eof = 0 | |
42 | |
43 (void) white space | |
44 -> ' ' + '\t' + '\r' + '\f' + '\v' | |
45 -> "/*", ~eof?..., "*/" | |
46 | |
47 (int) name | |
48 -> 'a-z' + 'A-Z':c = c-'A'; | |
49 | |
50 real | |
51 -> integer part:i, '.', fraction part:f = i+f; | |
52 -> integer part, '.'? | |
53 -> '.', fraction part:f = f; | |
54 | |
55 integer part | |
56 -> digit:d = d-'0'; | |
57 -> integer part:x, digit:d = 10*x + d-'0'; | |
58 | |
59 fraction part | |
60 -> digit:d =(d-'0')/10.; | |
61 -> digit:d, fraction part:f =(d-'0' + f)/10.; | |
62 | |
63 { /* -- EMBEDDED C ---------------------------------- */ | |
64 #include <stdio.h> | |
65 | |
66 double value[64]; /* registers */ | |
67 int main(int argc, char *argv[]) { | |
68 FILE *input; | |
69 long length; | |
70 char *buf; | |
71 ffcalce_pcb_type pcb; | |
72 int i; | |
73 if (argc < 2) { | |
74 printf("Usage: ffcalcp <file name>\n"); | |
75 return 1; | |
76 } | |
77 input = fopen(argv[1], "r"); | |
78 if (input == 0) { | |
79 printf("Cannot open %s\n", argv[1]); | |
80 return 2; | |
81 } | |
82 fseek(input, 0, SEEK_END); | |
83 length = ftell(input); | |
84 fseek(input, 0, SEEK_SET); | |
85 buf = malloc(length); | |
86 length = fread(buf, 1, length, input); | |
87 buf[length++] = 0; | |
88 init_ffcalce(&pcb); | |
89 for (i = 0; i < length && pcb.exit_flag == AG_RUNNING_CODE; i++) { | |
90 pcb.input_code = (unsigned) buf[i]; | |
91 ffcalce(&pcb); | |
92 } | |
93 return 0; | |
94 } | |
95 } /* -- END OF EMBEDDED C ----------------------------*/ |