Question

  • I have 2 classes together with their header files called lexer.cpp and lexer.h , token.cpp and token.h
  • These are needed to create a compiler. At the moment I'm still doing the lexemes, dividing and reading the Tokens.
  • Now I have a method which will read every character I load from the file, and when for example it identifies that it is an integer, it will return a Token (which are the Tokens in token.cpp)

The Problem:

In token.cpp I have an enum class Token which has all the tokens that can be read by my compiler (coding will be provided below)

Now in

  1. Lexer.cpp I have to call this class (enum class Token in Token.cpp). In token.h I just declared the class name
  2. Lexer.h I wrote the name of the method that is to be used in Lexer.cpp

But they are generating some errors.


Error: Error


Code:

Lexer.cpp

Token Lexer::getNextToken(char ch)
{
    return Token::tkDigit;
}

lexer.h

#ifndef lexer
#define lexer
#include "token.h"
class Lexer
{
public:

    char ReadChar();
    bool IsDigit (char ch);
    bool IsAlpha (char ch);
    bool IsIdentation (char ch);
    Token getNextToken (char ch);
};

#endif // lexer.h

Token.h

#ifndef token
#define token


enum class Token
{
    tkLetter,
    tkDigit,
    tkPrintable,
    tkType,

    //Literals
    tkBooleanLiteral,
    tkIntegerLiteral,
    tkRealLiteral,
    tkCharLiteral,
    tkStringLiteral,
    tkUnitLiteral,
    tkLiteral,
    tkIndentifier,

    //Operations
    tkMultpicativeOp,
    tkAdditiveOp,
    tkRelationalOp,

    //Params and Calls

    tkActualParams,
    tkFunctionCall,

    //cast and factors
    tkTypeCast,
    tkSubExpression,
    tkUnary,
    tkFactor,

    //Terms and Declarations

    tkTerm,
    tkSimpleExpression,
    tkExpression,
    tkAssignment,
    tkVariableDecl,
    tkFormalParam,
    tkFunctionDecl,

    //Statements
    tkReadStatement,
    tkWriteStatement,
    tkIfStatement,
    tkWhileStatement,
    tkHaltStatement,
    tkStatement,
    tkBlock,
    tkSxl,

};


#endif // token
Was it helpful?

Solution 2

Move definition of enum class Token from token.cpp in header token.h replacing the definition of the ampty enumeration with the same name and include this header in lexer.h

As I said move the enumeration declaration from token.cpp in token.h. Is it clear?

And function getNextToken should look as

Token Lexer::getNextToken(char ch)
{
    return Token::tkDigit;
}

OTHER TIPS

In Lexer.h you are using a type (Token) of which the compiler (given lexer.h dependencies) knows nothing.

Just include the declaration of Token, with an include

#ifndef lexer
#define lexer
#include "token.h"

class Lexer
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top