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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:21:56+00:00 2026-06-06T11:21:56+00:00

I’ve posted similar question here , but it was closed because I didn’t explained

  • 0

I’ve posted similar question here, but it was closed because I didn’t explained myself properly.
I’ll try to explain my problem again.

I managed to write LexicalAnalyzer that will tokenize the following to “public”, “class”, “A”, “{“,…,”if”,”(“,…,”}”

string seq = "public class A " +
             "{ " +
                  "public A() " +
                  "{ " +
                       "if(1==2) " +
                       "{ " +
                       "} " +
                       "else " +
                       "{ " +
                       "} " +
                 "} " +
             "} "

Now, I need to parse it to tree. As I read, it’s better to construct the parser in the way that will take rules. Meanwhile I need to write rules for “if” statement” that will be passed to parser and the last will build the parse tree. In future, I’ll add rules for “class” and other.

To parse I mean that eventually I will get similar tree like here in the right

My question is how to implement the rules and the parser? Can you direct me or give simple example please?

I’ve read some posts, but I didn’t found something that will help me to do what I need.

P.S. If it’s still unclear, please don’t close the post, but tell me and I’ll change it.

Thank you

  • 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-06T11:21:59+00:00Added an answer on June 6, 2026 at 11:21 am

    Since when is RTFM an answer? Anyways. What you are trying to do is not at all easy, since Java is context free (type-2). Try, for starters writing a syntax-analyser for a language of type-3 (Chomsky Hierarchy). But I’ll try to explain to you what you’d need to do anyways.

    You would have to define rules for Java which look like that (in my example I’ll define a function within a java class, where lowercase letters are terminals and uppercase letters are non-terminals). Terminals cannot be derived any further while non-terminals can.

    X -> Y means X derives to Y.
    X -> Y | Z means X derives either to Y or Z.

    f is any name.
    t is a Type, this would not be a terminal, if I’d try to go all the way, but since I define types to be non-declarable to make my life less of a pain, it’s a terminal.
    ‘(‘, ‘)’, ‘{‘, ‘}’, ‘,’ and ‘ ‘ are terminals.
    Eps is Epsilon and means nothing.

    S -> K t f(T) { N }
    T -> t f | t f , T
    F -> F, f | f
    K -> k K | k
    N -> L N | L
    L -> f(F);
    

    With this I could parse

    final boolean equals(Object obj) {
        compare(this, obj);
        compare(obj, this);
    }
    

    Which would result in:

    S -> K t f(T) { N } 
         with K -> k
      -> k t f(T) { N }
         with T -> t f
      -> k t f(t f) { N }
         with N -> L N
      -> k t f(t f) { L N }
         with L -> f(F);
      -> k t f(t f) { f(F); N }
         with F -> f, F
      -> k t f(t f) { f(f, F); N }
         with F -> f
      -> k t f(t f) { f(f, f); N }
         with N -> L
      -> k t f(t f) { f(f, f); L }
         with L -> f(F)
      -> k t f(t f) { f(f, f); f(F) }
         ...
      -> k t f(t f) { f(f, f); f(f, f); }
    
      -> k (=final) t(=boolean) f(=equals) (t(=Object) f(=obj)) { ... }
    

    Which proves that S defines my simplyfied java (Well it doesn’t, but at least I gave an example). So the next thing we have to do is figure out how to get a syntax-tree out of these rules.

    Thankfully, this is the easy part, because all you have to do is change the lines to be a tree. So S has children K t f(T) { N }. K has children K and k… Uppercase means a Node has children, Lowercase says it doesn’t.

    Last problem, you do not start with S but you start with the code already being written. Which leaves you with

    K t f(T) { N } -> S
    t f            -> T
    t f , T        -> T
    F, f           -> F
    f              -> F
    k K            -> K
    k              -> K
    L N            -> N
    N              -> L
    f(F);          -> L
    

    Parsing in reverse would look like this:

    final boolean equals(Object obj) {
       compare(this, obj);
       compare(obj, this);
    }
    final   -> k
    boolean -> t
    equals  -> f
    Object  -> t
    obj     -> f
    compare -> f
    this    -> f
    
    k t f(t f) { f(f, f); f(f,f); }
    with k -> K
    K t f(t f) ...
    with t f -> T
    K t f(T) ...
    ...
    

    Which would build the tree from down below.

    • 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 want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I want to construct a data frame in an Rcpp function, but when I

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.