PL0语法分析程序(表达式部分)

//getsym.h

//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;
}

//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();
 }
}

 

//test.cpp

//author:lu wen-bin  student code:04110251
//completed date:Jan 26th,2006
//functin: This program analyze the expression segment of PL0 language
//if the expression is correct,the program will print:Congtrulations,
//(^T^),your expression is conformed to the Grammar regular!
//if your expression missed paren,no matter what kind,rapren of lapren
//the program will indicate it and how many you missed
//the program adopted the recursive sub-function method
//consisted of three parts,the expr, the term and the factor

#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include"getsym.h"

//declartion of the function which will be used afterward
void term();
void factor();
void isparen_matching();

//the definition of the expression function
void expr(){
 if(!strcmp(sym,"plus") || !strcmp(sym,"minus")){
  getsym();
 }
 term();

 while(!strcmp(sym,"plus") || !strcmp(sym,"minus")){
  getsym();
  term();
 }
}

//the defination of the term function
void term(){
 factor();
 while(!strcmp(sym,"times") || !strcmp(sym,"slash")){
  getsym();
  factor();
 }
}

//the defination of the fatctor function
void factor(){
 if(!strcmp(sym,"ident")) getsym();    //first circumstance
 else if(!strcmp(sym,"number")) getsym(); //second
 else if(!strcmp(sym,"lparen")){  //if is lapren
  getsym();
  expr();
  if(!strcmp(sym,"rparen")){
   getsym();
  }
  else{            //if not followed a rapren,wrong
   cout<<"Sorry,you missed a rparen in the factor!"<<endl;
   exit(1);
  }
 }
 //else the factor is not correct
 else{
  cout<<"Sorry,your factor is not correct!"<<endl;
  exit(1);
 }
}

void isparen_matching(){
    //numofparen!=0 means the lapren and rparen
 //is not matching in amount
    if(numofparen!=0){
  cout<<"Sorry,you missed "<<fabs(numofparen)<<" lparen!"<<endl;
  exit(1);
 }
}

 

void main(){
    strcpy(sym,"initial");
 loadfile();
 getch();
 getsym();
 expr();
    isparen_matching();

 //if the analyzer program hasn't told wrong message,then the
 //expression is correct!
 cout<<"Congratulations! (^T^) Your exprssion is conformed to the Grammar regular!"<<endl;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值