Difference between revisions of "Antlr"
Jump to navigation
Jump to search
Line 10: | Line 10: | ||
= Base Class Language Parser = | = Base Class Language Parser = | ||
The abstract base class LanguageParser has some help code that makes language development and debugging easier. | The abstract base class LanguageParser has some help code that makes language development and debugging easier. | ||
+ | |||
+ | === Example === | ||
+ | <source lang='java'> | ||
+ | /** | ||
+ | * example parser | ||
+ | * @author wf | ||
+ | * | ||
+ | */ | ||
+ | public class ExpLanguageParser extends LanguageParser { | ||
+ | private ExpParser parser; | ||
+ | ExpLexer lexer; | ||
+ | |||
+ | public ExpParser getParser() { | ||
+ | return parser; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected ParseTree getRootContext(Parser parser) { | ||
+ | if (!(parser instanceof ExpParser)) { | ||
+ | throw new RuntimeException("wrong parser type for getRootContext, expected Rule but got "+parser.getClass().getName()); | ||
+ | } else { | ||
+ | ExpParser expParser=(ExpParser) parser; | ||
+ | return expParser.eval(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected ParseTree parse(ANTLRInputStream in, String inputText) | ||
+ | throws Exception { | ||
+ | lexer = new ExpLexer(in); | ||
+ | parser=new ExpParser(getTokens(lexer)); | ||
+ | ParseTree result=super.parse(lexer,getParser()); | ||
+ | return result; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void showParseTree() { | ||
+ | super.showParseTree(getParser()); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </source> | ||
= Motivation = | = Motivation = |
Revision as of 13:32, 14 October 2017
ANTLR is a parser generator tool.
BITPlan has been using ANTLR in projects for a few years now and Wolfgang Fahl has been active in improving ANTLR see e.g. #Motivation
Library with helpers for ANTLR Language development com.bitplan.antlr
To simplify Parser development with ANTLR BITPlan has created a library with some helper code for ANTLR Language Development and published it as Open Source at:
Base Class Language Parser
The abstract base class LanguageParser has some help code that makes language development and debugging easier.
Example
/**
* example parser
* @author wf
*
*/
public class ExpLanguageParser extends LanguageParser {
private ExpParser parser;
ExpLexer lexer;
public ExpParser getParser() {
return parser;
}
@Override
protected ParseTree getRootContext(Parser parser) {
if (!(parser instanceof ExpParser)) {
throw new RuntimeException("wrong parser type for getRootContext, expected Rule but got "+parser.getClass().getName());
} else {
ExpParser expParser=(ExpParser) parser;
return expParser.eval();
}
}
@Override
protected ParseTree parse(ANTLRInputStream in, String inputText)
throws Exception {
lexer = new ExpLexer(in);
parser=new ExpParser(getTokens(lexer));
ParseTree result=super.parse(lexer,getParser());
return result;
}
@Override
public void showParseTree() {
super.showParseTree(getParser());
}
}