[antlr-interest] Dynamic scopes, pANTLR3_COMMON_TOKEN, and the C runtime
Gary R. Van Sickle
g.r.vansickle at att.net
Sat Jan 3 06:06:01 PST 2009
Ok, so to answer my own question, it looks like this is one way to do it:
/// \name Tree Expressions
/// Parses tree structures with the following format:
///
/// PrimaryDeliverable
/// (
/// Firmware
/// (
/// Drivers
/// (
/// Driver_EEPROM
/// Driver_LCD
/// )
/// App
/// GUI
/// )
/// Hardware
/// );
///
/// ...and outputs an AST with the tree flattened into a simple list of
parent->child associations:
///
/// (ASSOCIATION PrimaryDeliverable Firmware)
/// (ASSOCIATION Firmware Drivers)
/// (ASSOCIATION Drivers Driver_EEPROM)
/// (ASSOCIATION Drivers Driver_LCD)
/// (ASSOCIATION Firmware App)
/// (ASSOCIATION Firmware GUI)
/// (ASSOCIATION PrimaryDeliverable Hardware))
///
//@{
tree_expression returns [ pANTLR3_BASE_TREE root_identifier ]
scope
{
// A place to save a pointer to the root IDENTIFIER of the tree or
subtree, so we can
// use it further down the rule chain.
pANTLR3_BASE_TREE root;
}
: IDENTIFIER
{
// Save the IDENTIFIER node in our dynamic scope
// so that child rules can find out who their parent is.
$tree_expression::root =
(pANTLR3_BASE_TREE)(ADAPTOR->create(ADAPTOR, $IDENTIFIER));
// Also return the IDENTIFIER node we just created, for the
"tree_expression tree_expression" case
// below in the "association" rule.
$root_identifier = (pANTLR3_BASE_TREE)(ADAPTOR->create(ADAPTOR,
$IDENTIFIER));
} '(' association_list ')' -> association_list
;
association_list
: association+
;
association
// This is to catch the "tree_expression tree_expression" case.
: te=tree_expression -> ^(ASSOCIATION {$tree_expression::root}
{$te.root_identifier}) tree_expression
// This handles everything else.
| IDENTIFIER -> ^(ASSOCIATION {$tree_expression::root} IDENTIFIER)
;
//@}
Are there better or more succinct ways to do what I'm trying to do here?
--
Gary R. Van Sickle
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org
> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Gary
> R. Van Sickle
> Sent: Saturday, January 03, 2009 4:24 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Dynamic scopes,
> pANTLR3_COMMON_TOKEN,and the C runtime
>
> Why does this segfault on me, and what's the proper way to do
> this sort of
> thing?:
>
>
> /// \name Tree Expressions
> //@{
>
> tree_expression
> scope
> {
> // A place to save a pointer to the root identifier of the
> tree or subtree, so we can
> // use it further down the rule chain.
> pANTLR3_COMMON_TOKEN root;
> }
> : IDENTIFIER { $tree_expression::root = $IDENTIFIER; } '('
> association_list ')'
> ;
>
> association_list
> : association*
> ;
>
> association
> : tree_expression
> | IDENTIFIER -> ^(ASSOCIATION {$tree_expression::root} IDENTIFIER)
> ;
>
> //@}
>
>
> I have an analogous construct in a different part of my
> grammar which does this same thing with a pANTLR3_BASE_TREE,
> with no observed ill effect.
>
> --
> Gary R. Van Sickle
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email
> -address
More information about the antlr-interest
mailing list