Category Archives: JavaScript

JavaCC

I learned to use JavaCC these days. And I found that JavaCC is a great toolkit.

What is JavaCC?

JavaCC is short for Java Compiler Compiler. It is a parser or scanner generator for Java.

Java Compiler Compiler [tm] (JavaCC [tm]) is the most popular parser generator for use with Java [tm] applications. A parser generator is a tool that reads a grammar specification and converts it to a Java program that can recognize matches to the grammar. In addition to the parser generator itself, JavaCC provides other standard capabilities related to parser generation such as tree building (via a tool called JJTree included with JavaCC), actions, debugging, etc.

You can find a lot of language grammar and AST on its site too. I see that C++ and Java 1.5 grammar are there. JavaScript isn’t there. But I find out that Dojo toolkit’s JSLinker provides an ECMAScript 262 one. That is to say, you can create parser and parse C++, Java or JavaScript sources into your AST for any uses. Lots of great features can be imagined.

I spent about a week learning and using JavaCC. I completed a parser for an IDL language named CAR. I also built an Eclipse editor using the generated AST parser. Bang, all things seems working well: keyword hight-lighting, outline view, error probing, … Great toolkit!

Using JavaCC is quite simple, just download javacc-4.0.zip and unzip it, then following its examples tutorial. I sugguest you to start with JavaGrammars and VTransformer examples, as they are about Java grammar.

I think, later, given opportunity, I will try JavaCC out on other projects. Maybe my Java2Script compiler would be benefited from JavaCC.

P.S. Update:
There is a JavaCC Eclipse Plugin project, you can download the plugin to help you writing your syntax file.

Posted in C++, Java, JavaScript | Tagged , | 2 Comments

Challenging Myself: Find 1st k-Digits Prime Number

Here is a task to challenge myself: Find the first k digits prime number. And k would be 16, 32, 64, 256, 1024, 8096, 65536, or even bigger.

And the most challenging thing would be most of key calculations will be performed in JavaScript engine hosted inside browsers. And a distributed algorithm will be designed. Algorithm which is similar to Google’s Map/Reduce will be designed. And Apache Hadoop’s Map/Reduce is also in references.

Any comments about this challenge? Or join me to design the algorithm! Or join the distributed computing game.

Keep tuned for how big the number k would be!

PS: After searching around Internet for existed prime number theories, I find out that k=16 is already a challenge for me! There are algorithms to find prime number in format of 2^p+1, whose digits may be bigger than 1000000. But there are not the 1st k-digits algorithms. According to theories, I should have to find out all prime numbers less than given n. But the count of prime numbers less than n is about n / ln (n), which is huge number already for k=16! So my first challenge would be 16.

Posted in AJAX, Challenge, Java, JavaScript | Comments Off on Challenging Myself: Find 1st k-Digits Prime Number

Why JavaScript’s Date#getMonth Start From Zero?

Today, one of my colleague asked me a question about JavaScript’s document.lastModified, here was my answer for her:

javascript:dd=new Date(document.lastModified);alert(dd.getFullYear() + “.” + (dd.getMonth() + 1) + “.” + dd.getDate() + “//” + dd.getHours() + “:” + dd.getMinutes() + “:” + dd.getSeconds());

Then she asked me why #getMonth should plus one why others need not. I knew that from the ECMAScript specification, Date#getMonth start from 0 to 11, representing January to December, but I couldn’t answer her question, cause I couldn’t figure out why only #getMonth needs such a hacks which confuse a lot of developers.

After searching a lot on Internet, I got nothing and gave up to found out why. Maybe those specification designers were C or C++ developers who always thought things should start from zero.

Posted in JavaScript, Tricks | Comments Off on Why JavaScript’s Date#getMonth Start From Zero?