








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
artificial intelligence and compiler constructionartificial intelligence and compiler construction
Typology: Assignments
1 / 14
This page cannot be seen from the preview
Don't miss anything!
#include<stdio.h> #include<stdlib.h> int queue[100]; int front=0; int rear=-1; struct node{ int data; struct node left; struct node right; }; /Insert data in the tree**/ struct node *insert(struct node root, int value) { if(root == NULL) { root = (struct node) malloc(sizeof(struct node)); root->left = root->right = NULL; root->data = value; return root; } else { if(value < root->data) { root->left = insert(root->left, value); } else { if(value > root-
data) { root->right = insert(root->right, value); } } return root; } }; void printNode(struct node *root){ if(root != NULL) { printf("%d ", root->data); } } /******** Queue ********/ void enqueue(struct node root){ if(root != NULL){ rear++; queue[rear] = root; } } void dequeue(){ if(rear>=front){ struct node root = queue[front]; printNode(root); front++; enqueue(root->left); enqueue(root->right); } } / BFS function / void bfs(struct node root){ if(root != NULL){ enqueue(root); do{ dequeue(root); }while(front <= rear); } } /DFS function / void dfs(struct node root){ if(root != NULL){ dfs(root->left); dfs(root->right); printf("%d ", root->data); } } /DLS function/ void dls(struct node *root, int i, int limit){ for(i = 0; i < limit; i++){ if(root != NULL){ if(limit > 0){ dls(root->left, i, limit- ); dls(root->right, i, limit- 1 );
printf("%d ", root->data); } } //printf("\n"); } } int main(){ struct node *root = NULL; int choose; int n=0, i=0, insertNode, limit; printf("Enter Your choice : \n 1 For insert a node \n 2 For BFS \n 3 for DFS\n 4 for DLS\n 0 for exit the program"); printf("
n---------------------------------"); while(1){ printf("\nEnter your option : "); scanf("%d", &choose); switch(choose){ case 1: printf("How many nodes of the tree :"); scanf("%d", &n); for(i = 0; i < n; i++ ){ printf("Enter root : "); scanf("%d", &insertNode); root = insert(root, insertNode); } break; case 2: printf("BFS traversal of that tree is : "); bfs(root); break;
case 3: printf("DFS traversal of that tree is : "); dfs(root); break; case 4: printf("Enter the depth limit for DLS: "); scanf("%d", &limit); dls(root, 0, limit); break; case 0: exit(0); break; } } } FARMER N GOAT: farmer,goat,cabbage,wolf=("farmer","go at","cabbage","wolf") carryables=(goat,cabbage,wolf,None) forbiddens=(set((goat,cabbage)), set((wolf,goat))) def mayhem(cfg): for shore in cfg[0]: if farmer not in shore: for forbidden in forbiddens: if shore.issuperset(forbidden): return True return False def done(cfg): left,right=cfg[0] return left==set() def ferry(cfg,item): left,right=[set(x) for x in cfg[0]] if farmer in left: src,dst=left,right else: src,dst=right,left if item and not item in src: return None desc="The farmer goes -->" if farmer in left else "The farmer goes <--" src.remove(farmer) dst.add(farmer) if item: src.remove(item) dst.add(item) desc+=" with the "+item else: desc+=" alone" return ((left,right),desc) def printcfg(cfg,level=0): left,right=cfg[0] verdict="(Not allowed)" if mayhem(cfg) else "(Ok)" #print (" "level,", ".join(left)," ~ ",", ".join(right),cfg[1],verdict) def onegeneration(cfg): followups=[] for item in carryables: followup=ferry(cfg,item) if not followup: continue followups.append(followup) return followups def generate(cfg,level=0): solutionstack.extend([None](level- len(solutionstack)+1)) solutionstack[level]=cfg[1] printcfg(cfg,level) childs=onegeneration(cfg) for child in childs: if mayhem(child): continue if child[0] in previouscfgs: continue previouscfgs.append(child[0]) generate(child,level+1) cfg=((set((farmer,goat,cabbage,wolf)), set()),"") previouscfgs=[cfg[0]] solutionstack=[] #print ("Trace of the recursive solution-finding process:") generate(cfg) print ("\nThe solution to the problem:") for step in solutionstack: if step: print (" ",step) OUTPUT: The solution to the problem: The farmer goes --> with the goat The farmer goes <-- alone The farmer goes --> with the cabbage The farmer goes <-- with the goat The farmer goes --> with the wolf The farmer goes <-- alone The farmer goes --> with the goat
#include<stdio.h> #include<string.h> #include<unistd.h> #include<sys/types.h> #include <sys/stat.h> #include <stdlib.h> #include<time.h> #define r 3 #define c 3 char matrix[r][c]; char new[r][c]; int count; char final[r][c] = {{'1','2','3'}, {'4','5','6'},{'7','8',' '}}; int i,j; char z ; int p,q,x,y; int t =0; int result = 0; void load(){ for(i=0;i<3;i++){ for(j=0;j<3;j++){ if(new[i][j] == '0'){ matrix[i][j]= ' '; continue; } matrix[i][j]= new[i][j]; } } } void blank(){ for(i=0;i<3;i++){ for(j=0;j<3;j++){ new[i][j]= ' '; } } } int main(){ time_t T= time(NULL); struct tm tm = *localtime(&T); char f[4]; int rsl ; int random,t; int randvalues[9]; main: count = 0; blank(); T= time(NULL); tm = *localtime(&T); srand(tm.tm_sec); while(count!=9){ rsl=rand()%9; sprintf(f,"%d",rsl); for(i=0;i<r;i++){ for(j=0;j<c;j++){ if((new[i][j]) == f[0]){ i = 4; j = 4; continue;
else if((new[i][j]) == ' '){ new[i][j] = f[0]; i = 4; j = 4; count++; } } } } load(); for(i=0;i<r;i++){ for(j=0;j<c;j++){ printf("|%c|",matrix[i] [j]); } printf("\n"); } while(1){ printf("enter value to change its position to blank space\n"); scanf(" %c",&z); if(z=='q'){ printf("\n*****You Quit*****\n"); goto main; // break; } for(i=0;i<r;i++){ for(j=0;j<c;j++){ if((matrix[i][j])== z) { p = i; q = j; } else if((matrix[i] [j])== ' '){ x = i; y = j; } } } t =0; int m , n ; m = p - 1; n = q ; if(m>=0){ if((matrix[m][n])== ' ') t=1; } m = p + 1; if(m<=2){ if((matrix[m][n])== ' ')t=1; } m = p; n = q - 1 ; if(n>=0){ if((matrix[m][n])== ' ')t=1; } n = q + 1 ; if(n<=2){ if((matrix[m][n])== ' ')t=1; }
if(t==1){ matrix[x][y] = z; matrix[p][q] = ' '; } t = 0; for(i=0;i<r;i++){ for(j=0;j<c;j++){ if((matrix[i][j])== final[i][j]){ t++; } } } system("clear"); for(i=0;i<r;i++){ for(j=0;j<c;j++){ printf("|%c|",matrix[i] [j]); } printf("\n"); } if(t==9){ printf("\n****you Win****\n"); break; } } return 1; } TIC TAC TOE: import numpy as np import random from time import sleep def create_board(): return(np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]])) def possibilities(board): l = [] for i in range(len(board)): for j in range(len(board)): if board[i][j] == 0: l.append((i, j)) return(l) def random_place(board, player): selection = possibilities(board) current_loc = random.choice(selection) board[current_loc] = player return(board) def row_win(board, player): for x in range(len(board)): win = True for y in range(len(board)): if board[x, y] != player: win = False continue if win == True: return(win) return(win) def col_win(board, player): for x in range(len(board)): win = True for y in range(len(board)): if board[y][x] != player: win = False continue if win == True: return(win) return(win) def diag_win(board, player): win = True y = 0 for x in range(len(board)): if board[x, x] != player: win = False if win: return win win = True if win: for x in range(len(board)): y = len(board) - 1 - x if board[x, y] != player: win = False return win def evaluate(board): winner = 0 for player in [1, 2]: if (row_win(board, player) or col_win(board,player) or diag_win(board,player)): winner = player if np.all(board != 0) and winner == 0: winner = - return winner def play_game(): board, winner, counter = create_board(), 0, 1 print(board) sleep(2) while winner == 0: for player in [1, 2]: board = random_place(board, player) print("Board after " + str(counter) + " move") print(board) sleep(2) counter += 1 winner = evaluate(board) if winner != 0: break return(winner) print("Winner is: " + str(play_game())) WATER-JUG:
import java.io.*; public class Main { public static int chkL(int k,int i,char[]L,int j,int[]D,char[]ch) { int cL=0,cD=0; while(k<ch.length&&i<L.length&&j<D.len gth) { if(ch[k]==L[i]) {cL++;i=0;k++;} else if(ch[k]==(char) (48+D[j])) {cD++;j=0;k++;} else if(i==L.length-1) {i=0;} else if(j==D.length-1) {j=0;} else {i++;j++;} } return cL+cD; } public static void main(String args[])throws IOException { int count=0,k=1,i,j; char L[]={ 'a','b','c','d','e','f','g','h','i','j ','k','l','m','n','o','p','q','r','s', 't','u','v','w','x','y','z', 'A','B','C','D','E','F','G','H','I','J ','K','L','M','N','O','P','Q','R','S', 'T','U','V','W','X','Y','Z'}; int D[]={0,1,2,3,4,5,6,7,8,9}; DataInputStream d=new DataInputStream(System.in); String s; System.out.println("Enter a string"); s=d.readLine(); char ch[]=s.toCharArray(); for(i=0;i<L.length;i++) { if(ch[0]==L[i]) {count=1;break;} } if(count==1) { k=1;i=0;j=0; int cL=chkL(k,i,L,j,D,ch); count+=cL; } if(ch.length==count) System.out.println(s+" is an identifier"); else System.out.println(s+" is not an identifier"); }
import java.io.; public class Main { public static void main(String[] args)throws IOException { String s; char []op={'+','-','','/','%','='}; int i,j,flag=0; DataInputStream d=new DataInputStream(System.in); System.out.println("Enter Expression"); s=d.readLine(); char ch[]=s.toCharArray(); System.out.println("Tokens are"); for(i=0;i<ch.length;i++) { flag=0; for(j=0;j<op.length;j++) { if(ch[i]==op[j]) { flag=1; break; } } if(flag==0) { System.out.print(ch[i]); } else if(flag==1) { System.out.print("
n"+op[j]+"\n"); } } } } INPUT C #include<stdio.h> #include<conio.h> void main() { int a,b=100,c=4; float y,d=116.66,e=3; double x,f=333.3; clrscr(); a=b/c; y=d/e; x=f/e; printf("The values are:\n%d\n %d\n%d",a,y,x); getch(); }
#include<stdio.h> #include<conio.h> int a,b,i,j,k,l,n; char nt[10],pro[10][10],ch; void main() { do{ for(i=0;i<10;i++) for(j=0;j<10;j++) { nt[j]='\0'; pro[i][j]='\0'; } printf("Enter the number of productions:"); scanf("%d",&n); printf("Enter the Grammar:\n"); for(i=0;i<n;i++) { printf("\t"); scanf("%s",pro[i]); } for(i=0;i<n;i++) nt[i]=pro[i][0]; printf("The FIRST SET is:\n"); for(i=0;i<n;i++) { printf("First[%c]: {",nt[i]); for(j=0;j<10;j++) { if(pro[i][j]=='>') { j++; for(k=0;k<n;k++) if(pro[i] [j]==nt[k]) p1(); for(a=0;a<10;a++) { if(pro[i] [a]=='/') { a++; for(k=0;k<n;k++) if(pro[i][a]==nt[k]) p1(); if(pro[i] [a]>=97&&pro[i][a]<=122) printf(" %c ",pro[i][a]); } } if(pro[i] [j]>=97&&pro[i][j]<=122) printf(" %c ",pro[i][j]); } } printf("}\n"); } //printf("\nWant to enter another Grammar('Y'es or 'N'o):"); ch=getch(); }while(ch=='y'||ch=='Y'); } p1() { for(l=0;l<n;l++) { if(pro[k][3]==nt[l]) { p2(); k=l; } } if(pro[k][3]>=97&&pro[k][3]<=122) { printf(" %c ",pro[k][3]); p2(); } return 0; } p2() { for(a=0;a<10;a++) { if(pro[k][a]=='/') { b=k; a++; for(l=0;l<n;l++) { if(pro[b] [a]==nt[l]) { p2(); b=l; } } if(pro[b][a]>=97&&pro[b] [a]<=122) printf(" %c ",pro[b] [a]); } } return 0; }
if(ifx[i]=='/'|| ifx[i]=='%'||ifx[i]=='') { if(stack[a]=='/'|| stack[a]=='%'||stack[a]==''|| stack[a]=='^') { pfx[k]=stack[a]; a--; k++; return 1; } } if(ifx[i]=='^') { if(stack[a]=='^') { pfx[k]=stack[a]; a--; k++; return 1; } } } GENERATE 3 ADDRESS CODE: import java.io.; public class Main { public static void main(String args[])throws IOException { String s,s2="",old="",nw="";char rep='A'; char []op={'=','','/','+','-'}; DataInputStream dis=new DataInputStream(System.in); System.out.println("Enter string"); s=dis.readLine(); int i=1,j=0; while(i<op.length) { s2=s;j=0;old="";nw=""; char []ch=s2.toCharArray(); while(j<ch.length) { if(op[i]==ch[j]) { System.out.println(rep+" = "+ch[j- 1]+""+ch[j]+""+ch[j+1]); old+=ch[j-1]; old+=ch[j]; old+=ch[j+1]; nw+=rep; s=s.replace(old, nw); rep++;i=0; break; } else j++; } i++; } if(s2.contains("=")) System.out.println(s2); } } #include<stdio.h> #include<conio.h> #include<string.h> void main() { int size[5]={2,4,4,1,8},i,j,k; char datatype[5] [10]={"int","float","double","char","l ong"}; char str[50]=""; FILE *fp; fp=fopen("input.c","r"); clrscr(); printf("------------------------------ \n"); printf("DType Size V_Name Value\n"); printf(" (bytes) \n"); printf("------------------------------ \n"); while(!feof(fp)) { fscanf(fp,"%s",str); for(i=0;i<5;i++) { if(strcmp(str,datatype[i])==0) { fscanf(fp,"%s",str); printf("%s\t%d
t",datatype[i],size[i]); for(j=0;j<(strlen(str)- 1);j++) { if(str[j]==',') { j++; printf("\n%s\t %d\t",datatype[i],size[i]); } if(str[j]=='=') { j++; printf("\t"); } printf("%c",str[j]); } printf("\n"); } } }
printf("------------------------------ "); fclose(fp); getch(); } Ll1: #include<stdio.h> #include<conio.h> void main() { int i=0,j=0,k=0; char first[5][4]={"d(\0\0","+e
0\0","d(\0\0","e\0\0","d(\0\0"}; char follow[5][5]={")$\0\0\0","
0)$\0\0","+)$\0\0", "+)$\0\0","+)$
0"}; char firtbl[5][9]={"E ->TE'
0","E'->+TE'\0","T ->FT'\0", "T'->*FT'\0","F ->id\0"}; char folltbl[5][8]={"\0","E'-
€","\0","T'->€","\0"}; char nonter[5][3]={"E\0","E'
0","T\0","T'\0","F\0"}; printf("The Given Grammar is:\n\n
t"); printf("E ->TE'\n\tE'->+TE'/e\n\tT ->FT'\n\tT'->*FT'/e"); printf("\n\tF ->(E)/id\n\n"); printf("LL(1) Parsing Table for the above grammar:\n\n"); printf("------------------------------ -------------"); printf("------------------------
n"); printf(" NT/IP id +
(E)"); else printf("\t\t\t %s",firtbl[i]); } if(first[i][j]=='€') { printf("\n\t"); for(k=0;k<5;k++) { if(i==1&&k==0) printf("\t\t"); if(follow[i] [k]=='+') printf("\t %s
t",folltbl[i]); if(follow[i] [k]=='*') printf("\t\t\t %s",folltbl[i]); if(follow[i] [k]==')') printf("\t\t\t %s",folltbl[i]); if(follow[i] [k]=='$') printf(" %s",folltbl[i]); } } } printf("\n"); } printf("------------------------------ -------------"); printf("------------------------
n"); getch(); } RECURSIVE DECENT: #include<stdio.h> #include<conio.h> #include<string.h> char input[100],ch; int i=0; int S() { if(input[i]=='c') { i++; if(A()) if(input[i]=='d') { i++; return 1; } }
do { printf("The Given Grammar is:
n"); printf("\n\tE->TE'\n\tE'-
+TE'/€\n\tT->FT'");
printf("\n\tT'->*FT'/€\n\tF-
(E)/id\n"); printf("Enter the Input String:"); scanf("%s",input); i=0; if(E()!=0) { if(input[i]=='\0') printf("\nString is Accepted"); else printf("\nString is Rejected"); } else printf("\nString is Rejected"); printf("\n\nWant to Enter Another String('Y'es or 'N'o):"); ch=getch(); }while(ch=='y'||ch=='Y'); } E() { T(); Ep(); } Ep() { if(input[i]=='+') { i++; T(); Ep(); } } T() { F(); Tp(); } Tp() { if(input[i]=='*') { i++; F(); Tp(); } } F() { if(input[i]=='i') { i++; if(input[i]=='d')
i++; return 1; } else return 0; } if(input[i]=='(') { i++; E(); if(input[i]==')') { i++; return 1; } else return 0; } else return 0; } PRINT SYMBOL TABLE CORESPONDING TO SAM #include<stdio.h> #include<conio.h> #include<string.h> void main() { int size[5]={2,4,4,1,8},i,j,k; char datatype[5] [10]={"int","float","double","char","l ong"}; char str[50]=""; FILE *fp; fp=fopen("input.c","r"); clrscr(); printf("------------------------------ \n"); printf("DType Size V_Name Value\n"); printf(" (bytes) \n"); printf("------------------------------ \n"); while(!feof(fp)) { fscanf(fp,"%s",str); for(i=0;i<5;i++) { if(strcmp(str,datatype[i])==0) { fscanf(fp,"%s",str); printf("%s\t%d
t",datatype[i],size[i]); for(j=0;j<(strlen(str)- 1);j++) { if(str[j]==',') { j++;
printf("\n%s\t %d\t",datatype[i],size[i]); } if(str[j]=='=') { j++; printf("\t"); } printf("%c",str[j]); } printf("\n"); } } } printf("------------------------------ "); fclose(fp); getch(); } RECURSIVE DECENT: #include<stdio.h> #include<conio.h> #include<string.h> char input[100],ch; int i=0; int S() { if(input[i]=='c') { i++; if(A()) if(input[i]=='d') { i++; return 1; } } return 0; } int A() { int j; j=i; if(input[i]=='a') { i++; if(input[i]=='b') { i++; return 1; } } i=j; if(input[i]=='a') { i++; return 1; } else return 0;
void main() { do { printf("The Given Grammar is:\n"); printf("\tS->cAd\n\tA-
ab/a\n"); printf("Enter the Input String: "); scanf("%s",input); i=0; if(strlen(input)<3|| strlen(input)>4) printf("\nString is Rejected"); else { if(S()!=0) printf("
nString is Accepted"); else printf("
nString is Rejected"); } printf("\nWant to Enter another string Yes(or)No:"); ch=getch(); }while(ch=='Y'||ch=='y'); }