Section 2.3: Problem 1 Solution »

Section 2.3: A Parsing Algorithm

The uniqueness of decomposition of terms and wffs is needed for arguments and definitions using recursion.

Terms

For a string of variable, constant and function symbols we define a function as follows:
  • ( is a term by itself, so its appearance in the string adds one complete term).
  • (same for constants).
  • where is an -place function symbol (a function symbol implies that there will be a complete term, , once all terms, arguments of , are found in the string, ).
  • .
For a term , . For any terminal segment of a term , is a concatenation of term, in particular, . For any proper initial segment of a term , , in particular, is not a term.

Parsing Algorithm

The following parsing algorithm for terms ensures that a string of symbols is parsed to a tree iff corresponds to a term, and in this case the tree is uniquely determined and represents the term.
  1. Form a tree with the only vertex being the expression .
  2. If all leaves of the tree are single symbol strings, then if they all are variable or constant symbols, stop(+), otherwise, stop(-). Otherwise select the left-most leaf having at least two symbols.
  3. The first symbol of must be an -place function symbol , otherwise stop(-).
  4. We form leaves below . Scan after the symbol until the initial segment such that is found. Put it the leftmost leaf. Proceed with the remainder of the string the same way for all other leaves below . If the string ended before all terms for leaves are found, or if the last term is found before the end of the string , stop(-). Go to step 2.
stop(+) the tree represents the term corresponding to the initial expression
stop(-) the initial expression does not correspond to any term
  • The algorithm is finite
  • The algorithm attempts to construct the tree in the only possible way, as the choice of each term in step 4 is determined by the two facts: first, must equal (and for every shorter string it was not), and, second, for any proper initial substring of a term , hence, anything longer than would not be a term.
    • Hence, the tree is unique.
    • If the algorithm fails, then does not represent a term, as the only possible way to try to understand (parse) it fails.
    • If the algorithm stops in step 2, then the resulting tree represents the only possible term corresponding to .
In terms of Section 1.4, the uniqueness can be rephrased as follows.
(Unique Readability for Terms) The set of terms is freely generated from the set of variable and constant symbols by the operations.

Formulas

We can further define for other symbols:
  • .
  • .
  • .
  • .
  • .
  • where is an -place predicate symbol.
  • .
For a wff , . For any proper initial segment of a wff , , in particular, is not a wff.

Parsing Algorithm

The following parsing algorithm ensures that a string of symbols is parsed to a tree iff corresponds to a wffs, and in this case the tree is uniquely determined and represents the wff.
  1. Form a tree with the only vertex being the expression .
  2. If all leaves of the tree are atomic formulas (an -place predicate symbol followed by terms), stop(+). Otherwise select the left-most leaf that is not an atomic formula.
  3. The first symbol(s) of must be either or , followed by other symbols, if not, stop(-). If is , form one leaf below it with , and go to step 2.
  4. If the second symbol of is , then there should be at least four symbols in with the last symbol being , i.e. (otherwise stop(-)), form one leaf below with , and go to step 2.
  5. Scan from left until first reaching where is a non-empty string having either or a balance of and (both methods work!), if no such found, then stop(-). There should be a binary connective symbol ( ) after , followed by a non-empty and , otherwise stop(-). We form two leaves, the left one and the right one , below . Go to step 2.
stop(+) the tree represents the wff corresponding to the initial expression
stop(-) the initial expression does not correspond to any wff
  • The algorithm is finite
  • The algorithm attempts to construct the tree in the only possible way, as the choice of in step 5 is determined by the two facts: first, must have or be balanced (and everything less than was not), and, second, any proper initial subexpression of a wff has less than 1 or unequal number of left and right parentheses, hence, anything longer than would not be a proper wff expression.
    • Hence, the tree is unique.
    • If the algorithm fails, then does not represent a wff, as the only possible way to try to understand (parse) it fails.
    • If the algorithm stops in step 2, then the resulting tree represents the only possible wff corresponding to .
In terms of Section 1.4, the uniqueness can be rephrased as follows.
(Unique Readability for Formulas) The set of wffs is freely generated from the set of atomic formulas by the operations , and , .