NAME ^

TGE - A tree grammar engine.

SYNOPSIS ^

    # define a grammar leaf.g
    Leaf:   min(.) = { 
        $P1 = getattribute node, "value"
        .return ($P1)
    }
    ...
    # and elsewhere...

    .sub _main :main
        load_bytecode 'TGE.pir'

        # Compile a grammar from the source grammar file
        .local pmc grammar
        grammar = new 'TGE'
        grammar.agcompile(source)

        .local pmc tree
        # ... the tree to be manipulated

        # Apply the grammar to the tree
        .local pmc TGI
        TGI = grammar.apply(tree)

        # Return the result of a particular rule on a particular tree
        .local pmc result
        result = TGI.get('min')
        # ...
    .end

DESCRIPTION ^

TGE is a tool for transforming trees. Think of it as a good old-fashioned substitution, but instead of taking and returning strings, it takes and returns trees.

TGE is most heavily used in the compiler tools suite, where it transforms the trees output by PGE into abstract syntax trees.

TGE has both a procedural interface and a syntax interface. The syntax interface is easiest for humans to use when constructing grammars by hand. The procedural interface is preferable for computer generated grammars.

This is the syntax for tree grammar rules:

    type: name(parent) = {
        # action
    }

The type is the type of node this particular rule applies to. The name is the name of the attribute the rule defines a value for. The parent says which node the attribute applies to: '.' if the attribute applies to the current node (generally synthesized attributes), '.childname' if the attribute applies to a child of the current node (generally inherited attributes). The action is a block of PIR code run to get the value of the attribute. Within the block, two parameters are supplied for you: node is the current node considered, and tree is the top-level node for the entire tree.

new ^

Create a new grammar object. [Not implemented: Optionally pass it a grammar specification in a string.] The grammar object holds a an array of TGE::Rule objects, which are the semantics defined by the grammar.

agrule ^

Add a rule to the current attribute grammar.

rule_install ^

Compile and install a rule in the current grammar.

rule_emit ^

Generate the PIR code for a rule, and emit it to a file.

agcompile ^

Compile a grammar from a source string.

apply ^

Use a precompiled grammar on a data structure. This returns an object on which you can call methods to fetch attributes on the top node of the data structure.

dump ^

Produce a data dump of the current contents of the grammar object.

AUTHOR ^

Allison Randal <allison@perl.org>


parrot