[antlr-interest] How to make this work ?? Again

mzukowski at yci.com mzukowski at yci.com
Mon Dec 23 09:24:56 PST 2002


Look at the generated code for your original, and then mine.  Note that
non-protected rules in lexer get thrown into a method called nextToken(),
look at the logic in there.

For question 1, assuming that your types are giving you nondeterminisms you
need to figure out which ones are ambiguous (look at the error message and
the generated code) and then add a predicate to one of them like:

type returns [Object obj]		
{obj = null;}
	:	(built_in_type)=>(obj = built_in_type)
	|	(obj = defined_type)		
...

Question 2 is trickier.  It's the "what if my keywords can also be
identifiers?" question discussed in the FAQ at
http://www.jguru.com/faq/view.jsp?EID=140.  The deal is that the parser
can't talk back to the lexer, so you need to either figure out how to have
the lexer solve the problem or have the parser be able to change keywords
back to identifiers.  A more specific example with sample input would be a
good place to start that discussion.

Monty

-----Original Message-----
From: v_vivekg <v_vivekg at yahoo.com> [mailto:v_vivekg at yahoo.com]
Sent: Friday, December 20, 2002 9:50 PM
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] How to make this work ?? Again


Thanks Monty,

That really worked. And I still wonder how. In my parser rules I call 
B_STRING or H_STRING and not BIN_OR_HEX_STRING as defined in lexer 
and it still works. Ok this solves one problem. I have two more 
problems to resolve related to ASN.1 grammar posted on antlr site.. 
Kindly help me out with them. 


Problem1 :

I want to use syntatic predicates in the parser. Like for a rule
with embedded code as 

type returns [Object obj]		
{obj = null;}
	:	(obj = built_in_type)
	|	(obj = defined_type)		
	|	(obj = useful_type)		
	|	(obj = selection_type ) 		
	|	(obj = type_from_Object )		
	|	(obj = value_set_from_objects)	
	|       (obj = type_and_constraint )		
	|	(obj = type_with_constraint)	
	|	(obj = macros_type)
        ;

How can I check for the correct type using predicates. ??
In the parser only type will be called .


Problem 2 :
In lexer I had declared tokens as

tokens { 
TOK1_ID = "TOK1" ;
TOK1_ID = "TOK1" ;
}

and UpperCase and Lower Case identifiers as

----------
UPPER	
options {testLiterals = false;}
	:   ('A'..'Z') 
		(options {warnWhenFollowAmbig = false;}
	:	( 'a'..'z' | 'A'..'Z' |'-' | '0'..'9' ))* 	;

LOWER
options {testLiterals = false;}
	:	('a'..'z') 
		(options {warnWhenFollowAmbig = false;}
	:	( 'a'..'z' | 'A'..'Z' |'-' | '0'..'9' ))* 	;

---------

testLiteral Option is set to true in the lexer. 
The problem is that while parsing if antlr finds a Keyword
in place of an ID there is an error.
I would like that in the parser when I specify LOWER or UPPER I should
get whatsoever is there , even if it is a token defined in the lexer.
How can this be done. I tried reversing the testLiterals option as 
well but it does not work.

I am sure these are very trival problem, I searched for an example
in this group and on google as well, but am still not able to solve
them.

Thanks for help

Regards
Vivek









--- In antlr-interest at yahoogroups.com, mzukowski at y... wrote:
> Like this:
> 
> protected
> BDIG		: ('0'|'1') ;
> protected
> HDIG		:	(options {warnWhenFollowAmbig = false;} :
('0'..'9')
> )
> 			|	('A'..'F')
> 			|	('a'..'f')
> 			;
> 
> protected B_STRING : "'"   (BDIG)+  "'B"  ;
> protected H_STRING : "'"   (HDIG)+  "'H"  ;
> 
> BIN_OR_HEX_STRING: (B_STRING)=>B_STRING {$setType(B_STRING);}
> 			 | H_STRING {$setType(H_STRING);}
> 			 ;
> 
> Monty
> 
> -----Original Message-----
> From: v_vivekg <v_vivekg at y...> [mailto:v_vivekg at y...]
> Sent: Wednesday, December 18, 2002 12:50 AM
> To: antlr-interest at yahoogroups.com
> Subject: [antlr-interest] How to make this work ??
> 
> 
> Hi all,
> 
> I have binary and hex digits defined as follows :
> 
> protected
> BDIG		: ('0'|'1') ;
> protected
> HDIG		:	(options {warnWhenFollowAmbig = false;} :
('0'..'9')
> )
> 			|	('A'..'F')
> 			|	('a'..'f')
> 			;
> 
> and Binary String and Hex String defined as follows in the lexer
> 
> B_STRING : "'"   (BDIG)+  "'B"  ;
> H_STRING : "'"   (HDIG)+  "'H"  ;
> 
> Now the problem is that this definition is not able to
> parse the string of type '01010111'H . The code goes into
> binary string parsing and gives error that 'B was expected
> but 'H was found instead.
> I tried with semantic and syntatic predicates with lot of
> options but unable to make this work.
> 
> Kindly help on how this simple thing can be made to work.
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
>  
> 
> Your use of Yahoo! Groups is subject to 
http://docs.yahoo.com/info/terms/


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list