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 8600229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:34:13+00:00 2026-06-12T01:34:13+00:00

This web page suggests that if your lex program has a large number of

  • 0

This web page suggests that if your lex program “has a large number of reserved words, it is more efficient to let lex simply match a string and determine in your own code whether it is a variable or reserved word.”

My question is: More efficient where, and why? If it means compiling the lexer is faster, I don’t really care about that because it is one step removed from the program which uses the lexer to parse input.

It seems to be that lex just uses your description to build a state machine that processes one character at a time. It does not seem logical that increasing the size of the state machine would necessarily cause it to become any slower than using one rule for identifiers and then doing several string comparisons.

Additionally, if it turns out that there is some logical reason for this to make sense as an optimization, what would be considered a large number of reserved words? I have approximately 20, as compared to about 30 other rules for various things. Would that be considered a large number of reserved words? Should I attempt to use the same strategy for some of the other symbols?

I have attempted to google for a result, but the only relevant articles I found stated this strategy as though it were well-known without giving any reason.

In case it is relevant, I am using flex 2.5.35.

Edit: Here is another reference which claims that lex produces an inefficient scanner when asked to match several long literal strings. It also does not give a reason.

  • 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-06-12T01:34:14+00:00Added an answer on June 12, 2026 at 1:34 am

    According to the flex manual, “[t]he speed of the scanner is independent of the number of rules or … how complicated the rules are with regard to operators such as ‘*’ and ‘|’.”

    The main performance losses are due to backtracking. This can be avoided by (among other things) using catch-all rules which will match tokens which “start with” the offending token. For example, if you have a list of reserved words consisting of [a-zA-Z_], and then a rule for matching identifiers of the form [a-zA-Z_][a-zA-Z_0-9]*, the rule for matching identifiers will catch any identifiers which start with the name of a reserved word without having to back up and try to match again.

    According to the faq, flex generates a deterministic finite automaton which “does all the matching simultaneously, in parallel.” The result of this is, as was said above, that the speed of the scanner is independent of the number of rules. On the other hand, string comparison is linear in the number of rules.

    As a result, the reserved word rules should, in fact, be considerably faster than a lookup table.

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

Sidebar

Related Questions

I want to embed this gadget in my web page. That's why I use
I have a web page that has a button that when clicked will download
I have a web page. When this web page is loaded, I want to
I want to get the text field test out of this web page. How
This is how my web page looks like: PosisionDataView is a web user control,
My web page returns an error for the calls to mysql_connect() for this Mac
web page : http://sideradesign.com/eco-art/gallery/ This code works fine in all brwosers except IE8 (haven't
This is my code to get a web page's image's URLs for some webpage,
Say I have a web page like this on an Ubuntu 12.04 web server
I'm reading a web page using HttpClient like this: httpclient = new DefaultHttpClient(); httpget

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.