Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Translate the C++ Sebesta scanner the Go language. The scanner is explained conceptually in the Sebesta text Concepts of Programming Languages, Chapter 4. C++ Code

Translate the C++ Sebesta scanner the Go language. The scanner is explained conceptually in the Sebesta text Concepts of Programming Languages, Chapter 4.

C++ Code

// main.cpp (C++ version)

// sebestaScannerCpp

//#include

//#include

//#include

#include // I/O

#include // file I/O

#include // format manipulation

#include // unix standard header

//#include

using namespace std;

int charClass;

string lexeme;

char nextChar;

int token;

int nextToken;

ifstream in_fp;

void my_getChar();

int lex();

const int LETTER = 0;

const int DIGIT = 1;

const int UNKNOWN = 99;

const int INT_LIT = 10;

const int IDENT = 11;

const int ADD_OP = 21;

const int SUB_OP = 22;

const int MULT_OP = 23;

const int DIV_OP = 24;

const int LEFT_PAREN = 25;

const int RIGHT_PAREN = 26;

int main(int argc, const char * argv[]) {

in_fp.open("/Users/maida/src/xcodeGIT/sebestaScannerCpp/sebestaScannerCpp/front.in");

if (in_fp.is_open()) {

// cout << "front.in opened" << endl;

my_getChar();

do {

lex();

} while (nextToken!=EOF);

}

else {

cout << "Cannot open front.in" << endl;

char cwd[1024];

if (getcwd(cwd, sizeof(cwd)) != NULL) {

cout << "Current working dir: " << cwd << endl;

}

}

return 0;

}

void my_addChar() {

lexeme += nextChar; // string concatenation

}

void my_getChar() {

in_fp>>nextChar;

if (in_fp.eof())

charClass = EOF;

else {

if (isalpha(nextChar))

charClass = LETTER;

else if (isdigit(nextChar))

charClass = DIGIT;

else charClass = UNKNOWN;

}

// cout << nextChar<

}

void getNonBlank() {

while (isspace(nextChar))

my_getChar();

}

int lookup(char ch) {

switch (ch) {

case '(':

my_addChar();

nextToken = LEFT_PAREN;

break;

case ')':

my_addChar();

nextToken = RIGHT_PAREN;

break;

case '+':

my_addChar();

nextToken = ADD_OP;

break;

case '-':

my_addChar();

nextToken = SUB_OP;

break;

case '*':

my_addChar();

nextToken = MULT_OP;

break;

case '/':

my_addChar();

nextToken = DIV_OP;

break;

default:

my_addChar();

nextToken = EOF;

break;

}

return nextToken;

}

int lex() {

lexeme = "";

getNonBlank();

switch (charClass) {

case LETTER:

my_addChar();

my_getChar();

while (charClass == LETTER || charClass == DIGIT) {

my_addChar();

my_getChar();

}

nextToken = IDENT;

break;

case DIGIT:

my_addChar();

my_getChar();

while (charClass == DIGIT) {

my_addChar();

my_getChar();

}

nextToken = INT_LIT;

break;

case UNKNOWN:

lookup(nextChar);

my_getChar();

break;

case EOF:

nextToken = EOF;

lexeme = "EOF";

}

cout<<"Next token is: "<

return nextToken;

}

The Test file it reads front.in

(sum + 47) / total

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

Recommended Textbook for

Advances In Databases And Information Systems Second East European Symposium Adbis 98 Poznan Poland September 1998 Proceedings Lncs 1475

Authors: Witold Litwin ,Tadeusz Morzy ,Gottfried Vossen

1st Edition

3540649247, 978-3540649243

More Books

Students also viewed these Databases questions

Question

=+4. What are their reputations?

Answered: 1 week ago

Question

What is electric dipole explain with example

Answered: 1 week ago