is there someone here with experiences in building compilers ? - Page 1
Forum section: electronics / general

author: legacy
author_rank: Super Contributor
timestamp: November 21, 2015, 09:39:18 pm
content: just to know

author: ChunkyPastaSauce
author_rank: Unknown
timestamp: November 21, 2015, 09:55:33 pm
content: richardmanhttps://www.eevblog.com/forum/microcontrollers/memory-efficient-int-to-chars-without-division-%28bitshift-ok%29-for-binary-bases/msg804588/#msg804588

author: richardman
author_rank: Frequent Contributor
timestamp: November 21, 2015, 10:44:11 pm
content: Quote from: legacy on November 21, 2015, 09:39:18 pmYes, since 1984.

author: legacy
author_rank: Super Contributor
timestamp: November 21, 2015, 11:24:17 pm
content: AST based ?with the complexity of C language compiler ?if so, can you give me a few points about fresh start up(book, slide, whatever is useful)

author: richardman
author_rank: Frequent Contributor
timestamp: November 22, 2015, 12:01:56 am
content: Quote from: legacy on November 21, 2015, 11:24:17 pmHi Legacy, yes, I wrote/contributed to a lot of code generators and optimizers over the years. I think your goal wants to do a Lint/MISRA checker right? If so, you need to first categorize the MISRA rules into two sets: a) ones that a single file processor can handle, b) ones that require knowledge about multiple files or entire program. Then I would start with a known tools like splint or may be even GCC/CLang and look at the deficiencies. The toughest part would be the multiple file requirements.As for books/slides etc., your needs are fairly specialized, e.g. you don't really need to know ins and outs of parsing etc. per se. You also need to specify whether your tool can handle incorrect input or only input that are accepted by compilers. I would highly recommend the latter.Hope this helps.

author: legacy
author_rank: Super Contributor
timestamp: November 22, 2015, 12:08:34 am
content: Quote from: richardman on November 22, 2015, 12:01:56 ambingobut I'd also like to create a compiler for my softcore (fpga), which has uncommon ISAQuote from: richardman on November 22, 2015, 12:01:56 amI have already written a db-oriented file checker, it collects information from every file.cit's currently used to do- deadcode check- inter modules dependenciesI can reuse this projectQuote from: richardman on November 22, 2015, 12:01:56 amGcc: too complexClang: may be, it looks better, and well documented!

author: amyk
author_rank: Super Contributor
timestamp: November 22, 2015, 03:53:42 am
content: Yes, I have written a few compilers/interpreters.Take a look at this for some inspiration, a C-like self-compiling interpreter in only 4 functions and under 1kLOC:https://github.com/rswier/c4/blob/master/c4.c

author: ivaylo
author_rank: Frequent Contributor
timestamp: November 22, 2015, 07:24:16 am
content: The Dragon Book is classic -https://en.m.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools

author: richardman
author_rank: Frequent Contributor
timestamp: November 22, 2015, 10:42:28 am
content: Quote from: ivaylo on November 22, 2015, 07:24:16 amClassic and "none-better", but not very useful for writing a MISRA checker.

author: legacy
author_rank: Super Contributor
timestamp: November 22, 2015, 04:15:30 pm
content: I few months ago I experimented an RPN code gen (postedhere)So I have already had a little taste about the background with such a things (and I can reuse my code)QuoteQuote

author: zapta
author_rank: Super Contributor
timestamp: November 22, 2015, 04:41:02 pm
content: I have had good experience with javacc (Java Compiler Compiler). You write lexical, grammar and semantic rules in one files and it all type safe. It also has a mode called JJTree where it creates the AST using generic types but I prefer the custom tree with classes that represent the underlying language.Here is an example of a C parserhttps://java.net/downloads/javacc/contrib/grammars/C.jj

author: richardman
author_rank: Frequent Contributor
timestamp: November 22, 2015, 09:35:08 pm
content: The lexer is the easiest part of a compiler. Parsing expressions is easy. The most impressive I have ever seen is two mutually recursive function with a loop each. With that few lines, all the precedence and associativity issues were taken care of.The hard part of parsing is the forward declaration issues, and any grammar that are LR-easy but LL-hard - this is why compiler parser generators are used. Otherwise, a top down parser wins everytime for cleanliness and ease of design/implementation. You also have to take care of the mechanics of maintaining name spaces and scopes, but those are trivial.Then comes the semantic analysis.Writing a toy compiler is easy. Writing a production compiler is hard.

author: paf
author_rank: Regular Contributor
timestamp: November 23, 2015, 05:57:46 pm
content: Software and/or Links for "making" compilers:Java based:AntLRhttp://www.antlr.org/JavaCChttps://java.net/projects/javaccJflex:http://jflex.de/CUPhttp://www2.cs.tum.edu/projects/cup/C based tools:Ragel (not for writing a full compiler, but may be useful):http://www.colm.net/open-source/ragel/Portable C compilerhttp://pcc.ludd.ltu.se/LCC compiler:https://sites.google.com/site/lccretargetablecompiler/Tiny C compiler:http://bellard.org/tcc/SDCC - Small Device C Compilerhttp://sdcc.sourceforge.net/(Old) Books:James Hendrix's book "A Small-C Compiler: Language, Usage, Theory, and Design":http://www.drdobbs.com/developer-network-small-c-compiler-book/184415519https://en.wikipedia.org/wiki/Small-CAllen Holub's book: "Compiler Design in C":http://www.holub.com/software/compiler.design.in.c.html

author: legacy
author_rank: Super Contributor
timestamp: November 24, 2015, 12:32:39 am
content: Quote from: paf on November 23, 2015, 05:57:46 pmthank you for this list

author: amyk
author_rank: Super Contributor
timestamp: November 24, 2015, 05:10:25 am
content: Quote from: richardman on November 22, 2015, 09:35:08 pmSomething like this?https://www.engr.mun.ca/~theo/Misc/exp_parsing.htm#more_climbing

author: richardman
author_rank: Frequent Contributor
timestamp: November 24, 2015, 09:26:24 am
content: If you want a copy of the Holub book, PM me. I will sell it cheap :-)

author: miguelvp
author_rank: Super Contributor
timestamp: November 24, 2015, 09:49:26 am
content: Quote from: richardman on November 24, 2015, 09:26:24 amI still have the hard cover one from 1990 that according to the big sticker it cost me $48 back then.But as the link says, it's freely downloadable.

author: paf
author_rank: Regular Contributor
timestamp: November 24, 2015, 12:51:27 pm
content: Forgot about this one, very easy to read as an intro, with C/C++ and Java versions, with updates and freely downloadable:Seth D. Bergmann - Compiler Design Textbookshttp://elvis.rowan.edu/~bergmann/books.html

author: legacy
author_rank: Super Contributor
timestamp: November 24, 2015, 01:21:56 pm
content: Quote from: paf on November 24, 2015, 12:51:27 pmCode:url="http://elvis.rowan.edu/~bergmann/books/c_cpp/miniC"list="bisect.ccos.cexp.cfact.cgen.cmakefilemini.cmini.hminiC.CminiC.hminiC.lminiC.y"for item in $listdoecho -n "$item ... "wget $url/$item 1>1 2>2echo "done"donethe above script gets sources, I just wanted to "check it out", but it seems I have a problem with YaccCode:makeyacc -v miniC.yminiC.y:65.42-43: $$ for the midrule at $5 of `ForStmt' has no declared typeminiC.y:66.69-70: $$ for the midrule at $5 of `ForStmt' has no declared typeminiC.y:67.42-43: $$ for the midrule at $8 of `ForStmt' has no declared typeminiC.y:70.42-43: $$ for the midrule at $8 of `ForStmt' has no declared typeminiC.y:73.42-43: $$ for the midrule at $8 of `ForStmt' has no declared typeminiC.y:88.42-43: $$ for the midrule at $2 of `WhileStmt' has no declared typeminiC.y:89.69-70: $$ for the midrule at $2 of `WhileStmt' has no declared typeminiC.y:90.42-43: $$ for the midrule at $6 of `WhileStmt' has no declared typeminiC.y:91.69-70: $$ for the midrule at $6 of `WhileStmt' has no declared typeminiC.y:97.42-43: $$ for the midrule at $5 of `IfStmt' has no declared typeminiC.y:98.72-73: $$ for the midrule at $5 of `IfStmt' has no declared typeminiC.y:99.42-43: $$ for the midrule at $7 of `IfStmt' has no declared typeminiC.y:100.70-71: $$ for the midrule at $7 of `IfStmt' has no declared typemake: *** [y.tab.c] Error 1

author: legacy
author_rank: Super Contributor
timestamp: December 06, 2015, 04:32:59 pm
content: Code:private boolean_t is_ok;/** this is a comment*//** // this kind of comment is not accepted*//*this is ADA: does it look nicer than C ?procedure Example isX: array (Integer range 1..5) of Integer;beginfor I in X'Range loopX(I) := I;end loop;end Example;*/private uint32_t my_var1;private uint32_t my_var2;public boolean_t is_ok;private char_t hAllo;private char_t msg1[]="hAllo";private char_t msg2;public uint32_t ciao;private boolean_t is_ok(){}/** hallo*//*// hAllo again*/private void_t hallo1(p_my_t p_my){}private void_t hallo2(p_my_t p_my){while (False){;}}private uint32_t my_var1;public typedef struct{{{}}} my_t;I am working on an interpreterits grammar looks almost like the C89 grammar, but more simple and with a strict definition of what is public and what it's privatealso every "type" must end with "_t", otherwise a syntax error is issuedconsider it as an "home work" to learn more about "interpreters and compilers"currently I have implemented only the parser without a semantical reaction

author: richardman
author_rank: Frequent Contributor
timestamp: December 07, 2015, 08:13:01 am
content: Quote from: legacy on December 06, 2015, 04:32:59 pmNEVER do that. Imposing a lexical convention onto language semantic is like dictating that spaces and indentation matter, or that variables must be upper case, or...You need to learn about language grammar. For example, the declaration placement of the original C was defined that way is so that a) you only have to look for a declaration in certain places, e.g. right after a {, and b) the first "word" tells you whether it is a declaration or not (e.g. a type name of a typedef name). It's not that big of a deal to look for declaration everywhere but limiting it like the original C is still much better than using a lexical convention.

author: legacy
author_rank: Super Contributor
timestamp: December 07, 2015, 04:34:42 pm
content: but a constraint that I have in my job with avionic is: every type must be defined with typedef struct/union and MUST ends with "_t"so you can't pass a parameter without a script typefoo(struct blabla var) is bannedfoo (blabla_t var) is allowedand you have to add "_t" to the end,typedef struct { … } blabla_t;and you have to use "p_" with pointer

author: richardman
author_rank: Frequent Contributor
timestamp: December 08, 2015, 08:08:09 am
content: Yes, but YOUR language does not have to do it at the lexical level. The proper way to do it is to define a grammar where type if accepted is appropriate, then during the SEMANTIC phase, you add brain-fart constraints like the Avionic do-do-heads require.This will allow your interpreter to be used in proper environment, without stupid constraints.Quote from: legacy on December 07, 2015, 04:34:42 pm

author: jsmith45
author_rank: Newbie
timestamp: December 13, 2015, 09:43:00 am
content: Quote from: richardman on December 08, 2015, 08:08:09 amMore practically, implementing these rules as a constrain that is applied after parsing often makes it easier to provide a more useful error message. (Such as being able to find the typedef for the type and show the correct typedef name they should have used in the error message).