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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:17:15+00:00 2026-06-17T10:17:15+00:00

I’m writing a really simple lexer for doing syntax highlighting of arbitrary text, one

  • 0

I’m writing a really simple lexer for doing syntax highlighting of arbitrary text, one of which is HTML. The goal of the lexer is just to provide a flat stream of tokens.

I started with the XML tutorial on the Antlr3 website, but am having some trouble with script tags.

An example of the HTML which causes this problem:

<head>
<script>alert(2 < 3);</script>
</head>

And the grammar..

@members {
    boolean inTag = false;
}

TAG_START_OPEN : '<'
                 { inTag = true; } ;
TAG_END_OPEN : '</'
               { inTag = true; } ;

TAG_CLOSE : { inTag }?=> '>' { inTag = false; } ;
TAG_SELF_CLOSE : { inTag }?=> '/>' { inTag = false; } ;
PCDATA : { !inTag }?=> (~'<')+ ;

// ... 

The problem is that the lexer gets confused when seeing the ‘<‘ tag within the Javascript code and thinks it is a close tag. I guess the goal would be for the lexer to use lookahead to determine whether a ‘<‘ is proceeded by ‘/script>’ if the open tag was a script tag, however I’m unsure of how to do this nicely with ANTLR.

Thanks in advance for any help.

  • 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-17T10:17:16+00:00Added an answer on June 17, 2026 at 10:17 am

    Here’s a quick demo of how you could accomplish this:

    grammar T;
    
    options {
      output=AST;
    }
    
    tokens {
      DATA;
      ATTRIBUTES;
      ATTRIBUTE;
      ATOMS;
    }
    
    @lexer::members {
      private boolean inTag = false;
      private boolean isScript = false;
    
      private boolean ahead(String s) {
        for(int i = 0; i < s.length(); i++) {
          int ch = input.LA(i + 1);
          if(ch != s.charAt(i)) {
            return false;
          }
        }
        return true;
      }
    }
    
    parse
     : tag EOF -> tag
     ;
    
    tag
     : TagOpen attributes TagOpenEnd atoms TagClose -> ^(TagOpen attributes atoms)
     ;
    
    attributes
     : attribute* -> ^(ATTRIBUTES attribute*)
     ;
    
    attribute
     : Key Assign Value -> ^(ATTRIBUTE Key Value)
     ;
    
    atoms
     : atom* -> ^(ATOMS atom*)
     ;
    
    atom
     : PCData
     | ScriptData
     | tag
     ;
    
    TagOpen
     : '<' Name 
       {
         inTag=true; 
         isScript = $Name.text.equals("script");
         setText($Name.text);
       }
     ;
    
    TagClose
     : {!inTag}?=> '</' Name '>' 
       {
         isScript = false;
         setText($Name.text);
       }
     ;
    
    TagOpenEnd
     : {inTag}?=> '>' {inTag=false;}
     ;
    
    Key
     : {inTag}?=> Name
     ;
    
    Assign
     : {inTag}?=> '='
     ;
    
    Value
     : {inTag}?=> '"' ~'"'* '"'
       {
         setText($text.substring(1, $text.length() - 1));
       }
     ;
    
    PCData
     : {!inTag && !isScript}?=> ~'<'+
       {
         if($text.trim().isEmpty()) {
           skip();
         }
       }
     ;
    
    ScriptData
     : {!inTag && isScript}?=> ({!ahead("</script>")}?=> . )+
     ;
    
    Space
     : {inTag}?=> (' ' | '\t' | '\r' | '\n')+ {skip();}
     ;
    
    fragment Name : ('a'..'z' | 'A'..'Z')+;
    

    If I now parse the input:

    <head>
      <script> alert(2 < 3); </script> 
      <span key="some value" x="<>">
        Mu <em>foo</em> bar!
      </span>
    </head>
    

    the following AST will be created by the generated parser:

    enter image description here

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

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I have a text area in my form which accepts all possible characters from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker
I am writing an app for my school newspaper, which is run completely online
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.