Wednesday, July 30, 2008
TRANSLATION RULES
a) INFIX TO PREFIX- If E is a variable or a constant, then Prefix(E) = E
- If E is E1 op E2, then Prefix(E) = Prefix(E1 op E2) = op Prefix(E1) Prefix(E2)
- If E is (E1), then Prefix(E) = Prefix(E1)
Prefix( ( A * B ) + ( C / D) )
= ( + Prefix(A * B) Prefix(C / D) )
= ( + ( * Prefix (A) Prefix (B) ) ( / Prefix (C) Prefix(D) )
= ( + ( * A B ) ( / C D ) )
b) PREFIX TO INFIX
- If E is a variable or a constant, then Infix(E) = E
- If E is op E1 E2, then Infix(E) = Infix(E1 E2) = Infix(E1) op Infix(E2)
- If E is (E1), then Infix(E) = Infix(E1)
Infix( + ( * A B ) ( / C D ) )
= ( Infix ( * A B ) + Infix ( / C D) )
= ( ( Infix(A) * Infix(B) ) + ( Infix(C) / Infix (D) )
= ( ( A * B ) + ( C / D) )
c) POSTFIX TO INFIX
- If E is a variable or a constant, then Infix(E) = E
- If E is E1 E2 op , then Infix(E) = Infix(E1 E2 op ) = Infix(E1) op Infix(E2)
- If E is (E1), then Infix(E) = Infix(E1)
Infix( ( A B * ) ( C D / ) + )
= ( Infix( A B *) + Infix( C D / ) )
= ( ( Infix(A) * Infix (B) ) + ( Infix(C) / Infix(D) )
= ( ( A * B ) + ( C / D) )
d) POSTFIX TO PREFIX
- If E is a variable or a constant, then Prefix(E) = E
- If E is E1 E2 op , then Prefix(E) = Prefix(E1 E2 op ) = op Prefix(E1 ) Prefix(E2)
- If E is (E1), then Prefix(E) = Prefix(E1)
Prefix( ( A B * ) ( C D / ) + )
= ( + ( Prefix( A B * ) ) ( Prefix( C D / ) )
= ( + ( * Prefix(A ) Prefix(B ) ) ( / Prefix(C ) Prefix(D ) )
= ( + ( * A B ) ( / C D ) )
Tuesday, July 22, 2008
2.1) Consider the context-free grammar S -> SS + | SS * | a
a.) S -> SS*
-> SS + S*
-> aS + S*
-> aa + S*
-> aa + a*
b.) Parse tree of this string![]()
c.) The grammar generates a language that accepts all possible arithmetic
combinations of a but using only + and * and postfix notation
2.2) What language can be generated by the following grammars?
a.) S -> 0 S 1 | 0 1
The grammar generates a language that accepts equal number of zeroes
and ones where the sequence of 0's precedes the 1's
b.) S-> +SS | -SS | a
The grammar generates a language that accepts all possible arithmetic
combination of a using only + and - and prefix notation
c.) S -> S ( S ) S | ε
The grammar generates a language that accepts adjacent, nested and
matched pairs of parentheses.
d.) S -> a S b S | b S a S | ε
The grammar generates a language that accepts an empty string or all
possible combination of strings containing equal number of a's and b's.
e.) S -> a | S + S | S S | S * | ( S )
An ambiguous grammar that generates a language that accepts strings of a.
2.3) 2.2.e is clearly an ambiguous grammar since string S + S * can be parsed in
more than one way as shown below.
2.4) Construct unambiguous context-free grammars for each of the following
language
a.) Arithmetic expression in postfix notation
expr -> expr expr +
expr -> expr expr -
expr -> digit
digit -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
b.) Left-associative lists of identifiers separated by commas
expr -> expr, id
expr -> id
c.) Right associative lists of identifiers separated by commas
expr -> id, expr
expr -> id
d.) Arithmetic expressions of integers and identifiers with the four binary
operators +, - , *, /
expr -> expr + term | expr - term | term
term -> term * factor | term / factor | factor
factor -> digit | identifier | (expr)
e.) Add unary plus and minus to the arithmetic operators of (d)
expr -> expr + term | expr - term | term
term -> term * factor | term / factor | factor
factor -> digit | identifier | (expr) | +factor | -factor
Subscribe to:
Comments (Atom)

