SableCC parsing given wrong result

I tried to parse the valid message using sablecc. There are three type of valid message format.

  1. aaa; (three continuous alpha character +semi {messageid} messageid semi )
  2. mm; ( or two 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 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.

Upvotes: 0

Views: 401

Answers (1)

This error is caused by overlapping token definition. Sablecc is worked bottom up tree structure not a sequence manner. This is code for solve the problem. Thanks Etienne for cleared the 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 = [['a' .. 'z'] + ['A' .. 'Z']] ;
    semi = ';' bl*;
    hypene ='-';

Productions
    program =  {single} statement |
                {sequence} program statement;
    m = {a} a | {fa} fa ;            
    co = hypene a [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 ;

Upvotes: 0

Related Questions