0

I am developing a new programming language for a project work. The new language compilers to html, I am currently working on the compiler. I know compilers have three (3) stages. #1 the lexical stage where tokens are generated form the grammar of the language, the next stage is the parser and then the output. My question is how do i start writing the grammar. Do i do it in an essay form or just short statement. Point me to resources that can help me. I have read a lot and all don't teach you how to write the grammar so if you can give an example. I am writing the compiler in python and using the lax/yacc modules called PLY

an example is:

HTML5 main{
      myHead.id = "cat";
      myTitle;
}

HEAD myHead;
TITLE myTitle = "text";

output of the above code in html is:

<DOCTYPE>

<HTML>
   <head id="cat">
       <TITLE>text</TITLE>
   </head>

   <body>
   </body>
</HTML>
  • 1
    Welcome to Stack Overflow! This question, while interesting, is a bit too broad to be answered here. We can answer specific questions with definite answers. Since your question doesn't have one unique answer, I don't think we can answer it. If you're curious about how to write grammars, you might want to look at the ANTLR or bison websites, which describe how to use those particular tools. – templatetypedef Jan 14 '13 at 17:58
  • 3
    My impression is that you want to write a compiler, but you don't have the background to do it. The classic response is "Get the Aho/Ullman/Sethi book on Compilers" and read it (that will take you awhile), and build a simple toy compiler before you try to build a real one. I'm not objecting to you trying; I'm suggesting without the right background your chance of producing something useful is minimal. – Ira Baxter Jan 14 '13 at 18:30
  • @IraBaxter thanks you are right. can you give me the link to it –  Jan 14 '13 at 18:36
  • http://www.amazon.com/Compilers-Principles-Techniques-Alfred-Aho/dp/0201100886. You might also read this SO answer about building translators: http://stackoverflow.com/questions/3455456/how-to-translate-between-programming-languages/3460977#3460977 – Ira Baxter Jan 14 '13 at 18:45
  • This site is not frendly to learners and new beis. If is only situted for proffessional sinces evry question get to be close by some one who things he knows –  Jan 15 '13 at 12:04

1 Answers1

1

If this is some kind of class asignment, you should look for clarification or further instructions. If you give more details on what you are trying to do we could give more specific guide.

You might want to take a look at the Bison manual for examples on how to write a grammar in it (bison is the GNU yacc workalike, except for exotic stuff it should be applicable to whatever yacc you've got). I'm not aware of any yacc/lex tools that output Python, bison/flex can be coaxed into C++ and perhaps Java, but not Python. And interfacing the resulting parser to Python is probably much more pain than gain.

There are other tools that are higher-level (i.e., do more of the routine work), like ANTLR.

Check the FAQ for the comp.compìlers newsgroup, it should point you to specific tools.

vonbrand
  • 11,412
  • 8
  • 32
  • 52
  • the lex/yacc does not output python but rather it is a python modul called PLY –  Jan 14 '13 at 18:17