Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------------------------------------------------------------------------------------------
- //1
- //1a:Program to count the number, words, spaces and lines in a given input file
- %{
- #include<stdio.h>
- int c=0;
- int w=0;
- int s=0;
- int l=0;
- %}
- %%
- " " { s++; w++;}
- [\n] {l++;w++;}
- [\t\n] {w++;}
- [^\t\n] {c++;}
- %%
- int yywrap()
- {
- return 1;
- }
- int main()
- {
- yyin=fopen("Info.txt", "r");
- yylex();
- printf("Characters = %d\nWords = %d\nSpaces = %d\nLines= %d\n",c,w,s,l);
- return 0;
- }
- //op
- lex 1a.l
- cc lex.yy.c -ll
- ./a.out
- //1b:Program to recognize and count the number of identifiers in a file
- %{
- #include<stdio.h>
- int i=0;
- %}
- digit [0-9]
- letter [a-z A-Z_]
- %%
- {letter}({letter}|{digit})* {i++;}
- {digit}({letter}|{digit})* {i;}
- %%
- int main()
- {
- printf("Enter the values:\n");
- yylex();
- printf("Number of identifiers = %d\n", i);
- return 0;
- }
- //op
- lex 1b.l
- cc lex.yy.c -ll
- ./a.out
- // ctrl+d to end loop
- ------------------------------------------------------------------------------------------------------------------------
- //2
- //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.
- %{
- #include<stdio.h>
- int ml=0;
- int sl=0;
- %}
- %%
- "/*"[a-z A-Z 0-9 ' ' \t \n]*"*/" ml++;
- "//".* sl++;
- %%
- int main()
- {
- yyin=fopen("f1.txt","r");
- yyout=fopen("f2.txt","w");
- yylex();
- fclose(yyin);
- fclose(yyout);
- printf("Number of single line comments =%d\n",sl);
- printf("Number of multi line comments =%d\n",ml);
- }
- //op
- gedit f1.txt
- lex 2a.l
- cc lex.yy.c -ll
- cat f2.txt
- //2b: Program to recognize whether a given sentence is simple or compound.
- %{
- #include<stdio.h>
- int c=0;
- %}
- %%
- [a-zA-Z]*[ ](and|or|but|yet|so)[ ][a-zA-Z]* {c=1;}
- .|[\n];
- %%
- int yywrap()
- {
- return 1;
- }
- void main()
- {
- printf("enter the text\n");
- yylex();
- if(c)
- {
- printf("The given statement is compound\n");
- }
- else
- {
- printf("The given statement is simple\n");
- }
- }
- //op
- lex 2b.l
- cc lex.yy.c -ll
- ./a.out
- ------------------------------------------------------------------------------------------------------------------------
- //3
- //3a: Program to count number of:
- // i.+ve and -ve integers
- // ii. +ve and -ve fractions
- %{
- #include<stdio.h>
- int pi=0,ni=0,pf=0,nf=0;
- %}
- %%
- [-][0-9]+ {ni++;}
- [+]?[0-9]+ {pi++;}
- [-][0-9]*\.[0-9]+ {nf++;}
- [+]?[0-9]*\.[0-9]+ {pf++;}
- %%
- void main(int argc,char *argv[])
- {
- if(argc!=2)
- {
- printf("usage :./a.out in.txt \n");
- exit(0);
- }
- yyin=fopen(argv[1],"r");
- yylex();
- printf("Number of positive integer %d\n",pi);
- printf("Number of negative integer %d\n",ni);
- printf("Number of positive fraction %d\n",pf);
- printf("Number of negative fraction %d\n",nf);
- }
- int yywrap(){
- return 1;
- }
- //in.txt
- 12
- -29
- 0
- +8
- -0.9
- //op
- lex 3a.l
- cc lex.yy.c -ll
- ./a.out in.txt
- //3b: Program to count the number of “scanf” and “printf” statements in a C program. Replace them with “readf” and “writef” statements respectively.
- %{
- #include<stdio.h>
- int sf=0,pf=0;
- %}
- %%
- "scanf" {sf++; fprintf(yyout,"readf");}
- "printf" {pf++; fprintf(yyout,"writef");}
- %%
- int main()
- {
- yyin=fopen("f1.c","r");
- yyout=fopen("f2.c","w");
- yylex();
- printf("no of scanf =%d\n no of printf =%d\n",sf,pf);
- return 0;
- }
- //op
- gedit f1.c
- lex 3b.l
- cc lex.yy.c -ll
- ./a.out
- cat f2.c
- ------------------------------------------------------------------------------------------------------------------------
- //4: Program to evaluate arithmetic expression involving operators +, -, *, /
- //4.l
- %{
- #include "y.tab.h"
- extern yylval;
- %}
- %%
- [0-9]+ {yylval=atoi(yytext);return num;}
- [\+\-\*\/] {return yytext[0];}
- [)] {return yytext[0];}
- [(] {return yytext[0];}
- . {;}
- \n {return 0;}
- %%
- //4.y
- %{
- #include<stdio.h>
- #include<stdlib.h>
- %}
- %name parse
- %token num
- %left'+''-'
- %left'*''/'
- %%
- input:exp {printf("%d\n",$$);exit(0);}
- exp:exp'+'exp{$$=$1+$3;}
- |exp'-'exp{$$=$1-$3;}
- |exp'*'exp{$$=$1*$3;}
- |exp'/'exp{if($3==0){printf("Divide by zero error\n ");exit(0);}
- else
- $$=$1/$3;}
- |'('exp')'{$$=$2;}
- |num{$$=$1;};
- %%
- int yyerror()
- {
- printf("error");
- exit(0);
- }
- int main()
- {
- printf("Enter an expression:\n");
- yyparse();
- }
- //op
- lex 4.l
- yacc -d 4.y
- cc lex.yy.c y.tab.c -lfl
- ./a.out
- ------------------------------------------------------------------------------------------------------------------------
- //5: Program to recognize a valid variable which starts with a letter, followed by any number of letter or digits
- //p5.l
- %{
- #include "y.tab.h"
- %}
- %%
- [a-zA-z] return L;
- [0-9] return D;
- %%
- //p5.y
- %{
- #include<stdio.h>
- #include<stdlib.h>
- %}
- %name parse
- %token L D
- %%
- var:L E {printf("Valid Variable\n"); return 0;}
- E:E L;
- |E D;
- | ;
- %%
- int main()
- {
- printf("Type the variable\n");
- yyparse();
- return 0;
- }
- int yyerror()
- {
- printf("Invalid Variable\n");
- exit(0);
- }
- //op
- lex p5.l
- yacc -d p5.y
- cc lex.yy.c y.tab.c -lfl
- ./a.out
- ------------------------------------------------------------------------------------------------------------------------
- //6: Program to recognize the strings using the grammar (a^nb^n ; n>=0)
- //p6.l
- %{
- #include "y.tab.h"
- %}
- %%
- a return A;
- b return B;
- . return yytext[0];
- \n return yytext[0];
- %%
- //p6.y
- %{
- #include<stdio.h>
- #include<stdlib.h>
- %}
- %name parse
- %token A B
- %%
- Str:S '\n' {return 0;}
- S:A S B;
- | ;
- %%
- int main()
- {
- printf("Type the string\n");
- if (!yyparse())
- printf("Valid String\n");
- return 0;
- }
- int yyerror()
- {
- printf("Invalid String\n");
- exit(0);
- }
- //op
- lex p6.l
- yacc -d p6.y
- cc lex.yy.c y.tab.c -lfl
- ./a.out
- ------------------------------------------------------------------------------------------------------------------------
- //7: C program to implement Pass1 of Assembler
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void main() {
- char opcode[10], operand[10], label[10], code[10], mnemonic[3];
- int locctr, start, length;
- FILE *fp1, *fp2, *fp3, *fp4;
- fp1 = fopen("input.txt", "r");
- fp2 = fopen("optab.txt", "r");
- fp3 = fopen("symtabl.txt", "w");
- fp4 = fopen("out.txt", "w");
- fscanf(fp1, "%s\t%s\t%s", label, opcode, operand);
- if (strcmp(opcode, "START") == 0) {
- start = atoi(operand);
- locctr = start;
- fprintf(fp4, "\t%s\t%s\t%s\n", label, opcode, operand);
- fscanf(fp1, "%s\t%s\t%s", label, opcode, operand);
- } else
- locctr = 0;
- while (strcmp(opcode, "END") != 0) {
- fprintf(fp4, "%d\t", locctr);
- if (strcmp(label, "**") != 0) fprintf(fp3, "%s\t%d\n", label, locctr);
- fscanf(fp2, "%s\t%s", code, mnemonic);
- while (strcmp(code, "END") != 0) {
- if (strcmp(opcode, code) == 0) {
- locctr += 3;
- break;
- }
- fscanf(fp2, "%s\t%s", code, mnemonic);
- }
- if (strcmp(opcode, "WORD") == 0)
- locctr += 3;
- else if (strcmp(opcode, "RESW") == 0)
- locctr += (3 * (atoi(operand)));
- else if (strcmp(opcode, "RESB") == 0)
- locctr += atoi(operand);
- else if (strcmp(opcode, "BYTE") == 0)
- ++locctr;
- fprintf(fp4, "%s\t%s\t%s\t\n", label, opcode, operand);
- fscanf(fp1, "%s\t%s\t%s", label, opcode, operand);
- }
- fprintf(fp4, "%d\t%s\t%s\t%s\n", locctr, label, opcode, operand);
- length = locctr - start;
- printf("The length of the code:%d\n", length);
- fclose(fp1);
- fclose(fp2);
- fclose(fp3);
- fclose(fp4);
- }
- //1.Input.txt
- ** START 2000
- ** LDA FIVE
- ** STA ALPHA
- ** LDCH CHARZ
- ** STCH C1
- ALPHA RESW 2
- FIVE WORD 5
- CHARZ BYTE C'Z'
- C1 RESB 1
- ** END **
- //2.Optab.txt
- START *
- LDA 03
- STA 0f
- LDCH 53
- STCH 57
- END *
- //op
- gcc p7.c
- ./a.out
- cat Symtabl.txt
- cat Out.txt
- ------------------------------------------------------------------------------------------------------------------------
- //8: C program to implement Absolute loader.
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- void main() {
- FILE *fp;
- int i,l, j, staddrl;
- char name[10], line[50], namel[10], staddr[10];
- printf("enter program name: ");
- scanf("%s", name);
- fp = fopen("abssrc.txt", "r");
- fscanf(fp, "%s", line);
- for(i=2, j=0; i<8, j<6; i++, j++) {
- namel[j] = line[i];
- }
- namel[i] = '\0';
- printf("name from obj: %s\n", namel);
- if(strcmp(name,namel) == 0) {
- do {
- fscanf(fp, "%s", line);
- if(line[0] == 'T') {
- for(i=2, j=0; i<8, j<6; i++, j++) {
- staddr[j] = line[i];
- }
- staddr[j] = '\0';
- staddrl = atoi(staddr);
- i=12;
- while(line[i] != '$') {
- if(line[i] != '^') {
- printf("00%d\t%c%c\n", staddrl, line[i], line[i+1]);
- staddrl++;
- i = i+2;
- } else {
- i++;
- }
- }
- } else if (line[0] == 'E') {
- break;
- }
- }while(!feof(fp));
- fclose(fp);
- }
- }
- //abssrc.txt
- H^00COPY^001000
- T^001000^3C^001011^10121A^11222B$
- T^002000^2E^101B2A^0C123A$
- E^001000
- (or)
- H^SAMPLE^001000^0035
- T^001000^0C^001003^071009$
- T^002000^03^111111$
- E^001000
- //op
- ./a.out
- 00COPY (or) SAMPLE
- ------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement