Sponsored Content
Homework and Emergencies Homework & Coursework Questions Lex: analyzing a C file and printing out identifiers and line numbers they're found on Post 302732387 by D2K on Saturday 17th of November 2012 12:00:45 PM
Old 11-17-2012
Lex: analyzing a C file and printing out identifiers and line numbers they're found on

Florida State University, Tallahassee, FL USA, Dr. Whalley, COP4342

1. The problem statement, all variables and given/known data:
Create a lex specification file that reads a C source program that ignores keywords and collects all identifiers (regular variable names) and also displays the line numbers each was found on.

I am having trouble with concatenating the line numbers in the 'C' function I created. I either get a segmentation fault or some compiler error. I wrote a program like this in Perl, but I've never written any code in C like this.

2. Relevant commands, code, scripts, algorithms:
Image


3. The attempts at a solution (include all code and scripts):
Code:
%{
#include <stdio.h>
#include <string.h>
    const char* id[1000];
    void insertId(char*, int);
    int i = 0;
    int newLineCnt = 1;
%}

%%
auto;
break;
case;
char;
continue;
default;
do;
double;
else;
extern;
float;
for;
goto;
if;
int;
long;
register;
return;
short;
sizeof;
static;
struct;
switch;
typedef;
union;
unsigned;
void;
while;
[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]);
            return;
        }
    }



    i++;
    
   // 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);
}

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Printing line numbers

Maybe this question is out there, but I searched and didnt see it. To print my files I use more filename | lpr -Pprinter I would like to print my scripts with line numbers. How do I do this? (2 Replies)
Discussion started by: MizzGail
2 Replies

2. Shell Programming and Scripting

Print Selection of Line between two Identifiers.

I have a following containing DATA in the following format: DATA....------ --------------- -------------- DATA.....------ -------------------- ------------------ DATA....------ --------------- -------------- I want to extract the selective DATA in between identifiers and ... (4 Replies)
Discussion started by: parshant_bvcoe
4 Replies

3. Shell Programming and Scripting

Assign Line Numbers to each line of the file

Hi! I'm trying to assign line numbers to each line of the file for example consider the following.. The contents of the input file are hello how are you? I'm fine. How about you? I'm trying to get the following output.. 1 hello how are you? 2 I'm fine. 3 How about you? ... (8 Replies)
Discussion started by: abk07
8 Replies

4. Shell Programming and Scripting

grep on string and printing line after until another string has been found

Hello Everyone, I just started scripting this week. I have no background in programming or scripting. I'm working on a script to grep for a variable in a log file Heres what the log file looks like. The x's are all random clutter xxxxxxxxxxxxxxxxxxxxx START: xxxxxxxxxxxx... (7 Replies)
Discussion started by: rxc23816
7 Replies

5. Shell Programming and Scripting

Printing the line number of first column found

Hello, I have a question on how to find the line number of the first column that contains specific data. I know how to print all the line numbers of those columns, but haven't been able to figure out how to print only the first one that is found. For example, if my data has four columns: 115... (3 Replies)
Discussion started by: user553
3 Replies

6. Shell Programming and Scripting

cut a line into different fields based on identifiers

cat fileanme.txt custom1=, custom2=, userPulseId=3005, accountPolicyId=1, custom3=, custom4=, homeLocationId=0, i need to make the fields appear in next line based on identifier (,) ie comma so output should read cat fileanme.txt custom1=, custom2=, userPulseId=3005, ... (8 Replies)
Discussion started by: vivek d r
8 Replies

7. Homework & Coursework Questions

[solved]Perl: Printing line numbers to matched strings and hashes.

Florida State University, Tallahassee, FL, USA, Dr. Whalley, COP4342 Unix Tools. This program takes much of my previous assignment but adds the functionality of printing the concatenated line numbers found within the input. Sample input from <> operator: Hello World This is hello a sample... (2 Replies)
Discussion started by: D2K
2 Replies

8. Shell Programming and Scripting

Printing unique numbers from each file

I have some files named file1, file2, fille3......etc. These files are in a folder f1. The content of files are shown below. I would like to count the unique pairs of third column in each file. some files have no data. It should be printed as zero. Your help would be appreciated. file1 ARG... (1 Reply)
Discussion started by: samra
1 Replies

9. Shell Programming and Scripting

Avoid printing entire line if string not found

so im searching the process table with: ps -ef | awk -F"./rello.java" '{ print substr($0, index($0,$2)) }' I only want it to print everything that's infront of the "./rello.java". That's because im basically getting the arguments that was passed to the rello.java script. this works. ... (2 Replies)
Discussion started by: SkySmart
2 Replies

10. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies
slarrr.f(3)							      LAPACK							       slarrr.f(3)

NAME
slarrr.f - SYNOPSIS
Functions/Subroutines subroutine slarrr (N, D, E, INFO) SLARRR Function/Subroutine Documentation subroutine slarrr (integerN, real, dimension( * )D, real, dimension( * )E, integerINFO) SLARRR Purpose: Perform tests to decide whether the symmetric tridiagonal matrix T warrants expensive computations which guarantee high relative accuracy in the eigenvalues. Parameters: N N is INTEGER The order of the matrix. N > 0. D D is REAL array, dimension (N) The N diagonal elements of the tridiagonal matrix T. E E is REAL array, dimension (N) On entry, the first (N-1) entries contain the subdiagonal elements of the tridiagonal matrix T; E(N) is set to ZERO. INFO INFO is INTEGER INFO = 0(default) : the matrix warrants computations preserving relative accuracy. INFO = 1 : the matrix warrants computations guaranteeing only absolute accuracy. Author: Univ. of Tennessee Univ. of California Berkeley Univ. of Colorado Denver NAG Ltd. Date: November 2011 Contributors: Beresford Parlett, University of California, Berkeley, USA Jim Demmel, University of California, Berkeley, USA Inderjit Dhillon, University of Texas, Austin, USA Osni Marques, LBNL/NERSC, USA Christof Voemel, University of California, Berkeley, USA Definition at line 95 of file slarrr.f. Author Generated automatically by Doxygen for LAPACK from the source code. Version 3.4.1 Sun May 26 2013 slarrr.f(3)
All times are GMT -4. The time now is 04:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy