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