Docsity

Assignment 3 for Compiler Construction 2008 | COP 5621, Assignments of Computer Science

Toggle sidebar
bg1
COP5621 - Fall 2008
Assignment 3
cparse
Your assignment is to read in a start symbol of a set of productions followed by the set of productions
from stdin and produce the collection of sets of LR(0) items to stdout. An example input file is givenon
the next page and is in the ˜whalley/cop5621exec/cparse.in file. The program makes the following assump-
tions about the input.
(1) The start symbol is the first character on the first line.
(2) Productions start at the beginning of each of the following lines and are of the form N->rhs,where
Nis a nonterminal and rhs is a string of nonterminals and terminals. A"|" representing alternative
rhs’s isnot allowed.
(3) A nonterminal is a single uppercase letter.
(4) A terminal is a single character that is not an uppercase letter,"@", or a "’".
The output consists of twoparts. First, the augmented grammar should be printed. Next, the sets of
LR(0) items will be printed. An example output file is givenonthe next page and is in the
˜whalley/cop5621exec/cparse.out file. The form of your output should match my output exactly,which will
facilitate grading. The program makes the following assumptions about the output.
(1) The position is represented with a "@".
(2) The augmented grammar start symbol is represented by a "’".
(3) A set of items is represented by the form I#, where # is a number.Sets are numbered starting from
zero.
(4) The size of an item is no more than 20 characters (including a NULL character).
If you decide to use C or C++, then you can use the following strings to format your output:
"I%d:\n" - set of items number
"%-20s goto(%c)=I%d\n" - item with a goto
"%-20s\n" - item without a goto
Youmay not assume anylimit to the number of productions, items in a set, or the number of sets. In
other words, use dynamically allocated linked lists.
E-mail your source file to [email protected] before the beginning of class on October 2.
pf2

Partial preview of the text

Download Assignment 3 for Compiler Construction 2008 | COP 5621 and more Assignments Computer Science in PDF only on Docsity!

COP5621 - Fall 2008

Assignment 3

cparse

Your assignment is to read in a start symbol of a set of productions followed by the set of productions

from stdin and produce the collection of sets of LR(0) items to stdout. An example input file is given on

the next page and is in the ˜whalley/cop5621exec/cparse.in file. The program makes the following assump-

tions about the input.

(1) The start symbol is the first character on the first line.

(2) Productions start at the beginning of each of the following lines and are of the form N->rhs , where

N is a nonterminal and rhs is a string of nonterminals and terminals. A "|" representing alternative

rhs ’s is not allowed.

(3) A nonterminal is a single uppercase letter.

(4) A terminal is a single character that is not an uppercase letter, "@", or a "’".

The output consists of two parts. First, the augmented grammar should be printed. Next, the sets of

LR(0) items will be printed. An example output file is given on the next page and is in the

˜whalley/cop5621exec/cparse.out file. The form of your output should match my output exactly, which will

facilitate grading. The program makes the following assumptions about the output.

(1) The position is represented with a "@".

(2) The augmented grammar start symbol is represented by a "’".

(3) A set of items is represented by the form I#, where # is a number. Sets are numbered starting from

zero.

(4) The size of an item is no more than 20 characters (including a NULL character).

If you decide to use C or C++, then you can use the following strings to format your output:

"I%d:\n" - set of items number

" % -20s goto(%c)=I%d\n" - item with a goto

" % -20s\n" - item without a goto

You may not assume any limit to the number of productions, items in a set, or the number of sets. In

other words, use dynamically allocated linked lists.

E-mail your source file to [email protected] before the beginning of class on October 2.

Below is an example input file.

E

E->E+T

E->T

T->T*F

T->F

F->(E)

F->i

Below is the corresponding example output file. The sets of items should actually be printed in a single

column, but was not on this page to save paper.

Augmented Grammar

´->E E->E+T E->T T->T*F T->F F->(E) F->i

Sets of LR(0) Items

I0: ’->@E goto(E)=I E->@E+T E->@T goto(T)=I T->@T*F T->@F goto(F)=I F->@(E) goto(()=I F->@i goto(i)=I

I1: ’->E@ E->E@+T goto(+)=I

I2: E->T@ T->T@F goto()=I

I3: T->F@

I4: F->(@E) goto(E)=I E->@E+T E->@T goto(T)=I T->@T*F T->@F goto(F)=I F->@(E) goto(()=I F->@i goto(i)=I

I5: F->i@

I6:

E->E+@T goto(T)=I T->@T*F T->@F goto(F)=I F->@(E) goto(()=I F->@i goto(i)=I

I7: T->T*@F goto(F)=I F->@(E) goto(()=I F->@i goto(i)=I

I8: F->(E@) goto())=I E->E@+T goto(+)=I

I9: E->E+T@ T->T@F goto()=I

I10: T->T*F@

I11: F->(E)@