csc312 automata theory lecture # 26 chapter # 12 by cohen context free grammars

10
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars

Upload: amie-mcdowell

Post on 17-Jan-2018

222 views

Category:

Documents


0 download

DESCRIPTION

Semantics: The grammatical rules which involve the meaning of words are called Semantics e.g. in English language, the sentence “Buildings sing” make no sense. Syntactics: The grammatical rules that don’t involve the meaning of the words but the structure of the words. Context Free Grammar (CFG): general definition A grammar or language based on rules that describe a change in the string without reference to elements not in the string. The concept of CFG was introduce by the linguist Noam Chomsky in

TRANSCRIPT

Page 1: CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars

CSC312Automata Theory

Lecture # 26

Chapter # 12 by CohenContext Free Grammars

Page 2: CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars

Compiler: A compiler is program that converts a high level language code into its equivalent assembly language. Grammar:Grammar is a set of rules by which a valid sentence in a language is constructed.Parsing the sentence:Parsing is the process of analyzing a text, made of a sequence of tokens (e.g. words), to determine its grammatical structure with respect to a given formal grammar.

2

Page 3: CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars

Semantics:The grammatical rules which involve the meaning of words are called Semantics e.g. in English language, the sentence “Buildings sing” make no sense.Syntactics:The grammatical rules that don’t involve the meaning of the words but the structure of the words. Context Free Grammar (CFG): general definitionA grammar or language based on rules that describe a change in the string without reference to elements not in the string.The concept of CFG was introduce by the linguist Noam Chomsky in 1956.

3

Page 4: CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars

CFG Terminology:Terminals:The symbols that cannot be replaced by anything are called terminals. Non-Terminals:The symbols that must be replaced by other things are called non-terminals. e.g.variable = expr;Derivation:The sequence of application of the rules that produces the finished string of terminal from the starting symbol is called a derivation. Productions:The grammatical rules are often called productions.

4

Page 5: CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars

Context Free Grammar (CFG): technical definitionA CFG is a collection of three things;1.An alphabet of letters called terminal, from which strings or words of the language are formed. 2.A set of symbols called non-terminals, one of which is the symbol S, standing for :start here”.3.A finite set of productions of the formOne non-terminal finite string of terminals and /or non-terminals

5

Page 6: CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars

Note: The terminal are designated by small letters, while the non-terminals are designated by cpital letters. There is at least one production that has the non-terminal S as its left side. Example:

S aA | bXA bAX cX

The sign means can be replaced by and the sign means develop into

6

Page 7: CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars

Context Free Language (CFL): The language generated by CFG is called context Free Language (CFL).Note: CFG can generate all regular languages and some non-regular languages, but not all the non-regular languages. Examples:

7

Page 8: CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars

Ambiguous CFG:If a CFG can generate a specific string in more than one different ways (i.e. more than one leftmost derivations or parse trees) then it is called ambiguous CFG. Note:A CFL is inherently ambiguous if all the CFGs generating the language are ambiguous.Some programming language have ambiguougs grammars e.g. C-language.Examples of CFG

8

Page 9: CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars

Note:i)We can generate CFG for each and every regular language. However, there are some nonregular languages for which we can construct CFG and for some nonregualr language we cannot construct CFG.ii)It may be noted that the productions

S SS|always define the language which is

closed w.r.t concatenation i.e. the language expressed by RE of type r*

9

Page 10: CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars

Note:iii) Null string can not be terminal, because it is not part of alphabet. It terminates non-terminals.iv) The string where non-terminal remain on right side is called working string. v) If a word has at the end, then we write = after removing it, otherwise we place sign. Examples:

10