You are on page 1of 12

En No: - 201303100810019

Practical 6
n n
AIM: -Construct Pushdown Automata for the Language a b

Input: Give an input string of letters from the alphabet .


Output: Your program must tell whether this string is acceptable (if the
n n
n n
string is from a b ) or not (if the string is not from a b ).

#include <stdio.h>
#include <string.h>
void push(char);
char pop();
char stack[100];
int top = -1;
void main()
{
charstr[100],f;
inti, count0 = 0,count1 = 0, len, n;
printf("Enter the value of n: ");
scanf("%d",&n);
printf("Enter string to check: ");
scanf("%s", str);
len = strlen(str);
for (i = 0; i<len; i++)
{
push(str[i]);
}
for (i = 0; i<len; i++)
{
f=pop();
if (f == 'a')
count0++;
else if (f == 'b')
count1++;
}
if (count0 == n && count1 == n)
printf("%s is accepted..\n", str);
else
printf("%s is not accepte...\n", str);
}
void push(char c)
{
stack[++top] = c;
}

CGPIT/IT/SEM-6/AFL (030080603)

En No: - 201303100810019

char pop()
{
return(stack[top--]);
}

OUTPUT: -

CGPIT/IT/SEM-6/AFL (030080603)

En No: - 201303100810019

Practical 7
AIM: -Generate a program that can construct a Turing Machine for a given
language L = 1s Complement over an alphabet set = {0,1}.
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main(){
inti,j,l,flag;
char s[50];
clrscr();
printf("Enter the digits for 1's complement: ");
scanf("%s",s);
l=strlen(s);
s[l++]='a';
for(i=0;i<l;i++){
if(s[i]=='0')
s[i]='1';
else if(s[i]=='1')
s[i]='0';
else if(s[i]=='a'){
flag=1;
break;
}
else{
flag=0;
break;
}
}
if(flag==1){
printf("1's Complement: ");
for(i=0;i<(l-1);i++)
printf("%c ",s[i]);
}
else{
printf("Invalid Input.");
}
getch();
}

Output:-

CGPIT/IT/SEM-6/AFL (030080603)

En No: - 201303100810019

Practical 8
AIM: -Write a Lex program to do following things: To count number of lines,
words, blank spaces and characters.
Cnt.l :%{
#include<stdio.h>
intchcnt=0,lcnt=0,wrcnt=0,spcnt=0;
FILE *yyin,*yyout;
%}
%%
\n {lcnt++;wrcnt++;}
[" "|\t] {spcnt++;wrcnt++;}
[a-z|A-Z|0-9] {chcnt++;}
. {}
%%
int main()
{
yyin=fopen("8.txt","r");
if(yyin==NULL)
{
printf("File not found");
}
yyout=fopen("new.txt","w");
yylex();
fprintf(yyout,"\nTotal no. of characters:%d",chcnt);
fprintf(yyout,"\nTotal no. of words:%d",wrcnt);
fprintf(yyout,"\nTotal no. of Line:%d",lcnt);
fprintf(yyout,"\nTotal no. of Spaces:%d",spcnt);
fclose(yyin);
fclose(yyout);
return 0;
}
intyywrap()
{
return 1;
}

CGPIT/IT/SEM-6/AFL (030080603)

En No: - 201303100810019

8.txt

Output:

CGPIT/IT/SEM-6/AFL (030080603)

En No: - 201303100810019

Practical 9
AIM: -Write a Lex program to do following things: To perform arithmetic
operations
%{
inta,b;
%}
%%
"+" {printf("Addition:%d",a+b);}
"-" {printf("subtraction:%d",a-b);}
"*" {printf("Multiplication:%d",a*b);}
"/" {printf("Division:%d",a/b);}
%%
void main()
{
printf("Enter The value of A and B:");
scanf("%d %d",&a,&b);
yylex();
fclose(yyin);
}

Output :-

CGPIT/IT/SEM-6/AFL (030080603)

En No: - 201303100810019

Practical 10
AIM: - Design a one pass assembler for a given source code and trace symbol
table, literal table and machine code .txt files.
START 900
LOOP: SUB A,B
JNC LOOP
LTORG
=5
END
Practical_10_Assembler.c :#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE *fptr1,*fptr2,*fptr3, *fptr4;
char line[50],line1[50],*token,*token1,temp1[50],mne[50][50],lc1[3],ch;
intk,i,lc=900,l=0;
fptr1 = fopen("ip.txt", "r");
fptr2 = fopen("mn.txt", "r");
fptr3= fopen("op.txt","a");
fptr4= fopen("symb.txt","a");
if (fptr1 == NULL || fptr2 == NULL)
{
printf("One Can not open the file");
exit(0);
}
else
{
fscanf(fptr1,"%s",line);
printf("%s",line);
do
{
/* 1. First It searches for the substring (:)(colon) and if it exists then
line containing colon will be tokenized based on the (:)colon.
2. Whatever string is being tokennized that is to be written in Symb.txt file
3. At the same time address of that line is also to be written in Symb.txt file.

CGPIT/IT/SEM-6/AFL (030080603)

En No: - 201303100810019

4. Conversion of integer value of LC is done in string LC1 and it is written in


Symb.txt file
*/
if(strstr(line,":")) //LP:
{
token=strtok(line,":");
printf("token = %s",token);
strcpy(temp1,token);
sprintf(lc1,"%d",lc); // sprintf function converts an integer to string
fprintf(fptr4,"%s\t",lc1);
fprintf(fptr4,"%s\n",token);
ch='\n';
fputc(ch,fptr4);
}
/* 1. Second, It searches for the substring (,)(comma) and if it exists then
line containing comma will be tokenized based on the (,)comma.
2. Whatever string is being tokennized that is compared with Predefined
value of agre&breg.If tokenized string is equal to AREG then (01) to
be written in op.txt file or else (02) (code for BREG) is to be written
in op.txt
*/
else if (strstr(line,","))
{
token=strtok(line,",");
strcpy(temp1,token);
if(strcmp(token,"AREG"))
{
fprintf(fptr3,"%s\t","(01)");
}
token=strtok(NULL,",");
strcpy(temp1,token);
if(strcmp(token,"BREG"))
{
fprintf(fptr3,"%s\n","(02)");
}
}
/*

CGPIT/IT/SEM-6/AFL (030080603)

En No: - 201303100810019

1. Third, It searches for the substring (" ")(Single Space) and if it exists then
line containing Single Space will be tokenized based on the delimeter (" ")(Single
Space).
2. Whatever string is being tokennized that is compared with the mn.txt file's
data.
3. For doing a comparision, mn.txt file is opened in read mode and whole line
at once is read and stored in string named line1.
4. Now, this line1 is compared with the token generated at step 1. If both are
equal
then line1 string is again tokenized using delimeter (" ")(Single Space).
Whatever string is being tokennized that is written in op.txt file.
5. At the same time address specified in Start instruction is also to be written in
op.txt.
*/
else
{
token=strtok(line," ");
if(strcmp(token,"900")!=0)
{
sprintf(lc1,"%d",lc);
fprintf(fptr3,"%s",lc1);
ch=')';
fputc(ch,fptr3);
fptr2 = fopen("mn.txt", "r");
//"%[^\n]%*c = this Regular Expression is used to read whole line
at once"
while(fscanf(fptr2,"%[^\n]%*c",line1)==1)
{
token1=strtok(line1," ");
if(strcmp(token1,token)==0) //s1,s2
{
token1=strtok(NULL," ");
fprintf(fptr3,"\t%s",token1);
}
}
lc++;
}
else
{

ch='(';
fputc(ch,fptr3);

CGPIT/IT/SEM-6/AFL (030080603)

En No: - 201303100810019

ch='C';
fputc(ch,fptr3);
ch=',';
fputc(ch,fptr3);
fprintf(fptr3,"%s",token);
ch=')';
fputc(ch,fptr3);
ch='\n';
fputc(ch,fptr3);
}
}
printf("\n%s",line);
}while(fscanf(fptr1,"%s",line) == 1);
}
fclose(fptr1);
fclose(fptr2);
fclose(fptr3);
fclose(fptr4);
return 0;
}

ip.txt

CGPIT/IT/SEM-6/AFL (030080603)

En No: - 201303100810019

mn.txt

op.txt

symb.txt

CGPIT/IT/SEM-6/AFL (030080603)

En No: - 201303100810019

Output:

CGPIT/IT/SEM-6/AFL (030080603)

You might also like