COMS W4115
Programming Languages and Translators
Homework Assignment #1
Due: 4:10pm, October 12, 2009
Instructions
- Do this assignment by yourself.
- You may consult the instructor or a TA for clarification,
but no other people.
- Problems (1-5) are each worth 20 points.
- Solutions to these problems will be posted
on Monday, October 12, 2009.
Problems
- Construct Lex-style regular expressions for the following patterns.
For each pattern, create a Lex program to find the first longest word
in the dictionary satisfying that pattern. As part of your
answer list your program and the longest word found. State what dictionary
you used (e.g., on Linux systems
/usr/dict/words
).
- All lowercase English words with a substring of five or more vowels.
A vowel may be repeated in the substring.
- All lowercase English words with exactly one vowel.
- All lowercase English words with the subsequence
aeiou
.
- All lowercase English words in which the letters are strictly monotonically
decreasing lexicographically as in "spoke".
- Let L be the language consisting of all
nonempty strings of
a
's and
b
's which, if they contain a b
, have every
b
immediately preceded and followed
by an a
.
- Construct a deterministic finite automaton
(DFA) for L.
- Minimize the number states in your DFA.
- From your minimized DFA construct a regular expression for L.
- Show how your regular expression generates
aababa
,
and how your automaton recognizes aababa
.
- Sequences of boolean expressions
- Construct a grammar that generates newline-terminated
sequences of boolean expressions
containing the logical
constants TRUE and FALSE, and the boolean operators
&& (for conditional AND), || (for conditional OR),
and ! (for NOT). Expressions can contain parentheses.
- Show the parse tree according to your grammar for the input
- TRUE && ! FALSE || FALSE
- Implement an interpreter that takes as input
lines of boolean expressions and produces as
output the truth value of each expression.
You can use lex and yacc or their equivalents
to implement your interpreter. Print the source
code for your interpreter and the sequences of
commands you used to create it and test it.
- Run your interpreter on the inputs
- TRUE || FALSE
- TRUE && FALSE
- TRUE && (TRUE || FALSE)
- Let L be the language consisting of all strings of
0
's and 1
's having the same number of
0
's as 1
's.
- Construct an LL(1) grammar for L.
- Construct a leftmost derivation for the string
010010
.
- Construct a predictive parsing table for your grammar.
- Show how your predictive parser parses the string
010010
.
- Consider the Yacc specification in Fig. 4.59 of ALSU (p. 292).
- Modify this translator to produce a postfix Polish expression for
each input line. For example, for the input "1*(2+3)" your translator
should produce "123+*".
- Implement your translator in Yacc (or its equivalent) and show the
output for various inputs.
aho@cs.columbia.edu