[OT] Haxe "if" context-sensitive? (Q for Grammar Experts)

Tim M tim.matthews7 at gmail.com
Mon Aug 31 17:56:02 PDT 2009


Nick Sabalausky Wrote:

> "Nick Sabalausky" <a at a.a> wrote in message 
> news:h7fl1o$f8u$1 at digitalmars.com...
> > I've been trying to make a grammar for the Haxe langauge, and I've been 
> > having a hell of a time emulating it's expression-based if/if-else (as 
> > opposed to statement-based as in D). I'm sure a big part of it is my 
> > inexperience with writing grammars, but I've also been starting to wonder 
> > if it's impossible to do context-free. For those unfamiliar, here's how 
> > the relevant parts in Haxe work:
> >
> > ------------------------------------------
> ....
> > ------------------------------------------
> >
> > So, anyone know if a grammar that handles this would need to be 
> > context-sensitive? Or am I just *really* bad at this? ;)
> >
> 
> I guess the key I'm looking for is this:
> 
> // With "if" being an expr, not a stmt:
> foo = if(blah) bar = 1;
> foo = if(blah) bar = 1; else bar = 2;
> 
> If I make assignment bind tighter than the "if expressions", then I can't 
> support "foo = if...", I can only support "foo = (if...)"
> 
> But if I make the "if expressions" bind tighter than assignment, then I 
> can't seem to solve the dangling-else conflict without introducing other 
> ambiguities.
> 
> Anyway, I'm not really looking to get an exact solution, just wondering if 
> there's something about it that causes it to be impossible for a 
> context-free grammar.
> 
> 

Although I don't really understand your problem or how it relates to the D lang. D and other C like languages do have if expressions too but they're just not called if expressions. They're called ternary expressions due to the way they have 3 blocks around single characters rather than the word 'if' but the concept is pretty much the same.

condition ? trueCode : falseCode



More information about the Digitalmars-d mailing list