?? parser.h
字號:
#ifndef MY_PARSER_H_
#define MY_PARSER_H_
/*************************************************
Copyright (C), 2002-2008, lonelyforest.
File name: parser.h
Author: 林公玉 (lonelyforest)
Version: 1.0
Date: 22-03-06 15:47
Description: 其中主要有 樹節點類(TreeNode), 語法樹類(Parser),
主要將從詞法分析器的來的 每個標識符 建造成一棵語
法樹,以便于后端語義分析,符號表生成等等。
*************************************************/
#include "scanner.h"
typedef enum { varK, funK, paramK, stmtK, expK } NodeKind;
typedef enum {
ifK, whileK, readK, writeK, returnK, callK } StmtKind;
typedef enum { OpK, ConstK, IDK } ExpKind;
// parser-tree max childrens
const int MAX_CHILDREN = 3;
// parser tree node
struct TreeNode
{
//------------------------------------------------------------------------
TreeNode();
~TreeNode();
TreeNode* LastSibling();
//------------------------------------------------------------------------
TreeNode* child[ MAX_CHILDREN]; // pointer to child node
TreeNode* father; // pointer to father node
TreeNode* sibling; // pointer to sibling node
//------------------------------------------------------------------------
NodeKind nodekind;
union {
StmtKind stmt;
ExpKind exp;
} kind;
enum tokenType type;
string name;
string scope; // node function scpo
bool bArr; // array ?
int lineno;
int iArrSize; // arrary size;
};
/**: class Scanner
※
* 構建語法樹,進行語法檢查,提示錯誤生成語法樹文件;
*
* 重要接口 BuildSyntaxTree() & getTreeFile()
* 分別將一個個的 Token 標識符構建為語法樹 &生成語法樹文件
&
* 將詞法分析器作為指針成員,動態生成, 這樣增加了操作的靈活性;
*
* author: lonelyforest;
* data: 2006.03.22
*/
// clas Parser, primary class and interface;
class Parser
{
public:
Parser(const string& filename);
~Parser();
TreeNode* BuildSyntaxTree();
//------------------------------------------------------------------------
void getTreeFile();
void getListFile(){ scan->getListFile(); }
//------------------------------------------------------------------------
void noListFile() { scan->noListFile();}
void noTreeFile() { traceParse = false; }
//------------------------------------------------------------------------
void add_err() { scan->add_err(); }
void add_warn() { scan->add_warn();}
int errCount()const { return scan->errCount();}
int warnCount()const{ return scan->warnCount(); }
bool is_good() const { return scan->is_good(); }
//------------------------------------------------------------------------
protected:
TreeNode* m_program;
TreeNode* newNode(const NodeKind Kind, const tokenType Type, const string& Name);
TreeNode* newStmtNode(const StmtKind Kind, const string& Name);
TreeNode* newExpNode(const ExpKind Kind, const tokenType Type, const string& Name);
//------------------------------------------------------------------------
bool match(tokenType Type);
/* 錯誤恢復,多謝一個網友后來給了個參考,讓我改寫錯誤恢復,
* 我發現這個方法似乎不是很好,但也不錯。
*/
void consumeUntil(const tokenType Type);
void consumeUntil(const tokenType type1, const tokenType type2);
//------------------------------------------------------------------------
protected:
//------------------------------------------------------------------------
// Gramer functions
TreeNode* program();
TreeNode* declaration_list();
TreeNode* declaration();
TreeNode* var_declaration();
TreeNode* fun_declaration();
TreeNode* params(); // ...
TreeNode* compound_stmt();
TreeNode* local_declarations();
TreeNode* read_stmt(); // read(int)
TreeNode* write_stmt(); // write(int);
TreeNode* expression_stmt();
TreeNode* subcompound_stmt(); // ...
TreeNode* if_stmt();
TreeNode* while_stmt();
TreeNode* return_stmt();
TreeNode* expression();
TreeNode* simple_expression();
TreeNode* additive_expression();
TreeNode* term();
TreeNode* factor();
TreeNode* var();
TreeNode* call();
TreeNode* args();
//------------------------------------------------------------------------
void PrintTree(TreeNode* pNode); // print the syntax tree to treeFile
private:
//------------------------------------------------------------------------
Scanner* scan; // use pointer, better than member
Token cToken, tToken, iToken; // current Token, typeToken, idnameToken
string Scope; // current scope
string treeFile; // print syntax tree, use extern name .st(syntax tree)
ofstream out; // open and write tree file
int indent;
//------------------------------------------------------------------------
bool traceParse;
};
#endif // MY_PARSER_H_
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -