0% found this document useful (0 votes)
6 views5 pages

Unit-3-Parser Basics, Need and Role of Parser

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Unit-3-Parser Basics, Need and Role of Parser

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Issues handled by Semantic Analysis phase are

 Variable re-declaration
 Variable initialization before use.
 Data type mismatch for an operation.

Syntax error handling :


Programs can contain errors at ma ny different levels. For example :
 Lexical, such as misspelling akeyword.
 Syntactic, such as an arithmetic expression with unbalanced parentheses.
 Semantic, such as an operator applied to an incompatible operand.
 Logical, such as an infinitely recursive call.
Functions of error handler :
 It should report the presence of errors clearly and accurately.
 It should recover from each error quickly enough to be able to detect subsequent errors.
 It should not significantly slow down the processing of correct programs.

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 of context-free grammar: The following grammar defines simple arithmetic


expressions:
E → E+E |E*E |( E ) | - E | id
In this grammar,
id + - * / ↑( ) are terminals.
E is non-terminal.
E is the start symbol.
Each line is a production – 5 productions.
E → E+E
E →E*E
E→(E)
E→-E
E → id
Derivations:
Two basic requirements for a grammar are :
1. To generate a valid string.
2. To recognize a valid string.
Derivation is a process that generates a valid string with the help of grammar by replacing the
nonterminals on the left with the string on the right side of the production.

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.

REGULAR EXPRESSION CONTEXT-FREE GRAMMAR


It is used to describe the tokens of It consists of a quadruple where S → start
programming languages. symbol, P → production, T → terminal, V

variable or non- terminal.
It is used to check whether the given input is It is used to check whether the given input is
valid or not using transition diagram. valid or not using derivation.
The transition diagram has set of states and The context-free grammar has set of
edges. productions.
It has no start symbol. It has start symbol.
It is useful for describing the structure of lexical It is useful in describing nested structures
constructs such as identifiers, constants, such as balanced parentheses, matching
keywords, and so forth. begin-end and so on.

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.

Example: Given grammar G : E → E+E |E*E |(E ) | - E |id


Sentence to be derived : – (id+id)
E → E+E
E →E*E
E→(E)
E→-E
E → id
 String that appear in leftmost derivation are called left sentinel forms.
 String that appear in rightmost derivation are called right sentinel forms.

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.

Example 1 : Given grammar G : E → E+E | E*E | ( E ) | - E | id


The sentence id+id*id has the following two distinct LMD derivations:

You might also like