跟踪 JFlex 中的状态
我正在编写一个自定义 Flex 文件来生成与 JSyntaxpane 一起使用的词法分析器。
我需要 lex 的自定义语言具有不同的状态,可以在一种堆栈中相互嵌入。
IE 中,您可以编写一个包含单引号字符串的表达式,然后使用特殊标记 eval() 在该字符串中嵌入另一个表达式。 但您也可以将表达式嵌入双引号字符串中。
例如:
someExpressionFunction('a single-quoted string with an eval(expression) embedded in it', "a double-quoted string with an eval(expression) embedded in it")
这是一个简化,有比这更多的状态,但假设我需要 DOUBLE_STRING 和 SINGLE_STRING 有不同的状态,它充分描述了我的情况。
确保关闭 eval 表达式时返回正确状态的最佳方法是什么(即,如果我在双引号中,则返回 DOUBLE_STRING,如果我在单引号中,则返回 SINGLE_STRING)
我想出的有效解决方案是使用堆栈和一些自定义方法来跟踪状态,以代替使用 yybegin 来启动不同的状态。
private Stack<Integer> stack = new Stack<Integer>();
public void yypushState(int newState) {
stack.push(yystate());
yybegin(newState);
}
public void yypopState() {
yybegin(stack.pop());
}
这是实现这一目标的最佳方法吗? 是否有我可以利用的更简单的 JFlex 内置功能或者我应该了解的最佳实践?
I'm writing a custom flex file to generate a lexer for use with JSyntaxpane.
The custom language I need to lex has different states that can be embedded into each other in a kind of stack.
I.E you could be writing an expression that has a single quoted string in it and then embed another expression within the string using a special token eval(). But you can also embed the expression within a double quoted string.
eg:
someExpressionFunction('a single-quoted string with an eval(expression) embedded in it', "a double-quoted string with an eval(expression) embedded in it")
This is a simplification, there are more states than this, but assuming I need to have different states for DOUBLE_STRING and SINGLE_STRING it adequately describes my situation.
What's the best way to ensure I return to the correct state upon closing the eval expression (i.e return to DOUBLE_STRING if I was in double quotes, SINGLE_STRING if I was in single quotes)
The solution I've come up with, which works, is to keep track of state using a Stack and some custom methods to use in lieu of using yybegin to start a different state.
private Stack<Integer> stack = new Stack<Integer>();
public void yypushState(int newState) {
stack.push(yystate());
yybegin(newState);
}
public void yypopState() {
yybegin(stack.pop());
}
Is this the best way to achieve this? Is there a simpler built-in function of JFlex I can leverage or a best practice I should know about?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是一种非常好的方法。 我实际上需要一些类似的功能来将 Groovy GString、Python 之类的 String 和一些 HTML 添加到 JavaDocs。
我还想添加的是词法分析器调用词法分析器来解析子部分。 就像嵌入 HTML 中的 JavaScript 一样。 但我没有时间去做这件事。
我喜欢 StackOverflow,但只是想知道你为什么不在 JSyntaxPane 的问题上发布这个?
I think that's one very good way of doing it. I actually needed some similar feature to add Groovy GString, Python like String and some HTML to JavaDocs.
What I would also like to add is a Lexer calling a Lexer to parse sub sections. Something like JavaScript embedded in HTML. But I could not get the time to do it.
I like StackOverflow, but just wondering why didn't you post this on JSyntaxPane's issues?