473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Parse variable names from string

Hello, I have the need to parse variable names from a string and save them
somewhere safe for future usage. Here's my first attempt (I don't have any
rules for valid names yet) - but I have a feeling that it's unnecessary
complex? Any input would be greatly appreciated.

#include <stdio.h>
#include <stdlib.h>

int get_ops(char *sen, char ***atom, char limit);

int main(void)
{
char *test = "a, b, c, d"; /* The real input is stripped of spaces */
char **atom;
get_ops(test, &atom, ',');
return 0;
}

int get_ops(char *sen, char ***atom, char limit)
{
int i, j, k;
char **tmp1, *tmp2;
*atom = malloc(sizeof (char *));
**atom = malloc(1);
for (i = j = k = 0; sen[i] != '\0'; ++i) {
if (sen[i] != limit) {
tmp2 = realloc((*atom)[k], j+2);
if (tmp2 == NULL)
return -2;
(*atom)[k] = tmp2;
tmp2 = NULL;
(*atom)[k][j++] = sen[i];
}
else if (sen[i] == limit) {
(*atom)[k++][j] = '\0';
tmp1 = realloc(*atom, (k +1)*sizeof(char *));

if (tmp1 == NULL)
return -2;
*atom = tmp1;
tmp1 = NULL;
(*atom)[k] = malloc(1);
j = 0;
}
}
return 0;
}

Jul 3 '07 #1
4 3306
On Jul 3, 12:53 pm, Victor Lagerkvist <plumsa...@gmail.comwrote:
Hello, I have the need to parse variable names from a string and save them
somewhere safe for future usage. Here's my first attempt (I don't have any
rules for valid names yet) - but I have a feeling that it's unnecessary
complex? Any input would be greatly appreciated.

#include <stdio.h>
#include <stdlib.h>

int get_ops(char *sen, char ***atom, char limit);

int main(void)
{
char *test = "a, b, c, d"; /* The real input is stripped of spaces */
char **atom;
get_ops(test, &atom, ',');
return 0;

}

int get_ops(char *sen, char ***atom, char limit)
{
int i, j, k;
char **tmp1, *tmp2;
*atom = malloc(sizeof (char *));
**atom = malloc(1);
for (i = j = k = 0; sen[i] != '\0'; ++i) {
if (sen[i] != limit) {
tmp2 = realloc((*atom)[k], j+2);
if (tmp2 == NULL)
return -2;
(*atom)[k] = tmp2;
tmp2 = NULL;
(*atom)[k][j++] = sen[i];
}
else if (sen[i] == limit) {
(*atom)[k++][j] = '\0';
tmp1 = realloc(*atom, (k +1)*sizeof(char *));

if (tmp1 == NULL)
return -2;
*atom = tmp1;
tmp1 = NULL;
(*atom)[k] = malloc(1);
j = 0;
}
}
return 0;

}
I guess that it is not nearly complex enough.
If you are gathering variable names from {presumably} C source code,
it will have to be fully grammar aware.
Normally, parsers put variable names into a hash table.
I suggest that you get an existing C parser, and just read the
variable list it creates when it scans a source file.
Here is a place to find a C grammar:
http://www.devincook.com/goldparser/grammars/index.htm
It works with the Gold Parser.
There are C grammars all over the place, so I am sure you can find one
for YACC or Antlr or whatever.
Jul 3 '07 #2
user923005 wrote:
On Jul 3, 12:53 pm, Victor Lagerkvist <plumsa...@gmail.comwrote:
>Hello, I have the need to parse variable names from a string and save
them somewhere safe for future usage. Here's my first attempt (I don't
have any rules for valid names yet) - but I have a feeling that it's
unnecessary complex? Any input would be greatly appreciated.

#include <stdio.h>
#include <stdlib.h>

int get_ops(char *sen, char ***atom, char limit);
<snip>
I guess that it is not nearly complex enough.
If you are gathering variable names from {presumably} C source code,
it will have to be fully grammar aware.
Normally, parsers put variable names into a hash table.
I suggest that you get an existing C parser, and just read the
variable list it creates when it scans a source file.
Here is a place to find a C grammar:
http://www.devincook.com/goldparser/grammars/index.htm
It works with the Gold Parser.
There are C grammars all over the place, so I am sure you can find one
for YACC or Antlr or whatever.
Actually, the only functionality I truly need is the names of the variables
(there's only one "type") and the number of them - everything else is a
bonus! They are given by the user from standard input, line by line, such
as:
<< build a, b, c

And that's all there is (more or less any types of names should be allowed),
and for some reason I usually become a sad panda when the code "runs away".
Jul 3 '07 #3
On Jul 3, 2:47 pm, Victor Lagerkvist <plumsa...@gmail.comwrote:
user923005 wrote:
On Jul 3, 12:53 pm, Victor Lagerkvist <plumsa...@gmail.comwrote:
Hello, I have the need to parse variable names from a string and save
them somewhere safe for future usage. Here's my first attempt (I don't
have any rules for valid names yet) - but I have a feeling that it's
unnecessary complex? Any input would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
int get_ops(char *sen, char ***atom, char limit);
<snip>
I guess that it is not nearly complex enough.
If you are gathering variable names from {presumably} C source code,
it will have to be fully grammar aware.
Normally, parsers put variable names into a hash table.
I suggest that you get an existing C parser, and just read the
variable list it creates when it scans a source file.
Here is a place to find a C grammar:
http://www.devincook.com/goldparser/grammars/index.htm
It works with the Gold Parser.
There are C grammars all over the place, so I am sure you can find one
for YACC or Antlr or whatever.

Actually, the only functionality I truly need is the names of the variables
(there's only one "type") and the number of them - everything else is a
bonus! They are given by the user from standard input, line by line, such
as:
<< build a, b, c

And that's all there is (more or less any types of names should be allowed),
and for some reason I usually become a sad panda when the code "runs away".- Hide quoted text -
In that case, why not just store them in a hash table to ensure you do
not have duplicates.

Jul 3 '07 #4
user923005 wrote:
On Jul 3, 2:47 pm, Victor Lagerkvist <plumsa...@gmail.comwrote:
>user923005 wrote:
On Jul 3, 12:53 pm, Victor Lagerkvist <plumsa...@gmail.comwrote:
Hello, I have the need to parse variable names from a string and save
them somewhere safe for future usage. Here's my first attempt (I don't
have any rules for valid names yet) - but I have a feeling that it's
unnecessary complex? Any input would be greatly appreciated.
>#include <stdio.h>
#include <stdlib.h>
>int get_ops(char *sen, char ***atom, char limit);
<snip>
I guess that it is not nearly complex enough.
If you are gathering variable names from {presumably} C source code,
it will have to be fully grammar aware.
Normally, parsers put variable names into a hash table.
I suggest that you get an existing C parser, and just read the
variable list it creates when it scans a source file.
Here is a place to find a C grammar:
http://www.devincook.com/goldparser/grammars/index.htm
It works with the Gold Parser.
There are C grammars all over the place, so I am sure you can find one
for YACC or Antlr or whatever.

Actually, the only functionality I truly need is the names of the
variables (there's only one "type") and the number of them - everything
else is a bonus! They are given by the user from standard input, line by
line, such as:
<< build a, b, c

And that's all there is (more or less any types of names should be
allowed), and for some reason I usually become a sad panda when the code
"runs away".- Hide quoted text -

In that case, why not just store them in a hash table to ensure you do
not have duplicates.
Ah, that would no doubt be sleek in this case. I thank thee for thine input.
Jul 4 '07 #5

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

Similar topics

0
by: Wes Batson | last post by:
I have a program that builds screens dynamically by getting all form information from a database including size, position, and variable names. I was wondering how I convert the names from a String...
5
by: MLH | last post by:
I'm working with lots of long strings now, it seems. I have to import them & parse them constantly. The A97 memo field type supports only 32768 chars. What happens when this is processed... Dim...
18
by: Carramba | last post by:
Hi! I am wondering what is the best way to control that compiler manage variable names longer then 8 signs. Does it enought to set 2 variables with the same name and difference after 8 sign and...
15
by: Thomas Scheiderich | last post by:
I thought I read that the case for the variable names is important. For example Dim Wheel As Integer Wheel here is a different variable from WHEEL. Is this correct?
2
by: Hamish Symington | last post by:
Hello, I'm trying to re-code a site which I wrote in PHP into ASP.NET using Visual Basic .NET. Something PHP has which has proved invaluable is the concept of variable variables. I haven't...
12
by: ross | last post by:
The following gives an error: <?php $super-man=100; ?> Parse error: parse error, unexpected '=' in /test.php on line 2 I understand that the "-" sign is seen as an operator. is there a...
16
by: John | last post by:
Does the length of my C variable names have any affect, performance-wise, on my final executable program? I mean, once compiled, etc., is there any difference between these two: number = 3; n =...
10
by: John Salerno | last post by:
If I want to have a list like this: where the first part of each tuple is a variable name and the second part is a label for the user to see, such as a form like this: First Name: ________...
6
by: Academia | last post by:
I want to search for Dim and replace it with Dim That is, I want to change the first character of Dim variable names to upper case. I can't figure know to use Regular Expression to do that....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.