Question

In: Computer Science

Consider the language defined as such: L = { w$w’ : w is a string of...

  1. Consider the language defined as such:

L = { w$w’ : w is a string of numbers 0-9 and can be of length ≥ 0, and w’ is the reverse string of w}

Write a recursive grammar that defines all strings in the language.

  1. Using the language above, write the Java code (using recursion) that will recognize if a string is part of the language or not.

Solutions

Expert Solution

(a)Consider the given Language

L = { w$w’ : w is a string of numbers 0-9 and can be of length ≥ 0, and w’ is the reverse string of w}

L={$,0$0,1$1,2$2,01$10,00$00,.....}

The recursive grammar that generate L is G=(V, T, P, S) which are defined as :

V={S}

T={0,1,2,3,4,5,6,7,8,9}

S-is the start Symbol

P={S→0S0/1S1/2S2/3S3/4S4/5S5/6S6/7S7/8S8/9S9/$}

i.e We recursively generate numbers(0-9) in such a way that we can each number start and end with the same digit until we place a $ at the center of the string.

(b)Using the language above, write the Java code (using recursion) that will recognize if a string is part of the language or not.

1.CODE SCREENSHOT :

2.OUTPUT :

3.CODE :

import java.util.Scanner;
class StringRecognizer
{
    
    public static boolean isValid(String s)
    {   // if length is 1 and the input is '$' then we accept
                if(s.length() == 1 &&s.charAt(0)=='$')
            return true; 
                // if length is 1 and the input is Not '$' then we reject
                // if length is 2 then we rejct since L={$,0$0,1$1,...}
                // The string lenght must be odd
                if(s.length() == 1 &&s.charAt(0)!='$'||s.length() == 2)
            return false; 
                /*
                *If we read digits at both end of the input ,then we compare 
                *and check weither the two characters are equal,if equal then
                *we search the remaining substring 
                */
        if(Character.isDigit(s.charAt(0))&&Character.isDigit(s.charAt(s.length()-1)))
                if(s.charAt(0) == s.charAt(s.length()-1))
               return isValid(s.substring(1, s.length()-1));
                //if the input is non  numeric then we return false 
        return false;
    }

    public static void main(String[]args)
    {
        //For capturing user input
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the String for check:");
        String string = scanner.nextLine();
        /* If function returns true then the string is
         * in the language else not
         */
        if(isValid(string))
            System.out.println(string + " is in the given Language");
        else
            System.out.println(string + " is not in the given Language");
    }
}

Related Solutions

For a given language L = { w | na(w) + nb(w) = nc(w) } where...
For a given language L = { w | na(w) + nb(w) = nc(w) } where S = G = {a, b, c} Looking for answer to 3 Construct a PDA M that accepts L with S = G = {a, b, c} Show the sequence of instantaneous descriptions for the acceptance of acacbcbc by M in 1). Give a CFG G that generates L, L(G) = L.
In C++ Consider the language L = { s$s' : s is a possibly empty string...
In C++ Consider the language L = { s$s' : s is a possibly empty string of characters other than $ , s' = reverse( s )} as defi ned in Chapter 6 . Write a recognition algorithm for this language that uses both a queue and a stack. Thus, as you traverse the input string, you insert each character of s into a queue and each character of s' into a stack. Assume that each input string contains exactly...
Let L ⊆ Σ ∗ and define S(L), the suffix-language of L, as S(L) = {w...
Let L ⊆ Σ ∗ and define S(L), the suffix-language of L, as S(L) = {w ∈ Σ ∗ | x = yw for some x ∈ L, y ∈ Σ ∗ } Show that if L is regular, then S(L) is also regular.
1. Design a FA for the language L = {w : na(w) ≤ 4}. 2. Design...
1. Design a FA for the language L = {w : na(w) ≤ 4}. 2. Design a FA that accepts all strings that contain the substring aba. 3. Design a FA for the language L = {ai baj : i, j ≥ 0, i + j is odd} 4. Design a FA for the language L = {bnam : n ≤ 2, m ≥ 3}
Draw an NFA for the following language then convert to a DFA: L = {w :...
Draw an NFA for the following language then convert to a DFA: L = {w : |w| is odd or |w| is a multiple of 4} where Σ = {0, 1}
Question II: Consider the language L below. If L is a regular language, construct the corresponding...
Question II: Consider the language L below. If L is a regular language, construct the corresponding DFA. If not, prove that L is not a regular language. L= {0n10n | n ≥1} Consider the language M consisting of those strings of 0’s and 1’s that have an equal number of 0’s and 1’s (not in any particular order). Suppose we know M is not a regular language. Consider the language N consisting of those strings of 0’s and 1’s that...
- Give state diagram of DFA recognizing L={w | w is 0,1-string, and contains 3k 0s...
- Give state diagram of DFA recognizing L={w | w is 0,1-string, and contains 3k 0s and 2m 1s for some integers k and m at least 0}. For examples, 0010111 is in L, but 010011 is not in L. The first string contains 3 0s and 4=2x2 1s, but the second string has an odd number of 1s.
Given the language L={w| the number of a’s is greater than or equal to the number...
Given the language L={w| the number of a’s is greater than or equal to the number of b’s in w} a) Using the Pumping Lemma to prove L is not a regular language. b) Using closure property to prove L is not a regular language.
Describe a TM (Turing Machine) which accepts the language L = {w2w | w is a...
Describe a TM (Turing Machine) which accepts the language L = {w2w | w is a string in {0, 1}* }. Here I mean informally describe how the TM works. You need not give the full program or diagram.
Consider an elastic string of length L whose ends are held fixed. The string is set...
Consider an elastic string of length L whose ends are held fixed. The string is set in motion with no initial velocity from an initial position u(x, 0) = f(x). Let L = 10 and a = 1 in parts (b) and (c). (A computer algebra system is recommended.) f(x) = 16x L ,      0 ≤ x ≤ L 4 , 4,      L 4 < x < 3L 4 , 16(L − x) L ,      3L 4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT