Docsity

Lexical Analyzer - Homework 2 | Systems Software | COP 3402, Assignments of Computer Science

Toggle sidebar
bg1
University of Central Florida
School of Electrical Engineering & Computer Science
COP 3402: System Software
Fall 2008
Homework #2 (Lexical analyzer)
(Team project ๏ƒ  Up to two people per team)
Due Monday Oct. 6th, 2008 by 11:59 p.m.
In this assignment you have to implement a lexical analyzer for the language nested C/0.
Your program must be capable to read in a source program written in nested C/0 and
produce, as output, the listing of the source program, the source program lexemes, and the
listing of the internal representation (a list of tokens) of the program. For identifiers, you
must show the token and an index to the symbol table. For numerical values, you must
show the token and the value.
For example, if the input is:
int x, y;
{
x := y + 56;
}.
The output should look like:
1.- A print out of source program:
int x, y;
{
x := y + 56;
}.
Remember:
1.- For constant values (3|56), โ€œ56โ€ must be transformed to its ASCII representation (as
discussed in class).
2.- The token separator can be โ€œ|โ€ or space.
3.- The notation โ€œ2|1โ€ means <identsym|symbol table index>
pf3
pf4

Partial preview of the text

Download Lexical Analyzer - Homework 2 | Systems Software | COP 3402 and more Assignments Computer Science in PDF only on Docsity!

University of Central Florida

School of Electrical Engineering & Computer Science

COP 3402: System Software

Fall 2008

Homework #2 (Lexical analyzer) (Team project ๏ƒ  Up to two people per team) Due Monday Oct. 6th, 2008 by 11:59 p.m. In this assignment you have to implement a lexical analyzer for the language nested C/0. Your program must be capable to read in a source program written in nested C/0 and produce, as output, the listing of the source program, the source program lexemes, and the listing of the internal representation (a list of tokens) of the program. For identifiers, you must show the token and an index to the symbol table. For numerical values, you must show the token and the value. For example, if the input is: int x, y; { x := y + 56; }. The output should look like: 1.- A print out of source program: int x, y; { x := y + 56; }. Remember: 1.- For constant values (3|56), โ€œ56โ€ must be transformed to its ASCII representation (as discussed in class). 2.- The token separator can be โ€œ|โ€ or space. 3.- The notation โ€œ2|1โ€ means <identsym|symbol table index>

2.- A print out of the lexemes, tokens and entry point in the Symbol Table: For example. Lexeme Token (index in symbol table or value) int 29 x 2 1 , 17 y 2 2 ; 18 { 21 x 2 1 := 20 y 2 2

  • 4 56 3 56 (ASCII value) ; 18 } 22

. 19 3.- Print out the symbol table entries (see struct at the end of the document). Lexical conventions for PL/0: A numerical value must be assigned to each token (internal representation): nulsym = 1, identsym = 2, numbersym = 3, plussym = 4, minussym = 5, multsym = 6, slashsym = 7, oddsym = 8, eqlsym = 9, neqsym = 10, lessym = 11, leqsym = 12, gtrsym = 13, geqsym = 14, lparentsym = 15, rparentsym = 16, commasym = 17, semicolonsym = 18, periodsym = 19, becomessym = 20, beginsym = 21, endsym = 22, ifsym = 23, thensym = 24, whilesym = 25, dosym = 26, callsym = 27, constsym = 28, intsym = 29, procsym = 30, writesym = 31, readsym = 32. Reserved words: { , call , const , do , } , if , odd , procedure , then , int , while. Special symbols: โ€˜ + โ€™, โ€˜ - โ€˜, โ€˜โ€™, โ€˜ / โ€™, โ€˜ ( โ€˜, โ€˜ ) โ€™, โ€˜ = โ€™, โ€™ , โ€™ , โ€˜ .โ€™, โ€˜ < โ€™, โ€˜>โ€™, โ€˜ ; โ€™ , โ€™:โ€™. Identifiers: identsym = letter (letter | digit) (assume 11 characters as maximum length) Numbers: must be 5 digits maximum. Invisible characters: (tab, white spaces, newline)

Example of a nested C/0 program: const m = 7, n = 85; int i,x,y,z,q,r; procedure mult; int a, b; { a := x; b := y; z := 0; while b > 0 do { if odd x then z := z+a; a := 2a; b := b/2; } }; { x := m; y := n; call mult; }.* Recommended data structure for the symbol. typedef struct { int kind; /* const = 1, var = 2, proc = 3. char name[10]; /* name up to 11 chars int val; /* number (ASCII value) int level; /* L level int adr; /* M address } namerecord_t; symbol_ table [MAX_NAME_TABLE_SIZE]; For constants you must store kind, name, and value. For variables you must store kind, name, L, and M. For procedures you must store kind, name, L, and M.