-2

I have been given an assignment to write a program in c that reads a c program and extract functions from it.Can anyone help?

>  #include<stdio.h>
>      #include<conio.h>
>      int c;
>      FILE *file; 
>      file = fopen("c.c", "r"); 
>      if (file) {
>         while ((c = getc(file)) != EOF)
>             putchar(c);
>         fclose(file); 
> 
>      }
supersaiyan
  • 79
  • 1
  • 2
  • 9
  • 1
    Yes, if you have a specific problem with your current implementation. – Ilja Everilä Aug 17 '17 at 09:08
  • 2
    Please, provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Quentin Aug 17 '17 at 09:10
  • @naccyde apologies! Better now? – supersaiyan Aug 17 '17 at 09:19
  • That's a simple "read input and echo to output" program, far from a lexer/parser for the C language that would "extract functions" (function calls? prototypes? definitions?). Why the *conio.h*? – Ilja Everilä Aug 17 '17 at 09:22
  • 1
    did you search for similar questions? seems like something that has been done – RealCheeseLord Aug 17 '17 at 09:22
  • Smells like [LEX & YACC](http://dinosaur.compilertools.net/) to operate as lexer (tokenizer) and parser. – Andre Kampling Aug 17 '17 at 09:24
  • Please provide diverse sample input and describe what the reaction of your program should be or give desired output. – Yunnosch Aug 17 '17 at 09:33
  • Lex & Yacc might be overkill for simple cases. – Yunnosch Aug 17 '17 at 09:34
  • 1
    @SakethGangam worse, actually. Your code will not compile because of the '>' chars and a '#incude' not at the start of a line. Also, as commented above, your code does next-to-nothing. If one was in a suspicious frame of mind, one might suspect that you found the first example of C file-handling you could get off the web and dumped it. – Martin James Aug 17 '17 at 09:50

1 Answers1

0

You will fine enough help here and here.

First of all if you want to read the c file, find your answer in the first link. Second of all after opening it from the first step, you should look for types (helping yourself with the second link): bool, int, void, ..., then you will save the line with a getline().

your getline should include the following if your c file is well indent :

  • the type (including pointers if any),
  • the name of the function,
  • the parameters

Then from the first '{' you will find your function implementation. If there is any more loop inside your fonction, you have to count all the '{' in order to know where your function finishes by putting them in a LIFO.

You will have to do this until the end of the doc to know all the functions.

Benjamin
  • 100
  • 11