Antlr 跳过标签外的文本 [英] Antlr Skip text outside tag
问题描述
我试图跳过/忽略自定义标签外的文本:
Im trying to skip/ignore the text outside a custom tag:
此文本是要跳过的唯一标记
?compo \5+5\ ?> 还有这个 <?复合 \1+1\ ?>
This text is a unique token to skip < ?compo \5+5\ ?> also this < ?compo \1+1\ ?>
我尝试使用以下词法分析器:
I tried with the follow lexer:
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 测试器对其进行测试)...
But it doesn't work (I test it by using the intelliJ tester on the rule "instructions")...
我还在COMPOSER"模式之外添加了一些跳过规则:
I have also add some skip rules outside the "COMPOSER" mode:
TEXT_SKIP : TAG_CLOSE .*? (TAG_OPEN | EOF) -> skip;
但是我没有任何结果...
But i don't have any results...
有人可以帮助我吗?
我更改了指令",现在为每个标签的每条指令正确构建了解析器树:
I change "instructions" and now the parser tree is correctly builded for every instruction of every tag:
instructions : (.*? TAG_OPEN statement TAG_CLOSE .*?)+;
但是我在标签之外有一个无法识别的字符错误...
But i have a not recognized character error outside the the tags...
推荐答案
下面是一个对我有用的快速演示.
Below is a quick demo that worked for me.
词法分析器语法:
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
;
带有输入的测试 此文本是要跳过的唯一标记 <?compo 5+5 ?>这个 <?compo 1+1 ?>
导致了以下树:
A test with the input This text is a unique token to skip <?compo 5+5 ?> also this <?compo 1+1 ?>
resulted in the following tree:
这篇关于Antlr 跳过标签外的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!