?? parse.h
字號:
/*
* file: parse.h
*/
#if _MSC_VER >1200 /* msvc6 or later */
#pragma once
#endif
#ifndef __PARSE_H_
#define __PARSE_H_
#include <string>
#include <vector>
#include <set>
#include <fstream>
#include <iostream>
#include <sstream>
#include <exception>
#include <utility>
#include <cassert>
#include "types.h"
#include "lex.h"
#include "cmd.h"
using std::wcout;
using std::cout;
using std::endl;
using std::ostringstream;
using std::ostream;
using std::istream;
using std::string;
using std::exception;
using std::vector;
using std::set;
using std::ofstream;
class Parse
{
protected:
typedef vector<vector<sint32> >
biarray;
typedef vector< sint32 >
array;
public: // data
/* 終結符結合率值 (0表示沒有設置結合率)*/
enum Assoc{NON_ALLOC=1,L_ALLOC,R_ALLOC};
vector<string> Vns; //非終結符字符串集
/* Vts[0]存放文件結束標志'#' */
vector<string> Vts; //終結符字符串集
sint32 nVns; //非終結符數
sint32 nVts; //終結符數
sint32 S; // Start Symbol
biarray P; //每一維數組的第0下標為產生式左部
// P2 中存放產生式的編號,Vn2及Vt2中存放非終結符及終結符編號.
// set<sint32> Vt2,Vn2,P2;
vector<Bool > Vn2,Vt2,P2;
uint32 nVns2,nVts2,nSymbols2,nRules2;
////////////////////////////////////////////////
sint32 lookahead; // 向前查看符號
sint32 nSymbols; // 全部符號數
sint32 nRules; // 規則數
vector< sint32 >
rPrec,tPrec; // 規則及終結符的優先級(值為0表示未指定)
vector< sint32 >
rAssoc,tAssoc; // 規則及終結符的結合性(值為0表示未指定)
vector< sint32 >
tNumVal; // 聲明token時指定的值. e.g. %token ID 222
// 值為 -1 表示未指定.
vector< sint32 > rline; // 規則所在行號.
vector< sint32 > actline; //
// 存放 terminal and nonterminal的union域值索引,如果值為 -1 表示
// 不存在 union 值域.
vector< sint32 > tUnion; // symbol's union value.
vector< sint32 > nontUnion;
vector< string > sUnion; // 存放 union 值域字符串值.
biarray first_set;
biarray follow_set;
Lex & lex;
Cmd & cmd;
public: // function
Parse(Lex &L,Cmd &C);
Void start_parse();
//求序列begin~end的first集
vector<sint32> get_First(const array::iterator &begin,
const array::iterator &end);
//求序列v的first集.
inline
vector<sint32> get_First(vector<sint32> &v);
//求文法中token的first集.
vector<sint32> get_First(const sint32 token);
//求文法中token的follow集.
inline
vector<sint32> get_Follow(sint32 token);
void print_First(); //打印first集
void print_Follow(); //打印follow集
void print_Rulers(); //打印產生式
void print_Terminals(); //打印終結符
void print_Nonterminals();//打印非終結符
Void
Parse::show_Rule(const sint32 n,ostream &out);
inline Void
Parse::show_Symbol(const sint32 sym , ostream &out);
/***********************************/
Void clear()
{//有必要時刪除主要的數組以釋放空間
P.clear();
Vns.clear();
Vts.clear();
}
Void fatal();
private: // function
Void crt_First(); //Create First Set.
Void crt_Follow(); //Create Follow Set.
Void reduce(); //文法化簡.
//// 語法分析器掃描器(遞歸下降法)
Void spec();
Void defines();
Void rules();
Void tail();
Void nlist(const sint32 , sint32 cur_assoc=0 ,sint32 cur_prec=0);
Void nlist2(const sint32);
Void rbodys(const sint32);
sint32 rbody(vector<sint32>&rule,Bool &IsHaveAct);
sint32 prec(const vector<sint32>&rule, Bool &IsHaveAct);
Void eat_up_rest();
Void eat_up_union();
Void eat_up_defs();
Void eat_act(const vector<sint32> &cur_rule);
sint32
symtype();
Bool match(sint32 token);
inline sint32
insert_Vts(const string &val, const sint32 cur_assoc,
const sint32 cur_prec,const sint32 unionval,
const sint32 number);
inline sint32
insert_Vns(const string &val, const sint32 unionval);
inline sint32
insert_ruler(const vector<sint32> & rule,const sint32 cur_prec,
const sint32 cur_assoc,const sint32 lineno);
inline sint32
isinVts(const string&);
inline virtual sint32
isinVns(const string&);
sint32 r_check(sint32 r); // 檢查文法規則 r 是否與別的文法產生重復
//////////////////////////////
void showErr();
public://private: // data
ofstream of_cpp; // output file of source file.
ofstream of_h; // output file of header file.
ofstream foutput; //
ofstream fdefine; // C語言定義部分.
ofstream faction; // 動作存放文件.
uint32 nerror;
ostringstream err_msg;
string union_define;
protected:
const sint32 STARTNONT; // 非終結符的起始位置.
const sint32 Null ;//= -100;
const sint32 SHARP;//=0; // '#'
sint32 size(const vector<Bool> &vec);
};
#endif /* __PARSE_H_ */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -