110 likes | 122 Views
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.
E N D
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.
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.
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.
Context Free Grammar (CFG): technical definition A CFG is a collection of three things; • An alphabet of letters called terminal, from which strings or words of the language are formed. • A set of symbols called non-terminals, one of which is the symbol S, standing for :start here”. • A finite set of productions of the form One non-terminal finite string of terminals and /or non-terminals
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
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:
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
Note: • 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. • 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*
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: