Author: Peter Jochum
Write regular expressions for the following character sets, or give reasons why no expression can be written:
a[a-z]*a|a
a[a-z]*|[a-z]*a
[1-9][0-9]*
^[0-9]*[0|2|4|6|8]
[0-8]*[0|1|3-9]*
#Solution 1 - Thanks to Selene
a*(b|bb)?(a+(b|bb)?)*
# Solution 2 - Thanks to Sebastian
(a* | (ba+) | (bba+))* (b|bb)?
DFA accepting the language:
b*ab*(b*ab*ab*)*$|a*ba*(a*ba*ba*)*
# Selene
(b*ab*ab*)*|(a*ba*ba*)*
Not possible - would need an unlimited number of states to memorize count.
Construct a DFA accepting case, char, continue
Language
A -> AA | (A) |
Generates a set of matching braces
Regex (?R) is a recursion of the whole pattern - Test on regex101
\((?R)*\)
# Examples
(())()
()(((())))
()()()()
Ambiguity shown by finding 2 leftmost or rightmost derivations for an input string:
Input string: ()
Leftmost derivation 1:
A -> (A)
-> ()
Leftmost derivation 2:
A -> AA
-> (A)A
-> ()A
-> ()
The following grammar generates all regular rexpressions over the alphabet of letters. (vertical bar in "" is operator).
rexp -> rexp "|" rexp
| rexp rexp
| rexp *
| (rexp)
| letter
Step | |
---|---|
rexp | rexp* |
(rexp)* | |
(rexp | rexp)* | |
(rexp rexp|rexp)* | |
(a rexp|rexp)* | |
(ab|rexp)* | |
(ab|b)* |
Input string: abc
Leftmost derivation 1:
Step | |
---|---|
rexp | rexp rexp |
a rexp | |
a rexp rexp | |
a b rexp | |
a b c |
Leftmost derivation 2:
Step | |
---|---|
rexp | rexp rexp |
rexp rexp rexp | |
a rexp rexp | |
a b rexp | |
a b c |
rexp -> rexp "|" C | C
C -> C F | F
F -> L * | L
L -> (rexp) | letter
d. What associativity does your answer in c. give to the binary operators? Why?:
Left recursion -> left associativity