Question

Question 1: Write a Yacc program that takes boolean expressions as input and produces the truth...

Question 1: Write a Yacc program that takes boolean expressions as input and produces the truth value of the expressions. The following grammar for boolean expressions:

bexpr → bexpr or bterm | bterm

bterm → bterm and bfactor | bfactor

bfactor→ not bfactor|( bexpr ) | true | false

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Parse tree for the sentence:

<bexpr> <bterm> <bfactor> NOT <bfactors <bexpr> <bexpr> OR <bfactor> <bfactor FALSE <bterm> TRUEGrammar generates all boolean expressions,

bexpr => bterm => bfactor => not bfactor => not (bexpr) => not (bexpr or bterm)

=> not (bterm or bterm) => not (bfactor or bfactor) => not (true or false)

By traversing the tree, the tree generated will lead to bfactor which will generates all boolean expression.

bexpr=> bterm=> bfactor=> true

bexpr=> bterm=> bfactor=>false

bexpr=> bterm=> bfactor=> not bfactor=> not true

bexpr=> bterm=> bfactor=> not bfactor=> not false

bexpr=> bexpr or bterm=> bterm or bterm=> bfactor or bfactor=> true or false

bexpr=> bexpr and bterm=> bterm and bterm=> bfactor and bfactor=> true and false

bexpr => bterm => bfactor => not bfactor => not (bexpr) => not (bexpr or bterm)

=> not (bterm or bterm) => not (bfactor or bfactor) => not (true or false)

Yacc program that takes boolean expressions as input and produces the truth value of the expressions:

test.l %{ #include "y.tab.h" %} AND [Aa][Nn][Dd] OR [Oo][Rr] NOT [Nn][Oo][Tt] op '&' | '|' | "!" %% [a-zA-Z] {return ALPHA;} [\t]+ ; [\n] {return '\n';} {AND} { return (AND); } {OR} { return (OR); } {NOT} { return (NOT); } [Tt][Rr][Uu][Ee] { yylval = 1; return (boolean); } [Ff][Aa][Ll][Ss][Ee] { yylval = 0; return (boolean); } . {();} %%
test.y %{ #include<stdio.h> #include<stdlib.h> int yylex(); %} %token ALPHA AND OR NOT TRUE FALSE boolean %left "&" "|" %right '!' %% program: bexpr '\n' {if ($1 >= 1) { printf("TRUE\n"); exit(0); } else{ printf("FALSE\n"); exit(0); } | ; bexpr: bexpr "|""|" bterm { $$ = $1 || $3; } | bterm { $$ = $1; } ; bterm: bterm "&""&" bfactor { $$ = $1 && $3; } | bfactor { $$ = $1; } ; bfactor: '!' bfactor { $$ = ! $2; } | '(' bexpr ')' { $$ = $2; } | TRUE { $$ = $1; } | FALSE {$$ = $1; } | boolean { $$ = $1; } ; %% int main() { printf("Enter your truth statement\n"); yyparse(); return 0; }
Add a comment
Know the answer?
Add Answer to:
Question 1: Write a Yacc program that takes boolean expressions as input and produces the truth...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • R327 Complete the following truth table by finding the truth values of the Boolean expressions for...

    R327 Complete the following truth table by finding the truth values of the Boolean expressions for all combinations of the Boolean inputs p, q, and . · false false false false false true false true false 5 more combinations

  • A truth table can be implemented with different boolean expressions and different circuits True False Question...

    A truth table can be implemented with different boolean expressions and different circuits True False Question 5 (2 points) The boolean expression "not x or y" is equivalent to the boolean expression "x or not Y" True False Question 6 (2 points) The boolean expression (x or y) and x is equivalent to the boolean expression (x and y) ory True False

  • C++ Write a program that takes an infix expression as an input and produces a postfix...

    C++ Write a program that takes an infix expression as an input and produces a postfix expression. Use stack to convert an infix expression into postfix expression. Include a function that evaluates a postfix expression.

  • QUESTION 2 Boolean or "truth-valued" expressions are how we express conditions that control choices and repetition...

    QUESTION 2 Boolean or "truth-valued" expressions are how we express conditions that control choices and repetition in computer languages. Consider the following Python Boolean expression, where variables alpha, beta, and gamma are of type Boolean: alpha and (beta or gamma) In any algebraic notation there are usually several different ways of writing the same expression. For instance, in integer arithmetic the value of expression '4 x (5 + 2)' is numerically equivalent to that of expression '(4 x 5) +...

  • containsSubSequence takes two Strings as input and returns a boolean: Returns true if the first input...

    containsSubSequence takes two Strings as input and returns a boolean: Returns true if the first input string contains all the characters of the second input string, in order, but not necessarily consecutively. > HW2.containsSubSequence("abracadabra", "abcd") true > HW2.containsSubSequence("abracadabra", "abdc") false you must not use either break or continue in your code. You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method I write something...

  • Problem 1: Complete this truth Table. Write a program that you can enter from the keyboard,a...

    Problem 1: Complete this truth Table. Write a program that you can enter from the keyboard,a 1 or 0 into three Boolean variables, A,B,C. Write an if statement that tests the condition ( (A or B ) and C ). Run the program 8 separate times, testing each of the following 8 combinations. Write the results below in the following truth table: Problem 1-Complete this truth Table. Write a program that you can enter from the keyboard, a 1 or...

  • Use Boolean Algebra to simplify the following Boolean expressions to three (3) literals. Please write down...

    Use Boolean Algebra to simplify the following Boolean expressions to three (3) literals. Please write down the intermediate steps. 1). F11(x,y,z) = x'yz+xyz +x'y'Z+xy'Z+ xy'z 2). F12(x,y,z) = (y'+xyz')' Question 2 [2 points) Obtain the function expression of F2 from the logic diagram. Question 3 [3 points) Obtain the truth table of the following function and rewrite the function in Canonical POS (Product of Maxterms) format: F3(a,b,c) = (a'+c)(a+b+c') +a'bc' Question 4 (2 points) Convert the following function to Canonical...

  • Write a C++ program that reads string expressions from an input file "prefix.txt" containing prefix expressions...

    Write a C++ program that reads string expressions from an input file "prefix.txt" containing prefix expressions (one expression per input line). For each prefix expression read from the input file, the program should: (1) convert the expression to a fully parenthesized infix expression and (2) find the value of the expression. Use a stack to solve the problem. Assume the expressions contain only integer numbers and the *,/, +, - operators. Generate the results in an output file "results.txt" in...

  • Given a boolean variable a, are the following expressions equivalent? !( !a ) a 1) No...

    Given a boolean variable a, are the following expressions equivalent? !( !a ) a 1) No answer text provided. 2) true 3) false 4) No answer text provided.

  • Write a java program that has a method called sameArrayBackwards. The method takes an array of...

    Write a java program that has a method called sameArrayBackwards. The method takes an array of integers, and checks if the numbers in the array are the same going forward as going backwards and will return a boolean (true or false) depending on the result. You can assume that there is at least one element in the array. Method Header: public static boolean sameArrayBackwards (int [] arr) You will test your method in the main method of your program by...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT