0

I'm trying to translate back the sentence "b((aa)a)b$" using the grammar shown in code comment. When I try to run it, it gives the following error. It seems to be an error with the string split method, but I don't know how to fix it. Any suggestions? Thanks.

run:

b((a)a)b$

Exception in thread "main" java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 3

a)

^ at java.util.regex.Pattern.error(Pattern.java:1955) at java.util.regex.Pattern.compile(Pattern.java:1700) at java.util.regex.Pattern.(Pattern.java:1351) at java.util.regex.Pattern.compile(Pattern.java:1028) at java.lang.String.split(String.java:2367) at exercise3.GrammarTest.parser(GrammarTest.java:28) at exercise3.GrammarTest.parser(GrammarTest.java:48) at exercise3.GrammarTest.main(GrammarTest.java:62)

Java Result: 1

BUILD SUCCESSFUL (total time: 1 second)

//grammar
//<P> → <S>$ (P is the start symbol)
//<S> → b<M>b
//<M> → (<L>
//<M> → a
//<L> → <M> a)


public static String parser(String original, String sym, String rep){
    if( !(original.contains(sym))){
        return original;
    }

    String str = "";
    String [] parts = original.split(sym, 2);

    str = parts[0] + rep + parts[1];

    System.out.println(str);
    return str;
}           

public static String parser(String str){
    String [] sym = new String[5];
    //recursive                //b((aa)a)b$
    str = parser(str, "a", "<M>"); //b((<M>a)a)b$
    str = parser(str, "<M>a)", "<L>"); //b((<L>a)b$
    str = parser(str, "(<L>", "<M>"); //b(<M>a)b$
    str = parser(str, "<M>a)", "<L>"); //b(<L>b$
    str = parser(str, "(<L>", "<M>"); //b<M>b$
    str = parser(str, "b<M>b", "<S>"); //<S>$
    str = parser(str, "<S>$", "<P>"); //<P>

    System.out.println(str);

    return str;
}

public static void main(String[] args) {
    String str = " b((aa)a)b$ ";
    str = parser(str);       

}
0

3 Answers 3

4

When you want to match for (, you have to escape it as \\(, because ( is a start for a group for regular expressions. For lazy people, Java includes a facade for this: Pattern.quote("(a"); /* gives you \\(a */

Sign up to request clarification or add additional context in comments.

Comments

1

The String .split method takes in a regex, not a plain string. You have brackets in your input, so it looks for a matching bracket to indicate a captured group.

Unfortunately, .contains does not take a regex, so using the same strings for both of these methods will be quite difficult. It would probably be better to simply split on the regex, and check if there is only one result. If so, there was no match.

Comments

1

You need to escape the parentheses in the pattern

str = parser(str, "<M>a\\)", "<L>"); //b((<L>a)b$
str = parser(str, "\\(<L>", "<M>"); //b(<M>a)b$
str = parser(str, "<M>a\\)", "<L>"); //b(<L>b$
str = parser(str, "\\(<L>", "<M>"); //b<M>b$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.