[antlr-interest] Actions in predicates

Jim Idle jimi at temporal-wave.com
Mon Feb 22 09:16:32 PST 2010


You might deal with this the way I do for the JavaFX parser:

// -----------------------
// Process a SEMI colon that is always required, regardless of
// where the construct is in the script. There are not too many
// of these.
//
requiredSemi 
	: { input.LA(1) != SEMI}?=>
	
	  // If there was no semi colon here, then we need to issue an error
	  // though we don't worry about it syntactically.
	  //
	  {
		  log.error(semiPos(), MsgSym.MESSAGE_JAVAFX_SEMI_REQUIRED);
	  }
	
	| (SEMI)=>SEMI		// This is what we want	
	;

// -------------------------
// Decides whether a SEMI is required at this point in the parse (and issues
// an error if it is and is not present), or is optional (in which case it eats it) or
// we have just consumed one, which means we can not worry about it as the
// previous rule obviously decided that it wasn't optional and consumed it.
// This rule could result in doubly reporting the absent semi colon if there
// is a construct that consumes one, but does not find one, then a higher
// rule calls this rule, which also decides one is necessary as it did not see one.
// However, the grammar is carefully constructed such that this does not happen
//
possiblyOptSemi
	: 	
			{
				// Call super class function to decide whether to look for
				// the SEMI or not and whether to log an error if one is 
				// missing.
				//
				checkForSemi();
			}
	;

Then do not put your ';' in a different channel. The checkForSemi() method encapsulates the code for deciding if the semi colon is allowed to be missing or not - languages really should not make such things optional, it makes it very difficult to generate decent error messages - and issues an error message if it is required, otherwise just returns silently.

You can get the JavaFX compiler source code from Kenai: http://kenai.com/projects/openjfx-compiler 

Actions that are specified as:

{{ }}

Will be executed within predicate lookahead as well as normal mode and you can then check the backtracking flag to see if you are in predicate mode. However, I suggest that you will find that difficult to maintain.

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Marcin Rzeznicki
> Sent: Monday, February 22, 2010 8:56 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Actions in predicates
> 
> Hi all
> Yet another question from me ( if someone could reply to the old one,
> then
> I'd be grateful:
> http://www.antlr.org/pipermail/antlr-interest/2010-February/037693.html
> -
> sorry for spamming):
> Is there a way to fire an action during syntactic predicate and only
> then? I
> am trying to parse language in which semicolons as delimiters are
> optional.
> So my lexer puts them by default in different channel. But there are
> cases
> when I need to resolve ambiguities using syntactic predicates and I'd
> really
> need to switch to emitting semicolons during predicates because parser
> can
> use this information to resolve an ambiguity. A short excerpt from the
> grammar I am working on:
> 
> basic_expression
>   :
>   (
>     (
>       target
>       | manifest_type
>     )
>     DOT
>   )
>     => call
>   | target
> 
> During predicate used to disambiguate call subrule I'd need to fire
> action
> which sets appropriate flag in my lexer ordering it to emit semicolons
> to
> the main channel. Furthermore, I'd have to switch this mode off when
> exiting
> a predicate. Following version of my grammar was produced as an attempt
> at
> this:
> 
> basic_expression
>   :
>   ( { <<set_flag_here>> }
>     (
>       target
>       | manifest_type
> 
> 
>     DOT { <<unset_flag_here>> }
>   )
>     => call
>   | target
> 
> Unfortunately it won't work because of two things. First of all, my
> actions
> are within (state.backtracking == 0) which is obviously not true during
> predicate evaluation, and, final action won't be executed if
> backtracking
> fails. Any ideas?
> 
>  Thanks for your answers
> 
> --
> Greetings
> Marcin Rzeźnicki
> 
> 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