When a keyword means different things in different contexts, is this an example of context sensitivity?

According to this answer => in Scala, this is a keyword that has two different meanings: 1 to indicate the type of function: 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 contextual?

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


Edit:

It seems like I don't understand context sensitive grammars enough. I know what the production rules should look like and what they mean (“this production only applies if A is surrounded by these characters”), but I'm just not sure how they relate to real (programmable) languages.

I think my confusion is related to reading something like “Chomsky coined this term because the meaning of a word may depend on its context,” and I connected => with the term “word” in the quote, and these two uses are two individual context.

It would be great if the answer touched my confusion.

+6
source share
1 answer

Some time has passed since I worked out the theory of formal language, but I will bite.

“Context-free” means that the production rules needed in the corresponding grammar do not have a “context” . It does not mean that a certain character cannot be displayed in different rules.


Editing addressing: in other words (and more informally), the decision about whether a language is context-sensitive or context-sensitive comes down to not looking at the “meaning” of a particular “word” or “word”. Instead , it means looking at the set of all legal expressions in that language and looking at whether they can only be “encoded”, taking into account the positional relationship of the “words” component, with each other . This is essentially what Pumping Lemma is testing .


For instance:

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

Where S is, of course, the starting character, superscripts are non-terminals, and quoted strings are terminals. Obviously, this can generate a string like:

 Double=>Double=x=>x 

but the grammar is still context-free .

As simple as observing that the nonterminal "=>" may appear in two "places" of the program, the Scala context does not.

However, it does not mean that:

  • the whole Scala language has no context,
  • context sensitive - it can be even more complicated
  • if you want to encode semantics from Scala to grammar, you get either context-sensitive or context-sensitive.

The latter is especially true since you mentioned “meaning” in the (nomen omen) context of formal languages.

+6
source

All Articles