2 Replies - 702 Views - Last Post: 19 November 2012 - 03:05 AM Rate Topic: -----

#1 dekker13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 12-September 11

[LEX] Strange abort trap error after inserting range of input.

Posted 18 November 2012 - 02:12 PM

I am creating a lex file that reads in C code and selects the identifiers in the code and their subsequent line numbers found. I am using a test file to see if everything is running smoothly, however almost half way through inputting the lines from STDIN, it exits with an 'Abort trap'. Anyone have a clue? :mellow:/>/>/> Thanks for looking.

Here's what I've input and the error I receive:
Posted Image

Here's my sample input:
Posted Image
%{
#include <stdio.h>
#include <string.h>
    char identifier[1000][100];
    char linesFound[1000][1000];
    void insertId(int, const char*);
    int i = 0;
    int lineNum = 1;
%}
NEWLINE \n

%x comment
%x double_quote

%%
{NEWLINE}                  lineNum++;

auto                        ;
break                       ;
float                       ;
for                         ;
goto                        ;
if                          ;
int                         ;
long                        ;

void                        ;
while                       ;
[*]*[a-zA-Z][a-zA-Z0-9_]*     insertId(lineNum, yytext);
[^a-zA-Z0-9_\n\/*"']+            ;
[0-9]+                      ;

%%

main()
{
    yylex();
    if(lineNum==1) 
    {
        printf("The number of lines we have are: %d\n", lineNum);
    }
    else
    {
        printf("The number of lines we have are: %d\n", lineNum-1);
    }

    int m;
    for(m=0; m<i; m++)
    {
        printf("identifier[%d] is: \"%s\" found on lines: %s\n", m, identifier[m], linesFound[m]);
    }
}

void insertId(int nLine, const char* s)
{
    char num[2];
    sprintf ( num, "%d", nLine);
    
    int iter;
    for(iter = 0; iter <= i; iter++)
    {
        if ( strcmp(identifier[iter], s) == 0 )
        {
            strcat( linesFound[iter], ", " );
            strcat( linesFound[iter], num );
            return;
        }
    }

    strcpy( identifier[i], s );
   // strcat( identifier[i], ": " );
    strcpy( linesFound[i], num );

    i++;
   
}




Is This A Good Question/Topic? 0
  • +

Replies To: [LEX] Strange abort trap error after inserting range of input.

#2 dekker13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 12-September 11

Re: [LEX] Strange abort trap error after inserting range of input.

Posted 18 November 2012 - 04:22 PM

Thanks to the moderator that moved this to the right forum. I was confused because it contains 'C' code but yet it's within Lex.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,037
  • Joined: 21-June 11

Re: [LEX] Strange abort trap error after inserting range of input.

Posted 19 November 2012 - 03:05 AM

Your num array only has space for two bytes - that's one digit plus the 0-terminator. So once your line number becomes two digits long, your memory gets corrupted.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1