Getting Grammarinator to work with openqasm3 grammar - quantum-computing

I've been trying to get grammarinator to generate random test programs based on the openQasm3.0 grammar (https://github.com/Qiskit/openqasm/blob/master/source/grammar/qasm3.g4), What I want to get is:
qasm3.g4 -> Antlr4py3 -> *.g4 files -> Grammarinator-generate -> a bunch of valid program files as tests.
So far what I got is:
antlr4py3 -o ./somedir qasm3.g4 (which is based on the instructions given on openqasm github)
Step 1 yielded a bunch of files, most importantly: qasm3Lexer.py, qasm3Parser.py, qasm3Lexer.tokens, qasm3tokens.
When I looked at them, they don't seem to reminiscence what grammarinator expects. Based on info and examples that I read online, I need to feed grammarinator-process and grammarinator-generate with some g4 files, such as qasm3Lexer4 and qasm3Parser.g4 etc, but I can't find them from Step 2. I tried to feed them with the *.py files from step 2, but they generated gibberish test programs.
Some of the commands that I tried are:
i) grammarinator-process qasm3Lexer.tokens qasm3.tokens -o
ii) grammarinator-process qasm3.g4 -o output
Followed by:
grammarinator-generate -l qasm3Unlexer.py -p qasm3Uparser.py -r program -n number -d depth -o test.txt
I've also tried various combinations, compared the samples with what's in qasm3, but to no avail, and I'm lost. Just wondering if someone could give me some pointers on how to proceed? Thanks in advance.

Sorry about this. I have definitely misunderstood a couple of things about antlr4 and grammarinator. The latter expects the qasm3.g4 input, which then generates the corresponding Unlexer and Unparser. Thus, the process is: qasm3.g4 (from github) -> either antlr4 or grammarinator -> outputs.
So in the end I managed to get grammarinator to generate some test programs by feeding the qasm3.g4 file into it:
grammarinator-process qasm3.g4 -o outputdir <--- this process generates two files, namely qasm3Unlexer.py and qasm3Unparser.py, and they are used in the next step to generate the test programs.
grammarinator-generate -l outputdir/qasm3Unlexer.py -p outputdir/qasm3Unparser.py -r program (or something that you want to test out based on qasm3.g4) -n somenum -d somedepth -o outputdir/test_%d.txt

Related

Gcov works, but Gcovr doesn't work

When I run gcov then the output says 87% covered... then I want to use gcovr, but it does never work. I tried to run from many directories, with lots of options. In some cases, a few files I get coverage but not the file I want to see.
My project consists of 3 files:
src\main.c
src\makefile
tst\test1.c
tst\test2.c
tst\makefile
tst\obj\all object files, gcno, gcda are stored here...
build\build.bat to make the project (windows)
Should I change the directory structure? How to see which version of gcov I should use?
I want to see the coverage for main.c, but only get the coverage for test1.c and test2.c and those are not relevant for coverage!!
Next to the test files, I have include directories on C:\compiler which are not required for coverage.
I tried Python27:
python c:\python27\scripts\gcovr -g --object-directory=. -r ..\..\..\ --html --html-details -o program.html -v
I tried Python36:
c:\python36\scripts\gcovr --use-gcov-files -v --object-dir=.
But never get the coverage for my device under test. Only gcov without gcovr works, so the files must be correct.
try add -f '' option. There bug in gcovr which is getting out when we build out of tree
I found several problems when running the GCOVR in Windows, especially concerning \ instead of / and using colon character : in the name like C:\file.c
When you use \ it needs to use 2 of them \ and somewhere in the gcovr it changes to \\ or \\\ ... this is not going well, at some point it tries to find the root of your source files by subtracting 2 paths. but it cannot find this, e.g.
\a\b\c\d it will not find in \a\b\c\d . So, look in : [github.com/barthoukes/gcovr][1]
THe solution is still very dirty, but it works for my projects in Windows. I think the gcovr people also are discussing this problem.
You only need the file: gcovr

ar on MSYS2 shell receives truncated paths when called from Makefile?

I'm using git-bash.exe from a PortableGit install, with environment variables from a different MinGW. So I have:
WORKGROUP+user#AD-X MINGW32 /z/user/Downloads
$ which ar
//WORKGROUP.EX.COM/Users/user/Downloads/mingw-w64/i686-4.9.3-posix-dwarf-rt_v4-rev1/mingw32/bin/ar
WORKGROUP+user#AD-X MINGW32 /z/user/Downloads
$ gcc --version | head -1
GNU ar (GNU Binutils) 2.25
Now there's a library I'm building, and at the end, the link step fails at the call of the ar command, which looks something like this:
ar -cr "Z:/user/Downloads/MyProjectNameABCDE/someLibraryABC/libs/someLibraryDEFGHI/lib/mingw/libsomeLibraryABCDebug.a" \
Z:/user/Downloads/MyProjectNameABCDE/someLibraryABC/libs/someLibraryDEFGHI/lib/mingw/obj/Debug/libs/someLibraryDEFGHI/test/someObject.o \
[...]
... and there's a bunch of objects listed in it - the command line is 10000 characters in length, which is still below getconf ARG_MAX of 32000 in MSYS2 shell of PortableGit (git-bash.exe). However, the failure I get is No such file or directory:
\\WORKGROUP.EX.COM\Users\user\Downloads\mingw-w64\i686-4.9.3-posix-dwarf-rt_v4-rev1\mingw32\bin\ar.exe: Z:/user/Downloads/MyProjectNameABCDE/someLibraryABC/libs/someLibraryDEFGHI/lib/mingw/obj/Debug/libs/some: No such file or directory
... and the path given is quite clearly a truncated version of the path where the object files are. What's even stranger, when I copy the full ar command line printed by the make process, and paste it back in the same terminal, it completes without error?
Would anyone have an idea why this happens, and what could I do to make sure ar completes when called from the Makefile?
Ok, so first I found in the Makefile where the ar command is ran, and I added the -v switch to it (so, -crv) for verbose.
I could find that most of the command line is read, and objects added, until it comes to about 8192 bytes of the command line, after which it is truncated, and the failure occurs. This is apparently a known issue:
How to avoid Max Command line size on Windows
Solving the 8192 Character Command Line Limit on Windows | MCU on Eclipse
... although, I'm not quite clear on why it should appear in a make process which already runs in git-bash.exe, that is, a MSYS2 shell ?!
Anyways, the workaround/fix I used is to use the file option of make, since just doing "#echo $CMD > arscript.sh" from the Makefile will again save only the 8kb truncated command line to file; so instead of the original call:
#$(AR) ${FLAGS_FOR_AR} "$#" $(FILES_FOR_AR)
... we save line that to file, and then call bash to interpret it as script; that is:
$(file >arscript.sh,#$(AR) ${FLAGS_FOR_AR} "$#" $(FILES_FOR_AR))
bash -x arscript.sh
... and this finally worked for me.

Line level profiling of a C program by GNU

Could Someone kindly tell me how can I profile single lines or blocks of code of a program in C with GNU profiler?
I used gprof ./a.out gmon.out which gives me flat profile and Call graph. However, I would like to see lines that are more frequently accessed.
Thanks,
This is probably one of those things that you just don't know the term you should've googled, so I'll answer it:
The term you are looking for is "annotation"-you want to annotate the source and see the line by line hits in the code.
Calling gprof with the -A flag will dump out the samples on each line that were caught.
See Also:
https://sourceware.org/binutils/docs/gprof/Annotated-Source.html
Ok, I'll post this answer so if some newbie like me searched for it can find it faster :)
here are the steps: source
gcc -fprofile-arcs -ftest-coverage fourcefile.c
(At the end of compilation files *.gcno will be produced)
Run the executable.
Run gcov: gcov sourcefile.c
(At the end of running, a file (*.gcov) will be produced which contains which contains all the required info)

C - program compiling, but unable to provide arguments

I'm on a Mac and in terminal I'm compiling my program
gcc -Wall -g -o example example.c
it compiles (there are no errors), but when I try to provide command line arguments
example 5 hello how are you
terminal responds with "-bash: example: command not found"
how am supposed to provide the arguments I want to provide after compiling?
Run it like this with path:
./example 5 hello how are you
Unless the directory where the example binary is part of the PATH variable, what you have won't work even if the binary you are running is in the current directory.
It is not a compilation issue, but an issue with your shell. The current directory is not in your PATH (look with echo $PATH and use which to find out how the shell uses it for some particular program, e.g. which gcc).
I suggest testing your program with an explicit file path for the program like
./example 5 hello how are you
You could perhaps edit your ~/.bashrc to add . at the end of your PATH. There are pro and conses (in particular some possible security issues if your current directory happens to be sometimes a "malicious" one like perhaps /tmp might be : bad guys might put there a gcc which is a symlink to /bin/rm so you need to add . at the end of your PATH if you do).
Don't forget to learn how to use a debugger (like gdb). This skill is essential when coding in C (or in C++). Perhaps consider also upgrading your gcc (Apple don"t like much its current GPLv3 license so don't distribute the recent one; try just gcc -v and notice that the latest released GCC is today 4.8.1).
./example 5 Hello how are you is the syntax you're looking for.
This article lends a good explanation as to why this is important.
Basically, when you hit Enter, the shell checks to see if the first set of characters is an absolute path. If it's not, it checks the PATH variable to find executables with the name of the command you are trying to run. If it's found, it will be run, but otherwise it will crash and burn and you will become very sad.

Determine if C library is installed on Unix

As a follow up question to my last one, is there any simple way to tell if a given C library is installed on a given machine (not programattically, just as a once off sort of thing)? I need to use libuuid, but I'm not sure if it's installed on the machines in question. The only two ways I can think of are:
1) Try to compile the code there (more work than I'd like to do)
2) Try something like "man libuuid" although seems like this wouldn't always be reliable if for some reason the manpages didn't get installed.
Is there some better other way?
Have you considered using autoconf? It's designed to check and see whether the build environment is set up correctly.
The simplest way is to invoke ld with the -l option. This will effectively test the existence of the library, searching the standard library locations automatically:
$ ld -luuid
ld: warning: cannot find entry symbol _start; not setting start address
$ echo $?
0
$ ld -luuidblah
ld: cannot find -luuidblah
$ echo $?
1
# so...
$ ld -luuid 2>/dev/null && echo "libuuid exists" || echo "libuuid not found"
EDIT
As dreamlax pointed out, this does not work for all unix variants. I don't know if it will work on all unixes (I've tested linux and OpenBSD) but you can try this instead:
$ echo "int main(){}" | gcc -o /dev/null -x c - -luuid 2>/dev/null
$ echo $?
0
$ echo "int main(){}" | gcc -o /dev/null -x c - -luuidblah 2>/dev/null
$ echo $?
1
Here's what I did using autoconf, which I'm showing here as a solid example for whoever else might come by next:
I created the file configure.ac which contained the following:
AC_INIT(package, 1.1, email)
AC_CHECK_LIB(uuid, uuid_generate_random, [echo "libuuid exists"], [echo "libuuid missing"])
I then ran the following commands in order (same folder I made configure.ac):
autoconf
./configure
At the end of the configure, it spat back whether or not it had found uuid_generate_random in the uuid library. Seemed to work perfectly (although unfortunately, two of the OSes were missing the library, but that's a whole other problem).
For anybody who may find this after the fact, the AC_INIT arguments here are throwaways and you can copy them wholesale. The arguments to AC_CHECK_LIB are: library name, the name of a function in that library, what to do on success, what to do on failure.
Even though Mehrdad's answer wasn't quite as helpful as I would have liked (i.e. to not have spent time trolling the docs) it seems to be the correct one and I'll be accepting it. mhawke: I really liked your answer, but I wasn't quite sure how to test to make sure it worked. It seemed to on SunOS, but always said no on the other two (AIX, HPUX) and I couldn't seem to come up with a library off the top of my head I could guarantee it would find.
Thanks for the help guys.
The autotools, as mentioned, checks for symbols within libraries. The way it does it is somewhat simple. As you mention in 1), autoconf, and its result the configure script, basically creates a dummy c program and attempts to link with the library in question. If it works, the library works, if it fails, obviously it wont work. Autoconf looks for specific symbols/function names in the library though.

Resources