【发布时间】:2013-08-12 01:57:05
【问题描述】:
所以我正在编写一个python解析器,我需要根据python grammar specification动态生成INDENT和DEDENT标记(因为python不使用显式分隔符)。
基本上我有一堆表示缩进级别的整数。在INDENT 标记中的嵌入式Java 操作中,我检查当前缩进级别是否高于堆栈顶部的级别;如果是,我推它;如果没有,我打电话给skip()。
问题是,如果当前缩进级别与堆栈中的多个级别匹配,我必须生成多个 DEDENT 标记,我不知道该怎么做。
我当前的代码:(注意within_indent_block和current_indent_level在别处管理)
fragment DENT: {within_indent_block}? (SPACE|TAB)+;
INDENT: {within_indent_block}? DENT
{if(current_indent_level > whitespace_stack.peek().intValue()){
whitespace_stack.push(new Integer(current_indent_level));
within_indent_block = false;
}else{
skip();
}
}
;
DEDENT: {within_indent_block}? DENT
{if(current_indent_level < whitespace_stack.peek().intValue()){
while(current_indent_level < whitespace_stack.peek().intValue()){
whitespace_stack.pop();
<<injectDedentToken()>>; //how do I do this
}
}else{
skip();
}
}
;
我该怎么做和/或有更好的方法?
【问题讨论】: