Pregunta

I am having a problem with comments in creating custom file in NetBeans. I got two types of multiline comment types:

  1. starting with /* and ending */
  2. starting with <!-- and ending -->

MORE : { "/*": XSCRIPT_COMMENT | "<!--": XML_COMMENT }

<XSCRIPT_COMMENT> TOKEN : { <X_SCRIPT_COMMENT_END: "*/" > : DEFAULT }

<XML_COMMENT> TOKEN : { <XML_COMMENT_END: "-->" > : DEFAULT }

<XSCRIPT_COMMENT,XML_COMMENT> MORE : { < ~[] > }

the problem is, that both multiline comments throws TokenMgrError when I write the initial part of comment (/* or <!--). The error occurs only when there is no ending part and lexer reaches end of file.

My goal is to create multiline comments which works similar to other comment types (When only initial part is written, the rest of document is comment type text).

Please excuse my english, not my native language.

¿Fue útil?

Solución 2

One way to do it is to use a single regular expression to match comments. For example a /* .. */ multiline comment can be matched by

"/*" (("*")* ~["*"])* ("*" ("*")* "/"?)?

Here I made the final "*/" optional. (The best way to come up with this sort of regular expression, in my experience, is to convert an NDFA to an RE, if you know how to do that.)

Another way to do it is to allow an empty string to end a comment. Add these two rules.

<XML_COMMENT> TOKEN : { <XML_COMMENT_END: "" > : DEFAULT }
<XSCRIPT_COMMENT> TOKEN : { <X_SCRIPT_COMMENT_END: "" > : DEFAULT }

Otros consejos

For me, MartinZ's aswer also resolve an JavaCC lexical problem.

 { <COMMENT: "/*" (~["*"])* |  
             "/*" (~["*"])* "*" (~["/"])* |
             "/*" (~["*"])* "*" (~["/"])* "/" > }

Solved by different regular expression. First I created deterministic automaton just with pen and paper, but graphic view is always the best.

<XSCRIPT_COMMENT:  
 "/*" (~["*"])* 
|
 "/*" (~["*"])* "*" (~["/"])*
| 
 "/*" (~["*"])* "*" (~["/"])* "/"
>

<XML_COMMENT:
 "<!--" (~["-"])*
|
 "<!--" (~["-"])* "-" (~["-"])*
|
 "<!--" (~["-"])* "-" (~["-"])* "-" (~[">"])*
|
 "<!--" (~["-"])* "-" (~["-"])* "-" (~[">"])* ">"
>

I realize that both expressions aren't from minimized DFA, but like this it is easier to understand.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top