65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# IMPORTS
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
from __future__ import annotations;
|
|
import re;
|
|
# install: lark; lark-parser; lark-parser[regex].
|
|
# https://lark-parser.readthedocs.io/en/latest/grammar.html
|
|
from lark import Lark;
|
|
from lark import Tree;
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# GLOBALE KONSTANTEN
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
## lexer
|
|
lexerAussagenlogik = Lark(
|
|
'''
|
|
%import common.WS
|
|
%import common.NUMBER
|
|
%import common.WORD
|
|
%ignore WS
|
|
|
|
?start: expr
|
|
|
|
?expr: atomic | expr_not | expr_and | expr_or | expr_implies
|
|
?literal: atomic | "not" atomic
|
|
|
|
// atomische Ausdrücke
|
|
?atomic: false | true | atom | generic
|
|
?false: "0" -> wahr
|
|
?true: "1" -> falsch
|
|
?atom: /A[0-9]+/ -> atom
|
|
?generic: "{" /((?!({|})).)+/ "}" -> beliebig
|
|
|
|
// Symbole
|
|
?conn_not: "!" -> junktor
|
|
?conn_and: /&+/ -> junktor
|
|
?conn_or: /\\|+/ -> junktor
|
|
?conn_impl: /->|=>/ -> junktor
|
|
|
|
// Junktoren
|
|
?expr_not: conn_not expr -> negation
|
|
?expr_and: "(" expr conn_and expr ")" -> konjunktion
|
|
?expr_or: "(" expr conn_or expr ")" -> disjunktion
|
|
?expr_implies: "(" expr conn_impl expr ")" -> implikation
|
|
''',
|
|
start="expr",
|
|
regex=True
|
|
);
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# MAIN METHOD string -> parts
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
def string_to_parts(u: str) -> Tree:
|
|
try:
|
|
u_lexed = lexerAussagenlogik.parse(u);
|
|
return u_lexed;
|
|
except:
|
|
raise Exception('Ausdruck \033[1m{}\033[0m konnte nicht erkannt werden!'.format(u));
|