CD _Lab Manual
CD _Lab Manual
Code:
#include<stdio.h>
#define max 100
int main()
{
char str[max], f='a';
int i;
printf("Enter string: ");
scanf("%s", str); for(i=0;str[i]!
='\0';i++)
{
switch(f)
{
case 'a': if(str[i]=='0')
{
f='b';
}
else if(str[i]=='1')
{
f='a';
}
break;
case 'b': if(str[i]=='0')
{
f='b';
}
else if(str[i]=='1')
{
f='c';
}
break;
case 'c': if(str[i]=='0')
{
f='b';
}
else if(str[i]=='1')
{
f='a';
}
break;
}
}
if(f=='c')
{
printf("\nString is accepted");
}
else
{
printf("\nString not accepted");
}
return 0;
}
Output:
PRACTICAL – 2
Introduction to Lex Tool.
LEX
➢ Lex is a program that generates lexical analyzer. It is used with YACC parser generator.
➢ The lexical analyzer is a program that transforms an input stream into a sequence of tokens.
➢ It reads the input stream and produces the source code as output through implementing the
lexical analyzer in the C program.
➢ Firstly lexical analyzer creates a program lex.1 in the Lex language. Then Lex compiler runs
the lex.1 program and produces a C program lex.yy.c.
➢ Finally C compiler runs the lex.yy.c program and produces an object program a.out.
➢ a.out is lexical analyzer that transforms an input stream into a sequence of tokens.
2. %%
3. { rules }
4. %%
5. { user subroutines }
Definitions include declarations of constant, variable and regular definitions.
Rules define the statement of form p1 {action1} p2 {action2}....pn {action}.
Where pi describes the regular expression and action1 describes the actions what action the
lexical analyzer should take when pattern pi matches a lexeme.
PRACTICAL – 3
%{
#include<stdio.h>
#include<string.h>
int i=0;
%}
/*Rules Section*/
%%
([a-zA-Z0-9])* {i++;}
"\n" {printf("%d\n",i);i=0;}
%%
int main()
{
yylex();
return 0;
}
Output:
b. Ceasor Cypher:
%%
[a-z] { char ch = yytext[0];
ch += 3;
if (ch > 'z') ch = ch - ('z' + 1 - 'a');
printf("Encrypted character is :%c ",ch);
}
[A-Z] { char ch = yytext[0];
ch +=3;
if (ch > 'Z') ch = ch - ('Z' + 1 - 'A');
printf("Encrypted character is:%c",ch);
}
%%
int main()
{
printf("Enter input:");
yylex();
return 0;
}
Output:
%{
#include<stdio.h>
%}
%%
\/\/(.*) {};
\/\*(.*\n)*.*\*\/ {};
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("ip.c","r");
yyout=fopen("out.c","w");
yylex();
return 0;
}
Input:
#include<stido.h>
#include<conio.h>
int main()
{
int a=10;//this is interger value
char name = "Avani”;
float b =10.0;
/*convert integer value in float*/
printf("hello this is second program");
return 0;
}
Output:
#include<stido.h>
#include<conio.h>
int main()
{
int a=10;
char name = "Avani";
float b =10.0;
\n {
printf("Roman Numeral: %s, Decimal Value: %d\n", yytext, decimalValue);
decimalValue = 0; // Reset the value for the next input
}
.{
/* Ignore any other characters */
}
%%
int main() {
yylex();
return 0;
}
Output:
Output:
Code:
%{
#include<stdio.h>
int flag=0;
%}
%%
and |or |but |because |if |then |nevertheless
{ flag=1; }
. ;
\n { return 0; }
%%
int main()
{
printf("Enter the sentence:\n");
yylex();
if(flag==0)
printf("Simple sentence\n");
else
printf("compound sentence\n");
}
int yywrap( )
{
return 1;
}
Input:
Output:
PRACTICAL – 5
Implementation of Recursive Descent Parser without
backtracking Input: The string to be parsed.
Output: Whether string parsed successfully or not.
Explanation: Students have to implement the recursive procedure for RDP for a
typical grammar. The production no. are displayed as they are used to derive
the string.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
char input[100];
int i,l;
void main()
{
printf("\nRecursive descent parsing for the following grammar\n");
printf("\nE -> T + E / T \n T -> F * T / F \n F -> ( E ) / i\n"); printf("\
nEnter the string to be checked:");
gets(input);
if(E())
{
if(input[i+1]=='\0')
printf("\nString is accepted");
else
printf("\nString is not accepted");
}
else
printf("\nString not accepted");
}
E()
{
if(T())
{
if(EP())
return(1);
else
return(0);
}
else
return(0);
}
EP()
{
if(input[i]=='+')
{
i++;
if(T())
{
if(EP())
return(1);
else
return(0);
}
else
return(0);
}
else
return(1);
}
T()
{
if(F())
{
if(TP())
return(1);
else
return(0);
}
else
return(0);
}
TP()
{
if(input[i]=='*')
{
i++;
if(F())
{
if(TP())
return(1);
else
return(0);
}
else
return(0);
}
else
return(1);
}
F()
{
if(input[i]=='(')
{
i++;
if(E())
{
if(input[i]==')')
{
i++;
return(1);
}
else
return(0);
}
else
return(0);
}
else if(input[i]>='a'&&input[i]<='z'||input[i]>='A'&&input[i]<='Z')
{
i++;
return(1);
}
else
return(0);
}
Output:
Practical-6
Extract Predecessor and Successor from given Control Flow Graph.
Code:
#include <stdio.h>
#include <stdlib.h>
// BST Node
typedef struct Node {
int key;
struct Node *left, *right;
} Node;
if (suc != NULL) {
printf("Successor is %d\n", suc->key);
} else {
printf("No Successor\n");
}
return 0;
}
Output:
Practical-7
Introduction to YACC and generate Calculator Program.
Code:
P14.l
%{
#include<stdio.h>
#include "y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap()
{
return 1;
}
P14.y
%{
int yylex(void);
int yyparse(void);
void yyerror();
#include<stdio.h>
%}
%token NUMBER
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
%%
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;}
;
%%
void main()
{
printf("\nEnter Any Arithmetic Expression:\n");
yyparse();
}
void yyerror()
{
printf("\nEntered arithmetic expression is Invalid\n\n");
}
Output:
Practical-8
Implement a C program for constructing LL (1)
parsing Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void push(char *,int *,char);
char stacktop(char *);
void isproduct(char,char);
int ister(char);
int isnter(char);
int isstate(char);
void error();
void isreduce(char,char);
char pop(char *,int *);
void printt(char *,int *,char [],int);
void rep(char [],int);
struct action
{
char row[6][5];
};
const struct action A[12]={
{"sf","emp","emp","se","emp","emp"},
{"emp","sg","emp","emp","emp","acc"},
{"emp","rc","sh","emp","rc","rc"},
{"emp","re","re","emp","re","re"},
{"sf","emp","emp","se","emp","emp"},
{"emp","rg","rg","emp","rg","rg"},
{"sf","emp","emp","se","emp","emp"},
{"sf","emp","emp","se","emp","emp"},
{"emp","sg","emp","emp","sl","emp"},
{"emp","rb","sh","emp","rb","rb"},
{"emp","rb","rd","emp","rd","rd"},
{"emp","rf","rf","emp","rf","rf"}
};
struct gotol
{
char r[3][4];
};
const struct gotol G[12]={
{"b","c","d"},
{"emp","emp","emp"},
{"emp","emp","emp"},
{"emp","emp","emp"},
{"i","c","d"},
{"emp","emp","emp"},
{"emp","j","d"},
{"emp","emp","k"},
{"emp","emp","emp"},
{"emp","emp","emp"},
};
char ter[6]={'i','+','*',')','(','$'};
char nter[3]={'E','T','F'};
char states[12]={'a','b','c','d','e','f','g','h','m','j','k','l'};
char stack[100];
int top=-1;
char temp[10];
struct grammar
{
char left;
char right[5];
};
const struct grammar rl[6]={
{'E',"e+T"},
{'E',"T"},
{'T',"T*F"},
{'T',"F"},
{'F',"(E)"},
{'F',"i"},
};
void main()
{
char inp[80],x,p,dl[80],y,bl='a';
int i=0,j,k,l,n,m,c,len;
printf(" Enter the input :");
scanf("%s",inp);
len=strlen(inp);
inp[len]='$';
inp[len+1]='\0';
push(stack,&top,bl); printf("\
n stack \t\t\t input");
printt(stack,&top,inp,i);
do
{
x=inp[i];
p=stacktop(stack);
isproduct(x,p);
if(strcmp(temp,"emp")==0)
error();
if(strcmp(temp,"acc")==0)
break;
else
{
if(temp[0]=='s')
{
push(stack,&top,inp[i]);
push(stack,&top,temp[1]); i+
+;
}
else
{
if(temp[0]=='r')
{
j=isstate(temp[1]);
strcpy(temp,rl[j-2].right);
dl[0]=rl[j-2].left;
dl[1]='\0';
n=strlen(temp);
for(k=0;k<2*n;k++)
pop(stack,&top);
for(m=0;dl[m]!='\0';m++)
push(stack,&top,dl[m]);
l=top;
y=stack[l-1];
isreduce(y,dl[0]);
for(m=0;temp[m]!='\0';m++)
push(stack,&top,temp[m]);
}
}
}
printt(stack,&top,inp,i);
}while(inp[i]!='\0');
if(strcmp(temp,"acc")==0)
printf(" \n accept the input ");
else
printf(" \n do not accept the input ");
}
void push(char *s,int *sp,char item)
{
if(*sp==100)
printf(" stack is full ");
else
{
*sp=*sp+1;
s[*sp]=item;
}
}
char stacktop(char *s)
{
char i;
i=s[top];
return i;
}
void isproduct(char x,char p)
{
int k,l;
k=ister(x);
l=isstate(p);
strcpy(temp,A[l-1].row[k-1]);
}
int ister(char x)
{
int i;
for(i=0;i<6;i++)
if(x==ter[i])
return i+1;
return 0;
}
int isnter(char x)
{
int i;
for(i=0;i<3;i++)
if(x==nter[i])
return i+1;
return 0;
}
int isstate(char p)
{
int i; for(i=0;i<12;i+
+) if(p==states[i])
return i+1;
return 0;
}
void error()
{
printf(" error in the input ");
exit(0);
}
void isreduce(char x,char p)
{
int k,l;
k=isstate(x);
l=isnter(p);
strcpy(temp,G[k-1].r[l-1]);
}
char pop(char *s,int *sp)
{
char item;
if(*sp==-1)
printf(" stack is empty ");
else
{
item=s[*sp];
*sp=*sp-1;
}
return item;
}
void printt(char *t,int *p,char inp[],int i)
{
int r;
printf("\n");
for(r=0;r<=*p;r++)
rep(t,r); printf("\t\t\
t");
for(r=i;inp[r]!='\0';r++)
printf("%c",inp[r]);
}
void rep(char t[],int r)
{
char c;
c=t[r];
switch(c)
{
case 'a': printf("0");
break;
case 'b': printf("1");
break;
case 'c': printf("2");
break;
case 'd': printf("3");
break;
case 'e': printf("4");
break;
case 'f': printf("5");
break;
case 'g': printf("6");
break;
case 'h': printf("7");
break;
case 'm': printf("8");
break;
case 'j': printf("9");
break;
case 'k': printf("10");
break;
case 'l': printf("11");
break;
default :printf("%c",t[r]);
break;
}
}
Output:
Practical – 09
Implement a C program to implement LALR parsing.
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct action {
char row[6][5];
};
struct gotol {
char r[3][4];
};
struct grammar {
char left;
char right[5];
};
void main() {
char inp[80], x, p, dl[80], y, bl = 'a';
int i = 0, j, k, l, n, m, c, len;
printf("Enter the input: ");
scanf("%s", inp);
len = strlen(inp);
inp[len] = '$';
inp[len + 1] = '\0';
do {
x = inp[i];
p = stacktop(stack);
isproduct(x, p);
if (strcmp(temp, "emp") == 0) {
error();
}
if (strcmp(temp, "acc") == 0) {
break;
} else {
if (temp[0] == 's') {
push(stack, &top, inp[i]);
push(stack, &top, temp[1]);
i++;
} else if (temp[0] == 'r') {
j = isstate(temp[1]);
strcpy(temp, rl[j - 2].right);
dl[0] = rl[j - 2].left;
dl[1] = '\0';
n = strlen(temp);
l = top;
y = stack[l - 1];
isreduce(y, dl[0]);
if (strcmp(temp, "acc") == 0) {
printf("\nAccept the input");
} else {
printf("\nDo not accept the input");
}
getch();
}
int ister(char x) {
for (int i = 0; i < 6; i++) {
if (x == ter[i]) {
return i + 1;
}
}
return 0;
}
int isnter(char x) {
for (int i = 0; i < 3; i++) {
if (x == nter[i]) {
return i + 1;
}
}
return 0;
}
int isstate(char p) {
for (int i = 0; i < 12; i++) {
if (p == states[i]) {
return i + 1;
}
}
return 0;
}
void error() {
printf("Error in the input");
exit(0);
}
switch(c)
{
case '+':return 0;
case '-':return 1;
case '*':return 2;
case '/':return 3;
case '^':return 4;
case 'i':return 5;
case '(':return 6;
case ')':return 7;
case '$':return 8;
}
}
int shift()
{
stack[++top]=*(input+i++);
stack[top+1]='\0';
}
int reduce()
{
int i,len,found,t; for(i=0;i<5;i+
+)//selecting handles
{
len=strlen(handles[i]); if(stack[top]==handles[i]
[0]&&top+1>=len)
{
found=1;
for(t=0;t<len;t++)
{
if(stack[top-t]!=handles[i][t])
{
found=0;
break;
}
}
if(found==1)
{
stack[top-t+1]='E';
top=top-t+1;
strcpy(lasthandle,handles[i]);
stack[top+1]='\0';
return 1;//successful reduction
}
}
}
return 0;
}
void dispstack()
{
int j;
for(j=0;j<=top;j++)
printf("%c",stack[j]);
}
void dispinput()
{
int j;
for(j=i;j<l;j++)
printf("%c",*(input+j));
}
int main()
{
int j;
input=(char*)malloc(50*sizeof(char));
printf("\nEnter the string : ");
scanf("%s",input);
input=strcat(input,"$");
l=strlen(input);
strcpy(stack,"$");
printf("\nSTACK\tINPUT\tACTION");
while(i<=l)
{
shift(); printf("\
n"); dispstack();
printf("\t");
dispinput();
printf("\tShift");
if(prec[getindex(stack[top])][getindex(input[i])]=='>')
{
while(reduce())
{
printf("\n");
dispstack();
printf("\t");
dispinput();
printf("\tReduced: E->%s",lasthandle);
}
}
}
if(strcmp(stack,"$E$")==0)
printf("\nAccepted;");
else
printf("\nNot Accepted;");
}
Output: