1 样板:simples3.jj
options
{
DEBUG_PARSER=true; //加上这个系统参数是为了可以显示文法推导的过程
}
PARSER_BEGIN(Simple3)
public class Simple3 {
public static void main(String args[]) throws ParseException {
Simple3 parser = new Simple3(System.in);//文字输入
//下面这句就是对文件的直接读取。
// parser = new TelParser(new java.io.FileInputStream(args[0]));
parser.Input();
}
}
PARSER_END(Simple3)
SKIP :
{
" "
| "/t"
| "/n"
| "/r"
}//忽略的字符
TOKEN :
{
<LBRACE: "{">
| <RBRACE: "}">
}//记号,作为分析的记号
void Input() :
{ int count; }//函数的处世变量
{
count=MatchedBraces()<EOF>//<EOF>代表不断循环,直到处错为止
{ System.out.println("The levels of nesting is " + count); }
}
int MatchedBraces() :
{ int nested_count=0; }
{
<LBRACE> [ nested_count=MatchedBraces() ] <RBRACE>
{ return ++nested_count; }
}