Advertisement
tiberiup

Analizor

Jan 17th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. FILE *fisier;
  7. char linie[100], atom1[30], tipAtom1[6], atom2[30], tipAtom2[6], cuvinteCheie[16][15];
  8. int pozitie, numarCuvinteCheie, liniaCurenta=1, penultimaIndentare, ultimaIndentare, ultimulIdentificator=-1, numarIdentificatori, esteIndiceAccesat;
  9.  
  10. struct identificator
  11. {
  12. char nume[30];
  13. char tip[6];
  14. }id[40];
  15.  
  16. void analizeaza();
  17. void preiaCuvinteCheie();
  18. void urmatorulAtom();
  19. int esteCuvantCheie(char *sir);
  20. int pozitieIdentificator(char *sir);
  21. void adaugaIdentificator(char *sir);
  22. void Eroare(char *mesaj);
  23. void Atentionare(char *mesaj);
  24. void treciPesteSpatiiOptionale();
  25.  
  26. int A(int indentare, int obligatoriu);
  27. int B(int indentare);
  28. int C(int indentare,int flag);
  29. int D(int indentare);
  30. int E(int flag1, int flag2, int flag3);
  31. int F(int indentare);
  32. int G(int indentare);
  33. int H();
  34. int I();
  35. int K(int indentare);
  36. int L(int indentare);
  37. int M();
  38. int N();
  39. int O();
  40. int P();
  41. int Q();
  42. int R();
  43. int S(int indentare);
  44. int T();
  45. int U(int indentare);
  46. int V(int indentare);
  47. int W();
  48. int X();
  49. int Y(int indentare);
  50. int Z(int indentare, int obligatoriu);
  51.  
  52. int en();
  53. int e0();
  54. int e1();
  55. int e2();
  56. int ec();
  57. int co();
  58. int coa();
  59. int in();
  60.  
  61. int main()
  62. {
  63. analizeaza();
  64.  
  65. return 0;
  66. }
  67.  
  68. void analizeaza()
  69. {
  70. preiaCuvinteCheie();
  71. fisier=fopen("cod.txt","r");
  72. fgets(linie,100,fisier);
  73. urmatorulAtom();
  74. if(A(0,1)>0)
  75. puts("Programul nu are erori.");
  76. fclose(fisier);
  77. }
  78.  
  79. void preiaCuvinteCheie()
  80. {
  81. FILE *f;
  82. int p=0,c;
  83. f=fopen("cuvinteCheie.txt","r");
  84. while(!feof(f))
  85. {
  86. c=fgetc(f);
  87. if(c!='\n')
  88. cuvinteCheie[numarCuvinteCheie][p++]=c;
  89. else
  90. {
  91. numarCuvinteCheie++;
  92. p=0;
  93. }
  94. }
  95. fclose(f);
  96. }
  97.  
  98. void urmatorulAtom()
  99. {
  100. int p=pozitie;
  101.  
  102. if(strcmp(tipAtom2,"spati"))
  103. {
  104. strcpy(atom1,atom2);
  105. strcpy(tipAtom1,tipAtom2);
  106. if(!strcmp(tipAtom2,"ranou"))
  107. {
  108. fgets(linie,100,fisier);
  109. if(!feof(fisier))
  110. p=pozitie=0;
  111. else
  112. return ;
  113. liniaCurenta++;
  114. ultimaIndentare=0;
  115. }
  116. }
  117.  
  118. if(isalpha(linie[p]))
  119. {
  120. strcpy(tipAtom2,"ident");
  121. while(isalnum(linie[p]))
  122. p++;
  123. }
  124. else if(isdigit(linie[p]))
  125. {
  126. strcpy(tipAtom2,"nripo");
  127. while(isdigit(linie[p]))
  128. {
  129. p++;
  130. if(linie[p]=='.')
  131. {
  132. p++;
  133. strcpy(tipAtom2,"nrrpo");
  134. }
  135. }
  136. }
  137. else if(linie[p]=='-'&&isdigit(linie[p+1])&&!strstr("nripo,nrine,nrrpo,nrrne,ident",tipAtom1)&&strcmp(atom1,")"))
  138. {
  139. strcpy(tipAtom2,"nrine");
  140. p++;
  141. while(isdigit(linie[p]))
  142. {
  143. p++;
  144. if(linie[p]=='.')
  145. {
  146. p++;
  147. strcpy(tipAtom2,"nrrne");
  148. }
  149. }
  150. }
  151. else if(strchr("()[]{}",linie[p]))
  152. {
  153. strcpy(tipAtom2,"paran");
  154. p++;
  155. }
  156. else if(strchr("+-*/%",linie[p]))
  157. {
  158. strcpy(tipAtom2,"opari");
  159. p++;
  160. }
  161. else if(strchr("!=<>",linie[p]))
  162. {
  163. strcpy(tipAtom2,"oprel");
  164. p++;
  165. if(strchr("!<>",linie[p-1])&&linie[p]=='=')
  166. p++;
  167. }
  168. else if(linie[p]==' '||linie[p]=='\t')
  169. {
  170. strcpy(tipAtom2,"spati");
  171. while(linie[p]==' '||linie[p]=='\t')
  172. p++;
  173.  
  174. if(pozitie==0)
  175. ultimaIndentare=p-pozitie;
  176. }
  177. else if(linie[p]==',')
  178. {
  179. strcpy(tipAtom2,"virgu");
  180. p++;
  181. }
  182. else if(linie[p]=='"')
  183. {
  184. strcpy(tipAtom2,"sirca");
  185. p++;
  186. while(linie[p]!='"')
  187. if(linie[p]=='\n')
  188. {
  189. Eroare("lipsesc \" de sfarsit");
  190. break;
  191. }
  192. else
  193. p++;
  194. if(linie[p]=='"')
  195. p++;
  196. }
  197. else if(linie[p]=='\n')
  198. {
  199. strcpy(tipAtom2,"ranou");
  200. ultimaIndentare=0;
  201. }
  202. else
  203. {
  204. strcpy(tipAtom2,"necun");
  205. p++;
  206. }
  207.  
  208. //printf("%d %d\n",pozitie,p);
  209. strncpy(atom2,linie+pozitie,p-pozitie);
  210. atom2[p-pozitie]=0;
  211.  
  212. if(esteCuvantCheie(atom2))
  213. strcpy(tipAtom2,"cheie");
  214.  
  215. pozitie=p;
  216. /*
  217. if(!strcmp(tipAtom2,"ranou"))
  218. puts("rand");
  219. else
  220. puts(atom2);
  221. */
  222.  
  223.  
  224. }
  225.  
  226. int esteCuvantCheie(char *sir)
  227. {
  228. int i;
  229. for(i=0;i<numarCuvinteCheie;i++)
  230. if(!strcmp(sir,cuvinteCheie[i]))
  231. return 1;
  232. return 0;
  233. }
  234.  
  235. int pozitieIdentificator(char *sir)
  236. {
  237. int i;
  238. for(i=0;i<numarIdentificatori;i++)
  239. if(!strcmp(sir,id[i].nume))
  240. return i;
  241. return -1;
  242. }
  243.  
  244. void adaugaIdentificator(char *sir)
  245. {
  246. int i;
  247. for(i=0;i<numarIdentificatori;i++)
  248. if(!strcmp(sir,id[i].nume))
  249. {
  250. ultimulIdentificator=i;
  251. return ;
  252. }
  253. strcpy(id[ultimulIdentificator=numarIdentificatori++].nume,sir);
  254. }
  255.  
  256. void Eroare(char *mesaj)
  257. {
  258. printf("Eroare la linia %d: %s.\n",liniaCurenta,mesaj);
  259. }
  260.  
  261. void Atentionare(char *mesaj)
  262. {
  263. printf("Atentionare la linia %d: %s.\n",liniaCurenta,mesaj);
  264. }
  265.  
  266. void treciPesteSpatiiOptionale()
  267. {
  268. if(!strcmp(tipAtom2,"spati"))
  269. urmatorulAtom();
  270. }
  271.  
  272. int A(int indentare, int obligatoriu)
  273. {
  274. int o;
  275.  
  276. if(ultimaIndentare>indentare)
  277. {
  278. Eroare("indentare neasteptata");
  279. return -1;
  280. }
  281. else if((o=B(indentare))>0)
  282. return 1;
  283. else if(o<0)
  284. return -1;
  285. else if((o=D(indentare))>0)
  286. return 1;
  287. else if(o<0)
  288. return -1;
  289. else if((o=G(indentare))>0)
  290. return 1;
  291. else if(o<0)
  292. return -1;
  293. else if((o=K(indentare))>0)
  294. return 1;
  295. else if(o<0)
  296. return -1;
  297. else if((o=L(indentare))>0)
  298. return 1;
  299. else if(o<0)
  300. return -1;
  301. else if((o=V(indentare))>0)
  302. return 1;
  303. else if(o<0)
  304. return -1;
  305. else if(obligatoriu)
  306. {
  307. Eroare("lipseste comanda");
  308. return -1;
  309. }
  310. return 0;
  311. }
  312.  
  313. int B(int indentare)
  314. {
  315. if(!strcmp(atom2,"Preia"))
  316. {
  317. urmatorulAtom();
  318. if(!strcmp(tipAtom2,"spati"))
  319. {
  320. urmatorulAtom();
  321. if(E(1,1,1)>0)
  322. {
  323. if(C(indentare,1)>=0)
  324. return 1;
  325. else
  326. return -1;
  327. }
  328. else
  329. {
  330. return -1;
  331. }
  332. }
  333. else
  334. {
  335. Eroare("lipseste spatiu dupa cuvantul cheie 'Preia'");
  336. return -1;
  337. }
  338. }
  339. else
  340. return 0;
  341. }
  342.  
  343. int C(int indentare,int flag)
  344. {
  345. treciPesteSpatiiOptionale();
  346. if(!strcmp(tipAtom2,"virgu"))
  347. {
  348. urmatorulAtom();
  349. treciPesteSpatiiOptionale();
  350.  
  351. if(!flag&&!strcmp(atom2,"sir"))
  352. {
  353. Atentionare("nu este nevoie de cuvantul cheie 'sir' pentru a afisa o variabila de tip sir de caractere");
  354. urmatorulAtom();
  355. treciPesteSpatiiOptionale();
  356. }
  357.  
  358. if(E(flag,1,1)>0)
  359. {
  360. return C(indentare,flag);
  361. /*
  362. if(C(indentare,flag)>0)
  363. return 1;
  364. else
  365. return -1;
  366. */
  367. }
  368. else
  369. {
  370. return -1;
  371. }
  372. }
  373. else if(!strcmp(tipAtom2,"ranou"))
  374. {
  375. urmatorulAtom();
  376. treciPesteSpatiiOptionale();
  377. return Z(indentare,0);
  378. /*
  379. int o;
  380. if((o=Z(indentare))>0)
  381. return 1;
  382. else if(o==0)
  383. return 0;
  384. else
  385. return -1;
  386. */
  387. }
  388. else if(!strcmp(atom2,";"))
  389. {
  390. Atentionare("nu este nevoie de ; la sfarsitul unei instructiuni");
  391. urmatorulAtom();
  392. treciPesteSpatiiOptionale();
  393. return C(indentare,flag);
  394. }
  395. else
  396. {
  397. char s[50];
  398. sprintf(s,"cuvant necunoscut: '%s' ",atom2);
  399. Eroare(s);
  400. return -1;
  401. }
  402. }
  403.  
  404. int D(int indentare)
  405. {
  406. if(!strcmp(atom2,"Afiseaza"))
  407. {
  408. urmatorulAtom();
  409. if(!strcmp(tipAtom2,"spati"))
  410. {
  411. urmatorulAtom();
  412. if(!strcmp(tipAtom2,"sirca")||E(0,1,1)>0)
  413. {
  414. if(!strcmp(tipAtom2,"sirca"))
  415. {
  416. urmatorulAtom();
  417. treciPesteSpatiiOptionale();
  418. }
  419.  
  420. if(C(indentare,0)>=0)
  421. return 1;
  422. else
  423. return -1;
  424. }
  425. else
  426. return -1;
  427. }
  428. else
  429. {
  430. Eroare("lipseste spatiu dupa cuvantul cheie 'Afiseaza'");
  431. return -1;
  432. }
  433. }
  434. else
  435. return 0;
  436. }
  437.  
  438. int E(int flag1, int flag2, int flag3)
  439. {
  440. char s[50];
  441. int o=0,p;
  442. esteIndiceAccesat=0;
  443.  
  444. if(!strcmp(atom2,"sir"))
  445. {
  446. o=1;
  447. urmatorulAtom();
  448. treciPesteSpatiiOptionale();
  449. }
  450.  
  451. if(!strcmp(tipAtom2,"ident"))
  452. {
  453. if(flag1)
  454. {
  455. adaugaIdentificator(atom2);
  456. p=ultimulIdentificator;
  457. }
  458. else
  459. {
  460. int p=pozitieIdentificator(atom2);
  461. if(p<0)
  462. {
  463. sprintf(s,"variabila '%s' nu a fost declarata",atom2);
  464. Eroare(s);
  465. return -1;
  466. }
  467. else
  468. ultimulIdentificator=p;
  469. }
  470.  
  471. urmatorulAtom();
  472. if(!strcmp(atom2,"("))
  473. {
  474. while(!strcmp(atom2,"("))
  475. {
  476. urmatorulAtom();
  477. if(en())
  478. {
  479. if(!strcmp(atom2,")"))
  480. {
  481.  
  482. urmatorulAtom();
  483. if(flag1)
  484. strcpy(id[p].tip,"colec");
  485. esteIndiceAccesat=1;
  486. }
  487. else
  488. {
  489. Eroare("lipseste )");
  490. return -1;
  491. }
  492. }
  493. else
  494. {
  495. Eroare("indicele colectiei trebuie sa fie expresie numerica");
  496. return -1;
  497. }
  498. /*
  499. if(!strcmp(tipAtom2,"ident"))
  500. {
  501.  
  502.  
  503. int p=pozitieIdentificator(atom2);
  504. if(p>=0)
  505. {
  506. if(strstr("nmsir,numar,sirca",id[p].tip))
  507. {
  508. urmatorulAtom();
  509. if(!strcmp(atom2,")"))
  510. {
  511. urmatorulAtom();
  512. if(flag1)
  513. strcpy(id[ultimulIdentificator].tip,"colec");
  514. }
  515. else
  516. {
  517. Eroare("lipseste )");
  518. return -1;
  519. }
  520. }
  521. else
  522. {
  523. sprintf(s,"variabila indice %s nu poate fi colectie de date",atom2);
  524. Eroare(s);
  525. return -1;
  526. }
  527. }
  528. else
  529. {
  530. sprintf(s,"variabila indice %s nu a fost declarata",atom2);
  531. Eroare(s);
  532. return -1;
  533. }
  534. }
  535. else
  536. {
  537. Eroare("lipseste indicele colectiei");
  538. return -1;
  539. }
  540. */
  541. }
  542.  
  543. return 1;
  544. }
  545. else
  546. {
  547. if(flag1)
  548. {
  549. if(o==0)
  550. strcpy(id[ultimulIdentificator].tip,"numar");
  551. else if(o==1)
  552. strcpy(id[ultimulIdentificator].tip,"sirca");
  553. }
  554. else if (flag3)
  555. {
  556. int p=pozitieIdentificator(atom1);
  557. if(!strcmp(id[p].tip,"colec"))
  558. {
  559. sprintf(s,"lipseste referirea la un element al variabilei colectie '%s'",atom1);
  560. Eroare(s);
  561. return -1;
  562. }
  563. }
  564. }
  565.  
  566. return 1;
  567. }
  568. else if(flag2)
  569. {
  570. if(flag1)
  571. Eroare("lipseste un nume de variabila");
  572. else
  573. Eroare("lipseste un nume de variabila sau un sir de caractere");
  574. return -1;
  575. }
  576.  
  577. return 0;
  578. }
  579.  
  580. int F(int indentare)
  581. {
  582. treciPesteSpatiiOptionale();
  583. if(!strcmp(tipAtom2,"virgu"))
  584. {
  585. urmatorulAtom();
  586. treciPesteSpatiiOptionale();
  587. if(H()>0)
  588. {
  589. return F(indentare);
  590. /*
  591. if(F(indentare)>0)
  592. return 1;
  593. else
  594. return -1;
  595. */
  596. }
  597. else
  598. {
  599. Eroare("lipseste un element dupa , ");
  600. return -1;
  601. }
  602. }
  603. else if(!strcmp(tipAtom2,"ranou"))
  604. {
  605. urmatorulAtom();
  606. treciPesteSpatiiOptionale();
  607. return Z(indentare,0);
  608. /*
  609. if(Z(indentare)>0)
  610. return 1;
  611. else
  612. return -1;
  613. */
  614. }
  615. else
  616. return -1;
  617. }
  618.  
  619. int G(int indentare)
  620. {
  621. if(!strcmp(atom2,"Fie"))
  622. {
  623. urmatorulAtom();
  624. if(!strcmp(tipAtom2,"spati"))
  625. {
  626. urmatorulAtom();
  627. if(H()>0)
  628. {
  629.  
  630. if(F(indentare)>=0)
  631. return 1;
  632. else
  633. return -1;
  634. }
  635. else
  636. {
  637. return -1;
  638. }
  639. }
  640. else
  641. {
  642. Eroare("lipseste spatiu dupa cuvantul cheie 'Fie'");
  643. return -1;
  644. }
  645. }
  646. else
  647. return 0;
  648. }
  649.  
  650. int H()
  651. {
  652. if(!strcmp(atom2,"sir"))
  653. {
  654. Atentionare("nu este nevoie de cuvantul cheie 'sir' pentru a declara o variabila de tip sir de caractere");
  655. urmatorulAtom();
  656. treciPesteSpatiiOptionale();
  657. }
  658.  
  659. char s[50];
  660. int p=pozitieIdentificator(atom2);
  661. if(p>=0)
  662. strcpy(s,id[p].tip);
  663.  
  664. if(E(1,1,0)>0)
  665. {
  666. if(p>=0)
  667. strcpy(id[p].tip,s);
  668.  
  669. if(!strcmp(atom2,"="))
  670. {
  671. urmatorulAtom();
  672. treciPesteSpatiiOptionale();
  673.  
  674. int p=ultimulIdentificator,o=I();
  675. if(o<0)
  676. {
  677. strcpy(id[p].tip,"");
  678. Eroare("lipseste element dupa = ");
  679. return -1;
  680. }
  681. else if(!esteIndiceAccesat)
  682. {
  683. if(o==1)
  684. strcpy(id[p].tip,"numar");
  685. else if(o==2)
  686. strcpy(id[p].tip,"sirca");
  687. else if(o==3)
  688. strcpy(id[p].tip,"colec");
  689. }
  690.  
  691.  
  692. return 1;
  693. }
  694. else
  695. {
  696. Eroare("lipseste = dupa numele de variabila la declarare");
  697. return -1;
  698. }
  699. }
  700. else
  701. return -1;
  702. }
  703.  
  704. int I()
  705. {
  706. if(strstr("nripo,nrine,nrrpo,nrrne",tipAtom2)||!strcmp(atom2,"-")||!strcmp(atom2,"("))
  707. {
  708. if(en()>0)
  709. return 1;
  710. else
  711. return -1;
  712. }
  713. else if(!strcmp(tipAtom2,"sirca"))
  714. {
  715. if(ec()>0)
  716. return 2;
  717. else
  718. return -1;
  719. }
  720. else if(!strcmp(atom2,"{"))
  721. {
  722. if(co()>0)
  723. return 3;
  724. else
  725. return -1;
  726. }
  727. else if(!strcmp(tipAtom2,"ident"))
  728. {
  729. int p=pozitieIdentificator(atom2);
  730. if(p<0)
  731. {
  732. char s[50];
  733. sprintf(s,"variabila '%s' nu a fost declarata",atom2);
  734. Eroare(s);
  735. return -1;
  736. }
  737.  
  738. if(!strcmp(id[p].tip,"numar"))
  739. {
  740. if(en()>0)
  741. return 1;
  742. else
  743. return -1;
  744. }
  745. else if(!strcmp(id[p].tip,"nmsir"))
  746. {
  747. if(en()>0)
  748. return 1;
  749. else
  750. return -1;
  751. }
  752. else if(!strcmp(id[p].tip,"sirca"))
  753. {
  754. if(ec()>0)
  755. return 2;
  756. else
  757. return -1;
  758. }
  759. else if(!strcmp(id[p].tip,"colec"))
  760. {
  761. int poz=pozitie;
  762. char s[6];
  763. strcpy(s,tipAtom2);
  764.  
  765. urmatorulAtom();
  766. if(strstr("spati,ranou",tipAtom2)||!strcmp(atom2,",")||!strcmp(atom2,"}"))
  767. return 3;
  768.  
  769. pozitie=poz;
  770. strcpy(atom2,id[p].nume);
  771. strcpy(tipAtom2,s);
  772.  
  773. if(ec()>0)
  774. return 2;
  775.  
  776. pozitie=poz;
  777. strcpy(atom2,id[p].nume);
  778. strcpy(tipAtom2,s);
  779.  
  780. if(en()>0)
  781. return 1;
  782.  
  783. return -1;
  784. }
  785. else
  786. return -1;
  787. }
  788. else
  789. return -1;
  790. }
  791.  
  792. int K(int indentare)
  793. {
  794. if(!strcmp(atom2,"Daca"))
  795. {
  796. urmatorulAtom();
  797. if(!strcmp(tipAtom2,"spati"))
  798. {
  799. urmatorulAtom();
  800. if(S(indentare)>0)
  801. return 1;
  802. else
  803. return -1;
  804. }
  805. else
  806. {
  807. Eroare("lipseste spatiu dupa cuvantul cheie 'Daca'");
  808. return -1;
  809. }
  810. }
  811. else
  812. return 0;
  813. }
  814.  
  815. int L(int indentare)
  816. {
  817. if(!strcmp(atom2,"Repeta"))
  818. {
  819. urmatorulAtom();
  820. if(!strcmp(tipAtom2,"spati"))
  821. {
  822. urmatorulAtom();
  823. if(!strcmp(atom2,"daca"))
  824. {
  825. urmatorulAtom();
  826. if(!strcmp(tipAtom2,"spati"))
  827. {
  828. urmatorulAtom();
  829. if(S(indentare)>0)
  830. return 1;
  831. else
  832. return -1;
  833. }
  834. else
  835. {
  836. Eroare("lipseste spatiu dupa cuvantul cheie 'daca'");
  837. return -1;
  838. }
  839. }
  840. else
  841. {
  842. Eroare("lipseste cuvantul cheie 'daca'");
  843. return -1;
  844. }
  845.  
  846. }
  847. else
  848. {
  849. Eroare("lipseste spatiu dupa cuvantul cheie 'Repeta'");
  850. return -1;
  851. }
  852. }
  853. else
  854. return 0;
  855. }
  856.  
  857. int M()
  858. {
  859. if(!strcmp(atom2,"nu"))
  860. {
  861. urmatorulAtom();
  862. if(!strcmp(tipAtom2,"spati"))
  863. urmatorulAtom();
  864. else
  865. {
  866. Eroare("lipseste spatiu dupa cuvantul cheie 'nu'");
  867. return -1;
  868. }
  869. }
  870.  
  871. if(N()>0)
  872. return 1;
  873. else
  874. return -1;
  875. }
  876.  
  877. int N()
  878. {
  879. if(O()>0)
  880. {
  881. int o=0;
  882. if(!strcmp(tipAtom2,"spati"))
  883. {
  884. urmatorulAtom();
  885. o=1;
  886. }
  887.  
  888. if(!strcmp(atom2,"si"))
  889. {
  890. if(o==0)
  891. {
  892. Eroare("lipseste spatiu inainte de cuvantul cheie 'si'");
  893. return -1;
  894. }
  895.  
  896. urmatorulAtom();
  897. if(!strcmp(tipAtom2,"spati"))
  898. {
  899. urmatorulAtom();
  900. if(N()>0)
  901. return 1;
  902. else
  903. return -1;
  904. }
  905. else
  906. {
  907. Eroare("lipseste spatiu dupa cuvantul cheie 'si'");
  908. return -1;
  909. }
  910. }
  911. else
  912. return 1;
  913. }
  914. else
  915. return -1;
  916. }
  917.  
  918. int O()
  919. {
  920. if(P()>0)
  921. {
  922. int o=0,p=pozitie;
  923. char s[50];
  924. strcpy(s,tipAtom2);
  925.  
  926. if(!strcmp(tipAtom2,"spati"))
  927. {
  928. urmatorulAtom();
  929. o=1;
  930. }
  931.  
  932. if(!strcmp(atom2,"sau"))
  933. {
  934. if(o==0)
  935. {
  936. Eroare("lipseste spatiu inainte de cuvantul cheie 'sau'");
  937. return -1;
  938. }
  939.  
  940. urmatorulAtom();
  941. if(!strcmp(tipAtom2,"spati"))
  942. {
  943. urmatorulAtom();
  944. if(O()>0)
  945. return 1;
  946. else
  947. return -1;
  948. }
  949. else
  950. {
  951. Eroare("lipseste spatiu dupa cuvantul cheie 'sau'");
  952. return -1;
  953. }
  954. }
  955. else
  956. {
  957. pozitie=p;
  958. strcpy(tipAtom2,s);
  959. return 1;
  960. }
  961. }
  962. else
  963. return -1;
  964. }
  965.  
  966. int P()
  967. {
  968. if(!strcmp(atom2,"("))
  969. {
  970. urmatorulAtom();
  971. treciPesteSpatiiOptionale();
  972. if(N()>0)
  973. {
  974. //urmatorulAtom();
  975. //treciPesteSpatiiOptionale();
  976. if(!strcmp(atom2,")"))
  977. return 1;
  978. else
  979. {
  980. Eroare("lipseste )");
  981. return -1;
  982. }
  983. }
  984. else
  985. return -1;
  986. }
  987. else if(Q()>0)
  988. return 1;
  989. else
  990. return -1;
  991. }
  992.  
  993. int Q()
  994. {
  995. if(en()>0)
  996. {
  997. if(!strcmp(tipAtom2,"oprel"))
  998. {
  999. urmatorulAtom();
  1000. treciPesteSpatiiOptionale();
  1001. if(en()>0)
  1002. return 1;
  1003. }
  1004. }
  1005.  
  1006. return -1;
  1007.  
  1008. }
  1009.  
  1010. int R()
  1011. {
  1012.  
  1013. }
  1014.  
  1015. int S(int indentare)
  1016. {
  1017. if(M()>0)
  1018. {
  1019. if(Y(indentare)>=0)
  1020. {
  1021. if(U(indentare)>=0)
  1022. return 1;
  1023. else
  1024. return -1;
  1025. }
  1026. else
  1027. return -1;
  1028. }
  1029. else
  1030. {
  1031. Eroare("conditie incorecta");
  1032. return -1;
  1033. }
  1034. }
  1035.  
  1036. int T()
  1037. {
  1038.  
  1039. }
  1040.  
  1041. int U(int indentare)
  1042. {
  1043. if(!strcmp(atom2,"Altfel"))
  1044. {
  1045. if(ultimaIndentare==indentare)
  1046. {
  1047. urmatorulAtom();
  1048. if(Y(indentare)>=0)
  1049. return 1;
  1050. else
  1051. return -1;
  1052. }
  1053. else
  1054. {
  1055. Eroare("ramura altfel trebuie sa aiba aceeasi indentare cu instructiunea asociata");
  1056. return -1;
  1057. }
  1058. }
  1059. else
  1060. return 0;
  1061. }
  1062.  
  1063. int V(int indentare)
  1064. {
  1065. if(!strcmp(atom2,"Parcurge"))
  1066. {
  1067. urmatorulAtom();
  1068. if(!strcmp(tipAtom2,"spati"))
  1069. {
  1070. urmatorulAtom();
  1071. if(!strcmp(tipAtom2,"ident"))
  1072. {
  1073. adaugaIdentificator(atom2);
  1074. strcpy(id[ultimulIdentificator].tip,"nmsir");
  1075.  
  1076. urmatorulAtom();
  1077. if(!strcmp(tipAtom2,"spati"))
  1078. {
  1079. urmatorulAtom();
  1080. if(!strcmp(atom2,"din"))
  1081. {
  1082. urmatorulAtom();
  1083. if(!strcmp(tipAtom2,"spati"))
  1084. {
  1085. urmatorulAtom();
  1086. if(W()>0)
  1087. {
  1088. if(Y(indentare)>=0)
  1089. return 1;
  1090. else
  1091. return -1;
  1092. }
  1093. else
  1094. return -1;
  1095. }
  1096. else
  1097. {
  1098. Eroare("lipseste spatiu dupa cuvantul cheie 'din'");
  1099. return -1;
  1100. }
  1101. }
  1102. else
  1103. {
  1104. Eroare("lipseste cuvantul cheie 'din'");
  1105. return -1;
  1106. }
  1107. }
  1108. else
  1109. {
  1110. Eroare("lipseste spatiu dupa variabila iterator");
  1111. return -1;
  1112. }
  1113. }
  1114. else
  1115. {
  1116. Eroare("lipseste variabila iterator");
  1117. return -1;
  1118. }
  1119. }
  1120. else
  1121. {
  1122. Eroare("lipseste spatiu dupa cuvantul cheie 'Parcurge'");
  1123. return -1;
  1124. }
  1125. }
  1126. else
  1127. return 0;
  1128. }
  1129.  
  1130. int W()
  1131. {
  1132. if(E(0,0,0)>0)
  1133. {
  1134. if(strstr("nmsir,sirca,colec",id[ultimulIdentificator].tip))
  1135. return 1;
  1136. else
  1137. {
  1138. Eroare("doar sirurile de caractere si colectiile pot fi iterate");
  1139. return -1;
  1140. }
  1141. }
  1142. else if(in()>0)
  1143. {
  1144. urmatorulAtom();
  1145. if(X()>=0)
  1146. return 1;
  1147. else
  1148. return -1;
  1149. }
  1150. else
  1151. {
  1152. Eroare("intervalul sau variabila care va fi iterata este scrisa incorect");
  1153. return -1;
  1154. }
  1155. }
  1156.  
  1157. int X()
  1158. {
  1159. if(!strcmp(tipAtom2,"spati"))
  1160. {
  1161. urmatorulAtom();
  1162. if(!strcmp(atom2,"cu"))
  1163. {
  1164. urmatorulAtom();
  1165. if(!strcmp(tipAtom2,"spati"))
  1166. {
  1167. urmatorulAtom();
  1168. if(!strcmp(atom2,"pas"))
  1169. {
  1170. urmatorulAtom();
  1171. if(!strcmp(tipAtom2,"spati"))
  1172. {
  1173. urmatorulAtom();
  1174. if(en()>0)
  1175. {
  1176. //urmatorulAtom();
  1177. //treciPesteSpatiiOptionale();
  1178. return 1;
  1179. }
  1180. else
  1181. {
  1182. Eroare("expresia numerica ce indica pasul este scrisa incorect");
  1183. return -1;
  1184. }
  1185. }
  1186. else
  1187. {
  1188. Eroare("lipseste spatiu dupa cuvantul cheie 'pas'");
  1189. return -1;
  1190. }
  1191. }
  1192. else
  1193. {
  1194. Eroare("lipseste cuvantul cheie 'pas'");
  1195. return -1;
  1196. }
  1197. }
  1198. else
  1199. {
  1200. Eroare("lipseste spatiu dupa cuvantul cheie 'cu'");
  1201. return -1;
  1202. }
  1203. }
  1204. else
  1205. return 0;
  1206. }
  1207. else
  1208. return 0;
  1209. }
  1210.  
  1211. int Y(int indentare)
  1212. {
  1213. if(!strcmp(tipAtom2,"ranou"))
  1214. {
  1215. urmatorulAtom();
  1216. if(ultimaIndentare>indentare)
  1217. {
  1218. urmatorulAtom();
  1219. int o=Z(ultimaIndentare,1);
  1220. if(o>=0)
  1221. {
  1222. /*
  1223. if(Z(0)>0)
  1224. return 1;
  1225. else
  1226. return -1;
  1227. */
  1228. return Z(indentare,0);
  1229. }
  1230. else
  1231. return -1;
  1232. }
  1233. else
  1234. {
  1235. Eroare("lipseste indentare dupa instructiune");
  1236. return -1;
  1237. }
  1238. }
  1239. else
  1240. {
  1241. Eroare("lipseste trecerea la un rand nou dupa instructiune");
  1242. return -1;
  1243. }
  1244. }
  1245.  
  1246. int Z(int indentare, int obligatoriu)
  1247. {
  1248. //printf("%d %d\n",ultimaIndentare,indentare);
  1249. if(ultimaIndentare<indentare)
  1250. return 0;
  1251. return A(indentare,obligatoriu);
  1252. }
  1253.  
  1254. int en()
  1255. {
  1256. if(!strcmp(atom2,"-"))
  1257. {
  1258. urmatorulAtom();
  1259. treciPesteSpatiiOptionale();
  1260. }
  1261. if(e0()>0)
  1262. return 1;
  1263. else
  1264. {
  1265. Eroare("expresie incorecta");
  1266. return -1;
  1267. }
  1268. }
  1269.  
  1270. int e0()
  1271. {
  1272. if(e1()>0)
  1273. {
  1274. while(!strcmp(atom2,"+")||!strcmp(atom2,"-"))
  1275. {
  1276. urmatorulAtom();
  1277. treciPesteSpatiiOptionale();
  1278. if(e1()<0)
  1279. return -1;
  1280. }
  1281. return 1;
  1282. }
  1283. else
  1284. return -1;
  1285. }
  1286.  
  1287. int e1()
  1288. {
  1289. if(e2()>0)
  1290. {
  1291. while(!strcmp(atom2,"*")||!strcmp(atom2,"/")||!strcmp(atom2,"%"))
  1292. {
  1293. urmatorulAtom();
  1294. treciPesteSpatiiOptionale();
  1295. if(e2()<0)
  1296. return -1;
  1297. }
  1298. return 1;
  1299. }
  1300. else
  1301. return -1;
  1302. }
  1303.  
  1304. int e2()
  1305. {
  1306. if(!strcmp(atom2,"("))
  1307. {
  1308. urmatorulAtom();
  1309. treciPesteSpatiiOptionale();
  1310. if(en()>0)
  1311. {
  1312. if(!strcmp(atom2,")"))
  1313. {
  1314. urmatorulAtom();
  1315. //treciPesteSpatiiOptionale();
  1316. return 1;
  1317. }
  1318. else
  1319. {
  1320. Eroare("lipseste )");
  1321. return -1;
  1322. }
  1323. }
  1324. }
  1325. else if(strstr("nripo,nrine,nrrpo,nrrne",tipAtom2))
  1326. {
  1327. if(strstr("nrine,nrrne",tipAtom2)&&strstr("-,+",atom1))
  1328. {
  1329. Eroare("+ sau - nu pot aparea inaintea unui numar negativ");
  1330. return -1;
  1331. }
  1332. else
  1333. {
  1334. urmatorulAtom();
  1335. //treciPesteSpatiiOptionale();
  1336. return 1;
  1337. }
  1338. }
  1339. else if(E(0,0,1)>0)
  1340. {
  1341. if(strcmp(id[ultimulIdentificator].tip,"sirca"))
  1342. return 1;
  1343. else
  1344. {
  1345. char s[100];
  1346. sprintf(s,"variabila de tip sir de caractere '%s' nu poate aparea intr-o expresie numerica",id[ultimulIdentificator].nume);
  1347. Eroare(s);
  1348. return -1;
  1349. }
  1350. }
  1351. else if(!strcmp(tipAtom2,"sirca"))
  1352. {
  1353. Eroare("un numar nu poate fi adaugat unui sir de caractere");
  1354. return -1;
  1355. }
  1356. else
  1357. return -1;
  1358. }
  1359.  
  1360. int ec()
  1361. {
  1362. while(1)
  1363. {
  1364. if(E(0,0,1)>0&&!strcmp(id[ultimulIdentificator].tip,"numar"))
  1365. {
  1366. Eroare("un sir de caractere nu poate fi concatenat (+) cu un numar");
  1367. return -1;
  1368. }
  1369. if(!strcmp(tipAtom2,"ranou")||!strcmp(atom2,",")||!strcmp(atom2,"}"))
  1370. return 1;
  1371. if(strcmp(tipAtom2,"sirca")&&strcmp(atom2,"+"))
  1372. {
  1373. Eroare("un sir de caractere poate fi doar concatenat (+) cu un altul");
  1374. return -1;
  1375. }
  1376. urmatorulAtom();
  1377. treciPesteSpatiiOptionale();
  1378. }
  1379.  
  1380. }
  1381.  
  1382. int co()
  1383. {
  1384. if(!strcmp(tipAtom2,"ident"))
  1385. {
  1386. int p=pozitieIdentificator(atom2);
  1387. if(p>=0)
  1388. {
  1389. if(!strcmp(id[p].tip,"colec"))
  1390. return 1;
  1391. else
  1392. return 0;
  1393. }
  1394. else
  1395. {
  1396. char s[50];
  1397. sprintf(s,"variabila %s nu a fost declarata",id[p].nume);
  1398. Eroare(s);
  1399. return -1;
  1400. }
  1401. }
  1402. else if(!strcmp(atom2,"{"))
  1403. {
  1404. urmatorulAtom();
  1405. treciPesteSpatiiOptionale();
  1406.  
  1407. if(!strcmp(atom2,"}"))
  1408. {
  1409. urmatorulAtom();
  1410. treciPesteSpatiiOptionale();
  1411. return 1;
  1412. }
  1413.  
  1414. if(I()>0)
  1415. {
  1416. if(coa()>=0)
  1417. if(!strcmp(atom2,"}"))
  1418. {
  1419. urmatorulAtom();
  1420. treciPesteSpatiiOptionale();
  1421. return 1;
  1422. }
  1423. else
  1424. {
  1425. Eroare("lipseste }");
  1426. return -1;
  1427. }
  1428. }
  1429.  
  1430. return -1;
  1431. }
  1432. else
  1433. return 0;
  1434. }
  1435.  
  1436. int coa()
  1437. {
  1438. if(!strcmp(atom2,","))
  1439. {
  1440. urmatorulAtom();
  1441. treciPesteSpatiiOptionale();
  1442.  
  1443. if(I()>0)
  1444. {
  1445. //urmatorulAtom();
  1446. treciPesteSpatiiOptionale();
  1447. if(coa()>=0)
  1448. return 1;
  1449. }
  1450. else
  1451. {
  1452. Eroare("lipseste un element dupa , ");
  1453. return -1;
  1454. }
  1455. }
  1456. else
  1457. return 0;
  1458. }
  1459.  
  1460. int in()
  1461. {
  1462. if(!strcmp(atom2,"[")||!strcmp(atom2,"("))
  1463. {
  1464. urmatorulAtom();
  1465. treciPesteSpatiiOptionale();
  1466. if(en()>0)
  1467. {
  1468. if(!strcmp(atom2,","))
  1469. {
  1470. urmatorulAtom();
  1471. treciPesteSpatiiOptionale();
  1472. if(en()>0)
  1473. {
  1474. if(!strcmp(atom2,"]")||!strcmp(atom2,")"))
  1475. return 1;
  1476. else
  1477. {
  1478. Eroare("lipseste inchiderea intervalului");
  1479. return -1;
  1480. }
  1481. }
  1482. else
  1483. {
  1484. Eroare("al doilea capat al unui interval trebuie sa fie expresie numerica");
  1485. return -1;
  1486. }
  1487. }
  1488. else
  1489. {
  1490. Eroare("capetele unui interval trebuie sa fie separate prin , ");
  1491. return -1;
  1492. }
  1493. }
  1494. else
  1495. {
  1496. Eroare("primul capat al unui interval trebuie sa fie expresie numerica");
  1497. return -1;
  1498. }
  1499. }
  1500. else
  1501. return 0;
  1502. }
  1503.  
  1504. //cod.txt
  1505. Preia n
  1506. Parcurge i din [0,n) cu pas 2
  1507. Preia v(i)
  1508. Fie nr=0, s=0
  1509. Parcurge x din v
  1510. Repeta daca x!=0
  1511. Fie s=s+x%10
  1512. Fie x=x/10
  1513. Altfel
  1514. Fie nr=nr+1
  1515. Fie m=s/nr
  1516. Afiseaza "Media este ", m
  1517.  
  1518. //cuvinteCheie.txt
  1519. Preia
  1520. sir
  1521. Afiseaza
  1522. Fie
  1523. Daca
  1524. Repeta
  1525. daca
  1526. Altfel
  1527. nu
  1528. si
  1529. sau
  1530. Parcurge
  1531. din
  1532. cu
  1533. pas
  1534.  
  1535. //Productii
  1536. A -> B | D | G | K | L | V
  1537. B -> Preia_EC
  1538. C -> , EC | \n Z
  1539. D -> Afiseaza_EC
  1540. E -> idnum | sir idsir
  1541. F -> , HF | \n Z
  1542. G -> Fie HF
  1543. H -> idnum=I
  1544. I -> expnum | expcar | colectie
  1545. K -> Daca_S
  1546. L -> Repeta_daca_S
  1547. M -> nu_N | N
  1548. N -> O_si_N | O
  1549. O -> P_sau_O | P
  1550. P -> Q | (N)
  1551. Q -> idnumsir rel R
  1552. R -> idnumsir | nr | sir
  1553. S -> M Y U
  1554. T -> tab Z T | λ
  1555. U -> Altfel Y | λ
  1556. V -> Parcurge_idnum_din_W Y
  1557. W -> intervalX | idsir | idcol
  1558. X -> _cu_pas_expnum | λ
  1559. Y -> \n tab Z T
  1560. Z -> A | λ
  1561.  
  1562. id -> litera(litera)*(cifra)*
  1563. idnum -> id | idcol(nr)
  1564. idsir -> id | idsir(nr) | idcol(nr)
  1565. idcol -> id | idcol(nr)
  1566. nr -> -nrp | nrp
  1567. nrp -> nrip(.(cifra)*|λ)
  1568. nrip -> cifra(cifra)*
  1569. rel -> = | != | < | <= | > | >=
  1570. tab -> 4x_
  1571. e -> (- | λ) e0
  1572. e0 -> e1 ((+ | -) e1)*
  1573. e1 -> e2 ((* | / | %) e2)*
  1574. e2 -> (e0) | nr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement