由买买提看人间百态

topics

全部话题 - 话题: cookcc
(共0页)
c*****t
发帖数: 1879
1
I want to introduce a new way of dealing with file pattern matching.
This is using a project called CookCC which I started.
Basically, instead of writing complicated lexer patterns using Pattern
for non-trivial text files, it is actually easier to write a lexer/parser
using CookCC. However, the traditional way of writing lexer/parser
(such as using Antlr / JavaCC) is that you are writing code in a
proprietary file format that doesn't have good Java editing.
So here is how CookCC does it. I am
c*****t
发帖数: 1879
2
你要是一点基础都没有,这没谁有能力帮你。
你可以考虑用 StreamTokenizer 来找出 identifier, + operator 等。
然后自己搞定。因为你的例子里面的 identifier 前面都有 : ,可能
稍微要改动些。
另外,你可以用 lex / yacc (再看我的 cookcc 例子)。不过这个没兴趣
的话跟天书一样,其实非常简单。
http://epaperpress.com/lexandyacc/ lex yacc tutorial for C
http://code.google.com/p/cookcc/ cookcc for java
即使你这次不想用,学会 lex / parse 以后受益无穷。
c*****t
发帖数: 1879
3
来自主题: Java版 - math expression
No need to use antlr. My CookCC is smaller, faster and better :)
In fact, operator precedence is the worst nightmare for LL parsers
such as antlr.
CookCC even comes with a calculator example (well, a mini script
interpreter of a few hundred lines) that you just need to do minor
modifcations:
http://code.google.com/p/cookcc/source/browse/trunk/tests/javaap/calc/Calculator.java
c*****t
发帖数: 1879
4
来自主题: Java版 - Design thought.. Sugguestions?
This is a long post...
I am trying to update CookCC (a lexer / parser generator in Java) for
a new feature.
One existing CookCC feature is that one can specify the lexer pattern
and parser grammar in Java annotations. For example:
@CookCCOption
class MyParser extends GeneratedParser
{
@Lex (pattern = "[_A-Za-z][_A-Za-z0-9]*", state = "INITIAL")
String scanIdentifier ()
{
return yyText ();
}
@Rules (rules = {
@Rule (lhs = "Expr", rhs = "Expr '+' Expr"),
c*****t
发帖数: 1879
5
AST is nothing. You can easily create one by hand.
For example, for the following simple calculator code:
print 2 * 3 + 5 * 7;
x = 1;
while (x < 10) {
if (x < 5)
print x * x;
else
print x + x;
x = x + 1;
}
The entire code for it, include AST classes, interpreter, lexer + parser
in CookCC (which generates Java) is 310 lines.
https://code.google.com/p/cookcc/source/browse/trunk/tests/java/parser/calc/
calc.xcc
Like I said, it is trivial if y... 阅读全帖
c*****t
发帖数: 1879
6
来自主题: Java版 - interesting
See this example:
http://code.google.com/p/cookcc/source/browse/#svn/trunk/tests/javaap/nestedclass
And look at WC1.java and WC1$Lexer.java.orig (added .orig for backup
purpose). This WC1$Lexer.java would be replaced by code generated
from a compiler-compiler.
It is not a good approach in general I agree, but I do see that it
makes it possible to organize files.
c*****t
发帖数: 1879
7
来自主题: Java版 - 请教:parse CSV文件
The presence of \, might indicate that \\ or other escape sequences.
Couple with error detection, it is a pain to write a perfect parser
by hand.
(btw, I think the standard CSV uses double quote for literals. It
is a bit strange to use \,)
Java's regular expression is not particularly efficient.
The best way is to use a lex tool (like my CookCC which is for Java)
to deal with this situation.
c*****t
发帖数: 1879
8
来自主题: Java版 - 请教大牛们一个问题
Trivial.
Learn to use lex / yacc. Then you can use CookCC easily write one
under 10 minutes.
m******t
发帖数: 2416
9
来自主题: Java版 - interesting "protect" behavior
You know, now I think you are just talking about these tricky situations as
a shameless plug for your CookCC. ;-)
Seriously though, I understand B is not under your control, but I don't
understand how, once you have fixed a callback method in A, B could change
it. Perhaps some example would help.
c*****t
发帖数: 1879
10
来自主题: Programming版 - any lexer/parser enthusiasts here?
Ha, good.
I created a new project called CookCC (hosted on google code) which
contains both lexer and parser generator (lalr (1) only). It's
written in Java. It basically replaces my yoolex and yooparse projects
I wrote N years ago, since Java is much easier to maintain and extend.
Right now I am working on the Java code generation using FreeMarker
template. I am wondering if anyone wants to join me on this project
to work on code generation for other languages, or assist in testing.
The perf
c*****t
发帖数: 1879
11
来自主题: Programming版 - any lexer/parser enthusiasts here?
所有在这里的 test case 都是测试过的(不过 parser 部分还在 svn 里,
我现在还差将 compressed table 放进 code generator)。
http://code.google.com/p/cookcc/source/browse/trunk/tests/
code generator 现在只有 Java 。其实弄 C/C++ 也不是太难(毕竟是
template approach)。
现在用的是 xml 输入。主要是 yacc/lex 的输入文件其实很复杂。手工写
parser 太困难。至于自动 generate,俺不正在写嘛。
至于 mix 代码和 grammar 。我有个想法,不适合 C/C++,但是很适合
Java / C# / Python 。这样可以直接利用现成的 Java/C#/Python editor
(同时该 editor 的功能,比如 refactoring,context sensitive help
等)。我已经手工推出个 prototype,就差 implementation 。
(共0页)