Compiler 8
Compiler 8
تصميم المترجمات
حسب ()412
:Lecture 8
Context Free
Grammar
factor,
the start symbol is : expression .
Example : Grammar for simple
arithmetic expressions
expression → expression + term
expression → expression - term
expression → term
term → term * factor
term → term / factor
term → factor
factor → ( expression )
factor → id
Informally a Context-Free Language
(CFL) is a language generated by a
Context-Free Grammar (CFG).
Informally, a CFG is a set of rules for
E E (E ) ( E E )
(id E ) (id id )
Derivations
E E (E ) ( E E )
( E id ) (id id )
E E+E
id + E
id + E*E
id + id * E
id + id * id
leftmost derivations for the
sentence
id + id * id
E E*E
E+E*E
id + E * E
id + id * E
id + id * id
Parse Trees and Derivations
A parse tree is a graphical representation of a
derivation that alters out the order in which
productions are applied to replace
nonterminals.
Each interior node of a parse tree represents
set
of all strings of balanced
parentheses {(), (()), ((())), …},
Verifying the Language Generated
by a Grammar
Describe the language produced by
the following Grammar:
S 0 S1
S
theset of all 0s followed by an equal
number of 1s, {01, 0011, 000111, ...}.