You are on page 1of 2

CS416 Compiler Design

Spring 2007 Programming Assignment 3 Due Date: April 24, 2007


In this homework, you are going to write a menu-driven program. The menu should have the following parts: Create A New LR(1) Parser - When this option is selected your program should read a CFG from a text file whose name is given, and create its LR(1) parsing table. If the given CFG is not a LR(1) grammar your program should be able to give a message indicating this fact. Parse A String If a LR(1) parsing table is created using the first option, read a string from keyboard (or from a file) and parse that string with the current LR(1) parser. If the given string is a syntactically correct, your program should print its rightmost derivation. If it is not, it should print error messages. Quit Quit from your system. The details of your system should be as follows: Create A New LR(1) Parser Your program should read a CFG from a text file whose file name is given. You may assume that all upper case letters are non-terminals and all other printable characters are terminals. Each line of this text file contains a single production rule. A production rule should be in the following form: anonterminal -> astring where is anonterminal is a non-terminal (an upper case letter) and astring is a string of terminals and non terminals (including the empty string)

For example, the followings are some possible production rules for a CFG: S -> aS S -> bA S -> A -> c A -> bAb

This means that the production rule is S

The non-terminals and the terminals of the given CFG are the non-terminals and the terminals appearing in the given production rules. The starting non-terminal is S. You may assume that the syntax of the production rules (as described above) is correct, and you do not have to handle the incorrect ones. If the given CFG is not a LR(1) grammar, your program should print a message indicating this fact together with the reason (which entries contain conflicts). If the given CFG is a LR(1) grammar, it should create its LR(1) parsing tables, and store it internally. Your program should also print that LR(1) parsing tables together with the canonical LR(1) collection into a file. Parse A String If there are LR(1) parsing tables that are created, your program should read a string and should parse using those LR(1) parsing tables. If the given string is in the language of the current grammar, your program should print its rightmost derivation. If it is not, your program should give error messages. Your program should give the positions of the errors together with error messages. Your program should use panic-mode error recovery to be able to give multiple error messages. For example, if the current grammar is the CFG above and the given string is abc , your program should print the following rightmost derivation below: S => aS => abA => abc For example, if the given string is an erroneous string abbab, it should print the following error message Error: Unexpected a in A at the position 4.

You should hand-in: Your source program (email to your assistant (Turhan Daybelge) ( daybelge@cs.bilkent.edu.tr )) Some test runs of your program with correct and incorrect strings (email to the assistant) Executable of the your program by email and a message indicating how to run your executable on a PC or a Unix machine which is available at our department.

DO IT YOURSELF CHEATING WILL BE PUNISHED