• Support PF! Buy your school textbooks, materials and every day products Here!

How to write a compiler program for if then else statement?

  • Thread starter ChakanaX
  • Start date
  • #1
6
0
< Mentor Note -- thread moved to HH from the technical physics forums, so no HH Template is shown >

I was given the following question to answer for my Automata class:
function =

if(condition) then

statement

else

statement

the gramma for this if-then-else is

S0 -> iCtSA (if <condition> then <statement> A)

A -> ;eS | ε (;else<statement>|ε)

1. please design an automata to simulate the compiler.

2. how to write a compiler program for if-then-else statement



What exactly does it mean when it says "write a compiler program for the if-then-else statement?" I designed an automata/state machine simulating the basic if then else format for part 1, but is it asking for an arbitrary if then else programming example of my own choosing( like any kind of if statement example) or a code simulation of the automata I created for part 1? Forgive me if my question seems very basic, I'm very new to Automata theory
 
Last edited by a moderator:

Answers and Replies

  • #2
11,519
5,066
Have they asked you to write compilers for expressions or other statements?

The IF statement condition is a specific form of expression that evaluates to a boolean value. Expressions may have a simple form or may be more complex with expressions within expressions. Similarly for IF statements, either the THEN or the ELSE statement block may contain more IF statements. As another complication the ELSE block may be optional.

I'm not sure how far your teacher wants you to go with this.

Can you tell us what you've learned and what you've tried to implement this?

Does the teacher really want you to describe the grammar of an IF statement?
 
  • #3
6
0
Have they asked you to write compilers for expressions or other statements?

The IF statement condition is a specific form of expression that evaluates to a boolean value. Expressions may have a simple form or may be more complex with expressions within expressions. Similarly for IF statements, either the THEN or the ELSE statement block may contain more IF statements. As another complication the ELSE block may be optional.

I'm not sure how far your teacher wants you to go with this.

Can you tell us what you've learned and what you've tried to implement this?

Does the teacher really want you to describe the grammar of an IF statement?
The class has been really unclear and nobody really knows what she wants in this assignment, but this was assigned right after we went over context free grammar, we reviewed the basics of Formalism, Derivations, Backup-Naur FORM, and Left-most and right-most derivations. And i assume that's as far as she wanted to go for this particular hw assignment. But it's still confusing since I have this due before i have the chance to go to the TA and ask what we are suppose to do. The professor is really hard to understand.
 
  • #4
6
0
The class has been really unclear and nobody really knows what she wants in this assignment, but this was assigned right after we went over context free grammar, we reviewed the basics of Formalism, Derivations, Backup-Naur FORM, and Left-most and right-most derivations. And i assume that's as far as she wanted to go for this particular hw assignment. But it's still confusing since I have this due before i have the chance to go to the TA and ask what we are suppose to do. The professor is really hard to understand. I have attached my attempt at part 1
 

Attachments

  • #6
berkeman
Mentor
56,913
6,880
The class has been really unclear and nobody really knows what she wants in this assignment
What textbook are you using for the class? Is the class just about compilers, or is it a more general computer science class?
 
  • #7
6
0
What textbook are you using for the class? Is the class just about compilers, or is it a more general computer science class?
We have two textbooks that we can use, they are:

1. Introduction to the Theory of Computation, by Michael Sipser
and
2. Introduction to Automata Theory, Languages, Computation

I have also attached the POWER POINTS slides that we used for this assignment, but from what jedishrfu stated previously, i'm left to assume she wants a bnf....only i'm confused about the "design a compiler program" part of the assignment so it's unclear....why couldn't she just say design a bnf more directly?
 

Attachments

Related Threads on How to write a compiler program for if then else statement?

Replies
3
Views
1K
Replies
18
Views
8K
  • Last Post
Replies
9
Views
2K
Replies
22
Views
2K
  • Last Post
Replies
2
Views
2K
Replies
3
Views
2K
  • Last Post
Replies
3
Views
2K
Replies
0
Views
2K
  • Last Post
Replies
5
Views
4K
Top