Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
    • Switch to GitLab Next
  • Sign in / Register
netbeans-antlr
netbeans-antlr
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4
    • Issues 4
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 0
    • Merge requests 0
  • Requirements
    • Requirements
    • List
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Operations
    • Operations
    • Incidents
    • Environments
  • Packages & Registries
    • Packages & Registries
    • Container Registry
  • Analytics
    • Analytics
    • CI/CD
    • Code Review
    • Insights
    • Issue
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Peter
  • netbeans-antlrnetbeans-antlr
  • Wiki
  • Code to compile a g4 file in runtime, produce a parser and lexer programatically

Last edited by Peter Aug 27, 2017
Page history

Code to compile a g4 file in runtime, produce a parser and lexer programatically

package antlr;

import com.github.mcheung63.TreeNode;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.antlr.runtime.tree.Tree;
import org.antlr.v4.Tool;
import org.antlr.v4.parse.ANTLRParser;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.LexerInterpreter;
import org.antlr.v4.runtime.ParserInterpreter;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.Rule;
import org.antlr.v4.tool.ast.GrammarAST;
import org.antlr.v4.tool.ast.GrammarASTErrorNode;
import org.antlr.v4.tool.ast.GrammarRootAST;
import org.junit.Test;

/**
 *
 * @author Peter <peter@quantr.hk>
 */
public class TestAntlrTool {

	@Test
	public void testToolCompileProgramatically() throws Exception {
		//String[] arg0 = {pathOfG4File, "-package", "mypackage"};
		//Tool tool = new Tool(arg0);
		Tool tool = new Tool();
		String content = new String(Files.readAllBytes(Paths.get(getClass().getResource("Assembler.g4").toURI())));
		GrammarRootAST ast = tool.parseGrammarFromString(content);
		System.out.println("ast.grammarType=" + ast.grammarType);
		if (ast.grammarType == ANTLRParser.COMBINED) {
			System.out.println("ast=" + ast);
			if ((GrammarAST) ast instanceof GrammarASTErrorNode) {
				System.out.println("GrammarASTErrorNode");
				return;
			}

			if (ast.hasErrors) {
				System.out.println("hasErrors");
				return;
			}

			Grammar grammar = tool.createGrammar(ast);
			tool.process(grammar, false);
			System.out.println("grammar=" + grammar);
			System.out.println("tool=" + tool);

			for (String rule : grammar.getRuleNames()) {
				System.out.println("rule=" + rule);
			}
			TreeNode<Tree> rootNode = new TreeNode<Tree>(null);
			LexerInterpreter lex = grammar.createLexerInterpreter(new ANTLRInputStream(";comment1\nmov ac,bx"));
			for (Token token : lex.getAllTokens()) {
				System.out.println("token=" + token);
			}
			lex.reset();

			BaseErrorListener printError = new BaseErrorListener() {
				@Override
				public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol,
						final int line, final int position, final String msg,
						final RecognitionException e) {
					System.out.println("ERROR :" + line + ":" + position + ": " + msg);
				}
			};

			CommonTokenStream tokenStream = new CommonTokenStream(lex);
			ParserInterpreter parser = grammar.createParserInterpreter(tokenStream);
			parser.getInterpreter().setPredictionMode(PredictionMode.LL);
			parser.removeErrorListeners();
			parser.addErrorListener(printError);
			String startRule = "assemble";
			Rule start = grammar.getRule(startRule);
			ParserRuleContext parserRuleContext = parser.parse(start.index);
			System.out.println("parserRuleContext.toStringTree()=" + parserRuleContext.toStringTree());
		}
	}
}
Clone repository
  • Code to compile a g4 file in runtime, produce a parser and lexer programatically
  • Code to parse a g4 file and produce a ast tree view
  • Home