ANTLR: Difference between revisions

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
imported>Citation bot
Added date. | Use this bot. Report bugs. | Suggested by Dominic3203 | Category:Parser generators | #UCB_Category 5/32
 
imported>Frap
 
Line 88: Line 88:
In the following example, a parser in ANTLR describes the sum of expressions can be seen in the form of "1 + 2 + 3":
In the following example, a parser in ANTLR describes the sum of expressions can be seen in the form of "1 + 2 + 3":
<syntaxhighlight lang="antlr">
<syntaxhighlight lang="antlr">
// Common options, for example, the target language
// Common options, for example, the target language
options
options
{
{
   language = "CSharp";
   language = "CSharp";
}
}
// Followed by the parser  
 
class SumParser extends Parser;
// Followed by the parser  
options
class SumParser extends Parser;
{
options
  k = 1; // Parser Lookahead: 1 Token
{
}
  k = 1; // Parser Lookahead: 1 Token
// Definition of an expression
}
statement: INTEGER (PLUS^ INTEGER)*;
 
// Here is the Lexer
// Definition of an expression
class SumLexer extends Lexer;
statement: INTEGER (PLUS^ INTEGER)*;
options
 
{
// Here is the Lexer
  k = 1; // Lexer Lookahead: 1 characters
class SumLexer extends Lexer;
}
options
PLUS: '+';
{
DIGIT: ('0'..'9');
  k = 1; // Lexer Lookahead: 1 characters
INTEGER: (DIGIT)+;
}
PLUS: '+';
DIGIT: ('0'..'9');
INTEGER: (DIGIT)+;
</syntaxhighlight>
</syntaxhighlight>
The following listing demonstrates the call of the parser in a program:
The following listing demonstrates the call of the parser in a program:
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
TextReader reader;
TextReader reader;
// (...) Fill TextReader with character
// (...) Fill TextReader with character
SumLexer lexer = new SumLexer(reader);
SumLexer lexer = new SumLexer(reader);
SumParser parser = new SumParser(lexer);
SumParser parser = new SumParser(lexer);


parser.statement();
parser.statement();
</syntaxhighlight>
</syntaxhighlight>


Line 163: Line 166:
* [https://tomassetti.me/antlr-mega-tutorial/ ANTLR (mega) Tutorial]
* [https://tomassetti.me/antlr-mega-tutorial/ ANTLR (mega) Tutorial]
* [http://www.bearcave.com/software/antlr/antlr_expr.html Why Use ANTLR?]
* [http://www.bearcave.com/software/antlr/antlr_expr.html Why Use ANTLR?]
*[http://www.placidsystems.com/antlrstudio.aspx ANTLR Studio]
* [http://www.placidsystems.com/antlrstudio.aspx ANTLR Studio]
* [https://mev.com/blog/building-a-real-estate-search-system-with-antlr Building a Real Estate Search System with ANTLR]


[[Category:1992 software]]
[[Category:1992 software]]

Latest revision as of 13:58, 11 June 2025

Template:Short description Template:More citations needed Script error: No such module "Infobox".Template:Template otherScript error: No such module "Check for unknown parameters".Template:Main other

In computer-based language recognition, ANTLR (pronounced antler), or ANother Tool for Language Recognition, is a parser generator that uses a LL(*) algorithm for parsing. ANTLR is the successor to the Purdue Compiler Construction Tool Set (PCCTS), first developed in 1989, and is under active development. Its maintainer is Professor Terence Parr of the University of San Francisco.Script error: No such module "Unsubst".

PCCTS 1.00 was announced April 10, 1992.[1][2]

Usage

ANTLR takes as input a grammar that specifies a language and generates as output source code for a recognizer of that language. While Version 3 supported generating code in the programming languages Ada95, ActionScript, C, C#, Java, JavaScript, Objective-C, Perl, Python, Ruby, and Standard ML,[3] Version 4 at present targets C#, C++, Dart,[4][5] Java, JavaScript, Go, PHP, Python (2 and 3), and Swift.

A language is specified using a context-free grammar expressed using Extended Backus–Naur Form (EBNF).Script error: No such module "Unsubst".[6] ANTLR can generate lexers, parsers, tree parsers, and combined lexer-parsers. Parsers can automatically generate parse trees or abstract syntax trees, which can be further processed with tree parsers. ANTLR provides a single consistent notation for specifying lexers, parsers, and tree parsers.

By default, ANTLR reads a grammar and generates a recognizer for the language defined by the grammar (i.e., a program that reads an input stream and generates an error if the input stream does not conform to the syntax specified by the grammar). If there are no syntax errors, the default action is to simply exit without printing any message. In order to do something useful with the language, actions can be attached to grammar elements in the grammar. These actions are written in the programming language in which the recognizer is being generated. When the recognizer is being generated, the actions are embedded in the source code of the recognizer at the appropriate points. Actions can be used to build and check symbol tables and to emit instructions in a target language, in the case of a compiler.Script error: No such module "Unsubst".[6]

Other than lexers and parsers, ANTLR can be used to generate tree parsers. These are recognizers that process abstract syntax trees, which can be automatically generated by parsers. These tree parsers are unique to ANTLR and help processing abstract syntax trees.Script error: No such module "Unsubst".[6]

Licensing

Template:Citation needed span and ANTLR 4 are free software, published under a three-clause BSD License.[7] Prior versions were released as public domain software.[8] Documentation, derived from Parr's book The Definitive ANTLR 4 Reference, is included with the BSD-licensed ANTLR 4 source.[7][9]

Various plugins have been developed for the Eclipse development environment to support the ANTLR grammar, including ANTLR Studio, a proprietary product, as well as the "ANTLR 2"[10] and "ANTLR 3"[11] plugins for Eclipse hosted on SourceForge.Script error: No such module "Unsubst".

ANTLR 4

ANTLR 4 deals with direct left recursion correctly, but not with left recursion in general, i.e., grammar rules x that refer to y that refer to x.[12]

Development

As reported on the tools[13] page of the ANTLR project, plug-ins that enable features like syntax highlighting, syntax error checking and code completion are freely available for the most common IDEs (Intellij IDEA, NetBeans, Eclipse, Visual Studio[14] and Visual Studio Code).

Projects

Software built using ANTLR includes:

Over 200 grammars implemented in ANTLR 4 are available on GitHub.[20] They range from grammars for a URL to grammars for entire languages like C, Java and Go.

Example

In the following example, a parser in ANTLR describes the sum of expressions can be seen in the form of "1 + 2 + 3":

// Common options, for example, the target language
options
{
  language = "CSharp";
}

// Followed by the parser 
class SumParser extends Parser;
options
{
  k = 1; // Parser Lookahead: 1 Token
}

// Definition of an expression
statement: INTEGER (PLUS^ INTEGER)*;

// Here is the Lexer
class SumLexer extends Lexer;
options
{
  k = 1; // Lexer Lookahead: 1 characters
}
PLUS: '+';
DIGIT: ('0'..'9');
INTEGER: (DIGIT)+;

The following listing demonstrates the call of the parser in a program:

TextReader reader;
// (...) Fill TextReader with character
SumLexer lexer = new SumLexer(reader);
SumParser parser = new SumParser(lexer);

parser.statement();

See also

Script error: No such module "Portal".

References

Template:Reflist

Bibliography

Template:Refbegin

  • Script error: No such module "citation/CS1".
  • Script error: No such module "citation/CS1".
  • Script error: No such module "citation/CS1".

Template:Refend

Further reading

Template:Refbegin

  • Script error: No such module "Citation/CS1".

Template:Refend

External links

  1. Script error: No such module "citation/CS1".
  2. Script error: No such module "citation/CS1".
  3. SML/NJ Language Processing Tools: User Guide
  4. Script error: No such module "citation/CS1".
  5. Script error: No such module "citation/CS1".
  6. a b c Script error: No such module "citation/CS1".
  7. a b Script error: No such module "citation/CS1".
  8. Template:Cite mailing list
  9. Script error: No such module "citation/CS1".
  10. Script error: No such module "citation/CS1".
  11. Script error: No such module "citation/CS1".
  12. What is the difference between ANTLR 3 & 4
  13. Script error: No such module "citation/CS1".
  14. Script error: No such module "citation/CS1".
  15. Script error: No such module "citation/CS1".
  16. Script error: No such module "citation/CS1".
  17. Script error: No such module "citation/CS1".
  18. Script error: No such module "citation/CS1".
  19. Script error: No such module "citation/CS1".
  20. Script error: No such module "citation/CS1".