There are many tools to
help define and execute new programming languages.
(None used here.)
Hopefully the following gives you a feel for the
programming language creation process.
Example of My Program Language
# my first program
set, x, 12
set, a, 4
set, b, 5
set, c, a + b
loop abc
print, " c is ", c , " x is ", x
if c > x: exit
set, c, c + 1
goto, abc
Language Definition
Basic rules
- each statement is on a single line
- each statement starts with a reserved word
- reserved words are case insensitive
- statements are executed one at a time in order
- comments start with a '#' character and are ignored
- variable names are single letters (a-z)
- variable names are case insensitive
- variables must be set to a value before they can be used
- all numeric values are integers
- strings are surrounded by double quotes
- the first error stops the interpreter
- "loop" marks a unique execution location
- "goto" jumps to a "loop" execution location
- spaces and/or a comma separates elements in a statement
- an "if" statement consists of an "if" expression and an action
- a colons ":" separates an "if" expression from an action
- end-of-line (EOL) or end-of-file (EOF) terminates a statement
Reserved Words
- exit
- goto
- if
- loop
- print
- set
Math Operators
Comparisons Operators (True or False)
Click HERE
for a formal (Backus Naur Form) description of my language.
Create a Lexical Analyzer?
- process the complete program before executing the program
- separate statements into tokens
- create a list of statements
- for each statement, create a list of tokens
The results of the example shown above might be:
program = [
["set", "x", 12],
["set", "a", 10],
["set", "b", 5],
["set", "c", "a","+","b"],
["loop", "abc"],
["print", '" c is ", c," x is ",x']
["if", "c", "<", "x", ":", "exit"]
["set", "c", "c", "+", "1"]
["goto", abc]
]
To eliminates testing for both upper and lower case
convert each statement to uppercase for processing?
Replace commas with spaces and split a statement (on spaces)
before processing it into tokens?
For if statements, split on colon (":") then process each
part separately?
Create an Execution "Engine"?
Process each statement one at a time and perform the action.
Create a dictionary to hold variable names and their values?
Create a dictionary to hold goto locations
in the program's list of statement?
FYI
The string startswith() method returns True if a string
starts with a specific value, otherwise it returns False.
Common methods used to remove characters from a string.
- the string replace() method
- the string translate() method
- string slices
- regular expressions
Tools
Flex (Fast Lexical Analyzer Generator)
Flex (lexical analyser generator)
(Wikipedia)
GNU Bison
GNU Bison
(Wikipedia)
Compiler Construction using Flex and Bison
Win flex-bison