changeset 80:0a452d69f0e2

remove global variable in main.cc
author Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
date Wed, 07 Oct 2015 16:08:34 +0900
parents 52da06c3f050
children 27883946b2dc
files c/regexParser/main.cc
diffstat 1 files changed, 66 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/c/regexParser/main.cc	Tue Sep 29 18:36:31 2015 +0900
+++ b/c/regexParser/main.cc	Wed Oct 07 16:08:34 2015 +0900
@@ -11,28 +11,20 @@
 #include <string.h>
 #include "regexParser.h"
 
-unsigned char *ptr;
-unsigned char tokenType;
-int tokenValue;
+typedef struct regexInfo {
+    unsigned char *ptr;
+    unsigned char tokenType;
+    int tokenValue;
+} RegexInfo, *RegexInfoPtr;
 
 NodePtr charClass();
 NodePtr group();
-NodePtr regex();
+NodePtr regex(RegexInfoPtr);
 NodePtr createNode(unsigned char,NodePtr,NodePtr);
 void token();
 NodePtr regexAtom();
 extern void printTree(NodePtr);
 
-
-bool isLiteral(char c) {
-    if (*ptr > 0x7f) return true;
-    else if (*ptr == '(') return false;
-    else if (*ptr == '[') return false;
-    else if (*ptr == '|') return false;
-    else if (*ptr == '*') return false;
-    return true;
-}
-
 /**
  * Create a node of regex parse tree.
  *     tokenType
@@ -49,66 +41,66 @@
 }
 
 // <charClass> ::= '['<literal>'-'<literal>']'
-NodePtr charClass() {
+NodePtr charClass(RegexInfoPtr ri) {
     NodePtr n = (NodePtr)malloc(sizeof(Node));
-    unsigned char startChar = *ptr;
-    while (*ptr == '-') {
-        ptr++;
+    unsigned char startChar = ri->ptr[0];
+    while (ri->ptr[0] == '-') {
+        ri->ptr++;
     }
-    unsigned char endChar = *ptr;
+    unsigned char endChar = ri->ptr[0];
     unsigned char *charTable = (unsigned char*)malloc(sizeof(char)*256);
 
     return n;
 }
 
 // <literal> ::= [a-z][A-Z][0-9]
-NodePtr literal() {
-    NodePtr n = createNode(*ptr,0,0);
-    ptr++;
+NodePtr literal(RegexInfoPtr ri) {
+    NodePtr n = createNode(ri->ptr[0],0,0);
+    ri->ptr++;
     return n;
 }
 
 // <group> ::= '('<regex>')'
-NodePtr group() {
-    return regex();
+NodePtr group(RegexInfoPtr ri) {
+    return regex(ri);
 }
 
 
 
-void token() {
-    while (*ptr != '\0') {
-        if (*ptr == '('){
-            ptr++;
-            tokenType = '(';
-            tokenValue = 0;
-            if (ptr[1] == ')') {
-                ptr++;
+void token(RegexInfoPtr ri) {
+    while (ri->ptr[0] != '\0') {
+        if (ri->ptr[0] == '('){
+            ri->ptr++;
+            ri->tokenType = '(';
+            ri->tokenValue = 0;
+            if (ri->ptr[1] == ')') {
+                ri->ptr++;
             }
             return;
-        } else if (*ptr == ')') {
-            ptr++;
-            tokenType = ')';
-            tokenValue = *ptr;
+        } else if (ri->ptr[0] == ')') {
+            ri->ptr++;
+            ri->tokenType = ')';
+            ri->tokenValue = ri->ptr[0];
             return;
-        } else if (*ptr == '[') {
-            ptr++;
-            tokenType = '[';
-            tokenValue = *ptr;
-            if (ptr[1] == ']') {
-                ptr++;
+        } else if (ri->ptr[0] == '[') {
+            ri->ptr++;
+            ri->tokenType = '[';
+            ri->tokenValue = ri->ptr[0];
+            if (ri->ptr[1] == ']') {
+                ri->ptr++;
             }
             return;
-        } else if (*ptr == '|'){
-            ptr++;
-            tokenType = '|';
-            tokenValue = 0;
+        } else if (ri->ptr[0] == '|'){
+            ri->ptr++;
+            ri->tokenType = '|';
+            ri->tokenValue = 0;
             return;
-        } else if (*ptr == '*'){
-            ptr++;
-            tokenType = '*';
-            tokenValue = 0;
+        } else if (ri->ptr[0] == '*'){
+            ri->ptr++;
+            ri->tokenType = '*';
+            ri->tokenValue = 0;
             return;
-        } else if (*ptr == '\\'){
+        } else if (ri->ptr[0] == '\\'){
             // need more proccesing 
             /*
                 \277
@@ -118,43 +110,43 @@
                 \utf-8 etc...
             */
         } else {
-            tokenType = 'a';
-            tokenValue = *ptr;
+            ri->tokenType = 'a';
+            ri->tokenValue = ri->ptr[0];
             return;
         }
     }
 
-    tokenType = 0;
-    tokenValue = 0;
+    ri->tokenType = 0;
+    ri->tokenValue = 0;
     return;
 }
 
 // <regexAtom> ::= <literal>|<charClass>|<group>
-NodePtr regexAtom() {
+NodePtr regexAtom(RegexInfoPtr ri) {
 
-    token();
+    token(ri);
     NodePtr n = NULL;
-    if (tokenType == 'a') n = literal();
-    else if (tokenType == '[') n = charClass();
-    else if (tokenType == '(') n = group();
+    if (ri->tokenType == 'a') n = literal(ri);
+    else if (ri->tokenType == '[') n = charClass(ri);
+    else if (ri->tokenType == '(') n = group(ri);
 
     return n;
 }
 
 // <regex> ::= <regexAtom>|<regexAtom>'*'|<regexAtom>'|'<regex>|<regexAtom><regex>
-NodePtr regex() {
-    NodePtr n = regexAtom();
-    while (*ptr) {
-        token();
-        if (tokenType == '*') {
+NodePtr regex(RegexInfoPtr ri) {
+    NodePtr n = regexAtom(ri);
+    while (ri->ptr[0]) {
+        token(ri);
+        if (ri->tokenType == '*') {
             n = createNode('*',n,0);
-        } else if (tokenType == '|') {
-            NodePtr n1 = regex();
+        } else if (ri->tokenType == '|') {
+            NodePtr n1 = regex(ri);
             n = createNode('|',n,n1);
-        } else if (tokenType == ')') {
+        } else if (ri->tokenType == ')') {
             return n;
         } else {
-            NodePtr n1 = regex();
+            NodePtr n1 = regex(ri);
             n = createNode('+',n,n1);
         }
     } return n;
@@ -163,14 +155,16 @@
 
 int main(int argc, char **argv)
 {
+    RegexInfoPtr ri = (RegexInfoPtr)malloc(sizeof(RegexInfo));
+
     for (int i = 1; i < argc; i++) {
         if (strcmp(argv[i],"-regex") == 0) {
-            ptr = (unsigned char*)argv[i+1]; i++;
+            ri->ptr = (unsigned char*)argv[i+1]; i++;
         }
     }
 
-    printf("regex : %s\n",ptr);
-    NodePtr n = regex();
+    printf("regex : %s\n",ri->ptr);
+    NodePtr n = regex(ri);
     printTree(n);
     return 0;
 }