[antlr-interest] Question about grammar trees

Esen Yilmaz esen2007 at gmail.com
Fri Apr 27 12:03:20 PDT 2012


I'd appreciate it very much if you could give me some help on the following
- I'm guessing it's a very simple issue but I can't get to grips with how
to solve it.

I've got a simple grammar (below) and it is meant to process inputs like:

group gA
{
  keyA1 = "1"

group gB
{
  keyB1 = "1"
}

keyA2 = "3"
}

I need to place code (C-code) within the grammar (or the tree grammar for
it I think) that will automatically give me a struct in my actual
application code like:

struct Group
{
  int numKVPs;
  int numGroups;

  KeyValuePair* pKVPList;
  Group* pGroupList;
};

struct MyTree
{
  int numKVPs;
  int numGroups;

  KeyValuePair* pKVPList;
  Group* pGroupList;
};

So that I can access the MyTree struct via something like:

parser->group(parser).tree;

Currently I see that the above member gives me a pANTLR3_BASE_TREE type
which still requires me to do a recursive traversal to get all of the
values and put them together into the structs I need above.

Any help would be greatly appreciated,

---------------
The grammar
---------------

grammar APA;

options
{
    output=AST;
    language=C;
    //ASTLabelType=MYTree; //pANTLR3_BASE_TREE;
}

/****************************************************************************************************
 PARSER RULES
****************************************************************************************************/

settings
:  group?
;

group
: GROUP IDENT BODY_START assignment* group* assignment* BODY_END
;

assignment
: IDENT ASSIGN STRING
;

/*
**************************************************************************************************
 LEXER RULES
****************************************************************************************************/

GROUP
: 'group'
;

BODY_START
: '{';

BODY_END
: '}';

DEFAULTS
: '[DEFAULTS]'
;

SETTINGS
: '[SETTINGS]'
;

ASSIGN
: '='
;

IDENT
: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
        ;

NUMBERS
: ('0'..'9')*
        ;

COMMENT
: '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
     | '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
     ;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;

STRING
     :  '\"' ( ~('\\'|'"') )* '\"'
     ;

fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;

fragment
ESC_SEQ
    :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
    |   UNICODE_ESC
    |   OCTAL_ESC
    ;

fragment
OCTAL_ESC
    :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7')
    ;

fragment
UNICODE_ESC
    :   '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
    ;


More information about the antlr-interest mailing list