📄 globals.h
字号:
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
extern int lineno = 0;
//int Error = 0;
typedef enum
{
ENDFILE,ERROR,
IF,THEN,ELSE,END,REPEAT,UNTIL,READ,WRITE,
ID,NUM,
ASSIGN,EQ,LT,PLUS,MINUS,TIMES,OVER,LPAREN,RPAREN,SEMI
} TokenType;
void printToken( TokenType token, const char* tokenString )
{ switch (token)
{ case IF:
case THEN:
case ELSE:
case END:
case REPEAT:
case UNTIL:
case READ:
case WRITE:
cout << "reserved word:" << tokenString << endl;
break;
case ASSIGN: cout << ":=" << endl;break;
case LT: cout << "<" << endl; break;
case EQ: cout << "=" << endl; break;
case LPAREN: cout << "(" << endl; break;
case RPAREN: cout << ")" << endl; break;
case SEMI: cout << ";" << endl; break;
case PLUS: cout << "+" << endl; break;
case MINUS:cout << "-" << endl; break;
case TIMES: cout << "*" << endl; break;
case OVER: cout << "/" << endl; break;
case ENDFILE: cout << "EOF" << endl; break;
case NUM:
cout << "NUM, val= " << tokenString << endl;
break;
case ID:
cout << "ID, name= " << tokenString << endl;
break;
case ERROR:
cout << "ERROR: " << tokenString << endl;
break;
default:
cout << "Unknown token: "<< token << endl;
}
}
typedef enum {StmtK,ExpK} NodeKind;
typedef enum {IfK,RepeatK,AssignK,ReadK,WriteK} StmtKind;
typedef enum {OpK,ConstK,IdK} ExpKind;
typedef enum {Void,Integer,Boolean} ExpType;
static int indentno = 0;
#define INDENT indentno+=2
#define UNINDENT indentno-=2
static void printSpaces(void)
{ int i;
for (i=0;i<indentno;i++)
cout<<" ";
cout<<"---- ";
}
char* CopyString(char* s)
{
char *t;
if(s==NULL) return NULL;
t = new char[strlen(s)+1];
if(t==NULL)
cout<<"Out of memory error at line "<<lineno<<endl;
else
strcpy(t,s);
return t;
}
class Parse;
class TreeNode
{
public:
TreeNode* child[3];
TreeNode* sibling;
int linenos;
NodeKind nodekind;
union { StmtKind stmt; ExpKind exp;} kind;
union { TokenType op;
int val;
char * name; } attr;
ExpType type;
TreeNode* NewStmtNode(StmtKind);
TreeNode * NewExpNode(ExpKind);
void printTree( TreeNode * );
};
TreeNode* TreeNode::NewStmtNode(StmtKind kind)
{ TreeNode * t = (TreeNode *) malloc(sizeof(TreeNode));
int i;
if (t==NULL)
cout<<"Out of memory error at line "<<lineno<<endl;
else {
for (i=0;i<3;i++) t->child[i] = NULL;
t->sibling = NULL;
t->nodekind = StmtK;
t->kind.stmt = kind;
t->linenos = lineno;
}
return t;
}
TreeNode* TreeNode::NewExpNode(ExpKind kind)
{ TreeNode * t = (TreeNode *) malloc(sizeof(TreeNode));
int i;
if (t==NULL)
cout<<"Out of memory error at line "<<lineno<<endl;
else {
for (i=0;i<3;i++) t->child[i] = NULL;
t->sibling = NULL;
t->nodekind = ExpK;
t->kind.exp = kind;
t->linenos = lineno;
t->type = Void;
}
return t;
}
void TreeNode::printTree(TreeNode* tree)
{ int i;
INDENT;
while (tree != NULL) {
printSpaces();
if (tree->nodekind==StmtK)
{ switch (tree->kind.stmt) {
case IfK:
cout<<"If\n";
break;
case RepeatK:
cout<<"Repeat\n";
break;
case AssignK:
cout<<"Assign to: "<<tree->attr.name<<endl;
break;
case ReadK:
cout<<"Read: "<<tree->attr.name<<endl;
break;
case WriteK:
cout<<"Write\n";
break;
default:
cout<<"Unknown ExpNode kind\n";
break;
}
}
else if (tree->nodekind==ExpK)
{ switch (tree->kind.exp) {
case OpK:
cout<<"Op: ";
printToken(tree->attr.op,"\0");
break;
case ConstK:
cout<<"Const: "<<tree->attr.val<<endl;
break;
case IdK:
cout<<"Id: "<<tree->attr.name<<endl;
break;
default:
cout<<"Unkown ExpNode kind\n";
break;
}
}
else cout<<"Unkown node kind\n";
for (i=0;i<3;i++)
printTree(tree->child[i]);
tree = tree->sibling;
}
UNINDENT;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -