ast generation not up to speed with parsing?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Darrell_Schiebel
Posted On:   Monday, September 25, 2006 03:57 PM

I'm having trouble generating ASTs for lists. I want these lists to be a flexible as possible... I've got something like: class psrString extends Parser; options { buildAST = true; } complex : COMPLEX | FLOAT ; variant : (OPENSQ!)? (complex ((COMMA^)? complex )*)) (CLOSESQ!)? ; class Scanner extends Lexer; options { filter=WS; } FLOAT : ('-'|'+')? FLT ((('-'|'+') FLT)? 'i' {$setType(COMPLEX);} )? ; COMMA : ',' ; OPENSQ : '[' ; CLOSESQ : ']' ; protected FLT : ( (D)+ ('.' (D)*)? | '.' (D)+ ) ( ('e'|'E') ('-'|'+')? (D)+ )? ; protected D : '0' .. '9' ; protected   More>>

I'm having trouble generating ASTs for lists. I want these lists to be a flexible as possible... I've got something like:


			
class psrString extends Parser;

options { buildAST = true; }

complex
: COMPLEX
| FLOAT
;

variant
: (OPENSQ!)? (complex ((COMMA^)? complex )*)) (CLOSESQ!)? ;

class Scanner extends Lexer;

options { filter=WS; }

FLOAT : ('-'|'+')? FLT
((('-'|'+') FLT)? 'i' {$setType(COMPLEX);} )? ;

COMMA : ',' ;
OPENSQ : '[' ;
CLOSESQ : ']' ;

protected FLT : ( (D)+ ('.' (D)*)? | '.' (D)+ ) ( ('e'|'E') ('-'|'+')? (D)+ )? ;
protected D : '0' .. '9' ;
protected WS : ' ' | ' ' | '
' {newline();} | '
' '
' {newline();} ;



Now, if there happens to be a COMMA, as in [1,2,3,4] , it is parsed and the AST is created fine... however, if the COMMAs are left out, as in [1 2 3 4] , then the AST only contains the first item (i.e. 1 ).


Can anyone give me a hint about how to handle this?


thanks,
Darrell

   <<Less

Re: ast generation not up to speed with parsing?

Posted By:   Darrell_Schiebel  
Posted On:   Monday, September 25, 2006 04:57 PM

While answering ones own question undoubtedly breaks some rule, I have found a partial answer at this nice website:


http://www.bearcave.com/software/antlr/


It looks like I need to do something like:



{ #variant = #([COMPLEX, "variant"], #variant); }


I'm still not completely sure what this is doing or how to introduce a dummy VARIANT token to substitute for COMPLEX here, but at least I think I can continue.


I guess time pressures kept me from digging as deep as I should have before asking for help, but any other suggestions or explaination would be welcome.


thanks, Darrell
About | Sitemap | Contact