Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 22nd, 2010, 05:08 PM
MTK358 MTK358 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Location: ~
Posts: 12 MTK358 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 24 m 50 sec
Reputation Power: 0
Simple interpreter with Lex/YACC

How would I make an interpreter for a simple language using Lex/YACC?

I probably can't just put the code to interpret the language in the grammar rules, because how will it handle ifs and loops?

Reply With Quote
  #2  
Old November 23rd, 2010, 08:17 PM
OmegaZero OmegaZero is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2007
Posts: 737 OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 m 19 sec
Reputation Power: 928
Normally you have a parser (possibly constructed with lex/yacc) which reduces the source code to a data structure (usually some sort of tree). You can then execute the source code directly from the structure or convert it into some other form.

For example an if statement might turn into a structure like
Code:
If
   Condition
   Then-Block
   Else-Block

Which is evaluated by a routine like
Code:
Evaluate(If)
   if Evaluate(Condition) == True
      Evaluate( Then-Block )
   else
      Evaluate( Else-Block )



You might want to start off with a language with a simpler structure (like a Lisp/Scheme variant) to get the principle down without having to worry about too many variations in syntax.
__________________
sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);

Reply With Quote
  #3  
Old November 24th, 2010, 08:01 AM
MTK358 MTK358 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Location: ~
Posts: 12 MTK358 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 24 m 50 sec
Reputation Power: 0
That makes sense, just one thing that confuses me is the exact structure of the tree: How to store the name/type of the node, how to manage child nodes/literals, etc.

Also, I was considering an alternative to Lex/YACC, mostly this:

EDIT: since I can't post URLs, I wrote this Perl one-liner that will print the URL:

Code:
perl -e '$_ = "jvvrQWWrkwoctvcEeqoWuqhvyctgWrgiWrgiE1Ejvon"; tr|QWE[c-z][a-b]|:/.[a-z]|; print;'

Reply With Quote
  #4  
Old November 24th, 2010, 03:24 PM
MTK358 MTK358 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Location: ~
Posts: 12 MTK358 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 24 m 50 sec
Reputation Power: 0
Lightbulb

I came up with a nice solution for the AST, but it requires an OO language:

Have a class Node that contains an eval() function and a list of children. The eval() function would return the value that the node evaluates to.

For example, an IntegerLiteralNode would store its value and its eval() would return that. Then, an AdditionNode can, for example, have two IntegerLiteralNodes as children, and its eval() would call eval() on the two chirdren and return the sum.

I just wonder how would I handle multiple data types? But I'll probably play around with a language containing only integers for now to test my AST concept.

Reply With Quote
  #5  
Old November 25th, 2010, 12:32 PM
OmegaZero OmegaZero is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2007
Posts: 737 OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level)OmegaZero User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 m 19 sec
Reputation Power: 928
You can write OO in C. You just don't get fancy keywords to make the compiler do all the work for you.

Code:
enum TermType { INT, FLOAT };

union TermData {
    int i;
    double d;
};

struct Term {
    TermType type;
    TermData data;
};

double eval( struct Term *a ) {
    if( a->type == INT ) {
        return a->data.i;
    }
    // . . .
}


- or if you prefer function pointers -

Code:
union TermData {
    int i;
    double d;
};

struct Term {
    double (*eval)(struct Term*);
    TermData data
};

void eval_int( struct Term *this ) {
    return this->data.i;
}

struct Term zero { eval_int, 0 };

Reply With Quote
  #6  
Old November 25th, 2010, 02:16 PM
MTK358 MTK358 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Location: ~
Posts: 12 MTK358 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 24 m 50 sec
Reputation Power: 0
What is a term and why does eval return void?

Anyway, this is more like what I was thinking (in C++, although it might be converted to C):

Code:
class Node
{
    Node* children;

public:
    virtual int eval();
    
    void addChild(Node* node);
    int childCount();
    Node* child(int index);
};

class AdditionNode
{
public:
    AdditionNode(Node* addend1, Node* addend2)
    {
        addChild(addend1);
        addChild(addend2);
    }

    virtual int eval()
    {
        return child(0)->eval() + child(1)->eval();
    }
};

class IntegerNode
{
    int value;

public:
    IntegerNode(int val)
    {
        value = val;
    }

    virtual int eval()
    {
        return value;
    }
};

Node* myNode = new AdditionNode(new IntegerNode(3), new IntegerNode(5));
printf("3 + 5 = %d\n", myNode.eval());

Reply With Quote
  #7  
Old November 25th, 2010, 03:16 PM
MTK358 MTK358 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Location: ~
Posts: 12 MTK358 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 24 m 50 sec
Reputation Power: 0
I made an example that compiles:

Code:
#include <cstdio>

class Node
{
public:
	virtual int eval() {}
};

class AdditionNode : public Node
{
	Node* a;
	Node* b;

public:
	AdditionNode(Node* _a, Node* _b) 
	{
		a = _a;
		b = _b;
	}

	virtual int eval()
	{
		return a->eval() + b->eval();
	}
};

class IntegerNode : public Node
{
	int value;

public:
	IntegerNode(int _value)
	{
		value = _value;
	}

	virtual int eval()
	{
		return value;
	}
};

int main()
{
	Node* n = new AdditionNode(new IntegerNode(3),
	                           new AdditionNode(new IntegerNode(1),
				                    new IntegerNode(4)));
	printf("3 + (1 + 4) = %d\n", n->eval());
}


Shouldn't be too hard for YACC, leg, or something like that to build a tree with these.
Comments on this post
Nylex agrees: Have some rep .

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Simple interpreter with Lex/YACC

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap