[antlr-interest] The filter option

Marcos Marín marcosmarin at gmail.com
Thu Aug 2 10:48:12 PDT 2007


Hello, I'm new to ANTLR, what I'm trying to do is a very simple parser to
keep track of variables in a C/C++ source file and what function they are
declared in (if any).
This is my grammar so far (I'm mostly just testing, it's not complete):

grammar cvartracker;

options {
    language = CSharp;
    filter = true;
}

tokens {
    SEMI = ';' ;
}

@lexer::namespace {
CVarTracker
}

@parser::namespace {
CVarTracker
}

@header {
using System.Collections.Generic;
}

@members {
public struct Variable
{
    public string Name;
    public string Type;
    public string Function;
}

Dictionary<string, List<Variable>> variables = new Dictionary<string,
List<Variable>> ();
string current_function = string.Empty;
int curly_level = 0;

public Dictionary<string, List<Variable>> Variables {
    get { return variables; }
}

}

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

parse
    : (sentence)* EOF
    ;

sentence
    : function_declaration { System.Console.WriteLine ("function: '{0}'",
$function_declaration.text); }
    | variable_declaration { System.Console.WriteLine ("variable: '{0}'",
$variable_declaration.text); }
    ;

function_declaration
    : PRIMITIVE IDENTIFIER '(' PRIMITIVE IDENTIFIER (',' PRIMITIVE
IDENTIFIER)* ')'
    ;

variable_declaration
    : PRIMITIVE IDENTIFIER SEMI
    | PRIMITIVE IDENTIFIER '=' .* SEMI
    ;


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

PRIMITIVE
    : 'int'
    | 'float'
    | 'char'
    ;

IDENTIFIER
    : (ALPHANUMERIC)+
    ;

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

COMMENT : '/*' .* '*/' { $channel=HIDDEN; } ;

LINE_COMMENT : '//' ~('\n'|'\r')* '\r'? '\n' { $channel=HIDDEN; } ;

fragment ALPHANUMERIC : ('a'..'z'|'A'..'Z'|'_'|'0'..'9') ;


Running this very simple test case:

int main (int arg)
{
    int some_int;
}

Nothing happens (ie, the System.Console.WriteLine() that is supposed to be
called when there is a function or variable declaration is not called). If I
turn the filter option off it does work, but then it complains about other
things I'm not interested in. Anyone have any ideas on what I can do?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070802/5b5d1e44/attachment.html 


More information about the antlr-interest mailing list