Question 4
Write a regular expression that generates each of the following language constructs:
(1) String constants with the following specifications:
A string constant consists of any sequence of characters enclosed by the quotation marks: “ and ”
The sequence may be empty.
The sequence cannot span multiple lines.
Don’t worry about escape characters (assume that they won’t appear in the input).
(2) Multiple-line comment in C, C++ and JAVA with the following specifications:
The comment consists of any sequence of characters enclosed by the delimiters /* and */.
The sequence may be empty.
The sequence cannot have the * character.
Don’t worry about escape characters (assume that they won’t appear in the input).
(3) An identifierwith the following specifications:
The identifier consists of alphabetic characters [a-Z], numbers [0-9] and underscores.
The first symbol must be an alphabetic character.
The last symbol must be an alphabetic character or an underscore.
The identifier must have at least one number.
1.
Answer:
Let Σ denotes set of all valid alphabets in the language then Σ*
denote set of string all possible string over Σ,
then the regular expression for String constant consists of any sequence of characters enclosed by
the quotation marks: " and " is given by ="Σ*" (the
required regular expression)
2.
Answer:
Let Σ denotes set of all valid alphabets in the language, Since *
can't in the string in comments
we construct Σ1=Σ*-{*}then
The comment consisting of any sequence of characters is :/*{Σ1}**/ (the required regular expression)
3.
Answer:
let alpabets=[a-Z]={a,b,c....X,Y,Z}
numbers=[0-9] ={0,1,2,...9}
identifier=alpabets(alpabets + numbers +
{_})*numbers(alpabets + numbers +
{_})*(alpabets + {_})
Get Answers For Free
Most questions answered within 1 hours.