In computer science, in the area of formal language theory, frequent use is made of a variety of string functions; however, the notation used is different from that used for computer programming, and some commonly used functions in the theoretical realm are rarely used when programming. This article defines some of these basic terms.
Contents
Strings and languages
A string is a finite sequence of characters. The empty string is denoted by
For example,
A language is a finite or infinite set of strings. Besides the usual set operations like union, intersection etc., concatenation can be applied to languages: if both
The language
For example, abbreviating
Alphabet of a string
The alphabet of a string is the set of all of the characters that occur in a particular string. If s is a string, its alphabet is denoted by
The alphabet of a language
For example, the set
String substitution
Let L be a language, and let Σ be its alphabet. A string substitution or simply a substitution is a mapping f that maps letters in Σ to languages (possibly in a different alphabet). Thus, for example, given a letter a ∈ Σ, one has f(a)=La where La ⊆ Δ* is some language whose alphabet is Δ. This mapping may be extended to strings as
f(ε)=εfor the empty string ε, and
f(sa)=f(s)f(a)for string s ∈ L and letter a ∈ Σ. String substitutions may be extended to entire languages as
Regular languages are closed under string substitution. That is, if each letter of a regular language is substituted by another regular language, the result is still a regular language. Similarly, context-free languages are closed under string substitution.
A simple example is the conversion fuc(.) to upper case, which may be defined e.g. as follows:
For the extension of fuc to strings, we have e.g.
For the extension of fuc to languages, we have e.g.
Another example is the conversion of an EBCDIC-encoded string to ASCII.
String homomorphism
A string homomorphism (often referred to simply as a homomorphism in formal language theory) is a string substitution such that each letter is replaced by a single string. That is, f(a)=s, where s is a string, for each letter a.
String homomorphisms are monoid morphisms on the free monoid, preserving the empty string and the binary operation of string concatenation. Given a language L, the set f(L) is called the homomorphic image of L. The inverse homomorphic image of a string s is defined as
f−1(s) = { w | f(w) = s }while the inverse homomorphic image of a language L is defined as
f−1(L) = { s | f(s) ∈ L }In general, f(f−1(L)) ≠ L, while one does have
f(f−1(L)) ⊆ Land
L ⊆ f−1(f(L))for any language L.
The class of regular languages is closed under homomorphisms and inverse homomorphisms. Similarly, the context-free languages are closed under homomorphisms and inverse homomorphisms.
A string homomorphism is said to be ε-free (or e-free) if f(a) ≠ ε for all a in the alphabet Σ. Simple single-letter substitution ciphers are examples of (ε-free) string homomorphisms.
An example string homomorphism guc can also be obtained by defining similar to the above substitution: guc(‹a›) = ‹A›, ..., guc(‹0›) = ε, but letting guc undefined on punctuation chars. Examples for inverse homomorphic images are
For the latter language, guc(guc−1({ ‹A›, ‹bb› })) = guc({ ‹a› }) = { ‹A› } ≠ { ‹A›, ‹bb› }. The homomorphism guc is not ε-free, since it maps e.g. ‹0› to ε.
String projection
If s is a string, and
Here
String projection may be promoted to the projection of a language. Given a formal language L, its projection is given by
Right quotient
The right quotient of a letter a from a string s is the truncation of the letter a in the string s, from the right hand side. It is denoted as
The quotient of the empty string may be taken:
Similarly, given a subset
Left quotients may be defined similarly, with operations taking place on the left of a string.
Syntactic relation
The right quotient of a subset
The relation is clearly of finite index (has a finite number of equivalence classes) if and only if the family right quotients is finite; that is, if
is finite. In the case that M is the monoid of words over some alphabet, S is then a regular language, that is, a language that can be recognized by a finite state automaton. This is discussed in greater detail in the article on syntactic monoids.
Right cancellation
The right cancellation of a letter a from a string s is the removal of the first occurrence of the letter a in the string s, starting from the right hand side. It is denoted as
The empty string is always cancellable:
Clearly, right cancellation and projection commute:
Prefixes
The prefixes of a string is the set of all prefixes to a string, with respect to a given language:
where
The prefix closure of a language is
Example:
A language is called prefix closed if
The prefix closure operator is idempotent:
The prefix relation is a binary relation