AFAIK, l'erreur est parce que nestingBlockCommentCharacters
peut match de +/
(deux fois ~'/'
).
Personnellement, je garderais le nestingBlockComment
comme une règle de lexer au lieu d'une règle d'analyseur. Vous pouvez le faire en ajoutant un peu helper dans la classe lexer:
public boolean openOrCloseCommentAhead() {
// return true iff '/+' or '+/' is ahead in the character stream
}
puis dans un lexer commentaire règle, utilisez un gated semantic predicates avec cette méthode d'aide comme l'expression booléenne dans le prédicat:
// match nested comments
Comment
: '/+' (Comment | {!openOrCloseCommentAhead()}?=> Any)* '+/'
;
// match any character
Any
: .
;
une petite démo-grammaire:
grammar DComments;
@lexer::members {
public boolean openOrCloseCommentAhead() {
return (input.LA(1) == '+' && input.LA(2) == '/') ||
(input.LA(1) == '/' && input.LA(2) == '+');
}
}
parse
: token+ EOF
;
token
: Comment {System.out.println("comment :: "+$Comment.text);}
| Any
;
Comment
: '/+' (Comment | {!openOrCloseCommentAhead()}?=> Any)* '+/'
;
Any
: .
;
et une classe principale pour le tester:
import org.antlr.runtime.*;
public class Main {
public static void main(String[] args) throws Exception {
ANTLRStringStream in = new ANTLRStringStream(
"foo /+ comment /+ and +/ comment +/ bar /+ comment +/ baz");
DCommentsLexer lexer = new DCommentsLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
DCommentsParser parser = new DCommentsParser(tokens);
parser.parse();
}
}
Ensuite, les commandes suivantes:
java -cp antlr-3.2.jar org.antlr.Tool DComments.g
javac -cp antlr-3.2.jar *.java
java -cp .:antlr-3.2.jar Main
(pour Windows, la dernière commande est: java -cp .;antlr-3.2.jar Main
)
produire la sortie suivante:
comment :: /+ comment /+ and +/ comment +/
comment :: /+ comment +/