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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:24:52+00:00 2026-05-10T19:24:52+00:00

Just for my own purposes, I’m trying to build a tokenizer in Java where

  • 0

Just for my own purposes, I’m trying to build a tokenizer in Java where I can define a regular grammar and have it tokenize input based on that. The StringTokenizer class is deprecated, and I’ve found a couple functions in Scanner that hint towards what I want to do, but no luck yet. Anyone know a good way of going about this?

  • 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. 2026-05-10T19:24:53+00:00Added an answer on May 10, 2026 at 7:24 pm

    The name ‘Scanner’ is a bit misleading, because the word is often used to mean a lexical analyzer, and that’s not what Scanner is for. All it is is a substitute for the scanf() function you find in C, Perl, et al. Like StringTokenizer and split(), it’s designed to scan ahead until it finds a match for a given pattern, and whatever it skipped over on the way is returned as a token.

    A lexical analyzer, on the other hand, has to examine and classify every character, even if it’s only to decide whether it can safely ignore them. That means, after each match, it may apply several patterns until it finds one that matches starting at that point. Otherwise, it may find the sequence ‘//’ and think it’s found the beginning of a comment, when it’s really inside a string literal and it just failed to notice the opening quotation mark.

    It’s actually much more complicated than that, of course, but I’m just illustrating why the built-in tools like StringTokenizer, split() and Scanner aren’t suitable for this kind of task. It is, however, possible to use Java’s regex classes for a limited form of lexical analysis. In fact, the addition of the Scanner class made it much easier, because of the new Matcher API that was added to support it, i.e., regions and the usePattern() method. Here’s an example of a rudimentary scanner built on top of Java’s regex classes.

    import java.util.*; import java.util.regex.*;  public class RETokenizer {   static List<Token> tokenize(String source, List<Rule> rules)   {     List<Token> tokens = new ArrayList<Token>();     int pos = 0;     final int end = source.length();     Matcher m = Pattern.compile('dummy').matcher(source);     m.useTransparentBounds(true).useAnchoringBounds(false);     while (pos < end)     {       m.region(pos, end);       for (Rule r : rules)       {         if (m.usePattern(r.pattern).lookingAt())         {           tokens.add(new Token(r.name, m.start(), m.end()));           pos = m.end();           break;         }       }       pos++;  // bump-along, in case no rule matched     }     return tokens;   }    static class Rule   {     final String name;     final Pattern pattern;      Rule(String name, String regex)     {       this.name = name;       pattern = Pattern.compile(regex);     }   }    static class Token   {     final String name;     final int startPos;     final int endPos;      Token(String name, int startPos, int endPos)     {       this.name = name;       this.startPos = startPos;       this.endPos = endPos;     }      @Override     public String toString()     {       return String.format('Token [%2d, %2d, %s]', startPos, endPos, name);     }   }    public static void main(String[] args) throws Exception   {     List<Rule> rules = new ArrayList<Rule>();     rules.add(new Rule('WORD', '[A-Za-z]+'));     rules.add(new Rule('QUOTED', '\'[^\']*+\''));     rules.add(new Rule('COMMENT', '//.*'));     rules.add(new Rule('WHITESPACE', '\\s+'));      String str = 'foo //in \'comment\'\nbar \'no //comment\' end';     List<Token> result = RETokenizer.tokenize(str, rules);     for (Token t : result)     {       System.out.println(t);     }   } } 

    This, by the way, is the only good use I’ve ever found for the lookingAt() method. 😀

    • 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

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 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

Just for my own purposes, I'm trying to build a tokenizer in Java where
OK, so I have a .NET project that uses plugins. The plugins are implemented
How do I attach an onclick event to a link_to_function such that clicking on
Every Christmas we draw names for gift exchanges in my family. This usually involves
We maintain a large number of .NET Project and Solution files by hand; it's

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.