[antlr-interest] Using predicates for keywords

Indhu Bharathi indhu.b at s7software.com
Mon Aug 17 03:11:25 PDT 2009


Maybe you can use a setter to set what 'ifKeyword' is and use that in a
semantic predicate.

Something like:

@members {
	String ifKeyword;

	Public void setIfKeyword(String str) {
		this.ifKeyword = str;
	}
}

ifKeyword
	:	{ LT(1).getText().equals(ifKeyword) }?=> /*nothing*/
	;

Lexer will lex 'if' as just an ID.

Hope that helps.


Cheers, Indhu


-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Heiko Folkerts
Sent: Monday, August 17, 2009 3:34 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Using predicates for keywords

Hi,
I want to use an abstract syntax in wich the keyword e.g. for an if
statement is available at runtime with a database call. So I don't want to
hard code 'if' in the parser but use an expression for a ifkeyword which
checks the database and returns wether the token is an if keyword or not.

I've tried with syntactic and semantic predicates, but even after reading
the sections from the antlr reference I don't know which way is the right
one for me.

The grammar would like something like this:
Statement:
Ifexpresseion
| loopexpression
;

Ifexpression: ifkeyword condition thenkeyword actionexpression;
Loopexpression: loopkeyword condition ':' actionexpression;

So how to code the ifkeyword and loopkeyword rules?

Thx in advance.
Btw.: thx to Jim for the help compiling the generated C code.

Mit freundlichem Gruß
Heiko Folkerts
Systementwicklung und -design
--
______________________________________________
DAVID GmbH · Wendenring 1 · 38114 Braunschweig
Tel.: +49 531 24379-14
Fax.: +49 531 24379-79
E-Mail: mailto:Heiko.Folkerts at david-bs.de
WWW:   http://www.david-bs.de 
Eintragung: Amtsgericht Braunschweig, HRB 3167
Geschäftsführer: Frank Ptok
______________________________________________

 

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address



More information about the antlr-interest mailing list