ADD to the below another scan format to parse that reads: Input lines: 1. addu $
ID: 3886048 • Letter: A
Question
ADD to the below another scan format to parse that reads:
Input lines:
1. addu $v0, $zero, $s10 ( with spaces between the source codes)
2. J L1
/*
*
* For detailed info regarding fgets, refer manpage at linprog
* % man 3 fgets
*
*/
#include <stdio.h>
#define MAXLINE 80
#define MAXREG 5
int main()
{
char line[MAXLINE]={0};
char oper[MAXLINE];
char rd[MAXREG],rs[MAXREG],rt[MAXREG];
while (fgets(line, MAXLINE, stdin)) {
/* check if a 3-address R format instruction */
if (sscanf(line, "%s $%[^,],$%[^,],$%s", oper, rd, rs, rt) == 4) {
printf("input line: %s ", line);
printf("parsed line: op:%10s rd:%5s rs:%5s rt:%5s ",
oper, rd, rs, rt);
}
/* you need to add other patterns for R,I,and J encoding */
else {
printf("input line: %s ", line);
printf("you need to add sscanf format ");
}
}
return 0;
}
Explanation / Answer
The code was developed
#include <stdio.h>
#define MAXLINE 80
#define MAXREG 5
int main()
{
char line[MAXLINE]={0};
char oper[MAXLINE];
char rd[MAXREG],rs[MAXREG],rt[MAXREG];
while (fgets(line, MAXLINE, stdin)) {
/* check if a 3-address R format instruction */
if (sscanf(line, "%s $%[^,],$%[^,],$%s", oper, rd, rs, rt) == 4) {
printf("input line: %s ", line);
printf("parsed line: op:%10s rd:%5s rs:%5s rt:%5s ",
oper, rd, rs, rt);
}
/* you need to add other patterns for R,I,and J encoding */
else {
printf("input line: %s ", line);
printf("you need to add sscanf format ");
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.