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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:03:03+00:00 2026-05-21T03:03:03+00:00

JsLex is a Javascript lexer I’ve written in Python. It does a good job

  • 0

JsLex is a Javascript lexer I’ve written in Python. It does a good job for a day’s work (or so), but I’m sure there are cases it gets wrong. In particular, it doesn’t understand anything about semicolon insertion, and there are probably ways that’s important for lexing. I just don’t know what they are.

What Javascript code does JsLex lex incorrectly? I’m especially interested in valid Javascript source where JsLex incorrectly identifies regex literals.

Just to be clear, by “lexing” I mean identifying tokens in a source file. JsLex makes no attempt to parse Javascript, much less execute it. I’ve written JsLex to do full lexing, though to be honest I would be happy if it merely was able to successfully find all the regex literals.

  • 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-21T03:03:03+00:00Added an answer on May 21, 2026 at 3:03 am

    Interestingly enough I tried your lexer on the code of my lexer/evaluator written in JS 😉 You’re right, it is not always doing well with regular expressions. Here some examples:

    rexl.re = {
      NAME: /^(?!\d)(?:\w)+|^"(?:[^"]|"")+"/,
      UNQUOTED_LITERAL: /^@(?:(?!\d)(?:\w|\:)+|^"(?:[^"]|"")+")\[[^\]]+\]/,
      QUOTED_LITERAL: /^'(?:[^']|'')*'/,
      NUMERIC_LITERAL: /^[0-9]+(?:\.[0-9]*(?:[eE][-+][0-9]+)?)?/,
      SYMBOL: /^(?:==|=|<>|<=|<|>=|>|!~~|!~|~~|~|!==|!=|!~=|!~|!|&|\||\.|\:|,|\(|\)|\[|\]|\{|\}|\?|\:|;|@|\^|\/\+|\/|\*|\+|-)/
    };
    

    This one is mostly fine – only UNQUITED_LITERAL is not recognized, otherwise all is fine. But now let’s make a minor addition to it:

    rexl.re = {
      NAME: /^(?!\d)(?:\w)+|^"(?:[^"]|"")+"/,
      UNQUOTED_LITERAL: /^@(?:(?!\d)(?:\w|\:)+|^"(?:[^"]|"")+")\[[^\]]+\]/,
      QUOTED_LITERAL: /^'(?:[^']|'')*'/,
      NUMERIC_LITERAL: /^[0-9]+(?:\.[0-9]*(?:[eE][-+][0-9]+)?)?/,
      SYMBOL: /^(?:==|=|<>|<=|<|>=|>|!~~|!~|~~|~|!==|!=|!~=|!~|!|&|\||\.|\:|,|\(|\)|\[|\]|\{|\}|\?|\:|;|@|\^|\/\+|\/|\*|\+|-)/
    };
    str = '"';
    

    Now all after the NAME's regexp messes up. It makes 1 big string. I think the latter problem is that String token is too greedy. The former one might be too smart regexp for the regex token.

    Edit: I think I’ve fixed the regexp for the regex token. In your code replace lines 146-153 (the whole ‘following characters’ part) with the following expression:

    ([^/]|(?<!\\)(?<=\\)/)*
    

    The idea is to allow everything except /, also allow \/, but not allow \\/.

    Edit: Another interesting case, passes after the fix, but might be interesting to add as the built-in test case:

        case 'UNQUOTED_LITERAL': 
        case 'QUOTED_LITERAL': {
            this._js =  "e.str(\"" + this.value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + "\")";
            break;
        }
    

    Edit: Yet another case. It appears to be too greedy about keywords as well. See the case:

    var clazz = function() {
        if (clazz.__) return delete(clazz.__);
        this.constructor = clazz;
        if(constructor)
            constructor.apply(this, arguments);
    };
    

    It lexes it as: (keyword, const), (id, ructor). The same happens for an identifier inherits: in and herits.

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

Sidebar

Related Questions

Does there exist a parser that generates an AST/parse tree at runtime? Kind of
There are many lexical analyzer and parser generators out there - lex/flex and yacc/bison,
I am looking for a parser generator for Java that does the following: My
I'm writing a custom flex file to generate a lexer for use with JSyntaxpane.
I'm fairly new to JFlex and JSyntaxPane although I have managed to hack together
I'm working on a compiler design project in Java. Lexical analysis is done (using
I'm looking to create an Intellij IDEA language support plugin for Erlang. The first
I'm wondering if Scalas/Haskells parser combinators are sufficient for parsing a programming language. More
I am new to JFlex. I have gathered that JFlex is a parser generator.
During the Compiler Design Lab hours, I'm using JLex as the Lexical Analyzer Generator,

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.