# Building truth tables

Sun Oct 26 01:00:34 CEST 2008

```On Oct 24, 5:53 am, andrea <kerny... at gmail.com> wrote:
> On 26 Set, 20:01, "Aaron \"Castironpi\" Brady" <castiro... at gmail.com>
> wrote:
>
>
>
> > Good idea.  If you want prefixed operators: 'and( a, b )' instead of
> > 'a and b', you'll have to write your own.  ('operator.and_' is bitwise
> > only.)  It may be confusing to mix prefix with infix: 'impl( a and b,
> > c )', so you may want to keep everything prefix, but you can still use
> > table( f, n ) like Tim said.
>
> After a while I'm back, thanks a lot, the truth table creator works,
> now I just want to parse some strings to make it easier to use.
>
> Like
>
> (P \/ Q) -> S == S
>
> Must return a truth table 2^3 lines...
>
> I'm using pyparsing and this should be really simple, but it doesn't
> allow me to recurse and that makes mu stuck.
> The grammar BNF is:
>
> Var :: = [A..Z]
> Exp ::= Var | !Exp  | Exp \/ Exp | Exp -> Exp | Exp /\ Exp | Exp ==
> Exp
>
> I tried different ways but I don't find a smart way to get from the
> recursive bnf grammar to the implementation in pyparsing...
> Any hint?

Tell you what.  At the risk of "carrot-and-stick, jump-how-high"
tyranny, I'll show you some output of a walk-through.  It should give
you an idea of the process.  You can always ask for more hints.

( ( ( !( R ) /\ ( !( P \/ Q ) ) ) -> S ) == S )
(((!(R)/\(!(P\/Q)))->S)==S)
(((!R/\(!(P\/Q)))->S)==S)
n1 := !R
(((n1/\(!(P\/Q)))->S)==S)
n2 := P\/Q
(((n1/\(!(n2)))->S)==S)
(((n1/\(!n2))->S)==S)
n3 := !n2
(((n1/\(n3))->S)==S)
(((n1/\n3)->S)==S)
n4 := n1/\n3
(((n4)->S)==S)
((n4->S)==S)
n5 := n4->S
((n5)==S)
(n5==S)
n6 := n5==S
(n6)
n6
{'n1': (<function not_ at 0x00A04070>, '!R', ('R',)),
'n2': (<function or_ at 0x00A040F0>, 'P\\/Q', ('P', 'Q')),
'n3': (<function not_ at 0x00A04070>, '!n2', ('n2',)),
'n4': (<function and_ at 0x00A040B0>, 'n1/\\n3', ('n1', 'n3')),
'n5': (<function imp_ at 0x00A04130>, 'n4->S', ('n4', 'S')),
'n6': (<function eq_ at 0x00A04170>, 'n5==S', ('n5', 'S'))}
{'Q': True, 'P': True, 'S': True, 'R': True} True
{'Q': True, 'P': True, 'S': False, 'R': True} False
{'Q': True, 'P': True, 'S': True, 'R': False} True
{'Q': True, 'P': True, 'S': False, 'R': False} False
{'Q': False, 'P': True, 'S': True, 'R': True} True
{'Q': False, 'P': True, 'S': False, 'R': True} False
{'Q': False, 'P': True, 'S': True, 'R': False} True
{'Q': False, 'P': True, 'S': False, 'R': False} False
{'Q': True, 'P': False, 'S': True, 'R': True} True
{'Q': True, 'P': False, 'S': False, 'R': True} False
{'Q': True, 'P': False, 'S': True, 'R': False} True
{'Q': True, 'P': False, 'S': False, 'R': False} False
{'Q': False, 'P': False, 'S': True, 'R': True} True
{'Q': False, 'P': False, 'S': False, 'R': True} False
{'Q': False, 'P': False, 'S': True, 'R': False} True
{'Q': False, 'P': False, 'S': False, 'R': False} True

Before you trust me too much, you might want to check at least some of
these, to see if the starting (complicated) expression is evaluated
correctly.  I didn't.

```