Muhammad Al-Xorazmiy nomidagi
Toshkent axborot texnologiyalari universiteti
4 – kurs Dasturiy injeniring fakulteti
316 – 20 guruh talabasi
Shirinov Shohruxning
Timsollarni tanib olish fanidan
1 – amaliy ishi
Mavzu : Berilgan zanjirlar uchun kontekst-grammatikani yaratish algoritmi
Maqsad:
• Siz berilgan satrlar to'plamini ishlab chiqaradigan kontekstsiz grammatikani (CFG) yaratishingiz kerak.
Topshiriq
• Xar bir talaba o`z ismini va familiyasini berilgan terminal zanjirlarining namunaviy to'plami buyicha misolni ishlang. Bunda zanjir daraxt shaklida ifodalang va dastur yozing.
Qoida: eng uzun tanlov zanjirini yaratish uchun ishlatiladigan almashtirish qoidasi qoldiq qoida deb ataladi va uning o'ng tomonining uzunligi 2 ga teng (bu qiymat algoritm qulayligi uchun tanlangan)
Dasturning Java tilidagi kodi:
package com.example.service;
import java.util.Scanner;
public class Task {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Ism yoki familiya kiriting: ");
String name = scanner.nextLine();
int length = name.length();
System.out.println();
for (int i = 0; i < length - 2; i++) {
if (i == 0) {
System.out.println("S -> " + name.charAt(0) + "A1 A1=" + name.substring(1, length));
continue;
}
System.out.println("A" + i + " -> " + name.charAt(i) + "A" + (i + 1) + " A" + (i + 1) + "=" + name.substring(i + 1, length));
if (i == length - 3)
System.out.println("A" + (i + 1) + " -> " + name.substring(i + 1, length));
}
System.out.println();
System.out.println();
StringBuilder str = new StringBuilder();
StringBuilder line = new StringBuilder();
StringBuilder word = new StringBuilder();
for (int i = 0; i < length - 1; i++) {
if (i == 0) {
str = new StringBuilder("S---");
line= new StringBuilder("| ");
word =new StringBuilder(name.charAt(i)+" ");
continue;
}
if (i==length-2){
str.append("A").append(i).append("---").append(name.charAt(i + 1));
line.append(" | ");
word.append(name.charAt(i)).append(" ");
break;
}
str.append("A").append(i).append("---");
line.append(" | ");
word.append(name.charAt(i)).append(" ");
}
System.out.println(str);
System.out.println(line);
System.out.println(word);
}
}
Dastur natijasi:
Ism: Shohrux;
S
Vn=shohrux;
Nt= SHOHRUX; s h o h r u h
Vn=shohrux;
Nt= S(A1)H(A2)O(A3)H(A4)R(A5)UH(A6) 1)S’’=>sS; 3)U=>hH; 5) R=>rOB; 2)S=>uU; 4)H=rR; 6) OB=>ob
Familiya: Shirinov
s h i r i n o v
Vn=rinov;
Nt= R(A1)I(A2)N(A3)OV(A4) 1)S’’=>mM; 3)A=>nN; 2)M=>aA; 4)N=nOV;
Dostları ilə paylaş: |