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

  • Thread starter Thread starter ChakanaX
  • Start date Start date
  • Tags Tags
    Compiler Program
AI Thread Summary
The discussion revolves around the assignment to write a compiler program for an if-then-else statement, with a focus on understanding the grammar and automata involved. Participants express confusion about the assignment's requirements, particularly whether to create a specific programming example or a simulation of the automata designed. The complexity of if statements, including optional else blocks and nested conditions, adds to the uncertainty. The lack of clarity from the professor and the timing of the assignment, following a review of context-free grammar, further complicate the situation. Overall, the participants seek guidance on how to approach the assignment effectively.
ChakanaX
Messages
6
Reaction score
0
< Mentor Note -- thread moved to HH from the technical physics forums, so no HH Template is shown >[/color]

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:
Physics news on Phys.org
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?
 
jedishrfu said:
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 homework 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.
 
ChakanaX said:
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 homework 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

  • autom.jpg
    autom.jpg
    15.1 KB · Views: 695
ChakanaX said:
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?
 
berkeman said:
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

Back
Top