Like Tree1Likes
  • 1 Post By laserlight

What to do with multi-character operators when parsing ?

This is a discussion on What to do with multi-character operators when parsing ? within the C++ Programming forums, part of the General Programming Boards category; I thought that I'd skip multi-character operators entirely, but am having difficulties to come up with enough single-char operators for ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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!
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    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
    21,457
    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.
    whiteflags likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03:03 AM
  3. warning: multi-character character constant
    By aadil7 in forum C Programming
    Replies: 2
    Last Post: 12-12-2010, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21