

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!


You should attempt these tutorial exercises before the PL3 tutorial session in the week concerned. Then you can contribute to the discussion during the tutorial session.
Sample solutions will be posted at the PL3 Moodle site later in the course. Attempt each exercise before consulting the sample solution.
1A. (Regular expressions)
In Cobol, an identifier consists of letters, digits, and hyphens (โ-โ), but always starts with a letter. Hyphens may not occur consecutively, nor at the end of an identifier. Thus โsetupโ, โset-upโ, and โs-et-upโ are well-formed identifiers, but โset--upโ, โ- setupโ, and โsetup-โ are ill-formed. (a) Write an RE that specifies the syntax of Cobol identifiers. (b) Write an EBNF grammar of Cobol identifiers.
1B. (Regular expressions)
The Unix utility program egrep provides a pattern-matching notation, which is summarized in the course notes (slide 1-16). (a) Show how each of the following egrep patterns could be expressed in standard RE notation: b[aeiou]t b.t bet b[aeiou]t b(a|e|i)+t (b) Write and test egrep commands to do the following with a given file: (i) Find every line that contains โโ, โโ, โฆ, or โโ. (ii) Find every line that contains a sequence of one or more lower-case letters enclosed between โ{โ and โ}โ. (iii) Find every line that contains a sequence of zero or more characters enclosed between โ{โ and โ}โ. (iv) Find every line that contains โMrโ, โMsโ, โMrsโ, or โMissโ. (v) Find every line that contains โbโ followed by one or more consecutive occurrences of โanโ followed by โaโ.
1C. (BNF)
Consider the mini-English BNF grammar in the course notes (slide 1-22). That grammar generates some sentences, such as โI sees the cat .โ, that would be ill- formed in the English language because the subject does not โagreeโ with the verb. (a) Modify the grammar to ensure that a 1st^ person subject agrees with a 1st^ person verb, and a 3rd^ person subject agrees with a 3 rd^ person verb. (b) Add 2 nd^ person subjects and verbs to the grammar. (c) (Optional) Add plurals to the grammar.
1D. (Phrase structure)
Calcโs BNF grammar includes the following production rules: expr = prim | expr โ+โ prim | expr โ-โ prim | expr โโ prim prim = num | id | โ(โ expr โ)โ (a) Draw the syntax trees of the following expressions: x+yz xy+z (b) Note that all operators have equal priority. Modify the grammar so that โโ has higher priority than โ+โ and โ-โ. For example, โx+yzโ should be evaluated like โx+(yz)โ, but โxy+zโ should be evaluated like โ(xy)+zโ.
1E. (Ambiguity)
Suppose that a programming languageโs BNF grammar includes the following