Thread: [Flex/Bison] Handling newlines

  1. #1
    Registered User
    Join Date
    Dec 2007

    [Flex/Bison] Handling newlines

    I'm not sure if a C programming forum is the right place to ask something regarding Flex and Bison, but people around here have been very helpful in the past so I decided to give it a try.

    So, we're required to create a lexical & semantics parser as part of the Compilers course, using Flex and Bison. My issue is that I don't have a proper way to handle newlines. We're required to ignore all whitespace and newlines except if specifically asked, so I did the simple thing and had my Flex file return nothing in case of whitespace. But there is a certain rule in the language we're asked to parse that requires a newline character at the end of the line.

    Rule goes something like this:
    program: PROGRAM variable_name newline
    That's the only rule that specifically requires a newline. Everywhere else we're asked to ignore it. So what's a proper way of requiring the newline there, but having it ignored everywhere else so that I don't have to add optional "newline" rules in every rule that I have in my syntax?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Sounds like you want one function to handle all the newlines. Then,
    if (first word in the line is "program: ")
      ; //do nothing
      delete the newline
    If you have logic to handle newlines spread all over, when specifically asked not to ignore them, you're going to have a mess on your hands - no doubt.

    Just a general note - I don't know Flex/Bison.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Yeah, that sounds logical. newline character detected in flex should call a function in bison which should be able to tell if the newline character was read after the program's name, and if so return it (accept it), or else ignore it.

    If only I knew how to do something like that in Flex/Bison.


  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    If the PROGRAM token can only occur in this particular grammar production, then in the lexer you can simply flip a switch when you return PROGRAM that tells you to not ignore the next newline you see. After returning that newline flip that switch back off again.


    program { do_newline = 1; return PROGRAM; }
    \n { if (do_newline) { do_newline = 0; return '\n'; } }
    These sorts of dependencies between the parser and the lexer are pretty common, and there is no single way of handling them. Basically, you are adding context-dependence to your grammar and it can be anywhere from relatively clean to horrible messy. The flex and bison user manuals have some examples of this sort of thing. But use them as inspiration, not templates for solving the problem.
    Last edited by brewbuck; 08-20-2010 at 12:33 PM.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  5. #5
    Registered User
    Join Date
    Dec 2007
    Many thanks. I realized what I had to do reading your suggestion before looking at the code snippet, but it was exactly that.

    Really, many thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testpad for itoa()
    By frktons in forum C Programming
    Replies: 92
    Last Post: 07-19-2010, 03:05 PM
  2. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  3. Handling critical errors
    By Memloop in forum C Programming
    Replies: 14
    Last Post: 03-26-2009, 06:14 AM
  4. event handling is serialized in MS Visual Studio C++ 2005 ??
    By mynickmynick in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2008, 04:47 AM
  5. need help on error handling.
    By broli86 in forum C Programming
    Replies: 9
    Last Post: 06-19-2008, 11:55 AM

Tags for this Thread