Practical No – 01
Program 1 –
Aim – Write a program to recognize constants, keywords and identifiers.
Code and Output –
%{
#include<stdio.h>
%}
%%
if|else|for|char|while|int|switch {printf("keyword");}
[a-z]([a-z])*([0-9])* {printf("Identifier");}
[0-9]* {printf("Constant");}
.* {printf("Invalid");}
%%
int main()
{
yylex();
return 0;
}
int yywrap()
{
return 0;
}
/*
OUTPUT:-
sumit@Sumitts-MBP downloads % flex identify.l
sumit@Sumitts-MBP downloads % gcc lex.yy.c
sumit@Sumitts-MBP downloads % ./a.out
1
Constant
int
keyword
a12
Identifier
*/
Program 2 –
Aim – Program to accept number/String and count length using macro(Count no. of
words).
Code and Output –
/*Count number of words*/
%{
#include<stdio.h>
int i;
%
%%
([A-Za-z0-9])* {i++;}
[\n] {printf("%d",i);} {return 0;}
%%
int main()
{
printf("Value of i: %d\n",i);
yylex();
printf("\nValue of i: %d\n",i);
return 0;
}
int yywrap()
{
return 0;
}
/*
Output:
mgm02@MGM02s-iMac Desktop % flex words.l
mgm02@MGM02s-iMac Desktop % gcc lex.yy.c
mgm02@MGM02s-iMac Desktop % ./a.out
Value of i: 0
Arjan Singh Johar
3
Value of i: 3
*/
Program 3 –
Aim – Lex program to count number of vowels and consonant
Code and Output –
%{
int vow_count=0;
int const_count =0;
%}
%%
[aeiouAEIOU] {vow_count++;}
[a-zA-Z] {const_count++;}
%%
int yywrap()
{
return 0;
}
int main()
{
printf("Enter the string of vowels and consonants:");
yylex();
printf("Number of vowels are: %d\n", vow_count);
printf("Number of consonants are: %d\n", const_count);
return 0;
}
/*
Output:
mgm02@MGM02s-iMac Desktop % flex vowels.l
mgm02@MGM02s-iMac Desktop % gcc lex.yy.c
mgm02@MGM02s-iMac Desktop % ./a.out
Enter the string of vowels and consonants:Arjan
Number of vowels are:2
Number of consonants are:3
*/
Program 4 –
Aim – Lex program to count the type of numbers (Positive Integer, Fraction/Negative
Integer, Fraction)
Code and Output –
%{
int postiveno=0;
int negtiveno=0;
int positivefractions=0;
int negativefractions=0;
%}
DIGIT [0-9]
%%
\+?{DIGIT}+ postiveno++;
-{DIGIT}+ negtiveno++;
\+?{DIGIT}*\.{DIGIT}+ positivefractions++;
-{DIGIT}*\.{DIGIT}+ negativefractions++;
.;
%%
int main()
{
yylex();
printf("\nNo. of positive numbers: %d", postiveno);
printf("\nNo. of Negative numbers: %d", negtiveno);
printf("\nNo. of Positive numbers in fractions: %d", positivefractions);
printf("\nNo. of Negative numbers in fractions: %d\n", negativefractions);
return 0;
}
int yywrap()
{
return 0;
}
/*
Output:
mgm02@MGM02s-iMac Desktop % flex vowels.l
mgm02@MGM02s-iMac Desktop % gcc lex.yy.c
mgm02@MGM02s-iMac Desktop % ./a.out
1 2 3 -4 -5 6.5 7.5
No. of positive numbers: 3
No. of Negative numbers: 2
No. of Positive numbers in fractions: 2
No. of Negative numbers in fractions: 0
*/
Program 5 –
Aim – DFA in LEX code which accepts strings ending with 11
Code and Output –
%{
#include<stdio.h>
%}
%s A B DEAD
%%
<INITIAL>1 BEGIN A;
<INITIAL>0 BEGIN INITIAL;
<INITIAL>[^01\n] BEGIN DEAD;
<INITIAL>\n BEGIN INITIAL; {printf("Not Accepted\n");}
<A>1 BEGIN B;
<A>0 BEGIN INITIAL;
<A>[^01\n] BEGIN DEAD;
<A>\n BEGIN INITIAL; {printf("Not Accepted\n");
<B>1 BEGIN B;
<B>0 BEGIN INITIAL;
<B>[^01\n] BEGIN DEAD;
<B>\n BEGIN INITIAL; {printf("Accepted\n");}
<DEAD>[^\n] BEGIN DEAD;
<DEAD>\n BEGIN INITIAL; {printf("Invalid\n");}
%%
int main()
{
printf("Enter String\n");
yylex();
return 0;
}
int yywrap()
{
return 1;
}
/*
sumit@Sumitts-MBP downloads % flex string11.l
sumit@Sumitts-MBP downloads % gcc lex.yy.c
sumit@Sumitts-MBP downloads % ./a.out
Enter String
001
Not Accepted
01
Not Accepted
011
Accepted
0011
Accepted
6911
Invalid
*/
Practical No – 02
Program 1 –
Aim – Program to identify REAL PRECISION of the given number
Code and Output –
%{
#include<stdio.h>
int c=0;
%}
%%
[0-9]+[.][0-9]+ {printf("%s\n",index(yytext,'.') + 1);}
[\n] {return 0;}
%%
int main()
{
yylex();
return 0;
}
int yywrap()
{
return 1;
}
/*
Output:
mgm02@MGM02s-iMac Desktop % flex Precision.l
mgm02@MGM02s-iMac Desktop % gcc lex.yy.c
mgm02@MGM02s-iMac Desktop % ./a.out
10.23
23
*/
Program 2 –
Aim – Write a program using LEX to recognize a valid arithmetic expression and to
recognize the identifiers and operators present. Print them separately
Code and Output –
%{
#include<stdio.h>
#include<string.h>
int flag=0,i=0,j,k=0;
char operand[20][20],oparator[20][20];
%}
%%
[a-zA-Z0-9]+ {flag++; strcpy(operand[i],yytext); i++;}
[-+*/] {flag--; strcpy(oparator[k],yytext); k++;}
%%
int main()
{
printf("enter an arithmetic expression");
yylex();
if(flag!=1)
printf("Invalid expression\n");
else
{
printf("Valid expression\n");
printf("The operands are\t");
for(j=0;j<i;j++)
printf("%s\t",operand[j]);
printf("\nThe operators are\t");
for(j=0;j<k;j++)
printf("%s\t",oparator[j]);
printf("\n");
}
return 1;
}
int yywrap()
{
return 1;
}
/*
Output:
mgm02@MGM02s-iMac Desktop % flex Arithmatic.l
mgm02@MGM02s-iMac Desktop % gcc lex.yy.c
mgm02@MGM02s-iMac Desktop % ./a.out
enter an arithmetic expression a+b*c
Valid expression
The operands are a b c
The operators are + *
*/
Program 3 –
Aim – Program to check valid expression and valid mobile no.
Code and Output –
Email Id –
%{
#include<stdio.h>
int f=0;
%}
%%
[a-z.0-9]+@[a-z]+[.]"com"|"org"|"in" {f=1;}
[\n] {return 0;}
%%
int main()
{
printf("enter the mail id ");
yylex();
if(f==1)
{
printf("the mail is valid");
}
else
{
printf("the mail is not valid");
}
return 0;
}
int yywrap()
{
return 1;
}
/*
Output:
mgm02@MGM02s-iMac Desktop % flex emailid.l
mgm02@MGM02s-iMac Desktop % gcc lex.yy.c
mgm02@MGM02s-iMac Desktop % ./a.out
enter the mail id johar.arjan5@gmail.com
the mail is valid%
*/
Mobile No –
%{
#include<stdio.h>
int f=0;
%}
%%
[7-9][0-9]{9} {f=1;}
[\n] {return 0;}
%%
int main()
{
printf("enter the mobile number");
yylex();
if(f==1)
{
printf("the mobile number is valid");
}
else
{
printf("the mobile number is not valid");
}
return 0;
}
int yywrap()
{
return 1;
}
/*
Output:
mgm02@MGM02s-iMac Desktop % flex mobileno.l
mgm02@MGM02s-iMac Desktop % gcc lex.yy.c
mgm02@MGM02s-iMac Desktop % ./a.out
enter the mobile number9823920630
the mobile number is valid%
*/
Program 4 –
Aim – Lex program to implement a simple Calculator
Code and Output –
%{
#include <stdio.h>
#include <stdlib.h>
int op = 0, i;
float a, b;
void digi();
%}
dig [0-9]+|([0-9]*)"."([0-9]+)
add "+"
sub "-"
mul "*"
div "/"
pow "^"
ln \n
%%
{dig} {digi();}
{add} {op=1;}
{sub} {op=2;}
{mul} {op=3;}
{div} {op=4;}
{pow} {op=5;}
{ln} {printf("\n The Answer :%f\n\n",a);}
%%
void digi()
{
if (op == 0)
{
/* atof() is used to convert
the ASCII input to float */
a = atof(yytext);
}
else
{
b = atof(yytext);
switch (op)
{
case 1: a = a + b; break;
case 2: a = a - b; break;
case 3: a = a * b; break;
case 4: a = a / b; break;
case 5:
for (i = a; b > 1; b--)
a = a * i;
break;
}
op = 0;
}
}
int main(int argc, char *argv[])
{
yylex();
return 0;
}
int yywrap()
{
return 1;
}
/*
Output:
arjan@Arjan:~$ flex calculator.l
arjan@Arjan:~$ gcc lex.yy.c
arjan@Arjan:~$ ./a.out
7+8
The Answer :15.000000
8*8
The Answer :64.000000
*/
Program 5 –
Aim – Lex program to count number of line ,char, words, tab, space
Code and Output –
%{
#include<stdio.h>
#include<string.h>
int lc=0,tc=0,ch=0,sc=0;
%}
%%
\n lc++;
([ ]) sc++;
\t tc++;
. ch++;
%%
int main()
{
yylex();
printf("Number of lines are %d\n",lc);
printf("Number of spaces are %d\n",sc);
printf("Number of tabs are %d\n",tc);
printf("Number of characters are %d\n",ch);
return 0;
}
/*
Output:
arjan@Arjan:~$ flex count.l
arjan@Arjan:~$ gcc lex.yy.c
arjan@Arjan:~$ ./a.out
Hello this is Arjan
studying in MGM'S JNEC
Situated in Aurangabad
Number of lines are 2
Number of spaces are 8
Number of tabs are 0
Number of characters are 55
*/
Practical No – 03
Program 1 –
Aim – Program in YACC to recognize the language (a^n b , n>=10).
Code and Output –
Anb.l –
%{
#include "y.tab.h"
%}
A [a]
B [b]
%%
{A} {return A;}
{B} {return B;}
[\n] {return 0;}
%%
int yywrap()
{
return 1;
}
Anb.y –
%{
#include<stdio.h>
#include<stdlib.h>
int yylex();
int yyerror();
%}
%token A B
%%
expr : t B
;
t:tA
|A
;
%%
int main()
{
printf("enter the string in a^nb format");
yyparse();
printf("valid string");
return 0;
int yyerror()
{
printf("Invalid String");
exit(0);
}
/*
Output:
mgm02@MGM02s-iMac Downloads % lex anbn.l
mgm02@MGM02s-iMac Downloads % yacc -d anbn.y
mgm02@MGM02s-iMac Downloads % gcc lex.yy.c y.tab.c -o pract3
mgm02@MGM02s-iMac Downloads % ./pract3
enter the string in a^nb formataaab
valid string%
*/
Program 2 –
Aim – Program in YACC to recognize the language (a^n b^n , n>=10).
Code and Output –
Anbn.l –
%{
#include "y.tab.h"
%}
A [a]
B [b]
%%
{A} {return A;}
{B} {return B;}
\n {return 0;}
%%
int yywrap()
{
return 1;
}
Anbn.y –
%{
#include<stdlib.h>
#include<stdio.h>
int yylex();
int yyerror();
%}
%token A B
%%
expr:A s B
;
s:A s B |
;
%%
int main()
{
printf("Enter a String:");
yyparse()
printf("Valid String");
return 0;
}
int yyerror()
{
printf("Invalid String");
exit (0);
}
Output:
arjan@Arjan:~$ lex anbn.l
arjan@Arjan:~$ yacc -d anbn.y
arjan@Arjan:~$ gcc lex.yy.c y.tab.c -o anbn
arjan@Arjan:~$ ./anbn
Enter a String:aabb
Valid String
Enter a String:aab
Invalid String
*/
Program 3 –
Aim – Program in YACC to perform bitwise calculator.
Code and Output –
Calc.l –
%{
#include <stdio.h>
#include <stdlib.h>
#include "y.tab.h" // Include the generated Yacc header
%}
DIGIT [0-9]
OPERATOR [&|^~]
%%
"and" { return AND; }
"or" { return OR; }
"xor" { return XOR; }
"not" { return NOT;
{DIGIT}+ { return NUMBER; } // Return the NUMBER token directly
{OPERATOR} { return yytext[0]; }
[ \t\n] ; // Skip whitespace
. { return yytext[0]; }
%%
int yywrap() {
return 1;
}
Calc.y –
%{
#include <stdio.h>
#include <stdlib.h>
%
%token NUMBER
%token AND OR XOR NOT
%left '&' '|
%left '^'
%right UNARY
%%
statement : expression { printf("Result: %d\n", $1); }
;
expression : NUMBER { $$ = $1;
| expression '&' expression { $$ = $1 & $3; }
| expression '|' expression { $$ = $1 | $3; }
| expression '^' expression { $$ = $1 ^ $3; }
| NOT expression %prec UNARY { $$ = ~$2; }
;
%%
int main()
yyparse();
return 0;
}
void yyerror(const char* msg) {
fprintf(stderr, "Error: %s\n", msg);}
/*
Output:
arjan@Arjan:~$ lex calc.l
arjan@Arjan:~$ yacc -d calc.y
arjan@Arjan:~$ gcc lex.yy.c y.tab.c -o calc
arjan@Arjan:~$ ./calc
10|6
Result:14
*/
Practical No – 05
Program –
Aim – C program to find First() of non-terminals.
Code and Output –
#include<stdio.h>
#include<ctype.h>
void FIRST(char );
int count,n=0;
char prodn[10][10], first[10];
int main()
{
int i,choice;
char c,ch;
printf("How many productions ? :");
scanf("%d",&count);
printf("Enter %d productions epsilon= $ :\n\n",count);
for(i=0;i<count;i++)
scanf("%s%c",prodn[i],&ch);
do
{
n=0;
printf("Element :");
scanf("%c",&c);
FIRST(c);
printf("\n FIRST(%c)= { ",c);
for(i=0;i<n;i++)
printf("%c ",first[i]);
printf("}\n");
printf("press 1 to continue : ");
scanf("%d%c",&choice,&ch);
}
while(choice==1);
return 0;
}
void FIRST(char c)
{
int j;
if(!(isupper(c)))first[n++]=c;
for(j=0;j<count;j++)
{
if(prodn[j][0]==c)
{
if(prodn[j][2]=='$') first[n++]='$';
else if(islower(prodn[j][2]))first[n++]=prodn[j][2];
else FIRST(prodn[j][2]);
}
}
}
/*
mgm07@MGM07s-iMac Desktop % gcc -o First First.c
mgm07@MGM07s-iMac Desktop % ./First
How many productions ? :5
Enter 5 productions epsilon= $ :
S=ABCD
A=b
B=c
C=d
D=e
Element :S
FIRST(S)= { b }
press 1 to continue : 1
Element :B
FIRST(B)= { c }
*/
Practical No – 06
Program –
Aim – C program to find Follow() of non-terminals.
Code and Output –
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
void follow(char c);
void first(char c);
int main()
{
int i,z;
char c,ch;
printf("Enter the no.of productions:");
scanf("%d",&n);
printf("Enter the productions(epsilon=$):\n");
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
do
{
m=0;
printf("Enter the element whose FOLLOW is to be found:");
scanf("%c",&c);
follow(c);
printf("FOLLOW(%c) = { ",c);
for(i=0;i<m;i++)
printf("%c ",f[i]);
printf(" }\n");
printf("Do you want to continue(0/1)?");
scanf("%d%c",&z,&ch);
}
while(z==1);
}
void follow(char c)
if(a[0][0]==c)f[m++]='$';
for(i=0;i<n;i++)
{
for(j=2;j<strlen(a[i]);j++)
{
if(a[i][j]==c)
{
if(a[i][j+1]!='\0')first(a[i][j+1]);
if(a[i][j+1]=='\0'&&c!=a[i][0])
follow(a[i][0])
}
}
}
}
void first(char c)
{
int k;
if(!(isupper(c)))f[m++]=c;
for(k=0;k<n;k++)
{
if(a[k][0]==c)
{
if(a[k][2]=='$') follow(a[i][0]);
else if(islower(a[k][2]))f[m++]=a[k][2];
else first(a[k][2]);
}
}
}
/*
mgm07@MGM07s-iMac Desktop % gcc -o Follow Follow.c
mgm07@MGM07s-iMac Desktop % ./Follow
Enter the no.of productions:5
Enter the productions(epsilon=$):
S=ABCD
A=b
B=c
C=d
D=e
Enter the element whose FOLLOW is to be found:S
FOLLOW(S) = { $ }
Do you want to continue(0/1)?1
Enter the element whose FOLLOW is to be found:C
FOLLOW(C) = { e }
Do you want to continue(0/1)?0
*/
Practical No – 04
Program –
Aim – Implementation of LR Parser.
Code and Output –
#include<stdio.h>
int main(){
char a[10]="S' -> .S";
char b[10]="S -> .AA";
char c[10]="A -> .AA";
char d[10]="A -> .b";
char table[10][10][10] = {
{"shift3","shift4"," ","1","2",""},
{" "," ","accept"," "," "},
{"shift3","shift4"," "," ","5"},
{"shift3","shift4"," "," ","6"},
{"reduce3","reduce3","reduce3"," "," "},
{"reduce1","reduce1","reduce1"," "," "},
{"reduce2","reduce2","reduce2"," "," "}
};
printf("The production rules are:");
printf("\n%s\n%s\n%s\n%s\n",a,b,c,d);
printf("The LRO table is:\n");
printf("\tAction\t\t\tGoto\n");
printf(" a\tb\t$\tS\tA");
for(int i = 0 ; i < 7 ; i++)
{
printf("\nI%d ",i+1);
for(int j = 0 ; j<5 ; j++)
{
printf("%s\t",table[i][j]);
}
}
printf("\nAs there are no Conflicts in the table This table is LRO");
return 0;
}
/*
Output:
mgm02@MGM02s-iMac ~ % cd Desktop
mgm02@MGM02s-iMac Desktop % gcc -o LRO.c LRO.c
mgm02@MGM02s-iMac Desktop % ./LRO.c
The production rules are:
S' -> .S
S -> .AA
A -> .AA
A -> .b
The LRO table is:
Action Goto
ab $ S A
I1 shift3 shift4 1 2
I2 accept
I3 shift3 shift4 5
I4 shift3 shift4 6
I5 reduce3 reduce3 reduce3
I6 reduce1 reduce1 reduce1
I7 reduce2 reduce2 reduce2
As there are no Conflicts in the table This table is LRO% */