Académique Documents
Professionnel Documents
Culture Documents
PART A
LEX PROGRAMS
1)Program to count the number of vowels and consonants in a given string.
%{
#include<stdio.h>
Int ccnt=0,vcnt=0;
%}
%%
[AEIOUaeiou] {vcnt++;}
[B-DF-HJ-NP-TV-Zb-df-hj-np-tv-z] {ccnt++;}
.|\n ;
%%
Int main()
Yylex();
Printf(“Number of VOWELS=%d\n,vcnt);
Printf(“Number of CONSONANTS=%d\n,ccnt);
Return 0;
COMMANDS:
$ vi p1.l
$ lex p1.l
$ cc lex.yy.c –ll
2)Program to count the number of characters,words,spaces and linesin a given input file.
SIT,CSE 1
SS LAB MANNUAL
%{
#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 words,=%d\n,vcnt);
Else
Return 0;
SIT,CSE 2
SS LAB MANNUAL
}
COMMANDS:
$ vi p2.l
$lex p2.l
$ cc lex.yy.c –ll
$ vi input.txt
$./a.out
SIT,CSE 3
SS LAB MANNUAL
a) Positive and negative integers
%{
#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]* ;
. |\n ;
Int main()
Yylex();
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
%%
%%
Int main()
Yyin=fopen(“f1.c”,”r”);
Yyout=fopen(“f2.c”,”w”);
Yylex();
Else
Fclose(yyin);
Fclose(yyout);
SIT,CSE 5
SS LAB MANNUAL
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;
/* it’s the
declaration of a,b,c
now
calculate b+c
store into a
*/
a=b+c;
$ ./a.out
$vi f2.c
#include<stdio.h>
main()
5) Program to count number of ‘scanf’ and ‘printf’ statements in a c program. Replace them with ;readf’ and
‘writef’ statements respectively.
SIT,CSE 6
SS LAB MANNUAL
%{
#include<stdio.h>
Int pcnt=0,scnt=0;
%}
%%
.|\n {fprintf(yyout,yytext);}
%%
Main()
yyin=fopen(“f1.c”,”r”);
yyout=fopen(“f2.c”,”w”);
Yylex();
Printf(“number of printfs=%d\n”,pcnt);
Printf(“number of scanfs=%d\n”,scnt);
Else
Fclose(yyin);
Fclose(yyout);
COMMANDS:
SIT,CSE 7
SS LAB MANNUAL
$ vi p5.l
$ lex p5.l
$ cc lex.yy.c –ll
$vi f1.c
$./a.out
$ vi f2.c
6)Program to recognize a valid arithmetic expression and identify the identifiers and operators present. Print
them separately.
%{
#include<string.h>
SIT,CSE 8
SS LAB MANNUAL
#include<stdio.h>
Int tcnt=0,bcnt=0,i=0,j=0;
Char oprt[10],oprd[10][7];
%}
%%
[(] {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”);
For(l=0;l<I;l++)
Printf(“%c\n”,oprt[l]);
For(l=0;l<j;l++)
Printf(“%s\n”,oprd[l]);
Else
SIT,CSE 9
SS LAB MANNUAL
Printf(“Unmatched braces”);
else
printf(“Invalid Expression”);
COMMANDS:
$ vi p6.l
$ lex p6.l
$ cc lex.yy.c –ll
$./a.out
%{
#include<stdio.h>
Int flag=0;
SIT,CSE 10
SS LAB MANNUAL
%}
%%
.|\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
8)Program to recognize and count the number of identifiers in a given input file.
%{
#include<stdio.h>
Int cnt=0;
SIT,CSE 11
SS LAB MANNUAL
%}
%%
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
fclose(yyin);
COMMANDS:
$ vi p8.l
SIT,CSE 12
SS LAB MANNUAL
$ lex p8.l
$ cc lex.yy.c –ll
$ vi f1.c
$./a.out
YACC PROGRAMS
1)Program to test the validity of a simple arithmetic expression involving operators +,-,* and /.
Lex file(y1.l)
%{
#include”y.tab.h”
SIT,CSE 13
SS LAB MANNUAL
%}
%%
\n {return ENTER;}
. {return yytext[0];}
%%
Yacc file(y1.y)
%{
#include<stdio.h>
Int flag=0;
%}
%token OP ENTER
%%
exp:’+’ OP
|’-‘OP
|OP’+’ exp
| OP’-‘ exp
| OP’*’ exp
| OP’/’ exp
|’(‘exp’)’
| OP
%%
Main()
Printf(“ENTER AN EXPRESSION\n”);
SIT,CSE 14
SS LAB MANNUAL
Yyparse();
If(flag)
Printf(“VALID EXPRESSION”);
Else
Printf(“INVALID EXPRESSION”);
Yyerror(char *s)
{}
COMMANDS:
$lex y1.l
$yacc –d y1.y
$./a.out
2)Program to recognize nested IF control statements and display the number of levels of nesting.
%{
#include"y.tab.h"
%}
%%
SIT,CSE 15
SS LAB MANNUAL
"if" {return IF;}
. {return TERMINAL;}
%%
Yaccfile(y2.y)
%{
#include<stdio.h>
int flag=0,count=0;
%}
%%
st :stmt {if(count>=10)flag=1;return;}
|IF'('cond')'ENTER {count++;}
|TERMINAL
%%
main()
yyparse();
if(flag)
SIT,CSE 16
SS LAB MANNUAL
else
printf("Invalid\n");
yyerror(char *s)
COMMANDS:
$lex y1.l
$yacc –d y1.y
$./a.out
3) Program to recognize valid arithmetic expression that uses operators +,-,* and /.
Lex file(y3.l)
%{
#include”y.tab.h”
%}
%%
SIT,CSE 17
SS LAB MANNUAL
\n {return ENTER;}
. {return yytext[0];}
%%
Yacc file(y3.y)
%{
#include<stdio.h>
Int flag=0;
%}
%token OP ENTER
%%
exp:exp’+’exp
|exp’-‘exp
|exp’*’exp
|exp’/’exp
|’(‘exp’)’
| OP
%%
Main()
Printf(“ENTER AN EXPRESSION\n”);
Yyparse();
If(flag)
Printf(“VALID EXPRESSION”);
SIT,CSE 18
SS LAB MANNUAL
Else
Printf(“INVALID EXPRESSION”);
Yyerror(char *s)
{}
COMMANDS:
$lex y3.l
$yacc –d y3.y
$./a.out
4)Program to recognize a valid variable ,which starts with a letter ,followed by any number of letters or digits.
Lex file(y4.l)
%{
#include”y.tab.h”
%}
%%
SIT,CSE 19
SS LAB MANNUAL
[a-zA-Z]+| {return LTR;}
\n {return ENTER;}
. {return yytext[0];}
%%
Yacc file(y1.y)
%{
#include<stdio.h>
Int flag=0;
%}
%%
str:LTR DG str
| DG LTR str
%%
Main()
Printf(“ENTER A VARIABLE\n”);
Yyparse();
If(flag)
Printf(“VALID VARIABLE”);
Else
Printf(“INVALID VARIABLE”);
Yyerror(char *s)
{}
SIT,CSE 20
SS LAB MANNUAL
COMMANDS:
$lex y4.l
$yacc –d y4.y
$./a.out
Lex file(y5.l)
%{
#include”y.tab.h”
%}
%%
SIT,CSE 21
SS LAB MANNUAL
\n {return ENTER;}
. {return yytext[0];}
%%
Yacc file(y5.y)
%{
#include<stdio.h>
Int flag=0;
%}
%token OP ENTER
%%
exp:exp’+’exp {$$=$1+$3;}
|exp’-‘exp {$$=$1-$3;}
|exp’*’exp {$$=$1*$3;}
|exp’/’exp {if($3==0)
else
$$=$1/$3; }
|’(‘exp’)’ { $$=$1; }
| OP {$$=$1; }
%%
Main()
Printf(“ENTER AN EXPRESSION\n”);
SIT,CSE 22
SS LAB MANNUAL
Yyparse();
If(flag)
Printf(“VALID EXPRESSION”);
else
Printf(“INVALID EXPRESSION”);
Yyerror(char *s)
{ printf(“%s”,s); }
COMMANDS:
$lex y5.l
$yacc –d y5.y
$./a.out
6) Program to recognize strings ‘aaab’,’abbb’,’ab’and ‘a’ using the grammar (an bn n>=0).
Y6.l
%{
#include”y.tab.h”
%}
%%
SIT,CSE 23
SS LAB MANNUAL
“b” {return CHARA;}
\n {return ENTER;}
. {return yytext[0];}
%%
Y6a.y
%{
#include<stdio.h>
Int ca=0,cb=0,flag=0;
%}
%%
|CHARA {ca++;}
|CHARB {cb++;}
%%
Main()
Printf(“ENTER AN EXPRESSION\n”);
Yyparse();
If(flag)
Printf(“VALID STRING”);
else
Printf(“INVALID STRING”);
SIT,CSE 24
SS LAB MANNUAL
}
Yyerror(char *s)
{ printf(“%s”,s); }
COMMANDS:
$lex y6.l
$yacc –d y6a.y
$./a.out
Y6b.y
%{
#include<stdio.h>
Int flag=0;
%}
%%
|CHARA
|CHARB
%%
Main()
Printf(“ENTER AN EXPRESSION\n”);
SIT,CSE 25
SS LAB MANNUAL
Yyparse();
If(flag)
Printf(“VALID STRING”);
else
Printf(“INVALID STRING”);
Yyerror(char *s)
{ printf(“%s”,s); }
COMMANDS:
$lex y6.l
$yacc –d y6b.y
$./a.out
Y7.l
%{
#include”y.tab.h”
%}
%%
SIT,CSE 26
SS LAB MANNUAL
\n {return ENTER;}
. {return yytext[0];}
%%
Y7.y
%{
#include<stdio.h>
Int ca=0,flag=0;
%}
%%
|CHARA {ca++;}
%%
Main()
Printf(“ENTER AN EXPRESSION\n”);
Yyparse();
If(flag)
Printf(“VALID STRING”);
else
Printf(“INVALID STRING”);
Yyerror(char *s)
{ printf(“%s”,s); }
COMMANDS:
SIT,CSE 27
SS LAB MANNUAL
$lex y7.l
$yacc –d y7.y
$./a.out
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:
a) Implement a one-Pass Assembler:
Input: . asm file(assembly language code)
One pass assembler that produces object program for later execution.
SIT,CSE 28
SS LAB MANNUAL
A one pass assembler scans the input and simultaneously generates the object code for it.
Here forward references are entered into lists which will be pointing to the symbols that have
undefined symbols in their definitions.
When the definition of a symbol is encountered, instructions that made forward references to that
symbol may no longer be available in memory for modification hence the assembler must
generate another text record in the object program with the correct operand address. When the
program is loaded, this address will be inserted into the instruction by the action of the loader.
SIT,CSE 29
SS LAB MANNUAL
SIT,CSE 30
SS LAB MANNUAL
SIT,CSE 31
SS LAB MANNUAL
b) 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.
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 &n (n-number of Expression which NULL (if A is not having
undefined symbols gives the value for A any other symbols
Eg: A in current definition) depending on it) else
Eg: MAXLEN/2
Eg: &1 Ptr to list which will
contain the list of
symbols depending on
A
MAXLEN * Ptr[HALFSZ,NULL]
We will repeat the procedure of defining symbols until there is no ‘*’ found in the symbol
table.
SIT,CSE 32
SS LAB MANNUAL
c) Simulate a Linking Loader: Linking Loader - Pass1
begin
begin
else enter control section name into ESTAB with value CSADDR
begin
else enter symbol into ESTAB with value (CSADDR + indicated address)
end {for}
SIT,CSE 33
SS LAB MANNUAL
Begin
begin
begin
begin
if found then add or subtract symbol value at location (CSADDR + specified address)
if an address is specified {in End record} then set EXECADDR to (CSADDR + specified address)
SIT,CSE 34
SS LAB MANNUAL
SIT,CSE 35
SS LAB MANNUAL
SIT,CSE 36
SS LAB MANNUAL
SIT,CSE 37
SS LAB MANNUAL
SIT,CSE 38
SS LAB MANNUAL
SIT,CSE 39