Beruflich Dokumente
Kultur Dokumente
LEX PROGRAMS
1(a).Program to count the number of Characters,Words,Spaces and Lines in a given
input line
PROGRAM:
%{
int WCount=0,CCount=0,BCount=0,LCount=0;
%}
Word [^ \t\n]+
Eol [\n]
%%
{Word} { WCount++;
CCount+=yyleng; }
{Eol} { CCount++; LCount++; }
[ ]
{ BCount++; }
.
{ ECHO; CCount; }
%%
main(int argc,char *argv[])
{
if(argc!=0)
{
FILE *fp;
fp=fopen(argv[1],"r");
if(!fp)
{
fprintf(stderr,"Unable to Open File %s for Reading!!!! \n",argv[1]);
exit(1);
}
yyin=fp;
yylex();
printf("\nNumber of Blanks=%d\nNumber of Characters=%d",BCount,CCount);
printf("\nNumber of Words=%d\nNumber of Line=%d\n",WCount,LCount);
}
else
{
printf("Too Few Parameters Required\n");
}
}
Bharath Bharadwaj B S
MIT,Mysore
OUTPUT:
$ lex 1a.l
$ cc lex.yy.c -ll
$ ./a.out
Unable to Open File (null) for Reading!!!!
$ cat>s.c
B
h
a
r
a
t
h
$ ./a.out s.c
Number
Number
Number
Number
of
of
of
of
Blanks=21
Characters=14
Words=7
Line=7
Bharath Bharadwaj B S
MIT,Mysore
Bharath Bharadwaj B S
MIT,Mysore
OUTPUT:
$ lex 1b.l
$ cc lex.yy.c -ll
$ ./a.out
Pass 3 Arguments
$ cat>s1
/*Program To find sum of 3 numbers*/
/*start of program*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,sum;
/*Notify user to input the value*/
printf("Enter The Value of a,b & c");
/*Accept the input for variables for a,b,c*/
scanf("%d%d%d",&a,&b,&c);
sum=a+b+c;
/*Output the result*/
printf("sum=%d\n",sum);
getch();
}
/*End of Program*/
$ ./a.out s1 s
Number of Comment Lines=6
$ cat s
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,sum;
printf("Enter The Value of a,b & c");
scanf("%d%d%d",&a,&b,&c);
sum=a+b+c;
printf("sum=%d\n",sum);
getch();
}
Bharath Bharadwaj B S
MIT,Mysore
Bharath Bharadwaj B S
MIT,Mysore
Bharath Bharadwaj B S
MIT,Mysore
Bharath Bharadwaj B S
MIT,Mysore
Bharath Bharadwaj B S
MIT,Mysore
Bharath Bharadwaj B S
MIT,Mysore
Bharath Bharadwaj B S
10
MIT,Mysore
Bharath Bharadwaj B S
11
MIT,Mysore
Bharath Bharadwaj B S
12
MIT,Mysore
5(b).Program
(anbn,n>=0)
to
recognize
strings
aaabbb,aabb
and
ab
using
the
grammar
PROGRAM:
LEX PART:
%%{
#include "y.tab.h"
%}
%s NOAB STR
%%
[^ab\n] {BEGIN NOAB;return Z;}
<NOAB>.* {return Z;}
a {return X;}
b {return Y;}
\n {return 0;}
%%
YACC PART:
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token X Y Z
%%
exp:X exp Y
|
;
%%
main()
{
printf("Enter The String:");
yyparse();
printf("String Recognised...\n");
}
yyerror()
{
printf("String Unrecognised....\n");
exit(0);
}
OUTPUT:
$ lex 5b.l
$ yacc -d 5b.y
$ cc y.tab.c lex.yy.c -ll
$ ./a.out
Enter The String:aaabbb
String Recognised...
$ ./a.out
Enter The String:aabb
Bharath Bharadwaj B S
13
MIT,Mysore
Bharath Bharadwaj B S
14
MIT,Mysore
return Z;}
YACC PART:
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token X Y Z
%%
exp:X X X X X X X X X X exp1 Y
;
exp1:X exp1
|
;
%%
main()
{
printf("Enter the String:");
yyparse();
printf("String is Recognised...\n");
}
int yyerror()
{
printf("String Unrecognised.....\n");
exit(1);
}
OUTPUT:
$ lex 6.l
$ yacc -d 6.y
$ cc y.tab.c lex.yy.c -ll
$ ./a.out
Enter the String:aaaaaaaaaaaaaab
String is Recognised...
$ ./a.out
Enter the String:ab
String Unrecognised.....
Bharath Bharadwaj B S
15
MIT,Mysore
Bharath Bharadwaj B S
16
MIT,Mysore
Bharath Bharadwaj B S
17
MIT,Mysore
Bharath Bharadwaj B S
18
MIT,Mysore
Bharath Bharadwaj B S
19
MIT,Mysore
Bharath Bharadwaj B S
20
MIT,Mysore
Regular\n");
Directory\n");
Link\n");
is FIFO\n");
OUTPUT:
$ ls
3b.c
a.out
Desktop
Documents
Downloads
fifo_pipe
Pictures Public Templates TEST TEST1 Videos
$ cc 3b.c
$ ./a.out TEST1
File is Regular
$ ./a.out TEST
File is Directory
$ ./a.out Link_to_TEST
File is Link
$ ./a.out fifo_pipe
File is FIFO
Bharath Bharadwaj B S
21
Link_to_TEST
Music
MIT,Mysore
Bharath Bharadwaj B S
22
MIT,Mysore
Bharath Bharadwaj B S
23
MIT,Mysore
counter = parsecondition(stmt,counter,strS1,stlen);
if(stmt[counter+1]==';')
{ // reached end of statement, generate the output
printf("\n Parsing the input statement....");
gen(strB,strS1,strS2,elseflag);
return 0;
}
if(stmt[counter]==')')
counter++;
// increment over ')'
counter = counter + 3;
// increment over 'else'
counter = parsecondition(stmt,counter,strS2,stlen);
counter = counter + 2; // move to the end of statement
if(counter == stlen)
Bharath Bharadwaj B S
24
MIT,Mysore
}
return 0;
}
/* Function : parsecondition
Description : This function parses the statement from the given index to get the
statement enclosed in ()
Input : Statement, index to begin search, string to store the condition, total
string length
Output : Returns 0 on failure, Non zero counter value on success
*/
int parsecondition(char input[],int cntr,char *dest,int totallen)
{
int index = 0,pos = 0;
while(input[cntr]!= '(' && cntr <= totallen)
cntr++;
if(cntr >= totallen)
return 0;
index = cntr;
while (input[cntr]!=')')
cntr++;
if(cntr >= totallen)
return 0;
while(index<=cntr)
dest[pos++] = input[index++];
dest[pos]='\0'; //null terminate the string
return cntr; //non zero value
}
/* Function : gen ()
Description : This function generates three address code
Input : Expression, statement for true condition, statement for
false condition, flag to denote if the 'else' part is present in
the statement
output :Three address code
*/
void gen(char B[],char S1[],char S2[],int elsepart)
{
int Bt =101,Bf = 102,Sn =103;
printf("\n\tIf %s goto %d",B,Bt);
printf("\n\tgoto %d",Bf);
printf("\n%d: ",Bt);
printf("%s",S1);
if(!elsepart)
printf("\n%d: ",Bf);
else
{ printf("\n\tgoto %d",Sn);
printf("\n%d: %s",Bf,S2);
printf("\n%d:",Sn);
}
Bharath Bharadwaj B S
25
MIT,Mysore
Bharath Bharadwaj B S
26
MIT,Mysore
%token id
%left '+''/''*''-'
%%
E:S
{infix_postfix(str);}
S:S'+'T |S'-'T
|T
T:T'*'F| T'/'F
|F
F:id |'('S')'
;
%%
#include<stdio.h>
main()
{
printf("\nEnter an identifier:");
yyparse();
}
Bharath Bharadwaj B S
27
MIT,Mysore
yyerror()
{
printf("invalid\n");
}
yylex(){
char ch=' ';
while(ch!='\n'){
ch=getchar();
str[i++]=ch;
if(isalpha(ch)) return id;
if(ch=='+'||ch=='*'|| ch=='-'||ch=='/') return ch;}
str[--i]='\0';
return(0);
exit(0);
}
switch(ch)
{
Bharath Bharadwaj B S
28
MIT,Mysore
Bharath Bharadwaj B S
29
MIT,Mysore
if(isalnum(cursymb)==0)
{printf("Error in input!"); exit(0);}
postfix[++pptr]=cursymb;
}
}
while(top!=-1)
postfix[++pptr]=pop(stack,&top);
printf("%s\n",postfix);
}
OUTPUT:
$ yacc -d 6.y
$ cc y.tab.h
$ cc y.tab.c -ll
$ ./a.out
Enter an identifier:a+b-c
ab+c$ ./a.out
Enter an identifier:a/(b+c)*d
abc+/d*
$ ./a.out
Enter an identifier:a+1-2
invalid
Bharath Bharadwaj B S
30
MIT,Mysore