java - SableCC parsing given wrong result -


i tried parse valid message using sablecc. there 3 type of valid message format.

  1. aaa; (three continuous alpha character +semi {messageid} messageid semi )
  2. mm; ( or 2 continuous alpha or numeric character {flightnum} carriercode semi)
  3. -amm (or hyphen + alpha character + 2 continuous alpha or numeric character {load} hypene co semi )

when input valid string programme, 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' .. '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 java code throws parser exception :

simpleadders.parser.parserexception: [1,1] expecting: messageid, carriercode, '-'

even though first string valid.

this error caused overlapping token definition. sablecc worked bottom tree structure not sequence manner. code solve problem. etienne cleared problem.

helpers     /* our helpers */      sp = ' ' ;     cr = 13 ; // carriage return     lf = 10 ; // line feed     tab = 9 ; // tab char     bl = sp | cr | lf | tab;   tokens     /* our simple token definition(s). */      fa = ['0' .. '9'] ;     = [['a' .. 'z'] + ['a' .. 'z']] ;     semi = ';' bl*;     hypene ='-';  productions     program =  {single} statement |                 {sequence} program statement;     m = {a} | {fa} fa ;                 co = hypene [m1]:m [m2]:m semi;     messageid = [a1]:a [a2]:a [a3]:a semi ;     carriercode =[m1]:m [m2]:m semi;                 statement = {messageid} messageid|                 {flightnum}carriercode |                 {load} co ; 

Comments