Mercurial > hg > Members > masakoha > testcode
annotate c/regexParser/main.cc @ 75:6541eae41a73
fix group function
author | masa |
---|---|
date | Fri, 28 Aug 2015 18:40:33 +0900 |
parents | b11dfc48f698 |
children | d98a036441e2 |
rev | line source |
---|---|
55 | 1 /* |
56
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
2 * <literal> ::= [a-z][A-Z][0-9] |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
3 * <charClass> ::= '['<literal>'-'<literal>']' |
58
4053c3e0fa7f
implement group()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
57
diff
changeset
|
4 * <group> ::= '('<regex>')' |
66
f8fb3b463f70
fix when '|' come procces
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
5 * <regexAtom> ::= <literal>|<charClass>|<group> |
f8fb3b463f70
fix when '|' come procces
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
6 * <regex> ::= <regexAtom>|<regexAtom>'*'|<regexAtom>'|'<regex>|<regexAtom><regex> |
55 | 7 */ |
8 | |
45 | 9 #include <stdio.h> |
56
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
10 #include <stdlib.h> |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
11 #include <string.h> |
57
71b497d25273
fix literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
12 |
63 | 13 typedef struct charClass { |
14 unsigned char table[256]; | |
15 struct utf8Range { | |
16 unsigned char *begin; | |
17 unsigned char *end; | |
64
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
18 struct utf8Range *next; |
63 | 19 } *rangeList; |
64
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
20 } CharClass, *CharClassPtr; |
63 | 21 |
56
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
22 typedef struct node { |
63 | 23 unsigned char type; |
24 union value { | |
25 charClass *cc; | |
67
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
26 unsigned char character; |
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
27 } Value, *ValuePtr; |
58
4053c3e0fa7f
implement group()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
57
diff
changeset
|
28 struct node *self; |
68
d27b3af1fe75
remove string()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
29 struct node *parent; |
56
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
30 struct node *left; |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
31 struct node *right; |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
32 } Node, *NodePtr; |
52
a2826bf4e80a
remove magic number
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
51
diff
changeset
|
33 |
64
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
34 unsigned char *ptr; |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
35 unsigned char tokenType; |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
36 int tokenValue; |
58
4053c3e0fa7f
implement group()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
57
diff
changeset
|
37 NodePtr regexHeadNode; |
4053c3e0fa7f
implement group()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
57
diff
changeset
|
38 |
56
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
39 NodePtr charClass(); |
58
4053c3e0fa7f
implement group()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
57
diff
changeset
|
40 NodePtr group(); |
63 | 41 NodePtr orexp(); |
56
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
42 NodePtr asterisk(); |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
43 NodePtr regex(); |
67
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
44 NodePtr createNode(unsigned char,NodePtr,NodePtr); |
64
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
45 extern void token(); |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
46 extern NodePtr regexAtom(); |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
47 |
55 | 48 |
62
a49b4a8b8c14
implement isLiteral
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
61
diff
changeset
|
49 bool isLiteral(char c) { |
63 | 50 if (*ptr > 0x7f) return true; |
51 else if (*ptr == '(') return false; | |
52 else if (*ptr == '[') return false; | |
53 else if (*ptr == '|') return false; | |
54 else if (*ptr == '*') return false; | |
55 return true; | |
62
a49b4a8b8c14
implement isLiteral
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
61
diff
changeset
|
56 } |
a49b4a8b8c14
implement isLiteral
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
61
diff
changeset
|
57 |
67
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
58 NodePtr createNode(unsigned char character, NodePtr left, NodePtr right) { |
57
71b497d25273
fix literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
59 NodePtr n = (NodePtr)malloc(sizeof(Node)); |
58
4053c3e0fa7f
implement group()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
57
diff
changeset
|
60 n->self = n; |
67
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
61 n->Value.character = character; |
56
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
62 n->left = left; |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
63 n->right = right; |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
64 return n; |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
65 } |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
66 |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
67 // <charClass> ::= '['<literal>'-'<literal>']' |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
68 NodePtr charClass() { |
62
a49b4a8b8c14
implement isLiteral
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
61
diff
changeset
|
69 NodePtr n = (NodePtr)malloc(sizeof(Node)); |
67
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
70 unsigned char startChar = *ptr; |
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
71 while (*ptr == '-') { |
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
72 ptr++; |
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
73 } |
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
74 unsigned char endChar = *ptr; |
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
75 unsigned char *charTable = (unsigned char*)malloc(sizeof(char)*256); |
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
76 |
62
a49b4a8b8c14
implement isLiteral
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
61
diff
changeset
|
77 return n; |
56
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
78 } |
55 | 79 |
56
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
80 // <literal> ::= [a-z][A-Z][0-9] |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
81 NodePtr literal() { |
65 | 82 NodePtr n = createNode(*ptr,0,0); |
57
71b497d25273
fix literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
83 ptr++; |
71b497d25273
fix literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
56
diff
changeset
|
84 return n; |
56
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
85 } |
55 | 86 |
63 | 87 // <group> ::= '('<regex>')' |
58
4053c3e0fa7f
implement group()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
57
diff
changeset
|
88 NodePtr group() { |
75 | 89 return regex(); |
58
4053c3e0fa7f
implement group()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
57
diff
changeset
|
90 } |
4053c3e0fa7f
implement group()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
57
diff
changeset
|
91 |
55 | 92 |
71
58d2b10988c9
move ptr++ in token()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
70
diff
changeset
|
93 |
63 | 94 void token() { |
58
4053c3e0fa7f
implement group()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
57
diff
changeset
|
95 while (*ptr != '\0') { |
74 | 96 if (*ptr == '('){ |
71
58d2b10988c9
move ptr++ in token()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
70
diff
changeset
|
97 ptr++; |
70
87dff3a124ab
resolve segmentation fault(But not correct performance)
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
69
diff
changeset
|
98 tokenType = '('; |
63 | 99 tokenValue = 0; |
67
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
100 if (ptr[1] == ')') { |
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
101 ptr++; |
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
102 } |
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
103 return; |
74 | 104 } else if (*ptr == ')') { |
105 ptr++; | |
106 tokenType = ')'; | |
107 tokenValue = *ptr; | |
108 return; | |
58
4053c3e0fa7f
implement group()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
57
diff
changeset
|
109 } else if (*ptr == '[') { |
71
58d2b10988c9
move ptr++ in token()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
70
diff
changeset
|
110 ptr++; |
63 | 111 tokenType = '['; |
64
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
112 tokenValue = *ptr; |
63 | 113 if (ptr[1] == ']') { |
114 ptr++; | |
115 } | |
116 return; | |
58
4053c3e0fa7f
implement group()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
57
diff
changeset
|
117 } else if (*ptr == '|'){ |
71
58d2b10988c9
move ptr++ in token()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
70
diff
changeset
|
118 ptr++; |
63 | 119 tokenType = '|'; |
120 tokenValue = 0; | |
121 return; | |
60
8616a045a7f4
impl asterisk
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
59
diff
changeset
|
122 } else if (*ptr == '*'){ |
71
58d2b10988c9
move ptr++ in token()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
70
diff
changeset
|
123 ptr++; |
63 | 124 tokenType = '*'; |
125 tokenValue = 0; | |
126 return; | |
73 | 127 } else if (*ptr == '\\'){ |
128 // need more proccesing | |
129 /* | |
130 \277 | |
131 \0xa5 | |
132 \[ | |
133 \\ | |
134 \utf-8 etc... | |
135 */ | |
136 } else { | |
137 tokenType = 'a'; | |
138 tokenValue = *ptr; | |
139 return; | |
56
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
140 } |
55 | 141 } |
71
58d2b10988c9
move ptr++ in token()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
70
diff
changeset
|
142 |
58d2b10988c9
move ptr++ in token()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
70
diff
changeset
|
143 tokenType = 0; |
58d2b10988c9
move ptr++ in token()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
70
diff
changeset
|
144 tokenValue = 0; |
58d2b10988c9
move ptr++ in token()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
70
diff
changeset
|
145 return; |
55 | 146 } |
147 | |
64
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
148 // <regexAtom> ::= <literal>|<charClass>|<group> |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
149 NodePtr regexAtom() { |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
150 |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
151 token(); |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
152 NodePtr n = NULL; |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
153 if (tokenType == 'a') n = literal(); |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
154 else if (tokenType == '[') n = charClass(); |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
155 else if (tokenType == '(') n = group(); |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
156 |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
157 return n; |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
158 } |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
159 |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
160 // <regex> ::= <regexAtom>|<regexAtom>'*'|<regexAtom>'|'<regex>|<regexAtom><regex> |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
161 NodePtr regex() { |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
162 NodePtr n = regexAtom(); |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
163 while (*ptr) { |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
164 token(); |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
165 if (tokenType == '*') { |
71
58d2b10988c9
move ptr++ in token()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
70
diff
changeset
|
166 n = createNode('*',n,0); |
64
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
167 } else if (tokenType == '|') { |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
168 NodePtr n1 = regex(); |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
169 n = createNode('|',n,n1); |
74 | 170 } else if (tokenType == ')') { |
171 return n; | |
64
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
172 } else { |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
173 NodePtr n1 = regex(); |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
174 n = createNode('+',n,n1); |
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
175 } |
67
4842ca2cf8ee
print character
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
66
diff
changeset
|
176 } return n; |
68
d27b3af1fe75
remove string()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
177 } |
d27b3af1fe75
remove string()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
178 |
69
eecddded9b91
implement printTree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
68
diff
changeset
|
179 void descendTree(NodePtr n) { |
eecddded9b91
implement printTree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
68
diff
changeset
|
180 static int d = 0; |
68
d27b3af1fe75
remove string()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
181 if (n->right != NULL) { |
d27b3af1fe75
remove string()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
182 d++; |
69
eecddded9b91
implement printTree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
68
diff
changeset
|
183 descendTree(n->right); |
eecddded9b91
implement printTree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
68
diff
changeset
|
184 d--; |
eecddded9b91
implement printTree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
68
diff
changeset
|
185 } |
eecddded9b91
implement printTree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
68
diff
changeset
|
186 printf("%*c%c\n",d*4, ' ',n->Value.character); |
eecddded9b91
implement printTree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
68
diff
changeset
|
187 if (n->left != NULL) { |
68
d27b3af1fe75
remove string()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
188 d++; |
69
eecddded9b91
implement printTree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
68
diff
changeset
|
189 descendTree(n->left); |
eecddded9b91
implement printTree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
68
diff
changeset
|
190 d--; |
68
d27b3af1fe75
remove string()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
191 } |
d27b3af1fe75
remove string()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
192 } |
d27b3af1fe75
remove string()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
67
diff
changeset
|
193 |
66
f8fb3b463f70
fix when '|' come procces
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
194 void printTree(NodePtr n) { |
72
c4b934048e2a
remove printNodeDate
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
71
diff
changeset
|
195 puts("---Print Node----"); |
69
eecddded9b91
implement printTree
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
68
diff
changeset
|
196 descendTree(n); |
72
c4b934048e2a
remove printNodeDate
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
71
diff
changeset
|
197 puts("-----------------"); |
66
f8fb3b463f70
fix when '|' come procces
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
198 } |
f8fb3b463f70
fix when '|' come procces
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
199 |
f8fb3b463f70
fix when '|' come procces
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
200 |
62
a49b4a8b8c14
implement isLiteral
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
61
diff
changeset
|
201 int main(int argc, char **argv) |
55 | 202 { |
56
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
203 for (int i = 1; i < argc; i++) { |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
204 if (strcmp(argv[i],"-regex") == 0) { |
64
e0ad6c145f89
remove some errors
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
205 ptr = (unsigned char*)argv[i+1]; i++; |
56
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
206 } |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
207 } |
55 | 208 |
56
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
209 printf("regex : %s\n",ptr); |
8901bc071d33
implement string() and literal()
Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
210 NodePtr n = regex(); |
66
f8fb3b463f70
fix when '|' come procces
Masataka Kohagura <e085726@ie.u-ryukyu.ac.jp>
parents:
65
diff
changeset
|
211 printTree(n); |
45 | 212 return 0; |
213 } |