Computer programming - Lab 4

If any questions remain about these problems, please ask during office hours.

Text processing

1. Collapse spaces Write a program that reads all input and prints it transformed as follows:
- any sequence of whitespace that does not contain newline is replaced with a single space character
- whitespace characters immediately before a newline are deleted.

2. Paragraph info Write a program that reads all input and prints for each paragraph the number of words and lines.
A paragraph is a portion of text that does not start with a newline, ends with a single newline or EOF and is separated by at least one newline from other paragraphs.

3. Fixed-width printing Write a function that takes an unsigned n as parameter, and prints a text read until end of input, fitting n characters per line. Words (sequences of non-whitespace chars) are printed with one space in between. A word that would exceed the line limit will be split inserting a hyphen - as last character on the line (no extra hyphen is needed if the last character that fits is a hyphen itself). Newlines in the original text are observed.
Hint: to handle the end of the line, write a function peek() which returns the next character without consuming it (use ungetc()).

4. Prettyprinting Write a program that properly indents a text read from standard input that has balanced braces { } . The output is formed as follows:

5. Nested comments Comments in the ML language start with (* and end with *). Comments may be nested.
Write a program that reads input with potentially nested comments and prints out everything except the comments.
Hint: Remember how many comment levels are open or use recursion to do that.

Reading input defined by a grammar

When the structure of the input is precisely defined by a grammar, the recursion can be directly converted into code by writing a function for each nonterminal (a grammar symbol which is defined by a rule in terms of other symbols). We have seen this in the examples for prefix expressions (also for normal(infix), postfix) -- each function there computes a value. If we only need to check if the input is well formed, it's even easier, our function will just return 1 (true) or 0 (false), see this simple example.
Use the same approach to solve similar problems.

6. Conditional expressions An expression is either an unsigned number or a conditional expression of the form (if expr1 expr2 expr3). The meaning is like in C: if the value of expr1 is nonzero, the conditional expression has the value of expr2, else that of expr3.
Write a function that returns the value of an expression read from input, or -1 on error. Whitespace in expressions is arbitrary.

7. Postfix expressions. Write a program that computes and prints the value of an expression in postfix form, read from standard input. An expression is a natural number, or two expressions followed by an operator + - * / and separated by any whitespace.
Example: 7 4 2 + - 5 - means 7 - (4 + 2) - 5
Hint: An expression is also defined by the grammar Expr ::= Number RestExp where RestExp ::= empty | Expr op RestExp
RestExpr will be either a loop inside Expr or a function which takes the value already computed to the lect of it as parameter; the operator op is applied to that value and the new Expr.


Marius Minea
Last modified: Thu Oct 19 20:15:00 EEST 2017