Convert C# Program to Pascal

This C# program is designed to convert simple C# programs into Pascal language code. The program reads a C# source code file and translates basic constructs like if, for, while, variable declarations, and method definitions into their equivalent Pascal syntax. The translated code is written to an output file in Pascal format. This is a useful tool for developers who need to port simple C# programs to Pascal or study the differences between the two programming languages.



Group

File Handling in C#

Objective

1. Provide a C# source code file as input.
2. The program parses the C# code and identifies basic constructs like loops, conditionals, and method definitions.
3. It converts the identified constructs into Pascal syntax.
4. The translated Pascal code is saved to a new output file.

Example usage:

convertToPascal inputFile.cs outputFile.pas


Create a program that converts simple C# programs, such as the following one, to the Pascal language.

Example C# Exercise

 Copy C# Code
using System;
using System.IO;

class CSharpToPascalConverter
{
    // Main method where the program starts execution
    static void Main(string[] args)
    {
        // Check if the user provided the input and output filenames as command line arguments
        if (args.Length != 2)
        {
            Console.WriteLine("Usage: convertToPascal  ");
            return;
        }

        // Get the input and output file paths from the command line arguments
        string inputFile = args[0];
        string outputFile = args[1];

        try
        {
            // Open the input C# file for reading
            string csharpCode = File.ReadAllText(inputFile);

            // Convert C# code to Pascal syntax
            string pascalCode = ConvertCSharpToPascal(csharpCode);

            // Write the Pascal code to the output file
            File.WriteAllText(outputFile, pascalCode);

            Console.WriteLine("C# code has been converted to Pascal and saved to " + outputFile);
        }
        catch (Exception ex)
        {
            // If there was an error, display the error message
            Console.WriteLine($"Error: {ex.Message}");
        }
    }

    // Method to convert C# code to Pascal code
    static string ConvertCSharpToPascal(string csharpCode)
    {
        // Convert common C# constructs to Pascal syntax
        csharpCode = csharpCode.Replace("public class", "program");
        csharpCode = csharpCode.Replace("static void Main", "begin");
        csharpCode = csharpCode.Replace("Console.WriteLine", "writeln");
        csharpCode = csharpCode.Replace("Console.ReadLine", "readln");
        csharpCode = csharpCode.Replace("{", "begin");
        csharpCode = csharpCode.Replace("}", "end.");
        csharpCode = csharpCode.Replace("int", "integer");
        csharpCode = csharpCode.Replace("string", "string");

        // Convert for loops
        csharpCode = csharpCode.Replace("for (int i =", "for i :=");
        csharpCode = csharpCode.Replace("i < ", "i <= ");
        csharpCode = csharpCode.Replace("i++)", "do");
        csharpCode = csharpCode.Replace("}", "end");

        // Convert if statements
        csharpCode = csharpCode.Replace("if (", "if ");
        csharpCode = csharpCode.Replace("else", "else");

        // Return the converted Pascal code
        return csharpCode;
    }
}

 Output

//Case 1 - Successful Conversion:

//For the command:
convertToPascal inputFile.cs outputFile.pas

//Output:
C# code has been converted to Pascal and saved to outputFile.pas

//Case 2 - Error (File Not Found):

//For the command:
convertToPascal nonExistentFile.cs outputFile.pas

//Output:
Error: The system cannot find the file specified.

Share this C# Exercise

More C# Practice Exercises of File Handling in C#

Explore our set of C# Practice Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  • Hex Viewer Utility - Dump Utility in C#

    This C# program is a "dump" utility that functions as a hex viewer. It displays the contents of a given file in hexadecimal format, with 16 bytes in each row and 24 rows in each sc...

  • Display Fields in a DBF File in C#

    In this exercise, you will create a program that reads a DBF file and displays a list of fields contained within it. The DBF file format, used by the old dBase database manager, is...

  • Censor Text File Program in C#

    In this exercise, you will create a program that reads a text file, checks each word against a list of words to censor, and writes the modified text to a new file. The words to cen...

  • Extract Data from SQL INSERT Statements in C#

    In this exercise, you will create a C# program that parses SQL INSERT commands from a file and extracts their data in a structured format. The program will read SQL INSERT statemen...

  • PGM Image Parser and Console Display in C#

    This program reads a PGM image file in binary format (P5) and represents its shades of gray in the console using different characters based on the intensity value. The program firs...

  • Display a BMP Image on Console in C#

    This program reads a 72x24 BMP image file and displays it on the console. It uses the information from the BMP header to determine the start of image data and processes the pixels ...