跳到正文

个人记录

gramma

suxxsfe约 55 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月12日
篇幅
约 55 分钟

版本

  1. v1当前

未来程序-前体(

SEMI 表示分号 COMMA 表示逗号 PAREN 表示小括号 SQUARE_BRA 表示中括号 CURLY_BRA 表示大括号 NUMBER 任意位数的数字 ASSIGN 表示赋值

cin,cout.endl 应看作变量

plaint
program: (#include LESS name GREATER)* (using namespace name SEMI)* (functionAndVariable)*

functionAndVariable: INT name LPAREN (INT variable)? (COMMA INT variable)* RPAREN LCURLY_BRA statements RCURLY_BRA
	| INT variable (COMMA variable)* SEMI
	| EMPTY


variable: name( LSQUARE_BRA expr RSQUARE_BRA )*

name: _ | $ | LETTER ( _ | $ | LETTER | NUMBER )*

statements: (statement)*

statement: LCURLY_BRA statements RCURLY_BRA
	| for LPAREN statementInFor SEMI exprCanBeEmpty SEMI exprCanBeEmpty RPAREN statements
	| while LPAREN expr RPAREN statements
	| if LPAREN expr RPAREN statements
	| if LPAREN expr RPAREN statements else statements
	| return expr SEMI
	| break SEMI
	| continue SEMI
	| expr SEMI
	| INT variable (COMMA variable)* SEMI
	| EMPTY

statementInFor: exprCanBeEmpty
	| INT variable (COMMA variable)*

exprCanBeEmpty: expr
	| EMPTY

expr: expr9 ((LEFT_SHIFT | RIGHT_SHIFT) expr9)*

expr9: expr8 (ASSIGN expr8)*

expr8: expr7 (OR expr7)*

expr7: expr6 (AND expr6)*

expr6: expr5 (XOR expr5)*

expr5: expr4 ((EQUAL | NOT_EQUAL) expr4)*

expr4: expr3 ((LESS_OR_EQUAL | GREATER_OR_EQUAL | LESS | GREATER) expr3)*

expr3: expr2 ((PLUS | MINUS) expr2 )*

expr2: expr1 ((MUL | DIV | MOD) expr1)*

expr1: (PLUS | MINUS | NOT)* expr0

expr0: NUMBER
	| (expr)
	| callFunction
	| variable

callFunction: name LPAREN (expr (COMMA expr)*)? RPAREN

备份

C++
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<unordered_map>
struct Code{
	char s[1048576],*ip;
	int row,column;
	inline Code(){row=1;column=1;ip=s;}
	inline void read(){
		char *now=s;
		while((*now++=getchar())!=EOF);
	}
	inline void advance(int cnt=1){while(cnt--)if(*ip++=='\n') row++,column=1;}
};
namespace Interpreter{
/**************************** function and define *************************/
#define _EOF                0
#define INT                 1
#define EQUAL               2
#define NOT_EQUAL           3
#define LEFT_SHIFT          4
#define RIGHT_SHIFT         5
#define LESS_OR_EQUAL       6
#define GREATER_OR_EQUAL    7
#define LESS                8
#define GREATER             9
#define AND                 10
#define OR                  11
#define XOR                 12
#define NOT                 13
#define LPAREN              14
#define RPAREN              15
#define LSQUARE_BRA         16
#define RSQUARE_BRA         17
#define LCURLY_BRA          18
#define RCURLY_BRA          19
#define PLUS                20
#define MINUS               21
#define MUL                 22
#define DIV                 23
#define MOD                 24
#define ASSIGN              25
#define COMMA               26
#define SEMI                27
#define BREAK               28
#define CONTINUE            29
#define RETURN              30
#define INCLUDE             31
#define USING               32
#define NAMESPACE           33
#define FOR                 34
#define WHILE               35
#define IF                  36
#define ELSE                37
#define NUMBER              38
#define NAME                39
#define CHAR                40 //define but can not use
#define IOSTREAM            41
#define TOKEN_TYPE_NUM      38 //does not contain number or name
const char *tokenTypeName[]={
	"","int ","==","!=","<<",">>","<=",">=","<",">",
	"&&","||","^","!","(",")","[","]","{","}","+","-",
	"*","/","%","=",",",";",
	"break","continue","return","#include","using","namespace",
	"for","while","if","else",
	"(const) NUMBER","NAME (of variable or function)","CHAR (set but could not use)","IOSTREAM (set but could not use)"
};
#define HASH_BASE 822
inline int strMatch(const char *s,const char *t){
	for(;*t;s++,t++)if(*s!=*t) return 0;
	return 1;
}
inline int isDigit(const char &c){return c>='0'&&c<='9';}
inline int isAlpha(const char &c){return (c>='a'&&c<='z')||(c>='A'&&c<='Z');}
inline int isInvisible(const char &c){return c==' '||c=='\n'||c=='\t'||c=='\r';}
inline int isNameBeginning(const char &c){return isAlpha(c)||c=='_'||c=='$';}
inline int isName(const char &c){return isAlpha(c)||isDigit(c)||c=='_'||c=='$';}
inline int getNumber(Code &code,unsigned long long &val){
	for(val^=val;isDigit(*code.ip);code.advance()) val=val*10+((*code.ip)^48);
	return !isName(*code.ip);
}
inline unsigned long long getName(const char *s){
	unsigned long long res;
	for(res^=res;isName(*s);s++) res=res*HASH_BASE+(*s);
	return res;
}
inline int getName(Code &code,unsigned long long &name){
	for(name^=name;isName(*code.ip);code.advance()) name=name*HASH_BASE+(*code.ip);
	return 1;
}
/****************************** lexer analysis (make token) ******************************/
struct Token{
	int type,row,column;
	unsigned long long val;//val: for NUMBER mean the number, for NAME mean the hash of name
	inline Token(){}
	inline Token(int r,int c){row=r;column=c;type=val=0;}
	inline ~Token(){}
};
inline void quitLexerError(const Token &t,const char *info=""){
	printf("lexer error on row %d column %d, %s",t.row,t.column,info);
	exit(1);
}
inline Token getNextToken(Code &code){
	while(isInvisible(*code.ip)) code.advance();
	Token res(code.row,code.column);
	if(!*code.ip||*code.ip==EOF) return res.type=_EOF,res;
	for(int i=1;i<TOKEN_TYPE_NUM;i++)if(strMatch(code.ip,tokenTypeName[i])){
		res.type=i;
		code.advance(std::strlen(tokenTypeName[i]));
		return res;
	}
	if(isDigit(*code.ip)){
		res.type=NUMBER;
		if(getNumber(code,res.val)) return res;
		quitLexerError(res,"invalid number\n");
	}
	else if(isNameBeginning(*code.ip)){
		res.type=NAME;
		if(getName(code,res.val)) return res;
		quitLexerError(res,"invalid variable or function name\n");
	}
	else quitLexerError(res,"could not identify lexer\n");
	return res;
}
inline Token peekNextToken(Code &code){
	char *ip=code.ip;
	int row=code.row,column=code.column;
	Token ret=getNextToken(code);
	code.ip=ip;code.row=row;code.column=column;
	return ret;
}
/************************** syntax analysis (build AST) ************************/
inline void quitSyntaxError(const Token &token,int expectedType,const char *info=""){
	printf("sysntax error on row %d column %d, expected type \" %s \" but found type \" %s \", %s",
			token.row,token.column,tokenTypeName[expectedType],tokenTypeName[token.type],info);
	exit(1);
}
/*************** structs define **********/
#define EXPR                       100000
#define EMPTY                      100001
struct AST{};
struct ProgramNode;
struct IncludeNode;
struct UsingNamespaceNode;
struct FunctionDecLaredNode;
struct VariableNode;
struct StatementsNode;
struct StatementNode;
struct ForNode;
struct WhileNode;
struct IfNode;
struct CallFunctionNode;
struct BinOpNode;
struct UnaryOpNode;
struct StatementInForNode;
struct ExprCanBeEmptyNode;
struct NumberNode;
inline void newVector(std::vector<AST*>&v){std::vector<AST*>().swap(v);}
inline void deleteVector(std::vector<AST*>&v){for(AST *x:v) delete x;}
struct ProgramNode:AST{
//		program: (#include LESS name GREATER)* (using namespace name SEMI)* (functionAndVariable)*
	std::vector<AST*>children;
	inline ProgramNode(){newVector(children);}
	inline ~ProgramNode(){deleteVector(children);}
};
struct IncludeNode:AST{
	unsigned long long name;
	inline IncludeNode(unsigned long long _name){name=_name;}
	inline IncludeNode(){name=0;}
};
struct UsingNamespaceNode:AST{
	unsigned long long name;
	inline UsingNamespaceNode(unsigned long long _name){name=_name;}
	inline UsingNamespaceNode(){name=0;}
};
/*
functionAndVariable: INT name LPAREN variables LPAREN{ statements }
    | INT variable (COMMA variable)* SEMI
    | EMPTY
*/
struct FunctionDeclaredNode:AST{
	unsigned long long name;
	std::vector<AST*>parameters;//VariableNode
	AST *statements;//StatementsNode
	inline FunctionDeclaredNode(){name=0;newVector(parameters);statements=NULL;}
	inline ~FunctionDeclaredNode(){deleteVector(parameters);delete statements;}
};
struct VariableNode:AST{
	int type;//only INT
	unsigned long long name;
	std::vector<AST*>size;//size for each dimention off a array (ExprNode)
	inline VariableNode(){name=0;newVector(size);}
	inline ~VariableNode(){deleteVector(size);}
};
struct VariablesNode:AST{
	std::vector<AST*>variables;//VariableNode
	inline VariablesNode(){newVector(variables);}
	inline ~VariablesNode(){deleteVector(variables);}
};
struct StatementsNode:AST{
	std::vector<AST*>statements;//statementNode
	inline ~StatementsNode(){deleteVector(statements);}
	inline StatementsNode(){newVector(statements);}
};
struct StatementNode:AST{
	/*                                                                                        type
	statement: LCURLY_BRA statements RCURLY_BRA                                               LCURLY_BRA
    | for LPAREN statementInFor SEMI exprCanBeEmpty SEMI exprCanBeEmpty RPAREN statements     FOR
    | while LPAREN expr RPAREN statements                                                     WHILE
    | if LPAREN expr RPAREN statements                                                        IF
    | if LPAREN expr RPAREN statements else statements                                        ELSE
    | return expr SEMI                                                                        RETURN
    | break SEMI                                                                              BREAK
    | continue SEMI                                                                           CONTINUE
    | expr SEMI                                                                               EXPR
    | INT variable (COMMA variable)* SEMI                                                     INT
    | EMPTY                                                                                   EMPTY
	*/
	int type;
	AST *child,*child2;//for expr, BinOpNode or UnaryOpNode; child2 is only used to record the body of ELSE
	inline StatementNode(){type=0;child=NULL;}
	inline ~StatementNode(){delete child;}
};
struct ForNode:AST{
	AST *doFirst;//StatementInForNode
	AST *exprCheck,*exprDo;//ExprNode
	AST *body;//StatementsNode
	inline ForNode(){doFirst=exprCheck=exprDo=body=NULL;}
	inline ~ForNode(){delete doFirst;delete exprCheck;delete exprDo;delete body;}
};
#define STATEMENT_EXPR_IN_FOR 100
#define STATEMENT_VARIABLES_IN_FOR 101
struct StatementInForNode:AST{
	int type;//type: STATEMENT_EXPR_IN_FOR or STATEMENT_VARIABLES_IN_FOR
	AST *child;
	inline StatementInForNode(){type=0;child=NULL;}
	inline ~StatementInForNode(){delete child;}
};
struct ExprCanBeEmptyNode:AST{
	AST *child;//NULL= empty, BinOpNode or UnaryOpNode
	inline ExprCanBeEmptyNode(){child=NULL;}
	inline ~ExprCanBeEmptyNode(){delete child;}
};
struct WhileNode:AST{
	AST *exprCheck;//ExprNode
	AST *body;//StatementsNode
	inline WhileNode(){exprCheck=body=NULL;}
	inline ~WhileNode(){delete exprCheck;delete body;}
};
struct IfNode:AST{
	AST *exprCheck;//ExprNode
	AST *body;//StatementsNode
	inline IfNode(){exprCheck=body=NULL;}
	inline ~IfNode(){delete exprCheck;delete body;}
};
struct CallFunctionNode:AST{
	unsigned long long name;
	std::vector<AST*>parameters;
	inline CallFunctionNode(){name=0;newVector(parameters);}
	inline ~CallFunctionNode(){deleteVector(parameters);}
};
struct BinOpNode:AST{
	int type;//type: is same as Token
	AST *left,*right;
	inline BinOpNode(){type=0;left=right=NULL;}
	inline BinOpNode(AST *_left,int _type,AST *_right){left=_left;type=_type;right=_right;}
	inline ~BinOpNode(){delete left;delete right;}
};
struct UnaryOpNode:AST{
	int type;//type: is same as Token
	AST *expr;
	inline UnaryOpNode(){type=0;expr=NULL;}
	inline UnaryOpNode(int _type,AST *_expr){type=_type;expr=_expr;}
	inline ~UnaryOpNode(){delete expr;}
};
struct NumberNode:AST{
	int val;
	inline NumberNode(int _val){val=_val;}
};
/********* structs define end ***************/

/********* check and build AST ************/
Code code;
Token currentToken;
namespace BuildAST{
inline void eat(int type){
	if(currentToken.type==type) return currentToken=getNextToken(code),void();
	quitSyntaxError(currentToken,type,"");
}
inline AST *program();
inline AST *include();
inline AST *usingNameSpace();
inline AST *functionAndVariable();
inline AST *functionDeclared();
inline AST *variable();
inline AST *variables();
inline AST *statements();
inline AST *statement();
inline AST *exprCanBeEmpty();
inline AST *expr();
inline AST *expr9();
inline AST *expr8();
inline AST *expr7();
inline AST *expr6();
inline AST *expr5();
inline AST *expr4();
inline AST *expr3();
inline AST *expr2();
inline AST *expr1();
inline AST *expr0();
inline AST *callFunction();
inline AST *_for();
inline AST *_while();
inline AST *_if();
inline AST *statementInFor();
inline AST *include(){
	eat(INCLUDE);eat(LESS);
	if(currentToken.type!=NAME) quitSyntaxError(currentToken,NAME,"");
	IncludeNode *res=new IncludeNode(currentToken.val);
	eat(NAME);eat(GREATER);
	return res;
}
inline AST *usingNamespace(){
	eat(USING);eat(NAMESPACE);
	if(currentToken.type!=NAME) quitSyntaxError(currentToken,NAME,"");
	UsingNamespaceNode *res=new UsingNamespaceNode(currentToken.val);
	eat(NAME);eat(SEMI);
	return res;
}
inline AST *functionAndVariable(){
	eat(INT);
	Token nextToken=peekNextToken(code);
	if(nextToken.type==LPAREN) return functionDeclared();
	AST *res=variables();eat(SEMI);
	return res;
}
inline AST *functionDeclared(){
	FunctionDeclaredNode *res=new FunctionDeclaredNode();
	res->name=currentToken.val;
	eat(NAME);eat(LPAREN);
	if(currentToken.type!=RPAREN) eat(INT),res->parameters.push_back(variable());
	while(currentToken.type!=RPAREN) eat(COMMA),eat(INT),res->parameters.push_back(variable());
	eat(RPAREN);eat(LCURLY_BRA);
	res->statements=statements();
	eat(RCURLY_BRA);
	return res;
}
inline AST *variables(){
	VariablesNode *res=new VariablesNode();
	res->variables.push_back(variable());
	while(currentToken.type==COMMA) eat(COMMA),res->variables.push_back(variable());
	return res;
}
inline AST *variable(){
	VariableNode *res=new VariableNode();
	res->type=INT;res->name=currentToken.val;eat(NAME);//only INT
	while(currentToken.type==LSQUARE_BRA){
		eat(LSQUARE_BRA);
		res->size.push_back(expr());
		eat(RSQUARE_BRA);
	}
	return res;
}
inline int checkExpr(){
	int type=currentToken.type;
	return type==PLUS||type==MINUS||type==NOT||type==NUMBER||type==LPAREN||type==NAME;
}
inline AST *statements(){
	StatementsNode *res=new StatementsNode();
	AST *now;
	while(now=statement()) res->statements.push_back(now);
	return res;
}
inline AST *statement(){
	StatementNode *res=new StatementNode();
	if(currentToken.type==LCURLY_BRA){
		eat(LCURLY_BRA);res->type=EXPR;
		res->child=statements();
		eat(RCURLY_BRA);
	}
	else if(currentToken.type==FOR) res->type=FOR,res->child=_for();
	else if(currentToken.type==WHILE) res->type=WHILE,res->child=_while();
	else if(currentToken.type==IF){
		res->type=IF;res->child=_if();
		if(currentToken.type==ELSE) eat(ELSE),res->type=ELSE,res->child2=statements();
	}
	else if(currentToken.type==RETURN){
		res->type=RETURN;eat(RETURN);
		res->child=expr();eat(SEMI);
	}
	else if(currentToken.type==BREAK) res->type=BREAK,eat(BREAK),eat(SEMI);
	else if(currentToken.type==CONTINUE) res->type=CONTINUE,eat(CONTINUE),eat(SEMI);
	else if(currentToken.type==INT){
		res->type=INT;eat(INT);
		res->child=variables();eat(SEMI);
	}
	else if(checkExpr()) res->type=EXPR,res->child=expr(),eat(SEMI);
	else if(currentToken.type==SEMI) res->type=EMPTY,eat(SEMI);//empty statement also should end with a SEMI
	else return NULL;//fail to find a statement
	return res;
}
inline AST *exprCanBeEmpty(){
	ExprCanBeEmptyNode *res=new ExprCanBeEmptyNode();
	if(checkExpr()) res->child=expr();
	return res;
}
inline AST *expr(){
	AST *left=expr9();
	for(int type;;){
		type=currentToken.type;
		if(type==LEFT_SHIFT||type==RIGHT_SHIFT) eat(type),left=new BinOpNode(left,type,expr9());
		else break;
	}
	return left;
}
inline AST *expr9(){//right combination !
	std::vector<AST*>tmp;
	tmp.push_back(expr8());
	for(int type;;){
		type=currentToken.type;
		if(type==ASSIGN) eat(type),tmp.push_back(expr8());
		else break;
	}
	int size=tmp.size();
	if(size==1) return tmp[0];
	BinOpNode *res=new BinOpNode(tmp[size-2],ASSIGN,tmp[size-1]);//size>=2
	for(int i=size-3;~i;i--) res=new BinOpNode(tmp[i],ASSIGN,res);
	return res;
}
inline AST *expr8(){
	AST *left=expr7();
	for(int type;;){
		type=currentToken.type;
		if(type==OR) eat(type),left=new BinOpNode(left,type,expr7());
		else break;
	}
	return left;
}
inline AST *expr7(){
	AST *left=expr6();
	for(int type;;){
		type=currentToken.type;
		if(type==AND) eat(type),left=new BinOpNode(left,type,expr6());
		else break;
	}
	return left;
}
inline AST *expr6(){
	AST *left=expr5();
	for(int type;;){
		type=currentToken.type;
		if(type==XOR) eat(type),left=new BinOpNode(left,type,expr5());
		else break;
	}
	return left;
}
inline AST *expr5(){
	AST *left=expr4();
	for(int type;;){
		type=currentToken.type;
		if(type==EQUAL||type==NOT_EQUAL) eat(type),left=new BinOpNode(left,type,expr4());
		else break;
	}
	return left;
}
inline AST *expr4(){
	AST *left=expr3();
	for(int type;;){
		type=currentToken.type;
		if(type==LESS_OR_EQUAL||type==GREATER_OR_EQUAL||type==LESS||type==GREATER) eat(type),left=new BinOpNode(left,type,expr3());
		else break;
	}
	return left;
}
inline AST *expr3(){
	AST *left=expr2();
	for(int type;;){
		type=currentToken.type;
		if(type==PLUS||type==MINUS) eat(type),left=new BinOpNode(left,type,expr2());
		else break;
	}
	return left;
}
inline AST *expr2(){
	AST *left=expr1();
	for(int type;;){
		type=currentToken.type;
		if(type==MUL||type==DIV||type==MOD) eat(type),left=new BinOpNode(left,type,expr1());
		else break;
	}
	return left;
}
inline AST *expr1(){
	std::vector<int>tmp;
	while(currentToken.type==PLUS||currentToken.type==MINUS||currentToken.type==NOT) tmp.push_back(currentToken.type),eat(currentToken.type);
	AST *expr=expr0();
	if(!tmp.size()) return expr;
	int size=tmp.size();//size>=1;
	UnaryOpNode *res=new UnaryOpNode(tmp[size-1],expr);
	for(int i=size-2;~i;i--) res=new UnaryOpNode(tmp[i],res);
	return res;
}
inline AST *expr0(){
	if(currentToken.type==NUMBER){
		NumberNode *res=new NumberNode(currentToken.val);
		eat(NUMBER);
		return res;
	}
	if(currentToken.type==LPAREN){
		eat(LPAREN);
		AST *res=expr();
		eat(RPAREN);
		return res;
	}
	Token nextToken=peekNextToken(code);
	if(nextToken.type==LPAREN) return callFunction();
	return variable();
}
inline AST *callFunction(){
	unsigned long long name=currentToken.val;
	eat(NAME);eat(LPAREN);
	CallFunctionNode *res=new CallFunctionNode();
	res->name=name;
	if(checkExpr()) res->parameters.push_back(expr());
	while(currentToken.type==COMMA) eat(COMMA),res->parameters.push_back(expr());
	eat(RPAREN);
	return res;
}
inline AST *_for(){
	eat(FOR);eat(LPAREN);
	ForNode *res=new ForNode();
	res->doFirst=statementInFor();eat(SEMI);
	res->exprCheck=exprCanBeEmpty();eat(SEMI);
	res->exprDo=exprCanBeEmpty();eat(RPAREN);
	res->body=statements();
	return res;
}
inline AST *_while(){
	eat(WHILE);eat(LPAREN);
	WhileNode *res=new WhileNode();
	res->exprCheck=expr();eat(RPAREN);
	res->body=statements();
	return res;
}
inline AST *_if(){
	eat(IF);eat(LPAREN);
	IfNode *res=new IfNode();
	res->exprCheck=expr();eat(RPAREN);
	res->body=statements();
	return res;
}
inline AST *statementInFor(){
	StatementInForNode *res=new StatementInForNode();
	if(currentToken.type==INT) eat(INT),res->type=STATEMENT_VARIABLES_IN_FOR,res->child=variables();
	else res->type=STATEMENT_EXPR_IN_FOR,res->child=exprCanBeEmpty();
	return res;
}
inline AST *program(){
	ProgramNode *res=new ProgramNode();
	while(currentToken.type==INCLUDE) res->children.push_back(include());
	while(currentToken.type==USING) res->children.push_back(usingNamespace());
	while(currentToken.type!=_EOF) res->children.push_back(functionAndVariable());
	return res;
}
}//namesapce BuildAST
/********* check and build AST end ************/

/************************** semantic analysis *******************************/
AST *ASTRoot;
/********** define ************/
inline void quitSemanticError(const char *info=""){
	printf("semantic error, %s\n",info);
	exit(1);
}
struct Symbol{
	unsigned long long name;
	int type;
	inline Symbol(){}
	inline Symbol(unsigned long long _name,int _type=0){name=_name;type=_type;}
};
struct BuiltinTypeSymbol:Symbol{
	inline BuiltinTypeSymbol(unsigned long long _name,int _type=0):Symbol(_name,_type){}
};
struct VariableSymbol:Symbol{
	inline VariableSymbol(unsigned long long _name,int _type):Symbol(_name,_type){}
};
struct ScopeSymbolTable{
	int level;
	std::unordered_map<unsigned long long,Symbol>table;
	ScopeSymbolTable *enclosingScope;
	inline int insert(const Symbol &symbol){
		if(table.count(symbol.name)) quitSemanticError("variable or function redeclaration");
		table[symbol.name]=symbol;
		return 1;
	}
	inline Symbol find(const Symbol &symbol){
		if(table.count(symbol.name)) return table[symbol.name];
		if(!enclosingScope) return Symbol(0,0);
		return enclosingScope->find(symbol);
	}
	inline ScopeSymbolTable(ScopeSymbolTable *encS,int lv=0){
		enclosingScope=encS;level=lv;
		if(!lv) level=encS->level+1;
		std::unordered_map<unsigned long long,Symbol>().swap(table);
		insert(BuiltinTypeSymbol(INT));
		insert(BuiltinTypeSymbol(IOSTREAM));
		insert(BuiltinTypeSymbol(CHAR));
		insert(VariableSymbol(getName("cin"),IOSTREAM));
		insert(VariableSymbol(getName("cout"),IOSTREAM));
		insert(VariableSymbol(getName("endl"),CHAR));
	}
};
/******** define end ***********/

/******** go on AST ************/
/*
check:
	if variables were declared before use
	if there are variables redefined
    if the left son of >> will return cin
    if the left son of << will return cout
    if functions have the correct parameters
*/
namespace AnalysisAST{
void program(AST *t){
	ScopeSymbolTable *currentSymbolTable=new ScopeSymbolTable(NULL,1);
	for(AST *child:t->children){
		if(typeid(child)==typeid(FunctionDeclaredNode)) functionDeclared(child,currentSymbolTable);
		else if(typeid(child)==typeid(VariablesNode)) variables(child,currentSymbolTable);
	}
	if(currentSymbolTable->find(VariableSymbol(getName("main"),INT))!=VariableSymbol(getName("main"),INT))
		quitAnalysisError("main function not found or the type of main function is not int");
}
void variables(AST *t,ScopeSymbolTable *currentSymbolTable){
	for(AST *var:t->variables) variable(var);
}
void variable(AST *t,ScopeSymbolTable *currentSymbolTable){//it is for variable declared !
		assert(typeid(t)==typeid(VariableNode));
	currentSymbolTable->insert(VariableSymbol(t->name,t->type));
}
void functionDeclared(AST *t,ScopeSymbolTable *currentSymbolTable){
	currentSymbolTable->insert(VariableSymbol(t->name,INT));
	currentSymbolTable=new ScopeSymbolTable(currentSymbolTable);
	for(AST *parameter:t->parameters) variable(parameter,currentSymbolTable);
	statements(t->statemets,currentSymbolTable);
}
void statements(AST *t,ScopeSymbolTable *currentSymbolTable){
	for(AST *stm:t->statements) statement(stm,currentSymbolTable);
}
void statement(AST *t,ScopeSymbolTable *currentSymbolTable){
/*                                                                                            type
	statement: LCURLY_BRA statements RCURLY_BRA                                               LCURLY_BRA
    | for LPAREN statementInFor SEMI exprCanBeEmpty SEMI exprCanBeEmpty RPAREN statements     FOR
    | while LPAREN expr RPAREN statements                                                     WHILE
    | if LPAREN expr RPAREN statements                                                        IF
    | if LPAREN expr RPAREN statements else statements                                        ELSE
    | return expr SEMI                                                                        RETURN
    | break SEMI                                                                              BREAK
    | continue SEMI                                                                           CONTINUE
    | expr SEMI                                                                               EXPR
    | INT variable (COMMA variable)* SEMI                                                     INT
    | EMPTY                                                                                   EMPTY
*/
		assert(typeid(t)==typeid(statement));
	if(t->type==LCURLY_BRA){
		currentSymbolTable=new ScopeSymbolTable(currentSymbolTable);
		statements(t->statements,currentSymbolTable);
		delete currentSymbolTable;
	}
	else if(t->type==FOR){
		currentSymbolTable=new ScopeSymbolTable(currentSymbolTable);
		_for(t->child,currentSymbolTable);
		delete currentSymbolTable;
	}
	else if(t->type==WHILE){
		currentSymbolTable=new ScopeSymbolTable(currentSymbolTable);
		_while(t->child,currentSymbolTable);
		delete currentSymbolTable;
	}
	else if(t->type==IF){
		currentSymbolTable=new ScopeSymbolTable(currentSymbolTable);
		_if(t->child,currentSymbolTable);
		delete currentSymbolTable;
	}
	else if(t->type==ELSE){
		ScopeSymbolTable *nextSymbolTable=new ScopeSymbolTable(currentSymbolTable);
		_if(t->child,nextSymbolTable);
		delete nextSymbolTable;
		currentSymbolTable=new ScopeSymbolTable(currentSymbolTable);
		statements(t->child2,currentSymbolTable);
		delete currentSymbolTable;
	}
	else if(t->type==EXPR) binOrUnaryOpOrExpr0(t->child,currentSymbolTable)
	else if(t->type==INT) variables(currentSymbolTable);
}
#define LEFT_VAL        100000
#define RIGHT_VAL       100001
#define STREAM          100002
#define IS_ENDL         100003
int binOrUnaryOpOrExpr0(AST *t,ScopeSymbolTable *currentSymbolTable){//return the type of the result
	if(typeid(t->child)==typeid(BinOpNode)) return binOp(t->child,currentSymbolTable);
	if(typeid(t->child)==typeid(UnaryOpNode)) return unaryOp(t->child,currentSymbolTable);
	if(typeid(t->child)==typeid(NumberNode)) return
		assert(0);
}
int unaryOp(AST *t,ScopeSymbolTable *currentSymbolTable){
	int type=binOrUnaryOpOrExpr0(t->child,currentSymbolTable);
	if(type)
	return type;
}
int binOp(AST *t,ScopeSymbolTable *currentSymbolTable){
	if(t->type==ASSIGN){
		int leftType=binOrUnaryOpOrExpr0(t->left,currentSymbolTable);
		int rightType=binOrUnaryOpOrExpr0(t->right,currentSymbolTable)
		if(leftType!=LEFT_VAL&&leftType!=STREAM) quitAnalysisError("type on the left of ASSIGN must be left val");
		if(rightType==IS_ENDL) quitAnalysisError("endl can only be used in cout");
		if(rightType==LEFT_VAL) rightType=LEFT_VAL;
		if(leftType!=rightType) quitAnalysisError("type between ASSIGN must be same");
		return rightType;
	}
	if()
	else{
		int leftType=binOrUnaryOpOrExpr0(t->left,currentSymbolTable);
		int rightType=binOrUnaryOpOrExpr0(t->right,currentSymbolTable)
		if(leftType==IS_END||rightType==IS_END||leftType==STREAM||rightType==STREAM) quitAnalysisError("fuck you!");
	}
}
}//namespace AnalysisAST
/******** end ******************/

/************************** interpreter ************************************/


/*************************** work *****************************************/
inline void analysis(const Code &_code){
	code=_code;
	currentToken=getNextToken(code);
	ASTRoot=BuildAST::program();
	AnalysisAST::analysis(ASTRoot);
}
inline void run(int *input){

}

/********************************* end **********************************************/
}//namespace Interpreter
int input[1048576];
Code code;
int main(){
		printf("%lld\n",sizeof(Interpreter::ProgramNode));
		freopen("test2.cpp","r",stdin);
//	scanf("%d",input);
//	for(int i=1;i<=input[0];i++) scanf("%d",input+i);
	code.read();
	Interpreter::analysis(code);
	Interpreter::run(input);
	return 0;
}
原文