University of New Brunswick, Faculty of Computer Science

CS4905 Introduction to Compiler Construction

yacc Examples

To compile these examples, type
lex name.l
on the command line, where name.l is replaced with the name of your lex input file. This generates the lex output file lex.yy.c . Then, generate the compiler with yacc by typing (e.g.)
yacc name.y
This generates the default yacc output file y.tab.c .
To create a log file y.output that shows all the states in the parser, where parse table conflicts arise, and the transitions from one state to the next, type
yacc -v name.y

Invoke the C compiler to compile your compiler using e.g.
cc -o name y.tab.c -ll
on the command line. The output executable file is called name, so to run the program, type
./name < test.txt
where test.txt is the name of an input file.

bpb.l A lexer to ignore white space that talks with bpb.y.

bpb.y An initial cut at a yacc grammar to recognize balanced parentheses and square brackets.

dc.l A lexer to extract NUMBER tokens for use with desk calculator dc.y.

dc.y A desktop calculator written in yacc that computes results for simple arithmetic expressions.