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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:42:49+00:00 2026-06-14T11:42:49+00:00

Say I have a simple grammar (lexer and parser) for parsing & evaluating simple

  • 0

Say I have a simple grammar (lexer and parser) for parsing & evaluating simple mathematical expressions (as in a few antlr examples out there) that also allows simple variable definitions (i.e. assigning float values) and use of these variables. E.g. the following can be handled:

 r = 2.5;
 PI = 3.14;
 PI * r * r;

This is supposed to be used in a more complex grammar. In fact, several different ones.
The problem is that the lexer for the above contains recognizes basically every string as token type ID, i.e. potential variable name, but the more complex grammars may contain other keywords.

If I do

lexer grammar ComplexLexer;
import SimpleMathExprLexer;
// ...
IF : 'if'|'IF';
THEN : 'then'|'THEN';
// ...

then it is not terribly helpful that ID already matches these keywords. Simply moving the import statement down below these rules does not work. Is there any way around this, or am I on the wrong path entirely when I look at composition?

  • 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-14T11:42:50+00:00Added an answer on June 14, 2026 at 11:42 am

    ANTLR will simply give precedence to the rule defined first. This means that if you have a lexer grammar G, that imports lexer grammars G3 and after that grammar G2:

    lexer grammar G;
    
    import G3, G2;
    
    ...
    

    or similarly:

    lexer grammar G;
    
    import G3;
    
    ...
    

    lexer grammar G3;
    
    import G2;
    
    ...
    

    the rules from G3 would get precedence over G2, but the rules from G would get precedence over both G3 and G2.

    Just as with single lexer grammars, the rules like IDENTIFIER you’d define after rules that match keywords like "if", "then", …

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

Sidebar

Related Questions

I'm working with antlr 3.2. I have a simple grammar that consists of atoms
Say I have simple program that emulates a board game with a number of
Say you have a simple loop while read line do printf ${line#*//}\n done <
Say I have a simple address class like below: public class Address { public
Say I have this simple method: public IEnumerable<uint> GetNumbers() { uint n = 0;
Say I have a simple table that contains username, firstname, lastname. How do I
Say I have a simple object such as class Something { public int SomeInt
Say I have a simple client/server scenario with one method: // client code $client
Say you have a simple class with some storage data structure (list, vector, queue,
Say I have a simple object which supports implicit casting to System.String public sealed

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.