Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reduce my parse tree into an abstract syntax tree?

What are the general strategies for reducing a parse tree (ie. concrete syntax tree) into an abstract syntax tree?

For example, I have the following grammar rule:

statement_list : statement
               | statement_list statement

which, if left as a parse tree, will generate fanning output that looks like

program
        statement_list
                statement_list
                        statement
                                definition
                                        p_type
                                        assignment
                statement
                        definition
        statement
                assign
                        assignment

If I concatenate the children of each node (since a statement list has no inherent meaning after parsing), I can achieve the following

program
        definition
                p_type
                assignment
        definition
        assign
                assignment

This worked well - however, I'm unaware of any "rules" for doing this. Are there specific grammar rules I should be looking to simplify? Is it a matter of feel, or is there a more mechanistic process?

like image 940
sdasdadas Avatar asked Jul 30 '13 01:07

sdasdadas


People also ask

How do you create an Abstract Syntax Tree?

Typically, you would split the work into a tokenizer which splits the input stream representing the expression into a list of tokens, and a parser which takes the list of tokens and constructs a parse tree\ast from it. The first column is the actual text value. The second represents the token type.

Is parse tree same as Abstract Syntax Tree?

A parse tree is a record of the rules (and tokens) used to match some input text whereas a syntax tree records the structure of the input and is insensitive to the grammar that produced it. Combining the above two definitions, An Abstract Syntax Tree describes the parse tree logically.

What makes Abstract Syntax Tree better than parse tree?

Abstract syntax trees are important data structures in a compiler. It contains the least unnecessary information. Abstract syntax trees are more compact than a parse tree and can be easily used by a compiler.

What is the role of abstract syntax trees in parsing?

An Abstract Syntax Tree, or AST, is a tree representation of the source code of a computer program that conveys the structure of the source code. Each node in the tree represents a construct occurring in the source code.


1 Answers

It's not a matter of "feel". An abstract syntax tree depends on the meaning (semantics) of what's been parsed, and I think these would be the rules:

  1. Remove nodes for tokens that don't add meaning. Those are intermediate keywords (like "then"), separators (like comma) and brackets (like parenthesis).
  2. Promote meaningful tokens (like "if") to be the parent of other tokens in the same rule.

There's no single recipe. It depends on what the phrases in the target language mean.

like image 120
Apalala Avatar answered Sep 21 '22 07:09

Apalala