antlr的功能在我的其他文章里提到了就不多说了,jfreechart是一个功能强大的java开源图表生成组件。
是不是经常有人问你,你做的项目一共有多少行代码,你编写了多少行代码?
本文的程序轻松帮你回答这个问题。
我越来越喜欢用antlr来完成分析问题,虽然它在性能和简易性方面不如lex/yacc相比,那它能很容易的结合到java项目里。
1 计算文件行数,和空行行数
//----------------------------------------------------------------------------
// the colimas source statistics scanner
//----------------------------------------------------------------------------
header{
package org.colimas.src.parser;
}
class sourcestatisticsparser extends parser;
options {
k = 2; // two token lookahead
codegenmakeswitchthreshold = 2; // some optimizations
codegenbitsettestthreshold = 3;
defaulterrorhandler = false;
}
file : //遍历文件
(code)+ file //有文字多行
| (empty)+ file //无文字多行
| code //文件最后一样
| eof //文件结束符
;
class sourcestatisticslexer extends lexer;
options {
testliterals=true; // test for literals
k=2; // 2 characters of lookahead
codegenbitsettestthreshold=20;
charvocabulary='/u0003'..'/uffff';
}
{
private long codeline=0; //有字符行数
private long emptyline=0; //空行行数
private long totalline=0; //文件行数=有字符行数+空行行数
public long getcodeline(){
return this.codeline;
}
public long getemptyline(){
return this.emptyline;
}
public long gettotalline(){
return this.codeline+this.emptyline;
}
}
char_literal
: ~('/uffff' | '/r' |'/n') //结束符,回车符,换行符以外的所有字符
;
char : (char_literal)+; //多个字符
empty
: ('/r')? ('/n') // dos/windows
// increment the line count in the scanner
{
newline(); //用于调试
emptyline++; //空行加1
$settype(token.skip);
}
;
code : char ( ((
闽公网安备 35060202000074号