[antlr-interest] Problem removing warning

Jim Idle jimi at temporal-wave.com
Fri Mar 19 10:41:58 PDT 2010


That will give a tree that isn't very useful ;-) You need to express this in LL form such that the things that can be elements of your compound appear at the bottom of the tree. Then use DOT and not '.', make that be the root node and do not try to impose any semantic verification via syntactical specifications. You get:


grammar T;

options { output=AST; }

tokens {	EXPR; FUNC;}

aago 	:	 (expr NL)+ EOF
		->^(EXPR expr)+
	;

expr
 : element (DOT^ element)* 
 ;

element
	: LPAREN! expr RPAREN!  // Sometimes you want to preserve the LPAREN here
	| ID (LPAREN^ expr RPAREN)? // function call
;

LPAREN 	:	 '('			;
RPAREN  :	 ')'			;
ID 	:	 ('A'..'Z')+ 		;
DOT	: 	'.'			;
NL	:	('\n'|'\r')+		; 
ANY 	:	 . {skip();}		;


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Gerald Rosenberg
> Sent: Friday, March 19, 2010 10:01 AM
> To: antlr-interest interest
> Subject: [antlr-interest] Problem removing warning
> 
> Having a bit of difficulty in figuring out how to unambiguously parse
> this into an AST.
> Order of the elements is significant, the parens are significant, and
> the leading dot is significant.
> 
>    (.buf_unittest.complex_opt1).foo;
>    .buf_unittest.complex_opt1.fum;
>    (buf_unittest.complex_opt1).(.buf_unittest.quux);
>    (.buf_unittest.complex_opt1).(buf_unittest.corge).qux;
>    (complex_opt2).baz;
>    (complex_opt2).(grault);
>    (complex_opt2).bar.foo;
>    (complex_opt2).bar.(quux);
>    (complex_opt2).bar.(buf_unittest.corge).qux;
>    (complex_opt2).(garply).foo;
>    (complex_opt2).(garply).(.buf_unittest.quux);
>    (complex_opt2).(buf_unittest.garply).(corge).qux;
>    (ComplexOptionType2.ComplexOptionType4.complex_opt4).waldo;
>    (complex_opt2).fred.waldo;
>    (buf_unittest.complex_opt3).qux;
>    (complex_opt3).complexoptiontype5.plugh;
>    (complexopt6).xyzzy;
> 
> The rule ident_parens following appears to work, but Antlr is
> complaining "Decision can match input such as "'.' ID" using multiple
> alternatives: 1, 2" on both identN and identO.  I can see the
> theoretical overlap, but cannot tell if the warning is actually
> significant or how to fix the rules to avoid the warning.
> 
> ident_parens
>      : (identM | identN | identO ) ('.' ( identM | identO ) )*
>      ;
> 
> identM
>      :  '(' '.' ipd+=ID ( '.' ipd+=ID )* ')'   -> ^( IDENT_PARENSDOT
> $ipd+ )
>      |  '(' ip+=ID ( '.' ip+=ID )* ')'         -> ^( IDENT_PARENS $ip+
> )
>      ;
> 
> identN
>      : '.' id+=ID ( '.' id+=ID )*              -> ^( IDENT_DOT $id+ )
>      ;
> 
> identO
>      : i+=ID ( '.' i+=ID )*                    -> ^( IDENT $i+ )
>      ;
> 
> 
> Thanks...






More information about the antlr-interest mailing list