您的位置:首页 > Web前端

PL0 表达式的计算(递归子程序法)

2006-01-27 13:47 204 查看
//getsym.h

//author:luwenbin   
//student code:04110251
//completed date:Jan 27th,2006
//function:this program is used to calculate the expression of PL0
//adopted method:recursive function
//the calculation is based on the correction of the Grammar,if the
//Grammar is illeagel,then the program will indicate the reason
//and halt the calculation,mainly due to the unmatchable of the right
//paren and the left paren

#include"getsym.h"      //this program will use the getsym function
#include<stdlib.h>
#include<iostream.h>
#include<string.h>

int term();                    //function declaration
int factor();

int expr(){
 //this function will return an integer value which
 //calculate the expression
 int etemp1,etemp2;
 if(!strcmp(sym,"plus") || !strcmp(sym,"minus")) getsym();
 etemp1=term();
 while(!strcmp(sym,"plus") || !strcmp(sym,"minus")){
  char tmp[20];
  strcpy(tmp,sym);
  getsym();
  etemp2=term();
  if(!strcmp(tmp,"plus")){
   etemp1=etemp1+etemp2;
  }
  else if(!strcmp(tmp,"minus")){
   etemp1=etemp1-etemp2;
  }
 }
 return etemp1;  //return the final value of expr
}

int term(){
 //this function will return an integer value
 //which calculate the term
 int ttemp1,ttemp2;
 ttemp1=factor();
 while(!strcmp(sym,"times") || !strcmp(sym,"slash")){
  char tmp[20];
  strcpy(tmp,sym);
  getsym();
  ttemp2=factor();
  if(!strcmp(tmp,"times")){
   ttemp1=ttemp1*ttemp2;
  }
  else if(!strcmp(tmp,"slash")){
   ttemp1=ttemp1/ttemp2;
  }
 }
 return ttemp1;           //return the final value of term;
}

int factor(){
//factor is the fundemantal segment of the expression which consisted
//of three parts,the indent or the number or the expression,which the
//lattar is recursive and it will call the expr() to calculate its
//value, in the factor if the paren is not matching,the program will
//indicate the mistake and halt the program
 int ftemp1;
 if(!strcmp(sym,"ident")){
  getsym();
  ftemp1=1;
 }
 else if(!strcmp(sym,"number")){
  ftemp1=num;
  getsym();
 }
 else if(!strcmp(sym,"lparen")){
  getsym();
  ftemp1=expr();
  if(!strcmp(sym,"rparen")){
   getsym();
  }
  else{
   cout<<"You missed a rparen!"<<endl;
   exit(1);
  }
 }
 else{
  cout<<"Your factor is not correct!"<<endl;
  exit(1);
 }
 return ftemp1;    //return the final value of factor
}

void main(){
 loadfile();
 getch();
 getsym();
 int result;
 result=expr();
 cout<<result<<endl;
}

//author:lu wen-bin  
//finished date:January 26th,2006
#include<fstream.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
//reserved words declaration
char* word[]=   {"begin","call","const","do","end","if","odd",
                 "procedure","read","then","var","while","write"};
char* wsym[]=   {"beginsym","callsym","constsym","dosym","endsym",
                 "ifsym","oddsym","procsym","readsym","thensym",
        "varsym","whilesym","writesym"};
char* symbol[]= {"+","-","*","/","(",")","=",",",".","#",";"};
char* sysm[]=   {"plus","minus","times","slash","lparen","rparen",
                 "eql","comma","peroid","neg","semicolon"};

//buffer block and full access variable declaration
char buffer[1024];
char ch;
int index=0;
int filelen;
char sym[20];
char ID[20];
int num;
//the following variable is used to check the lapren and rparen
//is matching or not
int numofparen=0;

//getch(),one time to get one char from the buffer block
void getch(){
 ch=buffer[index];
 index++;
}

//the main() method will use it to load file from the disk
//to the buffer block and it will initial some variables
void loadfile(){
 ifstream in("a.txt");
 int i=0;
 while(!in.eof()){
  buffer[i++]=in.get();
 }
 filelen=i;
 i=i-1;
    buffer[i]='@';
 index=0;
}

 

//test.cpp

//getsym() method,each time when used,it will rewrite the value of
//sym variable, if the program is analyzed, the getsym() will
//set "final" value to the sym variable
//which you can use such characater to end
void getsym(){
    while(isspace(ch) || ch=='/n' || ch=='/r')
  getch();
 if(ch=='@') strcpy(sym,"final");
 else if(isalpha(ch)){
  char tmp[20];
  int i=0;
  while(isalpha(ch) || isdigit(ch)){
   tmp[i++]=ch;
   getch();
  }
  tmp[i]='/0';
  for(i=0; i<13; i++)
   if(!strcmp(word[i],tmp)) break;
  if(i==13){
   strcpy(sym,"ident");
   strcpy(ID,tmp);
  }
  else{
   strcpy(sym,wsym[i]);
   strcpy(ID,tmp);
  }
 }
 else if(isdigit(ch)){
  char tmp[20];
  int i=0;
  while(isdigit(ch)){
   tmp[i++]=ch;
   getch();
  }
  tmp[i]='/0';
  num=atoi(tmp);
  strcpy(sym,"number");
 }
 else if(ch==':'){
  getch();
  if(ch=='='){
   strcpy(sym,"becomes");
   getch();
  }
  else strcpy(sym,"nul");
 }
 else if(ch=='<'){
  getch();
  if(ch=='='){
   strcpy(sym,"leq");
   getch();
  }
  else strcpy(sym,"lss");
 }
 else if(ch=='>'){
  getch();
  if(ch=='='){
   strcpy(sym,"geq");
   getch();
  }
  else strcpy(sym,"gtr");
 }
 else{
  char tmp[2];
  tmp[0]=ch;
  tmp[1]='/0';
  int i;
  for(i=0; i<11; i++)
   if(!strcmp(symbol[i],tmp)) break;
  if(i<11){
   strcpy(sym,sysm[i]);
   if(!strcmp(sym,"lparen")) numofparen++;
   if(!strcmp(sym,"rparen")) numofparen--;
   strcpy(ID,tmp);
  }
  getch();
 }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息