5

I am trying to develop a compiler based on the LLVM infrastructure. My language has a C like syntax and so I would like to leverage existing C compilers. Right now I am focusing on the frontend (lexical analysis, parsing), but I am confused as to which frontend would be the best approach. Clang, llvm-gcc or traditional flex/bison tools?

I am specially interested in the simplest solution which would allow me to generate LLVM IR code for later stages of my compiler.

6
  • If you're doing your own parsing and semantic analysis, generating LLVM IR would be easy enough. You could have reused an existing frontend, like Clang, if you had to borrow a large part of C syntax. Otherwise it does not worth it, generating IR is trivial once you've got a valid AST. Commented Jul 16, 2012 at 9:22
  • Thanks. What should I use for generating IR from the AST? Commented Jul 17, 2012 at 17:20
  • use just straightforward substitution rules - see the LLVM Kaleidoscope example. Commented Jul 17, 2012 at 20:38
  • Look at this tutorial: gnuu.org/2009/09/18/writing-your-own-toy-compiler it shows how use bison+flex to create an AST and then export it to LLVM I.R. Commented Jul 18, 2012 at 22:25
  • Thanks for your answers. My language borrows a lot from C, so I thought it is better to hack existing compiler frontends (clang). Right now I am interested to simply add my own keywords to the lexer for a start, but I get a bit scared by the size of codes in the lex library. Can anyone point me to which files I should modify to make this happen. I am reading the documentation but it takes quite some time to get used to all the classes.. Commented Jul 19, 2012 at 6:02

1 Answer 1

2

If you want to code everything by hand, then I recommand you the LLVM tutorial Kaleidoscope:

Sign up to request clarification or add additional context in comments.

2 Comments

Is there some source code example? It's easier for me to understand source than text.
Well the source code is also provided in the tutorial: llvm.org/docs/tutorial/LangImpl8.html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.