How to make interpreters
Page 1 of 113 Replies - 985 Views - Last Post: 09 April 2009 - 09:04 PM
#1
How to make interpreters
Posted 30 March 2009 - 08:30 AM
* my first program in CFPL
VAR abc, b, c AS INT
VAR x, w_23=w AS CHAR
VAR t=TRUE AS BOOL
START
abc=b=10
w_23=a
* this is a comment
OUTPUT: abc & hi & b & # & w_23 & [#]
STOP
Output of the sample program:
10hi10
a#
Language Grammar
Program Structure:
- every line contains a single statement
- all variable declaration is found on top of the program
- a line that starts with asterisk(*) is considered as a comment and comment can be found in any part of the program
- executable code should be found inside the START and STOP block
- all reserved words are in capital letters
- sharp sign(#) signifies next line or carriage return
- ampersand(&) serves as a concatenator
- the square braces([]) are as escape code
Data Types:
1. INT an ordinary number with no decimal part. It uses 32 bits. It can be positive or negative.
2. CHAR a single symbol. It uses UNICODE.
3. BOOL represents the literals true or false.
4. FLOAT a number with decimal part. It uses 64 bits.
Operators:
Arithmetic operators
( ) - parenthesis
*, /, % - multiplication, division, modulo
+, - - addition, subtraction
>, < - greater than, lesser than
>=, <= - greater than or equal to, lesser than or equal to
==, <> - equal, not equal
Logical operators (<BOOL expression><LogicalOperator><BOOL expression>)
AND - needs the two BOOL expression to be true to result to true, else false
OR - if one of the BOOL expressions evaluates to true, returns true, else false
NOT - the reverse value of the BOOL value
Unary operator
+ - positive
- - negative
Sample Programs
1. A program with arithmetic operation
VAR xyz, abc=100 AS INT
START
xyz= ((abc *5)/10 + 10) * -1
* xyz should have the value -60
OUTPUT: [[] & xyz & []]
STOP
Output of the sample program:
[-60]
2. A program with logical operation
VAR a=100, b=200, c=300 AS INT
VAR d=FALSE AS BOOL
START
d = (a < b AND c <>200)
OUTPUT: d
STOP
Output of the sample program:
TRUE
------------------------------------------------------------
please give your ideas on how to attach this... specially converting finite automata to codes
Replies To: How to make interpreters
#2
Re: How to make interpreters
Posted 30 March 2009 - 10:28 AM
2.) Please use the CODE tags around code.
#3
Re: How to make interpreters
Posted 30 March 2009 - 11:04 AM
#4
Re: How to make interpreters
Posted 30 March 2009 - 12:49 PM
#5
Re: How to make interpreters
Posted 30 March 2009 - 12:59 PM
#6
#7
#8
Re: How to make interpreters
Posted 30 March 2009 - 05:44 PM
I have to create an interpreter using C#, the codes that i gave are sample codes that will be interpreted, we will making a UI that accepts that sample codes, prompts an error if there is and prints/outputs the result if the syntax of the codes based from the given specification is correct. Lastly, it will generate a symbol table.
I want to know how to attack this problem.
I am not asking for the actual answer/codes, i want to know how to make the answers.
e.g. how to create a DFA to a code
a simple example will do.
- THANKS FOR THE TIME.
#9
Re: How to make interpreters
Posted 31 March 2009 - 02:30 PM
#10
Re: How to make interpreters
Posted 01 April 2009 - 08:06 PM
then start line by line:
VAR abc, b, c AS INT
would translate to C# as
int abc, b, c;
I was thinking of writing a Java -> C# converter. The hardest part of top layer interpreters, especially between languages, is the library references.
#11
Re: How to make interpreters
Posted 02 April 2009 - 05:35 AM
fremgenc, on 1 Apr, 2009 - 08:06 PM, said:
then start line by line:
VAR abc, b, c AS INT
would translate to C# as
int abc, b, c;
I was thinking of writing a Java -> C# converter. The hardest part of top layer interpreters, especially between languages, is the library references.
you could use a streamreader that reads the file line by line and parse each line using string.replace/string.contains, etc.
#12
Re: How to make interpreters
Posted 04 April 2009 - 12:37 PM
Amrykid, on 2 Apr, 2009 - 06:35 AM, said:
fremgenc, on 1 Apr, 2009 - 08:06 PM, said:
then start line by line:
VAR abc, b, c AS INT
would translate to C# as
int abc, b, c;
I was thinking of writing a Java -> C# converter. The hardest part of top layer interpreters, especially between languages, is the library references.
you could use a streamreader that reads the file line by line and parse each line using string.replace/string.contains, etc.
Or Regex.
#13
Re: How to make interpreters
Posted 04 April 2009 - 12:43 PM
#14
Re: How to make interpreters
Posted 09 April 2009 - 09:04 PM
I have figured it out and passed the project.
I have to read the file, and read it line by line and read it word per word using some delimeters and using REGEX from the C#, I was able to parse it and able to identify the tokens as identifiers, keyword, etc. Using the DFA, i was able to have the state transitions to check the validity of the line and execute the line if validated correct by converting it to a C# code.
|
|

New Topic/Question
Reply



MultiQuote







|