Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
package {

	import org.antlr.runtime.*;

	public class AntlrActionScriptTest {
		public function AntlrActionScriptTest(input:String) {
			var lexer:TLexer = new TLexer(new ANTLRStringStream(input));
			var tokens:CommonTokenStream = new CommonTokenStream(lexer);

			var parser:TParser = new TParser(tokens);
			parser.entry_rule();
 		}
	}
}

For complex parsers, you may want to avoid creating the lexers and parsers each time you want to parse input. In this case you can do the following, which will reset the lexer and parser by setting the input property:

Code Block

package {

	import org.antlr.runtime.*;

	public class AntlrActionScriptTest {

		private static var lexer:TLexer = new TLexer(null);
		private static var parser:TParser = new TParser(null);

		public function AntlrActionScriptTest(input:String) {
			lexer.input = new ANTLRStringStream(input);
			var tokens:CommonTokenStream = new CommonTokenStream(lexer);

			parser.input = tokens;
			parser.entry_rule();
 		}
	}
}

If you want to access the tokens types in your code, you'll have to import and access them from the lexer or parser module (e.g. TLexer.EOF, TLexer.IDENTIFIER):

...