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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:45:48+00:00 2026-05-13T11:45:48+00:00

I have a program, written in C#, that when given a C++ or C#

  • 0

I have a program, written in C#, that when given a C++ or C# file, counts the lines in the file, counts how many are in comments and in designer-generated code blocks. I want to add the ability to count how many functions are in the file and how many lines are in those functions. I can’t quite figure out how to determine whether a line (or series of lines) is the start of a function (or method).

At the very least, a function declaration is a return type followed by the identifier and an argument list. Is there a way to determine in C# that a token is a valid return type? If not, is there any way to easily determine whether a line of code is the start of a function? Basically I need to be able to reliably distinguish something like.

bool isThere() 
{
...
}

from

bool isHere = isThere()

and from

isThere()

As well as any other function declaration lookalikes.

  • 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-13T11:45:48+00:00Added an answer on May 13, 2026 at 11:45 am

    Start by scanning scopes. You need to count open braces { and close braces } as you work your way through the file, so that you know which scope you are in. You also need to parse // and /* … */ as you scan the file, so you can tell when something is in a comment rather than being real code. There’s also #if, but you would have to compile the code to know how to interpret these.

    Then you need to parse the text immediately prior to some scope open braces to work out what they are. Your functions may be in global scope, class scope, or namespace scope, so you have to be able to parse namespaces and classes to identify the type of scope you are looking at. You can usually get away with fairly simple parsing (most programmers use a similar style – for example, it’s uncommon for someone to put blank lines between the ‘class Fred’ and its open brace. But they might write ‘class Fred {‘. There is also the chance that they will put extra junk on the line – e.g. ‘template class __DECLSPEC MYWEIRDMACRO Fred {‘. However, you can get away with a pretty simple “does the line contain the word ‘class’ with whitespace on both sides? heuristic that will work in most cases.

    OK, so you now know that you are inside a namepace, and inside a class, and you find a new open scope. Is it a method?

    The main identifying features of a method are:

    • return type. This could be any sequence of characters and can be many tokens (“__DLLEXPORT const unsigned myInt32typedef * &”). Unless you compile the entire project you have no chance.
    • function name. A single token (but watch out for “operator =” etc)
    • an pair of brackets containing zero or more parameters or a ‘void’. This is your best clue.
    • A function declaration will not include certain reserved words that will precede many scopes (e.g. enum, class, struct, etc). And it may use some reserved words (template, const etc) that you must not trip over.

    So you could search up for a blank line, or a line ending in ; { or } that indicates the end of the previous statement/scope. Then grab all the text between that point and the open brace of your scope. Then extract a list of tokens, and try to match the parameter-list brackets. Check that none of the tokens are reserved words (enum, struct, class etc).

    This will give you a “reasonable degree of confidence” that you have a method. You don’t need much parsing to get a pretty high degree of accuracy. You could spend a lot of time finding all the special cases that confuse your “parser”, but if you are working on a reasonably consistent code-base (i.e. just your own company’s code) then you’ll probably be able to identify all the methods in the code fairly easily.

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

Sidebar

Ask A Question

Stats

  • Questions 302k
  • Answers 302k
  • 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 I think the first step will be to get it… May 13, 2026 at 8:24 pm
  • Editorial Team
    Editorial Team added an answer there are various tools you can use depending on the… May 13, 2026 at 8:24 pm
  • Editorial Team
    Editorial Team added an answer In my experience, every time I've wanted to remove a… May 13, 2026 at 8:24 pm

Related Questions

Alright. So I wanted to use a file written in c in c++. I
I am writing a fairly large and complex data analysis program and I have
We have written an application that sits in the tray controlling OpenVPN as an
Some overflow runtime error happens when my C++ program is trying to write some
I got a bit of a problem. Related to my earlier questions about Slipstreamed

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.