Basically what I'd to be able to do is say, "if the $ character doesn't have {, video, image, audio, link, etc. after it, then it should go to CHUNK".grammar Text;@header {}@lexer::members { private boolean readLabel = false; private boolean readUrl = false;}@members { private int numberOfVideos = 0; private int numberOfAudios = 0; private StringBuilder builder = new StringBuilder(); public String getResult() { return builder.toString(); }}text : expression* ;expression : fillInTheBlank { builder.append($fillInTheBlank.value); } | image { builder.append($image.value); } | video { builder.append($video.value); } | audio { builder.append($audio.value); } | link { builder.append($link.value); } | everythingElse { builder.append($everythingElse.value); } ;fillInTheBlank returns [String value] : BEGIN_INPUT LABEL END_COMMAND { $value = "<input type=\"text\" id=\"" + $LABEL.text + "\" name=\"" + $LABEL.text + "\" class=\"FillInTheBlankAnswer\" />"; } ;image returns [String value] : BEGIN_IMAGE URL END_COMMAND { $value = "<img src=\"" + $URL.text + "\" />"; } ;video returns [String value] : BEGIN_VIDEO URL END_COMMAND { numberOfVideos++; StringBuilder b = new StringBuilder(); b.append("<div id=\"video1\">Loading the player ...</div>\r\n"); b.append("<script type=\"text/javascript\">\r\n"); b.append("\tjwplayer(\"video" + numberOfVideos + "\").setup({\r\n"); b.append("\t\tflashplayer: \"/trainingdividend/js/jwplayer/player.swf\", file: \""); b.append($URL.text); b.append("\"\r\n\t});\r\n"); b.append("</script>\r\n"); $value = b.toString(); } ;audio returns [String value] : BEGIN_AUDIO URL END_COMMAND { numberOfAudios++; StringBuilder b = new StringBuilder(); b.append("<p id=\"audioplayer_"); b.append(numberOfAudios); b.append("\">Alternative content</p>\r\n"); b.append("<script type=\"text/javascript\">\r\n"); b.append("\tAudioPlayer.embed(\"audioplayer_"); b.append(numberOfAudios); b.append("\", {soundFile: \""); b.append($URL.text); b.append("\"});\r\n"); b.append("</script>\r\n"); $value = b.toString(); } ; link returns [String value] : BEGIN_LINK URL END_COMMAND { $value = "<a href=\"" + $URL.text + "\">" + $URL.text + "</a>"; } ; everythingElse returns [String value] : CHUNK { $value = $CHUNK.text; } ;BEGIN_INPUT : '${' { readLabel = true; } ;BEGIN_IMAGE : '$image{' { readUrl = true; } ;BEGIN_VIDEO : '$video{' { readUrl = true; } ;BEGIN_AUDIO : '$audio{' { readUrl = true; } ;BEGIN_LINK : '$link{' { readUrl = true; } ;END_COMMAND : { readLabel || readUrl }?=> '}' { readLabel = false; readUrl = false; } ;URL : { readUrl }?=> 'http://' ('a'..'z'|'A'..'Z'|'0'..'9'|'.'|'/'|'-'|'_'|'%'|'&'|'?'|':')+ ;LABEL : { readLabel }?=> ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9')* ;CHUNK //: (~('${'|'$video{'|'$image{'|'$audio{'))+ : ('a'..'z'|'A'..'Z'|'0'..'9'|' '|'\t'|'\n'|'\r'|'-'|','|'.'|'?'|'\''|':'|'\"'|'>'|'<'|'/'|'_'|'='|';'|'('|')'|'&'|'!'|'#'|'%'|'*')+ ;推荐答案否定的字符不能超过一个.因此,以下内容无效:You can't negate more than a single character. So, the following is invalid:~('${')但为什么不简单地将 '$'、'{' 和 '}' 添加到您的 CHUNK规则 和 删除 CHUNK 规则末尾的 +(否则它会吞噬很多,可能 '$video{' 在源代码中更进一步,因为您已经注意到自己了)?But why not simply add '$', '{' and '}' to your CHUNK rule and remove the + at the end of the CHUNK rule (otherwise it would gobble up to much, possibly '$video{' further in the source, as you have noticed yourself already)?. 现在 CHUNK 令牌将总是由单个字符组成,但您可以创建一个生产规则来解决这个问题:Now a CHUNK token will always consist of a single character, but you could create a production rule to fix this:chunk : CHUNK+ ;并在生产规则中使用 chunk 而不是 CHUNK(当然也可以使用 CHUNK+).and use chunk in your production rules instead of CHUNK (or use CHUNK+, of course).像 "{ } $foo $video{" 这样的输入将被标记如下:Input like "{ } $foo $video{" would be tokenized as follows:CHUNK {CHUNKCHUNK }CHUNKCHUNK $CHUNK fCHUNK oCHUNK oCHUNKBEGIN_VIDEO $video{编辑如果你让你的解析器输出一个 AST,你可以很容易地将一个或多个 CHUNK 匹配的所有文本合并成一个单一的 AST,其内部标记的类型为 CHUNK,像这样:EDITAnd if you let your parser output an AST, you can easily merge all the text that one or more CHUNK's match into a single AST, whose inner token is of type CHUNK, like this:grammar Text;options { output=AST;}...chunk : CHUNK+ -> {new CommonTree(new CommonToken(CHUNK, $text))} ;... 这篇关于如何使用 antlr 在两个终端规则中以不同方式解析特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 13:15