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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:10:53+00:00 2026-06-03T14:10:53+00:00

I have a script language based on Antlr: A parser and a tree grammar

  • 0

I have a script language based on Antlr: A parser and a tree grammar that builds runtime objects (e.g. statements). When I deal with the statements at runtime, I want to know the original source positions (e.g. when I throw errors, I want to state the line and position in the script source.)

What is the best strategy to attach the source positions to my runtime objects? And if I’m not asking too much, I want to have as little impact on my grammar files as possible.

I have tried to put as little code into the grammar as possible to increase quality, e.g. one of my (many) expressions looks like this:

multiplyExpression returns [Expression value]
: ^('*' l=expression r=expression)
{
    $value = sb.newBinaryExpression(CorIdentifier.MULTIPLY, $l.value, $r.value);
}
;

where sbis my ScriptBuilder that acts as an adapter between the generated code and my runtime. I know I can add the source position as an additional parameter to newBinaryExpressionbut then I have to touch all other expressions as well. I was hoping that I can put the token stream into sb only once and fetch the source position from the stream without affecting the grammar source at all.

I was hoping that, since Antlr is used by many scripting languages, there is a standard way to handle this since source position handling is a single aspect and I don’t want to have it cluttered all over the grammar file, not very DRY.

  • 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-03T14:10:54+00:00Added an answer on June 3, 2026 at 2:10 pm

    I was hoping that, since Antlr is used by many scripting languages, there is a standard way to handle this

    You make it sound like ANLTR does not support this. Sure there is: every CommonToken and CommonTree objects exposes public getLine() and getCharPositionInLine() methods, but you discard these instances and create your own nodes (Expression). Don’t be surprised to make some extra effort in embedding this information in your own nodes 🙂

    You could let your runtime objects extend CommonTree classes and let your (combined) grammar construct these custom runtime objects (your classes now inherit the getLine() and getCharPositionInLine() methods). See: Using custom AST node types.

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

Sidebar

Related Questions

I have some script in my default page that redirects users to language specific
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
We have an existing java-based heavyweight project that needed an interactive script interpreter. After
Heres my problem, I'm web developer and I have a web based script(PHP/CURL/HTML/JS) that
I have a script that places an image based on a mouse click thanks
Does anybody have a PHP (or other language) script for drawing a spiral. A
I inherited a TCL script (I have zero familiarity with the language) and need
I have this script on my html page: <script language='javascript'>parent.resizeTo(550,510);</script> I'd like to add
I have a script that displays countries in different languages. For example United Kingdom
I have Javascript as below: <script type="text/javascript" language="javascript"> function redirectToPrevious(abcURL) { window.location.replace(abcURL); } function

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.