⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scan.h

📁 编译器模拟程序
💻 H
字号:
#ifndef _SCAN_H_
#define _SCAN_H_

#include <iostream>
#include <string>
#include <deque>
#include <vector>

using namespace std;

/* MAXRESERVED = the number of reserved words */
const int MAXRESERVED = 6;

/* BUFLEN = length of the input buffer for
source code lines */
const int BUFLEN = 256;

/* MAXTOKENLEN is the maximum size of a token */
const int MAXTOKENLEN = 40;

typedef enum 
	/* book-keeping tokens */
	{
		ENDFILE,ERROR,
		/* reserved words */
		IF,ELSE,INT,RETURN, VOID, WHILE,
		/* multicharacter tokens */
		ID,NUM,
		/* special symbols */
		ASSIGN,NOTASSIGN,EQ,LT,LEQ,BT,BEQ,PLUS,MINUS,TIMES,OVER,LPAREN,RPAREN,
		LFK,RFK,LDK,RDK,SEMI, COMMA
	} TokenType;

struct tokenForUse{

	TokenType type;
	char tokenValue[MAXTOKENLEN+1];
	tokenForUse(){};
	tokenForUse::tokenForUse(TokenType t,char c);
	~tokenForUse(){};
};
struct treenode{

	struct tokenForUse content;
	treenode* leftChild;
	treenode* rightChild;
	treenode();
	treenode(tokenForUse &t);
	~treenode(){};
};

/*table of reserved words */
static struct{ 
	
	char* str;
	TokenType tok;
} reservedWords[MAXRESERVED]

	= {{"if",IF},{"else",ELSE},{"int", INT},
	{"return", RETURN},{"void", VOID},{"while", WHILE}};

class compiler{
	
private:
	
	deque<treenode *> nodes;
	vector<treenode *> verNodes;
	vector<treenode *>::iterator verNodesIter;
	
	/* TraceScan = TRUE causes token information to be
	* printed to the listing file as each token is
	* recognized by the scanner
	*/
	bool TraceScan;
	
	/* tokenString array stores the lexeme of each token */
	char tokenString[MAXTOKENLEN+1];
	
	int getNextChar(void);
	
	void ungetNextChar(void);
	
	/* Procedure printToken prints a token 
	* and its lexeme to the listing file
	*/
	void printToken(TokenType, const char* );
	
	TokenType reservedLookup (char * s);

	void error();
	void match(TokenType type);
	
public:
	
	tokenForUse token;
	/* source code text file */
	FILE* source; 
	
	/* listing output text file */
	FILE* listing; 

	compiler(){
		
		source = fopen("input.txt", "r");
		listing = fopen("out.txt", "w");
	};
	~compiler(){
		
		fclose(source);
		fclose(listing);
	}
	/* function getToken returns the 
	* next token in source file
	*/
	tokenForUse getToken(void);
	treenode* lexp_seq();
	treenode* lexp();
	int getResult(treenode* &root);
	void printResult(treenode* &root);
	void printTree(treenode* &root);
};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -