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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:44:11+00:00 2026-05-14T21:44:11+00:00

I currently have a working, simple language implemented in Java using ANTLR. What I

  • 0

I currently have a working, simple language implemented in Java using ANTLR. What I want to do is embed it in plain text, in a similar fashion to PHP.

For example:

Lorem ipsum dolor sit amet
<% print('consectetur adipiscing elit'); %>
Phasellus volutpat dignissim sapien.

I anticipate that the resulting token stream would look something like:

CDATA OPEN PRINT OPAREN APOS STRING APOS CPAREN SEMI CLOSE CDATA

How can I achieve this, or is there a better way?

There is no restriction on what might be outside the <% block. I assumed something like <% print('%>'); %>, as per Michael Mrozek’s answer, would be possible, but outside of a situation like that, <% would always indicate the start of a code block.


Sample Implementation

I developed a solution based on ideas given in Michael Mrozek’s answer, simulating Flex’s start conditions using ANTLR’s gated semantic predicates:

lexer grammar Lexer;

@members {
    boolean codeMode = false;
}

OPEN    : {!codeMode}?=> '<%' { codeMode = true; } ;
CLOSE   : {codeMode}?=> '%>' { codeMode = false;} ;
LPAREN  : {codeMode}?=> '(';
//etc.

CHAR    : {!codeMode}?=> ~('<%');


parser grammar Parser;

options {
    tokenVocab = Lexer;
    output = AST;
}

tokens {
    VERBATIM;
}

program :
    (code | verbatim)+
    ;   

code :
    OPEN statement+ CLOSE -> statement+
    ;

verbatim :
    CHAR -> ^(VERBATIM CHAR)
    ;
  • 1 1 Answer
  • 1 View
  • 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-14T21:44:12+00:00Added an answer on May 14, 2026 at 9:44 pm

    The actual concept looks fine, although it’s unlikely you’d have a PRINT token; the lexer would probably emit something like IDENTIFIER, and the parser would be responsible for figuring out that it’s a function call (e.g. by looking for IDENTIFIER OPAREN ... CPAREN) and doing the appropriate thing.

    As for how to do it, I don’t know anything about ANTLR, but it probably has something like flex’s start conditions. If so, you can have the INITIAL start condition do nothing but look for <%, which would switch to the CODE state where all the actual tokens are defined; then ‘%>’ would switch back. In flex it would be:

    %s CODE
    
    %%
    
    <INITIAL>{
        "<%"      {BEGIN(CODE);}
        .         {}
    }
    
     /* All these are implicitly in CODE because it was declared %s,
        but you could wrap it in <CODE>{} too
      */
    "%>"          {BEGIN(INITIAL);}
    "("           {return OPAREN;}
    "'"           {return APOS;}
    ...
    

    You need to be careful about things like matching %> in a context where it’s not a closing marker, like within a string; it’s up to you if you want to allow <% print('%>'); %>, but most likely you do

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

Sidebar

Related Questions

I'm currently working on a simple web application where users each have their own
I'm working on a relatively simple website with (currently) a single resource. I have
I currently have collision between 2 sprites working, using sprite.collidesWith(otherSprite) . The problem is
I'm working on code for a Java project, I currently have replaceAll(String, int) in
I am currently working on an ASP.NET MVC3 website. I have a simple authentication
I currently have a simple <div contenteditable=true> working, but, here's my problem. Currently, the
I currently have NSXMLParser working in my viewcontroller. I would like to create a
I currently have this working but it requires me to have a static method
We currently have a working php mail script, this works fine and as we
I am trying to get MSTest working with CruiseControl.Net. I currently have it working

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.