ssn
ssn

Reputation: 2711

The term "context" in programming languages and how context is affected by loading and updation?

What does the term context mean in context-free and context-sensitive languages? Can a variable have multiple contexts? If I need to store a particular value in a particular memory address how does does affect the context of the memory address? And if I want to update a variable how does the context of the variable change?

Upvotes: 0

Views: 551

Answers (2)

chill
chill

Reputation: 16888

A context-sensitive grammar, productions have the general form

aBc -> ab'c

where upper-case symbols are non-terminals and lower-case symbols are sequences of terminals and non-terminals.

In the above example, a and b would be the context of the non-terminal B and indicate that B may "expand" to b' only when surrounded by a and c.

In a context free grammar, the production might look like

B -> b'

i.e. B "expands" to b' regardless of whatever is around it, in other words, regardless of context.

Upvotes: 4

immortal
immortal

Reputation: 3188

Consider the following C program:

#include <stdio.h>

unsigned int my_value = 0;

void print_value(void)
{
    printf("My value: %d\n", my_value);
}

int main(void)
{
    int my_value = 5;
    printf("My main value: %d\n", my_value);
    print_value();
    return 0;
}

This program will print:

My main value: 5
My Value: 0

The reason is that the variable my_value is linked to the one defined in the main function in the context of main and to the one defined as a global variable in the context of the print_value function. Most languages, like C, have context for their variable definitions allowing what is called hiding of variables: a variable defined inside of a block (the area between { and } signs) is not overriding previous definitions of the variable name, but rather "hides" them for the context of the block.

There are languages that are not context-sensitive where a name exists since the moment of definition and until the program exits. Updating a variable in these languages will update its value program-wide.

Upvotes: 0

Related Questions