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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:10:25+00:00 2026-05-30T02:10:25+00:00

For an iOS application, I want to parse an HTML file that may contain

  • 0

For an iOS application, I want to parse an HTML file that may contain UNIX style variables for replacement. For example, the HTML may look like:

<html>
  <head></head>
  <body>
    <h1>${title}</h1>
    <p>${paragraph1}</p>
    <img src="${image}" />
  </body>
</html>

I’m trying to create a simple ParseKit grammar that will provide me two callbacks: One for passthrough HTML, and another for the variables it detects. For that, I created the following grammar:

@start        = Empty | content*;

content       = variable | passThrough;
passThrough   = /[^$]+/;
variable      = '$' '{' Word closeChar;

openChar      = '${';
closeChar     = '}';

I’m facing at least two issues with this: for variable I had originally declared it as openChar Word closeChar, but it did not work (I still don’t know why). The second issue (and more important) is that the parser stops when it finds <img src"${image}" /> (i.e. a variable inside a quoted string).

My questions are:

  1. How can I modify the grammar to make it work as expected?
  2. Is it better to use a tokenizer? If that’s the case, how should I configure it?
  • 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-30T02:10:26+00:00Added an answer on May 30, 2026 at 2:10 am

    Developer of ParseKit here. I’ll answer both of your questions:

    1) You are taking the correct approach, but this is a tricky case. There are several small gotchas, and your Grammar needs to be changed a bit.

    I’ve developed a grammar which is working for me:

    // Tokenizer Directives
    @symbolState = '"' "'"; // effectively tells the tokenizer to turn off QuoteState. 
                          // Otherwise, variables enclosed in quotes would not be found (they'd be embedded in quoted strings). 
                          // now single- & double-quotes will be recognized as individual symbols, not start- & end-markers for quoted strings
    
    @symbols = '${'; // declare '${' as a multi-char symbol
    
    @reportsWhitespaceTokens = YES; // tell the tokenizer to preserve/report whitespace
    
    // Grammar
    @start = content*;
    content = passthru | variable;
    passthru = /[^$].*/;
    variable = start name end;
    start = '${';
    end = '}';
    name = Word;
    

    Then implement these two callbacks in your Assembler:

    - (void)parser:(PKParser *)p didMatchName:(PKAssembly *)a {
        NSLog(@"%s %@", __PRETTY_FUNCTION__, a);
        PKToken *tok = [a pop];
    
        NSString *name = tok.stringValue;
        // do something with name
    }
    
    - (void)parser:(PKParser *)p didMatchPassthru:(PKAssembly *)a {
        NSLog(@"%s %@", __PRETTY_FUNCTION__, a);
        PKToken *tok = [a pop];
    
        NSMutableString *s = a.target;
        if (!s) {
            s = [NSMutableString string];
        }
    
        [s appendString:tok.stringValue];
    
        a.target = s;
    }
    

    And then your client/driver code will look something like this:

    NSString *g = // fetch grammar
    PKParser *p = [[PKParserFactory factory] parserFromGrammar:g assembler:self];
    NSString *s = @"<img src=\"${image}\" />";
    [p parse:s];
    NSString *result = [p parse:s];
    NSLog(@"result %@", result);
    

    This will be printed:

    result: <img src="" />
    

    2) Yes, I think it would definitely be much better to use the Tokenizer directly for this relatively simple case. Performance will be massively better. Here’s how you might approach the task with the Tokenizer:

    PKTokenizer *t = [PKTokenizer tokenizerWithString:s];
    [t setTokenizerState:t.symbolState from:'"' to:'"'];
    [t setTokenizerState:t.symbolState from:'\'' to:'\''];
    [t.symbolState add:@"${"];
    t.whitespaceState.reportsWhitespaceTokens = YES;
    
    NSMutableString *result = [NSMutableString string];
    
    PKToken *eof = [PKToken EOFToken];
    PKToken *tok = nil;
    while (eof != (tok = [t nextToken])) {
        if ([@"${" isEqualToString:tok.stringValue]) {
            tok = [t nextToken];
            NSString *varName = tok.stringValue;
    
            // do something with variable
        } else if ([@"}" isEqualToString:tok.stringValue]) {
            // do nothing
        } else {
            [result appendString:tok.stringValue];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a custom alert view within my iOS application. For example,
I want to build an iOS application that recognizes patterns in a cup of
I have an ios application that runs on xcode 3.2.6. I want to change
Let's say I want to make an iOS application that is using new notifications
I want to build an application that lets me import a image file and
I want to develop iOS application that is a remote cotrol via bluetooth for
I'm working on iOS application in which i want XML file to be the
I am developing an iOS application, and trying to zip the file I have
My phonegap/iOS application has a file named data.js which contains an array. The application
I want to create advertise for my company on IOS application . I heard

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.