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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:31:47+00:00 2026-05-28T05:31:47+00:00

Some keywords (string constant) in my grammar contain capital letters e.g. PREV_VALUE : ‘PreviousValue’;

  • 0

Some keywords (string constant) in my grammar contain capital letters
e.g.

PREV_VALUE : 'PreviousValue';

This causes strange parsing behavior: other tokens that contain same capital letters (‘P’,’V’) are parsed incorrectly.

Here’s a simplified version of the lexer grammar:

lexer grammar ExpressionLexer;

COMMA : ',';
LPAREN : '(';
RPAREN : ')';
LBRACK : '[';
RBRACK : ']';
PLUS : '+';
MINUS : '-';
MULT : '*';
DIV : '/';
PREV_VALUE : 'PreviousValue';
fragment DIGIT : ('0'..'9');
fragment LETTER : ('a'..'z'|'A'..'Z'|'_');
fragment TAB : ('\t') ;
fragment NEWLINE : ('\r'|'\n') ;
fragment SPACE : (' ') ;

When I try parsing such expression:

var expression = "P"; //Capital 'P' which included to the keyword 'PreviousValue'
var stringReader = new StringReader(expression);
var input = new ANTLRReaderStream(stringReader);
var expressionLexer = new ExpressionLexer(input);
var tokens = new CommonTokenStream(expressionLexer);

tokens._tokens collection contains one value

[0] = {[@0,1:1='<EOF>',<-1>,1:1]}

It’s incorrect.

If I change expression to ‘p’ (lowercase letter)
tokens._tokens collection contains two values

[0] = {[@0,0:0='p',<0>,1:0]}
[1] = {[@1,1:1='<EOF>',<-1>,1:1]}

It’s correct.

When string PREV_VALUE : 'PreviousValue'; is removed from grammar, both expressions are parsed correctly.

Is it possible to use different case in keywords?
Is there any example of using such keywords in ANTLR grammar?

  • 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-28T05:31:48+00:00Added an answer on May 28, 2026 at 5:31 am

    I find it hard to believe a p token is created based on the grammar you posted. Lexer rules that have fragment in front of them will not produce tokens: these rules are only used by other lexer rules.

    A simple demo shows this:

    lexer grammar ExpressionLexer;
    
    @lexer::members {
      public static void main(String[] args) throws Exception {
        ExpressionLexer lexer = new ExpressionLexer(new ANTLRStringStream(args[0]));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        tokens.fill(); // remove this line when using ANTLR 3.2 or an older version
        System.out.println(tokens);
      }
    }
    
    COMMA : ',';
    LPAREN : '(';
    RPAREN : ')';
    LBRACK : '[';
    RBRACK : ']';
    PLUS : '+';
    MINUS : '-';
    MULT : '*';
    DIV : '/';
    PREV_VALUE : 'PreviousValue';
    fragment DIGIT : ('0'..'9');
    fragment LETTER : ('a'..'z'|'A'..'Z'|'_');
    fragment TAB : ('\t') ;
    fragment NEWLINE : ('\r'|'\n') ;
    fragment SPACE : (' ') ;
    

    Now generate the lexer and compile the .java source file:

    java -cp antlr-3.3.jar org.antlr.Tool ExpressionLexer.g 
    javac -cp antlr-3.3.jar *.java

    and run a few tests:

    java -cp .:antlr-3.3.jar ExpressionLexer p
    line 1:0 no viable alternative at character 'p'

    which is correct since there is no (non-fragment) rule that starts with, or matches, a "p".

    java -cp .:antlr-3.3.jar ExpressionLexer P
    line 1:1 mismatched character '' expecting 'r'

    which is correct since the only (non-fragment) rule that starts with a "P" expects an "r" to be the next character (which isn’t there).

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

Sidebar

Related Questions

I have some keywords in one string splitted with . I have also table
So I have some data that looks like this: stringToSearch = 'this string needs
YouTube has this cool thing that creates a smart playlist from some starting keywords.
Why are some keywords highlighted blue and some gray in SQL Server Management Studio?
I have a longer text and some keywords. I want to highlight these keywords
Is it possible to have something like a JTextArea which will color some keywords
On some pages, I put keywords within anchor tags. What's better to use for
I need to dynamically build a Regex to catch the given keywords, like string
I am trying to parse the keywords from google suggest, this is the url:
I am trying to construct search method by accepting some keywords as a GET

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.