1 / 21

Постановка задачі

Постановка задачі Створити програму - інтерпретатор для модифікованої мови SPL, в якій добавлена операція піднесення в другу степінь. e xp (a , b) begin int z; z=1; while b do if b%2 then z=z*a end; a=a*a;b=b/2 end; return z end main() begin int x,y,z; read x; read y;

Download Presentation

Постановка задачі

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Постановка задачі Створити програму - інтерпретатор для модифікованої мови SPL, в якій добавлена операція піднесення в другу степінь

  2. exp(a,b) begin int z; z=1; while b do if b%2 then z=z*a end; a=a*a;b=b/2 end; return z end main() begin int x,y,z; read x; read y; print exp(x,y); z=x ^; print z; end

  3. void get() { if(nch==EOF) { lex=EOF; return; } while(isspace(nch)) { if(nch=='\n') nst++; nch=getc(PF); } if(isdigit(nch)) number(); else

  4. if(isalpha(nch)) word(); else if(nch=='('||nch==')'||nch==','||nch==';'||nch=='=' ||nch=='+'||nch=='-'||nch=='*'||nch=='/'||nch=='%'||nch=='^') { lex=nch; nch=getc(PF); } else if(nch==EOF) lex=EOF; else puts("Hедопустимый символ"); return; }

  5. void term() { int op; fact(); while(lex=='*'||lex=='/'||lex=='%'||lex=='^') { op=lex=='*'?5:lex=='/'?6:lex=='%'?7:11; get(); if(op!=11) fact(); gen(OPR,op); } return; }

  6. void operat(int a) { int j=t-1,kk; switch(a) { case 1:printf("1>"); fflush(stdout);push(red());break; case 2:printf("%d\n",st[t--]);break; case 3:st[j]+=st[t--];break; case 4:st[j]-=st[t--];break; case 5:st[j]*=st[t--];break;

  7. case 6:if(st[t]==0) puts("/:делитель =0"); st[j]/=st[t--];break; case 7:if(st[t]<=0) puts("%%:делитель =0"); st[j]%=st[t--];break; case 8:st[t]=-st[t];break; case 9:for(kk=0;kk<25;kk++) fprintf(ptc,"tc[%i]=%d \n",kk,st[kk]); j=st[sp-2];st[sp-j-2]=st[t]; t=sp-j-2;p=st[sp-1];sp=st[sp];break; case 10:p=-3; case 11:st[t]*=st[t]; } return; }

  8. Постановка задачі Створити програму - інтерпретатор для модифікованої мови SPL, в якійусі ключові слова на російській мові. Крім того, добавлені дві операції: піднесення у квадрат і збільшення числа (increment) на два.

  9. exp(a ,b) начало целый z; z=1; пока b делать если b%2 тогда z=z*a конец; а=a*a; b=b/2 конец main() начало целый x,y;

  10. читать x; читать y; печать exp(x,y); печать x^; печать x@; конец

  11. /*Кодылексемязыка SPL*/ enum{IFL=257,WHILEL,RETRL,PRITL,READL,BEGINL,ENDL,INTL,CONSTL,THENL,DOL, NUMB,IDEN}; int isalpharus(char c); int isspace1(char c); int isdigit1(char c);

  12. void get() { while(lex!=EOF) { while(isspace1(nch)) { if(nch=='\n') nst++; nch=getc(PF); } if(isdigit1(nch)) number();

  13. if(isalpharus(nch)) word(); else if(nch=='('||nch==')'||nch==','||nch==';'||nch=='=' ||nch=='+'||nch=='-'||nch=='*'||nch=='/'||nch=='%'|| nch=='^'|| nch=='@') { lex=nch; nch=getc(PF); } else if(nch==EOF) lex=EOF; else printf("Hедопустимыйсимвол nch=%c nst=%i\n",nch,nst); return; }

  14. void word() { static char*serv[]={"если","пока","возврат","печать", "читать","начало","конец","целый","константа", "тогда","делать"}; static int cdl[]={IFL,WHILEL,RETRL,PRITL,READL,BEGINL, ENDL,INTL, CONSTL,THENL,DOL}; int i; char tx[40]; char*p; for(p=tx;isdigit1(nch)||isalpharus(nch);nch=getc(PF)) *(p++)=nch; *p='\0';

  15. int isalpharus(char c) { int v; if(isalpha(c)||c=='а'||c=='б'||c=='в'||c=='г'||c=='д'||c=='е'||c=='ж'||c=='з'||c=='д'||c=='е'||c=='ж'||c=='з'||c=='и'||c=='й'||c=='к'||c=='л' ||c=='м'||c=='н'||c=='о'||c=='п'||c=='р'||c=='с'||c=='т'||c=='у' ||c=='ф'||c=='х'||c=='ц'||c=='ч'||c=='ш'||c=='щ' ||c=='ы'||c=='э'||c=='ю'||c=='я'||c=='ь') v=8; else v=0; return v; }

  16. int isspace1(char c) { int v; if(c==‘ \ ‘ ||c==‘ \n ‘ ||c=='\ t‘ ||c==‘ \f ') v=1; else v=0; return v; }

  17. int isdigit1(char c) { int v; if(c=='0'||c=='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'||c=='8'||c=='9') v=2; else v=0; return v; }

  18. void term() { int op; fact(); while(lex=='*'||lex=='/'||lex=='%'||lex=='^'||lex=='@') { op=lex=='*'?5:lex=='/'?6:lex=='%'?7:lex=='^'?11:12; get(); if(op!=11 && op!=12) { fact(); } gen(OPR,op); } return; }

  19. void operat(int a) { int j=t-1; switch(a) { case 1:printf("1>"); fflush(stdout);push(red());break; case 2:printf("%d\n",st[t--]);break; case 3:st[j]+=st[t--];break; case 4:st[j]-=st[t--];break; case 5:st[j]*=st[t--];break; case 6:if(st[t]==0) puts("/:делитель =0"); st[j]/=st[t--];break; case 7:if(st[t]<=0) puts("%%:делитель =0"); st[j]%=st[t--];break;

  20. case 8:st[t]=-st[t];break; case 9:j=st[sp-2];st[sp-j-2]=st[t]; t=sp-j-2;p=st[sp-1];sp=st[sp];break; case 10:p=-3; case 11: st[t] *= st[t]; break; case 12: st[t] += 2; break; } return; }

More Related