Advertisement
opurag

SSDC

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