#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    A problem with Lex


    Hi,

    I have some problem with Lex

    if I write 1 + 1 I'm getting:
    Found token INT ('1')
    Found token OP ('+')
    Found token INT ('1')

    If I write 1+1 I'm getting:
    Found token INT ('1')
    Found token INT ('+1')

    +1 is part of the language, what do I need to change so if I will write 1+1 I will get same as if I will write 1 + 1?

    this is my regular expressions:
    SIGN [+-]
    DIGIT [0-9]
    DIGIT_NZ [1-9]
    INTEGER ([0])|(({SIGN}{0,1})({DIGIT_NZ}{1})({DIGIT}*))

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Posts
    47
    Rep Power
    0
    Sounds like you need to ignore whitespace when you parse the string.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    You should not parse the sign with INTEGER and resolve this with your parser by having rule like

    NUMBER ::= INTEGER | ADDOP INTEGER

    Lex only deals with tokens in ignorance of the surrounding context. It can't tell when a '-' is a subtraction operator vs a unary negation (well you could have your parser screw around with the lexer mid-parse like perl does, but I don't even if that's possible in lex and in any case it would be horribly complicated).
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);

IMN logo majestic logo spyfu logo threadwatch logo seochat tools logo