Syntax for a simple language (iii): Make minor adjustments and change the build target to CSharp2

Source: Internet
Author: User

In order to make the tree grammar in the back more concise, this article makes some adjustments to the last one of the trees rewrite rules and some grammatical details. Also, the generated lexer and parser source targets are swapped to CSHARP2 for later use. NET's library.

To use the CSHARP2 target, you need to download the corresponding Run-time library from the official website. The latest edition of the current version is 3.1.1, which can be obtained from here. Csharp/csharp2 the details of the target, you can consult the official web documents. Above one of the grammar as a basis, to change to the CSHARP2 goal as long as a few embedded in the action of the System.out.println replaced by Console.WriteLine, Tostringtree replaced Tostringtree, It's OK to replace clear with clear. At a minimum, you need to reference Antlr3.Runtime.dll at compile time.

So how do you change the impact of replacing a build target?

First of all, the grammar has made some minor adjustments. For example, the program rules have at least one statement since they were originally allowed to have no statements; Blockstatement wrote a special branch for the empty block; Expressionstatement also added a expr_stmt fictional Token is the root node, and so on.

The biggest change is variabledeclaration and related rules. The rewrite rules of this rule in the previous article do not distinguish between initialization and no initialization, simple types and array types; In this article, the two distinctions are explicitly written in the rewrite rules, with different fictitious token as the root node of the generated tree. In this way, the number of lookahead needed will be reduced to write the tree grammar behind.

The AST generated by ANTLR is traversed in a depth-first way and can be viewed as a one-dimensional stream: each layer of parent-child relationship can be represented as:

Root-> "Down"-> element1-> element2-> ...-> elementn-> "Up"-> ...

where "down" and "up" are fictitious token inserted by ANTLR to specify the level of the tree.

In this way, after using the tree grammar to traverse the AST, it is actually traversing such a one-dimensional stream (Commontreenodestream). So we can also think of the tree grammar as a common parser grammar that implies "down" and "up" fictional token. The number of lookahead required in the tree grammar, then, is the same as that of parser grammar.

Take a look at the example below. The type of the variable declaration that appears in the previous variabledeclaration rewrite rule can be matched with such a tree grammar:

Java code

Type
  : ^ (simple_type int)
  | ^ (simple_type real) | ^ (
  array_type int integer+)
  | ^ (Array_type real in teger+)
  ;

The ^ (...) of the tree grammar. implies "down" and "up" these two fictitious token. In fact, this rule matches:

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.