Implementation of custom language-interpreter mode (1)

Source: Internet
Author: User

A friend has been waiting for my interpreter mode document. Now I am posting a version on my blog. You are welcome to discuss it!

Although there are hundreds of computer programming languages, sometimes we still want to use some simple languages to perform some specific operations. We only need to input a sentence or file to the computer, it can interpret sentences or files according to pre-defined grammar rules to implement corresponding functions. For example, a simple addition/subtraction interpreter is provided. If you enter an addition/subtraction expression, it can calculate the expression result, as shown in 18-1. When the input string expression is "1 ",
+ 2 + 3-4 + 1 ", the calculation result is 3.

Figure 18-1
Addition/subtraction Interpreter

As we know, languages such as C ++, Java, and C # cannot be directly interpreted as "1 +
For strings like 2 + 3-4 + 1 (which can be interpreted directly as numerical expressions), we must define a set of grammar rules to explain these statements, design a custom language. In actual development, these simple custom languages can be designed based on existing programming languages, in this case, you can use the interpreter mode to implement a custom language.

18.1 Robot Control Program

Sunny software wants to develop a robot control program for a toy company. The robot control program contains some simple English control commands, each of which corresponds to an expression ), this expression can be a simple expression or a compound expression. Each simple expression consists of three parts: moving direction, moving mode, and moving distance, the moving directions include Up, down, left, and right. The moving methods include move and run ); the moving distance is a positive integer. The two expressions can be connected with (and) to form a composite expression.

You can create a robot control command by performing operations on the graphic setting interface. After receiving the command, the robot moves according to the instruction settings. For example, enter the control command: Up move 5, then, "Move up five units", and enter the control command: Down
Run 10 and left move 20, then "Move 10 units down quickly and then move 20 units to the left ".

Developers of Sunny software company decided to customize a simple language to explain robot control commands. According to the above requirement description, the syntax rules for expressing the simple language in a formal language are as follows:

Expression: = direction action distance | composite // expression

Composite: = expression' and 'Expression // composite expression

Direction: = 'up' | 'down' | 'left' | 'right' // move the direction

Action: = 'move '| 'run' // move mode

Distance: = an integer // moving distance

The preceding language defines five grammar rules, corresponding to five language units. These language units can be divided into two types: terminator (also called Terminator expressions), such as direction, action, and distance, they are the smallest component units of the language and cannot be split. The other class is non-terminator (also called non-terminator expressions), such as expression and composite. They are all complete sentences, contains a series of terminologies or non-terminologies.

The language defined according to the above rules can constitute many statements, and computer programs will perform some operation according to these statements. To explain the statements, you can use the interpreter mode. In the interpreter mode, each grammar rule corresponds to a class, it is very convenient to expand, change grammar, and add new grammar rules. Next let's start learning the interpreter mode, let's see how to process robot control commands in interpreter mode.

[Author: Liu Wei http://blog.csdn.net/lovelion]

 

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.