Friday, May 25, 2012

Compiler Construction and JavaCC

Scanning and Parsing are two important phases of compilation process.While it is possible to code a scanner and a parser all by hand, it becomes tedios, specially when the grammar of the language gets more complex. There are Lexer and Parser generators so that we can generate them from a specified grammer.

JavaCC is one such compiler generator. Here are key points about JavaCC.
  • It can generate both scanner and parser.
  • TopDown parsing: generate the parse tree in a top-down manner.
  • It generates Recursive Descent parsers. The parser will execute a set of recursive methods to process the input (not based on parse tables)
  • Predictive (non-backtracking). It looks ahead a number of symbols to decide which grammar rule to use.
  • Supports LL(K) grammars. That means there must not be left recursion, and the grammar should be left refactored.
  • Detects left-recursion, possible ambiguities and warns during compiler generation.
  • Can use different look-ahead values in different local sections
  • Can use syntactic look-ahead. Hence can be considered as a LL(*) parser generator.
  • Gammar can be provided in EBNF notation. Removing left recursion is easy and the grammar is more readable.
  • Provides debugging support: tokenizing / parsing / looking ahead.
  • Supports panic-mode error recovery.
  • Supports Unicode.
  • Lots of good documentation / resources to follow.
  • Good tooling support, including an Eclipse plugin, IntelliJ IDEA plugin and a Maven plugin.
Having unicode support means you can build your compiler using any (natural) language you prefer! You'll only have to change the (programming) language keywords for the new (natural) language. You can also change the (context-free) grammar a bit if you want to make your (programming) language closer to the new (natural) language.

Here is my attempt to create a compiler in my own language, සිංහල (Sinhala). This uses a simplified version of c grammer.
https://github.com/amilamanoj/CMinus/



2 comments:

Navi said...

Good One...
we also going to do a compiler construction as our Btech Final year project...

Thanks for the post :)

swaggy said...

Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I'll be sure to check back again real soon.
http://heavyrentals.equipment