


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



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
. 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.