如何使用 antlr 在两个终端规则中以不同方式解析特殊字符? [英] How can I parse a special character differently in two terminal rules using antlr?
问题描述
我有一个语法在许多终端规则的开头使用 $
字符,例如 $video{
、$audio{
、$image{
、$link{
和其他类似的.
I have a grammar that uses the $
character at the start of many terminal rules, such as $video{
, $audio{
, $image{
, $link{
and others that are like this.
但是,我也想匹配所有不匹配这些规则的 $
和 {
和 }
字符.例如,我的语法没有正确匹配 CHUNK 规则中的 $100
,而是将 $
添加到 中可接受字符的长列表中>CHUNK 导致其他产生式规则被破坏.
However, I'd also like to match all the $
and {
and }
characters that don't match these rules too. For example, my grammar does not properly match $100
in the CHUNK rule, but adding the $
to the long list of acceptable characters in CHUNK causes the other production rules to break.
如何更改我的语法,使其足够聪明,可以将普通的 $、{ 和} 字符与我的特殊产生式规则区分开来?
How can I change my grammar so that it's smart enough to distinguish normal $, { and } characters from my special production rules?
基本上我能做的就是说,如果 $ 字符后面没有 {、视频、图像、音频、链接等,那么它应该转到 CHUNK".>
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 {
CHUNK
CHUNK }
CHUNK
CHUNK $
CHUNK f
CHUNK o
CHUNK o
CHUNK
BEGIN_VIDEO $video{
编辑
如果你让你的解析器输出一个 AST,你可以很容易地将一个或多个 CHUNK
匹配的所有文本合并成一个单一的 AST,其内部标记的类型为 CHUNK
,像这样:
EDIT
And 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 在两个终端规则中以不同方式解析特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!