plxcompiler.h

来自「pl/x的编译器」· C头文件 代码 · 共 155 行

H
155
字号
/************************************************
 *                                              *
 * Purpose    :the body of the PLX Compiler     *
 * Created by :tinysun                          *
 *                                              *
 ************************************************/
#pragma warning(disable:4786) 
#include <string>
#include <set>
#include <algorithm>
#include <vector>
#include <iostream>

#ifndef PLXCOMPILER
#define PLXCOMPILER

using namespace std;

const int al=11;            //length of identifiers(如果一个标识符超过10个字符将被分割处理,这里有待改进,不过pl0也没考虑~_~)
const int norw=24;           //no. of reserved words
const int nmax=14;           //max no. of digits in numbers
const int maxerr=30;         //max no. of errors
const int lineLength=255+1;  //max length of a line(如果超过255个字符,以后的字符将当注释处理!!有待改进)
const int STATCKSIZE=500;

typedef enum
{
	NUL,IDENT,/*aident,bident,pident,*/NUMBER,PLUS,MINUS,TIMES,SLASH,
	EQL,NEQ,LSS,LEQ,GTR,GEQ,LPAREN,RPAREN,COMMA,SEMICOLON,
	PERIOD,BECOMES,PROGSYM,INTESYM,LOGISYM,IFSYM,THENSYM,
	ELSESYM,WHILESYM,REPEATSYM,BEGINSYM,ENDSYM,ORSYM,ANDSYM,
	NOTSYM,TRUESYM,FALSESYM,DOSYM,UNTILSYM,WRITESYM,READSYM,
	FORSYM,FROMSYM,TOSYM,PROSYM,CALLSYM
}symbol;

typedef enum
{
	LIT,OPR,LOD,STO,CAL,INT,JMP,JPC,ADP/*传递函数参数*/
}fct;

typedef char alfa[al];  //type of identifier

//整型,逻辑、函数
typedef enum
{
	aident,bident,pident
}objectt;

//符号表项
typedef struct
{
	alfa      name;  //变量名(函数、整型、逻辑型)
	objectt   kind;  //类型
	int       num ;  //函数参数个数
	int       level; //层级
	int       adr  ; //偏移量
	int       size ; //局部变量+函数参数的大小

}item;

//定义指令格式
typedef struct
{
	fct    f;   //指令码
	int    l;   //层号
	int    a;   //偏移量
}instruction;

//定义跟随符号集合类型
typedef set<symbol> symset;
typedef vector<instruction>  PCODE;

class PLXCompiler
{
private:
	char     m_ch; //last character read
	symbol   m_sym; //last symbol 
    alfa     m_id; //last identifier read
	int      m_num; //last number read
	int      m_cc; //character count
	int      m_ll; //line length
	int      m_ln; //line no. of code
	int      m_cx; //记录当前生成的指令编号
	int      m_tx; //记录当前插入到符号表元素的编号
	bool     m_isEnd;//is reach end of the source file
	char  m_line[lineLength];//code line buffer

	vector<item> m_table;   //symbol table
	PCODE        m_code;   
	int          m_s[STATCKSIZE];
	int          m_p,m_b,m_t;

	symset m_firsts;  //非终结符s的头符号集合
	symset m_follows; //非终结符s的跟随符号集合
	symset m_followss; //非终结符ss的跟随符号集合
	symset m_followae; //非终结符ae的跟随符号集合
	symset m_followbe;  //非终结符be的跟随符号集合
	symset m_firstaf;  //非终结符af的跟随符号集合
	symset m_firstbf;  //非终结符bf的跟随符号集合
public:
	int      m_err; //no. of error
	FILE    *m_pfSource;//,*m_pfError,*m_pfResult; //handle of source file
protected:
	void Init();
	void removeNote(char *pstr,int len);  //去除注释
	bool isTable(char ch);                //判断ch是否为制表字符
public:
	void listCode(void);

    //begin accidence analyze(词法分析器)
	void getCh();
	void getSym();
	//end analyze

	//语法分析器
	void RelationExpression(int level,symset fsys);
	void ArithmeticExpression(int level,symset fsys);
	void ArithmeticTerm(int level,symset fsys);
	void ArithmeticFactor(int level,symset fsys);
	void BoolExpression(int level,symset fsys);
	void BoolTerm(int level,symset fsys);
	void BoolFactor(int level,symset fsys);
	void Procedure(int level,symset fsys);
	void ProcedureSque(int level,symset fsys);
	void Sentence(int level,symset fsys);
	void Definition(int level,int &dx,symset fsys);
	void SentenceSque(int level,symset fsys);
	void DefinitionSque(int level,int &dx,symset fsys);
	void Program(symset fsys);

	//生成中间代码
	void gen(fct f,int l,int a);
	//

	//PLX虚拟机
	int base(int l);
	void plxInterpret();
	//symbol table operation
	void enter(objectt k,int &dx,int lev);   //enter a  ident item into symbol table
	void enter(objectt k,int lev);  
	vector<item>::iterator position(alfa name); //find the item in the table by name


	//出错处理
	void error(int errorNum);
	void test(symset s1,symset s2,int errorNum);

    //Contruct function
	PLXCompiler();
	PLXCompiler(char * pScource);
	
	//Distruct function
	~PLXCompiler();
};
#endif

⌨️ 快捷键说明

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