Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello I just need the last part the project2.cpp source file. Thank you Its sometimes useful to make a little language for a simple problem.

Hello I just need the last part the project2.cpp source file. Thank you

Its sometimes useful to make a little language for a simple problem. We are making a language to let us play

with strings and numbers, and for this assignment we are building a lexical analyzer for this simple language.

Here are the lexical rules for the language:

1. The language has identifiers. An identifier starts with a letter and is followed by zero or more letters.

2. The language has string constants. A string constant is a sequence of characters, all on one line, enclosed

in double quotes. You do not have to support special handling for escape characters.

3. The language has integer constants, defined as a sequence of digits.

4. The language has 4 operators: +, -, *, and =

5. The language has 3 keywords: print, int and string.

6. Statements in the language end in a semicolon.

7. The language supports parentheses.

8. White space is used to separate tokens and lines for readability.

9. A comment begins with two slashes (//) and ends at a newline.

The lexical analyzer is to be implemented in a C++ function. The function will be passed a pointer to an input

stream to read from. It will return a Token representing the token and lexeme that has been recognized.

The definitions for the Token class and unique values for each of the tokens (TokenType) that you must

recognize is provided in the header file p2lex.h, which is on the course website.

The lexical analyzer must ignore white space and comments, using them only to note separation between

tokens. The program should maintain an external integer named linenum, which should be initialized to 1 and

incremented whenever a newline is seen by the lexer.

A lexical error should cause the token ERR to be returned. An end of file should cause DONE to be returned.

You MUST use the p2lex.h header file for your assignment. You may not change it. You do not need to hand

in p2lex.h, but it doesnt matter if you do. I will compile and test your program against the distributed p2lex.h

header file

You must produce and submit three files:

1. p2lex.cpp

This file should #include p2lex.h and should provide an implementation for the copy constructor, the

assignment operator, and the << operator to print out a Token.

The printed version of the token must be a string representation of the TokenType. We define the string

representation of a TokenType to be the symbol for the TokenType in lowercase letters. For example,

the string representation of the TokenType value PRINTKW should be printkw.

For the VAR, SCONST, ICONST and ERR tokens, the string representation must be followed by the

lexeme for the token. The lexeme should be printed in parentheses.

2. getToken.cpp

This file should #include p2lex.h and provide an implementation of the getToken function. The function

should read from the stream pointed to by the first argument and return the token that it recognizes.

3. project2.cpp

This file should #include p2lex.h and provide a main program to test the lexical analyzer

The specifications for the main program are as follows:

1. The program should accept at most one command line argument. If present, this one command

line argument is the name of a file to open and read for input. If not present, the program should

read from the standard input.

2. There may also, optionally, be an initial command line argument equal to the string -v. If it is,

then your program should be in verbose mode and should print each token that it recognizes in

addition to the remaining items.

3. The program should loop repeatedly, calling getToken(), until getToken returns an ERR or

DONE token

4. If the loop stopped because of an ERR token, it should print out the token

5. The program should print out a count of the number of times each type of token was seen.

6. The program should print out the lexeme for any string, integer or variable that appears more

than once

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The header file which was provided.

/* * p2lex.h * * CS280 Spring 2016 Project 2 */ #ifndef P2LEX_H_ #define P2LEX_H_ #include #include using namespace std;; class Token { public: // defines all of the known token types enum TokenType { VAR, // an identifier SCONST, // a string enclosed in "" ICONST, // an integer constant // four operators PLUSOP, // the + operator MINUSOP, // the - operator STAROP, // the * operator EQOP, // the assignment op // three keywords PRINTKW, // print INTKW, // int STRKW, // string LPAREN, // left parenthesis RPAREN, // right parenthesis SC, // the semicolon ERR, // some error condition was reached DONE, // end of file }; private: TokenType ttype; // the token type for this instance string tlexeme; // the corresponding lexeme public: // constructor has default values Token(TokenType t = ERR, string l = "") { ttype = t; tlexeme = l; } // copy constructor Token(const Token&); // getters const TokenType getType() const { return ttype; } const string getLexeme() const { return tlexeme; } // equality/inequality test against a token type; bool operator==(const Token::TokenType tt) const { return ttype == tt; } bool operator!=(const Token::TokenType tt) const { return ttype != tt; } // assignment operator const Token& operator=(const Token& rhs); // for printing out using << friend ostream& operator<<(ostream& out, const Token& t); }; extern Token getToken(istream* br); extern bool pushbackToken(Token& t); extern int linenum; #endif /* P2LEX_H_ */

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

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions