Making a Programming Language From Scratch/Line by Line Input System

Processing the File
To make a compiler obviously you have to take input from a file.Thus you need to be familiar with File I/O in at least one language you are comfortable in. Now how do you get the statements? Each line in a programming language is terminated by a very specific character which is NOT a newline. Most generally it is a semi-colon or an opening brace({). So to get a complete line we only need to input up till the terminating character. Note that this approach means that you cannot have the terminating characters within even a string or a character, but that they have to be represented by their ASCII code.

Algorithm For Input
1.Input Character 2.Check if Character is terminating character 2.1.If true, add character and terminate process 2.2.Else add character and continue 2.3.If line contains EOF(end of file) character, terminate program after processing line. [Usually done by the means of flags] 3.Repeat from step 1

This algorithm will take input from the file and then store it in the character array assigned to it.

Processing Keyword
Now, once you have the complete logical line, what are you going to do with it? Obviously you are going to process it. But how? There are two methods to it.

1. Lexing and Parsing the line 2.Getting keyword and taking appropriate action thereafter

In method 1 we take the line, run it through what is known as a 'Lexer' which breaks it down into tokens a 'Parser' which analyses it. Based on this it makes a wise decision and sends it to be processed accordingly.However this approach needs a very sophisticated system. This book seeks to skip the processes of Parsing and Lexing and go straight into processing. Thus this book uses the Pseudo-Parser and Pseudo-Lexer approach (Method 2) which Lexes and Parses the statement differently for each type of statement, which allows for simpler compilers and lot of grammatical freedom.

The algorithm for this approach is rather simple: 1.Take character from character array storing logical line till we encounter a 'delimiter' like ',",[space] and { 2.Compare the keyword thus derived with the set of all defined keywords. 2.1.If one matches, send the line to the matching function to process it.  2.2.If none match, produce an error message

After this process is over, check for flags set by the line-input system, if the flag is set, terminate program.