Andy2002
asked on
Tokens and parsing
Below is the code a program I am writing. It is a simple assembly simulator. I succesfully read memory from a file and store it in an array and set all my registers to 0. I have a file as follows where the first number tells me how many commands are in the file.
8
load R1,R8
add R1,16
loadind R2,R1
add R1,8
loadind R3,R1
add R3,R2
sub R1,4
storeind R3,R1
I have stored the above in the array code[]. Now I want to read each array elemnt and split its contents into tokens - eg load, R1 and R8 would all be tokens. How do I separet them so I can then process what they mean? At present they each line is stored in an array, but i don't know how to sepeare them. Also this is a standard file input, so should be possible for any input file. How do I just read the first line of a file in this case the 8) so i know how many commands to expect?
My whole program is shown below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char *STRNG;
STRNG code[20];
int registers[8];
int memory[20];
STRNG get_line(FILE * inf)
{
STRNG s;
int tok = getc(inf);
int count = 0;
s =(char *) malloc(80);
while (tok != '\n' && tok != EOF)
{
//s[count++] = tok;
s[count++] = (char) tok;
tok = getc(inf);
}
s[count] = '\0';
return s;
}
void setRegisters(){
int i;
for (i=0; i<8; i++)
registers[i] = 0;
}
void getMemory(FILE *inf) {
STRNG line;
int i = 0;
while (strlen(line = get_line(inf)))
{
printf ("%s\n", line);
memory[i++] = atoi(line);
}
}
void showMemory(){
int i;
for (i=0;i<20;i++){
printf("%d", memory[i]);}
}
int main (int argc, char *argv[])
{
FILE *data, *assembly;
if ( argc < 3 )
{
printf("Invalid arguments");
exit(EXIT_FAILURE);
}
if ( (assembly = fopen(argv[1], "r")) == NULL )
{
printf("Cannot open assembly file");
exit(EXIT_FAILURE);
}
if ( (data= fopen(argv[2], "r")) == NULL )
{
printf("Cannot open data file");
exit(EXIT_FAILURE);
}
showMemory("/n");
getMemory(data);
showMemory();
setRegisters();
int lines=8;
int count = 0;
for(count =0;count<lines;count++) code[count]= get_line(assembly);
for(count = 0;count<lines;count++) printf("%s\n",code[count]) ;
return 1;
}
regards
8
load R1,R8
add R1,16
loadind R2,R1
add R1,8
loadind R3,R1
add R3,R2
sub R1,4
storeind R3,R1
I have stored the above in the array code[]. Now I want to read each array elemnt and split its contents into tokens - eg load, R1 and R8 would all be tokens. How do I separet them so I can then process what they mean? At present they each line is stored in an array, but i don't know how to sepeare them. Also this is a standard file input, so should be possible for any input file. How do I just read the first line of a file in this case the 8) so i know how many commands to expect?
My whole program is shown below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef char *STRNG;
STRNG code[20];
int registers[8];
int memory[20];
STRNG get_line(FILE * inf)
{
STRNG s;
int tok = getc(inf);
int count = 0;
s =(char *) malloc(80);
while (tok != '\n' && tok != EOF)
{
//s[count++] = tok;
s[count++] = (char) tok;
tok = getc(inf);
}
s[count] = '\0';
return s;
}
void setRegisters(){
int i;
for (i=0; i<8; i++)
registers[i] = 0;
}
void getMemory(FILE *inf) {
STRNG line;
int i = 0;
while (strlen(line = get_line(inf)))
{
printf ("%s\n", line);
memory[i++] = atoi(line);
}
}
void showMemory(){
int i;
for (i=0;i<20;i++){
printf("%d", memory[i]);}
}
int main (int argc, char *argv[])
{
FILE *data, *assembly;
if ( argc < 3 )
{
printf("Invalid arguments");
exit(EXIT_FAILURE);
}
if ( (assembly = fopen(argv[1], "r")) == NULL )
{
printf("Cannot open assembly file");
exit(EXIT_FAILURE);
}
if ( (data= fopen(argv[2], "r")) == NULL )
{
printf("Cannot open data file");
exit(EXIT_FAILURE);
}
showMemory("/n");
getMemory(data);
showMemory();
setRegisters();
int lines=8;
int count = 0;
for(count =0;count<lines;count++) code[count]= get_line(assembly);
for(count = 0;count<lines;count++) printf("%s\n",code[count])
return 1;
}
regards
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just use lex or flex on input file and attach it with yacc or bision
lex will separate ur tokens and yacc/bison will parse it.
Why do u want to do all thing by yourself ? Let lex/yacc to do the job for u.
lex will separate ur tokens and yacc/bison will parse it.
Why do u want to do all thing by yourself ? Let lex/yacc to do the job for u.
No comment has been added lately and this question is therefore classified abandoned.
If asker wishes to close the question, then refer to
https://www.experts-exchange.com/help/closing.jsp
Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to jstiff
Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Sunny
EE Cleanup Volunteer
If asker wishes to close the question, then refer to
https://www.experts-exchange.com/help/closing.jsp
Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to jstiff
Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Sunny
EE Cleanup Volunteer
(i hope u are interested only inthe number .. which says number of lines to follow)
#include <stdio.h>
main(){
FILE *fp=fopen("filename.dat","
int num;
char buf[32];
if(fp){
fscanf(fp,"%d\n",&num);
while(num--){
fgets(buf,32,fp);
printf("%s",buf);//handle each command line
}
}
}