Why does this simple grammar fail to spot that pattern?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   jeancallisti
Posted On:   Tuesday, June 26, 2012 07:21 AM

Hello people, I know there are newbies (like me) asking all the time why a grammar fails, or how to disambiguate, etc., and that must be utterly boring/irritating. But I really can't figure out why it does that. My complete grammar is actually much more complex, but I've stripped it down to the simplest version (one single rule) to find the cause of the issue, and still can't find it. ======== on to the issue ========== Below you'll find a simple grammar. Yet, when I feed it with this (a text file containing nothing else), it fails to recognize it : /** @pluginName:   agsMyPlugin2 **/ The error message is : line 1:0 no viable alternative at input    More>>

Hello people,
I know there are newbies (like me) asking all the time why a grammar fails, or how to disambiguate, etc., and that must be utterly boring/irritating.

But I really can't figure out why it does that.

My complete grammar is actually much more complex, but I've stripped it down to the simplest version (one single rule) to find the cause of the issue, and still can't find it.


======== on to the issue ==========

Below you'll find a simple grammar.

Yet, when I feed it with this (a text file containing nothing else), it fails to recognize it :
/** @pluginName:   agsMyPlugin2 **/

The error message is :
line 1:0 no viable alternative at input '/** @pluginName:   agsMyPlugin2 **/ '

The grammar is below.



========== actual grammar =============

grammar EasyPluginGrammar;


options {
  language = Java;

  output = AST; //we tell ANTLR that we want to produce a crawlable tree
  ASTLabelType = CommonTree; // each node of the tree will be of type "CommonTree"
}

tokens { 
  SPECIALCOMMENTTAG;
  RAWSPECIALCOMMENT;
  NOSPECIALCOMMENT;

 }

 

plugin
      : specialComment EOF!
      ;



specialComment
        : '/**' '@' scTag ':' scValue '**/' -> SPECIALCOMMENTTAG scTag scValue
        | -> NOSPECIALCOMMENT
        ;
       
scTag : IDENT ;

scValue : RSC_TEXT //I also tried with "IDENT" instead of "RSC_TEXT"
        ;
 

fragment LETTER : 'a'..'z'|'A'..'Z' ;
fragment DIGIT : '0'..'9' ;
     
IDENT  : ( LETTER ) ( LETTER | DIGIT )* ;
 
INTEGER : DIGIT + ;


RSC_TEXT : ( ASCII_EXCEPT_BACKSLASH )+ ;

fragment ASCII_EXCEPT_BACKSLASH : ' '..'[' | ']'..'~' ;


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


fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;

fragment
HEX_DIGIT : (DIGIT|'a'..'f'|'A'..'F') ;

fragment
ESC_SEQ
    :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
    |   UNICODE_ESC
    |   OCTAL_ESC
    ;

fragment
OCTAL_ESC
    :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7')
    ;

fragment
UNICODE_ESC
    :   '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
    ;






   <<Less

Re: Why does this simple grammar fail to spot that pattern?

Posted By:   jeancallisti  
Posted On:   Thursday, June 28, 2012 04:19 AM

Thanks a lot for your suggestion, I will let you know asap.

Re: Why does this simple grammar fail to spot that pattern?

Posted By:   franz  
Posted On:   Wednesday, June 27, 2012 02:08 PM

I think the problem is the lexer doesn't know what to match after '@'. From the lexers view pluginName could be IDENT or RSC_NAME. But i've to admit I'm also new to ANTLR, but i tested with 

scValue : IDENT //I also tried with "IDENT" instead of "RSC_TEXT" 
        ; 

and it works with input:  /** @pluginName:   agsMyPlugin2 **/ 

If you need to have other token than IDENT there, rewrite it to an imaginary token RSC_NAME while constructing the tree.

Let me know if that helps, please.

Regards,
Franz
 
About | Sitemap | Contact