The following is a grammar that allows you to omit parentheses in infix algebraic expressions when the precedence rules remove ambiguity. For example,a + b * c meansa + (b * c). However, the grammar requires parentheses when ambiguity would otherwise result. That is, the grammar does not permit left-to-right association when several operators have the same precedence. For example,a/b * c is illegal. Notice diat the definitions introduce factors and terms.
= | + |-
= | * | /
= |( )
= a |b| ... | z
The recognition algorithm is based on a recursive chain of subtasks:find an expression—>find a term —>find a factor. What makes this a recursive chain is thatfind an expression usesfind a term, which in turn usesfind a factor. Find a factor either detects a base case or usesfind an expression,thus forming the recursive chain.
The pseudocode for the recognition algorithm follows:
FIND AN EXPRESSION
// The grammar specifies that an expression is either II a single term or a term followed by a + or a -,
// which then must be followed by a second term.
Find a term
if (the next symbol is a + or a -) {
Find a term }
//end if
FIND A TERM
// The grammar specifies that a term is either a II single factor or a factor followed by a * or a I,
// which must then be followed by a second factor.
Find a factor
if (the next symbol is a * or a I) {
Find a factor
} //end if
FIND A FACTOR
// The grammar specifies that a factor is either a
// single letter (the base case) or an
// expression enclosed in parentheses.
if (the first symbol is a letter){
Done
}
else if (the first symbol is a '('){
Find an expression starting at character after'('('
Check for‘)’
}
else{
No factor exists
}// end if
Design and implement a class of infix expressions, as described by the given grammar. Include a method to recognize a legal infix expression.
the algorithm must be able to backtrack when it hits a dead end, and you must eliminate the possibility that the algoridim will cycle.
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.