Flex 和 Bison 的词汇搭配

发布于 2024-08-08 09:58:28 字数 447 浏览 5 评论 0 原文

我一直在寻找一种不适合一般 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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪痕残 2024-08-15 09:58:28

我很困惑如何根据上下文提供不同的 Flex 正则表达式

Flex 有一个状态机制,您可以通过它在不同的正则表达式集之间切换。其语法位于

%x name_of_state

文件顶部(在 %} 之后)和匹配规则中(在第一个 %% 之后),

<name_of_state> *regex goes here*

则此正则表达式仅在以下情况下匹配在那种状态下。还有一个全局状态 <*> 可用于匹配任何状态中的任何内容。

改变状态的方法不止一种。例如,如果您想保留状态堆栈,则使用 yy_pop_stateyy_push_state。或者,您可以使用BEGIN(name_of_state)。要返回到初始状态,请使用 BEGIN(INITIAL)

I'm confused as to how exactly I can supply different flex regexes depending on the context

Flex has a state mechanism whereby you can switch it between different sets of regexes. The syntax for this is

%x name_of_state

at the top of the file (after the %}) and in your matching rules (after the first %%)

<name_of_state> *regex goes here*

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 and yy_push_state if you want to keep a stack of states. Alternatively you can use BEGIN(name_of_state). To go back to the initial state, use BEGIN(INITIAL).

Saygoodbye 2024-08-15 09:58:28

具体而言,如果您的特殊块始终由“batchblock {”发出信号,则可以完全在 Flex 内部处理 - 在 Bison(或 byacc,如果您想让您的生活至少更轻松一点)方面,您只会看到标记更改为“BATCH_ECHO”之类的内容。

要在 Flex 内部处理它,您可以使用其启动条件功能:

%x batchblock
%%

"batchblock"{ws}\{   { BEGIN(batchblock); }


<batchblock>echo     { return BATCH_ECHO; }
<batchblock>set      { return BATCH_SET;  }
/* ... */
<batchblock>\}       { begin(INITIAL);    }

开头的模式只能在“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:

%x batchblock
%%

"batchblock"{ws}\{   { BEGIN(batchblock); }


<batchblock>echo     { return BATCH_ECHO; }
<batchblock>set      { return BATCH_SET;  }
/* ... */
<batchblock>\}       { begin(INITIAL);    }

The patterns that start with <batchblock> can only match in the "batchblock" state, which is entered by the BEGIN(batchblock);.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文