5 Corresponds to prioritizing parentheses

Introduction

In a previous article, I added println to the interpreter (http://qiita.com/quwahara/items/82067b00cbe1cb974e4a). In the process, I added a function to parse ( and ) to Lexer. In addition, let's also support prioritization of operations using parentheses.

What you want to do with prioritized parentheses

I don't think there is any need for explanation, but if you have a program like the one below, we aim for the value of the calculation result ʻa to be 35`.

a = (3 + 4) * 5

How to implement

Implementation is done to parser. To think about where to implement parsing, observe and think about the current parsing method.

Take the formula without parentheses below as an observation target. When parsing an expression without parentheses, look at the order of the 1 tokens and which method handles them.

a = 1 + 2

The 1 token is the third in the token sequence and is processed by Parser'slead ()method.

Next, compare the expression below with the parentheses you want to correspond to the expression without the parentheses. The third token in the parenthesized expression is (.

a = (3 + 4)

From the previous observation, the ( token is in the same position as the 1 token, so You can expect the ( token to appear inside the lead () method. And since ( 3 + 4after thetoken is a normal expression, It can be parsed with the ʻexpression ()method. And if you make sure that there is a closing brace)` at the end, it seems that you can parse it.

Try to implement in Java

Move on to implementation.

Parser.java

An implementation of Parser.java. Added processing for prioritizing parentheses to the part of <-Add. First, check that the ( token appears in the lead () method. Hold the analysis result of the expression in parentheses with Token expr = expression (0). Then use consume (") ") to check for the closing brace. The return value is the retained analysis result. That's all for the correspondence.

Parser.java


    private Token lead(Token token) throws Exception {
        if (factorKinds.contains(token.kind)) {
            return token;
        } else if(token.kind.equals("paren") && token.value.equals("(")) {  // <-- Add
            Token expr = expression(0);
            consume(")");
            return expr;
        } else {
            throw new Exception("The token cannot place there.");
        }
    }

Interpreter.java

An implementation of Interpreter.java. Does the correspondence of the prioritizing brackets reflect? Changed String text =" a = (3 + 4) * 5 "; to an expression with parentheses. You should see 35 on the standard output.

Interpreter.java


    public static void main(String[] args) throws Exception {
        String text = "a = (3 + 4) * 5";    // <-- Update
        text += "println(a)";
        List<Token> tokens = new Lexer().init(text).tokenize();
        List<Token> blk = new Parser().init(tokens).block();
        new Interpreter().init(blk).run();
        // --> 35
    }

That's all for the implementation. Thank you.

in conclusion

The full source is available here.

Calc https://github.com/quwahara/Calc/tree/article-5-parenthesis/Calc/src/main/java

There is a continuation article.

** Supports unary operations ** http://qiita.com/quwahara/items/4069d47b511e4d11f44b

Recommended Posts

5 Corresponds to prioritizing parentheses
Corresponds to 17 arrays
Corresponds to Scope
Corresponds to 15 strings
8 Corresponds to multiple arguments
10 Corresponds to if statement
14 Corresponds to function expressions
19 Corresponds to object creation
16 Corresponds to method invocation
9 Corresponds to the return value
18 Corresponds to JSON-like object definitions
20 Corresponds to static method invocation
11 Corresponds to comparison and logical operators
to_ ○