Starting from:
$35

$29

Week – 5 Exercises Solution

1. Consider the below expression

n = n>6+3 || !2+3 || n = !c.n*!3 || 1+3==4

A correct declaration of precedence directives (%left, %nonassoc, %right) in this assignment will help produce these groupings. Notice, for instance, that the DOT operator has the highest precedence, while the (right-associative) ASSIGN operator has the lowest precedence. The (left-associative) PLUS and MINUS operators should have equal precedences. The (non-associative) EQUALITY and GREATER operators should also have equal precedences, though their precedences differ from those of PLUS and MINUS. Although the NOT operator is unary (i.e., only has one operand), you can declare it to be right associative for the sake of ordering its precedence.

    a) Write a CFG for the above expression.

    b) Implement Lex and Yacc program to recognize the above expression using the CFG written in (a). You should transform your grammar, as necessary, to eliminate conflicts. The output should represent whether the expression is valid or invalid for the given CFG.



2. Imagine the syntax of a programming language construct such as while-loop --

while (condition)
begin
statement;

end

where while, begin, end are keywords; condition can be a single comparison expression (such as x == 20, etc.); and statement is the assignment to a location the result of a single arithmetic operation (e.g., a = 10 * b).

    a) Write a CFG for the above construct.

    b) Implement Lex and Yacc program to recognize the above expression using the CFG written in (a). You should transform your grammar, as necessary, to eliminate conflicts. The output should represent whether the testcase is valid or invalid for the given CFG.
Upload the following files in moodle.



    1. CFG and Code for both problems.

    2. 1 Testcase with error and 1 Testcase without error for 2nd problem.

    3. Output



Note: Outputs in the form of screenshots alone will be considered for evaluation.

More products