

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Professor: Kulkarni; Class: Compiler Construction; Subject: Elect Engr & Computer Science; University: University of Kansas; Term: Fall 2008;
Typology: Assignments
1 / 3
This page cannot be seen from the preview
Don't miss anything!


cpretty performs lexical analysis to produce a pretty printing of a C file. The C file is read from standard input. A pretty printing is produced to standard output. The pretty printing will be in the form that can be processed by LATEX. For instance, you can invoke the executable to produce a pretty printing of test1.c with the following commands.
cpretty < test1.c > test1.tex latex test1.tex dvips โo test1.ps test1.dvi
You should accomplish this assignment by writing a single lex file. The following tables indicate when specific fonts should be used and the escape sequence in LATEXto switch to that font. Note that the backslash character is to specify an escape sequence.
lexical elements font reserved words times roman bold comments times roman slanted strings typewriter bold character literals typewriter numbers times roman bold slanted everything else standard
font LATEXescape sequence times roman \textrm{} typewriter \texttt{} bold \textbf{} slanted \textit{} standard
The list of C reserved words are given below.
auto double int struct break else long switch case extern register typedef char float return union continue for short unsigned default goto sizeof void do if static while
Here are some other LATEX commands you might need:
The code inside the lex file must be in C so it will be compatible with lex. Create a file called cpretty.l. You should at least have lex rules to detect the following cases. You may find additional rules are necessary.
a. start of a C comment e. list of reserved words b. end of a C comment f. newline c. start or end of a string g. number d. start or end of a character literal h. default character
To process your lex specification you can simply issue the command: lex cpretty.l
You also need to link this file with the lex library as done with the following command. gcc -g -o cpretty lex.yy.c -ll
You should comment your program so that others (e.g. the grader) can understand it. You should also have comments at the top of the file indicating your name, this course, and the assign- ment. E-mail your lex specification file to [email protected] before the beginning of class on 22 nd^ September.
The following C program (echo.c) is available in the โผkulkarni/EECS665 asg1 directory. I have also provided a template pretty.l file that you should extend for this assignment.
static char *sccsid = "@(#)echo.c 4.1 (Berkeley) 10/1/80"; #include <stdio.h>
main(argc, argv) int argc; char *argv[]; { register int i, nflg;
nflg = 0; if(argc > 1 && argv[1][0] == โ-โ && argv[1][1] == โnโ) { nflg++; argc--; argv++; } for(i=1; i<argc; i++) { fputs(argv[i], stdout); if (i < argc-1) putchar(โ โ); } if(nflg == 0) putchar(โ\nโ); exit(0); }