Tutorial 1
Tutorial 1
Tutorial 1
1. a(a|b)*a
2. ((ε|a)b*)*
3. (a|b)*a(a|b)(a|b)
4. a*ba*ba*ba*
5. (aa|bb)*((ab|ba)(aa|bb)*(ab|ba)(aa|bb)*)*
Answer
1. String of a's and b's that start and end with a.
2. String of a's and b's.
3. String of a's and b's that the character third from the last is a.
II. Most languages are case sensitive, so keywords can be written only one way, and
the regular expressions describing their lexeme is very simple. However, some
languages, like SQL, are case insensitive, so a keyword can be written either in
lowercase or in uppercase, or in any mixture of cases. Thus, the SQL keyword
SELECT can also be written select, Select, or sElEcT, for instance. Show how to
write a regular expression for a keyword in a case insensitive language. Illustrate
the idea by writing the expression for "select" in SQL.
Answer
select -> [Ss][Ee][Ll][Ee][Cc][Tt]
OR
Select(S|s)(E|e)(L|l)(E|e)(C|c)(T|t)
2. All strings of lowercase letters in which the letters are in ascending lexicographic
order.
4. All strings of a's and b's that do not contain the substring abb.
5. All strings of a's and b's that do not contain the subsequence abb.
Answer
1、
2、
a* b* ... z*
3、
\/\*([^*"]*|".*"|\*+[^/])*\*\/
b*(a+b?)*
5、
b* | b*a+ | b*a+ba*
1. The first ten letters (up to "j") in either upper or lower case.
3. The "digits" in a hexadecimal number (choose either upper or lower case for the
"digits" above 9).
4. The characters that can appear at the end of alegitimate English sentence (e.g. ,
exclamation point) .
Answer
1. [A-Ja-j]
3. [0-9a-f]
4. [.?!]
V. Note that these regular expressions give all of the following symbols (operator
characters) a special meaning:
\ " . ^ $ [ ] * + ? { } | /
Their special meaning must be turned off if they are needed to represent themselves in a
character string. We can do so by quoting the character within a string of length one or more;
e.g., the regular expression "**" matches the string ** . We can also get the literal meaning of
an operator character by preceding it by a backslash. Thus, the regular expression \*\* also
matches the string **. Write a regular expression that matches the string "\.
Answer
\"\\
VI. The operator ^ matches the left end of a line, and $ matches the right end of a line.
The operator ^ is also used to introduce complemented character classes, but the
context always makes it clear which meaning is intended. For example,
^[^aeiou]*$ matches any complete line that does not contain a lowercase vowel.
Answer:
if ^ is in a pair of brackets, and it is the first letter, it means complemented classes, or it means the
left end of a line.
Arithmetic operators
+
return (arithop, ‘+’)
0 1
-
return (arithop, ‘-’)
* 2
% /