Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars."— Presentation transcript:

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

2 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

3 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 1956. 3

4 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

5 Context Free Grammar (CFG): technical definition A 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 form One non-terminal  finite string of terminals and /or non-terminals 5

6 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 | bX A  bA X  cX The sign  means can be replaced by and the sign  means develop into 6

7 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

8 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

9 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

10 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


Download ppt "CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars."

Similar presentations


Ads by Google