COMS W4115
Programming Languages and Translators
Homework Assignment #1
Due: 4:10pm, October 12, 2009



Instructions

Problems

  1. 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).
    1. All lowercase English words with a substring of five or more vowels. A vowel may be repeated in the substring.
    2. All lowercase English words with exactly one vowel.
    3. All lowercase English words with the subsequence aeiou.
    4. All lowercase English words in which the letters are strictly monotonically decreasing lexicographically as in "spoke".

  2. 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.
    1. Construct a deterministic finite automaton (DFA) for L.
    2. Minimize the number states in your DFA.
    3. From your minimized DFA construct a regular expression for L.
    4. Show how your regular expression generates aababa, and how your automaton recognizes aababa.

  3. Sequences of boolean expressions
    1. 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.
    2. Show the parse tree according to your grammar for the input
    3.    TRUE && ! FALSE || FALSE
    4. 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.
    5. Run your interpreter on the inputs
    6.    TRUE || FALSE
         TRUE && FALSE
         TRUE && (TRUE || FALSE)

  4. 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.
    1. Construct an LL(1) grammar for L.
    2. Construct a leftmost derivation for the string 010010.
    3. Construct a predictive parsing table for your grammar.
    4. Show how your predictive parser parses the string 010010.

  5. Consider the Yacc specification in Fig. 4.59 of ALSU (p. 292).
    1. 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+*".
    2. Implement your translator in Yacc (or its equivalent) and show the output for various inputs.



aho@cs.columbia.edu