opurag

SSDC

May 1st, 2024 (edited)
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.33 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. int yywrap()
  19. {
  20. return 1;
  21. }
  22. int 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. int 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. int 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. void 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(argc!=2)
  153. {
  154. printf("usage :./a.out in.txt \n");
  155. exit(0);
  156. }
  157. yyin=fopen(argv[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. int 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 ");exit(0);}
  239. else
  240. $$=$1/$3;}
  241. |'('exp')'{$$=$2;}
  242. |num{$$=$1;};
  243. %%
  244. int yyerror()
  245. {
  246. printf("error");
  247. exit(0);
  248. }
  249. int main()
  250. {
  251. printf("Enter an expression:\n");
  252. yyparse();
  253. }
  254.  
  255.  
  256. //op
  257. lex 4.l
  258. yacc -d 4.y
  259. cc lex.yy.c y.tab.c -lfl
  260. ./a.out
  261.  
  262. ------------------------------------------------------------------------------------------------------------------------
  263. //5: Program to recognize a valid variable which starts with a letter, followed by any number of letter or digits
  264. //p5.l
  265. %{
  266. #include "y.tab.h"
  267. %}
  268. %%
  269. [a-zA-z] return L;
  270. [0-9] return D;
  271.  
  272. %%
  273.  
  274. //p5.y
  275. %{
  276. #include<stdio.h>
  277. #include<stdlib.h>
  278. %}
  279. %name parse
  280. %token L D
  281.  
  282. %%
  283. var:L E {printf("Valid Variable\n"); return 0;}
  284. E:E L;
  285. |E D;
  286. | ;
  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 <stdlib.h>
  359. #include <string.h>
  360. void main() {
  361.   char opcode[10], operand[10], label[10], code[10], mnemonic[3];
  362.   int locctr, start, length;
  363.  
  364.   FILE *fp1, *fp2, *fp3, *fp4;
  365.  
  366.   fp1 = fopen("input.txt", "r");
  367.   fp2 = fopen("optab.txt", "r");
  368.   fp3 = fopen("symtabl.txt", "w");
  369.   fp4 = fopen("out.txt", "w");
  370.  
  371.   fscanf(fp1, "%s\t%s\t%s", label, opcode, operand);
  372.  
  373.   if (strcmp(opcode, "START") == 0) {
  374.     start = atoi(operand);
  375.     locctr = start;
  376.     fprintf(fp4, "\t%s\t%s\t%s\n", label, opcode, operand);
  377.     fscanf(fp1, "%s\t%s\t%s", label, opcode, operand);
  378.   } else
  379.     locctr = 0;
  380.  
  381.   while (strcmp(opcode, "END") != 0) {
  382.     fprintf(fp4, "%d\t", locctr);
  383.  
  384.     if (strcmp(label, "**") != 0) fprintf(fp3, "%s\t%d\n", label, locctr);
  385.     fscanf(fp2, "%s\t%s", code, mnemonic);
  386.  
  387.     while (strcmp(code, "END") != 0) {
  388.       if (strcmp(opcode, code) == 0) {
  389.         locctr += 3;
  390.         break;
  391.       }
  392.       fscanf(fp2, "%s\t%s", code, mnemonic);
  393.     }
  394.     if (strcmp(opcode, "WORD") == 0)
  395.       locctr += 3;
  396.     else if (strcmp(opcode, "RESW") == 0)
  397.       locctr += (3 * (atoi(operand)));
  398.     else if (strcmp(opcode, "RESB") == 0)
  399.       locctr += atoi(operand);
  400.     else if (strcmp(opcode, "BYTE") == 0)
  401.       ++locctr;
  402.     fprintf(fp4, "%s\t%s\t%s\t\n", label, opcode, operand);
  403.     fscanf(fp1, "%s\t%s\t%s", label, opcode, operand);
  404.   }
  405.   fprintf(fp4, "%d\t%s\t%s\t%s\n", locctr, label, opcode, operand);
  406.   length = locctr - start;
  407.   printf("The length of the code:%d\n", length);
  408.  
  409.   fclose(fp1);
  410.   fclose(fp2);
  411.  
  412.   fclose(fp3);
  413.   fclose(fp4);
  414. }
  415.  
  416. //1.Input.txt
  417. **  START   2000
  418. **  LDA FIVE
  419. **  STA ALPHA
  420. **  LDCH    CHARZ
  421. **  STCH    C1
  422. ALPHA   RESW    2
  423. FIVE    WORD    5
  424. CHARZ   BYTE    C'Z'
  425. C1  RESB    1
  426. **  END **
  427.  
  428. //2.Optab.txt
  429. START   *
  430. LDA 03
  431. STA 0f
  432. LDCH    53
  433. STCH    57
  434. END *
  435.  
  436. //op
  437. gcc p7.c
  438. ./a.out
  439. cat Symtabl.txt
  440. cat Out.txt
  441. ------------------------------------------------------------------------------------------------------------------------
  442.  
  443. //8: C program to implement Absolute loader.
  444.  
  445. #include<stdio.h>
  446. #include<stdlib.h>
  447. #include<string.h>
  448. void main() {
  449.     FILE *fp;
  450.     int i,l, j, staddrl;
  451.     char name[10], line[50], namel[10], staddr[10];
  452.     printf("enter program name: ");
  453.     scanf("%s", name);
  454.     fp = fopen("abssrc.txt", "r");
  455.     fscanf(fp, "%s", line);
  456.     for(i=2, j=0; i<8, j<6; i++, j++) {
  457.         namel[j] = line[i];
  458.     }
  459.     namel[i] = '\0';
  460.     printf("name from obj: %s\n", namel);
  461.     if(strcmp(name,namel) == 0) {
  462.         do {
  463.             fscanf(fp, "%s", line);
  464.             if(line[0] == 'T') {
  465.                 for(i=2, j=0; i<8, j<6; i++, j++) {
  466.                     staddr[j] = line[i];
  467.                 }
  468.                 staddr[j] = '\0';
  469.                 staddrl = atoi(staddr);
  470.                 i=12;
  471.                 while(line[i] != '$') {
  472.                     if(line[i] != '^') {
  473.                         printf("00%d\t%c%c\n", staddrl, line[i], line[i+1]);
  474.                         staddrl++;
  475.                         i = i+2;
  476.                     } else {
  477.                         i++;
  478.                     }
  479.                 }
  480.             } else if (line[0] == 'E') {
  481.                 break;
  482.  
  483.             }
  484.         }while(!feof(fp));
  485.         fclose(fp);
  486.     }
  487. }
  488.  
  489.  
  490.  //abssrc.txt
  491. H^00COPY^001000
  492. T^001000^3C^001011^10121A^11222B$
  493. T^002000^2E^101B2A^0C123A$
  494. E^001000
  495. (or)
  496. H^SAMPLE^001000^0035
  497. T^001000^0C^001003^071009$
  498. T^002000^03^111111$
  499. E^001000
  500.  
  501. //op
  502. ./a.out
  503. 00COPY (or) SAMPLE
  504.  
  505. ------------------------------------------------------------------------------------------------------------------------
  506. //9. C program to implement the FIRST in context free grammar
  507.  
  508. #include<stdio.h>
  509.  
  510. #include<ctype.h>
  511.  
  512. #include<stdlib.h>
  513.  
  514. void FIRST(char);
  515. int count, n = 0;
  516. char prodn[10][10], first[10];
  517.  
  518. void main() {
  519.   int i, choice;
  520.   char c, ch;
  521.   printf("Enter the number of productions: ");
  522.   scanf("%d", & count);
  523.   printf("Enter %d productions:\nEpsilon=$\n", count);
  524.   for (i = 0; i < count; i++)
  525.     scanf("%s%c", prodn[i], & ch);
  526.   do {
  527.  
  528.     n = 0;
  529.     printf("Element :");
  530.     scanf("%c", & c);
  531.     FIRST(c);
  532.     printf("\nFIRST(%c)={", c);
  533.     for (i = 0; i < n; i++)
  534.       printf(" %c", first[i]);
  535.     printf("}\n");
  536.     printf("Press 1 to continue :");
  537.     scanf("%d%c", & choice, & ch);
  538.   }
  539.   while (choice == 1);
  540. }
  541.  
  542. void FIRST(char c) {
  543.   int j;
  544.   if (!(isupper(c))) first[n++] = c;
  545.   for (j = 0; j < count; j++) {
  546.     if (prodn[j][0] == c) {
  547.       if (prodn[j][2] == 'S') first[n++] = '$';
  548.       else if (islower(prodn[j][2])) first[n++] = prodn[j][2];
  549.       else FIRST(prodn[j][2]);
  550.     }
  551.   }
  552. }
  553.  
  554. //op
  555. ./a.out
  556. Enter the number of productions: 8
  557. Enter 8 productions:
  558. Epsilon=$
  559. E=TD
  560. D=+TD
  561. D=$
  562. T=FS
  563. S=*FS
  564. S=$
  565. F=(E)
  566. F=a
  567.  
  568. ------------------------------------------------------------------------------------------------------------------------
  569. //10. C program to implement Shift Reduce Parser for the given grammar:
  570. E → E+E
  571. E → E*E
  572. E →(E)
  573. E→ id
  574.  
  575. #include<stdio.h>
  576. #include<string.h>
  577.  
  578. int k = 0, z = 0, i = 0, j = 0, c = 0;
  579. char a[16], ac[20], stk[15], act[10];
  580.  
  581. void check();
  582. int main() {
  583.   puts("GRAMMAR is E->E+E \n E->E*E \n E->(E) \n E->id");
  584.   puts("enter input string ");
  585.   gets(a);
  586.   c = strlen(a);
  587.   strcpy(act, "SHIFT->");
  588.   puts("stack \t input \t action");
  589.   printf("\n$\t%s$\t---", a);
  590.   for (k = 0, i = 0; j < c; k++, i++, j++) {
  591.     if (a[j] == 'i' && a[j + 1] == 'd') {
  592.       stk[i] = a[j];
  593.       stk[i + 1] = a[j + 1];
  594.       stk[i + 2] = '\0';
  595.       a[j] = ' ';
  596.       a[j + 1] = ' ';
  597.       printf("\n$%s\t%s$\t%sid", stk, a, act);
  598.       check();
  599.     } else {
  600.       stk[i] = a[j];
  601.       stk[i + 1] = '\0';
  602.       a[j] = ' ';
  603.       printf("\n$%s\t%s$\t%ssymbols", stk, a, act);
  604.       check();
  605.     }
  606.   }
  607. }
  608. void check() {
  609.   strcpy(ac, "REDUCE TO E");
  610.  
  611.   for (z = 0; z < c; z++)
  612.     if (stk[z] == 'i' && stk[z + 1] == 'd') {
  613.       stk[z] = 'E';
  614.       stk[z + 1] = '\0';
  615.       printf("\n$%s\t%s$\t%s", stk, a, ac);
  616.       j++;
  617.     }
  618.   for (z = 0; z < c; z++)
  619.     if (stk[z] == 'E' && stk[z + 1] == '+' && stk[z + 2] == 'E') {
  620.       stk[z] = 'E';
  621.       stk[z + 1] = '\0';
  622.       stk[z + 2] = '\0';
  623.       printf("\n$%s\t%s$\t%s", stk, a, ac);
  624.       i = i - 2;
  625.     }
  626.   for (z = 0; z < c; z++)
  627.     if (stk[z] == 'E' && stk[z + 1] == '*' && stk[z + 2] == 'E') {
  628.       stk[z] = 'E';
  629.       stk[z + 1] = '\0';
  630.       stk[z + 1] = '\0';
  631.       printf("\n$%s\t%s$\t%s", stk, a, ac);
  632.       i = i - 2;
  633.     }
  634.   for (z = 0; z < c; z++)
  635.     if (stk[z] == '(' && stk[z + 1] == 'E' && stk[z + 2] == ')') {
  636.       stk[z] = 'E';
  637.       stk[z + 1] = '\0';
  638.       stk[z + 1] = '\0';
  639.       printf("\n$%s\t%s$\t%s", stk, a, ac);
  640.       i = i - 2;
  641.  
  642.     }
  643. }
  644.  
  645. //op
  646. ./a.out
  647. id+id*id
  648. ------------------------------------------------------------------------------------------------------------------------
  649.  
  650.  
Advertisement
Add Comment
Please, Sign In to add comment