448,572 Members | 1,225 Online
Need help? Post your question and get tips & solutions from a community of 448,572 IT Pros & Developers. It's quick & easy.

# Parse variable names from string

 P: n/a 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 #include 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 Replies

 P: n/a On Jul 3, 12:53 pm, Victor Lagerkvist #include 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

 P: n/a user923005 wrote: On Jul 3, 12:53 pm, Victor Lagerkvist Hello, I have the need to parse variable names from a string and savethem somewhere safe for future usage. Here's my first attempt (I don'thave any rules for valid names yet) - but I have a feeling that it'sunnecessary complex? Any input would be greatly appreciated.#include #include int get_ops(char *sen, char ***atom, char limit); 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

 P: n/a On Jul 3, 2:47 pm, Victor Lagerkvist #include int get_ops(char *sen, char ***atom, char limit); 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

 P: n/a user923005 wrote: On Jul 3, 2:47 pm, Victor Lagerkvist user923005 wrote: On Jul 3, 12:53 pm, Victor Lagerkvist #include #include >int get_ops(char *sen, char ***atom, char limit); 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 thevariables (there's only one "type") and the number of them - everythingelse is a bonus! They are given by the user from standard input, line byline, such as:<< build a, b, cAnd that's all there is (more or less any types of names should beallowed), 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 discussion thread is closed

Replies have been disabled for this discussion.