0

First i'm sorry for my bad english, it's not my natural language. So, i'm trying to parse a DSL to c++ code so i can later compile it with G++ or GCC. I don't need expression evaluation, variable tables or anything like this. I just need to copy them in the resulting file. As an example:

start:
  integer x
  read x
  write x + 2 
[]

this code should turn into:

#include <iostream>

int main(void){
   int x;
   std::cin>>x;
   std::cout<<x + 2;
return 0; }

and it should read an integer and write it + 2 to the console .. I would also like a if statement a while ... do ... a for and a do ... while ... It is possible with ANTLR4? I would like to use it, and if it's possible can you redirect me to a good tutorial series? I made something similar with flex and bison ... but it didn't turn out how i wished.

Thanks a lot men! You are awesome!

  • Your English is totally OK :) don't worry about it. We can ask if something is unclear. It would be, however, very helpful if you don't use ".." (that's not common in English), but made a "." when a sentence definitely ended. adding paragraphs (empty lines) also helps readability. The "thank you" phrase, however, is not very welcome on StackOverflow, because it just distracts from the question. – Marcus Müller Jan 11 '16 at 17:39
  • Unless your DSL is truly trivial, and its expressions are a perfect subset of what is legal in C++ (really?), you're going to have to generate code for expressions ("expression evaluation") and that will involve knowing the types of the operands. If it is trivial, you can simply use ANTLR's string templates to walk the tree and spit out the equivalent text. If not, you're going to need a lot more machinery. More discussion on this at http://stackoverflow.com/a/3460977/120163 – Ira Baxter Jan 11 '16 at 17:59

0 Answers0