Languages and Automata
Languages and Automata
Concept
Examples:
Formal Definition:
● Alphabet: A finite set of symbols (e.g., {a, b, c}).
● Regular Expression: A recursive structure built from an alphabet using the following
rules:
○ Base Cases:
■ Empty String: Represented by ε, matches the empty string.
■ Single Characters: Each character from the alphabet is a regular
expression.
○ Operators:
■ Concatenation: If R1 and R2 are regular expressions, then R1R2
represents the concatenation of R1 and R2.
■ Union: If R1 and R2 are regular expressions, then R1 | R2
represents the union (alternation) of R1 and R2.
■ Kleene Star: If R is a regular expression, then R* represents zero or
more repetitions of R.
■ Optional: If R is a regular expression, then R? represents zero or one
occurrence of R.
■ Repetitions: {n}, {n,}, and {n,m} specify the exact, minimum, and
range of repetitions, respectively.
Basic Symbols:
● a, b, ...: Represents individual characters.
● ε (epsilon): Represents the empty string.
● . (dot) : Represents any character (in some implementations).
● []: Defines a character class. For example, [abc] matches a, b, or c.
Quantifiers
In regular expressions, quantifiers control how many times an element in the pattern should
appear. Here's a breakdown of how each quantifier works, separated and explained:
1. * (Kleene Star):
○ Meaning: Matches zero or more occurrences of the preceding element.
○ Example: a* matches "", a, aa, aaa, etc.
2. + (Kleene Plus):
○ Meaning: Matches one or more occurrences of the preceding element.
○ Example: a+ matches a, aa, aaa, but not "".
3. ? (Optional):
○ Meaning: Matches zero or one occurrence of the preceding element.
○ Example: a? matches "" or a.
4. {n} (Exact Count):
○ Meaning: Matches exactly n occurrences of the preceding element.
○ Example: a{3} matches aaa but not aa or aaaa.
5. {n,} (At Least n):
○ Meaning: Matches n or more occurrences of the preceding element.
○ Example: a{2,} matches aa, aaa, aaaa, etc.
6. {n,m} (Between n and m):
○ Meaning: Matches between n and m occurrences of the preceding element.
○ Example: a{2,4} matches aa, aaa, or aaaa, but not a or aaaaa.
Examples
1. Regular Expression: abc
○ Description: Matches the exact string abc.
2. Regular Expression: a|b (or operator)
○ Description: Matches either a or b.
3. Regular Expression: a+
○ Description: Matches one or more occurrences of a.
4. Regular Expression: [a-z]
○ Description: Matches any lowercase letter from a to z.
5. Regular Expression: \d+
○ Description: Matches one or more digits.
6. Regular Expression: [A-Za-z0-9]
○ Description: Matches any alphanumeric character (uppercase, lowercase, or
digit).
7. Regular Expression: ^hello
○ Description: Matches any string that starts with hello.
8. Regular Expression: world$
○ Description: Matches any string that ends with world.
9. Regular Expression: a{2,5}
○ Description: Matches between 2 and 5 occurrences of a.
10. Regular Expression: (\d{3})-(\d{3})-(\d{4})
○ Description: Matches a phone number in the format xxx-xxx-xxxx where
x is a digit.