annotate c/regexParser/regexParser.cc @ 144:d8a4922eceae pairPro

remove some errors (not working)
author Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
date Mon, 14 Dec 2015 17:14:33 +0900
parents 32977f5a2ed0
children 50217a0545e8
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>
122
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
3 #include <string.h>
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
4 #include <ctype.h>
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 #include "regexParser.h"
115
ca30f8334741 rename createRegexParser.cc to regexParser.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 112
diff changeset
6
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
7 static NodePtr charClass(RegexInfoPtr);
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
8 static void token(RegexInfoPtr);
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
9 static NodePtr regexAtom(RegexInfoPtr);
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 NodePtr regex(RegexInfoPtr);
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 /**
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 * Create a node of regex parse tree.
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 * tokenType
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 * regexPosition(state)
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 * stateTransitionTable
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 */
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
18
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
19 static
116
66c633575b53 remove error and warning
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
20 NodePtr allocateNode() {
129
b930be74a16e remove word.h
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 128
diff changeset
21 NodePtr n = NEW(Node);
125
4d6ac69801ad fix createNode
Nozomi
parents: 122
diff changeset
22 n->cc = NULL;
4d6ac69801ad fix createNode
Nozomi
parents: 122
diff changeset
23 n->left = NULL;
4d6ac69801ad fix createNode
Nozomi
parents: 122
diff changeset
24 n->right = NULL;
116
66c633575b53 remove error and warning
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
25 return n;
66c633575b53 remove error and warning
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
26 }
66c633575b53 remove error and warning
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
27
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
28 static
134
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
29 NodePtr createNode(RegexInfoPtr ri,unsigned char type,CharClassPtr cc, NodePtr left, NodePtr right) {
116
66c633575b53 remove error and warning
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 115
diff changeset
30 NodePtr n = allocateNode();
108
70069d4647a0 implement malloc error checking
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 104
diff changeset
31
125
4d6ac69801ad fix createNode
Nozomi
parents: 122
diff changeset
32 n->tokenType = type;
134
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
33 n->cc = cc;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 n->left = left;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 n->right = right;
120
5d29b6a1b50f include Word in Node
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
36 n->nodeNumber = ri->nodeNumber;
5d29b6a1b50f include Word in Node
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 118
diff changeset
37 ri->nodeNumber++;
89
50a146c05192 add NodeNumber in Regex Parser tree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 87
diff changeset
38
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 return n;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
41
144
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
42 CharClassPtr createCharClassWord(RegexInfoPtr ri) {
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
43 CharClassPtr cc = NEW(CharClass);
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
44 cc->type = 'a';
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
45 cc->cond.w.word = ri->tokenValue;
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
46 cc->cond.w.length = ri->ptr - ri->tokenValue;
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
47 cc->nextState.bitContainer = 0;
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
48 return cc;
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
49 }
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
50
143
32977f5a2ed0 add charClassMerge
masa
parents: 142
diff changeset
51 CharClassPtr charClassMerge(CharClassPtr cc, unsigned char begin, unsigned char end, CharClassPtr next) {
144
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
52 CharClassPtr cc1 = NEW(CharClass);
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
53 return cc1;
142
de0f332d560c insert charClassMerge function
masa
parents: 136
diff changeset
54 }
125
4d6ac69801ad fix createNode
Nozomi
parents: 122
diff changeset
55
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 // <charClass> ::= '['<literal>'-'<literal>']'
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
57 static
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 NodePtr charClass(RegexInfoPtr ri) {
125
4d6ac69801ad fix createNode
Nozomi
parents: 122
diff changeset
59 CharClassPtr cc = NEW(CharClass);
135
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
60 NodePtr n = createNode(ri,'c',cc,0,0);
126
639b0b437ebf remove error (do not work)
masa
parents: 125
diff changeset
61 cc->type = 'r';
142
de0f332d560c insert charClassMerge function
masa
parents: 136
diff changeset
62 cc->nextState.bitContainer = 0;
de0f332d560c insert charClassMerge function
masa
parents: 136
diff changeset
63 RangeListPtr rangeList = &cc->cond.range;
144
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
64 rangeList->begin = (unsigned long)*ri->ptr;
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
65 rangeList->end = (unsigned long)*ri->ptr;
130
7925e9abb078 add or flag
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
66
135
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
67 for (ri->ptr++; *ri->ptr && *ri->ptr != ']'; ri->ptr++) {
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
68 if (*ri->ptr == '-') {
144
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
69 rangeList->end = (unsigned long)(ri->ptr + 1);
135
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
70 ri->ptr++;
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
71 continue;
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
72 }
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
73 if (ri->ptr[0] == 0 || ri->ptr[0] == ']') break;
144
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
74 if (ri->ptr[0] == rangeList->end + 1) {
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
75 rangeList->end = (unsigned long)ri->ptr;
135
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
76 continue;
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
77 }
126
639b0b437ebf remove error (do not work)
masa
parents: 125
diff changeset
78 rangeList->next = NEW(RangeList);
125
4d6ac69801ad fix createNode
Nozomi
parents: 122
diff changeset
79 rangeList = rangeList->next;
144
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
80 rangeList->begin = (unsigned long)ri->ptr;
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
81 rangeList->end = (unsigned long)ri->ptr;
125
4d6ac69801ad fix createNode
Nozomi
parents: 122
diff changeset
82 rangeList->next = NULL;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 }
142
de0f332d560c insert charClassMerge function
masa
parents: 136
diff changeset
84 // TODO literal support
de0f332d560c insert charClassMerge function
masa
parents: 136
diff changeset
85 // merge rangeList here
135
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
86 if (*ri->ptr) ri->ptr++;
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
87 token(ri);
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 return n;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 }
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 // <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
92 static
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 NodePtr literal(RegexInfoPtr ri) {
134
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
94 CharClassPtr cc = createCharClassWord(ri);
144
d8a4922eceae remove some errors (not working)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 143
diff changeset
95 token(ri);
134
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
96 NodePtr n = createNode(ri,'a',cc,0,0);
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 return n;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99
133
ccc673449351 Look ahead '*'
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
100 static
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 void token(RegexInfoPtr ri) {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 while (ri->ptr[0] != '\0') {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 if (ri->ptr[0] == '('){
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 ri->ptr++;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 ri->tokenType = '(';
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
106 ri->tokenValue = NULL;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 return;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 } else if (ri->ptr[0] == ')') {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 ri->ptr++;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 ri->tokenType = ')';
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
111 ri->tokenValue = ri->ptr;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 return;
135
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
113 } else if (ri->ptr[0] == ']') {
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
114 ri->ptr++;
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
115 ri->tokenType = ']';
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
116 ri->tokenValue = ri->ptr;
e1a262ec75f0 impl charclass
masa
parents: 134
diff changeset
117 return;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 } else if (ri->ptr[0] == '|'){
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 ri->ptr++;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 ri->tokenType = '|';
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
121 ri->tokenValue = NULL;
133
ccc673449351 Look ahead '*'
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
122 ri->orNum++;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 return;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 } else if (ri->ptr[0] == '*'){
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 ri->ptr++;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 ri->tokenType = '*';
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
127 ri->tokenValue = NULL;
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 } else if (ri->ptr[0] == '\\'){
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 // need more proccesing
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 /*
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 \277
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 \0xa5
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 \utf-8 etc...
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 */
142
de0f332d560c insert charClassMerge function
masa
parents: 136
diff changeset
138 } else if (ri->ptr[0] == '[') {
de0f332d560c insert charClassMerge function
masa
parents: 136
diff changeset
139 ri->ptr++;
de0f332d560c insert charClassMerge function
masa
parents: 136
diff changeset
140 ri->tokenType = 'c';
de0f332d560c insert charClassMerge function
masa
parents: 136
diff changeset
141 ri->tokenValue = ri->ptr;
de0f332d560c insert charClassMerge function
masa
parents: 136
diff changeset
142 return;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 } else {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 ri->tokenType = 'a';
118
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 116
diff changeset
145 ri->tokenValue = ri->ptr;
134
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
146 if (isalnum(ri->ptr[0])) {
122
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
147 ri->ptr++;
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
148 }
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 return;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 }
134
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
152 ri->tokenType = 0;
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
153 ri->tokenValue = NULL;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 return;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
156
130
7925e9abb078 add or flag
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 129
diff changeset
157 // <regexAtom> ::= <literal>|<charClass>|<group>
112
ec485345daf9 some function use static
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 108
diff changeset
158 static
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 NodePtr regexAtom(RegexInfoPtr ri) {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
160
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 NodePtr n = NULL;
124
masa
parents: 122
diff changeset
162 if (ri->tokenType == 'c') n = charClass(ri);
134
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
163 else if (ri->tokenType == 'a') n = literal(ri);
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
164 else if (ri->tokenType == '(') {
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
165 n = regex(ri);
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
166 if (ri->tokenType != ')') {
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
167 // error
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
168 }
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
169 token(ri);
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
170 }
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
171 if (ri->tokenType == '*') {
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
172 n = createNode(ri,'*',0,n,0);
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
173 token(ri);
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
174 }
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
175
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 return n;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 }
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
178
133
ccc673449351 Look ahead '*'
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
179 // <regex> ::= <regexAtom> | <regexAtom>'*'<regex> | <regexAtom>'|'<regex> | <regexAtom><regexAtom>'*' | <regexAtom><regex>
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 NodePtr regex(RegexInfoPtr ri) {
134
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
181 token(ri);
128
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 127
diff changeset
182 NodePtr n = regexAtom(ri);
134
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
183 while (ri->tokenType) {
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 if (ri->tokenType == '*') {
134
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
185 n = createNode(ri,'*',0,n,0);
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
186 token(ri);
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
187 return n;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 } else if (ri->tokenType == '|') {
134
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
189 n = createNode(ri,'|',0,n,0);
133
ccc673449351 Look ahead '*'
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents: 132
diff changeset
190 NodePtr n1 = regex(ri);
134
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
191 n->right = n1;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 } else if (ri->tokenType == ')') {
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 return n;
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 } else {
134
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
195 n = createNode(ri,'+',0,n,0);
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
196 NodePtr n1 = regexAtom(ri);
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
197 n->right = n1;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 }
134
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
199 }
dbafc753078e fix concatination & selection & grouping
masa
parents: 133
diff changeset
200 return n;
82
1d9bbf922bb6 add createRegexTree.cc
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 }