Skip to content

Commit

Permalink
не рабочий
Browse files Browse the repository at this point in the history
  • Loading branch information
Encapsulateed committed Feb 5, 2024
1 parent f700b38 commit 10831f2
Show file tree
Hide file tree
Showing 17 changed files with 1,077 additions and 39 deletions.
6 changes: 3 additions & 3 deletions lab5/input.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
S->aSb&aSb
S->a
S->b
S->bbS
S->SbS
S->bb
5 changes: 3 additions & 2 deletions lab5/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ void main(List<String> arguments) {

word = stdin.readLineSync() ?? 'null';
word = word.trim().replaceAll(' ', '');
print(word);

bool conj = false;
int step_num = -1;
Expand All @@ -32,7 +31,7 @@ void main(List<String> arguments) {
}

var cg = conjunctiveGrammar.fromFile('input.txt');

//print(cg.possible_grammars[0]);
List<bool> results = [];
for (var grammar in cg.possible_grammars) {
LR0Parser curr_parser = LR0Parser(grammar);
Expand All @@ -53,4 +52,6 @@ void main(List<String> arguments) {
print('Слово не распознаётся');
}
}
/**
* */
}
47 changes: 47 additions & 0 deletions lab5/result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
digraph {
rankdir = LR
dummy [shape=none, label="", width=0, height=0]
subgraph cluster_0 {
label="Level 0";
"[0, 0]" [label="[0, 0]"];
}
subgraph cluster_1 {
label="Level 1";
"[1, 2]" [label="[1, 2]"];
"[1, 3]" [label="[1, 3]"];
}
subgraph cluster_2 {
label="Level 2";
"[2, 2]" [label="[2, 2]"];
"[2, 7]" [label="[2, 7]"];
"[2, 1]" [label="[2, 1]"];
}
subgraph cluster_3 {
label="Level 3";
"[3, 2]" [label="[3, 2]"];
"[3, 3]" [label="[3, 3]"];
}
subgraph cluster_4 {
label="Level 4";
"[4, 2]" [label="[4, 2]"];
"[4, 7]" [label="[4, 7]"];
"[4, 9]" [label="[4, 9]"];
}
subgraph cluster_5 {
label="Level 5";
"[5, 2]" [label="[5, 2]"];
"[5, 3]" [label="[5, 3]"];
}
"[5, 2]" -> "[4, 7]";
"[5, 3]" -> "[4, 7]";
"[4, 2]" -> "[3, 3]";
"[4, 7]" -> "[3, 3]";
"[4, 9]" -> "[2, 7]";
"[3, 2]" -> "[2, 7]";
"[3, 3]" -> "[2, 7]";
"[2, 2]" -> "[1, 3]";
"[2, 7]" -> "[1, 3]";
"[2, 1]" -> "[0, 0]";
"[1, 2]" -> "[0, 0]";
"[1, 3]" -> "[0, 0]";
}
68 changes: 59 additions & 9 deletions lab5/src/lr0/LR0Fms.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import '../utils/grammar.dart';
import '../utils/Production.dart';
import '../state_machine/FSM.dart';
Expand All @@ -15,7 +17,6 @@ class LR0FMS extends FSM {
this._grammar = CompleteGrammar;
super.alphabet.addAll(_grammar.nonTerminals);
super.alphabet.addAll(_grammar.terminals);

buildDFA();
}

Expand All @@ -37,16 +38,62 @@ class LR0FMS extends FSM {
return production_possible_LR0_situations;
}

List<LR0Situation> CLOSURE() {
Set<String> possibleNT = {};
Set<LR0Situation> first_state = {};
first_state.add(LR0Situation(_grammar.startNonTerminal + '0',
_grammar.startNonTerminal.split(''), 0));
for (var r in _grammar.rules
.where((element) => element.left == _grammar.startNonTerminal)) {
if (_grammar.nonTerminals.contains(r.right[0])) {
possibleNT.add(r.right[0]);
for (var rr
in _grammar.rules.where((element) => element.left == r.right[0])) {
if (!first_state.any((element) =>
element.left == rr.left && element.right == rr.right)) {
first_state.add(LR0Situation(rr.left, rr.right, 0));
}
}
}
}
// print(first_state);
return first_state.toList();
}

void UPDATE_FIRST() {
for (var state
in super.states.where((element) => getStateIndex(element) != 0)) {
List<LR0Situation> for_add = [];
for (var lr0 in state.value as List<LR0Situation>) {
var next = lr0.getNext();
if (_grammar.nonTerminals.contains(next)) {
for (var rule in _grammar.rules) {
if (rule.right.length == 2) {
if (rule.left == next) {
(for_add).add(LR0Situation(rule.left, rule.right, 0));
}
}
}
}
}

for (var lr in for_add) {
state.name += '\n' + lr.toString();
(state.value as List<LR0Situation>).add(lr);
load_rules(getStateByIndex(0), state, lr.getNext());
}
}
}

void buildDFA() {
// Начальное состояние соответвует G+ - пополненной грамматике
List<LR0Situation> first_state_value = []
..addAll(_grammar.rules.map((P) => zeroClosure(P)[0].clone()));
List<LR0Situation> first_state_value = CLOSURE();
var state_name = first_state_value.join('\n');
State first_state = State.valued(state_name, first_state_value);
super.states.add(first_state);
super.startStates.add(first_state);
shift(first_state);

UPDATE_FIRST();
while (true) {
int p_l = super.states.length;

Expand Down Expand Up @@ -84,6 +131,11 @@ class LR0FMS extends FSM {
}
}
}
List<State> first = [];
First(getStateByIndex(7), first);
first.forEach((element) {
// print(getStateIndex(element));
});
}

Set<LR0Situation> getDstSet(State s, String X) {
Expand Down Expand Up @@ -209,7 +261,8 @@ class LR0FMS extends FSM {
}
}
} catch (e) {
//print(e);
//print(state.name);
print(e);
return;
// continue;
}
Expand Down Expand Up @@ -273,12 +326,9 @@ class LR0FMS extends FSM {

void First(State X, List<State> first) {
for (var tr in super.transactions.where((t) => t.to == X)) {
if (X != tr.from) {
if (X != tr.from && !first.contains(tr.from)) {
first.add(tr.from);
First(tr.from, first);
if (!first.contains(X)) {
break;
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions lab5/src/lr0/LR0State.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import '../utils/Production.dart';
import '../utils/grammar.dart';
import 'LR0Situation.dart';

class LR0State {
Set<LR0Situation> items = {};
}
4 changes: 2 additions & 2 deletions lab5/src/lr0/LR0Table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class LR0Table {

lr0_table[index] = {};
for (var X in _grammar.nonTerminals) {
if (X != _grammar.start_non_terminal + "0") {
if (X != _grammar.startNonTerminal + "0") {
lr0_table[index]![X] = [];
}
}
Expand Down Expand Up @@ -70,7 +70,7 @@ class LR0Table {
if (_grammar.terminals.contains(X)) {
lr0_table[index_I]![X]!.add(Action.shift(index_J));
} else if (_grammar.nonTerminals.contains(X)) {
if (X != _grammar.start_non_terminal + '0') {
if (X != _grammar.startNonTerminal + '0') {
lr0_table[index_I]![X]!.add(Action.goto(index_J));
}
}
Expand Down
4 changes: 3 additions & 1 deletion lab5/src/utils/Production.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ class Production {

@override
bool operator ==(Object other) =>
identical(this, other) || this.toString() == other.toString();
identical(this, other) ||
this.toString() == other.toString() ||
(this.left == (other as Production).left && this.right == other.right);
}
17 changes: 4 additions & 13 deletions lab5/src/utils/conjunctiveGrammar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class conjunctiveGrammar {
List<conjunctiveProdutcion> rules = [];

conjunctiveGrammar.fromFile(String filePath) {
// _alternativsPreWork(filePath);
_readGrammarFromFile(filePath);
_makeCFGs();
}
Expand All @@ -31,23 +32,13 @@ class conjunctiveGrammar {
}
}

var file = File(filePath);

// Открываем поток для записи в файл
var sink = file.openWrite();

try {
// Записываем каждую строку из списка в файл
for (var rule in updated_rules) {
sink.writeln(rule);
}
} finally {
// Закрываем поток и сохраняем изменения в файле
}
// Записываем обновленные правила в файл
File(filePath).writeAsStringSync(updated_rules.join('\n'));
}

void _readGrammarFromFile(String filePath) {
sleep(Duration(microseconds: 1000));

var lines = File(filePath).readAsStringSync().split('\n');

for (var line in lines) {
Expand Down
Loading

0 comments on commit 10831f2

Please sign in to comment.