Advertisement
Guest User

horga

a guest
Nov 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX_TEXT_LEN 1000
  4. enum{
  5. ID,
  6. VAL_INT,
  7. VAL_REAL,
  8. VAL_STR,
  9. VAR,FUNCTION,IF,ELSE,WHILE,END,RETURN,INT,REAL,STR,COLON,SEMICOLON,
  10. LPAR,RPAR,COMMA,OR,AND,NOT,EQUAL,NOTEQUAL,LESS,ASSIGN,ADD,SUB,MUL,DIV,FINISH};
  11. typedef struct{
  12. int tipat;
  13. union{
  14. char text[MAX_TEXT_LEN];
  15. int nrInt;
  16. double nrReal;
  17. };
  18. }Atom;
  19.  
  20. int nAtoms=0;
  21. Atom atom[1000];
  22. FILE *fis;
  23.  
  24.  
  25. void err(char* e){
  26. printf("Eroare: %s ",e);
  27. exit(0);
  28. }
  29. void adaugaAtom(int tip){
  30. atom[nAtoms].tipat=tip;
  31. nAtoms++;
  32. }
  33.  
  34. int lexer()
  35. {
  36. char buf[MAX_TEXT_LEN];
  37. int n=0;
  38. int state=0;
  39. int ch;
  40. int nrInt;
  41. double nrReal;
  42. for(;;)
  43. {
  44. if(state!=2 && state!=4 && state!=7 && state!=9 && state!=10 && state!=11 && state!=12 && state!=13 && state!=14 && state!=16 && state!=18 && state!=20 && state!=21 && state!=23 && state!=24 && state!=25 && state!=26 && state!=27 && state!=28 && state!=29 && state!=30)
  45. {
  46. ch=fgetc(fis);
  47. }
  48. // printf("# %d,%c(%d)\n",state,ch,ch);
  49. switch(state)
  50. {
  51. case 0:
  52. if(isalpha(ch)||ch=='_')
  53. {
  54. state=1;
  55. buf[n++]=ch;
  56. }
  57. else if(isdigit(ch))
  58. {
  59. state=3;
  60. buf[n++]=ch;
  61. }
  62. else if(ch=='"')
  63. {
  64. state=8;
  65. }
  66. else if(ch==':')
  67. {
  68. state=10;
  69. }
  70. else if(ch==';')
  71. {
  72. state=11;
  73. }
  74. else if(ch=='(')
  75. {
  76. state=12;
  77. }
  78. else if(ch==')')
  79. {
  80. state=13;
  81. }
  82. else if(ch==',')
  83. {
  84. state=14;
  85. }
  86. else if(ch=='|')
  87. {
  88. state=15;
  89. }
  90. else if(ch=='&')
  91. {
  92. state=17;
  93. }
  94. else if(ch=='!')
  95. {
  96. state=19;
  97. }
  98. else if(ch=='=')
  99. {
  100. state=22;
  101. }
  102. else if(ch=='<')
  103. {
  104. state=25;
  105. }
  106. else if(ch=='+')
  107. {
  108. state=26;
  109. }
  110. else if(ch=='-'){
  111. state=27;
  112. }
  113. else if(ch=='*')
  114. {
  115. state=28;
  116. }
  117. else if(ch=='/')
  118. {
  119. state=29;
  120. }
  121.  
  122. else if(ch==' '|| ch=='\n'|| ch=='\t'||ch=='\r')
  123. {
  124. state=0;
  125. }
  126.  
  127. else if(ch==EOF)
  128. {
  129. state=30;
  130. return FINISH;
  131. }
  132. else
  133. {
  134. err("Caracter necunoscut!");
  135. }
  136. break;
  137. case 1:
  138. if(isalnum(ch)||ch=='_')
  139. {
  140. buf[n++]=ch;
  141. }
  142. else
  143. {
  144. ungetc(ch,fis);
  145. state=2;
  146. }
  147. break;
  148. case 2:
  149. buf[n]='\0';
  150. if(!strcmp(buf,"var")){
  151. adaugaAtom(VAR);
  152. return VAR;
  153. }
  154. else if(!strcmp(buf,"function")){
  155. adaugaAtom(FUNCTION);
  156. return FUNCTION;
  157. }
  158. else if(!strcmp(buf,"if")){
  159. adaugaAtom(IF);
  160. return IF;
  161. }
  162. else if(!strcmp(buf,"else")){
  163. adaugaAtom(ELSE);
  164. return ELSE;
  165. }
  166. else if(!strcmp(buf,"while")){
  167. adaugaAtom(WHILE);
  168. return WHILE;
  169. }
  170. else if(!strcmp(buf,"end")){
  171. adaugaAtom(END);
  172. return END;
  173. }
  174. else if(!strcmp(buf,"return")){
  175. adaugaAtom(RETURN);
  176. return RETURN;
  177. }
  178. else if(!strcmp(buf,"int")){
  179. adaugaAtom(INT);
  180. return INT;
  181. }
  182. else if(!strcmp(buf,"real")){
  183. adaugaAtom(REAL);
  184. return REAL;
  185. }
  186. else if(!strcmp(buf,"str")){
  187. adaugaAtom(STR);
  188. return STR;
  189. }
  190. else{
  191. //daca nu e cuvant cheie,este ID
  192. adaugaAtom(ID);
  193. strcpy(atom[nAtoms-1].text,buf);
  194. return ID;
  195. }
  196.  
  197. case 3:
  198. if(isdigit(ch)) {
  199. buf[n++]=ch;
  200. }
  201. else if( ch=='.')
  202. {
  203. state=5;
  204. buf[n++]=ch;
  205. }
  206. else
  207. {
  208. ungetc(ch,fis);
  209. state=4;
  210. }
  211. break;
  212. case 4:
  213. buf[n]='\0';
  214. adaugaAtom(VAL_INT);
  215. atom[nAtoms-1].nrInt=atoi(buf);
  216. return VAL_INT;
  217. break;
  218. case 5:
  219. if(isdigit(ch)){
  220. state=6;
  221. buf[n++]=ch;
  222. }
  223. else{
  224. err("Lipsesc zecimalele(cifrele de dupa punct)");
  225. }
  226. break;
  227. case 6:
  228. if(isdigit(ch)){
  229. buf[n++]=ch;
  230. }
  231. else{
  232. ungetc(ch,fis);
  233. state=7;
  234. }
  235. break;
  236. case 7:
  237. buf[n]='\0';
  238. adaugaAtom(VAL_REAL);
  239. atom[nAtoms-1].nrReal=atof(buf);
  240. return VAL_REAL;
  241. break;
  242. case 8:
  243. if(ch!='"'){
  244. buf[n++]=ch;
  245. }
  246. else{
  247. state=9;
  248. }
  249. break;
  250. case 9:
  251. buf[n]='\0';
  252. adaugaAtom(VAL_STR);
  253. strcpy(atom[nAtoms-1].text,buf);
  254. return VAL_STR;
  255. break;
  256. case 10:
  257. adaugaAtom(COLON);
  258. return COLON;
  259. break;
  260. case 11:
  261. adaugaAtom(SEMICOLON);
  262. return SEMICOLON;
  263. break;
  264. case 12:
  265. adaugaAtom(LPAR);
  266. return LPAR;
  267. break;
  268. case 13:
  269. adaugaAtom(RPAR);
  270. return RPAR;
  271. break;
  272. case 14:
  273. adaugaAtom(COMMA);
  274. return COMMA;
  275. break;
  276. case 15:
  277. if(ch=='|'){
  278. state=16;
  279. buf[n++]=ch;
  280. }
  281. break;
  282. case 16:
  283. buf[n]='\0';
  284. adaugaAtom(OR);
  285. strcpy(atom[nAtoms-1].text,buf);
  286. return OR;
  287. break;
  288. case 17:
  289. if(ch=='&'){
  290. state=18;
  291. buf[n++]=ch;
  292. }
  293. break;
  294. case 18:
  295. buf[n]='\0';
  296. adaugaAtom(AND);
  297. strcpy(atom[nAtoms-1].text,buf);
  298. return AND;
  299. break;
  300. case 19:
  301. if(ch=='='){
  302. state=21;
  303. //buf[n++]=ch;
  304. }
  305. else{
  306. ungetc(ch,fis);
  307. adaugaAtom(NOT);
  308. return NOT;
  309. }
  310. break;
  311.  
  312. case 21:
  313. buf[n]='\0';
  314. adaugaAtom(NOTEQUAL);
  315. strcpy(atom[nAtoms-1].text,buf);
  316. return NOTEQUAL;
  317. break;
  318. case 22:
  319. buf[n]='\0';
  320. state=24;
  321. if(ch=='='){
  322. state=23;
  323. buf[n++]=ch;
  324. }
  325. break;
  326. case 24:
  327. buf[n]='\0';
  328. adaugaAtom(ASSIGN);
  329. return ASSIGN;
  330. break;
  331. case 23:
  332. buf[n]='\0';
  333. adaugaAtom(EQUAL);
  334. strcpy(atom[nAtoms-1].text,buf);
  335. return EQUAL;
  336. break;
  337. case 25:
  338. adaugaAtom(LESS);
  339. return LESS;
  340. break;
  341. case 26:
  342. adaugaAtom(ADD);
  343. return ADD;
  344. break;
  345. case 27:
  346. adaugaAtom(SUB);
  347. return SUB;
  348. break;
  349. case 28:
  350. adaugaAtom(MUL);
  351. return MUL;
  352. break;
  353. case 29:
  354. adaugaAtom(DIV);
  355. return DIV;
  356. break;
  357. case 30:
  358. adaugaAtom(FINISH);
  359. return FINISH;
  360. break;
  361.  
  362. default:
  363. printf("Stare neimplementata: %d", state);
  364. }
  365. }
  366. }
  367.  
  368. char* afis_nume(int n){
  369. switch(n){
  370. case 0:
  371. return "ID";
  372. case 1:
  373. return "VAL_INT";
  374. case 2:
  375. return "VAL_REAL";
  376. case 3:
  377. return "VAL_STR";
  378. case 4:
  379. return "VAR";
  380. case 5:
  381. return "FUNCTION";
  382. case 6:
  383. return "IF";
  384. case 7:
  385. return "ELSE";
  386. case 8:
  387. return "WHILE";
  388. case 9:
  389. return "END";
  390. case 10:
  391. return "RETURN";
  392. case 11:
  393. return "INT";
  394. case 12:
  395. return "REAL";
  396. case 13:
  397. return "STR";
  398. case 14:
  399. return "COLON";
  400. case 15:
  401. return "SEMICOLON";
  402. case 16:
  403. return "LPAR";
  404. case 17:
  405. return "RPAR";
  406. case 18:
  407. return "COMMA";
  408. case 19:
  409. return "OR";
  410. case 20:
  411. return "AND";
  412. case 21:
  413. return "NOT";
  414. case 22:
  415. return "EQUAL";
  416. case 23:
  417. return "NOTEQUAL";
  418. case 24:
  419. return "LESS";
  420. case 25:
  421. return "ASSIGN";
  422. case 26:
  423. return "ADD";
  424. case 27:
  425. return "SUB";
  426. case 28:
  427. return "MUL";
  428. case 29:
  429. return "DIV";
  430. case 30:
  431. return "FINISH";
  432. default: return "Eroare !";
  433. }
  434. }
  435.  
  436.  
  437. int main()
  438. {
  439.  
  440. if((fis=fopen("program.q","r"))==NULL)
  441. {
  442. printf("Eroare la deschidere!");
  443. return -1;
  444. }
  445.  
  446. while(lexer()!=FINISH) {
  447. // afisare atomi
  448. if(atom[nAtoms-1].tipat==1){
  449. printf("%s %d \n",afis_nume(atom[nAtoms-1].tipat),atom[nAtoms-1].nrInt);
  450. }
  451. else if(atom[nAtoms-1].tipat==2){
  452. printf("%s %f \n",afis_nume(atom[nAtoms-1].tipat),atom[nAtoms-1].nrReal);
  453. }
  454. else if(atom[nAtoms-1].tipat==3){
  455. printf("%s %s\n",afis_nume(atom[nAtoms-1].tipat), atom[nAtoms-1].text);
  456. }
  457. else
  458. printf("%s %s\n",afis_nume(atom[nAtoms-1].tipat), atom[nAtoms-1].text);
  459.  
  460.  
  461. }
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470. printf("FINISH");
  471. fclose(fis);
  472. return 0;
  473. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement