Question

If i could get any guidance on how to get this started it will be great....

If i could get any guidance on how to get this started it will be great. My prof. literally just gave us the information below (which are literally just instructions) and I am unsure on how to get started.

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);
1Updated Oct 4
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 #include’s 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.
The main program is controlled by command line flags, as follows ● -v ; optional; if provided, be “verbose” and print every valid token that is recognized ● -s ; optional; if provided, print all string constants defined in the input ● -i ; optional; if provided, print all the identifiers defined in the input
In addition to the (optional) flags, the program takes 0 or 1 file name. If no filename is provided, the program should read the standard input. If one filename is provided, the program should take input from that file. If more than one filename is provided, the program should print TOO MANY FILES, and should stop.
If an unrecognized flag is provided (an arg beginning with a - and not matching one of the three listed flags above), the program should print the flag followed by INVALID FLAG, and should stop.
If the test program is unable to open a file, it must print the filename followed by FILE NOT FOUND, and should stop.
If the test program finds that getToken returns an error, it should print “Lexical error” followed by a printout of the token, and should stop.
If the -v option is provided, every token should be printed out, as specified below.
After reaching end of input (the T_DONE token), the output of the program should be as follows:
Token count: N Identifier count: N String count: N Strings: {strings in alphabetical order} (only if -s is specified) Identifiers: {identifiers in alphabetical order} (only if -i is specified)
When printing out a token, the program should print the TokenType and, if required, the lexeme. The printed representation for the TokenType is a string equal to the symbolic name of the token. For example, the string representation for a T_ID is the string “T_ID”.
In the case of T_ID, T_ICONST, T_SCONST and T_ERROR, the printed representation of the symbolic name is followed by the lexeme in parenthesis. So for example, if getToken recognizes “foo” as an identifier, the output for the token will be T_ID(foo). Cosmic hint: implement operator<< for Token to make printing out tokens easy.

-------------------------------------------

/*
* lexer.h

#ifndef LEXER_H_
#define LEXER_H_

#include <string>
#include <iostream>
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_ */

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This is a Lexer and Parser program which comes under compiler construction

You need to write a program which acts like a parser and identifies what every literal is

To create an object you need to do like this Token t(T_ID, "foo");

make a switch statement which checks the literal

it will be similar to this

Token getToken(istream* br)
{
   char ch = br.get();

   enum KeyWords {Begin, Kint, Kstring, KIconst, KSconst, Kid}

   KeyWords now = Begin; //No Begin keyword in lexer, make it here. now is where I am  right now, which one am i using "now"
   //lexeme == "";
   string lexeme == "";

   if(now == Begin && ch == '\n')
            {linecount++;}

   switch(now)
      case Begin: 
         if(isalpha(ch))
         {now = Kid;}

         else if(ch =='"') 
         {now = Kstring;}

         else if(isdigit(ch))
         {now = KIconst;}
  // Token* tok = new Token(T_ID,lexeme);

}

loop through all the enums that are listed then you can what type that literal is

Add a comment
Know the answer?
Add Answer to:
If i could get any guidance on how to get this started it will be great....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • lex.h ----------------- #ifndef LEX_H_ #define LEX_H_ #include <string> #include <iostream> using std::string; using std::istream; using std::ostream;...

    lex.h ----------------- #ifndef LEX_H_ #define LEX_H_ #include <string> #include <iostream> using std::string; using std::istream; using std::ostream; enum Token { // keywords PRINT, BEGIN, END, IF, THEN, // an identifier IDENT, // an integer and string constant ICONST, RCONST, SCONST, // the operators, parens, semicolon PLUS, MINUS, MULT, DIV, EQ, LPAREN, RPAREN, SCOMA, COMA, // any error returns this token ERR, // when completed (EOF), return this token DONE }; class LexItem { Token token; string lexeme; int lnum; public: LexItem()...

  • The first project involves modifying the attached lexical analyzer and the compilation listing ge...

    The first project involves modifying the attached lexical analyzer and the compilation listing generator code. You need to make the following modifications to the lexical analyzer, scanner.l: 1. A second type of comment should be added that begins with // and ends with the end of line. As with the existing comment, no token should be returned. 2. The definition for the identifiers should be modified so that underscores can be included, however, consecutive underscores, leading and trailing underscores should...

  • C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header ...

    C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header files given. Then make a main program using Book and Warehouse to read data from book.dat and have functions to list and find book by isbn Objectives: Class Association and operator overloading This project is a continuation from Project 1. The program should accept the same input data file and support the same list and find operations. You will change the implementation of...

  • BACKGROUND Movie Review sites collect reviews and will often provide some sort of average review to...

    BACKGROUND Movie Review sites collect reviews and will often provide some sort of average review to sort movies by their quality. In this assignment, you will collect a list of movies and then a list of reviews for each movie. You will then take a simple average (total of reviews divided by number of reviews) for each movie and output a sorted list of movies with their average review. Here you are provided the shell of a program and asked...

  • Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A...

    Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A compiler must examine tokens in a program and decide whether they are reserved words in the Java language, or identifiers defined by the user. Design a program that reads a Java program and makes a list of all the identifiers along with the number of occurrences of each identifier in the source code. To do this, you should make use of a dictionary. The...

  • Objectives You will implement and test a class called MyString. Each MyString object keeps track ...

    Objectives You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows. Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard...

  • You’ll create the parser with a 2-part layered approach. You’ll first create a Lexer, which will read characters from a file and output tokens. Then you’ll write the second part, which will process th...

    You’ll create the parser with a 2-part layered approach. You’ll first create a Lexer, which will read characters from a file and output tokens. Then you’ll write the second part, which will process the tokens and determine if there are errors in the program. This program have 3 files Lexer.java, Token.java, Parser.java Part 1 Lexer: The class Lexer should have the following methods: public constructor which takes a file name as a parameter○private getInput which reads the data from the...

  • The task is to write Song.cpp to complete the implementation of the Song class, as defined...

    The task is to write Song.cpp to complete the implementation of the Song class, as defined in the provided header file, Song.h, and then to complete a program (called sales) that makes use of the class. As in Project 1, an input data file will be provided containing the song name and other information in the same format as in Program 1: Artist    Song_Title    Year    Sales    Medium As before, the program (sales) which are individual copies, will use this information to compute the gross...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • For this lab you will be creating a class representing the shape square. A Square is...

    For this lab you will be creating a class representing the shape square. A Square is a special case of a Rectangle where both sides have the same length. Rectangle has been provided for your use in this lab. A Rectangle has a height and a width as member variables, two constructors, two member functions, area() and perimeter(), and lastly, an input and output operator. Your Square class should publicly inherit from Rectangle. You will need to update the Constructors...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT