<C language> implement the ToDos
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ERRQUIT(msg) (err_quit((msg), __FILE__, __func__,
__LINE__))
#define MATCH(buf, c) (match(buf, c) || ERRQUIT("unexpected
char"))
struct Buffer
{
char str[100];
int index;
};
int err_quit(char *msg, const char *file, const char *func, int
line)
{
printf("Error in function %s (%s, line %d):
%s\n",
func, file, line,
msg);
exit(1);
return 0;
}
char get_char(struct Buffer *buf)
{
char retchar = buf->str[buf->index];
return retchar;
}
int match(struct Buffer *buf, char c)
{
if (get_char(buf) != c)
return 0;
buf->index++;
return 1;
}
void parse_add_exp(struct Buffer *buf);
void parse_mul_exp(struct Buffer *buf);
void parse_factor(struct Buffer *buf);
void parse_exp(struct Buffer *buf)
{
// TODO: print out the header that defines a fmt
string for printf and
// includes typical assembler
directives along with the start of
// the main function
parse_add_exp(buf);
MATCH(buf, '\0'); // Verify we completed parsing
the input string
// TODO: print ouf the footer that calls printf
with the parameters
// fmt and the evaluation
result
}
//
// parse_add_exp - This handles addition and subtraction
operations.
// it pops 2 values from the top of the
stack (with 'popl') and
// then generates the appropriate operation
(add or subtract). Finally,
// it pushes the result back onto the
stack.
//
void parse_add_exp(struct Buffer *buf)
{
parse_mul_exp(buf);
while (1)
{
char c = get_char(buf);
if (c == '+' || c == '-')
{
MATCH(buf, c);
parse_mul_exp(buf);
// TODO: pop two
numbers from the stack,
//
write an assembler operation to perform the additive (+/-)
function
//
and push the result
}
else
return;
}
}
//
// parse_mul_exp - This generates instructions to handle a
multiply
// It pops two elements at the top of the stack
into eax and ebx,
// generates a multiply instruction and pushes the
result onto the stack.
// It also handles division operations. It should
pop the two values
// into ecx and eax, set ebx to zero, perform the
operation and push
// the result.
//
void parse_mul_exp(struct Buffer *buf)
{
parse_factor(buf);
while (1)
{
char c = get_char(buf);
if (c == '*' || c == '/' || c ==
'%')
{
MATCH(buf, c);
parse_factor(buf);
// TODO: pop two
numbers from the stack,
//
write an assembler operation to perform the function
//
(multiplication, division, or mod) and push the result
//
Note: Be careful with the division and mod results
regarding
//
which register has the proper result value.
}
else
return;
}
}
//
// parse_factor - This analyzes the buffer characters and reads
a constant
// or if the next character is '(', it calls
parse_add_exp() to handle
// the contained expression. If it is a number, it
pushes the value
// onto the stack for use in an upcoming
operation.
//
void parse_factor(struct Buffer *buf)
{
char c = get_char(buf);
if ('0' <= c && c <= '9')
{
MATCH(buf, c);
// TODO: Generate a push instruction
to push the number to the stack
}
else if (c == '(')
{
MATCH(buf, '(');
parse_add_exp(buf);
MATCH(buf, ')');
}
else
ERRQUIT("unexpected char");
}
int main(int argc, char **argv)
{
struct Buffer mbuf;
fprintf(stderr, "enter expr: ");
scanf("%99s", mbuf.str);
mbuf.index = 0;
parse_exp(&mbuf);
}
-Expected output
$ gcc parse_expr.c -o parser
$ ./parser > compute.s
enter expr: 1+(2+3)*4+5
$ gcc compute.s
$ ./a.out result : 26
implement the ToDos #include #include #include #define ERRQUIT(msg) (err_qu
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am