?? bianyi.cpp
字號:
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
enum words{ id, // 標識符 0
mainsy, // 主函數 1
ifsy, // if 2
elsesy, // else 3
intsy, // int 4
intconst, // 整形常量 5
addop, // + 6
subop, // - 7
timeop, // * 8
divop, // / 9
lpar, // ( 10
rpar, // ) 11
lbpar, // { 12
rbpar, // } 13
geop, // >= 14
goop, // > 15
leop, // <= 16
loop, // < 17
neop, // != 18
eqop, // = 19
comma, // , 20
semicolon, // ; 21
eoline, // 回車轉行 22
eofile, // 文件結束 23
other // 其他錯誤 24
};
int CODE[200]={0}; // 詞法分析生成的代碼表,最大長度為200
char ID[50][10]={0}; // 標識符表最大長度為50,一個標識符的最多有效位為10
char KEYWORD[][5]={"\0","main","if","else","int"}; // 關鍵字表
int NEXT=0; // 代碼表的當前位置
int ID_NUMBER=1; // 標識符表的當前長度
int error=0,overflow=0; // 出錯和常數溢出標志
int G_next=0; // 記錄語法分析讀代碼表的當前位置
int Line=1; // 記錄源程序當前行數
int Isdigit(char); // 判斷輸入字符是否為數字
int Isletter(char); // 判斷輸入字符是否為字母
int Find_Keyword(char *);// 查找關鍵字表
int Find_ID(char *); // 查找標識符表
void WORD_ananlyse(); // 詞法分析器
// 語法分析器子函數
void Complex_Sentence();
void Declare();
void ID_List();
void Sentence_Sec();
void Sentence();
void Condition();
void Evaluate();
void Expression();
void Exp_Item();
void Exp_Item_();
void E_Item();
void Item();
void Item_();
void WORD_analyse() // 詞法分析
{
char ch; // 取輸入的字符
int c_number; // 計算標識符或常數的位數
char word[10]=""; // 存放單詞
while((ch=cin.peek())!='#') // 判斷是否結束
{
if (ch==10) // 將eoline填入代碼表
{
CODE[NEXT]=eoline;
NEXT++;
}
cin>>ch; // 讀入字符
if (Isdigit(ch)) // 處理整型常數
{
int value=0; // 常數值
c_number=0; // 計算常數的位數
while(Isdigit(ch))
{
if(c_number>5||(value>3276&&ch>7)) // 判斷常數是否溢出
overflow=1;
value=value*10+ch-48;
cin>>ch;
c_number++;
} cin.putback(ch); // 放回剛取的字符
CODE[NEXT]=intconst;// 將intconst填入代碼表
NEXT++;
CODE[NEXT]=value; // 將常數值填入代碼表
NEXT++;
}
else if(Isletter(ch)) // 接受字符串
{ int re_w; // 記錄單詞在關鍵字表或標識符表中的位置
word[0]=ch;
c_number=1;
while ((ch=cin.peek())!=' '&&c_number<10)
{
if(Isletter(ch)||Isdigit(ch))
{
word[c_number]=ch;
c_number++;
cin>>ch;
}
else break;
}
word[c_number]=0;
if((re_w=Find_Keyword(word))!=-1) // 若單詞為關鍵字
{
CODE[NEXT]=re_w;
NEXT++;
}
else if((re_w=Find_ID(word))!=-1) // 若單詞為標識符
{
CODE[NEXT]=id; // 將id填入代碼表
NEXT++;
CODE[NEXT]=re_w; // 將標識符在ID[]數組的位置填入代碼表
NEXT++;
}
else // 定義新的標識符
{
CODE[NEXT]=id; // 將id填入代碼表
NEXT++;
strcpy(ID[ID_NUMBER],word); // 將標識符填入ID[]數組
CODE[NEXT]=ID_NUMBER; // 將標識符在ID[]數組的位置填入代碼表
NEXT++;
ID_NUMBER++;
}
}
else if(ch=='!')
{
if(ch=='=')
{
cin>>ch;
CODE[NEXT]=neop; // 將neop填入代碼表
NEXT++;
}
}
else if(ch=='>')
{
if(cin.peek()=='=')
{
cin>>ch;
CODE[NEXT]=geop; // 將geop填入代碼表
NEXT++;
}
else // 將goop填入代碼表
{ CODE[NEXT]=goop; NEXT++;}
}
else if(ch=='<')
{
if(cin.peek()=='=')
{
cin>>ch;
CODE[NEXT]=neop; // 將leop填入代碼表
NEXT++;
}
else // 將loop填入代碼表
{ CODE[NEXT]=loop; NEXT++;}
}
else if(ch=='=') // 將eqop填入代碼表
{ CODE[NEXT]=eqop; NEXT++;}
else if(ch=='+') // 將addop填入代碼表
{ CODE[NEXT]=addop; NEXT++;}
else if(ch=='-') // 將subop填入代碼表
{ CODE[NEXT]=subop; NEXT++;}
else if(ch=='*') // 將timeop填入代碼表
{ CODE[NEXT]=timeop; NEXT++;}
else if(ch=='/') // 將divop填入代碼表
{ CODE[NEXT]=divop; NEXT++;}
else if (ch==',')
{ CODE[NEXT]=comma; NEXT++;}
else if(ch==';') // 將semicolon填入代碼表
{ CODE[NEXT]=semicolon; NEXT++;}
else if(ch=='(') // 將lpar填入代碼表
{ CODE[NEXT]=lpar; NEXT++;}
else if(ch==')') // 將rpar填入代碼表
{ CODE[NEXT]=rpar; NEXT++;}
else if(ch=='{') // 將lbpar填入代碼表
{ CODE[NEXT]=lbpar; NEXT++;}
else if(ch=='}') // 將rbpar填入代碼表
{ CODE[NEXT]=rbpar; NEXT++;}
else
{ CODE[NEXT]=other; NEXT++;}
}
CODE[NEXT]=eofile; // 裝入文件結束符
}
int Find_Keyword(char *word)
{ int i;
for (i=0 ; i<=5 ; i++)
{ if (!strcmp(KEYWORD[i],word)) // 找到匹配的關鍵字
return i;
}
return -1; // 匹配失敗
}
int Find_ID(char *word)
{
int i;
for (i=1 ; i<=ID_NUMBER ; i++)
{
if (!strcmp(ID[i],word))
return i; // 找到匹配的標識符
}
return -1; // 匹配失敗
}
int Isletter(char ch) // 是否為字母,是返回1 ,否返回0
{
if(((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z')))
return(1);
else return(0);
}
int Isdigit(char ch) // 是否為數字,是返回1 ,否返回0
{
if((ch>='0')&&(ch<='9'))
return(1);
else return(0);
}
void match(enum words word) // 匹配當前代碼
{
if (CODE[G_next]==word)
{
G_next++;
if(word==intconst||word==id)
G_next++;
}
else error=1; // 出錯
while (CODE[G_next]==eoline)
{ // 記錄行數
G_next++;
Line++;
}
}
void GRAMMAR_analyse() // 程序->main(){復合語句}
{
match(mainsy);
if (!error)
match(lpar);
if (!error)
match(rpar);
if (!error)
match(lbpar);
if (!error)
Complex_Sentence();
if (!error)
match(rbpar);
if (!error)
match(eofile);
}
void Complex_Sentence() // 復合語句->說明部分;復合語句|語句部分
{
if (CODE[G_next]==intsy)
{
Declare(); // 說明部分,聲明變量
match(semicolon);
if (CODE[G_next]!=rbpar && CODE[G_next]!=eofile && !error)
Complex_Sentence();// 復合語句
}
else
Sentence_Sec();
}
void Declare() // 說明部分->int 變量列
{
match(intsy);
if (!error) ID_List();
}
void Sentence_Sec() // 語句部分->語句 語句部分|語句
{
while (!error && CODE[G_next]!=eoline && CODE[G_next]!=rbpar )
Sentence();
}
void Sentence() // 語句->條件語句|賦值語句|;
{
if (CODE[G_next]==ifsy)
Condition(); // 條件語句
else if (CODE[G_next]==semicolon)
match(semicolon);// 空語句
else
Evaluate(); // 賦值語句
}
void ID_List() // 變量列->id,變量列|id
{
match(id);
if(!error&&CODE[G_next]==comma)
{
match(comma);
if (!error) ID_List();
}
}
void Condition() // 條件語句->if(表達式)語句 else 語句 | if(表達式)語句
{
match(ifsy);
if (!error)
match(lpar);
if (!error)
Expression();
if (!error)
match(rpar);
if (!error)
Sentence();
if (!error && CODE[G_next]==elsesy)
{
match(elsesy); if (!error)
Sentence();
}
}
void Expression() // 表達式->表達項>表達項|表達項>=表達項|表達項<表達項
{ // |表達項<=表達項|表達項!=表達項|表達項=表達項
Exp_Item();
if (!error)
switch(CODE[G_next])
{
case geop: match(geop);
break;
case goop: match(goop);
break;
case leop: match(leop);
break;
case loop: match(loop);
break;
case neop: match(neop);
break;
case eqop: match(eqop);
break;
default: return;
}
if (!error) Exp_Item();
}
void Exp_Item() //表達項->項 表項
{
Item();
if (!error)
Exp_Item_();
}
void Exp_Item_() // 表項->+項|表項 表項->-項|表項
{
if (CODE[G_next]==addop)
{
match(addop);
if (!error)
Item();
if (!error)
Exp_Item_();
}
else if (CODE[G_next]==subop)
{
match(subop);
if (!error)
Item();
if (!error)
Exp_Item_();
}
}
void Item() // 項->子項 項’
{
E_Item();
if (!error)
Item_();
}
void Item_() // 項’->* 子項 項’ 項’->/子項 項’
{
if (CODE[G_next]==timeop)
{
match(timeop);
if (!error)
E_Item();
if (!error)
Item_();
}
else if (CODE[G_next]==divop)
{
match(divop);
if (!error)
E_Item();
if (!error)
Item_();
}
}
void E_Item() // 子項->id|num|(表項)
{
switch(CODE[G_next])
{ case id: match(id);
break;
case intconst: match(intconst);
break;
default: match(lpar);
if (!error) Exp_Item();
if (!error) match(rpar);
}
}
void Evaluate() // 賦值語句->id=表達式
{
match(id);
if (!error)
match(eqop);
if (!error)
Expression();
if (!error)
match(semicolon);
}
void main()
{
cout<<"enter the origial codes"<<endl;
WORD_analyse(); // 詞法分析
GRAMMAR_analyse(); // 語法分析
cout<<"the coding table is "<<endl;
for(int i=0; i<NEXT;i++) // 輸出代碼表
{
cout<<CODE[i]<<" ";
if(CODE[i]==22) cout<<endl;
}
if (overflow) // 輸出結果
cout <<"\n something wrong about int." <<endl;
else if (error)
cout <<"\n the " <<Line<<"th line is wrong" <<endl;
else
cout <<"\n all right!" <<endl;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -