Question: A header file, projlex.h, will be provided for you. You MUST use the provided header file. You may NOT change it. Header File Provided: #ifndef
A header file, projlex.h, will be provided for you. You MUST use the provided header file. You may NOT change it.
Header File Provided:
#ifndef PROJLEX_H_
#define PROJLEX_H_
#include
#include
using std::string;
using std::istream;
using std::ostream;
enum TType {
// keywords
SET,
PRINT,
VAR,
REPEAT,
// an identifier
IDENT,
// an integer and string constant
ICONST,
SCONST,
// the operators, parens and semicolon
PLUS,
MINUS,
STAR,
COLON,
LSQ,
RSQ,
LPAREN,
RPAREN,
SC,
// any error returns this token
ERR,
// when completed (EOF), return this token
DONE
};
class Token {
TType tt;
string lexeme;
int lnum;
public:
Token() {
tt = ERR;
lnum = -1;
}
Token(TType tt, string lexeme, int line) {
this->tt = tt;
this->lexeme = lexeme;
this->lnum = line;
}
bool operator==(const TType tt) const { return this->tt == tt; }
bool operator!=(const TType tt) const { return this->tt != tt; }
TType GetTokenType() const { return tt; }
string GetLexeme() const { return lexeme; }
int GetLinenum() const { return lnum; }
};
extern ostream& operator
extern Token getNextToken(istream *in, int *linenum);
#endif /* PROJLEX_H_ */



-In C++
The lexical rules of the language are as follows: i. The language has identifiers, which are defined to be a letter followed by zero or more ii. The language has integer constants, which are defined to be an optional leading dash ii. The language has string constants, which are a double-quoted sequence of characters iv. The language has reserved the keywords var, print, set, and repeat. They will be the v. The language has several single-character tokens. They are +-*U; which will be letters or numbers. This will be the token IDENT. (for a negative number), followed by one or more digits. This will be the token ICONST. all on the same line. This will be the token SCONST tokens VAR, PRINT, SET, and REPEAT. the tokens PLUS MINUS STAR COLON LSQ RSQ LPAREN RPAREN SC A comment is all characters from a # to the end of the line; it is ignored and is not returned as a token. NOTE that a # in the middle of an SCONST is NOT a comment! vii. Whitespace between tokens can be used for readability. It serves to delimit tokens. vii. The newline should be recognized so that lines can be counted. ix. An error will be denoted by the ERR token. x. End of file will be denoted by the DONE token. Note that any error detected by the lexical analyzer will result in the ERR token, with the lexeme value equal to the string recognized when the error was detected. The lexical rules of the language are as follows: i. The language has identifiers, which are defined to be a letter followed by zero or more ii. The language has integer constants, which are defined to be an optional leading dash ii. The language has string constants, which are a double-quoted sequence of characters iv. The language has reserved the keywords var, print, set, and repeat. They will be the v. The language has several single-character tokens. They are +-*U; which will be letters or numbers. This will be the token IDENT. (for a negative number), followed by one or more digits. This will be the token ICONST. all on the same line. This will be the token SCONST tokens VAR, PRINT, SET, and REPEAT. the tokens PLUS MINUS STAR COLON LSQ RSQ LPAREN RPAREN SC A comment is all characters from a # to the end of the line; it is ignored and is not returned as a token. NOTE that a # in the middle of an SCONST is NOT a comment! vii. Whitespace between tokens can be used for readability. It serves to delimit tokens. vii. The newline should be recognized so that lines can be counted. ix. An error will be denoted by the ERR token. x. End of file will be denoted by the DONE token. Note that any error detected by the lexical analyzer will result in the ERR token, with the lexeme value equal to the string recognized when the error was detected
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
