;
classGrammer;
classLex {
private:
classLexical {
public:
stringLexname;
//类型名称intLexcode;
//类别编号publicLexical(
:
stringLexname,
intLexcode): Lexname(Lexname), Lexcode(Lexcode) {}
voidtoString() {
if(Lexcode >= OPERATOR_LIMIT_BASE)
cout<<
"界限符";
elseif(Lexcode >= OPERATOR_BASE)
cout<<
"运算符";
elseif(Lexcode >= CONSTANT_BASE)
cout<<
"常量";
elseif(Lexcode >= KEYWORD_BASE)
cout<<
"保留字";
elsecout<<
"标识符";
cout<<
" : ("<< Lexname <<
" , "<< Lexcode <<
")"}
<< endl;
stringgetLexTypename() {
if(Lexcode >= OPERATOR_LIMIT_BASE)
returnLexname;
elseif(Lexcode >= OPERATOR_BASE)
returnLexname;
elseif(Lexcode >= CONSTANT_BASE +
1)
return"csdigits";
elseif(Lexcode >= CONSTANT_BASE)
return"csstring";
elseif(Lexcode >= KEYWORD_BASE)
returnLexname;
elsereturn"id"};
}
;
friendclassGrammer;
typedefbool(Lex::*JudgeFunc)(
int,
bool);
conststaticintKEYWORD_BASE =
1000;
//关键字基址conststaticintKEYWORD_LEN =
21;
//关键字种类个数conststaticintCONSTANT_BASE =
10000;
//常量基址conststaticintCONSTANT_LEN =
3;
//常量种类个数conststaticintOPERATOR_BASE =
100000;
//符号基址conststaticintOPERATOR_LEN =
22;
//符号种类个数conststaticintOPERATOR_LIMIT_BASE =
1000000;
//界限符基址conststaticintOPERATOR_LIMIT_LEN =
11;
//界限符种类个数conststaticintVARIABLE_BASE =
0;
//变量基址conststaticstringKeywordArr[KEYWORD_LEN];
//保留字即关键字数组conststaticstringOperatorArr[OPERATOR_LEN];
//运算符数组conststaticstringOperatorLimitArr[OPERATOR_LIMIT_LEN];
//界限符数组conststaticstringConstantArr[CONSTANT_LEN];
//常量数组JudgeFunc ConstantFuncArr[CONSTANT_LEN];
//常量处理数组map<string, int>KeyValueMap;
//种类编码映射表vectorLt;
//词法结果数组stringcurDealStr;
//当前格式化后的字符串intcurPositionIndex;
//当前处理常量的常量类型是整型还是浮点还是字符串intcurPositionLen;
publicLex();
:
inlinevoidLexInit();
//初始化数据inlinevoidSystemInit();
inlinevoidTagInit();
inlinevoidAnnoInit();
inlinevoidKeywordInit();
inlinevoidConstantInit();
inlinevoidOperatorInit();
inlinevoidOperatorLimitInit();
voidScanner(
stringinStr);
//分析字符串的构造出词法表stringStrCleanUp(
stringinStr);
//去掉注释【此函数的代码随便写的不一定正确,所以请注意,如果发现代码问题,可以自己完成函数的功能】boolJudgeVar(
int,
bool);
//判断是否是标识符boolJudgeInte(
int,
bool);
//判断是否为整数常量boolJudgeDec(
int,
bool);
//判断是否为浮点常量boolJudgeStr(
int,
bool);
//判断是否为字符串常量boolJudgeSym(
int,
bool);
//判断是否为运算符boolJudgeSymLimit(
int,
bool);
//判断是否为界限符voidCycleDeal(
int&, JudgeFunc,
string);
//根据情况调用上述的几个判断函数inlinevoidprint();
//输出词法分析的结果inlinepair<
string,
int> out();
//啥都没用};
conststringLex::KeywordArr[KEYWORD_LEN] = {
"var",
"for",
"return",
"break",
"continue",
"function",
"const",
"let",
"class",
"extends",
"if",
"else",
"else if",
"while",
"new",
"import",
"from",
"in",
"of",
"null",
"undefined"};
conststringLex::OperatorArr[OPERATOR_LEN] = {
"++",
"--",
"+=",
"-=",
"^=",
"&=",
"+",
"-",
"*",
"/",
"=",
"&",
"^",
">",
"<",
">=",
"<=",
"==",
"~",
"%",
"||",
"&&"};
conststringLex::OperatorLimitArr[OPERATOR_LIMIT_LEN] = {
"(",
")",
"[",
"]",
"{",
"}",
",",
";",
".",
":",
"#"};
conststringLex::ConstantArr[CONSTANT_LEN] = {
"constantStr",
"constantInte",
"constantDec"ConstantFuncArr[
Lex::Lex() {
};
0ConstantFuncArr[
] = &Lex::JudgeStr;
1ConstantFuncArr[
] = &Lex::JudgeInte;
2}
] = &Lex::JudgeDec;
inlinevoid}
OperatorLimitInit();
OperatorInit();
ConstantInit();
KeywordInit();
AnnoInit();
TagInit();
SystemInit();
Lex::LexInit() {
inlinevoid}
KeyValueMap.clear();
Lex::SystemInit() {
inlinevoidKeyValueMap[
Lex::TagInit() {
"Tag"}
] = VARIABLE_BASE;
//此方法功能已经被抛弃inlinevoidKeyValueMap[
Lex::AnnoInit() {
"anno"] =
1}
;
inlinevoidLex::KeywordInit() {
for(
inti =
0}
}
KeyValueMap[KeywordArr[i]] = KEYWORD_BASE + i;
; i < KEYWORD_LEN; i ++) {
inlinevoidLex::ConstantInit() {
for(
inti =
0}
}
KeyValueMap[ConstantArr[i]] = CONSTANT_BASE + i;
; i < CONSTANT_LEN; i ++) {
inlinevoidLex::OperatorInit() {
for(
inti =
0}
}
KeyValueMap[OperatorArr[i]] = OPERATOR_BASE + i;
; i < OPERATOR_LEN; i ++) {
inlinevoidLex::OperatorLimitInit() {
for(
inti =
0}
}
KeyValueMap[OperatorLimitArr[i]] = OPERATOR_LIMIT_BASE + i;
; i < OPERATOR_LIMIT_LEN; i ++) {
stringLex::StrCleanUp(
stringinStr) {
intdelLen =
0;
boolisdel =
false;
for(
inti =
0; i < inStr.length(); i ++) {
if(i +
1< inStr.length() && inStr[i] ==
'/'&& inStr[i +
1] ==
'/'&& !isdel) {
intlast = inStr.find(
"\n", i);
if(last == -
1i = -
inStr.erase(i, last - i);
) last = inStr.length();
1;
continue}
;
elseif(i +
1< inStr.length() && inStr[i] ==
'/'&& inStr[i +
1] ==
'*') isdel =
true, delLen =
0;
elseif(i +
1< inStr.length() && inStr[i +
1] ==
'/'&& inStr[i] ==
'*'isdel =
) {
falsedelLen +=
;
2inStr.erase(i - delLen +
;
2i = -
, delLen);
1}
;
if}
(isdel) delLen ++;
for(
inti =
0; i < inStr.length(); i ++) {
if(inStr[i] ==
'\t'|| inStr[i] ==
'\n'inStr[i] =
) {
' '}
}
;
return}
inStr;
boolLex::JudgeVar(
intl,
boolflag) {
charch = curDealStr[l];
if(ch >=
'a'&& ch <=
'z'|| ch ==
'_'|| ch >=
'A'&& ch <=
'Z'|| flag && ch >=
'0'&& ch <=
'9')
returntrue;
returnfalse}
;
voidLex::CycleDeal(
int&p, JudgeFunc func,
stringDstr) {
intl = p;
stringtmp =
"";
while(l < curDealStr.length() && (
this->*func)(l,
truep = l -
}
l ++;
tmp += curDealStr[l];
)) {
1;
for(
inti =
0; i < KEYWORD_LEN; i ++) {
if}
Lt.push_back(Lexical(tmp, KeyValueMap[Dstr]));
}
}
Dstr = tmp;
(tmp == KeywordArr[i]) {
boolLex::JudgeInte(
intl,
boolflag) {
boolfg = curDealStr[l] >=
'0'&& curDealStr[l] <=
'9';
if(!flag && !fg)
returnfalse;
if(flag)
returnfg;
for(
inti = l; i < curDealStr.length(); i ++) {
if(curDealStr[i] >=
'0'&& curDealStr[i] <=
'9')
continue;
elseif(curDealStr[i] ==
'.')
returnfalse;
elsebreakcurPositionIndex =
}
;
1;
returntrue}
;
boolLex::JudgeDec(
intl,
boolflag) {
boolfg = curDealStr[l] >=
'0'&& curDealStr[l] <=
'9'|| curDealStr[l] ==
'.';
if(!flag && !fg)
returnfalse;
if(flag)
returnfg;
boolkh =
false;
for(
inti = l; i < curDealStr.length(); i ++) {
if(i +
1< curDealStr.length() && curDealStr[i +
1] ==
'.'&& curDealStr[i] >=
'0'&& curDealStr[i] <=
'9'|| i +
1< curDealStr.length() && curDealStr[i] ==
'.'&& curDealStr[i +
1] >=
'0'&& curDealStr[i +
1] <=
'9'kh =
) {
true;
break}
;
elseif(curDealStr[i] >=
'0'&& curDealStr[i] <=
'9')
continue;
elsebreak}
;
if(kh) curPositionIndex =
2;
return}
kh;
boolLex::JudgeStr(
intl,
boolflag) {
staticintcurStrLDoub_Sig = -
1;
staticintcurDoubSig = -
1;
if(flag) {
if(curDealStr[l -
1curDoubSig =
] == curStrLDoub_Sig && !curDoubSig) {
1;
returntrue}
;
elseif(curDealStr[l -
1curDoubSig =
] == curStrLDoub_Sig && curDoubSig) {
0;
returnfalse}
;
elsereturntrue}
;
if(curDealStr[l] ==
'\"') curStrLDoub_Sig =
'\"', curDoubSig =
0;
elseif(curDealStr[l] ==
'\'') curStrLDoub_Sig =
'\'', curDoubSig =
0;
else{
returnfalsecurPositionIndex =
}
;
0;
returntrue}
;
boolLex::JudgeSym(
intl,
boolflag) {
if(flag) {
returncurPositionLen -- >
0}
;
for(
inti =
0; i < OPERATOR_LEN; i ++) {
stringtmp = OperatorArr[i];
boolfg =
false;
for(
intj =
0; j < tmp.length(); j ++) {
iffg =
(j + l >= curDealStr.length()) {
true;
break}
;
elseiffg =
(curDealStr[j + l] != tmp[j]) {
true;
break}
}
;
ifcurPositionLen = tmp.length();
curPositionIndex = i;
(!fg) {
returntrue}
}
;
returnfalse}
;
boolLex::JudgeSymLimit(
intl,
boolflag) {
if(flag) {
returncurPositionLen -- >
0}
;
for(
inti =
0; i < OPERATOR_LIMIT_LEN; i ++) {
stringtmp = OperatorLimitArr[i];
boolfg =
false;
for(
intj =
0; j < tmp.length(); j ++) {
iffg =
(j + l >= curDealStr.length()) {
true;
break}
;
elseiffg =
(curDealStr[j + l] != tmp[j]) {
true;
break}
}
;
ifcurPositionLen = tmp.length();
curPositionIndex = i;
(!fg) {
returntrue}
}
;
returnfalse}
;
voidLex::Scanner(
stringcurDealStr = inStr;
inStr = StrCleanUp(inStr);
inStr) {
for(
intp =
0; p < curDealStr.length(); p ++) {
//fuck(p);chartmpc = curDealStr[p];
if(tmpc ==
' ')
continue;
intc_type = -
1;
if(JudgeVar(p,
false)) {
//标识符}
c_type = VARIABLE_BASE;
elseif(JudgeDec(p,
false) || JudgeStr(p,
false) || JudgeInte(p,
false)) {
//常量}
c_type = CONSTANT_BASE;
elseif(JudgeSym(p,
false)) {
//符号}
c_type = OPERATOR_BASE;
elseif(JudgeSymLimit(p,
false}
c_type = OPERATOR_LIMIT_BASE;
)) {
switch(c_type) {
caseCycleDeal(p, &Lex::JudgeVar,
VARIABLE_BASE:
"Tag");
break;
caseCycleDeal(p, ConstantFuncArr[curPositionIndex], ConstantArr[curPositionIndex]);
CONSTANT_BASE:
break;
caseCycleDeal(p, &Lex::JudgeSym, OperatorArr[curPositionIndex]);
OPERATOR_BASE:
break;
caseCycleDeal(p, &Lex::JudgeSymLimit, OperatorLimitArr[curPositionIndex]);
OPERATOR_LIMIT_BASE:
break;
default:
break}
}
}
;
inlinevoidLex::print() {
for(
inti =
0}
}
Lt[i].toString();
; i < Lt.size(); i ++) {
inlinepair<
string,
int> out() {
returnmake_pair(
"",
1}
);
classGrammer {
privateLex CLex;
:
map<string, vector<string> >Grav;
//最开始的语法集map<string, set<string> >Firstv;
//First集map<string, bool>FirstvBool;
//用于判断First集是否已经构造完成// map<string, bool> FollowvBool;boolFollowvNotAdd;
//用于判断Follow集是否构造完成boolFirstvNotAdd;
map<string, map<string, string> >anasiM;
//预测分析表Mset<string>terminator;
//终结符set<string>notTerminator;
//非终结符vector<string>_stack;
//栈符map<string, set<string> >Followv;
//Follow集publicGrammer();
:
inlinevoidinitLexProcessor(
string);
//初始化词法分析器inlinevoidinitGrammerProcessor(
string);
//初始化语法分析器voidanaisGrammer(
string);
//分析语法voidanaisFirst();
//分析First集voiddfsFirst(
string,
bool&);
//递归处理First集voidanaisFollow();
//分析Follow集voiddfsFollow(
string);
//处理Follow集,求Follow集的时候没有递归处理voidanaisAnaisM();
//分析预测分析表MvoidanaisProcesser();
//分析程序voidcreateInputQueue();
//这个函数暂时没有使用voidinsertFirstv(
string,
string);
voidinsertFirstv(
string,
set<string>::iterator,
set<string>::iterator);
voidinsertFollowv(
string,
string);
voidinsertFollowv(
string,
set<string>::iterator,
set<string>::iterator);
voidinsertGrav(
string,
string);
inlinevoidprint();
voidtrim(
string&);
//去掉字符串开头和结尾的空格voidtestprintGrav();
voidtestprintFirstv();
voidtestprintFollowv();
voidtestprintTerminator();
voidtestprintNotTerminator();
voidtestprintAnaisM();
voidtestprint_stack();
voidtestprint_Lt(
int}
Grammer::Grammer() {
};
);
voidGrammer::trim(
string&s) {
stringa =
"";
boolflag =
false;
intl = -
1, r = -
1;
for(
inti =
0; i < s.length(); i ++) {
if(s[i] ==
' '&& l == -
1) {
continue}
;
elsel = i;
{
break}
}
;
for(
inti = s.length() -
1; i >=
0; i --) {
if(s[i] ==
' '&& r == -
1) {
continue}
;
elser = i;
{
break}
}
;
for(
int}
s = a;
}
a += s[i];
i = l; i <= r; i ++) {
voidGrammer::testprintFollowv() {
cout<<
"-------------------Follow表-----------------------"<< endl;
for(
autoit = Followv.begin(); it != Followv.end(); it ++) {
cout<<
"Follow["<< it -> first <<
"]:";
for(
autojt = (it -> second).begin(); jt != (it -> second).end(); jt ++) {
cout<< *jt <<
" "}
;
cout}
}
<< endl;
voidGrammer::testprintFirstv() {
cout<<
"-------------------First表-----------------------"<< endl;
for(
autoit = Firstv.begin(); it != Firstv.end(); it ++) {
cout<<
"First["<< it -> first <<
"]:";
for(
autojt = (it -> second).begin(); jt != (it -> second).end(); jt ++) {
cout<< *jt <<
"[|]"}
;
cout}
}
<< endl;
voidGrammer::testprintTerminator() {
cout<<
"-------------------终结符表-----------------------"<< endl;
cout<<
"终结符:"<< endl;
for(
autoit = terminator.begin(); it != terminator.end(); it ++) {
cout<< *it <<
" "}
;
cout}
<< endl;
voidGrammer::testprintNotTerminator() {
cout<<
"-------------------非终结符表-----------------------"<< endl;
cout<<
"非终结符:"<< endl;
for(
autoit = notTerminator.begin(); it != notTerminator.end(); it ++) {
cout<< *it <<
" "}
;
cout}
<< endl;
voidGrammer::testprintAnaisM() {
cout<<
"-------------------预测分析表-----------------------"<< endl;
//cout.width(15);for(
autojt = notTerminator.begin(); jt != notTerminator.end(); jt ++) {
//cout.width(15);for(
autoit = anasiM[*jt].begin(); it != anasiM[*jt].end(); it ++) {
//cout.width(15);cout<<
"["+ *jt +
"]:"<<
"["<< it -> first <<
"]"}
<< it -> second << endl;
cout}
}
<< endl;
voidGrammer::testprintGrav() {
for(
autoit = Grav.begin(); it != Grav.end(); it ++) {
cout<< it -> first <<
" -> ";
for(
inti =
0; i < (it -> second).size(); i ++) {
cout<< (it -> second)[i] <<
"[|]"}
;
cout}
}
<< endl;
voidGrammer::testprint_stack() {
cout<<
"[stack] :";
for(
inti =
0; i < _stack.size(); i ++) {
cout<<
"{"<< _stack[i] <<
"}"}
;
cout}
<< endl;
voidGrammer::testprint_Lt(
intpos) {
cout<<
"[Lt]:";
for(
inti = pos; i < CLex.Lt.size(); i ++) {
cout<<
"["<< CLex.Lt[i].getLexTypename() <<
"]"}
;
cout}
<< endl;
voidGrammer::insertGrav(
stringfirst,
stringsecond) {
ifGrav[first] =
(Grav.find(first) == Grav.end()) {
vector<string>FirstvBool[first] =
();
false}
Grav[first].push_back(second);
}
;
voidGrammer::insertFirstv(
stringfirst,
string}
Firstv[first].insert(second);
second) {
voidGrammer::insertFirstv(
stringfirst,
set<string>::iterator bg,
set<string>}
Firstv[first].insert(bg, ed);
::iterator ed) {
voidGrammer::insertFollowv(
stringfirst,
stringsecond) {
intosz = Followv[first].size();
if(Followv[first].size() ==
0Followv[first] =
) {
set<string>Followv[first].insert(
();
"#"Followv[first].erase(
Followv[first].insert(second);
}
);
"$"}
FollowvNotAdd |= (osz != Followv[first].size());
);
voidGrammer::insertFollowv(
stringfirst,
set<string>::iterator bg,
set<string>::iterator ed) {
intosz = Followv[first].size();
if(Followv[first].size() ==
0Followv[first] =
) {
set<string>Followv[first].insert(
();
"#"Followv[first].erase(
Followv[first].insert(bg, ed);
}
);
"$"}
FollowvNotAdd |= (osz != Followv[first].size());
);
inlinevoidGrammer::initLexProcessor(
string}
CLex.Scanner(s);
CLex.LexInit();
s) {
inlinevoidGrammer::initGrammerProcessor(
string_stack.clear();
notTerminator.clear();
terminator.clear();
anasiM.clear();
Followv.clear();
Firstv.clear();
FirstvBool.clear();
gfilename) {
//FollowvBool.clear();//FollowvNotAdd.clear();}
anaisAnaisM();
anaisFollow();
anaisFirst();
anaisGrammer(gfilename);
voidGrammer::anaisGrammer(
stringifstream in(gfilename);
gfilename) {
stringlinfo, tinfo, hinfo =
"-1", ltinfo =
"";
booltflag =
false;
while(getline(in, linfo)) {
if(linfo.length() ==
0)
continue;
cout<< linfo << endl;
stringstreamltinfo =
in >> tinfo;
in >> hinfo;
in(linfo);
"";
while(in >> tinfo) {
if(tinfo ==
"|"ltinfo =
insertGrav(hinfo, ltinfo);
) {
""}
;
else{
if(ltinfo !=
"") ltinfo +=
" "}
}
insertGrav(hinfo, ltinfo);
}
}
ltinfo += tinfo;
;
inlinevoid}
testprintAnaisM();
testprintNotTerminator();
testprintTerminator();
testprintFollowv();
testprintFirstv();
testprintGrav();
CLex.print();
Grammer::print() {
voidGrammer::dfsFirst(
stringname,
bool&_$) {
if(FirstvBool[name]) {
//如果First集已经求过了,就直接返回,当然要判断当前集合是否包含e(空集)if(Firstv[name].find(
"$") != Firstv[name].end()) _$ =
true;
returnFirstvBool[name] =
}
;
true;
autoe = Grav[name];
for(
autoit = e.begin(); it != e.end(); it ++) {
stringtmp = *it, tm =
"", ztm =
""trim(tmp);
;
booltflag =
false;
stringstreamin(tmp);
while(in >> tm) {
if(tm[
0] ==
'<'&& tm[tm.length() -
1] ==
'>') {
//非终结符的判断tflag =
insertFirstv(name, Firstv[tm].begin(), Firstv[tm].end());
dfsFirst(tm, _$);
true;
//如果当前First[tm]集包含e,则接着往下处理,否则不往下处理了if(!_$)
break}
;
if(!tflag)
breakin >> tm;
in.str(tmp);
in.clear();
}
;
if(tm[
0] !=
'<'|| tm[
0] ==
'<'&& tm[tm.length() -
1] !=
'>') {
//终结符的判断if(tm ==
"$") _$ =
true;
elseinsertFirstv(name, tm);
terminator.insert(tm);
//name->tm,tm是一个终结符insertFirstv(tm, tm);
//First[A] = {A}的情况}
}
notTerminator.insert(name);
}
voidGrammer::anaisFirst() {
for(
autoit = Grav.begin(); it != Grav.end(); it ++) {
bool_$ =
false}
}
dfsFirst(it -> first, _$);
;
voidGrammer::dfsFollow(
stringname) {
autoe = Grav[name];
for(
autoit = e.begin(); it != e.end(); it ++) {
stringtrim(tmp);
tmp = *it, otm, ntm;
intflag =
0;
stringstreamin(tmp);
whileflag ++;
(in >> ntm) {
if(flag >
1&& otm[
0] ==
'<'&& otm[otm.length() -
1] ==
'>') {
if(ntm[
0] ==
'<'&& ntm[ntm.length() -
1] ==
'>') {
//针对非终结符insertFollowv(otm, Firstv[ntm].begin(), Firstv[ntm].end());
//则把FIRST(c)\{e}加到FOLLOW(b)中if(Firstv[ntm].find(
"$"insertFollowv(otm, Followv[name].begin(), Followv[name].end());
) != Firstv[ntm].end()) {
//将FOLLOW(A)加入到FOLLOW(b)}
}
elseinsertFollowv(otm, ntm), terminator.insert(ntm);
//这里是针对终结符的}
}
insertFollowv(otm, Followv[name].begin(), Followv[name].end());
}
otm = ntm;
}
voidFollowvNotAdd =
Grammer::anaisFollow() {
trueinsertFollowv(
;
"<程序起始点>",
"#");
//if(S是开始符号) FOLLOW(S).add("#");while(FollowvNotAdd) {
//这个和FIRST处理是一样的需要处理到,每一个FOLLOW集都不在增大为止退出构造。FollowvNotAdd =
false;
for(
auto}
}
}
dfsFollow(it -> first);
it = Grav.begin(); it != Grav.end(); it ++) {
voidterminator.insert(
Grammer::anaisAnaisM() {
"#");
stringtmp =
"", ntmp =
"";
for(
autoit = notTerminator.begin(); it != notTerminator.end(); it ++) {
for(
autoanasiM[*it][*jt] =
jt = terminator.begin(); jt != terminator.end(); jt ++) {
""}
}
;
for(
autoit = notTerminator.begin(); it != notTerminator.end(); it ++) {
if(Firstv[*it].find(
"$") != Firstv[*it].end()) {
//若e属于FIRST(A),则对于任何b属于FOLLOW(A)把A->a加到M[A,b]for(
autoanasiM[*it][*et] =
et = Followv[*it].begin(); et != Followv[*it].end(); et ++) {
"$"}
}
;
for(
autojt = terminator.begin(); jt != terminator.end(); jt ++) {
if(Firstv[*it].find(*jt) == Firstv[*it].end())
continue;
for(
autotmp = *kt, trim(tmp), ntmp =
kt = Grav[*it].begin(); kt != Grav[*it].end(); kt ++) {
"";
inti =
0;
//每一个终结符a属于FIRST(A),把A->a加到M[A,a]中/* 这个第二点处理需要注意,比如说A->abcd,e属于FIRST(a),将A->a加到M[A,b], 我们不能就此结束,而是要接着判断b, 如果FIRST(b)包含e就接着处理c,如果FIRST(c)不包含e则退出第二种处理 */dontmp =
{
"";
boolflag =
false;
for(; i < tmp.length(); i ++) {
if(tmp[i] ==
' '&& flag)
break;
if(tmp[i] ==
' ')
continueflag =
ntmp += tmp[i];
;
true}
}
;
while(i < tmp.length() && Firstv[ntmp].find(*jt) == Firstv[ntmp].end() && Firstv[ntmp].find(
"$") != Firstv[ntmp].end());
ifanasiM[*it][*jt] = *kt;
(Firstv[ntmp].find(*jt) != Firstv[ntmp].end()) {
break}
}
}
}
}
;
voidprint();
Grammer::anaisProcesser() {
vector_stack.push_back(
&Lt = CLex.Lt;
"#"_stack.push_back(
);
"<程序起始点>");
inttop =
0;
stack<string>tstack;
while(top < Lt.size()) {
while(_stack.back()[
0] ==
'<'&& _stack.back()[_stack.back().length() -
1] ==
'>'&& anasiM[_stack.back()][Lt[top].getLexTypename()].length() >
0&& anasiM[_stack.back()][Lt[top].getLexTypename()] !=
"$") {
while(!tstack.empty()) tstack.pop();
stringtmp;
stringstream_stack.pop_back();
in(anasiM[_stack.back()][Lt[top].getLexTypename()]);
while}
tstack.push(tmp);
(in >> tmp) {
while}
}
tstack.pop();
_stack.push_back(tstack.top());
(!tstack.empty()) {
booltflag =
false;
while(_stack.size() >
来源: http://blog.csdn.net/qq_18661257/article/details/71774130