Help with Compiler Design in C using Flex/Bison

Krysis09's Avatar, Join Date: May 2010
Newbie Member
Hey all,

This is my first time posting on GoExpert, so I'm excited to see what the community here has to offer! Anyway, I am currently working on creating a compiler (using Flex and Bison) in C for a subset of the Pascal programming language in my Compilers course... and to be honest - I'm a bit stuck!

I think I've made a lot of progress but, I'm getting stuck on this transition from Semantic Analysis to Code Generation... and what's worse is I'm not entirely sure I've gotten the semantic Analysis part all down!

I've created a Lex file (.l), a Bison/YACC file for my grammar (parser), a symbol table file that holds my insert() and lookup() methods for the symbol records, as well as a code generation file as well (although, there are some pieces of code in here that I'm unsure about - on how I translated the code ( ie. STORE/JUMP instructions).

Anyway, let me get to the specific question/request: I need some help modifying my .y Bison file to install, generate code, and back patch labels the different parts of the Pascal grammar. For Example:

Here's a small snippet of my grammar (starting symbol):

program:            program_header   block   PERIOD
program_header: PROGRAM IDENT SEMICOLON { install( $2 ); }
** where install( $2 ) installs the identifier (symbol name) into the symbol table. **
** where lowercase are non-terminals and UPPERCASE are terminals (Keywords in Pascal) **

I really have plenty of questions and would probably bog down this forum post if I went into any more random detail... if you have any clue about Compiler Design using Flex/Lex and Bison/YACC, I would REALLY appreciate your attention and help. I could and probably would need to go into more detail if I get some responses.

Just let me know what I need to elaborate on or to upload any files you need to view like: the mini-Pascal grammar, my lex file, my bison file, my symbol table, code generator, etc.

Thanks in advance,

Krysis09's Avatar, Join Date: May 2010
Newbie Member
Hmm, I'm not sure if I explained myself correctly in this post which is why I'm getting zero response - let me try to explain a bit more clearly .

I've been referencing Anthony A. Aaby's book: Compiler Construction using Flex and Bison at the following link:

I am at the point in my compiler creation at which I need to modify my parser file (.y file) to both install and generate code/labels for the grammar (pg. 38 in Aaby's book) for the subset the Pascal language... but, I'm confused as to how to go about it. More specifically, I don't understand WHEN to "install" identifiers, generate code, or, as Aaby put it, "content_check" identifiers in the grammar. I tried starting on doing my calls to install and gen_code, but I doubt I'm doing it correctly...

Install, gen_code(), and context_check() are all methods described by Mr. Aaby which:
1. Place a symbol into the symbol table
2. Generate the code for a particular terminal symbol (I think!)
3. Check whether a particular symbol exists in the symbol table... (or something to that effect).

Here are the methods I've written (basically based off of Aaby's methods) to install, generate code, and check the context of symbols of the grammar:

Code: c
install( char *sym_name )
        symrec *s;
        s = lookup( sym_name );
        if(s == 0)
                s = insert(sym_name);
                printf( "%s is already defined\n", sym_name );
context_check( enum code_ops operation, char *sym_name )
        symrec *identifier;
        identifier = lookup( sym_name );
        if( identifier == 0 )
                printf( "%s", sym_name );
                printf( "%s\n", " is an undeclared identifier." );
                gen_code( operation, identifer->offset );
/* Generates code at the current offset */
void gen_code( enum code_ops operation, int arg )
        code[code_offset].op = operation;
        code[code_offset++].arg = arg;

Where insert and lookup() are methods in my symbol table header file that inserts a new symbol and looks up a symbol name in the symbol table (respectively).

Attached is my .y file and the grammar for the subset of Pascal that I'll be creating the compiler for.

Hopefully, I've explained myself well enough.

P.S I wasn't able to post the link to Aaby's book, but if you wish to view it simply search for: Compiler Construction using Flex/Bison
Attached Files
File Type: txt pascalScanner.txt (3.4 KB, 52 views)
File Type: txt pasclParser.txt (6.0 KB, 55 views)
File Type: txt symtab.txt (3.9 KB, 60 views)
File Type: txt stackmMachine.txt (4.3 KB, 52 views)
File Type: txt PascalGrammar.txt (2.3 KB, 53 views)
xpi0t0s's Avatar, Join Date: Aug 2004
Post times:

04-30-2010, 10:45 PM initial post
05-01-2010, 03:36 PM "...I'm getting zero response..."

Maybe you didn't get a response because you waited less than five hours. We're not just sitting around here twiddling our thumbs waiting for questions; we have our own lives and just visit here occasionally, maybe daily, but you need to wait a day or two before deducing you "haven't had an answer".
Krysis09's Avatar, Join Date: May 2010
Newbie Member
Thanks for the response...