gpt4 book ai didi

c - 如何在 Ragel 中实现前瞻

转载 作者:太空宇宙 更新时间:2023-11-04 02:59:31 25 4
gpt4 key购买 nike

我有两个状态;一个是另一个更一般的状态的特定实例。我相信避免同时进入两种状态的正确方法是使用 k>1 实现前瞻,但我找不到任何示例说明如何执行此操作。

Ragle 用户指南说:

In both the use of fhold and fexec the user must be cautious of combining the resulting machine with another in such a way that the transition on which the current position is adjusted is not combined with a transition from the other machine.

我不完全确定这意味着什么,除了“不要尝试阅读当前表达式的末尾”。

我的机器是这样的:

seglen16 = any{2} >{ swab(p, &len, 2); len = len - 2; };            
action check {len--}
buffer = (any when check)* %when !check @{ printf("[%d]:%d\n", len, *p); };

# JPEG Markers
mk_app0 = 0xFF 0xE0;
mk_appx = 0xFF (0xE0..0xEF);
marker = 0xFF ^0x00;
nonmarker = !marker - zlen;

# JPEG APP Segments
seg_app0_jfif = mk_app0 seglen16 "JFIF" 0x00 buffer @{ printf("jfif app0\n"); };
seg_appx_unk = mk_appx nonmarker* @{ printf("unknown app content\n"); };
seg_app = (seg_app0_jfif | seg_app1_exif | seg_appx_unk);

# Main Machine
expr = (mk_soi @lerr(bad) nonmarker* seg_app* nonmarker* mk_eoi);

我想标记 JPEG header ,跳过未知段并处理 JFIF 等知名段。 JPEG 应用程序段 app0 以 0xFFE0 开头。如果 app0 包含 JFIF 数据,则 app0 标记后面将跟一个两字节长度和字符串“JFIF\0”。这意味着在识别应用程序段时我需要 7 个字节的先行。

最佳答案

I want to tokenize a JPEG header, skipping unknown segments and handling well-known segments like JFIF. The JPEG application segment app0 starts with 0xFFE0. If app0 contains JFIF data, the app0 marker will be followed by a two-byte length and the string "JFIF\0".

好的。

This means I need 7 bytes of lookahead when identifying application segments.

为什么?您可以将“未知”模式应用到所有片段除了那些使用一般模式已知的片段:

seg_app0_jfif = mk_app0 seglen16 "JFIF" 0x00 buffer @{ printf("jfif app0\n"); };
known_segment = (seg_app0_jfif | seg_app1_exif);
unknown_segment = ((mk_appx nonmarker*) - known_segment) @{ printf("unknown app content\n"); };
seg_app = (known_segment | unknown_segment);

这样做不需要先行。 Ragel 生成适当的状态和转换,同时处理这两种模式,直到处理了足够多的输入以消除它们的歧义。只有当 unknown_segment 不是 known_segment 时,才会发生对 unknown_segment 的结束操作,这看起来像是您要实现的行为。

关于c - 如何在 Ragel 中实现前瞻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13653335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com