0% found this document useful (0 votes)
9 views4 pages

Compiler File 2

Uploaded by

Siddhartha Negi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Compiler File 2

Uploaded by

Siddhartha Negi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

COMPILER DESIGN LAB

Problem Statement 9:
Implementation of DFA accepting even number of a and b over input {a, b} with dead state.

Source code:
%s A B C F
%%
<INITIAL>\n printf(" accepted\n");BEGIN INITIAL;
<INITIAL>a BEGIN A;
<INITIAL>b BEGIN B;
<A>a BEGIN INITIAL;
<A>b BEGIN C;
<A>\n BEGIN INITIAL; printf(" not accepted\n");
<B>a BEGIN C;
<B>b BEGIN INITIAL;
<B>\n BEGIN INITIAL; printf(" not accepted\n");
<C>a BEGIN B;
<C>b BEGIN A;
<C>\n BEGIN INITIAL; printf(" not accepted\n");
<A>[^ab\n] BEGIN F;
<B>[^ab\n] BEGIN F;
<C>[^ab\n] BEGIN F;
<INITIAL>[^ab\n] BEGIN F;
<F>[^\n] BEGIN F;
<F>[\n] BEGIN INITIAL;printf("Invalid Input\n");
.;
%%
int main(){ printf("Enter the String of a and b only:\n");
yylex();
}

int yywrap(){
return 1;
}
Output:

Name: Danish Ansari Section: E Course: B.tech Roll No: 26


COMPILER DESIGN LAB

Problem Statement 10:


Design a DFA in LEX Code which accepts string containing third last element ‘a’ over input
alphabet {a, b}.
Source code:
%s A B C D E F G H
%%
<INITIAL>a BEGIN A;
<INITIAL>b BEGIN INITIAL;
<A>a BEGIN D;
<A>b BEGIN B;
<B>a BEGIN E;
<B>b BEGIN C;
<C>a BEGIN A;
<C>b BEGIN INITIAL;
<D>a BEGIN G;
<D>b BEGIN F;
<E>a BEGIN A;
<E>b BEGIN B;
<F>a BEGIN E;
<F>b BEGIN C;
<G>a BEGIN G;
<G>b BEGIN F;
<INITIAL>\n BEGIN INITIAL;printf("not accepted\n");
<A>\n BEGIN INITIAL; printf(" not accepted\n");
<B>\n BEGIN INITIAL; printf(" not accepted\n");
<C>\n BEGIN INITIAL; printf(" accepted\n");
<D>\n BEGIN INITIAL; printf(" not accepted\n");
<E>\n BEGIN INITIAL; printf(" accepted\n");
<F>\n BEGIN INITIAL; printf(" accepted\n");
<G>\n BEGIN INITIAL; printf(" accepted\n");
<INITIAL>[^ab\n] BEGIN H;
<A>[^ab\n] BEGIN H;
<B>[^ab\n] BEGIN H;
<C>[^ab\n] BEGIN H;
<D>[^ab\n] BEGIN H;
<E>[^ab\n] BEGIN H;
<F>[^ab\n] BEGIN H;
<G>[^ab\n] BEGIN H;
<H>[^\n] BEGIN H;
<H>[\n] BEGIN INITIAL; printf("Invalid Input\n");
%%
void main(){
printf("Enter the String of a and b only:\n");
yylex();

Name: Danish Ansari Section: E Course: B.tech Roll No: 26


COMPILER DESIGN LAB

int yywrap(){
return 1;
}
Output:

Name: Danish Ansari Section: E Course: B.tech Roll No: 26


COMPILER DESIGN LAB

Problem Statement 11:


Design a DFA in LEX Code to Identify and print Integer & Float Constants and Identifier

Source code:
%{
#include<stdio.h>
%}

%S A B C D
%%
<INITIAL>[\n] BEGIN INITIAL;
<INITIAL>[0-9]+ BEGIN A;
<INITIAL>[0-9]*[.][0-9]+ BEGIN B;
<INITIAL>[a-zA-Z_][a-zA-Z0-9_]* BEGIN C;
<INITIAL>[^\n] BEGIN D;
<A>\n BEGIN INITIAL;printf("VALID INTEGER\n");
<A>[^\n] BEGIN D;
<B>\n BEGIN INITIAL;printf("VALID FLOAT\n");
<B>[^\n] BEGIN D;
<C>\n BEGIN INITIAL;printf("VALID IDENTIFIER\n");
<C>[^\n] BEGIN D;
<D>\n BEGIN INITIAL; printf("INVALID INPUT\n");
<D>[^\n] BEGIN D;
%%

void main(){
printf("Enter the string: ");
yylex();
}
int yywrap(){
return 1;
}

Output:

Name: Danish Ansari Section: E Course: B.tech Roll No: 26

You might also like