On Jun 28, 12:41 am, K.Z.Za...@ncl.ac.uk wrote:
Consider the following C code:
------------------------------
#include <stdio.h>
#include <string.h>
void InitTokenString(char InputStr[512]);
static char TokenString[100][100];
int length=0;
void main ()
{
char input1[100]="System.out.println (\"Hello\");";
char input2[100]="public void method1 (int x);";
InitTokenString(input1);
for (int i=0;i<length;i++)
{
printf ("%s\n",TokenString[i]);
}
// pause to see the output
int ch=getch();
}
/* Tokenize TokenString & Initialize Length*/
void InitTokenString(char InputStr[512])
{
char *str;
int index=0;
str = strtok(InputStr," ");
strcpy (TokenString[index],str);
while (1)
{
str = strtok(NULL," ");
/* check if there is nothing else to extract */
if (str == NULL)
{
break;
}
else
{
index++;
strcpy (TokenString[index],str);
}
}
length=++index;
}
------------------------------
I am trying to search a string for a Java function definition using
C.
I've already manage to tokenize the string into a number of tokens.
How can I search the function from the string effectively because
there are so much possibilities: Consider these possibilities:
- public void method ( ) // space after 'method'
- public void method( ) // no space after 'method'
- public void m.method() // a '.' before method so this is a function
call in Java
- public void method (); // a semicolon after ')'
Any help is really appreciated...
I guess that this is more nearly what you want:
#include <stdio.h>
#include <string.h>
static char TokenString[512][512];
static char OneString[512];
/* Tokenize TokenString & Initialize Length*/
int InitTokenString(char InputStr[512])
{
char *str;
static const char *delim = " .\"{};)(";
int count = 0;
str = strtok(InputStr, delim);
while (str) {
strcpy(TokenString[count++], str);
str = strtok(NULL, delim);
}
return count;
}
int main(void)
{
char input1[100] = "System.out.println (\"Hello\");";
char input2[100] = "public void method1 (int x);";
int count = InitTokenString(input1);
int i;
puts("1st string:");
for (i = 0; i < count; i++) {
printf("%s\n", TokenString[i]);
}
count = InitTokenString(input2);
puts("\n2nd string:");
for (i = 0; i < count; i++) {
printf("%s\n", TokenString[i]);
}
/* pause to see the output */
puts("Press the <Enterkey to continue");
fgets(OneString, sizeof OneString, stdin);
return 0;
}
However, I would not approach the problem this way at all, as you can
see (and as you have described) it will surely lead to frustration.
A much more sensible way would be to get a Java grammar (they are all
over the place) and then make a parser/lexer from it.
The Gold grammar engine might be worth a look.
P.S. aside from the fun of correcting your errors, there was not
really any C content in this thread.
Maybe:
http://www.devincook.com/goldparser/grammars/index.htm http://javatoolbox.com/tools/gold-parser http://www.devincook.com/goldparser/engine/java/
can be helpful.