Experiment No: 7: Name: Abhishek Subhash Tekalkar Roll - No: 124 Div: B
Experiment No: 7: Name: Abhishek Subhash Tekalkar Roll - No: 124 Div: B
Theory:
A grammar that is used to define mathematical operators is called an operator grammar or
operator precedence grammar. Such grammars have the restriction that no production has either
an empty right-hand side (null productions) or two adjacent non-terminals in its right-hand side.
Examples –
This is an example of operator grammar:
E->E+E/E*E/id
However, the grammar given below is not an operator grammar because two non-terminals are
adjacent to each other:
S->SAS/a
A->bSb/b
We can convert it into an operator grammar, though:
S->SbSbS/SbS/a
A->bSb/b
Operator precedence parser –
An operator precedence parser is a bottom-up parser that interprets an operator grammar. This
parser is only used for operator grammars. Ambiguous grammars are not allowed in any parser
except operator precedence parser.
There are two methods for determining what precedence relations should hold between a pair of
terminals:
There is not given any relation between id and id as id will not be compared and two variables
can not come side by side. There is also a disadvantage of this table – if we have n operators then
size of table will be n*n and complexity will be 0(n2). In order to decrease the size of table, we
use operator function table.
Operator precedence parsers usually do not store the precedence table with the relations; rather
they are implemented in a special way. Operator precedence parsers use precedence functions
that map terminal symbols to integers, and the precedence relations between the symbols are
implemented by numerical comparison. The parsing table can be encoded by two precedence
functions f and g that map terminal symbols to integers. We select f and g such that:
#include <stdio.h>
main()
{ int a =
20; int b =
10; int c =
15; int d =
5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e ); e
= ((a + b) * c) / d; // (30 * 15 ) / 5 printf("Value
of ((a + b) * c) / d is : %d\n" , e ); e = (a + b) *
(c / d); // (30) * (15/5) printf("Value of (a + b)
* (c / d) is : %d\n", e ); e = a + (b * c) / d; // 20
+ (150/5) printf("Value of a + (b * c) / d is :
%d\n" , e );
return 0;
}
Output:
Conclusion:-
Hence we understood how operator precedence parser and successfully implemented it