How do I distinguish keywords from identifiers and how can I have all keywords returned as KEYWORD?

Monty Zukowski

T. Parr: Just reference strings in your parser and the lexer will add them to a hashtable which it checks upon each completed token. If the token matches a "keyword" it returns that token type instead of the default (which you probably have as ID). Or you can manually add keywords via the literals option in the lexer/parser.

Monty: [to make all keywords come back as KEYWORD] Use the method called testLiteralsTable() which looks in the "literals" Hashtable to match keywords. Override that for the behavior you want, which in this case would be setting return value to KEYWORD instead of what is in the table.

        // Test the token text against the literals table 
        // Override this method to perform a different literals test 
        public int testLiteralsTable(int ttype) { 
                hashString.setBuffer(text.getBuffer(), text.length()); 
                Integer literalsIndex = (Integer)literals.get(hashString); 
                if (literalsIndex != null) { 
                        ttype = literalsIndex.intValue(); 
                } 
                return ttype; 
        } 
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact