Operator: Precedence Parsing | Compiler Design - Computer Science Engineering (CSE) PDF Download

3. OPERATOR : PRECEDENCE PARSING: 
Precedence/ Operator grammar: The grammars having the property:
1. No production right side is should contain ∈
2. No production sight side should contain two adjacent non-terminals. 

Is called an operator grammar.

Operator – precedence parsing has three disjoint precedence relations, <.,=and .> between certain pairs of terminals. These precedence relations guide the selection of handles and have the following
meanings:

Operator: Precedence Parsing | Compiler Design - Computer Science Engineering (CSE)
 

Operator precedence parsing has a number of disadvantages: 

1. It is hard to handle tokens like the minus sign, which has two different precedences.
2. Only a small class of grammars can be parsed.
3. The relationship between a grammar for the language being parsed and the operatorprecedence parser itself is tenuous, one cannot always be sure the parser accepts exactly the desired language.

Disadvantages:
 1. L(G) ¹L(parser)
 2. error detection
 3. usage is limited
 4. They are easy to analyse manually 


Example:
Grammar:  E→EAE|(E)|-E/id
A →+|-|*|/|­
Input string:   id+id*id
The operator – precedence relations are:

Operator: Precedence Parsing | Compiler Design - Computer Science Engineering (CSE)

Solution: 
This is not operator grammar, so first reduce it to operator grammar form,
by eliminating adjacent non-terminals.

Operator grammar is: E→E+E|E-E|E*E|E/E|E­E|(E)|-E|  id

The input string with precedence relations interested is:

$<.id.> + <.id.> * <.id.> $

Scan the string the from left end until first .> is encounted.

$<.id.>+<.id.>*<.id.<$
 

This occurs between the first id and +.
 

Scan backwards (to the left) over any =’s until a <. Is encounted. We scan backwards to $.

$<.id.>+<.id.>*<.id.>$ ­ ­

Everything to the left of the first .> and to the right of <. Is called handle. Here, the handle is the first
id.

Then reduce id to E. At this point we have: E+id*id

By repeating the process and proceding in the same way: $+<.id.>*<.id.>$

substitute E→id,

After reducing the other id to E by the same process, we obtain the right-sentential form
 

E+E*E

Now, the 1/p string afte detecting the non-terminals sis:

⇒ $+*$
 
Inserting the precedence relations, we get: $<.+<.*.>$
­  ­
The left end of the handle lies between + and * and the right end between * and $. It indicates that, in the right sentential form E+E*E, the handle is E*E.

Reducing by E→E*E, we get:

E+E

Now the input string is: $<.+$
Again inserting the precedence relations, we get:
⇒$<.+.>$
­ ­
reducing by E→E+E, we get,
$ $
and finally we are left with:
E
Hence accepted.

Operator: Precedence Parsing | Compiler Design - Computer Science Engineering (CSE)

 

The document Operator: Precedence Parsing | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Operator: Precedence Parsing - Compiler Design - Computer Science Engineering (CSE)

1. What is precedence parsing in computer science engineering?
Ans. Precedence parsing is a technique used in computer science engineering to analyze and evaluate the operators in a mathematical expression based on their priority or precedence. It helps in determining the order of execution of operators and operands in an expression.
2. How does precedence parsing work?
Ans. Precedence parsing works by assigning precedence levels to different operators. It scans the expression from left to right and compares the precedence levels of operators. If an operator with higher precedence is found, it is evaluated first. This process continues until the entire expression is parsed and evaluated.
3. What is the purpose of precedence parsing in computer science engineering?
Ans. The purpose of precedence parsing in computer science engineering is to ensure that mathematical expressions are evaluated correctly according to the rules of operator precedence. It helps in avoiding ambiguity and determining the correct order of operations in an expression.
4. Are there any limitations to precedence parsing?
Ans. Yes, there are some limitations to precedence parsing. It cannot handle expressions that involve parentheses or brackets. It also requires the operators to be defined with distinct precedence levels, which may not always be the case in complex expressions. Additionally, it may not be suitable for parsing languages with complex grammatical rules.
5. What are some applications of precedence parsing in computer science engineering?
Ans. Precedence parsing finds applications in various areas of computer science engineering. It is commonly used in compilers to parse and evaluate mathematical expressions. It is also employed in the design and implementation of programming languages, where correct evaluation of expressions is crucial. Additionally, it is utilized in mathematical software and scientific calculators to ensure accurate computation.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

past year papers

,

Semester Notes

,

Sample Paper

,

Extra Questions

,

pdf

,

MCQs

,

mock tests for examination

,

Previous Year Questions with Solutions

,

Objective type Questions

,

Operator: Precedence Parsing | Compiler Design - Computer Science Engineering (CSE)

,

Free

,

Exam

,

Important questions

,

ppt

,

shortcuts and tricks

,

Operator: Precedence Parsing | Compiler Design - Computer Science Engineering (CSE)

,

study material

,

video lectures

,

Summary

,

Viva Questions

,

Operator: Precedence Parsing | Compiler Design - Computer Science Engineering (CSE)

,

practice quizzes

;