[antlr-interest] advice on creating a grammar for a given DSL example

aryoo howaryoo at gmail.com
Mon Aug 16 10:33:44 PDT 2010


Hello,

I am new to ANTLR, I own the books and was able to reproduce the
hello-world and the book examples (Python style) successfully.

What I would love to do now is to
1) create my own grammar that could parse the file below.
2) generate some Python code that corresponds to the 'objects' and
some XML code that corresponds to the 'views'


My first attempt for part 1 is summarized below (grammar + error messages).

Any advice on what I did wrong or similar examples would be greatly appreciated.

Regards,
Arye.



****************************************example of file to parse********begin
module {
    name:"name_of_module"
    version:1.0
}

object {
    module:"name_of_module"
    name:"name_of_object1"
    column {
        name:"name_of_column1"
        type:"type_of_col1"
    }
    column {
        name:"name_of_column2"
        type:"type_of_col2"
    }
}


view {
    object:"name_of_object1"
    name:"name_of_view"
    type:"type_of_view"
    field {
        column:"name_of_column1"
    }
    field {
        column:"name_of_column2"
    }
}
****************************************example of file to parse********end





*******************************grammar*************begin
grammar MyGrammar;


tokens {
	MODULE 	= 'module' ;
	OBJECT	= 'object' ;
	VIEW	= 'view' ;
	MENU_ENTRY= 'menu_entry' ;
}

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

prog	: ( stat {print $stat.text} )+ ;


stat	:	module NEWLINE
    |	object NEWLINE
    |	view NEWLINE
    |	NEWLINE
    ;

module	: MODULE '{' module_expr+ '}';

module_expr :   'name' ':' ID NEWLINE
    |   'version' ':' ANY NEWLINE
    ;


object	: OBJECT '{' object_expr+ '}';

object_expr :   'module' ':' ID NEWLINE
    |   'name' ':' ID NEWLINE
    ;

view	: VIEW '{' view_expr+ '}';

view_expr :   'object' ':' ID NEWLINE
    |   'name' ':' ID NEWLINE
    ;



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

//NUMBER	: (DIGIT)+ ;

//WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ 	{ $channel = HIDDEN; } ;

//fragment DIGIT	: '0'..'9' ;


ID	:	('a'..'z'|'A'..'Z')+ ;

INT	:	'0'..'9'+ ;

NEWLINE	:	'\r'? '\n' ;

//WS	:	(' '|'\t'|'\n'|'\r')+ {self.skip()} ;
WS : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ 	{ $channel = HIDDEN; } ;

ANY	:	('a'..'z'|'A'..'Z'|'0'..'9'|'.'|','|'_')+ ;
*******************************grammar*************begin






*******************************output*************begin
line 3:4 missing NEWLINE at u'version'
line 6:0 missing NEWLINE at u'object'
module {
    name:nameofmodule
    version:1.0
}
line 8:4 missing NEWLINE at u'name'
line 8:9 extraneous input u'nameofobject1' expecting ID
line 10:13 extraneous input u'nameofcolumn1' expecting ID
line 13:4 mismatched input u'column' expecting NEWLINE
object {
    module:nameofmodule
    name:nameofobject1
    column {
        name:nameofcolumn1
        type:typeofcol1
    }
    column {
        name:nameofcolumn2
        type:typeofcol2
    }
}


view {
    object:nameofobject1
    name:nameofview
    type:typeofview
    field {
        column:nameofcolumn1
    }
    field {
        column:nameofcolumn2
    }
}


*******************************output*************end


More information about the antlr-interest mailing list