home—lectures—exams—hws—breeze (snow day)
lect06c
grammars, cont.
Review:
signature of map.
Btw: between map and filter, this covers a lot of the uses of loops.
We'll see 'fold' next week, which covers almost all the rest of the cases.
Functional programmers rarely use loops, because map,filter,fold do
almost all the cases you'd want.
(We will write a version of `while` next week, though, as a function!)
Review Grammars:
e.g.
S → aS
S → a
A Language: a set of strings.
(e.g. the set of all valid Java programs — an infinite set).
What language does the above grammar define?
The language defined by a grammar:
the set of all strings derivable.
To show that a
To specify a grammar, you officially need:
-
What are the terminals symbols?
(Our default: uppercase.)
-
What are the non-terminal symbols?
(Our default: lowercase.)
-
What are the rules?
A list of pairs of strings.
Written as lhs → rhs
-
What is the start-nonterminal, for the grammar?
(our convention: the first rule's left-hand-side is the start symbol.)
Most commonly seen:
context-free grammars, “CFG”: every rule's left-hand-side is a single non-terminal.
BNF: same as CFG, but use "::=" instead of →, and "|".
So we can't have “context sensitive” rules like
abSa → baSTa
V is → V's
ITEC420 teaser: If you allow full grammars like this, it can be shown that you
can't write a program to answer “is a given string derivable from a given grammar?”,
or even “does a given grammar generate any strings?” (!)
Note that the grammar for Java doesn't quite match "does the program compile" —
the CFG doesn't catch using-an-undefined-variable, for example.
(CFG grammars cannot capture this!)
This fact is usually ignored, and most everybody will say "yes there
is a CFG for Java."
regular grammar: each right-hand-side has at most one non-terminal.
(equivalent to regular expresssions, in expressibility:
A langauge is generated by some regular grammar if, and only if,
there is a regular expression that matches exactly the strings in a language.
We'll say "the language is regular", w/o committing ourselves
to any particular grammar or reg.exp.
(Note that these are regular expressions without back-references!)
A grammar for
<integer> --> <digit><integer> | <digit>
<digit> --> 0 | 1 | 2 | 3 ... | 9
<ident> --> <letter>|<letter><letterOrDigitSeq>
<letterOrDigitSeq> --> <letterOrDigit>|<letterOrDigit><letterOrDigitSeq>
<letterOrDigit> --> <letter> | <digit>
<letter> --> a | b | c ...
For comparison: see Java's Double syntax
EBNF: Allow shorthands in BNF: For a non-terminal A,
-
0-or-more As: A* (or, A…)
-
0-or-more As: A+
-
0-or-1 (optional) α/A: A? or [α]
Note that parentheses are used for grouping in regular expressions,
but usally not used in EBNF.
home—lectures—exams—hws—breeze (snow day)