Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need a C program to be converted to C++ (They are 2 files, parser & scanner) scan.c : #include stdio.h #include stdlib.h #include string.h

I need a C program to be converted to C++ (They are 2 files, parser & scanner)

scan.c :

#include "stdio.h" #include "stdlib.h" #include "string.h" #include "ctype.h"

#include "scan.h"

char token_image[100];

token scan() { static int c = ' '; /* next available char; extra (int) width accommodates EOF */ int i = 0; /* index into token_image */

/* skip white space */ while (isspace(c)) { c = getchar(); } if (c == EOF) return t_eof; if (isalpha(c)) { do { token_image[i++] = c; c = getchar(); } while (isalpha(c) || isdigit(c) || c == '_'); token_image[i] = '\0'; if (!strcmp(token_image, "read")) return t_read; else if (!strcmp(token_image, "write")) return t_write; else return t_id; } else if (isdigit(c)) { do { token_image[i++] = c; c = getchar(); } while (isdigit(c)); token_image[i] = '\0'; return t_literal; } else switch (c) { case ':': if ((c = getchar()) != '=') { fprintf(stderr, "error "); exit(1); } else { c = getchar(); return t_gets; } break; case '+': c = getchar(); return t_add; case '-': c = getchar(); return t_sub; case '*': c = getchar(); return t_mul; case '/': c = getchar(); return t_div; case '(': c = getchar(); return t_lparen; case ')': c = getchar(); return t_rparen; default: printf("error "); exit(1); } }

-------------------------------------------------------------------------------------------------------------------------

parse.c :

#include "stdio.h" #include "stdlib.h"

#include "scan.h"

const char* names[] = {"read", "write", "id", "literal", "gets", "add", "sub", "mul", "div", "lparen", "rparen", "eof"};

static token input_token;

void error () { printf ("syntax error "); exit (1); }

void match (token expected) { if (input_token == expected) { printf ("matched %s", names[input_token]); if (input_token == t_id || input_token == t_literal) printf (": %s", token_image); printf (" "); input_token = scan (); } else error (); }

void program (); void stmt_list (); void stmt (); void expr (); void term_tail (); void term (); void factor_tail (); void factor (); void add_op (); void mul_op ();

void program () { switch (input_token) { case t_id: case t_read: case t_write: case t_eof: printf ("predict program --> stmt_list eof "); stmt_list (); match (t_eof); break; default: error (); } }

void stmt_list () { switch (input_token) { case t_id: case t_read: case t_write: printf ("predict stmt_list --> stmt stmt_list "); stmt (); stmt_list (); break; case t_eof: printf ("predict stmt_list --> epsilon "); break; /* epsilon production */ default: error (); } }

void stmt () { switch (input_token) { case t_id: printf ("predict stmt --> id gets expr "); match (t_id); match (t_gets); expr (); break; case t_read: printf ("predict stmt --> read id "); match (t_read); match (t_id); break; case t_write: printf ("predict stmt --> write expr "); match (t_write); expr (); break; default: error (); } }

void expr () { switch (input_token) { case t_id: case t_literal: case t_lparen: printf ("predict expr --> term term_tail "); term (); term_tail (); break; default: error (); } }

void term_tail () { switch (input_token) { case t_add: case t_sub: printf ("predict term_tail --> add_op term term_tail "); add_op (); term (); term_tail (); break; case t_rparen: case t_id: case t_read: case t_write: case t_eof: printf ("predict term_tail --> epsilon "); break; /* epsilon production */ default: error (); } }

void term () { switch (input_token) { case t_id: case t_literal: case t_lparen: printf ("predict term --> factor factor_tail "); factor (); factor_tail (); break; default: error (); } }

void factor_tail () { switch (input_token) { case t_mul: case t_div: printf ("predict factor_tail --> mul_op factor factor_tail "); mul_op (); factor (); factor_tail (); break; case t_add: case t_sub: case t_rparen: case t_id: case t_read: case t_write: case t_eof: printf ("predict factor_tail --> epsilon "); break; /* epsilon production */ default: error (); } }

void factor () { switch (input_token) { case t_id : printf ("predict factor --> id "); match (t_id); break; case t_literal: printf ("predict factor --> literal "); match (t_literal); break; case t_lparen: printf ("predict factor --> lparen expr rparen "); match (t_lparen); expr (); match (t_rparen); break; default: error (); } }

void add_op () { switch (input_token) { case t_add: printf ("predict add_op --> add "); match (t_add); break; case t_sub: printf ("predict add_op --> sub "); match (t_sub); break; default: error (); } }

void mul_op () { switch (input_token) { case t_mul: printf ("predict mul_op --> mul "); match (t_mul); break; case t_div: printf ("predict mul_op --> div "); match (t_div); break; default: error (); } }

int main () { input_token = scan (); program (); return 0; }

-------------------------------------------------------------------------------------------------------------------------

It can be tested to see if it's working correctly by using a regular file with a simple code like this:

read A read B sum := A + B write sum write sum / 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions