CS 3723 Programming Languages
Spring 2001 -- Review for Mid-Term Exam
Exam is Wednesday, 21 March 2001
Topics:
- The Quadruple Machine:
- Target language for a simple compiler.
- Details of the machine.
- An interpreter for quadruples (Project 0).
- Finite Automata (DFAs and NFAs):
- The definition of NFA and DFA.
- The language accepted by (or recognized by) an FA.
- Simple examples.
- The algorithm to convert an NFA to a DFA that recognizes
the same language. (We stated without proof that there is always
a unique DFA with a minimal number of states that recognizes the
language recognized by any FA.)
- Scanners and the Use of Automata:
- Constructing a scanner based on a DFA
(using either gotos or a while loop and
select based
on a state variable).
- Practical examples, including the DFA for the "course"
language, and the construction of an actual scanner
(Project I).
- Formal Grammars and Languages:
- Definition of formal grammar (start symbol,
terminal symbols, non-terminal symbols, grammar rules).
(Also called a context-free grammar or a BNF grammar.)
- Derivation sequences (leftmost and rightmost).
- Language accepted by (or recognized by) a formal grammar.
- Parse trees. (Each derivation sequence determines a unique
parse tree, but a parse tree might have many derivations.)
- Ambiguity. (Definition: There is a sentence with more than
one parse tree, or with more than one leftmost derivation, or with
more than one rightmost derivation.)
- Ambiguity is bad.
- Remove ambiguity by either:
- Parsers and the Use of Grammars:
- Recursive Descent Parsers:
- Simple Shift-Reduce Parsers:
- A simple version of the methods in the text.
The text's methods don't require the grammar to be rewritten
as often as recursive descent does.
- Carries out the parse working backwards from an initial
sentence to the start symbol. Constructs a rightmost derivation
backwards.
- Uses a stack for symbols. Input symbols are always
(eventually) shifted onto the stack, but in some circumstances
a string of one or more symbols (mixed terminals or non-terminals)
might be "reduced", that is, matched with the right side of a
grammar rule that is used for backwards replacement on the stack
by the single non-terminal on the left side of the rule.
- Whether to shift or reduce is controlled by a table
which says what to do based on the symbol on the top of the
stack and on the "current" symbol. (The text's methods are
similar but more complicated.)
- See the review exercise below to help understand this material.
- Use of Parsers to Carry Out Translation or Other Tasks:
- Add extra code (semantic actions) to the parser, either
- In with the code of the functions representing each
non-terminal in recursive descent.
- Code attached to each grammar rule in shift-reduce.
The code is executed each time a rule is used in a reduction.
- A number of examples based on recursive descent parser of
arithmetic expressions or of Lisp S-expressions:
- Translate to RPN.
- Evaluator for arithmetic expressions.
- Translate ordinary arithmetic expressions to prefix
form (as in Lisp), or to fully parenthesized form.
- We talked a little about how one would carry out the same
translations using shift-reduce parsers.
- Other topics:
- Comiler overview.
- Contrasts:
- syntax versus semantics,
- run-time versus compile-time,
- translation versus interpretation.
Review Exercise on Shift-Reduce Parsers:
Consider the following grammar:
S ---> b M b ("S" is the start symbol)
M ---> ( L
M ---> a
L ---> M a )
Use the following shift-reduce table for this grammar:
| b | a | ( | ) | $ |
-----+-----+-----+-----+-----+------+
S | | | | | acc | ( "s" means "shift")
M | s | s | | | |
L | r | r | | | | ( "r" means "reduce")
b | | s | s | | r |
a | r | r | | s | | ( "acc" means "accept")
( | s | s | s | | |
) | r | r | | | |
- Carry out the shift-reduce parse of the following sentence,
showing the stack, current symbol, remaining symbols,
and next action to take at each stage. (This sentence has the
extra artifical symbol $ stuck in at the beginning
and the end.)
$ b ( ( a a ) a ) b $
(Remember that you should initially shift the starting
$ and then shift the next symbol.)
- Give the resulting parse tree.
Revision date: 2001-03-21.
(Please use ISO
8601, the International Standard.)