Yacc/Lex grammar

101 Uses for a Yacc

A parser generator such as Yacc/lex is how you get the front end of a compiler out quickly. It takes a description of the language, and gives you a C program (yes, I know, where is the Pascal generation version) that parses an input Pascal program and passes this information to the back end, the intermediate or direct machine code encoder.

Thats the good news. The bad news is that it leaves the major part of the compiler, the semantics, undone.

However, there are many reasons you might want to use a generated front end:  

Or, you might want to actually build a compiler.

pascal.y    Contains the yacc parser generator specification.

pascal.l    Contains the lex scanner generator specification.

Using and building the Yacc/Lex front end

Occasionally I get requests for how to use the above files. There is plenty of documentation on the net for how to use yacc and lex, and you should also know that I have never used either, so I am not much good for advice.

Yacc today exists as Bison, from GNU, and Lex exists today as Flex. They generate C code (one reason I have never felt inclined to use them). Here are a few pages to get you started.

http://dinosaur.compilertools.net/     Description of Yacc and lex.

http://www.gnu.org/software/bison/ Description and download for Bison.

http://flex.sourceforge.net/                Description and download for Flex.

Both of these programs come as part of the Cygwin distribution for Windows:


I used Bison and Flex on the pascal.y and pascal.l files and got the files lex.yy.c and pascal.tab.c, so everything works. I can't vouch for how correct the pascal.y and pascal.l files (in fact, I know there are some errors in the files vis-a-vis Standard Pascal). Building a compiler or other language translator is a challenging task and that isn't going to change because you use an automated front end translator.

For more information contact: Scott A. Moore samiam@moorecad.com