Professional Documents
Culture Documents
Grammars: Definitions Grammars Backus-Naur Form Derivation
Grammars: Definitions Grammars Backus-Naur Form Derivation
Definitions
Grammars
Backus-Naur Form
Derivation
terminology
trees
(2.1)
Definitions
(2.2)
Syntax
Semantics
Sentence
Language
a set of sentences
Lexeme
Token
Grammars
(2.3)
Contain 4 components
terminal symbols
nonterminal symbols
Grammars (continued)
(2.4)
4 components (continued)
productions
rules for transforming nonterminal
symbols into terminals or other
nonterminals
nonterminal ::= terminals and/or
nonterminals
each has lefthand side (LHS) and
righthand side (RHS)
every nonterminal must appear on LHS
of at least one production
(2.5)
Grammars (continued)
4 categories of grammars
regular
good for identifiers, parameter lists,
subscripts
context free
LHS of production is single non-terminal
context sensitive
recursively enumerable
enough
for PLs
(2.6)
Alternatives indicated by |
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Derivation
BNF (continued)
(2.7)
->
->
->
->
->
->
<stmts>
<stmt> | <stmt> ; <stmts>
<var> = <expr>
a | b | c | d
<term> + <term> | <term> - <term>
<var> | const
<program>
=>
=>
=>
=>
=>
=>
=>
=>
<stmts>
<stmt>
<var> = <expr>
a = <expr>
a = <term> + <term>
a = <var> + <term>
a = b + <term>
a = b + const
Derivation Terminology
(2.8)
(2.9)
Derivation Trees
A derivation tree is the tree resulting from applying
productions to rewrite start symbol
<stmts>
<stmt>
<expr>
<var>=
a
<term>+<term>
(2.10)
<expr>
<expr>
<expr>
const
<expr>
<op>
<expr>
<expr> <op>
<op> <expr>
const
<expr>
<expr>
const
const
const
<op>
<expr>
const
(2.11)
<expr>
<expr>
<term>
<term>
<term>/const
(2.12)
(2.13)
Grammar Hierarchies
(2.14)
(2.15)
(2.16)
Syntax Graphs
Are equivalent to CFGs
Terminals in circles
Non-terminals in rectangles
Lines and arrows indicate how constructs are built
type_identifier
(
identifier
,
constant
..
constant
(2.17)
(2.18)
(2.19)
Example
For the grammar:
<term> -> <factor> {(* | /) <factor>}
void term ()
{
factor ();
/* parse the first factor*/
while (next_token == ast_code ||
next_token == slash_code)
{
lexical (); /* get next token */
factor (); /* parse the next factor */
}
}