Header Ads

  • Highlights

    👩🏻‍💻 Hello Readers 🧑🏻‍💻!!! Would you like to Explore more ? Hit on ⓘ(Ads)... ✨🌹

    What is Context Free Grammar ?


    What is Context Free Grammar ?

    As we know that grammar is a set of rules, similarly context-free grammar has some set of recursive rules through which a pattern is generated. Which describes context-free languages. This Grammar is called context-free grammar because in this production rules are independent of context.

    In general, a context-free grammar G is a quadruple(4-tuple) (V, Σ(T), R(P), S) where,

    V is a set of variables,

    Σ set of terminal symbols and subset of V,

    R is a set of rules called as production rules (V − Σ) × V*,

    S is a start symbol V − Σ.

    (Nonterminals are usually represented by capital letters and terminals by lower case letters.) 

    Every CFG production rule can be written as Sa, where S is a unique nonterminal symbol and a is some combination of non-terminals or production rules.

    For example, the following is context-free grammar, as is any combination of the rules in isolation.

    Rule 1: S→aSa

    Rule 2: S→bSb

    Rule 3: S→ϵ

    (Epsilon is the empty string.)

    The following are examples of strings that may be constructed by this grammar: that is, members of the CFL, L, described by this CFG, and the corresponding derivations.

    aa: Rule 1, followed by Rule 3

    aabb: Rule 1, followed by Rule 2, followed by Rule 3.

    The empty string (Rule 3)

    The grammar G, with productions

    S → abB,

    A → aaBb,

    B → bbAa,

    A → λ,

    is context-free. We leave it to the reader to show that

    L (G) = {ab (b b a a) n b b a (b a) n : n ≥ 0}

    Context-free grammars are used in compilers and in particular for parsing, taking a string-based program, and figuring out what it means. Typically, CFGs are used to define the high-level structure of a programming language.

    Click here to have Examples based on Context-Free Grammar

    What are the applications of context-free grammar?

    • A context-free grammar is used as the basis for compiler design and implementation. 
    • Context-free grammars are used as specification mechanisms for programming languages 
    • Designers of compilers use such grammars to implement compiler’s components, such as scanners, parsers, code generators, code synthesizers.
    • The implementation of almost any programming language is preceded by a context-free grammar that specifies it.

     Also, Study about: 

    Also Read, some interview tips

    No comments

    Post Bottom Ad

    Powered by Blogger.