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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:33:28+00:00 2026-05-12T14:33:28+00:00

I am new to parser generators and I am wondering how the ANTLR grammar

  • 0

I am new to parser generators and I am wondering how the ANTLR grammar for an embedded language like JSP/ASP/PHP might look like, but unfortunately the ANTLR site doesn’t provide any such grammar files.

More precisely I don’t know exactly how to define an AnyText token which matches everything (including keywords which aren’t having any meaning outside the code blocks) and still be able to recognize them correctly inside the blocks.

For example the following snipped should be tokenized as something like: AnyText, BlockBegin, Keyword, BlockEnd, AnyText.

lorem ipsum KEYWORD dolor sit <% KEYWORD %> amet

Maybe there is also another parser generator which is suited better for my needs. I have only tried ANTLR up to now, because of its huge popularity here at stackoverflow 🙂

Many thanks in advance!

  • 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-05-12T14:33:28+00:00Added an answer on May 12, 2026 at 2:33 pm

    I can’t speak for ANTLR, as I use a different lexer/parser (the DMS Software Reengineering Toolkit, for which I have developed precisely such JSP and PHP lexer/parsers. (ASP isn’t different as you have observed in your question).

    But the basic idea is that the lexer needs lexical modes to recognize when you are picking up “anytext” and when you are processing “real” programming language text.
    So you need a starting lexical mode, say HTML, whose job is to absorb the HTML
    text, and when it encounters an transition-into PHP, switches modes.
    You also need a PHP mode which picks up all the PHP tokens,
    and switches back to HTML mode when the transition-out characters are encountered.
    Here’s a sketch:

    %%HTML -- mode
    #token HTMLText "~[]* \< \% "
       << (GotoPHPMode) >>
    
    %%PHP -- mode
    #token KEYWORD "KEYWORD"
    ...
    #token '%>'  "\%\>"
       << (GotoHTMLMode) >>
    

    Your lexer generator is likely to have some kind of mode-switching capability
    that you’ll have to use instead of this. And you’ll likely find that
    lexing the HTML stuff is more complicated than it looks (you have to worry
    about <SCRIPT tags and lots of other crazy HTML stuff, but those are
    details I presume you can handle.

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

Sidebar

Related Questions

I'm working on a new language and while writting the grammar I'd like to
please consider the following snippet: SimpleDateFormat parser = new SimpleDateFormat(MMdd); parser.setLenient(false); try { Date
I've read that Firefox 3.5 has a new feature in its parser ? Improvements
I'm new to jQuery and would like to parse an XML document. I'm able
Possible Duplicate: How to parse and process HTML with PHP? I am brand new
I am looking for method for parsing route path like this: ActionController::Routing.new(post_path).parse #=> {:controller
I am new to JFlex. I have gathered that JFlex is a parser generator.
I have written a parser-generator that has expressions like this: a = f:foo bar
I'm working on a parser and I'm really frustrated. In the language, we can
So I am working on a simple micro language/alternative syntax for PHP. Its syntax

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.