ANTLR4 Python 3从字符串中删除引号
我有一个规则来匹配语法中的字符串。我现在需要的是字符串的内容,而不是引号本身,所以我希望去掉引号ANTLR4 Python 3从字符串中删除引号,antlr,antlr4,Antlr,Antlr4,我有一个规则来匹配语法中的字符串。我现在需要的是字符串的内容,而不是引号本身,所以我希望去掉引号 StringLiteral : UnterminatedStringLiteral '"' ; UnterminatedStringLiteral : '"' (~["\\\r\n] | '\\' (. | EOF))* ; 我在上看到了一个解决方案,但它是ANTLR的旧版本,我需要将其转换为Python 3。有人对此有解决方案吗?ANTLR3语法: STRING: '\"' C
StringLiteral
: UnterminatedStringLiteral '"'
;
UnterminatedStringLiteral
: '"' (~["\\\r\n] | '\\' (. | EOF))*
;
我在上看到了一个解决方案,但它是ANTLR的旧版本,我需要将其转换为Python 3。有人对此有解决方案吗?ANTLR3语法:
STRING: '\"' CHARS '\"' {setText(getText().substring(1, getText().length()-1));} ;
仍在ANTLR4中工作。如果要将Java代码移植到Python,请执行以下操作:
StringLiteral
: UnterminatedStringLiteral '"' {self.text = self.text[1:-1]}
;
grammar YourGrammarName;
@lexer::header {
import re
}
...
StringLiteral
: UnterminatedStringLiteral '"' {self.text = re.sub(r'\\(.)', '\\1', self.text[1:-1])}
;
如果您还想去除用于转义其他字符的\
,请执行以下操作:
StringLiteral
: UnterminatedStringLiteral '"' {self.text = self.text[1:-1]}
;
grammar YourGrammarName;
@lexer::header {
import re
}
...
StringLiteral
: UnterminatedStringLiteral '"' {self.text = re.sub(r'\\(.)', '\\1', self.text[1:-1])}
;
ANTLR3语法:
STRING: '\"' CHARS '\"' {setText(getText().substring(1, getText().length()-1));} ;
仍在ANTLR4中工作。如果要将Java代码移植到Python,请执行以下操作:
StringLiteral
: UnterminatedStringLiteral '"' {self.text = self.text[1:-1]}
;
grammar YourGrammarName;
@lexer::header {
import re
}
...
StringLiteral
: UnterminatedStringLiteral '"' {self.text = re.sub(r'\\(.)', '\\1', self.text[1:-1])}
;
如果您还想去除用于转义其他字符的\
,请执行以下操作:
StringLiteral
: UnterminatedStringLiteral '"' {self.text = self.text[1:-1]}
;
grammar YourGrammarName;
@lexer::header {
import re
}
...
StringLiteral
: UnterminatedStringLiteral '"' {self.text = re.sub(r'\\(.)', '\\1', self.text[1:-1])}
;