Skip to content

Commit

Permalink
добавили конюнктивный разбор
Browse files Browse the repository at this point in the history
  • Loading branch information
Encapsulateed committed Jan 21, 2024
1 parent 3c78133 commit e38743d
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 181 deletions.
1 change: 1 addition & 0 deletions lab5/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.dart_tool/
.idea/
*.lock
*.iml
# Default ignored files
/shelf/
Expand Down
48 changes: 42 additions & 6 deletions lab5/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,52 @@ import 'src/state_machine/FSM.dart';
import 'src/utils/Production.dart';
import 'src/utils/conjunctiveGrammar.dart';
import 'src/utils/stack.dart';
import 'dart:io';

// import 'src/lr0/base/LR0Fms.dart';

void main(List<String> arguments) {
List<Grammar> grammars = [];
// dart main .dart -w"input your word" -c

Map<String, String> input_params = {};
String word = '';
bool conj = false;

for (var argument in arguments) {
var match_word = RegExp(r'-w(\w+)').firstMatch(argument);
var match_conj = RegExp(r'-c').firstMatch(argument);
if (match_word != null) {
word = match_word.group(1)!;
}

if (match_conj != null) {
conj = true;
}
}

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

for(var grammar in cg.possible_grammars){
print(grammar);
}
}
List<bool> results = [];
for (var grammar in cg.possible_grammars) {
LR0Parser curr_parser = LR0Parser(grammar);

curr_parser.Log(cg.possible_grammars.indexOf(grammar) + 1);

results.add(curr_parser.Parse(word));
}

if (conj == false) {
if (results.any((element) => element == true)) {
print('Существует хотя бы один корректный разбор ');
} else {
print('слово не принадлежит языку введеёной грамматики');
}
}
else{
if (!results.any((element) => element == true)) {
print('Существует хотя бы один корректный разбор ');
} else {
print('слово не принадлежит языку введеёной грамматики');
}
}
}
19 changes: 6 additions & 13 deletions lab5/src/lr0/LR0Table.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class LR0Table {
_grammar.terminals.add('\$');
makeTable();
_grammar.terminals.remove('\$');
_fsm.DumpToDOT();
}

}
LR0FMS get(){
return _fsm;
}
void makeTable() {
for (var I in _fsm.states) {
int index = _fsm.getStateIndex(I);
Expand Down Expand Up @@ -75,18 +77,9 @@ class LR0Table {
}
}

void log() {
_fsm.DumpToDOT();
for (var stateId in lr0_table.keys) {
for (var colId in lr0_table[stateId]!.keys) {
print(
'ACTION AT T[$stateId][$colId] === ${lr0_table[stateId]![colId]}');
}
}
}

void logToFile() {
var file = File('values/table.txt');
void logToFile(String path) {
var file = File(path);

// Открываем файл для записи
var sink = file.openWrite();
Expand Down
65 changes: 20 additions & 45 deletions lab5/src/lr0/lr0Parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import '../utils/Action.dart';
import '../utils/grammar.dart';
import '../utils/stack.dart';
import 'LR0Table.dart';
import 'dart:io';

class LR0Parser {
LR0Table _table = LR0Table.emtpy();
Expand All @@ -15,8 +16,6 @@ class LR0Parser {
LR0Parser(Grammar grammar) {
_grammar = grammar;
_table = LR0Table(_grammar);

_table.logToFile();
}

void parseLR0_params(
Expand Down Expand Up @@ -190,55 +189,31 @@ class LR0Parser {
return nodes_id_sequense--;
}

void print_stack(Map<int, GSSNode<List<String>>> Nodes) {
print('STACK END\n===========================');
for (var node in Nodes.values) {
if (node.value[0] != 'null') print('${node.value} ${node.prev}');
}
print('STACK START\n===========================');
}

bool ParseGss(String word, int n) {
List<Stack<String>> tokenStacks = [];
String getStrFromStack(Stack<String> inputStack, {bool reverse = true}) {
StringBuffer sb = StringBuffer();
List<String> tokens = inputStack.toList();

return false;
}

void getPrevSets(
GSSNode<List<String>> node, Set<GSSNode<List<String>>> all_prev) {
if (node.my_id == 0) {
return;
if (reverse) {
for (int i = tokens.length - 1; i >= 0; i--) {
sb.write('${tokens[i]} ');
}
} else {
for (int i = 0; i < tokens.length; i++) {
sb.write('${tokens[i]} ');
}
}
all_prev.addAll(node.prevSetValued());

all_prev.map((e) => getPrevSets(e, all_prev));
return sb.toString();
}
}

List<int> getNotNull(Map<int, GSSNode<List<String>>> nodes) {
var for_out = <int>[];

for (var key in nodes.keys) {
if (nodes[key]!.value[0] != 'null') {
for_out.add(key);
}
}
return for_out;
}

String getStrFromStack(Stack<String> inputStack, {bool reverse = true}) {
StringBuffer sb = StringBuffer();
List<String> tokens = inputStack.toList();
void Log(int index) {
String path = 'values/grammar_$index/';
var directory = Directory(path);

if (reverse) {
for (int i = tokens.length - 1; i >= 0; i--) {
sb.write('${tokens[i]} ');
}
} else {
for (int i = 0; i < tokens.length; i++) {
sb.write('${tokens[i]} ');
if (!directory.existsSync()) {
directory.createSync(recursive: true);
}
this._table.logToFile('${path}table.txt');
this._table.get().DumpToDOT('${path}fsm.txt');
}

return sb.toString();
}
4 changes: 2 additions & 2 deletions lab5/src/state_machine/FSM.dart
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class FSM {
}

// метод представления автомата в формате DOT
void DumpToDOT() {
void DumpToDOT(String path) {
String res = "";

for (var state in this.states) {
Expand All @@ -228,7 +228,7 @@ class FSM {
"$res"
"}\n";

File file = File('values/fsm.txt');
File file = File(path);
file.writeAsStringSync(res);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lab5/src/tests/DFSM_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void main() {

// Детерминизация автомата
FSM determinizedFSM = fsm.determinize();
determinizedFSM.DumpToDOT();


expect(determinizedFSM.states.length, greaterThan(0));
print(determinizedFSM.states.length);
Expand Down
18 changes: 0 additions & 18 deletions lab5/values/actions.txt

This file was deleted.

18 changes: 0 additions & 18 deletions lab5/values/final.txt

This file was deleted.

71 changes: 0 additions & 71 deletions lab5/values/fsm.txt

This file was deleted.

38 changes: 38 additions & 0 deletions lab5/values/grammar_1/fsm.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
digraph {
rankdir = LR
dummy [shape=none, label="", width=0, height=0]
"0 S0 -> ·S
S -> ·AA
A -> ·a
A -> ·a" [label = "0 S0 -> ·S
S -> ·AA
A -> ·a
A -> ·a", shape = circle]
"1 S0 -> S·" [label = "1 S0 -> S·", shape = doublecircle]
"2 S -> A·A
A -> ·a" [label = "2 S -> A·A
A -> ·a", shape = circle]
"3 A -> a·" [label = "3 A -> a·", shape = doublecircle]
"4 S -> AA·" [label = "4 S -> AA·", shape = doublecircle]
dummy -> "0 S0 -> ·S
S -> ·AA
A -> ·a
A -> ·a"
"0 S0 -> ·S
S -> ·AA
A -> ·a
A -> ·a" -> "1 S0 -> S·" [label = "S"]
"0 S0 -> ·S
S -> ·AA
A -> ·a
A -> ·a" -> "2 S -> A·A
A -> ·a" [label = "A"]
"0 S0 -> ·S
S -> ·AA
A -> ·a
A -> ·a" -> "3 A -> a·" [label = "a"]
"2 S -> A·A
A -> ·a" -> "4 S -> AA·" [label = "A"]
"2 S -> A·A
A -> ·a" -> "3 A -> a·" [label = "a"]
}
6 changes: 6 additions & 0 deletions lab5/values/grammar_1/table.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
State S A a $
0 [1] [2] [s(3)] []
1 [] [] [] [ACC]
2 [] [4] [s(3)] []
3 [] [] [r(2)] [r(2)]
4 [] [] [r(1)] [r(1)]
Loading

0 comments on commit e38743d

Please sign in to comment.