4 Replies - 6264 Views - Last Post: 30 November 2010 - 08:06 PM Rate Topic: -----

#1 sm5312  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 106
  • Joined: 09-April 09

LEX - identifier token

Posted 29 November 2010 - 08:28 AM

below is a part of my lex program to match iddentifiers.

digit [0-9]
letter [A-Za-z]
%%
{letter}({letter}|{digit})* { prientif("iddentifier");} 
%%



but this still matches strings like '123abc' where they are not iddentifiers!
how to solve this?
Is This A Good Question/Topic? 0
  • +

Replies To: LEX - identifier token

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: LEX - identifier token

Posted 29 November 2010 - 10:03 AM

Does your program really say prientif("iddentifier");? (Because if it does, I doubt that you can even compile it.)

Your problem is that even though it has no rule to match "123abc", it does have a rule to match the substring "abc" so it just skips over the "123" and announces a match to the "abc". So you have to alter your rule to require that the expression starts at the beginning of any input string.
Was This Post Helpful? 0
  • +
  • -

#3 sm5312  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 106
  • Joined: 09-April 09

Re: LEX - identifier token

Posted 30 November 2010 - 06:11 AM

that should be printf()

Quote

So you have to alter your rule to require that the expression starts at the beginning of any input string.

what do you mean here?
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: LEX - identifier token

Posted 30 November 2010 - 07:18 AM

You have to use an anchor in your regex. Look at www.regular-expressions.info/anchors.html

(You'll have to cut & paste that into your browser. I don't know why, but when I try to paste that url into a link, it keeps getting broken with junk in the middle of it.)

My link

This post has been edited by r.stiltskin: 30 November 2010 - 07:33 AM

Was This Post Helpful? 0
  • +
  • -

#5 sm5312  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 106
  • Joined: 09-April 09

Re: LEX - identifier token

Posted 30 November 2010 - 08:06 PM

ok fine thanks.

This post has been edited by sm5312: 30 November 2010 - 08:07 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1