Answered You can hire a professional tutor to get the answer.
I have to code in c++, and I'm given these files project 2.
I have to code in c++, and I'm given these files
project 2.cc
/*
* Copyright (C) Mohsen Zohrevandi, 2017
*
* Do not share this file with anyone
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main (int argc, char* argv[])
{
int task;
if (argc < 2)
{
cout << "Error: missing argumentn";
return 1;
}
/*
Note that by convention argv[0] is the name of your executable,
and the first argument to your program is stored in argv[1]
*/
task = atoi(argv[1]);
// TODO: Read the input grammar at this point from standard input
/*
Hint: You can modify and use the lexer from previous project
to read the input. Note that there are only 4 token types needed
for reading the input in this project.
WARNING: You will need to modify lexer.cc and lexer.h to only
support the tokens needed for this project if you are going to
use the lexer.
*/
switch (task) {
case 1:
cout << "decl idList idList1 colon ID COMMA n";
break;
case 2:
// TODO: perform task 2.
break;
case 3:
// TODO: perform task 3.
break;
case 4:
// TODO: perform task 4.
break;
case 5:
// TODO: perform task 5.
break;
default:
cout << "Error: unrecognized task number " << task << "n";
break;
}
return 0;
}
-----------------------------------------------------------------------------------------------
lexer.h
#ifndef __LEXER__H__
#define __LEXER__H__
#include <vector>
#include <string>
#include "inputbuf.h"
// Lexer modified for FIRST & FOLLOW project
typedef enum { END_OF_FILE = 0, ARROW, HASH, DOUBLEHASH, ID, ERROR } TokenType;
class Token {
public:
void Print();
std::string lexeme;
TokenType token_type;
int line_no;
};
class LexicalAnalyzer {
public:
Token GetToken();
TokenType UngetToken(Token);
LexicalAnalyzer();
private:
std::vector<Token> tokens;
int line_no;
Token tmp;
InputBuffer input;
bool SkipSpace();
Token ScanId();
};
#endif //__LEXER__H__
-----------------------------------------------------------------------------------------------
lexer.cc
#include <iostream>
#include <istream>
#include <vector>
#include <string>
#include <cctype>
#include "lexer.h"
#include "inputbuf.h"
using namespace std;
// Lexer modified for FIRST & FOLLOW project
string reserved[] = { "END_OF_FILE", "ARROW", "HASH", "DOUBLEHASH", "ID", "ERROR" };
void Token::Print()
{
cout << "{" << this->lexeme << " , "
<< reserved[(int) this->token_type] << " , "
<< this->line_no << "}n";
}
LexicalAnalyzer::LexicalAnalyzer()
{
this->line_no = 1;
tmp.lexeme = "";
tmp.line_no = 1;
tmp.token_type = ERROR;
}
bool LexicalAnalyzer::SkipSpace()
{
char c;
bool space_encountered = false;
input.GetChar(c);
line_no += (c == 'n');
while (!input.EndOfInput() && isspace(c)) {
space_encountered = true;
input.GetChar(c);
line_no += (c == 'n');
}
if (!input.EndOfInput()) {
input.UngetChar(c);
}
return space_encountered;
}
Token LexicalAnalyzer::ScanId()
{
char c;
input.GetChar(c);
if (isalpha(c)) {
tmp.lexeme = "";
while (!input.EndOfInput() && isalnum(c)) {
tmp.lexeme += c;
input.GetChar(c);
}
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.line_no = line_no;
tmp.token_type = ID;
} else {
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.lexeme = "";
tmp.token_type = ERROR;
}
return tmp;
}
// you should unget tokens in the reverse order in which they
// are obtained. If you execute
//
// t1 = lexer.GetToken();
// t2 = lexer.GetToken();
// t3 = lexer.GetToken();
//
// in this order, you should execute
//
// lexer.UngetToken(t3);
// lexer.UngetToken(t2);
// lexer.UngetToken(t1);
//
// if you want to unget all three tokens. Note that it does not
// make sense to unget t1 without first ungetting t2 and t3
//
TokenType LexicalAnalyzer::UngetToken(Token tok)
{
tokens.push_back(tok);;
return tok.token_type;
}
Token LexicalAnalyzer::GetToken()
{
char c;
// if there are tokens that were previously
// stored due to UngetToken(), pop a token and
// return it without reading from input
if (!tokens.empty()) {
tmp = tokens.back();
tokens.pop_back();
return tmp;
}
SkipSpace();
tmp.lexeme = "";
tmp.line_no = line_no;
input.GetChar(c);
switch (c) {
case '-':
input.GetChar(c);
if (c == '>') {
tmp.token_type = ARROW;
} else {
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.token_type = ERROR;
}
return tmp;
case '#':
input.GetChar(c);
if (c == '#') {
tmp.token_type = DOUBLEHASH;
} else {
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.token_type = HASH;
}
return tmp;
default:
if (isalpha(c)) {
input.UngetChar(c);
return ScanId();
} else if (input.EndOfInput())
tmp.token_type = END_OF_FILE;
else
tmp.token_type = ERROR;
return tmp;
}
}
-----------------------------------------------------------------------------------------------
inputbuf.h
#ifndef __INPUT_BUFFER__H__
#define __INPUT_BUFFER__H__
#include <string>
class InputBuffer {
public:
void GetChar(char&);
char UngetChar(char);
std::string UngetString(std::string);
bool EndOfInput();
private:
std::vector<char> input_buffer;
};
#endif //__INPUT_BUFFER__H__
-----------------------------------------------------------------------------------------------
inputbuffer.cc
#include <iostream>
#include <istream>
#include <vector>
#include <string>
#include <cstdio>
#include "inputbuf.h"
using namespace std;
bool InputBuffer::EndOfInput()
{
if (!input_buffer.empty())
return false;
else
return cin.eof();
}
char InputBuffer::UngetChar(char c)
{
if (c != EOF)
input_buffer.push_back(c);;
return c;
}
void InputBuffer::GetChar(char& c)
{
if (!input_buffer.empty()) {
c = input_buffer.back();
input_buffer.pop_back();
} else {
cin.get(c);
}
}
string InputBuffer::UngetString(string s)
{
for (unsigned i = 0; i < s.size(); i++)
input_buffer.push_back(s[s.size()-i-1]);
return s;
}
----------------------------------------------------------------------
Please help. I have no clue at the moment,
Im only asking for case 1.
I can figure out the rest.