Professional Documents
Culture Documents
SIT,CSE 1
SS LAB MANNUAL
2)Program to count the number of characters,words,spaces and linesin a given input file. %{ #include<stdio.h> Int ccnt=0,wcnt=0,scnt=0,lcnt=0; %} Word [^ \t\n ]+ Eol [\n] Space [ ] %% {eol} {ccnt++;lcnt++;} [\t] {ccnt++;scnt+=5;} {space} {ccnt++,scnt++;} . {ccnt++;}
{word} {ccnt+=yyleng;wcnt++;} %% Int main() { Yyin=fopen(input.txt,r); If(yyin) { Yylex(); Printf(Number of characters =%d\n,vcnt); Printf(Number of words,=%d\n,vcnt); Printf(Number of spaces =%d\n,vcnt); Printf(Number of lines =%d\n,ccnt); } Else
SIT,CSE 2
SS LAB MANNUAL
Printf(ERROR IN OPENING THE FILE); Return 0; } COMMANDS: $ vi p2.l $lex p2.l $ cc lex.yy.c ll $ vi input.txt $./a.out
SIT,CSE 3
SS LAB MANNUAL
3)program to count number of a) Positive and negative integers b) Positive and negative fractions %{ #include<stdio.h> Int pint=0,nint=0,pftn=0,nftn=0; %} %% \+?[0-9]+ {pint++;} -[0-9]+ {nint++;} \+?[0-9]*\.[0-9]+ | \+?[0-9]+\/[0-9]+ {pftn++;} -[0-9]*\.[0-9]+ | -[0-9]+\/[0-9]+ {nftn++;}
[+-]?[0-9]*\.[0-9]* | [+-]?[0-9]*\/[0-9]* ; . |\n ; Int main() { Printf(ENTER SOME NUMBERS AND PRESS<CTL+D>\N); Yylex(); Printf(Number of +ve int=%d\n Number of -ve int =%d\n,pint,nint); Printf(Number of +ve frctn=%d\n Number of -ve frctn=%d,pftn,nftn); Return 0; } COMMANDS: $ vi p3.l $ lex p3.l $ cc lex.yy.c ll
SIT,CSE 4
SS LAB MANNUAL
4) program to count number of comment lines in a given c program. Also eliminate them and copy that program into separate file. %{ #include<stdio.h> Int cnt=0; %} %x COMMENT %% //(.)*[ \t]*\n {cnt++; fprintf(yyout, );} /*(.)**/[ \t]*\n {cnt++;fprintf(yyout, );} /* {BEGIN COMMENT; fprintf(yyout, );}
<COMMENT>(.)* { fprintf(yyout, );} <COMMENT>\n {cnt++; fprintf(yyout, );} <COMMENT>*/[ \t]*\n {cnt++; fprintf(yyout, );} <COMMENT>(.)**/\n {cnt++; fprintf(yyout, );} %% Int main() { Yyin=fopen(f1.c,r); Yyout=fopen(f2.c,w); If(yyin && yyout) { Yylex(); Printf(Number of comment lines =%d\n,cnt); } Else Printf(ERROR IN OPENING FILES);
SIT,CSE 5
SS LAB MANNUAL
Fclose(yyin); Fclose(yyout); Return 0; } COMMANDS: $ vi p4.l $ lex p4.l $ cc lex.yy.c ll $vi f1.c //simple c program #include<stdio.h> /* calculate a=b+c */ main() { int a,b=20,c=50; /* its the declaration of a,b,c now calculate b+c store into a */ a=b+c; } $ ./a.out Number of comment lines=8 $vi f2.c #include<stdio.h> main()
SIT,CSE 6
SS LAB MANNUAL
{ int a,b,c; a=b+c; } 5) Program to count number of scanf and printf statements in a c program. Replace them with ;readf and writef statements respectively. %{ #include<stdio.h> Int pcnt=0,scnt=0; %} %% printf {pcnt++; fprintf(yyout,writef);} scanf {scnt++; fprintf(yyout,readf);} .|\n {fprintf(yyout,yytext);} %% Main() { yyin=fopen(f1.c,r); yyout=fopen(f2.c,w); if(yyin && yyout) { Yylex(); Printf(number of printfs=%d\n,pcnt); Printf(number of scanfs=%d\n,scnt); } Else Printf(error in opening files); Fclose(yyin); Fclose(yyout); }
SIT,CSE 7
SS LAB MANNUAL
SIT,CSE 8
SS LAB MANNUAL
6)Program to recognize a valid arithmetic expression and identify the identifiers and operators present. Print them separately. %{ #include<string.h> #include<stdio.h> Int tcnt=0,bcnt=0,i=0,j=0; Char oprt[10],oprd[10][7]; %} %% [+-/*%] {tcnt++; oprt[i++]=yytext[0];}
[a-zA-Z0-9]+ {tcnt++; strcpy(oprd[j++],yytext); } [(] {b++;} [)] {b--;} .|\n ; %% Main() { int l; Printf(ENTER AN EXPRESSION\n); Yylex(); If((tcnt!=1)&&(tcnt%2!=0)) { If(b==0) { Printf(valid Expression\n); Printf(the operators are\n); For(l=0;l<I;l++) Printf(%c\n,oprt[l]); Printf(the identifiers are\n);
SIT,CSE 9
SS LAB MANNUAL
For(l=0;l<j;l++) Printf(%s\n,oprd[l]); } Else Printf(Unmatched braces); } else printf(Invalid Expression); } COMMANDS: $ vi p6.l $ lex p6.l $ cc lex.yy.c ll $./a.out
SIT,CSE 10
SS LAB MANNUAL
7)Program to recognize whether a given sentence is simple or compound. %{ #include<stdio.h> Int flag=0; %} %% and | or | if | else |but |not .|\n ; %% Main() { Printf(ENTER A SENTENCE\n); Yylex(); If(flag) Printf(COMPOUND SENTENCE\n); Else Printf(SIMPLE SENTENCE\n); } COMMANDS: $ vi p7.l $ lex p7.l $ cc lex.yy.c ll $./a.out { flag=1;}
SIT,CSE 11
SS LAB MANNUAL
8)Program to recognize and count the number of identifiers in a given input file. %{ #include<stdio.h> Int cnt=0; %} %% int | void | double | char |float |short |long Char ch; While(1) { Ch=iput(); If(ch==,) cnt++; Else If(ch==;) { cnt++; break; } } .|\n ; %% Main() { Yyin=fopen(f1.c,r); If(yyin) { Yylex(); Printf(NUMBER OF IDENTIFIERS=%d\n,cnt); } Else {
SIT,CSE 12
SS LAB MANNUAL
Printf(ERROR IN OPENING FILE); fclose(yyin); } COMMANDS: $ vi p8.l $ lex p8.l $ cc lex.yy.c ll $ vi f1.c $./a.out
SIT,CSE 13
SS LAB MANNUAL
YACC PROGRAMS
1)Program to test the validity of a simple arithmetic expression involving operators +,-,* and /. Lex file(y1.l) %{ #includey.tab.h %} %% [a-zA-Z]+|[0-9]+ {return OP;} \n {return ENTER;}
. {return yytext[0];} %% Yacc file(y1.y) %{ #include<stdio.h> Int flag=0; %} %token OP ENTER %% Start: exp ENTER {flag=1; return;} ; exp:+ OP |-OP |OP+ exp | OP- exp | OP* exp | OP/ exp |(exp)
SIT,CSE 14
SS LAB MANNUAL
| OP ; %% Main() { Printf(ENTER AN EXPRESSION\n); Yyparse(); If(flag) Printf(VALID EXPRESSION); Else Printf(INVALID EXPRESSION); } Yyerror(char *s) {} COMMANDS: $lex y1.l $yacc d y1.y $cc lex.yy.c y.tab.c ll $./a.out
SIT,CSE 15
SS LAB MANNUAL
2)Program to recognize nested IF control statements and display the number of levels of nesting. Lex file (y2.l) %{ #include"y.tab.h" %} %% "if" {return IF;} ['('] {return yytext[0];} [')'] {return yytext[0];} . {return TERMINAL;}
%{ #include<stdio.h> int flag=0,count=0; %} %token IF TERMINAL ENTER %% st :stmt ; stmt :stmt IF '('cond')'ENTER {count++;} |IF'('cond')'ENTER ; cond :cond TERMINAL |TERMINAL ; SIT,CSE 16 {count++;} {if(count>=10)flag=1;return;}
SS LAB MANNUAL %% main() { yyparse(); if(flag) printf("Valid Nesting and Number of levels of nesting is:%d\n",count); else printf("Invalid\n"); } yyerror(char *s) { }
COMMANDS: $lex y1.l $yacc d y1.y $cc lex.yy.c y.tab.c ll $./a.out
SIT,CSE 17
SS LAB MANNUAL
3) Program to recognize valid arithmetic expression that uses operators +,-,* and /. Lex file(y3.l) %{ #includey.tab.h %} %% [a-zA-Z]+|[0-9]+ {return OP;} \n {return ENTER;}
. {return yytext[0];} %% Yacc file(y3.y) %{ #include<stdio.h> Int flag=0; %} %token OP ENTER %left + - %left * / %% Start: exp ENTER {flag=1; return;} ; exp:exp+exp |exp-exp |exp*exp |exp/exp |(exp) | OP
SIT,CSE 18
SS LAB MANNUAL
; %% Main() { Printf(ENTER AN EXPRESSION\n); Yyparse(); If(flag) Printf(VALID EXPRESSION); Else Printf(INVALID EXPRESSION); } Yyerror(char *s) {} COMMANDS: $lex y3.l $yacc d y3.y $cc lex.yy.c y.tab.c ll $./a.out
SIT,CSE 19
SS LAB MANNUAL
4)Program to recognize a valid variable ,which starts with a letter ,followed by any number of letters or digits. Lex file(y4.l) %{ #includey.tab.h %} %% [0-9]+ {return DG;}
. {return yytext[0];} %% Yacc file(y1.y) %{ #include<stdio.h> Int flag=0; %} %token DG LTR ENTER %% Start: LTR str ENTER { flag=1; return;} ; str:LTR DG str | DG LTR str ; %% Main() {
SIT,CSE 20
SS LAB MANNUAL
Printf(ENTER A VARIABLE\n); Yyparse(); If(flag) Printf(VALID VARIABLE); Else Printf(INVALID VARIABLE); } Yyerror(char *s) {} COMMANDS: $lex y4.l $yacc d y4.y $cc lex.yy.c y.tab.c ll $./a.out
SIT,CSE 21
SS LAB MANNUAL
5)Program to evaluate an arithmetic expression involving operators +,-,* and /. Lex file(y5.l) %{ #includey.tab.h Extern int yylval; %} %% [0-9]+ {yylval=atoi(yytext);return OP;} \n {return ENTER;}
. {return yytext[0];} %% Yacc file(y5.y) %{ #include<stdio.h> Int flag=0; %} %token OP ENTER %left + - %left * / %% Start: exp ENTER {flag=1;printf(=%d,$$); return;} ; exp:exp+exp {$$=$1+$3;} |exp-exp |exp*exp {$$=$1-$3;} {$$=$1*$3;}
SIT,CSE 22
SS LAB MANNUAL
|exp/exp {if($3==0) yyerror(DIVIDE BY ZERO ERROR); else $$=$1/$3; |(exp) | OP ; %% Main() { Printf(ENTER AN EXPRESSION\n); Yyparse(); If(flag) Printf(VALID EXPRESSION); else Printf(INVALID EXPRESSION); } Yyerror(char *s) { printf(%s,s); } COMMANDS: $lex y5.l $yacc d y5.y $cc lex.yy.c y.tab.c ll $./a.out { $$=$1; } {$$=$1; } }
SIT,CSE 23
SS LAB MANNUAL
6) Program to recognize strings aaab,abbb,aband a using the grammar (an bn n>=0). A)Implementation (an bn,n>=0) Y6.l %{ #includey.tab.h %} %% a b \n . %% Y6a.y %{ #include<stdio.h> Int ca=0,cb=0,flag=0; %} %token CHARA CHARB ENTER %% Start: exp1 exp2 ENTER {if(ca==cb) flag=1; return; } ; exp1: CHARA exp1 |CHARA ; {ca++;} {ca++;} {return CHARA;} {return CHARA;} {return ENTER;} {return yytext[0];}
SIT,CSE 24
SS LAB MANNUAL
exp2: CHARB exp1 |CHARB ; %% Main() { Printf(ENTER AN EXPRESSION\n); Yyparse(); If(flag) Printf(VALID STRING); else Printf(INVALID STRING); } Yyerror(char *s) { printf(%s,s); } COMMANDS: $lex y6.l $yacc d y6a.y $cc lex.yy.c y.tab.c ll $./a.out {cb+;} {cb++;}
SIT,CSE 25
SS LAB MANNUAL
Start: exp1 exp2 ENTER { flag=1; return; } |CHARA ENTER { flag=1; return; } ; exp1: CHARA exp1 |CHARA ; exp2: CHARB exp1 |CHARB ; %% Main() { Printf(ENTER AN EXPRESSION\n); Yyparse(); If(flag) Printf(VALID STRING); else Printf(INVALID STRING); } Yyerror(char *s) { printf(%s,s); } COMMANDS: $lex y6.l $yacc d y6b.y $cc lex.yy.c y.tab.c ll $./a.out
SIT,CSE 26
SS LAB MANNUAL
7) Program to recognize the grammar (an b ,n>=10). Y7.l %{ #includey.tab.h %} %% a b \n . %% Y7.y %{ #include<stdio.h> Int ca=0,flag=0; %} %token CHARA CHARB ENTER %% Start: exp1 CHARB ENTER {if(ca>=10) flag=1; return; } ; exp1: CHARA exp1 |CHARA ; {ca++;} {ca++;} {return CHARA;} {return CHARA;} {return ENTER;} {return yytext[0];}
SIT,CSE 27
SS LAB MANNUAL
%% Main() { Printf(ENTER AN EXPRESSION\n); Yyparse(); If(flag) Printf(VALID STRING); else Printf(INVALID STRING); } Yyerror(char *s) { printf(%s,s); } COMMANDS: $lex y7.l $yacc d y7.y $cc lex.yy.c y.tab.c ll $./a.out
SIT,CSE 28
SS LAB MANNUAL
PART B Students(a batch must consists of 2 students) must do one of the below mini projects.However all the projects must be carried out by some batches in a class: Implement a Multi-Pass Assembler:
A multi-pass assembler is a solution to the problem with symbol definitions in terms of forward references which cannot be solved in a two-pass assembler. In the first pass usual scanning of the input is done then N number of passes can be done for resolving the definition until every symbol is defined properly. Then a final pass will be done while we generate the actual object program. For the forward reference we make use of following type of symbol table which will update in every pass till the final one. Symbol name Eg: A &n (n-number of undefined symbols in current definition) Eg: &1 Expression which NULL (if A is not gives the value having any other for A symbols depending on it) else Eg: MAXLEN/2 Ptr to list which will contain the list of symbols depending on A Ptr[HALFSZ,NULL]
MAXLEN
SIT,CSE 29
SS LAB MANNUAL
We will repeat the procedure of defining symbols until there is no * found in the symbol table.
SIT,CSE 30
SS LAB MANNUAL
SIT,CSE 31
SS LAB MANNUAL
SIT,CSE 32
SS LAB MANNUAL
SIT,CSE 33