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

In summary: I have attached my attempt at part 1https://drive.google.com/file/d/0Bx5xz1R0pfjwxZTdjNmN1M0E/view?usp=sharingWe have two textbooks that we can use, they are:1. Introduction to the Theory of Computation, by Michael Sipserand2. Introduction to Automata Theory, Languages, ComputationI 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
  • #1
ChakanaX
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:
Physics news on Phys.org
  • #2
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
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.
 
  • #4
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: 636
  • #6
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?
 
  • #7
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

  • Lecture_4_context_free_gramma.pdf
    202.7 KB · Views: 356

1. What is a compiler program for if then else statements?

A compiler program for if then else statements is a computer program that translates code written in a high-level programming language (such as Java or C++) into machine code that can be understood and executed by a computer. It specifically handles the logical structure of if then else statements, which are used to make decisions in a program based on certain conditions.

2. How does a compiler program for if then else statements work?

The compiler program reads the code written in a high-level language and converts it into an intermediate representation, also known as an abstract syntax tree. It then goes through a series of stages, including lexical analysis, syntax analysis, semantic analysis, and code generation, to produce the final machine code. This process ensures that the code is correct and efficient before it is executed by the computer.

3. What are the benefits of using a compiler program for if then else statements?

The main benefit of using a compiler program for if then else statements is that it allows for code to be written in a high-level language, which is easier for humans to understand, and then translated into machine code, which is necessary for the computer to execute. This allows for faster and more efficient development of complex programs, as well as increased portability across different computer systems.

4. What are some common challenges when writing a compiler program for if then else statements?

Writing a compiler program for if then else statements can be a complex and challenging task. Some common challenges include dealing with different programming languages and their specific syntaxes, handling errors and debugging, and ensuring the generated code is efficient and accurate. Additionally, the process of designing and implementing the various stages of the compiler can also be a challenge.

5. Are there any resources available for learning how to write a compiler program for if then else statements?

Yes, there are many resources available for learning how to write a compiler program for if then else statements. Some popular books on the topic include "Compilers: Principles, Techniques, and Tools" by Aho, Sethi, and Ullman, and "Engineering a Compiler" by Cooper and Torczon. Online resources such as tutorials, articles, and forums can also be helpful in understanding the concepts and techniques involved in writing a compiler program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top