September 13 th Grammars Grammars They are a
September 13 th Grammars
Grammars • They are a way of describing the syntax of programming languages • We will be using – BNF grammar (as in Mini-Language Core) – Syntax diagrams (in Pascal) • BNF grammar – Was invented in 1958 as a way of describing Algol 58 by John Backus
BNF grammars • BNF grammar is context-free grammar • Noam Chomsky – classified grammars – Context-free grammar – Regular grammar – Context-sensitiver grammar –…
Characteristics of grammar • A grammar consists of – A four-tuple • • A set of terminals A set of non-terminals A set of rules (or productions) A start (or goal) symbol
Uses of grammars • It can be used to generate legal sentences in a programming language • It can be used to determine whether a given sentence in a programming language is correct – parsing
What did we learn from Mini. Language Core’s BNF • It described the language well enough for most of us to write a program in an unfamiliar language • We could determine whether a given statement we wrote in Mini-Language Core was correct • It really only told us about syntax, not about semantics • There were a number of things that we could not determine from the grammar
Terminology • Lexemes – lowest level syntactic units • Token – is a category of lexemes – index = 2 * count + 17; • • Index, count are identifiers =, *, + are operators 2, 17 are integer literals ; is a separator or terminator • Metalanguage – is a language used to describe other languages • Aside: identifiers can reference variables, constants, methods, programs, etc.
Potential Problems • A grammar may be ambiguous – Two different parse trees may be generated for the same expression.
Sections 3. 4 and later • Less important in this class – read to be able to answer homework questions but don’t agonize over the material in axiomatic and denotational semantics.
- Slides: 9