0% found this document useful (0 votes)
997 views8 pages

Exercises For Section 3.3

The document discusses the key concepts in lexical analysis including: - Tokens are abstract symbols consisting of a name and attribute value. Patterns describe the form of lexemes which are sequences of characters matching a token. - Regular expressions are used to specify patterns of lexemes. They define languages as sets of strings over an alphabet. - Key components of regular expressions include the empty string, concatenation, union, closure, and parentheses for grouping.

Uploaded by

Mule Won Tege
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
997 views8 pages

Exercises For Section 3.3

The document discusses the key concepts in lexical analysis including: - Tokens are abstract symbols consisting of a name and attribute value. Patterns describe the form of lexemes which are sequences of characters matching a token. - Regular expressions are used to specify patterns of lexemes. They define languages as sets of strings over an alphabet. - Key components of regular expressions include the empty string, concatenation, union, closure, and parentheses for grouping.

Uploaded by

Mule Won Tege
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

a) L à [b-d f-h j-n p-t v-z] String à L*(a|A)+ L*(e|E)+ L*(i|I ) + L*(o|O)+ L*(u|U)+L*

b) A*B*...Z*
c) S1 : the set of all characters and "*/" S2 : S1-/,* Comment -> /* (/* ** (S2 S1*)*)* */
d) want à 0|A?0?1(A0?1|01)*A?0?|A0? A à 0?2(02)*
e) want à (FE*G|(aa)*b)(E|FE*G) E à b(aa)*b F à...

Exercises for Section 3.3


3.3.1
Consult the language reference manuals to determine

1. the sets of characters that form the input alphabet (excluding those that may
only appear in character strings or comments)
2. the lexical form of numerical constants, and
3. the lexical form of identifiers,

for each of the following languages:

1. C
2. C++
3. C#
4. Fortran
5. Java
6. Lisp
7. SQL

3.3.2
Describe the languages denoted by the following regular expressions:

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.
4. String of a's and b's that only contains three b.
5. String of a's and b's that has a even number of a and b.

3.3.3
In a string of length n, how many of the following are there?

1. Prefixes.
2. Suffixes.
3. Proper prefixes.
4. ! Substrings.
5. ! Subsequences.

Answer

1. n + 1
2. n + 1
3. n - 1
4. C(n+1,2) + 1 (need to count epsilon in)
5. Σ(i=0,n) C(n, i)

3.3.4
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]

3.3.5
!Write regular definitions for the following languages:

1. All strings of lowercase letters that contain the five vowels in order.
2. All strings of lowercase letters in which the letters are in ascending lexicographic
order.
3. Comments, consisting of a string surrounded by /* and */, without an intervening
*/, unless it is inside double-quotes (")
4. !! All strings of digits with no repeated digits. Hint: Try this problem first with a
few digits, such as {O, 1, 2}.
5. !! All strings of digits with at most one repeated digit.
6. !! All strings of a's and b's with an even number of a's and an odd number of b's.
7. The set of Chess moves,in the informal notation,such as p-k4 or kbp*qn.
8. !! All strings of a's and b's that do not contain the substring abb.
9. All strings of a's and b's that do not contain the subsequence abb.

Answer

1、

want -> other* a (other|a)* e (other|e)* i (other|i)* o (other|o)* u (other|u)*


other -> [bcdfghjklmnpqrstvwxyz]

2、

a* b* ... z*

3、

\/\*([^*"]*|".*"|\*+[^/])*\*\/

4、

want -> 0|A?0?1(A0?1|01)*A?0?|A0?


A -> 0?2(02)*

Steps:

step1. Transition diagram

step2. GNFA
step3. Remove node 0 and simplify

step4. Remove node 2 and simplify

step5. Remove node 1 and simplify

5、

want -> (FE*G|(aa)*b)(E|FE*G)


E -> b(aa)*b
F -> a(aa)*b
G -> b(aa)*ab|a
F -> ba(aa)*b

Steps:

step1. Transition diagram

step2. GNFA

step3. Remove node A and simplify

step4. Remove node D and simplify

step5. Remove node C and simplify

8、

b*(a+b?)*

9、

b* | b*a+ | b*a+ba*

3.3.6
Write character classes for the following sets of characters:

1. The first ten letters (up to "j") in either upper or lower case.
2. The lowercase consonants.
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]
2. [bcdfghjklmnpqrstvwxzy]
3. [0-9a-f]
4. [.?!]

3.3.7
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

\"\\

3.3.9 !
The regular expression r{m, n} matches from m to n occurrences of the pattern r. For
example, a [ 1 , 5] matches a string of one to five a's. Show that for every regular
expression containing repetition operators of this form, there is an equivalent regular
expression without repetition operators.

Answer

r{m,n} is equals to r.(m).r | r.(m + 1).r | ... | r.(n).r

3.3.10 !
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.

1. How do you tell which meaning of ^ is intended?


2. Can you always replace a regular expression using the ^ and $ operators by an
equivalent expression that does not use either of these operators?

Answer

1. if ^ is in a pair of brakets, and it is the first letter, it means complemented classes,


or it means the left end of a line.

Token: a two tuple abstract symbol <name, attribute


value>
Pattern: description of the form or representation of
lexemes.
Lexeme: sequence of characters that match with a
pattern of a token identified by lexical analyzer as
instance of token.

Eg- printf("Sum=%d\n",total);

printf and total are lexemes matching the pattern for


token id, "sum=%d\n" is lexeme matching literal.

Specification of Tokens
Regular expressions are used to specify lexeme patterns.

Strings and Languages


Alphabet - finite set of symbols. Ex - {0,1} is binary
alphabet, ASCII is a popular alphabet.
String - finite sequence of symbols belonging to
alphabet.
Language - finite set of strings over a specific alphabet.
Substring - a smaller set of consecutive elements of
string. Ex- pil from compiler.
Subsequence - a smaller set of elements in any order from
string obtained by deleting zero or more elements. Ex
- cpile from compiler.

Regular Expressions
Mathematically, a regular expression is defined as - 
    
        1. ε is a regular expression 
                    L ( ε ) = { ε }
           It is the language consisting of only the empty
string.

        2. r = a is another regular expression for the


language -  
          L(a)={a}

        3. ( a ) + ( b )  →  L ( a )  U  L ( b )
2.             ( a ) | ( b )  →  L ( a )  U  L ( b )
3.             ( a ) . ( b )  →  L ( a )  .  L ( b )
4.             ( a ) *  →  ( L ( a ) ) *
5.             ( ( a ) )  →  L ( a )  
6. Tokens- Sequence of characters that have a collective meaning.
7. · Patterns- There is a set of strings in the input for which the same token
is produced as output. This set of strings is described by a rule called a
pattern associated with the token
8. · Lexeme- A sequence of characters in the source program that is
matched by the pattern for a token

Definitions:
Translator
A device that changes a sentence from one language to
another without change of meaning.
Compiler
A program that translates between programming
languages.
Interpreter
A processor that compiles and executes programming
language statements one by one in an interleaved manner.
Syntax
An alphabet and a set of rules defining spatial relationships
between symbols and symbol sets in a language.
Semantics
The meanings assigned to symbols and symbol sets in a
language.
Pragmatics
The meanings perceived to be associated with symbols
and symbol sets in a language

You might also like