Question: reorganize this source code / * * * * * * Header definitions * * * * * * / % { #include #include #include

reorganize this source code /****** Header definitions ******/%{
#include #include #include // function prototypes from lex int yyerror(char *s); int yylex(void); // addString func added int addString(char* str); int debug=0; // set to 1 to turn on extra printing /* Not used in this code, but this can be used in Compiler 1to save the constant strings that need output in thedata section of the assembly output */ char* savedStrings[100]; int lastStringIndex=0; void outputDataSec(); %}/* token value data types */%union { int ival; char* str; }/* Starting non-terminal */%start wholeprogram%type program functions function statements statement funcall arguments argument expression /* Token types */%token KWPROGRAM KWCALL ID STRING KWFUNCTION %token LBRACE RBRACE SEMICOLON LPAREN RPAREN COMMA NUMBER %token PLUS %%/******* Rules *******/ wholeprogram: functions program {//int argRegNum =0; if (debug) fprintf(stderr,"wholeprogram rule
"); printf("#
# RISC-V assembly output
#
"); outputDataSec(); printf("#
# Program instructions
#
\t.text
program:
%s",$2); printf("\tli\ta0,0
\tli\ta7,93
\tecall
"); printf("#
# Functions
#
%s
",$1); printf("#
# Library functions
#
"); printf("# Print a null-terminated string: arg: a0== string address
printStr:
\tli\ta7,4
\tecall
\tret
"); printf("# Print a decimal integer: arg: a0== value
printInt:
\tli\ta7,1
\tecall
\tret
"); } program: KWPROGRAM LBRACE statements RBRACE {if (debug) printf("yacc: program block
"); // $$ = "program block parsed"; $$ = strdup($3); } functions: function functions { if (debug) printf("yacc: multiple functions
"); char *code =(char*) malloc(strlen($1)+ strlen($2)+8); strcpy(code, $1); strcat(code, $2); $$ = code; }|/* empty */{ if (debug) printf("yacc: no functions
"); $$ = strdup(""); } function: KWFUNCTION ID LPAREN RPAREN LBRACE statements RBRACE { if (debug) printf("yacc: function definition
"); char *code =(char*) malloc(strlen($6)+128); // Assembly code for function sprintf(code,"%s:
%s\tret
", $2, $6); $$ = code; } statements: statement statements{if (debug) printf("yacc: multiple statements
"); // join both statements // space for both statements char *code =(char*) malloc(strlen($1)+ strlen($2)+8); strcpy(code, $1); strcat(code, $2); $$ = code; }|/*empty*/{if (debug) printf("yacc: no statements
"); $$ = strdup(""); } statement: funcall {if (debug) printf("yacc: function call
"); $$ = $1; } funcall: KWCALL ID LPAREN arguments RPAREN SEMICOLON {// if (debug) printf("function call!
"); if (debug) fprintf(stderr, "function call rule
"); char *code =(char*) malloc(strlen($4)+strlen($2)+32); // bytes of assembly code sprintf(code,"%s\tjal\t%s
",$4,$2); // RISC-V instruction $$ = code; //argRegNum =0; } arguments: /* empty */{ $$

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!