I tried to parse the valid message using sablecc. There are three type of valid message format.
{messageid} messageid semi
){flightnum} carriercode semi
){load} hypene co semi
)when I input valid string to the programme, it did not work.
input:
abc; //type 1
ZZ; //type 2
ZZ; //type 2
-ab2; //type3
sablecc grammar code :
Helpers
/* Our helpers */
fa = ['0' .. '9'] ;
a = [['a' .. 'z'] + ['A' .. 'Z']] ;
m= [a + fa];
sp = ' ' ;
cr = 13 ; // carriage return
lf = 10 ; // line feed
tab = 9 ; // tab char
bl = sp | cr | lf | tab;
Tokens
/* Our simple token definition(s). */
semi = ';' bl*;
co = (a)(m)(m);
messageid = (a)(a)(a) ;
carriercode = (m)(m);
hypene ='-';
Productions
program = {single} statement |
{sequence} program statement;
statement = {messageid} messageid semi |
{flightnum}carriercode semi |
{load} hypene co semi ;
compilation succeed, when run the java code it throws parser exception :
simpleAdders.parser.ParserException: [1,1] expecting: messageid, carriercode, '-'
Even though first string is valid.
PushbackReader input = new PushbackReader(new FileReader(filename), 1024); Lexer lexer = new Lexer(input){ @Override protected void filter() throws LexerException, IOException { System.out.println(this.token.getClass().getSimpleName() + ": [" + token.getText() + "]"); } }; Parser parser = new Parser(lexer); return parser.parse();
TCo: [abc] [1,1]: expecting: messageid, carriercode, '-'.
Etienne Gagnon, Ph.D. http://sablecc.org
messageid) other than throwing the exception, right? . is it any way to check sequentially (for an example first character is alpha ( there will be possibility, the statement may be
messageid or
flightnum), or is hyphene (it should be
load statement
) ,then second character like wise ....).