word

advertisement
Test #2 1995
7. Complete the following “Parsing is recognition together with the implicit or explicit
construction of the syntax tree”
8. Let the grammar G be defined as follows:
terminals
= {1, 2, 3, 4, +, *, -, (, )}
non-terminals = {digit, op, expr}
distinguished non-terminal = expr
productions = {expr
::= digit
| expr op expr
| - expr
| (expr)
digit
::= 1 | 2 | 3 | 4
op
::= + | * }
Show how the expression " 4 + (3 * -2) " can be derived in the grammar G. Make
sure to list all direct derivations.
expr => expr op expr
=> digit op expr
=> 4 op expr
=> 4 + expr
=> 4 + (expr)
=> 4 + (expr op expr)
=> 4 + (digit op expr)
=> 4 + (3 op expr)
=> 4 + (3 * expr)
=> 4 + (3 * -expr)
=> 4 + (3 * - digit)
=> 4 + (3 * - 2)
9. Give a parse tree for the expression " 4 + (3 * -2) " in the grammar G defined above.
Test #2 1996
7. Let the grammar G be defined as follows:
terminals = {0, 1, *}
distinguished non-terminal = code
code ::= 0
| *
| 0 code code
| 1 code
Show how the expression 010* can be derived in the grammar G. Make sure to list all
direct derivations.
code => 0 code code
=> 0 1 code code
=> 0 1 0 code
=> 0 1 0 *
8. Give a parse tree for the expression 0100 in the grammar G defined above
9. Show that the grammar G is ambiguous by finding a code that has two (or more)
syntax trees. Draw the two trees.
Test #2 1997
7. Let the grammar G be defined as follows:
terminals = {@, [, ], *}
non-terminals = {letter, list}
list ::= [ ]
| [ list ]
| [letter]
** I think this production rule should be added
| [ letter ] list
letter ::= @ | *
Show how the expression [*][[@]] can be derived in the grammar G. Make sure to
Write down all direct derivations.
list => [letter] list
=> [*] [list]
=> [*] [[letter]]
=> [*] [[@]]
8. Draw a syntax tree for the expression [@] [*] [[@]] in the grammar G defined above.
9. Write down a grammar G9 for a language L that includes the following expressions.
Note that the lists can be of different length and that each element in a list consists of a
digit from the set {0,1,2,3,4,5,6,7,8,9} and a character from the set {‘a’, ‘b’, ‘c’, ‘d’}
L = {[b2, 3a, d0], [ ], [b3, 5b, 6d, 7c], [2c], etc/
G9
terminals
= {0,1,2,3,4,5,6,7,8,9,’a’,’b’,’c’,’d’}
non-terminals
= {list, terms, letter, digit}
distinguished non-terminal = list
productions
={
list ::= [ ]
| [ terms ]
terms ::= term
| term , terms
term ::= letter digit
| digit letter
}
Download