I'm using ANTLR4 to parse a document (wikitext). In this document, strings surrounded by '' are italicized (like ''italics''). Strings surrounded by ''' are bold (like '''bold'''). But there is also the possibility of strings with both styles applied: '''''both'''''.
The issue is that these delimiters overlap each other. As a result, when the parser encounters five single quotes, it doesn't understand whether it's bold followed by italics or italics followed by bold. And I don't know how to tell it.
Ideally, it would know that it should choose whichever interpretation results in a successful parse, but if it can do that, I don't know how to make it work.
My grammar so far:
grammar WikiText; NOWIKI_OPEN: '<nowiki>'; NOWIKI_CLOSE: '</nowiki>'; BOLD: '\'\'\''; ITALICS: '\'\''; CHAR: .; nowiki: NOWIKI_OPEN CHAR* NOWIKI_CLOSE; plainText: CHAR+; boldText: BOLD wiki* BOLD; italicText: ITALICS wiki* ITALICS; nonPlainText: (boldText | italicText)+; wiki: (nonPlainText | plainText)+; document: (wiki | nowiki)*; 

'''''hi'' there.'''. You cannot tokenize beyond a single quote. Semantic analysis must be employed after parsing. Then, rewrite the tree afterwards for''and'''operators.