How to build a translator for a small language?

I am trying to figure out where to start this project. Perhaps someone can point me in the right direction.

I am given a small language in which I have to write an interpreter. The language consists of either an expression in parentheses:

(integer integer operator)

      

or an arithmetic IF statement, consisting of expressions of the form:

IF exp1 exp2 exp3 exp4

      

where exp2 is returned if exp1 is negative, exp3 is returned if exp1 is zero, and exp4 is returned if exp1 is positive.

The operator is either + or x (for addition and multiplication, respectively).

I need to execute a scanner / parser together and then an interpreter that will output the result. The interpreter part is not difficult, but I'm having a hard time figuring out how to start the scan / parsing process.

I started out using Java and had a Scanner object that collects input and stores it in a string. Then I split the String into a String array using nothing as a delimiter (so that every single character, character, space, etc., is stored at its own string index). This might not be the best way to do it as I can't figure out where to go from here. The part I can't figure out is how to return errors if this syntax is not respected, or how to detect parentheses and / or IFs, etc.

Here is the code snippet I described in the last paragraph:

public void run() {
    Scanner sc = new Scanner(System.in);

    while (sc.hasNext()) {
        String sLine = sc.nextLine();
        String[] scanned = sLine.split("");

      

Input examples:

(7 2 +)

Output: 9

IF (2 -2 +) (5 2 +) (5 -2 x) (5 2 x)

Output: -10

      

If anyone has a good direction for me please share. :)

+3


source to share


4 answers


I think using ANTLR, JavaCC, SampleCC, or other parser generator tools will use a sledgehammer to crack the nut. if there is no recursion in the grammar definition, only a few methods are sufficient. the following code gives a basic idea (it can't compile or work, I wrote it from scratch to illustrate how to get started):



public int parse(String input) {
Scanner scanner = new Scanner(input);

    return consumeLine(scanner);
}

public int consumeLine(Scanner scanner) {
    if( scanner.hasNext("(") ) {
        return consumeExpression(scanner);

    } else if( scanner.hasNext("IF") ) {
        return consumeIf(scanner);
    }
}


public int consumeExpression(Scanner scanner) {
    scanner.next("(");
    int a = scanner.nextInt();
    int b = scanner.nextInt();
    String op = scanner.next("[+-/*]");
    scanner.next(")");

    if( "+".equals(op) ) {
        return a + b;

    } else if( "-".equals(op) ) {
        return a - b;
    } ...

    throw new RuntimeException("parsing error");
}

public int consumeIf(Scanner scanner) {
    scanner.next("IF");
    int exp1 = consumeExpression(scanner);
    int exp2 = consumeExpression(scanner);
    int exp3 = consumeExpression(scanner);
    int exp4 = consumeExpression(scanner);

    if( exp1 < 0 ) {
        return exp2;
    } else if( exp1 == 0 ) {
        return exp3;
    } ...

    throw new RuntimeException("should not be here (TM)");
}

      

+2


source


You can use stack-based algorithms to handle postfix expressions. A simple idea would be to push integers on the stack and when you encounter an operator pop the integers off the stack and perform the operation mentioned by the operator as +, -.



+3


source


Easy with javacc if you're into java. You can mention your tokens and what to do with them in a compact and easy way, and then when it is compiled it generates all the code in java source needed to execute the logic.

javacc intro

javacc faq

+1


source


I recommend you use C ++ and the boost boost library ....

0


source







All Articles