master > master: code
This commit is contained in:
parent
fde13a92f5
commit
73f905a5cd
65
code/main.py
65
code/main.py
@ -10,12 +10,13 @@ import os;
|
||||
import sys;
|
||||
sys.tracebacklimit = 0;
|
||||
from itertools import product;
|
||||
from lark import Tree;
|
||||
from textwrap import dedent;
|
||||
from typing import Dict;
|
||||
from typing import Generator;
|
||||
from typing import List;
|
||||
from typing import Tuple;
|
||||
from typing import Union;
|
||||
from lark import Tree;
|
||||
|
||||
sys.path.insert(0, os.getcwd());
|
||||
|
||||
@ -41,32 +42,68 @@ I = ['A0', 'A2'];
|
||||
|
||||
def main():
|
||||
tree = string_to_parts(zeichenkette);
|
||||
print('Syntaxbaum von \033[1m{}\033[0m:\n'.format(zeichenkette));
|
||||
print(dedent(
|
||||
'''
|
||||
Syntaxbaum von
|
||||
F := \033[92;1m{F}\033[0m:
|
||||
'''.format(
|
||||
F=zeichenkette,
|
||||
)
|
||||
));
|
||||
print(tree.pretty());
|
||||
val = rekursiv_eval(tree, I)
|
||||
print('eval(Formel, I) = \033[1m{}\033[0m'.format(val));
|
||||
print(dedent(
|
||||
'''
|
||||
eval(F, I) = \033[94;1m{eval}\033[0m;
|
||||
atoms(F) = \033[94;1m{atoms}\033[0m; \033[91;1m<- *\033[0m
|
||||
depth(F) = \033[94;1m{d}\033[0m; \033[91;1m<- *\033[0m
|
||||
length(F) = \033[94;1m{l}\033[0m; \033[91;1m<- *\033[0m
|
||||
#parentheses(F) = \033[94;1m{p}\033[0m; \033[91;1m<- *\033[0m
|
||||
|
||||
\033[91;1m*\033[0m \033[2mnoch nicht implementiert!\033[0m
|
||||
\033[1;2;4mChallenge:\033[0m \033[2mschreibe diese Methoden. Probiere mit Stift-und-Zettel die Methoden händisch auszuführen und vergleiche mit dem Code-Output.\033[0m
|
||||
'''.format(
|
||||
eval = rekursiv_eval(tree, I),
|
||||
atoms = rekursiv_atoms(tree),
|
||||
d = rekursiv_depth(tree),
|
||||
l = rekursiv_length(tree),
|
||||
p = rekursiv_parentheses(tree),
|
||||
)
|
||||
));
|
||||
return;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# SEKUNDÄRVORGÄNGE
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def rekursiv_eval(fml: Tree, I: List[str]) -> bool:
|
||||
if fml.data == 'atom':
|
||||
def rekursiv_atoms(fml: Tree) -> List[str]:
|
||||
## Herausforderung: schreibe diese Funktion!
|
||||
return [];
|
||||
|
||||
def rekursiv_depth(fml: Tree) -> int:
|
||||
## Herausforderung: schreibe diese Funktion!
|
||||
return 0;
|
||||
|
||||
def rekursiv_length(fml: Tree) -> int:
|
||||
## Herausforderung: schreibe diese Funktion!
|
||||
return 0;
|
||||
|
||||
def rekursiv_parentheses(fml: Tree) -> int:
|
||||
## Herausforderung: schreibe diese Funktion!
|
||||
return 0;
|
||||
|
||||
def rekursiv_eval(fml: Tree, I: List[str]) -> int:
|
||||
if fml.data in ['atom', 'beliebig']:
|
||||
index = fml.children[0];
|
||||
return 'A{}'.format(index) in I;
|
||||
elif fml.data == 'beliebig':
|
||||
name = fml.children[0];
|
||||
return '{}'.format(name) in I;
|
||||
return 1 if ('{}'.format(index) in I) else 0;
|
||||
elif fml.data == 'wahr':
|
||||
return True;
|
||||
return 1;
|
||||
elif fml.data == 'falsch':
|
||||
return False;
|
||||
return 0;
|
||||
elif fml.data == 'negation':
|
||||
teilformel1 = fml.children[1];
|
||||
if isinstance(teilformel1, Tree):
|
||||
val1 = rekursiv_eval(teilformel1, I);
|
||||
return not val1;
|
||||
return 1 - val1;
|
||||
elif fml.data == 'konjunktion':
|
||||
teilformel1 = fml.children[0];
|
||||
teilformel2 = fml.children[2];
|
||||
@ -87,7 +124,7 @@ def rekursiv_eval(fml: Tree, I: List[str]) -> bool:
|
||||
if isinstance(teilformel1, Tree) and isinstance(teilformel2, Tree):
|
||||
val1 = rekursiv_eval(teilformel1, I);
|
||||
val2 = rekursiv_eval(teilformel2, I);
|
||||
return (val1 <= val2);
|
||||
return 0 if val1 == 1 and val2 == 0 else 1;
|
||||
else:
|
||||
raise Exception('Evaluation nicht möglich!');
|
||||
return True;
|
||||
|
@ -33,7 +33,7 @@ lexerAussagenlogik = Lark(
|
||||
?atomic: false | true | atom | generic
|
||||
?false: "0" -> wahr
|
||||
?true: "1" -> falsch
|
||||
?atom: "A" /[0-9]+/ -> atom
|
||||
?atom: /A[0-9]+/ -> atom
|
||||
?generic: "{" /((?!({|})).)+/ "}" -> beliebig
|
||||
|
||||
// Symbole
|
||||
@ -44,8 +44,8 @@ lexerAussagenlogik = Lark(
|
||||
|
||||
// Junktoren
|
||||
?expr_not: conn_not expr -> negation
|
||||
?expr_and: "(" expr conn_or expr ")" -> konjunktion
|
||||
?expr_or: "(" expr conn_and expr ")" -> disjunktion
|
||||
?expr_and: "(" expr conn_and expr ")" -> konjunktion
|
||||
?expr_or: "(" expr conn_or expr ")" -> disjunktion
|
||||
?expr_implies: "(" expr conn_impl expr ")" -> implikation
|
||||
''',
|
||||
start="expr",
|
||||
|
Loading…
Reference in New Issue
Block a user