Question

In: Computer Science

Write the RE for identifiers that can consist of any sequence of letters (l) or digit...

Write the RE for identifiers that can consist of any sequence of letters (l) or digit (d) or "_" but the first char must be a letter and the last char cannot be a "_"

Solutions

Expert Solution

So, as given in the question, we need to write a Regular Expression (RE), which consists of any sequence of letters (we represent letters as 'l'), or any sequence of digits (we represent digits as 'd'), or the particular symbol '_'. Here a condition is given that is, the first char or symbol must be a letter, which means must be 'l', and not any digit 'd' or symbol '_'. Also, the last char or symbol can not be the symbol '_'.

So, what can our language be? Let us first see the language with examples -

Language (L) can be {l, ld, ll, l_d, l_l, lll, ldd, l_d_d, ...} (can go infinitely)

(Very Important) Here it is stated that the start symbol or char will be the letter 'l', and the last char can not be '_', so last char can be 'l', and in between, you may have any sequence of letters means you may have a sequence of 0 length too, so only 'l' will be there in the language. A word from our language set L, 'ld', start with 'l', end with 'd', which is valid, and in 'l_d', it starts with 'l', the middle is a single sequence of the symbol '_' and then end in digit 'd'. With this, we are creating the language but 'd' can not be in language, because it does not start with 'l'.

Also, I will tell you a little bit about Kleen star (represented as '*'), which means making the set of all possible strings from a finite length of alphabet or symbol given. You can also rephrase it as you can construct strings of any length or sequence from a given set of symbols (which is here {'l', 'd', '_'}). It can also be a null string or strings with a length of 0, which we call as epsilon (represented as ).

Ex - a* = {, a, aa, aaa, aaaa, ....} (where 'a' is our symbol)

Suppose we have some symbol set {a, b}, and we have RE as (a + b), then the language set contains {a, b}, means EITHER 'a' OR 'b', & if we have RE as (a . b), then the language set contains {ab}, means 'a' AND 'b'. Just like logic gates, + is OR and . (dot) is represented as AND.

Now that we know what our language is, and we have a good idea about Kleen star, & other RE operators, so let us make the RE for the abovementioned language. So, our RE will be =

l . (l + d + _)* . (l + d)

or

letters . (letters + digits + _)* . (letters + digits)

Although I have told you how to write it indirectly, let me explain to you again, what this exactly means. So, here in our RE, the language starts with 'l', then we have 'l . (l + d + _)*', and it means l AND (l OR d OR _)*, where

(l + d + _)* = {, l ,d, _, ld, dl, l_, d_, _d, _l, ll, dd, ___, lll, ___, ddd, ldl, l_d, d_l, ... } (as I told, any possible sequence of string starting from length 0 to infinity). So, 'l . (l + d + _)*' = {l, ll, ld, l_, l_l, l_d, ld_, ...}, but this can end in '_', so this can not be the end of our RE.

In (l + d), we can have either 'l' or 'd', but can not have both, so by this we can not have the last char as '_'.

Hope you got it now. Thank you.


Related Solutions

Write the RE for identifiers that can consist of any sequence of letters (l) or digit...
Write the RE for identifiers that can consist of any sequence of letters (l) or digit (d) or "_" but the first char must be a letter and the last char cannot be a "_" (10)
Describe the rules governing identifiers. Can identifiers be re-used in the same Java class file? Provide...
Describe the rules governing identifiers. Can identifiers be re-used in the same Java class file? Provide three examples of different kinds of invalid identifiers. Java
1An identification code is to consist of 2 letters followed by 5 digit. Determine the fallowing?...
1An identification code is to consist of 2 letters followed by 5 digit. Determine the fallowing? 1a) how many different codes are posible if repetition is permitted? 1b) how many different codes are possible if repetition is not permitted, but the first 2 letters must ve the same letter? 1c) how many different codes are possible if the first letter must be, P, Q, R, S, T, and repetition for everything else is not permitted?
Write a regular expression that can represent any digit, without more than one digit or non...
Write a regular expression that can represent any digit, without more than one digit or non digit characters.
write a program that uses exactly four for loops to print the sequence of letters below...
write a program that uses exactly four for loops to print the sequence of letters below A A A A A A A A A A B B B B B B B C C C C C C E E E E E
How many: New York license plates can be made that consist of 3 letters followed by...
How many: New York license plates can be made that consist of 3 letters followed by 4 numbers given the restriction that if the first letter is an ‘N’, then the second letter must be a ‘Y’? New York license plates can be made that consist of 3 letters followed by 4 numbers given the restrictions that (1) if the first letter is an ‘N’, then the second letter must be a ‘Y’ and (2) if the second letter is...
Write a function flipSwitches that accepts one argument, a sequence of upper or lower case letters...
Write a function flipSwitches that accepts one argument, a sequence of upper or lower case letters (the sequence can either be a str or a list, if you write your code correctly, it shouldn’t matter). This is a sequence of switches, uppercase means to turn a switch on, and lowercase to turn a switch off.   For example, ‘A’ means to turn the switch ‘A’ on, and ‘a’ means to turn the switch ‘A’ off.   (Turning an on switch on again,...
The user enters some text into a textbox. It can contain any characters (letters, digits, spaces,...
The user enters some text into a textbox. It can contain any characters (letters, digits, spaces, punctuation marks, and there is no length limit). When a button Count is hit , display the number of upper-case letters in the inputted phrase. This must be done in visual studios using C#.
Exercise 1: You are required to re-write the following code to catch the exception that can...
Exercise 1: You are required to re-write the following code to catch the exception that can occur. import java.util.Scanner; public class ExceptionDemo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer: "); int number = scanner.nextInt(); // Display the result System.out.println( "The number entered is " + number);           } }
Q4. The sentences below may or may not contain errors, Re-write them, correcting any errors you...
Q4. The sentences below may or may not contain errors, Re-write them, correcting any errors you find to make them acceptable without changing their meanings. i. The invited guest for the programme is still not in; it’s already 2 o’clock pm. ii. All what they needed to do was to apologize to the victims. iii. Many people are happy about what president said in his speech that, “…, but what we don’t know is to bring people back”. iv. We...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT