Thread: What to do with multi-character operators when parsing ?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    What to do with multi-character operators when parsing ?

    I thought that I'd skip multi-character operators entirely, but am having difficulties to come up with enough single-char operators for covering the ground.

    My idea was to make the tokens run through another function ...say.. the 'joiner' which would do stuff like..
    *Join multi character operators together.
    *Convert integers separated by a ' . ' into doubles ..etc

    But I can't imagine what to do when the joining of the operators isn't necessary.

    For example :
    When the input is a mathematical expression in rpn(postfix) notation, two '+' `s together does not necessarily mean an increment operator, but it could mean so at times!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Your parser should be smart enough to take them as one token. Note that lexical parsing is kind of a big deal and there is a lot of literature on the subject. From your problem, I think you don't have a context free language to parse, so your lexer should look at the context of trouble statements to decide how to parse them. You've given me kind of a poor example, but if the statement is a math expression then the RPN must have at least 4 other operands. If it's not exactly that, then you must be using postfix increment. If it's not that, then there is a syntax error.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Basically, you are asking how to handle a case where there can be ambiguity. One way is to specify the grammar such that it is not ambiguous for the parser that you wish to implement. Another way is to perform lookahead such that the parser can resolve the ambiguity with the information gained from the lookahead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Character Constant.. how to correct the error? 3:
    By Tinray Reyes in forum C++ Programming
    Replies: 2
    Last Post: 06-04-2011, 08:30 AM
  2. scanf multi dimension character array help
    By BrandNew in forum C Programming
    Replies: 3
    Last Post: 03-02-2011, 04:03 AM
  3. warning: multi-character character constant
    By aadil7 in forum C Programming
    Replies: 2
    Last Post: 12-12-2010, 10:02 PM
  4. multi-character character constant error
    By robwhit in forum C Programming
    Replies: 3
    Last Post: 07-15-2007, 12:12 AM
  5. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM