Design a language in your own program with XML

xiaoxiao2021-03-06  47

Design a language in your own program with XML

Long-term use of the program language to design someone to write the program, have you thought of thinking about itself to design a language? Perhaps because of some of some languages ​​in some languages, I want to transform, perhaps because I want to give yourself an interesting challenge. Regardless of the purpose, I believe there are many people who have this idea, but there are not many people who actually do, because the first few levels of levels that must be designed for a new language, is actually uncommon obstacles. Tools such as YECC (YET ANOTHER Compiler) can be used to help design new program languages, but I found that XML can be another option, or even better choices than Yacc, because you can get started quickly. After all, XML is a general tool, which is very wide. It is likely that your company has used XML to plan, and you also have a certain degree of understanding of XML. This makes you don't need to spend too much time to learn XML, you can use XML to make your program language.

Some people may doubt my statement, and think that the program language is very strange, long-lasting experience in XML applications, so that we can confirm that it is a file, it is a file. Not the program code. Don't let this first-in-one concept blindly, the use of XML, XML is very wide, the set of advertisements "The only limited IS YOUR IMAGINATION (your imagination is the only restriction)". I think that use XML to be used as Meta Language in the program language, not only, even very suitable.

There is a demarcation requirement between XML and programming languages. The following Java program is an example:

Public class test {

Public static void main (String [] args) {

For (int x = 0; x <10; x ) {

// do something

}

}

}

If you switch to XML, you can follow:

String [] args

 int x = 0 

X & lt10

x

You will definitely think this is more messy, the original code is longer ....... In fact, there are now many commercial or free XML editing software to easily overcome this, not only easily edit, but also produce different looks with different XSLs. You can also use DTD to use these powerful XML editing software, then the error that appears in the program will be automatically picked up, equal to helping you do a grammar check. Without your own design editing software, your programming can have professional editing software available. Of course, using XML to design the program language, it is definitely not only for the above editing. When you design a program code parser (Parser), if your program is an XML format, you can write a Parser in less than ten minutes, and others need to spend more than ten days. DOM and SAX are very popular XML Parser APIs, no matter from IBM's XML4J or Sun's JAXP (of course, there are many other companies' products), they all support DOM and SAX, you only have information to feed these APIs, Parse Tree "is" inexplicably "produced. I estimate that the use of the DOM / SAX API is less than ten lines, you can analyze the XML file, you won't think about it takes ten minutes to write this ten lines!

The benefits of the above editing and analysis are nothing, a bigger advantage is that XML allows you to have a compiler without writing, as long as you will write the template you need to write into XSLT's format. There are now many commercial or free XSL programs that allow us to easily convert an XML file to another. The compiled result is another XML format (equal to assembly language), although not Target Code, but there is a direct correspondence between Target Code. You only need to write a small program (group transpiling) to turn it into target code. Datual!

So, as long as you are ready:

1. The original language DTD (the definition of the original language) 2. The DTD of the assembly language (the definition of the assembly language) 3. XSLT4 of the original language transfers language. Group translation

You have an actual programming language. The above two DTDs and an XSLT are XML file files. Only the group transpiler is really written in Java, but this program is usually very short, I estimate that only more than one hundred or hundreds of line program code is almost, if you define the DTD of the assembly language is very good, even Will not be one hundred lines. Basically this group transpiler is just Tree Traversal, only a few Method, you can go back (Recursive), and the program code is of course short.

Does this article cause you a language interest? If so, I will give you a few suggestions before you start planning your program language:

The speed of compilation is usually relatively slow (although you don't feel able to feel), after all, you use a general XSLT program instead of developing a compiler.

If you want to design something like a look, don't want to get too complicated, you can take the Java language as template, then remove some features, define a java language subset, I see this language is called Javlet (small Java is ok.

If you want to design a language, you want to make a gorgeous language, you can take the Java language as template, plus some features, define a java language SuperSet, I see this language is called JavadiaAlact (Java dialect) is fine. I have to make good duty: this XSLT file may not design, after all, it is equal to the compiler: (

You must choose a platform for Target Code. I don't think x86 is a good choice, because the implementation architecture is too complex, although PPC may be better than X86, but it is still very complicated. I suggest your Target platform to use Java Virtual Machine (Java virtual machine) because Java Bytecode architecture is simple, and your programming can continue to use all APIs in Java. Java Bytecode is a tree architecture, and the group transpiler is very good. If you need a Java virtual machine knowledge, you can refer to my "Java Virtual Machine" book (published by Ore).

转载请注明原文地址:https://www.9cbs.com/read-115986.html

New Post(0)