[antlr-interest] dynamic parsers
Terence Parr
parrt at cs.usfca.edu
Mon Nov 21 13:24:26 PST 2005
On Nov 21, 2005, at 1:17 PM, Sergej Bogomolow wrote:
> I've got a question concerned dynamic parsers. Does
> antlr 3.0 make it possible to create dynamic parsers,
> i.e. intepreters?
You bet! I'm going to make it even better. Here is one of my
functional tests:
public void testSimpleParse() throws Exception {
Grammar pg = new Grammar(
"parser grammar p;\n"+
"prog : WHILE ID LCURLY (assign)* RCURLY;\n" +
"assign : ID ASSIGN expr SEMI ;\n" +
"expr : INT | FLOAT | ID ;\n");
Grammar g = new Grammar();
g.importTokenVocabulary(pg);
g.setGrammarContent(
"lexer grammar t;\n"+
"WHILE : \"while\";\n"+
"LCURLY : '{';\n"+
"RCURLY : '}';\n"+
"ASSIGN : '=';\n"+
"SEMI : ';';\n"+
"ID : ('a'..'z')+ ;\n"+
"INT : (DIGIT)+ ;\n"+
"FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n"+
"fragment DIGIT : '0'..'9';\n" +
"WS : (' ')+ ;\n");
CharStream input = new ANTLRStringStream("while x { i=1; y=3.42;
z=y; }");
Interpreter lexEngine = new Interpreter(g, input);
CommonTokenStream tokens = new CommonTokenStream(lexEngine);
tokens.setTokenTypeChannel(g.getTokenType("WS"), 99);
//System.out.println("tokens="+tokens.toString());
Interpreter parseEngine = new Interpreter(pg, tokens);
ParseTree t = parseEngine.parse("prog");
String result = t.toStringTree();
String expecting =
"(<grammar p> (prog [@0,0:4='while',<4>,1:0] [@2,6:6='x',<5>,1:6]
[@4,8:8='{',<6>,1:8] (assign [@6,10:10='i',<5>,1:10]
[@7,11:11='=',<8>,1:11] (expr [@8,12:12='1',<10>,1:12])
[@9,13:13=';',<9>,1:13]) (assign [@11,15:15='y',<5>,1:15]
[@12,16:16='=',<8>,1:16] (expr [@13,17:20='3.42',<11>,1:17])
[@14,21:21=';',<9>,1:21]) (assign [@16,23:23='z',<5>,1:23]
[@17,24:24='=',<8>,1:24] (expr [@18,25:25='y',<5>,1:25])
[@19,26:26=';',<9>,1:26]) [@21,28:28='}',<7>,1:28]))";
assertEqual(result, expecting);
}
I plan on making that really easy to just give me a few rules and
have it do it's magic.
> I have my statechart grammar. If I use my program to
> translate this grammar, then I'll get ASt class files
> and a Antlr file.
Actually, I'm interpreting all that so it can't have actions but is
much easier to use.
> Then I run antlr and the result is a
> parser. In the next step this parser should be
> compiled. Is it possible to create a dynamic parser
> which intepret the statechart grammar on the fly using
> antlr 3.0?
You can easily compile and run antlr parsers on the fly as every one
of my function tests do this. See the TestCompileAndExecSupport.java
file in the v3 distribution.
> It is important to point that the Java
> parser must be available in the compiled form at the
> end of the process.
No problem :)
Ter
More information about the antlr-interest
mailing list