fbpx
Wikipedia

Directive (programming)

In computer programming, a directive or pragma (from "pragmatic") is a language construct that specifies how a compiler (or other translator) should process its input. Depending on the programming language, directives may or may not be part of the grammar of the language and may vary from compiler to compiler. They can be processed by a preprocessor to specify compiler behavior, or function as a form of in-band parameterization.

In some cases directives specify global behavior, while in other cases they only affect a local section, such as a block of programming code. In some cases, such as some C programs, directives are optional compiler hints and may be ignored, but normally they are prescriptive and must be followed. However, a directive does not perform any action in the language itself, but rather only a change in the behavior of the compiler.

This term could be used to refer to proprietary third-party tags and commands (or markup) embedded in code that result in additional executable processing that extend the existing compiler, assembler and language constructs present in the development environment. The term "directive" is also applied in a variety of ways that are similar to the term command.

The C preprocessor edit

In C and C++, the language supports a simple macro preprocessor. Source lines that should be handled by the preprocessor, such as #define and #include are referred to as preprocessor directives.

Another C construct, the #pragma directive, is used to instruct the compiler to use pragmatic or implementation-dependent features. Two notable users of this directive are OpenMP and OpenACC.

Syntactic constructs similar to C's preprocessor directives, such as C#'s #if, are also typically called "directives", although in these cases there may not be any real preprocessing phase involved.

All preprocessor commands begin with a hash symbol (#) with the exception of the import and module directives in C++.[1]

History edit

Directives date to JOVIAL.[2]

COBOL Had a COPY directive.

In ALGOL 68, directives are known as pragmats (from "pragmatic"), and denoted pragmat or pr; in newer languages, notably C, this has been abbreviated to "pragma" (no 't').

A common use of pragmats in ALGOL 68 is in specifying a stropping regime, meaning "how keywords are indicated". Various such directives follow, specifying the POINT, UPPER, RES (reserved), or quote regimes. Note the use of stropping for the pragmat keyword itself (abbreviated pr), either in the POINT or quote regimes:

.PR POINT .PR .PR UPPER .PR .PR RES .PR 'pr' quote 'pr' 

Today directives are best known in the C language, of early 1970s vintage, and continued through the current C99 standard, where they are either instructions to the C preprocessor, or, in the form of #pragma, directives to the compiler itself. They are also used to some degree in more modern languages; see below.

Other languages edit

  • In Ada, compiler directives are called pragmas (short for "pragmatic information").
  • In Common Lisp, directives are called declarations, and are specified using the declare construct (also proclaim or declaim).[3] With one exception, declarations are optional, and do not affect the semantics of the program. The one exception is special, which must be specified where appropriate.
  • In Turbo Pascal, directives are called significant comments, because in the language grammar they follow the same syntax as comments. In Turbo Pascal, a significant comment is a comment whose first character is a dollar sign and whose second character is a letter; for example, the equivalent of C's #include "file" directive is the significant comment {$I "file"}.
  • In Perl, the keyword "use", which imports modules, can also be used to specify directives, such as use strict; or use utf8;.
  • Haskell pragmas are specified using a specialized comment syntax, e.g. {-# INLINE foo #-}.[4]
  • PHP uses the directive declare(strict_types=1).
  • Python has two directives – from __future__ import feature (defined in PEP 236 -- Back to the __future__), which changes language features (and uses the existing module import syntax, as in Perl), and the coding directive (in a comment) to specify the encoding of a source code file (defined in PEP 263 -- Defining Python Source Code Encodings). A more general directive statement was proposed and rejected in PEP 244 -- The `directive' statement; these all date to 2001.
  • ECMAScript also adopts the use syntax for directives, with the difference that pragmas are declared as string literals (e.g. "use strict";, or "use asm";), rather than a function call.
  • In Visual Basic, the keyword "Option" is used for directives:
    • Option Explicit On|Off - When on disallows implicit declaration of variables at first use requiring explicit declaration beforehand.
    • Option Compare Binary - Results in string comparisons based on a sort order derived from the internal binary representations of the characters - e.g. for the English/European code page (ANSI 1252) A < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø. Affects intrinsic operators (e.g. =, <>, <, >), the Select Case block, and VB runtime library string functions (e.g. InStr).
    • Option Compare Text - Results in string comparisons based on a case-insensitive text sort order determined by your system's locale - e.g. for the English/European code page (ANSI 1252) (A=a) < (À = à) < (B=b) < (E=e) < (Ê = ê) < (Z=z) < (Ø = ø). Affects intrinsic operators (e.g. =, <>, <, >), the Select Case block, and VB runtime library string functions (e.g. InStr).
    • Option Strict On|Off - When on disallows:
      • typeless programming - where declarations which lack an explicit type are implicitly typed as Object.
      • late-binding (i.e. dynamic dispatch to CLR, DLR, and COM objects) on values statically typed as Object.
      • implicit narrowing conversions - requiring all conversions to narrower types (e.g. from Long to Integer, Object to String, Control to TextBox) be explicit in code using conversion operators (e.g. CInt, DirectCast, CType).
    • Option Infer On|Off - When on enables the compiler to infer the type of local variables from their initializers.
  • In Ruby, interpreter directives are referred to as pragmas and are specified by top-of-file comments that follow a key: value notation. For example, coding: UTF-8 indicates that the file is encoded via the UTF-8 character encoding.
  • In C#, compiler directives are called pre-processing directives. There are a number of different compiler directives including #pragma, which is specifically used to control compiler warnings and debugger checksums.[5][6]
  • The SQLite DBMS includes a PRAGMA directive that is used to introduce commands that are not compatible with other DBMS.[7]
  • In Solidity, compiler directives are called pragmas.

Assembly language edit

  • In assembly language, directives, also referred to as pseudo-operations or "pseudo-ops", generally specify such information as the target machine, mark separations between code sections, define and change assembly-time variables, define macros, designate conditional and repeated code, define reserved memory areas, and so on. Some, but not all, assemblers use a specific syntax to differentiate pseudo-ops from instruction mnemonics, such as prefacing the pseudo-op with a period, such as the pseudo-op .END, which might direct the assembler to stop assembling code.

PL/SQL edit

See also edit

Footnotes edit

  1. ^ "P1857R1 - Modules Dependency Discovery".
  2. ^ "Chapter 17 - Directives" (PDF). Computer Programming Manual for JOVIAL (J73) Language (PDF) (Technical report). June 1981. pp. 243–263. RADC-TR-81-143. Retrieved May 28, 2023.
  3. ^ Steele 1990, Chapter 9: Declarations, p. 215–237.
  4. ^ "7.20. Pragmas". GHC 7.8.3 Documentation. Retrieved 18 July 2014.
  5. ^ dotnet-bot. "Lexical structure - C# language specification". docs.microsoft.com. Retrieved 2019-11-01.
  6. ^ BillWagner. "#pragma - C# Reference". docs.microsoft.com. Retrieved 2019-11-01.
  7. ^ "Pragma statements supported by SQLite". www.sqlite.org.
  8. ^ Feuerstein, Steven; Pribyl, Bill (23 January 2014). Oracle PL/SQL Programming (6 ed.). O'Reilly Media, Inc. (published 2014). ISBN 9781449324414. Retrieved 2016-06-16. PL/SQL has a PRAGMA keyword with the following syntax: PRAGMA instruction_to_compiler; [...] PL/SQL offers several pragmas [...]

References edit

External links edit

  • OpenMP Website
  • OpenACC Website

directive, programming, this, article, needs, additional, citations, verification, please, help, improve, this, article, adding, citations, reliable, sources, unsourced, material, challenged, removed, find, sources, directive, programming, news, newspapers, bo. This article needs additional citations for verification Please help improve this article by adding citations to reliable sources Unsourced material may be challenged and removed Find sources Directive programming news newspapers books scholar JSTOR December 2013 Learn how and when to remove this template message In computer programming a directive or pragma from pragmatic is a language construct that specifies how a compiler or other translator should process its input Depending on the programming language directives may or may not be part of the grammar of the language and may vary from compiler to compiler They can be processed by a preprocessor to specify compiler behavior or function as a form of in band parameterization In some cases directives specify global behavior while in other cases they only affect a local section such as a block of programming code In some cases such as some C programs directives are optional compiler hints and may be ignored but normally they are prescriptive and must be followed However a directive does not perform any action in the language itself but rather only a change in the behavior of the compiler This term could be used to refer to proprietary third party tags and commands or markup embedded in code that result in additional executable processing that extend the existing compiler assembler and language constructs present in the development environment The term directive is also applied in a variety of ways that are similar to the term command Contents 1 The C preprocessor 2 History 3 Other languages 3 1 Assembly language 3 2 PL SQL 4 See also 5 Footnotes 6 References 7 External linksThe C preprocessor editMain article C preprocessor In C and C the language supports a simple macro preprocessor Source lines that should be handled by the preprocessor such as define and include are referred to as preprocessor directives Another C construct the pragma directive is used to instruct the compiler to use pragmatic or implementation dependent features Two notable users of this directive are OpenMP and OpenACC Syntactic constructs similar to C s preprocessor directives such as C s if are also typically called directives although in these cases there may not be any real preprocessing phase involved All preprocessor commands begin with a hash symbol with the exception of the import and module directives in C 1 History editDirectives date to JOVIAL 2 COBOL Had a COPY directive In ALGOL 68 directives are known as pragmats from pragmatic and denoted pragmat or pr in newer languages notably C this has been abbreviated to pragma no t A common use of pragmats in ALGOL 68 is in specifying a stropping regime meaning how keywords are indicated Various such directives follow specifying the POINT UPPER RES reserved or quote regimes Note the use of stropping for the pragmat keyword itself abbreviated pr either in the POINT or quote regimes PR POINT PR PR UPPER PR PR RES PR pr quote pr Today directives are best known in the C language of early 1970s vintage and continued through the current C99 standard where they are either instructions to the C preprocessor or in the form of pragma directives to the compiler itself They are also used to some degree in more modern languages see below Other languages editIn Ada compiler directives are called pragmas short for pragmatic information In Common Lisp directives are called declarations and are specified using the declare construct also proclaim or declaim 3 With one exception declarations are optional and do not affect the semantics of the program The one exception is special which must be specified where appropriate In Turbo Pascal directives are called significant comments because in the language grammar they follow the same syntax as comments In Turbo Pascal a significant comment is a comment whose first character is a dollar sign and whose second character is a letter for example the equivalent of C s include file directive is the significant comment I file In Perl the keyword use which imports modules can also be used to specify directives such as use strict or use utf8 Haskell pragmas are specified using a specialized comment syntax e g INLINE foo 4 PHP uses the directive declare strict types 1 Python has two directives from future import feature defined in PEP 236 Back to the future which changes language features and uses the existing module import syntax as in Perl and the coding directive in a comment to specify the encoding of a source code file defined in PEP 263 Defining Python Source Code Encodings A more general directive statement was proposed and rejected in PEP 244 The directive statement these all date to 2001 ECMAScript also adopts the use syntax for directives with the difference that pragmas are declared as string literals e g use strict or use asm rather than a function call In Visual Basic the keyword Option is used for directives Option Explicit On Off When on disallows implicit declaration of variables at first use requiring explicit declaration beforehand Option Compare Binary Results in string comparisons based on a sort order derived from the internal binary representations of the characters e g for the English European code page ANSI 1252 A lt B lt E lt Z lt a lt b lt e lt z lt A lt E lt O lt a lt e lt o Affects intrinsic operators e g lt gt lt gt the Select Case block and VB runtime library string functions e g InStr Option Compare Text Results in string comparisons based on a case insensitive text sort order determined by your system s locale e g for the English European code page ANSI 1252 A a lt A a lt B b lt E e lt E e lt Z z lt O o Affects intrinsic operators e g lt gt lt gt the Select Case block and VB runtime library string functions e g InStr Option Strict On Off When on disallows typeless programming where declarations which lack an explicit type are implicitly typed as Object late binding i e dynamic dispatch to CLR DLR and COM objects on values statically typed as Object implicit narrowing conversions requiring all conversions to narrower types e g from Long to Integer Object to String Control to TextBox be explicit in code using conversion operators e g CInt DirectCast CType Option Infer On Off When on enables the compiler to infer the type of local variables from their initializers In Ruby interpreter directives are referred to as pragmas and are specified by top of file comments that follow a key value notation For example coding UTF 8 indicates that the file is encoded via the UTF 8 character encoding In C compiler directives are called pre processing directives There are a number of different compiler directives including pragma which is specifically used to control compiler warnings and debugger checksums 5 6 The SQLite DBMS includes a PRAGMA directive that is used to introduce commands that are not compatible with other DBMS 7 In Solidity compiler directives are called pragmas Assembly language edit In assembly language directives also referred to as pseudo operations or pseudo ops generally specify such information as the target machine mark separations between code sections define and change assembly time variables define macros designate conditional and repeated code define reserved memory areas and so on Some but not all assemblers use a specific syntax to differentiate pseudo ops from instruction mnemonics such as prefacing the pseudo op with a period such as the pseudo op END which might direct the assembler to stop assembling code PL SQL edit Oracle Corporation s PL SQL procedural language includes a set of compiler directives known as pragmas 8 See also edit pragma once Preprocessor directive in C and C Footnotes edit P1857R1 Modules Dependency Discovery Chapter 17 Directives PDF Computer Programming Manual for JOVIAL J73 Language PDF Technical report June 1981 pp 243 263 RADC TR 81 143 Retrieved May 28 2023 Steele 1990 Chapter 9 Declarations p 215 237 7 20 Pragmas GHC 7 8 3 Documentation Retrieved 18 July 2014 dotnet bot Lexical structure C language specification docs microsoft com Retrieved 2019 11 01 BillWagner pragma C Reference docs microsoft com Retrieved 2019 11 01 Pragma statements supported by SQLite www sqlite org Feuerstein Steven Pribyl Bill 23 January 2014 Oracle PL SQL Programming 6 ed O Reilly Media Inc published 2014 ISBN 9781449324414 Retrieved 2016 06 16 PL SQL has a PRAGMA keyword with the following syntax PRAGMA instruction to compiler PL SQL offers several pragmas References editSteele Guy L 1990 Common Lisp the Language Digital Press pp 215 237 ISBN 1 55558 041 6 External links editOpenMP Website OpenACC Website OpenHMPP Website Retrieved from https en wikipedia org w index php title Directive programming amp oldid 1195498757, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.