Since the ANTLR uses this tool to implement a language for the calculation of the taxi field indicator, we begin to learn the Antr tool, using ANTLR4, to do some simple translation and experience of the record bar.
ANTLR is a powerful syntax generator that you can use to read, process, execute, or translate structured text or binary files. It is widely used in research and engineering to build languages, tools, and frameworks.
ANTLR automatically creates a syntax tree for the language you describe, showing how the input statements match the syntax, and automatically generates a syntax tree that the tracker uses to execute specific code on the nodes of the tree.
In order to implement a language, we need to create an application that can read the sentences and respond correctly to the input symbols and phrases. If the application is calculating and executing statements, we call it an interpreter, such as a calculator, a profile reader, and a Python interpreter. If we are simply converting sentences from one language to another, we call them compilers, such as converters and compilers for Java to C #. For an accurate response, the interpreter and compiler must identify all valid sentences (sentence), phrases (phrase), word groups (subphrase) in a particular language. Identifying a phrase (phrase) means that we can define different components and differentiate them. Once identified, the application needs to do the right thing.
Language-aware programs are called parsers or parser. Grammar refers to the rules governing the members of a language, and we specify the syntax of the language by establishing a ANTLR grammar file. A grammar file is a collection of rules, each of which represents the structure of a phrase (phrase). The grammar file itself needs to follow the syntax of one language: ANTLR's meta-language.
It will be much easier if we divide the parsing into two similar but functionally different stages. These two separate stages reflect the context in which our brains read text. The first is to process the character stream into a word, which is called lexical analysis (lexical), we can do lexical analysis of the program as a lexical analyzer (lexer). The lexical analyzer can make a word type, such as int, ID, float, and so on, with related words. After the lexical analyzer has typed the word as a type, the parser only needs to focus on the type.
The second is to recognize the structure of sentences through a series of words, called grammatical analysis. The ANTLR parser establishes a data structure, called a syntax tree, to document how the parser recognizes the structure of an input statement and the phrase component of a sentence, as shown in.
By establishing a syntax tree, the parser passes a convenient data structure such as a syntax tree to the program to pass the syntax parser to the message of how the word is composed into a phrase. Different applications that need to recognize the same language can reuse the syntax tree. The syntax tree is also handy for compilers that need to compile multiple times.
At the same time, ANTLR supports the integration of program code directly into the grammar file by adding attributes (attributes) and actions (actions) to the grammar file, which is done directly at the time of parsing.
The ANTLR tool generates a recursive descent parser (recursive-descent parsers) with syntax rules. The recursive descent parser is a collection of recursive methods that are recursively recursive from the root to the leaf. When parsing, when there are multiple options for a rule, you need to check one or more of the words entered next to make predictions, select one of the multiple options for the rule, and the work will be done automatically by ANTLR.
ANTLR provides two tree traversal mechanisms (tree-walking mechanisms) in its runtime. By default, ANTLR generates a listener (Listener) of a syntax tree to respond to events that are triggered by traversing the syntax tree. The listener is an automatic traversal of the tree in depth-first, and we only need to complete the various events that are triggered at each node, while the accessor (visitor) provides a controlled traversal, and we control the traversal to determine whether to invoke the child node's access method. Because this topic uses the way that the listener realizes, therefore elaborated the listener mechanism.
Syntax tree Listener (Parse trees Listener)
To be able to traverse the syntax tree and trigger the invocation of an event in the listener, the ANTLR runtime provides a class Parsetreewalker. In a language application, we create subclasses of the Parsetreelistener class to contain application-specific code. ANTLR generates a method of entry and exit for each rule, and when traversing into this node is the method that triggers the entry, the exit method is triggered when all the child nodes of the node are traversed. The mechanism of the listener is to automate the traversal of the syntax tree, without having to write the traversal of the syntax tree, and the listener provides a class that contains the input and output functions for each rule, and we simply inherit the class and replicate the functions.
ANTLR V4 Study Notes