1

I am trying to compile a test class to test a simple grammer.

import org.antlr.v4.runtime.*;


public class Test {
    public static void main(String[] args) throws Exception 
    {
        CharStream input = null;
        // pick an input stream (filename from commandline or stdin)
        if(args.length > 0) input = new ANTLRFileStream(args[0]);
        else input = new ANTLRInputStream(System.in);
        // create the lexer
        DrinkLexer lex = new DrinkLexer(input);
        // create a buffer of tokens between the lexer and parser
        CommonTokenStream tokens = new CommonTokenStream(lex);
        // create the parser, attaching it to the token buffer
        DrinkParser p = new DrinkParser(tokens);
        p.drinkSentence(); // launch parser at drinkSentence file
    }
}

How would I go about replacing the deprecated class?

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
  • 1
    Clearly it says "as of 4.7 Please use CharStreams interface. "https://www.antlr.org/api/Java/org/antlr/v4/runtime/ANTLRFileStream.html and more can be learned from https://www.antlr.org/api/Java/org/antlr/v4/runtime/CharStreams.html – Lex Li Apr 12 '22 at 05:57

1 Answers1

2

Use the various static methods from CharStreams:

CharStream input = null;
// pick an input stream (filename from commandline or stdin)
if(args.length > 0) input = CharStreams.fromFileName(args[0]);
else input = CharStreams.fromStream(System.in);
// create the lexer
DrinkLexer lex = new DrinkLexer(input);
// create a buffer of tokens between the lexer and parser
CommonTokenStream tokens = new CommonTokenStream(lex);
// create the parser, attaching it to the token buffer
DrinkParser p = new DrinkParser(tokens);
p.drinkSentence(); // launch parser at drinkSentence file
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288