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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:40:34+00:00 2026-06-10T05:40:34+00:00

I am developing a lexer grammar for C/C++ source code. The goal of the

  • 0

I am developing a lexer grammar for C/C++ source code. The goal of the grammar is to fight plagiarism between students at university.

To improve the effectiveness of the grammar, I want ANTLR to create the same token for the 4(?) different ways a student could increment a variable:

i++
++i
i += 1
(i = i + 1) [I doubt that this can be solved with ANTLR]

Each of these expressions should result in the token INCREMENT.

What I have come up with so far: (only the neccessary parts of the grammar are reproduced here)

options {
    language = CSharp3;
    filter = true;
    k = 2;
}

INCREMENT : IDENTIFIER (PLUSPLUS | ADDEQUAL '1') | PLUSPLUS IDENTIFIER ;
IDENTIFIER 
    :   LETTER (LETTER | DIGIT)*;

/*
 * covers both decimal and hex integer literals
 */
INTEGER_LITERAL : 
   DIGIT+ | '0x' HEX_DIGIT+;

ADDEQUAL            : '+=';
PLUSPLUS            : '++';

fragment
LETTER  :   'A'..'Z' | 'a'..'z';

fragment
HEX_DIGIT : DIGIT | 'a'..'f' | 'A'..'F';

fragment
DIGIT : '0'..'9';

testing this grammar for i += 1 results in the token sequence IDENTIFIER ADDEQUAL INTEGER_LITERAL instead of INCREMENT.

Why is that?
From my understanding the precedence of rules is top to bottom plus INCREMENT is the “bigger” rule.

What adjustments to the grammar need I make to get the desired result?

  • 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-10T05:40:36+00:00Added an answer on June 10, 2026 at 5:40 am

    testing this grammar for i += 1 results in the token sequence IDENTIFIER ADDEQUAL INTEGER_LITERAL instead of INCREMENT.

    Why is that?

    Because "i += 1" contains spaces you didn’t account for inside your INCREMENT rule.

    What adjustments to the grammar need I make to get the desired result?

    Account for the spaces (and line breaks, possibly).

    However, creating a lexer alone does not seem the way to go here. You really need a parser, IMO. And the option k = 2; sets look ahead for parser rules, not lexer rules: so in case you stick to lexing only, you mind as well remove it.

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

Sidebar

Related Questions

Developing websites are time-consuming. To improve productivity, I would code a prototype to show
Developing an application for Android, i want to record data that will be usefull
Developing a chrome extension using javascript is one of my university projects. I don't
Developing mobile apps is a challenging job. Customers want to be present not only
Developing server side code i finally got my eyes X-crossed trying to write -
Developing for Android 2.3, I have a question regarding layouts. I use a vertival
Developing a project of mine I realize I have a need for some level
Developing a C# .NET 2.0 WinForm Application. Need the application to close and restart
Developing a project in Django with my IDE setup as Eclipse with PyDev. The
Iam developing one application.In that iam placing the radio buttons(uiimageview) on table view and

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.