LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-04-2016, 10:24 PM   #1
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,150

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Smile Interpreter - Compilers


Has anyone tried doing a simple interpreter?

Links or examples is greatly appreciated.

Thanks.
 
Old 09-05-2016, 08:07 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
http://fundamental-code.com/interp/
https://ruslanspivak.com/lsbasi-part1/
or just goooooogle
 
1 members found this post helpful.
Old 09-05-2016, 03:53 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
The Structure and Interpretation of Computer Programs is more or less all about getting you to the point where you can write a LISP interpreter. Here's one of the many online copies:

http://sarabander.github.io/sicp/
 
1 members found this post helpful.
Old 09-05-2016, 04:19 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Your question covers a lot of territory!

It might be helpful if you can elaborate a little on your current knowledge/expertise level and what your goals actually are.

The input side of interpreters and compilers are essentially the same - tokenizer (aka lexer, scanner) and parser (aka syntax checker) designed for your particular grammar, which together produce an intermediate representation (commonly some form of AST - abstract syntax tree) of the input language stream. The output side, interpreter, compiler or other processing all work from that intermediate representation, ideally.

So, how much of the total picture are you interested in?

The best printed resource I know of for the total picture is Modern Compiler Design. The first 5 chapters of that book, together with the same author's Parsing Techniques: A Practical Guide, will cover just about anything you have in mind, in depth, but very readable and usable! There is a free download of the first edition of Parsing Techniques on the linked page.

If you are really just starting out, I recommend Levine's Flex & Bison from O'Reilly. You may find some poor reviews on this book, I do not fully understand why. I suspect that the subject matter is just inherently complex and unfamiliar and many people are looking for instant point-and-click style of learning. But the book provides a very useful and readable introduction to the subject and the tools, plus excellent examples with downloadable source code... it will be time well spent to work your way through it - more than once as your understanding grows. You will need some supplemental reading for BNF notation of context-free grammars if not already familiar, plenty online but one of the best written and most useful I have recently seen here.

So, try to clearly identify your current level and the scope of your project - and get down to business!

Last edited by astrogeek; 09-05-2016 at 04:36 PM.
 
1 members found this post helpful.
Old 09-05-2016, 11:51 PM   #5
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,150

Original Poster
Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Hello guys, thanks for all your replies. Links are really helpful.

@Astrogeek, yes the question covers a lot of topic and i'm just starting.

I'm actually trying to learn it phase by phase.

I'm trying to find examples in phases of:

- Tokens
- Lexers
- Parsers



Just need to grasp the basic before lurking into the whole thing.

Thanks.
 
Old 09-06-2016, 01:38 AM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
If you are new to the subject then Flex is a good place to start!

The Flex manual is mostly complete and easy enough to read. (As I recall, it seems to just dive right in on first pass and makes the head spin, but becomes clear very quickly if you stick with it).

Download the source and extract the examples directory - it has some good ones! (You don't have to build the package to use the examples, just use whatever Flex comes with your distro).

Once you get oriented to what Flex actually does it is extremely easy to quickly generate test cases and experiment - I'll be happy to suggest a few once you have begun working through the manual.

Flex is a lexer generator - that is, it generates compilable C code for lexers (tokenizers) from your specification file. So you will need to know or learn some basic C and how to use gcc, the GNU compiler.

And regular expressions - you will need to become moderately proficient with them if not already as they are the primary input to a Flex specification.

Once you know what Flex does, you can move on quickly to integrating it with Bison.

Good luck and keep us posted!

Last edited by astrogeek; 09-06-2016 at 01:46 AM. Reason: typos
 
Old 09-06-2016, 05:06 AM   #7
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,150

Original Poster
Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Hi Astrogeek, I've done some few Java, is there an equivalent of it in Java?

In C, I will definitely need to brush up again from start. It's been decades since my last C programming.

Thank you.
 
Old 09-06-2016, 05:25 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
http://www.javaworld.com/article/207...he-basics.html
http://www.javaworld.com/article/207...structure.html
...
 
Old 09-06-2016, 07:30 AM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Yes, I've written one ... and, made money from it. (I still do.)

Today, probably the easiest thing to do is to look at an existing compiler or interpreter, e.g. on GitHub. Study exactly what they did. Whack-a-mole what they did to turn it into what you want.
 
Old 09-07-2016, 09:59 PM   #10
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,150

Original Poster
Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Cool

Quote:
Originally Posted by sundialsvcs View Post
Yes, I've written one ... and, made money from it. (I still do.)

Today, probably the easiest thing to do is to look at an existing compiler or interpreter, e.g. on GitHub. Study exactly what they did. Whack-a-mole what they did to turn it into what you want.
Hi Sundialsvcs, I'm trying to do it via Java and utilize javacc. I'm still trying to check the process or steps.

Like if I have the BNF, what steps should I do next. Is there a link somewhere that gives an overview some sort of a block diagram or flowchart?

The process for making an interpreter and a compiler is different?

Thanks.
 
Old 09-08-2016, 02:49 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
probably you missed my previous post, there were two links about "how to build an interpreter in java".
An interpreter need not process the complete file, just one line at a time, the compiler will always process everything and will create an executable.
The compilers have much better optimizers, they uses different passes to generate byte code, optimize, generate binary, linking, whatever.
The interpreters usually much simpler, but they may handle variables differently.
 
1 members found this post helpful.
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ Compilers Completely Clueless Programming 22 08-14-2009 08:12 PM
FREE Compilers and Cross-Compilers for LINUX and WINDOWS. Bernstein34 Linux - Software 1 05-05-2008 12:54 PM
c++ compilers metallica1973 Linux - Software 16 04-11-2006 11:23 AM
C++ Compilers (!~CoW~!) Linux - Software 12 11-18-2003 09:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration