【发布时间】:2014-06-12 09:12:34
【问题描述】:
我正在为一种语言构建一个解析器,该语言包含特殊预处理器部分中的预处理器指令(由{ 和} 封闭)。其中之一类似于C#define。
我想对预处理器部分使用孤岛语法在一次运行中对文件进行 lex。
当我点击#define 指令时,我想包含另一个岛语法,其中包含“常规”部分的所有标记(大约 200 个),除了预处理区域开始标记并在不同的通道上发出标记和当然有一个停止标记,它返回到预处理器岛语法。由于我解析的文件是有效的,因此真正删除预处理器区域起始令牌 { 并不是很重要,但会很好。
有没有办法“重用”两种模式的词法分析器规则(我可以向一个命名的非常量通道发出我可以在进入/离开岛时更改的值)?
这是一些示例源文件:
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
【问题讨论】: