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

  • Home
  • SEARCH
  • 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 227309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:35:15+00:00 2026-05-11T19:35:15+00:00

I’m looking to tokenize Java/Javascript-like expressions in Javascript code. My input will be a

  • 0

I’m looking to tokenize Java/Javascript-like expressions in Javascript code. My input will be a string containing the expression, and the output needs to be an array of tokens.

What’s the best practice for doing something like this? Do I need to iterate the string or is there a regular expression that will do this for me?

I need this to be able to support:

  • Number and String literals (single and double quoted, with quote escaping)
  • Basic mathematical and boolean operators and comparators (+, -, *, /, !, and, not, <, >, etc)
  • Dot and bracket notation for object access with recursion (foo.bar, foo[‘bar’], foo[2][prop])
  • Parenthesis with nesting
  • Ternary operator (foo ? bar : ‘baz’)
  • Function calls (foo(bar))

I specifically want to avoid using eval() or anything of the sort for security reasons. Besides, eval() wouldn’t tokenize the expression for me anyway.

  • 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-11T19:35:15+00:00Added an answer on May 11, 2026 at 7:35 pm

    Learn to write a recursive-descent parser. Once you understand the concepts, you can do it in any language: Java, C++, JavaScript, SystemVerilog, … whatever. If you can handle strings then you can parse.

    Recursive-descent parsing is a basic technique for parsing that can easily be coded by hand. This is useful if you don’t have access to (or don’t want to fool with) a parser generator.

    In a recursive-descent parser, every rule in your grammar is translated to a procedure that parses the rule. If you need to refer to other rules, then you do so by calling them – they’re just procedures.

    A simple example: expressions involving numbers, addition and multiplication (this illustrates operator precedence). First, the grammar:

    expr ::= term
             | expr "+" term
    
    term ::= factor
             | term "*" factor
    
    factor ::= /[0-9/+ (I'm using a regexp here)
    

    Now to write the parser (which includes the lexer; with recursive-descent you can throw the two together). I’ve never used JavaScript, so let’s try this in (my rusty) Java:

    class Parser {
      string str;
      int idx; // index into string
    
      Node parseExpr() throws ParseException
      {
        Node op1 = parseTerm();
        Node op2;
    
        while (idx < str.size() && str.charAt(idx) == '+') {
          idx++;
          op2 = parseTerm();
          op1 = new AddNode(op1, op2);
        }
        return op1;
      }
    
      Node parseTerm() throws ParseException
      {
        Node op1 = parseFactor();
        Node op2;
    
        while (idx < str.size() && str.charAt(idx) == '*') {
          idx++;
          op2 = parseFactor();
          op1 = new MultNode(op1, op2);
        }
        return op1;
      }
    
      Node parseFactor() throws ParseException
      {
        StringBuffer sb = new StringBuffer();
        int old_idx = idx;
    
        while (idx < str.size() && str.charAt(idx) >= '0' && str.charAt(idx) <= '9') {
          sb.append(str.charAt(idx));
          idx++;
        }
        if (idx == old_idx) {
          throw new ParseException();
        }
        return new NumberNode(sb.toString());
      }
    }
    

    You can see how each grammar rule translates into a procedure. I haven’t tested this; that’s an exercise for the reader.

    You also need to worry about error detection. A real-world compiler needs to recover from parse errors to try to parse the remainder of its input. A one-line expression parser like this one does not need to try recovery at all, but it does need to determine that a parse error exists and flag it. The easiest way to do this if your language allows it is to throw an exception, and catch it at the entry point to the parser. I haven’t detected all possible parse errors in my example above.

    For more info, look up “LL parser” and “Recursive descent parser” in Wikipedia. As I said at the beginning, if you can understand the concepts (and they’re simple compared to the concepts behind LALR(1) state machine configuration closures) then you are empowered to write a parser for small tasks in any language, as long as you have some rudimentary string capability. Enjoy the power.

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

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 117k
  • 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 The question's been answered guys: Javascript image scroller not working… May 11, 2026 at 10:45 pm
  • Editorial Team
    Editorial Team added an answer The operation triggers SIGFPE: SIG is a common prefix for… May 11, 2026 at 10:45 pm
  • Editorial Team
    Editorial Team added an answer This copies the first distinct simpleType of any name but… May 11, 2026 at 10:45 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.