?? table.cpp
字號:
/*
This part becomes simple after FIRST set and FOLLOW set
are constructed
finished on 6.8
Math Frog
*/
#include "global.h"
string LL1_table[MAXSIZE][MAXSIZE]; // import
list<string> tmpfirst; // for holding first set of alpha
bool isLL1Grammer;
void initTable() // initialize the LL1_table
{
list<string>::iterator iter;
LL1_table[0][0] = "M[VN,VT]";
int row = 1,col = 1;
for(iter = VN.begin();iter != VN.end();iter++)
LL1_table[row++][0] = *iter;
for(iter = VT.begin();iter != VT.end();iter++)
LL1_table[0][col++] = *iter;
LL1_table[0][col] = "$" ; // make $ a terminal
// for decide the position
}
void buildTable()
{
list<string>::iterator iter,it,copy;
string hold;
int save,pos;
int row,col; // for adding element to the table ...
for(int line = 0;line < colineno;line++){
// compute A->X1X2...Xn
iter = coProduction[line].begin();
hold = *iter;// save for searching
for(row = 1;row <= VN.size();row++)
if(LL1_table[row][0] == hold) break; // find the current non-terminal in the table
for(int i = 0;i < VN.size();i++)
if(hold == symbol[i].name)
{pos = i;break;} // in the symbol ...
iter++;iter++; // to the right
it = iter; // begin finding
////computing the first set of alpha ///////////////
while(it != coProduction[line].end()){
if(isIn(*it,VT) || *it == ee) // the current symbol is a terminal
{tmpfirst.push_back(*it);break;} // because terminal's first set has no e in
// so the computation ends or in the last
else{ // is not a terminal
for(int i = 0;i < VN.size();i++)
if(*it == symbol[i].name)
{save = i;break;}
for(copy = symbol[save].first.begin();copy != symbol[save].first.end();copy++)
if(!isIn(*copy,tmpfirst))
tmpfirst.push_back(*copy); // to copy
}
if(!isIn(ee,tmpfirst)) break; // symbol[save]'s first has no e ,to end
// the computation...
it++;
}
///////////////////////////////////////////////
// scan alpha's first set...
for(it = tmpfirst.begin();it != tmpfirst.end();it++)
if(isIn(*it,VT)){ // the current symbol is a terminal
// then add A->alpha to LL1_table[A][alpha]
// decide the position
for(col = 1;col <= VT.size() + 1;col++)
if(*it == LL1_table[0][col]) break;
listToString(LL1_table[row][col],coProduction[line]);
}
if(isIn(ee,tmpfirst)){ // if the empty string is in the FIRST(X1X2..Xn)
for(it = symbol[pos].follow.begin();it != symbol[pos].follow.end();it++)
if(isIn(*it,VT) || *it == "$"){
if(*it == "$")
listToString(LL1_table[row][VT.size() + 1],coProduction[line]);
else{
for(col = 1;col <= VT.size() + 1;col++)
if(*it == LL1_table[0][col]) break;
listToString(LL1_table[row][col],coProduction[line]);
}
}
}
tmpfirst.erase(tmpfirst.begin(),tmpfirst.end());
}
for(row = 1;row <= VN.size();row++)
for(col = 1;col <= VT.size() + 1;col++)
if(LL1_table[row][col].empty())
LL1_table[row][col] = "Error";
}
void printTable()
{
int row ,col;
cout << "***********************************" << endl;
cout << "** The LL(1) Table is as follow **" << endl;
cout << "***********************************" << endl;
for(row = 1;row <= VN.size();row++){
for(col = 1;col <= VT.size() + 1;col++){
cout << " |** M[ " << LL1_table[row][0] << ", " << LL1_table[0][col] << " ] = ";
cout << LL1_table[row][col] << "\t";
cout << " **|" << endl;
}
}
cout << "***********************************" << endl;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -