📄 lexicalanalyser.h
字号:
/*
LexicalAnalyser.h: interface for the following classes:
CToken
CLexicalAnalyser
CExpression
CSymbol
CIntLiteralExpression
CRealLiteralExpression
Author: Terence Kam
Note
----
1. You may use this code for free, but I will greatly appreaciate it if
you give proper credits and acknowledgement when you use my code.
2. I'm not responsible if my code cause any damage to anybody or
their property. Although my code tested well and behaved properly
in my computer system, I can't guarantee that it will do so in
other computer systems. Therefore, use the code AT YOUR OWN RISK.
3. My code is tested using Microsoft Visual C++ 6.0
4. I extracted this code from an existing project. If there are any
unnecessary compiler directives (for eg. the !define(AFX_PARSER_H
... below) feel free to remove it.
5. Any bug fix or improvement is greatly welcomed! :-)
6. Good luck and fruitful programming!
*/
#if !defined(AFX_PARSER_H__7830FD60_54C6_11D2_8864_9D0DD3024277__INCLUDED_)
#define AFX_PARSER_H__7830FD60_54C6_11D2_8864_9D0DD3024277__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <afx.h>
#include <afxtempl.h>
enum {nilT, idT, intT, realT, stringT, errorT, endlineT, endfileT};
class CLexicalAnalyser;
//This class is a token
class CToken
{
friend class CLexicalAnalyser;
public:
CToken();
virtual ~CToken();
int getTokenType() { return TokenType;}
CString getTokenName() { return TokenName;}
int getTokenInt(){ return TokenInt;}
double getTokenReal(){ return TokenReal;}
protected:
int TokenType;
CString TokenName;
union
{
int TokenInt;
double TokenReal;
};
};
//This class represent one symbol
class CSymbol
{
public:
CSymbol();
CSymbol( CString string, int type);
virtual ~CSymbol();
CString SymbolString;
int TokenType;
bool operator < (CSymbol &Symbol);
bool operator <= (CSymbol &Symbol);
bool operator > (CSymbol &Symbol);
bool operator >= (CSymbol &Symbol);
};
//This class is the lexical analyser
class CLexicalAnalyser
{
public:
void SetCurrentPosition(POSITION pos);
POSITION GetCurrentPosition();
void ClearAllTokens();
void ResetPosition();
bool IsAllAplhaOrDigit(CString s,int begin, int length);
BOOL IsSequenceEmpty();
void AddToken(CToken token);
CToken NextToken();
CToken GetCurrentToken();
void String2TokenSequence(CString sequence);
void setSymbol(CSymbol Symbol);
CLexicalAnalyser();
virtual ~CLexicalAnalyser();
protected:
POSITION CurrentPosition;
CArray<CSymbol,CSymbol &> SymbolTable;
CList<CToken, CToken &> TokenSequence;
};
enum { realExpression, integerExpression};
//This class is an expression
class CExpression
{
protected:
unsigned short ExpressionType;
public:
CExpression();
virtual ~CExpression();
unsigned short GetExpressionType() {return ExpressionType;}
virtual double evalReal() {return 0;}
virtual int evalInt(){ return 0;}
};
//This class is an integer literal expression
class CIntLiteralExpression: public CExpression
{
protected:
int value;
public:
CIntLiteralExpression():value(0){ ExpressionType=integerExpression;}
CIntLiteralExpression(int val);
~CIntLiteralExpression() {}
virtual int evalInt() { return value; }
virtual double evalReal() { return (double)value;}
};
//This class is a real literal expression
class CRealLiteralExpression: public CExpression
{
protected:
double value;
public:
CRealLiteralExpression():value(0){ ExpressionType=integerExpression;}
CRealLiteralExpression(double val);
~CRealLiteralExpression() {}
virtual double evalReal() {return value;}
virtual int evalInt() { return (int)value;}
};
#endif // !defined(AFX_PARSER_H__7830FD60_54C6_11D2_8864_9D0DD3024277__INCLUDED_)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -