Question 1: Given the productions.
S-> Sa | aaA | AbS
A-> acA
a) List the parse table. Is the grammar LL(1) in this form? If no, why not?
b) If not, rewrite the grammar until it is LL(1), proven. If you cannot accomplish this, why? Either way, show the appropriate sets leading to your decision.
Question 2: Given the LL(1) grammar
S-> aS | B
B-> bBa | Cb
C-> c |d
Assume scanner ( ) and error ( ) functions return the next token and abort processing, respectively. Write a complete LL(1) recursive-descent parser in C- like pseudo code without rewriting the grammar.
Question 3: In your grammar there is no function call. Suppose we want to change function calls so that they evaluate to some returned data, and this data could be used the same way as variables are used in expressions (not left of assignment). For example, you could write x = 2 + F1(5) * 3 which, assuming that F1(5) returns 10, should put 32 into x.
Show changes needed in syntax and explain semantics of function call.
Question 4: Write a program would read two numbers and then print all numbers between the first and the second, inclusive. For example, on input 2 and 5 the program would print 2 3 4 5 (output one per line from the virtual machine).
Question 5: Given the production:
S-> aSAc | Acb
A-> bbb| empty
Implement a complete pseudo code for a recursive descent parser. Assume scanner ( ) function returns the next token and error ( ) aborts processing with an error message. Do not forget the main program. This grammar is LL(1) so no don’t modify.
Question 6: Given:
S -> SabC | abC | aCa
C -> ccC | c | empty | D
D -> dd
Rewrite the grammar as LL(1) if possible or otherwise argue why it is not possible. Prove that it is indeed LL(1), after the modifications, showing only the sets that are needed and using them for your proof.
Question 7: Suppose you have a language where a valid program is a sequence of assignments, with each ending with a semicolon. An assignment has syntax and semantics as in our language. Expressions can use variables and integers. Variables are not defined just used. There are two predefined variables READ and WRITE. READ evaluates to the standard input value, WRITE doesn’t evaluate to anything but it prints to the output the value being assigned to it. Expressions are as follow: binary -,+,*,/,^, and unary!. Expression can be parenthesized, which overrides any precedence.
Precedence is set as: weakest are + and -, then *, then /, then ^, then the unary. Associativity is right to left for + and -, and left to right for all others. Write the unambiguous CFG grammar.
Example statements
x:=READ+5;
WRITE:=x;
x=y+x/(x*5);
Question 8: Suppose you have a language where a valid program is a sequence of statements and nothing else. Every statement ends with semicolon. A statement is either input READ(variable) or assignment
variable = expression, where expression is C-like expression involving ( ) and +,-,*,/ all arithmetical binary operators except - which is both unary and binary, and no other operators. Associativity is set so that all operators are left to right except * which is right to left, Precedence is set so that ( ) overrides anything, the rest, from the strongest to the weakest are:
unary minus
+ and -, the same
* and /, the same.
There are no numbers nor anything else. Variables are not defined.
Example program:
READ(x);
READ(y);
x=y+x/(x*y);
Design unambiguous CFG. Make sure to state what the tokens are.
Question 9: Our project grammar uses input statement which reads input into a variable before it can be used. For example, to read a value and multiply by 10 we must do the following:
READ,x;
y = = x*10;
Suppose that we also want to allow the following:
y = = READ*10;
to do exactly the same semantics except the value is not stored in x. This change should apply to other cases as well, for example we want to be able to say:
IF (READ > 0) THEN ... #meaning if the input is greater than 0
y= =10+READ*READ ... # multiply two inputs, add 10, put result in y
Again, the original syntax/semantics should be preserved and this should be an additional way to accomplish something. Show the grammar to allow that. Prove that it is LL(1).
Question 10: Write a valid C-program that would read an input and then compute and output its factorial. Are there limitations of this program? Be very specific.