?? analyse.cpp
字號:
#include "scan.h"
/*語法分析部分:
* 轉換
* lexp →NUMBER| ( op lexp-seq )
* op →+ | - | *
* lexp -seq → lexp-seq lexp|lexp
* 為EBNF如下:
* <lexp> -> <NUMBER> | (<op> <lexp -seq>)
* <op> -> + | - | *
* <lexp -seq> -> <lexp> { <lexp> }
*
*/
void compiler::error(){
cout << "context error......" << endl;
fprintf(listing, "%s\n", "An error occoured.");
exit(0);
}
void compiler::match(TokenType type){
if(token.type == type){
token = getToken();
}
else{
error();
}
}
treenode* compiler::lexp(){
treenode* tempNode = new treenode();
if(token.tokenValue[0]=='('){
match(LPAREN);
if(strcmp(token.tokenValue,"+") == 0){
match(PLUS);
tempNode->content = *(new tokenForUse(PLUS, '+'));
treenode* left = new treenode();
left = lexp_seq();
treenode* right = new treenode();
right = lexp_seq();
tempNode->leftChild = left;
tempNode->rightChild = right;
}
else
if(strcmp(token.tokenValue,"-") == 0){
match(MINUS);
tempNode->content = *(new tokenForUse(MINUS, '-'));
treenode* left = new treenode();
left = lexp_seq();
treenode* right = new treenode();
right = lexp_seq();
tempNode->leftChild = left;
tempNode->rightChild = right;
}
else
if(strcmp(token.tokenValue,"*") == 0){
match(TIMES);
tempNode->content = *(new tokenForUse(TIMES, '*'));
treenode* left = new treenode();
left = lexp_seq();
treenode* right = new treenode();
right = lexp_seq();
tempNode->leftChild = left;
tempNode->rightChild = right;
}
else
error();
match(RPAREN);
}else
if(token.type == NUM){
tempNode->content = token;
match(NUM);
}
return tempNode;
}
treenode* compiler::lexp_seq(){
return lexp();
}
void compiler::printResult(treenode* &root){
if(root != NULL){
printResult(root->leftChild);
fprintf(listing, "%s\t", root->content.tokenValue);
printResult(root->rightChild);
}
}
void compiler::printTree(treenode* &root){
//按層次遍歷;
nodes.push_back(root);
while(!nodes.empty()){
verNodes.push_back(nodes.front());
if(nodes.front()->leftChild != NULL){
nodes.push_back(nodes.front()->leftChild);
nodes.push_back(nodes.front()->rightChild);
}
nodes.pop_front();
}
int i=0;
int cap = 0;
int numoftwo = 0;
while(i<verNodes.size()){
if(cap == 0){
fprintf(listing, "\n%s\n", verNodes[i]->content.tokenValue);
cap = 2;
i++;
}
else{
for(int j=0; j<cap && i<verNodes.size(); j++){
fprintf(listing, "%s\t", verNodes[i]->content.tokenValue);
if(verNodes[i]->content.type != NUM){
numoftwo++;
}
i++;
}
cap = numoftwo*2;
cout << endl;
fprintf(listing, "\n");
}
}
}
int compiler::getResult(treenode* &root){
int result;
switch(root->content.type){
case PLUS :{
result = getResult(root->leftChild) + getResult(root->rightChild);
break;
}
case MINUS :{
result = getResult(root->leftChild) - getResult(root->rightChild);
break;
}
case TIMES :{
result = getResult(root->leftChild) * getResult(root->rightChild);
break;
}
case NUM :{
result = atoi(root->content.tokenValue);
break;
}
}
return result;
}
int main(){
compiler myComp = *new compiler();
fprintf(myComp.listing, "詞法分析結果: \n");
//語法樹的根結點;
treenode* resultroot = new treenode();
//語法分析;
myComp.token = myComp.getToken();
resultroot = myComp.lexp();
fprintf(myComp.listing, "轉換為正常算術表達式: \n");
myComp.printResult(resultroot);
fprintf(myComp.listing, "\n最終計算結果: ");
fprintf(myComp.listing, "%d\n", myComp.getResult(resultroot));
fprintf(myComp.listing, "打印語法樹(按層打印對應不是很整齊): \n");
myComp.printTree(resultroot);
cout << "結果詳見out.txt." << endl;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -