comparison tests/agcl/ffcalc/ffc-rp.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 pointer input
9 reentrant parser
10 mutex type = IPrivateResource
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 ffcalcp_pcb_type pcb;
72 if (argc < 2) {
73 printf("Usage: ffcalcp <file name>\n");
74 return 1;
75 }
76 input = fopen(argv[1], "r");
77 if (input == 0) {
78 printf("Cannot open %s\n", argv[1]);
79 return 2;
80 }
81 fseek(input, 0, SEEK_END);
82 length = ftell(input);
83 fseek(input, 0, SEEK_SET);
84 buf = malloc(length+1);
85 length = fread(buf, 1, length, input);
86 buf[length] = 0;
87 pcb.pointer = (unsigned char *) buf;
88 ffcalcp(&pcb);
89 return 0;
90 }
91 } /* -- END OF EMBEDDED C ----------------------------*/