
EECS 476, Prog. Lang. Design: Formal Language Description/ page 1
Formal Language Description
I. Syntax
A. Formal description is helpful (definition by example is not sufficient)
1. It facilitates understanding the lang.
2. It can be used to "automatically" create syntax analysis part of compiler; also debug-
gers
B. Formal Language Theory: Chomsky in 50's
1. A grammar describes a language and consists of:
a. Start symbol
b. Set of terminal symbols (alphabet of the language)
c. Set of non-terminal symbols
d. Set of rewrite (production) rules
2. E.g. Simple grammar for subset of English [Drake]:
S -> NP VP
NP -> Art Adj N | Art N | N
VP -> V | V NP | V PP
PP -> Prep NP
N -> book | class | quiz
etc.
3. Can build a parse tree
4. Chomsky Hierarchy
a. Type 0: recursively enumerable. Left side of rule must be non-empty
b. Type 1: context-sensitive. xAy -> xBy
c. Type 2: context-free. Allows only a single non-terminal on l.h.s.
d. Type 3: regular. r.h.s. may only be a single terminal or single nonterminal fol-
lowed by a single terminal (left recursive) Note it can't define nested structures.
- Lexers usually described using a regular lang.,
- Syntax of prog. lang. usually described using a context-free lang.
C. BNF
1. E.g. A small grammar & a derivation [Seb p.109-110]
Trace derivation by numbering rules and showing where thay are applied
a. Each string in derivation is a sentential form
b. Always replacing left-most non-terminal gives a left-most derivation
2. Ambiguous grammar for assignment statement [Seb. pp.110-113]
Grammars describe the hierarchical structure of sentences in the language they
describe
a. Parse Tree - a representation of the hierarchical structure. Every subtree of a
parse tree describes one instance of an abstraction in the statement
b. Syntax analyzers for compilers generate parse trees either explicitly or implicitly
(either top-down or bottom-up)
c. Ambiguity is a problem: Semantics will be determined by syntax (e.g. which
operation goes first: + or / ?)
d. Operator Precedence: transformations lower in the tree have precedence (they
occur before) over transformations higher in the tree (which occur later). An
ambiguous parse tree means there is conflicting precedence information. [see
overhead]