line directive

Discussion in 'C Programming' started by Mantorok Redgormor, Nov 16, 2003.

  1. what is the purpose of the #line directive?
    how is it suppose to be used?

    I didn't see it mentioned in the faq and I can't make any sense of it from
    the standard.



    --
    nethlek
    Mantorok Redgormor, Nov 16, 2003
    #1
    1. Advertising

  2. Mantorok Redgormor

    CBFalconer Guest

    Mantorok Redgormor wrote:
    >
    > what is the purpose of the #line directive?
    > how is it suppose to be used?


    >From N869:


    6.10.4 Line control

    Constraints

    [#1] The string literal of a #line directive, if present,
    shall be a character string literal.

    Semantics

    [#2] The line number of the current source line is one
    greater than the number of new-line characters read or
    introduced in translation phase 1 (5.1.1.2) while processing
    the source file to the current token.

    [#3] A preprocessing directive of the form

    # line digit-sequence new-line

    causes the implementation to behave as if the following
    sequence of source lines begins with a source line that has
    a line number as specified by the digit sequence
    (interpreted as a decimal integer). The digit sequence
    shall not specify zero, nor a number greater than
    2147483647.

    [#4] A preprocessing directive of the form

    # line digit-sequence "s-char-sequence-opt" new-line

    sets the presumed line number similarly and changes the
    presumed name of the source file to be the contents of the
    character string literal.

    [#5] A preprocessing directive of the form

    # line pp-tokens new-line

    (that does not match one of the two previous forms) is
    permitted. The preprocessing tokens after line on the
    directive are processed just as in normal text (each
    identifier currently defined as a macro name is replaced by
    its replacement list of preprocessing tokens). The
    directive resulting after all replacements shall match one
    of the two previous forms and is then processed as
    appropriate.

    In other words it can be used to play games with the compilers
    opinion of the current file name and the current source line
    number.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Nov 16, 2003
    #2
    1. Advertising

  3. Mantorok Redgormor

    Sidney Cadot Guest

    Mantorok Redgormor wrote:

    > what is the purpose of the #line directive?
    > how is it suppose to be used?


    This is usually used by code generators (e.g. lex/flex/bison/yacc), or
    languages that compile to C instead of to assembly (e.g. f2c). If the
    generated program contains a compile error, you can get an error/warning
    in terms of the original file (which you wrote) instead of the
    generated file (that the generator/translator wrote).

    Example: a lex input file (sample.l) containing an error:

    ----- start of file
    %%
    fubar printf("a syntax error" 123);
    %%
    ----- end of file

    (pass it through lex)

    $ lex sample.l
    $ wc -l lex.yy.c

    1499 lex.yy.c

    (the generated file is no less than 1499 lines long)

    $ cc lex.yy.c

    sample.l: In function `yylex':
    sample.l:2: error: parse error before numeric constant

    (Thanks to #line, I get an error in terms of the original, while the
    faulty printf statement actually ends up line 605 of lex.yy.c)


    Best regards,

    Sidney
    Sidney Cadot, Nov 16, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M. Uli Kusterer

    Equivalent to C's #line directive?

    M. Uli Kusterer, Sep 20, 2004, in forum: Java
    Replies:
    5
    Views:
    478
    Daniel Bonniot
    Sep 21, 2004
  2. AA

    Using #line directive

    AA, Nov 30, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    543
    Michael Nemtsev
    Dec 1, 2006
  3. Pawel

    #line directive makes gdb crazy ?

    Pawel, Jan 12, 2007, in forum: C Programming
    Replies:
    3
    Views:
    612
    Kenneth Brody
    Jan 15, 2007
  4. AA

    Using #line directive

    AA, Nov 30, 2006, in forum: C++
    Replies:
    0
    Views:
    354
  5. Alf P. Steinbach

    Re: Using #line directive

    Alf P. Steinbach, Nov 30, 2006, in forum: C++
    Replies:
    1
    Views:
    323
    Richard Heathfield
    Nov 30, 2006
Loading...

Share This Page