I have a C function that I would like to use in my .y bison file. How do I do that? Where do I put the code for my function?
Thanks
In your .y file you can do something like:
%{
#include "types.h"
#include "interface.h"
void doStuff(void);
%}
Either #includeing a header file or declaring it right there.
This means that if interface.h declared a add_colour and add_colourd function you could do something like this later on:
Colour:
ColourSpace { add_colour($1); }
| STAR ColourSpace { add_colourd($2); }
calling them from witin your generated code.
If you wanted to implement them in that generated code to you could use %% at the end of the file and do something like:
%%
void doStuff() {
// Do some stuff!
}
So you can write "plain old C" inside your .y file too.
Related
I have the following code in a file named foo.c.
/** #file */
#include <stdio.h>
/** Prints hello */
#define hello() printf("hello, ")
int main()
{
/** Prints world */
#define world() printf("world\n")
hello();
world();
}
I have a file named Doxyfile in the same directory.
PROJECT_NAME = Foo
JAVADOC_AUTOBRIEF = YES
When I run the doxyfile command, I get a documentation that looks like this.
Why is there no documentation generated for the world() macro? How can I ensure that documentation is generated for the world() macro too without bringing it out of the function main()?
Macros/variables/etc inside functions bodies are outside the scope of Doxygen and thus no documentation is generated for them at this time.
If I have some #define in a header file, will it be usable in a source code that includes that header?
[Something like #define Bytef unsigned int]
Yes you can do that.
An include works as follows:
imagin you have a file.
header.h
content:
void HappyMakerPrototype();
void AnotherPrototype();
and a source file
src.c
content:
void dummydec();
#include "header.h"
void main ()
{
return;
}
In the first step of compilation it will run through the preprocessing.
Here the include line just gets replaced by all the content of your included file.
So that If you would request the output for the preprocessed file it would look like:
(in gcc and clang compiler you can request the preprocessed file with parameter -E I guess that will help you understanding)
void dummydec();
void HappyMakerPrototype();
void AnotherPrototype();
void main ()
{
return;
}
Yes you can do. While you are including that header file it will inherit all the things from that header. So you can use that Macro.
Yes. Including a file is as the same as if you copy and pasted the contents of the header file at the exact location as the #include directive.
I'm just starting with flex and i have some concerns about this tool.
%{
#include "parser.h"
int line_num = 1;
%}
\n { line_num++; }
%%
In the above code i'm just counting the lines in my scanning file, right?
How could I call the line_num value from another .c document? Whit a function like:
int getLineNumber(void);
And also, how could I detect lexical errors with this tool? I know that it is with the ".*" pattern but how to print it(in a function on a different .c again) like:
printf ("%d: error: %s\n", getLineNumber(), message);
Thanks.
In the code you've shown, you're already defining a global variable line_num. Just declare extern int line_num; in your header file and you can access it anywhere in your program.
If you want to avoid the global variable, replace the beginning of your scanner with something like:
%{
#include "parser.h"
static int line_num = 1;
int getLineNumber(void) {
return line_num;
}
%}
And put a declaration for the getLineNumber function in your header. (This is just ordinary C stuff, flex doesn't add anything weird here.)
For error handling, you can add a rule like:
. { reportUnrecognizedToken(); }
And then put a function like this somewhere:
void uncrecognizedToken() {
printf("Unrecognized token on line %d\n", getLineNumber());
exit(1);
}
Basically, I have a FileA.c:
//FIleA.c
void inline something()
{
//short code here
}
void inline another()
{
//short code here
}
Now I want to call these inline functions in another file main.c without using a header file.
How should I declare the prototypes of these functions in main.c?
//main.c
#include "FileA.c"
void something();
void another();
// or ???
int main()
{
something();
another();
something();
another();
return 0;
}
This answer actually suggests that there's no possible use case for defining inline functions in another .c file in this way.
On the other hand, if you #include "FileA.c" in your main file anyway, then you don't need to do anything, because you are using a header file (ending the name of an included file with .c doesn't change what it fundamentally is, it just confuses people reading your code).
I have C header file containing the following type definition:
// example.h
typedef struct Vertex {
int color;
} Vertex;
I try to wrap this struct with SWIG, but apparently I am doing something wrong. My SWIG interface file looks like
// example.i
%module example
%inline %{
#include "example.h"
}
But if I copy the contents of my header file into my interface file so that the latter looks like
%module example
%inline %{
typedef struct Vertex {
int color;
} Vertex;
%}
I can access the struct from Ruby in the following way
irb> require 'example'
# => true
irb> Examlpe::Vertex
# => Vertex
Is there a way to automatically wrap a header file? I don't want to copy and paste the contents of the header file to the interface file every time I change it.
Thanks in advance for your help.
-- t6d
It's been a while since I used Swig but as I recall %inline is used to pass through the inline part directly to the compiler; Swig itself doesn't see it, What I think you need is:
%module example
%include<example.h>