1.i)Write a C program to simulate the following DFA.
The program should accept a
string from user and print 'valid' if it is accepted or 'invalid', if not accepted.
Consider that the input are {0,1}
#include <stdio.h>
int main()
{
char pat[50],*str ,ch;
int state = 1;
printf("enter pattern");
scanf("%s",pat);
str = pat;
while (*str)
{
/* code */
ch = *str;
switch(ch){
case '0':
if(state ==1)
state=1;
else
state=3;
break;
case '1':
if (state == 1)
state =2;
else
state = 3;
break; }
str++;
}
if (state ==2)
printf("valid");
else
printf("invalid");
}
*****************************************************
ii)Consider that identifies define a program language are as follows:-
Every identify is a combination of later (a-z,A-Z) and digit (0-9) only.
It can only start with a letter.
#include <stdio.h>
int main()
{
char check[50],*s,ch;
int state=0;
printf("Enter a pattern:");
scanf("%s",check);
s=check;
while(*s)
{
if(((*s>='a')&&(*s<='z'))||((*s>='A')&&(*s<='Z')))
{
if(state==0)
state=1;
else
state=1;
s=s+1;
}
else if ((*s>='0')&&(*s<='9'))
{
if(state==0)
state=0;
else
state=1;
s=s+1;
}
}
if(state==0)
printf("Invalid");
else
printf("Valid");
}
***************************************************************************
2. Write a lex program to recognise and display keyword, number and words in a
given statement.
%{
#include<stdio.h>
%}
%%
if |
else |
printf {printf("\n%s is a keyword",yytext);}
[0-9]+ {printf("\n%s is a number",yytext);}
[a-zA-Z]+ {printf("\n%s is a word",yytext);}
.|\n {ECHO;}
%%
int main()
{
printf("enter a string");
yylex();
}
int yywrap()
{
return 1;
}
*****************************************************************************
3.i) Write a lex program to display capital words from the given input string.
%{
#include<stdio.h>
%}
%%
[A-Z]+[\t\n ] { printf("%s",yytext); }
. ;
%%
int main( )
{
printf("Enter some string with capital words in between\n");
yylex();
}
int yywrap( )
{
return 1;
}
********************************************************************************
3)ii) write a program in lex to count the number of vowels and consonants.
%{
int vow_count=0;
int const_count =0;
%}
%%
[aeiouAEIOU] {vow_count++;}
[a-zA-Z] {const_count++;}
%%
int yywrap(){}
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;
}
***********************************************************************************
*
4)i) write a lex code to remove the comment from any C program given at runtime and
stored into out.c file.
/*Definition Section*/
%{
#include <stdio.h>
%}
/*Rule Section*/
%%
\/\/(.*) {};
\/\*(.*\n)*.*\*\/ {};
%%
/*call the yywrap function*/
int yywrap()
{
return 1;
}
/*Auxiliary function*/
/*Driver function*/
int main()
{
yyin=fopen("comm.c","r");
yyout=fopen("out.c","w");
/*call the yylex function.*/
yylex();
return 0;
}
******************************************************************************
4)ii) write a lex program to check whether a number is Armstrong number or not.
/* Lex program to check whether given - number is armstrong number or not */
%{
/* Definition section */
#include <math.h>
#include <string.h>
void check(char*);
%}
/* Rule Section */
%%
[0-9]+ check(yytext);
%%
int main()
{
/* yyin as pointer of File type */
extern FILE* yyin;
yyin = fopen("num", "r");
// The function that starts the analysis
yylex();
return 0;
}
void check(char* a)
{
int len = strlen(a), i, num = 0;
for (i = 0; i < len; i++)
num = num * 10 + (a[i] - '0');
int x = 0, y = 0, temp = num;
while (num > 0) {
y = pow((num % 10), len);
x = x + y;
num = num / 10;
}
if (x == temp)
printf("%d is armstrong number \n", temp);
else
printf("%d is not armstrong number\n", temp);
}
int yywrap()
{
return 1;
}
*********************************************************
5)i) write a lex program to check odd and even number
%{
/* Definition section */
#include<stdio.h>
int num, r, b=0, p=1;
%}
DIGIT [0-9]
/* Rule Section */
%%
{DIGIT}+ { num=atoi(yytext);
while (num > 0)
{
r= num % 2;
b+= r*p;
p*= 10;
num/= 2;
}
printf("%d", b);
}
.|\n ECHO;
%%
// driver code
int main()
{
yylex();
return 0;
}
int yywrap()
{
return 0;
}
*********************************************************
5)ii) Write a lex program for decimal to binary conversion
%{
/* Definition section */
#include<stdio.h>
int num, r, b=0, p=1;
%}
DIGIT [0-9]
/* Rule Section */
%%
{DIGIT}+ { num=atoi(yytext);
while (num > 0)
{
r= num % 2;
b+= r*p;
p*= 10;
num/= 2;
}
printf("%d", b);
}
.|\n ECHO;
%%
// driver code
int main()
{
yylex();
return 0;
}
int yywrap()
{
return 0;
}
**********************************************************************
5)iii) Write a lex program for binary to decimal conversion
%{
#include <stdio.h>
#include <math.h>
%}
%%
[01]+ {
int decimal = 0;
int length = yyleng;
int i;
for (i = 0; i < length; i++) {
if (yytext[i] == '1') {
decimal += pow(2, length - i - 1);
}
}
printf("Binary: %s -> Decimal: %d\n", yytext, decimal);
}
\n |
. {}
%%
int yywrap() {
return 1;
}
int main() {
printf("Enter a binary number:\n");
yylex();
return 0;
}
***********************************************************************
6)i) write a YACC program to recognise a valid arithmetic expression that user
operator +,-,*,/
LEX:
%{
#include"y.tab.h"
%}
%%
[0-9]+ {yylval=atoi(yytext); return NUMBER;}
[a-zA-Z]+ {return ID;}
[\t]+ ;
\n {return 0;}
. {return yytext[0];}
%%
int yywrap() {
return 1;
}
YACC:
%{
#include<stdio.h>
int flag = 1;
%}
%token NUMBER ID
%left '+' '-'
%left '*' '/'
%%
expr: expr '+' expr
| expr '-' expr
| expr '*' expr
| expr '/' expr
| '-'NUMBER
| '-'ID
|'('expr')'
|NUMBER
|ID
;
%%
int main()
{
printf("Enter the expression:\n");
yyparse();
if (flag)
printf("\nExpression valid");
else
printf("\nExpression invalid");
}
int yyerror(char *s)
{
flag = 0;
}
*************************************************************************
6)ii)Write a YACC program to implement a calculator and recognise a valid
arithmetic expression
LEX:
%{
/* Definition section */
#include<stdio.h>
#include "y.tab.h"
extern int yylval;
%}
/* Rule Section */
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap()
{
return 1;
}
YACC:
%{
/* Definition section */
#include<stdio.h>
int flag=0;
%}
%token NUMBER
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
/* Rule Section */
%%
ArithmeticExpression: E{
printf("\nResult=%d\n", $$);
return 0;
};
E :E'+'E {$$=$1+$3;}
|E'-'E {$$=$1-$3;}
|E'*'E {$$=$1*$3;}
|E'/'E {$$=$1/$3;}
|E'%'E {$$=$1%$3;}
|'('E')' {$$=$2;}
| NUMBER {$$=$1;}
;
%%
//driver code
void main()
{
printf("\nEnter Any Arithmetic Expression:\n");
yyparse();
if(flag==0)
printf("\nEntered arithmetic expression is Valid\n\n");
}
void yyerror()
{
printf("\nEntered arithmetic expression is Invalid\n\n");
flag=1;
}
************************************************************************
7) Write a YACC program for the following grammar:-
E-> E+T | T
T->T*F | F
F->(E) | id
LEX:
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
[a-zA-Z][0-9a-zA-Z]* { return ID; }
[\t] ;
[\n] {return 0;}
. {return yytext[0];}
%%
int yywrap()
{
return 1;
}
YACC:
%{
#include<stdio.h>
int flag=0;
%}
%token ID
%%
E : E'+'T {printf("Rule 1\n");}
| T {printf("Rule 2\n");}
;
T : T'*'F {printf("Rule 3\n");}
| F {printf("Rule 4\n");}
;
F : '('E')' {printf("Rule 5\n");}
| ID {printf("Rule 6\n");}
;
%%
void main()
{
printf("Enter arithmetic expression:\n");
yyparse();
if (flag==0)
printf("Valid\n");
}
int yyerror(char *s)
{
flag = 1;
printf("Invalid\n");
}
*******************************************************************************
8) write a YACC program to recognise string 'aaab','abbb','ab','a' using
grammar(a^n, b^n, n>1)
LEX:
%{
#include "y.tab.h"
%}
%%
a return A;
b return B;
.|\n return yytext[0];
%%
int yywrap()
{
return 1;
}
YACC:
%{
#include<stdio.h>
int valid=1;
%}
%token A B
%%
str:S'\n' {return 0;}
S:A S B
|
;
%%
main()
{
printf("Enter the string:\n");
yyparse();
if(valid==1)
printf("\nvalid string");
}
int yyerror(char *msg)
{
printf("invalid string\n");
exit(0);
}
****************************************************************************
n) Program to recognize nested IF control statements and display the levels of
nesting.
LEX:
%{
#include "y.tab.h"
%}
%%
"if" {return IF;}
[sS][0-9]* {return S;}
"<"|">"|"=="|"<="|">="|"!=" {return RELOP;}
[0-9]+ {return NUMBER;}
[a-z][a-zA-Z0-9_]* {return ID;}
\n {return NL;}
. {return yytext[0];}
%%
YACC:
%{
#include<stdio.h>
#include<stdlib.h>
int count=0;
%}
%token IF RELOP S NUMBER ID NL
%%
stmt: if_stmt NL {printf("No. of nested if statements=%d\n",count);exit(0);}
;
if_stmt : IF'('cond')''{'if_stmt'}' {count++;}
|S
;
cond: x RELOP x
;
x:ID | NUMBER
;
%%
int yyerror(char *msg)
{
printf("the statement is invalid\n");
exit(0);
}
main()
{
printf("enter the statement\n");
yyparse();
}