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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:15:02+00:00 2026-06-09T06:15:02+00:00

I’m making a simple parser for some Java-like language (just for learning purposes). I’m

  • 0

I’m making a simple parser for some Java-like language (just for learning purposes). I’m having trouble determining whether a statement is a variable declaration. This may be a problem with my lexer (which is really sloppy). If the lexer sees some text, it simply labels it an identifier, even if that text is a keyword or a type. The job of telling those apart is given to the parser.

This has worked so far, but now I’m trying to parse variable declarations, like this one here:

int x = 3;

The problem is I don’t know how to determine whether this is a variable declaration. If I just look at the first token and find that it’s an “identifier”, that doesn’t tell me anything, since this line of code also starts with an identifier:

System.out.print("hi");

And statements like this are handled by another part of the parser.

Another solution I thought about was checking to see if the first token is a type. For example, I could have a method that looks something like:

boolean isType(String t) {
    if( t.equals("int")  ||
        t.equals("long") ||
        t.equals("char") ||
        /* et cetera */ )
        return true;
    else return false;
} 

The problem with this is that it only allows for a certain set of types. Since my little language is compiled to Java bytecode, I need it to recognize arbitrary classes as types.

So my question is: is it possible to determine whether a statement is a variable declaration or not, without knowing all the possible variable types?

  • 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-09T06:15:04+00:00Added an answer on June 9, 2026 at 6:15 am

    I’ve had to do something of this flavor for a class, about 4 years ago. Although I don’t remember all the details of the “official” way to do it, but

    What I would do, is I would look ahead, at the future symbols to determine whether or not it’s a variable declaration, so as Benjamin Gruenbaum said, if you see a legal identifier(at the beginning of a line) followed by another legal identifier, than the first one is probably a variable declaration.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a

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.