Unit-3-Parser Basics, Need and Role of Parser
Unit-3-Parser Basics, Need and Role of Parser
Variable re-declaration
Variable initialization before use.
Data type mismatch for an operation.
CONTEXT-FREE GRAMMARS
A Context-Free Grammar is a quadruple that consists of terminals, non-terminals, start symbol
and productions.
Terminals : These are the basic symbols from which strings are formed.
Non-Terminals : These are the syntactic variables that denote a set of strings. These help to
define the language generated by the grammar.
Start Symbol : One non-terminal in the grammar is denoted as the “Start-symbol” and the set of
strings it denotes is the language defined by the grammar.
Productions : It specifies the manner in which terminals and non-terminals can be combined to
form strings. Each production consists of a non-terminal, followed by an arrow, followed by a
string of non-terminals and terminals.
Example: Considerthefollowinggrammarforarithmeticexpressions:
E → E+E |E*E |( E ) | - E | id
Word = “- (id+id ) “
To generate a valid string - (id+id ) from the grammar the steps are
1. E → - E
2. E → - (E )
3. E → - (E+E )
4. E → - (id+E )
5. E → - (id+id )
In the above derivation,
E is the start symbol.
-(id+id) is the required sentence (only terminals).
Strings such as E, -E, -(E), . . . are called sentinel forms.
Types of derivations
The two types of derivation are:
1. Left most derivation - In leftmost derivations, the leftmost non-terminal in each
sentinel is always chosen first forreplacement.
2. Right most derivation - In rightmost derivations, the rightmost non-terminal in each sentinel
is always chosen first for replacement.
Sentinels:
Given a grammar G with start symbol S, if S → α , where α may contain non-
terminals or terminals, then α is called the sentinel form of G.
Yield or frontier of tree:
Each interior node of a parse tree is a non-terminal. The children of node can be a
terminal or non-terminal of the sentinel forms that are read from left to right. The
sentinel form in the parse tree is called yield or frontier of the tree.
Ambiguity:
A grammar that produces more than one parse for some sentence is said to be
ambiguous grammar.