You are on page 1of 2

Programming Languages

Assignment 3

Note:
1. The answer to the theory question should be submitted in a text file (DOC, DOCX, TXT,
or PDF).
1. Explain the following with examples (if applicable):
(a) Relation between regular expressions and finite state automata
(b) Relation between NFAs and DFAs
(c) Lookahead
(d) Context free grammars and BNF notation
(e) Relation between regular expressions and context free grammars
(f) Parse tree and abstract syntax tree
(g) Left recursion and left factoring
(h) Derivation, leftmost derivation, rightmost derivation
(i) Ambiguity in grammars
(j) Are common programming languages context free or context sensitive. If not, give an
example.
2. Write regular expressions for the following languages:
1. All strings of lowercase letters that contain the five vowels in order
2. All strings of lowercase letters in which the letters are in ascending lexicographic order
3. Comments, consisting of a string surrounded by /* and */, without an intervening */,
unless it is inside double-quotes(")
3. Write finite state machines (either DFA or NFA) to represent the following regular expressions:
1. a(a|b) a
2. ((|a)b)ast
4. Modify the hand-crafted lexical analyser we had discussed in the class to have the following:

(a) Modified ID token class to allow an underscore anywhere in the identifier. For example:
a_a, a_, _a are all valid identifiers.
(b) Additional token classes to identify white-spaces (tabs and spaces) and newlines, punctuation marks (parenthesis marks, curly braces, semi colon, colon), operators (addition,
multiplication, assignment, equals, greater than, greater than and equal to).
Note: Please follow the code structure of the code that was shared, i.e. each FSA should have
an interface, implementation and test file. Relevant rules should be added to the makefile to
allow unit-testing and integration into the main lexer program. Your implementation should
tested against valid and invalid inputs. (directory: lexer/manual/)
5. (a) Implement all the above using the OCamllex tool. Use the OCamllex file discussed in the
class as a starter.
(b) Have a token class Keyword of string. This should get returned when an id is detected,
and its lexeme is member of a predefined set of strings (e.g. int, for, let etc.). You may
use a Hastbl or just a list to implement this set. Populate it at the lexer initialisation
stage.
(directory: lexer/auto/)
6. (a) Write a context free grammar for a calculator which allows saving values in variables for
later use. For example, the following is a valid input for the calculator:
a = 10;
b = a * 20;
c = b / a;

Features like branching, looping and function calls are not needed in this language.
(b) Write a modified and equivalent version of the grammar to make it amenable to be parsed
using recursive descent parsing.
(c) Implement a recursive descent parser for this language using OCaml.
(d) Demonstrate the correct working of your parser by creating an appropriate set of test
cases.
(directory: parser/manual/)
7. Implement the parser for the above grammar using OCamlyacc. This grammer should work
for the test cases in Q. 6. (directory: parser/auto/)

Page 2

You might also like