***************************** CS570/670 -- Spring 2002 LAB6 Instructor: Ernesto Gomez ******************************Write a program that does the following:1. Reads a grammar.2. Finds the canonical LR
***************************** CS570/670 -- Spring 2002 LAB6 Instructor: Ernesto Gomez ******************************Write a program that does the following:1. Reads a grammar.2. Finds the canonical LR(0) sets.INPUT:A grammar G.OUTPUT:The list of all productions in GThe canonical LR(0) collection of sets for G- In addition to numbering the sets, as done in the text, identify the symbol X that generates the set through the function GOTO(I,X)DELIVERABLES:Program source code, with internal documentationTest results for grammar G419 and at least one othergrammar. HINT:====Modify your Lab 2 program for this exercise. The routines to input andprint the grammar are the same, and the data structure to hold the itemsis the same as the data structure to hold a production, with a singleadded data item to indicate the position of the dot (.) in the righthand side.If you leave the routines for FIRST and FOLLOW as part of the program,even though you do not use them in this exercise, the resulting codecontains most of the ingredients you need to produce an SLR parser.ALGORITHMS:==========You will need the algorithms for CLOSURE (pg. 222) and GOTO (pg. 224).The algorithms are recursive, they start from the unique production thatcontains the start symbol of an extended grammar. As described in section4.7 in the text, alternately apply CLOSURE and GOTO until nothing morecan be done. Note that CLOSURE(I) fills out sets, GOTO(I,X) may add newsets of items to the collection. In addition to labelling sets of items with a number (as the book does),label all the sets of items with the symbol that produces the set in the GOTO procedure. The first set of items, I0, should be labelled with thestart symbol of the extended grammar.Optional: when computing the GOTO function, you will find that it linksparticular sets of items: for an item I in set Ik, GOTO(I,X) will producean item in the set Ij, where Ij is the set of items generated by X.Keep track of which sets of items can GOTO which other sets; this givesyou a directed graph which is the DFA used in a parser. When printingout a set of items, also print a list of the numbers of the othersets of items to which GOTO can transfer. (10 extra points).NOTES:=====Follow the textual conventions on pg.166 of text:A is a non-terminal.X is a grammar symbol (terminals or non-terminal).In addition we will use the following conventions:1 - all terminals will be single characters.2 - all non-terminals will be single letters.3 - epsilon (the empty string) is: e4 - the "produces" symbol in productions is: ->5 - end of input / end of section symbol is: $The format for a grammar is as follows:terminalterminal..terminal$productionproductionproduction$A first section with one terminal per line, ending in $, anda second section with one production per line, ending in $.The format of a production is:A->stringwhere the string represents a single produciton with no whitespaceor "or" symbols, and A is a single non-terminal.Sample grammar:==============Example 4.19 from the book is in ~egomez/cs570/first/g419. Thefollowing modifications are made, to conform to the format above:epsilon is replaced with e.E' is replaced with S.id is replaced with i.LR(0) sets for this grammar are on page 225At least one more sample grammar will be placed in the same directory.Optional features:=================You may choose to write your gramman interpreter so that it understands the "or" symbol: |You may choose to ignore whitespace for better readability.
it has to be programmed in c
Show more