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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:24:09+00:00 2026-06-10T18:24:09+00:00

I have an ANTLR JavaScript grammar (taken from the Internet), which seem to support

  • 0

I have an ANTLR JavaScript grammar (taken from the Internet), which seem to support everything except for regex literals.

The problem with a regex literal is that you have two rules, essentially:

multiplicativeExpression
    : unaryExpression (LT!* ('*' | '/' | '%')^ LT!* unaryExpression)*

and

regexLiteral
    : '/' RegexLiteralChar* '/'

where the rule RegexLiteralChar uses different lexer rules than a normal expression (eg. a double quote does not terminate it).

This means that I need to, in some way, change some kind of lexer state from my parser. How can I do this? Is it even possible?

  • 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-10T18:24:11+00:00Added an answer on June 10, 2026 at 6:24 pm

    Looking at the grammar mentioned in the comment by Bart Kiers here, you can see this comment,

    The major challenges faced in defining this grammar were:

    -1- Ambiguity surrounding the DIV sign in relation to the multiplicative expression and the regular expression literal. This is
    solved with some lexer driven magic: a gated semantical predicate
    turns the recognition of regular expressions on or off, based on the
    value of the RegularExpressionsEnabled property. When regular
    expressions are enabled they take precedence over division
    expressions. The decision whether regular expressions are enabled is
    based on the heuristics that the previous token can be considered as
    last token of a left-hand-side operand of a division.

    …

    The areRegularExpressionsEnabled() function is defined as,

    private final boolean areRegularExpressionsEnabled()
    {
        if (last == null)
        {
            return true;
        }
        switch (last.getType())
        {
        // identifier
            case Identifier:
        // literals
            case NULL:
            case TRUE:
            case FALSE:
            case THIS:
            case OctalIntegerLiteral:
            case DecimalLiteral:
            case HexIntegerLiteral:
            case StringLiteral:
        // member access ending 
            case RBRACK:
        // function call or nested expression ending
            case RPAREN:
                return false;
        // otherwise OK
            default:
                return true;
        }
    }
    

    And then the function is used in the RegularExpressionLiteral expression,

    RegularExpressionLiteral
        : { areRegularExpressionsEnabled() }?=> DIV RegularExpressionFirstChar RegularExpressionChar* DIV IdentifierPart*
        ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have lexer/parser (Generated from an ANTLR grammar file) which (for performance reasons) I
I have a simple ANTLR grammar, which I have stripped down to its bare
i have an problem with antlr. I have the following simple grammar: grammar bxk;
Hi I have a small problem in my ANTLR tree grammar. I am using
I have the following ANTLR grammar which is given as an example by Terrence
I have an ANTLR grammar which creates an AST, and then I have written
I have an ANTLR grammar file as part of a C# project file and
I have really simple XML (HTML) parsing ANTLR grammar: wiki: ggg+; ggg: tag |
I have a project containing files generated from a .g file (antlr 2.7.x). The
By default, a parser generated using ANTLR-3 extends from org.antlr.runtime.Parser. How do I have

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.