Longest lexeme rule.
(longest-lexp trees) → yes/no
Some ambiguities in the lexical grammar arise
because some lexemes are concatenations of others.
In the example in grammar-lexp,
These ambiguities are eliminated
by imposing the extra-grammatical requirement that
lexing always take the longest possible sequence of characters
that may constitute a lexeme,
regardless of what follows.
In particular, this requirement applies even when
taking the longest sequence of characters causes
subsequent lexing and parsing to fail,
whereas taking a shorter sequence would let
subsequent lexing and parsing succeed.
For example,
This longest lexeme rule is formalized by this predicate,
which applies to the CST sequence
Given a Unicode character sequence
Theorem:
(defthm exist-longer-lexeme-p-suff (implies (and (abnf-tree-with-root-p tree1 "lexeme") (prefixp (abnf::tree->string tree) (abnf::tree->string tree1)) (not (equal (abnf::tree->string tree) (abnf::tree->string tree1))) (prefixp (abnf::tree->string tree1) (abnf::tree-list->string trees))) (exist-longer-lexeme-p tree trees)))
Theorem:
(defthm booleanp-of-exist-longer-lexeme-p (b* ((yes/no (exist-longer-lexeme-p tree trees))) (booleanp yes/no)) :rule-classes :rewrite)
Theorem:
(defthm exist-longer-lexeme-p-of-tree-fix-tree (equal (exist-longer-lexeme-p (abnf::tree-fix tree) trees) (exist-longer-lexeme-p tree trees)))
Theorem:
(defthm exist-longer-lexeme-p-tree-equiv-congruence-on-tree (implies (abnf::tree-equiv tree tree-equiv) (equal (exist-longer-lexeme-p tree trees) (exist-longer-lexeme-p tree-equiv trees))) :rule-classes :congruence)
Theorem:
(defthm exist-longer-lexeme-p-of-tree-list-fix-trees (equal (exist-longer-lexeme-p tree (abnf::tree-list-fix trees)) (exist-longer-lexeme-p tree trees)))
Theorem:
(defthm exist-longer-lexeme-p-tree-list-equiv-congruence-on-trees (implies (abnf::tree-list-equiv trees trees-equiv) (equal (exist-longer-lexeme-p tree trees) (exist-longer-lexeme-p tree trees-equiv))) :rule-classes :congruence)
Function:
(defun longest-lexp (trees) (declare (xargs :guard (abnf::tree-listp trees))) (let ((__function__ 'longest-lexp)) (declare (ignorable __function__)) (or (endp trees) (endp (cdr trees)) (and (not (exist-longer-lexeme-p (car trees) trees)) (longest-lexp (cdr trees))))))
Theorem:
(defthm booleanp-of-longest-lexp (b* ((yes/no (longest-lexp trees))) (booleanp yes/no)) :rule-classes :rewrite)
Theorem:
(defthm longest-lexp-of-tree-list-fix-trees (equal (longest-lexp (abnf::tree-list-fix trees)) (longest-lexp trees)))
Theorem:
(defthm longest-lexp-tree-list-equiv-congruence-on-trees (implies (abnf::tree-list-equiv trees trees-equiv) (equal (longest-lexp trees) (longest-lexp trees-equiv))) :rule-classes :congruence)