Unit 3-1
Unit 3-1
A grammar consists of one or more variables that represent classes of strings (i.e., languages) . There
are rules that say how the strings in each class are constructed. The construction can use :
Or both
Context-Free Grammar
A context-free grammar (CFG) is a formal system used to describe a class of languages known
as context-free languages (CFLs). Purpose of context-free grammar is:
V is the (finite) set of variables (or non terminals or syntactic categories). Each variable
represents a language, i.e., a set of strings
T is a finite set of terminals, i.e., the symbols that form the strings of the language being
defined
P is a set of production rules that represent the recursive definition of the language.
S is the start symbol that represents the language being defined. Other variables represent
auxiliary classes of strings that are used to define the language of the start symbol.
A grammar is said to be the Context-free grammar if every production is in the form of:
V (Variables/Non-terminals): These are symbols that can be replaced using production rules.
They help in defining the structure of the grammar. Typically, non-terminals are represented
by uppercase letters (e.g., S, A, B).
T (Terminals): These are symbols that appear in the final strings of the language and cannot
be replaced further. They are usually represented by lowercase letters (e.g., a, b, c) or specific
symbols.
But on the right-hand side here it can be a Variable or Terminal or both combination of
Variable and Terminal.
The above equation states that every production which contains any combination of the 'V' variable
or 'T' terminal is said to be a context-free grammar.
Production rules: Specify how non terminals can be replaced with other non terminals or
terminals
(e.g., E→E+EE → E + EE→E+E).
Model Description
Regular
Match strings by describing their structure.
Expressions
Suppose we want to describe all legal arithmetic expressions using addition, subtraction,
multiplication, and division.
Production Rules:
CFG:
E → int
E → E Op E
E → (E)
Op → +
Op → -
Op → *
Op → /
Example Derivation:
⇒ E Op E
E
⇒ E Op int
⇒ int Op int
⇒ int / int
Designing a CFG
Examples:
S → ε | a | b | aSa | bSb
2. Balanced Parentheses:
S → ε | (S) | SS
Memory
Finite Unbounded recursion
Requirements
Non-CFG Example
a->bSa, or
a->ba is not a CFG as on the left-hand side there is a terminal which does not follow the CFGs rule.
Lets consider the string "aba" and and try to derive the given grammar from the productions given.
We start with symbol S, apply production rule S->bSa and then (S->a) to get the string "aba".
Parse tree of string "aba"
In the computer science field, context-free grammars are frequently used, especially in the areas of
formal language theory, compiler development, and natural language processing. It is also used for
explaining the syntax of programming languages and other formal languages.
o CFGs are good for defining basic rules of a language, but they can’t handle
everything.
o Some rules in English or programming languages are too complex for CFG.
o Sometimes, CFG can allow more than one meaning for the same sentence or code.
o This is called ambiguity, and it makes it hard for the computer to understand the
correct meaning.
o They can’t check if the types match, if variables are used properly, or if functions are
called correctly.