y.tab.c not generated on fedora with bison -d - c

Update: So turns out all I had to do to make bison generate t.tab.c was to use the -y switch. However, my original assumption of a problem with the code when the problem gave me a segfault seems correct according to the answers posted. Here is what it all looks like now:
The source is exactly the same as downloaded from the O'reilly website and I triple checked the ch1-04 code that this is based on. It's all extremely simple stuff. A program that recognises a sentence as a subject VERB and object Here is the code : ch1-05.l, ch1-05.y, ch1-05y.h
Original Question:
So succinctly I'm working my way to learning how to build a compiler.
I'm not very used to C\C++ and have never used lex and yacc before, this I'm following this book:
Now, in the book on page 21 this is what it says:
Now being on fedora 2 I don't have yacc. I installed bison.
Here is what the scene on my fedora looks like:
Do notice I am using -d.
If you'd really like to see the code here is : ch1-05.l, ch1-05.y, ch1-05y.h
I was actually silly enough and thought y.tab.c was an mistake in the book and they actually meant ch1-05.tab.c. I tried compiling with that and it gave me a segmentation fault :/ It took me a day to realize there is supposed to be a y.tab.c there.
I'm quite constantly checking SO so anything you need from me I will respond quick. I'd really like to get this done with because I'm on holiday due to a brain haemorrhage and I'd like to get this book complete soon. I have quite a list of books and quite a holiday :D

Thinking that bison would produce ch1-05.tab.c instead of y.tab.c wasn't silly at all, in fact that's what it did. Bison is GNU's version of Yacc, and though it is compatible with it in terms of grammar description, there are some differences in usage.
If you want to revert to conservative Yacc mode, try bison -y [...]:
-y
--yacc
--fixed-output-files
Equivalent to -o y.tab.c; the parser output file is called
y.tab.c, and the other outputs are called y.output and y.tab.h.
The purpose of this switch is to imitate yacc’s output file name
conventions. Thus, the following shell script can substitute
for yacc and is often installed as yacc:
bison -y "$#"
If you get a segfault running you program, it's more likely due to a programming error on your side. How did you link you binary ?
( ... some googling later ... )
Try this in ch1-05.y:
extern FILE *yyin;
main()
{
yyin = stdin; /* initialize yyin */
while(!feof(yyin)) {
yyparse();
}
}

The default output file name is one of the differences between yacc and bison. If you use bison -y then it will act more like yacc and produce a y.tab.c (and y.tab.h if -d is also used).
The -y option will just change the name of ch1-05.tab.c to y.tab.c, it won't solve the bug that caused your segfault.

Related

Syntax error near unexpected token '('

As a beginner, I am trying to write a simple c program to learn and execute the "write" function.
I am trying to execute a simple c program simple_write.c
#include <unistd.h>
#include <stdlib.h>
int main()
{
if ((write(1, “Here is some data\n”, 18)) != 18)
write(2, “A write error has occurred on file descriptor 1\n”,46);
exit(0);
}
I also execute chmod +x simple_write.c
But when i execute ./simple_write.c, it gives me syntax error near unexpected token '('
Couldn't figure out why this happens ??
P.S: The expected output is:-
$ ./simple_write
Here is some data
$
You did
$ chmod +x simple_write.c
$ ./simple_write.c
when you should have done
$ cc simple_write.c -o simple_write
$ chmod +x simple_write # On second thought, you probably don’t need this.
$ ./simple_write
In words: compile the program to create an executable simple_write
(without .c) file, and then run that. 
What you did was attempt to execute your C source code file
as a shell script.
Notes:
The simple_write file will be a binary file. 
Do not look at it with tools meant for text files
(e.g., cat, less, or text editors such as gedit).
cc is the historical name for the C compiler. 
If you get cc: not found (or something equivalent),
try the command again with gcc (GNU C compiler). 
If that doesn’t work,
If you’re on a shared system (e.g., school or library),
ask a system administrator how to compile a C program.
If you’re on your personal computer (i.e., you’re the administrator),
you will need to install the compiler yourself (or get a friend to do it). 
There’s lots of guidance written about this; just search for it.
When you get to writing more complicated programs,
you are going to want to use
make simple_write
which has the advantages of
being able to orchestrate a multi-step build,
which is typical for complex programs, and
it knows the standard ways of compiling programs on that system
(for example, it will probably “know” whether to use cc or gcc).
And, in fact, you should be able to use the above command now. 
This may (or may not) simplify your life.
P.S. Now that this question is on Stack Overflow,
I’m allowed to talk about the programming aspect of it. 
It looks to me like it should compile, but
The first write line has more parentheses than it needs.
if (write(1, "Here is some data\n", 18) != 18)
should work.
In the second write line,
I count the string as being 48 characters long, not 46.
By the way, do you know how to make the first write fail,
so the second one will execute?  Try
./simple_write >&-
You cannot execute C source code in Linux (or other systems) directly.
C is a language that requires compilation to binary format.
You need to install C compiler (the actual procedure differs depending on your system), compile your program and only then you can execute it.
Currently it was interpreted by shell. The first two lines starting with # were ignored as comments. The third line caused a syntax error.
Ok,
I got what i was doing wrong.
These are the steps that I took to get my problem corrected:-
$ gedit simple_write.c
Write the code into this file and save it (with .c extension).
$ make simple_write
$ ./simple_write
And I got the desired output.
Thanks!!

YouCompleteMe suggests only "local" used code

I'm trying to use YCM for the first time so in order to make it work I decided to give a chance for the YCM-Generator, which generates the .ycm_extra_conf.py file automatically based on the makefile.
So far my program is just a simple hello world.
#include <stdio.h>
int main()
{
printf("Hello World!");
return 0;
}
I'm using the CMakeLists.txt trick to generate the makefile.
file(GLOB sources *.h *.c)
add_executable(Foo ${sources})
then after executing the YCM-Generator script, I get this output
Running cmake in '/tmp/tmp_YknVy'... $ cmake
/home/pedro/Desktop/Projetos/teste
Running make... $ make -i -j4
Cleaning up...
Build completed in 1.5 sec
Collected 2 relevant entries for C compilation (0 discarded).
Collected 0 relevant entries for C++ compilation (0 discarded).
Created YCM config file with 0 C flags
YCM plugin does find the .ycm_extra_conf.py file, but the auto-completion doesn't work right, for example, if I type "floa", it doesn't suggests "float", but It only suggests things that I used before like "int" or "printf".
Am I missing something or this is working as intended?
So I fixed it.
For c it does require a .ycm_extra_conf.py , while a friend of mine could make it work without one in c++.
The auto complete only suggest automatically functions that were previously used, if you don't remember a function name you have to press <Ctrl-Space>
YCM-Generator didn't do the job, so I modified the example file myself following the comments.
If you are used to Visual Assist, the auto complete works but it's really weak if compared to VA, which is a shame... I really hope someone port that plugin to Linux.

Error compiling gold in binutils

I am trying to compile the project KernelGen (http://hpcforge.org/plugins/mediawiki/wiki/kernelgen/index.php/Compiling) for some research project I am doing , the build script is using rpmbuild and it looks like it has a dependency on gold from binutils. Build keeps failing when trying to compile gold with the following error:
/bin/sh ./../ylwrap yyscript.y y.tab.c yyscript.c y.tab.h yyscript.h y.output yyscript.output -- byacc -d
byacc: e - line 42 of "/home/xxx/rpmbuild/BUILD/binutils-2.23.2/gold/yyscript.y", syntax error
%pure-parser
^
The file yyscript.c looks like this:
/* We need to use a pure parser because we might be multi-threaded.
We pass some arguments through the parser to the lexer. */
%pure-parser
%parse-param {void* closure}
%lex-param {void* closure}
/* Since we require bison anyhow, we take advantage of it. */
%error-verbose
/* The values associated with tokens. */
It looks like it is some kind of grammar file that fails to parse. Things is this file is from the binutils package itself and I can't find a reason why it is failing to parse.
Any ideas ?
Your version of byacc doesn't properly support %pure-parser, which I believe was originally a bison extension. Use bison instead.

Building code with a self-designed C compiler

As a part of my college project, we're supposed to develop a C compiler. The lexer and parser part is over - the tools Flex & Bison made our uphill task much simpler. Now where we're stuck is that we are unable to move on with the project. How exactly does one proceed once we have these three files in hand:
y.tab.h
y.tab.c
lex.yy.c
We also managed to produce an executable by using the following command on the DOS prompt
gcc lex.yy.c y.tab.c -o example1
and i got the executable example.exe...
Now how to proceed to get the compiler running? How to get it to build user C code?
You should check whether your example.exe can read a C program and report any syntax errors.
If it cannot, fix your .y and/or .l files and try again.
If it can, great, you have a working parser. Now turn it into a working compiler. Keep adding code, known as semantic actions, to the .y file, implementing more and more of the compiler's functionality. First, make sure you can report semantic errors, such as invalid types or missing declarations. Then implement assembly code generation.
When the .y file becomes too big, turn some code into functions and move them to separate .c files.

Is there something like IDLE (python) for C? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there an interpreter for C?
I want to practice C a bit and I would like to have something that allows to write and test C code quickly. I want to have an interpreter with UI where I can write my code and execute it.
Are there any good solutions for that?
The closest solution to what you are looking for seems to be the C shell (CSH) or C Scripting Language (CSL).
Alternatively, have an editor open where you will write your C sample, then have console window where you will execute your favourite C compiler. The idea is to have simple workflow like this:
$ gvim test.c
$ gcc test.c
$ ./a.out
Don't forget, C is not a scripting language.
However, you may find JIT compiler for C, C++, and the likes discussion helpful.
Though "interpreters" per se don't exist (or not practically), I'd advise on using a modern IDE. Eclipse + CDT allows you to have "on the fly compilation", just like in java. Your project is ready to run whenever you are, with reduced latency due to compilation (if you have a decent computer).
For other answers, I advise on NOT using directly gcc test.c. Use a makefile or use at least gcc -Wall -g -o myapp test.c top have additional information during compilation (useful as C has many more pitfalls than python). Please note as well that testis astandard program and that . might not be in your PATH : myapp is a better name than test ;-)
There is Cling. Never used it, so I can't really tell you more, but it looks like what you are looking for.
You might also find other lead in this question: Is there an interpreter for C?
you can take a look at : http://codepad.org/
or the easy way is to create a sh script like :
vim $1 ; gcc $1 ; ./a.out
You can't interpret C++ code as far as I know...
What you could do (and what I do when I quickly need to write some simple things ) is set up a simple make file and open a new file with some simple text editor like Kate that has a console plugin. Then you can write some code and type "make" to see the result of your code in the konsole / whichever shell you are using

Resources