简体   繁体   中英

What is the best way performance wise to create an object while parsing in ANTLR4?

I'm parsing a message and creating an custom object Message while doing the parsing. I was wondering Performance wise, is it better to create the object in the parser actions or elsewhere?

for now I'm overriding the exit rule methods.

parser.addParseListener(new MessageGrammarBaseListener(){
            MessageType message = null;

            @Override
            public void exitStartofMessage(StartofMessageContext ctx) {
                message = new MessageType();
            }

            @Override
                public void exitPersonalInformation( PersonalInformationContext ctx) {
                    Person p = new Person();
                    p.setName(ctx.name.getText());
                    message.setPersonalInformation(p);

                    }
(...)
});

is it better to do it this way or in the parser action ?

It's a good practice to separate parsing process and AST bypass, because of parsing can be implemented for different runtimes. Also these actions are different logical steps. So I think you do everything correctly.

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