ANTLR http://antlr.org/
ANTLRWorks http://antlr.org/works/index.html
ANTLR Cheet sheet http://www.antlr.org/wiki/display/ANTLR3/ANTLR+Cheat+Sheet
grammar SwiftBlock;
message : block+;
block : '{' '1' ':' expr {System.out.println("expr1=" + $expr.text);} '}' |
'{' '2' ':' expr {System.out.println("expr2=" + $expr.text);} '}' |
'{' '3' ':' expr3* '}' |
'{' '4' ':' expr4+ '}' |
'{' '5' ':' expr5+ '}' ;
expr3 : '{' expr ':' expr '}' {System.out.println("expr3=" + $expr3.text);} ;
expr4 : ('\n'|'\r')+ (':' expr ':' expr {System.out.println("expr4=" + $expr4.text);} | '-')+;
expr5 : ('\n'|'\r')+ '{' expr ':' expr '}' {System.out.println("expr5=" + $expr5.text);} ;
expr : FIELDVALUE* {System.out.println("expr=" + $expr.text);} ;
/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
FIELDVALUE : (~('-'|':'|'}'|'{'|'\n'|'\r'))+;
Grammar Interpreter
Syntax Diagram
Parse tree detail
java -cp .:/home/app/antlr-3.1.3/lib/antlr-3.1.3.jar org.antlr.Tool SwiftBlock.g
import java.io.FileInputStream;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
public class AntlrSwiftBlock {
public static void main(String[] args) throws Exception {
FileInputStream fileInputStream = new FileInputStream("/home/dave/workspace/antlrSwift/src/swift.txt");
ANTLRInputStream input = new ANTLRInputStream(fileInputStream);
SwiftBlockLexer lexer = new SwiftBlockLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
SwiftBlockParser parser = new SwiftBlockParser(tokens);
parser.message();
}
}
{1:F01AAAABB99BSMK3513951576}{2:O9400934081223BBBBAA33XXXX03592332770812230834N}{3:{113:xxxx}{108:abcdefgh12345678}}{4:
:20:0112230000000890
:25:SAKG800030155USD
:28C:255/1
:60F:C011223USD175768,92
:61:0112201223CD110,92NDIVNONREF//08 IL053309
/GB/2542049/SHS/312,
:62F:C011021USD175879,84
-}{5:
{CHK:0F4E5614DD28}}
expr=F01AAAABB99BSMK3513951576
expr1=F01AAAABB99BSMK3513951576
expr=O9400934081223BBBBAA33XXXX03592332770812230834N
expr2=O9400934081223BBBBAA33XXXX03592332770812230834N
expr=113
expr=xxxx
expr3={113:xxxx}
expr=108
expr=abcdefgh12345678
expr3={108:abcdefgh12345678}
expr=20
expr=0112230000000890
expr4=
:20:0112230000000890
expr=25
expr=SAKG800030155USD
expr4=
:25:SAKG800030155USD
expr=28C
expr=255/1
expr4=
:28C:255/1
expr=60F
expr=C011223USD175768,92
expr4=
:60F:C011223USD175768,92
expr=61
expr=0112201223CD110,92NDIVNONREF//08 IL053309
expr4=
:61:0112201223CD110,92NDIVNONREF//08 IL053309
line 7:0 required (...)+ loop did not match anything at input '/GB/2542049/SHS/312,'
expr=62F
expr=C011021USD175879,84
expr4=
:62F:C011021USD175879,84
expr=CHK
expr=0F4E5614DD28
expr5=
{CHK:0F4E5614DD28}
No comments:
Post a Comment