/StringJava

A parser and translator for a language supporting only string types and operations.

Primary LanguageJavaMIT LicenseMIT

StringJava

A parser and translator for a language supporting string operations. The language supports the concatenation operator over strings, function definitions and calls, conditionals (if-else i.e, every "if" must be followed by an "else"), and the following logical expressions:

  • is-prefix-of (string1 prefix string2): Whether string1 is a prefix of string2.
  • is-suffix-of (string1 suffix string2): Whether string1 is a suffix of string2.

All values in the language are strings.

The parser, based on a context-free grammar, will translate the input language into Java. JavaCUP was used for generating the parser, combined with JFlex lexer.

Infer the desired syntax of the input and output languages from the examples below or with the grammar in the parser.cup file. The output language is a subset of Java so it can be compiled using the "javac" command and executed using the "java" command or online Java compilers like this, if you want to test your output.

There is no type checking for the argument types or a check for the number of function arguments. We assume that the program input will always be semantically correct.

Moreover, for each function declaration of the input program, the translated Java program must contain an equivalent static method of the same name. Finaly, keep in mind that in the input language the function declations must precede all statements.

Example #1

Input:

    name()  {
        "John"
    }
    surname() {
        "Doe"
    }
    fullname(first_name, sep, last_name) {
        first_name + sep + last_name
    }
    name()
    surname()
    fullname(name(), " ", surname())

Output (Java):

public class Main {
    public static void main(String[] args) {
        System.out.println(name());
        System.out.println(surname());
        System.out.println(fullname(name(), " ", surname()));
    }
    public static String name() {
        return "John";
    }
    public static String surname() {
        return "Doe";
    }
    public static String fullname(String firstname, String sep, String last_name) {
        return first_name + sep + last_name;
    }
}

Example #2

Input:

    name() {
        "John"
    }
    repeat(x) {
        x + x
    }
    cond_repeat(c, x) {
        if (c prefix "yes")
            if("yes" prefix c)
                repeat(x)
            else
                x
        else
            x
    }
    cond_repeat("yes", name())
    cond_repeat("no", "Jane")

Example #3

Input:

    findLangType(langName) {
        if ("Java" prefix langName)
            if(langName prefix "Java")
                "Static"
            else
                if("script" suffix langName)
                    "Dynamic"
                else
                    "Unknown"
        else
            if ("script" suffix langName)
                "Probably Dynamic"
            else
                "Unknown"
    }
    findLangType("Java")
    findLangType("Javascript")
    findLangType("Typescript")