Visit Our UNIX and Linux User Community

Lex: concatenating word into array of c-strings

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Lex: concatenating word into array of c-strings
# 1  
Old 11-16-2012
Lex: concatenating word into array of c-strings

Alright, so I'm writing a file for the lexical analyzer (lex). It will be used to check C code (collecting the identifiers and storing those names along with the line numbers the identifier was found on). I'm not used to 'C' so I'm having some difficulty.

I am using a function (insertId()) to actually add to the c-string array.
#include <stdio.h>
#include <string.h>
    const char* id[1000];
    void insertId(char*, int);
    int i = 0;
    int newLineCnt = 1;

[a-zA-Z][a-zA-Z0-9_]+  {
                            id[i] = yytext;
                           // printf("The word found was: %s and 'i' is: %d\n", id[i], i);
                            insertId(yytext, newLineCnt);
"\n"                   newLineCnt++; 
[a-z]              printf("Lowercase word\n");
[A-Z]              printf("Uppercase word\n");
[0-9]              printf("Integer\n");
";"                printf("Semicolon");
"("                printf("Open parentheses\n");
")"                printf("Close parentheses");
void insertId(char* str, int nLine)
    char num[2];
    sprintf ( num, "%d", nLine);
    static char string[100];
    int iter;
    for(iter = 0; iter < i+1; iter++)
        if ( strcmp(str, id[iter]) == 0 )
            strcat( string, ", " );
            strcat( string, num );
            strcat ( id[iter], string );
            //printf("The word found was: %s\n", id[iter]);

   // printf("That string was: %s\n", str);
    strcpy ( string, str);
    strcat ( string, ": ");
    sprintf ( num, "%d", nLine);         
    strcat (string, num);
  //  sprintf ( num, "%d", nLine);
  //  strcat (string, num);
    id[i] = string;

    //printf("The word found was:   %s on line %d\n", id[i-1], nLine);

Sample input:
Output should look like this below:
argc: 4, 5, 11, 13, 16, 18
argv: 4, 6, 11, 14, 17
 exit: 23
fputs: 17
       i: 8, 16, 17, 18
main: 4
  nflg: 8, 10, 12, 21
putchar: 19, 22
 sccsid: 1
 stdout: 17

# 2  
Old 11-16-2012

Last edited by Corona688; 11-17-2012 at 02:26 PM..
# 3  
Old 11-17-2012
Originally Posted by Corona688
Thanks for the reply! I don't clearly understand the code you've provided me. In fact I'm a bit confused Smilie. Is there any other way to append the line numbers other than what I think I'm seeing (dynamic allocation)?

---------- Post updated at 11:47 AM ---------- Previous update was at 11:05 AM ----------

Does the code you've provided store a linked list of line numbers (integers)? I'm trying to print out one inputted word and the line number found and it's giving me '0' for the line number. Line number is originally set to '1' in the program.

    printf("%s and line numbers are:%d\n", id[0].token, id[0].list[id[0].len]);

Last edited by Corona688; 11-17-2012 at 02:27 PM..
# 4  
Old 11-17-2012
# 5  
Old 11-17-2012
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.

Previous Thread | Next Thread
Test Your Knowledge in Computers #815
Difficulty: Medium
In CSS, E[foo] matches an E element with a 'foo' class name.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenating 2 lines from 2 files having matching strings

Hello All Unix Users, I am still new to Unix, however I am eager to learn it.. I have 2 files, some lines have some matching substrings, I would like to concatenate these lines into one lines, leaving other untouched. Here below is an example for that.. File 1 (fasta file): >292183... (6 Replies)
Discussion started by: Mohamed EL Hadi
6 Replies

2. Shell Programming and Scripting

Concatenating strings and run it in bash

Hi, all, I tried to write a simple shell script as follow: #!/bin/bash # What want to do in bash is following # : pcd_viewer cloud_cluster_0.pcd cloud_cluster_1.pcd cloud_cluster_2.pcd cloud_cluster_3.pcd cloud_cluster_4.pcd STR = "pcd_viewer" for i in `seq 0 4` do STR... (1 Reply)
Discussion started by: bedeK
1 Replies

3. Shell Programming and Scripting

Problem in concatenating two Strings

Hi Friends, I'm new to shell scripting and trying to concatenate two Strings to create a filepath like string but I'm getting an unexpected result. here is my code for '': script_dir="$(dirname $0)" echo "Script Dir:$script_dir" dirtest1="/installedUtility"... (6 Replies)
Discussion started by: kuldeept
6 Replies

4. Shell Programming and Scripting

How to preserve space while concatenating strings? (KSH)

I have these str1=$(echo "This is string one with spaces \n This is also my sentence 1") When I echo $str1, it displays the new line character properly. Now I have another new variable say str2. I want to concatenate in this way.. str1 + newline character + and then str2. That's I... (3 Replies)
Discussion started by: dahlia84
3 Replies

5. Shell Programming and Scripting

CSH: Concatenating Strings, how to add new line character and functions?

Hello, I'm trying to run a program on a directory (traverse sub dirs too) through my csh script. Arrays support in CSH is appalling, something like associative arrays would have helped me do this so much easier. Anyway, I want to hold some details extracted from the program and then at the... (0 Replies)
Discussion started by: ragabonds
0 Replies

6. Shell Programming and Scripting

concatenating strings

I m new to shell scripting and what i want is take as an i/p from command line the name of the file and inside my script i should redirect the o/p of my few commands to this file concatenated with .txt for example if i give ./linux filename i should get the o/p in filename.txt i need to... (2 Replies)
Discussion started by: tulip
2 Replies

7. Programming

Concatenating array of strings into one string separated by spaces

Hi, Well as the title says, I have an array of strings (delimited by null). The length of the array is variable and length of each string is variable as well. What I need is one huge string with the original strings in the array separated by spaces. For example is an array is such that array... (12 Replies)
Discussion started by: newhere
12 Replies

8. Shell Programming and Scripting

mailx: concatenating strings for message body (KSH)

Hi all, Think this is a pretty simple problem, but I've been thinking about it for a few days. Let's say that I'm going to have to output the contents of a file as the body of a mailx message. I'll probably do this: cat <filename> | mailx <extra commands> However, how do I go about doing... (1 Reply)
Discussion started by: rockysfr
1 Replies

9. Shell Programming and Scripting

concatenating strings..

hey guys.. probably a simple question but i cant seem to find any info on it. i have a small array of strings, and i want to concatenate the contents of the array into one big string. any ideas on how i can do this? cheers. (2 Replies)
Discussion started by: jt_csv
2 Replies

10. Shell Programming and Scripting

Concatenating Strings

Is there any function to concatenate strings in shell script (2 Replies)
Discussion started by: radhika03
2 Replies

Featured Tech Videos