Antlr 跳过标签外的文本
Antlr Skip text outside tag
我正在尝试 skip/ignore 自定义标签外的文本:
This text is a unique token to skip < ?compo +5\ ?> also this < ?compo +1\ ?>
我尝试使用以下词法分析器:
TAG_OPEN : '<?compo ' -> pushMode(COMPOSER);
mode COMPOSER;
TAG_CLOSE : ' ?>' -> popMode;
NUMBER_DIGIT : '1'..'9';
ZERO : '0';
LOGICOP
: OR
| AND
;
COMPAREOP
: EQ
| NE
| GT
| GE
| LT
| LE
;
WS : ' ';
NEWLINE : ('\r\n'|'\n'|'\r');
TAB : ('\t');
...
和解析器:
instructions
: (TAG_OPEN statement TAG_CLOSE)+?;
statement
: if_statement
| else
| else_if
| if_end
| operation_statement
| mnemonic
| comment
| transparent;
但它不起作用(我使用 intelliJ 测试器在规则 "instructions" 上测试它)...
我还在 "COMPOSER" 模式之外添加了一些跳过规则:
TEXT_SKIP : TAG_CLOSE .*? (TAG_OPEN | EOF) -> skip;
但是我没有任何结果...
有人可以帮助我吗?
编辑:
我更改了 "instructions",现在为每个标记的每条指令正确构建了解析器树:
instructions : (.*? TAG_OPEN statement TAG_CLOSE .*?)+;
但是我在标签之外有一个无法识别的字符错误...
下面是一个对我有用的快速演示。
词法分析器语法:
lexer grammar CompModeLexer;
TAG_OPEN
: '<?compo' -> pushMode(COMPOSER)
;
OTHER
: . -> skip
;
mode COMPOSER;
TAG_CLOSE
: '?>' -> popMode
;
OPAR
: '('
;
CPAR
: ')'
;
INT
: '0'
| [1-9] [0-9]*
;
LOGICOP
: 'AND'
| 'OR'
;
COMPAREOP
: [<>!] '='
| [<>=]
;
MULTOP
: [*/%]
;
ADDOP
: [+-]
;
SPACE
: [ \t\r\n\f] -> skip
;
解析器语法:
parser grammar CompModeParser;
options {
tokenVocab=CompModeLexer;
}
parse
: tag* EOF
;
tag
: TAG_OPEN statement TAG_CLOSE
;
statement
: expr
;
expr
: '(' expr ')'
| expr MULTOP expr
| expr ADDOP expr
| expr COMPAREOP expr
| expr LOGICOP expr
| INT
;
输入 This text is a unique token to skip <?compo 5+5 ?> also this <?compo 1+1 ?>
的测试产生了以下树:
我找到了另一个解决方案(不像以前那样优雅):
在通用上下文中创建通用 TEXT
令牌(因此在标签模式之外)
TEXT : ( ~[<] | '<' ~[?])+ -> skip;
创建一个解析器规则来处理通用文本
code
: TEXT
| (TEXT? instruction TEXT?)+;
创建一个解析器规则来处理指令
instruction
: TAG_OPEN statement TAG_CLOSE;
我正在尝试 skip/ignore 自定义标签外的文本:
This text is a unique token to skip < ?compo +5\ ?> also this < ?compo +1\ ?>
我尝试使用以下词法分析器:
TAG_OPEN : '<?compo ' -> pushMode(COMPOSER);
mode COMPOSER;
TAG_CLOSE : ' ?>' -> popMode;
NUMBER_DIGIT : '1'..'9';
ZERO : '0';
LOGICOP
: OR
| AND
;
COMPAREOP
: EQ
| NE
| GT
| GE
| LT
| LE
;
WS : ' ';
NEWLINE : ('\r\n'|'\n'|'\r');
TAB : ('\t');
...
和解析器:
instructions
: (TAG_OPEN statement TAG_CLOSE)+?;
statement
: if_statement
| else
| else_if
| if_end
| operation_statement
| mnemonic
| comment
| transparent;
但它不起作用(我使用 intelliJ 测试器在规则 "instructions" 上测试它)...
我还在 "COMPOSER" 模式之外添加了一些跳过规则:
TEXT_SKIP : TAG_CLOSE .*? (TAG_OPEN | EOF) -> skip;
但是我没有任何结果...
有人可以帮助我吗?
编辑:
我更改了 "instructions",现在为每个标记的每条指令正确构建了解析器树:
instructions : (.*? TAG_OPEN statement TAG_CLOSE .*?)+;
但是我在标签之外有一个无法识别的字符错误...
下面是一个对我有用的快速演示。
词法分析器语法:
lexer grammar CompModeLexer;
TAG_OPEN
: '<?compo' -> pushMode(COMPOSER)
;
OTHER
: . -> skip
;
mode COMPOSER;
TAG_CLOSE
: '?>' -> popMode
;
OPAR
: '('
;
CPAR
: ')'
;
INT
: '0'
| [1-9] [0-9]*
;
LOGICOP
: 'AND'
| 'OR'
;
COMPAREOP
: [<>!] '='
| [<>=]
;
MULTOP
: [*/%]
;
ADDOP
: [+-]
;
SPACE
: [ \t\r\n\f] -> skip
;
解析器语法:
parser grammar CompModeParser;
options {
tokenVocab=CompModeLexer;
}
parse
: tag* EOF
;
tag
: TAG_OPEN statement TAG_CLOSE
;
statement
: expr
;
expr
: '(' expr ')'
| expr MULTOP expr
| expr ADDOP expr
| expr COMPAREOP expr
| expr LOGICOP expr
| INT
;
输入 This text is a unique token to skip <?compo 5+5 ?> also this <?compo 1+1 ?>
的测试产生了以下树:
我找到了另一个解决方案(不像以前那样优雅):
在通用上下文中创建通用
TEXT
令牌(因此在标签模式之外)TEXT : ( ~[<] | '<' ~[?])+ -> skip;
创建一个解析器规则来处理通用文本
code : TEXT | (TEXT? instruction TEXT?)+;
创建一个解析器规则来处理指令
instruction : TAG_OPEN statement TAG_CLOSE;