473,848 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3335
On Jul 3, 12:53 pm, Victor Lagerkvist <plumsa...@gmai l.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...@gmai l.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...@gmai l.comwrote:
user923005 wrote:
On Jul 3, 12:53 pm, Victor Lagerkvist <plumsa...@gmai l.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...@gmai l.comwrote:
>user923005 wrote:
On Jul 3, 12:53 pm, Victor Lagerkvist <plumsa...@gmai l.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
1558
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 in the database to a variable name to be used in a declaration such as JLabel <database String name> = new JLabel(); Any help is greatly appreciated. Thanks.
5
11747
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 MyString As String Am I getting VLS declaration or a FLS declaration? Can I control which I get somehow? I have done some homework, but I don't understand
18
1760
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 se thats happends? -- Thanx in advance! ;-)
15
1974
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
1914
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 had any success with working out how to do it and hoped someone might be able to help.
12
12184
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 way to get around this? I need to have "-" in variable names.
16
6751
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 = 3; I know its setting aside storage for the variable itself; does it also use up more storage if the variable name is longer? I realize it would probably take quite a lot of long variable names to make any impact if so, but I was just curious...
10
7133
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: ________ Last Name: ________
6
1892
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. Is it possible?
0
9893
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9737
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11000
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10663
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9499
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7069
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5734
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4544
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 we have to send another system
2
4136
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.