Results 1 to 9 of 9
Thread: Single Line Comments in JavaCC
- 06-05-2011, 04:24 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 6
- Rep Power
- 0
Single Line Comments in JavaCC [SOLVED]
Hi all,
To be able to skip single-line comments starting with # in JavaCC, I found the following snippet:
This snippet works well except when the comment is placed on the last line of input file; because EOF cannot be matched with any of "\n" | "\r" | "\r\n". I tried to add <EOF> at the end of the regular expression but JavaCC gave me an error saying that it cannot identify <EOF>.Java Code:SKIP : { <"#" (~["\n","\r"])* ("\n" | "\r" | "\r\n")> }
Does anyone know how to handle this problem?
Thanks.Last edited by mjdousti; 06-07-2011 at 12:34 AM.
- 06-05-2011, 09:28 AM #2
Does JavaCC recognize '$' as the end of a line?
Get in the habit of using standard Java naming conventions!
- 06-05-2011, 10:01 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Have you tried the construction mentioned in the Faq (3.15: Why do the example Java and C++ token managers report an error when the last line of a file is a single line comment?). It's written for // comments, but perhaps it applies to # comments as well where the file doesn't end in a new line.
- 06-05-2011, 10:59 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,391
- Blog Entries
- 7
- Rep Power
- 17
Yep, that's the solution; I once hacked it by fiddling with the InputStream, i.e. it would synthesize a last new line sequence if the eof was reached before an end of line was read; the solution you mentioned is much less hackerish. (simply replace the // by # in the example).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-05-2011, 11:23 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
I just got bored and googled... I've never used JavaCC.
They mention in that Faq that "some compilers are more liberal and do not insist on that final newline". Is that the case with Java? I mean, are .java files *supposed* to end with a newline (like .c ones)? If so, it's the first I've heard of that...
- 06-05-2011, 11:52 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,391
- Blog Entries
- 7
- Rep Power
- 17
I checked the JLS: it mentions line comments as a sequence of chars following a // pair and ending with a new line sequence. That, to me, implies that a lexically correct Java sourse code file that has a one line comment on the last line should end with an end of line sequence. Note that the unicode escape sequence \u000a counts as an end of line sequence; this leaves the question: does a last line that doesn't contain a single line comment nees to have an end of line sequence? It must be somewhere in the JLS but I'm upgrading my Eclipse and Java installation at the moment. so if you're still bored, be my guest ;-)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-05-2011, 12:26 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
True. A quick check with Eclipse suggests that its compiler is of the "liberal" variety.That, to me, implies that a lexically correct Java sourse code file that has a one line comment on the last line should end with an end of line sequence
But where does the JLS say that? I see 3.7 (Comments):
Comment: EndOfLineComment
EndOfLineCmment: / / CharactersInLine_opt
CharactersInLine:
InputCharacter
CharactersInLine InputCharacter
3.4 Line Terminators says,
InputCharacter: UnicodeInputCharacter but not CR or LF
That would suggest that a comment can consist of two / followed by a whole bunch of characters (not CR or LF) and so may validly end a program source file.
-----
18.1 (the Grammar) says
CompilationUnit: [[Annotations] package QualifiedIdentifier ; ] {ImportDeclaration} {TypeDeclaration}
ie some optional stuff, followed by zero or more declarations. Which again suggests a new line isn't required. (the CompilationUnit is the "goal symbol"). Indeed the compilation unit might be empty.
- 06-05-2011, 12:32 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,391
- Blog Entries
- 7
- Rep Power
- 17
Near the top of 3.7:
To me that says that an end of line should be present for a single line comment ...
Originally Posted by JLS
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-05-2011, 08:36 PM #9
Member
- Join Date
- Dec 2007
- Posts
- 6
- Rep Power
- 0
A great discussion! Thanks to pbrockway2 and JosAH.
So, let me summarize the solution. It seems that JavaCC doesn't understand "$". Adding a "?" at the end of regular expression like below allows the lexical analyzer to skip the comment line till reaching a new line or even finishing the read without reading more characters, i.e. reaching end of file.
Java Code:SKIP : { <"#" (~["\n","\r"])* ("\n" | "\r" | "\r\n")[COLOR="red"][B]?[/B][/COLOR]> }
Similar Threads
-
Constructors pls explan the code line by line in comments
By vibaviattigala in forum New To JavaReplies: 1Last Post: 02-19-2011, 04:03 AM -
Take two inputs in single line
By Himanshu23 in forum New To JavaReplies: 5Last Post: 12-25-2010, 01:56 AM -
JavaCC parser generator.
By Danpanda in forum New To JavaReplies: 0Last Post: 04-19-2010, 12:07 AM -
Are my comments correct?
By twiggy62 in forum New To JavaReplies: 2Last Post: 02-08-2010, 05:34 AM -
Comments
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 08:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks