Shell scan for variables in "C" source program

Go To StackoverFlow.com

0

Can anyone help me with some advice on how to solve the following problems?

The idea of the problem is to scan a Foo.c file to find all variables, how many times they occur, and the lines were they do occur.

The implementation can be in at least one of the methods:

  1. Build a bat script and eventually additional C program(s) to solve the problem. Run the implementation in a cmd window.

  2. Build a ps1 script and eventually additional C program(s) to solve the problem. Run the implementation in a PowerShell window.

2012-04-04 02:39
by Bogdan Maier
That could be entertaining. Do you have to distinguish type names from variable names? Macros from variables? Comment stripping can be entertaining in its own right if taken to extremes. And you won't be wanting to pick up material from inside strings. Just a bunch of issues to consider - Jonathan Leffler 2012-04-04 04:27
If this is homework, they can't possibly expect him to parse C unless it's a capstone project or something.

Most likely, the real objective is to split lines into words, sort and count, and then eliminate reserved words - DigitalRoss 2012-04-04 05:26

The vars can be the same type for example int, or char - Bogdan Maier 2012-04-04 06:34


1

I think that, in order to get all variable declarations and uses, and only variable declarations and uses, you're going to need to at least partially parse the source files and analyze the resulting abstract syntax trees.

Your first step, then, is to either write a parser or figure out how to utilize an existing one.

2012-04-04 05:07
by Taymon


0

If you are programming C# you can use ANTLR V3 to parse your sources the "C" grammar exists.

2012-04-04 09:06
by JPBlanc


0

You could certainly try to write this as a bat script, but believe me, I've written close to 200 bat scripts and it's horrendous. cmd.exe's findstr would be your friend, but between bat and regex, you're gonna go crazy. Powershell would definitely be better, however a real scripting language would be your best bet, like perl, ruby, or python.

Luckily, in your case anyways, all var in C are explicitly declared, so you could scan once for all the var declarations and create an array of them. Then, scan a second time looking for instances of those variable names. Total number of instances would be total_times_seen -1 since the first would be the var declaration. This assumes of course they are only declared once...

2013-12-14 00:51
by kernelsmith
Ads