ANTLR4 RegEx词法分析器模式 [英] ANTLR4 RegEx lexer modes

查看:189
本文介绍了ANTLR4 RegEx词法分析器模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为XSD内的RegEx使用Regx解析器. 我以前的问题描述如下: ANTLR4解析RegEx

I am working on a Regx parser for RegEx inside XSD. My previous problem was descrived here: ANTLR4 parsing RegEx

从那以后,我将Lexer和Parser分开了. 现在我在解析括号内的括号时遇到了问题.应将它们视为方括号内的字符,并视为外部方括号. 这是我的词法分析器语法:

I have split the Lexer and Parser since than. Now I have a problem parsing parantheses inside brackets. They should be treated as characters inside the brackets and as grouping tokens outside. This is my lexer grammar:

lexer grammar RegExLexer;

Char    : ALPHA ;
Int     : DIGIT ;

LBrack  : '[' ;//-> pushMode(modeRange) ;
RBrack  : ']' ;//-> popMode ;
LBrace  : '(' ;
RBrace  : ')' ;
Semi    : ';' ;
Comma   : ',' ;
Asterisk: '*' ;
Plus    : '+' ;
Dot     : '.' ;
Dash    : '-' ;
Question: '?' ;
LCBrace : '{' ;
RCBrace : '}' ;
Pipe    : '|' ;
Esc     : '\\' ;

WS : [ \t\r\n]+ -> skip ;

fragment DIGIT : [0-9] ;
fragment ALPHA : [a-zA-Z] ;

这是示例:

[0-9a-z()]+

我觉得我应该在方括号中使用模式来更改ALPHA片段的行为.如果我复制该片段,则会收到一条错误消息,提示我不能两次声明. 我已经阅读了有关此内容的参考资料,但仍然不知道应该怎么做.

I feel like i should use modes on brackets to change the behaviour of ALPHA fragment. If I copy the fragment, I get an error saying I can't have the declaration twice. I have read the reference about this and I still don't get what i should do.

如何实现这些模式?

推荐答案

您将不得不在解析器(而不是词法分析器)中处理此问题.当lexer看到'('时,它将返回令牌LBrace.对于lexer而言,没有关于令牌在何处可见的上下文.它只是将输入分割为令牌.您必须定义解析规则,并且在处理解析树时,然后您可以确定括号中是否包含LBrace.

You're going to have to handle this in the parser, not the lexer. When lexer sees a '(', it will return token LBrace. For lexer, there is no context as to where token is seen. It simply carves up the input into tokens. You will have to define parse rules and when processing parse tree, you can then determine was the LBrace inside brackets or not.

这篇关于ANTLR4 RegEx词法分析器模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆