我正在为包含特殊预处理器部分中的预处理器指令的语言(由{
和}
包含)构建解析器。其中之一与C
#define
类似。如何在两个单独的词法分析模式中使用词法分析器规则?
我想在一次运行中使用针对预处理器部件的孤岛语法来分析文件。 当我点击#define
指令时,我想包含另一个孤岛语法,其中包含“常规”部分的所有标记(大约200个),除了预处理区域开始标记并在不同通道上发出标记以及当然有一个停止令牌返回到预处理器岛语法。由于我解析的文件是有效的,所以预处理器区域起始令牌{
真的被删除并不是非常重要,但会很好。
有没有办法为两种模式“重复使用”词法分析器规则(我可以发射到命名的非const通道,该值可以在进入/离开岛时改变)?
这是一些示例源文件:
int a = 42;
{ // start preprocessor section
// simple single line #define
#define ABC 42
// will be fix "2 * 42" even if ABS is changed later on
#define DEF 2 * ABC
// multiple line define (all but last line needs to have a "\" before the newline
#define GHI 3 \
+ 4
// the definition can contain (almost) arbitrary code, except line comments, preprocessor sections and preprocessor statements
#define JKL if (a > 23) then b = c + d; str = "} <- this must not be the end of the preprocessor section"; end_if;
} // end preprocessor section
这就是我对“少数”规则的做法。 200条规则的做法是什么? – Onur
@Onur以这种形式创建原始规则的200份副本。 –
我采取了你的方法,虽然我不手动做样板代码(请参阅我的答案)。 – Onur