Implementation of custom language-interpreter mode (4)

Source: Internet
Author: User

18.4 complete solution
To be able to interpret robot control commands, Sunny software developers use the interpreter mode to design and implement robot control programs. Five grammar rules are provided for implementation. The Terminator expressions direction, action, and distance correspond to the direnode node class, ActionNode class, And DistanceNode class, the non-terminator expression and composite correspond to the SentenceNode class and the AndNode class.
We can use the abstract syntax tree to express the specific interpretation process, for example, the abstract syntax tree 18-4 corresponding to the robot control command "downrun 10 and left move20:
,

Figure 18-4 example of the abstract syntax tree of the Robot Control Program
The basic structure of the robot control program instance is shown in 18-5:

Figure 18-5 robot control program structure
In Figure 18-5, AbstractNode acts as the abstract expression, DirectionNode, ActionNode, and DistanceNode acts as the terminator expression, and AndNode and SentenceNode act as the non-terminator expression. The complete code is as follows:
[Java] view plaincopy
// Note: This example simulates the output results of Robot Control commands and translates English commands into Chinese commands. Actually, different control programs are called for Robot Control, including control of the movement direction, mode, and distance
Import java. util .*;
 
// Abstract Expression
Abstract class AbstractNode {
Public abstract String interpret ();
}
 
// And explanation: Non-terminator expression
Class AndNode extends actnode {
Private AbstractNode left; // The left expression of And
Private AbstractNode right; // the right expression of And
 
Public AndNode (AbstractNode left, AbstractNode right ){
This. left = left;
This. right = right;
}

// And expression interpretation operation
Public String interpret (){
Return left. interpret () + "then" + right. interpret ();
}
}
 
// Simple sentence explanation: Non-terminator expression
Class SentenceNode extends AbstractNode {
Private AbstractNode direction;
Private AbstractNode action;
Private AbstractNode distance;
 
Public SentenceNode (AbstractNode direction, AbstractNode action, AbstractNode distance ){
This. direction = direction;
This. action = action;
This. distance = distance;
}

// Explain a simple sentence
Public String interpret (){
Return direction. interpret () + action. interpret () + distance. interpret ();
}
}
 
// Direction Description: Terminator expression
Class DirectionNode extends actnode {
Private String direction;

Public DirectionNode (String direction ){
This. direction = direction;
}

// Explain the operation of the direction expression
Public String interpret (){
If (direction. equalsIgnoreCase ("up ")){
Return "up ";
}
Else if (direction. equalsIgnoreCase ("down ")){
Return "down ";
}
Else if (direction. equalsIgnoreCase ("left ")){
Return "to left ";
}
Else if (direction. equalsIgnoreCase ("right ")){
Return "to the right ";
}
Else {
Return "invalid command ";
}
}
}
 
// Action Description: Terminator expression
Class ActionNode extends actnode {
Private String action;

Public ActionNode (String action ){
This. action = action;
}

// Action (movement mode) expression interpretation operation
Public String interpret (){
If (action. Revoke signorecase ("move ")){
Return "move ";
}
Else if (action. inclusignorecase ("run ")){
Return "fast moving ";
}
Else {
Return "invalid command ";
}
}
}
 
// Distance Description: Terminator expression
Class DistanceNode extends AbstractNode {
Private String distance;

Public DistanceNode (String distance ){
This. distance = distance;
}

// Explain the distance expression
Public String interpret (){
Return this. distance;
}
}
 
// Instruction processing class: tool class
Class InstructionHandler {
Private String instruction;
Private AbstractNode node;

Public void handle (String instruction ){
AbstractNode left = null, right = null;
AbstractNode direction = null, action = null, distance = null;
Stack stack = new Stack (); // declare a Stack object to store the abstract syntax tree
String [] words = instruction. split (""); // command String separated by Spaces
For (int I = 0; I <words. length; I ++ ){
// This instance uses the stack method to process commands. If "and" is encountered ", the last three words are used as the three Terminator expressions to form a simple sentence SentenceNode as the right expression of "and, the expression popped up from the top of the stack is used as the left expression of "and", and the new "and" expression is pushed into the stack. If (words [I]. equalsIgnoreCase ("and ")){
Left = (AbstractNode) stack. pop (); // The stack top expression is displayed as the left expression.
String word1 = words [++ I];
Direction = new direnode node (word1 );
String word2 = words [++ I];
Action = new ActionNode (word2 );
String word3 = words [++ I];
Distance = new DistanceNode (word3 );
Right = new SentenceNode (direction, action, distance); // right expression
Stack. push (new AndNode (left, right); // press the new expression into the stack.
}
// If it is explained from the beginning, the first three words are formed into a simple sentence SentenceNode and the sentence is pushed into the stack.
Else {
String word1 = words [I];
Direction = new direnode node (word1 );
String word2 = words [++ I];
Action = new ActionNode (word2 );
String word3 = words [++ I];
Distance = new DistanceNode (word3 );
Left = new SentenceNode (direction, action, distance );
Stack. push (left); // press the new expression into the stack.
}
}
This. node = (AbstractNode) stack. pop (); // pops up all expressions from the stack.
}

Public String output (){
String result = node. interpret (); // interpreted expression
Return result;
}
}
The tool class InstructionHandler is used to process input commands. It divides input commands into string arrays, combines 1st, 2nd, and 3rd words into one sentence, and stores them in the stack; if the word "and" is found, the 1st, 2nd, and 3rd words after "and" are combined into a new sentence as the right expression of "and, the original sentences are extracted from the stack as the left expression, And then combined into an And node into the stack. Wait until the entire instruction parsing ends.
Write the following client test code:
[Java]
Class Client {
Public static void main (String args []) {
String instruction = "up move 5 and down run 10 and left move 5 ";
InstructionHandler handler = new InstructionHandler ();
Handler. handle (instruction );
String outString;
OutString = handler. output ();
System. out. println (outString );
}
}
Compile and run the program. The output result is as follows:
Move up 5, move down quickly 10, move to left 5


Author: Liu Wei

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.