I just started work on a grammar to read well, context free grammars,
and am running into a problem. (I'm probably just doing something dumb.)
I've attached my grammar.

The ARROW token (used between the left and right sides of a production)
should recognize either ':' or '->', but the AntlrWorks interpreter only
accepts '->'. If I try to parse the input 'a -> b;', I get the proper
result. If I try to parse 'a : b;', it gives a MismatchedTokenException.
(I am pretty sure I saw the same behavior using the debug option, but I
don't have the JDK on this computer and can't confirm it.)

The rules in question are:

  COLON : ':'; // used in multiple places

  ARROW 
      : '->'
      | COLON
      ;

  production
      : SYMBOL ARROW disjunction SEMICOLON
      ;

(If you're curious, COLON is used elsewhere so I put it into its own
token. (I saw a couple posts from this mailing list previously
recommending not using string literals in parsing rules.) What I'm
posting is a slightly simplified version where that other use is
removed. It doesn't change the behavior though.)


The parse tree AntlrWork's interpreter shows for 'a->b;' is

           rules
             |
        production
             |
   +-----+---+-----+----------+
   |     |         |          |
   a    ->     disjunction    ;
                   |
                termString
                   |
                 term
                   |
                 atom
                   |
                   b

But for 'a: b;' I get

           rules
             |
        production
             |
   +---------+-----+
   |               |
   a    MismatchedTokenException(11!=16)

Can anyone explain what's going on?

Thanks,
Evan

--

You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.


grammar cfg;

PIPE    : '|';
PLUS    : '+';
STAR    : '*';

LPAREN  : '(';
RPAREN  : ')';
LBRACE  : '{';
RBRACE  : '}';

COLON   : ':';
SEMICOLON : ';';


NUMBER
        : '0'..'9'+
        ;

fragment LETTER 
        : 'a'..'z' | 'A'..'Z' | '0'..'9' | '_' | '-'
        ;

// Encompasses both terminals and nonterminals. Distinguishing them is done 
later.
// This is way more broad than your typical programming language identifier: 
leading
// digits are OK and hyphens are OK.
SYMBOL
        : LETTER+
        ;
        
// For separating productions and rules
ARROW   
        : '->'
        | COLON
        ;

WHITESPACE
        : '\r' | '\n' | ' ' { $channel = HIDDEN; }
        ;
        


// Either a symbol or id:symbol, or a parenthisized expression.
atom
        : SYMBOL
        | NUMBER COLON SYMBOL
        | LPAREN disjunction RPAREN
        ;

term
        : atom (STAR|PLUS)?
        ;

// Represents concatenation     
termString
        : term+
        ;

disjunction
        : termString (PIPE termString)*
        ;       


production
        : SYMBOL ARROW disjunction SEMICOLON
        ;

rules
        : production+
        ;

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to