SOLVED:CSC 446 ASSIGN #5

$65.00

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (8 votes)

Part 1

 

Change the grammar of the C language to include the following modified grammar rules:

 

PROG              ->        TYPE  idt  REST PROG |

const  idt  =  num ; PROG |

e

 

DECL              ->        TYPE IDLIST |

const  idt  =  num  ;  DECL |

e

 

This will allow the declaration of constants in the language.  You will have to add the reserved word const to your lexical analyzer and rewrite the DECL and PROG procedure in your parser.

 

Part 2

 

Add the appropriate semantic actions to your parser to insert all constants, variables and functions into your symbol table.  For constants you will have to set either the value or valuer field as returned by your lexical analyzer.  Variables will require the type, size and current offset of the new variable.  The first variable at a given depth will be at offset 0.  Integers have size 2, characters have size 1 and float has size 4.  Update the offset field by the size of the new variable after inserting the variable.  Functions will require that you keep track of the size of all local variables and formal parameters, the number and type of all formal parameters and the return type of the function.  In all three cases the fields lexeme, token and depth are required.

 

When processing a function you must also insert all formal parameters into the symbol table at the appropriate depth along with their type.

 

Your parser must also reject multiple declarations of the same name at the same depth.

 

  1. int num, num2, num;

 

The second occurrence of num would be a multiple declaration.

 

Add an action that will print out the contents of the symbol table to the monitor as you exit each function.  Print out the lexeme and the class of the lexeme for each entry in the table at the depth you are exiting.  At the end of the program write out the same for all entries at depth one.