TIPS: Convert scripts to Java

Source: Internet
Author: User

A former colleague mentioned the idea of converting PHP into Java. // 2009-7-18
I don't know much about PhP. As for "how to convert scripts into Java", the following is my opinion.

Objective: to convert a script into Java (a script generally refers to a lightweight language, such as Ruby or Javascript, without considering its runtime environment ).
Steps:
1. Compile the syntax description of the script (generative set ).

 

2. parse the syntax description into an idiom diagram (the so-called abstract syntax tree). The node types of the tree correspond one to one with the structure components in the source code, such as function, variabledefine, ifelse, for, while, return, parameterlist, codeblock, and expresssion. -- It indicates the syntax structure of the source code.

 

3. Use the abstract syntax tree to match a script instance (A) and generate a matching tree (I call it a matching tree). The Node Type of the matching tree is similar to that of the syntax tree. The matching tree has a strict tree structure-the nodes contained in it correspond to the components in instance a one-to-one.
The specific process is as follows:
3.1 generate a class specifically used to read scripts.
3.2 run this class and pass in script instance (a). A matching tree will be generated after successful running.
3.3 In fact, the generated matching tree needs to be standardized to facilitate subsequent semantic analysis. For example, convert each expression subtree into a binary tree (to facilitate the analysis and evaluation of operator priority ).

 

4. Next we will build semantic nodes for each type of nodes. A semantic node instance is responsible for semantic analysis or translation of the source code components by matching the node of the corresponding tree.

Note: syntax graphs and matching trees are both associated and different in concept.

Some experiences show that 1 ~ The three steps consume most of the time, debugging also consumes most of the time. If you use javacc (or use my javacce), most of the time is saved.
These problems may occur during the above process:
1. Java does not support dynamic addition and deletion of functions or attributes.
2. The script has built-in objects or native methods (if any) and their functions in Java. Because built-in objects and native methods do not have source code, they cannot be translated and can only be imitated. This requires a lot of work.
3. script may have its own regular language. It may be difficult to convert it into a Java regular language (see java. util. RegEx. pattern.
4. Evaluate the expression. An expression can contain almost all syntax elements (such as class ).
5 .......
Semantic Analysis of languages with a rigorous syntax structure does not seem theoretically difficult.
In turn, to convert Java into a script, the workload will be greatly increased, because Java is a "heavyweight language ".

End.

 

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.