Wednesday, October 27, 2010

A small grep Tool

Yesterday, I was reading about the regular expressions design that how first developer wrote the complex code with minimal complexity, Since now I know the design of the minimal version of regular expression code I decided to share it. code in itself is quite simple and self explanatory..although I marked the complex part with appropriate comments.


 
int grep (char *regexp, char *text)
{
if (*regexp == '$' ){ /*End of regular expression*/
if ( *(regexp+1) == '\0' ){
return 1;
} else {
return 0;
}
} else if ( *regexp == '.' ) { /*Single character presence*/
return grep ( regexp + 1, text + 1 );
} else if ( *regexp == '*') { /*multi character presence*/
return matchstar(regexp+1, text);
} else if ( *regexp == *text ) {
return grep ( regexp + 1, text + 1 );
} else {
return 0;
}
}


The above code has the four important regions and will detect the expressions for *,.,$ code will return 1 on success.

 
int matchstar(char *regexp, char *text)
{
if ( grep (regexp, text ) ){ /*end of multi character match*/
return 1;
} else {
matchstar(regexp, text+1);
}
}


The code is matching multi character presence and checking it's end recursively.

 
int main(int argc, char **argv)
{
FILE *file;
char text[200];
char c;
size_t size = 0;

if (argc >= 3) {
file = fopen(argv[1], "r");

while(EOF != (c = getc(file))){
if (c=='\n'){
text[size] = '\0';
int i = 0;
for ( i = 0; i < size; i++){
if (grep(argv[2], text + i)) {
printf("%s\n", text);
break;
}
}
size = 0;
} else {
text[size] = c;
size++;
}
}
}
return 0;
}



Compilation Instruction

[~/CODE]
13:04:24 $ gcc -o grep grep.c

[~/CODE]
13:04:36 $ gedit grep.c


./grep file_name regular_expression$

[~/CODE]
13:37:32 $ cat test
my
myname is
my name is khan
dog
cat is my pet
peterson is an awesome player

[~/CODE]
13:37:35 $ ./grep test m*k$
my
myname is
my name is khan
cat is my pet
peterson is an awesome player

[~/CODE]
13:37:39 $ ./grep test dog$
dog

[~/CODE]
13:37:43 $ ./grep test awsome$

[~/CODE]
13:37:49 $ ./grep test aw.some$
peterson is an awesome player



:-fat0ss

No comments:

Post a Comment