faqts : Computers : Programming : Language processing : CompilerCompiler : Bison

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 7 people (100%) answered Yes
Recently 7 of 7 people (100%) answered Yes

Entry

Compiler: Syntax: Backus-Naur form:Yacc:Windows:Bison: Can you give another simple example in Bison?

Oct 21st, 2003 22:23
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 22 October 2003 - 06:01 am --------------------

Compiler: Syntax: Backus-Naur form:Yacc:Windows:Bison: Can you give 
another simple example in Bison?

---

Note: THIS HAS TO BE FURTHER LOOKED AT. IT DOES COMPILE OK, BUT THE
SEMANTIC ROUTINES HAVE TO BE CHECKED E.G.
TO BE CONTINUED...

---

Steps: Overview:

---

 1. Given your example of Backus Naur Form rules:

     expression = term

                  term '|' expression

     term = factor

            factor term

     factor = atom

              atom unaryoperator

     unaryoperator = '+'

     atom = character

     character = abcdefghijklmnopqrstuvwxyz

---

2. Create a text file, using your favorite text editor (e.g. TSE,
   vi, notepad, ...), containing the following text:

--- cut here ---------------------------------------------------------

   %{

    #include <ctype.h>

   %}

   %token CHARACTER

   %%

   line : expression '\n'                 {printf( "%d\n", $1 ); }

   expression : term                      { $$ = $1; }

              | term '|' expression       { $$ = $1; }

   term       : factor                    { $$ = $1; }

              | factor term               { $$ = $1; }

   factor     : atom                      { $$ = $1; }

              | atom unaryoperator        { $$ = $1; }

   unaryoperator : '+'                    { $$ = $1; }

   atom       : CHARACTER                 

              ;

   %%

   yylex() {

    int c;

    c = getchar();

   return( CHARACTER );

   }

   yyerror( s )
    char *s;
   {
    printf ("%s\n", s);
   }

   main() {
    yyparse();
   }

--- cut here ---------------------------------------------------------

3. Save this text file, e.g. as 'myfilename.y'

   (for example in the same directory as bison.exe and bison.simple)

4. Make sure you have files

    bison.exe
    and
    bison.simple

   in the same directory

   http://www.monmouth.com/~wstreett/lex-yacc/lex-yacc.html

5. Now compile this program by typing

    bison.exe myfilename.y

   on the MSDOS command line

6. If this successfully compiles, it will generate program called

    myfilename_tab.c

7. An example of the content of this file 'myfilename_tab.c',
   using the example here above can be seen below.

8. Compile this 'myfilename_tab.c' in the usual way, e.g. using
   your favorite C or C++ compiler.
   (Borland command line compiler, Borland C++Builder, Microsoft Visual
   C++, ...)

   ---

   e.g. using the free Borland command line compiler, type on the MSDOS
   command line:

   c:\borland\bcc55\bin\bcc32.exe -Ic:\borland\bcc55\include -
Lc:\borland\bcc55\lib myfilename_tab.c

   (note: this should all be on one line)

9. This will create, if successfully compiled, an .exe file, called

    myfilename_tab.exe

10. To use this .exe file, type the .exe filename,
    followed by enter, and then some expression
    containing digits and '+' and '-'.

    e.g.

    1. type the command

        myfilename_tab.exe

    2. then press <ENTER>

    3. then type e.g.

        fried+|chicken+

       (so *no* spaces in this simple example)

    4. then press again <ENTER>

    5. that will show some result

    6. Pressing again <ENTER> will give the error
       'parse error', and you will exit.

---

11. Trouble shooting:

    1. Note, if you type e.g.

         myfilename_tab.exe fried+|chicken+

       then <ENTER>, this is not going to work.

       You should first type

         myfilename_tab.exe

       then <ENTER>

       then fried+|chicken+ (with no spaces)

       then <ENTER>

       then you should see some result

    2. If you get unresolved '_main', it means you should include the
       'main()' function in your file myfilename_tab.c

    3. If you get unresolved '_yyerror', it means you should include
       the 'yyerror()' function in your file myfilename_tab.c


    4. If you get a 'parse error', it means that the input tokens are
       not recognized.

----------------------------------------------------------------------