When a keyword means different things in different contexts, is that an example of context sensitivity?
Asked Answered
C

1

7

According to this answer => in Scala is a keyword which has two different meanings: 1 to denote a function type: Double => Double and 2 to create a lambda expression: (x: Double): Double => 2*x.

How does this relate to formal grammars, i.e. does this make Scala context sensitive?

I know that most languages are not context free, but I'm not sure whether the situation I'm describing has anything to do with that.


Edit:

Seems like I don't understand context sensitive grammars well enough. I know how the production rules are supposed to look, and what they mean ("this production applies only if A is surrounded by these symbols"), but I'm just not sure how they relate to actual (programming) languages.

I think my confusion stems from reading something like "Chomsky introduced this term because a word's meaning can depend on its context", and I connected => with the term "word" in the quote, and those two uses of it being two separate contexts.

It be great if an answer would address my confusion.

Cassock answered 8/3, 2014 at 18:38 Comment(0)
P
7

It's been a while since I've handled formal language theory, but I'll bite.

"Context-free" means that the production rules required in the corresponding grammar do not have a "context". It does not mean that a specific symbol cannot appear in different rules.


Addressing the edit: in other words (and more informally), deciding whether a language is context-free or context-sensitive boils down not to looking at the "meaning" of a specific "word" or "words". Instead, it amounts to looking at the set of all legal expressions in that language, and seeing whether you can "encode" them only by taking into account the positional relationships the component "words" have with one another. This is essentially what the Pumping Lemma checks.


For example:

S → Type"="Body
Type → "Double"
Type → "Double""=>""Double"
Body → Lambda
Body → NormalBody
NormalBody → "x"
Lambda -> "x""=>"NormalBody

Where S is of course the start symbol, uppercased IDs are nonterminals, and quoted strings are terminals. Obviously, this can generate a string like:

Double=>Double=x=>x

but the grammar is still context-free.

So just this, as in the observation that the nonterminal "=>" can appear in two "places" of the program, does not make Scala context-sensitive.

However, it does not mean that:

  • the entire Scala language is context-free,
  • it is context-sensitive - it can be even more complex,
  • if you would like to encode the semantics of Scala into a grammar, you would end up with either a context-free or a context-sensitive one.

The last thing is especially relevant since you've mentioned "meaning" in the (nomen omen) context of formal languages.

Phraseogram answered 8/3, 2014 at 19:20 Comment(4)
Thank you for your answer, that's what I thought was the truth. However I am still a little confused, could you please see my edit?Cassock
@yannbane : hmm, I hoped the second paragraph would explain away this confusion. I've edited in a more informal elaboration. If the CS-theorists lynch me for that, I'm on your conscience.Veator
Thanks for the clarification. Another question if I may: a language is not context free if there are strings which are successfully (and unambiguously) parsed by the grammar, but aren't actually valid programs - correct? For example, if a variable has to be declared before it is used, and that is not somehow specified by the grammar, the language is not context free? Or is it that the grammar is context free (i.e. produces a context free language), but the resulting language is just limited by additional rules imposed by further passes that have nothing to do with the formal definition?Cassock
@yannbane: neither, actually. It now sounds like you're confusing formal languages and programming languages. A formal language is simply a mathematical construct that fits the def. A programming language does not have to be specified as a formal language, especially when talking about its semantics. Picture it like this: "Frog blast the vent core" is a correct sentence in English, but is also unintelligible. If you're still confused, I recommend asking a supplemental question on the CS SE.Veator

© 2022 - 2024 — McMap. All rights reserved.