1

I am writing a lexer for a custom language and want to match a single line of comment starting with # like in python but here comment always starts from the beginning of line. Example:

#this is a comment and #this is not a comment

I have tried this: "^#" (~["\n"])* ("\n")? but not working. How can I achieve this?

1 Answer 1

0

Unfortunately JavaCC does not support the ^ for start of line, nor the $ for end of line.

You can use lexical states. Use the default state * for the start of a line. For example:

// If at the start of a line, skip a # and all other charaters on the line.
// Stay in the same state.
<*>          SKIP : { <COMMENT : "#" (~["\n"])* ("\n") > : * }

// Skip any newlines and return to (or stay in) the start of line state.
<*, MIDLINE> SKIP : { <NEWLINE : "\n" >                  : * }

// Skip any other white space.
<*, MIDLINE> SKIP : { <WHITESPACE : " " | "\t"  >        : MIDLINE }

// If not at the start of line, a # is a token.
<MIDLINE>    TOKEN : { <HASH    : "#" >                   : MIDLINE }

// Numbers can be at the start of a line or the middle of a line.
// After a number, we are no longer at the start of line.
<*, MIDLINE> TOKEN : { <NUMBER : ["0"-"9"]+ >            : MIDLINE }

and so on.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.