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

  • SEARCH
  • Home
  • 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 7647269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:25:18+00:00 2026-05-31T10:25:18+00:00

I have some constants defined in a header file that contain the max length

  • 0

I have some constants defined in a header file that contain the max length of certain strings to be parsed by a file parser written in flex/bison. I want to move the code for checking string lengths from the c code to my regular expressions to make things a bit more concise.

Right now I have rules that look like:

[[:alnum:]]+ { yylval.sval = (char*) strdup(yytext); return STRING; }

Where the length check takes place in the bison rules.

I want to modify this so that it checks for a match no longer than MAX_STR_LEN which is defined in a header called constants.h. If MAX_STR_LEN is equal to 32 than I would want the same effect as:

[[:alnum:]]{1,32}

Is there anyway to do this without running my flex file through an additional step of preprocessing?

EDIT:

The following rule will fail because MAX_STR_LEN is not a literal number, it is seen as a string so flex thinks that 2 actions have been defined for a single rule.

[[:alnum:]]{1,MAX_STR_LEN} { do_something(); }

Additionally if one tries to define a macro in the declarations part of the flex file than it also fails as such.

max_len 32 /* Also fails if 32 is replaces with MAX_STR_LEN */
%%
[[:alnum:]]{1,max_len} { do_something(); }
  • 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-31T10:25:20+00:00Added an answer on May 31, 2026 at 10:25 am

    You could deal with it in the action rule:

    [[:alnum:]]+    { if (yyleng > MAX_STR_LEN) yyless(MAX_STRING_LEN);
                      yylval.sval = (char*) strdup(yytext); return STRING; }
    

    This makes the rule effectively the same as your {1,MAX_STR_LEN} modifier on the pattern

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

Sidebar

Related Questions

I have some constants that represent the valid options in one of my model's
Possible Duplicate: Avoiding repeated constants in CSS We have some theme colors that are
I have a lot of constants that are somehow related, at some point I
I have structure defined in some header (D3DXVECTOR3) How can I declare: static member
I have defined and enum in cython header file api.pxd : ctypedef enum InstructionType:
I have a .h file in which hundreds of constants are defined as macros:
I have a simple class that defines some constants, e.g.: module Foo class Bar
I'm using MySQL 4.1. Some tables have duplicates entries that go against the constraints.
I have some UI in VB 2005 that looks great in XP Style, but
I have a Owners class defined that is used just fine in existing classes.

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.