“context“ in C and Python

51 篇文章 2 订阅
24 篇文章 0 订阅

"context" as a linguistic concept

C and C++ are not context free

"context" in this regard is just the usual semantical ambience surrounding a word or sentence that helps to determine the word's or sentence's meaning.

for programming languages, grammar may sometimes relies on locality and most commonly, scopes, of codes to be interpreted correctly, as shown in the article:

It seems that the Internet is still confused about this, so let’s consider the question:

Are C and C++ context-free languages?

And the answer is

No, C and C++ are context-sensitive languages.

There are several reasons.

  1. To parse C and C++, you start by using a very powerful preprocessor. These preprocessors are inevitably written by hand (they are not based on a theoretic foundation like regular expressions or context-free grammars).

  2. C and C++ lexers require lexical feedback to differentiate between typedef names and identifiers. That is, the context-sensitive lexer needs help from the “context-free” parser to distinguish between an identifier “foo” and a typedef name “foo”. In this snippet,

         int foo;
         typedef int foo;
         foo x;
    

    the first “foo” is an identifier while the second and third are typedef names. You need to parse typedef declarations to figure this out (and since types have nested parentheses, this is definitely at least as hard as parsing a context-free language).

    This means that the parser and lexer are mutually recursive, so it doesn’t make sense to say that the parser is context free while the lexer is context sensitive.

  3. The grammar is ambiguous: it has LR conflicts, such as the if-then-else conflict. Parsers typically resolve this using context (an “else” matches the closest “if”).

If you believe that these reasons are not sufficient to show that the languages are context sensitive, please consult my previous post on the subject.

The author went on to discuss why context-based languages can be problematic sometimes, read by the link if interested.

"Context" as a "struct"

IBM Documentation

Context is a data type that you can use to store sets of data.

Contexts are like the struct data type in C/C++. Contexts can be used to store elements of any combinations of data types, including other contexts and arrays. This data is stored in a set of variables called member variables that are "contained" inside the context. Member variables can be of any type, including other contexts.

You reference member variables using the dot notation. This is also the way that you reference member variables in a struct in languages like C and C++. In this notation, you specify the name of the context and the name of the member variable separated by a period (.). You use this notation when you assign values to member variables and when you reference the variables elsewhere in a policy.

e.g.: C's ffmpeg libraries use "Context" for interfaces

c - What does the word "context" usually mean in structures? - Stack Overflow

Context Manager in Python

Context Manager in Python - GeeksforGeeks

here, we see "context" is synonymous with "scope"

Managing Resources using context manager: Suppose a block of code raises an exception or if it has a complex algorithm with multiple return paths, it becomes cumbersome to close a file in all the places. Generally in other languages when working with files try-except-finally is used to ensure that the file resource is closed after usage even if there is an exception. Python provides an easy way to manage resources: Context Managers. The with keyword is used. When it gets evaluated it should result in an object that performs context management. Context managers can be written using classes or functions(with decorators).

e.g.

  File management using context manager and with statement: On executing the with block, the following operations happen in sequence:

  • FileManager object is created with test.txt as the filename and w(write) as the mode when __init__ method is executed.
  • The __enter__ method opens the test.txt file in write mode(setup operation) and returns a file object to variable f.
  • The text ‘Test’ is written into the file.
  • The __exit__ method takes care of closing the file on exiting the with block(teardown operation). When print(f.closed) is run, the output is True as the FileManager has already taken care of closing the file which otherwise needed to be explicitly done.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值