I want to generate a symbol table from a SIC Assembly code. For that I have tried to separate every strings form the assembly code.
Hence while attempting first step for that, I tried this code.
Here,
What I have tried is to read the assembly code form a file line by line. And then to separate the strings in the line to tokens.
When I remove the token separation section (as mentioned in the code) I am getting all lines printed as expected.
But If I run along with token separation, the first line is getting read and the tokens are separated as I expected. But for the second line I am getting error as segmentation fault.
I couldn’t trace where I went wrong.
So, I need explanations form experts.
Thanks in advance.
FILE* sourceCode = fopen("/home/muthu/LangFiles/SIC/PASS1/PROGRAM.txt","r"); if(checkForFileOpeningErrors() == ERROR) //Iam using separate function return EXIT_FAILURE; //Terminate the program. int maxLineLength = 50; while(1) { char* lineReader = NULL; // since getline will reallocate. if( getline(&lineReader,(size_t*)&maxLineLength,sourceCode) == -1 ) break; printf("%s",lineReader); // TOKEN SEPARATION STARTS HERE.... If I comment this section out iam getting all lines printed char* wordReader; wordReader = strtok(lineReader," \n"); printf("%s\n",wordReader); while(1) { wordReader = strtok(NULL," \n"); printf("%s\n",wordReader); } // TOKEN SEPARATION ENDS HERE.... }
My FILE:
COPY START 1000
FIRST STL RETADR
CLOOP JSUB RDREC
LDA LENGTH
COMP ZERO
JEQ ENDFIL
JSUB WRREC
J CLOOP
.
.
.
END
My Sample output:
muthu@muthu-G31M-ES2L:~/LangFiles/PASS1$ ./a.out
All Files successfully opened!! Operation has begun...
COPY START 1000
COPY
START
1000
segmentation Fault.
When do you expect this loop to terminate?