Matching opening and closing sequences of characters where the length of the closing sequence depends on the length of the opening sequence #4391
Unanswered
RomanShumikhin
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I am writing a simple grammar for a text where strings are represented in this format
"{n}.+"{n}
wheren
can be any positive number.I.e. a string can start with one or more double quotes and the parser should keep consuming characters until it finds a closing sequence of double quotes of the exactly same length as the opening opening sequence.
Valid strings in this format are:
and so on.
A string can contain line break, i.e this is also a valid string:
A string can also contain a sequence of double quotes which is not the same length as the closing sequence, but that sequence must be separated from the opening and closing double quote sequences by at least one character that is not a double quote.
This is a valid string ^ that contains a line break, a double quote, and another line break.
These are not valid strings for my grammar (the number opening double quotes is not equal to the number of closing double quotes):
I tried finding more information on how to do that, and the closest solution I found is this Stack Overflow question https://stackoverflow.com/questions/75542733/specifying-the-length-of-a-token-by-the-value-of-another-token-in-antlr-c-sharp
Could somebody please point me in the right direction?
I assume I need to record the length of the opening sequence of double quotes in a lexer (I managed to do that using the SO example), switch to a different lexer mode and start collecting characters until I find the closing sequence. The problem is in the consuming of the exact number of closing double quotes. I don't know how to do that in ANTLR4.
Thank you very much in advance.
Beta Was this translation helpful? Give feedback.
All reactions