or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

character-streams.mddebug-support.mderror-handling.mdindex.mdlexical-analysis.mdparsing.mdtoken-streams.mdtree-construction.md
tile.json

tessl/maven-org-antlr--antlr-runtime

Java runtime library for ANTLR v3 - a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.antlr/antlr-runtime@3.5.x

To install, run

npx @tessl/cli install tessl/maven-org-antlr--antlr-runtime@3.5.0

index.mddocs/

ANTLR Runtime

ANTLR (ANother Tool for Language Recognition) runtime library for Java. This is a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions containing actions in a variety of target languages. The runtime provides excellent support for tree construction, tree walking, translation, error recovery, and error reporting.

Package Information

  • Package Name: org.antlr:antlr-runtime
  • Package Type: maven
  • Language: Java
  • Installation: Add to pom.xml: <dependency><groupId>org.antlr</groupId><artifactId>antlr-runtime</artifactId><version>3.5.3</version></dependency>

Core Imports

import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;

For specific classes:

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.Parser;
import org.antlr.runtime.Lexer;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeAdaptor;

Basic Usage

import org.antlr.runtime.*;

// Create input stream from string
CharStream input = new ANTLRStringStream("hello world");

// Create lexer (assuming MyLexer is generated by ANTLR)
MyLexer lexer = new MyLexer(input);

// Create token stream
TokenStream tokens = new CommonTokenStream(lexer);

// Create parser (assuming MyParser is generated by ANTLR) 
MyParser parser = new MyParser(tokens);

// Parse input
try {
    MyParser.program_return result = parser.program();
    CommonTree tree = (CommonTree) result.getTree();
    System.out.println(tree.toStringTree());
} catch (RecognitionException e) {
    System.err.println("Parsing failed: " + e.getMessage());
}

Architecture

ANTLR runtime is built around several key components:

  • Input Streams: Character and token streams for reading input data
  • Lexical Analysis: Lexer classes for tokenizing character streams
  • Parsing: Parser classes for syntactic analysis of token streams
  • Tree Construction: AST building and manipulation utilities
  • Error Handling: Comprehensive exception hierarchy for parse errors
  • Debug Support: Debugging and profiling tools for parser development

Capabilities

Character Input Streams

Character stream implementations for reading input from various sources including strings, files, and readers.

public interface CharStream extends IntStream {
    public static final int EOF = -1;
    public String substring(int start, int stop);
    public int LT(int i);
    int getLine();
    void setLine(int line);
    void setCharPositionInLine(int pos);
    int getCharPositionInLine();
}

public class ANTLRStringStream implements CharStream {
    public ANTLRStringStream(String input);
}

public class ANTLRFileStream extends ANTLRStringStream {
    public ANTLRFileStream(String fileName) throws IOException;
    public ANTLRFileStream(String fileName, String encoding) throws IOException;
}

Character Input Streams

Token Streams

Token stream implementations for managing sequences of tokens produced by lexers.

public interface TokenStream extends IntStream {
    public Token LT(int k);
    int range();
    public Token get(int i);
    public TokenSource getTokenSource();
    public String toString(int start, int stop);
    public String toString(Token start, Token stop);
}

public class CommonTokenStream implements TokenStream {
    public CommonTokenStream(TokenSource tokenSource);
    public CommonTokenStream(TokenSource tokenSource, int channel);
}

Token Streams

Lexical Analysis

Base classes and interfaces for implementing lexers and token sources.

public abstract class Lexer extends BaseRecognizer implements TokenSource {
    protected CharStream input;
    public Lexer(CharStream input);
    public Lexer(CharStream input, RecognizerSharedState state);
    public abstract Token nextToken();
}

public interface Token {
    public static final int EOF = CharStream.EOF;
    public static final int DEFAULT_CHANNEL = 0;
    public static final int HIDDEN_CHANNEL = 99;
    
    public String getText();
    public void setText(String text);
    public int getType();
    public void setType(int ttype);
    public int getLine();
    public void setLine(int line);
    public int getCharPositionInLine();
    public void setCharPositionInLine(int pos);
}

Lexical Analysis

Parsing

Core parsing infrastructure including parser base classes and rule return scopes.

public class Parser extends BaseRecognizer {
    public TokenStream input;
    public Parser(TokenStream input);
    public Parser(TokenStream input, RecognizerSharedState state);
    public void setTokenStream(TokenStream input);
    public TokenStream getTokenStream();
}

public abstract class BaseRecognizer {
    public static final int MEMO_RULE_FAILED = -2;
    public static final int MEMO_RULE_UNKNOWN = -1;
    
    public void reset();
    public Object match(IntStream input, int ttype, BitSet follow) throws RecognitionException;
}

Parsing

Tree Construction and Manipulation

Abstract Syntax Tree (AST) construction, traversal, and manipulation utilities.

public interface Tree {
    public Tree getChild(int i);
    public int getChildCount();
    public Tree getParent();
    public void setParent(Tree t);
    public void addChild(Tree t);
    public void setChild(int i, Tree t);
    public Object deleteChild(int i);
    public String toString();
    public String toStringTree();
}

public abstract class BaseTree implements Tree {
    protected List<Tree> children;
    public void addChild(Tree t);
    public void setChild(int i, Tree t);
    public String toStringTree();
}

public class CommonTree extends BaseTree {
    public Token token;
    public CommonTree(Token t);
    public String toString();
}

Tree Construction and Manipulation

Error Handling

Comprehensive exception hierarchy and error recovery mechanisms for robust parsing.

public class RecognitionException extends Exception {
    public IntStream input;
    public int index;
    public Token token;
    public Object node;
    public int c;
    public int line;
    public int charPositionInLine;
    public boolean approximateLineInfo;
}

public class MismatchedTokenException extends RecognitionException {
    public int expecting;
}

public class NoViableAltException extends RecognitionException {
    public int decisionNumber;
    public int stateNumber;
}

Error Handling

Debug Support

Debugging, profiling, and tracing tools for parser development and optimization.

public interface DebugEventListener {
    public void enterRule(String grammarFileName, String ruleName);
    public void exitRule(String grammarFileName, String ruleName);
    public void consumeToken(Token token);
    public void consumeHiddenToken(Token token);
    public void LT(int i, Token t);
}

public class DebugParser extends Parser {
    protected DebugEventListener dbg;
    public void setDebugListener(DebugEventListener dbg);
}

public class Profiler extends BlankDebugEventListener {
    public ProfileStats getDecisionProfile();
    public String getReport();
}

Debug Support

Types

Core Interfaces

public interface IntStream {
    public static final int EOF = -1;
    public void consume();
    public int LA(int i);
    public int mark();
    public int index();
    public void rewind(int marker);
    public void rewind();
    public void release(int marker);
    public void seek(int index);
    public int size();
    public String getSourceName();
}

public interface TokenSource {
    public Token nextToken();
    public String getSourceName();
}

public class CommonToken implements Token, Serializable {
    protected int type;
    protected int line;
    protected int charPositionInLine;
    protected int channel;
    protected transient CharStream input;
    protected String text;
    protected int index;
    protected int start;
    protected int stop;
    
    public CommonToken(int type);
    public CommonToken(CharStream input, int type, int channel, int start, int stop);
    public CommonToken(int type, String text);
    public CommonToken(Token oldToken);
    
    public String getText();
    public void setText(String text);
    public int getType();
    public void setType(int ttype);
    public int getLine();
    public void setLine(int line);
    public int getCharPositionInLine();
    public void setCharPositionInLine(int pos);
    public int getChannel();
    public void setChannel(int channel);
    public int getTokenIndex();
    public void setTokenIndex(int index);
    public CharStream getInputStream();
    public void setInputStream(CharStream input);
    public int getStartIndex();
    public void setStartIndex(int start);
    public int getStopIndex();
    public void setStopIndex(int stop);
    public String toString();
}

State and Configuration

public class RecognizerSharedState {
    public BitSet[] following;
    public int _fsp;
    public boolean errorRecovery;
    public int lastErrorIndex;
    public boolean failed;
    public int syntaxErrors;
    public int backtracking;
    public Map<Integer, Integer>[] ruleMemo;
    public int tokenStartLine;
    public int tokenStartCharPositionInLine;
    public int channel;
    public int type;
    public String text;
}

public class RuleReturnScope {
    public Token getStart();
    public Token getStop(); 
    public Object getTree();
}