flex problems in using it to compile - lex

I create a simple test for lex:
%{
#include
%}
%%
stop printf("Stop command received\n");
start printf("Start command received\n");
%%
name it as aaa, but I can't use flex to compile it. Is there something wrong with my procedure? I just use flex example

#include without something to include is an error.
If you name it aaa, you probably need to do flex aaa, not flex example.

Related

How do I know which Systemverilog macros are defined when using Modelsim or Questasim?

I'm using Questasim 10.4c to simulate a Systemverilog design that uses the `ifdef compiler directive at a bunch of places. Example:
`ifdef FOR_SIMULATION_ONLY
<code>
`endif
After compiling, I haven't found any way to get Questasim to be able to tell me explicitly whether FOR_SIMULATION_ONLY has been defined, though. I've dug through the user guide and command reference manual and the closest thing I've found is putting a -E option in all my vlog compile statements and then examining the files created to see if FOR_SIMULATION_ONLY is defined. Can Questasim tell me, though, whether it's been defined without having to use the vlog -E method?
There is no switch to do that. You can put in
`ifdef FOR_SIMULATION_ONLY
$info("FOR_SIMULATION_ONLY defined");
`endif
and get a message at elaboration time.

Using listings package in macro

I'm creating a latex document that will describe various C++ syscall functions. I need to include their prototypes, descriptions, return values, and common uses. I've been told that to include code, I need to use the listings package, but I am using code within sentences and tables, so the commands for listings gets crowded and unreadable.
The first table without bold characters and titles for the columns.
Here's my code.
\begin{tabular}{l|l}
\begin{lstlisting}
void perror(const char* s)
\end{lstlisting}
&
\begin{lstlisting}
stdio.h, errno.h
\end{lstlisting}
\\
& prints argument message \begin{lstlisting}
s
\end{lstlisting}
\ based on global int
\begin{lstlisting}
errno
\end{lstlisting}
\end{tabular}
The code looks really messy and is difficult to read. I tried to fix this with a macro but it did not work. I'm programming in shareLatex right now.
\newcommand{\lstcode}[1]{
\begin{lstlisting}
#1
\end{lstlisting} }
I like the idea of simply using backticks for code in the middle of sentences. Even individually, the perror is bulky as code and I'd love to make the table look more like a table in my code too. How do I:
include code in a way that doesn't clutter up my table? (perror's declaration)
include code in the middle of a sentence? (s and errno)
Problem with creating a newenvironment in LaTeX
Macros have, thankfully, been created before for lstlisting and require that a special environment is made for them.
\lstnewenvironment
{⟨name⟩}[⟨number⟩][⟨opt. default arg.⟩]
{\lstset{label=#1}}
{⟨ending code⟩}
EDIT: After trying this, I couldn't use it. I switched to \texttt{} instead for its easiness of use. Any other choices would be cool, but for now this is good.

How to have more than one include in org babel?

I am working with C++ in org babel and would like to know how to format the includes so that I can list more than one.
Here's what I'm trying (that fails)
#+BEGIN_SRC C++ :includes <cstdio> :includes <iostream> :includes <string>
using namespace std;
printf("Hello ");
cout << "world\n";
#+END_SRC
In this case both printf and cout are undeclared in this scope. I can drop the unnecessary :includes <string> from the headers and the cout doesn't kick up an error, so it seems like only the last :includes counts. I have tried loading multiple includes into the same line using nothing, commas and spaces as delimiters and I always get an error about extra tokens at the end of the include directive. I have also tried using :includes+ in case that worked on the header line, but it didn't.
I'm fairly certain that what I'm trying to do should be possible because it says in the documentation that
:includes
(C & CC+ only) accepts either a single string name, or a list of names of files to #include in the execution of the code block
Org-version: 8.2.7-4-g880362-elpa
EDIT:
It's worth noting that other header arguments can just be strung together (i.e. :results raw drawer will produce unformatted results in a drawer), so there's a decent chance that this is a bug. Using :include <cstdio> <iostream> produces a compiler error that there's an extra token at the end of the #include <cstdio> line.
EDIT 2:
Turns out it's actually a bug in org, so it's been submitted.
After some experimenting, during which I noticed that some error messages look fairly lispy, and using the superficial knowledge I have of Lisp, I found the answer:
#+begin_src C++ :includes '("<math.h>" "<iostream>" "<algorithm>")
// Freely use symbols from those headers
#+end_src
Another issue that I'm having is that I can only get it to execute if I capitalize C++ but I only get syntax highlighting if I use minuscules ("c++"). Pure joy.

How can I reference #defines in a C file from python?

I have a C file that has a bunch of #defines for bits that I'd like to reference from python. There's enough of them that I'd rather not copy them into my python code, instead is there an accepted method to reference them directly from python?
Note: I know I can just open the header file and parse it, that would be simple, but if there's a more pythonic way, I'd like to use it.
Edit:
These are very simple #defines that define the meanings of bits in a mask, for example:
#define FOO_A 0x3
#define FOO_B 0x5
Running under the assumption that the C .h file contains only #defines (and therefore has nothing external to link against), then the following would work with swig 2.0 (http://www.swig.org/) and python 2.7 (tested). Suppose the file containing just defines is named just_defines.h as above:
#define FOO_A 0x3
#define FOO_B 0x5
Then:
swig -python -module just just_defines.h ## generates just_defines.py and just_defines_wrap.c
gcc -c -fpic just_defines_wrap.c -I/usr/include/python2.7 -I. ## creates just_defines_wrap.o
gcc -shared just_defines_wrap.o -o _just.so ## create _just.so, goes with just_defines.py
Usage:
$ python
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import just
>>> dir(just)
['FOO_A', 'FOO_B', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_just', '_newclass', '_object', '_swig_getattr', '_swig_property', '_swig_repr', '_swig_setattr', '_swig_setattr_nondynamic']
>>> just.FOO_A
3
>>> just.FOO_B
5
>>>
If the .h file also contains entry points, then you need to link against some library (or more) to resolve those entry points. That makes the solution a little more complicated since you may have to hunt down the correct libs. But for a "just defines case" you don't have to worry about this.
You might have some luck with the h2py.py script found in the Tools/scripts directory of the Python source tarball. While it can't handle complex preprocessor macros, it might be sufficient for your needs.
Here is a description of the functionality from the top of the script:
Read #define's and translate to Python code.
Handle #include statements.
Handle #define macros with one argument.
Anything that isn't recognized or doesn't translate into valid
Python is ignored.
Without filename arguments, acts as a filter.
If one or more filenames are given, output is written to corresponding
filenames in the local directory, translated to all uppercase, with
the extension replaced by ".py".
By passing one or more options of the form "-i regular_expression"
you can specify additional strings to be ignored. This is useful
e.g. to ignore casts to u_long: simply specify "-i '(u_long)'".
#defines are macros, that have no meaning whatsoever outside of your C compiler's preprocessor. As such, they are the bane of multi-language programmers everywhere. (For example, see this Ada question: Setting the license for modules in the linux kernel from two weeks ago).
Short of running your source code through the C-preprocessor, there really is no good way to deal with them. I typically just figure out what they evalutate to (in complex cases, often there's no better way to do this than to actually compile and run the damn code!), and hard-code that value into my program.
The (well one of the) annoying parts is that the C preprocessor is considered by C coders to be a very simple little thing that they often use without even giving a second thought to. As a result, they tend to be shocked that it causes big problems for interoperability, while we can deal with most other problems C throws at us fairly easily.
In the simple case shown above, by far the easiest way to handle it would be to encode the same two values in constants in your python program somewhere. If keeping up with changes is a big deal, it probably wouldn't be too much trouble to write a Python program to parse those values out of the file. However, you'd have to realise that your C code would only re-evaluate those values on a compile, while your python program would do it whenever it runs (and thus should probably only be run when the C code is also compiled).
If you're writing an extension module, use http://docs.python.org/3/c-api/module.html#PyModule_AddIntMacro
I had almost exactly this same problem so wrote a Python script to parse the C file. It's intended to be renamed to match your c file (but with .py instead of .h) and imported as a Python module.
Code: https://gist.github.com/camlee/3bf869a5bf39ac5954fdaabbe6a3f437
Example:
configuration.h
#define VERBOSE 3
#define DEBUG 1
#ifdef DEBUG
#define DEBUG_FILE "debug.log"
#else
#define NOT_DEBUGGING 1
#endif
Using from Python:
>>> import configuration
>>> print("The verbosity level is %s" % configuration.VERBOSE)
The verbosity level is 3
>>> configuration.DEBUG_FILE
'"debug.log"'
>>> configuration.NOT_DEBUGGING is None
True

C preprocessor: removing quotes from an argument

I'm abusing the C preprocessor for my build system to produce a "readme" plain-text file and a web page from the same source file. The construction is something like this:
The actual definitions are in data.h:
#define WEBSITE "http://example.com"
Note that the // in the URL must be quoted, or else it will be treated as the start of a comment. A similar problem occurs when using a , in the argument; the quotes are necessary, or else the comma would be treated as an argument separator.
Using this header, a file readme.txt.pp is run through the C preprocessor:
#include "data.h"
Visit the website at WEBSITE!
Of course, the preprocessor output is:
Visit the website at "http://example.com"!
The quotes appear in the output. Is there any way, or workaround, to get this code to give the output:
Visit the website at http://example.com!
I'm using Visual C++ 2008. I know that the preprocessor is not the ideal tool for this job; suggestions that use other built-in VC++ features are also welcome. (I tried XML with XSLT, but it is impossible to include one XML file into another, which was a show-stopper.)
Regarding XSLT, have a look at the document() function to read from multiple source documents.
I don't think there's any way to remove the quotes from the value of WEBSITE, since they are there in the definition of the macro. You might consider using the m4 macro processor instead of the C preprocessor.
Probably being late for Thomas, this might, however, still be useful for anyone lately stumbling over this question like me...
Try this:
#define DUMMY
#define WEBSITE http:/DUMMY/example.com
So the line comment disappears, and the preprocessor resolves DUMMY to nothing.
Try disabling the C++ style comments if possible. I don't know how that works in VS, but using a GCC compiler I can pass the -std=c89 flag to gcc to disable C++ style comments and hence making
#define WEBSITE http://example.com
possible.