473,498 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help on Parsing

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...

Jun 28 '07 #1
2 1600
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.
Jun 28 '07 #2

"user923005" <dc*****@connx.comwrote in message
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.
To understand how to build a parser from firct principles, check out
MiniBasic, on my website.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
9423
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
0
1479
by: burn_hall | last post by:
Hi, I have a problem and can't figure it out and need your help, please look at the following code and the output also a xml file snippet is down there too. Looking at the output I don't know why...
16
2849
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
6
4298
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
1730
by: uestebanez | last post by:
Hi everybody! I have a little problem working with libxml SAX API. The problem is that I don't know how to stop parsing when I have processed the data I need. I don't want to parse all file, I...
4
2625
by: ralphNOSPAM | last post by:
Is there a function or otherwise some way to pull out the target text within an XML tag? For example, in the XML tag below, I want to pull out 'CALIFORNIA'. ...
8
11118
by: mrwoopey | last post by:
Hi, I need to get data from a client via a HL7 interface. How can I do this? Can I do this via visual basic? Please post, code, links, anything! Your help is appreciated! Phin
1
1628
by: syhzaidi | last post by:
How can we do Parsing of Hexdecimel in C# reading string from stream file for eg.. i have a file like.......... 0f 2f 12 2d 3a.......in hexa decimal save in a file.txt and i m reading it from...
13
4473
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
2
6337
by: embz | last post by:
this post concerns three pages. 1. this page: http://www.katherine-designs.com/sendemail.php i get the following errors: a lot of it seems to deal with the PHP code i inserted to the page....
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.