How to call a flex parser in c [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to call a flex parser in c ?

By calling yylex().
By default lex reads from stdin, if you want it to read from other stream, assign yyin, like
yyin = fopen("myfile", "r");

It's worth noting that yylex is not declared anywhere so you need to declare it:
int yylex ();
Traditionally it seems that the entire output of lex or flex would be incorporated in the C program via #include.
Recent versions of Flex include an option to create a header file, either on the command line via the
--header-file
option, or in the script
%option header-file
The header file contains stuff which can be used, for example, to ask Flex to read from memory rather than a file.

Related

What does a C #include statement do in a Fortran code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
In the middle of a code there is a C language statement. I don't know why it is there and how the compiler does not give back an error. Is it for C binding? Does it mean that this module can be used by C program or vice versa?
USE LISTS
USE LINKEDLIST_ROUTINES
#include "macros.h"
IMPLICIT NONE
PRIVATE
It is not a C language statement, but a C preprocessor (cpp) statement.
Any text file can use the preprocessor, even Fortran source codes, but you must call the preprocessor before compiling.
Many Fortran compilers will call the preprocessor for you with flags -cpp or -fpp or similar. They might also call it for you if the file suffix starts with capital F.
What the #include "file" does is the same as what it does in C source files, it inserts the text from the file in that location.
There is also a standard Fortran (90+) statement include. It is similar, but happens after any eventual pre-processing has been done, see Includes revealing with Fortran preprocessor for more.

Platform independent method to access command line in C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
On windows, the programmer could do something like: system("ls > outputFile.txt")
Is there a platform independent way to access the command line, or a least a way to determine which platform the program is being executed on (because calls for the same functionality vary quite a bit)?
The system(3) function is standard ANSI C, it's already platform-independent. Any conforming C implementation will allow you to call it to run the system default command line processor/shell application. Of course, the actual programs you can run will vary from system to system (e.g. dir only works on Windows, while ls usually works on Unix-like platforms).
system() itself is a standard C function defined in stdlib.h. The way it interprets its argument, though, is not standard (e.g. ls in UNIX, dir in Windows/DOS, etc.). If you're really asking whether there's a platform-independent way to list the files in a directory, the answer is (unfortunately) no. Some libraries do provide portable (to some degree) implementations, most notably Boost: How can I get the list of files in a directory using C or C++?

How do I generate a .cpp file from source code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm starting with C programming, and have written a program and am ready to compile. I've heard that mingw is a good choice, but the documentation for using it starts with a .cpp file and then turns that into a .exe. What I currently have is just the pure source (i.e. just text commands), how do I turn that into a .cpp? Thanks.
A cpp file is a C++ source file. Many tools (compilers, editors, etc.) can work with .c files or .cpp files.
A source file refers to the actual code you write. You know, stuff like
int main() {
printf("Hello, world!");
return 0;
}
That would be source code. 4 lines of it. So generally, you can't generate it. You have to write it. It sort of sounds like you wrote a .c file and are getting confused because you're using a tool whose example uses a .cpp file.
If the tool works with both C and C++, then use it with the .c file.
If it only works with C++... then you'll have to use something else.
If you have written code in a file like "xyz.txt" then simply rename it to "xyz.c". Also, you can take a file named like "xyz.c" create a copy of it and rename that copy to "xyz.cpp" and then modify "xyz.cpp" so that it can be compiled as C++ code.
In either case the "xyz.c" when compiled becomes "xyz.exe" and "xyz.cpp" will also become an "xyz.exe" load module, but not the same as the one generated from C code.
If you want load modules for a program written in C and the same program written in C++ then you will have to give them different names, like: "xyz.c" and "abd.cpp".
Hope this helps, and welcome to our world of programming!

About printf and scanf [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the meaning that printf and scanf commands are the part of C language, as they don't need inclusion of #include<stdio.h>.
Why aren't others part of the C language?
What is the mean that,(print f) and (scan f) commands are the part of c language
They are not "commands", but rather functions, and they aren't part of the language either.
they don't need inclusion of #include
They do. They need the headers/declarations even more than others, since they are variadic. Not providing a prototype for them will quickly lead to undefined behavior.
why don't others be the part of c language
Again, these aren't part of the language because... because they are not part of the language. They are stand-alone functions, which don't contribute to the core syntax and semantics of a C program. They aren't included in C's context-free grammar. The C standard does describe them, though - since they are part of the C standard library.
Actually, no, they are not part of the language in the way you think they are. If you call print("hello, %d", 5); it will create an implicit declaration based on the parameters you've provided and the returning type will be int.
Luckily, there is a match for this in libc which is implicitly linked to your program, and linker will be able to link your source file and the library definition of printf.
In certain IDE, it is possible that printf & scanf are used and not underlined as false while editig the source code, because of the indexer which knows these functions exists. But you will not be able to compile it. The include is not optional as the compiler itselfs doesn't know printf nor scanf.
printf() and scanf() Example programs
In layman language
printf() is a function use to display (output)
scanf() is a function used for reading any input
printf and scanf aren't part of the grammar, but they are part of the language by virtue of being in the standard library as specified by the language definition. You do need to include stdio.h to use them properly, though.

Linking files with LD [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I read this tutorial: http://www.osdever.net/tutorials/view/writing-a-simple-c-kernel
I tried linking the files using the likerscript that the tutorial provides. But LD gives me an error saying that it cannot read the file put out by nasm. Does anyone know what I am doing wrong?
If you executed the tutorial precisely as shown, then the problem is most likely here:
nasm -f aout kernel_start.asm -o ks.o
This produces an object file in the thoroughly obsolete a.out format. You're probably working through the tutorial on either a Windows or a Linux host system; the linkers that come with these systems expect object files in PECOFF and ELF format, respectively. There is probably another thing you can put after the -f in the above command that will make nasm produce the correct format.
Alternatively, learn to write AT&T assembly language instead. Then you can make an object file out of your .asm file with gcc -c just like the C source code, and you will automatically get the right format. The AT&T equivalent of the trivial startup file you have in that tutorial would be
.text
.globl start
start:
call k_main
cli
hlt
Take note also that I removed the leading underscore from the call instruction's argument. That underscore is only appropriate if the C code is compiled to an a.out-format object file, which (we suspect) it isn't.

Resources