introduction to parsing parsing first-order logic |in this last pyparsing subsection we will create parser for dsl for expressing formulas in first-order logic this has the most complex bnf of all the examples in the and the implementation requires us to handle operatorsincluding their precedences and associativitiessomething we have not needed to do so far there is no handcrafted version of this parser--once we have reached this level of complexity it is better to use parser generator but in addition to the pyparsing version shown herein the following section' last subsection there is an equivalent ply parser for comparison ply first-order logic parser here are few examples of the kind of first-order logical formulas that we want to be able to parsea forall xa exists ya - true true true -forall xexists ytrue (forall xexists ytrue-true true -true true forall xx true (forall xx xforall xx true (forall xx xtrue we have opted to use ascii characters rather than the proper logical operator symbolsto avoid any distraction from the parser itself sowe have used forall for exists for -for (implies)for (logical or)for (logical and)and for (logical notsince python strings are unicode it would be easy to use the real symbols--or we could adapt the parser to accept both the ascii forms shown here and the real symbols in the formulas shown herethe parentheses make difference in the last two formulas--so those formulas are different--but not for the two above them (those starting with true)which are the same despite the parentheses naturallythe parser must get these details right one surprising aspect of first-order logic is that not (~has lower precedence than equals (=)so is actually ( bthis is why logicians usually put space after bnf for our first-order logic dsl is given in figure for the sake of clarity the bnf does not include any explicit mention of whitespace (no \ or \selements)but we will assume that whitespace is allowed between all terminals and nonterminals although our subset of bnf syntax has no provision for expressing precedence or associativitywe have added comments to indicate associativities for the formulas