Tiplarni dinamik tarzda aniqlash. Reja: Tiplarni dinamik tarzda aniqlash - bet 77

|
Tiplarni dinamik tarzda aniqlash. Reja: Tiplarni dinamik tarzda aniqlash
|
bet | 77/143 | Sana | 20.07.2024 | Hajmi | 0,81 Mb. | | #268096 |
Bog'liq Tiplarni dinamik tarzda-fayllar.org
Len – belgilar soni, *pstr ga qo‘shilgan, satr oxiri yoki oxiri emasligi aniqlovchi statistika va qiymatlari qaytaradi.
int read_until_eof (it&) .*$ 0 0 1 OK
|
int read_until_eof (it&, pstr*) .*$ len len OK
|
int read_fix_length (it&, n) .{n} -1 0 OK
|
int read_fix_length (it&, n, pstr*) .{n} -(1+len) 0 2 OK
|
int read_fix_str (it&, s) str -(1+len) 0 ili (1+len) 9 OK
|
int read_fix_char (it&, c) c -1 0 yoki 1 11 OK
|
int read_charclass (it&, is) [ ] -1 0 yoki 1 OK
|
int read_charclass (it&, spn) [ ] -1 0 yoki 1 OK
|
int read_charclass (it&, bspn) [ ] -1 0 yoki 1 OK
|
int read_charclass_s (it&, is, pstr*) [ ] -1 0 yoki 1 OK
|
int read_charclass_s (it&, spn, pstr*) [ ] -1 0 yoki 1 1 OK
|
int read_charclass_s (it&, bspn, pstr*) [ ] -1 0 yoki 1 5 OK
|
int read_charclass_c (it&, is, ch*) [ ] -1 0 yoki 1 OK
|
int read_charclass_c (it&, spn, ch*) [ ] -1 0 yoki 1 1 OK
|
int read_charclass_c (it&, bspn, ch*) [ ] -1 0 yoki 1 OK
|
int read_c (it&, ch*) . -1 0 5 OK
|
int read_while_charclass (it&, is) [ ]* -(1+len) len OK
|
int read_while_charclass (it&, spn) [ ]* -(1+len) len 2 OK
|
int read_while_charclass (it&, bspn) [ ]* -(1+len) len OK
|
int read_while_charclass (it&, is, pstr*) [ ]* -(1+len) len OK
|
int read_while_charclass (it&, spn, pstr*) [ ]* -(1+len) len OK
|
int read_while_charclass (it&, bspn, pstr*) [ ]* -(1+len) len 1 OK
|
int read_until_charclass (it&, is) .*[ ]<- -(1+len) len OK
|
int read_until_charclass (it&, spn) .*[ ]<- -(1+len) len 1 OK
|
int read_until_charclass (it&, bspn) .*[ ]<- -(1+len) len OK
|
int read_until_charclass (it&, is, pstr*) .*[ ]<- -(1+len) len OK
|
int read_until_charclass (it&, spn, pstr*) .*[ ]<- -(1+len) len 2 OK
|
int read_until_charclass (it&, bspn, pstr*) .*[ ]<- -(1+len) len OK
|
int read_until_char (it&, c) .*c -(1+len) len OK
|
int read_until_char (it&, c, pstr*) .*c -(1+len) len OK
|
Oxirgi belgini o‘qigandan so‘ng, iterator undan keyin emas, balki unga qaratilgan.
Funksiya nomidan foydalanib, xato kodini yoki xato xabarini qaytarish qulay, shuning uchun parserlar matni yanada aniqroq ko‘rinadi, maxsus makroslar keltiramiz:
#define r_if(expr) if((expr)==0) #define r_while(expr) while((expr)==0) #define r_ifnot(expr) if(expr) #define r_whilenot(expr) while(expr)
#define rm_if(expr) if((expr)>=0) #define rm_while(expr) while((expr)>=0) #define rm_ifnot(expr) if((expr)<0) #define rm_whilenot(expr) while((expr)<0) #define rp_if(expr) if((expr)>0) #define rp_while(expr) while((expr)>0) #define rp_ifnot(expr) if((expr)<=0) #define rp_whilenot(expr) while((expr)<=0)
|
Bu maʻlumotlarni saqlash va bool tipiga mos akslantirish bo‘lsada baʻzi sinfni qilish uchun juda muhim.
Umuman, o‘zgarmas bo‘lgan havolalarga qarshiman, funksiyasi o‘zgartirish kerak argumentlar ko‘rsatgichlari bilan emas, balki bir ko‘rsatkich orqali unga o‘tgan bo‘lishi kerak
Ushbu funksiyalardan foydalanishning ikkita misol keltiramiz: 7.2-dastur. Arifmetik ifodalarni hisoblash.
#define ONE_SOURCE //strin.cpp
#include "../strin.h"
using namespace str; using std::cout; using std::endl;
#define ifnot(expr) if(!(expr)) #define r_if(expr) if((expr)==0) #define r_while(expr) while((expr)==0) #define r_ifnot(expr) if(expr)
#define r_whilenot(expr) while(expr)
|
template void dump(it_t tmp, mes_t mes){
string s; read_fix_length(tmp,50,&s);
std::cerr <}
template
const char * read_sum(it_t & it, double * prez);
template
const char * read_expr(it_t & it, double * prez){ const char * err; if(read_s_fix_char(it,'(')){
r_ifnot(err = read_sum(it,prez)) return err;
ifnot(read_s_fix_char(it,')')) return "qavsni yoping";
return 0;
}
int x; ifnot(read_dec(it,&x)){
return "son";
}
*prez = x; return 0;
}
DEF_STRING(md,"*/")
template
const char * read_mul(it_t & it, double * prez){ typedef char_type ch_t;
const char * err;
r_ifnot(err = read_expr(it,prez)) return err;
while(true){
ch_t zn; ifnot(read_s_charclass_c(it,make_span(md().s),&zn))
return 0; double x;
r_ifnot(err=read_expr(it,&x))
|
return err; if(zn==(ch_t)'*') *prez *= x;
else *prez /= x;
}
return 0;
}
DEF_STRING(pm,"+-")
template
const char * read_sum(it_t & it, double * prez){ typedef char_type ch_t;
const char * err;
r_ifnot(err = read_mul(it,prez)) return err;
while(true){
ch_t zn; ifnot(read_s_charclass_c(it,make_span(pm().s),&zn))
return 0; double x;
r_ifnot(err = read_mul(it,&x)) return err;
if(zn==(ch_t)'+') *prez += x;
else *prez -= x;
}
return 0;
}
int main()
{
using namespace std; if(0)
{
string str_expression_1 = "5+84/(51)"; string str_expression_2 = "15 + (16 /6 *4)"; string str_expression_3 = "7 + ( 4* 4)"; string str_expression = str_expression_3;
cout <<"hisoblash ifodasi:" r_ifnot(err=read_sum(p,&rez)){
int pos=p-str_expression.c_str(); for(int i=1; i
|
cout <<' '; cout <<'^'
cout << "natija: " << rez << endl; cout <<"================" << endl;
}
if(1)
{
try{
while(!atend(strin)){
cout <<"arifmetik ifoda kiriting va 'end' bilan tugating "
err=read_sum(strin,&rez); linecol lc=get_linecol(strin); read_until_str(strin,"end"); r_if(err)
cout << "natija: " << rez << endl;
else{
cout <<" zanjir " <}
}
}
catch(const char * mes){
cerr << "xatolik: " << mes << endl; return -1;
}
catch(string & mes){
cerr << "xatolik: " << mes << endl; return -1;
}
catch(stream_exception & mes){
cerr << "xatolik: " << mes.what() << endl; return -1;
}
catch(...){
cerr << "noaniq [atolik" << endl;
}}
return 0;
}
|
Bugungi kunda dasturchilar uchun maxsus sintaktik tahlillovchilar yaratish ko‘p hollarda shart emas. Chunki bu mashaqqatli va ko‘p vaqt talab qiladi. Umuman olganda qandaydir dasturiy taʻminotlarni, axborot tizimlarining lingvistik taʻminoti yaratilsa, shu taʻminot uchun analizator yaratish kerak bo‘lishi mumkin. Dunyoda dasturlash tillarini ishlab chiqaruvchilar bu sohada juda oldilab ketgan. Shuning uchun sintatik tahlillovchilar bo‘yicha nazariy tushunchaga ega bo‘lish yetarlidi. Ammo ayrim matematik yangi sonli sinflar uchun ishlab chiqish mumkin.
|
| |