lep.html


001| '''\
001| Logical Expression Parser.
001| 
001| Parses logical expression into an object tree and generates a truth table
001| covering all combinations of detected input variables.
001| 
001| Example: (a | not b) & not (c | not d)
001| 
001| The example above would generate a truth table for the 16 possible input
001| combinations for a, b, c, and d. (Assuming each could be true or false.)
001| 
001| Test run:
001| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
001| command: main
001| args: 1
001| Main:
001| source: (not a | b) & not (not c & d)
001| 
001| Parse...
001| expr.NOT:
001| term.name: a (NEW)
001| expr.t1: a
001| expr.t1: NOT a
001| expr.op: OR
001| term.name: b (NEW)
001| expr.t2: b
001| term.expr: (NOT a OR b)
001| expr.t1: (NOT a OR b)
001| expr.op: AND
001| expr.NOT:
001| expr.NOT:
001| term.name: c (NEW)
001| expr.t1: c
001| expr.t1: NOT c
001| expr.op: AND
001| term.name: d (NEW)
001| expr.t2: d
001| term.expr: (NOT c AND d)
001| expr.t1: (NOT c AND d)
001| expr.t2: NOT (NOT c AND d)
001| 
001| Terms...
001| ['a', 'c', 'b', 'd']
001| 
001| expression: ((NOT a OR b) AND NOT (NOT c AND d))
001| Truth Table:
001|   | a      c      b      d       | OUT
001|  0: False  False  False  False   = True
001|  1: True   False  False  False   = False
001|  2: False  True   False  False   = True
001|  3: True   True   False  False   = False
001|  4: False  False  True   False   = True
001|  5: True   False  True   False   = True
001|  6: False  True   True   False   = True
001|  7: True   True   True   False   = True
001|  8: False  False  False  True    = False
001|  9: True   False  False  True    = False
001| 10: False  True   False  True    = True
001| 11: True   True   False  True    = False
001| 12: False  False  True   True    = False
001| 13: True   False  True   True    = False
001| 14: False  True   True   True    = True
001| 15: True   True   True   True    = True
001| 
001| --
001| done: <type 'tuple'> (2)
001| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
001| 
001| '''

002| ####################################################################################################
003| from sys import argv
004| from lep_parser import source, parse_expression
005| ####################################################################################################
006| 
007| 
008| ##------------------------------------------------------------------------------------------------##
009| samples = []
010| samples.append('((A xor B) xor Ci)')
011| samples.append('(((A xor B) and Ci) or (A and B))')
012| samples.append('not foo and not bar')
013| samples.append('(a | b) & not (a & b)')
014| samples.append('(a & not b) | (not a & b)')
015| samples.append('(a and not b) or (not a and b)')
016| samples.append('(not a | b) & not (not c & d)')
017| samples.append('(foo and bar) xor not (boo or far)')
018| samples.append('not ((not (foo & not bar) | not (foo & baz)) | ((bar & zip) | not (foo | zip)))')
019| 
020| sample_text = samples[0]
021| 
022| ##================================================================================================##
023| def gen_truth_table (expr, terms):
024|     print()
025|     print('Truth Table:')
026| 
027|     ns = []
028|     for ix in range(len(terms)):
029|         ns.append('%-5s ' % terms[ix].n)
030|     s = ''.join(ns)
031|     h1 = ' | %s | =' % s
032|     h2 = '---|--%s-|------' % ('-' * len(s))
033| 
034|     print(h1)
035|     print(h2)
036|     for bx in range(2 ** len(terms)):
037|         ts = []
038|         for ix in range(len(terms)):
039|             terms[ix].v = True if (bx & (2 ** ix)) else False
040|             ts.append('%-5s ' % terms[ix].v)
041|         v = expr()
042|         print('%3d| %s = %-5s' % (bx, ''.join(ts), v))
043|     print(h2)
044|     print()
045| 
046| 
047| ##================================================================================================##
048| def do_test (*args):
049|     print('Test:')
050|     print('test/parameters: %d' % len(args))
051|     txt = args[0] if 0 < len(args) else sample_text
052|     src = source(txt)
053| 
054|     print('parse: "%s"' % src)
055|     try:
056|         expr = parse_expression(src)
057|     except Exception as e:
058|         print(e)
059|         raise
060| 
061|     print('terms: %s' % list(src.lib.keys()))
062|     ts = []
063|     for t in list(src.lib.values()):
064|         ts.append(t)
065|     print([str(t) for t in ts])
066| 
067|     print()
068|     print('expression: %s' % expr)
069|     gen_truth_table(expr, ts)
070| 
071|     return 'Done'
072| ##================================================================================================##
073| def do_main (*args):
074|     print('main/parameters: %d' % len(args))
075|     print()
076|     for txt in args:
077|         try:
078|             src = source(txt)
079|             expr = parse_expression(src)
080|             print('expression: %s' % expr)
081|             ts = list(src.lib.values())
082|             print(ts)
083|             gen_truth_table(expr, ts)
084|         except:
085|             raise
086|     return 'Done'
087| ####################################################################################################
088| def dispatch (command, *args):
089|     print('command: %s' % cmd)
090|     print('arguments: %d' % len(args))
091|     if command == 'test': return do_test(*args)
092|     if command == 'do': return do_main(*args)
093|     return 'Nothing to do!'
094| ####################################################################################################
095| if __name__ == '__main__':
096|     print('autorun: %s' % argv[0])
097|     cmd = argv[1] if 1 < len(argv) else 'do'
098|     etc = argv[2:] if 2 < len(argv) else samples[0:2] #[sample_text]
099|     obj = dispatch(cmd, *etc)
100|     print(obj)
101| ####################################################################################################
102| '''eof'''
103|