Thanks Jim, I'll check that. On Tue, Feb 22, 2011 at 11:19 AM, Jim Idle <[email protected]> wrote:
> Use the mailing list search at antlr.markmail.org for examples of local > variable out of scope in predicates. Generally you cannot use local > parameters as elements of your predicate because the code generator > generates the predicate as a separate method and so the local parameter to > your rule is out of scope. You need to use a scope variable to do that. > > That said, your design is the best one in that you are trying to enforce > semantics/context within your parser grammar rules. This will result in a > parser that is error prone and is also unable to give out good error > messages to your users. The better way to do this is to accept any > BYTE_VALUE and merge type_sell and type_itsp into one left factored rule. > Then you can tell this rule what to check for as a rule parameter and > compare what it finds. If what it finds is not what is expected you can > now issue a semantic error such as "Must have a type selector here" or > something. Ideally you would create a tree and then walk the tree to check > semantics and so on. > > Hope that helps, > > Jim > > > > -----Original Message----- > > From: [email protected] [mailto:antlr-interest- > > [email protected]] On Behalf Of Alex Lujan > > Sent: Tuesday, February 22, 2011 7:22 AM > > To: [email protected] > > Subject: [antlr-interest] Rule optimization - inline > > > > Hi All, > > > > I'm having a problem with what I believe is an optimization within the > > ANTLR code generation. > > > > Consider the following sample grammar: > > > > grammar CharSelectionTest; > > > > @header { > > import org.apache.commons.lang.StringUtils; > > } > > > > type : (type_sell | type_itsp) data ; > > > > type_sell : character['s']; > > > > type_itsp : character['i']; > > > > data : BYTE_VALUE*; > > > > character[char character_to_match] > > : {input.LT(1).getText().charAt(0) == > > character_to_match}? BYTE_VALUE; > > > > BYTE_VALUE : '\u0000'..'\uFFFE'; > > > > > > When ANTLR generates the Parser java code, the type() method seems to > > be replacing the call to the character rule with an inline equivalent: > > > > public final void type() throws RecognitionException { > > try { > > { > > int alt1=2; > > int LA1_0 = input.LA(1); > > > > if ( (LA1_0==BYTE_VALUE) ) { > > int LA1_1 = input.LA(2); > > > > // Compile error: character_to_match is not defined! > > if ( ((input.LT(1).getText().charAt(0) == > > character_to_match)) ) { > > alt1=1; > > } > > else if ( ((input.LT(1).getText().charAt(0) == > > character_to_match)) ) { > > alt1=2; > > } > > else { > > NoViableAltException nvae = > > new NoViableAltException("", 1, 1, input); > > > > throw nvae; > > } > > } > > ... > > } > > > > Note that the variable character_to_match is not defined, since it's > > supposed to be a parameter of the character rule. > > > > Is there anything wrong with the rule definition in this simple > > grammar? > > > > Is this a known issue? > > > > Any workarounds / solutions to this problem? > > > > Thanks very much for your help. > > > > -- > > Alejandro Lujan > > > > List: http://www.antlr.org/mailman/listinfo/antlr-interest > > Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your- > > email-address > > List: http://www.antlr.org/mailman/listinfo/antlr-interest > Unsubscribe: > http://www.antlr.org/mailman/options/antlr-interest/your-email-address > -- Alejandro Lujan Apption Software (613) 725 62 68 x625 List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address -- You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en.
