annotate c/regexParser/regexParser.cc @ 118:31b0ba0050fa testcode

text
author Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
date Thu, 26 Nov 2015 17:19:00 +0900
parents 66c633575b53
children 5d29b6a1b50f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 #include <stdlib.h>
89
50a146c05192 add NodeNumber in Regex Parser tree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 87
diff changeset
2 #include <stdio.h>
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 #include "regexParser.h"
115
ca30f8334741 rename createRegexParser.cc to regexParser.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 112
diff changeset
4 #include "error.h"
ca30f8334741 rename createRegexParser.cc to regexParser.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 112
diff changeset
5
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
6 static NodePtr allocateNode();
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
7 static NodePtr createNode(RegexInfoPtr,unsigned char*,NodePtr,NodePtr);
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
8 static NodePtr charClass(RegexInfoPtr);
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
9 static NodePtr group(RegexInfoPtr);
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
10 static void token(RegexInfoPtr);
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
11 static NodePtr regexAtom(RegexInfoPtr);
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 NodePtr regex(RegexInfoPtr);
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 /**
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 * Create a node of regex parse tree.
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 * tokenType
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 * regexPosition(state)
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 * stateTransitionTable
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 */
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
20
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
21 static
116
66c633575b53 remove error and warning
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
22 NodePtr allocateNode() {
66c633575b53 remove error and warning
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
23 NodePtr n = (NodePtr)malloc(sizeof(node));
66c633575b53 remove error and warning
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
24 n->cc = (CharClassPtr)malloc(sizeof(CharClass));
66c633575b53 remove error and warning
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
25 n->cc->cond = (ConditionList)malloc(sizeof(Condition));
66c633575b53 remove error and warning
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
26 return n;
66c633575b53 remove error and warning
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
27 }
66c633575b53 remove error and warning
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
28
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
29 static
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
30 NodePtr createNode(RegexInfoPtr ri,unsigned char *character, NodePtr left, NodePtr right) {
116
66c633575b53 remove error and warning
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
31 NodePtr n = allocateNode();
108
70069d4647a0 implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
32 if (n == NULL) {
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
33 mallocFailedMessage();
108
70069d4647a0 implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
34 }
70069d4647a0 implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
35
89
50a146c05192 add NodeNumber in Regex Parser tree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 87
diff changeset
36 n->tokenType = ri->tokenType;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 n->left = left;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 n->right = right;
89
50a146c05192 add NodeNumber in Regex Parser tree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 87
diff changeset
39
50a146c05192 add NodeNumber in Regex Parser tree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 87
diff changeset
40 if (ri->tokenType == 'a') {
91
912d7bd51f38 remove static variable in createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 89
diff changeset
41 n->nodeNumber = ri->nodeNumber;
912d7bd51f38 remove static variable in createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 89
diff changeset
42 ri->nodeNumber++;
89
50a146c05192 add NodeNumber in Regex Parser tree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 87
diff changeset
43 ri->tokenType = 0;
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
44 n->cc->cond->w = getWord(ri->tokenValue);
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
45 ri->ptr += n->cc->cond->w->length-1;
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
46 } else {
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
47 WordPtr w = (WordPtr)malloc(sizeof(Word));
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
48 w->word = character;
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
49 w->length = 1;
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
50 n->cc->cond->w = w;
89
50a146c05192 add NodeNumber in Regex Parser tree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 87
diff changeset
51 }
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 return n;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
54
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 // <charClass> ::= '['<literal>'-'<literal>']'
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
56 static
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 NodePtr charClass(RegexInfoPtr ri) {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 NodePtr n = (NodePtr)malloc(sizeof(Node));
108
70069d4647a0 implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
59 if (n == NULL) {
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
60 mallocFailedMessage();
108
70069d4647a0 implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
61 }
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 while (ri->ptr[0] == '-') {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 ri->ptr++;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 return n;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
67
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 // <literal> ::= [a-z][A-Z][0-9]
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
69 static
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 NodePtr literal(RegexInfoPtr ri) {
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
71 NodePtr n = createNode(ri,ri->ptr,0,0);
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 return n;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
74
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 // <group> ::= '('<regex>')'
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
76 static
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 NodePtr group(RegexInfoPtr ri) {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 return regex(ri);
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
80
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
81 static
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 void token(RegexInfoPtr ri) {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 while (ri->ptr[0] != '\0') {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 if (ri->ptr[0] == '('){
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 ri->ptr++;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 ri->tokenType = '(';
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
87 ri->tokenValue = NULL;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 if (ri->ptr[1] == ')') {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 ri->ptr++;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 return;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 } else if (ri->ptr[0] == ')') {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 ri->ptr++;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 ri->tokenType = ')';
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
95 ri->tokenValue = ri->ptr;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 return;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 } else if (ri->ptr[0] == '[') {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 ri->ptr++;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 ri->tokenType = '[';
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
100 ri->tokenValue = ri->ptr;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 if (ri->ptr[1] == ']') {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 ri->ptr++;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 return;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 } else if (ri->ptr[0] == '|'){
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 ri->ptr++;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 ri->tokenType = '|';
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
108 ri->tokenValue = NULL;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 return;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 } else if (ri->ptr[0] == '*'){
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 ri->ptr++;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 ri->tokenType = '*';
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
113 ri->tokenValue = NULL;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 return;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 } else if (ri->ptr[0] == '\\'){
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 // need more proccesing
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 /*
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 \277
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 \0xa5
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 \[
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 \\
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 \utf-8 etc...
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 */
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 } else {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 ri->tokenType = 'a';
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
126 ri->tokenValue = ri->ptr;
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
127 ri->ptr++;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 return;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 ri->tokenType = 0;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 ri->tokenValue = 0;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 return;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
135
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 // <regexAtom> ::= <literal>|<charClass>|<group>
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
137 static
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 NodePtr regexAtom(RegexInfoPtr ri) {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
139
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 token(ri);
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 NodePtr n = NULL;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 if (ri->tokenType == 'a') n = literal(ri);
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 else if (ri->tokenType == '[') n = charClass(ri);
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 else if (ri->tokenType == '(') n = group(ri);
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
145
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 return n;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
147 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
148
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 // <regex> ::= <regexAtom>|<regexAtom>'*'|<regexAtom>'|'<regex>|<regexAtom><regex>
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 NodePtr regex(RegexInfoPtr ri) {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 NodePtr n = regexAtom(ri);
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 while (ri->ptr[0]) {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 token(ri);
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 if (ri->tokenType == '*') {
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
155 unsigned char *syntax = (unsigned char*)malloc(sizeof(unsigned char));
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
156 syntax[0] = '*';
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
157 n = createNode(ri,syntax,n,0);
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 } else if (ri->tokenType == '|') {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 NodePtr n1 = regex(ri);
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
160 unsigned char *syntax = (unsigned char*)malloc(sizeof(unsigned char));
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
161 syntax[0] = '|';
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
162 n = createNode(ri,syntax,n,n1);
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 } else if (ri->tokenType == ')') {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 return n;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
165 } else {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 NodePtr n1 = regex(ri);
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
167 unsigned char *syntax = (unsigned char*)malloc(sizeof(unsigned char));
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
168 syntax[0] = '+';
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
169 n = createNode(ri,syntax,n,n1);
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 } return n;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 }