Exercise
Pascal To C# Translator
Objective
Create a basic Pascal to C# translator. It will accept program such as:
example program;
var
i: integer;
max: integer;
begin
writeLn("How many times?");
readLn(max);
for i := 1 to max do
writeLn("Hola");
end.
The steps you must follow are:
Read from beginning to end a text file, whose name will be entered by the user in command line or in an interactive way: up to 2 points.
Dump the contents to another text file, whose name will be the same, but with ".cs" extension instead of ".pas": up to 4 points.
Replace "WriteLn" with "Console.WriteLine", " = "with "==", " := " with "=", simple quotes with double quotes, "begin" with "{" and "end;", "end.", "end" (in that order) with "}", : up to 6 points.
Replace "program x;" with "class x {" followed with "Main", Replace "readln(x)" with "x=Convert.ToInt32(Console.RadLine())" ("x" must be any other identifier): up to 8 points.
Eliminate "var" and replace "x: integer" with "int x" (but "x" must be any other identifier): up to 9 points. Give a proper format to "for": up to 10 points.
Create a compilable C# source from the previous Pascal source and similar ones: up to 11 points.
Write Your C# Exercise
C# Exercise Example
// Importing necessary namespaces for basic operations
using System; // For basic Console operations
using System.IO; // For file handling
using System.Text.RegularExpressions; // For using regular expressions
public class PascalToCSharpTranslator
{
// Method to translate Pascal code to C# code
public static string TranslatePascalToCSharp(string pascalCode)
{
// Replacing "WriteLn" with "Console.WriteLine"
pascalCode = pascalCode.Replace("WriteLn", "Console.WriteLine");
// Replacing " = " with "=="
pascalCode = pascalCode.Replace(" = ", "==");
// Replacing " := " with "="
pascalCode = pascalCode.Replace(" := ", "=");
// Replacing single quotes with double quotes
pascalCode = pascalCode.Replace("'", "\"");
// Replacing "begin" with "{"
pascalCode = pascalCode.Replace("begin", "{");
// Replacing "end;" and "end." with "}"
pascalCode = pascalCode.Replace("end;", "}");
pascalCode = pascalCode.Replace("end.", "}");
// Replacing "program x;" with "class x {"
pascalCode = Regex.Replace(pascalCode, @"program\s+(\w+);", "class $1 {");
// Replacing "readln(x)" with "x = Convert.ToInt32(Console.ReadLine())"
pascalCode = Regex.Replace(pascalCode, @"readln\((\w+)\)", "$1 = Convert.ToInt32(Console.ReadLine())");
// Replacing "var" and "x: integer" with "int x"
pascalCode = Regex.Replace(pascalCode, @"var", "");
pascalCode = Regex.Replace(pascalCode, @"(\w+):\s*integer", "int $1");
// Formatting "for" loops to C# style
pascalCode = Regex.Replace(pascalCode, @"for\s+(\w+)\s*:=\s*(\d+)\s+to\s*(\d+)\s*do", "for(int $1 = $2; $1 <= $3; $1++)");
return pascalCode;
}
// Method to process the Pascal code file and generate the C# code file
public static void ProcessFile(string inputFileName)
{
// Reading the Pascal code from the input file
string pascalCode = File.ReadAllText(inputFileName);
// Translating Pascal code to C#
string cSharpCode = TranslatePascalToCSharp(pascalCode);
// Creating the output file name by changing the extension from .pas to .cs
string outputFileName = Path.ChangeExtension(inputFileName, ".cs");
// Writing the translated C# code to the output file
File.WriteAllText(outputFileName, cSharpCode);
Console.WriteLine($"Translation complete. The C# code has been saved to {outputFileName}");
}
}
class Program
{
static void Main(string[] args)
{
// Asking user for the Pascal file name
Console.Write("Enter the Pascal file name (with .pas extension): ");
string inputFileName = Console.ReadLine();
// Calling the ProcessFile method to handle the translation
PascalToCSharpTranslator.ProcessFile(inputFileName);
}
}