我一直在寻找一种不适合一般 Flex/Bison 范式的语言。根据语义上下文,它具有完全不同的标记规则。例如:
main() {
batchblock
{
echo Hello World!
set batchvar=Something
echo %batchvar%
}
}
Bison 显然支持识别这些类型的语法,但它需要“词汇搭配”来有效地支持它们。它提供了一个执行此操作的接口 - 但我很困惑如何根据上下文提供不同的 Flex 正则表达式 - 如果这可能的话。
提前致谢 :)
I've been looking to recognise a language which does not fit the general Flex/Bison paradigm. It has completely different token rules depending on semantic context. For example:
main() {
batchblock
{
echo Hello World!
set batchvar=Something
echo %batchvar%
}
}
Bison apparently supports recognition of these types of grammars, but it needs "Lexical Tie Ins" to support them effectively. It provides an interface for doing this -- but I'm confused as to how exactly I can supply different flex regexes depending on the context -- if this is even possible.
Thanks in advance :)
发布评论
评论(2)
Flex 有一个状态机制,您可以通过它在不同的正则表达式集之间切换。其语法位于
文件顶部(在
%}
之后)和匹配规则中(在第一个%%
之后),则此正则表达式仅在以下情况下匹配在那种状态下。还有一个全局状态
<*>
可用于匹配任何状态中的任何内容。改变状态的方法不止一种。例如,如果您想保留状态堆栈,则使用
yy_pop_state
和yy_push_state
。或者,您可以使用BEGIN(name_of_state)
。要返回到初始状态,请使用BEGIN(INITIAL)
。Flex has a state mechanism whereby you can switch it between different sets of regexes. The syntax for this is
at the top of the file (after the
%}
) and in your matching rules (after the first%%
)Then this regex is only matched when in that state. There is also a global state
<*>
which can be used to match anything in any state.There is more than one way to change states. For example,
yy_pop_state
andyy_push_state
if you want to keep a stack of states. Alternatively you can useBEGIN(name_of_state)
. To go back to the initial state, useBEGIN(INITIAL)
.具体而言,如果您的特殊块始终由“batchblock {”发出信号,则可以完全在 Flex 内部处理 - 在 Bison(或 byacc,如果您想让您的生活至少更轻松一点)方面,您只会看到标记更改为“BATCH_ECHO”之类的内容。
要在 Flex 内部处理它,您可以使用其启动条件功能:
以
开头的模式只能在“batchblock”状态下匹配,该状态由输入开始(批处理块);
。As it stands, specifically, if your special block is consistently signaled by 'batchblock {', this can be handled entirely inside of flex -- on the Bison (or byacc, if you want to make your life at least a little easier) side, you'd just see tokens that changed to something like 'BATCH_ECHO'.
To handle it inside of flex, you'd use its start conditions capability:
The patterns that start with
<batchblock>
can only match in the "batchblock" state, which is entered by theBEGIN(batchblock);
.