SaltyCrane Blog — Notes on JavaScript and web development

Example using bison and flex with cygwin on Windows

Here is an example of how to use bison and flex (yacc and lex)with cygwin on windowsxp.
  1. Go to http://www.cygwin.com/, install Cygwin including bison 2.3-1, flex 2.5.4a-3, gcc-core 3.4.4-1, and make 3.81-1.

  2. Create a file called "simple.flex" in "c:\temp":
    %{                                                                                          
    #include "simple.tab.h"
    extern int line_number;
    %}
    %option noyywrap

    %%
    "float" { printf("FROM FLEX FLOAT %s\n", yytext); return FLOAT; }
    "int" { printf("FROM FLEX INT %s\n", yytext); return INT; }
    [;] { return *yytext; }
    [_a-zA-Z][_a-zA-Z0-9]* { printf("FROM FLEX IDENTIFIER: %s\n", yytext); return IDENTIFIER; }
    [ \t\r]+ /* eat up whitespace */
    [\n] { line_number++; }
    %%

  3. Create a file called "simple.y" in "c:\temp":
    %{                                                                                   
    #include <stdarg.h>
    #include "simple_shared.h"
    #define YYSTYPE char *
    int yydebug=1;
    int indent=0;
    char *iden_dum;
    %}
    %token FLOAT
    %token INT
    %token IDENTIFIER

    %% /* Grammar rules and actions follow */
    declaration:
    type_specifier identifier_dum ';'
    { printf("%3d: FROM BISON declaration\n", line_number); }
    ;
    type_specifier:
    FLOAT
    { printf("%3d: FROM BISON FLOAT\n", line_number); }
    | INT
    { printf("%3d: FROM BISON INT\n", line_number); }
    ;
    identifier_dum:
    IDENTIFIER
    { iden_dum = $1; printf("%3d: IDENTIFIER: %s\n", line_number, &iden_dum); }
    ;
    %%

    main ()
    {
    yyparse ();
    }

  4. Create a file called "simple_shared.h" in "c:\temp":
    int line_number=1;

  5. Create a file called "Makefile" in "c:\temp":
    simple: lex.yy.o simple.tab.o
    	gcc -o simple $^
    
    simple.tab.h: simple.y
    	bison --debug --verbose -d simple.y
    
    simple.tab.c: simple.y
    	bison -d simple.y
    
    lex.yy.c: simple.flex simple.tab.h
    	flex  simple.flex
    

  6. Create a file called "input.c" in "c:\temp":
    float variable;

  7. Open a Cygwin bash shell.
  8. "cd /cygdrive/c/temp"
  9. "make"
  10. "./simple.exe < input.c"

You should get some output like this:
$ ./simple.exe < input.c                      
Starting parse
Entering state 0
Reading a token: FROM FLEX FLOAT float
Next token is token FLOAT ()
Shifting token FLOAT ()
Entering state 1
Reducing stack by rule 2 (line 21):
$1 = token FLOAT ()
1: FROM BISON FLOAT
-> $$ = nterm type_specifier ()
Stack now 0
Entering state 4
Reading a token: FROM FLEX IDENTIFIER: variable
Next token is token IDENTIFIER ()
Shifting token IDENTIFIER ()
Entering state 6
Reducing stack by rule 4 (line 27):
$1 = token IDENTIFIER ()
1: IDENTIFIER:
-> $$ = nterm identifier_dum ()
Stack now 0 4
Entering state 7
Reading a token: Next token is token ';' ()
Shifting token ';' ()
Entering state 8
Reducing stack by rule 1 (line 17):
$1 = nterm type_specifier ()
$2 = nterm identifier_dum ()
$3 = token ';' ()
1: FROM BISON declaration
-> $$ = nterm declaration ()
Stack now 0
Entering state 3
Reading a token: Now at end of input.
Stack now 0 3
Cleanup: popping nterm declaration ()

How to use an external editor with Tornado 2.2

  1. In Tornado 2.2, go to "Tools" menu -> "Options..." -> "External Editor" tab
  2. In the "Command line:" field, enter "C:\Program Files\UltraEdit\uedit32.exe $filename" (Or if you don't use UltraEdit, replace with the path to your editor.)
  3. Check the "File menu", "Project", and "Build output window" checkboxes. Click "OK".

How Tornado 2.2 sets the PATH environment variable to find your Diab compiler

I've determined the version of my diab compiler is 5.0.3 in Tornado 2.2.1 (see this previous post). I got a new version 5.2.1 diab compiler and installed it, however Tornado was still looking in the original c:\Tornado2.2\host\diab\WIN32\bin directory location for the diab tools (see this previous post). I've finally figured out that there is a tcl script which runs at Tornado startup which sets the environment and does other things. On my machine, this was located at "C:\Tornado2.2\host\resource\tcl\app-config\Tornado\02Project.win32.tcl". Down around line 1449, I found some commands that set the DIABLIB and PATH environment variables. Instead of modifying the tcl script, I just moved the new version diab tools to the original directory.
 Technorati tags: , ,

How to determine the path and other environment variables during a build in Tornado 2.2

How to determine the path and other environment variables when building a project in Tornado 2.2. I don't have C:\Tornado2.2\host\diab\WIN32\bin in my path, but it looks like the Tornado project facilty adds it to my path. Here is how I figured that out.
  1. Open notepad.exe and type "set" inside it.
  2. Save it to "c:\temp\checkenv.bat"
  3. In Tornado 2.2, in the Workspace window, select the "Builds" tab
  4. Double-click on one of your projects
  5. Right-click on the build (e.g. "PPC860diab") and select "Properties..."
  6. Click on the "C/C++ compiler" tab
  7. In the "Tool" box, replace "dcc" (or "ccsimpc") with "c:\temp\checkenv.bat"
  8. Click "OK"
  9. Right-click on your project or build and select "Build 'xxx.out'" where xxx is your project name
  10. Your "Build Output" window should now contain a listing of all your environment variables, including the PATH variable.

PyQt4 Examples

PyQt is the best GUI toolkit for Python. Here are some examples and notes I am in the process of collecting.

There is a good collection of tutorials and examples included with the PyQt 4 installation. On Windows, go to "Start" -> "All Programs" -> "PyQt GPL v4.x.x" -> "Examples Source" for many tutorials and examples ported from C++/QT to Python/PyQt.

Update 2008-09-28: For Ubuntu Linux, the examples are part of the python-qt4-doc package. Execute sudo apt-get install python-qt4-doc then navigate to /usr/share/doc/python-qt4-doc/examples.

How to install PyQt 4.3 and Python 2.5 on Windows

Update 1/10/2008: PyQt has made the install process a lot simpler because it has bundled everything you need in one installer including QT 4.3 open source edition. Now all you need to do is install Python and the PyQt bundle. Immediately following are the updated steps. Below that is the old instructions.

Update 7/1/2008: Updated for PyQt 4.4.2


NEW INSTRUCTIONS

Here are the steps to install and create a simple "Hello World" GUI application using PyQt 4.4, and Python 2.5, on Windows.


Install Python 2.5
  1. Go to http://www.python.org/download/ and click on "Python 2.5.x Windows installer"
  2. Save and run the Windows installer
  3. Go through the steps and accept the defaults.

Install the PyQt 4.4 bundle (including QT 4.4)
  1. Go to http://www.riverbankcomputing.co.uk/software/pyqt/download and select the "PyQt-Py2.5-gpl-4.4.2-1.exe" link.
  2. Save and run the file.
  3. Go through the steps and accept the defaults.

Run a "Hello World" application
  1. Go to "Start" -> "All Programs" -> "Python 2.5" -> "IDLE (Python GUI)"
  2. Open a new window ("File" -> "New window")
  3. Type the following code inside and save:
    import sys
    from PyQt4.QtGui import *
    app = QApplication(sys.argv)
    button = QPushButton("Hello World", None)
    button.show()
    app.exec_()
  4. Hit "F5" to run. A window with a single push button should pop up.

For more examples, go to "Start" -> "All Programs" -> "PyQt GPL v4.4.2 for Python v2.5" > "Examples" > "PyQt Examples Source" (For a default installation, this is also located at C:\Python25\PyQt4\examples.) To start, look in the "tutorial" directory.


OLD INSTRUCTIONS

Here are the steps to install and create a simple "Hello World" GUI application using PyQt 4.1.1, Python 2.5, and QT 4.2.2 Open Source edition (GPL) on Windows XP with the MinGW compiler.

Install Python 2.5
  1. Go to http://www.python.org/download/ and click on "Python 2.5 Windows installer"
  2. Save and run the Windows installer
  3. Go through the steps and accept the defaults.

Install MinGW
  1. Go to http://www.mingw.org/download.shtml
  2. Download the following "bin" files from the "Current" section:
    • gcc-core-3.4.2-20040916-1.tar.gz
    • gcc-g++-3.4.2-20040916-1.tar.gz
    • mingw-runtime-3.9.tar.gz
    • w32api-3.6.tar.gz
  3. Extract all the files to "c:\mingw"

Install QT 4.2.2 Open Source edition
  1. Go to the Open Source download page at http://www.trolltech.com/developer/downloads/qt/windows. Note there is also an Evaluation version. This is *not* the one you want.
  2. Under the "Download" heading, select the "http://ftp.iasi.roedu.net/mirrors/ftp.trolltech.com/qt/source/qt-win-opensource-4.2.2-mingw.exe" link.
  3. Go through the steps and accept the defaults.
  4. When you get to the MinGW page, leave the "Download and install minimal MinGW installation" box unchecked and make sure the location of the MinGW installation is set to "c:\mingw". Click "Install".
  5. You will get an error message which says that the installer could not find a valid "w32api.h" file. You can install the 3.2 version from the mingw site, but the 3.6 version works. Click "Yes" to continue. Click "Finish" to finish the installation.

Install PyQt 4.1.1
  1. Go to http://www.riverbankcomputing.co.uk/pyqt/download.php and select the "PyQt-gpl-4.1.1-Py2.5-Qt4.2.2.exe" link.
  2. Save and run the file.
  3. Go through the steps and accept the defaults.

Check your Environment Variables
  1. Right-click on "My Computer" and select "Properties"
  2. Click the "Advanced" tab
  3. Click "Environment Variables"
  4. The following variables should be set:
    • user variable QTDIR - "c:\qt\4.2.2"
    • user variable QMAKESPEC - "win32-g++"
    • system variable PATH - include "C:\Qt\4.2.2\bin;C:\Python25\Scripts;C:\Python25;C:\Python25\DLLs;"

Run a "Hello World" application
  1. Go to "Start" -> "All Programs" -> "Python 2.5" -> "IDLE (Python GUI)"
  2. Open a new window ("File" -> "New window")
  3. Type the following code inside and save:
    import sys
    from PyQt4.QtGui import *
    app = QApplication(sys.argv)
    button = QPushButton("Hello World", None)
    button.show()
    app.exec_()
  4. Hit "F5" to run.
Technorati tags: ,

How to setup the MinGW gcc tools for your Managed Make C Project in CDT 3.1 and Eclipse 3.2

Here is a good article about how to setup MinGW tools with CDT and Eclipse. However I got one error when I setup my project. I got a
"/usr/bin/sh: c:mingwbinmingw32-gcc: command not found"
error. This was because I had "c:\cygwin\bin" in my path. To remedy this, you can set the "PATH" environment variable for your project. This will allow you to search the "c:\mingw\bin" path instead of the "c:\cygwin\bin" path without changing your path outside of Eclipse. This is also the solution if you have the wrong "include" directories listed in your "Includes" folder. E.g., if you have cygwin directories in your "Includes" folder, probably the cygwin gcc command is being used. See also my other post, How to set the include paths for gcc in a Managed Make project in Eclipse 3.2.1 and CDT 3.1

Update 2/5/2007:
You will also likely get a warning which says: Error launching 'cygpath' command'
See my post, Eclipse/CDT bug: Error launching 'cygpath' command about that.
To setup a GDB debugger, see my post, How to use the mingw gdb debugger with Eclipse 3.2 / CDT 3.1
Also, here is another good tutorial about setting up CDT: http://max.berger.name/howto/cdt/

Here are the detailed steps:
  1. Right-click on your project and select "Properties"
  2. Select "C/C++ Build" from the sidebar
  3. Click the "Tool Settings" tab
  4. Click the "GCC C Compiler" and enter "mingw32-gcc" in the "Command:" field
  5. Click the "GCC C Linker" and enter "mingw32-gcc" in the "Command:" field
  6. Click the "Build Settings" tab
  7. Uncheck the "Use default command" checkbox
  8. Replace "make" with "mingw32-make -k"
  9. Click the "Environment" tab; click the "Configuration" tab
  10. Click the "New" button
  11. From the "Name" dropdown box, select "Path"
  12. In the "Value" field, delete the contents and enter "C:\mingw\bin"
  13. In the "Delimiter" field, leave it as ";"
  14. In the "Operation" field, select "Replace"
  15. Click "OK"; click "OK"

How to determine the version of your Diab compiler in Tornado 2.2 / VxWorks 5.5

Did you ever wonder what version Diab compiler you were using with Tornado 2.2? I have a directory path called "C:\Tornado2.2\host\diab\5.1.2\WIN32\bin", but 5.1.2 was not my version. There was a file called "relnote.htm" which said 5.0.2, but that was slightly incorrect too. The answer is to use the "-VV" option with "dcc.exe", the Diab compiler. (Use "dplus -VV" for C++.) Note the option is two capital "V"s, not a "W". Here is how to do it.

Steps:
1. In the "Workspace" window, click on the "Builds" tab.
2. Double-click on one of the projects using a Diab compiler
2. Right-click on the build (e.g. "PPC860diab") and select "Properties..."
3. Click on the "C/C++ compiler" tab
4. In the text box put "-VV " as the first option
5. Click "OK"
6. Right-click on your project or build and select "Build 'xxx.out'" where xxx is your project name
7. Your "Build Output" window should now contain a line something like "dcc Rel 5.0.3" which is your Diab compiler version.

How to switch from the GNU to the Diab compiler in VxWorks / Tornado 2.2

 I was using the GNU compiler for aVxWorks / Tornado project and wanted to switch to the Diab compiler. Here is how to do it:

1. In the "Workspace" window, click on the "Builds" tab.
2. Right-click on the project that you want to change and select "New Build..."
3. Select the "Default Build Spec for" option and select the Diab toolchain for your hardware.
4. Click "OK". The Diab compiler should now be the "Active Build".
5. To switch back to the GNU compiler, click on the "+" sign to expand the branch, then right-click on the GNU build, and select "Set 'XXXXXXgnu' as Active Build".