Compiler Design Lab
Compiler Design Lab
Compiler Design Lab
PROGRAM 1
Objective:Program to construct a DFA which accept the language L = {a nbm | n
mod 2=0, m≥1}
Input: a a b b b
Input: a a a b b b
#include <stdio.h>
#include <string.h>
int dfa = 0;
void start(char c)
{
if (c == 'a') {
dfa = 1;
}
else if (c == 'b') {
dfa = 3;
}
else {
dfa = -1;
}
}
void state1(char c)
{
if (c == 'a') {
dfa = 2;
}
else if (c == 'b') {
dfa = 4;
}
else {
dfa = -1;
}
}
void state2(char c)
{
if (c == 'b') {
dfa = 3;
}
else if (c == 'a') {
dfa = 1;
}
else {
dfa = -1;
}
}
void state3(char c)
{
if (c == 'b') {
dfa = 3;
}
else if (c == 'a') {
dfa = 4;
}
else {
dfa = -1;
}
}
void state4(char c)
{
dfa = -1;
}
else if (dfa == 1)
state1(str[i]);
else if (dfa == 2)
state2(str[i]);
else if (dfa == 3)
state3(str[i]);
else if (dfa == 4)
state4(str[i]);
else
return 0;
}
if (dfa == 3)
return 1;
else
return 0;
}
int main()
{
char str[] = "aaaaaabbbb";
if (isAccepted(str))
printf("ACCEPTED");
else
printf("NOT ACCEPTED");
return 0;
}
Test cases:
Input: a a b b b
Output: ACCEPTED
n = 2 (even) m=3 (>=1)
Input: a a a b b b
Output: NOT ACCEPTED
n = 3 (odd), m = 3
Input: a a a a
Output: NOT ACCEPTED
n = 4, m = 0( must be >=1)
PROGRAM 2
INPUT:
Enter an identifier: first
Enter an identifier:1aqw
#include<conio.h>
#include<stdio.h>
#include <ctype.h>
void main()
{
char a[10]; int flag, i=1;
clrscr();
printf("\n Enter an identifier:");
gets(a);
if(isalpha(a[0]))
flag=1;
else
printf("\n Not a valid identifier");
while(a[i]!='\0')
{ if(!isdigit(a[i])&&!isalpha(a[i]))
{
flag=0;
break;
}
i++;
}
if(flag==1)
printf("\n Valid identifier");
getch();
}
TEST CASE
Input: Enter an identifier: first
Output: Valid identifier
Enter an identifier:1aqw
Not a valid identifier
PROGRAM 3
INPUT:
Enter any operator: *
Enter any operator: +
Enter any operator: -
#include<stdio.h>
#include,conio.h>
void main()
{
char s[5];
clrscr();
printf("\n Enter any operator:");
gets(s);
switch(s[0])
{
case'>': if(s[1]=='=')
printf("\n Greater than or equal");
else printf("\n Greater than");
break;
case'<': if(s[1]=='=')
printf("\n Less than or equal");
else
printf("\nLess than");
break;
case'=': if(s[1]=='=')
printf("\nEqual to");
else
printf("\nAssignment");
break;
case'!': if(s[1]=='=')
printf("\nNot Equal");
else
printf("\n Bit Not");
break;
case'&': if(s[1]=='&')
printf("\nLogical AND");
else
printf("\n Bitwise AND");
break;
case'|': if(s[1]=='|')
printf("\nLogical OR");
else printf("\nBitwise OR");
break;
case'+': printf("\n Addition");
break;
case'-': printf("\nSubstraction");
break;
case'*': printf("\nMultiplication");
break;
case'/': printf("\nDivision");
break;
case'%': printf("Modulus");
break;
default: printf("\n Not a operator");
}
getch();
}
Test case:
Input Enter any operator: *
Output Multiplication
Input Enter any operator: +
Output Addition
Input Enter any operator: -
Output subtraction
program 4
Develope a Lexical Analzer to recognize a few patterns in C.
Input:
int
a
=
+
Code:
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
return (true);
return (false);
}
bool isOperator(char ch)
ch == '=')
return (true);
return (false);
return (false);
return (true);
!strcmp(str, "break") ||
return (true);
return (false);
if (len == 0)
return (false);
return (true);
if (len == 0)
return (false);
return (false);
if (str[i] == '.')
hasDecimal = true;
return (hasDecimal);
}
int i;
return (subStr);
if (isDelimiter(str[right]) == false)
right++;
right++;
left = right;
if (isKeyword(subStr) == true)
left = right;
return;
int main()
parse(str);
return (0);
Test Case1
‘int’ is a keyword
Test Case2
‘a’ is a valid identifier
Test Case2
‘=’ is an operator
Test Case4
‘+’ is an operator
Program 5
Write a program to eliminate left recursion from the given grammar.
INPUT:
E-EA/A
A-AT/a
T=a
#include<stdio.h>
#include<string.h>
#define SIZE 10
int main () {
char non_terminal;
char beta,alpha;
int num;
char production[10][SIZE];
int index=3; /* starting of the string following "->" */
printf("Enter Number of Production : ");
scanf("%d",&num);
printf("Enter the grammar as E->E-A :\n");
for(int i=0;i<num;i++){
scanf("%s",production[i]);
}
for(int i=0;i<num;i++){
printf("\nGRAMMAR : : : %s",production[i]);
non_terminal=production[i][0];
if(non_terminal==production[i][index]) {
alpha=production[i][index+1];
printf(" is left recursive.\n");
while(production[i][index]!=0 &&
production[i][index]!='|')
index++;
if(production[i][index]!=0) {
beta=production[i][index+1];
printf("Grammar without left recursion:\n");
printf("%c-
>%c%c\'",non_terminal,beta,non_terminal);
printf("\n%c\'-
>%c%c\'|E\n",non_terminal,alpha,non_terminal);
}
else
printf(" can't be reduced\n");
}
else
printf(" is not left recursive.\n");
index=3;
}
}
Test Case 1
E-AE’
E’-AE’/E
Test Case 2
A-aA’
A’-TA’/E
Test Case 3
Program 6
Objective: Write a program to eliminate left factor from the given grammar
Program 7
Objective: Write a program to compute first of non-terminals.
Input:
E -> TR
R -> +T R| #
T -> F Y
Y -> *F Y | #
F -> (E) | i
Code:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int count, n = 0;
if (xxx == 1)
continue;
// Function call
findfirst(c, 0, 0);
ptr += 1;
if (first[i] == calc_first[point1][lark])
{
chk = 1;
break;
}
}
if(chk == 0)
{
printf("%c, ", first[i]);
calc_first[point1][point2++] = first[i];
}
}
printf("}\n");
jm = n;
point1++;
}
printf("\n");
printf("-----------------------------------------------\n\n");
char donee[count];
ptr = -1;
// Checking if Follow of ck
// has alredy been calculated
for(kay = 0; kay <= ptr; kay++)
if(ck == donee[kay])
xxx = 1;
if (xxx == 1)
continue;
land += 1;
// Function call
follow(ck);
ptr += 1;
void follow(char c)
{
int i, j;