31

I'm working on a compiler design project in Java. Lexical analysis is done (using jflex) and I'm wondering which yacc-like tool would be best(most efficient, easiest to use, etc.) for doing syntactical analysis and why.

vuzun
  • 942
  • 2
  • 8
  • 15

5 Answers5

23

If you specifically want YACC-like behavior (table-driven), the only one I know is CUP.

In the Java world, it seems that more people lean toward recursive descent parsers like ANTLR or JavaCC.

And efficiency is seldom a reason to pick a parser generator.

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
kdgregory
  • 38,754
  • 10
  • 77
  • 102
  • 1
    Difficulty: CUP is no longer maintained and [CUP 2](http://www2.in.tum.de/cup2) is beta. Is it still OK? – minopret Dec 27 '12 at 16:20
  • @minopret - I haven't used CUP in over a dozen years, but unless there was a major unfixed bug, then it should be OK even if not actively maintained. However, unless you have a need for YACC-like behavior, I'd look at ANTLR. – kdgregory Dec 29 '12 at 23:30
  • I am working on a GitHub markdown file (.md) creator that takes a code file, written in any language, and generates a .md file for it. I'm doing this in Java. Can these softwares handle *any* language or just Java? – Mike Warren Feb 11 '17 at 00:02
  • 1
    @MikeWarren - parser generators can create a parser for any language that has a regular grammar. This includes most programming languages. However, creating a grammar from scratch is a non-trivial task, especially if you're not familiar with it. I would look for pre-written grammars for the various tools, and pick the tool that (1) has output that you like, an (2) has the most grammars that you can adapt. – kdgregory Feb 12 '17 at 12:11
5

In the past, I've used ANLTR for both lexer and parser, and the JFlex homepage says it can interoperate with ANTLR. I wouldn't say that ANTLR's online documentation is that great. I ended up investing in 'The Definitive ANTLR reference', which helped considerably.

toolkit
  • 49,809
  • 17
  • 109
  • 135
5

GNU Bison has a Java interface,

http://www.gnu.org/software/bison/manual/html_node/Java-Bison-Interface.html

You can use it go generate Java code.

ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
3

There is also jacc.

Jacc is about as close to yacc as you can get, but it is implemented in pure java and generates a java parser.

It interfaces well with jFlex

http://web.cecs.pdx.edu/~mpj/jacc/

CaTalyst.X
  • 1,645
  • 13
  • 16
2

Another option would be the GOLD Parser.

Unlike many of the alternatives, the GOLD parser generates the parsing tables from the grammar and places them in a binary, non-executable file. Each supported language then has an engine which reads the binary tables and parses your source file.

I've not used the Java implementation specifically, but have used the Delphi engine with fairly good results.

Steve N
  • 1,371
  • 1
  • 12
  • 18