[DGD] example grammar

Felix A. Croes felix at dworkin.nl
Sun Jan 4 22:06:23 CET 1998


Below is an example grammar that can be used to test the current
(incomplete) parse_string implementation on LPC source files.  Note that
the grammar does not handle floating point values.

The value returned is an array of token values; any structure in the
grammar is currently ignored.  The maximum number of tokens the function
can currently handle is 10000, so use it with small source files only.

Regards,
Dworkin

/*
 * NAME:        foo
 * DESCRIPTION: bar
 */
mixed *parse(string foo)
{
    return parse_string("\
whitespace = /[ \t\n\r\v\f]+/                   \
whitespace = /\\/\\*([^*]|\\*+[^/*])*\\*\\// "+"\
whitespace = /#[^\n]*\n/                        \
                                                \
number = /[0-9]+/                               \
ident = /[a-zA-Z_][a-zA-Z_0-9]*/                \
char = /'[^\\\\]'/                              \
char = /'\\\\.'/                                \
string = /\"([^\\\\\"]*(\\\\.)*)*\"/         "+"\
                                                \
token: number                                   \
token: ident                                    \
token: char                                     \
token: string                                   \
token: keyword                                  \
token: '{'                                      \
token: '}'                                      \
token: ';'                                      \
token: operator                              "+"\
                                                \
keyword: 'string'                               \
keyword: 'nomask'                               \
keyword: 'do'                                   \
keyword: 'break'                                \
keyword: 'else'                                 \
keyword: 'case'                                 \
keyword: 'object'                               \
keyword: 'default'                              \
keyword: 'static'                               \
keyword: 'continue'                             \
keyword: 'int'                                  \
keyword: 'rlimits'                              \
keyword: 'float'                                \
keyword: 'for'                                  \
keyword: 'inherit'                              \
keyword: 'while'                                \
keyword: 'if'                                   \
keyword: 'catch'                                \
keyword: 'switch'                               \
keyword: 'mapping'                              \
keyword: 'private'                              \
keyword: 'void'                                 \
keyword: 'atomic'                               \
keyword: 'return'                               \
keyword: 'mixed'                                \
keyword: 'varargs'                              \
                                             "+"\
operator: '!='                                  \
operator: '!'                                   \
operator: '~'                                   \
operator: '%='                                  \
operator: '%'                                   \
operator: '&='                                  \
operator: '&'                                   \
operator: '*='                                  \
operator: '*'                                   \
operator: '++'                                  \
operator: '+='                                  \
operator: '+'                                   \
operator: '--'                                  \
operator: '-='                                  \
operator: '-'                                   \
operator: '...'                                 \
operator: '..'                                  \
operator: '/='                                  \
operator: '/'                                   \
operator: ':'                                   \
operator: '::'                                  \
operator: '<<='                                 \
operator: '<<'                                  \
operator: '<='                                  \
operator: '<'                                   \
operator: '=='                                  \
operator: '='                                   \
operator: '>>='                                 \
operator: '>>'                                  \
operator: '>='                                  \
operator: '>'                                   \
operator: '^='                                  \
operator: '^'                                   \
operator: '||'                                  \
operator: '|='                                  \
operator: '|'                                   \
operator: '['                                   \
operator: ']'                                   \
operator: '('                                   \
operator: ')'                                   \
operator: ','                                   \
operator: '?'                                   \
", foo);
}



More information about the DGD mailing list