Introduction to ANTLR (another tool for language recognition) [Translation]

zhaozj2021-02-11  175

Introduction of affinity ANTLR grammar

Through example, you can gradually learn ANTLR is the best. A simple calculator is often used to get started, the reason is simple: it is easy to understand. This has many similarities and tutorials giving Antlr, but I will use my own language to describe a calculator. First we create some programs that can be evaluated directly to simple expressions. Then, I will generate a tree structure and calculate these trees to get the same answer.

When you know that you need to decompose a character input stream into multiple marks, the good start is to think about the textual structure of an expression.

Grammatical Wizard Execution

Language recognition

We accept arithmetic expressions including , - and *, such as 3 4 * 5-1, or enhanced parentheses expressions, such as (3 4) * 5.

All ANTLR grammar is subclasses of Lexer, syntax parsing programs, and syntax parsers. Since you should start thinking about the problem in the grammatism, you should create a subclass of a textual analysis. After the category declaration, you can use expansion Bacolls model symbol specified rules:

Class Exprparser Extends Parser;

EXPR: MEXPR ((Plus | minus) MEXPR) *

;

MEXPR

: Atom (Star Atom) *

;

Atom: int

| LPAREN EXPR RPAREN

;

Lexer follows similar patterns that only need to define some operators and blank characters. Put Lexer into the same file, such as expr.g, is the easiest thing to do:

Class Exprlexer Extends Lexer;

Options {

K = 2; // Needed for newline Junk

Charvocabulary = '/ u0000' .. '/ u007f'; // allow ascii

}

Lparen: '(';

Rparen: ')';

Plus: ' ';

Minus: '-';

Star: '*';

INT: ('0' .. '9') ;

WS: (''

| '/ r' '/ n'

| '/ n'

| '/ t'

)

{$ settype (token.skip);

;

From this syntax definition file expr.g generating program (Java), you can run the ANTLR as follows:

$ Java Antlr.tool Expr.g

ANTLR PARSER Generator Version 2.7.2 1989-2003 jguru.com

$

What is Antlr what?

When it is found that there is no need to complete this tutorial, you may see what Antlr has generated in the identifier file and found that this is very inspirated. ANTLR generates an identification program that simulates something created by the grammar analysis program you submitted by manual recursive; on the other hand, YACC and his friends generate a table that is full of plasticity when simulating the self-motivation.

ANTLR will produce the following files:

Exprlexer.java

Exprparser.java

Exprparsertokentypes.java

ExprparsertokeTypes.txt

If you look at the content, such as ExprParser.java, you will see a method to generate a method for each rule defined in the file expr.g. For example, the code of the MEXPR in ATOM rules looks similar to the following code:

Public void mexpr () {

atom ();

While (la (1) == star) {match (star);

atom ();

}

}

Public void atom () {

Switch (la (1)) {// switch on lookahead token Type

Case Int:

Match (int);

Break;

Case Lparen:

Match (lparen);

EXPR ();

Match (rparen);

Break;

DEFAULT:

// Error

}

}

Note that the rule definition is translated into method calls, while the marking definition is translated into a Match (token) function call. The only difficulty in creating a syntax Pars is calculating forward-seeking information.

The marker class defines your lexer analyzer (Lexer) and all marks categories used by Parser:

/ / $ ANTLR 2.7.2: "expr.g" -> "exprparser.java" $

Public interface exprparsrtokentypes {

INT EOF = 1;

INT NULL_TREE_LOOKAHEAD = 3;

INT PLUS = 4;

INT minus = 5;

INT star = 6;

INT INT = 7;

INT lparen = 8;

INT rPAREN = 9;

INT WS = 10;

}

Test Lexer / Parser

In order to use Parser as a result in ExprParser.java in practice, you must use the main () function as follows:

Import Antlr. *;

Public class

Main

{

Public static void main (string [] args) throws exception {

ExprleXer Lexer = New ExprleXer (System.in);

Exprparser Parser = New Exprparser (Lexer);

Parser.expr ();

}

}

$ Java

Main

3 (4 * 5)

$

Give the wrong input:

$ Java

Main

3

LINE 1: 3: Unexpected token:

$

or

$ Java

Main

3 (4

LINE 1: 6: EXPECTING RPAREN, FOUND 'NULL'

$

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

New Post(0)