Writing a compiler, need litle guidence

by desertofwater Banned
9 replies
Hello guys, glad to be here, nice forum

I want to write a simple language compiler for learning purposes. Here is the grammar :

S -> expr | int | float
expr -> <int> + <int> | <float> + <float>

Now that I know the grammar for my language can someone guide me what next? I know there a lot of books there like flex and yacc, compilers books, but i think i will learn a lot more if i break the ice with something simple first, these books confuse, so please help this guy

By the way I am experience programmer, but in other areas, i am familiar with grammar, finite state machines, assembly language, as high level, and more so i can understand if you guide me a litle.

Thank you
#compiler #guidence #litle #writing
  • Profile picture of the author Mr. Enthusiastic
    Edited:
    I'm leaving in the old version, otherwise the interaction between Adrian and me makes no sense. Since we got a great compiler post from CDurlock I see that there's interest in this thread, so please ignore my delete request.

    Previous version
    :
    Can a moderator delete this thread please? I imagine that it's very unlikely any marketing project will ever be stalled, just because someone needs some tips about compiling their finite state machines. (It is a valid question in computer science but seems pretty deep for the marketing world.)
    {{ DiscussionBoard.errors[1455971].message }}
    • Profile picture of the author Bewley
      Originally Posted by Mr. Enthusiastic View Post

      Can a moderator delete this thread please? I imagine that it's very unlikely any marketing project will ever be stalled, just because someone needs some tips about compiling their finite state machines. (It is a valid question in computer science but seems pretty deep for the marketing world.)
      Oh I say! How cheeky of the OP to ask a programming question in a programming forum :confused:

      Where does it say (in the forum description) that it has to be IM related? How do you know it is NOT IM related?

      Adrian
      {{ DiscussionBoard.errors[1459150].message }}
  • Profile picture of the author HomeBizNizz
    I recommend a Google search: programming forum
    {{ DiscussionBoard.errors[1458504].message }}
  • Profile picture of the author CDarklock
    Originally Posted by desertofwater View Post

    I want to write a simple language compiler for learning purposes.
    This is a very deep subject. The canonical reference is Aho and Ullman's Principles of Compiler Design, which I believe has been retitled since the original printing and added some co-authors, but look up "Alfred V. Aho" on Amazon and you should find it. There's a dragon on the cover; they change the colour of the dragon at each reprint - first one (1977) was green, second (1986?) was red, I don't know what colours later editions are.

    I'm personally a much greater fan of Allen Holub's Compiler Design in C, but the dragon book is the big one if you're working on this sort of thing.

    Oh, and incidentally: you're doing the right thing. Building a compiler USED to be a minimal requirement for a degree in CS, but no longer... and honestly, I often want to tear my hair out at the way recent CS grads have no damn clue what the machine is doing. Get a good understanding of how your program goes from high-level to low-level to machine code, and how a linker works. It will make you a MUCH better developer.
    Signature
    "The Golden Town is the Golden Town no longer. They have sold their pillars for brass and their temples for money, they have made coins out of their golden doors. It is become a dark town full of trouble, there is no ease in its streets, beauty has left it and the old songs are gone." - Lord Dunsany, The Messengers
    {{ DiscussionBoard.errors[1458839].message }}
  • Profile picture of the author wayfarer
    I think this thread probably got moved here from somewhere else. Oh well, no point replying, as the OP appears to be banned now for whatever reason.
    Signature
    I build web things, server things. I help build the startup Veenome. | Remote Programming Jobs
    {{ DiscussionBoard.errors[1459506].message }}
  • Profile picture of the author Mr. Enthusiastic
    Adrian, at the time I posted my note, nobody had responded. If CDarklock's post had showed up I would not have said anything about deletion. It's obvious his post could be the start of a meaty discussion!

    The code and web forums are in the heading "Warrior Support." This suggests to me that the intention might have been to help people doing Internet Marketing Warrior stuff, through discussion of programming as it relates to the IM Warrior business. Maybe I'm wrong, but both as a programmer and a marketer, it's my job to try to make reasonable inferences.

    I suspect that the original poster got banned because he said he was a student looking for help with his programming homework. He jumped in with a batch of rather technical questions.

    That leads me to suspect that the moderators preferred to keep the forum focused. It would be more for programming questions which have a pretty immediate application to internet marketing, rather than general interest programming.

    I don't want to second-guess the moderators. I do want to respect that there are lots of other great programming forums on the Internet but nothing else that has code discussion within a great IM forum.

    I've never written a compiler myself. I do have enough understanding of both programming and business to know that compiler writing is highly unlikely to be an element of Internet marketing as it's currently practiced.

    I'll edit my previous post since this could turn into a good discussion of compilers. I can't imagine what IM project might possibly involve writing a compiler but I agree that understanding compilers makes programmers better.

    If this post itself is inappropriate, I'll delete both it and my earlier one on request.
    {{ DiscussionBoard.errors[1459683].message }}
    • Profile picture of the author highrider21
      I'm just curious what language do you write a compiler in? Do you use assembly? But then what language do you write an assembly compiler in?
      Signature

      {{ DiscussionBoard.errors[1486261].message }}
      • Profile picture of the author xiaophil
        I actually just finished writing one. And yes, it's directly related to internet marketing. The problem is that most people don't understand what they are - even CS grads or software developers, as evidenced from some of the comments made so far.

        I'm guessing from CDarklocks reference to earlier editions of 'The Dragon Book' that he has at some point been through the process of writing a compiler from scratch. A most admirable feat indeed. I really wonder what they teach on CS courses nowadays when many of the recent grads I speak with have no idea what recursion or pointers are.

        Compilers are indeed a deep and fascinating topic, and without getting into detail I would like to try to broaden the awareness of their scope and application.

        A compiler is a language recognizer and translator. It takes a stream of input symbols, matches them to rules about how those symbols can be arranged, and then generates a stream of output symbols.

        A common case, (and as far as some people are concerned, the only one) is for the input symbols to be the source code of a computer programming language and the output symbols to be the object code for a specific microprocessor.

        In reality, a compiler can be applied to any task requiring a text-to-text transformation. The only requirement is that all of the possible combinations of the sequence of input symbols (a language) can be defined as a set of rules (a grammar).

        I've written compilers for such diverse applications as translating CAD files, batch loading databases, and interpreting 'little languages' for specific domains. Recently, I wrote one for an article marketing application. I think there are plenty of applications for text-based recognition and transformation (compilers) in internet marketing.

        Originally Posted by highrider21 View Post

        I'm just curious what language do you write a compiler in?
        Any language you like. I don't code them from scratch any more, as there are heaps of great tools now that can speed up development tremendously.

        Breaking down a translation problem into multiple steps makes it easier to approach and understand. Typically, the very front end (a scanner or lexer) would convert a stream of symbols (e.g. letters) into tokens (e.g. words). Next, a parser would match those tokens against rules of a grammar and create a syntax tree. Next, tree-walkers can parse the tree as many times as necessary, annotating or rearranging it. The final tree-walker usually generates the required output.

        I started using a system many years ago, called PCCTS (the Purdue Compiler Compiler Tool Set) which has now evolved into ANTLR (Another Tool for Language Recognition). Development is active and ongoing. While writing a compiler from scratch is always an option, and a great learning experience, nowadays I would wholeheartedly steer anyone interested in doing such a thing in a reasonable time-frame toward the ANTLR tool suite. It really is great, and there are heaps of examples and documentation to help you hit the ground running.

        ANTLR is written in Java, can be defined in terms of itself (always a good sign for a compiler compiler) and happily generates compilers in most any language you care to mention, including Java, C, C++, C#, Objective C, Python, Ruby, Perl, PHP, Delphi and JavaScript!

        So there you go, as well as being a fascinating topic, compilers are useful for all kinds of things, and can be directly applied to internet marketing. Perhaps most important of all though - by using tools like ANTLR, compilers can be FUN!
        {{ DiscussionBoard.errors[1486697].message }}
  • Profile picture of the author Mr. Enthusiastic
    Phil, thanks for your post. I'd enjoy chatting with you about programming theory over a beer some time. Maybe next time we're on the same continent?

    I agree transformational grammar is useful for any programmer to understand. After all, any program optionally reads input, optionally processes, and optionally generates output, and a useful program typically does all three. Parsing parameters or metadata, to configure processing options, could be seen as an application of transformational grammar.

    ANTLR looks interesting and it's gotta have the coolest hand sign of any software tool yet. I'm mainly a .Net guy these days, so I might play around with their C# version.

    Thinking of compilers in the narrow sense: parsing language tokens in order to generate machine-language opcodes, so that a programming language can be executed. I continue to think that the ability to make this type of compiler is unlikely to be in the IM toolkit.

    We see questions here from people who are just learning to fart around with php, who don't understand object-oriented code, modular development, design for testability, how to write specs, etc. Probably 80% of the people posting here don't really understand software fundamentals such as how pointers work or how to normalize a database, or hardware fundamentals such as how caches feed execution pipelines on today's CPU's.

    That doesn't mean that they can't really rock at using software in their marketing businesses, just that they won't be as efficient with it as someone who has a deeper understanding.

    Given that there are already good compilers for several excellent languages with great libraries (php, C++, C#, vb.net, etc.), I don't see much need for a "marketing warrior" to make a narrowly defined compiler as I first thought of in the comp-sci definition.

    I do see lots of opportunities to streamline marketing work with the kind of ad-hoc parsing tool you brought into the discussion with a broader look at compilation. And I thank you for bringing in that broader definition.

    Have you explored XSLT at all?
    {{ DiscussionBoard.errors[1491728].message }}

Trending Topics