Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
Assignment 2
Regular Languages, Context-Free Languages, Pushdown Automata,
Lexical analysis, Parsing, and Turing machines
In these exercises, you will
? implement lexical analysers using lex (Problem 3);
? implement parsers using lex and yacc (Problems 1, 4);
? practise proof by induction (Problem 2);
? practise using the Pumping Lemma for regular languages (Problem 7);
? learn some more about Turing machines (Problem 5);
? learn more about Pushdown Automata (Problems 6–7).
How to manage this assignment
? You should start working on this assignment now, and spread the work over the time until
it is due. Aim to do at least three questions before the mid-semester break. Do as much as
possible before your week 10 prac class. There will not be time during the class itself to do
the assignment from scratch; there will only be time to get some help and clarification.
? Don’t be deterred by the length of this document! Much of it is an extended tutorial to get
you started with lex and yacc (pp. 2–5) and documentation for functions, written in C, that
are provided for you to use (pp. 5–7); some sample outputs also take up a fair bit of space.
Although lex and yacc are new to you, the questions about them only require you to modify
some existing input files for them rather than write your own input files from scratch.
Instructions
Instructions are as for Assignment 1, except that some of the filenames have changed. The file
to download is now asgn2.tar.gz, and unpacking it will create the directory asgn2 within your
FIT2014 directory. You need to construct new lex files, using chain.l as a starting point, for
Problems 1, 3 & 4, and you’ll need to construct a new yacc file from chain.y for Problem 4. Your
submission must include (as well as the appropriate PDF files for the exercises requiring written
solutions):
? a lex file prob1.l which should be obtained by modifying a copy of chain.l
? a lex file prob3.l which should also be obtained by modifying a copy of chain.l
? a lex file prob4.l which should be obtained by modifying a copy of prob3.l
? a yacc file prob4.y which should be obtained by modifying a copy of chain.y
? PDF files for the exercises requiring written solutions, namely, prob1.pdf, prob2.pdf, prob5.pdf,
prob6.pdf, and prob7.pdf.
1
Each of the problem directories under the asgn2 directory contains empty files with the required
filenames. These must each be replaced by the files you write, as described above. Before submission,
check that each of these empty files is, indeed, replaced by your own file.
To submit your work:
1. edit Makefile as described in Lab 0,
2. enter the command ‘make’ from within the asgn2 directory,
3. submit the resulting .tar.gz file to Moodle.
As last time, make sure that you have tested the submission mechanism and that you understand
the effect of make on your directory tree.
INTRODUCTION: Lex, Yacc and the CHAIN language
In this part of the Assignment, you will use the lexical analyser generator lex or its variant flex,
initially by itself, and then with the parser generator yacc.
Some useful references on Lex and Yacc:
? T. Niemann, Lex & Yacc Tutorial, http://epaperpress.com/lexandyacc/
? Doug Brown, John Levine, and Tony Mason, lex and yacc (2nd edn.), O’Reilly, 2012.
? the lex and yacc manpages
We will illustrate the use of these programs with a language CHAIN based on certain expressions
involving strings. Then you will use lex and yacc on a language CRYPT of expressions based on
cryptographic operations.
CHAIN
The language CHAIN consists of expressions of the following type. An expression consists of a
number of terms, with # between each pair of consecutive terms, where each term is either a string
of lower-case letters or an application of the Reverse function to such a string. Examples of such
expressions include
mala # y # Reverse(mala)
block # drive # cut # pull # hook # sweep # Reverse(sweep)
Reverse(side) # Reverse(direction) # Reverse(gear)
For lexical analysis, we wish to treat every lower-case alphabetical string as a lexeme for the token
STRING, and the word Reverse as a lexeme for the token REVERSE.
Lex
An input file to lex is, by convention, given a name ending in .l. Such a file has three parts:
? definitions,
? rules,
? C code.
These are separated by double-percent, %%. Comments begin with /* and end with */. Any
comments are ignored when lex is run on the file.
You will find an input file, chain.l, among the files for this Assignment. Study its structure
now, identifying the three sections and noticing that various pieces of code have been commented
out. Those pieces of code are not needed yet, but some will be needed later.
2
We focus mainly on the Rules section, in the middle of the file. It consists of a series of statements
of the form
pattern { action }
where the pattern is a regular expression and the action consists of instructions, written in C,
specifying what to do with text that matches the pattern.
1
In our file, each pattern represents a set
of possible lexemes which we wish to identify. These are:
? a string of lower-case letters;
– This is taken to be an instance of the token STRING (i.e., a lexeme for that token).
? the specific string Reverse;
– Such a string is taken to be an instance of the token REVERSE.
? certain specific characters: #, (, );
? white space, being any sequence of spaces and tabs;
? the newline character.
Note that all matching is case-sensitive.
Our action is, in most cases, to print a message saying what token and lexeme have been found.
For white space, we take no action at all. A character that cannot be matched by any pattern yields
an error message.
If you run lex on the file chain.l, then lex generates the C program lex.yy.c.
2 This is the
source code for the lexical analyser. You compile it using a C compiler such as cc.
$ flex chain.l
$ cc lex.yy.c
By default, cc puts the executable program in a file called a.out.
3 This can be executed in
the usual way, by just entering ./a.out at the command line. If you prefer to give the executable
program another name, such as chain-lex, then you can tell this to the compiler using the -o
option: cc lex.yy.c -o chain-lex.
When you run the program, it will initially wait for you to input a line of text to analyse. Do
so, pressing Return at the end of the line. Then the lexical analyser will print, to standard output,
messages showing how it has analysed your input. The printing of these messages is done by the
printf statements from the file chain.l. Note how it skips over white space, and only reports on
the lexemes and tokens.
$ ./a.out
mala # y #Reverse( mala)
Token: STRING; Lexeme: mala
Token and Lexeme: #
Token: STRING; Lexeme: y
Token and Lexeme: #
Token: REVERSE; Lexeme: Reverse
Token and Lexeme: (
Token: STRING; Lexeme: mala
Token and Lexeme: )
Token and Lexeme: <newline>
1This may seem reminiscent of awk, but note that: the pattern is not delimited by slashes, /. . . /, as in awk; the
action code is in C, whereas in awk the actions are specified in awk’s own language, which has similarities with C but
is not the same; and the action pertains only to the text that matches the pattern, whereas in awk the action pertains
to the entire line in which the matching text is found.
2The C program will have this same name, lex.yy.c, regardless of the name you gave to the lex input file.
3a.out is short for assembler output.
3
Try running this program with some input expressions of your own.
Yacc
We now turn to parsing, using yacc.
Consider the following grammar for CHAIN.
S ?→ E
S ?→ ε
E ?→ E#E
E ?→ STRING
E ?→ REVERSE(STRING)
In this grammar, the non-terminals are S and E. Treat STRING and REVERSE as just single
tokens, and hence single terminal symbols in this grammar.
We now generate a parser for this grammar, which will also evaluate the expressions, with #
interpreted as concatenation and Reverse(. . . ) interpreted as reversing a string.
To generate this parser, you need two files, prob1.l (for lex) and chain.y (for yacc):
? Copy chain.l to a new file prob1.l, and then modify prob1.l as follows:
– in the Definitions section, uncomment the statement #include "y.tab.h";
– in the Rules section, in each action:
? uncomment the statements of the form
· yylval.str = ...;
· return TOKENNAME;
· return *yytext;
? Comment out the printf statements. These may still be handy if debugging is
needed, so don’t delete them altogether, but the lexical analyser’s main role now is
to report the tokens and lexemes to the parser, not to the user.
– in the C code section, comment out the function main(), which in this case occupies
about four lines at the end of the file.
? chain.y, the input file for yacc, is provided for you. You don’t need to modify this yet.
An input file for yacc is, by convention, given a name ending in .y, and has three parts, very loosely
analogous to the three parts of a lex file but very different in their details and functionality:
? Declarations,
? Rules,
? Programs.
These are separated by double-percent, %%. Comments begin with /* and end with */.
Peruse the provided file chain.y, identify its main components, and pay particular attention to
the following, since you will need to modify some of them later.
? in the Declarations section:
– lines like
char *reverse(char *);
char *simpleSub(char *, char*);
.
.
.
which are declarations of functions (but they are defined later, in the Programs section);
4
– declarations of the tokens to be used:
%token <str> STRING
%token <str> REVERSE
– declarations of the nonterminal symbols to be used (which don’t need to start with an
upper-case letter):
%type <str> start
%type <str> expr
– nomination of which nonterminal is the Start symbol:
%start start
? in the Rules section, a list of grammar rules in BNF, except that the colon “:” is used instead
of →, and there must be a semicolon at the end of each rule. Rules with a common left-handside
may be written in the usual compact form, by listing their right-hand-sides separated by
vertical bars, and one semicolon at the very end. The terminals may be token names, in which
case they must be declared in the Declarations section and also used in the lex file, or single
characters enclosed in forward-quote symbols. Each rule has an action, enclosed in braces
{. . . }. A rule for a Start symbol may print output, but most other rules will have an action of
the form $$ = . . . . The special variable $$ represents the value to be returned for that rule,
and in effect specifies how that rule is to be interpreted for evaluating the expression. The
variables $1, $2, . . . refer to the values of the first, second, . . . symbols in the right-hand side
of the rule.
? in the Programs section, various functions, written in C, that your parsers will be able to use.
You do not need to modify these functions, and indeed should not try to do so unless you are
an experienced C programmer and know exactly what you are doing! Most of these functions
are not used yet; some will only be used later, in Problem 4.
After constructing the new lex file prob1.l as above, the parser can be generated by:
$ yacc -d chain.y
$ flex prob1.l
$ cc lex.yy.c y.tab.c
The executable program, which is now a parser for CHAIN, is again named a.out by default,
and will replace any other program of that name that happened to be sitting in the same directory.
$ ./a.out
mala # y #Reverse( mala)
malayalam4
Run it with some input expressions of your own.
Problem 1. [7 marks]
(a) Construct prob1.l, as described above, so that it can be used with chain.y to build a
parser for CHAIN.
(b) Show that the grammar for CHAIN given above is ambiguous.
4Malayalam is the main language of the southern Indian state of Kerala. The word was given as an example of a
palindrome by an FIT2014 student in a lecture in 2017.
5
(c) Find an equivalent grammar (i.e., one that generates the same language) that is not
ambiguous.
Cryptographic expressions
A cryptographic calculator performs simple operations on strings of a kind that are used in
classical cryptosystems. It is also able to combine these operations in a natural way.
Suppose x = x1x2 · · · xn and k = k1k2 · · · kt are two strings, where xi and ki denote the i-th
letters of x and k respectively.
The available operations are:
? sum: this is written x + k in our expressions, though our C function that computes it is
called sum(. . . ). The resulting string has length min{n, t}, and its i-th letter is xi +ki mod 26,
where the letters of the English alphabet correspond to numbers via a = 0, b = 1, . . . , z = 25.
For example,
x = thebushwasalivewithexcitement
k = mrskoalahadabrandnewbabyandthenewsspreadlikewildfire
x + k = fywlisswhsdljmejlglaycjrezhga
? difference: this is written x ? k, and is computed by the C function diff(. . . ). The
resulting string has length min{n, t}, and its i-th letter is xi ? ki mod 26.
? Vigen`ere cypher: this is written Vigenere(x, k), where x is the plaintext and k is the key.
We first concatenate k with itself as many times as necessary in order to make it at least as
long as x. Then we form the sum. The result is a string whose i-th letter is
(xi + k((i?1) mod t)+1) mod 26.
For example, if the plaintext is
inaholeinthegroundtherelivedahobbit
and the key is
bilbo
then Vigenere(inaholeinthegroundtherelivedahobbit, bilbo) returns the cyphertext
jvlicmmtohimrscvvouvfzpmwwmobvpjmjh
? Simple Substitution: this is written SimpleSub(x, k). Again, x is plaintext, and k is the
key, but this time k must be a permutation of the 26-letter English alphabet, represented as
a string in which each letter appears exactly once (and t = 26). Every a in the plaintext is
replaced by the 1st letter k1 of the key; every b in the plaintext is replaced by the 2nd letter,
k2, of the key; and so on. In general, the plaintext letter xi
is replaced by kxi
.
For example, if the plaintext is
thequickbrownfoxjumpsoverthelazydog
and the key is
qwertyuiopasdfghjklzxcvbnm
then SimpleSub(thequickbrownfoxjumpsoverthelazydog,qwertyuiopasdfghjklzxcvbnm) returns
the cyphertext
zitjxoeawkgvfygbpxdhlgctkzitsqmnrgu
6
? Local Transposition: this is written LocTran(x, k). The letters of the plaintext x are not
replaced, as happens in the previous cyphers, but rather rearranged according to a permutation,
which is represented by a string k of w digits, where w = |k|.
5 This permutation string k has
length ≤ 10, and consists of the digits 0, 1, . . . , w ? 1 arranged in some order. For example,
if k has length 3 (so w = 3), then k can be any of the strings 012, 021, 102, 120, 201, 210.
The plaintext x is divided into blocks of w letters each, and the letters within each block are
permuted according to k. If there are extra letters at the end — too few letters to make up
another full block — then these are just copied across, with no change in their positions.
For example, if the plaintext is
thefamilyofdashwood
and the key is
201
then LocTran(thefamilyofdashwood,201) returns the cyphertext
ethmfayildofhasowod
These operations can be combined. Any valid expression can be given as the first argument
to one of the cypher functions, or as any argument of a sum or difference, to give another valid
espression. So you can form expressions like
Vigenere(LocTran(triantiwontigongolope,3201),bunyip) + muldjewangk
Let CRYPT be the language of cryptographic expressions of this type that can be generated by
the following grammar.