28 Apr C++ Library And Build The Parser
At Programming Homework Tutors, we believe in providing our students with practical, real-world examples of how to apply the concepts they learn in class. That’s why we’ve developed a variety of sample projects to help you see how our courses can be used to create impactful solutions in your field of study.
Description
For this assignment, you will be given a program, convert it to an intermediate representation that can be easily run by other code.
Basics
You will be given several files. It is recommended that you add these files to your solution for assignment 2 as a starting point. You are allowed to use, modify, or ignore any of the files from your solution for assignment 2, and you may create any other files that are required to implement the program.
Grammar Description
The grammar (given in extended Backus-Naur form) for the grammar description is as follows:
program → variables body
variables → idList SEMICOLON
idList → ID { COMMA ID }
body → LeftBRACE statementList RightBRACE
statementList → statement { statement }
statement → assignStatement | ifStatement | printStatement | switchStatement | whileStatement
assignStatement → ID EQUAL primary SEMICOLON | ID EQUAL expression SEMICOLON
ifStatement → IF conditional body
printStatement → PRINT ID SEMICOLON
switchStatement → SWITCH ID LeftBRACE caseList [ defaultCase ] RightBRACE
whileStatement → WHILE conditional body
expression → primary op primary
conditional → primary relationalOperator primary
primary → ID | NUM
op → PLUS | MINUS | MULT | DIV
relationalOperator → GREATER | LESS | NotEQUAL
caseList → case { case }
case → CASE NUM COLON body
defaultCase → DEFAULT COLON body
The tokens in the above grammar are defined as follows:
COLON = :
SEMICOLON = ;
COMMA = ,
LeftBRACE = {
RightBRACE = }
WHILE = WHILE
EQUAL = =
PLUS = +
MINUS = –
MULT = *
DIV = /
GREATER = >
LESS = <
NotEQUAL = <>
ID = letter(letter | digit)*
NUM = 0 | (digit digit*)
IF = IF
PRINT = print
SWITCH = SWITCH
CASE = CASE
DEFAULT = DEFAULT
Things to note about this grammar:
- Expressions are greatly simplified and are not recursive.
- There is no explicit typing of variables. All variables are INT by default.
- All variables are declared at the beginning of the program.
- All arithmetic is integer arithmetic.
- The if statement has no else.
- The print statement has been added and the print keyword is in lower case.
- Any terminals are written in capital letters and are defined the same as the previous projects (except for the print keyword).
Tokenizer
Because the list of keywords in this language is different, you will need to modify the tokenizer that was provided in assignment 2 (or you created for assignment 2) to handle those changes. You may assume that the rules on generating tokens remains the same.
Running the Program
The additional code that was provided will take the list of statements and run them. You may assume that this works. If necessary, I will provide updates through the additional code. This additional code can be used in your unit testing. These files are identical to the files that are used in Gradescope.
Execution Semantics
All statements are to be executed in the order that they appear in the program except for if, switch, and while statements. Note that if, switch, and while statements may be nested.
If Statement
An if statement has the following semantics:
- The conditional is evaluated.
- If the conditional evaluates to true, the body of the if statement is executed.
- Execution then proceeds with the statement after the if statement.
Print Statement
A print statement “print a;” prints the value of the variable a at the time of execution.
Switch Statement
A switch statement has the following semantics:
- The value of the switch statement is checked against each case number in order of appearance.
- The body of the first case number that matches the value is executed.
- If none of the cases match, then the body of the default case is executed if it exists.
- Execution then proceeds with the statement after the switch statement.
A switch statement in the code:
SWITCH expression {
CASE value1 : { statementList1 }
…
CASE valuen : { statementListn }
DEFAULT : { defaultStatementList }
}
nextSectionOfCode
is equivalent to:
IF expression == value1 {
statementList1
GOTO label
}
…
IF expression == valuen {
statementListn
GOTO label
}
defaultStatementList
label:
nextSectionOfCode
While Statement
A while statement has the following semantics:
- The conditional is evaluated.
- If the conditional evaluates to true, the body of the while statement is executed. Control passes back to step 1.
- If the conditional evaluates to false, execution then proceeds with the statement after the while statement.
A while loop in the code:
WHILE condition {
statementList
}
is equivalent to:
label:
IF condition {
statementList
GOTO label
}
Generating Code
Your code will need to take the input file and generate a linked list that contains all of the instructions and is easy to execute. This section describes the types of statements and the information needed for the list you generate to run.
The various types with “Type” in the name in the subsections below are all enumerations that can be found in the file types.h.
Handling Constants and Variables
Constants and variables are handled through the ValueNode class. The class has one member variable:
int value; // Value of the constant or variable
When converting the code, take all constants and store it in value, then pass the pointer to whatever statement needs it. When converting a variable, the value will also be stored in a ValueNode object.
Handling Simple Assignments
Simple assignments are stored in an AssignmentStatement class. The class has five member variables:
ValueNode *leftHandSide;
ValueNode *operand1;
ValueNode *operand2; // Ignored if operator is NO_OPERATOR
OperatorType operator; // Set to NO_OPERATOR if there is no operator
StatementNode *nextStatement; // Handles the next statement
The variables operator and operand2 are optional. If they are provided, the value calculated by the expression operand1 operator operand2 will be assigned to leftHandSide. If they are not provided, the value operand1 will be assigned to leftHandSide instead.
Note that variables are initialized to zero when declared.
Handling Goto Statements
While there is no goto statement in this programming language, goto statements are required by the switch and while statements for the intermediate representation for the code to work.
Goto statements are stored in a GotoStatement class. The class has one member variable:
StatementNode *nextStatement; // Next statement to be run
Handling If and While Statements
If statements are stored in an IfStatement class. The class has five member variables:
ConditionType conditionOperation;
ValueNode *operand1;
ValueNode *operand2;
StatementNode *trueBranch; // Next statement if condition is true
StatementNode *falseBranch; // Next statement if condition is false
The two operands are compared using the condition operation. If the comparison is true, control transfers to the statement indicated by trueBranch. If the comparison is false, control transfers to the statement indicated by falseBranch.
While statements can be built using a combination of IfStatements and GotoStatements.
Handling No Operation Statements
While there is no noop statement in this programming language, noop statements are useful in situations where there just needs to be a statement to pass control to.
Noop statements are stored in a NoOpStatement class. The class has one member variable:
StatementNode *nextStatement; // Next statement to be run
Handling Print Statements
Print statements are stored in a PrintStatement class. The class has one member variable:
ValueNode *ID; // ID to be printed out
This statement will print out the value of ID->GetValue().
Handling Switch Statements
Switch statements can be built using a combination of IfStatements and GotoStatements in a similar way as while.
Executing the Intermediate Representation
After the list of statements has been built, it needs to be executed. Execution begins with the first statement in the list. The pseudocode for execution is as follows:
currentStatement = first statement of program
while (currentStatement != NULL)
If currentStatement is an assignment, make the assignment, then go to the next statement.
If currentStatement is a goto, go to the next statement indicated by the goto.
If currentStatement is an if, determine if the condition is true or not. Go to the next statement based on the result.
If currentStatement is a no operation (do nothing), then go to the next statement.
If currentStatement is a print, print the value, then go to the next statement.
The code that actually runs the program is the function ExecuteProgram in compiler.cpp.
Implementation
You will need to implement the function parseGenerateIntermediateRepresentation and return a linked list containing the statements that need to be run.
Input/Output
The input is provided by a file, and your program will need to return a pointer to the StatementNode that corresponds to the first line of the program. You do not need to provide anything else, as your program will be tested by running the program and comparing your output with the expected output.
Disclaimer
The sample projects provided on our website are intended to be used as a guide and reference for educational purposes only. While we have made every effort to ensure that the projects are accurate and up-to-date, we do not guarantee their accuracy or completeness. The projects should be used at your own discretion, and we are not responsible for any loss or damage that may result from their use.
At Programming Homework Tutors, we are dedicated to helping students and educators achieve their goals by providing them with the resources they need to succeed. Our website offers a variety of tools and resources that can help you with the project mentioned above.
Whether you need help with research, project management, or technical support, our team of experts is here to assist you every step of the way. We offer online courses, tutorials, and community forums where you can connect with other learners and get the support you need to succeed.
If you’re looking to take your skills to the next level and make an impact in your field, we invite you to explore our website and see how we can help you achieve your goals.
Latest Topic
-
Cloud-Native Technologies: Best Practices
20 April, 2024 -
Generative AI with Llama 3: Shaping the Future
15 April, 2024 -
Mastering Llama 3: The Ultimate Guide
10 April, 2024
Category
- Assignment Help
- Homework Help
- Programming
- Trending Topics
- C Programming Assignment Help
- Art, Interactive, And Robotics
- Networked Operating Systems Programming
- Knowledge Representation & Reasoning Assignment Help
- Digital Systems Assignment Help
- Computer Design Assignment Help
- Artificial Life And Digital Evolution
- Coding and Fundamentals: Working With Collections
- UML Online Assignment Help
- Prolog Online Assignment Help
- Natural Language Processing Assignment Help
- Julia Assignment Help
- Golang Assignment Help
- Design Implementation Of Network Protocols
- Computer Architecture Assignment Help
- Object-Oriented Languages And Environments
- Coding Early Object and Algorithms: Java Coding Fundamentals
- Deep Learning In Healthcare Assignment Help
- Geometric Deep Learning Assignment Help
- Models Of Computation Assignment Help
- Systems Performance And Concurrent Computing
- Advanced Security Assignment Help
- Typescript Assignment Help
- Computational Media Assignment Help
- Design And Analysis Of Algorithms
- Geometric Modelling Assignment Help
- JavaScript Assignment Help
- MySQL Online Assignment Help
- Programming Practicum Assignment Help
- Public Policy, Legal, And Ethical Issues In Computing, Privacy, And Security
- Computer Vision
- Advanced Complexity Theory Assignment Help
- Big Data Mining Assignment Help
- Parallel Computing And Distributed Computing
- Law And Computer Science Assignment Help
- Engineering Distributed Objects For Cloud Computing
- Building Secure Computer Systems Assignment Help
- Ada Assignment Help
- R Programming Assignment Help
- Oracle Online Assignment Help
- Languages And Automata Assignment Help
- Haskell Assignment Help
- Economics And Computation Assignment Help
- ActionScript Assignment Help
- Audio Programming Assignment Help
- Bash Assignment Help
- Computer Graphics Assignment Help
- Groovy Assignment Help
- Kotlin Assignment Help
- Object Oriented Languages And Environments
- COBOL ASSIGNMENT HELP
- Bayesian Statistical Probabilistic Programming
- Computer Network Assignment Help
- Django Assignment Help
- Lambda Calculus Assignment Help
- Operating System Assignment Help
- Computational Learning Theory
- Delphi Assignment Help
- Concurrent Algorithms And Data Structures Assignment Help
- Machine Learning Assignment Help
- Human Computer Interface Assignment Help
- Foundations Of Data Networking Assignment Help
- Continuous Mathematics Assignment Help
- Compiler Assignment Help
- Computational Biology Assignment Help
- PostgreSQL Online Assignment Help
- Lua Assignment Help
- Human Computer Interaction Assignment Help
- Ethics And Responsible Innovation Assignment Help
- Communication And Ethical Issues In Computing
- Computer Science
- Combinatorial Optimisation Assignment Help
- Ethical Computing In Practice
- HTML Homework Assignment Help
- Linear Algebra Assignment Help
- Perl Assignment Help
- Artificial Intelligence Assignment Help
- Uncategorized
- Ethics And Professionalism Assignment Help
- Human Augmentics Assignment Help
- Linux Assignment Help
- PHP Assignment Help
- Assembly Language Assignment Help
- Dart Assignment Help
- Complete Python Bootcamp From Zero To Hero In Python Corrected Version
- Swift Assignment Help
- Computational Complexity Assignment Help
- Probability And Computing Assignment Help
- MATLAB Programming For Engineers
- Introduction To Statistical Learning
- Database Systems Implementation Assignment Help
- Computational Game Theory Assignment Help
- Database Assignment Help
- Probabilistic Model Checking Assignment Help
- Mathematics For Computer Science And Philosophy
- Introduction To Formal Proof Assignment Help
- Creative Coding Assignment Help
- Foundations Of Self-Programming Agents Assignment Help
- Machine Organization Assignment Help
- Software Design Assignment Help
- Data Communication And Networking Assignment Help
- Computational Biology
- Data Structure Assignment Help
- Foundations Of Software Engineering Assignment Help
- Mathematical Foundations Of Computing
- Principles Of Programming Languages Assignment Help
- Software Engineering Capstone Assignment Help
- Algorithms and Data Structures Assignment Help
No Comments