![]() |
![]() |
|
home—lectures—exams—hws—breeze (snow day)
New terms: - the *language* a grammar defines. - "parse" - un/ambiguous (expr) - distinguish: the grammar from its semantics
S → aS S → b
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 string is in the grammar, show me a derivation (possibly a left-most derivation); equivalently show me a parse-tree.
To specify a grammar, you officially need four things:
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: java syntax: numbers
EBNF: Allow shorthands in BNF: For a non-terminal A,
Example:
Write a simple grammar that matches arithmetic-expressions:
home—lectures—exams—hws—breeze (snow day)
©2012, Ian Barland, Radford University Last modified 2012.Oct.10 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |