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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:18:32+00:00 2026-05-13T08:18:32+00:00

I have an interesting problem. I need to analyse a source code and to

  • 0

I have an interesting problem. I need to analyse a source code and to determine the types of variables before it is compiled. So, Reflection cannot be used!

There are only five types:

double      x = 1.23;
long        x = 3;
string     s='Hello World!' 
bool        b=true 
object[]     A = [1, 1+2, 'Hello', s]

An example of cource code:

for (i=0; i < 5; i++)
{
    a=2;
    b=4;
    c=6;
    tesstClass.Str = 'sss';
}

I decided to use regular expressions to solve the problem.

First, I find all pieces of code with the desirable variable (expressions with it) as follows:

string pattern = variable + @"[\w.]*\s*[-*+/]?=\s*[\w\s+'*/-]*\s*;";
MatchCollection mc = Regex.Matches(code, pattern);

Second, I analyse each Match using 5 regular expressions (one for each type):

string stringPattern = @"'[^'\r\n]*'"; //String;
string doublePattern = @"\b[0-9]+\.[0-9]+\b"; //Double
string longPattern = @"[-+]?\b\d+\b"; // Integer with a sign
string boolPattern = @"\b(false|true)\b"; // Boolean
string arrayPattern = @"\[([\w']*\s*,?\s*)*\]"; // Array

I am very bad in regular expressions. So I’ve defined a set of very simple r. expressions. Can you help me to refine them.

  • 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-13T08:18:33+00:00Added an answer on May 13, 2026 at 8:18 am

    The normal way of doing this would be to get the AST of your program and then simply search for the variable declarations you need. Gramars as suggested are a nice way of generating such AST.

    But, if you need to analyse your program on the fly you can’t use this option because your code might have parse errors. In this case I feel your pain…

    Your only option is to parse your source code and regular expressions might help a bit.

    First, I would begin with a regex similar to this:

    (double|long|string|bool|object)\s*(\[\s*\])?\s+(YOUR_VARIABLE_TOKEN)
    

    obs: YOUR_VARIABLE_TOKEN is missing because the variable has strong and defined rules about how it can be constructed for each language.

    I didn’t test this regex and it certainly isn’t perfect. It was just to give you an idea.

    Second, you would have to validate these matches with certain exception cases. For instance:

    1. The declaration might be inside a String literal : "bool a;"
    2. The declaration might be inside a comment : /* bool a; */

    Also, this is not a very strange request. Eclipse does this kind of evaluation too in some cases like indenting.

    This is not an easy task though, specially, finding those exception cases. Good Luck.

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

Sidebar

Ask A Question

Stats

  • Questions 367k
  • Answers 367k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer 2008 - sure. Set up a maintenance bach, tell it… May 14, 2026 at 4:47 pm
  • Editorial Team
    Editorial Team added an answer You can set the contents of any element with innerHTML.… May 14, 2026 at 4:47 pm
  • Editorial Team
    Editorial Team added an answer if you have a lots of elements on the stage,… May 14, 2026 at 4:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.