Question
Creating a getToken file according to the header file given. Can someone help me create a getToken file according to the details given below and
Creating a getToken file according to the header file given.
Can someone help me create a getToken file according to the details given below and the header file "lexer.h" which i have below. I have been trying to do this and I can't get it, I tried switch statements and if-else statements but am still not doing it right. This is a C++ assignment.
For this assignment we are going to create a function that reads an input stream and classifies it into tokens from a language that we define here. In this language we make the following rules: An identifier is a letter followed by zero or more letters or numbers An integer constant is one or more digits A string constant is characters enclosed in double quotes, all on the same line The following keywords are defined: int string set print println There are four operators + - * / The left and right parenthesis are valid in the language A semicolon is valid in the language A comment begins with two slashes //. The two slashes and all characters up to a newline are discarded Any amount of whitespace can be used to delimit tokens. The whitespace is ignored.1 Anything that is not a recognized token is an error.
The file lexer.h is provided and must be used, unchanged, for this assignment.
All of the tokens that this assignment will recognize are provided in lexer.h as a member of the TokenType enum. For the assignment, you must write the function getToken. The function is defined in lexer.h as follows: extern Token getToken(istream* br);
The Token that is returned is a class that is defined in lexer.h. The getToken() function should return the next token... or T_ERROR if it cannot recognize a token or T_DONE when there is no more input. You should implement the getToken function in one source file that #includes lexer.h You must also implement a separate main function in a separate file. The main function is meant to serve as a test engine for the getToken function.
This is the lexer.h or header file.
#ifndef LEXER_H_ #define LEXER_H_
#include #include using std::string; using std::istream; using std::ostream;
enum TokenType { // keywords T_INT, T_STRING, T_SET, T_PRINT, T_PRINTLN,
// an identifier T_ID,
// an integer and string constant T_ICONST, T_SCONST,
// the operators, parens and semicolon T_PLUS, T_MINUS, T_STAR, T_SLASH, T_LPAREN, T_RPAREN, T_SC,
// any error returns this token T_ERROR,
// when completed (EOF), return this token T_DONE };
class Token { TokenType tt; string lexeme; int lnum;
public: Token(TokenType tt = T_ERROR, string lexeme = "") : tt(tt), lexeme(lexeme) { extern int lineNumber; lnum = lineNumber; }
bool operator==(const TokenType tt) const { return this->tt == tt; } bool operator!=(const TokenType tt) const { return this->tt != tt; }
TokenType GetTokenType() const { return tt; } string GetLexeme() const { return lexeme; } int GetLinenum() const { return lnum; } };
extern ostream& operator<<(ostream& out, const Token& tok);
extern Token getToken(istream* br);
#endif /* LEXER_H_ */
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started