Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 391587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:04:34+00:00 2026-05-12T16:04:34+00:00

Suppose I have a lex regular expression like [aA][0-9]{2,2}[pP][sS][nN]? { return TOKEN; } If

  • 0

Suppose I have a lex regular expression like

[aA][0-9]{2,2}[pP][sS][nN]? { return TOKEN; }

If a user enters

A75PsN
A75PS

It will match

But if a user says something like

A75PKN

I would like it to error and say “Character K not recognized, expecting S”

What I am doing right now is just writing it like

let [a-zA-Z]
num [0-9]

{let}{num}{2,2}{let}{2,3}

And then essentially re-lexing the string in Yacc so that I can have meaningful error conditions

How can I get around this?

The only thing I can think of is to use named groups?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:04:34+00:00Added an answer on May 12, 2026 at 4:04 pm

    Wow! Interesting scheme.

    If you’re going to detect that in the lexical analyzer, you would have to have a catch-all rule that deals with ‘any otherwise unrecognized string’ and produces an error message.

    Determining that it was the K that caused the trouble is going to be hell.

    [^aA][0-9]{2,2}[pP][sS][nN]? { report_error(); return ERROR; }
    [aA][0-9]{2,2}[^pP][sS][nN]? { report_error(); return ERROR; }
    [aA][0-9]{2,2}[pP][^sS][nN]? { report_error(); return ERROR; }
    [aA][0-9]{2,2}[pP][sS][^nN]  { report_error(); return ERROR; }
    

    Note the placing of the carets, and the absence of the question mark!
    Dealing with non-digits, or too many digits, or too few digits – urgh!

    Generally, you would be better of recognizing all ‘identifiers’ and then validating which ones are OK:

    [a-zA-Z][0-9]{2,2}[a-zA-Z]{2,5} { return validate_id_string(); }
    

    Choose your poison what you allow into the validation routine; it decides whether what was entered was OK or not, and its return value controls what the Lex rule returns to the grammar. This is one way of distinguishing keywords from identifiers too.

    Generalize and simplify the regular expression to suit what really goes on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the following function like this: Image('/path/to/original.image', '1/1', '150*', './thumb.jpg');… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer Check you database schema to see if the field (referenced… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer I figured out the problem - there was a session… May 13, 2026 at 2:13 am

Related Questions

As a pet project, I was thinking about writing a program to migrate applications
I have a project for school where we need to use flex and bison.
I want to put output information of my program to a folder. if given
I must admin this is kind of funny even though I think I understand

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.