diff tests/agcl/parsifal/sort.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/parsifal/sort.syn	Sat Dec 22 17:52:45 2007 -0500
@@ -0,0 +1,133 @@
+{
+  class String {
+    char *value;
+  public:
+    String();
+    String(unsigned char *);
+    int operator < (const String &);
+  };
+}
+
+char = 33..127 - '<' - '>'
+blank = 32 + '\t'
+eof = 0
+
+[
+  context type = unsigned char *
+]
+
+(unsigned char *) words
+ -> word                          =PCB.pointer;
+ -> words, blank..., word         =PCB.pointer;
+
+word = char...
+
+(String) text
+ -> words:p, blank?...            =String(p);
+
+(String) address
+ -> '<', blank?..., words:w, blank?..., '>'          =w;
+
+line
+ -> blank?..., text:t                                  =map[t] = t;
+ -> blank?..., text, address:a                         =map[a] = String();
+ -> blank?..., address:a, text                         =map[a] = String();
+
+grammar
+ -> line?..., eof
+
+{
+  #include <string.h>
+  #include <stdio.h>
+  #include "agmap.h"
+
+
+  #define GET_CONTEXT CONTEXT = PCB.pointer
+
+  AgMap<String, String> map;
+
+  String::String() : value(new char[PCB.pointer - CONTEXT + 1]) {
+    int length = PCB.pointer - CONTEXT;
+    strncpy(value, CONTEXT, length);
+    value[length] = 0;
+  }
+
+  String::String(unsigned char *p) : value(new char[p - CONTEXT + 1]) {
+    int length = p - CONTEXT;
+    strncpy(value, CONTEXT, length);
+    value[length] = 0;
+  }
+
+  int operator < (const String &s) {
+    return strcmp(value, s.value) < 0;
+  }
+
+  enum ConditionCode {
+    success,
+    incorrectArgumentCount,
+    cannotOpenInput,
+    cannotSeek,
+    ftellFailure,
+    insufficientMemory,
+    readError,
+    parseError
+  };
+
+ int main(int argc, char *argv[]) {
+
+    /* Check for enough arguments */
+    if (argc != 2) {
+      printf("Usage: %s <filename>\n", argv[0]);
+      return incorrectArgumentCount;
+    }
+
+    /* Open input file */
+    FILE *input = fopen(argv[1],"r");
+    if (input == NULL) {
+      printf("Cannot open %s\n", argv[1]);
+      return cannotOpenInput;
+    }
+
+    /* find out how big the file is */
+    if (fseek(input, SEEK_SET, SEEK_END)) {
+      printf("Seek error on %s\n", argv[1]);
+      return cannotSeek;
+    }
+    long fileLength = ftell(input);
+    if (fileLength < 0 ) {    // -1L is error return
+      printf("Error getting file length (%d) of %s\n", fileLength, argv[1]);
+      return ftellFailure;
+    }
+    /* fseek to beginning of file */
+    if (fseek(input, 0, SEEK_SET)) {
+      printf("Seek error on %s\n", argv[1]);
+      return cannotSeek;
+    }
+
+    /* Allocate storage for input string */
+    char *scriptString = new char[(size_t) fileLength + 1];
+    if (scriptString == NULL) {
+      printf("Insufficient memory\n");
+      return insufficientMemory;
+    }
+
+    /* read file */
+    size_t stringLength = fread(scriptString, 1, (unsigned)fileLength, input);
+    if (stringLength == 0) {
+      printf("Unable to read %s\n", argv[1]);
+      delete [] scriptString;
+      fclose(input);
+      return readError;
+    }
+    scriptString[stringLength] = 0;            // Terminate string with null
+    fclose(input);
+
+    PCB.pointer = scriptString;
+    sort();
+
+
+
+    return success;
+  }
+
+}