Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Darkly)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Uncategorized
  3. The thing about C needing newlines at the end of files keeps coming up.

The thing about C needing newlines at the end of files keeps coming up.

Scheduled Pinned Locked Moved Uncategorized
9 Posts 8 Posters 12 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • David Chisnall (*Now with 50% more sarcasm!*)D This user is from outside of this forum
    David Chisnall (*Now with 50% more sarcasm!*)D This user is from outside of this forum
    David Chisnall (*Now with 50% more sarcasm!*)
    wrote last edited by
    #1

    The thing about C needing newlines at the end of files keeps coming up. I am officially old, so I remember using parser generators that had the same limitation as the original AT&T C compiler, which couldn't handle token streams that ended in a token, but I can't remember if it was a lex or yacc limitation (I think it was lex, and lex would not parse the last token correctly). But I can't find a single reference to support this. So either I'm really old and my memory is failing, or the web is now 100% slop.

    Can anyone remember the details of the lex / yacc limitation?

    Stefan EissingI Wouter LindenhofD Jean-Baptiste "JBQ" QuéruJ Laurence TrattL David Glover-AokiD 6 Replies Last reply
    1
    0
    • David Chisnall (*Now with 50% more sarcasm!*)D David Chisnall (*Now with 50% more sarcasm!*)

      The thing about C needing newlines at the end of files keeps coming up. I am officially old, so I remember using parser generators that had the same limitation as the original AT&T C compiler, which couldn't handle token streams that ended in a token, but I can't remember if it was a lex or yacc limitation (I think it was lex, and lex would not parse the last token correctly). But I can't find a single reference to support this. So either I'm really old and my memory is failing, or the web is now 100% slop.

      Can anyone remember the details of the lex / yacc limitation?

      Stefan EissingI This user is from outside of this forum
      Stefan EissingI This user is from outside of this forum
      Stefan Eissing
      wrote last edited by
      #2

      @david_chisnall Perhaps @CoolSWEng can shed some light on this?

      1 Reply Last reply
      0
      • David Chisnall (*Now with 50% more sarcasm!*)D This user is from outside of this forum
        David Chisnall (*Now with 50% more sarcasm!*)D This user is from outside of this forum
        David Chisnall (*Now with 50% more sarcasm!*)
        wrote last edited by
        #3

        @rootnode

        I have vague recollections of any whitespace being fine after the last token, but a newline was the easiest to make sure was there.

        The header file thing would be the same: if you start a file with a token and end a header with a token, they will become a single token after including. But the only place that would need to be a newline is if the header ended with a preprocessing directive and I think the execution order of the preprocessor meant that this wasn't a problem.

        1 Reply Last reply
        0
        • David Chisnall (*Now with 50% more sarcasm!*)D David Chisnall (*Now with 50% more sarcasm!*)

          The thing about C needing newlines at the end of files keeps coming up. I am officially old, so I remember using parser generators that had the same limitation as the original AT&T C compiler, which couldn't handle token streams that ended in a token, but I can't remember if it was a lex or yacc limitation (I think it was lex, and lex would not parse the last token correctly). But I can't find a single reference to support this. So either I'm really old and my memory is failing, or the web is now 100% slop.

          Can anyone remember the details of the lex / yacc limitation?

          Wouter LindenhofD This user is from outside of this forum
          Wouter LindenhofD This user is from outside of this forum
          Wouter Lindenhof
          wrote last edited by
          #4

          @david_chisnall @icing

          One of them had an issue from what I recall and this was related to the fact that it read lines and not a character stream. And the reason I recall that was because multiline comments using /*…*/ was way harder than it has right to be. That being said this was many years ago and my understanding of parsers was bad.

          1 Reply Last reply
          0
          • David Chisnall (*Now with 50% more sarcasm!*)D David Chisnall (*Now with 50% more sarcasm!*)

            The thing about C needing newlines at the end of files keeps coming up. I am officially old, so I remember using parser generators that had the same limitation as the original AT&T C compiler, which couldn't handle token streams that ended in a token, but I can't remember if it was a lex or yacc limitation (I think it was lex, and lex would not parse the last token correctly). But I can't find a single reference to support this. So either I'm really old and my memory is failing, or the web is now 100% slop.

            Can anyone remember the details of the lex / yacc limitation?

            Jean-Baptiste "JBQ" QuéruJ This user is from outside of this forum
            Jean-Baptiste "JBQ" QuéruJ This user is from outside of this forum
            Jean-Baptiste "JBQ" Quéru
            wrote last edited by
            #5

            @david_chisnall Feels more likely to be a lex issue than a yacc issue, where lex would drop the incomplete token on EOF, for not treating EOF as a pseudo-character that matches nothing. I admit that feels odd to me, though.

            Though, in that case, having a space or a tab as the last character should also work, newline has no reason for only being the last legal character.

            Preprocessor requirement, maybe, where newlines are significant?

            1 Reply Last reply
            0
            • David Chisnall (*Now with 50% more sarcasm!*)D David Chisnall (*Now with 50% more sarcasm!*)

              The thing about C needing newlines at the end of files keeps coming up. I am officially old, so I remember using parser generators that had the same limitation as the original AT&T C compiler, which couldn't handle token streams that ended in a token, but I can't remember if it was a lex or yacc limitation (I think it was lex, and lex would not parse the last token correctly). But I can't find a single reference to support this. So either I'm really old and my memory is failing, or the web is now 100% slop.

              Can anyone remember the details of the lex / yacc limitation?

              Laurence TrattL This user is from outside of this forum
              Laurence TrattL This user is from outside of this forum
              Laurence Tratt
              wrote last edited by
              #6

              @david_chisnall One possibility: it may have had a hand-written lexer that had this limitation / design decision? Maybe even because lex hadn't been invented at that point (IIRC it postdates yacc by a year or two).

              1 Reply Last reply
              0
              • David Chisnall (*Now with 50% more sarcasm!*)D David Chisnall (*Now with 50% more sarcasm!*)

                The thing about C needing newlines at the end of files keeps coming up. I am officially old, so I remember using parser generators that had the same limitation as the original AT&T C compiler, which couldn't handle token streams that ended in a token, but I can't remember if it was a lex or yacc limitation (I think it was lex, and lex would not parse the last token correctly). But I can't find a single reference to support this. So either I'm really old and my memory is failing, or the web is now 100% slop.

                Can anyone remember the details of the lex / yacc limitation?

                David Glover-AokiD This user is from outside of this forum
                David Glover-AokiD This user is from outside of this forum
                David Glover-Aoki
                wrote last edited by
                #7

                @david_chisnall bash “while read” loops will also ignore the last line if it doesn’t end in a newline.

                1 Reply Last reply
                0
                • Andrew EadesM This user is from outside of this forum
                  Andrew EadesM This user is from outside of this forum
                  Andrew Eades
                  wrote last edited by
                  #8

                  @rootnode @david_chisnall Even today Xcode adds a newline to the end of a file. I think most tokenisers of yore would have used fgets to read a file line by line.

                  1 Reply Last reply
                  0
                  • David Chisnall (*Now with 50% more sarcasm!*)D David Chisnall (*Now with 50% more sarcasm!*)

                    The thing about C needing newlines at the end of files keeps coming up. I am officially old, so I remember using parser generators that had the same limitation as the original AT&T C compiler, which couldn't handle token streams that ended in a token, but I can't remember if it was a lex or yacc limitation (I think it was lex, and lex would not parse the last token correctly). But I can't find a single reference to support this. So either I'm really old and my memory is failing, or the web is now 100% slop.

                    Can anyone remember the details of the lex / yacc limitation?

                    HP van BraamH This user is from outside of this forum
                    HP van BraamH This user is from outside of this forum
                    HP van Braam
                    wrote last edited by
                    #9

                    @david_chisnall I can't really vouch for this information, but I can tell you that if this was due to either lex of yac it's probably lex.

                    If lex can't properly terminate the token stream then in the parser you'd have to write LF or EOF in the grammar a bazillion times I think.

                    Easier to just tell people to add a newline.

                    This is conjecture of course but I'm reasonably confident in this based on using flex and bison.

                    1 Reply Last reply
                    0
                    • R ActivityRelay shared this topic
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    Powered by NodeBB Contributors
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups