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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:07:01+00:00 2026-06-08T08:07:01+00:00

Im parsing a SCPI string, which looks something like: HEADER:HEADER:HEADER:CMD NUMBER MULTIPLIER UNIT; The

  • 0

Im parsing a SCPI string, which looks something like:

HEADER:HEADER:HEADER:CMD NUMBER MULTIPLIER UNIT;

The spaces between the tokens NUMBER, MULTIPLIER and UNIT are not necessarily there, nor are the tokens of a fixed length. I have been able to parse (from L to R) as far as the end of NUMBER. However the MULTIPLIER and UNIT tokens are each optional and can have characters that are the same.

e.g. suffix could be ‘P’ (where P could mean pico [mult] or poise [unit])

or ‘MA’ (could be mega [mult] or milli-Amp [mult-unit])

Does anyone have any experience parsing such syntax’s, or indeed anyone else, have any ideas on how to parse these into their correct tokens.

EDIT: For the pedant, I guess this is more lexical analysis than parsing.

  • 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-08T08:07:02+00:00Added an answer on June 8, 2026 at 8:07 am

    Perhaps in your simple example, doing it with a couple of nested ifs would be easier than trying a more powerful method, but if you don’t want to do that manually or if the actual problem is a bit bigger, you can try matching your input with regular expression (standard lexer stuff).

    On a POSIX system, you can use regexec.

    Edit: How to do it with if (and select):

    I assume your input is in text and you have already read up to the end of NUMBER, so your index i, shows that!

    // helper function: find next non-whitespace character
    char get_prev(char *text, int *end, int i)
    {
        for (; *end > i; --*end)
            if (text[*end] != ' ' && text[*end] != '\t'
                && text[*end] != '\n' && text[*end] != '\r')
            // or `if (text[*end] > ' ')` if ASCII
                break;
        return text[(*end)--];
    }
    
    ... your function...
        // read up to i
        int end = strlen(text);
        int power_of_10 = 0;  // for MULT
        enum unit unit = UNKNOWN; // for UNIT
        switch (get_prev(text, &end, i))
        {
            case 'P':
                unit = POISE;
                break;
            case 'A':
                unit = AMP;
                break;
            ...
            default: // unforeseen character
            case '\0':
                // neither UNIT nor MULT exist
                break;
        }
        if (unit != UNKNOWN)
            switch (get_prev(text, &end, i))
            {
                case 'M':
                    power_of_ten = -3;  // milli
                    break;
                case 'A':
                    switch (get_prev(text, &end, i))
                    {
                        case 'M':
                            power_of_ten = 6;  // mega
                            break;
                        ...
                    }
                    break;
                ...
                default: // unforeseen character
                case '\0':
                    // MULT doesn't exist
                    break;
            }
    

    Note, in this case, I assumed UNIT is mandatory. I’m not sure how you can distinguish between mega and milliamp in 10MA if both MULT and UNIT are optional. However you can add more cases to the first switch, that correspond to values of MULT and change power_of_10 there to. For example, if in the first switch you see k, you can understand that UNIT doesn’t exist and power_of_10 is 3.

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

Sidebar

Related Questions

I have a problem parsing a XML file which contains special characters like ,
I'm looking at parsing a delimited string, something on the order of a,b,c But
Parsing the string message seems bad. Or was this exception not meant to be
When parsing an xml file in android, I'm doing like this: try { InputStream
Not parsing the parameters in the beginning but parse the input string read from
I have question about parsing in Html helper : I have sth like: @foreach
I wrote a script parsing a .csv file in groovy using tokenize, which ended
Im parsing an XML with NSXMLParser like say in Apple Developer Documentation. Im do
Parsing is something I come across a lot in development, but as a junior
When parsing a freeform language like C, it is easy for the parser to

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.