Automating Abstract Syntax Tree construction for Context Free Grammars

In most of the compilers or programming languages tools, parsers are used for transforming human readable code into Parse Trees or Abstract Syntax Trees (AST). A popular method to create parsers is to use a parser generator. Advanced parser generators (e.g. ANTLR, SDF) are able to generate directly ASTs if the grammar is annotated with AST generation rules. These annotations are typically done manually, by adding to each grammar rule a constructor and associating an AST component to each constructor. This might be inconvenient for people with little experience in writing grammars or for those who already have a grammar for their language.

 

In this paper, we present a generic method for inferring such AST generation rules and a tool which automatically generates the annotated grammar. Assuming you have a grammar for a language, some input programs and their corresponding ASTs, the tool will infer the rules for constructing the AST. If the input programs cover the whole range of the language syntax constructs then the parser corresponding to the generated annotated grammar is able to parse and transform into an AST any program of the given language.

Sidebar