[antlr-interest] Newbie: NoViableAltException

Diehl, Matthew J matthew.j.diehl at intel.com
Wed Jul 25 11:59:16 PDT 2007


> -----Original Message-----
> From: Buck, Robert [mailto:rbuck at verisign.com] 
> Sent: Tuesday, July 24, 2007 6:30 PM
> To: Andrew Lentvorski
> Cc: Diehl, Matthew J; antlr-interest at antlr.org
> Subject: RE: [antlr-interest] Newbie: NoViableAltException
> 
> Say, how did you get ANTLRWorks to display the output in that format,
> e.g. '(', TUPLET "a1", '=', '(', TUPLET "a2", '=', TUPLET "v2))"
> 
> Or was that just your handywork?
> 
> That is the sort of output that would be useful to see. The UI is only
> showing the grammar rules, not what they are made up of.
> 
> Bob
Yeah, others have mentioned ways similar to this, and sorry my
explanation was pretty sloppy last time, but the way to do this was
somewhat explained in my first e-mail.  I didn't write it all out due to
lack of time, but I'll do it again for a simple grammar:

Given the following lexer stuff:
/////lexer stuff:
Quote : '\"';
Backslash : '\\';
Mul : '*';
Plus : '+';
Minus : '-';

BinaryString
  : Quote ('0'|'1'|'-'|'x'|'l'|'h'|'z'|'_')+ Quote
  ;

StringLiteral
  : Quote (GraphicCharacter | Backslash | Mul | ' ')* Quote
  ;

Exponent
  : 'e' (Plus | Minus)? Integer
  ;

fragment
Integer
  : '0'..'9' ('_' | '0'..'9')*
  ;
fragment
GraphicCharacter

WS : (' ' | '\t' | '\n' | '\r'){$channel=HIDDEN;}
  ;

//////To display the emitted tokens (note, fragments are not tokens, but
parts of one, so we don't need to do anything with the fragment rules)
make parser rules for each lexer rule:

//parser stuff:
grammar mytest;
everything
  : quote | backslash | mul | plus | minus | binaryString |
stringLiteral | exponent
  ;
quote
  : Quote
  ;
backslash
  : Backslash
  ;
mul 
  : Mul
  ;
plus 
  : Plus
  ;
minus 
  : Minus
  ;
binaryString 
  : BinaryString
  ;
stringLiteral 
  : StringLiteral
  ;
exponent
  : Exponent
  ;
<add the lexer stuff from above>
(notice that there is an individual parser (first letter lowercase) rule
for each lexer (first letter uppercase) rule)
If you run the debugger under the starting rule: 'everything', given
correct syntax, it will display the token stream in the window.  If this
doesn't make sense...sorry, just give it a try.

Matt


More information about the antlr-interest mailing list