Assignment #1 - Compiler Construction - Fall 2008 | EECS 665, Assignments of Electrical and Electronics Engineering

Material Type: Assignment; Professor: Kulkarni; Class: Compiler Construction; Subject: Elect Engr & Computer Science; University: University of Kansas; Term: Fall 2008;

Typology: Assignments

Pre 2010

Uploaded on 03/11/2009

koofers-user-7b1
koofers-user-7b1 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
EECS 665 โ€“ Fall 2008
Assignment 1
cpretty - lexical analysis for a C pretty printer
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 L
A
T
E
X. 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 L
A
T
E
Xto 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 L
A
T
E
Xescape 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 L
A
T
E
X commands you might need:
1. Backslash โ€“ \backslash
2. Space โ€“ ~
3. Special characters (&,%,$,#, ,{,}) should be preceded by a \.
4. Math symbols (+,โˆ’,โˆ—, /, >, <, =) should be enclosed within $ signs, like $+$, $>$.
5. Blank line starts a new paragraph.
6. Lines can be explicitly terminated using two backslashes (\\).
7. Additional vertical spacing โ€“ \smallskip, \medskip, \bigskip
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.
1
pf3

Partial preview of the text

Download Assignment #1 - Compiler Construction - Fall 2008 | EECS 665 and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

EECS 665 โ€“ Fall 2008

Assignment 1

cpretty - lexical analysis for a C pretty printer

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:

  1. Backslash โ€“ \backslash
  2. Space โ€“ ~
  3. Special characters (&,%,$,#, ,{,}) should be preceded by a .
  4. Math symbols (+, โˆ’, โˆ—, /, >, <, =) should be enclosed within $ signs, like $+$, $>$.
  5. Blank line starts a new paragraph.
  6. Lines can be explicitly terminated using two backslashes (\).
  7. Additional vertical spacing โ€“ \smallskip, \medskip, \bigskip

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); }