Format description file syntax parsing 3.JavaCC

  JavaCC syntax description file format is as follows:

options {
    JavaCC的选项
}

PARSER_BEGIN(解析器类名)
package 包名;
import 库名;

public class 解析器类名 {
    任意的Java代码
}
PARSER_END(解析器类名)

扫描器的描述

解析器的描述

  JavaCC and java parser contents as defined in a single class , and therefore this description of the related classes and between PARSER_BEGIN PARSER_END.

Take the following section of code to do the actual sample, splitting and parsing code in sections.

1. Sample Code

options {
    STATIC = false;
}

PARSER_BEGIN(Adder)
package com.susu.testJavaCC;
import java.io.*;
public class Adder {
    public static void main(String[] args) {
        for (String arg : args) {
            try {
                System.out.println(evaluate(arg));
//                return(evaluate(arg));
            } catch (ParseException ex) {
                System.err.println(ex.getMessage());
            }
        }
    }

    public static long evaluate(String src) throws ParseException {
        Reader reader = new StringReader(src);
        return new Adder(reader).expr();
    }
}
PARSER_END(Adder)

SKIP: { <[" ", "\t", "\r", "\n"]> }
TOKEN: {
    <INTEGER: (["0"-"9"])+>
}

long expr():
{
    Token x, y;
}
{
    x=<INTEGER> "+" y=<INTEGER> <EOF>
    {
        return Long.parseLong(x.image) + Long.parseLong(y.image);
    }
}

2. Code structure analysis

  1. STATIC option in the options block is set to false, this option is set to true, then all members and JavaCC generated methods will be defined as static, if STATIC set to true, the generated parser can not be used in a multithreaded environment , this option is always set to false. (The STATIC default is true)
  2. From PARSER_BEING (Adder) to PARSER_END (Adder) parser class definition. Parser class members and methods need to be defined also written here. In order to achieve even if only Adder class can run, defined here as the main function.
  3. After the SKIP and TOKEN section defines the scanner. SKIP said to skip spaces, tabs (tab) and line breaks. TOKEN represents an integer character scanning and generate token.
  4. long expr ... to the last part of the definition of a parser narrow. This partial parsing token sequence and perform certain operations.

3. main function code analysis

  The main function of the string of all command line arguments as a calculation object equation, be calculated by successively evaluate method.
  The method of generating evaluate Adder class object instance. And let Adder object to calculate (analytic) parameter string src.
  To run JavaCC generated parser class, you require the following two steps:

  1. Generating object instances of the parser classes
  2. Method statements generated objects with the same name and calls to parse

Point 1: parser JavaCC4.0 JavaCC5.0 generated and the following four types defined default constructor.

  • Parser (InputStream s): the first type constructor InputStream object passing through the structural analysis. This constructor encoded input string can not be set, like Chinese characters can not be processed.
  • Parser (InputStream s, String encoding): second kind InputStream object constructor addition, may be provided to generate input code string parser. But if the Chinese want to parse strings or comments, then you must use the first two kinds / 3 kinds constructor.
  • Parser (Reader r): A third constructor for resolving Reader into the read target content.
  • Parser (xxxx TokenManager tm): A fourth scanner is passed as a parameter.

  After a parser generator, method syntax called with the same name as this example and need to be resolved. Call here Adder object expr way back to start parsing after parsing the end will return to normal semantic value.

Guess you like

Origin www.cnblogs.com/suhaha/p/11697970.html