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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:04:02+00:00 2026-05-26T06:04:02+00:00

I am trying to do a lexer for a subset of Java with JavaCC.

  • 0

I am trying to do a lexer for a subset of Java with JavaCC. And a variable name can be any combination of letter, digit and _, beginning with a letter. I have only one problem, reserved words (such as int, new, …) can not be used as a variable name and I was wondering how to declare this. Right now I have this where the reserved words are declared first, and then the rule for variable names, is it enought and then it will be to the parser to deal with it ?

//Reserved words
TOKEN:{
  < TOK_BOOLEAN : "boolean" > |
  < TOK_BREAK : "break" > |
  < TOK_CLASS : "class" >
}

TOKEN:{
  < TOK_ID : <LETTER> (<LETTER>|<DIGIT>|"_")+ > |
  < #DIGIT : ["0"-"9"] > |
  < #LETTER : ["a"-"z"] | ["A"-"Z"] >
}

TOK_ID is the rule for variable name.

Thank you and ask me if something is not clear.

  • 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-26T06:04:03+00:00Added an answer on May 26, 2026 at 6:04 am

    The JavaCC lexer chooses the first definition that gains the longest match, so your definition should be sufficient.

    This behavior is documented in the JavaCC TokenManager Tutorial. The JavaCC FAQ explains it here.

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

Sidebar

Related Questions

Writing a lexer of .java source files in Java. I have a stream of
I am trying to create a simple parser/lexer, my problem is I have to
Trying to make a make generic select control that I can dynamically add elements
I'm trying to build a simple lexer/parser with Alex/Happy in Haskell, and I would
I'm trying to use Flex with Visual C++. However, the generated lexer (which is
Is there any way to return multiple tokens in OCamlLex? I'm trying to write
Trying to figure out a way where I can pass some data/fields from a
I am trying to make a simple Lexer to understand how they work. I
I'm newbie to flex. I'm trying to write a simple re-entrant lexer/scanner with flex.
So I am trying to write a compiler in F# and have been looking

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.