Multiple YACC grammars in one program - yacc

How do I compile, link and call different YACC grammars using yyparse() in one program?

Use the -p option for each separate yacc grammar generation
-p prefix
Use prefix instead of yy as the prefix for all external
names produced by yacc. For X/Open compliance, when the
environment variable _XPG is set, then the -p option will work
as described in the previous sentence. If the environment
variable _XPG is not set, then the -p option will work as
described below in the -P option.

Related

CMake Toolchain File for GHS

I am trying to create a cmake toolchain file for the GreenHills compiler.
When I am using cmake -T C:\ghs\multi506 everything works fine.
But after creating a toolchain file with the line
set(GHS_TOOLSET_ROOT C:/ghs/multi506) I get the error message:
CMake Error: No GHS toolsets found in GHS_TOOLSET_ROOT "C:/ghs/multi506/"
What is the problem?
The use of -T and GHS_TOOLSET_ROOT is finicky. This is because of trying to maintain backward compatibility with the original generator implementation of trying to find the "latest" compiler.
'-T' takes either a absolute path or a relative path.
-T C:\ghs\multi506 means that the compilers are in this directory. It will contain gbuild.exe, etcetera.
-T multi506 will append multi506 to GHS_TOOLSET_ROOT. Therefore CMake will look for gbuild.exe in C:\ghs\multi506.
If you don't use -T then auto search mode is enabled. CMake will search for directories named comp_[^;]+, which is the naming scheme Green Hills uses for its compilers, in GHS_TOOLSET_ROOT. So in this case it will be looking for something like C:\ghs\multi506\comp_20210504.
I prefer using -T with an absolute path to the compiler directory.

syntax of the regex language used by ctest -R

As specified on
https://cmake.org/cmake/help/v3.7/manual/ctest.1.html,
ctest supports filtering tests to run by regex. Unfortunately I can't seem to find out what syntax the regex language used by ctest has.
I'd like to do something like
ctest -R "SomeTest|SomeOtherTest" # should execute the two tests named and no other tests.
Turns out I was calling ctest the wrong way. The way i was wrapping it in a bash script I had to escape the pipe as \\\|.

How do I save preprocessor output using the Dev-C++ IDE?

I'd like to be able to view preprocessor output in order to make sure my preprocessor directives run correctly. Dev-C++ has an option in Tools > Compiler Options... > General to add commands when calling the compiler, and I've added the command -E C:\Personal\preprocessed.cpp. I got a compiler error saying the file didn't exist, but shouldn't the compiler just create the file in that case? I created the file, and now I'm getting this error: cannot specify -o with -c, -S or -E with multiple files.
Why am I using Dev-C++ instead of Visual Studio? Since I'm still learning, I'd like to be able to test just a few lines of code without having to create an entire new project.
Yes, I've seen this question and no adequate answer was given. Please don't mark this as a duplicate.
Thanks in advance for your help!
I've added the command -E C:\Personal\preprocessed.cpp. I got a compiler error saying the file
didn't exist, but shouldn't the compiler just create the file in that case?
No, because the -E option
takes no argument, filename or otherwise. It simply instructs the
compiler to do nothing but preprocessing. The preprocessed code is written to the standard output. Thus:
Thus:
g++ -E C:\Personal\preprocessed.cpp foo.cpp
tells the compiler that you want run g++ -E with the pair of input files C:\Personal\preprocessed.cpp and foo.cpp,
which as you've discovered is not allowed.
The simple thing that you want to do is absurdly difficult with your IDE of choice. Assuming
the source file you want to preprocess is C:\Personal\foo.cpp and the g++ is in your PATH,
just open a command window in C:\Personal and run:
g++ -E foo.cpp > foo.ii
I suggest the output file foo.ii - though you can call it whatever you like - because g++ recognizes the extension .ii as denoting C++ source code that has already been preprocessed. You can run:
g++ -Wall -o prog foo.ii
and foo.ii will be compiled and linked as program prog without being preprocessed again.

cmake: setting default values for arguments

My UNIX Makefile is as follows:
param=0
run:
./foo -r "fun($(param))"
So if I do make run, I get ./foo - r "fun(0)" and
for make run param=10, I get ./foo -r "fun(10)".
Now I want to generate similar Makefile using cmake.
add_custom_target(
run
./foo -r "\"fun($(param))\""
)
How do I set the default value for param within cmake configuration file?
The concept in CMake is a bit different. You can define "cache variables" (basically variables that are remembered for subsequent builds in the same build dir, and can be customized by users) that come with default values and documentation strings and such. These can then be changed either by passing -D name:type=value options to cmake, or using one of the friendlier frontends (e.g. ccmake, the curses UI for CMake).
Example based on your question:
SET(param 0 CACHE STRING "Test variable defaulting to '0'")
# ...
add_custom_target(run ./foo -r "\"fun(${param})\"")
You'll find more details in the exhaustive docs for CMake.
PS. this is for variables inside CMake and specifically CMakeLists.txt itself; the possibility to change the value is not carried over into the generated Makefile as far as I can tell. I'm not sure that's possible in the first place because it probably wouldn't be compatiable with all of the targets supported by CMake (e.g. Visual Studio projects and what not). In any case, CMake doesn't seem to have been designed for generating build files used independently of CMake.
Use
set (projectname_param 0)
to set it.

Invoke OCaml compiler to just produce .cmi

maybe I'm just failing in a really simple thing but I'm developing an intepreter written in OCaml with standard tools as ocamllex and ocamlyacc and I have this situation:
iparser.mly contains parser spec
ilexer.mll contains lexer spec
impossible.ml contains the vm that executes the code and all the types needed
The instruction type that defines various instructions is in impossible.ml and it is needed by the parser but impossible.ml uses the parser defined in iparser.mly so they both need each other to compile correctly.
Is there a way to produce just the .cmi file for my impossible.ml? In this way the parser would know about types defined in impossible.ml and it will allow me to compile impossible.cmo/.cmi and later compile also impossible.cmo. Then I can link all of them together.
So far my compiling script is:
ocamlyacc iparser.mly
ocamlc -c iparser.mli
ocamllex ilexer.mll
ocamlc -c ilexer.ml
ocamlc -c iparser.ml
ocamlc -c impossible.ml
ocamlc -o imp.exe ilexer.cmo iparser.cmo impossible.cmo
but this doesn't work because ocamlc -c iparser.ml needs at least impossible.cmi to know the types.
Any suggestions? Thanks in advance..
You need to create an impossible.mli and compile that. That will produce the impossible.cmi and only the .cmi.
Alternatively:
ocamlc -i impossible.ml
will print the mli to stdout. You could do something like this:
ocamlc -i impossible.ml > impossible.mli
ocamlc -c impossible.mli
IMHO, you cannot legitimately compile recursively-dependant modules this way. Either factor out the interdependencies in the third module (usually easy), or pass them as parameters (or mutable initialization references - ugly), or use recursive modules.