forked from jasder/antlr
play with tests
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9879]
This commit is contained in:
parent
6013c4c97d
commit
9ae5049d44
|
@ -1,29 +1,8 @@
|
|||
grammar E;
|
||||
|
||||
prog: classDef+ ; // match one or more class definitions
|
||||
|
||||
classDef
|
||||
: 'class' ID '{' member+ '}' // a class has one or more members
|
||||
{System.out.println("class "+$ID.text);}
|
||||
lexer grammar E;
|
||||
DUH : 'eee' {int y=1;}
|
||||
| 'fff' {int z=3;}
|
||||
;
|
||||
|
||||
member
|
||||
: 'int' ID ';' // field definition
|
||||
{System.out.println("var "+$ID.text);}
|
||||
| 'int' f=ID '(' ID ')' '{' stat '}' // method definition
|
||||
{System.out.println("method: "+$f.text);}
|
||||
;
|
||||
|
||||
stat: expr ';'
|
||||
{System.out.println("found expr: "+$stat.text);}
|
||||
| ID '=' expr ';'
|
||||
{System.out.println("found assign: "+$stat.text);}
|
||||
;
|
||||
|
||||
expr: INT
|
||||
| ID '(' INT ')'
|
||||
;
|
||||
|
||||
ID : ('a'..'z'|'A'..'Z')+ ;
|
||||
INT : '0'..'9'+ ;
|
||||
WS : (' '|'\t'|'\n'|'\r')+ {$channel=HIDDEN;} ;
|
||||
I : '0'..'9'+ {System.out.println("I");}
|
||||
| 'z' {int x = 2;}
|
||||
;
|
||||
WS : (' '|'\n') -> type(WS) ;
|
||||
|
|
|
@ -1,49 +1,11 @@
|
|||
/*
|
||||
[The "BSD license"]
|
||||
Copyright (c) 2011 Terence Parr
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. The name of the author may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
import org.antlr.v4.runtime.*;
|
||||
|
||||
public class TestE {
|
||||
public static void main(String[] args) throws Exception {
|
||||
// START:rig
|
||||
CharStream input = new ANTLRFileStream(args[0]);
|
||||
ELexer lexer = new ELexer(input);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
EParser parser = new EParser(tokens);
|
||||
parser.setBuildParseTree(true);
|
||||
ParserRuleContext ctx = parser.prog();
|
||||
System.out.println(ctx.toStringTree(parser));
|
||||
// Either view in dialog box:
|
||||
//ctx.inspect(parser);
|
||||
// Or in postscript
|
||||
ctx.save(parser, "/tmp/t.ps");
|
||||
// System.out.println(Trees.getPS(ctx, parser, "Helvetica", 10));
|
||||
// END:rig
|
||||
}
|
||||
public static void main(String[] args) throws Exception {
|
||||
CharStream input = new ANTLRFileStream(args[0]);
|
||||
E lex = new E(input);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lex);
|
||||
tokens.fill();
|
||||
for (Object t : tokens.getTokens()) System.out.println(t);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue