How can I get my own C ++ compiler to parse patterns, nested classes, etc. Strong C ++ features?

It is a university task in my group to write a C-like language compiler. Of course, I'm going to implement a small part of our favorite C ++.
The exact task is absolutely stupid, and the lecturer told us that he should be self-compiling (should be able to compile himself) - so he meant not to use libraries such as Boost and STL.
He also does not want us to use templates because it is difficult to implement.
The question is whether this is true for me, since I myself will write this project, with a deadline of late May - mid June (this year), to implement not only templates, but also nested classes, namespaces, virtual tables functions at the level of syntax analysis?
PS I'm not a noobie in C ++

+4
source share
9 answers

Stick to C compiler execution.

Believe me, it’s quite difficult to create a decent C compiler, especially if you expected to compile it. Trying to support all C ++ functions, such as nested classes and templates, will put you in a crazy state. Perhaps the group could do this, but on my own, I think the C compiler is more than enough.

If you're dead on this, at least run a C-like language first (so you have something to take). Then focus on the demo.

+28
source

"The exact task is absolutely stupid" - I do not think that you are able to fairly evaluate this decision. It is better to abandon this view.

"I will write this project myself" - you said it was a group project. You say that your group does not want to agree with your opinion that it should turn into C ++, so you shoot and work independently? Another bit that I would recommend changing.

It doesn't matter how much you are aware of C ++. Your abilities with grammars, parsers, lexers, AST and code generation seem much more related.

Without knowing more about you or the assignment, I would say that it will be good for you if the initial assignment is completed by the end of May. Three months have passed. Stick to the destination. It may surprise you with its difficulties.

If you finish early and fulfill your obligation to your team, I would say that you should not be shy to change what was created to add C ++ features.

I bet that it took Bjarne Stroustrup more than three months to add objects to C. Do not overestimate yourself or underestimate the original assignment.

+22
source

No problems. And while you are at it, why not implement the operating system so that it also works.

+10
source

Follow the appointment. Write a compiler for a C-like language! I would choose a subset of C. Delete floating point data types and any other function that is not needed when creating your compiler.

Writing a C compiler is a lot of work. You cannot do this in a couple of months. Writing a C ++ compiler is completely insane. You cannot do this after 5 years.

+7
source

I would like to emphasize several points mentioned earlier and give some links.

1) CONNECTION TO ANSI STANDARD C 1989 WITHOUT OPTIMIZATION.

2) Do not worry, with proper guidance, good organization and enough hard work, this is doable.

3) Read the C programming language for coverage.

4) Understand the important concepts of compiler development from Dragon Book .

5) Look at lcc for both code and book .

6) Look Lex and Yacc (or Flex and Bison)

7) Writing a C compiler (up to the point that it can compile independently) is a ritual of passing ritual among programmers. Enjoy it.

+5
source

For a class project, I believe that requiring a compiler to compile is a lot to ask. I guess this is what was meant by a stupid question. This means that you need to figure out in advance how much of C you are going to implement, and stick to this when creating the compiler. So, building a symbol table using primitives, not just using an STL map. This may be useful for a data structure course, but not suitable for a compiler course. This should be about understanding the problems associated with the compiler and using data structures, rather than coding data structures.

Building a compiler is a great way to understand what happens to your code after the compiler has mastered it. What is the target language? When I took the compilers, it took us 3 semesters to create a compiler to go from sorta-pascal to assembly. This is not a trivial task. Its one of those things that at first seems simple, but the more you delve into it, the harder it gets.

+3
source

You should be able to complete a c-like language in a period of time. Assuming you take more than 1 course, this is exactly what you could do on time. C ++ is also doable, but with a lot more extra hours. Assuming C ++ templates / virtual functions are redefining themselves, and you could compromise the purpose. Therefore, it is best to stick with the compiler of the subset c and finish it on time. You must also consider the time required to ensure quality. If you want to be solid, then QA will also take a good time.

+2
source

Namespaces or nested classes or virtual functions at the syntax level are fairly simple, with just one or two rules for parsing. It is much more difficult at higher levels when deciding which function / class chooses (shadow name, ambiguous names between namespaces, etc.) or when compiling into bytecode / running AST. So, you can write them, but if not necessary, skip it and write only one functional model.

+1
source

If you are talking about a compiler with code generation, then forget it. If you just intend to do the lexical and syntactic analytic side of things, then some form of templates can be executable all the time, depending on which compiler tools you use.

+1
source

All Articles