[antlr-interest] Day 2: Trying to get an AST in C#. Progress, but still not working.

Peter Bosch pbosch at highpointsoftware.com
Mon May 7 14:15:46 PDT 2012


PROGRESS! Thanks, Kevin Carroll.

It now compiles and runs, so we're getting somewhere. So far, I have ...
- Renamed two HIDDEN keywords in MarblesLexer.cs (i.e. hidden channel) to
Hidden. (Is this due to a bug, or an error in my input?)
- Made my parse rule public so it could be called.
- Updated my parsing code to call 
	AstParserRuleReturnScope<object,IToken> ParseReturn =
Parser.parse();

Now, I get a parse tree, but it appears that the second and then the third
Box and Rock are overwriting the first. I debug into the parser's execution,
and I can see the BOX token with three ROCK tokens, so I'm wondering why
they're not making it into the AST. 

Anyway, I only get

ROOT
  BOX
    ROCK
      COLOR
        "Black"
      WEIGHT
        23
      R_TYPE
        "Quartz"

Where I expect to get a ROOT with 3 BOXes, and 3 ROCKs in each BOX and
properties under each ROCK.

To remind, my grammar is as follows - can anyone point out my error? (If you
need the input file contents, it's included in yesterday's email. Didn't
want to unnecessarily clog up the email tubes...

grammar Marbles;

// options, then tokens, then @header then @members.
options {
  language=CSharp3;
  output=AST;
}

tokens {
    ROOT;
}

@header {
    //using System;
}

@parser::namespace { Marbles }
@lexer::namespace { Marbles }

@parser::members {
	protected void Print(string s){ System.Console.WriteLine("Token: " +
s); } }

// Parser Rules.

public parse
    :  r=box* EOF -> ^(ROOT $r*)
    ;
 
color	: K_COLOR '=' s=STRINGLITERAL {Print("C");} ->^(K_COLOR $s) 
    ;

weight	: K_WEIGHT '=' w=INTEGER -> ^(K_WEIGHT $w)
    ;
 
htype	: K_HTYPE '=' t=STRINGLITERAL -> ^(K_HTYPE $t)
    ;
 
rtype	: K_RTYPE '=' t=STRINGLITERAL -> ^(K_RTYPE $t)
    ;
    
rock	: 
    '{' K_ROCK c=color w=weight t=rtype '}' {Print("R");} ->^(K_ROCK $c $w
$t)
    ;
    
hammer	:
    '{' K_HAMMER t=htype? w=weight? '}'  ->^(K_HAMMER $t? $w?)
    ;
    
box	:
    '[' K_BOX r=rock* ']' {Print("B");} -> ^(K_BOX $r*)
    ;
    
bag	:
    '[' K_HAMMER h=(hammer)* ']' -> ^(K_HAMMER $h*)
    ;
    

 
// Lexer Rules
EOL	: ('\r'? '\n') {$channel=HIDDEN;}
    ;
    
WS      : (' '|'\t'|'\f'| EOL)+ {$channel=HIDDEN;}
    ;
    
K_BOX	: 'BOX'
    ;
K_BAG	: 'BAG'
    ;
K_ROCK	: 'ROCK'
    ;
K_COLOR	: 'COLOR'
    ;
K_HAMMER        : 'HAMMER'
    ;
K_HTYPE	: 'H_TYPE'
    ;
K_RTYPE	: 'R_TYPE'
    ;
K_WEIGHT          : 'WEIGHT'
    ;
    
STRINGLITERAL
    : '"' (~('"' | '\\'))* '"'
    ;
    
INTEGER	: ('0'..'9')+
    ;

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

Peter Bosch
Highpoint Software Systems
E-mail : pbosch at highpointsoftware.com
Phone : 262-893-5400
P-Mail : S42 W27451 Oak Grove Lane Waukesha, WI, 53189
Useful and Usable Decision Support & Technical Software





More information about the antlr-interest mailing list