Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse code to build a compiler in Java?

I need to write a compiler. It's homework at the univ. The teacher told us that we can use any API we want to do the parsing of the code, as long as it is a good one. That way we can focus more on the JVM we will generate.

So yes, I'll write a compiler in Java to generate Java.

Do you know any good API for this? Should I use regex? I normally write my own parsers by hand, though it is not advisable in this scenario.

Any help would be appreciated.

like image 594
fmsf Avatar asked Mar 23 '09 08:03

fmsf


1 Answers

Regex is good to use in a compiler, but only for recognizing tokens (i.e. no recursive structures).

The classic way of writing a compiler is having a lexical analyzer for recognizing tokens, a syntax analyzer for recognizing structure, a semantic analyzer for recognizing meaning, an intermediate code generator, an optimizer, and last a target code generator. Any of those steps can be merged, or skipped entirely, if makes the compiler easier to write.

There have been many tools developed to help with this process. For Java, you can look at

  • ANTLR - http://www.antlr.org/
  • Coco/R - http://ssw.jku.at/Coco/
  • JavaCC - https://javacc.dev.java.net/
  • SableCC - http://sablecc.org/
like image 191
Markus Jarderot Avatar answered Sep 19 '22 16:09

Markus Jarderot