Socratic Phoenix
Socratic Phoenix

Reputation: 556

Custom AST in JavaCC?

I've recently begun designing my first programming language, and being already fluent in java, I've started to build the AST API for my language in java. I plan on compiling to java byte-code, and that is functional and working in the current part of the AST that I've implemented. I've tried a couple different methods of parsing (that all failed) before stumbling upon parser-generators, in particular, JavaCC. I've done some basic research into JavaCC and EBNF, and was wondering if JavaCC could support a fully custom AST API (including constructor arguments and the like) while parsing my language. I wanted to ask this here before doing an deep research and watching/reading tutorials on JavaCC. From what I've seen, JavaCC can support AST's, but I'm not sure what the constraints are. Moreover, I know JavaCC has its own AST API, but I'd like to stick to the one I've developed already, as it is working and matches my language well.

Upvotes: 2

Views: 1763

Answers (1)

Theodore Norvell
Theodore Norvell

Reputation: 16221

Absolutely. For example you can write nonterminals like this

CommandNode whileCommand() : {
    ExpressionNode e ;
    CommandNode doPart ;
} {
    <WHILE> e = expression() <DO>
    doPart = sequence() <ENDWHILE>
    { return new WhileCommand( e, doPart ) ; }
}

The builder pattern can be useful to isolate the parser from some of the details of the AST.

Upvotes: 4

Related Questions