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
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
ADVERTISEMENT