Introduction

This is the eight session of the Expert System course using C++ and Qt. Now we’re gonna implement our PROPLexAnalyzer class using Qt containers and functions.

There’s a function that is a little complex to this video ( getToken()) that we’re gonna discuss in the following video.

So let’s start!

Code

The constructor and destructor are quite simple, just setting values and delete them (automatically):

QPROPLexAnalizer::QPROPLexAnalizer():m_nLastPos(0)
{
    m_inputs.clear();
}

QPROPLexAnalizer::~QPROPLexAnalizer()
{
}

The next function is  buildToken() that is gonna create a LEXEM type variable and returning. This is useful for debugging purposes:

//If we want to create a Token manually
QPROPLexAnalizer::LEXEM QPROPLexAnalizer::buildToken(LEXEM::Type type, QString token){
    QPROPLexAnalizer::LEXEM L;
    L.m_type=type;
    L.m_sToken=token;
    return L;
}

Next one, the pushFile() function that checks if a file was opened correctly and push it to the file stack, otherwise displays a debugging error:

//Get file with rules and added to our stack of files
bool QPROPLexAnalizer::pushFile(QString fileName){
    QFile* file= new QFile(fileName);
    if(file->open(QFile::ReadOnly | QFile::Text)){
        m_inputs.push(file);
        return true;
    }
	//Stream for debugging
    qDebug()<<"Error while uploading the file";
    return false;
}

popFile() function reverses the actions of the pushFile() function, deleting the first file in the stack:

//Eliminate the last file from the stack
void QPROPLexAnalizer::popFile(){
    if(m_inputs.empty())
        return;
    QFile* file=m_inputs.top();
    m_inputs.pop()->close();
    delete file;
}

Lastly, read() function checks if there’s a file and save it all to a QString:

//Read through the stream and set it to the end,
//then we need to charge the file again.
QString QPROPLexAnalizer::read(){
    if(!m_inputs.empty()){
        QTextStream stream(m_inputs.top());
        return stream.readAll();
    }
    return QString("empty");
}

You should also see our video where we developed these function step by step:

 

Thank you for seeing this video, remember to share the video, visit our web page and social media, and leave a comment if you have someone.

 

Leave a Reply

Your email address will not be published.