Lexical Analyzer in C++

Hi everyone, here is my project for spring break. I need to code a lexical analyzer. Here's what a got so far but I need help putting everything together.
I have 2 source files and 1 header which was provided by the instructor.


this is my main prog2part2.cpp

#include <iostream>
#include <fstream>
#include <vector>
#include <cctype>
#include <map>
#include "projlex.h"
using namespace std;

bool vflag=false, mciflag = false, sumflag = false;

int main(int argc, char *argv[]){




for( int i=1; i<argc; i++ )
{
if( argv[i][0] != '-' ) continue;
string str( argv[i] );
if( str == "-v" ) vflag=true;
else if( str == "-mci" ) mciflag=true;
else if( str == "-sum" ) sumflag=true;
else { cout << "INVALID FLAG" << " " << str << endl; return 1; }
}


bool filefound = false;
ifstream file;

for(int i=1; i<argc; i++){

if(argv[i][0] == '-')
continue;

if(filefound){

cout << "TOO MANY FILE NAMES" << endl;
return 1;
}

filefound = true;
file.open(argv[i]);
if(!file.is_open()){

cout << "UNABLE TO OPEN" << " "<< argv[i] << endl;
return 1;
}

}


return 0;
}

lexical.cpp (lexical analyzer function)

#include "projlex.h"
#include <string> /* STRING */
#include <iostream> /* COUT, ENDL */
#include <fstream>

using namespace std;

Token getNextToken(istream *in, int *linenum){

Token T;
string curr;
char ch =in->peek();

if(ch == ';'){
//curr = ";";
return Token(SC, ';', linenum*);
}
else if( ch == '+'){
//curr = "+";
return Token(PLUS, '+', linenum*);
}
else if(ch == '-'){
//curr = "-";
return Token(MINUS, '-', linenum*);
}
else if( ch == ':'){
//curr = ":";
return Token(COLON, ':', linenum*);
}
else if(ch =='['){
//curr = "[";
return Token(LSQ, '[', linenum*);
}
else if(ch == ']'){
//curr = "]";
return Token(RSQ, ']', linenum*);
}
else if(ch == '('){
//curr = "(";
return Token(LPARAN, '(', linenum*);
}
else if(ch ==')'){
//curr = ")";
return Token(RPARAN, ')', linenum*);
}
else if(ch =='*'){
//curr= "*";
return Token(STAR, '*', linenum*);
}
if (isdigitch){
ch = in->peek();
while(isalpha(ch)){
curr + = ch;
}
T.tt = IDENT;
T.lexeme = curr;
}
else if(ch == '"'){
while (ch = in->peek()){
curr + = ch;
if( ch == '"'){
T.lexeme = curr;
T.tt= SCONST;
break;
}
}
}

return Token(SC, "", 1);
}

projlex.h (header provided by the instructor)

/*
* projlex.h
*
* CS280
* Spring 2018
*/

#ifndef PROJLEX_H_
#define PROJLEX_H_

#include <string>
#include <iostream>
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<<(ostream& out, const Token& tok);

extern Token getNextToken(istream *in, int *linenum);


#endif /* PROJLEX_H_ */
Last edited on
You have not asked a question.

Also, it would greatly help if you edited your post, and put [code] and [/code] tags around your code, so that it is highlighted and spaced properly.
Last edited on
Topic archived. No new replies allowed.