简体   繁体   中英

ANTLR or SableCC for DSL Implementation?

Has somebody used both for Language implementation and is able to compare them, pointing out strengths and weaknesses? I seek a RAD tool with support for AST Walker Code generation. SableCC is LALR and thus supports ´Left recursion´, whereas ANTLR is LL(*). Is this important for typical grammars or DSLs? I need to perform some domain-specific analysis as well. (The target language of my compiler will be OpenCL C). As this will be for a student project it is important that I do not lose that much time on the tedious side, that is implementing the Front-End of the language.

I cannot say much about ANTLR, but maybe some information about SableCC.

Design

It generate a parser, which generated code and hand-written code are clean separated using Visitor pattern, and integrates the transform from Concrete Syntax Tree to Abstract Syntax Tree. As a result the designer can get a AST after the parser parses successful the input, and he can walk through the tree and make action on corresponding nodes.

The designer can first write and debug his grammar, try to optimize the transform from Concrete Syntax Tree to Abstract Syntax Tree. After he has a solid AST he can write action code in separated class. So the designer write grammar only once and can write more type of action for the grammar, for example once for Syntax Highlight, once for Semantic analysis and code generator. I have done it in a productive system. It works very well.

With ANTLR the designer can construct the AST tree by adding action code in grammar t generate the AST, then reuses it for different manner. But there a not a clean separation between generated code and hand-written code.

An other aspect maybe support of IDE. Since with SableCC you have separated code, you can easy use auto-complete function of IDE.

grammar

SableCC is a LR(1) parser generator, so it is IMO easier to write grammar for ANTLR, which is a LL(k) parser generator, (without trick). I think (aber not sure) SableCC is the only one LR(1) java parser generator, which is so popular.

output parser

ANTLR can generate parser in many languages, while SableCC can only generate parser in Java (mainstream). There some plugin / adapter to generate parser in other language, however according to the author (http://www.mare.ee/indrek/sablecc/) they are too old. SableCC 4 can generate more, but it is in beta, which is not recommend for serious project.

Development Support

ANTLR hat a IDE to write grammar. It is ANTLRWorks, which can visual grammar, navigate in source (like jump to definition of token or production). SableCC hat no such tools. There are primitive Syntax Highlight script for VIM and a poor feature plugin for Netbeans.

Conclusion

IMO I think for big project, required long term maintenance SableCC is more suitable than ANTLR.

Martin Fowler has a informative about SableCC, you can find it here. http://martinfowler.com/bliki/HelloSablecc.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM