13 Replies - 1258 Views - Last Post: 09 April 2009 - 09:04 PM Rate Topic: -----

#1 rasty_ytsar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 30-March 09

How to make interpreters

Posted 30 March 2009 - 08:30 AM

Sample Program:
* 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

Is This A Good Question/Topic? 1

Replies To: How to make interpreters

#2 cipher_nemo  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 35
  • Joined: 17-July 08

Re: How to make interpreters

Posted 30 March 2009 - 10:28 AM

1.) This isn't C#
2.) Please use the CODE tags around code.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: How to make interpreters

Posted 30 March 2009 - 11:04 AM

so let me guess, you have this assignment for school and you want us to do it for you?
Was This Post Helpful? 0
  • +
  • -

#4 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: How to make interpreters

Posted 30 March 2009 - 12:49 PM

Usually you ask a question in a new topic...
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: How to make interpreters

Posted 30 March 2009 - 12:59 PM

View Postprogramble, on 30 Mar, 2009 - 02:49 PM, said:

Usually you ask a question in a new topic...


I would assume this is his question...

'please give your ideas on how to attach this... specially converting finite automata to codes "
Was This Post Helpful? 0
  • +
  • -

#6 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: How to make interpreters

Posted 30 March 2009 - 01:06 PM

View Posteclipsed4utoo, on 30 Mar, 2009 - 01:59 PM, said:

View Postprogramble, on 30 Mar, 2009 - 02:49 PM, said:

Usually you ask a question in a new topic...


I would assume this is his question...

'please give your ideas on how to attach this... specially converting finite automata to codes "


Right... But what does it mean?
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: How to make interpreters

Posted 30 March 2009 - 01:30 PM

View Postprogramble, on 30 Mar, 2009 - 03:06 PM, said:

View Posteclipsed4utoo, on 30 Mar, 2009 - 01:59 PM, said:

View Postprogramble, on 30 Mar, 2009 - 02:49 PM, said:

Usually you ask a question in a new topic...


I would assume this is his question...

'please give your ideas on how to attach this... specially converting finite automata to codes "


Right... But what does it mean?


No idea.
Was This Post Helpful? 0
  • +
  • -

#8 rasty_ytsar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 30-March 09

Re: How to make interpreters

Posted 30 March 2009 - 05:44 PM

What does it mean?

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.
Was This Post Helpful? 0
  • +
  • -

#9 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: How to make interpreters

Posted 31 March 2009 - 02:30 PM

Parse the code using regex...
Was This Post Helpful? 0
  • +
  • -

#10 fremgenc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 119
  • Joined: 15-November 07

Re: How to make interpreters

Posted 01 April 2009 - 08:06 PM

first, read in the file
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.
Was This Post Helpful? 0
  • +
  • -

#11 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: How to make interpreters

Posted 02 April 2009 - 05:35 AM

View Postfremgenc, on 1 Apr, 2009 - 08:06 PM, said:

first, read in the file
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.
Was This Post Helpful? 0
  • +
  • -

#12 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: How to make interpreters

Posted 04 April 2009 - 12:37 PM

View PostAmrykid, on 2 Apr, 2009 - 06:35 AM, said:

View Postfremgenc, on 1 Apr, 2009 - 08:06 PM, said:

first, read in the file
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.
Was This Post Helpful? 0
  • +
  • -

#13 egof  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 132
  • Joined: 27-March 09

Re: How to make interpreters

Posted 04 April 2009 - 12:43 PM

Sounds like your trying to build a lexigraphic analyzer. Have you looked up lexx and Yacc?
Was This Post Helpful? 0
  • +
  • -

#14 rasty_ytsar  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 30-March 09

Re: How to make interpreters

Posted 09 April 2009 - 09:04 PM

Thanks to all.

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1