[antlr-interest] Antlr first time user, help requested
John B. Brodie
jbb at acm.org
Tue Jul 6 07:42:48 PDT 2010
On Tue, 2010-07-06 at 09:47 -0400, John B. Brodie wrote:
....snipped....
> change all references to TEXT to be text
sorry, should be: change all references to TEXT to be text+
I thought that maybe i should test what I am recommending so attached
please find a grammar that parses your example input.
-------------- next part --------------
grammar Test;
options {
output = AST;
ASTLabelType = CommonTree;
}
@members {
private static final String [] x = new String[] {
"PageMetaData:\n"+
"name: This is a test name\n"+
"categories: category1, category2,\n"+
" category3\n"+
"note: These are notes\n"+
" that the newlines are important, but not the leading whitespace\n"
};
public static void main(String [] args) {
for( int i = 0; i < x.length; ++i ) {
try {
System.out.println("about to parse:`"+x[i]+"`");
TestLexer lexer = new TestLexer(new ANTLRStringStream(x[i]));
CommonTokenStream tokens = new CommonTokenStream(lexer);
TestParser parser = new TestParser(tokens);
TestParser.start_return p_result = parser.start();
CommonTree ast = p_result.tree;
if( ast == null ) {
System.out.println("resultant tree: is NULL");
} else {
System.out.println("resultant tree: " + ast.toStringTree());
}
System.out.println();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
start : definition ;
definition
: (NEWLINE! | WS!)* header NEWLINE
testName NEWLINE
categories NEWLINE
(tags NEWLINE)?
(note NEWLINE)?
(automatedTests NEWLINE)?
(automatedTest NEWLINE)?
EOF! ;
header : HEADER_TEXT ;
testName : NAME_LABEL text+;
categories : CATEGORIES_LABEL WS!? CAMELCASE (comma CAMELCASE)* ;
tags : 'tags:' WS!? CAMELCASE (comma CAMELCASE)* ;
note : 'note:' text+ (NEWLINE+ WS! text+)* ;
automatedTests : 'tests:' WS!? FILE (comma FILE)* ;
automatedTest : 'test:' text+ ;
text : ~NEWLINE;
comma : COMMA (NEWLINE!? WS!)?;
// single-line comments
SL_COMMENT :
'//'
( options { greedy=false; } : . )*
( '\r' | '\r\n' | '\n' ) // EOL untested under MS-Windows
{ $channel=HIDDEN; }
;
HEADER_TEXT : 'PageMetaData:' ;
NAME_LABEL : 'name:' ;
CATEGORIES_LABEL : 'categories:' ;
TAGS_LABEL : 'tags:' ;
NOTE_LABEL : 'note:' ;
AUTOMATED_TESTS_LABEL : 'automated-tests:' ;
AUTOMATED_TEST_LABEL : 'automated-test:' ;
COMMA : ',' ;
WS : (' ' | '\t')+ ;
NEWLINE : ('\r' '\n' | '\n' | '\r' );
CAMELCASE : ('A'..'Z'|'a'..'z'|'0'..'9')+;
FILE : ('A'..'Z'|'a'..'z'|'0'..'9'| '_' | '-' | '.' | '/')+;
OTHER : . ;
More information about the antlr-interest
mailing list