Interpreter Pattern

Interpreter Pattern

An Interpreter Pattern says that “to define a representation of grammar of a given language, along with an interpreter that uses this representation to interpret sentences in the language”.

Basically the Interpreter pattern has limited area where it can be applied. We can discuss the Interpreter pattern only in terms of formal grammars but in this area there are better solutions that is why it is not frequently used.

This pattern can applied for parsing the expressions defined in simple grammars and sometimes in simple rule engines.

Advantage of Interpreter Pattern

  • It is easier to change and extend the grammar.
  • Implementing the grammar is straightforward.

Usage of Interpreter pattern:

It is used:

  • When the grammar of the language is not complicated.
  • When the efficiency is not a priority.

Example of Interpreter Pattern

Let’s understand the example of Interpreter Pattern by the above UML diagram.

UML for Interpreter Pattern:

Interpreter Pattern UML

Implementation of above UML

Step 1

Create a Pattern interface.

public interface Pattern {

    public String conversion(String exp);

}

Step 2

Create a InfixToPostfixPattern class that will allow what kind of pattern you want to convert.

File: InfixToPostfixPattern.java

import java.util.Stack;

public class InfixToPostfixPattern implements Pattern {

@Override  

    public String conversion(String exp) {  

        int priority = 0;// for the priority of operators.  

       String postfix = “”;  

        Stack<Character> s1 = new Stack<Character>();  

       for (int i = 0; i < exp.length(); i++)  

        {  

           char ch = exp.charAt(i);  

           if (ch == ‘+’ || ch == ‘-‘ || ch == ‘*’ || ch == ‘/’||ch==‘%’)  

           {  

              // check the precedence  

              if (s1.size() <= 0)  

                 s1.push(ch);  

           }  

           else  

              {  

                 Character chTop = (Character) s1.peek();  

                 if (chTop == ‘*’ || chTop == ‘/’)  

                    priority = 1;  

                 else  

                    priority = 0;  

                 if (priority == 1)  

                 {  

                    if (ch == ‘*’ || ch == ‘/’||ch==‘%’)  

                    {  

                       postfix += s1.pop();  

                                                  i–;  

                    }  

                    else  

                    { // Same  

                       postfix += s1.pop();  

                       i–;  

                    }  

                 }  

                 else  

                 {  

                    if (ch == ‘+’ || ch == ‘-‘)  

                    {  

                       postfix += s1.pop();  

                       s1.push(ch);  

                    }  

                    else  

                       s1.push(ch);  

                 }  

              }  

           }  

           else  

           {               

              postfix += ch;  

           }  

        }

int len = s1.size();for(

int j = 0;j<len;j++)postfix+=s1.pop();return postfix;

}}// End of the InfixToPostfixPattern class.

Step 3

Create a InterpreterPatternClient class that will use InfixToPostfix Conversion.

File: InterpreterPatternClient.java

public class InterpreterPatternClient {

   public static void main(String[] args) {

     String infix = “a+b*c”;

     InfixToPostfixPattern ip = new InfixToPostfixPattern();

     String postfix = ip.conversion(infix);

     System.out.println(“Infix:   + infix);

     System.out.println(“Postfix: ” + postfix);

  }

}

Output:

Infix:   a+b*c

Postfix: abc*+

 

Popular posts from this blog

Window function in PySpark with Joins example using 2 Dataframes (inner join)

Complex SQL: fetch the users who logged in consecutively 3 or more times (lead perfect example)

Credit Card Data Analysis using PySpark (how to use auto broadcast join after disabling it)