Untitled
unknown
plain_text
3 years ago
2.2 kB
4
Indexable
#include<stdio.h> #include<string.h> #include<conio.h> #include<stdlib.h> char oper[24][2]={"-","+","=","*","/","&","|","~","<",">","^","%","!","+=","*=","%=","/=","++","--","<=",">=","!=","=="}; char *op="",buf[8],tmpvar[10],tmpopr[10],varlist[10][10]={""},*mstr; int len,i=0,k,cnt,j, v=0,vl=0,fdup=0,fopr=0,b,ii,c,inval=0; int lexi(char *); int valid(); int main() { char *str,*temp; clrscr(); printf("\nEnter the Expression\n"); scanf("%s",mstr); if(valid()==0) { lexi(mstr); printf("\n\nExpression is Valid\n\n"); } else { printf("\n Invalid Expression ~ brackets"); getch(); return 0; } if(inval==0) { printf("\n%s\n",op); printf("\n\nSYMBOL TABLE\n"); for(i=0;i<vl;i++) printf("\n%s ID%d",varlist[i],i); getch(); } return 0; } int lexi(char *str) { len=strlen(str); while(i<len) { if(str[i]=='('||str[i]==')') { sprintf(buf,"%c",str[i]); strcat(op,buf); i++; } v=0; while(isalpha(str[i])||isdigit(str[i])) { tmpvar[v]=str[i]; v++; i++; } tmpvar[v]='\0'; fdup=0; if(!isdigit(tmpvar[0])) { for(cnt=0;cnt<=vl;cnt++) if(strcmp(tmpvar,varlist[cnt])==0) { sprintf(buf,"ID%d",cnt); strcat(op,buf); fdup=1; } if(fdup!=1) { strcpy(varlist[vl],tmpvar); sprintf(buf,"ID%d",vl); strcat(op,buf); vl++; } } else strcat(op,tmpvar); if(i==0||!isalpha(str[0])||(!isalpha(str[len-1])&& !isdigit(str[len-1]))&&str[len-1]!=')') { printf("\nInvalid expression \n"); inval++; getch(); return 0; } k=0; if(str[i]=='('||str[i]==')') { sprintf(buf,"%c",str[i]); strcat(op,buf); i++; } while(!isalpha(str[i])&&!isdigit(str[i])&&str[i]!='('&&str[i]!=')') { tmpopr[k]=str[i]; k++; i++; } tmpopr[k]='\0'; fopr=0; for (j=0;j<24;j++) if(strcmp(tmpopr,oper[j])==0) { strcat(op,tmpopr); fopr=1; break; } if(str[len-1]!=')'&&(strcmp(tmpopr,"")!=0&&fopr==0)) { printf("\nInvalid Expression \n"); inval++; getch(); return 0; } } } int valid() { b=ii=0; while(mstr[ii++]!='\0') { if(mstr[ii]=='(') { b++; ii++; if (isalpha(mstr[ii])) continue; else { printf("\n Invalid expression ~ brackets"); inval++; getch(); exit(0); } } if(mstr[ii]==')') { b--; ii++; } } return b; }
Editor is loading...