opurag

CDSS

Jun 19th, 2024
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1. ------------------------------------------------------------------------------------------------------------------------
  2. //1
  3. //1a:Program to count the number, words, spaces and lines in a given input file
  4.  
  5. %{
  6. #include<stdio.h>
  7. int c=0;
  8. int w=0;
  9. int s=0;
  10. int l=0;
  11. %}
  12. %%
  13. " "{ s++; w++;}
  14. [\n]{l++;w++;}
  15. [\t\n]{w++;}
  16. [^\t\n]{c++;}
  17. %%
  18. void yywrap()
  19. {
  20. return 1;
  21. }
  22. void main()
  23. {
  24. yyin=fopen("Info.txt", "r");
  25. yylex();
  26. printf("Characters = %d\nWords = %d\nSpaces = %d\nLines= %d\n",c,w,s,l);
  27. return 0;
  28. }
  29.  
  30. //op
  31. lex 1a.l
  32. cc lex.yy.c -ll
  33. ./a.out
  34.  
  35. //1b:Program to recognize and count the number of identifiers in a file
  36.  
  37. %{
  38. #include<stdio.h>
  39. int i=0;
  40. %}
  41. digit [0-9]
  42. letter [a-z A-Z_]
  43. %%
  44. {letter}({letter}|{digit})*{i++;}
  45. {digit}({letter}|{digit})*{i;}
  46. %%
  47. void main()
  48. {
  49. printf("Enter the values:\n");
  50.  
  51. yylex();
  52. printf("Number of identifiers = %d\n", i);
  53. return 0;
  54. }
  55.  
  56. //op
  57. lex 1b.l
  58. cc lex.yy.c -ll
  59. ./a.out
  60.  
  61. // ctrl+d to end loop
  62. ------------------------------------------------------------------------------------------------------------------------
  63. //2
  64. //2a:Programs to count the numbers of comments lines in a given C program. Also eliminate them and copy the resulting program into separate file.
  65.  
  66. %{
  67. #include<stdio.h>
  68. int ml=0;
  69. int sl=0;
  70. %}
  71.  
  72. %%
  73.  
  74. "/*"[a-z A-Z 0-9 ' '\t \n]*"*/"ml++;
  75. "//".*sl++;
  76.  
  77. %%
  78. void main()
  79. {
  80. yyin=fopen("f1.txt","r");
  81. yyout=fopen("f2.txt","w");
  82. yylex();
  83. fclose(yyin);
  84. fclose(yyout);
  85. printf("Number of single line comments =%d\n",sl);
  86. printf("Number of multi line comments =%d\n",ml);
  87. }
  88.  
  89. //op
  90. gedit f1.txt
  91. lex 2a.l
  92. cc lex.yy.c -ll
  93. cat f2.txt
  94.  
  95.  
  96. //2b: Program to recognize whether a given sentence is simple or compound.
  97.  
  98. %{
  99. #include<stdio.h>
  100. int c=0;
  101. %}
  102.  
  103. %%
  104. [a-zA-Z]*[ ](and|or|but|yet|so)[ ][a-zA-Z]*{c=1;}
  105. .|[\n];
  106.  
  107. %%
  108. int yywrap()
  109. {
  110. return 1;
  111. }
  112.  
  113. int main()
  114. {
  115. printf("enter the text\n");
  116. yylex();
  117. if(c)
  118. {
  119. printf("The given statement is compound\n");
  120. }
  121. else
  122. {
  123. printf("The given statement is simple\n");
  124. }
  125. }
  126.  
  127.  
  128. //op
  129. lex 2b.l
  130. cc lex.yy.c -ll
  131. ./a.out
  132.  
  133. ------------------------------------------------------------------------------------------------------------------------
  134. //3
  135. //3a: Program to count number of:
  136. // i.+ve and -ve integers
  137. // ii. +ve and -ve fractions
  138.  
  139. %{
  140. #include<stdio.h>
  141. int pi=0,ni=0,pf=0,nf=0;
  142. %}
  143. %%
  144. [-][0-9]+{ni++;}
  145. [+]?[0-9]+{pi++;}
  146. [-][0-9]*\.[0-9]+{nf++;}
  147. [+]?[0-9]*\.[0-9]+{pf++;}
  148. %%
  149.  
  150. void main(int argc,char *argv[])
  151. {
  152. if(argv!=2)
  153. {
  154. printf("usage :./a.out in.txt \n");
  155. exit(0);
  156. }
  157. yyin=fopen(argc[1],"r");
  158. yylex();
  159. printf("Number of positive integer %d\n",pi);
  160. printf("Number of negative integer %d\n",ni);
  161. printf("Number of positive fraction %d\n",pf);
  162. printf("Number of negative fraction %d\n",nf);
  163. }
  164. int yywrap(){
  165. return 1;
  166. }
  167.  
  168. //in.txt
  169. 12
  170. -29
  171. 0
  172. +8
  173. -0.9
  174.  
  175. //op
  176. lex 3a.l
  177. cc lex.yy.c -ll
  178. ./a.out in.txt
  179.  
  180.  
  181. //3b: Program to count the number of “scanf” and “printf” statements in a C program. Replace them with “readf” and “writef” statements respectively.
  182.  
  183. %{
  184. #include<stdio.h>
  185. int sf=0,pf=0;
  186. %}
  187. %%
  188. "scanf"{sf++; fprintf(yyout,"readf");}
  189. "printf"{pf++; fprintf(yyout,"writef");}
  190. %%
  191.  
  192. void main()
  193. {
  194. yyin=fopen("f1.c","r");
  195. yyout=fopen("f2.c","w");
  196. yylex();
  197. printf("no of scanf =%d\n no of printf =%d\n",sf,pf);
  198. return 0;
  199. }
  200.  
  201. //op
  202. gedit f1.c
  203. lex 3b.l
  204. cc lex.yy.c -ll
  205. ./a.out
  206. cat f2.c
  207.  
  208. ------------------------------------------------------------------------------------------------------------------------
  209. //4: Program to evaluate arithmetic expression involving operators +, -, *, /
  210. //4.l
  211. %{
  212. #include "y.tab.h"
  213. extern yylval;
  214. %}
  215. %%
  216. [0-9]+{yylval=atoi(yytext);return num;}
  217. [\+\-\*\/]{return yytext[0];}
  218. [)]{return yytext[0];}
  219. [(]{return yytext[0];}
  220. .{;}
  221. \n{return 0;}
  222. %%
  223.  
  224. //4.y
  225. %{
  226. #include<stdio.h>
  227. #include<stdlib.h>
  228. %}
  229. %name parse
  230. %token num
  231. %left'+''-'
  232. %left'*''/'
  233. %%
  234. input:exp {printf("%d\n",$$);exit(0);}
  235. exp:exp'+'exp {$$=$1+$3;}
  236. |exp'-'exp {$$=$1-$3;}
  237. |exp'*'exp {$$=$1*$3;}
  238. |exp'/'exp{ if($3==0){printf("Divide by zero error\n ");
  239. exit(0);}
  240. else
  241. $$=$1/$3;}
  242. |'('exp')'{$$=$2;}
  243. |num{$$=$1;};
  244. %%
  245. int yyerror()
  246. {
  247. printf("error");
  248. exit(0);
  249. }
  250. int main()
  251. {
  252. printf("Enter an expression:\n");
  253. yyparse();
  254. }
  255.  
  256.  
  257. //op
  258. lex 4.l
  259. yacc -d 4.y
  260. cc lex.yy.c y.tab.c -lfl
  261. ./a.out
  262.  
  263. ------------------------------------------------------------------------------------------------------------------------
  264. //5: Program to recognize a valid variable which starts with a letter, followed by any number of letter or digits
  265. //p5.l
  266. %{
  267. #include "y.tab.h"
  268. %}
  269. %%
  270. [a-zA-z] return L;
  271. [0-9] return D;
  272.  
  273. %%
  274.  
  275. //p5.y
  276. %{
  277. #include<stdio.h>
  278. #include<stdlib.h>
  279. %}
  280. %name parse
  281. %token L D
  282.  
  283. %%
  284. var:L E {printf("Valid Variable\n"); return 0;}
  285. E:E L;
  286. |E D;
  287. %%
  288.  
  289. int main()
  290. {
  291. printf("Type the variable\n");
  292. yyparse();
  293. return 0;
  294. }
  295. int yyerror()
  296. {
  297. printf("Invalid Variable\n");
  298. exit(0);
  299. }
  300.  
  301. //op
  302. lex p5.l
  303. yacc -d p5.y
  304. cc lex.yy.c y.tab.c -lfl
  305. ./a.out
  306.  
  307. ------------------------------------------------------------------------------------------------------------------------
  308. //6: Program to recognize the strings using the grammar (a^nb^n ; n>=0)
  309. //p6.l
  310. %{
  311. #include "y.tab.h"
  312. %}
  313. %%
  314. a return A;
  315. b return B;
  316. . return yytext[0];
  317. \n return yytext[0];
  318. %%
  319.  
  320. //p6.y
  321. %{
  322. #include<stdio.h>
  323. #include<stdlib.h>
  324. %}
  325. %name parse
  326. %token A B
  327.  
  328. %%
  329. Str:S '\n'{return 0;}
  330. S:A S B;
  331.  
  332. | ;
  333. %%
  334.  
  335. int main()
  336. {
  337. printf("Type the string\n");
  338. if (!yyparse())
  339. printf("Valid String\n");
  340. return 0;
  341. }
  342. int yyerror()
  343. {
  344. printf("Invalid String\n");
  345. exit(0);
  346. }
  347.  
  348. //op
  349. lex p6.l
  350. yacc -d p6.y
  351. cc lex.yy.c y.tab.c -lfl
  352. ./a.out
  353. ------------------------------------------------------------------------------------------------------------------------
  354.  
  355. //7: C program to implement Pass1 of Assembler
  356.  
  357. #include <stdio.h>
  358. #include <string.h>
  359. void main() {
  360. char opcode[10], operand[10], label[10], code[10], mnemonic[3];
  361. int locctr, start, length;
  362.  
  363. FILE *fp1, *fp2, *fp3, *fp4;
  364.  
  365. fp1 = fopen("ip.txt", "r");
  366. fp2 = fopen("optab.txt", "r");
  367. fp3 = fopen("symtabl.txt", "w");
  368. fp4 = fopen("out.txt", "w");
  369.  
  370. fscanf(fp1, "%s\t%s\t%s", label, opcode, operand);
  371.  
  372. if (strcmp(opcode, "START") == 0) {
  373. start = atoi(operand);
  374. locctr = start;
  375. fprintf(fp4, "\t%s\t%s\t%s\n", label, opcode, operand);
  376. fscanf(fp1, "%s\t%s\t%s", label, opcode, operand);
  377. } else
  378. locctr = 0;
  379.  
  380. while (strcmp(opcode, "END") != 0) {
  381. fprintf(fp4, "%d\t", locctr);
  382.  
  383. if (strcmp(label, "**") != 0) fprintf(fp3, "%s\t%d\n", label, locctr);
  384. fscanf(fp2, "%s\t%s", code, mnemonic);
  385.  
  386. while (strcmp(code, "END") != 0) {
  387. if (strcmp(opcode, code) == 0) {
  388. locctr += 3;
  389. break;
  390. }
  391. fscanf(fp2, "%s\t%s", code, mnemonic);
  392. }
  393. if (strcmp(opcode, "WORD") == 0)
  394. locctr += 3;
  395. else if (strcmp(opcode, "RESB") == 0)
  396. locctr += atoi(operand);
  397. else if (strcmp(opcode, "BYTE") == 0)
  398. ++locctr;
  399. fprintf(fp4, "%s\t%s\t%s\t\n", label, opcode, operand);
  400. fscanf(fp1, "%s\t%s\t%s", label, opcode, operand);
  401. }
  402. fprintf(fp4, "%d\t%s\t%s\t%s\n", locctr, label, opcode, operand);
  403. length = locctr - start;
  404. printf("The length of the code:%d\n", length);
  405.  
  406. fclose(fp1);
  407. fclose(fp2);
  408.  
  409. fclose(fp3);
  410. fclose(fp4);
  411. }
  412.  
  413. //1.Input.txt
  414. ** START 2000
  415. ** LDA FIVE
  416. ** STA ALPHA
  417. ** LDCH CHARZ
  418. ** STCH C1
  419. ALPHA RESW 2
  420. FIVE WORD 5
  421. CHARZ BYTE C'Z'
  422. C1 RESB 1
  423. ** END **
  424.  
  425.  
  426. //op
  427. gcc p7.c
  428. ./a.out
  429. cat Symtabl.txt
  430. cat Out.txt
  431. ------------------------------------------------------------------------------------------------------------------------
  432.  
  433. //8: C program to implement Absolute loader.
  434.  
  435. #include<stdio.h>
  436. #include<stdlib.h>
  437. #include<string.h>
  438. void main() {
  439. FILE fp;
  440. int i,l, j, staddrl;
  441. char name[10], line[50], namel[10], staddr[10];
  442. printf("enter program name: ");
  443. scanf("%s", name);
  444. fp = fopen("abssrc.txt", "r");
  445. fscanf(fp, "%s", line);
  446. for(i=2, j=0; i<8, j<6; i++, j++) {
  447. namel[j] = line[i];
  448. }
  449. namel[i] = '\0';
  450. printf("name from obj: %s\n", namel);
  451. if(strcmp(name,namel) == 0) {
  452. do {
  453. fscanf(fp, "%s", line);
  454. if(line[0] == 'T') {
  455. for(i=2, j=0; i<8, j<6; i++, j++) {
  456. staddr[j] = line[i];
  457. }
  458. staddr[j] = '\0';
  459. staddrl = atoi(staddr);
  460. i=12;
  461. while(line[i] != '$') {
  462. if(line[i] != '^') {
  463. printf("00%d\t%c%c\n", staddrl, line[i], line[i+1]);
  464. staddrl++;
  465. i = i+2;
  466. } else {
  467. i++;
  468. }
  469. }
  470. } else if (line[0] == 'E') {
  471. break;
  472.  
  473. }
  474. }while(!feof(fp));
  475. fclose(fp);
  476. }
  477. }
  478.  
  479.  
  480. //abssrc.txt
  481. H^00COPY^001000
  482. T^001000^3C^001011^10121A^11222B$
  483. T^002000^2E^101B2A^0C123A$
  484. E^001000
  485. (or)
  486. H^SAMPLE^001000^0035
  487. T^001000^0C^001003^071009$
  488. T^002000^03^111111$
  489. E^001000
  490.  
  491. //op
  492. ./a.out
  493. 00COPY (or) SAMPLE
  494.  
  495. ------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment