expr -> expr op expr | ( expr ) | number op -> + | - | * | /To understand how various parsers work, we must discuss various derivations as follows:
Defn: A CFG consists of:
P#1: Consider the above grammar for expressions. Give the leftmost derivation of the string (id+id). Notationally, this would read, show expr =>*lm (id+id)
Note1: If S =>*lmα, then we say that α is the left-sentential form of grammar G.
Note2: We can do the same derivations with similar notation for the rightmost derivation and the right-sentential form of the grammar G.
So where are we? Well for certain types of parsers, ambiguity is not a good thing and must be eliminated. For others, we can use some disambiguating rules to eliminate unwanted parse trees. Our discussion leads us to discussing these two topics in detail.
P#2: Consider the following grammar:
expr -> expr op expr | term op -> + | - | * term -> numbera) Is this grammar ambiguous? If so, why? If not, why not?
b) Rewrite the above grammar so that multiplication has precedence over addition and subtraction and in doing so, is not ambiguous.
Consider the following grammar:
stmt -> ifstmt | other ifstmt -> if ( expr ) stmt | if ( expr ) stmt else stmt expr -> T | FQ#1: What can you say about this grammar?
Many times left recursion is unwanted in CFGs. Two kinds of left recursion can exist:
Immediate Left Recursion:
E -> E + T | T T -> T * F | F F -> (E) | idNonimmediate Left Recursion:
S -> Aa | b A -> Ac | Sd | eLet's begin by describing the elimination of immediate left recursion.
Productions of the form: A -> Aα | β need to be replaced by productions of the form:
A -> βA' A' -> αA' | eP#3: Eliminate the immediate left recursion from the above example.
P#4: Eliminate the immediate left recursion from the following example.
E -> E + T | T | Q T -> T * F | F F -> ( E ) | id Q -> numberWe need to discuss Algorithm 4.1 on p. 177 in conjunction with the following problem:
P#5: (a) What is L(S)? (b) Eliminate all left recursion in the grammar:
S -> Ba | b B -> Sa | a