diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/agcl/ffcalc/ffc-re.syn	Sat Dec 22 17:52:45 2007 -0500
@@ -0,0 +1,95 @@
+{/*   FOUR FUNCTION CALCULATOR: FFCALC.SYN           */}
+
+// -- CONFIGURATION SECTION ----------------------------
+[
+  default token type = double
+  disregard white space
+  lexeme { real}
+  event driven
+  reentrant parser
+  mutex type = HANDLE
+]
+
+// -- FOUR FUNCTION CALCULATOR -------------------------
+(void) calculator $
+ -> [calculation?, '\n']..., eof
+
+(void) calculation
+ -> expression:x                      =printf("%g\n",x);
+ -> name:n, '=', expression:x                         ={
+                  printf("%c = %g\n",n+'A',value[n]=x);}
+ -> error
+
+expression
+ -> term
+ -> expression:x, '+', term:t                     = x+t;
+ -> expression:x, '-', term:t                     = x-t;
+
+term
+ -> factor
+ -> term:t, '*', factor:f                         = t*f;
+ -> term:t, '/', factor:f                         = t/f;
+
+factor
+ -> name:n                                   = value[n];
+ -> real
+ -> '(', expression:x, ')'                          = x;
+ -> '-', factor:f                                  = -f;
+
+// -- LEXICAL UNITS ------------------------------------
+digit   = '0-9'
+eof     = 0
+
+(void) white space
+ -> ' ' + '\t' + '\r' + '\f' + '\v'
+ -> "/*", ~eof?..., "*/"
+
+(int) name
+ -> 'a-z' + 'A-Z':c                             = c-'A';
+
+real
+ -> integer part:i, '.', fraction part:f          = i+f;
+ -> integer part, '.'?
+ -> '.', fraction part:f                            = f;
+
+integer part
+ -> digit:d                                     = d-'0';
+ -> integer part:x, digit:d              = 10*x + d-'0';
+
+fraction part
+ -> digit:d                                =(d-'0')/10.;
+ -> digit:d, fraction part:f           =(d-'0' + f)/10.;
+
+{ /* -- EMBEDDED C ---------------------------------- */
+  #include <stdio.h>
+
+  double value[64];                      /* registers */
+  int main(int argc, char *argv[]) {
+    FILE *input;
+    long length;
+    char *buf;
+    ffcalce_pcb_type pcb;
+    int i;
+    if (argc < 2) {
+      printf("Usage: ffcalcp <file name>\n");
+      return 1;
+    }
+    input = fopen(argv[1], "r");
+    if (input == 0) {
+      printf("Cannot open %s\n", argv[1]);
+      return 2;
+    }
+    fseek(input, 0, SEEK_END);
+    length = ftell(input);
+    fseek(input, 0, SEEK_SET);
+    buf = malloc(length);
+    length = fread(buf, 1, length, input);
+    buf[length++] = 0;
+    init_ffcalce(&pcb);
+    for (i = 0; i < length && pcb.exit_flag == AG_RUNNING_CODE; i++) {
+      pcb.input_code = (unsigned) buf[i];
+      ffcalce(&pcb);
+    }
+    return 0;
+  }
+} /* -- END OF EMBEDDED C ----------------------------*/