CoolFace
Datasetpublic

enigmare/v2-crawler

sourceHugging Faceupdated 28d agoView on Hugging Face
1likes904downloads
compiler-construction.jsonl1428 linesDownload Raw Back to stackoverflow
1{"id":"stack-2171177","source":"stackoverflow","questionId":2171177,"title":"What is an application binary interface (ABI)?","tags":["compiler-construction","abi"],"text":"Title: What is an application binary interface (ABI)?\nTags: compiler-construction, abi\nSource: Stack Overflow\n\nQuestion:\nI never clearly understood what an ABI is. Please don't point me to a Wikipedia article. If I could understand it, I wouldn't be here posting such a lengthy post.\n\nThis is my mindset about different interfaces:\n\nA TV remote is an interface between the user and the TV. It is an existing entity, but useless (doesn't provide any functionality) by itself. All the functionality for each of those buttons on the remote is implemented in the television set.\n\n **Interface:** It is an \"existing entity\" layer between the\n `functionality` and `consumer` of that functionality. An interface by itself\n doesn't do anything. It just invokes the functionality lying behind.\n\n \n Now depending on who the user is there are different type of interfaces.\n\n \n **Command Line Interface (CLI)** commands are the existing entities,\n the consumer is the user and functionality lies behind.\n\n \n `functionality:` my software functionality which solves some\n purpose to which we are describing this interface.\n\n \n `existing entities:` commands\n\n \n `consumer:` user\n\n \n **Graphical User Interface(GUI)** window, buttons, etc. are the existing\n entities, and again the consumer is the user and functionality lies behind.\n\n \n `functionality:` my software functionality which solves some problem to which we are describing this interface.\n\n \n `existing entities:` window, buttons etc..\n\n \n `consumer:` user\n\n \n **Application Programming Interface(API)** functions (or to be\n more correct) interfaces (in interfaced based programming) are the\n existing entities, consumer here is another program not a user, and again\n functionality lies behind this layer.\n\n \n `functionality:` my software functionality which solves some\n problem to which we are describing this interface.\n\n \n `existing entities:` functions, Interfaces (array of functions).\n\n \n `consumer:` another program/application.\n\n \n **Application Binary Interface (ABI)** Here is where my problem starts.\n\n \n `functionality:` ???\n\n \n `existing entities:` ???\n\n \n `consumer:` ???\n\n- I've written software in different languages and provided different kinds of interfaces (CLI, GUI, and API), but I'm not sure if I have ever provided any ABI.\n\nWikipedia says:\n\n ABIs cover details such as\n\n \n \n \n- data type, size, and alignment;\n the calling convention, which controls how functions' arguments are\n passed and return values retrieved;\n the system call numbers and how an application should make system calls\n to the operating system;\n \n \n Other ABIs standardize details such as\n\n \n \n \n- the C++ name mangling,\n \n- exception propagation, and\n calling convention between compilers on the same platform, but do\n not require cross-platform compatibility.\n \n\nWho needs these details? Please don't say the OS. I know assembly programming. I know how linking & loading works. I know exactly what happens inside.\n\nWhy did C++ name mangling come in? I thought we are talking at the binary level. Why do languages come in?\n\nAnyway, I've downloaded the [PDF] System V Application Binary Interface *Edition 4.1 (1997-03-18)* to see what exactly it contains. Well, most of it didn't make any sense.\n\nWhy does it contain two chapters (4th & 5th) to describe the ELF file format? In fact, these are the only two significant chapters of that specification. The rest of the chapters are \"processor specific\". Anyway, I though that it is a completely different topic. Please don't say that ELF file format specifications *are* the ABI. It doesn't qualify to be an *interface* according to the definition.\n\nI know, since we are talking at such a low level it must be very specific. But I'm not sure how is it \"instruction set architecture (ISA)\" specific?\n\nWhere can I find Microsoft Windows' ABI?\n\nSo, these are the major queries that are bugging me.\n\n========================================\n\nTop Answer:\nIf you know assembly and how things work at the OS-level, you are conforming to a certain ABI. The ABI govern things like how parameters are passed, where return values are placed. For many platforms there is only one ABI to choose from, and in those cases the ABI is just \"how things work\". \n\nHowever, the ABI also govern things like how classes/objects are laid out in C++. This is necessary if you want to be able to pass object references across module boundaries or if you want to mix code compiled with different compilers.\n\nAlso, if you have an 64-bit OS which can execute 32-bit binaries, you will have different ABIs for 32- and 64-bit code.\n\nIn general, any code you link into the same executable must conform to the same ABI. If you want to communicate between code using different ABIs, you must use some form of RPC or serialization protocols.\n\nI think you are trying too hard to squeeze in different types of interfaces into a fixed set of characteristics. For example, an interface doesn't necessarily have to be split into consumers and producers. An interface is just a convention by which two entities interact.\n\nABIs can be (partially) ISA-agnostic. Some aspects (such as calling conventions) depend on the ISA, while other aspects (such as C++ class layout) do not.\n\nA well defined ABI is very important for people writing compilers. Without a well defined ABI, it would be impossible to generate interoperable code.\n\nEDIT: Some notes to clarify:\n\n- \"Binary\" in ABI does not exclude the use of strings or text. If you want to link a DLL exporting a C++ class, somewhere in it the methods and type signatures must be encoded. That's where C++ name-mangling comes in.\n\n- The reason why you never provided an ABI is that the vast majority of programmers will never do it. ABIs are provided by the same people designing the platform (i.e. operating system), and very few programmers will ever have the privilege to design a widely-used ABI.\n\n========================================\n\nCode:\n```text\nfunctionality\n```\n\n```text\nconsumer\n```\n\n```text\nfunctionality:\n```\n\n```text\nexisting entities:\n```\n\n```text\nconsumer:\n```\n\n```text\nfunctionality:\n```\n\n```text\nexisting entities:\n```\n\n```text\nconsumer:\n```\n\n```text\nfunctionality:\n```\n\n```text\nexisting entities:\n```\n\n```text\nconsumer:\n```\n\n```text\nfunctionality:\n```\n\n```text\nexisting entities:\n```\n\n```text\nconsumer:\n```\n\n```text\nextern \"c\"\n```\n\n```text\n_MyFunction1:\n```\n\n```text\n_MyFunction1:\n```\n\n```text\njoda-time-<major>.<minor>.<patch>.jar\n```\n\n```text\njoda-time-1.7.2.jar\n```\n\n```text\njoda-time-1.7.2.jar\n```\n\n```text\njoda-time-1.7.2-dalvik.jar\n```\n\n```text\n-target\n```\n\n```text\n<major>-<minor>-<patch>-<platform/ABI>\n```\n\n```text\nrpm\n```\n\n```text\ndll\n```\n\n```text\njar\n```\n\n```text\nwar\n```\n\n```text\napk\n```\n\n```text\n2.11\n```\n\n```text\nvoid initfoo(FOO * foo)\nint usefoo(FOO * foo, int bar)\nvoid cleanupfoo(FOO * foo)\n```\n\n```text\nint dostuffwithfoo(int bar) {\n  FOO foo;\n  initfoo(&foo);\n  int result = usefoo(&foo,bar)\n  cleanupfoo(&foo);\n  return result;\n}\n```\n\n```text\nFOO * newfoo(void)\nint usefoo(FOO * foo, int bar)\nvoid deletefoo(FOO * foo)\n```\n\n```text\nint dostuffwithfoo(int bar) {\n  FOO * foo;\n  foo = newfoo();\n  int result = usefoo(foo,bar)\n  deletefoo(foo);\n  return result;\n}\n```\n\n```text\n#include <assert.h>\n#include <stdlib.h>\n\n#include \"mylib.h\"\n\nint main(void) {\n    mylib_mystruct *myobject = mylib_init(1);\n    assert(myobject->old_field == 1);\n    free(myobject);\n    return EXIT_SUCCESS;\n}\n```\n\n```text\n#include <stdlib.h>\n\n#include \"mylib.h\"\n\nmylib_mystruct* mylib_init(int old_field) {\n    mylib_mystruct *myobject;\n    myobject = malloc(sizeof(mylib_mystruct));\n    myobject->old_field = old_field;\n    return myobject;\n}\n```\n\n```text\n#ifndef MYLIB_H\n#define MYLIB_H\n\ntypedef struct {\n    int old_field;\n} mylib_mystruct;\n\nmylib_mystruct* mylib_init(int old_field);\n\n#endif\n```\n\n```text\ncc='gcc -pedantic-errors -std=c89 -Wall -Wextra'\n$cc -fPIC -c -o mylib.o mylib.c\n$cc -L . -shared -o libmylib.so mylib.o\n$cc -L . -o main.out main.c -lmylib\nLD_LIBRARY_PATH=. ./main.out\n```\n\n```text\ntypedef struct {\n    int new_field;\n    int old_field;\n} mylib_mystruct;\n```\n\n```text\nmyobject->old_field == 1\n```\n\n```text\ntypedef struct {\n    int old_field;\n    int new_field;\n} mylib_mystruct;\n```\n\n```text\nmylib_mystruct* mylib_init(int old_field, int new_field);\n```\n\n```text\nmyobject->old_field = old_field;\n```\n\n```text\nmyobject->old_field = old_field + 1;\n```\n\n```text\nmylib_mystruct\n```\n\n```text\nnew_field\n```\n\n```text\nold_field\n```\n\n```text\nmain.out\n```\n\n```text\nint\n```\n\n```text\nnew_field\n```\n\n```text\nold_field\n```\n\n```text\nnew_field\n```\n\n```text\nold_field\n```\n\n```text\nint\n```\n\n```text\nmylib_mystruct\n```\n\n```text\nnew_field\n```\n\n```text\nold_field\n```\n\n```text\nmain.c\n```\n\n```text\nmain.c\n```\n\n```text\nmain.c\n```\n\n```text\nModule compiled with _ cannot be imported by the _ compiler\n//or\nCompiled module was created by a newer version of the compiler\n```\n\n```text\nABI\n```\n\n```text\nApplication Binary Interface\n```\n\n```text\nABI\n```\n\n```text\ncalling convention\n```\n\n```text\nApplication layer\n```\n\n```text\nSwift\n```\n\n```text\nObjective-C\n```\n\n```text\nApplication - OS layer\n```\n\n```text\nSwift Standard Library\n```\n\n```text\nSwift Run Time Library\n```\n\n```text\nLibrary layer\n```\n\n```text\nModule Stability\n```\n\n```text\n.swiftinterface\n```\n\n```text\n.swiftmodule\n```\n\n```text\nLibrary layer\n```\n\n```text\nLibrary Evolution\n```\n\n========================================\n\nComments:\n- \"Please don't say, OS\" Compilers need to know the ABI. Linkers need to know the ABI. The kernel needs to know the ABI in order to set up the program in RAM for it to run properly. As for C++ see below, it intentionally turns labels into gibberish because of overloading and private methods, and the linker and any other compiler need to have compatible name mangling to work with it, in other words the same ABI.\n- \"how is it \"Instruction Set Architecture(ISA)\" specific?\" you cannot run an x86 executable on an ARM processor, period. So the ABI used on an x86 system cannot be the same one used on ARM.\n- I think the question is so clear; exactly describing what is the answer format expected and yet not a single satisfactory answer that which can be accepted.\n- You are using a very strange definition of \"interface\". Consider merely an interface as the point at which, or mechanism through which, two or more entities interact. An ABI is merely a description of the low-level (i.e. bit twiddling) interaction of components in a software system---allowing, for instance, code written in C to call code written in Fortran by appropriate translation of data types and selection of calling conventions. No need to get philosphical. A CLI and an API are not meaningfully in the same class of entity, and attempting to find such a framework is fruitless.\n- @legends2k My take on the issue is that OP does indeed know what an ABI is, but doesn't realize that. The vast majority of programmers will never ever design or provide an ABI, because that is the job of OS/platform designers.\n- @JesperE: I do agree to your point. But probably the OP wants to know it clearly, in the format s/he sees fit, even though s/he mightn't need to provide an ABI.\n- I think the answer should be something like `Consumer: Compiler (Writer)`, `Provider: OS (Maker)`, `Functionality: Defining protocol on how to pass data`... (am just giving examples here).\n- @JesperE: As a matter of fact, I didn't :)\n- I'm not at all convinced that my template is faulty. Because every where this template for interface is holding true. So, yes I want I expect ABI also to fit into this template but thats not it. IMPORTANT thing is I still don't understand. I don't know if I'm so dumb or something else but it just not getting into my head. I'm unable to realize the answers and the wiki article.\n- I was ignorant. Recently while working with all these things. I realized what ABI is actually is. Yeah, I do agree that my template is faulty. Its not appropriate to fit ABI into my template. Thanks @ JasperE. It just took work experience to realize your answer.\n- @claws, This explanation may help: stackoverflow.com/questions/3784389/&hellip;\n- The easiest way to think of an interface, in general, is as a contract between a framework, library, platform, etc. If you want to use that system, you are expected to adhere to the rules laid out in the interface. It is a \"Rules of engagement\" notice.\n- I'm not at all convinced that my template is faulty. Because every where this template for interface is holding true. So, yes I want I expect ABI also to fit into this template but thats not it. IMPORTANT thing is I still don't understand. I don't know if I'm so dumb or something else but it just not getting into my head. I'm unable to realize the answers and the wiki article.\n- @jesperE, \"The ABI govern things like how parameters are passed, where return values are placed. \" is refer to \"cdecl,stdcall,fastcall,pascal\" right?\n- Yes. The proper name is \"calling convention\", which is a part of the ABI. en.wikipedia.org/wiki/X86_calling_conventions\n- this is *the* correct and *precise* answer without the verbosity (rather *noise*)!\n- I recommend writing a bit of assembly. This will help people understand ABI in a more tangible way.\n- @bta, Thanks for the great answer. Does calling convention is a kind of ABI? Thanks\n- Nice answer. Except this isn't what an ABI is. An ABI is a set of rules that determines calling convention, and rules for laying out structures. Pascal passes arguments on the stack in the reverse order from C applications, so pascal and C compilers do NOT compile to the same ABI. The respective standards for C and Pascal compilers implicitly ensure that this will be the case. C++ compilers cannot define a \"standard\" way to mangle names, since there is no standard way. C++ name mangling conventions were not compatible between C++ compilers when there were competing C++ compilers on Windows.\n- Definitely also see autotools.io/libtool/version.html and fedoramagazine.org/&hellip;\n- @RobinDavies: On platforms where Pascal compilers would have called functions pop arguments given by their callers, C compilers would generally define means by which a programmer could indicate that particular functions should use, or should be expected to use, the same calling conventions as the Pascal compilers even though C compilers would generally by default use a convention where called functions leave on the stack anything placed there by their callers.\n- @RobinDavies *determines calling convention* Dealing with stdcall, pascal, cdecl over the years, your comment made instant sense.\n- `extern \"C\"` is to compile and link in a way C langauge does. Since there is no standardized way for C++ compiler to deal with name mangling, no so-called \"standardized way of recording names\". `extern \"C\"` proved to be useful when hybrid programming using C and C++ in same file.\n- A super high level definition from my computer architecture class note(CDA4205) says: \"it is the combination of the basic instruction set and the operating system interface provided for application programming.\"\n- Why would the ABI be defined by the architecture? Why would different OSes on the same architecture not be able to define different ABIs?\n- The Windows x86-64 calling convention doesn't use the SysV calling convention that all other x86-64 OSes use. Linux/OS X/FreeBSD all the same calling convention, but they *don't* the full ABI. An OS's ABI includes system-call numbers. e.g. freebsd.org/doc/en_US.ISO8859-1/books/developers-handbook/&hellip; says that `SYS_execve` is 11 on 32bit linux, but 59 on FreeBSD.\n- thanks for your comment, i have modified my comment to better answer the difference between ABI and API.\n- You're still missing the difference between a calling convention and a full ABI (system calls and everything). You can run some FreeBSD binaries on Linux, because Linux (the kernel) provides a FreeBSD compatibility layer. Even then, this is limited to binaries that don't try to use any part of the FreeBSD ABI that Linux doesn't provide. (e.g. any FreeBSD-only system call). ABI-compatible means you can run the same binary on both systems, not just that they'd compile similarly.\n- \"FreeBSD compatibility layer\", never heard of that. Can you point to the relevant linux kernel source code? But the reverse do exists: freebsd.org/doc/en_US.ISO8859-1/books/handbook/linuxemu.html&zwnj;&#8203;.\n- It's not something I use. I *thought* something like that existed, but maybe it doesn't anymore. tldp.org/HOWTO/Linux+FreeBSD-6.html says it's unmaintained, and that howto is from 2000. xD. unix.stackexchange.com/questions/172038/&hellip; confirms that it was abandoned and never re-done (since nobody wanted it badly enough to get it done). `personality(2)` can set `PER_BSD`. I think I remember seeing `personality(PER_LINUX)` in `strace` output all the time, but modern 64bit Linux binaries don't do that anymore.\n- Also, I removed my downvote since sometimes *ABI* does just mean calling convention, name-mangling, and stuff like that, but not the whole system call interface to the OS. I still think this answer is bit messy and scattered, so I'm not upvoting it. IIRC, the other answers do a good job, and this one doesn't cover much if anything that isn't already covered by higher-voted answers.\n- python3 -m venv .venv source .venv/bin/activate # On Windows: .venv\\Scripts\\activate\n- what does `cc='gcc...'` mean? `cc` should be the same as `gcc` in most Linux systems, but I don't get what `='gcc some_flags...'` part means.\n- @starriet it defines the bash shell variable `cc` to `gcc ...`. Then I use gcc with those flags multiple times. It is just to not repeat the flags.\n- adding a new field will break ABI, you don't see it in your example because in your code the main is too small using only 1 library, had used a few others would have shown the violation\n- @codeswimmer can you provide an example?\n- @CiroSantilliOurBigBook.com just reading a book books.google.com.au/&hellip; page 10","metadata":{"transformedAt":"2026-08-18T18:32:17.738Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":76,"totalLines":541,"estimatedTokens":4501}}2{"id":"stack-1669","source":"stackoverflow","questionId":1669,"title":"Learning to write a compiler","tags":["compiler-construction","language-agnostic"],"text":"Title: Learning to write a compiler\nTags: compiler-construction, language-agnostic\nSource: Stack Overflow\n\nQuestion:\n*Preferred languages*: C/C++, Java, and Ruby.\n\nI am looking for some helpful books/tutorials on how to write your own compiler simply for educational purposes. I am most familiar with C/C++, Java, and Ruby, so I prefer resources that involve one of those three, but any good resource is acceptable.\n\n========================================\n\nTop Answer:\nThis is a pretty vague question, I think; just because of the depth of the topic involved. A compiler can be decomposed into two separate parts, however; a top-half and a bottom-one. The top-half generally takes the source language and converts it into an intermediate representation, and the bottom half takes care of the platform specific code generation.\n\nNonetheless, one idea for an easy way to approach this topic (the one we used in my compilers class, at least) is to build the compiler in the two pieces described above. Specifically, you'll get a good idea of the entire process by just building the top-half.\n\nJust doing the top half lets you get the experience of writing the lexical analyzer and the parser and go to generating some \"code\" (that intermediate representation I mentioned). So it will take your source program and convert it to another representation and do some optimization (if you want), which is the heart of a compiler. The bottom half will then take that intermediate representation and generate the bytes needed to run the program on a specific architecture. For example, the the bottom half will take your intermediate representation and generate a PE executable. \n\nSome books on this topic that I found particularly helpful was Compilers Principles and Techniques (or the Dragon Book, due to the cute dragon on the cover). It's got some great theory and definitely covers Context-Free Grammars in a really accessible manner. Also, for building the lexical analyzer and parser, you'll probably use the *nix tools lex and yacc. And uninterestingly enough, the book called \"lex and yacc\" picked up where the Dragon Book left off for this part.\n\n========================================\n\nCode:\n```text\npublic String maxAsString(int a, int b) {\n    if (a > b) {\n        return Integer.valueOf(a).toString();\n    } else if (a < b) {\n        return Integer.valueOf(b).toString();\n    } else {\n        return \"equals\";\n    }\n}\n```\n\n```text\nBCELifier bcelifier = new BCELifier(\"MyClass\", System.out);\nbcelifier.start();\n```\n\n```text\nprivate void createMethod_1() {\n  InstructionList il = new InstructionList();\n  MethodGen method = new MethodGen(ACC_PUBLIC, Type.STRING, new Type[] { Type.INT, Type.INT }, new String[] { \"arg0\", \"arg1\" }, \"maxAsString\", \"MyClass\", il, _cp);\n\n  il.append(InstructionFactory.createLoad(Type.INT, 1)); // Load first parameter to address 1\n  il.append(InstructionFactory.createLoad(Type.INT, 2)); // Load second parameter to adress 2\n    BranchInstruction if_icmple_2 = InstructionFactory.createBranchInstruction(Constants.IF_ICMPLE, null); // Do if condition (compare a > b)\n  il.append(if_icmple_2);\n  il.append(InstructionFactory.createLoad(Type.INT, 1)); // Load value from address 1 into the stack\n  il.append(_factory.createInvoke(\"java.lang.Integer\", \"valueOf\", new ObjectType(\"java.lang.Integer\"), new Type[] { Type.INT }, Constants.INVOKESTATIC));\n  il.append(_factory.createInvoke(\"java.lang.Integer\", \"toString\", Type.STRING, Type.NO_ARGS, Constants.INVOKEVIRTUAL));\n  il.append(InstructionFactory.createReturn(Type.OBJECT));\n  InstructionHandle ih_13 = il.append(InstructionFactory.createLoad(Type.INT, 1));\n  il.append(InstructionFactory.createLoad(Type.INT, 2));\n    BranchInstruction if_icmpge_15 = InstructionFactory.createBranchInstruction(Constants.IF_ICMPGE, null); // Do if condition (compare a < b)\n  il.append(if_icmpge_15);\n  il.append(InstructionFactory.createLoad(Type.INT, 2));\n  il.append(_factory.createInvoke(\"java.lang.Integer\", \"valueOf\", new ObjectType(\"java.lang.Integer\"), new Type[] { Type.INT }, Constants.INVOKESTATIC));\n  il.append(_factory.createInvoke(\"java.lang.Integer\", \"toString\", Type.STRING, Type.NO_ARGS, Constants.INVOKEVIRTUAL));\n  il.append(InstructionFactory.createReturn(Type.OBJECT));\n  InstructionHandle ih_26 = il.append(new PUSH(_cp, \"equals\")); // Return \"equals\" string\n  il.append(InstructionFactory.createReturn(Type.OBJECT));\n  if_icmple_2.setTarget(ih_13);\n  if_icmpge_15.setTarget(ih_26);\n  method.setMaxStack();\n  method.setMaxLocals();\n  _cg.addMethod(method.getMethod());\n  il.dispose();\n}\n```\n\n========================================\n\nComments:\n- ANTLR all the way. All the resources proposed below looks like an overkill to me. ANTLR is always a compiler designer best friend. A\n- If your main focus is to learn how compiling ideas work in general - you can check and SICP short for Structured Interpretation of Computer program based in Scheme ( List) but teaches the general principles . mitpress.mit.edu/sicp . I was recommended this book by a veteran who works for a company and does these works compilation and interpretation for a living !\n- A shameless plug: my answer on a similar question.\n- I wrote an article on creating a compiler on my blog: orangejuiceliberationfront.com/how-to-write-a-compiler It focuses on the very basics and getting started, really. There's a bunch more compiler/codegen/parser/language design-related articles on there.\n- I've read `Let's Build a Compiler` [compilers.iecc.com/crenshaw/] series, it is really nice writeup and is a good starting point.\n- I think one worth mentioning is Coursera's compilers course. It has nice videos and walks through creating a java like language / simple compiler. Coursera Compilers Link\n- I wanted to keep this answer posted to being as original as possible so I decided to post this reference here: tutorialspoint.com/compiler_design/index.htm What I liked about this site is that it doesn't get involved with actually writing any code to create a compiler, but it does break down the compiler into its parts: phases and stages. It does describe the logic and algorithmic design approach without any specific language paradigm as it expresses the notations of an arbitrary language and alphabet. It is a quick read, but gives you the concepts of what is needed for each part.\n- The link by @TheVillageIdiot is broken (due to a formatting error in the comment), this works: compilers.iecc.com/crenshaw\n- I agree that this series is a bit outdated, although it is still useful. However, my biggest gripe with it is the fact that it tries to output straight to assembly language rather than building any type of parse tree, which means (contrary to what is stated in the first article) that it isn't very useful for writing an interpreter.\n- Game Scripting Mastery is a great learning resource because when you're done you will have a playable, scriptable 2D adventure game. This makes every exercise focused on a specific purpose, and keeps the reader motivated.\n- Dragon is a bit overly focussed on grammar based parsing. If you are not trying to parse something sheer impossible like C++ or so using parser generators, but can use e.g. a handcrafted LL grammar you might want to look out for something that treats a higher percentage compiler fields other than grammar transformation and proving\n- The first link is dead.\n- Compiler Construction PDF ethoberon.ethz.ch/WirthPubl/CBEAll.pdf\n- I strongly recommend *against* the C version of \"Modern Compiler Implementation\", it's crippled by low-level details due to C. It completely clutters the book. Java 1st is not too good as its OO design is poor, Java 2nd ed is no longer about the Tiger language. So I strongly recommend the ML one: it is not necessary to be fluent in ML to understand it. ML is definitely well suited for the job.\n- But the documentation for setting it up of Visual Studio is badly written, plus no examples\n- This seems like an **extremely good resource** thanks.\n- I wouldn't say \"too complicated\", I would say \"badly written\".\n- What does \"not quite Java\" mean?\n- haha, sorry, i meant its written for .Net, which in principal is similar to java. Both are JIT in style. :)\n- If you haven't read the Dragon book. Please don't recommend it. In fact, have you ever implemented a compiler?\n- Yeah, as the name implies, the Dragon Book is a monster. Very in-depth, but a very good resource nonetheless. I wouldn't recommend it for beginners, though...\n- @Neil: You haven't google'd me, have you? lol. blog.280z28.org But no, I haven't read that book.\n- I'm reading it (the dragon book) presently, and also Lex/Yacc at the same time, I'm finding the book quite good. Personally.\n- If you like it all well and good. My problem is with people that blindly recommend it whenever the word \"compiler\" is mentioned. Particularly if they haven't actually read it!\n- Neil, what do you find bad about the book? I haven't read it yet, I just keep hearing it's a good book, until now.\n- To be fair, I prefaced it with \"I hear...\". :) #1 and #3 are the points I feel are extremely important to know going in but aren't mentioned as often.\n- @GMan I personally find it unreadable & I disagree with its approach. Aho (main author) has always believed in the table-driven, automated approach to compilers. I disagree, particularly when teaching people how to write them. Having said that, as I hated whatever edition I've got (can't be arsed looking, but early I think) , the later editions may be worthwhile, I suppose.\n- It's still worth reading the Dragon Book even if you disagree with its approach. Compiler design is a very sticky subject and it's important to understand all the strange issues one has to contend with.\n- ++ You're right that it's good to know all those things, and it can be a big job, but I also learned from some experts how *not* to make things a big deal. It's good to know things, and it's even better to know when not to use them, which is most of the time.\n- We were taught Compilers subject in college with Dragon Book, and i can vouch for it, it was one of the best books I've came through\n- As others say, is BIG argument, I think sushi a task is a final work for bachelor, it requires to know a LOT of concepts of math, computer science and so on.\n- If you don't know these topics, you shouldn't really be trying to build a serious compiler. However, if you have 2-3 years undergraduate computer science education (programming, data structures, assembly language), the MetaII paper will work for you.\n- If you have not much time, don' write a compiler.\n- Added it to the list thanks :)","metadata":{"transformedAt":"2026-08-18T18:32:17.738Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":109,"estimatedTokens":2662}}3{"id":"stack-95635","source":"stackoverflow","questionId":95635,"title":"What does a just-in-time (JIT) compiler do?","tags":["compilation","compiler-construction","jit"],"text":"Title: What does a just-in-time (JIT) compiler do?\nTags: compilation, compiler-construction, jit\nSource: Stack Overflow\n\nQuestion:\nWhat does a JIT compiler specifically do as opposed to a non-JIT compiler? Can someone give a succinct and easy to understand description?\n\n========================================\n\nTop Answer:\nIn the beginning, a compiler was responsible for turning a high-level language (defined as higher level than assembler) into object code (machine instructions), which would then be linked (by a linker) into an executable.\n\nAt one point in the evolution of languages, compilers would compile a high-level language into pseudo-code, which would then be interpreted (by an interpreter) to run your program. This eliminated the object code and executables, and allowed these languages to be portable to multiple operating systems and hardware platforms. Pascal (which compiled to P-Code) was one of the first; Java and C# are more recent examples. Eventually the term P-Code was replaced with bytecode, since most of the pseudo-operations are a byte long.\n\nA Just-In-Time (JIT) compiler is a feature of the run-time interpreter, that instead of interpreting bytecode every time a method is invoked, will compile the bytecode into the machine code instructions of the running machine, and then invoke this object code instead. Ideally the efficiency of running object code will overcome the inefficiency of recompiling the program every time it runs.\n\n========================================\n\nCode:\n```text\nwhile(i<10){\n    // ...\n    a=a+i;\n    // ...\n }\n```\n\n```text\nIntermediate representation    JIT    Native machine code for the current CPU architecture\n\n     Java bytecode            --->        machine code\n     Javascript (run with V8) --->        machine code\n```\n\n========================================\n\nComments:\n- ibm.com/support/knowledgecenter/SSYKE2_7.1.0/&hellip;\n- Updated Link\n- I found youtube.com/watch?v=yQ27DjKnxwo to be useful.\n- And in contrast to interpreted code, that begins running the bytecode or VM instructions immediately without delay, but will run the instructions slower than machine language.\n- A JIT is often used with interpreted code to convert it to machine language but yes, purely interpreted code (without any JITting) is slow. Even Java bytecode without a JITter is really slow.\n- The target doesn't have to be machine code, though. JRuby has a JIT compiler which will compile Ruby sourcecode to Java bytecode after a couple of invocations. Then, after another couple of invocations, the JVM JIT compiler kicks in and compiles the bytecode to native code.\n- Good example: the TraceMonkey engine. A JIT compiler for javascript now included in Firefox. wiki.mozilla.org/JavaScript:TraceMonkey\n- It is worth noting that, as alluded to by J&#246;rg, JIT is not necessarily invoked right away. Often, code will be interpreted until it is determined that it will be worth JITting. Since JITting can introduce delays, it may be faster to **NOT** JIT some code if it is rarely used and thus a fast response is more important than overall runtime.\n- @Mark Cidade: Have you got a link to an article, explaining JIT in more technical details (how it compiles to native byte code, how executes, memory model used, ...)?\n- @dma_k I imagine that the details can be different between platforms but you can start with Wikipedia, I guess: en.wikipedia.org/wiki/Just-in-time_compilation\n- can you explain *...\"after the program has started\"* ?\n- I updated the answer: \"Conventional compilers build the whole program as an EXE file BEFORE the first time you run it. For newer style programs, an assembly is generated with pseudocode (p-code). Only AFTER you execute the program on the OS (e.g., by double-clicking on its icon) will the (JIT) compiler kick in and generate machine code (m-code) that the Intel-based processor or whatever will understand.\"\n- If I'm a fanboy of anything, I'm a JS fanboy but this line suggests JITs are typically faster than pre-compiled, which I don't think is a feat anybody has pulled off yet: \"A JIT has access to dynamic runtime information whereas a standard compiler doesn't and can make better optimizations like inlining functions that are used frequently.\"\n- @ErikReppen: If a new machine comes out, compiling and optimizing a program for that new machine using a conventional compiler will likely yield results faster than a JIT. On the other hand, a JIT that's optimized for that new machine will be able to be able to optimize the performance of code *which was published before that new machine was invented*.\n- @supercat Couldn't you say the same about the source code of a conventionally compiled language? Or is it just that nobody would ever bother to re-compile an old offering for new hardware?\n- @ErikReppen: There isn't an awful lot of old software being recompiled for new hardware. Generally, software vendors prefer to push new software, but some old software can be nice as long as it runs. I miss my old DOS-based text editor (PC-Write), which Windows 7 effectively forced me to give up. QBasic was sometimes nice too for some simple one-off tasks. I don't think that I could very often finish a one-off task in QBASIC in less time than it takes to even launch Visual Studio, but in many cases it would be pretty close. QBasic, however, hasn't been recompiled in *ages*.\n- @supercat Have you tried DOSbox? I also like Scite when I've got those \"Visual Studio crashed because I typed too fast\" blues (and yes, in one environment I worked in, it would crash because I typed the CSS too fast. I'm guessing it was 25 real-time parsers in addition to all the ones I thought I'd turned off all jumping on the problem of helping me write the Razor, HTML, CSS, and JavaScript at the same time. Scite has color-coding. It loads. I've seen it crash once or twice and I'm not sure it was even the app's fault.\n- @ErikReppen: I've used PC Write occasionally in a VM, but the fact that VM drives are in a separate universe from everything else limits the usefulness.\n- @MarkCidade Can you please take a look at this question: stackoverflow.com/questions/28135312/&hellip; ?\n- Caution: \"A JIT compiler runs after the program has started and compiles the code...\" might be misleading without a conceptual understanding. It is right, although it does not mean that it runs *right after* running the program, in a separate step, as in JIT compilation being the first and separate step of running the program\n- This should not be the accepter answer as JIT doesn't happen right away, the JVM starts out interpreting and then once certain method reach the CompileThreshold they will be optimized and compiled to machine code by JIT.\n- What do you mean \"method level\"?\n- This is not correct, it is run by the interpreter, JIT will only kick in after the CompileThreshold is reached for the method in question\n- Why aren't compiled codes stored in somewhere in user's computer so the next time the application is run JIT doesn't have to recompile them again?\n- Good observations. It is possible to do this, but whether it actually beneficial depends on the platform and usage of the app. JIT optimisation is not necessarily the same as offline, or ahead of time optimisation so the benefit may only be 'not JITting' which may, or may not help much.\n- However this phrase *\"a Just-In-Time (JIT) compiler is a feature of the run-time interpreter\"* causes confusion; e.g. - stackoverflow.com/questions/16439512/&hellip;\n- Actually, the JIT was an add-on, and you can still disable it using the -Xint parameter to Java, so it's just a feature.\n- @CraigTrader - The Hotspot JIT compiler has been an inseparable part of Hotspot JVMs since Java 1.3. These days, calling it \"a feature of the interpreter\" is confusing. And the fact that you can disable JIT compilation is orthogonal.\n- I don't fully agree. JIT is not evolution - it is alternative of classic compilers.\n- JIT is one step on the evolutionary path from hard-wiring mechanical switches to specifying search criteria by saying \"OK Google\" to your smart phone. The current JIT available as part of Java 7/8 is leaps and bounds beyond what was available as part of Java 2 -- that's evolution as well.\n- @i486 - Sun / Oracle have (AFAIK) never shipped a classical (\"ahead of time\") compiler for Java that generates native code. It is a stretch to argue that JIT is an alternative ... when they thing it is supposedly an alternative for was never shipped. (I discount the GCJ AOT compiler because that was nothing to do with Sun / Oracle, and it wasn't a complete solution either. It is certainly non-viable now.)\n- Silicon Graphics had a native-code compiler for Java, though SGI is gone now.\n- That was a very long time ago. I can't find anything on the web past Java 1.1 releases for SGI.\n- The whole code isn't compiled when loaded into the JVM, as there's little information (read: guide) on how to go about the compilation. Keep in mind that performance is the ultimate goal. JIT is rather selective: monitoring and selecting the most popular methods for optimization. And it keeps doing this until maximum level of optimization has been reached for individual methods.\n- JIT offers some special advantages in frameworks with support for real generic types; it's possible to define a generic method which would be capable of producing an unbounded range of types, each of would require different machine code, but only have the JIT generate code for types which are actually produced. By contrast, in C++ it's necessary that the compiler generate code for all types a program will ever use.\n- The JVM doesn't JIT code the first time it runs it. The first few times, it interprets bytecode. Then, if that code runs often enough, it may decide to bother JITting it.\n- You are saying JIT in Java is JVM. However we already provide the compiled code to JVM, isn't it? Then it compiles it again you mean?\n- @KorayTugay - We provide Bytecodes to JVM and JVM will convert part of that to machine code on demand.so resources are saved.\n- @Duraiamuthan.H Could you please take a look at Can you please take a look at this question: stackoverflow.com/questions/28135312/&hellip; ?\n- In Java JIT is not JVM. It is just a part of it.\n- @happs - you are right JIT is part of JVM and it makes JVM efficient.\n- May you give an example of a method which is in machine code and another one which is not?\n- up-vote cuz of the last paragraph is really nice to tell new people to JIT at least 3 different JIT examples in the wild\n- @ninjalj I think it depends on the implementation, i.e. which JVM we're using, but anyways this says different from what you said. It says *\"JIT only occurs when the byte-code is executed for the first time.\"* What do you think about this?\n- Not sure why this was marked -1. I think the point here is that run time statistics are used to help optimize.\n- Yes, but the answer didn't phrase it like that. Literally, JIT does not optimize the hottest 20% of the code.\n- I would avoid answering question like this if you don't provide something new / better. If you get any reaction it's probably a downvote or criticism: Your answer is imprecise. \"JIT\" is not limited to a Java Virtual Machine, \"faster but uses more memory\" is a likely effect but not inherent to the JIT concept, and methods are often not compiled on the first invocation, rather after several when it becomes clear that spent time on JIT'ing is advantageous overall.\n- \"It is not necessary to compile the bytecode for 10 times again and again as the same instruction is going to execute for 10 times\" - what about a regular compiler? Does it compile this piece several times?\n- My interpretation of JIT is that it acts like memoization, where frequently used functions are 'stored' and the expense of compilation from java bytecode to native ISA-dependent code is bypassed. If this is correct, why doesn't java compile completely to native code from the start? This would reduce any kind of run-time compilation and make java 'native' to the machine?\n- Because it would delay the launch of the application. JIT enables fast startup and accelerates application execution. It's a tradeoff.\n- I didn't know bytecode is interpreted thankx for the info\n- I also checked the source, but I don't think this is the typical JVM implementation. It depends on the implementation, but this is not the usual modern JVM I think. Could somebody please clarify if this answer is correct or modern JVM is different?","metadata":{"transformedAt":"2026-08-18T18:32:17.739Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":89,"estimatedTokens":3130}}4{"id":"stack-2976630","source":"stackoverflow","questionId":2976630,"title":"How does Go compile so quickly?","tags":["performance","go","build","compiler-construction"],"text":"Title: How does Go compile so quickly?\nTags: performance, go, build, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI've Googled and poked around the Go website, but I can't find an explanation for Go's extraordinary build times. Are they products of the language features (or lack thereof), a highly optimized compiler, or something else? I'm not trying to promote Go; I'm just curious.\n\n========================================\n\nTop Answer:\nI think it's not that Go compilers are *fast*, it's that other compilers are *slow*.\n\nC and C++ compilers have to parse enormous amounts of headers - for example, compiling C++ \"hello world\" requires compiling 18k lines of code, which is almost half a megabyte of sources!\n\n```\n$ cpp hello.cpp | wc\n 18364 40513 433334\n```\n\nJava and C# compilers run in a VM, which means that before they can compile anything, the operating system has to load the whole VM, then they have to be JIT-compiled from bytecode to native code, all of which takes some time.\n\nSpeed of compilation depends on several factors.\n\nSome languages are designed to be compiled fast. For example, Pascal was designed to be compiled using a single-pass compiler.\n\nCompilers itself can be optimized too. For example, the Turbo Pascal compiler was written in hand-optimized assembler, which, combined with the language design, resulted in a really fast compiler working on 286-class hardware. I think that even now, modern Pascal compilers (e.g. FreePascal) are faster than Go compilers.\n\n========================================\n\nCode:\n```text\npackage main\nfunc main() {\n    var a int \n    a = 0\n}\nnotused.go:3: a declared and not used\n```\n\n```text\n$ cpp hello.cpp | wc\n  18364   40513  433334\n```\n\n========================================\n\nComments:\n- @Support, I'm aware of that. I think that implementing a compiler in such a way that it compiles with noticeable quickness is anything but premature optimization. More than likely, it represents the outcome of good software design and development practices. Also, I can't stand to see Knuth's words taken out of context and applied incorrectly.\n- The pessimist's version of this question is \"Why does C++ compile so slowly?\" stackoverflow.com/questions/588884/&hellip;\n- I voted to reopen this question as it is not opinion-based. One can give a good technical (non-opinionated) overview of language and/or compiler choices which facility compilation speed.\n- For small projects, Go seems slow to me. This is because I remember Turbo-Pascal being far far faster on a computer that was probably thousands of times slower. prog21.dadgum.com/47.html?repost=true. Every time I type \"go build\" and nothing happens for several seconds I think back to crusty old Fortran compilers and punched cards. YMMV. TLDR: \"slow\" and \"fast\" are relative terms.\n- Definitely recommend reading dave.cheney.net/2014/06/07/five-things-that-make-go-fast for more detailed insights\n- This phrase is not in the Go FAQ anymore, but a more detailed explanation of the \"dependency analysis\" topic comparing the C/C++ and Pascal/Modula/Go approach is available in the talk Go at Google\n- That's not true. You cannot fully parse Go source code without a symbol table.\n- I also don't see why garbage collection enhances compile times. It just doesn't.\n- These are quotes from the FAQ: golang.org/doc/go_faq.html I can't say if they failed to accomplish their goals (symbol table) or if their logic is faulty (GC).\n- @TheBlastOne concurrency and garbage collection are added to make development/code design faster, not compilation. While the previous paragraph helps provide a bit of context to make that clear, I agree that the wording is more than a little unclear.\n- @Atom Can you point me out a place where parsing without a symbol table is impossible in the grammar of Go?\n- @FUZxxl Go to golang.org/ref/spec#Primary_expressions and consider the two sequences [Operand, Call] and [Conversion]. Example Go source code: identifier1(identifier2). Without a symbol table it is impossible to decide whether this example is a call or conversion. | Any language can be to some extent parsed without a symbol table. It is true that most parts of Go source codes can be parsed without a symbol table, but it isn't true that it is possible to recognize all the grammar elements defined in golang spec.\n- @Atom Ah... I certainly missed that part.\n- Is it possible that the compiler needs a \"type table\" (of all the defined types), but not a \"symbol table\" (all the variables)? i.e. If the code doesn't define any new types, you won't need to store anything.\n- @Atom: It may be possible that semantically type casts *are* function calls. A function that returns its argument, with its type tag changed.\n- @Atom It's certainly possible to parse Go without a symbol table. There is no requirement that call and conversion be disambiguated during the parse. Simply parse both of them as a conversionOrCall and determine what the specific case is after parsing is complete.\n- @280Z28 How would you suggest to handle the following invalid piece of code: x:=aType. The parser cannot decide whether to print the error until it sees the symbol table.\n- @Atom You work hard to prevent the parser from ever being the piece of code that reports an error. Parsers generally do a poor job of reporting coherent error messages. Here, you create a parse tree for the expression as though `aType` is a variable reference, and later in the semantic analysis phase when you find out it's not you print a meaningful error at that time.\n- Microsoft's C# compiler does not run in a VM. It is still written in C++, primarily for performance reasons.\n- Turbo Pascal and later Delphi are the best examples for blazingly fast compilers. After the architect of both has migrated to Microsoft, we've seen vast improvements in both MS compilers, and languages. That's not a random coincidence.\n- How is 18k \"almost half of\" 1,000k (e.g. a megabyte)?\n- 18k lines (18364 to be exact) of code is 433334 bytes (~0,5MB)\n- The C# compiler has been compiled with C# since 2011. Just an update in case anyone reads this later.\n- The C# compiler and the CLR that runs the generated MSIL are different things however. I'm fairly certain the CLR is not written in C#.\n- `modern Pascal compilers (e.g. FreePascal) are faster than Go compilers.` <- No, they are not. Especially not free pascal. And especially not anything made by embarcadero. (we use both at work)\n- `go build test.go | wc` does not give any word count\n- @nurettin out of curiosity, just tested that freepascal can do compilation of simple files in under 100ms on my over 10 years old windows box. Go compilation takes about 700ms. I don't see compilers from other languages come even close to pascal compilation.\n- Point (4) is not entirely true. Modules that depend on each other should be compiled in order of dependency to allow for cross-module inlining and stuff.\n- @FUZxxl: This only concerns the optimization stage though, you can have perfect parallelism up to the backend IR generation; only cross-module optimization is thus concerned, which can be done at the link stage, and link is not parallel anyway. Of course, if you do not want to duplicate your work (re-parsing), you are better off compiling in a \"lattice\" way: 1/ modules with no dependency, 2/ modules depending only on (1), 3/ modules depending only on (1) and (2), ...\n- Which is perfectly easy to do using basic utilities such as a Makefile.\n- Go's compiler inlines small functions. I'm not sure how targeting a small number of CPUs makes you faster slower... I assume gcc isn't generating PPC code while I'm compiling for x86.\n- @BradFitzpatrick hate to resurrect an old comment but by targeting a smaller number of platforms developers of the compiler can spend more time optimizing it for each one.\n- using an intermediate form allows you to support a lot more architectures since now you only have to write a new backend for each new architecture\n- \"the optimizer works on the assembly code\" Assembly code sounds platform dependent, do they really have a separate optimizer for each supported platform?\n- @Mark my understanding is that they have a platform independent assembly language which they compile Go code into. Then they translate that into the architecture-specific instruction set. golang.org/doc/asm\n- @Student That sounds a lot like an \"internal representation\", which this answer claims Go doesn't do. Perhaps they were making some arbitrary distinction between IR and what Go is doing?\n- *A Pascal/Modula-2/Oberon/Oberon-2 compiler would compile itself in less than one second on a modern machine* [citation needed]\n- Citation added, see reference [1].\n- \"...principles ... finding their way into modern languages like Go (Google), Swift (Apple)\" Not sure how Swift made into that list: the Swift compiler is *glacial*. At a recent CocoaHeads Berlin meetup, someone provided some numbers for a mid-size framework, they came to 16 LOC per second.","metadata":{"transformedAt":"2026-08-18T18:32:17.739Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":86,"estimatedTokens":2250}}5{"id":"stack-44931479","source":"stackoverflow","questionId":44931479,"title":"Compiling vs Transpiling","tags":["compiler-construction","abstraction","transpiler"],"text":"Title: Compiling vs Transpiling\nTags: compiler-construction, abstraction, transpiler\nSource: Stack Overflow\n\nQuestion:\nWhile searching about the difference, I came across these definitions:\n\n**Compiling** is the general term for taking source code written in one language and transforming into another.\n\n**Transpiling** is a specific term for taking source code written in one language and transforming into another language that has a similar level of abstraction.\n\nI understand what Abstraction is.\n\nBut what does \"similar level of abstraction\" mean in the above definition? And how do we find the level of abstraction in a language?\n\n========================================\n\nTop Answer:\nHere's a sort of descriptive way to answer\n\nIf you think of layers of abstraction as this example:\n\n- CPU-level (actual logic gates on the CPU).\n\n- Machine code.\n\n- Assembly code.\n\n- C/C++, JVM/bytecode.\n\n- JavaScript, Python.\n\nA compiler goes to a lower level (lower number). A transpiler switches from one language (or version of a language) to another at the same number.\n\n========================================\n\nCode:\n```text\nclass\n```\n\n```text\nbreak\n```\n\n```text\nelm\n```\n\n```text\ntranspiling\n```\n\n========================================\n\nComments:\n- Is this the source? stevefenton.co.uk/2012/11/compiling-vs-transpiling\n- Curious, where that list of \"abstraction levels\" came from?\n- Just random illustrative examples, I know the first two are kinda mushy tbh, but I wanted a longer list :P\n- I like it and it would be really cool if it was some \"official\" classification like that. Since without it it's hard to put a tool (a translator) into one or another category. Eg: is `javac` a compiler or not.\n- `C&#47;C++, JVM&#47;bytecode` makes no sense, 4 should just be bytecode and C/C++ should be in 5. JVM is a runtime environment, not code.\n- byte code goes at the 5 level., not the 4 level.\n- \"Some examples of transpilers:\" --- babel calls itself a compiler. The separation between transpilers and compilers is really artificial.\n- @zerkms As I said, transpilers are a subset of compilers.\n- I'd like to add one thing, we are talking about natural language here. Thus fuzzy definitions is expected. I would expect the \"ease of reading\" the output of a transpiler to be similar to what it was before while a compiler makes things harder to read. Thus Webpack/npm is a compiler, you don't want to read it's output. (At least when you add the \"Loaders\" for e.g. .vue files.\n- Opinion: I posit that the definitions should be: Compilation: language -> lower level language. Transpilation: language -> same level language.\n- @Deji Not necessarily same level-language, but a language at a similar level of abstraction.\n- With regards to the answer by @tapananand there might be at least 2 models concluding all said above : - model A : superset is transpiled to its set (JSX by babel to vanilla JS) - model B : some set compiled to its subset (some programming codebase to machine code, *not mentioning intermediate representation [IR] for some virtual machine optimisation or relevant*)\n- There is the \"typescript compiler\" (or tsc) tool, its naming implies that TypeScript is *compiled* not *transpiled*... yet the tool converts TypeScript to Javascript, which are the same level of abstraction wrt the underlying hardware. Is TypeScript compiled, or transpiled?\n- @AlexMcMillan TypeScript is COMPILED, because it is a superset of javascript, and as such gets compiled down to javascript.\n- @Araymer Do you understand the difference between compilation and transpilation? Because TS is not compiled; nor is JS.\n- Yes, I do. And because TS is a superset of JS, it is considered a higher level of abstraction, and therefore is referred to as \"compiled\". The compiled JS then gets interpreted as normal. Hence why if you look up TS, the single most discussed topic is its \"compilation\". But, the line between transpilation and compilation doesn't have an official quantitative definition. It gets pretty mushy, so your opinion on what's compiled (differing from the creators, as it is) is just your opinion.\n- Microsoft writes that the TypeScript compiler transpiles TypeScript source code to JavaScript: code.visualstudio.com/docs/typescript/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.739Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":73,"estimatedTokens":1062}}6{"id":"stack-3265357","source":"stackoverflow","questionId":3265357,"title":"Compiled vs. Interpreted Languages","tags":["compiler-construction","programming-languages","interpreter"],"text":"Title: Compiled vs. Interpreted Languages\nTags: compiler-construction, programming-languages, interpreter\nSource: Stack Overflow\n\nQuestion:\nI'm trying to get a better understanding of the difference. I've found a lot of explanations online, but they tend towards the abstract differences rather than the practical implications.\n\nMost of my programming experiences has been with CPython (dynamic, interpreted), and Java (static, compiled). However, I understand that there are other kinds of interpreted and compiled languages. Aside from the fact that executable files can be distributed from programs written in compiled languages, are there any advantages/disadvantages to each type? Oftentimes, I hear people arguing that interpreted languages can be used interactively, but I believe that compiled languages can have interactive implementations as well, correct?\n\n========================================\n\nTop Answer:\nA language itself is neither compiled nor interpreted, only a specific implementation of a language is. Java is a perfect example. There is a bytecode-based platform (the JVM), a native compiler (gcj) and an interpeter for a superset of Java (bsh). So what is Java now? Bytecode-compiled, native-compiled or interpreted? \n\nOther languages, which are compiled as well as interpreted, are Scala, Haskell or Ocaml. Each of these languages has an interactive interpreter, as well as a compiler to byte-code or native machine code.\n\nSo generally categorizing languages by \"compiled\" and \"interpreted\" doesn't make much sense.\n\n========================================\n\nCode:\n```text\nRUN\n```\n\n========================================\n\nComments:\n- You chose exactly the worst languages for this comparison. Both are bytecompiled. The only real difference between them is the JITer, and even Python has a partial one (psyco).\n- A good example of an interactive compiled language is Clojure - everything is fully compiled (first to the JVM then to native code via the JIT). However a lot of the recompilation happens dynamically, and development is often done in an interactive REPL shell where you can evaluate any function you want in the running environment.\n- Standard ML is another interactive compiled language; the built-in compiler issues real native machine code too.\n- publib.boulder.ibm.com/infocenter/zos/basics/index.jsp?topic&zwnj;&#8203;=/&hellip;\n- Just because OP came up with two languages as examples doesn't make the question *about* either of them. Removed those tags. I could add the `language-agnostic` tag, but that's already implicit in `compiler-construction` and `programming-languages`.\n- C is not really tied to a \"machine\". The syntax and semantics of C are rather simple. It shouldn't be particularly difficult to implement a C-interpreter, only very time-consuming (because the standard library has to be implemented as well). And btw, Java can be compiled into native machine code (using gcj).\n- @lunaryorn: I disagree about GCJ. GCJ merely gives you an executable based environment. \"Compiled applications are linked with the GCJ runtime, libgcj, which provides the core class libraries, a garbage collector, and a bytecode interpreter\"\n- GCJ *does* produce native machine code, and not just an executable environment with embedded interpreter and bytecode. libgcj provides a bytecode interpreter to support calls from native code into Java bytecode, not to interpret the compiled program. If libgcj did not provide a bytecode interpreter, GCJ would not comply to the Java spec.\n- @lunaryorn: Ah. Ok, I appreciate the clarification and stand corrected. We primarily use Java in a windows environment so I haven't tried gcj in years.\n- You can interpret C.\n- Surely you can build a compiler for an interpreted language, but the compiled machine-code is itself a mirror of the runtime.\n- Not just a mirror of the runtime. E.g. imagine constructs like `eval()` in many scripting languages: You'd actually have to include the *compiler* in the resulting program, not just the runtime.\n- Not all compiled languages need a slow compilation stage. Serious Common Lisp implementations are compilers, and they often don't bother with an interpreter, preferring to just compile real fast on the fly. On the other hand, Java does need a compilation step, and it usually is visible.\n- @David completely agree! As I said I was generalising somewhat :-) I use Clojure quite a bit, which compiles on the fly (to the JVM) in exactly the same way.\n- @mikera great answer. Just one clarification regarding your point for interpreter being a more viable choice for dynamic languages. Why is implementing interpreter easy for dynamic languages like JavaScript?\n- @mikera If the interpreter does the following: 1- takes the non-native code. 2- calls the appropriate function on that code which is, not surprisingly, calls the appropriate native function. On the other hand the JIT compilation does the following: 1- takes the non-native code. 2- compiles it to an intermediate language which is, as I imagine, translated to the native machine code somehow and executed. then what's real advantages of the JIT compilation over interpreting?\n- @Kareem: the JIT compiler only does 1) and 2) *once* - after that it is native code all the way. The interpreter needs to do both 1) and 2) every time the code is called (which may be many, many times...). So over time, the JIT compiler wins by a long margin.\n- @mikera Isn't bytecode translated into machine code during execution? If not, then when exactly is it translated to machine code?\n- Yes bytecode is translated to machine code *at some point* during the overall program execution (as opposed to before program execution, as is the case with a traditional compiler). But a given piece of code might be executed 10 million+ times during overall program execution. It (probably) only gets compiled *once* from bytecode to machine code. Hence the runtime overhead of JIT is small, and can be ignored for long-running programs. After the JIT compiler has finished doing its job, you will effectively be running pure machine code all the way.\n- This is actually a false dichotomy. There is nothing intrinsic to a language that makes it compiled our interpreted. It is nothing more than a widely held misconception. Many languages have both implementations and all languages can have either.\n- @mmachenry it is not a false dichotomy. \"programming language\" includes both design and implementation. While in an *theoretical sense* a given language definition can be both compiled and interpreted, in *real world practice* there are considerable differences in implementation. Nobody has yet solved how to effectively compile certain language constructs, for example - it is an open research problem.\n- @mikera Indeed, it is. There are benefits to compiling and there are benefits to interpreting. Just because compiler technology is developing to improve on certain languages features don't mean we can say anything about the benefits of compiling a language with that feature. Conflating language and implementation causes us to have false understandings of choosing compilation or interpretation for an implementation. For example your comment \"[interpreters] Can be more convenient for dynamic languages\"\n- Add basic syntax checking of the whole program to the list for compilers.\n- So basically, with interpreted languages, there is a layer between the machine and the code you write, whereas a compiled language does not have a layer of indirection.?\n- `Opportunity to apply quite powerful optimizations during the compile stage` So does this mean that the same source file can produce different object files depending on how it is compiled? I.e. the statements in a particular source file and the machine instructions from a resulting compilation do not necessarily have a one-to-one relationship, but perhaps a one-to-many relationship?\n- The interpreter itself is compiled. Right? Because at the very end, you need machine code. Right?\n- However, C++'s compilation model is inherited from C and was designed without consideration of features such as templates. This awkwardness contributes to C++'s long compile times much more than any other factor – and makes it a poor example.\n- I agree. Or let's say: There are native compilers (creating machine code for the CPU to eat), and not-so-native-compilers (creating tokenized stuff, i.e. intermediate code, that some just-in-time compiler compiles to machine code before (or during) runtime ONCE), and there are \"real\" non-compilers that never produce machine code and never let the CPU run the code. The latter are interpreters. Today, native compilers which directly produce machine (CPU) code at compile-time are becoming more and more rare. Delphi/Codegear is one of the best survivors.\n- on this terms java can't be considered a \"compiled language\", but it's compile phase give the advantages of compiling (type checking, early error detection, etc.), and produce bytecode that can be run on every OS, with a Java virtual machine provided.\n- Can you fix this sentence: \"There are compilers that translate other languages to JavaScript, which is then often run in a web browser, which might interpret the JavaScript, or compile it a virtual machine or native code.\"\n- Nailed it. Another common mistake is to attribute the usefulness of a language to its existing APIs.","metadata":{"transformedAt":"2026-08-18T18:32:17.739Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":59,"estimatedTokens":2350}}7{"id":"stack-93039","source":"stackoverflow","questionId":93039,"title":"Where are static variables stored in C and C++?","tags":["c++","c","compiler-construction"],"text":"Title: Where are static variables stored in C and C++?\nTags: c++, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn what segment (.BSS, .DATA, other) of an executable file are static variables stored so that they don't have name collision?\nFor example:\n\n```\nfoo.c: bar.c:\nstatic int foo = 1; static int foo = 10;\nvoid fooTest() { void barTest() {\n static int bar = 2; static int bar = 20;\n foo++; foo++;\n bar++; bar++;\n printf(\"%d,%d\", foo, bar); printf(\"%d, %d\", foo, bar);\n} }\n```\n\nIf I compile both files and link it to a main that calls fooTest() and barTest repeatedly, the printf statements increment independently. Makes sense since the foo and bar variables are local to the translation unit.\n\nBut where is the storage allocated?\n\nTo be clear, the assumption is that you have a toolchain that would output a file in ELF format. Thus, I *believe* that there **has** to be some space reserved in the executable file for those static variables.\n\nFor discussion purposes, lets assume we use the GCC toolchain.\n\n========================================\n\nTop Answer:\nWhen a program is loaded into memory, it’s organized into different segments. One of the segment is **DATA segment**. The Data segment is further sub-divided into two parts:\n\n- **Initialized data segment:** All the global, static and constant data are stored here.\n\n- **Uninitialized data segment (BSS):** All the uninitialized data are stored in this segment.\n\nHere is a diagram to explain this concept:\n\nHere is very good link explaining these concepts: Memory Management in C: The Heap and the Stack\n\n========================================\n\nCode:\n```text\nfoo.c:                         bar.c:\nstatic int foo = 1;            static int foo = 10;\nvoid fooTest() {               void barTest() {\n  static int bar = 2;            static int bar = 20;\n  foo++;                         foo++;\n  bar++;                         bar++;\n  printf(\"%d,%d\", foo, bar);     printf(\"%d, %d\", foo, bar);\n}                              }\n```\n\n```text\nDATA\n```\n\n```none\nMemory Area     Characteristics and Object Lifetimes\n     --------------  ------------------------------------------------\n\n     Const Data      The const data area stores string literals and\n                     other data whose values are known at compile\n                     time.  No objects of class type can exist in\n                     this area.  All data in this area is available\n                     during the entire lifetime of the program.\n\n                     Further, all of this data is read-only, and the\n                     results of trying to modify it are undefined.\n                     This is in part because even the underlying\n                     storage format is subject to arbitrary\n                     optimization by the implementation.  For\n                     example, a particular compiler may store string\n                     literals in overlapping objects if it wants to.\n\n\n     Stack           The stack stores automatic variables. Typically\n                     allocation is much faster than for dynamic\n                     storage (heap or free store) because a memory\n                     allocation involves only pointer increment\n                     rather than more complex management.  Objects\n                     are constructed immediately after memory is\n                     allocated and destroyed immediately before\n                     memory is deallocated, so there is no\n                     opportunity for programmers to directly\n                     manipulate allocated but uninitialized stack\n                     space (barring willful tampering using explicit\n                     dtors and placement new).\n\n\n     Free Store      The free store is one of the two dynamic memory\n                     areas, allocated/freed by new/delete.  Object\n                     lifetime can be less than the time the storage\n                     is allocated; that is, free store objects can\n                     have memory allocated without being immediately\n                     initialized, and can be destroyed without the\n                     memory being immediately deallocated.  During\n                     the period when the storage is allocated but\n                     outside the object's lifetime, the storage may\n                     be accessed and manipulated through a void* but\n                     none of the proto-object's nonstatic members or\n                     member functions may be accessed, have their\n                     addresses taken, or be otherwise manipulated.\n\n\n     Heap            The heap is the other dynamic memory area,\n                     allocated/freed by malloc/free and their\n                     variants.  Note that while the default global\n                     new and delete might be implemented in terms of\n                     malloc and free by a particular compiler, the\n                     heap is not the same as free store and memory\n                     allocated in one area cannot be safely\n                     deallocated in the other. Memory allocated from\n                     the heap can be used for objects of class type\n                     by placement-new construction and explicit\n                     destruction.  If so used, the notes about free\n                     store object lifetime apply similarly here.\n\n\n     Global/Static   Global or static variables and objects have\n                     their storage allocated at program startup, but\n                     may not be initialized until after the program\n                     has begun executing.  For instance, a static\n                     variable in a function is initialized only the\n                     first time program execution passes through its\n                     definition.  The order of initialization of\n                     global variables across translation units is not\n                     defined, and special care is needed to manage\n                     dependencies between global objects (including\n                     class statics).  As always, uninitialized proto-\n                     objects' storage may be accessed and manipulated\n                     through a void* but no nonstatic members or\n                     member functions may be used or referenced\n                     outside the object's actual lifetime.\n```\n\n```text\nstatic\n```\n\n```text\nstorage     :   where is it stored, for example data, stack, heap...\nscope       :   who can see us, for example global, local...\ntype        :   what is our type, for example int, int*...\naddress     :   where are we located\nvalue       :   what is our value\n```\n\n```text\nvoid main(void)\n{\nstatic int i;\n}\n```\n\n```text\nvoid main(void)\n{\nstatic int i=10;\n}\n```\n\n```text\n(gdb) disas fooTest\nDump of assembler code for function fooTest:\n   0x000000000040052d <+0>: push   %rbp\n   0x000000000040052e <+1>: mov    %rsp,%rbp\n   0x0000000000400531 <+4>: mov    0x200b09(%rip),%eax        # 0x601040 <foo>\n   0x0000000000400537 <+10>:    add    $0x1,%eax\n   0x000000000040053a <+13>:    mov    %eax,0x200b00(%rip)        # 0x601040 <foo>\n   0x0000000000400540 <+19>:    mov    0x200afe(%rip),%eax        # 0x601044 <bar.2180>\n   0x0000000000400546 <+25>:    add    $0x1,%eax\n   0x0000000000400549 <+28>:    mov    %eax,0x200af5(%rip)        # 0x601044 <bar.2180>\n   0x000000000040054f <+34>:    mov    0x200aef(%rip),%edx        # 0x601044 <bar.2180>\n   0x0000000000400555 <+40>:    mov    0x200ae5(%rip),%eax        # 0x601040 <foo>\n   0x000000000040055b <+46>:    mov    %eax,%esi\n   0x000000000040055d <+48>:    mov    $0x400654,%edi\n   0x0000000000400562 <+53>:    mov    $0x0,%eax\n   0x0000000000400567 <+58>:    callq  0x400410 <printf@plt>\n   0x000000000040056c <+63>:    pop    %rbp\n   0x000000000040056d <+64>:    retq   \nEnd of assembler dump.\n\n(gdb) disas barTest\nDump of assembler code for function barTest:\n   0x000000000040056e <+0>: push   %rbp\n   0x000000000040056f <+1>: mov    %rsp,%rbp\n   0x0000000000400572 <+4>: mov    0x200ad0(%rip),%eax        # 0x601048 <foo>\n   0x0000000000400578 <+10>:    add    $0x1,%eax\n   0x000000000040057b <+13>:    mov    %eax,0x200ac7(%rip)        # 0x601048 <foo>\n   0x0000000000400581 <+19>:    mov    0x200ac5(%rip),%eax        # 0x60104c <bar.2180>\n   0x0000000000400587 <+25>:    add    $0x1,%eax\n   0x000000000040058a <+28>:    mov    %eax,0x200abc(%rip)        # 0x60104c <bar.2180>\n   0x0000000000400590 <+34>:    mov    0x200ab6(%rip),%edx        # 0x60104c <bar.2180>\n   0x0000000000400596 <+40>:    mov    0x200aac(%rip),%eax        # 0x601048 <foo>\n   0x000000000040059c <+46>:    mov    %eax,%esi\n   0x000000000040059e <+48>:    mov    $0x40065c,%edi\n   0x00000000004005a3 <+53>:    mov    $0x0,%eax\n   0x00000000004005a8 <+58>:    callq  0x400410 <printf@plt>\n   0x00000000004005ad <+63>:    pop    %rbp\n   0x00000000004005ae <+64>:    retq   \nEnd of assembler dump.\n```\n\n```text\nDisassembly of section .data:\n\n0000000000601030 <__data_start>:\n    ...\n\n0000000000601038 <__dso_handle>:\n    ...\n\n0000000000601040 <foo>:\n  601040:   01 00                   add    %eax,(%rax)\n    ...\n\n0000000000601044 <bar.2180>:\n  601044:   02 00                   add    (%rax),%al\n    ...\n\n0000000000601048 <foo>:\n  601048:   0a 00                   or     (%rax),%al\n    ...\n\n000000000060104c <bar.2180>:\n  60104c:   14 00                   adc    $0x0,%al\n```\n\n```text\n#include <stdio.h>\n\nint f() {\n    static int i = 1;\n    i++;\n    return i;\n}\n\nint main() {\n    printf(\"%d\\n\", f());\n    printf(\"%d\\n\", f());\n    return 0;\n}\n```\n\n```text\ngcc -ggdb -c main.c\n```\n\n```text\nobjdump -Sr main.o\n```\n\n```text\nstatic int i = 1;\n i++;\n4:  8b 05 00 00 00 00       mov    0x0(%rip),%eax        # a <f+0xa>\n        6: R_X86_64_PC32    .data-0x4\n```\n\n```text\nobjdump -Sr\n```\n\n```text\n-S\n```\n\n```text\n-r\n```\n\n```text\nf\n```\n\n```text\n.data-0x4\n```\n\n```text\n.data\n```\n\n```text\n-0x4\n```\n\n```text\n%rip\n```\n\n```text\nR_X86_64_PC32\n```\n\n```text\n00 00 00 00\n```\n\n```text\ni = 1\n```\n\n```text\nstatic int i = 0\n```\n\n```text\n.bss\n```\n\n```text\nstatic int i = 1\n```\n\n```text\n.data\n```\n\n```cpp\n// demo.cpp (compile this file with 'g++ demo.cpp')\nstatic int s_null;  // uninitialized\nstatic int s_0 = 0; // zero-initialized\nstatic int s_1 = 1; // initialized with non-zero value\n\nint main() {}\n```\n\n```text\n$ objdump -C -t a.out | grep \" s_\"\n0000000000601034 l     O .bss   0000000000000004              s_null\n0000000000601038 l     O .bss   0000000000000004              s_0\n000000000060102c l     O .data  0000000000000004              s_1\n\n// objdump option explained\n-C : Demangle symbol names\n-t : Print the symbol table entries\n```\n\n```text\n$ objdump -C -j .data -j .bss -dz a.out \n\na.out:     file format elf64-x86-64\n\n\nDisassembly of section .data:\n\n0000000000601028 <__data_start>:\n  601028:       00 00                   add    %al,(%rax)\n  60102a:       00 00                   add    %al,(%rax)\n\n000000000060102c <s_1>: // OK, s_1 is here, with initial value of 0x01\n  60102c:       01 00 00 00                                         ....\n\nDisassembly of section .bss:\n\n0000000000601030 <__bss_start>:\n  601030:       00 00                   add    %al,(%rax)\n  601032:       00 00                   add    %al,(%rax)\n\n0000000000601034 <s_null>:\n  601034:       00 00 00 00                                         ....\n\n0000000000601038 <s_0>:\n  601038:       00 00 00 00 00 00 00 00                             ........\n```\n\n```text\n.bss\n```\n\n```text\n.data\n```\n\n```text\n.data\n```\n\n```text\n.bss\n```\n\n```text\n$ objdump -t -C a.out\n\na.out:     file format mach-o-x86-64\n\nSYMBOL TABLE:\n0000000100001008 l       0e SECT   03 0000 [.data] main.bar\n0000000100001018 l       0e SECT   03 0000 [.data] swap.bar\n\n$ objdump -s -C -j .data a.out\n\na.out:     file format mach-o-x86-64\n\nContents of section .data:\n 100001000 01000000 02000000 58000000 00000000  ........X.......\n 100001010 00100000 01000000 59000000           ........Y...\n```\n\n```text\nbar\n```\n\n```text\n88\n```\n\n```text\n89\n```\n\n```text\nmain.c\n```\n\n```text\nswap.c\n```\n\n```text\na.out\n```\n\n```text\n58000000\n```\n\n```text\n59000000\n```\n\n```text\nbar\n```\n\n========================================\n\nComments:\n- Most people are telling you that they should be stored in .DATA section instead of answering your question: where exactly in the .DATA section and how can you find where. I see you already marked an answer, so you already know how to find it?\n- why initialised and uninitialised are placed in different sections : linuxjournal.com/article/1059\n- The storage allocated to your global/static variables at runtime has nothing to do with their name resolution, which happens during build/link time. After the executable has been built - there're no more names.\n- This question is meaningless, being built on the false premise that \"name collision\" of unexported symbols is a thing that can exist. The fact that there's no legitimate question might explain how dire some of the answers are. It's hard to believe so few people got this.\n- no. static keyworld has overloaded meanings: in such a case static is storage modifier, not linkage modifier.\n- ugasoft: the statics outside the function are linkage modifiers, inside are storage modifiers where there can be no collision to start with.\n- @paxdiablo: you have mentioned two types of static variables. Which one of them does this article (en.wikipedia.org/wiki/Data_segment ) refer to? Data segment also holds the global variables (which are exactly opposite in nature to static ones). `So, how does a segment of memory (Data Segment) store variables that can be accessed from everywhere (global variables) and also those which have limited scope (file scope or function scope in case of static variables)?`\n- @eSKay, it haas to do with visibility. There can be things stored in a segment which are local to a compilation unit, others which are fully accessible. One example: think of each comp-unit contributing a block to the DATA segment. It knows where *everything* is in that block. It also publishes the addresses of those things in the block that it wishes other comp-units to have access to. The linker can resolve those addresses at link time.\n- By \"non-0 initialized\" you probably mean \"initialized, but with something other than 0\". Because there's no such thing as \"non initialized\" static data in C/C++. Everything static is zero-initialized by default.\n- @Don Neufeld: your answer does not answer the question at all. I do not understand why it is accepted. Because the both the 'foo' and 'bar' are non-0 initialized. The question is where to place two static/global variable with the same name in .bss or .data\n- I have used implementations where static data that was explicitly zero-initialized went in `.data`, and static data with no initializer went in `.bss` .\n- @M.M In my case whether static member is uninitialized (implicitly initialized to 0 ) or explicitly initialized to 0, in both cases it added up in .bss section.\n- Is this info specific to a certain executable file type? I assume, since you didn't specify, that it applies at least to ELF and Windows PE executable files, but what about other types?\n- -1 for inaccurate comment - uninitialized data does NOT go into DATA. Uninitialized and zero-initialized data go into BSS section.\n- +1 for thorough categorization at high level. It would be great if you could also point to the source(s) of this info.\n- The answer above says 0 initialized goes into BSS. Does 0 initialized mean uninitialized or 0 per se ? If it means 0 per se then I think you should include it in your answer.\n- Constant data are not stored in .data segment but within .const segment of the text section.\n- Instead of this (\"**Initialized data segment**: All the global, static and constant data are stored here. **Uninitialized data segment(BSS)**: All the uninitialized data are stored in this segment.\"), I think it should say this: (\"**Initialized data segment**: All the global & static variables that were initialized to a non-zero value, and all constant data, are stored here. **Uninitialized data segment(BSS)**: All the global and static variables that were either NOT initialized, or initialized to zero, are stored in this segment.\").\n- Also note that as far as I understand it, \"initialized data\" can consist of initialized **variables** *and* **constants**. On a microcontroller (ex: STM32), **Initialized variables** are stored by default in *Flash* memory and *copied to RAM at startup*, and **initialized constants** are left in, and intended to be read from, *Flash only*, along with the *text*, which contains the program itself, and is left in *Flash only.*\n- So what I'm gathering from this diagram is that variables which are global or static (since static variables act like global variables in duration) are neither on the heap *nor* the stack, but are rather allocated on memory apart from both of those. Is that right? I suppose I could take a look at an STM32 linker script again to study the memory allocation more too.\n- More reading here--this also contains a very similar memory layout diagram image: geeksforgeeks.org/memory-layout-of-c-program/amp.\n- Linked pdf is available at github.com/margam2410/Documents/blob/master/teoX.pdf\n- +1 for @GabrielStaples for highlighting the fact that initialized data can be further classified into read-only (=> .rodata section) and read-write (=> .data section).\n- @ZeZNiQ, thanks. For more details on this I've written some pretty detailed answers on STM32 microcontroller memory layout here (1. Electronics Stack Exchange: How do I find out at compile time how much of an STM32's Flash memory and dynamic memory (SRAM) is used up?) and here (2. Convert binutils `size` output from \"sysv\" format (`size --format=sysv my_executable`) to \"berkeley\" format (`size --format=berkeley my_executable`)).\n- There is much much more than that. Even existing answers are not complete. Just to mention something else: thread locals.","metadata":{"transformedAt":"2026-08-18T18:32:17.740Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":45,"totalLines":467,"estimatedTokens":4502}}8{"id":"stack-193560","source":"stackoverflow","questionId":193560,"title":"Writing a compiler in its own language","tags":["compiler-construction","bootstrapping"],"text":"Title: Writing a compiler in its own language\nTags: compiler-construction, bootstrapping\nSource: Stack Overflow\n\nQuestion:\nIntuitively, it would seems that a compiler for language `Foo` cannot itself be written in Foo. More specifically, the *first* compiler for language `Foo` cannot be written in Foo, but any subsequent compiler could be written for `Foo`.\n\nBut is this actually true? I have some very vague recollection of reading about a language whose first compiler was written in \"itself\". Is this possible, and if so how?\n\n========================================\n\nTop Answer:\nI recall listening to a Software Engineering Radio podcast wherein Dick Gabriel spoke about bootstrapping the original LISP interpreter by writing a bare-bones version in LISP **on paper** and hand assembling it into machine code. From then on, the rest of the LISP features were both written in and interpreted with LISP.\n\n========================================\n\nCode:\n```text\nFoo\n```\n\n```text\nFoo\n```\n\n```text\nFoo\n```\n\n```text\nmake bootstrap\n```\n\n```text\nReflection.Emit\n```\n\n```text\n...\nif (c == 92) { // backslash\n    c = getc();\n    if (c == 110) { // n\n        return 10;\n    } else if (c == 92) { // another backslash\n        return 92;\n    } else {\n        ...\n    }\n}\n...\n```\n\n```text\n...\nif (c == '\\\\') {\n    c = getc();\n    if (c == 'n') {\n        return '\\n';\n    } else if (c == '\\\\') {\n        return '\\\\';\n    } else {\n        ...\n    }\n}\n...\n```\n\n```text\nvoid compileFunction(char * name, char * filename, char * code) {\n    if (strcmp(\"compileFunction\", name) == 0 && strcmp(\"compile.c\", filename) == 0) {\n        code = A;\n    } else if (strcmp(\"xxx\", name) == 0 && strcmp(\"yyy.c\", filename) == 0) {\n        code = B;\n    }\n\n    ... code to compile the function body from the string in \"code\" ...\n}\n```\n\n```text\n\\n\n```\n\n```text\n\\r\n```\n\n```text\ncompileFunction\n```\n\n```text\nADD\n    /   \\\n  MPY     3\n /   \\\n5     x\n```\n\n```text\n[ADD,[MPY,5,x],3]\n```\n\n```text\nADD[MPY[5,x],3]\n```\n\n```text\n<NAME>(<unparse>)=><action>;\n      (<unparse>)=><action>;\n            ...\n      (<unparse>)=><action>;\n```\n\n```text\nexpr_gen(ADD[expr_gen(x),expr_gen(y)])=> x+y;\n```\n\n```text\nexpr_gen(#node[expr_gen(x),expr_gen(y)])=> #action;\n\n  node:   ADD, SUB, MPY, DIV;\n  action: x+y, x-y, x*y, x/y;\n\n        (NUMBER(x))=> x;\n        (SYMBOL(x))=> val:(x);\n```\n\n```text\n.MACHOP #opnm register,@indirect offset (index): // Instruction's parameters.\n.MORG 36, O(18): $/36; // Align to 36 bit boundary print format: 18 bit octal $/36\nO(9):  #opcd;          // Op code 9 bit octal print out\n (4):  register;       // 4 bit register field appended print\n (1):  indirect;       // 1 bit appended print\n (4):  index;          // 4 bit index register appended print\nO(18): if (#opcd&&3==1) offset // immediate mode use value else\n       else offset/36;         // memory address divide by 36\n                               // to get word address.\n// Vectored entry opcode table:\n#opnm := MOVE, MOVEI, MOVEM, MOVES, MOVS, MOVSI, MOVSM, MOVSS,\n         MOVN, MOVNI, MOVNM, MOVNS, MOVM, MOVMI, MOVMM, MOVMS,\n         IMUL, IMULI, IMULM, IMULB, MUL,  MULI,  MULM,  MULB,\n                           ...\n         TDO,  TSO,   TDOE,  TSOE,  TDOA, TSOA,  TDON,  TSON;\n// corresponding opcode value:\n#opcd := 0O200, 0O201, 0O202, 0O203, 0O204, 0O205, 0O206, 0O207,\n         0O210, 0O211, 0O212, 0O213, 0O214, 0O215, 0O216, 0O217,\n         0O220, 0O221, 0O222, 0O223, 0O224, 0O225, 0O226, 0O227,\n                           ...\n         0O670, 0O671, 0O672, 0O673, 0O674, 0O675, 0O676, 0O677;\n```\n\n```text\n400020 201082 000005            MOVEI r1,5(r2)\n```\n\n```text\n<name> <formula type operator> <expression> ;\n```\n\n```text\n/*  Character Class Formula                                    class_mask */\nbin: '0'|'1';                                                // 0b00000010\noct: bin|'2'|'3'|'4'|'5'|'6'|'7';                            // 0b00000110\ndgt: oct|'8'|'9';                                            // 0b00001110\nhex: dgt|'A'|'B'|'C'|'D'|'E'|'F'|'a'|'b'|'c'|'d'|'e'|'f';    // 0b00011110\nupr:  'A'|'B'|'C'|'D'|'E'|'F'|'G'|'H'|'I'|'J'|'K'|'L'|'M'|\n      'N'|'O'|'P'|'Q'|'R'|'S'|'T'|'U'|'V'|'W'|'X'|'Y'|'Z';   // 0b00100000\nlwr:  'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i'|'j'|'k'|'l'|'m'|\n      'n'|'o'|'p'|'q'|'r'|'s'|'t'|'u'|'v'|'w'|'x'|'y'|'z';   // 0b01000000\nalpha:  upr|lwr;                                             // 0b01100000\nalphanum: alpha|dgt;                                         // 0b01101110\n```\n\n```text\ntest    byte ptr [eax+_classmap],dgt\n```\n\n```text\njne      <success>\n```\n\n```text\nje       <failure>\n```\n\n```text\nstring .. (''' .ANY ''' | '\"' $(-\"\"\"\" .ANY | \"\"\"\"\"\",\"\"\"\") '\"') MAKSTR[];\n```\n\n```text\n-\"\"\"\" .ANY\n```\n\n```text\n\"\"\"\"\"\",\"\"\"\"\n```\n\n```text\nnumber .. \"0B\" bin $bin MAKBIN[]        // binary integer\n         |\"0O\" oct $oct MAKOCT[]        // octal integer\n         |(\"0H\"|\"0X\") hex $hex MAKHEX[] // hexadecimal integer\n// look for decimal number determining if integer or floating point.\n         | ('+'|+'-'|--)                // only - matters\n           dgt $dgt                     // integer part\n           ( +'.' $dgt                  // fractional part?\n              ((+'E'|'e','E')           // exponent  part\n               ('+'|+'-'|--)            // Only negative matters\n               dgt(dgt(dgt|--)|--)|--)  // 1 2 or 3 digit exponent\n             MAKFLOAT[] )               // floating point\n           MAKINT[];                    // decimal integer\n```\n\n```text\n(a b | c d)\\ e\n```\n\n```text\n:<node name> creates a node object and pushes it onto the node stack.\n..           Token formula create token objects and push them onto \n             the parse stack.\n!<number>    pops the top node object and top <number> of parstack \n             entries into a list representation of the tree. The \n             tree then pushed onto the parse stack.\n+[ ... ]+    creates a list of the parse stack entries created \n             between them:\n              '(' +[argument $(',' argument]+ ')'\n             could parse an argument list. into a list.\n```\n\n```text\nExp = Term $(('+':ADD|'-':SUB) Term!2); \nTerm = Factor $(('*':MPY|'/':DIV) Factor!2);\nFactor = ( number\n         | id  ( '(' +[Exp $(',' Exp)]+ ')' :FUN!2\n               | --)\n         | '(' Exp ')\" )\n         (^' Factor:XPO!2 |--);\n```\n\n```text\nd^(x+5)^3-a+b*c => ADD[SUB[EXP[EXP[d,ADD[x,5]],3],a],MPY[b,c]]\n\n              ADD\n             /   \\\n          SUB     MPY\n         /   \\   /   \\\n      EXP     a b     c\n     /   \\\n    d     EXP     \n         /   \\\n      ADD     3\n     /   \\\n    x     5\n```\n\n```text\nprogram = $((declaration            // A program is a sequence of\n                                    // declarations terminated by\n            |.EOF .STOP)            // End Of File finish & stop compile\n           \\                        // Backtrack: .EOF failed or\n                                    // declaration long-failed.\n             (ERRORX[\"?Error?\"]     // report unknown error\n                                    // flagging furthest parse point.\n              $(-';' (.ANY          // find a ';'. skiping .ANY\n                     | .STOP))      // character: .ANY fails on end of file\n                                    // so .STOP ends the compile.\n                                    // (-';') failing breaks loop.\n              ';'));                // Match ';' and continue\n\ndeclaration =  \"#\" directive                // Compiler directive.\n             | comment                      // skips comment text\n             | global        DECLAR[*1]     // Global linkage\n             |(id                           // functions starting with an id:\n                ( formula    PARSER[*1]     // Parsing formula\n                | sequencer  GENERATOR[*1]  // Code generator\n                | optimizer  ISO[*1]        // Optimizer\n                | pseudo_op  PRODUCTION[*1] // Pseudo instruction\n                | emitor_op  MACHOP[*1]     // Machine instruction\n                )        // All the above start with an identifier\n              \\ (ERRORX[\"Syntax error.\"]\n                 garbol);                    // skip over error.\n```\n\n```text\nformula =   (\"==\" syntax  :BCKTRAK   // backtrack grammar formula\n            |'='  syntax  :SYNTAX    // grammar formula.\n            |':'  chclass :CLASS     // character class define\n            |\"..\" token   :TOKEN     // token formula\n              )';' !2                // Combine node name with id \n                                     // parsed in calling declaration \n                                     // formula and tree produced\n                                     // by the called syntax, token\n                                     // or character class formula.\n                $(-(.NL |\"/*\") (.ANY|.STOP)); Comment ; to line separator?\n\nchclass = +[ letter $('|' letter) ]+;// a simple list of character codes\n                                     // except \nletter  = char | number | id;        // when including another class\n\nsyntax  = seq ('|' alt1|'\\' alt2 |--);\n\nalt1    = seq:ALT!2 ('|' alt1|--);  Non-backtrack alternative sequence.\n\nalt2    = seq:BKTK!2 ('\\' alt2|--); backtrack alternative sequence\n\nseq     = +[oper $oper]+;\n\noper    = test | action | '(' syntax ')' | comment; \n\ntest    = string | id ('[' (arg_list| ,NILL) ']':GENCALL!2|.EMPTY);\n\naction  = ':' id:NODE!1\n        | '!' number:MAKTREE!1\n        | \"+[\"  seq \"]+\" :MAKLST!1;\n\n//     C style comments\ncomment  = \"//\" $(-.NL .ANY)\n         | \"/*\" $(-\"*/\" .ANY) \"*/\";\n```\n\n========================================\n\nComments:\n- Possible duplicate of Bootstrapping still requires outside support\n- This is a very old question, but say I wrote an interpreter for language Foo in Java. Then with the language foo, I wrote it's own interpreter. Foo would still require the JRE right?\n- You *could* write the first `Foo` compiler in `Foo` itself. Your source code would be a `Foo` program with `Foo` instructions for how to generate machine code (or, in more modern terms, some other backend code) given a `Foo` source code input. Now, you would need something *or someone* that understands `Foo`'s specification well enough to trace out the correct output of that program by hand, run on itself. As far as I know, however, precisely what I'm describing has never actually been done with any language, for obvious reasons.\n- Maybe a stupid question: If you want to port your compiler to another architecture of microprocessor the bootstrapping should restart from a working compiler for that architecture. Is this right? If this is right this means that is better to keep the first compiler as it could be useful to port your compiler to other architectures (especially if is written in some 'universal language' like C)?\n- @piertoni it'd typically be easier to just retarget the compiler backend to the new microprocessor.\n- Use LLVM as backend, for example\n- You can indeed (it isn't that difficult either), but its only practical application would be in a parser generator.\n- Indeed I used that very method to produce the LIME parser generator. A restricted, simplified, tabular representation of the metagrammar goes through a simple recursive-descent parser. Then, LIME generates a parser for the language of grammars, and then it uses that parser to read the grammar someone is actually interested in generating a parser for. This means I don't have to know how to write what I just wrote. It feels like magic.\n- Actually you can't, as BNF can't describe itself. You need a variant such as that used in *yacc* where the non-terminal symbols aren't quoted.\n- You can not use bnf to define bnf as can not be recognized. EBNF fixed that by quoting constant string tokens of the language.\n- Well, technically you *could* just compile your starting source code by hand. Do you understand C well enough to be able to read some C source code and through it by hand and determine what its output is? A `foo` compiler written in `foo` is just another `foo` program, whose outputs happen in this case to be machine code or other backend code. Theoretically speaking, you could start writing your first `foo` compiler in `foo` itself, if you're confident enough you can correctly deduce from the specification what the output should be, and have patience to trace through it by hand.\n- The first link to a supposedly Smalltalk-related article is currently pointing to a page without apparent useful and immediate info.\n- \"you have to compile really every single binary of the target system\" and yet you have to start with a gcc binary that you got from somewhere, because the source can't compile itself. I wonder if you traced back the lineage of each gcc binary that was used to recompile each successive gcc, would you get all the way back to K&R's original C compiler?\n- @robru I don't know about K&R's process, I'm sure this wasn't the case for it, but theoretically, the source *could* compile itself from the very beginning. And as long as you have something or someone who can correctly execute the program and deduce what the output should be and write it down, you could get an executable binary. It's just: why even bother doing that when you have someone like Dennis Ritchie really talented at assembly code who can hand-code in assembly and then bootstrap from that?\n- I don't see why? There is no rule you have to bootstrap more than once (like for every a new platform), you can also crosscompile with a current one.\n- Everything is bootstrapped from a genesis transistor with a lot of hands on\n- What is the point of the second half about writing virus infested compilers? :)\n- @mhvelplund Just spreading the knowledge how bootstrapping can kill you.\n- Do you have a public code repository or some documentation? I'd love to see it.","metadata":{"transformedAt":"2026-08-18T18:32:17.740Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":34,"totalLines":343,"estimatedTokens":3448}}9{"id":"stack-768634","source":"stackoverflow","questionId":768634,"title":"Parse a .py file, read the AST, modify it, then write back the modified source code","tags":["python","compiler-construction","abstract-syntax-tree"],"text":"Title: Parse a .py file, read the AST, modify it, then write back the modified source code\nTags: python, compiler-construction, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI want to programmatically edit python source code. Basically I want to read a `.py` file, generate the AST, and then write back the modified python source code (i.e. another `.py` file).\n\nThere are ways to parse/compile python source code using standard python modules, such as `ast` or `compiler`. However, I don't think any of them support ways to modify the source code (e.g. delete this function declaration) and then write back the modifying python source code.\n\nUPDATE: The reason I want to do this is I'd like to write a Mutation testing library for python, mostly by deleting statements / expressions, rerunning tests and seeing what breaks.\n\n========================================\n\nTop Answer:\nThe builtin ast module doesn't seem to have a method to convert back to source. However, the codegen (archive.org, GitHub) module here provides a pretty printer for the ast that would enable you do do so.\neg.\n\n```\nimport ast\nimport codegen\n\nexpr=\"\"\"\ndef foo():\n print(\"hello world\")\n\"\"\"\np=ast.parse(expr)\n\np.body[0].body = [ ast.parse(\"return 42\").body[0] ] # Replace function body with \"return 42\"\n\nprint(codegen.to_source(p))\n```\n\nThis will print:\n\n```\ndef foo():\n return 42\n```\n\nNote that you may lose the exact formatting and comments, as these are not preserved.\n\nHowever, you may not need to. If all you require is to execute the replaced AST, you can do so simply by calling compile() on the ast, and execing the resulting code object.\n\n========================================\n\nCode:\n```text\n.py\n```\n\n```text\n.py\n```\n\n```text\nast\n```\n\n```text\ncompiler\n```\n\n```text\nast\n```\n\n```text\nlineno\n```\n\n```text\ncol_offset\n```\n\n```text\nimport ast\nimport codegen\n\nexpr=\"\"\"\ndef foo():\n   print(\"hello world\")\n\"\"\"\np=ast.parse(expr)\n\np.body[0].body = [ ast.parse(\"return 42\").body[0] ] # Replace function body with \"return 42\"\n\nprint(codegen.to_source(p))\n```\n\n```text\ndef foo():\n    return 42\n```\n\n```text\nrule mutate_addition(s:sum, p:product):sum->sum =\n  \" \\s + \\p \" -> \" \\s - \\p\"\n if mutate_this_place(s);\n```\n\n```text\nast\n```\n\n```text\ncodegen\n```\n\n```text\nast.NodeVisitor\n```\n\n```text\nvisitor_\n```\n\n```text\ncodegen\n```\n\n```text\npip install git+https://github.com/berkerpeksag/astor.git#egg=astor\n```\n\n```text\n>>> import ast\n>>> import astor\n>>> print(astor.to_source(ast.parse('def foo(x): return 2 * x')))\ndef foo(x):\n    return 2 * x\n```\n\n```text\ncodegen\n```\n\n```text\nastor\n```\n\n```text\nastor\n```\n\n```text\nastor\n```\n\n```text\nastor.to_source\n```\n\n```text\n>>> import ast\n>>> import astunparse\n>>> print(astunparse.unparse(ast.parse('def foo(x): return 2 * x')))\n\n\ndef foo(x):\n    return (2 * x)\n```\n\n```text\nastor\n```\n\n```text\nastunparse\n```\n\n```text\nexample = \"\"\"\ndef foo(): # Test\n  '''My func'''\n  log(\"hello world\")  # Print\n\"\"\"\n\nimport ast, asttokens\natok = asttokens.ASTTokens(example, parse=True)\n\ncall = next(n for n in ast.walk(atok.tree) if isinstance(n, ast.Call))\nstart, end = atok.get_text_range(call)\nprint(atok.text[:start] + ('WRAP(%s)' % atok.text[start:end])  + atok.text[end:])\n```\n\n```text\ndef foo(): # Test\n  '''My func'''\n  WRAP(log(\"hello world\"))  # Print\n```\n\n```text\nWRAP(...)\n```\n\n```text\n>>> import ast\n>>> tree = ast.parse(\"print 'Hello Python!!'\")\n>>> exec(compile(tree, filename=\"<ast>\", mode=\"exec\"))\nHello Python!!\n```\n\n```text\n>>> import ast\n>>> tree = ast.parse(\"print 'Hello Python!!'\")\n>>> ast.dump(tree)\n\"Module(body=[Print(dest=None, values=[Str(s='Hello Python!!')], nl=True)])\"\n```\n\n```text\n>>> import ast\n>>> tree = ast.parse(\"print ('Hello Python!!')\")\n>>> ast.dump(tree)\n\"Module(body=[Expr(value=Call(func=Name(id='print', ctx=Load()), args=[Str(s='Hello Python!!')], keywords=[]))])\"\n```\n\n```text\n#!/usr/bin/env python\n'''\nThis utility converts the python (2.7) statements to Python 3 alike function calls before running the code.\n\nUSAGE:\n     python print2to3.py <filename>\n'''\nimport ast\nimport sys\n\nclass P2to3(ast.NodeTransformer):\n    def visit_Print(self, node):\n        new_node = ast.Expr(value=ast.Call(func=ast.Name(id='print', ctx=ast.Load()),\n            args=node.values,\n            keywords=[], starargs=None, kwargs=None))\n        ast.copy_location(new_node, node)\n        return new_node\n\ndef main(filename=None):\n    if not filename:\n        return\n\n    with open(filename, 'r') as fp:\n        data = fp.readlines()\n    data = ''.join(data)\n    tree = ast.parse(data)\n\n    print \"Converting python 2 print statements to Python 3 function calls\"\n    print \"-\" * 35\n    P2to3().visit(tree)\n    ast.fix_missing_locations(tree)\n    # print ast.dump(tree)\n\n    exec(compile(tree, filename=\"p23\", mode=\"exec\"))\n\nif __name__ == '__main__':\n    if len(sys.argv) <=1:\n        print (\"\\nUSAGE:\\n\\t print2to3.py <filename>\")\n        sys.exit(1)\n    else:\n        main(sys.argv[1])\n```\n\n```text\nclass A(object):\n    def __init__(self):\n        pass\n\ndef good():\n    print \"I am good\"\n\nmain = good\n\nif __name__ == '__main__':\n    print \"I am in main\"\n    main()\n```\n\n```text\nast\n```\n\n```text\nast\n```\n\n```text\nast\n```\n\n```text\nast\n```\n\n```text\nast\n```\n\n```text\nast\n```\n\n```text\nast.parse()\n```\n\n```text\nast.dump()\n```\n\n```text\nast\n```\n\n```text\nast\n```\n\n```text\nast.NodeTransformer\n```\n\n```text\nast\n```\n\n```text\nprint \" x is %s\" % (\"Hello Python\")\n```\n\n```text\nast.unparse(ast_obj)\n```\n\n```py\nclass Replace(Rule):\n    \n    def match(self, node):\n        assert isinstance(node, ast.Name)\n        assert node.id == 'placeholder'\n        \n        replacement = ast.Constant(42)\n        return ReplacementAction(node, replacement)\n```\n\n```text\n--- test_file.py\n+++ test_file.py\n\n@@ -1,11 +1,11 @@\n\n def main():\n-    print(placeholder * 3 + 2)\n-    print(2 +               placeholder      + 3)\n+    print(42 * 3 + 2)\n+    print(2 +               42      + 3)\n     # some commments\n-    placeholder # maybe other comments\n+    42 # maybe other comments\n     if something:\n         other_thing\n-    print(placeholder)\n+    print(42)\n \n if __name__ == \"__main__\":\n     main()\n```\n\n```text\nplaceholder\n```\n\n```text\n42\n```\n\n```text\nlibcst\n```\n\n```py\nimport ast\ncode=\"\"\"\ndef foo():\n   print(\"hello world\")\n\"\"\"\np=ast.parse(code)\nprint(ast.unparse(p))\n```\n\n```py\n>>> import fst  # pip install pfst, import fst\n\n>>> ext_ast = fst.parse('if a: b = c, d  # comment')\n\n>>> ext_ast.f.body[0].body[0].value.elts[1:1] = 'u,\\nv  # blah'\n\n>>> print(fst.unparse(ext_ast))\nif a: b = (c, u,\n          v,  # blah\n          d)  # comment\n```\n\n```text\npfst\n```\n\n========================================\n\nComments:\n- Deprecated since version 2.6: The compiler package has been removed in Python 3.0.\n- What can't you edit the source? Why can't you write a decorator?\n- Holy cow! I wanted to make a mutation tester for python using the same technique (specifically creating a nose plugin), are you planning on open sourcing it?\n- @Ryan Yeah I'll open source anything I create. We should keep in contact on this\n- Definitely, I sent you an email through Launchpad.\n- Are you running any genetic algorithms on your mutations? :P\n- macropy provides syntax sugar for manipulating ast at import time.\n- Real world use case for code generation: Kid and Genshi (I believe) generate Python from XML templates for speedy rendering of dynamic pages.\n- the unparse example is still maintained, here is the updated py3k version: hg.python.org/cpython/log/tip/Tools/parser/unparse.py\n- With regard to `unparse.py` script - it may be really cumbersome to use it from another script. But, there is a package called astunparse (on github, on pypi) which is basically a properly packaged version of `unparse.py`.\n- Could you maybe update your answer by adding parso as the preferred option? It's very good and updated.\n- @Ryan. Can you please give me tools to get AST and CFG for python source code?\n- `lib2to3` seems to be a bit undocumented though, although python3porting.com/fixers.html has some notes. -- there's a problem, the API is claimed unstable, and as explained in docs.python.org/3/library/2to3.html the library is not able to parse some Python 3.10 syntax and it will be removed soon.\n- Just for anyone using this in the future, codegen is largely out-of-date and has a few bugs. I've fixed a couple of them; I have this as a gist on github: gist.github.com/791312\n- Notice the latest codegen is updated in 2012 which is after the above comment, so I guess codegen is updated. @mattbasta\n- astor appears to be a maintained successor to codegen\n- This does not show how to print, it execs?\n- It's not true that `parso` is up to date with modern Python as of now (6 years later). See Soft Keywords and How to Implement Them &#183; Issue #138 &#183; davidhalter/parso (tl;dr `match` keyword is unimplemented). Besides, the fact that it uses LL(1) parsing makes it really difficult to modify the library. And also the library **does not check for syntax ambiguity**, which means that if the grammar is modified manually, it is easy to introduce bugs.\n- Yea, my assumption that it would be maintained turned to be wrong. I guess I will have to change mutmut to use the black parser or something...\n- It will mess up some code formatting. E.g. removing some blank lines or replacing double quotes with apostrophes.\n- This is definitely the best modern alternative in my opinion. Guido likes it enough for this sort of global automated mechanical code maintenance to give it a shout out here: youtu.be/atSMXLwtIBo?t=139\n- Any reason why we should prefer this over libcst?\n- @user202729 Depends on use case. For large scale refactoring use libcst. This one is meant more for quick scripts - it treats the AST as a random-access mutable container so you don't have to make a pass over the whole file to change one or two parts.","metadata":{"transformedAt":"2026-08-18T18:32:17.740Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":55,"totalLines":430,"estimatedTokens":2457}}10{"id":"stack-9429491","source":"stackoverflow","questionId":9429491,"title":"How are GCC and g++ bootstrapped?","tags":["c++","c","compiler-construction"],"text":"Title: How are GCC and g++ bootstrapped?\nTags: c++, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThis has been bugging me for a while. How do GCC and g++ compile themselves?\n\nI'm guessing that every revision gets compiled with a previously built revision. Is this true? And if it is, does it mean that the oldest g++ and GCC versions were written in assembly?\n\n========================================\n\nTop Answer:\nIf you want to replicate the bootstrap process of GCC in a modern environment (x86 Linux), you can use the tools developed by the bootstrappable project:\n\nWe can start with `hex0` assembler (on x86 it's 357 byte binary) which does\nroughly what the following two commands do\n\n```\nsed 's/[;#].*$//g' hex0_x86.hex0 | xxd -r -p > hex0\nchmod +x hex0\n```\n\nI.e. it translates ASCII equivalent of binary program into binary code, but it\nis written in hex0 itself.\n\nBasically, hex0 has equivalent source code that is in one to one correspondence\nto its binary code.\n\n`hex0` can be used to build a slighly more powerful `hex1` assembler that\nsupports a few more features (one character labels and calculates offsets).\nhex1 is written in hex0 assembly.\n\n`hex1` can be used to build `hex2` (even more advanced assembler that supports multi character labels).\n\n`hex2` then can be used to build a macro assembler (where program using macros instead of hex opcodes).\n\nYou can then use thismacro assembler to build `cc_x86` which is a \"C compiler\" written in assembly. cc_x86 only supports a small subset of C but that's an impresive start.\n\nYou can use `cc_x86` to build `M2-Planet` (Macro Platform Neutral Transpiler) which is a C compiler written in C. M2-Planet is self hosting and can build itself.\n\nYou can then use M2-Planet to build GNU Mes which is a small scheme interpreter.\n\nmes can be used to run mescc which is a C compiler written in scheme and lives in the same repository as mes.\n\nmescc can be used to rebuild mes and also build mes C library.\n\nThen mescc can be used to build a slighly patched Tiny C compiler.\n\nThen you can use it to build newer version of TCC 0.9.27.\n\nGCC 4.0.4 and musl C library can be built with TCC 0.9.27.\n\nThen you can build newer GCC using older GCC.\nE.g. GCC 4.0.4 -> GCC 4.7.4 -> modern GCC.\n\nTL;DR:\n\nhex0 -> hex1 -> hex2 -> M0 -> M2-Planet -> Mes -> Mescc -> TCC -> GCC.\n\n========================================\n\nCode:\n```text\nsed 's/[;#].*$//g' hex0_x86.hex0 | xxd -r -p > hex0\nchmod +x hex0\n```\n\n```text\nhex0\n```\n\n```text\nhex0\n```\n\n```text\nhex1\n```\n\n```text\nhex1\n```\n\n```text\nhex2\n```\n\n```text\nhex2\n```\n\n```text\ncc_x86\n```\n\n```text\ncc_x86\n```\n\n```text\nM2-Planet\n```\n\n========================================\n\nComments:\n- Each revision can finally be compiled by itself. :)\n- This is interesting to read if you want to see how the first compilers came about.\n- @parkovski Is the link dead?\n- Link last seen on Jun 04, 2016: web.archive.org/web/20160604035203/homepage.ntlworld.com/&hellip;\n- Isn't step 2 typically repeated a couple of times? I believe `clang` does it.\n- I don't see why, except for testing. Assuming the initial compiler implements the language correctly, it builds a correct though perhaps suboptimal compiler, while step 2 would implement all the optimizations that the new compiler has. The final binary should not change after step 2.\n- The 3-step version of the bootstrap build process is indeed for verification: the compiler itself is used as its own test case. GCC compiled with [other] should produce the same results (identical binaries, discounting macros like `__DATE__` and `__TIME__` which vary even between invocations of the *same* compiler) as GCC compiled with [GCC compiled with [other]] - if not, that's a bug, and the 3-stage bootstrap build is designed to catch that.\n- @pmjordan: \"if not, that's a bug\" or, less likely, a devious backdoor in the process of being introduced (\"Reflections on Trusting Trust\").\n- @pmjordan: Actually, the binaries could also differ because code generation was improved (e.g. better optimization). So in general the binaries need not be identical.\n- @sleske: They should converge, however. Unless you reach *The Singularity* :)\n- @sleske: that's not true. The binary output of step 2 must be identical to the binary output of step 3, otherwise there's a bug somewhere. The reason is as pmjordan says: NewCompiler1 and NewCompiler2 are programs with identical source (that of NewCompiler). They are given identical input (the source for NewCompiler). Therefore they will produce identical output no matter what compiler they themselves were compiled with (in this case, NewCompiler1 was compiled with OldCompiler, and NewCompiler2 was compiled with NewCompiler1). That is, NewCompiler2 and NewCompiler3 are binary identical.\n- @SteveJessop: Sorry, my mistake. Of course the output of step 1 and step 2 above can be different, but 2 and 3 must generate the same GCC binary.\n- I you ever wondered: What if we lost all C compiler binaries? And had to bootstrap from scratch? This is how I'd go about it: There's the Tiny C Compiler (which actually can compile the Linux kernel, so it's quite feature complete). All it's C source files make a mere 30k lines of code, including comments. Though even it was a quite some effort, somebody who understands C could learn from the sources, how to generate binary output and \"compile\" the TCC sources from hand (I actually thinking of punch cards here). Then recompile TCC with that and use it to bootstrap GCC or similar.\n- @datenwolf: something like that, yes. If we can assume that we've lost all C compiler binaries, but we still have an assembler, then we might write an assembler program TinyTinyC. It would be a less feature-complete C compiler than TinyC: we don't need it to be able to compile GCC or the linux kernel, we only need it to be able to compile TinyC. Then run it on the source of TinyC, that gives us a C compiler capable of compiling Linux (and hopefully glibc and GCC) and we're in business. If we don't even have an assembler, then we'd first bootstrap one of those, it's easier than a C compiler.\n- @SteveJessop: If TCC can't compile GCC I'd try PCC as a intermediary.\n- @datenwolf: I'm probably not the only geek who wrote his own VM and a compiler for it. It's not *that* hard.\n- @user1010005: It's very unlikely we lost all C compiler binaries. I've still got several Linux Distrubtions on DVDs at home, the Visual Studio CDs, etc. All it takes is only one working copy of a C compiler to bootstrap the rest.\n- @user1010005: What if all power plants on the world explode at once?\n- Bootstrapping is cool. How do you make a flat surface without a flat surface? How do you get machine tools in a world with no machine tools? How do you make 32nm chips in a world without working chips? Make vehicles without vehicles? All these things are built on previous generations.\n- @phresnel: We would have more grave problems than recompiling GCC if that happened, wouldn't we?\n- @larsmans: As your Wikipedia link (and Stroustrop) describe, **the first C++ compiler was written in C++**, not C. At that time, there was already a tool that would convert C++ source to C source, making this chicken/egg scenario possible.\n- @DrewDormann: but then what do you call the tool that compiles C++ to C source? And what was that originally written in? (You could say C with Classes, but I consider that cheating :)\n- @larsmans: From the horse's mouth: He wrote a **preprocessor** in C that converted C With Classes source to C source. Then he wrote a **compiler** in C++ that apparently didn't choke that preprocessor. www2.research.att.com/~bs/bs_faq.html#bootstrapping\n- @DrewDormann: I think this is a matter of definition (compiler/translator/preprocessor), but I amended the answer anyway. Thanks for the link.\n- @larsmans: I completely agree. I'm no master of semantics, so I just cautiously refer to Stroustrup's own account. I'll take his word for it.\n- Why is stage 3 needed? How can stage 2 and 3 possibly differ (except for `__TIME__` of course, which should not matter)?\n- @CiroSantilli新疆改造中心六四事件法轮功: Imagine the case of a backdoor; your stage 1 compiler (GCC, not the one doing the bootstrapping) recognizes a comment of \"//KeywordChecks\" which appears right before a series of if-elseif statements, so it adds in a chunk of instructions to the newly built binary that recognizes \"//BADCODE\" as a keyword and if found inserts a chunk of instructions that does devious stuff. Now your stage 2 compiler recognizes \"//BADCODE\" which the stage 1 compiler doesn't. So upon recompiling itself in step 3, if \"//BADCODE\" is a comment, then stage 2 and 3 will differ.\n- Note that current versions of GCC are actually written in C++, so if all you start with is a C compiler, then you'll first need to build some other C++ compiler which is written in C. (An old version of G++ might work.)\n- @Ryan and if \"//BADCODE\" isn't in the compiler's source?\n- @nog642 pick something that does appear in the compiler's source. In case it wasn't obvious, that was just used as an example of a placeholder string to search for and replace.\n- Your forgot you need to recompile modern GCC to get all the optimisation in C++ (gcc is written in C++ after all nowadays) of that modern GCC and not just 4.7.4. 🙄🙄\n- Well, this is just a short summary. When you build modern GCC you can use its build system to recompile it automatically. And there are lots of missing steps here that don't deal with compilers, you need to build shell, binutils and many others. E.g. you can take a look at what live-bootstrap does github.com/fosslinux/live-bootstrap/blob/master/parts.rst though for now it only goes to g++ 4.7.4.\n- 1. Do you mean x86_64 or x86 32-bit? 2. What does it matter that M2 Planet is self-hosting? 3. Why is it necessary to rebuild mes and build the mes C library? 4. If GNU Mes is a C compiler already, why do we need to build TCC? i.e. why can't GCC be built with mes? 5. Why can't the patched-TCC be used to build GCC and musl, but is rather used to build an unpatched TCC?\n- It is 32-bit version. 64-bit support is close but not done. 2. Well, in this bootstrapping process it's the first self-hosting program, i.e. you can mostly forget about assembly by then and focus on more and more advanced compilers. 3. Because mes built with mescc is a bit more capable than mes built with M2-Planet (and is also faster). 4. Because mescc can't build more complicated software, if I remember, it doesn't even have floats. Also mescc is very slow, it takes 10 minutes to build tcc and GCC is muc larger. 5. This is a patched TCC 0.9.26 which is much older. TCC 0.9.27 is more capable.\n- the hex1, hex2 and macro assembler links above have been dead","metadata":{"transformedAt":"2026-08-18T18:32:17.741Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":141,"estimatedTokens":2681}}11{"id":"stack-6121146","source":"stackoverflow","questionId":6121146,"title":"Reading GHC Core","tags":["performance","compiler-construction","haskell","ghc"],"text":"Title: Reading GHC Core\nTags: performance, compiler-construction, haskell, ghc\nSource: Stack Overflow\n\nQuestion:\nCore is GHC's intermediate language. Reading Core can help you better understand the performance of your program. Someone asked me for documentation or tutorials on reading Core, but I couldn't find much.\n\nWhat documentation is available for reading GHC Core?\n\nHere's what I've found so far:\n\n- Write Haskell as fast as C: exploiting strictness, laziness and recursion\n\n- Haskell as fast as C: working at a high altitude for low level performance\n\n- RWH: Chapter 25. Profiling and optimization\n\n- High-Performance Haskell talk at CUFP (slide 65-80)\n\n========================================\n\nTop Answer:\nA tip: If you don't care about type annotations and coercions use `-ddump-simpl` together with the `-dsuppress-all` option. The Core output should be much more readable.\n\n========================================\n\nCode:\n```text\nGHC.Core.Expr\n```\n\n```text\nshare/doc/ghc/core.pdf\n```\n\n```text\nmap\n```\n\n```text\n-ddump-simpl\n```\n\n```text\n-dsuppress-all\n```\n\n========================================\n\nComments:\n- See also stackoverflow.com/questions/6048194/&hellip;\n- skillsmatter.com/skillscasts/&hellip; might be useful, too.\n- Every single answer you give is always ridiculously complete. Have another up-vote and keep it up; I'm leaning heaps.\n- The amount of CC-wiki documentation that Don and the general Haskell community has released via SO is staggering. Keep up the good Q's and A's, everybody!\n- I know it's mentioned, but I think the usefulness of **ghc-core** should be emphasized in the answer.\n- The link to GHC's `-fext-core` has bitrotted, and it seems that recent GHCs don't have this flag anymore. The latest GHC where I can find this section in the user's guide is 7.8: downloads.haskell.org/~ghc/7.8.4/docs/html/users_guide/&hellip;. What's the best way to update this reference? Link to docs for `-ddump-simpl` instead?! Simply remove the reference?!\n- @sjakobi, indeed, that feature was removed a few years ago—the developers decided it wasn't worth the trouble to maintain.\n- STG is a much lower level than Core. The compilation pipeline is: Haskell -> Core -> STG -> C-- -> Machine Code\n- `-dsuppress-all` is really useful. You can also use `-dsuppress-coercions` if you only want to get rid of casts (useful when there are lots of newtypes around).\n- `-dsuppress-coercions -dsuppress-type-applications` gets rid of the worst of the noise while keeping most of what you want.","metadata":{"transformedAt":"2026-08-18T18:32:17.741Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":60,"estimatedTokens":628}}12{"id":"stack-1093536","source":"stackoverflow","questionId":1093536,"title":"How does the C# compiler detect COM types?","tags":["c#","com","compiler-construction","c#-4.0"],"text":"Title: How does the C# compiler detect COM types?\nTags: c#, com, compiler-construction, c#-4.0\nSource: Stack Overflow\n\nQuestion:\n**EDIT:** I've written the results up as a blog post.\n\nThe C# compiler treats COM types somewhat magically. For instance, this statement looks normal...\n\n```\nWord.Application app = new Word.Application();\n```\n\n... until you realise that `Application` is an interface. Calling a constructor on an interface? Yoiks! This actually gets translated into a call to `Type.GetTypeFromCLSID()` and another to `Activator.CreateInstance`.\n\nAdditionally, in C# 4, you can use non-ref arguments for `ref` parameters, and the compiler just adds a local variable to pass by reference, discarding the results:\n\n```\n// FileName parameter is *really* a ref parameter\napp.ActiveDocument.SaveAs(FileName: \"test.doc\");\n```\n\n(Yeah, there are a bunch of arguments missing. Aren't optional parameters nice? :)\n\nI'm trying to investigate the compiler behaviour, and I'm failing to fake the first part. I can do the second part with no problem:\n\n```\nusing System;\nusing System.Runtime.InteropServices;\nusing System.Runtime.CompilerServices;\n\n[ComImport, GuidAttribute(\"00012345-0000-0000-0000-000000000011\")]\npublic interface Dummy\n{\n void Foo(ref int x);\n}\n\nclass Test\n{\n static void Main()\n {\n Dummy dummy = null;\n dummy.Foo(10);\n }\n}\n```\n\nI'd like to be able to write:\n\n```\nDummy dummy = new Dummy();\n```\n\nthough. Obviously it'll go bang at execution time, but that's okay. I'm just experimenting.\n\nThe other attributes added by the compiler for linked COM PIAs (`CompilerGenerated` and `TypeIdentifier`) don't seem to do the trick... what's the magic sauce?\n\n========================================\n\nTop Answer:\nBetween you and Michael you've almost got the pieces put together. I think this is how it works. (I didn't write the code, so I might be slightly mis-stating it, but I'm pretty sure this is how it goes.)\n\nIf:\n\n- you are \"new\"ing an interface type, and\n\n- the interface type has a known coclass, and\n\n- you ARE using the \"no pia\" feature for this interface\n\nthen the code is generated as (IPIAINTERFACE)Activator.CreateInstance(Type.GetTypeFromClsid(GUID OF COCLASSTYPE))\n\nIf:\n\n- you are \"new\"ing an interface type, and\n\n- the interface type has a known coclass, and\n\n- you ARE NOT using the \"no pia\" feature for this interface\n\nthen the code is generated as if you'd said \"new COCLASSTYPE()\".\n\nJon, feel free to bug me or Sam directly if you have questions about this stuff. FYI, Sam is the expert on this feature.\n\n========================================\n\nCode:\n```text\nWord.Application app = new Word.Application();\n```\n\n```text\n// FileName parameter is *really* a ref parameter\napp.ActiveDocument.SaveAs(FileName: \"test.doc\");\n```\n\n```text\nusing System;\nusing System.Runtime.InteropServices;\nusing System.Runtime.CompilerServices;\n\n[ComImport, GuidAttribute(\"00012345-0000-0000-0000-000000000011\")]\npublic interface Dummy\n{\n    void Foo(ref int x);\n}\n\nclass Test\n{\n    static void Main()\n    {\n        Dummy dummy = null;\n        dummy.Foo(10);\n    }\n}\n```\n\n```text\nDummy dummy = new Dummy();\n```\n\n```text\nApplication\n```\n\n```text\nType.GetTypeFromCLSID()\n```\n\n```text\nActivator.CreateInstance\n```\n\n```text\nref\n```\n\n```text\nCompilerGenerated\n```\n\n```text\nTypeIdentifier\n```\n\n```text\n[System.Runtime.InteropServices.CoClass(typeof(Test))]\npublic interface Dummy { }\n```\n\n```text\n[CoClass(typeof(SpVoiceClass))]\npublic interface SpVoice : ISpeechVoice, _ISpeechVoiceEvents_Event { }\n```\n\n```text\nSpVoice\n```\n\n```text\nSPVoiceClass\n```\n\n```text\n[ComImport, TypeLibType(...), Guid(\"...\"), DefaultMember(\"Name\")]\npublic interface _Application\n{\n     ...\n}\n\n[ComImport, Guid(\"...\"), CoClass(typeof(ApplicationClass))]\npublic interface Application : _Application\n{\n}\n\n[ComImport, ClassInterface(...), ComSourceInterfaces(\"...\"), Guid(\"...\"), \n TypeLibType((short) 2), DefaultMember(\"Name\")]\npublic class ApplicationClass : _Application, Application\n{\n}\n```\n\n```text\n[ComImport, TypeIdentifier, Guid(\"...\"), CompilerGenerated]\npublic interface _Application\n\n[ComImport, Guid(\"...\"), CompilerGenerated, TypeIdentifier]\npublic interface Application : _Application\n```\n\n```text\nWord.Application application = new Word.Application();\n```\n\n```text\nApplication application = new ApplicationClass();\n```\n\n```text\nApplication application = (Application) \n    Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid(\"...\")));\n```\n\n```text\nCoClass\n```\n\n```text\nApplication\n```\n\n```text\nApplication\n```\n\n```text\nApplicationClass\n```\n\n```text\nCoClass\n```\n\n```text\nCoClass\n```\n\n```text\npublic class Program\n{\n    public class Foo : IFoo\n    {\n    }\n\n    [Guid(\"00000000-0000-0000-0000-000000000000\")]\n    [CoClass(typeof(Foo))]\n    [ComImport]\n    public interface IFoo\n    {\n    }\n\n    static void Main(string[] args)\n    {\n        IFoo foo = new IFoo();\n    }\n}\n```\n\n```text\nComImportAttribute\n```\n\n```text\nGuidAttribute\n```\n\n```text\nnew IFoo()\n```\n\n========================================\n\nComments:\n- You're way ahead of me on this stuff (most stuff), but just for clairification, it sounds like what you are after is dependency injection functionality with a more normalized syntax--would that be accurate?\n- Aren't optional parameters nice? IMO, No they are not nice. Microsoft is trying to fix the flaw in Office COM interfaces by adding bloat to C#.\n- Nice question (+1). I've noticed the same thing about calling a constructor on an interface. It looks horrendous in code because it'll really confuse any developers not familiar with what's going on. I always wondered what was going on behind the scenes to make it work. Looks like I'll be buying your new book ;-)\n- Are you creating a type library for your interface? I suspect you need to declare the interface using IDL and then register the resulting type library MIDL outputs in the registry. I also suspect this is just one step on the way to your goal.\n- @Mehrdad: Optional parameters are useful beyond COM, of course. You need to be careful with the default values, but between them and named arguments, it's a lot easier to build a usable immutable type.\n- @jeffamaphone: Nope, I'm not trying to create a type library at all. I'm trying to fake up a COM type so I can see what the C# compiler does without actually having to build any COM types :)\n- True. Specifically, named parameters can be **practically required** for interop with some dynamic environments. Sure, without a doubt, it's a useful feature but that doesn't mean it comes for free. It costs simplicity (an explicitly stated design goal). Personally, I think C# is amazing for features the team left off (otherwise, it could have been a C++ clone). C# team is great but a corporate environment can hardly be politics-free. I *guess* Anders himself wasn't very happy about this as he stated in his PDC'08 talk: \"took us ten years to get back to where we were.\"\n- I agree that the team will need to keep a close eye on complexity. The dynamic stuff adds a lot of complexity for little value for *most* developers, but high value for *some* developers.\n- I've seen framework developers beginning to discuss its uses in many places. IMO it's just time until we find a good use for `dynamic` ... we're just too used to static/strong typing to see why it'd matter outside of COM.\n- Very interesting - will try it later. The linked PIA types don't have CoClass though. Maybe it's something to do with the linking process - I'll have a look in the original PIA...\n- thanks, i was trying but i was missing **ComImport** attribute, but when i go i to source code i was working using F12 only shows **CoClass** and **Guid**, why is that?","metadata":{"transformedAt":"2026-08-18T18:32:17.741Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":29,"totalLines":274,"estimatedTokens":1916}}13{"id":"stack-629017","source":"stackoverflow","questionId":629017,"title":"how does array[100] = {0} set the entire array to 0?","tags":["c++","c","compiler-construction"],"text":"Title: how does array[100] = {0} set the entire array to 0?\nTags: c++, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nHow does the compiler fill values in `char array[100] = {0};`? What's the magic behind it?\n\nI wanted to know how internally compiler initializes.\n\n========================================\n\nTop Answer:\nImplementation is up to compiler developers.\n\nIf your question is \"what will happen with such declaration\" - compiler will set first array element to the value you've provided (0) and all others will be set to zero because it is a default value for omitted array elements.\n\n========================================\n\nCode:\n```text\nchar array[100] = {0};\n```\n\n```text\nchar array[100] = {};\n```\n\n```text\nint array[256] = {[0 ... 255] = 0};\n```\n\n```text\nchar array[100] = {0};\n\nint main(void)\n{\n...\n}\n```\n\n```text\nint foo(void)\n{\nchar array[100] = {0};\n...\n}\n```\n\n```text\nint foo(void)\n{ \nchar array[100];\n\nmemset(array, 0, sizeof(array));\n....\n}\n```\n\n```text\nint foo(void)\n{ \nstatic char array[100] = {0};\n...\n}\n```\n\n========================================\n\nComments:\n- In C or C++? They are two separate questions.\n- I don't have a source, but I'm pretty sure that I read somewhere that there is no default value for array declarations; you get whatever garbage was already there. There's no sense in wasting time setting these values when you're likely to overwrite them anyway.\n- Ryan, if you don't set a value for the first element that the whole array is uninitialised and indeed contains garbage, but if you set a value for at least one element of it the whole array becomes initialised so unspecified elements get initialised implicitly to 0.\n- For C++ an empty initializer list for a bounded array default-initializes all elements.\n- dalle, I'm not into c++ so my response was about c.\n- qrdl, i think dalle (and me too, btw) read it as you would say \"char a[100] = {}\" would leave it uninitialized. but actually it is only valid in C++, and invalid syntax in C.\n- Well, I didn't mean that. {} is an empty initialiser (invalid in C, as you pointed out) so quite obviously it will initialise the array.\n- This answer is incorrect. Implementation of `char array[100] = {0}` is *not* undefined. See the above answer by @bk1e.\n- @NatanYellin Where did I say that this is undefined? Please read the full answer before commenting and downvoting.\n- @qrdl You're right. I misunderstood your comment about the implementation. Unfortunately, I can't change my vote now.\n- Do all C compilers do this? I was lead to believe only Visual Studio does this.\n- online draft of c++ specs broken, anyone has new link?\n- Welcome! since you asked for Looking more such sorts of tricks, I had provided\n- You certainly can do this if you choose, but there are obvious disadvantages to relying on compiler-specific extensions like this one.\n- @Dan Olson his question himself is asking about compiler specific and hence posted this. If you feel it is useless, i will delete.\n- It's not useless, it's interesting. The caveat just deserves to be noted.\n- It's stuff like this keeps me coming back to SO and reading more than the top few answers...\n- Note that this may work for gcc and not g++. This doesn't work for me with g++ 6.2.0","metadata":{"transformedAt":"2026-08-18T18:32:17.741Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":87,"estimatedTokens":811}}14{"id":"stack-1653649","source":"stackoverflow","questionId":1653649,"title":"How was the first compiler written?","tags":["compiler-construction","machine-instruction"],"text":"Title: How was the first compiler written?\nTags: compiler-construction, machine-instruction\nSource: Stack Overflow\n\nQuestion:\nI heard about the chicken and the egg and bootstrapping. I have a few questions.\n\nWhat wrote the first compiler that converted something into binary instructions?\n\nIs assembly compiled or translated into binary instructions?\n\n...I'd find it hard to believe they wrote a compiler in binary.\n\n========================================\n\nTop Answer:\nPlease read about compiler bootstrapping and the history of compiler writing\n\nThe idea is to write a very simple compiler directly in machine code, use it to write a more sophisticated compiler, use the second one to build a third one and so on until you can have a full featured compiler.\n\n========================================\n\nComments:\n- possible duplicate of When someone writes a new programming language, what do they write it IN?\n- @nawfal, it is a difference between a new programming language and the first compiler, so no - it is not a duplicate\n- @PauliSudarshanTerho what is the difference? The spirit of the questions are the same. You can't write a programming language, that question is in turn talking about first compiler.\n- In your spiritual imaginations maybe? In reality you not find anything mentioned about the first compiler in that link. And you should not advice anyone writing a new language to start from scratch. And if so then why would you want to hide the answers about how first compiler is written if it is important for writing a new language?\n- Learn from me - This is a duplicate: stackoverflow.com/questions/4772768/&hellip;\n- The question is about the first compiler, not the first programs in general, despite programs sometimes being compilers; the history of the two is not the same. (An analogy: the answer to the question of when the first animals appeared on the Earth is not the answer to the question of when the first cats appeared on the Earth, despite cats being animals.)\n- My first computer was a Z80-based machine in whose ROM monitor I had to hand-assemble a bootstrap loader to bring up the basics of an operating system (CP/M) so I could assemble the rest of said operating system into a working system, complete with a disk-based bootstrap loader. Fun times. So yeah, you can hand-assemble just fine. It's slow and painful and error-prone (which is why we automated things) but it's possible.\n- The first link is broken.\n- Written by hand. How? Wiring or punching cards? I guess they had hexadecimal keypads.\n- @user985399 With pencil and paper. How programs were entered into a computer is a different matter, but it was initially via stepping switches. Hexadecimal keypads are very advanced technology compared to the first computers.\n- The link seems to be 404 right now, in any case, \"Grace\" above is Grace Hopper.\n- I've heard that Hopper wrote the first compiler, but the description above it makes it sound more like a linker than a compiler. Still, good story. It's amazing to think there was a time when computer scientists were skeptical about the idea of compilers...\n- @mehaase this is why it is called \"compiler\". it makes a compilation of routines, each of which is (potentially) written in machine language directly.\n- @MarkE.Haase The people Hopper referred to here were applications engineers and scientists using computers to for specific calculation tasks; they were not \"computer scientists\". There was a handful of cyberneticists around in 1952, but I doubt that she spoke to any of those.\n- Writing the Altair BASIC interpreter after making the sales pitch? Coding the bootstrapper on the plane ride to Albuquerque? That sounds kind of ridiculous. And fun.\n- @Shurane: ha! Those point are relevant too but to me the nitty-gritty of how they made the BASIC interpreter and how the group crammed it into the tiny space is a thing of beauty and astounding programming ability/hackery.\n- I would like to add that a second edition of CODE by Charles Petzold was published in 2022. ISBN: 978-0-13-790910-0 charlespetzold.com/books","metadata":{"transformedAt":"2026-08-18T18:32:17.741Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":40,"estimatedTokens":1020}}15{"id":"stack-27513234","source":"stackoverflow","questionId":27513234,"title":"In which language is the C# compiler written?","tags":["c#","compiler-construction","roslyn"],"text":"Title: In which language is the C# compiler written?\nTags: c#, compiler-construction, roslyn\nSource: Stack Overflow\n\nQuestion:\nI looked at the source code at http://referencesource.microsoft.com/, and it appears all the source code is in C#.\n\nI also looked at the source code for the new C# compiler platform (Roslyn), and it is also in C#. How is that possible? Is C# language compiler written in C#? Or am I missing something obvious? If C# compiler is written in C# then how does it work?\n\n========================================\n\nTop Answer:\nCompilers are utility programs - they turn programming language text into machine code. If the programming language describes software that just happens to be a compiler.....\n\nCompilers can also produce machine code for other architectures. For example, Apple compiles iOS using racks of Intel-based servers. The compiler does not have to run the ARM code it generates, just write it to disk.\n\nCompiler 2.0 must be written in a language compiler 1.0 can process, but it can certainly create compiler 2.0 with newer features like optimization. You can then re-compile the source code using compiler 2.0 and make a better version of itself. Again, the compiler doesn't know it's making another version of itself.\n\nIf we go far enough back into the mists of time then we do reach a point where we have no compiler - the very first iteration of a high-level language. Then we have to get out the pencils and opcode books and write the first one in assembly. How did we write the first assembler? Direct machine code entry, probably on punched paper tape, or flipping switches on the front panel.\n\n========================================\n\nComments:\n- Many compilers are written in the language they compile - Google bootstrapping to learn more.\n- I think the *original* compiler was written in C++.\n- Well, a hammer can be forged by using another hammer. Previous version of it...\n- By using a spec. And backwards compatibility.\n- it gets compiled into IL\n- The link you posted is the link to the source code of the Framework library, not to the compiler.\n- Possibly related: Implementing a compiler in “itself” and Bootstrapping a language\n- It's not nearly as mind blowing as something like a self-hosted JVM implementation written in Java (JikesRVM).\n- @SK-logic: AFAIK, JikesRVM is basically a statically compiled VM, which just happens to be written in Java. What is more mindblowing IMO, is something like the Maxine RVM, which runs inside of itself, compiling itself with its own dynamic JIT compiler while it is running. So, in Jikes, there is still a clear separation between compiling the VM and running the VM, at least as far as I understand it.\n- I'm pretty sure for most popular languages there are compilers written in that language.\n- I remember being blown away by code like this in a Lisp interpreter: `(defun car (cons) (car cons))`. It looks like infinite recursion, but it isn't, because of open-coding in the compiler.\n- So when a change has to be made to the \"original compiler\", does that has to be compiled with the old compiler *(written in C,C++)* ?\n- There would be no need to change the \"original compiler\" the newer versions would be modified\n- @CriketerOnSO, the new compiler will replace the old one, so there will be no need to modify the old one. But if MS wanted to do that, they would recompile the old compiler with a C++ compiler, as they did before.\n- @ThomasLevesque Self-hosting is the end result of boot-strapping.\n- @CriketerOnSO: No, of course not. How would it even be possible to compile the old compiler with the old compiler? The compiler is for C#, but it is written in C++, you need a C++ compiler to compile it.\n- The same is applied to C/C++ compilers, which have a little 'bootstrapping' in assembly which compiles a little subset of C, so it compiles another subset of C increasing support for some 'high-level' programming, and so on... until reaching the last/current subset of the compiler, which can be tested compiling it self. This is and can be used in any kind of language.\n- When new keywords are added to the language, how it will be compiled? because we don't have the compiler(yet) which understands the new tokens ?\n- @SriramSakthivel, the code of the compiler can't use the new keywords, at least not until there is a compiler that understands them. You always use an older version of the compiler to build the new one.\n- And the paper tape is just flipping switches via holes in the paper. :-)\n- Paper tape as a storage technology will *never* take off. It's just too complex and error-prone, plus it burns easily if there is a short circuit in the reader and that will completely destroy your program.","metadata":{"transformedAt":"2026-08-18T18:32:17.741Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":44,"estimatedTokens":1179}}16{"id":"stack-9535250","source":"stackoverflow","questionId":9535250,"title":"Why is the .bss segment required?","tags":["c","linux","compiler-construction","data-segment"],"text":"Title: Why is the .bss segment required?\nTags: c, linux, compiler-construction, data-segment\nSource: Stack Overflow\n\nQuestion:\nWhat I know is that global and static variables are stored in the `.data` segment, and uninitialized data are in the `.bss` segment. What I don't understand is why do we have dedicated segment for uninitialized variables? If an uninitialized variable has a value assigned at run time, does the variable exist still in the `.bss` segment only?\n\nIn the following program, `a` is in the `.data` segment, and `b` is in the `.bss` segment; is that correct? Kindly correct me if my understanding is wrong.\n\n```\n#include \n#include \n\nint a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9};\nint b[20]; /* Uninitialized, so in the .bss and will not occupy space for 20 * sizeof (int) */\n\nint main ()\n{\n ;\n}\n```\n\nAlso, consider following program,\n\n```\n#include \n#include \nint var[10]; /* Uninitialized so in .bss */\nint main ()\n{\n var[0] = 20 /* **Initialized, where this 'var' will be ?** */\n}\n```\n\n========================================\n\nTop Answer:\nThe `.bss` segment is an optimization. The entire `.bss` segment is described by a single number, probably 4 bytes or 8 bytes, that gives its size in the running process, whereas the `.data` section is as big as the sum of sizes of the initialized variables. Thus, the `.bss` makes the executables smaller and quicker to load. Otherwise, the variables could be in the `.data` segment with explicit initialization to zeroes; the program would be hard-pressed to tell the difference. (In detail, the address of the objects in `.bss` would probably be different from the address if it was in the `.data` segment.)\n\nIn the first program, `a` would be in the `.data` segment and `b` would be in the `.bss` segment of the executable. Once the program is loaded, the distinction becomes immaterial. At run time, `b` occupies `20 * sizeof(int)` bytes.\n\nIn the second program, `var` is allocated space and the assignment in `main()` modifies that space. It so happens that the space for `var` was described in the `.bss` segment rather than the `.data` segment, but that doesn't affect the way the program behaves when running.\n\n========================================\n\nCode:\n```text\n#include <stdio.h>\n#include <stdlib.h>\n\nint a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9};\nint b[20]; /* Uninitialized, so in the .bss and will not occupy space for 20 * sizeof (int) */\n\nint main ()\n{\n   ;\n}\n```\n\n```text\n#include <stdio.h>\n#include <stdlib.h>\nint var[10];  /* Uninitialized so in .bss */\nint main ()\n{\n   var[0] = 20  /* **Initialized, where this 'var' will be ?** */\n}\n```\n\n```text\n.data\n```\n\n```text\n.bss\n```\n\n```text\n.bss\n```\n\n```text\na\n```\n\n```text\n.data\n```\n\n```text\nb\n```\n\n```text\n.bss\n```\n\n```text\nfor(i=0; i<all_explicitly_initialized_objects; i++)\n{\n  .data[i] = init_value[i];\n}\n\nmemset(.bss, \n       0, \n       all_implicitly_initialized_objects);\n```\n\n```text\nmain()\n```\n\n```text\n.data\n```\n\n```text\n.bss\n```\n\n```text\ninit_value\n```\n\n```text\n.bss\n```\n\n```text\n.data\n```\n\n```text\n.bss\n```\n\n```text\n.bss\n```\n\n```text\n.bss\n```\n\n```text\n.data\n```\n\n```text\n.bss\n```\n\n```text\n.data\n```\n\n```text\n.bss\n```\n\n```text\n.data\n```\n\n```text\na\n```\n\n```text\n.data\n```\n\n```text\nb\n```\n\n```text\n.bss\n```\n\n```text\nb\n```\n\n```text\n20 * sizeof(int)\n```\n\n```text\nvar\n```\n\n```text\nmain()\n```\n\n```text\nvar\n```\n\n```text\n.bss\n```\n\n```text\n.data\n```\n\n```text\n.bss\n```\n\n```text\nSHT_NOBITS\n```\n\n```text\n.bss\n```\n\n```text\n.data\n```\n\n```text\n0\n```\n\n```text\nSHT_NOBITS\n```\n\n```text\nsh_size\n```\n\n```text\nSHT_NOBITS\n```\n\n```text\nsh_size\n```\n\n```text\nSHT_NOBITS\n```\n\n```text\nobjdump\n```\n\n```text\nreadelf\n```\n\n```text\n.bss\n```\n\n========================================\n\nComments:\n- You can read BSS as *Better Save Space*.\n- For example, consider having many uninitialized buffers 4096 bytes in length. Would you want all of those 4k buffers to contribute to the size of the binary? That would be a lot of wasted space.\n- @jonathen killer : Why is entire bss segment described by single number ??\n- @JonathanLeffler I mean all zero initialised static variable goes in bss . So shouldn't be its value be just zero ?and also why are they not given space on .data section how can doing so make it slow ?\n- @JonathanLeffler Please See This Question \" stackoverflow.com/questions/8385322/&hellip;\" . I think the selected best answer is wrong in explaining what is static memory allocation. Can You Confirm ? .\n- @SurajJain: the number stored is the number of bytes to be filled with zeros. Unless there are no such uninitialized variables, the length of the bss section won't be zero, even though all the bytes I the bss section will be zero once the program is loaded.\n- @JonathanLeffler Oh ok. I understand it . And Also why is using .data for this is slow.? And Please see the question i have written about above. As it will mislead many newcomers. Selected best answer is not correct.\n- @SurajJain: I've gone and edited that accepted answer because it was at best misleading. There's room to argue that my edit is too substantive. Note that the most up-voted answer was already better. the voting, not just the accepted vs non-accepted status.\n- @JonathanLeffler Thanks A lot. I just saw your edit. You are right that answer was so misleading. It misleaded me. And i then read the most upvoted answer and it cleared my doubts. Strange that wrong answer become highlight of all the answers and get selected too as best answer.\n- @SurajJain: The bytes in the .data section have to be read from 'disk' (spinning magnetic platters, or solid-state). It takes time to read the data. By contrast, reading 8 bytes for the size of the bss segment is trivial (compared with reading 64 MiB of zeroes on disk, say) and then setting the memory to zero. Plus it wastes disk space — why store 64 MiB of zeroes when you could store just 8 bytes saying \"there are 64 MiB of zeros\". Plus it takes space on backups — remember those?\n- @JonathanLeffler Many People Asked The Person Who wrote the answer to edit it .But he did not .I thought i might edit it . But then i did not believed in myself that answer is really wrong.\n- At this stage, it's probably better that I do the edit than you. Your edit would be reviewed and might not make it through ('changing the author's meaning'). For worse or worse, I have more rep, especially w.r.t the C tag, so I can maybe get away with it without causing too many ructions. But the answerer could decide my edit is not valid and roll the change back; I'd not gainsay him (but I would go add a downvote that I've not done yet).\n- @JonathanLeffler I Understand .I just wanted to see the best answer to be at least correct and not mislead people specially new comers . Also i want to know you said in bss it is stored how many bytes would be filled with zero. I want ask when program is run then for example if there are 2 static uninitialised or initialised to zero variable. The bss section holds the number of bytes to be written to 0. When the program is run where are the actual 0 bytes written in ,data segment or where because bss section only tells how many bytes to be zeroed it does not itself stores `static int i = 0`.\n- @JonathanLeffler Also If is write `static int i[100] = {1,2,3....}` .Then would my executable be 4*100 bytes bigger. ?? Or does in `.data`section it only tells where in memory the static array would be stored like from address `0X00000000` to `0x10232333` something like that. You said `.data` section is as big as the sum of sizes of the initialized variables.\n- The .bss section in the executable is simply a number. The .bss section in the in-memory process image is normally memory adjacent to the .data section and often the runtime .data section is combined with the .bss; there is no distinction made in the runtime memory. Sometimes, you can find where the bss started (`edata`). In practical terms, the .bss doesn't exist in memory once the process image is completed; the zeroed data is simple part of the .data section. But the details vary depending on the o/s etc.\n- The partially initialized array `i` would have to be stored in the data section because it must be self-contiguous, and there's no way to ensure that there isn't other initialized data before and after it, so the only way to make sure the correct values are stored is to keep the 100 `int` (400 bytes) worth of data in the .data section of the executable. If it were the last array in .data, then in theory you could have 3 `int` initializers in .data and 97 zeros in .bss. I can confidently predict that doesn't happen in real life.\n- So that means at run time all the static variables that are explicitly or implicitly initialised to zero will be store in `.data` section . Or in some section and there will be no `.bss` Sorry i am asking so many question i just want to get the things right in my head. Also Could You suggest some resources like book or thing for studying this. Pointers on c book how is it ?\n- @JonathanLeffler Here \"stackoverflow.com/questions/21350478/&hellip;\" is written that in .data section only it is written the memory address where the static initialized variables will go and the value itself are not stored.\n- @JonathanLeffler What i meant when i wrote `static int i[100] = {1,2,3,...}` is that all places are initialized to 1 , 2 ,3 and so on.\n- @JonathanLeffler Okay i understand . Thanks A lot for keeping with me so much. Also for correcting that answer it will help many.\n- To clarify: the only difference between .data and .bss is that on start-up, the \"copy-down\" can be run sequentially, hence faster. If it were not split into the two segments then the initialisation would have to skip the RAM spots belonging to the uninitialised variables, so wasting time.\n- Thank you for your explaination about the startup process, but what happens when a variable in `.bss` becomes initialized ? Does it overwrite the `0` and stays in `.bss` ? Is it removed from .bss and written in `.data` (thus shortening the `.bss` segment) ?\n- TLDR; use `.data` for constant and `.bss` for pointers","metadata":{"transformedAt":"2026-08-18T18:32:17.741Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":48,"totalLines":286,"estimatedTokens":2516}}17{"id":"stack-5026517","source":"stackoverflow","questionId":5026517,"title":"What's the difference between parse trees and abstract syntax trees (ASTs)?","tags":["compiler-construction","terminology","compiler-theory","abstract-syntax-tree","parse-tree"],"text":"Title: What's the difference between parse trees and abstract syntax trees (ASTs)?\nTags: compiler-construction, terminology, compiler-theory, abstract-syntax-tree, parse-tree\nSource: Stack Overflow\n\nQuestion:\nAre they generated by different phases of a compiling process? Or are they just different names for the same thing?\n\n========================================\n\nTop Answer:\nHere's an explanation of **parse trees** (concrete syntax trees, CSTs) and **abstract syntax trees** (ASTs), in the context of compiler construction. They're similar data structures, but they're constructed differently and used for different tasks.\n\n### Parse trees\n\nParse trees are usually generated as the next step after lexical analysis (which turns the source code into a series of tokens that can be viewed as meaningful units, as opposed to just a sequence of characters).\n\nThey are tree-like data structures that shows how an input string of terminals (source code tokens) has been generated by the grammar of the language in question. The root of the parse tree is the most general symbol of the grammar - the start symbol (for example, *statement*), and the interior nodes represent nonterminal symbols that the start symbol expands to (can include the start symbol itself), such as *expression*, *statement*, *term*, *function call*. The leaves are the terminals of the grammar, the actual symbols which appear as identifiers, keywords, and constants in the language / input string, e.g. **for**, **9**, **if**, etc.\n\nWhile parsing the compiler also performs various checks to ensure the correctness of syntax - and and syntax error reports can be imbedded into parser code.\n\nThey can be used for syntax-directed translation via syntax-directed definitions or translation schemes, for simple tasks such as converting an infix expression to a postfix one.\n\nHere's a graphical representation of a parse tree for the expression `9 - 5 + 2` (note the placement of the terminals in the tree and the actual symbols from the expression string):\n\n### Abstract syntax trees\n\nASTs represent the syntactic *structure of the some code*. The trees of programming constructs such as expressions, flow control statements, etc - grouped into operators (interior nodes) and operands (leaves). For example, the syntax tree for the expression `i + 9` would have the operator `+` as root, the variable `i` as the operator's left child, and the number `9` as the right child.\n\nThe difference here is that nonterminals and terminals don't play a role, as ASTs don't deal with grammars and string generation, but programming constructs, and thus they represent relationships between such constructs, and not the ways they are generated by a grammar.\n\nNote that the operators themselves are programming constructs in a given language, and don't have to be actual computational operators (like `+` is): `for` loops would also be treated in this way. For example, you could have a syntax tree such as `for [ expr, expr, expr, stmnt ]` (represented inline), where `for` is an *operator*, and the elements inside the square brackets are its children (representing C's `for` syntax) - also composed out of operators etc.\n\nASTs are usually generated by compilers in the syntax analysis (parsing) phase as well, and are used later for semantic analysis, intermediate representation, code generation, etc.\n\nHere's a graphical representation of an AST:\n\n========================================\n\nCode:\n```text\ngrammar Expr002;\n\noptions \n{\n    output=AST;\n    ASTLabelType=CommonTree; // type of $stat.tree ref etc...\n}\n\nprog    :   ( stat )+ ;\n\nstat    :   expr NEWLINE        -> expr\n        |   ID '=' expr NEWLINE -> ^('=' ID expr)\n        |   NEWLINE             ->\n        ;\n\nexpr    :   multExpr (( '+'^ | '-'^ ) multExpr)*\n        ; \n\nmultExpr\n        :   atom ('*'^ atom)*\n        ; \n\natom    :   INT \n        |   ID\n        |   '('! expr ')'!\n        ;\n\nID      : ('a'..'z' | 'A'..'Z' )+ ;\nINT     : '0'..'9'+ ;\nNEWLINE : '\\r'? '\\n' ;\nWS      : ( ' ' | '\\t' )+ { skip(); } ;\n```\n\n```text\nx=1\ny=2\n3*(x+y)\n```\n\n```text\n9 - 5 + 2\n```\n\n```text\ni + 9\n```\n\n```text\n+\n```\n\n```text\ni\n```\n\n```text\n9\n```\n\n```text\n+\n```\n\n```text\nfor\n```\n\n```text\nfor [ expr, expr, expr, stmnt ]\n```\n\n```text\nfor\n```\n\n```text\nfor\n```\n\n```text\nAbstract Syntax Tree\n```\n\n```text\nParse Tree\n```\n\n```text\nConcrete Syntax Tree\n```\n\n```text\nSyntax Tree\n```\n\n========================================\n\nComments:\n- Parse Tree is the result of your grammar with its artifacts (you can write an infinity of grammars for the same language), an AST reduce the Parse Tree the closest possible to the language. Several grammars for the same language will give different parse trees but should result to the same AST. (you can also reduce different scripts (different parse trees from the same grammar) to the same AST)\n- The link is not pointing to correct information\n- Thanks @HrishikeshDevhare. I've just removed it since there's no point keeping it around anymore.\n- It can still be accessed via Wayback Machine. web.archive.org/web/20020803201420/http://www.jguru.com/faq/&zwnj;&#8203;&hellip; I gave it a read. @KenWayneVanderLinde\n- How do you derive the AST from the parse tree? What's the method of simplifying a parse tree into an AST?\n- There is no specific algorithm to derive the AST from the parse tree. What goes into the AST is more of a personal preference but must contain enough info to accomplish the task. I excluded the parens from the AST by using the ANTLR ! operator in the grammar since they are not needed, but by default ANTLR would have included them. I think of the parse tree as giving you everything whether you need it or not, and the AST as giving you the bare minimum. Remember that you will traverse the trees a lot, so size matters.\n- You mean like CST (concrete syntax tree) vs AST (abstract syntax tree)?\n- Semantic actions/rules embedded in a parser or parser generator’s syntax files are the usual way of semantic analysis and creating an AST, while the parse tree is rarely, if ever constructed or used by user code, except perhaps for parser correctness verification.\n- Of interest: Abstract semantic graph\n- Of interest: Abstract and Concrete Syntax\n- I wish your answer was an accepted one. It is much more detailed, and better explained.\n- @Salil thanks!:) I wrote about these things on my blog as well: flowing.systems/tag/mcd\n- Is there any author who defined parse tree for the first time?","metadata":{"transformedAt":"2026-08-18T18:32:17.742Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":151,"estimatedTokens":1616}}18{"id":"stack-2508828","source":"stackoverflow","questionId":2508828,"title":"Where to learn about VS debugger 'magic names'","tags":["c#","compiler-construction","debugging"],"text":"Title: Where to learn about VS debugger 'magic names'\nTags: c#, compiler-construction, debugging\nSource: Stack Overflow\n\nQuestion:\nIf you've ever used Reflector, you probably noticed that the C# compiler generates types, methods, fields, and local variables, that deserve 'special' display by the debugger. For instance, local variables beginning with 'CS$' are not displayed to the user. There are other special naming conventions for closure types of anonymous methods, backing fields of automatic properties, and so on.\n\nMy question: where to learn about these naming conventions? Does anyone know about some documentation?\n\nMy objective is to make PostSharp 2.0 use the same conventions.\n\n========================================\n\nCode:\n```text\n0 --> short lived temporaries\n1 --> return value temporaries\n2 --> temporaries generated for lock statements\n3 --> temporaries generated for using statements\n4 --> durable temporaries\n5 --> the result of get enumerator in a foreach\n6 --> the array storage in a foreach\n7 --> the array index storage in a foreach.\n```\n\n```text\n1 --> the iterator state (\"state\")\n2 --> the value of current in an iterator (\"current\")\n3 --> a saved parameter in an iterator\n4 --> a hoisted 'this' in an iterator (\"this\")\n5 --> a hoisted local in an iterator\n6 --> the hoisted locals from an outer scope\n7 --> a hoisted wrapped value (\"wrap\")\n8 --> the closure class instance (\"locals\")\n9 --> the cached delegate instance (\"CachedAnonymousMethodDelegate\")\na --> the iterator instance (\"iterator\")\nb --> an anonymous method\nc --> anonymous method closure class (\"DisplayClass\")\nd --> iterator class\ne --> fixed buffer struct (\"FixedBuffer\")\nf --> anonymous type (\"AnonymousType\")\ng --> initializer local (\"initLocal\")\nh --> query expression temporary (\"TransparentIdentifier\")\ni --> anonymous type field (\"Field\")\nj --> anonymous type type parameter (\"TPar\")\nk --> auto prop field (\"BackingField\")\nl --> iterator thread id\nm --> iterator finally (\"Finally\")\nn --> fabricated method (\"FabricatedMethod\")\no --> dynamic container class (\"SiteContainer\")\np --> dynamic call site (\"Site\")\nq --> dynamic delegate (\"SiteDelegate\")\nr --> com ref call local (\"ComRefCallLocal\")\ns --> lock taken local (\"LockTaken\")\n```\n\n```text\nGeneratedNames.cs\n```\n\n```text\n__Deleted$\n```\n\n```text\nP<N>C__SI\n```\n\n========================================\n\nComments:\n- Thank you! I'll see if I can make PostSharp closure classes behave as nicely as what the C# compiler generates!\n- @SLaks: The opposite of a short-lived temporary. Durable temporaries are essentially local variables without names; they have a specific location on the stack that lives for the lifetime of the stack frame. Short-lived temporaries are just pushed on the stack when their storage is needed and then popped off when it is no longer needed. Durable temporaries are much easier to debug, but can make lifetimes of temporaries much longer. We generate durable temporaries when optimizations are off.\n- I have a concept similar to closure classes, but instead of having hoisted parameters as fields, I have them as local variables. This works pretty well for parameters, but how to tell the debugger that 'this' is not 'ldarg.0' but the local variable with index 4? Is there any magic name?\n- @Eric - could you update this response with names generated by C# 5.0 (async/await)? I've seen some new prefixes :)","metadata":{"transformedAt":"2026-08-18T18:32:17.742Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":5,"totalLines":75,"estimatedTokens":847}}19{"id":"stack-5657454","source":"stackoverflow","questionId":5657454,"title":"Is gcc C compiler written in C itself?","tags":["c","gcc","compiler-construction"],"text":"Title: Is gcc C compiler written in C itself?\nTags: c, gcc, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIs gcc C compiler written in C itself ? Or is it written in Assembly ? If the compiler is written in C, then what is the compiler used to compile the compiler code ?\n\n========================================\n\nTop Answer:\nOriginally it was written in some assembly language then it began to dog food itself.\n\n========================================\n\nCode:\n```text\n112 .opt\n    118 .def\n    140 .cc\n    185 .x\n    250 .exp\n    353 .md\n    366 .mm\n    414 .f\n    430 .f03\n    521 .m\n    625 .a\n   1082 .go\n   1371 .h\n   1602 .ads\n   1655 .adb\n   1828 .ada\n   3860 .f90\n  11231 .C        // C++ \n  23811 .c        // C\n```\n\n```text\ngcc-5.1.0-src/gcc/\n```\n\n```text\nGCC\n```\n\n========================================\n\nComments:\n- Yes it is (mostly). Of course, one needs a C compiler to compile C so, as with most compilers, there is a series of \"boot-strap\" phases. The general idea is covered at Installing GCC: Building and Installation of GCC - LFS.\n- Different brand workstations in the 1980s and early 90s had their own unix-like environments with c compiler, tools, etc... that were all a little bit different from each other. With the GCC source you could do an initial compile of gcc on one of these platforms, and then compile gcc with itself a few times to more or less check for a fixed point.\n- I haven't seen any references to RMS using assembler for the original bootstrap. What's your source for that? He had several other compilers to bootstrap with, so that seems a lot of work.\n- I though the term was bootstrap.\n- hahaha I read this a few weeks ago and thought you were out of you mind with that expression, didn't realize it was standard. Good Show sir ! en.wikipedia.org/wiki/Eating_your_own_dog_food\n- Thanks for the great bootstrapping link. I had assumed that compilers were always written in simpler languages all the way back to machine code for *sort of* this reason. (For example, what if you realise that the binary for your compiler contains a mistake? This provides a much stronger example of course.) So what are the advantages of writing the compiler in it's own language language? Do extremely paranoid people worry about this and build their compilers without \"self\" bootstrapping?\n- Some language groups consider it a mark of honor to be able to compile themselves. Go recently made a big push in this direction. Swift, on the other hand, notably has no current plans to rewrite their compiler in Swift. Note however, that GCC was always written in C. It just was compiled with other compilers. But if all C compilers were written in BCPL, that wouldn't fix anything regarding paranoia. It just moves the issue back one step. (You'd get the same paranoia benefits by just compiling GCC w/ clang.)\n- Note that gcc has a policy that gcc major version X can always be compiled with gcc major version X-1, so any new features added to the compiler in X can only be used in the gcc source itself from X+1. e.g. you should be able to compile any gcc version 9, no matter what the minor version number is, with any gcc 8, again no matter what the minor version is.\n- Adding to that, if you wanted to truly, provably solve the paranoia problem, you'd have to find the bottom turtle.\n- Note that even `.c` files in GCC contain C++ code because developers didn't rename the file when they migrate to C++\n- Is no one going to mention Ada, Go and at least two different Fortrans?\n- *Update: In 2022, all C++ source files in GCC were renamed to .cc*","metadata":{"transformedAt":"2026-08-18T18:32:17.742Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":60,"estimatedTokens":896}}20{"id":"stack-2676144","source":"stackoverflow","questionId":2676144,"title":"What is the difference between LR, SLR, and LALR parsers?","tags":["algorithm","parsing","compiler-construction","grammar","lr-grammar"],"text":"Title: What is the difference between LR, SLR, and LALR parsers?\nTags: algorithm, parsing, compiler-construction, grammar, lr-grammar\nSource: Stack Overflow\n\nQuestion:\nWhat is the actual difference between LR, SLR, and LALR parsers? I know that SLR and LALR are types of LR parsers, but what is the actual difference as far as their parsing tables are concerned?\n\nAnd how to show whether a grammar is LR, SLR, or LALR? For an LL grammar we just have to show that any cell of the parsing table should not contain multiple production rules. Any similar rules for LALR, SLR, and LR?\n\nFor example, how can we show that the grammar\n\n```\nS --> Aa | bAc | dc | bda\nA --> d\n```\n\nis LALR(1) but not SLR(1)?\n\n**EDIT (ybungalobill)**: I didn't get a satisfactory answer for what's the difference between LALR and LR. So LALR's tables are smaller in size but it can recognize only a subset of LR grammars. Can someone elaborate more on the difference between LALR and LR please? LALR(1) and LR(1) will be sufficient for an answer. Both of them use 1 token look-ahead and *both* are table driven! How they are different?\n\n========================================\n\nTop Answer:\nLALR parsers merge similar states within an LR grammar to produce parser state tables that are exactly the same size as the equivalent SLR grammar, which are usually an order of magnitude smaller than pure LR parsing tables. However, for LR grammars that are too complex to be LALR, these merged states result in parser conflicts, or produce a parser that does not fully recognize the original LR grammar.\n\nBTW, I mention a few things about this in my MLR(k) parsing table algorithm here.\n\n**Addendum**\n\nThe short answer is that the LALR parsing tables are smaller, but the parser machinery is the same. A given LALR grammar will produce much larger parsing tables if all of the LR states are generated, with a lot of redundant (near-identical) states.\n\nThe LALR tables are smaller because the similar (redundant) states are merged together, effectively throwing away context/lookahead info that the separate states encode. The advantage is that you get much smaller parsing tables for the same grammar.\n\nThe drawback is that not all LR grammars can be encoded as LALR tables because more complex grammars have more complicated lookaheads, resulting in two or more states instead of a single merged state.\n\nThe main difference is that the algorithm to produce LR tables carries more info around between the transitions from state to state while the LALR algorithm does not. So the LALR algorithm cannot tell if a given merged state should really be left as two or more separate states.\n\n========================================\n\nCode:\n```text\nS --> Aa | bAc | dc | bda\nA --> d\n```\n\n```text\nS → L = R | R\nL → * R | id\nR → L\n```\n\n```text\nS → L•= R\nR → L•\n```\n\n```text\nS → b d•a / $\nA → d• / c\n```\n\n```text\n•\n```\n\n```text\n=\n```\n\n```text\nR → L\n```\n\n```text\nR\n```\n\n```text\nR\n```\n\n```text\n=\n```\n\n```text\n$\n```\n\n```text\n/\n```\n\n```text\nA\n```\n\n```text\na\n```\n\n```text\ndc\n```\n\n```text\nS\n```\n\n```text\ndc\n```\n\n```text\nbdc\n```\n\n```text\nb\n```\n\n```text\nb\n```\n\n```text\nbdc\n```\n\n```text\nd\n```\n\n```text\nfrom copy import deepcopy\nimport pandas as pd\n\ndef update_items(I, C):\n    if len(I) == 0:\n         return C\n    for nt in C:\n         Int = I.get(nt, [])\n         for r in C.get(nt, []):\n              if not r in Int:\n                  Int.append(r)\n          I[nt] = Int\n     return I\n\ndef compute_action_goto(I, I0, sym, NTs): \n    #I0 = deepcopy(I0)\n    I1 = {}\n    for NT in I:\n        C = {}\n        for r in I[NT]:\n            r = r.copy()\n            ix = r.index('.')\n            #if ix == len(r)-1: # reduce step\n            if ix >= len(r)-1 or r[ix+1] != sym:\n                continue\n            r[ix:ix+2] = r[ix:ix+2][::-1]    # read the next symbol sym\n            C = compute_closure(r, I0, NTs)\n            cnt = C.get(NT, [])\n            if not r in cnt:\n                cnt.append(r)\n            C[NT] = cnt\n        I1 = update_items(I1, C)\n    return I1\n\ndef construct_LR0_automaton(G, NTs, Ts):\n    I0 = get_start_state(G, NTs, Ts)\n    I = deepcopy(I0)\n    queue = [0]\n    states2items = {0: I}\n    items2states = {str(to_str(I)):0}\n    parse_table = {}\n    cur = 0\n    while len(queue) > 0:\n        id = queue.pop(0)\n        I = states[id]\n        # compute goto set for non-terminals\n        for NT in NTs:\n            I1 = compute_action_goto(I, I0, NT, NTs) \n            if len(I1) > 0:\n                state = str(to_str(I1))\n                if not state in statess:\n                    cur += 1\n                    queue.append(cur)\n                    states2items[cur] = I1\n                    items2states[state] = cur\n                    parse_table[id, NT] = cur\n                else:\n                    parse_table[id, NT] = items2states[state]\n        # compute actions for terminals similarly\n        # ... ... ...\n                    \n    return states2items, items2states, parse_table\n        \nstates, statess, parse_table = construct_LR0_automaton(G, NTs, Ts)\n```\n\n```text\nG = {}\nNTs = ['E', 'T', 'F']\nTs = {'+', '*', '(', ')', 'id'}\nG['E'] = [['E', '+', 'T'], ['T']]\nG['T'] = [['T', '*', 'F'], ['F']]\nG['F'] = [['(', 'E', ')'], ['id']]\n```\n\n```text\ndef augment(G, S): # start symbol S\n    G[S + '1'] = [[S, '$']]\n    NTs.append(S + '1')\n    return G, NTs\n\ndef compute_closure(r, G, NTs):\n    S = {}\n    queue = [r]\n    seen = []\n    while len(queue) > 0:\n        r = queue.pop(0)\n        seen.append(r)\n        ix = r.index('.') + 1\n        if ix < len(r) and r[ix] in NTs:\n            S[r[ix]] = G[r[ix]]\n            for rr in G[r[ix]]:\n                if not rr in seen:\n                    queue.append(rr)\n    return S\n```\n\n```text\n#S --> Aa | bAc | dc | bda\n#A --> d    \nG = {}\nNTs = ['S', 'A']\nTs = {'a', 'b', 'c', 'd'}\nG['S'] = [['A', 'a'], ['b', 'A', 'c'], ['d', 'c'], ['b', 'd', 'a']]\nG['A'] = [['d']]\n```\n\n```text\n# S --> A \n# A --> a A b | c\nG = {}\nNTs = ['S', 'A']\nTs = {'a', 'b', 'c'}\nG['S'] = [['A']]\nG['A'] = [['a', 'A', 'b'], ['c']]\n```\n\n```text\ndef parse(input, parse_table, rules):\n    input = 'aaacbbb$'\n    stack = [0]\n    df = pd.DataFrame(columns=['stack', 'input', 'action'])\n    i, accepted = 0, False\n    while i < len(input):\n        state = stack[-1]\n        char = input[i]\n        action = parse_table.loc[parse_table.states == state, char].values[0]\n        if action[0] == 's':   # shift\n            stack.append(char)\n            stack.append(int(action[-1]))\n            i += 1\n        elif action[0] == 'r': # reduce\n            r = rules[int(action[-1])]\n            l, r = r['l'], r['r']\n            char = ''\n            for j in range(2*len(r)):\n                s = stack.pop()\n                if type(s) != int:\n                    char = s + char\n            if char == r:\n                goto = parse_table.loc[parse_table.states == stack[-1], l].values[0]\n                stack.append(l)\n                stack.append(int(goto[-1]))\n        elif action == 'acc':  # accept\n            accepted = True\n        df2 = {'stack': ''.join(map(str, stack)), 'input': input[i:], 'action': action}\n        df = df.append(df2, ignore_index = True)\n        if accepted:\n            break\n        \n    return df\n\nparse(input, parse_table, rules)\n```\n\n```text\na^ncb^n, n >= 1\n```\n\n```text\na^2cb^2\n```\n\n```text\na^2cb^2\n```\n\n========================================\n\nComments:\n- well, even I'm looking for a proper answer on this, LALR(1) is just a slight modification of LR(1), where the table size is reduced so that we can minimize the memory usage ...\n- GCC used to parse C++ using Bison == LALR. You can always augment your parser with extra goo to handle the cases (lookahead, is-this-a-typename) that give you heartache. The question is \"How painful a hack?\" For GCC it was pretty painful, but they made it work. That doesn't mean this is recommended, which is my point about using GLR.\n- I don't understand how using GLR helps you with C++. If you don't know whether something is a type name or not, then you just don't know how to parser `x * y;` -- how does using GLR help with that?\n- The point is that the GLR parser will produce *both* parses (as \"ambiguous subtree(s)\" in an integrated parse \"tree\" (really DAG). You can resolve which of the subrees you want to keep, later, by bringing in other context information. Our C++ parser is remarkably simply regarding this issue: it doesn't try to *solve* the problem. That means we don't have to tangle symbol table construction with with parsing, so both our parser and the symbol table construction for C++ are individually clean and consequently much each to build and maintain.\n- +1 I like the Honalee idea. My G/L(AL)R parser generator had the seeds of something like this in it; it produces the minimal LALR machine, and then I was going to split states where there were conflicts, but I never carried through. This looks like a nice way to produce an minimal size \"LR\" like set of parse tables. While it won't help GLR in terms of what it can parse, it may cut the number of parallel parses that GLR has to carry and that would be useful.\n- This is not accurate","metadata":{"transformedAt":"2026-08-18T18:32:17.742Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":31,"totalLines":310,"estimatedTokens":2286}}21{"id":"stack-6319086","source":"stackoverflow","questionId":6319086,"title":"Are GCC and Clang parsers really handwritten?","tags":["c","parsing","compiler-construction","compilation"],"text":"Title: Are GCC and Clang parsers really handwritten?\nTags: c, parsing, compiler-construction, compilation\nSource: Stack Overflow\n\nQuestion:\nIt seems that GCC and LLVM-Clang are using **handwritten recursive descent parsers**, and **not** machine generated, Bison-Flex based, bottom up parsing.\n\nCould someone here please confirm that this is the case?\nAnd if so, why do mainstream compiler frameworks use handwritten parsers?\n\n**Update** : interesting blog on this topic here\n\n========================================\n\nTop Answer:\nThere's a folk-theorem that says C is hard to parse, and C++ essentially impossible.\n\n**It isn't true.**\n\nWhat is true is that C and C++ are pretty hard to parse using LALR(1) parsers without hacking the parsing machinery and tangling in symbol table data. GCC in fact used to parse them, using YACC and additional hackery like this, and yes it was ugly. *Now GCC uses handwritten parsers, but still with the symbol table hackery. The Clang folks never tried to use automated parser generators; AFAIK the Clang parser has always been hand-coded recursive descent.*\n\nWhat is true, is that C and C++ are relatively easy to parse with stronger automatically generated parsers, e.g., GLR parsers, and you don't need any hacks. The Elsa C++ parser is one example of this. Our C++ Front End is another (as are all our \"compiler\" front ends, GLR is pretty wonderful parsing technology).\n\nOur C++ front end isn't as fast as GCC's, and certainly slower than Elsa; we've put little energy into tuning it carefully because we have other more pressing issues (nontheless it has been used on millions of lines of C++ code). Elsa is likely slower than GCC simply because it is more general. Given processor speeds these days, these differences might not matter a lot in practice.\n\nBut the \"real compilers\" that are widely distributed today have their roots in compilers of 10 or 20 years ago or more. Inefficiencies then mattered much more, and nobody had heard of GLR parsers, so people did what they knew how to do. Clang is certainly more recent, but then folk theorems retain their \"persuasiveness\" for a long time.\n\nYou don't have to do it that way anymore. You can very reasonably use GLR and other such parsers as front ends, with an improvement in compiler maintainability. \n\nWhat *is* true, is that getting a grammar that matches your friendly neighborhood compiler's behavior is hard. While virtually all C++ compilers implement (most) of the original standard, they also tend have lots of dark corner extensions, e.g., DLL specifications in MS compilers, etc. If you have a strong parsing engine, you can\nspend your time trying to get the final grammar to match reality, rather than trying to bend your grammar to match the limitations of your parser generator.\n\nEDIT November 2012: Since writing this answer, we've improved our C++ front end to handle full C++11, including ANSI, GNU, and MS variant dialects. While there was lots of extra stuff, we don't have to change our parsing engine; we just revised the grammar rules. We *did* have to change the semantic analysis; C++11 is semantically very complicated, and this work swamps the effort to get the parser to run.\n\nEDIT February 2015: ... now handles full C++14. (See get human readable AST from c++ code for GLR parses of a simple bit of code, and C++'s infamous \"most vexing parse\").\n\nEDIT April 2017: Now handles (draft) C++17.\n\n========================================\n\nCode:\n```text\nfunc<4 > 2>\n```\n\n```text\nfunc<4 > 2>\n```\n\n```text\nfunc<1>\n```\n\n```text\nfunc<4 > 2 > 1 > 3 > 3 > 8 > 9 > 8 > 7 > 8>\n```\n\n```text\nO(n)\n```\n\n========================================\n\nComments:\n- Almost all the mainstream compilers are using handwritten parsers. What's a problem with that?\n- you have to do it (semi-) manually if you need performance.\n- And not only performance - better error messages, ability to recover, etc.\n- What about MS VisualStudio? though not open-sourced, could someone from MS verify that they too are using a hand written recursive descent parser?\n- @GeneBushuyev, from the GCC wiki: \"...Although **timings showed a 1.5% speedup**, the main benefits are facilitating of future enhancements ...\" this speedup seems rather marginal ...\n- Why is this surprising? Even my recursive command line parser is hand written...\n- No free version of yacc in 1987? I think there were free versions when yacc was first delivered under Unix back in the 70s. And IIRC (other poster seems the same), GCC *used* to have a YACC-based parser. I heard the excuse for changing it was to get better error reporting.\n- I'd like to add it's often easier to generate good error messages from a handwritten parser.\n- Your point on Timing is inaccurate. GCC used to have YACC based parser, but this was replaced with a handwritten recursive descent parser, later on.\n- Does that mean that ObjC, C and C++ has LL(k) Grammars?\n- No: even C, the simplest of the three, has an ambiguous grammar. For example, `foo * bar;` could parse as either a multiplication expression (with the result unused), or a declaration of a variable `bar` with type pointer-to-`foo`. Which one is correct depends on whether a `typedef` for `foo` is in scope at the time, which is not something that can be determined with any amount of lookahead. But that just means that the recursive descent parser needs some ugly extra machinery added to handle that.\n- I can confirm from empirical evidence, that C++11, C, and Objective C have context free grammars that a GLR parser can handle.\n- Regarding context sensitiveness, this answer claims neither: that parsing these languages is likely Turing-complete.\n- PostScript: Just as getting the grammar to match what the vendors really do is harder, get name and type resolution to match the different vendor's interpretation of the C++11 manual is even harder, because the only evidence you have are programs that compile slightly differently, if you can find them. We're largely past that as of Aug 2013 for C++11 proper, but I despair a little at the C++ committee which seems hell-bent on producing an even larger (and from experience, more confusing) standard in the form of C++1y.\n- I’d really like to know: How do you handle that `foo * bar;` ambiguity?\n- @Martin: our parser parses it *both* ways, producing a tree containing special \"ambiguity nodes\" whose children are the alternative parses; the children do maximal sharing of their children so we end up with a DAG instead of a tree. *After* parsing completes, we run an attribute grammar evaluator (AGE) over the DAG (fancy name for \"walk the tree and do stuff\" if you don't know it) which computes the types of all declared identifiers. ...\n- ... The ambiguous children can't both be type-consistent; the AGE on discovering an ambiguous child that cannot be sensibly typed simply deletes it. What is left are the well-typed children; thus, we have determined which parse of \"foo*bar;\" is correct. This trick works for all kinds of crazy ambiguities found in the real grammars we build for the real dialects of C++11, and *completely* separates parsing from semantic analysis for names. This clean separation means lots less engineering work to do (no tangles to debug). See stackoverflow.com/a/1004737/120163 for more discussion.\n- If you want to see a tree with ambiguity nodes, this page shows an Oberon grammar and parse tree with a few ambiguity nodes: semdesigns.com/Products/DMS/DMSParsers.html\n- C++ is bloody hard to parse, and I *would* say it's essentially impossible. Search for \"Turing-complete semantic predicates\" in this page (about 2/3rds down): blog.reverberate.org/2013/09/&hellip; ; the link in that list item is also relevant.\n- @TimČas: What are you trying to say? Yes, you can probably write programs that no compiler can actually parse, esp. if you use templates to encode algorithms with enormously long runtime. To the extent that C++ is parseable in practice, there are compilers that do it, and tools like ours that do it. Beyond this practical extent nobody cares.\n- @IraBaxter: I think the fact that, for N different compilers, C++ is parsed (and compiled) in N different ways because of its complexity, states that there *ARE* problems. And I can guarantee you that the people who run into these bugs do care; they've just spent hours of their life [if they're lucky] trying to debug a program that didn't have a problem (their compiler did).\n- @TimCas: You seem to be confusing \"bugs in the implementation\" with \"impossible to parse\". We'll agree the standard is complicated, and even probably broken in many dark corners. Have you checked Java standard/compilers recently? (Have you looked at PHP at all???) Can you exhibit any specific compiler (even non-C++) that has no bugs in it? *What is your point?*\n- @IraBaxter: I'm not mixing them up. I'm just replying to your comment: \"To the extent that C++ is parseable in practice, there are compilers that do it, and tools like ours that do it. Beyond this practical extent nobody cares.\". People do care when parsing is so complicated (almost?) nobody gets it right.\n- @TimCas: Tell that to the C++ committee. And, to every other language standards committee, and compiler implementers. NONE OF THEM ARE PERFECT. NOBODY GETS IT RIGHT. So what?\n- @TimCas: Actually, I'm with you on railing at the apparant stupidity of designing language syntax (and semantics) that are so complicated that is is so hard to get it right (yes, C++ language suffers here badly). I wish language design committees would design syntax so simpler parsing technologies would work, and explicitly define the language semantics and check it with some semantic analysis tools. Alas, the world doesn't seem to be like that. So, I take the view that you build what you have to build as well as you can, and get on with life, in spite of the awkwardness.\n- @IraBaxter: There isn't much choice regarding \"dealing with it\", now is there? My only argument is that parsing C++ *is* essentially impossible. I've provided examples of why it is so; you somehow seem to agree with me on that, and yet you end up being *very* aggressive in disagreement. I've provided very clear examples of C++ parsing issues, which show why it's *essentially* impossibly to do right (and *actually* impossible to do at all without a turing-complete parser). *Quod erat demonstrandum.*\n- Clang uses a hand-written parser partly because it's easy to reuse the same core for C, C++, *and* Objective C, and partly because they're trying to support virtually everything out there, from MSVC's extensions to GCC extensions to their own custom extensions and allow them all to be individually enabled via command-line arguments.\n- @kirbyfan64sos: One can do that just fine with grammars. We use a grammar-based approach because its easy to reuse the same grammar for C (in all of its dialects) *and* Objective C, and C++ with MSVC's extensions to GCC extensions as well as our own custom extensions. Which language/dialect we use for a specific tool is individually enabled by tool-generation time command-line arguments so we don't pay a runtime cost checking for all the alternative dialects when the generated tools runs. And our dev team is LOT smaller than Clangs.\n- @IraBaxter how does the advertisement for your company's products enhance this answer?\n- Adverstisement? The motivating question is about whether one can write a parser for C or C++ with parser generators, and mentions a variety of front end tools that do not. Surely that is obvious. This answer shows that one can do it well and produce production tools; many readers think this is interesting based on the vote count. How does your objection to the fact that the tool happens to be commercial improve this discussion?\n- Custom hand-written parsers allow to produce much more sensible errors/warnings. This was always a weak suit for parser generators (which usually can only spew something like \"expected ; but found foo\" unhelpful errors until specifically massaged).\n- @DanM: That's the usual excuse for custom parsers. What I think this really means is that people that hand build such parsers spend more energy trying to produce good error messages, whereas people that use parser generators, and don't understand how they work/aren't willing to enhance it help with error diagnostics, simply blow off the extra work. Its one man's opinion, but I think it is laziness on the part of parser generator users. See my comment on Doug's answer.\n- @IraBaxter some errors require more semantic knowledge, not just syntax/parser one. Also, if the effort it requires to maintain good diagnostics for generated and handwritten parsers are comparable, when why bother? I've yet to see a parser generator that'd make it easy without becoming an expert in it. I mean it can be explained as laziness. GCC maintainers were lazy. Programmers were lazy. It's what drives IT.\n- @IraBaxter **10 years latter with i7 32 GB RAM and large SSD compile-time performance still matters.** At least with Visual Studio, in a medium size project, one can easily make massive changes such that IntelliSense is not able to keep up. As a comparison, compilation of C# code is at least 10 times faster while the final application has similar performance.\n- Yes, semantic analysis is harder by a lot. We have some 4000 lines of grammar rules that comprise our C++11 grammar, and some 180,000 lines of attribute grammar code for the \"semantic analyses\" Doub lists above, with another 100,000 lines of supporting code. Parsing really isn't the problem, although it is hard enough if you start on the wrong foot.\n- I'm not so sure that hand-written parsers are *necessarily* better for error reporting/recovery. It does appear that people have put more energy into such parsers than into enhancing parsers produced by automatic parser generators in practice. There seems to be pretty good research on the topic; this particular paper has really caught my eye: M.G. Burke, 1983, A practical method for LR and LL syntactic error diagnosis and recovery, PhD thesis, Department of Computer Science, New York University, See archive.org/details/practicalmethodf00burk\n- ... continuing this thought train: if you are willing to modify/extend/customize your hand-built parser to check for special cases for better diagnosis, then you should be willing to make equal investment in better diagnoses of a mechanically generated parser. For any special parse that you can encode for the manual one, you can code a check for the mechanical one, too (and for (G)LR parsers, you can pretty much do this as semantic checks on reductions). To the extent that seems unappetizing, one is just being lazy but that's not an indictment of the mechanically generated parsers IMHO.\n- @IraBaxter I would be really glad if you could some of the resources with us about `writing a decent parser by hand in C`.\n- If you want to build toys, that's an OK path. Its workable for real languages but parser generators are really the right way to handle complex grammars; I've made that point already in another answer to this question. If you want to write recursive descent parsers, another of my SO answers tells you how to do it. See stackoverflow.com/a/2336769/120163 Price: You get to deal with the complicated cases that parser generators handle for you.\n- \"for me, it is far more ugly\". What I can tell you is that engineering of a production quality parser using GLR and delay ambiguity resolution is practical with a really small team. All the other solutions I have seen have involved years of gnashing teeth in public over the backflips and hacks required to make it work with LR, recursive descent, you name it. You can postulate lots of other cool new parsing technologies, but as far as I can tell, that's just more gnashing of teeth at this point. Ideas are cheap; execution is dear.\n- @IraBaxter: Rats! citeseerx.ist.psu.edu/viewdoc/&hellip;\n- @Fizz: Interesting paper on parsing Fortress, a complex scientific programming langauge. They said several things of note: a) classic parser generators (LL(k), LALR(1)) can't handle tough grammars, b) they tried GLR, had troubles with scale but the developers were inexperienced so they didn't complete [that isn't GLR's fault] and c) they used a backtracking (transactional) Packrat parser and put a lot of effort into it including work to produce better error messages. Regarding their example of parsing \"{|x||x←mySet,3|x}\", I beleive GLR would do it just fine and it doesn't need spaces.\n- `func 2>` is not `func`. That code does not compile. The first `>` close the template.\n- This question already has a very high quality answer, what you are trying to add?","metadata":{"transformedAt":"2026-08-18T18:32:17.742Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":108,"estimatedTokens":4178}}22{"id":"stack-17410081","source":"stackoverflow","questionId":17410081,"title":"Why is it impossible to build a compiler that can determine if a C++ function will change the value of a particular variable?","tags":["c++","compiler-construction"],"text":"Title: Why is it impossible to build a compiler that can determine if a C++ function will change the value of a particular variable?\nTags: c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI read this line in a book: \n\n It is provably impossible to build a compiler that can actually\n determine whether or not a C++ function will change the value of a\n particular variable.\n\nThe paragraph was talking about why the compiler is conservative when checking for const-ness. \n\n**Why is it impossible to build such a compiler?** \n\nThe compiler can always check if a variable is reassigned, a non-const function is being invoked on it, or if it is being passed in as a non-const parameter...\n\n========================================\n\nTop Answer:\nImagine such compiler exists. Let's also assume that for convenience it provides a library function that returns 1 if the passed function modifies a given variable and 0 when the function doesn't. Then what should this program print?\n\n```\nint variable = 0;\n\nvoid f() {\n if (modifies_variable(f, variable)) {\n /* do nothing */\n } else {\n /* modify variable */\n variable = 1;\n }\n}\n\nint main(int argc, char **argv) {\n if (modifies_variable(f, variable)) {\n printf(\"Modifies variable\\n\");\n } else {\n printf(\"Does not modify variable\\n\");\n }\n\n return 0;\n}\n```\n\n========================================\n\nCode:\n```text\nvoid foo() {\n    if (bar() == 0) this->a = 1;\n}\n```\n\n```text\nfoo\n```\n\n```text\na\n```\n\n```text\nbar\n```\n\n```text\nvoid maybe(int& val) {\n    cout << \"Should I change value? [Y/N] >\";\n    string reply;\n    cin >> reply;\n    if (reply == \"Y\") {\n        val = 42;\n    }\n}\n```\n\n```text\ndo tons of complex stuff\nif (condition on result of complex stuff)\n{\n    change value of x\n}\nelse\n{\n    do not change value of x\n}\n```\n\n```text\nx\n```\n\n```text\ndo tons of complex stuff\n```\n\n```text\nvoid foo(int& x)\n {\n    ifstream f(\"f.dat\", ifstream::binary);\n    f.read((char *)&x, sizeof(x));\n }\n```\n\n```text\nvoid bar(int& x)\n{\n  foo(x);\n}\n```\n\n```text\nx\n```\n\n```text\nbar\n```\n\n```text\nint variable = 0;\n\nvoid f() {\n    if (modifies_variable(f, variable)) {\n        /* do nothing */\n    } else {\n        /* modify variable */\n        variable = 1;\n    }\n}\n\nint main(int argc, char **argv) {\n    if (modifies_variable(f, variable)) {\n        printf(\"Modifies variable\\n\");\n    } else {\n        printf(\"Does not modify variable\\n\");\n    }\n\n    return 0;\n}\n```\n\n```text\nint y;\n\nint main(int argc, char *argv[]) {\n   if (argc > 2) y++;\n}\n```\n\n```text\ny\n```\n\n```text\n//   g++ -o foo foo.cc\n\n#include <iostream>\nvoid const_func(const int&a, int* b)\n{\n   b[0] = 2;\n   b[1] = 2;\n}\n\nint main() {\n   int a = 1;\n   int b = 3;\n\n   std::cout << a << std::endl;\n   const_func(a,&b);\n   std::cout << a << std::endl;\n}\n```\n\n```text\n1\n2\n```\n\n```text\nconst\n```\n\n```text\nfoo(int x){\n   if(x)\n       y=1;\n}\n```\n\n```text\nint y;\nmain(){\n    int x;\n    ...\n    run the program normally\n    ...\n    foo(x);\n}\n```\n\n```text\nstatic int global;\n\nvoid foo()\n{\n}\n```\n\n```text\nstatic int global;\n\nint foo()\n{\n    if ((rand() % 100) > 50)\n    {\n        global = 1;\n    }\n    return 1;\n```\n\n========================================\n\nComments:\n- First thing that comes to my mind is dynamic link libraries. If I compile code on my machine, and you compile code on your machine, and we link them *at run time*, how could your compiler know if I modified variables or not?\n- If I ask for input in my program and based on that, I either modify or not not modify a given variable. Then it can never be predicted at compile-time whether I will change the value of that variable not. It might, but you can't say for sure it \"will\".\n- @MooingDuck Exactly this. More broadly, the compiler does not compile the function individually, but compiles it as part of a broader picture which may not all be within the compiler's scope.\n- but i am not getting why anyone will want to know that the function is changing some values during its execution,as there are some other ways of keeping the track of the state of variable or its value.\n- @ MooingDuck Does object code mark if a particular function is const? Also, in your header file, if you marked one or more of your functions as const or one of the parameters const, then the compiler can determine if your code modifies variables or not. Right? If yes, then even if you compiled your code on your machine and gave me your header, my compiler can determine whether the variable changed or not. Right?\n- \"impossible\" may be an overstatement - \"computationally infeasible\" (as in NP-hard) may be a better characterization, but is a little harder for the student to grasp. Imagine a linked list or other abstract data structure. If I call a function that changes one node in that list/tree/whatever, how could a compiler ever hope to prove exactly which node got modified (and maybe more importantly, which ones didn't) without basically fully simulating the program with the expected input, all while not taking 3 days to compile one source file...\n- @twalberg Impossible is not an overstatement, the Halting problem applies here as several answers explain. It is simply not possible to algorithmically fully analyze a general program.\n- @Fiktik Agreed for the completely general/arbitrary case. However, many well-constructed programs doing proper input validation don't have to deal with \"arbitrary input\", and so may not be instances of the full halting problem, but some reduced subset that may very well be provable, although not necessarily efficiently...\n- @twalberg Compilers that only compile a subset of valid programs aren't very useful.\n- @twalberg I agree there definitely is a sublanguage where the halting problem does not apply. However from Turing's proof we can deduce that such sublanguage is no longer turing-complete, so really not so interesting.\n- If compilers could do that kind of analysis about how a program will behave, we wouldn't need to execute our programs. (I'm sure that's a gross overgeneralization.)\n- I think this is a variation of the so-called \"halting problem\". Given an arbitrary program in a sufficiently rich programming language it becomes impossible to determine if control ever flows through a particular point. Note that this is not simply \"difficult\", it's truly impossible.\n- You seem to forget that this is not pure math problem - \"The problem is to determine, given a program *and an input to the program*, whether the program will eventually halt when run with that input.\" When one says \"While deciding whether these programs (such as \"Hello world\") halt is simple, more complex programs prove problematic.\" - this means that you go into the territory of mathematical logic.\n- @Caleb While a \"correct\" compiler should be able to compile any valid program, the subset of valid programs that are actually useful is very small (but still infinite, and with no possible way to characterize the set), so even a \"broken\" compiler that can only handle \"useful\" programs (whatever that means) is still of some use... Most compilers strive to be \"correct\", but due to bugs and other shortcomings never quite completely reach that goal...\n- `void UserChange() { int* p; scanf(\"%p\", &p); (*p)++; }` impossible to say what will be changed here at runtime.\n- That book's text is unclear which obfuscates the issue. It is trying to say, \"let's get an infinite number of monkeys to write every conceivable C++ function which could ever be written. There will be cases where if we pick a variable that (some particular function the monkeys wrote) uses, we can't work out whether the function will change that variable.\" Of course for some (even many) functions in any given application, this can be determined by the compiler, and very easily. But not for all (or necessarily most).\n- For same reason you can't detect infinite loop, (can you notice there is a break key on keyboard, you have to press yourself if you feels you your code in infinite loop)\n- Can someone explain why the last sentence of my question is wrong? Why is this check not enough? Here's the last question: The compiler can always check if its being reassigned, a non-const function is being invoked on it or if it is being passed in as a non-const parameter. Why is this check not sufficient to determine const-ness?\n- Would this change with proper quantum computing algorithms? It sounds like a perfect application for quantum computers.\n- @mrsoltys, quantum computers are \"only\" exponentially faster for some problems, they can not solve undecidable problems.\n- @mrsoltys Those exponentially complicated algorithms (like factoring) is perfect for quantum computers, but halting problem is a logical dilemma, it's not computable no matter what kind of \"computer\" you have.\n- @mrsoltys, just to be a smartass, yes, it would change. Unfortunately, it would mean the algorithm is both terminated and still running, unfortunately, you can't tell which without directly observing, by which you affect the actual state.\n- Can you explain why is this problem the same as the halting problem?\n- @DavidBrown, In order to decide if a function will return a given value, you have to determine if that function can end. In the example given, it's necessary to know if there exists a scenario in which bar() will return 0. This requires solving the Halting Problem.\n- I don't think you have to invoke the halting problem to explain this limitation. Isn't it sufficient just to say that a function could modify a variable or not depending run-time conditions, which are not known at compile time? E.g. `foo(int x) { if (x < 1) a = 3;`\n- \"... you can't write a program that will determine whether any given program will terminate\" - without actually *executing* the program that is.\n- @Thorbj&#248;rnRavnAndersen: O.K., so, suppose I'm executing a program. How exactly do I determine whether it will terminate?\n- @Thorbj&#248;rnRavnAndersen But if you *actually* execute the program, and it doesn't terminate (e.g. an infinite loop), you will never find out that it doesn't terminate... you just keep on executing one more step, because it could be the last one...\n- Good answer, @Caleb. It may be worth noting, however, that while it is impossible to write a compiler that can determine whether or not a function changes a variable for *all* possible functions, it is possible to write a compiler that can determine that *certain* functions will modify a varaible, or that they will never modify a variable.\n- Basically the statement in the original question does *not* mean that a compiler, when asked \"Does this function modify X?\" can only ever answer \"I don't know.\" However, what it *does* mean is that compilers can group functions into \"yes\", \"no\", and \"I don't know\" categories, but for the set of all possible funtion the \"I don't know\" category can never be empty; there will always be *some* functions for which it cannot tell.\n- Thanks, @DaveL. I tried to write as much -- I emphasized *in some cases*. See also my response to twalberg's comment on the question.\n- @ruakh not the point. You cannot determine *IF* it will terminate without running it until it terminates.\n- @Thorbj&#248;rnRavnAndersen: You also can't determine *IF* it will terminate *by* running it until it terminates, because -- what if it doesn't? In other words -- you can't write a program that will examine some other program and is guaranteed to print \"Halts\" if it halts and \"Doesn't halt\" if it doesn't.\n- @BenJackson I agree, because even if you can prove a function halts, you still may not be able to prove it doesn't modify a variable.\n- @DavidBrown The connection to the halting problem isn't that you can't decide that the function halts or not (though there's that too), it's that you can't decide *for all valid functions* whether a given function does *any* specific thing. The same argument (really not the one I make above) that proves that you can't decide about halting can also be used to show that you can't decide whether the function calls a function, changes a variable, etc. nightcracker's answer illustrates this nicely.\n- @BenJackson Aliasing adds complexity, but the problem remains even if you remove aliasing. I chose to focus on the fact that the compiler has insufficient information because it's easy to understand, and on the halting problem because it seals the deal. But another answer covering aliasing would certainly be helpful.\n- There appears to be some confusion between the Halting Problem - which means that there are some things that are *uncomputable* and questions of difficulty (e.g. NP-Hard problems) which are *impractical* to compute. The Halting Problem arises from a fundamental property of computing (and indeed of mathematics of which computing is a subset - see en.wikipedia.org/wiki/G%C3%B6del%27s_incompleteness_theorem) - this is a function of the program *and* the input - even knowing both you cannot determine if the program will halt.\n- @Dale M - You may say that \"Hello world\" program will halt very quickly. This is a fundamental property of computing.\n- @SChepurin: If you make \"Hello world\" wait for external event to terminate, the it will be impossible to predict whether it will ever stop, because external event might not happen.\n- \"It is certainly possible to build a compiler that checks whether or not a C++ function can change the value of a particular variable\" No, it is not. See Caleb's answer. For a compiler to know if foo() can change a, it would have to know if it is possible for bar() to return 0. And there is no computable function that can tell all possible return values of any computable function. So there exist code paths such that the compiler won't be able to tell if they will ever be reached. If a variable is changed only in a code path that can't be reached it won't change, but a compiler won't detect it\n- @MartinEpsz By \"can\" I mean \"is allowed to change\", not \"can possibly change\". I believer that this is what OP had in mind when talking about `const`-ness checks.\n- @dasblinkenlight I would have to agree that I believe the OP may have meant the first one, \"is allowed o change\", or \"may or may not change\" vs. \"will definitely not change\". Of course I can't think of a scenario where this would be an issue. You could even modify the compiler to simply answer \"may change\" on any function containing either the identifier or a call to a function which has a \"may change\" answer attribute. That said, C and C++ are horrible languages to try this with, since they have such a loose definition of things. I think this is why const-ness would be an issue in C++ at all.\n- @MartinEpsz: \"And there is no computable function that can tell all possible return values of any computable function\". I think that checking \"all possible return values\" is an incorrect approach. There are mathematical systems (maxima, mathlab) that can solve equations, which means it would make sense to apply similar approach to functions. I.e. treat it as an equation with several unknowns. The problems are flow control + side effects => unsolvable situations. IMO, without those (functional language, no assignment/side effects), it would've possible to predict which path program will take\n- The compiler can know that x is not changing in bar if bar x is passed as pass-by-reference-to-const, right?\n- Yes, but if I add a `const_cast` in foo, it would still make `x` change - I'd be in breach of the contract that says that you are not to change `const` variables, but since you can convert anything to \"more const\", and `const_cast` exists, the designers of the language surely had the idea in mind that sometimes there are good reasons to believe that `const` values may need changing.\n- @MatsPetersson: I believe that if you const_cast you get to keep all the pieces that break because the compiler may, but does not have to compensate for that.\n- @ZanLynx: Yes, I'm sure that's correct. But at the same time, the cast does exist, which means that someone who designed the language did have some sort of idea that \"we may need this at some point\" - which means it's not meant to not do anything useful at all.\n- One thing I wish was supported in languages would be a distinction between ephemeral, returnable, and persistable references (or pointers). Ephemeral references may only be copied to other ephemeral references, returnable ones may be copied to ephemeral or returnable ones, and persistable ones can be copied any which way. The return value of a function will be constrained by the most restrictive of the arguments that are passed as \"returnable\" parameters. I consider it unfortunate that in many languages, when one passes a reference there's nothing to indicate how long it may be used.\n- That would certainly be useful. There are of course patterns for this, but in C++ (and many other languages) it is always possible to \"cheat\".\n- A major way in which .NET is superior to Java is that it has a concept of an ephemeral reference, but unfortunately there is no way for objects to expose properties as ephemeral references (what I'd really like to see would be a means by which code using a property would pass an ephemeral reference to a code (along with temporary variables) that should be used to manipulate the object.\n- Nice! The I am a liar paradox as written by a programmer.\n- It's actually just a nice adaption of the famous proof for undecidability of the halting problem.\n- In this concrete case \"modifies_variable\" should return true: There's at least one execution path in which the variable is indeed modified. And that execution path is reached after a call to an external, non-deterministic function - so whole function is non-deterministic. For these 2 reasons, the compiler should take the pesimistic view and decide it does modify the variable. If the path to modifying the variable is reached after a deterministic comparison (verifiable by the compiler) yields false (i.e. \"1==1\") then compiler could safely say such function never modifies variable\n- @JoePineda How is that the pessimistic view? If it was, say, current microsecond instead of an arbitrary integer, wouldn't the \"pessimistic\" view be \"it's still the same\"? You seem to be giving `modifies_variable()` a lot more knowledge than it actually has...\n- @JoePineda: The question is whether `f` modifies the variable — not whether it could modify the variable. This answer is correct.\n- what if modifies_variable works for all functions other than itself.\n- @eddardstark: Wouldn't change the answer. It's called on `f`, here, after all. And no, you can't easily fix that by outlawing indirect calls as well.\n- @KonstantinWeitz exactly. This problem is at least as hard as the halting problem since the compiler must be able to decide whether or not a program halts before a variable is modified to see if the variable is actually modified.\n- Firstly this isn't quite the problem, should be modifies_variable(f, variable). Secondly let's assume that modifies_variable() is entirely plausible when looking at a function from the outside. Even in that (impossible) case this would cause the compiler to infinite loop. I think this obfuscates rather than highlights the problem by introducing recursion which includes the test. A simpler example would simply be a function which determines its execution path through rand() or some other external non-statically-analysable factor, which is what the original book was trying to convey and failing.\n- @ElZorko You first point about `modifies_variable(f, variable)` is true, but I kept the \"API\" as simple as possible for clarity. Your second point I do not agree with. Note that the book said it's **provably** impossible. For me it's clear that the book tries to convey the mathematical aspect of it and the relation to the halting problem/similar problems. And while using a non-statically-analysable factor - such as a geiger counter hooked up to the PC - would be a simpler and more easily understandable aspect of this problem it does not reach into the fundamental impossibility of this problem.\n- In terms of the mathematical aspect, it's theoretically possible to \"solve\" the halting problem given an (entirely deterministic) Turing machine and a deterministic program with access to a limited (as in finite) amount of memory. Arguments against this come down to time and scale rather than mathematical proof (ironically). The most accessible (and conveniently, central) elements of this problem for programmers remain the aspects relating to non-deterministic programs.\n- @nightcracker, great reduction of the \"modify variable\" to the halting problem. Very concrete approach!\n- @NeilG to answer the question \"does f modify the variable\" we need to know what \"modifies_variable\" itself does. The compiler knows not its code, or if it's deterministic, and can't execute it at compile time to know what it'd yield. Imagine \"modifies_variable\" was really a function that queries the user to answer \"y/n\" and returns a 1 or 0 based on that. Function f would modify the variable, or not, based on an external, code-opaque, non-deterministic function. So any self-respecting compiler should fall to the safe side and assume it does, occasionally, modify it.\n- @Izkata no, rather the opposite: I'm making the compiler really ignorant on what modifies_variable is about on purpose, to highlight my point: deciding whether a variable may modified or not really depends on the function (and the functions it may call) being deterministic or not. If all possible execution paths are deterministic then for a constant input compiler could execute at compile time and *know* for sure if variable will be changed or not. When you put a non-det. function call in the middle (or your input depends on an external source) all you know is you know nothing.\n- @JoePineda: nothing prevents me from copy/pasting the code of `modifies_variable` from the compiler source, totally nullifying your argument. (assuming open-source, but the point should be clear)\n- @nightcracker once you do that, then we do have the haltungproblem in disguise and the original answer does apply, but not before :)\n- How come G&#246;del's Incompleteness Theorem hasn't even been mentioned yet? :(\n- This happens because `a` and `b` are stack variables, and `b[1]` just happens to be the same memory location as `a`.\n- -1. Undefined Behavior removes all restrictions on the compiler's behavior.\n- Unsure about the down vote. This is just an example that goes to the OP's original question about why can't a compiler figure out if something is truly `const` if everything is labelled `const`. It is because undefined behavior is a part of C/C++. I was trying to find a different way to answer his question rather than mention the halting problem or external human input.\n- I'm not sure I your reasoning, about why our program terminates iff it changes the value of `y`. Looks to me like `foo()` returns quickly, and then `main()` exits. (Also, you're calling `foo()` without an argument... that's part of my confusion.)\n- @LarsH: Iff the modified program terminates, the last function it called was f. If y was modified, f was called (the other statements can't change y, since it was only introduced by the modification). Hence, if y was modified, the program terminates.\n- This is the best answer imho, it's important to make that distinction.\n- @Kip \"trivially impossible to decide\" probably means \"impossible to decide, and the proof is trivial\".\n- If I recall it correctly, that's the whole point of functional programming, right? By using only purely deterministic, no side-effects functions, compilers are free to do aggressive optimizations, pre-execution, post-execution, memoization and even execution at compile time. The point that I think a lot of the answerers are ignoring (or confused about) is that it *is* indeed possible *for a well-behaved subset of all programs*. And no, this subset isn't trivial or uninteresting, actually it's very useful. But it's indeed impossible for the absolute general case.\n- Overloading is a compile-time concept. You probably meant \"overridden method\".\n- @FredOverflow: yes, I mean overriden. Overloading is indeed a compile time concept. Thanks for spotting it (of course if the implementation comes from another compilation unit, the compiler can still have troubles analysing it, but that was not what I meant). I will fix the answer.\n- what you say is true, but even for very simple programs for wich everything is known at compile time you won't be able to proove anything, not even that the program will stop. This is the halting problem. For instance you could write a program based on Hailstone Sequences en.wikipedia.org/wiki/Collatz_conjecture and make it return true if it converge to one. Compilers won't be able to do it (as it would overflow in many cases) and even mathematicians don't know if it's true or not.\n- If you mean \"there are *some* very simple looking programs for which you cannot prove anything\" I entirely agree. But Turing's classic Halting Problem proof relies essentially on a program itself being able to tell whether it halts in order to set up a contradiction. As this is mathematics not implementation. There are certainly programs it is entirely possible to statically determine at compile time whether a particular variable will be modified, and whether the program will halt. It may not be mathematically provable, but it's practically achievable in certain cases.","metadata":{"transformedAt":"2026-08-18T18:32:17.743Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":22,"totalLines":304,"estimatedTokens":6368}}23{"id":"stack-39235612","source":"stackoverflow","questionId":39235612,"title":"How is TypeScript 100.0% written in TypeScript?","tags":["javascript","typescript","compiler-construction"],"text":"Title: How is TypeScript 100.0% written in TypeScript?\nTags: javascript, typescript, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn TypeScript repository on GitHub and according to GitHub, the repository just included 100.0% TypeScript *(.ts files)* \n\nhttps://i.sstatic.net/Is7XE.png\n\nHow does it work? and how TypeScript can compile itself to JavaScript just by itself?\n\n========================================\n\nTop Answer:\nI just want to add something that I think is interesting. \n\nIn git you can have a file called .gitattributes. Github has a project called linguist which can make use of that file for their language details section in every repository. In the typescript repo there is a .gitattributes file which has the following content:\n\n```\n*.js linguist-language=TypeScript\n* -text\n```\n\nYou can fork the typescript repo, remove that file, commit to github and wait for some time while their repository analysis job completes and the language graph will change.https://i.sstatic.net/nqtzo.jpg\n\n========================================\n\nCode:\n```text\nn-1\n```\n\n```text\nlib\n```\n\n```text\n*.js linguist-language=TypeScript\n* -text\n```\n\n========================================\n\nComments:\n- Well, after the first version, you have a compiler and can use that to compile the next compiler, and so on, no?\n- This is how a programmer asks: \"What came first, the chicken or the egg?\"\n- @ssube You said *_after the first version_*. So what language was used to write 1st version? I mean the very first compilation.\n- Same way Java is primarily written in Java. At it's very core is a basic set of native libs that tie into the VM and the Java programming language builds onto there basic capabilities to create more complex objects. This way you only have to modify these basic low-level libraries to create ports for different systems rather than porting the entire language over.\n- But, according to the article, it still needs some of the native languages (here JavaScript) to compile some part of the superset of JavaScript (typeScript). And in Github there are some js files too...\n- @Kermani but since Typescript is a superset of JavaScript, you can write a valid JS program in TS, compile it, then use it to compile a TS program using more language features, and so on. Technically speaking, it could be TS all the way down. Most other transpilers aren't strict supersets (think of C and C++), so they can't take advantage of that quite so much.\n- My understanding is that: TS is superset of JS ==> JS is TS ==> Any JS code is also TS code ==> TS compiler is 100% written in TS.\n- Last-known-good, - fine! But how did they implement the initial version? Which language/compiler did they use? C?\n- @JerryGreen JavaScript I'm pretty sure\n- That's fantastic answer, I guess many people who might come to this question, had no idea about this file. Maybe you could also the link to the Typescript forked repository without that specific file\n- I forked the repo just to test what will happen when I delete that file. After I saw the result I've deleted it, since I am not going to contribute anything to it.","metadata":{"transformedAt":"2026-08-18T18:32:17.743Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":55,"estimatedTokens":781}}24{"id":"stack-14954721","source":"stackoverflow","questionId":14954721,"title":"What is the difference between a token and a lexeme?","tags":["compilation","compiler-construction","token"],"text":"Title: What is the difference between a token and a lexeme?\nTags: compilation, compiler-construction, token\nSource: Stack Overflow\n\nQuestion:\nIn Compiler Construction by Aho Ullman and Sethi, it is given that the input string of characters of the source program are divided into sequence of characters that have a logical meaning, and are known as tokens and lexemes are sequences that make up the token so what is the basic difference?\n\n========================================\n\nTop Answer:\nWhen a source program is fed into the lexical analyzer, it begins by breaking up the characters into sequences of lexemes. The lexemes are then used in the construction of tokens, in which the lexemes are mapped into tokens. A variable called **myVar** would be mapped into a token stating , where \"num\" should point to the variable's location in the symbol table.\n\nShortly put:\n\n- Lexemes are the words derived from the character input stream.\n\n- Tokens are lexemes mapped into a token-name and an attribute-value.\n\nAn example includes:\n\nx = a + b * 2\n\nWhich yields the lexemes: {x, =, a, +, b, *, 2}\n\nWith corresponding tokens: {, , , , , , }\n\n========================================\n\nCode:\n```text\n[Token]       [Informal Description]                  [Sample Lexemes]\nif            characters i, f                         if\nelse          characters e, l, s, e                   else\ncomparison    < or > or <= or >= or == or !=          <=, !=\nid            letter followed by letters and digits   pi, score, D2\nnumber        any numeric constant                    3.14159, 0, 6.02e23\nliteral       anything but \", surrounded by \"'s       \"core dumped\"\n```\n\n```text\nif\n```\n\n```text\nelse\n```\n\n```text\ncomparison\n```\n\n```text\nid\n```\n\n```text\nnumber\n```\n\n```text\nliteral\n```\n\n```text\n<\n```\n\n```text\n>\n```\n\n```text\nelse\n```\n\n```text\n3.14\n```\n\n```text\nx=b\n```\n\n```text\nx\n```\n\n```text\n=\n```\n\n```text\nb\n```\n\n```text\n<id, 0>\n```\n\n```text\n<=>\n```\n\n```text\n<id, 1>\n```\n\n```text\ncout << 3+2+3;\n```\n\n```text\n;\n```\n\n```text\n,\n```\n\n```text\n:\n```\n\n```text\nLexeme        Token category\n------------------------------\nsum      |    Identifier\n =       |    Assignment operator\n 3       |    Integer literal\n +       |    Addition operator\n 2       |    Integer literal\n ;       |    End of statement\n```\n\n```text\nfoo\n```\n\n```text\nbar\n```\n\n```text\nID\n```\n\n```text\naddition ::= variable '+' variable\n```\n\n```text\nvery_looooong_variable_name+n\n```\n\n```text\n+\n```\n\n========================================\n\nComments:\n- > Compiler Construction by Aho Ullman and Sethi - Is this the same book as the dragon book? I'm having trouble locating a book by this name?\n- The \"Dragon Book\" is *Compilers: Principles, Techniques, and Tools* by Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman.\n- It was probably a sloppy reference to the \"Dragon Book\" (e.g., the auhors' names were not capitalised in the first revision). It also left out crucial punctuation.\n- In colloquial compiler usage, people tend to use the two terms interchangeably. The precise distinction is nice, if and when you need it.\n- While not a purely computer science definition, here is one from natural language processing that is of relevance from Introduction to lexical semantics `an individual entry in the lexicon`\n- Absolutely clear explanation. This is how the things should be explained in the heaven.\n- great explanation. I have one more doubt, I also read about parsing stage, parser asks for tokens from lexical analyzer, as parser can not validate tokens. can you please explain by taking simple input at parser stage and when does parser asks for tokens from lexer.\n- @PrasannaSasne `can you please explain by taking simple input at parser stage and when does parser asks for tokens from lexer.` SO is not a discussion site. That is a new question and needs to be asked as a new question.\n- @GuyCoder, I have posted my question where I want some information about symbol table. Can you please look into this once. `https:&#47;&#47;stackoverflow.com&#47;questions&#47;63820620&#47;what-values-are&zwnj;&#8203;-stored-into-symbol-&zwnj;&#8203;table-in-compiler-co&zwnj;&#8203;nstruction`. i will also post above question in different thread. Thank you\n- Is it supposed to be ? because 2 is a not an identifier\n- but where does it says that x is an identifier? does that mean a symbol table is a 3 column table having 'name'=x , 'type' ='identifier(id)', pointer ='0' as a particular entry?then it must have some other entry like 'name'=while, 'type' ='keyword', pointer ='21' ??\n- An answer should be more specific. An example could be useful.\n- \"the scanner detects whitespaces. It ignores them and does not form any token for a whitespace at all.\" — this is a great example of tokens not always representing input 1:1 but being what is convenient to feed into the parser.\n- This explanation is so straightforward! Thanks for the sharing","metadata":{"transformedAt":"2026-08-18T18:32:17.743Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":29,"totalLines":175,"estimatedTokens":1226}}25{"id":"stack-13537","source":"stackoverflow","questionId":13537,"title":"Bootstrapping still requires outside support","tags":["compiler-construction","language-agnostic","bootstrapping"],"text":"Title: Bootstrapping still requires outside support\nTags: compiler-construction, language-agnostic, bootstrapping\nSource: Stack Overflow\n\nQuestion:\nI've heard of the idea of bootstrapping a language, that is, writing a compiler/interpreter for the language in itself. I was wondering how this could be accomplished and looked around a bit, and saw someone say that it could only be done by either\n\n- writing an initial compiler in a different language.\n\n- hand-coding an initial compiler in Assembly, which seems like a special case of the first\n\nTo me, neither of these seem to actually be *bootstrapping* a language in the sense that they both require outside support. Is there a way to actually write a compiler in its own language?\n\n========================================\n\nTop Answer:\nThe explanation you've read is correct. There's a discussion of this in *Compilers: Principles, Techniques, and Tools* (the Dragon Book):\n\n- Write a compiler C1 for language X in language Y\n\n- Use the compiler C1 to write compiler C2 for language X in language X\n\n- Now C2 is a fully self hosting environment.\n\n========================================\n\nCode:\n```text\n*/*.{ml,mli}\n```\n\n```text\nboot/ocamlc\n```\n\n```text\n*.ml[i]\n```\n\n```text\nboot/ocamlc\n```\n\n```text\nwget\n```\n\n```text\n*.melt\n```\n\n```text\nmelt/generated/*.cc\n```\n\n```text\n[A-Z]*.c\n```\n\n```text\ndx.h\n```\n\n```text\n_[0-9]*\n```\n\n========================================\n\nComments:\n- I'm not very experienced with such things, but I would assume that the *initial* compiler would have to be written in another language. I'm fairly certain that \"bootstrapping\", in reference to compilers, simply refers to writing *a* compiler for a language in the language it's meant to compile, not writing *the first* compiler for the language in the language it's meant to compile.\n- Thanks for the info, everyone. When explained with the idea of initially writing a limited compiler, then building up on top of that, then the idea of bootstrapping makes more sense. I'm taking a Compilers class this semester, a decision largely influenced by Steve Yegge's post on how important a class in Compilers is, and I just bought a copy of the Dragon book from the Amazon link that got so downmodded on SO earlier.\n- See also similar question: Implementing a compiler in itself\n- The first Lisp compiler, at least, was bootstrapped using an existing Lisp *interpreter*. So not another language semantically, but another language implementation.\n- This is off-topic.. Interesting, but confusing, and not an answer to the question.\n- Whatever happened to parts 2 and 3? ... How did I not notice that @Wing posted the same thing 3 years before me? I'm a dunce. At least I linked the paper (with help).","metadata":{"transformedAt":"2026-08-18T18:32:17.743Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":76,"estimatedTokens":682}}26{"id":"stack-6216449","source":"stackoverflow","questionId":6216449,"title":"Where can I learn the basics of writing a lexer?","tags":["language-agnostic","lexer","compiler-construction"],"text":"Title: Where can I learn the basics of writing a lexer?\nTags: language-agnostic, lexer, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI want to learn how to write a lexer. My university course had an assignment where we had to write a parser (and a lexer to go along with it) but this was given to us with no instruction or feedback (beyond the mark) so I didn't really learn much from it.\n\nAfter searching for this topic, I can only find fairly advanced write ups which focus on areas which I feel are a few steps ahead of where I am at. I want a discussion on the basics of writing a lexer for a very simple language which I can use as a basis for investigating tokenising more complex languages.\n\nAt this stage I'm not really interested in best practices or optimisation techniques but instead prefer a focus on the essentials. What are some good resources to get me started?\n\n========================================\n\nTop Answer:\nThe Dragon Book is probably the definitive guide on the subject, although it can be a bit overwhelming. Language Implementation Patterns and Programming Language Pragmatics are great resources as well.\n\n========================================\n\nComments:\n- The Kaleidoscope tutorial was the part that really answered this question for me.\n- For more info on writing an LL(1) parser by hand see this answer.\n- +1 on the Dragon book. Learned A LOT from it when in university. Yeah, there's a lot there, but if you're really interested in compiler design and implementation, it's a great resource.","metadata":{"transformedAt":"2026-08-18T18:32:17.743Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":22,"estimatedTokens":385}}27{"id":"stack-2906064","source":"stackoverflow","questionId":2906064,"title":"Why is writing a compiler in a functional language easier?","tags":["compiler-construction","haskell","ocaml","functional-programming"],"text":"Title: Why is writing a compiler in a functional language easier?\nTags: compiler-construction, haskell, ocaml, functional-programming\nSource: Stack Overflow\n\nQuestion:\nI've been thinking of this question very long, but really couldn't find the answer on Google as well a similar question on Stackoverflow. If there is a duplicate, I'm sorry for that.\n\nA lot of people seem to say that writing compilers and other language tools in functional languages such as OCaml and Haskell is much more efficient and easier then writing them in imperative languages.\n\nIs this true? And if so -- why is it so efficient and easy to write them in functional languages instead of in an imperative language, like C? Also -- isn't a language tool in a functional language slower then in some low-level language like C?\n\n========================================\n\nTop Answer:\nA lot of compiler tasks are pattern matching on tree structures. \n\nBoth OCaml and Haskell have powerful and concise pattern matching capabilities.\n\nIt's harder to add pattern matching to imperative languages as whatever value is being evaluated or extracted to match the pattern against must be side-effect free.\n\n========================================\n\nComments:\n- I wouldn't say it's easier. But the functional nature of compiling tasks such as parsing probably lend themselves quite naturally to functional programming. Functional languages like OCaml can be extremely fast, rivaling the speed of C.\n- Folks, is this really argumentative? Surely someone has some insight. I'd like to know myself.\n- I think there should atleast be some good reasons why to use a functional language over a imperative one. I've found some article which basically came down on that functional languages have no side-effects and such. But it wasn't really clear at all. However, if this is argumentative, then it might be better to close it or reformulate the question.\n- Can we change it to “…efficient and easy”? That makes it less argumentative.\n- Is it really argumentative to acknowledge that some niches are better suited to a particular style of language? \"Why is C better than Javascript for writing device drivers\" wouldn't be controversial, I'd think...\n- I thought it would be the opposite. I am reading \"super tiny compiler\" and it uses variable mutation all over the place.\n- Sounds like a reasonable answer, but is this the only thing? e.g. would things such as tail recursion also play a role?\n- That would seem to indicate that it is more of an issue of the type system than of the actual execution model. Something based on imperative programming with immutable values over structural types might be fine.\n- @wvd: Tail recursion optimization is an implementation detail, not a language feature as such, that makes linear recursive functions equivalent to an iterative loop. A recursive function to walk a linked list in C would benefit from it just as much as recursing on a list in Scheme does.\n- @wvd gcc C has tail call elimination, as do other mutable state languages\n- @wvd: I would say no on the tail recursion, in the end it is only an optimization and lots of lisps are great for writing compilers in, and only a subset of those promise TCO.\n- As the JMatch project (cs.cornell.edu/Projects/jmatch) shows, it's not impossible to add powerful pattern matching to a completely non-functional language like java. However it is a fact that currently the only common languages that have pattern matching are (at least partly) functional in nature.\n- @camccann: If the language standard guarantees tco (or at least guarantees that recursive functions of a certain form will never cause a stack overflow or a linear growth of memory consumption), I'd consider that a language feature. If the standard doesn't guarantee it, but the compiler does it anyway, it's a compiler feature.\n- @sepp2k: It's an odd case because it's an \"optimization\" that's all but required for a functional language compiler to produce useful output. Language specifications do sometimes dictate implementation details or performance bounds for various reasons, but I have a hard time calling such things features of a language itself. Naught but quibbling over terminology, though, so disregard me at will!\n- Most complete answer so far, I'll mark this as the accepted answer, however I think Pete Kirkham's answer is also good.\n- What about \"prooving correctness\", since correctness of a compiler is an important attribute, I have often heard that fans of functional languages incorporate a \"proof\" of correctness into their workflow somehow. I have no idea what that really means in practical terms, but as compiler reliability is important, this seems worthwhile.\n- @WarrenP: The \"proof-carrying code\" concept comes from statically-typed functional languages. The idea is that you use the type-system in such a way so that a function can only typecheck if it's correct, so the fact the code compiles is the proof of correctness. Of course this isn't fully possible while keeping the language turing-complete and typechecking decidable. But the stronger the type-system, the closer you can get to that goal.\n- The reason that this concept is mainly popular in the functional community is that in languages with mutable state, you'd also have to encode information about when and where state change occurs in the types. In languages where you know that the result of a function only depends on its arguments, it's much easier to encode a proof in the types (it's also much easier to manually proof the code's correctness because you don't have to consider which global state is possible and how it will affect the behavior of the function). However none of this is specifically related to compilers.\n- @Warren P: Typically that means a mathematical specification of what the program should do, and a proof that the program in fact does that. In compiler terms, this would mean something like \"given this definition of the source language, the output code will perform the same computations, and any optimizations will alter only the time/space usage of the program, not its behavior\".\n- The single most important feature is pattern matching in my opinion. Optimizing an abstract syntax tree with pattern matching is stupidly easy. Doing it without pattern matching is often frustratingly hard.\n- So you could generalise the answer to: a compiler is a function from a sentence in one language into a sentence in another language (preserving some kind of \"semantic mapping\" across meanings of both languages). If you view your compiler as a function it's reasonable to look to functional programming for support implementing it...\n- This relates to what is called, in some Programming Language Theory circles, the \"expression problem\". For example, see this question, wherein I demonstrate some truly horrible Haskell code that does things the \"extensible types\" way. Contrariwise, forcing an OOP language into the \"extensible operations\" style tends to motivate the Visitor Pattern.","metadata":{"transformedAt":"2026-08-18T18:32:17.743Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":45,"estimatedTokens":1754}}28{"id":"stack-3380498","source":"stackoverflow","questionId":3380498,"title":"How to approach creating a JVM programming language?","tags":["compiler-construction","programming-languages","jvm","jvm-languages"],"text":"Title: How to approach creating a JVM programming language?\nTags: compiler-construction, programming-languages, jvm, jvm-languages\nSource: Stack Overflow\n\nQuestion:\nI have created a compiler in C (using Lex & Bison) for a dynamic typed programming language that supports loops, functions declarations inside functions, recursive calls, etc. I also created a virtual machine that runs the intermediate code created by the compiler.\n\nI was thinking about compiling it to Java bytecode instead of my own intermediate code.\n\nI saw that the question about creating a JVM language has already been asked, but I don’t find the answer very informative.\n\nSo here are my questions:\n\n- I guess to create a language for JVM a must is to read the JVM specification book, what other books can you suggest (except Dragon Book of course)? I’m mostly concerned about books or tutorials on how to create a JVM language, not a compiler in general.\n\n- There are many Java libraries to read, write and change `.class` files like jclasslib, bcel, gnu bytecode, etc. Which one would you suggest? Also, are you aware of C libraries that do the same job?\n\n- I was thinking about having a look at maybe another language that targets the JVM like Clojure, Jython or JRuby. But all these languages are very high level and complicated (to create a compiler for them). I was looking for a simpler (I don't mind if it's unknown or unused) programming language that targets the JVM and with an open-source compiler. Any ideas?\n\n========================================\n\nTop Answer:\nLast semester I have attended a \"Compiler Construction\" course. Our project was exactly what you want to do.\n\nThe language I've used to write my language was Scala. It runs on a JVM but supports a lot of advanced features that Java doesn't (still fully compatible with a pure java JVM).\n\nTo output java bytecode I've used the Scala CAFEBABE library. Well documented and you don't have to go deep inside java classes to understand what to do.\n\nBeside the book, I think you can find a lot of infos by going trough the labs we've done during the course.\n\n========================================\n\nCode:\n```text\n.class\n```\n\n```text\nbytecode/InsanelyFastByteCodeCreator.java\n```\n\n```text\nInsanlyFastByteCodeCreator\n```\n\n```text\n?name\n```\n\n```text\n?\n```\n\n```text\ni\n```\n\n```text\na\n```\n\n```text\n?push/?pop\n```\n\n```text\n?store/?load\n```\n\n```text\ninvoke???\n```\n\n```text\nif_???\n```\n\n========================================\n\nComments:\n- Thanks for your answer, I will take a look at Jasmin. And also I would be glad if you could upload the source so I can take a look. About the book you suggested, it seems interesting but it's out of print and quite old :(.\n- The book is very cheap second-hand though. I found a copy for a few dollars.\n- Have a look at my edit above, if you have any questions, I'll be glad to help.\n- The link to the \"source-code itself\" is broken. Although I guess that is to be expected after 8 years.\n- @LlewVallis, if I interpret all information right, the code seems to be here: github.com/replimoc/compiler.\n- This sound like a great course. Would you mind to your notes or code?\n- No problem, I will check where my backups are and post a link here so you can download it asap.\n- Neat, I've been looking for an hands-on compiler course that targets the JVM with all the material online for self-study.\n- `Are you planning to upload the code to anyplace?` I'm not proud of that code :( ... I maybe would rewrite the whole thing. Anyway If I do I will let you know. Thank you very much for your suggestions.\n- Compiling for the JVM will allow one's code to be run more widely than if one compiles to native code. Further, compiling to bytecode will make it possible for code to do some things which are not possible within the Java language itself.","metadata":{"transformedAt":"2026-08-18T18:32:17.744Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":90,"estimatedTokens":953}}29{"id":"stack-7378337","source":"stackoverflow","questionId":7378337,"title":"What is the difference between LR(0) and SLR parsing?","tags":["algorithm","parsing","compiler-construction","lr-grammar"],"text":"Title: What is the difference between LR(0) and SLR parsing?\nTags: algorithm, parsing, compiler-construction, lr-grammar\nSource: Stack Overflow\n\nQuestion:\nI am working on my compilers concepts however I am a little confused...\nGoogling got me nowhere to a definite answer.\n\nIs SLR and LR(0) parsers one and same? If not, whats the difference?\n\n========================================\n\nTop Answer:\nAdding on top of the above answers, the difference in between the individual parsers in the class of bottom-up parsers is whether they result in shift/reduce or reduce/reduce conflicts when generating the parsing tables. The less it will have the conflicts, the more powerful will be the grammar (LR(0) For example, consider the following expression grammar:\n\nE → E + T\n\nE → T\n\nT → F\n\nT → T * F\n\nF → ( E )\n\nF → id\n\nIt's not LR(0) but SLR(1). Using the following code, we can construct the LR0 automaton and build the parsing table (we need to augment the grammar, compute the DFA with closure, compute the action and goto sets):\n\n```\nfrom copy import deepcopy\nimport pandas as pd\n\ndef update_items(I, C):\n if len(I) == 0:\n return C\n for nt in C:\n Int = I.get(nt, [])\n for r in C.get(nt, []):\n if not r in Int:\n Int.append(r)\n I[nt] = Int\n return I\n\ndef compute_action_goto(I, I0, sym, NTs): \n #I0 = deepcopy(I0)\n I1 = {}\n for NT in I:\n C = {}\n for r in I[NT]:\n r = r.copy()\n ix = r.index('.')\n #if ix == len(r)-1: # reduce step\n if ix >= len(r)-1 or r[ix+1] != sym:\n continue\n r[ix:ix+2] = r[ix:ix+2][::-1] # read the next symbol sym\n C = compute_closure(r, I0, NTs)\n cnt = C.get(NT, [])\n if not r in cnt:\n cnt.append(r)\n C[NT] = cnt\n I1 = update_items(I1, C)\n return I1\n\ndef construct_LR0_automaton(G, NTs, Ts):\n I0 = get_start_state(G, NTs, Ts)\n I = deepcopy(I0)\n queue = [0]\n states2items = {0: I}\n items2states = {str(to_str(I)):0}\n parse_table = {}\n cur = 0\n while len(queue) > 0:\n id = queue.pop(0)\n I = states[id]\n # compute goto set for non-terminals\n for NT in NTs:\n I1 = compute_action_goto(I, I0, NT, NTs) \n if len(I1) > 0:\n state = str(to_str(I1))\n if not state in statess:\n cur += 1\n queue.append(cur)\n states2items[cur] = I1\n items2states[state] = cur\n parse_table[id, NT] = cur\n else:\n parse_table[id, NT] = items2states[state]\n # compute actions for terminals similarly\n # ... ... ...\n \n return states2items, items2states, parse_table\n \nstates, statess, parse_table = construct_LR0_automaton(G, NTs, Ts)\n```\n\nwhere the grammar G, non-terminal and terminal symbols are defined as below\n\n```\nG = {}\nNTs = ['E', 'T', 'F']\nTs = {'+', '*', '(', ')', 'id'}\nG['E'] = [['E', '+', 'T'], ['T']]\nG['T'] = [['T', '*', 'F'], ['F']]\nG['F'] = [['(', 'E', ')'], ['id']]\n```\n\nHere are few more useful function I implemented along with the above ones for LR(0) parsing table generation:\n\n```\ndef augment(G, S): # start symbol S\n G[S + '1'] = [[S, '$']]\n NTs.append(S + '1')\n return G, NTs\n\ndef compute_closure(r, G, NTs):\n S = {}\n queue = [r]\n seen = []\n while len(queue) > 0:\n r = queue.pop(0)\n seen.append(r)\n ix = r.index('.') + 1\n if ix The following figure (expand it to view) shows the LR0 DFA constructed for the grammar using the above code:\n\nhttps://i.sstatic.net/XdqIL.png\n\nThe following table shows the LR(0) parsing table generated as a pandas dataframe, notice that there are couple of shift/reduce conflicts, indicating that the grammar is not LR(0).\n\nhttps://i.sstatic.net/K2F0p.png\n\nSLR(1) parser avoids the above shift / reduce conflicts by reducing only if the next input token is a member of the Set of the nonterminal being reduced. So the above grammar is not LR(0), but it's SLR(1). The following parse table is generated by SLR:\n\nhttps://i.sstatic.net/DJYu7.png\n\nThe following animation shows how an input expression is parsed by the above SLR(1) grammar:\n\nhttps://i.sstatic.net/9zWoz.gif\n\nBut, the following grammar which accepts the strings of the form `a^ncb^n, n >= 1` is LR(0):\n\nA → a A b\n\nA → c\n\nS → A\n\nLet's define the grammar as follows:\n\n```\n# S --> A \n# A --> a A b | c\nG = {}\nNTs = ['S', 'A']\nTs = {'a', 'b', 'c'}\nG['S'] = [['A']]\nG['A'] = [['a', 'A', 'b'], ['c']]\n```\n\nhttps://i.sstatic.net/P3oSt.png\n\nAs can be seen from the following figure, there is no conflict in the parsing table generated.\n\nhttps://i.sstatic.net/T8xm7.png\n\n========================================\n\nCode:\n```text\nyacc\n```\n\n```text\nbison\n```\n\n```text\nfrom copy import deepcopy\nimport pandas as pd\n\ndef update_items(I, C):\n    if len(I) == 0:\n         return C\n    for nt in C:\n         Int = I.get(nt, [])\n         for r in C.get(nt, []):\n              if not r in Int:\n                  Int.append(r)\n          I[nt] = Int\n     return I\n\ndef compute_action_goto(I, I0, sym, NTs): \n    #I0 = deepcopy(I0)\n    I1 = {}\n    for NT in I:\n        C = {}\n        for r in I[NT]:\n            r = r.copy()\n            ix = r.index('.')\n            #if ix == len(r)-1: # reduce step\n            if ix >= len(r)-1 or r[ix+1] != sym:\n                continue\n            r[ix:ix+2] = r[ix:ix+2][::-1]    # read the next symbol sym\n            C = compute_closure(r, I0, NTs)\n            cnt = C.get(NT, [])\n            if not r in cnt:\n                cnt.append(r)\n            C[NT] = cnt\n        I1 = update_items(I1, C)\n    return I1\n\ndef construct_LR0_automaton(G, NTs, Ts):\n    I0 = get_start_state(G, NTs, Ts)\n    I = deepcopy(I0)\n    queue = [0]\n    states2items = {0: I}\n    items2states = {str(to_str(I)):0}\n    parse_table = {}\n    cur = 0\n    while len(queue) > 0:\n        id = queue.pop(0)\n        I = states[id]\n        # compute goto set for non-terminals\n        for NT in NTs:\n            I1 = compute_action_goto(I, I0, NT, NTs) \n            if len(I1) > 0:\n                state = str(to_str(I1))\n                if not state in statess:\n                    cur += 1\n                    queue.append(cur)\n                    states2items[cur] = I1\n                    items2states[state] = cur\n                    parse_table[id, NT] = cur\n                else:\n                    parse_table[id, NT] = items2states[state]\n        # compute actions for terminals similarly\n        # ... ... ...\n                    \n    return states2items, items2states, parse_table\n        \nstates, statess, parse_table = construct_LR0_automaton(G, NTs, Ts)\n```\n\n```text\nG = {}\nNTs = ['E', 'T', 'F']\nTs = {'+', '*', '(', ')', 'id'}\nG['E'] = [['E', '+', 'T'], ['T']]\nG['T'] = [['T', '*', 'F'], ['F']]\nG['F'] = [['(', 'E', ')'], ['id']]\n```\n\n```text\ndef augment(G, S): # start symbol S\n    G[S + '1'] = [[S, '$']]\n    NTs.append(S + '1')\n    return G, NTs\n\ndef compute_closure(r, G, NTs):\n    S = {}\n    queue = [r]\n    seen = []\n    while len(queue) > 0:\n        r = queue.pop(0)\n        seen.append(r)\n        ix = r.index('.') + 1\n        if ix < len(r) and r[ix] in NTs:\n            S[r[ix]] = G[r[ix]]\n            for rr in G[r[ix]]:\n                if not rr in seen:\n                    queue.append(rr)\n    return S\n```\n\n```text\n# S --> A \n# A --> a A b | c\nG = {}\nNTs = ['S', 'A']\nTs = {'a', 'b', 'c'}\nG['S'] = [['A']]\nG['A'] = [['a', 'A', 'b'], ['c']]\n```\n\n```text\na^ncb^n, n >= 1\n```\n\n========================================\n\nComments:\n- The parsing algorithm is the same. The tables are different.\n- The precise answer is: *the reductions in the action table*. For an item `A → α•`, an SLR(1) adds a *reduce* action for each terminal `a` that belongs to `(A)`. An LR(0) adds a *reduce* action for all the terminals—in other words, regardless of the next terminal, since it has no lookahead. That difference makes LR(0) more prone to conflicts than SLR(1), so the languages an LR(0) can parse is a subset of the languages an SLR(1) can parse.\n- This is an excellent answer. Exactly answers the question in a very clear and educational manner. One of the best answers I have come across on SO.\n- @templatetypedef: I think you should expound a little on the difference between L(AL)R(1) and SLR(1), which is why SLR(1) exists as as an interesting choice. But +1.\n- @Ira Baxter- I just updated the discussion to talk a bit more about LALR(1) and LR(1). Can you look over this and let me know if there's anything you think I should add?\n- @templatetypedef: GLR can handle any *context-free* grammar. Other than that, this is good enough.\n- A really good answer! I want to add that a CYK parser works slightly differently and though I can't judge your sentence \"GLR tends to be faster in practice\", there is a guaranteed upper limit of `O(n&#179;)` time for the CYK, and no such upper limit for the GLR. The CYK achieves guaranteed performance by needing a pre-processed grammar, i.e. any context-free grammar can be rewritten to Chomsky normal form, which serves to guarantee that any reduction by a grammar rule will always result in a shorter sentential form.\n- @templatetypedef: Hi! I asked a question here stackoverflow.com/questions/13143186/&hellip; . I request to you please visit.\n- @templatetypedef Awesome explanations, and your lectures are - to the best of my knowledge - the only ones making sense on this subject on the whole internet :) This inspired me to transform my online toy LR parser into a toy LR(1) parser (well, I think it is). If anyone is interested: parse.fmdj.fr\n- Excellent answer. Very clear explanation of the topic. Will definitely go through your lectures.\n- @templatetypedef Is it correct that LR(0) parsers use only LR(0) automaton, whereas SLR parsers use also the `ACTION` subroutine?\n- @newbie LR(0) parsers do have an ACTION table, but the action depends purely on the state, not the state plus the next token.\n- @templatetypedef Thank you, this make sense. Do LR(0) parsers build ACTION table calculating viable prefixes and looking for a handle or something else? I.e. if we have `A → β&#183;` item in configuration set, should we say it's \"reducing\" state?\n- @newbie The item you gave above is a reduce item. The ACTION entry for this state would be to reduce.","metadata":{"transformedAt":"2026-08-18T18:32:17.744Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":305,"estimatedTokens":2477}}30{"id":"stack-2377273","source":"stackoverflow","questionId":2377273,"title":"How does an interpreter/compiler work","tags":["compiler-construction","interpreter"],"text":"Title: How does an interpreter/compiler work\nTags: compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nHow does an interpreter/compiler work? What is the difference between interpreter and compiler.\n\n========================================\n\nTop Answer:\n### **What is a translator?**\n\n An **S -> T translator** accepts code expressed in source language S, and translates it to equivalent code expressed in another (target) language T.\n\n**Examples of translators:**\n\n- Compilers - translates high level code to low level code, e.g. *Java -> JVM*\n\n- Assemblers - translates assembly language code to machine code, e.g. *x86as -> x86*\n\n- High-level translators - translates code from one PL to another, e.g. *Java -> C*\n\n- Decompilers - translates low-level code to high-level code, e.g. *Java JVM bytecode -> Java*\n\n### **What is an interpreter?**\n\n An **S interpreter** accepts code expressed in language S, and immediately executes that code. It works by fetching, analysing, and executing one instruction at a time.\n\nGreat when user is entering instructions interactively (think Python) and would like to get the output before putting in the next instruction. Also useful when the program is to be executed only once or requires to be portable. \n\n- Interpreting a program is much slower than executing native machine code\n\n- Interpreting a high-level language is ~100 times slower\n\n- Interpreting an intermediate-level (such as JVM bytecode) language is ~10 slower\n\n- If an instruction is called repeatedly, it will be analysed repeatedly - time-consuming!\n\n- No need to compile code\n\n### **Differences**\n\n**Behaviour**\n\nA compiler translates source code to machine code, but does not execute the source or object code.\n\nAn interpreter executes source code one instruction at a time, but does not translate the source code.\n\n**Performance**\n\n- A compiler takes quite a long time to translate the source program to native machine code, but subsequent execution is fast\n\n- An interpreter starts executing the source program immediately, but execution is slow\n\n**Interpretive compilers**\n\nAn interpretive compiler is a good compromise between compilers and interpreters. It translates source program into virtual machine code, which is then interpreted.\n\nAn interpretive compiler combines fast translation with moderately fast execution, provided that:\n\n- VM code is lower than the source language, but higher than native machine code\n\n- VM instructions have simple formats (can be quickly analysed by an interpreter)\n\nExample: JDK provides an interpretive compiler for Java.\n\n========================================\n\nComments:\n- so c# uses both compiler and interpreter?\n- That's not correct. Both languages are both compiled and interpreted.\n- @Matt H: Yep, that would be correct if you are counting the runtime as well; because runtime interprets the complied assemblies. From C#'s point of view, it is first compiled and then executed.\n- @developer all languages are compiled and interpreted. Either interpreted(after compilation) by a virtual machine or a real machine. Or interpreted by special programs(called interpreters) that work on a grammatically correct formally defined languages.\n- @Aniket nope, no interpretation takes place on object code. If you compile a program into something the machine understands, then you dont need an interpreter. Moreover, the point of the question is the difference between the object code produced by a compiler and the midcode (eg: bytecode) produced for example by a Java compiler that is then interpreted by a real Interpreter each time its run.\n- It is not generally true that interpreters work line-by-line. The vast majority of interpreters don't. It is also not universally true that interpreters generate low-level code: there are interpreters that walk the AST (no code generation), interpreters that generate bytecode and then interpret that and interpreters that JIT-compile to machine code.\n- You can compile a language into another language, not necessarily machine code\n- Nearly all interpreters execute compiled byte code, and some (simulators) execute compiled machine code. Interpretation is only 'performance costly' if the instructions being interpreted are simple compared to the fetch overhead.\n- What about C# and VB? In which section do they belong in your diagram?\n- \"An interpreter executes source code one instruction at a time,but does not translate the source code\"(taken from diff heading) I think interpreter translates and executes both actions at a time but not just executing as you explained. apart from this explanation was clear and perfect +1.\n- Most compilers do not generate assembly language. Linking does not imply more memory. Source code interpreters exist. C# is interpreted, or indeed Java and C# are both compiled and interpreted.. C and C++ exist in interpreted versions, as does Fortran.\n- 'A line at a time' is not correct. Most interpreters precompile the code to a p-code and then execute that one p-code instruction at a time.\n- Interpreters do not 'translate byte code'. They *execute* it. There are Basic implementations that execute byte code, not source code.\n- 90% of this answer is just advertising.","metadata":{"transformedAt":"2026-08-18T18:32:17.744Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":83,"estimatedTokens":1305}}31{"id":"stack-29971097","source":"stackoverflow","questionId":29971097,"title":"How to create AST with ANTLR4?","tags":["java","compiler-construction","antlr","abstract-syntax-tree","antlr4"],"text":"Title: How to create AST with ANTLR4?\nTags: java, compiler-construction, antlr, abstract-syntax-tree, antlr4\nSource: Stack Overflow\n\nQuestion:\nI've been searching A LOT about this and I couldn't find anything useful that REALLY helps me build an AST. I already know that ANTLR4 doesn't build AST like ANTLR3 used to do. Everyone say: \"Hey, use visitors!\", but I couldn't find any example or more detailed explanation on HOW can I do this...\n\nI have a grammar must like C, but with every commands written in Portuguese (portuga programming language). I can easily generate the parse tree using ANTLR4. My question is: What I need to do now to create an AST?\n\nBTW, I'm using Java and IntelliJ...\n\n**EDIT1:** The closest I could get was using the answer of this topic: Is there a simple example of using antlr4 to create an AST from java source code and extract methods, variables and comments?\nBut it only prints the name of the visited methods..\n\nSince the first attempt didn't work for me as I expected, I tried to use this tutorial from ANTLR3, but I couldn't figure out how to use StringTamplate instead of ST...\n\nReading the book The Definitive ANTLR 4 Reference I also couldn't find anything related to ASTs.\n\n**EDIT2:** Now I have one class to create the DOT file, I just need figure out on how to use visitors properly\n\n========================================\n\nTop Answer:\nI have created a small Java project that allows you to test your ANTLR grammar instantly by compiling the lexer and parser generated by ANTLR in-memory. You can just parse a string by passing it to the parser, and it will automatically generate an AST from it which can then be used in your application. \n\nFor the purpose of reducing the size of the AST, you could use a NodeFilter to which you could add the production-rule names of the non-terminals that you would like to be considered when constructing the AST.\n\nThe code and some code examples can be found at\nhttps://github.com/julianthome/inmemantlr\n\nHope the tool is useful ;-)\n\n========================================\n\nCode:\n```none\ngrammar Math;\n\ncompileUnit\n    :   expr EOF\n    ;\n\nexpr\n    :   '(' expr ')'                         # parensExpr\n    |   op=('+'|'-') expr                    # unaryExpr\n    |   left=expr op=('*'|'/') right=expr    # infixExpr\n    |   left=expr op=('+'|'-') right=expr    # infixExpr\n    |   func=ID '(' expr ')'                 # funcExpr\n    |   value=NUM                            # numberExpr\n    ;\n\nOP_ADD: '+';\nOP_SUB: '-';\nOP_MUL: '*';\nOP_DIV: '/';\n\nNUM :   [0-9]+ ('.' [0-9]+)? ([eE] [+-]? [0-9]+)?;\nID  :   [a-zA-Z]+;\nWS  :   [ \\t\\r\\n] -> channel(HIDDEN);\n```\n\n```csharp\ninternal abstract class ExpressionNode\n{\n}\n\ninternal abstract class InfixExpressionNode : ExpressionNode\n{\n    public ExpressionNode Left { get; set; }\n    public ExpressionNode Right { get; set; }\n}\n\ninternal class AdditionNode : InfixExpressionNode\n{\n}\n\ninternal class SubtractionNode : InfixExpressionNode\n{\n}\n\ninternal class MultiplicationNode : InfixExpressionNode\n{\n}\n\ninternal class DivisionNode : InfixExpressionNode\n{\n}\n\ninternal class NegateNode : ExpressionNode\n{\n    public ExpressionNode InnerNode { get; set; }\n}\n\ninternal class FunctionNode : ExpressionNode\n{\n    public Func<double, double> Function { get; set; }\n    public ExpressionNode Argument { get; set; }\n}\n\ninternal class NumberNode : ExpressionNode\n{\n    public double Value { get; set; }\n}\n```\n\n```csharp\ninternal class BuildAstVisitor : MathBaseVisitor<ExpressionNode>\n{\n    public override ExpressionNode VisitCompileUnit(MathParser.CompileUnitContext context)\n    {\n        return Visit(context.expr());\n    }\n\n    public override ExpressionNode VisitNumberExpr(MathParser.NumberExprContext context)\n    {\n        return new NumberNode\n        {\n            Value = double.Parse(context.value.Text, NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent)\n        };\n    }\n\n    public override ExpressionNode VisitParensExpr(MathParser.ParensExprContext context)\n    {\n        return Visit(context.expr());\n    }\n\n    public override ExpressionNode VisitInfixExpr(MathParser.InfixExprContext context)\n    {\n        InfixExpressionNode node;\n\n        switch (context.op.Type)\n        {\n            case MathLexer.OP_ADD:\n                node = new AdditionNode();\n                break;\n\n            case MathLexer.OP_SUB:\n                node = new SubtractionNode();\n                break;\n\n            case MathLexer.OP_MUL:\n                node = new MultiplicationNode();\n                break;\n\n            case MathLexer.OP_DIV:\n                node = new DivisionNode();\n                break;\n\n            default:\n                throw new NotSupportedException();\n        }\n\n        node.Left = Visit(context.left);\n        node.Right = Visit(context.right);\n\n        return node;\n    }\n\n    public override ExpressionNode VisitUnaryExpr(MathParser.UnaryExprContext context)\n    {\n        switch (context.op.Type)\n        {\n            case MathLexer.OP_ADD:\n                return Visit(context.expr());\n\n            case MathLexer.OP_SUB:\n                return new NegateNode\n                {\n                    InnerNode = Visit(context.expr())\n                };\n\n            default:\n                throw new NotSupportedException();\n        }\n    }\n\n    public override ExpressionNode VisitFuncExpr(MathParser.FuncExprContext context)\n    {\n        var functionName = context.func.Text;\n\n        var func = typeof(Math)\n            .GetMethods(BindingFlags.Public | BindingFlags.Static)\n            .Where(m => m.ReturnType == typeof(double))\n            .Where(m => m.GetParameters().Select(p => p.ParameterType).SequenceEqual(new[] { typeof(double) }))\n            .FirstOrDefault(m => m.Name.Equals(functionName, StringComparison.OrdinalIgnoreCase));\n\n        if (func == null)\n            throw new NotSupportedException(string.Format(\"Function {0} is not supported\", functionName));\n\n        return new FunctionNode\n        {\n            Function = (Func<double, double>)func.CreateDelegate(typeof(Func<double, double>)),\n            Argument = Visit(context.expr())\n        };\n    }\n}\n```\n\n```csharp\ninternal abstract class AstVisitor<T>\n{\n    public abstract T Visit(AdditionNode node);\n    public abstract T Visit(SubtractionNode node);\n    public abstract T Visit(MultiplicationNode node);\n    public abstract T Visit(DivisionNode node);\n    public abstract T Visit(NegateNode node);\n    public abstract T Visit(FunctionNode node);\n    public abstract T Visit(NumberNode node);\n\n    public T Visit(ExpressionNode node)\n    {\n        return Visit((dynamic)node);\n    }\n}\n```\n\n```csharp\nif (node is AdditionNode) {\n    return Visit((AdditionNode)node);\n} else if (node is SubtractionNode) {\n    return Visit((SubtractionNode)node);\n} else if ...\n```\n\n```csharp\ninternal class EvaluateExpressionVisitor : AstVisitor<double>\n{\n    public override double Visit(AdditionNode node)\n    {\n        return Visit(node.Left) + Visit(node.Right);\n    }\n\n    public override double Visit(SubtractionNode node)\n    {\n        return Visit(node.Left) - Visit(node.Right);\n    }\n\n    public override double Visit(MultiplicationNode node)\n    {\n        return Visit(node.Left) * Visit(node.Right);\n    }\n\n    public override double Visit(DivisionNode node)\n    {\n        return Visit(node.Left) / Visit(node.Right);\n    }\n\n    public override double Visit(NegateNode node)\n    {\n        return -Visit(node.InnerNode);\n    }\n\n    public override double Visit(FunctionNode node)\n    {\n        return node.Function(Visit(node.Argument));\n    }\n\n    public override double Visit(NumberNode node)\n    {\n        return node.Value;\n    }\n}\n```\n\n```csharp\ninternal class Program\n{\n    private static void Main()\n    {\n        while (true)\n        {\n            Console.Write(\"> \");\n            var exprText = Console.ReadLine();\n\n            if (string.IsNullOrWhiteSpace(exprText))\n                break;\n\n            var inputStream = new AntlrInputStream(new StringReader(exprText));\n            var lexer = new MathLexer(inputStream);\n            var tokenStream = new CommonTokenStream(lexer);\n            var parser = new MathParser(tokenStream);\n\n            try\n            {\n                var cst = parser.compileUnit();\n                var ast = new BuildAstVisitor().VisitCompileUnit(cst);\n                var value = new EvaluateExpressionVisitor().Visit(ast);\n\n                Console.WriteLine(\"= {0}\", value);\n            }\n            catch (Exception ex)\n            {\n                Console.WriteLine(ex.Message);\n            }\n\n            Console.WriteLine();\n        }\n    }\n}\n```\n\n```text\nexpr\n```\n\n```text\nMathParser.*Context\n```\n\n```text\nMathBaseVisitor<T>\n```\n\n```text\nVisitFuncExpr\n```\n\n```text\nSystem.Math\n```\n\n```text\nAbstractParseTreeVisitor<T>\n```\n\n```text\ndynamic\n```\n\n```text\nif\n```\n\n```text\nString source_code = \"...your cpp source code...\";\n\nCodePointCharStream stream_from_string = CharStreams.fromString(source_code);\nCPP14Lexer lexer = new CPP14Lexer(new ANTLRInputStream(source_code));\nCommonTokenStream tokens = new CommonTokenStream(lexer);\nCPP14Parser parser = new CPP14Parser(tokens);\n\nString parserName = \"CPP14Parser\";\nClassLoader cl = Thread.currentThread().getContextClassLoader();\nClass<? extends Parser> parserClass = null;\nparserClass = cl.loadClass(parserName).asSubclass(Parser.class);\n\nString startRuleName = \"translationunit\"; //as specified in my CPP14.g4 file\nMethod startRule = parserClass.getMethod(startRuleName);\nParserRuleContext tree = (ParserRuleContext)startRule.invoke(parser, (Object[])null);\nSystem.out.println(tree.toStringTree(parser));\n```\n\n```text\nimport java.lang.reflect.Method;\nimport org.antlr.v4.runtime.CommonTokenStream;\nimport org.antlr.v4.runtime.CharStreams;\nimport org.antlr.v4.runtime.CodePointCharStream;\nimport org.antlr.v4.runtime.ANTLRInputStream;\nimport org.antlr.v4.runtime.ParserRuleContext;\nimport org.antlr.v4.runtime.Parser;\n```\n\n```text\njava -cp .:antlr-4.9-complete.jar org.antlr.v4.gui.TestRig CPP14 translationunit -tree filename.cpp\n```\n\n```text\njava -jar yournaltrfile.jar yourgrammar.g4\n```\n\n```text\n// START: header\ngrammar VecMath;\ntokens { VEC }       // define imaginary token for vector literal\n// END: header\n\n// START: stat\nprog:   stat+ ;              // build list of stat trees\nstat:   ID assign='=' expr  #StatAssign // '=' is operator subtree root\n    |   print='print' expr  #StatPrint  // 'print' is subtree root\n    ;\n// END: stat\n\n// START: expr\nexpr: left=expr op=('*'|'.') right=expr #ExprMult // '*', '.' are roots\n    | left=expr op='+' right=expr       #ExprAdd  // '+' is root node\n    | '[' expr (',' expr)* ']'    #ExprVec  // VEC is root\n    | INT                               #ExprInt\n    | ID                                #ExprId\n    ;\n// END: expr\n\nID  :   'a'..'z'+ ;\nINT :   '0'..'9'+ ;\nWS  :   (' '|'\\r'|'\\n')+ -> skip ;\n```\n\n```text\npackage walking.v4.vecmath_ast.impl;\n\nimport org.antlr.v4.runtime.CommonToken;\n\nimport walking.v4.vecmath_ast.antlr.VecMathBaseVisitor;\nimport walking.v4.vecmath_ast.antlr.VecMathParser.ExprAddContext;\nimport walking.v4.vecmath_ast.antlr.VecMathParser.ExprContext;\nimport walking.v4.vecmath_ast.antlr.VecMathParser.ExprIdContext;\nimport walking.v4.vecmath_ast.antlr.VecMathParser.ExprIntContext;\nimport walking.v4.vecmath_ast.antlr.VecMathParser.ExprMultContext;\nimport walking.v4.vecmath_ast.antlr.VecMathParser.ExprVecContext;\nimport walking.v4.vecmath_ast.antlr.VecMathParser.ProgContext;\nimport walking.v4.vecmath_ast.antlr.VecMathParser.StatAssignContext;\nimport walking.v4.vecmath_ast.antlr.VecMathParser.StatContext;\nimport walking.v4.vecmath_ast.antlr.VecMathParser.StatPrintContext;\nimport walking.v4.vecmath_ast.antlr.VecMathParser;\n\n\npublic class VecMathBuildASTVisitor extends VecMathBaseVisitor<AST> {\n    @Override\n    public AST visitProg(ProgContext ctx) {\n        AST ast = new AST();\n        for (StatContext stmt : ctx.stat()) {\n            ast.addChild(visit(stmt));\n        }\n        return ast;\n    }\n\n    @Override\n    public AST visitStatAssign(StatAssignContext ctx) {\n        AST ast = new AST(ctx.assign);\n\n        ast.addChild(new AST(ctx.ID().getSymbol()));\n        ast.addChild(visit(ctx.expr()));\n        return ast;\n    }\n\n    @Override\n    public AST visitStatPrint(StatPrintContext ctx) {\n        AST ast = new AST(ctx.print);\n        ast.addChild(visit(ctx.expr()));\n        return ast;\n    }\n\n    @Override\n    public AST visitExprMult(ExprMultContext ctx) {\n        AST ast = new AST(ctx.op);\n        ast.addChild(visit(ctx.left));\n        ast.addChild(visit(ctx.right));\n        return ast;\n    }\n\n    @Override\n    public AST visitExprAdd(ExprAddContext ctx) {\n        AST ast = new AST(ctx.op);\n        ast.addChild(visit(ctx.left));\n        ast.addChild(visit(ctx.right));\n        return ast;\n    }\n\n    @Override\n    public AST visitExprVec(ExprVecContext ctx) {\n        AST ast = new AST(new CommonToken(VecMathParser.VEC, \"VEC\"));\n        for (ExprContext expr : ctx.expr()) {\n            ast.addChild(visit(expr));\n        }\n        return ast;\n    }\n\n    @Override\n    public AST visitExprId(ExprIdContext ctx) {\n        AST ast = new AST(ctx.ID().getSymbol());\n        return ast;\n    }\n\n    @Override\n    public AST visitExprInt(ExprIntContext ctx) {\n        AST ast = new AST(ctx.INT().getSymbol());\n        return ast;\n    }\n}\n```\n\n```text\npackage  walking.v4.vecmath_ast.impl;\n\nimport org.antlr.v4.runtime.CommonToken;\nimport org.antlr.v4.runtime.Token;\n\nimport walking.v4.vecmath_ast.antlr.VecMathParser;\n\nimport java.util.ArrayList;\nimport java.util.List;\n\n// Homogenous AST node type \npublic class AST {\n   Token token;             // from which token do we create this node?\n   List<AST> children;      // normalized list of children \n\n   public AST() { ; } // for making nil-rooted nodes\n\n   public AST(Token token) { this.token = token; }\n\n   /** Create node from token type; used mainly for imaginary tokens */\n   public AST(int tokenType) { this.token = new CommonToken(tokenType); }\n\n   /** external visitors execute the same action for all nodes\n    * with same node type while walking\n    */\n    public int getNodeType() { return token.getType(); }\n\n    public void addChild(AST t) {\n        if (children == null) children = new ArrayList<>();\n        children.add(t);\n    }\n\n    public List<AST> getChildren() { return children; }\n    \n    /** to represent flat lists. A list is a subtree w/o a root, which we simulate\n     * with a nil root node. A nil node is a node with token == null.\n     */\n    public boolean isNil() { return token == null; }\n\n    /** Compute string for single node */\n    public String toString() { \n        String typeName = VecMathParser.VOCABULARY.getSymbolicName(getNodeType());\n        typeName = typeName == null ? token.getText() : typeName;\n        return token != null ? \"<\" +typeName +\", '\" + token.getText() +\"'>\": \"nil\"; \n    }\n\n    /** Compute string for a whole tree */\n    public String toStringTree() {\n        if (children == null || children.size() == 0) return this.toString();\n\n        StringBuffer buf = new StringBuffer();\n        if (!isNil()) {\n           buf.append('(');\n           buf.append(this.toString());\n           buf.append(' ');\n        }\n        for (int i = 0; i < children.size(); i++) {\n            AST t = (AST) children.get(i); // normalized (unnamed) children\n            if (i>0) buf.append(' ');\n            buf.append(t.toStringTree());\n        }\n        if (!isNil()) buf.append(')');\n        return buf.toString();\n    }\n}\n```\n\n```text\npackage walking.v4.vecmath_ast;\n\nimport org.antlr.v4.runtime.CommonTokenStream;\nimport org.antlr.v4.runtime.tree.ParseTree;\nimport org.antlr.v4.runtime.CharStream;\nimport org.antlr.v4.runtime.CharStreams;\n\nimport walking.v4.vecmath_ast.antlr.VecMathLexer;\nimport walking.v4.vecmath_ast.antlr.VecMathParser;\n\nimport walking.v4.vecmath_ast.impl.AST;\nimport walking.v4.vecmath_ast.impl.VecMathBuildASTVisitor;\n\npublic class Test {\n    public static void main(String[] args) throws Exception {\n        CharStream input = CharStreams.fromFileName(args[0]);\n        VecMathLexer lexer = new VecMathLexer(input);\n\n        CommonTokenStream tokens = new CommonTokenStream(lexer);\n        VecMathParser parser = new VecMathParser(tokens);\n\n        ParseTree tree = parser.prog();\n\n        for (AST ast : new VecMathBuildASTVisitor().visit(tree).getChildren()) {\n            System.out.println(ast.toStringTree());\n        }\n    } \n}\n```\n\n```text\nx = 3 + 4\ny = 3 + 4 + 5\na = 3 * 4\na = 3 * 4 * 5\nc = 3 * 4 + 5\nprint x * [2, 3, 4]\nprint x * [2+5, 3, 4]\n```\n\n```text\n(<=, '='> <ID, 'x'> (<+, '+'> <INT, '3'> <INT, '4'>))\n(<=, '='> <ID, 'y'> (<+, '+'> (<+, '+'> <INT, '3'> <INT, '4'>) <INT, '5'>))\n(<=, '='> <ID, 'a'> (<*, '*'> <INT, '3'> <INT, '4'>))\n(<=, '='> <ID, 'a'> (<*, '*'> (<*, '*'> <INT, '3'> <INT, '4'>) <INT, '5'>))\n(<=, '='> <ID, 'c'> (<+, '+'> (<*, '*'> <INT, '3'> <INT, '4'>) <INT, '5'>))\n(<print, 'print'> (<*, '*'> <ID, 'x'> (<VEC, 'VEC'> <INT, '2'> <INT, '3'> <INT, '4'>)))\n(<print, 'print'> (<*, '*'> <ID, 'x'> (<VEC, 'VEC'> (<+, '+'> <INT, '2'> <INT, '5'>) <INT, '3'> <INT, '4'>)))\n```\n\n========================================\n\nComments:\n- Could you some of what you've tried?\n- @SandyGifford I edited my post trying to explain... I don't have my code right now because I just deleted what I made. Right now I only have the generated codes from ATNLR4 (parser, lexer and base visitors and listeners)\n- Unfortunately, I don't know anything about ANTLR (you came up in my queue) but you have raised the quality of the post!\n- See this answer for a discussion of \"CST\" vs \"AST\": stackoverflow.com/a/29456792/120163 The commentary at the end discusses how another actually gets an AST (essentially by walking the CST and manufacturing the AST he wants).\n- I was in a similar situation like you, after I failed to find a super simple AST example using ANTLR I have created one myself github.com/adamsiemion/antlr-java-ast\n- @AdamSiemion your example errors with cannot find symbol class Java8Lexer\n- This method is quite manual work. Is there another solution which does not need creating code for every kind of node?.\n- @Waschbaer IMO fine-tuning the nodes manually improves the maintainability and is worth it, but you may disagree. ANTLR 3 had an AST output mode, so you could use that, if you're comfortable with the usage of an outdated tool. Or perhaps you could use metaprogramming/templating to generate some boilerplate code, but you may well end up with more work in the end. One other option is to work with the CST directly.\n- In many years of reading SO for tips, suggestions, and answers, this stands out as one of the best answers I've seen. Outstanding presentation of the concepts.\n- How did you get the MathBaseVisitor class? My grammar is called QueryParser, and I don't have a QueryParserBaseVisitor class. I have a QuertyParserBaseListener, but it's more like an in-out walker, which is practically impossible to work with. (I tried)\n- @Trejkaz this class is generated by ANTLR, just like the listener - you should have a `QueryBaseVisitor` (unless your grammar name *includes* that `Parser` part, but in that case you'll also have a `QueryParserParser`). IIRC the NuGet package generates it automatically, but if you run ANTLR manually, you have to add the `-visitor` option.\n- Ah, `-visitor` is the solution. The default Gradle task doesn't turn it on either. :|\n- @Trejkaz oops, I totally forgot this question was about java :) so yeah, `-visitor` (forget the NuGet thing)\n- @LucasTrzesniewski Could you clarify how you are using the `AstVisitor` class inside the `BuildAstVisitor` class? I'm trying to implement an AST in Java for my grammar but I'm stuck now that I've created the AST Nodes. Also, could you, if it still exists, make the solution available as a whole? It would help a lot to understand the connections between all the classes.\n- @Johannes I am *not* using `AstVisitor` in `BuildAstVisitor` at all. `AstVisitor` is used as a base class for `EvaluateExpressionVisitor` which is used in a subsequent step: `BuildAstVisitor` converts the CST to an AST, and `EvaluateExpressionVisitor` is called on that AST. I can take a look to see if I still have the solution files, but 100% of the code is in the answer.\n- @LucasTrzesniewski Thanks for the clarification. I'm still curious though, because you use the `Visit` method in the `BuildAstVisitor` which seems to be defined in the abstrtact `AstVisitor`... Additionally, I'm struggeling with the `BuildAstVisitor` when you need to create an abstract node from multiple rules. In your example you assign a label to multiple tokens, but that doesn't seem to work for rules. (I've opened a question here, if you would be so nice as to take a look at it stackoverflow.com/questions/44003999/&hellip;).\n- @Johannes I found the solution, I uploaded it here raw with all the generated output and dependencies. It wasn't meant to look pretty, almost everything is in the same file. The two `Visit` methods are unrelated, they happen to have the same name, but as both classes are visitors, well, they use the same name :)\n- @LucasTrzesniewski thanks again for this post. For anyone interested in implementing this concept with Java, I made a small project based off of this answer: github.com/nmancus1/parseva-math\n- This is all nice and easy when you simply return a double for everything. How would you do it if you had to sometimes return different data types ? I tried returning a general interface and have different data types extend from it, but the visitor code becomes bloated with casting and instance checks..\n- @Lorenzo I'm not really surprised you have lots of casts, but it's hard to tell if that's normal or not without seeing an example. I encourage you to ask a new question and provide the details.\n- @Lucas Trzesniewski I posted a question about it here (i want to for example be able to do arithmetics with either strings or numbers just like python, and so the return type would change based on if there's a string as an operand or not): stackoverflow.com/q/70224725/3225638\n- I tried using your 'small' project but you have hundreds of files with no comments, it is impossible to figure out what's going on. You have everything wrapped in your own version of wrapper functions. A user would have to download your entire project and use it as-is, and learn to use your new classes (GenericParser??). I can't use your code to figure out how to create my own AST in my own code.\n- Hi John inmemantlr's code base consists of 48 JavaClasses (`find inmemantlr-api&#47;src&#47;main -name \"*.java\" | nl`) most of which are pretty well-commented (javadoc.io/doc/com.github.julianthome/inmemantlr-api/1.3.9)&zwnj;&#8203;. To illustrate the points you've mentioned above (API usage, ParseTree creation), I have provided explanations in the `README.md` and provided test cases in github.com/julianthome/inmemantlr/tree/master/inmemantlr-api&zwnj;&#8203;/&hellip;. However, in case you have issues with the tool, would be happy to help you. Please just drop me an email or create a an issue on github.\n- Could it be that you've pulled the grammars-v4 submodule (please have a look at `inmemantlr-api&#47;src&#47;test&#47;resources&#47;grammars-v4`)? Actually, this module is not part of inmemantlr's code-base; it is used to make sure that inmemantlr works on all the grammars-v4 grammars. However, the submodule is not pulled per default when executing `git clone https:&#47;&#47;github.com&#47;julianthome&#47;inmemantlr`.\n- @Julian amazing tools. It's really powerful and easy to use. Thanks a ton for sharing it with the community. Would like to see more examples in the wiki.\n- @VaibhavJain thank you very much. If you have some suggestions on how to improve the documentation/tool/API, I would be glad if you could create an issue on the github project page ;). Thanks again.\n- Why use reflection when the class/method are available at compile time?\n- Your imports don't include CPP14Parser or CPP14Lexer. Are we supposed to implement it ourselves or did you miss it?","metadata":{"transformedAt":"2026-08-18T18:32:17.744Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":25,"totalLines":631,"estimatedTokens":6037}}32{"id":"stack-3455456","source":"stackoverflow","questionId":3455456,"title":"What kinds of patterns could I enforce on the code to make it easier to translate to another programming language?","tags":["php","python","compiler-construction","coding-style","abstract-syntax-tree"],"text":"Title: What kinds of patterns could I enforce on the code to make it easier to translate to another programming language?\nTags: php, python, compiler-construction, coding-style, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI am setting out to do a side project that has the goal of translating code from one programming language to another. The languages I am starting with are PHP and Python (Python to PHP should be easier to start with), but ideally I would be able to add other languages with (relative) ease. The plan is:\n\nThis is geared towards web development. The original and target code will be be sitting on top of frameworks (which I will also have to write). These frameworks will embrace an MVC design pattern and strict coding conventions. This should make translation somewhat easier.\n\nI am also looking at IOC and dependency injection, as they might make the translation process easier and less error prone.\n\nI'll make use of Python's parser module, which lets me fiddle with the Abstract Syntax Tree. Apparently the closest I can get with PHP is token_get_all(), which is a start.\n\nFrom then on I can build the AST, symbol tables and control flow.\n\nThen I believe I can start outputting code. **I don't need a perfect translation**. I'll still have to review the generated code and fix problems. Ideally the translator should flag problematic translations.\n\nBefore you ask \"What the hell is the point of this?\" The answer is... It'll be an interesting learning experience. If you have any insights on how to make this less daunting, please let me know.\n\n### EDIT:\n\nI am more interested in knowing what kinds of patterns I could enforce on the code to make it easier to translate (ie: IoC, SOA ?) the code than how to do the translation.\n\n========================================\n\nTop Answer:\nMy answer will address the specific task of parsing Python in order to translate it to another language, and not the higher-level aspects which Ira addressed well in his answer.\n\nIn short: **do not use the parser module, there's an easier way.**\n\nThe `ast` module, available since Python 2.6 is much more suitable for your needs, since it gives you a ready-made AST to work with. I've written an article on this last year, but in short, use the `parse` method of `ast` to parse Python source code into an AST. The `parser` module will give you a parse tree, not an AST. Be wary of the difference. \n\nNow, since Python's ASTs are quite detailed, given an AST the front-end job isn't terribly hard. I suppose you can have a simple prototype for some parts of the functionality ready quite quickly. However, getting to a complete solution will take more time, mainly because the semantics of the languages are different. A simple subset of the language (functions, basic types and so on) can be readily translated, but once you get into the more complex layers, you'll need heavy machinery to emulate one language's core in another. For example consider Python's generators and list comprehensions which don't exist in PHP (to my best knowledge, which is admittedly poor when PHP is involved).\n\nTo give you one final tip, consider the `2to3` tool created by the Python devs to translate Python 2 code to Python 3 code. Front-end-wise, it has most of the elements you need to translate Python to *something*. However, since the cores of Python 2 and 3 are similar, no emulation machinery is required there.\n\n========================================\n\nCode:\n```text\nast\n```\n\n```text\nparse\n```\n\n```text\nast\n```\n\n```text\nparser\n```\n\n```text\n2to3\n```\n\n```text\nword = 'This is not a word'\nprint word[::-2]\n```\n\n```text\nmyinput = raw_input(\"Enter something: \")\nprint myinput.replace('a', 'A')\nprint sum(ord(c) for c in myinput)\nprint myinput[::-1]\n```\n\n```text\nPython source code -> Python ast -> Python source code compatible with Veloce mode -> Python ast -> JavaScript source code\n```\n\n```text\nspam.egg\n```\n\n```text\ngetattribute(spam, \"egg\")\n```\n\n========================================\n\nComments:\n- Have you looked at systems like the .NET CLR or Perl6's Parrot? They compile a set of languages down to an intermediate representation which can be run by a common interpreter. If you can go back up from the intermediate representation to a language, you've got a translator.\n- @Borealid AFAIK the .NET CIL is (relatively) easy to compile *into*, but good luck getting readable code back from that. Looking at Parrot now.\n- There are similar projects for other languages; I am not sure how rich their authors are. And I am actually restraining myself a lot here, by needing a framework and adhering to strict coding conventions.\n- I cannot add any specific knowledge, but have you looked at pyjamas (pyjs.org), specifically translator.py? This is a python to javascript compiler.\n- @stephan I love the name (pyjamas). Thanks for the link. I think Python made it really easy to do these translations thanks to the parser module. That's why there's a Python-to-Perl translator but not the other way around.\n- Have a look at phpqatools.org for some tools that allow AST and opcode based analysis of PHP code.\n- @NullUserException In response to \"AFAIK the .NET CIL is (relatively) easy to compile into, but good luck getting readable code back from that.\" Well, this is exactly what Reflector does!\n- @NullUserException Indeed, Reflector does a perfect job. Always remember pdbs :)\n- Re EDIT: If you have control over the code that will be translated, the most obvious thing to do is to avoid constructs that are hard to translate! For example, C is much easier to translate to Java if there isn't any pointer arithmetic. For Python, I'd probably stay away from closures. The other thing you can do is to write the source code in a way that more difficult parts to translate are always coded idiomatically, making them easier to recognize and to handle special cases.\n- Can't find a question here.\n- write an llvm frontend\n- Have you ever considered contributing your \"painfully built\" PHP definition back to the PHP community at large, or is to too closely associated with your own revenue stream to make that feasible?\n- I've been asked to make everything we do \"open source\" by lots of folks that didn't want to contribute to a revenue stream, and didn't have the energy to do the work and open source it themselves. If you only contribute a small part to a very big project, and/or you have another source of income, \"open source\" seems fine. If you've done all the work yourself and its your only source of income, this is a lot less attractive. [I don't want to get into a discussion about the relative merits of \"free software\" philosophy, so I won't participate in any futher comments along this line]\n- I agree with what you said here, which is why I phrased the question as I did. I guess we are to intuit from that response that you feel it's too closely tied to your revenue, and there's absolutely nothing wrong with that - I just thought it worth asking.\n- @IraBaxter You just say common idioms about pratices related to computer that can be applied to a lot of other pratices. The only thing intersting in all you've written are the links to semanticdesigns.com (which happens to be your company)\n- @IraBaxter why is there a link to a press release for your company in this answer?\n- @xaxxon: It provides explicit proof that I know what I'm talking about. Readers appear to approve of the answer, even if you don't.\n- It provides proof you made a web page. Why don't you provide a link to relevant source for it. That would actually be useful -- assuming it's any good.\n- You often provide links to Clang-related pages in your answers. That only proves that somebody else can make a web page. Most of us assume that a well-written web page implies there is serious, real work behind and not just some fraudulent attempt to deceive the reader as you seem to imply in your answer. Do you actually believe that web page is fraudulent? The page contains reference information to \"relevant\" source; it is anonomized because the contract for the work required that. That I can't help.\n- @xaxxon: *assuming its any good*. OK, you can accuse the vision and tools of being useless (\"not any good'). Or, you can decide that I am not a liar, and that the tools were used to accomplish a very hard technical reengineering task for a critical system in one of the world's most advanced aircraft. I may not be able to change your opinion. Your opinion doesn't change the facts.\n- If you want validation of the ideas in/behind DMS, you can look at some technical paper published after being reviewed by presumably unbiased researchers knowledgeable in the field. See semanticdesigns.com/Company/Publications/&hellip; and Akers, R., Baxter, I., Mehlich, M. , Ellis, B. , Luecke, K., Case Study: Re-engineering C++ Component Models Via Automatic Program Transformation, Information & Software Technology 49(3):275-291 2007. sciencedirect.com/science/article/pii/S0950584906001856\n- Weeeell. `2to3` is just AST to AST. It doesn't support doing anything that goes beyond the capabilities of the `ast` module. Notice that all of the translations go *from* syntax supported by the host python process *to* syntax supported by the host python process. There's no translator that does adds, say, function annotations, because 2.6 doesn't support it.\n- ... and the OP's question might be framed, short term, how to get from Python 2.6 AST to ... something in PHP. The ast module likely won't want to represent the PHP syntax well, so its not even ast to ast.\n- @Aaron: `2to3` can be seen as an example of using the AST generated from `ast`.\n- AFAIK, 2to3 arguably is an easier translation than Python to PHP (after all, its Python to Python, right)? And even it doesn't work particularly well. Notice the large amount of Python 2.6 that hasn't been shoved through 2to3 yet... because there's apparantly a bunch of post translation hand patching that still has to be done. If were 100% automated, Python 2.6 would be dead.\n- Actually, I didn't say \"don't bother\". What I said was, \"translating languages in a general ways is very hard\". If the OP proceeds down his original path of using Python trees to try and generate PHP, he will learn a lot and I'm all in favor of the learning experience; I started there, too. He won't be able to add new languages easily.\n- @IraBaxter I can't support your statement, doing Python->PHP and PHP->Javascript would be kind of easy. cf. last part of stackoverflow.com/a/22850139/140837 in the middle of the answer I also deal with your \"argumentation\"\n- It was a design goal of Vala to be translated to C and make development with gnome libraries easy.\n- \"Have you ever written a tokenizer/parser based on a language grammar? \" I have done it using JavaCC.\n- Joel's intern did a partial job over a summer. His source language was a subset of an existing language, and presumably this subset could be adjusted somewhat. That makes the job a lot easier. Similarly, NullPointerException might want to start with the easier parts of Python, perhaps passing through the harder stuff for manual conversion (as noted in the questions).\n- @NullUserException: You'll have some exposure, but you'll basically be doing a re-implementation of JavaCC, only instead of Java as the output language, you'll be doing . @David, quite so. Even Thistle needs some help on some of the language constructs. If I were the OP, I'd go for functional first, then optimize, otherwise I'd be stuck forever trying to get C++ to do string slicing (with steps) :p\n- @WayneWerner For the record, languages like C# don't require newlines at all. (At least, not after you've stripped out the single-line comments.) So you could write any C# program in one line. But of course I understand what you're getting at.\n- @aboveyou00: I don't think that is right. If you disallow preprocessor conditionals, you might be right.\n- @IraBaxter (you again...) the syntax you say hard to translate is not hard to translate at all if you write it as myinput.__getitem__(Slice(None, None, -1)) or myinput.__slice__(None, None, -1). I don't understand what you find difficult.\n- @amirouche: You seem to be willfully misreading what is said. I merely made the point, specifically in response to aboveyou00, that C# *does* have preprocessor conditionals, and those conditionals *require* newlines, so you cannot write \"any C# program\" in a single line. Frankly, presence of newlines or not do not affect the fundamental difficulties of translation in any interesting way.\n- @IraBaxter it's of course not willfully at all, I'm not coming to SO to fool people around especially with my real name. The comment was meant to Wayne Werner.\n- @amirouche: if the comment was meant for Wayne, don't prefix it with my name. That just adds confusion. I don't understand your remark about \"real name\". AFAIK, only \"aboveyou00\" hasn't provided his real name, and I don't see how it matters in this discussion.\n- The only thing that remains thrilling for me about one-to-one computer language to computer language translation are described in stackoverflow.com/questions/22621164/&hellip;\n- I second the other answer about data types. In Pythonium I wasn't even planning to support a correct integer & float type in the compliant mode without asm.js.\n- OK, so if I give you a Python package of 100K SLOC, and you run it through your \"translator\", do I get a working program? How much post-translation hand-work does it take to fix it? What you said here was, \"given an already existing good parser for Python that builds ASTs, I can build a partial translator in 6 months\". Nobody is surprised. 6 months isn't by most people's standards \"kind of easy\" (quoting from another of your comments). Solving the remaining problems will require more effort. My answer said, basically \"doing this isn't easy\", and \"doing it in a general way is hard\".\n- ... that last point in response to OP's original desire: \"ideally I would be able to add other languages with (relative) ease.\", which my answer addresses specifically.\n- I beg to disagree, especially when you know what you are doing, it's easy and what follows is not hard it's just a matter of getting things done. I'm not sure where you are dealing with anything specific to the question. You are saying in 4 or 5 paragraph that your company does it, and it's hard. But otherwise you are spreading FUD about the subject, while being kind off-topic like you are in stackoverflow.com/questions/22621164/&hellip;. In full time 6 month I would have written a full translator.\n- @IraBaxter anwser is not answering question.","metadata":{"transformedAt":"2026-08-18T18:32:17.745Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":129,"estimatedTokens":3659}}33{"id":"stack-5099384","source":"stackoverflow","questionId":5099384,"title":"Is C open source?","tags":["c","compiler-construction","open-source"],"text":"Title: Is C open source?\nTags: c, compiler-construction, open-source\nSource: Stack Overflow\n\nQuestion:\nDoes C (or any other low-level language, for that matter) even have source, or is the compiler the part that \"does all the work\", including parsing? If so, couldn't different compilers have different C dialects? Where does the stdlib factor into this? I would really like to know how this works.\n\n========================================\n\nTop Answer:\nC is a standard which specifies how C compilers should generate programs.\n\nC itself doesn't have any source code, just like a musical note doesn't have any plastic.\n\nSome C compilers, such as GCC, are open source.\n\n========================================\n\nCode:\n```text\nstdlib\n```\n\n```text\nstdlib\n```\n\n```text\n-std=...\n```\n\n```text\nc99\n```\n\n```text\ntypeof()\n```\n\n========================================\n\nComments:\n- good answer, although the musical note / plastic analogy was a bit surreal. :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.745Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":43,"estimatedTokens":238}}34{"id":"stack-18247888","source":"stackoverflow","questionId":18247888,"title":"How can a C compiler be written in C?","tags":["c","compiler-construction","kernighan-and-ritchie"],"text":"Title: How can a C compiler be written in C?\nTags: c, compiler-construction, kernighan-and-ritchie\nSource: Stack Overflow\n\nQuestion:\nThis question may stem from a misunderstanding of compilers on my part, but here goes...\n\nOne can find the following statement in the preface to the first edition of K&R (page xi):\n\n The operating system, *the C compiler*, and essentially all UNIX applications programs (including all of the software used to prepare this book) are written in C.\n\n(my emphasis)\n\nHere's what I don't understand: doesn't that C compiler have to be compiled itself before it can compile any C code? And if that C compiler is written in C, wouldn't compiling it require an already existing C compiler?!\n\nThe only way out of this infinite-regression conundrum (or chicken-and-egg problem) is that the C compiler written in C that K&R are referring to was actually compiled with an already existing C compiler that was written in a language other than C. The C compiler written in C then superseded the latter.\n\nOr am I completely off?\n\n========================================\n\nTop Answer:\nUsually, a first compiler is written in another language (directly in PDP11 assembler in this case, or in C for most of the \"modern\" languages). Then, this first compiler is used to program a compiler written in the language itself.\n\nYou can read this page about the history of the C language. You will see that it is also strongly linked to the UNIX system.\n\n========================================\n\nComments:\n- Well, you start with one in assembly or whatever, and once you have that, it's possible to use it to make one in C and so on afterward.\n- C was first written in B which was a stripped down BCPL which was a stripped down Algol 60 I believe. B was probably written in a macro assembler though.\n- Pascal cuoq... In the first point,language Y should have a compiler first,right?I think languages like Clojure were written in this way.Could you explain \"Hand compiling\"?\n- @AV94 Hand-compiling would mean, generating by hand the same output that the compiler would generate, using possibly a calculator or such aids to compute results as needed.\n- Be careful transporting 1970 truths to today. GCC switched to C++ :-)\n- Hmmm, GCC in C++... I'm a bit doubtful about that (and scared also! :-)).\n- gcc.gnu.org/gcc-4.8/changes.html first line.","metadata":{"transformedAt":"2026-08-18T18:32:17.745Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":36,"estimatedTokens":588}}35{"id":"stack-8372414","source":"stackoverflow","questionId":8372414,"title":"Purpose of \"let expression\" (LetExpr) in the Java compiler?","tags":["java","compiler-construction","language-design","let"],"text":"Title: Purpose of \"let expression\" (LetExpr) in the Java compiler?\nTags: java, compiler-construction, language-design, let\nSource: Stack Overflow\n\nQuestion:\nThe Java compiler seems to have support for `let` expressions in `com.sun.tools.javac.tree.*` (look for `LetExpr`).\n\nOne comment in JCTree even mentions some syntax\n\n```\n(let int x = 3; in x+2)\n```\n\nwhich of course is not accepted by the grammar of the language and rejected in an earlier compiler phase.\n\nI'm wondering about the origin of this construct, which I have never seen before.\n\nIs it used internally by `javac` or is it synthesized by other tools? Is it maybe just an artifact from the very early days of Java from a language feature which never saw the light?\n\nIs there anything useful which can be done with it today?\n\n*Generally speaking, why does it exist?*\n\n========================================\n\nTop Answer:\nThis is called the let form and is used for \"abbreviating\". \n\nOn the other hand, in procedural languages this is called \"declaring a variable\" because the variable's \"value\" cell can mutate in procedure languages. (In functional languages, it's just an abbreviation and no different to just writing it out in the first place)\n\nI can think of a lot of languages that use it in the source code the language user writes (Haskell, ML, Scheme, SBCL, Arc, ...), so not sure how you didn't see it yet... \n\nOr did you mean just in Java?\n\n```\nlet x = 2 in (x + 5)\n```\n\nIs shorthand for:\n\n```\n(\\x (x + 5)) 2\n```\n\nwhich will eventually be reduced to \n\n```\n(2 + 5)\n```\n\nwhere `\\` is supposed to be lambda.\n\nAs for why it's in Java, not sure. What it's supposed to do is declare variables, so check whether it's used there.\n\n========================================\n\nCode:\n```text\n(let int x = 3; in x+2)\n```\n\n```text\nlet\n```\n\n```text\ncom.sun.tools.javac.tree.*\n```\n\n```text\nLetExpr\n```\n\n```text\njavac\n```\n\n```text\nInteger foo = 0;\nfoo++;\n```\n\n```text\nInteger foo = 0;\nlet int foo_helper = foo.intValue() in foo_helper++;\n```\n\n```text\nlet x = 2 in (x + 5)\n```\n\n```text\n(\\x (x + 5)) 2\n```\n\n```text\n(2 + 5)\n```\n\n```text\n\\\n```\n\n========================================\n\nComments:\n- That page doesn't say that it *exists* for autoboxing, only that it's *used* in autoboxing. Since the page is about a bug in autoboxing, not about let-expressions, it's not surprising that autoboxing is the only use for let-expressions that it mentions. (I'm not saying you're wrong -- I really don't know -- but I think it would be helpful to add a more relevant/convincing/explicit source, if you know of one.)\n- what does `let int foo_helper = foo.intValue() in foo_helper++;` even mean?\n- @ArtB Its similar to ml syntax. let var = value in expression. Thus in the the expression, all instances of var evaluate to value.\n- How is `let int foo_helper = foo.intValue() in foo_helper++;` different from `{ int foo_helper = foo.intValue(); foo_helper++; }`? It seems to be something like a pointer, no? In the second expression, the value of foo would not be updated after the expression\n- In a strict functional language (e.g. ML, Scheme), using a let binding is quite different from writing out the expression because the expression must be evaluated at the point where it is bound. The most likely use of this that comes to mind for Java is to convert code with side-effects to Static Single Assignment form, which is more easily analyzed to produce optimized bytecode.","metadata":{"transformedAt":"2026-08-18T18:32:17.745Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":111,"estimatedTokens":856}}36{"id":"stack-845355","source":"stackoverflow","questionId":845355,"title":"Do programming language compilers first translate to assembly or directly to machine code?","tags":["assembly","gcc","compilation","compiler-construction"],"text":"Title: Do programming language compilers first translate to assembly or directly to machine code?\nTags: assembly, gcc, compilation, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm primarily interested in popular and widely used compilers, such as gcc. But if things are done differently with different compilers, I'd like to know that, too.\n\nTaking gcc as an example, does it compile a short program written in C directly to *machine* code, or does it first translate it to human-readable assembly, and only then uses an (in-built?) assembler to translate the assembly program into binary, *machine* code -- a series of instructions to the CPU?\n\nIs using assembly code to create a binary executable a significantly expensive operation? Or is it a relatively simple and quick thing to do?\n\n(Let's assume we're dealing with only the x86 family of processors, and all programs are written for Linux.)\n\n========================================\n\nTop Answer:\nAlmost all compilers, including gcc, produce assembly code because it's easier---both to produce and to debug the compiler. The major exceptions are usually just-in-time compilers or interactive compilers, whose authors don't want the performance overhead or the hassle of forking a whole process to run the assembler. Some interesting examples include\n\nStandard ML of New Jersey, which runs interactively and compiles every expression on the fly.\n\nThe tinycc compiler, which is designed to be fast enough to compile, load, and run a C script in well under 100 milliseconds, and therefore doesn't want the overhead of calling the assembler and linker.\n\nWhat these cases have in common is a desire for \"instantaneous\" response. Assemblers and linkers are plenty fast, but not quite good enough for interactive response. Yet.\n\nThere are also a large family of languages, such as Smalltalk, Java, and Lua, which compile to bytecode, not assembly code, but whose implementations may later translate that bytecode directly to machine code without benefit of an assembler.\n\n(Footnote: in the early 1990s, Mary Fernandez and I wrote the New Jersey Machine Code Toolkit, for which the code is online, which generates C libraries that compiler writers can use to bypass the standard assembler and linker. Mary used it to roughly double the speed of her optimizing linker when generating `a.out`. If you don't write to disk, speedups are even greater...)\n\n========================================\n\nCode:\n```text\ngcc -v\n```\n\n```text\na.out\n```\n\n```text\nx = y + z + w\n```\n\n```text\nreg1 = y + z\nx = reg1 + w\n```\n\n========================================\n\nComments:\n- Related: Does a compiler always produce an assembly code? - no, big mainstream C compilers that provide a complete toolchain often go straight to machine code, especially ones (unlike GCC) that only target a few ISAs / object file formats. But yes, compilers with smaller dev teams often leave the object-file handling to an existing assembler. Also related: What do C and Assembler actually compile to?\n- No, MSVC's asm output is not something it actually generates if you don't ask for it. And unlike LLVM, It's not even a real reflection of exactly what it would put in an object file if you did just compile. (e.g. if you assemble its output with MASM, you'll get a different .obj. The compiler asm output adds extra definitions for functions you didn't use. I think I've read that you sometimes even get link errors if you try to separately compile + assemble + link instead of just compiling + linking with MSVC.)\n- Some historical compilers used to produce executables directly. Some could even write an executable .COM file in a single pass during compilation [following the code for each procedure, the compiler could output a list of patch-points within that procedure along with the address of the previous procedure's patch-point list; startup code could make all of the necessary patches when the code was loaded]. This made rapid compilation possible in a very small memory footprint, even when using floppy disks.\n- If MS compilers produce object code directly. Does that mean they have its own transformation process or they just transform in ram to assembly and then to object code, without saving the assemblycode as file and use that file as a next input?\n- `gcc` internally does truly compile to a temporary `.s` asm file, and runs `as` on it. The `-S` option just stops there. MSVC on the other hand normally only outputs a `.obj`, and it's assembly-output option makes a huge bloated `.asm` file (with definitions of templates you never called, or of library functions) that sometimes needs trimming down to even assemble + link correctly without duplicate-symbol errors. GCC does compile to asm in a very real sense during normal operation, MSVC doesn't. (Neither do ICC or clang/LLVM, but they can output asm that matches their .o)\n- True, but only GCC (out of big mainstream C/C++ compilers) actually writes asm as text to a file. GIMPLE is only dumped to a file as text if you use a debugging option, otherwise it's only represented via non-text data structures inside GCC's `cc1`. Similarly for LLVM-IR; it's probably never serialized into bytecode, let alone text, just passed around as data structures between the clang front-end and the LLVM back-end and its optimizer passes. I've heard of LLVM-GCC but IDK how it works. I guess you're saying it outputs a `.ll` of LLVM-IR, and runs llvm-as on it to optimize into a `.o`.\n- clang/LLVM, MSVC, and ICC all produce machine code directly. GCC is the exception, not the rule, among mainstream C/C++ compilers, at least for x86. These days, many compilers are implemented as front-ends for LLVM.\n- @PeterCordes please notice the date on my answer. The world has changed!\n- Clang didn't exist in 2009, but I think my point was still mostly true for big mainstream C++ implementations back then. Many compilers for other languages do leave the object file format handling to a separate assembler so this answer isn't wrong, just ignoring a few C++ compilers that get used more than many other smaller compilers combined. Or in other words, this answer could use some maintenance. (See also Does a compiler always produce an assembly code? for my attempt at answering basically a duplicate.)\n- *If a compiler directly converts code to binary code, by definition, it will be called assembler and not a compiler.* - Tell that to tcc, the Tiny C Compiler, which very directly compiles C source into x86 machine code, with out even an internal representation like GIMPLE or LLVM bytecode used internally. It's definitely *not* an assembler because its input is portable C.\n- Even clang/LLVM never actually creates a file containing asm text or LLVM bytecode, but it does have internal data structures that represent target-neutral LLVM \"instructions\" during optimization. Perhaps also ones that represent machine instructions in the final stages of optimization and codegen.\n- GCC's C and C++ compilers combine C pre-processing and actual compilation to asm into a single step, done by `&#47;usr&#47;lib&#47;gcc&#47;x86_64-pc-linux-gnu&#47;10.1.0&#47;cc1` or `cc1plus` for example. This has been the case for many years. Decades ago CPP was a separate step that produced a temp file, but that's no longer the case. Then yes, asm->object files is done with (usually) `as` from GNU Binutils (a separately-maintained package than GCC), and then linking with `ld` (also from Binutils).\n- GDB is yet another separate program, and is not involved at all in how the `gcc` front-end turns source into a linked executable.\n- Perhaps what you meant was that GDB's source code is in the same repository as GNU Binutils. This is true, although they're normally packaged separately. And is irrelevant to building executables.","metadata":{"transformedAt":"2026-08-18T18:32:17.745Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":65,"estimatedTokens":1950}}37{"id":"stack-390992","source":"stackoverflow","questionId":390992,"title":"JavaScript parser in Python","tags":["javascript","python","parsing","compiler-construction"],"text":"Title: JavaScript parser in Python\nTags: javascript, python, parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThere is a JavaScript parser at least in C and Java (Mozilla), in JavaScript (Mozilla again) and Ruby. Is there any currently out there for Python?\n\nI don't need a JavaScript interpreter, per se, just a parser that's up to ECMA-262 standards.\n\nA quick google search revealed no immediate answers, so I'm asking the SO community.\n\n========================================\n\nTop Answer:\nNowadays, there is at least one better tool, called `slimit`:\n\n SlimIt is a JavaScript minifier written in Python. It compiles\n JavaScript into more compact code so that it downloads and runs\n faster.\n\n \n SlimIt also provides a library that includes a JavaScript parser,\n lexer, pretty printer and a tree visitor.\n\nDemo:\n\nImagine we have the following javascript code:\n\n```\n$.ajax({\n type: \"POST\",\n url: 'http://www.example.com',\n data: {\n email: 'abc@g.com',\n phone: '9999999999',\n name: 'XYZ'\n }\n});\n```\n\nAnd now we need to get `email`, `phone` and `name` values from the `data` object.\n\nThe idea here would be to instantiate a `slimit` parser, visit all nodes, filter all assignments and put them into the dictionary:\n\n```\nfrom slimit import ast\nfrom slimit.parser import Parser\nfrom slimit.visitors import nodevisitor\n\ndata = \"\"\"\n$.ajax({\n type: \"POST\",\n url: 'http://www.example.com',\n data: {\n email: 'abc@g.com',\n phone: '9999999999',\n name: 'XYZ'\n }\n});\n\"\"\"\n\nparser = Parser()\ntree = parser.parse(data)\nfields = {getattr(node.left, 'value', ''): getattr(node.right, 'value', '')\n for node in nodevisitor.visit(tree)\n if isinstance(node, ast.Assign)}\n\nprint fields\n```\n\nIt prints:\n\n```\n{'name': \"'XYZ'\", \n 'url': \"'http://www.example.com'\", \n 'type': '\"POST\"', \n 'phone': \"'9999999999'\", \n 'data': '', \n 'email': \"'abc@g.com'\"}\n```\n\n========================================\n\nCode:\n```text\nfrom pynarcissus import jsparser\nfrom collections import defaultdict\n\nclass Visitor(object):\n\n    CHILD_ATTRS = ['thenPart', 'elsePart', 'expression', 'body', 'initializer']\n\ndef __init__(self, filepath):\n    self.filepath = filepath\n    #List of functions by line # and set of names\n    self.functions = defaultdict(set)\n    with open(filepath) as myFile:\n        self.source = myFile.read()\n\n    self.root = jsparser.parse(self.source, self.filepath)\n    self.visit(self.root)\n\n\ndef look4Childen(self, node):\n    for attr in self.CHILD_ATTRS:\n        child = getattr(node, attr, None)\n        if child:\n            self.visit(child)\n\ndef visit_NOOP(self, node):\n    pass\n\ndef visit_FUNCTION(self, node):\n    # Named functions\n    if node.type == \"FUNCTION\" and getattr(node, \"name\", None):\n        print str(node.lineno) + \" | function \" + node.name + \" | \" + self.source[node.start:node.end]\n\n\ndef visit_IDENTIFIER(self, node):\n    # Anonymous functions declared with var name = function() {};\n    try:\n        if node.type == \"IDENTIFIER\" and hasattr(node, \"initializer\") and node.initializer.type == \"FUNCTION\":\n            print str(node.lineno) + \" | function \" + node.name + \" | \" + self.source[node.start:node.initializer.end]\n    except Exception as e:\n        pass\n\ndef visit_PROPERTY_INIT(self, node):\n\n    # Anonymous functions declared as a property of an object\n    try:\n        if node.type == \"PROPERTY_INIT\" and node[1].type == \"FUNCTION\":\n            print str(node.lineno) + \" | function \" + node[0].value + \" | \" + self.source[node.start:node[1].end]\n    except Exception as e:\n        pass\n\n\ndef visit(self, root):\n\n    call = lambda n: getattr(self, \"visit_%s\" % n.type, self.visit_NOOP)(n)\n    call(root)\n    self.look4Childen(root)\n    for node in root:\n        self.visit(node)\n\nfilepath = r\"C:\\Users\\dward\\Dropbox\\juggernaut2\\juggernaut\\parser\\test\\data\\jasmine.js\"\nouterspace = Visitor(filepath)\n```\n\n```text\n$.ajax({\n    type: \"POST\",\n    url: 'http://www.example.com',\n    data: {\n        email: 'abc@g.com',\n        phone: '9999999999',\n        name: 'XYZ'\n    }\n});\n```\n\n```text\nfrom slimit import ast\nfrom slimit.parser import Parser\nfrom slimit.visitors import nodevisitor\n\n\ndata = \"\"\"\n$.ajax({\n    type: \"POST\",\n    url: 'http://www.example.com',\n    data: {\n        email: 'abc@g.com',\n        phone: '9999999999',\n        name: 'XYZ'\n    }\n});\n\"\"\"\n\nparser = Parser()\ntree = parser.parse(data)\nfields = {getattr(node.left, 'value', ''): getattr(node.right, 'value', '')\n          for node in nodevisitor.visit(tree)\n          if isinstance(node, ast.Assign)}\n\nprint fields\n```\n\n```text\n{'name': \"'XYZ'\", \n 'url': \"'http://www.example.com'\", \n 'type': '\"POST\"', \n 'phone': \"'9999999999'\", \n 'data': '', \n 'email': \"'abc@g.com'\"}\n```\n\n```text\nslimit\n```\n\n```text\nemail\n```\n\n```text\nphone\n```\n\n```text\nname\n```\n\n```text\ndata\n```\n\n```text\nslimit\n```\n\n```py\n>>> from pyjsparser import parse\n>>> parse('var $ = \"Hello!\"')\n{\n\"type\": \"Program\",\n\"body\": [\n    {\n        \"type\": \"VariableDeclaration\",\n        \"declarations\": [\n            {\n                \"type\": \"VariableDeclarator\",\n                \"id\": {\n                    \"type\": \"Identifier\",\n                    \"name\": \"$\"\n                },\n                \"init\": {\n                    \"type\": \"Literal\",\n                    \"value\": \"Hello!\",\n                    \"raw\": '\"Hello!\"'\n                }\n            }\n        ],\n        \"kind\": \"var\"\n    }\n  ]\n}\n```\n\n```text\nimport js2py\nesprima = js2py.require(\"esprima@4.0.1\")\nesprima.parse(\"a = () => {return 11};\")\n# {'body': [{'expression': {'left': {'name': 'a', 'type': 'Identifier'}, 'operator': '=', 'right': {'async': False, 'body': {'body': [{'argument': {'raw': '11', 'type': 'Literal', 'value': 11}, 'type': 'ReturnStatement'}], 'type': 'BlockStatement'}, 'expression': False, 'generator': False, 'id': None, 'params': [], 'type': 'ArrowFunctionExpression'}, 'type': 'AssignmentExpression'}, 'type': 'ExpressionStatement'}], 'sourceType': 'script', 'type': 'Program'}\n```\n\n```text\nangular.js\n```\n\n```text\nconst\n```\n\n```text\nlet\n```\n\n========================================\n\nComments:\n- I would suggest js2xml from scrapinghub: github.com/scrapinghub/js2xml\n- Use Tree-sitter's JavaScript grammar through their Python bindings\n- ANTLR 4 seems to be back alive.\n- Unfortunately spidermonkey is dead upstream.\n- This can't emit JavaScript from a parsed AST, can it? I wanted to modify the AST and emit new JavaScript, but it doesn't seem like this can do that.\n- @gsingh2011 No, it kinda struggled with parsing so doing the other way is way past its capabilities.\n- This is just great! Also you can access each value as a Dictionary, like: `print fields['url']`\n- This library from 2013 doesn't seem to be able to parse Javascript classes.\n- Slimit is nice but only supports ES5 :(\n- This appears to need yacc to be installed on the first run.\n- slimit last updated in 2018, looks quite dead\n- Tested and work pretty well. You can use it and reconstruct for example some JSON data from it, captured by crawlers.\n- good documentation & you can test some code online as well, esprima.org/demo/parse.html","metadata":{"transformedAt":"2026-08-18T18:32:17.745Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":15,"totalLines":283,"estimatedTokens":1764}}38{"id":"stack-672461","source":"stackoverflow","questionId":672461,"title":"what is \"stack alignment\"?","tags":["c++","data-structures","mingw","visual-c++","compiler-construction"],"text":"Title: what is \"stack alignment\"?\nTags: c++, data-structures, mingw, visual-c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nWhat is stack alignment? \nWhy is it used? \nCan it be controlled by compiler settings?\n\nThe details of this question are taken from a problem faced when trying to use ffmpeg libraries with msvc, however what I'm really interested in is an explanation of what is \"stack alignment\".\n\nThe Details:\n\nWhen runnig my msvc complied program which links to avcodec I get the\nfollowing error: \"Compiler did not align stack variables. Libavcodec has\nbeen miscompiled\", followed by a crash in avcodec.dll. \n\n- avcodec.dll was not compiled with msvc, so I'm unable to see what is going on inside.\n\n- When running ffmpeg.exe and using the same avcodec.dll everything works well.\n\n- ffmpeg.exe was not compiled with msvc, it was complied with gcc / mingw (same as avcodec.dll)\n\nThanks,\n\nDan\n\n========================================\n\nTop Answer:\nIIRC, stack alignment is when variables are placed on the stack \"aligned\" to a particular number of bytes. So if you are using a 16 bit stack alignment, each variable on the stack is going to start from a byte that is a multiple of 2 bytes from the current stack pointer within a function.\n\nThis means that if you use a variable that is When calling functions, one method of passing arguments to the next function is to place them on the stack (as opposed to placing them directly into registers). Whether or not alignment is being used here is important, as the calling function places the variables on the stack, to be read off by the calling function using offsets. If the calling function aligns the variables, and the called function expects them to be non-aligned, then the called function won't be able to find them.\n\nIt seems that the msvc compiled code is disagreeing about variable alignment. Try compiling with all optimisations turned off.\n\n========================================\n\nCode:\n```text\n+----+\n|0000| \n|0001|\n+----+\n|0002|\n|0003|\n+----+\n|0004|\n|0005|\n+----+\n| .. |\n```\n\n```text\n+----+\n|0000| B\n|0001| W\n+----+\n|0002| W\n|0003|\n+----+\n```\n\n```text\n+----+\n|0000| B\n|0001| -\n+----+\n|0002| W\n|0003| W\n+----+\n```\n\n```text\n// Some compilers won't align this as it's on the stack...\nint __declspec(align(32)) needsToBe32Aligned = 0;\n// Change to\nstatic int __declspec(align(32)) needsToBe32Aligned;\nneedsToBe32Aligned = 0;\n```\n\n========================================\n\nComments:\n- Since others have explained what stack alignment is and why it is used, I just want to add my two cents regarding *\"Can it be controlled by compiler settings?\"*. See this question\n- This explains very nice why an array of words should be aligned. Because accessing a specific element would otherwise require two reads. But in the example with the struck containing a byte and a word: If you read the complete struct then in both cases you have to read both words anyway.\n- @ToonKrijthe **\" But words can only be fetched at even addresses. \"** Why this needs to be true, can't the memory/stack pointer point to 0001 in your example and then read a word of information starting there?\n- @User10482, I will take 2 read actions in that case. And as such is slower.\n- @ToonKrijthe Why is that? The machine can read memory in blocks of 2 bytes, with one read action, in your example (like 0000, 0001 or 0002, 0003). So if my address register is pointing to 0001 (odd address instead of even), then I can read 2 bytes from there (i.e. 0001 and 0002) directly in one read action, right?\n- So you are saying that it's a restriction put by the CPU manufacturer that the address must be even? @ToonKrijthe\n- @User10482 Go read the pdf 'What Every Programmer Should Know About Memory', page 7, Figure 2.7. Memory is not as you picture it in your head, its not stored as a line, its a Matrix, so you can read a whole row in one go , but if you are not aligned , you need 2 read access.\n- @ToonKrijthe could you please tell me how does the instruction `and rsp, 0xfffffffffffffff0` alling stack 16 bytes in AMD64 architecture.\n- @User10482 might be useful\n- sizeof(char) is always 1 byte, which is always at least 8 bits... not bytes. Alignment depends on compiler platform, and (x86, anyway) is generally 4byte for 32bit architectures, 8byte for 64bit archs.\n- Thanks, was a brainfart indeed on the size of a byte :P. I had picked 16 bytes as an arbitrary example, but using the smaller example makes it much clearer.\n- No, stack alignment is about maintaining the alignment of the stack pointer itself. Single-byte local variables on the stack can be at any address. If there's only one, then there will be padding before the next variable, because most ABIs align the basic types like `int` to their own width (natural alignment). Only for arg-passing on the stack are single-byte objects padded to the \"stack width\" or slot (the size of a single `push` instruction).\n- Compilers *do* align variables on the stack to the alignment guarantees / requirements for that type specified in the ABI. Normally that means natural alignment: alignment = width, so a 4-byte `int` gets 4-byte alignment. Maintaining 16-byte alignment for the stack pointer itself makes it possible to align variables on the stack by 16, 8, 4, or 2 with no extra cost.","metadata":{"transformedAt":"2026-08-18T18:32:17.745Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":97,"estimatedTokens":1323}}39{"id":"stack-3406942","source":"stackoverflow","questionId":3406942,"title":"How exactly does java compilation take place?","tags":["java","compiler-construction","jvm"],"text":"Title: How exactly does java compilation take place?\nTags: java, compiler-construction, jvm\nSource: Stack Overflow\n\nQuestion:\nConfused by java compilation process\n\nOK i know this: We write java source code, the compiler which is platform independent translates it into bytecode, then the jvm which is platform dependent translates it into machine code.\n\nSo from start, we write java source code. The compiler javac.exe is a .exe file. What exactly is this .exe file? Isn't the java compiler written in java, then how come there is .exe file which executes it? If the compiler code is written is java, then how come compiler code is executed at the compilation stage, since its the job of the jvm to execute java code. How can a language itself compile its own language code? It all seems like chicken and egg problem to me.\n\nNow what exactly does the .class file contain? Is it a abstract syntax tree in text form, is it tabular information, what is it?\n\ncan anybody tell me clear and detailed way about how my java source code gets converted in machine code.\n\n========================================\n\nTop Answer:\nIsn't the java compiler written in java, then how come there is .exe file which executes it? \n\nWhere do you get this information from? The `javac` executable could be written in any programming language, it is irrelevant, all that is important is that it is an executable which turns `.java` files into `.class` files.\n\nFor details on the binary specification of a .class file you might find these chapters in the Java Language Specification useful (although possibly a bit technical):\n\n- Virtual Machine Startup\n\n- Loading of Classes and Interfaces\n\nYou can also take a look at the Virtual Machine Specification which covers:\n\n- The `class` file format\n\n- The Java Virtual Machine instruction set\n\n- Compiling for the Java Virtual Machine\n\n========================================\n\nCode:\n```text\n0:   new #2; //class SomeObject\n3:   dup\n4:   invokespecial   #3; //Method SomeObject.\"<init>\":()V\n7:   astore_1\n8:   aload_1\n9:   invokevirtual   #4; //Method SomeObject.doSomething:()V\n12:  return\n```\n\n```text\n%JDK_HOME%/bin/java.exe -cp:myclasspath com.sun.tools.javac.Main fileToCompile\n```\n\n```text\nexe\n```\n\n```text\n.class\n```\n\n```text\n.class\n```\n\n```text\njavap -c Main\n```\n\n```text\njavac\n```\n\n```text\njavac\n```\n\n```text\n.class\n```\n\n```text\njava\n```\n\n```text\njavac\n```\n\n```text\njavac\n```\n\n```text\njavac\n```\n\n```text\n.java\n```\n\n```text\n.class\n```\n\n```text\nclass\n```\n\n```text\njavac.exe\n```\n\n========================================\n\nComments:\n- A language can easily compile its own language code. C/C++ compilers are often written in C or C++, the cobra language compiler is written in cobra, and there are many examples of en.wikipedia.org/wiki/Self-hosting compilers.\n- The compiler doesn't have to be platform independent, it just has to conform to specifications which only specify input and output. You could write a compiler in perl for all the resulting bytecode would care.\n- related stackoverflow question: stackoverflow.com/questions/1220914/&hellip;\n- Not entirely relevant but it's good to mention that Sun's JVM is written in C, and Oracle's JVM (Hotspot) is written in C++.\n- Will check out the links. Plus the scenario i added as a comment to rekin's answer. Am i right with it?\n- Technically, the first compiler could be created by an *interpreter* written in some other language, though the lines between interpretation and JIT compilation are a little blurry as ultimately they both produce native machine code.\n- True. In fact, there are even crazier options. The first C++ compiler was simply a translator that produced C code for use withe a C compiler.\n- Huh? Why did something vote this down? Is there a problem with my answer?\n- @Mike: Well, javac is not written in C, it's written in Java, so your first sentence is not entirely correct. Otherwise a fine answer :-).\n- @sleske Is javac written in Java? I actually didn't know that. It makes sense, I guess. But, I bet javac 1.0 wasn't written in Java ;)\n- It's widely known that the (Sun/Oracle) Java compiler is written in Java. Since Java 6, there's even a sufficial API to it, but long before that, people called it via the unofficial API in the sun.tools packages.\n- \"The Java compiler (at least the one that comes with the Sun/Oracle JDK) is indeed written in Java\" I did not know this! Thank you, sir.\n- So here's the scenario from what you described, we write source code. javac.exe the executable, primarily exists to collect parameters and to start the JVM. The JVM then executes compiler(collection of large number of already compiled .class files) code. The compiler then executes our written program. Also when the compiler was being designed, some other language(C in our case) is used to create the .class files for the compiler. am i right with what i said? .exe file is a wrapped java bytecode? It isn't exactly wrapping code. what kind of complicated batch scripts are you talking about?\n- Correct, `javac` is only a convenient wrapper. The real compiler code is in `com.sun.tools.javac` package of Sun's JDK. You can go there and take a look at the source code - it can be interesting to see the internals. You can also invoke `javac` compiler straight from Java, without calling any external processes. If You take that into account, it makes sense that `javac` is only a launcher of certain classfile buried inside JDK. I can only suspect, but I believe it does as little as starting JVM and passing the arguments.\n- And about the batch script, take a look at the last paragraph of the answer. I edited it to answer Your comment.\n- Ok thanks a lot, appreciate your efforts for the clear reply.\n- A tokenizer tokenizes. It doesn't produce ASTs or syntax errors. You don't know whether `javac` uses the Visitor pattern or not.\n- @EJP afair, those are the notes from reading the openjdk's javac source code\n- Not in CMD.EXE as far as I know. You cannot just say \"foobar.jar\" on the commadn line and have it executed. This might be a Windows 95/98/ME limitation in COMMAND.EXE that resulted in that decision. These days it would be nice though.","metadata":{"transformedAt":"2026-08-18T18:32:17.745Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":135,"estimatedTokens":1538}}40{"id":"stack-26323215","source":"stackoverflow","questionId":26323215,"title":"Do any languages / compilers utilize the x86 ENTER instruction with a nonzero nesting level?","tags":["assembly","x86","compiler-construction","nested-function","stack-frame"],"text":"Title: Do any languages / compilers utilize the x86 ENTER instruction with a nonzero nesting level?\nTags: assembly, x86, compiler-construction, nested-function, stack-frame\nSource: Stack Overflow\n\nQuestion:\nThose familiar with x86 assembly programming are very used to the typical function prologue / epilogue:\n\n```\npush ebp ; Save old frame pointer.\nmov ebp, esp ; Point frame pointer to top-of-stack.\nsub esp, [size of local variables]\n...\nmov esp, ebp ; Restore frame pointer and remove stack space for locals.\npop ebp\nret\n```\n\nThis same sequence of code can also be implemented with the `ENTER` and `LEAVE` instructions:\n\n```\nenter [size of local variables], 0\n...\nleave\nret\n```\n\nThe `ENTER` instruction's second operand is the *nesting level*, which allows multiple parent frames to be accessed from the called function.\n\nThis is not used in C because there are no nested functions; local variables have only the scope of the function they're declared in. This construct does not exist (although sometimes I wish it did):\n\n```\nvoid func_a(void)\n{\n int a1 = 7;\n\n void func_b(void)\n {\n printf(\"a1 = %d\\n\", a1); /* a1 inherited from func_a() */\n }\n\n func_b();\n}\n```\n\nPython however *does* have nested functions which behave this way:\n\n```\ndef func_a():\n a1 = 7\n def func_b():\n print 'a1 = %d' % a1 # a1 inherited from func_a()\n func_b()\n```\n\nOf course Python code isn't translated directly to x86 machine code, and thus would be unable (unlikely?) to take advantage of this instruction.\n\n**Are there any languages which compile to x86 and provide nested functions? Are there compilers which will emit an `ENTER` instruction with a nonzero second operand?**\n\nIntel invested a nonzero amount of time/money into that nesting level operand, and basically I'm just curious if anyone uses it :-)\n\nReferences:\n\n- Intel® 64 and IA-32 Architectures Software Developer’s Manual Vol 2: Instruction Set Reference\n\n- NASM Manual - ENTER: Create Stack Frame\n\n========================================\n\nTop Answer:\nAs Iwillnotexist Idonotexist pointed out, GCC *does* support nested functions in C, using the exact syntax I've shown above.\n\nHowever, it does not use `ENTER` instruction. Instead, variables which are used in nested functions are grouped together in the local variables area, and a pointer to this group is passed to the nested function. Interestingly, this \"pointer to parent variables\" is passed via a nonstandard mechanism: On x64 it is passed in `r10`, and on x86 (cdecl) it is passed in `ecx`, which is reserved for the `this` pointer in C++ (which doesn't support nested functions anyway).\n\n```\n#include \nvoid func_a(void)\n{\n int a1 = 0x1001;\n int a2=2, a3=3, a4=4;\n int a5 = 0x1005;\n\n void func_b(int p1, int p2)\n {\n /* Use variables from func_a() */\n printf(\"a1=%d a5=%d\\n\", a1, a5);\n }\n func_b(1, 2);\n}\n\nint main(void)\n{\n func_a();\n return 0;\n}\n```\n\nProduces the following (snippet of) code when compiled for 64-bit: \n\n```\n00000000004004dc :\n 4004dc: push rbp\n 4004dd: mov rbp,rsp\n 4004e0: sub rsp,0x10\n 4004e4: mov DWORD PTR [rbp-0x4],edi\n 4004e7: mov DWORD PTR [rbp-0x8],esi\n 4004ea: mov rax,r10 ; ptr to calling function \"shared\" vars\n 4004ed: mov ecx,DWORD PTR [rax+0x4]\n 4004f0: mov eax,DWORD PTR [rax]\n 4004f2: mov edx,eax\n 4004f4: mov esi,ecx\n 4004f6: mov edi,0x400610\n 4004fb: mov eax,0x0\n 400500: call 4003b0 \n 400505: leave \n 400506: ret \n\n0000000000400507 :\n 400507: push rbp\n 400508: mov rbp,rsp\n 40050b: sub rsp,0x20\n 40050f: mov DWORD PTR [rbp-0x1c],0x1001\n 400516: mov DWORD PTR [rbp-0x4],0x2\n 40051d: mov DWORD PTR [rbp-0x8],0x3\n 400524: mov DWORD PTR [rbp-0xc],0x4\n 40052b: mov DWORD PTR [rbp-0x20],0x1005\n 400532: lea rax,[rbp-0x20] ; Pass a, b to the nested function\n 400536: mov r10,rax ; in r10 !\n 400539: mov esi,0x2\n 40053e: mov edi,0x1\n 400543: call 4004dc \n 400548: leave \n 400549: ret\n```\n\nOutput from `objdump --no-show-raw-insn -d -Mintel`\n\nThis would be equivalent to something more verbose like this:\n\n```\nstruct func_a_ctx\n{\n int a1, a5;\n};\n\nvoid func_b(struct func_a_ctx *ctx, int p1, int p2)\n{\n /* Use variables from func_a() */\n printf(\"a1=%d a5=%d\\n\", ctx->a1, ctx->a5);\n}\n\nvoid func_a(void)\n{\n int a2=2, a3=3, a4=4;\n struct func_a_ctx ctx = {\n .a1 = 0x1001,\n .a5 = 0x1005,\n };\n\n func_b(&ctx, 1, 2);\n}\n```\n\n========================================\n\nCode:\n```text\npush ebp ; Save old frame pointer.\nmov  ebp, esp ; Point frame pointer to top-of-stack.\nsub  esp, [size of local variables]\n...\nmov  esp, ebp ; Restore frame pointer and remove stack space for locals.\npop  ebp\nret\n```\n\n```text\nenter [size of local variables], 0\n...\nleave\nret\n```\n\n```c\nvoid func_a(void)\n{\n    int a1 = 7;\n\n    void func_b(void)\n    {\n        printf(\"a1 = %d\\n\", a1);  /* a1 inherited from func_a() */\n    }\n\n    func_b();\n}\n```\n\n```python\ndef func_a():\n    a1 = 7\n    def func_b():\n        print 'a1 = %d' % a1      # a1 inherited from func_a()\n    func_b()\n```\n\n```text\nENTER\n```\n\n```text\nLEAVE\n```\n\n```text\nENTER\n```\n\n```text\nENTER\n```\n\n```text\nenter\n```\n\n```text\nenter\n```\n\n```text\nleave\n```\n\n```text\nenter\n```\n\n```text\nleave\n```\n\n```text\npascal\n```\n\n```text\nret K\n```\n\n```text\nbound\n```\n\n```c\n#include <stdio.h>\nvoid func_a(void)\n{\n    int a1 = 0x1001;\n    int a2=2, a3=3, a4=4;\n    int a5 = 0x1005;\n\n    void func_b(int p1, int p2)\n    {\n        /* Use variables from func_a() */\n        printf(\"a1=%d a5=%d\\n\", a1, a5);\n    }\n    func_b(1, 2);\n}\n\nint main(void)\n{\n    func_a();\n    return 0;\n}\n```\n\n```text\n00000000004004dc <func_b.2172>:\n  4004dc:   push   rbp\n  4004dd:   mov    rbp,rsp\n  4004e0:   sub    rsp,0x10\n  4004e4:   mov    DWORD PTR [rbp-0x4],edi\n  4004e7:   mov    DWORD PTR [rbp-0x8],esi\n  4004ea:   mov    rax,r10                    ; ptr to calling function \"shared\" vars\n  4004ed:   mov    ecx,DWORD PTR [rax+0x4]\n  4004f0:   mov    eax,DWORD PTR [rax]\n  4004f2:   mov    edx,eax\n  4004f4:   mov    esi,ecx\n  4004f6:   mov    edi,0x400610\n  4004fb:   mov    eax,0x0\n  400500:   call   4003b0 <printf@plt>\n  400505:   leave  \n  400506:   ret    \n\n0000000000400507 <func_a>:\n  400507:   push   rbp\n  400508:   mov    rbp,rsp\n  40050b:   sub    rsp,0x20\n  40050f:   mov    DWORD PTR [rbp-0x1c],0x1001\n  400516:   mov    DWORD PTR [rbp-0x4],0x2\n  40051d:   mov    DWORD PTR [rbp-0x8],0x3\n  400524:   mov    DWORD PTR [rbp-0xc],0x4\n  40052b:   mov    DWORD PTR [rbp-0x20],0x1005\n  400532:   lea    rax,[rbp-0x20]              ; Pass a, b to the nested function\n  400536:   mov    r10,rax                     ; in r10 !\n  400539:   mov    esi,0x2\n  40053e:   mov    edi,0x1\n  400543:   call   4004dc <func_b.2172>\n  400548:   leave  \n  400549:   ret\n```\n\n```c\nstruct func_a_ctx\n{\n    int a1, a5;\n};\n\nvoid func_b(struct func_a_ctx *ctx, int p1, int p2)\n{\n    /* Use variables from func_a() */\n    printf(\"a1=%d a5=%d\\n\", ctx->a1, ctx->a5);\n}\n\nvoid func_a(void)\n{\n    int a2=2, a3=3, a4=4;\n    struct func_a_ctx ctx = {\n        .a1 = 0x1001,\n        .a5 = 0x1005,\n    };\n\n    func_b(&ctx, 1, 2);\n}\n```\n\n```text\nENTER\n```\n\n```text\nr10\n```\n\n```text\necx\n```\n\n```text\nthis\n```\n\n```text\nobjdump --no-show-raw-insn -d -Mintel\n```\n\n```text\n! global variable\n%integer sum\n\n! nested routine\n%integer %function mcode001( %integer number, x )\n 0004 EB 00                                 JMP L1001\n 0006                      L1002  EQU $\n 0006 C8 00 00 02                           ENTER 0000,2\n    ! local variable\n    %integer r\n\n    r = number + x\n 000A 8B 45 0C                              MOV EAX,[EBP+12]\n 000D 03 45 08                              ADD EAX,[EBP+8]\n 0010 89 45 F4                              MOV [EBP-12],EAX\n\n    %result = r\n 0013 8B 45 F4                              MOV EAX,[EBP-12]\n 0016 C9                                    LEAVE\n 0017 C3                                    RET\n%end\n 0018                      L1001  EQU $\n\n! call the nested routine\nsum = mcode001(46,24)&255\n 0018 6A 2E                                 PUSH 46\n 001A 6A 18                                 PUSH 24\n 001C E8 00 00                              CALL 'MCODE001' (INTERNAL L1002 )\n 001F 83 C4 08                              ADD ESP,8\n 0022 25 FF 00 00 00                        AND EAX,255\n 0027 89 45 F8                              MOV [EBP-8],EAX\n\n! show the result itos converts binary integer to text\nprintstring(\"Result =\".itos(sum,3)); newline\n 002A FF 75 F8                              PUSH WORD PTR [EBP-8]\n 002D 6A 03                                 PUSH 3\n 002F 8D 85 F8 FE FF FF                     LEA EAX,[EBP-264]\n 0035 50                                    PUSH EAX\n 0036 E8 42 00                              CALL 'ITOS' (EXTERN 66)\n 0039 83 C4 0C                              ADD ESP,12\n 003C 8D 85 F8 FD FF FF                     LEA EAX,[EBP-520]\n 0042 50                                    PUSH EAX\n 0043 B8 00 00 00 00                        MOV EAX,COT+0\n 0048 50                                    PUSH EAX\n 0049 68 FF 00 00 00                        PUSH 255\n 004E E8 03 00                              CALL '_IMPSTRCPY' (EXTERN 3)\n 0051 83 C4 0C                              ADD ESP,12\n 0054 8D 85 F8 FD FF FF                     LEA EAX,[EBP-520]\n 005A 50                                    PUSH EAX\n 005B 8D 85 F8 FE FF FF                     LEA EAX,[EBP-264]\n 0061 50                                    PUSH EAX\n 0062 68 FF 00 00 00                        PUSH 255\n 0067 E8 05 00                              CALL '_IMPSTRCAT' (EXTERN 5)\n 006A 83 C4 0C                              ADD ESP,12\n 006D 81 EC 00 01 00 00                     SUB ESP,256\n 0073 89 E0                                 MOV EAX,ESP\n 0075 50                                    PUSH EAX\n 0076 8D 85 F8 FD FF FF                     LEA EAX,[EBP-520]\n 007C 50                                    PUSH EAX\n 007D 68 FF 00 00 00                        PUSH 255\n 0082 E8 03 00                              CALL '_IMPSTRCPY' (EXTERN 3)\n 0085 83 C4 0C                              ADD ESP,12\n 0088 E8 34 00                              CALL 'PRINTSTRING' (EXTERN 52)\n 008B 81 C4 00 01 00 00                     ADD ESP,256\n 0091 E8 3C 00                              CALL 'NEWLINE' (EXTERN 60)\n\n%endofprogram\n 0094 C9                                    LEAVE\n 0095 C3                                    RET\n      _TEXT  ENDS\n      CONST  SEGMENT WORD PUBLIC 'CONST'\n 0000                                       db 08,52 ; .R\n 0002                                       db 65,73 ; es\n 0004                                       db 75,6C ; ul\n 0006                                       db 74,20 ; t.\n 0008                                       db 3D,00 ; =.\n      CONST  ENDS\n      _TEXT  SEGMENT WORD PUBLIC 'CODE'\n             ENDS\n      DATA  SEGMENT WORD PUBLIC 'DATA'\n      DATA    ENDS\n              ENDS\n      _SWTAB  SEGMENT WORD PUBLIC '_SWTAB'\n      _SWTAB   ENDS\n```\n\n```text\nExample  PROCEDURE(LONG a, LONG b)\nc        long\n  code\n  a = a + b\n  return c\n```\n\n```text\n----example(LONG,LONG)\n  push   08CH\n  enter  0CH,0\n  push   ebx\n  mov    [ebp][\"A\"], eax\n  mov    [ebp][\"B\"], ebx\n  etc...\n```\n\n========================================\n\nComments:\n- +1, the most interesting question of today. For 1), GCC supports nested functions in C using exactly your syntax. But explicitly not in C++.\n- @IwillnotexistIdonotexist I coincidentally just ran across that same page. Interestingly it compiles on gcc 4.7.2 with the default options. Looking forward to looking at the disassembly. Fun stuff!\n- Even if it made sense to use it, that instruction is not particularly efficient.\n- @harold Understood, but that is as it is normally used. I'd imagine that implementing `enter 200h, 31` via `mov`/`push` would be less efficient.\n- Maybe so. I have the time for `ENTER a,b` listed here as `79 + 5b` on Nehalem (the number of &#181;ops scales even worse in `b`) (similar numbers apply to most architectures), it's sort of hard to do worse than that.\n- @harold Wow, that is bad. I stand corrected!\n- For what it is worth, I understand from `grep`-ing `gcc-4.8.2&#47;gcc&#47;config&#47;i386&#47;i386.c:10339` that GCC simply never emits `ENTER` at all nowadays. And the comment at that line is quite clear: `&#47;* Note: AT&T enter does NOT have reversed args. Enter is probably slower on all targets. Also sdb doesn't like it. *&#47;`\n- @IwillnotexistIdonotexist Very good to know. I think the instruction has been largely damned to obsolescence. Amazingly though, it is still valid in 64-bit mode, unlike many other obsolete instructions which AMD took the opportunity to can.\n- @IwillnotexistIdonotexist FWIW, that was part of the very first version of GCC. `git log -p` on their cvs->svn->git converted repository shows that it already existed in the initial check-in in 1992.\n- And my private svn checkout of LLVM 3.5 has at `llvm&#47;lib&#47;Target&#47;X86&#47;X86FrameLowering.cpp:355` a comment for the `emitPrologue()` method which reads in part `; Spill general-purpose registers [for all callee-saved GPRs] pushq % [if not needs FP] .cfi_def_cfa_offset (offset from RETADDR) .seh_pushreg %`. There are no mentions of `ENTER`, only pushes; And the enum constant for x86 `ENTER` occurs only 3 times in all of LLVM; It doesn't even look as though they have testcases for it.\n- So neither GCC nor LLVM produce `ENTER` ever, not even for `-Os`. If somebody can dig in the sources of ICC and MSVC (Har, har, fat chance of that happening) and confirm it never generates `ENTER`, you'll know that the answer to 2) is approximately **no**.\n- FWIW, Pascal has nested functions, but none of the x86 Pascal compilers I know uses `ENTER`.\n- Interesting to see what `gcc -O0` does. It's probably rare for gcc not to inline a nested function with optimization enabled. Although maybe if there are many call-sites in the outer function... (especially if you optimize for size with `-Os`.)\n- @Peter The other case would be where the inner function is passed as a callback to some external function. It is then that the closure-stub on the stack is really necessary, as a single function pointer cannot otherwise encapsulate both the function address and its data.\n- Oh right, I think I've seen gcc emit mov-immediate stores of x86 machine code for the stub you're talking about. And it emits assembler directives to mark the stack executable so this can work, so linking an object file that uses that will make your whole program's stack executable! lists.llvm.org/pipermail/cfe-dev/2015-September/045063.html (no clang support yet)\n- Here's an example of gcc writing machine-code bytes to the stack before passing a function-pointer to a nested function (to a function it can't see): godbolt.org/g/NaSZWp.\n- Yep, that's exactly what I was referring to. Pretty cool stuff, really!\n- What happens when you have multiple nested function calls within a single function, that use different sets of local variables such that you cannot group the local variables used in a single contiguous block?\n- @TLW Good question. But there's no reason they *need* to be contiguous; as long as the nested functions know the layout, they can access which ever variables they want -- just like a normal pointer-to-struct.\n- +1 *\"Note that the x86 was originally designed as a Pascal machine\"* - I often wondered which high-level languages the designers had in mind when they added high-level language support instructions. Any additional historical perspective you could link to?\n- Have a look at stevemorse.org/8086 - Morse is the designer of the chip, and the chapters about Pascal and PL/M might be illuminating.\n- @JonathonReinhart once upon a time structured programming was the silver bullet and Pascal influenced languages like Modula and especially Ada which was \"the language\" of the United States Department of Defense. So hardware support of those languages does not surprise me\n- @gsg Thank you for that link! Lots of interesting insight into the design decisions that were made in the early days.\n- Gcc has '-Os' option. When optimizing for size, one typically doesn't care, if the corresponding instruction is inefficient.\n- @AkiSuihkonen: `-Os` is not really \"optimize for size\" but rather \"optimize for performance without performing any optimizations which are likely to adversely affect size\".\n- Question about \"Pascal Machine\" and x86 retrocomputing.stackexchange.com/q/6959/8579\n- GCC12 now has a `-Oz`, like clang's `-Oz`, which does aggressively optimize for machine-code size, even at a large cost in speed. (e.g. `push 1`/`pop rax` instead of `mov eax,1`). But neither GCC nor clang's `-Oz` currently has a peephole optimization to use `enter` at all, let alone with a non-zero nesting level. godbolt.org/z/efa4feTEh shows gcc/clang `-Oz -fno-omit-frame-pointer -mno-red-zone`, with clang ironically using `sub rsp,4` / `add rsp,4` instead of a dummy push/pop like it normally does. (Maybe it's also minimizing stack-space usage, even in leaf functions?)\n- This is the exact perspective I was hoping for; thank you. I'm still curious as to why AMD decided to keep ENTER in AMD64, even though it seems *no one* uses it.\n- @JonathonReinhart: Making the decoders reject it in 64-bit mode but accept it in other modes might have *increased* complexity. AMD were very conservative about cleaning up the instruction set, because they weren't sure AMD64 would catch on, and didn't want to be stuck with more transistors that nobody used. We can basically blame capitalism for this huge missed opportunity to tidy up x86 machine code and change things that make a high-performance implementation tricky. (e.g. setcc could have changed to `setcc r&#47;m32`, saving instructions to booleanize into an `int` instead of `char`)\n- `-fomit-frame-pointer` is the default now. gcc still uses `leave` when it makes frame pointers. (It does so even in optimized code for functions with a VLA: godbolt.org/g/LF3Rrk). I tested with a few different `-mtune=` options, and they all used `leave`. clang doesn't use `leave`, though, ever. That's a missed optimization for `-Os` (optimize for size), because it's only 3 uops vs. at least 2 for mov/pop (and maybe a stack-sync uop).\n- gcc and clang don't use `enter` even if you compile with `-Os` or `-Oz`. `enter n,0` is 12 uops on Skylake, with 1 per 8 clocks throughput. On Ryzen, it's 12 uops with 1 per 16 clocks throughput. At `-Oz`: optimize for size at all costs, it might make sense for clang to use `enter`, because it does stuff like `push 2` / `pop rax` to save 2 bytes vs. `mov eax,2`. (gcc doesn't have a `-Oz` mode.) See agner.org/optimize for instruction tables and a microarch guide to make sense of them. See also the SO x86 tag wiki\n- Thanks to @PeterCordes for the information. Fits what I see.\n- This does not answer the question. The question was not what Linux uses or what GCC emits, but whether languages exist that *do* use the instruction with a non-zero nesting level.\n- Interesting! Can you provide a reference? And it would be cool if you could provide a code snippet with the generated assembly.\n- Search for IMP77 on github (under siliconsam). Then search for ENTER in the source code for pass2.imp and pass3coff.c Source file pass2.imp indicates the code generation and pass3coff.c adds the actual ENTER instruction into the COFF object file. Example IMP code and generated Intel code to\n- Hey John, thanks for the update. Rather than adding a new answer, please edit your original answer and add this content there. Then delete these other two answers. Thanks!\n- `enter 0CH,0` has the 2nd operand (nesting level) = 0, so it's just a very slow way to do `push ebp` / `mov ebp, esp` / `sub esp, 0Ch`. This question is asking about compilers that use a non-zero nesting level, not ones that just use it at all to make slow code while saving a few bytes of code-size.","metadata":{"transformedAt":"2026-08-18T18:32:17.746Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":27,"totalLines":500,"estimatedTokens":4938}}41{"id":"stack-2198950","source":"stackoverflow","questionId":2198950,"title":"Why is (void) 0 a no operation in C and C++?","tags":["c++","c","compiler-construction"],"text":"Title: Why is (void) 0 a no operation in C and C++?\nTags: c++, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have seen debug printfs in *glibc* which internally is defined as `(void) 0`, if *NDEBUG* is defined. Likewise the `__noop` for Visual C++ compiler is there too. The former works on both GCC and VC++ compilers, while the latter only on VC++. Now we all know that both the above statements will be treated as no operation and no respective code will be generated; but here's where I've a doubt.\n\nIn case of `__noop`, MSDN says that it's a intrinsic function provided by the compiler. Coming to `(void) 0` ~ Why is it interpreted by the compilers as no op? Is it a tricky usage of the C language or does the standard say something about it explicity? Or even that is something to do with the compiler implementation?\n\n========================================\n\nTop Answer:\nAny expression that doesn't have any side-effects can be treated as a no-op by the compiler, which dosn't have to generate any code for it (though it may). It so happens that casting and then not using the result of the cast is easy for the compiler (and humans) to see as not having side-effects.\n\n========================================\n\nCode:\n```text\n(void) 0\n```\n\n```text\n__noop\n```\n\n```text\n__noop\n```\n\n```text\n(void) 0\n```\n\n```text\n(void)0\n```\n\n```text\n;\n```\n\n```text\nno-op\n```\n\n```text\nif\n```\n\n```text\n#define noop ((void)0)\n```\n\n```text\n(void)\n```\n\n```text\nint x = noop;\n```\n\n```text\nerror: void value not ignored as it ought to be\n```\n\n```text\n'void' illegal with all types\n```\n\n```text\n#ifdef NDEBUG\n#if defined __cplusplus && __GNUC_PREREQ (2,95)\n# define __ASSERT_VOID_CAST static_cast<void>\n#else\n# define __ASSERT_VOID_CAST (void)\n#endif\n# define assert(expr)           (__ASSERT_VOID_CAST (0))\n#else\n/* more code */\n#endif\n```\n\n```text\nNDEBUG\n```\n\n```text\n#define assert(ignore) ((void)0)\n```\n\n```text\nassert\n```\n\n```text\n<assert.h>\n```\n\n```text\nNDEBUG\n```\n\n```text\nassert\n```\n\n```text\nassert(whatever);\n```\n\n```text\n((void)(0));\n```\n\n```text\n<assert.h>\n```\n\n```text\nassert\n```\n\n```text\n<assert.h>\n```\n\n```text\nNDEBUG\n```\n\n```text\n<assert.h>\n```\n\n```text\nassert\n```\n\n```text\n(void)0\n```\n\n```text\nvoid (0)\n```\n\n```text\ndbgprintf(\"Hello World!\"); -> (void) 0(\"Hello World!\");\n```\n\n```text\ndbgprintf(\"Helloworld\")\n```\n\n```text\n(void) 0(\"Hello world\")\n```\n\n```text\n(void) 0;\n```\n\n```text\n(void) 0\n```\n\n```text\ndbgprintf(\"...\")\n```\n\n```text\n#include <iostream>\n#define TRACE ((void)0)\nint main() {\n  TRACE(\"joke\");\n  std::cout << \"ok\" << std::endl;\n  return 0;\n}\n```\n\n```text\n((void)0)(\"joke\")\n```\n\n```text\n-Wall -Wextra\n```\n\n```text\n#define dbgprintf(x)\n```\n\n```text\nif (cond) dbgprintf(x)\n```\n\n```text\n#define dbgprintf(x) 0\n```\n\n```text\n#define dbgprintf(x) (void)0\n```\n\n```text\nvoid\n```\n\n```text\n-Wall -Wextra\n```\n\n========================================\n\nComments:\n- By just giving `0;` as a statement, I don't get warnings or errors, and I am sure it'll not do any effictive operation and is equal to a no op; Even if so, why type cast it to void? Also, in case of the `#define dbgprintf (void) 0`, when it's called like `dbgprintf(\"Hello World!\");` -> `(void) 0(\"Hello World!\");` - what does it mean?\n- this should probably be `#define dbgprintf(x) (void)0;` though I found `#define dbgprintf(x)` perfectly sufficient. I think the cast to void is to remove any return value, so if it is used in context that requires value (and should not), it will cause an error/warning, instead of passing silently.\n- Yea, I didn't notice that the #define ignore param (x) and just does a `(void) 0`. Thanks for pointing it out :)\n- Well, imagine you are a compiler. What code would you generate for this statement:`(void) 0;`? Now you know.\n- @KubaOber: Haha.. true, I was asking more about the specifics on the typecast, in a language laweryese way ;)\n- Why not use `#define dbgprintf` instead of `#define dbgprintf ((void) 0)` ??? @legends2k Is there a reason?\n- @AntiMoron The question is more centered on what does `((void) 0)` mean. Not how to define a no-op macro.\n- Of course I know that. Just curious about. For `(void)0` needs more typing.\n- @SF. `#define dbgprintf(x)` is not sufficient, because macros may be used under bare `if` clause(without `{}`), i.e. `if (something) dbgprintf(...)`. Thus defining as such silently creates logical error in your code.\n- @imkzh True, albeit if it's code for my own consumption, not some sort of library, I tend not to duplicate security practices, of which one is \"No bare if!\" which can bite in way more cases than this. Albeit this one is especially insidious as the bug will strike only with debug off.\n- @SF. That is! I personally the \"no bare if\" rule, but I notice there are some popular(to my surprise, they are also large and modern) libraries using \"bare if\"s, so I decide to point out that `#define xxx` is not safe enough to remove them in production code.\n- Thanks Chris, for clarifying regarding why the type cast to void is made; it prevents from accidental usage of the said noop exp. in expression like assignment, now I did understand.\n- Can we say that, it's like \"pass\" in Python?\n- @UnhandledException Yes, it is!\n- I have an issue with this `(void)(0)` I am actually trying to do `(void)(0) += 1` and returning it the value updated that it `1`. But I get `0` as a result ?\n- Yeah, it's glibc and not glib, thanks for notifying; corrected it in the question.\n- doc.ddart.net/msdn/header/include/assert.h.html - This is one of the many *assert.h*s I've seen with `(void) 0`.\n- @legends2k: `assert(expr);` is not a debug printing macro. My answer above answers your question about `assert()`.\n- I agree, it's assert. But the whole doubt was to know more about the `(void) 0` in general as no operation, based on the language or compiler constructs. Thanks!","metadata":{"transformedAt":"2026-08-18T18:32:17.746Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":45,"totalLines":234,"estimatedTokens":1456}}42{"id":"stack-2722302","source":"stackoverflow","questionId":2722302,"title":"Can compiler optimization introduce bugs?","tags":["optimization","compiler-construction","compiler-optimization"],"text":"Title: Can compiler optimization introduce bugs?\nTags: optimization, compiler-construction, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nToday I had a discussion with a friend of mine and we debated for a couple of hours about \"compiler optimization\".\n\nI defended the point that **sometimes**, a compiler optimization might introduce bugs or at least, undesired behavior.\n\nMy friend totally disagreed, saying that \"compilers are built by smart people and do smart things\" and thus, can **never** go wrong.\n\nHe didn't convince me at all, but I have to admit I lack of real-life examples to strengthen my point.\n\nWho is right here? If I am, do you have any real-life example where a compiler optimization produced a bug in the resulting software? If I'm mistaking, should I stop programming and learn fishing instead?\n\n========================================\n\nTop Answer:\n**When a bug goes away by disabling optimizations, most of the time it's still your fault**\n\nI am responsible for a commercial app, written mostly in C++ - started with VC5, ported to VC6 early, now successfully ported to VC2008. It grew to over 1 Million lines in the last 10 years. \n\nIn that time I could confirm a single code generation bug thast occured when agressive optimizations where enabled.\n\nSo why am I complaining? Because in the same time, there were dozens of bugs that made me doubt the compiler - but it turned out to be my insufficient understanding of the C++ standard. The standard makes room for optimizations the compiler may or may not make use of.\n\nOver the years on different forums, I've seen many posts blaming the compiler, ultimately turning out to be bugs in the original code. No doubt many of them obscure bugs that need a detailed understanding of concepts used in the standard, but source code bugs nonetheless. \n\nWhy I reply so late: stop blaming the compiler before you have confirmed it's actually the compiler's fault.\n\n========================================\n\nCode:\n```text\n// fails because the overflow test gets removed\nif (ptr + len < ptr || ptr + len > max) return EINVAL;\n```\n\n```text\n// The compiler optimizes this to an infinite loop\nfor (i = 1; i > 0; i += i) ++j;\n```\n\n```text\n// the compiler can remove these \"useless writes\"\nmemset(password_buffer, 0, sizeof(password_buffer));\n```\n\n```text\n-foptimize-sibling-calls\n```\n\n```text\n-O2\n```\n\n```text\nFunction foo( const type & tp );\n```\n\n```text\nfoo( foo2() );\n```\n\n```text\nfoo2()\n```\n\n```text\ntype\n```\n\n```text\nO3\n```\n\n```text\ndouble x=0.4;\nif(x<0.5) { below5(); } else { above5(); }\n```\n\n```text\ndouble x=0.4;\nif(x<0.5) { below5(); } else { System.Console.Write(x); above5(); }\n```\n\n```text\nabove5();\n```\n\n```text\nx\n```\n\n```text\nbelow5();\n```\n\n```text\n#pragma optimize( \"\", off)\n```\n\n========================================\n\nComments:\n- It's not common, but it happens. A Google search on `codegen bug optimization` turns up examples. Of course, compilers also have bugs *without* optimization enabled, so no, optimization is not the sole feature in the compiler which is defect-free. :)\n- \"compilers are built by smart people and do smart things\" and thus, can never go wrong - ha ha ha! If he believes that, give me his address - I've got a bridge that needs selling.\n- @Neil - is the bridge optimized?\n- @DVK - Doesn't matter, I'm sure it was built by smart people.\n- @Justin - I was about to comment that if they were smart, they'd be designing the bridge and not building it... and then I recalled the end of \"Office Space\" and realized you're right :)\n- He obviously never had to deal with compiling C code.\n- @CraigStuntz: I hope there aren't too many bug optimisations around! ;)\n- connect.microsoft.com/VisualStudio/feedback/details/573262/&hellip; connect.microsoft.com/VisualStudio/feedback/details/766477/&hellip; connect.microsoft.com/VisualStudio/feedback/details/411031/&hellip; connect.microsoft.com/VisualStudio/feedback/details/676003/&hellip;\n- When a programmer tells you that the compiler causes an error, it is a lie in 99% of cases. The compiler is to blame for everything: medium.com/@Code_Analysis/&hellip;\n- Weird question, but very well then, here's your proof: stackoverflow.com/questions/37830382/compiler-bug-in-f-4\n- It's more like an intuition. I trust the tool, but I'm sure compiler optimization can **sometimes** do things you wouldn't except. I just can't give an example. Maybe i'm wrong, and there is no such examples.\n- Well there are examples (provided above and in comments), but it seems like a silly argument (especially your friends \"supporting argument\") until it becomes a real issue. But you are indeed correct, it is absolutely possible.\n- An ability to examine compiler output when code isn't behaving as expected is often useful. In some cases, such examination leads to the discovery of a weird corner case in the C spec which means code isn't actually guaranteed to work as expected. In other cases, it leads to the discovery of genuine compiler bugs [e.g. using a special-purpose CPU register to hold a variable, but then using it for its dedicated purpose without saving it first].\n- A more common reason to turn off optimizations is that debugging is usually harder with them on.\n- Non-volatile reads is a great example of *misbehavior* caused by compiler (or runtime) optimization, though I'm not sure if that's the sort of thing that one would classify as a \"bug\", since it's the duty of the developer to account for such things.\n- @Adam Robinson: Agree completely. I've expanded on this point in my answer.\n- Thank you for your answer. I love the \"That's why you can turn them off\" => Just unobjectionable ! ;)\n- The example given is why the \"volatile\" keyword exists. It's a code error. A bug-free compiler will not introduce bugs even with optimization.\n- Compiler optimization can bring existing bugs to the surface. Tghey (usually) *introduce* them - unless there's a compiler bug.\n- @Mr. Shiny has given his reason for believing why optimization can be turned off, but this is far from *indisputable* and I doubt it's explained as such in most compiler documentation. When debugging people typically want machine code that can be easily related to the original code and so would work with builds where optimization is turned off, while QA builds (and the final release) would have optimization turned on. Anyone who, on discovering an issue like the one described by @Mr. Shiny, then \"fixes\" it by turning optimization off is fixing the wrong thing in my opinion.\n- Disagree. Optmizations may bring bugs in your code to the surface, and the optimizer itself may be buggy, but \"optimization can *introduce* bugs\" is factually wrong and also puts the blame on the wrong target.\n- This answer is simply wrong. All compiler optimizations must be fully compliant with with the language's standard. So compiler optimizations never introduce bugs in your code. Instead, they sometimes help you find hidden bugs that YOU have made. Of course, if there is a bug in the compiler itself, then your code might not be compiled correctly. But this doesn't mean that compiler optimizations can introduce bugs. It's an invalid argument,\n- @HadiBrais You contradict yourself. If your compiler has a bug in an optimizer, then it introduces bugs in your code. I've encountered this many times. Also, here is an example of a compiler optimizing away a null-pointer check incorrectly. Sometimes certain optimizations are wrong for valid code.\n- @Mr.ShinyandNew安宇 A particular **implementation** of an optimizer can introduce bugs if it was buggy (which is obvious and no one disagrees). But **compiler optimizations** themselves don't introduce bugs because they are defined to be compliant with the the standard. Your answer is wrong because it does not answer the OP's question and has clearly confused many people including the OP.\n- @HadiBrais did you the link I just gave you? GCC optimized away a null pointer check in a situation where null was a valid pointer. How is that not introducing a bug? The optimization may be legal, but it's wrong.\n- @Mr.ShinyandNew安宇 You're missing the point again and again. Regarding the example from link you provided, the compiler optimization didn't introduce any bugs. The bug was already there made by the programmer at line `struct sock *sk = tun->sk;` dereferencing `tun` without NULL-check. Second, the C++ standard states that NULL is a pointer to no object and cannot be dereferenced. Based on this, the compiler optimized the code, which is perfectly OK and compliant with the standard.\n- @HadiBrais I'd say you're missing the point. The null pointer *can* be valid, no matter what the standard claims, as is evidenced by the fact that you can map a page at address zero. Removing the null check was not a valid optimization. There are other examples, such as re-ordering memory accesses that break double-checked locking, because re-ordering those accesses is valid on single-threads (but it's invalid in multi-threaded situations). The compiler doesn't always know the full details of the code. Sometimes its assumptions are wrong.\n- @Mr.ShinyandNew安宇 It doesn't matter whether a null pointer can be valid or not. All that matters is that the standard says it's invalid, and the compiler only has to conform to the standard. When a programmer writes code that violates the standard or when he doesn't understand the standard, he or she has to take full responsibility of the consequences. I've read all the \"examples\" in this thread and they are all invalid for the same reason.\n- @HadiBrais So your position is that the spec is never wrong or incomplete, that compilers are never buggy, and that reasonable people can't disagree on what \"correct\" code is? Therefore, optimizations can never introduce bugs or undesirable behaviour. That's a pretty narrow way to look at things. In the world I live in, specs can be wrong or incomplete, optimizations can be valid only under certain conditions that aren't always true, optimizers can be wrong or buggy, etc.\n- @Mr.ShinyandNew安宇 Specs can be illogical or vague and compilers can be buggy. I never claimed otherwise. However, a compiler optimization can introduce bugs ONLY when it has bugs that have been triggered. This what I've said twice in my first and second comment. But the examples you've provided and your answer suggest that compiler optimizations can introduce bugs even if they are NOT buggy, which is where you're mistaken as I've explained several times.\n- @HadiBrais Just because you've claimed something is true several times doesn't make it right. Firstly, the question asked \"Can optimizers introduce bugs or undesirable behaviour\" and there are several clear reasons why they *can*. This is indisputable. You are nitpicking about the supposed case where an optimizer is behaving correctly and merely exposing an existing bug (that doesn't exist outside of optimization).\n- Consider the fact that the C standard says that pointer overflow is undefined. The compiler takes this to mean that pointers can never overflow, when in reality they can. This means that a programmer who understands the hardware they're dealing with cannot actually check for pointer overflow easily, because the compiler writer can use \"undefined behaviour\" to introduce ANY code at all into the program. This is technically legal but logically stupid.\n- Also consider strict aliasing. If a compiler is assuming strict aliasing, it can optimize things better. strict aliasing isn't always appropriate though. Those optimizations would be \"legal\" but wrong.\n- @Mr.ShinyandNew安宇: It's too bad the authors of C89 were sloppy in their description of aliasing rules. Their intention was to draft rules with which the vast majority of existing code would already be in compliance, and the rules can be interpreted in such a fashion, but it has become fashionable to retroactively reinterpret the rules so to say that constructs which were in widespread use before C89 came on the scene were never legitimate, even if such reinterpretation of the rules would offer no alternative that was anywhere near as efficient.\n- What about optimizing out useless writes of zeroes to certain memory location? That can be fine security issue. And is perfect example of perfectly valid optimizer with perfect code and perfect compiler introducing a BUG. So yes, they can do just that.\n- @darkestkhan That example is in my answer, \"Clearing memory of sensitive information\".\n- I know but the one who was defending optimizations didn't seem to even notice this one example.\n- @darkestkhan That's also an invalid example because the standard *allows* the compiler to optimize memory reads and writes *when the single-threaded observable behavior doesn't change and there is no undefined behavior*. If the programmer misunderstood the language and thought that `memset` won't be optimized away, then *it is the programmer that is at fault*, it is the programmer that introduced the bug in the code, not the compiler. The programmer is supposed to use secure functions such as `memset_s` or `SecureZeroMemory`, which will not be optimized away...\n- ...This answer essentially says \"optimizations can introduce bugs when the programmer misunderstands the language or expect things that are not guaranteed by the language\", which makes no sense, and \"optimizations can introduce bugs when the compiler has bugs\", which is very obvious and is *not* what the OP question is about. Jon Skeet's answer is the correct answer.\n- @HadiBrais Look, the fact is that compilers can not always know if the \"single-threaded observable behaviour doesn't change\". So instead they add hacks like *memset_s*. That's just weaseling out of their obligations to write usable software that doesn't surprise the user IMO.\n- I think my comments are very clear, as well as Jon Skeet's answer. The right thing to do now is to edit your answer to point future readers to Jon Skeet's answer. It's up to you now.\n- @HadiBrais Sorry, from my perspective the evidence is clear that compiler optimizations have gone too far and too often cause problems by insisting on an interpretation of the rules that makes no sense; this combined with how the rules change over time means new compilers break old code in \"legal\" ways. I'll go as far as to say that the language specifications are themselves wrong in some cases and compiler writers are sneaking through loopholes that should not exist. The OP asked for \"bugs or undesired behaviour\" caused by optimizations and the fact is there are many many pitfalls.\n- *Then the C language standard is amended by compiler developers* That's only true if you mean the de-facto undocumented assumptions that developers were assuming when compiling for 2's complement C implementations. Even ISO C89 says that signed overflow is UB; that hasn't changed. What changed is how aggressively compilers take advantage of the assumption that the program contains no UB. This is a real issue that *exposes* lurking bugs in some code, and makes modern C not close to a \"portable assembler\" that it would sometimes be nice to have.\n- You could say that older compilers kind of defined lots of behaviour that ISO C left undefined, and implementations have stopped doing that in more cases. But those were never truly *standard* behaviours. You do have a point, but please phrase it more accurately.\n- Every single example in here is broken (assuming the overflow in them is signed overflow). Signed overflow has *always* been defined by the standard to be undefined behaviour. The compiler has the right to assume that undefined behavior cannot occur (as the standard gives *no* obligations on what must occur then), so here it is always the code that is broken, not the compiler optimizations.\n- @GabrielRavier you're just repeating what everyone who sides on the compiler language standards side of the debate says. \"The standard doesn't say the compiler can't eat your dog, so don't complain if it eats your dog\". The fact is that in most cases eating my dog is unreasonable, and furthermore, things like overflow are extraordinarily difficult to handle \"correctly\" without tons of boilerplate code everywhere, whilst being trivially easy to handle if your compiler just lets the cpu handle the overflow the way you're taught 2s complement math works. Languages should not be filled with traps.\n- The argument is about whether \"optimizations can introduce bugs\". I argue that it does not introduce bugs, unless you are not writing C. If your program does not work after optimization, then either there is a bug in the optimizer or there is a bug in your program. If the optimizer conforms to the transformations that the C standard allows, then the bug is in your code. If you argue that the transformations are stupid and should not be done, I'd say that you are not writing C, you are writing \"C except the compiler is not allowed to do these transformations\", which is not C.\n- @GabrielRavier You're arguing that the compiler eating your dog is valid C, because it's not not valid C. That's logically true, but also undesirable behaviour/buggy. It's also pretty clear from the industry usage that most people cannot reliably keep straight all the forbidden things that compilers are aggressively pouncing on. But seriously, look at the overflow case: overflow is SO EASY to handle the wrong way, and SO ANNOYING to handle the right way. Do you test all your integers for possible overflows before adding/subtracting them? Do you even know anyone who does?\n- @Mr.ShinyandNew安宇 \"You're arguing that the compiler eating your dog is valid C, because it's not not valid C.\". I don't really get what you're trying to say here. \"the compiler eating your dog\" is not valid C as far as I know (except if you prepend it with a bunch of preprocessor defines for every single word of that sentence, but that's besides the point). Also, I'm confused about what \"it\" designates in that sentence, if it designates \"the compiler eating your dog\", the sentence is weirdly self-contradictory, otherwise, I don't get what you're talking about and would like some clarification.\n- Your argument that the compiler can introduce any behavior it wants when the program does something undefined means the compiler can eat your dog if your program overflows an integer. My position is that it should not do that, and in fact it should respect the de facto standards and conventions of programming, like supporting overflow on 2s complement hardware. That and other \"optimizations\" that assume more than the programmer intends are constant headaches for all but expert programmers. Just read the presentation from cert in my answer... People are getting this stuff wrong.\n- Apparently this was a bug in the compiler. Of course compilers can (do!) have bugs: they're pieces of software. I'm more interested in examples of bugs (undesired/unexpected behavior) in the optimized code that are not caused by compiler bugs.\n- I pointed out a well-known issue in a post below where C++'s optimizer will introduce a bug using the double-checked locking pattern. This is correct behavior as far as the current C++ spec is concerned, it is not a compiler bug, it is well-specified behavior that works perfectly fine with optimizations turned off, but breaks when it is turned on.\n- @Jon Skeet: I never downvote (well, hardly ever), but at 172k you even notice? :-)\n- ++ Especially in Fortran. We're always running into situations where the Fortran works fine, but only if you use a particular opt level. And running Fortran under an IDE? where the optimizer (even if you didn't ask for it) has felt at liberty to totally scramble the code and shuffle the variables? all in the name of \"Optimization\"? Gimme a *brreak*!\n- @Mike -- I your pain, my recent situation is a Fortran program I'm porting from one cluster to another 'identical' one. IDE ? surely you mean Emacs :-)\n- Sorry I'm stuck in the Windows world. Fortran is a hot potato - MS, DEC, Compaq, now Intel. Did I miss any foster parents? It works under .net, but only if your boss keeps paying for upgrades. Plus GCC, where we have lovely GDB. I was once told, like it or not \"Fortran is like Rock 'n Roll, it will Never Die!\"\n- As you seem to indicate, that is a shortcoming of C++ not a problem with optimizing compilers. It's a problem that exists even without optimization.\n- Not at all. With no optimization double-checked locking is fine. It's a painful type of bug to find because it will work fine in a debug build. It's only because the optimizer is allowed to make logical changes that are equivalent in a single-threaded system that there is a problem. You could claim that it is an issue with what is allowed in C++ by the optimizer, but it is still an area where compiling with optimization turned on can cause issues.\n- This is why C11 / C++11 introduced a thread-aware memory model, and `std::atomic`. Pre-C++11 lockless atomics and roll-your-own locking were always highly dependent on a lot of assumptions about how compilers worked (and often needed inline-asm) and abuse of `volatile`.\n- I have written a few bug-free programs in the past. \"Hello world\" and such. :P\n- I write safety-critical hard real-time software, and I *always* build with max optimization. If there is a compiler bug I want to find it early, not wait until we're out of CPU time AND calendar time and have someone say \"-O3\" can help, and then ship buggy code because we didn't get enough testing. If you don't trust your tools, don't use them.\n- @Martinho Fernandes: Yeap, there's even a saying: Is it possible to write a completely bug-free program? Yes, but it will be useless.\n- @phkahler I have seen DO-178b certified projects, some of those DAL-A level where the design board forbids any type of compiler optimization. Even threads are usually forbidden. I have made some research on compilers and met many people who agrees that C++ and Java languages have very serious flaws in their design (hence you dont seem them in DAL-A software), although people try to live with it or ignore them. I have very few tools I trust and most of the stress of my work is because I am forced to use buggy libraries and/or tools\n- @Francisco - We use C in automotive stuff, not C++ or Java. Mass produced products are not subject to DO-178b whatever that is. We are subject to lawyers which is probably worse. Agreed that threads are bad. Certified compilers are not generally available for our targets, and I have seen GCC used to build code for airbag controllers with much success. I've seen compiler bugs, processor bugs, and hardware design issues. Building code at O3 is the least of my worries.\n- @phkahler: It's like you say, you have to trust your tools or find others to replace them. I happen to know that compilers and optimkizers are written by some very smart people (not me though :()and with front-, middle- and back-ends maintained by separate teams code and optimizer quality should increase. As to the question of optimizer rules: it's easier to write optimizers according to generalized rules and produce what some might call bugs (volatile example) than trying to specify every single exception to the rules and risk bloated code that is difficult to maintain. +1\n- Personally I do not think ereOn wanted a joke answer. Still, maybe you could change \"will\" to \"can\" so your answer is at least partially correct/reasonable?\n- @Trisped My answer was whimsical, but I think accurate. Do compiler optimizations sometimes introduce bugs? Yes. To say no, that's absolutely impossible would mean that compiler writers are infallible programming gods, which is of course false. Do compiler optimizations OFTEN introduce bugs? No. They're usually well tested. But of course we'd have to say which compiler.","metadata":{"transformedAt":"2026-08-18T18:32:17.747Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":172,"estimatedTokens":5955}}43{"id":"stack-5192223","source":"stackoverflow","questionId":5192223,"title":"Convert javascript code to c code","tags":["javascript","c","compiler-construction"],"text":"Title: Convert javascript code to c code\nTags: javascript, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIs there any way to convert C code to JavaScript and from JavaScript to C? I found `V8 juice` which can generate `JavaScript-side` classes from C++, but it's only one way (C++ to JavaScript).\n\nI'm not looking for a software.\n\n========================================\n\nTop Answer:\nThere are a few compilers that translate JavaScript and TypeScript to C:\n\n- QuickJS compiles JavaScript to C using an embedded JavaScript engine. This implementation is fairly complete, unlike the other compilers listed here.\n\n- ts2c translates JavaScript and TypeScript source code to C, but it only supports a small subset of JavaScript's features.\n\n- NectarJS compiles a subset of JavaScript to C or WebAssembly.\n\nSimilarly, it may be possible to compile some statically-typed JavaScript programs to WebAssembly using AssemblyScript, and then decompile them to C using wasm2c.\n\nAlternatively, it might be possible to compile JavaScript to another language that compiles to C:\n\n- Compile JavaScript to Python, and then compile Python to C using Cython or RPython. Since these compilers are compatible with a subset of Python, this should allow a subset of JavaScript to be translated to C.\n\n- Compile JavaScript to Lua using Castl, and then translate the Lua code to C using lua2c.\n\n========================================\n\nCode:\n```text\nV8 juice\n```\n\n```text\nJavaScript-side\n```\n\n```text\neval()\n```\n\n```text\n.so\n```\n\n========================================\n\nComments:\n- I'm running now node.js to execute javascript code under my linux server, so I want to let other people to add plugins by writing c code. This is why I need a library or an engine like v8 juice to convert c code to javascript automaticaly.\n- Not an Answer, just a comment: There is an JScript compiler windows/.NET brings. It's called jsc.exe. It can't compile every JavaScript. If it is installed it's located either in c:\\Windows\\winsxs or in C:\\Windows\\Microsoft.NET\\Framework\\. If you have an plain javascript algorithm without fancy stuff, it may work.\n- It doesn't make any sense to me.\n- Just use Haxe, much better but similar language that compiles to all targets imaginary. From Javascript to C, runs on devices, servers, browsers, even embedded and airborne software.\n- until now I have been manually translating javascripts to c++, and even a partial parser would be helpful making the translation easier, however I have not really seen any worth mentioning, so I guess you will have to do it manually\n- @Ska do you have an example of haxe running on embedded and airborne hardware?\n- Nonetheless, computer programs are deterministic so it's theoretically possible to compile everything back into C because all the mechanisms used in interpreting JavaScript exist in C already. Someone would have to spend time making that tool. Who's down to volunteer? x}\n- It's possible and very easy. All you have to do is put PHP as a preprocessor for C instead of what it now uses(#ifdef won't cut it if you want the resulting code to be readable, and you will need constructs like generic maps to do the conversion). After that, all you do is create a wall between \"compile-time\" code and \"runtime-code\", after this, it's trivial to marshall js to c and visa versa via json, and let c evaluate javascript to internal representation of javascript, but to do this and have nice code, you need a more powerful preprocessor for c.\n- Sadly at the moment there is no \"nice way\" to have maps in C, all of them are ugly and messy and bug prone, and javascript relies almost exclusively on maps. You can do it in C++ but At this point you may as well just embed V8 because it's written in C++ and will do it better than you will, but won't have \"absolute free unmangled abstractions and gdb-friendliness\" that C has. With a more powerful preprocessor, C will easily be able to bridge to any upward language(that supports runtime generics via objects, whereas in C you need to invent an object to store the metadata of void pointers)\n- I emphasize the preprocessor because the whole purpose of translating ANYTHING to C, is to turn your abstractions into free and gdb-friendly abstractions. That is, you gain fine-grained control over where your data is, how it is stored at any moment, and have absolutely no mangling and have perfect debuggability at per-instruction level. This is absolutely possible, but all attempts to do so never quite made the golden bridge from C to javascript, if we would, C++ would not exist since C would just be the best language to use at any level of programming. Sadly C needs new toys.\n- What about on a microcontroller? My aim is to convert some math algorithms from js into c++ automatically so I don't need to manually port\n- Why not use JerryScript by Samsung ? jerryscript.net\n- But we want fullsets! That's interesting though.\n- Can other engines like Duktape also be used like QuickJS for this?\n- Hey! Go back to 1990's! No one is using interrupts now with C! (except osdev maniacs, like me!)\n- Unfortunately qjsc is not a JS to C transpiler; it merely outputs a bytecode for Bellard's interpreter, as a C array. I believe the OP (like myself) was looking for something that output idiomatic C, at least where possible. ts2c crashes on every input I've tried. Still looking.","metadata":{"transformedAt":"2026-08-18T18:32:17.747Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":66,"estimatedTokens":1345}}44{"id":"stack-39246498","source":"stackoverflow","questionId":39246498,"title":"Compiler vs Interpreter vs Transpiler","tags":["compiler-construction","language-agnostic","terminology","interpreter","transpiler"],"text":"Title: Compiler vs Interpreter vs Transpiler\nTags: compiler-construction, language-agnostic, terminology, interpreter, transpiler\nSource: Stack Overflow\n\nQuestion:\nDuring a ReactJS session that I was attending, the presenter used a term **transpiler** for some code conversion/porting happening. In the past, I have heard the terms compiler and interpreter. I've used in the context of converting a user written language code to a runnable form on a computer system. Transpiler is new to me. How is a transpiler different from a compiler or an interpreter and why is it needed?\n\n========================================\n\nTop Answer:\n**Compiler** - compiles code to a lower level code.\n\nExample: \n\n- `\"Developer code\"` -> `\"Machine code\"`\n\n- `PHP` -> `C`\n\n- `Java` -> `bytecode`\n\n**Transpiler** - compiles code to same level of code/abstraction.\n\nExample: \n\n- `\"Developer code\"` -> `\"Another developer code or version\"`\n\n- `JavaScript ES2015+` -> `JavaScript ES5`\n\n**Interpreter** - interprets code, not really in the same class/league/context with the two above.\n\nExample: `php.exe`\n\n- \"Your PHP code/scripts inside `index.php`\" -> \"Results to `html` or just like pure `index.html`\"\n\n========================================\n\nCode:\n```text\n\"Developer code\"\n```\n\n```text\n\"Machine code\"\n```\n\n```text\nPHP\n```\n\n```text\nC\n```\n\n```text\nJava\n```\n\n```text\nbytecode\n```\n\n```text\n\"Developer code\"\n```\n\n```text\n\"Another developer code or version\"\n```\n\n```text\nJavaScript ES2015+\n```\n\n```text\nJavaScript ES5\n```\n\n```text\nphp.exe\n```\n\n```text\nindex.php\n```\n\n```text\nhtml\n```\n\n```text\nindex.html\n```\n\n========================================\n\nComments:\n- You're right - transpiler is a redundant term, and should never be used. Any compiler is a \"transpiler\".\n- Dear downvoter - If you can leave an appropriate feedback/comment then I can take an appropriate action to improve the post.\n- `Xtend` (eclipse.org/xtend) and `NetRexx` (netrexx.org) are 2 languages that are translated to Java\n- Another example is TypeScript language that translates (transpiled) to JavaScript.\n- Agreed. I built one in 1979. It is essentially just a compiler with a different kind of target language.\n- And computer used to be called a \"difference engine\". Mouse used to be called \"X-Y position indicator for a display system\". A predecessor to cars was called \"fire engine for transporting wagons and especially artillery\". It is in the nature of language to shorten things: see Zipf's Law and linguistic economy. \"It was good enough for us back in the day, these youngsters...\" is rubbish. It's called transpiler because \"source-to-source transforming compiler\" is too long for everyday use, now that transpilers *are* everyday use (TypeScript/CoffeeScript/ES6 and SCSS being the prominent examples).\n- Your examples are all \"this is the clumsy phrase we used when we only had a few\"; it wasn't long before the standard terminology was established and people have not invented replacement terms for mouse. My point is this technology has been with us 50+ years. \"Transformation System\" is the way one shortens the longer phrase if you want linguistic economy. And these have been in everyday use in many areas of computing; perhaps just you didn't notice them. Having TypeScript appear doesn't suddenly change the concept, and cause a need for a new term. You sound to me like one of those priests.\n- @Amadan You aren't even accurate. Babbage's device was called an Analytic Engine. Its predecessor was called a difference engine, but it wasn't a computer. This was 1837. The term 'computer' as presently understood has been in use for seventy years,\n- Whether my exact examples are correct or not is irrelevant. I just wanted to point out that it is not a conspiracy, it is just language being language - ask any linguist. Language changes, new words come into existence all the time, and survive if they manage to be useful to a sizable enough chunk of the population. \"Priests\"? Give me a break. The issue with TypeScript is not that it is a new concept; but that the concept broke out from the micropopulation it was in before, and spread to masses: that's what drives the linguistic change.\n- @Amadan I don't think any of your examples are accurate actually, and as your argument is fundamentally anecdotal it stands or falls by your examples. It is not in dispute that language is dynamic. The question is whether we need a new term for an existing concept. Negative reaction to language change is just as legitimate a force in language evaluation as innovation: a point that linguists invariable overlook.\n- They overlook it because natural language changes NATURALLY. The whole point is that the masses decide, not the minority programmers from 1979. Believe me it's better to embrace it and be content rather than spend your time and effort debating the frivolous.\n- I get my vote too about how the langauge evolves, but it doesn't get counted if I don't complain.\n- Non of @Amadan examples are correct actually. Was rooting for him, even the mouse was not named that, it was \"described\" as that. en.wikipedia.org/wiki/Douglas_Engelbart And Ira Baxter, checked out your linkedin profile. You're a sage. Much respect.\n- @Cozzbie: You're quoting Wikipedia. I'm quoting the actual patent that Engelbart filed. But again, and I stand by it: whether my specific examples are correct or not is irrelevant. I only reacted to note how I would respect this answer, and the huge experience behind it, so much more if it resisted the urge to diss people for no reason other than using language differently than him. The last paragraph of the answer is ugly and beneath him; it is not objective, and not worthy of being a StackOverflow answer.\n- What about SQL to java code? is it a compiler or transpiler? from the definition above it sounds to me like a compiler. isn't it?\n- @user1870400 My opinion here for SQL is to use the the term \"code generation\" or \"migration\". Thats what you are likely to here in case of SQL and such. But depends on what exact you might want to do. I'd say SQL is not a programming language but a \"query language\" (hope no one is going to rost me for that statement). Also when you think about OpenAPI/swagger you can generate code out of a definition format. That's probably not what should be called compiled but \"generated\".\n- Quick question, if I create a language using C++, and I turn that language's source code into C++ as part of my \"compiler\", would that really just be transpiling?\n- Where do you see Python fit in this interpretation? Python compiles source code into bytecode hen executes using a bytecode interpreter. Given all of this, I see Python at the same level as Java: a compiler.\n- @Jim M mdn calls Svelte a compiler. Can you explain how Svelte work with your terminology? Thank you.\n- This answer would profit from citing sources for the used statements. I don't say it's wrong, just that giving sources could distinguish it from other answers.","metadata":{"transformedAt":"2026-08-18T18:32:17.747Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":14,"totalLines":116,"estimatedTokens":1741}}45{"id":"stack-3891513","source":"stackoverflow","questionId":3891513,"title":"How does C-- compare to LLVM?","tags":["c","compiler-construction","code-generation","llvm","c-minus-minus"],"text":"Title: How does C-- compare to LLVM?\nTags: c, compiler-construction, code-generation, llvm, c-minus-minus\nSource: Stack Overflow\n\nQuestion:\nAfter learning a bit of how LLVM work I'm really excited about how portable low-level code can be generated and how modular this 'thing' is built.\n\nBut I discovered today the existence of C-- that seems to some concepts with LLVM.\n\nSo I'm looking for some information helping me understand the main differences between these two projects... and why both exist.\n\nFor me LLVM looks a bit like the ultimate Swiss Army knife for compiler infrastructure, and C-- looks far less advanced.\n\n========================================\n\nComments:\n- apparently, you can' have `-` in a tag. cminusminus it is.\n- I don't know about quality of implementation, but LLVM seems to have a lot more action on it's mailing lists.\n- using 'mm' is a common way to express minus minus. i believe it's used as a joke for many c++ bindings, such as glibmm. 'glib--'. i don't imagine newcomers are going to know to search for cmm tho.\n- @matt also, cmm already exists as a completely different language than c--.\n- I just added a c-minus-minus tag (already used by two other questions.\n- @ThomasM.DuBuisson: why do you say \"completely different\"? From your first link: \"Cmm is the GHC implementation of the C-- language.\" Although I don't know if this was true when you wrote your comment 20 months ago.\n- @dubiousjim I don't know, maybe because I believed it? At any rate, Cmm isn't C-- - the paragraph you quote goes on to mention there are significant differences.\n- So, if I understand correctly the main difference is the abstraction level of the intermediate representation used by theses compiler. thanks !\n- @Alois Cochard, that is how it seemed to me. I am no compiler expert, I just play one on Stack Overflow. :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.747Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":460}}46{"id":"stack-8422775","source":"stackoverflow","questionId":8422775,"title":"Why does C need \"struct\" keyword and not C++?","tags":["c++","c","compiler-construction"],"text":"Title: Why does C need \"struct\" keyword and not C++?\nTags: c++, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI've always been a little confused about what's going on here:\n\n```\n#include \n\nint main() { \n timeval tv;\n tv.tv_sec = 1;\n\n for (;;) {\n select(0, 0, 0, 0, &tv);\n printf(\"%s\\n\", \"Hello World!\");\n }\n}\n```\n\nSorry if that doesn't compile, just wrote it as a quick example.\n\nCode like this won't compile under gcc unless I add the keyword **struct** prior to the use of the struct timeval. g++ on the other hand handles it fine as is.\n\nIs this a difference between how C and C++ handle structures or is it just a difference in the compilers? (I'm very C++ oriented, and the use of struct in C on lines like this has always somewhat baffled me).\n\n========================================\n\nTop Answer:\nConsider the original idea of C++ (or, when it was just an idea, \"C with classes\"), that of an OO-oriented language that was compatible with C to the point where most valid C programs were also valid C++ programs.\n\nC++ built its class model by starting with C's `struct` and adding some further functionality:\n\n- Inheritance (though you can come close in C with having the first member of a struct the struct you want to \"inherit\" from).\n\n- Information hiding (through `public`, `private` etc)\n\n- Member methods (which were originally turned by macros into C code outside the struct with an added `this` parameter - many implementations are still similar in practice).\n\nAt this point there were two problems. The first is that the default access had to be public, since C has no information hiding and therefore from a C++ perspective has everything public. For good OO one should default to private. This was solved by adding `class` which is pretty much identical to `struct` except for the default is `private` rather than `public`.\n\nThe other is that this OO perspective should have `timeval` or any other class/struct on the same \"footing\" as `int` or `char`, rather than constantly annotated in the code as special. This was solved by relaxing the rule that one must place `struct` (or `class`) before the name of the type in declaring a variable of that type. Hence `struct timeval tv` can become `timeval tv`.\n\nThis then influenced later C-syntax OO languages, like Java and C# to the point where, for example, only the shorter form (`timeval tv`) would be valid syntax in C#.\n\n========================================\n\nCode:\n```text\n#include <stdio.h>\n\nint main() {  \n    timeval tv;\n    tv.tv_sec = 1;\n\n    for (;;) {\n        select(0, 0, 0, 0, &tv);\n        printf(\"%s\\n\", \"Hello World!\");\n    }\n}\n```\n\n```text\nstruct\n```\n\n```text\nstruct\n```\n\n```text\nclass\n```\n\n```text\nstruct\n```\n\n```text\nstat\n```\n\n```text\nstruct stat\n```\n\n```text\nstat\n```\n\n```text\ntypedef struct YourStructure\n{\n   int x;\n   // more fields\n} YourStructure;\n```\n\n```text\nclass\n```\n\n```text\nstruct foo x;         // create a structure of pattern foo\ntypedef foo foo_type; // \"define\" a type\nfoo_type x;           // create an instance of type foo_type\n```\n\n```text\nfoo x; // create an instance of type foo\n```\n\n```text\nstruct foo\n```\n\n```text\nstruct foo\n```\n\n```text\nfoo\n```\n\n```text\nstruct\n```\n\n```text\npublic\n```\n\n```text\nprivate\n```\n\n```text\nthis\n```\n\n```text\nclass\n```\n\n```text\nstruct\n```\n\n```text\nprivate\n```\n\n```text\npublic\n```\n\n```text\ntimeval\n```\n\n```text\nint\n```\n\n```text\nchar\n```\n\n```text\nstruct\n```\n\n```text\nclass\n```\n\n```text\nstruct timeval tv\n```\n\n```text\ntimeval tv\n```\n\n```text\ntimeval tv\n```\n\n========================================\n\nComments:\n- I like to think of C having a \"typedef namespace\" and a \"struct namespace\". If a struct isn't typedef'd, you'll need to explicitly put the \"struct\" before it in order to \"resolve\" its \"namespace\". You can imagine that C++ automatically chucks all structs into the \"typedef namespace\". This is of course not really what's going on, but I find this mental model useful.\n- C needs the keyword because that's its syntax. I'm not sure what's being asked here. Are you wondering about the design rationale?\n- @Rooke, for additional confusion, the \"typedef\" namespace is actually the object namespace; `typedef` is a storage class like `static`, `extern` and `auto`, so a typedef declaration is syntactically the same as an object declaration.\n- @Rooke, that's pretty much exactly what's going on, C struct tags are in their own namespace, and .. what Simon said about typedef namespace. In original K+R C, all names of struct members were in a *common* namespace - this is why the ancient system structs like 'struct stat' all have prefixes ( st_dev, st_ino, st_mode ...) to avoid collisions with members of other structs. Each such name was associated with a type and offset, not what struct it was in. So the -> operator didn't care what pointer type was on the left. That was fixed pretty early on though.\n- Permissive is a very unfitting word.\n- Because this is about namespaces not about permissiveness.\n- @Let_Me_Be What should I say?\n- That C++ have less sorts of namespaces than C (which have different spaces for variables, labels, struct names, ...)\n- @Basile From the coder's perspective, C++ struct syntax is more forgiving. That's the most important point. I doubt the motivation for the variation was related to namespaces. The motivation was surely to make life easier and simpler for coders.\n- @BasileStarynkevitch, C++ has inherited the same \"types\" of namespaces (in the sense of C) from C. It only has an extra rule to interpret identifiers in the tag namespace as types. See my answer for an example.\n- I used the namespace word in the generic sense (like you would say that Scheme has only one namespace, but Common Lisp has two), not in the sense of the `namespace` keyword of C++\n- @BasileStarynkevitch I know what you mean when you talk about namespace. I just think that's an implementation detail that's not particularly illuminating from the perspective of the person asking the question.\n- There is also an example or `struct sigaction` and `int sigaction(...)` method for handling signals in Linux (from `signal.h`) where you need to to `struct` keyword if you want to use the `struct`\n- I'd like to add, struct is basically the same as class, except struct's default member export is 'public' but class's is 'private'.\n- Why can't c omit it? Just implicit typedef.\n- It is not just implicit typedef, but \"implicit typedef unless somebody else accidentally already uses the name for something else\". This is much context dependent and may have the impact that then your header works in most cases and then it would fail for spurious reasons where some user (or third party header) uses the same name.","metadata":{"transformedAt":"2026-08-18T18:32:17.747Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":31,"totalLines":208,"estimatedTokens":1671}}47{"id":"stack-3299648","source":"stackoverflow","questionId":3299648,"title":"Python Compilation/Interpretation Process","tags":["python","compiler-construction","interpreter","python-internals"],"text":"Title: Python Compilation/Interpretation Process\nTags: python, compiler-construction, interpreter, python-internals\nSource: Stack Overflow\n\nQuestion:\nI'm trying to understand the python compiler/interpreter process more clearly. Unfortunately, I have not taken a class in interpreters nor have I read much about them.\n\nBasically, what I understand right now is that Python code from `.py` files is first compiled into python bytecode (which I assume are the `.pyc` files I see occasionally?). Next, the bytecode is compiled into machine code, a language the processor actually understands.\nPretty much, I've read this thread Why python compile the source to bytecode before interpreting?\n\nCould somebody give me a good explanation of the whole process keeping in mind that my knowledge of compilers/interpreters is almost non-existent? Or, if that's not possible, maybe give me some resources that give quick overviews of compilers/interpreters?\n\nThanks\n\n========================================\n\nTop Answer:\nTo complete the great Marcelo Cantos's answer, here is just a small column-by-column summary to explain the output of disassembled bytecode.\n\nFor example, given this function:\n\n```\ndef f(num):\n if num == 42:\n return True\n return False\n```\n\nThis may be disassembled into (Python 3.6):\n\n```\n(1)|(2)|(3)|(4)| (5) |(6)| (7)\n---|---|---|---|----------------------|---|-------\n 2| | | 0|LOAD_FAST | 0|(num)\n |-->| | 2|LOAD_CONST | 1|(42)\n | | | 4|COMPARE_OP | 2|(==)\n | | | 6|POP_JUMP_IF_FALSE | 12|\n | | | | | |\n 3| | | 8|LOAD_CONST | 2|(True)\n | | | 10|RETURN_VALUE | |\n | | | | | |\n 4| |>> | 12|LOAD_CONST | 3|(False)\n | | | 14|RETURN_VALUE | |\n```\n\nEach column has a specific purpose:\n\n- The corresponding **line number** in the source code\n\n- Optionally indicates the **current instruction** executed (when the bytecode comes from a frame object for example)\n\n- A label which denotes a possible **`JUMP` from an earlier instruction** to this one\n\n- The **address** in the bytecode which corresponds to the byte index (those are multiples of 2 because Python 3.6 use 2 bytes for each instruction, while it could vary in previous versions)\n\n- The instruction name (also called **opname**), each one is briefly explained in the `dis` module and their implementation can be found in `ceval.c` (the core loop of CPython)\n\n- The **argument** (if any) of the instruction which is used internally by Python to fetch some constants or variables, manage the stack, jump to a specific instruction, etc.\n\n- The **human-friendly interpretation** of the instruction argument\n\n========================================\n\nCode:\n```text\n.py\n```\n\n```text\n.pyc\n```\n\n```text\n>>> def fib(n): return n if n < 2 else fib(n - 2) + fib(n - 1)\n... \n>>> fib(10)\n55\n>>> import dis\n>>> dis.dis(fib)\n  1           0 LOAD_FAST                0 (n)\n              3 LOAD_CONST               1 (2)\n              6 COMPARE_OP               0 (<)\n              9 JUMP_IF_FALSE            5 (to 17)\n             12 POP_TOP             \n             13 LOAD_FAST                0 (n)\n             16 RETURN_VALUE        \n        >>   17 POP_TOP             \n             18 LOAD_GLOBAL              0 (fib)\n             21 LOAD_FAST                0 (n)\n             24 LOAD_CONST               1 (2)\n             27 BINARY_SUBTRACT     \n             28 CALL_FUNCTION            1\n             31 LOAD_GLOBAL              0 (fib)\n             34 LOAD_FAST                0 (n)\n             37 LOAD_CONST               2 (1)\n             40 BINARY_SUBTRACT     \n             41 CALL_FUNCTION            1\n             44 BINARY_ADD          \n             45 RETURN_VALUE        \n>>>\n```\n\n```text\n>>> fib.func_code.co_consts\n(None, 2, 1)\n```\n\n```text\n>>> code = fib.func_code.co_code\n>>> code\n'|\\x00\\x00d\\x01\\x00j\\x00\\x00o\\x05\\x00\\x01|\\x00\\x00S\\x01t\\x00\\x00|\\x00\\x00d\\x01\\x00\\x18\\x83\\x01\\x00t\\x00\\x00|\\x00\\x00d\\x02\\x00\\x18\\x83\\x01\\x00\\x17S'\n>>> import opcode\n>>> op = code[0]\n>>> op\n'|'\n>>> op = ord(op)\n>>> op\n124\n>>> opcode.opname[op]\n'LOAD_FAST'\n>>>\n```\n\n```text\ndis\n```\n\n```text\nfib\n```\n\n```text\nLOAD_FAST 0\n```\n\n```text\nn\n```\n\n```text\nfib\n```\n\n```text\nLOAD_CONST 1\n```\n\n```text\nCOMPARE_OP 0\n```\n\n```text\nif n < 2\n```\n\n```text\nfib\n```\n\n```text\nfib\n```\n\n```text\nPOP_TOP\n```\n\n```text\nJUMP_IF_FALSE\n```\n\n```text\nfib\n```\n\n```text\nLOAD_FAST\n```\n\n```text\n'\\x00\\x00'\n```\n\n```text\nLOAD_FAST\n```\n\n```text\ndef f(num):\n    if num == 42:\n        return True\n    return False\n```\n\n```erlang\n(1)|(2)|(3)|(4)|          (5)         |(6)|  (7)\n---|---|---|---|----------------------|---|-------\n  2|   |   |  0|LOAD_FAST             |  0|(num)\n   |-->|   |  2|LOAD_CONST            |  1|(42)\n   |   |   |  4|COMPARE_OP            |  2|(==)\n   |   |   |  6|POP_JUMP_IF_FALSE     | 12|\n   |   |   |   |                      |   |\n  3|   |   |  8|LOAD_CONST            |  2|(True)\n   |   |   | 10|RETURN_VALUE          |   |\n   |   |   |   |                      |   |\n  4|   |>> | 12|LOAD_CONST            |  3|(False)\n   |   |   | 14|RETURN_VALUE          |   |\n```\n\n```text\nJUMP\n```\n\n```text\ndis\n```\n\n```text\nceval.c\n```\n\n========================================\n\nComments:\n- You do not \"interpret into machine code\" — that's what compilers do. Python interpreter just executes the bytecode. (And it's .pyc for bytecode.)\n- On a side note, you might find helpful to know that the last modification time of the original .py file is encoded in the .pyc file. This allows Python to figure out if a new .pyc file needs to be created or not. The purpose of .pyc files is, of course, to avoid parsing the whole script each time the script is invoked. A Python program will not run faster if the .pyc is used. Only the loading time changes.\n- So if it's not turned into machine code... how is it finally executed by my x86 procesor? I was under the impression that everything that is happening on my computer could eventually be broken down into 1's and 0's that are being read by my processor or some other hardware.\n- @JGord: I've extended my answer to address your comment.\n- Ok, so the part that is still blowing my mind is that my processor does not understand the LOAD_FAST opcode correct? That is bytecode for the python virtual machine. So somehow the virtual machine was written in a language that could be assembled into x86. Sure. However, how does the virtual machine actually do the operations it interprets from the byte code on my hardware? Let's take an example. My script does some calculation and it needs to send the result over the bus to my graphics card. The python virtual machine cannot do that am I right? Somehow the physical proc is doing something?\n- The interpreter/VM is in C. It is (to oversimplify somewhat) a loop that uses the current byte to choose one of many cases in a huge switch statement. Somewhere in the middle of the switch, there is a `case LOAD_FAST:` followed by code that reads the next two bytes, looks up the specified parameter in some \"parameters\" collection, and pushes it onto a stack object. To interact with the outside world, Python allows calls to extension modules, which act like Python code and objects, but are really compiled code and can thus talk to graphics cards, etc., directly, on behalf of your script(s).\n- To be a bit more explicit about your last question: there is no Python opcode for \"talk to the graphics card\". There is an opcode for \"call this function in this module\", and if the module is a graphics programming extension module, the interpreter will call the library's entry point for the requested function, passing it some parameters. The C library (assuming it's C) teases out the parameters, converting them from Python objects into C values and structs, and forwards the call onto a bona-fide graphics library, which then plonks a colorful triangle on your screen, or whatever.\n- Wow, thanks Marcelo! That's exactly what I was looking for. Mind telling me how/where you learned that? Also, you sure the C library is where the parameters are 'teased out'? That seems counterintuitive to me. Why wouldn't the python interpreter which understands python bytecode (and thus I assume understands python objects) tease out the parameters and just send C variables as parameters?\n- Idle curiosity ... almost three decades worth of it (not all of it Python, of course). The Python interpreter doesn't know about the C data types required by arbitrary C libraries. So it essentially passes its own internal representation of the Python-object parameters to the library, which does what it wants with them. Have a read here for a taste of how this works.\n- Hi Marcelo, despite reading your comments and post, I'm still confused about how Python byte code instructions get carried out by CPU. The VM can read Python byte code and carry out the instructions, but ultimately for the VM to carry out the instructions, they need to send those instructions to the CPU (right?). So isn't there any stage of compiling those instructions into CPU readable instructions? Thank you.\n- @Moondra The VM is itself a piece of native code — written in C and compiled to machine code — that does the actual work. It walks over the bytecode and performs the operations it finds therein. Revisiting the LOAD_FAST example I gave earlier, the code may be found at github.com/python/cpython/blob/3.6/Python/ceval.c#L1274. It's fairly noisy, but the guts of it is `GETLOCAL(oparg)`, which fetches the parameter corresponding to the opcode's argument into a local C variable, and `PUSH(value)`, which pushes the parameter onto the Python stack. That is, essentially, LOAD_FAST.\n- @MarceloCantos Thank you for your reply. I had taken a look at the some of the bytecode and CPython code, but I only have experience with Python, and none with C so that's why I'm having so much trouble. \"The VM is itself a piece of native code — written in C and compiled to machine code — that does the actual work\" -- Do you mean all of the VM switch cases are precompiled to a machine language? Thus, when the VM chooses a switch case, and executes it, it actually directly communicating with the CPU's memory? Thus no need for C to compile once again.\n- So far I understand that the bytecode gets translated into switch cases, which the VM executes. Essentially, the switch cases are representations of the bytecode instructions, but just written in C. But since I don't understand C at all, (still have a lot to learn about Python unfortunately), I don't understand what happens when you execute C code in the switches. I've read that C code first needs to be compiled to machine code before getting executed. Is that what we do here when we execute a switch case?\n- Or Is everything pre-compiled to machine code, so making executions of switch cases yield results right away. Thank you so much for you patience.\n- @Moondra CPython never translates bytecode into switch cases. The C code I linked to is the code that gets compiled into machine code. That machine code is a CPU-ready representation of the C code. You should think of the C code and the machine code as being different representations of exactly the same thing. The C is a human-readable form, while the machine code is a machine-readable form. A key point to understand is that the C program (in its compiled machine code form) is the only thing the CPU sees as code.\n- … The Python bytecode, in contrast, is seen by the CPU as just data. The C code *interprets* that data as code to be executed, hence the name *interpreter*.\n- It might help to think of the Python bytecode as a cooking recipe, and the C code as a cooking robot that reads and follows recipes in order to cook food. The robot itself has code inside it, which could well be C code, and the recipes are just data read in through the robot's eyes in order to know how to execute a particular cooking procedure. At one level, the recipe is code — a set of instructions to . At another level, it's just data to be fed to the robot's brain. hth\n- Ah, sorry 'translate' was a poor choice of word. I meant to use interpret. So eventually the C code is getting compiled to machine code! That's where the confusing was stemming from. Now, as far I know, Python code can interpreted line by line -- when I use the interpreter I can run lines in real time, but we can't do that with C right? From what I'm understanding is that C has to compile the entire code to machine language in one shot. It has to interpret the entirety of the byte-code again (past lines and the new lines), before compiling it to machine code.\n- So everytime we run Python code via the interpreter, are we forcing C to recompile the entire bytecode once again (the new lines as well as the older lines)?\n- @Moondra The C code is compiled once to make the Python interpreter (python.exe on Windows, just python on most other platforms). That's the \"cooking\" robot. You then run the interpreter, passing it the Python code to run. The interpreter reads the code and \"compiles\" it into bytecode, which it then interprets. It does this every time you run that piece of code.\n- There is an optimization for imported modules. After compiling an imported .py file, the interpreter caches the bytecode as a .pyc file alongside the imported .py file. Whenever a module is imported, the interpreter first checks to see if there's a corresponding .pyc file that's newer than the .py file. If there is, it loads the .pyc file directly instead of recompiling the .py file.\n- @MarceloCantos If The C code is only needs to be compiled once (to run the interpreter) are we can run code as many times through the interpreter -- the interpreter seems to be essentially a virtual CPU specifically built to read Python bytecode. I guess that's why it's called a virtual machine. I think I got it. Thank you so much for you patience. These details are hard to find on the web, in a way that beginners can understand. Once again thank you so much.\n- @Moondra That's spot on! Glad to help.\n- Hi, @MarceloCantos thanks for your great explanation! And though the `co_code` of function `fib` is `'|\\x00\\x00d\\x01\\x00j\\x00\\x00o\\x05\\x00\\x01|\\x00\\x00S\\x01t\\x00&zwnj;&#8203;\\x00|\\x00\\x00d\\x01\\x&zwnj;&#8203;00\\x18\\x83\\x01\\x00t\\&zwnj;&#8203;x00\\x00|\\x00\\x00d\\x0&zwnj;&#8203;2\\x00\\x18\\x83\\x01\\x0&zwnj;&#8203;0\\x17S'` the Python interpreter no need to import module `opcode` then translate like `|` to `LOAD_FAST` then do some operation, right? It just do the operation when it read `|`, the module `opcode` is just help people unstanding `co_code` of function, right?\n- @MarceloCantos Thanks for your reply, and I've asked a related question the link is stackoverflow.com/questions/55843979/&hellip; . Hope you will answer it when you have time. Thanks in advance.\n- In Python 3, use `__code__` instead of `func_code` on a function, e.g. `fib.__code__.co_code`.\n- How to read the implementation speed from it? There's no timing.\n- @YumiTada What do you mean by \"implementation speed\"? This is just compiled bytecode and not (yet) executed, so timing is irrelevant here.","metadata":{"transformedAt":"2026-08-18T18:32:17.747Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":26,"totalLines":252,"estimatedTokens":3755}}48{"id":"stack-2426091","source":"stackoverflow","questionId":2426091,"title":"What are the differences between a Just-in-Time-Compiler and an Interpreter?","tags":["java",".net","compiler-construction","interpreter","jit"],"text":"Title: What are the differences between a Just-in-Time-Compiler and an Interpreter?\nTags: java, .net, compiler-construction, interpreter, jit\nSource: Stack Overflow\n\nQuestion:\nWhat are the differences between a Just-in-Time-Compiler and an Interpreter, and are there differences between the .NET and the Java JIT compiler?\n\n========================================\n\nTop Answer:\nI've always found that a more abstract explanation sometimes helps.\nLet's say that you are trying to ask everyone in Mexico \"Hello. How are you?\" (your **source language**) Of course, you'll first need to translate it to Spanish (the **native language** of the country). That translation would be \"Hola. Como estas?\"\n\nIf you know Spanish, there would be no need for you to translate (**native code / assembler**). You just ask \"Hola. Como estas?\"\n\nIf you don't know Spanish, there are 3 ways to deal with it.\n\nThe first is to get a Spanish Dictionary (a **compiler**) and look up what the Spanish words are before you go. Perhaps you realize that \"Hola. Que tal?\" is one syllable shorter (**compiler optimization**) and use that instead. This is **language compilation**; you are converting the information to the native language beforehand. \n\nThe second is where you look up the words in the Spanish Dictionary while you are standing in front of the first person and then store the result (looking up the words **just-in-time**). The advantage here is that you could get a Mandarin Dictionary and then do the same experiment in China without having to keep ten sticky notes (**binaries for different platforms**) of translated phrases.\n\nThe third is where you look up the words while you are standing in front of each person. In essence, you interpret the words for each person separately (you act as an **interpreter**). The advantage here is that any changes are instantly reflected with the next person (you could change to asking \"Hello. What color is your dog?\" without having to fly home and restart - you don't need to **recompile** the phrases).\n\n- Translating beforehand means you can ask people fastest (**pre-compiliation**); you don't need to even bring the dictionary with you.\n\n- Translating when you see the first person in each country is almost as fast as translating beforehand but still allows you to travel to multiple countries without needing to go home to get a dictionary but means that you need to bring several dictionaries with you (**a platform independent runtime**).\n\n- Translating on demand is much slower but allows you to change the words without traveling home (**source distributed language**).\n\n========================================\n\nComments:\n- Your question title doesn't really match the question text. Both Java and .NET are jitted, neither are interpreted.\n- it served as a second question\n- then you should ask it as a second, different question. This way you are just confusing people.\n- Putting two questions in one is a problem, and putting just one in the title is more so. People will tend to search on the title, and therefore totally miss your second question. Looking over the answers as I write, including the two deleted ones, your second question has been completely overlooked.\n- Does the JIT Compiler of the CLR compiles the WHOLE code once or not?\n- No, it compiles only the necessary code. This gives you an atvantage to optimise in a runtime.\n- But the optimisation process per call occurs only one time, doesn't it (.NET JIT)? Because I red the .NET JIT compiles a peace of code only one time.\n- I would say this can be misleading to someone new to JIT etc. because it does not say explicitly the difference: Interpreter is like program that uses your source code as a guidelines as to which of \"his own\" subroutines to call. That is why it is called \"interpreter\". On the other hand, JIT transforms all your source code directlly to native.\n- Isn't point 1 and 3 the same? I mean how would an interpreter execute code without translating it somehow and use something that is already there?\n- Your answer is related to the Java JIT Compiler doesn't it?\n- Yup, but I believe that the JIT technique was first developed on smalltalk.\n- So a JIT causes a program to get faster the long it runs?\n- Not necessarily, because there could be some logic in the program causing it to be slow. The JIT won't change the logic. But at least the cost of CPU cycles that are spent converting the logic to machine code will only be incurred once.\n- Well done for including the BASIC/8080 instruction as an example. This helped me a lot.\n- Awesome, I love this answer. All others are too technical and would confuse non-programmers.\n- How would I take advantage of the stored (spanish) result when using Mandarin in China?\n- Ah ok I think I understand... As long as I am in Mexico I use the stored result?\n- This is an awesome explanation! Just one question though... what is a Source distributed language? I can't find a clear definition of it.\n- so the difference is mostly on code compliation optimization. for JIT, a section of code compiled with optimization, while interpreter doesn't do any optimization.","metadata":{"transformedAt":"2026-08-18T18:32:17.748Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":51,"estimatedTokens":1284}}49{"id":"stack-9105505","source":"stackoverflow","questionId":9105505,"title":"Differences between Just in Time compilation and On Stack Replacement","tags":["java","compiler-construction","jvm","jit","vm-implementation"],"text":"Title: Differences between Just in Time compilation and On Stack Replacement\nTags: java, compiler-construction, jvm, jit, vm-implementation\nSource: Stack Overflow\n\nQuestion:\nBoth of them pretty much do the same thing. Identify that the method is hot and compile it instead of interpreting. With OSR, you just move to the compiled version right after it gets compiled, unlike with JIT, where the compiled code gets called when the method is called for the second time. \n\nOther than this, are there any other differences?\n\n========================================\n\nTop Answer:\nYes, that's pretty much it. Just-in-time compilation can improve performance by compiling \"hot spots\" (spots of bytecode that are known / supposed to execute very often) of bytecode to native instructions. On-Stack Replacement complements JIT capabilities by replacing long running interpreted \"hot\" bytecode by it's compiled version when it becomes available. The mentioned On-Stack Replacement article shows a nice example where JIT compilation would not be very useful without OSR.\n\n========================================\n\nComments:\n- This blog is another good resource... azulsystems.com/blog/cliff/&hellip;\n- working link: web.archive.org/web/20140126120326/http://www.azulsystems.co&zwnj;&#8203;m/&hellip;\n- Unfortunately, article link doesn't work anymore. Here is another post on OSR: xmlandmore.blogspot.com/2012/06/&hellip;\n- I've updated the dead link with a snapshot from the web archive. Do note that this article is from 2012 :)","metadata":{"transformedAt":"2026-08-18T18:32:17.748Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":380}}50{"id":"stack-52113522","source":"stackoverflow","questionId":52113522,"title":"Why don't C++ compilers do better constant folding?","tags":["c++","compiler-construction","eigen","automatic-differentiation","ceres-solver"],"text":"Title: Why don't C++ compilers do better constant folding?\nTags: c++, compiler-construction, eigen, automatic-differentiation, ceres-solver\nSource: Stack Overflow\n\nQuestion:\nI'm investigating ways to speed up a large section of C++ code, which has automatic derivatives for computing jacobians. This involves doing some amount of work in the actual residuals, but the majority of the work (based on profiled execution time) is in calculating the jacobians. \n\nThis surprised me, since most of the jacobians are propagated forward from 0s and 1s, so the amount of work should be 2-4x the function, not 10-12x. In order to model what a large amount of the jacobian work is like, I made a super minimal example with just a dot product (instead of sin, cos, sqrt and more that would be in a real situation) that the compiler should be able to optimize to a single return value:\n\n```\n#include \n#include \n\nusing Array12d = Eigen::Matrix;\n\ndouble testReturnFirstDot(const Array12d& b)\n{\n Array12d a;\n a.array() = 0.;\n a(0) = 1.;\n return a.dot(b);\n}\n```\n\nWhich should be the same as \n\n```\ndouble testReturnFirst(const Array12d& b)\n{\n return b(0);\n}\n```\n\nI was disappointed to find that, without fast-math enabled, neither GCC 8.2, Clang 6 or MSVC 19 were able to make any optimizations at all over the naive dot-product with a matrix full of 0s. Even with fast-math (https://godbolt.org/z/GvPXFy) the optimizations are very poor in GCC and Clang (still involve multiplications and additions), and MSVC doesn't do any optimizations at all.\n\nI don't have a background in compilers, but is there a reason for this? I'm fairly sure that in a large proportion of scientific computations being able to do better constant propagation/folding would make more optimizations apparent, even if the constant-fold itself didn't result in a speedup.\n\nWhile I'm interested in explanations for why this isn't done on the compiler side, I'm also interested for what I can do on a practical side to make my own code faster when facing these kinds of patterns.\n\n========================================\n\nTop Answer:\nI was disappointed to find that, without fast-math enabled, neither GCC 8.2, Clang 6 or MSVC 19 were able to make any optimizations at all over the naive dot-product with a matrix full of 0s.\n\nThey have no other choice unfortunately. Since IEEE floats have signed zeros, adding `0.0` is not an identity operation:\n\n```\n-0.0 + 0.0 = 0.0 // Not -0.0!\n```\n\nSimilarly, multiplying by zero does not always yield zero:\n\n```\n0.0 * Infinity = NaN // Not 0.0!\n```\n\nSo the compilers simply cannot perform these constant folds in the dot product while retaining IEEE float compliance - for all they know, your input might contain signed zeros and/or infinities.\n\nYou will have to use `-ffast-math` to get these folds, but that may have undesired consequences. You can get more fine-grained control with specific flags (from http://gcc.gnu.org/wiki/FloatingPointMath). According to the above explanation, adding the following two flags should allow the constant folding:\n\n`-ffinite-math-only`, `-fno-signed-zeros`\n\nIndeed, you get the same assembly as with `-ffast-math` this way: https://godbolt.org/z/vGULLA. You only give up the signed zeros (probably irrelevant), NaNs and the infinities. Presumably, if you were to still produce them in your code, you would get undefined behavior, so weigh your options.\n\nAs for why your example is not optimized better even with `-ffast-math`: That is on Eigen. Presumably they have vectorization on their matrix operations, which are much harder for compilers to see through. A simple loop is properly optimized with these options: https://godbolt.org/z/OppEhY\n\n========================================\n\nCode:\n```text\n#include <Eigen/Core>\n#include <Eigen/Geometry>\n\nusing Array12d = Eigen::Matrix<double,12,1>;\n\ndouble testReturnFirstDot(const Array12d& b)\n{\n    Array12d a;\n    a.array() = 0.;\n    a(0) = 1.;\n    return a.dot(b);\n}\n```\n\n```text\ndouble testReturnFirst(const Array12d& b)\n{\n    return b(0);\n}\n```\n\n```text\nstatic const int DontVectorize = 0x80000000;\n\nnamespace Eigen {\nnamespace internal {\n\ntemplate<typename _Scalar, int _Rows, int _Cols, int _MaxRows, int _MaxCols>\nstruct traits<Matrix<_Scalar, _Rows, _Cols, DontVectorize, _MaxRows, _MaxCols> >\n: traits<Matrix<_Scalar, _Rows, _Cols> >\n{\n  typedef traits<Matrix<_Scalar, _Rows, _Cols> > Base;\n  enum {\n    EvaluatorFlags = Base::EvaluatorFlags & ~PacketAccessBit\n  };\n};\n\n}\n}\n\nusing ArrayS12d = Eigen::Matrix<double,12,1,DontVectorize>;\n```\n\n```text\n-ffast-math\n```\n\n```text\nEIGEN_DONT_VECTORIZE\n```\n\n```text\nDontVectorize\n```\n\n```text\nMatrix\n```\n\n```text\ntraits<>\n```\n\n```text\nMatrix\n```\n\n```text\n#include <array>\n#include <cstddef>\nconstexpr std::size_t n = 12;\nusing Array = std::array<double, n>;\n```\n\n```text\n<utility>\ntemplate<std::size_t... is>\ndouble dot(const Array& x, const Array& y, std::index_sequence<is...>)\n{\n    return ((x[is] * y[is]) + ...);\n}\n\ndouble dot(const Array& x, const Array& y)\n{\n    return dot(x, y, std::make_index_sequence<n>{});\n}\n```\n\n```text\ndouble test(const Array& b)\n{\n    const Array a{1};    // = {1, 0, ...}\n    return dot(a, b);\n}\n```\n\n```text\ntest(std::array<double, 12ul> const&):\n  movsd xmm0, QWORD PTR [rdi]\n  ret\n```\n\n```text\ntest(std::array<double, 12ul> const&): # @test(std::array<double, 12ul> const&)\n  movsd xmm0, qword ptr [rdi] # xmm0 = mem[0],zero\n  ret\n```\n\n```text\ndouble test(const Array& b)\n{\n    const Array a{1, 1};    // = {1, 1, 0...}\n    return dot(a, b);\n}\n```\n\n```text\ntest(std::array<double, 12ul> const&):\n  movsd xmm0, QWORD PTR [rdi]\n  addsd xmm0, QWORD PTR [rdi+8]\n  ret\n```\n\n```text\ndot\n```\n\n```text\n-ffast-math\n```\n\n```text\nfor (std::size_t i = 0; i < n; ++i) ...\n```\n\n```text\n-0.0 + 0.0 = 0.0 // Not -0.0!\n```\n\n```text\n0.0 * Infinity = NaN // Not 0.0!\n```\n\n```text\n0.0\n```\n\n```text\n-ffast-math\n```\n\n```text\n-ffinite-math-only\n```\n\n```text\n-fno-signed-zeros\n```\n\n```text\n-ffast-math\n```\n\n```text\n-ffast-math\n```\n\n========================================\n\nComments:\n- Other than `b` you don't have any constants. Locals and code to initialise them which may have visible side-effect. Also with floating point the compiler would have to full emulate the production environment to guarantee the same results.\n- Floating point numbers are not real numbers, they have rigorous correctness requirements which are violated by obvious optimisations. E.g. `(1.0 &#47; 3.0) * 3.0` != `(1.0 * 3.0)&#47;3.0` because rounding behaviour is fully specified, so you cannot simply cancel the 3.\n- The answer depends on the implementation of `dot`. Probably, it is not just a `for` loop with accumulation, but involves rescaling. No wonder that compilers can't optimize it.\n- So as @Evgeny says, it depends on the implementation, to optimise it the compiler would have to prove that the implementation of dot isn't going to introduce rounding, because to comply with the standard the rounding which mathematically you might think of as an \"error\" is in fact required so that calculations are reproducible and expressions have an unambiguous meaning.\n- The point of `-ffast-math` is to say \"it's not necessary to comply with the standard\". MSVC equivalent of fast-math is `&#47;fp:fast` you may find that it does some optimisation if you specify that.\n- With floating point, if you are sure of your expected results, you should create a specific condition returning a predefined result. Compiler won't optimize it for you.\n- To give a concrete example `((1.0 * 1343.0 + 100)&#47;1343.0 - 100&#47;1343.0) == 9.99999999999999890` **not** `1.0`\n- The problem is with `Array12d::dot()`. You'll have to provide code for that to get help optimizing it.\n- @Ben Am I missing something here? The question has not been edited and already talks about how using `-ffast-math` still yields unsatisfactory results. Why are you elaborating so much on it?\n- Might some `constexpr` functions help?\n- Once you added `-ffast-math` the remaining \"problem\" is explicit vectorization, see my answer.\n- You don't say what compiler switches you are using (except fastmath). Can we presume you are using `-O3` and the MS equivalent?\n- You can see the options in the godbolt. -O3 for gcc/clang, /Ox for MSVC.\n- @Ben You have to be careful with that assumption about rounding. As I understand it the compilers are allowed to use any bit precision that they like for constants. If they assume infinite precision they can use rational arithmetic and *will constant fold*. Many people are bitten by math that works different under different precisions. They are wrong of course: more precision is always better.\n- @ggael +1 vectorization is the real big gain here on modern cpus.\n- The question is about constant folding. Is analyzing array content a part of constant folding? That would surprise me. Here we have a field that could be anything, this is not a constant value known at compile time.\n- Only clang optimizes a `for` loop, gcc doesn't do it.\n- Why can't the compiler optimize the remaining vector instructions? Is it a QoI issue or is there a technical reason?\n- @Rakete1111 Presumably because nobody sat down to write detailed enough rules/model by which the compiler would track constant propagation through vector instructions. Some rules (such as multiplying by or adding 0.0) have evidently been included already, but it's probably difficult to make them as encompassing as the scalar ones.\n- That would be technically possible by \"un-vectorizing\" the code, but this would go against what the user explicitly asked, so this is debatable whether its reasonable or not.\n- You are asking an awful lot of the compiler...for it to do what you want would require it to really develop some machine insight into the particulars of the problem. It's not impossible, but not the kind of think compiler writers focus on. To us humans, it is obvious that a dot product in N dimensions where all but the first element of one vector is zeros is a trivial multiplication, but that is not the compiler's focus. Further, as noted above, to maintain consistency floating point must do what it does. Python, for one, uses many 30 year-old Fortran libraries for this reason.\n- Can you give any insights into why is MSVC not able to optimize this code ? perhaps theres a workaround ?\n- I mean, since MSVC is able to constant fold doubles, etc.","metadata":{"transformedAt":"2026-08-18T18:32:17.748Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":27,"totalLines":263,"estimatedTokens":2589}}51{"id":"stack-8696229","source":"stackoverflow","questionId":8696229,"title":"How to create a C compiler for custom CPU?","tags":["c","compiler-construction","customization"],"text":"Title: How to create a C compiler for custom CPU?\nTags: c, compiler-construction, customization\nSource: Stack Overflow\n\nQuestion:\nWhat would be the easiest way to create a C compiler for a custom CPU, assuming of course I already have an assembler for it?\n\nSince a C compiler generates assembly, is there some way to just define standard bits and pieces of assembly code for the various C idioms, rebuild the compiler, and thereby obtain a cross compiler for the target hardware?\n\nPreferably the compiler itself would be written in C, and build as a native executable for either Linux or Windows.\n\n**Please note:** I am *not* asking how to write the compiler itself. I did take that course in college, I know about general compiler-compilers, etc. In this situation, I'd just like to configure some existing framework if at all possible. I don't want to modify the language, I just want to be able to target an arbitrary architecture. If the answer turns out to be \"it doesn't work that way\", *that* information will be useful to myself and anyone else who might make similar assumptions.\n\n========================================\n\nTop Answer:\nThere's the concept of a cross-compiler, ie., one that runs on one architecture, but targets a different one. You can see how GCC does it (for example) and add a new architecture to the set, if that's the compiler you want to extend.\n\nEdit: I just spotted a question a few years ago on a GCC mailing list on how to add a new target and someone pointed to this\n\n========================================\n\nComments:\n- There is no *fundamental* requirement that the compiler produce assembler: that's just a common and convenient practice.\n- @dmckee - let's assume it's a requirement in this case. I know there are those in this community that would have an answer for this. Please note, the answer for what I am asking is *not* \"go take a compiler writing course\".\n- PS: Assuming you've got this custom CPU that's so brand-new you don't even have a compiler for it. Where's the *OS* going to come from??? What do you mean \"native executable\"? Native to *WHAT*?!?\n- @JustJeff: What you're asking is \"how to write my own backend for GCC?\" (substitute \"clang\", etc. for \"GCC\" if appropriate). This is still non-trivial, and probably the best place to start is with the relevant documentation (e.g. llvm.org/releases/2.3/docs/WritingAnLLVMBackend.html). (I'll quite happily admit that I'm out of my depth at this point!)\n- @paulsm4 - yes, assume it's new. assume no OS. 'native executable' was used to mean 'a binary that executes on the cpu'\n- Yes. How do you add a new architecture. Please elaborate?\n- Edited the answer with a link. It's a bit big document, but I guess it's the way to go...\n- Have you even *looked* at the GCC source to see how it does it? PS: *do* look at the gcc source, and *also* look at LLVM. I think you'll find both informative...\n- @paulsm4 - you know, this comment is a better answer than what you put as an actual answer.\n- LLVM is very complex and totally undocumented. Don't even touch it. It wasted 1 month of my life.\n- I am the same person who left the previous comment. I am working on LLVM for one year and half already. I could develop my own backend and assemble. Yup it takes one year to grasp the LLVM infrastructure, but I guess it worth it.\n- @Ehsan Do you think you could write some documentation? :-D\n- @wizzwizz4 I might do it after I finish my PhD dissertation.\n- For a more complete and updated documentation, check out llvm.org/docs/WritingAnLLVMBackend.html\n- Unfortunately, the link is dead.\n- Interesting option. Usually people don't even think of free compilers outside of the gcc/clang/llvm family. Can you elaborate a little for the OP on how the retargeting process was done? (e.g., is there a clearly defined intermediate \"generic machine code\" stage from which you simply write a more or less direct translator into the real CPU instruction set?)\n- Yes, the VBCC compiler front-end outputs a generic machine code of sorts. It's calling back-end function (the ones you have to write) to translate those instructions into your target assembly instructions. The compiler is fairly powerful and offers good optimization. It takes very little time to get a functioning (although not very optimizing) backend going. If you're goal is to achieve best code possible, then it get's a bit harder.","metadata":{"transformedAt":"2026-08-18T18:32:17.748Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":40,"estimatedTokens":1096}}52{"id":"stack-2135788","source":"stackoverflow","questionId":2135788,"title":"What do C and Assembler actually compile to?","tags":["c++","c","compiler-construction","linker","assembly"],"text":"Title: What do C and Assembler actually compile to?\nTags: c++, c, compiler-construction, linker, assembly\nSource: Stack Overflow\n\nQuestion:\nSo I found out that C(++) programs actually don't compile to plain \"binary\" (I may have gotten some things wrong here, in that case I'm sorry :D) but to a range of things (symbol table, os-related stuff,...) but...\n\nDoes assembler \"compile\" to pure binary? That means no extra stuff besides resources like predefined strings, etc.\n\nIf C compiles to something else than plain binary, how can that small assembler bootloader just copy the instructions from the HDD to memory and execute them? I mean if the OS kernel, which is probably written in C, compiles to something different than plain binary - how does the bootloader handle it?\n\nedit: I know that assembler doesn't \"compile\" because it only has your machine's instruction set - I didn't find a good word for what assembler \"assembles\" to. If you have one, leave it here as comment and I'll change it.\n\n========================================\n\nTop Answer:\nLet's take a C program.\n\nWhen you run `gcc`, `clang`, or 'cl' on the c program, it will go through these stages:\n\n- Preprocessor (#include, #ifdef, trigraph analysis, encoding translations, comment management, macros...) including lexing into preprocessor tokens and eventually resulting in flat text for input to the compiler proper.\n\n- Lexical analysis (producing tokens and lexical errors).\n\n- Syntactical analysis (producing a parse tree and syntactical errors).\n\n- Semantic analysis (producing a symbol table, scoping information and scoping/typing errors) Also data-flow, transforming the program logic into an \"intermediate representation\" that the optimizer can work with. (Often an SSA). clang/LLVM uses LLVM-IR, gcc uses GIMPLE then RTL.\n\n- Optimization of the program logic, including constant propagation, inlining, hoisting invariants out of loops, auto-vectorization, and many many other things. (Most of the code for a widely-used modern compiler is optimization passes.) Transforming through intermediate representations is just part of how some compilers work, making it impossible / meaningless to \"disable all optimizations\"\n\n- Outputing into assembly source (or another intermediate format like .NET IL bytecode)\n\n- Assembling of the assembly into some binary object format.\n\n- Linking of the assembly into whatever static libraries are needed, as well as relocating it if needed.\n\n- Output of final executable in elf, PE/coff, MachO64, or whatever other format\n\nIn practice, some of these steps may be done at the same time, but this is the logical order. Most compilers have options to stop after any given step (e.g. preprocess or asm), including dumping internal representation between optimization passes for open-source compilers like GCC. (`-ftree-dump-...`)\n\nNote that there's a 'container' of elf or coff format around the actual executable binary, unless it's a DOS `.com` executable\n\nYou will find that a book on compilers(I recommend the Dragon book, the standard introductory book in the field) will have *all* the information you need and more.\n\nAs Marco commented, linking and loading is a large area and the Dragon book more or less stops at the output of the executable binary. To actually go from there to running on an operating system is a decently complex process, which Levine in Linkers and Loaders covers.\n\nI've wiki'd this answer to let people tweak any errors/add information.\n\n========================================\n\nCode:\n```text\nint main () { printf(\"Hello, world\\n\"); }\n```\n\n```text\ntext\n```\n\n```text\nmain\n```\n\n```text\nmain\n```\n\n```text\nrodata\n```\n\n```text\n\"Hello, world\\n\"\n```\n\n```text\nprintf\n```\n\n```text\nhello.o\n```\n\n```text\nnm\n```\n\n```text\nobjdump\n```\n\n```text\ngcc\n```\n\n```text\nclang\n```\n\n```text\n-ftree-dump-...\n```\n\n```text\n.com\n```\n\n```text\n1:*a 0100\n2:* mov AH,07\n3:* int 21\n4:* cmp AL,00\n5:* jnz 010c\n6:* mov AH,07\n7:* int 21\n8:* mov AH,4C\n9:* int 21\n10:*\n11:*r CX\n12:*10\n13:*n respond.com\n14:*w\n15:*q\n```\n\n```text\nas foo.s\n```\n\n```text\na.out\n```\n\n```text\n.Org 100h\n```\n\n========================================\n\nComments:\n- The bootloader is just machine code without the binary headers and other stuff that the OS uses when it loads a binary in memory. Assembler, C, and C++ are all compiled (most of the time) down to binaries. Simply put, they can get \"packaged\" differently.\n- @lamas, Where did you find that out? My book C: The Complete Reference, 4th edition, by Herbert Schildt, which I just bought, says it compiles to binary. The ANSI standard for C should clarify the matter. Unfortunately, I do not have a copy it.\n- @Geoffey Schildt's book has strong claims to be the worst technical book ever written - it is riddled with errors and untruths.\n- Before progressing further as a developer, you should have clear understanding of the terms: *machine code*, *binary code*, *assembly language*, *executable code* and *microcode*. Many people treat these as the same meaning, but they are different, especially with Embedded Systems Programming.\n- Thanks for your answers regarding bootloaders, but they should have been an example for my question only\n- @Neil you should really substantiate claims like that with a link.\n- See accu.org for a background why Schildt's book is not worth your time or money.\n- Note that on PCs, the bootloader code is a different kind of machinecode (16-bit) than most programs and kernel (32/64-bit)\n- Note also that gcc doesn't compile to machinecode. It compiles to assembler. AS then translates the assembler to machinecode. However most compilers do directly output machinecode\n- @anon, why it is that such bad textbooks always sell so well?? I mean the reviews are pretty bad but the number of sales of his textbooks are one of the best??\n- Related, possible duplicate: Does a compiler always produce an assembly code?\n- That's true in most cases. Some assembly languages have psuedo-operations which are sorta macros.\n- Assembler !== binary. In assembler you can use symbolic names, labels and so on, which has no direct representation in binary, they need to be replaced by actual numbers. If you add some code before label, then that label should be moved to some other address. Assembler is simple programming language, which translates directly to binary, but is not binary itself.\n- Almost directly. The same opcode compiles to different binary depending on details such as how the data is addressed. Likewise, even an assembler will sneak in prefix operators as needed. So while there is a very, very close relationship, they're not quite 1:1.\n- I think you are a bit confused about registers. You are correct that there isn a on-to-one corespondence between an assembler opcode and a machine code instruction, however.\n- @Paul Nathan: Good point. Macro-assemblers are a step closer to compilers.\n- @Neil: You're right to point out that registers, by definition, don't have addresses, as they're not in memory. However, on architectures with a large number of general-purpose registers (many RISC CPU's), we can be forgiven for thinking of the register number as an address \"of sorts\".\n- It depends a bit on what assembler you use, though most assemblers these days are macro assemblers, offering a bit more.\n- @Neil, that would be between an assembly mnemonic and a cpu opcode, or machine instruction.\n- Assembler (a human readable macro language which is translated to machine code) != Assembly (the binary file generated by common language infrastructure compilers, where each operation has a binary string). I think you may have misunderstanding.\n- Every \"fact\" in this answer is wrong.\n- Assembler is as optimized as you make it. C++, managed or otherwise, normally compiles into complex executables with headers and segments, not plain binary. The BIOS and the early parts of the OS are plain binary.\n- Neil - why not correct it then?\n- \"microcode\" is a completely misleading word to use when you're referring to \"intermediate code\" -- and intermediate code is actually considered \"binary\" (probably not *native* binary).\n- Plain binary? Everything stored on a hard drive is binary, that statement is meaningless.\n- \"Assembler compiles to pure binary, but, as strange as it gets, it is less optimized than C(++)\" What is that even supposed to mean? There are misleading issues with this accepted answer.\n- jsoverson: In this context, \"plain binary\" refers to opcodes without the headers and segments.\n- ThePosey: My guess is that they're trying to say that assemblers don't optimize code, whereas compilers typically do (when not in debug mode). Not claiming their answer was clear or correct, just that they might have been thinking of the right thing.\n- True, but not an answer to the asked question.\n- I was aiming for the 'does machine code compile to binary' side of the question. Tried to paint the relationship, rather than just saying 'not really'.\n- The thing to remember is that the CIL is contained inside a COFF executable.\n- Hmm, the Dragon book is mostly about parsing. I'd recommend \"Linkers and Loaders\" by Levine, iecc.com/linker which is also available on the web.\n- Linkers and loaders is also a good book.\n- Actually, in the \"logical\" order, lexical analysis occurs before preprocessing, because the preprocessor operates on a stream of tokens. That's how it is defined in the C standard, and that is also how it happens in modern versions of gcc (when the preprocessor was rewritten and turned into a lexing library).\n- Thomas: Interesting! I am out of date\n- C standard, 5.1.1.2 suggests that traditional lexing is logically separate from preprocessor lexing.\n- Which was really interesting when we had 100kwords memory, but is it nowadays still an advantage or more an artefact? A compilation granularity that would utilize available memory better (e.g. to avoid repeated header reparsing, relative slow disk I/O or even just binary startup time) would be more in line with modern requirements?\n- debug.exe *is* an assembler. (A bad one by modern standards, e.g. no labels so you have to calculate branch target addresses by hand.) Also, raw machine code is *not* an object file; if it was literally raw (like `nasm -f bin` output, e.g. a .com file), there's no section metadata, or any other metadata. I made an edit to that paragraph.\n- @PeterCordes True, but it would be unfair to compare it to modern standards as this was part of the MSDOS install base back in the 80's and 90s, this was long before Linux / open source, appeared on the scene which opened up the corridors of the general awareness of standards. :)\n- It's 100% fair *if* people are proposing still using it today! Apparently some poor unfortunate folks get homework that requires them to write 16-bit x86 DOS code for debug.exe, leading to questions on SO about it. That's what I meant by saying \"by modern standards\". Also, it was better than nothing at the time, but even then I assume you'd want TASM, MASM, or AS86 if you could get them, for anything more than small toy stuff.\n- Most C compilers compile directly to relocatable machine code. It is faster to skip the slow textual step. Some (like 16-bit compilers capable of .COM files) can generate non-relocatable code directly. One could argue though that in directly machinecode generating compilers, the assembler is a relative separate standing part.\n- Relocatable code is not a requirement of C, and many platforms don't use it.\n- Is there any script for your course available online?\n- @Lothar my course is online at cs.tufts.edu/comp/40. For past years, see my home page. For obvious reasons the answers are not online.\n- As discussed under your answer on a duplicate (Do programming language compilers first translate to assembly or directly to machine code?) mainstream C++ compilers with large development teams like MSVC, ICC, and clang/LLVM (but still *not* GCC), all output relocatable `.o` / `.obj` files directly by default, with machine-code generation and object file format handling as a library (in LLVM's case) not a separate program. See also Does a compiler always produce an assembly code?","metadata":{"transformedAt":"2026-08-18T18:32:17.748Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":18,"totalLines":185,"estimatedTokens":3034}}53{"id":"stack-807244","source":"stackoverflow","questionId":807244,"title":"C compiler asserts - how to implement?","tags":["c","compiler-construction","assertions"],"text":"Title: C compiler asserts - how to implement?\nTags: c, compiler-construction, assertions\nSource: Stack Overflow\n\nQuestion:\nI'd like to implement an \"assert\" that prevents compilation, rather than failing at runtime, in the error case.\n\nI currently have one defined like this, which works great, but which increases the size of the binaries.\n\n```\n#define MY_COMPILER_ASSERT(EXPRESSION) switch (0) {case 0: case (EXPRESSION):;}\n```\n\nSample code (which fails to compile).\n\n```\n#define DEFINE_A 1\n#define DEFINE_B 1\nMY_COMPILER_ASSERT(DEFINE_A == DEFINE_B);\n```\n\nHow can I implement this so that it does not generate any code (in order to minimize the size of the binaries generated)?\n\n========================================\n\nTop Answer:\nThe following `COMPILER_VERIFY(exp)` macro works fairly well.\n\n// combine arguments (after expanding arguments)\n#define GLUE(a,b) __GLUE(a,b)\n#define __GLUE(a,b) a ## b\n\n#define CVERIFY(expr, msg) typedef char GLUE (compiler_verify_, msg) [(expr) ? (+1) : (-1)]\n\n#define COMPILER_VERIFY(exp) CVERIFY (exp, __LINE__)\n\nIt works for both C and C++ and can be used anywhere a typedef would be allowed. If the expression is true, it generates a typedef for an array of 1 char (which is harmless). If the expression is false, it generates a typedef for an array of -1 chars, which will generally result in an error message. The expression given as an arugment can be anything that evaluates to a compile-time constant (so expressions involving sizeof() work fine). This makes it much more flexible than\n\n#if (expr)\n#error\n#endif\n\nwhere you are restricted to expressions that can be evaluated by the preprocessor.\n\n========================================\n\nCode:\n```text\n#define MY_COMPILER_ASSERT(EXPRESSION) switch (0) {case 0: case (EXPRESSION):;}\n```\n\n```text\n#define DEFINE_A 1\n#define DEFINE_B 1\nMY_COMPILER_ASSERT(DEFINE_A == DEFINE_B);\n```\n\n```text\n/** A compile time assertion check.\n *\n *  Validate at compile time that the predicate is true without\n *  generating code. This can be used at any point in a source file\n *  where typedef is legal.\n *\n *  On success, compilation proceeds normally.\n *\n *  On failure, attempts to typedef an array type of negative size. The\n *  offending line will look like\n *      typedef assertion_failed_file_h_42[-1]\n *  where file is the content of the second parameter which should\n *  typically be related in some obvious way to the containing file\n *  name, 42 is the line number in the file on which the assertion\n *  appears, and -1 is the result of a calculation based on the\n *  predicate failing.\n *\n *  \\param predicate The predicate to test. It must evaluate to\n *  something that can be coerced to a normal C boolean.\n *\n *  \\param file A sequence of legal identifier characters that should\n *  uniquely identify the source file in which this condition appears.\n */\n#define CASSERT(predicate, file) _impl_CASSERT_LINE(predicate,__LINE__,file)\n\n#define _impl_PASTE(a,b) a##b\n#define _impl_CASSERT_LINE(predicate, line, file) \\\n    typedef char _impl_PASTE(assertion_failed_##file##_,line)[2*!!(predicate)-1];\n```\n\n```text\n#include \"CAssert.h\"\n...\nstruct foo { \n    ...  /* 76 bytes of members */\n};\nCASSERT(sizeof(struct foo) == 76, demo_c);\n```\n\n```text\n$ gcc -c demo.c\ndemo.c:32: error: size of array `assertion_failed_demo_c_32' is negative\n$\n```\n\n```text\nassert()\n```\n\n```text\ntypedef\n```\n\n```text\n__LINE__\n```\n\n```text\n__FILE__\n```\n\n```text\n#ifdef DEBUG\n#define MY_COMPILER_ASSERT(EXPRESSION)   switch (0) {case 0: case (EXPRESSION):;}\n#else\n#define MY_COMPILER_ASSERT(EXPRESSION)\n#endif\n```\n\n```text\n#ifdef DEBUG\n#error Please don't compile now\n#endif\n```\n\n```text\n#define MY_COMPILER_ASSERT(EXPRESSION) char x[(EXPRESSION)];\n```\n\n```text\nstatic asserts\n```\n\n```text\nchar x[1];\n```\n\n```text\nchar x[0];\n```\n\n```text\n#define COMPILER_ASSERT(expr)  enum { ARG_JOIN(CompilerAssertAtLine, __LINE__) = sizeof( char[(expr) ? +1 : -1] ) }\n```\n\n```text\n// combine arguments (after expanding arguments)\n#define GLUE(a,b) __GLUE(a,b)\n#define __GLUE(a,b) a ## b\n\n#define CVERIFY(expr, msg) typedef char GLUE (compiler_verify_, msg) [(expr) ? (+1) : (-1)]\n\n#define COMPILER_VERIFY(exp) CVERIFY (exp, __LINE__)\n```\n\n```text\n#if (expr)\n#error\n#endif\n```\n\n```text\nCOMPILER_VERIFY(exp)\n```\n\n```text\n#include \"myfile.hpp\"\n\nstatic_assert(sizeof(MyClass) == 16, \"MyClass is not 16 bytes!\")\n\nvoid doStuff(MyClass object) { }\n```\n\n```text\nstatic_assert(exp, message)\n```\n\n```text\n#define STATIC_ASSERT(expr, msg)   \\\ntypedef char ______Assertion_Failed_____##msg[1];  __unused \\\ntypedef char ______Assertion_Failed_____##msg[(expr)?1:2] __unused\n```\n\n```text\nunsigned char testvar;\n STATIC_ASSERT(sizeof(testvar) >= 8, testvar_is_too_small);\n```\n\n```text\nconflicting types for '______Assertion_Failed_____testvar_is_too_small'\n```\n\n```c\nstatic_assert(2 + 2 == 4, \"2+2 isn't 4\"); // OK\nstatic_assert(2 + 2 == 5, \"2+2 isn't 4\"); // Compile-time error\n```\n\n```text\ncompile_assert(idx < buf_len, \"index in range\");\n```\n\n```text\n#define COMPILE_TIME_ASSERT(expr) {typedef char COMP_TIME_ASSERT[(expr) ? 1 : -1];}\n```\n\n```text\n_Static_assert((expr), \"message\");\n```\n\n```text\nstatic_assert((expr), \"optional message\");\n```\n\n```text\n{ }\n```\n\n```text\n{ }\n```\n\n```text\n[!!(expr)]\n```\n\n```text\nstatic_assert\n```\n\n```text\nassert.h\n```\n\n```text\nstatic_assert\n```\n\n```text\n_Static_assert\n```\n\n========================================\n\nComments:\n- I really don't think it's possible to create a static assert in plain C, would love to know though!\n- Duplicate with several good answers: stackoverflow.com/questions/174356/&hellip;\n- Since this question is relatively old: `_Static_assert` and its associated macro `static_assert` are standardized as of C11. This is now built-in to the language.\n- Surely the optimizer can throw away empty switches.\n- It's possible to create a compile time assert in C and C++, this example does it, and it really does stop the build at compile time with the file and line number github.com/jonnygrant/compile_assert\n- It is useful because anything that can be verified at compile time is something that didn't require a test case at run time. When building a portable protocol implementation it is useful to validate assumptions about structure size and layout. Since sizes and offsets are known at compile time, testing them then is preferred. Also, implementing a compile-time assert without code generation means that there is no reason to take it out of release builds. At worst it clutters the symbol table with orphan type names.\n- Unfortunately this aborts at the preprocessor level, so it's unable to handle things like `assert(sizeof(long) == sizeof(void *))`.\n- This isn't even valid C since there is an unterminated character constant\n- But in C89 that would break compiling no matter what since variables can only be declared at the top of the scope. Maybe wrapping it in curly brackets might fix it? Also, zero-sized arrays are only forbidden in ISO C I think. You would also get a slew of warnings about multiple declarations and unused variables if not in its own scope.\n- `#define MY_COMPILER_ASSERT(EXPRESSION) do {char x[(EXPRESSION)?1:-1];} while (0)` would be better: inside braces, so declaration is legal and scoped, sizes 1 and -1 are clearly valid/invalid on all C versions, and forces you to `MY_COMPILER_ASSERT(...);` with a trailing semicolon, for visual consistency with all other function-like things in C.\n- Declaring a typedef is preferable to delaring an unused variable. An unused typedef is harmless, but an unused variable may itself generate a warning on many compilers.\n- I like this but it causes a warning for an unused declared variable: main.c:22:47: warning: typedef ‘compiler_verify_39’ locally defined but not used [-Wunused-local-typedefs]. Since my project treats warnings as errors I can not use this.\n- Things have moved on in the decade since I posted this answer. If your compiler supports C11, you can use the _Static_assert that is now part of the language.\n- @Alex, this warning can be silenced with `(void)(sizeof(TYPEDEF_NAME));`. You might need an extra layer of dereference to pass GLUE as a parameter to the CVERIFY() macro, so that you have a TYPEDEF_NAME to work with. And `(void)ENTITY;` is a general way of dismissing 'unused' warnings.\n- If you don't care about portability, in GCC __COUNTER__ can be used to give a unique identifier to paste onto the typedef name. It was added fairly recently (4.3)\n- I've been amazed that the C preprocessor has never had something like **COUNTER**. Macro assemblers have had similar constructs as long as there have been macro assemblers, not to mention the ability for one macro to define another which can be used to build any kind of unique symbol one might need. Unfortunately for me, my embedded systems projects are generally stuck with gcc 3.4.5 or so, if not some proprietary compiler that (mostly) complies with C89.\n- @RBerteig Hello, I would like to use this code for an Open Source project. What would you licence it under?\n- @HannesLandeholm If I were publishing it myself, I would normally apply the MIT license. Published as it is here without any further discussion, it is fair to assume that the general claim that user content is licensed as CC-BY-SA applies. To that end, you could simply add \"CC-BY-SA 3.0 from stackoverflow.com/a/809465/68204\" to the comment block. Personally, I prefer not to impose the \" alike\" restriction on code, but attribution and a link here is always appropriate.\n- Definitely quite cool. glib does the same with G_STATIC_ASSERT()\n- This approach now triggers GCC warnings about unused typedefs, unfrotunately.\n- The question is about C not C++.\n- C11 provides the similarly named `_Static_assert` (see stackoverflow.com/a/7287341/446106). Additionally, `assert.h` contains a `static_assert` macro that maps to it (en.cppreference.com/w/c/error/static_assert)\n- This compiler assert does not work if added in the middle of a code block since C does not allow a new declaration after executable statements.\n- @BitsBitsBits You mean C versions before C99?\n- This would be problematic if the `msg` will be repeated in the same file. Could be improved with some `__LINE__` incorporated into the type name.\n- With C23, `static_assert(assertion);` with no following string is OK too.\n- What does this answer add that wasn't already said in previous answers?","metadata":{"transformedAt":"2026-08-18T18:32:17.748Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":36,"totalLines":279,"estimatedTokens":2597}}54{"id":"stack-5130654","source":"stackoverflow","questionId":5130654,"title":"When / How does Linux load shared libraries into address space?","tags":["c","linux","compiler-construction","linker","shared-libraries"],"text":"Title: When / How does Linux load shared libraries into address space?\nTags: c, linux, compiler-construction, linker, shared-libraries\nSource: Stack Overflow\n\nQuestion:\nWhen is the address of shared objects specified in programs? During linking? Loading? If I wanted to find the memory address of the `system` command inside of `libc` inside of my program I could find it easily in `gdb`, but what if I don't want to bring the program into a debugger?\n\nCould this address change from run to run? Are there any other static analysis tool that will allow be to view where libraries or functions will be loaded into this program's memory space when run?\n\nI want this information outside of the program (ie. using utilities like `objdump` to gather information)\n\n========================================\n\nTop Answer:\n\"Go right to the source and ask the horse...\"\n\nDrepper - How To Write Shared Libraries\n\nMust-read documentation for Linux library writers. Explains the mechanics of loading in some detail.\n\n========================================\n\nCode:\n```text\nsystem\n```\n\n```text\nlibc\n```\n\n```text\ngdb\n```\n\n```text\nobjdump\n```\n\n```text\n/lib/ld-linux.so.2 ./your_program your_prog_params\n```\n\n```text\nLD_TRACE_LOADED_OBJECTS=1 /bin/echo\n```\n\n```text\nld.so\n```\n\n```text\nld-linux.so.2\n```\n\n```text\nld-linux.so.*\n```\n\n```text\n.interp\n```\n\n```text\nld.so\n```\n\n```text\nld.so\n```\n\n```text\nld.so\n```\n\n```text\nopen\n```\n\n```text\nmmap\n```\n\n```text\nldd\n```\n\n```text\nLD_TRACE_LOADED_OBJECTS=1\n```\n\n```text\nld.so\n```\n\n```text\nld.so\n```\n\n```text\necho 0 | sudo tee /proc/sys/kernel/randomize_va_space\n```\n\n```text\nsystem\n```\n\n```text\nlibc.so\n```\n\n```text\nnm\n```\n\n```text\nnm -D /lib/libc.so\n```\n\n```text\nobjdump -T /lib/libc.so\n```\n\n```text\nvoid *self = dlopen(NULL, RTLD_NOW);\ndlsym(self, \"system\"); // returns the pointer to the system() function\n```\n\n```text\ndlopen()\n```\n\n```text\nvoid *addr = &system;\n```\n\n```text\nnm\n```\n\n```text\nlibc.so\n```\n\n```text\nsystem\n```\n\n```text\nlibc.so\n```\n\n```text\nlibc.so\n```\n\n```text\nsystem\n```\n\n```text\nlibc.so\n```\n\n```text\nsystem\n```\n\n========================================\n\nComments:\n- and then there's `prelink`, which changes the order considerably.\n- please see edit in OP, but definitely leave this answer as it is helpful for another variation of the potentially vague title. (how you answered it)\n- It's possible that neither will do what OP wants, since `system` is probably resolved to a PLT entry in the main program image that performs the actual jump into the shared library.\n- This is pretty much exactly what I wanted - I'm assuming the best way to determine the offset of system within libc.so is to use nm again with the debugging symbols installed? Or is there an easier / more robust manner to do this.\n- @Ryan, nm needs no debug symbols, it can read a symbol table directly (which is used by `ld.so`).\n- @osgx so then would my original question be correct, subtracting the debugging symbols?\n- wonderful information, thank you. Do you know of any good articles that explain how this process works (generating GOT / PLT tables), or would googling yield sufficient results?\n- If you want to call a `system` with absolute address, you can do it without using a GOT and PLT tables. In my point, the best googling for `ld.so` is codesearch: google.com/&hellip;\n- Yes, I know you can do it without using GOT and PLT, it was mere curiosity on my part! :)\n- may be,... the book on linkers will help? iecc.com/linker/linker10.html Also there is a blog of author of `gold` linker and elf part of binutils ld linker www.airs.com/blog/archives/41\n- pdf of some blogposts from airs.com www.cs.pitt.edu/~abraham/CS0449/slides/Linking.pdf\n- This was very informative! I didn't know that much about ld.so and ldd!\n- This answer is incorrect in that `ld.so` does *not* load the main program, the kernel does. The kernel also doesn't look at any *sections* (which could be stripped completely), it finds the interpreter in `PT_INTERP` *segment*.\n- @EmployedRussian, Can you post more correct answer? I'm not expert in this and you is.","metadata":{"transformedAt":"2026-08-18T18:32:17.749Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":36,"totalLines":185,"estimatedTokens":1014}}55{"id":"stack-212237","source":"stackoverflow","questionId":212237,"title":"Constants and compiler optimization in C++","tags":["c++","optimization","compiler-construction","compiler-optimization","const-correctness"],"text":"Title: Constants and compiler optimization in C++\nTags: c++, optimization, compiler-construction, compiler-optimization, const-correctness\nSource: Stack Overflow\n\nQuestion:\nI've read all the advice on const-correctness in C++ and that it is important (in part) because it helps the compiler to optimize your code. What I've never seen is a good explanation on how the compiler uses this information to optimize the code, not even the good books go on explaining what happens behind the curtains. \n\nFor example, how does the compiler optimize a method that is declared const vs one that isn't but should be. What happens when you introduce mutable variables? Do they affect these optimizations of const methods?\n\n========================================\n\nTop Answer:\nI think that the const keyword was primarily introduced for compilation checking of the program semantic, not for optimization.\n\nHerb Sutter, in the GotW #81 article, explains very well why the compiler can't optimize anything when passing parameters by const reference, or when declaring const return value. The reason is that the compiler doesn't have any way to be sure that the object referenced won't be changed, even if declared const: one could use a const_cast, or some other code can have a non-const reference on the same object.\n\nHowever, quoting Herb Sutter's article:\n\nThere is [only] one case where saying\n\"const\" can really mean something, and\nthat is when objects are made const at\nthe point they are defined. In that\ncase, the compiler can often\nsuccessfully put such \"really const\"\nobjects into read-only memory[...].\n\nThere is a lot more in this article, so I encourage you reading it: you'll have a better understanding of constant optimization after that.\n\n========================================\n\nCode:\n```text\nvoid someType::somefunc();\n\nvoid MyFunc()\n{\n    someType A(4);   // \n    Fling(A.m_val);\n    A.someFunc();\n    Flong(A.m_val);\n}\n```\n\n```text\nvoid f(Type dont_modify); // or\nvoid f(Type const& dont_modify);\n```\n\n```text\nconst\n```\n\n```text\nconst\n```\n\n```text\nconst\n```\n\n```text\nconst\n```\n\n```text\nclass Test\n{\npublic:\n  Test (int value) : m_value (value)\n  {\n  }\n\n  void SetValue (int value) const\n  {\n    const_cast <Test&>(*this).MySetValue (value);\n  }\n\n  int Value () const\n  {\n    return m_value;\n  }\n\nprivate:\n  void MySetValue (int value)\n  {\n    m_value = value;\n  }\n\n  int\n    m_value;\n};\n\nvoid modify (const Test &test, int value) \n{\n  test.SetValue (value);\n}\n\nvoid main ()\n{\n  const Test\n    test (100);\n\n  cout << test.Value () << endl;\n  modify (test, 50);\n  cout << test.Value () << endl;\n}\n```\n\n```text\n100\n50\n```\n\n```text\nvolatile const\n```\n\n```text\nconst\n```\n\n```text\nconst_cast\n```\n\n========================================\n\nComments:\n- I don't think const helps with optimization at all. A.someFunc() could easily do const_cast(this)->m_val = 42; or any number of other things that would change m_val (e.g. if there is a non-const pointer to A in a global variable, someFunc could change it)\n- The compiler is allowed to assume you won't cast away const; you do so at your own risk. And, as A lives entirely within MyFunc(), the compiler can track if there's a global pointer to it. (In this example, there can't be)\n- I'm not sure this answer (and comment) is correct. As far as I know, it is completely legal to cast away constness and modify an object as long as it is not a const object. In your example, A is not const, so `somefunc` (which is not declared const, btw) is allowed to cast away constness on `this` and modify the current object. However, if A was defined as `const someType A(4)`, then modifying it by casting away constness would yeld undefined behavior. Consequently, I don't think the compiler can optimize anything in the example you gave.\n- It is important to make a distinction between a const object and a const handle (pointer or reference) to an object (which can be const or not). The former can potentially lead to compiler optimizations, will the latter cannot (AFAIK).\n- @JamesCurran \"*The compiler is allowed to assume you won't cast away const*\" Wrong.\n- @curiousguy: Would you like to offer some support of that statement? If the compiler cannot assume that a const object remains const, the keyword become pointless. (per the Standard 7.1.6.1 (4) - casting away constness leads to undefined behavior)\n- @JamesCurran You can't modify a const object, but you can cast away const. If you decide to cast away const, it's your own responsibility to ensure that you won't end up modifying a const object.\n- It would be really interesting to see the assembler produced for the two versions, where someFunce is declared with\\without const.\n- It would be a very recless programmer to cast away constness. What would she be thinking, \"naaah, the documentation doesnt really mean that it's immutable. It doesn't apply to *me*\"?\n- @gnud: Irrelevant - the compiler can't assume the programmer isn't doing it.\n- @Mike F: Extremely relevant, the compiler CAN assume the programmer isn't doing it. The standard states (7.1.6.1/4) that: \"Except that any class member declared mutable can be modified, any attempt to modify a const object during its lifetime results in undefined behavior.\"\n- @NickLewis Of course, but I assume the answer was talking about `const` references/pointers to objects not declared `const` and the ability to cast away `const` on those and thus modify the referent - which *is* defined, and which, along with `mutable` and casting off `const` on a non-`const` instance, is a very valid objection to the oft-repeated but never-cited popular myth that `const` gives some free pass to optimising compilers.\n- Either your build tools need link-time optimization, or your copy constructor (and possibly destructor) need to be visible to the compiler at the call site. Otherwise, the compiler will not be able to optimize out a copy.\n- You don't understand `const`.\n- while I agree that optimisation is frequently touted yet never demonstrated that I've seen, this post isn't evidence in favour of that. you're deliberately bombarding the compiler with demands that it must let you shoot yourself in the foot, and invoking UB in the process, then concluding that `const` is broken. no, your code is broken. `void main` is a fine cherry on the top of this proving that.\n- does it mean it's illegal to remove constness by `const_cast` for anything except primitive types or const objects with mutable member(s)?\n- @AndyT: Yes it is. You are allowed to `const_cast` away the constness only for objects which are not const in the first place.\n- @AlexandreC. no you can `const_cast` away the constness of any object. But attempting to modify it is undefined behaviour. The cast itself is always fine.\n- @Simple: You are technically right. However what is the point of `const_cast`ing if you don't do anything with the object ? :)\n- @AlexandreC. it's useful to call a `const` overload from the non-`const` overload and then cast the constness of the result away.\n- @Simple: Yes, I know the technique. You offer the result for modification though.\n- @AlexandreC.: One can `const_cast` only for having the *potential* of modification, in certain cases, without creating the possibility of performing such modifications right away. For instance you may want a `const_iterator` type to internally store a (const-casted) `iterator`, only for the purpose of being able to return it when a non-const container calls its `erase` method with that `const_iterator` (since that is what the method must return). Any direct use of the const_iterator would shield off the internal non-constness, and not allow the target to be modified.\n- All of the optimization opportunities mentioned in this answer appear to be possible only in case of *compile-time* constants. They seem useless in the case of `const` declared objects whose value cannot be known by the compiler, for instance because their constructor uses non-constant arguments.\n- @MarcvanLeeuwen: My point is that the variables referenced by the inner mutable iterator are not const in the first place. In this case, yes, you can use `const_cast`.\n- Since when is \"I *think* compilers can blah blah\" an answer? Many people *think* `const` provides some kind of benefit to optimisation, and loudly proclaim this all around, yet never seem to present generated code demonstrating this... Despite that, newbies believe them anyway, and the myth balloons evermore. Sigh.\n- @AlexandreC. maybe I want to call an old C function that takes a char* (because const wasn't around when the code was written) and I know that the function doesn't actually modify the passed pointer. Then I could `const_cast` away constness and pass the result into the function.\n- @ Marc van Leeuwen: if the computation \"x=f(y)\" appears in a loop, and \"y\" is invariant to the loop and computing \"f\" is side-effect free, this computation could be hoisted out of the loop. This is an optimization opportunity that is not a compile-time constant, and compilers do this stuff all the time. So it would be a good question whether this hoisting can be done with a const member function \"f\". And I think it can't because the compiler can not always see if \"f\" affects mutable data members.\n- The OP should seriously consider removing the \"accepted\" status of this answer. According to Chandler Carruth (who \"leads the C++, Clang, and LLVM teams at Google\"), there is virtually no optimization that can take place for anything `const`. He mentioned there are maybe \"two cases\" that *can* be optimized, but I don't know what they are. So people who *actually write optimizers for compilers* don't optimize for `const` because it can just be cast away. This is according to what they actually say and do, not according to what we think/wish about it.\n- This is unfortunately not true. The compiler has to assume that const_cast and mutable internals will exist elsewhere, unless it can see all of the code in one pass (header-only code).","metadata":{"transformedAt":"2026-08-18T18:32:17.749Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":157,"estimatedTokens":2499}}56{"id":"stack-794015","source":"stackoverflow","questionId":794015,"title":"What do people mean when they say C++ has \"undecidable grammar\"?","tags":["c++","compiler-construction"],"text":"Title: What do people mean when they say C++ has \"undecidable grammar\"?\nTags: c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nWhat do people mean when they say this? What are the implications for programmers and compilers?\n\n========================================\n\nTop Answer:\nWhat it probably means is that C++ grammar is syntactically ambiguous, that you can write down some code that could mean different things, depending on context. (The grammar is a description of the syntax of a language. It's what determines that `a + b` is an addition operation, involving variables a and b.)\n\nFor example, `foo bar(int(x));`, as written, could be a declaration of a variable called bar, of type foo, with int(x) being an initializer. It could also be a declaration of a function called bar, taking an int, and returning a foo. This is defined within the language, but not as a part of the grammar.\n\nThe grammar of a programming language is important. First, it's a way to understand the language, and, second, it's part of compiling that can be made fast. Therefore, C++ compilers are harder to write and slower to use than if C++ had an unambiguous grammar. Also, it's easier to make certain classes of bugs, although a good compiler will provide enough clues.\n\n========================================\n\nCode:\n```text\na + b\n```\n\n```text\nfoo bar(int(x));\n```\n\n```text\nx * y(z);\n```\n\n```text\nint main() {\n    int x, y(int), z;\n    x * y(z);\n}\n```\n\n```text\nint main() {\n    struct x { x(int) {} } *z;\n    x * y(z);\n}\n```\n\n```text\nint main() {\n    DECLARE struct x { x(int) {} } *z;\n    DECLARE x * y(z);\n}\n```\n\n========================================\n\nComments:\n- which people? where are they saying it?\n- I suspect most people here don't know what \"undecidable\" means in computer science. Check out the Wikipedia article: en.wikipedia.org/wiki/Undecidable_problem\n- Most stuff I can google for about C++ having \"undecidable\" grammar merely states that a statement's meaning depends on previous definitions, i.e. on its context. Wow, how undecidable! This is like saying that a basketball play is \"undecidably\" good or bad because it depends on who's ahead and what the remaining time is.\n- @Daniel, That's not the formal meaning of undecidable. If the compiler always says valid/not valid for a program, then the language (or the subset the compiler actually works on) is decidable. If the compiler could potentially churn away forever and not terminate (or at least until it runs out of memory), then the language is not decidable.\n- @Jay - thanks, I was talking about \"most stuff I can google for\", not about the formal definition :) I appreciate your answer below, it's very clarifying.\n- If you make it 'foo * bar(int(x))' then it can be: (a) an expression, (b) an object declaration or (c) a function declaration.\n- funny, the turing complete template system in c++ is what I consider one of its greatest strengths.\n- This was news to me, but I totally saw it at the first sentence -- brilliant! @Evan -- yeah but being undecidable is not necessarily a \"defect\" -- it's just the way it is; just like axiomatic logic is not \"defective\" only because it is incomplete (G&#246;del).\n- @Evan, it's be a strength for C++ programmers in that you can compute things at compile time. However, it makes it more difficult to write a good C++ compiler.\n- This is not true. An implementation is allowed to reject a program because template instantiation/recursion exceeds some arbitrary depth. The C++11 standard recommends allowing at least 1024 nested template instantiations, but that isn't actually a requirement. Thus all template metaprograms halt in O(1) time. Similarly C and C++ have preprocessor metaprograms, but the (recursive) nesting level of `#include` is allowed to be limited.\n- @Potatoswatter: it's more useful to consider C++ templates to be Turing-complete (unless your program hits the instantiation depth, which you can usually even increase), just as almost everybody does with computers (which do not have unlimited memory, hence are not Turing-complete). The compiler is unable to distinguish a genuinely looping program from one which would have needed just one more template instantiation.\n- @Blaisorblade: Useful yes, but in terms of formal definitions, it makes a big difference that the standard specifies an implementation-defined limit. However (and I should have remembered this in December), there is at least one instance of template recursion *without* template nesting, in the drill-down behavior of `operator->`. Endless recursion within this mechanism crashes possibly all C++ compilers except GCC, which I fixed last June by adjusting it to be considered (incorrectly in Standard terms) as nesting. gcc.gnu.org/bugzilla/show_bug.cgi?id=49118\n- This answer is incorrect. It does have to do with the ambiguity of the C++ grammar. The problem is that type/variable-name disambiguation that is traditionally performed by the \"lexer hack\" in plain C can require arbitrary template instantiation to resolve in C++. Since template instantiation is Turing-complete, simply producing a *parse tree* in C++ is undecidable in general, unless you limit template instantiation depth.\n- you're famous: blog.reverberate.org/2013/08/&hellip;\n- Thanks for the update Jay. I just noticed one little thing in your updated answer: you imply that \"semantic analysis\" is a monolithic thing, and that you have to do all of it to parse C++. But I think the only thing required to parse C++ is partial instantiation of class templates. I *think* (but can't prove) that many other parts of semantic analysis (overload resolution, implicit conversion, arithmetic conversions) aren't actually necessary to disambiguate the parse tree. It's possible I'm missing something, but \"semantic analysis\" seems like slightly too broad a brush.\n- Hmm, this might be too pedantic of me. Everyone I talk to about this just calls the whole package \"semantic analysis.\" Oh well.\n- Of course, this is not limited to C++ - consider BASIC, for example - is \"x = 1\" an assignment or a test? Only in context can you tell.\n- Yossi Kreinin does talk about \"the problem making the C++ grammar undecidable\", but that's bullshit; in fact the same website elsewhere, while explaining that C++ has undecidable grammar, says that \"This shows (on an intuitive level) that the C++ grammar is quite context-sensitive.\" yosefk.com/c++fqa/defective.html#defect-2\n- @anon: in BASIC it's enough to know against which production you are matching \"y=1\", i.e. if it is an expression or a statement. C++ is more complex, since the same character sequence, at the same position and in the same immediate context can mean totally different things.\n- There's a choice of terms, but it doesn't matter here whether the program terminates. In formal languages, a language is decidable if an algorithm can decide whether a word belongs to the language. In plain terms, if a program compiles it belongs to the language, no matter the run-time results. Furthermore, simply-typed lambda calculus is a quite simple example of a language where every program terminates, and many other more complex variations exist.\n- This is an old question & answer and I don't care to edit it, but perhaps I worded it poorly. The C++ template system is definitely Turing complete, hence it's undecidable whether a piece of code (or a 'word' in the technical jargon) is a valid C++ program or not. But does one consider the full C++ specification the 'C++ grammar'? Or can a C++ program be 'grammatically correct' but still fail to compile? The mention of context-free grammars in my answer is a bit misleading though.\n- As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.","metadata":{"transformedAt":"2026-08-18T18:32:17.749Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":77,"estimatedTokens":1977}}57{"id":"stack-2983139","source":"stackoverflow","questionId":2983139,"title":"assign operator to variable in python?","tags":["python","compiler-construction","operators"],"text":"Title: assign operator to variable in python?\nTags: python, compiler-construction, operators\nSource: Stack Overflow\n\nQuestion:\nUsual method of applying mathematics to variables is \n\n```\na * b\n```\n\nIs it able to calculate and manipulate two operands like this?\n\n```\na = input('enter a value')\nb = input('enter a value') \nop = raw_input('enter a operand')\n```\n\nThen how do i connect op and two variables `a` and `b`?\n\nI know I can compare op to `+`, `-`, `%`, `$` and then assign and compute....\n\nBut can i do something like `a op b`, how to tell compiler that op is an operator?\n\n========================================\n\nTop Answer:\nThe operator module http://docs.python.org/library/operator.html exposes functions corresponding to practically all Python operators. You can map operator symbols to those functions to retrieve the proper function, then assign it to your op variable and compute op(a, b).\n\n========================================\n\nCode:\n```text\na * b\n```\n\n```text\na = input('enter a value')\nb = input('enter a value') \nop = raw_input('enter a operand')\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\n+\n```\n\n```text\n-\n```\n\n```text\n%\n```\n\n```text\n$\n```\n\n```text\na op b\n```\n\n```text\nimport operator\nops = {\n    \"+\": operator.add,\n    \"-\": operator.sub,\n    \"*\": operator.mul,\n    \"/\": operator.div\n}   \nop_char = input('enter a operand')\nop_func = ops[op_char]\nresult = op_func(a, b)\n```\n\n```py\na = input('enter a value')\nb = input('enter a value') \nop = input('enter an operand')\nexpression = a + op + b # simple string concatenation\nresult = eval(expression)\n```\n\n```text\neval\n```\n\n```text\nast.literal_eval\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\nop\n```\n\n```text\nresult\n```\n\n```text\nimport operator\nops = {\n    \"+\": operator.add,\n    \"-\": operator.sub,\n    \"*\": operator.mul,\n    \"/\": operator.truediv\n}   \nop_char = input('enter a operand')\na = 1\nb = 2\nresult = ops[op_char](a,b)\nprint(result)\n```\n\n```text\na = int(input(\"Give a number for an operation: \"))\nb = int(input(\"Give a number for the other operation: \"))\noperation = input(\"Choose a math operation (+, -, *, /): \")\n\nif operation == \"+\":\n           result = a + b\nelif operation == \"-\":\n            result = a - b\nelif operation == \"*\":\n           result = a * b\nelif operation == \"/\":\n           result = a / b\nelse:\n           result = \"Invalid operation\"\n\nprint(f\"The result is: {result}\")\n```\n\n========================================\n\nComments:\n- For python 3.x the `operator.truediv` or `operator.floordiv` should be used instead docs.python.org/3/library/operator.html\n- additionally, using op_func in this way doesn't work. As the edit queue on this answer is full I've fixed these errors in an answer below (assuming this answer remains ACCEPTED w/ 76 upvotes...)\n- @RobertHoughton: what do you claim \"doesn't work\" with this answer? (I don't know what you mean by \"the edit queue\" either, but that's maybe just something I don't see.)\n- It should be expression = a + c + b . btw, using eval for user input is a bad idea. ast.literal_eval is better.\n- @SilentGuy you're very much right! I've updated my answer\n- `eval` wasn't new to Python 3. The reason people aren't suggesting it is because it is not safe to use on user input. It can execute any Python expression (with any side effects) that someone types in. Also, `literal_eval` only evaluates literals. It cannot evaluate an arithmetic expression like `'5+10'`.\n- `ast.literal_eval` cannot do arithmetic in general, or deal with other operators. It can only handle `+` and `-` because of a quirk in the Python grammar.\n- As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.","metadata":{"transformedAt":"2026-08-18T18:32:17.749Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":19,"totalLines":161,"estimatedTokens":951}}58{"id":"stack-601974","source":"stackoverflow","questionId":601974,"title":"CLR vs JIT","tags":["c#","compiler-construction","clr","jit"],"text":"Title: CLR vs JIT\nTags: c#, compiler-construction, clr, jit\nSource: Stack Overflow\n\nQuestion:\nWhat is the difference between the JIT compiler and CLR? If you compile your code to il and CLR runs that code then what is the JIT doing? How has JIT compilation changed with the addition of generics to the CLR?\n\n========================================\n\nTop Answer:\nYou compile your code to IL which gets executed and compiled to machine code during runtime, this is what's called JIT.\n\n**Edit**, to flesh out the answer some more (still overly simplified):\n\nWhen you compile your C# code in visual studio it gets turned into IL that the CLR understands, the IL is the same for all languages running on top of the CLR (which is what enables the .NET runtime to use several languages and inter-op between them easily).\n\nDuring runtime the IL is interpreted into machine code (which is specific to the architecture you're on) and then it's executed. This process is called Just In Time compilation or JIT for short. Only the IL that is needed is transformed into machine code (and only once, it's \"cached\" once it's compiled into machinecode), **just in time** before it's executed, hence the name JIT.\n\nThis is what it would look like for C#\n\n C# Code `>` C# Compiler `>` IL `>` .NET Runtime `>` JIT Compiler `>` Machinecode `>` Execution\n\nAnd this is what it would look like for VB\n\n VB Code `>` VB Compiler `>` IL `>` .NET Runtime `>` JIT Compiler `>` Machinecode `>` Execution\n\nAnd as you can see only the two first steps are unique to each language, and everything after it's been turned into IL is the same which is, as I said before, the reason you can run several different languages on top of .NET\n\n========================================\n\nCode:\n```text\n<T>\n```\n\n```text\n>\n```\n\n```text\n>\n```\n\n```text\n>\n```\n\n```text\n>\n```\n\n```text\n>\n```\n\n```text\n>\n```\n\n```text\n>\n```\n\n```text\n>\n```\n\n```text\n>\n```\n\n```text\n>\n```\n\n```text\n>\n```\n\n```text\n>\n```\n\n========================================\n\nComments:\n- I fleshed out my answer since you accepted it. If this makes it too verbose and no longer what you would consider an acceptable answer add a comment to say so and I'll shrink it/revert it. It just seemed a shame to lose the info from the other answers\n- -1 for referring to IL as bytecode, it's a Java semantic. IL (formerly MSIL) is the correct term don't confuse the two. Change that and you got my vote.\n- Is it true that the JIT compiles the code to machine code the first time you run the program, and after that the compilation is skipped and the compiled code is used? If so it might be worth incorporating this into your answer.\n- The JITTing currently happens on a per method basis. the code is self modifying. This is an implmentation detail they are free to change though\n- CIL IS a bytecode, I don't understand why do you thing it isn't, refer to the ECMA CLI spec (Partition II) for hex code for the CIL.\n- @fredrikholmstrom - Good, the size of IL instructions are 2 bytes and not single bytes. The term bytecode does not belong in the CLI. IL is just short of intermediate language and it's just that, intermediate.\n- Correction, ECMA CLI spec (Partition III)\n- If you talk bytecode, I would presume you where talking about Java bytecode. If you say IL, I'll presume you are talking about the .NET platform. It's as simple as that. I want to maintain that distinction.\n- Minor point - the size of IL instructions are variable, from 1 byte (e.g. Load integer constant 0), to 5 bytes (e.g. load large integer constant), and beyond - the 'switch' IL instruction is unbounded in how many bytes it can contain!\n- `When you compile in VS Your source code is compiled into IL` , Can you explain how visual studio compiles to IL ? , Does VS calls C# compiler internally which is installed with .NET Framework in our machine ?\n- @Ted: Was this the sort of thing you were interested in? I didn't give a more detailed explanation (like Fredrik's) because I figured you already knew most of that sort of thing and were interested in the relationship between the CLR and the JIT.\n- No I never down voted it. I think all the answers are really good. I think I just should have asked the question differently. I was really interested in the relationship between the CLR and the JIT compiler and how it has changed with the addition of generics.\n- Dammit! Stop calling it bytecode, it's IL, an intermediate language representation. As the size of IL instructions are 16-bit words.\n- I fear you are fighting a losing battle on this one John :) I think \"Intermediate code\" is reasonable general term where you want to include java's, 'net's, LLVM's etc. but dammit if bytecode isn't snappy...\n- If you talk bytecode, I would presume you where talking about Java bytecode. If you say IL, I'll presume you are talking about the .NET platform. It's as simple as that. I want to keep that distinction alive.\n- As I said before, CIL is bytecode as I understand it... even the CLI spec refers to it as so... I don't understand why do you state it isn't\n- Bytecode is a generic term for an intermediate language based on byte values to describe operations and though it's true it usually refering to a single byte for the \"code\", it isn't completely needed nor is it exclusively used in java.\n- I always thought the JIT operated on a per-class basis, now you're suggesting that it's working on a per-method basis. Did they change this behaviour? Or am I mistaken?\n- Konrad - it is an implementation *detail* whichever it uses. The JIT uses a sort of trampolining to achieve this on a per method basis last time I looked.\n- IIRC it's per method in the desktop CLR and per class in the Compact Framework.\n- Micro**S**oft? Why?","metadata":{"transformedAt":"2026-08-18T18:32:17.749Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":107,"estimatedTokens":1429}}59{"id":"stack-40799696","source":"stackoverflow","questionId":40799696,"title":"How is GCC IR different from LLVM IR?","tags":["gcc","compilation","compiler-construction","llvm-ir"],"text":"Title: How is GCC IR different from LLVM IR?\nTags: gcc, compilation, compiler-construction, llvm-ir\nSource: Stack Overflow\n\nQuestion:\nWhy do people prefer LLVM IR, and how exactly is it different from the GCC IR? Is target dependency a factor here?\n\nI'm a complete newbie to compilers, and wasn't able to find anything relevant even after many hours of searching for an answer. Any insights would be helpful.\n\n========================================\n\nCode:\n```text\n// Base class which all operators aggregate\nstruct GTY(()) tree_base {\n  ENUM_BITFIELD(tree_code) code : 16;\n\n  unsigned side_effects_flag : 1;\n  unsigned constant_flag : 1;\n  unsigned addressable_flag : 1;\n\n  ...  // Many more fields\n};\n\n// Typed operators add type to base data\nstruct GTY(()) tree_typed {\n  struct tree_base base;\n  tree type;\n};\n\n// Constants add integer value to typed node data\nstruct GTY(()) tree_int_cst {\n  struct tree_typed typed;\n  HOST_WIDE_INT val[1];\n};\n\n// Complex numbers add real and imaginary components to typed data\nstruct GTY(()) tree_complex {\n  struct tree_typed typed;\n  tree real;\n  tree imag;\n};\n\n// Many more operators follow\n...\n```\n\n```text\nunion GTY ((ptr_alias (union lang_tree_node),\n            desc (\"tree_node_structure (&%h)\"), variable_size)) tree_node {\n  struct tree_base GTY ((tag (\"TS_BASE\"))) base;\n  struct tree_typed GTY ((tag (\"TS_TYPED\"))) typed;\n  struct tree_int_cst GTY ((tag (\"TS_INT_CST\"))) int_cst;\n  struct tree_complex GTY ((tag (\"TS_COMPLEX\"))) complex;\n```\n\n```text\nif (gimple_assign_p (stmt)\n      && gimple_assign_rhs_code (stmt) == PLUS_EXPR\n      && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)\n    {\n      ...\n```\n\n```text\nif (auto BO = dyn_cast<BinaryOperator>(V))\n  if (BO->getOpcode() == Instruction::Add\n      && isa<ConstantInt>(BO->getOperand(1))\n    {\n```\n\n```text\nif (!tree_fits_shwi_p (arg1))\n    return false;\n\n  *exponent = tree_to_shwi (arg1);\n```\n\n```text\nalloca\n```\n\n```text\nADDR_EXPR\n```\n\n```text\ntree\n```\n\n```text\nDECL_NAME\n```\n\n```text\nTREE_IMAGPART\n```\n\n```text\n--enable-checking\n```\n\n```text\nHOST_WIDE_INT\n```\n\n```text\nAPInt\n```\n\n```text\nHOST_WIDE_INT\n```\n\n========================================\n\nComments:\n- The biggest practical difference is that for LLVM there is a modular API available. And for GCC, developers made everything they could to make sure such an API cannot be exposed. That's why LLVM is a go to solution if you want an IR to plug in.\n- Great answer. You might mention that IR and GIMPLE are similar enough that LLVM initially used DragonEgg to convert the GIMPLE generated by GCC into IR. Of course, that was before Clang.\n- @Olsonist Very good point, added.","metadata":{"transformedAt":"2026-08-18T18:32:17.749Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":14,"totalLines":120,"estimatedTokens":663}}60{"id":"stack-8976423","source":"stackoverflow","questionId":8976423,"title":"Is it a good idea to compile a language to C?","tags":["c","gcc","compiler-construction","ghc"],"text":"Title: Is it a good idea to compile a language to C?\nTags: c, gcc, compiler-construction, ghc\nSource: Stack Overflow\n\nQuestion:\nAll over the web, I am getting the feeling that writing a C backend for a compiler is not such a good idea anymore. GHC's C backend is not being actively developed anymore (this is my unsupported feeling). Compilers are targeting C-- or LLVM.\n\nNormally, I would think that GCC is a good old mature compiler that does performs well at optimizing code, therefore compiling to C will use the maturity of GCC to yield better and faster code. Is this not true?\n\nI realize that the question greatly depends on the nature of the language being compiled and on other factors such that getting more maintainable code. I am looking for a rather more general answer (w.r.t. the compiled language) that focuses solely on performance (disregarding code quality, ..etc.). I would be also really glad if the answer includes an explanation on why GHC is drifting away from C and why LLVM performs better as a backend (see this) or any other examples of compilers doing the same that I am not aware of.\n\n========================================\n\nTop Answer:\nLet me list my two biggest problems with compiling to C. If this is a problem for your language depends on what kind of features you have.\n\n**Garbage collection** When you have garbage collection you may have to interrupt regular execution at just about any point in the program, and at this point you need to access all pointers that point into the heap. If you compile to C you have no idea where those pointers might be. C is responsible for local variables, arguments, etc. The pointers are probably on the stack (or maybe in other register windows on a SPARC), but there is no real access to the stack. And even if you scan the stack, which values are pointers? LLVM actually addresses this problem (thought I don't know how well since I've never used LLVM with GC).\n\n**Tail calls** Many languages assume that tail calls work (i.e., that they don't grow the stack); Scheme mandates it, Haskell assumes it. This is not the case with C. Under certain circumstances you can convince some C compilers to do tail calls. But you want tail calls to be reliable, e.g., when tail calling an unknown function. There are clumsy workarounds, like trampolining, but nothing quite satisfactory.\n\n========================================\n\nComments:\n- One answer is that C can't give you direct access to key resources of the machine. Try manipulating the stack pointer in vanilla C.\n- I disagree with closing this question. There are clear facts to be given against compiling to C, but now I can't give them.\n- GCC is THE worst compiler known to mankind.\n- @Jake'Alquimista'LEE care to substantiate your claim with any evidence? In my personal experience, GCC in 2024 still leads by a wide margin as the most reliable, consistent, do-as-expected, and customizable compiler.\n- @JackG Try to write some NEON codes in intrinsics, it won't take long until you give up. It's THAT bad.\n- @JackG To be specific,, write a simple matrix transposition, and be amazed seeing it running at less than half the speed compared to the assembly version.\n- @Jake'Alquimista'LEE That sounds like a “you” problem not understanding GCC well enough to leverage its potential. E.g. most of the performance issues stem from popular distros like Ubuntu configuring GCC to use excessive security/hardening flags by default. For low-level SIMD optimizing like matrix transposition, GCC is a godsend and can be coaxed into generating optimal SIMD with portable vector extensions. Then, recompiling it for other architecture usually lands close to optimal SIMD, whereas you’d start from scratch if you wrote assembly.\n- @JackG It seems you don't have ANY experience in NEON at all to start with. All the permutation instructions such as VZIP, VTRN, VUZIP will result in unnecessary VMOV instructions when compiled.\n- @Jake'Alquimista'LEE Can you a Godbolt demoing that? I have hand-inspected plenty of GCC portable vector extensions and have yet to see extra `vmov`s as an issue compiling to ARM or AARCH64. (I'm not trying to dismiss you; I'm genuinely curious if there's cases I haven't seen yet where GCC emits unnecessary vmovs.)\n- @JackG godbolt.org/z/Pvhv7bo74 And I've had TONS of bad experiences with GCC. GCC sucks, period.\n- @JackG godbolt.org/z/1Kq5brjMv This is even a better example, dealing with a 16x16 matrix. In addition to those unnecessary `vomv`s, GCC makes intensive stack swap since it can't handle registers well. See that it claims 100bytes of stack at the beginning?\n- @JackG \"bad experiences\" is a monumental understatement. I'd call them \"disasters\" Far more than a decade passed since NEON has been available, and GCC still can't handle `vtrn`, `vzip`, and `vuzp` properly. Godsend? Really? GCC is an immediate red flag in my book.\n- This is a great answer if you're also concerned about portability, which is also an important consideration.\n- I agree - should I ever implement a language, a ridiculously portable C89 implementation will be the first item on my list, long before a clever JIT compiler using LLVM.\n- Can you name any targets that LLVM doesn't support that GCC does?\n- C means C, not GCC. There are probably over 100 different C compilers in the world (at least if you only demand C89 and not C99 or C11). Combined, they support a lot more targets than GCC or LLVM could ever.\n- Targeting \"general C\" instead of a general compiler means you'll never actually release a finished product, but only a construction kit. At best, a puzzle at worst :)\n- @MarcovandeVoort: That makes no sense. If you write C, then unless you do really stupid stuff, it automatically works on almost any compiler (barring really stupid pathological things and implementation is allowed to do, and the whole translation limits/\"at least one program\" issue).\n- The point is more that there is more to creating a compiler that has a C codegenerator than generating standard C. Think parameters, build systems, runtime library issues + versioning etc etc.\n- JIT compilation is a big plus of course. I agree with what you are saying to an extent.. I just can't see what one \"loses\" when compiling to C but does not lose it if one compiles to LLVm, can you please elaborate on that a little bit. I mean I agree that one \"loses\" some structure with language transformation, but does the same not happen with LLVM or any other backend language?\n- OK, imagine in C the statement `x++` - this could be compiled to copy x to another register, then increment the value of x, then return the copied (previous) value of x. A very obvious optimization is to compile this using a test-and-set instruction, if the processor supports it, which does exactly this, but faster and atomically. If you represent the same statement in C as `x = x + 1`, it may not be optimized, because it's not exactly the same - you never need to return the previous value, right?\n- So to get this optimization, you would have to build *your* compiler - the one generating c - to know the difference between the two and produce different c code depending on the situation. If you compile to LLVM bytecode, LLVM can infer this from your generated bytecode by e.g. checking if you look at the return value and deciding to optimize then. GCC *may* be smart enough to do this, since this is such a trivial example, but it's just easier for an optimizer to find this sort of low hanging fruit when dealing with a lower level language, like LLVM bytecode, than when dealing with c.\n- *\"The creators of the C compiler have probably spent a lot of time optimizing certain human-made patterns in the language\"* Do you have any evidence of this, and everything that follows it, at all? When you're compiling to C you're going to simplify the patterns, yes, but simple patterns are often the most efficient ones.\n- @jco I encourage you to look at GCC's optimizations. That page is 1431 lines documenting the GCC team's performance enhancements. A good many of these are language-level optimizations. Picking one at random: having the compiler unroll some loops (-funroll-loops) as opposed to unrolling all loops (-funroll-all-loops), when the second is known to be less performant. In order to use GCC's optimizations, you have to target the precursor C code to trigger that unrolling, and study the binary to validate. Better just to target LLVM, IMO.\n- Note that there is a terrible difference between LLVM IR as principle (universal backend for languages), or LLVM IR in practice (anything not stressed by the clang frontend is unimplemented, undocumented and/or buggy)\n- This answer makes no sense: if targetting C loses some magic idiomatic information, targeting IR should lose *even more* since it's what C converts to. If LLVM can figure out the `x++` pattern from a `x = x + 1` then no extra info needs to come down from the C layer. Besides, the first thing that LLVM does with the IR is to canonicalize it, so it first tries to *remove* extra information.\n- I *believe* the GCC backend is supposed to stick around for bootstrapping (that's just a vague memory though). For LLVM, a major reason is that is't modular *and there already are lots of great modules*. Specifically, using LLVM gives you (paraphrasing Don Steward) 25 years of imperative language optimizations for free, and more target architectures than most compiler teams can ever hope to implement (let alone maintain).\n- Yes, for bootstrapping it's around to stay. It's not as important to produce code suitable for gcc's optimiser then, and that's maintainable with little enough work. But `-fvia-C` is already gone in 7.2. Second point: Stewart, with 't'. I don't know if he said it, but it's very true.\n- Source: donsbot.wordpress.com/2010/03/01/&hellip;. About the t: Yeah, sorry - sadly I can't edit any more.\n- Generating C code and letting the underlying system's C compiler optimize for you gives you >25 years of imperative language optimizations for free. :-)\n- @R.. Not to the same extent, as others have argued. C compilers are good at optimizing C code, not at optimizing arbitary low-level code; and C is far worse than (LLVM) assembly at encoding low-level details (and a compiler like GHC does optimize at this level). For instance, try pinning registers and selectively replacing function pointers with the code they point to - for C, that requires an obscure GCC extension and 2000 lines of Perl code. For LLVM, it requires defining a calling convention, some care when generating code, and 180 lines of Haskell.\n- Sounds like that doesn't fall under \"imperative language optimizations\". I agree there might be benefits to functional languages in not going through an imperative language for the translation, but I still question the magnitude of the benefits especially relative to the costs.\n- Well, the LLVM backend gives the existing register allocators lots of freedom to utilize even \"pinned\" registers when sensible (at least in between function calls), while the C Backend works by making the pinned register completely unavailable to GCC's register allocator. And what cost? The LLVM backend was created in a few man-months (well, I'm guessing here, but it was one guy's thesis), is tiny in code size, handily keeps up with (or even beats) the existing backends and provides more flexibility.\n- That's the big thing for me: writing the LLVM backend was, very clearly, astronomically simpler than writing and maintaining the GCC backend.\n- I claim it's not possible to produce idiomatic C code and still support accurate GC and tail calls.\n- The LLVM backend is comparable in complexity to the *unregisterised* C backend - that is, the C backend without tail-calls and support for pinning certain VM registers to machine registers. Supporting these in the C backend is what added most of the complexity, and we get these in LLVM without much fuss.\n- I agree with your first sentence but not your second para. Even if C semantics are complex from a standpoint of writing an implementation or interfacing with arbitrary C code, things are much easier when you're just using a limited subset of the language. The only way I can think someone naive might screw it up is by violating aliasing rules or being unaware that certain arithmetic ops invoke UB.\n- I think the first step on the road to perdition is believing that C's semantics are simple. :)\n- Only in very first order. If you look closer, it already stumbles on something simple as pascal with its nested procedures and the ability to pass them (as procvar) that keep access to the parent's stack frame.\n- The last point is basically dogfooding. The same arguments could be used against using LLVM\n- Many languages = functional languages ? :-)\n- Yes, mostly functional, I believe.\n- Lennart is spot on here - these are two of the main issues we have had with GHC in compiling via C. Dealing with garbage collection properly really entails managing the stack yourself - the alternative is to use conservative GC, which is not really viable in a production system. LLVM solves the tail-call problem, but its solution to the GC problem isn't good enough yet for GHC (and it's not clear it ever will be - there was a serious attempt in C-- to do this right, and even there it entailed some compromises).\n- But wait, tail call optimization were even in the ancient gcc-3.0.4 *(search for -foptimize-sibling-calls)*.\n- As far as I know gcc cannot optimize all kinds of tail calls. Tail calls can be to known functions, but can also be indirect, i.e., via a function pointer.\n- Last time I saw Tail calls were optimized out by GCC\n- C also lacks rotate operators which most processors have, and cannot directly manipulate the stack.","metadata":{"transformedAt":"2026-08-18T18:32:17.750Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":70,"estimatedTokens":3446}}61{"id":"stack-25847349","source":"stackoverflow","questionId":25847349,"title":"For { A=a; B=b; }, will \"A=a\" be strictly executed before \"B=b\"?","tags":["c++","c","optimization","compiler-construction","standards"],"text":"Title: For { A=a; B=b; }, will \"A=a\" be strictly executed before \"B=b\"?\nTags: c++, c, optimization, compiler-construction, standards\nSource: Stack Overflow\n\nQuestion:\nSuppose `A`, `B`, `a`, and `b` are all variables, and the addresses of `A`, `B`, `a`, and `b` are all different. Then, for the following code:\n\n```\nA = a;\nB = b;\n```\n\nDo the C and C++ standard explicitly require `A=a` be strictly executed before `B=b`? Given that the addresses of `A`, `B`, `a`, and `b` are all different, are compilers allowed to swap the execution sequence of two statements for some purpose such as optimization?\n\nIf the answer to my question is different in C and C++, I would like to know both.\n\nEdit: The background of the question is the following. In board game AI design, for optimization people use lock-less shared-hash table, whose correctness strongly depends on the execution order if we do not add `volatile` restriction.\n\n========================================\n\nTop Answer:\nThe compiler is only obligated to emulate the observable behavior of a program, so if a re-ordering would not violate that principle then it would be allowed. Assuming the behavior is well defined, if your program contains undefined behavior such as a data race then the behavior of the program will be unpredictable and as commented would require use of some form of synchronization to protect the critical section. \n\n**A Useful reference**\n\nAn interesting article that covers this is Memory Ordering at Compile Time and it says:\n\n The cardinal rule of memory reordering, which is universally followed\n by compiler developers and CPU vendors, could be phrased as follows:\n\n \n \n *Thou shalt not modify the behavior of a single-threaded program.*\n\n \n\n**An Example**\n\nThe article provides a simple program where we can see this reordering:\n\n```\nint A, B; // Note: static storage duration so initialized to zero\n\nvoid foo()\n{\n A = B + 1;\n B = 0;\n}\n```\n\nand shows at higher optimization levels `B = 0` is done before `A = B + 1`, and we can reproduce this result using godbolt, which while using `-O3` produces the following (*see it live*):\n\n```\nmovl $0, B(%rip) #, B\naddl $1, %eax #, D.1624\n```\n\n**Why?**\n\nWhy does the compiler reorder? The article explains it is exactly the same reason the processor does so, because of complexity of the architecture:\n\n As I mentioned at the start, the compiler modifies the order of memory\n interactions for the same reason that the processor does it –\n performance optimization. Such optimizations are a direct consequence\n of modern CPU complexity.\n\n**Standards**\n\nIn the draft C++ standard this is covered in section `1.9` *Program execution* which says (*emphasis mine going forward*):\n\n The semantic descriptions in this International Standard define a\n parameterized nondeterministic abstract machine. This International\n Standard places no requirement on the structure of conforming\n implementations. In particular, they need not copy or emulate the\n structure of the abstract machine. Rather, conforming implementations\n are required to emulate (only) the observable behavior of the abstract\n machine as explained below.5\n\nfootnote `5` tells us this is also known as the *as-if rule*:\n\n This provision is sometimes called the **“as-if” rule**, because an\n implementation is **free to disregard any requirement** of this\n International Standard as long as the result is as if the requirement\n had been obeyed, as far as can be determined from the observable\n behavior of the program. For instance, an actual implementation need\n not evaluate part of an expression if it can deduce that its value is\n not used and that no side effects affecting the observable behavior of\n the program are produced.\n\nthe draft C99 and draft C11 standard covers this in section `5.1.2.3` *Program execution* although we have to go to the index to see that it is called the *as-if rule* in the C standard as well:\n\n as−if rule, 5.1.2.3\n\n**Update on Lock-Free considerations**\n\nThe article An Introduction to Lock-Free Programming covers this topic well and for the OPs concerns on *lock-less shared-hash table* implementation this section is probably the most relevant:\n\n **Memory Ordering**\n\n \n As the flowchart suggests, any time you do lock-free programming for\n multicore (or any symmetric multiprocessor), and your environment does\n not guarantee sequential consistency, you must consider how to prevent\n memory reordering.\n\n \n On today’s architectures, the tools to enforce correct memory ordering\n generally fall into three categories, which prevent both compiler\n reordering and processor reordering:\n\n \n \n \n- A lightweight sync or fence instruction, which I’ll talk about in future posts;\n \n- A full memory fence instruction, which I’ve demonstrated previously;\n \n- Memory operations which provide acquire or release semantics.\n \n \n Acquire semantics prevent memory reordering of operations which \n it in program order, and release semantics prevent memory reordering\n of operations preceding it. These semantics are particularly suitable\n in cases when there’s a producer/consumer relationship, where one\n thread publishes some information and the other reads it. I’ll also\n talk about this more in a future post.\n\n========================================\n\nCode:\n```text\nA = a;\nB = b;\n```\n\n```text\nA\n```\n\n```text\nB\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\nA\n```\n\n```text\nB\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\nA=a\n```\n\n```text\nB=b\n```\n\n```text\nA\n```\n\n```text\nB\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\nvolatile\n```\n\n```text\nint A, B;  // Note: static storage duration so initialized to zero\n\nvoid foo()\n{\n    A = B + 1;\n    B = 0;\n}\n```\n\n```text\nmovl    $0, B(%rip) #, B\naddl    $1, %eax    #, D.1624\n```\n\n```text\nB = 0\n```\n\n```text\nA = B + 1\n```\n\n```text\n-O3\n```\n\n```text\n1.9\n```\n\n```text\n5\n```\n\n```text\n5.1.2.3\n```\n\n```text\n{ A=a, B=b; /*etc*/ }\n```\n\n========================================\n\nComments:\n- Even if the compiler was guaranteed to generate code in that order, the CPU itself does out of order execution.\n- Not only compilers are allowed to do this, CPUs are allowed to do this, memory controllers are allowed to do this, caches are allowed to do this, and so on.\n- I just added an interesting example that shows this practically and not just theoretically.\n- Any time you're doing multi-threading you get into another dimension entirely. Even if the code is executed sequentially you have no guarantee (without further controls) that execution will appear sequential as viewed from another processor. If you're trying to do something similar to your shared hash table you need to spend A LOT of time studying up on synchronization issues.\n- Indeed, this is the wrong question. What matters is visibility of the changes to other threads. Cache coherency protocols can, in the absence of memory fences, cause reordering of memory accesses.\n- @BenVoigt Thanks for your comments, but I don't quite understand. If the two statements did execute in order, would it be possible that at a certain point, another thread might have seen `B` become `b` but haven't seen `A` become 'a'?\n- @ACcreator: Yes, that's possible, depending on the particular cache coherency protocol in use. For example, x86 provides stronger guarantees than Itanium. With multithreading you also need to be concerned about tearing, and speculative writes.\n- @BenVoigt Thank you. For x86, could in-order execution guarantee in-order visibility of the changes? Apart from using lock, to solve this problem on general platforms (x86, Itanium, arm...), does there exist any better approaches?\n- @ACcreator: Use a memory fence. That's cheaper than a critical section, but still ensures that the caches have to be synchronized in the correct order also.\n- You see some interesting details and examples in Memory Reordering Caught in the Act which is from the same author from the article I link in my answer. The article in my answer also covers this a little bit as well, I just have not hard the chance to add a multi-threaded example yet. Both articles cover fences and other barriers.\n- @ShafikYaghmour Thank you. I will go through these materials later :)\n- I updated my answer with another article from the same author(*Jeff Preshing*) he just writes the best blog entries on this topic. It should address most of the issues you are dealing with in your application and how to handle them.\n- Also, neither could be performed if they didn't affect the program's observable behaviour. (i.e. optimized out completely)\n- It's probably worth pointing out that accessing or modifying a variable only counts as \"observable behaviour\" if the variable is volatile.\n- @MikeSeymour I guess with operator overloading in C++, `A = a` could readily have observable behaviour, even for non-volatile variables.\n- @DavidHeffernan: Yes, I should have been more precise, sorry. I meant \"accessing or modifying a fundamentally-typed variable\". Of course user-defined operations could have observable behaviour.\n- it will certainly change the observable behaviour in poorly written multithreaded programs! LOL!\n- It's also worth noting that some types of observable behavior are permitted to be optimized away, so the \"as-if\" rule is a little wishy-washy when it comes to constructors/destructors that cause observable behavior.\n- In this case I think it's worth stressing (as G_G implies) that the \"as if\" requirement is that the observable behavior *of a program with defined behavior* doesn't change. The questioner must not take this answer to mean that any change of instruction order is guaranteed not change the behavior of his lockless hashtable. In fact if he's asking this question it's because that code contains a data race, so its behavior is not defined and may very well change due to optimizations, accidents of scheduling, and whatnot.\n- @Cornstalks copy elision is not performed under the as-if rule; there is specific text that defines copy elision.\n- @MattMcNabb: Ah, that makes sense. I've always heard it explained with the \"as-if\" rule being the justification, but apparently those explanations must be wrong (or, more likely, I misunderstood what I had read). Thanks.\n- it only makes me wonder, why GCC asm produces `addl $1, %eax` instead of `incl %eax`? Even for `a++` it only produces `a += 1` ... ICC behaves as expected though.\n- @vaxquis not clear to me, seems like some form of attempted optimization, probably dependent on the assumptions being made by `gcc`.\n- Besides the questionable use of the comma operator, this does not address the OPs edit to the question.","metadata":{"transformedAt":"2026-08-18T18:32:17.750Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":25,"totalLines":265,"estimatedTokens":2644}}62{"id":"stack-5342345","source":"stackoverflow","questionId":5342345,"title":"How do generics get compiled by the JIT compiler?","tags":["c#","generics","compiler-construction","jit"],"text":"Title: How do generics get compiled by the JIT compiler?\nTags: c#, generics, compiler-construction, jit\nSource: Stack Overflow\n\nQuestion:\nI know that generics are compiled by JIT (like everything else), in contrast to templates that are generated when you compile the code.\n\nThe thing is that new generic types can be created in runtime by using reflection.\n\nWhich can of course affect the generic's constraints. Which already passed the semantic parser. \n\nCan someone explain how this is handled ? And what exactly happens ?\n\n (Both the code generation and semantic check)\n\n========================================\n\nTop Answer:\nReference types generics all become the **same** type; value type generics are instantiated **separately**.\n\nThis is because reference types are all really just `Object` references (4 or 8 bytes), whereas value types are different and cannot be handled by a single piece of code, due to stack layout differences, etc. Therefore, instantiating multiple copies of a generic type with value types will increase the memory usage by a lot, whereas instantiating multiple copies with reference types won't.\n\n========================================\n\nCode:\n```text\nList<int>\n```\n\n```text\nList<T>\n```\n\n```text\nList<int>\n```\n\n```text\nList<long>\n```\n\n```text\nList<double>\n```\n\n```text\nList<float>\n```\n\n```text\nList<int>\n```\n\n```text\nList<long>\n```\n\n```text\nList<float>\n```\n\n```text\nObject\n```\n\n========================================\n\nComments:\n- Constraints are not just enforced by the compiler, the jitter checks them too. There are several not-so-trivial aspects to generics, how they get ngen-ed is particularly boggling. It *did* take them 5 years.\n- They aren't the same type. They the same machine code for member functions (methods)... but each is a distinct type with its own copy of static member variables.","metadata":{"transformedAt":"2026-08-18T18:32:17.750Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":70,"estimatedTokens":460}}63{"id":"stack-131871","source":"stackoverflow","questionId":131871,"title":"Algorithm for implementing C# yield statement","tags":["c#","algorithm","compiler-construction","iterator","state-machine"],"text":"Title: Algorithm for implementing C# yield statement\nTags: c#, algorithm, compiler-construction, iterator, state-machine\nSource: Stack Overflow\n\nQuestion:\nI'd love to figure it out myself but I was wondering **roughly what's the algorithm for converting a function with yield statements into a state machine for an enumerator?** For example how does C# turn this:\n\n```\nIEnumerator strings(IEnumerable args)\n { IEnumerator enumerator2 = getAnotherEnumerator(); \n foreach(var arg in arg) \n { enumerator2.MoveNext();\n yield return arg+enumerator.Current;\n } \n }\n```\n\ninto this:\n\n```\nbool MoveNext()\n { switch (this.state)\n {\n case 0:\n this.state = -1;\n this.enumerator2 = getAnotherEnumerator();\n this.argsEnumerator = this.args.GetEnumerator();\n this.state = 1;\n while (this.argsEnumerator.MoveNext())\n {\n this.arg = this.argsEnumerator.Current;\n this.enumerator2.MoveNext();\n this.current = this.arg + this.enumerator2.Current;\n this.state = 2;\n return true;\n\n state1:\n this.state = 1;\n }\n this.state = -1;\n if (this.argsEnumerator != null) this.argsEnumerator.Dispose();\n break;\n\n case 2:\n goto state1;\n }\n return false;\n}\n```\n\nOf course the result can be completely different depending on the original code.\n\n========================================\n\nTop Answer:\nJust spotted this question - I wrote an article on it recently. I'll have to add the other links mentioned here to the article though...\n\n========================================\n\nCode:\n```text\nIEnumerator<string> strings(IEnumerable<string> args)\n { IEnumerator<string> enumerator2 = getAnotherEnumerator();     \n   foreach(var arg in arg) \n    { enumerator2.MoveNext();\n      yield return arg+enumerator.Current;\n    } \n }\n```\n\n```text\nbool MoveNext()\n { switch (this.state)\n    {\n        case 0:\n            this.state = -1;\n            this.enumerator2 = getAnotherEnumerator();\n            this.argsEnumerator = this.args.GetEnumerator();\n            this.state = 1;\n            while (this.argsEnumerator.MoveNext())\n            {\n                this.arg = this.argsEnumerator.Current;\n                this.enumerator2.MoveNext();\n                this.current = this.arg + this.enumerator2.Current;\n                this.state = 2;\n                return true;\n\n              state1:\n                this.state = 1;\n            }\n            this.state = -1;\n            if (this.argsEnumerator != null) this.argsEnumerator.Dispose();\n            break;\n\n        case 2:\n            goto state1;\n    }\n    return false;\n}\n```\n\n```text\nforeach (var x in y)\n{\n   //body\n}\n```\n\n```text\nvar enumerator = y.GetEnumerator();\nwhile (enumerator.MoveNext())\n{\n    var x = enumerator.Current;\n    //body\n}\n\nif (y != null)\n{\n    enumerator.Dispose();\n}\n```\n\n```text\nclass ClosureEnumerable : IEnumerable<string>\n {\n    private IEnumerable<string> args;\n    private ClassType originalThis;\n    public ClosureEnumerator(ClassType origThis, IEnumerable<string> args)\n    {\n        this.args = args;\n        this.origianlThis = origThis;\n    }\n    public IEnumerator<string> GetEnumerator()\n    {\n        return new Closure(origThis, args);\n    }\n }\n\nclass Closure : IEnumerator<string>\n{\n    public Closure(ClassType originalThis, IEnumerable<string> args)\n    {\n        state = 0;\n        this.args = args;\n        this.originalThis = originalThis;\n    }\n\n    private IEnumerable<string> args;\n    private IEnumerator<string> enumerator2;\n    private IEnumerator<string> argEnumerator;\n\n    //- Here ClassType is the type of the object that contained the method\n    //  This may be optimized away if the method does not access any \n    //  class members\n    private ClassType originalThis;\n\n    //This holds the state value.\n    private int state;\n    //The current value to return\n    private string currentValue;\n\n    public string Current\n    {\n        get \n        {\n            return currentValue;\n        }\n    }\n}\n```\n\n```text\ncurrentValue = expr;\nstate = //the state number of the yield statement;\nreturn true;\n```\n\n```text\nstate = -1;\nreturn false;\n```\n\n```text\nIEnumerator<string> strings(IEnumerable<string> args)\n{\n   return new ClosureEnumerable(this,args);\n}\n```\n\n========================================\n\nComments:\n- I think marxidad want to figure out what is the algorithm used by the compiler to interpret and transform the C# code of iterator blocks into the IL which corresponds to a state machine.\n- You linked to part 4 of the series. You meant to link to part 1, which really does answer the question.\n- While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes or disappears.\n- Wow. Excellent and comprehensive answer. I wish I could vote this up more than once.","metadata":{"transformedAt":"2026-08-18T18:32:17.750Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":196,"estimatedTokens":1198}}64{"id":"stack-8509035","source":"stackoverflow","questionId":8509035,"title":"Why only literal strings saved in the intern pool by default?","tags":["c#",".net","string","compiler-construction","clr"],"text":"Title: Why only literal strings saved in the intern pool by default?\nTags: c#, .net, string, compiler-construction, clr\nSource: Stack Overflow\n\nQuestion:\nWhy by default only literal strings are saved in the intern pool?\n\nExample from MSDN:\n\n```\nString s1 = \"MyTest\";\nString s2 = new StringBuilder().Append(\"My\").Append(\"Test\").ToString(); \nString s3 = String.Intern(s2); \nConsole.WriteLine(\"s1 == '{0}'\", s1);\nConsole.WriteLine(\"s2 == '{0}'\", s2);\nConsole.WriteLine(\"s3 == '{0}'\", s3);\nConsole.WriteLine(\"Is s2 the same reference as s1?: {0}\", (Object)s2==(Object)s1); \nConsole.WriteLine(\"Is s3 the same reference as s1?: {0}\", (Object)s3==(Object)s1);\n\n/*\nThis example produces the following results:\ns1 == 'MyTest'\ns2 == 'MyTest'\ns3 == 'MyTest'\nIs s2 the same reference as s1?: False\nIs s3 the same reference as s1?: True\n*/\n```\n\n========================================\n\nTop Answer:\nThe language designers decided the cost of interning every intermediate string value was not worth the performance cost. Interning of garbage-collectible strings requires a single global weak map which can become a bottleneck when you have large numbers of threads.\n\n========================================\n\nCode:\n```text\nString s1 = \"MyTest\";\nString s2 = new StringBuilder().Append(\"My\").Append(\"Test\").ToString(); \nString s3 = String.Intern(s2); \nConsole.WriteLine(\"s1 == '{0}'\", s1);\nConsole.WriteLine(\"s2 == '{0}'\", s2);\nConsole.WriteLine(\"s3 == '{0}'\", s3);\nConsole.WriteLine(\"Is s2 the same reference as s1?: {0}\", (Object)s2==(Object)s1); \nConsole.WriteLine(\"Is s3 the same reference as s1?: {0}\", (Object)s3==(Object)s1);\n\n/*\nThis example produces the following results:\ns1 == 'MyTest'\ns2 == 'MyTest'\ns3 == 'MyTest'\nIs s2 the same reference as s1?: False\nIs s3 the same reference as s1?: True\n*/\n```\n\n```text\nn\n```\n\n```text\nString.Format(\"{0:X2}\",n)\n```\n\n========================================\n\nComments:\n- Why should every string be interned?\n- @BoltClock, Why should every literal string be interned?\n- The main reason literals are interned is to save memory space that's otherwise wasted by repeating the same string many times throughout a program, not to mention it's laughably easy to do it. But I can't think of a reason for non-literal strings to be interned.\n- Thanks! excellent blog you got over there... =) Maybe you can answer a related question about string.Empty?\n- Does interning literal strings have *ANY* run-time cost? By my understanding, if six assemblies each use the literal string \"Quack\" at least once, then six instances of the string \"Quack\" will be created--one for each assembly, without the run-time bothering to examine their contents. Because of Reflection, .net must generally assume that it will always be possible for any function to be executed and thus for any literal string to be accessed, so the lifetime of a literal string will be the lifetime of the assembly where it appears. I'd consider \"cheap at runtime\" to be an understatement.\n- @supercat: Your understanding is correct; literal string interning is done on a per-assembly basis. As for your question about the lifetime of such a string, I do not know the answer off the top of my head. The runtime could I suppose deallocate such a string when there are no live references to it, and reconstitute it later; how would you be able to tell that the reference was different? But it could just as easily keep it alive forever.\n- @gdoron: I actually don't know the answer to that question; I have wondered the same thing myself. I'm having lunch with some of the CLR old timers today; I'll see if any of them remember. And I'm glad you like the blog.\n- Wait, so if one assembly has \"Quack\" and another assembly has \"Quack\", they will have two strings that are interned, and have identical contents, but will be different references? I thought the entire point of interning was so that identical contents will be identical references...\n- @configurator: In the case of literals we only guarantee reference identity of usages of that literal in the same assembly. Interning literals across assemblies has a runtime performance cost; if you want to take on that expense then you have to write the code to do it yourself.\n- Oh dear. I think there's a potential bug in my compiler than - I'm using a `Symbol` struct which is simply an immutable wrapper around `string` that calls `Intern` and keeps the result - and uses reference comparisons instead of string comparisons. Does calling `Intern` manually on two strings pre-interned differently across assemblies keep two different copies?\n- @configurator: I have no idea. What an interesting question! I think you'll have to try it and see.\n- After some testing: fortunately for me, this doesn't happen in VS 2010 - strings are interned as equal across assembly boundaries. However, that means I can't check the answer to my question either.\n- That related question got an answer. Is it the right answer?\n- Great answer. I'm so unhappy since you aren't in C# team anymore.\n- The edit queue is full, so I'll comment instead. Here's the non-dead version of the link to Eric's blog post (linked in this answer): ericlippert.com/2009/09/28/string-interning-and-string-empty\n- @EricLippert: Interning strings wouldn't require a global weak map. If each string held an initially-blank reference to a string with the same content which ranked higher on some metric (perhaps age), then strings could be compared by chasing down the references to the highest-ranking equivalent strings. Two strings with the same \"best\" equivalent could be considered equal without having to examine their contents; otherwise, if two strings were examined and found to be equal, one of the \"equivalent\" strings could be set to be an equivalent of the other.\n- @EricLippert: For such a design to avoid memory leaks it would probably be necessary to have GC support (so that if a string has a reference to a better string, any \"better string\" reference which identifies it would be changed to identify the better one) but such a design could in many usage scenarios offer noticeable benefit without needing *any* kind of hash map, and could work quite nicely with per-thread hash maps (note that if `x` and `y` are found to be equal, and `a` and `b` likewise, finding `x` equal to `a` would imply it was equal to `b`, and that `y` was equal to both `a` and `b`.\n- @EricLippert: Consequently, if each thread had its own hash map, two threads which both created the string \"Fred\" would start out holding separate references, but both would get interned to the same backing store as soon as any \"Fred\" from one thread was compared to any \"Fred\" from the other.\n- @supercat, Interning has to be explicit because the Java language requires that `new X()` is distinct from all references that existed prior, and string has a public constructor.\n- @MikeSamuel: Interning of *explicitly exposed* references must be explicit, but objects hold references to backing stores which are *neither* modified nor exposed to outside code, those backing stores could be interned implicitly. For example, in Java given `String A=\"foo\", B=new String(A);`, `B` would be a new `String` object, but both `A` and `B` would hold references to the same `char[]`. Interestingly, the implementation of `String.equals()` I looked at (I think v7) doesn't check for reference equality of the `char[]` instances even though two distinct string objects could...\n- ...be bound to the same backing store. I would guess that having `String.equals` merge `char[]` arrays for long strings that turned out to be equal could be a performance win if the memory model could support it, were it not for the existence of code that uses Reflection or native methods to manipulate the backing stores of `String` objects. If it had always been recognized that a `String` object could legitimately change among equivalent backing stores and thus modifying the `char[]` instances was 100% illegitimate, that could be a useful optimization.\n- @supercat, Yeah. I think you understand the reflection problems; it's unfortunate, but all backing stores are explicitly visible and mutable in the presence of `java.lang.reflect.Field.setAccessible`. If this topic of string interning interests you, you might look at attempts to address the umbrella group of problems: value types for Java.\n- @MikeSamuel: I really wish Java had made `string` behave as a primitive type, most likely implemented as a reference which could only be dereferenced using native methods. To be useful, it would need to allow member-style method invocation, something other primitives don't, but IMHO all primitives should have allowed that anyway. IMHO, `someLong = someFloat.roundLong()` would be much cleaner and less trouble-prone than `someLong = Math.round((double)someFloat);`.\n- @MikeSamuel: Having `string` be a primitive type would have allowed the `==` operator to consistently test equality, and would also have allowed improved performance in many cases. At present, accessing a character from a string requires two levels of indirection; if a `string` variable could either hold a `char[]`, a `byte[]` or some other representation, then in the case where it held a `byte[]` or `char[]` accessing a character would require two reads from the same base and two \"constant\" compare-to-constant operations.","metadata":{"transformedAt":"2026-08-18T18:32:17.750Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":92,"estimatedTokens":2336}}65{"id":"stack-913671","source":"stackoverflow","questionId":913671,"title":"Are there Lisp native code compilers?","tags":["compiler-construction","lisp"],"text":"Title: Are there Lisp native code compilers?\nTags: compiler-construction, lisp\nSource: Stack Overflow\n\nQuestion:\nAre there native code compilers for Lisp? To which extent can it even be compiled, with all it's dynamic nature, garbage collection, macros and what else?\n\n========================================\n\nTop Answer:\nThere a many Lisp compilers that compile to native code. CMUCL, SBCL, ClozureCL are known among the open-source Lisp compilers.\n\nGarbage collection is not an obstacle to compilation to native code. Also, in some cases Lisp can use stack allocation that does not need GC and may greatly improve performance (using dynamic-extent declaration; at least SBCL supports this).\n\nMacros (and any code that is run at read-time (read macros and read-eval), compile-time (macros, compiler macros, code in eval-when)) require incremental compilation (first macro-function has to be compiled, and then code that uses macro can be compiled). This somewhat complicates compilation, but it is not too much a problem. Also, macros and compiler macros even help the compilation process because they allow programmer to write code generators and code optimizers, essentially customizing the compiler.\n\nSo the compiler is more complicated than some simpler languages (like C), but complexity is manageable (see Design of CMU Common Lisp).\n\nDynamic nature of Common Lisp is controllable and designed to be effeciently compilable. In contrast to some other dynamic languages (e.g., Python), dynamism is restricted (e.g., you can not take the current lexical environment at run-time) which give compilers some freedom to optimize.\n\n========================================\n\nCode:\n```text\n? (defun foo (x y) (if (= x y) (sin x) (* y (cos x))))\nFOO\n? (disassemble 'foo)\nL0\n  [0]     (leaq (@ (:^ L0) (% rip)) (% fn))\n  [7]     (cmpl ($ 16) (% nargs))\n  [10]    (jne L209)\n  [16]    (pushq (% rbp))\n  [17]    (movq (% rsp) (% rbp))\n```\n\n```text\n[172]   (pushq (@ 77752))\n  [179]   (jmpq (@ 10 (% temp0)))\nL189\n  [189]   (leaq (@ (:^ L0) (% rip)) (% fn))\n  [196]   (jmpq (@ .SPNVALRET))\nL209\n  [209]   (uuo-error-wrong-number-of-args)\nNIL\n```\n\n========================================\n\nComments:\n- And to think just until now I thought LISP was interpreted.\n- Are you talking about Common Lisp, exclusively?\n- @Amit, yes and no. I want to find a way to generate (small enough to be practical) Windows *.exe files to write some one-purpose throw-away utilities, like file converters, which I could then give to someone else at work to use without begging them to install Lisp on their machines (especially since they are still mad at me after I made them install Python). For that purpose I'd prefer Common Lisp as it seems more \"practical\". On the other hand, I'd like to know about Scheme (or whatever) compilers as well, to see the big picture.\n- Are your real question(s): Are there ways to package lisp code as an OS native .exe (for whatever OS you are implementing)? Are there ways to handle exceptions such that the user is never exposed to Lisp?\n- iro.umontreal.ca/~gambit Gambit Homepage ccs.neu.edu/home/will/Larceny Larceny Homepage Gambit compiles to C, Larceny compiles either to C, machine code or the CLR.\n- I had tried several compilers some time ago. What I was able to do with them was something like ~10 MB for a \"Hello, World!\" Windows .exe file, which shows you a Lisp prompt if there's a runtime error. So it looks like the whole Lisp system was dragged along with it, which makes you think it's not really too \"native\", maybe...\n- Hmm, java drags the whole java runtime, .net drags the whole clr, c++ drags the whole c++rt, lisp drags the whole lisp runtime. It's just the same thing — almost every language requires its own runtime. Lisp's runtime is big because of its flexibility (the same is true for jvm and clr). However, the code is still native: i.e., instructions are not for VM, but for the target CPU.\n- I think some lisps provide a way of dumping images with some features removed (like the compiler, if it's not used). perhaps that might help in shrinking it a bit?\n- And if something is changed during runtime, what happens? Does Lisp generate new version of the machine code, or the changes only affect it's \"internal data structure layout\"?\n- Depends on the changes. Some may not require a compiler, some may use the internal compiler, some will just use newly loaded code (to replace or extend existing code), some may not need a compiler. All variants are possible.\n- Why the Downvotes? The OP wanted to know about scheme as well, and chicken is a native compiler.","metadata":{"transformedAt":"2026-08-18T18:32:17.750Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":60,"estimatedTokens":1151}}66{"id":"stack-8833524","source":"stackoverflow","questionId":8833524,"title":"What are the stages of compilation of a C++ program?","tags":["c++","compiler-construction","compilation","c++-faq"],"text":"Title: What are the stages of compilation of a C++ program?\nTags: c++, compiler-construction, compilation, c++-faq\nSource: Stack Overflow\n\nQuestion:\nAre the stages of compilation of a C++ program specified by the standard?\n\nIf so, what are they? \n\n**If not, an answer for a widely-used compiler** (I'd prefer MSVS) **would be great.**\n\nI'm talking about preprocessing, tokenization, parsing and such. What is the order in which they are executed and what do they do in particular?\n\nEDIT: I know what compilation, linking and preprocessing do, I'm mostly interested in the others and the order. Explanations for these are, of course, also welcomed since I might not be the only one interested in an answer.\n\n========================================\n\nTop Answer:\nThe 9 so-called \"phases of translation\" are listed in the standard in `[lex.phases]` (2.2 in C++11, 2.1 in C++03).\n\nThe detail demanded in the standard varies: preprocessing is split up into several phases, because it's important at various points in the standard exactly what has \"already been done\" and what is \"left to do\" when a particular bit of behavior is defined. So although it doesn't tell you how to write a lexer, it gives you a pretty clear roadmap.\n\nLinking on the other hand is left mostly to the implementation to decide how it's actually achieved, because the standard doesn't care *how* a given name is looked up, just what it refers to.\n\nIt doesn't give any detail on parsing, either, it just says \"The resulting tokens are syntactically and semantically analyzed and translated\". That's because the whole of chapters 3-15 are required to fill in that detail.\n\nIt doesn't mention internal representations during parsing/translation at all, and neither does it mention optimization phases -- they're important to the design of compilers, but they're not important to the standard. Optimization can occur in different places in different compilers. For a long time, optimization was almost entirely in the compilation phase, before emitting object files, and linkers were dumb as a post. I think now serious C++ implementations can all do at least some optimization across multiple TUs. So \"the others\" aren't just left out of the standard, they do actually change over time.\n\n========================================\n\nCode:\n```text\n[lex.phases]\n```\n\n========================================\n\nComments:\n- Here's the GCC internals manual's page. I thought it used the language you wanted but evidently not. If you look in the GCC source then there's loads of individual optimisation passes. I'd guess no, the standard specifies what it needs to achieve not how it must do it, and that your best bet would be an academic compiler construction course or textbook - I'm sure there's plenty around.\n- @sharptooth I rolled back the question - I believe this title can much more easily be found by people seeking the same thing.\n- @Luchian Grigore: Okay, I just though the title change really mattered - \"how it usually done\" v \"what the Standard says about how it should be done\". Anyway it's your question, you decide.\n- Anything beyond that mentioned explicitly in the standard seems like an implementation detail that shouldn't really matter. Is this purely for curiosity, or are you trying to solve a problem?\n- Denial, anger, bargaining, depression, acceptance.\n- FWIW, in gcc phase 6 isn't part of preprocessing (`gcc -E` doesn't concatenate adjacent string literals). I don't have another compiler to hand to compare.\n- A footnote in this page says: \"Implementations must behave as if these separate phases occur, although in practice different phases might be folded together\".\n- @rodrigo: I already quoted that footnote. You probably missed it because I didn't put it at the bottom of the quote. I've just moved it there.","metadata":{"transformedAt":"2026-08-18T18:32:17.750Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":46,"estimatedTokens":951}}67{"id":"stack-1085490","source":"stackoverflow","questionId":1085490,"title":"How do C/C++ compilers work?","tags":["c++","c","compiler-construction","compiler-optimization"],"text":"Title: How do C/C++ compilers work?\nTags: c++, c, compiler-construction, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nAfter over a decade of C/C++ coding, I've noticed the following pattern - very good programmers tend to have detailed knowledge of the innards of the compiler. \n\nI'm a reasonably good programmer, and I have an ad-hoc collection of compiler \"superstitions\", so I'd like to reboot my knowledge and start from the basics.\n\nCan anyone recommend links to online resources or favorite books? I'm particularly interested in C/C++ compiling, optimization, GCC and LLVM.\n\n========================================\n\nTop Answer:\n- GCC Internals Manual.\n\n- CPP Internals Manual\n\n- LLVM Documentation\n\n========================================\n\nCode:\n```text\nlcc\n```\n\n```text\nlcc\n```\n\n========================================\n\nComments:\n- Take a look here for resources: stackoverflow.com/questions/1669/learning-to-write-a-compile&zwnj;&#8203;r\n- GCC has an \"internals\" manual that documents some specific internal details, like its machine-description files, and the data structures it uses to represent function logic, and GIMPLE and RTL internal representations: gcc.gnu.org/onlinedocs/gccint. But it's not really an overview of how it works.\n- what is the actual question here? is it \"how do compilers work\"? or \"please recommend me a learning resource about how compilers work\"?\n- I was thumbing through the GCC internals manual, it doesn't seem useful for \"Learning\" how a compiler works. It's not a teaching document it assumes that you already have a knowlege of the subject.\n- Thanks, nice sequence. I take the dragon book is : en.wikipedia.org/wiki/index.html?curid=188976\n- Yes, that is the dragon book. I read the 1st edition. It had a much simpler dragon....\n- Gah. People keep recommending this. Not me. Start with a casual introduction---say \"Let's build a compiler\"---then look at a Computer Sciencey reference with all the math and theory.\n- I'd recommend against trying to understand GCC. It's fairly unusual as far as compilers go, and its architecture is poor by design (as in, the design is crippled on purpose. Yes, I'm serious. No, I'm not just making a joke at GCC's expense).\n- When it comes to understanding what you are doing, LEX and YACC just add an extra layer of technology that obscures your view of what's going on. If the goal is UNDERSTANDING how a compiler works a recursive decent parser will give you a better understanding than using LEX and YACC, and generally speaking if you're just doing it as a learning exercise you are probably not going to write a optimising compiler in your free time without someone else helping you.\n- If you are interested in compiler optimizations only then you can try SUIF\n- Thanks for the tip - I will check lcc out\n- Brillant Engineers? Jack Crenshaw designed parts of the space shuttle, and home made computers were a HOBBY of his. Not to dispute the intellect of folks who wrote lcc, but you don't have to be brilliant to design a compiler. It's really not that hard.\n- The reference was not to Crenshaw but to gcc. RMS is many things, but brilliant engineer is not one of them. Then add 1000 monkeys and stir well...\n- I doubt that claim, considering that LLVM does not even support making C library calls. That's something you must implement in your frondend and that for every platform you ant to support. E.g. Clang has an implementation and so has Rust (Rust also uses LLVM as backend). This alone takes weeks to implement and without you cannot call any external system API. If you want a backend that is easy to use and that does offer you C API calls, check out QBE: c9x.me/compile","metadata":{"transformedAt":"2026-08-18T18:32:17.751Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":48,"estimatedTokens":920}}68{"id":"stack-436300","source":"stackoverflow","questionId":436300,"title":"Are constant C expressions evaluated at compile time or at runtime?","tags":["c","optimization","compiler-construction","standards","c-preprocessor"],"text":"Title: Are constant C expressions evaluated at compile time or at runtime?\nTags: c, optimization, compiler-construction, standards, c-preprocessor\nSource: Stack Overflow\n\nQuestion:\nIf I write a *#define* that performs an operation using other preprocessor constants, is the final value computed each time the macro appears at runtime? Does this depend on optimizations in the compiler, or is it covered under a standard?\n\nExample:\n\n```\n#define EXTERNAL_CLOCK_FREQUENCY 32768\n#define TIMER_1_S EXTERNAL_CLOCK_FREQUENCY\n#define TIMER_100_MS TIMERB_1_S / 10\n```\n\nWill the operation *32768 / 10* occur at runtime every time I use the TIMER_100_MS macro?\n\nI would like to avoid the following:\n\n```\n#define EXTERNAL_CLOCK_FREQUENCY 32768\n#define TIMER_1_S EXTERNAL_CLOCK_FREQUENCY\n#define TIMER_100_MS 3276\n```\n\n### Summary\n\nA compiler is required to be able to evaluate constant integral expressions because they are necessary for calculating things like array sizes at compile time. However, the standard only says they \"can\" -- not \"must\" -- do so. Therefore, only a brain-dead compiler would not evaluate a constant integral expressions at compile time, but a simple check of the assembly output for an unconventional compiler would verify each case.\n\n========================================\n\nTop Answer:\nMost answers in here focused on the effect of the macro substitution. But i think he wanted to know whether\n\n```\n32768 / 10\n```\n\nis evaluated at compile time. First of all, that is an arithmetic constant expression, and in addition a integral constant expression (because it has only got literals of integer type). The implementation is free to calculate it at runtime, but it must also be able to calculate it at compile time, because\n\n- it must give a diagnostic message if a constant expression is not representable in the type that its expression has\n\n- such expressions are allowed in contexts that require the value at translation time, for example if used as the size of an array dimension.\n\nIf the compiler can principally calculate the result already at compile time, it should use that value, and not recalculate it at runtime i think. But maybe there is some reason still to do that. I wouldn't know. \n\n**Edit**: I'm sorry i've answered the question as if it were about C++. Noticed today you asked about C. Overflowing in an expression is deemed as undefined behavior in C, regardless of whether it happens in a constant expression or not. The second point is also true in C, of course. \n\n**Edit**: As a comment notes, if the macro is substituted into an expression like `3 * TIMER_100_MS`, then this would evaluate `(3 * 32768) / 10`. Therefore, the simple and direct answer is *\"No, it would not occur at runtime every time, because the division may not occur at all because of precedence and associativity rules\"*. My answer above assumes that the macro is always substituted such that the division actually happens.\n\n========================================\n\nCode:\n```text\n#define EXTERNAL_CLOCK_FREQUENCY    32768\n#define TIMER_1_S                   EXTERNAL_CLOCK_FREQUENCY\n#define TIMER_100_MS                TIMERB_1_S / 10\n```\n\n```text\n#define EXTERNAL_CLOCK_FREQUENCY    32768\n#define TIMER_1_S                   EXTERNAL_CLOCK_FREQUENCY\n#define TIMER_100_MS                3276\n```\n\n```text\nTIMER_100_MS\n```\n\n```text\n32768 / 10\n```\n\n```text\n32768 / 10\n```\n\n```text\n#define identifier token-sequence\n```\n\n```text\nTIMERB_100_MS\n```\n\n```text\n32768 / 10\n```\n\n```text\n#include <stdlib.h>\n#include <stdio.h>\n\n#define EXTERNAL_CLOCK_FREQUENCY    32768\n#define TIMER_1_S                   EXTERNAL_CLOCK_FREQUENCY\n#define TIMER_100_MS                TIMER_1_S / 10\n\nint main(int argc, char **argv)\n{\n  printf(\"%d\\n\", TIMER_100_MS);\n\n  return(0);\n}\n\ngcc -S test.c\ncat test.s\n\n...\n    popl    %ebx\n    movl    $3276, 4(%esp)\n    leal    LC0-\"L00000000001$pb\"(%ebx), %eax\n    movl    %eax, (%esp)\n    call    L_printf$stub\n...\n```\n\n```text\n#define TIMER_100_MS      TIMERB_1_S / 10\n```\n\n```text\n#define TIMER_100_MS      (TIMERB_1_S / 10)\n```\n\n```text\ni = 10 * TIMER_100_MS;\n```\n\n```text\n32768 / 10\n```\n\n```text\n3 * TIMER_100_MS\n```\n\n```text\n(3 * 32768) / 10\n```\n\n```text\n/\n```\n\n========================================\n\nComments:\n- note that `TIMERB_1_S &#47; 10` is `3276`, not `3277`\n- I'm totally confused now. You post a reply which contradicts this (says they \"can\" be evaluated at translation time, not they \"must\" be.\n- @David: the macro will be EXPANDED at compile time (how else could it be), but there is no *guarantee* that the expression \"TIMERB_1_S / 10\" (ie the division) is not evaluated at runtime.\n- I don't see how that is relevant. If that answers the question that was being asked, the question needs to be rewritten.\n- Using this technique, you can show that even complicated math operations, like `log(3.0)` will get translated into inline immediate values, although the disassembly will encode `log(3.0)` as `4607626529066517259` which is the 64-bit integer cast of the equivalent IEEE double...\n- This is the key point. The preprocessor manipulates text, then the compiler gets it and knows *nothing* about how much preprocessing has gone before...\n- Even if the preprocessor would do so, it can't do so as long it isn't defined this way: `#define TIMER_100_MS (TIMERB_1_S &#47; 10)` Since you don't paranthese the expression, It would be wring to evaluate it to `3277` where you might have written `1 &#47; TIMER_100_MS` (Where the result anyway wouldn't be what is wanted I guess) But if the preprocessor had evaluated the statement here allready the it would even break the operator priority order.\n- Thanks for citing your source, it's a valuable resource.\n- Very good point and it's actually a critical difference: You might want to do x % TIMER_100_MS, and without parenthesis this will first take mod of x by TIMERB_1_S and then divide the result by 10. Had such a bug before when implementing a circular buffer :)\n- Strictly speaking, this is wrong. Because of the macro substitution, you cannot know what this evaluates to since it matters what appears next to it after the substitution. For example, `3 * 32768 &#47; 10` may be different than `32768 &#47; 10 * 3` in an integer expression due to order of operations going left to right. If the macro definition surrounded the expression with `()`, then the answer would be true.\n- @Brick but in that case it is not an \"operation 32768 / 10\". The question however was about the \"operation 32768 / 10\" only. So my answer says nothing about the \"3 * 32768 / 10\" case because it is \"(3 * 32768) / 10\". My answer is true about \"32768 / 10 * 3\" and the other cases where there is such an operation.\n- This is the question, copied directly: \"Will the operation 32768 / 10 occur at runtime every time I use the TIMER_100_MS macro?\" It's definitely about the use of the macro, and the macro evaluates without the parenthesis. You cannot know if this simplifies or not unless you know every place that the macro is used.\n- Constant Folding! that's what I was looking for.\n- The result of integer division is well-defined. Where does floating-point math come into play?","metadata":{"transformedAt":"2026-08-18T18:32:17.751Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":161,"estimatedTokens":1790}}69{"id":"stack-42227535","source":"stackoverflow","questionId":42227535,"title":"Are modern C++ compilers able to avoid calling a const function twice under some conditions?","tags":["c++","optimization","compiler-construction","call"],"text":"Title: Are modern C++ compilers able to avoid calling a const function twice under some conditions?\nTags: c++, optimization, compiler-construction, call\nSource: Stack Overflow\n\nQuestion:\nFor instance, if I have this code:\n\n```\nclass SomeDataProcessor\n{\npublic:\n bool calc(const SomeData & d1, const SomeData & d2) const;\nprivate:\n //Some non-mutable, non-static member variables\n}\n\nSomeDataProcessor sdp;\nSomeData data1;\nSomeData data2;\n\nsomeObscureFunction(sdp.calc(data1, data2),\n sdp.calc(data1, data2));\n```\n\nLet's consider the **potentially** equivalent code:\n\n```\nbool b = sdp.calc(data1, data2);\nsomeObscureFunction(b,b);\n```\n\nFor this to be valid, the `calc()` function should meet some requirements, and for the example I call the property `_pure_const_formula_`\n\nA `_pure_const_formula_` would:\n\n- Not change any member, static or global variable state\n\n- Call only `_pure_const_formula_` functions\n\n- Maybe some other conditions that I don't have in mind\n\nFor instance, calling a random number generator would not fit these requirements.\n\nIs the compiler allowed to replace the first code with the second one, even if it needs to dig recursively into called functions? Are modern compilers able to do this?\n\n========================================\n\nTop Answer:\n**Yes, absolutely.**\n\nCompilers do this all the time, and more.\n\nFor example, if all your function did were return `true`, and its definition were visible to the compiler at the callsite, the entire function call would probably be elided, resulting in just:\n\n```\nsomeObscureFunction(true, true);\n```\n\nA program for which the compiler has sufficient information may be \"optimised\" from a quite complex chain of tasks down to perhaps one or two instructions. Now, actually operating on member variables is pushing the optimiser to its limit to some degree, but if the variables are `private`, are given a known initial value, and are not mutated by any other member function, I don't see why a compiler couldn't just inline its known value if it wanted to. Compilers are very, very smart.\n\nPeople think that a compiled program is a one-to-one mapping of lines in your source code, but this is almost never true. The entire purpose of C++ is that it is an *abstraction* of what your computer's actually going to be doing when it runs your program.\n\n========================================\n\nCode:\n```text\nclass SomeDataProcessor\n{\npublic:\n    bool calc(const SomeData & d1, const SomeData & d2) const;\nprivate:\n    //Some non-mutable, non-static member variables\n}\n\nSomeDataProcessor sdp;\nSomeData data1;\nSomeData data2;\n\nsomeObscureFunction(sdp.calc(data1, data2),\n                    sdp.calc(data1, data2));\n```\n\n```text\nbool b = sdp.calc(data1, data2);\nsomeObscureFunction(b,b);\n```\n\n```text\ncalc()\n```\n\n```text\n_pure_const_formula_\n```\n\n```text\n_pure_const_formula_\n```\n\n```text\n_pure_const_formula_\n```\n\n```text\npure\n```\n\n```text\n__attribute__((pure))\n```\n\n```text\nstrlen\n```\n\n```text\nconst\n```\n\n```text\nconst\n```\n\n```text\nsomeObscureFunction(true, true);\n```\n\n```text\ntrue\n```\n\n```text\nprivate\n```\n\n```text\nconst\n```\n\n```text\nconst\n```\n\n```text\nconst\n```\n\n```text\nconst\n```\n\n```text\nthis\n```\n\n```text\nconst\n```\n\n```text\nconst\n```\n\n```text\nconst\n```\n\n```text\npure\n```\n\n```text\nconst\n```\n\n```text\ngcc\n```\n\n```text\npure\n```\n\n```text\nconst\n```\n\n```text\nconst\n```\n\n```text\nconst\n```\n\n========================================\n\nComments:\n- you can find more info here en.wikipedia.org/wiki/Aliasing_(computing)\n- Yes, sorry, I had left some code from a more complex code example...\n- Are we assuming that the definition of `calc` is visible to the compiler, as it would be if whole-program optimization (aka link-time code generation) is available? That makes a big difference here; it changes the answer entirely.\n- If the compiler can see the implementation though inline declaration or LTO then yes it can remove duplicate calls. Otherwise you need to use compiler specific tricks to do it.\n- @ZanLynx and CodyGray: +1billion. LTO (or inline definition) makes questions like this a no-brainer: the answer is then an absolute *yes*: any optimisation is possible, as the function's body can be a candidate for inlining at any call site, with all the possible optimisations that enables.\n- I guess adding `constexpr` can further improve optimization in some cases\n- @Xeverous Which cases? Adding it to where? Surely marking a function as `constexpr` means its definition must be visible in the same translation unit, in which case, it would already be known that it would return the same results both times, and that optimisation would be applied.\n- Rand() would change a global and OP asked if compilers were smart enough to check for changing globals.\n- Yes, I had in mind the RNG case, but this one requires at least a static or global variable, so if the compiler can examine the functions called inside calc() to check if they change a global state, would the compiler be able to do that kind of optimization?\n- OP already said there would be constraints. You just repeated them. If you want to take the code in the question as gospel, literally, verbatim, then you have to throw away half the question prose and report that the program won't even compile, let alone link, let alone be optimised.\n- `no modern compiler` Are you deliberately using a restrictive definition of *compiler* to not include link-time optimisation? Because **all** of the major modern compiler toolchains trivially support LTO, which means the definition of the function can be considered for optimisation at every call site. (Of course, even those that don't could optimise this if the function is declared in the header and they can see its lack of other side effects.)\n- There are modern compilers (clang, for example) that can often tell that the proposed optimization has no observable differences and do in fact optimize away the second function call. Of course any compiler that makes this optimization where it's illegal is broken, but there's no reason a compiler can't make the optimization where it is legal. (And there are dozens of examples of people assuming compilers would never be smart enough to make particular optimizations and then getting badly burned when they were.)\n- I had in mind the RNG case, but this one requires at least a static or global variable, so if the compiler can examine the functions called inside calc() to check if they are\n- *\"invoking this method multiple times with the same input parameters might give different results\"* Right, but the compiler can often see when that's not the case.\n- @LightnessRacesinOrbit Yes, but then again in most of these cases, the compiler doesn't even need the const keyword.\n- Of course, a `mutable` field would screw things up further too\n- @Axel: Yes, indeed.\n- @MarkKCowan: And let's not forget `volatile`!\n- On a trolling note, if the compiler memoises `random()` which `returns a randomly generated number`, giving it the same (yet still technically \"randomly-generated\") result for every invocation during execution, but a different value for each execution, is the program still satisfying its specification? XKCD did something along these lines a while back...\n- @MarkKCowan xkcd.com/221/\n- Aha, thanks for this very useful answer. I had exactly that kind of idea in mind, ie a somewhat expensive .depth() tree iterator member function.\n- Keep in mind that even with the `pure` attribute, gcc would only elide redundant calls if it can determine through static analysis that they're indeed called with the same parameters. If the compiler cannot do this, you can still eliminate redundant calls through memoization, but a C/C++ compiler won't do that automatically for you.\n- Pure function determination is an extremely common compiler optimization, in most languages. It's difficult in C (mostly due to pointers) but even GCC will attempt to do it with the compiler flag `-Wsuggest-attribute=pure` (which emits a warning when it thinks a function can be marked `pure`)\n- How is `strlen` pure? If you call it twice with the same argument you may get 2 different results.\n- @nwp you're modifying the string so it's not the same argument. The pointer is the same, but the data it points to had been modified.\n- According to the documentation modifying the string is allowed. The condition you linked to is *\"declare that a function [...] returns a value depending only on its arguments (pure)*\", so besides that anything goes. Is that a bug in the documentation or am I just reading it wrong?\n- @nwp: I think it's intended to be interpreted as 'depending only on its arguments or any memory (transitively) accessible through its arguments'. Otherwise it wouldn't even be able to calculate the string length at all.\n- @nwp : Joren is right, the documentation refers to any data that is accessible from the passed arguments. For example passing a pointer to a global variable won't ever be elided, because the compiler cannot make sure that the global is not modified by some other thread.\n- Regarding threading and globals -- compilers are indeed allowed to assume that other threads don't modify globals, absent insertion of memory barriers and whatnot.\n- You are assuming that the function is defined inline. However, the function declared and used in the OP is most certainly not defined inline. And the optimizer is powerless in estimating purity of functions for which it cannot see any source code. So \"Yes, absolutely\" is absolutely false, both in general, and especially in the context of the question.\n- @cmaster: Read the answer again. *\"if all your function did were return true, **and its definition were visible to the compiler at the callsite**, the entire function call would probably be elided\"* I'm not \"assuming\" anything!\n- @cmaster: *\"However, the function declared and used in the OP is most certainly not defined inline.\"* It doesn't compile, either.\n- @cmaster In addition to Lightness’ other observations, link-time optimisation can cause the function be inlined even across object boundaries.\n- I've seen clang do a number of optimizations that people never thought compilers would be able to do (sometimes with horrible results due to people relying on that belief) with link-time optimization.\n- @cmaster MSVC, Clang and GCC have all supported LTO for some years now, so notions like \"defined inline\" are rapidly becoming dated from an optimization point of view.\n- @KonradRudolph Arguably LTO means that at the \"compiler\" gets to examine the definition at link time at the callsite.\n- @Leushenko: Plus, \"defined inline\" was always irrelevant if the definition was visible in the same TU.\n- Well, there are a lot of people who still link without link time optimizations, as link time optimizations tend to be irrelevant with well written code (= no insane repeated calls across compilation unit boundaries within the inner loop of the application). If you want to base your answer on link time optimizations being switched on, that's fine, but then you should say so explicitly.\n- @cmaster: I didn't base my answer on that, as I've explained a few times now\n- This isn't kindergarten; the constraints are clearly labelled. I see no reason to turn the answer into lego! The question literally asks for these conditions, and I have provided them. All one must do is *read*.","metadata":{"transformedAt":"2026-08-18T18:32:17.754Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":29,"totalLines":238,"estimatedTokens":2833}}70{"id":"stack-41415006","source":"stackoverflow","questionId":41415006,"title":"Which contemporary computer languages are LL(1)?","tags":["compiler-construction","formal-languages","ll-grammar"],"text":"Title: Which contemporary computer languages are LL(1)?\nTags: compiler-construction, formal-languages, ll-grammar\nSource: Stack Overflow\n\nQuestion:\n(I am spending the holiday time on some language theory. Excuse me if this is a naive question.)\n\nAccording to here:\n\n LL grammars, particularly LL(1) grammars, are of great practical\n interest, as parsers for these grammars are easy to construct, and\n many computer languages are designed to be LL(1) for this reason.\n\nSo, out of curiosity, which contemporary computer langauges are LL(1) ? Does C, Java ,C# or Python fall into this category?\n\n========================================\n\nTop Answer:\nIf I take \"computer languages\" more broadly than \"programming languages,\" I believe there are several declarative languages you could consider, though it may also depend on what you consider contemporary.\n\nPossible candidates:\n\n- XML\n\n- LLVM IR\n\n- many configuration languages, like INI files\n\n- some network protocols\n\n- some data formats, like CSV files\n\nMost (all?) flavors of the languages that describe regular expressions are not regular expressions but are LL(1).\n\nFor actual *programming* languages, these are all probably too old to be considered contemporary, and many have common extensions that probably violate LL(k).\n\n- assembly languages\n\n- BASIC\n\n- LISP\n\n- The Wirth languages: Pascal, Modula, Oberon\n\n- Prolog\n\nI don't know all of the above languages well enough to be conclusive, which is why I suggest they are *possible* candidates. If you know of an syntactic ambiguity in any of these, please educate me in the comments.\n\n========================================\n\nCode:\n```text\n(PREFIX* OPERAND POSTFIX*) (INFIX PREFIX* OPERAND POSTFIX*)*\n```\n\n```text\noperand    => IDENTIFIER | CONSTANT | '(' expression ')'\nterm       => operand | PREFIX operand | operand POSTFIX\nexpression => term | term INFIX expression\n```\n\n```text\n(\n```\n\n```text\n)\n```\n\n```text\na+b*c\n```\n\n```text\na*b+c\n```\n\n```text\n+\n```\n\n```text\na-b-c\n```\n\n```text\n((a-b)-c)\n```\n\n```text\n(a-(b-c))\n```\n\n```text\n(x)*(y)\n```\n\n```text\nx\n```\n\n========================================\n\nComments:\n- I did read a very interesting article on exactly this topic [and using grammars ans regex], and had thought I'd bookmarked it but unfortunately it seems not. I seem to think Perl is up there. (sorry for this all round unhelpful comment)\n- @Martin Perl is definitely not LL(1). Parsing Perl is undecidable.\n- It's the invert then, Perl is at the bottom (LL4?). Interestingly the only language written by a linguist and so it's meant to be much easier to write as a human, than read as a machine (comparatively speaking).\n- A better place to ask this question would be Lambda the Ultimate. See related question Good languages with simple grammar.\n- @martin: the `(k)` in `LL(k)` refers to the number of lookahead tokens you might need to look at in order to decide what to do with the current token. Many languages are not `LL` at all, and in practise, it rarely helps to increase the value of `k`, although you can increase the power of `LL` parsing if you allow `k` to be unbounded (see ANTLR, for example). In that case, the parser is no longer linear time and you might want a more powerful algorithm, such as LR.\n- Ahh I completely misunderstood. I was reading previously about the grammar compexity of computer languages and they where on a scale of 1-4 and I think referenced by `LL`, this was the interesting link I couldn't link. Sorry for the confusion.\n- \"The claim that a language is not context-free because it requires identifiers to be declared is perhaps a bit more controversial\" -- Why would something which is trivially true be controversial?\n- @JohnColeman: I'll refrain from the obvious political analogy and leave you to read the comment threads in my posts on the subject. I was also surprised that it was controversial.\n- To me it is sort of like the fact that physical computers are not universal Turing machines because they have finite memory. Trivially true, but perfectly consistent with the fact that Turing machines provide a good way of thinking about physical computers. Similarly *real life* programming languages are almost never context-free, but this trivial truth is perfectly consistent with the fact that context-free grammars provide a good way to think about many such languages.\n- @johncoleman: sure, and a cfg parser is *part* of any c++ parser. But it's not the whole story, by a long shot. (Python, which does not require declarations, really is CF, modulo the layout sensitivity. But a close examination of the grammar used to parse, compared with the grammar used to explain, shows some of the limitations of LL parsing. Perhaps I should add a reference for that.)\n- Your \"technically\" seems based on the failure to distinguish \"the language\" from \"the syntax of the language\". \"Time flies like a banana\" is a syntactically valid sentence, that makes litte sense semantically. That the syntax of a language is LL(1), doesn't require that all syntactically valid program texts are also valid programs. Also, the case that some symbols of a language have a special lexical representation (indents in Python) does not cause the language to not be LL(1). You mention VW-grammars. VW-grammars distinguish the arbitrary representation and grammatical terminal symbols.\n- Further, VW-grammars are rather special, in that they are Turing-equivalent languages, and as such can express the full semantics of a programming language. (as demonstrated by Cleaveland and Uzgalis in their book \"Grammars for Programming Languages\".)\n- @LHP That's what I wondered as well. I'll try to add a bit more explanations. let's have a really simple language, involving defining integer variables and printing them, each instruction is ended by a \";\" like { var myvar = 10; print myvar2; }. Since any instruction starts by either \"var\" or \"print\", it is LL(1) : each type of expression can be deduced by it's first token. When building the AST, we don't if the var is declared when printing it : PROG[DCL[\"myvar\", 10], PRINT[\"myvar2\"]. Finally, when parsing the AST to build program, we check for existence of variables and return an error.\n- This seems like an extended comment rather than an answer. You're correct that languages like C and C++ are context sensitive because they require semantic information to resolve syntactic ambiguities. But it's a reach to claim that all languages that require identifiers to be declared are context sensitive. For example, Pascal requires declarations, but that's a semantic requirement--those declarations are not necessary to resolve syntactic ambiguities nor to build a parse tree.\n- @adrian: I could add Pascal to the list in point 3, if it's parseable up to declaration checking with an LL grammar (I've only done it with an LR parser, but it's probably in the list). But I stand by my claim that it's not LL in a formal language sense because inputs with undeclared variables are not well-formed. You an construct a parse tree for the pseudo-English \"*All mimsy was the borogoves\" but I think I'm not the only one who would say that's a grammatical and not a semantic error, without knowing what a borogove is or how to recognise its mimsiness...\n- In part, that's the point of the rhyme, and isalso what Chomsky was trying to express with the grammatically correct utterance about colorless green ideas. In the same way, I don't actually need to know anything about the semantics of a Pascal program to observe that an identifier is not in scope. To my mind that's a lot more similar to subject-verb disagreement (syntactic) than semantic violations like furious sleep. As I said, I accept that not everyone will approach syntax from this perspective but it's definitely the way I learned the terms as a mathematical linguist, and I think...\n- it's a valid answer. But I did write point 3 with the objection you raise in mind. Nothing in the answer implies that LL or LR parsers aren't useful. Just that they cannot capture the entirety of \"well-formed\", (which is not the same as \"could blow up at runtime wiith the wrong input\").\n- Fwiw, I think the criticism of LL in point 2 should be sufficient independent of grammatical agreement.\n- @rici: Is it even possible for a finite generative grammar alone to \"capture the entirety of 'well-formed'\" for a Turing complete programming language? The C++ Standard, for example, defines a well-formed program as a \"C++ program constructed according to the syntax rules, diagnosable semantic rules, and the one-definition rule.\" Is it even possible to express something like the one-definition rule in a grammar? Or is it an inherently semantic constraint? (BTW, I consider the sentences about borogroves and green ideas to be grammatically correct nonsense.)\n- @adrian: sure, generative grammars are turing complete. If you restrict them to a single non-terminal on the left-hand side, then C++ is impossible but you could define well-formed awk without too much trouble. As for the borogoves, at least in my idiolect, the version I provided weren't grammatical, likes these sentence. I think that's a common view of English grammar, and it's what Chomsky hoped to achieve.\n- If I understand you correctly, you are saying that \"PROGRAM p; BEGIN q(a); x := f(y) END.\" is not a valid Pascal program, as it does not declare any of its identifiers except the program name, and *therefore* also not a grammatically correct Pascal program? If so, I believe your view is rather uncommon, or at least that it used to be so. Also you do need to know Pascal semantics to assess this: \"PROGRAM p(output); BEGIN writeln END.\" writeln is an identifier, it is not declared, has a well-defined meaning here, but could have been redeclared as something else like any other identifier.\n- @lhp: your example is certainly not a valid Pascal program, right? It is not necessary (or possible) to start to execute it to determine that it's not valid; the error is purely textual, or \"static\" in the words of the Pascal report. A language, in formal language theory, is just the set of texts which satisfy the language's definition, and that set cannot be generated with a CFG. There is a context-free superset, but that's always the case, since Σ* is regular and a superset of any language over Σ. So the existence of a CF superset is not mathematically interesting.\n- Also, i don't require the semantics to validate a program which uses writeln. I just need the implicit prologue, which can be written as a text string containing a sequence of Pascal declarations. It's still textually verifiable and this static rather than dynamic.\n- My examples certainly are grammatically valid Pascal programs. Both of them. The first isn't executable, because the identifiers are not defined, but saying it isn't valid would be like saying \"Santa brings presents for Christmas\" isn't an English sentence because Santa does not exist. And that \"implicit prologue\" you mention can not \"be written as a text string containing a sequence of Pascal declarations\", as the declaration for writeln cannot be written in Pascal. And if we can assume such a prologue, it could just as well include definitions of q, a,x, f and y...\n- The clean split into a siloed lexer and parser is often not possible, and counter productive. LALR(1) can be made more expressive by having the parser give lexer the set of accepted tokens each time it needs to consume a token - C is then LALR(1). A post-lex stage, where the lexer output is post-processed, can generate INDENT/DEDENT tokens, adjust token positions for based on line splicing, mark identifiers as typenames, etc. The purely theoretical \"is this LL\" doesn't mean a recursive descent parser won't parse the language. It just won't be \"pure\". Lexer-parser collab goes a long way.\n- @rici The error here was trying to liken the grammar of programming languages to that of natural languages. When you write a compiler, you split the tasks between syntax and semantic analysis to avoid unnecessary complexity: that Pascal program is syntactically correct, but it's semantically incorrect. If you try to do both in the syntax analysis stage, the code complexity and coupling will grow significantly, and you'll also have to do multiple passes for the most trivial of features. Point 1 isn't even a \"technically\", it's a \"rhetorically\".","metadata":{"transformedAt":"2026-08-18T18:32:17.754Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":12,"totalLines":130,"estimatedTokens":3093}}71{"id":"stack-2525677","source":"stackoverflow","questionId":2525677,"title":"How to write the Visitor Pattern for Abstract Syntax Tree in Python?","tags":["python","parsing","compiler-construction","abstract-syntax-tree","visitor-pattern"],"text":"Title: How to write the Visitor Pattern for Abstract Syntax Tree in Python?\nTags: python, parsing, compiler-construction, abstract-syntax-tree, visitor-pattern\nSource: Stack Overflow\n\nQuestion:\nMy collegue suggested me to write a visitor pattern to navigate the AST. Can anyone tell me more how would I start writing it?\n\nAs far as I understand, each Node in AST would have `visit()` method (?) that would somehow get called (from where?). That about concludes my understanding.\n\nTo simplify everything, suppose I have nodes `Root`, `Expression`, `Number`, `Op` and the tree looks like this:\n\n```\nRoot\n |\n Op(+)\n / \\\n / \\\n Number(5) \\\n Op(*)\n / \\\n / \\\n / \\\n Number(2) Number(444)\n```\n\nCan anyone think of how the visitor pattern would visit this tree to produce output:\n\n```\n5 + 2 * 444\n```\n\nThanks, Boda Cydo.\n\n========================================\n\nTop Answer:\nSee the docs for `ast.NodeVisitor`, e.g. a crude possibility might be:\n\n```\nimport ast\n\nclass MyVisitor(ast.NodeVisitor):\n def visit_BinaryOp(self, node):\n self.visit(node.left)\n print node.op,\n self.visit(node.right)\n def visit_Num(self, node):\n print node.n,\n```\n\nof course this doesn't emit parentheses even where needed, etc, so there's actually more work done, but, it's a start;-).\n\n========================================\n\nCode:\n```text\nRoot\n        |\n       Op(+)\n      /   \\\n     /     \\\n Number(5)  \\\n             Op(*)\n             /   \\\n            /     \\\n           /       \\\n       Number(2)   Number(444)\n```\n\n```text\n5 + 2 * 444\n```\n\n```text\nvisit()\n```\n\n```text\nRoot\n```\n\n```text\nExpression\n```\n\n```text\nNumber\n```\n\n```text\nOp\n```\n\n```text\naccept()\n```\n\n```text\nvisit()\n```\n\n```text\naccept()\n```\n\n```text\nvisit()\n```\n\n```text\nvisit_*()\n```\n\n```text\nimport ast\n\nclass MyVisitor(ast.NodeVisitor):\n  def visit_BinaryOp(self, node):\n    self.visit(node.left)\n    print node.op,\n    self.visit(node.right)\n  def visit_Num(self, node):\n    print node.n,\n```\n\n```text\nast.NodeVisitor\n```\n\n```text\nclass Operation(object):\n    def __init__(self, op, arg1, arg2):\n        self.op = op\n        self.arg1 = arg1\n        self.arg2 = arg2\n    def accept(self, visitor):\n        visitor.visitOperation(self)\n\nclass Integer(object):\n    def __init__(self, num):\n        self.num = num\n    def accept(self, visitor):\n        visitor.visitInteger(self)\n\nclass Float(object):\n    def __init__(self, num):\n        self.num = num\n    def accept(self, visitor):\n        visitor.visitFloat(self)\n    \nexpression = Operation('+', Integer('5'),\n                            Operation('*', Integer('2'), Float('444.1')))\n```\n\n```text\nclass InfixPrintVisitor(object):\n    def __init__(self):\n        self.expression_string = ''\n    def visitOperation(self, operation):\n        operation.arg1.accept(self)\n        self.expression_string += ' ' + operation.op + ' '\n        operation.arg2.accept(self)\n    def visitInteger(self, number):\n        self.expression_string += number.num\n    def visitFloat(self, number):\n        self.expression_string += number.num\n```\n\n```text\nclass PrefixPrintVisitor(object):\n    def __init__(self):\n        self.expression_string = ''\n    def visitOperation(self, operation):\n        self.expression_string  += operation.op + ' '\n        operation.arg1.accept(self)\n        self.expression_string  += ' '\n        operation.arg2.accept(self)\n    def visitInteger(self, number):\n        self.expression_string += number.num\n    def visitFloat(self, number):\n        self.expression_string += number.num\n```\n\n```text\ninfixPrintVisitor = InfixPrintVisitor()\nexpression.accept(infixPrintVisitor)\nprint(infixPrintVisitor.expression_string)\nprefixPrintVisitor = PrefixPrintVisitor()\nexpression.accept(prefixPrintVisitor)\nprint(prefixPrintVisitor.expression_string)\n```\n\n```text\n5 + 2 * 444.1\n+ 5 * 2 444.1\n```\n\n```text\nclass Operation(object):\n    def __init__(self, op, arg1, arg2):\n        self.op = op\n        self.arg1 = arg1\n        self.arg2 = arg2\n    \nclass Integer(object):\n    def __init__(self, num):\n        self.num = num\n\nclass Float(object):\n    def __init__(self, num):\n        self.num = num\n    \nexpression = Operation('+', Integer('5'), \n                            Operation('*', Integer('2'), Float('444.1')))\n```\n\n```text\nfrom functools import singledispatch\n\n@singledispatch\ndef visitor_print_infix(obj):\n    pass\n@visitor_print_infix.register(Operation)\ndef __(operation):\n    return visitor_print_infix(operation.arg1) + ' ' \\\n               + operation.op + ' ' \\\n               + visitor_print_infix(operation.arg2)\n@visitor_print_infix.register(Integer)\n@visitor_print_infix.register(Float)\ndef __(number):\n    return number.num\n```\n\n```text\nfrom functools import singledispatch\n\n@singledispatch\ndef visitor_print_prefix(obj):\n    pass\n@visitor_print_prefix.register(Operation)\ndef __(operation):\n    return operation.op + ' ' \\\n               + visitor_print_prefix(operation.arg1) + ' ' \\\n               + visitor_print_prefix(operation.arg2)\n@visitor_print_prefix.register(Integer)\n@visitor_print_prefix.register(Float)\ndef __(number):\n    return number.num\n```\n\n```text\nprint(visitor_print_infix(expression))\nprint(visitor_print_prefix(expression))\n```\n\n```text\n5 + 2 * 444.1\n+ 5 * 2 444.1\n```\n\n```text\naccept()\n```\n\n```text\nvisit_Type()\n```\n\n```text\n@functools.singledispatch()\n```\n\n```text\naccept()\n```\n\n```text\npass\n```\n\n```text\nsingledispatch\n```\n\n```text\nfunctools.singledispatchmethod()\n```\n\n```text\nfunctools.singledispatch()\n```\n\n========================================\n\nComments:\n- I think your visit_BinaryOp implementation should call `node.left.accept(self)` and `node.right.accept(self)` instead of `self.visit(node.left)` and `self.visit(node.right)`.","metadata":{"transformedAt":"2026-08-18T18:32:17.754Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":32,"totalLines":296,"estimatedTokens":1421}}72{"id":"stack-6211111","source":"stackoverflow","questionId":6211111,"title":"Javascript parser generator","tags":["javascript","parsing","compiler-construction"],"text":"Title: Javascript parser generator\nTags: javascript, parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI've seen some javascript parser generators:\n\nJison (http://zaach.github.com/jison/)\n\nPEG.js (http://pegjs.majda.cz/)\n\nANTLR (http://www.antlr.org/)\n\nJS/CC (http://jscc.jmksf.com/)\n\nDoes any one have any experience with them?\n\nWhat are the pros and cons? Are these libraries ready for production? And performance?\n\n**EDIT**:\n\nI want to parse and run a different language in javascript\n\n========================================\n\nTop Answer:\nI recently wrote this parser combinator library called parsinator.js that supports state and debugging functionality:\n\nhttps://github.com/fresheneesz/parsinator.js\n\nOld Answer:\n\nPEG.js is incredibly easy to work with. Its \"try online\" feature is really useful in learning how to use the system quickly. I'm currently using it in the Rhino environment to parse a language I'm writing. I'm not a big fan of Rhino, but PEG.js sure is nice.\n\n========================================\n\nComments:\n- This question should not have been closed as \"not constructive\". Asking for pros and cons of modules and programming tools is not in any way subjective.\n- I agree that this should not have been marked as \"not constructive\". For example the performance of JavaScript Parser generators can be measured using a benchmark and a benchmark is not subjective...\n- My AGL parser builder is written in Kotlin common, so it can be used on any Kotlin target (JVM, JavaScript, native code, etc). medium.com/@dr.david.h.akehurst/&hellip;\n- I recently wrote this parser combinator library called parsinator.js that supports state and debugging functionality: github.com/fresheneesz/parsinator.js\n- I want to parse and run a different language in javascript.\n- @Atomble There's an online demo of Jison, so it's very easy to get started with it: zaach.github.io/jison/try\n- JavaCC can be used together with GWT to generate JavaScript based parsers as per the following blog : consoliii.blogspot.co.uk/2014/04/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.754Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":44,"estimatedTokens":513}}73{"id":"stack-11397731","source":"stackoverflow","questionId":11397731,"title":"Communication between lexer and parser","tags":["c#","c++","parsing","compiler-construction","lexer"],"text":"Title: Communication between lexer and parser\nTags: c#, c++, parsing, compiler-construction, lexer\nSource: Stack Overflow\n\nQuestion:\nEvery time I write a simple lexer and parser, I stumble upon the same question: how should the lexer and the parser communicate? I see four different approaches:\n\nThe lexer eagerly converts the entire input string into a vector of tokens. Once this is done, the vector is fed to the parser which converts it into a tree. This is by far the simplest solution to implement, but since all tokens are stored in memory, it wastes a lot of space.\n\nEach time the lexer finds a token, it invokes a function on the parser, passing the current token. In my experience, this only works if the parser can naturally be implemented as a state machine like LALR parsers. By contrast, I don't think it would work at all for recursive descent parsers.\n\nEach time the parser needs a token, it asks the lexer for the next one. This is very easy to implement in C# due to the `yield` keyword, but quite hard in C++ which doesn't have it.\n\nThe lexer and parser communicate through an asynchronous queue. This is commonly known under the title \"producer/consumer\", and it should simplify the communication between the lexer and the parser a lot. Does it also outperform the other solutions on multicores? Or is lexing too trivial?\n\nIs my analysis sound? Are there other approaches I haven't thought of? What is used in real-world compilers? It would be really cool if compiler writers like Eric Lippert could shed some light on this issue.\n\n========================================\n\nTop Answer:\nI think there is no golden rule here. Requirements may vary from one case to another. So, reasonable solutions can be different also. Let me comment on your options from my own experience.\n\n\"Vector of tokens\". This solution may have big memory footprint. Imagine compiling source file with a lot of headers. Storing the token itself is not enough. Error message should contain context with the file name and the line number. It may happen that lexer depends on the parser. Reasonable example: \">>\" - is this a shift operator or this is closing of 2 layers of template instantiations? I would not recommend this option.\n\n(2,3). \"One part calls another\". My impression is that more complex system should call less complex one. I consider lexer to be more simple. This means parser should call lexer. I do not see why C# is better than C++. I implemented C/C++ lexer as a subroutine (in reality this is a complex class) that is called from the grammar based parser. There were no problems in this implementation.\n\n\"Communicating processes\". This seems to me an overkill. There is nothing wrong in this approach, but maybe it is better to keep the things simple? Multicore aspect. Compiling single file is a relatively rare case. I would recommend to load each core with its own file.\n\nI do not see other reasonable options of combiming lexer and parser together.\n\nI wrote these notes thinking about compiling sources of the software project. Parsing a short query request is completely different thing, and reasons can significantly differ.\nMy answer is based on my own experience. Other people may see this differently.\n\n========================================\n\nCode:\n```text\nyield\n```\n\n```text\nbool next_token(std::string&,const std::set<char>&)\n```\n\n```text\nstd::string\n```\n\n```text\nstd::set<char>\n```\n\n```text\nnext_token\n```\n\n```text\nvector<string>\n```\n\n```text\n::next_token\n```\n\n```text\nextract_nectar\n```\n\n```text\nsetcontext\n```\n\n```text\n#4\n```\n\n========================================\n\nComments:\n- I must be missing something, what's wrong with having a `GetNextToken()` synchronous function?\n- Write lexerless parsers (e.g., on top of PEGs) and forget about this whole issue.\n- WRT #2, any parser can be implemented as a state machine (your processor is a state machine after all).\n- @DeadMG, still don't get it, I implemented a parser for a full compiler with no `yield return` statement, only a function that returns the next token. It holds the position of the current statement in a field of the parser class (technically it holds the current and previous tokens, and returns previous, so I have lookahead, but that's details).\n- @Blindy: the misunderstanding is on the question side. Your approach and the approach marked as #3 are the same, and as you mention, there is no need for a `yield return` statement at all. There are other equivalent options, like the lexer generating an `iterator` that grabs the next token in `operator++`... but this is again the same approach: the parser requests the next token somehow.\n- @Blindy In C#, the difference between a function that converts a string into a vector of tokens and a function that gets the next token is minimal thanks to the `yield` keyword. You can pretend you have the control flow all the time and just iterate over the string with a for loop and yield return the tokens. In C++, implementing the two different functions requires different \"mindsets\". If you think implementing iterators is easy in C++, you have probably done it a lot of times, and you have probably never used `yield` in other languages to see how easy implementing iterators can actually be.\n- I know that this is an old answer but, I will correct you a little, it is possible to implement a lexer/parser for context senstiv parsing, where we only find the next token when needed. f.x. indentation sensitive parsing, are possible simply by expanding a normal LR parser with checking indentation like lookahead. in respect to error handling, there is no gain parsing all tokens first. if you need some extra tokens you could simply parse lex a little longer undtil you got the desired information, this will most likely enhance both memory usage and performance.","metadata":{"transformedAt":"2026-08-18T18:32:17.754Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":86,"estimatedTokens":1453}}74{"id":"stack-6048194","source":"stackoverflow","questionId":6048194,"title":"Good introductory text about GHC implementation?","tags":["optimization","compiler-construction","haskell","ghc","heap-memory"],"text":"Title: Good introductory text about GHC implementation?\nTags: optimization, compiler-construction, haskell, ghc, heap-memory\nSource: Stack Overflow\n\nQuestion:\nWhen programming in Haskell (and especially when solving Project Euler problems, where suboptimal solutions tend to stress the CPU or memory needs) I'm often puzzled why the program behaves the way it is. I look at profiles, try to introduce some strictness, chose another data structure, ... but mostly it's groping in the dark, because I lack a good intuition.\n\nAlso, while I know how Lisp, Prolog and imperative languages are typically implemented, I have no idea about implementing a lazy language. I'm a bit curious too.\n\nHence I would like to know more about the whole chain from program source to execution model. \n\nThings I wonder about:\n\nwhat typical optimizations are applied?\n\nwhat is the execution order when there are multiple candidates for evaluation (while I know it's driven from the needed outputs, there may still be big performance differences between first evaluating A and then B, or evaluating B first to detect that you don't need A at all)\n\nhow are thunks represented?\n\nhow are the stack and the heap used?\n\nwhat is a CAF? (profiling indicates sometimes that the hotspot is there, but I have no clue)\n\n========================================\n\nTop Answer:\nThis is probably not what you had in mind in terms of an introductory text, but Edward Yang has an ongoing series of blog posts discussing the Haskell heap, how thunks are implemented, etc.\n\nIt's entertaining, both with the illustrations and also by virtue of explicating things without delving into too much detail for someone new to Haskell. The series covers many of your questions:\n\n- The Haskell heap and how thunks are stored - the first post in the series\n\n- Bindings and CAFs\n\n- How the IO monad gets translated into primitives\n\nOn a more technical level, there are a number of papers that cover (in concert with other things), parts of what you're wanting to know.:\n\n- A paper by SPJ, Simon Marlow et al on GC in Haskell - I haven't read it, but since GC often represents a good porton of the work Haskell does, it should give insight.\n\n- The Haskell 2010 report - I'm sure you'll have heard of this, but it's too good not to link to. Can make for dry reading in places, but one of the best ways to understand what makes Haskell the way it is, at least the portions I've read.\n\n- A history of Haskell - is more technical than the name would suggest, and offers some very interesting views into Haskell's design, and the decisions behind the design. You can't help but better understand Haskell's implementation after reading it.\n\n========================================\n\nComments:\n- Not exactly what I'd call introductory, but SPJ's book on the implementation of functional languages is a good read.\n- Uuhh.. For me, it was a deep dig into the big pile of papers published by the researchers of GHC. If you are interested in data representation, this series of blog posts by Edward Z. Yang could be interesting for you.\n- Related: How does a Haskell compiler works?\n- This is a question I always wanted to ask. Getting acquainted with the papers is a good start. They are readable. You may be interested on those about garbage collection, since they reveal most of the underlying structure (how closure blocks are implemented). For the different compiler passes, I have no idea.\n- Regarding the second item, wouldn't it be the case that if A is truly only used for some values of B, then A will either be evaluated after B or not evaluated at all due to never being forced? Unless something else is creating unnecessary strictness, in which case A is probably getting evaluated even when it's not needed anyway.\n- @Alexandre C: that's a good suggestion, as I am pretty familiar with GC algorithms. -- thanks\n- I honestly don't know how you find the time to give as many detailed, diagrammed, well-referenced answers as you do, but I'm glad you do :)\n- Most of it is cached in my head from years of grad school :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.754Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":55,"estimatedTokens":1016}}75{"id":"stack-21150454","source":"stackoverflow","questionId":21150454,"title":"Representing an Abstract Syntax Tree in C","tags":["c","compiler-construction","struct","tree","abstract-syntax-tree"],"text":"Title: Representing an Abstract Syntax Tree in C\nTags: c, compiler-construction, struct, tree, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI'm implementing a compiler for a simple toy language in C. I have a working scanner and parser, and a reasonable background on the conceptual function/construction of an AST. My question is related to the specific way to represent an AST in C. I've come across three styles pretty frequently in different texts/resources online:\n\n**One struct per type of node.**\n\nThis has a base node \"class\"(struct) that is the first field in all the child structs. The base node contains an enum that stores the type of node(constant, binary operator, assignment, etc). Members of the struct are accessed using a set of macros, with one set per struct. It looks something like this:\n\n```\nstruct ast_node_base {\n enum {CONSTANT, ADD, SUB, ASSIGNMENT} class;\n};\n\nstruct ast_node_constant {\n struct ast_node_base *base;\n int value;\n};\n\nstruct ast_node_add {\n struct ast_node_base *base;\n struct ast_node_base *left;\n struct ast_node_base *right;\n};\n\nstruct ast_node_assign {\n struct ast_node_base *base;\n struct ast_node_base *left;\n struct ast_node_base *right;\n};\n\n#define CLASS(node) ((ast_node_base*)node)->class;\n\n#define ADD_LEFT(node) ((ast_node_add*)node)->left;\n#define ADD_RIGHT(node) ((ast_node_add*)node)->right;\n\n#define ASSIGN_LEFT(node) ((ast_node_assign*)node)->left;\n#define ASSIGN_RIGHT(node) ((ast_node_assign*)node)->right;\n```\n\n**One struct per layout of node.**\n\nThis appears to be mostly the same as the above layout, except instead of having ast_node_add and ast_node_assign it would have an ast_node_binary to represent both, because the layout of the two structs is the same and they only differ by the contents of base->class. The advantage to this seems to be a more uniform set of macros(LEFT(node) for all nodes with a left and right instead of one pair of macros per), but the disadvantage seems that the C type checking won't be as useful(there would be no way to detect an ast_node_assign where there should only be an ast_node_add, for example).\n\n**One struct total, with a union to hold different types of node data.**\n\nA better explanation of this than I can give can be found here. Using the types from the previous example it would look like:\n\n```\nstruct ast_node {\n enum { CONSTANT, ADD, SUB, ASSIGNMENT } class;\n union {\n int value;\n struct {\n struct ast_node* left;\n struct ast_node* right;\n } op;\n };\n};\n```\n\nI'm inclined to like the third option the most because it makes recursive traversal much easier(in that lots of pointer casting is avoided in favor of the union), but it also doesn't take advantage of C type checking. The first option seems the most dangerous in that it relies on pointers to structs being cast to access the member of any node(even different members of the same node requiring different cases to access(base vs. left)), but these casts are type checked so that might be moot. The second option to me seems like the worst of both worlds, although maybe I'm missing something.\n\n**Which of these three schemes are the best, and why? Is there a better fourth option I haven't come across yet?** I'm assuming none of them are a \"one size fits all\" solution, so if it matters the language I'm implementing is a statically typed imperative language, almost a small subset of C.\n\nA specific question I have about the third(union) layout. **If I use only the value field, will there be empty space following the value to accommodate for the possibility of op being written to?**\n\n========================================\n\nCode:\n```text\nstruct ast_node_base {\n    enum {CONSTANT, ADD, SUB, ASSIGNMENT} class;\n};\n\nstruct ast_node_constant {\n    struct ast_node_base *base;\n    int value;\n};\n\nstruct ast_node_add {\n    struct ast_node_base *base;\n    struct ast_node_base *left;\n    struct ast_node_base *right;\n};\n\nstruct ast_node_assign {\n    struct ast_node_base *base;\n    struct ast_node_base *left;\n    struct ast_node_base *right;\n};\n\n#define CLASS(node) ((ast_node_base*)node)->class;\n\n#define ADD_LEFT(node) ((ast_node_add*)node)->left;\n#define ADD_RIGHT(node) ((ast_node_add*)node)->right;\n\n#define ASSIGN_LEFT(node) ((ast_node_assign*)node)->left;\n#define ASSIGN_RIGHT(node) ((ast_node_assign*)node)->right;\n```\n\n```text\nstruct ast_node {\n  enum { CONSTANT, ADD, SUB, ASSIGNMENT } class;\n  union {\n    int value;\n    struct {\n      struct ast_node* left;\n      struct ast_node* right;\n    } op;\n  };\n};\n```\n\n========================================\n\nComments:\n- I think you meant to link note 26 instead of note 25, in the sentence \"A better explanation of this than I can give can be found here.\"\n- Thanks! This is just what I wanted to hear, glad to know I'm not off course yet.\n- @user1547129: It will be annoying but possibly worth it to avoid parent pointers if you can. I don't think you will really need them at this stage, but they'll cause a headache when you want to move around trees and have to un-link and re-link them.\n- @Mehrdad: All data structures are annoying, because you have make sure they (and their invariants) stay up to date. You've pointed out a tradeoff, between maintaining parent pointers and coding parent list caches to allow you walk back up the tree when you don't have parent links. (I personally prefer the parent links; most code involving a tree is *inspecting* it and that ought to be the easiest to write. [Note: I build tools that do massive tree transformations].). YMMV.\n- is the union the second one you choose?\n- I don't understand your question. My answer seems to make clear I prefer the union as the first among alternatives. Having said that, my DMS system, which handles ASTs for over 50 different language in a uniform way, has several baseline node layouts based on frequency of utilization. But it isn't one-per-nonterminal. We use terminal nodes (only have parent link, type, and value), fixed-arity children (with 1 to 15 children), and list-nodes having one parent and a dynamic array of children to capture lists. When you make a long-term investment, its OK to have several node types.","metadata":{"transformedAt":"2026-08-18T18:32:17.754Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":125,"estimatedTokens":1536}}76{"id":"stack-14678060","source":"stackoverflow","questionId":14678060,"title":"Interpreters vs Compilers vs Virtual Machines","tags":["compiler-construction","vm-implementation"],"text":"Title: Interpreters vs Compilers vs Virtual Machines\nTags: compiler-construction, vm-implementation\nSource: Stack Overflow\n\nQuestion:\nI have a question about Interpreters, Compilers, and Virtual Machines (VMs).\n\nNow I know the differences between Interpreters and Compilers but what is different about the **VIRTUAL MACHINES** from the previous 2? What are the Pros and Cons of a VM over Interpreters and Compilers?\n\nThanks a lot.\n\n========================================\n\nTop Answer:\nPlain English\n\n**Compiler** transforms one language into another. For example, C# into IL, Java into byte code, C++ into binary machine code. No execution happens at the compilation stage.\n\n**Interpreter** interprets (executes) one line at a time from the source file. Such as PHP, Perl, other scripting languages\n\n**Virtual Machine** can refer to several things, the two I am aware of:\n\n- Hypervisor-related virtual machine, such as Hyper-V, Xen. These allow you to run several OS on a single piece of hardware\n\n- Software run time, like Java Virtual Machine, Common Language Runtime. This piece of software allow one to run platform independent intermediate language (IL code, byte code) and execute machine-specific instructions (just-in-time compilation). Usually, such VM is responsible for other satellite tasks: resource management, memory clean up, threading, security etc\n\nThere are no pros and cons. All three do different jobs that can hardly be compared.\n\nLanguages and platforms come in no particular order, there are literally hundreds of other samples\n\n========================================\n\nCode:\n```text\nnew\n```\n\n========================================\n\nComments:\n- Compiler & Interpreter & VM can be written for any languages and any language can be translated into a form that can be executed by a descent VM. Any language can be compiled and of course any language can be interpreted. That doesn't mean that it's interesting thing to do.\n- Top Notch Mate, just what I needed\n- \"doesn't actuallly exist\" isn't exactly the proper expression. What is true is that there might not be a manufactured machine processor that can consume the same bytecode language of a given VM. It doesn't mean that such a machine processor can not be manufactured.","metadata":{"transformedAt":"2026-08-18T18:32:17.755Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":43,"estimatedTokens":563}}77{"id":"stack-3857743","source":"stackoverflow","questionId":3857743,"title":"How to create a .Net programming language?","tags":[".net","visual-studio","parsing","compiler-construction"],"text":"Title: How to create a .Net programming language?\nTags: .net, visual-studio, parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have created a few different full programming languages using some of the various parsing tools available. However, how would someone create a programming language that runs the .Net framework? Would I have to output the .Net IL and compile that or is there a higher level of abstraction?\n\nAlso, is there an easy way to get the language working in Visual Studio?\n\n========================================\n\nTop Answer:\nThere is a LOLCode compiler built in C# that might be a good starting point.\n\nhttp://code.google.com/p/lolcode-dot-net/\n\nAs for implementing in Visual Studio, there is an open source view engine called Spark (http://www.sparkviewengine.com/) that did some work with getting intellisense to work. The context is different, but the lesson is the same, it's hard, and it's not a very well documented process.\n\n========================================\n\nCode:\n```text\n.dll\n```\n\n```text\n.exe\n```\n\n========================================\n\nComments:\n- You would output the .NET IL. The .NET runtime would handle compiling and running that code. Not sure about the other pieces.\n- Similar resources-to-learn-how-to-create-a-compiler-interpreter-for-&zwnj;&#8203;the-net\n- Check out this article from MSDN magazine on implementing a compiler in C# for a given language: msdn.microsoft.com/en-us/magazine/cc136756.aspx\n- The link is dead. Here is an archive.org snapshot: web.archive.org/web/20150111033016/http://msdn.microsoft.com&zwnj;&#8203;/&hellip;\n- How do you take IL code and compile/run it? Do you need to generate a project which .NET Core, Framework, and/or Mono could run?\n- A compiler is anything that converts from one form to another.\n- Has this been deprecated by Roslyn or is it still relevant?\n- CCI is helpful to create *any* language, Roslyn targets only C# and VB.NET\n- This has been discontinued: \"Moving forward, there will only be servicing investments in CCI to support existing uses. \"\n- Hmm, no, there is a distinction between \"discontinued\" and \"done\". It is discontinued when you can't download it anymore. Lots of Microsoft projects are done, they still service it to provide critical bug fixes but won't add to it anymore. Winforms for example has been done for 14 years now, lots of people still use it. It actually gets more servicing updates than WPF, also done.","metadata":{"transformedAt":"2026-08-18T18:32:17.755Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":42,"estimatedTokens":613}}78{"id":"stack-586362","source":"stackoverflow","questionId":586362,"title":"How does the Erlang compiler implement pattern matching?","tags":["compiler-construction","erlang","pattern-matching"],"text":"Title: How does the Erlang compiler implement pattern matching?\nTags: compiler-construction, erlang, pattern-matching\nSource: Stack Overflow\n\nQuestion:\nI am wondering how pattern matching is usually implemented. for example in Erlang do you think its implemented at the byte-code level( there's a byte-code for it so that its done efficiently) or is it generated as a series of instructions (series of byte-codes) by the compiler?\n\nIt is such a useful thing that I just have to put it into a toy language I am building.\n\n========================================\n\nTop Answer:\nA very good description of compiling pattern matching is given in \"The implementation of functional programming languages\" by Simon Peyton Jones. It is a bit old but a very good book. It also contains, amongst other things, a description of compiling list comprehensions.\n\nThe Erlang compiler uses both of these algorithms from the book.\n\n========================================\n\nCode:\n```text\n-module(match).\n-export([match/1]).\nmatch(X) -> {a,Y} = X.\n```\n\n```text\n> c(match, to_core).\n```\n\n```text\n$ erlc +to_core match.erl\n```\n\n```text\nmodule 'match' ['match'/1,\n                'module_info'/0,\n                'module_info'/1]\n    attributes []\n'match'/1 =\n    %% Line 3\n    fun (_cor0) ->\n        case _cor0 of\n          <{'a',Y}> when 'true' ->\n              _cor0\n          ( <_cor1> when 'true' ->\n                primop 'match_fail'\n                    ({'badmatch',_cor1})\n            -| ['compiler_generated'] )\n        end\n'module_info'/0 =\n    fun () ->\n        call 'erlang':'get_module_info'\n            ('match')\n'module_info'/1 =\n    fun (_cor0) ->\n        call 'erlang':'get_module_info'\n            ('match', _cor0)\n```\n\n```text\n> c(match, 'S').\n```\n\n```text\n$ erlc -S match.erl\n```\n\n```text\n{module, match}.  %% version = 0\n\n{exports, [{match,1},{module_info,0},{module_info,1}]}.\n\n{attributes, []}.\n\n{labels, 8}.\n\n\n{function, match, 1, 2}.\n  {label,1}.\n    {func_info,{atom,match},{atom,match},1}.\n  {label,2}.\n    {test,is_tuple,{f,3},[{x,0}]}.\n    {test,test_arity,{f,3},[{x,0},2]}.\n    {get_tuple_element,{x,0},0,{x,1}}.\n    {test,is_eq_exact,{f,3},[{x,1},{atom,a}]}.\n    return.\n  {label,3}.\n    {badmatch,{x,0}}.\n\n\n{function, module_info, 0, 5}.\n  {label,4}.\n    {func_info,{atom,match},{atom,module_info},0}.\n  {label,5}.\n    {move,{atom,match},{x,0}}.\n    {call_ext_only,1,{extfunc,erlang,get_module_info,1}}.\n\n\n{function, module_info, 1, 7}.\n  {label,6}.\n    {func_info,{atom,match},{atom,module_info},1}.\n  {label,7}.\n    {move,{x,0},{x,1}}.\n    {move,{atom,match},{x,0}}.\n    {call_ext_only,2,{extfunc,erlang,get_module_info,2}}.\n```\n\n```text\n{test,is_tuple,...\n```\n\n```text\n{test,test_arity,...\n```\n\n```text\n{get_tuple_element,...\n```\n\n```text\n{test,is_eq_exact,...\n```\n\n```text\nerlc -S test.erl\n```\n\n```text\n{test,is_tuple,{f,1},[{x,0}]}.\n{test,test_arity,{f,1},[{x,0},2]}.\n{get_tuple_element,{x,0},0,{x,1}}.\n{get_tuple_element,{x,0},1,{x,2}}.\n{test,is_nonempty_list,{f,4},[{x,2}]}.\n```\n\n========================================\n\nComments:\n- wonderful answer, lots of great info here (especially the compilation directives). thank you\n- great. appreciate it, looks like lots of useful stuff\n- thanks. I've had this book downloaded for a while now but never had the time to read it. how do you know that Erlang uses algorithms from it?\n- Sorry for not replying earlier, much earlier. The reason I know is that I implementented compiling pattern matching for the current compiler and this is where I took the algorithm from.\n- that works ;). thanks for working on erlang, its a bit odd but definitelly a breath of fresh air. made my life a better place for sure\n- @rvirding Does that include the pattern matcher for binary data?\n- @MartinBerger The pattern matcher for binary data was added later but is part of compiling patterns. It is a little more complex than matching other data types as the patterns can overlap much more in many different ways, for example you can match against different size integers at the start of the binary.\n- @rvirding Thank you. There is an old paper Efficient manipulation of binary data using pattern matching. that describes an Erlang pattern matcher for binary data. Is this still what's in the current compiler?","metadata":{"transformedAt":"2026-08-18T18:32:17.755Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":146,"estimatedTokens":1065}}79{"id":"stack-3662410","source":"stackoverflow","questionId":3662410,"title":"create my own programming language","tags":["c++","compiler-construction","programming-languages","interpreter"],"text":"Title: create my own programming language\nTags: c++, compiler-construction, programming-languages, interpreter\nSource: Stack Overflow\n\nQuestion:\n**Possible Duplicates:**\n\n References Needed for Implementing an Interpreter in C/C++\n\n How to create a language these days?\n\n Learning to write a compiler \n\nI know some c++, VERY good at php, pro at css html, okay at javascript. So I was thinking of how was c++ created I mean how can computer understand what codes mean? How can it read... so is it possible I can create my own language and how?\n\n========================================\n\nTop Answer:\n\"Every now and then I feel a temptation to design a programming\nlanguage but then I just lie down until it goes away.\" — L. Peter\nDeutsch\n\nEDIT (for those who crave context):\n\n\"[L. Peter Deutsch] also wrote the PDP-1 Lisp 1.5 implementation, Basic PDP-1 LISP, 'while still in short pants' between the age of 12-15 years old.\"\n\n========================================\n\nComments:\n- Already a large selection of resources here: stackoverflow.com/questions/1669/learning-to-write-a-compile&zwnj;&#8203;r\n- Forget text based 'language' and do something more domain specific, why not tell the computer what you want it to do graphically, through hand waves or something more natural, like facial recognition. ted.com/talks/peter_molyneux_demos_milo_the_virtual_boy.html\n- I wanted to put this as an answer, but since it got closed, I'll put it here: This is a book which has been used in many colleges as a comp sci 101 book. It takes you through the rudiments of designing computer hardware, assembly language, compilers, and high level languages. It won't make you an over-night expert at any of them but it makes them all very approachable. You'll amaze yourself with what you'll learn how to do. \"The Elements of Computing Systems\": www1.idc.ac.il/tecs\n- @Yi Jiang: If you're not happy with that link, here are some more duplicates: stackoverflow.com/questions/294852/&hellip;, stackoverflow.com/questions/1550025/&hellip;, stackoverflow.com/questions/2413565/&hellip;, stackoverflow.com/questions/2853412/&hellip;, stackoverflow.com/questions/2923287/&hellip;, etc.\n- Don't rush into this. If you're \"ok\" with C++, learn the language. Sit down and do A LOT (I mean, a lot) of throwaway projects so you can understand how programming languages work. PHP is a nice precursor, but it ruins your sense of how programming works. PHP is nice; they do all the work for you and you never have to include things and use libraries, etc. C++, on the other hand, is not so nice, but is has much more capabilities (for whatever basis of comparison you have seeing as PHP is a web *interpreted* [that word again] language and C++ is platform first gen.). Good luck anyway!\n- I don't think I would recommend the dragon book to a 13-year-old.\n- @mmyers, if he already knows some C++ and *very* good at php...he may have the skills and knowledge necessary to understand Dragon book. If nothing else it'll help to teach him what else he needs to know/learn *first*.\n- @mmyers Just because he's 13 doesn't make it easier to write a compiler.\n- Most compilers are written in their own languages [ en.wikipedia.org/wiki/Bootstrapping_(compilers) ]. Similarly, the PHP interpreter is primarly written in C -- there's little, if any assembly involved.\n- Err, you can write compilers for any language in any other language. I mean for practice in college we created lisp using java, then java using lips. Of course it was very exact syntax and not extremely smart.\n- @Frank: @Parris: I don't think Marc's point was that the compiler should be written in assembly, but that it should compile to assembly. Or maybe he was just answering the part of the question that said \"how can computer understand what codes mean?\".\n- @sepp2k There are many compilers that don't \"compile to assembly\". javac, any interpreted language...\n- Assembler doesn't have to be the implementation language *nor* the output format. However, knowing assembler means you have a base model for the functionality a byte-code or interpreter will have to provide under the hood, particularly for implementing recursive function calls. That said, feel free to just muddle your way through. Creating a language of your own that uses a std::map to hold variables, then finding recursion doesn't work is a good learning experience.\n- Neither an understanding of assembly nor electronics is necessary for writing a high-level language (although they can certainly help). For example, a lot of recently-created languages run in the JVM: en.wikipedia.org/wiki/List_of_JVM_languages\n- Although these are fine things to learn, except for the first none of these things seem necessary for writing a compiler. Even the first isn't needed if one uses LLVM or similar technology.\n- 'high-level' wasn't specified in the question\n- Plus he also said “ you might want to “. It’s not that he has.\n- \"Compiler\" wasn't specified in the question. He said he wants to create a new language. When designing a new language, typically a machine-code compiler is not the first implementation.\n- I second the assembler part of this: That is as low as you need to go to have a good idea. It doesn't matter which assembler, though. You just need to know about the stack/heap, registers like the program counter, and typical instructions most assemblers have (conditionals, flow control, stuff involved in calling functions). Ideally, start with a teaching assembler, or a clean and simple one like Motorola 68000 assembler, if you can create a simple assembler program that recursively calls a function, uses conditionals and loops, and maybe makes one system call like printf(), you know enough.\n- Ugh, I have professionally written several compilers for small domain-specific languages, and I recently did my first LLVM project and found the learning curve very very steep. Surely for a student it would be better to make a very tiny toy language and write their own simple recursive descent compiler and bytecode interpreter, rather than try to understand all the industrial-strength compiler concepts you need to make use of LLVM.\n- @Larry Gritz: depend if you are interested in writing the compiler or designing the language. If you contend yourself with generating llvm assembly I think it could be quite simple (once you have generated your AST), it doesn't mean you shouldn't read a good book though.\n- +1 for starting with writing a calculator. I would however try to read something like the Dragon Book and use a compiler compiler such as Bison or ANTLR.\n- Agreed, writing a simple expression parser and interpreter is a good first step, but will probably not answer the OP's questions about how C++ works, how the computer runs code. Not sure I agree about the compiler compilers/parser generators though (in this particular case of learning how stuff works). It might be worth writing your own tokenizer and parser from hand first, just to understnd what those tools do for you.\n- +1 for advocating common sense, and the avoidance of masochism... =) (**edited** to add link to a source for the quote: books.google.com/&hellip;)\n- At age 13, I was debugging an assembler in Signetics 2650 assembly language. The project became easier after I learned Algebra. After the university Compiler Theory & Design course, I now fully understand it.\n- Not a good answer, given the context of the OP. It's great to have created a simple version of most parts of a system yourself once. It's the best way to get a better idea of what is involved, and gives you better judgement of how your system might work internally, and thus when you hit weird bugs, there is a better likelihood of you knowing what could have caused it. Reinventing parts of your computer (as a learning exercise) makes you a better programmer.","metadata":{"transformedAt":"2026-08-18T18:32:17.755Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":55,"estimatedTokens":1957}}80{"id":"stack-1104922","source":"stackoverflow","questionId":1104922,"title":"Compiler test cases or how to test a compiler","tags":["compiler-construction"],"text":"Title: Compiler test cases or how to test a compiler\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nCompilers like all software, would also be prone to bugs, logical errors.\n\nHow does one validate the output generated by the compiler. Typically, my question is(are)\n\nHow to validate that the machine code generated is correct?\n\nHow to ensure that the machine code generated is according to the language specification.\n\nDoes it make sense to just pick an open source project (in C if one is also writing a compiler in C) to just compile it through the \"compiler\". In that case also, how do judge that the compiler is behaving as expected. \n\nAre there any formal test cases (literature) provided by the language standards committee that a \"language complying\" compiler has to satisfy? \n\nWhat are the sure \"give aways\" that the problem in a program compiled by a *compiler* is a compiler bug and not a program bug.\n\n**- Any examples where mainstream compilers get confused and compile the code wrong?**\n\nLinks to any literature would be appreciated.\n\n========================================\n\nTop Answer:\nGood test suites for real languages are expensive to create and maintain. There's a reason that the Plum Hall test suite, which is industry standard for ANSI C, is so bloody expensive.\n\nGeorge Necula's translation validation is a brilliant idea but also quite expensive to implement.\n\nThe one thing that's cheap and easy is this: maintain a suite of regression tests, and **every time you fix a bug in your compiler, put a suitable test into your regression suites**. With compilers, it's unbelievable how easy it is to keep reintroducing the same bug over and over. Disciplined additions to your regression suite will prevent that, and they don't cost much.\n\n========================================\n\nComments:\n- A large proprietary testsuite: solidsands.nl/supertest-general\n- Does not answer everything but the link is excellent. Thanks.\n- Any open-source alternative to Plum Hall?\n- I submitted the request product information to Plum Hall but the server returns 500 error code. And I cannot find any email of Plum Hall...It seems that Plum Hall has been not maintained for a while, or I'm not lucky that I just met a server crash?\n- but if I make small source files and test them individually, how does it ensures that they will all work when part of the same program. Like for example, I pick up an open source project and let *my* compiler loose on it.\n- It doesn't. But it is far more likely your compiler will fail one of the small programs. There is no possible way of proving that a compiler can correctly compile any given source - this would be equivalent to solving the halting Problem.\n- Generally you get a mixing of test cases that are generated from the language spec (mostly really small) and cases from bugs (as small as the reproduction case can be cut down to). As for knowing things will work in all cases, I'm not sure you can (for most languages) even prove that the spec is consistent let alone that it is implemented correctly.\n- isnt it odd that something like this is not standardized or atleast an effort is made to standardize it.\n- Perhaps this is a working D-language test suite link.","metadata":{"transformedAt":"2026-08-18T18:32:17.755Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":44,"estimatedTokens":809}}81{"id":"stack-5920200","source":"stackoverflow","questionId":5920200,"title":"How to prevent common sub-expression elimination (CSE) with GHC","tags":["optimization","compiler-construction","haskell","ghc"],"text":"Title: How to prevent common sub-expression elimination (CSE) with GHC\nTags: optimization, compiler-construction, haskell, ghc\nSource: Stack Overflow\n\nQuestion:\nGiven the program:\n\n```\nimport Debug.Trace\nmain = print $ trace \"hit\" 1 + trace \"hit\" 1\n```\n\nIf I compile with `ghc -O` (7.0.1 or higher) I get the output:\n\n```\nhit\n2\n```\n\ni.e. GHC has used common sub-expression elimination (CSE) to rewrite my program as:\n\n```\nmain = print $ let x = trace \"hit\" 1 in x + x\n```\n\nIf I compile with `-fno-cse` then I see `hit` appearing twice.\n\nIs it possible to avoid CSE by modifying the program? Is there any sub-expression `e` for which I can guarantee `e + e` will not be CSE'd? I know about `lazy`, but can't find anything designed to inhibit CSE.\n\nThe background of this question is the cmdargs library, where CSE breaks the library (due to impurity in the library). One solution is to ask users of the library to specify `-fno-cse`, but I'd prefer to modify the library.\n\n========================================\n\nTop Answer:\nHow about removing the source of the trouble -- the implicit effect -- by using a sequencing monad that introduces that effect? E.g. the strict identity monad with tracing:\n\n```\ndata Eval a = Done a\n | Trace String a\n\ninstance Monad Eval where\n return x = Done x\n\n Done x >>= k = k x\n Trace s a >>= k = trace s (k a)\n\nrunEval :: Eval a -> a\nrunEval (Done x) = x\n\ntrack = Trace\n```\n\nnow we can write stuff with a guaranteed ordering of the `trace` calls:\n\n```\nmain = print $ runEval $ do\n t1 while still being pure code, and GHC won't try to get to clever, even with `-O2`:\n\n```\n$ ./A\n hit\n hit\n 2\n```\n\nSo we introduce just the computation effect (tracing) sufficient to teach GHC the semantics we want.\n\nThis is *extremely* robust to compile optimizations. So much so that GHC optimizes the math to `2` at compile time, yet still retains the ordering of the `trace` statements.\n\nAs evidence of how robust this approach is, here's the core with `-O2` and aggressive inlining:\n\n```\nmain2 =\n case Debug.Trace.trace string trace2 of\n Done x -> case x of \n I# i# -> $wshowSignedInt 0 i# []\n Trace _ _ -> err\n\ntrace2 = Debug.Trace.trace string d\n\nd :: Eval Int\nd = Done n\n\nn :: Int\nn = I# 2\n\nstring :: [Char]\nstring = unpackCString# \"hit\"\n```\n\nSo GHC has done everything it could to optimize the code -- including computing the math statically -- while still retaining the correct tracing.\n\n*References*: the useful `Eval` monad for sequencing was introduced by Simon Marlow.\n\n========================================\n\nCode:\n```text\nimport Debug.Trace\nmain = print $ trace \"hit\" 1 + trace \"hit\" 1\n```\n\n```text\nhit\n2\n```\n\n```text\nmain = print $ let x = trace \"hit\" 1 in x + x\n```\n\n```text\nghc -O\n```\n\n```text\n-fno-cse\n```\n\n```text\nhit\n```\n\n```text\ne\n```\n\n```text\ne + e\n```\n\n```text\nlazy\n```\n\n```text\n-fno-cse\n```\n\n```text\nunit = reverse \"\" `seq` ()\n\nmain = print $ trace \"hit\" (case unit of () -> 1) +\n               trace \"hit\" (case unit of () -> 1)\n```\n\n```text\nexprIsBig\n```\n\n```text\nExpr\n```\n\n```text\nNote\n```\n\n```text\nLet\n```\n\n```text\nCase\n```\n\n```text\nunit\n```\n\n```text\n()\n```\n\n```text\nreverse\n```\n\n```text\ntrace\n```\n\n```text\nhit\n```\n\n```text\n-O2\n```\n\n```text\n{-# OPTIONS_GHC -fno-cse #-}\n```\n\n```text\nlet x () = trace \"hi\" 1 in x () + x ()\n```\n\n```text\nlet\n    x dummy = trace \"hi\" $ dummy `seq` 1\n    x1      = x ()\n    x2      = x x1 \nin x1 + x2\n```\n\n```text\n-fno-cse\n```\n\n```text\nx\n```\n\n```text\ndummy\n```\n\n```text\ndata Eval a = Done a\n            | Trace String a\n\ninstance Monad Eval where\n  return x = Done x\n\n  Done x    >>= k = k x\n  Trace s a >>= k = trace s (k a)\n\nrunEval :: Eval a -> a\nrunEval (Done x) = x\n\ntrack = Trace\n```\n\n```text\nmain = print $ runEval $ do\n            t1 <- track \"hit\" 1\n            t2 <- track \"hit\" 1\n            return (t1 + t2)\n```\n\n```text\n$ ./A\n    hit\n    hit\n    2\n```\n\n```text\nmain2 =\n  case Debug.Trace.trace string trace2 of\n    Done x -> case x of \n        I# i# -> $wshowSignedInt 0 i# []\n    Trace _ _ -> err\n\ntrace2 = Debug.Trace.trace string d\n\nd :: Eval Int\nd = Done n\n\nn :: Int\nn = I# 2\n\nstring :: [Char]\nstring = unpackCString# \"hit\"\n```\n\n```text\ntrace\n```\n\n```text\n-O2\n```\n\n```text\n2\n```\n\n```text\ntrace\n```\n\n```text\n-O2\n```\n\n```text\nEval\n```\n\n```text\nmain :: IO ()\nmain = print $ runEval $ do\n            t1 <- track \"hit 1\" (trace \"really hit 1\" 1)\n            t2 <- track \"hit 2\" 2\n            return (t1 + t2)\n```\n\n```text\nhit 1\nhit 2\nreally hit 1\n```\n\n```text\nDone x    >>= k = k x\nTrace s a >>= k = k (trace s a)\n```\n\n```text\nhit 1\nreally hit 1\nhit 2\n```\n\n```text\nhit 2\nreally hit 2\nhit 1\n```\n\n```text\nhit 1\nhit 2\nreally hit 2\n```\n\n```text\nt1 <- ...\n```\n\n```text\nt1\n```\n\n```text\nreturn (t1 + t2)\n```\n\n```text\n(t1 + t2)\n```\n\n```text\n(t1 + t2)\n```\n\n```text\n(t2 + t1)\n```\n\n```text\n-O3\n```\n\n========================================\n\nComments:\n- Hm, *long shot*, but this reminds me that I wanted to try to make the assert magic user-visible somehow. In case that turned out to be possible, it could also act as a tag preventing GHC from CSE-ing specific function invocations.\n- @Peter: I did try the assert stuff, but that then required `-fno-ignore-asserts` to work.\n- `(due to impurity in the library)`. Obviously, since doing that breaks Haskell's semantics, the compilers will get confused. Is there no way to refactor your code to be referentially transparent in a way the compiler can understand? E.g. ST monad or make it pure?\n- You used unsafePerformIO, you got what you deserved.\n- @Don Stewart:This is used to extract annotations like `opt \"example\"` from user code (see here). As user code can be just about anything, this has to break referential transparency by design. A pretty cool hack, but very fragile.\n- -fno-cse in my source file won't work - it has to be in the source file of the person using my library, which makes it less desirable. There is no actual data dependency, so I'd have force the end user to add fake data dependencies, making the library API really ugly. These are both useful answers to anyone who isn't writing a library and comes across this problem.\n- Answering your own question? Cool!\n- @augustss Indeed, I suspect the `reverse \"\"` will fall to constructor specialisation before the CSE changes - I am sure eventually I'll be outwitted by the compiler.\n- @Tener I didn't have an answer when I posted it - and I hold out the hope that someone may still have a better answer than mine, which is most robust to future GHC improvements.\n- The answer is not to use unsafePerformIO. The way you are using it is wrong, because what you are doing is not implementing an pure function in an impure way. You are actually using the side effects. That's not what Haskell is for.\n- Unsuitable for my particular application, but of course, this is exactly how it should be done in almost all cases. For real code you should probably use `putStrLn` instead of `trace`, and have `runEval` return in the `IO` monad.\n- I'm not so sure picking up the `IO` sledgehammer is so obviously the solution in production. We get nice optimization benefits from the not-overly-sequenced tracing monad, and it is pure, so it composes more easily. I suspect it has some uses.\n- Neil's use of unsafePerformIO is actually rather clever and not at all performance critical (command line argument decoding). I don't know of any way to do it quite as conveniently as it is with unsafePerformIO. But I'm willing to pay a price in inconvenience for purity.","metadata":{"transformedAt":"2026-08-18T18:32:17.755Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":51,"totalLines":372,"estimatedTokens":1861}}82{"id":"stack-4934707","source":"stackoverflow","questionId":4934707,"title":"Is it possible to transform LLVM bytecode into Java bytecode?","tags":["java","compiler-construction","llvm"],"text":"Title: Is it possible to transform LLVM bytecode into Java bytecode?\nTags: java, compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nI have heard that google app engine can run any programming language that can be transformed to Java bytecode via it's `JVM`. I wondered if it would be possible to convert LLVM bytecode to Java bytecode as it would be interesting to run languages that `LLVM` supports in the Google App Engine `JVM`.\n\n========================================\n\nTop Answer:\nI doubt you can, at least not without significant effort and run-time abstractions (e.g. building half a Von Neumann machine to execute certain opcodes). LLVM bitcode allows the full range of low-level unsafe \"do what you want but we won't clean up the mess\" features, from direct, raw, constructor-free memory allocation up to completely unchecked casts - real casts, not conversions -you can take `i32` and `bitcast` it to to a `%stuff *` if you wish. Also, JVMs are heavily geared towards objects and methods, while the LLVM guys are lucky they have function pointers and structs.\n\nOn the other hand, it seems that C can be compiled to Java bytecode and LLVM bitcode can be compiled to Javascript (although many features, e.g. dynamic loading and stdlib functions, are lacking), so it should be possible, given enough effort.\n\n========================================\n\nCode:\n```text\nJVM\n```\n\n```text\nLLVM\n```\n\n```text\nJVM\n```\n\n```text\ni32\n```\n\n```text\nbitcast\n```\n\n```text\n%stuff *\n```\n\n========================================\n\nComments:\n- AFAIK LLVM is a hardware/OS abstraction layered library rather than a byte code virtual machine. It provides some of the same advantages but need to be compiled from source for each target platform.\n- @Peter: No, you can interpret it and JIT-compile it (`lli`). But yes, the instructions are way more low-level and it's not really similar to other virtual machines.\n- @Ben, please reconsider the accepted answer in light of what I mention in stackoverflow.com/a/13540256/304330, thanks.\n- So basically LLVM bitcode is far closer to assembly than Java Bytecode so I would have to somehow 'reclaim' all the information 'lost' when a program is converted to the lower-level representation if I wanted to run it in a JVM. Which I guess is pretty impossible.\n- @Ben: Yes, it's pretty much portable (well, kind of) assembly... in an even more low-level fashion than C. Not only you'd have to do quite a lot of work when reverse-engineering e.g. Ada code compiled with `llvm-gcc`, at least C and C++ can do many things Java bytecode simply doesn't permit (for better or worse). Likewise, LLVM permits these things but the JVM doesn't.\n- The classic example I go to: `char *vga = (char *) 0xB8000`. LLVM can handle that just fine. Pretty sure JVM bytecode cannot.\n- You can actually do any raw memory operations (and raw casts) you like in Java via sun.misc.Unsafe. Any byte code calling Unsafe methods is, of course, accessing the raw menory via native code (JNI), so any LLVM constructs that were translated to byte code which calls Unsafe methods are essentially doing the memory operations via C. It would extremely clunky doing some things via Unsafe. But one can imagine a more extensive library of functions than sun.misc.Unsafe specifically designed for supporting LLVM (or other C-like) memory ops, also implemented by cross-platform native calls.\n- It's the reverse (allows building LLVM-based VMs that run e.g.Java/JVM languages on LLVM; OP wants to run LLVM languages on the JVM).\n- Fwiw, following that link: \"The VMKit project is retired.\"","metadata":{"transformedAt":"2026-08-18T18:32:17.755Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":53,"estimatedTokens":897}}83{"id":"stack-43322","source":"stackoverflow","questionId":43322,"title":"What's safe for a C++ plug-in system?","tags":["c++","plugins","compiler-construction"],"text":"Title: What's safe for a C++ plug-in system?\nTags: c++, plugins, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nPlug-in systems in C++ are hard because the ABI is not properly defined, and each compiler (or version thereof) follows its own rules. However, COM on Windows shows that it's possible to create a minimal plug-in system that allows programmers with different compilers to create plug-ins for a host application using a simple interface.\n\nLet's be practical, and leave the C++ standard, which is not very helpful in this respect, aside for a minute. If I want to write an app for Windows and Mac (and optionally Linux) that supports C++ plug-ins, and if I want to give plug-in authors a reasonably large choice of compilers (say less than 2 year old versions of Visual C++, GCC or Intel's C++ compiler), what features of C++ could I count on?\n\nOf course, I assume that plug-ins would be written for a specific platform.\n\nOff the top of my head, here are some C++ features I can think of, with what I think is the answer:\n\n- vtable layout, to use objects through abstract classes? (yes)\n\n- built-in types, pointers? (yes)\n\n- structs, unions? (yes)\n\n- exceptions? (no)\n\n- extern \"C\" functions? (yes)\n\n- stdcall non-extern \"C\" functions with built-in parameter types? (yes)\n\n- non-stdcall non-extern \"C\" functions with user-defined parameter types? (no)\n\nI would appreciate any experience you have in that area that you could . If you know of any moderately successful app that has a C++ plug-in system, that's cool too.\n\nCarl\n\n========================================\n\nTop Answer:\nQt has a very nice system for plugins that I've used in the past. It uses Qt's meta-object system to overcome many of the problems typically found when trying to develop C++ plugins.\n\nOne example is how `Q_DECLARE_INTERFACE` works, to prevent you from using an incompatible plugin. Another is the build key, to make sure you load the correct plugin for your architecture, OS, compiler. If you don't use Qt's plugin system, these are things you will have to worry about and invent solutions for on your own. It's not necessarily rocket science, and I'm not saying you'd fail at it, but the guys at Trolltech are pretty smart and have spent a while thinking about it, and I'd rather use what they created than reinvent the wheel myself.\n\nAnother example is that RTTI typically doesn't work across DLL boundaries, but when using Qt, things like qobject_cast which rely on the meta-object system do work across DLL boundaries.\n\n========================================\n\nCode:\n```text\n#if defined(IN_THE_PLUGIN)\nvoid MyObject::somefunction() { MyObject_somefunction(this); }\n#endif\n```\n\n```text\nQ_DECLARE_INTERFACE\n```\n\n========================================\n\nComments:\n- This answer would be better if you made the \"Here\" link a bit more descriptive. Right now, you can't tell to what you're linking.\n- If you're going for that, I'd say Lua is a much better fit, since it's much easier to integrate with C++ and it's much more lightweight.\n- That said, there are some times where scripting simply isn't enough -- especially if you want to allow plugins that have access to the operating system.\n- Thanks for the advice - I've come across that title so many times that I went ahead and bought it this time.\n- This link is broken","metadata":{"transformedAt":"2026-08-18T18:32:17.755Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":61,"estimatedTokens":831}}84{"id":"stack-552134","source":"stackoverflow","questionId":552134,"title":"Why isn't pass struct by reference a common optimization?","tags":["performance","optimization","compiler-construction","assembly","struct"],"text":"Title: Why isn't pass struct by reference a common optimization?\nTags: performance, optimization, compiler-construction, assembly, struct\nSource: Stack Overflow\n\nQuestion:\nUp until today, I had always thought that decent compilers automatically convert struct pass-by-value to pass-by-reference if the struct is large enough that the latter would be faster. To the best of my knowledge, this seems like a no-brainer optimization. However, to satisfy my curiosity as to whether this actually happens, I created a simple test case in both C++ and D and looked at the output of both GCC and Digital Mars D. Both insisted on passing 32-byte structs by value when all the function in question did was add up the members and return the values, with no modification of the struct passed in. The C++ version is below.\n\n```\n#include \"iostream.h\"\n\nstruct S {\n int i, j, k, l, m, n, o, p;\n};\n\nint foo(S s) {\n return s.i + s.j + s.k + s.l + s.m + s.n + s.o + s.p;\n}\n\nint main() {\n S s;\n int bar = foo(s);\n cout My question is, why the heck wouldn't something like this be optimized by the compiler to pass-by-reference instead of actually pushing all those `int`s onto the stack?\n\nNote: Compiler switches used: GCC -O2 (-O3 inlined foo().), DMD -O -inline -release.\n\nEdit: Obviously, in the general case the semantics of pass-by-value vs. pass-by-reference won't be the same, such as if copy constructors are involved or the original struct is modified in the callee. However, in a lot of real-world scenarios, the semantics will be identical in terms of observable behavior. These are the cases I'm asking about.\n\n========================================\n\nTop Answer:\nThe problem is you're asking the compiler to make a decision about the intention of user code. Maybe I want my super large struct to be passed by value so that I can do something in the copy constructor. Believe me, someone out there has something they validly need to be called in a copy constructor for just such a scenario. Switching to a by ref will bypass the copy constructor. \n\nHaving this be a compiler generated decision would be a bad idea. The reason being is that it makes it impossible to reason about the flow of your code. You can't look at a call and know what exactly it will do. You have to a) know the code and b) guess the compiler optimization.\n\n========================================\n\nCode:\n```text\n#include \"iostream.h\"\n\nstruct S {\n    int i, j, k, l, m, n, o, p;\n};\n\nint foo(S s) {\n    return s.i + s.j + s.k + s.l + s.m + s.n + s.o + s.p;\n}\n\nint main() {\n    S s;\n    int bar = foo(s);\n    cout << bar;\n}\n```\n\n```text\nint\n```\n\n```text\nconst\n```\n\n```text\nconst&\n```\n\n```text\nstatic\n```\n\n```text\nconst\n```\n\n```text\ndouble x; // using non structs, oh-well\n\nvoid Foo(double d)\n{\n      x += d; // ok\n      x += d; // Oops\n}\n\nvoid main()\n{\n     x = 1;\n     Foo(x);\n}\n```\n\n```text\nstruct FOO { ... };\n\nvoid func1(struct FOO *foo1);\nvoid func2(struct FOO foo2);\n\nvoid test(void)\n{\n  struct FOO foo;\n  func1(&foo);\n  func2(foo);\n}\n```\n\n```text\nfoo\n```\n\n```text\nfunc2\n```\n\n```text\nfunc1\n```\n\n```text\nfoo1\n```\n\n```text\nfunc2\n```\n\n```text\nfoo\n```\n\n```text\nfoo2\n```\n\n```text\nfunc2\n```\n\n```text\nfoo\n```\n\n```text\nfunc2\n```\n\n```text\nfoo\n```\n\n```text\nfoo2\n```\n\n```text\nvoid func3(const struct FOO);\n```\n\n```text\nconst\n```\n\n```text\n__attribute__((noinline))\nint foo(S s) {\n    return s.i + s.j + s.k + s.l + s.m + s.n + s.o + s.p;\n}\n\nint bar(S s) {\n    return foo(s);\n}\n```\n\n```text\nfoo(S):\n        mov     eax, DWORD PTR [rsp+12]\n        add     eax, DWORD PTR [rsp+8]\n        add     eax, DWORD PTR [rsp+16]\n        add     eax, DWORD PTR [rsp+20]\n        add     eax, DWORD PTR [rsp+24]\n        add     eax, DWORD PTR [rsp+28]\n        add     eax, DWORD PTR [rsp+32]\n        add     eax, DWORD PTR [rsp+36]\n        ret\nbar(S):\n        jmp     foo(S)\n```\n\n```text\nint bar(S s) {\n    return foo(s) + 1;\n}\n```\n\n```text\nbar(S):\n        push    QWORD PTR [rsp+32]\n        push    QWORD PTR [rsp+32]\n        push    QWORD PTR [rsp+32]\n        push    QWORD PTR [rsp+32]\n        call    foo(S)\n        add     rsp, 32\n        add     eax, 1\n        ret\n```\n\n```text\nint bar(S s) {\n    s.i += 1;\n    return foo(s);\n}\n```\n\n```text\nbar(S):\n        add     DWORD PTR [rsp+8], 1\n        jmp     foo(S)\n```\n\n```text\nstruct\n```\n\n```text\nbar\n```\n\n```text\nfoo\n```\n\n```text\nbar\n```\n\n```text\nfoo\n```\n\n```text\nbar\n```\n\n```text\ns\n```\n\n```text\nbar\n```\n\n```text\nfoo()\n```\n\n```text\nfoo/bar\n```\n\n```text\nS\n```\n\n```text\nfoo\n```\n\n```text\nbar\n```\n\n```text\nfoo\n```\n\n```text\n+ 1\n```\n\n```text\nfoo\n```\n\n```text\nbar::s\n```\n\n```text\nfoo\n```\n\n```text\ns\n```\n\n```text\nbar()\n```\n\n```text\n-O2\n```\n\n```text\nS\n```\n\n```text\nfoo\n```\n\n```text\nmain\n```\n\n```text\nfoo()\n```\n\n```text\nfoo\n```\n\n```text\nfoo\n```\n\n```text\nfoo\n```\n\n```text\nmain\n```\n\n```text\nfoo\n```\n\n```text\ns\n```\n\n```text\nargc\n```\n\n========================================\n\nComments:\n- A better test-case would be if `s` has static storage class, instead of being an (uninitialized!) automatic local. Then a calling-convention that passed by const-reference for objects that don't fit in a couple registers would be more of a win. (It has to be baked into the calling convention based only on struct size, see Michael's answer.)\n- Yes, I suppose you could have the ABI for your language declare that structs are passed by reference, but turned into copies if ever the callee tries to modify them. Sounds a bit messy though -- you need to employ all the tricky worst-case aliasing analysis to determine that they are guaranteed to be untouched. Easier just to have explicit rules in the language -- if you don't want passing a struct to slow you down, make a reference or pointer to it.\n- C++ language rules define the program semantics, but the asm implementation could pass by pointer in cases that are still pass-by-value according to C++. For example, the Windows x64 calling convention passes large structs by pointer (but non-const, so the caller needs to make a copy). A calling convention that passed by pointer with a caller-owns-the-memory semantics could optimize away the copy if the copy-constructor produces a bit-identical copy of the object. tl;dr: it's possible to design a calling convention like this without breaking C++, I think.\n- But the real problem might be objects that know their own address.\n- When doing link-time-optimization, a.k.a. link time code generation or whole program compilation, the compiler doesn't need to compile the call only based on the declaration. It has full insight into what's going on. For compiling embedded applications that are size- and speed-sensitive, link time code generation is the only way to go anyway.\n- You're kind of mixing up the asm calling-convention with how the compiler implements the semantics of the C++ source. This optimization could be made possible by designing the calling convention that way (see @supercat's answer) based only on the sizes of types, as you say not *just* by the compiler alone and not by looking inside the callee.\n- I think your answer is restricted to the context of ABI conventionism. In the core C++ language - especially with C++20 modules - we can strongly assume situations where each function body is known. Then the situation is interpreted entirely different, the question of OP DOES make sense and the compiler could generate code-graphs specialized for each invocation. But your answer is very relevant to today's compilers which are imperfect in many ways!\n- IIRC the const comment is not valid in D2.0. The compiler /is/ free to assume no changes via const references.\n- Was the 2nd line supposed to be `d += x; &#47;&#47; Oops`? Obviously code-gen for functions receiving args by reference would have to know that the caller owns the memory, so modification of that C++ object has to be done in a local copy. i.e. copy inside the receiving function if necessary.\n- Was preparing to write my own answer pointing out that this is a calling-convention-design optimization, or a private-function optimization. +1. The other answers seem to be assuming that the compiler would change the C++ source semantics, which obviously isn't allowed.\n- Full optimization for MSVC is `-Ox`, not just `-O2`. But that doesn't change the code-gen at all in this case. That's shockingly poor constant-propagation of the zeros in the struct, and CSE of using `argc` three times. Not to mention the store/reload. I know MSVC is usually not great, but this looked like a really easy optimization after inlining. Manually inlining makes MSVC just use `lea` to multiply by 3 like the other compilers, so apparently MSVC still sucks at optimizing after inlining. (IIRC, older versions sometimes failed to host vector constants out of loops after inlining.)\n- *Large structures are passed by hidden pointer* is only true for Windows x64. In x86-64 System V, large structs are passed by value on the stack. Windows x64 still has to make a copy because the callee owns the pointed-to memory (unless I guess the object dies so the caller can let their only copy be clobbered). I think the design decision is to optimize for variadic functions so every arg is 8 bytes and you can index them as an array. Possibly because it's also more forgiving of ABI mismatches when adding new members to the end of a struct that read-only callees don't know about.\n- I checked, none of gcc/clang/MSVC let their copy of a struct be clobbered when calling by value. godbolt.org/g/YFDhru. (`__attribute__((ms_abi))` for the Linux compilers). So that's a missed optimization.\n- @PeterCordes - I'm not following the problem with `&#47;O2` vs `&#47;Ox`. I just picked an arbitrary optimization level of `O2` for all three compilers but these certainly aren't necessarily equivalent: the compilers are very different beasts and take very different decisions both overall and at different levels. `O2` isn't the \"max\" optimization level for gcc or clang either.\n- @PeterCordes Large structures passed-by-value are passed by hidden pointer in SysV. Of course a copy may be made as the language requires - but they are still passed via hidden pointer (as opposed to registers which is used for smaller structures).\n- Even with only `-O2`, gcc and clang make optimal code. MSVC doesn't, so it's definitely worth checking if `-O2` is insufficient, or if MSVC just can't do it at all. (When I first commented, I hadn't realized you'd only used `-O2` for gcc/clang.)\n- In my (very old) experience what you are saying about `&#47;O2` vs `&#47;Ox` isn't true on MSVC: the same basic optimizations apply, but with some small differences. I.e,. `&#47;O2` isn't trying to compile faster or anything like that: the division is mostly that one has some small additional optimizations that have some kind of tradeoff.\n- Look again: godbolt.org/g/xbqJvn shows that gcc/clang don't set `rdi`, they *just* store to the stack above RSP before `call foo` for x86-64 SysV. The only access to `rdi` is to read `argc` from `edi`.\n- Re: MSVC. I don't know enough about it to be sure that a missed-optimization at `-O2` isn't going to be found at `-Ox`. I seem to recall seeing code-gen differences, but maybe only when auto-vectorizing. I forget what it was; I just always use `-Ox` when looking at MSVC output.\n- @PeterCordes - based on a quick search, `&#47;O2` does *strictly more* optimizations than `&#47;Ox`, on MSVC. So in perhaps I was actually favoring MSVC since I was using it's max O value (or close to it), while \"only\" using `-O2` for clang and gcc where `-O3` is significantly different (especially for gcc).\n- @PeterCordes - but in your `ms_abi` example you have this `byref(s)` call, which could capture a pointer to the `bar::s` object, so it isn't provably dead before (or after) the `foo(s)` call as your comment mentions, so I don't think compilers could pass `foo` the local copy (for example, `foo` could easily detect the optimization and break things, e.g,. by writing a `foo::s` which aliases `bar::s`). Here's an example that removes that call, and here in fact `clang` (only) seems to be able to optimize it: it passes its local copy of `s` by reference to `foo`.\n- oh right, good catch. Maybe I could have used inline asm to make the compiler put the struct in memory once, so there was something to copy from. A normal memory barrier won't work because the whole point is *not* to have escape-analysis think it may have escaped. Or perhaps a non-inline `__attribute__((pure))` function? I haven't played with that, and IDK how well it works. Or maybe a `*(volatile char*)&s` access to `s` would force it into memory without making it `volatile` itself.\n- Or I could just do `S s = generate(argc);`, because a function isn't allowed to keep a reference to its return value. In asm it has a pointer to it, but in C / C++ it doesn't. godbolt.org/g/BKihZY. gcc/clang/msvc still copy the return value to pass as an arg. And for x86-64 SysV, they don't use the return value as the stack-arg for a `call`, which would also be possible.\n- Sure - it's not too surprising to me that neither gcc or clang can optimize this yet: after all, `ms_abi` is probably pretty small part of their userbase and on the SysV side this optimization is much less useful. You can still make the argument that `clang` is kind of optimizing this case since as I linked above it avoids a copy when the address doesn't escape (but we can still argue whether it is passing by reference or simply eliding its local copy and doing its initialization directly in the on-stack copy it will pass to foo.\n- Note that this has diverged somewhat from what the OP is asking: here you are talking about whether the compiler can use the fact that a local variable is dead in order to elide the copy, but rather whether it can use the fact that the callee doesn't modify the object to avoid the copy in the caller and instead pass by \"reference\". It seems that at least in limited cases (mostly constrained by the on-stack passing in SysV) gcc can do this.\n- Your answer covers the original topic nicely, especially explaining how inlining avoids most of the need, at least for cases where it would be a decent fraction of the total work a program was doing. It's mostly only solveable as an ABI-design issue (pass by hidden const-ref that the callee can copy if it wants to modify it. I think the callee would have to be the one to run the copy-constructor in case it cares about the address of the object, because the caller doesn't know where it will be copied to. copy-constructors can be non-inline, so the rules can't depend on exactly what they do.)\n- I was assuming that compilers would optimize away their local copy of the object in the simpler case you linked, or at least re-store the from `edi` instead of store/reload/store! Surprised that's not the case for gcc. clang for ms_abi is interesting. But clang for SysV shows it's not eliding its local copy of the object in that case, so maybe the ms_abi case really is optimizing out the copy.\n- Yeah, having an ABI design that would allow that would be interesting. Stack passing doesn't seem all that bad since you are talking about a memory reference in either case (stack vs hidden pointer) and it saves registers by essentially multiplexing all stack arguments off of `rsp`. Hidden pointers could help in cases like this though. Passing-by-value is seeing a bit of resurgence in C++ due to move semantics, but it often doesn't pan out e.g., when objects don't have a move-constructor, so it could help there (i.e., you could repeatedly pass an object by value through a series of ...\n- ... calls without making a bunch of copies). Of course, inlining already usually solves this in the cases where it applies (which is all the time in C++ due to the style of most libraries). Good point about copy constructor complexities.\n- Another gotcha with passing on a buffer instead of copying: you need escape analysis to make sure there are no global pointers to it, otherwise the callee could find their by-value function arg modified as a side-effect of function calls! Added a section about that at the bottom of passing rvalue to non-ref parameter, why can't the compiler elide the copy?.","metadata":{"transformedAt":"2026-08-18T18:32:17.756Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":60,"totalLines":379,"estimatedTokens":4041}}85{"id":"stack-2280787","source":"stackoverflow","questionId":2280787,"title":"Unreachable code: error or warning?","tags":["compiler-construction","language-agnostic","language-design"],"text":"Title: Unreachable code: error or warning?\nTags: compiler-construction, language-agnostic, language-design\nSource: Stack Overflow\n\nQuestion:\nThis is a language design question:\n\nDo you think **unreachable code** (in programming languages in general) should raise a **warning** (i.e. \"report problem and compile anyway\") or an **error** (\"refuse to compile\")?\n\nPersonally I strongly feel it should be an error: if the programmer writes a piece of code, it should always be with the intention of actually running it in *some* scenario. But the C# compiler for example seems to disagree with this and merely reports a warning.\n\nNote: I realize good dead code detection is a very difficult problem, but that is not the focus of this question.\n\nHere are some examples of pieces of code where some statements are clearly unreachable:\n\n```\nreturn;\nfoo();\n```\n\n--\n\n```\nthrow new Exception();\nfoo();\n```\n\n--\n\n```\nif (...) {\n return;\n} else {\n throw new Exception();\n}\nfoo();\n```\n\n========================================\n\nTop Answer:\n**An error** means that the compiler is physically unable to deal with your code.\n\n**A warning** means that the compiler is capable of dealing with your code, but it believe that what you have written is wrong in some way.\n\nIt seems pretty clear cut to me - it should be a warning.\n\nBesides, what about the case where I've decided to shorten a method for debugging purposes:\n\n```\npublic bool ShowMenu()\n{\n return true;\n /* The standard implementation goes here */\n}\n```\n\nI know its wrong, but for the compiler to ask me to also comment out that code would just be a pain.\n\n========================================\n\nCode:\n```text\nreturn;\nfoo();\n```\n\n```text\nthrow new Exception();\nfoo();\n```\n\n```text\nif (...) {\n  return;\n} else {\n  throw new Exception();\n}\nfoo();\n```\n\n```text\nif (false) {\n  doStuffThatITemporarilyDisabled();\n}\n```\n\n```text\nint i = 2, j = 3;\nint result = 0;\n\n// FIXME: Commented out for now because I have to recheck calculation\nif (false) {\n  result = i*2+j+3+i*j;\n}\n\nSystem.out.println(\"Result of difficult calculation = \"+result);\n```\n\n```text\npublic bool ShowMenu()\n{\n    return true;\n    /* The standard implementation goes here */\n}\n```\n\n```c\n// C code\n#define ENABLE_FOO //Comment off this to turn off foo(void);\nint foo(void)\n{\n#ifdef ENABLE_FOO\n    // Do actual stuff.\n    int returnVaue = 2;\n    // ...\n    return returnValue;\n#else\n    return 0; // Turned off\n#endif\n}\n// Compiling using clang, enforcing dead code detection:\n// clang main.c -Wall -Werror\n```\n\n```c\n// Class\n#define MYCLASS_ENABLE_FOO // Comment off to enable -[MyClass foo]\n@interface MyClass : NSObject\n#ifdef MYCLASS_ENABLE_FOO\n- (int)foo;\n#endif\n@end\n// The like is done in implementation.\n// Invoking:\nint main(int argc, char **argv)\n{\n    MyClass *object = [[MyClass alloc] init];\n    int value = ([object respondsToSelector:@selector(foo)]) ? // Introspection.\n                [object foo] : 0;\n    printf(\"Value: %d\", value);\n    return 0;\n}\n```\n\n========================================\n\nComments:\n- This is true, but it's not an answer.\n- Unfortunately, Java and C# are kind of popular :)\n- Isn't this what comments are for?\n- @Martijn Using comments to comment out code is bad practice because the compiler cannot warn you if you forget to remove them.\n- Also: comments prevent the IDE from applying refactorings inside the disabled code.\n- The refactoring is a very good argument I hadn't thought about before! Thanks. :-)\n- I disagree. An error means \"I can't compile\". A warning is \"I can compile, but I think you did something wrong\". Under those definitions, this is most definitely a warning.\n- Furthermore, because of the Halting Problem and co., you can determine for sure that code will be run...so at best you're looking at heuristics. Improve the heuristics, and bam!...suddenly old code no longer compiles, and your compiler is not backwards compatible.\n- Per your statement (but not my opinion), Java sucks. See the Java Language Specification for J7SE (emphasis original): \"**It is a compile-time error if a statement cannot be executed because it is unreachable.**\" C# is different: \"A warning is reported if the compiler determines that a statement is unreachable. It is specifically not an error for a statement to be unreachable.\"\n- @DragonLord Why is it a problem if his premise leads to the logical conclusion that Java sucks? His premise is opinion, but if true, then Java does actually suck.","metadata":{"transformedAt":"2026-08-18T18:32:17.756Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":158,"estimatedTokens":1112}}86{"id":"stack-183882","source":"stackoverflow","questionId":183882,"title":"Interesting compiler projects","tags":["compiler-construction"],"text":"Title: Interesting compiler projects\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm currently in the process of choosing a project for a grad-level compiler course to be done over the next 8 weeks. I'd like to do something related to optimization since I haven't worked much in that area before, but anything in the field is fair game. \n\nWhat was the most interesting compiler-related project you've done? What did you learn the most from?\n\n**Edit:** Thank you all for your great suggestions. I apologize for not updating this for so long.\n\nThe project I ended up doing was a simple autovectorization optimization on LLVM. LLVM has vector types, but there didn't seem to be any way to take advantage of them without support for the front-end. This optimization converted normal scalar code into vector code.\n\nSince auto-vectorization is a fairly difficult optimization to implement, we limited our scope as much as we could. First, in order to expose instruction level parallelism in the code, we looked for one-block loops that matched our criteria, then unrolled them a specific number of times so they would be conveniently vectorizable. We then implemented the packing algorithm laid out in Exploiting Superword Level Parallelism with Multimedia Instruction Sets by Larsen and Amarasinghe.\n\nEven a simplified version of this optimization is pretty complicated. There are a lot of constraints; for instance, you don't want to vectorize a variable that lives out of the loop, since the rest of the program expects it to be scalar. We put in a lot of hours in the last few weeks. The project was a lot of fun though, and we learned a lot.\n\n========================================\n\nTop Answer:\nWith an 8-week timeframe, you're going to need to be careful about \"scope creep\". That is don't be too ambitious, esp. if this project includes other aspects of compiler construction (lexing/parsing), or if you're still learning the tools (debugger, yacc) and intermediate data structures (DAG).\n\nThat said, my first suggestion would be to try some Live Variable Analysis. The algorithms are pretty well established, so you'd pretty much just need to code it up specific to your data structures, etc.\n\nThis would let you do a limited form of Dead Code Removal. That is, if you detect that a variable is declared but never used, don't allocate space for it. If you detect that a value is set but never read, don't generate the set.\n\nLive Variable Analysis can help with Register Allocation too, so you might be able to tackle that too if there's time, and you should be able to re-use some of what you build for Dead Code Removal.\n\n========================================\n\nCode:\n```text\n:)\n```\n\n========================================\n\nComments:\n- So Jay it's been over 8 weeks. Let us know what happened.\n- Any news Jay? I will soon start teaching an undergrad compiler course and it will be interesting to know what you did.\n- With you totally. I think generating simple languages (or getting existing languages to act like the language you want) is a very basic skill.\n- Good answer. I would only add what I said in my own answer about optimization - that it is overrated.\n- We used a Motorola 68000 as if it were a stack machine. You can't get much simpler than that.\n- I often wish IBM had chosen Motorola instead of Intel. Things would be a lot different.\n- I doubt it could be done in 8-week time frame. Automatic Vectorization is a notoriously hard problem, and even GCC doesn't have that.\n- I hate to mark an accepted answer since these are all great suggestions, and there isn't really a true answer to this question. However, this is the project we actually did, and it was a very interesting project so I feel I should award some points here.\n- @Calyth, You're right, we never could have implemented a full autovectorization optimization in 8 weeks. We limited the scope of our optimization as much as possible without making it completely useless. We still ended up putting more hours into it than we anticipated.\n- I'm not sure tail-calls make sense in an OO language, because you need to statically know which method will get executed for the recursive message send. So that means doing that in a JIT, using runtime type feedback, if I'm not mistaken ?\n- Tail-call optimization does not need to statically know the target. The JVM does present a different obstacle (stack introspection), which can be finessed though (there was a paper by Appel and others).\n- I think you meant to say \"Python to C++\". Yes. Good project.\n- Even though I'm many months too late, I'm upvoting this in support of Shed Skin. It's what I would have suggested had I found this question at the time it was asked.","metadata":{"transformedAt":"2026-08-18T18:32:17.756Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":51,"estimatedTokens":1180}}87{"id":"stack-1921474","source":"stackoverflow","questionId":1921474,"title":"Difference between compilers and parsers?","tags":["parsing","compiler-construction"],"text":"Title: Difference between compilers and parsers?\nTags: parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nBy concept/function/implementation, what are the differences between compilers and parsers?\n\n========================================\n\nTop Answer:\nA parser just reads a text into an internal, more abstract representation, often a tree or graph of some sort.\n\nA compiler translates such an internal representation into another format. Most often this means converting source code into executable programs. But the target doesn't have to be machine code. It can be another programming language as well; the compiler would still be a compiler. Obviously a compiler needs a parser to actually read its input.\n\n========================================\n\nCode:\n```text\nparses\n```\n\n========================================\n\nComments:\n- What's the difference between a refrigerator and a compressor.\n- The refrigerator makes heavy use of the compressor to do it's job but a compressor is by no means a refrigerator. Substitute compiler for refigirator and parser for compressor.\n- I'd like to think the question the OP meant was:\"What's the relationship between compilers and parsers?\" (which most answers seem to deal with), but then the word 'difference' is not only in the title, but in the main question body too, in which case Nifle's analogy should be sufficient. ;-)\n- +1 for noting the different parts and also mentioning how they would relate to text (even though for a theoretical computer scientist a complete program would be a single word :-)).\n- A parser would check for the syntax of sentences being correct, though. And the lexer might already look into a dictionary to see whether the words are made up or are indeed correct words to use. But that task is a little fuzzy between lexers and parsers, though.\n- The parser not only checks the syntax, but checking the syntax is more a byproduct of constructing an abstract representation, isn't it?\n- I think it is the other way around: An abstract representation may be a by-product of checking the syntax.\n- parsers do not have to produce a tree structure\n- Do you have an example for a parser which does not produce a tree?\n- What about a parser for a language that represents arbitrary graphs? Parsers generally just translate something textual into some internal representation. The latter one is most often a tree for languages with formally-defined context-free grammars but it can actually be anything that is convenient to handle.\n- A recursive decent parser does not have to produce a tree.\n- Yes, but do you have a concrete example?\n- Sure, the recursive descent Small C compiler I wrote about 25 years ago. But seriously, recursive descent is a well known technique - do you doubt its existence?\n- @Neil: No, just not refreshing before commenting and therefore missing the last comment","metadata":{"transformedAt":"2026-08-18T18:32:17.756Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":38,"estimatedTokens":718}}88{"id":"stack-14324546","source":"stackoverflow","questionId":14324546,"title":"Why do the older C language specs require function-local variables to be declared up-front?","tags":["c","compiler-construction","language-design"],"text":"Title: Why do the older C language specs require function-local variables to be declared up-front?\nTags: c, compiler-construction, language-design\nSource: Stack Overflow\n\nQuestion:\nIn the C programming language, all of the language revisions I have worked with enforced up-front variable declarations before any non-declarative/assignative expressions would be evaluated. C++ seems to have waived this requirement from all versions. I also recognize more modern version of C have waived this requirement as well, but I have yet to use any of those standards. \n\nThe question I have is this: **What historic reason was there for preventing the C language from declaring freely on-demand instead of up front?**\n\nObviously there are a number of reasons that come to mind from an engineering standpoint, but none of them seem especially plausible to me.\n\n- Preventing an obscure compiler behavioral error from occurring (such as infinite parsing loops, a massive memory bloat for evaluation, or some weird corner cases with Macros.)\n\n- Preventing undesirable compiler output. This could be anything from symbol output muddling the debug process and the ease of development of debugging tools, to unexpected stack storage orders.\n\n- Readability. I find this hard to swallow as well, seeing as C, while designed for readability compared to other languages of the era, did not enforce this type of structure nearly anywhere else. (Unless you see prototyping as being a similar enforcement, but if I recall prototypes were added in the '89 spec.)\n\n- Implementation complexity and practical reasons. This is the one I'm most inclined to believe. As engineers we have to make certain considerations in order to ship a viable product in a time-frame allotted. While I will grant that the professional landscape for Computer Science and Software Engineering have both changed dramatically, Business is still business. At the end of the day I'm sure Bell wanted a finished product that could be used in the Unix programming environment to showcase what they had achieved.\n\nDoes anyone have any good sources backing up any of the above? Did I miss something entirely? We can speculate from dawn till dusk, but I'm looking for good hard references.\n\n========================================\n\nTop Answer:\nIn C89, variable definitions are required to be at the start of a block. (See the C standard for the definition of a block) This was as far as I know done to simplify the way the variables are handled in assembler. For example, let us have a look at a simple function:\n\n```\nvoid foo()\n{\n int i = 5;\n printf(\"%i\\n\", i);\n}\n```\n\nwhen the gcc translates this function into assembler code, the call to foo() would boil down to a bunch of instructions, including setting up a stackframe for the function scope. this stackframe includes space for the variables defined in the scope of the function, and to match the same scope in the higher level language C, they were required to be defined at the beginning of the block.\n\nAt the end, it was about ease of implementation, and also efficiency, because declaring a bunch of variables at once, that it, at the beginning of a block, enables the compiler to bulk-push them on the stack, and around ~89 that was also a performance consideration.\n\nOf course, this answer is horribly simplified and aims only to give a brief idea about why this has been done the way it was done. For more Information, you should probably read some drafts of the early C89 standard.\n\n========================================\n\nCode:\n```text\nc02.c\n```\n\n```text\nblkhed()\n```\n\n```text\nauto\n```\n\n```text\nreturn\n```\n\n```text\nvoid foo()\n{\n    int i = 5;\n    printf(\"%i\\n\", i);\n}\n```\n\n```text\nint a 5;\nint b a; /* ERROR in nascent versions of C */\n```\n\n```text\nint a = 5, b = a;\nstruct { int x, y; } s = { a, b }; /* ERRROR even in C89/90 */\n```\n\n```text\n=\n```\n\n========================================\n\nComments:\n- I believe this may be a dupe. But I'm too lazy to look for it.\n- I don't think any technical reason. It's a pretty trivial code transformation to add a block that opens before any declaration that occurs after an expression-statement, and that extends to the end of the block in which the declaration occurs. AFAIK, by the time of C89 it's a pretty much arbitrary restriction because there wasn't (yet) enough support for the alternative.\n- Tagged `language-design` since you're asking about rationale behind some design decisions.\n- Right, I gave that reason under #4. But that isn't a fact, it's an opinion.\n- Btw, it's worth noting that standard C has *never* required variables to be declared up-front in functions. In C89 they can be declared at the start of any block within a function. But you might find compilers that responded to this by moving the stack pointer every time a block is entered that has an initial declarative reason -- such compilers could conform to C89 whilst still making it \"a bad idea\" to actually use the facility.\n- Steve, I don't know if that is true or not, it would make sense however. Can someone confirm that a standards compliant compiler on the original 73' standard can do this? I don't even remember the 89' standard supporting this, but I could see it based on other 89' optimizations.\n- @DanielGreen: Ah, for the purposes of my remark, the \"original '73 standard\" isn't a standard, it's just whatever Ritchie shipped.\n- @SteveJessop, I thought that ANSI ratified the 73 version of K&R C. Seems I am wrong. A quick trip down Wiki lane proved that. Learned something new.\n- I doubt you'll find hard evidence for the reason. The place to look would be Dennis Ritchie's home page. He has a section about C and its immediate ancestors that has a fair amount of early history of C. I've read through most of what's there, and don't recall seeing this mentioned. The early compiler code tends to agree with the simplicity idea though -- it used *seriously* tricky code to fit into available memory (e.g., the code generator reused the space occupied by the parser code to store data after parsing was finished).\n- @DanielGreen this quote was already present in K&R 1st edition *\"Declarations of variables (including initializations) may the left brace that introduces any compound statement, not just the one that begins a function*\n- \"More modern\" versions? C99 has been around long enough to need to shave!\n- @ecatmur: \"modern\" generally means \"more recently than I started doing it\" ;-) At Oxford University (where I studied mathematics), \"modern history\" starts in the 15th century (or probably more like the 4th century if you ask a Classicist). Has done ever since the first Regius Professor of Modern History was appointed in 1724, always will do!\n- I'm far from an expert on this, but my guess was always that folks used to assembly programming liked being able to see all the stack variables in one place and mentally add up the function stack size.\n- Excellent detective work! This actually answers my question quite effectively ecatmur!","metadata":{"transformedAt":"2026-08-18T18:32:17.756Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":98,"estimatedTokens":1750}}89{"id":"stack-1218262","source":"stackoverflow","questionId":1218262,"title":"Why do some compilers use \"a.out\" as the default name for executables?","tags":["c","unix","history","compiler-construction"],"text":"Title: Why do some compilers use \"a.out\" as the default name for executables?\nTags: c, unix, history, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nMost UNIX C compilers link executables by default to a file called \"a.out\". Why? Is this a written standard or just de-facto standard behavior? What would break if these compilers would just produce an error message (or use a different default name) instead of producing \"a.out\"?\n\n========================================\n\nTop Answer:\nA.out is actually the name of an UNIX executable file format. (ELF is another)\n\nThe compiler produces that name (by tradition) if you don't give your executable a name explicitly.\n\n What would break if these compilers\n would just error out instead of\n producing \"a.out\"?\n\nNothing would break, but what do you mean by 'error out'? Do you mean, refuse to compile unless an executable name is supplied to the linker ?\n\n========================================\n\nComments:\n- We never know if it won't break something. There might still be some tools that depend on this fact of automatically generated filenames for the resulting executable. Most of the *modern* tools don't, though.\n- a.out is also the name of the executable format. Makes for a nice handy 8.3 filename too though.\n- `a.out` has not been the default executable format for more than a decade. These days `ELF` (Executable Linkable Format) is used.\n- @JesperJuhl But the question was not about what is in use currently, but about where did the name come from. Discussing other, more modern formats, is irrelevant to the question.\n- @BarbaraKwarc \"assembler output\" I think.","metadata":{"transformedAt":"2026-08-18T18:32:17.756Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":28,"estimatedTokens":407}}90{"id":"stack-501412","source":"stackoverflow","questionId":501412,"title":"Why does autoboxing make some calls ambiguous in Java?","tags":["java","compiler-construction","overloading","autoboxing"],"text":"Title: Why does autoboxing make some calls ambiguous in Java?\nTags: java, compiler-construction, overloading, autoboxing\nSource: Stack Overflow\n\nQuestion:\nI noticed today that auto-boxing can sometimes cause ambiguity in method overload resolution. The simplest example appears to be this:\n\n```\npublic class Test {\n static void f(Object a, boolean b) {}\n static void f(Object a, Object b) {}\n\n static void m(int a, boolean b) { f(a,b); }\n}\n```\n\nWhen compiled, it causes the following error:\n\n```\nTest.java:5: reference to f is ambiguous, both method\n f(java.lang.Object,boolean) in Test and method\n f(java.lang.Object,java.lang.Object) in Test match\n\nstatic void m(int a, boolean b) { f(a, b); }\n ^\n```\n\nThe fix to this error is trivial: just use explicit auto-boxing:\n\n```\nstatic void m(int a, boolean b) { f((Object)a, b); }\n```\n\nWhich correctly calls the first overload as expected.\n\nSo why did the overload resolution fail? Why didn't the compiler auto-box the first argument, and accept the second argument normally? Why did I have to request auto-boxing explicitly?\n\n========================================\n\nTop Answer:\nThe compiler *did* auto-box the first argument. Once that was done, it's the second argument that's ambiguous, as it could be seen as either boolean or Object.\n\nThis page explains the rules for autoboxing and selecting which method to invoke. The compiler first tries to select a method *without using any autoboxing at all*, because boxing and unboxing carry performance penalties. If no method can be selected without resorting to boxing, as in this case, then boxing is on the table for *all* arguments to that method.\n\n========================================\n\nCode:\n```text\npublic class Test {\n    static void f(Object a, boolean b) {}\n    static void f(Object a, Object b) {}\n\n    static void m(int a, boolean b) { f(a,b); }\n}\n```\n\n```text\nTest.java:5: reference to f is ambiguous, both method\n    f(java.lang.Object,boolean) in Test and method\n    f(java.lang.Object,java.lang.Object) in Test match\n\nstatic void m(int a, boolean b) { f(a, b); }\n                                  ^\n```\n\n```text\nstatic void m(int a, boolean b) { f((Object)a, b); }\n```\n\n```text\npublic class Test {\n    static void f(Object a, boolean b) {}\n    static void f(int a, Object b) {}\n\n    static void m(int a, boolean b) { f(a, b); } // ambiguous\n}\n```\n\n```text\nf(boolean a, Object b)\nf(Object a , boolean b)\n```\n\n========================================\n\nComments:\n- Thanks @Kevin. Yes, I could explicitly box it, but I didn't. So why didn't it choose the most specific overload, which in this case is the first one?\n- Because the compiler didn't have enough information to make that decision for you. *Both* methods applied because boolean can be boxed to an Object\n- I don't think this answers the question. When multiple methods apply, the one that requires the least amount of widening conversion is automatically chosen. You get an ambiguity error only when multiple methods need the same number of \"widenings\". My guess is that boxing isn't counted as widening.\n- Thanks @Niyaz. But even if a and b can be converted to objects, it's *not necessary*. So, the compiler (IMHO) must convert a to an object, but for b it should choose the most specific overload, which is the first one. What's wrong with this logic?\n- If there are many more arguments in the given example, how does the compiler select the \"most specific overload\"? That is the problem.\n- Niyaz. what difference does it make if he casts to Object ? both functions accept Object. so i would say by instinct that it doesn't make any better match.\n- As I said, when the compiler tries to match by itself: if A can be converted to an object, so can be B. So either of the two functions are possible. When we cast A to Object manually, (even though in both functions A is an Object) the compiler does not need any more casts. It just matches.\n- But couldn't it try to \"perform the least possible number of boxing/unboxing conversions\"? That would lead to the most specific overload, wouldn't it? (Of course, if the number of least possible conversions ties for two or more methods then the call is ambiguous.)\n- The compiler can select functions by matching the arguments. It usually does like that. But tat is done only when there is NO ambiguity. If there is one, since there is no way to know which function the programmer REALLY intended to call, the compiler gave an error.\n- In that case, wouldn't f(Object,boolean) be the more \"specific\" method?\n- Thank you. But why is it not trying to perform the least possible number of boxing/unboxing conversions? Why is it all or none?\n- @Hosam: Excellent question, but I don't know the definitive answer. Possibly because it would be too computationally complex to find the invocation with the least conversions. It's a much simpler implementation for the compiler to apply boxing to all parameters or none.\n- \"because boxing and unboxing carry performance penalties.\" True, but not the issue here. Phase 1 is to guarantee backward compatibility with pre-JDK5.\n- @Bill, I don't think it's too complex computationally. It's just a matter of calculating the number of conversions for each candidate method, sorting them, and choosing the best one (unless the first two are equal). This does't seem complex to me!\n- so, it's like \"once auto-boxed, we can go dirty with other arguments too\" and \"if no auto-box happened yet, we try not to autobox others too\" ?\n- Thank you @iny. I apologize for being out of votes for today!\n- @litb, yes apparently so. But I wonder why they are not trying to \"perform the least possible number of boxing/unboxing conversions\".\n- Thank you @eljenso. This clarifies the compiler's issue, but then it makes me wonder why the second phase is defined as so. Couldn't it be amended with \"doing the least possible number of boxing/unboxing conversions\"?\n- Well, thank you @eljenso! I agree that, in your example, the call is ambiguous. I would think that the two overloads will cost the same number of boxing conversions, so the call is indeed ambiguous. But (regardless of the JLS), I don't see my example ambiguous. What do you think?\n- I can understand your point of view when you say that you think f(Object, boolean) is a better match than f(Object, Object). However, the JLS is unambiguous here and says your call is ambiguous. You will have to come up with your own language to implement your proposed method lookup algorithm.\n- (contd.) But you may not call it Java then. Method overloading is already one of the hardest parts to understand/support in a statically typed language. So instead of complicating things even further with boxing, it would be best to avoid overloading or disallow it altogether.\n- Actually, the real reason for the error is &#167;15.12.2.5. The rules for selecting the \"most specific method\" from the list only consider subtyping conversions, and don't take into the account auto boxing that are considered during \"phase 2\". You may want to update the answer.\n- I've updated the answer, I hope you like it better now. Thanks Scott.","metadata":{"transformedAt":"2026-08-18T18:32:17.757Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":109,"estimatedTokens":1778}}91{"id":"stack-1942801","source":"stackoverflow","questionId":1942801,"title":"When should I omit the frame pointer?","tags":["c","optimization","compiler-construction"],"text":"Title: When should I omit the frame pointer?\nTags: c, optimization, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIs there any substantial optimization when omitting the frame pointer?\nIf I have understood correctly by reading this page, `-fomit-frame-pointer` is used when we want to avoid saving, setting up and restoring frame pointers.\n\nIs this done only for each function call and if so, is it really worth to avoid a few instructions for every function?\nIsn't it trivial for an optimization.\nWhat are the actual implications of using this option apart from the debugging limitations?\n\nI compiled the following C code with and without this option\n\n```\nint main(void)\n{\n int i;\n\n i = myf(1, 2);\n}\n\nint myf(int a, int b)\n{\n return a + b;\n}\n```\n\n,\n\n```\n# gcc -S -fomit-frame-pointer code.c -o withoutfp.s\n# gcc -S code.c -o withfp.s\n```\n\n.\n\n`diff -u` 'ing the two files revealed the following assembly code:\n\n```\n--- withfp.s 2009-12-22 00:03:59.000000000 +0000\n+++ withoutfp.s 2009-12-22 00:04:17.000000000 +0000\n@@ -7,17 +7,14 @@\n leal 4(%esp), %ecx\n andl $-16, %esp\n pushl -4(%ecx)\n- pushl %ebp\n- movl %esp, %ebp\n pushl %ecx\n- subl $36, %esp\n+ subl $24, %esp\n movl $2, 4(%esp)\n movl $1, (%esp)\n call myf\n- movl %eax, -8(%ebp)\n- addl $36, %esp\n+ movl %eax, 20(%esp)\n+ addl $24, %esp\n popl %ecx\n- popl %ebp\n leal -4(%ecx), %esp\n ret\n .size main, .-main\n@@ -25,11 +22,8 @@\n .globl myf\n .type myf, @function\n myf:\n- pushl %ebp\n- movl %esp, %ebp\n- movl 12(%ebp), %eax\n- addl 8(%ebp), %eax\n- popl %ebp\n+ movl 8(%esp), %eax\n+ addl 4(%esp), %eax\n ret\n .size myf, .-myf\n .ident \"GCC: (GNU) 4.2.1 20070719\n```\n\nCould someone please shed light on the **key** points of the above code where -fomit-frame-pointer did actually make the difference?\n\n**Edit:** `objdump`'s output replaced with `gcc -S`'s\n\n========================================\n\nTop Answer:\nThe only downside of omitting it is that debugging is much more difficult.\n\nThe major upside is that there is one extra general purpose register which can make a big difference on performance. Obviously this extra register is used only when needed (probably in your very simple function it isn't); in some functions it makes more difference than in others.\n\n========================================\n\nCode:\n```text\nint main(void)\n{\n        int i;\n\n        i = myf(1, 2);\n}\n\nint myf(int a, int b)\n{\n        return a + b;\n}\n```\n\n```text\n# gcc -S -fomit-frame-pointer code.c -o withoutfp.s\n# gcc -S code.c -o withfp.s\n```\n\n```text\n--- withfp.s    2009-12-22 00:03:59.000000000 +0000\n+++ withoutfp.s 2009-12-22 00:04:17.000000000 +0000\n@@ -7,17 +7,14 @@\n        leal    4(%esp), %ecx\n        andl    $-16, %esp\n        pushl   -4(%ecx)\n-       pushl   %ebp\n-       movl    %esp, %ebp\n        pushl   %ecx\n-       subl    $36, %esp\n+       subl    $24, %esp\n        movl    $2, 4(%esp)\n        movl    $1, (%esp)\n        call    myf\n-       movl    %eax, -8(%ebp)\n-       addl    $36, %esp\n+       movl    %eax, 20(%esp)\n+       addl    $24, %esp\n        popl    %ecx\n-       popl    %ebp\n        leal    -4(%ecx), %esp\n        ret\n        .size   main, .-main\n@@ -25,11 +22,8 @@\n .globl myf\n        .type   myf, @function\n myf:\n-       pushl   %ebp\n-       movl    %esp, %ebp\n-       movl    12(%ebp), %eax\n-       addl    8(%ebp), %eax\n-       popl    %ebp\n+       movl    8(%esp), %eax\n+       addl    4(%esp), %eax\n        ret\n        .size   myf, .-myf\n        .ident  \"GCC: (GNU) 4.2.1 20070719\n```\n\n```text\n-fomit-frame-pointer\n```\n\n```text\ndiff -u\n```\n\n```text\nobjdump\n```\n\n```text\ngcc -S\n```\n\n```text\n-fomit-frame-pointer\n```\n\n```text\n$ gcc code.c -S -o withfp.s\n$ gcc code.c -S -o withoutfp.s -fomit-frame-pointer\n$ diff -u withfp.s withoutfp.s\n```\n\n```text\nmyf:\n-       pushl   %ebp\n-       movl    %esp, %ebp\n-       movl    12(%ebp), %eax\n-       addl    8(%ebp), %eax\n-       popl    %ebp\n+       movl    8(%esp), %eax\n+       addl    4(%esp), %eax\n    ret\n```\n\n```text\n-S\n```\n\n========================================\n\nComments:\n- Try the diff again by compiling with -S. Compare the assembly language: It will be much more readable.\n- Related, see ARM: link register and frame pointer.\n- On `x64` it is omitted by default - see Why does GCC drop the frame pointer on 64-bit?\n- If I have to set explicitly other optimization options, what's the meaning of this option being separate? Your point that my code is simple seems valid though!\n- This option is separate because it significant downsides for debugging.\n- It's separate because it has functional implications for other things, like running your code in a debugger, or linking with other code. I assume you'd see a reduction in register spills even with the optimizer turned off, but since I don't know for sure I'm hedging my bets.\n- @EricSeppanen can you elaborate on the difference between ISA and micro-architecture registers? Do you perhaps mean x87/MMX/SSE/etc. with the latter? Or \"internal\" registers (like eip)? Thanks!\n- I realized that micro-architecture registers most probably refer to registers not seen b the programmer, but rather accessed by register renaming etc. Can anyone confirm that's true? Thanks!\n- @andreee: Yes, this answer is pretty clearly meaning architectural vs. physical registers (that architectural are renamed onto). Register renaming avoids false dependencies when you reuse the same architectural register for a different value (when you're done with the old value you had there), but it doesn't help you if you need 9 different variables \"live\" in the same loop.\n- Not only does it make debugging much more dufficult. Gnu docsonline says that it makes debugging impossible\n- They are wrong. `printf()` debugging (which **IS** still debugging) is very possible, for example.\n- You can still debug at the instruction (assembly language) level regardless of any compiler options used. Not as easy as source level debugging to be sure, but \"impossible\" is definitely the wrong word.\n- This answer is obsolete (probably even when it was posted). Modern debug info formats like DWARF make it possible for a debugger to find variable values even when they're in registers in optimized code (if they exist at all). For un-optimized code, `gcc -O0 -fomit-frame-pointer -g` should have a negligible effect on debugging on GNU/Linux. Not that you *should* bother to use `-fomit-frame-pointer` in debug builds, but that proves it doesn't make debugging much if any harder.\n- There are still situations where frame pointers are necessary for a good debugging experience. The Linux kernel doesn't have a DWARF parser (and is unlikely to gain one), so tools that stack walk inside the kernel (e.g. bpftrace's `ustack()`) don't work properly without frame pointers.","metadata":{"transformedAt":"2026-08-18T18:32:17.757Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":211,"estimatedTokens":1679}}92{"id":"stack-1540886","source":"stackoverflow","questionId":1540886,"title":"What _did_ the C operators /\\ and \\/ do?","tags":["c","compiler-construction","operators","history","algol"],"text":"Title: What _did_ the C operators /\\ and \\/ do?\nTags: c, compiler-construction, operators, history, algol\nSource: Stack Overflow\n\nQuestion:\nAnyone can \"declare\" ones own operators in C.... that is if one is a C compiler guru and has the source code to the C compiler! ;-)\n\nFurther questions to puzzle:\n\nHow are these operations done in\nC99? gcc? ...\n\n- And why were /\\ & \\/ dropped?\n\n- Which types were the /\\ and \\/ operators valid for?\n\nGoogling for \"/\\ \\/\" naturally returns nothing. Wikipedia has a page for neither /\\ nor \\/. But I have spotted form similar operators are built into the XML character entities!\n*Source added:* I found the offending example in the PDP's cc source file \"c00.c\":\n\n```\n/*\n * Return the next symbol from the input.\n * peeksym is a pushed-back symbol, peekc is a pushed-back\n * character (after peeksym).\n * mosflg means that the next symbol, if an identifier,\n * is a member of structure or a structure tag or an enum tag\n */\nsymbol()\n{\n...\n case BSLASH:\n if (subseq('/', 0, 1))\n return(MAX);\n goto unkn;\n\n case DIVIDE:\n if (subseq('\\\\', 0, 1))\n return(MIN);\n if (subseq('*',1,0))\n return(DIVIDE);\n...\n}\n```\n\n *Actual Implementations:*\nThe /\\ and \\/ operators date back as far as Sixth Edition Unix 1975 (so far). Examples: Unix V6(1975), Unix V7(1979) and more currently BSD 2.11(1992-2008)\n\n========================================\n\nTop Answer:\nNeither /\\ nor / are defined as operators in the ISO C89 standard, and I don't think they were ever defined in any earlier version. And they are definitely not defined in C99 as far as I know.\n\nHere's a draft of the ANSI C89 standard, for reference: http://flash-gordon.me.uk/ansi.c.txt\n\n(You are likely a victim of some weird arcane preprocessor magic)\n\n========================================\n\nCode:\n```text\n/*\n * Return the next symbol from the input.\n * peeksym is a pushed-back symbol, peekc is a pushed-back\n * character (after peeksym).\n * mosflg means that the next symbol, if an identifier,\n * is a member of structure or a structure tag or an enum tag\n */\nsymbol()\n{\n...\n case BSLASH:\n  if (subseq('/', 0, 1))\n   return(MAX);\n  goto unkn;\n\n case DIVIDE:\n  if (subseq('\\\\', 0, 1))\n   return(MIN);\n  if (subseq('*',1,0))\n   return(DIVIDE);\n...\n}\n```\n\n```text\n\\/\n```\n\n```text\nsup\n```\n\n```text\n/\\\n```\n\n```text\ninf\n```\n\n```text\n^\n```\n\n```text\na /\\ b   ===>   a / b\n\na \\/ b   ===>   a / b\n```\n\n```text\na = b /\\ c;\n```\n\n```text\na = b \\/ c;\n```\n\n```text\n+=\n```\n\n```text\n=+\n```\n\n```text\na =/\\ b;\n```\n\n```text\na /\\= b;\n```\n\n```text\n/\\\n/ Comment content\n```\n\n```text\n/\\\n\\\n\\\n\\\n\\\n/ Still a legal comment.\n```\n\n```text\n// Yet another comment \\\nThis line is in the comment \\\\\nAnd so is this one!\n```\n\n```text\n\\/\n```\n\n```text\n/\\\n```\n\n========================================\n\nComments:\n- I'm ... unaware that these pairs of characters ever had a meaning in C. what makes you think that they did?\n- I don't believe I've ever seen those operators anywhere. Ever.\n- the logical symbols `AND` and `OR` ?\n- Where in code did you see this? Could you some lines?\n- Interesting! So it looks plausible for '`a = b &#47;\\ c;`' to assign the maxiumum of b and c to a, and '`a = b \\&#47; c;`' to do the minimum. And, in those days, it was probable that the modern '`+=`' operators were still written as '`=+`' and were in fact two tokens, so the hypothetical '`a =&#47;\\ b;`' would have been the max-assignment operator, etc. Intriguing!\n- It also occurs to me that Thompson's ACM Turing Award speech 'Reflections On Trusting Trust' (ece.cmu.edu/~ganger/712.fall02/papers/p761-thompson.pdf) is somehow relevant.\n- @Jonathan Leffler: without the full context of the code it's hard to be sure, but it *looks* like `\\&#47;` was MAX and `&#47;\\` was`MIN`. Probably not the direction I would have chosen!\n- @Greg - yup...I suspect you're correct. Maybe that was why it was never standardized?\n- @Jonathan: quite possibly. Not to mention the confusion with the standard symbols for logical AND and OR as Nick D mentions above.\n- +1 for the most interesting question I've come across that started out looking like one of the goofiest. I'd never heard of these operators before - if only they had stuck we'd have been saved years of headaches having to deal with crappy MIN/MAX macros that evaluated operands twice.\n- +1 for weird arcane preprocessor magic. This looks like the kind of thing you might see in an IOCCC entry.\n- I think the OP means the two-character combinations `&#47;` and`\\/` which are even more meaningless.\n- If someone does this in code I have to maintain, they'd damn well have better left the state...\n- @T.E.D. - what about splitting the / and the * of a comment over multiple lines by using backslash-newline?\n- `\\&#47;` for `sup` and `&#47;\\` for`inf` are established mathematical notation. See also en.wikipedia.org/wiki/Lattice_%28order%29\n- This reminds me of the deprecated ? (min and max) operators and their assignment variants ?= that G++ used to allow and that I've had the misfortune of encountering.\n- You probably guess right: I found implementations of the /\\ and \\/ operators in both Unix V6 & V7. (bitsavers.org/bits/Interdata/32bit/unix/univWollongong_v6/&hellip; & stuff.mit.edu/afs/sipb/project/v7unix/fs/usr/src/cmd/c/c00.c&zwnj;&#8203;). I appears that K&R forgot to document /\\ and \\/ when they wrote the old testament. Now all I need to know is where TH is Wollongong? ;-)\n- @NevilleDNZ Good job. See maps.google.com/&hellip;\n- \"The internet\" says the \\ character was added to ASCII by 'Bob Bemer' specifically to allow the /\\ and \\/ operators. c.f. home.ccil.org/~remlaps/www.bobbemer.com/BRACES.HTM - Maybe sometimes - even to this day - MIN and MAX are implemented in C with precarious side effects: eg #define MIN(a,b) ((a<b)?(a):(b))\n- Just curious -why were the terms 'sup' and 'inf' used instead of what I'd think were the more immediately understandable 'max' and 'min'? The linked pages describing 'Supremum' and 'Infimum' make my head hurt.\n- Because, in general, a maximum and a minimum of a set don't need to exists. For example, `[0,1)` has not maximum but its supremum is well defined. See also en.wikipedia.org/wiki/Lattice_%28order%29 and associated references.\n- The language surprises me once again.\n- I saw this when I was browsing through the C99 standard and thought, \"Hey that's cool! ... and mostly useless.\"\n- How many comment matching regular expressions handle this correctly? And does it actually matter, except to compiler writers who have to worry about such details? (Answers: very few, and not really.)","metadata":{"transformedAt":"2026-08-18T18:32:17.757Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":18,"totalLines":189,"estimatedTokens":1635}}93{"id":"stack-4016305","source":"stackoverflow","questionId":4016305,"title":"How to emit and execute Java bytecode at runtime?","tags":["java","jvm","code-generation","bytecode","compiler-construction"],"text":"Title: How to emit and execute Java bytecode at runtime?\nTags: java, jvm, code-generation, bytecode, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am writing an interpreter in Java for a domain-specific language with some scripting capabilities. I have already implemented a parser and now need to do a back end. To this end I am considering either to write my own interpreter (either working with abstract syntax trees or with some custom bytecodes) or target JVM (emit and execute Java bytecode at runtime).\n\nCould someone with more experience in this area say how feasible is the approach of targeting JVM and what libraries would you recommend to use for emitting Java bytecode?\n\n========================================\n\nTop Answer:\nI can suggest that you take a look at these libraries:\n\n- CGLIB\n\n- BCEL\n\n- asm\n\n- Javassist\n\n========================================\n\nCode:\n```text\npackage hello;\n\nimport java.lang.reflect.Method;\n\nimport org.objectweb.asm.ClassWriter;\nimport org.objectweb.asm.Label;\nimport org.objectweb.asm.MethodVisitor;\nimport org.objectweb.asm.Opcodes;\n\npublic class HelloWorldASM implements Opcodes {\n    public static byte[] compile(String name) {\n        ClassWriter cw = new ClassWriter(0);\n        MethodVisitor mv;\n\n        cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, \"hello/HelloWorld\", null,\n                \"java/lang/Object\", null);\n\n        cw.visitSource(\"HelloWorld.java\", null);\n\n        {\n            mv = cw.visitMethod(ACC_PUBLIC, \"<init>\", \"()V\", null, null);\n            mv.visitCode();\n            Label l0 = new Label();\n            mv.visitLabel(l0);\n            mv.visitLineNumber(4, l0);\n            mv.visitVarInsn(ALOAD, 0);\n            mv.visitMethodInsn(INVOKESPECIAL, \"java/lang/Object\", \"<init>\",\n                    \"()V\");\n            mv.visitInsn(RETURN);\n            Label l1 = new Label();\n            mv.visitLabel(l1);\n            mv.visitLocalVariable(\"this\", \"Lhello/HelloWorld;\", null, l0, l1,\n                    0);\n            mv.visitMaxs(1, 1);\n            mv.visitEnd();\n        }\n        {\n            mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, \"main\",\n                    \"([Ljava/lang/String;)V\", null, null);\n            mv.visitCode();\n            Label l0 = new Label();\n            mv.visitLabel(l0);\n            mv.visitLineNumber(7, l0);\n            mv.visitFieldInsn(GETSTATIC, \"java/lang/System\", \"out\",\n                    \"Ljava/io/PrintStream;\");\n            mv.visitLdcInsn(String.format(\"Hello, %s!\", name));\n            mv.visitMethodInsn(INVOKEVIRTUAL, \"java/io/PrintStream\", \"println\",\n                    \"(Ljava/lang/String;)V\");\n            Label l1 = new Label();\n            mv.visitLabel(l1);\n            mv.visitLineNumber(8, l1);\n            mv.visitInsn(RETURN);\n            Label l2 = new Label();\n            mv.visitLabel(l2);\n            mv.visitLocalVariable(\"args\", \"[Ljava/lang/String;\", null, l0, l2,\n                    0);\n            mv.visitMaxs(2, 1);\n            mv.visitEnd();\n        }\n        cw.visitEnd();\n\n        return cw.toByteArray();\n    }\n\n    public static class DynamicClassLoader extends ClassLoader {\n        public Class<?> define(String className, byte[] bytecode) {\n            return super.defineClass(className, bytecode, 0, bytecode.length);\n        }\n    };\n\n    public static void main(String[] args) throws Exception {\n        DynamicClassLoader loader = new DynamicClassLoader();\n        Class<?> helloWorldClass = loader.define(\"hello.HelloWorld\",\n                compile(\"Test\"));\n        Method method = helloWorldClass.getMethod(\"main\", String[].class);\n        method.invoke(null, (Object) new String[] {});\n    }\n}\n```\n\n```text\nClassReader cr = new ClassReader(new FileInputStream(\"HelloWorld.class\"));\ncr.accept(new ASMifierClassVisitor(new PrintWriter(System.out)), 0);\n```\n\n```text\npackage hello;\n\nimport org.objectweb.asm.ClassWriter;\nimport org.objectweb.asm.Label;\nimport org.objectweb.asm.MethodVisitor;\nimport org.objectweb.asm.Opcodes;\n\npublic class HelloWorldPlugin implements Opcodes {\n    public static interface Plugin {\n        void sayHello(String name);\n    }\n\n    public static byte[] compile() {\n\n        ClassWriter cw = new ClassWriter(0);\n        MethodVisitor mv;\n\n        cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, \"hello/MyClass\", null,\n                \"java/lang/Object\",\n                new String[] { \"hello/HelloWorldPlugin$Plugin\" });\n\n        cw.visitInnerClass(\"hello/HelloWorldPlugin$Plugin\",\n                \"hello/HelloWorldPlugin\", \"Plugin\", ACC_PUBLIC + ACC_STATIC\n                        + ACC_ABSTRACT + ACC_INTERFACE);\n\n        {\n            mv = cw.visitMethod(ACC_PUBLIC, \"<init>\", \"()V\", null, null);\n            mv.visitCode();\n            Label l0 = new Label();\n            mv.visitLabel(l0);\n            mv.visitLineNumber(5, l0);\n            mv.visitVarInsn(ALOAD, 0);\n            mv.visitMethodInsn(INVOKESPECIAL, \"java/lang/Object\", \"<init>\",\n                    \"()V\");\n            mv.visitInsn(RETURN);\n            Label l1 = new Label();\n            mv.visitLabel(l1);\n            mv.visitLocalVariable(\"this\", \"Lhello/MyClass;\", null, l0, l1, 0);\n            mv.visitMaxs(1, 1);\n            mv.visitEnd();\n        }\n        {\n            mv = cw.visitMethod(ACC_PUBLIC, \"sayHello\",\n                    \"(Ljava/lang/String;)V\", null, null);\n            mv.visitCode();\n            Label l0 = new Label();\n            mv.visitLabel(l0);\n            mv.visitLineNumber(9, l0);\n            mv.visitFieldInsn(GETSTATIC, \"java/lang/System\", \"out\",\n                    \"Ljava/io/PrintStream;\");\n            mv.visitTypeInsn(NEW, \"java/lang/StringBuilder\");\n            mv.visitInsn(DUP);\n            mv.visitLdcInsn(\"Hello, \");\n            mv.visitMethodInsn(INVOKESPECIAL, \"java/lang/StringBuilder\",\n                    \"<init>\", \"(Ljava/lang/String;)V\");\n            mv.visitVarInsn(ALOAD, 1);\n            mv.visitMethodInsn(INVOKEVIRTUAL, \"java/lang/StringBuilder\",\n                    \"append\", \"(Ljava/lang/String;)Ljava/lang/StringBuilder;\");\n            mv.visitMethodInsn(INVOKEVIRTUAL, \"java/lang/StringBuilder\",\n                    \"toString\", \"()Ljava/lang/String;\");\n            mv.visitMethodInsn(INVOKEVIRTUAL, \"java/io/PrintStream\", \"println\",\n                    \"(Ljava/lang/String;)V\");\n            Label l1 = new Label();\n            mv.visitLabel(l1);\n            mv.visitLineNumber(10, l1);\n            mv.visitInsn(RETURN);\n            Label l2 = new Label();\n            mv.visitLabel(l2);\n            mv.visitLocalVariable(\"this\", \"Lhello/MyClass;\", null, l0, l2, 0);\n            mv.visitLocalVariable(\"name\", \"Ljava/lang/String;\", null, l0, l2,\n                    1);\n            mv.visitMaxs(4, 2);\n            mv.visitEnd();\n        }\n        cw.visitEnd();\n\n        return cw.toByteArray();\n    }\n\n    public static class DynamicClassLoader extends ClassLoader {\n        public DynamicClassLoader(ClassLoader parent) {\n            super(parent);\n        }\n\n        public Class<?> define(String className, byte[] bytecode) {\n            return super.defineClass(className, bytecode, 0, bytecode.length);\n        }\n    };\n\n    public static void main(String[] args) throws Exception {\n        DynamicClassLoader loader = new DynamicClassLoader(Thread\n                .currentThread().getContextClassLoader());\n        Class<?> helloWorldClass = loader.define(\"hello.MyClass\", compile());\n        Plugin plugin = (Plugin) helloWorldClass.newInstance();\n        plugin.sayHello(\"Test\");\n    }\n}\n```\n\n```text\nClass\n```\n\n```text\ndefineClass\n```\n\n========================================\n\nComments:\n- Has the DSL been designed/specified already? Or are you developing the DSL alongside the parser and interpreter?\n- @Stobor: The language has been specified and the parser written.\n- Thanks for the links. Have you used any of these and which one would you recommend?\n- I have used Javassist and CGLIB, but for way simpler tasks than your needs. One thing to note is that BCEL uses CGLIB, so perhaps it has bigger capabilities.\n- Yes, I know. And you're welcome. But I won't take the credit for it, the credit goes to Andrei Loskutov, the original author.\n- Thanks for the link. It seems that with XText I will have to reimplement the frontend which I would like to avoid.\n- Yeah, I understand. It depends on your team size and company size. I think time by time it is necessary to completely give up something propietary to use something what others also use.","metadata":{"transformedAt":"2026-08-18T18:32:17.757Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":229,"estimatedTokens":2121}}94{"id":"stack-12600296","source":"stackoverflow","questionId":12600296,"title":"Dynamically compiled language vs statically compiled language","tags":["java","c","compiler-construction","compilation","programming-languages"],"text":"Title: Dynamically compiled language vs statically compiled language\nTags: java, c, compiler-construction, compilation, programming-languages\nSource: Stack Overflow\n\nQuestion:\nThe first line of this article by Brian Goetz made me to post this question in SO . Here 's the line again:\n\n Writing and interpreting performance benchmarks for dynamically\n compiled languages, such as Java, is far more difficult than for\n statically compiled languages like C or C++.\n\nI know the answer of statically typed vs dynamically typed language . But what is the difference between dynamically compiled language vs statically compiled language?\n\n========================================\n\nTop Answer:\nC and C++ source code are normally compiled to native machine code by a compiler.\n\nJava is compiled to bytecode by the Java compiler. When you run your Java program, a just-in-time (JIT) compiler might compile the Java bytecode to native machine code for the CPU that the program is running on.\n\nCompiling the program to native machine code when the programs runs is also called dynamic compilation.","metadata":{"transformedAt":"2026-08-18T18:32:17.757Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":272}}95{"id":"stack-2903113","source":"stackoverflow","questionId":2903113,"title":"What does a JIT compiler do?","tags":["compiler-construction","jit"],"text":"Title: What does a JIT compiler do?\nTags: compiler-construction, jit\nSource: Stack Overflow\n\nQuestion:\nI was just watching the Google IO videos and they talked about the JIT compiler that they included in the android. They showed a demo of performance improvements thanks to the JIT compiler. I wondered what does exactly a JIT compiler do and wanted to hear from different people.\n\nSo, what is the duty of a JIT compiler?\n\n========================================\n\nTop Answer:\nJIT is short for \"just in time\". A JIT compiler compiles code, which is often in an intermediate language like Java bytecode or Microsoft IL, into native, executable code, and it does this at the moment that the code is called. So until the code is called, it exists only in portable, non-machine specific bytecode or IL, and then when it is called, native code is generated (which is then re-used on subsequent calls).\n\n========================================\n\nComments:\n- possible duplicate of What does a just-in-time (JIT) compiler do?\n- @PauliL: if JIT transforms byte code to machine understandable format, so how is byte code platform independent?\n- @ShardaPrasadJaiswal: Bytecode is platform independent. JOT compiler compiles it into the native code of your computer when you run the program.\n- This question had a number of decent answers already; not sure why it needed a copy-and-paste of existing content.\n- @AndrewBarber I landed on this question and found no where clear mentioning that JIT is kind of alternative to interpreter. Only after doing some more research I got a clear insight of it.I only posted so that it could help others. Specially the IBM documentation is very helpful.\n- Two of the top three answers were quite clear about the comparison to interpreters.\n- @AniketThakur seconded.^","metadata":{"transformedAt":"2026-08-18T18:32:17.757Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":449}}96{"id":"stack-1967987","source":"stackoverflow","questionId":1967987,"title":"How to generate AST from Java source-code?","tags":["java","parsing","compiler-construction"],"text":"Title: How to generate AST from Java source-code?\nTags: java, parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nAs far as I know, the only way to parse Java source-code into an AST (Abstract Syntax Tree) is to use the Java Compiler Tree API: com.sun.source.tree\n\nI have two questions:\n\n- What JDKs support com.sun.source.tree?\n\n- Is there a portable replacement that works for all JDKs?\n\n========================================\n\nTop Answer:\nRegarding your second question, there are dozens of Java parsers available in addition to Sun's. Here is a small sample:\n\n- Eclipse's org.eclipse.jdt.core.dom package.\n\n- Spoon outputs a very nice annotated parse tree with type information and variable binding (and uses Eclipse's parser internally)\n\n- ANTLR is a parser-generator, but there are grammars for Java available\n\n- javaparser (which I have not used)\n\nMy best advice is to try each of them to see which works best for your needs.\n\n========================================\n\nCode:\n```text\norg.eclipse.jdt.internal.compiler.**\n```\n\n========================================\n\nComments:\n- If I'm not mistaken, Eclipse uses a different version of the Java model with their own parser, and there might be a way to reuse that for general parsing.\n- What do you mean by \"support\" in your first question? Are you asking which versions of Java from which vendors contain the com.sun.source.tree package? I would imagine only Sun's does. If you want to parse source code with another JDK (say, IBM's), then a standalone parser library is probably necessary.\n- @Brett, I know that com.sun.source.tree was only introduced in JDK6. I'm wondering if all non-Sun JDKs support this API.\n- com.sun is not portable. It may exist in other JDKs but do not count on it.\n- Redistributing tools.jar: good point! OpenJDK's classpath exception makes for a great license.\n- google-java-format uses `com.google.errorprone:javac-shaded` to get the AST. `javac-shaded` embeds OpenJDK parser in itself. Example can be found at `JavaInputAstVisitor.java` in google-java-format.\n- Entry point is here\n- What is the difference between Eclipse's jdt DOM and Spoon?\n- The actual AST classes are roughly analogous, but Spoon's parse tree includes semantic information like variable binding without requiring a massive IDE infrastructure to be running. One can parse and analyze Java files by simply adding one jar file to the classpath.\n- Here is last WebArchive for the link above. web.archive.org/web/20090801122725/http://www.ibm.com/&hellip;\n- Thanks for updating the link to the project (it has been moved a few months ago and in the meantime I became a contributor to this project :D)","metadata":{"transformedAt":"2026-08-18T18:32:17.757Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":49,"estimatedTokens":668}}97{"id":"stack-538056","source":"stackoverflow","questionId":538056,"title":"JIT compiler vs offline compilers","tags":["c#",".net","c++","compiler-construction","jit"],"text":"Title: JIT compiler vs offline compilers\nTags: c#, .net, c++, compiler-construction, jit\nSource: Stack Overflow\n\nQuestion:\nAre there scenarios where JIT compiler is faster than other compilers like C++?\n\nDo you think in the future JIT compiler will just see minor optimizations, features but a similar performance, or will there be breakthroughs that will make it infinitely superior to other compilers?\n\nIt looks like the multi core paradigm has some promise but it's not universal magic.\n\nAny insights?\n\n========================================\n\nTop Answer:\nYes, JIT compilers can produce faster Machine Code optimized for the current environment. But practically VM programs are slower than Native programs because JITing itself consumes time (more Optimization == more time), and for many methods JITing them may consume more time than executing them. And that's why GAC is introduced in .NET\n\nA side effect for JITing is large memory consumption. However that's not related to computation speed, it may slow down the whole program execution, because large memory consumption increases the probability that your code will be paged out to the secondary storage.\n\nExcuse me for my bad English.\n\n========================================\n\nCode:\n```text\n//code before\nif(errorCondition)\n{\n  //error handling\n}\n//code after\n```\n\n```text\n//code before\nBranch if not error to Code After\n//error handling\nCode After:\n//Code After\n```\n\n```text\ninterface IGenericAction { bool Act<T>(); }\n\nstruct Blah<T>\n{\n  public static void ActUpon(IGenericAction action)\n  {\n     if (action.Act<T>())\n       Blah<Blah<T>>.ActUpon(action);\n  }\n}\n```\n\n```text\nBlah<Int32>.ActUpon(act)\n```\n\n```text\nact.Act<Int32>()\n```\n\n```text\nBlah<Blah<Int32>>.ActUpon(act)\n```\n\n```text\nact.Act<Blah<Int32>>()\n```\n\n```text\nActUpon\n```\n\n```text\naction<Blah<...50 levels deep...>>.Act()\n```\n\n```text\nBlah<Blah<...50 levels deep...>>.ActUpon\n```\n\n```text\nBlah<Blah<...51 levels deep...>>.ActUpon\n```\n\n```text\nmemcpy()\n```\n\n========================================\n\nComments:\n- You only mention what a JIT compiler *can* do, not what it actually does. JIT'ing is generally constrained by having to be fast. It's ok for an offline compiler to take a long time to optimize the code, but a JIT compiler has to be done in a second or two, at most. So both have their advantages.\n- As I *clearly* indicated in the post, HotSpot actually does dynamic, profile-guided recompilation, and .NET actually does CPU-specific compilation. I doubt either affects startup speed one bit (think about it). Virtual call optimization? Look at V8. I agree both have advantages.\n- Not only can a JIT compiler optimize code based on how the runtime observes the code being used, it can also use more aggressive (\"dangerous\") optimizations because it can deoptimize the code by throwing away the compiled code when the optimization assumptions are no longer valid. Hotspot does this.\n- Not only can virtual method calls be replaced with static calls based on runtime characteristics, they can also be inlined based on the same runtime characteristics.\n- All of this can and is done by AOT compilers (e.g. tracking run-time behavior and switching to alternative code paths, using AOT when software is installed to optimize for a specific machine, basic \"hoisting\" of branches out of inner loops that compilers have been doing for half a century, etc).\n- I know that - \"offline compilers ... without inserting special code, ask the dev to run the program, put it through its paces, and recompile.\"\n- Nils, JIT compiler can profile live dataset on the fly whereas static is still limited to static dataset\n- I think you mean NGEN, not GAC.\n- GAC = GLobal Assembly Cache en.wikipedia.org/wiki/Global_Assembly_Cache ngen is the tool used to add an assembly to it\n- Often with JITers, instead of paging compiled code out, it will just drop the least recently used compiled code and recompile it later when needed.\n- I know what they are. The point is that it's NGEN which *actually* increases the speed via precompilation.\n- @Bahaa: no, gacutil is used to add assemblies to the GAC. NGEN is used to add assemblies to the native images cache.\n- +1 for hybrids. FWIW, NGEN mostly does what you're describing.\n- rebasing is not as slow as you make it out to be. Slower than loading at preferred address, absolutely. Slower than JIT, doubtful. And since JITted code is never shared, that's another case where AOT is a win, until you have a load address conflict.","metadata":{"transformedAt":"2026-08-18T18:32:17.757Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":12,"totalLines":108,"estimatedTokens":1122}}98{"id":"stack-8564025","source":"stackoverflow","questionId":8564025,"title":"OCaml internals: Exceptions","tags":["exception","compiler-construction","ocaml","internals","setjmp"],"text":"Title: OCaml internals: Exceptions\nTags: exception, compiler-construction, ocaml, internals, setjmp\nSource: Stack Overflow\n\nQuestion:\nI'm curious to know how exceptions are dealt with in OCaml runtime to make them so lightweight. Do they use setjmp/longjmp or do they return a special value in each function, and propagate it?\n\nIt seems to me that longjmp would put a little strain on the system, but only when an exception is raised, while checking for each function return value would need to check for every and each value after calling a function, which seems to me would put a lot of checks and jumps, and it seems it would perform worst.\n\nBy looking at how OCaml interfaces with C ( http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html#toc142 ), and looking at callback.h, it seems that an exception is tagged by using the memory alignment of objects ( #define Is_exception_result(v) (((v) & 3) == 2) ). This seems to indicate that its implementation doesn't use longjmp and checks each function result after each function call. Is that it? Or the C function already tries to catch any exception, and then converts it to this format?\n\nThank you!\n\n========================================\n\nCode:\n```text\nsetjmp/longjmp\n```\n\n```text\ntry <expr> with <handle>\n```\n\n```text\nKpushtrap/Kpoptrap\n```\n\n```text\ntry..with\n```\n\n```text\nLpushtrap/Lpoptrap\n```\n\n```text\nPUSHTRAP\n```\n\n```text\nPOPTRAP\n```\n\n```text\nRAISE\n```\n\n```text\nsetjmp\n```\n\n```text\nlongjmp/setjmp\n```\n\n```text\ncaml_callback\n```\n\n```text\ncaml_callback_exn\n```\n\n========================================\n\nComments:\n- Wow! Very nice! Thank you! Do you know why setjmp/longjmp doesn't use a similar approach? Is there any way to access those primitives from C other than **asm** calls? Thank you again!\n- @Waneck I suppose `setjmp&#47;longjmp` use a similar implementation on architectures that make this practical. But the callee-saved registers of the calling convention make those things more costly. I added a remark about them.","metadata":{"transformedAt":"2026-08-18T18:32:17.757Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":12,"totalLines":69,"estimatedTokens":499}}99{"id":"stack-8819846","source":"stackoverflow","questionId":8819846,"title":"How is Javascript translated to bytecode?","tags":["javascript","compiler-construction"],"text":"Title: How is Javascript translated to bytecode?\nTags: javascript, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI can't find information on the web how W3C languages compile to machine code. I know that the gap between the web and the processor must be somehow the browser, but how does it work and what are the steps till Javascript is executed in the processor?\n\nLinks to scientific documents would be also greatly appreciated.\n\n========================================\n\nTop Answer:\nThis may help : http://www.ecma-international.org/publications/standards/Ecma-262.htm\n\nThere is no spec for how to translate into bytecode (That is up to the browser developers) but there are specs about how the language should behave\n\n========================================\n\nComments:\n- code.google.com/p/v8 This is where google hosts their v8 javascript engine. Why do you want to know this btw?\n- thanks! I need to know because I want to compare mobile apps that run in a webview to the ones who run on the native operating system for my bachelor thesis. This may be helpful for performance comparison\n- JavaScript is not a W3C language. It (or rather ECMAScript) is an ECMA-International standard.\n- Every engine does it differently. Not all are part of browsers. There is no single specification saying how JavaScript should translate into bytecode, but each engine has its own way.\n- Not seeing the reason for the downvote.\n- These were all helpful, I aldready learned a lot from the answers and will dig into the links. Thanks @ all.\n- +1, one of the few questions here, an idiot like me knew how to answer (since I've been writing useless parsers and lexers all my life)\n- @T.J.Crowder it isnt really a question about programming, more about language.\n- @TJHeuvel: Wow, that's an incredibly fine distinction, I sure wouldn't down- or close-vote on that. For me, a question about programming languages is, ipso facto, a question about programming or plenty close enough. (In any case, that would be more a reason for a close vote as opposed to a downvote, but don't let's quibble. :-) )\n- I just want to add that I first asked the question here cstheory.stackexchange.com/questions, they sent me back here :)\n- BTW Javascript is a dialect of ECMAScript in case you didn't know\n- Actually, JavaScript is an implementation of the ECMAScript standard: JavaScript contains extensions which are not defined by the standard.\n- While those are the *conceptual* steps, in a modern engine it's more complicated than that. :-)\n- @T.J.Crowder I agree. But I thought, I'd give the OP a general idea and concept behind it first.\n- Neither of those links to the Mozilla documentation work any longer.","metadata":{"transformedAt":"2026-08-18T18:32:17.757Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":34,"estimatedTokens":673}}100{"id":"stack-494389","source":"stackoverflow","questionId":494389,"title":"Why doesn't Sun do a C# to Java byte code compiler?","tags":["c#","java",".net","compiler-construction","jvm"],"text":"Title: Why doesn't Sun do a C# to Java byte code compiler?\nTags: c#, java, .net, compiler-construction, jvm\nSource: Stack Overflow\n\nQuestion:\n### We Want to Run Our C# Code on the JVM\n\nMy company has a large C# code base. Well over half of this code is our core engine for creating, reading, modifying, calculating and writing Excel workbooks. We frequently get questions from customers and potential customers asking whether we are going to build a Java version of our engine - many of them are not at all interested in the UI. We even have a few customers who have taken the trouble to use our .NET library from their Java applications. \n\nSo, we would like to build a Java version of our core engine, ideally without maintaining a separate Java source code base.\n\nEric Sink described this problem very well. I am in a similar position except for the fact that our software license includes royalty free deployment, making Eric's choice of Mainsoft a nonstarter for us.\n\nI have been Googling the likes of \"c# to jvm\" every few months for several years now with no joy. Having spent ~7 years developing similar software for Java, I am confident that the .NET APIs we use in our core engine could easily be encapsulated and we could accomplish everything we need using the Java libraries. So, if we only had a C# -> JVM compiler we could build our core engine for Java and we would no longer have to turn away Java developers who would like to use it.\n\nI am not asking for the technical reasons why Sun does not do a C# compiler. I recognize that Java has no properties or an unsigned 64 bit long, etc... For the sake of argument, just assume that all of these technical issues could be dealt with by extending the JVM and / or other means.\n\nAnd I am not asking for yet another debate on why one language / stack might be better than the other. The reality in our business is that there are plenty of potential customers using each.\n\n### Why Should Sun do a C# Compiler? (IMO of course)\n\nMaking it easier to run C# code on the Java platform means more developers and more software for the platform. Is there anything more important to the success of a platform? Jonathan Schwartz is a software guy. I will leave it to others smarter than me to decide whether or not he took on an impossible job as President and CEO of Sun, but having met with Jonathan shortly after he joined Sun my impression is that he understands software and the need for a large base of developers.\n\n### So Why Doesn't Sun do a C# Compiler?\n\n- NIH syndrome?\n\n- The ghost of Scott McNealy?\n\n- Too many Java developers dislike or distrust anything related to Microsoft?\n\n- They agreed not to as part of taking the big bucks?\n\n- ???\n\nThere must be a good reason. I just cannot for the life of me figure out what it is...\n\n========================================\n\nTop Answer:\nJoe Erickson wrote:\n\n \n Making it easier to run C# code on the\n Java platform means more developers\n and more software for the platform.\n\nThis is an untrue statement. Running C# code on the JVM does not create Java programmers, it creates C# programmers who can execute on a JVM. It only expands the reach of C#, assuming the JVM also translates any microsoft specific calls (i.e. win32) into something that is platform neutral. So if Sun translates IL to Java Bytecode, the only group it helps is: Microsoft. And, given Sun's history with Microsoft during the original C#-Java schism/Visual J++ lawsuits...\n\nPlus, you have to face the technical infeasibility, whether you want to or not. There are fundamental differences in the way the bytecodes are executed that are far more important issues than whether or not there's an unsigned long datatype. \n\nIf you must have C# on a non-Microsoft platform, use Mono\n\n========================================\n\nComments:\n- this is not a technical question, it's asking us to read the minds of Sun executives - my Kreskin program says they don't like C#, they like Java, and want the two to remain distinct, otherwise they cannot justify their existence\n- My #1 and #2 suggestions above go along with your Amazing insight. With the current Pres of Sun being a software guy, who knows, maybe we'll get an Amazing answer from the top?\n- Should Sun \"embrace and extend\" C# instead of pretending that it doesn't exist. I've been in the biz of developing software for other developers since before Gosling noticed his Oak tree and it seems to me that embracing C# would be a good thing - but maybe I drink too much of the Microsoft Koolaid?\n- There was a project a while ago in Sun to run VB code on the JVM. They lost interest though - probably as a result of lack of developer interest. Sun have enough on their plate with all the Dynamic languages they are supporting: JRuby, JPython etc\n- You could just port it to Java and drop the minority platform.\n- Tom - I did the Java thing from 1996 until 2002. My official reason for leaving Actuate? Microsoft understood the need for basic things like enumerated types (since added to Java but it took way too long) and excellent Windows UI support and Actuate had no interest in building .NET products.\n- Fortyrunner - No doubt that Ruby, Python and such are hot these days. I've been too busy starting a company to play with a new language for a while so I haven't had time to play with the new Dynamic languages...maybe it takes an obsessed advocate inside of Sun for any of these to succeed.\n- It may be a long shot, but have you looked into leveraging the Stab programming language? It basically IS C# for the JVM. Seems to me you could just about write some Java class wrappers, try to compile your code as Stab and *almost* get away with it? Of course I've never tried such a thing so I could be way off.\n- IMO Sun does not want to create Java programmers. They want developers writing software for their platform whether it's C or C++ or Java or Ruby or Python - but maybe not C#?\n- You and I disagree on technical feasibility. Mainsoft is a small ISV who manages to convert .NET assemblies to Java jar files. If a small ISV can do it - with no ability to change the JVM - certainly Sun can do it if they choose to.\n- I've only just realized that my last two paragraphs were probably off-topic. The original poster wants a compiler of C# to JVM bytecode, not an IL bytecode translator. I'll leave the post as is, but consider that part retracted. :)\n- And then I see replies. Heh. I see what you mean, but I don't think Sun has any real incentive, especially if an ISV has already done it for them.\n- I will look into mainsoft, however.\n- The problem with Mainsoft, as Eric Sink pointed out in the article referred to, is that they have \"Enterprise Pricing\" which means that a five man ISV such as my company is not the kind of customer they are looking for. Their product is very expensive.\n- It also looks like it's not really automatic; my first guess is that they do it manually. I'm listening to their integration webcast now.\n- Unfortunately, the people who ask us for a Java version of our engine are not interested in a Mono solution - if they were we would not be thinking of going to all the trouble of doing a Java version.\n- Yes, Java is similar, but we would like to make our core engine available and we don't want to maintain C# and Java code bases. The reality is that we, and many other companies, have C# code bases today that would arguably be more likely to run on Java someday if there were a C# compiler.\n- In my post I pointed out that we want to port our core engine and we could easily encapsulate the .NET APIs we use and use Java APIs on the Java platform. Having said that, we might be the exception and the lack of .NET libraries might make C# a dead end language just as you point out J# is.\n- I think if you want a Java version you are going to have to bite the bullet and port to Java. I doubt there will ever be a high quality C# compiler for the JVM. Without a good compiler there is not point even thinking about it. Sorry.\n- Wait... C# is a dead-end language? Now *that's* a riot!\n- If we had to start today we'd definitely do it in Java. I am afraid your are correct that we are unlikely to ever get a high quality C# compiler for the JVM.\n- Randolpho - oops, guess I wasn't too clear - how about \"the lack of .NET libraries for the JVM might make a C# to JVM compiler a dead end project.\"\n- Ok, that I agree with. The APIs are very incompatible.\n- Actually Sun DOES have incentive to make C# run on Java. Sun makes their money off HARDWARE, not software. If C# stuff ran on the JVM, possibly they'd sell more Sun SPARCstations to run that C# on JVM code.\n- They might as well offically support mono on solaris than port C# and the .NET BCL to the JVM. If Sun were to implement the ECMA 334/335 standards your apps sill would not run as they use parts which are not part of that standard. Those other parts require MS licensing, I doubt that would happen.\n- @Robert: I agree with Tim; Mono on Solaris is the best way to allow C# on a Sun platform.\n- I wish Microsoft would do it! I think we all have a sense for the reasons why they won't. I would love to do it, but I've got about 100 years worth of ideas which relate to problems I'm already obsessed with...so that's just not going to happen.\n- Now, MICROSOFT has no incentive to do this as they DO make money off software (quite a bit, too). Hardware companies try to commoditize the software, software companies try to commoditize the hardware.\n- Their incentive would be to try and kill Java. Offering backwards compatibility with the competition was how they killed Netwear, remember.\n- Since C# code must assume that any method can throw any exception, the compiler could simply emit byte code which indicates that function F() throws any checked exceptions thrown by methods called by F() (hmmm...I remember doing a lot of essentially this same thing by hand back in the day).\n- As for delegates, the proper way to do it would be to extend the JVM. It could be done without extensions but it (emitted byte code) probably wouldn't be pretty or efficient.\n- That is no problem. Checked exceptions are a property of the Java language, not of the JVM. Other JVM languages havn't checked exceptions.\n- Unfortunately, our potential customers are Java developers who want something which works in their current Java environment - whether it's a web server, application server, Applet, Java GUI app or whatever.\n- I've seen it - but it's not what we need. Unfortunately, our potential customers are Java developers who want something which works in their current Java environment - whether it's a web server, application server, Applet, Java GUI app or whatever\n- IKVM has a runtime dependency on CLI, which Joe already said he doesn't want. See my post for the gory details.\n- Well, unfortunately there never is a free lunch. There's nothing in it for Microsoft to build a compiler to Java and Sun for sure won't do it. A compromise must be made somewhere to solve this particular problem.\n- This is a good suggestion which we will certainly do eventually (we've already been playing with Windows Azure with this in mind). However, this is not a solution for many of our potential customers for performance (we have customers with 500MB workbooks) and / or security reasons.\n- I guess I would need to know more about your application requirements. It sounds like you might want to look into using Windows Communication Foundation (WCF). This is MS's new web services platform, which has built in support for security, MTOM streaming, TCP/IP binding, etc.\n- note that WCF doesn't do 'interoperable' tcpip: it only supports \"optimised\" connections to other WCF services, so it'll be just as bad for performance as a webservice. Maybe they'll change this in the future, but until then roll your own. (or use gsoap and a c++/cli wrapper)\n- Check out whether you can MTOM from Java to WCF? If it possible I would build a WCF service. I would never build an asmx web service, asmx is obsolete.\n- This is how i would do it, I would use WCF (BasicHttp Binding) over port 80 (WebService). I have made a call from just the Http get from this.\n- Agreed - as it stands today a port and ongoing maintance of two code bases would seem to be our best option. We have plenty of opportunities on the .NET side which allow us to leverage our C# engine, so this where we will invest our time.\n- IIRC the JVM is in effect a CPU so what would be needed would be not a JVM based CLI VM but a CLI JIT that targets the JVM (more or less)\n- BTW IKVM no longer uses GNU Classpath. It switched to Open JDK some time ago.\n- If you read the original post, you will see that I refer to Mainsoft and the reason why it's a nonstarter for us.\n- so you do... sorry. Anyway it would be of interest to others.\n- It costs $5,000 per CPU on Tomcat and $15,000 per CPU on WebSphere Application Server, plus $6,000 per developer seat.... $11000 ^#$(@^$@&$$@%# NO THANK YOU.\n- I run my own for profit company so I do understand the need to make a profit. It seems to me that Sun is (was if the Oracle deal closes) interested in having as much software as possible run on their JVM and therefore on their hardware. Making it easier to get C# code running on the JVM would arguably make more software available.\n- +1 Interesting! Have you used this for transformation of non-trivial/production code?\n- We will not do a C# to Java byte code compiler because we have many years worth of work to do while staying focused on what we do best - which is spreadsheet technology.\n- That's a great question. Of course, it's too late now. I wonder if they considered it. I'm guessing that performance would be a big issue - no value types for example.\n- It is just not technically possible at this point to implement the C# spec on the JVM. C# supports pointers, unsigned types, user defined value types, true generics, passing by reference, and loads of other things. For a very C#-like JVM language, check out Stab: code.google.com/p/stab-language","metadata":{"transformedAt":"2026-08-18T18:32:17.758Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":108,"estimatedTokens":3506}}101{"id":"stack-2998768","source":"stackoverflow","questionId":2998768,"title":"How can a language's compiler be written in that language?","tags":["compiler-construction","recursion","bootstrapping"],"text":"Title: How can a language's compiler be written in that language?\nTags: compiler-construction, recursion, bootstrapping\nSource: Stack Overflow\n\nQuestion:\n**Possible Duplicates:**\n\n implementing a compiler in &ldquo;itself&rdquo;\n\n Bootstrapping a language \n\nHow can you write a compiler in the same language as the language you're writing that compiler for? Isn't that sort of recursive?\n\n**Edit**:\nThis may be deleted, but otherwise... :\n\n**How** to bootstrap:\n\n- Writing a compiler in its own language\n\n- Bootstrapping a language\n\n**Why** to bootstrap:\n\n- Bootstrapping a compiler: why?\n\n========================================\n\nTop Answer:\nIt is. You usually need a bootstrap version of the language either compiled or interpreted from another language.\n\nAnd to bend your mind a little more, years ago I read the history of a Pascal compiler written as a grad student project. It written in Pascal and compiled with the system's built-in Pascal compiler. Eventually, it was good enough to replace the system's built-in Pascal compiler. Unfortunately, they found a bug in code generation, but the fix for the code generator triggered the bug in the compiler, generating a bad compiler. To fix it required hand-patching the binaries from the installed compiler to then apply the patch to the source to replace itself.\n\n========================================\n\nCode:\n```text\nx\n```\n\n```text\nx-1\n```\n\n```text\nx\n```\n\n========================================\n\nComments:\n- Various earlier versions and related links: stackoverflow.com/questions/13537/bootstrapping-a-language stackoverflow.com/questions/1493747/&hellip; stackoverflow.com/questions/193560/&hellip; stackoverflow.com/questions/1173780/&hellip; stackoverflow.com/questions/2035838/&hellip; stackoverflow.com/questions/2740994/&hellip; The word you wanted was \"bootstrapping\".\n- I wonder how often people implement compilers for mainstream languages using esoteric ones.\n- @Phil Ross - wow, thanks, how'd you find that? wasn't sure how to search :)\n- I searched for compiler bootstrapping using the search box in the top right.\n- +1 I've built a simple Lisp interpreter in JAVA.\n- so... why can't you just use the first version of the compiler? Why would you want a double-level compiler?\n- @froadie You mean why not compile every version with version 1? Usually new versions of a compiler generate better machine code than the previous versions, so building with the newest version will make the compiler itself as fast/efficient as possible. Also if the language itself is changing and you want to use those newer features in the compiler's source code, you'll need to build with a more recent version\n- @MichaelMrozek that's pretty cool, are you saying that all languages derive from assembly?\n- @froadie see this q bootstrapping-a-compiler-why?\n- @nawfal - I added a link to that question in my question itself shortly after asking the question several years ago\n- @froadie oh yes, I missed it...\n- I found this question wondering exactly what one would do in this kind of bug situation. Hand-patching had not occurred to me. Funny story. haha\n- Not necessarily. The very first \"compiler\" can also be a human, in which case you do not need a bootstrap compiler in a different language *at all*. This is how the first compilers for most of Niklaus Wirth's languages were written: he basically assigned them to his students :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.758Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":64,"estimatedTokens":848}}102{"id":"stack-10343155","source":"stackoverflow","questionId":10343155,"title":"X86 assembly - Handling the IDIV instruction","tags":["c","assembly","compiler-construction","x86"],"text":"Title: X86 assembly - Handling the IDIV instruction\nTags: c, assembly, compiler-construction, x86\nSource: Stack Overflow\n\nQuestion:\nI am currently writing a simple C compiler, that takes a .c file as input and generates assembly code (X86, AT&T syntax).\nEveryting is good, but when I try to execute a IDIVQ instruction, I get a floating-point exception. Here's my input:\n\n```\nint mymain(int x){\n int d;\n int e;\n d = 3;\n e = 6 / d;\n return e;\n}\n```\n\nAnd here is my generated code:\n\n```\nmymain:\n.LFB1:\n .cfi_startproc\n pushq %rbp\n .cfi_def_cfa_offset 16\n movq %rsp, %rbp\n .cfi_offset 6, -16\n .cfi_def_cfa_register 6\n movq %rdi, -40(%rbp)\n movq $3, -8(%rbp)\n movq $6, %rax\n movq -8(%rbp), %rdx\n movq %rdx, %rbx\n idivq %rbx\n movq %rax, -16(%rbp)\n movq -16(%rbp), %rax\n leave\n .cfi_def_cfa 7, 8\n ret\n .cfi_endproc\n.LFE1:\n .size mymain, .-mymain\n```\n\nAccording to http://www.cs.virginia.edu/~evans/cs216/guides/x86.html, **idivq %rbx** should produce 6/d (the quotient) in **%rax**. But I'm getting a floating-point exception, and I can't seem to find the problem.\n\nAny help will be much appreciated!\n\n========================================\n\nTop Answer:\nThe `idivq` instruction divides a 128-bit integer (`rdx:rax`) by the given source operand.\n\n- `rax` holds the lower 64-bits of the dividend.\n\n- `rdx` holds the upper 64-bits of the dividend.\n\nWhen the quotient doesn't fit into 64-bits, `idiv` will fault (#DE exception, which the OS handles by delivering a SIGFPE signal as required by POSIX for arithmetic exceptions).\n\nSince you're compiling code that uses signed `int`, you also need to sign extend `rax` to `rdx:rax`, that means copying the `rax` sign bit to every bit of `rdx` and is accomplished with cqo alias cqto:\n\n```\nmovq %rdx, %rbx # or load into RBX or RCX in the first place\ncqo\nidivq %rbx # signed division of RDX:RAX / RBX\n```\n\nIf you'd been doing `unsigned` division, you'd zero RDX to zero-extend RAX into RDX:RAX:\n\n```\nmovq %rdx, %rbx\nxor %edx, %edx # zero \"rdx\"\ndivq %rbx # unsigned division of RDX:RAX / RBX\n```\n\nAlso note that in the x86-64 System V ABI, `int` is a 32-bit signed type, not 64-bit. Widening it to 64-bit is legal in this case (because the result is the same) but makes your code slower, especially for division.\n\n========================================\n\nCode:\n```text\nint mymain(int x){\n  int d;\n  int e;\n  d = 3;\n  e = 6 / d;\n  return e;\n}\n```\n\n```text\nmymain:\n.LFB1:\n    .cfi_startproc\n    pushq   %rbp\n    .cfi_def_cfa_offset 16\n    movq    %rsp, %rbp\n    .cfi_offset 6, -16\n    .cfi_def_cfa_register 6\n    movq    %rdi, -40(%rbp)\n    movq    $3, -8(%rbp)\n    movq    $6, %rax\n    movq    -8(%rbp), %rdx\n    movq    %rdx, %rbx\n    idivq   %rbx\n    movq    %rax, -16(%rbp)\n    movq    -16(%rbp), %rax\n    leave\n    .cfi_def_cfa 7, 8\n    ret\n    .cfi_endproc\n.LFE1:\n    .size mymain, .-mymain\n```\n\n```text\nmovq    %rdx, %rbx\ncqto                  # sign extend rax to rdx:rax\nidivq   %rbx\n```\n\n```text\nidiv\n```\n\n```text\nrdx\n```\n\n```text\nrax\n```\n\n```text\nrdx:rax\n```\n\n```text\ncqto\n```\n\n```text\ncqo\n```\n\n```text\nmovq    %rdx, %rbx        # or load into RBX or RCX in the first place\ncqo\nidivq   %rbx              # signed division of RDX:RAX / RBX\n```\n\n```text\nmovq    %rdx, %rbx\nxor     %edx, %edx      # zero \"rdx\"\ndivq    %rbx            # unsigned division of RDX:RAX / RBX\n```\n\n```text\nidivq\n```\n\n```text\nrdx:rax\n```\n\n```text\nrax\n```\n\n```text\nrdx\n```\n\n```text\nidiv\n```\n\n```text\nint\n```\n\n```text\nrax\n```\n\n```text\nrdx:rax\n```\n\n```text\nrax\n```\n\n```text\nrdx\n```\n\n```text\nunsigned\n```\n\n```text\nint\n```\n\n========================================\n\nComments:\n- Unrelated to this question, but should you be doing `movq %rdi, -40(%rbp)` without having adjusted the `esp` register? Or is that OK because of the x64 'red zone'?\n- @MichaelBurr: That's ok because the x86-64 System V guarantees a red-zone of 128 bytes below RSP; this is what mainstream compilers like GCC do: Why is there no \"sub rsp\" instruction in this function prologue and why are function parameters stored at negative rbp offsets?. And of course you shouldn't adjust *ESP*, that would truncate RSP to 32 bits and make later stack operations fault, if you did any before `leave`.\n- Zeroing rdx will work with positive numbers, but in case of negative rax probably rdx = -1 is needed... Isn't it?\n- I think marekb is right - shouldn't the `xorq` instrcution be a `cqo` instruction to sign extend `rax` into `rdx:rax`?\n- In this case, with signed typing: I think so yes.\n- Unsigned: `xor %rdx,%rdx` / `div`. Signed: `cqo` / `idiv`. Using idiv after zeroing rdx is only appropriate if your divisor is signed but your dividend is unsigned. (And you want a signed result). A C compiler should only generate that as an optimization after casting a `uint64_t` to an `__int128_t`, when it can prove there won't be a divide exception. Also note that 64bit `div` is somewhat faster than 64bit `idiv` on recent Intel CPUs.\n- Indeed, I was running my tests and encountered an error when handling signed values. I haven't seen this instruction before, but it seems to solve the issue now. Thank you!\n- NASM version of the same question&answer: nasm idiv a negative value","metadata":{"transformedAt":"2026-08-18T18:32:17.758Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":23,"totalLines":217,"estimatedTokens":1290}}103{"id":"stack-18446171","source":"stackoverflow","questionId":18446171,"title":"how do compilers assign memory addresses to variables?","tags":["c","compiler-construction","linker","operating-system"],"text":"Title: how do compilers assign memory addresses to variables?\nTags: c, compiler-construction, linker, operating-system\nSource: Stack Overflow\n\nQuestion:\nI teach a course where students get to ask questions about programming (!): I got this question:\n\n Why does the machine choose were variables go in memory? Can we tell\n it where to store a variable?\n\nI don't really know what to say. Here's my first attempt:\n\n The compiler (not the machine) chooses where to store the variables in the process address \n space automatically. Using C, we cannot tell the machine where to store variables.\n\nBut that \"automatically\" is somewhat anticlimactic and begs the question... and I've realized I don't even know if it's the compiler or the runtime or the OS or who does the assignment. Maybe someone can answer the student's question better than me.\n\n========================================\n\nTop Answer:\nI think the answer to this question starts with an understanding of the layout of a program in memory. Underneath the operating system, a computer's main memory is just a giant array. When you run a program, the operating system will take a chunk of this memory and break it up into logical sections for the following purposes:\n\nstack: this area of memory stores information about all functions currently in scope, including the currently running function and all of its ancestors. Information stored includes local variables and the address to return to when the function is done.\n\nheap: this area of memory is used when you want to dynamically allocate some storage. Generally your local variable would then contain an address (ie, it would be a pointer) in the heap where your data is stored, and you could publish this address to other parts of your program without worrying that your data will be overwritten when the current function goes out of scope.\n\ndata, bss, text segments: these are more or less outside the scope of this particular question, but they store things such as global data and the program itself.\n\nHope that helps. There are lots of good resources online as well. I just googled \"layout of a program in memory\" and found this one: http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory\n\n========================================\n\nCode:\n```text\nmov eax, DWORD PTR SS:[esp]\nmov eax, DWORD PTR SS:[esp + 4]\nmov eax, DWORD PTR SS:[esp + 8]\n```\n\n```text\nlocal variables\n```\n\n```text\nreturn\n```\n\n```text\nesp\n```\n\n```text\nesp + N\n```\n\n```text\nalloc\n```\n\n```text\nnew\n```\n\n```text\nalloc\n```\n\n```text\nnew\n```\n\n```text\nfree\n```\n\n```text\ndelete\n```\n\n```text\nint *a = 0x123\n```\n\n```text\nint foo(int a, int b) { return a+b;}\n```\n\n```text\nint bar(int *a, int *b) { return (*a) + (*b);}\n```\n\n========================================\n\nComments:\n- Technically it's the linker that does this.\n- `Using C, we cannot tell the machine where to store variables` maybe not standard C, but any dialect used for a DSP/microcontroller will use some extension to allow you to put the variable in SDRAM/IRAM/EEPROM/...\n- Consider reading Linkers and Loaders by John R. Levine.\n- @haccks only partly, as Michael says the linker does the core job. This can be seen easily by letting the linker generate a map file which basically shows you 'this variable goes there, the stack goes there, this other section goes there' etc\n- Isn't it the Operating System that decides where the variables are stored? When a program is started, I think, the OS allocates a Virtual Space in memory for it to use. Both the stack and heap are located inside this Space. Global Variables and Local Variables (automatic) are put in the stack; the former when the program starts and deleted when it terminates, the latter when they are declared and deleted when out of scope.\n- @angel yes that's true, but the OS just says 'here's a piece of memory starting at address x'. Where exactly in that piece the stack/heap/... are placed is decided by the linker.\n- So, it seems to be both the OS and the Linker that decide...\n- \"Why does the machine choose\" rephrased as \"How does the machine choose\" - really refocuses on the gist of the question, and which software does what in this regard (OS, compiler/assembler etc.) - a pointer in C with a poorly specified address can really mess things up for instance.\n- Local variables are typically grouped by alignment requirements. This reduces the space required by allowing several `char` objects to be stored adjacent to each other in one word, et cetera. If you laid them out in sequence (e.g., by appearance of declaration), you would have to include more padding bytes.\n- It would be nice to also discuss static data (usually called \"data/rodata/bss\" sections), and maybe also static code (\"text\"). These are sections of code which are laid out by the linker and whose addresses are largely predictable.","metadata":{"transformedAt":"2026-08-18T18:32:17.758Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":14,"totalLines":104,"estimatedTokens":1207}}104{"id":"stack-19663564","source":"stackoverflow","questionId":19663564,"title":"What is the difference between LALR and LR parsing?","tags":["parsing","compiler-construction","context-free-grammar","lalr","lr-grammar"],"text":"Title: What is the difference between LALR and LR parsing?\nTags: parsing, compiler-construction, context-free-grammar, lalr, lr-grammar\nSource: Stack Overflow\n\nQuestion:\nI understand both LR and LALR are bottom-up parsing algorithms, but what's the difference between the two?\n\nWhat's the difference between LR(0), LALR(1), and LR(1) parsing? How can I tell if a grammar is LR(0), LALR(1), or LR(1)?\n\n========================================\n\nTop Answer:\nLR(0), SLR(1), LALR(1) parsers all have the same number of states. Minimal LR(1) parsers will have a few more states if the grammar requires it, to avoid reduce-reduce conflicts. \n\nCanonical LR(1) parsers will have many more states, too many for medium or large computer languages. \n\nSLR(1) parser generators build an LR(0) state machine and determine the k=1 lookaheads by examining the grammar (which may report erroneous conflicts). \n\nLALR(1) parser generators build an LR(0) state machine and determine the k=1 lookaheads by examining the LR(0) state machine (which is very complicated). \n\nCanonical LR(1) parser generators build an LR(1) state machine. \n\nMinimal LR(1) parser generators build an LR(1) state machine and merge compatible states during the build process.\n\n========================================\n\nComments:\n- Thank you Sir.it has helped a lot am still not clear on a few things so i will read up on those and get back with my doubts I know the choice of book varies from person to person but any book that you would suggest for sir\n- For parsing, the best book I know of is \"Parsing Techniques: A Practical Guide\" by Grune and Jacobs, which has a fantastic treatment of all sorts of parsing algorithms.\n- could u just tell me wat is the intial sate for this production\n- @1994- The start state corresponds to the closure of S' -> .S[$'], where S' is the new start symbol, S is the original start symbol, and $ is the end-of-input marker.\n- if a grammar is SLR then will the LR(1) and LALR(1) have the same number of states as the SLR?\n- @1994- SLR and LALR parsers always have the same number of states as one another, but they usually have significantly rewer states than a corresponding LR(1) parser.\n- Thanks for making it clear. :) I have a small query. Can you please clear that? Is LR(1) is other name for CLR(1)? Or CLR(1) and LR(1) are different?\n- @templatetypedef in the lecture note web.stanford.edu/class/archive/cs/cs143/cs143.1128/lectures/&zwnj;&#8203;05/&hellip; there is an error on page 321, the epsilon edge goes to [E )] not to the pair [T )]\n- @alinsoar Great catch! I’ll keep that in mind for the next time I teach that class.","metadata":{"transformedAt":"2026-08-18T18:32:17.758Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":36,"estimatedTokens":656}}105{"id":"stack-7591169","source":"stackoverflow","questionId":7591169,"title":"Why is it hard to beat AOT compiler with a JIT compiler (in terms of app. performance)?","tags":["performance","compiler-construction","jit","vm-implementation"],"text":"Title: Why is it hard to beat AOT compiler with a JIT compiler (in terms of app. performance)?\nTags: performance, compiler-construction, jit, vm-implementation\nSource: Stack Overflow\n\nQuestion:\nI was thinking that JIT compilers will eventually beat AOT compilers in terms of the performance of the compiled code, due to the inherent advantage of JIT (can use information available only at runtime). One argument is that AOT compilers can spend more time compiling code, but a server VM could spend a lot of time, too. \n\nI do understand that JIT does seem to beat AOT compilers in some cases, but they still seem to lag behind in most cases. \n\nSo my question is, what are the specific, tough problems that is preventing JIT compilers to beat AOT compilers? \n\n**EDIT:**\n\nSome common arguments: \n\n- AOT compilers can spend more time doing advanced optimizations **->** If you are running a server VM for days, you can spend the same amount of time, if not longer.\n\n- Byte code interpretation has cost **->** Most JIT compilers cache native machine instructions anyways these days.\n\n**Yet another edit:**\n\nFor a specific example, see this article: Improving Swing Performance: JIT vs AOT Compilation. From what I can gather from this article, the authors are basically saying that when there are no hotspots, the advantage of having runtime information decreases and thus AOT without the overhead of JIT, wins. But by 40%?? That doesn't seem to make a lot of sense. Is it simply that the JIT compiler that was compared wasn't tuned for this situation? Or is it something more fundamental?\n\n========================================\n\nTop Answer:\nCompilation and optimization are a function of code, context, and time.\n\n### Original Tradeoff\n\n**AOT** can use as much time as needed, with deep code analysis, knowledge of execution platform, and additional hints for optimization. The disadvantages are slower builds, less portable code, bad optimizations based on wrong assumptions, and lacking support for advanced features like reflection during runtime.\n\n**JIT** is much more portable and can support advanced code changes at runtime, but takes longer to start and has limited time and resources which means limited optimizations.\n\n### Modern Developments\n\nThe latest JIT compilers are much faster and start much quicker while still producing good code, closing most of the gap with AOT for startup latency.\n\nMultiple compilation passes allow JIT to continually optimize the program as it runs, leading to similar performance as AOT after the warm-up period. The JIT can look at what code is actually used, how often it's used, and other factors like the running hardware that aren't available during AOT compilation.\n\n**The most advanced JIT compilers can meet or exceed AOT performance and maintain it under more scenarios.** AOT is still the best option for fast startup and consistent/predictable operation, but no longer the default option for best overall performance.\n\n========================================\n\nComments:\n- You seem to completely miss the idea of \"just\". Compiler perf is measured by humans, not machines. Nobody puts up with a 10 second wait.\n- @Hans Passant: In a client VM, I see the point. But in a server VM? The JVM for example has that distinction.\n- I guess this just boils down to \"Why is it hard to write an compiler that emits well-optimized machine code?\". Also, comparing Java/JIT to C++/AOT is comparing Apples to Oranges. (Unless there is a *specific* Java AOT/JIT being talked about?) While it is true that an AOT \"has more time\" to spend on the entire process, don't forget javac has already done good chunks of compiling -- lexing, parsing, some optimizing, albeit to a \"virtual machine\" -- itself.\n- @Enno Shioji: From the article you linked: *\"Where does the 40% performance win come from? We found the answer unexpectedly when we aimed at further improving Swing performance: the absence of hot methods.\"*\n- @Rob: Yeah, but does it make sense that that will result in 40% performance difference? At minimum, the JIT could start behaving like AOT once it detects that hotspot optimization is not worth it. That seems like a reasonably easy thing to fix.\n- @EnnoShioji: Just saw you accepted; I had forgotten about this question. A few things; a JIT compiler can't just \"switch\" to AOT at runtime, because AOT by definition occurs before execution time. Unfortunately, a large number of implementation details ultimately go into making up that 40% difference, so it's difficult to pinpoint.\n- See the second two paragraphs on this page: publib.boulder.ibm.com/infocenter/javasdk/v5r0/&hellip; -- it seems that in the JVM, 'hot' methods are compiled to native instructions when an internal call counter is exceeded. Otherwise byte-code interpretation is performed as normal. (My point is that AOT compilation is not relevant at runtime.)\n- The more time one is willing to spend building a piece of code, the faster it can run. If users of AOT compilers are willing to give the build process more time, it will be able to produce more efficient code. An advantage of JIT compilation, however, is that is *infinitely* faster than AOT compilation for code which is never run. In some cases involving .NET generics, a finite (small even) amount of source code can specify an unbounded family of methods which would each require separate machine code; if only a dozen such methods are run, only those twelve will be compiled to machine code.\n- What about profile-guided AOT compilation?\n- AOT can be just as portable as JIT (and in some cases the exact same byte-code is used for both AOT and JIT - e.g. Microsoft's CIL); and JIT can be \"not portable at all\" (e.g. no JIT compiler for the system you want to use). JIT can also have missing or incorrect optimizations (and almost always has deliberately missed optimizations because they were too expensive to do at run-time).\n- For performance, AOT is typically slower that it could/should be (e.g. optimized for a generic target by developer & not optimized for user's specific machine) but that's a weakness in current tools (not a weakness of AOT itself); and modern JIT still fails to be as good as \"not great AOT\". To make up for how awful \"advanced JIT\" performance is they also (almost always) rely on large libraries full of \"AOT compiled\" native code to hide the performance disaster. There has never been any \"advanced JIT\" that is able to beat AOT in a fair comparison (and never will be).\n- @Brendan Those are pedantic what-if's, not an argument about the concepts. CIL is irrelevant. JIT is more portable because it can run the same artifact on many machines instead of requiring a matching prebuilt binary. Having no JIT compiler available is the same as having no AOT support for the platform (an implementation detail). And as stated, the secondary passes overcome any runtime limitation by amortizing compilation across app lifetime while matching actual usage. JIT can do everything AOT can, but can also do better by using input factors that AOT is fundamentally unable to use.\n- Do me a few favors - install Gentoo Linux on a computer before trying to tell me AOT can't optimize for the specific machine, and/or learn what \"run-time dispatch\" (that Intel's ICC compiler does) is; then try to run Java on MS-DOS to find out how portable that is. Then try benchmarking \"pure\" Java code (without using a library full of AOT compiled native code; and without AOT compiling it to byte-code first). Finally, try amortizing the unnecessary performance disaster on a short lived small utility (where an AOT program will finish before you've even loaded a huge JIT compiler).\n- @Brendan Obviously if JIT doesn't have ever have as much time as AOT then it's physically impossible to accomplish the same results. This is already covered in the answer. You're so busy with anecdotes and some dogmatic fight against JIT that you're conflating concepts, implementations, and edge cases. Let's end it here.","metadata":{"transformedAt":"2026-08-18T18:32:17.758Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":59,"estimatedTokens":1999}}106{"id":"stack-3497168","source":"stackoverflow","questionId":3497168,"title":"Lisp compiler design","tags":["compiler-construction","lisp"],"text":"Title: Lisp compiler design\nTags: compiler-construction, lisp\nSource: Stack Overflow\n\nQuestion:\nI am looking for a compiler design book. I am learning it at college; but lectures were never meant for me. Moreover, at my college they don't do much practical and I believe even if I sincerely do the course about finite automata and compiler design, I will not know how to implement a compiler. So, I am looking for books about implementing a compiler. I find \"Modern Compiler Implementation\" good. It had three options of language and I chose the C book because C being a small language there will be more for me to do and more to learn during doing. However, I wanted to learn the course designing a compiler for Lisp or python [may be in the same language too]; but I could not find much material available. Lisp is an old language and there should be some documentation about designing a compiler for it. I need your suggestions regarding this.\n\nThank you.\n\n========================================\n\nTop Answer:\nHere is a great overview of a compiler design for Scheme: An Incremental Approach to Compiler Construction. It's a quite short article that describes how to build a machine code compiler for Scheme \"from scratch\".\n\n========================================\n\nComments:\n- The canonical compiler resources questions is Learning to write a compiler.\n- I like the idea of writing a Scheme/Lisp compiler because you don't have to handle all the lexing and parsing and the constructs and grammar is relatively simple.\n- @erijang: one 'only' has to implement the machinery to read some form fo s-expressions, implement some special forms (say, 30) and a macro system. The literature on the latter is huge.\n- How about SICP? Both last videoes and chapters compiles a LISP for a stack machine.\n- I found this very helpful: michaux.ca/articles/scheme-from-scratch-introduction","metadata":{"transformedAt":"2026-08-18T18:32:17.758Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":22,"estimatedTokens":470}}107{"id":"stack-1917935","source":"stackoverflow","questionId":1917935,"title":"How does C# compilation get around needing header files?","tags":["c#","c","compiler-construction","build-process","clr"],"text":"Title: How does C# compilation get around needing header files?\nTags: c#, c, compiler-construction, build-process, clr\nSource: Stack Overflow\n\nQuestion:\nI've spent my professional life as a C# developer. As a student I occasionally used C but did not deeply study it's compilation model. Recently I jumped on the bandwagon and have begun studying Objective-C. My first steps have only made me aware of holes in my pre-existing knowledge.\n\nFrom my research, C/C++/ObjC compilation requires all encountered symbols to be pre-declared. I also understand that building is a two-step process. First you compile each individual source file into individual object files. These object files might have undefined \"symbols\" (which generally correspond to the identifiers declared in the header files). Second you link the object files together to form your final output. This is a pretty high-level explanation but it satisfies my curiosity enough. But I'd also like to have a similar high-level understanding of the C# build process.\n\n**Q:** How does the C# build process get around the need for header files? I'd imagine perhaps the compilation step does two-passes?\n\n(Edit: up question here How do C/C++/Objective-C compare with C# when it comes to using libraries?)\n\n========================================\n\nTop Answer:\nUPDATE: This question was the subject of my blog for February 4th 2010. Thanks for the great question!\n\nLet me lay it out for you. In the most basic sense the compiler is a \"two pass compiler\" because the phases that the compiler goes through are:\n\n- Generation of *metadata*.\n\n- Generation of *IL*.\n\nMetadata is all the \"top level\" stuff that describes the structure of the code. Namespaces, classes, structs, enums, interfaces, delegates, methods, type parameters, formal parameters, constructors, events, attributes, and so on. Basically, everything *except* method bodies.\n\nIL is all the stuff that goes in a method body -- the actual imperative code, rather than metadata about how the code is structured.\n\nThe first phase is actually implemented via a great many passes over the sources. It's way more than two.\n\nThe first thing we do is take the text of the sources and break it up into a stream of tokens. That is, we do lexical analysis to determine that\n\n```\nclass c : b { }\n```\n\nis class, identifier, colon, identifier, left curly, right curly.\n\nWe then do a \"top level parse\" where we verify that the token streams define a grammaticaly-correct C# program. However, we skip parsing method bodies. When we hit a method body, we just blaze through the tokens until we get to the matching close curly. We'll come back to it later; we only care about getting enough information to generate metadata at this point.\n\nWe then do a \"declaration\" pass where we make notes about the location of every namespace and type declaration in the program.\n\nWe then do a pass where we verify that all the types declared have no cycles in their base types. We need to do this first because in every subsequent pass we need to be able to walk up type hierarchies without having to deal with cycles.\n\nWe then do a pass where we verify that all generic parameter constraints on generic types are also acyclic.\n\nWe then do a pass where we check whether every member of every type -- methods of classes, fields of structs, enum values, and so on -- is consistent. No cycles in enums, every overriding method overrides something that is actually virtual, and so on. At this point we can compute the \"vtable\" layouts of all interfaces, classes with virtual methods, and so on.\n\nWe then do a pass where we work out the values of all \"const\" fields.\n\nAt this point we have enough information to emit almost all the metadata for this assembly. We still do not have information about the metadata for iterator/anonymous function closures or anonymous types; we do those late.\n\nWe can now start generating IL. For each method body (and properties, indexers, constructors, and so on), we rewind the lexer to the point where the method body began and parse the method body.\n\nOnce the method body is parsed, we do an initial \"binding\" pass, where we attempt to determine the types of every expression in every statement. We then do a whole pile of passes over each method body.\n\nWe first run a pass to transform loops into gotos and labels.\n\n(The next few passes look for bad stuff.)\n\nThen we run a pass to look for use of deprecated types, for warnings.\n\nThen we run a pass that searches for uses of anonymous types that we haven't emitted metadata for yet, and emit those.\n\nThen we run a pass that searches for bad uses of expression trees. For example, using a ++ operator in an expression tree.\n\nThen we run a pass that looks for all local variables in the body that are defined, but not used, to report warnings.\n\nThen we run a pass that looks for illegal patterns inside iterator blocks.\n\nThen we run the reachability checker, to give warnings about unreachable code, and tell you when you've done something like forgotten the return at the end of a non-void method.\n\nThen we run a pass that verifies that every goto targets a sensible label, and that every label is targetted by a reachable goto.\n\nThen we run a pass that checks that all locals are definitely assigned before use, notes which local variables are closed-over outer variables of an anonymous function or iterator, and which anonymous functions are in reachable code. (This pass does too much. I have been meaning to refactor it for some time now.)\n\nAt this point we're done looking for bad stuff, but we still have way more passes to go before we sleep.\n\nNext we run a pass that detects missing ref arguments to calls on COM objects and fixes them. (This is a new feature in C# 4.)\n\nThen we run a pass that looks for stuff of the form \"new MyDelegate(Foo)\" and rewrites it into a call to CreateDelegate.\n\nThen we run a pass that transforms expression trees into the sequence of factory method calls necessary to create the expression trees at runtime.\n\nThen we run a pass that rewrites all nullable arithmetic into code that tests for HasValue, and so on.\n\nThen we run a pass that finds all references of the form base.Blah() and rewrites them into code which does the non-virtual call to the base class method.\n\nThen we run a pass which looks for object and collection initializers and turns them into the appropriate property sets, and so on.\n\nThen we run a pass which looks for dynamic calls (in C# 4) and rewrites them into dynamic call sites that use the DLR.\n\nThen we run a pass that looks for calls to removed methods. (That is, partial methods with no actual implementation, or conditional methods that don't have their conditional compilation symbol defined.) Those are turned into no-ops.\n\nThen we look for unreachable code and remove it from the tree. No point in codegenning IL for it.\n\nThen we run an optimization pass that rewrites trivial \"is\" and \"as\" operators.\n\nThen we run an optimization pass that looks for switch(constant) and rewrites it as a branch directly to the correct case.\n\nThen we run a pass which turns string concatenations into calls to the correct overload of String.Concat.\n\n(Ah, memories. These last two passes were the first things I worked on when I joined the compiler team.)\n\nThen we run a pass which rewrites uses of named and optional parameters into calls where the side effects all happen in the correct order.\n\nThen we run a pass which optimizes arithmetic; for example, if we know that M() returns an int, and we have 1 * M(), then we just turn it into M().\n\nThen we do generation of the code for anonymous types first used by this method.\n\nThen we transform anonymous functions in this body into methods of closure classes.\n\nFinally, we transform iterator blocks into switch-based state machines.\n\nThen we emit the IL for the transformed tree that we've just computed.\n\nEasy as pie!\n\n========================================\n\nCode:\n```text\nusing System;\n\nnamespace ConsoleApplication11\n{\n    class Program\n    {\n        public static Stringg ReturnsTheWrongType()\n        {\n            return null;\n        }\n\n        static void Main(string[] args)\n        {\n            CallSomeMethodThatDoesntExist();\n        }\n\n        public static Stringg AlsoReturnsTheWrongType()\n        {\n            return null;\n        }\n    }\n}\n```\n\n```text\nStringg\n```\n\n```text\nclass c : b { }\n```\n\n========================================\n\nComments:\n- This is not a complete answer... It only addresses intra-project references, not inter-library references\n- It didn't seem to be that the question involved understanding the type system - just how the compiler resolves type references within an assembly scope.\n- @codekaizen - I have to disagree, since c header files are used for forward references in your code as well as references to external libraries.\n- @Jeffery, sure, but that's not really what the question was asking. Also, the mechanism of referencing external types is so fundamentally different in C# than C++ that it's really much more than just \"referencing\". C++ contains external type definitions in header files, C# accesses assembly metadata and uses the Common Type System.\n- But the question really doesn't involve type representation.\n- +1 as this would be my answer to the question as stated and the real reason there is no need for header includes.\n- @Ken - what about if no types are imported? It doesn't really answer then question in that case.\n- Does this really answer the question? As stated. This is a good example how the two-pass compiler works to resolve references within the current source file. But I C/C++ headers are usually used to provide the signatures and extern definitions that will be supplied by other sources/objects in the project (or another project). Thus, it seems that the actual answer is that such meta data is provided in the referenced assemblies so there is no need for the headers.\n- I think it does answer the question. The problem is how does the compiler know about types used later in the compilation process. The compiler first has to parse the existing C# code to generate the type information. Strictly, it doesn't need to reference any other library if it doesn't need to import types, but it still needs to build a symbol table from the entire source base.\n- @Kevin and @codekaizen, I believe you both have valid points. My question didn't specify if I was interested in the process when types need to be imported. (I didn't know enough at the time to know that I was indeed interested in it.) Updating my question to stipulate this.\n- Actually I'll create a new question instead of editing this one.\n- Ah yes, assembly metadata... I'll have to read up on that. Thanks.\n- Looks more like a 30 pass compiler to me ;)\n- Wow one of the most insightful answers I've ever read! Thanks!\n- I am glad that I am working on more difficult problems and not on 'easy as pie' problems! ;)\n- Well, even if I was completely wrong and down-voted a thousand times, I'm glad I decided to take a stab at this question if only to read how the C# compiler works internally from one of the creators.\n- It's really not as Easy as pie!\n- I wish I were able to fav answers instead of just questions.\n- for me, making a pie is a way harder than whatever programming things\n- @Eric -- Do all your blog posts start out as SO answers?\n- @zildjohn01: No, but these days a lot of them do. My blog posts mostly start out as *questions* that I've answered somewhere else; it used to be that my primary source of questions was the internal programming language discussion email lists at Microsoft. I also re-use a lot of my analysis of errors in books; I edit books about C# as a hobby. But SO is such a rich mine for great questions that it is now the primary source.","metadata":{"transformedAt":"2026-08-18T18:32:17.759Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":178,"estimatedTokens":2950}}108{"id":"stack-4414440","source":"stackoverflow","questionId":4414440,"title":"recursive descent parser and functional programming","tags":["compiler-construction","f#","functional-programming"],"text":"Title: recursive descent parser and functional programming\nTags: compiler-construction, f#, functional-programming\nSource: Stack Overflow\n\nQuestion:\nSo lately I have been working on writing a simple compiler to better understand compiler concepts. Being a diligent reader of stackoverfolow, it seems there is a consensus that writing a compiler in a functional language is easier than an imperative one. To this end I thought I would try and kill two birds and write a compiler in F# to both learn a functional language and write a compiler at the same time.\n\nI have been reading through the dragon book and decided to start with a recursive descent parser written by hand in F#. The dragon book, however, has almost all of the code samples in an imperative style. For instance, the match token function does a significant part of its work via side effect. \n\nSo my question is what would a more traditional functional approach to parsing (i.e. few side effects) look like? I know that the Haskell compiler (GHC) is written in Haskell but I would appreciate a somewhat smaller and easier to comprehend code sample.\n\nSecond, is it worthwhile to try and adopt a functional approach to parsing, or is it really on optimizations to intermediate code that functional languages shine and I just haven't gotten there yet? That is, should I fuddle through the parsing in F# using an imperative style and switch to a more functional approach later on?\n\n========================================\n\nTop Answer:\nOne strategy for functional parsing is monadic parser combinators. You can read some about it here (and links) or use a library like FParsec. I do not recommend this approach if you're just learning/starting F#/compilers, though.\n\nAnother approach with F# is to use FsLex/FsYacc (in the PowerPack). I kinda loathe Lex/Yacc technology, so I also don't recommend this.\n\nI think you should write a recursive decent parser by hand. I don't have strong feelings regarding a tokenizer, but simply tokeninize the entire file into a(n immutable) `list` of tokens and then doing recursive descent (and leveraging some pattern-matching) is a good way to to deal with parsing. And of course, you'll want to use discrimated unions to represent the AST output of the parser (a la here).\n\nI haven't read the dragon book in a long time, but I'm apparently the only person on the planet who doesn't like it. I would consider abandoning that text in favor of a book that discusses compilers using some ML-based language, though I can't recommend one offhand.\n\nEDIT\n\nI haven't done one of these in a while, so I took a few minutes to code a small sample.\n\n```\n// AST for tiny language\ntype Op = \n | Plus \n | Minus \ntype Expr = \n | Literal of int \n | BinaryOp of Expr * Op * Expr // left, op, right \ntype Stmt =\n | IfThenElse of Expr * Stmt * Stmt // cond, then, else; 0=false in cond \n | Print of Expr\n\n// sample program\nlet input = @\"\n if 1+1-1 then \n print 42 \n else \n print 0\"\n\n// expected AST\nlet goal = \n IfThenElse(\n BinaryOp( BinaryOp(Literal(1),Plus,Literal(1)), Minus, Literal(1)), \n Print(Literal(42)), \n Print(Literal(0))) \n\n////////////////////////////////////////////////////////////////////////////\n// Lexer\n\ntype Token =\n | IF\n | THEN\n | ELSE\n | PRINT\n | NUM of int // non-negative\n | PLUS\n | MINUS\n | EOF\n\nlet makeTokenizer (s:string) =\n let i = ref 0\n let keywords = [\n \"if\", IF \n \"then\", THEN\n \"else\", ELSE\n \"print\", PRINT\n \"+\", PLUS\n \"-\", MINUS ]\n let rec getNextToken() =\n if !i >= s.Length then\n EOF\n elif System.Char.IsWhiteSpace(s.[!i]) then\n incr i\n getNextToken()\n elif System.Char.IsDigit(s.[!i]) then\n let mutable j = !i\n while j MAXINT\n else \n let keyword = keywords |> List.tryPick (fun (kwStr,kwTok) ->\n if s.IndexOf(kwStr, !i) = !i then\n i := !i + kwStr.Length\n Some(kwTok)\n else\n None)\n match keyword with\n | Some k -> k\n | None -> \n failwith \"unexpected char '%c' at position %d\" s.[!i] !i\n getNextToken\n\nlet tokens = \n let nextToken = makeTokenizer input\n let t = ref(nextToken())\n [ \n yield !t\n while !t <> EOF do\n t := nextToken()\n yield !t\n ]\n\nprintfn \"%A\" tokens // sanity check our tokenizer works\n\n/////////////////////////////////////////////////////////////////////////\n// Parser\n\nlet parseExpr toks =\n match toks with\n | NUM x :: rest ->\n let mutable rest = rest\n let mutable expr = Literal x\n while rest.Head = PLUS || rest.Head = MINUS do\n let op,y,r = \n match rest with\n | PLUS::NUM y::t -> Plus, Literal y, t\n | MINUS::NUM y::t -> Minus, Literal y, t\n | _ -> \n failwith \"parse error in expression, expected number\"\n expr failwith \"parse error in expression, expected number\"\nlet rec parseStmt toks =\n match toks with\n | PRINT :: rest -> \n let e,rest = parseExpr(rest)\n Print(e), rest\n | IF :: rest ->\n let e,rest = parseExpr(rest)\n match rest with\n | THEN :: rest ->\n let s1,rest = parseStmt(rest)\n match rest with\n | ELSE :: rest ->\n let s2,rest = parseStmt(rest)\n IfThenElse(e,s1,s2), rest\n | _ -> \n failwith \"parse error after if branch, espected 'else'\"\n | _ -> \n failwith \"parse error after if expression, expected 'then'\"\n | _ -> failwith \"parse error, expected statement\"\nlet parseProgram toks =\n let s,rest = parseStmt toks\n match rest with\n | [EOF] -> s\n | _ -> failwith \"parse error after statement, expected EOF\"\n\nlet p = parseProgram tokens\nprintfn \"%A\" p\nassert( p = goal )\n```\n\n(Hopefully there are no egregious bugs.)\n\n========================================\n\nCode:\n```text\n> let rec (|Term|_|) = function\n    | Factor(e1, t) ->\n        let rec aux e1 = function\n          | '+'::Factor(e2, t) -> aux (e1 + e2) t\n          | '-'::Factor(e2, t) -> aux (e1 - e2) t\n          | t -> Some(e1, t)\n        aux e1 t\n    | _ -> None\n  and (|Factor|_|) = function\n    | '-'::Factor(e, t) -> Some(-e, t)\n    | Atom(e1, '*'::Factor(e2, t)) -> Some(e1 * e2, t)\n    | Atom(e, t) -> Some(e, t)\n    | _ -> None\n  and (|Atom|_|) = function\n    | c::t when '0'<=c && c<='9' -> Some(int(string c), t)\n    | '('::Term(e, ')'::t) -> Some(e, t)\n    | _ -> None;;\nval ( |Term|_| ) : char list -> (int * char list) option\nval ( |Factor|_| ) : char list -> (int * char list) option\nval ( |Atom|_| ) : char list -> (int * char list) option\n```\n\n```text\n> let (Term e) = List.ofSeq \"1+2*(3-4)*-5\";;\nval e : int * char list = (11, [])\n```\n\n```text\ntype expr =\n  | Int of int\n  | Neg of expr\n  | Add of expr * expr\n  | Sub of expr * expr\n  | Mul of expr * expr\n\n  static member (~-) f = Neg f\n  static member (+) (f, g) = Add(f, g)\n  static member (-) (f, g) = Sub(f, g)\n  static member (*) (f, g) = Mul(f, g)\n\nlet rec (|Term|_|) = function\n  | Factor(e1, t) ->\n      let rec aux e1 = function\n        | '+'::Factor(e2, t) -> aux (e1 + e2) t\n        | '-'::Factor(e2, t) -> aux (e1 - e2) t\n        | t -> Some(e1, t)\n      aux e1 t\n  | _ -> None\nand (|Factor|_|) = function\n  | '-'::Factor(e, t) -> Some(-e, t)\n  | Atom(e1, '*'::Factor(e2, t)) -> Some(e1 * e2, t)\n  | Atom(e, t) -> Some(e, t)\n  | _ -> None\nand (|Atom|_|) = function\n  | c::t when '0'<=c && c<='9' -> Some(Int(int(string c)), t)\n  | '('::Term(e, ')'::t) -> Some(e, t)\n  | _ -> None\n\nlet (Term e) = List.ofSeq \"1+2*(3-4)*-5\"\n```\n\n```text\n+ - *\n```\n\n```text\n+\n```\n\n```text\n-\n```\n\n```text\n*\n```\n\n```text\nSystem.Reflection.Emit\n```\n\n```text\nmodule Tools =\n\n    open System\n    open System.Diagnostics\n    open LazyList \n\n    [<Struct;DebuggerStepThrough>]\n    type State<'a, 'b> (input:LazyList<'a>, data:'b) = //'\n        member this.Input = input\n        member this.Data = data\n\n    type Result<'a, 'b, 'c> = //'\n    | Success of 'c * State<'a, 'b>\n    | Failure of list<string> * State<'a, 'b>    \n\n    type Parser<'a, 'b, 'c> = //' \n        State<'a, 'b> -> seq<Result<'a, 'b, 'c>>\n\n    let zero<'a, 'b, 'c> (state:State<'a, 'b>) = //'\n        Seq.empty<Result<'a, 'b, 'c>>\n\n    let item<'a, 'b> (state:State<'a, 'b>) = seq { //'\n        match state.Input with\n        | Cons (head, tail) ->\n            yield Success(head, State (tail, state.Data))\n        | Nil -> ()\n    } \n\n    let result<'a, 'b, 'c> (value:'c) (state:State<'a, 'b>) = seq  { //'\n        yield Success (value, state)\n    }\n\n    let run p i d =\n        p (State(i, d)) \n\n    let (>>=) (m:Parser<'a, 'b, 'c>) (f:'c -> Parser<'a, 'b, 'd>) (state:State<'a, 'b>) = //'\n        let rec run errors = seq {\n            for r in m state do\n                match r with\n                | Success (v, s) ->\n                    yield! f v s\n                | Failure (ms, s) ->\n                    yield! run (errors @ ms)\n        }\n        run []\n\n    let (<|>) (l:Parser<'a, 'b, 'c>) (r:Parser<'a, 'b, 'c>) (state:State<'a, 'b>) = //'  \n        let rec run p = seq {\n            for result in p state do\n                match result with\n                | Success (_, _) ->\n                    yield result\n                | Failure (_, _) -> ()\n        }\n        Seq.append (run l) (run r)\n\n    type ParseMonad() =        \n        member this.Bind (f:Parser<'a, 'b, 'c>, g:'c -> Parser<'a, 'b, 'd>) : Parser<'a, 'b, 'd> = f >>= g //'     \n        member this.Combine (f, g) = f <|> g      \n        member this.Delay (f:unit -> Parser<'a, 'b, 'c>) (state:State<'a, 'b>) = f () state //'\n        member this.Return x = result x\n        member this.ReturnFrom p = p\n        member this.Zero () = zero\n\n    let parse = ParseMonad()\n\n    let (|>>) (parser:Parser<'a, 'b, 'c>) (f:'c -> 'd) = parse { //'\n        let! v = parser\n        return f v   \n    }\n\n    let satisfy predicate = parse {\n        let! value = item\n        if predicate value then\n            return value \n    }\n\n    let maybe parser = parse {\n        return! parser |>> Some <|> result None \n    }\n\n    let choice (ps:seq<Parser<'a, 'b, 'c>>) (state:State<'a, 'b>) = seq { //'\n        if not (LazyList.isEmpty state.Input) then\n            for p in ps do\n                yield! p state    \n    }\n\n    let between left right parser =\n        parse {\n            let! _ = left\n            let! v = parser\n            let! _ = right\n            return v\n        }\n\n    let skip p = parse {\n        let! v = p\n        return ()\n    }\n\n    let many parser = \n        let rec many result = parse {\n            let! v = parser\n            let result = v::result\n            return! many result\n            return result    \n        }\n        many []\n\n    let many1 parser = parse {\n        let! r = many parser\n        if not r.IsEmpty then\n            return r\n    }\n\n    let manyFold parser start (f:_ -> _ -> _) = parse {\n        let! r = many parser\n        return r |> List.fold f start\n    }\n\n    let many1Fold parser start (f:_ -> _ -> _) = parse {\n        let! r = many1 parser\n        return r |> List.fold f start\n    } \n\n    let isNotFollowedBy p =\n        parse {\n            let! v = maybe p\n            match v with\n            | Some _ -> ()\n            | None -> return ()\n        }\n\n    let pipe2 (p1:Parser<'a, 'b, 'c>) (p2:Parser<'a, 'b, 'd>) (f:'c -> 'd -> 'e) = //' \n        parse {\n            let! v1 = p1\n            let! v2 = p2\n            return f v1 v2\n        }\n\n    let pipe3 (p1:Parser<'a, 'b, 'c>) (p2:Parser<'a, 'b, 'd>) (p3:Parser<'a, 'b, 'e>) (f:'c -> 'd -> 'e -> 'f) = //' \n        parse {\n            let! v1 = p1\n            let! v2 = p2\n            let! v3 = p3\n            return f v1 v2 v3\n        }\n\n    let pipe4 (p1:Parser<'a, 'b, 'c>) (p2:Parser<'a, 'b, 'd>) (p3:Parser<'a, 'b, 'e>) (p4:Parser<'a, 'b, 'f>) (f:'c -> 'd -> 'e -> 'f -> 'g) = //' \n        parse {\n            let! v1 = p1\n            let! v2 = p2\n            let! v3 = p3\n            let! v4 = p4\n            return f v1 v2 v3 v4\n        }\n\n    let pipe5 (p1:Parser<'a, 'b, 'c>) (p2:Parser<'a, 'b, 'd>) (p3:Parser<'a, 'b, 'e>) (p4:Parser<'a, 'b, 'f>) (p5:Parser<'a, 'b, 'g>) (f:'c -> 'd -> 'e -> 'f -> 'g -> 'h) = //' \n        parse {\n            let! v1 = p1\n            let! v2 = p2\n            let! v3 = p3\n            let! v4 = p4\n            let! v5 = p5\n            return f v1 v2 v3 v4 v5\n        }\n\n    let tuple2<'a, 'b, 'c, 'd, 'e> (p1:Parser<'a, 'b, 'c>) (p2:Parser<'a, 'b, 'd>) (f:'c * 'd -> 'e) = //' \n        parse {\n            let! v1 = p1\n            let! v2 = p2\n            return f (v1, v2)\n        }\n\n    let tuple3 (p1:Parser<'a, 'b, 'c>) (p2:Parser<'a, 'b, 'd>) (p3:Parser<'a, 'b, 'e>) (f:'c * 'd * 'e -> 'f) = //' \n        parse {\n            let! v1 = p1\n            let! v2 = p2\n            let! v3 = p3\n            return f (v1, v2, v3)\n        }\n\n    let tuple4 (p1:Parser<'a, 'b, 'c>) (p2:Parser<'a, 'b, 'd>) (p3:Parser<'a, 'b, 'e>) (p4:Parser<'a, 'b, 'f>) (f:'c * 'd * 'e * 'f -> 'g) = //' \n        parse {\n            let! v1 = p1\n            let! v2 = p2\n            let! v3 = p3\n            let! v4 = p4\n            return f (v1, v2, v3, v4)\n        }\n\n    let tuple5 (p1:Parser<'a, 'b, 'c>) (p2:Parser<'a, 'b, 'd>) (p3:Parser<'a, 'b, 'e>) (p4:Parser<'a, 'b, 'f>) (p5:Parser<'a, 'b, 'g>) (f:'c * 'd * 'e * 'f * 'g -> 'h) = //' \n        parse {\n            let! v1 = p1\n            let! v2 = p2\n            let! v3 = p3\n            let! v4 = p4\n            let! v5 = p5\n            return f (v1, v2, v3, v4, v5)\n        }\n\n    let createParserRef<'a, 'b, 'c> () = //'\n        let dummyParser = fun state -> failwith \"a parser was not initialized\"\n        let r = ref dummyParser\n        (fun state -> !r state), r : Parser<'a, 'b, 'c> * Parser<'a, 'b, 'c> ref //'\n```\n\n```text\nand conditionalExpressionNoIn = \n    parse {\n        let! e1 = logicalORExpressionNoIn\n        return! parse {\n            do! skip expectQuestionMark\n            let! e2 = assignmentExpression\n            do! skip expectColon\n            let! e3 = assignmentExpressionNoIn\n            return ConditionalExpressionNoIn (e1, e2, e3)\n        }\n        return ConditionalExpressionNoIn (e1, SourceElement.Nil, SourceElement.Nil)\n    }\n```\n\n```text\nLazyList\n```\n\n```text\n// AST for tiny language\ntype Op = \n    | Plus \n    | Minus \ntype Expr = \n    | Literal of int \n    | BinaryOp of Expr * Op * Expr // left, op, right \ntype Stmt =\n    | IfThenElse of Expr * Stmt * Stmt // cond, then, else; 0=false in cond \n    | Print of Expr\n\n// sample program\nlet input = @\"\n    if 1+1-1 then \n        print 42 \n    else \n        print 0\"\n\n// expected AST\nlet goal = \n    IfThenElse(\n        BinaryOp( BinaryOp(Literal(1),Plus,Literal(1)), Minus, Literal(1)), \n        Print(Literal(42)), \n        Print(Literal(0))) \n\n////////////////////////////////////////////////////////////////////////////\n// Lexer\n\ntype Token =\n    | IF\n    | THEN\n    | ELSE\n    | PRINT\n    | NUM of int  // non-negative\n    | PLUS\n    | MINUS\n    | EOF\n\nlet makeTokenizer (s:string) =\n    let i = ref 0\n    let keywords = [\n        \"if\", IF \n        \"then\", THEN\n        \"else\", ELSE\n        \"print\", PRINT\n        \"+\", PLUS\n        \"-\", MINUS ]\n    let rec getNextToken() =\n        if !i >= s.Length then\n            EOF\n        elif System.Char.IsWhiteSpace(s.[!i]) then\n            incr i\n            getNextToken()\n        elif System.Char.IsDigit(s.[!i]) then\n            let mutable j = !i\n            while j < s.Length && System.Char.IsDigit(s.[j]) do\n                j <- j + 1\n            let numStr = s.Substring(!i, j - !i)\n            i := j\n            NUM(System.Int32.Parse(numStr)) // may throw, e.g. if > MAXINT\n        else \n            let keyword = keywords |> List.tryPick (fun (kwStr,kwTok) ->\n                if s.IndexOf(kwStr, !i) = !i then\n                    i := !i + kwStr.Length\n                    Some(kwTok)\n                else\n                    None)\n            match keyword with\n            | Some k -> k\n            | None -> \n                failwith \"unexpected char '%c' at position %d\" s.[!i] !i\n    getNextToken\n\nlet tokens = \n    let nextToken = makeTokenizer input\n    let t = ref(nextToken())\n    [ \n        yield !t\n        while !t <> EOF do\n            t := nextToken()\n            yield !t\n    ]\n\nprintfn \"%A\" tokens // sanity check our tokenizer works\n\n/////////////////////////////////////////////////////////////////////////\n// Parser\n\nlet parseExpr toks =\n    match toks with\n    | NUM x :: rest ->\n        let mutable rest = rest\n        let mutable expr = Literal x\n        while rest.Head = PLUS || rest.Head = MINUS do\n            let op,y,r = \n                match rest with\n                | PLUS::NUM y::t -> Plus, Literal y, t\n                | MINUS::NUM y::t -> Minus, Literal y, t\n                | _ -> \n                    failwith \"parse error in expression, expected number\"\n            expr <- BinaryOp(expr, op, y)\n            rest <- r\n        expr, rest\n    | _ -> failwith \"parse error in expression, expected number\"\nlet rec parseStmt toks =\n    match toks with\n    | PRINT :: rest -> \n        let e,rest = parseExpr(rest)\n        Print(e), rest\n    | IF :: rest ->\n        let e,rest = parseExpr(rest)\n        match rest with\n        | THEN :: rest ->\n            let s1,rest = parseStmt(rest)\n            match rest with\n            | ELSE :: rest ->\n                let s2,rest = parseStmt(rest)\n                IfThenElse(e,s1,s2), rest\n            | _ -> \n                failwith \"parse error after if branch, espected 'else'\"\n        | _ -> \n            failwith \"parse error after if expression, expected 'then'\"\n    | _ -> failwith \"parse error, expected statement\"\nlet parseProgram toks =\n    let s,rest = parseStmt toks\n    match rest with\n    | [EOF] -> s\n    | _ -> failwith \"parse error after statement, expected EOF\"\n\nlet p = parseProgram tokens\nprintfn \"%A\" p\nassert( p = goal )\n```\n\n```text\nlist\n```\n\n```text\ntoken list -> ast * token list\n```\n\n========================================\n\nComments:\n- Parsing is easy enough in Haskell, but code generation within that language I find very nice. Especially if you want to play with static analysis.\n- Possibly see also stackoverflow.com/questions/531707/&hellip;\n- I hadn't seen representing an AST with a discriminated union before. That is a pretty cool feature.\n- @Samsdram: Actually, when people are talking about how easy it is to write compilers in functional languages *that* is *exactly* what they are talking about. It's actually not so much about being a functional language, but rather a language with a sophisticated type system and powerful pattern matching.\n- Indeed, DUs and pattern-matching are just made for tree-processing, and compilers are all about tree-processing.\n- @J&#246;rg W Mittag: I would also mention the Tail call which in our case ensures no stack overflow will happen during parsing as opposed to using recursion in traditional imperative languages where return address is put on top of the stack in each call.\n- @Brain: why do you loath Lex/Yacc technology? how does the F# compiler go about lexing and parsing?\n- The F# compiler uses FsLex and FsYacc, which is maybe part of why I don't like it. :) Yacc parsers are hard to author and understand and debug/diagnose IMO. And I also don't like having to use a DSL (an EDSL would be ok) because I lose so much tooling. And these are like 30- or 40- year-old technologies, so I have to imagine there is something better now. (Last time I used ANTLR was about 10 years ago, I think maybe it addresses one or two of my criticisms, but not all.) As an aside, I find parser combinators very hard to debug/diagnose as well.\n- @Brain: interesting. So you prefer hand-written recursive decent parsers? Is that technique suitable for languages as advanced as F#? I wonder how much room there is for improvement in this area, given parsing has got to be one of the oldest topics in computer science (though perhaps that is false thinking since computer science is quite young). Maybe it's just an inherently difficult topic, with no golden solution (i.e. \"easy\" to author and debug/diagnose yet powerful too).\n- I prefer hand-written or LL parsers for debugging/diagnosis. So for example, the code ANTLR generates is debuggable, as I recall, since it uses LL technology. Yacc uses LR, which is in some sense more powerful, but it adds an abstraction layer that makes it very hard to tell what is going on when you try to debug it. I think part of the problem is that CS treated parsing as a 'solved problem' (which it is, in the academic sense), but real-world software requires more. (TBC...)\n- ...A command-line compiler only requires you to be able to parse correct programs (or else report an error). But an IDE requires that you can parse incorrect programs, and still recover and provide useful parse info so Intellisense, Parameter Info, etc, all continue to function even when the programmer is in the middle of editing the file and has un-finished code that does not parse. Which means you need sophisticated error recovery logic and lots of extra rules in the grammar to deal with common mal-formed fragments of code. And Yacc/LR tools are typically too opaque when parses fail.\n- Very nice example but for any complex language the implementation will balloon out of control.\n- @Brian: I find ocamllex and ocamlyacc easy to use including debugging. The performance is great and they have IDE support. I think it would be great if F# was up-to-par with them (e.g. named submatches in the lexer and support in the IDE) and you could even go further by making them EDSLs using run-time code generation. CamlP4 is an EDSL, of course. As for preserving info during edits, I think it is better to cache previous results rather than trying to dick with the parser.","metadata":{"transformedAt":"2026-08-18T18:32:17.759Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":14,"totalLines":629,"estimatedTokens":5342}}109{"id":"stack-425988","source":"stackoverflow","questionId":425988,"title":"Maximum Method Name Length","tags":["compiler-construction","programming-languages","specifications","name-length"],"text":"Title: Maximum Method Name Length\nTags: compiler-construction, programming-languages, specifications, name-length\nSource: Stack Overflow\n\nQuestion:\nDoes anyone happen to know what the maximum length of a method name is in your programming language of choice? I was going to make this a C# specific question, but I think it would be nice to know across the spectrum.\n\nWhat are the factors involved as well:\n\n- Does the language specification limit this?\nWhat does the compiler limit it to?\n\n- Is it different on 32bit vs 64bit machines?\n\n========================================\n\nTop Answer:\nPHP seems to be limited only by the script's memory limit.\n\nWith 128Mb I was able to create a class (and method) with 4 million characters.\n\n```\n\n```\n\n========================================\n\nCode:\n```text\n<?php\nini_set('memory_limit', '128M');\n$i = 1024 * 1024;\n\nwhile ($i < 10000000)\n{\n    $className = str_repeat('i', $i);\n    eval(\"class $className { public function $className() { echo '$i<br>'; } }\");\n    new $className();\n    $i *= 2;\n}\n\n?>\n```\n\n```text\nWelcome to Clozure Common Lisp Version 1.3-dev-r11583M-trunk  (DarwinX8664)!\n? array-dimension-limit\n72057594037927936\n? \n\nWelcome to Clozure Common Lisp Version 1.3-dev-r11583M-trunk  (DarwinX8632)!\n? array-dimension-limit\n16777216\n?\n```\n\n```text\narray-dimension-limit\n```\n\n```text\narray-dimension-limit\n```\n\n```text\nint a123456789a123...;\n```\n\n```text\nint whyintheworldwouldyoueverhaveanidenfifierthislongitsreallyjustquiteridiculousimeancmonyoucouldatleasthavethecommoncourtesyofmakingitcamelcasesoitsnotsohardtoreadcmonjuststopnowyourereallyreachingtomakethisaslongaspossiblearentyou123412341234alrightwellthatsenoughnowisntitwelliguessnotbecauseimstillgoingthisisofficallytheworstidentifiereverಠ_ಠokaynowthatithasunicodeitsofficialbutseriouslythisthingissolongthatihadtogetupinthemiddleofittotakeabreakbeforesittingdowntofinishtoppingitofftothemaxcharlimitof___511;\n```\n\n========================================\n\nComments:\n- Lol, curiosity mostly. A colleague and I were writing some unit test methods and one was particularly verbose (well under any limit), but we became intrigued as to what the maximum length allowed was.\n- OK - just checking - I was figured it was something like that. The only time I ran into anything like this was the stupid warning messages in VC6 for debug builds that had STL - the symbol names were longer than 256 chars or something so they would be truncated.\n- @Tim: I must suffer through tens of thousands of those warnings every day...\n- I have one project where I have seen error messages with mangled symbol names long enough to flush a 5000 line scroll back buffer. But that is Uber-template meta stuff.\n- Your answer seems to contradict Kirill Osenkov's and DLeh's answers of 511. It would nice if you had a reference for your C# claim as I believe you are wrong here...\n- @SerjSagan: I've just tried it and it's larger than 511, although there *is* a limit. I'd say it's still larger than the bounds of readability and sensible machine-generated names though. I'll edit with the precise limit at this point in time...\n- Your answer seems to contradict John Skeet's answer. It would nice if you had a reference for your C# claim of 511","metadata":{"transformedAt":"2026-08-18T18:32:17.759Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":82,"estimatedTokens":808}}110{"id":"stack-5477483","source":"stackoverflow","questionId":5477483,"title":"Why are \"Executable files\" operating system dependent?","tags":["windows","linux","compiler-construction","operating-system","executable"],"text":"Title: Why are \"Executable files\" operating system dependent?\nTags: windows, linux, compiler-construction, operating-system, executable\nSource: Stack Overflow\n\nQuestion:\nI understand that each CPU/architecture has it's own instruction set, therefore a program(binary) written for a specific CPU cannot run on another. But what i don't really understand is why an executable file (binary like .exe for instance) cannot run on Linux but can run on windows even on the very same machine.\n\nThis is a basic question, and the answer i'm expecting is that .exe and other binary formats are probably not Raw machine instructions but they contain some data that is operating system dependent. If this is true, then what this OS dependent data is like? and as an example what is the format of an .exe file and the difference between it and Linux executables?\n\nIs there a source i can get brief and detailed information about this?\n\n========================================\n\nTop Answer:\n.exe and other binary formats are [definitely] not Raw machine instructions but they contain some data that is operating system dependent. \n\n what this OS dependent data is like? and as an example what is the format of an .exe file and the difference between it and Linux executables?\n\nWell, I guess Google failed you utterly. .EXE formats are very well-defined by Windows documentation.\n\nhttp://support.microsoft.com/kb/65122\n\nThe Linux `ld` application loads an executable into memory prior to \"exec\" to that file. You could read up on `ld` format or even the famous `a.out` file.\n\nhttp://linux.die.net/man/1/ld\n\nhttp://en.wikipedia.org/wiki/A.out\n\nhttp://en.wikipedia.org/wiki/Executable\n\n========================================\n\nCode:\n```text\nld\n```\n\n```text\nld\n```\n\n```text\na.out\n```\n\n```text\nlcall\n```\n\n```text\nstat()\n```\n\n```text\n.exe\n```\n\n```text\n.exe\n```\n\n```text\n.com\n```\n\n```text\nbless\n```\n\n========================================\n\nComments:\n- Who says they can't? Windows binaries run on Linux and Mac with help from Wine. Also you might have to compromise on the brief *and* detailed requirement.\n- Actually it usually can with a compatibility layer such as WINE. The issue is at least twofold: file format and interfaces to the operating system. The latter tends to be the bigger problem. For example an ELF file from a BSD system will not run on a Linux system, although the other way it can run (depending on some factors) through a compatibility layer that maps the system calls.\n- First, thanks a lot for your answers. My question was why they do not work (without any workarounds), but since you've mentioned WINE and after knowing the answers, i have to ask the obvious question : If we can run .exe on Linux using WINE, then it should be practically possible to have a program that converts from .exe file to Linux executable FILE (and i suppose vice versa). This can be done by an easy direct mapping of instructions/system calls/file structure. So do we have such conversion programs?\n- Linux specific version: stackoverflow.com/questions/1030238\n- Telling windows executables can run on Linux using WINE is like telling Commodore instructions can run on Windows using a fully fledged emulator imitating every function of Commodore hardware inside Windows.\n- But Wine is not an emulator!\n- So program logic is fundamentally different between platforms? The kernel doesn't just manage threads and pass instructions to the cpu? Does this have anything to do with why ascii text files are formatted slightly differently? Maybe this is an example of OS dependent libraries? So many questions. sorry\n- operating system services like user interface ? @geekosaur","metadata":{"transformedAt":"2026-08-18T18:32:17.759Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":80,"estimatedTokens":914}}111{"id":"stack-7562477","source":"stackoverflow","questionId":7562477,"title":"Why do we need prefix, postfix notation","tags":["algorithm","compiler-construction","programming-languages","formal-languages"],"text":"Title: Why do we need prefix, postfix notation\nTags: algorithm, compiler-construction, programming-languages, formal-languages\nSource: Stack Overflow\n\nQuestion:\nI know how each of them can be converted to one another but never really understood what their applications are. The usual infix operation is quite readable, but where does it fail which led to inception of prefix and postfix notation\n\n========================================\n\nTop Answer:\nPostfix notation, also known as RPN, is very easy to process left-to-right. An operand is pushed onto a stack; an operator pops its operand(s) from the stack and pushes the result. Little or no parsing is necessary. It's used by Forth and by some calculators (HP calculators are noted for using RPN).\n\nPrefix notation is nearly as easy to process; it's used in Lisp.\n\n========================================\n\nCode:\n```text\n1 # 2 $ 3\n```\n\n```text\n1 2 # 3 $\n```\n\n```text\n1 2 3 $ #\n```\n\n```text\na ? b : c\n```\n\n```text\n+ 1 * 2 7 3 4\n```\n\n```text\n*\n```\n\n```text\n+\n```\n\n```text\n+ 1 (* 2 7 3) 4\n```\n\n========================================\n\nComments:\n- sin(x) is prefix. -x is prefix. Prefix is used regularly along with infix. What more do you need to know except \"you're already using it\".\n- @S.Lott: I believe he means for binary operators.\n- For postfix, en.wikipedia.org/wiki/Reverse_Polish_Notation\" \"... parenthesis-free as long as operator arities are fixed.\"\n- It may be that infix is quite readable only because we are so accustomed to it!\n- I feel that prefix would be more intuitive had we been raised accustomed to it. Prefix is easy to program for in computers, but postfix uses less memory. Infix is the worst in every way, both for humans (IMO) and computers\n- I suggest you to read about these notations for this link itviewson.files.wordpress.com/2012/06/infixprefixpostfix.pdf&zwnj;&#8203;. It will clear your all confusions\n- Maybe its practically easy for the compilers?\n- @MooingDuck If evaluated from right-to-left prefix is same as postfix. If evaluated left-to-right prefix is faster than postfix because it supports short-circuit optimization for boolean operators.\n- Prefix and postfix notation still require one to know how many operands each operator takes. They can't be parsed without that knowledge. Lisp gets around this by parenthesizing each sub-expression. `1 2 # 3 $` could equivalent to `($ (# 1 2) 3)` or `($ 1 (# 2) 3)`.\n- Actually prefix is easier to us too because we don't have to use parenthesis. It's just that we learned infix before prefix.\n- No, Lisp doesn't use prefix notation. In Lisp, everything is parenthesized.\n- @DerekLedbetter: Yes, everything is parenthesized, but the stuff between the parentheses uses prefix notation: `(+ 2 2)`. It's not *pure* prefix notation.\n- 8 years later: The parentheses make it possible to specify how many arguments are passed to a function without knowing how many it expects.\n- Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.\n- This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review\n- Prefix and postfix don't require parsing *at all.* Just evaluation. Your second paragraph is just waffle.","metadata":{"transformedAt":"2026-08-18T18:32:17.759Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":68,"estimatedTokens":879}}112{"id":"stack-909092","source":"stackoverflow","questionId":909092,"title":"Why is the compiler package discontinued in Python 3?","tags":["python","compiler-construction","python-3.x"],"text":"Title: Why is the compiler package discontinued in Python 3?\nTags: python, compiler-construction, python-3.x\nSource: Stack Overflow\n\nQuestion:\nI was just pleasantly surprised to came across the documentation of Python's compiler package, but noticed that it's gone in Python 3.0, without any clear replacement or explanation.\n\nI can't seem to find any discussion on python-dev about how this decision was made - does anyone have any insight inot this decision?\n\n========================================\n\nTop Answer:\nTo provide specific references to the decision trail:\n\n- https://mail.python.org/pipermail/python-3000/2007-May/007577.html\n\n- PEP 3108\n\nAnd, for what it's worth, I started Python3 port of the compiler package, to be maintained outside of the stdlib:\n\n- https://github.com/pfalcon/python-compiler\n\n- https://pypi.org/project/python-compiler/\n\n========================================\n\nCode:\n```text\ncompiler\n```\n\n========================================\n\nComments:\n- Note that for many Python 3.x changes, you need to search the archives for the python-3000 list in addition to python-dev. For example, one discussion of this decision can be seen at: mail.python.org/pipermail/python-3000/2008-January/011693.ht&zwnj;&#8203;ml\n- To expand on this answer: the compiler package was an absolute pain to maintain and almost always either slightly out of date or subtly different from the \"real\" compiler. Making it easier to work with the real compiler via the `ast` module made maintenance a lot easier while still providing similar functionality.","metadata":{"transformedAt":"2026-08-18T18:32:17.759Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":36,"estimatedTokens":390}}113{"id":"stack-10369421","source":"stackoverflow","questionId":10369421,"title":"Why does adding local variables make .NET code slower","tags":["c#",".net","performance","compiler-construction","jit"],"text":"Title: Why does adding local variables make .NET code slower\nTags: c#, .net, performance, compiler-construction, jit\nSource: Stack Overflow\n\nQuestion:\nWhy does commenting out the first two lines of this for loop and uncommenting the third result in a 42% speedup?\n\n```\nint count = 0;\nfor (uint i = 0; i Behind the timing is vastly different assembly code: 13 vs. 7 instructions in the loop. The platform is Windows 7 running .NET 4.0 x64. Code optimization is enabled, and the test app was run outside VS2010. [**Update:** Repro project, useful for verifying project settings.]\n\nEliminating the intermediate boolean is a fundamental optimization, one of the simplest in my 1980's era Dragon Book. How did the optimization not get applied when generating the CIL or JITing the x64 machine code?\n\nIs there a \"Really compiler, I would like you to optimize this code, please\" switch? While I sympathize with the sentiment that premature optimization is akin to the love of money, I could see the frustration in trying to profile a complex algorithm that had problems like this scattered throughout its routines. You'd work through the hotspots but have no hint of the broader warm region that could be vastly improved by hand tweaking what we normally take for granted from the compiler. I sure hope I'm missing something here.\n\n**Update:** Speed differences also occur for x86, but depend on the order that methods are just-in-time compiled. See Why does JIT order affect performance?\n\n**Assembly code** (as requested):\n\n```\nvar isMultipleOf16 = i % 16 == 0;\n00000037 mov eax,edx \n00000039 and eax,0Fh \n0000003c xor ecx,ecx \n0000003e test eax,eax \n00000040 sete cl \n count += isMultipleOf16 ? 1 : 0;\n00000043 movzx eax,cl \n00000046 test eax,eax \n00000048 jne 0000000000000050 \n0000004a xor eax,eax \n0000004c jmp 0000000000000055 \n0000004e xchg ax,ax \n00000050 mov eax,1 \n00000055 lea r8d,[rbx+rax]\n```\n\n```\ncount += i % 16 == 0 ? 1 : 0;\n00000037 mov eax,ecx \n00000039 and eax,0Fh \n0000003c je 0000000000000042 \n0000003e xor eax,eax \n00000040 jmp 0000000000000047 \n00000042 mov eax,1 \n00000047 lea edx,[rbx+rax]\n```\n\n========================================\n\nTop Answer:\nQuestion should be \"Why do I see such a difference on my machine?\". I cannot reproduce such a huge speed difference and suspect there is something specific to your environment. Very difficult to tell what it can be though. Can be some (compiler) options you have set some time ago and forgot about them.\n\nI have create a console application, rebuild in Release mode (x86) and run outside VS. Results are virtually identical, 1.77 seconds for both methods. Here is the exact code:\n\n```\nstatic void Main(string[] args)\n{\n Stopwatch sw = new Stopwatch();\n sw.Start();\n int count = 0;\n\n for (uint i = 0; i Please, anyone who has 5 minutes copy the code, rebuild, run outside VS and post results in comments to this answer. I'd like to avoid saying \"it works on my machine\".\n\n**EDIT**\n\nTo be sure I have created a **64 bit** Winforms application and the results are similar as in the the question - the **first method is slower** (1.57 sec) than the second one (1.05 sec). The difference I observe is 33% - still a lot. Seems there is a bug in .NET4 64 bit JIT compiler.\n\n========================================\n\nCode:\n```text\nint count = 0;\nfor (uint i = 0; i < 1000000000; ++i) {\n    var isMultipleOf16 = i % 16 == 0;\n    count += isMultipleOf16 ? 1 : 0;\n    //count += i % 16 == 0 ? 1 : 0;\n}\n```\n\n```text\nvar isMultipleOf16 = i % 16 == 0;\n00000037  mov         eax,edx \n00000039  and         eax,0Fh \n0000003c  xor         ecx,ecx \n0000003e  test        eax,eax \n00000040  sete        cl \n    count += isMultipleOf16 ? 1 : 0;\n00000043  movzx       eax,cl \n00000046  test        eax,eax \n00000048  jne         0000000000000050 \n0000004a  xor         eax,eax \n0000004c  jmp         0000000000000055 \n0000004e  xchg        ax,ax \n00000050  mov         eax,1 \n00000055  lea         r8d,[rbx+rax]\n```\n\n```text\ncount += i % 16 == 0 ? 1 : 0;\n00000037  mov         eax,ecx \n00000039  and         eax,0Fh \n0000003c  je          0000000000000042 \n0000003e  xor         eax,eax \n00000040  jmp         0000000000000047 \n00000042  mov         eax,1 \n00000047  lea         edx,[rbx+rax]\n```\n\n```text\nstatic void Main(string[] args)\n{\n    Stopwatch sw = new Stopwatch();\n    sw.Start();\n    int count = 0;\n\n    for (uint i = 0; i < 1000000000; ++i)\n    {\n        // 1st method\n        var isMultipleOf16 = i % 16 == 0;\n        count += isMultipleOf16 ? 1 : 0;\n\n        // 2nd method\n        //count += i % 16 == 0 ? 1 : 0;\n    }\n\n    sw.Stop();\n    Console.WriteLine(string.Format(\"Ellapsed {0}, count {1}\", sw.Elapsed, count));\n    Console.ReadKey();\n}\n```\n\n```text\nclass Program\n{\n    static void Main()\n    {\n        ConditionalTest();\n        SingleLineTest();\n        MultiLineTest();\n        ConditionalTest();\n        SingleLineTest();\n        MultiLineTest();\n        ConditionalTest();\n        SingleLineTest();\n        MultiLineTest();\n    }\n\n    public static void ConditionalTest()\n    {\n        Stopwatch stopwatch = new Stopwatch();\n        stopwatch.Start();\n        int count = 0;\n        for (uint i = 0; i < 1000000000; ++i) {\n            if (i % 16 == 0) ++count;\n        }\n        stopwatch.Stop();\n        Console.WriteLine(\"Conditional test --> Count: {0}, Time: {1}\", count, stopwatch.ElapsedMilliseconds);\n    }\n\n    public static void SingleLineTest()\n    {\n        Stopwatch stopwatch = new Stopwatch();\n        stopwatch.Start();\n        int count = 0;\n        for (uint i = 0; i < 1000000000; ++i) {\n            count += i % 16 == 0 ? 1 : 0;\n        }\n        stopwatch.Stop();\n        Console.WriteLine(\"Single-line test --> Count: {0}, Time: {1}\", count, stopwatch.ElapsedMilliseconds);\n    }\n\n    public static void MultiLineTest()\n    {\n        Stopwatch stopwatch = new Stopwatch();\n        stopwatch.Start();\n        int count = 0;\n        for (uint i = 0; i < 1000000000; ++i) {\n            var isMultipleOf16 = i % 16 == 0;\n            count += isMultipleOf16 ? 1 : 0;\n        }\n        stopwatch.Stop();\n        Console.WriteLine(\"Multi-line test  --> Count: {0}, Time: {1}\", count, stopwatch.ElapsedMilliseconds);\n    }\n}\n```\n\n========================================\n\nComments:\n- I'd be curious to see the different assembly code. Could you post it?\n- have you tested bool isMultipleOf16 = ...?\n- @David.Chu.ca - that wouldn't make a difference - `var` is \"compiler, please infer the type of this variable, and pretend I wrote that instead\". In this case, it will have inferred `bool` for itself.\n- how about static isMulitppleOF16?\n- @EdwardBrey: Since you did this in *Debug* mode all bets are off\n- The only way to get this code is by having the jitter optimizer disabled. Invalid test.\n- @BrokenGlass You sound rather confident. I was careful to ensure I was in Release mode and that optimizations remained enabled. To verify, I turned optimization off and verified that the timings got slower and the assembly code got larger. I changed the Project Configuration to \"Debug\" and turned on the \"Suppress JIT optimization on module\" setting. When I went back to unsuppressed release optimized, the code got smaller and faster again. If I am making an error, is there anything I should be take note of to spot it?\n- @Hans I tried two separate computers with the same result. I also reimplemented on multiple projects, because I couldn't believe it was really true. Still, same result. I haven't done anything I know of to disable the JIT optimizers on my systems, although I don't know where to look for this. Can you repro?\n- Sure, I can get the same code you posted by turning on the \"Suppress JIT optimization\" debugger option. The optimized code is *very* different.\n- @Hans The 13 lines of assembly code above are with \"Suppress JIT optimization\" turned off. If I turn suppression on, it gets even worse and balloons to 21 lines of assembly code.\n- I cannot repro your result with the exact same OS and VS+.NET versions, no idea why you are getting unoptimized code.\n- @Hans Let's try to narrow this down. I put a repro on GitHub. What do you get for results?\n- @EdwardBrey: I can't find a source at the moment, but I believe the jitter and/or other optimizer settings are different if you have a debugger attached *at all* (that is, if you're running from Visual Studio, even if you compiled in \"Release\" mode). Try running your code from the command line (not from VS) and see what happens.\n- @Daniel I ran all my timing tests from the command line outside Visual Studio. I only ran within Visual Studio to get assembly code listings. As a double-check, just now I tried running the timing test with Visual Studio not even running. I still see the different timings.\n- @EdwardBrey: Running the code as x86, makes both run the same time. Edit: But only on .NET 4. .NET 2 has a difference with both.\n- @leppie Could you try the x86 configuration on the test project on github? (Github has a ZIP button to quickly grab a zip file of the project.) I get very different timings between the one- and two-statement tests, even for x86.\n- @EdwardBrey: That's what I did.\n- @leppie If you turn off \"Suppress JIT optimization\" in the VS2010 options and put a breakpoint on a statement in the loop, do you see different assembly code for the different forms, specifically the addition of `sete`, `movzx`, and `test` instructions for the two-statement form?\n- When I dump the JIT compiled code for the two versions (via WinDbg), I do see differences, but I also see significantly more code than you list in your question. Remember there is not a straight forward mapping between the source lines and the JIT compiled code, so you should list all of it. Comparing just the body of the loop may not provide enough detail for a comparison.\n- I wondered to myself when I saw the assembly code if somehow optimization was disabled. I got the assembly code by stopping at a breakpoint in the VS2010 debugger and using the Disassembly window (whereas the timings I got by running without a debugger). As a test, I turned on the Tools > Options > Debugging > General > \"Suppress JIT optimization on module\" setting. Sure enough, the assembly code got even larger.\n- In the native C++ world, it's perfectly normal for breakpoints and code order to be weird when optimizations are enabled. Likewise, variables like `isMultipleOf16` are not always available in the debugger. That's why there is debug mode. At the end of the day, we're still running the same machine code, so I don't see why the CLR would make things any different. Indeed, when exceptions occur in C#, even in debug mode I sometimes get a message about a variable's value being \"optimized away\", even in debug mode.\n- +1 for the remark that debug settings can affect codegeneration.\n- First method: 1.8736291s, second method: 1.8566318s on my machine, rebuilt with Release (x86), ran outside VS, using the exact same code.\n- You need to do something with `count` (such as include it in your `WriteLine` statement). Otherwise the optimizer does some selective optimization, which changes with the timing.\n- @EdwardBrey, I have changed my 64 bit tests and got the same results as you are now. Corrected my answer to reflect.\n- @EdwardBrey, I can only reproduce it in 64 bit app\n- I got 1.86s (1st) and 1.38s (2nd) in x64, while in x86 I got around 2.09s for both methods.\n- I updated the test project on github to include x64 and x86 configurations. After doing a batch rebuild of both configurations and running each .exe outside VS2010 on two computers (both Core i5), I see a very different timings for Test1 and Test2 on both x64 and x86. Maciej and Marcus, to help eliminate variables, could you each grab the project and try it? (Github has a ZIP button to quickly grab a zip file of the project.)\n- @EdwardBrey Running your test gives me slower execution on Multiline version on both platforms. But if I change the test so that it runs 4xMultiline and then 4xSingleline there is no speed difference on x86 (x64 is unaffected)\n- @Maciej +1 for a great observation. It looks like which method gets called first is what matters. This appears to be almost certainly due to JIT order. But why JIT order matters is perplexing. I updated the question body with a link to the new question that your observation prompted.\n- @EdwardBrey Can you upvote my answer instead of the comment as it will show \"real\" appreciation :-)\n- @Maciej Sorry, but I don't think your answer really answers the question. It helped us find a confounding issue, namely that for x86, alignment penalties can cause well-optimized code to run as slowly as less-than-fully optimized code. But the question still remains, why are there cases where less-than-fully optimized x64 (and x86) code is generated in the first place?\n- If I had a dollar for every time a programmer told me, \"My code doesn't work. It must be a bug in the framework (or compiler or runtime library, etc.),\" and later found out that it was a bug in his own code, I could retire.\n- @Jim: I've seen it plenty of times myself. The best antidote I know is to isolate the behavior as fundamentally as possible and provide the vendor with a repro. And to keep a wait and see attitude. That's where we're at.\n- @TankorSmash Perhaps Microsoft deleted it as part of moving the code to GitHub. It's not on my Connect dashboard anymore. It seems that many issues I reported are gone. Some sort of notification would have been nice. I resubmitted the issue to the GitHub project and updated the answer accordingly.\n- I updated the repro project to include an \"if\" test. I made single- and multi-line variants. On both x64 and x86, the single-line version is faster (when there is no alignment penalty). I also made a variants that don't have any conditional code in the loop at all (just bit math). They're neck and neck on x86 (I haven't checked the assembly). On x64, the version with a local variable runs faster! It's still amazing that the local variable ever matters at all.\n- I'd be extremely wary of reading too much into articles from 2005 and .net 1.1! Quite a lot has changed in the last 7 years.","metadata":{"transformedAt":"2026-08-18T18:32:17.759Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":227,"estimatedTokens":3545}}114{"id":"stack-2178115","source":"stackoverflow","questionId":2178115,"title":"Are compilers allowed to eliminate infinite loops?","tags":["c","optimization","compiler-construction","standards","infinite-loop"],"text":"Title: Are compilers allowed to eliminate infinite loops?\nTags: c, optimization, compiler-construction, standards, infinite-loop\nSource: Stack Overflow\n\nQuestion:\nCan optimizing compiler delete infinite loops, which does not changes any data, like\n\n```\nwhile(1) \n /* noop */;\n```\n\nFrom analyzing a data flow graph compiler can derive, that such loop is \"dead code\" without any side effects.\n\nIs deleting of infinite loops prohibited by C90/C99 standards?\n\nDoes C90 or C99 standards permit compiler to deleting such loops?\n\nUpd: \"Microsoft C version 6.0 did essentially this optimization.\", see link by caf.\n\n```\nlabel: goto label;\nreturn 0;\n```\n\nwill be transformed to\n\n```\nreturn 0;\n```\n\n========================================\n\nTop Answer:\nThere's no way to detect infinite loops universally: see the Halting Problem. So the best any compiler could do is take a decent guess - for example the obvious case mentioned in the OP.\n\nBut why would this be desirable? I could see emitting a warning and still allowing the behavior, but to remove the loop is not an \"optimization\" - it changes the behavior of the program!\n\n========================================\n\nCode:\n```text\nwhile(1) \n  /* noop */;\n```\n\n```text\nlabel: goto label;\nreturn 0;\n```\n\n```text\nreturn 0;\n```\n\n```text\nwhile(1) \n  /* noop */;\n```\n\n```text\nfor (p = q; p != 0; p = p -> next) {\n    ++count;\n}\nfor (p = q; p != 0; p = p -> next) {\n    ++count2;\n}\n```\n\n```text\nfor (p = q; p != 0; p = p -> next) {\n        ++count;\n        ++count2;\n}\n```\n\n```text\nwhile(1) ;\nprintf( \"hello world\\n\" ) ;\n```\n\n```text\n6.8.5\n```\n\n```text\n157\n```\n\n```text\n5.1.2.3\n```\n\n```text\n5.1.2.3\n```\n\n```text\n1.10\n```\n\n```text\nN1528\n```\n\n```text\n5.1.2.4\n```\n\n```text\nN1528\n```\n\n```text\ncomp.lang.c\n```\n\n========================================\n\nComments:\n- It is busy-waiting, and daemons shall not use busy-waiting a lot. I call such construction \"dead\" in means of Data Flow. If statement does not change any variables, and does not contain side-effect, it can be eliminated by optimizing compiler.\n- Code after loop is not \"unreachable\", loop can be interrupted with signal, and there can be \"longjmp\" in signal handler.\n- \"Optimizing endless loops\" == \"how to make endless loops finish faster\"\n- In Embedded Systems, there are often *background loops* which may or may not perform processing while an interrupt service routine performs important processing. So the `while(1)` is not dead code, but a common implementation.\n- Updated my answer w.r.t. to C99.\n- Possible duplicate: How do I make an infinite empty loop that won’t be optimized away?\n- That would not be a really nice daemon since while(1); technically doesn't put that thread to sleep and as very processor intensive. Better would be something like while(1) Sleep(5000); or something similar\n- That is a side-issue. At any rate, `while (1)` is anything but *dead code* -- which was my point. And yes, I think I saw some code analysis tool report it as such -- an infinite loop.\n- But `while(1);` *is* essentially dead code -- it doesn't do anything useful except block the CPU. Much better to have something like: char exiting=0; while(!exiting) { exiting = process_event(); }\n- @TMN: I was talking about the generic case, when you do something inside the loop. I see where the comments come from now :-)\n- @TMN: Perhaps it would be helpful if the Standard were to define a macro called `__SIDE_EFFECT();` which would be equivalent to reading an arbitrary `volatile` variable and ignoring the result, except that the code generator would be allowed to omit the read if it could do so without affecting any other aspect of program behavior. In that case, `while(condition_yielding_true) __SIDE_EFFECT();` would be well-defined if `condition_yielding_true` was well-defined, without regard for whether the condition was recognizable as a constant.\n- The first rule of the halting problem is that nobody mentions the halting problem.\n- While it's impossible for arbitrary programs, it's certainly possible for sufficiently trivial loops such as this one, though.\n- Consider a function `unsigned normalize_lsb(unsigned x) { while (!(x & 1)) x>>=1; return x;}`. In most cases where the return value of the function is ignored, the program behavior that would result from replacing the function with a no-op would be acceptable, *even if `x` might be zero*`. Since proving the return value will be ignored would often be easy, while proving that`x` couldn't be zero, allowing a compiler to omit the loop in such cases would be a useful optimization *provided that optimizations were limited to deferring or skipping execution of the loop, rather than* ...\n- ...*allowing completely arbitrary behavior*. If the only way for programmers to ensure anything meaningful about program behavior in cases where `x` might be zero would be to either add code to skip the loop if `x` is zero, or add a dummy side effect within the loop, the net effect would be that programmers would have to write otherwise-unnecessary code whose performance, with the \"optimization\", would be worse than if the optimization didn't exist but the programmer didn't have to make accommodations for it.\n- Thanks! Can you find some more links to such threads in usenet?\n- You can belive even in God, but there is can be no ISO standard of the God behaviour. Are there C-like languages stadards which allow this?\n- @osgx: Consider the code \"int main(void) {do_something(); do_something_else(); return 0;}\" Suppose the compiler can look at the code and determine that do_something() does not write any volatile variable, nor does it write any variable that do_something_else() is ever going to use. The only thing do_something() could possibly do that would alter the output of do_something_else() would be to infinite-loop. If a compiler were only allowed to drop code which couldn't loop infinitely, it would be forced to include lots of useless code. Allowing infinite loops to be dropped makes things easier.\n- c99 5.1.2.3 \"Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations\" - it is the only side effects. They \"change the state of the execution environment\". \"An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced\"\n- @osgx: By my interpretation, all the confusing stuff about loops being assumed to terminate may be summed up as, \"the time required to execute a piece of code, even if infinite, shall not in and of itself be considered a side-effect that a compiler must maintain.\"\n- @osgx: Since writing the above, I have come to realize that a more accurate statement would have been \"The Standard could have been simplified, *and the semantics improved*, if the confusing stuff about loops being presumed to terminate were summed up [as shown]\". When I wrote the statement, I hadn't realized that the authors of any compiler people actually use would deliberately *seek out* cases where programs invoke Undefined Behavior and use them as excuses to negate laws of time and causality.\n- Does it mean that per \"implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced\" `while(1);` can be optimized out?\n- @pmor: Worse thant that: if clang can deduce that a loop couldn't exit unless some condition were true, and also that the loop has no side effects beyond the (possibly infinite) time to run it, and some later code evaluates the same condition, clang will optimize out *both* the loop and the later condition check.\n- It seems that the C11 wording implies that `while (1,1) &#47;* no-op *&#47;;` *can* be optimised out, since the introduction of a comma operator means that it's no longer a constant expression.\n- @caf I agree *Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators*\n- How about `goto` loops?\n- You seem to read 6.8.5 as having time consumption semantics. I don't find any such requirement in that section. I see nothing there requiring the abstract machine to expend infinite time on `while(1);`. Why do you believe the abstract machine is not permitted to complete that loop in finite time?\n- @EricTowers: For any number of seconds N, a program that always waits N+1 seconds and performs some visible action would not be observably different from one that waits forever without doing anything, nor from one that sometimes performs and action immediately and sometimes performs it after N+1 seconds. A program that performs any observable action outside a particular (possibly) empty set of \"next\" observable actions, however, would be observably different from a program whose actions are limited to those in the set.\n- @EricTowers: BTW, I wonder how many useful optimizations are facilitated by making assumptions about loops that wouldn't be better facilitated by having a means of qualifying functions or prototypes as \"pure\"?\n- Does it mean that per \"implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced\" `while(1);` can be optimized out?\n- @pmor: Code which follows an unconditional branch and is not the target of another branch is *statically unreachable*. Although it is in general difficult if not intractable to determine whether there are any inputs a program could receive that would result in a particular statically reachable piece of code actually being executed, classifying parts of the program as statically unreachable is trivial: code between an unconditional branch and the next branch target is statically unreachable, as is code between a call to a function with no reachable exits and the next branch target.\n- @supercat A simple question: are self-modifying programs / code out of scope of the C standard? Is it possible to write a strictly conforming self-modifying C program?\n- @pmor: The only means of having a program modify executable behavior which would not involve Undefined Behavior would be writing an executable file in binary mode and then using the `system` function to execute it. The behavior of the `system` function is Implementation-Defined in almost all cases, because while the actual behavior is outside the Standard's jurisdiction, it would have come across as silly to have a function whose behavior is never defined.\n- @pmor: Actually, I suppose another alternative would be to write a C source file in text mode, and then use the `system` function to invoke a C compiler on it, and then run the executable from that, but the main point is that the Standard says nothing about the effect of doing something like `system(\"cc foo.c\");` or `system(\".&#47;a.out\");`, but an execution environment might specify such things.","metadata":{"transformedAt":"2026-08-18T18:32:17.760Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":154,"estimatedTokens":2718}}115{"id":"stack-6241627","source":"stackoverflow","questionId":6241627,"title":"How do system calls work?","tags":["compiler-construction","process","operating-system","interrupt","system-calls"],"text":"Title: How do system calls work?\nTags: compiler-construction, process, operating-system, interrupt, system-calls\nSource: Stack Overflow\n\nQuestion:\nI understand that a user can own a process and each process has an address space (which contains valid memory locations, this process can reference). I know that a process can call a system call and pass parameters to it, just like any other library function. This seems to suggest that all system calls are in a process address space by sharing memory, etc. But perhaps, this is only an illusion created by the fact that in high level programming language, system calls look like any other function, when a process calls it.\n\nBut, now let me take a step deeper and analyze more closely on what happens under the hood. How does compiler compile a system call? It perhaps pushes the system call name and parameters supplied by the process in a stack and then put the assembly instruction say \"TRAP\" or something -- basically the assembly instruction to call a software interrupt.\n\nThis TRAP assembly instruction is executed by hardware by first toggling the mode bit from user to kernel and then setting the code pointer to say beginning of interrupt service routines. From this point on, the ISR executes in kernel mode, which picks up the parameters from the stack (this is possible, because kernel has access to any memory location, even the ones owned by user processes) and executes the system call and in the end relinquishes the CPU, which again toggles the mode bit and the user process starts from where it left off.\n\nIs my understanding correct?\n\nAttached is rough diagram of my understanding:\n\n========================================\n\nTop Answer:\nYes, you've got it pretty much right. One detail though, when the compiler compiles a system call, it will use the *number* of the system call rather than the *name*. For example, here is a list of Linux syscalls (for an old version, but the concept is still the same).\n\n========================================\n\nCode:\n```text\nstatic int do_getname(const char __user *filename, char *page)\n{\n    int retval;\n    unsigned long len = PATH_MAX;\n\n    if (!segment_eq(get_fs(), KERNEL_DS)) {\n        if ((unsigned long) filename >= TASK_SIZE)\n            return -EFAULT;\n        if (TASK_SIZE - (unsigned long) filename < PATH_MAX)\n            len = TASK_SIZE - (unsigned long) filename;\n    }\n\n    retval = strncpy_from_user(page, filename, len);\n    if (retval > 0) {\n        if (retval < len)\n            return 0;\n        return -ENAMETOOLONG;\n    } else if (!retval)\n        retval = -ENOENT;\n    return retval;\n}\n```\n\n```text\ngetpid(2)\n```\n\n```text\nchdir(2)\n```\n\n```text\nINT 0x80\n```\n\n```text\nSYSENTER\n```\n\n```text\nperl\n```\n\n```text\nsyscall()\n```\n\n```text\nget_fs()\n```\n\n```text\n4:4\n```\n\n```text\nmkdir()\n```\n\n```text\nmkdir\n```\n\n```text\nsyscall()\n```\n\n```text\nINTERNAL_SYSCALL\n```\n\n```text\nsysdeps/unix/sysv/linux/i386/sysdep.h\n```\n\n```text\nsyscall\n```\n\n```text\nsysdeps/unix/sysv/linux/i386/sysdep.S\n```\n\n```text\nsysdeps/unix/sysv/linux/i386/sysdep.h\n```\n\n```text\nENTER_KERNEL\n```\n\n```text\n0x80\n```\n\n```text\nlinux-gate.so\n```\n\n```text\n#include < stdio.h  >    \n#include < stdlib.h >    \nint main()    \n{    \n    printf(\"Running ps with \"system\" system call \");    \n    system(\"ps ax\");    \n    printf(\"Done.\\n\");    \n    exit(0);    \n}\n```\n\n```text\nread_from_file\n```\n\n```text\nINT 0x80\n```\n\n========================================\n\nComments:\n- Could you please elaborate on what is \"C *runtime* library\". Also, a user program is allowed to call system calls directly without going through any library, is that correct?\n- @p2pnode: the C runtime library is a library that C programs are normally linked against, in Unix it's usually called `libc`. And yes, programs can call system calls directly.\n- so if programs *can* call system calls directly, how would these calls be compiled? Here it seems, the C runtime library would have *no* role to play..?\n- @p2pnode: you'd write inline asm to call the system call.\n- On TLBs with ASIDs, flushing shouldn't be necessary. But otherwise, a really comprehensive answer.\n- @ninjalj, ooh, that'd help immensely. Let me guess, they're mostly available on PAE systems or 64 bit platforms? :)\n- they're available on MIPS at least.\n- Aha! That's the missing link I've spent half an hour looking for. :D","metadata":{"transformedAt":"2026-08-18T18:32:17.760Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":23,"totalLines":154,"estimatedTokens":1086}}116{"id":"stack-185836","source":"stackoverflow","questionId":185836,"title":"Equivalent of Class Loaders in .NET","tags":[".net","compiler-construction","programming-languages","clr","language-features"],"text":"Title: Equivalent of Class Loaders in .NET\nTags: .net, compiler-construction, programming-languages, clr, language-features\nSource: Stack Overflow\n\nQuestion:\nDoes anyone know if it possible to define the equivalent of a \"java custom class loader\" in .NET?\n\n**To give a little background:**\n\nI am in the process of developing a new programming language that targets the CLR, called \"Liberty\". One of the features of the language is its ability to define \"type constructors\", which are methods that are executed by the compiler at compile time and generate types as output. They are sort of a generalization of generics (the language does have normal generics in it), and allow code like this to be written (in \"Liberty\" syntax):\n\n```\nvar t as tuple;\nt.i = 2;\nt.j = 4;\nt.k = 5;\n```\n\nWhere \"tuple\" is defined like so:\n\n```\npublic type tuple(params variables as VariableDeclaration[]) as TypeDeclaration\n{\n //...\n}\n```\n\nIn this particular example, the type constructor `tuple` provides something similar to anonymous types in VB and C#.\n\nHowever, unlike anonymous types, \"tuples\" have names and can be used inside public method signatures.\n\nThis means that I need a way for the type that eventually ends up being emitted by the compiler to be shareable across multiple assemblies. For example, I want\n\n`tuple` defined in Assembly A to end up being the same type as `tuple` defined in Assembly B.\n\nThe problem with this, of course, is that Assembly A and Assembly B are going to be compiled at different times, which means they would both end up emitting their own incompatible versions of the tuple type.\n\nI looked into using some sort of \"type erasure\" to do this, so that I would have a shared library with a bunch of types like this (this is \"Liberty\" syntax):\n\n```\nclass tuple\n{\n public Field1 as T;\n}\n\nclass tuple\n{\n public Field2 as T;\n public Field2 as R;\n}\n```\n\nand then just redirect access from the i, j, and k tuple fields to `Field1`, `Field2`, and `Field3`.\n\nHowever that is not really a viable option. This would mean that at compile time `tuple` and `tuple` would end up being different types, while at runtime time they would be treated as the same type. That would cause many problems for things like equality and type identity. That is too leaky of an abstraction for my tastes. \n\nOther possible options would be to use \"state bag objects\". However, using a state bag would defeat the whole purpose of having support for \"type constructors\" in the language. The idea there is to enable \"custom language extensions\" to generate new types at compile time that the compiler can do static type checking with.\n\nIn Java, this could be done using custom class loaders. Basically the code that uses tuple types could be emitted without actually defining the type on disk. A custom \"class loader\" could then be defined that would dynamically generate the tuple type at runtime. That would allow static type checking inside the compiler, and would unify the tuple types across compilation boundaries.\n\nUnfortunately, however, the CLR does not provide support for custom class loading. All loading in the CLR is done at the assembly level. It would be possible to define a separate assembly for each \"constructed type\", but that would very quickly lead to performance problems (having many assemblies with only one type in them would use too many resources).\n\n**So, what I want to know is:**\n\nIs it possible to simulate something like Java Class Loaders in .NET, where I can emit a reference to a non-existing type in and then dynamically generate a reference to that type at runtime before the code the needs to use it runs?\n\n**NOTE:**\n\n*I actually already know the answer to the question, which I provide as an answer below. However, it took me about 3 days of research, and quite a bit of IL hacking in order to come up with a solution. I figured it would be a good idea to document it here in case anyone else ran into the same problem. *\n\n========================================\n\nTop Answer:\nI think this is the type of thing the DLR is supposed to provide in C# 4.0. Kind of hard to come by information yet, but perhaps we'll learn more at PDC08. Eagerly waiting to see your C# 3 solution though... I'm guessing it uses anonymous types.\n\n========================================\n\nCode:\n```text\nvar t as tuple<i as int, j as int, k as int>;\nt.i = 2;\nt.j = 4;\nt.k = 5;\n```\n\n```text\npublic type tuple(params variables as VariableDeclaration[]) as TypeDeclaration\n{\n   //...\n}\n```\n\n```text\nclass tuple<T>\n{\n    public Field1 as T;\n}\n\nclass tuple<T, R>\n{\n    public Field2 as T;\n    public Field2 as R;\n}\n```\n\n```text\ntuple\n```\n\n```text\ntuple<x as int>\n```\n\n```text\ntuple<x as int>\n```\n\n```text\nField1\n```\n\n```text\nField2\n```\n\n```text\nField3\n```\n\n```text\ntuple<x as int>\n```\n\n```text\ntuple<y as int>\n```\n\n```text\nusing System;\nusing System.Collections.Generic;\nusing System.Reflection;\nusing System.Reflection.Emit;\n\nnamespace SharedLib\n{\n    public class Loader\n    {\n        private Loader(ModuleBuilder dynamicModule)\n        {\n            m_dynamicModule = dynamicModule;\n            m_definedTypes = new HashSet<string>();\n        }\n\n        private static readonly Loader m_instance;\n        private readonly ModuleBuilder m_dynamicModule;\n        private readonly HashSet<string> m_definedTypes;\n\n        static Loader()\n        {\n            var name = new AssemblyName(\"$Runtime\");\n            var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);\n            var module = assemblyBuilder.DefineDynamicModule(\"$Runtime\");\n            m_instance = new Loader(module);\n            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);\n        }\n\n        static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)\n        {\n            if (args.Name == Instance.m_dynamicModule.Assembly.FullName)\n            {\n                return Instance.m_dynamicModule.Assembly;\n            }\n            else\n            {\n                return null;\n            }\n        }\n\n        public static Loader Instance\n        {\n            get\n            {\n                return m_instance;\n            }\n        }\n\n        public bool IsDefined(string name)\n        {\n            return m_definedTypes.Contains(name);\n        }\n\n        public TypeBuilder DefineType(string name)\n        {\n            //in a real system we would not expose the type builder.\n            //instead a AST for the type would be passed in, and we would just create it.\n            var type = m_dynamicModule.DefineType(name, TypeAttributes.Public);\n            m_definedTypes.Add(name);\n            return type;\n        }\n    }\n}\n```\n\n```text\n.assembly extern $Runtime\n{\n    .ver 0:0:0:0\n}\n.assembly extern SharedLib\n{\n    .ver 1:0:0:0\n}\n```\n\n```text\n.method privatescope specialname rtspecialname static \n        void  .cctor() cil managed\n{\n    //generate any constructed types dynamically here...\n}\n```\n\n```text\nclass Tuple_i_j<T, R>\n{\n    public T i;\n    public R j;\n}\n\nclass Tuple_x_y_z<T, R, S>\n{\n    public T x;\n    public R y;\n    public S z;\n}\n```\n\n```text\nvar loader = SharedLib.Loader.Instance;\nlock (loader)\n{\n    if (! loader.IsDefined(\"$Tuple_i_j\"))\n    {\n        //create the type.\n        var Tuple_i_j = loader.DefineType(\"$Tuple_i_j\");\n        //define the generic parameters <T,R>\n       var genericParams = Tuple_i_j.DefineGenericParameters(\"T\", \"R\");\n       var T = genericParams[0];\n       var R = genericParams[1];\n       //define the field i\n       var fieldX = Tuple_i_j.DefineField(\"i\", T, FieldAttributes.Public);\n       //define the field j\n       var fieldY = Tuple_i_j.DefineField(\"j\", R, FieldAttributes.Public);\n       //create the default constructor.\n       var constructor= Tuple_i_j.DefineDefaultConstructor(MethodAttributes.Public);\n\n       //\"close\" the type so that it can be used by executing code.\n       Tuple_i_j.CreateType();\n    }\n}\n```\n\n```text\nSystem.Reflection.Emit\n```\n\n```text\nSystem.AppDomain\n```\n\n```text\nMain\n```\n\n```text\nSystem.AppDomain\n```\n\n```text\nTypeResolve\n```\n\n```text\ntuple<i as int, j as int>\n```\n\n```text\ntuple<x as double, y as double, z as double>\n```\n\n```text\ntuple<x as Foo>\n```\n\n========================================\n\nComments:\n- Ugh, how is your module constructor definition different from the ordinary class constructor? Is the difference in using `privatescope` as opposed to `private hidebysig`?\n- Ahh, just figured it out. No difference except the module cctor is not placed in any particular type. Didn't know you could even do that :)","metadata":{"transformedAt":"2026-08-18T18:32:17.760Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":24,"totalLines":299,"estimatedTokens":2154}}117{"id":"stack-584507","source":"stackoverflow","questionId":584507,"title":"What is the purpose of null?","tags":["language-agnostic","compiler-construction","null","language-design"],"text":"Title: What is the purpose of null?\nTags: language-agnostic, compiler-construction, null, language-design\nSource: Stack Overflow\n\nQuestion:\nI am in a compilers class and we are tasked with creating our own language, from scratch. Currently our dilemma is whether to include a 'null' type or not. What purpose does null provide? Some of our team is arguing that it is not strictly necessary, while others are pro-null just for the extra flexibility it can provide.\n\nDo you have any thoughts, especially for or against null?\nHave you ever created functionality that required null?\n\n========================================\n\nTop Answer:\n`null` is a sentinel value that is not an integer, not a string, not a boolean - not anything really, except something to hold and be a \"not there\" value. Don't treat it as or expect it to be a 0, or an empty string or an empty list. Those are all valid values and can be geniunely valid values in many circumstances - the idea of a null instead means there is *no* value there.\n\nPerhaps it's a little bit like a function throwing an exception instead of returning a value. Except instead of manufacturing and returning an ordinary value with a special meaning, it returns a special value that already has a special meaning. If a language expects you to work with `null`, then you can't really ignore it.\n\n========================================\n\nCode:\n```text\ngetline()\n```\n\n```text\n((void *)0)\n```\n\n```text\nnull\n```\n\n```text\nnull\n```\n\n```text\nnull\n```\n\n```text\nnull\n```\n\n```text\nswitch(x)\n{\n    case 1:\n    case 2:\n        foo;\n        break;\n}\n```\n\n```text\nswitch (x)\n{\n    case 1:\n        foo();\n    case 2:\n        bar();\n        break;\n}\n```\n\n```text\nswitch (x)\n{\n    case 1:\n        foo();\n        goto case 2;\n    case 2:\n        bar();\n        break;\n}\n```\n\n```text\nvoid\n```\n\n```text\nNULL\n```\n\n```text\nMaybe\n```\n\n```text\nnull\n```\n\n```text\nUndefinedValue\n```\n\n```text\nnull\n```\n\n```text\nnull\n```\n\n```text\n#toString\n```\n\n```text\n#equals\n```\n\n========================================\n\nComments:\n- Thanks for making the title make sense, Patrick.\n- possible duplicate of stackoverflow.com/questions/178026/&hellip;\n- It depends on how your language will be used. I can't imagine C without it.\n- Would be nice if they came up with some sort of non-nullable idenfier. In C# they recently added nullable types which you can declare as MyType? ...would be nice if you could identify method parameters to never be null...ie something like MyType! or whatever...\n- I've always been a fan of the Null Object Pattern.\n- @moffdub I think you should copy the (Tony Hoare's) abstract to the answer as well.\n- Not having null would have been a TEN billion dollar mistake. Think about it: all pointers would have to be initialized to some 'valid' value, which when encountered would cause the program to happily keep on computing *in the wrong context*, leading to extremely hard-to-find bugs = worse than crash\n- The mistake with null is that it forces you to constantly check if something is null every five lines in your code. It takes you away from the problem domain and back into tedium. And if you aren't meticulous enough... crash.\n- @Steven A. Lowe: That is simply not true. Take a look at Haskell:s Maybe type. It reminds of a null but it's explicit rather than implicit whether a specific variable may or may not contain a value. This often leads to better design and should be easy for the compiler to remove when optimizing.\n- With all due respect to Dr Hoare, I call hubris. ;-) Machine-language programmers invented the null reference the first time they set a pointer to zero.\n- I'm curious what Dr. Hoare would have had as the default value for unassigned pointers. While it would be possible to have pointers default to a trap representation (in which case one would be forbidden from even reading an unitialized pointer to find out if it had been written), having a null pointer value which won't cause an error until it is actually used would seem, in many cases, to be much more practical. The ability to assign a pointer a value which can be examined but not used is certainly useful in many contexts; certainly not a mistake in my book.\n- @StevenA.Lowe, is it a ten billion dollar mistake, then, that primitive types cannot be null?\n- @dainichi: probably. uninitialized variables can have random values (especially in older languages) which can likewise wreak havoc\n- @StevenA.Lowe, you are probably right that uninitialized variables can be necessary in imperative languages where it's a lesser evil than meaningless default values. But uninitialized variables can still be handled much more gracefully, i.e. runtime systems (and to some extent compilers) can detect uninitialized use of variables (done for primitively typed local variables in Java). But allowing them to be passed around like e.g. Java does is a huge hole in the type system and not really defensible in a high-level language.\n- A linked list could be build like so: Node->Node->Node->EndNode In fact, that's how lists are build in languages that don't have `null` values.\n- i would keep it too, indeed. i mean what else would you assign to a reference that point to nothing? (note the pun)\n- Well it's a question that's based on a wrong assumption. If you need to assign a reference that points at nothing, you've built your application incorrectly.\n- Brenton: So how would you implement a callback from a class? You have to store a pointer to a callback function in the class but what value would if have before you call setCallback()?\n- nullable types were added to .NET for because they are necessary to interact with applications or other languages that inherently support it such as databases.\n- `null` is a hack. Look at how typed functional languages use the `option` or `Maybe` type to accomplish the same thing in a more principled way.\n- Isn't it better to get and catch a \"NullPointerException\" instead of pointing to something that looks like legal values but in fact is not?\n- As simon said, a NullPointerException is an argument *for* null, not against it.\n- Added the clarification based on the comments. If someone wants to indicate \"this parameter hasn't been set to a useful value\", I'd rather have that come through explicitly. \"null\" by itself doesn't tell me anything.\n- But null literally means \"this hasn't been set to a useful value\".\n- No, it means \"there isn't anything at the end of this reference. If you it, you'll die.\" It does not contain useful default values ala an NullEmployee named \"NullName\" with SSN of \"000-00-0000\" that is clearly unpopulated yet won't crash your application.\n- it won't crash your application if you check for the null either. You forget to check, that's your fault for shoddy programming. If you really want default values, you check for null and then change it.\n- Repeatedly checking for null is a potentially sensible thing to do with an API that was flung at you from over the wall by a vendor. When you're working with a team of people, it doesn't make sense. The multiplication of code is on the wrong side of the equation. If my friend checks in a method returns an ArrayList of valid values, I assume that I'm going to receive an empty ArrayList if there aren't any valid results. I don't expect to get a null.\n- I disagree - without nullable primitives you often end up with a disconnect between systems that treat anything as nullable and your non-null primitives end up with boolean flags to indicate yes this number means something versus no, actually it doesn't. Or you end up with special sentinel values.\n- Yeah, but thats what System.Nullable<> is for. Which is basically automatic implementation of the boolean flag.\n- Good answer to not rely on the value being zero, because it doesn't have to be. You could add that historically, for performance, compilers will often use the value 0 as null pointer value because testing against 0 (or not 0) is typically a single machine instruction.\n- \"Don't equate it with a 0, or an empty string or an empty list.\" But also do not place too much emphasis on them being different. While you might be technically right, avoiding the subtle and confusing details is way safer. I kind of like the way Oracle treats empty strings as null.\n- I prefer null and \"\" to be distinct. If I'm checking for a null, I usually don't want \"\" to match. That Oracle gotcha is dangerous.\n- @statiscan I'm not away of any lang that equates null to \"\", aka the empty string.\n- MySQL will convert null to \"\" in some circumstances.\n- Your assertion is only true if and only if all variables can be assigned a value. No special values (aka sentinels) either. Mathematicians have long ago moved beyond the natural numbers. bool isCompliant (yes/no) or Nullable isCompliant (yes/no/unknown). Nulls more accurately model real life.\n- Null is the identity of what opeartor, exactly?\n- @[Norman Ramsey]: the question assumes an invalid premise. Null is not an element in a series, it is a sentinel meaning 'no value'.\n- @Norman: Though I agree that `null` isn't as fundamental as zero, many languages have an operator for which `null` is the identity element: en.wikipedia.org/wiki/Null_coalescing_operator\n- This seems a better answer to the question of whether to allow empty lists than the one about whether to have a null value.\n- Indeed, without a null, more boolean flags would to be used to indicate that \"The value that has been set to zero actually isn't a value at all\".\n- You had a philosophy major inside you? How the hell did that get there!?\n- Actually, zero generally *has* been defined as the empty set, since Frege, though this probably doesn't matter much to computing. (Frege defined it as the set containing the empty set. The definition of larger numbers has been less constant.)\n- Interesting...I remember the prof who taught my Formal Logic class saying exactly \"...zero is the set containing the empty set.\" That lead me to think my initial statement is correct; the empty set itself is not zero, like NULL != 0.\n- I don't know if this makes sense on some philosophical level, but if it does on a theoretical/practical level, you need to explain better. In, say, Java, null is different from an empty set, and while emptySet.size() returns 0, null.size() throws a null pointer exception.\n- I'm not sure what I need to explain better. I confess that I included a comment on the origins of the concept of null simply as a commentary. Following this, I gave my opinion on one reason null is useful in programming languages.\n- I think you are totally correct but totally didn't answer the question. He is asking about writing a compiler not an application.\n- Including a null in the language widens the domain of possible programs that can be written in a langauge to include a certain class of program that is not particularly well thought out. I think this is relevant information for someone who is designing a new language.\n- No certainly not. At least when you get a null pointer exception or a segfault it's a painful reminder you're doing something wrong. Doing a backflip to silence the exception doesn't mean you're not still doing something wrong.\n- Thats not correct. Null represents 'Nothing', while 0 is a valid 'Not Nothing' value.\n- I always understood it as 'the absence of value' a la Stand and Deliver. I am in no way saying that null == 0, I'm saying that in the numbers domain, when you have no value it is represented by null. In the objects domain, the absence of value is null. I guess I was trying to be too bold ;)\n- @ Craig - except when you're dividing by it.\n- I wouldn't quite say null is the object reference's equivalent of 0 but I can kind of see the connection... e.g. when making measurements, 0 represents the absence of any measurable effect.\n- When an array of a reference type is created, with what should the array elements be initially populated? While one could define a language in such a way that an array as a whole could not be accessed until a constructor had been run for each element, there are many circumstances where that really wouldn't work very well; e.g. one has an array `Src` and a permutation mapping `Permute`, and wishes to create a new array `Dest` such that `Dest[Permute[i]]=src[i]`. If `Dest` is pre-filled with `null`, an error in `Permute[]` is less likely to go undetected than...\n- ...if the compiler insists on being able to determine every element's value before the array as a whole can be accessed. One might plausibly argue that the performance costs would be outweighed by the advantages of a non-nullable type, but having a standard default value for every type [note that a variable of reference type which holds `null` has a valid *value*, even though that values doesn't identify any object] is extremely valuable in its own right.\n- The argument against this though, is you return 3 possible values, One for yes, one for no, and one for don't know. But in reality, when coding with it, if (a == dontKnow) makes no difference to if (a == null) right after obtaining the value. Except maybe a different thought process when directly reading the code.\n- People making assumptions is the reason for bugs with `null`. Assuming a reference cannot be `null` without checking it either explicitly in code or checking the documentation or code where the reference came from is the real issue -- its an edge case. Just like a negative length, distance or age is an edge case. You need to make sure that can't happen (by disallowing it in a constructor for example) or program defensively against it.","metadata":{"transformedAt":"2026-08-18T18:32:17.760Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":18,"totalLines":172,"estimatedTokens":3399}}118{"id":"stack-2349468","source":"stackoverflow","questionId":2349468,"title":"Starting off a simple (the simplest perhaps) C compiler?","tags":["c","compiler-construction","programming-languages"],"text":"Title: Starting off a simple (the simplest perhaps) C compiler?\nTags: c, compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nI came across this: Writing a compiler using Turbo Pascal\n\nI am curious if there are any tutorials or references explaining how to go about creating a simple C compiler. I mean, it is enough if it gets me to the level of making it understand arithmetic operations. I became really curious after reading this article by Ken Thompson. The idea of writing something that understands itself seems exciting. \n\nWhy did I put up this question instead of asking Google? I tried Google and the Pascal one was the first link. The rest did no seem relevant and added to that... I am not a CS major (so I still need to learn what all those tools like yacc do) and I want to learn this by doing and am hoping people with more experience are always better at these things than Google. I want to read some article written in the same spirit as the one I listed above but that which highlights at least the bootstrapping phases of building a simple C compiler.\n\nAlso, I don't know the best way to learn. Do I start off building a C compiler in C or some other language? Do I write a C compiler or some other language? I feel questions like this are better answered once I have some direction to explore. Any suggestions?\n\nAny suggestions?\n\n========================================\n\nTop Answer:\nI advise you this tutorial:\n\n- LLVM tutorial\n\nIt is a small example on how to implement a \"small language\" compiler. The source code is very small and is explained step by step.\n\nThere is also the C front end library for the LLVM (Low Level Virtual Machine which represent the internal structure of a program) library:\n\n- Clang\n\n========================================\n\nCode:\n```text\ni = 0;\n```\n\n========================================\n\nComments:\n- well trying to create a C (even a subset) compiler with the beginner flag is a little bit surprising. You should first try to something more simple first.\n- Writing your own compiler can be a lot of fun. But consider getting that CS degree first. Gotta have the weapons to slay the dragon.\n- @Phong: Beginner in the area of compilers :) Have a couple of years of programming experience as such... but all the time I've been doing things without knowing how they actually \"work\". @nobugz: Its on my list :) Just that nothing really prevents me from doing it on my own first.\n- The canonical compiler references question is stackoverflow.com/questions/1669/learning-to-write-a-compile&zwnj;&#8203;r . BTW-- I am a big fan of the Crenshaw compiler as a first introduction. It is no trouble to translate pascal into c, so feel free to just work it. If you're going to stick with compilers you'll want a more complete reference pretty soon, though.\n- Yes looks like I will start off with Crenshaw first and then refer to the beast link here on SO..\n- @nobugz: You don't need a CS degree in order to be a capable programmer. I'm not saying the classes don't help though ;-)\n- I once wrote a toolkit (in Python) for visualizing your parse tree and instruction generation (for a compiler design course in which we were creating a language which was a tiny subset of C, but the toolkit isn't limited to that minilanguage in any way). Uploaded it to sourceforge under the name DTCT (Duff's Toy Compiler Tools). I'm pretty sure they took it down after several years of inactivity (it's nearing a decade now), but might be available somewhere.\n- Also see my SmallerC, which is not Small C, but similar in spirit.\n- @Legend This is part of Imagist's answer. `Parsers and compilers are two separate problems. For example I might write a compiler in C, but I would never write a parser in C (I would use a parser generator). For very simple parsers where speed isn't a high priority, I might hand-code the parser in Perl or Python, which have good text-manipulation facilities.`\n- Awesome! Thank you very much... That thread seems like a beast by itself.. Will start digging into it...\n- +1 for ANTLR. It might not be the best parser generator but the debugging and testing tools are hard to top.\n- @duffymo: thanks, What I really liked on this tutorial is that they dont rely on any external soft for the lexer/parser function.\n- Yes, it's really terrific. Nice find. Thanks for posting it.\n- Sorry just trying to understand... Are you suggesting writing something using the higher level languages like Python or Ruby? If yes, sure, I am open to trying that as well.. As I mentioned, I am a complete n00b in this area so anything that makes me understand the main concept is welcome... Also could you clarify the resume part? :D\n- A non-trivial program in one of those modern languages will look good on your resume. It would be substantially easier to implement a compiler in something like Ruby or Python. Haskell would be by no means easy, but it might lead you to a much better compiler.\n- Understood... I've only heard of Haskell... Never tried it though. Writing one in Python or Ruby never occurred to me. If that's the case, I'll definitely look into it. Thanks\n- Can we skip the *which language is better* contest? As much as I personally enjoy Perl or Python, the OP said C was the desired language.\n- Agreed with mctylr, but at least my background makes a functional language look like a much better \"host\" than Python. Compilers involve building structures quickly and carefully, and the \"easy\" languages get too hairy when it comes to things like identifier bindings and the underlying meaning of their builtin data structures. Also disagreeing on the Agile part. For a beginner project, this should be one person working one step at a time. *Do* save your test cases. *Don't* obsess over your plan.\n- @Potatoswatter, regarding Agile, I realize he is just one guy so the team parts of Agile won't apply. But I think it's very important that he incrementally prototype the program. He really needs to have something trivial running, then something slightly larger, then slightly larger. If he tries to really write a compiler and then start testing he will be totally doomed.\n- @Legend: and by the way, don't listen to all the naysayers. You probably aren't going to make a breakthrough or even a competitive language, but neither would someone *with* a degree on their first attempt. So long as you're having fun, you'll always find something more to achieve.\n- So true... Sometimes I do get nervous looking at the 'real' CS grads... In fact, a recent post at slashdot: ask.slashdot.org/comments.pl?sid=10/02/19/147251 made me think more. But at the end of the day, I guess it all depends on how we think... :)\n- It took me quite a while to digest what you've written. Very informative post. Thank you for your time...\n- Thank you, I am glad you found it useful. I hope I can answer your question in a manner that is helpful, and encourages you to be successful.\n- Isn't the original article (Schorre's) behind ACM's pay-wall? If so, please note it. I happen to be an ACM member, but not everyone is.\n- I'm just not sure if I can post a link here but I'm sure alternate versions are available through Google Scholar... Thanks a lot Ira Baxter.\n- A link to an (public) ACM Queue article about following the META II paper using Python. META II: Digital Vellum in the Digital Scriptorium by Dave Long (Jan 2015, Vol 13, Issue 1).\n- @mctylr: His article on MetaII, is longer than the MetaII paper itself :-}\n- Oh... I think I have that book with me somewhere..! Thanks\n- I wouldn't call CIL a C compiler. CIL consists of a parser that reads C files and a writer that outputs equivalent C files. It does help with code analysis and transformation.\n- I don't think I did call CIL a compiler. It's a front end. With C, that's a \"big chunk\", as I said.\n- I know I did not really ask this question but you pretty much gave me a solution to a series of questions that I was about to ask in the future :) Looks like a really interesting book. Ordered it yesterday... just want to see how it feels to start from the roots... Thank again...\n- This book looks like what I've wanted for a very long time. I'm partially through a MS in CS, but I have no undergrad in CS and know I am lacking tons of low level knowledge. This books looks like an excellent place to start. Thanks.\n- @The111 - also see this... joelonsoftware.com/navLinks/fog0000000262.html","metadata":{"transformedAt":"2026-08-18T18:32:17.760Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":71,"estimatedTokens":2105}}119{"id":"stack-10617215","source":"stackoverflow","questionId":10617215,"title":"How to write your own code generator backend for gcc?","tags":["c","gcc","compiler-construction"],"text":"Title: How to write your own code generator backend for gcc?\nTags: c, gcc, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have created my very own (very simple) byte code language, and a virtual machine to execute it. It works fine, but now I'd like to use gcc (or any other freely available compiler) to generate byte code for this machine from a normal c program. So the question is, how do I modify or extend gcc so that it can output my own byte code? Note that I do NOT want to compile my byte code to machine code, I want to \"compile\" c-code to (my own) byte code.\n\nI realize that this is a potentially large question, and it is possible that the best answer is \"go look at the gcc source code\". I just need some help with how to get started with this. I figure that there must be some articles or books on this subject that could describe the process to add a custom generator to gcc, but I haven't found anything by googling.\n\n========================================\n\nTop Answer:\nI am busy porting gcc to an 8-bit processor we design earlier. I is kind of a difficult task for our machine because it is 8-bit and we have only one accumulator, but if you have more resources it can became easy. This is how we are trying to manage it with gcc 4.9 and using cygwin:\n\n- Download gcc 4.9 source\n\n- Add your architecture name to `config.sub` around line 250 look for `# Decode aliases for certain CPU-COMPANY combinations.` In that list add `| my_processor \\`\n\n- In that same file look for `# Recognize the basic CPU types with company name.` add yourself to the list: `| my_processor-* \\`\nSearch for the file `gcc/config.gcc`, in the file look for case ${target} it is around line 880, add yourself in the following way:\n\n```\n;;\nmy_processor*-*-*)\n c_target_objs=\"my_processor-c.o\"\n cxx_target_objs=\"my_processor-c.o\"\n target_has_targetm_common=no\n tmake_file=\"${tmake_file} my_processor/t-my_processor\"\n ;;\n```\n\n- Create a folder `gcc-4.9.0\\gcc\\config\\my_processor`\n\n- Copy files from an existing project and just edit it, or create your own from scratch. In our project we had copied all the files from the msp430 project and edited it all\nYou should have the following files (not all files are mandatory):\n\n- `my_processor.c`\n\n- `my_processor.h`\n\n- `my_processor.md`\n\n- `my_processor.opt`\n\n- `my_processor-c.c`\n\n- `my_processor.def`\n\n- `my_processor-protos.h`\n\n- `constraints.md`\n\n- `predicates.md`\n\n- `README.txt`\n\n- `t-my_processor`\n\n- create a path `gcc-4.9.0/build/object`\n\n- run `../../configure --target=my_processor --prefix=path for my compiler --enable-languages=\"c\"`\n\n- make\n\n- make install\n\n- Do a lot of research and debugging.\n\n- Have fun.\n\n========================================\n\nCode:\n```text\nmake compile test\n```\n\n```text\n;;\nmy_processor*-*-*)\n  c_target_objs=\"my_processor-c.o\"\n  cxx_target_objs=\"my_processor-c.o\"\n  target_has_targetm_common=no\n  tmake_file=\"${tmake_file} my_processor/t-my_processor\"\n  ;;\n```\n\n```text\nconfig.sub\n```\n\n```text\n# Decode aliases for certain CPU-COMPANY combinations.\n```\n\n```text\n| my_processor \\\n```\n\n```text\n# Recognize the basic CPU types with company name.\n```\n\n```text\n| my_processor-* \\\n```\n\n```text\ngcc/config.gcc\n```\n\n```text\ngcc-4.9.0\\gcc\\config\\my_processor\n```\n\n```text\nmy_processor.c\n```\n\n```text\nmy_processor.h\n```\n\n```text\nmy_processor.md\n```\n\n```text\nmy_processor.opt\n```\n\n```text\nmy_processor-c.c\n```\n\n```text\nmy_processor.def\n```\n\n```text\nmy_processor-protos.h\n```\n\n```text\nconstraints.md\n```\n\n```text\npredicates.md\n```\n\n```text\nREADME.txt\n```\n\n```text\nt-my_processor\n```\n\n```text\ngcc-4.9.0/build/object\n```\n\n```text\n../../configure --target=my_processor --prefix=path for my compiler --enable-languages=\"c\"\n```\n\n========================================\n\nComments:\n- Go look at LLVM. GCC is known for being hard to extend.\n- this guide could help you: drdobbs.com/retargeting-the-gnu-c-compiler/184401529 “ An inside look on how to deploy the GNU C compiler to your platform of choice.” — a pretty good read, though I did not yet try to it.\n- I did exactly this some time around 1990-91. The only thing I had to amend in the RTL was the location of the return value when it is floating-point. Very simple. Took longer to build than to implement.\n- Some very practical advice here :) Thanks! Gcc seems to be a fearsome beast. I'll certainly take a look at LLVM as well. This project seems larger than I originally thought, but I'll give it a shot...\n- @pearcoding On which ground do you claim LLVM is easier to port than gcc? Did you attempt to port both and came to this conclusion, or did you use someone else's conclusion? If the latter, reference please. Thanks.\n- This answer deserves more upvotes. Just out of curiousity, on what architecture did you try to port gcc? How well did you success? Did you also port binutils, or did you use an external assembler instead? How could you test full correct behaviour of the emitted code? Thanks.\n- @Bregalad we cross compiled from Linux to our own architecture, a very simple instruction set we designed. So the compiler lived on some Linux server. We abounded the project but learnt a lot. If I do it over I will spend more time learning gcc internals. We did not port binutils at all, but build our own assembler. Our archirecture was simple enough to take this route. I am sure there are better methods for testing emitted code, but we only evaluated the generated assembly code manually.\n- A lot dated, but a *good* answer. See also GCC's webpage: \"Extending and Contributing to GCC\", Wikipedia: \"GCC - Back-end\", and the newest answers that a search would turn up; as over the years these type of Q&A's go out of date at least bi-annually.","metadata":{"transformedAt":"2026-08-18T18:32:17.761Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":22,"totalLines":178,"estimatedTokens":1421}}120{"id":"stack-631059","source":"stackoverflow","questionId":631059,"title":"Was C# compiler written in C++?","tags":["c#",".net","compiler-construction","roslyn"],"text":"Title: Was C# compiler written in C++?\nTags: c#, .net, compiler-construction, roslyn\nSource: Stack Overflow\n\nQuestion:\nWas C# compiler written in C++?\n\n========================================\n\nTop Answer:\nYes.\n\nThe Mono C# compiler is written in C#.\n\n========================================\n\nComments:\n- Hm, on the surface it sounds silly, kind of like making dog food out of dog food. I'm sure it has merit, though.\n- So what would be used to compile the compiler written in C#? Talk about recursion!\n- @Josh: the previous version of the compiler. It's iterative, not recursive. They're not using the same version of the compiler to compile itself. Rather, they implement the next version of the compiler using the previous version.\n- Nothing unusual about that. A large fraction of \"serious\" languages are eventually bootstrapped to be self hosting.\n- @Kent: I think one of the tests for that compiler must be compiling itself! :)\n- Your objections are non-sense! C compilers nowadays can be perfectly written in C! C# compilers can be written in C#. The only thing that has to be C is the .NET runtime itself! @Kent Boogaart: precisely. And since C# is backwards compatible(usually), it'd be perfectly fine to compile itself.\n- Seems pointless creating a C# compiler in C#, which is calling C++ anyway (the CLR)\n- @Hosam Actually, this could be the first step to self-awareness. Skynet, here we come!\n- @luiscubal, I think everyone's just having fun with the concept.\n- It is called Bootstrapping and it is fairly common to have a compiler written in the languages own language. It is usually a matter of pride as well and marks a major milestone for the language. en.wikipedia.org/wiki/Bootstrapping_(compilers)\n- @Simucal: the link isn't working because the closing parenthesis is not being included in the hyperlink. This one should work: en.wikipedia.org/wiki/Bootstrapping_%28compilers%29\n- I seem to recall that most parts of Delphi were written in, well, Delphi.\n- @Michael Meadows fish food is made from fish? :P so why not\n- To add a real world example that makes sense, Would it be wrong to use last years model of power screwdriver to assemble this years model?\n- Don't know why you'd want to rewrite it in C#. The point of languages such as C# is ease of development, not speed. With a compiler, speed is very much important and every second matters - especially on large projects.\n- This post is aging. Is C# compiler written in C# a reality now (.Net)?\n- For the purpose of history, the first c++ compiler was written in c++.\n- Was the compiler that compiles the mono C# compiler also written in C#?\n- This answers a question, but not the OP's question.","metadata":{"transformedAt":"2026-08-18T18:32:17.761Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":36,"estimatedTokens":668}}121{"id":"stack-4911993","source":"stackoverflow","questionId":4911993,"title":"How to generate and run native code dynamically?","tags":["c++","linux","compiler-construction","x86","jit"],"text":"Title: How to generate and run native code dynamically?\nTags: c++, linux, compiler-construction, x86, jit\nSource: Stack Overflow\n\nQuestion:\nI'd like to write a very small proof-of-concept JIT compiler for a toy language processor I've written (purely academic), but I'm having some trouble in the middle-altitudes of design. Conceptually, I'm familiar with how JIT works - you compile bytecode into (machine or assembly?) code to run. At the nuts-and-bolts level however, I'm not quite gripping *how* you actually go about *doing* that.\n\nMy (very \"newb\") knee-jerk reaction, since I haven't the first clue where to start, would be to try something like the following:\n\n- mmap() a block of memory, setting access to PROT_EXEC\n\n- write the native code into the block\n\n- store the current registers (stack pointer, et al.) someplace cozy\n\n- modify the current registers to point into the native code block in the mapped region\n\n- the native code would now get executed by the machine\n\n- restore the previous registers\n\nIs that even *close* to a/the correct algorithm? I've tried perusing different projects that I know have JIT compilers to study (such as V8) but these codebases turn out to be difficult to consume because of their size, and I've little idea where to start looking.\n\n========================================\n\nTop Answer:\nYoumay want to have a look at *libjit* which provides exactly the infrastructure you're looking for:\n\n The libjit library implements\n just-in-time compilation\n functionality. Unlike other JITs, this\n one is designed to be independent of\n any particular virtual machine\n bytecode format or language.\n\nhttp://freshmeat.net/projects/libjit\n\n========================================\n\nCode:\n```text\n#include <stdio.h>\n#include <windows.h>\n\ntypedef unsigned char byte;\n\nint arg1;\nint arg2;\nint res1;\n\ntypedef void (*pfunc)(void);\n\nunion funcptr {\n  pfunc x;\n  byte* y;\n};\n\nint main( void ) {\n\n  byte* buf = (byte*)VirtualAllocEx( GetCurrentProcess(), 0, 1<<16, MEM_COMMIT, PAGE_EXECUTE_READWRITE );\n\n  if( buf==0 ) return 0;\n\n  byte* p = buf;\n\n  *p++ = 0x50; // push eax\n  *p++ = 0x52; // push edx\n\n  *p++ = 0xA1; // mov eax, [arg2]\n  (int*&)p[0] = &arg2; p+=sizeof(int*);\n\n  *p++ = 0x92; // xchg edx,eax\n\n  *p++ = 0xA1; // mov eax, [arg1]\n  (int*&)p[0] = &arg1; p+=sizeof(int*);\n\n  *p++ = 0xF7; *p++ = 0xEA; // imul edx\n\n  *p++ = 0xA3; // mov [res1],eax\n  (int*&)p[0] = &res1; p+=sizeof(int*);\n\n  *p++ = 0x5A; // pop edx\n  *p++ = 0x58; // pop eax\n  *p++ = 0xC3; // ret\n\n  funcptr func;\n  func.y = buf;\n\n  arg1 = 123; arg2 = 321; res1 = 0;\n\n  func.x(); // call generated code\n\n  printf( \"arg1=%i arg2=%i arg1*arg2=%i func(arg1,arg2)=%i\\n\", arg1,arg2,arg1*arg2,res1 );\n\n}\n```\n\n```text\nint ing(int i) {\n    return i + 1;\n}\n```\n\n```text\n#define _XOPEN_SOURCE 700\n#include <assert.h>\n#include <stddef.h> /* NULL */\n#include <sys/mman.h> /* mmap, munmap */\n\nunion funcptr {\n    int (*f)(int);\n    unsigned char *bytes;\n};\n\nint main(void) {\n    unsigned char *buf = (unsigned char *)mmap(NULL, 4, PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);\n    assert(buf != MAP_FAILED);\n    unsigned char *p = buf;\n\n    // return i + 1;\n    // lea 0x1(%rdi),%eax\n    *p++ = 0x8d;\n    *p++ = 0x47;\n    *p++ = 0x01;\n\n    // ret\n    *p++ = 0xC3;\n\n    assert(((union funcptr){ .bytes = buf }).f(1) == 2);\n\n    // Just to check if we can modify the code witout any explicit icache flushing.\n    // return i + 2;\n    // lea 0x1(%rdi),%eax\n    buf[2] = 0x02;\n\n    assert(((union funcptr){ .bytes = buf }).f(1) == 3);\n\n    int ret = munmap(buf, 4);\n    assert(!ret);\n}\n```\n\n```text\ngcc -ggdb3 -O3 -std=c99 -Wall -Wextra -pedantic -o main main.c\n./main\n```\n\n```text\nassert(((int (*)(int))(buf))(1) == 2);\n```\n\n```text\nint inc(int i) {\n    return i + 1;\n}\n```\n\n```text\ngcc -ggdb3 -O3 -std=c99 -Wall -Wextra -pedantic -c -o notmain.o notmain.c\nobjdump -d notmain.o\n```\n\n```text\n0000000000000000 <inc>:\n   0:   f3 0f 1e fa             endbr64\n   4:   8d 47 01                lea    0x1(%rdi),%eax\n   7:   c3                      ret\n```\n\n```text\nmmap\n```\n\n```text\nmmap\n```\n\n```text\nunion\n```\n\n```text\n-O3\n```\n\n```text\nendbr64\n```\n\n========================================\n\nComments:\n- You can probably simplify things further: you can often just take the starting address of your code within the `mmap`'ed block and cast it to a function pointer. In that case, the code would need to save and restore its own registers and such. You would want to look at the calling conventions in your platforms ABI (Application Binary Interface) for exactly what you need to save (and how to get arguments from C code, call C functions, etc.).\n- Not that I have that much experience with this, but you might way to check out PiPi's python interpreter. I've looked through the CPython interpreter and it's pretty good to read.\n- @Jeremiah Willcock: It seems to me like that's roughly the technique demonstrated by @Shelwien below, am I correct?\n- mmap to PROT_EXEC will probably not work. I don't believe current versions of Linux allow any memory to be both writable and executable at the same time. You need to map it writable, write it, then map it executable. Or so I believe.\n- Actually it seems to be working just fine in my experiment thus far. I'm using a recent build of the kernel (2.6.35) and setting my `mmap()` access rights with `PROT_READ | PROT_WRITE | PROT_EXEC`\n- @Chris: I guess it works for you. I know it didn't work for me. It might have been a Gentoo secure kernel. You might run into trouble on some systems.\n- Not that you're targeting non-x86, but beware that self modifying code (or on-the-fly generated code) requires explicit cache synchronization on other platforms. It's pretty much just x86 which does it transparently (which means loads of silicon). Just call msync() on the buffer after finishing writing and before executing it.\n- @John Ripley: Thanks for the advice. That'd be a tough one to debug!\n- @Shelwien: It was most definitely not portable before, since most modern OS's would not accept execution of the stack.\n- Thanks for the great example! I very nearly didn't catch that `funcptr` was a `union` at first - after that it made perfect sense.\n- First version used a simple typecast there, like ((pfunc)(void*)buf)(); , but codepad said something about ISO C++ not allowing to cast random stuff to functions, so i had to replace it with a union.\n- What would be the modification(s) required to work on x64? I'm not an assembly expert, but I don't see anything that x64 wouldn't find reverse-compatible.\n- I meant that the generated code there is 32-bit, x64 code would be different. I can make a x64 version if necessary, but is it?\n- No, not at all - it was simply my [mis]understanding that 32 bit code could run on a 64 bit machine with little or no modification.\n- Anyway, updated to a \"universal\" version. Previous one wasn't compatible because x64 doesn't have PUSHA/POPA, nor IMUL r32,[m64]\n- I see. I translated this code for Linux (basically just swapped your `VirtualAllocEx` for an `mmap`) and she works like a charm!\n- @Shelwien: I have thought that tricks like above should only be available for system to block polymorphic viruses. But on the other side virtual machines won't be able to make translation of the program being interpreted into machine codes on the fly.\n- Polymorphic viruses don't have to execute generated code in memory, generated code is used to infect another machine, so blocking of data execution won't help much.\n- Interesting find. This may well be useful if I decide I ever actually want to implement a non-trivial JIT.\n- The only thing I would hesitate on here is that with threads, you then have to deal with synchronization, et al. - otherwise (if synchronization can be ignored and/or deferred) that's a pretty clever idea.\n- ISO C still doesn't define the behaviour of type-punning non-function pointers to function-pointers, a union or `memcpy` is just hiding it from the compiler. Probably most readable if you do something like `int (*fptr)(int) = (void*)buf;` in C. GCC `-Wall` doesn't warn about that, nor about `int (*fptr)(int) = reinterpret_cast(buf);` - godbolt.org/z/M9Kr6d9hv\n- GNU C *does* define the behaviour, but you have to use `__builtin___clear_cache(buf, buf+3);` to be sure the compiler won't do dead-store elimination. (Casting to function pointer and calling through it doesn't get treated as \"used\". In this case it doesn't get optimized away because it doesn't know how `mmap(MAP_ANONYMOUS)` works: it's a pointer to memory that could be globally reachable some other way, so has to be in sync before an opaque function call. (In case the called function reads those bytes as data. Reading them as code just happens to work).\n- Despite the name, `__builtin___clear_cache` doesn't actually do anything to cache on x86, only on ISAs without coherent I-cache. On x86 it just blocks dead-store elimination and similar reordering of stores. See How does __builtin___clear_cache work? / gcc.godbolt.org/z/GvKPzYvo5 - in a `-zexecstack` build on an older system where that uses READ_IMPLIES_EXEC, so `malloc` returns executable memory, you actually do need `__builtin___clear_cache` because GCC knows about `malloc`. Or maybe if you `mprotect` some stack mem?\n- godbolt.org/z/5671x3MYn is a better example, using a stack buffer with `-zexecstack` which still works on modern kernels. It shows dead store elimination leading to a jump to uninitialized stack space vs. with clear_cache a working program. See also execute binary machine code from C / How to get c code to execute hex machine code?\n- Also, `#define _XOPEN_SOURCE 700` makes GCC / Glibc choose not to define `MAP_ANONYMOUS` when compiling as C on Godbolt (but works as C++). Leaving it out works, but perhaps there's a higher version number that's appropriate.\n- @PeterCordes thanks for these comments, Peter! Long time no see!","metadata":{"transformedAt":"2026-08-18T18:32:17.761Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":218,"estimatedTokens":2474}}122{"id":"stack-3785366","source":"stackoverflow","questionId":3785366,"title":"How-to ensure that compiler optimizations don't introduce a security risk?","tags":["c++","security","optimization","memory","compiler-construction"],"text":"Title: How-to ensure that compiler optimizations don't introduce a security risk?\nTags: c++, security, optimization, memory, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have to write a Windows service that handles at some point confidential data (such as PIN codes, passwords, and so on). Those informations are needed for a very short amount of time: usually they are sent almost immediately to a smart card reader.\n\nLets consider this piece of code:\n\n```\n{\n std::string password = getPassword(); // Get the password from the user\n\n writePasswordToSmartCard(password);\n\n // Okay, here we don't need password anymore.\n // We set it all to '\\0' so it doesn't stay in memory.\n std::fill(password.begin(), password.end(), '\\0');\n}\n```\n\nNow my concern is about compiler optimizations. Here the compiler might detect that password is about to be deleted and that changing its value at this point is useless and just remove the call.\n\nI don't expect my compiler to care about the value of future-unreferenced memory.\n\nAre my concerns legitimate ? How can I be sure that such a piece of code won't be optimized-out ?\n\n========================================\n\nTop Answer:\nIt's problematic, but for another reason. Who said that `std::string password = getPassword();` doesn't leave yet another copy in the memory? (Probably you need to write a \"secure\" allocator class for this that zeros memory on \"destruct\" or \"deallocate\")\n\nIn your peace of code you can avoid optimization by getting a volatile pointer to the string data (I don't know if you can do it in standard way) and then zero the data through.\n\n========================================\n\nCode:\n```text\n{\n  std::string password = getPassword(); // Get the password from the user\n\n  writePasswordToSmartCard(password);\n\n  // Okay, here we don't need password anymore.\n  // We set it all to '\\0' so it doesn't stay in memory.\n  std::fill(password.begin(), password.end(), '\\0');\n}\n```\n\n```text\nstd::string\n```\n\n```text\nstd::string password = getPassword();\n```\n\n```text\n#pragma optimize( \"\", off )\n\n// Code, not to optimize goes here\n\n#pragma optimize( \"\", on )\n```\n\n```text\nstd::string\n```\n\n```text\nConfidentialString\n```\n\n========================================\n\nComments:\n- Thanks. I just wonder how this function works: what prevents the compiler to optimize it out ?\n- @ereOn: That's easy, its code is just not presented to the compiler when your program is compiled, so the compiler can't see it and decide that it \"does nothing useful\". For example, it can be compiled into a DLL already and only linked dynamically to.\n- @sharptooth: Makes sense, indeed. I will accept this as soon as I can ;) Do you have any links/tutorial about writing a safe password-handling class ?\n- @ereOn: No, I've never seen one. If you decide to write your own - take care of two things - memory deallocation and string shortening. Erase memory before returning it to the memory manager and when the string is shortened erase the previously occupied part. I guess `std::string` can be enhanced for that. Maybe you even get good hints if you ask a separate question - whether it's possible to tweak `std:string` to make it secure.\n- `std::string` is just `std::basic_string, std::alloc>`. I'd think the allocator should be both the 'right' place and sufficient to handle securely zeroing any memory about to be freed.\n- @Christopher Creutzig Yes, allocation can be handled easily, but what about cases when the string is shortened without reallocating the buffer?\n- @sharptooth: The memory will be reallocated or freed when the string object is destroyed. The allocator will be asked to do either of those and can do whatever safe-zero operation the OS or runtime provides. Yes, shortening a string may result in some data lying around longer than necessary – but never as free memory that might be re-used. (What password string is shortened anyway?)\n- @sharptooth: STL-style allocators handle all allocation and deallocation (they don't have a specific resize operation, string/vector/etc. use allocate/copy/deallocate).\n- @Christopher: std::allocator for the third template argument.\n- @Roger: Right, sorry for the typo.\n- Well, the `getPassword()` thing was just made up to write my example code. But you're absolutely right, one must care about this too.\n- @ereOn Whatever way you generate the password you must be extremely careful in this case. The easiest way is to use a custom allocator that zeros deallocated memory by one of the above methods (volatile or SecureZeroMemory). Note that std::string doesn't destruct its elements, only deallocates.\n- I tend to agree. However, in my place, when a software crashes, a dump file is sent to a specific service or the developer himself. Even if those people can be trusted, If I can avoid confidential data to be stored somewhere, that's even better.\n- @ereOn: I'm afraid that dump file, if it contains the image of the process, is itself a security risk. Nothing you do to wipe out sensitive data can protect you because the developer has access to the code and can easily subvert your security measures.\n- The developer can't change the code that runs under our production environment. If the code is well and safely designed, knowing how it work can't help to hack the data. And if you manage to remove all sensitive data from the dumps, those can't help either.\n- I'd be surprised if there's a platform where `std::fill()` is *not* inlined. In fact, I'd expect my implementation to fall back to some platform-specific intrinsic for this. And a compiler should certainly be able to optimize this.\n- Because std::fill is a template, it is available to be inlined. (Even with exported templates, removed in C++0x, something has to be available to generate the right code.)\n- I wasn't aware of that function. Thanks, it will surely help.\n- You don't have to invent your own string class from scratch, just your own allocator.\n- @Roger Pate: you're probably still better off rolling your own - makes it easier to control what external APIs you can (and can't!) pass the password to, lets you keep the string encrypted internally, and avoid unintended copy construction. Besides, how often do you need to do string manipulation on a passphrase? :)","metadata":{"transformedAt":"2026-08-18T18:32:17.761Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":96,"estimatedTokens":1563}}123{"id":"stack-76527163","source":"stackoverflow","questionId":76527163,"title":"Why is the 'auto' keyword useful for compiler writers in C?","tags":["c","compiler-construction","keyword","auto","automatic-storage"],"text":"Title: Why is the 'auto' keyword useful for compiler writers in C?\nTags: c, compiler-construction, keyword, auto, automatic-storage\nSource: Stack Overflow\n\nQuestion:\nI'm currently reading \"*Expert C Programming - Deep C Secrets*\", and just came across this:\n\nThe storage class specifier `auto` is never needed. It is mostly meaningful to a compiler-writer\nmaking an entry in a symbol table — it says \"this storage is automatically allocated on entering the\nblock\" (as opposed to statically allocated at compiletime, or dynamically allocated on the heap). `auto`\nis pretty much meaningless to all other programmers, since it can only be used inside a function, but\ndata declarations in a function have this attribute by default.\n\nI saw that someone asked about the same thing here, but they don't have any answer and the link given in comments only explains why there's such a keyword in C, inherited from B, and the differences with C++11 or pre-C++11.\n\nI'm posting anyway to focus on the part stating that the `auto` keyword is somehow useful in compiler writing, but what is the idea nor the connection with a symbol table?\n\nI really insist on the fact that I ask only about a potential usage when programming a compiler in C (**not** coding a C compiler).\n\nTo clarify, I asked this question because I'd like to know if there's an example of code where `auto` can be justified, because the author stated there would be, when writing compilers.\n\nHere the whole point is that I think to have understood `auto` (inherited from B, where it was mandatory, but useless in C), but I can't imagine any example when using it is useful (or at least not useless).\n\nIt really seems that there isn't any reason at all to use `auto`, but is there any old source code or something like that corresponding to the quoted statements?\n\n========================================\n\nTop Answer:\nAs far as I can tell from 40+ years of C programming, including compiler work, the `auto` keyword has been completely useless in C for 50 years.\n\nTo answer your precise question, *Why is `auto` keyword useful for compiler-writers in C?* It isn't useful at all; C compiler writers are just required to parse it as a keyword and implement its semantics as a storage class specifier.\n\nIt seems to be a left over from B, the predecessor to the C language, developed by Ken Thompson and Dennis Ritchie at Bell Labs in the late sixties and early seventies. I have never used B and I doubt Peter, whom I met in 1984 at Inria, has either.\n\nBefore C23, `auto` can only be used to specify automatic storage class for definitions in the scope of a function. This is the default, so `auto` is fully redundant and as long as the type or another qualifier is specified, `auto` can be removed. There isn't any case where it was needed, so its inclusion in the C Standard is only rooted in the early history of the C language.\n\n`auto` has been used in C++ since C++11 to enable type inference in variable definitions, with or without automatic storage, where the compiler detects the type from that of the initializer.\n\nWith the current trend pushing for convergence on a common subset for the C and C++ languages, new semantics have been attached to this keyword in C23 modelled after the C++ semantics, but more restricted:\n\n**6.7.1 Storage-class specifiers**\n\n`auto` may appear with all the others except `typedef`;\n\n`auto` shall only appear in the declaration specifiers of an identifier with file scope or along with other storage class specifiers if the type is to be inferred from an initializer.\n\nIf `auto` appears with another storage-class specifier, or if it appears in a declaration at file scope, it is ignored for the purposes of determining a storage duration of linkage. It then only indicates that the declared type may be inferred.\n\nType inference is specified as:\n\n**6.7.9 Type inference**\n\n**Constraints**\n\n1 A declaration for which the type is inferred shall contain the storage-class specifier `auto`.\n\n**Description**\n\n2 For such a declaration that is the definition of an object the init-declarator shall have one of the forms\n\n*direct-declarator = assignment-expression*\n\n*direct-declarator = { assignment-expression }*\n\n*direct-declarator = { assignment-expression , }*\n\nThe declared type is the type of the assignment expression after lvalue, array to pointer or function to pointer conversion, additionally qualified by qualifiers and amended by attributes as they appear in the declaration specifiers, if any. If the direct declarator is not of the form *identifier attribute-specifier-sequenceopt*, possibly enclosed in balanced pairs of parentheses, the behavior is undefined.\n\nType inference is very useful in C++ because types can be very complex and almost impossible to specify in variable definitions, especially with templates. Conversely, using it in C is probably counter productive, lessening code readability and encouraging laziness and error prone practices. It was already bad enough to hide pointers behind typedefs, now you can hide them completely with the `auto` keyword.\n\nTo finish on a less serious note, I remember seeing it used in tricky interview tests, where the candidate is asked to find why this code does not compile:\n\n```\n#include \n#include \n\nint main(void) {\n char word[80];\n int auto = 0;\n while (scanf(\"%79s\", word) == 1) {\n if (!strcmp(word, \"car\")\n || !strcmp(word, \"auto\")\n || !strcmp(word, \"automobile\"))\n auto++;\n }\n printf(\"cars: %d\\n\", auto);\n return 0;\n}\n```\n\n========================================\n\nCode:\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\n#include <stdio.h>\n#include <string.h>\n\nint main(void) {\n    char word[80];\n    int auto = 0;\n    while (scanf(\"%79s\", word) == 1) {\n        if (!strcmp(word, \"car\")\n        ||  !strcmp(word, \"auto\")\n        ||  !strcmp(word, \"automobile\"))\n            auto++;\n    }\n    printf(\"cars: %d\\n\", auto);\n    return 0;\n}\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\ntypedef\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nmain()\n{\n    extrn printf;\n    auto x;\n    x = 25;\n    printf('%d', x);\n}\n```\n\n```text\nmain()\n{\n    extern printf();\n    auto x; /* type is int by default */\n    x = 42;\n    printf(\"%d\", x);\n}\n```\n\n```text\nauto\n```\n\n```text\nextrn\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nregister\n```\n\n```text\nstatic\n```\n\n```text\nextern\n```\n\n```text\n_Thread_local\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nauto\n```\n\n```text\nenum\n```\n\n```text\nstatic\n```\n\n```text\nvoid soso(int x) {\n  int y = x * 2;\n  // The compiler could optimize this code if it knew that y was allocated on the stack.\n  int z = y + 3;\n}\n```\n\n========================================\n\nComments:\n- Compilers issue warnings as well as generating code, for example reading an uninitialised variable, or returning a pointer to a variable, and for that it needs to know if it was static or auto.\n- Remember, there's a lot of things in the C specification that seemed like a great idea at the time, but were later proven to be kind of useless. There's a lot of feedback between the C and C++ standards teams, where C is probably like \"oh, yeah, `auto` meaning \"figure it out\" does make a lot more sense, but oh well.\"\n- @WeatherVane Maybe I'm not understanding correctly what you meant, but here I'm expecting to know which (if any) examples in C may use auto.\n- Just because you don't need to explicitly say `auto` doesn't mean the compiler doesn't need that information, which will be available via the symbol table. As your quote says: \"a compiler-writer making an entry in a symbol table\".\n- The book you quote from seems to be from 1994 (or before). So yeah, maybe it was true 29 years ago, but not so much these days.\n- Aside: I believe C23 is going to re-purpose `auto` to be more like C++.\n- @pmacfarlane I'm aware of this, I thought that this may not be true nowadays, but even if it's only an old rare artifact, I'd only like to fully understand the quotes, eventually seeing outdated source code to understand this, that's purely theoretical.\n- \"I can't imagine any example when using it is useful\" -- I notice the book author claimed it to be useful without actually explaining how or why. Perhaps they were mistaken, or speaking of some'80s or early-90s \"dumb\" compilers that needed more hints?\n- @Chi_Iroh, A singular use case for `auto` I've seen was when the attributes of an object like `static`, `extern`, `auto`, .... were rolled into a macro. In the case when no attributes were needed, the macro was assigned `auto` rather than *nothing*.\n- Oh, fun usage ! I like it (theoretically, of course).\n- I think it simply means that compilers always know the storage class of a variable, so even if you don't write a storage class, the compiler remembers it the same as if you typed `auto`, and the compiler frequently has to check if variables are `auto`, so compiler writers think about it a lot even though programmers don't.\n- @pmacfarlane That's not really an aside - being able to repurpose a keyword like that means two things: the keyword had no useful, actual syntactical meaning, and no one used it anyway so the repurpose wouldn't break existing code. And from that, it can be inferred `auto` was of no practical use to anyone, compiler writer or otherwise.\n- This question happens to be well received, but otherwise *Retrocomputing* is happy to take such questions.\n- Notwithstanding your \"insist\" and \"clarify\" paras, there's two ways of reading this. The first is \"Can a compiler writer benefit from being able to use 'auto' in the code he's writing?\" The second is \"Can a compiler writer benefit from being able to assume that other programmers use 'auto' where appropriate in the code his compiler will be processing?\".\n- @MarkMorganLloyd What do you think about my edit ? Do I need to clarify a bit more ?\n- @Chi_Iroh Frankly I don't think there's very much can- or needs to- be done, I was thinking of commenting earlier and possibly should have. I think the important thing is that the community appears to have reached a consensus, and the bottom line is that when B and C were designed the process of designing a language and compiler was far less understood than it is today, and- of course- the amount of space available for the code and data associated with compile-time inference was limited.\n- Answer validated because it fits with everything I saw on the Internet, and I'm not surprised at all that even a so much experienced C programmer hasn't seen an utility for this keyword. And also the fact that you're getting some upvotes strengthens this point.\n- Isn't `auto` having C++ semantics already a done deal in C23?\n- @pmacfarlane It doesn't have C++ semantics exactly, but it can be used to infer a type as in `auto x = foo();` in C23.\n- @Ted I'm no expert in C++ (working on it!), but isn't that basically also how it works in C++? Is there a difference?\n- @pmacfarlane That's one use of `auto` that works in both languages. C++ has a lot more of them. Not all uses would not be possible in C and making C++ syntax legal in C for those cases would make header files used by both C and C++ really error prone.\n- @TedLyngmo: IMHO, It is a sad move to try and converge C toward its distant cousin.\n- @pmacfarlane: I'm afraid you are correct... so much crap went into C23 I missed this one. Answer amended\n- @chqrlie It will spawn a legion of new SO questions where people mess up their types by using `auto` everywhere. It's a very lazy solution that causes more problems than it solves. In saying that, I like it in C++ because it has some very verbose compound types that you don't get in C.\n- Very fun code, I like it\n- *auto is used in C++ to enable type inference* - For the record, that was new in C++11. In C++ before that, `auto` worked as it does in C before C23, as a storage-class specifier. godbolt.org/z/6WeGab6of (and deduced return types for functions only with C++14.)\n- @PeterCordes: good point. Answer amended.\n- @pmacfarlane our workplace just instituted a code checker that insists you use `auto` whenever the type can be inferred by context. I really dislike that rule, because sometimes explicit types are useful documentation about what you're working with.\n- @MarkRansom: I agree 100%. `auto` is useful in many places in C++, but enforcing it everywhere seems counter productive. In C however, I would support a rule that forbids its usage anywhere.\n- @MarkRansom: the blog link in your profile seems broken.\n- @chqrlie yes, it's broken. It actually never worked, but I dropped my web provider a couple of years ago so now it's even more broken.\n- I think the idea is the compiler mantains the *storage category* in the symbol table (e.g. static, stack, etc.). With the idea that the mapping from source code to compiler internals is thin, keywords more or less map directly: `static` maps to static, `auto` maps to stack, possibly `register` maps to register, etc. (only, auto is implicit, and register is calculated by the compiler and the keyword ignored).\n- There's a bit more to it. The `auto` keyword exists because the earliest C compilers could not compile without it. Another round of syntax lifting could have removed it but it wasn't done. Just regex-removing `auto` wouldn't have worked because variables were declared `auto x;` within the compiler source itself. You see, `int` was implicit.\n- @Joshua How much early ? Do you mean pre-ANSI ones ? If yes, is there a way to compile pre-ANSI code today ? It seems impossible with GCC because there's no std flag prior to std=c89.\n- @Chi_Iroh: Early early. We're talking the original PDP-11 Unix compiler. This weird behavior exists because that compiler depended on it. (You can check pcc and see if it still does nor not.) (AFAIK std=c89 can successfully compile all K&R C so no earlier option was needed.)\n- Ok thank you, I'll definitely take a look about pcc and other old compilers.\n- Interesting argument, yet there are other concepts for which *regularity* would require more keywords: `static` vs *public* for global symbols, `static` vs *dynamic* for local symbols, `extern` vs *locally defined* symbols, `const` vs *modifiable*... Having a redundant keyword actually goes against one of the C designers' cardinal values: simplicity. It is probable they kept the `auto` keyword for compatibility with ancient code originally written in B as shown in user7860670's answer, where `int` was implicit.\n- @Bob__: I did, indeed.\n- @chqrlie: Storage-class specifiers are specifically about \"variables\", by opposition to function: you don't apply `register` to a function. So, with regard to global variables, we're talking `static` vs `extern` (if you want to declare it in a header), and with regard to locally scoped variables, that `static` vs `auto` vs `register`. So for variables it's fairly regular, actually. As for `const` (and `volatile`), it was introduced in ANSI C, and was not present in the K&R version, so it being different is fairly normal.\n- There's one case where it is useful in a current-version C program ( > C90 && < C23 ) -- if there's a macro involved. `MAILBOX x;` might be `int`, might be `char`, might be `volatile`, might be `static`. `auto MAILBOX x;` might be `int`, might be `char`, might be `volatile`.... but for certain it is not `static`, and instead of silently broken code you will get a compile error if any future programmer ever tries to add `static` into the #define of `MAILBOX`.\n- @BenVoigt: Nice one indeed; I've amended the answer from never to rarely, and mentioned your example.\n- Isn't a register faster than the stack ? I may be wrong but I believe compilers actually move stacked variables into registers whenever they can to speed up the code.","metadata":{"transformedAt":"2026-08-18T18:32:17.761Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":39,"totalLines":324,"estimatedTokens":3989}}124{"id":"stack-1132051","source":"stackoverflow","questionId":1132051,"title":"Is finding the equivalence of two functions undecidable?","tags":["math","compiler-construction","function","computer-science","theory"],"text":"Title: Is finding the equivalence of two functions undecidable?\nTags: math, compiler-construction, function, computer-science, theory\nSource: Stack Overflow\n\nQuestion:\nIs it impossible to know if two functions are equivalent? For example, a compiler writer wants to determine if two functions that the developer has written perform the same operation, what methods can he use to figure that one out? Or can what can we do to find out that two TMs are identical? Is there a way to normalize the machines?\n\nEdit: If the general case is undecidable, how much information do you need to have before you can correctly say that two functions are equivalent?\n\n========================================\n\nTop Answer:\nYes, it is undecidable. This is a form of the halting problem.\n\nNote that I mean that it's undecidable for the general case. Just as you can determine halting for sufficiently simple programs, you can determine equivalency for sufficiently simple functions, and it's not inconceivable that this could be of some use for an application. But you cannot make a general method for determining equivalency of any two possible functions.\n\n========================================\n\nCode:\n```text\ndef create_f_prime(f):\n    def f_prime(n):\n        f(n)\n        return 1\n    return f_prime\n```\n\n```text\ndef create_g(f_prime, x):\n    def g(n):\n        return 1 if n == x else f_prime(n)\n    return g\n```\n\n```text\ndef halts(f, x):\n    def f_prime(n): f(n); return 1\n    def g(n): return 1 if n == x else f_prime(n)\n    return equiv(f_prime, g) # If only equiv would actually exist...\n```\n\n```text\n-- Tells whether two functions f and g are equivalent.\nequiv :: (Integer -> Integer) -> (Integer -> Integer) -> Bool\nequiv f g = undefined -- If only this could be implemented :)\n\n-- Tells whether f halts on input x\nhalts :: (Integer -> Integer) -> Integer -> Bool\nhalts f x = equiv f' g\n  where\n    f' n = f n `seq` 1\n    g  n = if n == x then 1 else f' n\n```\n\n```text\nseq\n```\n\n========================================\n\nComments:\n- Although this is undecidable in the general case, there are formal verification tools that can prove the correctness of two programs with respect to a specification.\n- The only viable solutions would a glass box one that involves theorem provers and the like. A brute force solution would be easily thwarted by a pair of functions the don't halt for some case.\n- This isn't enough. It may well be possible to show that 2 functions will result in the same effect (including halting or not) without showing anything about what they do (giving answerers like \"both will return the same result or not halt, but I don't know which\")\n- Proving f(x)==f(x) is easy even if it's termination is undecidable. So simple counter example. So spitting out the halting problem shows lack of understanding without applying it in the specific case\n- You can decide if two functions whose termination is undecidable. As f(x)==f(x) is decidable so a copy of a function is a special case\n- +1, but this begs another question: What is the minimum set of limitations necessary to make this decidable? Obviously, if the input set is defined to be sufficiently small, and a limit is introduced to the execution time, both functions could be brute forced.\n- \"When is one thing equal to some other thing\" -- www.math.harvard.edu/~mazur/preprints/when_is_one.pdf\n- What can we show if we only ask if the two functions will /return/ different results? That is a function that never returns, never returns different results than anything.\n- That isn't going to help. The 'return value problem' is still equivalent to the halting problem even if it's not halting we're asking about. And besides, you're begging the question of the halting problem by asking to *exclude* functions that do not halt. (Begging the question in the actual rhetorical meaning, not the way people misuse it conversationally as l0b0 demonstrates.)\n- I think you are correct in your conclusion, but I don't think your argument is valid (or maybe I just don't understand it). A better argument would be: given function F and input N derive `G1(n) = (n != N) ? true : (F(n), true)` and `G2(n) = true`. Showing G1 == G2 for all n shows F halts for N\n- @chaos, by excluding the halting case, I'm saying that any answerer is valid from the comparator function if either input doesn't halt. So it still doesn't need to answerer that. And I think the original form is \"*beggaring* the question\": guardian.co.uk/lifeandstyle/2001/sep/08/weekend.zoewilliams\n- Think of it this way: because the halting problem is undecidable, you cannot find out if a program is going to halt other than by running it. Someone who's better at proving this stuff than I am could show you how that also means you can't find out what output a function is going to produce other than by running it, and for this comparison that means running it for all possible inputs, which is certainly possible for some functions and equally certainly not possible for all.\n- I don't know what this \"beggaring the question\" madness is, but it's nothing to do with begging the question. Begging the question means using premises that assume a conclusion to a question that has not actually been resolved: en.wikipedia.org/wiki/Begging_the_question. i.e. someone doing this is \"begging\" to have another question taken as answered. Nothing to do with making a question \"poorer\".\n- Just because you can't show that a function will halt doesn't imply that you can't show things of the form \"If the function halts, it's output will be X\" or by extension: \"If both functions halt, they will return the same result\". Note that nether of these make any claim as to if the function will halt or not and, importantly, the second doesn't even disclaim that one function halts and the other doesn't. -- My assertion is that the halting problem can't be directly used to argue about this problem unless you require correct result for non halting input functions.\n- To me \"making a question poorer\" seems like a nice way say that the question \"[uses] premises that assume a conclusion\". But I'm not interested enough to argue the point further.\n- This seems to be a correct proof about partial functions. What about total functions? Is the problem of determining whether a total function f1 is equivalent to a total function f2 undecidable? If yes, why?\n- If the two programs are guaranteed to halt for every input then we can check equivalence by comparing the outputs over the entire input space and this should be recursive and decidable right?\n- @IndrajitBanerjee comparing the outputs over the entire input space will not work if the input space is infinite.\n- This is incorrect for Boolean functions where deciding functional equivalence is the same as showing f(x) XOR g(x) is UNSATisfiable. For a thorough and better answer it is always useful to look at cases where it is not undecidable especially when huge industries e.g. logic synthesis make use of them. Neglecting this shows basic academic knowledge with no practical appreciation if the world you live in.\n- Boolean functions are decidable by showing f(x) XOR g(x) is UNSATisfiable. Such functionality equivalence is used in large scale hardware synthesis to verify circuit correctness. You would even have the device you are using if it weren't for it ...\n- I think your 3rd assertion is false. I think that no general solution exists for even proving two function are equivalent because there are convincing arguments (see Stephan202's answer) that this can require solving the halting problem.\n- I didn't say that, but stackoverflow ate my longer response. I constrained the problem to proving that two functions that are equivalent can be detected as equivalent. The COMDAT folding used by the MSVC linker does just that.\n- So if we want to compare two `total` functions (those which halt on every input) and return yes if they match for every input, no otherwise. How to prove that this problem is undecidable (is it)?\n- If there are finitely many inputs in the domain, and both functions halt for all of them, and you have a decidable equality check for the codomain, then you just try both functions on all the inputs and compare the outputs.\n- Then it isn't decidable.\n- Exactly, so how to prove that? Halt/not halt arguments used for non-total functions no longer work here as everything is guaranteed to halt.","metadata":{"transformedAt":"2026-08-18T18:32:17.762Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":87,"estimatedTokens":2099}}125{"id":"stack-8760956","source":"stackoverflow","questionId":8760956,"title":"Can a compiler automatically detect pure functions without the type information about purity?","tags":["c","gcc","haskell","compiler-construction","d"],"text":"Title: Can a compiler automatically detect pure functions without the type information about purity?\nTags: c, gcc, haskell, compiler-construction, d\nSource: Stack Overflow\n\nQuestion:\nSo I'm arguing with my friend who claims that a compiler like GCC can detect a pure function automatically without any type information. I doubt that.\n\nLanguages like D or Haskell have purity in their type systems and a programmer explicitly defines what function is pure or not. A pure function has no side effects and can therefore very easily be parallelized.\n\nSo the question is: Is this all necessary or not? Could a compiler detect purity, without any meta or type information, just by assuming that anything that does IO or accesses global variables automatically is not pure?\n\n========================================\n\nTop Answer:\nThere is another problem. Consider\n\n```\nint isthispure(int i) {\n if (false) return getchar();\n return i + 42;\n}\n```\n\nThe function is effectively pure, though it contains impure code, but this code cannot be reached.\nNow suppose `false` is replaced by `g(i)` but we know quite sure that g(i) is false (for example, g might check if its argument is a Lychrel number).\nTo prove that isthispure is indeed pure, the compiler would have to prove that no Lychrel numbers exist.\n\n(I admit that this is a quite theoretical consideration. One could also decide that if a function contains any impure code, it is itself impure. But this is not justified by the C type system, IMHO.)\n\n========================================\n\nCode:\n```text\nint f(int x)\n{\n    return x*2;\n}\n```\n\n```text\n-Wsuggest-attribute=pure\n```\n\n```text\n-Wsuggest-attribute=const\n```\n\n```text\npure\n```\n\n```text\nconst\n```\n\n```text\npure\n```\n\n```text\npure\n```\n\n```text\nconst\n```\n\n```text\npure\n```\n\n```text\nconst\n```\n\n```text\nint isthispure(int i) {\n   if (false) return getchar();\n   return i + 42;\n}\n```\n\n```text\nfalse\n```\n\n```text\ng(i)\n```\n\n========================================\n\nComments:\n- Well, both answers would be interesting - if it would be possible in theory and if any production ready compiler does something like that.\n- Also, is the question about whether compilers *would be able* to detect pure functions or whether compilers *actually apply* such a detection algorithm?\n- If it wasn't for separate compilation, C could have perfect knowledge of what is pure and impure. Just because that information isn't called \"type\" doesn't mean a compiler can't be aware of it. The problem with separate compilation - functions linked in from object files or whatever don't include purity metadata. Even if they did, that information isn't available until linking, so the compiler doesn't see it. Within a particular source file, though, the compiler may identify functions that don't have any possible source of impurity and may use that information for certain optimisations.\n- Also, for languages like C and C++, the compiler is expected to produce assembly that pretty much reflects the exact structure of the program given, so I don't think it would be allowed to apply optimizations that are as intrusive as automatic parallelization. By the way, do you know whether GCC applies such detection? If yes, for what purpose?\n- @NiklasBaumstark: C has the \"as-if\" rule, which allows the implementation to do anything it wants so long as a conforming program cannot distinguish the result. Parallelising the execution of pure functions would fall under this umbrella.\n- I've just expanded my answer to answer the GCC question.\n- A wise compiler, when guessing about the \"purity\" of a function (for the sake of optimization), will allow false negatives (might miss chances for optimization), but not false positives (otherwise would perform erroneous optimization).\n- @ehird: That's true, but if a C compiler automatically created threads behind your back, nobody would go near it.\n- @PeterAlexander: I dunno — if someone offered me a C compiler which automatically parallelised programs with good efficiency and low overhead, I wouldn't say no. Of course, such a thing is a silver bullet.\n- are you sure about this const func == strict purity claim? .. i just get \" error: non-member function cannot have 'const' qualifier\" ... it works on c++ class member functions, but then it means the non-mutable members of the object pointed to by this can not be modified.\n- @WillemHengeveld I don’t know if it’s still relevant to you but it might be to other people who come here: there’s a difference between `const` as you are using and the GCC `const` attribute, `__attribute__((const))`.\n- +1, but actually the compiler would only need to check that the integers ranging from `INT_MIN` to `INT_MAX` are not Lycrel numbers, which is a much easier task than proving that none exist. A good static analyzer could certainly apply a brute-force approach to functions whose argument space is smaller than some bound `M` (e.g. `M=1<<32` or so).\n- Even Haskell doesn't *exactly* assert that there is definite purity or impurity. When a function uses `unsafePerformIO`, the author may be wrong that nothing impure occurs. Also, there are functions with return type `IO a` which perform no IO.\n- @amindfv: Well, `unsafePerformIO` isn't \"really\" part of Haskell, even if it is a standard part of the FFI.\n- @ehird: Is it part of the FFI? It's in System.IO, where all of the basic file handle stuff is\n- @amindfv: Actually, it's in System.IO.Unsafe; however, the structure of the standard base package doesn't mirror the structure of the standard, it's a superset. `unsafePerformIO` is in the FFI addendum to Haskell 98; the FFI was included in Haskell 2010, but without `unsafePerformIO` (there's a less powerful `unsafeLocalState` instead).\n- @R. I bet you can't do that for only 10 ints (say from 190 to 200) in reasonable time. There are Lycrel candidates (was it 192 or 196, dunno at the moment), where your personal computer may be busy a year, or so and still will not have proved that it is none.\n- On the opposite side from unsafePerformIO, there's IO actions that turn out to be completely pure. For example `return 0` - the type is `IO Int` (or `IO Num` or whatever), but there are no effects. This is very much like the C example for this question - especially if we add some genuinely effectful action that will never be used such as `if False then getChar else (return ' ')`. Haskell code can appear effectful yet have no effects too - it's just that the deception is explicit in the type rather than implicit.","metadata":{"transformedAt":"2026-08-18T18:32:17.762Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":111,"estimatedTokens":1624}}126{"id":"stack-20252876","source":"stackoverflow","questionId":20252876,"title":"Wanted: good definition of the term \"lowering\" in the context of compilers","tags":["compiler-construction","terminology"],"text":"Title: Wanted: good definition of the term \"lowering\" in the context of compilers\nTags: compiler-construction, terminology\nSource: Stack Overflow\n\nQuestion:\nCan anyone point me to a good definition of the term \"lowering\" in the context of compilers?\n\nFrom what I can tell, it is the translation of a higher-level operation into an equivalent combination of lower-level operations, but I'm not really sure, and I can't find one after a few minutes of searching on Google. (There are a few usages of \"lowering\" e.g. in GCC or LLVM but no references to a definition.)\n\n========================================\n\nTop Answer:\n*The Dragon Book* doesn't use the term. Kennedy+Allen's *Optimizing Compilers for Modern Architectures A Dependence-based Approach* doesn't use the term. Steve Muchnick's *Advanced Compiler Design and Implementation* doesn't use the term.\n\n*Engineering A Compiler* uses the term but doesn't define it. Bob Morgan's *Building an Optimizing Compiler* (1998) uses the term a lot and defines it.\n\nLowering : The instructions are lowered so that each operation in the\nflow graph represents a single instruction in the target machine.\n\nIt is a more general term and there is no single definition. My own understand is that a compiler *lowers* an operation from a higher abstraction layer to a lower, for example, in LLVM lowering from MachineInstr to MCInst.\n\n========================================\n\nCode:\n```text\nwhile\n```\n\n```text\nforeach\n```\n\n```text\nfor\n```\n\n```text\nfor\n```\n\n```text\nscope guard\n```\n\n```text\ntry-finally\n```\n\n```text\ncompare the upper 32 bits with a signed comparison\nif they are equal, compare the lower 32 bits with an unsigned comparison\n```\n\n========================================\n\nComments:\n- Isn't this called `reducing`? Or maybe that's what it's called in grammar definitions, I can't remember.\n- Pretty vague. That's what the entire compiler does. I would call specific examples 'strength reduction', e.g. `*2` => `<< 1`.","metadata":{"transformedAt":"2026-08-18T18:32:17.762Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":58,"estimatedTokens":493}}127{"id":"stack-6210105","source":"stackoverflow","questionId":6210105,"title":"How can CoffeeScript be written in CoffeeScript?","tags":["compiler-construction","coffeescript","interpreter","bootstrapping"],"text":"Title: How can CoffeeScript be written in CoffeeScript?\nTags: compiler-construction, coffeescript, interpreter, bootstrapping\nSource: Stack Overflow\n\nQuestion:\nSo as a new web programmer (background is mostely in C,C++, and Python) with no javascript experience (or desire to experience it, based on what I have seen) I have been doing some precursory research on CoffeeScript and am really liking what I see. One cool little nuance I noticed was that CoffeeScript was written in CoffeeScript, which is cool... but I don't really understand how that is possible. Unfortunately I opted to take Network Security instead of Compilers during my last senior level Computer Science courses.\n\nI have heard of people writing languages in target language to interpret using existing compilers for that language, but I can't dig up any info on how this might work since this is the first implementation.\n\nMy guess is an embedded shell script that might take care of laying down some of the initial framework for building a **self-referential** (*does this term properly describe this behavior?)* language?\n\n========================================\n\nTop Answer:\nMatt's answer is excellent. Let me just elaborate with some CoffeeScript-specific info:\n\nThe original version of the CoffeeScript compiler was written in Ruby, but it was transitioned to CoffeeScript for v0.5.0, on Feb 21, 2010.\n\nAs a practical matter, the bootstrapped compiler can make things difficult with an ever-changing language, as the compiler has to be rewritten to accomodate those changes. This paragraph from the official docs gives you some idea of the challenges involved:\n\n `git checkout lib && bin/cake build:full` is a good command to run when you're working on the core language. It'll refresh the `lib` directory (in case you broke something), build your altered compiler, use that to rebuild itself (a good sanity test) and then run all of the tests. If they pass, there's a good chance you've made a successful change.\n\nThe `lib` directory contains the compiled JavaScript of the CoffeeScript compiler (got that?), providing a helpful intermediary in the bootstrapping process. Since the CoffeeScript code of the compiler never runs *directly* on itself, that makes it easier to make breaking changes to the language.\n\n========================================\n\nCode:\n```text\nLanguage X\n```\n\n```text\ngit checkout lib && bin/cake build:full\n```\n\n```text\nlib\n```\n\n```text\nlib\n```\n\n========================================\n\nComments:\n- en.wikipedia.org/wiki/Bootstrapping_(compilers)\n- be warned that you *need* to know Javascript well to code in CoffeeScript, since all runtime errors will point to the compiled source.\n- Re terminology: You could say that the CoffeeScript compiler was **bootstrapped** from its original Ruby implementation. You could also say that the CoffeeScript language is **self-hosting**.\n- possible duplicate of Bootstrapping a language\n- I have heard the term Bootstrapped before, but had no real clear understandign of it. This is really interesting, thanks for the great links, I will do some reading up on bootstrapping. I have heard of the dragon book as well reading \"Must read programming books\" threas on SO, I will have to check it out\n- I agree with Matt. But, if you want to learn Coffeescript, I highly suggest learning Javascript first. It may look a bit alien-like to people from different language-backgrounds, but its concepts (like function and prototype base) are useful for a variety of software/application solutions and frameworks. Again, Coffeescript compiles to Javascript. So, obviously, hidden underneath the classes and different-looking concepts in CS, Javascript's concepts play a great role. And do read The Dragon Book to understand how compilers work.\n- wish i could upvote this more, thank you for giving language specifics.","metadata":{"transformedAt":"2026-08-18T18:32:17.762Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":53,"estimatedTokens":963}}128{"id":"stack-17383492","source":"stackoverflow","questionId":17383492,"title":"How does the compiler know that the comma in a function call is not a comma operator?","tags":["c","function","compiler-construction","comma-operator"],"text":"Title: How does the compiler know that the comma in a function call is not a comma operator?\nTags: c, function, compiler-construction, comma-operator\nSource: Stack Overflow\n\nQuestion:\nConsider the function call (calling `int sum(int, int)`)\n\n```\nprintf(\"%d\", sum(a,b));\n```\n\nHow does the compiler decide that the `,` used in the function call `sum(int, int)` is not a comma operator?\n\n**NOTE**: I didn't want to actually use the comma operator in the function call. I just wanted to know how the compiler knows that it is not a comma operator.\n\n========================================\n\nTop Answer:\nFrom C99 6.5.17:\n\n As indicated by the syntax, the comma operator (as described in this subclause) cannot\n appear in contexts where a comma is used to separate items in a list (such as arguments to functions or lists\n of initializers). On the other hand, it can be used within a parenthesized expression or within the second\n expression of a conditional operator in such contexts. In the function call\n\n```\nf(a, (t=3, t+2), c)\n```\n\n \n the function has three arguments, the second of which has the value 5.\n\nAnother similar example is the initializer list of arrays or structs:\n\n```\nint array[5] = {1, 2};\nstruct Foo bar = {1, 2};\n```\n\nIf a comma operator were to be used as the function parameter, use it like this:\n\n```\nsum((a,b))\n```\n\nThis won't compile, of course.\n\n========================================\n\nCode:\n```c\nprintf(\"%d\", sum(a,b));\n```\n\n```text\nint sum(int, int)\n```\n\n```text\n,\n```\n\n```text\nsum(int, int)\n```\n\n```text\nparameter-list\n```\n\n```text\nparameter-list\n```\n\n```text\nassignment-expression\n```\n\n```text\nexpression\n```\n\n```text\nexpression\n```\n\n```text\nassignment-expression\n```\n\n```text\nif\n```\n\n```text\nwhile\n```\n\n```text\nfor\n```\n\n```text\nexpression\n```\n\n```text\nf(a, (t=3, t+2), c)\n```\n\n```text\nint array[5] = {1, 2};\nstruct Foo bar = {1, 2};\n```\n\n```text\nsum((a,b))\n```\n\n```text\nf(a, (t=3, t+2), c)\n```\n\n```text\n6.5.2 postfix-expression:\n       ...\n       postfix-expression ( argument-expression-list_opt )\n```\n\n```text\nargument-expression-list:\n       assignment-expression\n       argument-expression-list , assignment-expression    <-- arglist comma\n\nexpression:\n       assignment-expression\n       expression , assignment-expression                  <-- comma operator\n```\n\n```text\n()\n```\n\n```text\nprintf(\"Hello, world\\n\");\n```\n\n========================================\n\nComments:\n- you are talking about which one of the two commas...\n- @SazzadurRahaman; comma in the function call.\n- Why people are voting to close this!!!!!!!!\n- Disagree on this question being off-topic. The question asks a subtle detail about how a certain syntax can be interpreted by implementations and it can be conclusively answered by citing the relevant standardese quotes. *Efforts on trying to solve the problem* doesn't apply here. Understanding or hunting down standardese quotes is not really a trivial task.\n- There are two function calls, one to `sum` and one to `printf`.\n- en.wikipedia.org/wiki/Occurs_check the compiler converts the C code to symbols and then performs an occurs check\n- I once had some C code behave strange because I was doing a division by an integer via a pointer. ie, the expression was `a&#47;*b`. It was fixed by adding some whitespace: `a &#47; *b`\n- @Stewart: I do not understand what do you want to say?\n- Just that a compiler, like any software, is simply a machine following rules. In my example, the rule that `&#47;*` means 'start comment' is senior to `&#47;` means 'division', `*` means dereference pointer. With your example it'll be something equally simple, such as `,` inside `()` means 'argument separator'. That's all.\n- &#171;@SazzadurRahaman; comma in the function call&#187;: but they're both function calls! `:P`\n- True but not an answer to the question.\n- @Yu : I did't want to use comma operator. I just want to know how compiler know that it is not a comma operator!\n- @sasha.sochka See the OP's comment. He wants to know how parsers work, not how to use a comma in a function call.\n- @haccks Got it, edited my words. Using a comma operator as the function parameter is not practically useful, but knowing how to use it is still interesting, so I'll keep this part though.\n- @YuHao; Thanks dude! at least. And also thanks for edit to my post.\n- my kids don't take that for an answer why should the OP... but that is the reason, because the ambiguous case is prohibited.\n- I had forgotten that there is a technical term with that name. I merely mean that any given token can only be understood in the context in which it appears. In other words, I'm using \"context sensitive\" as and adjective rather than a noun. However, I suspect that the only people confused by this were people who already knew the answer!\n- This is a good answer but you should also mention that the *things between the commas* are `assignment-expression` nonterminals rather than `expression` nonterminals (as discussed in Jens' answer), thus disallowing `,` at top level of a `parameter-list` from being the comma operator. If the standard did what you describe without also doing this, the overall grammar would be ambiguous.\n- @Zack, quite so. I've expanded the answer with that info.\n- @EricLippert: I don't think it makes sense to say C has a context-free grammar. If you go that direction, then you could also claim C++ has a CFG (since, just like in C's case, it's ambiguous and requires a semantic pass to reject invalid programs). If you want to be really strict then you could also claim most programming languages do *not* have CFGs because they all require declarations before definitions before the program is deemed valid, which isn't context-free. Neither is a very useful definition since it puts most languages in the same category. (cont'd)\n- @EricLippert: (cont'd) ... from a practical standpoint (maybe not so much on the theory side) I feel a useful definition would be that C is context-free iff it has a CFG that unambiguously parses all valid C programs *assuming there are no undeclared identifiers*. But in that case, C is not context-free (and thus has no CFG) because of the classic `T * T;` ambiguity, which requires knowing what `T` *is* (and not merely whether it's declared). Hence I don't think it makes sense to say C is context-free.\n- @Mehrdad: I take your point, but you are using \"grammar\" in a broader sense than it is usually construed. The famous sentence \"Colourless green ideas sleep furiously.\" is *grammatical* in English but it is *nonsensical*, and the sentence \"Bob Smith is the king of England.\" is *grammatical* but *false*. The *grammar of the C language* does not intend to be one-stop-shopping for determining what is a *legal* C program any more than the grammar of English determines what is a true statement.\n- @EricLippert: Thanks for the response. I'm not sure I understand what you mean though. If a program is syntactically valid C, then the grammar must parse it correctly -- otherwise it's not the grammar of C. Ditto with the converse. If you define the grammar to be something that accepts syntactically invalid C programs as well, then how do you define context-free-ness? It's *always* possible to make an overly-broad CFG for a CSL (just accept every \"tricky\" string and leave the rest to semantic analysis...). What's your definition and what *would* it classify as context-sensitive, if not C? Why?\n- @haccks: a conditional-expression or a unary-expression followed by an assignment-operator followed by an assignment-expression.\n- I did't get your point please elaborate.It should be appreciated\n- To expand a bit on @Jens answer: let's change the problem and simplify it. Instead of \"expressions\" let's have golf balls (painted yellow) and also big clear plastic balls that can be opened up and have stuff stuck inside them: `(` stuff `)`. The grammar says, in effect, that you may have yellow golf balls, which are automatically separated. Or, you may provide a clear ball *as long you've used both halves*. The clear ball works as a unit, it can't be opened up and separated. So: f( (a,b), g ) has one \"clear ball\" (a,b) and one \"yellow ball\" g and hence exactly two balls, er, arguments.\n- I ran out of comment room, so, continued, and back to the real C grammar: the parentheses allow you to escape out to a \"full blown\" expression, where commas are comma expression parts. Until you have an \"extra\" open parenthesis, though, you're in this more limited \"assignment-expression\" sub-grammar (like the \"yellow golf balls\" idea), where commas are simply not allowed. If the parser comes across a comma in this context, it has to stop and finish the assignment-expression. This works because `(` \"finishes off\" with `)`: the bracketing ends the full expression context.\n- @torek; I did't get your line: ` *as long you've used both halves* `(sorry for my bad English).\n- @torek and also line:` *the parentheses allow you to escape out to a \"full blown\" expression, where commas are comma expression parts* `\n- Hm, I don't have any other natural language to express this. Consider `{` … `}`, `[` … `]`, and `(` … `)`. They \"match up\": if you write `a[3}` it's obviously wrong. If you write `a[(3]` it's still obviously wrong. `(` is ended only by the matching `)`. That \"closes off\" the whole sequence, making it clear what goes with what.\n- The question is about C. Talking about \"parser\" in the context of C++ is unnecessary complication.\n- @anatolyg - Fixed. I must have C++ on the brain at the moment.","metadata":{"transformedAt":"2026-08-18T18:32:17.762Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":22,"totalLines":182,"estimatedTokens":2376}}129{"id":"stack-34367781","source":"stackoverflow","questionId":34367781,"title":"Why do C and C++ compilers place explicitly initialized and default initialized global variables in different segments?","tags":["c++","c","variables","memory-management","compiler-construction"],"text":"Title: Why do C and C++ compilers place explicitly initialized and default initialized global variables in different segments?\nTags: c++, c, variables, memory-management, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI was reading this great post about memory layout of C programs. It says that default initialized global variables resides in the BSS segment, and if you explicitly provide a value to a global variable then it will reside in the data segment.\n\nI've tested the following programs in C and C++ to examine this behaviour.\n\n```\n#include \n// Both i and s are having static storage duration\nint i; // i will be kept in the BSS segment, default initialized variable, default value=0\nint s(5); // s will be kept in the data segment, explicitly initialized variable,\nint main()\n{\n std::coutOutput:\n\n```\n0x488020 0x478004\n```\n\nSo, from the output it clearly looks like both variable i & s resides in completely different segments. But if I remove the initializer (initial value 5 in this program) from the variable S and then run the program, it gives me the below output.\n\nOutput:\n\n```\n0x488020 0x488024\n```\n\nSo, from the output it clearly looks like both variables i and s resides in the same (in this case BSS) segment.\n\nThis behaviour is also the same in C.\n\n```\n#include \nint i; // i will be kept in the BSS segment, default initialized variable, default value=0\nint s=5; // s will be kept in the data segment, explicitly initialized variable,\nint main(void)\n{\n printf(\"%p %p\\n\",(void*)&i,(void*)&s);\n}\n```\n\nOutput:\n\n```\n004053D0 00403004\n```\n\nSo, again we can say by looking at the output (means examining the address of variables), both variable i and s resides in completely different segments. But again if I remove the initializer (initial value 5 in this program) from the variable S and then run the program it gives me the below output.\n\nOutput:\n\n```\n004053D0 004053D4\n```\n\nSo, from the output it clearly looks like both variables i and s resides in the same (in this case BSS) segment.\n\nWhy do C and C++ compilers place explicitly initialized and default initialized global variables in different segments? Why is there a distinction about where the global variable resides between default initialized and explicitly initialized variables? If I am not wrong, the C and C++ standards never talk about the stack, heap, data segment, code segment, BSS segment and all such things which are implementation-specific. So, is it possible for a C++ implementation to store explicitly initialized and default initialized variables in the same segments instead of keeping it in different segments?\n\n========================================\n\nTop Answer:\nThe really short answer is \"because it takes up less space\". (As noted by others, the compiler doesn't have to do this!)\n\nIn the executable file, the `data` section will contain data that has its value store in the relative place. This means for every byte of initialized data, that data section contains one byte. \n\nFor zero-initialized globals, there is no reason to store a lot of zeros. Instead, just store the size of the whole set of data in one single size-value. So instead of storing 4132 bytes of zero in the `data` seciton, there is just a \"BSS is 4132 bytes long\" - and it's up to the OS/runtime to set up so that it is zero. - in some cases, the runtime of the compiler will `memset(BSSStart, 0, BSSSize)` or similar. In for example Linux, all \"unused\" memory is filled with zero anyway when the process is created, so setting BSS to zero is just a matter of allocating the memory in the first place. \n\nAnd of course, shorter executable files have several benefits: Less space taken up on your hard-disk, faster loading time [extra bonus if the OS pre-fills the allocated memory with zero], faster compile time as the compiler/linker doesn't have to write the data to disk.\n\nSo there is an entirely practical reason for this.\n\n========================================\n\nCode:\n```text\n#include <iostream>\n// Both i and s are having static storage duration\nint i;     // i will be kept in the BSS segment, default initialized variable, default value=0\nint s(5);  // s will be kept in the data segment, explicitly initialized variable,\nint main()\n{\n    std::cout<<&i<<' '<<&s;\n}\n```\n\n```text\n0x488020 0x478004\n```\n\n```text\n0x488020 0x488024\n```\n\n```text\n#include <stdio.h>\nint i;      // i will be kept in the BSS segment, default initialized variable, default value=0\nint s=5;    // s will be kept in the data segment, explicitly initialized variable,\nint main(void)\n{\n    printf(\"%p %p\\n\",(void*)&i,(void*)&s);\n}\n```\n\n```text\n004053D0 00403004\n```\n\n```text\n004053D0 004053D4\n```\n\n```text\ndata\n```\n\n```text\ndata\n```\n\n```text\nmemset(BSSStart, 0, BSSSize)\n```\n\n========================================\n\nComments:\n- .bss: \"Typically only the length of the bss section, but no data, is stored in the object file ... Operating systems may use a technique called zero-fill-on-demand to efficiently implement the bss segment\"\n- Feel privileged... I once had to use a compiler that would place static variables in `.data` if they had `= 0` initializer, and in `.bss` if they had no initializer. And also had to use the same codebase on another (broken) compiler that did not zero-initialize static variables that had no initializer.\n- Trivia quiz: what does BSS mean?\n- It stands for Block Started by Symbol.","metadata":{"transformedAt":"2026-08-18T18:32:17.762Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":134,"estimatedTokens":1346}}130{"id":"stack-497992","source":"stackoverflow","questionId":497992,"title":"Why are Virtual Machines necessary?","tags":["compiler-construction","vm-implementation"],"text":"Title: Why are Virtual Machines necessary?\nTags: compiler-construction, vm-implementation\nSource: Stack Overflow\n\nQuestion:\nI was reading this question to find out the differences between the Java Virtual Machine and the .NET CLR and Benji's answer got me wondering why Virtual Machines are necessary in the first place.\n\nFrom my understanding of Benji's explanation, the JIT compiler of a Virtual Machine interprets the intermediate code into the actual assembly code that runs on the CPU. The reason it has to do this is because CPUs often have different numbers of registers and according to Benji, \"some registers are special-purpose, and each instruction expects its operands in different registers.\" This makes sense then that there is a need for an intermediary interpreter like the Virtual Machine so that the same code can be run on any CPU.\n\nBut, if that's the case, then what I don't understand is why C or C++ code compiled into machine code is able to run on any computer as long as it is the correct OS. Why then would a C program I compiled on my Windows machine using a Pentium be able to run on my other Windows machine using an AMD?\n\nIf C code can run on any CPU then what is the purpose of the Virtual Machine? Is it so that the same code can be run on any OS? I know Java has VM versions on pretty much any OS but is there a CLR for other OS's besides Windows?\n\nOr is there something else I'm missing? Does the OS do some other interpretation of assembly code it runs to adapt it to the particular CPU or something?\n\nI'm quite curious about how all this works, so a clear explanation would be greatly appreciated.\n\nNote: The reason I didn't just post my queries as comments in the JVM vs. CLR question is because I don't have enough points to post comments yet =b.\n\nEdit: Thanks for all the great answers! So it seems what I was missing was that although all processors have differences there is a common standardization, primarily the X86 architecture, which provides a large enough set of common features so that the C code compiled on one X86 processor will work for the most part on another X86 processor. This furthers the justification for Virtual Machines, not to mention I forgot about the importance of garbage collection.\n\n========================================\n\nTop Answer:\nYour assumption that C code can run on any processor is incorrect. There are things like registers and endianness which will make compiled C programs not work at all on one platform, while it might work on another.\n\nHowever, there are certain similarities that processors , for example, Intel x86 processors and AMD processors a large enough set of properties that most code compiled against one will run on the other. However, if you want to use processor-specific properties, then you need a compiler or set of libraries which will do that for you.\n\nAs for why you would want a virtual machine, beyond the statement that it will handle differences in processors for you, there is also the fact that virtual machines offer services to code that are not available to programs compiled in C++ (not managed) today.\n\nThe most prominent service offered is garbage collection, offered by the CLR and the JVM. Both of these virtual machines offer you this service for free. They manage the memory for you.\n\nThings like bounds checking, access violations (while still possible, they are extremely difficult) are also offered.\n\nThe CLR also offers a form of code security for you.\n\nNone of these are offered as part of the basic runtime environment for a number of other languages which don't operate with a virtual machine.\n\nYou might get some of them by using libraries, but then that forces you into a pattern of use with the library, whereas in .NET and Java services that are offered to you through the CLR and JVM are consistent in their access.\n\n========================================\n\nComments:\n- Ah I see, so that's the bit of info I was missing. So then the number of registers and other primary CPU details that are important to the assembly code are standard across all X86 processors?\n- you won't need different compiler but you will still need different virtual machine that will run on given platform.\n- What about a method that calls itself recursively and blows the call stack.\n- @mP: It won't overflow the stack - c# will cease execution before any other memory is overwritten, so a stackoverflow becomes an out of stack error.\n- Ok ok, I meant a the classic virus hack where you overwrite the return address on the stack - is it possible in C#?\n- BTW can someone educate me as too why my answer's not upto scratch\n- Most operating systems do a good job of enforcing security controls on regular compiled code. On UNIX-like systems a C program can't change the bootsector unless it has permissions to do so. Furthermore you can avoid stack overflows etc by using a compiled language that implements proper safeguards.\n- and now .NET framework has been open sourced. MSVC can compile apps for Android, iOS or other platforms, too\n- Ah nice point, I overlooked the garbage collection feature. That alone is probably enough justification for me! Memory leaks are a pain...\n- Garbage collection is not a monopoly of virtual machines. There are libraries that do that also for compiled languages.\n- actually garbage collection is not the main reason for having virtual machines... main reasons are code access security and just-in-time compiling.\n- @wdu & lubos hasko: If you read the response closer, you will see that I said it is not the exclusive domain of virtual machines, but rather, the virtual machines offer their services in a consistent way, whereas in other environments you have different models which aren't easily interchangable.\n- Too bad I can't mark second best answer... or third for that matter. Actually there were a lot of very good answers to this question, each with something the other answers didn't focus on. Anyways yours was best until I read Adam's detailed explanation. Still, great answer!\n- Wow interesting, I didn't know the JIT could optimize the code to run faster like that!\n- Aw, SO didn't notify me others were posting. I thought I had this question all to myself! ;-P\n- Yeah, we need some type of AJAX window to let us know who we're racing against! haha\n- Wow your answer is great! Casper's answer was great too but I'm going to give the best to you because yours was so detailed. Thanks, I learned a lot!\n- @Adam: I have numerous programs of varying complexity written in Java that run equally well on numerous operating systems and hardware platforms - so I would contend that Java comes very close to solving this problem; far closer than CLI does (with only Windows and almost Linux).\n- @Software Monkey, Java is a great language, but the last time I used it I couldn't use serial ports reliably across architectures, sound and 3D graphics were problematic - even generic apps weren't as expressive as native apps. .NET is less portable, though. Native apps still win over either.\n- Hmm, thanks for the microcode explanation. I didn't realize that even the assembly is interpreted!\n- @Daniel: The term \"interpreted\" doesn't really quite fit, for a couple of reasons. First of all, even in microcode-based processors, the \"microcode instruction set\" will often include instructions which are designed around the document instruction set. For example, on the 8088, the main microcode for the \"ADD ,\" instruction set would likely be three instructions: \"load register operand to op1; load EA operand to op2 and put op1+op2 into res1; store res1 and prepare next instruction\". To execute `ADD SI,BX` would take three steps, as indicated above.\n- For a more complicated instruction like \"ADD [SI+BX+12],DX\", the \"load EA to op2\" would trigger a nested sequence, something like \"load SI to op1; load next instruction byte to op2 and put op1+op2 into res2; load op1 with res2 and op2 with BX, and put op1+op2 into res2; fetch memory at address res2 to op2\". The \"store res1\" would, rather than writing to a register, store memory at address res2.\n- @Daniel: On newer machines, the term \"interpreted\" is even less applicable, since the interpreters generally examine one instruction, decide what to do, examine the next, etc., but modern machines translate instructions which are fetched from memory into a form which can run more efficiently. This translated form may be \"interpreted\" in a sense somewhat like the 8086 code above, but the \"interpreter\" being able to execute several simultaneous instructions in a single step.\n- Thanks for your answer. Understanding that native code compilers and VMs both use intermediate languages, they just compile to machine language at different times, makes the overall picture much more clear.","metadata":{"transformedAt":"2026-08-18T18:32:17.763Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":67,"estimatedTokens":2197}}131{"id":"stack-5832412","source":"stackoverflow","questionId":5832412,"title":"Compiling an AST back to source code","tags":["php","compiler-construction","abstract-syntax-tree"],"text":"Title: Compiling an AST back to source code\nTags: php, compiler-construction, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI'm currently in the process of building a PHP Parser written in PHP, as no existing parser came up in my previous question. The parser itself works fairly well.\n\nNow obviously a parser by itself does little good (apart from static analysis). I would like to apply transformations to the AST and then compile it back to source code. Applying the transformations isn't much of a problem, a normal Visitor pattern should do.\n\nWhat my problem currently is, is how to compile the AST back to source. There are basically two possibilities I see:\n\n- Compile the code using some predefined scheme\n\n- Keep the formatting of the original code and apply 1. only on Nodes that were changed.\n\nFor now I would like to concentrate on 1. as 2. seems pretty hard to accomplish (but if you got tips concerning that, I would like to hear them).\n\nBut I'm not really sure which design pattern can be used to compile the code. The easiest way I see to implement this, is to add a `->compile` method to all Nodes. The drawback I see here, is that it would be pretty hard to change the formatting of the generated output. One would need to change the Nodes itself in order to do that. Thus I'm looking for a different solution.\n\nI have heard that the Visitor pattern can be used for this, too, but I can't really imagine how this is supposed to work. As I understand the visitor pattern you have some `NodeTraverser` that iterates recursively over all Nodes and calls a `->visit` method of a `Visitor`. This sounds pretty promising for node manipulation, where the `Visitor->visit` method could simply change the Node it was passed, but I don't know how it can be used for compilation. An obvious idea would be to iterate the node tree from leaves to root and replace the visited nodes with source code. But this somehow doesn't seem a very clean solution?\n\n========================================\n\nCode:\n```text\n->compile\n```\n\n```text\nNodeTraverser\n```\n\n```text\n->visit\n```\n\n```text\nVisitor\n```\n\n```text\nVisitor->visit\n```\n\n```text\nPrettyPrintBlock:\n     Print(\"{\"}; PrintNewline();\n     Call PrettyPrint(Node.children[1]); // prints out statements in block\n     Print(\"}\"); PrintNewline();\n     return;\n\n\n PrettyPrintStatements:\n     do i=1,number_of_children\n         Call PrettyPrint(Node.children[i]); Print(\";\"); PrintNewline(); // print one statement\n     endo\n     return;\n```\n\n```text\nPrettyPrintBlock:\n     Box1=PrimitiveBox(\"{\"); Box2=PrimitiveBox(\"}\");\n     ChildBox=PrettyPrint(Node.children[1]); // gets box for statements in block\n     ResultBox=VerticalBox(Box1,Indent(3,ChildBox),Box2);\n     return ResultBox;\n\nPrettyPrintStatements:\n     ResultBox=EmptyBox();\n     do i=1,number_of_children\n         ResultBox=VerticalBox(ResultBox,HorizontalBox(PrettyPrint(Node.children[i]); PrimitiveBox(\";\")\n     endo\n     return;\n```\n\n```text\nblock = '{' statements '}' ; -- grammar rule to recognize block of statements\n<<PrettyPrinter>>: { V('{',I(statements),'}'); };\n```\n\n========================================\n\nComments:\n- Thanks for that pretty extensive description, I will try those tips in the next few days. PS: The Simlpe Language Example link gives me a 404.\n- @nikic: It was wrong on my first submission but I corrected it. Try again.\n- Okay, Thanks for your help Ira :) I managed to implement the pretty printer (it took quite some time to get rid of many many edge-case bugs). Though it doesn't retain any whitespace or comment information. I figured that it would be to difficult to implement. You can find the resulting package over at github: github.com/nikic/PHP-Parser :) Thanks again!\n- Losing people's comments is pretty much a sure fire way to get them to reject your prettyprinted code. Just sayin'.\n- The process of converting an AST back to source IS NOT called pretty printing; \"Pretty printing\" includes both using a parser to convert the code to an AST, and then convert back that AST to source again, following some kind of convention of spaces, indentation, etc. (and actually, if you don't use an AST, and just use regexp to add spaces, etc, that counts as pretty printing too!) **To refer to only the process of \"returning an AST to source\" (with pretty indentation or without it) is called \"unparsing\", and the tool to do that is called Unparser**\n- Sorry if you don't like my definition. I use the term prettyprinting to include any AST to text conversion, whether that forces a standard layout, preserves the layout of the original, or mixes the two together in different parts of the tree for the same language or mixes the two because the tree is a hybrid tree containing subtrees of one language with arbitrary subtrees of another (and yes, I have machinery to do all of that seamlessly). I'm not pleased with \"unparsing\" ; while what it does is logically inverse of parsing, it doesn't actually do any parsing at all so the term confuses.\n- Your reference, Wikipedia, certainly defines the term, using references from 1991. My use of the term goes back to Draco, which defined the term before 1983. See semanticdesigns.com/Company/Publications/&hellip; So that's been a standard term in my business for a long, long time.\n- @IraBaxter Well, is not that I *\"dislike\" your definition*, it's just that it seems to be the wrong term for this. If you check the article for Prettyprint, you'll notice the concept is about \"stylistic formatting\" an existing code, and is **not** about returning an AST back to source. I must mention that I'm not a big fan of the term \"unparser\", but is the only term I have found so far that actually refers to the *very specific process of translating an AST to source*, and to **nothing else**.\n- There is another term: escodegen, a project that converts an AST generated by \"esprima\" back to source, calls itself **\"code generator\"**, however, that term has a definition quite broad; code generation is the process of taking an AST and converting it to *something*, usually machine code. By definition it could be indeed back to source, so I guess the term could be used instead. But \"unparser\" would be a \"code generator that converts back to source\", so is more specific.\n- Somehow I think we will survive the vocabulary war. Thanks for your opinion.\n- Amazing answer. This answer is a must read for anyone climbing up the learning curve of grammar parsing. Well done.","metadata":{"transformedAt":"2026-08-18T18:32:17.763Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":8,"totalLines":93,"estimatedTokens":1615}}132{"id":"stack-405770","source":"stackoverflow","questionId":405770,"title":"Why are compilers so stupid?","tags":["performance","language-agnostic","compiler-construction"],"text":"Title: Why are compilers so stupid?\nTags: performance, language-agnostic, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI always wonder why compilers can't figure out simple things that are obvious to the human eye. They do lots of simple optimizations, but never something even a little bit complex. For example, this code takes about 6 seconds on my computer to print the value zero (using java 1.6):\n\n```\nint x = 0;\nfor (int i = 0; i It is totally obvious that x is never changed so no matter how often you add 0 to itself it stays zero. So the compiler could in theory replace this with System.out.println(0).\n\nOr even better, this takes 23 seconds:\n\n```\npublic int slow() {\n String s = \"x\";\n for (int i = 0; i First the compiler could notice that I am actually creating a string s of 100000 \"x\" so it could automatically use s StringBuilder instead, or even better directly replace it with the resulting string as it is always the same. Second, It does not recognize that I do not actually use the string at all, so the whole loop could be discarded!\n\nWhy, after so much manpower is going into fast compilers, are they still so relatively dumb?\n\n**EDIT**: Of course these are stupid examples that should never be used anywhere. But whenever I have to rewrite a beautiful and very readable code into something unreadable so that the compiler is happy and produces fast code, I wonder why compilers or some other automated tool can't do this work for me.\n\n========================================\n\nTop Answer:\nIn my opinion, I don't believe it's the job of the compiler to fix what is, honestly, bad coding. You have, quite explicitly, told the compiler you want that first loop executed. It's the same as:\n\n```\nx = 0\nsleep 6 // Let's assume this is defined somewhere.\nprint x\n```\n\nI wouldn't want the compiler removing my `sleep` statement just because it did nothing. You may argue that the sleep statement is an explicit request for a delay whereas your example is not. But then you will be allowing the compiler to make very high-level decisions about what your code should do, and I believe that to be a bad thing.\n\nCode, and the compiler that processes it, are tools and you need to be a tool-smith if you want to use them effectively. How many 12\" chainsaws will refuse to try cut down a 30\" tree? How many drills will automatically switch to hammer mode if they detect a concrete wall?\n\nNone, I suspect, and this is because the cost of designing this into the product would be horrendous for a start. But, more importantly, you shouldn't be using drills or chainsaws if you don't know what you're doing. For example: if you don't know what kickback is (a very easy way for a newbie to take off their arm), stay away from chainsaws until you do.\n\nI'm all for allowing compilers to *suggest* improvements but I'd rather maintain the control myself. It should not be up to the compiler to decide unilaterally that a loop is unnecessary.\n\nFor example, I've done timing loops in embedded systems where the clock speed of the CPU is known exactly but no reliable timing device is available. In that case, you can calculate precisely how long a given loop will take and use that to control how often things happen. That wouldn't work if the compiler (or assembler in that case) decided my loop was useless and optimized it out of existence.\n\nHaving said that, let me leave you with an old story of a VAX FORTRAN compiler that was undergoing a benchmark for performance and it was found that it was *many* orders of magnitude faster than its nearest competitor.\n\nIt turns out the compiler noticed that the result of the benchmark loops weren't being used anywhere else and optimized the loops into oblivion.\n\n========================================\n\nCode:\n```java\nint x = 0;\nfor (int i = 0; i < 100 * 1000 * 1000 * 1000; ++i) {\n    x += x + x + x + x + x;\n}\n```\n\n```java\nSystem.out.println(x);\n```\n\n```java\npublic int slow() {\n   String s = \"x\";\n   for (int i = 0; i < 100000; ++i) {\n       s += \"x\";\n   }\n   return 10;\n}\n```\n\n```text\n#include <stdio.h>  /* printf() */\n\nint factorial(int n) {\n   return n == 0 ? 1 : n * factorial(n - 1);\n}\n\nint main() {\n   int n = 10;\n\n   printf(\"factorial(%d) = %d\\n\", n, factorial(n));\n\n   return 0;\n}\n```\n\n```text\nfactorial:\n   .LFB13:\n           testl   %edi, %edi\n           movl    $1, %eax\n           je  .L3\n           .p2align 4,,10\n           .p2align 3\n   .L4:\n           imull   %edi, %eax\n           subl    $1, %edi\n           jne .L4\n   .L3:\n           rep\n           ret\n```\n\n```text\nmain:\n   .LFB14:\n           subq    $8, %rsp\n   .LCFI0:\n           movl    $3628800, %edx\n           movl    $10, %esi\n           movl    $.LC0, %edi\n           xorl    %eax, %eax\n           call    printf\n           xorl    %eax, %eax\n           addq    $8, %rsp\n           ret\n```\n\n```text\n-O1\n```\n\n```text\nfactorial()\n```\n\n```text\n-O2\n```\n\n```text\n-O3\n```\n\n```text\nmain()\n```\n\n```text\nmovl $3628800, %edx\n```\n\n```text\nfactorial(10)\n```\n\n```text\nfactorial()\n```\n\n```text\nint x = 1;\nint y = 1;\nint z = x - y;\nfor (int i = 0; i < 100 * 1000 * 1000 * 1000; ++i) {\n    z += z + z + z + z + z;\n}\nSystem.out.println(z);\n```\n\n```text\nif\n```\n\n```text\n+=\n```\n\n```text\n+=\n```\n\n```text\nx = 0\nsleep 6 // Let's assume this is defined somewhere.\nprint x\n```\n\n```text\nsleep\n```\n\n```text\njavac\n```\n\n```text\nscalac\n```\n\n```text\ngroovyc\n```\n\n```text\ngcc\n```\n\n```text\n\"starting windows\"\n\"enjoy freecell/solitaire\"\n\"shutting down windows\"\n```\n\n```text\nextern int x; // defined elsewhere\nfor (int i = 0; i < 100 * 1000 * 1000 * 1000; ++i) {\n    x += x + x + x + x + x;\n}\nreturn x;\n```\n\n```text\nextern volatile int x; // defined elsewhere\nfor (int i = 0; i < 100 * 1000 * 1000 * 1000; ++i) {\n    x += x + x + x + x + x;\n}\nreturn x;\n```\n\n```text\n[js@HOST2 java]$ gcj --main=Optimize -O2 Optimize.java\n[js@HOST2 java]$ ./a.out\n0\n[js@HOST2 java]$\n```\n\n```text\nclass Optimize {\n    private static int doIt() {\n        int x = 0;\n        for (int i = 0; i < 100 * 1000 * 1000 * 1000; ++i) {\n            x += x + x + x + x + x;\n        }\n        return x;\n    }\n    public static void main(String[] args) {\n        for(int i=0;i<5;i++) {\n            doIt();\n        }\n    }\n}\n```\n\n```text\n1       java.lang.String::hashCode (60 bytes)\n1%      Optimize::doIt @ 4 (30 bytes)\n2       Optimize::doIt (30 bytes)\n```\n\n```text\nx\n```\n\n```text\ngcj\n```\n\n```text\n0\n```\n\n```text\njava -XX:+PrintCompilation Optimize\n```\n\n```text\n#include <stdio.h>\nint main()\n{\n    int x = 0;\n    for (int i = 0; i < 100 * 1000 * 1000 * 1000; ++i) {\n        x += x + x + x + x + x;\n    }\n    printf(\"%d\", x);\n}\n```\n\n```text\n10 FOR X = 1 TO 1000\n20 NEXT : REM 1-SECOND DELAY\n```\n\n```text\ndelay\n```\n\n```text\n10FORX=1TO1000:NEXT\n```\n\n```text\nx += x + x + x + x + x\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n========================================\n\nComments:\n- It seems you might get fewer downvotes or answers focusing on your example code if you used some \"real-world\" examples instead of good ones. :-)\n- javac doesn't do any kind of optimizations to the bytecode anymore, I heard that in java 1.4 it used to do some but the issue was that since optimizations easily go from trivial to platform specific, Sun decided to remove all optimizations. Java could use a precompiler stage for these...\n- That second one takes less than a second in Perl, no matter how I translate it.\n- Plus `100 * 1000 * 1000 * 1000` is optimized to 100000000000\n- @Brad - I'm betting Perl implements string concatenation more efficiently than Java. i.e. StringBuilder... try that in Java and compare the results.\n- You wrote that code with the intention for it to happen, why would the compiler remove it? Maybe you use for loops to stall you program, or you would just want to test filling the stack, ...\n- Because the computers are built to obey, not to think.\n- You're aware that the JVM performs bytecode optimisation, right?\n- Compilers aren't meant to make optimizations that are \"obvious to the human eye\". That's what programmers are for. The compiler is meant to optimize the generated code beyond what a human can do without an unreasonable amount of effort.\n- Nothing is more frustrating than technology that tries to be too smart. (Auto-spelling correction is one example)\n- If you know how to do these kinds of compiler optimisations and be 100% confident that you won't contravene the intentions of the programmer that wrote the code, then I recommend you pursure a career as a compiler developer. If you don't know how to do this, then I think it's very unfair to \"ask why are compilers (and their developers, by proxy), so stupid\"\n- Because compilers are written by programmers, and if they could do everything programmers could do the people that wrote the compilers would be out of a job.\n- Computers do what they are told.. sooooooo\n- I want to write the readable code, and not code that makes the compiler happy. These are just stupid simple examples to show that the compiler is dumb, nothing I would use in a real program.\n- \"but that just proves my point\": How does it prove your point? The question is a general one about why there are still things that are obvious to the human eye and not to the compiler, and you pick fault with the (obviously exaggerated for effect) examples instead.\n- The point is that if the examples weren't exaggerated for effect, they wouldn't exemplify the point of the question, which I went on to explain - you either write horrible code on purpose because you're trying to, or you do it by accident because you make a mistake or don't know what you're doing.\n- Not that I agree, but he is saying that it proves his point because these are contrived examples and not snippets of \"real\" code. If someone could provide real, production code that was similarly (mis-)treated by the compiler, that would be a counterexample that undermines his argument.\n- ^^ Neither of those is any fault of the compiler, and even if it could, I wouldn't want my compiler \"fixing\" things like that.\n- Understood. :) Of course these are not real-world examples, but simply pointing that out is not an actual answer. I also don't understand 'I wouldn't want my compiler \"fixing\" things like that' -- why not? Given that good compilers *do* fix things like that, do you now want to move to inferior ones?\n- @ShreevatsaR: See Pax's answer, he/she said it much better than I did.\n- Im not sure this is a good way to look at it, this way we might still be stuck with writing assembler code.\n- Compilers having optimizations helps a lot, For example you can just write stuff in a loop and the compiler can use the SSE instructions if they are available, or can just compile the loop normally otherwise. To do this manually you'd need to write platform specific code for every CPU version.\n- Compilers work in several passes, and the constant folding + constant propagation pass(es) will reduce your example to the one posted by the OP, so the code required to perform this optimization is the same for both cases.\n- Memory allocation/management happens to be one of those things Java programmers take for granted.\n- I think most C++ compilers will eliminate the addition, but not the loop, nor the assignment. Assigning to the stack causes a side effect. Eliminating the side effect could potentially change the meaning of the program, and would therefore not be correct.\n- Scott, I tried it with the latest GCC. It removes the entire calculation including the loop. All that remains is a printf call and the surrounding code that pushes a zero onto the stack.\n- Ok. I had tried both GCC and VC++ with -O3 and /Ox, and they left the loop there. I don't think I was running the latest version of GCC though. I would think that you could make an argument against that being a safe optimization in C++. For Java, or Haskell, it would be safe, but not for C.\n- I would imagine that in order to get it to work right in C++ they must be doing some sort of aliasing analysis, which is pretty advanced.\n- Scott, You're right. I just checked VS2008 and it does not do the optimization. My GCC (4.3.1) however does the trick. The C-standard does btw not mention the stack at all, so it's valid to do any optimizations that change stack-allocation patterns.\n- Excellently useful comments :)\n- @Scott: No fancy aliasing analysis is needed. Nothing can alias a local variable whose address is never taken.\n- \"Your second example can't be optimized in most languages because...\" To me, this paragraph suggests a misunderstanding of what side effects are.\n- As of C++14, a C++ compiler can eliminate any and all memory allocations it wants, as well as group multiple requests into a single request, as long as the total memory requested after optimization is not greater than the sums of all user requests. This would allow trivial optimization of the second example as well. See open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3664.html\n- Upvoted for the second part. It is much more fulfilling to make well-written code go faster by hitting some machine idiom than to optimize away badly written code.\n- In C# release mode it isnt optimized out. In C++ it is (code in my post)\n- javac does not optimize\n- Put much better than my answer, +1 :)\n- A 14\" chainsaw is capable of cutting down a 22\" tree, you just have to cut from both sides ;-)\n- -1 one for not distinguishing between standardised language definition and implementation defined run-time behaviour (== performance). Compilers are mostly allowed to emit whatever code they want to, as long as the *meaning* doesn't change.\n- Most compilers optimize loops away that are not used. I believe the compilers try to be as smart as possible, but they simply can't, for technical reasons.\n- @DavidS, having worked on a lot of embedded systems where timing loops are important, I'd prefer the compiler to suggest I can remove them for greater performance but not have that decision taken out of my hands. Ditto for \"useless\" memory accesses which may actually be memory-mapped I/O.\n- @DavidS (cont), but these are situations where *meaning* includes the speed of execution. That stuff is probably best done by using a lower optimization level, or declaring variables as volatile.\n- If you don't want optimisation, don't use optimisation flags. You clearly don't want optimisation if you are relying on loops that do nothing but are there to take up time. The optimisation compiler is *meant* to speed things up while keeping the functional aspects of the code the same. This is also what 99% of people want. For some reason you want compilers to be designed for your 1% use-case.\n- So does a big for-loop unless it's optimized away :-)\n- @paxdiablo for MMIO there's volatile to tell the compiler it shouldn't optimize away (or change the order of) memory accesses. And delay loops are the one thing I always do in assembly because the exact clock cycle count tends to be important. I.e. neither are good arguments to say the compiler shouldn't, by default remove what it perceives as dead code.\n- @paxdiablo: What about after the optimizer has done several other passes? I'd like to be able to write a generic function with a loop in it, but if the optimizer decides to inline my function into a particular call site and can tell that it can just use some sort of vector instruction because my value is small enough and the data is properly aligned, I would hope that it does so.\n- +1 for reference to the Java HotSpot Technology. Those kind of optimizations are not the compilers job, but rather of the (virtual) machine, since only that knows the context and can verify the outcome.\n- I think they have on the fly stack replacement now, so you don't have to wait to the second call to get the JIT compile code running...\n- javac does no optimizations. Try to compile \"1+1+1+1\" and show the generated JVM assembly instructions\n- Did you just make up the stuff about being predictable?\n- gcc has built-in knowledge of the most common C functions, so that optimizations can be performed when they are used.It is true in general but normally for common types and functions (good) compilers have library knowledge.\n- 1 is really really easy to spot for a compiler. Good compilers will fix it. For the 2nd, yes the compiler needs to have some knowledge of the classes, but \"String\" is a basic type and I think it could be acceptable to have it.\n- @LtWorf: For simple cases. In general, you are running against [en.wikipedia.org/wiki/Rice's_theorem](Rice's theorem).\n- Check gcc, it's done there. I am aware that it can't be done for every function but it does make sense to do it for the common ones.\n- > Sun's javac does have some basic optimizations Does it? Can you name one? I have some experience with it and I never found it optimizing anything at all. Not even constant expressions (which is an extremely trivial optimization)\n- Compilers can generate dead code all by themselves. It's useful to be able to pull it out.\n- If the code is small enough it can fit in the CPU cache and be executed quickly. The larger it is, the more misses it generates. If it's really large it will even generate page misses.\n- Are you sure that gcc's compiler is not capable see the meaning of a method, a loop or a program? -- Jason's example suggests me that some pre-calculations is occurring at compile-time.\n- Too bad we'll never see the Windows source code to test that out :).\n- If you use GCJ you use the same code-generation backend as GCC, so you get more or less the same optimizations. You can also use GCJ to compile to .class files btw. No need to compile to native code.\n- Nils, yeah i'm aware of that. i took the compile-to-machine code explicitly, to see how it behaves in that case (to see whether java possibly *forbids* those optimizations. apparently, it doesn't).\n- Johannes - Genius as always !\n- That's kick-ass! Do you know if gcc does more pre-computation on function calls with constant values? What does gcc do when choosing an n which produces a factorial which is greater than a 64 bit value?\n- I have no idea. It's all black magic to me. :-) Experimentation reveals that larger values will make my version of gcc not attempt pre-computation. If you're interested in playing around with this kind of thing, just compile a program with the -S flag. eg, if you do \"gcc -O3 -S factorial.c\", it will produce a file, factorial.s, which contains the generated assembly. There's probably some other flags to dump whatever gcc's intermediate representation is, although that's more specialized than straight assembly.\n- \"movl $3628800\" suggests me that gcc pre-calculates at least some functions at compile-time. --- When does gcc stop to pre-compute functions at compile-time? --- It is likely that gcc somehow estimates the size of the function's output, and this way, decides whether to pre-compute or not at compile-time.\n- @Jason: Which command did you use to get the machine codes? --- I run unsuccessfully % gcc -O2 code.c %\n- @Masi: you need to use the -S flag. so, eg, \"gcc -O2 -S code.c\" will produce a file named \"code.s\" containing the assembly.\n- I once almost fell off my chair as I tried a few variants of swapping two variable contents (temp variable, xor and arithmetic) and compared how they look in assembly. My main method used constants and with optimizations MSVC emitted simply the `printf` statements with the swapped values (along with the compiled functions but they weren't called anymore). Such kinds of precomputation are pretty nice, indeed.\n- its a simple static optimization(because the variable you do pass don't change), no big deal. Compilers do only cover the cases they are designed for, nothing else, they will never think/abstract/understand the algorithm\n- Wow this is by far one of my favorite answers on SO! Thanks!\n- @Masi @Jason: This is called \"detecting Referential Transparency.\" All decent optimizing compilers do it, and honestly it is not that impressive. *Much* more impressive, to me at least, are the clever ways compilers optimize switch/if statements, sometimes going as far as completely removing the branching!\n- I investigated the performance of some simple C programs compiled with GCC and found it quite easy to beat the performance and instruction count of the assembler GCC generates even though I have never done any x86 assembler before. flyingfrogblog.blogspot.co.uk/2012/04/&hellip;\n- It is ridiculous how this post praises the most simplest optimization that can be done. I guess it goes well with the myth \"the compiler will optimize it\" when it will probably not. Every optimization is there because someone put it there and it only takes place when the heuristics are being hit in the right ways.\n- No, it's because javac doesn't optimize things. Gcc will detect and optimize it.\n- i hope this will never happen, or it should happen if im no more alive :P\n- javac compiler does no optimization at all. It doesn't even replace constants so if you write \"1+1+1+1+1+1+1+1+1+1+1+1+1+1+1\" it generates code for adding \"1\" several times rather than just putting in the resulting constant.\n- That is correct, but there are no function calls inside the 1st loop, so it is really obvious to see what the code does.","metadata":{"transformedAt":"2026-08-18T18:32:17.763Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":41,"totalLines":384,"estimatedTokens":5302}}133{"id":"stack-34892601","source":"stackoverflow","questionId":34892601,"title":"What does the weak_alias function do and where is it defined?","tags":["c","linux","gcc","compiler-construction","gnu"],"text":"Title: What does the weak_alias function do and where is it defined?\nTags: c, linux, gcc, compiler-construction, gnu\nSource: Stack Overflow\n\nQuestion:\nSo I'm looking through the source of gcc compiler and I've come along this in fork.c:\n\n```\nint\n__fork ()\n{\n __set_errno (ENOSYS);\n return -1;\n}\nlibc_hidden_def (__fork)\nstub_warning (fork)\n\nweak_alias (__fork, fork)\n#include \n```\n\nI'm trying to figure out what weak_alias does. I've used the grep command inside the glibc source files to find all occurrences of #define weak_alias:\n\n```\ngrep -r \"#define weak_alias\"\n```\n\nI've found many occurrences of the macro:\n\n```\n#define weak_alias(n, a)\n```\n\nbut the macros don't actually explain anything. They just define that statement they don't show how its being replaced. For example one occurrence is in profil.c:\n\n```\n/* Turn off the attempt to generate ld aliasing records. */\n#undef weak_alias\n#define weak_alias(a,b)\n```\n\nSo any ideas what weak_alias does and where it is being defined?\n\nThanks in advance\n\n========================================\n\nTop Answer:\nIt is a macro that does the following:\n\nIt declares a weak function, if you didnt provide a strong symbol name for that function it will call the function you have laised it to. for example\n\n```\nint _foo(){ return 1;}\n\n//And weak alias\nint __attribute__((weak, alias(\"_foo\"))) foo();\n```\n\nSo if you haven't provided actual implementation for foo it will basically use _foo and return 1.\n\n========================================\n\nCode:\n```text\nint\n__fork ()\n{\n  __set_errno (ENOSYS);\n  return -1;\n}\nlibc_hidden_def (__fork)\nstub_warning (fork)\n\nweak_alias (__fork, fork)\n#include <stub-tag.h>\n```\n\n```text\ngrep -r \"#define weak_alias\"\n```\n\n```text\n#define weak_alias(n, a)\n```\n\n```text\n/* Turn off the attempt to generate ld aliasing records. */\n#undef weak_alias\n#define weak_alias(a,b)\n```\n\n```text\n/* Define ALIASNAME as a weak alias for NAME.\n   If weak aliases are not available, this defines a strong alias.  */\n# define weak_alias(name, aliasname) _weak_alias (name, aliasname)\n# define _weak_alias(name, aliasname) \\\n  extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)));\n```\n\n```text\nint _foo(){ return 1;}\n\n//And weak alias\nint __attribute__((weak, alias(\"_foo\"))) foo();\n```\n\n========================================\n\nComments:\n- Possible duplicate of Need help with glibc source\n- Thank you. Checked the first 10 pages of Google and didn't see this.\n- Exactly what I was looking for. Thank you.","metadata":{"transformedAt":"2026-08-18T18:32:17.763Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":113,"estimatedTokens":622}}134{"id":"stack-1238858","source":"stackoverflow","questionId":1238858,"title":"Can a compiled language be homoiconic?","tags":["compiler-construction","lisp","scheme","interpreter","homoiconicity"],"text":"Title: Can a compiled language be homoiconic?\nTags: compiler-construction, lisp, scheme, interpreter, homoiconicity\nSource: Stack Overflow\n\nQuestion:\nBy definition the word *homoiconic* means: \n\n Same representation of code and data\n\nIn LISP this means that you could have a quoted list and evaluate it, so `(car list)` would be the function and `(cdr list)` the arguments. This can either happen at compile- or at run-time, however it requires an interpreter.\n\nIs it possible that compiled languages without a compile-time interpreter can be homoiconic as well? Or is the concept of homoiconicity limited to interpreters?\n\n========================================\n\nTop Answer:\nyes. lisp can be compiled to a native binary\n\n========================================\n\nCode:\n```text\n(car list)\n```\n\n```text\n(cdr list)\n```\n\n```text\n(EVAL code)  =  (run (compile-expression code))\n```\n\n```text\n&functionName\n```\n\n```text\nsomePtrCastToFnPtr(SomeArgs)\n```\n\n========================================\n\nComments:\n- As a question, do you consider Perl to be homoiconic? It can represent its own code as a string, and has an eval() function.\n- Is it my imagination, or did we have a drive-by downvote of every answer on this page?\n- David: unless you store all Perl data in strings, and call all Perl functions with eval(), then no, I would not consider Perl homoiconic. :-)\n- Assembly is homoiconic.\n- I didn't deny this, but the binary still contains an interpreter.\n- @ott and a CPU contains microcode that is an interpreter of sorts ... everything is at some level, an interpreter.\n- I don't care about the underlying hardware. Let's pretend that the instruction set is hard-wired in silicon.\n- @ott, it doesn't matter ... homoiconicity is an attribute at some defined level of abstraction: You can have a homoiconic language compiled to an arbitary level ... and that level may/may not express homoiconic semantics and so on ...\n- the binary does not need an Interpreter if the code is compiled. Some Lisps don't even haven an interpreter.\n- @Rainer, not even mentioning the LISPM / Lisp-on-a-chip stuff scribd.com/doc/938809/Design-of-a-LISPBased-Microprocessor\n- @ott - in that case how about Machine Language\n- @ott: no, a microprogrammed CPU is not the only possible design; in some CPUs instruction decoding is hardwired for all instructions.\n- @dsm: \"machine language\" is the lowest programming level accessible externally; in a microprogrammed CPU there is another programming level (microcode) only accessible to a CPU maker.\n- Procedural macros are an example of compile-time interpretation.\n- Yes, procedural macros are an example of compile-time evaluation (it's irrelevant whether this is done through interpretation or compilation) -- but such macros can be done completely outside of the object language. For example, you can use any preprocessor language as a first step in compiling code in any other language.\n- I don't get what this had to do with my question.\n- What's vague about \"homoiconic/ity\"? The exact Wikipedia quote says: \"In computer programming, homoiconicity is a property of some programming languages, in which the primary representation of programs is also a data structure in a primitive type of the language itself, from homo meaning the same and icon meaning representation\". That being said, you are right in pointing out that this is not a concept that properly applies to the discussion of compilers and interpreters. What one \"does\" with a language is different from how that language is structured.\n- Why do you exclude Strings? If you exclude Strings, you exclude the very language for which the word \"homoiconic\" was invented in the first place!\n- Excluding strings also disqualifies a number of languages that have a string-based evaluation function, like JavaScript. It just happens that S-expressions are more convenient and less error-prone.\n- The vague part already starts with the word 'representation'. What's that? Text on paper? Bits in a computer? How are representations connected? For me 'Homoiconic' (the name) suggests that the representation is something visual like text on paper/screen/... But then we cross the line between internal and external representations.\n- 'String' is meaningless, since all text-based programming languages have a string representation. In Java you can build a string, write it out compile it, load it the result with the class loader. Does that make Java 'homoiconic'? If yes, the whole concept of 'homoiconicity' is useless. The idea that 'homoiconic' may want to capture is that code has an **internal representation that is structured data** (-> not plain strings).\n- and that these internal data structures itself (not the code, just a data format) have an external representation, that is also used to write programs with. Lisp: internal representation of Lisp is Lisp data. Lisp data has an external representation as S-expressions. S-Expressions are used for program code, too. -> homoiconic\n- @Rainer Joswig - Thanks for that clarification. So, really, you're unhappy with the ambiguity of the word 'representation' combined with a strongly 'visual' take on the meaning of 'homoiconic'. Would you be happier if a definition of 'homoiconic' specified 'internal representation', or is it that 'homoiconic' imposes the visual experience metaphor too strongly for you? I'm asking the question because I have often found that discussions of technical/scientific subjects is clouded by unwanted meaning 'bleeding' out of words, so I like to collect examples of it for pondering the problem.\n- @Pinochle,right, if you look at 'iconicity' in other contexts, it connects form and meaning. This is all vague in the context of programming languages, and the specific definition of 'homoiconicity' is also vague and the term 'homoiconicity' suggests a meaning that does not really match the description. 'icon' is related to an external form, a sign or a visual analogy. The interesting part in Lisp is that Lisp data has an internal and external representation, and that both representations are primary means to encode source code (externally as s-expressions, internally as Lisp data structures).\n- @Rainer Joswig - I just read your response to Jimmy Miller on c.l.l. from Aug. 4. That, coupled with what you said here, has convinced me - -- \"code is data\" is a better way of describing the phenomenon than using \"homoiconicity\" which is far to imagistic and potentially misleading.","metadata":{"transformedAt":"2026-08-18T18:32:17.763Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":69,"estimatedTokens":1616}}135{"id":"stack-2320402","source":"stackoverflow","questionId":2320402,"title":"How to define a grammar for a programming language","tags":["compiler-construction","programming-languages","grammar","context-free-grammar"],"text":"Title: How to define a grammar for a programming language\nTags: compiler-construction, programming-languages, grammar, context-free-grammar\nSource: Stack Overflow\n\nQuestion:\nHow to define a grammar (context-free) for a new programming language (imperative programming language) that you want to design from scratch.\n\nIn other words: How do you proceed when you want to create a new programming language from scratch.\n\n========================================\n\nTop Answer:\nYou'll want to have a look at EBNF (Extended Backus-Naur Form).\n\n(Assuming you want to write a context free grammar, that is.)\n\n========================================\n\nCode:\n```text\nif <- if(exp) block\n```\n\n```text\nif <- if(exp) block else block2\n```\n\n```text\nif <- if exp then block else block end\n```\n\n```text\nif <- if exp then exp end\n```\n\n```text\nif <- if exp then exp (elseif exp)* else exp end\n```\n\n========================================\n\nComments:\n- define for programming? for define for reference?\n- Perhaps if you gave us more information. You mention compiler as a tag. Are you writing a compiler for a new programming language?\n- Bison is completely out of date compared to ANTLR or Racket\n- EBNF is only useful for expressing a CFG, not in actually designing it.\n- OP wants to define a grammar; a question asking how to implement it would surely involve a healthy dose of answers containing lex/yacc (or flex/bison) - in which case, yacc/bison syntax is just a stone's throw from EBNF. Additionally, implementing a language is not the same (on an academic or a practical level) as implementing, say, a linked list. There needs to be a strong theoretical foundation, or all those `shift&#47;reduce` conflicts are going to be baffling. And EBNF is a great place to get your feet wet, IMHO.\n- Can you explain about 3rd step(`make sure that punctuation and tokens from other levels interspersed or appended&#47;prepended is not gonna cause an ambiguity.`) how to achieve this ? Individual constructs works fine but when they are combined together then it leads to conflict . I am using `YACC&#47;BISON` as compiler construction tool.","metadata":{"transformedAt":"2026-08-18T18:32:17.763Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":48,"estimatedTokens":530}}136{"id":"stack-17792887","source":"stackoverflow","questionId":17792887,"title":"Can Tail Call Optimization and RAII Co-Exist?","tags":["c++","recursion","compiler-construction","tail-recursion","raii"],"text":"Title: Can Tail Call Optimization and RAII Co-Exist?\nTags: c++, recursion, compiler-construction, tail-recursion, raii\nSource: Stack Overflow\n\nQuestion:\nI can't think of a true RAII language that also has tail call optimization in the specs, but I know many C++ implementations can do it as an implementation-specific optimization.\n\nThis poses a question for those implementations that do: given that destructors are invoked at the end of a automatic variable's scope and *not* by a separate garbage collection routine, doesn't it violate TCO's constraint that a recursive call must be the last instruction at the end of a function?\n\nFor example:-\n\n```\n#include \n\nclass test_object {\npublic:\n test_object() { std::cout \"Constructing...\" would be written 999 times and then \"Destructing...\" another 999 times. Ultimately, 999 `test_object` instances would be automatically allocated before the unwind. But assuming an implementation had TCO, would 1000 stack frames exist or just 1?\n\nDoes the destructor after the recursive call collide with the defacto TCO implementation requirements?\n\n========================================\n\nTop Answer:\nIf the compiler perform the TCO then the order in which destructors are called is changed with respect to when it doesn't do TCO.\n\nIf the compiler can prove that this reordering doesn't matter (e.g. if the destructor is trivial) then as per the *as-if* rule it can perform TCO. However, in your example the compiler can't prove that and will not do TCO.\n\n========================================\n\nCode:\n```text\n#include <iostream>\n\nclass test_object {\npublic:\n    test_object() { std::cout << \"Constructing...\\n\"; }\n    ~test_object() { std::cout << \"Destructing...\\n\"; }\n};\n\nvoid test_function(int count);\n\nint main()\n{\n    test_function(999);\n}\n\nvoid test_function(int count)\n{\n    if (!count) return;\n    test_object obj;\n    test_function(count - 1);\n}\n```\n\n```text\ntest_object\n```\n\n```text\nvoid nonRAII_recursion(int a) {\n  int* arr = new int[a];\n  // do some stuff with array \"arr\"\n  delete[] arr;\n  nonRAII_recursion(--a);  // tail-call\n};\n```\n\n```text\nvoid RAII_recursion(int a) {\n  std::vector<int> arr(a);\n  // do some stuff with vector \"arr\"\n  RAII_recursion(--a);  // tail-call\n};  // arr gets destroyed here, not good for TCO.\n```\n\n```text\nvoid RAII_recursion(int a) {\n  {\n    std::vector<int> arr(a);\n    // do some stuff with vector \"arr\"\n  }; // arr gets destroyed here\n  RAII_recursion(--a);  // tail-call\n};\n```\n\n```text\nRAII_recursion\n```\n\n========================================\n\nComments:\n- Interesting though actually. I was going to say \"of course it doesn't prevent TCO\", but the more I look at it, the more I think it does...\n- There is no tail call here (and check your code, it doesn't compile as is). Though in theory a good compiler could notice the pattern and turn this into 2 loops.\n- Short answer: yes, deterministic destructors prevent TCO. Long answer: actually some compilers (such as LLVM I believe) implement a more permissive form of TCO and might tolerate more cases...\n- Yeah, it's kinda hard to tell. I was wondering at what stage of execution the destruction actually happens. I'd assumed it would happen just before the instruction pointer being put back and the stack frame removed, but surely that would happen after the last function statement? I think I may have to get a disassembler out...\n- Thanks for the code fix. Serves me right for some inattentive last-minute class renaming...\n- @ljackman: actually, returning in C++ is supposed to be a two-steps process, and destruction intervenes between those steps. So the timeline is: 1. \"Callee\" evaluate the return expression and put the return result in a specific \"return slot\" (may involve a copy/move), 2. \"Callee\" execute destructors, 3. \"Caller\" copy/move result from \"return slot\" into the variable (or temporary) in its own frame. Any two of the copy/move may be optimized away, but semantically the moment the destructors are scheduled is well defined.\n- You learn something new everyday... I suppose it depends whether the implementation allows TCO before or after step 2, given that a void function doesn't need to return anything anyway.\n- In the case of RAII, it will never be able to prove that the reordering doesn't matter, because almost by definition, it does matter.\n- @James Kanze. Indeed. If we see RAII in the sense that the destructor *releases a resource* (which is the most accepted sense of the term) then it's impossible to do TCO. If we see RAII in a broader sense, meaning that destructors are called when leaving the current scope, then, in some cases, it's still possible to do TCO.\n- Destructors which do nothing are *not* RAII. And in general, the presence of non-trivial constructors or destructors does inhibit TCO.\n- Additional note: as of C++14, the standard explicitly allows merging of some *new-expression*s without the aid of as-if rules. So the case gets more interesting.\n- If the vector's destructor has an externally-visible side-effect, I think the C++ standard would require that they all be constructed, and then all be destructed in the reverse order, would it not? Knowing whether the destructor of `arr` could have any side-effects would require analysis of all code that *ever* receives a pointer or reference to `arr`. Maybe possible in some cases, but not all.\n- @supercat: The standard specifies observable behavior (i.e., behaves \"as if\" destructed in reverse order), that's a bit of a Schrodinger's cat problem, as soon as you add code to make the order of execution observable, the standard guarantees the order, but when you remove that code, you can't be sure of the order of things you can't observe.\n- @supercat: As for destructing the vector having \"externally-visible side-effects\", that's of course dependent on the compiler and the situation. It certainly cannot always be determined. Also, how smart the compiler is w.r.t. mem-allocations is also critical (e.g., does it see it as just a function-call with possible side-effects, does it consider changes to the heap as a visible side-effect, etc.), and compilers do have some freedom in that. I agree that clean-up code is hard to re-order safely.\n- I would expect that most compilers would not be smart enough to do TCO to the second code snippet even with an inlined destructor, but haven’t tested it. The reason is that the compiler doesn’t know if `&#47;&#47; do some stuff with vector \"arr\"` includes putting a pointer to `arr` (or a pointer to one of the elements of `arr`) in a place that the recursive call can find (`static` local variable, global variable, `a`, etc.). Even if the compiler knows this didn’t happen, I think the `delete[]` call in `arr`’s destructor is still not reorderable unless the compiler uses strict pointer safety.\n- If the TCO is all for implementing the PTC (proper tail call) guarantee, which essentially requires O(1) space complexity of the nested active calls, then the cleanup calls can be deferred to the enclosing non-tail context and merged into one instance when the effects are *idempotent*: there is no difference between one call and more than one calls of the cleanup. Note the space consumption itself is not a visible side effect contributing to observable behaviors (in almost every language I've seen). This is still difficult to prove, though.\n- Such idempotent cleanups in C++ including release of the space of the automatic storage (technically, not counted as \"deallocation\"), and calls of default (not user-defined) `::operator delete` calls (subject to deallocation merging since C++14) determined statically (as if `consteval`-able) with no leak of information of the arguments. There need more assumptions provided by the language spec for other cases, otherwise it is at least impossible to assume it is safe to ignore the side effects when the definition of the deallocation function is not visible in the C++ translation units.\n- Also note not only the cleanup may violate the requirements of TCO. Conversions of the returned value can take effect even later than the cleanup calls. For example, when returning an lvalue from a function with an object type as its return type, there is at least a copy initialization (optionally with calls to some user-defined conversion functions). It is still sometimes possible to do TCO because the conversion can be idempotent, with or without the copy elision.","metadata":{"transformedAt":"2026-08-18T18:32:17.764Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":110,"estimatedTokens":2106}}137{"id":"stack-15984671","source":"stackoverflow","questionId":15984671,"title":"What does Backpatching mean?","tags":["language-agnostic","intermediate-language","compiler-construction","intermediate-code"],"text":"Title: What does Backpatching mean?\nTags: language-agnostic, intermediate-language, compiler-construction, intermediate-code\nSource: Stack Overflow\n\nQuestion:\nWhat does `backpatching` mean ? Please illustrate with a simple example.\n\n========================================\n\nTop Answer:\nIn intermediate code generation stage of a compiler we often need to execute \"jump\" instructions to places in the code that don't exist yet. To deal with this type of cases a target label is inserted for that instruction. \nA marker nonterminal in the production rule causes the semantic action to pick up.\n\n========================================\n\nCode:\n```text\nbackpatching\n```\n\n========================================\n\nComments:\n- Vote to reopen. Definitely a real question, with a real answer.\n- I wrote the same answer provided by you in my today's exam. :)\n- @user207421, Different countries , Different methods & rules. Don't assume. Some countries exams are conducted in a different manner. For ex, in India: once you are in exam hall you don't have anything but a pencil or pen. When you are writing whatever in your head on paper, you don't have to give the citation. Counter question to this would be: do you give citations in SAT ? or subject test of GRE ?\n- @rak Counter-counter question: what on earth is SAT? And GRE? The risk associated with not paraphrasing or citing properly is that the examiner will Google your answer and find literally the same text here, and mark accordingly.\n- @user207421, if you write it in your own words (from your head), I doubt if it will match. Again not every examiner will google (different countries/ education system!) especially if your exam is on pen and paper. You will need an OCR or some kind of automated system to do that. Which if they have, why would they use pen and paper method for exam at the first place?\n- How does backpatching work in the context of an assembler translating code from assembly language to machine code?\n- an example should be helpful\n- It is done by flow-of-control statements. Unless a boolean expression is also a flow-of-control statement, e.g. `&&` or `||` in C, it doesn't occur.\n- @EJP it would be great if you add some of your knowledge here. Thanks. :)\n- Why? What do you consider is missing from your answer?","metadata":{"transformedAt":"2026-08-18T18:32:17.764Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":33,"estimatedTokens":573}}138{"id":"stack-2598958","source":"stackoverflow","questionId":2598958,"title":"Writing a Compiler for .net - IL or Bytecode?","tags":[".net","compiler-construction","cil"],"text":"Title: Writing a Compiler for .net - IL or Bytecode?\nTags: .net, compiler-construction, cil\nSource: Stack Overflow\n\nQuestion:\nI'm currently diving into the inner workings of .net, which means IL. As an exercise, I want to build a brainf..k compiler for .net (yes, they already exist, but as said it's for learning purposes).\n\nFor the moment I'm just writing some text files that contain .il and compile them with ilasm, which works. But I wonder if I could/should go one level deeper and write bytecode directly?\n\nMy \"concern\" is the Windows PE Stuff when compiling an EXE - instead of ilasm I would need some sort of Bytecode linker that would take my MSIL/CIL bytecode and generate the PE Stuff for it?\n\nOr do compilers \"only\" compile their language to IL and execute ilasm? Is there a managed version of it that I can call/embed from my compiler?\n\n========================================\n\nTop Answer:\n`System.Reflection.Emit` provides facilities to create IL code in a statically-typed way without having to generate and compile text files with IL.\n\n========================================\n\nCode:\n```text\nusing System;\nusing System.Reflection.Emit;\nusing System.Reflection;\n\nnamespace SO2598958\n{\n    class Program\n    {\n        static void Main()\n        {\n            AssemblyBuilder asm = AppDomain.CurrentDomain.DefineDynamicAssembly(\n                new AssemblyName(\"TestOutput\"),\n                AssemblyBuilderAccess.RunAndSave);\n\n            ModuleBuilder mod = asm.DefineDynamicModule(\"TestOutput.exe\",\n                \"TestOutput.exe\");\n            TypeBuilder type = mod.DefineType(\"Program\", TypeAttributes.Class);\n\n            MethodBuilder main = type.DefineMethod(\"Main\",\n                MethodAttributes.Public | MethodAttributes.Static);\n            ILGenerator il = main.GetILGenerator();\n            il.Emit(OpCodes.Ldstr, \"Hello world!\");\n            il.Emit(OpCodes.Call, typeof(Console).GetMethod(\"WriteLine\",\n                BindingFlags.Public | BindingFlags.Static,\n                null, new Type[] { typeof(String) }, null));\n            il.Emit(OpCodes.Ret);\n\n            type.CreateType();\n            asm.SetEntryPoint(main);\n            asm.Save(\"TestOutput.exe\");\n        }\n    }\n}\n```\n\n```text\nSystem.Reflection.Emit\n```\n\n========================================\n\nComments:\n- Note that you don't really need to generate any actual native code if you output PE - you'll need to generate PE headers as defined by CLI spec, and those include some native executable bits, but they are fully predefined - it's just a stub that calls _CorExeMain - so you can generate it once, and treat it as opaque byte sequence in your compiler.\n- Completely forgot about Reflection.Emit. If I understand this correctly, I can write the complete compiler using it and AssemblyBuilder?\n- I believe that was how the initial implementations of IronPython was made.\n- If you're referring to the SO2598958 namespace, that's just an artifact of the fact that I create a solution for the answer, and commit it to my repository, so it's easy to find back the correct folder for the question. All my StackOverflow answers that I've produced some code for that I cared to keep is here: svn.vkarlsen.no:81/public/&hellip;\n- Note that Reflection.Emit APIs do not cover all possible codegen cases, and for some others it's clunky even when possible. It's definitely good enough for a BF compiler, but pretty much all compilers for \"serious\" languages that I'm aware of use something else - so you may want to take that as a hint.\n- What are some specific limitations of `Reflection.Emit`? What kinds of things can't it do? Are there alternatives that function similarly?\n- No, I don't want to write my own .net Runtime (yet :)), just a Source Code -> .net Compiler.","metadata":{"transformedAt":"2026-08-18T18:32:17.764Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":71,"estimatedTokens":943}}139{"id":"stack-710793","source":"stackoverflow","questionId":710793,"title":"How does static field initialization work in C#?","tags":["c#","compiler-construction"],"text":"Title: How does static field initialization work in C#?\nTags: c#, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nShould static field initialization be completed before constructor is called?\n\nThe following program provides output that seems incorrect to me.\n\n```\nnew A()\n_A == null\nstatic A()\nnew A()\n_A == A\n```\n\nThe code:\n\n```\npublic class A\n{\n public static string _A = (new A()).I();\n\n public A()\n {\n Console.WriteLine(\"new A()\");\n if (_A == null)\n Console.WriteLine(\"_A == null\");\n else\n Console.WriteLine(\"_A == \" + _A);\n }\n\n static A()\n {\n Console.WriteLine(\"static A()\");\n }\n\n public string I()\n {\n return \"A\";\n }\n}\n\nclass Program\n{\n static void Main(string[] args)\n {\n var a = new A();\n }\n}\n```\n\n========================================\n\nTop Answer:\nOne extra side note - the C# specification (I'm looking at 4.0, but it's there in 3.0 too) says in 10.5.5.1 Static Field Initialization:\n\n If a static constructor (§10.12)\n exists in the class, execution of the\n static field initializers occurs\n immediately prior to executing that\n static constructor. Otherwise, the\n static field initializers are executed\n at an implementation-dependent time\n prior to the first use of a static\n field of that class.\n\nYou have a static constructor, so the \"Otherwise\" clause does not apply. But I think it's relevant information to your question to know that if you don't have a static constructor, the static field initializers can be executed 'at an implementation-dependent time'. This could matter if your static field initializer is doing some type of data initialization or object creation which you rely on without accessing the static field itself. \n\nIt is esoteric, I guess, but I saw it happen today as the 'implementation-dependent time' appears to have changed between C# 3.0 and 4.0 - at least for the situation I was looking at. The easy solution of course is simple - just add a static constructor...\n\n========================================\n\nCode:\n```text\nnew A()\n_A == null\nstatic A()\nnew A()\n_A == A\n```\n\n```text\npublic class A\n{\n    public static string _A = (new A()).I();\n\n    public A()\n    {\n        Console.WriteLine(\"new A()\");\n        if (_A == null)\n            Console.WriteLine(\"_A == null\");\n        else\n            Console.WriteLine(\"_A == \" + _A);\n    }\n\n    static A()\n    {\n        Console.WriteLine(\"static A()\");\n    }\n\n    public string I()\n    {\n        return \"A\";\n    }\n}\n\nclass Program\n{\n    static void Main(string[] args)\n    {\n       var a = new A();\n    }\n}\n```\n\n```text\nConsole.WriteLine(\"new A()\");\nif (_A == null)\n    Console.WriteLine(\"_A == null\");\n```\n\n```text\nvar a = new A();\n```\n\n```text\nA._A\n```\n\n```text\n_A = (new A()).I();\n```\n\n```text\nA { static A(); }\n```\n\n```text\nvar a = new A();\n```\n\n```text\npublic static string _A = (new A()).I();\n```\n\n```text\npublic static string _A = (new A()).I();\n\n// and\n\nstatic A()\n{\n    Console.WriteLine(\"static A()\");\n}\n```\n\n```text\npublic A()\n{\n    Console.WriteLine(\"new A()\");\n    if (_A == null)\n        Console.WriteLine(\"_A == null\");\n    else\n        Console.WriteLine(\"_A == \" + _A);\n}\n```\n\n========================================\n\nComments:\n- FWIW, I'll mention that if one wants to *defer* some \"static\" initialization until a *specific* static field is called, a way to do so (for a field that is a reference type) is to have a private static field that starts out null, and then a public static getter or method that does the \"initialization\" only when null, then sets private field and returns it.\n- With all due respect... static constructor doesn't run first. The static field initializer runs first.\n- Construction of A._A happens in the static constructor. It's just that the compiler prepends all fields initializations in the static constructor (.cctor) before the code declared in C# static constructor.\n- @prankster: I was just trying to say that his static construction + initialization is all happening prior to his non-static construction. I reworded and edited to be more clear.\n- @ReedCopsey So, as per my understanding, the compiler first tries to initialize the static field _A after which the compiler is forced to create an object which in turn executes the non-static constructor(different from the usual process where first all static members are executed - fields and methods), prints the write lines, comes back to the caller with a new object of A() and immediately before calling I(), compiler as per the default behavior executes the static constructor, prints the \"static A()\" and then finally the I() is executed and value \"A\" is assigned.\n- But note that spec says \"prior to the first use of **a** static field of that class\". That is, you can guarantee that **all** static initialization has been done, simply by accessing **any** static field. [At least that is what I have seen in practice.]\n- I've just seen this vary between Debug and Release mode builds. Truly nasty stuff to find.","metadata":{"transformedAt":"2026-08-18T18:32:17.764Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":179,"estimatedTokens":1231}}140{"id":"stack-15673216","source":"stackoverflow","questionId":15673216,"title":"Why some compilers prefer hand-crafted parser over parser generators?","tags":["parsing","compiler-construction","vala"],"text":"Title: Why some compilers prefer hand-crafted parser over parser generators?\nTags: parsing, compiler-construction, vala\nSource: Stack Overflow\n\nQuestion:\nAccording to Vala documentation: \"Before 0.3.1, Vala's parser was the classic flex scanner and Bison LALR parser combination. But as of commit eba85a, the parser is a hand-crafted recursive descent parser.\"\nMy question is: Why?\n\nThe question could be addressed to any compiler which isn't using parser generator. What are pros and cons for such a move from parser generator to hand-crafted parser? What are disadvantages of using parser generators (Bison, ANTLR) for compilers?\n\nAs a side comment: I'm interested in Vala specifically because I like the idea of having language with modern features and clean syntax but compilable into \"native\" and \"unmanaged\" high-level language (C in case of Vala). I have found only Vala so far. I'm thinking of having fun by making Vala (or similar language) compilable to C++ (backed by Qt libs). But since I don't want to invent completely new language I'm thinking of taking some existing grammar. Obviously hand-crafted parsers don't have written formal grammar I might reuse. Your comments on this idea are welcome (is the whole idea silly?).\n\n========================================\n\nTop Answer:\nI have written half a dozen hand crafted parsers (in most cases recursive descent parser AKA top-down parser) in my career and have seen parsers generated by parser generators and I must admit I am biased against parser generators. \n\nHere are some pros and cons for each approach.\n\n**Parser generators**\n\nPros:\n\n- Quickly get a working parser (at least if you do not know how to hand code it).\n\nCons:\n\n- Generated code is hard to understand and debug.\n\n- Difficult to implement proper error handling. The generator will create a correct parser for syntactically correct code but will choke on incorrect code and in most cases will not be able to provide proper error messages.\n\n- A bug in parser generator may halt your project. You need to fix the bug in somebody else's code (if source code is available), wait for the author to fix it or workaround the bug (if possible at all).\n\n**Hand crafted recursive descent parser**\n\nPros:\n\n- Generated code is easy to understand. Recursive parsers usually have one function corresponding to each language construct, e.g. parseWhile to parse a 'while' statement, parseDeclaration to parse a declaration and so on. Understanding and debugging the parser is easy.\n\n- It is easy to provide meaningful error messages, to recover from errors and continue parsing in the way that makes most sense in a particular situation.\n\nCons:\n\nIt will take some time to hand code the parser especially if you do not have an experience with this stuff.\n\nThe parser may be somewhat slow. This applies to all recursive parsers not just hand written ones. Having one function corresponding to each language construct to parse a simple numeric literal the parser may make a dozen or more nested calls starting from e.g. parseExpression through parseAddition, parseMultiplication, etc. parseLiteral. Function calls are relatively inexpensive in a language like C but still cam sum up to a significant time.\n\nOne solution to speedup a recursive parser is to replace parts of your recursive parser by a bottom-up sub-parser which often is much faster. The natural candidates for such sub-parser are the expressions which have almost uniform syntax (i.e. binary and unary expressions) with several precedence levels. The bottom-up parser for an expression is usually also simple to hand code, it is often just one loop getting input tokens from the lexer, a stack of values and a lookup table of operator precedence’s for operator tokens.\n\n========================================\n\nComments:\n- Have you asked J&#252;rg Billeter (author of the commit)?\n- Hmmm, no, I haven't. I will try to reach him. I'm changing Title of my question for making it more general.\n- Likely hand-crafting a parser can be made to be faster/more space efficient, since it doesn't have to be generic and may be able to use more specific tricks.\n- You may want to look at Mozilla's Rust project, which already targets C++.\n- Error messages and error recovery are handled much better in a handwritten parser. Of course there are nice ways of generating such code from some high level declarative language, but these modern ways are unknown (or too scary) to the yacc-infected LR folks. They would rather prefer to write a recursive descent parser manually then go into something PEG-based.\n- Also see mortoray.com/2012/07/20/why-i-dont-use-a-parser-generator for some more issues with generators.\n- One advantage with parser generator is its ability to target any programming language. For example, one can generate lexer/parser in Python as well as in Java using Antlr. This is definitely an important lookout for use cases wherein you're targeting multiple programming communities.\n- Crosslink: same question on SoftwareEngineering: language design - Should I use a parser generator or should I roll my own custom lexer and parser code? - Software Engineering Stack Exchange\n- Thank you. By \"native\" I mean \"directly compiled to native code\", meaning no byte code in a middle, no JIT-compilation, no VM, therefore no GC.\n- @vladimir What, like this or this? By visiting those links you may have gained insightful information: That C may also be interpreted. It might also be insightful to learn that C#, a typically JIT-compiled-to-byte-code language, can also be compiled to machine code and that JIT is an optimisation method that also applies to *native machine code*. You might be interested to learn that C is specified in terms of a VM called the \"abstract machine\", and that the C specifications don't exclude garbage collection.\n- @vladimir: Javascript was traditionally interpreted, but is now compiled by the V8 engine to IA-32, x86-64, ARM, or MIPS. Release the connection you have established between \"translation method\" (eg. interpretation, compilation) and \"programming language\". Code written in one programming language can be compiled (translated) to any other turing complete programming language, or interpreted (translated directly to behaviour).\n- I have to rephrase the part about \"native\" and \"unmanaged\". Let's say a language which is cheap at runtime. C# or Java program with empty GUI takes 40Mb at least. Also the GUI might be slow. For example, Qt app for monitoring high-frequency trading took 10 times less memory and was able to update numbers on a screen 4-5 times faster then WPF. And this is not because of me. I'm good in C# and WPF (much better then in Qt). So, what I really meant under \"native\" and \"unmanaged\" is \"without heavy runtime\". Such as C++ compiled by GCC. Although somewhy I don't want to go back to C++ syntax.\n- @vladimir Which part of the C# language specification states that GUI is a requirement? WPF is part of the .NET framework, which also consists of the CLR. Only a few parts of the CLR are required in a valid C# implementation. Furthermore, optimisation is another detail of implementation that is irrelevant to specification. Languages don't have speed; That's an attribute that compilers/interpreters produce... and a C# program that produces the same results as a C program may very well turn into the same machine code.\n- as a programmer I can't consider language without it's compiler and runtime environment and libraries. Regardless of what specification says, boss says we need fast UI application for high-frequency trading. I'm responsible for technology selection. Let's say, I like C# very much (I really do), but will currently EXISTING compilers and libraries fit the current task? So liking or not liking syntax is disconnected from performance of concrete compiler, runtime an libraries. However the whole platform selection fully depends on all these and syntax is the last by the way.\n- So my dream is C#-like syntax, C(compiled by GCC)-like performance and libraries selection like in Java (or C++). Seems like compiling C# somehow to C/C++ is what I need. Because then I'm getting syntax I like, potentially I can get portability, good performance and thin runtime (with GCC compiler) and some selection of C++ libraries which could be mapped without wrappers to the language.\n- @vladimir Is the GUI the most significant bottleneck in your application? Which parts of it, specifically? Use a profiler to determine the answer to those two questions. Is it more feasible to use an alternative to that/those specific controls, or to repopulating the controls with values, than to redesign the entire UI in Qt? Research alternative methods of population such as \"data binding\", or functions such as \"BeginUpdate\" and \"EndUpdate\" which might provide performance boosts for large updates. If you can't find anything, look for alternative controls. Is it possible to integrate Qt in C#?\n- @vladimir If the GUI isn't the most significant bottleneck in your application, then why are you focusing on it? Using a \"faster programming language\" won't *significantly* improve the performance of your solution; You need to look at your algorithm to do that. Whatever your trading program is doing behind the GUI, or to update the GUI, is taking longer than it should... so focus on finding alternatives for that.\n- I know all techniques of optimization mentioned by you. And I'm using them. Also I tried to implement an app on Qt. And figured out that it's faster and less memory consuming. The difference is so big that I started to think how I can get such technology with C# (MS compiler and runtime). Wrapping Qt libs to C# would help but not that much. Because I see that heaviness of the .Net process related not only to the libraries but it also because of heavy runtime. That is how I came to the idea of compiling C# to something different then CIL.\n- please, don't treat me like I don't know what I'm doing. I'm working on automated market making system for options on the trading floor. My client application is subscribing to low-latency server. Yes - the GUI is a bottleneck of the client application. GUI is the only thing there besides very effective trasnsport library. The .NET runtime just on a start eats more memory then the whole Qt based app. Is'n it weakness of runtime? Not even mentioning the GUI WPF library.\n- @vladimir You know all of this and yet you don't know why a recursive descent parser would be preferable? ... and yet you're messing around trying to translate Vala to C++ instead of C because you want the Qt toolkit? Well, sorry... I can't help you.\n- Well, I'm not sure to where I want to translate Vala, I'm not even sure if it should be Vala. I just found that Vala implements the idea very similiar to mine and that the idea is good to me.\n- Not knowing aspects of compiler optimization (moving from parser gen to hand-crafted parser) only means that I don't have hands-on experience in that area. From what I said you can imagine that i have some other unique knowledge in programming area. Finally if that would be that simple there would be no compilers using parser generators and parser generators themselves. Knowing or no knowing things doesn't give a right of speaking arrogantly. Please, respect other opinion and give real arguments instead of \"sorry... I can't help you\".\n- +1 I would expect errorhandling to be the primary cause. Ambiguities and repeated rule writing can sometimes also get handled by extensions in the parser generator (bison does some)\n- GCC went the other way, too. They replaced Bison with a hand-written recursive descent parser for C++ in 3.4, and for C and Objective C in 4.1. I'm not trying to make a point or anything, just thought you might find it interesting.\n- Awesome! Thanks for the inside. I'll learn more before deciding how to make Qt libs available in Vala.","metadata":{"transformedAt":"2026-08-18T18:32:17.764Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":77,"estimatedTokens":2976}}141{"id":"stack-1602978","source":"stackoverflow","questionId":1602978,"title":"How to write a linker","tags":["c","compiler-construction","linker","interpreter","vm-implementation"],"text":"Title: How to write a linker\nTags: c, compiler-construction, linker, interpreter, vm-implementation\nSource: Stack Overflow\n\nQuestion:\nI have written a compiler for C that outputs byte code. The reason for this was to be able to write applications for an embedded platform that runs on multiple platforms.\n\nI have the compiler and the assembler.\n\nI need to write a linker, and am stuck.\n\nThe object format is a custom one, designed around the byte code interpreter, so I cant really use any existing linkers.\n\nMy biggest hurdle is how to organize the object code to output the linked binary.\nDynamic linking is not necessary, at this time.\nI need to get static linking working first.\n\n========================================\n\nTop Answer:\nIan Lance Taylor, one of the main developers on the gold linker(now part of binutils), posted a series of blogs on how linkers work. You can find it here.\n\n========================================\n\nComments:\n- The above links to the first post in the series. For a list of all the posts, see airs.com/blog/index.php?s=linkers","metadata":{"transformedAt":"2026-08-18T18:32:17.764Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":26,"estimatedTokens":266}}142{"id":"stack-1443982","source":"stackoverflow","questionId":1443982,"title":"When do compilers inline functions?","tags":["c++","optimization","compiler-construction","inline","inlining"],"text":"Title: When do compilers inline functions?\nTags: c++, optimization, compiler-construction, inline, inlining\nSource: Stack Overflow\n\nQuestion:\nIn C++, do functions only get inlined if they are explicitly declared `inline` (or defined in a header file), or are compilers allowed to inline functions as they see fit?\n\n========================================\n\nTop Answer:\nThe **inline** keyword really just tells the linker (or tells the compiler to tell the linker) that multiple identical definitions of the same function are not an error. You'll need it if you want to define a function in a header, or you will get \"multiple definition\" errors from the linker, if the header is included in more than one compilation unit.\n\nThe rationale for choosing **inline** as the keyword seems to be that the only reason why one would want to define a (non-template) function in a header is so it could be inlined by the compiler. The compiler cannot inline a function call, unless it has the full definition. If the function is not defined in the header, the compiler only has the declaration and cannot inline the function even if it wanted to.\n\nNowadays, I've heard, it's not only the compiler that optimizes the code, but the linker can do that as well. A linker could (if they don't do it already) inline function calls even if the function wasn't defined in the same compilation unit.\n\nAnd it's probably not a good idea to define functions larger than perhaps a single line in the header if at all (bad for compile time, and should the large function be inlined, it might lead to bloat and worse performance).\n\n========================================\n\nCode:\n```text\ninline\n```\n\n```text\ninline\n```\n\n```text\ninline\n```\n\n```text\ninline\n```\n\n========================================\n\nComments:\n- note: inline is only hint to the compiler. The compiler is not required to inline it.\n- @Martin York: Unfortunately you're right. And in 99.9% of the cases it is the best to let the compiler decide in the last instance. But in some cases (performance critical programs like Image processing, etc.) it would be nice to have a ultimate inline forcing switch. But I can understand the compiler programmers. Because as soon as this switch would exist many would use it in many inappropriate cases no matter how insistently the documentation warns to only use it in rare cases...\n- More accurately, `inline` has a different meaning in C/C++ code than the one used to describe the optimization. In C/C++ code, `inline` simply means that multiple definitions of a function may exist, and that the linker should merge them back together. It does *not* mean that calls to that function should be inlined. They're two entirely separate concepts.\n- @rstevens: You already have that; it's spelled `#define`.\n- \"Turning optimizations on\" is not a standard term. You're describing actual implementations, who do have that switch. In that case you don't need the hypothetical *may*: The compiler *will* inline suitable functions.\n- Since compiler does everything for us, why on earth we need an inline hint?\n- @lz_prgmr: Well, besides the subtle point is mentioned by UncleBens below, I think we don't. It's important to note that compilers *were* not that clever from the beginning and we are kind of stuck with old features of the language.\n- One more thing: it also allows you to define the function in more than one compilation unit\n- Even though, I start to question the correctness of your answer: If a function is not declared as inline but implemented in header file, the compiler will complain about link errors, rather than inline them as default if appears in multiple translation unit.\n- @lz_prgmr: It affects the \"complaining\" behavior of the compiler, as pointed out, but that doesn't mean the compiler is forced to inline it or anything in that regard. The compiler is free to inline anything it likes and not inline even if you decorate a function with `inline`.\n- So the \"inline\" keyword is necessary, with which we even can't compile successfully, not to mention to inline it.\n- This makes debugging using GDB / WinDBG difficult.\n- This is the really important aspect of inline.","metadata":{"transformedAt":"2026-08-18T18:32:17.764Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":53,"estimatedTokens":1043}}143{"id":"stack-22681346","source":"stackoverflow","questionId":22681346,"title":"Are compilers built with previous version of themselves protected from code injection?","tags":["c++","c","compiler-construction","compilation"],"text":"Title: Are compilers built with previous version of themselves protected from code injection?\nTags: c++, c, compiler-construction, compilation\nSource: Stack Overflow\n\nQuestion:\nI was wondering if today's modern compilers like MS cc, gcc, clang, icc, newer versions were built with the current version of the same compiler?\n\nBecause of course of this risk:\n\nhttp://scienceblogs.com/goodmath/2007/04/15/strange-loops-dennis-ritchie-a/\n\nhttp://c2.com/cgi/wiki?TheKenThompsonHack\n\nI'm sure everyone involved with the afore-mentioned compilers' development knows about this issue, whereby code is injected into the compiler by an earlier version of itself and propagates invisibly.\n\nNow the real problem, is not really one of backdoors, but much more about code generation correctness isn't it ? How about if somewhere in the build chain some pervert twist was introduced by pure mistake, and today's compiler generate incorrect code, even if the compiler's source look OK, because of the Ken Thompson's flaw?\n\nSo if they are built with themselves, how do they protect themselves?\n\n========================================\n\nTop Answer:\nIn general the answer is 'yes', for compilers implemented in their own languages. Building the compiler with itself is one of the best tests for correctness. Successive runs should keep producing the same binary. 'GC' for example is built with a four-stage bootstrap process.\n\nOf course some languages can't be used for compiler-writing.\n\n**EDIT** It should be made clear that this answer was posted when the substantive question was \"Are compilers built with previous version of themselves?\" It has subsequently been changed.\n\n========================================\n\nComments:\n- Compilers today are maintained not by single persons, but by quite sizeable teams. Peer review is the operational term here. Anyway, voting for close, \"primarily opinion based\".\n- Not only is there peer-reviews, there are quite extensive tests for each compiler to go though before being released. Though, even in trivial applications bugs happen, in a non-trivial like modern C or a C++ compilers there is a 100% chance of bugs being in the released versions.\n- stackoverflow.com/questions/193560/&hellip; stackoverflow.com/questions/494372/&hellip; stackoverflow.com/questions/5657454/&hellip;\n- @DevSolar: `opinion based?` seriously ? and what do you say about perfectly verifiable EJP's comment ? and why don't you give a chance to implementors of answering ? There ARE microsoft's people in this community. I'm sure there also are GCC contributors.\n- And what would you like them to say? The answer to your first question - are compilers built with their own binaries - is \"yes\", as any kind of googling or experience with building a system would have told you even without stijn's help. The second part of your question *is* opinion-based, as it's pretty hard to prove the non-existence of something, especially something like the Thompson Backdoor. As for code generation, what do you think regression tests are for?\n- As far as it goes, compilers are not always built with themselves; for example, I've built GCC using the SUNWspro compiler. This would not prevent a backdoor in the code, but it would have to be in the *current* code.\n- Possibly. What about: are conveyor belt systems build in factories using a conveyor belt? Are robots built using robots (or are they made by hand)? Are computers designed using computers (or drawn on paper)?\n- To get some confidence in the compiler, you would (a) compile it with multiple compilers from different manufacturers; this will obviously produce different binaries, then (b) compile it with these different binaries, which then should give identical binaries. That's for a compiler that can compile the language it's written in; COBOL compilers might not be written in COBOL.\n- en.wikipedia.org/wiki/Bootstrapping_%28compilers%29\n- en.wikipedia.org/wiki/Self-hosting\n- Even compiler writers use source control.\n- I'm not going to vote to close any question that Eric Lippert is willing to answer. And it really isn't an exact duplicate.\n- @gnasher729 They sure aren't.\n- @david.pfx: thanks its a nice edit, that resets the question into a \"one focus\" question, with the title on the main focus.\n- \"Of course some languages can't be used for compiler-writing.\" This statement is untrue unless we're including really domain-specific languages. Any Turing-complete language with I/O can be used to implement a compiler for any other language.\n- @nibot In practice that isn't so. It isn't economically feasible to write a COBOL compiler in COBOL, for example, and only a madman would try it. I didn't. Too many things are missing: recursion, for a start.\n- @nibot: I challenge you to write a self-hosting PL/SQL compiler, or ABAP... ;-) Let's just say, some languages cannot *reasonably* be used for compiler writing. (Although I've heard of BF-written BF compilers, and BF is a language that cannot be mentioned with \"reasonable\" in the same sentence without quotation marks. ;-) )\n- @nibot You're also ignoring the issue of the runtime library. You can't write a runtime library for Cobol in Cobol.\n- What puzzles me in bootstrap and also Diverse Double Compiling is that I cannot fathom by what miracle should we obtain \"identical output\" ? Because a new version of a compiler exists because it generates better assembly (more optimized), therefore compiling the compiler with different versions or vendors would invariably result in different outputs, because of varying implementation choices in code generation. proof : yosoygames.com.ar/wp/2013/12/&hellip;\n- @v.oddou: Read my scenario again carefully. Compilers Alpha and Beta need not have the same output; those are two different compilers with different source code and different behaviour. But surely compilers Beta and Gamma should have identical behaviour as they were compiled from the same source code. If the Beta sources compiled with Alpha and the Beta sources compiled with Beta give compilers with different behaviours then something is likely wrong with Beta.\n- Thanks for help :) Ok, I;ve made this diagram: postimg.org/image/p4lqovfwn now I would grant you that test's binary 1 and 2 should be equivalent (modulo `__TIME__` macros stuff) but `Orange Beta` and `Blue Beta` would be different binaries, and though input/output symetry should be respected, the time it will take to `Orange` to compile will be different than `Blue`s because of different level of optimization in `Alpha` and `Beta`. So the 2 binaries are actually very different. I still miss something\n- I think I'm on the way to understand (that's why I accepted your answer this time), `Gamma` is \"second-chained built\" therefore it should be equivalent to `Beta` because it is just a rebuild ? Is that provable ?","metadata":{"transformedAt":"2026-08-18T18:32:17.764Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":53,"estimatedTokens":1705}}144{"id":"stack-7360752","source":"stackoverflow","questionId":7360752,"title":"How C++ virtual inheritance is implemented in compilers?","tags":["c++","inheritance","compiler-construction","virtual-inheritance"],"text":"Title: How C++ virtual inheritance is implemented in compilers?\nTags: c++, inheritance, compiler-construction, virtual-inheritance\nSource: Stack Overflow\n\nQuestion:\nHow the compilers implement the virtual inheritance?\n\nIn the following code:\n\n```\nclass A {\n public:\n A(int) {}\n};\n\nclass B : public virtual A {\n public:\n B() : A(1) {}\n};\n\nclass C : public B {\n public:\n C() : A(3), B() {}\n};\n```\n\nDoes a compiler generate two instance of `B::ctor` function, one without `A(1)` call, and one with it? So when `B::constructor` is called from derived class's constructor the first instance is used, otherwise the second.\n\n========================================\n\nTop Answer:\nThe compiler does not create another constructor of B - but it ignores the `A(1)`. Since `A` is virtually inherited, it is constructed first, with its default constructor. And since it's already constructed when `B()` is invoked, the `A(1)` part is ignored.\n\nEdit - I missed the `A(3)` part in `C`'s constructor initialization list. When virtual inheritance is used, only the most derived class initializes the virtual base classes. So `A` will be constructed with `A(3)` and not its default constructor. The rest still stands - any initializations of `A` by an intermediate class (here `B`) are ignored.\n\nEdit 2, trying to answer the actual question regarding the implementation of the above:\n\nIn Visual Studio (at least 2010), a flag is used instead of having two implementations of `B()`. Since `B` virtually inherits from `A`, before it calls `A`'s constructor, the flag is checked. If the flag is not set, the call to `A()` is skipped. Then, in every class deriving from `B`, the flag is reset after it initializes `A`. The same mechanism is used to prevent `C` from initializing `A` if it's part of some `D` (if `D` inherits from `C`, `D` will initialize `A`).\n\n========================================\n\nCode:\n```text\nclass A {\n  public:\n    A(int) {}\n};\n\nclass B : public virtual A {\n  public:\n    B() : A(1) {}\n};\n\nclass C : public B {\n  public:\n    C() : A(3), B() {}\n};\n```\n\n```text\nB::ctor\n```\n\n```text\nA(1)\n```\n\n```text\nB::constructor\n```\n\n```text\nB1()\nB2() // no A\n```\n\n```text\nB1():\n    A(1)\n    B() body\n```\n\n```text\nC():\n    A(3)\n    B2()\n       B() body\n    C() body\n```\n\n```text\nA(1)\n```\n\n```text\nA(1)\n```\n\n```text\nA\n```\n\n```text\nB()\n```\n\n```text\nA(1)\n```\n\n```text\nA(3)\n```\n\n```text\nC\n```\n\n```text\nA\n```\n\n```text\nA(3)\n```\n\n```text\nA\n```\n\n```text\nB\n```\n\n```text\nB()\n```\n\n```text\nB\n```\n\n```text\nA\n```\n\n```text\nA\n```\n\n```text\nA()\n```\n\n```text\nB\n```\n\n```text\nA\n```\n\n```text\nC\n```\n\n```text\nA\n```\n\n```text\nD\n```\n\n```text\nD\n```\n\n```text\nC\n```\n\n```text\nD\n```\n\n```text\nA\n```\n\n```text\n<ctor-dtor-name> ::= C1   # complete object constructor\n             ::= C2   # base object constructor\n             ::= C3   # complete object allocating constructor\n             ::= D0   # deleting destructor\n             ::= D1   # complete object destructor\n             ::= D2   # base object destructor\n```\n\n========================================\n\nComments:\n- Armen: I guess the simplest is that `A` constructor initializer checks and sets a flag that it's been execeuted. That implies some pre-initialization, clearing that flag, but the compiler knows that `B` and `C` has some virtual inheritance to deal with. The slightly more difficult issue is the vtable layout; as I recall Bjarne wrote somewhere that he had to implement it to convince himself that it was possible.\n- I just got asked this question in an interview... and I had no idea how to answer it! :(\n- Actually, gcc *does* generate two versions of the constructor, one to be called when B is instantiated, another one for C. stackoverflow.com/questions/6921295/&hellip;\n- :) My question is how the compiler ignore that part? In the assembly code of B::ctor what is generated for A(1) call?\n- -1 This purported answer is restating the question. With more details yes. But it's just the question again.\n- MKo, @Alf, my bad. Added an explanation of the actual implementation in VS.\n- You didn't understand the question at all\n- @Stefan Reich Wrong. I already working on internals on how to implement virtual inheritance ...\n- Well. It seems you are talking about normal inheritance/virtual methods/VMT/overloading, not about virtual base classes which are a special construct for multiple inheritance. But never mind.","metadata":{"transformedAt":"2026-08-18T18:32:17.764Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":33,"totalLines":211,"estimatedTokens":1089}}145{"id":"stack-531477","source":"stackoverflow","questionId":531477,"title":"Strange assembly from array 0-initialization","tags":["c++","c","compiler-construction","assembly"],"text":"Title: Strange assembly from array 0-initialization\nTags: c++, c, compiler-construction, assembly\nSource: Stack Overflow\n\nQuestion:\nInspired by the question Difference in initalizing and zeroing an array in c/c++ ?, I decided to actually examine the assembly of, in my case, an optimized release build for Windows Mobile Professional (ARM processor, from the Microsoft Optimizing Compiler). What I found was somewhat surprising, and I wonder if someone can shed some light on my questions concerning it.\n\nThese two examples are examined:\n\n```\nbyte a[10] = { 0 };\n\nbyte b[10];\nmemset(b, 0, sizeof(b));\n```\n\nThey are used in the same function, so the stack looks like this:\n\n```\n[ ] // padding byte to reach DWORD boundary\n[ ] // padding byte to reach DWORD boundary\n[ ] // b[9] (last element of b)\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ] // b[0] = sp + 12 (stack pointer + 12 bytes)\n[ ] // padding byte to reach DWORD boundary\n[ ] // padding byte to reach DWORD boundary\n[ ] // a[9] (last element of a)\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ] // a[0] = sp (stack pointer, at bottom)\n```\n\nThe generated assembly with my comments:\n\n```\n; byte a[10] = { 0 };\n\n01: mov r3, #0 // r3 = 0\n02: mov r2, #9 // 3rd arg to memset: 9 bytes, note that sizeof(a) = 10\n03: mov r1, #0 // 2nd arg to memset: 0-initializer\n04: add r0, sp, #1 // 1st arg to memset: &a[1] = a + 1, since only 9 bytes will be set\n05: strb r3, [sp] // a[0] = r3 = 0, sets the first element of a\n06: bl memset // continue in memset\n\n; byte b[10];\n; memset(b, 0, sizeof(b));\n\n07: mov r2, #0xA // 3rd arg to memset: 10 bytes, sizeof(b)\n08: mov r1, #0 // 2nd arg to memset: 0-initializer\n09: add r0, sp, #0xC // 1st arg to memset: sp + 12 bytes (the 10 elements\n // of a + 2 padding bytes for alignment) = &b[0]\n10: bl memset // continue in memset\n```\n\nNow, there are two things that confuses me:\n\n- What's the point of lines 02 and 05? Why not just give &a[0] and 10 bytes to memset?\n\n- Why isn't the padding bytes of a 0-initialized? Is that only for padding in structs?\n\nEdit: I was too curious to not test the struct case:\n\n```\nstruct Padded\n{\n DWORD x;\n byte y;\n};\n```\n\nThe assembler for 0-initializing it:\n\n```\n; Padded p1 = { 0 };\n\n01: mov r3, #0\n02: str r3, [sp]\n03: mov r3, #0\n04: str r3, [sp, #4]\n\n; Padded p2;\n; memset(&p2, 0, sizeof(p2));\n\n05: mov r3, #0\n06: str r3, [sp]\n07: andcs r4, r0, #0xFF\n08: str r3, [sp, #4]\n```\n\nHere we see in line 04 that a padding indeed occur, since `str` (as opposed to `strb`) is used. Right?\n\n========================================\n\nTop Answer:\nBoth bits of code are bugfree. The two lines mentioned aren't smart, but you're just proving that this compiler is emitting suboptimal code.\n\nPadding bytes are usually only initialized if that simplifies the assembly or speeds up the code. E.g if you have padding between two zero-filled members, it's often easier to zero-fill the padding as well. Also, if you have padding at the end and your memset() is optimized for multi-byte writes, it may be faster to overwrite that padding too.\n\n========================================\n\nCode:\n```text\nbyte a[10] = { 0 };\n\nbyte b[10];\nmemset(b, 0, sizeof(b));\n```\n\n```text\n[ ] // padding byte to reach DWORD boundary\n[ ] // padding byte to reach DWORD boundary\n[ ] // b[9] (last element of b)\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ] // b[0] = sp + 12 (stack pointer + 12 bytes)\n[ ] // padding byte to reach DWORD boundary\n[ ] // padding byte to reach DWORD boundary\n[ ] // a[9] (last element of a)\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ]\n[ ] // a[0] = sp (stack pointer, at bottom)\n```\n\n```text\n; byte a[10] = { 0 };\n\n01: mov   r3, #0        // r3 = 0\n02: mov   r2, #9        // 3rd arg to memset: 9 bytes, note that sizeof(a) = 10\n03: mov   r1, #0        // 2nd arg to memset: 0-initializer\n04: add   r0, sp, #1    // 1st arg to memset: &a[1] = a + 1, since only 9 bytes will be set\n05: strb  r3, [sp]      // a[0] = r3 = 0, sets the first element of a\n06: bl    memset        // continue in memset\n\n; byte b[10];\n; memset(b, 0, sizeof(b));\n\n07: mov   r2, #0xA      // 3rd arg to memset: 10 bytes, sizeof(b)\n08: mov   r1, #0        // 2nd arg to memset: 0-initializer\n09: add   r0, sp, #0xC  // 1st arg to memset: sp + 12 bytes (the 10 elements\n                        // of a + 2 padding bytes for alignment) = &b[0]\n10: bl    memset        // continue in memset\n```\n\n```text\nstruct Padded\n{\n    DWORD x;\n    byte y;\n};\n```\n\n```text\n; Padded p1 = { 0 };\n\n01: mov   r3, #0\n02: str   r3, [sp]\n03: mov   r3, #0\n04: str   r3, [sp, #4]\n\n; Padded p2;\n; memset(&p2, 0, sizeof(p2));\n\n05: mov   r3, #0\n06: str   r3, [sp]\n07: andcs r4, r0, #0xFF\n08: str   r3, [sp, #4]\n```\n\n```text\nstr\n```\n\n```text\nstrb\n```\n\n```text\nbyte a[10] = { };\n```\n\n```text\n; unsigned char a[10] = { };\n\nxor eax, eax\nmov DWORD PTR _a$[esp+40], eax\nmov DWORD PTR _a$[esp+44], eax\nmov WORD PTR _a$[esp+48], ax\n\n; unsigned char b[10] = { 0 };\n\nmov BYTE PTR _b$[esp+40], al\nmov DWORD PTR _b$[esp+41], eax\nmov DWORD PTR _b$[esp+45], eax\nmov BYTE PTR _b$[esp+49], al\n\n; unsigned char c[10];\n; memset(c, 0, sizeof(c));\n\nmov DWORD PTR _c$[esp+40], eax\nmov DWORD PTR _c$[esp+44], eax\nmov WORD PTR _c$[esp+48], ax\n```\n\n```text\n/EHsc /FAs /O2\n```\n\n```text\nmemset()\n```\n\n========================================\n\nComments:\n- well, after reading comments below, it seems that msvc just isn't very consistent about zeroing memory.\n- Actually, this code very well could be optimal. The way instructions are pipelined on ARM could easily make it more efficient to strb then branch off and loop. That said, the performance difference would likely be negligible, and you're using an extra 4 bytes, so who knows.\n- Unlikely. You've got unaligned memory accesses (one byte and 9 bytes - ARM often has a 16 bits bus. That means read/modify/write!). Also, you've got extra register pressure : you need R3 as well.\n- wooh!! why on earth does it do that? :P at least you'd expect the explicit 0 initialization to first copy the value in al, to all bytes in eax. it's like an optimization was half-done for explicit initialization using 0.","metadata":{"transformedAt":"2026-08-18T18:32:17.765Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":243,"estimatedTokens":1513}}146{"id":"stack-7957963","source":"stackoverflow","questionId":7957963,"title":"How can I create an executable .exe PE file manually?","tags":["windows","compiler-construction","linker","executable","portable-executable"],"text":"Title: How can I create an executable .exe PE file manually?\nTags: windows, compiler-construction, linker, executable, portable-executable\nSource: Stack Overflow\n\nQuestion:\nAll texts on how to create a compiler stop after explaining lexers and parsers. They don't explain how to create the machine code. I want to understand the end-to-end process.\n\nCurrently what I understand is that, the Windows exe file formats are called Portable Executable. I read about the headers it has and am yet to find a resource which explains this easily.\n\nMy next issue is, I don't see any resource which explains how machine code is stored in the file. Is it like 32-bit fixed length instructions stored one after another in the `.text` section?\n\nIs there any place which at least explains how to create an exe file which does nothing (it has a No Op instruction). My next step then would be linking to dll files to print to console.\n\n========================================\n\nTop Answer:\nAs in many of his articles, I'd say Matt Pietrek's piece about PE internals remains the best introdction to the matter more than a decade after being written.\n\n========================================\n\nCode:\n```text\n.text\n```\n\n========================================\n\nComments:\n- Note that different systems have different representations for executable files.\n- He mentioned Windows... I think that's what he's referring to, specifically (The EXE format).\n- Yes, I would like to focus on Windows first. When I am comfortable with this, I can move on to ELF.\n- This is not answer-worthy, but Microsoft implements a version of the COFF format, with a description here: msdn.microsoft.com/en-us/windows/hardware/gg463119\n- possible duplicate of How to read / write .exe machine code manually?\n- I am unable to find anything useful on VX Heavens. It has some links to, I guess Russian sites.\n- -EDIT- Found out that I need to search from the box to get to the links. The more direct link could be forum.vxheavens.com/viewtopic.php?id=186\n- Just tried myself, and yes your right. First time I've ever seen any problems with the site. Unfortunately, nothing I can help with, I don't run the site, I guess you'll need to take a look and see if there is any help/admin link to contact the site owners. As I said it's run by the people that run \"Gamedev.Net\" so maybe worth going there, and asking around.\n- You can still find it on web.archive.org, but the downloads no longer work. A web search for \"Bernd Luevelsmeyer pe file format\" might work better.\n- Crying shame that it's not still live though :-( Over the years I contributed some of those format documents to the project myself. Up in my loft somewhere I actually still have a whole bunch of the docs printed out on a very old dot matrix printer and stored in a big ring-binder.\n- @user3789797 don't you think that would be better served as an answer in it's own right, rather than a comment on my answer, you are after all answering the question directly as opposed to adding anything extra to the comments on mine.\n- nope, afraid not. It is about the same subject that much is true, but the docs I was referring to where the original \"wottsists file format docs\", not the tiny PE project, hence as I say, your better off as a stand alone answer.\n- I don't see anything related to my question.\n- from your question: \"All texts on how to create a compiler stop after explaining lexers and parsers. They don't explain how to create the machine code. I want to understand the end-to-end process\". The book cited (see chapter 3 for \"Hello World\"-like program) explains how to write assembly programs, compile it into machine language, and then link it creating an executable file.\n- Your links are helpful. One thing missing is the code generation part. What exactly do you mean by that they don't have pure machine code?\n- One more note. I use 7zip to extract the different sections from exe or dll. This is very simple.\n- 1. When you say, the code generation part, you mean how to create the ELF file? 2. Well, pure machine code is not readable code. But, an ELF file has some \"metadata\" attached with it. I will update my answer then maybe we can read to an answer.\n- Yes I want to understand the final step. I am pretty clear what ELF and PE means. By code generation I mean just the machine code. The documents on PE doesn't throw any light on that.\n- A friend suggested - inst.eecs.berkeley.edu/~cs164/fa11. This looks pretty good.\n- No longer available. This link on his Wikipedia entry still works, but is two decades old...","metadata":{"transformedAt":"2026-08-18T18:32:17.765Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":48,"estimatedTokens":1139}}147{"id":"stack-4202181","source":"stackoverflow","questionId":4202181,"title":"Explanation about viable prefix","tags":["parsing","compiler-construction"],"text":"Title: Explanation about viable prefix\nTags: parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn Ullman's book of compilers, in shift reduce parsing, following definition of viable prefix is given : \n\n\"The set of prefixes of right sentential forms that can appear on the stack of a shift-reduce parser are called viable prefixes. An equivalent definition of a viable prefix is that it is a prefix of right sentential form that does not continue past the right end of the rightmost handle of that sentential form. By this definition, it is always possible to add terminal symbols to the end of a viable prefix to obtain a right sentential form. Therefore, there is apparently no error as long as the portion of the input seen to a given point can be reduced to a viable prefix.\" \n\nI can't understand this definition. Could someone explain the meaning of viable prefix with an example ?\n\nIn particular, please explain the meaning of\n\n\"An equivalent definition of a viable prefix is that it is a prefix of right sentential form that does not continue past the right end of the rightmost handle of that sentential form\"\n\n========================================\n\nTop Answer:\nConsider the grammar given in book(i'm restating it here)\n\n```\nE -> E+T | T\nT -> T*F | F \nF -> (E) | id\n```\n\nwhich is augmented by adding E' -> E in it\n\nNow have a look at this derivation,\n\n```\nE' -> E\n -> E+T\n -> E+T*F\n```\n\nClaim **E+T*** is a viable prefix\n\nArgument: This derivation is a right sentential form & E+T* is a prefix of it.\nHandle currently is T*F (as reducing T*F to T we can reach the start symbol & hence a successful parse)\n\nAnd hence, **E+T*** is a viable prefix as it is a prefix of right sentential form & doesn't extend past the rightmost handle for this sentential form. :)\n\nOther way to define it is:\n\n```\nThe prefixes of right sentential forms that can appear on the stack of a shiftreduce\nparser are called viable prefixes.\n```\n\n========================================\n\nCode:\n```text\nexpr -> expr + term | term\n    term -> term * factor | factor\n    factor -> NUMBER | ( expr )\n```\n\n```text\nexpr -> expr + term\n         -> expr + term * factor\n         -> expr + term * NUMBER\n         -> expr + factor * NUMBER\n         -> expr + NUMBER * NUMBER\n         -> expr + term + NUMBER * NUMBER\n         -> expr + NUMBER + NUMBER * NUMBER\n         -> term + NUMBER + NUMBER * NUMBER\n         -> NUMBER + NUMBER + NUMBER * NUMBER\n```\n\n```text\nterm * factor\n```\n\n```text\nterm\n```\n\n```text\nterm\n```\n\n```text\nE -> E+T | T\nT -> T*F | F \nF -> (E) | id\n```\n\n```text\nE' -> E\n   -> E+T\n   -> E+T*F\n```\n\n```text\nThe prefixes of right sentential forms that can appear on the stack of a shiftreduce\nparser are called viable prefixes.\n```\n\n========================================\n\nComments:\n- This just shows how experts (book authors) can make the learning experience so terribly complicated. They seem to think that concise language is understandable language.\n- @Domi please can you suggest some compiler text, having the same content as the ullman text but the language is lucid. It shall be very helpful. Thank you...\n- Check this. The portion of an item before the dot represents a viable prefix. A viable prefix is a string of grammar symbols that can comprise the first part of the right side of a production. Example: a, aX, aXY, and aXYb are viable prefixes of the production A -> aXYb.\n- @Shashwat I believe that's another way of explaining the same thing: `aXYb` is a simple phrase since it makes up the right side of a production, and the 'dot' in the linked notes is effectively a cursor representing a point of progress through potential inputs. I suppose it's meaningful to discuss viable prefixes of an individual production (and useful when constructing a shift-reduce parser's finite state machine), but a finished shift-reduce parser is normally interested primarily in viable prefixes of a complete sentential form.\n- @JeffreyHantin, If for every CFG the set of viable prefixes of that grammar is regular language than why LR parsers can not detect handle occurrence in case of unambigous CFG of NCFL?\n- I mean if set of all viable prefix of context free grammar is already a regular then why LR parsers fail particularly in case of non-deterministic context free languages?\n- @VimalPatel LR parsers, and deterministic push-down automata in general, have bounded lookahead. NCFL may require *unbounded* lookahead to recognize, as in the case of the language of all even-length bit strings that are palindromes. Nondeterministic push-down automata bypass the bound by exploring multiple potential parsing paths, and are therefore strictly more powerful than deterministic push-down automata. This may be counterintuitive given that there is no similar power disparity between DFA and NFA.","metadata":{"transformedAt":"2026-08-18T18:32:17.765Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":110,"estimatedTokens":1202}}148{"id":"stack-6376662","source":"stackoverflow","questionId":6376662,"title":"What would an AST (abstract syntax tree) for an object-oriented programming language look like?","tags":["java","compiler-construction","programming-languages","abstract-syntax-tree"],"text":"Title: What would an AST (abstract syntax tree) for an object-oriented programming language look like?\nTags: java, compiler-construction, programming-languages, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI'm reading about AST (abstract syntax trees) but all the samples I see use expressions such as: \n\n```\na + b * c\n```\n\nWhich could be represented in a lispy like syntax as:\n\n```\n(+ a (* b c) )\n```\n\nWhich will be the equivalent to: \n\n```\n+\n / \\\na * \n / \\\n b c\n```\n\nMy question is How an AST for a class in a OOPL would look like?\n\nMy naive attempt is for this Java code:\n\n```\nclass Person { \n String name;\n int age;\n public String toString() { \n return \"name\";\n }\n }\n```\n\nIs: \n\n```\n;Hand written\n(classDeclaration Person \n (varDeclaration String name)\n (varDeclaration int age )\n (funcDeclaration String toString \n (return \"name\")\n )\n )\n```\n\nBut I'm not quite sure how close or far am I to a real AST representation.\n\nDoes it depends on the language I choose. How much detail is needed? Are those \"xyzDeclaraction\" needed or could be as:\n\n```\n(Person (String name) (int age))\n```\n\nWhere can I see a \"real\" representation of an actual programming language to learn more.\n\n========================================\n\nTop Answer:\nOP: *Where can I see a real representation of an actual programming language to learn more?*\n\nFor your source text as a file Person.java:\n\n```\nclass Person { \n String name;\n int age;\n public String toString()\n { return \"name\"; } \n}\n```\n\nwhat follows are both Concrete and Abstract Syntax Tree in an S-expression-style dump of the parser tree from our DMS Software Reengineering Toolkit, using its Java1.6 parser. All the apparant complexity is pretty much caused by the real complexity of the language (e.g., of Java itself). \n\nThe CST clearly contains more stuff (139 nodes) than the AST (54 nodes). The AST drops everything that can be automatically inferred from the grammar, given the AST. This includes removing non-value-carrying leaves, unary productions, and compressing spines caused by left or right recursive grammar rules into explicit list nodes.\n\nA left paren signals a new subtree. Following the left paren is the name of the node type; @Java~Java1_.6 might seem unnecessary until you understand DMS can handle many languages at once, including langauges nested inside one another. The #nnnnnn is the memory address of the node. ^M means \"this node has M parents and is left off when M==1. Things inside [...] are the node value. A { M } means this list node has M list-children. Each node is stamped with position information.\n\nThis is the Concrete Syntax tree (see further down for AST):\n\n```\n(compilation_unit@Java~Java1_6=1#4885d00^0 Line 1 Column 1 File C:/temp/Person.java\n (type_declarations@Java~Java1_6=15#4885cc0 Line 1 Column 1 File C:/temp/Person.java\n (type_declarations@Java~Java1_6=16#4884d80 Line 1 Column 1 File C:/temp/Person.java)type_declarations\n (type_declaration@Java~Java1_6=17#4885ca0 Line 1 Column 1 File C:/temp/Person.java\n (type_class_modifiers@Java~Java1_6=77#4884dc0 Line 1 Column 1 File C:/temp/Person.java)type_class_modifiers\n (class_header@Java~Java1_6=89#4884ec0 Line 1 Column 1 File C:/temp/Person.java\n |('class'@Java~Java1_6=459#4884c60[Keyword:0] Line 1 Column 1 File C:/temp/Person.java)'class'\n |(IDENTIFIER@Java~Java1_6=447#4884e20[`Person'] Line 1 Column 7 File C:/temp/Person.java)IDENTIFIER\n |(type_parameters@Java~Java1_6=408#4884e80 Line 1 Column 14 File C:/temp/Person.java)type_parameters\n )class_header\n (class_body@Java~Java1_6=94#4885c80 Line 1 Column 14 File C:/temp/Person.java\n |('{'@Java~Java1_6=448#4884e60[Keyword:0] Line 1 Column 14 File C:/temp/Person.java)'{'\n |(class_body_declarations@Java~Java1_6=111#4885c60 Line 2 Column 5 File C:/temp/Person.java\n | (class_body_declarations@Java~Java1_6=111#4885380 Line 2 Column 5 File C:/temp/Person.java\n | (class_body_declarations@Java~Java1_6=110#4885400 Line 2 Column 5 File C:/temp/Person.java\n | (class_body_declaration@Java~Java1_6=118#4885360 Line 2 Column 5 File C:/temp/Person.java\n | |(field_declaration@Java~Java1_6=168#4885440 Line 2 Column 5 File C:/temp/Person.java\n | | (field_modifiers@Java~Java1_6=170#4884f40 Line 2 Column 5 File C:/temp/Person.java)field_modifiers\n | | (type@Java~Java1_6=191#48852c0 Line 2 Column 5 File C:/temp/Person.java\n | | (name@Java~Java1_6=406#48851e0 Line 2 Column 5 File C:/temp/Person.java\n | | (IDENTIFIER@Java~Java1_6=447#4884f20[`String'] Line 2 Column 5 File C:/temp/Person.java)IDENTIFIER\n | | (type_arguments@Java~Java1_6=407#4885160 Line 2 Column 12 File C:/temp/Person.java)type_arguments\n | | )name\n | | (brackets@Java~Java1_6=157#4885260 Line 2 Column 12 File C:/temp/Person.java)brackets\n | | )type\n | | (variable_declarator_list@Java~Java1_6=179#4884e00 Line 2 Column 12 File C:/temp/Person.java\n | | (variable_declarator@Java~Java1_6=181#4885300 Line 2 Column 12 File C:/temp/Person.java\n | | (variable_declarator_id@Java~Java1_6=167#4885320 Line 2 Column 12 File C:/temp/Person.java\n | | |(IDENTIFIER@Java~Java1_6=447#4885140[`name'] Line 2 Column 12 File C:/temp/Person.java)IDENTIFIER\n | | |(brackets@Java~Java1_6=157#4885040 Line 2 Column 16 File C:/temp/Person.java)brackets\n | | )variable_declarator_id\n | | )variable_declarator\n | | )variable_declarator_list\n | | (';'@Java~Java1_6=440#4885100[Keyword:0] Line 2 Column 16 File C:/temp/Person.java)';'\n | |)field_declaration\n | )class_body_declaration\n | )class_body_declarations\n | (class_body_declaration@Java~Java1_6=118#48852e0 Line 3 Column 5 File C:/temp/Person.java\n | (field_declaration@Java~Java1_6=168#4885480 Line 3 Column 5 File C:/temp/Person.java\n | |(field_modifiers@Java~Java1_6=170#4885340 Line 3 Column 5 File C:/temp/Person.java)field_modifiers\n | |(type@Java~Java1_6=192#4885220 Line 3 Column 5 File C:/temp/Person.java\n | | (primitive_type@Java~Java1_6=198#4885420 Line 3 Column 5 File C:/temp/Person.java\n | | ('int'@Java~Java1_6=479#48853e0[Keyword:0] Line 3 Column 5 File C:/temp/Person.java)'int'\n | | )primitive_type\n | | (brackets@Java~Java1_6=157#4885200 Line 3 Column 12 File C:/temp/Person.java)brackets\n | |)type\n | |(variable_declarator_list@Java~Java1_6=179#4885540 Line 3 Column 12 File C:/temp/Person.java\n | | (variable_declarator@Java~Java1_6=181#4885520 Line 3 Column 12 File C:/temp/Person.java\n | | (variable_declarator_id@Java~Java1_6=167#4885500 Line 3 Column 12 File C:/temp/Person.java\n | | (IDENTIFIER@Java~Java1_6=447#4884fc0[`age'] Line 3 Column 12 File C:/temp/Person.java)IDENTIFIER\n | | (brackets@Java~Java1_6=157#48854e0 Line 3 Column 15 File C:/temp/Person.java)brackets\n | | )variable_declarator_id\n | | )variable_declarator\n | |)variable_declarator_list\n | |(';'@Java~Java1_6=440#48854c0[Keyword:0] Line 3 Column 15 File C:/temp/Person.java)';'\n | )field_declaration\n | )class_body_declaration\n | )class_body_declarations\n | (class_body_declaration@Java~Java1_6=117#4885c40 Line 4 Column 5 File C:/temp/Person.java\n | (method_declaration@Java~Java1_6=135#4885c00 Line 4 Column 5 File C:/temp/Person.java\n | (method_modifiers@Java~Java1_6=141#4885700 Line 4 Column 5 File C:/temp/Person.java\n | |(method_modifiers@Java~Java1_6=142#4884e40 Line 4 Column 5 File C:/temp/Person.java)method_modifiers\n | |(method_modifier@Java~Java1_6=147#48856a0 Line 4 Column 5 File C:/temp/Person.java\n | | ('public'@Java~Java1_6=453#48853a0[Keyword:0] Line 4 Column 5 File C:/temp/Person.java)'public'\n | |)method_modifier\n | )method_modifiers\n | (type_parameters@Java~Java1_6=408#4885740 Line 4 Column 12 File C:/temp/Person.java)type_parameters\n | (type@Java~Java1_6=191#4885900 Line 4 Column 12 File C:/temp/Person.java\n | |(name@Java~Java1_6=406#48852a0 Line 4 Column 12 File C:/temp/Person.java\n | | (IDENTIFIER@Java~Java1_6=447#4885660[`String'] Line 4 Column 12 File C:/temp/Person.java)IDENTIFIER\n | | (type_arguments@Java~Java1_6=407#48851a0 Line 4 Column 19 File C:/temp/Person.java)type_arguments\n | |)name\n | |(brackets@Java~Java1_6=157#48858c0 Line 4 Column 19 File C:/temp/Person.java)brackets\n | )type\n | (IDENTIFIER@Java~Java1_6=447#48855c0[`toString'] Line 4 Column 19 File C:/temp/Person.java)IDENTIFIER\n | (parameters@Java~Java1_6=158#48858e0 Line 4 Column 27 File C:/temp/Person.java\n | |('('@Java~Java1_6=450#4885840[Keyword:0] Line 4 Column 27 File C:/temp/Person.java)'('\n | |(')'@Java~Java1_6=451#4885620[Keyword:0] Line 4 Column 28 File C:/temp/Person.java)')'\n | )parameters\n | (brackets@Java~Java1_6=157#4885060 Line 5 Column 7 File C:/temp/Person.java)brackets\n | (block@Java~Java1_6=217#4885be0 Line 5 Column 7 File C:/temp/Person.java\n | |('{'@Java~Java1_6=448#48851c0[Keyword:0] Line 5 Column 7 File C:/temp/Person.java)'{'\n | |(statement_sequence@Java~Java1_6=218#4885ba0 Line 5 Column 9 File C:/temp/Person.java\n | | (statement_sequence_member@Java~Java1_6=223#4885b80 Line 5 Column 9 File C:/temp/Person.java\n | | (executable_statement@Java~Java1_6=243#4885b60 Line 5 Column 9 File C:/temp/Person.java\n | | ('return'@Java~Java1_6=491#4884f60[Keyword:0] Line 5 Column 9 File C:/temp/Person.java)'return'\n | | (expression@Java~Java1_6=332#4885ac0 Line 5 Column 16 File C:/temp/Person.java\n | | |(conditional_expression@Java~Java1_6=345#4885a60 Line 5 Column 16 File C:/temp/Person.java\n | | | (conditional_or_expression@Java~Java1_6=347#4885a20 Line 5 Column 16 File C:/temp/Person.java\n | | | (conditional_and_expression@Java~Java1_6=349#48859e0 Line 5 Column 16 File C:/temp/Person.java\n | | | (inclusive_or_expression@Java~Java1_6=351#48857e0 Line 5 Column 16 File C:/temp/Person.java\n | | | |(exclusive_or_expression@Java~Java1_6=353#48855a0 Line 5 Column 16 File C:/temp/Person.java\n | | | | (and_expression@Java~Java1_6=355#4885940 Line 5 Column 16 File C:/temp/Person.java\n | | | | (equality_expression@Java~Java1_6=357#4885880 Line 5 Column 16 File C:/temp/Person.java\n | | | | (relational_expression@Java~Java1_6=360#4885800 Line 5 Column 16 File C:/temp/Person.java\n | | | | |(shift_expression@Java~Java1_6=366#48856c0 Line 5 Column 16 File C:/temp/Person.java\n | | | | | (additive_expression@Java~Java1_6=370#4885180 Line 5 Column 16 File C:/temp/Person.java\n | | | | | (multiplicative_expression@Java~Java1_6=373#4885780 Line 5 Column 16 File C:/temp/Person.java\n | | | | | (unary_expression@Java~Java1_6=383#4885600 Line 5 Column 16 File C:/temp/Person.java\n | | | | | |(unary_expression_not_plus_minus@Java~Java1_6=389#4885680 Line 5 Column 16 File C:/temp/Person.java\n | | | | | | (literal@Java~Java1_6=390#4884f80 Line 5 Column 16 File C:/temp/Person.java\n | | | | | | (STRING@Java~Java1_6=536#4885120[`name'] Line 5 Column 16 File C:/temp/Person.java)STRING\n | | | | | | )literal\n | | | | | |)unary_expression_not_plus_minus\n | | | | | )unary_expression\n | | | | | )multiplicative_expression\n | | | | | )additive_expression\n | | | | |)shift_expression\n | | | | )relational_expression\n | | | | )equality_expression\n | | | | )and_expression\n | | | |)exclusive_or_expression\n | | | )inclusive_or_expression\n | | | )conditional_and_expression\n | | | )conditional_or_expression\n | | |)conditional_expression\n | | )expression\n | | (';'@Java~Java1_6=440#48856e0[Keyword:0] Line 5 Column 22 File C:/temp/Person.java)';'\n | | )executable_statement\n | | )statement_sequence_member\n | |)statement_sequence\n | |('}'@Java~Java1_6=449#4885b40[Keyword:0] Line 5 Column 28 File C:/temp/Person.java)'}'\n | )block\n | )method_declaration\n | )class_body_declaration\n |)class_body_declarations\n |('}'@Java~Java1_6=449#4885bc0[Keyword:0] Line 6 Column 1 File C:/temp/Person.java)'}'\n )class_body\n )type_declaration\n )type_declarations\n (optional_CONTROL_Z@Java~Java1_6=5#4885ce0 Line 7 Column 1 File C:/temp/Person.java)optional_CONTROL_Z\n)compilation_unit\n```\n\nThis is the AST (automatically generated by DMS from the CST):\n\n```\n(compilation_unit@Java~Java1_6=1#486f900^0 Line 1 Column 1 File C:/temp/Person.java\n (type_declarations@Java~Java1_6=15#486f4c0 {1} Line 1 Column 1 File C:/temp/Person.java\n (type_declaration@Java~Java1_6=17#486f5e0 Line 1 Column 1 File C:/temp/Person.java\n (type_class_modifiers@Java~Java1_6=77#486eda0 Line 1 Column 1 File C:/temp/Person.java)type_class_modifiers\n (class_header@Java~Java1_6=89#486ee60 Line 1 Column 1 File C:/temp/Person.java\n |(IDENTIFIER@Java~Java1_6=447#486ede0[`Person'] Line 1 Column 7 File C:/temp/Person.java)IDENTIFIER\n |(type_parameters@Java~Java1_6=408#486ee20 Line 1 Column 14 File C:/temp/Person.java)type_parameters\n )class_header\n (class_body@Java~Java1_6=94#486f040 Line 1 Column 14 File C:/temp/Person.java\n |(class_body_declarations@Java~Java1_6=111#486ee40 {3} Line 2 Column 5 File C:/temp/Person.java\n | (class_body_declaration@Java~Java1_6=118#486f300 Line 2 Column 5 File C:/temp/Person.java\n | (field_declaration@Java~Java1_6=168#486f380 Line 2 Column 5 File C:/temp/Person.java\n | (field_modifiers@Java~Java1_6=170#486eec0 Line 2 Column 5 File C:/temp/Person.java)field_modifiers\n | (type@Java~Java1_6=191#486f240 Line 2 Column 5 File C:/temp/Person.java\n | |(name@Java~Java1_6=406#486f180 Line 2 Column 5 File C:/temp/Person.java\n | | (IDENTIFIER@Java~Java1_6=447#486eea0[`String'] Line 2 Column 5 File C:/temp/Person.java)IDENTIFIER\n | | (type_arguments@Java~Java1_6=407#486f0e0 Line 2 Column 12 File C:/temp/Person.java)type_arguments\n | |)name\n | |(brackets@Java~Java1_6=157#486f200 Line 2 Column 12 File C:/temp/Person.java)brackets\n | )type\n | (variable_declarator@Java~Java1_6=181#486ef20 Line 2 Column 12 File C:/temp/Person.java\n | |(variable_declarator_id@Java~Java1_6=167#486efe0 Line 2 Column 12 File C:/temp/Person.java\n | | (IDENTIFIER@Java~Java1_6=447#486f0c0[`name'] Line 2 Column 12 File C:/temp/Person.java)IDENTIFIER\n | | (brackets@Java~Java1_6=157#486f060 Line 2 Column 16 File C:/temp/Person.java)brackets\n | |)variable_declarator_id\n | )variable_declarator\n | )field_declaration\n | )class_body_declaration\n | (class_body_declaration@Java~Java1_6=118#486f000 Line 3 Column 5 File C:/temp/Person.java\n | (field_declaration@Java~Java1_6=168#486f320 Line 3 Column 5 File C:/temp/Person.java\n | (field_modifiers@Java~Java1_6=170#486f2a0 Line 3 Column 5 File C:/temp/Person.java)field_modifiers\n | (type@Java~Java1_6=192#486eee0 Line 3 Column 5 File C:/temp/Person.java\n | |(primitive_type@Java~Java1_6=198#486ef60 Line 3 Column 5 File C:/temp/Person.java)primitive_type\n | |(brackets@Java~Java1_6=157#486ee00 Line 3 Column 12 File C:/temp/Person.java)brackets\n | )type\n | (variable_declarator@Java~Java1_6=181#486f2c0 Line 3 Column 12 File C:/temp/Person.java\n | |(variable_declarator_id@Java~Java1_6=167#486f3a0 Line 3 Column 12 File C:/temp/Person.java\n | | (IDENTIFIER@Java~Java1_6=447#486f120[`age'] Line 3 Column 12 File C:/temp/Person.java)IDENTIFIER\n | | (brackets@Java~Java1_6=157#486ef00 Line 3 Column 15 File C:/temp/Person.java)brackets\n | |)variable_declarator_id\n | )variable_declarator\n | )field_declaration\n | )class_body_declaration\n | (class_body_declaration@Java~Java1_6=117#486f7a0 Line 4 Column 5 File C:/temp/Person.java\n | (method_declaration@Java~Java1_6=135#486f480 Line 4 Column 5 File C:/temp/Person.java\n | (method_modifiers@Java~Java1_6=141#486f460 {1} Line 4 Column 5 File C:/temp/Person.java\n | |(method_modifier@Java~Java1_6=147#486f400 Line 4 Column 5 File C:/temp/Person.java)method_modifier\n | )method_modifiers\n | (type_parameters@Java~Java1_6=408#486f540 Line 4 Column 12 File C:/temp/Person.java)type_parameters\n | (type@Java~Java1_6=191#486f740 Line 4 Column 12 File C:/temp/Person.java\n | |(name@Java~Java1_6=406#486f620 Line 4 Column 12 File C:/temp/Person.java\n | | (IDENTIFIER@Java~Java1_6=447#486f080[`String'] Line 4 Column 12 File C:/temp/Person.java)IDENTIFIER\n | | (type_arguments@Java~Java1_6=407#486f640 Line 4 Column 19 File C:/temp/Person.java)type_arguments\n | |)name\n | |(brackets@Java~Java1_6=157#486f700 Line 4 Column 19 File C:/temp/Person.java)brackets\n | )type\n | (IDENTIFIER@Java~Java1_6=447#486f140[`toString'] Line 4 Column 19 File C:/temp/Person.java)IDENTIFIER\n | (parameters@Java~Java1_6=158#486f760 Line 4 Column 27 File C:/temp/Person.java)parameters\n | (brackets@Java~Java1_6=157#486f820 Line 5 Column 7 File C:/temp/Person.java)brackets\n | (block@Java~Java1_6=217#486f780 Line 5 Column 7 File C:/temp/Person.java\n | |(statement_sequence@Java~Java1_6=218#486f6e0 Line 5 Column 9 File C:/temp/Person.java\n | | (statement_sequence_member@Java~Java1_6=223#486f6c0 Line 5 Column 9 File C:/temp/Person.java\n | | (executable_statement@Java~Java1_6=243#486f6a0 Line 5 Column 9 File C:/temp/Person.java\n | | (unary_expression_not_plus_minus@Java~Java1_6=389#486f720 Line 5 Column 16 File C:/temp/Person.java\n | | |(literal@Java~Java1_6=390#486f280 Line 5 Column 16 File C:/temp/Person.java\n | | | (STRING@Java~Java1_6=536#486f160[`name'] Line 5 Column 16 File C:/temp/Person.java)STRING\n | | |)literal\n | | )unary_expression_not_plus_minus\n | | )executable_statement\n | | )statement_sequence_member\n | |)statement_sequence\n | )block\n | )method_declaration\n | )class_body_declaration\n |)class_body_declarations\n )class_body\n )type_declaration\n )type_declarations\n (optional_CONTROL_Z@Java~Java1_6=5#486f4e0 Line 7 Column 1 File C:/temp/Person.java)optional_CONTROL_Z\n)compilation_unit\n```\n\nEDIT March 2015: Here's a link to some C++ AST examples\n\nEdit May 2015: DMS has long done Java 1.7 and 1.8, too.\n\n========================================\n\nCode:\n```text\na + b * c\n```\n\n```text\n(+ a (* b c) )\n```\n\n```text\n+\n / \\\na   * \n   / \\\n  b   c\n```\n\n```text\nclass Person { \n     String name;\n     int    age;\n     public String toString() { \n        return \"name\";\n     }\n }\n```\n\n```text\n;Hand written\n(classDeclaration Person \n     (varDeclaration String name)\n     (varDeclaration int    age )\n     (funcDeclaration String toString \n           (return \"name\")\n     )\n )\n```\n\n```text\n(Person (String name) (int age))\n```\n\n```text\nExp                    \n                      /  |  \\                   \n                     /   |   \\                       *\n                 Ident BinOp Ident       into       / \\\n                  /      |     \\                  \"x\" \"y\"\n                 /       |      \\\n               \"x\"       *      \"y\"\n```\n\n```text\nProgram\n    ClassDeclList\n        ClassDecl\n            Identifier\n                id: Person\n            VarDeclList\n                VarDecl\n                    type: String\n                    id: name\n                VarDecl\n                    type: int\n                    id: age\n            MethDeclList\n                MethodDecl\n                    modifiers: public\n                    returnType: String\n                    id: toString\n                    Formals\n                        (empty)\n                    StmtList\n                        returnStmt\n                            Identifier\n                                id: name\n```\n\n```text\nvarDeclList\n```\n\n```text\nmethDeclList\n```\n\n```text\nstmtList\n```\n\n```text\nclass Person {  \n    String name;\n    int    age;\n    public String toString()\n      { return \"name\";     } \n}\n```\n\n```text\n(compilation_unit@Java~Java1_6=1#4885d00^0 Line 1 Column 1 File C:/temp/Person.java\n (type_declarations@Java~Java1_6=15#4885cc0 Line 1 Column 1 File C:/temp/Person.java\n  (type_declarations@Java~Java1_6=16#4884d80 Line 1 Column 1 File C:/temp/Person.java)type_declarations\n  (type_declaration@Java~Java1_6=17#4885ca0 Line 1 Column 1 File C:/temp/Person.java\n   (type_class_modifiers@Java~Java1_6=77#4884dc0 Line 1 Column 1 File C:/temp/Person.java)type_class_modifiers\n   (class_header@Java~Java1_6=89#4884ec0 Line 1 Column 1 File C:/temp/Person.java\n   |('class'@Java~Java1_6=459#4884c60[Keyword:0] Line 1 Column 1 File C:/temp/Person.java)'class'\n   |(IDENTIFIER@Java~Java1_6=447#4884e20[`Person'] Line 1 Column 7 File C:/temp/Person.java)IDENTIFIER\n   |(type_parameters@Java~Java1_6=408#4884e80 Line 1 Column 14 File C:/temp/Person.java)type_parameters\n   )class_header\n   (class_body@Java~Java1_6=94#4885c80 Line 1 Column 14 File C:/temp/Person.java\n   |('{'@Java~Java1_6=448#4884e60[Keyword:0] Line 1 Column 14 File C:/temp/Person.java)'{'\n   |(class_body_declarations@Java~Java1_6=111#4885c60 Line 2 Column 5 File C:/temp/Person.java\n   | (class_body_declarations@Java~Java1_6=111#4885380 Line 2 Column 5 File C:/temp/Person.java\n   |  (class_body_declarations@Java~Java1_6=110#4885400 Line 2 Column 5 File C:/temp/Person.java\n   |   (class_body_declaration@Java~Java1_6=118#4885360 Line 2 Column 5 File C:/temp/Person.java\n   |   |(field_declaration@Java~Java1_6=168#4885440 Line 2 Column 5 File C:/temp/Person.java\n   |   | (field_modifiers@Java~Java1_6=170#4884f40 Line 2 Column 5 File C:/temp/Person.java)field_modifiers\n   |   | (type@Java~Java1_6=191#48852c0 Line 2 Column 5 File C:/temp/Person.java\n   |   |  (name@Java~Java1_6=406#48851e0 Line 2 Column 5 File C:/temp/Person.java\n   |   |   (IDENTIFIER@Java~Java1_6=447#4884f20[`String'] Line 2 Column 5 File C:/temp/Person.java)IDENTIFIER\n   |   |   (type_arguments@Java~Java1_6=407#4885160 Line 2 Column 12 File C:/temp/Person.java)type_arguments\n   |   |  )name\n   |   |  (brackets@Java~Java1_6=157#4885260 Line 2 Column 12 File C:/temp/Person.java)brackets\n   |   | )type\n   |   | (variable_declarator_list@Java~Java1_6=179#4884e00 Line 2 Column 12 File C:/temp/Person.java\n   |   |  (variable_declarator@Java~Java1_6=181#4885300 Line 2 Column 12 File C:/temp/Person.java\n   |   |   (variable_declarator_id@Java~Java1_6=167#4885320 Line 2 Column 12 File C:/temp/Person.java\n   |   |   |(IDENTIFIER@Java~Java1_6=447#4885140[`name'] Line 2 Column 12 File C:/temp/Person.java)IDENTIFIER\n   |   |   |(brackets@Java~Java1_6=157#4885040 Line 2 Column 16 File C:/temp/Person.java)brackets\n   |   |   )variable_declarator_id\n   |   |  )variable_declarator\n   |   | )variable_declarator_list\n   |   | (';'@Java~Java1_6=440#4885100[Keyword:0] Line 2 Column 16 File C:/temp/Person.java)';'\n   |   |)field_declaration\n   |   )class_body_declaration\n   |  )class_body_declarations\n   |  (class_body_declaration@Java~Java1_6=118#48852e0 Line 3 Column 5 File C:/temp/Person.java\n   |   (field_declaration@Java~Java1_6=168#4885480 Line 3 Column 5 File C:/temp/Person.java\n   |   |(field_modifiers@Java~Java1_6=170#4885340 Line 3 Column 5 File C:/temp/Person.java)field_modifiers\n   |   |(type@Java~Java1_6=192#4885220 Line 3 Column 5 File C:/temp/Person.java\n   |   | (primitive_type@Java~Java1_6=198#4885420 Line 3 Column 5 File C:/temp/Person.java\n   |   |  ('int'@Java~Java1_6=479#48853e0[Keyword:0] Line 3 Column 5 File C:/temp/Person.java)'int'\n   |   | )primitive_type\n   |   | (brackets@Java~Java1_6=157#4885200 Line 3 Column 12 File C:/temp/Person.java)brackets\n   |   |)type\n   |   |(variable_declarator_list@Java~Java1_6=179#4885540 Line 3 Column 12 File C:/temp/Person.java\n   |   | (variable_declarator@Java~Java1_6=181#4885520 Line 3 Column 12 File C:/temp/Person.java\n   |   |  (variable_declarator_id@Java~Java1_6=167#4885500 Line 3 Column 12 File C:/temp/Person.java\n   |   |   (IDENTIFIER@Java~Java1_6=447#4884fc0[`age'] Line 3 Column 12 File C:/temp/Person.java)IDENTIFIER\n   |   |   (brackets@Java~Java1_6=157#48854e0 Line 3 Column 15 File C:/temp/Person.java)brackets\n   |   |  )variable_declarator_id\n   |   | )variable_declarator\n   |   |)variable_declarator_list\n   |   |(';'@Java~Java1_6=440#48854c0[Keyword:0] Line 3 Column 15 File C:/temp/Person.java)';'\n   |   )field_declaration\n   |  )class_body_declaration\n   | )class_body_declarations\n   | (class_body_declaration@Java~Java1_6=117#4885c40 Line 4 Column 5 File C:/temp/Person.java\n   |  (method_declaration@Java~Java1_6=135#4885c00 Line 4 Column 5 File C:/temp/Person.java\n   |   (method_modifiers@Java~Java1_6=141#4885700 Line 4 Column 5 File C:/temp/Person.java\n   |   |(method_modifiers@Java~Java1_6=142#4884e40 Line 4 Column 5 File C:/temp/Person.java)method_modifiers\n   |   |(method_modifier@Java~Java1_6=147#48856a0 Line 4 Column 5 File C:/temp/Person.java\n   |   | ('public'@Java~Java1_6=453#48853a0[Keyword:0] Line 4 Column 5 File C:/temp/Person.java)'public'\n   |   |)method_modifier\n   |   )method_modifiers\n   |   (type_parameters@Java~Java1_6=408#4885740 Line 4 Column 12 File C:/temp/Person.java)type_parameters\n   |   (type@Java~Java1_6=191#4885900 Line 4 Column 12 File C:/temp/Person.java\n   |   |(name@Java~Java1_6=406#48852a0 Line 4 Column 12 File C:/temp/Person.java\n   |   | (IDENTIFIER@Java~Java1_6=447#4885660[`String'] Line 4 Column 12 File C:/temp/Person.java)IDENTIFIER\n   |   | (type_arguments@Java~Java1_6=407#48851a0 Line 4 Column 19 File C:/temp/Person.java)type_arguments\n   |   |)name\n   |   |(brackets@Java~Java1_6=157#48858c0 Line 4 Column 19 File C:/temp/Person.java)brackets\n   |   )type\n   |   (IDENTIFIER@Java~Java1_6=447#48855c0[`toString'] Line 4 Column 19 File C:/temp/Person.java)IDENTIFIER\n   |   (parameters@Java~Java1_6=158#48858e0 Line 4 Column 27 File C:/temp/Person.java\n   |   |('('@Java~Java1_6=450#4885840[Keyword:0] Line 4 Column 27 File C:/temp/Person.java)'('\n   |   |(')'@Java~Java1_6=451#4885620[Keyword:0] Line 4 Column 28 File C:/temp/Person.java)')'\n   |   )parameters\n   |   (brackets@Java~Java1_6=157#4885060 Line 5 Column 7 File C:/temp/Person.java)brackets\n   |   (block@Java~Java1_6=217#4885be0 Line 5 Column 7 File C:/temp/Person.java\n   |   |('{'@Java~Java1_6=448#48851c0[Keyword:0] Line 5 Column 7 File C:/temp/Person.java)'{'\n   |   |(statement_sequence@Java~Java1_6=218#4885ba0 Line 5 Column 9 File C:/temp/Person.java\n   |   | (statement_sequence_member@Java~Java1_6=223#4885b80 Line 5 Column 9 File C:/temp/Person.java\n   |   |  (executable_statement@Java~Java1_6=243#4885b60 Line 5 Column 9 File C:/temp/Person.java\n   |   |   ('return'@Java~Java1_6=491#4884f60[Keyword:0] Line 5 Column 9 File C:/temp/Person.java)'return'\n   |   |   (expression@Java~Java1_6=332#4885ac0 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |(conditional_expression@Java~Java1_6=345#4885a60 Line 5 Column 16 File C:/temp/Person.java\n   |   |   | (conditional_or_expression@Java~Java1_6=347#4885a20 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |  (conditional_and_expression@Java~Java1_6=349#48859e0 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |   (inclusive_or_expression@Java~Java1_6=351#48857e0 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |   |(exclusive_or_expression@Java~Java1_6=353#48855a0 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |   | (and_expression@Java~Java1_6=355#4885940 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |   |  (equality_expression@Java~Java1_6=357#4885880 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |   |   (relational_expression@Java~Java1_6=360#4885800 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |   |   |(shift_expression@Java~Java1_6=366#48856c0 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |   |   | (additive_expression@Java~Java1_6=370#4885180 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |   |   |  (multiplicative_expression@Java~Java1_6=373#4885780 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |   |   |   (unary_expression@Java~Java1_6=383#4885600 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |   |   |   |(unary_expression_not_plus_minus@Java~Java1_6=389#4885680 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |   |   |   | (literal@Java~Java1_6=390#4884f80 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |   |   |   |  (STRING@Java~Java1_6=536#4885120[`name'] Line 5 Column 16 File C:/temp/Person.java)STRING\n   |   |   |   |   |   | )literal\n   |   |   |   |   |   |)unary_expression_not_plus_minus\n   |   |   |   |   |   )unary_expression\n   |   |   |   |   |  )multiplicative_expression\n   |   |   |   |   | )additive_expression\n   |   |   |   |   |)shift_expression\n   |   |   |   |   )relational_expression\n   |   |   |   |  )equality_expression\n   |   |   |   | )and_expression\n   |   |   |   |)exclusive_or_expression\n   |   |   |   )inclusive_or_expression\n   |   |   |  )conditional_and_expression\n   |   |   | )conditional_or_expression\n   |   |   |)conditional_expression\n   |   |   )expression\n   |   |   (';'@Java~Java1_6=440#48856e0[Keyword:0] Line 5 Column 22 File C:/temp/Person.java)';'\n   |   |  )executable_statement\n   |   | )statement_sequence_member\n   |   |)statement_sequence\n   |   |('}'@Java~Java1_6=449#4885b40[Keyword:0] Line 5 Column 28 File C:/temp/Person.java)'}'\n   |   )block\n   |  )method_declaration\n   | )class_body_declaration\n   |)class_body_declarations\n   |('}'@Java~Java1_6=449#4885bc0[Keyword:0] Line 6 Column 1 File C:/temp/Person.java)'}'\n   )class_body\n  )type_declaration\n )type_declarations\n (optional_CONTROL_Z@Java~Java1_6=5#4885ce0 Line 7 Column 1 File C:/temp/Person.java)optional_CONTROL_Z\n)compilation_unit\n```\n\n```text\n(compilation_unit@Java~Java1_6=1#486f900^0 Line 1 Column 1 File C:/temp/Person.java\n (type_declarations@Java~Java1_6=15#486f4c0 {1} Line 1 Column 1 File C:/temp/Person.java\n  (type_declaration@Java~Java1_6=17#486f5e0 Line 1 Column 1 File C:/temp/Person.java\n   (type_class_modifiers@Java~Java1_6=77#486eda0 Line 1 Column 1 File C:/temp/Person.java)type_class_modifiers\n   (class_header@Java~Java1_6=89#486ee60 Line 1 Column 1 File C:/temp/Person.java\n   |(IDENTIFIER@Java~Java1_6=447#486ede0[`Person'] Line 1 Column 7 File C:/temp/Person.java)IDENTIFIER\n   |(type_parameters@Java~Java1_6=408#486ee20 Line 1 Column 14 File C:/temp/Person.java)type_parameters\n   )class_header\n   (class_body@Java~Java1_6=94#486f040 Line 1 Column 14 File C:/temp/Person.java\n   |(class_body_declarations@Java~Java1_6=111#486ee40 {3} Line 2 Column 5 File C:/temp/Person.java\n   | (class_body_declaration@Java~Java1_6=118#486f300 Line 2 Column 5 File C:/temp/Person.java\n   |  (field_declaration@Java~Java1_6=168#486f380 Line 2 Column 5 File C:/temp/Person.java\n   |   (field_modifiers@Java~Java1_6=170#486eec0 Line 2 Column 5 File C:/temp/Person.java)field_modifiers\n   |   (type@Java~Java1_6=191#486f240 Line 2 Column 5 File C:/temp/Person.java\n   |   |(name@Java~Java1_6=406#486f180 Line 2 Column 5 File C:/temp/Person.java\n   |   | (IDENTIFIER@Java~Java1_6=447#486eea0[`String'] Line 2 Column 5 File C:/temp/Person.java)IDENTIFIER\n   |   | (type_arguments@Java~Java1_6=407#486f0e0 Line 2 Column 12 File C:/temp/Person.java)type_arguments\n   |   |)name\n   |   |(brackets@Java~Java1_6=157#486f200 Line 2 Column 12 File C:/temp/Person.java)brackets\n   |   )type\n   |   (variable_declarator@Java~Java1_6=181#486ef20 Line 2 Column 12 File C:/temp/Person.java\n   |   |(variable_declarator_id@Java~Java1_6=167#486efe0 Line 2 Column 12 File C:/temp/Person.java\n   |   | (IDENTIFIER@Java~Java1_6=447#486f0c0[`name'] Line 2 Column 12 File C:/temp/Person.java)IDENTIFIER\n   |   | (brackets@Java~Java1_6=157#486f060 Line 2 Column 16 File C:/temp/Person.java)brackets\n   |   |)variable_declarator_id\n   |   )variable_declarator\n   |  )field_declaration\n   | )class_body_declaration\n   | (class_body_declaration@Java~Java1_6=118#486f000 Line 3 Column 5 File C:/temp/Person.java\n   |  (field_declaration@Java~Java1_6=168#486f320 Line 3 Column 5 File C:/temp/Person.java\n   |   (field_modifiers@Java~Java1_6=170#486f2a0 Line 3 Column 5 File C:/temp/Person.java)field_modifiers\n   |   (type@Java~Java1_6=192#486eee0 Line 3 Column 5 File C:/temp/Person.java\n   |   |(primitive_type@Java~Java1_6=198#486ef60 Line 3 Column 5 File C:/temp/Person.java)primitive_type\n   |   |(brackets@Java~Java1_6=157#486ee00 Line 3 Column 12 File C:/temp/Person.java)brackets\n   |   )type\n   |   (variable_declarator@Java~Java1_6=181#486f2c0 Line 3 Column 12 File C:/temp/Person.java\n   |   |(variable_declarator_id@Java~Java1_6=167#486f3a0 Line 3 Column 12 File C:/temp/Person.java\n   |   | (IDENTIFIER@Java~Java1_6=447#486f120[`age'] Line 3 Column 12 File C:/temp/Person.java)IDENTIFIER\n   |   | (brackets@Java~Java1_6=157#486ef00 Line 3 Column 15 File C:/temp/Person.java)brackets\n   |   |)variable_declarator_id\n   |   )variable_declarator\n   |  )field_declaration\n   | )class_body_declaration\n   | (class_body_declaration@Java~Java1_6=117#486f7a0 Line 4 Column 5 File C:/temp/Person.java\n   |  (method_declaration@Java~Java1_6=135#486f480 Line 4 Column 5 File C:/temp/Person.java\n   |   (method_modifiers@Java~Java1_6=141#486f460 {1} Line 4 Column 5 File C:/temp/Person.java\n   |   |(method_modifier@Java~Java1_6=147#486f400 Line 4 Column 5 File C:/temp/Person.java)method_modifier\n   |   )method_modifiers\n   |   (type_parameters@Java~Java1_6=408#486f540 Line 4 Column 12 File C:/temp/Person.java)type_parameters\n   |   (type@Java~Java1_6=191#486f740 Line 4 Column 12 File C:/temp/Person.java\n   |   |(name@Java~Java1_6=406#486f620 Line 4 Column 12 File C:/temp/Person.java\n   |   | (IDENTIFIER@Java~Java1_6=447#486f080[`String'] Line 4 Column 12 File C:/temp/Person.java)IDENTIFIER\n   |   | (type_arguments@Java~Java1_6=407#486f640 Line 4 Column 19 File C:/temp/Person.java)type_arguments\n   |   |)name\n   |   |(brackets@Java~Java1_6=157#486f700 Line 4 Column 19 File C:/temp/Person.java)brackets\n   |   )type\n   |   (IDENTIFIER@Java~Java1_6=447#486f140[`toString'] Line 4 Column 19 File C:/temp/Person.java)IDENTIFIER\n   |   (parameters@Java~Java1_6=158#486f760 Line 4 Column 27 File C:/temp/Person.java)parameters\n   |   (brackets@Java~Java1_6=157#486f820 Line 5 Column 7 File C:/temp/Person.java)brackets\n   |   (block@Java~Java1_6=217#486f780 Line 5 Column 7 File C:/temp/Person.java\n   |   |(statement_sequence@Java~Java1_6=218#486f6e0 Line 5 Column 9 File C:/temp/Person.java\n   |   | (statement_sequence_member@Java~Java1_6=223#486f6c0 Line 5 Column 9 File C:/temp/Person.java\n   |   |  (executable_statement@Java~Java1_6=243#486f6a0 Line 5 Column 9 File C:/temp/Person.java\n   |   |   (unary_expression_not_plus_minus@Java~Java1_6=389#486f720 Line 5 Column 16 File C:/temp/Person.java\n   |   |   |(literal@Java~Java1_6=390#486f280 Line 5 Column 16 File C:/temp/Person.java\n   |   |   | (STRING@Java~Java1_6=536#486f160[`name'] Line 5 Column 16 File C:/temp/Person.java)STRING\n   |   |   |)literal\n   |   |   )unary_expression_not_plus_minus\n   |   |  )executable_statement\n   |   | )statement_sequence_member\n   |   |)statement_sequence\n   |   )block\n   |  )method_declaration\n   | )class_body_declaration\n   |)class_body_declarations\n   )class_body\n  )type_declaration\n )type_declarations\n (optional_CONTROL_Z@Java~Java1_6=5#486f4e0 Line 7 Column 1 File C:/temp/Person.java)optional_CONTROL_Z\n)compilation_unit\n```\n\n========================================\n\nComments:\n- For example, see this: github.com/mono/mono/blob/master/mcs/mcs/class.cs\n- I wonder if the `varDecl` and `methodDecl` nodes could be omitted since we are already in a `varDeclList` branch. Also how the inicialization for a final field would be represented? Would that be a new node `assignVal` or the value `varDecl`?\n- Hm.. As I see it `VarDeclList` etc are needed except in degenerate cases where there happens to be only one `VarDecl`. That is, a `ClassDecl` will (among other things) contain a `VarDeclList`. That's why I included it in the example tree.\n- link to tutorial is broken, a fix would be nice please\n- Exactly which DMS product did you use to generate the Java AST? I'm trying to download it, and I see products **Test (Code) Coverage**, **Source Code Browser**, **Formatter / Obfuscator**, etc., but no generic \"parser.\"\n- @thatWiseGuy Almost all SD products related to Java will construct that AST as they go about their business by virtue of containing the DMS parser for Java. Those products won't *give* you that AST; they are focused on the task they are designed to do (e.g., collect test coverage, format/obfuscate, etc.). The one product that will give you an AST as a manipulable data stucture, and/or as XML output, is the DMS Software Reengineering Toolkit (\"DMS\") www.semanticdesigns.com/Products/DMS/DMSToolkit.html with its Java front end. DMS does not have a trial download, contact SD for more details","metadata":{"transformedAt":"2026-08-18T18:32:17.765Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":14,"totalLines":640,"estimatedTokens":8969}}149{"id":"stack-8551435","source":"stackoverflow","questionId":8551435,"title":"Scala bomb? (like a zip bomb)","tags":["scala","class","compiler-construction"],"text":"Title: Scala bomb? (like a zip bomb)\nTags: scala, class, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nPlease excuse the funny title, I am using it in analogy with \"zip bomb\". Is it possible to create a scala source file, that will, when compiled, produce a large number of class files (or a very large single class file)? Is there any way the size of the class files could grow faster than linearly with the size of the source file?\n\n========================================\n\nCode:\n```text\nclass Huge[@specialized A, @specialized B, @specialized C](\n  val a: A, val b: B, val c: C\n) {} // 730 files, 2.9 MB\n\nclass Gigantic[@specialized A, @specialized B, @specialized C, @specialized D](\n  val a: A, val b: B, val c: C, val d: D\n) {} // 6562 files, 26 MB\n```\n\n========================================\n\nComments:\n- What in hell could you possibly need that for?\n- @NikitaVolkov It sounds like an interesting mental exercise, but I have a problem trying to find a practical application.\n- Ok, for those people who need pratical applications: Trying to protect a service that compiles Scala code from DOS attacks.\n- What a fun question!\n- @KimStebel: Don't forget that Scala's type system is Turing-complete, which means it is possible to write a program which will take an infinite amount of time to typecheck.\n- Thanks, I wasn't really concerned with runtime though, since that will be limited anyway.\n- @Kim, this does't really answer the stated question, but your best bet to protect your service is to have some kind of watchdog that just kills the compiler after `x` seconds or `y` MB.\n- Ah, I've never used specialization, great answer!\n- Rex, for info, there is a bug raised for the pattern matching issues.scala-lang.org/browse/SI-1133, it fails because too much code is generated. This should be fixed with the new virtual pattern matcher in 2.10 though.","metadata":{"transformedAt":"2026-08-18T18:32:17.765Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":32,"estimatedTokens":468}}150{"id":"stack-1626446","source":"stackoverflow","questionId":1626446,"title":"What is the size of an empty struct in C?","tags":["c","compiler-construction"],"text":"Title: What is the size of an empty struct in C?\nTags: c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nAccording to me, it is zero but there seems to be bit confusion here\n\nI have tested it with gcc compiler and it gives me zero as output. I know that in C++, size of an empty class is 1. Let me know if I am missing anything here.\n\n========================================\n\nTop Answer:\nThe C grammar doesn't allow the contents of a `struct` to be empty - there has to be at least an unnamed bitfield or a named member (as far as the grammar is concerned - I'm not sure if a struct that contains only an unnamed bitfield is otherwise valid).\n\nSupport for empty structs in C are an extension in GCC.\n\nIn C++ and empty struct/class member-specification is explicitly permitted, but the size is defined to be 1 - unless as part of the empty base optimization the compiler is allowed to make an empty base class take no space in the derived class.\n\n========================================\n\nCode:\n```text\nstruct-or-union-specifier:\n  struct-or-union identifieropt { struct-declaration-list }\n  struct-or-union identifier\n\nstruct-or-union:\n  struct\n  union\n\nstruct-declaration-list:\n  struct-declaration\n  struct-declaration-list struct-declaration\n\nstruct-declaration:\n  specifier-qualifier-list struct-declarator-list ;\n\n/* type-specifier or qualifier required here! */\nspecifier-qualifier-list:\n  type-specifier specifier-qualifier-listopt\n  type-qualifier specifier-qualifier-listopt\n\nstruct-declarator-list:\n  struct-declarator\n  struct-declarator-list , struct-declarator\n\nstruct-declarator:\n  declarator\n  declaratoropt : constant-expression\n```\n\n```text\nstruct identifier { };\n```\n\n```text\nstruct identifier { int : 0; };\n```\n\n```text\nstruct identifier { type ident[]; };\n```\n\n```text\nstruct\n```\n\n```text\nstruct node\n{\n// empty struct.\n};\n\nint main()\n{\nprintf(\"%d\", sizeof(struct node));\nreturn 0;\n}\n```\n\n========================================\n\nComments:\n- C++ Standard (at least C++03) doesn't state that result of `sizeof` applied to an empty class/struct is equal to 1.\n- but following code works perfectly with gcc compiler and it prints zero ` struct abc { }; printf(\"size of empty struct %d\\n\", sizeof(struct abc));'\n- compiling with `-ansi -pedantic` gives \"main.c:2: warning: struct has no members\"\n- Empty structs are a GCC extension.\n- I don't believe the C standard says a diagnostic is required if you violate a rule contained only in the BNF. For the most part, a diagnostic is required only if a \"shall\" or \"shall not\", contained with a \"constraints\" clause, is violated. In C++, the situation is slightly different (it specifies that \"The set of *diagnosable rules* consists of all syntactic and semantic rules in this International Standard...\"\n- C99 at least says \"A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, ...\"\n- Congrats on maxing your rep out on one answer... :P\n- \"With the gcc compiler\" being the operative words; this is a gcc-specific extension (of questionable value IMO). Visual Studio rightly chokes on it.\n- Oh, thanks so much. I appreciate that you enjoy the rep festival with me.\n- \"No named members\" refers to the specific situation with unnamed bitfleld (see litb's reply above). A plain empty struct (no members at all) is a constraint violation, not UB.\n- I've just reread it, and I see no such constraint. I'm looking at &#167;6.7.2.1. The constraints I see are in paragraphs 2 through 4. Paragraph 2 says the only allowed incomplete type is a flexible array. Paragraph 3 says a bitfield can have a width of zero and no name, or a name and a width between 1 and the size of the underlying object. Paragraph 4 says the underlying type of a bitfield shall be _Bool, unsigned, or int (or something else that's implementation defined).\n- It's a syntax rule violation, which demands a diagnostic message.\n- If it's a syntax rule violation, then it certainly requires a diagnostic -- but I can't see anything that says the BNF forms part of the syntax rules. For the most part, they're treated as non-normative (e.g. Appendix A contains the whole BNF, but is specifically labeled as \"informative\").\n- @Jerry, the rules within \"Syntax\" sections (like 6.7.2.1/1 in C99) are syntax rules of course. Annex A only summarizes all rules that are given already in clause 6, thus it's informative.\n- \"Of course\" usually translates to having taken something for granted without any real support -- and this seems to be no exception. Don't get me wrong -- I'm not saying you're wrong (exactly), only that this is the sort of thing it should explicitly specify, not take for granted.","metadata":{"transformedAt":"2026-08-18T18:32:17.765Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":98,"estimatedTokens":1199}}151{"id":"stack-3763960","source":"stackoverflow","questionId":3763960,"title":"Purpose of C/C++ Prototypes","tags":["c++","c","compiler-construction"],"text":"Title: Purpose of C/C++ Prototypes\nTags: c++, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI was reading wikipedia on C/C++ Prototype statements and I'm confused:\n\nWikipedia say: \"By including the function prototype, you inform the compiler that the function \"fac\" takes one integer argument and you enable the compiler to catch these kinds of errors.\"\n\nand uses the below as an example:\n\n```\n#include \n\n /* \n * If this prototype is provided, the compiler will catch the error \n * in main(). If it is omitted, then the error will go unnoticed.\n */\n int fac(int n); /* Prototype */\n\n int main(void) { /* Calling function */\n printf(\"%d\\n\", fac()); /* ERROR: fac is missing an argument! */\n return 0;\n }\n\n int fac(int n) { /* Called function */\n if (n == 0) \n return 1;\n else \n return n * fac(n - 1);\n}\n```\n\nBut the function definition of the called function already includes *all* the information that the prototype tells the compiler, so why can't the compiler deduce this information from the called function's definition since they contain *identical* statements/information letter for letter?\n\nWhat am I missing? Seems like extra work for no obvious gain.\n\n**Edit: Thanks guys. I assumed the compilers were multi-pass I guess. I'm spoiled to current languages like Python. It makes sense since it's so old to need some kludges to do things accurately in a single pass. It seems more obvious to me now. Apparently it requires fairly intimate knowledge of how the compiler links and compiles.**\n\n========================================\n\nTop Answer:\nTwo reasons:\n\nThe compiler reads the file top-to-bottom. If `fac` is used in `main` which is above `fac`, and no prototype exists, the compiler doesn't know how to check that that call is being done correctly, since it hasn't yet reached the definition of `fac`.\n\nIt's possible to split up a C or C++ program into multiple files. `fac` may be defined in a completely different file from the file that the compiler is currently processing, and so it needs to know that that function exists somewhere, and how it is supposed to be called.\n\nNote that the comments in the example you posted only apply to C. In C++, that example will *always* produce an error, even if the prototype is omitted (although it will produce a different error depending on whether the prototype exists or not). In C++, all functions are required to be defined or prototyped before being used. \n\nIn C, you can omit the prototype and the compiler will allow you to call the function with any number of arguments (including zero), and will assume a return type of `int`. But just because it doesn't yell at you during compilation doesn't mean that the program will work right if you don't call the function the right way. That's why it's useful to prototype in C: so the compiler can double-check on your behalf.\n\nThe philosophy behind C and C++ that motivates this kind of feature is that these are relatively low-level languages. They don't do a lot of hand-holding and they don't do much if any run-time checking. If your program does something incorrect, it will crash or behave bizarrely. Therefore, the languages incorporate features like this that enable the compiler to identify certain types of errors at compile-time, so that you can more easily find and fix them.\n\n========================================\n\nCode:\n```text\n#include <stdio.h>\n\n /* \n  * If this prototype is provided, the compiler will catch the error \n  * in main(). If it is omitted, then the error will go unnoticed.\n  */\n int fac(int n);              /* Prototype */\n\n int main(void) {             /* Calling function */\n     printf(\"%d\\n\", fac());   /* ERROR: fac is missing an argument! */\n     return 0;\n }\n\n int fac(int n) {             /* Called function  */\n     if (n == 0) \n         return 1;\n     else \n         return n * fac(n - 1);\n}\n```\n\n```text\nmain()\n```\n\n```text\nfac()\n```\n\n```text\nfac()\n```\n\n```text\nmain()\n```\n\n```text\nfac\n```\n\n```text\nmain\n```\n\n```text\nfac\n```\n\n```text\nfac\n```\n\n```text\nfac\n```\n\n```text\nint\n```\n\n```text\n#include <stdio.h>\nint main() {\n   print( 5, \"hi\" );  // [1]\n}\nint print( int count, const char* txt ) {\n   int i;\n   for ( i = 0; i < count; ++i ) \n      printf( \"%s\\n\", txt );\n}\n```\n\n```text\nprint\n```\n\n```text\nint\n```\n\n```text\nprint( \"hi\", 5 );\n```\n\n```text\nprint\n```\n\n========================================\n\nComments:\n- Note that this wikipedia article contains wrong things. You can see on the discussion page how some guys just won't let it be corrected. I gave up on that.\n- This is why I define functions before they are called (in the same source file, anyway). It eliminates the need for the separate declaration, although it means my code reads \"backwards\".\n- In case #2 wouldn't includes or something of that type tell it which file to find the real function in? In python you'd just import module and then reference the function as module.function, does c not have namespaces that can be referenced like this?\n- How does it resolve #1? Nothing in the prototype tells the compiler that fac calls main and/or that main calls fac?\n- In case #2, what do you think the includes contain? They contain prototypes! But if you're writing your own `fac` function, *you* need to provide the prototype, whether it's in a seperate included file or in the same source file. In case #1, (I assume you're referring to Mystagogue's answer here), the problem is not that there's anything wrong with a recursive dependency, it's that if you do have a recursive dependency, there's no way to order the the two functions such that the compiler reads the definition of both before processing a call to either.\n- The declaration of a function often resides in a header file, and it's implementation on a source file. The source file is compiled to yield an object file. Other source files or programs that want to use the function just include the header and then link the object file. This way, the function has to be compiled only once, but can be used many times in many source-files and programs.\n- Essentially, all of your questions require an understanding of the C/C++ compiler/linker, and how C/C++ compiles and then links together multiple `.cpp` or `.c` files in a project. `#include` only includes header files, which normally only contain prototypes. The actual function may be written in another .cpp file or .c file. In order for the compiler to realize \"okay, this is a real function and not a syntax error\", it needs a prototype in the header file to tell it.\n- @pythonnewbie: Both (and many more) issues are easily resolved now, but remember that C dates back to the 70s. Sure, the language could have required a multi-pass compiler that has access to all used `.cpp` files (as opposed to the headers with the prototypes only and the already-compiled object files with the implementation) so it can deduce function signatures and everything. But that was pretty damn expensive back then, so they didn't.\n- @Delnan, your comment turned my lightbulb on the most. Too bad it's only a comment. Thanks for the insight. Didn't know it was trying to compile in a single-pass. How...archaic :P\n- @pythonnewbie: Some might say \"elegant\" ;)\n- Actually, the whole design is multi-pass, in a sense: the preprocessor pass and the linker pass are separate. Remember, back in the day you would have issues fitting everything in 64 KB. Compiling source files one at a time kept the amount of RAM needed low. The only component that saw all parts was the linker, and C is designed to work with dumb (=memory-efficient) linkers.\n- This, along with linking to an already compiled library, are really the two reason that declaration is necessary. All the other are cosmetic/preferences.","metadata":{"transformedAt":"2026-08-18T18:32:17.765Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":162,"estimatedTokens":1932}}152{"id":"stack-567682","source":"stackoverflow","questionId":567682,"title":"Online compilers/runtime for Java, C++, Python and ObjC?","tags":["java","c++","python","compiler-construction","runtime"],"text":"Title: Online compilers/runtime for Java, C++, Python and ObjC?\nTags: java, c++, python, compiler-construction, runtime\nSource: Stack Overflow\n\nQuestion:\nDoes anyone know of a good online compiler/runtime (for C++, Java, Python, ObjC etc.) that I can access on the web? \n\nWhat I'm looking for is something that would allow me to type in a program in a web form and to run the program and see the results online.\n\n(Let's not get into the why for now. Suffice it to say for the moment that I don't always have access to a compiler/runtime, and firing up an IDE is just overkill for testing out some code snippets)\n\nI know of codepad.org -- but I'm looking for something better.\n\n========================================\n\nTop Answer:\nideone is something better but what particular advence do you need?\n\n========================================\n\nComments:\n- Related thread: stackoverflow.com/questions/523568/&hellip;\n- How is \"how do I do coding on someone else's computer?\" NOT programming related?\n- It's random theft of services. If they want help with algorithm, data structure, language or library, that's fine. But \"how do I code on the web?\" is akin to \"help me buy the 'best' programming laptop.\"\n- Why is it \"random theft of services\" to use a web service someone has set up for that purpose?\n- I don't know if it's better, but rextester\n- How on earth can this be marked as answer?? The question explicitly mentions Java, which is NOT included in the languages supported by codepad.org, and the OP expliciely says he already knows codepad.org and needs something better...\n- I didn't notice the OP already knows about codepad until after I wrote the answer - so my bad on that one\n- codepad.org seems to be dead for some days, do you have any news about it?\n- This is great for Java. Been looking for something like this.\n- +1 I was surprised codepad didn't support Java. So I googled, and your answer was perfect.\n- Ah, but the section where ideone fails for me is it cannot run GUI code, and it cannot have interaction with the program during running, AFAIR","metadata":{"transformedAt":"2026-08-18T18:32:17.766Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":32,"estimatedTokens":517}}153{"id":"stack-4251964","source":"stackoverflow","questionId":4251964,"title":"Can PyPy/RPython be used to produce a small standalone executable?","tags":["python","compiler-construction","translate","pypy","rpython"],"text":"Title: Can PyPy/RPython be used to produce a small standalone executable?\nTags: python, compiler-construction, translate, pypy, rpython\nSource: Stack Overflow\n\nQuestion:\n(Or, \"Can PyPy/RPython be used to compile/translate Python to C/C++ *without requiring the Python runtime*?\")\n\nI have tried to comprehend PyPy with its RPython and its Python, its running and its compiling and its translating, and have somewhat failed.\n\nI have a hypothetical Python project (for Windows); I would like to keep its size down, in the order of a hundred kilobytes (O.N.O.) rather than the several megabytes that using py2exe entails (after UPX). Can I use PyPy1 in any way to produce a standalone executable which does not depend on Python26.dll? If I can, does it need to the RPython restrictions like `for` only working on builtin types, or is it full Python syntax?\n\nI do realise that if this can be done I almost certainly couldn't use C modules from Python directly.\n\n1 *(Since the time of asking, the situation has become clearer, and this part of the toolchain is more clearly branded as RPython rather than PyPy; it wasn't so in 2010.)*\n\n========================================\n\nCode:\n```text\nfor\n```\n\n========================================\n\nComments:\n- This will almost certainly not produce an executable as small as you want, but since it's related, I thought I'd point out that cython claims it can now build stand-alone executables: wiki.cython.org/EmbeddingCython\n- @Forest: I didn't realise Cython could do that, but due to requiring the Python runtime I think it's unlikely to get any (or at least much) smaller than py2exe + UPX.\n- Thanks for that. That's what I wanted to know and expected the answer would be. A secondary question related to that which I forgot to ask, is it possible to access the Windows API through PyPy in that? Would `ctypes.windll` work or would it be more complex?\n- ctypes doesn't work in RPython. There is a C interface, it's called rffi, grep for usages around (modules use it). There is quite a bit of windows API exposed here and there, look into source (sorry, but this is all fairly internal stuff, no docs ;-)","metadata":{"transformedAt":"2026-08-18T18:32:17.766Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":29,"estimatedTokens":537}}154{"id":"stack-2632641","source":"stackoverflow","questionId":2632641,"title":"hand coding a parser","tags":["c#","compiler-construction","parsing","lexer"],"text":"Title: hand coding a parser\nTags: c#, compiler-construction, parsing, lexer\nSource: Stack Overflow\n\nQuestion:\nFor all you compiler gurus, I wanna write a recursive descent parser and I wanna do it with just code. No generating lexers and parsers from some other grammar and don't tell me to read the dragon book, i'll come around to that eventually.\n\nI wanna get into the gritty details about implementing a lexer and parser for a reasonable simple language, say CSS. And I wanna do this right. \n\nThis will probably end up being a series of questions but right now I'm starting with a lexer. Tokenization rules for CSS can be found here.\n\nI find my self writing code like this (hopefully you can infer the rest from this snippet):\n\n```\npublic CssToken ReadNext()\n{\n int val;\n while ((val = _reader.Read()) != -1)\n {\n var c = (char)val;\n switch (_stack.Top)\n {\n case ParserState.Init:\n if (c == ' ')\n {\n continue; // ignore\n }\n else if (c == '.')\n {\n _stack.Transition(ParserState.SubIdent, ParserState.Init);\n }\n break;\n\n case ParserState.SubIdent:\n if (c == '-')\n {\n _token.Append(c);\n }\n _stack.Transition(ParserState.SubNMBegin);\n break;\n```\n\nWhat is this called? and how far off am I from something reasonable well understood? I'm trying to balance something which is fair in terms of efficiency and easy to work with, using a stack to implement some kind of state machine is working quite well, but I'm unsure how to continue like this.\n\nWhat I have is an input stream, from which I can read 1 character at a time. I don't do any look a head right now, I just read the character then depending on the current state try to do something with that.\n\nI'd really like to get into the mind set of writing reusable snippets of code. This `Transition` method is currently means to do that, it will pop the current state of the stack and then push the arguments in reverse order. That way, when I write `Transition(ParserState.SubIdent, ParserState.Init)` it will \"call\" a sub routine `SubIdent` which will, when complete, return to the `Init` state.\n\nThe parser will be implemented in much the same way, currently, having everything in a single big method like this allows me to easily return a token when I found one, but it also forces me to keep everything in one single big method. Is there a nice way to split these tokenization rules into separate methods?\n\n========================================\n\nTop Answer:\nYou need to write your own Recursive Descent Parser from your BNF/EBNF. I had to write my own recently and this page was a lot of help. I'm not sure what you mean by \"with just code\". Do you mean you want to know how to write your own recursive parser?\n\nIf you want to do that, you need to have your grammar in place first. Once you have your EBNF/BNF in place, the parser can be written quite easily from it.\n\nThe first thing I did when I wrote my parser, was to read everything in and then tokenize the text. So I essentially ended up with an array of tokens that I treated as a stack. To reduce the verbosity/overhead of pulling a value off a stack and then pushing it back on if you don't require it, you can have a `peek` method that simply returns the top value on the stack without popping it.\n\n**UPDATE**\n\nBased on your comment, I had to write a recursive-descent parser in Javascript from scratch. You can take a look at the parser here. Just search for the `constraints` function. I wrote my own `tokenize` function to tokenize the input as well. I also wrote another convenience function (`peek`, that I mentioned before). The parser parses according to the EBNF here.\n\nThis took me a little while to figure out because it's been years since I wrote a parser (last time I wrote it was in school!), but trust me, once you get it, you *get* it. I hope my example gets your further along on your way. \n\n**ANOTHER UPDATE**\n\nI also realized that my example may not be what you want because you might be going towards using a shift-reduce parser. You mentioned that right now you are trying to write a tokenizer. In my case, I did write my own tokenizer in Javascript. It's probably not robust, but it was sufficient for my needs.\n\n```\nfunction tokenize(options) {\n var str = options.str;\n var delimiters = options.delimiters.split(\"\");\n var returnDelimiters = options.returnDelimiters || false;\n var returnEmptyTokens = options.returnEmptyTokens || false;\n var tokens = new Array();\n var lastTokenIndex = 0;\n\n for(var i = 0; i Based on your code, it looks like you are reading, tokenizing, and parsing at the same time - I'm assuming that's what a shift-reduce parser does? The flow for what I have is tokenize first to build the stack of tokens, and then send the tokens through the recursive-descent parser.\n\n========================================\n\nCode:\n```text\npublic CssToken ReadNext()\n{\n    int val;\n    while ((val = _reader.Read()) != -1)\n    {\n        var c = (char)val;\n        switch (_stack.Top)\n        {\n            case ParserState.Init:\n                if (c == ' ')\n                {\n                    continue; // ignore\n                }\n                else if (c == '.')\n                {\n                    _stack.Transition(ParserState.SubIdent, ParserState.Init);\n                }\n                break;\n\n            case ParserState.SubIdent:\n                if (c == '-')\n                {\n                    _token.Append(c);\n                }\n                _stack.Transition(ParserState.SubNMBegin);\n                break;\n```\n\n```text\nTransition\n```\n\n```text\nTransition(ParserState.SubIdent, ParserState.Init)\n```\n\n```text\nSubIdent\n```\n\n```text\nInit\n```\n\n```text\nfunction readToken() // note: returns only one token each time\n    while !eof\n        c = peekChar()\n        if c in A-Za-z\n            return readIdentifier()\n        else if c in 0-9\n            return readInteger()\n        else if c in ' \\n\\r\\t\\v\\f'\n            nextChar()\n        ...\n    return EOF\n\nfunction readIdentifier()\n    ident = \"\"\n    while !eof\n        c = nextChar()\n        if c in A-Za-z0-9\n            ident.append(c)\n        else\n            return Token(Identifier, ident)\n            // or maybe...\n            return Identifier(ident)\n```\n\n```text\nlex\n```\n\n```text\nANTLR\n```\n\n```text\nragel\n```\n\n```text\nfunction tokenize(options) {\n            var str = options.str;\n            var delimiters = options.delimiters.split(\"\");\n            var returnDelimiters = options.returnDelimiters || false;\n            var returnEmptyTokens = options.returnEmptyTokens || false;\n            var tokens = new Array();\n            var lastTokenIndex = 0;\n\n            for(var i = 0; i < str.length; i++) {\n                if(exists(delimiters, str[i])) {\n                    var token = str.substring(lastTokenIndex, i);\n\n                    if(token.length == 0) {\n                        if(returnEmptyTokens) {\n                            tokens.push(token);\n                        }\n                    }\n\n                    else {\n                        tokens.push(token);\n                    }\n\n                    if(returnDelimiters) {\n                        tokens.push(str[i]);\n                    }\n\n                    lastTokenIndex = i + 1;\n                }\n            }\n\n            if(lastTokenIndex < str.length) {\n                var token = str.substring(lastTokenIndex, str.length);\n                token = token.replace(/^\\s+/, \"\").replace(/\\s+$/, \"\");\n\n                if(token.length == 0) {\n                    if(returnEmptyTokens) {\n                        tokens.push(token);\n                    }\n                }\n\n                else {\n                    tokens.push(token);\n                }\n            }\n\n            return tokens;\n        }\n```\n\n```text\npeek\n```\n\n```text\nconstraints\n```\n\n```text\ntokenize\n```\n\n```text\npeek\n```\n\n```text\nTerm, Operator, Term\n```\n\n```text\nExpression\n```\n\n========================================\n\nComments:\n- I don't understand why need _stack for recursive descent\n- this is a state machine implementation, i'm going to use this to feed the parser with tokens. currently I'm asking for help with the lexer/tokenization part.\n- Don't know if this will help you, but I have found msdn.microsoft.com/en-us/magazine/cc136756.aspx rather helpful in writing a simple compiler. It's about writing a compiler for the .NET framework, but the basic compiler principles are applicable beyond a .NET Framework compiler.\n- @Zach - thanks for the link, there's some nice code examples in there.\n- Just code means that you pick your language, mine is C#, and then you write your lexer and parser entierly within the language. No tooling is allowed. I would eventually love to end up with something like the Boost Spirit parser framework which is written entierly in C++ and will provide a C++ programming model for parser generation. That's acceptable, but a distant goal for me right now.\n- It's funny you say that! I had to write a recursive-descent parser from scratch in Javascript :p.\n- Thank's for the link, looks very helpful, but right now I'm currently just trying to write a lexer/tokenizer that I will later use for writing the parser.\n- +1 the link to wikipedia shows the basics in short, in the dragon book (from 80's) they use a lookahead and match() instead accept() and expect()\n- It appears he has started down the path of writing a shift-reduce parser, so recursive-descent may be less intuitive to him.\n- @Heath, what's shift-reduce ;) I still can't tell the difference between these two. I hate it when I get these damn shift-reduce or reduce-reduce conflicts... try to be very specific though, I know about the typicall if else if case.\n- @Heath I just noticed that later. I vaguely recall the shift-reduce parser from school. I'll need to read up on it.\n- @John same here - I vaguely remember the term :) I think it's something I need to look into. Recursive-descent is what I remember from school.\n- Thanks for the JavaScript source, when I look at the parser, it doesn't look very formal. I'd really wanna get away from splitting and/or substringing on specific characters and patterns. Often that might do but I really want the formal lexer approach here, where my lexer is a well defined state machine.\n- Ummm, look up shift-reduce first? Another name for it is \"bottom-up parsing\" -- en.wikipedia.org/wiki/Bottom-up_parsing\n- @Heath ahh bottom-up - that makes more sense. That's what I remember from school @John, what I posted is simply the tokenizer. The parser doesn't perform any `substring` operations. The only place I use patterns is at the lowest level of the parse tree (for example, ensuring valid identifier names as such). Sorry I just realized I pointed you to a not-so-precise location. The parser actually starts at `constraints`\n- @Heath Right, I rember now... But I'm still stuck trying to refactor that tokenizer into something less like one big method.\n- @John you can have your tokenize only tokenize and return an array (or stack) of tokens. Then delegate the parsing to the recursive-descent parser, which will consume the stack as it parses.\n- @Vivin I want the formal lexical analsysis, why is that part always left out? Maybe it's not really that exciting. Anyway, still stuck with state machine implemention in one big loop with lots of if statements and the occasional switch statement.\n- @Vivin I believe that's the plan. I'm probably gonna start a new question when I get stuck with the parser part.\n- @John A lexer simply converts a sequence of characters into a sequence of tokens (see en.wikipedia.org/wiki/Lexical_analysis). That's also what a tokenizer does. So your first task is to identify what constitutes a token. You can figure that out from your grammar.\n- @Vivin Right! Check out Dietrich's answer, it explains a lot.\n- When it comes to writing the parser I belive I'd prefer the recursive descent variant. I believe I'm currently just using the stack and while loop thing to build a state machine, which will be my lexer... what's my alternative? is still need a lexer don't I?\n- The state machine you are building is very similar to shift-reduce. This implies to me that you would have more fun doing it that way. Also, the end product is better.\n- Hmm... shift-reduce until proven otherwise.\n- I just want to point out that there are very good reasons to use a recursive descent parser. Of course, it's entirely possible that they didn't need to go that far in rewriting their error handling to achieve that effect, but still, it's a good reason to do it, at least with more complex cases.\n- You know, If I had this kind of help when I did my thesis things would have been some much more understandable. Thanks for a great answer! No combining of the lexer and parser will ever take place, that much I've learned the hard way.\n- I have but 1 question left for you, where does `c` go? When I say return `IDENTIFIER` where does the token value reside? This is a trivial question but I have to ask! If I just put the character in a string builder when do I flush that and do I ever run the risk of leaving junk in my string builder?\n- I corrected the pseudocode to show one way to handle keeping track of `c`. Originally I had been vague because there are a few different ways people tend to keep track of token values.\n- Yeah, try to build the lexical analyzer as one object that you can call, like Lexer.Next(), then build the syntactic analyzer, which will use the lexer. I DO recommend you that your lexer returns both the value of the identifier, as well as the type, ie: identifier:a,integer:3,string:\"hello\". this will let you do the syntactic analysis a lot easier.\n- I'm not gonna do code gen ;) And you are dead wrong, I love theory. It's just really hard to get started sometimes...\n- @John, I gathered you are not going to code gen, that is why I believe recursive decent parser is a good option for you. I agree on the theory, if you have some practical experience it helps later to better grasp the theory.","metadata":{"transformedAt":"2026-08-18T18:32:17.766Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":273,"estimatedTokens":3496}}155{"id":"stack-6219914","source":"stackoverflow","questionId":6219914,"title":"Can you add custom compiler warnings in Objective-C?","tags":["objective-c","xcode","compiler-construction","compiler-warnings"],"text":"Title: Can you add custom compiler warnings in Objective-C?\nTags: objective-c, xcode, compiler-construction, compiler-warnings\nSource: Stack Overflow\n\nQuestion:\n*(I found the answer to this elsewhere while writing the question, but I thought it might be helpful to others if I posted it since I couldn't find anything here.)*\n\nI want to mark methods that need better error handling. I'd like them to show up as compiler warnings so other developers (who may be responsible for that area) will notice, and hopefully fix at their leisure.\n\n*(Other approaches welcome, I looked at `__attribute__((warning))` but couldn't get it to work.)*\n\n========================================\n\nTop Answer:\nSelect your target and then select the Build Phases tab. At the bottom of the window you’ll see an option to Add Build Phase at the bottom of the screen. You can use the Add Build Phase to add a Run Script build phase. The Run Script option allows you to select a shell and execute arbitrary code against the project.\n\nTo warn about ***TODO & FIXME*** comments, use /bin/sh as the shell and paste in this script:\n\n```\nTAGS=\"TODO:|FIXME:\"\necho \"searching ${SRCROOT} for ${TAGS}\"\nfind \"${SRCROOT}\" \\( -name \"*.h\" -or -name \"*.m\" \\) -print0 | xargs -0 egrep --with-filename --line-number --only-matching \"($TAGS).*\\$\" | perl -p -e \"s/($TAGS)/ warning: \\$1/\"\n```\n\nSource : Generate Xcode Warnings from TODO Comments\n\n========================================\n\nCode:\n```text\n__attribute__((warning))\n```\n\n```text\n#warning Needs better error handling, please.\n```\n\n```text\nTAGS=\"TODO:|FIXME:\"\necho \"searching ${SRCROOT} for ${TAGS}\"\nfind \"${SRCROOT}\" \\( -name \"*.h\" -or -name \"*.m\" \\) -print0 | xargs -0 egrep --with-filename --line-number --only-matching \"($TAGS).*\\$\" | perl -p -e \"s/($TAGS)/ warning: \\$1/\"\n```\n\n========================================\n\nComments:\n- This isn't really a good idea, as it makes warnings a regular part of the build rather than indications of possible errors. You'd be better off using //FIXME or //TODO, which many IDEs automatically pick up, to mark sections of code that need work.\n- @jshier I consider it developer error (or at least I would only mark ones that I consider developer error.) The idea is to help others (and myself) get better at error handling so as do the right thing going forward. The warnings will disappear when they're addressed, and I think they need to be addressed. I don't think //FIXME and //TODO are strong enough.\n- (Also, when you're working on a small enough team it makes more sense and isn't obnoxious because it's only seen by your target audience.)\n- Just be careful when you use //FIXME or //TODO. You need to add a colon at the end. The right syntax is: //FIXME: fix me comment and // TODO: to do comment\n- @theReverend They show up in the method list in the jump bar that's right above the code window.","metadata":{"transformedAt":"2026-08-18T18:32:17.766Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":51,"estimatedTokens":716}}156{"id":"stack-1561515","source":"stackoverflow","questionId":1561515,"title":"Where can I find C# 3.0 grammar?","tags":["c#","compiler-construction","c#-3.0","antlr","grammar"],"text":"Title: Where can I find C# 3.0 grammar?\nTags: c#, compiler-construction, c#-3.0, antlr, grammar\nSource: Stack Overflow\n\nQuestion:\nI'm planning to write a C# 3.0 compiler in C#. Where can I get the grammar for parser generation?\n\nPreferably one that works with ANTLR v3 without modification.\n\n========================================\n\nTop Answer:\nTake a look at C# Language Specification. In the chapter **B. Grammar** you'll find the grammar.\n\n========================================\n\nComments:\n- You are aware that we already ship for free a compiler that compiles C# 3, right? :-) But seriously, why are you building your own? Just for fun, or is there some business purpose? (The reason I ask is because we are very interested in learning what \"services\" people want out of our compiler other than simply \"spit me out some IL for this source code\".)\n- Eric: Primarily for fun. However, I come up with some language ideas from time to time that I wish I could test.\n- @Mehrdad, if you do get some code going, can i play around with it :)\n- @Stan R: There are plenty of open source C# compilers out there right now. Mono's C# compiler is written in C#, for instance.\n- Given you dont need modification, you dont really want to write much, do you?\n- @leppie: I'm OK with modification, but I don't want to spend time writing basic stuff for the C# language too much. I prefer to get one working quickly and experiment with it afterwards. I already know how to do parsers. My primary goal is not learning how to parse stuff. I considered messing with Mono C# compiler. I prefer writing my own.\n- @Mehrdad: \"...language ideas\" is pretty vague. If you want to do anything interesting with C# (or really any other language) you not only need a parser (therefore grammar) but you also need to build trees, build symbol tables, analyze symbol usage, ... There's a lot more to this than just the grammar. The Mono framework might be a lot more helpful than you think.\n- Check out answers to stackoverflow.com/questions/358052/c-anltr-grammar\n- Ira: Of course I have seen that question. I explicitly mentioned C# 3.0, since I've found a bunch of stuff for 1.0. -- I said \"primarily for fun\" by the way. For me, it's more fun to write my own and use that to test my stuff rather than try to understand the structure of code done by Mono guys.\n- @Mehrdad: I've built tools to manipulate languages. They take a lot more work than you might expect. You're making what I see as a classic mistake of \"if I just had a parser...\". Best of luck.\n- Ira: Of course. It's not supposed to provide any business value. It's a personal project and it's going to be fun. I'll be doing it in my free time.\n- The linked grammar is not C# 3.0. It doesn't support lambdas. That's specifically important to me.\n- It would seem that adding support for lambdas in terms of existing constructs in the grammar is fairly trivial, since you only need to define argument list. This will probably need LL(*), however, since you can parse something like `(a**` and not know if this will end up being an expression like `(a**b)` (i.e. multiply `a` by the result of a dereference of `b`), or a lambda expression `(a** b) =>`, until you hit the `=>`. Since there's no limit on amount of indirection (pointer to pointer to ...), it looks like it it's LL(*) to me. But since ANTLR3 supports opt-in LL(*), it's not a problem.\n- @Pavel: It's not just that. It doesn't support generics. I'll probably write my own parser or grammar from scratch if I can't find a reasonably good C# 3.0 grammar.\n- Yeah, of course the spec contains grammar. However, the grammar in that *Word document* is scattered through the whole doc and is unsuitable for parser generation.\n- It's not *only* scattered throughout; we have an appendix at the end with the whole thing in one place. You are probably right that it would take some modification to make it work for a parser generator.\n- Eric: Oh, didn't notice that section. Thanks for pointing out.\n- Micheal: It's less than 40 pages long. When I think about it, it's possible to deal with it and start from scratch. +1","metadata":{"transformedAt":"2026-08-18T18:32:17.766Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":35,"estimatedTokens":1027}}157{"id":"stack-8546305","source":"stackoverflow","questionId":8546305,"title":"What does SCC stand for, with respect to LLVM?","tags":["compiler-construction","llvm"],"text":"Title: What does SCC stand for, with respect to LLVM?\nTags: compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nI was reading through this document http://llvm.org/docs/WritingAnLLVMPass.html, where in I came across CallGraphSCCPass. I searched for SCC abbreviation, but wasn't able to find one. What does SCC stand for? Where can I read more about it?\n\n========================================\n\nTop Answer:\nI believe it stands for Strongly Connected Components, since that documentation talks about Tarjan's algorithm.\n\n========================================\n\nCode:\n```text\nsite:llvm.org\n```\n\n========================================\n\nComments:\n- To my understanding, they actually contract SCCs into single vertices, and the graphs become a DAG, which could be bottom-up passed. Is it correct?\n- @zsf222: Yes, that's my understanding.\n- Accidentally, SCC also stands for 'source code comments'...","metadata":{"transformedAt":"2026-08-18T18:32:17.766Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":25,"estimatedTokens":228}}158{"id":"stack-4527902","source":"stackoverflow","questionId":4527902,"title":"What is the order of the scala compiler phases?","tags":["scala","compiler-construction","scalac"],"text":"Title: What is the order of the scala compiler phases?\nTags: scala, compiler-construction, scalac\nSource: Stack Overflow\n\nQuestion:\nI want to sharpen my picture of the phases of scala compilation. I know that certain things have to happen in the compiler, but don't really know the order in which they happen and how the order should affect my programming.\n\nAm I right in saying that the following things are the full list of what the compiler does?\n\n- parse program\n\n- types checked\n\n- do erasure\n\n- implicit conversion\n\n- byte-code generated\n\n- optimize\n\nIf so, what is the order that it does these phases?\nHow does this order affect the programmer, especially the type-level programmer?\n\n========================================\n\nTop Answer:\nHave you looked here? That page points to https://www.scala-lang.org/old/sites/default/files/sids/nielsen/Thu,%202009-05-28,%2008:13/compiler-phases-sid.pdf (PDF), which describes the compiler phases for Scala 2.8.\n\n========================================\n\nCode:\n```text\n» scalac -Xshow-phases\n\n             phase name  id  description\n             ----------  --  -----------\n                 parser   1  parse source into ASTs, perform simple desugaring\n                  namer   2  resolve names, attach symbols to named trees\n         packageobjects   3  load package objects\n                  typer   4  the meat and potatoes: type the trees\n                 patmat   5  translate match expressions\n         superaccessors   6  add super accessors in traits and nested classes\n             extmethods   7  add extension methods for inline classes\n                pickler   8  serialize symbol tables\n              refchecks   9  reference/override checking, translate nested objects\n           selectiveanf  10  \n           selectivecps  11  \n                uncurry  12  uncurry, translate function values to anonymous classes\n              tailcalls  13  replace tail calls by jumps\n             specialize  14  @specialized-driven class and method specialization\n          explicitouter  15  this refs to outer pointers, translate patterns\n                erasure  16  erase types, add interfaces for traits\n            posterasure  17  clean up erased inline classes\n               lazyvals  18  allocate bitmaps, translate lazy vals into lazified defs\n             lambdalift  19  move nested functions to top level\n           constructors  20  move field definitions into constructors\n                flatten  21  eliminate inner classes\n                  mixin  22  mixin composition\n                cleanup  23  platform-specific cleanups, generate reflective calls\n                  icode  24  generate portable intermediate code\n                inliner  25  optimization: do inlining\ninlineExceptionHandlers  26  optimization: inline exception handlers\n               closelim  27  optimization: eliminate uncalled closures\n                    dce  28  optimization: eliminate dead code\n                    jvm  29  generate JVM bytecode\n               terminal  30  The last phase in the compiler chain\n```\n\n```text\nscalac -Xshow-phases\n```\n\n```text\n-Xshow-phases -Ydebug\n```\n\n```text\nphase name  id  description\n     ----------  --  -----------\n         parser   1  parse source into ASTs, perform simple desugaring\n     jspretyper   2  capture pre-typer only tree info (for Scala.js)\n          namer   3  resolve names, attach symbols to named trees\n packageobjects   4  load package objects\n          typer   5  the meat and potatoes: type the trees\n      jsinterop   6  prepare ASTs for JavaScript interop\n         patmat   7  translate match expressions\n superaccessors   8  add super accessors in traits and nested classes\n     extmethods   9  add extension methods for inline classes\n        pickler  10  serialize symbol tables\n       xsbt-api  11  \nxsbt-dependency  12  \n      refchecks  13  reference/override checking, translate nested objects\n        uncurry  14  uncurry, translate function values to anonymous classes\n      tailcalls  15  replace tail calls by jumps\n     specialize  16  @specialized-driven class and method specialization\n  explicitouter  17  this refs to outer pointers\n        erasure  18  erase types, add interfaces for traits\n    posterasure  19  clean up erased inline classes\n       lazyvals  20  allocate bitmaps, translate lazy vals into lazified defs\n     lambdalift  21  move nested functions to top level\n   constructors  22  move field definitions into constructors\n        flatten  23  eliminate inner classes\n          mixin  24  mixin composition\n         jscode  25  generate JavaScript code from ASTs\n        cleanup  26  platform-specific cleanups, generate reflective calls\n     delambdafy  27  remove lambdas\n          icode  28  generate portable intermediate code\n        inliner  xx  optimization: do inlining\n inlinehandlers  xx  optimization: inline exception handlers\n       closelim  xx  optimization: eliminate uncalled closures\n       constopt  xx  optimization: optimize null and other constants\n            dce  xx  optimization: eliminate dead code\n            jvm  29  generate JVM bytecode\n  xsbt-analyzer  30  \n       terminal  31  the last phase during a compilation run\n```\n\n```text\nphase name  id  description\n     ----------  --  -----------\n         parser   1  parse source into ASTs, perform simple desugaring\n          namer   2  resolve names, attach symbols to named trees\n packageobjects   3  load package objects\n          typer   4  the meat and potatoes: type the trees\n  nativeinterop   5  prepare ASTs for Native interop\n         patmat   6  translate match expressions\n superaccessors   7  add super accessors in traits and nested classes\n     extmethods   8  add extension methods for inline classes\n        pickler   9  serialize symbol tables\n       xsbt-api  10  \nxsbt-dependency  11  \n      refchecks  12  reference/override checking, translate nested objects\n        uncurry  13  uncurry, translate function values to anonymous classes\n      tailcalls  14  replace tail calls by jumps\n     specialize  15  @specialized-driven class and method specialization\n  explicitouter  16  this refs to outer pointers\n        erasure  17  erase types, add interfaces for traits\n    posterasure  18  clean up erased inline classes\n       lazyvals  19  allocate bitmaps, translate lazy vals into lazified defs\n     lambdalift  20  move nested functions to top level\n   constructors  21  move field definitions into constructors\n        flatten  22  eliminate inner classes\n          mixin  23  mixin composition\n            nir  24  \n        cleanup  25  platform-specific cleanups, generate reflective calls\n     delambdafy  26  remove lambdas\n          icode  27  generate portable intermediate code\n        inliner  xx  optimization: do inlining\n inlinehandlers  xx  optimization: inline exception handlers\n       closelim  xx  optimization: eliminate uncalled closures\n       constopt  xx  optimization: optimize null and other constants\n            dce  xx  optimization: eliminate dead code\n            jvm  28  generate JVM bytecode\n  xsbt-analyzer  29  \n       terminal  30  the last phase during a compilation run\n```\n\n========================================\n\nComments:\n- If you want to learn more about Scala compiler phases, look here: iteratorshq.com/blog/scala-compiler-phases-with-pictures","metadata":{"transformedAt":"2026-08-18T18:32:17.766Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":162,"estimatedTokens":1836}}159{"id":"stack-24639279","source":"stackoverflow","questionId":24639279,"title":"Why does GHC typecheck before desugaring?","tags":["haskell","compiler-construction","ghc"],"text":"Title: Why does GHC typecheck before desugaring?\nTags: haskell, compiler-construction, ghc\nSource: Stack Overflow\n\nQuestion:\nIs there a good reason to run the typechecker first? It would seem that the typechecker would be vastly simpler if it ran on a smaller syntax, especially because with the current system every syntax extension needs to touch the typechecker. This question applies especially to arrow syntax, the typechecking of which as described in comments here is known to be bogus.\n\nI imagine one reason for this would be not emitting errors that mention generated code, but this situation is already covered in cases where a `deriving` clause fails to typecheck; GHC knows that code was generated.\n\n========================================\n\nCode:\n```text\nderiving\n```\n\n========================================\n\nComments:\n- I think this is a very broad question, but seeing how all extensions to date are purely based on syntax and name availability (including ones like `ApplicativeDo`), there is no need for type-guided desugaring. The only real reason one would want to do this is to avoid the problem that desugared programs don't correspond to what the programmer provided, and when the compiler needs to provide diagnostic information (e.g. specific type errors), these programs need to be 'resugared' in order to refer meaningfully to the programmer's programs.\n- I think this was mentioned somewhere in A History of Haskell as a stylistic choice in GHC's implementation which has generally lead to a bit of overhead but improved likelihood to write nice error messages.\n- Readers: I would highly recommend reading the entire GHC article, it's very interesting.","metadata":{"transformedAt":"2026-08-18T18:32:17.766Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":22,"estimatedTokens":420}}160{"id":"stack-8706998","source":"stackoverflow","questionId":8706998,"title":"How to efficiently implement closures in LLVM IR?","tags":["compiler-construction","llvm","code-generation","language-design","llvm-ir"],"text":"Title: How to efficiently implement closures in LLVM IR?\nTags: compiler-construction, llvm, code-generation, language-design, llvm-ir\nSource: Stack Overflow\n\nQuestion:\nI started adding closures (lambdas) to my language that uses LLVM as the backend. I have implemented them for simple cases where they can be always inlined i.e. code for the closure definition itself doesn't need to be generated, as it is inlined where used.\n\nBut how to generate the code for a closure in case the closure isn't always inlined (for example, it is passed to another function that isn't inlined). Preferably, the call sites shouldn't care whether they are passed regular functions or closures and would call them as normal functions.\n\nI could generate a function with a synthetic name, but it would have to take the referencing environment as an extra argument and that function couldn't just be passed to another function that doesn't know about the needed extra argument.\n\nI have thought of one possible solution using LLVM's trampoline intrinsics, which \"excise\" a single parameter from a function, returning a pointer to a trampoline function that takes one less parameter. In this case, if the function generated for the closure took the referencing environment as the first parameter, I could excise it and get back a function that takes exactly as many parameters as the closure actually declares. Does this sound doable? Efficient? Are there any better solutions?\n\nCode example:\n\n```\ndef applyFunctionTo(value: Int, f: (Int) -> Int) = f(value)\n\ndef main() = {\n val m := 4;\n val n := 5;\n val lambda := { (x: Int) => x + m + n };\n applyFunctionTo(3, lambda)\n}\n```\n\nNow, lets imagine that this wouldn't get inlined to `def main() = 3 + 4 + 5`, and that `applyFunctionTo` would possibly be compiled separately, and we can't change the call site there. With trampolining, I imagine the generated code would be something like this (expressed in pseudocode, * means pointer):\n\n```\ndef main$lambda$1(env: {m: Int, n: Int}*, x: Int) = x + env.m + env.n\ndef main() = {\n m = 4\n n = 5\n env* = allocate-space-for {Int, Int}\n env = {m, n}\n tramp* = create-trampoline-for(main$lambda$1*, env*)\n return applyFunctionTo(3, tramp*)\n // release memory for env and trampoline if the lambda didn't escape\n}\n```\n\nDoes this seem right?\n\n========================================\n\nTop Answer:\nA dumb idea would be that for each closure you generate a thread local structure to hold the required data (could be just a pointer to a local structure, or several pointers).\n\nThe creator of the closure is the responsible for setting the TLS variables and \"saving\" the state they had (to allow recursive call).\n\nThe user then calls the function normally, it's executed and use the environemnt.\n\nAfter the call, the creator of the closure \"restores\" the original values into the TLS variables.\n\n========================================\n\nCode:\n```text\ndef applyFunctionTo(value: Int, f: (Int) -> Int) = f(value)\n\ndef main() = {\n  val m := 4;\n  val n := 5;\n  val lambda := { (x: Int) => x + m + n };\n  applyFunctionTo(3, lambda)\n}\n```\n\n```text\ndef main$lambda$1(env: {m: Int, n: Int}*, x: Int) = x + env.m + env.n\ndef main() = {\n  m = 4\n  n = 5\n  env* = allocate-space-for {Int, Int}\n  env = {m, n}\n  tramp* = create-trampoline-for(main$lambda$1*, env*)\n  return applyFunctionTo(3, tramp*)\n  // release memory for env and trampoline if the lambda didn't escape\n}\n```\n\n```text\ndef main() = 3 + 4 + 5\n```\n\n```text\napplyFunctionTo\n```\n\n========================================\n\nComments:\n- There is no difference between implementing closures and implementing objects with virtual methods.\n- It's possible that you are right, however, the language will not have virtual methods (yet). At least it will have closures and a lot of other stuff before that. I might add some features in a dumb order because I'm just doing it for learning purposes, mostly. I only hope that something useful comes from it eventually.\n- I meant there is no reason to invent anything new for closures: you can just do the same thing as, say, a C++ compiler is already doing. Chances are that it is already the most efficient thing to do.\n- Good point, but a C++ compiler is probably complicated enough that it might not be the best one to learn from. For example I don't have inheritance or even any kind of polymorphism yet, so my functions and methods are all just functions in LLVM and all function/method calls are pretty simple.\n- ok, then emitting a structure with the captured environment and a single function pointer (as the first element) should be sufficient for your lambda lifting implementation. This way you can bitcast your closure to a function pointer and pass it as the first argument to a call.\n- Sorry, I don't get your meaning exactly. I added a code example above. There would be no lambda lifting, because I can't modify the call site (but maybe I misunderstand what you meant or miscommunicated in the original question). If the function pointer is in the environment, how does that help to get the environment from the function? Also maybe I wasn't clear before that I'd strongly prefer that call sites didn't change depending on whether a lambda or regular function is passed to them. And regular functions should be preferred.\n- Ah, nevermind. I think I got what you meant now. It's similar to what the other answer suggested as an alternative. I think I'll give the trampolines a try, though.\n- I'll try the trampolines for now, but it seems that the alternative of passing functions as a pair of pointers might actually be better in case the majority of functions passed around in programs are either closures that actually have free variables or methods of objects (where 'this' can be considered to be a free variable). I'm not sure how exactly the language will turn out, but I might consider switching to that representation later.","metadata":{"transformedAt":"2026-08-18T18:32:17.766Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":100,"estimatedTokens":1474}}161{"id":"stack-898432","source":"stackoverflow","questionId":898432,"title":"How is static variable initialization implemented by the compiler?","tags":["c++","compiler-construction","static","initialization"],"text":"Title: How is static variable initialization implemented by the compiler?\nTags: c++, compiler-construction, static, initialization\nSource: Stack Overflow\n\nQuestion:\nI'm curious about the underlying implementation of static variables within a function.\n\nIf I declare a static variable of a fundamental type (char, int, double, etc.), and give it an initial value, I imagine that the compiler simply sets the value of that variable at the very beginning of the program before `main()` is called:\n\n```\nvoid SomeFunction();\n\nint main(int argCount, char ** argList)\n{\n // at this point, the memory reserved for 'answer'\n // already contains the value of 42\n SomeFunction();\n}\n\nvoid SomeFunction()\n{\n static int answer = 42;\n}\n```\n\nHowever, if the static variable is an instance of a class:\n\n```\nclass MyClass\n{\n //...\n};\n\nvoid SomeFunction();\n\nint main(int argCount, char ** argList)\n{\n SomeFunction();\n}\n\nvoid SomeFunction()\n{\n static MyClass myVar;\n}\n```\n\nI know that it will not be initialized until the first time that the function is called. Since the compiler has no way of knowing when the function will be called for the first time, how does it produce this behavior? Does it essentially introduce an if-block into the function body?\n\n```\nstatic bool initialized = 0;\nif (!initialized)\n{\n // construct myVar\n initialized = 1;\n}\n```\n\n========================================\n\nTop Answer:\nThis question covered similar ground, but thread safety wasn't mentioned. For what it's worth, C++0x will make function static initialisation thread safe.\n\n(see the C++0x FCD, 6.7/4 on function statics: \"If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for\ncompletion of the initialization.\")\n\nOne other thing that hasn't been mentioned is that function statics are destructed in reverse order of their construction, so the compiler maintains a list of destructors to call on shutdown (this may or may not be the same list that atexit uses).\n\n========================================\n\nCode:\n```text\nvoid SomeFunction();\n\nint main(int argCount, char ** argList)\n{\n    // at this point, the memory reserved for 'answer'\n    // already contains the value of 42\n    SomeFunction();\n}\n\nvoid SomeFunction()\n{\n    static int answer = 42;\n}\n```\n\n```text\nclass MyClass\n{\n    //...\n};\n\nvoid SomeFunction();\n\nint main(int argCount, char ** argList)\n{\n    SomeFunction();\n}\n\nvoid SomeFunction()\n{\n    static MyClass myVar;\n}\n```\n\n```text\nstatic bool initialized = 0;\nif (!initialized)\n{\n    // construct myVar\n    initialized = 1;\n}\n```\n\n```text\nmain()\n```\n\n```text\nmain()\n```\n\n```text\nint foo(int a){\n    static int x = a;\n    return x;\n}\n```\n\n```text\n# demangled asm from g++ -O3\nfoo(int):\n        movzx   eax, BYTE PTR guard variable for foo(int)::x[rip]  # guard.load(acquire)\n        test    al, al\n        je      .L13\n        mov     eax, DWORD PTR foo(int)::x[rip]    # normal load of the static local\n        ret              # fast path through the function is the already-initialized case\n\n\n.L13:            # jumps here on guard == 0, on the first call (and any that race with it)\n                 # It would be sensible for GCC to put this code in .text.cold\n        push    rbx\n        mov     ebx, edi             # save function arg in a call-preserved reg\n        mov     edi, OFFSET FLAT:guard variable for foo(int)::x  # address\n        call    __cxa_guard_acquire          # guard_acquire(&guard_x) presumably a normal mutex or spinlock\n        test    eax, eax \n        jne     .L14                         # if (we won the race to do the init work) goto .L14\n        mov     eax, DWORD PTR foo(int)::x[rip]  # else it's done now by another thread\n        pop     rbx\n        ret\n.L14:\n        mov     edi, OFFSET FLAT:guard variable for foo(int)::x\n        mov     DWORD PTR foo(int)::x[rip], ebx       # init static x (from a saved in RBX)\n        call    __cxa_guard_release\n        mov     eax, DWORD PTR foo(int)::x[rip]       # missed optimization:  mov eax, ebx  \n                # This thread is the one that just initialized it, our function arg is the value. \n                # It's not atomic (or volatile), so another thread can't have set it, too.\n        pop     rbx\n        ret\n```\n\n```text\nint bar(int a){\n    static int x = 1;\n    return ++x + a;\n}\n```\n\n```text\nbar(int):\n        mov     eax, DWORD PTR bar(int)::x[rip]\n        add     eax, 1\n        mov     DWORD PTR bar(int)::x[rip], eax   # store the updated value\n        add     eax, edi                          # and add it to the function arg\n        ret\n\n.section .data\n\nbar(int)::x:\n        .long   1\n```\n\n```text\n.data\n```\n\n```text\n-std=gnu++03\n```\n\n```text\nldarb  w8, [x8]\n```\n\n```text\nx\n```\n\n```text\nstatic\n```\n\n========================================\n\nComments:\n- Did something change? I've heard that after c++11 each initialization of static is thread-safe.\n- @VictorPolevoy: Yes - when this answer was written, C++11 didn't exist. In C++11, the standard included thread support, and this was added to the description of initialization of block-scope static variables (6.7/4): \"If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization\".\n- I suggest you to edit your answer since it is accepted as answer and has 10 votes. Also this question is most viewable with the topic of initialization of static variables.\n- Thanks for the part about initialization by a simple constant versus a function's return value! Good point!\n- @VictorPolevoy: gcc/clang use an acquire load of a guard variable to check that static locals have been initialized, if they don't have compile-time-constant initializers. e.g. godbolt.org/z/do89eqdMP If the guard variable is false, then they pick one thread to do the initializing, and have other threads wait for it if they also see a false guard variable. They've been doing this for a long time, since before C++11 required it. (e.g. as old as GCC4.1 on Godbolt, from May 2006.)\n- Can you give a reference/citation to its being thread safe in C++0x? I haven't found one.","metadata":{"transformedAt":"2026-08-18T18:32:17.766Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":14,"totalLines":211,"estimatedTokens":1541}}162{"id":"stack-8450030","source":"stackoverflow","questionId":8450030,"title":"Can modern C++ compilers inline functions that are defined in a cpp file","tags":["c++","compiler-construction","inline"],"text":"Title: Can modern C++ compilers inline functions that are defined in a cpp file\nTags: c++, compiler-construction, inline\nSource: Stack Overflow\n\nQuestion:\nI am aware that the keyword `inline` has useful properties e.g. for keeping template specializations inside a header file.\nOn the other hand I have often read that `inline` is almost useless as hint for the compiler to actually inline functions.\nFurther the keyword cannot be used inside a cpp file since the compiler wants to inspect functions marked with the `inline` keyword whenever they are called.\n\nHence I am a little confused about the \"automatic\" inlining capabilities of modern compilers (namely gcc 4.43). When I define a function inside a cpp, can the compiler inline it anyway if it deems that inlining makes sense for the function or do I rob him of some optimization capabilities ? (Which would be fine for the majority of functions, but important to know for small ones called very often)\n\n========================================\n\nTop Answer:\nWithin the compilation unit the compiler will have no problem inline functions (even if they are not marked as inline). Across compilation units it is harder but modern compilers can do it.\n\nUse of the `inline tag` has little affect on 'modern' compilers and whether it actually inlines functions (it has better heuristics than the human mind) (unless you specify flags to force it one way or the other (which is usually a bad idea as humans are bad at making this decision)).\n\n========================================\n\nCode:\n```text\ninline\n```\n\n```text\ninline\n```\n\n```text\ninline\n```\n\n```text\n-combine\n```\n\n```text\n-fwhole-program\n```\n\n```text\ngcc\n```\n\n```text\ncpp\n```\n\n```text\ninline tag\n```\n\n========================================\n\nComments:\n- You should check out your compiler's link-time optimization support. In GCC, this is enabled with `-flto`.\n- Many thanks for all the answers. Since they are similar I chose the fastest one as accepted, while all three have taught me something.\n- We do not want to encourage fast answers. There is already too much of a race that people type common (but incorrect information) quickly to get the up-votes. We want to encourage thoughtful and informative answers. Note: Note that I disagree with your final choice just the method it was derived by.\n- @Loki: I totally agree with you and this race is a common plague in q&a. Perhaps I should add one more reason. If the content is valid, and satisfies my answer, I tend to accept the answer from the one with the lowest rep. I think that helps people to build up some reputation, encourages participation and leads to more diversity in the portal. Of course this is only true for good answers.\n- To clarify: do you mean inline *across* files? By \"defined in a cpp file\" do you mean \"not in a shared hpp\" is that it?\n- +1, but I disagree with the `inline tag has little affect`. From what I've seen, even the most recent additions of MSVC will inline any and all functions marked as such, (and then crash when it runs out of memory. hooray!)\n- @MooingDuck: From: msdn.microsoft.com/en-us/library/z8y1yy88%28v=vs.71%29.aspx The insertion (called inline expansion or inlining) occurs only if the compiler's cost/benefit analysis show it to be profitable. . Inlining everything marked inline by a user would be very silly. Humans are notoriously bad programmers and have little real understanding of many parts of the system (long live our machine overlords).\n- @LokiAstari: connect.microsoft.com/VisualStudio/feedback/details/699728/&hellip;&zwnj;&#8203;: Proof that MSVC will inline some functions even to the point of running out of memory. GCC compiles and runs it just fine.\n- That just shows that MS compiler is over-aggressive in its heuristics while gcc is a bit more timid.","metadata":{"transformedAt":"2026-08-18T18:32:17.766Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":65,"estimatedTokens":948}}163{"id":"stack-21195910","source":"stackoverflow","questionId":21195910,"title":"Does the return value always go into eax register after a method call?","tags":["c++","assembly","compiler-construction","x86"],"text":"Title: Does the return value always go into eax register after a method call?\nTags: c++, assembly, compiler-construction, x86\nSource: Stack Overflow\n\nQuestion:\nI have written a hooking library, that examines a PE executables dll import table, to create a library that enables changing of parameters and return values. I have a few questions on how the return value is passed from a function.\n\nI have learned that the return value of a function is saved in the accumulator register. Is this always the case? If not, how does the compiler know where to look for the function result?\n\nWhat about the return type size? An integer will easily fit, but what about a bigger structure? Does the caller reserve stack space so the method it calls could write the result onto stack?\n\n========================================\n\nTop Answer:\nIf the function get inlined, the result is not saved in eax, also if results are passed by reference/pointer, that register won't be used.\n\nlook at what happens to a function that return doubles (on a 32 bit machine)\n\n```\ndouble func(){\n volatile double val=5.0;\n return val;\n}\n\nint main(){\n double val = func();\n return 0;\n}\n```\n\ndoubles are not in eax.\n\n```\nfunc():\n pushq %rbp\n movq %rsp, %rbp\n movabsq $4617315517961601024, %rax\n movq %rax, -8(%rbp)\n movq -8(%rbp), %rax\n movq %rax, -24(%rbp)\n movsd -24(%rbp), %xmm0\n popq %rbp\n ret\nmain:\n pushq %rbp\n movq %rsp, %rbp\n subq $24, %rsp\n call func()\n movsd %xmm0, -24(%rbp)\n movq -24(%rbp), %rax\n movq %rax, -8(%rbp)\n movl $0, %eax\n leave\n ret\n```\n\n========================================\n\nCode:\n```text\nsome_struct foo(int arg1, int arg2);\n    some_struct s = foo(1, 2);\n```\n\n```text\nsome_struct* foo(some_struct* ret_val, int arg1, int arg2);\n    some_struct s; // constructor isn't called\n    foo(&s, 1, 2); // constructor will be called in foo\n```\n\n```text\nsizeof(some_struct) <= 4\n```\n\n```text\nip4_type\n```\n\n```text\nunsigned\n```\n\n```text\nunsigned\n```\n\n```text\nin_addr\n```\n\n```text\nchar*\n```\n\n```text\nunigned\n```\n\n```text\ndouble func(){\n    volatile double val=5.0;\n    return val;\n}\n\nint main(){\n    double val = func();\n    return 0;\n}\n```\n\n```text\nfunc():\n    pushq   %rbp\n    movq    %rsp, %rbp\n    movabsq $4617315517961601024, %rax\n    movq    %rax, -8(%rbp)\n    movq    -8(%rbp), %rax\n    movq    %rax, -24(%rbp)\n    movsd   -24(%rbp), %xmm0\n    popq    %rbp\n    ret\nmain:\n    pushq   %rbp\n    movq    %rsp, %rbp\n    subq    $24, %rsp\n    call    func()\n    movsd   %xmm0, -24(%rbp)\n    movq    -24(%rbp), %rax\n    movq    %rax, -8(%rbp)\n    movl    $0, %eax\n    leave\n    ret\n```\n\n```text\nEAX\n```\n\n```text\nEAX\n```\n\n```text\nEDX\n```\n\n========================================\n\nComments:\n- Why not compile a couple of examples and see for yourself?\n- No, the ARM7 processor doesn't have an EAX register. Neither do other processors.\n- Obviously if the return type does not fit in `eax`, it can't be in it. Refer to the documentation of whatever calling convention you're using.\n- Bottom line: it depends completely on the compiler and the conventions it uses. (I build a compiler that passes double float args in EAX/EDX [sometimes]).\n- not if your methods return void and pass values through references or pointers.\n- The MS Windows32 coventions do not pass arguments in EAX: Standard CDECL calls push arguments in the stack; fastcall's first argument is in ECX. Oh, you mean *popular* operating systems, unlike Windows :-}\n- Just for the sake of completeness: Borland's compilers on Windows use eax as a first argument with __fastcall ;)\n- I like this answer because it names the context, giving a searchable term in case someone wants to orient themselves further.\n- your code is 64 bits, not 32. There's no rax, rbp, rsp... on 32-bit x86\n- Can someone help me figure out why is this happening: `movq %rax, -8(%rbp) movq -8(%rbp), %rax movq %rax, -24(%rbp) movsd -24(%rbp), %xmm0` in the `func()` call (line 5 to line 8)?\n- addition: 64-bit values afaik are returned in edx:eax on x86\n- @MarcovandeVoort yes, you're right, though it is true for integer values only.\n- Well, for integer values other than COMP :-)\n- In MSVC: `A scalar return value that can fit into 64 bits, including the __m64 type, is returned through RAX`\n- @ScienceDiscoverer on a 64bit CPU, yes. What was written earlier applies to a 32bit CPU.","metadata":{"transformedAt":"2026-08-18T18:32:17.767Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":14,"totalLines":164,"estimatedTokens":1077}}164{"id":"stack-14674912","source":"stackoverflow","questionId":14674912,"title":"Why are there LR(0) parsers but not LL(0) parsers?","tags":["parsing","compiler-construction","lr-grammar","ll-grammar"],"text":"Title: Why are there LR(0) parsers but not LL(0) parsers?\nTags: parsing, compiler-construction, lr-grammar, ll-grammar\nSource: Stack Overflow\n\nQuestion:\nI've been reading on both in Wikipedia, and noticed that although LR(0) parsers exist, there's no such thing as LL(0) parser.\n\nFrom what I read, I understand that the k in LL(k)/LR(k) means how many characters the parser can see **beyond** the current character that it's currently working on.\n\nSo my question is, why is there no such thing as LL(0) parser even though LR(0) exists?\n\n========================================\n\nComments:\n- Thanks! Just one question, LL(0) parser can still read the input right? So isn't it's decision based on the current token AND the current nonterminal that's on top of it's stack?\n- @Shmoopy- An LL(0) parser only looks at input tokens when the first token of the string derived so far is a terminal. If there is a nonterminal at the front, the parser does not look at the input at all.\n- Your explanation seems interesting, but not entirely clear to me. By definition, LL(0) can look at 0 input tokens to decide which production to choose. So do you mean that it only sees input in retrospect (as encoded in its state) and hence can only \"handle\" grammars that produce can only derive a single terminal?\n- @EliBendersky- In an LL(0) grammar, the parser decides whether to do a predict or a match step based on the next symbol left in the sentential form. If it's a terminal, it does a match step. If it's a nonterminal, it does a predict step based on what that nonterminal is. When doing the predict step, the parser looks at the next 0 tokens of the input stream to determine which production to use. Because of this, the parser has to be able to uniquely determine which production to use based on the nonterminal in the sentential form, without looking at the input at all. Does that make sense?\n- Wouldn't LL(0) parser be like recursive-descent parser?\n- @MichaelHoChum Not really. LL parsers always commit to a fixed production when they make a prediction, so an LL(0) parser would have to entirely guess the productions to use correctly with no feedback on what to do based on the input. LL(1) is closer to recursive descent, but has no backtracking.\n- What can be said about SLR(0) and LALR(0)? I can find only SLR(1) and LALR(1) parsers discussed in dragon book. Also they are not discussed online. I know for LR(0), reduce moves appear in full row. What happens in case of SLR(0) and LALR(0)?\n- @anir Interesting question - I’ve never seen SLR(0) before. I suspect that it’s completely equivalent to LR(0), but I’d need to consult a formal definition of SLR(k) parsers to know for sure.\n- Asked the same question here. Got some good comments.\n- @templatetypedef suppose the grammar is ***S->aA | bB***, ***A->*** *something*, ***B->*** *something*. Suppose the parser is LL(0) and the input is ab. At the start we would have S in the stack. Now an LL(0) parser doesn't need lookahead symbols to decide which production to choose since it can decide that based on the current symbol itself which is a. So it would choose the production S->aA. So I don't see why you said that the language would have only one string or the parser just keeps looping.\n- The 1 in LL(1) refers to the number of tokens the parser can look at at each step when making a decision about which production to pick. In an LL(0) parser, that means the parser can’t see any tokens when determining which production to pick, so if the input is ab the parser can’t see the a when deciding which of the S productions to pick.\n- Is there an answer that explains why LL(1) grammar and LR(0) grammar are incomparable.","metadata":{"transformedAt":"2026-08-18T18:32:17.767Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":26,"estimatedTokens":921}}165{"id":"stack-11803678","source":"stackoverflow","questionId":11803678,"title":"Why does MSFT C# compile a Fixed \"array to pointer decay\" and \"address of first element\" differently?","tags":["c#",".net","compiler-construction","il"],"text":"Title: Why does MSFT C# compile a Fixed \"array to pointer decay\" and \"address of first element\" differently?\nTags: c#, .net, compiler-construction, il\nSource: Stack Overflow\n\nQuestion:\nThe .NET c# compiler (.NET 4.0) compiles the `fixed` statement in a rather peculiar way.\n\nHere's a short but complete program to show you what I am talking about.\n\n```\nusing System;\n\npublic static class FixedExample {\n\n public static void Main() {\n byte [] nonempty = new byte[1] {42};\n byte [] empty = new byte[0];\n \n Good(nonempty);\n Bad(nonempty);\n\n try {\n Good(empty);\n } catch (Exception e){\n Console.WriteLine(e.ToString());\n /* continue with next example */\n }\n Console.WriteLine();\n try {\n Bad(empty);\n } catch (Exception e){\n Console.WriteLine(e.ToString());\n /* continue with next example */\n }\n }\n\n public static void Good(byte[] buffer) {\n unsafe {\n fixed (byte * p = &buffer[0]) {\n Console.WriteLine(*p);\n }\n }\n }\n\n public static void Bad(byte[] buffer) {\n unsafe {\n fixed (byte * p = buffer) {\n Console.WriteLine(*p);\n }\n }\n }\n}\n```\n\nCompile it with \"csc.exe FixedExample.cs /unsafe /o+\" if you want to along.\n\nHere's the generated IL for the method `Good`:\n\n**Good()**\n\n```\n.maxstack 2\n .locals init (uint8& pinned V_0)\n IL_0000: ldarg.0\n IL_0001: ldc.i4.0\n IL_0002: ldelema [mscorlib]System.Byte\n IL_0007: stloc.0\n IL_0008: ldloc.0\n IL_0009: conv.i\n IL_000a: ldind.u1\n IL_000b: call void [mscorlib]System.Console::WriteLine(int32)\n IL_0010: ldc.i4.0\n IL_0011: conv.u\n IL_0012: stloc.0\n IL_0013: ret\n```\n\nHere's the generated IL for the method `Bad`:\n\n**Bad()**\n\n```\n.locals init (uint8& pinned V_0, uint8[] V_1)\n IL_0000: ldarg.0\n IL_0001: dup\n IL_0002: stloc.1\n IL_0003: brfalse.s IL_000a\n IL_0005: ldloc.1\n IL_0006: ldlen\n IL_0007: conv.i4\n IL_0008: brtrue.s IL_000f\n IL_000a: ldc.i4.0\n IL_000b: conv.u\n IL_000c: stloc.0\n IL_000d: br.s IL_0017\n IL_000f: ldloc.1\n IL_0010: ldc.i4.0\n IL_0011: ldelema [mscorlib]System.Byte\n IL_0016: stloc.0\n IL_0017: ldloc.0\n IL_0018: conv.i\n IL_0019: ldind.u1\n IL_001a: call void [mscorlib]System.Console::WriteLine(int32)\n IL_001f: ldc.i4.0\n IL_0020: conv.u\n IL_0021: stloc.0\n IL_0022: ret\n```\n\nHere's what `Good` does:\n\n- Get the address of buffer[0].\n\n- Dereference that address.\n\n- Call WriteLine with that dereferenced value.\n\nHere's what 'Bad` does:\n\n- If buffer is null, GOTO 3.\n\n- If buffer.Length != 0, GOTO 5.\n\n- Store the value 0 in local slot 0,\n\n- GOTO 6.\n\n- Get the address of buffer[0].\n\n- Deference that address (in local slot 0, which may be 0 or buffer now).\n\n- Call WriteLine with that dereferenced value.\n\nWhen `buffer` is both non-null and non-empty, these two functions do the same thing. Notice that `Bad` just jumps through a few hoops before getting to the `WriteLine` function call.\n\nWhen `buffer` is null, `Good` throws a `NullReferenceException` in the *fixed-pointer declarator* (`byte * p = &buffer[0]`). Presumably this is the desired behavior for fixing a managed array, because in general any operation inside of a *fixed-statement* will depend on the validity of the object being fixed. Otherwise why would that code be inside the `fixed` block? When `Good` is passed a null reference, it fails immediately at the start of the `fixed` block, providing a relevant and informative stack trace. The developer will see this and realize that he ought to validate `buffer` before using it, or perhaps his logic incorrectly assigned `null` to `buffer`. Either way, clearly entering a `fixed` block with a `null` managed array is not desirable.\n\n`Bad` handles this case differently, even undesirably. You can see that `Bad` does not actually throw an exception until `p` is dereferenced. It does so in the roundabout way of *assigning null* to the same local slot that holds `p`, then later throwing the exception when the `fixed` block statements dereference `p`.\n\nHandling `null` this way has the advantage of keeping the object model in C# consistent. That is, inside the `fixed` block, `p` is still treated semantically as a sort of \"pointer to a managed array\" that will not, when null, cause problems until (or unless) it is dereferenced. Consistency is all well and good, but the problem is that **p is not a pointer to a managed array**. It is a pointer to the first element of `buffer`, and anybody who has written this code (`Bad`) would interpret its semantic meaning as such. You can't get the size of `buffer` from `p`, and you can't call `p.ToString()`, so why treat it as though it were an object? In cases where `buffer` is null, there is clearly a coding mistake, and I believe it would be vastly more helpful if `Bad` would throw an exception at the *fixed-pointer declarator*, rather than inside the method.\n\nSo it seems that `Good` handles `null` better than `Bad` does. What about empty buffers?\n\nWhen `buffer` has Length 0, `Good` throws `IndexOutOfRangeException` at the *fixed-pointer declarator*. That seems like a completely reasonable way to handle out of bounds array access. After all, the code `&buffer[0]` should be treated the same way as `&(buffer[0])`, which should obviously throw `IndexOutOfRangeException`.\n\n`Bad` handles this case differently, and again undesirably. Just as would be the case if `buffer` were `null`, when `buffer.Length == 0`, `Bad` does not throw an exception until `p` is dereferenced, and at that time it throws **NullReferenceException, not IndexOutOfRangeException!** If `p` is never dereferenced, then the code does not even throw an exception. Again, it seems that the idea here is to give `p` the semantic meaning of \"pointer to a managed array\". Yet again, I do not think that anybody writing this code would think of `p` that way. The code would be much more helpful if it threw `IndexOutOfRangeException` in the *fixed-pointer declarator*, thereby notifying the developer that the array passed in was empty, and not `null`.\n\nIt looks like `fixed(byte * p = buffer)` should have been compiled to the same code as was `fixed (byte * p = &buffer[0])`. **Also notice that even though `buffer` could have been any arbitrary expression, it's type (`byte[]`) is known at compile time and therefore the code in `Good` would work for any arbitrary expression.**\n\n**Edit**\n\nIn fact, notice that the implementation of `Bad` actually does the error checking on `buffer[0]` *twice*. It does it explicitly at the beginning of the method, and then does it again implicitly at the `ldelema` instruction.\n\nSo we see that the `Good` and `Bad` are semantically different. `Bad` is longer, probably slower, and certainly does not give us desirable exceptions when we have bugs in our code, and even fails much later than it should in some cases.\n\nFor those curious, the section 18.6 of the spec (C# 4.0) says that behavior is \"Implementation-defined\" in both of these failure cases:\n\nA fixed-pointer-initializer can be one of the following:\n\n• The token “&” followed by a variable-reference (§5.3.3) to a moveable variable (§18.3) of an unmanaged type T, provided the type T* is implicitly convertible to the pointer type given in the fixed statement. In this case, the initializer computes the address of the given variable, and the variable is guaranteed to remain at a fixed address for the duration of the fixed statement.\n\n• An expression of an array-type with elements of an unmanaged type T, provided the type T* is implicitly convertible to the pointer type given in the fixed statement. In this case, the initializer computes the address of the first element in the array, and the entire array is guaranteed to remain at a fixed address for the duration of the fixed statement. **The behavior of the fixed statement is implementation-defined if the array expression is null or if the array has zero elements.**\n\n... other cases ...\n\nLast point, the MSDN documentation suggests that the two are \"equivalent\" :\n\n// The following two assignments are equivalent...\n\nfixed (double* p = arr) { /*...*/ }\n\nfixed (double* p = &arr[0]) { /*...*/ }\n\nIf the two are supposed to be \"equivalent\", then why use different error handling semantics for the former statement?\n\nIt also appears that *extra effort* was put into writing the code paths generated in `Bad`. The compiled code in `Good` works fine for all the failure cases, and is the same as the code in `Bad` in non-failure cases. Why implement new code paths instead of just using the simpler code generated for `Good`?\n\n**Why is it implemented this way?**\n\n========================================\n\nTop Answer:\nSo we see that the Good and Bad are semantically different. Why?\n\nBecause Good is case 1 and bad is case 2.\n\nGood does not assign an \"An expression of an array-type\". It assigns \"The token “&” followed by a variable-reference\" so it is case 1. Bad assigns \"An expression of an array-type\" making it case 2. If this is true the MSDN documentation is wrong.\n\nIn any case this explains why the C# compiler creates two different (and in the second case specialized) code patterns.\n\nWhy does case 1 generate such simple code? I am speculating here: Taking the address of an array element is probably compiled the same way as using `array[index]` in a `ref`-expression. At the CLR level, `ref` parameters and expressions are just managed pointers. So is the expression `&array[index]`: It is compiled to a managed pointer that is not pinned but \"interior\" (this term comes from Managed C++ I think). The GC fixes it automatically. It behaves like a normal object reference.\n\nSo case 1 gets the usual managed pointer treatment while case 2 gets a special, implementation defined (not undefined) behavior.\n\nThis is not answering all of your questions but at least it provides some reasons for your observations. I'm kind of hoping for Eric Lippert to add his answer as an insider.\n\n========================================\n\nCode:\n```text\nusing System;\n\npublic static class FixedExample {\n\n    public static void Main() {\n        byte [] nonempty = new byte[1] {42};\n        byte [] empty = new byte[0];\n        \n        Good(nonempty);\n        Bad(nonempty);\n\n        try {\n            Good(empty);\n        } catch (Exception e){\n            Console.WriteLine(e.ToString());\n            /* continue with next example */\n        }\n        Console.WriteLine();\n        try {\n            Bad(empty);\n        } catch (Exception e){\n            Console.WriteLine(e.ToString());\n            /* continue with next example */\n        }\n     }\n\n    public static void Good(byte[] buffer) {\n        unsafe {\n            fixed (byte * p = &buffer[0]) {\n                Console.WriteLine(*p);\n            }\n        }\n    }\n\n    public static void Bad(byte[] buffer) {\n        unsafe {\n            fixed (byte * p = buffer) {\n                Console.WriteLine(*p);\n            }\n        }\n    }\n}\n```\n\n```text\n.maxstack  2\n  .locals init (uint8& pinned V_0)\n  IL_0000:  ldarg.0\n  IL_0001:  ldc.i4.0\n  IL_0002:  ldelema    [mscorlib]System.Byte\n  IL_0007:  stloc.0\n  IL_0008:  ldloc.0\n  IL_0009:  conv.i\n  IL_000a:  ldind.u1\n  IL_000b:  call       void [mscorlib]System.Console::WriteLine(int32)\n  IL_0010:  ldc.i4.0\n  IL_0011:  conv.u\n  IL_0012:  stloc.0\n  IL_0013:  ret\n```\n\n```text\n.locals init (uint8& pinned V_0, uint8[] V_1)\n  IL_0000:  ldarg.0\n  IL_0001:  dup\n  IL_0002:  stloc.1\n  IL_0003:  brfalse.s  IL_000a\n  IL_0005:  ldloc.1\n  IL_0006:  ldlen\n  IL_0007:  conv.i4\n  IL_0008:  brtrue.s   IL_000f\n  IL_000a:  ldc.i4.0\n  IL_000b:  conv.u\n  IL_000c:  stloc.0\n  IL_000d:  br.s       IL_0017\n  IL_000f:  ldloc.1\n  IL_0010:  ldc.i4.0\n  IL_0011:  ldelema    [mscorlib]System.Byte\n  IL_0016:  stloc.0\n  IL_0017:  ldloc.0\n  IL_0018:  conv.i\n  IL_0019:  ldind.u1\n  IL_001a:  call       void [mscorlib]System.Console::WriteLine(int32)\n  IL_001f:  ldc.i4.0\n  IL_0020:  conv.u\n  IL_0021:  stloc.0\n  IL_0022:  ret\n```\n\n```text\nfixed\n```\n\n```text\nGood\n```\n\n```text\nBad\n```\n\n```text\nGood\n```\n\n```text\nbuffer\n```\n\n```text\nBad\n```\n\n```text\nWriteLine\n```\n\n```text\nbuffer\n```\n\n```text\nGood\n```\n\n```text\nNullReferenceException\n```\n\n```text\nbyte * p = &buffer[0]\n```\n\n```text\nfixed\n```\n\n```text\nGood\n```\n\n```text\nfixed\n```\n\n```text\nbuffer\n```\n\n```text\nnull\n```\n\n```text\nbuffer\n```\n\n```text\nfixed\n```\n\n```text\nnull\n```\n\n```text\nBad\n```\n\n```text\nBad\n```\n\n```text\np\n```\n\n```text\np\n```\n\n```text\nfixed\n```\n\n```text\np\n```\n\n```text\nnull\n```\n\n```text\nfixed\n```\n\n```text\np\n```\n\n```text\nbuffer\n```\n\n```text\nBad\n```\n\n```text\nbuffer\n```\n\n```text\np\n```\n\n```text\np.ToString()\n```\n\n```text\nbuffer\n```\n\n```text\nBad\n```\n\n```text\nGood\n```\n\n```text\nnull\n```\n\n```text\nBad\n```\n\n```text\nbuffer\n```\n\n```text\nGood\n```\n\n```text\nIndexOutOfRangeException\n```\n\n```text\n&buffer[0]\n```\n\n```text\n&(buffer[0])\n```\n\n```text\nIndexOutOfRangeException\n```\n\n```text\nBad\n```\n\n```text\nbuffer\n```\n\n```text\nnull\n```\n\n```text\nbuffer.Length == 0\n```\n\n```text\nBad\n```\n\n```text\np\n```\n\n```text\np\n```\n\n```text\np\n```\n\n```text\np\n```\n\n```text\nIndexOutOfRangeException\n```\n\n```text\nnull\n```\n\n```text\nfixed(byte * p = buffer)\n```\n\n```text\nfixed (byte * p = &buffer[0])\n```\n\n```text\nbuffer\n```\n\n```text\nbyte[]\n```\n\n```text\nGood\n```\n\n```text\nBad\n```\n\n```text\nbuffer[0]\n```\n\n```text\nldelema\n```\n\n```text\nGood\n```\n\n```text\nBad\n```\n\n```text\nBad\n```\n\n```text\nBad\n```\n\n```text\nGood\n```\n\n```text\nBad\n```\n\n```text\nGood\n```\n\n```text\nbyte * p = null\n```\n\n```text\nWriteLine(*p)\n```\n\n```text\n&arr[0]\n```\n\n```text\narr\n```\n\n```text\narray[index]\n```\n\n```text\nref\n```\n\n```text\nref\n```\n\n```text\n&array[index]\n```\n\n========================================\n\nComments:\n- @pst I removed `SoWhat` and its explanation for brevity. Fixed.\n- Have you checked what the C# spec says about `arr[0]`? I'm pretty sure `arr[0]` throws an exception when `arr` is null or empty in any context, regardless of any `fixed` statement that surrounds it.\n- @hvd Yes that is why `fixed(byte *p = buffer)` should just be treated as `fixed(byte *p = &buffer[0])`. The latter takes care of error checking more cleanly. I'll add this to the question.\n- Funny, I always thought this behavior was the most intuitive...\n- I wonder how this is defined (or stated to be UB/IB) in C .. to toss some Oranges into the mix.\n- C has no managed memory so the entire concept of the special `fixed` behavior described here doesn't exist. Other than that: in C, it is undefined behavior to reference an element of an array that does not exist, so `&arr[0]` is undefined when `arr` is `NULL`. I don't think standard C or C++ currently permits 0-length arrays (C++0x appears to, when using `new[]`); GCC does as an extension, but I have no idea what the behavior there is.\n- @MichaelEdenfield Not exactly. `&arr[0]` is indeed invalid in C when `arr` is null, but that's because `&arr[0]` is short for `&*(arr+0)`, and arithmetic is invalid on null pointers. `&*arr`, on the other hand, *is* valid, even when `arr` is null, as is `&arr[1]` when `arr` is an array of length 1.\n- I was always lead to believe that this was the one place there `*(arr + 1)` and `arr[1]` were different: if `arr` had no element at `[1]` then the first was a legal pointer one past the end of the array, and the second was undefined. I don't do enough C anymore to keep track :)\n- \"If this is true the MSDN documentation is wrong.\" Not really, just slightly misleading, as I understand it. The two forms are equivalent for those buffers for which both forms have defined behaviour.\n- @hvd, yes for those cases. But not for all. The documentation does not qualify its statement they way you just did.\n- @hvd I should expect that \"Implementation defined behavior\" is defined in the MSDN docs. Clearly it is not in this case.\n- Note that MS always covers their own butts by adding this caveat to their C# language pages: \"For more information, see the C# Language Specification. The language specification is the definitive source for C# syntax and usage.\"\n- @usr I have not heard the term \"interior\" before, but in this case the \"fixing\" part of the code is the same for both `Good` and `Bad`. *Only* the error checking at the beginning is different. The GC and JIT see the two \"fixing\" parts the same way (a managed ref converted to native int). At least, that's true if there aren't any JIT hacks in this case (I doubt that there are).\n- @MichaelGraczyk I'm not sure where MS normally does list C# implementation-defined behaviour, I'm not able to find it at all, not just for your specific question.\n- Oddly enough, conforming to the ECMA spec *requires* you to document those choices, but the Microsoft C# 4.0 spec conspicuously omits the \"portability issues\" appendix :)\n- According to the spec implementation defined behavior is only allowed in the second case, but Good implements the first case. `&array[0]` is *not* an array expression.\n- \"we'll have to hope that someone on the C# compiler team happens along\" That's my hope :)\n- @usr true, I was trying to get that point across in the post but I'll be more explicit about it :)\n- @MichaelEdenfield The standard actually does treat the two the same in this case. It is only in error checking that the two are different. For the second case, it says \"the initializer computes the address of the first element in the array\". That is the same as case one (since \"the given variable\" is precisely the first element in the array).\n- The standard clearly differentiates between the two; the fact that they produce the same *result* does not make them the same, as evidenced by the fact that case 2 has an implementation-defined special case and case 1 does not.\n- @MichaelEdenfield That's only for error cases. Also you say, \"byte * p = null is a perfectly legitimate statement.\" This statement is actually \"Implementation defined\". I am asking why their implementation defined it undesirably.","metadata":{"transformedAt":"2026-08-18T18:32:17.767Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":81,"totalLines":625,"estimatedTokens":4413}}166{"id":"stack-26374435","source":"stackoverflow","questionId":26374435,"title":"What is meant by the FENCE instruction in the RISC-V instruction set?","tags":["compiler-construction","processor","microprocessors","riscv"],"text":"Title: What is meant by the FENCE instruction in the RISC-V instruction set?\nTags: compiler-construction, processor, microprocessors, riscv\nSource: Stack Overflow\n\nQuestion:\nWhile going through the RISC-V ISA, I have seen an instruction in the memory model section (FENCE instruction). What does it mean exactly?\n\n========================================\n\nTop Answer:\nThe RISC-V ISA uses a relaxed memory model where the order of loads and stores performed by one thread may be different when seen by another. This is done to enable techniques to increase memory system performance.\n\nFor example, Thread 1 may execute:\n\n- Load A\n\n- Store B\n\n- Store C\n\nBut Thread 2 could see the loads and the stores out of order with regard to the first thread:\n\n- Store C\n\n- Load A\n\n- Store B\n\nThe FENCE ensures that all operations before the fence are observed before any operation after the fence. So if the above changed to:\n\nThread 1:\n\n- Load A\n\n- Store B\n\n- FENCE\n\n- Store C\n\nThen Thread 2 would be guaranteed to see the load to A and the store to B before the store to C, but still could see the store to B before the load of A.\n\nThread 2:\n\n- Store B\n\n- Load A\n\n- Store C\n\nSource: RISC-V ISA (Section 2.7 page 20)\n\nIncorporating Chris P's comment:\n\nI/O (I and O flag) and memory accesses (R and W) can be controlled\nseparately with the FENCE instruction For example: You can control\nthat only memory writes should be ordered by FENCE but memory reads\nand I/O operations are unaffected by FENCE. For this, the PW and SW\nbits should be set. If only PW (predecessor write) is set, then FENCE\nwill ensure, that all memory writes before FENCE are also observed by\nother harts (threads) before FENCE, but memory writes after FENCE can\nalso be observed before.\n\n========================================\n\nComments:\n- Can you elaborate more on the meaning of the flags this instruction takes as input? Specifically the predecessor and successor sets of I, O, R, and W flags and how their various settings affect the behavior of FENCE?\n- @SamuelA.FalvoII I/O (I and O flag) and memory accesses (R and W) can be controlled separately with the FENCE instruction For example: You can control that only memory writes should be ordered by FENCE but memory reads and I/O operations are unaffected by FENCE. For this, the PW and SW bits should be set. If only PW (predecessor write) is set, then FENCE will ensure, that all memory writes before FENCE are also observed by other harts (threads) before FENCE, but memory writes after FENCE can also be observed before.\n- If I am compiling software for RISC-V, should I target an architecture with or without fencing? I am targeting general-purpose RISC-V processors, so the \"G\" extension seems like a convenient option to target, but around the Internet I see `IMAFD` everywhere (which is G without fencing) so I'm wondering if that's a better target.\n- @AaronFranke FENCE is included in the base RISC-V ISA (per risc-v-spec-v2.2, page 20)\n- @ChrisP Can you add this to original answer. Its a necessary piece of information\n- @Mah35h - Comment added.","metadata":{"transformedAt":"2026-08-18T18:32:17.767Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":72,"estimatedTokens":768}}167{"id":"stack-1002503","source":"stackoverflow","questionId":1002503,"title":"How is C++'s multiple inheritance implemented?","tags":["c++","inheritance","compiler-construction","multiple-inheritance","language-implementation"],"text":"Title: How is C++'s multiple inheritance implemented?\nTags: c++, inheritance, compiler-construction, multiple-inheritance, language-implementation\nSource: Stack Overflow\n\nQuestion:\nSingle inheritance is easy to implement. For example, in C, the inheritance can be simulated as:\n\n```\nstruct Base { int a; }\nstruct Descendant { Base parent; int b; }\n```\n\nBut with multiple inheritance, the compiler has to arrange multiple parents inside newly constructed class. How is it done?\n\nThe problem I see arising is: should the parents be arranged in AB or BA, or maybe even other way? And then, if I do a cast:\n\n```\nSecondBase * base = (SecondBase *) &object_with_base1_and_base2_parents;\n```\n\nThe compiler must consider whether to alter or not the original pointer. Similar tricky things are required with virtuals.\n\n========================================\n\nTop Answer:\nThere was this pretty old MSDN article on how it was implemented in VC++.\n\n========================================\n\nCode:\n```text\nstruct Base { int a; }\nstruct Descendant { Base parent; int b; }\n```\n\n```text\nSecondBase * base = (SecondBase *) &object_with_base1_and_base2_parents;\n```\n\n```text\nclass Derived : A, B {} // A comes first, then B\n\nclass Derived : B, A {} // B comes first, then A\n```\n\n```text\nSecondBase base = (SecondBase *) object_with_base1_and_base2_parents;\n```\n\n```text\nclass BaseA { int a; };\nclass BaseB { int b; };\nclass Descendant : public BaseA, BaseB {};\nint main() {\n        Descendant d;\n        BaseB * b = (BaseB*) &d;\n        Descendant *d2 = (Descendant *) b;\n        printf(\"Descendant: %p, casted BaseB: %p, casted back Descendant: %p\\n\", &d, b, d2);\n}\n```\n\n```text\nDescendant: 0xbfc0e3e0, casted BaseB: 0xbfc0e3e4, casted back Descendant: 0xbfc0e3e0\n```\n\n========================================\n\nComments:\n- en.wikipedia.org/wiki/Diamond_problem\n- You C simulation forgets the VTable (implementation detail) pointer.\n- @Dario: This article deals with the overload problems in multiple inheritance but doesn't contain anything about the object layout and casting of objects in C++.\n- @Martin York: If there are no virtual methods in the classes, there is no v-table pointer.\n- Alternate link: citeseerx.ist.psu.edu/viewdoc/&hellip;\n- After reading both, depending on what you're after. This (*Stroustrup*'s one) is quite detailed and covers the theory. The one proposed by *Nemanja* below (*MSDN*'s one) is simpler and covers a real implementation.\n- Both links are dead now, but usenix.org/publications/compsystems/1989/fall_stroustrup.pdf works for me.\n- This is probably common, but I don't think it's required.\n- I have seen it the other way. It all depends on the implementation.\n- The order only has an effect on the order of constructor invocations. But the layout is unspecified. I did tests a while ago, and GCC puts empty bases first in memory, to make use of the empty base class optimization.\n- I think it is much easier to implement if the language does not support \"raw\" pointers to refernce objects and no POD backwards compatibility. Because if they do the casting of pointers to objects is very tricky. A language could add as much meta information into the reference class and the class instance as it want. In C++ this could not be done very easily.","metadata":{"transformedAt":"2026-08-18T18:32:17.767Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":79,"estimatedTokens":817}}168{"id":"stack-475949","source":"stackoverflow","questionId":475949,"title":"How to determine whether a language is LL(1) LR(0) SLR(1)","tags":["parsing","compiler-construction","theory","grammar","bnf"],"text":"Title: How to determine whether a language is LL(1) LR(0) SLR(1)\nTags: parsing, compiler-construction, theory, grammar, bnf\nSource: Stack Overflow\n\nQuestion:\nIs there a simple way to determine whether a grammar is LL(1), LR(0), SLR(1)... just from looking on the grammar without doing any complex analysis?\n\nFor instance: To decide whether a BNF Grammar is LL(1) you have to calculate First and sets - which can be time consuming in some cases.\n\nHas anybody got an idea how to do this faster?\nAny help would really be appreciated!\n\n========================================\n\nTop Answer:\nIn answer to your main question: For a very simple grammar, it may be possible to determine whether it is LL(1) without constructing FIRST and sets, e.g.\n\n A &rarr; A + A | a\n\n \n is not LL(1), while\n\n \n A &rarr; a | b\n\nis.\n\nBut when you get more complex than that, you'll need to do some analysis.\n\n A &rarr; B | a\n\n B &rarr; A + A\n\nThis is not LL(1), but it may not be immediately obvious \n\nThe grammar rules for arithmetic quickly get very complex:\n\n expr &rarr; term { '+' term }\n\n term &rarr; factor { '*' factor }\n\n factor &rarr; number | '(' expr ')'\n\nThis grammar handles only multiplication and addition, and already it's not immediately clear whether the grammar is LL(1). It's still possible to evaluate it by looking through the grammar, but as the grammar grows it becomes less feasable. If we're defining a grammar for an entire programming language, it's almost certainly going to take some complex analysis.\n\nThat said, there are a few obvious telltale signs that the grammar is not LL(1) &mdash; like the A &rarr; A + A above &mdash; and if you can find any of these in your grammar, you'll know it needs to be rewritten if you're writing a recursive descent parser. But there's no shortcut to verify that the grammar *is* LL(1).\n\n========================================\n\nCode:\n```text\np0 S' → E\np1 E → id\np2 E → id ( E )\np3 E → E + id\n```\n\n```text\nid ( id + id )\n```\n\n========================================\n\nComments:\n- Good point about the distinction between language and grammar. If a grammar is not LL(1), it may still be possible to construct an LL(1) grammar for the language.\n- +1 for pedantry. It's a key distinction, and the fact that it's usually glossed over is an obstacle to understanding.\n- @JasonOrendorff: Ditto.\n- BTW, you don't need to compute for LL(1), since it's only defined in terms of FIRST.\n- @jpalecek: You actually do need sets for LL(1) in case the nonterminals are nullable. That way, you can \"look past\" the nonterminal to see which production to use.","metadata":{"transformedAt":"2026-08-18T18:32:17.767Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":69,"estimatedTokens":647}}169{"id":"stack-2129532","source":"stackoverflow","questionId":2129532,"title":"GLR parsing algorithm resources","tags":["compiler-construction","parsing","parser-generator","glr"],"text":"Title: GLR parsing algorithm resources\nTags: compiler-construction, parsing, parser-generator, glr\nSource: Stack Overflow\n\nQuestion:\nI am writing a GLR parser generator and would like some advice on resources relating to this algorithm both on the internet and of the dead-tree variety (books for those unfamiliar with the geek-speak).\n\nI know Bison can generate GLR parsers, and given it's under the GPL I can examine its code, however it'd be nice to have a full description of the algorithm.\n\nSo, does anybody know of any good resources out there which I can make use of? Thanks.\n\n========================================\n\nTop Answer:\nAdrian Johnstone publishes a lot of work on advanced versions of GLR algorithms. His publications website will likely be an interesting resource.\n\n========================================\n\nComments:\n- Very cool project (Terse, not the parser generator), I’ll its progress with interest.\n- Unfortunately I've been distracted by many things, so the project has stalled but... more work will commence on it soon promise!!\n- Is it really as simple as that? Or are there specific alternative GLR algorithms out there?\n- That's basically it from my reading of the Bison manual (gnu.org/software/bison/manual/bison.html#GLR-Parsers) - it's actually pretty clear on how GLR parsers work.\n- You might find it a bit more complicated than this.\n- yeah, having read one of the papers @Matthew Slattery recommended, I do think the algorithm is rather more involved than that :)","metadata":{"transformedAt":"2026-08-18T18:32:17.767Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":376}}170{"id":"stack-4516778","source":"stackoverflow","questionId":4516778,"title":"when is java faster than c++ (or when is JIT faster then precompiled)?","tags":["java","performance","optimization","compiler-construction","jit"],"text":"Title: when is java faster than c++ (or when is JIT faster then precompiled)?\nTags: java, performance, optimization, compiler-construction, jit\nSource: Stack Overflow\n\nQuestion:\n**Possible Duplicate:**\n\n JIT compiler vs offline compilers \n\nI have heard that under certain circumstances, Java programs or rather parts of java programs are able to be executed faster than the \"same\" code in C++ (or other precompiled code) due to JIT optimizations. This is due to the compiler being able to determine the scope of some variables, avoid some conditionals and pull similar tricks at runtime.\n\nCould you give an (or better - some) example, where this applies? And maybe outline the exact conditions under which the compiler is able to optimize the bytecode beyond what is possible with precompiled code?\n\n**NOTE :** This question is **not** about comparing Java to C++. Its about the possibilities of JIT compiling. Please no flaming. I am also not aware of any duplicates. Please point them out if you are.\n\n========================================\n\nTop Answer:\nWikipedia: http://en.wikipedia.org/wiki/Just-in-time_compilation#Overview\n\nIn addition, it can in some cases ***offer better performance than static compilation***, as *many optimizations are only feasible at run-time*:\n\nThe compilation can be *optimized to the targeted CPU and the operating system model* where the application runs. For example JIT can choose SSE2 CPU instructions when it detects that the CPU supports them. To obtain this level of optimization specificity with a static compiler, one must either compile a binary for each intended platform/architecture, or else include multiple versions of portions of the code within a single binary.\n\nThe system is able to *collect statistics* about how the program is actually running in the environment it is in, and it can *rearrange and recompile for optimum performance*. However, some static compilers can also take profile information as input.\n\nThe system can do *global code optimizations* (e.g. inlining of library functions) without losing the advantages of dynamic linking and without the overheads inherent to static compilers and linkers. Specifically, when doing global inline substitutions, a *static compilation process may need run-time checks* and ensure that a virtual call would occur if the actual class of the object overrides the inlined method, and boundary condition checks on array accesses may need to be processed within loops. With just-in-time compilation in many cases *this processing can be moved out of loops, often giving large increases of speed*.\n\nAlthough this is possible with statically compiled garbage collected languages, a bytecode system can more easily *rearrange executed code for better cache utilization*.\n\n========================================\n\nComments:\n- this one turns out to actually be a duplicate. sorry for the inconvenience. please merge\n- Great information, but reading closely reveals that precompilation actually can and does do many of the \"JIT-only\" optimizations.\n- @BenVoigt That's a good point. The main remaining argument is that the JIT has access to information specific to the running process, instead of a pre-created profile or similar. Therefore, it can perform bold optimizations more frequently and with greater chances of success.\n- The first point is additionally valid as many Java libraries where written before new CPU archectures were available. These old libraries still make use of the latest CPu improvements. To make use of the latest architecture in C++ you have to be able to compile from source which many not be possible/practical with third party libraries. esp. if the developer is not the end user. e.g. you have an application which must be deployed to many different types of PCs, it can be a nightmare to release every possible platform so the lowest common denomiator is often chosen.\n- I believe the JIT can perform polymorphic inlining. i.e it knows up to two possible \"virtual\" method which are usually called and these can be inlined and if the object is not one of these classes there is a fall back. This means even virtual methods with multiple possible implementations can be inlined based on runtime behaviour.\n- C++ profile-guided optimizers use these same tricks.\n- a good link is sometimes as good as an answer can get. very insightful. thank you\n- Nice straw man argument. C++ doesn't require you to use `malloc`, see for example wireshark's pool allocator.\n- so the argument doesn't apply to C++ allocation with new then? Is it because heap allocated by HeapAlloc and alike is used for allocation with new?\n- thank you. this is the level of detail i hoped for.","metadata":{"transformedAt":"2026-08-18T18:32:17.767Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":43,"estimatedTokens":1173}}171{"id":"stack-23018496","source":"stackoverflow","questionId":23018496,"title":"Where is the \"Section to segment mapping\" stored in ELF files?","tags":["compiler-construction","elf","readelf"],"text":"Title: Where is the \"Section to segment mapping\" stored in ELF files?\nTags: compiler-construction, elf, readelf\nSource: Stack Overflow\n\nQuestion:\nAs part of trying to write a compiler completely from scratch, I'm currently working on the part the handles ELF files.\n\nAfter skimming through several articles and specifications about them, I still don't quite understand where section to segment mappings are stored.\nWhen observing small executables generated by NASM+ld, I can see that the .text section is somehow mapped onto a LOAD-type program header, but how?\n\nA small piece of readelf's output when given a small (working) executable as input:\n\n```\nProgram Headers:\n Type Offset VirtAddr PhysAddr\n FileSiz MemSiz Flags Align\n LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000\n 0x0000000000000084 0x0000000000000084 R E 200000\n\n Section to Segment mapping:\n Segment Sections...\n 00 .text\n```\n\nIs this mapping even required to have a working executable? Or can they be omitted completely and you would still have a valid executable?\n\n========================================\n\nTop Answer:\nI did a test according to the @Employed Russian.\n\nreadelf -l ./libandroid_servers.so \n\n```\nElf file type is DYN (Shared object file)\nEntry point 0x0\nThere are 6 program headers, starting at offset 52\n\nProgram Headers:\n Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n PHDR 0x000034 0x00000034 0x00000034 0x000c0 0x000c0 R 0x4\n LOAD 0x000000 0x00000000 0x00000000 0x0f830 0x0f830 R E 0x1000\n LOAD 0x010000 0x00010000 0x00010000 0x00cf4 0x011ac RW 0x1000\n DYNAMIC 0x010540 0x00010540 0x00010540 0x00130 0x00130 RW 0x4\n GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0\n EXIDX 0x00f2e8 0x0000f2e8 0x0000f2e8 0x00548 0x00548 R 0x4\n\n Section to Segment mapping:\n Segment Sections...\n 00 \n 01 .hash .dynsym .dynstr .rel.plt .rel.dyn .plt .text .rodata .ARM.extab .ARM.exidx \n 02 .init_array .fini_array .data.rel.ro .dynamic .got .data .bss \n 03 .dynamic \n 04 \n 05 .ARM.exidx\n```\n\n01 LOAD offset: 0x000000 fileSize 0x0f830\n\n.ARM.exidx section end addr: hex(0x00f2e8 + 0x00548) = 0xf830\n\n02 LOAD offset: 0x010000 fileSize: 0x00cf4\n\n.init_array section begin addr: 10000h\n\n.bss section end addr: hex(0x00f2e8 + 0 ) = 0x10cf4\n\nYou see the `readelf` surely print the sections in a segments by computes. They match well.\n\n========================================\n\nCode:\n```text\nProgram Headers:\n  Type           Offset             VirtAddr           PhysAddr\n                 FileSiz            MemSiz              Flags  Align\n  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000\n                 0x0000000000000084 0x0000000000000084  R E    200000\n\n Section to Segment mapping:\n  Segment Sections...\n   00     .text\n```\n\n```text\nreadelf\n```\n\n```text\nElf file type is DYN (Shared object file)\nEntry point 0x0\nThere are 6 program headers, starting at offset 52\n\nProgram Headers:\n  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\n  PHDR           0x000034 0x00000034 0x00000034 0x000c0 0x000c0 R   0x4\n  LOAD           0x000000 0x00000000 0x00000000 0x0f830 0x0f830 R E 0x1000\n  LOAD           0x010000 0x00010000 0x00010000 0x00cf4 0x011ac RW  0x1000\n  DYNAMIC        0x010540 0x00010540 0x00010540 0x00130 0x00130 RW  0x4\n  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0\n  EXIDX          0x00f2e8 0x0000f2e8 0x0000f2e8 0x00548 0x00548 R   0x4\n\n Section to Segment mapping:\n  Segment Sections...\n   00     \n   01     .hash .dynsym .dynstr .rel.plt .rel.dyn .plt .text .rodata .ARM.extab .ARM.exidx \n   02     .init_array .fini_array .data.rel.ro .dynamic .got .data .bss \n   03     .dynamic \n   04     \n   05     .ARM.exidx\n```\n\n```text\nreadelf\n```\n\n========================================\n\nComments:\n- File offsets, that's it! I thought it had something to do with equal virtual memory addresses. Thanks, it works.\n- Do you mean: if the executable contains the optional section header table, then that table is made to point to the middle of segments that contain the sections is that it?\n- @CiroSantilli巴拿馬文件六四事件法轮功 No, I didn't. The question is about where the \"Section to Segment mapping\" in `readelf` output is stored, and the answer is that it's computed on-demand by `readelf` itself, and is not stored anywhere.\n- Humm, then I don't understand how it can be computed on demand (other than what me previous comment said, which is \"on demand\", but requires the optional section header). I mean, the the option section header is not present, then it can't be determined right?\n- @CiroSantilli巴拿馬文件六四事件法轮功 If the section header is not present, then `readelf` will not produce the \"Section to Sergment mapping\" output.\n- how do you figure out if you have the section headers? is that with readelf -S ?If that gives an output then you have a table of section headers? In the comments it is given as \"The\" optional section header which implies only one of them are present. Do you mean the array of section headers?\n- @Har U can find a sections with `readelf --sections --wide a.out`, note that section headers are optional and may not be present at all.","metadata":{"transformedAt":"2026-08-18T18:32:17.768Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":126,"estimatedTokens":1284}}172{"id":"stack-21410675","source":"stackoverflow","questionId":21410675,"title":"Getting the original variable name for an LLVM Value","tags":["compiler-construction","clang","llvm","debug-symbols","llvm-ir"],"text":"Title: Getting the original variable name for an LLVM Value\nTags: compiler-construction, clang, llvm, debug-symbols, llvm-ir\nSource: Stack Overflow\n\nQuestion:\nThe operands for an `llvm::User` (e.g. instruction) are `llvm::Value`s.\n\nAfter the *mem2reg* pass, variables are in SSA form, and their names as corresponding to the original source code are lost. `Value::getName()` is only set for some things; for most variables, which are intermediaries, its not set.\n\nThe *instnamer* pass can be run to give all the variables names like *tmp1* and *tmp2*, but this doesn't capture where they originally come from. Here's some LLVM IR beside the original C code:\n\nI am building a simple html page to visualise and debug some optimisations I am working on, and I want to show the SSA variables as **namever** notation, rather than just temporary instnamer names. Its just to aid my readability.\n\nI am getting my LLVM IR from clang with a commandline such as:\n\n```\nclang -g3 -O1 -emit-llvm -o test.bc -c test.c\n```\n\nThere are calls to `llvm.dbg.declare` and `llvm.dbg.value` in the IR; how do you turn into the original sourcecode names and SSA version numbers?\n\nSo how can I determine the original variable (or named constant name) from an `llvm::Value`? Debuggers must be able to do this, so how can I?\n\n========================================\n\nTop Answer:\nGiven a `Value`, getting variable name from it can be done by traversing all the `llvm.dbg.declare` and `llvm.dbg.value` calls in the enclosing function, checking if any refers to that value, and if so, return the `DIVariable` associated with the value by that intrinsic call.\n\nSo, the code should look something like (roughly, not tested or even compiled):\n\n```\nconst Function* findEnclosingFunc(const Value* V) {\n if (const Argument* Arg = dyn_cast(V)) {\n return Arg->getParent();\n }\n if (const Instruction* I = dyn_cast(V)) {\n return I->getParent()->getParent();\n }\n return NULL;\n}\n\nconst MDNode* findVar(const Value* V, const Function* F) {\n for (const_inst_iterator Iter = inst_begin(F), End = inst_end(F); Iter != End; ++Iter) {\n const Instruction* I = &*Iter;\n if (const DbgDeclareInst* DbgDeclare = dyn_cast(I)) {\n if (DbgDeclare->getAddress() == V) return DbgDeclare->getVariable();\n } else if (const DbgValueInst* DbgValue = dyn_cast(I)) {\n if (DbgValue->getValue() == V) return DbgValue->getVariable();\n }\n }\n return NULL;\n}\n\nStringRef getOriginalName(const Value* V) {\n // TODO handle globals as well\n\n const Function* F = findEnclosingFunc(V);\n if (!F) return V->getName();\n\n const MDNode* Var = findVar(V, F);\n if (!Var) return \"tmp\";\n\n return DIVariable(Var).getName();\n}\n```\n\nYou can see above I was too lazy to add handling of globals, but it's not that big a deal actually - this requires iterating over all the globals listed under the current compile unit debug info (use `M.getNamedMetadata(\"llvm.dbg.cu\")` to get a list of all the compile units in the current module), then checking which matches your variable (via the `getGlobal` method) and returning its name.\n\n**However**, keep in mind the above will only work for values directly associated with original variables. Any value that is a result of any computation will not be properly named this way; and in particular, values that represent field accesses will not be named with the field name. This is doable but requires more involved processing - you'll have to identify the field number from the GEP, then dig into the type debug information for the struct to get back the field name. Debuggers do that, yes, but no debugger operates in LLVM IR land - as far as I know even LLVM's own LLDB works differently, by parsing the DWARF in the object file into Clang types.\n\n========================================\n\nCode:\n```text\nclang -g3 -O1 -emit-llvm -o test.bc -c test.c\n```\n\n```text\nllvm::User\n```\n\n```text\nllvm::Value\n```\n\n```text\nValue::getName()\n```\n\n```text\nllvm.dbg.declare\n```\n\n```text\nllvm.dbg.value\n```\n\n```text\nllvm::Value\n```\n\n```text\n$ cat  > z.c\nlong fact(long arg, long farg, long bart)\n{\n    long foo = farg + bart;\n    return foo * arg;\n}\n\n$ clang -emit-llvm -O3 -g -c z.c\n$ llvm-dis z.bc -o -\n```\n\n```text\ndefine i64 @fact(i64 %arg, i64 %farg, i64 %bart) #0 {\nentry:\n  tail call void @llvm.dbg.value(metadata !{i64 %arg}, i64 0, metadata !10), !dbg !17\n  tail call void @llvm.dbg.value(metadata !{i64 %farg}, i64 0, metadata !11), !dbg !17\n  tail call void @llvm.dbg.value(metadata !{i64 %bart}, i64 0, metadata !12), !dbg !17\n  %add = add nsw i64 %bart, %farg, !dbg !18\n  tail call void @llvm.dbg.value(metadata !{i64 %add}, i64 0, metadata !13), !dbg !18\n  %mul = mul nsw i64 %add, %arg, !dbg !19\n  ret i64 %mul, !dbg !19\n}\n```\n\n```text\n-O0\n```\n\n```text\n-O3\n```\n\n```text\nllvm.dbg.value\n```\n\n```text\nllvm.dbg.declare\n```\n\n```cpp\nconst Function* findEnclosingFunc(const Value* V) {\n  if (const Argument* Arg = dyn_cast<Argument>(V)) {\n    return Arg->getParent();\n  }\n  if (const Instruction* I = dyn_cast<Instruction>(V)) {\n    return I->getParent()->getParent();\n  }\n  return NULL;\n}\n\nconst MDNode* findVar(const Value* V, const Function* F) {\n  for (const_inst_iterator Iter = inst_begin(F), End = inst_end(F); Iter != End; ++Iter) {\n    const Instruction* I = &*Iter;\n    if (const DbgDeclareInst* DbgDeclare = dyn_cast<DbgDeclareInst>(I)) {\n      if (DbgDeclare->getAddress() == V) return DbgDeclare->getVariable();\n    } else if (const DbgValueInst* DbgValue = dyn_cast<DbgValueInst>(I)) {\n      if (DbgValue->getValue() == V) return DbgValue->getVariable();\n    }\n  }\n  return NULL;\n}\n\nStringRef getOriginalName(const Value* V) {\n  // TODO handle globals as well\n\n  const Function* F = findEnclosingFunc(V);\n  if (!F) return V->getName();\n\n  const MDNode* Var = findVar(V, F);\n  if (!Var) return \"tmp\";\n\n  return DIVariable(Var).getName();\n}\n```\n\n```text\nValue\n```\n\n```text\nllvm.dbg.declare\n```\n\n```text\nllvm.dbg.value\n```\n\n```text\nDIVariable\n```\n\n```text\nM.getNamedMetadata(\"llvm.dbg.cu\")\n```\n\n```text\ngetGlobal\n```\n\n========================================\n\nComments:\n- Which program did you use to create such a nice code assembly | source comparision?\n- @JackL. I quickly wrote it myself. Its just a javascript canvas. When someone earns 500 pts giving human-readable namesver to the Values, I might even release it hint hint ;)\n- @Will Did you end up releasing your comparison tool? It would be tremendously useful to many people, I suspect.\n- @ransford afraid not. And, sadly, I never did get much nearer going from intermediary name to SSAnum either. My project ran into other difficulties such as LLVM not preserving pointers, which I understand has bitten lots of people wanting precise GC and porting to VLIWs etc too :(\n- I think theory and practice diverge :( I've never had any llvm.dbg.value calls emitted by clang. The documentation I had read before I asked on SO.\n- @Will: clang does not emit `llvm.dbg.value`. Optimizations emit them when they place values into registers (instead of the more easily accessible stack slots).\n- Well ok, then how do you get mem2reg or whatever to emit them? And would they be the secret sauce that lets me turn Values into sourcecode names, and if so, how?\n- @Will: if you take a non-trivial C function and emit LLVM IR from it with clang -g (debug info enabled), you'll see debug information in it. llvm.dbg.declare links stack values to original C objects. `mem2reg` may then create some llvm.dbg.value intrinsics; not all LLVM-level values have direct mapping to original C objects, of course - some are just temporaries, ABI-related values, C++ related lowering, etc.\n- Actually, that's pretty much *exactly* what I've been doing. I just don't have any llvm.dbg.values in the emitted bc. I've added more info to my question, and I'm waiting for the bounty period to start in the hope someone actually works out the actual code to go from Value to name/version.\n- `clang -O0` generates unoptimized code, which won't have `llvm.dbg.value` because all the locals are on stack. You need to generate optimized code to see `llvm.dbg.value`\n- let us continue this discussion in chat\n- Very nice and very similar to how I've been finding proper names (although I've been making a reverse map of the Function's symbol table to speed things up slightly). The big problem is in the *however* section, though; almost all variables in IR are temporaries, although as a human you can them back and see where they come from.\n- It looks like it no longer works (debug values had a major rework shortly after this answer and now they don't return Value * among other things) and I'm not sure what is the correct way to do this now.\n- Hi, the links were helpful but I was still unable to achieve this. Could you please elaborate on how you managed to get the original variable names? Thanks","metadata":{"transformedAt":"2026-08-18T18:32:17.768Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":20,"totalLines":219,"estimatedTokens":2196}}173{"id":"stack-338102","source":"stackoverflow","questionId":338102,"title":"Is there a compiler or IDE for C on Windows that's regarded as an industry standard?","tags":["c","windows","ide","compiler-construction"],"text":"Title: Is there a compiler or IDE for C on Windows that's regarded as an industry standard?\nTags: c, windows, ide, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nTaking advice from this post, I purchased a copy of 'The C Programming Language' and am happily reading my way through. \n\nHowever, all the stuff I've written in the past has been interpreted, and I have no idea where to look for a good C compiler or an IDE (is there even one?). Google searches throw up a lot of results for C++ compilers, which I don't think is the same thing? Haha. I was wondering if there is a compiler or IDE for C that's regarded as an industry standard (kinda in the same way that Zend Studio is pretty much *the* IDE for PHP), or at least one that is generally considered to be a good quality product.\n\n========================================\n\nTop Answer:\nI'm surprised no-one's mentioned Pelles C. Great little C IDE for Windows; includes an LCC-based compiler. That said, NetBeans 6.5 has decent support for C and C++, and Code::Blocks is well worth a whirl.\n\n========================================\n\nComments:\n- Which OS do you use for development?\n- Oh man, I am not going anywhere near Vi again :P\n- I guess emacs it is then (although I think that vi is easier to learn). Trust me, it's faster to edit a txt file in vi than than almost any IDE.\n- Careful C is NOT a subset of C++ (there are some things in C that won't work on a C++ compiler). So if you want to do true C, you can't use a C++ compiler.\n- C and C++ are in certain sense similar, but there are important and fundamental differences you have to know or you will run into trouble.\n- I didn't say any of those things. I said most C++ compilers will compile C, which is true, even if they use things in C that don't work in C++.\n- This doc has some good discussions on the differences between C and C++, research.att.com/~bs/3rd_compat.pdf\n- Just as an aside, thealliedhacker isN't saying that C and C++ are the same or subsets or whatever. For instance, the Visual Studio C++ kit comes with a C compiler as well. If you write a C source file, it will happily compile C-only things.\n- CodeSlave: No one said anything about using a C++ compiler. MSVC++ contains both C and C++ compilers.\n- Every C++ compiler I've ever come across can also compile C - in fact, by default they'll use C language rules if the source file has a .c extension. However, the reverse obviously is not true - there are plenty of C compilers that don't know squat about C++.\n- I agree that there are compilers that can compile both c and c++ (gcc). It's safe so long as the compiler is smart enough to behave appropriately for each kind of source file. My caution comes from seeing c text books with wrong examples where the author (wrongly) used a c++ compiler to test them.\n- C is not a subset of C++. C++ is not a superset of C. Visual Studio's C compiler is rubbish and does not support C99.\n- See my command to thealliedhacker about C not being a subset of C++\n- The VC language extensions can be disabled in project settings, leaving you with a pretty compliant compiler.\n- @CodeSlave: The Watcom ide supports plain C\n- use extern C {} in Visual Studio for standard C linkage. -- msdn.microsoft.com/en-us/library/0603949d.aspx\n- Visual C++'s C compiler does not support C99, let alone C11.\n- Isn't gcc a C compiler? I think you were refering to g++\n- From the GCC manual: The abbreviation GCC has multiple meanings in common use. The current official meaning is “GNU Compiler Collection”, which refers generically to the complete suite of tools. The name historically stood for “GNU C Compiler”...\n- Dunno, at least for me, it's easier to learn a language form the language specification, after going through a very very quick bootstrapping tutorial, but many \"dry\" specs include such a basic tutorial in the first chapter.\n- I've got the book and learned from it many years ago. It's a reasonable language definition, but is a bare minimum. Pretty much the definition of \"Reference\" instead of \"Training\". I'm shocked that anyone would dispute this. ANY book you bought on c would be better for learning than the white book. I guess some people have to defend spending $60 on what is essentially a pamphlet.\n- I don't know about that. My friend and I both had to learn C for an assignment.. He used one of the big Deitel&Deitel tomes, I was using the \"white book\". To be sure, the Deitel book hand-holds you more, but it's faster to read through and to use as a reference. Personally, although I don't like C too much, the book was a fine one.\n- It's the same! It hasn't change in years","metadata":{"transformedAt":"2026-08-18T18:32:17.768Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":40,"estimatedTokens":1158}}174{"id":"stack-5444303","source":"stackoverflow","questionId":5444303,"title":"Loop unrolling vs Loop tiling","tags":["c++","optimization","compiler-construction","nasm"],"text":"Title: Loop unrolling vs Loop tiling\nTags: c++, optimization, compiler-construction, nasm\nSource: Stack Overflow\n\nQuestion:\nCan someone please tell if the 2 optimization techniques are same or different? \n\nAlso, is it responsibility of programmer or compiler to do it?\n\n========================================\n\nTop Answer:\nThese are two totally different performance optimisations.\n\n**Loop unrolling** is a *code* optimisation where code is replicated within a loop and the total number of loop iterations is reduced. The benefit is reduced loop overhead (normally only relevant for very small loops), and better instruction scheduling with reduced dependency stalls in superscalar CPUs. This can be done both manually and/or as a compiler optimisation.\n\n**Tiling** is a *memory* optimisation which aims to make better use of cache by processing *tiles* (small blocks within a larger data structure), typically in the context of an image or other 2D data structure. This is normally implemented at the source code level, as part of the overall design of an algorithm implementation.\n\n========================================\n\nComments:\n- Loop unrolling is a *speed* optimization. Both optimizations (about all, really) are applied to the code.\n- @delnan: they are both *performance* (i.e. speed) optimisations, but loop unrolling achieves this through increased code efficiency, whereas tiling achieves performance improvement through better use of the cache/memory hierarchy.\n- These types of optimization can be critical for performance. I would love to see a performant matmul without them. I agree it can increase complexity and therefore potential bugs, but even those can be managed with good practices. Tiling and unrolling are optimizations that all programmer dealing with heavy math should be familiar with.\n- Unrolling of small loops is critical even with a large or unknown number of iterations. Compilers can unroll e.g. 8 times and add code to deal with the last 0-7 iterations. (The can. You have to check if they actually did.)","metadata":{"transformedAt":"2026-08-18T18:32:17.768Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":511}}175{"id":"stack-15970486","source":"stackoverflow","questionId":15970486,"title":"Are the Optimization Keywords in C and C++ Reasonable?","tags":["c++","c","performance","optimization","compiler-construction"],"text":"Title: Are the Optimization Keywords in C and C++ Reasonable?\nTags: c++, c, performance, optimization, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nSo we've all heard the don't-use-`register` line, the reasoning being that trying to out-optimize a compiler is a fool's errand.\n\n`register`, from what I know, doesn't actually state anything about CPU registers, just that a given variable can't be referenced indirectly. I'll hazard a guess that it's often referred to as obsolete because compilers can detect a lack of addressing automatically thus making such optimizations transparent.\n\nBut if we're firm on that argument, can't it be levelled at every optimization-driven keyword in C? Why do we use `inline` and C99's `restrict` for example?\n\nI suppose that some things like aliasing make deducing some optimizations **hard** or even impossible, so where is the line drawn before we start venturing into Sufficiently Smart Compiler territory?\n\nWhere should the line should be drawn in C and C++ between spoon-feeding a compiler optimization information and assuming it knows what it's doing?\n\n**EDIT**: Jens Gustedt pointed out that my conflating of C and C++ isn't right since two of the keywords have semantic differences and one doesn't exist in standard C++. I had a good link about `register` in C++ which I'll add if I find it...\n\n========================================\n\nTop Answer:\n`inline` is used in the scenario where you implement a non-templated function within the header then include it from multiple compilation units.\n\nThis ensures that the compiler should create just one instance of the function as though it were inlined, so you do not get a link error for multiply defined symbol. It does not however require the compiler to actually inline it.\n\nThere are GNU flags I think force-inline or similar but that is a language extension.\n\n========================================\n\nCode:\n```text\nregister\n```\n\n```text\nregister\n```\n\n```text\ninline\n```\n\n```text\nrestrict\n```\n\n```text\nregister\n```\n\n```text\nregister\n```\n\n```text\ninline\n```\n\n```text\ninline\n```\n\n```text\nrestrict\n```\n\n```text\ninline\n```\n\n```text\nregister\n```\n\n```text\nsin(x) * sin(x) + cos(x) * cos(x)\n```\n\n```text\n1\n```\n\n```text\nregister\n```\n\n```text\nregister\n```\n\n```text\nregister\n```\n\n```text\ninline\n```\n\n```text\nrestrict\n```\n\n```text\nregister\n```\n\n```text\ninline\n```\n\n```text\nrestrict\n```\n\n```text\ninline\n```\n\n```text\nregister\n```\n\n```text\ninline\n```\n\n```text\nregister\n```\n\n```text\nregister\n```\n\n```text\ninline\n```\n\n```text\ninline\n```\n\n```text\ninline\n```\n\n```text\ngcc\n```\n\n```text\ncicc\n```\n\n```text\nregister\n```\n\n```text\ninline\n```\n\n```text\n#pragma unroll\n```\n\n```text\nvoid my_memcpy(void *dst, const void *src, size_t size) {\n    for (size_t i = 0; i < size; i++) {\n        ((char *)dst)[i] = ((const char *)str)[i];\n    }\n}\n```\n\n```text\nregister\n```\n\n```text\ninline\n```\n\n```text\nrestrict\n```\n\n```text\nmemcpy\n```\n\n```text\ndst\n```\n\n```text\nsrc\n```\n\n```text\nsize\n```\n\n```text\nrestrict\n```\n\n```text\nrestrict\n```\n\n========================================\n\nComments:\n- I don't know much about C, but in C++, `inline` is rather used to circumvent the ODR.\n- \"Where should the line should be drawn\" - I think it should always be benchmarked disassembled. Choose the one which produces the better result.\n- You really should distinguish C and C++, here, they are not the same with all three keywords that you mention: `inline` and `register` have different semantics and `restrict` doesn't even exist in C++.\n- @JensGustedt Thanks, I'll add it as an edit.\n- The fact `register` was deprecated in C++11 is fairly telling. There's no benefit with modern optimizing compilers. GCC however, uses it in a non-standard extension\n- i would argue that `register` and `restrict` are similar in that they both tell the compiler what you allow it to assume. `register` is better in that it also allows the compiler to enforce. but of the same kind. oh, wait a minute... this applies also to `inline`.\n- Compilers today can normally see all of the call sites of a function, at least if you use the maximum optimization algorithms. It's true, however, that the locality affected by `register` is much smaller, and more importantly, that the algorithms for optimizing register allocation are now well known and univerally used, which is not the case for the optimizations related to `inline` or `restrict`.\n- @JamesKanze: Out of interest, what are the mechanics of the compiler being able to see all the call sites? Say I build a library containing a function named `foo()`, and give the `.a` file to you. You write a function called `bar()` which invokes `foo()`. How can the compiler know anything about `bar()` when compiling `foo()`?\n- @NPE The usual solution, I think, is for the compiler to generate annotated byte code, similar to what a Java compiler generates, and then only compile it to machine code in the link phase. See `-lto` for g++, `&#47;GL` for VC++.\n- @NPE can the compiler assume `restrict` semantics on unique_ptr?\n- @JamesKanze: `-lto` or `&#47;GL` allow to optimize on compilation unit granularity - not true whole program. So yes - the static libraries would be considered IF they were compiled with appropriate flags but dynamic libraries are also possible (plugins, shared libraries etc.) in which case even if they would be compiled in such way they might be replaced in a future with say a bugfix.\n- @MaciejPiechotka That's not what the documentation for them says. Obviously, you can't optimize across DLL boundaries, but then, why are you using DLLs? There are very few cases where a DLL is justified.\n- @JamesKanze: I was saying about DLL. I'm using Linux when the situation is reversed - the shared libraries installed globally are the default. And there is a few reason - for example pushing DLL with fixed bug is small update done in one place. If the libraries are distributed with program then you need to update all of them (update every program). If you link statically then you need to recompile every program or it will not be patched. They save space (you need only one copy of say zlib, gtk+ or qt per system) which is not so important for disk but allows more efficient caching in memory.\n- In addition on Windows the DLLs pose problems for C++ - as the MSVC have no guarantee of ABI either you need to be extra careful about methods you are using (no STL etc.) or you need to use the same compiler for all libraries. I misspoke about compilation unit - I meant assembly (dynamic library or program).\n- @MaciejPiechotka For system functions, shared libraries should always be the default. But system functions aren't subject to optimization anyway. For anything that is not system or pseudo-system (e.g. something like a database), you should avoid shared libraries. For anything else, shared libraries are simply a way of ensuring that your clients run versions you haven't tested, which might not work with your software.\n- @MaciejPiechotka Regarding your last comment: I agree. For a suitable definition of assembly. But that is, or should be, more or less irrelevant. Different assemblies only interface at higher levels; the performance issues are in the individual assemblies.\n- @JamesKanze: If change of version breaks software than there is bug somewhere - on the other hand on Linux there is central place (distribution) which goal is to check if everything is working. I'm afraid that we have to agree to disagree (different philosophies regarding the management of libraries). I disagree about the optimization of system libraries - `memcpy` etc. are usually highly optimized routines (often manually) so there is point in using them. And if pointer *do* come across boundary then even if performance issues are in assembly you won't be able to perform alias analysis.\n- `inline` *can* be used to make a library header-only. Being header-only is a serious disadvantage in general, however; it allows people to use your library on machines you've never heard of, much less tested it on.\n- The gnu way of forcing inlining is actually the function attribute \"always_inline\".\n- When you started programming in C, possibly you were writing code very close to the system and were writing in a single-process environment where your program was the only thing running on your DOS PC.\n- +1 but i think what we need are types with added semantic restrictions in order to express \"i want standard functionality, don't optimize if that changes the semantics\". mostly for g++ though. a special floating point type that g++ can't foul up, and where the numeric limits info is always reliable, and special integer types that it can't foul (on assumption of no wrapping) no matter `-fwrapv` or not.\n- @CashCow When I started programming in C, the compiler only had 64 KB of RAM to play around in, and the disk it would spill to was a floppy. Optimization strategies which take maybe 10 ms today could take minutes. And not all of them were known: Sethi-Ullman was state of the art, and register coloring had just been published.\n- Regarding the `register` keyword, it depends on the compiler. GCC will honor it if you don't turn on any optimizations - which can in fact make the code much faster.\n- As side note `sin(x) * sin(x) + cos(x) * cos(x)` CANNOT be optimized into `1` as while identity holds for real numbers it is not the case for IEEE 754 floating point number. For example `sin(nan) * sin(nan) + cos(nan) * cos(nan) = nan`(similarly for `inf` result is `nan`). Similarly the original formula might have rounding errors (for 32 bit float the result of `(sin(x) * sin(x) + cos(x) * cos(x)) - 1` is `-5.9604645e-8`).\n- Measure first before you modify (and again after) is a given. Still, with most current compilers, judicious use of `inline` can make significant improvement (once you've determined that the improvement is needed), with very little effort. And I've seen a couple of cases where `restrict` could have brought an order of magnitude improvement in a small, critical function (which could take up to 20 minutes to execute).\n- With regards to your last paragraph---there does seem to be a tendancy to abuse `inline`. Premature optimization is premature optimization, and the original version of a program should not contain any `inline` functions. But that doesn't mean that when optimization is necessary, `inline` is a bad tool. It's one of the cheapest tools (in terms of human effort and cost to code readability) you can use. The key is to only use it when necessary.\n- `inline` semantics are useful in C99, since an implementation of the function can be instantiated in a translation unit. If the compiler decides *not* to inline the code, it doesn't have to implement statically scoped implementations in multiple translation units. This is useful for function pointers too.\n- Isn't the point of inline that it really does not force the compiler to do anything? It is merely a hint, unlike what register was, and so, just like the register keyword, may become useless. Regarding the point about size, try using -Osize, rather than just removing all the inline keywords","metadata":{"transformedAt":"2026-08-18T18:32:17.768Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":44,"totalLines":240,"estimatedTokens":2778}}176{"id":"stack-916897","source":"stackoverflow","questionId":916897,"title":"In terms of programming, what do semantics mean?","tags":["compiler-construction","theory"],"text":"Title: In terms of programming, what do semantics mean?\nTags: compiler-construction, theory\nSource: Stack Overflow\n\nQuestion:\nThis is a sentence from Eric Lippert's blog:\n\n Given that unfortunate situation, it\n makes sense to emphasize the storage\n mechanism first, and then the\n **semantics** second.\n\nIt's easy to get a dictionary definition of what \"semantic\" means but what does it mean in terms of computer jargon?\n\n========================================\n\nTop Answer:\nThe dictionary definition applies.\n\nSemantics is (are?) all about the meaning of words. \n\nFor example, if you use the .NET type KeyValuePair to represent something OTHER than a key and value, you've got a semantic problem. It may work... it may be the best solution, but it isn't semantically accurate. \n\nThis is exactly what he's talking about in that blog post. We list return types first in C-like languages, but really the return comes last. That's semantically inaccurate.\n\n========================================\n\nCode:\n```text\nx = 5;\n```\n\n```text\nx := 5;\n```\n\n```text\nx <- 5\n```\n\n```text\nIf x = 5 Then Console.WriteLine(\"x is 5\")\n```\n\n```text\nx\n```\n\n```text\nstatic int customerCount\n```\n\n```text\ndim shared customerCount as Integer\n```\n\n```text\nC#\n```\n\n```text\nC#\n```\n\n```text\ncustomerCount\n```\n\n```text\ncustomerCount\n```\n\n========================================\n\nComments:\n- This question appears to be off-topic because it is about general programming, not a programming problem. Best fit for programmers.stackexchange.\n- This may be a meaning of \"semantic\" but I don't see how it fits the context (or common usage for that matter) at all.\n- Not sure if this answer relates to the blog article that Richard posted (haven't read it), but if this answer relates to the question of this thread \"In terms of programming, what do semantics mean?\" then I'd say that your answer is concise and satisfying.\n- And may I add that in VHDL, *signal assignment* (*with* `<=`) is different from *variable assignment* (*with* `:=`) in that while a signal assignment `a <= b` may only be executed once, it says that `b` is now the *driver* of signal `a`; Thus if the driving signal `b` changes (by some other assignment in the future), then so will the driven signal `a`, without any need for \"propagating\" the assignment. In effect you are wiring `a` to `b` as opposed to setting it to the current value and letting them change independently after the assignment. This is another potential semantic of assignment.\n- That's how I interpret the meaning of semantics in programming context too.","metadata":{"transformedAt":"2026-08-18T18:32:17.768Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":80,"estimatedTokens":641}}177{"id":"stack-19664993","source":"stackoverflow","questionId":19664993,"title":"Is it possible to implement Common Lisp's macro system in scheme?","tags":["compiler-construction","macros","scheme","common-lisp"],"text":"Title: Is it possible to implement Common Lisp's macro system in scheme?\nTags: compiler-construction, macros, scheme, common-lisp\nSource: Stack Overflow\n\nQuestion:\nHopefully this is not a redundant question. \n\nAs a newcomer to scheme I am aware that `syntax-case` macros are more powerful than the `syntax-rules` alternative, at the cost of unwanted complexity. \n\nIs it possible, however, to implement Common Lisp's macro system in scheme, which is more powerful than `syntax-rules`, using `syntax-case`?\n\n========================================\n\nTop Answer:\nHere's Guile's implementation of `define-macro`. Note that it's implemented entirely with `syntax-case`:\n\n```\n(define-syntax define-macro\n (lambda (x)\n \"Define a defmacro.\"\n (syntax-case x ()\n ((_ (macro . args) doc body1 body ...)\n (string? (syntax->datum #'doc))\n #'(define-macro macro doc (lambda args body1 body ...)))\n ((_ (macro . args) body ...)\n #'(define-macro macro #f (lambda args body ...)))\n ((_ macro transformer)\n #'(define-macro macro #f transformer))\n ((_ macro doc transformer)\n (or (string? (syntax->datum #'doc))\n (not (syntax->datum #'doc)))\n #'(define-syntax macro\n (lambda (y)\n doc\n #((macro-type . defmacro)\n (defmacro-args args))\n (syntax-case y ()\n ((_ . args)\n (let ((v (syntax->datum #'args)))\n (datum->syntax y (apply transformer v)))))))))))\n```\n\nGuile has special support for Common Lisp-style docstrings, so if your Scheme implementation doesn't use docstrings, your `define-macro` implementation could be even simpler:\n\n```\n(define-syntax define-macro\n (lambda (x)\n (syntax-case x ()\n ((_ (macro . args) body ...)\n #'(define-macro macro (lambda args body ...)))\n ((_ macro transformer)\n #'(define-syntax macro\n (lambda (y)\n (syntax-case y ()\n ((_ . args)\n (let ((v (syntax->datum #'args)))\n (datum->syntax y (apply transformer v)))))))))))\n```\n\n========================================\n\nCode:\n```text\nsyntax-case\n```\n\n```text\nsyntax-rules\n```\n\n```text\nsyntax-rules\n```\n\n```text\nsyntax-case\n```\n\n```text\ndefmacro\n```\n\n```text\ndefmacro\n```\n\n```text\nsyntax-case\n```\n\n```text\nsyntax-case\n```\n\n```text\ndefmacro\n```\n\n```text\nlambda\n```\n\n```text\nsyntax-rules\n```\n\n```text\nsyntax-rules\n```\n\n```text\nsyntax-rules\n```\n\n```text\nsyntax-rules\n```\n\n```text\nsyntax-rules\n```\n\n```text\nsyntax-rules\n```\n\n```text\nsyntax-case\n```\n\n```text\nsyntax-rules\n```\n\n```text\ndefmacro\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\ndefmacro\n```\n\n```text\ngensym\n```\n\n```text\n&environment\n```\n\n```text\ndefmacro\n```\n\n```text\ndefmacro\n```\n\n```text\ngensym\n```\n\n```text\nsyntax-rules\n```\n\n```text\nsyntax-case\n```\n\n```text\nx\n```\n\n```text\nsyntax-e\n```\n\n```text\nsyntax->datum\n```\n\n```text\ndatum->syntax\n```\n\n```text\nthis\n```\n\n```text\nsyntax->datum\n```\n\n```text\ndefmacro\n```\n\n```text\nsyntax->datum\n```\n\n```text\ndefmacro\n```\n\n```text\ndefmacro\n```\n\n```text\nsyntax-case\n```\n\n```text\ndefmacro\n```\n\n```text\nfluid-let-syntax\n```\n\n```text\nsyntax-rules\n```\n\n```text\ndefmacro\n```\n\n```text\ndefmacro\n```\n\n```text\nsyntax-rules\n```\n\n```text\n%%__names__\n```\n\n```text\n(define-syntax define-macro\n  (lambda (x)\n    \"Define a defmacro.\"\n    (syntax-case x ()\n      ((_ (macro . args) doc body1 body ...)\n       (string? (syntax->datum #'doc))\n       #'(define-macro macro doc (lambda args body1 body ...)))\n      ((_ (macro . args) body ...)\n       #'(define-macro macro #f (lambda args body ...)))\n      ((_ macro transformer)\n       #'(define-macro macro #f transformer))\n      ((_ macro doc transformer)\n       (or (string? (syntax->datum #'doc))\n           (not (syntax->datum #'doc)))\n       #'(define-syntax macro\n           (lambda (y)\n             doc\n             #((macro-type . defmacro)\n               (defmacro-args args))\n             (syntax-case y ()\n               ((_ . args)\n                (let ((v (syntax->datum #'args)))\n                  (datum->syntax y (apply transformer v)))))))))))\n```\n\n```text\n(define-syntax define-macro\n  (lambda (x)\n    (syntax-case x ()\n      ((_ (macro . args) body ...)\n       #'(define-macro macro (lambda args body ...)))\n      ((_ macro transformer)\n       #'(define-syntax macro\n           (lambda (y)\n             (syntax-case y ()\n               ((_ . args)\n                (let ((v (syntax->datum #'args)))\n                  (datum->syntax y (apply transformer v)))))))))))\n```\n\n```text\ndefine-macro\n```\n\n```text\nsyntax-case\n```\n\n```text\ndefine-macro\n```\n\n```text\n(define-syntax (define-macro x)\n  (syntax-case x ()\n    ((_ (name . args) . body)\n      (syntax (define-macro name (lambda args . body))))\n    ((_ name transformer)\n      (syntax\n       (define-syntax (name y)\n         (syntax-case y ()\n           ((_ . args)\n             (datum->syntax-object\n               (syntax _)\n               (apply transformer\n                 (syntax-object->datum (syntax args)))))))))))\n\n(define-macro (when test . body) `(cond (,test . ,body)))\n\n(define-macro (aif test-form then-else-forms)\n  `(let ((it ,test-form))\n     (if it ,then-else-forms)))\n\n(define-macro (awhen pred? . body)\n  `(aif ,pred? (begin ,@body)))\n```\n\n```text\ndefine-macro\n```\n\n========================================\n\nComments:\n- The short answer is \"yes\". A quick Google search turns up Scheme (and Scheme-like language) implementations that support Common Lisp style macros. E.g., Racket's legacy macro support has an implementation of `defmacro`.\n- That said, this doesn't appear to be a specific programming problem; answers to it seem like they'll be enumerations of implementations, and those kind of list questions are typically off-topic for Stack Overflow.\n- You can't really call it 'unwanted complexity' if you need `defmacro` implemented - then it is 'required functionality'\n- Since when does one \"need\" `defmacro` in Scheme? `syntax-case` is strictly more powerful than `defmacro`.\n- @Joshua-taylor: His question is if it's possible to make defmacro by using syntax-case. I think it's a fair question.\n- @Sylwester I missed the \"using `syntax-case`\" at the end. Thanks for pointing that out. It still probably isn't specific enough (e.g., \"what has Bracket tried so far?\"), but the bit about \"using `syntax-case`\" is good; I retract my close vote.\n- Why on earth would a standard prelude provide unhygienic macro support? That seems to go against what Scheme is about.\n- Because it is sometimes useful.\n- @ChrisJester-Young, hygienic macros are nearly useless - you can't do anything besides trivial syntax sugar with them. Proper, powerful metaprogramming is only possible with non-hygienic macros.\n- @SK-logic Perhaps, but in the Scheme world, we use `syntax-case` (or perhaps explicit renaming) for macros with controlled unhygiene, not `defmacro`, which is uncontrolled unhygiene. I frequently use `syntax-case`, but I would absolutely 100% avoid `defmacro`.\n- @user448810 As I just said to SK-logic, `syntax-case` is sometimes useful. `define-macro` is only good for compatibility with CL-style macros. But it's seriously deprecated for new Scheme code.\n- @ChrisJester-Young, in the Scheme world people know close to nothing about metaprogramming, so no surprise you're avoiding the most powerful and flexible tool.\n- @SK-logic Given that you can implement `defmacro` using `syntax-case`, clearly it's powerful enough to break hygiene wherever you want to. I am saying that you do *not* need 100% uncontrolled unhygiene in order to have metaprogramming.\n- @ChrisJester-Young, likewise, you can use defmacro to define `syntax-case`. But, given that in practice hygiene can be useful only in the trivial, marginal cases, there is little point in bothering implementing a hygienic macro expander. `defmacro`-style macros should be used most of the time, therefore, the simpler macro system should be fundamental, and hygienic must be an add-on on top, not the other way around.\n- @ChrisJester-Young: You have made my point: define-macro is good for maintaining compatibility with CL-style macros. Therefore, as I said, it is sometimes useful. And thus I keep it in my Standard Prelude. I also use define-macro because such definitions are sometimes easier to read than the equivalent hygienic macro. And my Standard Prelude also has gensym, because that is sometimes useful in conjunction with define-macro. They purity of hygienic macros is also sometimes useful, and I use them more often than define-macro. But not exclusively. It's good to have more than one way to do things.\n- `defmacro` has both benefits and disadvantages. So has `syntax-rules` and `syntax-case` too. Whats better is a matter of opinion so the discussion, sadly, becomes a peeing contest.\n- Thanks! This worked for me! Very useful, coming from a Clojure background.\n- Hi, this doesn't seem to work with r6rs libraries. In the namespace where the macro is defined, if I import some symbols from a library with a prefix, the macro still seems to only recognize them if the symbols are not prefixed. Also, it doesn't seem to recognize other symbols defined in the context where it is being expanded.\n- In Guile, some of us are in the process of converting legacy `define-macro` macros to `syntax-rules` and `syntax-case`, and yes, it's absolutely great. For example, hygienic macros could call package-private (unexported) functions the simple, obvious way---which would have been much more burdensome to do in a `defmacro`-style macro. Oh, and by the way, I took on board your advice to use syntax parameters: git.savannah.gnu.org/cgit/guile.git/commit/&hellip;\n- By the way, this whole `defmacro` purge started when ijp made the observation that hygienic macros and `defmacro`s don't mix, and stis was like, what, we still have `defmacro`s in Guile?! Granted, we started \"N years\" later than Racket, but I'm glad it's actually being done now.","metadata":{"transformedAt":"2026-08-18T18:32:17.768Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":55,"totalLines":359,"estimatedTokens":2427}}178{"id":"stack-603687","source":"stackoverflow","questionId":603687,"title":"How do I generate sentences from a formal grammar?","tags":["compiler-construction","computer-science","grammar","parsing"],"text":"Title: How do I generate sentences from a formal grammar?\nTags: compiler-construction, computer-science, grammar, parsing\nSource: Stack Overflow\n\nQuestion:\n**What's a common way of generating sentences from a grammar?**\n\nI want an algorithm that's sort of the opposite of a parser. That is, given a formal context-free grammar (say LL), I want to generate an arbitrary sentence that conforms to that grammar. I use *sentence* here to mean any valid body of text, so it can actually be a whole program (even if it doesn't make any sense—as long as it's syntactially correct). \n\nExample grammar:\n\n```\nprogram : NEWLINE? \nimports : (\"import\" NEWLINE)* \nnamespace : \"namespace \" NEWLINE \"{\" \"}\" \nidentifier: (A-Za-z_) (A-Za-z0-9_)*\n...\n```\n\nExample generated *program*:\n\n```\nimport jkhbhhuob\nimport aaaaa888_\n\nnamespace u8nFGubgykb\n{ class ui0op_np { ... }\n}\n```\n\n========================================\n\nTop Answer:\nHere is a Python example using the NLTK:\n\n```\nfrom nltk import CFG, ChartParser\nfrom random import choice\n\ndef produce(grammar, symbol):\n words = []\n productions = grammar.productions(lhs = symbol)\n production = choice(productions)\n for sym in production.rhs():\n if isinstance(sym, str):\n words.append(sym)\n else:\n words.extend(produce(grammar, sym))\n return words\n\ngrammar = CFG.fromstring('''\nS -> NP VP\nPP -> P NP\nNP -> Det N | Det N PP | 'I'\nVP -> V NP | VP PP\nV -> 'shot' | 'killed' | 'wounded'\nDet -> 'an' | 'my' \nN -> 'elephant' | 'pajamas' | 'cat' | 'dog'\nP -> 'in' | 'outside'\n''')\n\nparser = ChartParser(grammar)\n\ngr = parser.grammar()\nprint(' '.join(produce(gr, gr.start())))\n```\n\nThe example is adapted from the book. The sentences generated are syntactically correct but still total gibberish.\n\n========================================\n\nCode:\n```text\nprogram   : <imports> NEWLINE? <namespace>\nimports   : (\"import\" <identifier> NEWLINE)* \nnamespace : \"namespace \" <identifier> NEWLINE \"{\" <classes> \"}\" \nidentifier: (A-Za-z_) (A-Za-z0-9_)*\n...\n```\n\n```text\nimport jkhbhhuob\nimport aaaaa888_\n\nnamespace u8nFGubgykb\n{ class ui0op_np { ... }\n}\n```\n\n```text\nvoid GenerateRule(someRule)\n{\n  foreach (part in someRule.Parts)\n  {\n    if (part.IsLiteral) OutputLiteral(part);\n    if (part.IsIdentifier) Output(GenerateIdentifier(part)));\n    if (part.IsRule) GenerateRule(part.Rule);\n  }\n}\n```\n\n```text\nnonterm:  otherstuff <nonterm>?\n```\n\n```text\n(...)\n```\n\n```text\n?\n```\n\n```text\n\"...\"\n```\n\n```text\n(name, string)\n```\n\n```py\nfrom nltk import CFG, ChartParser\nfrom random import choice\n\ndef produce(grammar, symbol):\n        words = []\n        productions = grammar.productions(lhs = symbol)\n        production = choice(productions)\n        for sym in production.rhs():\n            if isinstance(sym, str):\n                words.append(sym)\n            else:\n                words.extend(produce(grammar, sym))\n        return words\n\ngrammar = CFG.fromstring('''\nS -> NP VP\nPP -> P NP\nNP -> Det N | Det N PP | 'I'\nVP -> V NP | VP PP\nV -> 'shot' | 'killed' | 'wounded'\nDet -> 'an' | 'my' \nN -> 'elephant' | 'pajamas' | 'cat' | 'dog'\nP -> 'in' | 'outside'\n''')\n\nparser = ChartParser(grammar)\n\ngr = parser.grammar()\nprint(' '.join(produce(gr, gr.start())))\n```\n\n========================================\n\nComments:\n- See stackoverflow.com/a/41434860/120163\n- That's not what he's trying to do\n- Beardo's right—I want the other way around, i.e., to generate a string of text froma grammar.\n- For a lot of applications, a combination of hand-generated sequences and grammar-generated sequences hits the sweet spot. One application I was thinking of is to describe any kind of state machine and then generate a sequence of state transitions.\n- Yup. That's why I suggested drawing the number of repeats from a converging function. It also why my initial suggestion (50/50) for optionals is wrong. Gotta go fix that now...\n- for recent versions of nltk, use `import CFG` instead of `import parse_cfg` and then use `CFG.fromstring(...)` instead of `parse_cfg(...)`.\n- Addition elifs would be needed to account for all possible \"leaves\" as we can't expect them to all be literals. But I assume there's libraries in python to generate a random string compliant to a given regex. I don't know what else nltk offers, like ? or * but you'd want an elif for each.","metadata":{"transformedAt":"2026-08-18T18:32:17.769Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":163,"estimatedTokens":1067}}179{"id":"stack-475223","source":"stackoverflow","questionId":475223,"title":"What is the difference between implementing a compiler and an interpreter?","tags":["compiler-construction","interpreter"],"text":"Title: What is the difference between implementing a compiler and an interpreter?\nTags: compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nI've read the whole Dragon Book recently (just for fun, I'm not really planning to implement an actual compiler), and I was left with this big question dangling in my head.\n\nWhat is different between implementing a compiler and an interpreter?\n\nTo me a compiler is made up of:\n\n- Lexer\n\n- Parser (which builds the syntax tree)\n\n- Generate Intermediate code (like 3 address code)\n\n- Do all these crazy things to optimize if you want :-)\n\n- Generate \"assembly\" or \"native code\" from the 3 address code.\n\nNow, obviously, the interpreter also has the same lexer and parser as the compiler.\n\nBut what does it do after that?\n\nDoes it \"read\" the syntax tree and execute it directly? (kind of like having an instruction pointer pointing to the current node in the tree, and the execution is one big tree traversal plus the memory management for the call stack) (and if so, how does it do it? I'm hoping the execution is better than a huge switch statement that checks what type of node it is)\n\nDoes it generate 3 address code and interpret that? (if so, how does it do it? Again, I'm looking for something more elegant than a mile long switch statement)\n\n- Does it generate real native code, load it into memory, and make it run? (at which point I'm guessing it's not an interpreter anymore, but more like a JIT compiler)\n\nAlso, at which point does the concept of \"virtual machine\" cut in? What do you use a virtual machine for in a language? (to be clear about my level of ignorance, to me a virtual machine is VMWare, I have no idea how the concept of VM applies to programming languages / executing programs). \n\nAs you can see, my question is quite broad. I'm mostly looking for not only which method is used but mostly to first understand the big concepts, and then get into how it works in detail. I want the ugly, raw details. Obviously, this is more a quest for references to things to read rather than expecting you to answer all these details in here.\n\nThanks!\n\nDaniel\n\nEDIT: Thank you for your answers so far. I realized my title was misleading though. I understand the \"functional\" difference between a compiler and an interpreter.\n\nWhat i'm looking for is the difference as to how you *implement* an interpreter, vs a compiler.\n\nI understand now how a compiler is implemented, the question is how an interpreter differs from that.\n\nFor example: VB6 is clearly both a compiler and an interpreter. I understand now the compiler part. However, I can not grasp how, when running inside the IDE, it could let me stop the program at any arbitrary point, change the code, and resume execution with the new code.\nThat's just one tiny example, it's not the answer i'm looking for. What i'm trying to understand, as I explain below, is what happens after I have a parse tree. A compiler will generate new code from it in the \"target\" language. What does an interpreter do?\n\nThank you for your help!\n\n========================================\n\nTop Answer:\nA compiler is a program that translates a program in one programming language to a program in another programming language. That's it - plain and simple.\n\nAn interpreter translates a programming language into its semantic meaning.\n\nAn x86 chip is an interpreter for x86 machine language.\n\nJavac is a compiler for java to the java virtual machine. java, the executable application, is an interpreter for the jvm.\n\nSome interpreters some elements of compilation in that they may translate one language into another internal language that is easier to interpret.\n\nInterpreters usually, but not always, feature a read-eval-print loop.\n\n========================================\n\nCode:\n```text\ncase '+':\n    symtbl[var3] = symtbl[var1] + symtbl[var2];\n    break;\n```\n\n```text\ncase '+':\n    printf(\"%s = %s + %s;\",symtbl[var3],symtbl[var1],symtbl[var2]);\n    break;\n```\n\n========================================\n\nComments:\n- Might I suggest a different question - how do I implement an interpreter? Edit and continue can be either an interpretation or compilation feature.\n- I'll +1 you but there are many complexities. Eg, the python interpreter has a build step (compiles py to pyc when needed). In addition, some CPUs have microcode as their \"real\" language choosing to emulate the \"visible\" CPU on top of that).\n- An interpreter doesn't transform the input to \"a form that must be executed on a piece of software\" -- it may or may not transform the input internally, but the main point is that it ends up *executing* the code! Right there and then!\n- Most people would view PHP as an interpreter but I assure you it compiles plaintext source to an intermediate form. After all, that's how PHP accelerators (opcode caches) like APC work.\n- Can you provide more detail as to what you mean by \"binding it\"? Also, after binding, how is it executed?\n- read-eval-print loop: that sounds interesting, i'll do some reading on that. thanks!\n- So basically, it's tree traversal, plus an insanely huge switch statement?\n- Not necessarily a visible switch if your implementation language supports classes with virtual methods. But yes, difference is what you do while traversing the tree.\n- Now, if an interpreter generates machine code and executes it, how is that different from a compiler? Is it just that it compiles every single time?\n- Also, do you have more information as to how an interpreter is implemented? Something i can read? Thanks!\n- @[Daniel Magliola]: re #1 yes, it compiles every single time and does not output the results of compilation for reust; few interpreters actually do this though, it's very expensive to redo all that work every time\n- @[Daniel Magliola]: re #2 the core of an interpreter is the 'evaluate' or 'execute' function that executes the input source code immediately; there are many ways to do this. your best bet is to look up how the interpreter works for each language you are interested in - basic, python, lisp, etc.\n- The Dragon book answers this question early in it's first chapter.\n- It's interesting that you mention a processor as an interpreter. That does give me a new point of view into this.\n- Thanks, actually plinth was the first to describe it that way.\n- Thank you, that was very clear. Now, to make sure I understood, the VM is just a \"target machine\" to generate code for, which will pick up your \"compiled\" code and either compile it for the real machine or interpret it and run it, right? It's like splitting compilation in 2 independent steps?","metadata":{"transformedAt":"2026-08-18T18:32:17.769Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":99,"estimatedTokens":1650}}180{"id":"stack-688098","source":"stackoverflow","questionId":688098,"title":"Compile to java bytecode (without using Java)","tags":["java","compiler-construction","bytecode"],"text":"Title: Compile to java bytecode (without using Java)\nTags: java, compiler-construction, bytecode\nSource: Stack Overflow\n\nQuestion:\nMy compilers class is creating a language that we intend to compile to Java Bytecode. We have made plenty of progress and are nearing the time where it's time for code generation.\n\nWe are having problems locating information on how to create .class files from our compiler. Do you have any resources that can give us some assistance? We already have plenty of documentation on the instruction set, but need information on how to directly fill out the class file/the writing of hex.\n\nWe do not need information or suggestions on decompiling the .class files.\n\nEven a simple example of writing out a .class file from scratch would be excellent.\n\n**The JVM spec is not what we're after.** What we really need is an example or a walkthrough.\n\n========================================\n\nTop Answer:\nThere are a number of projects out there that provide a high level interface to creating Java class files without you having to write the class files yourself. Take a look at the following:\n\n- ASM - http://asm.objectweb.org/\n\n- BCEL - http://jakarta.apache.org/bcel/\n\n- Trove - http://teatrove.sourceforge.net/trove.html\n\nAll provide an API to create class files. You could always look at the code they've written to do this and write some similar code for your compiler although I would imagine that it's a fair amount of work. \n\nWith BCEL take a look at ClassGen, that should enable you to write out class files in the format you want, a simple example follows:\n\n```\nClassGen cg = new ClassGen(\"HelloWorld\", \"java.lang.Object\",\n \"\", ACC_PUBLIC | ACC_SUPER,\n null);\n```\n\n========================================\n\nCode:\n```text\nClassGen cg = new ClassGen(\"HelloWorld\", \"java.lang.Object\",\n                             \"<generated>\", ACC_PUBLIC | ACC_SUPER,\n                             null);\n```\n\n```text\npublic class Hello {\n        public static void main(String[] args) {\n                System.out.println(\"H\");\n        }\n}\n```\n\n```text\nCompiled from \"Hello.java\"\npublic class Hello extends java.lang.Object{\npublic Hello();\n  Code:\n   0:   aload_0\n   1:   invokespecial   #1; //Method java/lang/Object.\"<init>\":()V\n   4:   return\n\npublic static void main(java.lang.String[]);\n  Code:\n   0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;\n   3:   ldc     #3; //String H\n   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V\n   8:   return\n\n}\n```\n\n```text\nCA FE BA BE 00 00 00 32 00 1D 0A 00 06 00 0F 09         _______2________\n00 10 00 11 08 00 12 0A 00 13 00 14 07 00 15 07         ________________\n00 16 01 00 06 3C 69 6E 69 74 3E 01 00 03 28 29         _____<init>___()\n56 01 00 04 43 6F 64 65 01 00 0F 4C 69 6E 65 4E         V___Code___LineN\n75 6D 62 65 72 54 61 62 6C 65 01 00 04 6D 61 69         umberTable___mai\n6E 01 00 16 28 5B 4C 6A 61 76 61 2F 6C 61 6E 67         n___([Ljava/lang\n2F 53 74 72 69 6E 67 3B 29 56 01 00 0A 53 6F 75         /String;)V___Sou\n72 63 65 46 69 6C 65 01 00 0A 48 65 6C 6C 6F 2E         rceFile___Hello.\n6A 61 76 61 0C 00 07 00 08 07 00 17 0C 00 18 00         java____________\n19 01 00 01 48 07 00 1A 0C 00 1B 00 1C 01 00 05         ____H___________\n48 65 6C 6C 6F 01 00 10 6A 61 76 61 2F 6C 61 6E         Hello___java/lan\n67 2F 4F 62 6A 65 63 74 01 00 10 6A 61 76 61 2F         g/Object___java/\n6C 61 6E 67 2F 53 79 73 74 65 6D 01 00 03 6F 75         lang/System___ou\n74 01 00 15 4C 6A 61 76 61 2F 69 6F 2F 50 72 69         t___Ljava/io/Pri\n6E 74 53 74 72 65 61 6D 3B 01 00 13 6A 61 76 61         ntStream;___java\n2F 69 6F 2F 50 72 69 6E 74 53 74 72 65 61 6D 01         /io/PrintStream_\n00 07 70 72 69 6E 74 6C 6E 01 00 15 28 4C 6A 61         __println___(Lja\n76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 29         va/lang/String;)\n56 00 21 00 05 00 06 00 00 00 00 00 02 00 01 00         V_!_____________\n07 00 08 00 01 00 09 00 00 00 1D 00 01 00 01 00         ________________\n00 00 05 2A B7 00 01 B1 00 00 00 01 00 0A 00 00         ___*____________\n00 06 00 01 00 00 00 01 00 09 00 0B 00 0C 00 01         ________________\n00 09 00 00 00 25 00 02 00 01 00 00 00 09 B2 00         _____%__________\n02 12 03 B6 00 04 B1 00 00 00 01 00 0A 00 00 00         ________________\n0A 00 02 00 00 00 03 00 08 00 04 00 01 00 0D 00         ________________\n00 00 02 00 0E                                          _____\n```\n\n========================================\n\nComments:\n- I just wrote a class file parser... the info that you need to emit bytecode is contained in chapter 4. What specifically is it that you find lacking in chapter 4?\n- Could you give us a tutorial or an example?\n- See my update, but I think you need to be more specific about what part of emitting a class file you do not understand.\n- Indeed. The class file format is exactly \"information on how to directly fill out the class file/writing of hex.\"\n- I would suggest writing a minimal class in Java, compiling it, and then looking through the class file with a hex debugger and the spec - preferably making lots of notes. Using BCEL or something similar would be faster, but you'd learn less.\n- @all: I really appreciate the responses. What I realize now is that we simply overlooked the value of the jvm spec. When I asked my teammates what they wanted, they said 'an example' which I thought was unusual as we can produce our own examples by viewing the class files as you mention. Anyway, thx\n- Anybody else gives this answer and they get a negative vote for recommending the JVM spec in spite of the phrase in bold - but Jon gets three up votes. He's right, but I'll bet that someone without the mad cred that Jon's accumulated gets dissed. Props to you, Jon.\n- hard to say, cus both Jon's answer and the edit occurred \"an hour ago\" but the bolded line about not wanting the JVM spec is edited in, so its likely Jon's answer occurred before the edit.\n- Jon and I suggest the VM spec. The voting down happened and then the edit or the edit and the voting down. Either way the edit and the down vote happened at pretty much the same time.\n- The edit was certainly after my answer - and I'd like to point out that I got a downvote too. This answer is on +4 -1 at the time of this writing.\n- yup - I was very confused that you had 4 and I had 1 then it was 3 and 0 :-) Took me a bit to figure out the seemingly random down votes (I really wish people would leave comments when they vote down) :-)\n- I'd suggest ASM rather than BCEL for your own sanity.\n- haven't used either of them, just suggested it as a reference to see how the class file was written (I still say chapters 4 and 6 is all that is needed though) unless specific issues arise.","metadata":{"transformedAt":"2026-08-18T18:32:17.769Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":117,"estimatedTokens":1683}}181{"id":"stack-1899373","source":"stackoverflow","questionId":1899373,"title":"How do compilers know where to find #include ?","tags":["c++","compiler-construction","include","header-files"],"text":"Title: How do compilers know where to find #include ?\nTags: c++, compiler-construction, include, header-files\nSource: Stack Overflow\n\nQuestion:\nI am wondering how compilers on Mac OS X, Windows and Linux know where to find the C header files.\n\nSpecifically I am wondering how it knows where to find the #include with the `<>` brackets.\n\n```\n#include \"/Users/Brock/Desktop/Myfile.h\" // absolute reference\n#include // system relative reference?\n```\n\nI assume there is a text file on the system that it consults. How does it know where to look for the headers? Is it possible to modify this file, if so where does this file reside on the operating system?\n\n========================================\n\nTop Answer:\nThe OS does not know where look for these files — the compiler does (or more accurately, the preprocessor). It has a set of search paths where it knows to look for headers, much like your command shell has a set of places where it will look for programs to execute when you type in a name. The GCC documentation explains how that compiler does it and how these search paths can be changed.\n\n========================================\n\nCode:\n```text\n#include \"/Users/Brock/Desktop/Myfile.h\"    // absolute reference\n#include <stdio.h>                         // system relative reference?\n```\n\n```text\n<>\n```\n\n```text\n/usr/include/c++/4.2.1/iostream\n```\n\n```text\ntouch empty.cpp\ng++ -v empty.cpp\n```\n\n```text\n%INCLUDE%\n```\n\n```text\n#include <boost/tokenizer.hpp>\n```\n\n```text\ng++ -I$BOOST_ROOT {blah,  blah}\n```\n\n========================================\n\nComments:\n- See my answer to that question: I updated it, and I think it should work for you.\n- You edited your post again, rendering my comment incomprehensible. Here's what I am talking about: stackoverflow.com/questions/1898837/&hellip;\n- @Alok I'm trying it out now thanks\n- This is one of those famous questions \"can I do X\" where the answer should be \"if you have to ask, you *shouldn't*\"\n- Upvote for good information & good resource link. Thanks Chuck\n- Just what I was looking for.","metadata":{"transformedAt":"2026-08-18T18:32:17.769Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":63,"estimatedTokens":513}}182{"id":"stack-10542082","source":"stackoverflow","questionId":10542082,"title":"Compiler written in Java: Peephole optimizer implementation","tags":["java","compiler-construction","compiler-optimization"],"text":"Title: Compiler written in Java: Peephole optimizer implementation\nTags: java, compiler-construction, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nI'm writing a compiler for a subset of Pascal. The compiler produces machine instructions for a made-up machine. I want to write a peephole optimizer for this machine language, but I'm having trouble substituting some of the more complicated patterns.\n\n### Peephole optimizer specification\n\nI've researched several different approaches to writing a peephole optimizer, and I've settled on a back-end approach:\n\n- The Encoder makes a call to an `emit()` function every time a machine instruction is to be generated.\n`emit(Instruction currentInstr)` checks a table of peephole optimizations:\n\nIf the current instruction matches the tail of a pattern: \n\n- Check previously emitted instructions for matching\n\n- If all instructions matched the pattern, apply the optimization, modifying the tail end of the code store\n\n- If no optimization was found, emit the instruction as usual\n\n### Current design approach\n\nThe method is easy enough, it's the implementation I'm having trouble with. In my compiler, machine instructions are stored in an `Instruction` class. I wrote an `InstructionMatch` class stores regular expressions meant to match each component of a machine instruction. Its `equals(Instruction instr)` method returns `true` if the patterns match some machine instruction `instr`.\n\nHowever, I can't manage to fully *apply* the rules I have. First off, I feel that given my current approach, I'll end up with a mess of needless objects. Given that a complete list of peephole optimizations numbers can number around 400 patterns, this will get out of hand fast. Furthermore, I can't actually get more difficult substitutions working with this approach (see \"My question\").\n\n### Alternate approaches\n\nOne paper I've read folds previous instructions into one long string, using regular expressions to match and substitute, and converting the string back to machine instructions. This seemed like a bad approach to me, please correct me if I'm wrong.\n\n### Example patterns, pattern syntax\n\n```\nx: JUMP x+1; x+1: JUMP y --> x: JUMP y\nLOADL x; LOADL y; add --> LOADL x+y\nLOADA d[r]; STOREI (n) --> STORE (n) d[r]\n```\n\nNote that each of these example patterns is just a human-readable representation of the following machine instruction template:\n\n```\nop_code register n d\n```\n\n(n usually indicates the number of words, and d an address displacement). The syntax `x: ` indicates that the instruction is stored at address `x` in the code store.\n\nSo, the instruction `LOADL 17` is equivalent to the full machine instruction `5 0 0 17` when the `LOADL` opcode is 5 (`n` and `r` are unused in this instruction)\n\n### My question\n\nSo, given that background, my question is this: How do I effectively match and replace patterns when I need to include parts of previous instructions as variables in my replacement? For example, I can simply replace all instances of `LOADL 1; add` with the increment machine instruction - I don't need any part of the previous instructions to do this. But I'm at a loss of how to effectively use the 'x' and 'y' values of my second example in the substitution pattern.\n\n**edit**: I should mention that each field of an `Instruction` class is just an integer (as is normal for machine instructions). Any use of 'x' or 'y' in the pattern table is a variable to stand in for any integer value.\n\n========================================\n\nCode:\n```none\nx: JUMP x+1; x+1: JUMP y  -->  x: JUMP y\nLOADL x; LOADL y; add     -->  LOADL x+y\nLOADA d[r]; STOREI (n)    -->  STORE (n) d[r]\n```\n\n```text\nop_code register n d\n```\n\n```text\nemit()\n```\n\n```text\nemit(Instruction currentInstr)\n```\n\n```text\nInstruction\n```\n\n```text\nInstructionMatch\n```\n\n```text\nequals(Instruction instr)\n```\n\n```text\ntrue\n```\n\n```text\ninstr\n```\n\n```text\nx: <instr>\n```\n\n```text\nx\n```\n\n```text\nLOADL 17\n```\n\n```text\n5 0 0 17\n```\n\n```text\nLOADL\n```\n\n```text\nn\n```\n\n```text\nr\n```\n\n```text\nLOADL 1; add\n```\n\n```text\nInstruction\n```\n\n```text\nPUSHVAR x\n PUSHK i\n ADD\n POPVAR x\n MOVE x,k\n```\n\n```text\nPUSHK i, PUSHK j, ADD ==> PUSHK i+j\n PUSHK i, POPVAR x ==> MOVE x,i\n```\n\n```text\nPEEPHOLESTATE (an enum symbol, initialized to EMPTY)\n FIRSTCONSTANT (an int)\n SECONDCONSTANT (an int)\n```\n\n```text\nGeneratePUSHK:\n    switch (PEEPHOLESTATE) {\n        EMPTY: PEEPHOLESTATE=PUSHK;\n               FIRSTCONSTANT=K;\n               break;\n        PUSHK: PEEPHOLESTATE=PUSHKPUSHK;\n               SECONDCONSTANT=K;\n               break;\n        PUSHKPUSHK:\n        #IF consumeEmitLoadK // flush state, transition and consume generated instruction\n               emit(PUSHK,FIRSTCONSTANT);\n               FIRSTCONSTANT=SECONDCONSTANT;\n               SECONDCONSTANT=K;\n               PEEPHOLESTATE=PUSHKPUSHK;\n               break;\n        #ELSE // flush state, transition, and reprocess generated instruction\n               emit(PUSHK,FIRSTCONSTANT);\n               FIRSTCONSTANT=SECONDCONSTANT;\n               PEEPHOLESTATE=PUSHK;\n               goto GeneratePUSHK;  // Java can't do this, but other langauges can.\n        #ENDIF\n     }\n\n  GenerateADD:\n    switch (PEEPHOLESTATE) {\n        EMPTY: emit(ADD);\n               break;\n        PUSHK: emit(PUSHK,FIRSTCONSTANT);\n               emit(ADD);\n               PEEPHOLESTATE=EMPTY;\n               break;\n        PUSHKPUSHK:\n               PEEPHOLESTATE=PUSHK;\n               FIRSTCONSTANT+=SECONDCONSTANT;\n               break:\n     }  \n\n  GeneratePOPX:\n    switch (PEEPHOLESTATE) {\n        EMPTY: emit(POP,X);\n               break;\n        PUSHK: emit(MOV,X,FIRSTCONSTANT);\n               PEEPHOLESTATE=EMPTY;\n               break;\n        PUSHKPUSHK:\n               emit(MOV,X,SECONDCONSTANT);\n               PEEPHOLESTATE=PUSHK;\n               break:\n     }\n\nGeneratePUSHVARX:\n    switch (PEEPHOLESTATE) {\n        EMPTY: emit(PUSHVAR,X);\n               break;\n        PUSHK: emit(PUSHK,FIRSTCONSTANT);\n               PEEPHOLESTATE=EMPTY;\n               goto GeneratePUSHVARX;\n        PUSHKPUSHK:\n               PEEPHOLESTATE=PUSHK;\n               emit(PUSHK,FIRSTCONSTANT);\n               FIRSTCONSTANT=SECONDCONSTANT;\n               goto GeneratePUSHVARX;\n     }\n```\n\n```text\nflush() {\n    switch (PEEPHOLESTATE) {\n        EMPTY: break;\n        PUSHK: emit(PUSHK,FIRSTCONSTANT);\n               break;\n        PUSHKPUSHK:\n               emit(PUSHK,FIRSTCONSTANT),\n               emit(PUSHK,SECONDCONSTANT),\n               break:\n      }\n      PEEPHOLESTATE=EMPTY;\n      return; }\n```\n\n```text\nPUSHK  1\n      PUSHK  2\n      ADD\n      PUSHK  5\n      POPVAR X\n      POPVAR Y\n```\n\n========================================\n\nComments:\n- Wow, this is quite the thorough response. Thank you for the suggestion, I'll certainly consider this approach in my design.","metadata":{"transformedAt":"2026-08-18T18:32:17.769Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":24,"totalLines":246,"estimatedTokens":1710}}183{"id":"stack-32503355","source":"stackoverflow","questionId":32503355,"title":"(Compiler) else if(true) vs else scenario","tags":["java","if-statement","compiler-construction","language-lawyer"],"text":"Title: (Compiler) else if(true) vs else scenario\nTags: java, if-statement, compiler-construction, language-lawyer\nSource: Stack Overflow\n\nQuestion:\nTake the following Java code snippet:\n\n```\n....\n else if (true){ //hard-coded as true\n ///launch methodA\n }\n else {\n ///launch methodA (same code as in the ` else if ` statement)\n }\n....\n```\n\nWhat I was wondering is how the compiler deals with this. Wouldn't it be logical for the compiler to remove the `else if(true)` statement altogether in order to not have to perform a check, even though it is hard-coded as true. Specifically in Eclipse, how is the code above interpreted? \n\nOr what about in the following scenario:\n\n```\n....\n else if (true){ //hard-coded as true\n ///launch methodA\n }\n else {\n ///launch methodBB\n }\n....\n```\n\nWouldn't it be logical in this case for the compiler to remove the `else` statement? Because while running, the `else` statement is unreachable.\n\n========================================\n\nTop Answer:\nThe compiler optmizes it at compile time:\n\n```\npublic class Test {\n public static void main(String[] args) {\n if(true) {\n System.out.println(\"Hello\");\n } else {\n System.out.println(\"Boom\");\n }\n}\n```\n\nGives me (with my `Java 1.8.0_45`):\n\n```\nCompiled from \"Test.java\"\npublic class Test {\n publicTest();\n Code:\n 0: aload_0\n 1: invokespecial #1 // Method java/lang/Object.\"\":()V\n 4: return\n\n public static void main(java.lang.String[]);\n Code:\n 0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;\n 3: ldc #3 // String Hello\n 5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V\n 8: return\n}\n```\n\nThe code just prints `Hello`. `Boom` is not even considered.\n\nAll recent Java compilers eliminate dead code at compile time.\n\n========================================\n\nCode:\n```text\n....\n    else if (true){ //hard-coded as true\n     ///launch methodA\n    }\n    else {\n     ///launch methodA (same code as in the ` else if ` statement)\n    }\n....\n```\n\n```text\n....\n    else if (true){ //hard-coded as true\n     ///launch methodA\n    }\n    else {\n     ///launch methodBB\n    }\n....\n```\n\n```text\nelse if(true)\n```\n\n```text\nelse\n```\n\n```text\nelse\n```\n\n```text\nif (false) { x=3; }\n```\n\n```text\nstatic final boolean DEBUG = false;\n```\n\n```text\nif (DEBUG) { x=3; }\n```\n\n```text\nelse if (true){ //hard-coded as true\n ///launch methodA\n}\nelse {\n ///launch methodA (same code as in the else if statement)\n}\n```\n\n```text\nelse {\n ///launch methodA\n}\n```\n\n```text\nelse if (true){ //hard-coded as true\n ///launch methodA\n}\nelse {\n ///launch methodBB\n}\n```\n\n```text\nelse {\n ///launch methodA\n}\n```\n\n```text\npublic class Test {\n    public static void main(String[] args) {\n    if(true) {\n        System.out.println(\"Hello\");\n    } else {\n        System.out.println(\"Boom\");\n    }\n}\n```\n\n```text\nCompiled from \"Test.java\"\npublic class Test {\n  publicTest();\n    Code:\n       0: aload_0\n       1: invokespecial #1        // Method java/lang/Object.\"<init>\":()V\n       4: return\n\n  public static void main(java.lang.String[]);\n    Code:\n       0: getstatic     #2        // Field java/lang/System.out:Ljava/io/PrintStream;\n       3: ldc           #3        // String Hello\n       5: invokevirtual #4        // Method java/io/PrintStream.println:(Ljava/lang/String;)V\n       8: return\n}\n```\n\n```text\nJava 1.8.0_45\n```\n\n```text\nHello\n```\n\n```text\nBoom\n```\n\n```text\npublic static void aa(String b) {\n  if (true) {\n\n  } else if (true) {\n    System.out.println(\"asas\");\n  } else {\n  }\n}\n```\n\n```text\npublic static void aa(String b) {}\n```\n\n```text\nelse\n```\n\n========================================\n\nComments:\n- Just wondering... the most logical (or at least the most efficient) would be the compiler to leave only the `launch methodA`, in both cases.\n- \"Wouldn't it be logical...\" Yes, and that's what most modern compilers actually do.\n- Actually, Eclipse has its own compiler, `ecj` (Eclipse Compiler for Java). Sun's/Oracle's `javac` (written by Martin Odersky, the designer of Scala) is a batch compiler, not designed for interactive, incremental use. `ecj` (like all of Eclipse) is derived from IBM VisualAge for Smalltalk, and thus specifically designed for \"while-you-type\" interactive, incremental compilation inside an IDE.\n- Beyond that, it would be rather annoying if a programmer who has a variable like `static boolean enableLogging;` which is actually *used as a variable* couldn't later change it to `static final boolean elableLogging = false;` without having to manually edit all the code that reads it.","metadata":{"transformedAt":"2026-08-18T18:32:17.769Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":20,"totalLines":221,"estimatedTokens":1129}}184{"id":"stack-35391324","source":"stackoverflow","questionId":35391324,"title":"How does a parser for C++ differentiate between comparisons and template instantiations?","tags":["c++","parsing","syntax","compiler-construction"],"text":"Title: How does a parser for C++ differentiate between comparisons and template instantiations?\nTags: c++, parsing, syntax, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn C++, the symbols '' are used for comparisons as well as for signifying a template argument. Thus, the code snippet\n\n```\n[...] Foo [...]\n```\n\nmight be interpreted as any of the following two ways:\n\n- An object of type Foo with template argument Bar\n\n- Compare Foo to Bar, then compare the result to whatever comes next\n\nHow does the parser for a C++ compiler efficiently decide between those two possibilities?\n\n========================================\n\nTop Answer:\nThe important point to remember is that C++ grammar is not context-free. I.e., when the parser sees `Foo There are difficult cases, when you literally have to guide the parser. For example, suppose that are writing a class template with a template member function, which you want to specialize explicitly. You might have to use syntax like:\n\n```\na->template foo();\n```\n\n(in some cases; see Calling template function within template class for details)\n\nAlso, comparisons inside non-type template arguments must be surrounded by parentheses, i.e.:\n\n```\nfoo B)>\n```\n\nnot\n\n```\nfoo B>\n```\n\nNon-static data member initializers bring more fun: http://open-std.org/JTC1/SC22/WG21/docs/cwg_active.html#325\n\n========================================\n\nCode:\n```text\n[...] Foo < Bar > [...]\n```\n\n```text\nFoo\n```\n\n```text\ntemplate <...> Foo ...\n```\n\n```text\ntemplate Foo\n```\n\n```text\nFoo < Bar\n```\n\n```text\nFoo < Bar >\n```\n\n```text\nFoo\n```\n\n```text\nFoo < Bar\n```\n\n```text\nFoo\n```\n\n```text\nFoo<Bar>\n```\n\n```text\nFoo\n```\n\n```text\nFoo\n```\n\n```text\ntemplate\n```\n\n```text\nFoo\n```\n\n```text\ntemplate\n```\n\n```text\n<\n```\n\n```text\n}\n```\n\n```text\ntemplate\n```\n\n```text\na->template foo<int>();\n```\n\n```text\nfoo<(A > B)>\n```\n\n```text\nfoo<A > B>\n```\n\n```text\nFoo < Bar\n```\n\n```text\nFoo\n```\n\n```text\n<\n```\n\n========================================\n\nComments:\n- You can have a good idea of what appens by checking what is a LL grammar. ;-)\n- @Caduchon AFAIK C++ grammar is not LL nor LR. In fact I believe it's not context-free at all; this is hinted in the answer where knowledge about the context seems to be necessary to correctly parse.\n- @Bakuriu: Most conventional grammars (single-nonterminal left-hand-side) are context-free by definition. C++ doesn't change this. The interpretation of the syntax is not-context free in practice for most langauges. C++ is not different is this regard.\n- @Askaga: you are confusing \"parsing\" in the narrow sense with determining the meaning of the syntax, of which part is resolving the meaning of each identifier used at each point in the source code (\"name resolution\".\n- To add to this answer: when compiler does not have the context, e.g. when `Foo` is a dependent name, you need to tell compiler that it is a template: `template Foo`\n- I see. So since in C++ everything has to be declared before it is used, the lexer and parser stages can be interlinked, and the parser constantly feeds back the current type and variable names to the lexer so it can generate the most probable token, either *identifier* or *type identifier*, right? And in case it is undecidable, the programmer has to prefix the *type identifier* with the \"template\" keyword.\n- There actually at least three kinds of identifier the lexer typically distinguishes: 'template', 'typedef/class name' and 'normal identifier'. There are two different prefixes for the case it is unknown: `template` and `typename`.\n- `Foo` is a *template-id*.\n- great point. It is a bit an off-topic question. Let's say the C++ designers or designers of a new language decide using another symbol for template parameters rather than ``. The candidates `( )`, `[ ]` and `{ }` are used for the other purposes. Would there be any other alternative for a language?","metadata":{"transformedAt":"2026-08-18T18:32:17.769Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":24,"totalLines":155,"estimatedTokens":968}}185{"id":"stack-3179415","source":"stackoverflow","questionId":3179415,"title":"Is there a systematic way to discover which implicit defs are in scope, and which one is bound at a particular point?","tags":["debugging","compiler-construction","scala","implicit"],"text":"Title: Is there a systematic way to discover which implicit defs are in scope, and which one is bound at a particular point?\nTags: debugging, compiler-construction, scala, implicit\nSource: Stack Overflow\n\nQuestion:\nOften there's no need to pay any attention to implicit arguments in Scala, but sometimes it's very helpful to understand how the compiler is automatically providing them. Unfortunately, this understanding seems to be hard to obtain!\n\n **Is there a general method to discover how an implicit parameter has been provided, in a given piece of code?**\n\nIdeally, one day IDE integration would provide this information in some way, but I expect for now I'll have to dig deeper. Is there some way to ask the compiler to explain exactly which implicit definition it chooses at any given point? Can this be deciphered indirectly from other compiler output?\n\nAs an example, I'd like to know how to work out on my own where the `implicit bf: CanBuildFrom[Repr, B, That]` argument to `TraversableLike.map` comes from, without reading questions like this one on Stack Overflow!\n\n========================================\n\nTop Answer:\nIdeally, one day IDE integration would provide this information in some way, ...\n\nThat day is today in with JetBrains' IDEA. If you run the latest EAP of IDEA version 9 (9.0.3 EA #95.289) with a recent nightly release of the Scala plug-in, this capability is present. Every value expression may be selected and a command issued that displays a pop-up showing all applicable implicit conversions with the one the compiler will select highlighted.\n\nAnd since there are apparently a few out there who don't yet know it, there is a free and open-source Community Edition of IDEA and it does support the Scala plug-in.\n\n========================================\n\nCode:\n```text\nimplicit bf: CanBuildFrom[Repr, B, That]\n```\n\n```text\nTraversableLike.map\n```\n\n```text\n-Xprint:typer\n```\n\n```text\nCTRL-ALT-SHIFT-I\n```\n\n========================================\n\nComments:\n- Shouldn't typer phase provide AST even with inferred type information when scalac does implicit resolution? stackoverflow.com/q/27468053/306488.","metadata":{"transformedAt":"2026-08-18T18:32:17.769Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":45,"estimatedTokens":535}}186{"id":"stack-2464478","source":"stackoverflow","questionId":2464478,"title":"Open source C compiler in C#?","tags":["c#","c","compiler-construction"],"text":"Title: Open source C compiler in C#?\nTags: c#, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI've been getting into compiler creation. I've found some terrific beginner stuff and advanced stuff but nothing in the middle. I've created 3 different simple proof-of-concept compilers for toy languages but I want to expose myself to something real.\n\nThe most straight forward real language in terms of syntax seems to be C. Since the language I'm most comfortable with right now is C#, I'd love to study the source code of a real non-tutorial C compiler written in C#. Does one (with source code available) exist?\n\nIdeally I'd like a C compiler, not a .NET or C# compiler, but with the source code written in C#.\n\nI know C# --> C feels a little backwards but it'll allow me to ease deeper into compilers starting with a familiar language before I go changing that too.\n\nAlthough I'm not looking for C#/.NET compilers, here are some in case someone sees this question who is looking for that:\n\n- Create a Language Compiler for the .NET Framework\n\n- Mono C# Compiler\n\n- Source for a C# compiler written in pure C# (.NET v1) (thanks Luiscencio)\n\n========================================\n\nTop Answer:\nThe most straightforward real language in terms of syntax seems to be C.\n\nI'm not sure what you mean by \"real language\", but whatever \"real language\" means, I cannot agree that C has a \"straightforward\" lexical or syntactic grammar, and its semantics are underspecified. If you want an extremely straightforward language with pretty well-defined semantics, why not go for Scheme? Scheme has a very easy grammar but is certainly not trivial to get its semantics right.\n\n========================================\n\nComments:\n- github.com/phisiart/C-Compiler\n- Good find and I know there's the Mono one as well but I'm really looking for a C compiler if possible.\n- To quote DMR from cm.bell-labs.com/cm/cs/who/dmr/chist.html: In 1971 I began to extend the B language by adding a character type and also rewrote its compiler to generate PDP-11 machine instructions instead of threaded code. Thus the transition from B to C was contemporaneous with the creation of a compiler capable of producing programs fast and small enough to compete with assembly language. I called the slightly-extended language NB, for `new B.'\n- In what way are the semantics underspecified?\n- @Dinah: Many aspects of C and C++ are left up to the discretion of the compiler writer, and different compiler writers choose different semantics. What order function arguments are evaluated in, for instance. (Though, to be fair, scheme also leaves this underspecified. However, this is mitigated somewhat by the fact that scheme discourages side-effecting code, which makes it less relevant which one goes first.)\n- I've never touched Scheme but I could give it a shot. Do you know of an open source Scheme compiler in C#?\n- @Dinah: There are Scheme implementations for .NET but I do not know if they are open source.\n- If I recall correctly, C can be compiled in a single pass. So in some sense, C is very *straightforward* ;-)","metadata":{"transformedAt":"2026-08-18T18:32:17.769Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":39,"estimatedTokens":775}}187{"id":"stack-17904440","source":"stackoverflow","questionId":17904440,"title":"Dump IR after each LLVM optimization (each pass), both LLVM IR passes and backend debugging","tags":["debugging","compiler-construction","clang","llvm"],"text":"Title: Dump IR after each LLVM optimization (each pass), both LLVM IR passes and backend debugging\nTags: debugging, compiler-construction, clang, llvm\nSource: Stack Overflow\n\nQuestion:\nI want to find some debugging options for Clang/LLVM which work like GCC's -fdump-tree-all-all, -fdump-rtl-all, and -fdump-ipa-all-all.\n\nBasically, I want to have an LLVM IR dump before and after each optimization pass. Also, it can be useful to have all dumps of the AST from Clang and all phases of code generation (backend phases, Selection DAG, ISEL-SDNode, register allocation, and MCInsts).\n\nI was able to find only the Clang's `-ccc-print-phases`, but it will only print high-level phases names, e.g., preprocess-compile-assemble-link; but no any dump of IR.\n\nAlso there is *Life of an instruction in LLVM* paper with `-cc1-ast-dump` option to dump Clang ASTs, but I want more, especially for code generation.\n\n========================================\n\nTop Answer:\nIt is not fully about your question, but to see the passes applied, you can do:\n\n`clang test.c -Ofast -march=core-avx2 -mllvm -debug-pass=Arguments`\n\nYou will see something like:\n\n`Pass Arguments: -datalayout -notti -basictti -x86tti -targetlibinfo -jump-instr-table-info -targetpassconfig -no-aa -tbaa -scoped-noalias -basicaa -collector-metadata -machinemoduleinfo -machine-branch-prob -jump-instr-tables -verify -verify-di -domtree -loops -loop-simplify -scalar-evolution -iv-users -loop-reduce -gc-lowering -unreachableblockelim -consthoist -partially-inline-libcalls -codegenprepare -verify-di -stack-protector -verify -domtree -loops -branch-prob -machinedomtree -expand-isel-pseudos -tailduplication -opt-phis -machinedomtree -slotindexes -stack-coloring -localstackalloc -dead-mi-elimination -machinedomtree -machine-loops -machine-trace-metrics -early-ifcvt -machinelicm -machine-cse -machine-sink -peephole-opts -dead-mi-elimination -processimpdefs -unreachable-mbb-elimination -livevars -machinedomtree -machine-loops -phi-node-elimination -twoaddressinstruction -slotindexes -liveintervals -simple-register-coalescing -misched -machine-block-freq -livedebugvars -livestacks -virtregmap -liveregmatrix -edge-bundles -spill-code-placement -virtregrewriter -stack-slot-coloring -machinelicm -edge-bundles -prologepilog -machine-block-freq -branch-folder -tailduplication -machine-cp -postrapseudos -machinedomtree -machine-loops -post-RA-sched -gc-analysis -machine-block-freq -block-placement2 -stackmap-liveness -machinedomtree -machine-loops`\n\n========================================\n\nCode:\n```text\n-ccc-print-phases\n```\n\n```text\n-cc1-ast-dump\n```\n\n```text\nllc -help-hidden | grep dags\n```\n\n```text\n-view-dag-combine-lt-dags  - Pop up a window to show dags before the\n                             post legalize types dag combine pass\n-view-dag-combine1-dags    - Pop up a window to show dags before\n                             the  first dag combine pass\n-view-dag-combine2-dags    - Pop up a window to show dags before the\n                             second dag combine pass\n-view-isel-dags            - Pop up a window to show isel dags\n                             as they are selected\n-view-legalize-dags        - Pop up a window to show dags before legalize\n-view-legalize-types-dags  - Pop up a window to show dags\n                             before legalize types\n-view-misched-dags         - Pop up a window to show MISched\n                             dags after they are processed\n-view-sched-dags           - Pop up a window to show sched\n                             dags as they are processed\n-view-sunit-dags           - Pop up a window to show SUnit dags\n                             after they are processed\n```\n\n```text\n-debug\n```\n\n```text\n-view*-dags\n```\n\n```text\ngcc  -fdump-tree-all -o test file1.c file2.c file1.h -I  .\n```\n\n```text\nllvm-gcc-4.2\n```\n\n```text\n-fdump-tree-all\n```\n\n```text\nclang test.c -Ofast -march=core-avx2 -mllvm -debug-pass=Arguments\n```\n\n```text\nPass Arguments:  -datalayout -notti -basictti -x86tti -targetlibinfo -jump-instr-table-info -targetpassconfig -no-aa -tbaa -scoped-noalias -basicaa -collector-metadata -machinemoduleinfo -machine-branch-prob -jump-instr-tables -verify -verify-di -domtree -loops -loop-simplify -scalar-evolution -iv-users -loop-reduce -gc-lowering -unreachableblockelim -consthoist -partially-inline-libcalls -codegenprepare -verify-di -stack-protector -verify -domtree -loops -branch-prob -machinedomtree -expand-isel-pseudos -tailduplication -opt-phis -machinedomtree -slotindexes -stack-coloring -localstackalloc -dead-mi-elimination -machinedomtree -machine-loops -machine-trace-metrics -early-ifcvt -machinelicm -machine-cse -machine-sink -peephole-opts -dead-mi-elimination -processimpdefs -unreachable-mbb-elimination -livevars -machinedomtree -machine-loops -phi-node-elimination -twoaddressinstruction -slotindexes -liveintervals -simple-register-coalescing -misched -machine-block-freq -livedebugvars -livestacks -virtregmap -liveregmatrix -edge-bundles -spill-code-placement -virtregrewriter -stack-slot-coloring -machinelicm -edge-bundles -prologepilog -machine-block-freq -branch-folder -tailduplication -machine-cp -postrapseudos -machinedomtree -machine-loops -post-RA-sched -gc-analysis -machine-block-freq -block-placement2 -stackmap-liveness -machinedomtree -machine-loops\n```\n\n========================================\n\nComments:\n- Hmm, there are \"`-print-after-all` - Print IR after each pass; -print-before-all - `Print IR before each pass`\" for IR phases, but how to get dumps from backend?\n- You can print debug info from backend phases yourself, eg. `CGDebugInfo` object from codegen.\n- mishr, Isn't CGDebugInfo from clang IR emitter, not from llvm backend? I want to get info from LLVM IR-to-machine code backend (codegen)\n- True. Maybe this will help. See the last 2 sections, and maybe you need to chase how it was done. nondot.org/sabre/LLVMNotes/DebugInfoVariableInfo.txt\n- Eli, can you add information about clang and IR dumps into your answer too?\n- And -print-machineinstrs may be useful. My llc from llvm 3.2 doesn't have any of `-view-*-dags`.\n- @osgx: see my edit w.r.t. not having `-view-*`. As for other dumps, feel free to edit my answer with the information you've already pasted into the questions and comments :)\n- Does it output LLVM IR core, or only GCC's internal representation?\n- Zinovy, thank you, it can be helpful too. What is the mininal version of clang/llvm to use the \"`-debug-pass=Arguments`\"?\n- `-mllvm -debug-pass=Structure` is even nicer. It includes the output of `-debug-pass=Arguments`, but also dumps a tree of optimizations applied with human-readable names.\n- Re `-debug-pass=Arguments`: Not `-fdebug-pass-arguments`? Did it change in a later version?\n- `-debug-pass=Arguments&#47;Structure` cannot be recognize in the latest clang. Use `-fdebug-pass-arguments&#47;structure` instead as Peter pointed out.","metadata":{"transformedAt":"2026-08-18T18:32:17.769Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":102,"estimatedTokens":1728}}188{"id":"stack-2632601","source":"stackoverflow","questionId":2632601,"title":"Why are forward declarations necessary?","tags":["c++","c","compiler-construction","declaration"],"text":"Title: Why are forward declarations necessary?\nTags: c++, c, compiler-construction, declaration\nSource: Stack Overflow\n\nQuestion:\n**Possible Duplicate:**\n\n Should C++ eliminate header files? \n\nIn languages like C# and Java there is no need to declare (for example) a class before using it. If I understand it correctly this is because the compiler does two passes on the code. In the first it just \"collects the information available\" and in the second one it checks that the code is correct.\n\nIn C and C++ the compiler does only one pass so everything needs to be available at that time.\n\nSo my question basically is why isn't it done this way in C and C++. Wouldn't it eliminate the needs for header files?\n\n========================================\n\nTop Answer:\nBottom line: there have been advances in compiler technology that make forward declarations unnecessary. Plus computers are thousands of times faster, and so can make the extra calculations necessary to handle the lack of forward declarations.\n\nC and C++ are older and were standardized at a time when it was necessary to save every CPU cycle.\n\n========================================\n\nCode:\n```text\nint i;\n\nint f() { \n     i = 1;\n     int i = 2;\n}\n```\n\n```text\ni=1\n```\n\n```text\nf()\n```\n\n```text\ni\n```\n\n========================================\n\nComments:\n- The C++ compiler will read sequentially [that is, it will read top to bottom...] Thats just how the language works. Your suggestion of \"passing\" through twice and looking at the function prototypes then would work, but unfortunately thats not how the language works.\n- Duplicate: stackoverflow.com/questions/752793\n- C as a language was standardised more than 30 years ago, when the underlying technology was both far less capable and far more expensive than things are today. A gentle suggestion: Try to learn a little about the history of things, and understand that the world has changed tremendously since those decisions were made and will continue to change after you make choices in your development efforts. Someone, someday, *will* wonder \"WTF?!\" about your decisions.... ;-)\n- It is not entirely true for C++, it has a partially 2-pass compiler. Class methods written inline within the class declarations may refer to class members that appear below it. Not sure why they didn't make it consistent.\n- @Hans: Those methods are necessarily in the same file, at least.\n- Pascal, which was popular on small computers before C was, was explicitly designed to be compilable in one pass with a recursive-descent parser.\n- This it not a duplicate. I haven't bothered to look into what options the Arudino IDE passes to the C++ compiler it uses but by default it works just fine without forward declarations. Even if it's a pre-processing step that step could be added to C++ so at least within the same file it's fully possible to function without forward declarations.\n- :-) In other words - C# is better than C++.\n- You're missing the key words here: backwards compatibility. Your last line makes it sound like C and C++ have only one version of the standard from the stone ages. It should read \"and were *first* standardized...and to maintain backwards compatibility, the method remains the same.\" @Franci: When you're done writing an OS in C#, come get me.\n- @Franci: No... in other words, modern language compilers have made forward declarations obsolete because they do not have to worry about backwards compatibility. It *could* be done in C++. Have fun writing hardware drivers in C# bud.\n- @GMan - Save the Unicorns: You have excellent points about \"first standardized\" and \"backwards compatibility\". Regarding an OS in C#: I give you Singularity. Now, granted, some performance-critical portions of the kernel were written in C, but given that some portions of the kernel are frequently written in assembly, I'd say they've stepped up a bit.\n- @Ed: obsolete? This way you need to store the metainformation in the assembly (for .NET), something which C++ can successfully avoid.\n- @Vlad: Who cares? As a programmer I don't care how it is done, I care that I don't have to write forward declarations. When this becomes a performance issue let me know.\n- @Randolpho: Yeah, because Singularity is very widely used, and they still had to write parts of it in C.\n- @Ed: A good programmer cares about quality of his output.\n- @Ed Swangren: exactly! (re: performance) Performance isn't the end-all be-all of programming, it's just something that you might need in some cases. Productivity, stability, and maintainability are all infinitely more important.\n- @Gman: but can't that be implemented as just a switch in the compiler?\n- @Vlad: That's funny. Somehow storing metadata reduces the quality of your output? Please provide a real life example where this is an issue.\n- @Randolpho: \"infinitely more important\" is probably a *slight* exaggeration.\n- @user: The compiler has nothing to do with the language. I'm sure there exist somewhere a branch of some compiler that implements modules in C++. But to make such a thing standardized is no trivial task. I think it was proposed for C++0x, but it's not accepted. Such a change requires old code to work as is while allowing new syntax to specify new behavior. There are more pressing issues to worry about.\n- @Ed Swangren - it is possible to write hardware drivers in C#. In fact, there are user-level drivers written in C#. As for the backward compatibility - that is somewhat valid argument, only if you insist that any C++ source should be compatible with any C++ compiler ever written. However, there's no reason why there couldn't be a C++ v2 that introduces new language extensions that are supported only by C++ v2 compilers.\n- @Ed: the words \"real life example\" suggest that you consider storing metadata in the output file a neglectable issue. For some people it's an issue, of course. For example, you deploy to the user something that he strictly doesn't need to have. If you want some more \"business-sounding\" reasons, you expose your source code to the reverse engineering.\n- @Matthew Crumley: Ok, perhaps *infinitely* was a bit exaggerated. More like several hundred thousand orders of magnitude. Better?\n- @Randolpho: The approach \"don't care if it's fast and efficient, but it should be fun to code\" drives me as user crazy. I've seen too many programs written with this attitude.\n- @Vlad: No. Find me this user who has an issue directly caused by storing metadata in an assembly. That user does not exits, and you cannot give me a real life example, so yes, it is a non-issue. As for writing drivers in C#, it would be a huge pain to get the low level access that you need and would just be silly. Many things are theoretically possible. I could pound in a nail with a screwdriver, but why in the world would I do such a thing when hammers exist?\n- GMan> in fact the \"modules\" feature in C++ was planned but couldn't be included in the new standard as it was already a lot of work. So today it's planned to be discussed for the following TR2 or major version. Would be nice if it took less than 5 years :P\n- @(another at)Vlad: Again, show me where these things are causing performance issues. Show me numbers where it matters.\n- @Ed: Users don't complain directly about the metadata, because they don't know what it is. However they complain about programs getting bigger and slower, about cost they need to pay on the hardware in order to be able to do simple things. Imho at least part of these problems comes from attitude \"don't care about the efficiency\".\n- @Vlad: Did I say (write) anything about being \"fun to code\"? No, sir. If a program is unusable it is fail, and performance from a UI perspective certainly falls in that category. But does that mean that you absolutely have to implement Han's algorithm because the framework's array sort only offers a derivative of Heapsort? No, it doesn't. Odds are, you don't; even bubblesort might be fast enough. What is far more valuable than sofware that performs a half-second faster is software that can be written faster and (if necessary) modified faster.\n- @Ed: numbers? Very simple: how much RAM was it needed for running window manager on client's OS 10 years ago, and how much is it needed now? Compare yourself.\n- @Randolpho: I disagree. Writing software faster makes the software company get invested money back fast. But good software requires time to be written. As user, I personally prefer fast, well-debugged and highly-optimized software. [And yes, I sometimes can distinguish software which uses bubblesort.]\n- @Randolpho: no, you didn't say \"fun to code\". I just understood you that way, sorry if that was wrong.\n- @Vlad: Oh, I agree that putting software out there fastest isn't the most important, but it's highly important, particularly in a world as competitive as software. But what I value even more than either productivity (speed of release) or performance (speed of execution) is maintainability (speed of modification). Software lives a long time, and everyone loves to forget that big-ass elephant-in-the-room \"maintenance\" phase of a software project.\n- @Vlad: I suppose that would be a valid point if modern machines had the same amount of RAM in them as they did years ago. As they have much more and the price has declined, why not use the extra bits for something productive? That does not make your code \"bloated and slow\", there is a difference.\n- BTW, I should mention that I am a systems engineer who often works in environments with limited resources. However, I am also practical. Your basic GUI does not benefit from micro-optimization.\n- @Randolpho: You are right--however I try to look from the point of view of user, not business. Unfortunately in our Universe the software must be competitive (this includes being promptly developed and released, as well as cheap in production), so optimization up the the last bits has died out. Definitely maintenance is a big issue, but I believe that a good design can contribute more to it than any generic framework.\n- @Ed: I suspect we mix the cause and the effect now. The processor speed/memory requirements race was (partially) driven by the declining software quality--which posed actual need for more resources. The usual program nowadays runs at approximately the same speed as 10 years ago (from the user POV), and doesn't bring substantial functionality gain, so the increased power of hardware was compensating increased inaccuracy of development.\n- @Vlad: Very true. But beware premature or even micro-optimization. You have to think about the return on investment; is it really worth the extra time to squeeze 30 less milliseconds out of that method just to satisfy some notion that performance is king?\n- @Randolpho: Totally agree, \"premature optimization is root of all evil\". Still, I miss the times when programming was not a business but an art, so writing the program as optimal as possible was a kind of \"point of honour\".\n- @Vlad: It still is an art, you just have to shift your focus. Look at the code. If some n00b off the street comes in and reads your code, can he understand it? Then you have written a work of art.\n- Unfortunately, that doesn't explain how C# manages our 100-assembly Solution containing thousands of source files and millions of lines of code better than C++ manages a single .h file (which may still need a forward declaration even though all the information it needs is in the one file).\n- @Jason: the benefits of separate compilation are different: when you change only implementation, your recompilation will be almost instant. (Of course this make the very first compilation slower.) I don't know why your C++ cannot manage a single header file, I did never encounter any problems with mine.\n- @Vlad: You said \"...compiler needs information about types ... declared in other compilation modules\", but in fact, even a single C++ class in a single header *can* require predeclaration - i.e even within one compilation module. That is, C++ parses in a linear way through the code (thus requiring predeclaration of future types when they are referenced), while C# effectively builds a database for the codebase that allows it to have random access to all the types.\n- @Jason: you are right. This is sometimes an advantage, and sometimes not. For example, C# needs to recompile all the .cs files as soon as any of them changes, because it cannot see in advance which module requires which information. With C++, if no header is changed, only recompilation of the changed .cpp files is needed. From the other side, C++ requires the developer to understand which headers he needs to include.\n- @Jason: predeclaration is needed sometimes, because C++ reads the input in 1 pass. So in order to reference something declared later on in the same file, you need to make a forward declaration. However I don't see this as a complicated problem for a decent developer.\n- @Vlad: I agree with you - indeed, predeclaration is easy to code, and has some advantages - these are probably two of the main reasons nobody has bothered to write a 2-pass C++ compiler. (There simply isn't enough demand to justify the enormous cost)\n- Excellent summary. Reminded me of the \"good old days\" compiling a C program on a 2 floppy drive 640K PC - Took about 10 minutes with a half dozen or more floppy changes. All that for a program containing no more than a couple hundred statements! And thought I was in heaven with all that power.\n- Great answer, it's always nice to get some historical perspective for us younger people!\n- Your guess would be incorrect.","metadata":{"transformedAt":"2026-08-18T18:32:17.770Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":98,"estimatedTokens":3403}}189{"id":"stack-3180456","source":"stackoverflow","questionId":3180456,"title":"Java compiler/interpreter","tags":["java","compiler-construction","terminology"],"text":"Title: Java compiler/interpreter\nTags: java, compiler-construction, terminology\nSource: Stack Overflow\n\nQuestion:\nWhy we do we say that Java is a compiled and interpreted language?\n\nWhat is the advantage of this (being compiled and interpreted)?\n\n========================================\n\nTop Answer:\nWhy do we say Java is compiled and interpreted language.\n\nBecause source code (`.java` files) is *compiled* into bytecode (`.class` files) that is then *interpreted* by a Java Virtual Machine (also known as a JVM) for execution (the JVM can do further optimization but this is anoher story). \n\n What is the advantage over this(being compiled/interpreted)\n\nPortability. The **same** bytecode can be executed on any platform as long as a JVM is installed (\"compile once, run anywhere\").\n\n========================================\n\nCode:\n```text\n.java\n```\n\n```text\n.class\n```\n\n========================================\n\nComments:\n- the answer is very abstract .Can u please elobrate\n- \"Interpreted languages like PHP are completely system independent\" -- not entirely true: `pow(2,36)` will give `6.87194767360e+10` on a 32-bit system and `68719476736` on a 64-bit system.\n- A minor nitpick - but byte code is machine language. There is nothing stopping you from having a physical machine that implements the JVM specifications. In that case, the byte code would be directly executed on hardware and there would be no need for a software layer or interpretation.\n- @amphetamachine Fair enough, how about \"effectively\"?\n- @emory - true, very true. I simplified it for the sake of explanation, I'll edit to be more correct\n- awesome simple explanation.","metadata":{"transformedAt":"2026-08-18T18:32:17.770Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":40,"estimatedTokens":412}}190{"id":"stack-3513780","source":"stackoverflow","questionId":3513780,"title":"Has the use of C to implement other languages constrained their designs in any way?","tags":["c","programming-languages","stackless","compiler-construction"],"text":"Title: Has the use of C to implement other languages constrained their designs in any way?\nTags: c, programming-languages, stackless, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIt seems that most new programming languages that have appeared in the last 20 years have been written in C. This makes complete sense as C can be seen as a sort of portable assembly language. But what I'm curious about is whether this has constrained the design of the languages in any way. What prompted my question was thinking about how the C stack is used directly in Python for calling functions. Obviously the programming language designer can do whatever they want in whatever language they want, but it seems to me that the language you choose to write your new language in puts you in a certain mindset and gives you certain shortcuts that are difficult to ignore. Are there other characteristics of these languages that come from being written in that language (good or bad)?\n\n========================================\n\nTop Answer:\nI tend to disagree.\n\nI don't think it's so much that a language's compiler or interpreter is implemented in C &mdash; after all, you can implement a virtual machine with C that is completely unlike its host environment, meaning that you *can* get away from a C / near-assembly language mindset.\n\nHowever, it's more difficult to claim that the C language *itself* didn't have any influence on the design of later languages. Take for example the usage of curly braces `{ }` to group statements into blocks, the notion that whitespace and indentation is mostly unimportant, native type's names (`int`, `char`, etc.) and other keywords, or the way how variables are defined (ie. type declaration first, followed by the variable's name, optional initialization). Many of today's popular and wide-spread languages (C++, Java, C#, and I'm sure there are even more) these concepts with C. (These probably weren't completely new with C, but AFAIK C came up with that particular mix of language syntax.)\n\n========================================\n\nCode:\n```text\n{ }\n```\n\n```text\nint\n```\n\n```text\nchar\n```\n\n========================================\n\nComments:\n- I can't believe no one has mentioned Haskell. The hugs interpreter is written in C, and Haskell is as far off from C as it gets.\n- Couldn't you make an analogous question about whether the design of assembly constrains language design? If the base language doesn't directly give you the expressive constructs you want, you bootstrap.\n- You got a point and I upvoted, but note that even lua and perl are still imperative languages. Functional languages (Haskell, ML-derivates, Pure, Clean) usually aren't done in C. I blame this on the fact that those who create such a language already know other functional languages, most of which are waaay better for compiler writing than C.\n- You \"disapprove of c\"? Wow, that seems harsh. While I \"dislike\" and choose not to use Java, I save my disapproval for more heinous things like whale hunting and wearing socks with sandals.\n- @delnan: Check bdonlan's answer. They preprocessed Haskell into C. @AShelly: It's really not the point of the answer.\n- @DeadMG: I know GHC can output C and compile it via gcc, but something different - first, it's only one of several ways of running Haskell, second, GHC itself is written in Haskell.\n- @delnan: So what? You can process Haskell into C. That's the point of my answer.\n- Saying that a language is close to perl is definitely NOT saying that it's far from C. :-) Perl fits into the C worldview as far as I'm concerned. Python, Perl, Ruby, Java are all HEAVILY influenced by C and fit within C's sphere of influence.\n- @DeadMG: You *can* (in theory, and most of the time in practice) compile any language to any other language. That doesn't say anything about either language.\n- @delnan: Except that the destination language does not limit the languages implementable in it. Which is the point of this question.\n- It's very ugly to do something in C which looks like first-class functions to the implemented language, yeah. But same applies for dynamic typing, objects, and a dozen other features C-based languages have anyway.\n- IIRC, C and most other imperative languages are classed as \"ALGOL-like languages\" since they are heavily influenced by ALGOL.\n- Last time I checked, it's the C-family.\n- Both @rmeador and @DeadMG are correct, IMHO. ALGOL has provided some main language concepts (found in languages such as C or Pascal), and C has later provided a popular syntax.\n- Most Java and .NET VMs are written in C or C++.\n- And in which language was the JVM/CLR written? Sun's JVM is C++, I guess same applies for the CLR. Okay, that's one step above C, but still...\n- I don't see any reason why garbage collection in C should be limited to reference counting schemes (even if that's likely, since reference counting might be easiest to implement). IIRC there's some good GCs for C, e.g. the one by Hans Boehm.\n- ++ @stakx for mentioning Boehm. It's conservative (e.g. won't catch 'em all, only most), but doesn't use reference counting.\n- I think it's safe to say that Microsoft's implementation of the CLR (that is, .NET) is written in C++, using the COM architecture.\n- @stakx: I'd put money on the CLR not being written using COM (which was implemented in C).\n- @JeremyP: I didn't mean to say that .NET's execution environment is implemented *like* COM, but that it (at least partly) *uses* COM itself, and that it's mostly written in C++. This seems like a reasonable guess to me; first, because COM can be seen as the CLR's ancestor; second, some people at Microsoft once made public another implementation of the CLI (the Shared Source CLI, aka Rotor), which is written in C++ / C#. I'm sure their source code at the time wasn't fundamentally different from .NET's source code. There's more clues to this, but in all truth, probably noone can tell for sure.\n- ...but maybe if they wrote the *next* C# compiler in C# but compiled with the current written-in-C compiler...\n- @FrustratedWithFormsDesigner indeed that is an option.","metadata":{"transformedAt":"2026-08-18T18:32:17.770Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":57,"estimatedTokens":1527}}191{"id":"stack-3122622","source":"stackoverflow","questionId":3122622,"title":"Why is creating a 64bit Delphi so hard?","tags":["delphi","compiler-construction","64-bit"],"text":"Title: Why is creating a 64bit Delphi so hard?\nTags: delphi, compiler-construction, 64-bit\nSource: Stack Overflow\n\nQuestion:\nThe Internet is full of developers requesting a 64bit Delphi, and users of Delphi software requesting 64 versions. \n\n- delphi 32bit : 1.470.000 pages\n\n- delphi 64bit : 2.540.000 pages :-)\n\nThat's why I've been wondering why Embarcadero still doesn't offer such a version. \n\nIf it was easy to do, I'm sure it would've been done a long time ago already. So what exactly are the **technical** difficulties that Embarcedero need to overcome? \n\n- Is it the compiler, the RTL/VCL, or the IDE/Debugger?\n\n- Why is the switch from 32bit to 64bit more complicated than it was for Borland to switch from 16bit to 32bit?\n\n- Did the FPC team face similar problems when they added 64bit support?\n\n- Am I overseeing something important when I think that creating a 64bit Delphi should be easier than Kylix or Delphi.Net?\n\n========================================\n\nTop Answer:\nIf it weren't for the limitation on shell extensions (I have one that loads into Windows Explorer), I'd probably never care about 64. But due to this limitation, I need it, and I need it now. So I'll probably have to develop that part in Free Pascal. It's a pity, as aside from this, there are few apps that would actually benefit from 64. IMO, most users are either drinking the coolaid, or are angry about having been duped into buying something that sounded great but turned into a headache. I know a guy who is happy to run Win7/64 so he has enough RAM to run a full copy of XP in a VM, which he wouldn't need if he'd gotten Win7/32 like I told him too. :-<\n\nI think everyone has been duped by the HW manufacturers, particularly the RAM dealers who would otherwise have a very soft market.\n\nAnyway, back to the question at hand... I'm caught between a rock and a hard place. My customers are placing demands on me, due to an architecture decision from M$ (not allowing 32-bit DLLs in Windows Explorer) and perception issues (64-bit must be twice as good as 32, or maybe 32 has to run on the \"penalty core\" or something). So I'm being driven by a largely \"artificial\" motivation. And therefore, I must project that onto Embarcadero. But in the end, the need for 64-bit support in Delphi is IMO, mostly based on BS. But they're going to have to respond to it, as will I.\n\n========================================\n\nComments:\n- To the people that try to close this question: I'm not trying to start a political discussion or something. I'm interested in the technical problems that Embarcadero is facing.\n- it probably would help if you made this a CW. There most likely is not **one** correct answer to this question. (I did not vote to close btw).\n- Even though I agree that a 64 bit delphi would be most welcome, I think the logical reason for more pages from a search for 64bit delphi over 32bit delphi, is the explicit addition of the 32bit word. Why would people write pages about *32bit* delphi and not just delphi in 2010. A more fair comparison would be delphi 64bit compared against all pages refering to delphi, that is not about 64bit.\n- @TommyA: you're completely right of course.. The number of pages was not really meant to be serious though (see TSmiley instance). Everybody knows that Google pagecounts mean shit, right? Create a programming language called \"extreme\" or \"sloppy\" to get on top of the Tiobe index :-)\n- This strikes me as something that should be in an email sent to CodeGear...\n- Building something from scratch and converting something into something else - is complete different thing. The main problem with Delphi is that internals of the compiler is not prepared for such change, as other already mentioned.\n- Free Pascal is not 100% compatible with your existing Delphi code. Nor is the LCL 100% compatible with your VCL forms.\n- I find it strange that FreePascal must be 100% compatible to be used instead of Delphi. But on the other hand people are more likely to port their Delphi code to C/C++. What needs more work?\n- I strongly disagree about the MacOS being more important. The problem is that they are focusing on new customers rather than current customers. By not making 64bit job #1 they are telling customers \"you can't trust us to keep up\". This was a very bad move on their part.\n- Exactly: they are focusing in new customers (is the first step into a whole new market in fact). Why? Well, new customers always give more money than existing ones heh. And more money is what Embarcadero wanted when buying CodeGear to the old Borland. So, seems that in their strategy going MacOSx is more profitable in the short term. Of course this is my pure speculation, but I think it fits the reality very well. If you ask me, I think they should put public a 64-bit compiler of Delphi available for the current customers, without an IDE in the meanwhile, even without the VCL. Its a hot topic.\n- You know how many customers you have for sure - and you should try to keep them -, you do not know how many new ones you will gain. Remember they are not rolling out a full native IDE and library for MacOSX: they built a cross-compiler (which requires a Windows license to run the IDE) and a Qt-based library that will build apps with a MacOSX look and feel, but not fully native applications. Let's see how many new customers that approach will gain, especially after the Kylix failure still spotting reputation. And how many they will lose delaying 64 bit over and over.\n- Yes, but with gaining new market I doesn't mean MacOSx native developers, they have their tools in their platform and won't migrate easily. I more mean to open a new market with RAD capabilities in a platform that is growing, that should attract a lot of native developers working in -for example- Visual-C++. Of course the Qt approach may be is not the best, but is a good start at least, and that allows you to have the almost the same code for both platforms. The key for Embarcadero is to grow as a player in the native development market, that said Wayne Williams in an interview at least.\n- No, there was no difficulty with porting the existing compiler to 64-bit, there was a CHOICE to rewrite the compiler in such a way as to support multiple back-end compilers - specifically Mac and Linux as well as 64-bit x86.\n- @someone: Windows native developers have their tools too (VC++), that means that Delphi shouldn't be a Windows native tool? Why it should attract VC++ developers willing to code for MacOS? If Embarcadero wants to grow in the native development market, it has to deliver state-of-the-art native solutions, not cobbled up ones. Hampering the actual Windows product while trying to target new markets is stupid: why people should buy a tool that can't keep pace even on its original platform? This way the only customers can be actual Delphi customer trying to target MacOS, IMHO very few new ones\n- Typical conversation at work: `Coworker:` \"Why do you want me to build a 32bits version of my c++ dll?! The server OS and all the software on it is 64bit.\" `Me:` \"uh.. well.. you know.. I use Delphi.. it doesn't support 64bit, so if our software needs to interact, your stuff needs to be 32bits too\". `Coworker:` \"dude, wtf?\"\n- Shell extensions is not the only reason that 64-bit is needed, there are apps that need to address > 2GB of ram, such as ours. We had no choice but to rewrite parts of our application in fpc.\n- The need for 64 bit Delphi is a very technical one indeed for the company where I work. We routinely load 90GB+ models in memory and we currently have to turn down clients with larger data models because we cannot support it in a 32bit Delphi app. We need the extra addressing space that 64bit offers.\n- Alan/Marjan: you can already address 3 GB in 32bit if you set the large address aware flag (it's not as much as in x64 of course but at least it's a 50% increase).\n- \"due to an architecture decision from M$ (not allowing 32-bit DLLs in Windows Explorer)\" - it's not MS decision - x64 architecture does not allow this.\n- I'm astonished. 8 up votes and no down votes at the time of writing when you completely ignored the question and just gave your personal opinions on 64 bit software in general?\n- Amazing how the talk about 64bit, seems to be about RAM only. I know it's one of the big factors, but let us not forget all the other advantages of the amd64 technology. Yes pointers take double the amount of memory (as from 16 to 32bit), and you likely do not need more than 3Gb for your software. But since the amount of registers in the processor is doubled (giving 3 times as many to work with), and the addition of several instruction wise improvements which this comment is too short to hold, I think it would be fair to assume that there are several good reasons for a user to work with 64bit.\n- @Chris Thornton: There's no downside to 64bit, it runs 32bit apps entirely natively. There's no need to buy 32bit, since all modern desktop/laptop CPUs are 64bit and all 32bit apps work fine in 32bit emulation. There's no reason NOT to get 64bit. Plus, then I'd only be able to use about half my RAM. Plus, the way you talk about M$, I think that you have NFI what you're talking about. @Remko: That flag doesn't always work- it can introduce kernel-mode instability and failure. The existence of the 3GB flag is not a guarantee that it actually works.\n- In a 64-bit Delphi the standard integer type would be 64-bit, right? That is one good thing. I have encountered situations in which I need to use 64-bit integers simply because 4294967296 is a too small upper bound. 18446744073709551615 is much better.\n- @DeadMG - are you running the 64-bit version of IE, or are you running 32-bit IE? If you're running 32, why?\n- @DeadMG: you first write \" 64bit, it runs 32bit apps entirely natively\" then \"all 32bit apps work fine in 32bit emulation\". The truth AFAIK is WOW64 translates 32 bit calls into 64 bit ones, thereby 32 bits are not run \"entirely natively\". WOW64 is efficient, but some would like to start to code really \"natively\". @Rejbrand: Windows uses the LLP64 model, where C long type will be still 32 bit long, it looks Delphi will with something alike and Cardinal/Integer will be 32 bit still. Use Int64 if you need a larger integer.\n- @Andreas Rejbrand - no, most likely, integer would be 32-bit. But NativeInt would become 64-bit, since SizeOf(NativeInt) = SizeOf(Pointer).\n- @Chris Thoron: I run Chrome. @Idsandon: They're close enough. There's no safety or real performance difference or coding difference required. You get a 32bit app and under x64, it works identically. There's \"native\", and there's, \"I want to code in machine language\".\n- @Remko on 64-bit windows, large address aware applications get the full 4g, not just 3g.\n- @DeadMG: large address aware or 3GB switch will not make the kernel instable, it will however divide the kernel space in another and limits certain memory pools eg (non)paged pool in favour of user mode space. Regarding the size of integer or cardinal: (T)Handles are an issue in the current state since in C they are pointer (which will become 64 bits) and in Delphi a cardinal, which most likely will remain 32 bits. So a change is required here.\n- Yep, that's the one. Though it promised a 64-bit preview compiler for mid-2009 - that is the one that is now promised for mid-2011...\n- Its late 2012 now. Our customers are waiting for a 64-bit version for some years now. And we seriously considering porting all software to C++.\n- @GameCat: and I think there lies the issue, the tradeoff is: is embarcadero gaining more customer by introducing new platforms or loosing more because of not (fully) supporting the existing one!\n- this is the price of using Delphi. anybody who could port everything to C++, should just do so. But that's insane. Any decent programmer with 20 \"person-years\" of source code in ObjectPascal going into even ONE of their major projects, is not going to leave behind Delphi/Pascal, without setting themselves back at least several years of work. Free Pascal is not a useful option for most people, either.\n- Unfortunately I can only give you one up-vote for this. Showing customers that you can't depend on them is absolutely horrible for business. I would be quite reluctant to do a long-term project in Delphi at this point even though I think it's an excellent language.","metadata":{"transformedAt":"2026-08-18T18:32:17.770Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":69,"estimatedTokens":3101}}192{"id":"stack-8262409","source":"stackoverflow","questionId":8262409,"title":"Is there a lolcode compiler for x86 or x86_64?","tags":["compiler-construction","lolcode"],"text":"Title: Is there a lolcode compiler for x86 or x86_64?\nTags: compiler-construction, lolcode\nSource: Stack Overflow\n\nQuestion:\nOn the lolcode website I saw many references to various lolcode interpreters. But is there a lolcode compiler for the x86 or x86_64 architecture, that compiles directly to native machine code?\n\nI have looked at several interpreter implementations, including Lci, lolcode.net (compiler to .NET), and lolcode interpreters in Python, Java and JavaScript. None of these do what I want.\n\nPart of my rationale for asking is that if no such compiler exists, I may be interested in working on one, possibly by writing a LOLCODE -> C translator, and then leveraging a C compiler such as GCC to target native architectures.\n\n========================================\n\nComments:\n- Why the downvote? Lolcode may seem like a joke to you (whoever downvoted), but it is a programming language with nearly 20 implementations!\n- There is a compiler to .NET on the implementation list, but you are looking for a compiler to machine code, right? A lolcode interpreter in lolcode is missing too :)\n- I am aware of the compiler which targets the .NET. :)\n- The fact that lolcode is a joke, doesn't make it any less a programming language. On-topic, IMO.\n- @Patrick, the C interpreter for LOLCODE is pretty good.\n- @GoranJovic: that is exactly my point.\n- @DejanLekic: Can you show us what *you* have found out so far?\n- phresnel, I've found interpreters in all mainstream languages, compiler to .NET, but no compiler native code. I was thinking that perhaps the best would be to do a LOLCODE -> C translator, and then use a C compiler like GCC to target anything...\n- @phresnel: I would not ask this question on StackOverflow if a search engine gave me the answer... The second reason I ask this question is a research - if there are no compilers, I may decide to work on one.\n- @DejanLekic: Mention what you have tried. Or do you want us to make the same mistakes? It is not mockery, rather we want you to help us :)\n- The first (and the best) I tried is Lci ( icanhaslolcode.org ). I also tried the lolcode.net (.NET), lolcode interpreter in Python, Java and JavaScript.\n- Just as a suggestion if you eventually decide to write your own compiler: Try out LLVM, it makes writing compilers for new languages so easy - there's even a tutorial somewhere on the site on how to build a mini-compiler. Definitively sounds like fun!\n- lolcode.com/implementations/implementations That's your best resource. There's a 1.2 Java implementation that has a heck of a lot of functionality (and, being Java, would use x64 if your JVM is there). I actually used it for a heck of a lot of scripting and it was relatively elegant (and fun). Stupidly, LOL -> OpenJDK (GNU) -> Native actually works.\n- CAN I HAS LOLCODE S.HIGHLIGHT ON SO?","metadata":{"transformedAt":"2026-08-18T18:32:17.770Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":28,"estimatedTokens":706}}193{"id":"stack-679815","source":"stackoverflow","questionId":679815,"title":"Haskell - How to best to represent a programming language's grammar?","tags":["compiler-construction","haskell","grammar","representation"],"text":"Title: Haskell - How to best to represent a programming language's grammar?\nTags: compiler-construction, haskell, grammar, representation\nSource: Stack Overflow\n\nQuestion:\nI've been looking at Haskell and I'd quite like to write a compiler in it (as a learning exercise), since a lot of its innate features can be readily applied to a compiler (particularly a recursive descent compiler).\n\nWhat I can't quite get my head around is how to represent a language's grammar in a Haskell-ian way. My first thought was to use recursive data type definitions, but I can't see how I use them to match against keywords in the language (\"if\") for example.\n\nThoughts and suggestions greatly appreciated,\n\nPete\n\n========================================\n\nTop Answer:\nYou represent programs using mutually recursive algebraic data types, and to parse programs you use **parsing combinators**. There are a million flavors; you will find three helpful tutorial papers on the schedule for my class for Monday, March 23, 2009. They are\n\nGraham Hutton and Erik Meijer, *Functional Pearl: Monadic parsing in Haskell* (1998)\nGraham Hutton, *Higher-order Functions for Parsing* (1992)\nJeroen Fokker, *Functional Parsers* (1995)\n\nThe Hutton and Meijer paper is the shortest and simplest, but it uses monads, which are not obvious to the amateur. However they have a very nice grammar of and parser for expressions. If you don't grok monads yet, Fokker's tutorial is the one.\n\n========================================\n\nCode:\n```text\nexpr ::= var\n      |  \"true\"\n      |  \"false\"\n      |  \"if\" expr \"then\" expr \"else\" expr\n      |  \"(\" expr \")\"\n```\n\n```text\nif true then x else (if false then y else true)\n```\n\n```text\ndata Expr = Var String\n          | Lit Bool\n          | If Expr Expr Expr\n```\n\n```text\nparse \"if x then false else true\" \n  ==  If (Var \"x\") (Lit False) (Lit True)\n```\n\n```text\nx\n```\n\n```text\nVar \"x\"\n```\n\n```text\ntrue\n```\n\n```text\nLit True\n```\n\n========================================\n\nComments:\n- With what little I know, I'd recommend looking at en.wikibooks.org/wiki/Write_Yourself_a_Scheme_in_48_Hours (it uses Parsec etc.) but I'm not leaving it as an answer because it's quite possibly a terrible one. Hopefully someone who knows better will answer (and then I'll delete this comment).\n- The Dragon Book is indeed a classic, but I've heard that current thinking about compiler implementation has passed it by (e.g., JIT runtime optimizations, etc). I'm no authority myself, just a shameless passer of innuendo. 8)\n- Thanks for your response, I'm looking for advice more specific to Haskell, and I'd rather not use a tool (I'm trying to learn not have a tool do it all for me :) ).\n- Writing compilers in Haskell is quite a different (and more pleasant) exercise compared to writing compilers in imperative languages. [And @duffymo: 'innuendo' probably doesn't mean what you think it means :)]\n- Awesome..links. You are the Prog language guru around here. Thanks\n- Ironically, I think learning about parser combinators is the best way to understand monads.\n- The problem with parser combinators is they do not usually support left recursive grammars...\n- @CallumRogers the same is true of handwritten recursive-descent parsers. Niklaus Wirth showed the way years ago: instead of left recursion, use sequences.","metadata":{"transformedAt":"2026-08-18T18:32:17.770Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":77,"estimatedTokens":827}}194{"id":"stack-4385160","source":"stackoverflow","questionId":4385160,"title":"How does a Haskell compiler work?","tags":["compiler-construction","haskell","language-implementation"],"text":"Title: How does a Haskell compiler work?\nTags: compiler-construction, haskell, language-implementation\nSource: Stack Overflow\n\nQuestion:\nWhere can I get some paper/doc/whatever which describes how a Haskell compiler actually works? I read quite a few of the docs of GHC, but stopped after getting a headache. So, something which doesn't require a PhD to understand it and isn't written in the You're-supposed-to-be-already-familiar-with-it style would be preferable. It's not a problem if it's really long and takes some time to understand it though.\n\nPS: Most interesting would be something about GHC, but anything is ok.\n\n========================================\n\nTop Answer:\nAre you looking for details especially about compiling lazy-evaluation? There is Simon Peyton-Jones's book mentioned by Max Bolingbroke, also the book detailing Clean's implementation is online:\n\nhttp://wiki.clean.cs.ru.nl/Functional_Programming_and_Parallel_Graph_Rewriting\n\nIf you have a university affiliation and want something smaller you could try to get these books (Henderson & Diller are certainly out of print):\n\nAntoni Diller \"Compiling Function Languages\" ISBN 0 471 92027 4\n\nPeter Henderson \"Functional Programming Application and Implementation\" ISBN 0-13-331579-7\n\nAJT Davie \"An Introduction to Functional Programming Systems using Haskell\" ISBN 0 521 27724 8\n\nDiller has a full compiler for a lazy language (implemented in Pascal) via combinator reduction. This was the implementation technique invented by David Turner for SASL. Henderson has many parts of a compiler for LISPkit a miniature, lazy variant of Lisp. Davie details quite a bit of the machinery for compiling a lazy language, for instance there's a description of the STG thats much shorter than Simon Peyton-Jones's book (the STG is the abstract machine SPJ used for Haskell).\n\nThe Clean developers have quite a bit of info on implementing SAPL (a Simple Applicative Language) if you look through their publications list:\n\nhttps://clean.cs.ru.nl/Publications\n\nFinally there are quite a number of papers documenting aspects of the Utrecht Haskell Compiler UHC (and EHC). I think most of the information is how the compiler is organized (with attribute grammars and \"Shuffle\") and how the type systems (there are various levels of type system in EHC) are implemented, rather than how the back-end 'compilation' works.\n\n========================================\n\nComments:\n- Excellent question. I'd like to know particularly whether CPS transform a la scheme is used or not. I strongly believe it is \"the\" way to implement functional languages, but I may be overlooking the difficulties.\n- If you want a grasp on what CPS is, the excellent \"lambda the ultimate goto\" paper from the scheme author is a great read.\n- Not exactly what you're asking for, but I'd recommend looking at compilers other than GHC to start. JHC's source is extremely readable, and the UHC code has a lot of good theoretical documentation; either of these would be easier going than GHC.\n- @John: Good point, I already tried but found out, that you're totally lost as soon as you don't really understand what the programer tries to do.\n- @Alexandre C. Where can I get it?\n- @FUZxxi: you didn't even try google, did you ?\n- @Alexandre C: FOr the question it self, I did. But not for your comment. I should probably do.\n- The question was answered, but not some comments. To set the record straight: no, @AlexandreC, a CPS transformation is not involved, instead, lambda lifting is used to transform the program to a set of supercombinators to make compiled graph reduction possible. It is possible to argue that there is an equivalence between CPS and the STG. And Scheme, today, counts as an imperative language. No prior PhD is necessary, but the full answer to the question is more work than a PhD (not original, though).\n- One of the best papers on that subject that I have read is: The Glasgow Haskell Compiler by Simon Marlow and Simon Peyton-Jones. The Architecture of Open Source Applications. aosabook.org/en/ghc.html\n- I guess you're right. It's just that I want to understand what's going on there. IMHO it's easier to understand what your program is doing, if you actually know how it gets compiled.\n- You answer isn't very helpful. Haskell is so different from mainstream languages.\n- @FUZxxl, I sympathise with your point of view; with procedural languages this is very much the case, and especially something like C. However with Haskell the distance from language to machine is much greater, so you spend much more time thinking in terms of the language model. The only time this isn't true is when thinking about performance. However understanding the whole compiler isn't a lot of use. Learn about thunks, strictness analysis and the intermediate code emitted by GHC.\n- @Paul Johnson Isn't thunks, strictness analysis and itermediate code one of the majot parts of the compiler? Probably I should have to ask for this instead of about the whole beast itself.\n- What I mean is, you need to learn the concepts rather than the algorithms. Its like, the way to understand square roots is to understand that sqrt(x)^2=x, not by reading about Newton-Raphson\n- Thank you for your comment. Please notice that I asked specially for Haskell, where these steps are a bit more sophisticated, eg. Haskell allows new operators with arbitrary precedence which turn parsing into a guesswork, type-inference and classes, which IMHO are making the analysis of what going on quite a bit challenging and a big bunch of optimizations just to get rid of laziness. But thanks for your clear answer!\n- And don't forget the stg paper -- \"Implementing Lazy Functional Languages on Stock Hardware\": research.microsoft.com/apps/pubs/default.aspx?id=67083\n- And the videos linked on the GHC developers' wiki.\n- Link-only answers are discouraged on this site. If you just want to point out a resource, please make a comment instead.\n- Okay, sorry! Just wanted to help with a good article on that question. I have just added comment with content of this answer.\n- Cool! Thank you for your cooperation.","metadata":{"transformedAt":"2026-08-18T18:32:17.771Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":55,"estimatedTokens":1529}}195{"id":"stack-1961604","source":"stackoverflow","questionId":1961604,"title":"How much time would it take to write a C++ compiler using flex/yacc?","tags":["c++","compiler-construction","bison","yacc","flex-lexer"],"text":"Title: How much time would it take to write a C++ compiler using flex/yacc?\nTags: c++, compiler-construction, bison, yacc, flex-lexer\nSource: Stack Overflow\n\nQuestion:\nHow much time would it take to write a C++ compiler using lex/yacc?\n\nWhere can I get started with it?\n\n========================================\n\nTop Answer:\nIt will probably take you **years**, and you'll probably switch to some other parser generator in the process.\n\nParsing C++ is notoriously error-prone. The grammar is not fully LR-parsable, as many parts are context-sensitive. You won't be able to get it working right in flex/yacc, or at least it'll be really awkward to implement. There are only two front-ends I know of that get it right. Your best bet is to use one of these and focus on writing the back-end. That's where the interesting stuff is anyway :-).\n\nExisting C++ Front Ends:\n\nThe **EDG front-end** is used by most of the commercial vendors (Intel, Portland Group, etc.) in their compilers. It costs money, but it's very thorough. People pay big bucks for it because they don't want to deal with the pain of writing their own C++ parser.\n\n**GCC's C++ front-end** is thorough enough for production code, but you'd have to figure out how to integrate this into your project. I believe it's fairly involved to separate it from GCC. This would also be GPL, but I'm not sure whether that's a problem for you. You can use the GCC front-end in your project via gcc_xml, but this will only give you XML for classes, functions, namespaces, and typedefs. It won't give you a syntax tree for the code.\n\nAnother possibility is to use **clang**, but their C++ support is currently spotty. It'll be nice to see them get all the bugs out, but if you look at their C++ status page you'll notice there are more than a few test cases that still break. Take heed -- clang is a big project. If it's taking these guys years to implement a C++ front-end, it's going to take you longer.\n\nOthers have mentioned **ANTLR**, and there is a C++ grammar available for it, but I'm skeptical. I haven't heard of an ANTLR front end being used in any major compilers, though I do believe it's used in the NetBeans IDE. It might be suitable for an IDE, but I'm skeptical that you'd be able to use it on production code.\n\n========================================\n\nCode:\n```text\n>>\n```\n\n```text\nC++ LEX  Tokens:   http://www.computing.surrey.ac.uk/research/dsrg/fog/CxxLexer.l\nC++ YACC Grammer:  http://www.computing.surrey.ac.uk/research/dsrg/fog/CxxGrammar.y\n                   http://www.computing.surrey.ac.uk/research/dsrg/fog/CxxTester.y\n```\n\n========================================\n\nComments:\n- Good luck. (And @Neil, the new tag is (incorrectly) `gnu-flex` or, alternately, `lex`)\n- You can start with reading Software Estimation by Steve McConnell of Code Complete fame.\n- some help : Let's Build a Compiler, by Jack Crenshaw at compilers.iecc.com/crenshaw\n- IMHO it's Adobes problem that they picked a name for their product that is already widely used.\n- Well, on SO it our problem. I suspect the number of people using Adobe Flex (not me, I hasten to add) greatly exceeds the users of the flex tool - who's name is not copyrighted or trademarked, as far as I know.\n- @Nils - I agree, but the discussion on Meta shows that the consensus is for the new technology that will be gone in 5 years rather than the tried-and-true niche program that's been around nearly forever. Meta discussion around it (started by me. I'm famous!): meta.stackexchange.com/questions/23959/&hellip;\n- +1 for mentioning LLVM. I use it for my back end. Great stuff.\n- A compiler does not inately need a linker, assembler or preprocessor. I once wrote a small C compiler that didn't need either.\n- If I am not mistaken, gcc switched to using a recursive descent parser sometime late in the 3.x series.\n- gcc uses lex and yacc for the C++ front-end. For all the ambiguities you mention there are explicit commands to resolve the conflicts. Personally I doubt there is a better framework for parsing C++ out there. **BUT** writing a C++ lexer/parser without lots of compiler experience is a non starter for a single developer using lex/yacc (its just to large and complicated).\n- @Martin York, in fact the bison/yacc parser was replaced with a recursive descent parser in gcc-3.4 - gcc.gnu.org/gcc-3.4/changes.html\n- Recursive descent parsers are also much, much easier to understand. In fact, if you're interested in language development, I'd probably recommend starting with hand-generating a recursive descent parser for a relatively simple grammar.\n- @Martin, yacc is an interesting tool but the bigger the project, the more work it is to get yacc to DTRT. For a simple problem, every approach is easy, including yacc. For a tough problem, like C++, the choices are: use something more modern like a PEG tool, fight a lot with yacc and stir in some strange hacks, or just type in a straightforward RD parser. Personally, I would rather write code (RD) than bash my head against yacc, but I've done it both ways...never for C++ tho\n- @kyoryu: Recursive descent parsers are NOT that much easier to understand than the pure grammar, especially for an artifact of the scale of C++. You really want a parser generator driven from the language definition in terms of BNF rules. The people that say C++ is hard to parse are those that use YACC (and variants) and those that tangle name/type resolution with parsing. GLR parser generators allow you to build a very good parser using the BNF rules, and isolate name/type resolution; this separate makes each task a lot easier (although not easy). See my answer here.\n- @Ira: I was comparing recursive descent parsers to LALR parsers. Also, with parsers, writing at least a trivial one by hand can give you insight into what a parser generator is doing. I've also argued against entering the world of parsers/compilers with C++, just because (as you rightly point out) it is an incredibly complex language.\n- I completely disagee. As do the gcc team. Where the C++ front-end is lex and yacc.\n- Martin: it's not. \"the bison/yacc parser was replaced with a recursive descent parser in gcc-3.4\"\n- Martin: it's certainly possibly to use yacc, and gcc has done it both with and without. Ruby has a complex grammar and the main implementation does use yacc. I've written parsers both ways, certainly there are no easy \"always do it this way\" answers, I just think it's important to realize that the parser will be roughly the same amount of effort either way. The real problem with yacc is that while easy stuff is really easy, you can also get stuck on hard-to-understand bugs. With RD you just fix the code.\n- I think the main point is not whether or not flex/yacc are good tools, but to point out that they're a pretty small portion of the overall problem. Great, you've parsed a file into some intermediate representation (AST/whatever) - *now what?*\n- gcc_xml parses everything except the code, so it's not useful for a compiler. You only get function and type declarations.\n- Nice if you can afford this, Ira, are you affilitated with this crowd at semanticdesigns? stackoverflow.com/questions/526797/&hellip;, stackoverflow.com/questions/792454/&hellip;\n- I *am* the crowd at Semantic Designs. Check my bio here where this is clearly stated. Agreed, nice if you can afford it. The alternative (build the whole thing yourself) is nice if you can afford it, too, but you *can't*; neither you nor your employer can afford for you to spend the huge amount of time it takes to build such tools. And it makes even less sense if you intend to do it as a hobby, unless its a life-long task. A question of the form of \"how do implement a simple compiler\" wouldn't draw this response.","metadata":{"transformedAt":"2026-08-18T18:32:17.771Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":64,"estimatedTokens":1939}}196{"id":"stack-7991877","source":"stackoverflow","questionId":7991877,"title":"Why is an interpreter slower than a compiler in practice?","tags":["compiler-construction","interpreter"],"text":"Title: Why is an interpreter slower than a compiler in practice?\nTags: compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nDon't they both have to convert to machine code at some point to execute or am i missing something more basic?\n\n**EDIT:** \n\nPlease consider more sophisticated interpreting schemes e.g. cases where the code is translated to Byte code and the byte code is only regenerated when source code changes e.g. CPython implementation of Python?\nI am not really interested in ancient interpreters that re-execute line by line....\n\nThanks!\n\n========================================\n\nTop Answer:\nOK, a lot of incorrect posts here, time for a long answer.\n\nA compiler is basically clear - it translates a program form the source language to the target language. Both languages can be whatever - high level language, virtual machine bytecode, machine code.\n\nAn interpreter, on the other hand does not perform a *translation*, but *directly performs* the actions, prescribed by the source language construct, a.k.a. interprets it.\n\nLet's consider a hypothetical `add` instruction in a stack based machine, which adds the two top elements of the stack and pushes the result back. An interpreter will *directly perform* that \"add the two top elements and push the result back\", in a manner similar to:\n\n```\nswitch (op)\n{\n\n ....\n\n case OP_ADD: \n op1 = pop (stack);\n op2 = pop (stack);\n res = op1 + op2;\n push (stack, res);\n break;\n\n...\n}\n```\n\nAs you can see, for a single `add` insn, there are many operations performed: reading and writing memory, incrementing and decrementing the stack pointer, the add operation itself, the overhead of the switch (if the interpreter is implemented that way), the overhead of the loop which reads each subsequent insn and decides how to process it, etc.\n\nIf the interpeter worked on an AST, it may look like:\n\n```\nswicth (op)\n{\n ...\n case OP_ADD:\n op1 = tree->eval (left);\n op2 = tree->eval (right);\n return op1 + op2;\n ...\n}\n```\n\nAgain, many, many insns to perform whatever is required by the `add` semantics.\n\n========================================\n\nCode:\n```text\nswitch (op)\n{\n\n ....\n\n  case OP_ADD:  \n    op1 = pop (stack);\n    op2 = pop (stack);\n    res = op1 + op2;\n    push (stack, res);\n    break;\n\n...\n}\n```\n\n```text\nswicth (op)\n{\n   ...\n   case OP_ADD:\n     op1 = tree->eval (left);\n     op2 = tree->eval (right);\n     return op1 + op2;\n   ...\n}\n```\n\n```text\nadd\n```\n\n```text\nadd\n```\n\n```text\nadd\n```\n\n========================================\n\nComments:\n- A pure interpreter does not convert to machine code. It may process directly ASTs or convert to virtual machine code (bytecode) and process that.\n- yeah there is an intermediate step...but when you say \"process that\"...what does that mean? does it not have to turn into machine code before it is executed? if not is that why it is slower?\n- @algorithmicCoder, consider a typical stack VM bytecode interpretation: for a sequence `PUSH 2; PUSH 2; ADD` it will update stack pointer three times, perform three costly memory read/write operations, while in a native code you'll most likely stick to the registers. The latter can be 100x times faster, even for such a trivial example.\n- @algorithmicCoder, see my longer post below.\n- How about cases where the code is translated to Byte code and the byte code is only regenerated when source code changes e.g. CPython implementation of Python?\n- It is not nearly a \"*typical*\" way of interpretation. Virtual machines are a much more typical implementation in 21st century. Ruby, Perl, Python, Lua, whatever else - everything is VM-compiled now.\n- yes this is the real source of my confusion...why exactly is Python then said to be slower than say C++ or Java even though it is actually compiled into bytecode...i am guessing the execution of the program after it is turned into bytecode is the tricky part...why is this part slower than when we just compile and run?\n- @algorithmicCoder, Python execution is slower mainly because of its dynamic type system, which makes it virtually impossible to compile it efficiently to a native code. It is the very semantics of the language which makes it \"slow\", not the interpretation.\n- Aha! this is what i suspected. Ok now can you explain briefly HOW the type system comes into play. Do types have to be figured out EVERY time the interpreter runs the byte code? What does this entail? Or is it figure out only when the source code is compiled to byte code?.... sorry if these are basic questions...also i'd appreciate a good book to use in learning these fundamentals!\n- @algorithmicCoder, every time Python is executing, say, `a+b`, it have to get the type of `a`, type of `b` and dispatch the appropriate addition implementation (concatenation for strings, integer addition for integers, etc.). It does not matter if you compile it to native or to a bytecode, the bottleneck will always be in this dynamic dispatch. Of course there are some funny tricks for getting rid of the dynamic typing - e.g., V8 and the other modern Javascript compilers are using a form of an abstract interpretation for specialising types where possible.\n- @SK-logic Very late to the party, but how come semantically similar languages like Julia be this much faster if the main factor are said semantics?\n- @Raumschifffan Julia is only fast when type annotations are used (or when types can be inferred). Otherwise it is too resorting to dynamic dispatch, with a significant performance hit. Same goes for Common Lisp, Scheme and other dynamically typed languages - and most of their implementations are of much higher quality than Python, yet they take a noticable performance hit every time dynamic dispatch is involved.\n- This answer is not correct. Interpreter may not executes line by line and converts each line to machine instruction at run time.\n- True. But, a modern VM implementation won't be that simple: it can be either a direct threaded code (see the OCaml bytecode VM for example), or all the instructions can be inlined. But still, most of the implementations won't bother translating a stack semantics into 3-address.\n- @SK-logic, yes, a modern VM implementation would be a hybrid compiler/interpreter and the interpreter part would be written in either assembler or C with extensions (like computed goto), if it exists at all. Speaking of implementation techniques, I've seen some interpreter, which actually kept the top three or so elements of the operand stack in registers.\n- Modern VMs also do JIT compiling which takes a slight performance hit when compiling but after that runs at the speed of native code (depending on how well the JIT can optimize, of course)\n- +1 for 'While we speak of interpreted and compiled languages, what we are really discussing is the usual execution style of a language.'","metadata":{"transformedAt":"2026-08-18T18:32:17.771Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":124,"estimatedTokens":1705}}197{"id":"stack-875791","source":"stackoverflow","questionId":875791,"title":"How do modern compilers use mmx/3dnow/sse instructions?","tags":["gcc","compiler-construction","x86","sse","assembly"],"text":"Title: How do modern compilers use mmx/3dnow/sse instructions?\nTags: gcc, compiler-construction, x86, sse, assembly\nSource: Stack Overflow\n\nQuestion:\nI've been reading up on the x86 instruction set extensions, and they only seem useful in some quite specific circumstances (eg HADDPD - (Horizontal-Add-Packed-Double) in SSE3). These require a certain register layout that needs to be either deliberately set up, or occur from the series of instructions before it. How often do general-purpose compilers like gcc actually use these instructions (or a subset thereof), or are they mainly to be used in hand-coded assembler? How does the compiler detect where it is appropriate to use SIMD instructions?\n\n========================================\n\nTop Answer:\nMono can use SIMD extensions as long as you use its classes for vectors. You can read about it here: http://tirania.org/blog/archive/2008/Nov-03.html\n\nGCC should do some automatic vectorisation as long as you're using `-O3` or a specific flag. They have an info page here: http://gcc.gnu.org/projects/tree-ssa/vectorization.html\n\n========================================\n\nCode:\n```text\n-O3\n```\n\n========================================\n\nComments:\n- Has intrinsics become (a lot) better the last few years? Last time I checked, both MSVC and ICC had quite lousy register allocation, and even I was easily able to beat the compiler-intrinsic version with hand-coded assembly.\n- I believe recent versions of MSVC have made *some* improvements to intrinsics-generated code. But I don't know how much difference that has made.\n- MSVC's output for scalar SSE is still just terrible, especially if you use an intrinsic anywhere.\n- 2020 update: the major ahead-of-time compilers (not JITs) can fairly reliably vectorize simple \"vertical\" operations where the loop body accesses some arrays all with the same index. Like `A[i] = B[i] * x + C[i]` or whatever, for integer or FP. Not gathers or scatters like `A[idx[i]]`. With arrays of different type-widths, or any shuffling or structs, or any more complicated stuff like a serial dependency (e.g. prefix sum) you still often need manual vectorization for best results.\n- Some compilers can even vectorize math library functions like `log` or `exp`, but fast SIMD approximations can be a big win, e.g. if you know you don't care about handling NaN or Inf inputs, and you can accept lower precision output.\n- 64-bit real, aka `double`, benefits from SIMD on any CPU with SSE2, except maybe Pentium-M / Core Solo where 128b vector ops were split into two 64-bit halves, and multi-uop instructions cause decode bottlenecks. On anything after Core2 or AMD K10, SIMD is a clear win for `double` as well.\n- Yup, modern compilers targeting x86-64 freely and liberally use 16-byte loads/stores to copy around structs and to zero things.","metadata":{"transformedAt":"2026-08-18T18:32:17.771Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":31,"estimatedTokens":707}}198{"id":"stack-9099391","source":"stackoverflow","questionId":9099391,"title":"Playing with gcc's intermediate GIMPLE format","tags":["c","gcc","compiler-construction","backend","gimple"],"text":"Title: Playing with gcc's intermediate GIMPLE format\nTags: c, gcc, compiler-construction, backend, gimple\nSource: Stack Overflow\n\nQuestion:\nAccording to this article gcc uses several intermediate formats before generating code. I read that the GIMPLE format uses three address code, which seems to be the easiest intermediate language to use. But I need some more detail, as I need to build a tool that can take the intermediate code and insert some code to it before generating the final code.\n\nFor this I first need to know how can I even generate the GIMPLE format code and save it in a file. So I'm looking for some documents and examples. Also, if anyone has worked with such things, can I know the complexity of this task, which is to insert some code into the intermediate code?\n\n========================================\n\nTop Answer:\nYou can easily generate GIMPLE representation of any file using the flag `-fdump-tree-gimple`.\n\nIf you want to write a plugin, then you might be interested in how passes work on GCC. You can see the output of each pass with flags of the form: \n\n```\n-fdump--\n```\n\nwhere `ir` could be:\n\n- `tree` : Intraprocedural passes on GIMPLE\n\n- `ipa` : Interprocedural passes on GIMPLE\n\n- `rtl` : Intraprocedural passes on RTL\n\nUse ` = all` to see all the dumps, e.g. `-fdump-ipa-all`.\n\n========================================\n\nCode:\n```text\n-fdump-<ir>-<passname>\n```\n\n```text\n-fdump-tree-gimple\n```\n\n```text\nir\n```\n\n```text\ntree\n```\n\n```text\nipa\n```\n\n```text\nrtl\n```\n\n```text\n<passname> = all\n```\n\n```text\n-fdump-ipa-all\n```\n\n```text\n-fdump-tree-gimple\n```\n\n```text\n__GIMPLE\n```\n\n```text\n-fgimple\n```\n\n```text\ngcc -c -fdump-tree-gimple hi.c\n```\n\n```text\nhi.c.004t.gimple\n```\n\n```text\n-dump-tree-gimple\n```\n\n========================================\n\nComments:\n- This is one link, gcc.gnu.org/wiki/GimpleFrontEnd\n- A good example is at gcc.gnu.org/onlinedocs/gcc-4.3.4/gccint/GIMPLE-Example.html\n- if you are looking for a way to generate code, you might look at llvm. At least it is much better documented.\n- Related: stackoverflow.com/questions/1496497/&hellip;\n- I'm the main developer of MELT, and I have added a few days ago new tutorial slides on gcc-melt.org/GCC-MELT-HiPEAC2012.pdf (there are also other stuff of gcc-melt.org ...)\n- gcc-melt.org is dead, new link is starynkevitch.net/basile/gcc-melt/GCC-MELT-HiPEAC2012.pdf\n- @Abex pitty the site went down, I have updated with your link, though next time just (propose an) edit to my answer :)\n- The question is tagged C. So the other languages are of no relevance.","metadata":{"transformedAt":"2026-08-18T18:32:17.772Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":14,"totalLines":100,"estimatedTokens":639}}199{"id":"stack-41899881","source":"stackoverflow","questionId":41899881,"title":"What is a clobber?","tags":["compiler-construction","clang","llvm","inline-assembly"],"text":"Title: What is a clobber?\nTags: compiler-construction, clang, llvm, inline-assembly\nSource: Stack Overflow\n\nQuestion:\nClang `TargetInfo` has a method called `getClobbers`: \n\n Returns a string of target-specific clobbers, in LLVM format. \n\nSo, what is a clobber?\n\n========================================\n\nCode:\n```text\nTargetInfo\n```\n\n```text\ngetClobbers\n```\n\n```text\ngetClobbers\n```\n\n```text\ngetClobbers\n```\n\n```text\n$1\n```\n\n```text\n$1\n```\n\n========================================\n\nComments:\n- Might be worth adding that \"trashed\" here means the assembler code needs to use the registers (a register could be seen as a very fast temporary variable built into the CPU instead of being on a separate RAM chip if you're unfamiliar with assembler), and so will not preserve the values the caller put into them.\n- @uliwitness Thanks, added.\n- i386 and amd64 gcc (and clang I'm pretty sure) implicitly clobber condition codes, `\"cc\"`, in asm statements because many integer instructions unavoidably write them. I know you're not trying to make an exhaustive list, but that's one for a major ISA.\n- @PeterCordes Thank you, that's a good addition to the answer. I haven't yet found code in Clang which does so mentioned only GCC.","metadata":{"transformedAt":"2026-08-18T18:32:17.773Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":45,"estimatedTokens":306}}200{"id":"stack-813926","source":"stackoverflow","questionId":813926,"title":"Statically typed Lua","tags":["compiler-construction","lua","static-typing"],"text":"Title: Statically typed Lua\nTags: compiler-construction, lua, static-typing\nSource: Stack Overflow\n\nQuestion:\nI am looking for a Lua front-end compiler that is type-checked at compile time, but outputs standard Lua 5.1 byte-code (that has only run-time types). What I want is a decent amount of static, compile-time syntactic analysis and optional typing, to detect trivial errors sooner than run-time. The resulting byte-code would have to play nicely with existing Lua byte-code that was compiled with the standard LoadString().\n\nTo be clear -- any difference would only occur at byte-compilation time. At runtime, the byte code would have no idea that anything different/unusual happened to it during the compile phase.\n\nWhat I have in mind sounds a lot like ActionScript; I wouldn't even mind an ActionScript compiler that outputs Lua byte code!\n\nHas anyone heard of such an effort? I've seen some references to using MetaLua to do this, but honestly I am not bright enough to make heads of tails of their documentation\n\n========================================\n\nTop Answer:\nPlease see this Metalua blog post.\n\n```\n-{ extension \"types\" }\n\nfunction sum (x :: list(number)) :: number\n local acc :: number = 0\n for i=1, #x do acc=acc+x[i] end\n return acc\nend\n```\n\nThis is looks like a run-time solution though.\n\nAnyway, feel free to ask your question in Metalua mailing list. If you want to extend Lua syntax, Metalua is the first tool to look at.\n\nP.S. Please never write Lua as all-caps!\n\n========================================\n\nCode:\n```text\n-{ extension \"types\" }\n\nfunction sum (x :: list(number)) :: number\n  local acc :: number = 0\n  for i=1, #x do acc=acc+x[i] end\n  return acc\nend\n```\n\n```lua\n--- @alias recipe_prototype any\n--- @alias recipe_name string\n--- @alias ingredient_name string\n\n--- @class Coordinate\n--- @field x number\n--- @field y number\n\n--- @class Entity\n--- @field entity_number number unique identifier of entity\n--- @field name string entity name\n--- @field position Coordinate\n--- @field direction any defines.direction.east/south/west/north\n\n--- @class BlueprintSection\n--- @field entities Entity[]\n--- @field inlets number[] index of inlets in entities list\n--- @field outlets number[] index of outlets in entities list\n\n--- @type BlueprintSection\nBlueprintSection = {}\n\n--- @return BlueprintSection\nfunction BlueprintSection.new()\n    --- ...\nend\n\n--- @param other BlueprintSection\n--- @param xoff number optional, x-offset of the other section, default to width of self\n--- @param yoff number optional, y-offset of the other section, default to 0\n--- @return BlueprintSection new self\nfunction BlueprintSection:concat(other, xoff, yoff)\n   -- ...\nend\n```\n\n========================================\n\nComments:\n- By the way, you say strong typing, but I think you mean static typing. There is a difference. For example, Python is strongly typed, but dynamically typed. C is weakly typed, but statically typed.\n- I am not looking for LUA.NET. LUA.NET implies some sort of .NET runtime. The .NET runtime is nothing like the LUA ByteCode VM. What I am looking for would only apply strong-typing at byte-compile time, and then run the resulting code blissfully unaware that anything unusually happened to it at compile time.\n- Unfortunately, I am already working with a large (and growing) LUA code base. Due to LUA's nature, changing any existing library code is pretty much impossible. An *optional* strong-typing front end would solve this problem, without necessarily changing the basic feel of the language.\n- @Armentage, Lua is not an acronym, it is a proper noun. It is written \"Lua\", not \"LUA\". See lua.org/about.html#name for the official story.\n- @Armentage: If you want to pursue this in the fall I might be able to find a student who would be interested.\n- We had an intern at my firm build something like this over the summer. Results were interesting... his mentor actually had him build his type-safe lua pre-processor in Haskell...\n- I hear what you're saying; I've been putting a lot of thought into this, and while it seems very simply to cover the most basic cases (i.e. catching local x:int = \"hello\" as an error) things get very difficult when you start worrying about tables or returning typed tuples from functions. But those guys at Adobe figured it out with Javascript!\n- Table and the infinite number of ways they are used is definitely the problem. If you're willing to have lots of annotations things probably get simpler.\n- Norman, knowledge should be shared. I for one would be very interested in learning what were the conclusions of your study!\n- @Armentage: Note that another difficulty is that you would need to expose this type system to things like userdata, allowing the creation of new types that can be checked. Remember: the primary purpose of Lua is embedding, so if you have type safety, it needs to combine well with user extensions of the language.\n- The slides for the Dyla14 paper are available: lua.org/wshop14/Murbach.pdf","metadata":{"transformedAt":"2026-08-18T18:32:17.773Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":98,"estimatedTokens":1250}}201{"id":"stack-27568021","source":"stackoverflow","questionId":27568021,"title":"If clang is the front end of a compiler, then why it can produce executable file?","tags":["compiler-construction","clang","llvm"],"text":"Title: If clang is the front end of a compiler, then why it can produce executable file?\nTags: compiler-construction, clang, llvm\nSource: Stack Overflow\n\nQuestion:\nOne thing I really don't understand is about the function of clang, if clang is the front end part of the compiler, it should just do the parser work for the source code, the the remain work will be done by LLVM. But clang can produce executable file too. So how to understand it ?What is the relation between clang and llvm?\n\n========================================\n\nTop Answer:\nLLVM is a compiler backend that was written before clang, which originally used the front end from gcc in a tool called 'llvm-gcc'. Clang is the name of the front end code, but clang is also the name of a tool that includes the clang front end, but will also run the whole compile for you. Later phases of compilation are either built into the clang tool as libraries, or if they are separate executables clang knows how to invoke them. With the right command line arguments, you can make clang stop part way thru\n\n- -emit-ast just does the parse and makes the Abstract Syntax Tree\n\n- -emit-llvm makes the LLVM Intermediate Representation, but not turn it into code for your computer\n\nClang will work as the driver for the whole build because that's what programmers usually want, the soure parsed, the object generated, the executable made. Wanting the Abstract Syntax Tree spit back at you is pretty rare. \n\nObviously this is the souce for all thing LLVM http://llvm.org\n\nHere is a video of Chriss Lattner explaing what LLVM is https://www.youtube.com/watch?v=029YXzHtRy0 . Chandler Carruth has some vids on youtuble explain parts of clang that he has worked on.\n\n========================================\n\nComments:\n- what is the difference between LLVM IR and LLVM?\n- LLVM IR is the intermediate representation that front-ends produce and back-ends transform to a distinct ISA. LLVM is just the name of the project.","metadata":{"transformedAt":"2026-08-18T18:32:17.773Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":27,"estimatedTokens":491}}202{"id":"stack-12177796","source":"stackoverflow","questionId":12177796,"title":"What is the difference between - 1) Preprocessor,linker, 2)Header file,library? Is my understanding correct?","tags":["c","compiler-construction","linker","preprocessor","header-files"],"text":"Title: What is the difference between - 1) Preprocessor,linker, 2)Header file,library? Is my understanding correct?\nTags: c, compiler-construction, linker, preprocessor, header-files\nSource: Stack Overflow\n\nQuestion:\nOkay, until this morning I was thoroughly confused between these terms. I guess I have got the difference, hopefully.\n\nFirstly, the confusion was that since the preprocessor already includes the header files into the code which contains the functions, what library functions does linker link to the object file produced by the assembler/compiler? Part of the confusion primarily arose due to my ignorance about the difference between a header file and a library.\n\nAfter a bit of googling, and stack-overflowing (is that the term? :p), I gathered that the header file mostly contains the function declarations whereas the actual implementation is in another binary file called the library (I am still not 100% sure about this).\n\nSo, suppose in the following program:-\n\n```\n#include\nint main()\n{\n printf(\"whatever\");\n return 0;\n}\n```\n\nThe preprocessor includes the contents of the header file in the code. The compiler/compiler+assembler does its work, and then finally linker combines this object file with another object file which actually has stored the way `printf()` works.\n\nAm I correct in my understanding? I may be way off...so could you please help me? \n\n**Edit:** I have always wondered about the C++ STL. It always confused me as to what it exactly is, a collection of all those headers or what? Now after reading the responses, can I say that STL is an object file/something that resembles an object file?\n\nAnd also, I thought where I could read the function definitions of functions like `pow()`, `sqrt()` etc etc. I would open the header files and not find anything. So, is the function definition in the library in binary unreadable form?\n\n========================================\n\nTop Answer:\nThis is an extremely common source of confusion. I think the easiest way to understand what's happening is to take a simple example. Forget about libraries for a moment and consider the following:\n\n```\n$ cat main.c\nextern int foo( void );\nint main( void ) { return foo(); }\n$ cat foo.c\nint foo( void ) { return 0; }\n$ cc -c main.c\n$ cc -c foo.c\n$ cc main.o foo.o\n```\n\nThe declaration `extern int foo( void )` is performing exactly the same function as the header file of a library. `foo.o` is performing the function of the library. If you understand this example, and why neither `cc main.c` nor `cc main.o` work, then you understand the difference between header files and libraries.\n\n========================================\n\nCode:\n```text\n#include<stdio.h>\nint main()\n{\n      printf(\"whatever\");\n      return 0;\n}\n```\n\n```text\nprintf()\n```\n\n```text\npow()\n```\n\n```text\nsqrt()\n```\n\n```text\nmake\n```\n\n```text\nmake\n```\n\n```text\n#include\n```\n\n```text\nmain()\n```\n\n```text\nmain()\n```\n\n```text\nmain()\n```\n\n```text\nmain()\n```\n\n```text\nmain()\n```\n\n```text\nexit()\n```\n\n```text\n$ cat main.c\nextern int foo( void );\nint main( void ) { return foo(); }\n$ cat foo.c\nint foo( void ) { return 0; }\n$ cc -c main.c\n$ cc -c foo.c\n$ cc main.o foo.o\n```\n\n```text\nextern int foo( void )\n```\n\n```text\nfoo.o\n```\n\n```text\ncc main.c\n```\n\n```text\ncc main.o\n```\n\n========================================\n\nComments:\n- The understanding you mention under the code is correct.\n- and finally an executable or another library is produced.... ;)\n- One explanation (particular to C, but C++ is pretty much the same) can be found in my C tutorial, in the chapter on headers: masters-of-the-void.com/book10.htm\n- @DevSolar except that it isn't. It **is** created from object files, but it's certainly not an object file itself.\n- @H2CO3: shared libraries are often considered object files in their own right.\n- @H2CO3: no, shared libraries. Static libraries are `ar` archives of plain old object files. Shared libraries are \"shared object\" files (`.so` extension on Linux and some other systems).\n- @larsmans I'm aware of that. But technically, despite of having the name \"shared object\", they **are not** object files. They're properly **linked** using the linker, just like an executable and can be opened for direct execution of their contents (e. g, the `dlopen()` API) because they contain location/address info which object files do not have.\n- @H2CO3: `dlopen()` starts with \"dl\" because it's a call to the *dynamic **linking** loader*. Ref. \".dll\" on Windows (dynamic *link* library)...\n- @H2CO3: It's a matter of definition, I guess. FOLDOC's definition is object code is so broad that even executable binaries would be object files. My point is that structurally, shared libs resemble `.o` files more than `.a` files, in that they're ELF (or Mach-O, COFF) files rather than archives of those.\n- I have alwayss wondered about the C++ STL.. it always confused me as to what it exactly is...collection of all those headers or what? Now after reading the responses, can I say that STL is an object file/something that resembles an object file? And also, I thought where I could read the function definitions of functiosn like pow(),sqrt() etc etc. I would open the ehader files and not find anything. So, is the function definition in the library in binary unreadable form?\n- @NikharAgrawal STL is a header-only \"library\" - there's no actual executable code, just templates, data types and typedefs. And yes, the C source code of the stdlib functions is in the /usr/lib/libc.so (libc.dylib, etc.) dynamic library file in binary form. The source code of these functions is available on the official webpage/version control repo of the particular libc implementation, e. g. on GNU.org, opensource.apple.com etc.\n- Thanks.. I'm not sure I completely understand this example... but that's coz I don't understand the word 'extren'. I'll do the research on it a li'l later and bug you again in case of doubts. :)\n- It appears that the cat Linux command is being used to create two small, simple C source code files, main.c and foo.c, each of which are first compiled and then are linked. The cc command has sufficient intelligence so that if you specify object files, the main.o and foo.o files, it will just perform a link using those files.\n- `cat` is not creating the files, merely displaying them.\n- Thanks for such detailed explanation. As mentioned - `One problem the linker can run into is that sometimes it may come across a marker when it is processing the object code files that requires an actual memory address.` Could you give example of such situation where actual memory address is required?\n- @YugSingh something that comes to mind is when a DLL is loaded. I was thinking about your question and I'm wondering if parts of this answer need updating with virtual memory and MMU technologies common even down to embedded processors.","metadata":{"transformedAt":"2026-08-18T18:32:17.773Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":18,"totalLines":154,"estimatedTokens":1709}}203{"id":"stack-2618494","source":"stackoverflow","questionId":2618494,"title":"C++ compilers and back/front ends","tags":["c++","compiler-construction","frontend","backend"],"text":"Title: C++ compilers and back/front ends\nTags: c++, compiler-construction, frontend, backend\nSource: Stack Overflow\n\nQuestion:\nFor my own education I am curious what compilers use which C++ front-end and back-end.\nCan you enlighten me where the following technologies are used and what hallmarks/advantages they have if any?\n\nOpen64 - is it back-end, front-end, or both? Which compilers use it? I encounter it in CUDA compiler.\n\nEDG - as far as I can tell this is a front-end use by Intel compilers and Comeau. do other compilers use it? I found quite a few references to it in boost source code.\n\nANTLR - this is general parser. Do any common compilers use it?\n\nRegarding compilers:\n\nwith front-end/back-end does gcc compiler suite uses? does it have common heritage with any other compiler?\n\nwhat front-end/back-end PGI and PathScale compilers use?\n\nwhat front-end/back-end XL compiler uses (IBM offering).\n\nin-depth links on the Internet or your personal know-how would be great.\nI did some Google searching, but information I generally encountered was rather superficial.\n\nThanks.\n\n========================================\n\nTop Answer:\nThe Clang project provides new front-ends for C/C++/Objective C on top of the LLVM backend. The LLVM project also provide a LLVM-gcc, using the GCC front end and the LLVM backend. The DragonEgg project seeks to replace the GCC backend with LLVM.\n\nThe Codeplay VectorC, Sieve and Offload compilers use a custom front-end and back-end\n\n========================================\n\nComments:\n- thanks . With nvcc (open 64) I get major problems when trying to use boost type traits (or anything that includes it). hopefully I will have some reference when looking for compiler workarounds\n- C++ isn't LALR, so that's why (name your favorite parser generator) isn't used for the front end. ANTLR works based on LL(*) instead of LALR, but I stink that also isn't sufficient for some of C++'s trickier syntax. C++ grammar isn't context-free, which is a requirement for most generalized parser generators.\n- @Ben:While that's pretty accurate with respect to most parser generators, there are a few that should be able to handle C++. I'm reasonably certain C++ should fit within the (few) constraints of a GLR grammar. A few generators can deal with GLR grammars (e.g., Elkhound).\n- @Jerry: From scottmcpeak.com/elkhound: GLR works with any context-free grammar C++ isn't context-free, so not even Elkhound can parse it. Not to say that it isn't a useful part of the task, but I believe you end up with lexed tokens parsed down to a set of possible meanings, and then the contextual analyzer finishes the parsing process as it builds symbol tables, etc.\n- @Ben:Yes and no -- C++ has context-dependent semantics (e.g. the infamous \"most vexing parse\"), but a parser doesn't *need* to deal with that -- it only has to accept well formed code and reject malformed code. From that perspective, C++ is (or can be treated as) context free -- though you have to \"sort out\" more later in the compiler if you do that.\n- A parser that doesn't distinguish \"most vexing parse\" isn't a complete parser. Like I said, such a quasi-parser is still very useful, but the output still isn't fully parsed, the contextual analyzer has to finish the job.\n- @Ben:not really true -- determining meaning is the domain of semantic analysis. Yes, we'd generally *prefer* that a parser make the semantic analysis easy, but it's not required for a parser to qualify as a parser.\n- A parser is supposed to determine what \"part of speech\" each word has in a sentence, or what function each token has in source code. See yosefk.com/c++fqa/web-vs-c++.html#misfeature-2 and merriam-webster.com/dictionary/parse\n- @Ben:quoting Yossi Kreinin as an authority hardly does your cause any good. In any case, it simply comes down to a question of how you choose to define \"parser\". Frankly, I just don't much care -- if you prefer to think of cfront (for one example) as not having had a parser because it was developed using yacc, so be it.\n- @Jerry: In general, that page in full of FUD. But it does have several good examples of context-dependent parse. And he's absolutely right that the abstract syntax tree is totally different in those cases. Really, I think we're in agreement on this issue: a parser generator can be very helpful in processing C++ but the contextual parts of the grammar have to be more fully processed by later steps.","metadata":{"transformedAt":"2026-08-18T18:32:17.773Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":47,"estimatedTokens":1108}}204{"id":"stack-4649947","source":"stackoverflow","questionId":4649947,"title":"Why doesn't C# allow me to use the same variable name in different scopes?","tags":["c#",".net","compiler-construction"],"text":"Title: Why doesn't C# allow me to use the same variable name in different scopes?\nTags: c#, .net, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nLike for instance:\n\n```\nif ( this.IsValid )\n{\n Matrix matrix = new Matrix();\n}\n\nMatrix matrix = new Matrix();\n```\n\nThe compiler warns me saying:\n\n\"A local variable named '`matrix`' cannot be declared in this scope because it would give a different meaning to '`matrix`', which is already used in a 'child' scope to denote something else.\n\nAren't these variables in different scopes, so I wouldn't be able to access the first `matrix` from outside the if statement anyway?\n\n========================================\n\nTop Answer:\nIt is my belief that this is done in order to avoid obscure mistakes or code that's hard to read.\n\nUsing the same name of variable between a method scope and a child scope can lead to code that's very hard to read, since the variable type and, worse, meaning, can change and the only *hint* to the reader will be type declaration keyword before the variable.\n\nHowever, I can also tell you that the IL generated for methods by the C# compiler will stick all variable declarations at the top, so maybe this decision driver was to simplify the variable parsing tree for the compiler.\n\nIn fact, you can find this at MSDN:\n\nThe scope of a name is the region of\nprogram text within which it is\npossible to refer to the entity\ndeclared by the name without\nqualification of the name. Scopes can\nbe nested, and an inner scope may\nredeclare the meaning of a name from\nan outer scope. (This does not,\nhowever, remove the restriction\nimposed by Section 3.3 that within a\nnested block it is not possible to\ndeclare a local variable with the same\nname as a local variable in an\nenclosing block.) The name from the\nouter scope is then said to be hidden\nin the region of program text covered\nby the inner scope, and access to the\nouter name is only possible by\nqualifying the name.\n\n*Emphasis added.*\n\nAnd, from Section 3.3:\n\nEach block or switch-block creates a\ndifferent declaration space for local\nvariables and constants. Names are\nintroduced into this declaration space\nthrough local-variable-declarations\nand local-constant-declarations. If a\nblock is the body of an instance\nconstructor, method, or operator\ndeclaration, or a get or set accessor\nfor an indexer declaration, the\nparameters declared in such a\ndeclaration are members of the block's\nlocal variable declaration space. The\nlocal variable declaration space of a\nblock includes any nested blocks.\nThus, within a nested block it is not\npossible to declare a local variable\nwith the same name as a local variable\nin an enclosing block.\n\n*Emphasis added.*\n\nSo, the thing is that while the **scopes** are different, the **variable space** is the same.\n\n========================================\n\nCode:\n```text\nif ( this.IsValid )\n{\n    Matrix matrix = new Matrix();\n}\n\nMatrix matrix = new Matrix();\n```\n\n```text\nmatrix\n```\n\n```text\nmatrix\n```\n\n```text\nmatrix\n```\n\n```text\nclass C \n{\n    int x;\n    void M()\n    {\n        x = 10; // means \"this.x\"\n        for(whatever)\n        {\n            int x = whatever;\n        }\n    }\n }\n```\n\n```text\nclass C \n{\n    int x;\n    void M()\n    {\n        int x;\n        x = 10; // no longer means \"this.x\"\n        for(whatever)\n        {\n            x = whatever;\n        }\n    }\n }\n```\n\n```text\nclass C \n{\n    int x;\n    void M()\n    {\n        {\n            x = 10; // means \"this.x\"\n        }\n        for(whatever)\n        {\n            int x = whatever; // Legal; now the \n        }\n    }\n }\n```\n\n```text\nvoid YourMethod() \n{\n    if ( this.IsValid ) \n    {    \n        Matrix matrix = new Matrix();\n    }\n\n    {\n        Matrix matrix = new Matrix(); \n    }\n}\n```\n\n```text\n{}\n```\n\n```text\nMatrix matrix = new Matrix();\n\nif ( this.IsValid ) \n{\n    Matrix matrix = new Matrix(); \n}\n```\n\n========================================\n\nComments:\n- Even if the question is eligible, this prevents you from doing careless mistakes.\n- over here [bytes.com/topic/c-sharp/answers/&hellip; you can find an explanation from Jon Skeet. And here stackoverflow.com/questions/296755/child-scope-cs0136 also by Jon Skeet.\n- @Tim: Yeah but then one needs to make unnecessary new variable names, every time, something similar must be used, just to satisfy the compiler. IMO if the code is clear, this wouldn't cause any issue. I can see what each matrix meant without doing: matrixTemporary, matrixReal, etc.\n- @Joan Venge: If the variable would be unnecessary you would not use two separate variables but the same.\n- @Tim: I didn't say the variables would be unnecessary but the variable names being different.\n- Standard C# team wisdom, it's a fertile source of bugs. No other reason.\n- Note that this is a duplicate of stackoverflow.com/questions/2693138/&hellip;\n- I don't think it's confusing, because it clearly shows that the matrix inside the if is meant to be temporary, so could be created to do a certain calculation, etc.\n- Thanks but I can't. The second matrix and statements following it must always execute (whether this.IsValid or not)\n- To the downvoter. Why is this wrong? You might try it. You will find that you are the one that is wrong. The second set of braces declares a secondary scope that is always executed. It's not one or the other.\n- @Joan, it will execute both.\n- Thanks Matthew, I thought there was an else there, now makes sense. Will upvote.\n- This is a rare yet beautiful aspect of C# in my opinion :D I love how I can create my custom sub-scopes. I came to this question to find out if this is a common practice among C# coders.\n- I wouldn't say this is common. Many people would probably frown upon this as it's not obvious what is going on... but it will work. debatably better way would be to give the variables better/unique names or just define a single variable and reuse it as needed in the function.\n- That seems more counter-intuitive IMO, but even in that case, I would expect the compiler to hide the outer scope variable and create a new one. But in my example, one would think the first matrix is already in another scope, so wouldn't be able to affect the second matrix variable, like it's accessibility doesn't reach beyond the if statement it's contained within.\n- @Eric: Thanks Eric. I don't understand how in your last example, second int x is valid. Is it because you encapsulated x = 10 with {}s?\n- @Joan: Correct. With the x=10 in a block of its own, now the local variable declaration space which encloses it does not *overlap* with the other local variable declaration space that contains the other usage of x.\n- @Eric: Thanks Eric. I got it now. I have seen some languages when written like in your first example, the second x would hide the first, so you think this is a bad design? Lastly you said in your comment \"... which encloses it does not overlap ...\", do you know a place I can learn about the overlapping concept in C#? I didn't hear it before.\n- @Joan: see the section of the spec I referred to. The relevant clause is \"within the local variable declaration space immediately enclosing that occurrence\" -- the *immediately* means that you don't look at more-outer blocks, and the *within* means you do look at more-inner blocks.\n- @Joan: To answer your question: I once spent almost an entire day tracking down a bug which turned out to be me accidentally using \"i\" to mean two different things in a very long, complicated C++ routine. It was completely not obvious that as I stepped through it in the debugger, that suddenly \"i\" in the debugger was referring to a *different variable* (sometimes with the same value!) as I stepped around. It was a boneheaded stupid bug to introduce, but a very easy bug to introduce, and the C++ compiler was no help to me whatsoever. The rules in C# are much more likely to promote good code.\n- @Eric: Thanks Eric, for sure your judgement helps millions of programmers out there. It's good to know the reasoning, so I am more informed about these.\n- @Eric: must say that being topped by one of my idols was very very cool! :)","metadata":{"transformedAt":"2026-08-18T18:32:17.773Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":205,"estimatedTokens":2019}}205{"id":"stack-54947302","source":"stackoverflow","questionId":54947302,"title":"Handling calls to (potentially) far away ahead-of-time compiled functions from JITed code","tags":["assembly","rust","compiler-construction","x86-64","jit"],"text":"Title: Handling calls to (potentially) far away ahead-of-time compiled functions from JITed code\nTags: assembly, rust, compiler-construction, x86-64, jit\nSource: Stack Overflow\n\nQuestion:\nThis question was put on hold as too broad, presumably because of the research I included in an effort to \"show my work\" instead of asking a low effort question. To remedy this, allow me to summarize the entire question in a single sentence (credit to @PeterCordes for this phrase):\n\n How do I efficiently call (x86-64) ahead-of-time compiled functions (that I control, may be further than 2GB away) from JITed code (that I am generating)?\n\nThis alone, I suspect, would be put on hold as \"too broad.\" In particular, it lacks a \"what have you tried.\" So, I felt the need to add additional information showing my research/thinking and what I have tried. Below is a somewhat stream of consciousness of this.\n\n**Note that none of the questions posed below here are ones I expect to be answered; they are more rhetorical.** Their purpose is to demonstrate why I can't answer the above question (despite my research, I lack the experience in this area to make definitive statements such as @PeterCordes's \"branch prediction hides the latency of fetching and checking the function pointer from memory, assuming that it predicts well.\"). Also note that the Rust component is largely irrelevant here as this is an assembly issue. My reasoning for including it was the ahead-of-time compiled functions are written in Rust, so I was unsure if there was something that Rust did (or instructed LLVM to do) that could be advantageous in this situation. It is totally acceptable for an answer to not consider Rust at all; in fact, I expect this will be the case.\n\nThink of the following as scratch work on the back of a math exam:\n\n*Note:* I muddled the term intrinsics here. As pointed out in the comments, \"ahead-of-time compiled functions\" is a better description. Below I'll abbreviate that *AOTC* functions.\n\nI'm writing a JIT in Rust (although Rust is only relevant to a bit of my question, the bulk of it relates to JIT conventions). I have *AOTC* functions that I've implemented in Rust that I need to be able to `call` from code emitted by my JIT. My JIT `mmap(_, _, PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED)`s some pages for the jitted code. I have the addresses of my *AOTC* functions, but unfortunately they are much further away than a 32-bit offset. I'm trying to decide now how to emit calls to these *AOTC* functions. I've considered the following options (these are not questions to be answered, just demonstrating why I can't answer the core question of this SO thread myself):\n\n(Rust specific) Somehow make Rust place the *AOTC* functions close to (maybe on?) the heap so that the `call`s will be within a 32-bit offset. It's unclear that that is possible with Rust (There is a way to specify custom linker args, but I can't tell to what those are applied and if I could target a single function for relocation. And even if I could where do I put it?). It also seems like this could fail if the heap is large enough.\n\n(Rust specific) Allocate my JIT pages closer to the *AOTC* functions. This could be achieved with `mmap(_, _, PROT_EXEC, MAP_FIXED)`, but I'm unsure how to pick an address that wouldn't clobbering existing Rust code (and keeping within arch restrictions--is there a sane way to get those restrictions?).\n\nCreate stubs within the JIT pages that handle the absolute jump (code below), then `call` the stubs. This has the benefit of the (initial) call site in the JITted code being a nice small relative call. But it feels wrong to have to jump through something. This seems like it would be detrimental to performance (perhaps interfering with RAS/jump address prediction). Additionally, it seems like this jump would be slower since its address is indirect and it depends on the `mov` for that address.\n\n```\nmov rax, {ABSOLUTE_AOTC_FUNCTION_ADDRESS}\njmp rax\n```\n\nThe reverse of (3), just inlining the above at each intrinsic call site in the JITed code. This resolves the indirection issue, but makes the JITted code larger (perhaps this has instruction cache and decoding consequences). It still has the issue that the jump is indirect and depends on the `mov`.\n\nPlace the addresses of the *AOTC* functions on a PROT_READ (only) page near the JIT pages. Make all the call sites near, absolute indirect calls (code below). This removes the second level of indirection from (2). But the encoding of this instruction is unfortunately large (6 bytes), so it has the same issues as (4). Additionally, now instead of depending on a register, jumps unnecessarily (insofar as the address is known at JIT time) depend on memory, which certainly has performance implications (despite perhaps this page being cached?).\n\n```\naotc_function_address:\n .quad 0xDEADBEEF\n\n# Then at the call site\ncall qword ptr [rip+aotc_function_address]\n```\n\n Futz with a segment register to place it closer to the *AOTC* functions so that calls can be made relative to that segment register. The encoding of such a call is long (so maybe this has decoding pipeline issues), but other than that this largely avoids lots of the tricky bits of everything before it. But, maybe calling relative to a non-`cs` segment performs poorly. Or maybe such futzing is not wise (messes with the Rust runtime, for example). (as pointed out by @prl, this doesn't work without a far call, which is terrible for performance)\n\nNot really a solution, but I could make the compiler 32-bit and not have this problem at all. That's not really a great solution and it also would prevent me from using the extended general purpose registers (of which I utilize all).\n\nAll of the options presented have drawbacks. Briefly, 1 and 2 are the only ones that don't seem to have performance impacts, but it's unclear if there is a non-hacky way to achieve them (or any way at all for that matter). 3-5 are independent of Rust, but have obvious performance drawbacks.\n\nGiven this stream of consciousness, I arrived at the following rhetorical question (which don't need explicit answers) to demonstrate that I lack the knowledge to answer the core question of this SO thread by myself. **I have struck them to make it abundantly clear that I am not posing all of these are part of my question.**\n\nFor approach (1), is it possible to force Rust to link certain `extern \"C\"` functions at a specific address (near the heap)? How should I choose such an address (at compile time)? Is it safe to assume that any address returned by `mmap` (or allocated by Rust) will be within a 32 bit offset of this location?\n\nFor approach (2), how can I find a suitable place to place the JIT pages (such that it doesn't clobber existing Rust code)?\n\n**And some JIT (non-Rust) specific questions:**\n\nFor approach (3), will the stubs hamper performance enough that I should care? What about the indirect `jmp`? I know this somewhat resembles linker stubs, except as I understand linker stubs are at least only resolved once (so they don't need to be indirect?). Do any JITs employ this technique?\n\nFor approach (4), if the indirect call in 3 is okay, is inlining the calls worth it? If JITs typically employ approach (3/4) is this option better?\n\nFor approach (5), is the dependence of the jump on memory (given that the address is known at compile time) bad? Would that make it less performant that (3) or (4)? Do any JITs employ this technique?\n\nFor approach (6), is such futzing unwise? (Rust specific) Is there a segment register available (not used by the runtime or ABI) for this purpose? Will calls relative to a non-`cs` segment be as performant as those relative to `cs`?\n\nAnd **finally (and most importantly)**, is there a better approach (perhaps employed more commonly by JITs) that I'm missing here?\n\nI can't implement (1) or (2) without my Rust questions having answers. I could, of course, implement and benchmark 3-5 (perhaps 6, although it would be nice to know about the segment register futzing beforehand), but given that these are vastly different approaches, I was hoping there was existing literature about this that I couldn't find, because I didn't know the right terms to google for (I'm also currently working on those benchmarks). Alternatively maybe someone who's delved into JIT internals can their experience or what they've commonly seen?\n\nI am aware of this question: Jumps for a JIT (x86_64). It differs from mine because it is talking about stringing together basic blocks (and the accepted solution is way too many instructions for a frequently called intrinsic). I am also aware of Call an absolute pointer in x86 machine code, which while it discusses similar topics to mine, is different, because I am not assuming that absolute jumps are necessary (approaches 1-2 would avoid them, for example).\n\n========================================\n\nCode:\n```text\nmov rax, {ABSOLUTE_AOTC_FUNCTION_ADDRESS}\njmp rax\n```\n\n```text\naotc_function_address:\n    .quad 0xDEADBEEF\n\n# Then at the call site\ncall qword ptr [rip+aotc_function_address]\n```\n\n```text\ncall\n```\n\n```text\nmmap(_, _, PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED)\n```\n\n```text\ncall\n```\n\n```text\nmmap(_, _, PROT_EXEC, MAP_FIXED)\n```\n\n```text\ncall\n```\n\n```text\nmov\n```\n\n```text\nmov\n```\n\n```text\ncs\n```\n\n```text\nextern \"C\"\n```\n\n```text\nmmap\n```\n\n```text\njmp\n```\n\n```text\ncs\n```\n\n```text\ncs\n```\n\n```text\nrel32\n```\n\n```text\ncall qword [rel pointer]\n```\n\n```text\nmov r64,imm64\n```\n\n```text\ncall r64\n```\n\n```text\ncall rel32\n```\n\n```text\n_mm_popcnt_u32()\n```\n\n```text\n_pdep_u32()\n```\n\n```text\n_mm_mfence()\n```\n\n```text\ncall\n```\n\n```text\nmprotect\n```\n\n```text\nVirtualProtect\n```\n\n```text\nMAP_POPULATE\n```\n\n```text\nmmap\n```\n\n```text\nmmap(MAP_32BIT)\n```\n\n```text\nMAP_32BIT\n```\n\n```text\nmmap\n```\n\n```text\nMAP_FIXED\n```\n\n```text\nMAP_FIXED_NOREPLACE\n```\n\n```text\nEEXIST\n```\n\n```text\n/proc/self/maps\n```\n\n```text\nMAP_FIXED_NOREPLACE\n```\n\n```text\ncall rel32\n```\n\n```text\noff == (off as i32) as i64\n```\n\n```text\nmov r64,imm64\n```\n\n```text\ncall r64\n```\n\n```text\nmovsxd\n```\n\n```text\ncmp\n```\n\n```text\nmov r32,imm32\n```\n\n```text\nMAP_32BIT\n```\n\n```text\ntarget == (target as u32) as u64\n```\n\n```text\nmov\n```\n\n```text\nmov r/m64, sign_extended_imm32\n```\n\n```text\nmov r64,imm64\n```\n\n```text\nr11\n```\n\n```text\ncall\n```\n\n```text\nmmap\n```\n\n```text\nmmap\n```\n\n```text\nmmap\n```\n\n```text\ncall rel32\n```\n\n```text\nmov\n```\n\n```text\njmp reg\n```\n\n```text\nmain(){puts(\"hello\"); puts(\"world\");}\n```\n\n```text\njmp\n```\n\n```text\njmp qword [xxx@GOTPLT]\n```\n\n```text\njmp\n```\n\n```text\njmp rel32\n```\n\n```text\njmp rel32\n```\n\n```text\njmp\n```\n\n```text\nret\n```\n\n```text\ncall\n```\n\n```text\ncall\n```\n\n```text\ncall +0\n```\n\n```text\nnop\n```\n\n```text\ncall rax\n```\n\n```text\ncall/pop\n```\n\n```text\nmov r64, imm64\n```\n\n```text\ncall reg\n```\n\n```text\njmp\n```\n\n```text\ncall qword [rel nearby_func_ptr]\n```\n\n```text\ngcc -fno-plt\n```\n\n```text\ncall [rip + symbol@GOTPCREL]\n```\n\n```text\ncall [RIP-relative]\n```\n\n```text\ncall rel32\n```\n\n```text\naddr32 call rel32\n```\n\n```text\ncall [RIP + symbol@GOTPCREL]\n```\n\n```text\ncall rel32\n```\n\n```text\n.o\n```\n\n```text\ngcc\n```\n\n```text\ngcc -fno-plt\n```\n\n```text\nclang -O2 -g\n```\n\n```text\nclang --help\n```\n\n```text\njmp qword [symbol@GOTPLT]\n```\n\n```text\nmov r64,imm64\n```\n\n```text\njmp\n```\n\n```text\njmp\n```\n\n```text\nmovabs\n```\n\n```text\njmp\n```\n\n========================================\n\nComments:\n- I like the question, but it seems really broad. You may get better feedback in r/compilers and r/programminglanguages, which are subreddits with people sharing interests in those domains.\n- Is the Rust code below 4G? If so, you can use the MAP_32BIT flag.\n- Onto the actual question: what are those intrinsics used for? It seems very strange to me to have intrinsics that are both performance critical YET should not be inlined. Hiding performance critical bits behind an opaque function pointer seems rather counter-intuitive: could give an example of a couple intrinsics that would help tailoring recommendations?\n- The best choice is the answer given in the final question you linked: call through a register. If a call is frequent enough to be in thepredictor, it’s as fast as any other type of call; if not, it’s not much slower. Calling through memory ((3) or (5)) is worse.\n- (6) doesn’t work at all. You can’t call using a segment register except by using a far call that loads cs, which is extremely slow. Not to mention putting an unexpected value in cs. Plus requiring OS support.\n- @MatthieuM. Yeah I was afraid of that. I see the one close for too broad. I can definitely see how it would be. Perhaps because I dumped all my thoughts out. My worry was if I just flat out asked \"how should I do this?\" then it would seem like I wanted others to do my research for me. The thrust of the question is: \"I've thought about this a lot, but what do JITs usually do in this scenario? And if it's one of the methods enumerated, how do I overcome the technical hurdles?\" Perhaps that got lost? I'll certainly post to those subs. If you have any thoughts on how to narrow, I'm all ears :)\n- @prl Ohhhh! `MAP_32BIT` will do it! That definitely guarantees a relative `call` can be used. Unfortunately, this won't work on BSDs, but it looks like luajit has a way to handle this. Will investigate. Thanks :D\n- @MatthieuM. Perhaps I overstated their performance criticality. They're I/O which I've implemented in rust to allow for easier testing (being able to stub out without changing the emitted code). I suspect their implementations are fairly large so inlining may be a little ridiculous. Further, it would then require me to comply (and work around) the internal rust ABI (which as far as I can tell isn't something I can rely on being stable?). Also because of the stubbing, they're generic, so I'd have to sort out how to copy their implementations at runtime; seems like a tall order. Or not?\n- @prl Your feedback about indirect calling and through memory is also helpful thanks! I also wasn't very sure about the segment stuff, but thanks for clearing it up!\n- @BaileyParker Oh please don't be discouraged by a close vote. It's amazing to have questions like this!\n- @BaileyParker: If it's I/O then I think (3) becomes perfectly reasonable given the overhead of context switching and I/O. If there's a large number of intrinsics, though, it may negatively affect start-up time.\n- Now that you’ve added that the called functions are large and for I/O, any of solutions 3, 4, or 5 are fine. Any performance cost is negligible compared to a system call to do I/O. Do whichever is most convenient for the code.\n- I have to agree both that this is an interesting set of questions, and that they are entirely unsuited for Stack Overflow :( The Rust-specific numbered questions could each be a Q&A post on their own (and I think they would be valuable to have). Questions about whether something is \"worth it\" or whether there exists \"a better approach\" probably can't easily be made on-topic, though. In addition to Reddit, maybe you should consider users.rust-lang.org. Parts of your question may also be right for Software Engineering SE if edited (read the rules to see what that site expects).\n- Updated my answer on Call an absolute pointer in x86 machine code (which you already linked) to mention `MAP_32BIT` explicitly. That's one thing I had in mind, but I guess I kind of said it backwards for a JIT context (I previously said \"pick your absolute addresses...\", but normally in JIT you're picking the code location and then trying to reach existing code.) And not everyone knows about MAP_32BIT, or giving `mmap` a non-NULL hint near your existing code if it's not in the low 2GB (i.e. from a PIE executable).\n- @trentcl: I disagree; this basically boils down to \"how to I efficiently call ahead-of-time compiled functions from JITed code?\" with some ideas thrown around to evaluate and comment on. A question with some existing ideas is more fun than if that was literally all there was, because then there's more to talk about. :) Asking them separately would be an XY problem. x86 performance questions are very much on topic on SO. Anyway, I spent maybe longer than I intended writing an answer here (it's under half the 30k char limit though. Anyway I could have made it much shorter. :P)\n- @MatthieuM.: Disagree on too broad. The question seemed clear and answerable to me. The ideas proposed in the question made it clear the OP would be able to understand an interesting answer, and didn't need hand-holding through what bytes to emit in order to make it work at all.\n- @prl: besides performance, the other showstopper for `call far` is that there's no encoding for absolute *direct* `call far ptr16:64` (with the address as an immediate), only for `ptr16:32` in 32-bit mode. Memory-indirect `call [m16:64]` is OP's option 5 `call [mem]` but worse. You don't need \"OS support\", though; your JITer can read its own CS and use that value. AFAIK, it's always allowed to set CS to the current selector in all mainstream OSes. Hard to imagine how it could break unless the GDT/LDT entry became invalid since entering user-space.\n- @PeterCordes: I will note that I did NOT vote to close, and actually upvoted the question. I do notice it's closed now (for 30 minutes), and this means that I can vote to re-open... though I think the question should be reworded. The list of questions at the end is probably what is prompting the close => too many question marks = too broad. A single question: \"How to emit calls to intrisincs efficiently in JIT code?\", which lists known approaches and their drawbacks/unknowns without \"questions\" would aim for the same goal, but be formulated in a way less likely to attract downvotes.\n- I appreciate all the feedback and debate from everyone. Just dropping my perspective on this in: Peter hit my intention right on the nose. That was the core question (he also phrased it a lot better than I did). I always feel it necessary to substantiate my questions with my prior research/thought to demonstrate that I have indeed done it and am not just \"gimme teh codez\"-ing. But the point is well received that my phrasing/formatting could have drawn \"too broad\" claims. I will reformat to combat this. Thanks everyone! :)\n- Wow! Thank you so much Peter! This is fantastic! You answered questions I didn't even know I had! If I could still bounty this question, I would absolutely do it and award it to you! I'm going to go back and read this a bunch more times and re-consult your sources. Thank you for all your effort :)","metadata":{"transformedAt":"2026-08-18T18:32:17.774Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":92,"totalLines":469,"estimatedTokens":4635}}206{"id":"stack-3455062","source":"stackoverflow","questionId":3455062,"title":"Artificial Intelligence Compiler","tags":["optimization","compiler-construction","artificial-intelligence","compiler-errors"],"text":"Title: Artificial Intelligence Compiler\nTags: optimization, compiler-construction, artificial-intelligence, compiler-errors\nSource: Stack Overflow\n\nQuestion:\nI was wondering, is it possible to use Artificial Intelligence to make compilers better? \n\nThings I could imagine if it was possible - \n\n- More specific error messages\n\n- Improving compiler optimizations, so the compiler could actually understand what you're trying to do, and do it better\n\nIf it **is** possible, are there any research projects on this subject?\n\n========================================\n\nTop Answer:\nAn optimizing compiler is actually a very complex expert system and Expert systems is one of the oldest branches of artificial intelligence.\n\n========================================\n\nComments:\n- In C# you can use tools like StyleCop and FxCop and resharper to help find potential problems. You can write custom rules, until they take so long to run that you will need a server farm. What is your goal anyway? Resharper can rewrite loops as LINQ, and that in turn often helps the C# compiler to emit the fastest code possible. However, speed should not be the only goal.","metadata":{"transformedAt":"2026-08-18T18:32:17.774Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":287}}207{"id":"stack-4720499","source":"stackoverflow","questionId":4720499,"title":"Possible optimizations in Haskell that are not yet implemented in GHC?","tags":["haskell","optimization","compiler-construction","ghc"],"text":"Title: Possible optimizations in Haskell that are not yet implemented in GHC?\nTags: haskell, optimization, compiler-construction, ghc\nSource: Stack Overflow\n\nQuestion:\nSo, purely functional languages have their own class of potentials due to the clear separation between pure and impure code. I have seen several features that are somewhat simpler to implement in Haskell like Nested Data Parallelism or Stream Fusion.\n\nMy question is, what are other improvements/optimizations that are more or less unique to Haskell in terms of feasibility/simplicity but not yet implemented? (I mostly care about GHC, but also love to hear about others)\n\n========================================\n\nTop Answer:\nAnother issue that SPJ states in his paper on modular supercompilation is combining supercompilation with unboxing. Possibilities for unboxing in supercompiled program are significantly reduced. This causes decrease in performance in comparison with unoptimized program passed through GHC strict-analyser/unboxer. See http://research.microsoft.com/en-us/um/people/simonpj/papers/supercompilation/\n\n========================================\n\nComments:\n- There are a few projects on this, for example Supero (community.haskell.org/~ndm/supero) though it's built on top of GHC. Basically supercompilation is an extended form of constant folding. Who cares for the compile time if it's only for optimized builds ?\n- Also, see cl.cam.ac.uk/~mb566\n- also, take a look at code.google.com/p/hosc (it has web-app interface)\n- @Matthiey, another issue pointed by SPJ is code size explosion (up to 30 times)\n- Thanks for the comment (plus the buzzword *supercompilation*). I was about to create a question whether this is actually implemented, since it seems to be one of the natural strengths of a pure language - basically everything out of the IO monad could be optimized away with this.\n- @maxtaldykin who cares anyway, we do have computers with 8 GB now and soon much much more will be the standard. Yes its bad for tight loop code (code-cache) but a supercompiler could *in principle* analyze such cases and redo size increasing optimizations\n- @David \"everything out of the IO monad could be optimized away with this\". Not true. Diverging computations and, in general, codata may not be optimized. In particular anywhere there is recursion (both at the type level or at the value level) there's potential for divergence and for many computations it is hard or impossible to statically prove that they do not diverge. The fact that there is no properly inductive type (all types in Haskell are pointed) complicates things further as inductive reasoning about your program is not possible.\n- Thanks. That's very informative. But should this have been a comment to Carl's response?\n- Yes, sorry for miscommenting, I'm new to Stackoverflow...\n- Note that author ordering is usually significant. SPJ's influence on that work was undoubtedly significant, yet the first author is Max, so I'd call it \"Max states\" or \"Max and SPJ state\". I know that SPJ is far more well-known, but it's common courtesy to attribute the first author.\n- GHC certainly does do worker-wrapper transformation and has been for a very long time. It's used to exploit strictness information. Of course, W/W is a lot more general, and there may be many more ways to exploit it, but it's certainly used, if not in its full generality.","metadata":{"transformedAt":"2026-08-18T18:32:17.774Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":28,"estimatedTokens":848}}208{"id":"stack-6360737","source":"stackoverflow","questionId":6360737,"title":"Generating LLVM code for 'lambda', 'define'","tags":["haskell","compiler-construction","lisp","language-design","llvm"],"text":"Title: Generating LLVM code for 'lambda', 'define'\nTags: haskell, compiler-construction, lisp, language-design, llvm\nSource: Stack Overflow\n\nQuestion:\nSo I now have a fairly complete LISP (scheme) interpreter written in haskell. Just for fun I want to try to have it compile down to LLVM. Most of the code generation seems pretty straight forward, but I'm at a loss as to how to generate code for a `lambda` expression (kind of important in lisp ;) ) and how to manage the heap when I encounter a `define` expression.\n\nHow might I generated code for these expressions? \n\nNote: I can generate code for the body of the lambda expression, What is confusing me is how to \"put\" that code somewhere and make it callable.\n\n========================================\n\nCode:\n```text\nlambda\n```\n\n```text\ndefine\n```\n\n```text\ncompileFunction\n```\n\n```text\nnewFunction\n```\n\n========================================\n\nComments:\n- You may want to implement an explicit lambda lifting pass prior to your code generation (and since it is Scheme, you're likely to be doing a CPS-transform prior to that). It will leave you with only the global functions and an explicit closure envoronments allocation.\n- The language that is compiled in the blog post is first order and doesn't handle closures at all. I don't see how that is helpful for compiling scheme lambdas.\n- @sepp2k You'll need to do closure conversion first, which I assume John is doing, since he's reading SICP.\n- I could be wrong, but I don't remember closure conversion being covered in SICP.","metadata":{"transformedAt":"2026-08-18T18:32:17.774Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":37,"estimatedTokens":384}}209{"id":"stack-51340463","source":"stackoverflow","questionId":51340463,"title":"Converting SSA to stack machine","tags":["compiler-construction","cil","ssa","stack-machine"],"text":"Title: Converting SSA to stack machine\nTags: compiler-construction, cil, ssa, stack-machine\nSource: Stack Overflow\n\nQuestion:\nIt is well known how to convert code from SSA representation to a register machine. (Basically, graph coloring register allocation is the core of such conversion.)\n\nBut what's the general method for converting from SSA to a stack machine? (CIL byte code, in the case I'm looking at.) I would expect it to be simpler, given the lack of need for register allocation?\n\n========================================\n\nTop Answer:\nSSA is basically set of \"logic\" gates each with multiple inputs and typically one output.\n\nSo essentially you need to treat each gate as a set of stack pushes for the inputs, followed by a zero-operand operator that combines the stack values into the result for that gate. For instance, a + b * c as SSA with a multiply-and-accumulate operator has 3 pushes for a,b,c followed by a MAC_TOS operator.\n\nIf one has a chain of such gates, you can take the output of an earlier gate, which is already on the stack, and simply acts as if it has been pushed.\n\nSo, and SSA computation looks like an n-ary tree of gates with the output coming out at the root.\n\nYou can walk the tree in in-fix order, pushing operands that have not already been pushed, and generating a gate's operator when all operands have been computed.\n\nSo the SSA graph (tree):\n\n```\na \n \\\n * \nb / \\\n +\nc /\n \\ /\n -\n /\nd\n```\n\ncan be used to produce\n\n```\npush a\npush b\ntimes\npush c\npush d\nsubtract\ntimes\n```\n\n========================================\n\nCode:\n```text\niload index\n```\n\n```text\nistore index\n```\n\n```text\nlocal 5 = MUL local[2], local[4]\n```\n\n```text\nILOAD 4\nILOAD 2\nMUL\nISTORE 5\n```\n\n```text\nldarg\n```\n\n```text\nstarg\n```\n\n```text\na \n  \\\n   * \nb /  \\\n      +\nc     /\n  \\  /\n   -\n  /\nd\n```\n\n```text\npush a\npush b\ntimes\npush c\npush d\nsubtract\ntimes\n```\n\n========================================\n\nComments:\n- Perhaps this might be better suited for CS.se.\n- Right, if one only needed to translate expressions, that would suffice. But in general SSA `a`, `b` etc may be in arbitrary order, and may be function calls with side effects so order needs to be preserved, so you need to do a forward pass over each basic block to do things in the given order. How do you reconcile that with the above scenario of backward chaining in postfix order?\n- Your question was phrased as basic insight, so my answer was at the same level: SSA is basically *only* expressions; it is the functional equivalent of chunks of your code. Real code generators lead more complex lives. If you have multiple expressions with shared subexpressions (e.g, a DAG with multiple results), you want to evaluate the shared subexpressions first (recursively) so that you can maintain a stack compatible ordering of computations; you may need a memory location to hold the shared result.\n- You'll note that phi nodes add a complication because their value may come from very different computations. They too,might need a temporary location. Good news: you can assign temporaries using register coloring :-} .If you have side effects of an operator, then you will have to add additional sequencing arcs to the SSA nodes that models where ordering is necessary, and honor in the code generation process.\n- This is an extremely lucid way of explaining it, good work.\n- @IraBaxter Thanks, illuminating! What if there's no (heap) memory location to spill the results of shared subexpressions, but there are appropriate stack operators like `dup`, `drop`, `swap`, `rot` in FORTH? As the set of supported stack operatos vary across implementations, maybe the solution is similar to optimizing compilation or query optimization?\n- Assume you end up with N temps needing to spill. Then you get to treat the top N location of the stack as your N temps. There are N! ways to arrange the temps on top of the stack; you need to find out which one of those causes you to issue the fewest stack manipulation operations to bring the temps you want into top place in stack when needed. I don't know how to accomplish that optimization.\n- ... well, hum. Given that the number of shared subexpressions is probably small, you could probably do a brute force search of (abstracted) code sequences and determine the code with the smallest number of stack-top operations.","metadata":{"transformedAt":"2026-08-18T18:32:17.774Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":113,"estimatedTokens":1082}}210{"id":"stack-45992580","source":"stackoverflow","questionId":45992580,"title":"Proof of the Futamura projections in Haskell","tags":["haskell","compiler-construction","interpreter"],"text":"Title: Proof of the Futamura projections in Haskell\nTags: haskell, compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nI read Dan Piponi's excellent blog post on The Three Projections of Doctor Futamura. Towards the end of the article he has an appendix with a proof of the Futamura projections in Haskell. However, I find his article lacking information about the languages involved. What must the source, target and object languages of the specializer be in order for the Futamura projections to work? For example, would the Futamura projections work if I wrote a Haskell to LLVM specializer in Haskell? It would be helpful if you wrote a Haskell program to prove this just like Dan Piponi did in his article.\n\n========================================\n\nCode:\n```hs\nα → β : ℒ -- A program is a function from α to β as implemented in language ℒ.\n```\n\n```hs\n(α → β : 𝒮) × α → β : 𝒪 -- An interpreter for language 𝒮 as implemented in 𝒪.\n(α → β : 𝒮) → (α → β : 𝒯) : 𝒯 -- A compiler from 𝒮 to 𝒯 as implemented in 𝒯.\n(ι × α → β : 𝒮) × ι → (α → β : 𝒯) : 𝒮 -- A self-hosting specializer from 𝒮 to 𝒯.\n(ι × α → β : 𝒮) → (ι → (α → β : 𝒯) : 𝒯) : 𝒯 -- A compiler compiler from 𝒮 to 𝒯.\n```\n\n```hs\n{-# LANGUAGE RankNTypes #-}\n\nmodule Futamura where\n\nnewtype Program a b language = Program { runProgram :: a -> b }\n\ntype Interpreter source object = forall a b.       Program (Program a b source, a) b object\ntype Compiler    source target = forall a b.       Program (Program a b source) (Program a b target) target\ntype Specializer source target = forall input a b. Program (Program (input, a) b source, input) (Program a b target) source\ntype Partializer source target = forall input a b. Program (Program (input, a) b source) (Program input (Program a b target) target) target\n\nprojection1 :: Specializer object target -> Interpreter source object -> Program a b source -> Program a b target\nprojection1 specializer interpreter program = runProgram specializer (interpreter, program)\n\nprojection2 :: Specializer object target -> Interpreter source object -> Compiler source target\nprojection2 specializer interpreter = runProgram specializer (specializer, interpreter)\n\nprojection3 :: Specializer source target -> Partializer source target\nprojection3 specializer = runProgram specializer (specializer, specializer)\n```\n\n```text\nα\n```\n\n```text\nβ\n```\n\n```text\nRankNTypes\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.774Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":54,"estimatedTokens":600}}211{"id":"stack-10264635","source":"stackoverflow","questionId":10264635,"title":"Compiler output language - LLVM IR vs C","tags":["compiler-construction","code-generation","llvm","llvm-ir"],"text":"Title: Compiler output language - LLVM IR vs C\nTags: compiler-construction, code-generation, llvm, llvm-ir\nSource: Stack Overflow\n\nQuestion:\nFor writing a compiler, what are the advantages and disadvantages of using LLVM IR vs C for a target language? I know both are used, and I imagine that the final machine code would be similar if I were to use clang to compile the C. So what are other things to consider?\n\n========================================\n\nTop Answer:\nI doubt you can implement proper debugging support for your language when targeting C.\n\n========================================\n\nCode:\n```text\ntcc\n```\n\n```text\ntcc\n```\n\n========================================\n\nComments:\n- What do you mean IR is typed? Isn't C typed as well?\n- Right, C is typed. But you don't get an indication of the error until you try to compile the C code. With LLVM IR you get an indication of the error when you generate the IR. Much easier to debug.\n- That was exactly the reason I've been searcing for this thread. I see no way there could be \"source maps\" on debug symbols, because there are backwards incompatible changes in C compilers' debug symbols generators. One would have to update debug symbol mapping software with each change of supported C compilers.\n- you forgot: if you want C interop (which language doesn't?) you have to code all those nasty C ABIs yourself because llvm doesn't do that all by itself (it splits that work 50/50 with clang)","metadata":{"transformedAt":"2026-08-18T18:32:17.774Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":30,"estimatedTokens":363}}212{"id":"stack-8767965","source":"stackoverflow","questionId":8767965,"title":"Recursive Descent Parser","tags":["c++","parsing","compiler-construction"],"text":"Title: Recursive Descent Parser\nTags: c++, parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThe book 'Modern Compiler Design' is the nice book about compilers. In its source code something that is annoying me is AST or Abstract Syntax Tree. Suppose we want to write a parenthesized expression parser which parses something like: `((2+3)*4) * 2`! The book says that we have an AST like:\n\n```\n((2+3)*4) * 2\n / | \\\n (2+3) *4 * 2\n / | \\\n (2+3) * 4\n / | \\\n 2 + 3\n```\n\nSo should I save a tree in memory or just use recursive calls; Note: if I don't store it in memory, how can I convert it to machine code ?\n\nParser code:\n\n```\nint parse(Expression &expr)\n{\n if(token.class=='D')\n { \n expr.type='D';\n expr.value=token.val-'0';\n get_next_token();\n return 1;\n }\n if(token.class=='(') \n {\n expr.type='P';\n get_next_token();\n parse(&expr->left);\n parse_operator(&expr->op);\n parse(&expr->right);\n if(token.class!=')')\n Error(\"missing )\");\n get_next_token();\n return 1;\n }\n return 0;\n}\n```\n\nGrammar is:\n\n```\nexpr -> expr | (expr op expr)\ndigit -> 0|1|2....|9\nop -> +|*\n```\n\n========================================\n\nTop Answer:\nNine times out of ten you'll save the AST in memory for whatever you are doing after lexing and parsing are done.\n\nOnce you have an AST you can do a number of things:\n\n- Evaluate it directly (perhaps using recursion, perhaps using your own custom stack)\n\n- Transform it into some other output, such as code in another language or some other type of translation.\n\n- Compile it to preferred instruction set\n\n- etc.\n\n========================================\n\nCode:\n```text\n((2+3)*4) * 2\n          /   |     \\\n       (2+3)  *4    * 2\n        /     | \\\n     (2+3)    *  4\n     / | \\\n    2  + 3\n```\n\n```text\nint parse(Expression &expr)\n{\n  if(token.class=='D')\n  { \n    expr.type='D';\n    expr.value=token.val-'0';\n    get_next_token();\n    return 1;\n  }\n  if(token.class=='(') \n  {\n    expr.type='P';\n    get_next_token();\n    parse(&expr->left);\n    parse_operator(&expr->op);\n    parse(&expr->right);\n    if(token.class!=')')\n      Error(\"missing )\");\n    get_next_token();\n    return 1;\n  }\n  return 0;\n}\n```\n\n```text\nexpr -> expr | (expr op expr)\ndigit   -> 0|1|2....|9\nop  -> +|*\n```\n\n```text\n((2+3)*4) * 2\n```\n\n```text\n#include <stdio.h>\n#include <stdlib.h>\n\nvoid error(const char *what) {\n    fprintf(stderr, \"ERROR: %s\\n\", what);\n    exit(1);\n}\n\nvoid compileLiteral(const char *& s) {\n    int v = 0;\n    while (*s >= '0' && *s <= '9') {\n        v = v*10 + *s++ - '0';\n    }\n    printf(\"    mov  eax, %i\\n\", v);\n}\n\nvoid compileSymbol(const char *& s) {\n    printf(\"    mov  eax, dword ptr \");\n    while ((*s >= 'a' && *s <= 'z') ||\n           (*s >= 'A' && *s <= 'Z') ||\n           (*s >= '0' && *s <= '9') ||\n           (*s == '_')) {\n        putchar(*s++);\n    }\n    printf(\"\\n\");\n}\n\nvoid compileExpression(const char *&);\n\nvoid compileTerm(const char *& s) {\n    if (*s >= '0' && *s <= '9') {\n        // Number\n        compileLiteral(s);\n    } else if ((*s >= 'a' && *s <= 'z') ||\n               (*s >= 'A' && *s <= 'Z') ||\n               (*s == '_')) {\n        // Variable\n        compileSymbol(s);\n    } else if (*s == '-') {\n        // Unary negation\n        s++;\n        compileTerm(s);\n        printf(\"    neg  eax\\n\");\n    } else if (*s == '(') {\n        // Parenthesized sub-expression\n        s++;\n        compileExpression(s);\n        if (*s != ')')\n            error(\"')' expected\");\n        s++;\n    } else {\n        error(\"Syntax error\");\n    }\n}\n\nvoid compileMulDiv(const char *& s) {\n    compileTerm(s);\n    for (;;) {\n        if (*s == '*') {\n            s++;\n            printf(\"    push eax\\n\");\n            compileTerm(s);\n            printf(\"    mov  ebx, eax\\n\");\n            printf(\"    pop  eax\\n\");\n            printf(\"    imul ebx\\n\");\n        } else if (*s == '/') {\n            s++;\n            printf(\"    push eax\\n\");\n            compileTerm(s);\n            printf(\"    mov  ebx, eax\\n\");\n            printf(\"    pop  eax\\n\");\n            printf(\"    idiv ebx\\n\");\n        } else break;\n    }\n}\n\nvoid compileAddSub(const char *& s) {\n    compileMulDiv(s);\n    for (;;) {\n        if (*s == '+') {\n            s++;\n            printf(\"    push eax\\n\");\n            compileMulDiv(s);\n            printf(\"    mov  ebx, eax\\n\");\n            printf(\"    pop  eax\\n\");\n            printf(\"    add  eax, ebx\\n\");\n        } else if (*s == '-') {\n            s++;\n            printf(\"    push eax\\n\");\n            compileMulDiv(s);\n            printf(\"    mov  ebx, eax\\n\");\n            printf(\"    pop  eax\\n\");\n            printf(\"    sub  eax, ebx\\n\");\n        } else break;\n    }\n}\n\nvoid compileExpression(const char *& s) {\n    compileAddSub(s);\n}\n\nint main(int argc, const char *argv[]) {\n    if (argc != 2) error(\"Syntax: simple-compiler <expr>\\n\");\n    compileExpression(argv[1]);\n    return 0;\n}\n```\n\n```text\nmov  eax, 1\npush eax\nmov  eax, dword ptr y\npush eax\nmov  eax, 3\nneg  eax\npush eax\nmov  eax, dword ptr x\nmov  ebx, eax\npop  eax\nadd  eax, ebx\nmov  ebx, eax\npop  eax\nimul ebx\nmov  ebx, eax\npop  eax\nadd  eax, ebx\n```\n\n```text\nmov  eax, dword ptr x\nsub  eax, 3\nimul dword ptr y\ninc  eax\n```\n\n```text\n1+y*(-3+x)\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.774Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":258,"estimatedTokens":1296}}213{"id":"stack-2287821","source":"stackoverflow","questionId":2287821,"title":"How much impact does use of 'var' have on performance of C# Compiler?","tags":["c#","compiler-construction","var"],"text":"Title: How much impact does use of 'var' have on performance of C# Compiler?\nTags: c#, compiler-construction, var\nSource: Stack Overflow\n\nQuestion:\nI find the `var` keyword greatly helps in reducing noise in my C# code, with little loss of readability; I'd say that I now use explicit typing only when the compiler forces me to.\n\nI know that using var does not change the runtime characteristics of my code. But the question has just occurred to me: am I paying a big penalty at compile time for all the extra work that the compiler is now doing on my behalf?\n\nHas anybody done any benchmarks to see how much difference extensive use of `var` makes to **compilation times**?\n\n========================================\n\nTop Answer:\nThe types need to be checked anyway, this may even save time... ok, unlikely :)\n\nYou shouldn't care though - if your development environment is slow, buy more memory or a new computer. Don't change the way you write code.\n\n========================================\n\nCode:\n```text\nvar\n```\n\n```text\nvar\n```\n\n========================================\n\nComments:\n- Do you have any evidence or reasoning for that answer?\n- I erroneously voted for closing (but I cannot take my vote back). The other question is related to some extent, but not exact duplicate.\n- In order to notice a difference the project would have to be so large you would have huge compile times anyway. You would be trying to solve the problem of the 15 min compile and not care about the var keyword. Type inference is one of the main features of F# and there is no measurable difference that I've ever noticed in an F# project that declares NO types. This question is silly imo.\n- I feel this question has not been answered. The compiler obviously performs differently when you use var, until someone does some bench-marking, or come up with accurate reasoning on the impact it would have, this question should have remained open.\n- similar question stackoverflow.com/questions/356846/&hellip;\n- +1 ... and compiling C# is blazing fast if you compare to other languages like C or C++\n- I appreciate the point: I'm not going to change my programming practice because of this - but I would like some specifics on the difference it makes.\n- Do you realize how many different things the compiler does for you already? `var` is just another tiny task...\n- +1 for \"may even save time\". There is no reason to assume that type inference costs time; in fact, it should be 1.271 femtoseconds faster to just *use* the type on the right side than to check if that type is assignable to the left side.\n- Plus parsing the token `var` from text will be faster than parsing the token `IDictionary>`, saving yet another 0.431 femtoseconds.\n- The length of the list doesn't really say anything about the relative temporal weights of each item, though.\n- @didibus: I encourage you to post an answer that you like better.\n- In retrospect, I see I was a bit out of touch. I just wish the OP would keep this question unanswered so that maybe eventually, someone who does have actual benchmark data or reasoning towards the performance implications on the compiler would come them here. Or if the OP actually followed this answer, and performed benchmarks of his own, if he could the results here.","metadata":{"transformedAt":"2026-08-18T18:32:17.774Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":45,"estimatedTokens":818}}214{"id":"stack-4603701","source":"stackoverflow","questionId":4603701,"title":"Garbage collection when compiling to C","tags":["c","compiler-construction","garbage-collection"],"text":"Title: Garbage collection when compiling to C\nTags: c, compiler-construction, garbage-collection\nSource: Stack Overflow\n\nQuestion:\nWhat are the techniques of garbage collection when compiling a garbage collected language to C? I know of two:\n\nmaintain a shadow stack that saves all roots explicitly in a data structure\n\nuse a conservative garbage collector like Boehm's\n\nThe first technique is slow, because you have to maintain the shadow stack. Potentially every time a function is called, you need to save the local variables in a data structure.\n\nThe second technique is also slow, and inherently does not reclaim all garbage because of using a conservative garbage collector.\n\nMy question is: what is the state of the art of garbage collection when compiling to C. Note that I do not mean a convenient way to do garbage collection when programming in C (this is the goal of Boehm's garbage collector), just a way to do garbage collection when *compiling to C*.\n\n========================================\n\nCode:\n```text\nstruct vm\n{\n    struct scope *root;\n};\n\nstruct scope\n{\n    struct scope *prev, *next;\n    size_t size;\n    struct ref *refs;\n};\n\nvoid foo(struct vm *vm, struct scope *caller)\n{\n    struct ref local_refs[42];\n    struct scope scope = {\n        caller, NULL, sizeof local_refs / sizeof *local_refs, local_refs };\n\n    caller->next = &scope;\n\n    // ...\n\n    caller->next = NULL;\n}\n```\n\n========================================\n\nComments:\n- This is a nasty one. It's also why e.g. LLVM and C-- are popular (they allow garbage collection without dealing with it maually).\n- Is the type system in the originating language based on DAGs or general graphs? DAGs only need reference counting (as a consequence of being acyclic).\n- General graphs, unfortunately. I also thought about reference counting, but that doesn't seem to solve everything because you still need a way to traverse the roots to collect cycles...\n- What does it mean to compile *to* C? Can you give a sample command?\n- This kind of technique is most useful when you're using C as a target language for a compiler for some other language, as its error prone and requires carefully keeping track of everything\n- Thanks, that's an interesting technique. However doesn't this mean that local variables don't live in registers, which is most likely even slower than saving locals when making a call?\n- @Jules: the compiler is still free to cache any local value in registers for computations, but you're right, it has to sync the values on function call; I'll have to think about how aproptiate use of `const` and `restrict` could improve the situation by telling the compiler that the called function won't modify the locals...","metadata":{"transformedAt":"2026-08-18T18:32:17.774Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":57,"estimatedTokens":677}}215{"id":"stack-213427","source":"stackoverflow","questionId":213427,"title":"Translate C# code into AST?","tags":["c#","compiler-construction","abstract-syntax-tree"],"text":"Title: Translate C# code into AST?\nTags: c#, compiler-construction, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nIs it currently possible to translate C# code into an Abstract Syntax Tree?\n\nEdit: some clarification; I don't necessarily expect the compiler to generate the AST for me - a parser would be fine, although I'd like to use something \"official.\" Lambda expressions are unfortunately not going to be sufficient given they don't allow me to use statement bodies, which is what I'm looking for.\n\n========================================\n\nTop Answer:\nIs it currently possible to translate C# code into an Abstract Syntax Tree?\n\nYes, trivially in special circumstances (= using the new Expressions framework):\n\n```\n// Requires 'using System.Linq.Expressions;'\nExpression> f = x => x * 2;\n```\n\nThis creates an expression tree for the lambda, i.e. a function taking an `int` and returning the double. You can modify the expression tree by using the Expressions framework (= the classes from in that namespace) and then compile it at run-time:\n\n```\nvar newBody = Expression.Add(f.Body, Expression.Constant(1));\nf = Expression.Lambda>(newBody, f.Parameters);\nvar compiled = f.Compile();\nConsole.WriteLine(compiled(5)); // Result: 11\n```\n\nNotice that all expressions are immutable so they have to be built anew by composition. In this case, I've prepended an addition of 1.\n\nNotice that these expression trees only work on real expressions i.e. content found in a C# function. You can't get syntax trees for higher constructs such as classes this way. Use the CodeDom framework for these.\n\n========================================\n\nCode:\n```text\nSyntaxTree tree = SyntaxTree.ParseCompilationUnit(\n    @\" C# code here \");\nvar root = (CompilationUnitSyntax)tree.Root;\n```\n\n```text\n// Requires 'using System.Linq.Expressions;'\nExpression<Func<int, int>> f = x => x * 2;\n```\n\n```text\nvar newBody = Expression.Add(f.Body, Expression.Constant(1));\nf = Expression.Lambda<Func<int, int>>(newBody, f.Parameters);\nvar compiled = f.Compile();\nConsole.WriteLine(compiled(5)); // Result: 11\n```\n\n```text\nint\n```\n\n========================================\n\nComments:\n- Wonder how is ur attempt on translating C# code to AST going?\n- Erik accepted this? It uses the very lambda forms he said he didn't want.\n- Ira: you should pay attention to the development of the discussion. This entry was posted *before* Erik’s edit/clarification. Apparently, none of the other answers were better *at the time* (notice: *one year ago!*) so he didn’t accept another answer. Your answer is probably what he would have wanted.\n- This is helpful to see what C# don't offer a library for us to manipulate C# API. It is due to it's compiler is a classical one, a black box!\n- I've used ANTLR in the past, and it's quite nice. I haven't used the C# grammar, but most of the contributors there are pretty cluey.\n- @Cheeso: Hmm, 2004 would mean we scooped MS. Well, it never do to suggest that, so I modified it say 2010. Fixed.\n- Moreover. Nemerle can compile C# sources using Nemerle compiler ! :)","metadata":{"transformedAt":"2026-08-18T18:32:17.774Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":69,"estimatedTokens":769}}216{"id":"stack-711732","source":"stackoverflow","questionId":711732,"title":"Is keyword 'event' optional in C#?","tags":["c#","events","compiler-construction"],"text":"Title: Is keyword 'event' optional in C#?\nTags: c#, events, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nWhat is the difference between eventOne (with keyword 'event') and eventTwo (w/o keyword)?\n\n```\nclass Program\n{\n public event EventHandler eventOne;\n public EventHandler eventTwo;\n\n public void RaiseOne()\n {\n if (eventOne != null)\n eventOne(this, EventArgs.Empty);\n }\n\n public void RaiseTwo()\n {\n if (eventTwo != null)\n eventTwo(this, EventArgs.Empty);\n }\n\n static void Main(string[] args)\n {\n var p = new Program();\n p.eventOne += (s, e) => Console.WriteLine(\"One\");\n p.eventTwo += (s, e) => Console.WriteLine(\"Two\");\n p.RaiseOne();\n p.RaiseTwo();\n }\n}\n```\n\n========================================\n\nTop Answer:\nBy using the **event** keyword you tell C# go generate to hidden methods, **add_XXX** and **remove_XXX** for your underlying delegate. This makes sure that anyone using your class can only attach and remove delegates to the event. The key point is that nobody outside your class and raise the event, so you've got complete control over when this will happen.\n\nIf you don't use **event** then you're just exposing a public delegate that anyone can add to, remove from and invoke. It's highly unlikely that you want anyone other than your class to do the invoking.\n\n========================================\n\nCode:\n```text\nclass Program\n{\n    public event EventHandler eventOne;\n    public EventHandler eventTwo;\n\n    public void RaiseOne()\n    {\n        if (eventOne != null)\n            eventOne(this, EventArgs.Empty);\n    }\n\n    public void RaiseTwo()\n    {\n        if (eventTwo != null)\n            eventTwo(this, EventArgs.Empty);\n    }\n\n    static void Main(string[] args)\n    {\n        var p = new Program();\n        p.eventOne += (s, e) => Console.WriteLine(\"One\");\n        p.eventTwo += (s, e) => Console.WriteLine(\"Two\");\n        p.RaiseOne();\n        p.RaiseTwo();\n    }\n}\n```\n\n```text\neventOne\n```\n\n```text\nEventHandler\n```\n\n```text\neventTwo\n```\n\n```text\nEventHandler\n```\n\n```text\nusing System;\nclass Program\n{\n    static void Main(string[] args)\n    {\n        var a = new A();\n        a.eventOne += (s, e) => Console.WriteLine(\"One\");\n        a.eventTwo += (s, e) => Console.WriteLine(\"Two\");\n        a.RaiseOne();\n        a.RaiseTwo();\n                // won't compile\n        a.eventOne(null, EventArgs.Empty);\n        a.eventTwo(null, EventArgs.Empty);\n    }\n\n}\n\nclass A {\n    public event EventHandler eventOne;\n    public EventHandler eventTwo;\n\n    public void RaiseOne()\n    {\n        if (eventOne != null)\n            eventOne(this, EventArgs.Empty);\n    }\n\n    public void RaiseTwo()\n    {\n        if (eventTwo != null)\n            eventTwo(this, EventArgs.Empty);\n    }\n}\n```\n\n========================================\n\nComments:\n- Have you had a look at actual IL? You will be amazed.\n- Yes, I have. What amazed you?\n- While 'add' and 'remove' are actually generated, compiler optimized them out, so in the code above the only difference will be access modifiers on delegates.\n- No, the compiler isn't \"optimising them out\". *Within* the class, eventOne refers to the field. From *outside* the class, it refers to the event.\n- (See section 10.8.1 of the C# 3.0 spec for more details.)\n- So the reason Prankster's code shows no apparent difference is because all the actions on eventOne and eventTwo were performed within the same class? Code that accessed them from a different class would illustrate the difference?","metadata":{"transformedAt":"2026-08-18T18:32:17.774Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":136,"estimatedTokens":867}}217{"id":"stack-3955371","source":"stackoverflow","questionId":3955371,"title":"Why do almost all OO languages compile to bytecode?","tags":["oop","compiler-construction","programming-languages","bytecode"],"text":"Title: Why do almost all OO languages compile to bytecode?\nTags: oop, compiler-construction, programming-languages, bytecode\nSource: Stack Overflow\n\nQuestion:\nOf the object-oriented languages I know, pretty much all but C++ and Objective-C compile to bytecode running on some sort of virtual machine. Why have so many different languages settled on compiling to bytecode, as opposed to machine code? Is it possible in princible to have a high-level memory-managed OOP language that compiled to machine code?\n\nEdit: I'm aware that multiplatform support is often advanced as an advantage of this approach. However, it's quite possible to compile natively on multiple platforms, without making a new compiler per platform. One can, per example, emit C code and then compile that with GCC.\n\n========================================\n\nTop Answer:\nThis is done to allow a VM or JIT compiler the chance to compile the code on demand optimally for the architecture on which the code is executed. Also, it allows for cross-platform bytecode to be created once and then executed on multiple hardware architectures. This allows for hardware specific optimizations to be placed into the compiled code.\n\nSince byte code is not limited to a microarchitecture, it can be smaller than machine code. Complex instructions can be represented vs. the much more primitive instructions available in modern day CPUs, since the constraints in the design of CPU instructions are very different from the constraints in designing a bytecode architecture.\n\nThen there's the issue of security. The bytecode can be verified and analyzed prior to execution (i.e., no buffer overflows, variables of a certain type being accessed as something they are not), etc...\n\n========================================\n\nCode:\n```text\nfoo + bar;\n```\n\n========================================\n\nComments:\n- I guess this is because they are intended for efficiency, and bytecode/VM almost always comes with a performance tradeoff. Note, however, that both C++ and Objective-C compile to LLVM bytecode that afterwards can be converted to native code.\n- @Diego: In *some* implementations. `gcc` doesn't compile to LLVM, and I'm pretty sure Visual Studio doesn't either.\n- @sepp2k: I don't understand what do you mean with \"gcc doesn't compile to LLVM\". LLVM is able to compile C, C++, and Objective-C to bytecode and to native code afterwards (of course, in some platforms) by itself.\n- @Diego: I mean that while clang uses LLVM, gcc does not. So saying \"C++ compiles to LLVM\" is misleading because it makes it sound as if all or most C++ implementations compiled to LLVM.\n- I don't think there is any reason to link bytecode/VM based languages with OOP. As you have notes yourself, C++ and Objective-C do not use VMs. Rather it is more of a trend in newer languages, which also happen to have good OOP support. IMHO, just because the two occur together, it does not mean one implies the other.\n- @sepp2k `gcc` can use LLVM if you want it to. Check out the dragonegg.\n- emitting c code is possible but its a bit of a nightmare.. works well for small projects. . Another benefit of a bytecode interpreter is no or a light toolchain. Thus making it more suitable for users.\n- These are compelling features of VM - but they are nearly orthogonal to OOP.\n- Verification is not necessarily possible, the byte code must have been designed to allow it.\n- LUA compiles to bytecode, and runs on a VM, but is just as flexible as Javascript, as far as program structuring goes. I believe (at least some) LISP implementations do this, as well.\n- Python *is* compiled. This happens when you run the script, if it didn't happen earlier.\n- hmm, i take issue with juxtaposing byte code vs \"interpeter\" because byte code (AKA p-code) is executed by an interpreter - even if you call that a VM. You mean \"pure interpretation\" there. Also, Javascript is not necessarily only pure interpreter - Google Chrome *compiles* JS to native code (lookup V8 Javascript engine). Ruby has a bytecode interpreter nowadays too, the so called YARV (vs previous MRI).\n- There are key difference between byte code and interpreters. Interpreters have to parse source code and build ASTs in order to interpret and validate code. Bytecode does not. Even interpreting the cached AST performs far worse than byte code. Calling byte code an interpreter is vastly under estimating the performance of byte code vs. interpreters. While most languages are now byte code optimized they did not start out that way. Early versions of Javascript, LUA, Python, and Perl did not start out with byte code. Ruby only got YARV in the last two years of it's almost 20 year life.\n- Translation of text to an intermediate representation is often not the most expensive part of compilation. I think a bigger issue relates to lookups. If code in one class file used a field of a class defined in another, and both files were stored as text, resolution of a single the field access could require scanning the entire second file. A bytecode file, by contrast, can include a list of fields defined in a class and their addresses, as well as a list of foreign-class fields which are accessed; the runtime can use both lists to build in memory a list of foreign-class field addresses.\n- I think you meant \"heavily\" instead of \"hardly\" in your last sentence.\n- Microsoft's .Net runs on non-PC architectures as well - Xbox360 and Windows Mobile 7, hence portability.\n- I suppose .Net compiles to bytecode for cross-language compatibility\n- @thelaststud: Yes, that's probably it.\n- Just as a side note, reading this would be better if you fixed your spelling.\n- Ironically, I don't understand the meaning of your suggestion. I'm no natural english speaker; what does \"(to) fix spelling\" mean?\n- @slomojo - What he means is that your answer has lot of spelling mistakes IE: **cathegory->category**\n- I disagree with your ease hierarchy. bytecode interpreters are easier to write than pure interpreters, and JITs are harder to write than to-machine-language compilers.\n- oh, I meant bytecode interpreter + to-bytecode-compiler. With the JIT-compiler, that depends on how far and where it optimizes the code: A JIT-compiler itself could be written to run compiled, interpreted, or self-optimizing at runtime via an other JIT-compiler. The last case does not need to compile to machine code, instead it might just fuse its own (partially) compiled routines. For example, it is really easy to write a LISP-to-LISP (JIT-)compiler in LISP or any other functional language – even with different LISP-dialects.","metadata":{"transformedAt":"2026-08-18T18:32:17.774Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":51,"estimatedTokens":1646}}218{"id":"stack-24306893","source":"stackoverflow","questionId":24306893,"title":"What is the difference between compilation and interpretation?","tags":["compiler-construction","compilation","interpreter","interpretation"],"text":"Title: What is the difference between compilation and interpretation?\nTags: compiler-construction, compilation, interpreter, interpretation\nSource: Stack Overflow\n\nQuestion:\nI just had a conversation with a colleague and where were talking about the V8 JavaScript engine. According to Wikipedia,\n\n V8 compiles JavaScript to native machine code [...] before executing it, instead of more traditional techniques such as interpreting bytecode or compiling the whole program to machine code and executing it from a filesystem.\n\nwhere (correct me if I'm wrong) \"*interpreting bytecode*\" is the way Java works, and \"*compiling the whole program*\" would apply for languages like C or C++. Now we were wondering, debating and posing false assertions and presumptions about differences, similarities. To end this, I recommended asking the experts on SO.\n\nSo, who is able to\n\n- name, explain and/or reference all major methods (e.g. precompiling vs. runtime interpretation)\n\n- to visualize or to provide a scheme about the relations between source, compilation and interpretation\n\n- give examples (name programming languages) for the major methods of #1.\n\nNotes:\n\n- I am not looking for a long prosaic essay about the different paradigms, but an visually supported, quick overview.\n\n- I know that Stackoverflow is not intended to be a encyclopedia for programmers (but rather a Q&A platform for more specific questions). But since I can find a lot of popular questions, that kind of provide an encyclopedic view to certain topics (e.g. [1], [2], [3], [4], [5]), I started this question.\n\n- If this question would rather fit into any other StackExchange site (e.g. cstheory), please let me know or flag this question for moderation.\n\n========================================\n\nTop Answer:\nMany execution environments nowadays use bytecode (or something similar) as an intermediate representation of the code. So the source code is first *compiled* into an intermediate language, which is then either *interpreted* by a virtual machine (which decodes the bytecode instruction set) or is *compiled further* into machine code, and executed by the hardware.\n\nThere are very few production languages which are interpreted *without* being precompiled into some intermediate form. However, it’s easy to conceptualise such an interpreter: just think of a class hierarchy with subclasses for every type of language element (`if` statement, `for`, etc.), and each class having an `Evaluate` method which evaluates a given node. This is also commonly known as the interpreter design pattern.\n\nAs an example, consider the following code fragment implementing an `if` statement in a hypothetical interpreter (implemented in C#):\n\n```\nclass IfStatement : AstNode {\n private readonly AstNode condition, truePart, falsePart;\n\n public IfStatement(AstNode condition, AstNode truePart, AstNode falsePart) {\n this.condition = condition;\n this.truePart = truePart;\n this.falsePart = falsePart;\n }\n\n public override Value Evaluate(EvaluationContext context) {\n bool yes = condition.Evaluate(context).IsTrue();\n if (yes)\n truePart.Evaluate(context);\n else\n falsePart.Evaluate(context);\n return Value.None; // `if` statements have no value.\n }\n}\n```\n\nThis is a very simple but fully functional interpreter.\n\n========================================\n\nCode:\n```csharp\nclass IfStatement : AstNode {\n    private readonly AstNode condition, truePart, falsePart;\n\n    public IfStatement(AstNode condition, AstNode truePart, AstNode falsePart) {\n        this.condition = condition;\n        this.truePart = truePart;\n        this.falsePart = falsePart;\n    }\n\n    public override Value Evaluate(EvaluationContext context) {\n        bool yes = condition.Evaluate(context).IsTrue();\n        if (yes)\n            truePart.Evaluate(context);\n        else\n            falsePart.Evaluate(context);\n        return Value.None; // `if` statements have no value.\n    }\n}\n```\n\n```text\nif\n```\n\n```text\nfor\n```\n\n```text\nEvaluate\n```\n\n```text\nif\n```\n\n========================================\n\nComments:\n- Surprised this doesn't have more votes as it's an important question with some fantastic answers.\n- \"True interpreters\" is an unfortunate choice of words. It's a matter of layers and POV. It can be argued that even a bytecode interpreter is not a \"true interpreter\" because it needs someone to compile the source code to bytecode before it interprets it. A \"true interpreter\" reads a bit of *source code* line-by-line or statement-by-statement and immediately executes each as it is recognized. A bytecode interpreter is an interpreter, too, but only if you ignore the previous compilation step, which turns it into a VM or virtual CPU.\n- @uliwitness I don’t agree with this assessment. There are simply two different phases here which should not be conflated: the Python interpreter *is* a true interpreter, even though it interprets bytecode rather than Python code – either statement by statement (that’s known as a REPL) or one file at a time. The original source code is *also* compiled into an intermediate format but this doesn’t detract from the fact that that intermediate format is then interpreted, and that the implementation of this step is classified as an interpreter.\n- But where do you stop this distinction? Intel CPUs these days contain an interpreter that translates the Intel instruction set into the actual RISC-like hardware instructions. At that POV, all code is interpreted. We need to distinguish terminology here. Traditionally, \"interpreted programming language\" has meant the source code is interpreted, not bytecode. There is both the algorithm called \"interpreter\" (which a bytecode interpreter uses) and the higher-level concept of an \"interpreted programming language\" which the OP asked about. We shouldn't conflate them.\n- Konrad: following that reasoning (classic pre JIT) Java is also an interpreter, since it compiles to bytecode, and the VM is an interpreter. It is dangerous to name the whole process after one phase of it. A good working definition of compilation is translation to a level closer to the machine. That applies to bytecode generation too.\n- @Marco But Java’s JIT does **not** interpret the bytecode. It compiles it to machine code.\n- @uliwitness “Where to stop this distinction?” – I never spoke of stopping anywhere. But you’ve already made the (mostly used, and usually meaningful) distinction yourself: at the software/hardware interface. But on the other hand, as you’ve said: “At that POV, all code is interpreted” – that is correct, and at that POV it’s an entirely meaningful definition to work with.\n- @KonradRudolph, the dominant JVM implementation, HotSpot, is indeed an interpreter. It only JIT-compiles the congested paths, and the single use paths are interpreted directly.\n- SK-logic stole my line, though I didn't know if it still held for current versions. OTOH this is all nitpicking. C# doesn't do interpretation, and is only marginally different. My point was more that putting a hard division line on compilation vs interpretation purely on generating machinecode at some point is quite arbitrary. Systems that are nearly equivalent (like Java and C#) suddenly are separated by a wide rift.\n- @Marco I’ve removed the inaccurate portion. That said, I stand by the rest of what I’ve said. I actually almost agreed with your last comment until I thought about it some more: you are trying to make Java and C# seem almost identical – and from the user perspective they may well be, but this isn’t what we are talking about here. We are talking about how they are executed, and as SK-logic has mentioned, they are treated in quite fundamentally different ways, even if some phases overlap. This – no more, no less – is what I’m saying. (continued …)\n- @Marco In particular, contrary to what your first comment stated, I’ve never claimed, nor implied, that bytecode generation wasn’t compilation, and it was not my intention to “name the whole process after one phase of it” – instead, I responded to one particular point in OP’s question (about which, it turns out, I was wrong, since I thought that Java byte code is always JIT-compiled same as the .NET Intermediate Language).\n- As said I don't know the exact current situation, since I don't track Java (or C#) anymore. I can vaguely remember they wanted to remove the interpretation with Java 1.5. It is also possible they left it in J2ME, since there it is a great saver of memory. (always JITting has a memory footprint impact IIRC). And *I* stand by my case that if a definition creates a wide rift between Java and C#, the definition is flawed :_)\n- “This 'keeping around' makes it slower than a pure interpreter” – on the contrary: JITs are usually (and were originally *designed* to be) substantially faster than pure interpreters.\n- @konrad-rudolph See the explanation below that. For translating an entire program and running every expression once, it is slower (in the absolute), but since that pretty much never happens in practice, as at least some part is *usually* executed twice or not at all, JIT is faster. But for an example of a JIT being slower than a pure interpreter, look no farther than the \"Hello World\" example.\n- I’m not convinced. By the same token compiled code is slower than interpreted code but that statement is obviously misleading and simply not true except for trivial cases (your “Hello World” example). In neither case is “slower than a pure interpreter” an informative statement.\n- @konrad-rudolph I'm explaining the theory behind the different approaches and the trade-offs here, which is needed to choose the weighting of any hybrid approach (which e.g. JITs are, they are hybrid compiler/interpreter/cache constructs). It is *exactly* the non-practical, theoretical case you need to know here. Only *then* do you look at your concrete practical use case (which differs for a web site popup, a web input field filter, a one-off batch operation or systems programming) and decide which combination will be best for you.\n- @konrad-rudolph I've added a qualifier behind the \"slower than a pure interpreter\" to give an example of which part is actually slower. Also, look at Safari on Mac for an example that uses almost all approaches. The host itself is compiled, which is fastest execution. JavaScript is usually interpreted for one-offs like onLoad (least overhead), bytecode interpreted for stuff that gets run a few times (more overhead but no repeated parsing), and stuff that's run repeatedly (like an input field filter) is compiled using the new FTL feature.\n- Good comments, they should almost be part of the answer though, I feel.\n- @uliwitness JIT Compiler as you have mentioned \" makes the code run slower\" than the pure interpreter is absolutely wrong. Why we have a JIT compiler first hand is because it does code optimizations to run the code faster than the pure interpreter. And it is up to the developer and programmer to adhere to the best coding practices for JIT compiler to optimize our code.\n- @ImranRafiqRather \"slower\" is a fuzzy word. As the comment in brackets mentions, code *starts running* slower (\"overhead\"). I am not saying that the code runs more slowly when it gets around to actually running the individual instructions. For more detail, you can read a more exhaustive version of this post here: orangejuiceliberationfront.com/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.775Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":122,"estimatedTokens":2846}}219{"id":"stack-1630417","source":"stackoverflow","questionId":1630417,"title":"What is the *conceptually* smallest *compiler* that can compile itself?","tags":["compiler-construction"],"text":"Title: What is the *conceptually* smallest *compiler* that can compile itself?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn the spirit of this question, I'd like to ask a similar question, but about *compilers*, not about *interpreters*.\n\nWhat is the *conceptually smallest* compiler that can *compile* its own code?\n\nWhen I say \"conceptually smallest\" I mean that it uses only very basic concepts and builds up from there, rather than that it contains very short code. An example of why this is an important distinction is OTCC a very tiny C compiler which is small because it's obfuscated, not necessarily because it is conceptually simple (it may also be conceptually simple, but I don't know; it's obfuscated).\n\nI would also like to add that the following also might be a very conceptually small program, but it doesn't actually tell us anything about what's going on, so it's not really what I'm looking for either:\n\n```\n(writefile argv[2] (generate (parse (readfile argv[1]))))\n```\n\nWhat I'm really looking for is a language that is:\n\n- Turing complete.\n\n- Capable of compiling itself.\n\nI'm interested in this because\n\n- it would be an interesting case study and\n\n- it could be useful as a starting point for bootstrapping compilers.\n\nIf it doesn't exist, I may just write it myself. :)\n\n========================================\n\nTop Answer:\n[I know this is a very late entry, but I think this is really relevant].\n\nThe smallest self-compiling compiler I know about is Val Schorre's 1963 MetaII compiler. Yes, from Nineteen Sixty Three.\n(There's a link on that page to his technical paper on the topic).\nIf you like compilers, *run* to get this paper; its a gas, and its only 10 pages. \n\nThis isn't theory; this is practical. His paper provides with the compiler source code (some 20-30 lines IIRC), a description of metacompiling machinery, and a metacompiled program processes the source code and regenerates the exact same metacompiled program. You can replicate this result yourself in 1-2 days of really fun if not mind boggling codeing to implement the metamachine. [I learned to build compilers from this paper back in 1970 by doing exactly this].\nOr, you can go play with a modern tutorial on MetaII that has it all prebuilt in JavaScript.\n\nOnce you have this metacompiler running, you can extend the syntax and the metamachine easily to bootstrap to larger metacompilers with more features, and/or to generate compilers for real applications. (I built a Pascal like BASIC compiler this way in the early 70s).\n\nYou can go the other way: you can start taking things out, and see how much you can remove and still be able to boostrap back up to the MetaII level. I did this once and managed to get rid of about 30% without losing cability or even a lot of expressive power; it dropped to some 20 lines of text and, remarkably, a simpler meta-machine.\n\nA clever fellow named Doug Michels, associated a long time ago with the 1980s (Unix supplier) Santa Cruz Operation, told me that he had gone considerably further and reduced the metacompiler self description to a very small number of characters. I never saw the work so I don't really know how far he got.\n\n[EDIT] Dig, dig, dig... found this gem (on Linkedin):\n\nBill McKeeman, Adjunct Faculty at Dartmouth said:\n\n*Doug was my undergraduate student; his senior thesis assignment was simple: write the shortest, extendable, self-compiling compiler. The front end took **27 characters**; the whole thing took 63. It all fit on one IBM card. He published the result.*\n\nDig, dig, dig some more: This seems to be Doug's 27 character paper. See Figure 2. By \"front end\", McKeeman apparantly means \"just the parser\"; the paper contains full translators that are a little larger.\n\nYou can't get compilers this small unless they *are* \"conceptually simple\".\n\n========================================\n\nCode:\n```text\n(writefile argv[2] (generate (parse (readfile argv[1]))))\n```\n\n```text\ndel SelfReplication.exe\n csc SelfReplication.cs\n move SelfReplication.cs SelfReplication-old.cs\n SelfReplication.exe\n```\n\n========================================\n\nComments:\n- it's a different question but has some overlap - might wanna have a look --> stackoverflow.com/questions/1585067/&hellip;\n- If you're not sure if OTCC is conceptually simple or not, look at the unobfuscated source code. The symbol names are somewhat terse, but it's decently well-commented.\n- possible duplicate of What language features are required in a programming language to make a compiler?\n- Couldn't you just use a language with a built in \"compile\" command?\n- Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.","metadata":{"transformedAt":"2026-08-18T18:32:17.775Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":81,"estimatedTokens":1221}}220{"id":"stack-6552141","source":"stackoverflow","questionId":6552141,"title":"How can a stack underflow happen in C++?","tags":["c++","compiler-construction","stackunderflow"],"text":"Title: How can a stack underflow happen in C++?\nTags: c++, compiler-construction, stackunderflow\nSource: Stack Overflow\n\nQuestion:\nWhat is a simple example in C++ that causes a stack underflow in the case of invoking and returning from method calls?\n\nI am familiar with the calling convention, i.e `thiscall`, `stdcall` and the `cdecl` and way they would clean the stack. Wouldn't a stack underflow automatically be taken care of by the code generated by the compiler?\n\nWhat are the situations that can get me into trouble with stack underflow?\n\n========================================\n\nTop Answer:\nI am not sure if you are talking of the data structure stack and the underflow problem in it or something else. As far as the `stack(data structure) underflow` problem is concerned here is a explanation.\n\n`stack` is a last in, first out (LIFO) abstract data type and data structure. A stack can have any abstract data type as an element, but is characterized by only three fundamental operations: ***push***, ***pop*** and ***stack top***. \n\nThe ***push*** operation adds a new item to the top of the stack, or initializes the stack if it is empty. If the stack is full and does not contain enough space to accept the given item, the stack is then considered to be in an overflow state. The pop operation removes an item from the top of the stack. \n\nA ***pop*** either reveals previously concealed items, or results in an empty stack, but if the stack is empty then it goes into ***underflow state*** (It means no items are present in stack to be removed). \n\nThe ***stack top*** operation gets the data from the top-most position and returns it to the user without deleting it. The same underflow state can also occur in stack top operation if stack is empty.\n\n*Consider a stack implementation example:*\n\n```\ntemplate class Stack \n{\npublic:\n bool isEmpty() const;\n size_t size() const;\n Item pop();\n void push(const Item& it);\nprivate:\n\n};\n```\n\nNow consider the following operations being performed on this stack.\n\n```\nC++ command resulting stack\n------------------------------------------------\nStack S;\n _____ (empty stack of ints)\n\nS.push(7); \n | 7 | <-- top\n -----\n\nS.push(2); \n | 2 | <-- top \n | 7 |\n -----\n\nS.push(73); \n |73 | <-- top \n | 2 |\n | 7 |\n -----\n\nS.pop(); \n | 2 | <-- top\n | 7 | -----\nS.pop(); \n -----\nS.pop(); \n | 7 | <-- top\n -----\nS.pop(); \n ----- (empty)\n\nS.pop(); \n ERROR \"stack underflow\"\n```\n\n========================================\n\nCode:\n```text\nthiscall\n```\n\n```text\nstdcall\n```\n\n```text\ncdecl\n```\n\n```text\nstdcall\n```\n\n```text\ncdecl\n```\n\n```text\nthiscall\n```\n\n```text\ntemplate <class Item> class Stack \n{\npublic:\n    bool isEmpty() const;\n    size_t size() const;\n    Item pop();\n    void push(const Item& it);\nprivate:\n\n};\n```\n\n```text\nC++ command                      resulting stack\n------------------------------------------------\nStack<int> S;\n                                  _____ (empty stack of ints)\n\n\n\nS.push(7);                            \n                                  | 7 |  <-- top\n                                  -----\n\nS.push(2);                            \n                                  | 2 |  <-- top \n                                  | 7 |\n                                  -----\n\nS.push(73);                           \n                                  |73 |  <-- top \n                                  | 2 |\n                                  | 7 |\n                                  -----\n\nS.pop();                           \n                                  | 2 |  <-- top\n                                  | 7 |                    -----\nS.pop();      \n                                  -----\nS.pop();                           \n                                  | 7 |  <-- top\n                                  -----\nS.pop();                           \n                                  -----  (empty)\n\nS.pop();                           \n                    ERROR \"stack underflow\"\n```\n\n```text\nstack(data structure) underflow\n```\n\n```text\nstack\n```\n\n========================================\n\nComments:\n- Are you sure you're not talking about stack *over* flow?\n- I think you have the wrong forum. This is stack OVERflow. If you want to find out about stack UNDERflow, you should visit that site. ;) sorry, had to do it...\n- @Martinho and @eeeeaaii - umm.. a stack underflow is a actual issue as well, although not something you would run into often.\n- I almost answered the question reading stackoverflow. May be because of my time spent on this forum made me to read stack-underflow as stackoverflow :)\n- Yes.. an experienced programmer eye can catch a stack-overflow. What hacky cases could create a stack-underflow ?? Messing with the calling conventions ??\n- @Eric: you can cause a stack underflow in the call stack? Without invoking UB? Can you please elaborate? Maybe make that an answer?\n- @de costo: I think `while (1) { __asm pop EAX }` would do it (it's not standard C++ of course).\n- @Eric Petroelje yes I know, I was joking\n- I like how there's \"C++\" in the title twice, even though you don't put the language in the title on SO :)\n- I should have authored this question... :)\n- As in calling a invoking a method call defined with a different calling convention ??\n- Yep, if the compiler didn't catch it then simply ending a loop that didn't start would give underflow.\n- @Sean: could this also be caused by an incorrect forward declaration of a function? like `void foobar(int a);` and in another place `void foobar(int a, int b);`?\n- @Alex If you use stack framing then correctly poping the stack is not a problem.\n- @Jean-BaptisteYun&#232;s what does stack framing prevent? The OP asks on scenarios where this could go wrong, so are you suggesting that turning off stack framing would allow this?\n- @alex Stack framing would prevent bad arg-by-arg poping in the case of incoherent prototyping, isn't it?\n- @Jean-BaptisteYun&#232;s do you have a reference to what Stack framing is? If you are just referring to calling conventions and frame sizes, then it would not prevent anything, because it's about mismatching contracts.\n- @Sean And here I am thinking that this wasn't a real thing... interesting!","metadata":{"transformedAt":"2026-08-18T18:32:17.775Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":184,"estimatedTokens":1546}}221{"id":"stack-39075510","source":"stackoverflow","questionId":39075510,"title":"%option noinput nounput: what are they for?","tags":["compiler-construction","flex-lexer","lexer"],"text":"Title: %option noinput nounput: what are they for?\nTags: compiler-construction, flex-lexer, lexer\nSource: Stack Overflow\n\nQuestion:\nI am new in this, so I wondered why do I need to use these directives\n\n```\n%option nounput\n%option noinput\n```\n\nYeah, I am aware that otherwise I'd have these warnings:\n\n```\nlex.yy.c:1237:17: warning: ‘yyunput’ defined but not used [-Wunused-function]\n static void yyunput (int c, register char * yy_bp )\n ^\nlex.yy.c:1278:16: warning: ‘input’ defined but not used [-Wunused-function]\n static int input (void)\n ^\n```\n\nWhat is actually the matter with these directives in flex?\nIn which case I'd be able to use these functions (what for):\n\n```\nstatic void yyunput (int c, register char * yy_bp );\nstatic int input (void);\n```\n\nWhat are the for?\n\n========================================\n\nCode:\n```text\n%option nounput\n%option noinput\n```\n\n```text\nlex.yy.c:1237:17: warning: ‘yyunput’ defined but not used [-Wunused-function]\n    static void yyunput (int c, register char * yy_bp )\n                ^\nlex.yy.c:1278:16: warning: ‘input’ defined but not used [-Wunused-function]\n    static int input  (void)\n               ^\n```\n\n```text\nstatic void yyunput (int c, register char * yy_bp );\nstatic int input  (void);\n```\n\n```text\ninput\n```\n\n```text\nunput\n```\n\n```text\ninput()\n```\n\n```text\ninput()\n```\n\n```text\nunput(c)\n```\n\n```text\nunput\n```\n\n```text\nyyless\n```\n\n```text\ninput\n```\n\n```text\nunput\n```\n\n```text\n%option\n```\n\n```text\ninput\n```\n\n```text\nunput\n```\n\n========================================\n\nComments:\n- Thank you so much for this explanation! ^__^\n- @rici The `%option noinput` works, but I cannot find it in the flex manual. Do you have any reference to it?\n- @magjac: at the end of the options overview in the paragraph starting \"A number of options are available...\"","metadata":{"transformedAt":"2026-08-18T18:32:17.775Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":15,"totalLines":109,"estimatedTokens":452}}222{"id":"stack-1147623","source":"stackoverflow","questionId":1147623,"title":"Trying to understand gcc's complicated stack-alignment at the top of main that copies the return address","tags":["linux","gcc","assembly","x86","compiler-construction"],"text":"Title: Trying to understand gcc's complicated stack-alignment at the top of main that copies the return address\nTags: linux, gcc, assembly, x86, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nhi I have disassembled some programs (linux) I wrote to understand better how it works, and I noticed that the main function always begins with:\n\n```\nlea ecx,[esp+0x4] ; I assume this is for getting the adress of the first argument of the main...why ?\nand esp,0xfffffff0 ; ??? is the compiler trying to align the stack pointer on 16 bytes ???\npush DWORD PTR [ecx-0x4] ; I understand the assembler is pushing the return adress....why ?\npush ebp \nmov ebp,esp\npush ecx ;why is ecx pushed too ??\n```\n\nso my question is: why all this work is done ??\nI only understand the use of:\n\n```\npush ebp \nmov ebp,esp\n```\n\nthe rest seems useless to me...\n\n========================================\n\nTop Answer:\nThis is done to keep the stack aligned to a 16-byte boundary. Some instructions require certain data types to be aligned on as much as a 16-byte boundary. In order to meet this requirement, GCC makes sure that the stack is initially 16-byte aligned, and allocates stack space in multiples of 16 bytes. This can be controlled using the option -mpreferred-stack-boundary=*num*. If you use -mpreferred-stack-boundary=2 (for a 22=4-byte alignment), this alignment code will not be generated because the stack is always at least 4-byte aligned. However you could then have trouble if your program uses any data types that require stronger alignment.\n\nAccording to the gcc manual:\n\n On Pentium and PentiumPro, double and long double values should be aligned to an 8 byte boundary (see -malign-double) or suffer significant run time performance penalties. On Pentium III, the Streaming SIMD Extension (SSE) data type __m128 may not work properly if it is not 16 byte aligned.\n\n \n To ensure proper alignment of this values on the stack, the stack boundary must be as aligned as that required by any value stored on the stack. Further, every function must be generated such that it keeps the stack aligned. Thus calling a function compiled with a higher preferred stack boundary from a function compiled with a lower preferred stack boundary will most likely misalign the stack. It is recommended that libraries that use callbacks always use the default setting.\n\n \n This extra alignment does consume extra stack space, and generally increases code size. Code that is sensitive to stack space usage, such as embedded systems and operating system kernels, may want to reduce the preferred alignment to -mpreferred-stack-boundary=2.\n\nThe `lea` loads the original stack pointer (from before the call to `main`) into `ecx`, since the stack pointer is about to modified. This is used for two purposes:\n\n- to access the arguments to the `main` function, since they are relative to the original stack pointer\n\n- to restore the stack pointer to its original value when returning from `main`\n\n========================================\n\nCode:\n```text\nlea    ecx,[esp+0x4] ; I assume this is for getting the adress of the first argument of the main...why ?\nand    esp,0xfffffff0 ; ??? is the compiler trying to align the stack pointer on 16 bytes ???\npush   DWORD PTR [ecx-0x4] ; I understand the assembler is pushing the return adress....why ?\npush   ebp                \nmov    ebp,esp\npush   ecx  ;why is ecx pushed too ??\n```\n\n```text\npush   ebp                \nmov    ebp,esp\n```\n\n```text\n;# As you have already noticed, the compiler wants to align the stack\n;# pointer on a 16 byte boundary before it pushes anything. That's\n;# because certain instructions' memory access needs to be aligned\n;# that way.\n;# So in order to first save the original offset of esp (+4), it\n;# executes the first instruction:\nlea    ecx,[esp+0x4]\n\n;# Now alignment can happen. Without the previous insn the next one\n;# would have made the original esp unrecoverable:\nand    esp,0xfffffff0\n\n;# Next it pushes the return addresss and creates a stack frame. I\n;# assume it now wants to make the stack look like a normal\n;# subroutine call:\npush   DWORD PTR [ecx-0x4]\npush   ebp\nmov    ebp,esp\n\n;# Remember that ecx is still the only value that can restore the\n;# original esp. Since ecx may be garbled by any subroutine calls,\n;# it has to save it somewhere:\npush   ecx\n```\n\n```text\nlea\n```\n\n```text\nmain\n```\n\n```text\necx\n```\n\n```text\nmain\n```\n\n```text\nmain\n```\n\n```text\nlea    ecx,[esp+0x4] ; I assume this is for getting the adress of the first argument of     the main...why ?\nand    esp,0xfffffff0 ; ??? is the compiler trying to align the stack pointer on 16 bytes ???\npush   DWORD PTR [ecx-0x4] ; I understand the assembler is pushing the return adress....why ?\npush   ebp                \nmov    ebp,esp\npush   ecx  ;why is ecx pushed too ??\n```\n\n========================================\n\nComments:\n- What compiler are you using, and can you provide the complete disassembled main subroutine?\n- see also Why is gcc generating an extra return address? not sure if one should be as a duplicate of the other.","metadata":{"transformedAt":"2026-08-18T18:32:17.775Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":124,"estimatedTokens":1267}}223{"id":"stack-7361144","source":"stackoverflow","questionId":7361144,"title":"Where can I find learning resources for implementing a C preprocessor?","tags":["c","parsing","compiler-construction","c-preprocessor"],"text":"Title: Where can I find learning resources for implementing a C preprocessor?\nTags: c, parsing, compiler-construction, c-preprocessor\nSource: Stack Overflow\n\nQuestion:\nMuch has been written over the years on implementing parsers, but the C preprocessor is not quite the same as any of the stages of a typical parser, and implementation thereof doubtless has its of particular pitfalls to watch out for. Does anyone know of anything written on the topic of implementing a C preprocessor?\n\n========================================\n\nTop Answer:\nHartmut Kaiser, the author of Boost Wave, wrote a nice article on CodeProject http://www.codeproject.com/KB/recipes/wave_preprocessor.aspx about the Boost Wave project. You can use Boost Wave to make your own C preprocessor with custom extensions.\n\n========================================\n\nComments:\n- here's another by Paul\n- btw I find it quite interesting that Pauls's words \"Very very few preprocessors get this completely right\" are still hold good almost 20 years later and most major players (such as gcc, clang and msvc) preprocessors are in disagreement with mensonides handling of complex scenarios with interleaved invocations and I only found couple (edg and nvc) besides mine own implementation producing expected output - godbolt.org/z/zsGGdar4a","metadata":{"transformedAt":"2026-08-18T18:32:17.775Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":17,"estimatedTokens":326}}224{"id":"stack-25848604","source":"stackoverflow","questionId":25848604,"title":"Why does F# generic struct have extra __dummy field?","tags":["generics","compiler-construction","struct","f#","sizeof"],"text":"Title: Why does F# generic struct have extra __dummy field?\nTags: generics, compiler-construction, struct, f#, sizeof\nSource: Stack Overflow\n\nQuestion:\nUsing F# Interactive, you can verify the following sizes:\n\n```\n// sizeof = 4 bytes\ntype A (i: int) = struct end\n\n// sizeof> = 8 bytes (use any type parameter)\ntype B (i: int) = struct end\n```\n\nThe reason for the extra size seems to be the presence of an integer `__dummy` field in the generic case. Using F# Interactive again, you can see this using `typeof`:\n\n- `typeof` shows `DeclaredFields = [|Int32 i|]`\n\n- `typeof>` shows `DeclaredFields = [|Int32 i; Int32 __dummy|]`\n\nI don't understand why this `__dummy` field has been added.\n\nI think the code responsible for adding it is here:\n\nhttps://github.com/fsharp/FSharp.Compiler.Service/blob/master/src/fsharp/ilxgen.fs\n\nLine 6377 shows this:\n\n```\nif requiresExtraField then \n yield mkILInstanceField(\"__dummy\",cenv.g.ilg.typ_int32,None,ILMemberAccess.Assembly) ]\n```\n\nLine 6290 is where `requiresExtraField` is defined: \n\n```\nlet requiresExtraField = \n let isEmptyStruct = \n (match ilTypeDefKind with ILTypeDefKind.ValueType -> true | _ -> false) &&\n // All structs are sequential by default \n // Structs with no instance fields get size 1, pack 0\n tycon.AllFieldsAsList |> List.exists (fun f -> not f.IsStatic)\n\n isEmptyStruct && cenv.opts.workAroundReflectionEmitBugs && not tycon.TyparsNoRange.IsEmpty\n```\n\nI assume that `isEmptyStruct` is supposed to mean that the struct does not have any instance fields. But the code as written is testing whether the struct *does* have any instance fields, which for most structs, including mine, is going to be true. I think the last part of the final test is whether there are any generic type parameters. So `requiresExtraField` is `false` for `type A` (not generic) and `true` for `type B` (generic type).\n\nIs this a compiler bug, or is the code correct? If it is correct, then what's the purpose of this `__dummy` field? Is there some way I can avoid having it?\n\nAs another test, I removed my one and only instance field, and not surprisingly, I got the following sizes, showing that the `__dummy` field was no longer added:\n\n```\n// sizeof = 1\ntype AA = struct end\n\n// sizeof> = 1\ntype BB = struct end\n```\n\nThe reason I want to have a value type, rather than a reference type, is that I will be storing lots of these objects in my data structures, not just passing them around.\n\n========================================\n\nCode:\n```text\n// sizeof<A> = 4 bytes\ntype A (i: int) = struct end\n\n// sizeof<B<int>> = 8 bytes (use any type parameter)\ntype B<'T> (i: int) = struct end\n```\n\n```text\nif requiresExtraField then \n    yield mkILInstanceField(\"__dummy\",cenv.g.ilg.typ_int32,None,ILMemberAccess.Assembly) ]\n```\n\n```text\nlet requiresExtraField = \n    let isEmptyStruct = \n        (match ilTypeDefKind with ILTypeDefKind.ValueType -> true | _ -> false) &&\n        // All structs are sequential by default \n        // Structs with no instance fields get size 1, pack 0\n        tycon.AllFieldsAsList |> List.exists (fun f -> not f.IsStatic)\n\n    isEmptyStruct && cenv.opts.workAroundReflectionEmitBugs && not tycon.TyparsNoRange.IsEmpty\n```\n\n```text\n// sizeof<AA> = 1\ntype AA = struct end\n\n// sizeof<BB<int>> = 1\ntype BB<'T> = struct end\n```\n\n```text\n__dummy\n```\n\n```text\ntypeof\n```\n\n```text\ntypeof<A>\n```\n\n```text\nDeclaredFields = [|Int32 i|]\n```\n\n```text\ntypeof<B<int>>\n```\n\n```text\nDeclaredFields = [|Int32 i; Int32 __dummy|]\n```\n\n```text\n__dummy\n```\n\n```text\nrequiresExtraField\n```\n\n```text\nisEmptyStruct\n```\n\n```text\nrequiresExtraField\n```\n\n```text\nfalse\n```\n\n```text\ntype A\n```\n\n```text\ntrue\n```\n\n```text\ntype B\n```\n\n```text\n__dummy\n```\n\n```text\n__dummy\n```\n\n```text\nworkAroundReflectionEmitBugs=tcConfig.isInteractive; // REVIEW: is this still required?\n```\n\n```text\nrequiresExtraField\n```\n\n```text\ncenv.opts.workAroundReflectionEmitBugs\n```\n\n```text\n__dummy\n```\n\n========================================\n\nComments:\n- \"cenv.opts.workAroundReflectionEmitBugs\" suggests that this is a workaround to some reflection emit bug. Have you checked if C# does the same thing? - A quick look at the code history shows that this dates back to at least November 2010, F# 2.0 - the earliest version of the code available online AFAIK.\n- Since sizeof> = 4 in a program, is its size in the interactive really an issue for you?\n- @jyoung: Oh, I never realized that this was only the case with F# Interactive. I just tried sizeof> in a program, like you said, and it gives 4, whereas F# Interactive gives 8. Then my issue is solved, thank you! I found that source file using Google, so I didn't know the bigger picture of what it is for. In the corresponding fsi file, the public `IlxAssemblyGenerator` type at the end says that it is *An incremental ILX code generator for a single assembly*. Just for my edification, what is that all about?\n- Type the lines let f()=1;; let g()=f();; let f()=2;; g();; You'll see that g() is still 1. So a 'new' f() was (incrementally) added to the assembly, but the 'old' f() is still there being referenced by the previous increments of the code.\n- @jyoung: Thanks. So the \"ILX code generator\" is only used in situations like F# Interactive, and is not part of the regular F# compiler, I assume.\n- I think it is the same generator. Your problem arises because of the fscopts.fs's line: 'workAroundReflectionEmitBugs=tcConfig.isInteractive; // REVIEW: is this still required?'\n- @jyoung: Ah ha, the mystery is now solved. Thanks once again. By the way, how do I mark my own posting as 'answered'? Do I use the *Answer Your Question* button?\n- bananasareyellow, I would invite @JYoung to post the answer as an answer, so credit can be given where credit is due. If JYoung is unconcerned about reputation and declines the invitation, you could post the answer yourself and accept that.\n- Go ahead bananasareyellow, post the answer. You understand it better than me now.\n- This doesn't answer the question of whether the logic for `isEmptyStruct` is backwards, as it appears to be.\n- @ildjarn: You are right, it doesn't. I marked it as answered because my principle issue was the size of the struct due to the presence of the dummy field. That test is still questionable.","metadata":{"transformedAt":"2026-08-18T18:32:17.775Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":24,"totalLines":192,"estimatedTokens":1560}}225{"id":"stack-3773064","source":"stackoverflow","questionId":3773064,"title":"What are some tips for optimizing the assembly code generated by a compiler?","tags":["assembly","x86","compiler-construction"],"text":"Title: What are some tips for optimizing the assembly code generated by a compiler?\nTags: assembly, x86, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am currently in the process of writing a compiler and I seem to have run into some problems getting it to output code that executes in a decent timeframe.\n\nA brief overview of the compiler:\n\n 7Basic is a compiler that aims to compile 7Basic code directly into machine code for the target architecture / platform. Currently 7Basic generates x86 assembly given a source file.\n\nThe problem is that the assembly code generated by the compiler is **slow and inefficient**.\n\nFor example, this code (which compiles down to this assembly code) takes nearly 80.47 times longer to execute than the equivalent C code.\n\nPart of the problem is that the compiler is generating code like the following:\n\n```\npush eax\npush 5000000\npop ebx\npop eax\n```\n\nInstead of the more logical:\n\n```\nmov ebx,5000000\n```\n\n...which accomplishes the same thing.\n\nMy question is: what are some techniques to avoid this sort of problem? The parser basically uses recursion to parse the expressions, so the code generated reflects this.\n\n========================================\n\nTop Answer:\nYou might want to consider generating C code rather than assembly and then let a C compiler (e.g. gcc) handle the code generation for you. There's no point trying to re-invent the wheel.\n\n========================================\n\nCode:\n```text\npush eax\npush 5000000\npop ebx\npop eax\n```\n\n```text\nmov ebx,5000000\n```\n\n```text\npush eax        ; 1\npush 5000000    ; 2\npop ebx         ; 3\npop eax         ; 4\n```\n\n```text\npush eax        ; 1\nmov ebx,5000000 ; 2a\npop eax         ; 4\n```\n\n```text\nmov ebx,5000000 ; 2a\n```\n\n```text\neax\n```\n\n========================================\n\nComments:\n- Add -O3 to your compiler command line ;-)\n- \"...a **cross-platform** BASIC compiler that generates native **Win32** executables.\" Wait, what?\n- @musicfreak: Well, it also aims to generate ELF-executables too.\n- The Crenshaw tutorial says a (very!) little about peephole optimization in a one pass compiler. Short-short version: buffer the output by N instructions and check for possible optimizations in the buffer each time a new instruction is emitted to the buffer. Push results out as needed. What optimizations are to be done? Ah...that's the hard part, isn't it?\n- @dmckee: That seems like helpful advice.\n- Okay, could this be done as the code is being generated? That would be better.\n- Usually peephole optimisation is run as a separate pass after you've generated an intermediate assembly output. If you're compiling for multiple architectures, then it would necessarily have to be run *after* you've compiled to an IL form, and then to your target assembly language.\n- Well, I kind of have a class hierarchy for the assembly code output module, so each architecture's output module derives from a base class. Aren't there some optimizations common to all of my supported architectures (currently planning to include x86 and x86-64)?\n- Crenshaw showed one method for doing it in-line. Probably not as globally efficient as another pass, but tradeoffs *are* the name of the game.\n- Eventually the compiler is going to generate machine code, so this is not an option.\n- Eventually the C compiler is going to generate machine code, too.\n- What I meant was that eventually the compiler will directly generate the machine code itself.\n- If your compiler is going to have a suitable open source license then you could possibly integrate some of the gcc back end, if you don't want to use it as part of a tool chain.\n- Cool. Then there is hope that this codegen can be improved. ;> Step 1: figure out how to recognize constant value loads in your intermediate representation and emit them as mov reg,imm. Step2: figure out why your code generator is pushing and popping eax in this example, since it is not relevant to the core operation at all. Smells of bug.\n- It's not a bug. It's supposed to do that simply because of the way expressions are evaluated. This is why I asked the question.\n- Well then you need to work on the way your expressions are evaluated if its adding cruft to your codegen. ;> Peephole optimizations (as mentioned in another answer) can help clean up the mess left by a poor codegen, but in my opinion it's better to emit better code to begin with.\n- The compiler is not supposed to have any understanding of the architecture for the generated machine code. Otherwise it will be impossible to add another architecture.\n- Great advice - the language doesn't have the concept of scope yet, nor does it have functions / subroutines. Still a work in progress. But when it does, I'll be sure to have local variables go on the stack.\n- What is your intermediate code representation? TAC/Quadruples?\n- Don't have one :) The compiler sends 'pseudo-commands' to the output module which generates the exact assembly instructions.\n- You should reconsider your strategy--you should generate some sort of intermediate code so you can make some necessary optimizations before generating target code.","metadata":{"transformedAt":"2026-08-18T18:32:17.775Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":98,"estimatedTokens":1280}}226{"id":"stack-13902239","source":"stackoverflow","questionId":13902239,"title":"How to implement JJTree on grammar","tags":["java","parsing","compiler-construction","abstract-syntax-tree","javacc"],"text":"Title: How to implement JJTree on grammar\nTags: java, parsing, compiler-construction, abstract-syntax-tree, javacc\nSource: Stack Overflow\n\nQuestion:\nI have an assignment to use JavaCC to make a Top-Down Parser with Semantic Analysis for a language supplied by the lecturer. I have the production rules written out and no errors.\nI'm completely stuck on how to use JJTree for my code and my hours of scouring the internet for tutorials hasn't gotten me anywhere.\nJust wondering could anyone take some time out to explain how to implement JJTree in the code?\nOr if there's a hidden step-by-step tutorial out there somewhere that would be a great help!\n\nHere are some of my production rules in case they help.\nThanks in advance!\n\n```\nvoid program() : {}\n{\n (decl())* (function())* main_prog()\n}\n\nvoid decl() #void : {}\n{\n (\n var_decl() | const_decl()\n )\n}\n\nvoid var_decl() #void : {}\n{\n ident_list() type()\n ( ident_list() type())* \n}\n\nvoid const_decl() #void : {}\n{\n identifier() type() expression()\n ( identifier() type() expression())* \n} \n\nvoid function() #void : {}\n{\n type() identifier() param_list() \n \n (decl())*\n (statement() )*\n returnRule() (expression() | {} )\n \n}\n```\n\n========================================\n\nTop Answer:\nHere is an example that uses JJTree\nhttp://anandsekar.github.io/writing-an-interpretter-using-javacc/\n\n========================================\n\nCode:\n```text\nvoid program() : {}\n{\n  (decl())* (function())* main_prog()\n}\n\nvoid decl() #void : {}\n{\n  (\n    var_decl() | const_decl()\n   )\n}\n\nvoid var_decl() #void : {}\n{\n  <VAR> ident_list() <COLON> type()\n (<COMMA> ident_list() <COLON> type())* <SEMIC>\n}\n\nvoid const_decl()  #void : {}\n{\n  <CONSTANT> identifier() <COLON> type() <EQUAL> expression()\n ( <COMMA> identifier() <COLON> type() <EQUAL > expression())* <SEMIC>\n} \n\nvoid function() #void : {}\n{\n  type() identifier() <LBR> param_list() <RBR>\n  <CBL>\n  (decl())*\n  (statement() <SEMIC> )*\n  returnRule() (expression() | {} )<SEMIC>\n  <CBR>\n}\n```\n\n```text\nmv TestParser.jj TestParser.jjt\n```\n\n```text\nvoid decl() :\n{}\n{\n     var_decl()\n  |  const_decl()\n}\n```\n\n```text\nvoid var_decl() #VAR :\n{}\n{\n  <VAR> id() <COL> id() <EQ> expr() <SCOL>\n}\n\nvoid id() #ID :\n{}\n{\n  <ID>\n}\n\nvoid expr() #EXPR :\n{}\n{\n  <ID>\n}\n```\n\n```text\nVAR \n   / | \\\n  /  |  \\\nID  ID  EXPR\n```\n\n```text\nvoid id() #ID :\n{Token t;}\n{\n  t=<ID> {jjtThis.value = t.image;}\n}\n\nvoid expr() #EXPR :\n{Token t;}\n{\n  t=<ID> {jjtThis.value = t.image;}\n}\n```\n\n```text\nVAR \n        |\n    .---+------.\n   /    |       \\\n  /     |        \\\nID[\"x\"] ID[\"int\"] EXPR[\"i\"]\n```\n\n```text\n// TestParser.jjt\nPARSER_BEGIN(TestParser)\n\npublic class TestParser {\n  public static void main(String[] args) throws ParseException {\n    TestParser parser = new TestParser(new java.io.StringReader(args[0]));\n    SimpleNode root = parser.program();\n    root.dump(\"\");\n  }\n}\n\nPARSER_END(TestParser)\n\nTOKEN :\n{\n   < OPAR  : \"(\" > \n | < CPAR  : \")\" >\n | < OBR   : \"{\" >\n | < CBR   : \"}\" >\n | < COL   : \":\" >\n | < SCOL  : \";\" >\n | < COMMA : \",\" >\n | < VAR   : \"var\" >\n | < EQ    : \"=\" > \n | < CONST : \"const\" >\n | < ID    : (\"_\" | <LETTER>) (\"_\" | <ALPHANUM>)* >\n}\n\nTOKEN :\n{\n   < #DIGIT    : [\"0\"-\"9\"] >\n | < #LETTER   : [\"a\"-\"z\",\"A\"-\"Z\"] >\n | < #ALPHANUM : <LETTER> | <DIGIT> >\n}\n\nSKIP : { \" \" | \"\\t\" | \"\\r\" | \"\\n\" }\n\nSimpleNode program() #PROGRAM :\n{}\n{\n  (decl())* (function())* <EOF> {return jjtThis;}\n}\n\nvoid decl() :\n{}\n{\n     var_decl()\n  |  const_decl()\n}\n\nvoid var_decl() #VAR :\n{}\n{\n  <VAR> id() <COL> id() <EQ> expr() <SCOL>\n}\n\nvoid const_decl() #CONST :\n{}\n{\n  <CONST> id() <COL> id() <EQ> expr() <SCOL>\n}\n\n\nvoid function() #FUNCTION :\n{}\n{\n  type() id() <OPAR> params() <CPAR> <OBR> /* ... */ <CBR>\n}\n\nvoid type() #TYPE :\n{Token t;}\n{\n  t=<ID> {jjtThis.value = t.image;}\n}\n\nvoid id() #ID :\n{Token t;}\n{\n  t=<ID> {jjtThis.value = t.image;}\n}\n\nvoid params() #PARAMS :\n{}\n{\n  (param() (<COMMA> param())*)?\n}\n\nvoid param() #PARAM :\n{Token t;}\n{\n  t=<ID> {jjtThis.value = t.image;}\n}\n\nvoid expr() #EXPR :\n{Token t;}\n{\n  t=<ID> {jjtThis.value = t.image;}\n}\n```\n\n```text\njava -cp javacc.jar jjtree TestParser.jjt\n```\n\n```text\njava -cp javacc.jar javacc TestParser.jj\n```\n\n```text\njavac -cp .:javacc.jar *.java\n```\n\n```text\nvar n : int = I; \n\nconst x : bool = B; \n\ndouble f(a,b,c) \n{ \n}\n```\n\n```text\njava -cp . TestParser \"var n : int = I; const x : bool = B; double f(a,b,c) { }\"\n```\n\n```text\nPROGRAM\n decl\n  VAR\n   ID\n   ID\n   EXPR\n decl\n  CONST\n   ID\n   ID\n   EXPR\n FUNCTION\n  TYPE\n  ID\n  PARAMS\n   PARAM\n   PARAM\n   PARAM\n```\n\n```text\njj\n```\n\n```text\njj\n```\n\n```text\njjt\n```\n\n```text\njjtree\n```\n\n```text\njjt\n```\n\n```text\njj\n```\n\n```text\njavacc\n```\n\n```text\njj\n```\n\n```text\njava\n```\n\n```text\njavacc.jar\n```\n\n```text\njava\n```\n\n```text\njavac\n```\n\n```text\njj\n```\n\n```text\nTestParser.jj\n```\n\n```text\n#\n```\n\n```text\n:\n```\n\n```text\n#void\n```\n\n```text\n#void\n```\n\n```text\nvar_decl()\n```\n\n```text\nconst_decl()\n```\n\n```text\nvar_decl\n```\n\n```text\n#VAR\n```\n\n```text\nid\n```\n\n```text\nexpr\n```\n\n```text\n<ID>\n```\n\n```text\n.value\n```\n\n```text\n.image\n```\n\n```text\n\"var x : int = i;\"\n```\n\n```text\nmain\n```\n\n```text\njjtree\n```\n\n```text\njavacc.jar\n```\n\n```text\njj\n```\n\n```text\nTestParser.jj\n```\n\n```text\njavacc\n```\n\n```text\njavacc.jar\n```\n\n```text\njavac -cp .;javacc.jar *.java\n```\n\n```text\nID\n```\n\n```text\ndump()\n```\n\n```text\nsrc/grammar\n```\n\n========================================\n\nComments:\n- What's a `JJTree`? ..And what language is that written in (does not look like any Java I recognize)?\n- @AndrewThompson it's JavaCC, as per the tag.\n- What does your teacher say when you ask, \"Where do I look for a reasonable overview of JavaCC and JJTree?\" Sure s/he can't be expecting you to know how to do this with no background or reference material.\n- @IraBaxter, Our whole class has been emailing our lecturer asking for some help and the answers we get aren't very helpful. He has some notes up on it but they don't explain how/why the code is implemented the way it is. Trust me we have tried getting in touch but it seems like a waste of time.\n- And you've gone over javacc.java.net/doc/JJTree.html in great detail?\n- yes, I found it very hard to since I didn't know where to start\n- I think you need to talk to your teacher in person. I am a compiler writer and I agree that the JJTree documentation is a very hard slog indeed. I doubt that I would even be teaching it frankly.\n- This was a better answer than I could have hoped for! I ended up getting the AST to print out but I wasn't too sure what was happening. This is a great explanation! Thanks so much","metadata":{"transformedAt":"2026-08-18T18:32:17.776Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":53,"totalLines":472,"estimatedTokens":1631}}227{"id":"stack-60547","source":"stackoverflow","questionId":60547,"title":"C (or any) compilers deterministic performance","tags":["c","compiler-construction","deterministic","embedded"],"text":"Title: C (or any) compilers deterministic performance\nTags: c, compiler-construction, deterministic, embedded\nSource: Stack Overflow\n\nQuestion:\nWhilst working on a recent project, I was visited by a customer QA representitive, who asked me a question that I hadn't really considered before:\n\n How do you know that the compiler you are using generates machine code that matches the c code's functionality exactly and that the compiler is fully deterministic?\n\nTo this question I had absolutely no reply as I have always taken the compiler for granted. It takes in code and spews out machine code. How can I go about and test that the compiler isn't actually adding functionality that I haven't asked it for? or even more dangerously implementing code in a slightly different manner to that which I expect?\n\nI am aware that this is perhapse not really an issue for everyone, and indeed the answer might just be... \"you're over a barrel and deal with it\". However, when working in an embedded environment, you trust your compiler implicitly. How can I prove to myself and QA that I am right in doing so?\n\n========================================\n\nTop Answer:\nYou can apply that argument at any level: do you trust the third party libraries? do you trust the OS? do you trust the processor?\n\nA good example of why this may be a valid concern of course, is how Ken Thompson put a backdoor into the original 'login' program ... and modified the C compiler so that even if you recompiled login you still got the backdoor. See this posting for more details.\n\nSimilar questions have been raised about encryption algorithms -- how do we know there isn't a backdoor in DES for the NSA to snoop through?\n\nAt the end of the you have to decide if you trust the infrastructure you are building on enough to not worry about it, otherwise you have to start developing your own silicon chips!\n\n========================================\n\nComments:\n- Exactly. I'm assuming that if the guy asked is because the thing is sensitive and they have reasons to, not because they are paranoid wankers...\n- No, Ken didn't, and quoting some blogger doesn't make it so. Read Ken's real paper (Reflections on Trusting Trust) to see what he really did: he suggested that he could do it. Neither the alleged compiler nor the alleged login binary has ever been seen.\n- Answer does not actually address the question.\n- The Jargon file claims that Ken actually did do it. catb.org/jargon/html/B/back-door.html\n- No, it's quite possible to write a provably correct compiler, and to ground the proving ultimately in the correctness of a small hand-checked proof-checker. What's uncomputable is to take an arbitrary program and tell for sure whether it's the correct compiler you want.\n- Right on. Use unit tests, static testing and integration testing to verify systems behavior. The customer's QQ guy wanted to know if you know how to test software to assure quality. Validate your unit and integration test data set using either a test oracle or some validated independent means. (E.g. Hand generated unit test results, validated by several engineers.)\n- Make sure you use sufficient tests. If the application domain has little requirement for correctness, testing is easy. To do this rigorously, the formal requirements have to be represented by the software, and tested by the unit and integration tests. If the formal correctness requirements are not defined, then any answer is right. The customer will pick whichever one you didn't choose.\n- +1 For `\"You'll stop doing that the first time you come across a compiler bug\"`.\n- But how do you know the verifier(Coq) is correct? How do you know that the C specification was transcribed into Coq's language correctly?\n- @Mike: There are a few programs whose behavior would be defined by the C Standard but not by CompCert C, most notably those that decompose pointers into an integer or sequence of bytes, manipulate those values, and then form new pointers from the results. ComperCert C is designed so that the behavior of the output code from any optimization stage must match that of the input code to that stage for all possible input values where the behavior would be defined. To make this practical, CompCert defines behaviors in some cases where the C Standard would not require it to do so.\n- If you are worried about malicious bugs in the compiler, you might be happy to know that another defense has been developed: “Diverse Double-Compiling” (DDC). dwheeler.com/trusting-trust\n- @David Cary: A related work is the compiler bug finding techniques used by this guy: blog.regehr.org","metadata":{"transformedAt":"2026-08-18T18:32:17.776Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":39,"estimatedTokens":1151}}228{"id":"stack-6612449","source":"stackoverflow","questionId":6612449,"title":"What are the arguments to the types.CodeType() python call?","tags":["python","google-app-engine","compiler-construction","introspection","dynamic-cast"],"text":"Title: What are the arguments to the types.CodeType() python call?\nTags: python, google-app-engine, compiler-construction, introspection, dynamic-cast\nSource: Stack Overflow\n\nQuestion:\nI'm currently trying to roll my own \"marshal\" code for python so i can store compiled python code on Google App Engine to serve scripts on a dynamic way. As you all can verify, \"marshal\" isn't supported on GAE and \"pickle\" can't serialize code objects.\n\nI found out i can construct a code object with `types.CodeType()` but it expects 12 arguments.\n\nAs much as i've tried, i can't find any documentation on this call and i really need to construct the code object so i can `exec()` it. My question is, **does anyone know what are the parameters for this `types.CodeType()` \"constructor\" or any way to introspect it?** i have used the `info()` function defined here but it spits out just generic info!\n\nQuick FAQ:\n\n- Q: Why compile the code?\n\n- A: CPU time costs real money on Google App Engine, and every bit of CPU cycles i can save counts.\n\n- Q: Why not use \"marshal\"?\n\n- A: That's one of the unsupported modules in Google App Engine.\n\n- Q: Why not use \"pickle\"?\n\n- A: Pickle doesn't support serialization of code objects.\n\n**UPDATE**\n\nGoogle App Engine infrastructure doesn't allow the instantiation of code objects as of 7th July 2011, so my argument here is moot. Hope this gets fixed in the future on GAE.\n\n========================================\n\nTop Answer:\nThe question asked:\n\n what are the parameters for this types.CodeType() \"constructor\"\n\nFrom the python docs about the inspect module:\n\n```\nco_argcount: number of arguments (not including * or ** args)\nco_code: string of raw compiled bytecode\nco_consts: tuple of constants used in the bytecode\nco_filename: name of file in which this code object was created\nco_firstlineno: number of first line in Python source code\nco_flags: bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg\nco_lnotab: encoded mapping of line numbers to bytecode indices\nco_name: name with which this code object was defined\nco_names: tuple of names of local variables\nco_nlocals: number of local variables\nco_stacksize: virtual machine stack space required\nco_varnames: tuple of names of arguments and local variables\n```\n\nThis blog post has much more detailed explanation: http://tech.blog.aknin.name/2010/07/03/pythons-innards-code-objects/\n\nNote: the blog post talks about python 3 while the quoted python docs above is python 2.7.\n\n========================================\n\nCode:\n```text\ntypes.CodeType()\n```\n\n```text\nexec()\n```\n\n```text\ntypes.CodeType()\n```\n\n```text\ninfo()\n```\n\n```text\nimport types, copy_reg\ndef code_ctor(*args):\n    # delegate to new.code the construction of a new code object\n    return types.CodeType(*args)\ndef reduce_code(co):\n    # a reductor function must return a tuple with two items: first, the\n    # constructor function to be called to rebuild the argument object\n    # at a future de-serialization time; then, the tuple of arguments\n    # that will need to be passed to the constructor function.\n    if co.co_freevars or co.co_cellvars:\n        raise ValueError, \"Sorry, cannot pickle code objects from closures\"\n    return code_ctor, (co.co_argcount, co.co_nlocals, co.co_stacksize,\n        co.co_flags, co.co_code, co.co_consts, co.co_names,\n        co.co_varnames, co.co_filename, co.co_name, co.co_firstlineno,\n        co.co_lnotab)\n# register the reductor to be used for pickling objects of type 'CodeType'\ncopy_reg.pickle(types.CodeType, reduce_code)\nif __name__ == '__main__':\n    # example usage of our new ability to pickle code objects\n    import cPickle\n    # a function (which, inside, has a code object, of course)\n    def f(x): print 'Hello,', x\n    # serialize the function's code object to a string of bytes\n    pickled_code = cPickle.dumps(f.func_code)\n    # recover an equal code object from the string of bytes\n    recovered_code = cPickle.loads(pickled_code)\n    # build a new function around the rebuilt code object\n    g = types.FunctionType(recovered_code, globals( ))\n    # check what happens when the new function gets called\n    g('world')\n```\n\n```text\nPyCodeObject *\nPyCode_New(int argcount, int nlocals, int stacksize, int flags,\n           PyObject *code, PyObject *consts, PyObject *names,\n           PyObject *varnames, PyObject *freevars, PyObject *cellvars,\n           PyObject *filename, PyObject *name, int firstlineno,\n           PyObject *lnotab)\n```\n\n```text\nif (!PyArg_ParseTuple(args, \"iiiiSO!O!O!SSiS|O!O!:code\",\n                      &argcount, &nlocals, &stacksize, &flags,\n                      &code,\n                      &PyTuple_Type, &consts,\n                      &PyTuple_Type, &names,\n                      &PyTuple_Type, &varnames,\n                      &filename, &name,\n                      &firstlineno, &lnotab,\n                      &PyTuple_Type, &freevars,\n                      &PyTuple_Type, &cellvars))\n    return NULL;\n```\n\n```text\ndef __init__(self, argcount, nlocals, stacksize, flags, code,\n                   consts, names, varnames, filename, name, \n                   firstlineno, lnotab, freevars=None, cellvars=None): # ...\n```\n\n```text\nco_argcount: number of arguments (not including * or ** args)\nco_code: string of raw compiled bytecode\nco_consts: tuple of constants used in the bytecode\nco_filename: name of file in which this code object was created\nco_firstlineno: number of first line in Python source code\nco_flags: bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg\nco_lnotab: encoded mapping of line numbers to bytecode indices\nco_name: name with which this code object was defined\nco_names: tuple of names of local variables\nco_nlocals: number of local variables\nco_stacksize: virtual machine stack space required\nco_varnames: tuple of names of arguments and local variables\n```\n\n========================================\n\nComments:\n- can you explain what exactly you are trying to do? what you will do with pickled code?\n- I wish every question was a little more like this.\n- @Anurag Uniyal: I'm running \"managed\" code on Google App Engine infrastructure, that code comes from a frontend where coders can program directly on the browser, and the code gets compiled on the fly to check for syntax/type errors (much like Cloud9 but for GAE). I don't want to recompile the code on execution because the spikes of recompilation can shatter my CPU quota and my wallet. The idea is to get the bytecode (which i already have) and run it with a sandboxed environment.\n- @something: That code helps a lot! thank you! however, the \"new\" module is deprecated on 2.6 and the python runtime for GAE is about to change to 2.7 so i have to know the exact order of arguments for `type.CodeType`\n- @Chiguireitor: I removed the need for the \"new\" module and posted it as an answer. Hope it helps.\n- The built-in `help()` function will give you information on the names of the parameters to the code object type constructor. These map pretty obviously to the `co_` attributes of a code object.\n- That helped a lot @something, thank you... Had to fiddle with the type.CodeType to make it work. The argument list is exactly like the one here\n- It took me a bit to realize how this fits together with my answer: there are two optional arguments, freevars and cellvars. I assume they were added later.\n- `freevars` and `cellvars` are used in closures. They're optional because not all functions use them.\n- After all this trouble i deployed the solution to my GAE server and it greeted me with a \"RuntimeError: cannot create code objects in restricted execution mode\" :-(\n- Hm. Perhaps you can compile the objects once and then keep them in a dict?\n- @aukaost : cPickle is know disabled. so this no longer works.\n- Note that if you are going to create your own codetype you may need to specify `co_kwonlyargcount`, only in the documentation of data model (starting in the 3.8.6) is specified but i am using 3.7.9 and i need to specify it too so you may need to add it.\n- Thanks nick, that was what i was testing earlier this afternoon, i know this solution would consume a lot of memory if several scripts get cached on each instance, but as i have seen it, currently this is the best way to implement my use case. Any tips if dynamic code loading will be available on the 2.7 release? That would be great :-)\n- In the case of the op, I would say python 2.5.2.","metadata":{"transformedAt":"2026-08-18T18:32:17.776Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":171,"estimatedTokens":2096}}229{"id":"stack-6513806","source":"stackoverflow","questionId":6513806,"title":"Would it be possible to add type inference to the C language?","tags":["c","compiler-construction","type-inference","language-design"],"text":"Title: Would it be possible to add type inference to the C language?\nTags: c, compiler-construction, type-inference, language-design\nSource: Stack Overflow\n\nQuestion:\nLet's say, we create a reimplementation of C, with the only difference being\nthat types are inferred. Storage classes and modifiers would still need to be\ngiven (const, static, restrict etc), and let's restrict our attention to single\nfile C programs for the moment. Could it be done? What are the major\nimpediments?\n\nSome thoughts on what might cause problems with type inference\n\n- structs with the same field name would need to be disambiguated manually\n\n- same for unions with the same field names\ncasts would probably need a \"from\" annotation, something like\n\n```\nvar i = (uint32_t -> uint64_t) *some_pointer;\n```\n\nThese problems would require a bit of user annotation, but shouldn't be too\nburdensome, is there some killer issue that blows this idea out of the water?\n\nEdit: To clarify, I'm not talking about adding generics or parametric polymorphism, just type inference for existing C types.\n\nEdit 2014: Anyone interested in this concept may want to look into Rust\n\n========================================\n\nTop Answer:\nGCC 5.1 supports:\n\n- `__auto_type` extension, analogous C++11 `auto`\n`typeof` extension, analogous to C++11 `decltype`\n\n```\n/* Same as: double j = 0.5; */\ntypeof(1 + 0.5) j = 0.5;\nassert(j == 0.5);\n```\n\nLinux kernel example: How does the typecheck macro from the Linux kernel work?\n\n`_Generic` C11 keyword: Syntax and Sample Usage of _Generic in C11\n\n```\n__auto_type i = 1;\nassert(_Generic((i), int: 1, default: 0));\n```\n\n========================================\n\nCode:\n```text\nvar i = (uint32_t -> uint64_t) *some_pointer;\n```\n\n```text\n(int)\n```\n\n```text\nstatic foo;\n```\n\n```text\ncrc8 = 0x00; /* 8 bits; cf uint8_t crc8 = 0; */\ncrc32 = 0x00000000; /* 32 bits; cf uint32_t crc32 = 0; */\n```\n\n```text\nlength(p) {\n  if (p == NULL) return 0;\n  else return 1 + length(p->next);\n}\n```\n\n```text\nnext\n```\n\n```text\n+\n```\n\n```text\n/* Same as: double j = 0.5; */\ntypeof(1 + 0.5) j = 0.5;\nassert(j == 0.5);\n```\n\n```text\n__auto_type i = 1;\nassert(_Generic((i), int: 1, default: 0));\n```\n\n```text\n__auto_type\n```\n\n```text\nauto\n```\n\n```text\ntypeof\n```\n\n```text\ndecltype\n```\n\n```text\n_Generic\n```\n\n```text\nint length(T p) {\n  if (p == NULL) return 0;\n  else return 1 + length(p->next);\n}\n```\n\n```text\n#include <stdint.h>\n#define NULL ((void*)0)\ntypedef int bool;\nbool false = 0;\nbool true = 1;\ntypedef  struct T {struct T* next;}* T;\n```\n\n========================================\n\nComments:\n- I'm probably being slow; can you give an example for your first bullet point?\n- What you're describing sounds more like a dynamic type system. Type inference is a different idea all together.\n- I don't feel I have the expertise to submit a full answer, but my intuition says this is perfectly doable, at least for a C-like language that covers maybe 90%-95% of C. We already have viable type inference for Python (see the ShedSkin project), so I would imagine similar techniques could be used for a type-declaration-free (or nearly so) version of C.\n- Here we go again (JavaScript, PHP, ...): `42 + \"3\"` is 45 or 423?\n- @Oli Charlesworth: Suppose I have two struct types, `struct Point { int x; int y; }` and `struct Rect { float width; float height; float x; float y; }`. I then have a variable `var some_struct`. When I write `some_struct.x`, what does the compiler do?\n- @pmg: Type inference has nothing to do with bizarre automatic type conversions. In fact, it interacts rather badly with them.\n- @pmg: In C as it is now, it's undefined behavior for your specific case and pointer addition in the general case. This would not change; the question is not about adding implicit casts, but simply inferring unspecified types. In fact, I'm not sure how anyone is reading duck typing out of this; that's rather more far-reaching than this proposal.\n- @Oli: `struct a { int f; }; struct b { char *f; };`. But to the OP, this is not in fact a problem, because you always have to associate the field label with something that has a `struct` type. (`f` in isolation is in a different namespace entirely.)\n- @geekosaur that's a good point, since they have a built-in namespace\n- @pmg could you make a full answer fleshing out your concerns?\n- If by \"definite\" you mean \"not inferred\", why do pointers need such a type? `some_function() { return \"foo\"; } ... p = some_function()`. Now `p` has inferred type `const char*`, or `char*` if we prefer a foolish similarity to C. What's the problem? Of course `malloc` doesn't give us anything to infer a type from, so presumably either some casts would remain, or else some differences from C would be needed.\n- That's what I mean; not inferred. And for function parameters I mean, for example: in `function foo(variable) { &#47;*...*&#47; }` the type of the parameter cannot be inferred.\n- In most cases, the type of the parameter can be inferred from the way it's used. For example, in `foo(variable) { printf(variable, 123); }` we can see that variable must be a type which is valid as the first argument of `printf`. If we don't do anything that narrows down the required type of `variable` in `foo`, then the type inference engine can assume that it doesn't matter what the type is. Admittedly, there are places where the programmer will need to \"help\" the compiler, but it's certainly not every place.\n- Well C doesn't allow generics, so in this case (theoretically) you could look at what struct types were in scope that had a field called next, and if there were two or more, require an annotation.\n- IMHO, for *C itself* to remain a workable language, it needs to add new ways of declaring types that would allow a programmer to distinguish \"number whose value is from 0 to 4294967295\" from \"member of the algebraic ring of values congruent mod 4294967296\". Adding the former to any other kind of number should promote both to the most convenient size large enough to hold both; adding a number to the latter should yield a result of the same algebraic ring type.\n- There's no good reason why a processor with 64-bit registers shouldn't promote computations involving numbers to use the full 64 bits, but on the flip side, a lot of code will break if `uint32_t` suddenly ceases to behave as an abstract algebraic ring. Adding a means of declaring something to be of type (hypothetical syntax) `unsigned[int 32]` when the former is appropriate and `unsigned [restrict 32]` when the latter is needed could greatly improve code robustness and machine-independence while reducing the need for typecasts.\n- BTW, with regard to \"complete control of data\", that's horribly far from being the case, though some small additions to the language (like those above) would 99.9% fix the problem. A further enhancement I'd like to see would be to allow types to specify storage layout [e.g. have `unsigned [restrict uint8_t (0:8,8:8,16:8,24:8)]` indicate that the 32-bit value must be stored using four `uint8_t` values of 8 bits each, LSB first, *regardless of the machine's word size*; if that matches the machine's word order, great; if not, and if the type is used in a `union` or...\n- ...its address is taken, the compiler would be required to use whatever sequence of shifts and stores would be necessary to handle the specified format. Not a huge change to the language, but it would allow many programs to be written in such a way as to be more concise, robust, and performant (if a big-endian platform has a swap-bytes-in-words instruction, a compiler for that platform could use it when loading/storing the above integer type more easily than if the type was read with code like `value=(p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24))`)\n- None of this involves type inference in the sense asked in the question (I believe). C++11 `auto` in particular is extremely trivial, and a far shot from type inference in the sense that people from any other community would usually use that term.\n- @AndreasRossberg Can you give a minimal code example of what such a feature would look like? en.wikipedia.org/wiki/C%2B%2B11#Type_inference says C++11 has type inference (I know, wikipedia ... =) ) I'll have a look into rust and Hindley–Milner.\n- Type inference traditionally describes the situation where type information is not just collected bottom up, but derived from *uses*. Simple example: `succ(n) { return n+1 }`, if you can infer the type of `n` (and thus `succ`) from its usage in an addition.","metadata":{"transformedAt":"2026-08-18T18:32:17.776Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":158,"estimatedTokens":2127}}230{"id":"stack-20746755","source":"stackoverflow","questionId":20746755,"title":"When does C/C++ have to be compiled?","tags":["c++","c","compiler-construction"],"text":"Title: When does C/C++ have to be compiled?\nTags: c++, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThis question assumes that the C/C++ code that is written is portable and does not use platform specific code.\n\nI am beginning to learn C++ and how to compile code. I know that C was created to avoid assembly languages because programs would have to be rewritten for different hardware configurations. I also know that C/C++ has to be recompiled for different computer configurations.\n\nThat made me wonder what constitutes a new configuration. I can download an executable file from the internet that works just fine, and the internet obviously does not know my hardware configuration. Does code have to be compiled for each different CPU? Does code compiled for one OS version work for another one? (Does Windows 7 code work for Windows 8 without a recompilation?)\n\nIn a nutshell, what conditions define a new configuration that has to be compiled for?\n\n========================================\n\nTop Answer:\nAt a hardware level, a C program has to be compiled for each processor architecture. A code compiled for x86 will work on x86 processors, regardless of model or maker (intel/amd), but won't work on Itanium architectures for instance.\n\nAs for OS, if the program uses OS-specific libraries, it will work only on that OS. For instance, a program that uses `CreateFile` from Windows will work on Windows only. There are ways, like macros, in which a program can call a different function depending on what OS was compiled for. Even if a program calls only portable (ISO) functions, it still need to be recompiled for each OS (windows, linux, mac) because executable formats are different for each OS (linux uses ELF, windows uses PE).\n\nFurthermore, for instance, when you deploy a library, for a specific OS and architecture, sometimes you need to have 2 compiled versions: a release and a debug version.\n\nCompilers have optimization options for certain architectures, so you will obtain a program that can run on any x86 architecture, but is optimised for Intel Nehalem processors.\n\nSometimes, new processors introduce new hardware features that are transparent, that is the code does not need to be rewritten, but, in order for the program to benefit from them, the program must be recompiled with a new version of the compiler that can tap into these new features. An example of this was Pentium Pro who introduced new conditional move instructions (C code doesn't need to be rewritten, but the compiler need to be aware so that it generates these instructions).\n\n========================================\n\nCode:\n```text\nCreateFile\n```\n\n```text\n-mfpmath\n```\n\n```text\n-mfpmath=387\n```\n\n========================================\n\nComments:\n- Generally there are two things you need to care about: CPU Architecture and ABI.\n- Does Windows 8 code work for Windows 7 without a recompilation? - Possibly.\n- @joschuck If you don't use platform-specific API's, yes it will run.\n- CPU families usually have a common core to their (assembly) language that they all . So as long as your code is compiled for the correct general platform and OS, it should work.\n- Running compiled C code on a box, on which you did NOT do the compilation, is a recipe for disaster IMO.\n- @ChristopherHarris, and yet commercial software does exist and works well enough.\n- @StoryTeller So you are saying C is portable? I am not denying that commercial software written in C/C++ is available, but to say that \"General platform and OS\" should work.. means you suggest that different flavors or linux would run C/C++ code just fine in a production environment?\n- @ChristopherHarris, in what sense!? Using a standard compliant compiler to compile standard compliant code to a certain platform configuration, will make that code runable on that platform configuration, which may be on another box. And as long as you don't depend on platform specific functionality.. yes, it could run on different flavors of linux.\n- @remyabel: I think you meant homogeneous.\n- @remyabel: You can only edit comments for 5 minute after you post them. If you like, you can copy-and-paste your comment into a new one and delete the old one (and then I'll delete my comments).\n- A lot of hardware is homogeneous nowadays so it's not always necessary to compile a program, you can sometimes distribute binaries. But if you were to compile for completely different architectures, you would need a cross compiler or to distribute the source files so can it be compiled on the destination machine.\n- possible duplicate of Binary compatibility over what range of machines?\n- @sorush-r If the processor changes, the code will have to be recompiled.\n- @ThomasMatthews I assume only operating system (in this case Win 7 and 8) are changed.\n- By the way, word size of a CPU is not the primary consideration. The Intel, Freescale and ARM processor have different instruction sets. An instruction on one is not compatible on the others.\n- @ThomasMathews even better the x86 instruction set has optional instructions, most x86 binaries contain multiple paths to use optional instructions when available and default to the slower core set otherwise. You can tell most compilers to assume that these instructions are present, resulting in smaller and sometimes faster binaries at the cost of being incompatible with other x86 CPUs.\n- Even if a C or C++ program does not use OS-specific libraries, it will often have to be linked with a platform-specific run-time library. So, for example, a \"Hello, world!\" app built for Windows can't be run on Linux, or vice versa, even if the hardware architecture is the same.\n- @KristopherJohnson It's not just the libraries. The format of an executable is different on Linux and on Windows, so even if you don't use anything from the library (which means no input or output), you likely cannot run the executable.\n- Valid points and definitely noteworthy, but doesn't quite answer his question.\n- The operating system *does* make a difference if you use `new` or `delete`, or large parts of the standard library (like `std::cin` or `std::filebuf`).\n- And also, of course, the actual format of the executable varies. If the code is fully compiled and linked (the usual case), Windows cannot load an executable in Linux format and vice versa (as far as I know---there are emulators under Linux which do support running Windows programs, but you need to run them under a special program).\n- Like @bolov said, this doesn't answer my question, but I did not previously know this. +1 for the interesting information.","metadata":{"transformedAt":"2026-08-18T18:32:17.776Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":66,"estimatedTokens":1650}}231{"id":"stack-1898890","source":"stackoverflow","questionId":1898890,"title":"What C99 features are considered harmful or unsupported","tags":["c","compiler-construction","c99","c89"],"text":"Title: What C99 features are considered harmful or unsupported\nTags: c, compiler-construction, c99, c89\nSource: Stack Overflow\n\nQuestion:\nI usually write C code in C89, now some features of C99 (like `intxx_t` or `__VA_ARGS__` or `snprintf`) are very useful, and can be even vital.\n\nBefore I more my requirements from C89 to C99 I wanted to know which of C99 features were widely supported and which ones were not widely supported or even considered harmful.\n\nI know we could just check our target compiler support, but this would narrow our support a lot, and as this is for open source software, I'd prefer having a wider support.\n\nFor example, we use Solaris (suncc) compiler and gcc, but there might be other compiler we would move out of the way while we could keep compatibility with very little efforts.\n\nFor example, I never worked on Windows nor I know anything about Windows compilers, but it would be good to keep Windows compatibility.\n\n========================================\n\nTop Answer:\n`goto` is still considered harmful.\n\nSomehow I have collected *four* down votes. I presented the statement above to add levity, and am only 30% serious about the concept behind it.\n\nI expect the down votes are from youngsters who do not understand the history of programming languages. Not *every single* `goto` is evil, but&ndash;compared to 100% unadulterated spaghetti code I have worked on (millions of lines of FORTRAN 66)&ndash;it is reasonable and productive to replace as many `goto` statements with structured statements (`for`, `while`, `do .. while`, `switch`) as possible. But sometimes a `goto` is just fine when it avoids complexity, such as extra flag variables to break out of multiple nested loops.\n\n========================================\n\nCode:\n```text\nintxx_t\n```\n\n```text\n__VA_ARGS__\n```\n\n```text\nsnprintf\n```\n\n```text\n<stdint.h>\n```\n\n```text\ngoto\n```\n\n```text\ngoto\n```\n\n```text\ngoto\n```\n\n```text\nfor\n```\n\n```text\nwhile\n```\n\n```text\ndo .. while\n```\n\n```text\nswitch\n```\n\n```text\ngoto\n```\n\n```text\n__VA_ARGS__\n```\n\n```text\n<tgmath.h>\n```\n\n```text\nrestrict\n```\n\n```text\nrestrict\n```\n\n```text\ngets\n```\n\n```text\nrealloc\n```\n\n```text\nrealloc(ptr, 0)\n```\n\n========================================\n\nComments:\n- MinGW solves your Window's problems. It's basically `gcc` for Windows.\n- Do you want your software to support embedded platforms? Cross compilers for embedded platforms aren't so likely to support C99.\n- For a related question about the most useful new features in C99, see stackoverflow.com/questions/2047065/&hellip;\n- Oh, it's fun to put one in each program just to annoy purists. But please, have it point downward and not upward.\n- Goto is necessary when you program real world programs in C.\n- No, `goto` is *occasionally useful* when you program real world programs in C.\n- Visual C++ 2010 has stdint.h.\n- Only 11 years late! *sigh*\n- The same applies to `inline`.","metadata":{"transformedAt":"2026-08-18T18:32:17.776Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":19,"totalLines":115,"estimatedTokens":725}}232{"id":"stack-7463034","source":"stackoverflow","questionId":7463034,"title":"How deep do compilers inline functions?","tags":["c++","function","compiler-construction","inline"],"text":"Title: How deep do compilers inline functions?\nTags: c++, function, compiler-construction, inline\nSource: Stack Overflow\n\nQuestion:\nSay I have some functions, each of about two simple lines of code, and they call each other like this: `A` calls `B` calls `C` calls `D` ... calls `K`. (So basically it's a long series of short function calls.) How deep will compilers usually go in the call tree to inline these functions?\n\n========================================\n\nTop Answer:\nI've seen compilers inline more than 5 functions deep. But at some point, it basically becomes a space-efficiency trade-off that the compiler makes. Every compiler is different in this aspect. Visual Studio is very conservative with inlining. GCC (under -O3) and the Intel Compiler love to inline...\n\n========================================\n\nCode:\n```text\nA\n```\n\n```text\nB\n```\n\n```text\nC\n```\n\n```text\nD\n```\n\n```text\nK\n```\n\n```text\nint foo(int a, int b) { return foo(a, b, 3); }\n```\n\n```text\nvoid foo(Bar* x) {\n  if (not x) { return; } // null pointer, pfff!\n\n  // ... BIG BLOC OF STATEMENTS ...\n}\n\nvoid bar(Bar* x) {\n  // DO 1\n  foo(x);\n  // DO 2\n}\n```\n\n```text\nvoid foo@0(Bar* x) {\n  // ... BIG BLOC OF STATEMENTS ...\n}\n\nvoid bar(Bar* x) {\n  // DO 1\n  if (x) { foo@0(x); }\n  // DO 2\n}\n```\n\n```text\n-O3\n```\n\n```text\n-Oz\n```\n\n```text\nstatic\n```\n\n```text\n__force_inline\n```\n\n```text\ngcc\n```\n\n```text\n-finline-limit\n```\n\n========================================\n\nComments:\n- You could simply test and look at the assembly! Your compiler docs should tell you how to specify the inlining depth; I think it's something like 50 for GCC by default.\n- I believe this should be compiler specific and you post no information of your compiler.\n- Under MSVC you have partial control over this using `#pragma inline_depth` (msdn.microsoft.com/en-us/library/cx053bca.aspx) though I have had problems with it in certain situations(such as recursive inlining, which is meant to be possible, but never worked, ended up doing it manually)\n- IIRC in gcc it depends on some approximate \"instruction count\" for the function being inlined (i.e. how long it actually is); nesting level does not play role from what I read in the docs (`-finline-limit` and friends), in the sense that function of length 10 will be inlined just the same as 5 + nested 5.\n- If the functions are called only once there is no reason to avoid inline. GCC will also inline aggressively if profile feedback says it should.\n- @Zan Lynx: That's mostly correct. There are some cases, where it's better to not inline. If the function is in a performance-critical loop and is rarely called (like a trap handler), then it's better to not inline it so to keep the code-size of the loop small. (this will sometimes allow you to use short-jumps instead of long-jumps)\n- I don't usually do this, but I think I should change the accepted answer. :) I didn't know about partial inlining and neither about inlining based on number of calls. Thanks for the details.","metadata":{"transformedAt":"2026-08-18T18:32:17.776Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":14,"totalLines":99,"estimatedTokens":746}}233{"id":"stack-238441","source":"stackoverflow","questionId":238441,"title":"Can a C compiler rearrange stack variables?","tags":["c++","c","compiler-construction","alignment","callstack"],"text":"Title: Can a C compiler rearrange stack variables?\nTags: c++, c, compiler-construction, alignment, callstack\nSource: Stack Overflow\n\nQuestion:\nI have worked on projects for embedded systems in the past where we have rearranged the order of declaration of stack variables to decrease the size of the resulting executable. For instance, if we had:\n\n```\nvoid func()\n{\n char c;\n int i;\n short s;\n ...\n}\n```\n\nWe would reorder this to be:\n\n```\nvoid func()\n{\n int i;\n short s;\n char c;\n ...\n}\n```\n\nBecause of alignment issues the first one resulted in 12 bytes of stack space being used and the second one resulted in only 8 bytes.\n\nIs this standard behavior for C compilers or just a shortcoming of the compiler we were using? \n\nIt seems to me that a compiler should be able to reorder stack variables to favor smaller executable size if it wanted to. It has been suggested to me that some aspect of the C standard prevents this, but I haven't been able to find a reputable source either way.\n\nAs a bonus question, does this also apply to C++ compilers?\n\n**Edit**\n\nIf the answer is yes, C/C++ compilers can rearrange stack variables, can you give an example of a compiler that definitely does this? I'd like to see compiler documentation or something similar that backs this up.\n\n**Edit Again**\n\nThanks everybody for your help. For documentation, the best thing I've been able to find is the paper Optimal Stack Slot Assignment in GCC(pdf), by Naveen Sharma and Sanjiv Kumar Gupta, which was presented at the GCC summit proceedings in 2003.\n\nThe project in question here was using the ADS compiler for ARM development. It is mentioned in the documentation for that compiler that ordering declarations like I've shown can improve performance, as well as stack size, because of how the ARM-Thumb architecture calculates addresses in the local stack frame. That compiler didn't automatically rearrange locals to take advantage of this. The paper linked here says that as of 2003 GCC also didn't rearrange the stack frame to improve locality of reference for ARM-Thumb processors, but it implies that you could.\n\nI can't find anything that definitely says this was ever implemented in GCC, but I think this paper counts as proof that you're all correct. Thanks again.\n\n========================================\n\nTop Answer:\nNot only can the compiler reorder the stack layout of the local variables, it can assign them to registers, assign them to live sometimes in registers and sometimes on the stack, it can assign two locals to the same slot in memory (if their live ranges do not overlap) and it can even completely eliminate variables.\n\n========================================\n\nCode:\n```text\nvoid func()\n{\n    char c;\n    int i;\n    short s;\n    ...\n}\n```\n\n```text\nvoid func()\n{\n    int i;\n    short s;\n    char c;\n    ...\n}\n```\n\n```text\n#pragma pack(push, 16)\n\ntypedef struct _S_speedy_struct{\n\n double fval[4];\n int64  lval[4];\n int32  ival[8];\n\n}S_speedy_struct;\n\n#pragma pack(pop)\n\nint function(...)\n{\n  int i, t, rv;\n  S_speedy_struct *ptr;\n  char buff[112]; // sizeof(struct) + alignment\n\n  // ugly , I know , but it works...\n  t = (int)buff;\n  t +=  15; // alignment - 1\n  t &= -16; // alignment\n  ptr = (S_speedy_struct *)t;\n\n  // speedy code goes on...\n}\n```\n\n========================================\n\nComments:\n- That's a really good point. I hadn't though of this when I was discussing it with my coworker.\n- It can even assign multiple variables to the same register or stack location if it can prove that the variables are never alive in the same section of code. This is common practice, especially with inlined code that leads to short variable lives.\n- This was for a project I worked on a while ago where we were using an older version of the Arm Developer Suite (ADS) compiler to build for several ARM processors. I'm really just asking to settle a discussion about how other compilers handle this.\n- Locals almost always have to be dumped to the stack at some point. The only exception to this is if your function *never* calls another function, in which case everything is transient and can be handled without a stack frame.\n- ... or if a variable is not live around a function call. Like x = f (x) - the old value can be in a register, and the new value goes to a register again.","metadata":{"transformedAt":"2026-08-18T18:32:17.776Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":114,"estimatedTokens":1074}}234{"id":"stack-12876543","source":"stackoverflow","questionId":12876543,"title":"%left and %right in yacc","tags":["syntax","compiler-construction","yacc","operator-precedence"],"text":"Title: %left and %right in yacc\nTags: syntax, compiler-construction, yacc, operator-precedence\nSource: Stack Overflow\n\nQuestion:\n```\n{%\n#include\n#include\n%}\n \n%token ID NUM IF THEN LE GE EQ NE OR AND ELSE\n\n%right '='\n%left AND OR\n%left '' LE GE EQ NE\n%left '+''-'\n%left '*''/'\n%right UMINUS\n%left '!'\n\n%%\n```\n\nThe code mentioned above is part of the yacc for a simple `IF ELSE` program.\n\nWhat do `%right` and `%left` mean?\n\n========================================\n\nTop Answer:\nI know this is an old question but in case some one else is looking for this information :\n\n`%left`, `%right` and `%nonassoc`, defines how yacc will solve repetition of operators. In case you have:\n\n```\n1 + 2 + 3\n```\n\nboth operators have the same precedence level ( they are the same :) ), in this case yacc can solve:\n\n```\n// using %left\n(1 + 2) + 3\n```\n\nor:\n\n```\n// using %right\n1 + (2 + 3)\n```\n\nand finally:\n\n```\n//using %nonassoc\n1 + 2 + 3 is considered illegal and a syntax error!\n```\n\nyou can read more in here.\n\n========================================\n\nCode:\n```text\n{%\n#include<stdio.h>\n#include<stdlib.h>\n%}\n  \n%token ID NUM IF THEN LE GE EQ NE OR AND ELSE\n\n%right '='\n%left AND OR\n%left '<' '>' LE GE EQ NE\n%left '+''-'\n%left '*''/'\n%right UMINUS\n%left '!'\n\n%%\n```\n\n```text\nIF ELSE\n```\n\n```text\n%right\n```\n\n```text\n%left\n```\n\n```text\nexp ::= exp + exp\nexp ::= ID\n```\n\n```text\n%left\n```\n\n```text\n%right\n```\n\n```text\n(6+5)-7\n```\n\n```text\n6+(5-7)\n```\n\n```text\nf()+g()+h()\n```\n\n```text\n1 + 2 + 3\n```\n\n```text\n// using %left\n(1 + 2) + 3\n```\n\n```text\n// using %right\n1 + (2 + 3)\n```\n\n```text\n//using %nonassoc\n1 + 2 + 3 is considered illegal and a syntax error!\n```\n\n```text\n%left\n```\n\n```text\n%right\n```\n\n```text\n%nonassoc\n```\n\n========================================\n\nComments:\n- Also as `%left '*' '&#47;'` is written below `%left '+' '-'`, `* and &#47;` gets more precedence than `+ and -`","metadata":{"transformedAt":"2026-08-18T18:32:17.776Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":156,"estimatedTokens":470}}235{"id":"stack-1420908","source":"stackoverflow","questionId":1420908,"title":"What is the reason for the creation of LLVM?","tags":["compiler-construction","llvm","compiler-theory"],"text":"Title: What is the reason for the creation of LLVM?\nTags: compiler-construction, llvm, compiler-theory\nSource: Stack Overflow\n\nQuestion:\nWhat are the differences between an LLVM and a regular compiler?\n\nIs it more dynamic and thus can be used to compile normally very dynamic languages (i.e. Javascript) into static binary code?\nWhat are the principles behind creating one?\n\nI know the Dragon Book for compilers, but is there such a thing for a LLVM? \n\nEDIT:\nI have found this interesting project.\n\n========================================\n\nTop Answer:\nNothing it is a regular compiler. Its primary reason for creation was to create a platform for compiler research. Therefore it is designed to be very modular so that you can work on that part of the compiler that deals with your research and not have to worry about other parts of the compiler. There is no dragon compiler just as there is no LLVM book(any theory you read in the dragon book or any other compiler book should be directly applicable). In fact while I haven't looked in on LLVM in a while their documentation was pretty poor.\n\n========================================\n\nComments:\n- Their Kaleidoscope tutorials in C++ and OCaml rock.\n- Curious, what sort of problems did gcc cause Apple?\n- @mt3: My understanding is Apple wanted to create extensions to gcc which it did not want to open source, but they were forced to under the GPL. I can't think where I learned this (I wasn't around when it happened, certainly), however this article desctibes a similar problem (search the transcript for 'LLVM').\n- The third format of intermediate representation is an in-memory representation. All the three are isomorphic and LLVM provides tools (llvm-as and llsvm-dis) to convert the textual representation to the on-file binary file and vice-versa\n- GPL has strictly required its user to open-source their work and GPL license being infectious turns out to be annoyed by many campanies.\n- @toolbear: I don't remember at which location, but I've additionally read an article expressing similar Apple concerns. For what it's worth, this is not brand new research.","metadata":{"transformedAt":"2026-08-18T18:32:17.776Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":29,"estimatedTokens":530}}236{"id":"stack-3440297","source":"stackoverflow","questionId":3440297,"title":"There is no such thing as a \"compiled language\" or \"interpreted language\"","tags":["compiler-construction","programming-languages","interpreter"],"text":"Title: There is no such thing as a \"compiled language\" or \"interpreted language\"\nTags: compiler-construction, programming-languages, interpreter\nSource: Stack Overflow\n\nQuestion:\n\"There is no such thing as a \"compiled language\" or \"interpreted language\". Whether a language implementer chooses to write a compiler, an interpreter or anything in between is an implementation detail and has nothing to do with the language. \"\n\nIs the above statement is true ?\n\n========================================\n\nTop Answer:\nSort of. Generally, both interpreters and compilers first need to parse the source code and turn it into a representation, called AST (abstract syntaxt tree). A compiler then turns the AST into executable code (through various transformations), while an interpreter might just directly 'interpret' the AST or sometimes compile and execute it (just-in-time compilation).\n\nThe statement is correct in that this has nothing to do with the language: in theory, you can write an interpreter and compiler for any language. Which one to use really depends on the use-case, scenario and environment.\n\nA compiler has the advantage that he only need to do his job once, regardless of how often you then execute the program. An interpreter needs to parse the source every time (or do some caching), thus you have an overhead for each execution which might take way longer than the actual execution time of the final program. On the other hand, an interpreter is more flexible (it can take into amount the current environment and thus do optimizations a compiler is not allowed to do). But the differences don't stop here, these are just two obvious points.\n\n========================================\n\nCode:\n```text\nset s [eval {sum $a $b $c}]\n```\n\n========================================\n\nComments:\n- @Bill the Lizard: probably from me. Although I claim no originality. Especially the statement \"a language is neither compiled nor interpreted, a language just *is*\" is not originally from me, it is a quote from Shriram Krishnamurthi from an interview on Channel9 a couple of years ago. I think it was this one: Channel9.MSDN.Com/shows/Going+Deep/&hellip;\n- @jorg W Mittag : I am sorry to you, but its true i quoted it from your answer, coz i didnt understood the underlying meaning of your statement. i apologizes to you !\n- @Tuhin: My apologies about misspelling your name, it was just a typo.\n- Would have been good if the statements in the question were cited so we could see the context in which it was proposed and gauge the reliability, expertise and the biases of the person saying it. I appreciate the point, but there are languages that are *typically* compiled and those that are *typically* interpreted, so the terms can usefully serve as a shorthand where exact rigour is not required. There are also proprietary and niche languages where the single implementation is a compiler, in which case the distinction serves little purpose.\n- I've written several interpreters and compilers - none of them built or used an AST.\n- What did you use instead ? And how did you do semantic analysis ?\n- I think you are misunderstanding the question. Nobody is denying the existence of compilers and interpreters. What is being denied is the existence of compiled languages and interpreted languages. A language is just a set of abstract mathematical rules. A language isn't compiled or interpreted. A language just *is*. As evidence, just take one of the gazillions of programming languages for which *no* implementation exists. (Say, Smalltalk-71 or Modula-1.)\n- I'm saying pretty much what everyone else is saying. How is it that I'm singled out as the one who is misunderstanding? I'm talking about the underlying implementation details, but coming to the same conclusion as most of the other respondents.\n- Purely interpreted languages never generate machine code. Rather, each statement will be processed using the equivalent of either an if-ifelse-else tree or a switch-case statement. The interpreter doesn't generate any machine code from the statements it examines; rather, it simply does the appropriate actions directly.\n- \"A compiled program is one that is turned into machine code\". Compilers don't always target machine code. For example, the C# and OCaml compilers generate bytecodes for virtual machines.\n- -1 \"must be partially interpreted\". You still don't have to interpret.\n- How, exactly, would you go about compiling a block like the above when you can't be sure if parts of it are data or code? Or, better yet, code like [$something {sum $b $c}]... which you don't know if the value of the variable $something is [eval] or [puts] (meaning the value in the curly braces could be code or data) until runtime.\n- The same way SBCL compiles `(EVAL '(+ 2 3))` or any other compiled language that supports metaprogramming. Code is data does not preclude compilation.\n- I was under the impression that SBCL is able to compile that because it can tell, at the time of compilation, that EVAL treats it's argument as code. Is it possible to redefine what EVAL (for every place it's used, even already defined functions) does at runtime?\n- \"Is it possible to redefine what EVAL...does at runtime?\". Yes, you can redefine what anything does at run-time using something like Lisp.\n- Interesting, then how does the compiler know whether to compile the input of a function to code or data, since it won't know which it is until it actually gets to the point where it runs the command?\n- It'll generate code for both with a run-time test to jump to the appropriate one. So it is really no different to any other control flow construct. If the code to be evaluated is generated at run-time then it will invoke the compiler at run time and jump into the newly generated code. I did this in HLVM where even the garbage collector is generated for types as you type them in: hlvm.forge.ocamlcore.org\n- I tend to think that, if you have to have conditional logic in the compiled code to be able to handle every single argument to every command being either code or data, the final result is going to be... unwieldy. Add to that the fact that you point out that you need to be able to handle compiling code at runtime (pretty much an interpreter), and I'm not sure what I said is far off from what you're saying. Especially when you take into account things like compiling to bytecode (at runtime or not).\n- Yes, if you regard run-time compilation as interpretation then they are the same thing. I think of an interpreter as a program that executes another program directly without translating it into another language. That is a subjective definition not univerally held...","metadata":{"transformedAt":"2026-08-18T18:32:17.777Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":47,"estimatedTokens":1669}}237{"id":"stack-35399281","source":"stackoverflow","questionId":35399281,"title":"How can I build the post-dominator tree of a function with an endless loop?","tags":["algorithm","graph","compiler-construction","llvm","decompiler"],"text":"Title: How can I build the post-dominator tree of a function with an endless loop?\nTags: algorithm, graph, compiler-construction, llvm, decompiler\nSource: Stack Overflow\n\nQuestion:\nOne of my side projects is a decompiler that turns native code into LLVM IR, simplifies it and outputs pseudo-C. An essential phase of the program's process is pattern-independent control flow structuring, which finds regions in a program and turns them into structured control flow statements (i.e. not gotos).\n\nI had to roll out my own code for finding regions because LLVM's regions don't look exactly how the paper expects them. However, finding regions requires you to have a post-dominator tree, and it turns out that LLVM's post-dominator tree building algorithm can't make one for functions that have no end block, like functions that \"end\" with an endless loop.\n\nThis appears to be because the tree-building algorithm needs a starting point. Normally, the starting point is the function's returning block, since it post-dominates every other block; but there isn't any in functions that spin into an endless loop, since they don't have any `return` or `unreachable` terminator. (At this point, it might be worth noting that LLVM's region code also relies on a post-dominator tree and is also useless for functions for which it can't be built.)\n\nIt seems to me that even though this algorithm fails, the fact that a function doesn't return doesn't mean that you can't make a post-dominator tree for it.1 In fact, if that endless loop has a single back-edge (which is something that I can ensure), the node with that back edge necessarily post-dominates every other node in the graph, so it should be possible to make a post-dominator tree.\n\nIf I could find that node, I could probably feed it to LLVM's post-dom infrastructure and get a reasonable post-dominator tree out of it. Unfortunately, I'm not very imaginative and the only straightforward way that I can think of to identify that crucial node is that \"it's the one that post-dominates everything\", which certainly won't help me bootstrap a post-dominator tree.\n\nFinding back edges isn't particularly hard. As Doug Currie says, you can do it with a simple DFS, and in fact, another part of my project does exactly that. However, in the case of a function with an endless loop and nested, terminating loops, I don't know how I would tell the inner back edge from the outer back edge without domination information. (If it can help, at this stage of the process, every loop is guaranteed to have a single entry node and at most one exit node.)\n\nSo how can I build the post-dominator tree of a function that doesn't have any returning basic block?\n\n1. My compiler and graph theory background is entirely self-taught. This might not be accurate.\n\n========================================\n\nCode:\n```text\nreturn\n```\n\n```text\nunreachable\n```\n\n========================================\n\nComments:\n- As a hack, to make your analysis succeed, could you perhaps insert a fake end-condition in the loop, which never actually becomes true, e.g. in pseudo-code: `if (5 == 7) break;`?\n- @500-InternalServerError, I still need to find out where to put it exactly, and that would be at the node with the back-edge, which I don't know how to find.\n- You can find back edges with DFS; see, e.g., cs.yale.edu/homes/aspnes/pinewiki/DepthFirstSearch.html\n- @DougCurrie, there's no guarantee that a function with an endless loop has just one loop. If the endless loop has an inner (terminating) loop, I don't know how to determine which back edge belongs to the endless loop. I should include that in the question.\n- @zneak, see \"Identifying Loops In Almost Linear Time\" by G RAMALINGAM, pages.cs.wisc.edu/~ramali/Papers/toplas99.ps which describes near linear time algorithms for finding loops and loop nesting.\n- This is actually what I was trying to get at with my comment, though I wasn't able to word is as well as you have.\n- Does that work even when not every unreachable node is part of an endless cycle?\n- By definition, all unreachable nodes must be part of an endless cycle, since every node must have at least one successor (if only itself).\n- Seems to me that an unvisited node could unconditionally lead to an endless cycle without being part of it, no?\n- @zneak: Yes, but such a node must lead unconditionally to an endless cycle -- you can't have unvisited nodes without endless cycles. So once you've added never-taken branches that result in all nodes being reachble by the reverse-edge traversal from the exit, you have taken care of all the endless cycles. Its just that there are multiple ways you might do that (that may result in more or less efficient code), but all such ways are \"correct\" in the sense that they preserve the semantics of the original program.\n- I settled on this: (1) ask LLVM to make its post-dom tree; (2) find every back edge in the function; (3) check if every back edge destination has a tree node. If so, use LLVM's post-dominator tree. Otherwise, take the tree's root and add every back edge destination that didn't have a tree node as a root, and calculate a new post-dominator tree. It appears to work.\n- Some additional context for people who may stumble upon this: it's entirely true that you can add an exiting edge to any node in the loop and get *correct* results, but best results are achieved when you add them to a node that has a back-edge. This allows that node to post-dominate the header.","metadata":{"transformedAt":"2026-08-18T18:32:17.777Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":47,"estimatedTokens":1369}}238{"id":"stack-888100","source":"stackoverflow","questionId":888100,"title":"Why python compile the source to bytecode before interpreting?","tags":["python","compiler-construction","interpreter","bytecode"],"text":"Title: Why python compile the source to bytecode before interpreting?\nTags: python, compiler-construction, interpreter, bytecode\nSource: Stack Overflow\n\nQuestion:\nWhy python compile the source to bytecode before interpreting?\n\nWhy not interpret from the source directly?\n\n========================================\n\nTop Answer:\nBecause you can compile to a `.pyc` once and interpret from it many times.\n\nSo if you're running a script many times you only have the overhead of parsing the source code once.\n\n========================================\n\nCode:\n```text\n.pyc\n```\n\n```text\nimport\n```\n\n========================================\n\nComments:\n- Sounds like homework to me.\n- Even the old MS BASIC on my TRS-80 used a very simple encoding scheme: as soon as I typed or edited a line, the BASIC keywords were collapsed into single bytes.\n- @DavidThornley: Many 1980's computers used a derivative of MS-basic which converted keywords to tokens, but kept numbers and variable names in textual form, thus having to compute their values or look them up at runtime. Atari BASIC did more processing, building a table of variables and replacing their names with indices. It also converted numeric numbers to BCD floating-point numbers. Only string literals and comments were stored as text. Such things might have allowed Atari BASIC to outperform others, except that each line's number was stored as binary, but GOTO targets...\n- ...were stored as BCD floating-point, so every GOTO required a BCD-to-binary conversion. Still, it's interesting that the author of Atari BASIC converted the program into a parsed representation rather than simply replacing keywords with tokens.\n- Very well explained!!\n- Just running a \".py\" file does result in re-lexing and parsing the source code over and over, right? If the file is large, this can be a significant overhead. I'm not sure why import receives special treatment. I'd really appreciate any help in understanding.\n- I didn't -1, but I will be honest that I didn't understand your point, especially this part of it: \"would be more practical than to find and replace text in some source code string\"\n- Watch the video on channel 9, or write your own VM, either two would probably explain the whole process in detail for you. What I meant by that quote is that it's sometimes easier to perform optimizations on a higher-level of abstraction than assembly. When you do this normally you work with an AST (abstract syntax tree), if you don't have one you can still perform the same optimizations but they have to move source code around, i.e. find and replace and that's really impractical, execution environments tend to go with other intermediate representation for this reason (see three-address code).\n- The byte-code is just a more efficent compact and practical representation of the AST.","metadata":{"transformedAt":"2026-08-18T18:32:17.777Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":39,"estimatedTokens":707}}239{"id":"stack-1515940","source":"stackoverflow","questionId":1515940,"title":"Learning how programming languages work","tags":["compiler-construction","interpreter"],"text":"Title: Learning how programming languages work\nTags: compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nI've been programming for years (mainly Python), but I don't understand what happens behind the scenes when I compile or execute my code.\n\nIn the vein of a question I asked earlier about operating systems, I am looking for a gentle introduction to programming language engineering. I want to be able to define and understand the basics of terms like compiler, interpreter, native code, managed code, virtual machine, and so on. What would be a fun and interactive way to learn about this?\n\n========================================\n\nTop Answer:\ncompilers, interpreters and virtual machines are just examples of implementation details. What you might look for is programming languages theory, generative grammar, language translators, and you need possibly some computer architecture to relate theory with implementations.\n\nPersonally, I learned from Sebesta's book. It gives a very wide introduction to the subject without going into minute details. It also, has a good chapter on the history of programming languages (~20 languages ~3 papers per language). It has nice explanation about grammars and theory of languages in general. Also, It gives a good introduction into Scheme, Prolog, and programming paradigms (Logic, Functional, Imperative^, Object oriented).\n\n^ It concentrate a lot more on the imperative paradigm than the first two.\n\n========================================\n\nCode:\n```text\n=\n                    / \\\n                   /   \\ \n                  a     +\n                       / \\\n                      /   \\\n                     6     *\n                          / \\\n                         b   c\n```\n\n========================================\n\nComments:\n- Definitive compiler resources question on SO: stackoverflow.com/questions/1669/learning-to-write-a-compile&zwnj;&#8203;r There are some suggested resources there which take a very easy going approach: if you can program, you are ready to learn compiler methods. Do not be afraid. I lik the Crenshaw tutorial.\n- ^ That is an excellent list of resources. Thanks.","metadata":{"transformedAt":"2026-08-18T18:32:17.777Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":38,"estimatedTokens":541}}240{"id":"stack-22612848","source":"stackoverflow","questionId":22612848,"title":"Is there a difference between cast and strong type assignment?","tags":["c#","types","compiler-construction"],"text":"Title: Is there a difference between cast and strong type assignment?\nTags: c#, types, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI sort of ran into this today when writing some code. Take the following as an example:\n\n```\nlong valueCast = (long)(10 + intVariable);\nlong valueTyped = 10L + intVariable;\n```\n\nIs there any difference between these two or are they compiled to exactly the same thing? Is there a convention for one over the other?\n\nSo I know this isn't a critical question (both work). I'm just very curious about what the difference(s) might be!\n\n**EDIT** - Modified the code example to be closer to what my original scenario actually is. I wanted the question to be clear so I replaced the variable with a constant. Didn't realize the compiler would to the arithmetic automatically (thereby changing the answers to this question)\n\n========================================\n\nTop Answer:\nWell, this is a compile error:\n\n```\nlong valueCast = (long)(2147483647 + 2);\n```\n\n\"The operation overflows at compile time in checked mode.\"\n\nWhereas this works fine:\n\n```\nlong valueTyped = (2147483647L + 2);\n```\n\nAs Jon Skeet says, the difference is whether you convert to `long` before doing the sum, or afterwards.\n\n========================================\n\nCode:\n```text\nlong valueCast  = (long)(10 + intVariable);\nlong valueTyped = 10L + intVariable;\n```\n\n```text\nunchecked\n{\n    long valueCast  = (long)(2000000000 + 2000000000);\n    long valueTyped = 2000000000L + 2000000000;\n    Console.WriteLine(valueCast);\n    Console.WriteLine(valueTyped);\n}\n```\n\n```text\n-294967296\n4000000000\n```\n\n```text\nlong\n```\n\n```text\nlong\n```\n\n```text\nlong\n```\n\n```text\nlong valueCast = (long)(2147483647 + 2);\n```\n\n```text\nlong valueTyped = (2147483647L + 2);\n```\n\n```text\nlong\n```\n\n========================================\n\nComments:\n- @Fr&#233;d&#233;ricHamidi No, they are functionally different, so they certainly won't compile to the same thing. If the parenthesis were different, and the cast were before the addition, *then* they could potentially compile to the same thing.\n- @Servy, point taken. I was about to speak up, but the questioner changing literal `2` into `intVariable` makes my point moot.\n- As an FYI for others later. I tried referencing the type documentation for long first. It didn't solve my question but it might help someone else later.\n- I discuss your question in my recent article on arithmetic pitfalls, here: devproconnections.com/development/&hellip;\n- Also, this \"late promotion\" defect is most commonly seen with multiplications, ie, `long p = (long)(a * b * c * d * e);` when what is meant is `long p = (long)a * b * c * d * e);`\n- @EricLippert Off topic a bit, but I'm guessing C# uses two's complement to store integer + long numbers then?\n- @drew_w: The spec does not come right out and say so, but yes, integers in C# are twos-complement.\n- So (long)10 + 2 and 10L + 2 are equivalents? (I really have the doubt)\n- @Areks: Yes, *those* are equivalent... although the latter would allow you to express constants outside the 32-bit range, of course.\n- Thank you Jon! I watched your answer being constructed and voted from scratch, I feel like I can die with no regrets now!\n- And of course, Jon edits in essentially the same example as I'm typing my answer. :)\n- @dev_feed Well, it is Jon Skeet we're talking about, after all.","metadata":{"transformedAt":"2026-08-18T18:32:17.777Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":99,"estimatedTokens":842}}241{"id":"stack-15148425","source":"stackoverflow","questionId":15148425,"title":"C++: Class specialization a valid transformation for a conforming compiler?","tags":["c++","compiler-construction","compiler-optimization","virtual-functions","vtable"],"text":"Title: C++: Class specialization a valid transformation for a conforming compiler?\nTags: c++, compiler-construction, compiler-optimization, virtual-functions, vtable\nSource: Stack Overflow\n\nQuestion:\n*Hopefully this isn't too specialized of a question for StackOverflow: if it is and could be migrated elsewhere let me know...*\n\nMany moons ago, I wrote a undergraduate thesis proposing various devirtualization techniques for C++ and related languages, generally based on the idea of precompiled specialization of code paths (somewhat like templates) but with checks to choose the correct specializations are chosen at runtime in cases where they cannot be selected at compile-time (as templates must be).\n\nThe (very) basic idea is something like the following...suppose you have a class `C` like the following:\n\n```\nclass C : public SomeInterface\n{\npublic:\n C(Foo * f) : _f(f) { }\n\n virtual void quack()\n {\n _f->bark();\n }\n\n virtual void moo()\n {\n quack(); // a virtual call on this because quack() might be overloaded\n }\n\n // lots more virtual functions that call virtual functions on *_f or this\n\nprivate:\n Foo * const _f; // technically doesn't have to be const explicitly\n // as long as it can be proven not be modified\n};\n```\n\nAnd you knew that there exist concrete subclasses of `Foo` like `FooA`, `FooB`, etc, with known complete types (without necessarily having an exhaustive list), then you could precompile specialized versions of `C` for some selected subclasses of `Foo`, like, for example (note the constructor is not included here, purposely, since it won't be called):\n\n```\nclass C_FooA final : public SomeInterface\n{\npublic:\n virtual void quack() final\n {\n _f->FooA::bark(); // non-polymorphic, statically bound\n }\n\n virtual void moo() final\n {\n C_FooA::quack(); // also static, because C_FooA is final\n // _f->FooA::bark(); // or you could even do this instead\n }\n\n // more virtual functions all specialized for FooA (*_f) and C_FooA (this)\n\nprivate:\n FooA * const _f;\n};\n```\n\nAnd replace the constructor of `C` with something like the following:\n\n```\nC::C(Foo * f) : _f(f)\n{\n if(f->vptr == vtable_of_FooA) // obviously not Standard C++\n this->vptr = vtable_of_C_FooA; \n else if(f->vptr == vtable_of_FooB)\n this->vptr = vtable_of_C_FooB;\n // otherwise leave vptr unchanged for all other values of f->vptr\n}\n```\n\nSo basically, the dynamic type of the object being constructed is changed based on the dynamic type of the arguments to its constructor. (Note, you can't do this with templates because you can only create a `C` if you know the type of `f` at compile-time). From now on, any call to `FooA::bark()` through `C::quack()` only involves one virtual call: either the call to `C::quack()` is statically bound to the non-specialized version which dynamically calls `FooA::bark()`, or the call to `C::quack()` is dynamically forwarded to `C_FooA::quack()` which statically calls `FooA::bark()`. Furthermore, dynamic dispatch might be eliminated completely in some cases if the flow analyzer has enough information to make a static call to `C_FooA::quack()`, which could be very useful in a tight loop if it allows inlining. (Although technically at that point you'd probably be OK even without this optimization...)\n\n(Note that this transformation is safe, although less useful, even if `_f` is non-const and protected instead of private and `C` is inherited from a different translation unit...the translation unit creating the vtable for the inherited class won't know anything at all about the specializations and the constructor of the inherited class will just set the `this->vptr` to its own vtable, which will not reference any specialized functions because it won't know anything about them.)\n\nThis might seem like a lot of effort to eliminate one level of indirection, but the point is that you can do it to any arbitrary nesting level (any depth of virtual calls following this pattern could be reduced to one) based only on local information within a translation unit, and do it in a way that's resilient even if new types are defined in other translation units that you don't know about...you just might add a lot of code bloat that you wouldn't have otherwise if you did it naively.\n\nAnyway, **independent of whether this kind of optimization would really have enough bang-for-the-buck be worth the effort of implementation and also worth the space overhead in the resulting executable**, my question is, is there anything in Standard C++ which would prevent a compiler from performing such a transformation?\n\nMy feeling is no, since the standard doesn't specify at all how virtual dispatch is done or how pointers-to-member-functions are represented. I'm pretty sure there's nothing about the RTTI mechanism preventing `C` and `C_FooA` from masquerading as the same type for all purposes, even if they have different virtual tables. The only other thing I could think of that could possibly matter is some close reading of the ODR, but probably not.\n\nAm I overlooking something? Barring ABI/linking issues, would transformations like this be possible without breaking conforming C++ programs? (Furthermore, if yes, could this be done currently with the Itanium and/or MSVC ABIs? I'm fairly sure the answer there is yes, as well, but hopefully someone can confirm.)\n\n**EDIT**: Does anyone know if anything like this is implemented in any mainstream compiler/JIT for C++, Java, or C#? (See discussion and linked chat in the comments below...) I'm aware JITs do speculative static-binding/inlining of virtuals directly at call sites, but I don't know if they do anything like this (with entirely new vtables being generated and chosen based on a single type check done at the constructor, rather than at each call site).\n\n========================================\n\nTop Answer:\nOn first reading, this sounds like a c++-focused variation of polymorphic inline caching. I think that V8 and Oracle's JVM both use it, and I know that .NET does.\n\nTo answer your original question: I don't think there's anything in the standard that forbids these kinds of implementations. C++ takes the \"as-is\" rule quite seriously; so long as you faithfully implement the right semantics, you can do the implementation in any crazy way you like. c++ virtual calls aren't very complicated, so I doubt you'd trip over any edge cases there either (unlike if, say, you were trying to do something clever with *static* binding).\n\n========================================\n\nCode:\n```text\nclass C : public SomeInterface\n{\npublic:\n    C(Foo * f) : _f(f) { }\n\n    virtual void quack()\n    {\n        _f->bark();\n    }\n\n    virtual void moo()\n    {\n        quack(); // a virtual call on this because quack() might be overloaded\n    }\n\n    // lots more virtual functions that call virtual functions on *_f or this\n\nprivate:\n    Foo * const _f; // technically doesn't have to be const explicitly\n                    // as long as it can be proven not be modified\n};\n```\n\n```text\nclass C_FooA final : public SomeInterface\n{\npublic:\n    virtual void quack() final\n    {\n        _f->FooA::bark(); // non-polymorphic, statically bound\n    }\n\n    virtual void moo() final\n    {\n        C_FooA::quack(); // also static, because C_FooA is final\n        // _f->FooA::bark(); // or you could even do this instead\n    }\n\n    // more virtual functions all specialized for FooA (*_f) and C_FooA (this)\n\nprivate:\n    FooA * const _f;\n};\n```\n\n```text\nC::C(Foo * f) : _f(f)\n{\n    if(f->vptr == vtable_of_FooA) // obviously not Standard C++\n        this->vptr = vtable_of_C_FooA; \n    else if(f->vptr == vtable_of_FooB)\n        this->vptr = vtable_of_C_FooB;\n    // otherwise leave vptr unchanged for all other values of f->vptr\n}\n```\n\n```text\nC\n```\n\n```text\nFoo\n```\n\n```text\nFooA\n```\n\n```text\nFooB\n```\n\n```text\nC\n```\n\n```text\nFoo\n```\n\n```text\nC\n```\n\n```text\nC<Foo>\n```\n\n```text\nf\n```\n\n```text\nFooA::bark()\n```\n\n```text\nC::quack()\n```\n\n```text\nC::quack()\n```\n\n```text\nFooA::bark()\n```\n\n```text\nC::quack()\n```\n\n```text\nC_FooA::quack()\n```\n\n```text\nFooA::bark()\n```\n\n```text\nC_FooA::quack()\n```\n\n```text\n_f\n```\n\n```text\nC\n```\n\n```text\nthis->vptr\n```\n\n```text\nC\n```\n\n```text\nC_FooA\n```\n\n```text\nFoo::~Foo()\n```\n\n```text\nC::quack()\n```\n\n```text\nc\n```\n\n```text\nc._f\n```\n\n```text\nFoo::bark()\n```\n\n```text\n_f\n```\n\n```text\nFooA\n```\n\n```text\nc\n```\n\n========================================\n\nComments:\n- Isn't this pretty much the same thing as making C a template ?\n- Yes, but you can't choose a specialization at runtime unless you do it manually...you can only choose a specific template at compile-time, meaning you can't wrap compile-time polymorphism with run-time polymorphism without doing the type checks yourself manually, which is error prone and fragile. This method is basically having the compiler do it for you, based on runtime information.\n- Well the right foo has to be chosen manually anyway, so I don't really see the difference. Well, one can now put multiple Cs with different foos in one container - but this will probably never happen since C has virtual functions itself and one probably wants container>.\n- What do you mean \"foo has to be chosen manually\"? It's done at runtime, and you don't need an exhaustive type tree to do it...you just specialize for the types you know.\n- Yes, exactly, you call through SomeInterface, and you dynamically get the right `C_FooX`, but you then statically call the right `FooX`. Or you statically call `C` and it dynamically gets you the right `FooX`. Two virtual calls instead of one, and you can do this to however arbitrary nesting level as long as the type information is in the current TU, and it's resilient against additional types being added in other TUs (so you don't need whole program analysis).\n- (The big problem is that, unlike templates, you don't know which of these specializations you actually need until runtime, so if you do it blindly you'll duplicate huge amounts of code...so you need heuristics or programmer hints)\n- This ideone.com/71VWTG should produce perfect instructions. There's only one virtual call if we know the type. I don't really see the problem. :)\n- but you instantiated a template over foo! that's the whole point...what if foo was passed to you from elsewhere? Also, the call `foo_->foo();` will not be reliably statically dispatched...it might here, with flow analysis, since everything is in one TU, but you can create a unique_ptr that holds a subtype of Foo. Just because it's a unique_ptr doesn't provide any guarantee that it's non-polymorphic because there's no syntax in C++ for specifying a non-polymorphic pointer to a polymorphic type.\n- It is not only statically dispatched, the function disappears completely (gets inlined), because we already know the exact type. If we do not know the type I just pass an unique_ptr, as demonstrated. Edit: Note that the type of the template parameter does not have to be polymorphic! It can be anything you want!\n- That relies on flow analysis that might not be reliable. What if you were passed a unique_ptr from another TU? There's no way to know it actually holds a Foo rather than a subtype of Foo. And the point is, in the `unique_ptr` case there will be two virtual calls instead one one.\n- Look, I program with templates all the time :) I'm not saying they don't work when you have compile time type information. In fact, they work so well, I think the compiler should generate pseudo-templates them automatically so they can be used when you don't have compile-time type information. That's the whole point.\n- Yes, of course, because the unique_ptr class demonstrates the unknown case. And it is definitely reliable that the other call gets inlined *because it is not virtual*. That's the whole point.\n- let us continue this discussion in chat\n- Sorry, my mistake on the flow analysis part, didn't look at the implementation carefully enough (but the rest of my comments about getting an unknown foo are valid)\n- (Nice discussion in the chat, if anyone wants to jump in)\n- AFAIK polymorphic inline caching only is done at the call site directly, though, right, so guard checks still need to be done per-call? (This technique only does a single guard check at the constructor, if the class fulfills are certain pattern, and doesn't incur any overhead after that).\n- Yes, but in practice if you have a PIC you also have a JIT, so if you can statically prove that the target has a particular type, then you can compile out the branch too. But I'm not sure you'd bother; branches are cheap so long as they're easy to predict, so as long as the type is always same, you win.\n- It's not always the same type though, that's the problem. It's the same type for a single given object, but the same code might be called alternatively between many different objects yielding different targets, trashing your branch prediction. This specializes the entire class for each type, so each class gets to predict the branch independently.\n- (It basically allows near-zero-overhead Pimpl, within restrictions.)\n- Yes, it seems you need the source of all non-pure virtual destructors of all base classes of `FooA` available in the TU to do this, then, and only do it if you can prove that there are no calls from there to any virtual function of `C`. Inconvenient, but possible to work with. Anyway, thanks, that was helpful...any other holes you can think of?\n- Darn, you also need to protect against the case that `C`'s constructor is being called during the constructor of a subclass of `FooA`...I think you can work around this but it requires that you control `FooA` and provide some mechanism that allows `C` to verify that `FooA` is fully constructed.\n- \"non-pure\" is irrelevant here. A pure virtual destructor has a definition and is called just like any other, and can cause exactly the same scenario.\n- Right, not sure what I was thinking with that.\n- Actually, the second scenario is fine too, you just need to be able to statically analyze `FooA`'s constructor--what the derived types do is irrelevant if there's no way `FooA`'s constructor calls `C`'s constructor...I think I worked this out a long time ago and just forgot the details.\n- I'm going to accept this just on the basis that you've helped me uncover and plug a hole in this method, unless someone else has a more authoritative answer.","metadata":{"transformedAt":"2026-08-18T18:32:17.777Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":33,"totalLines":301,"estimatedTokens":3581}}242{"id":"stack-6672008","source":"stackoverflow","questionId":6672008,"title":"LLVM and the future of optimization","tags":["optimization","gcc","compiler-construction","llvm","compiler-optimization"],"text":"Title: LLVM and the future of optimization\nTags: optimization, gcc, compiler-construction, llvm, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nI realize that LLVM has a long way to go, but theoretically, can the optimizations that are in GCC/ICC/etc. for individual languages be applied to LLVM byte code? If so, does this mean that any language that compiles to LLVM byte code has the potential to be equally as fast? Or are language specific optimizations (before the LLVM bytecode stage) going to always play a large part in optimizing any specific program.\n\nI don't know much about compilers or optimizations (only enough to be dangerous), so I apologize if this question isn't well defined.\n\n========================================\n\nTop Answer:\nI addition to Dietrich's excellent answer, I think it is important to appreciate that it is not just the compiler that determines how fast programming languages are. In addition to the various optimizations that a given language may allow/disallow, there is also the matter of *how* you do certain tasks in various programming languages and what the language *lets* you do.\n\nFor example, it is relatively easy to optimize C code to maximize cache efficiency (reducing slow reads from memory) while this is much harder in Haskell. Pointer hacks are impossible in Java. As is the strategy of allocating a huge chunk of memory and parceling it out by hand.\n\nThus, some languages will always be slower simply because they don't allow for the same level of optimization. Note that I'm not necessarily saying that this is a bad thing because with that slowness come extremely powerful constructs.\n\nI think that a better way to look at it is that LLVM will allow for a certain set of optimizations to be applied to all languages that compile down to it. Thus, while it *will* make such languages faster, it will not make them *equally* fast.\n\nEdit: Pointer hacks in Haskell. So many possibilities ...\n\n========================================\n\nCode:\n```text\ntypedef int (*returns_int)();\nstruct pair { returns_int first, second};\ntypedef struct pair *(*returns_pair)();\n\nint function(returns_pair arg);\n```\n\n```text\nint function(int x, int y); // note that it takes *two* arguments now\n```\n\n```text\n(Int, Int) -> Int\n```\n\n```text\nfunction\n```\n\n```text\nfunction machinecode_to_sourcecode( given_machinecode ){\n  it = all_posibble_strings_iterator()\n  while (true) {\n    possible_sourcecode = it->get_string()\n    machine_code = compile possible_sourcecode with X\n    if (compilation succeeded)\n        if(machine_code == given_machinecode)\n           return possible_sourcecode;\n    else\n       it = it->next_string()\n  }\n}\n```\n\n========================================\n\nComments:\n- llvm is doing quite well actually, starting with version 28 it was producing better/faster code than gcc 4.x (for the applications I tested it with). That was using it as a cross compiler not a runtime virtual machine. The number of optimization combinations with llvm as a cross compiler grow exponentially relative to what gcc can offer. You can optimize any individual file or combine the bytecode in any combination and optimize that combination. Optimize from C/C++ to bytecode or wait until after bytecode has been linked, etc.\n- My question really revolved around leveling the playing field between the languages that get lots of attention in optimizing compilers (C, C++), and the languages that don't yet have the interest or manpower (haskell, d, ocaml, etc) to really do the level of optimization needed to bring them to the same level as C and C++. The hope was that LLVM would make all these languages have similar performance, which made choosing a language a more open choice (for my particular field, which is numerical simulations and modeling, and needs the speed).\n- I would think the concept of turning the language into an internal language, bytecode or icode or what have you, then applying a list of optimizations, which are not really specific to the language, should be doable, then when going from the internal code to the target you have more optimizations there, which again are not tied to the original language. If these other languages can build front ends to llvm for example, or gcc for that matter, they should be able to take advantage of the existing optimizers.\n- @dwelch: That was my original thought, however, it looks like, in order to get the best performance, you would need to do language specific optimizations (look at the answer by Dietrich), then LLVM byte-code specific optimizations. So, while the general performance of languages will improve (because there will be a set of \"llvm optimizations\" that every language can take advantage of), the languages with the manpower behind them will ultimately be faster because there are more people and more interest in writing language specific optimizations.\n- definitely agree with that. You need the talent and manpower. And you need the demand/interest, if nobody wants to use it then you lose the early adopters and beta testers and eventually the talent/manpower loses interest and moves on. Good, bad, or otherwise this is what keeps C/C++ going.\n- So, ultimately, LLVM will not level the playing field between different languages, or at least not completely. C and C++ will, for the foreseeable future have more manpower behind them doing language specific optimizations in the frontend which will make them faster languages overall... Or am I reading this wrong?\n- Yes and no. My point is that the LLVM optimizer is never enough, by itself, to compete with a language-specific optimizer. I did not intend to compare languages to each other. I would hesitate to make any predictions about the \"manpower\" behind C/C++ compilers versus other languages. Certainly, LLVM won't change whether folks write web apps in C (they mostly don't) or write audio codecs in Python (they mostly don't). But if you can get (for example) Haskell code easily within 2x performance of C code, then it gets harder to justify low-level programming.\n- ok, I've worked at it, but I have no idea how to parse the C that you wrote. What does \"typedef int (*returns_int)();\" mean? I'm lost.\n- It's exactly what the name says -- a function which returns an int.\n- So you are declaring that the type of a function which returns an int, is an int? Is that piece of code legal? or is it pseudocode? Sorry for the questions, I've never seen anything like that in C.\n- Maybe you should review C typedefs? The name of the type is `returns_int`, it is a function which returns an `int`. The way to write that in C is `typedef int (*returns_int)(void);` This kind of definition is used often in libraries for callback functions.\n- You're right to say that LLVM is a poor choice for optimizing high level languages, because of the loss of semantic information, but it's also worth noting that a high level representation that has all of the semantic information of a program is a bad place to perform low level optimizations. What tends to happen is that as a program is compiled, it transitions through several IRs that become progressively lower level. LLVM is still useful for high-level languages like Haskell, just not as the first step in the optimizer.\n- In practise, llvm captures much more than assembly, so it's possible in many cases - depending on algorithm complexity and available resources - to apply transformations, that are more convenient to high level languages, to llvm byte code itself.\n- Pointer hacks are possible in Haskell. You'll get a lot of worried looks, the code will be ugly, but you can do it. (Yes, Haskell does have pointer types.)\n- \"the IR is not really suitable for Functional Languages.\" very wrong, but this was old so no down vote :). julialang.org\n- For any given bytecode there will be an infinite number of source versions which produce that bytecode. As a trivial example you can always just wrap a function in its inverse `func( inv( func( inv( a )`. Therefore, even ignoring the practicality of this approach, it doesn't work in theory (it takes *infinite* time).\n- Not necessarily... when you want to find any source that produces given bitcode it might be possible by setting appropriate search over space of all possible valid source-codes (where such kind of inverse, you've presented might be a kind of loop), but unfortunately I have no hard proof under my hand or counter-example to disprove, so please kindly take it as intuition (btw. that might interest you as well)","metadata":{"transformedAt":"2026-08-18T18:32:17.777Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":80,"estimatedTokens":2131}}243{"id":"stack-1680024","source":"stackoverflow","questionId":1680024,"title":"Compiler optimization: Java bytecode","tags":["java","optimization","compiler-construction","bytecode"],"text":"Title: Compiler optimization: Java bytecode\nTags: java, optimization, compiler-construction, bytecode\nSource: Stack Overflow\n\nQuestion:\nI'm currently writing a toy compiler targeting Java bytecode in the translation.\n\nI would like to know if there is some kind of catalog, maybe a summary, of various simple peephole optimizations that can be made in the emitted bytecode before writing the .class file. I actually am aware of some libraries that have this functionality, but I'd like to implement that myself.\n\n========================================\n\nComments:\n- Most of the optimization are not done in the Java compiler but in the runtime. Is your target performance results or performance result without runtime optimizations?\n- The real target would be compiler learning. I guess that makes optimizations in compile-time more interesting to me, so I can see what's going on and implement it myself, rather than leaving it to another software.\n- IS there a .NET version perhaps?\n- Merge classes and interfaces: How does the compiler know that it can collapse a type hierarchy? It can only be sure about private interfaces.\n- In Java, compilation \"really\" happens at runtime: this is what the JIT does inside the JVM. The javac compiler is just a source to bytecode translator, and is required to emit certain bytecode for given source. So Proguard does part of what JIT does, not what javac does, in the Java ecosystem.\n- Do any of the standard Java tools, seven years on, do any of these optimizations?\n- Yes, the JIT. It always did, and is constantly being optimized.\n- @JesseTG Hey there, I also have the similar confusion. So my goal is to find a way to optimize the Java bytecode **before** runtime. I am aware that JIT can do lots of runtime optimization, but I want to optimize bytecode before getting it executed in JIT. Do you happen to know a good tool that fits this demand?\n- @JesseTG, right now what I am looking at is to first convert bytecode to LLVM IR, then leverage optimization passes there. But this does not work out well; one thing is that this translation itself is hard, and I somehow find that optimization passes in LLVM do not take Java/JVM-language into consideration and therefore the optimization chance is slim.","metadata":{"transformedAt":"2026-08-18T18:32:17.777Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":562}}244{"id":"stack-18357181","source":"stackoverflow","questionId":18357181,"title":"How can Lisp be both dynamic and compiled?","tags":["compiler-construction","lisp","dynamic-typing","static-typing"],"text":"Title: How can Lisp be both dynamic and compiled?\nTags: compiler-construction, lisp, dynamic-typing, static-typing\nSource: Stack Overflow\n\nQuestion:\nOkay, so first to get this out of the way: I have read the following answer:\n\nHow is Lisp dynamic and compiled?\n\nbut I don't really understand its answer.\n\nIn a language like Python, the expression:\n\n```\nx = a + b\n```\n\nCannot really be compiled, as for the \"compiler\" it would be impossible to know the types of a and b (as the types are known only at run-time), and therefore how to add them. \n\nThis is what makes a language like Python impossible to compile without type declarations, correct? With declarations, the compiler knows that e.g. a and b are integers, and therefore knows how to add them, and translate that into native code.\n\nSo how does:\n\n```\n(setq x 60)\n(setq y 40)\n(+ x y)\n```\n\nwork?\n\nCompiled being defined as *native ahead-of-time compilation*.\n\n**EDIT**\n\nIn reality, this question is more about whether dynamic languages without type declarations can be compiled, and if so, how?\n\n**EDIT 2**\n\nAfter much research (i.e. avid Wikipedia browsing) I think I understand the following:\n\n- dynamic typed languages are languages where the types are checked at run-time\n\n- static typed languages are languages where the types are checked when the program is compiled\n\n- type declarations allow the compiler to make the code more efficient because instead of making API calls all the time it can use more native 'functions' (that is why you can add type declarations to Cython code to speed it up, but don't have to, because it can still just call the Python libraries in the C code)\n\n- there are no datatypes in Lisp; therefore no types to be checked (the type is the data itself)\n\n- Obj-C has both static and dynamic declarations; the former are type-checked at compile time, the latter at run-time\n\nCorrect me if I am wrong on any of the above points.\n\n========================================\n\nTop Answer:\nCompilation is a simple translation from one language to another.\nIf you can express the same thing in language `A` and language `B`, you can compile this thing expressed in language `A` into the same thing in language `B`.\n\nOnce you have expressed your intent in some language, it is executed by being **interpreted**. Even when doing C, or some other *compiled* language, your statement is:\n\n- Translated from C -> Assembly language\n\n- Translated from Assembly -> machine code\n\n- Interpreted by the machine.\n\nA computer is actually an interpreter for a *very* basic language. Since it is so basic and so hard to work with, people came up with other languages which are easier to work with, and can be easily translated into equivalent statements in machine code (e.g. C). Then, you can hijack the compilation phase by performing the translation 'on-the-fly' as JIT compiler do, or by writing your own interpreter which executes directly statement in your high-level language (e.g. LISP or Python).\n\nBut note that interpreter are just a shortcut to execute your code directly ! If instead of executing the code, the interpreter printed whatever call it would be making, would it to execute the code, you would have... a compiler. Of course, that would be a very stupid compiler, and it would not make use of most of the information it has.\n\nActual compilers will try to gather as much information as they can from the *whole* program before generating the code. For instance, the following code:\n\n```\nconst bool dowork = false;\n\nint main() {\n if (dowork) {\n //... lots of code go there ... \n }\n return 0;\n}\n```\n\nWill in theory generate all the code inside the `if` branch. But a clever compiler will probably consider it unreachable and just ignore it, making use of the fact that it knows everything in the program and knows that `dowork` will always be `false`.\n\nIn addition to that, some language have *types*, which can help to dispatch function call, ensure some things at compile-time and help for the translation to machine code. Some languages like C *require* the programmer to declare the type of their variables. Others like LISP and Python just infer the type of the variable when it is set, and panic at runtime if you try to use a value of a certain type if another type is required (e.g. if you write `(car 2)` in most lisp interpreters, it will raise some error telling you a pair is expected). Types can be used to allocate the memory at compile time (e.g. a C compiler will allocate exactly `10 * sizeof(int)` bytes of memory if it is required to allocate a `int[10]`), but this is not exactly *required*. In fact, most C programs use pointers to store arrays, which are basically dynamic. When dealing with a pointer, a compiler will generate/link to code which, at runtime, will perform the necessary checks, reallocations, etc. But the bottom line is that dynamic and compiled are not to be opposed. The Python or Lisp interpreters are compiled programs, but still can act on dynamic values. In fact, assembly language itself is not really typed, as the computer can perform any operation on any object, since all it 'sees' are streams of bits, and operations on bits. Higher level languages introduce arbitrary types and limits to make things more readable and prevent you from doing completely crazy things. But this is just to *help you*, not an absolute requirement.\n\nNow that the philosophical rant is over, let's look at your example:\n\n```\n(setq x 60)\n(setq y 40)\n(+ x y)\n```\n\nAnd let's try to compile that to a valid C program. Once that is done, C compilers abound, so we can translate LISP -> C -> machine language, or pretty much anything else. Keep in mind that compilation is only translation (optimisations are cool too, but optional).\n\n```\n(setq\n```\n\nThis allocate a value. But we don't know what is allocated to what. Let's continue\n\n```\n(setq x 60)\n```\n\nOk, we're allocating 60 to x. 60 is an integer literal, so its C type is `int`. Since there is no reason to assume `x` is of another type, this is equivalent to the C:\n\n```\nint x = 60;\n```\n\nSimilarly for `(setq y 40)`:\n\n```\nint y = 40;\n```\n\nNow we have:\n\n```\n(+ x y)\n```\n\n`+` is a function which, depending on implementations, can take several types of arguments, but we know that `x` and `y` are integers. Our compilers knows that there exist an equivalent C statement, which is:\n\n```\nx + y;\n```\n\nSo we just translate it. Our final C program:\n\n```\nint x = 60;\nint y = 40;\nx + y;\n```\n\nWhich is a perfectly valid C program. It can get more tricky than this. For instance, if `x` and `y` are very big, most LISP won't let them overflow while C will, so you might code your compiler to have its own integer type as array of ints (or whatever you find relevant). If you are able to define common operations (like `+`) on these types, your new compiler will maybe translate the previous code into this instead:\n\n```\nint* x = newbigint(\"60\");\nint* y = newbigint(\"40\");\naddbigints(x, y);\n```\n\nWith your functions `newbigint` and `addbigints` defined elsewhere, or generated by the compiler. It will still be valid C, so it will compile. In fact, your own interpreter is probably implemented in some lower-level language and already has representations for LISP objects in its own implementation, so it can use these directly.\n\nBy the way, that is exactly what the Cython compiler does for Python code :)\n\nYou can define types statically in Cython to get some extra speed/optimisations, but it is not required. Cython can translate your Python code directly into C, and then into machine code.\n\nI hope that it makes it clearer ! Remember:\n\n- ALL code is interpreted, eventually\n\n- Compilers **translate** code into something which is easier/quicker to interpret. They often perform optimisations along the way, but this is not part of the definition\n\n========================================\n\nCode:\n```text\nx = a + b\n```\n\n```text\n(setq x 60)\n(setq y 40)\n(+ x y)\n```\n\n```text\n(setq x 60)\n(setq y 40)\n(+ x y)\n```\n\n```text\n(defun foo-opt (x y)\n  (declare (optimize (speed 3) (safety 0) (debug 0) (fixnum-safety 0))\n           (inline +))\n  (declare (fixnum x y))\n  (the fixnum (+ x y)))\n```\n\n```text\n0:      4157             push  r15\n       2:      55               push  rbp\n       3:      4889E5           moveq rbp, rsp\n       6:      4989DF           moveq r15, rbx\n       9:      4803FE           addq  rdi, rsi\n      12:      B901000000       move  ecx, 1\n      17:      4889EC           moveq rsp, rbp\n      20:      5D               pop   rbp\n      21:      415F             pop   r15\n      23:      C3               ret   \n      24:      90               nop   \n      25:      90               nop   \n      26:      90               nop   \n      27:      90               nop\n```\n\n```text\n0:      49396275         cmpq  [r10+75], rsp\n       4:      7741             ja    L2\n       6:      4883F902         cmpq  rcx, 2\n      10:      753B             jne   L2\n      12:      4157             push  r15\n      14:      55               push  rbp\n      15:      4889E5           moveq rbp, rsp\n      18:      4989DF           moveq r15, rbx\n      21:      4989F9           moveq r9, rdi\n      24:      4C0BCE           orq   r9, rsi\n      27:      41F6C107         testb r9b, 7\n      31:      7517             jne   L1\n      33:      4989F9           moveq r9, rdi\n      36:      4C03CE           addq  r9, rsi\n      39:      700F             jo    L1\n      41:      B901000000       move  ecx, 1\n      46:      4C89CF           moveq rdi, r9\n      49:      4889EC           moveq rsp, rbp\n      52:      5D               pop   rbp\n      53:      415F             pop   r15\n      55:      C3               ret   \nL1:   56:      4889EC           moveq rsp, rbp\n      59:      5D               pop   rbp\n      60:      415F             pop   r15\n      62:      498B9E070E0000   moveq rbx, [r14+E07]   ; SYSTEM::*%+$ANY-CODE\n      69:      FFE3             jmp   rbx\nL2:   71:      41FFA6E7020000   jmp   [r14+2E7]        ; SYSTEM::*%WRONG-NUMBER-OF-ARGUMENTS-STUB\n  ...\n```\n\n```text\n+\n```\n\n```text\n+\n```\n\n```text\n+\n```\n\n```text\n+\n```\n\n```text\nconst bool dowork = false;\n\nint main() {\n    if (dowork) {\n        //... lots of code go there ... \n    }\n    return 0;\n}\n```\n\n```text\n(setq x 60)\n(setq y 40)\n(+ x y)\n```\n\n```text\n(setq\n```\n\n```text\n(setq x 60)\n```\n\n```text\nint x = 60;\n```\n\n```text\nint y = 40;\n```\n\n```text\n(+ x y)\n```\n\n```text\nx + y;\n```\n\n```text\nint x = 60;\nint y = 40;\nx + y;\n```\n\n```text\nint* x = newbigint(\"60\");\nint* y = newbigint(\"40\");\naddbigints(x, y);\n```\n\n```text\nA\n```\n\n```text\nB\n```\n\n```text\nA\n```\n\n```text\nB\n```\n\n```text\nif\n```\n\n```text\ndowork\n```\n\n```text\nfalse\n```\n\n```text\n(car 2)\n```\n\n```text\n10 * sizeof(int)\n```\n\n```text\nint[10]\n```\n\n```text\nint\n```\n\n```text\nx\n```\n\n```text\n(setq y 40)\n```\n\n```text\n+\n```\n\n```text\nx\n```\n\n```text\ny\n```\n\n```text\nx\n```\n\n```text\ny\n```\n\n```text\n+\n```\n\n```text\nnewbigint\n```\n\n```text\naddbigints\n```\n\n========================================\n\nComments:\n- And how can Objective-C be dynamic and compiled? Well... dynamism vs. static nature and \"compiled-ness\" don't describe the same property. A language can be statically typed and compiled (like C), statically typed and interpreted (like C++ interpreted by Cling), dynamically typed and compiled (like Objective-C, Lisp, or JIT-ed JavaScript) and dynamically typed and interpreted (like Python, PHP, Lua, ...). They really have nothing to do with each other. The fact that static typing makes it easier for a compiler to catch errors and generate more efficient code is irrelevant.\n- As to \"how to add them\": polymorphism. The compiler generates code that does some kind of dynamic trickery based on the (run-time) types of `a` and `b`.\n- Then why does compiled Python need type annotations? And doesn't Obj-C have type annotations?\n- Objective-C doesn't have type annotations, but it has declarations, just like C. Does it really **need** them? Not sure. Objective-C objects can, at least, be queried for their class at runtime.\n- sorry, that's what I meant\n- but how does the compiler know the types of the variables, to be able to translate to the respective machine code? T\n- It doesn't know the types (at least not in Objective-C). Check out some papers on polymorphism.\n- Lisp by default has no data types for variables, thus it can't check them. The data itself has the type information.\n- The statement \"there are no datatypes in Lisp\" is wrong. There are datatypes, but they are not a property of variables but of values (which might be referred to by a variable). For example, if you `(let ((n 8)))`, then `n` does not have a type, but the value `8`, which `n` happens to be bound to in the scope of this `let`, is of type `integer`.\n- @Aristides The compiler doesn't need to know at compile time. It might require some type before computing though. You might be interested in 90 minute Scheme to C compiler talk which comes with code. It's not incremental and not so sophisticated, but pretty impressive by Marc Feeley. (author of Gambit)\n- @Aristides Matt Might has a compiler too that has more types.\n- TL;DR: Paragraph #4 is the essence.\n- 'answered 18mins ago' ? Damn, I'm a slow typist :) Very nice explanation !\n- Since I didn't see links in the answer note that in Common Lisp, you can compile code with `compile` and investigate the result with `disassemble`.\n- (Note: Most C'ish languages, including C++ and C# (the ones that i know of that define both `bool` and `const`), reserve `do` as a keyword. The `int main()` example probably won't compile. Doesn't make the point any less valid, though.)","metadata":{"transformedAt":"2026-08-18T18:32:17.778Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":41,"totalLines":406,"estimatedTokens":3396}}245{"id":"stack-3032874","source":"stackoverflow","questionId":3032874,"title":"How does compiling circular dependencies work?","tags":["compiler-construction","circular-dependency"],"text":"Title: How does compiling circular dependencies work?\nTags: compiler-construction, circular-dependency\nSource: Stack Overflow\n\nQuestion:\nI've made the example in Java but I think (not tested) that it works in other (all?) languages.\n\nYou have 2 files. First, `M.java`:\n\n```\npublic class MType {\n XType x;\n MType() {x = null;}\n}\n```\n\nSecond, another file (in the same directory), `XType.java`:\n\n```\npublic class XType {\n MType m;\n public XType(MType m) {this.m = m;}\n}\n```\n\nOk it's bad programming, but if you run `javac XType` it compiles: compiles even `MType` because `XType` needs it. But ... `MType` needs `XType` ... how does that work? How does the compiler know what is happening?\n\nI would like to know how the compiler (javac or any other compilers you know) manages that situation, not how to avoid it.\n\nI'm asking because I'm writing a precompiler and I would like to manage that situation.\n\n========================================\n\nTop Answer:\nThe first file doesn't need to know anything about XType except that it is a type, and similarly for MType in the second file. Also, in Java, all objects are effectively the same size (because everything is accessed through references), so the size of the object is not needed. This is not so in other languages - your code as it stands would not compile in C++, for example (language syntax apart).\n\n========================================\n\nCode:\n```text\npublic class MType {\n    XType x;\n    MType() {x = null;}\n}\n```\n\n```text\npublic class XType {\n   MType m;\n   public XType(MType m) {this.m = m;}\n}\n```\n\n```text\nM.java\n```\n\n```text\nXType.java\n```\n\n```text\njavac XType\n```\n\n```text\nMType\n```\n\n```text\nXType\n```\n\n```text\nMType\n```\n\n```text\nXType\n```\n\n```text\npublic class Example {  \npublic static void main(String [] args) {\n    assert(x==0);           \n    x++;\n    assert(x==1);\n}\nstatic int x=0;\n}\n```\n\n========================================\n\nComments:\n- So when it compiles the second file the compiler already knows what the first is?\n- @Fabio - Each pass of the compiler processes all the files gathering the information needed for the next pass.\n- but if use a method of XType? It must know methods of Xtype.. no?\n- also, I'm not sure what you mean by \"all objects are effectively the same size\".\n- He is referring to the fact that (for example) `m` holds a reference, and all references have the same size. But that's not the only information that is required about a type. It is also necessary to know its members and lots of information about its super-type hierarchy.","metadata":{"transformedAt":"2026-08-18T18:32:17.778Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":100,"estimatedTokens":635}}246{"id":"stack-3894598","source":"stackoverflow","questionId":3894598,"title":"How is LINQ compiled into the CIL?","tags":["c#","asp.net","linq","compiler-construction","cil"],"text":"Title: How is LINQ compiled into the CIL?\nTags: c#, asp.net, linq, compiler-construction, cil\nSource: Stack Overflow\n\nQuestion:\nFor example:\n\n```\nvar query = from c in db.Cars select c;\nforeach(Car aCar in query)\n{\n Console.WriteLine(aCar.Name);\n}\n```\n\nHow would this translate once it is compiled? What happens behind the scenes?\n\n========================================\n\nTop Answer:\nYou should compile it and run `ildasm` against the resulting executable to find out.\n\n========================================\n\nCode:\n```text\nvar query = from c in db.Cars select c;\nforeach(Car aCar in query)\n{\n     Console.WriteLine(aCar.Name);\n}\n```\n\n```text\npublic static void Main()\n{\n    var query = db.Cars.Select<Car, Car>(c => c);\n    foreach (Car aCar in query)\n    {\n         Console.WriteLine(aCar.Name);\n    }\n}\n```\n\n```text\nprivate Car lambda0(Car c)\n{\n    return c;\n}\nprivate Func<Car, Car> CachedAnonymousMethodDelegate1;\npublic static void Main()\n{\n    if (CachedAnonymousMethodDelegate1 == null)\n        CachedAnonymousMethodDelegate1 = new Func<Car, Car>(lambda0);\n    var query = db.Cars.Select<Car, Car>(CachedAnonymousMethodDelegate1);\n    foreach // ...\n}\n```\n\n```text\npublic static void Main()\n{\n    var parameter = Expression.Parameter(typeof(Car), \"c\");\n    var lambda = Expression.Lambda<Func<Car, Car>>(parameter, new ParameterExpression[] { parameter }));\n    var query = db.Cars.Select<Car, Car>(lambda);\n    foreach // ...\n}\n```\n\n```text\nIEnumerator<Car> enumerator = null;\ntry\n{\n    enumerator = query.GetEnumerator();\n    Car aCar;\n    while (enumerator.MoveNext())\n    {\n        aCar = enumerator.Current;\n        Console.WriteLine(aCar.Name);\n    }\n}\nfinally\n{\n    if (enumerator != null)\n        ((IDisposable)enumerator).Dispose();\n}\n```\n\n```text\n// Put db.Cars on the stack\nL_0016: ldloc.0 \nL_0017: callvirt instance !0 DatabaseContext::get_Cars()\n\n\n// “if” starts here\nL_001c: ldsfld Func<Car, Car> Program::CachedAnonymousMethodDelegate1\nL_0021: brtrue.s L_0034\nL_0023: ldnull \nL_0024: ldftn Car Program::lambda0(Car)\nL_002a: newobj instance void Func<Car, Car>::.ctor(object, native int)\nL_002f: stsfld Func<Car, Car> Program::CachedAnonymousMethodDelegate1\n\n\n// Put the delegate for “c => c” on the stack\nL_0034: ldsfld Func<Car, Car> Program::CachedAnonymousMethodDelegate1\n\n\n// Call to Enumerable.Select()\nL_0039: call IEnumerable<!!1> Enumerable::Select<Car, Car>(IEnumerable<!!0>, Func<!!0, !!1>)\nL_003e: stloc.1\n\n\n// “try” block starts here\nL_003f: ldloc.1 \nL_0040: callvirt instance IEnumerator<!0> IEnumerable<Car>::GetEnumerator()\nL_0045: stloc.3\n\n\n// “while” inside try block starts here\nL_0046: br.s L_005a\nL_0048: ldloc.3   // body of while starts here\nL_0049: callvirt instance !0 IEnumerator<Car>::get_Current()\nL_004e: stloc.2 \nL_004f: ldloc.2 \nL_0050: ldfld string Car::Name\nL_0055: call void Console::WriteLine(string)\nL_005a: ldloc.3   // while condition starts here\nL_005b: callvirt instance bool IEnumerator::MoveNext()\nL_0060: brtrue.s L_0048  // end of while\nL_0062: leave.s L_006e   // end of try\n\n\n// “finally” block starts here\nL_0064: ldloc.3 \nL_0065: brfalse.s L_006d\nL_0067: ldloc.3 \nL_0068: callvirt instance void IDisposable::Dispose()\nL_006d: endfinally\n```\n\n```text\n// typeof(Car)\nL_0021: ldtoken Car\nL_0026: call Type Type::GetTypeFromHandle(RuntimeTypeHandle)\n\n\n// Expression.Parameter(typeof(Car), \"c\")\nL_002b: ldstr \"c\"\nL_0030: call ParameterExpression Expression::Parameter(Type, string)\nL_0035: stloc.3 \n\n\n// Expression.Lambda(...)\nL_0036: ldloc.3 \nL_0037: ldc.i4.1           // var paramArray = new ParameterExpression[1]\nL_0038: newarr ParameterExpression\nL_003d: stloc.s paramArray\nL_003f: ldloc.s paramArray\nL_0041: ldc.i4.0                    // paramArray[0] = parameter;\nL_0042: ldloc.3 \nL_0043: stelem.ref \nL_0044: ldloc.s paramArray\nL_0046: call Expression<!!0> Expression::Lambda<Func<Car, Car>>(Expression, ParameterExpression[])\n\n\n// var query = Queryable.Select(...);\nL_004b: call IQueryable<!!1> Queryable::Select<Car, Car>(IQueryable<!!0>, Expression<Func<!!0, !!1>>)\nL_0050: stloc.1\n```\n\n```text\ndb.Cars\n```\n\n```text\nIEnumerable<Car>\n```\n\n```text\nlambda0\n```\n\n```text\n<Main>b__0\n```\n\n```text\nMain\n```\n\n```text\nCS$<>9__CachedAnonymousMethodDelegate1\n```\n\n```text\ndb.Cars\n```\n\n```text\nIQueryable<Car>\n```\n\n```text\nforeach\n```\n\n```text\ntry/finally\n```\n\n```text\nIEnumerable<Car>\n```\n\n```text\nIQueryable<Car>\n```\n\n```text\nildasm\n```\n\n========================================\n\nComments:\n- I assume that this is a LINQ-to-SQL query, rather than just a filter on a collection? The former will be doing a lot more work behind the scenes than the latter, obviously.\n- Actually, let's go for a LINQ-to-Objects filter on a collection.\n- I have done this, but I was hoping for a fuller and more detailed explanation. :)\n- or use reflector if you want something more userfriendly.\n- very good answer! I think step step 2 is the most important one here. For completeness could you add info about where the linq is not in process (like linq2sql perhaps) please?\n- @Preet: Not sure what you mean, but I added a note about LINQ to SQL.\n- @Preet: I added the complete compilation for LINQ-to-SQL now. :)\n- great explanation, but i think you should add description of that Select and other LINQ is implemented via `foreach ... yield return` thus it is not storing collections in memory\n- @Andrey: I don’t understand. Nothing in this posting makes any such claim as to what is stored in memory and what isn’t. This posting answers the question, which is *how is the query expression compiled into IL.*\n- @Timwi ok, i am just telling how according to my opinion you can make this answer perfect. If you disagree it is up to you :)\n- @Timwi, good answer! I have one comment though. In part 1, you write that it's converted into Select(c => c). Are you sure it does type inference there? I thought the first step is to only do a \"dumb\" translation and let the compiler do all the type inference in step 2. So in step 1, it would be the select without the types..\n- @Anton: You are right that the type inference is separate from the conversion of LINQ syntax to method syntax. The latter is indeed a “dumb” translation. However, it happens before the stuff that I labeled “step 2”. I just conflated them into one “step”.","metadata":{"transformedAt":"2026-08-18T18:32:17.778Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":20,"totalLines":237,"estimatedTokens":1569}}247{"id":"stack-2331584","source":"stackoverflow","questionId":2331584,"title":"Global variable implementation","tags":["c","variables","compiler-construction","global-variables"],"text":"Title: Global variable implementation\nTags: c, variables, compiler-construction, global-variables\nSource: Stack Overflow\n\nQuestion:\nWhen I write the following program:\n\nfile 1:\n\n```\n#include \nint global; \nvoid print_global1() {\n printf(\"%p\\n\", &global);\n}\n```\n\nfile 2:\n\n```\n#include \nchar global; \nvoid print_global2() {\n printf(\"%p\\n\", &global);\n}\n```\n\nfile 3:\n\n```\nvoid print_global1();\nvoid print_global2();\nint main()\n{\n print_global1();\n print_global2();\n\n return 0;\n}\n```\n\noutput:\n\n```\n$ ./a.out\n0x804a01c\n0x804a01c\n```\n\nHere is my question:\n\n- Why are the linker implementing \"int global\" and \"char global\" as the same global variable:\n\n- How come the compiler does not complain (not the smallest warning with `-Wall -Wextra -ansi` ...)\n\n- How are the size of the global variable managed (the size of int and char are different)\n\nPS: The second question is architecture/compiler related, so lets take the gcc or Visual C++ (for C) with the int size as 32 bits\n\nEDIT: THIS IS NOT A QUESTION FOR C++ BUT for C!\n\nI use gcc version 4.4.1 and on Ubuntu 9.10, Here is the compilation console output:\n\n```\n$ ls\nglobal_data1.c global_data2.c global_data.c\n\n$ gcc -Wall -Wextra -ansi global_data*.c\n$ ./a.out\n0x804a01c\n0x804a01c\nor \n$ gcc -Wall -Wextra -ansi -c global_data*.c\n$ gcc -Wall -Wextra -ansi global_data*.o\n$ ./a.out\n0x804a01c\n0x804a01c\n```\n\n========================================\n\nTop Answer:\nThis has to do with something called \"tentative definition\" in C. First, if you assign to `global` in both file1 and file2, you will get an error in C. This is because `global` is not tentatively defined in file1 and file2 anymore, it is really defined.\n\nFrom the C standard (emphasis mine):\n\n A declaration of an identifier for an object that has file scope *without an initializer*, and without a storage-class specifier or with the storage-class specifier static, constitutes a *tentative definition*. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.\n\nFor your case, \"translation unit\" (basically) each source file.\n\nAbout \"composite types\":\n\n For an identifier with internal or external linkage declared in a scope in which a prior\n declaration of that identifier is visible, if the prior declaration specifies internal or\n external linkage, the type of the identifier at the later declaration becomes the composite\n type.\n\nFor more on tentative definitions, see this question and its answers.\n\nIt seems like for your case, it should be undefined behavior because `global` is defined at the end of the translation units, so you get two definitions of `global`, and what's worse, they are different. Looks like the linker by default doesn't complain about this though.\n\nGNU ld has an option called `--warn-common`, which warns you for multiple tentative definitions (common symbol is linker's name for tentatively defined variables):\n\n```\n$ gcc -Wl,--warn-common file*.c\n/tmp/ccjuPGcq.o: warning: common of `global' overridden by larger common\n/tmp/ccw6nFHi.o: warning: larger common is here\n```\n\nFrom the manual:\n\n If there are only (one or more) common symbols for a variable, it goes in the uninitialized data area of the output file. The linker merges multiple common symbols for the same variable into a single symbol. If they are of different sizes, it picks the largest size. The linker turns a common symbol into a declaration, if there is a definition of the same variable.\n\n \n The `--warn-common` option can produce five kinds of warnings. Each warning consists of a pair of lines: the first describes the symbol just encountered, and the second describes the previous symbol encountered with the same name. One or both of the two symbols will be a common symbol.\n\n========================================\n\nCode:\n```text\n#include <stdio.h>    \nint global;    \nvoid print_global1() {\n        printf(\"%p\\n\", &global);\n}\n```\n\n```text\n#include <stdio.h>\nchar global;    \nvoid print_global2() {\n        printf(\"%p\\n\", &global);\n}\n```\n\n```text\nvoid print_global1();\nvoid print_global2();\nint main()\n{\n        print_global1();\n        print_global2();\n\n        return 0;\n}\n```\n\n```text\n$ ./a.out\n0x804a01c\n0x804a01c\n```\n\n```text\n$ ls\nglobal_data1.c  global_data2.c  global_data.c\n\n$ gcc -Wall -Wextra -ansi global_data*.c\n$ ./a.out\n0x804a01c\n0x804a01c\nor \n$ gcc -Wall -Wextra -ansi -c global_data*.c\n$ gcc -Wall -Wextra -ansi global_data*.o\n$ ./a.out\n0x804a01c\n0x804a01c\n```\n\n```text\n-Wall -Wextra -ansi\n```\n\n```text\nchar global = 1; // no more tentative...but explicit.\n```\n\n```text\nint global = 1; // in file1\n\nchar global = 1; // in file2\n```\n\n```text\ngcc\n```\n\n```text\ng++\n```\n\n```text\n/tmp/cc8Gnf4h.o:(.bss+0x0): multiple definition of `global'\n/tmp/ccDQHZn2.o:(.bss+0x0): first defined here\n/usr/bin/ld: Warning: size of symbol `global' changed from 4 in a.o to 1 in b.o\n```\n\n```text\n$ gcc -Wl,--warn-common file*.c\n/tmp/ccjuPGcq.o: warning: common of `global' overridden by larger common\n/tmp/ccw6nFHi.o: warning: larger common is here\n```\n\n```text\nglobal\n```\n\n```text\nglobal\n```\n\n```text\nglobal\n```\n\n```text\nglobal\n```\n\n```text\n--warn-common\n```\n\n```text\n--warn-common\n```\n\n========================================\n\nComments:\n- +1 Is there a way tool which will allow me to know the size of the variable \"global\" ?.\n- I am not doing C++ but C program (thats why i didn't put the C++ flag in my answer)! It looks like the C standard specification allows this behavior (but I am not really sure). I can still use the C++ linker to check if there is multiple definition but I am not really sure if it is really safe...\n- @Phong: you are right..the C std allows this. I've updated my answer.\n- @codaddict: +1, Thanks for the quick update! I now better understand how it is handle by the compiler.\n- It is not so much C that allows tentative definitions as it is recognized as a common extension in Appendix J of the C99 standard. See also: stackoverflow.com/questions/1987413/&hellip;\n- @Jonathan: tentative definitions have always been a part of the ANSI C standard. Section 3.7.2 in the C89 standard describes them. See groups.google.com/group/comp.lang.c/msg/47ae65fdb11e7111 for a very good description.\n- @Alok: you're correct - what I was intending to refer to was the 'common extension' in J.5.11 'Multiple external definitions' where it says \"There may be more than one external definition for the identifier of an object, with or without the explicit use of the keyword extern; if the definitions disagree, or more than one is initialized, the behavior is undefined\".\n- @alok: +1 Thanks for teaching me the -Wl,--warn-common gcc option.","metadata":{"transformedAt":"2026-08-18T18:32:17.778Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":18,"totalLines":238,"estimatedTokens":1720}}248{"id":"stack-20273434","source":"stackoverflow","questionId":20273434,"title":"Preventing StackOverflow in language interpreters","tags":[".net","compiler-construction","f#","stack-overflow","tail-recursion"],"text":"Title: Preventing StackOverflow in language interpreters\nTags: .net, compiler-construction, f#, stack-overflow, tail-recursion\nSource: Stack Overflow\n\nQuestion:\nF# as a language is great for writing language interpreters or compilers, however, one thing keeps hitting us where we don't want it: the StackOverflowException.\n\nIt's well known that an SO-exception cannot be caught and cannot be recovered from. An obvious technique for preventing such an exception is by counting the depth of the stack as you go along. Overhead, yes, but doable and perhaps not necessary in every function.\n\nWith F#, this technique doesn't bring much benefit though. We make a lot of use of tail-call optimization techniques in the on-the-fly generated expressions of the interpreter. The problems we face with SO-exceptions are:\n\n- how can we inform the user of them, instead of crashing the whole current AppDomain?\n\n- if we go for counting the stack-depth, how do we know whether a function is TCO'ed or inlined so we don't have to count up?\n\n- if we go for another approach (like inspecting the stack itself at given depth-intervals), is there any (known) way to do this without seriously impeding performance?\n\nJust increasing the stack-size is not going to help enough, we want to give the user a loggable error, preferably catchable by the calling application. For that we need to be able to hand-throw the exception, which makes it catchable. But how do we determine the right moment?\n\n**Update**:\n\nHans Passant correctly suggests predictability here. However, the programmers using this DSL expect that (certain) calls get TCO'ed, hence they don't want a strong stack-limit. They know what they are doing. Still, their programs need to be able to die gracefully, at least to the extend that any calling application (i.e., a C# program using our libraries) \nis not harmed.\n\n========================================\n\nCode:\n```text\nprivate static int TopOfStack;\nprivate const int STACK_SIZE = 1000000;\n```\n\n```text\nstatic Interpreter() {\n    InitializeGlobalEnvironment();\n\n    //---------------------------------------------------\n    // Get the address of a new variable allocated on the stack \n    // to represent the amount of memory available. Record \n    // the address.\n    //---------------------------------------------------\n    int stackVariable;\n    TopOfStack = (int)&stackVariable;\n}\n```\n\n```text\ninternal static ExecutionContext EnterFunctionContext(IValue thisArg, LIST args, FUNCTION function) {\n...\n    LexicalEnvironment localEnv = ECMA.NewDeclarativeEnvironment(function.Scope);\n\n    ExecutionContext context = new ExecutionContext() {\n        Strict = function.IsStrict,\n        VariableEnvironment = localEnv,\n        LexicalEnvironment = localEnv\n    };\n\n    int remainingStackSpace;\n\n    if (STACK_SIZE - (TopOfStack - (int)&remainingStackSpace) < short.MaxValue) \n            throw new ECMARuntimeException(\"stack overflow\", RuntimeErrorType.RangeError);\n\n\n    CallStack.Push(context);\n    LexicalEnvironment env = CurrentContext.VariableEnvironment;\n...\n}\n```\n\n```text\nRecursiveCall(0);\n\nfunction RecursiveCall(counter){\n    return RecursiveCall(++counter);\n}\n```\n\n```text\nRangeError: stack overflow\n```\n\n```text\nThread(ParameterizedThreadStart, Int32)\n```\n\n========================================\n\nComments:\n- @HansPassant, thanks for jumping in. It has crossed our minds to solve it that way, but it feels so much like solving a small problem with a sledge hammer that we haven't even attempted it yet. Furthermore, our compiler should be callable as a library from within a host language and spawning a process each time a small snippet is compiled is really too much overhead.\n- Oh, you updated your comment. The problem here is the predictability. The DSL may contain many functions that will get TCO'ed, setting a hard limit (which we currently have) appears to be too rigid, because we cannot count towards that limit in any conceivable way.\n- @HansPassant: our program translates another language into compiled expressions and then runs that against variable input. To complex things further, the language definition contains an evaluate-function. Increasing stack-size only works to a certain level, I don't know why, but after a certain threshold it brings the program to snail-speed. For one, our program will be called from an IDE (not ours), bringing the IDE down because we have an SO would be disastrous. There used to be a way around with AppDomains, but I believe that is not possible any longer.\n- @HansPassant: just some things that I've come across: CER (constrained execution region) and `RuntimeHelpers.ProbeForSufficientStack`. Not sure it will solve our problems and not sure how I could use CER in my own application.\n- Why don't you just rewrite your interpreter so that it doesn't leak stack space?\n- We are implementing an open standard, fairly complex, functional language that allows a lot of freedom of expressions. We cannot rewrite every conceivable expression that the result only consists of continuations etc. We already prevent a certain depth of recursion in the DSL, and maybe that's all we can do, but the way F# optimizes things, we shouldn't need to be too rigid about this. Also, from a security standpoint, we should not allow SO's to tear down everything, so we either need a foolproof way to ensure stackdepth, or catch it (I'm currently experimenting with hosting the CLR).\n- \"We cannot rewrite every conceivable expression that the result only consists of continuations\". I don't understand why that is not possible. Can you post some code or a concrete example to highlight why that is not possible?\n- Consider implementing a stackless interpreter or compiler. CPS transform might help.","metadata":{"transformedAt":"2026-08-18T18:32:17.778Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":96,"estimatedTokens":1438}}249{"id":"stack-6281835","source":"stackoverflow","questionId":6281835,"title":"How Do C++ Compilers Merge Identical String Literals","tags":["c++","visual-c++","compiler-construction","linker"],"text":"Title: How Do C++ Compilers Merge Identical String Literals\nTags: c++, visual-c++, compiler-construction, linker\nSource: Stack Overflow\n\nQuestion:\nHow does compiler (MS Visual C++ 2010) combine identical string literals in different cpp source files? For example, if I have the string literal \"hello world\\n\" in src1.cpp and src2.cpp respectively. The compiled exe file will have only 1 \"hello world\" string literal probably in the constant/readonly section. Is this task done by the linker?\n\nWhat I hope to achieve is that I got some modules written in assembly to be used by C++ modules. And these assembly modules contain many long string literal definitions. I know the string literals are identical to some other string literals in the C++ source. If I link my assembly generated obj code with the compiler generated obj code, would these string literals be merged by the linker to remove redundant strings as is the case when all modules are in C++?\n\n========================================\n\nTop Answer:\nYes, the process of merging the resources is done by the linker.\n\nIf your resources in your compiled assembly code are properly tagged as resources, the linker will be able to merge them with compiled C code.\n\n========================================\n\nCode:\n```text\nchar* another_string = \"this is a string\";\n```\n\n```text\nSECTION HEADER #3\n  .rdata name\n       0 physical address\n       0 virtual address\n      11 size of raw data\n     147 file pointer to raw data (00000147 to 00000157)\n       0 file pointer to relocation table\n       0 file pointer to line numbers\n       0 number of relocations\n       0 number of line numbers\n40301040 flags\n         Initialized Data\n         COMDAT; sym= \"`string'\" (??_C@_0BB@LFDAHJNG@this?5is?5a?5string?$AA@)\n         4 byte align\n         Read Only\n\nRAW DATA #3\n  00000000: 74 68 69 73 20 69 73 20 61 20 73 74 72 69 6E 67  this is a string\n  00000010: 00\n```\n\n```text\n/GF\n```\n\n```text\n/GF\n```\n\n```text\n/GF\n```\n\n```text\n/GF\n```\n\n```text\nanother_string1\n```\n\n```text\nml.exe\n```\n\n```text\nml.exe\n```\n\n```text\n// s.c\n#include <stdio.h>\n\nvoid f();\n\nint main() {\n    printf( \"%p\\n\", \"foo\" );\n    printf( \"%p\\n\", \"foo\" );\n    f();\n}\n\n// s2.c\n#include <stdio.h>\n\nvoid f() {\n    printf( \"%p\\n\", \"foo\" );\n    printf( \"%p\\n\", \"foo\" );\n}\n```\n\n```text\ngcc s.c s2.c\n```\n\n```text\n00403024\n00403024\n0040302C\n0040302C\n```\n\n```text\n.model flat, c\n\n.data\n    string1 db \"This is the first string\", 10, 0\n    string2 db \"This is the second string\\n\", 10, 0\n```\n\n```text\nextern char string1[];\nextern char string2[];\n```\n\n```text\n#include \"foo.h\"\n\nvoid baz() { std:::cout << string1; }\n```\n\n```text\n#include\n```\n\n========================================\n\nComments:\n- If your program is dependent on identical string literals occupying the same memory, you should redesign the program.\n- @MarkRansom: he never said his code is dependent on that; he's asking for what the expected behaviour is, which is entirely reasonable.\n- When in doubt, you can always perform the \"merging\" or \"combining\" yourself by defining string literal once and referring to it by name in other parts of the program.\n- @Thomas Easier said than done.\n- @Paul Sonier, the expected behavior is that the compiler and linker can do whatever they want so long as valid code produces the expected output. It is a mistake to rely on unspecified behavior, even if it seems consistent, because compiler versions and option switches might change everything. I didn't say the question wasn't worth asking, just providing a warning.\n- @Neil, it's done all the time for the purposes of language translation.\n- @Mark Normally using things like Windows string resources, which are not the same kind of thing as C++ string literals.\n- But @Mark, the behavior in this case *is* specified, according to the `&#47;GF` compiler switch. This question is asking what, if anything, needs to be done in an assembler file to ensure that \"string literals\" in the assembler file are compatible with string literals from other files so that Visual C++ can consider all of them for merging.\n- The parsing portion of the compiler can merge resources within the same translation units; no need to wait for the linker.\n- @Thomas, the parsing portion of the compiler is not involved in detecting literals defined in assembler code.\n- The compiler not the linker. I can see only one occur in one obj file. When linking all object file to a so or dll or dylib, string literal in different object file not merged.\n- Also note that `&#47;GF` puts each string into a COMDAT in a read-only section. If the linker has identical COMDAT folding (IDF) enabled, then duplicate strings will be consolidated. Note that IDF is based on the actual contents of the COMDAT as well as the attributes of the sections they're in. So, for example, if the assembly string is not in a read-only section, the linker won't consolidate it with ones generated by the compiler. Though the linker might use the name of the COMDAT as a first cut, the actual contents must be identical as well.","metadata":{"transformedAt":"2026-08-18T18:32:17.778Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":143,"estimatedTokens":1259}}250{"id":"stack-5074145","source":"stackoverflow","questionId":5074145,"title":"Is JIT compiler a Compiler or Interpreter?","tags":["c#",".net","compiler-construction","interpreter","jit"],"text":"Title: Is JIT compiler a Compiler or Interpreter?\nTags: c#, .net, compiler-construction, interpreter, jit\nSource: Stack Overflow\n\nQuestion:\nMy question is whether JIT compiler which converts the IL to Machine language is exactly a compiler or an interpreter.\n\nOne more question :\nIs HTML, JavaScript a compiled language or interpreted language?\n\nThanks in Advance\n\n========================================\n\nTop Answer:\nTechnically, a compiler translates from one language to another language. Since a JIT compiler receives an IL as its input and outputs native machine binary, it easily fits this criteria and should be called a compiler.\n\nRegarding Javascript, making a distinction here is more difficult. If you want to be pedantic, there's no such thing as a \"compiled language\" or \"interpreted language\". I mean, it's true that in practice most languages have one common way of running them and if that is an interpreter they are usually called interpreted languages, but interpretation or compilation are (usually) not traits of the language itself. Python is almost universally considered interpreted, but it's possible to write a compiler which compiles it to native binary code; does it still deserve the \"interpreted\" adjective?\n\nNow to get to the actual answer: Javascript is *typically* ran by an interpreter which, among other things, uses a JIT compiler itself. Is that interpreted or compiled, then? Your call.\n\n========================================\n\nCode:\n```text\njust-in-time compiler(JIT)\n```\n\n```text\nJust-in-time compilation\n```\n\n```text\nJIT compiler\n```\n\n========================================\n\nComments:\n- There is no hard line between a \"compiled\" language and an \"interpreted\" language. At some point, all source code must be translated to machine code (unless you're writing assembly).\n- it runs in mixed mode for most of the part (like most JITs), compiling everything is unnecessary (your main method for instance is run once one, no point to compile it). Inner loops are prime candidate for compilation. Javascript can be both but nowadays is mostly compiled.\n- Sorry for the nitpick, but in English.. a \"doubt\" means that you do not believe something is true, or you are not sure if it's true. What you are asking is a \"question\". It's proper to say that if you doubt something, you question it, but that doesn't mean that all questions are doubts.\n- @bestsss The .NET CLR does JIT every method. So does Mono. Also why do you assume Main is run once? Programs are allowed to call Main as many times as they please.\n- @MichaelGraczyk, JIT every method is waste and the code has to be put somewhere, the generated code needs memory [not the regular heap even as it needs the execution bit]. JIT w/o profiling creates crappy/non-optimal code and letting the interpreter grok the code will be faster too. While 'main' can be run as you'd like, it's just not the norm at any rate. Microbenchmarks w/ loops in the main method result in OCRs too. Even TieredCompilation in Java that JITs a lot more via C1 produces TONS of unnecessary compiled code easily increasing the footprint of a server by 100ish MBs.\n- @bestsss All the same, the .NET CLR does not do any interpreting (DLR aside.) It uses now and always has used JITed code for all methods.\n- Modern web browsers can JIT-compile Javascript. Also, they store rendered HTML in an in-memory cache, which is analogous to JIT for programs.\n- Interestingly, Chrome's V8 engine used to not be able to interpret javascript, and needed to compile it to machine code first (JIT). `In recent years, V8 has had an interpreter as its first execution tier.`\n- if the JIT does on the stack replacement I do consider it compilation, the code is not needed to be compiled 100% just the hot parts.\n- -1 \"I've come to think that there really isn't any 'right' anwser\". Compilers do partial specialization is the right answer.","metadata":{"transformedAt":"2026-08-18T18:32:17.778Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":49,"estimatedTokens":973}}251{"id":"stack-21737736","source":"stackoverflow","questionId":21737736,"title":"How \"Hello World\" works in C","tags":["c","compiler-construction"],"text":"Title: How \"Hello World\" works in C\nTags: c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am searching for a month for blog posts about \"how C program works\". Most of them go like\n\n- Compilers do these things\n\n- Linkers do these things\n\n- Program is put into memory; and stacks, heaps, etc.\n\nI thought I would first read about how compiler works to understand the flow of the program into the machine. Dragon Book seems to be universally preferred. But truthfully, it's too intensive. I am not good enough now to go through it all. \n\nSo I began to read about hardware. But There too, they explain about buses, I/O signals, structure of memory, writing cache-friendly code etc. But with no proper examples.\n\nBut still I could not find myself satisfied or being able to completely visualize the process.\n\n2 hours ago I decided to ask this question.(Since I am scared of It might be not useful to SO community, or off-topic question or other down-votable categories) and I did not find out any post relating to this exactly. There was one about \"how compiler does the compilation\", but the answers showed that it's too broad a question.\n\nMy question is this:\n\nI would like to know how, in depth, a C program works. If you cannot tell me explicitly, please redirect me to a book or another post on another website that can give me the answer to this.\n\nI am here until I get a response. If you have any suggestions regarding this post, tell me. And It is not my first language, so please take all my sentences as being soft and polite.\n\nThanks.\n\nUPDATE:\n\nAlong with the accepted answer, there are some very nice links as well as suggestions which give partial answers or the way to proceed further to understanding what I am trying to understand.\n\n========================================\n\nTop Answer:\nIt's too broad a question (as you have observed).\n\nIf you really want to understand from bottom up - buy an OLD computer from the 80's off ebay. Sinclair Spectrum/BBC it really doesn't matter but make sure you get plenty of books and manuals that go with it.\n\nYou will learn plenty because these machines were well documented and what wasn't documented was discovered and then documented :)\n\nThey are also sooooo much simpler than a modern quad core multi-gigabyte memory job. It will all fit inside your head easily\n\nor for a modern start, Arduino or Raspberry pie maybe.\n\n========================================\n\nComments:\n- It must be black magic!\n- Then the magicians here must be able to explain the trick to me :)\n- See this answer to a similar question.\n- This is a useful question and it's great that you're asking it. Too many beginners ignore how things work under the hood. BTW, \"Hello\" and \"World\" would not be tokens from the lexer's POV.\n- I changed it, is it okay now Ed?\n- @humble_fool: I didn't mean you should change it. You asked the question, I wouldn't expect you to already know the answer. :)\n- @BasileStarynkevitch Its related but it is not solving my problem. I want a explaination using example.\n- This borders on \"too broad\". I think you're basically asking how CPU instructions produce screen output; I think you seem to have a pretty good idea of how C is converted into those instructions, just not how they move through the system to produce something like characters on a screen?\n- The best way to get an answer is to *study* computer science. This takes several years (e.g. learning at university), after which you'll be able to write a book to answer your question.\n- @meagar I think he is asking how everything starting from the CPU, source code, compiler, linker, loader etc fit together and produce the effect they do.\n- It's like I know some bits from here and there. But unable to connect all that stuff. But it's not like I want to ask you what are the drawing algorithms or that stuff. But yea, I would like if you explain that \"it happens through this module in linux kernel which you can see here\"\n- One great link that explains how a program gets executed on a linux-based system is this\n- \"*I am unable to continue with programming like this*\" - Please don't treat this too seriously, **especially** if you've been learning CS/programming for just 2 years. I bet majority of programmers don't know half of the answer to this. It's good that you even want to know, it just happens that your question is quite complex. Don't see this as your failure just because it might take quite some time to learn and feel confident about understanding the whole process.\n- @BasileStarynkevitch Yeah, I am going through the process of learning. But If someone who has studied it and written a book , and he could show me how he/she visualizes it, I can atleast know what I am dealing with.\n- I don't think it's an unreasonable question to wonder how a simple application runs from start to finish and I do believe there is someone(not me >.>) on SO able to answer this question. I think the answer should be at least this detailed\n- @humble_fool: I believe I know enough to write such a book, but I also believe that not much people actually would be interested in it. So it is not worth the effort writing it. Also, you can find partial answers and mix them yourself. On Linux, you could write a `helloworld.c` program, compile it to a `hello-world-bin`, then `strace .&#47;hello-world-bin` and try to understand all of that trace.\n- You're asking for about 12 credit hours worth of information\n- It would take way to long for me to attempt to answer for each of these, nor could I create a great example for every single one. But your general compilation will have some stages: 1. Lexical Analysis 2. Syntax Analysis 3. Type Checking 4. Intermediate Code Generation 5. Register Allocation 6. Machine Code Generation 7. Assembly and Linking. A decent read on the topic is diku.dk/~torbenm/Basics/basics_lulu2.pdf . Its a little long but its not too hard to digest.\n- @BasileStarynkevitch I am completely sure that it would be a best-seller.\n- This is a great, broad and off-topic question.\n- @Ot&#225;vioD&#233;cio I am sorry sir. Can I ask it on some other stackexchange network site which would handle these kinds of topics. Beside I have already mentioned that you can have links supplementing your answer.\n- And one point of advice. When you're looking for a fundamental explation of computer architecture, just to demystify it. You'll know you're in the right place when you're reading about `and gates` and `not gates` and `or gates`, and eventually half-adders and multiplexers and latches and flip-flops, and other circuit-level stuff\n- The answer can be as broad as the person who is answering wants. I don't think this question is too broad.\n- @fotanus Exactly. Thanks. I wanted to say it but couldn't find words.\n- I have edited my question, will it still remain marked too broad to answer???\n- I think it will take a few days to get it unhold. Thanks for all your help guys.. Starters like me respect all of you computer-lover-programmers a lot. And I wish that there should be a Like button on Top of SO , so that I could like it everyday.Good night.\n- check out Code: The Hidden Language of Computer Hardware and Software\n- Someone has narrowed it down for me.Thanks a lot for that. Though it's not exactly going to serve my initial purpose. But it will do it more than half way. :)\n- I think it has been narrowed down, So why they are still keeping it on hold...???\n- www.nand2tetris.com , it is opening a gaming-site. ??\n- Someone pasted the wrong link... I hate it when that happens.\n- Correct link: nand2tetris.org\n- I will definitely see it.\n- I think that's the best answer to the current question... :)\n- I disagree. Old machines (unless physical machines before 1940) will not reveal the inner workings any more than AMD64 based machines, for example. In fact they may hide more, since one can easily download the Intel developer manuals and learn the theory of its architecture, whereas you have fewer options for understanding a Z80 to the same extent.\n- The Z80 (not used in the BBC Micro actually) is probably one of the most documented processors. It took me 1 (one) google to get the entire manual & architecture in detail direct from Zilog. Nothing is hidden. 1 (one) google to find a C compiler and assembler. It's all so much simpler and the resources are all out there.\n- @Vortico: The thing about the Speccy/BBC is that they didn't really have an OS, so you were generally dealing directly with the hardware. The CPUs were conceptually much simpler than modern ones - there was no caching, no memory managers, no uops translations and superscalar architectures. They just read an instruction, did something, then read the next instruction and so on. That is the one drawback to the RasPi - the hardware is really complex.\n- Yeah, doing something like this is my wish too. But it's very much time consuming and might not be possible before holidays.","metadata":{"transformedAt":"2026-08-18T18:32:17.779Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":87,"estimatedTokens":2231}}252{"id":"stack-24588893","source":"stackoverflow","questionId":24588893,"title":"Swift Explicit vs. Inferred Typing : Performance","tags":["performance","types","compiler-construction","swift"],"text":"Title: Swift Explicit vs. Inferred Typing : Performance\nTags: performance, types, compiler-construction, swift\nSource: Stack Overflow\n\nQuestion:\nI'm reading a tutorial about Swift (http://www.raywenderlich.com/74438/swift-tutorial-a-quick-start) and it preconized to not set type explicitly because it's more readable this way.\n\nI do not really agree about this point but that's not the question. My question is : Is it more efficient, in terms of performance (compiler...) to set type explicitly ?\n\nFor example, would this : `var hello: Int = 56` be more efficient than this : `var tutorialTeam = 56`\n\n========================================\n\nTop Answer:\nFrom my experience, there's been a huge performance impact in terms of compilation speed when using explicit vs inferred types. A majority of my slow compiling code has been resolved by explicitly typing variables. \n\nIt seems like the Swift compiler still has room for improvement in this area. Try benchmarking some of your projects and you'll see a big difference.\n\nHere's an article I wrote on how to speed up slow Swift compile times and how to find out what is causing it.\n\n========================================\n\nCode:\n```text\nvar hello: Int = 56\n```\n\n```text\nvar tutorialTeam = 56\n```\n\n```text\nvar num = 2\n```\n\n```text\nvar num: Double = 2\n```\n\n```text\nnum\n```\n\n```text\nInt\n```\n\n```text\nDouble\n```\n\n```text\nclass func shuffleAny(inout array: [Any]) {\n    for (var i = 0; i < array.count; i++) {\n        let currentObject: Any = array[i]\n        let randomIndex = Int(arc4random()) % array.count\n        let randomObject: Any = array[randomIndex]\n\n        array[i] = randomObject;\n        array[randomIndex] = currentObject\n    }\n}\n```\n\n```text\nclass func shuffleIntObjects(inout array: [Int]) {\n    for (var i = 0; i < array.count; i++) {\n        let currentObject: Int = array[i]\n        let randomIndex = Int(arc4random()) % array.count\n        let randomObject: Int = array[randomIndex]\n\n        array[i] = randomObject;\n        array[randomIndex] = currentObject\n    }\n}\n```\n\n```text\nAny\n```\n\n```text\nInt\n```\n\n```text\n[Any]\n```\n\n```text\nInt\n```\n\n```text\n[Int]\n```\n\n========================================\n\nComments:\n- Since its a compiler check, it won't affect performance of your app at all. `let a = 1` and `let a:Int = 1` are completely equivalent. If you inspect `a` by using alt+click, it will say its an `Int` type.\n- Won't the compiler check any type for 1 before it sets it as an Int ?\n- Um? Perhaps you're not quite understanding the point of static type inference. When you leave an annotation out, it doesn't mean that the compiler just treats the variable as some generic register value that it has to work around. Type inference occurs first, the variable is then treated as though it were that type in all expressions it's used in. If for some reason the compiler cannot deduce the type of the variable, it becomes a type error. Nothing occurs at runtime. There is no performance gain to be had.\n- Ok thanks for your answer, I've no \"low level\" language experience that's why ! You can make an answer and I'll mark my question as solved if you want.\n- @CodaFi second the suggestion you add that as an answer; the difference between compile and run time is not clear to lots of readers and you explain it very well.\n- @jrturton Meh, I think Dave Wood's analysis is enough. Besides, I can't think of a way to reword the comment that wouldn't be bland and dense.\n- The article's advice on this point seems really bad. Making it clear what type a value has is going to make the code easier to read.\n- This is the correct answer. The compiled out is the same, so it's literally impossible for explicit or implicit types to make any difference in performance.\n- @PaulManta - thank you, it annoyed me to see the accepted answer being blatantly wrong!\n- Good idea to check the compiled output.\n- `Whether or not you include the explicit type is a matter of taste. In some contexts it might make your code more readable.` Exactly! Adding type annotations \"to improve compiler performance\" will in the long run cause the code to be bloated and less readable. In the meantime, the compiler performance is improving because of the work of the Swift compiler engineers - which will render the added type annotations useless.\n- This is wrong. Annotating types makes code compile faster because the inference is skipped.\n- What about performance in context of developing in Xcode as the developer developing? As in, does my quality of life improve when explicitly typing? People are profiling method execution, and binary-for-release-compilation-times, which is nonsensical imho, and frankly none of my business. My anecdotal intuition is yes, explicitly typing in a complex swiftui view greatly improves performance. Xcode literally throws it hands up \"Can't type check, too confused, break this file up\" Does it improve \"compile time\", spiritually meaning preview generation, predictive inference, symbol lookup?","metadata":{"transformedAt":"2026-08-18T18:32:17.779Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":14,"totalLines":113,"estimatedTokens":1246}}253{"id":"stack-7787308","source":"stackoverflow","questionId":7787308,"title":"How can I declare a global variable in LLVM?","tags":["compiler-construction","llvm"],"text":"Title: How can I declare a global variable in LLVM?\nTags: compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nI'd like to record some dynamic behaviors into some global variables. So I wrote a pass to instrument the code and insert some instructions to update the global variable. I tried to use the GlobalVariable constructor to define a global variable, but there are two problems. First, how can I DEFINE the global variables in the module containing main function? Second, how can I DECLARE those global variables in other modules? It's like \"extern double someThing;\".\n\nThe target programs are written in C.\n\n========================================\n\nCode:\n```c++\n// Global Variable Declarations\n\nGlobalVariable* gvar_ptr_abc = new GlobalVariable(/*Module=*/*mod, \n        /*Type=*/PointerTy_0,\n        /*isConstant=*/false,\n        /*Linkage=*/GlobalValue::CommonLinkage,\n        /*Initializer=*/0, // has initializer, specified below\n        /*Name=*/\"abc\");\ngvar_ptr_abc->setAlignment(4);\n\n// Constant Definitions\nConstantPointerNull* const_ptr_2 = ConstantPointerNull::get(PointerTy_0);\n\n// Global Variable Definitions\ngvar_ptr_abc->setInitializer(const_ptr_2);\n```\n\n```text\nllc -march=cpp\n```\n\n```text\ncpp\n```\n\n```text\nint *\n```\n\n========================================\n\nComments:\n- What language are you using? (LLVM is not a language, it's \"a collection of modular and reusable compiler and toolchain technologies\").\n- It sounds like they're using the LLVM API to construct programs directly, rather than via compiling a language.\n- Yes, I just want to insert several instructions.\n- That looks like C++, not C (the `ConstantPointerNull::get()` call).\n- @Keith Thompson, you can't write an LLVM pass in C anyway, so what's the problem?\n- The OP said the target programs are written in C. (I'm not familiar with LLVM, so I won't comment further.)\n- Thanks for answering part of the question. Can you explain a bit on the use of \"CommonLinkage\" rather than \"ExternalLinkage\"? I guess this piece of code implies that I should use the constructor to both declare and define a global variable. The only difference is that for declaration I set the initializer to 0?\n- @dalibocai, you'll use `ExternalLinkage` in the module where you're declaring an external variable. This one is a *definition* (see the initialiser).\n- Update to anyone who sees this -- don't bother trying the C++ backend, it doesn't exist anymore.\n- Is there a reason to initialize it afterwards, rather than just create the constant first and pass directly as initializer?","metadata":{"transformedAt":"2026-08-18T18:32:17.779Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":55,"estimatedTokens":640}}254{"id":"stack-23912375","source":"stackoverflow","questionId":23912375,"title":"What language is Haskell written in?","tags":["haskell","compiler-construction","io"],"text":"Title: What language is Haskell written in?\nTags: haskell, compiler-construction, io\nSource: Stack Overflow\n\nQuestion:\nAlso (besides the title question), is there source code available for different impure Haskell functions like IO?\n\nI don't mean the Haskell side of the code, but whatever Haskell implements to do IO.","metadata":{"transformedAt":"2026-08-18T18:32:17.779Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":8,"estimatedTokens":80}}255{"id":"stack-1422729","source":"stackoverflow","questionId":1422729,"title":"How does an uninitiliazed variable get a garbage value?","tags":["c","variables","compiler-construction","programming-languages","initialization"],"text":"Title: How does an uninitiliazed variable get a garbage value?\nTags: c, variables, compiler-construction, programming-languages, initialization\nSource: Stack Overflow\n\nQuestion:\nWhen we create a variable and don't initialize it, then some (random) number called *garbage value* is assigned to it.\n\n- How this value is assigned to the variable?\n\n- What is whole concept/mechanism behind this?\n\n- Does this happen only in C?\n\n========================================\n\nTop Answer:\nMemory is used and reused at various points in your application. For example, as the call stack of your application grows and shrinks the same location in memory may be overwritten many, many times. The thing to remember is that as a piece of memory is abandoned it is not zeroed out, so if you do not specify a new initial value for that place in memory when you use it again you will get the old, \"garbage\" value.\n\nSome languages and structure implementations do default-initialize memory as it is used. Others do not, so it is important to read the documentation of your language carefully to know what to expect.\n\n========================================\n\nCode:\n```text\n.bss\n```\n\n========================================\n\nComments:\n- I would be the last to condemn homework-type questions, but isn't this a bit *too* obvious?\n- @DrJokepu If it would have been obvious to me, I wouldn't waste my time on it.\n- @Ravi, the answer is of course not obvious. What I meant, this looks like a set of questions from a school assignment to me. That's because the questions are constructed in a way that makes the impression that the original asker knew the answers to his or her questions.\n- @ DrJokepu Oh!I misunderstood it.Still, ignore the style of the question.I definitely didn't ask that question to test people's knowledge.I really don't know about it.\n- @DrJokepu - I got the opposite impression \"who and how does it?\" screams to me a \"wrong question\", asked by someone who genuinely doesn't know what's going on, since it has no answer.\n- Ah! Well put than mine :). Great! +1\n- For example, MSVC initializes everything to 0xcccccccc in debug mode (which can come quite helpful sometimes)\n- I'm trying to think of the name of the debugger (late 90s IIRC) that used 0xf00d to fill newly allocated memory...any help?\n- @pm100 The letter `o` is not a digit in hexadecimal.","metadata":{"transformedAt":"2026-08-18T18:32:17.779Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":39,"estimatedTokens":588}}256{"id":"stack-7740761","source":"stackoverflow","questionId":7740761,"title":"how do static variables inside functions work?","tags":["c++","c","compiler-construction","static"],"text":"Title: how do static variables inside functions work?\nTags: c++, c, compiler-construction, static\nSource: Stack Overflow\n\nQuestion:\nIn the following code:\n\n```\nint count(){\n static int n(5);\n n = n + 1;\n return n;\n}\n```\n\nthe variable `n` is instantiated only once at the first call to the function.\n\nThere should be a flag or something so it initialize the variable only once.. I tried to look on the generated assembly code from gcc, but didn't have any clue.\n\nHow does the compiler handle this?\n\n========================================\n\nTop Answer:\nThis is entirely up to the implementation; the language standard says nothing about that.\n\nIn practice, the compiler will usually include a hidden flag variable somewhere that indicates whether the static variable has already been instantiated or not. The static variable and the flag will probably be in the static storage area of the program (e.g. the data segment, not the stack segment), not in the function scope memory, so you may have to look around about in the assembly. (The variable can't go on the call stack, for obvious reasons, so it's really like a global variable. \"static allocation\" really covers all sorts of static variables!)\n\n*Update:* As @aix points out, if the static variable is initialized to a *constant expression*, you may not even need a flag, because the initialization can be performed at load time rather than at the first function call. In C++11 you should be able to take advantage of that better than in C++03 thanks to the wider availability of constant expressions.\n\n========================================\n\nCode:\n```text\nint count(){\n    static int n(5);\n    n = n + 1;\n    return n;\n}\n```\n\n```text\nn\n```\n\n```text\n#include <string>\n\nvoid count() {\n    static std::string str;\n    str += ' ';\n}\n```\n\n```text\n_Z5countv:\n.LFB544:\n        .cfi_startproc\n        .cfi_personality 0x3,__gxx_personality_v0\n        .cfi_lsda 0x3,.LLSDA544\n        pushq   %rbp\n        .cfi_def_cfa_offset 16\n        movq    %rsp, %rbp\n        .cfi_offset 6, -16\n        .cfi_def_cfa_register 6\n        pushq   %r13\n        pushq   %r12\n        pushq   %rbx\n        subq    $8, %rsp\n        movl    $_ZGVZ5countvE3str, %eax\n        movzbl  (%rax), %eax\n        testb   %al, %al\n        jne     .L2                     ; <======= bypass initialization\n        .cfi_offset 3, -40\n        .cfi_offset 12, -32\n        .cfi_offset 13, -24\n        movl    $_ZGVZ5countvE3str, %edi\n        call    __cxa_guard_acquire     ; acquire the lock\n        testl   %eax, %eax\n        setne   %al\n        testb   %al, %al\n        je      .L2                     ; check again\n        movl    $0, %ebx\n        movl    $_ZZ5countvE3str, %edi\n.LEHB0:\n        call    _ZNSsC1Ev               ; call the constructor\n.LEHE0:\n        movl    $_ZGVZ5countvE3str, %edi\n        call    __cxa_guard_release     ; release the lock\n        movl    $_ZNSsD1Ev, %eax\n        movl    $__dso_handle, %edx\n        movl    $_ZZ5countvE3str, %esi\n        movq    %rax, %rdi\n        call    __cxa_atexit            ; schedule the destructor to be called at exit\n        jmp     .L2\n.L7:\n.L3:\n        movl    %edx, %r12d\n        movq    %rax, %r13\n        testb   %bl, %bl\n        jne     .L5\n.L4:\n        movl    $_ZGVZ5countvE3str, %edi\n        call    __cxa_guard_abort\n.L5:\n        movq    %r13, %rax\n        movslq  %r12d,%rdx\n        movq    %rax, %rdi\n.LEHB1:\n        call    _Unwind_Resume\n.L2:\n        movl    $32, %esi\n        movl    $_ZZ5countvE3str, %edi\n        call    _ZNSspLEc\n.LEHE1:\n        addq    $8, %rsp\n        popq    %rbx\n        popq    %r12\n        popq    %r13\n        leave\n        ret\n        .cfi_endproc\n```\n\n```text\nn\n```\n\n```text\nint\n```\n\n```text\ng++\n```\n\n```text\nstd::string\n```\n\n```text\nbypass initialization\n```\n\n========================================\n\nComments:\n- Try with a type that has a non-trivial constructor. You should see more traces of what is actually done. (See e.g. stackoverflow.com/questions/6967179/&hellip; for what type of stuff can happen around this type of static initializer with GCC)\n- @Mat: non-trivial *and* non-constexpr, one might add. E.g. `std::shared_ptr()` is OK.\n- I have never heard of these secret \"hidden flags\". Which compiler does this, can you give any sources? At least every compiler/linker I have seen simply initializes all statics at program startup, then threat them as they would with any global variable. EDIT: Ah nevermind, didn't see the C++ tag. You should probably clearify that this \"flag\" only applies to class constructors, not primitive data types like int.\n- @Lundin: What about `void foo() { static int n = read_from_user(); }`? How can this be initialized at load time?\n- I am pretty sure it also applies to C if you initialize to e.g. the result of a function `static int foo = get_initial_foo();`\n- @Random: In C, initializer elements must be constant.\n- Well, actually the standard is pretty specific about how these things should behave, including the order in which non-trivial constructors and destructors should be run.\n- Function static variables are initialised on the first call, not at startup.\n- @R.MartinhoFernandes The as-if rule applies and all compilers will put the value 5 into the binary and not execute any code on first call.\n- @DavidHeffernan oh, I didn't pay much attention to the example. You're right.\n- Yes, this is exactly how it will be done. All statics no matter where they are declared, as well as all globals, will be placed in a special RAM segment which is initialized at program start. When the code with the \"local\" static is executed, it is treated just as if the static was declared outside the function and already initialized.\n- It should be noted that while C++ supports static local variables initialised by a non-constant expression, C seems to not allow that, at least gcc says `error: initializer element is not constant` when you say `static int x = rand()`.\n- Good point - if the variable is initialized to a constant expression, then you probably don't need any flag at all.","metadata":{"transformedAt":"2026-08-18T18:32:17.779Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":160,"estimatedTokens":1513}}257{"id":"stack-18862622","source":"stackoverflow","questionId":18862622,"title":"Has a programming language ever been written in itself?","tags":["compiler-construction"],"text":"Title: Has a programming language ever been written in itself?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nOkay, so I know that, typically, a language is written in another language (such as Python is written in C).\n\nHowever, has there ever been a language which is written using itself? Obviously, they'd have to use a compiler / interpreter from a previous version of the language, which means, at least initially, it would've been written in something else.\n\nI realize it would be incredibly annoying and pointless to use an existing language to develop a compiler for a new language, only to immediately abandon it and write a new compiler in your new language (which you would then compile using your original compiler), but I don't really see a reason why it couldn't be done (though, again, I do see why it wouldn't be done).\n\nDespite this, has it ever happened?\n\n========================================\n\nComments:\n- Programming languages are specifications. Implementations (e.g. compilers, VMs) are, well, implementations of those specifications. And yes, there are implementations that run/compile the programming language they were written in. Normally there are a series of \"bootstraps\" to get up to that point, however.\n- Kind of duplicate: bootstrapping-a-language\n- Ah, of course. In my head, I was thinking of developing a complete general-purpose compiler first, then developing a compiler in the language later. I didn't consider simply developing a small, minimalistic compiler first. Thanks!","metadata":{"transformedAt":"2026-08-18T18:32:17.779Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":19,"estimatedTokens":383}}258{"id":"stack-645468","source":"stackoverflow","questionId":645468,"title":"Have you ever effectively used lexer/parser in real world application?","tags":["parsing","compiler-construction","tokenize","lexical-analysis"],"text":"Title: Have you ever effectively used lexer/parser in real world application?\nTags: parsing, compiler-construction, tokenize, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nRecently, I've started learning ANTLR. I know that lexers/parsers together can be used to construct programming languages.\n\nOther than DSLs or programming languages, have you ever directly or indirectly used lexer/parser tools (and knowledge) to solve real world problems? Is it possible for the average programmer to solve those problems without knowledge of lexers or parsers?\n\n========================================\n\nTop Answer:\nSyntax highlighting. The Scite text editor allows you to write your own lexer (in C++) to provide syntax highlighting for any custom language. I wrote my own custom lexer for Scite as a refresher on this topic (I studied it a while ago at my university).\n\nRegular Expressions are often used as an alternative for pattern matching and simple language processing. This is even more common of recent years thanks to the improved RegEx support in frameworks such as .NET. In many cases developers may not even know of lexing/parsing techniques and so fall into usng Regex by default.\n\nHowever, as another answer says, Regex can quickly become inefficient, slow and difficult to maintain for anything more than a simple grammar/language. In that situation parser/lexers are generally the best choice.\n\n========================================\n\nCode:\n```text\nsscanf\n```\n\n========================================\n\nComments:\n- Agreed - I never want to write one by hand again. ANTLR has a bit of a learning curve on how to integrate it into your language, though.","metadata":{"transformedAt":"2026-08-18T18:32:17.779Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":29,"estimatedTokens":417}}259{"id":"stack-10284379","source":"stackoverflow","questionId":10284379,"title":"What does pointer reversal in mark and sweep garbage collection buy you?","tags":["pointers","compiler-construction","garbage-collection"],"text":"Title: What does pointer reversal in mark and sweep garbage collection buy you?\nTags: pointers, compiler-construction, garbage-collection\nSource: Stack Overflow\n\nQuestion:\nI feel like I am missing something painfully simple but I am trying to understand mark and sweep garbage collection per Andrew Appel's Modern Compiler Implementation in ML book and there's a small paragraph inside the Mark and Sweep section titled Pointer Reversal (270).\n\nAt this point I think I understand how it works. In a nutshell, as you traverse the graph you flip all the pointers so that your predecessor is inside your set of fields. Then when you are done with a given element, you flip the pointers back so they point at the right place again.\n\nIf that is correct, what exactly does it buy you? Appel attempts to explain this but I don't fully grok his wording.\n\n========================================\n\nComments:\n- Oh I see what you mean now. So it's a way to get stack-like functionality when you don't have enough memory available to actually allocate a stack. That's actually pretty clever (as these things always are). Small issue but you mean during the marking stage right? Sweeping is what comes after?\n- It's interesting to note that some garbage-collection schemes use handles (indirect pointers), but .net uses direct pointers; when an object is relocated, every reference to that object has to be updated to point to the new address. I would conjecture that's why objects are required to be large enough to hold three object references even though there are only two object references' worth of overhead: when an object gets relocated, the first 12/24 bytes of memory that was formerly used by the old object can be used as scratchpad for the GC process. Does that sound right?","metadata":{"transformedAt":"2026-08-18T18:32:17.779Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":16,"estimatedTokens":444}}260{"id":"stack-4332286","source":"stackoverflow","questionId":4332286,"title":"How do I find how C++ compiler implements something except inspecting emitted machine code?","tags":["c++","optimization","compiler-construction","assembly"],"text":"Title: How do I find how C++ compiler implements something except inspecting emitted machine code?\nTags: c++, optimization, compiler-construction, assembly\nSource: Stack Overflow\n\nQuestion:\nSuppose I crafted a set of classes to abstract something and now I worry whether my C++ compiler will be able to peel off those wrappings and emit really clean, concise and fast code. How do I find out what the compiler decided to do?\n\nThe only way I know is to inspect the disassembly. This works well for simple code, but there're two drawbacks - the compiler might do it different when it compiles the same code again and also machine code analysis is not trivial, so it takes effort.\n\nHow else can I find how the compiler decided to implement what I coded in C++?\n\n========================================\n\nTop Answer:\nI was actually wondering about that.\n\nI have been quite interested, for the last few months, in the Clang project.\n\nOne of Clang particular interests, wrt optimization, is that you can emit the optimized LLVM IR code instead of machine code. The IR is a high-level assembly language, with the notion of structure and type.\n\nMost of the optimizations passes in the Clang compiler suite are indeed performed on the IR (the last round is of course architecture specific and performed by the backend depending on the available operations), this means that you could actually see, right in the IR, if the object creation (as in your linked question) was optimized out or not.\n\nI know it is still assembly (though of higher level), but it does seem more readable to me:\n\n- far less opcodes\n\n- typed objects / pointers\n\n- no \"register\" things or \"magic\" knowledge required\n\nWould that suit you :) ?\n\n========================================\n\nCode:\n```text\nsize\n```\n\n```text\n.o\n```\n\n```text\nobjdump\n```\n\n```text\nobjdump -x\n```\n\n========================================\n\nComments:\n- I want to know the answer to this too, but I really can't see how the answer can be anything other than \"Hope the compiler docs mention it somewhere\".\n- +1. however, i don't see how one can know. besides disassembly, or knowing the compiler source (and simulating it in one's head).\n- More generally, first answer the question \"why do you care\" then check to see if what you care about is good enough. If it is move on, if not; profile to find what is taking up the most resources. Often, the answers to all three will surprise you.\n- So now not only the developers should emit readable code, the compilers should too? You can get that if you want if you use -O0, almost a 1:1 mapping everywhere. I think the OP meant be \"clean\" no unnecessary register spills, no calls to constructors which don't do anything, removing a wrapper function by calling the inner function directly etc.\n- @drhirsch Nice necro comment :) But seriously, I understood what he was getting at, I didn't mean that the compiler had to emit clean (human clean) code. I was talking about it, however, because the OP wanted to understand what was going on in the compiler by looking at the disassembly.\n- So, this IR high level assembly language... it sounds a lot like \"C\" from your description! Certainly sounds interesting... got a link to some examples ?\n- @timday: the reference is there llvm.org/docs/LangRef.html a quick tutorial can be found here llvm.org/releases/2.6/docs/tutorial/JITTutorial1.html It is a high level assembly and not C-like at all (notably because it's expressed in SSA form).\n- Interesting. Any idea how to get clang to output IR?\n- @dhardy: If you have a tiny example, you can always use the online the LLVM Try Out page :) Otherwise, there is the `-emit-llvm` flag. I think the result is a binary file, but if it is it can be converted back to text using `llvm-dis`.","metadata":{"transformedAt":"2026-08-18T18:32:17.779Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":63,"estimatedTokens":939}}261{"id":"stack-2703316","source":"stackoverflow","questionId":2703316,"title":"Regular expression for bit strings with even number of 1s","tags":["regex","compiler-construction","grammar"],"text":"Title: Regular expression for bit strings with even number of 1s\nTags: regex, compiler-construction, grammar\nSource: Stack Overflow\n\nQuestion:\nLet `L= { w in (0+1)* | w has even number of 1s}`, i.e. L is the set of all bit strings with even number of 1s. Which one of the regular expressions below represents L?\n\nA) (0*10*1)*\n\nB) 0*(10*10*)*\n\nC) 0*(10*1)* 0*\n\nD) 0*1(10*1)* 10*\n\nAccording to me option `D` is never correct because it does not represent the bit string with zero 1s. But what about the other options? We are concerned about the number of 1s(even or not) not the number of zeros doesn't matter.\n\nThen which is the correct option and why?\n\n========================================\n\nTop Answer:\nTo solve such a problem you should\n\n- Supply counterexample patterns to all \"incorrect\" regexps. This will be either a string in `L` that is not matched, or a matched string out of `L`.\nTo prove the remaining \"correct\" pattern, you should answer two questions:\n\n- Does every string that matches the pattern belong to `L`? This can be done by devising properties each of matched strings should satisfy--for example, number of occurrences of some character...\n\n- Is every string in `L` matched by the regexp? This is done by dividing `L` into easily analyzable subclasses, and showing that each of them matches pattern in its own way.\n\n(No concrete answers due to [homework]).\n\n========================================\n\nCode:\n```text\nL= { w in (0+1)* | w has even number of 1s}\n```\n\n```text\nD\n```\n\n```text\n0\n```\n\n```text\n11011\n```\n\n```text\n1100\n```\n\n```text\nimport re\n\na = re.compile(\"(0*10*1)*\")\nb = re.compile(\"0*(10*10*)*\")\nc = re.compile(\"0*(10*1)* 0*\")\nd = re.compile(\"0*1(10*1)* 10*\")\n\ncandidates = [('a',a),('b',b),('c',c),('d',d)]\ntests = ['0110', '1100', '0011', '11011']\nfor test in tests:\n    for candidate in candidates:\n        if not candidate[1].match(test):\n            candidates.remove(candidate)\n            print \"removed %s because it failed on %s\" % (candidate[0], test)\n\nntests = ['1', '10', '01', '010', '10101']\nfor test in ntests:\n    for candidate in candidates:\n        if candidate[1].match(test):\n            candidates.remove(candidate)\n            print \"removed %s because it matched on %s\" % (candidate[0], test)\n```\n\n```text\nL\n```\n\n```text\nL\n```\n\n```text\nL\n```\n\n```text\nL\n```\n\n```text\nL\n```\n\n```text\n^0*(10*10*)*$\n\n^          # match beginning of string\n0*         # match zero or more '0'\n(          # start group 1\n 10*       # match '1' followed by zero or more '0'\n 10*       # match '1' followed by zero or more '0'\n)*         # end group 1 - match zero or more times\n$          # end of string\n```\n\n```text\nB\n```\n\n```text\n1\n```\n\n```text\n(0*10*10*)\n```\n\n========================================\n\nComments:\n- Note that these are not string searching regexps; these are language matching regexps. So remember to anchor them when testing.\n- The string \"`000`\" has an even number of 1s (zero 1s) but the A regex doesn't match it. (I guess I should have said that the A regex doesn't match `0+` as it does get the empty string). --- I pointed it out because It's an important corner case that hadn't been brought up and I did so *here* because I didn't think it was worth it's own answer.\n- Ah. OK, gotcha... Updated! Thanks!\n- Just because you haven't disproven B, doesn't mean that you have proven B. Nice effort, though, just fallacious logic.\n- oops, my bad. when anchoring the expressions (putting each one between a ^ and a $), the only one that survives is B. of course, you'd still have to prove it...\n- I don't think the whitespaces within the regular expressions are supposed to count. You should rerun it with whitespace ignored.\n- Could you please give some details? An opinion is usually not sufficient.\n- Your expression does not match 011011. It should read: (0*10*10*)*, which is no better than 0*(10*10*)*\n- This doesn't match 01111","metadata":{"transformedAt":"2026-08-18T18:32:17.779Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":15,"totalLines":136,"estimatedTokens":973}}262{"id":"stack-6851869","source":"stackoverflow","questionId":6851869,"title":"lightweight javascript to javascript parser","tags":["javascript","parsing","compiler-construction"],"text":"Title: lightweight javascript to javascript parser\nTags: javascript, parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nHow would I go about writing a lightweight javascript to javascript parser. Something simple that can convert some snippets of code.\n\nI would like to basically make the internal scope objects in functions public.\n\nSo something like this\n\n```\nvar outer = 42;\nwindow.addEventListener('load', function() {\n var inner = 42;\n function magic() {\n var in_magic = inner + outer;\n console.log(in_magic);\n }\n magic();\n}, false);\n```\n\nWould compile to\n\n```\n__Scope__.set('outer', 42);\n__Scope__.set('console', console);\nwindow.addEventListener('load', constructScopeWrapper(__Scope__, function(__Scope__) {\n __Scope__.set('inner', 42);\n __Scope__.set('magic',constructScopeWrapper(__Scope__, function _magic(__Scope__) {\n __Scope__.set('in_magic', __Scope__.get('inner') + __Scope__.get('outer'));\n __Scope__.get('console').log(__Scope__.get('in_magic'));\n }));\n __Scope__.get('magic')();\n}), false);\n```\n\nDemonstation Example\n\nMotivation behind this is to serialize the state of functions and closures and keep them synchronized across different machines (client, server, multiple servers). For this I would need a representation of `[[Scope]]`\n\n**Questions:**\n\n- Can I do this kind of compiler without writing a full JavaScript -> (slightly different) JavaScript compiler?\n\n- How would I go about writing such a compiler?\n\n- Can I re-use existing js -> js compilers?\n\n========================================\n\nTop Answer:\nI don't think your task is easy or short given that you want to access and restore all the program state. One of the issues is that you might have to capture the program state at any moment during a computation, right? That means the example as shown isn't quite right; that captures state sort of before execution of that code (except that you've precomputed the sum that initializes magic, and that won't happen before the code runs for the original JavaScript). I assume you might want to capture the state at any instant during execution.\n\nThe way you've stated your problem, is you want a JavaScript parser in JavaScript. \nI assume you are imagining that your existing JavaScript code J, includes such a JavaScript parser and whatever else is necessary to generate your resulting code G, and that when J starts up it feeds copies of itself to G, manufacturing the serialization code S and somehow loading that up.\n(I think G is pretty big and hoary if it can handle all of Javascript)\nSo your JavaScript image contains J, big G, S and does an expensive operation (feed J to G) when it starts up. \n\nWhat I think might serve you better is a tool G that processes your original JavaScript code J offline, and generates program state/closure serialization code S (to save and restore that state) that can be added to/replace J for execution. J+S are sent to the client, who never sees G or its execution. This decouples the generation of S from the runtime execution of J, saving on client execution time and space.\n\nIn this case, you want a tool that will make generation of such code S easiest. A pure JavaScript parser is a start but isn't likely enough; you'll need symbol table support to know which function code is connected a function call F(...), and which variable definition in which scope corresponds to assignments or accesses to a variable V. You may need to actually modify your original code J to insert points of access where the program state can be captured. You may need flow analysis to find out where some values went. Insisting all of this in JavaScript narrows your range of solutions.\n\nFor these tasks, you will likely find a program transformation tool useful. Such tools contain parsers for the langauge of interest, build ASTs representing the program, enable the construction of identifier-to-definition maps (\"symbol tables\"), can carry out modifications to the ASTs representing insertion of access points, or synthesis of ASTs representing your demonstration example, and then regenerate valid JavaScript code containing the modified J and the additions S.\nOf all the program transformation systems that I know about (which includes all the ones at the Wikipedia site), none are implemented in JavaScript.\n\nOur DMS Software Reengineering Toolkit is such a program transformation system offering all the features I just described. (Yes, its big and hoary; it has to be to handle the complexities of real computer languages). It has a JavaScript front end that contains a complete JavaScript parser to ASTs, and the machinery to regenerate JavaScript code from modified or synthesized ASTs. (Also big and hoary; good thing that hoary + hoary is still just hoary). Should it be useful, DMS also provides support for building control and dataflow analysis.\n\n========================================\n\nCode:\n```text\nvar outer = 42;\nwindow.addEventListener('load', function() {\n   var inner = 42;\n   function magic() {\n       var in_magic = inner + outer;\n       console.log(in_magic);\n   }\n   magic();\n}, false);\n```\n\n```text\n__Scope__.set('outer', 42);\n__Scope__.set('console', console);\nwindow.addEventListener('load', constructScopeWrapper(__Scope__, function(__Scope__) {\n    __Scope__.set('inner', 42);\n    __Scope__.set('magic',constructScopeWrapper(__Scope__, function _magic(__Scope__) {\n        __Scope__.set('in_magic', __Scope__.get('inner') + __Scope__.get('outer'));\n        __Scope__.get('console').log(__Scope__.get('in_magic'));\n    }));\n    __Scope__.get('magic')();\n}), false);\n```\n\n```text\n[[Scope]]\n```\n\n========================================\n\nComments:\n- I mentioned the motivation. I need programatic access to closure scope so I can serialize and synchronize closures across different physical machines.\n- Hmmm, but wouldn't you end up serialising almost everything? Lexical scoping is very \"parental\" as opposed to, say, block scope. You trying to write a decent node module to implement horizontal scalability?\n- @davin Yes. I would indeed serialize everything. The aim is to be able to have backwards compatibility.\n- You might want to refactor how the compiled code comes out. There is currently no way to distinguish between `myvariable='string'` and `var myvariable='mystring'`.\n- @Lime I just wrote a quick demo. I know there are plenty of edge cases I have not handled.\n- @Raynos: I came across this Parser API from Mozilla today. Haven't looked at it in depth, but perhaps is something like what you're looking for. Found it at dherman at mozilla blog.\n- @patrick_dw that means the compiler will only run on spidermonkey. So no node.js support :(\n- I wish I could add more bounty, because I am incredibly interested in an easish way to actually do this...\n- @Lime I doubt it exists. You have to write a transpiler.\n- \"Transpiler\"? What on earth is that?\n- @IraBaxter it's me misspelling transcompiler\n- @Raynos: Wow. I looked this up in Wikipedia... which defines it as a compiler that maps source code to source code. I guess that's OK as definition, but I'm astonished that I've never heard this term and I've been building them (I call them \"source to source translators\") for almost 40 years. (See my bio). But yes, according to this definition, you have to write a \"transpiler\". I don't see how knowing that helps much; its still a lot of work, and you still need a full JavaScript parser as a minimum.\n- @IraBaxter once I get round to it I'll write one, I also have a different set of motivations for writing one.\n- I was thinking that, but hacking around with those without understanding the code is a sure-fire way to create bugs. I was hoping there was some parsing technique for not having to write a full compiler\n- @Raynos: If you want to handle all the issues that JavaScript raises, you should expect to have what amounts to a full JavaScript front end (parser, symbol table construction) [but not a full compiler]. I think the term \"lightweight\" isn't workable for this.\n- @Raynos: so exactly what are you hoping for? You seem to want something that doesn't handle the required complexity, and you don't want to deal with a beast that does. The JSHint/JSLint proposal doesn't require you to write a compiler; it requires you to use a JavaScript front end that already exists. What's the issue?\n- @IraBaxter I was curious whether there were any other solutions then take an existing js parser and hack it to meet your needs.\n- @Raynos: Given that you have a custom problem, I'd doubt if your solution is just likely to fall on you. So, you have to customize something. Given that you need full JavaScript parsing capability to process Javascript effectively (well, there's a faint chance you might not but that seems like a bad bet and you wouldn't want to be surprised most of way through the process) customizing an existing parser seems like far the best bet. (@missingno: +1)\n- Your DMS page doesn't have a useful \"here is where you download / install / try / buy it\" page. Not one I could find anyhow.\n- It isn't a tool that you can \"download and play with in an afternoon\". Contact me offline for a longer discussion; see bio.\n- So your message is, \"use a bad (Pratt) parser\", or use *just* a lexer (you can't do any serious JavaScript manipulation with this)? I simply don't understand why you think this is an answer.\n- @Ira, if the input is pretty standard non-obfuscated JS, then he can use the Pratt parser. If the input is messy JS and performance is not that important, then the es-lab parser is fine. If the transformations falls into the set of things that can be easily handled at a lexical level, then do lexical transformation. You're wrong about not being able to do serious JavaScript manipulation at a lexical level; often it's easier to do it with a parse tree, but sometimes performance tradeoffs justify the engineering effort.\n- OP wants to take arbitrary code and manipulate it, if I understand his request. If you have to do any interesting manipulation of a complex language, it is damn hard to do on lexemes. That's the reason people invented parsers. In this particular case, OP needs to know the value of all state variables --> the value of symbols, local or otherwise. I don't see how he can do that without understanding nesting, scopes, ... And you'd be crazy to do that without parsing. What specific complex task have you done with just lexemes?\n- @Ira, with lexical analysis only, I have identified all function bodies to assign readable names to anonymous functions and keep a timed log of all function entrances and exits for profiling and dynamic call graph generation. I have also done tracking of catch and finally blocks to allow enforcement of uncatchable exceptions. The value of state variables cannot be determined with any level of static analysis as it's undecidable. You can however associate all symbols with scope by matching curly braces and identifying var declarations and formal parameters all of which can be done lexically.\n- if you take lexical tokens and do all that nested brace counting and testing for things that look like function prototypes, you are in essence just building a bad parser. For tasks like the ones you've described, perhaps a bad parser works well enough. As you bring more and more of the language structure into the picture, you get more and more realistic parsing. If you want to do sophisticated transformation (try lifting a statement out of a function) you pretty much need the full parser. I don't think OP will be able to do his task with a parser of the style you suggest. YMMV.\n- @Ira, true but with the overhead of a stack, not the overhead of building, traversing, and collecting a tree structure in JavaScript. I agree that inlining requires significant work as does alpha-renaming which is why I used es-lab for alpha renaming but there's a surprising amount you can do with lexing if you assume a valid program and don't care about associativity and precedence within expressions. I believe what the OP wants to do can be done lexically by adding an object & `with` per scope & hoisting decls.\n- `Magic` is hard with closure definitions..so you will have to create some artificial ID for each instance of each closure, and then also figure out a way of how to import/export enviorinment variables which are available to each closure instance.\n- @Scren closures aren't that difficult. You just keep a solid reference to how the scope chain is set up. The real problem is making sure `eval` and `with` don't break. That's going to be a right pain that I might just ignore.\n- I think you are right -- however that also means that you can only solve this by complete parsing and keeping tracking of variable-names etc, so that you can create context blocks for each access point. Hence, you need to leverage one of the parsers either from one of the obfuscator/compressors, or by taking relevant parts from V8","metadata":{"transformedAt":"2026-08-18T18:32:17.779Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":133,"estimatedTokens":3231}}263{"id":"stack-5737905","source":"stackoverflow","questionId":5737905,"title":"why c/c++ allows omission of leftmost index of a multidimensional array in a function call?","tags":["c++","c","compiler-construction","language-design"],"text":"Title: why c/c++ allows omission of leftmost index of a multidimensional array in a function call?\nTags: c++, c, compiler-construction, language-design\nSource: Stack Overflow\n\nQuestion:\nI was just wondering why it is allowed to omit the leftmost index of a multidimensional array when passing the array to a function ? Why not more than one indexes? And how does the compiler find out the size with one index omitted ?\n\n========================================\n\nTop Answer:\n**In a declaration**\n\nActually, you can't leave out the rightmost or the leftmost dimension entirely.\n\nHowever, the leftmost *only* can be deduced for you if you have an initialiser.\n\n**In a function argument list**\n\nWhen you pass an array into a function by value, you're actually passing a pointer to the first element of that array. Yes, it looks from the syntax like you're passing an array but, no, you're not.\n\nConsider:\n\n```\nvoid f(int ar[3])\n\nvoid f(int ar[])\n```\n\nBoth are confusing syntax for the equivalent:\n\n```\nvoid f(int* ar)\n```\n\nNo trace of an array, let alone one of specifically three elements.\n\nNow:\n\n```\nvoid f(int ar[][3])\n```\n\nThis is confusing syntax for the equivalent:\n\n```\nvoid f(int (*ar)[3])\n```\n\nwhere `int (*)[3]` is the type of a pointer to the first element of your array (pointer to `int[3]`).\n\nIn conclusion, don't pay too much attention to the array-like syntax that looks like `[]`; it's not really representative of what's truly happening.\n\n========================================\n\nCode:\n```text\nA -> _ _ _ _ _ _ ...\ni:   0 1 2 3 4 5 ...\n```\n\n```text\nB -> [_ _ _][_ _ _][_ _ _][_ _ _]...\ni:    0 0 0  1 1 1  2 2 2  3 3 3\nj:    0 1 2  0 1 2  0 1 2  0 1 2\n     ^first row    ^third row\n```\n\n```text\nint sum(int arr[10]){\n    int s = 0, i;\n    for(i=0; i<10; i++){\n        s += arr[i];\n    }\n    return s;\n}\n```\n\n```text\nint sum(int arr[], int n){\n    int s = 0, i;\n    for(i=0; i<n; i++){\n        s += arr[i];\n    }\n    return s;\n}\n```\n\n```text\n_\n```\n\n```text\nA[i][j]\n```\n\n```text\nvoid f(int ar[3])\n\nvoid f(int ar[])\n```\n\n```text\nvoid f(int* ar)\n```\n\n```text\nvoid f(int ar[][3])\n```\n\n```text\nvoid f(int (*ar)[3])\n```\n\n```text\nint (*)[3]\n```\n\n```text\nint[3]\n```\n\n```text\n[]\n```\n\n```text\nint arr[10] = {0,1,2,3,4,5,6,7,8,9};\nfoo(arr);\n```\n\n```text\nvoid foo(int *a)     // <- foo receives a pointer to int, not an array\n```\n\n```text\nvoid foo(int a[])    // <-- a[] is a synonym for *a\n```\n\n```text\nint arr[10][20];\nfoo(arr);\n```\n\n```text\nvoid foo(int (*a)[20])  // <-- foo receives a pointer to an array, not an array of arrays\n```\n\n```text\nvoid foo(int a[][20])  // <-- a[][20] is a synonym for (*a)[20]\n```\n\n```text\nsizeof\n```\n\n```text\n&\n```\n\n```text\nT\n```\n\n```text\nT\n```\n\n```text\narr\n```\n\n```text\nfoo\n```\n\n```text\narr\n```\n\n```text\nsizeof\n```\n\n```text\n&\n```\n\n```text\nint\n```\n\n```text\nint\n```\n\n```text\nfoo\n```\n\n```text\nT a[]\n```\n\n```text\nT a[N]\n```\n\n```text\nT *a\n```\n\n```text\na\n```\n\n```text\nT\n```\n\n```text\nT\n```\n\n```text\nfoo\n```\n\n```text\na\n```\n\n```text\nint\n```\n\n```text\narr\n```\n\n```text\nint\n```\n\n```text\nint\n```\n\n```text\nfoo\n```\n\n```text\na\n```\n\n========================================\n\nComments:\n- Could you provide some code as an example ?\n- \"declaration of ‘array’ as multidimensional array must have bounds for all dimensions **except the first**\" where's evidence the first can't? ... ideone.com/kR76X ...ah. I vaguely recall it being treated just like a pointer if it only has one dimension and you leave it blank.\n- What about `void foo(int bar[][3]);`?\n- @Philip: Pfft, the bit about him passing to a function is an edit to the question. I'll add to my answer.\n- @Tomalak: Didn't notice the OP's edit. I certainly didn't intend to hint you at something, I was just curious.\n- @Philip: No problem; I'm glad you pointed it out. Have edited.\n- @Mark: You're thinking of when a function has an argument `int ar[]`; however, this is not related to declarations. :)\n- Since when is `void f(int ar[3])` illegal? The 3 is meaningless and useless (unless you write `static` before it) but it's not illegal as far as I know.\n- @BarryTheHatchet: the links you given in ur answer are broken.\n- @Destructor Dangit","metadata":{"transformedAt":"2026-08-18T18:32:17.780Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":45,"totalLines":268,"estimatedTokens":1030}}264{"id":"stack-50431793","source":"stackoverflow","questionId":50431793,"title":"How to compile and keep \"unused\" C declarations with clang -emit-llvm","tags":["c","compiler-construction","clang","llvm","bitcode"],"text":"Title: How to compile and keep \"unused\" C declarations with clang -emit-llvm\nTags: c, compiler-construction, clang, llvm, bitcode\nSource: Stack Overflow\n\nQuestion:\n### Context\n\nI'm writing a compiler for a language that requires lots of runtime functions. I'm using LLVM as my backend, so the codegen needs types for all those runtime types (functions, structs, etc) and instead of defining all of them manually using the LLVM APIs or handwriting the LLVM IR I'd like to write the headers in C and compile to the bitcode that the compiler can pull in with `LLVMParseBitcodeInContext2`.\n\n### Issue\n\nThe issue I'm having is that clang doesn't seem to keep any of the type *declarations* that aren't used by any any function *definitions*. Clang has `-femit-all-decls` which *sounds* like it's supposed to solve it, but it unfortunately isn't and Googling suggests it's misnamed as it only affects unused definitions, not declarations.\n\nI then thought perhaps if I compile the headers only into `.gch` files I could pull them in with `LLVMParseBitcodeInContext2` the same way (since the docs say they use \"the same\" bitcode format\", however doing so errors with `error: Invalid bitcode signature` so something must be different. Perhaps the difference is small enough to workaround?\n\nAny suggestions or relatively easy workarounds that can be automated for a complex runtime? I'd also be interested if someone has a totally alternative suggestion on approaching this general use case, keeping in mind I don't want to statically link in the runtime function bodies for every single object file I generate, just the types. I imagine this is something other compilers have needed as well so I wouldn't be surprised if I'm approaching this wrong.\n\ne.g. given this input:\n\n### runtime.h\n\n```\nstruct Foo {\n int a;\n int b;\n};\n\nstruct Foo * something_with_foo(struct Foo *foo);\n```\n\nI need a bitcode file with this equivalent IR\n\n### runtime.ll\n\n```\n; ...etc...\n\n%struct.Foo = type { i32, i32 }\n\ndeclare %struct.Foo* @something_with_foo(%struct.Foo*)\n\n; ...etc...\n```\n\nI could write it all by hand, but this would be duplicative as I also need to create C headers for other interop and it'd be ideal not to have to keep them in sync manually. The runtime is rather large. I guess I could also do things the other way around: write the declarations in LLVM IR and generate the C headers.\n\nSomeone else asked about this years back, but the proposed solutions are rather hacky and fairly impractical for a runtime of this size and type complexity: Clang - Compiling a C header to LLVM IR/bitcode\n\n========================================\n\nTop Answer:\nClang's precompiled headers implementation does not seem to output LLVM IR, but only the AST (Abstract Syntax Tree) so that the header does not need to be parsed again:\n\n The AST file itself contains a serialized representation of Clang’s\n abstract syntax trees and supporting data structures, stored using the\n same compressed bitstream as LLVM’s bitcode file format.\n\nThe underlying binary format may be the same, but it sounds like the content is different and LLVM's bitcode format is merely a container in this case. This is not very clear from the help page on the website, so I am just speculating. A LLVM/Clang expert could help clarify this point.\n\nUnfortunately, there does not seem to be an elegant way around this. What I suggest in order to minimize the effort required to achieve what you want is to build a minimal C/C++ source file that in some way uses all the declarations that you want to be compiled to LLVM IR. For example, you just need to declare a pointer to a struct to ensure it does not get optimized away, and you may just provide an empty definition for a function to keep its signature.\n\nOnce you have a minimal source file, compile it with `clang -O0 -c -emit-llvm -o precompiled.ll` to get a module with all definitions in LLVM IR format. \n\nAn example from the snippet you posted:\n\n```\nstruct Foo {\n int a;\n int b;\n};\n\n// Fake function definition.\nstruct Foo * something_with_foo(struct Foo *foo)\n{\n return NULL;\n}\n\n// A global variable.\nstruct Foo* x;\n```\n\nOutput that shows that definitions are kept: https://godbolt.org/g/2F89BH\n\n========================================\n\nCode:\n```text\nstruct Foo {\n  int a;\n  int b;\n};\n\nstruct Foo * something_with_foo(struct Foo *foo);\n```\n\n```text\n; ...etc...\n\n%struct.Foo = type { i32, i32 }\n\ndeclare %struct.Foo* @something_with_foo(%struct.Foo*)\n\n; ...etc...\n```\n\n```text\nLLVMParseBitcodeInContext2\n```\n\n```text\n-femit-all-decls\n```\n\n```text\n.gch\n```\n\n```text\nLLVMParseBitcodeInContext2\n```\n\n```text\nerror: Invalid bitcode signature\n```\n\n```text\n// Forward declarations are emitted lazily on first use.\nif (!FD->doesThisDeclarationHaveABody()) {\n  if (!FD->doesDeclarationForceExternallyVisibleDefinition())\n    return;\n```\n\n```text\n// Forward declarations are emitted lazily on first use.\nif (!FD->doesThisDeclarationHaveABody()) {\n  if (!FD->doesDeclarationForceExternallyVisibleDefinition() && false)\n    return;\n```\n\n```text\nclang\n```\n\n```text\n&& false\n```\n\n```text\nclang\n```\n\n```text\n.ll\n```\n\n```text\n.bc\n```\n\n```text\n--emit-all-declarations\n```\n\n```text\nstruct Foo {\n  int a;\n  int b;\n};\n\n// Fake function definition.\nstruct Foo *  something_with_foo(struct Foo *foo)\n{\n    return NULL;\n}\n\n// A global variable.\nstruct Foo* x;\n```\n\n```text\nclang -O0 -c -emit-llvm -o precompiled.ll\n```\n\n========================================\n\nComments:\n- Can you give a simple example of what you want to achieve?\n- Edited to include simple example\n- Is modifying `clang` in scope of a possible solution?\n- @AjayBrahmakshatriya :) it's definitely a possibility and I'm happy to do the work if it were relatively easy to find out *where* it strips them to begin with. I digged for about an hour and couldn't find it specifically. I'd need to spend a lot more time, unless you know already?\n- @jayphelps I have posted an answer. Sorry for the delay. I knew where the code was, but was away from my desktop for a few days.\n- Thanks! I'm trying to avoid this as then I need to write code that removes the definitions (function bodies) from the LLVMModule, otherwise it complicates the linking process later with duplicate bodies.\n- @jayphelps You can use the `Function::deleteBody` function for that as a one-liner (link). Just iterate on all functions on the precompiled module and call `deleteBody` on them.\n- Thanks Banex! I'm aware, I'd just really prefer not to hack around this and even though I was fairly confident there was not a non-hacky solution I thought I'd ask. If no better solution is proposed your question will be selected as the answer (and bounty winner) in 4 days. Thanks again!\n- So wonderful! Of course it'd be ideal to not have to modify clang, but this is indeed the correct answer for a non-hacky solution and it's possible they'll accept a PR when I add it as a flag. Thanks much!\n- @jayphelps you can try submitting a PR but whether they will accept it depends on if they think this is an important feature. And I also doubt there is any other way to achieve this result without modifying `clang`, since these declarations are explicitly dropped here.","metadata":{"transformedAt":"2026-08-18T18:32:17.780Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":196,"estimatedTokens":1795}}265{"id":"stack-255442","source":"stackoverflow","questionId":255442,"title":"Which parser generator to teach my students?","tags":["parsing","compiler-construction"],"text":"Title: Which parser generator to teach my students?\nTags: parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am teaching (with others) a relatively introductory course in computer science for IT professionals without a background in CS. Since I developed the course materials on automata and grammars, I am also responsible for teaching about compilers and compiler construction.\n\nYears ago, when I studied compilation in college, all our examples came from Lex and Yacc. Are these still in widespread use? Is there something that is more commonly used for Java? The students are proficient in C and Java but have never used parser generators.\n\nAny tips on what to teach would be appreciated\n\n========================================\n\nTop Answer:\nI don't use lexer and parser generators. They're simple enough to generate by hand, and are the easiest parts of a compiler to write. Besides, when you build them by hand, you can make them really fast.\n\n========================================\n\nComments:\n- This would be on-topic at cseducators.stackexchange.com (though since it's still in private beta, it's easiest to enter through here area51.stackexchange.com/proposals/92460/&hellip;)\n- That's right. If they already know java, antlr is the way to go.\n- If you teach CS, you may be interested in the new CS Educator's Stack Exchange (though since it's still in private beta, it's easiest to enter through here)\n- I even used spirit to parse command line arguments ... :)\n- Don't like the program_options library, eh? :)\n- in our university (i'm still studying), we use Boost::Spririt as basis for teaching both parsing and advanced C++. It works really well for those who are motivated to learn, but of course, seems far too indepth for cruisy students\n- You apparently have never written complicated parser/lexers... you really don't want to maintain those by hand if they get complicated.\n- Yes, I've done parsers and lexers by hand that do arbitrary lookahead and backtracking, and even one for C++! (The C++ is a bit harder because of its interactions with the preprocessor, tokens that exist or not based on compiler switches, etc.)\n- More Necromancy... I love the arrogance of youth. Jorn, if you still have any attention span left... www.digitalmars.com\n- @Jorn Do you know who you are addressing? lol\n- I don't, but I stand by my comment. Sorry if I have offended anyone.\n- Open source GLR parser: Elkhound. scottmcpeak.com/elkhound. It's even been used to generate a C++ parser.","metadata":{"transformedAt":"2026-08-18T18:32:17.780Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":31,"estimatedTokens":627}}266{"id":"stack-29292716","source":"stackoverflow","questionId":29292716,"title":"Why earlier versions of C made it mandatory to declare variables in the beginning?","tags":["c++","c","compiler-construction"],"text":"Title: Why earlier versions of C made it mandatory to declare variables in the beginning?\nTags: c++, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have been going through a bit of history of C, and I find that in earlier versions of C, like in C89 standard, it is mandatory to declare variables at the beginning of a block. \n\nBut I also find there are some relaxations from C99 standard specification, where variable can be declared anywhere before it is used.\n\nMy question is why the earlier versions made it mandatory? my emphasis is to know if there was any technical difficulties in designing the compiler at those days, that prevented them identifying declarations at any point.\n\nAlso, with a compiler design perspective I understand, with such a restriction in C89, it is easy to handle variable declarations and usage with the help of an intermediate file to store the mappings. But are there methods that can handle the case without using an intermediary file, say some memory based storage?.\n\n========================================\n\nTop Answer:\nC99 Rationale didn't directly explain why it was not permited in C89, but did say it was added in C99 because it was permited in other languages and the it has been found useful.\n\n \n\n### Rationale for International Standard — Programming Languages — C\n\n \n §6.2.4 Storage durations of objects\n\n \n A new feature of C99: C89 requires all declarations in a block to occur before any statements. On the other hand, many languages similar to C (such as Algol 68 and C++) permit declarations and statements to be mixed in an arbitrary manner. This feature has been found to be useful and has been added to C99.\n\n========================================\n\nComments:\n- There's no need for an intermediate file to store the mappings, and there never was.\n- @immibis - if its not too much trouble, can you elaborate on some of the memory based methods (or any other non file based method).\n- Frankly, I greatly prefer languages like Pascal where you *must* declare your variables separately, up-front. It's just a better way to structure code.\n- @AmrithKrishna You're basically asking how the compiler can use arrays...?\n- Close-voters, please think twice before voting to close this as \"primarily opinion-based\". OP is asking for the technical justification of a piece of the C89 standard.\n- It seems to me like it was more of a style enforcement. That variables being declared at the top of their block scope was just good style.\n- @FpggyDay: you're so wrong it's amazing (minimising the scope of data and code to where it's actually useful - and meaningful values are ready to assign - is a fundamental principle of maintainable programming), and you're increasing the impression that this question should be closed as opinion based by diverting from the technical aspects of the question into a statement of opinion.\n- @Pradhan He's asking why Dennis Ritchie made a decision in 1972. Only he knows that, unless it's documented somewhere.\n- @EJP I am more interested in the technical considerations that possibly led to such a decision, as I clearly state that emphasis is on understanding the technical difficulties.\n- @EJP No, he is asking about a decision in the C89 ANSI standard. It isn't a stretch to imagine that the decisions made were well-documented. It is indeed possible that this was a purely stylistic decision which, at the time it was made, had no supporting technical reasons for either side; however, given that it is a very fundamental aspect, I would be very surprised if this were true.\n- @Pradhan Most of the decisions in the C89 standard were made by Dennis Ritchie in 1972, certainly including this one, and the C89 standard does not contain technical justifications for every language feature.\n- @AmrithKrishna Same answer. Ask Dennis Ritchie.\n- @EJP Really? I did not know that. I assumed \"ANSI Standard\" implied there would have been deliberations similar to todays C++ ISO standards.\n- Don't forget, back in the early 1970's when C was first being designed, the machines didn't necessarily have as much as 64 KiB of memory (they had *less*!) in which to work. Simplification was necessary to get the compiler to perform.\n- @Pradhan: To a very large extent, the C Standard committee took the language as it was, and the library, and made mostly minimal changes to it. The biggest change was function prototypes, which had been extensively tested with C++. Lesser changes were in the definition of the preprocessor and the `#` and `##` operators. They took a subset of the Unix C library and made it standard, but seldom changed the functionality of the functions which were adopted (which is why `gets()` survived until evicted in C11). The least successful features were committee inventions like trigraphs and locales.\n- @JonathanLeffler IIRC mktime was a committee invention.\n- The corollary way to think of this is that in the prehistory of computers compiling C89 and before, it would have taken nontrivial user time to pre-parse through every block to find all the locals before going back to start compiling and emitting code, should the compiler have been structured in such a one-pass way.\n- Wouldn't you need back patching (or live with the inefficiency of incrementing the stack pointer multiple times within a function) anyway since new variables can still be declared inside of inner blocks?\n- @sepp2k: the same kind of optimisation can be applied at the block level rather than the variable level, so you don't have a perfect solution, but it can be dramatically better. (FWIW, my understanding is that some modern compilers derive a \"high water mark\" from the largest total size of automatic variables in nested scopes throughout the function, and do one stack pointer operation for that amount of memory, so local-scope memory isn't necessarily promptly returned to the stack, but is at least reused by later local scopes).\n- @sepp2k It wouldn't be surprising if inner blocks allocated stack memory on entry and released it on exit.\n- @immibis what about on a goto?\n- @Random832 treat it as a return from N blocks and disallow got into a block with newly declared variables\n- @ratchetfreak: you can't disallow goto into a block with its own declared variables, C89 permits it. A C compiler must deal with goto by considering the \"landscape\" at the source and target, including stack position and perhaps what variables are in what registers, and make whatever adjustments it needs just before (or less likely, just after) the jump. So `if (something) goto X;` doesn't necessarily end up as a conditional branch opcode targeting X, even if the CPU has one.","metadata":{"transformedAt":"2026-08-18T18:32:17.780Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":54,"estimatedTokens":1665}}267{"id":"stack-4863349","source":"stackoverflow","questionId":4863349,"title":"Is there a garbage collection algorithm that meets these requirements?","tags":["algorithm","compiler-construction","garbage-collection"],"text":"Title: Is there a garbage collection algorithm that meets these requirements?\nTags: algorithm, compiler-construction, garbage-collection\nSource: Stack Overflow\n\nQuestion:\nI am writing a compiler for a statically-typed object-oriented language. Currently I'm researching garbage collection algorithms to use. I'm wondering if there's a collector that is:\n\n- Open source and documented, so that I can implement it.\n\n- Acurrate\n\n- Generational\n\n- Global, i.e there is only one collector per process, as opposed to say one per thread.\n\n- Incremental and/or concurrent, to avoid long pauses from major collections.\n\n- Fits with this programming paradigm. An example of what doesn't would be a collector which becomes very slow in presence of destructive assignment.\n\nEdit: To clarify, I was wondering if there's an implementable *algorithm* that does this, not if there's an off-the-shelf collector.\n\n========================================\n\nTop Answer:\nThere's one not-at-all-experimental garbage collection algorithm that actually meets all your requirements: simple automatic refcounting. On the whole, refcounting really doesn't get enough credit as a viable option, but actually it works really nicely in many situations, there are never any big batch delays, and there's no need for complicated magic.\n\nOne concern is still cleaning up circular references, which you can at least leave to be done extremely rarely; app developers who care about speed can just explicitly break the loops when they need the objects to go away.\n\nA little-appreciated feature of refcounting is that it's much more dcache-friendly than other forms of garbage collection. If you're running a loop that allocates some small temporary objects every time through the loop, a refcounting GC (or explicit memory management, of course) can reuse the same memory each time, avoiding unnecessary cache flushes. Any other kind of GC would only free up the objects periodically, resulting in a much bigger memory footprint and therefore slowness.\n\nRefcounting is not very efficient for heavily multi-threaded systems, because you need to acquire locks every time you touch the refcount. But if you're designing a new language anyhow, there's one huge thing you can do to improve performance and reliability all over your language: prevent almost all objects from being shared between threads. ie. make sharing explicit. If you do that, you will know which objects are vs. aren't shared, and therefore which ones need to be locked when incrementing/decrementing the refcount and which can be left unlocked. When there isn't any locking, refcounting performance can be really excellent.\n\n========================================\n\nComments:\n- If you target the .NET or Java platform you will get one for free.\n- Here's a ridiculously good series of articles on garbage collection.\n- @Henk, he's writing a compiler\n- @Thomas Compilers can target the .NET or Java platforms\n- @Henk: That is true, but several things in my language don't really work on CLR or the JVM. For one thing I intend to not have null references. There is also multiple inheritance. Suffice it to say that compiling to any of those languages is not convenient.\n- There are a few implemented here: jikesrvm.org\n- Have you tried looking at this? vmkit.llvm.org\n- Even though you're not targetting the JVM, you could take a look at its garbage collector: it's open source, and there's been a lot of work invested in it.\n- @thomasmc: Right, and a GC is part of a runtime env.\n- @SK-Logic: I will definetly check that project out.\n- After some research I found that Mono's new collector is stop-the-world, so it doesn't meet the requisites.","metadata":{"transformedAt":"2026-08-18T18:32:17.780Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":46,"estimatedTokens":919}}268{"id":"stack-823138","source":"stackoverflow","questionId":823138,"title":"Understanding empty main()'s translation into assembly","tags":["c","gcc","compiler-construction","assembly"],"text":"Title: Understanding empty main()'s translation into assembly\nTags: c, gcc, compiler-construction, assembly\nSource: Stack Overflow\n\nQuestion:\nCould somebody please explain what GCC is doing for this piece of code? What is it initializing? The original code is:\n\n```\n#include \nint main()\n{\n\n}\n```\n\nAnd it was translated to:\n\n```\n.file \"test1.c\"\n .def ___main; .scl 2; .type 32; .endef\n .text\n.globl _main\n .def _main; .scl 2; .type 32; .endef\n_main:\n pushl %ebp\n movl %esp, %ebp\n subl $8, %esp\n andl $-16, %esp\n movl $0, %eax\n addl $15, %eax\n addl $15, %eax\n shrl $4, %eax\n sall $4, %eax\n movl %eax, -4(%ebp)\n movl -4(%ebp), %eax\n call __alloca\n call ___main\n leave\n ret\n```\n\nI would be grateful if a compiler/assembly guru got me started by explaining the stack, register and the section initializations. I cant make head or tail out of the code.\n\nEDIT: \nI am using gcc 3.4.5. and the command line argument is gcc -S test1.c\n\nThank You,\nkunjaan.\n\n========================================\n\nTop Answer:\nHere's a good step-by step breakdown of a simple `main()` function as compiled by GCC, with lots of detailed info: GAS Syntax (Wikipedia)\n\nFor the code you pasted, the instructions break down as follows:\n\n- First four instructions (pushl through andl): set up a new stack frame\n\n- Next five instructions (movl through sall): generating a weird value for eax, which will become the return value (I have no idea how it decided to do this)\n\n- Next two instructions (both movl): store the computed return value in a temporary variable on the stack\n\n- Next two instructions (both call): invoke the C library init functions\n\n- `leave` instruction: tears down the stack frame\n\n- `ret` instruction: returns to caller (the outer runtime function, or perhaps the kernel function that invoked your program)\n\n========================================\n\nCode:\n```text\n#include <stdio.h>\nint main()\n{\n\n}\n```\n\n```text\n.file   \"test1.c\"\n    .def    ___main;    .scl    2;  .type   32; .endef\n    .text\n.globl _main\n    .def    _main;  .scl    2;  .type   32; .endef\n_main:\n    pushl   %ebp\n    movl    %esp, %ebp\n    subl    $8, %esp\n    andl    $-16, %esp\n    movl    $0, %eax\n    addl    $15, %eax\n    addl    $15, %eax\n    shrl    $4, %eax\n    sall    $4, %eax\n    movl    %eax, -4(%ebp)\n    movl    -4(%ebp), %eax\n    call    __alloca\n    call    ___main\n    leave\n    ret\n```\n\n```text\nesp -----> 000a3   fa\n           000a4   21\n           000a5   66\n           000a6   23\nebp -----> 000a7   54\n```\n\n```text\nesp -----> 000a2   e4\n           000a3   fa\n           000a4   21\n           000a5   66\n           000a6   23\nebp -----> 000a7   54\n```\n\n```text\nmov  %ebp, %esp\npop  %ebp\n```\n\n```text\n1.    pushl       %ebp\n2.    movl        %esp, %ebp\n3.    subl        $8, %esp\n4.    andl        $-16, %esp\n5.    movl        $0, %eax\n6.    addl        $15, %eax\n7.    addl        $15, %eax\n8.    shrl        $4, %eax\n9.    sall        $4, %eax\n10.   movl        %eax, -4(%ebp)\n11.   movl        -4(%ebp), %eax\n12.   call        __alloca\n13.   call        ___main\n14.   leave\n15.   ret\n```\n\n```text\npushl %ebp\n```\n\n```text\npushl   %ebp\n    movl    %esp, %ebp\n    subl    $8, %esp\n```\n\n```text\nandl    $-16, %esp\n```\n\n```text\nmovl    $0, %eax\n    movl    %eax, -4(%ebp)\n    movl    -4(%ebp), %eax\n```\n\n```text\ncall    __alloca\n    call    ___main\n```\n\n```text\nmain()\n```\n\n```text\nleave\n```\n\n```text\nret\n```\n\n```text\ngcc -S emptymain.c\n```\n\n```text\n.file   \"emptymain.c\"\n        .text\n.globl _qqq\n        .def    _qqq;      .scl    2;      .type   32;     .endef\n_qqq:\n        pushl   %ebp\n        movl    %esp, %ebp\n        popl    %ebp\n        ret\n```\n\n```text\nmain()\n```\n\n```text\nqqq()\n```\n\n```text\nmain()\n```\n\n```text\npushl       %ebp\nmovl        %esp, %ebp\n```\n\n```text\nsubl        $8, %esp\nandl        $-16, %esp\n```\n\n```text\nmovl        $0, %eax\naddl        $15, %eax\naddl        $15, %eax\nshrl        $4, %eax\nsall        $4, %eax\nmovl        %eax, -4(%ebp)\nmovl        -4(%ebp), %eax\n```\n\n```text\ncall        __alloca\n```\n\n```text\ncall        ___main\n```\n\n```text\nleave\n```\n\n```text\nret\n```\n\n```text\n.file   \"main.c\"\n        .text\n        .p2align 4,,15\n.globl main\n        .type   main, @function\nmain:\n        leal    4(%esp), %ecx\n        andl    $-16, %esp\n        pushl   -4(%ecx)\n        pushl   %ebp\n        movl    %esp, %ebp\n        pushl   %ecx\n        popl    %ecx\n        popl    %ebp\n        leal    -4(%ecx), %esp\n        ret\n        .size   main, .-main\n        .ident  \"GCC: (Debian 4.3.2-1.1) 4.3.2\"\n        .section        .note.GNU-stack,\"\",@progbits\n```\n\n```text\n.file   \"main.c\"\n```\n\n```text\n.text\n```\n\n```text\n.p2align 4,,15\n```\n\n```text\n.globl main\n        .type   main, @function\n```\n\n```text\nmain:\n```\n\n```text\nleal    4(%esp), %ecx\n        andl    $-16, %esp\n        pushl   -4(%ecx)\n```\n\n```text\npushl   %ebp\n        movl    %esp, %ebp\n```\n\n```text\npushl   %ecx\n        popl    %ecx\n```\n\n```text\npopl    %ebp\n        leal    -4(%ecx), %esp\n```\n\n```text\nret\n```\n\n```text\n.size   main, .-main\n        .ident  \"GCC: (Debian 4.3.2-1.1) 4.3.2\"\n        .section        .note.GNU-stack,\"\",@progbits\n```\n\n```text\nint f(void) {\n  return 17;\n}\n```\n\n```text\n.file   \"f.c\"\n        .text\n        .p2align 4,,15\n.globl f\n        .type   f, @function\nf:\n        pushl   %ebp\n        movl    $17, %eax\n        movl    %esp, %ebp\n        popl    %ebp\n        ret\n        .size   f, .-f\n        .ident  \"GCC: (Debian 4.3.2-1.1) 4.3.2\"\n        .section        .note.GNU-stack,\"\",@progbits\n```\n\n```text\nmain\n```\n\n```text\nalloca\n```\n\n```text\nmain\n```\n\n```text\nmain\n```\n\n```text\nmain\n```\n\n```text\nmain\n```\n\n========================================\n\nComments:\n- What was your compile command-line?\n- .. and what version of GCC are you using?\n- I love the sheer simplicity of this question.\n- I am using gcc 3.4.5. which do you recommend?\n- what are those .scl 2; .type 32; .endef after main btw?","metadata":{"transformedAt":"2026-08-18T18:32:17.780Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":46,"totalLines":373,"estimatedTokens":1472}}269{"id":"stack-40430578","source":"stackoverflow","questionId":40430578,"title":"Static Semantics meaning?","tags":["syntax","static","compiler-construction","semantics"],"text":"Title: Static Semantics meaning?\nTags: syntax, static, compiler-construction, semantics\nSource: Stack Overflow\n\nQuestion:\nWhat does term \"static semantics\" mean in programming?\n\nWhat is relationship between static semantics, semantics and dynamic semantics?\n\nI know that semantics stands for checking if written code (without syntax errors) has any meaning.\n\n========================================\n\nTop Answer:\nStatic properties of a program are those that the compiler can detect at compile time.\n\nOnly **Static Semantic** errors can be detected by the compiler at compile time.\n\n**Dynamic Semantic** errors can only be detected upon execution, not by the compiler prior to execution.\n\n========================================\n\nComments:\n- Great response! Thanks.\n- It means nothing of the sort. Grammar rules are about syntax, not semantics. Don't post guesswork here.","metadata":{"transformedAt":"2026-08-18T18:32:17.780Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":218}}270{"id":"stack-5048921","source":"stackoverflow","questionId":5048921,"title":"Different meanings of parentheses in C++","tags":["c++","parsing","compiler-construction","parentheses"],"text":"Title: Different meanings of parentheses in C++\nTags: c++, parsing, compiler-construction, parentheses\nSource: Stack Overflow\n\nQuestion:\nI am a bit confused with the interpretation of parentheses by the compiler. Can some one please explain what actually happens in such contexts?\n\nCasting: `(int)a` or `int(a)`\n\nParameter passing:\n\n```\ntemplate \nint size(t (&)[n]){return n;}\n```\n\nObviously there could be many different contexts where parentheses change the meaning or interpretation. Can some one please explain what exactly is happening behind the curtain? How does the compiler know how to interpret in each context? Is there a general guideline or is it a specific rule for each case?\n\n========================================\n\nTop Answer:\ncasting (int)a or int(a) \n\n(int)a is a cast\n\nint(a) is the construction of an int, passing in a to the int ctor\n\nExpressions are evaluated according to operators' precedence, arity, and whether the operator is right or left associative. Read the operator precedence chart in your C++ text.\n\nGet a copy of the program c++decl; it reads C++ expressions and outputs an English langauge explanation of the expression. Or read this explanation.\n\n========================================\n\nCode:\n```text\ntemplate <typename t>\nint size(t (&)[n]){return n;}\n```\n\n```text\n(int)a\n```\n\n```text\nint(a)\n```\n\n```text\nint(value)\n```\n\n```text\nint(value)\n```\n\n```text\n(int)value\n```\n\n```text\ntemplate <typename T, size_t N>\nsize_t (T (&)[N]) {\n    return N;\n}\n```\n\n```text\ntemplate <typename T, size_t N>\nsize_t (T (&array)[N]) {\n    return N;\n}\n```\n\n```text\nT (&array)[N]\n```\n\n```text\nT& array[N]\n```\n\n```text\nvoid (*functionPointer)()\n```\n\n```text\nvoid *functionPointer()\n```\n\n```text\nvector<int> v();\n```\n\n```text\nvector<int> v(10);\n```\n\n```text\nN\n```\n\n```text\nT\n```\n\n```text\nN\n```\n\n```text\nT (&)[N]\n```\n\n```text\nT (&)[N]\n```\n\n```text\narray\n```\n\n```text\nT\n```\n\n```text\nN\n```\n\n```text\narray\n```\n\n```text\nN\n```\n\n```text\nT&\n```\n\n```text\n*\n```\n\n```text\nfunctionPointer\n```\n\n```text\nvoid *\n```\n\n```text\nvector<int>\n```\n\n```text\nv\n```\n\n```text\nv\n```\n\n```text\nvector<int>\n```\n\n```text\nvector<int>\n```\n\n```text\n10\n```\n\n```text\nT (&array)[N]\n```\n\n```text\nT(value)\n```\n\n```text\n(T)value\n```\n\n```text\nT (&array)[N]\n```\n\n```text\n&\n```\n\n```text\nT\n```\n\n```text\narray\n```\n\n```text\n§A.14 Preprocessing directives\ncontrol-line: # define identifier lparen identifier-list_opt ) replacement-list new-line\ncontrol-line: # define identifier lparen ... ) replacement-list new-line\ncontrol-line: # define identifier lparen identifier-list , ... ) replacement-list new-line\n\n§A.2 Lexical conventions\nraw-string: \" d-char-sequence_opt ( r-char-sequence_opt ) d-char-sequence_opt \"\n\n§A.4 Expressions\nprimary-expression: ( expression )\nlambda-declarator: ( parameter-declaration-clause ) mutable_opt exception-specification_opt attribute-specifier-seq_opt trailing-return-type_opt\npostfix-expression: const_cast < type-id > ( expression )\npostfix-expression: dynamic_cast < type-id > ( expression )\npostfix-expression: postfix-expression ( expression-list_opt )\npostfix-expression: reinterpret_cast < type-id > ( expression )\npostfix-expression: simple-type-specifier ( expression-list_opt )\npostfix-expression: static_cast < type-id > ( expression )\npostfix-expression: typeid ( expression )\npostfix-expression: typeid ( type-id )\npostfix-expression: typename-specifier ( expression-list_opt )\nunary-expression: alignof ( type-id )\nunary-expression: sizeof ( type-id )\nunary-expression: sizeof ... ( identifier )\nnew-expression: ::_opt new new-placement_opt ( type-id ) new-initializer_opt\nnew-placement: ( expression-list )\nnew-initializer: ( expression-list_opt )\nnoexcept-expression: noexcept ( expression )\ncast-expression: ( type-id ) cast-expression\n\n§A.5 Statements\nselection-statement: if ( condition ) statement\nselection-statement: if ( condition ) statement else statement\nselection-statement: switch ( condition ) statement\niteration-statement: do statement while ( expression ) ;\niteration-statement: for ( for-init-statement condition_opt ; expression_opt ) statement\niteration-statement: for ( for-range-declaration : for-range-initializer ) statement\niteration-statement: while ( condition ) statement\n\n§A.6 Declarations\nstatic_assert-declaration: static_assert ( constant-expression , string-literal ) ;\ndecltype-specifier: decltype ( auto )\ndecltype-specifier: decltype ( expression )\nasm-definition: asm ( string-literal ) ;\nalignment-specifier: alignas ( assignment-expression ..._opt )\nalignment-specifier: alignas ( type-id ..._opt )\nattribute-argument-clause: ( balanced-token-seq )\nbalanced-token: ( balanced-token-seq )\n\n§A.7 Declarators\nnoptr-declarator: ( ptr-declarator )\nparameters-and-qualifiers: ( parameter-declaration-clause ) attribute-specifier-seq_opt cv-qualifier-seq_opt ref-qualifier_opt exception-specification_opt\nnoptr-abstract-declarator: ( ptr-abstract-declarator )\ninitializer: ( expression-list )\n\n§A.10 Special member functions\nmem-initializer: mem-initializer-id ( expression-list_opt )\n\n§A.11 Overloading\noperator-function-id: operator ( )\n\n§A.13 Exception handling\nhandler: catch ( exception-declaration ) compound-statement\ndynamic-exception-specification: throw ( type-id-list_opt )\nnoexcept-specification: noexcept ( constant-expression )\n```\n\n```text\nif-group\n```\n\n```text\nelif-group\n```\n\n```text\nconstant-expression\n```\n\n```text\nlparen\n```\n\n```text\n(\n```\n\n```text\nraw-string\n```\n\n```text\n(\n```\n\n```text\n)\n```\n\n```text\ncast-expression: ( type-id ) cast-expression\n```\n\n```text\npostfix-expression: simple-type-specifier ( expression-list_opt )\n```\n\n```text\nparameters-and-qualifiers: ( parameter-declaration-clause ) attribute-specifier-seq_opt cv-qualifier-seq_opt ref-qualifier_opt exception-specification_opt\n```\n\n```text\nnoptr-abstract-declarator: ( ptr-abstract-declarator )\n```\n\n========================================\n\nComments:\n- Thats true. But this doesnt apply to the second case. How does the compiler interpret in that case? Thx\n- Also cdecl.org is useful but doesn't fully support C++. (As a test, it described a reference correctly, but said it's unsupported in C).","metadata":{"transformedAt":"2026-08-18T18:32:17.780Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":54,"totalLines":325,"estimatedTokens":1535}}271{"id":"stack-688386","source":"stackoverflow","questionId":688386,"title":"Is C++ converted into CIL?","tags":[".net","c++","compiler-construction","cil"],"text":"Title: Is C++ converted into CIL?\nTags: .net, c++, compiler-construction, cil\nSource: Stack Overflow\n\nQuestion:\nI have been a long time C# and .NET developer, and have been playing with the idea of learning C++.\n\nOne of the primary reasons I have been thinking about this, is how much faster C++ can be over apps using .NET. But am I right in assuming that if I write a C++ app in Visual Studio, and/or reference .NET libraries in a C++ application that, that C++ is converted in CIL (just like C#) - and therefore I'd lose any benefit from coding in it?\n\nSo my question is really this: are C++ components of an application referencing .NET assemblies compiled in the \"traditional\" way, or compiled into CIL?\n\n========================================\n\nTop Answer:\nIt's probably a good idea to keep the concepts separate.\n\nFirst, C++ is a language, and it doesn't specify anything about what platform should be targeted. In principle, straight C++ code could be compiled to native x86 assembler, Java bytecode, MSIL or anything else you care to think of. I believe Adobe recently made a C++ compiler which generates Flash bytecode.\n\nSecond, with typical indecisiveness, Microsoft has created two C++-derived languages targeting .NET. First, they made the \"managed extensions for C++\". Then they decided it sucked, ditched it and tried to pretend it never existed.\n\nNow their best bet for .NET-style C++ is called C++/CLI, but *it is not C++*. It extends and changes the language in a number of nonstandard ways. (And I believe the C++ standard committee requested that they change the name to avoid confusion. But they didn't)\n\nVisual Studio 2005 and newer supports C++/CLI. (in \"Add project\", they're listed under Visual C++ -> CLR)\n\n*However* (you didn't think it was that simple, did you?), Microsoft has done it again. After specifying C++/CLI, which is actually a reasonably well-designed attempt at integrating C++ with CLI, they realized that virtually no one uses it!\nTurns out that even C++ programmers generally prefer to use C# when they're working in .NET, and proper, native C++ otherwise. \n\nSo now, they're focusing on making interop between *native* C++ and .NET simpler and more powerful. However, C++/CLI isn't likely to go away. It works, and in some cases it's useful. It's just not the C++-killer they originally hoped for.\n\nVisual Studio (since forever) also supports native C++ applications, compiled to x86 machine code, untainted by .NET. These are listed in the \"Add Project\" dialog under Visual C++ -> Win32.\n\nSo if you want to learn C++, you have two choices:\nLearn C++/CLI, which limits you to a MS-only language which yes, generates MSIL instead of native machine code, and requires .NET to run, and generally isn't worth the bother because if you're going to take a dependency on .NET *anyway*, why not write in C#?\n\nOr learn proper C++, which is completely separate from .NET and can't directly reference .NET assemblies.\n\nThe key takeaway point is that they're separate languages. Either you compile as C++/CLI, which means the compiler will allow you to reference .NET assemblies, and will generate MSIL code, or you compile as C++, in which case the .NET world doesn't exist.\n\nAnd finally, a note of caution. Despite my wording above (\"proper C++\" and \"untainted by .NET\"), C++ isn't \"better\". In many cases, it is not faster either. C++ has the *potential* to be faster, but it depends a lot more on the programmer.\n\nThe C# compiler will turn pretty much anything into reasonably efficient code.\nC++ on the other hand, is full of pitfalls that will make your code *slower* than the equivalent C#.\n\nhttp://blogs.msdn.com/ricom/archive/2005/05/10/416151.aspx and the blog posts it references are worth a read for anyone curious about the performance of similar code written in the two languages.\n\nThere is only one area where C++ applications will be consistently faster, and that's in startup time. a .NET application may have to load the .NET framework and JIT the MSIL code, where a native application... just starts.\n\nBut other than that, it is probably a mistake to assume that C++ will be faster. It *can* be, because it gives you a bit more control. But usually, that just means the compiler is less able to save you from the inefficiencies you create in your code.","metadata":{"transformedAt":"2026-08-18T18:32:17.780Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":48,"estimatedTokens":1076}}272{"id":"stack-453870","source":"stackoverflow","questionId":453870,"title":"Suggestions for writing a programming language?","tags":["compiler-construction","programming-languages","interpreter","scripting-language"],"text":"Title: Suggestions for writing a programming language?\nTags: compiler-construction, programming-languages, interpreter, scripting-language\nSource: Stack Overflow\n\nQuestion:\nWhat tips can you give a person who is looking to write a programming or script language? I\nam not worried about how to program nor design a compiler but how to develop one quickly using tools and code generators.\n\nLast time i tried i coded it in c++ and the states and syntax took almost as long as writing the actual logic. I know the tools would help.\n\nI was thinking i could generate c++ code and have gcc compile that. Using the tools above how long would you estimate it would take to write a program or script language?\n\nVariations on this question have been asked repeatedly, as far back as Learning to write a compiler. Here is an incomplete list of SO resources on the topic.\n\n========================================\n\nTop Answer:\nDave Hanson, who with Chris Fraser spent 10 years building one of the world's most carefully crafted compilers, told me once that one of the main things he learned from the experience was not to try to write a compiler in C or C++. \n\nIf you want to develop something quickly, don't generate native code; target an existing virtual machine such as the CLR, JVM, or the Lua virtual machine. Generate code using maximal munch.\n\nAnother good option if you're writing an interpreter is just to use the memory management and other facilities of your underlying programming language. Parse to an AST and then interpret by tree walk of the AST. This will get you off the ground fast. Performance is not the greatest, but it's acceptable. (Using this technique I once wrote a PostScript interpreter in Modula-3. The first implementation took a week and although it later underwent some performance tuning, primarily in the lexer, it never had to be replaced.)\n\nAvoid LALR parser generators; use something that saves your time, like ANTLR or the Elkhound GLR parser generator.\n\n========================================\n\nComments:\n- I've tried cleaning up this question.. I might hack it up further.\n- Alright, I've re-focused the title.. and removed extraneous comments in the question. I think it is a better question now.\n- Your \"refocusing\" removed information like the mention of antlr, and the idea that it would compile to C++. I think you might have edited too much.\n- @Ned Batchelder, removing ANTLR was intentional. He listed off 5 programming languages that he had \"heard\" of, and then stated that he had never used them before. They weren't used in reference to his question about writing a language and didn't add to his question.\n- possible duplicate of Creating your own language\n- ok, fine. :p I'm just saying it 'cause I spent several weeks writing a parser for a crude scripting language, but figured out later that I could've just used Javascript or Python by writing my object model and exposing it to an existing scripting language. Who wants to learn a new language?\n- +1, we already have way too many programming languages. If it's really necessary, there are frameworks to create domain specific languages.\n- @WimCoenen With that kind of thinking, maybe the world should have stop improving buildings, cars, phones, planes, weapons, etc. If a 1,000 people do the same thing, at least 1 person will eventually venture off to see how to improve it. That's called progress.\n- @SpicyWeenie: my \"kind of thinking\" is that it is inefficient for everybody to build their own car. Inefficiency impedes progress.\n- @WimCoenen It's illogical and senseless to think everyone wants to build their own car. Some like the design concepts, manufacturing, rebuilding, troubleshooting, maintenance and just plain driving it. How many times have the light bulb and phone been improved on? That would count as inefficient by your process. It the world were to your model, we would still be on horses throwing rocks and beating each other with sticks. Your model doesn't promote ingenuity nor creativity, because everything we take for granted stems from an older idea repeated done beforehand.\n- @SpicyWeenie: \"It's illogical and senseless to think everyone wants to build their own car.\" Exactly. Leave the car building to the guys who have the means to assemble a team of experts and innovate, like Elon Musk did with Tesla Motors.\n- @WimCoenen Except ... writing a compiler is not analogous to building a car. You don't need tons of raw material, rented space, endless financial capital, and a team of specific engineers just to buid one prototype, and then mass produce it if the design is defensible. This is a compiler. This is not Tesla motors.\n- I take issue with your perspective, especially in light of it being a 5-year-old answer. OP said (and my emphasis): \"I am not worried about how to program nor design a compiler but how to develop one **quickly** using tools and code generators.\" This implies an emphasis of using it to get something else done, not focusing on the joy or learning of programming language design itself. Having done something similar at the time, I shared my experience. I found it MUCH easier to implement in Java than C++.\n- Is there more about the Postscript interpreter? (I have a collection.)\n- @droog it's part of the source of ldb","metadata":{"transformedAt":"2026-08-18T18:32:17.780Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":43,"estimatedTokens":1321}}273{"id":"stack-8993426","source":"stackoverflow","questionId":8993426,"title":"What are C# lambda's compiled into? A stackframe, an instance of an anonymous type, or?","tags":["c#","compiler-construction","lambda"],"text":"Title: What are C# lambda's compiled into? A stackframe, an instance of an anonymous type, or?\nTags: c#, compiler-construction, lambda\nSource: Stack Overflow\n\nQuestion:\nWhat are C# lambda's compiled into? A stackframe, an instance of an anonymous type, or?\n\nI've read this question. Which mostly answers \"why\" you can't use a lambda when also using implicit type features. But, this question is aimed at answering what construct the compiler produces to actually carry out the code of a lambda. Is it a method call of an anonymous type (something like anonymous types that implement an interface in Java?) or is it just a stack frame with references to closed variables and the accepting the parameter signature? Some lambda's don't close over anything -- so are there then 2 different resulting outputs from the compile.\n\n========================================\n\nTop Answer:\nLambda expressions are indeed anonymous functions, but with more versatility. These two articles authored by the MSDN have a lot of information on lambda expressions, how to use them, what precedence the operator `=>` has, what their relation to anonymous functions are, and some advanced suggestions of use.\n\nLambda Expressions (MSDN)\n\n=> Operator (MSDN)\n\n========================================\n\nCode:\n```text\nint i = ...\nFunc<int,int> func = x => 2*x*i;\n```\n\n```text\nvar capture = new SecretType();\ncapture.i = ...\nFunc<int,int> func = capture.SecretMethod;\n```\n\n```text\nclass SecretType {\n    public int i;\n    public int SecretMethod(int x) { return 2*x*i; }\n}\n```\n\n```text\nint i = ...\nExpression<Func<int,int>> func = x => 2*x*i;\n```\n\n```text\nvar capture = new SecretType();\ncapture.i = ...\nvar p = Expression.Parameter(\"x\", typeof(int));  \nExpression<Func<int,int>> func = Expression.Lambda<Func<int,int>>(\n    Expression.Multiply(\n        Expression.Multiply(Expression.Constant(2),p),\n        Expression.PropertyOrField(Expression.Constant(capture), \"i\")\n    ), p);\n```\n\n```text\n=>\n```\n\n```text\npublic class C\n{\n    private int field = 0;\n\n    public void M()\n    {\n        int local = 0;\n\n        Func<int> f1 = () => 0;\n        // f1 is a delegate that references a compiler-generated static method in C\n\n        Func<int> f2 = () => this.field;\n        // f2 is a delegate that references a compiler-generated instance method in C\n\n        Func<int> f3 = () => local;\n        // f3 is a delegate that references an instance method of a compiler-generated nested class in C\n    }\n}\n```\n\n```text\nExpression<TDelegate>\n```\n\n========================================\n\nComments:\n- There are actually two completely different implementations - is it a delegate? Or an expression-tree?\n- Probably an expression tree is what I'm after here: (x) => x.ToString() something like that. I'm guessing a delegate is actually an instance of a type because of the +=, and what not.\n- @lucidquier the syntax is identical; you can't tell what it is unless you know the type of the assignment or parameter type.\n- Jon has excellent coverage of this topic in C# in Depth - it isn't trivial, so I do recommend: read the chapter in there.","metadata":{"transformedAt":"2026-08-18T18:32:17.780Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":91,"estimatedTokens":776}}274{"id":"stack-2095277","source":"stackoverflow","questionId":2095277,"title":"Difference between C++ and Java compilation process","tags":["java","c++","compiler-construction"],"text":"Title: Difference between C++ and Java compilation process\nTags: java, c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI searched in google for the differences between C++ and Java compilation process, but C++ and Java language features and their differences are returned.\n\nI am proficient in Java, but not in C++. But I fixed few bugs in C++. From my experience, I noticed that C++ always took more time to build compared to Java for minor changes.\n\n========================================\n\nTop Answer:\nJava compiles code into bytecode, which is interpreted by the Java VM. C++ must compile into object code, then to machine language. Because of this, it's possible for Java to compile only a single class for minor changes, while C++ object files must be re-linked with other object files to machine code executable (or DLLs). This may make the process take a bit longer.\n\n========================================\n\nComments:\n- Read jalf's answer here: stackoverflow.com/questions/318398/&hellip;\n- That's a QoI (quality of implementation) issue. Some C++ compilers implement \"precompiled headers\", which makes the build much, much faster.\n- Same question as stackoverflow.com/questions/318398/&hellip;\n- Certainly a dupe and it should be closed; but don't delete it because this title will come up on a very different set of searches.","metadata":{"transformedAt":"2026-08-18T18:32:17.781Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":338}}275{"id":"stack-18965870","source":"stackoverflow","questionId":18965870,"title":"#error \"Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h\"","tags":["gcc","compiler-construction","llvm","bison","flex-lexer"],"text":"Title: #error \"Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h\"\nTags: gcc, compiler-construction, llvm, bison, flex-lexer\nSource: Stack Overflow\n\nQuestion:\nI have been trying to the tutorial at http://gnuu.org/2009/09/18/writing-your-own-toy-compiler/5/ (using flex, bison and llvm) but when typing the line\n\n g++ -o parser parser.cpp tokens.cpp main.cpp\n\nI get the following errors:\n\n```\nIn file included from /usr/local/include/llvm/Support/PointerLikeTypeTraits.h:18:0,\n from /usr/local/include/llvm/ADT/PointerIntPair.h:17,\n from /usr/local/include/llvm/IR/Use.h:28,\n from /usr/local/include/llvm/IR/Value.h:17,\n from node.h:3,\n from parser.y:2:\n/usr/local/include/llvm/Support/DataTypes.h:48:3: erreur: #error \"Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h\"\n/usr/local/include/llvm/Support/DataTypes.h:52:3: erreur: #error \"Must #define __STDC_CONSTANT_MACROS before \" \"#including Support/DataTypes.h\"\nparser.y: In function ‘void yyerror(const char*)’:\nparser.y:6:58: erreur: ‘printf’ was not declared in this scope\nIn file included from /usr/local/include/llvm/Support/PointerLikeTypeTraits.h:18:0,\n from /usr/local/include/llvm/ADT/PointerIntPair.h:17,\n from /usr/local/include/llvm/IR/Use.h:28,\n from /usr/local/include/llvm/IR/Value.h:17,\n from node.h:3,\n from tokens.l:3:\n/usr/local/include/llvm/Support/DataTypes.h:48:3: erreur: #error \"Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h\"\n/usr/local/include/llvm/Support/DataTypes.h:52:3: erreur: #error \"Must #define __STDC_CONSTANT_MACROS before \" \"#including Support/DataTypes.h\"\nIn file included from /usr/local/include/llvm/Support/PointerLikeTypeTraits.h:18:0,\n from /usr/local/include/llvm/ADT/PointerIntPair.h:17,\n from /usr/local/include/llvm/IR/Use.h:28,\n from /usr/local/include/llvm/IR/Value.h:17,\n from node.h:3,\n from main.cpp:2:\n/usr/local/include/llvm/Support/DataTypes.h:48:3: erreur: #error \"Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h\"\n/usr/local/include/llvm/Support/DataTypes.h:52:3: erreur: #error \"Must #define __STDC_CONSTANT_MACROS before \" \"#including Support/DataTypes.h\"\n```\n\nI have seen lots of posts like this on the internet, and most of the answers include defining these constants on the command line or by using gcc Makefile.\n\nI don't understand how to do that, could someone help me out?\n\n========================================\n\nTop Answer:\nBased on the docs here, you should be able to fix the problem by adding the following command line options:\n\n```\n-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS\n```\n\nAfterwards, there might be some other errors:\n\n parser.o: In function `NInteger::NInteger(long long)':\n parser.cpp:(.text._ZN8NIntegerC2Ex[_ZN8NIntegerC5Ex]+0x23): undefined reference to`vtable for NInteger'\n parser.o: In function `NDouble::NDouble(double)':\n parser.cpp:(.text._ZN7NDoubleC2Ed[_ZN7NDoubleC5Ed]+0x24): undefined reference to`vtable for NDouble'\n\nTry to implement every **codeGen** in every class without `llvm` stuff (i.e., change `node.h`). Then you will be able to compile and run the tutorial.\n\nBy the way, when you compile the code, you might want to use `llvm-config` command the get the option instead of using `-D` option:\n\n```\ng++ -c `llvm-config --cppflags` xxxx.cpp\n```\n\n========================================\n\nCode:\n```text\nIn file included from /usr/local/include/llvm/Support/PointerLikeTypeTraits.h:18:0,\n                 from /usr/local/include/llvm/ADT/PointerIntPair.h:17,\n                 from /usr/local/include/llvm/IR/Use.h:28,\n                 from /usr/local/include/llvm/IR/Value.h:17,\n                 from node.h:3,\n                 from parser.y:2:\n/usr/local/include/llvm/Support/DataTypes.h:48:3: erreur: #error \"Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h\"\n/usr/local/include/llvm/Support/DataTypes.h:52:3: erreur: #error \"Must #define __STDC_CONSTANT_MACROS before \" \"#including Support/DataTypes.h\"\nparser.y: In function ‘void yyerror(const char*)’:\nparser.y:6:58: erreur: ‘printf’ was not declared in this scope\nIn file included from /usr/local/include/llvm/Support/PointerLikeTypeTraits.h:18:0,\n                 from /usr/local/include/llvm/ADT/PointerIntPair.h:17,\n                 from /usr/local/include/llvm/IR/Use.h:28,\n                 from /usr/local/include/llvm/IR/Value.h:17,\n                 from node.h:3,\n                 from tokens.l:3:\n/usr/local/include/llvm/Support/DataTypes.h:48:3: erreur: #error \"Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h\"\n/usr/local/include/llvm/Support/DataTypes.h:52:3: erreur: #error \"Must #define __STDC_CONSTANT_MACROS before \" \"#including Support/DataTypes.h\"\nIn file included from /usr/local/include/llvm/Support/PointerLikeTypeTraits.h:18:0,\n                 from /usr/local/include/llvm/ADT/PointerIntPair.h:17,\n                 from /usr/local/include/llvm/IR/Use.h:28,\n                 from /usr/local/include/llvm/IR/Value.h:17,\n                 from node.h:3,\n                 from main.cpp:2:\n/usr/local/include/llvm/Support/DataTypes.h:48:3: erreur: #error \"Must #define __STDC_LIMIT_MACROS before #including Support/DataTypes.h\"\n/usr/local/include/llvm/Support/DataTypes.h:52:3: erreur: #error \"Must #define __STDC_CONSTANT_MACROS before \" \"#including Support/DataTypes.h\"\n```\n\n```text\n-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS\n```\n\n```text\n-D\n```\n\n```text\n-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS\n```\n\n```text\ng++ -c `llvm-config --cppflags`  xxxx.cpp\n```\n\n```text\nNInteger::NInteger(long long)':\n  parser.cpp:(.text._ZN8NIntegerC2Ex[_ZN8NIntegerC5Ex]+0x23): undefined reference to\n```\n\n```text\nNDouble::NDouble(double)':\n  parser.cpp:(.text._ZN7NDoubleC2Ed[_ZN7NDoubleC5Ed]+0x24): undefined reference to\n```\n\n```text\nllvm\n```\n\n```text\nnode.h\n```\n\n```text\nllvm-config\n```\n\n```text\n-D\n```\n\n========================================\n\nComments:\n- @Oak Unfortunately I still get the errors after adding this command line option. Any clues? I'm on OSX.\n- @gideon sorry, no. You better ask on LLVM-dev.","metadata":{"transformedAt":"2026-08-18T18:32:17.781Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":147,"estimatedTokens":1518}}276{"id":"stack-751186","source":"stackoverflow","questionId":751186,"title":"Forcing the .NET JIT compiler to generate the most optimized code during application start-up","tags":["c#",".net","optimization","compiler-construction","jit"],"text":"Title: Forcing the .NET JIT compiler to generate the most optimized code during application start-up\nTags: c#, .net, optimization, compiler-construction, jit\nSource: Stack Overflow\n\nQuestion:\nI'm writing a DSP application in C# (basically a multitrack editor). I've been profiling it for quite some time on different machines and I've noticed some 'curious' things. \n\nOn my home machine, the first run of the playback loop takes up about 50%-60% of the available time, (I assume it's due to the JIT doing its job), then for the subsequent loops it goes down to a steady 5% consumption. The problem is, if I run the application on a slower computer, the first run takes up more than the available time, causing the playback to get interrupted and messing the output audio, which is unacceptable. After that, it goes down to a 8%-10% consumption.\n\nEven after the first run, the application keeps calling some time-consuming routines from time to time (every 2 seconds more or less), which causes the steady 5% consumption to experience very short peaks of 20%-25%. I've noticed that if I let the application run for a while these peaks will also go down to a 7%-10%. (I'm not sure if it's due to the JIT recompiling these portions of code).\n\nSo, I have a serious problem with the JIT. While the application will behave nicely even in very slow machines, these 'compiling storms' are going to be a big problem. I'm trying to figure out how to resolve this issue and I've come up with an idea, which is to mark all the 'sensible' routines with an attribute that will tell the application to 'squeeze' them beforehand during start-up, so they'll be fully optimized when they're really needed. But this is only an idea (and I don't like it too much either) and I wonder if there's a better solution to the whole problem.\n\nI'd like to hear what you guys think.\n\n(NGEN the application is not an option, I like and want all the JIT optimizations I can get.)\n\nEDIT:\n\nMemory consumption and garbage collection kicks are not an issue, I'm using object pools and the maximum peak of memory during playback is 304 Kb.\n\n========================================\n\nTop Answer:\nThe initial speed indeed sounds like Fusion+JIT, which would be helped by ILMerge (for Fusion) and NGEN (for JIT); you could always play a silent track through the system at startup so that this does all the hard work without the user noticing any distortion?\n\nNGEN is a good option; is there a *reason* you can't use it?\n\nThe issues you mention *after* the initial load do **not** sound like they are related to JIT. Perhaps garbage collection.\n\nHave you tried profiling? Both CPU and memory (collections)?\n\n========================================\n\nCode:\n```text\nPrepareMethod\n```\n\n```text\nNGen\n```\n\n========================================\n\nComments:\n- It would help if you said **why** NGEN is not an option; understanding the problem is half the job...\n- What JIT optimizations are not available when NGen-ing?\n- @Marc: I'd NGen the app as a last resort, but I'd rather to not using it if there's a better solution available.\n- Then first you need to prove what the problem is. The ongoing pauses are nothing to do with JIT. JIT has no part to play once code has been visited at least once. You are going to have to profile....\n- @Marc: Is not the JIT able to recompile some portions of code if they're executed a certain number of times?\n- You do realise that NGEN *is* JIT? It's just the jitter running over the code, and saving it, rather than doing it at runtime. There is NO difference. Also: There is no \"quick and nasty\" JIT vrs \"slower and better\" JIT. Just JIT. And you can't tweak it.\n- Well, it's not the same actually. By using NGEN you lose optimizations based on runtime information, and that's the only reason I'm trying to avoid using it.\n- @Trap: No, there's no runtime profiling going on in the .NET JITer.\n- @dthorpe: I'm not sure that's correct. One of the things the JIT can do is replace property calls with direct access to the underlying field, but this can only be done during runtime, not at NGEN time.\n- @Steven: Replacing indirects with direct references doesn't require execution profiling. I think what Trap is referring to is the Java Hotspot optimizer, which hasn't made an appearance in the .NET space AFAIK\n- @dthorpe: Are you sure? The JIT can't tell whether the property is a thin, inlineable wrapper until that assembly is loaded, so if the calling assembly is NGENed, it would necessarily have to call the non-inlined property.\n- @Steven: Yes, you are correct: NGEN may codegen indirect references where the inline JIT will not. Note that NGEN in .NET 2.0 and later eliminates many of the indirections that NGEN in .NET 1.x was infamous for. This still doesn't change my statement: the .NET JIT compiler does not perform *recompilation of already compiled code* based on execution profiling. What Trap asked in the comment above was whether the .NET JIT can reoptimize/recompile to native code after the code has executed a few times. I'm not aware of anything like that in .NET.\n- @dthorpe: Yes, we're on the same page now. Once code is compiled, by JIT or NGEN, it stays compiled. There's no recompiling to optimize based on profiling.\n- For me the the only reason not to use ngen would be that it requires administrative privileges during installation, thus not being applicable to per-user installations on corporate networks. Maybe that is the reason for the OP as well?\n- Why not do NGEN as an option on install then? So if you have admin rights, do it. Otherwise warn the user and dont do it.\n- Interesting. I had no clue that denormals were such a monstrous speed bump.\n- Forcing JIT compilation without actually executing the methods is genius! Thank you!","metadata":{"transformedAt":"2026-08-18T18:32:17.781Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":63,"estimatedTokens":1442}}277{"id":"stack-5419698","source":"stackoverflow","questionId":5419698,"title":"What alternative syntax exist for C/C++? (think SPECS or Mirah)","tags":["c++","c","compiler-construction","syntax","code-generation"],"text":"Title: What alternative syntax exist for C/C++? (think SPECS or Mirah)\nTags: c++, c, compiler-construction, syntax, code-generation\nSource: Stack Overflow\n\nQuestion:\nI wondered if there are any simpler or more powerful syntax for C or C++. I have already come across SPECS. That is an alternative syntax for C++. But are there any others and what about C?\n\nIt could also be a sort of code generator so that things like functors could be defined less verbosely. I imagine it could be made as a code generator that compiles to C or C++ code which is very similar to the code you wrote in the alternative syntax.\n\nMirah is an example of doing this for Java.\n\nIdeally I would want to write C in Go like syntax. I like how they fixed switch-case, and in general made everything much less verbose.\n\n========================================\n\nTop Answer:\n```\n#define BEGIN {\n#define END }\n```\n\nNo! **Just say NO!**\n\n========================================\n\nCode:\n```text\n.lzz\n```\n\n```text\n.h\n```\n\n```text\n.cpp\n```\n\n```text\n#define BEGIN {\n#define END }\n```\n\n========================================\n\nComments:\n- In the compiled language world, this doesn't really make much sense; all you need is a language with binding support for C/C++ runtime systems. (Note that a MSVC C++ code won't run on Linux directly anyway; it's the runtime that matters.) Thus, in that light, FORTRAN is an \"alternative syntax\", or Modula II, or anything you can compile to objects linkable to the desired runtime.\n- This is not true. A lot of complexity and power from C++ comes from the templates which is an compile-time language.\n- Just from the link you posted, I see that they claim to have a `switch` statement without fall through and on the other end to have the same semantics. That doesn't go together.\n- According to the link you provided, Mirah is a completely separate language that compiles to the same JVM as Java, not an alternate syntax for Java.\n- I landed on this page looking for syntax like Scala or Python, for fully powered C++. Unfortunately SPECS doesn't begin answering that at all, and I fear that the completely changed declaration syntax will scare away too many people. The key to making a programming language successful is to use as similar syntax to other languages as possible while still changing the most annoying parts (i.e. see Python that is still essentially based on C syntax despite a few major changes).\n- I was thinking of something like that, but I am not sure what the best tools for doing something like that is.\n- @Adam Smith, any parsing toolchain will be ok - e.g., Antlr with a Java backend, or (much better) any functional language with ADTs (SML, OCaml, Haskell, ...).\n- It is a huge effort to define a custom syntax that will suit C++ development (do I need to mention that the mere parsing of C++ is extremely painful and only a few teams in the world have managed to do that entirely?). Having a custom language with wider user group would allow it to be better and have a selection of IDEs and other tools to go with it.\n- Picking a language an modifying it to your needs is a decent way of implementing a new language. Also, languages with equal semantics, with only differences in syntax are equal. Syntax is too unimportant in comparison to the other parts of the language.\n- In a corporate environment you can seldom use whatever language you want. Besides for integration with existing libraries etc one might prefer something that compiles to C or C++. I want to be able to write low level code and interface tightly with C, so regular Go is not and option. But I can live without the garbage collection and goroutines.","metadata":{"transformedAt":"2026-08-18T18:32:17.781Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":56,"estimatedTokens":914}}278{"id":"stack-23757143","source":"stackoverflow","questionId":23757143,"title":"What is a cell in the context of an interpreter or compiler?","tags":["python","compiler-construction","closures","interpreter","rust"],"text":"Title: What is a cell in the context of an interpreter or compiler?\nTags: python, compiler-construction, closures, interpreter, rust\nSource: Stack Overflow\n\nQuestion:\nPython code objects have an attribute `co_cellvars`. The documentation to PyPy's bytecode interpreter often uses the term *Cell*.\n\nAmong other langauges, Rust provides a Cell datatype. Googling suggests they relate to closures somehow.\n\nWhat is a *cell*, in the context of a programming language implementation? What problem do cells solve?\n\n========================================\n\nCode:\n```text\nco_cellvars\n```\n\n```text\ndef multiplier(n, d):\n    \"\"\"Return a function that multiplies its argument by n/d.\"\"\"\n    def multiply(x):\n        \"\"\"Multiply x by n/d.\"\"\"\n        return x * n / d\n    return multiply\n```\n\n```text\n>>> two_thirds = multiplier(2, 3)\n>>> two_thirds(7)\n4.666666666666667\n```\n\n```text\n>>> multiplier.__code__.co_cellvars\n('d', 'n')\n```\n\n```text\n>>> two_thirds.__closure__\n(<cell at 0x7f7a81282678: int object at 0x88ef60>,\n <cell at 0x7f7a81282738: int object at 0x88ef40>)\n```\n\n```text\n>>> two_thirds.__code__.co_freevars\n('d', 'n')\n```\n\n```text\n>>> {v: c.cell_contents for v, c in zip(\n        two_thirds.__code__.co_freevars,\n        two_thirds.__closure__\n)}\n{'d': 3, 'n': 2}\n```\n\n```text\ncell\n```\n\n```text\ntwo_thirds\n```\n\n```text\nn\n```\n\n```text\nd\n```\n\n```text\nmultiply\n```\n\n```text\nmultiplier\n```\n\n```text\nmultiply\n```\n\n```text\nmultiplier\n```\n\n```text\nmultiplier\n```\n\n```text\nmultiply\n```\n\n```text\nmultiplier\n```\n\n```text\n__closure__\n```\n\n```text\ncell\n```\n\n```text\n__code__\n```\n\n```text\nco_freevars\n```\n\n```text\ncell_contents\n```\n\n========================================\n\nComments:\n- In Rust, `Cell` is just a way of getting around inherited immutability so that you can modify data despite having an immutable reference to it. This is quite a different thing to what Python means by it. \"Cell\" is a pretty generic term, you know.\n- @ChrisMorgan sounds like you're familiar with Cell in both the contexts I mention. Would you be willing to submit an answer?\n- I'm not really familiar with the internals of the Python code object representation, but I can guess at what it is meaning by \"cell\".\n- Is it possible to modify one of these stored variables to alter the function after creation? (e.g. d = 5)\n- @EdwardGaremo Yes you can: `c.cell_contents = 5`. More general: `func.__closure__[func.__code__.co_freevars.index(name)].cell&zwnj;&#8203;_contents = value`.","metadata":{"transformedAt":"2026-08-18T18:32:17.781Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":23,"totalLines":129,"estimatedTokens":614}}279{"id":"stack-11253303","source":"stackoverflow","questionId":11253303,"title":"How does the Java Runtime Environment compare with the .NET framework in terms of compilation process?","tags":["c#","java","compiler-construction"],"text":"Title: How does the Java Runtime Environment compare with the .NET framework in terms of compilation process?\nTags: c#, java, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am learning about the conversion of source code to machine code via the `.NET` and `JRE` Frameworks. To start off I did some research comparing the two processes and created this diagram. I need some help in criticizing its correctness, and more importantly adding any serious things I missed out to better understand the compilation pathway.\n\n========================================\n\nCode:\n```text\n.NET\n```\n\n```text\nJRE\n```\n\n========================================\n\nComments:\n- What do you mean with \"assembler\" there? As it looks now, no that's wrong: The CLR/JVM does not generate assembly but instead direct machine code. At least the JVM (I don't think CLR) can generate assembly as a byproduct, but that's hardly necessary.\n- @Voo, by assembler I mean a program that will convert human-readable assembly to machine code that the cpu architecture can understand. I do see that this may be entirely redundant in the process.\n- @EJP, Voo is saying that the JVM creates machine code, not the Java compiler which generates byte code.\n- Most modern compilers do not generate humanly readable assembly which are then assembled to machine code, but create the machine code directly.\n- @jesterli Yep than that can be removed. Assembly can be generated in an additional step if necessary (JVM only), but generally we generate native code directly.\n- Would it be correct usage of terminology to say that the CLR/JVM is the 'platform' on which the CIL/bytecode is run? And how would we use the term 'framework'?\n- Could you please explain what you mean \"In .NET an assembly is the compilation unit\"?\n- What you see in Visual Studio in a single project gets compiled to a single `.exe` or `.dll` – the resulting assembly. Compilation unit refers to the smallest compilable unit which gets relevant if you want to do partial recompilation for example. In Java you'd just have to recompile the classes that changed, in .NET you'd have to recompile a whole project. Mind you, the difference is negligible for most cases – compilers for both platforms are blazingly fast, especially compared to C++.\n- +1 for the Java .class recompile. I never realized it, but after looking at the obj folder, apparently .NET don't separate the object result for each class. One can separate the classes into libraries to reduce recompile, but Java explicitly separate each class.\n- @Joey you seem to be mixing code compile and JIT compile here. I don't know how JVM Jitter work but it is just not true that CLR has to JIT compile whole assembly at once. JIT, by very definition, means just in time and CLR makes no exception to it. It just-in-time compiles to machine code the code that is to be executed and this almost always is a single method (in fact in highly simplified view CLR maintains a method table which points to jitter and when jitter has compiled the method to machine code, it updates the method table to point to the just compiled machine-code method)\n- @Martheen If java classes are packed into a JAR, and if you update a class wouldn't you need to re-generate the JAR. Similarly if you change any class in .Net, the assembly has to re-generated. It is just that .Net does not provide an option to deploy/use classes without a packing. Also visual studio provide an option to build/re-build. Build (intelligently) only compiles what has changed in the source code (and it is far more granular than a compilation unit of class) and then updates and re-packs assembly while re-build re-compiles whole assembly.\n- Amit, sorry, yes, I was mistaken there. It is fixed now. You don't need to regenerate the JAR, though, you just need to replace classes in it.\n- @Joey as I said, I am not too familiar with Java and hence my assertion about JAR was more in the form of a question :) Also I now fear that I may be a bit off the mark about build/re-build feature but alas I can not edit the comment.\n- I failed to find official documentation about how exactly it works. And there is an `obj` folder for C# projects, so it might really try intelligently to do so. I just thought I read a post by Eric Lippert where he said that it's always the whole assembly being build but with no proof in one direction or the other I simply removed the statement.\n- @AmitMittal Thanks. So did .NET Build compile in method-level granularity?\n- @Joey, C++ (.cpp files) can also be executed using the .NET environment. Does this mean it can be both an interpreted language using the compilation method above, and a directly compiled language?\n- @Martheen Have not yet found definite reference to visual studio's build/re-build options that can answer that but JITTER most of the time do work at method level granularity\n- @jesterII as Joey mentioned .Net interprets nothing. It JITs IL into machine code and then machine code directly runs on the processor. It is not entirely correct to say that C++ (or in general unmanaged code) can be executed using .Net. .Net has no role in executing the unmanaged code but (thankfully) it enables loading of unmanaged code and provides services that greatly ease unmanaged and managed interaction. Look at it this way; after JIT, managed code is essentially machine code and can call into other machine code loaded in the same process. .Net on its part greatly simplifies this.\n- @AmitMittal, thanks for responding. What I was talking about was this .NET version of C++ (learn.microsoft.com/en-us/archive/msdn-magazine/2005/februa&zwnj;&#8203;ry/&hellip;) called C++/CLI.","metadata":{"transformedAt":"2026-08-18T18:32:17.781Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":40,"estimatedTokens":1416}}280{"id":"stack-6872865","source":"stackoverflow","questionId":6872865,"title":"In compiler construction, is a symbol the same as a token?","tags":["terminology","token","symbols","compiler-construction"],"text":"Title: In compiler construction, is a symbol the same as a token?\nTags: terminology, token, symbols, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn compiler construction, when you talk about tokens, is a token the same like a symbol / just another term for a symbol? After some research I think to understand, that a token is a symbol with a reference to the symbol table, therefore some kind of attributed symbol / a symbol with some additional informations? Thanks for any clearfication :-)\n\n========================================\n\nTop Answer:\nA symbol is the GIVEN constant IN THAT KEY as in S=S in that table, or Pi = Pi in that equation, whereas a token represents the given as the medium OF exchange in that condition.\n\n========================================\n\nCode:\n```text\nchar s[100];\n```\n\n```text\n<\"char\", IDENTIFIER>\n```\n\n```text\n<\"s\", IDENTIFIER>\n```\n\n```text\n<\"[\", OPEN_SQUARE_BRACKET>\n```\n\n```text\n<\"100\", INTEGER_LITERAL>\n```\n\n```text\n<\"]\", CLOSE_SQUARE_BRACKET>\n```\n\n```text\n<\";\", SEMI_COLON>\n```\n\n```text\nstrcpy(s, \"Hello, world\\n\");\n```\n\n```text\nint x = arr[4];\n```\n\n```text\nint\n```\n\n```text\nx\n```\n\n```text\n=\n```\n\n```text\narr\n```\n\n```text\n[\n```\n\n```text\n4\n```\n\n```text\n]\n```\n\n```text\n;\n```\n\n```text\nx\n```\n\n```text\narr\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\nint\n```\n\n========================================\n\nComments:\n- thank you so much for this great reply! I somehow mixed up the name for the symbol table ;-)\n- So following from above - would it be fair to argue that all tokens entered into the symbol table are in fact - only identifiers? i.e. what tokens might be entered into the symbol table that are not identifiers? ..Is the definition of a symbol exactly an identifier? (as opposed to other tokens '[', ';', '100', etc. I'm speculating, I have no idea.)\n- As far as I know you are correct: the only tokens that are entered in the symbol table are identifiers (of functions, variables, etc) (See also en.wikipedia.org/wiki/Symbol_table)","metadata":{"transformedAt":"2026-08-18T18:32:17.781Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":22,"totalLines":109,"estimatedTokens":496}}281{"id":"stack-2017806","source":"stackoverflow","questionId":2017806,"title":"An alternative to flex and bison duo?","tags":["compiler-construction"],"text":"Title: An alternative to flex and bison duo?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIs there any alternative to flex and bison (lex/yacc) combo? Any other tools that let specify a language grammar in BNF?\n\n========================================\n\nTop Answer:\nThere's ANTLR\n\n========================================\n\nComments:\n- I really like Boost::Spirit's approach to this problem. I won't even have to leave Visual Studio :)","metadata":{"transformedAt":"2026-08-18T18:32:17.781Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":16,"estimatedTokens":113}}282{"id":"stack-60567540","source":"stackoverflow","questionId":60567540,"title":"Why does GCC compile itself 3 times?","tags":["gcc","architecture","compiler-construction","bootstrapping"],"text":"Title: Why does GCC compile itself 3 times?\nTags: gcc, architecture, compiler-construction, bootstrapping\nSource: Stack Overflow\n\nQuestion:\nI have compiled GCC from source but I can't seem to fully understand the utility of `gcc` compiling itself **three** times.\n\nWhat benefit does this serve ?\n\nThis answer says:\n\n \n \n- Build new version of GCC with existing C compiler\n \n- re-build new version of GCC with the one you just built\n \n- (optional) repeat step 2 for verification purposes.\n \n\nNow my question is that once the first step is complete and the compiler is built **why waste time rebuilding it ?**\n\nIs it just for verification ? If so, it seems pretty wasteful.\n\nThings get more complicated over here,\n\n The build for this is more complex than for prior packages, because\n you’re sending more information into the configure script and the make\n targets aren’t standard.\n\nI mean the whole compiler is written in C right, so why not just do everything in one pass ?\n\n**What is the use of the 3-phase bootstrap ?**\n\nThanks in advance.\n\n========================================\n\nTop Answer:\nConsidering the question from an information theory perspective, the first stage in a three stage compilation of a compiler does not produce a compiler. It produces a hypothesis that requires experimental verification. The sign of a good compiler distribution package is that it will produce, out of the box and without further work for the system administrator or compiler developer, a working compiler of the distribution's version and with the desired features of that version of that brand of compiler.\n\nMaking that happen is not simple. Consider the variables in the target environment.\n\n- Target operating system brand\n\n- Operating system version\n\n- Operating system settings\n\n- Shell environment variables\n\n- Availability of headers for inclusion\n\n- Availability of libraries for linking\n\n- Settings passed to the build process\n\n- Architecture of the target processing unit\n\n- Number of processing units\n\n- Bus architecture\n\n- Other characteristics of the execution model\n\n- Mistakes the developers of the compiler might make\n\n- Mistakes the person building the compiler might make\n\nIn the GNU compiler tool set, and in many tarball distributions, the program \"configure\" attempts to produce a build configuration that adapts to as many of the permutations of these as is reasonably possible. The completion without error or warning from configure is not a guarantee that the compiler will function. Furthermore, and more importantly for this question, the completion of the build is no guarantee either.\n\nThe newly built compiler may function for HelloWorld.c but not for a collection of a thousand source files in a multi-project, multi-repository collection of software called, \"Intelligent Interplanetary Control and Acquisition System.\"\n\nStage two and three are reasonable attempts at checking at least some of the compiler capabilities, since the compiler source itself is handy and demands quite a bit out of the hypothetically working compiler just built.\n\nIt is important to understand that the result of stage one and the result of stage two will not match. Their executables and other built artifacts are results from two different compilers. The stage one result is compiled with whatever the build system found in one of the directories listed in the \"PATH\" variable to compile C and C++ source code. The stage two result is compiled with the hypothetically working new compiler. The interesting probabilistic consideration is this:\n\n If the result of using stage one's result to compile the compiler again equals exactly the result of using stage two's result to compile the compiler a third time, then both are likely correct for at least the features that the compiler's source code requires.\n\nThat last sentence may need to be reread a dozen times. Its actually a simple idea, but the redundancy of the verb compile and noun compiler can tie a knot that takes a few minutes to untie and be able to retie. The source, the target, and the action executed have the same linguistic root, not just once but three times.\n\nThe build instructions for the compiler, as of May 25th, 2020, states the converse, which is easier to understand but merely anecdotal, not getting at the cruz of the reason three stages are important.\n\n If the comparison of stage2 and stage3 fails, this normally indicates that the stage2 compiler has compiled GCC incorrectly, and is therefore a potentially serious bug which you should investigate and report.\n\nIf we consider C/C++ development from a reliability assessment, test-first, eXtreme Programming, 6-Sigma, or Total Quality Management perspective, what component in a C/C++ development environment has to be more reliable than the compiler? Not many. And even the three stage bootstrapping of a compiler that the GNU compiler package has been using since early days is a reasonable but not an exhaustive test. That's why there are additional tests in the package.\n\nFrom a continuous integration point of view, the entire body of software under development by those that are about to use the new compiler should be tested before and after a new compiler is compiled and deployed. That's the most convenient way to ensure new compiler didn't break the build.\n\nBetween the three reliability check points, most people are satisfied.\n\n- Ensuring the compiler compiles itself consistently\n\n- Other tests the compiler developers have put into their distribution\n\n- The developer or system administrators source code domain is not broken by the upgrade\n\nOn a mathematics side note, it is actually impossible to exhaustively test a compiler with the silicon and carbon available on planet earth. The bounds of recursion in C++ language abstractions (among other things) are infinite, so the silicon or time required places testing every permutation of source code cannot realistically exist. On the carbon side, no group of people can free up the requisite time to study the source sufficiently to guaranteed that some finite limit is not imposed in some way by the compiler source.\"\n\nThe three levels of checks, only one of which is the three stage bootstrap process, will likely suffice for most of us.\n\nA further benefit of the three stage compile is that the new compiler is compiled with the new compiler which is presumably better either in terms of speed or resource consumption and possibly both.\n\n========================================\n\nCode:\n```text\ngcc\n```\n\n```text\nlibgcc\n```\n\n```text\nlibstdc++-v3\n```\n\n```text\n-O0\n```\n\n```text\n-O2\n```\n\n```text\n--disable-bootstrap\n```\n\n========================================\n\nComments:\n- The meaning of \"it\" in the phrase \"why waste time rebuilding it?\" is different for stage 1 than for stages 2 and 3. See the new and more in depth answer below. The last two stages are far from a waste of time.\n- So thats all it is, a test ? I can disable the whole process and to it in 1 pass ?\n- Usually a new version of GCC produces better (i.e. faster) code. So the idea of the second build is to produce a compiler which runs faster, taking advantage of the better optimisations which are implemented in it itself. If you were confident that your existing compiler already compiles as well as possible, why are you attempting to upgrade? Conversely, if you are upgrading, why would you not want to take advantage of the upgrade? The (optional) third compile, as the quoted answer says, is to verify that everything works as expected.\n- @rici The main reasons one usually builds a compiler from source are for debugging, development, or making use of highly experimental language features that may require custom patches. Making the compiler itself marginally faster is irrelevant for these.","metadata":{"transformedAt":"2026-08-18T18:32:17.781Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":137,"estimatedTokens":1944}}283{"id":"stack-9475642","source":"stackoverflow","questionId":9475642,"title":"What is the BNF of BNF? i.e. How do we define a BNF meta-grammar?","tags":["compiler-construction","code-generation"],"text":"Title: What is the BNF of BNF? i.e. How do we define a BNF meta-grammar?\nTags: compiler-construction, code-generation\nSource: Stack Overflow\n\nQuestion:\nI am trying to define the BNF of BNF. In other words, I am trying to define the meta-grammar of BNF. That is, a BNF grammar that is an instance of itself and can generate any other BNF grammar. \n\nAny tips/hints/snippets would be highly appreciated!\n\nThank you!\n\n========================================\n\nTop Answer:\n```\n ::= ' '>' '::=' \n ::= '|' | '' | ''\n```\n\n========================================\n\nCode:\n```text\nbnf = rules ;\nrules = rule ;\nrules = rules rule ;\nrule = lefthandside EQUAL righthandside SEMICOLON  ;\nlefthandside = IDENTIFIER ;\nrighthandside = ;\nrighthandside = righthandside token ;\ntoken = IDENTIFIER ;\ntoken = QUOTEDLITERAL ;\n```\n\n```text\nEQUAL = '=' ;\nSEMICOLON = ';' ;\nIDENTIFIER = letter ;\nIDENTIFIER = IDENTIFIER letterordigit ;\nletterordigit = letter ;\nletterordigit = digit ;\nletter = 'A' ;\n...\nletter = 'Z' ;\ndigit = '0' ;\n...\ndigit = '9' ;\n```\n\n```text\n<line> ::= '<' <word> '>' '::=' <definition>\n<definition> ::= <word> '|' | '' <definition> | ''\n```\n\n========================================\n\nComments:\n- Your question seems to be answered by the wikipedia article on BNF.\n- @GregHewgill Interesting. I was taught by Frank de Remer that BNF can't describe itself.\n- @EJP: De Remer was (is?) pretty spectacular with parser generators and grammars I don't believe he would have taught that. I suspect you some mis-remembered what you learned.\n- @IraBaxter That is exactly what he taught me.\n- @EJP: So, presumably that fact is backed up by some impossibility proof. Do you recall that?\n- @IraBaxter The issue Frank stated was lexical rather than grammatical: the variables are quoted rather than the literals, e.g. is a variable and + is not, rather than the other way around, so you end up with a vicious regress when trying to describe a variable.\n- @EJP: So, you can't describe the lexical structure of a variable name because the \"\" and the grammar doesn't go down to the character. If you *do* go down to the character, quoting character literals, defining a variable to have a spelling that involves \"<\", then this isn't a problem.\n- @IraBaxter I'm just telling you what Frank told me ;-) He pulled another trick which I'm sure is a standard lecturer's stunt, but I had never seen it before. We went around the class of about 30, standing up, saying our names and a bit of background, then he went around again pointing and giving everybody's names. Flawless.\n- @EJP: If you wanted a BNF-of-BNF using \"\" for identifier names, just modify the self-BNF I supplied in the obvious way.","metadata":{"transformedAt":"2026-08-18T18:32:17.781Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":66,"estimatedTokens":668}}284{"id":"stack-5948703","source":"stackoverflow","questionId":5948703,"title":"Difference in inlining functions by compiler or linker?","tags":["c++","compiler-construction","linker","inline"],"text":"Title: Difference in inlining functions by compiler or linker?\nTags: c++, compiler-construction, linker, inline\nSource: Stack Overflow\n\nQuestion:\nI am wondering whether there is any difference between inlining functions on a linker level or compiler level in terms of execution speed?\n\ne.g. if I have all my functions in .cpp files and rely on the linker to do inlining, will this inlining potentially be less efficient than say defining some functions in the headers for selected inlining on the compiler level or unity builds without any linking and all inlining done by the compiler?\n\nIf the linker is just as efficient, why would one then still bother inlining functions explicitly on the compiler level? Is that just for convenience, say there is just a one line constructor hence one can't be bothered with a .cpp file?\n\nI suppose this might depend on the compiler, in which case I would be most interested in Visual C++ (Windows) and gcc (Linux).\n\nThanks\n\n========================================\n\nTop Answer:\nGenerally, by the time the linker is run, your source has already been compiled into machine code. The linkers job is to take all the code fragments and **link** then together (possibly fixing addresses along the way). In such a case, there is no room for performing inlining.\n\nBut all is not lost. Gcc does provide a mechanism for link time optimization (using the -flto) option when compiling and linking. This causes gcc to produce a byte code that can then be compiled and linked by the linker into a single executable. Since the byte code contains more information than optimized machine code. The linker can now perform radical optimization on the **whole** codebase. Something that the compiler cannot do.\n\nSee here for more details on gcc. Not to sure about VC++ though.\n\n========================================\n\nComments:\n- Which linker do you use that does inlining?\n- @unapersson: Visual C++ has so-called link-time code generation that seems to be able to do just about anything while emitting code.\n- The linker is collecting all the modules and then calls up the compiler again to finish the code generation. That allows inlining between .cpp files, among other things.\n- Linkers don't inline code, they don't know how to *delete* machine code produced by the compiler. /LTCG serves a very different purpose, it *adds* code to provide instrumentation data to optimize the executable image layout. That code is temporary.\n- @Hans Passant: msdn.microsoft.com/en-us/library/xbf3tbeh(v=vs.80).aspx says that among other things does: *Cross-module inlining*. At any rate, there are other linkers that do it, so stating that *Linkers don't inline code* is an overstatement (in case you want to consider that in the previous case, the linker does not really *optimize* but rather calls the compiler to optimize).\n- Good answer … but as I understood it, the question is precisely whether all other things are equal.\n- @Konrad Rudolph: Well, true. And also the toolchain can be buggy, so one has to test it on his toolchain to be sure.\n- I definitely don't agree with this. Static compilers and linkers have vastly more time in which to do their work than JIT or dynamic optimization, which makes the scope of their efforts much larger.\n- @DeadMG: That would cancel \"all else being equal\", won't it?\n- I am not sure that this hierarchy actually holds... Consider a variable that is only used as argument to a function, and that the actual implementation of the function completely ignores: `int x = complex_calculation(); foo( x );`, and `void foo(int x) { std::cout << \"Hi!\"; }`. The compiler cannot possibly know whether `x` is used inside `foo` (unless it can inline it itself), and that means that it will *create* the variable and it will *call* `complex_calculation`. Even if the linker inlines `foo`, I don't think that the linker can actually remove those costs after the fact.\n- @David Rodr&#237;guez - dribeas: Technically it could if the intermediate data it uses to generate code stores enough to detect that.\n- @sharptooth: The point I was trying to make is that there are different types of optimizations that make sense at different points in time. To be able to optimize that away, the linker would have to contain a full blown code optimizer (not just inliner, i.e. not just the ability to move code around, but actually replace existing code). On the opposite end, a JIT has information on actual usage patterns that can allow it to optimize in ways that would be impossible at compile time (or would require the output of a profiler to be precise)\n- @David Rodr&#237;guez - dribeas: In fact link-time code generation in Visual C++ has a lot of data to work with - it's not a set of plain old .obj files, it's some inflated data from which VC++ generates machine code right before linkage - it's a phase separate from compilation - msdn.microsoft.com/en-us/magazine/cc301698.aspx\n- @sharptooth: Then all else never is equal, making your answer completely irrelevant. You have to factor in the real circumstances around such things.\n- The relevant links for VC++ are /LTCG and /GL.\n- (review) Well phrased answer for a first answer. Introduce LTO with its full-name the first time you use it, especially since the question didn't define LTO, if your answer is accepted people will probably read it right after the question.\n- Thanks for the tip! I'll remember that next time I answer. LTO is of course Link Time Optimization.\n- Sorry I am a bit confused. On the one hand you say LTO is as least as efficient as explicit inlining, on the other hand you say explicit inlining might nudge it to being inlined (and hence I assume potentially more efficient) when LTO wouldn't inline it?\n- LTO is at least as efficient as \"regular linking\" (non-LTO). LTO or not, explicit inlining is potentially more efficient than letting the compiler choose what to inline.\n- So does that mean that in terms of optimization and final execution speed one should try to inline rather more than less? E.g. for small classes (e.g. below 50 lines of code), write everything in the class definition of the header file? If one has a lot of those headers included in multiple cpp files, does this additional size of each compilation unit get reduced again by the linker?\n- I usually inline functions that have <=5 lines of code, but there’s not really any fool-proof rule – it requires profiling to be sure. Generally the compiler will make good decisions for you, but if it supports Profile Guided Optimization, that can help it better decide which ones need to be inlined. VC++/GCC both support PGO. I’m not sure how well compilers will merge identical functions from multiple TUs.","metadata":{"transformedAt":"2026-08-18T18:32:17.781Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":48,"estimatedTokens":1674}}285{"id":"stack-2174126","source":"stackoverflow","questionId":2174126,"title":"Is C# used anywhere besides .NET programming?","tags":["c#","compiler-construction","programming-languages"],"text":"Title: Is C# used anywhere besides .NET programming?\nTags: c#, compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nIt's my understanding that C#, though created by Microsoft, is a programming language that can be used or implemented for things other than .NET programming. For example, I could create a compiler for C# to target native x86 or other processors or even a JVM. Is my understanding incorrect? Does anyone know if C# is used for anything besides .NET?\n\n========================================\n\nTop Answer:\nC# is used for an experimental operating system called 'Singularity' which is written in the managed level from the ground up. An interesting project to watch!\n\n**Edit:** Thanks ***Jörg W Mittag*** for his comment - Helios is actually a modified form of Singularity to support satellite kernels, to quote from the paper I am reading right now '*Helios is an operating system designed to simplify the task of writing, deploying and tuning applications for heterogenous platforms. Helios introduces satellite kernels, which export a single uniform set of OS abstractions across CPUs of disparate architectures and performance characteristics. Satellite Kernels allows developer to write applications against familiar operating system APIs and abstractions.*'...interesting...\n\nHope this helps,\nBest regards,\nTom.\n\n========================================\n\nComments:\n- I beg to differ...Mono is not .NET. It is a platform written in C#. ;)\n- Well, there's not that much to watch. The project ended years ago, and most of the researchers are now working on Helios (which is based on Singularity and expands its ideas towards heterogeneous systems).\n- $1249 for a single developer...that's a bit steep if you're a lone programmer working on project to market...\n- Nitpick: the CLR is Microsoft's proprietary implementation. The specification is called *VES* (virtual execution system).\n- Also, it's not true that there is only one implementation of Java. Sun alone has *four* implementations, Oracle has one, IBM has at least two, Microsoft has/had one, Apache has one, RedHat has one, Apple has one, Azul has one, pretty much every cell phone vendor has one, Google has one and then there's dozens of Open Source and Research ones.\n- By Google's implementation, do you mean the one on Android? Isn't that normal Java compiled for a non-standard JVM?\n- Rotor's link is here...microsoft.com/downloads/&hellip;\n- It is not really a C# code per se, as it gets translated into objective c to be compiled...\n- @tommieb75 true, but it still fits within the OP's question and it doesn't require developers to write Objective-C. They are writing C# (even LINQ is supported to a certain extent).\n- @tommieb75 - see stackoverflow.com/questions/1453355/how-monotouch-works/&hellip;\n- Not just iPod, all iOS platforms including iPhone and iPad.\n- For the record: note that with the recent release of the iPhone OS version 4.0 SDK, Apple have outlawed using all forms of using cross-compiled languages on the iPhone OS family of devices.\n- For the record: Apple tried to do this but had to back down. No Mono apps were ever rejected from the App Store because of this and it is ancient history now.","metadata":{"transformedAt":"2026-08-18T18:32:17.782Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":34,"estimatedTokens":807}}286{"id":"stack-557905","source":"stackoverflow","questionId":557905,"title":"Why is building a cross-compiler harder than building a regular compiler?","tags":["compiler-construction","cross-platform","cross-compiling"],"text":"Title: Why is building a cross-compiler harder than building a regular compiler?\nTags: compiler-construction, cross-platform, cross-compiling\nSource: Stack Overflow\n\nQuestion:\nEverything I've read seems to imply that building a cross-compiler is significantly harder than building a compiler that targets the platform it runs on. Is this true? If so, why? It seems like generating assembly code and system calls for an arbitrary platform shouldn't be any harder than generating such code and system calls for the platform the compiler is running on, but maybe I'm just being naive.\n\n========================================\n\nTop Answer:\nThis doesn't have to be harder, but it can be depending on the compiler architecture.\n\nA compiler is not only translating source code into asm and system calls. It's also integrating pre-existing *helper code* into the generated files. This is code includes startup code, functions preamble, part of the C api that can be inlined, etc.\n\nIn a normal compiler C1 for platform A built on platform A, the original compiler C0 can build C1 and its *helper code* (for A, since C0 targets A) directly.\n\nIn a cross compiler C2 for platform B built on platform A, the original compiler C0 must first build a special version of C2 which doesn't need the *helper code* (because the *helper code* is for B, while C0 targets A), then it must run C2 to generate the *helper code*. Depending on the compiler, it may then have to generate a second version of C2 that includes the *helper code*.\n\nThe act of building the limited version of C2, without *helper code* is the bootstrapping.\n\n========================================\n\nComments:\n- By building, do you mean \"programming\" or \"compiling\"?\n- Be interesting to know what you've read. It's wrong :-)\n- @dsimcha no, it's not *significantly* harder, although it might be slightly harder based on common sense, *but I still don't exactly get what you're trying to ask*.\n- I disagree with this. If I had a compiler written portably, in say ansi-C, I could easily compile it on any platform that had an ansi-c compiler. How would IO factor into it, and what does it have to do with building an OS?\n- Unless you're talking about linking / archiving, but that's a whole different ballgame.\n- That's flawed logic as it's not logic because it starts with 'I think'. There's no explanation of why a multi-target compiler is much harder as well.\n- I thought it was pretty obvious... you need multiple binarizers, one for each target. You also need to make sure you're representing data internally in a way that will work not just for one instruction set, but potentially multiple very different instruction sets.\n- A compiler targeted for another platform needs to handle only one platform, that to which it is targeted.","metadata":{"transformedAt":"2026-08-18T18:32:17.782Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":31,"estimatedTokens":697}}287{"id":"stack-16446274","source":"stackoverflow","questionId":16446274,"title":"Why isn't the Prelude's words function written more simply?","tags":["haskell","optimization","compiler-construction"],"text":"Title: Why isn't the Prelude's words function written more simply?\nTags: haskell, optimization, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nConsider the `words` Prelude function; it is really easy and one could write it in the following manner:\n\n```\nwords' :: String -> [String]\nwords' [] = []\nwords' str = before : words' (dropWhile isSpace after) where\n (before, after) = break isSpace str\n```\n\nHowever, I noticed that its original Prelude code seems much less... natural:\n\n```\nwords :: String -> [String]\nwords s = case dropWhile {-partain:Char.-}isSpace s of\n \"\" -> []\n s' -> w : words s''\n where (w, s'') =\n break {-partain:Char.-}isSpace s'\n```\n\nI assume that there are optimization-related reasons for it. The question is: am I wrong to expect that the compiler should optimize the `words'` function just as well as its Prelude version? I did use the same functions (`break`, `dropWhile`, `isSpace`).\n\nI was once very surprised that GHC did not perform some of the simplest low-level optimizations:\n\nC vs Haskell Collatz conjecture speed comparison\n\nbut aside for the `{-partain:Char.-}` bits (this hint for the compiler does not seem very helpful in this situation IMO) the `words` code seems unnecesarily bloated for a high-level language. What is the reason behind it in this case?\n\n========================================\n\nCode:\n```text\nwords' :: String -> [String]\nwords' [] = []\nwords' str = before : words' (dropWhile isSpace after) where\n    (before, after) = break isSpace str\n```\n\n```text\nwords                   :: String -> [String]\nwords s                 =  case dropWhile {-partain:Char.-}isSpace s of\n                                \"\" -> []\n                                s' -> w : words s''\n                                      where (w, s'') =\n                                             break {-partain:Char.-}isSpace s'\n```\n\n```text\nwords\n```\n\n```text\nwords'\n```\n\n```text\nbreak\n```\n\n```text\ndropWhile\n```\n\n```text\nisSpace\n```\n\n```text\n{-partain:Char.-}\n```\n\n```text\nwords\n```\n\n```text\n*Main> words \"    \"\n[]\n*Main> words' \"     \"\n[\"\"]\n```\n\n```text\ndropWhile isSpace\n```\n\n```text\nQuickCheck\n```\n\n========================================\n\nComments:\n- I don't think the `{-partain:Char.-}` bit is anything more than a commented-out module name, by the way. According to Google, someone with last name Partain worked on GHC some time ago. I'm guessing that was just him signing his comments.\n- Oh, I thought that it might have had some influence on the compiler. Nice catch with the Partain guy!\n- Well, I don't think my version is in any way better than the original, but that is the point; to me it seemed that the only difference was verbosity.\n- I had another look in `Prelude` and some other core libraries and I must say that verbosity is in fact quite rare in function definitions (and is probably explained by cases like the one you described). The original question title was an overstatement :).","metadata":{"transformedAt":"2026-08-18T18:32:17.782Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":12,"totalLines":102,"estimatedTokens":737}}288{"id":"stack-31720814","source":"stackoverflow","questionId":31720814,"title":"Are the optimizations done in LTO the same as in normal compilation?","tags":["c++","optimization","compiler-construction","g++","llvm"],"text":"Title: Are the optimizations done in LTO the same as in normal compilation?\nTags: c++, optimization, compiler-construction, g++, llvm\nSource: Stack Overflow\n\nQuestion:\nWhile compiling a translation unit the compiler is doing a lot of optimizations - inlining, constant folding/propagation, alias analysis, loop unrolling, dead code elimination and many others I haven't even heard of.\nAre all of them done when using LTO/LTCG/WPO between multiple translation units or is just a subset (or a variant) of them done (I've heard about inlining)?\nIf not all optimizations are done I would consider unity builds superior to LTO (or maybe using them both when there are more than 1 unity source files).\n\nMy guess is that it's not the same (unity builds having the full set of optimizations) and also that it varies a lot across compilers.\n\nThe documentation on lto of each compiler doesn't precisely answer this (or I am failing at understanding it).\n\nSince lto involves saving the intermediate representation in the object files in theory LTO could do all the optimizations... right?\n\nNote that I am not asking about build speed - that is a separate issue.\n\n**EDIT**:\nI am mostly interested in gcc/llvm.\n\n========================================\n\nCode:\n```text\ngcc -c -O2 -flto foo.c\n          gcc -c -O2 -flto bar.c\n          gcc -o myprog -flto -O2 foo.o bar.o\n```\n\n```text\nint f1() { return 10; }\n```\n\n```text\nint f2(int i) { return 2*i; }\n```\n\n```text\nint main()\n{   \n    int res=f1();\n    res=f2(res);\n    res++;\n\n    return res;\n}\n```\n\n```text\n00000000004005e0 <main>:\n  4005e0:   b8 15 00 00 00          mov    $0x15,%eax\n  4005e5:   c3                      retq   \n  4005e6:   66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)\n  4005ed:   00 00 00\n```\n\n```text\n-fwhole-program\n```\n\n========================================\n\nComments:\n- Like you said yourself, this is likely to vary greatly between compilers. It may be feasible to answer for both GCC and LLVM, but beyond that it's too broad. Please narrow the scope.\n- @BЈовић link-time optimizations (or link time code generation or whole program optimization)\n- That depends on the compiler and the target. For example, DSPs can improve performances by putting some code in the first block of memory, which usually has faster access.","metadata":{"transformedAt":"2026-08-18T18:32:17.782Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":66,"estimatedTokens":573}}289{"id":"stack-3848001","source":"stackoverflow","questionId":3848001,"title":"Extending the Mono C# compiler: is there any documentation or precedent?","tags":["c#","compiler-construction","mono","gmcs"],"text":"Title: Extending the Mono C# compiler: is there any documentation or precedent?\nTags: c#, compiler-construction, mono, gmcs\nSource: Stack Overflow\n\nQuestion:\nI am currently involved in some interesting programming language research which has, up until now, centred around extending the upcoming Java 7.0 compiler with some very powerful programmer-productivity-based features. The work should be equally applicable to related programming languages such as C#.\n\nI'm currently scoping out the options for prototyping a C# port of the functionality. I would prefer open-source options so that the fruits of this work can be shared with the broadest-possible audience. Thus the Mono C# compiler seems to be the most obvious starting point. I'm an experienced C# developer so writing the code isn't the problem. I'm mainly concerned about extending the compiler in a maintainable and supported fashion. In the Mono FAQ on the subject (link) it is stated that \"Mono has already been used as a foundation for trying out new ideas for the C# language (there are three or four compilers derived from Mono's C# compiler)\". Unfortunately, there are no further pointers than this and, so far, Google searches have not turned anything up.\n\nI'm wondering if anybody out there has any information on this. Do `mcs`/`gmcs`/`dmcs` have a standard extensibility model? Specifically, I will be performing some interesting transformations on a program's abstract syntax tree. Is there a standard mechanism for inserting functionality into the compiler chain between abstract syntax tree generation and the type checker and then code generation?\n\nUp until now I've written some ad-hoc extensions to the code (primarily in the code generator) but this doesn't seem to be a maintainable solution especially given that I intend to keep my extensions up to date with the Git trunk of Mono as much as possible. Furthermore it would be nice to be able to make updates to my extensions without having to recompile the whole compiler every time I make a change. I would like to be able to wrap all my AST manipulations into a single .NET assembly that could be dynamically loaded by `mcs`/`gmcs`/`dmcs` without having to hack at the core compiler code directly.\n\nAny thoughts or pointers on extending the Mono C# compiler would be gratefully received!\n\nUPDATES (23 October 2010)\n\nIn response to the responses to my question, I decided that I would start working on a branch of Mono in order to create a simple extensibility model for the compiler. It's in its very early stages, but here it is at GitHub:\n\nhttp://github.com/rcook/mono-extensibility\n\nAnd the main commit is: http://github.com/rcook/mono-extensibility/commit/a0456c852e48f6822e6bdad7b4d12a357ade0d01\n\nIf anybody would be interested in collaborating on this project, please let me know!\n\n========================================\n\nTop Answer:\nThe mono C# compiler is a bit of a hack. I spent around a week figuring out how to use information from the parse tree. The compiler does not produce any intermediate representation and code generation may break parts of the parse tree.\nStill, the parser and tokenizer might prove useful to you and you just take it from there.\nSharpDevelop also provides a C# parser.\nThe SharpDevelop parser is easier to use than the mono C# parser.\nIf F# also works for you, I would recommended. The source much cleaner than mono and available under open source license.\n\n========================================\n\nCode:\n```text\nmcs\n```\n\n```text\ngmcs\n```\n\n```text\ndmcs\n```\n\n```text\nmcs\n```\n\n```text\ngmcs\n```\n\n```text\ndmcs\n```\n\n```text\nIEnumerable\n```\n\n```text\nIEnumerable\n```\n\n```text\n~T\n```\n\n```text\nIEnumerable<T>\n```\n\n========================================\n\nComments:\n- Alternatively, take a look at Boo. Compiler extensibility is part of the \"package\".\n- You can find another small patch here. Maybe it's good to create a catalog of these patches.\n- and @Jord&#227;o: +1 each for your helpful links. In the interests of sharing here's a link to my personal blog post: clopenset.com/content/&hellip;. The attached patch is a simple hack to allow instrumentation of method bodies which is somewhat different from the AST/type checker modifications I'm planning to make, but was an interesting experiment nonetheless.\n- I've started working on an extensibility branch: github.com/rcook/mono-extensibility/commit/&hellip;\n- I've done some hacking of my own and agree with your assessments. I decided to branch it anyway: github.com/rcook/mono-extensibility/commit/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.782Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":85,"estimatedTokens":1134}}290{"id":"stack-2940023","source":"stackoverflow","questionId":2940023,"title":"What are the benefits/consequences of compiling an armv7 only architecture?","tags":["iphone","compiler-construction","armv7","armv6"],"text":"Title: What are the benefits/consequences of compiling an armv7 only architecture?\nTags: iphone, compiler-construction, armv7, armv6\nSource: Stack Overflow\n\nQuestion:\nIn compiling iPhone apps, there is a setting for \"Optimized\" architecture (armv7 only) vs a standard armv6/armv7 architecture.\n\nWhat are the benefits/consequences of compiling an armv7 only architecture?\n\n========================================\n\nTop Answer:\nUnless your program requires OpenGLES 2.0 (which is only supported on armv7-supporting devices), you should compile the standard fat (armv6/armv7) binary. A fat binary is basically two (or more) Mach-O binaries glued together, with a single header page at the beginning. The performance cost is negligible: the dynamic loader must take an extra page fault for the header page to determine which architecture to load.\n\nBuilding for armv7 only will essentially halve the size of your executable, although it's unlikely that your executable is all that large to begin with. You can use the \"size\" and \"otool\" commands on the host to get more information about the various sections in your app's binary, e.g. \"size -arch armv6 build/Release/MyApp.app/MyApp\" will get the size of various sections in the armv6 version of a binary, \"size -arch armv7 build/Release/MyApp.app/MyApp\" will get the size of various sections in the armv7 version of a binary, and obviously \"ls -l build/Release/MyApp.app/MyApp\" will get the actual file size of the binary.\n\n========================================\n\nComments:\n- out of the current line-up, what are the devices that are not armv7?\n- Probably the iPhone, iPhone 3G, iPod Touch 1st and 2nd gen.\n- @Alex For people who have iPhone 3G, what happens when the app is in the app store ? They can't download the app or the app is crashing when they launch it ?\n- Apple will probably disapprove the App if you haven't flagged in accordingly (in Info.plist) to only run on 3rd generation and above devices. They way to do that I believe is to specify that your app requires OpenGL ES 2.0. The image in this blogpost (meachware.blogspot.com/2010/08/infoplist-cheat-sheet.html) shows you the correct setting (opengles-2).\n- To flag your app is for armv7 only in the App Store, you add \"armv7\" to the UIRequiredDeviceCapabilities list in Info.plist.","metadata":{"transformedAt":"2026-08-18T18:32:17.782Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":575}}291{"id":"stack-2741299","source":"stackoverflow","questionId":2741299,"title":"How is a new programming language actually formed/created?","tags":["compiler-construction","programming-languages","computer-science"],"text":"Title: How is a new programming language actually formed/created?\nTags: compiler-construction, programming-languages, computer-science\nSource: Stack Overflow\n\nQuestion:\nFortran->Algol->Cpl->Bcpl->C->C++->Java .....\n\nSeems like every language is built upon an ancestor language. \nMy question : New languages extend parent ones or there is some kind of a trick?\n\ne.g. System.out.print() in Java ; is it actually printf() in C, and so on (printf is actually .. in Cpl)?\n\nIf so, doesn't this make every further language be slower and need more memory? What separates a new language from a framework?\n\n========================================\n\nTop Answer:\nHow is a new programming language actually formed/created ?\n\nIt's a multistage process:\n\nPointy-headed type theorists and other professionals are continually proposing new language features. You can read about them in places like the *Proceedings of the ACM Symposium on Principles of Programming Languages (POPL)*, which has been held annually since 1973.\n\nMany of these proposals are actually implemented in some **research language**; some research languages I personally find promising include Coq and Agda. Haskell is a former research language that made it big. A research language that gets 10 users is often considered a success by its designers. Many research languages never get that far.\n\nFrom research to deployment I know of two models:\n\n**Model A:** A talented amateur comes along and synthesizes a whole bunch of existing features, maybe including some new ideas, into a new language. The amateur has talent, charisma, and maybe a killer app. Thus C, Perl, Python, Ruby, and Tcl are born.\n\n**Model P:** A talented professional make career sacrifices in order to build and promulgate a new language. The professional has talent, a deep knowledge of the field, and maybe a killer app. Thus Haskell, Lua, ML, Pascal, Scala, and Scheme are born.\n\nYou propose another model:\n\n**Model E:** A talented person, whether amateur or professional, *extends* or modifies another language. There's a built-in user base who may be interested in the extension. Maybe users can explore new ideas without paying heavy transition costs. Thus C# and C++ are born.\n\nMy definition of a professional is someone who is paid to know about programming languages, to pass on that knowledge, and to develop new knowledge in programming languages. Unfortunately this is not the same as designing and implementing new languages, and it is not the same as making implementations that many people can use. This is why most successful programming languages are designed and built by amateurs, not professionals.\n\nThere have been quite a few interesting research languages that have had hundreds or even thousands of users but yet never quite made it big. Of these one of my favorites is probably Icon. I have argued elsewhere that nobody really knows why languages become popular. \n\n Do new languages extend parent ones?\n\nThis is actually very rare. C++ is the most popular example, and perhaps Algol-W or PL/S came close. But it's much more common to use the parent language for inspiration only, and some languages (Python comes to mind) acknowledge multiple \"parents\" as sources of inspiration. \n\n Doesn't this make every further language be slower and need more memory?\n\nNot necessarily. C++ is slower and uses more memory not because it is descended from C but because it was developed by accretion over time to the point where only a very skilled user can *reliably* write C++ code that is as fast as similar C code. (I want to be very clear: it's not that C++ can't be fast; it's that the cost of C code is always obvious from reading the source, and the cost of C++ code is sometimes not at all obvious from reading the source.) For more information about the evolution of C++, read Jim Waldo's book *The Evolution of C++*.\n\nJava was initially slow because of just-in-time compilation and other wacky things in the implementation. They also saddled themselves with this dynamic class-loading stuff which is really hard to get things to be fast (because a class could be extended dynamically at any moment). Kenny Zadeck, Roger Hoover, and David Chase built a really fast native-code compiler for Java *without* dynamic class loading.\n\nFor a counterexample, I think Scheme programs ran faster and used less memory than the Lisp programs that preceded them&mdash;in part because Guy Steele is both a brilliant designer and a brilliant implementor. (Rare combination, that.)\n\nBut there is something to what you say: people who lack the expertise to build a good compiler from scratch or who lack the expertise to design a whole language from scratch may well hack up an implementation of something not too different from a parent. In such cases one is quite likely to wind up with a language that is less well designed, less well implemented, slower, and using more memory than its predecessor. (Tony Hoare famously said that Algol 60 was an improvement on most of its successors [sic]).\n\nIt's also true that the more recently a language is designed, the more computing resources are available for the same price. Early C compilers had to operate effectively in as little as 128K of RAM. Today's C++ compilers face no such constraints, and there is every reason for them to use more memory: it is really cheap to populate a machine with gigabytes of RAM, and to limit ones use to mere megabytes saves nothing; the larger memory is already paid for.\n\n**Summary:** Languages come into being because people want to make programming better, and they have new ideas. Languages get their start when somebody takes a whole bunch of ideas, some new and some proven, and synthesizes them into a coherent whole. It's a big job. One way to make the job easier is to draw not just on proven *features*, but proven *designs*, of one or more predecessor languages. This kind of design creates the impression of \"parenthood\", but actual extension or near-extension (in the case of C++ extending C) is rare. Time and space costs don't necessarily get larger as languages evolve, but it's often the case that people create languages by making existing designs more complex, and **the more complex the design, the harder it is to implement efficiently.** It's therefore not unusual that programs written in a new language seem slower or to use more memory than similar programs written in an ancestor language. Finally, as with all other forms of software, compilers designed and built recently tend to use more RAM and CPU than compilers built ten years ago, simply because large quantities of RAM and CPU cycles are available at bargain-basement prices.\n\n========================================\n\nCode:\n```text\nenum\n```\n\n========================================\n\nComments:\n- Languages are not written \"on top\" of each other typically. Many languages compile to Assembly or some bytecode (Java, .NET). Others are interpreted - the interpreters typically are in C/C++ at the core (not always true, but sorta true).\n- Actually, there *are* C++ interpreters, and they *are* dog slow.\n- Languages can effect the maximum speed of a compiler--or more accurately a language can be built to increase the efficiency of a compiler.\n- @Bill of course. In theory though PHP can be faster than Assembly.\n- @Earlz Depends, for optimal coding that is not correct. PHP does not translate until an efficient enough set of codes. In fact, your example is a perfect demonstration of my point--PHP is not really made to be optimized well and will not likely be very fast because it would be so hard to create an efficient compiler.\n- @Bill but you could create a CPU with an instruction set designed for PHP. Coding in C or some other low level language may not be as fast as PHP because of PHP assumptions made for optimization etc. My point is that Languages are not slow Implementations are. There is no set in stone thing saying PHP must be definition be slower than some other language.\n- @Earlz it seems like we're almost saying the same thing--and I'm not disagreeing with you anyway, I've seen DAMN FAST basic compilers, but still the language doesn't allow you to provide hints to the compiler for advanced optimizations. The implementation has MUCH more to do with it than the language does, but the language also has an impact on what the implementation can do at a given level of effort. An implementation of a simple language with minimal syntax would be easier to optimize than a one for a powerful OO language with dynamic typing, reflection and no \"int\" type--for instance.\n- Haven't code generation projects been on the way since the early 60s?\n- @Paul Nathan: Yes of course, and I believe it's because they serve a fundamental purpose. What I think may be new is a rising awareness of what they accomplish, in terms of implementing DSLs. Another way to look at it is *partial evaluation*. The danger is that they be thought of in terms of what they *are* rather than in terms of what they *accomplish*.\n- I'm not knocking them, just... being aware of the continual non-fulfillment of the promises regarding them. :-)\n- @Paul: I think we're saying the same thing from different angles.","metadata":{"transformedAt":"2026-08-18T18:32:17.782Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":78,"estimatedTokens":2301}}292{"id":"stack-56262889","source":"stackoverflow","questionId":56262889,"title":"Why are global variables in x86-64 accessed relative to the instruction pointer?","tags":["c","assembly","compiler-construction","x86-64","relative-addressing"],"text":"Title: Why are global variables in x86-64 accessed relative to the instruction pointer?\nTags: c, assembly, compiler-construction, x86-64, relative-addressing\nSource: Stack Overflow\n\nQuestion:\nI have tried to compile c code to assembly code using `gcc -S -fasm foo.c`.\nThe c code declare global variable and variable in the main function as shown below:\n\n```\nint y=6;\nint main()\n{\n int x=4;\n x=x+y;\n return 0;\n}\n```\n\nnow I looked in the assembly code that has been generated from this C code and I saw, that the global variable y is stored using the value of the rip instruction pointer.\n\nI thought that only const global variable stored in the text segment but, looking at this example it seems that also regular global variables are stored in the text segment which is very weird.\n\nI guess that some assumption i made is wrong, so can someone please explain it to me?\n\nthe assembly code generated by c compiler:\n\n```\n.file \"foo.c\"\n .text\n .globl y\n .data\n .align 4\n .type y, @object\n .size y, 4\ny:\n .long 6\n .text\n .globl main\n .type main, @function\n\nmain:\n.LFB0:\n .cfi_startproc\n pushq %rbp\n .cfi_def_cfa_offset 16\n .cfi_offset 6, -16\n movq %rsp, %rbp\n .cfi_def_cfa_register 6\n movl $4, -4(%rbp)\n movl y(%rip), %eax\n addl %eax, -4(%rbp)\n movl $0, %eax\n popq %rbp\n .cfi_def_cfa 7, 8\n ret\n .cfi_endproc\n.LFE0:\n```\n\n========================================\n\nTop Answer:\nAlthough the .data and .text segments are independent of one another, once linked, their offsets relative to one another are fixed (at least in the gcc x86-64 `-mcmodel=small` code model, which is the default code model and works for all programs whose code+data is less than 2GB).\n\nSo wherever the system loads an executable in the process's address space, the instructions and the data they reference will have fixed offsets relative to one another.\n\nFor these reasons, x86-64 programs compiled for the (default) small code model use RIP-relative addressing for both code and global data. Doing so means the compiler doesn't need to dedicate a register to point to wherever the system loaded the executable's .data section; the program already knows its own RIP value and the offset between that and the global data it wants to access, so the most efficient way of accessing it is via a 32-bit fixed offset from RIP.\n\n(Absolute 32-bit addressing modes would take more space, and 64-bit absolute addressing modes are even less efficient, and only available for RAX/EAX/AX/AL.)\n\nYou can find more info about this on Eli Bendersky's website: Understanding the x64 code models\n\n========================================\n\nCode:\n```text\nint y=6;\nint main()\n{\n        int x=4;\n        x=x+y;\n        return 0;\n}\n```\n\n```text\n.file   \"foo.c\"\n        .text\n        .globl  y\n        .data\n        .align 4\n        .type   y, @object\n        .size   y, 4\ny:\n        .long   6\n        .text\n        .globl  main\n        .type   main, @function\n\nmain:\n.LFB0:\n        .cfi_startproc\n        pushq   %rbp\n        .cfi_def_cfa_offset 16\n        .cfi_offset 6, -16\n        movq    %rsp, %rbp\n        .cfi_def_cfa_register 6\n        movl    $4, -4(%rbp)\n        movl    y(%rip), %eax\n        addl    %eax, -4(%rbp)\n        movl    $0, %eax\n        popq    %rbp\n        .cfi_def_cfa 7, 8\n        ret\n        .cfi_endproc\n.LFE0:\n```\n\n```text\ngcc -S -fasm foo.c\n```\n\n```text\nmov   $.LC0, %edi     # mov r32, imm32\ncall  puts\n```\n\n```text\n.data\n```\n\n```text\nconst\n```\n\n```text\n.data\n```\n\n```text\ndisp32\n```\n\n```text\nRIP+rel32\n```\n\n```text\nmov foo, %eax\n```\n\n```text\nmov foo(%rip), %eax\n```\n\n```text\nmov\n```\n\n```text\nputs(\"hello\");\n```\n\n```text\nmov array(%rax), %edx\n```\n\n```text\nadd $4, %eax\n```\n\n```text\nlea .LC0(%rip), %rdi\n```\n\n```text\nmov $.LC0, %edi\n```\n\n```text\n.cfi\n```\n\n```text\n-mcmodel=small\n```\n\n========================================\n\nComments:\n- Just because it's accessed relative to `rip` doesn't mean it's in the `.text` segment (rather section). As you can clearly see in your assembly code, it's preceded by a `.data` so it's there not in `.text`. It's just a position independent way to address memory, which is frequently enabled by default nowadays due to ASLR.\n- Modern Linux distros make gcc default to the small PIC code-model, for PIE executables. x64 is Windows terminology, not generally used in Linux or GCC. Otherwise, nice answer.\n- The \"small\" code mode is position-dependent, so all addresses are link-time constants. Your point about needing a pointer to the .data section would only apply for the small PIC/PIE model, where absolute static address aren't known at link time.\n- Also related: what does \"mov offset(%rip), %rax\" do?","metadata":{"transformedAt":"2026-08-18T18:32:17.782Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":19,"totalLines":194,"estimatedTokens":1154}}293{"id":"stack-528122","source":"stackoverflow","questionId":528122,"title":"Why doesn't C# offer constness akin to C++?","tags":["c#","compiler-construction","reference","constants"],"text":"Title: Why doesn't C# offer constness akin to C++?\nTags: c#, compiler-construction, reference, constants\nSource: Stack Overflow\n\nQuestion:\nReferences in C# are quite similar to those on C++, except that they are garbage collected. \n\nWhy is it then so difficult for the C# compiler to support the following:\n\n- Members functions marked `const`.\n\n- References to data types (other than string) marked `const`, through which only `const` member functions can be called ?\n\nI believe it would be really useful if C# supported this. For one, it'll really help the seemingly widespread gay abandon with which C# programmers return naked references to private data (at least that's what I've seen at my workplace).\n\nOr is there already something equivalent in C# which I'm missing? (I know about the `readonly` and `const` keywords, but they don't really serve the above purpose)\n\n========================================\n\nTop Answer:\nAs Jon already covered (of course) const correctness is not as simple as it might appear. C++ does it one way. D does it another (arguably more correct/ useful) way. C# flirts with it but doesn't do anything more daring, as you have discovered (and likely never well, as Jon well covered again).\n\nThat said, I believe that many of Jon's \"theoretical reasons\" are resolved in D's model.\n\nIn D (2.0), const works much like C++, except that it is fully transitive (so const applied to a pointer would apply to the object pointed to, any members of that object, any pointers that object had, objects they pointed to etc) - but it is explicit that this *only* applies from the variable that you have declared const (so if you already have a non-const object and you take a const pointer to it, the non-const variable can still mutate the state).\n\nD introduces another keyword - invariant - which applies to the object itself. This means that nothing can ever change the state once initialised.\n\nThe beauty of this arrangement is that a const method can accept both const and invariant objects. Since invariant objects are the bread and butter of the functional world, and const method can be marked as \"pure\" in the functional sense - even though it may be used with mutable objects.\n\nGetting back on track - I think it's the case that we're only now (latter half of the naughties) understanding how best to use const (and invariant). .Net was originally defined when things were more hazy, so didn't commit to too much - and now it's too late to retrofit.\n\nI'd love to see a port of D run on the .Net VM, though :-)\n\n========================================\n\nCode:\n```text\nconst\n```\n\n```text\nconst\n```\n\n```text\nconst\n```\n\n```text\nreadonly\n```\n\n```text\nconst\n```\n\n```text\ncall\n```\n\n```text\ncallvirt\n```\n\n```text\nreadonly\n```\n\n========================================\n\nComments:\n- I disagree with your claim that \"most of the pros of constness are performance related\" - I'd say most are correctness and maintainability related.\n- It's not for the convenience of the JITter, but for the programmer. In C++, if I'm trying alter a const object, the compiler gives me a error. So, for example, I can return const references from my methods. In C# everybody seems to be returning references that anybody outside can change. Dangerous!\n- Thanks Jon. That's illuminating.\n- Interesting, I hadn't seen that. I think with the functional perspective (as brought out in my reply) it makes any difficulty worth it, though.","metadata":{"transformedAt":"2026-08-18T18:32:17.782Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":76,"estimatedTokens":858}}294{"id":"stack-2628914","source":"stackoverflow","questionId":2628914,"title":"Integrating a Custom Compiler with the Visual Studio IDE","tags":[".net","visual-studio","compiler-construction","ide-customization"],"text":"Title: Integrating a Custom Compiler with the Visual Studio IDE\nTags: .net, visual-studio, compiler-construction, ide-customization\nSource: Stack Overflow\n\nQuestion:\n**Background**: I want to create a custom VB compiler, extending the \"original\" compiler, to handle my custom compile-time attributes.\n\n**Question**: after I've created my custom compiler and I've got an executable file capable of compiling VB code via the standard command-line interface, how do I integrate this compiler with the Visual Studio IDE? (such that pressing \"compile\" or \"build\" will make use of my compiler instead of the default compiler).\n\n**EDIT**: (Correct me if i'm wrong)\n\nFrom the reactions here, I see this question is a bit shocking, so I shall further explain my needs and background:\n.NET provides us with a great mechanism called Attributes. As far as I understand, making attributes apply their intended behavior upon the attributed element (assembly, module, class, method, etc.) - attributes must be reflected upon. So the real trick here is reflecting and applying behavior at the right spot. \n\nLets take Serialization for example: We decorate a class with the Serializable attribute. We then pass an instance of the class to the formatter's Serialize method. The formatter reflects upon the instance, checking if it has the Serializable attribute, and acting accordingly.\n\nNow, if we examine the Synchronization, Flags, Obsolete and CLSCompliant attributes, then the real question is: who reflects upon them? At least in some cases, it has to be the compiler (and/or IDE). Therefore, it seems that if I wish to create custom attributes that change an element's behavior regardless of any specific consumer, i must extend the compiler to reflect upon them at compilation. \n\nOf course, these are not my personal insights: the book \"Applied .NET Attributes\" provides a complete example of creating a custom attribute and a custom C# compiler to reflect upon that attribute at compilation (the example is used to implement \"java-style checked exceptions\").\n\n========================================\n\nTop Answer:\nCheck out \"BuildAction Property\" and \"CustomTool Property\".\n\nA few years ago, I read an excellent article about creating a custom compiler, registering it, and setting these properties. I cannot find the article now, but this should get you started.\n\n========================================\n\nComments:\n- Really? You *really* want to do this?\n- Why not? It is not like \"M.A. Hanin\" is trying to do something outside of his field.\n- @M.A. Hanin: I won't explain. If they are shocked then they shouldn't be answering the question. Correct?\n- @AMissico, I think that fear and shock should be addressed with reason. Sure, it isn't fun when people let you feel that \"this is outside of your league\", especially when they won't rationalize their claim, but I'm willing to explain myself - just because there is a chance my explanation will draw insightful comments. BTW, your answers are very appreciated, +2 from me.\n- @M.A. Hanin: Good book, I added to my books to read. I am very into custom attributes. Especially on Enum's.\n- The original link was dead so I replaced it but I can't be sure the new links are correct. They seem to be more or less on topic however.","metadata":{"transformedAt":"2026-08-18T18:32:17.782Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":36,"estimatedTokens":817}}295{"id":"stack-41910764","source":"stackoverflow","questionId":41910764,"title":"Where is the return object stored?","tags":["c++","compiler-construction"],"text":"Title: Where is the return object stored?\nTags: c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI generally understand how a function returns an object by value. But I wanted to understand it on the lower level. Assembly level if reasonable.\n\nI understand this this code\n\n```\nClassA fun(){\n ClassA a;\n a.set(...);\n return a;\n}\n```\n\nis transformed internally to\n\n```\nvoid fun(Class& ret){\n ClassA a;\n a.set(...);\n ret.ClassA::ClassA(a);\n}\n```\n\nWhich effectively call the copy constructor on the return value.\n\nI also understand that there are some optimizations (like NRVO) that could generate the following code avoiding the copy constructor.\n\n```\nvoid fun(Class& ret){\n ret.set(...);\n}\n```\n\nHowever my question is a bit more basic. It doesn't really have to do with Objects in specific. It could be even primitive types.\n\nLets say we have this code:\n\n```\nint fun(){\n return 0;\n}\nint main(){\n fun();\n}\n```\n\nMy question is where is the return object stored in memory.\n\nIf we look at the stack... There is the stack frame of `main` and then the stack frame of `fun`. Is the return object stored in some address like maybe between the two stack frames? Or maybe it is stored somewhere in the `main` stack frame (and possibly that's the address which is passed by reference in the generated code).\n\nI have thought about it and the second one seems more practical however I don't understand how the compiler know how much memory to push in the stack frame of `main`? Does it calculate what is the largest return type and push that even though there could be some wasted memory? Or is it done dynamically, it allocates that space only before the function is called?\n\n========================================\n\nTop Answer:\nThe answer is ABI specific but generally the calls are compiled with an hidden parameter which is the pointer to the memory that the function should use, like you said suppose the function is compiled as\n\n```\nvoid fun(Class& ret){\n ClassA a;\n a.set(...);\n ret.ClassA::ClassA(a);\n}\n```\n\nThen at call site you will have something like\n\n```\nClass instance = fun();\nfun(instance);\n```\n\nNow this makes the caller reserve `sizeof(Class)` bytes on the stack and pass that address to the function so that `fun` can \"fill\" that space.\n\nThis is no different from how the stack frame of the caller would reserve space for its own locals, the only difference is that the address to one of its locals is passed to `fun`.\n\nMind that if `sizeof(Class)` is less than the size of a register (or a couple of registers) it is totally possible that value is returned directly inside them.\n\n========================================\n\nCode:\n```text\nClassA fun(){\n    ClassA a;\n    a.set(...);\n    return a;\n}\n```\n\n```text\nvoid fun(Class& ret){\n    ClassA a;\n    a.set(...);\n    ret.ClassA::ClassA(a);\n}\n```\n\n```text\nvoid fun(Class& ret){\n    ret.set(...);\n}\n```\n\n```text\nint fun(){\n   return 0;\n}\nint main(){\n    fun();\n}\n```\n\n```text\nmain\n```\n\n```text\nfun\n```\n\n```text\nmain\n```\n\n```text\nmain\n```\n\n```text\nint fun()\n{\n   return 0;\n}\n```\n\n```text\nax\n```\n\n```text\neax\n```\n\n```text\nrax\n```\n\n```text\ndx:ax\n```\n\n```text\nvoid fun(Class& ret)\n```\n\n```text\nvoid fun(Class& ret){\n    ClassA a;\n    a.set(...);\n    ret.ClassA::ClassA(a);\n}\n```\n\n```text\nClass instance = fun();\nfun(instance);\n```\n\n```text\nsizeof(Class)\n```\n\n```text\nfun\n```\n\n```text\nfun\n```\n\n```text\nsizeof(Class)\n```\n\n========================================\n\nComments:\n- Depends on the compiler, but you can always look at its assembly.\n- By the way, `ret.ClassA::ClassA(a);` doesn't compile. This is how it should be written: `new (&ret) ClassA(a);`.\n- Strong implementation detail. In general the compiler will try to return anything in CPU registers. If it does not fit then it will have the caller reserve space on its stack frame and pass a pointer to this storage. The likely scenario in this case. Just try it on your own compiler, ask it to generate an assembly listing. You'll have a fact instead of a guess.\n- This is really misleading. Regardless of optimization, translation units must be able to call functions compiled inside other translation units, even if the two translation units have been compiled with different optimization settings. So optimization can only modify calling conventions for functions only visible within the translation unit they are defined in (i.e. `static`). For externally visible functions, the platform ABI rules the calling convention independent of optimization settings.\n- @rici what is misleading? My entire post, or some specific point that I am making?\n- The last two paragraphs where you suggest that optimization level affects thw calling convention.\n- @rici okay, my memory might be betraying me, perhaps you need to explicitly specify a specific calling convention like `fastcall` for those optimizations to happen. I will remove those parts from my answer.\n- Most compilers let you specify non-standard calling conventions but you need to do so in the header file so that both caller and callee produce interoperable code. Whether it us an optimization or not is a matter of opinion, but it is certainly not a *compiler* optimization since it is explicitly requested by the code.\n- @rici Right. However, in light of this, perhaps Sam Varshavchik's answer needs the same clarifications.\n- @MikeNakis: yes rici is correct. Think about the fact that if optimizations would be able to change calling conventions arbitrarily you would possibly have an object file with `foo(int x)` which expects the argument into a register and another object file which calls `foo` by passing it on the stack. Since compiled code is not aware of what's happening then the only choice is being sure that everything is compiled respecting the ABI contract from both sides.\n- @Jack yes, I know, and I have fixed my answer. But since we are discussing it, Isn't the compiler free to do whatever it pleases with functions that are private to a compilation unit? (`static` functons outside of classes, private methods of classes) Also, isn't there such a thing as \"whole program optimizations\"?\n- @MikeNakis: Even with whole program optimization, functions with external linkage are exported so that they could (in theory) be referenced by an external library, including one loaded at runtime with a dynamic loader. So the only case in which a compiler could optimize a calling convention is a function without external linkage, in which case the compiler is free to do what it pleases. In that case the most common optimization is partial or complete inlining.\n- Normally the calling convention is specified by the platform ABI, which may be OS-dependent but is not strictly speaking part of the OS, nor is it up to the compiler. Of course, documentation might be found anywhere (if you're lucky) or nowhere (if you're not) but searching for an ABI is often a good start.","metadata":{"transformedAt":"2026-08-18T18:32:17.782Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":20,"totalLines":204,"estimatedTokens":1716}}296{"id":"stack-1585067","source":"stackoverflow","questionId":1585067,"title":"Is there any self-improving compiler around?","tags":["language-agnostic","compiler-construction","artificial-intelligence","self-modifying"],"text":"Title: Is there any self-improving compiler around?\nTags: language-agnostic, compiler-construction, artificial-intelligence, self-modifying\nSource: Stack Overflow\n\nQuestion:\nI am not aware of any self-improving compiler, but then again I am not much of a compiler-guy.\n\n**Is there ANY self-improving compiler out there?**\n\nPlease note that I am talking about *a compiler that improves itself* - not *a compiler that improves the code it compiles*.\n\nAny pointers appreciated!\n\n**Side-note**: in case you're wondering why I am asking have a look at this post. Even if I agree with most of the arguments I am not too sure about the following:\n\nWe have programs that can improve\ntheir code without human input now —\nthey’re called compilers.\n\n... hence my question.\n\n========================================\n\nTop Answer:\nMilepostGCC is a MachineLearning compiler, which improve itself with time in the sense that it is able to change itself in order to become \"better\" with time. A simpler iterative compilation approach is able to improve pretty much any compiler.\n\n========================================\n\nComments:\n- Self-improving in what aspect? How would the compiler be improving itself? Is it adding a new language on it's own, so gcc decides it wants to compile Ruby, and so learns how? Are improve how it compiles C by adding a new optimization level?\n- What about a compiler that compiles itself?\n- @JamesBlack self-improving in ANY Aspect :) - I am just trying to understand if there's such a thing\n- Until you can say specifically what you mean this is \"Not a Real Question\". I mean, do you think that the compiler can comprehend it's own performance and decide what to do about it?\n- I mean ANY self-improving aspect, including the one you mention, which sounds like a question to me. It's not about what I think, I am just reaching out to understand if there's any current research on the topic at ANY level.\n- Does profile-guided optimization count? I know the Intel and TI compilers support this, but have never tried it out\n- They need to optimize their own source-code (not the code they compile) and possibly recompile themselves in order for that to count! :)\n- A self-hosted compiler would \"improve itself\" by compiling itself, wouldn't it?\n- What do you mean by self-hosted? If it compiles itself it has the potential to improve itself (even in trivial ways) but it might be not actually doing it\n- Just so you know, self-hosting compilers are not rare. SBCL is self-hosting, gcc is self-hosting, etc.\n- @jrockway - self-hosting compilers compile themselves, meaning that if they optimize the code they compile ... they optimize themselves. Am I onto something or is it just a case of dog chasing its tail?\n- Until you rigorously define \"improve\" in terms of compilers, any discussion on this topic is a dance of ignorance.\n- @Sam Rigor is not required - I am good with whatever people think is an improvement, as long as it's a self-improvement to the compiler source code made by the compiler itself.\n- Ah -- the irony ... your post is even tagged \"singularity\"\n- \"then it would become a virus to make all devs use it\"... so Java, then?\n- I don't know about you, but *my* compilers attend night classes to learn underwater basket-weaving!\n- skynet imporved its compiler\n- Firstly : that's not self improving is it? Secondly : If someone thinks that that is an application of AI, it is not. What it simply does is checks for performance bottlenecks and tries to improve those by compromising on other things.\n- Thanks, I see your point but I am talking about a compiler that improves itself - not the code that it compiles.\n- so the answer is: NO - there's no such a thing out there. Time travel is possible in theory but I don't see anyone from the future :) - All jokes aside, thanks for the link. Any other examples you can think of or Goedel machines are the only theorized examples of such a thing?\n- They are state-of-the-art to my knowledge, previous work was done by Marcus Hutter (hutter1.net), a student of Schmidhuber. But I need to be clear: Goedel machines are not \"theorized\" in the sense that there are vague statements that they are possible; no, in Schmidhuber's work there is a clear plan to build (program) one. What is left is engineering, which is not necessary easy: in particular it requires an axiomatic model of the hardware performance where it run. With current crappy CPUs is unrealistic a deterministic performance model, I was thinking to let the program...\n- .. monitor itself (via hardware performance counters), learn and perfect incrementally an axiomatic probabilistic performance model. But this is pure speculation.\n- From the linked page - \"The searcher systematically and efficiently tests computable proof techniques (programs whose outputs are proofs) until it finds a provably useful, computable self-rewrite. We show that such a self-rewrite is globally optimal - no local maxima! - since the code first had to prove that it is not useful to continue the proof search for alternative self-rewrites.\" How long does that typically take to run? That sounds alot like it would be a race against the heat death of the universe. Especially since existing compiler theory is pretty advanced already.\n- No: time is equally divided between doing the actual task and searching for better solutions. Imagine a robot executing a plan - if there is a stringent time-limit, at worst it will complete the task executing a less efficient plan. So, if there is a time-limit the actual task will be completed anyway (if both the plan is computable and doable in that time-limit, of course). Also, if one have a good starting point there is nothing that prevent to start from there - possibly a rewrite will discover a better version, not (yet) computable by traditional means.\n- @MaD70: Didn't find any reference to Goedel machines on Hutter's page (I may not have known exactly what to look for), but it looks impossible to me. It isn't possible to algorithmically find the simplest program equivalent to program X. If it were, we could, say, write a program that would test Goldbach's conjecture and either print a counterexample or never stop, and reduce it to either a simple infinite loop or a print statement. Any sort of exhaustive search would race against proton decay and lose.\n- @David Thornley: the link points directly to Goedel machines home page on Schmidhuber's site: idsia.ch/~juergen/goedelmachine.html Read a paper on *that* page: he does not wrote that a Goedel machine will find \"the simplest program equivalent to program X\". A Goedel machine will rewrite itself incrementally and it only assures, by proof, that each rewrite will be better than the preceding version. Eventually, it will find by chance, in the time allotted, a global optimal rewrite, but this is not guaranteed, of course, and you can never be sure that current rewrite is optimal.\n- @MaD70: Thanks, found a preprint there. It appears to me that the machine will limit itself to provably correct improvements, which has its own issues. Note that a deterministic CPU performance model is almost certainly slower than a real model, which means that it's got a performance deficit to begin with. It doesn't look like it'll be practical to me, although we're likely to learn stuff by trying it.\n- @David Thornley: you are welcome. On an *axiomatic* deterministic CPU performance model: you need to understand it NOT as \"emulation\" but as instruction cycles counting, as in the past. So given a rewrite, you can calculate its performance by a simple addition of each instruction cycle of the rewritten program. But see next comment.\n- @youngsters: in the past, CPU datasheets declared for each instruction how many cycles were needed to execute them (1 cycle = 1 tick of clock). CPUs performances were much more predictable in that time. Of course, nowadays we have a completely different picture. For that reason in the second comment to this answer I alluded to an axiomatic **probabilistic** performance model, automatically constructed (machine learning again).\n- So your argument in favor is an non-existent machine based on a vague unimplementable theory? Sorry, the answer is NO.\n- I am talking more of improvements to the compiler source-code by the compiler itself","metadata":{"transformedAt":"2026-08-18T18:32:17.783Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":60,"estimatedTokens":2077}}297{"id":"stack-13348439","source":"stackoverflow","questionId":13348439,"title":"How can you print instruction in llvm","tags":["c++","c","compiler-construction","llvm"],"text":"Title: How can you print instruction in llvm\nTags: c++, c, compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nFrom an llvm pass, I need to print an llvm instruction (Type `llvm::Instruction`) on the screen, just like as it appears in the llvm bitcode file. Actually my compilation is crashing, and does not reach the point where bitcode file is generated. So for debugging I want to print some instructions to know what is going wrong.\n\n========================================\n\nTop Answer:\nAssuming I is your instruction\n\n```\nI.print(errs());\n```\n\n========================================\n\nCode:\n```text\nllvm::Instruction\n```\n\n```text\nprint\n```\n\n```text\nI.print(errs());\n```\n\n```text\nfor(auto& B: F){\n        for(auto& I: B){\n            errs() << I << \"\\n\";\n        }\n      }\n```\n\n```text\n%3 = alloca i32, align 4\n  %4 = alloca i8**, align 8\n  store i32 %0, i32* %3, align 4\n  store i8** %1, i8*** %4, align 8\n  %5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))\n  ret i32 0\n```\n\n========================================\n\nComments:\n- off topic: I like the way you're still using the generated name for about 3 years..\n- Can you give an example? Thanks","metadata":{"transformedAt":"2026-08-18T18:32:17.783Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":53,"estimatedTokens":305}}298{"id":"stack-14039843","source":"stackoverflow","questionId":14039843,"title":"Does a compiler always produce an assembly code?","tags":["gcc","assembly","compiler-construction"],"text":"Title: Does a compiler always produce an assembly code?\nTags: gcc, assembly, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nFrom **Thinking in C++ - Vol 1**:\n\n In the second pass, the code generator walks through the parse tree\n and generates either assembly language code ***or*** machine code for the\n nodes of the tree.\n\nWell at least in GCC if we give the option of generating the assembly code, the compiler obeys by creating a file containing assembly code. ***But, when we simply run the command `gcc` without any options does it not produce the assembly code internally?***\n\nIf yes, then why does it ***need*** to first produce an assembly code ***and then*** translate it to machine language?\n\n========================================\n\nTop Answer:\nThe assembler stage can be justified by two reasons:\n\n- it allows c/c++ code to be translated to a machine independent abstract assembler, from which there exists easy conversions to a multitude of different instruction set architectures\n\n- it takes out the burden of validating correct opcode, prefix, r/m, etc. instruction encoding for CISC architectures, when one can utilize an existing software [component].\n\nThe 1st edition of that book is from 2000, but is may as well talk about the early 90's, when c++ itself was translated to c and when the gnu/free software idea (including source code for compilers) was not really known.\n\n**EDIT**: One of several nonsensical abstract machine independent languages used by GCC is RTL -- Register Transfer Language.\n\n========================================\n\nCode:\n```text\ngcc\n```\n\n```text\n.o\n```\n\n```text\nrustc\n```\n\n```text\n.o\n```\n\n```text\n.obj\n```\n\n```text\njavac\n```\n\n```text\nas\n```\n\n```text\n.o\n```\n\n```text\n.obj\n```\n\n```text\ntcc\n```\n\n```text\nas\n```\n\n```text\ngcc\n```\n\n```text\ncc1\n```\n\n```text\nas\n```\n\n```text\n_\n```\n\n```text\nas\n```\n\n```text\n.globl main\n```\n\n```text\n.asciiz \"Hello World!\\n\"\n```\n\n```text\nas\n```\n\n```text\ncc1\n```\n\n========================================\n\nComments:\n- Assembly language is just a plain-text version of machine code. It is easier to read, but there is a 1:1 correspondence.\n- @DanBystr&#246;m thanks, but that's not the question.\n- Note: not every compiler generates code for a *physical* machine. There are compilers that generate code for a *virtual* machine, such as P-code, or the code ran by a flash application, or maybe even the code ran by an executor for a query-engine. Just-in-time-code is another exception, it may or may not be compiled to physical opcodes for a physical machine. Generally speaking: any intermediate representation of the code may exist at some stage of the compilation process.\n- An example is dmd compiler that doesn't generate assembly code.\n- @AnishaKaul For compiled program the translation is performed only once (so it's acceptable) and for interpreted program the translation is performed every time it's executed\n- Easier to debug by reading ascii than bits. And if you already have a reliable tool that goes from asm to object, use it (the unix way, build layers on top of other tools).\n- \"Machine independent abstract assembler\" is just nonsense.\n- This doesn't explain why `as` is a separate program that the `gcc` front-end actually runs separately from `cc1` (the C -> asm preprocessor+compiler). Sure gcc uses GIMPLE and RTL internally, but it doesn't write text representations of those IR formats to files unless you use a special option for debugging. LLVM uses LLVM-IR internally, and also has a built-in assembler that knows about different object-file formats for each target (ELF, PE/COFF, MachO64 on x86-64, etc.) Object file formats are one of the main reasons for keeping the assembler separate, AFAIK.\n- *compilers like javac that target a portable bytecode format have less reason to use asm* - the Java byte code is very high level so the assembly stage does not really apply.\n- also to my understanding gcc started using the assembler provided by the Unix vendor instead of having to bring its own. This was one of the reasons that a binary distribution was made of gcc for Solaris as the compiler tool chain was not part of the basic operating system.\n- @Thorbj&#248;rnRavnAndersen: Indeed, that's a good example of GCC working with a vendor-supplied assembler / linker like I mentioned, instead of needing GNU `as` / `ld` too, to support the file formats. Re: Java bytecode: it's still a binary format with instructions, and one can make a text representation of it (e.g. for debugging purposes). The main reason it's not useful to separate the parser/compiler logic from writing `.class` files is that the `.class` file format is portable; there's no need to consider writing Java bytecode into different formats of binary files. Not that it's high-level.","metadata":{"transformedAt":"2026-08-18T18:32:17.783Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":20,"totalLines":125,"estimatedTokens":1191}}299{"id":"stack-4729097","source":"stackoverflow","questionId":4729097,"title":"extending the c programming language with gcc","tags":["c","gcc","compiler-construction","programming-languages","translation"],"text":"Title: extending the c programming language with gcc\nTags: c, gcc, compiler-construction, programming-languages, translation\nSource: Stack Overflow\n\nQuestion:\nI want to write my own programming language as an extension of the c programming language. The entire programming language that I am inventing are simply shorthands which translate to valid c code. For example:\n\n```\nnamespace TcpConnection {\n void* connect(char *addr)\n}\n```\n\nwould translate to:\n\n```\nvoid* TcpConnection_connect(char *addr)\n```\n\nAll that is done is a simple name replacement. This is only one example of an extension which I want to provide. Another simple extension would be function overloading (this would concatenate to the end of the function name the types of its arguments.\n\nIn any case, the result is perfectly valid C code. Is there any way to do this without going into gcc code?\n\n========================================\n\nTop Answer:\nFor prototyping, maybe just go with a preprocessor written in the language of your choice (C, Perl, Python...) and then build it into your Makefile rules. Just to get an easy, low-cost way to try it all out...\n\nUse a different file extension, and turn .foo into .c.\n\n========================================\n\nCode:\n```text\nnamespace TcpConnection {\n    void* connect(char *addr)\n}\n```\n\n```text\nvoid* TcpConnection_connect(char *addr)\n```\n\n```text\n#define x3_pragma_(...) _Pragma(#__VA_ARGS__)\n#define x3_asm(...) ((const struct x3instruction []){ \\\n    x3_pragma_(X3 ASM __VA_ARGS__) \\\n})\n```\n\n```text\nx3_asm(exit = find val(0))\n```\n\n```text\n((const struct x3instruction []){\n#pragma X3 ASM exit = find val(0)\n})\n```\n\n```text\n((const struct x3instruction []){\n{ { { X3_OPFINDVAL, { .as_uint = (0) } }, { X3_OPEXIT, { 0 } } } },\n})\n```\n\n```text\ngcc -E foo.c | perl x3pp.pl | gcc -o foo.o -x c -\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.783Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":69,"estimatedTokens":455}}300{"id":"stack-526351","source":"stackoverflow","questionId":526351,"title":"How do I unroll (compile) an interpreter loop?","tags":["c","optimization","compiler-construction","programming-languages","interpreter"],"text":"Title: How do I unroll (compile) an interpreter loop?\nTags: c, optimization, compiler-construction, programming-languages, interpreter\nSource: Stack Overflow\n\nQuestion:\nI heard that some languages go from interpreted to compiled by \"unrolling the interpreter loop\".\n\nLet's say I have the following pseudo-c-code interpreter for an ast tree.\n\n```\nint interpret(node)\n{\n switch(node) {\n case PLUS:\n return interpret(child(0))+interpret(child(1));\n case MINUS:\n return interpret(child(0))-interpret(child(1)); \n }\n}\n```\n\nHow do I unroll this loop to create a compiled program?\n\n**I see you all downvoting this like I don't know what I am talking about, but here is a quote from Wikipedia that states exactly what I am describing.**\n\n*\"Factor was originally only interpreted, but is now fully compiled (the non-optimizing compiler basically unrolls the interpreter loop\"*\n\n========================================\n\nTop Answer:\nI'm slightly confused. I don't think 'unrolling the loop' is the right term here. Even if you refactor the code to not have any recursive calls, you will still be using an interpreter.\n\nYou can compile this program with GCC. Then you will have a compiled program, albeit the compiled program will be interpreting the AST.\n\nOne way to turn this into a compiler would be, instead of doing `return interpret(child(0))+interpret(child(1));`, you would generate assembly instructions which would do the addition instead, and then output those to a file.\n\n========================================\n\nCode:\n```text\nint interpret(node)\n{\n    switch(node) {\n        case PLUS:\n             return interpret(child(0))+interpret(child(1));\n        case MINUS:\n             return interpret(child(0))-interpret(child(1));       \n    }\n}\n```\n\n```text\nfor (int i = 0; i < 4; ++i) {\n    a[i] = b[i] + c[i];\n}\n```\n\n```text\na[0] = b[0] + c[0];\na[1] = b[1] + c[1];\na[2] = b[2] + c[2];\na[3] = b[3] + c[3];\n```\n\n```text\nASSIGN\n    |\n +--+---+\n |      |\nREF   MINUS\n |      |\n x   +--+---+\n     |      |\n    VAR    PLUS\n     |      |\n     a   +--+--+\n         |     |\n        VAR  CONST\n         |     |\n         b     3\n```\n\n```text\nint interpret(node) {\n    switch(node) {\n        case PLUS:\n             return interpret(child(0))+interpret(child(1));\n        case MINUS:\n             return interpret(child(0))-interpret(child(1));       \n        case ASSIGN:\n             return set(child(0), interpret(child(1));\n        case VAR:\n             return fetch(child(0));\n        case CONST:\n             return value(child(0));\n        ...\n    }\n}\n```\n\n```text\nstring compile(node) {\n    switch(node) {\n        case PLUS:\n             return(compile(child(0))) + compile(child(1)) + ADD);\n        case MINUS:\n             return(compile(child(0))) + compile(child(1)) + SUB);\n        case ASSIGN:\n             return(PUSHA(child(0))) + compile(child(1)) + STORE);\n        case REF:\n             return(PUSHA(child(0)));\n        case VAR:\n             return(PUSHA(child(0)) + FETCH);\n        case CONST:\n             return(PUSHLIT + value(child(0)));\n        ...\n    }\n}\n```\n\n```text\nPUSHA x\nPUSHA a\nFETCH\nPUSHA b\nFETCH\nPUSHLIT 3\nADD \nSUB\nSTORE\n```\n\n```text\ninterpret\n```\n\n```text\ninterpet\n```\n\n```text\ncompile\n```\n\n```text\nreturn interpret(child(0))+interpret(child(1));\n```\n\n```text\nint compile(node)\n{\n    switch(node) {\n        case PLUS:\n             return compile(child(0))&&compile(child(1))&&compile_op(op_plus);\n        case MINUS:\n             return compile(child(0))&&interpret(child(1))&&compile_op(op_minus);       \n    }\n}\n```\n\n```text\ninterpret\n```\n\n```text\nfor (;;)\n    stack = (stack[0].function_pointer)(stack);\n```\n\n```text\ndef foo (x,y):\n   print( add(x, y) )\n```\n\n```text\npop a\npop b\nstack[ return_offset ] = a + b\nreturn stack\n```\n\n```text\npop x\npop y\npush _\npush &print\npush y\npush x\npush &add\n```\n\n```text\n>> foo(5,6)\n[&foo, 5, 6]\n[&add, 5, 6, &print, _]\n[&print, 11]\n=> 11\n[]\n```\n\n```text\ncompiled_foo (stack): \n    stack = begin_foo(stack) // arranges stack for call to add\n    stack = add(stack)\n    stack = print(stack)\n    return stack\n```\n\n========================================\n\nComments:\n- I think that the Wikipedia usage of “loop unrolling” here is completely appropriate, even if rather figurative. Valid, and what's more, rather interesting, question.\n- You could perhaps mention what kind of code you are producing here (stack machine vs. other forms of intermediate language). Otherwise, good answer.\n- @Konrad: Thanks for the suggestion. I edited to incorporate it.","metadata":{"transformedAt":"2026-08-18T18:32:17.783Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":19,"totalLines":214,"estimatedTokens":1128}}301{"id":"stack-19101449","source":"stackoverflow","questionId":19101449,"title":"How does compiler lay out code in memory","tags":["memory","assembly","compiler-construction","operating-system","virtual-memory"],"text":"Title: How does compiler lay out code in memory\nTags: memory, assembly, compiler-construction, operating-system, virtual-memory\nSource: Stack Overflow\n\nQuestion:\nOk I have a bit of a noob student question.\n\nSo I'm familiar with the fact that stacks contain subroutine calls, and heaps contain variable length data structures, and global static variables are assigned to permanant memory locations. \n\nBut how does it all work on a less theoretical level?\n\nDoes the compiler just assume it's got an entire memory region to itself from address 0 to address infinity? And then just start assigning stuff?\n\nAnd where does it layout the instructions, stack, and heap? At the top of the memory region, end of memory region?\n\nAnd how does this then work with virtual memory? The virtual memory is transparent to the program?\n\nSorry for a bajilion questions but I'm taking programming language structures and it keeps referring to these regions and I want to understand them on a more practical level.\n\nTHANKS much in advance!\n\n========================================\n\nTop Answer:\nThis is a wide open question with lots of topics.\n\nAssuming the typical compiler -> assembler -> linker toolchain. The compiler doesnt know a whole lot, it simply encodes stack relative stuff, doesnt care how much or where the stack is, that is the purpose/beauty of a stack, dont care. The compiler generates assembler the assembler is assembled into an object, then the linker takes info linker script of some flavor or command line arguments that tell it the details of the memory space, when you \n\n```\ngcc hello.c -o hello\n```\n\nyour installation of binutils has a default linker script which is tailored to your target (windows, mac, linux, whatever you are running on). And that script contains the info about where the program space starts, and then from there it knows where to start the heap (after the text, data and bss). The stack pointer is likely set either by that linker script and/or the os manages it some other way. And that defines your stack.\n\nFor an operating system with an mmu, which is what your windows and linux and mac and bsd laptop or desktop computers have, then yes each program is compiled assuming it has its own address space starting at 0x0000 that doesnt mean that the program is linked to start running at 0x0000, it depends on the operating system as to what that operating systems rules are, some start at 0x8000 for example.\n\nFor a desktop like application where it is somewhat a single linear address space from your programs perspective you will likely have .text first then either .data or .bss and then after all of that the heap will be aligned at some point after that. The stack however it is set is typically up high and works down but that can be processor and operating system specific. that stack is typically within the programs view of the world the top of its memory.\n\nvirtual memory is invisible to all of this the application normally doesnt know or care about virtual memory. if and when the application fetches an instruction or does a data tranfer it goes through hardware which is configured by the operating system and that converts between virtual and physical. If the mmu indicates a fault, meaning that space has not been mapped to a physical address, that can sometimes be intentional and then another use of the term \"Virtual memory\" applies. This second definition the operating system can then for example take some other chunk of memory, yours or someone elses, move that to hard disk for example, mark that other chunk as not being there, and then mark your chunk as having some ram then let you execute not knowing you were interrupted with some ram that you didnt know you had to take from someone else. Your application by design doesnt want to know any of this, it just wants to run, the operating system takes care of managing physical memory and the mmu that gives you a virtual (zero based) address space...\n\nIf you were to do a little bit of bare metal programming, without mmu stuff at first then later with, microcontroller, qemu, raspberry pi, beaglebone, etc you can get your hands dirty both with the compiler, linker script and configuring an mmu. I would use an arm or mips for this not x86, just to make your life easier, the overall big picture all translates directly across targets.\n\n========================================\n\nCode:\n```text\n.text\n```\n\n```text\n.data\n```\n\n```text\n0x80000000\n```\n\n```text\nint main()\n```\n\n```text\ngcc hello.c -o hello\n```\n\n========================================\n\nComments:\n- Can I ask a quick up? Is the underlying machinery for calling functions, creating their local variables and adding them to the stack included in the program when it's compiled? Or is the machinery a part of the operating system?\n- Thanks! So it's safe to assume you have the entire memory for yourself I see.\n- This is great. Exactly was looking for. Any recommendations on good sites/books on this stuff? I don't even know what to google. I don't mean parse trees but more on how all this stuff practically works at the low level when converting the parse tree to code.\n- A good place to start would be a google search for \"windows memory layout\". This link comes up on the first page and looks to be pretty informative: Anatomy of a Program in Memory. From here you could probably find some other concepts to google.\n- Thanks! One thing that was throwing me off was that you have to declare an address to store a variable in assembly so I was thinking the compiler/assembly generator just assumed that it had it's own full address space.","metadata":{"transformedAt":"2026-08-18T18:32:17.783Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":73,"estimatedTokens":1402}}302{"id":"stack-11712479","source":"stackoverflow","questionId":11712479,"title":"Which bitset implementation should I use for maximum performance?","tags":["c++","performance","compiler-construction","bitset"],"text":"Title: Which bitset implementation should I use for maximum performance?\nTags: c++, performance, compiler-construction, bitset\nSource: Stack Overflow\n\nQuestion:\nI'm currently trying to implement various algorithms in a Just In Time (JIT) compiler. Many of the algorithms operate on bitmaps, more commonly known as bitsets.\n\nIn C++ there are various ways of implementing a bitset. As a true C++ developer, I would prefer to use something from the STL. The most important aspect is performance. I don't necessarily need a dynamically resizable bitset.\n\nAs I see it, there are three possible options. \n\nI. One option would be to use `std::vector`, which has been optimized for space. This would also indicate that the data doesn't have to be contiguous in memory. I guess this could decrease performance. On the other hand, having one bit for each bool value could improve speed since it's very cache friendly. \n\nII. Another option would be to instead use a `std::vector`. It guarantees that the data is contiguous in memory and it's easier to access individual elements. However, it feels strange to use this option since it's not intended to be a bitset. \n\nIII. The third option would be to use the actual `std::bitset`. That fact that it's not dynamically resizable doesn't matter.\n\nWhich one should I choose for maximum performance?\n\n========================================\n\nTop Answer:\nI answered a similar question recently in this forum. I recommend my BITSCAN library. I have just released version 1.0. BITSCAN is specifically designed for fast bit scanning operations. \n\nA BitBoard class wraps a number of different implementations for typical operations such as *bsf*, *bsr* or *popcount* for 64-bit words (aka bitboards). Classes BitBoardN, BBIntrin and BBSentinel extend bit scanning to bit strings. A bit string in BITSCAN is an array of bitboards. The base wrapper class for a bit string is BitBoardN. BBIntrin extends BitBoardN by using Windows compiler intrinsics over 64 bitboards. BBIntrin is made portable to POSIX by using the appropriate asm equivalent functions.\n\nI have used BITSCAN to implement a number of efficient solvers for NP combinatorial problems in the graph domain. Typically the adjacency matrix of the graph as well as vertex sets are encoded as bit strings and typical computations are performed using bit masks. Code for simple bitencoded graph objects is available in GRAPH. Examples of how to use BITSCAN and GRAPH are also available.\n\nA comparison between BITSCAN and typical implementations in STL (*bitset*) and BOOST (*dynamic_bitset*) can be found here: \nhttp://blog.biicode.com/bitscan-efficiency-at-glance/\n\n========================================\n\nCode:\n```text\nstd::vector<bool>\n```\n\n```text\nstd::vector<char>\n```\n\n```text\nstd::bitset\n```\n\n```text\nstd::vector<bool>\n```\n\n```text\nstd::vector<char>\n```\n\n```text\nstd::bitset\n```\n\n```text\nstd::vector<char>\n```\n\n```text\nstd::vector<char>\n```\n\n========================================\n\nComments:\n- Benchmark! Related.\n- There is also Boost.Dynamic Bitset to consider. But seriously there is really no way to tell which performance has best performance without knowing the usage pattern. For example: If your collection is small and often accessed `vector` might give you faster access then the bitsets, due to not having to do bitshifting/masking. However when less often accessed/bigger the higher amount of cache misses due to the bigger memory footprint might very will kill that benefit.\n- At the risk of pointing out something possibly obvious: the std::bitset is allocated on the stack and is thus pretty limited in maximum size in most cases. I don't know anything about the amount of data you need to store, however.\n- How big does it need to be? I mean, can you just fit it in an unsigned long long or something like that?\n- The `operator[]` are similar enough yes, but the constructors aren't.\n- @MooingDuck: True. I use typedef's to simplify migration from one type to another, but not to make it effortless. I also use typedef's for collections so I can hide the real implementation (list, vector, deque, ...), which reduces the real code changes with about 90% if I ever change container type.\n- Shortly, here is the conclusion of the paper: \"We have shown that `boost::dynamic_bitset` is considerably more efficient than most of the other implementations in terms of execution speed, while the implementation using `std::vector` outperformed the other implementations in terms of memory efficiency.\"","metadata":{"transformedAt":"2026-08-18T18:32:17.783Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":76,"estimatedTokens":1127}}303{"id":"stack-716613","source":"stackoverflow","questionId":716613,"title":"How to make a .Net or JVM language?","tags":[".net","compiler-construction","jvm"],"text":"Title: How to make a .Net or JVM language?\nTags: .net, compiler-construction, jvm\nSource: Stack Overflow\n\nQuestion:\nI am seeing all these new languages for .NET and JVM. How does one begin to make one? \n\nI can't find any good documentation on JVM or MSIL specifications.\n\n**Edit**\n\nI already know how to parse, I am more interested in how there are so many people making new languages that are based on those platforms.\n\n========================================\n\nTop Answer:\nThe Java Virtual Machine Specifications, Second Edition is available online.\n\nOne can target the JVM by having a writing a compiler that produces Java bytecode (which is defined in the JVM specifications.)\n\nOr, by utilizing existing libraries such as Apache BCEL, one can produce valid `class` files through a Java program without having to actually manipulate the bytecode at the byte-level.\n\n========================================\n\nCode:\n```text\nclass\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.783Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":28,"estimatedTokens":234}}304{"id":"stack-3901947","source":"stackoverflow","questionId":3901947,"title":"How do compilers optimize our code?","tags":["c++","c","optimization","compiler-construction","compiler-optimization"],"text":"Title: How do compilers optimize our code?\nTags: c++, c, optimization, compiler-construction, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nI ran into this question when i was answering another guys question. How do compilers optimize the code? Can keywords like const, ... help? Beside the fact with volatiles and inline functions and how to optimize the code all by your self!\n\n========================================\n\nTop Answer:\nOne very big thing you can do ( beyond what the compiler can do for you ) is to be aware of the cache. Since accessing the memory is really time expensive, the cache tries to help you by storing not only the data you accessed it but the nearby elements as well. This is why `foo` will run so much faster than `bar`:\n\n```\narray[ NUM_ROWS ][ NUM_COLS ];\n\nfoo() \n{\n int row, col;\n int sum = 0;\n\n // accesses the elements in the array continuously\n for ( row = 0; row **Edit:**\nAnother thing to be aware of is repeated string concatenation. Done wrong, this can make code that otherwise seems to run in `O( n )` actually be in `O( n^2 )` - see an article on Joel on Software\n\n**Edit:** s/disk/memory/\n\n========================================\n\nCode:\n```text\narray[ NUM_ROWS ][ NUM_COLS ];\n\nfoo() \n{\n    int row, col;\n    int sum = 0;\n\n    // accesses the elements in the array continuously\n    for ( row = 0; row < NUM_ROWS ; row++ ) \n    {\n         for ( col = 0; col < NUM_COLS; col++ )\n         {\n              sum += array[ row ][ col ];\n         }\n    }\n}\n\nbar() \n{\n    int row, col;\n    int sum = 0;\n\n    // skips from row to row ( big jumps that might miss the cache )\n    for ( col = 0; col < NUM_COLS ; col++ ) \n    {\n         for ( row = 0; row < NUM_ROWS; row++ )\n         {\n              sum += array[ row ][ col ];\n         }\n    }\n}\n```\n\n```text\nfoo\n```\n\n```text\nbar\n```\n\n```text\nO( n )\n```\n\n```text\nO( n^2 )\n```\n\n========================================\n\nComments:\n- This is my second day in stackoverflow, but I see the const optimization topic for the third time already... Why are programmers so obsessed with optimization especially when in most cases it is not needed? This is bound to be a dupe\n- Very interesting question. Try reading en.wikipedia.org/wiki/Compiler_optimization\n- @ djechelon: Thanks :D, I already have done that. I mean the very specific cases. You know, the cases which are concerned mostly with gcc.\n- @Armen - May I ask why you believe optimization is 'not needed'?\n- @linuxuser27- If you're creating a calculator app that adds 1+1, do you really need to optimize? The answer will show in less than a blink of an eye anyways- this is what cheap calculators do- use the slowest processor that can still produce the answer in an unnoticeable amount of time. Of course, optimization is needed in *some* situations, just not most of them. I do believe optimization is something some programmers like to do though.\n- If you're really interested, head on over to a large library (university library is most likely to have) and pick up a copy of \"The Dragon Book\" (en.wikipedia.org/wiki/Dragon_Book_%28computer_science%29). Any edition will do. Even if you're not interested in writing a compiler, I guarantee that you will learn *something* from this book.\n- @DMan - Agreed of course there are places where optimization will not occur. But that was not the statement. It was 'in most cases it is not needed'. I find this kind of naive. If compilers did not optimize code most programs would run considerably slower. So my question is why does one state that in 'most cases' optimization is not needed?\n- Also here is a good link i found, which is not my answer but, it is very great: strchr.com/what_your_compiler_can_do_for_you\n- @linuxuser27: I think what @Armen was trying to say was \"I most cases, [programmer-guided] optimization is not needed.\" Really, choosing the simplest and cleanest approach and algorithm for a problem has far more beneficial results than trying to squeeze an extra instruction through the CPU.\n- Amen @Armen ;) For some applications, optimizing the bottlenecks is viable. But that should at most be 1% percent of the programming. Yet still, it feels like 10% of the questions at SO go like \"is this fast than this\" or \"will this be optimized\", in only 15% of the cases the code in question might actually be a bottleneck and at most in 5% the OP actually *knows* it's a bottleneck. The cases where it's actually relevant is another order of magnitude lower. It's horrible.\n- GMan answered for me :) Always take heed of the two rules of optimization\n- @GMan and Armen - I see. I completely agree with you then. Manual optimizations are likely not need most of the time.\n- @linuxuser27: I'm sort of surprised at the idea that manual optimization is not needed. For little micro-optimizations I can see that, but there's a wide world of macro optimization, where the stakes are much higher, and only the programmer can do it. And it's not just about the \"right algorithm\". It's about sweeping away complexity. Example: 43x speedup - stackoverflow.com/questions/926266/&hellip;\n- Funny thing you should care about such low-level things and still write row++ instead of ++row :P\n- What does your example code have to do with accessing the disk?\n- being aware of the cache will save you a lot more than ++row :P @jayrdub: that involves an explanation of how memory *actually* works in the machine. Basically, `array[ row ][ col ]` is a call to main memory which is originally stored on the hard disk. Because the hard disk moves so much slower than the CPU, computers will store information in a 'cache' where it is easier to access.\n- I think you have a weird computer\n- @Armen Tsirunyan - The value of the expression `row++` is not used here. Your compiler will likely generate identical code for `++row` and `row++` in such a case case.\n- @jayrdub you should look at some assembly when you get a chance.\n- @Alex: Tanx Alex, this was the answer i gave to the guy who i answered his question. Other stuff like this are hidden in our code and we don't notice. I really appreciate experiences :D.\n- @Alex: For most cases (i.e. those not involving array a large fraction of the size of main memory) the cache that is at issue when dealing with row-major versus column-major array access schemes is one or more levels of the *CPU* cache and has nothing to do with the *disk* cache.\n- @Alex Reece - @jayrdub is right, this is memory, not disk. That said, memory and disk can be equivalent, eg. if this part of memory is not paged in and the kernel will have to go to disk to retrieve it.\n- So you're saying accessing column-major vs row-major won't increase my L1 cache hit rate?\n- @Alex Reece - I don't think anyone is saying that. The objection is that you say \"disk\" when you are referring to memory.\n- @Alex Reece: One of the most common optimization is turning the loops inside out to increase cache hit. It is likely that the optimizer would transform the column-major version into a row-major if it detects that (given the size) this should increase the cache hit.\n- @Alex Reece: Wikipedia's article: en.wikipedia.org/wiki/Loop_interchange\n- @Matthieu Dang, thats nice. What compilers do that?\n- @Alex: gcc (-floop-interchange) see gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html, and I think it's the one called loop-rotate in llvm: llvm.org/docs/Passes.html#loop-rotate\n- The question is not about manual optimization, but what compilers may do and how they do it.\n- Hi, Thank you for your advice, but i guess i am on the edge of learning it completely so i don't wanna misunderstand it.\n- @DerKuchen: well, it's about both of those.\n- compilers can do that if they know what hardware their code is running in! sometimes should the GPU come in, they mess up. I recently wrote a code which used both cpu and gpu (cuda) and the bug was just that simple O2 optimization. When i turned it off, all things made sense.\n- @Green Code: Compilers are software too, so of course they are sometimes buggy. But for mature compilers, the output is usually correct and blazing fast compared to whatever most programmers could write on their own.\n- @Green Code: there are various level of optimizations. Some are machine independent, some not. Machine dependent may not be bug-less, especially for young architecture, simply because they haven't been tested extensively yet. CUDA brings a new difficulty too: suddenly there are parts of the code that should be optimized for CPU and others for GPU. None of the C++ compilers that I know of have been meant to optimize for two different architectures at once.\n- Compiler's aren't that smart. They can't even re-order a simple Boolean expression like (A && B && C && D) to optimize the short-circuiting. They play it safe, because they can't determine whether methods have side effects, and since they can't determine that, they can't safely re-order the expression. Since they aren't doing so, they aren't even bothering to calculate operand complexity at compile-time, nor do they profile the cost of evaluating the OPs to see which ones best trigger short-circuits and which ones are expensive and better to be short-circuited (i.e. ordered last).","metadata":{"transformedAt":"2026-08-18T18:32:17.783Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":117,"estimatedTokens":2298}}305{"id":"stack-18644104","source":"stackoverflow","questionId":18644104,"title":"For what and from where is Compiler-RT invoked?","tags":["compiler-construction","runtime","clang","llvm","llvm-clang"],"text":"Title: For what and from where is Compiler-RT invoked?\nTags: compiler-construction, runtime, clang, llvm, llvm-clang\nSource: Stack Overflow\n\nQuestion:\nI would like to know the following about LLVM's Compiler-RT project: from what program is it invoked. To my understanding, Compiler-RT is a collection of functions that handle instructions in LLVM that don't really have hardware counterparts (is there more to it than this?). So if I use division in LLVM, it should be replaced by an appropriate Compiler-RT function. First, if this is not correct, please correct me!\n\nSecond, I am curious as to who generates the Compiler-RT usage. Is it Clang or is it LLVM directly. Could I write a different front-end to LLVM and would LLVM automatically handle the use of Compiler-RT when appropriate?","metadata":{"transformedAt":"2026-08-18T18:32:17.783Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":8,"estimatedTokens":198}}306{"id":"stack-7100776","source":"stackoverflow","questionId":7100776,"title":"Garbage collection implementation in compiled languages","tags":["compiler-construction","garbage-collection","language-implementation"],"text":"Title: Garbage collection implementation in compiled languages\nTags: compiler-construction, garbage-collection, language-implementation\nSource: Stack Overflow\n\nQuestion:\nWhen implementing precise garbage collection, there is always the issue of figuring out which words on the stack are pointers and which are other kinds of data such as integers or floating point numbers. Interpreted languages typically solve this problem by making everything a pointer; compilers for some languages such as Lisp typically solve it by using tag bits to distinguish between pointers and integers.\n\nBut what about JIT compilers for languages such as Java and C# that support full unboxed machine word integers and floating-point numbers? How do they tell which of the contents of the stack and CPU registers are pointers?\n\n========================================\n\nTop Answer:\nLanguages like Java and C# are specified in such a way that they do not *require* precise collection. An implementation might use a conservative collector, where patterns of bits that appear to look like a pointer are treated like a pointer (but might really be an integer or float). For example, the Boehm collector is a conservative collector that could be used for JIT-ed languages.\n\n========================================\n\nComments:\n- Just wonder why the close votes? It's a genuine question. As for a possible implementation - type information is available statically, so all the pointers could go into a separate stack, for example.\n- And a relevant link: mono-project.com/Generational_GC#Precise_Stack_Marking\n- Sure, Mono used to use Boehm. But typical implementations (Sun Java, Microsoft .Net, latest version of Mono etc) do use precise garbage collection. I'm interested in how.","metadata":{"transformedAt":"2026-08-18T18:32:17.784Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":20,"estimatedTokens":438}}307{"id":"stack-75144082","source":"stackoverflow","questionId":75144082,"title":"Using tree-sitter as compiler's main parser","tags":["parsing","compiler-construction","syntax-highlighting","treesitter"],"text":"Title: Using tree-sitter as compiler's main parser\nTags: parsing, compiler-construction, syntax-highlighting, treesitter\nSource: Stack Overflow\n\nQuestion:\nCan a parser, generated by *tree-sitter*, be used both for both syntax highlighting and compiler itself? If not - why?\n\nIt would be counterproductive to write 2 different parsers and maintain them.\n\n**Note:** I haven't used *tree-sitter* yet, but consider using it for highlighting syntax of my own programming language. Due-to that, I may misunderstand how it's parser actually works.\n\n========================================\n\nCode:\n```text\nbison\n```\n\n```text\nNode::kind\n```\n\n```text\nut8_text\n```\n\n========================================\n\nComments:\n- Of course, all that is needed is a tool that analyses the grammar.json and generates a typed language-specific shim file from it. Perhaps someone wants to have a go at that :)","metadata":{"transformedAt":"2026-08-18T18:32:17.784Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":30,"estimatedTokens":221}}308{"id":"stack-2863096","source":"stackoverflow","questionId":2863096,"title":"Which programming languages have JIT compilers?","tags":["compiler-construction","programming-languages","jit"],"text":"Title: Which programming languages have JIT compilers?\nTags: compiler-construction, programming-languages, jit\nSource: Stack Overflow\n\nQuestion:\nI know C# and Java do. Anyone else know of any others?\n\n========================================\n\nTop Answer:\nLua has the impressive LuaJIT.\n\nPLT Scheme has had a JIT for some time now.\n\nI believe both of these are limited to x86.\n\n========================================\n\nCode:\n```text\nJust-In-Time Compilation\n```\n\n========================================\n\nComments:\n- Specifically, Cling: root.cern/cling (LLVM-based) and the much older CINT: hanno.jp/gotom/Cint.html\n- Right. I changed the title of this question to reflect that fact, thanks!\n- +1 for mentioning Dalvik. That's what originally motivated me to ask this question.\n- LuaJIT for x86-64 is in beta and will be available soon.","metadata":{"transformedAt":"2026-08-18T18:32:17.784Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":30,"estimatedTokens":210}}309{"id":"stack-49032551","source":"stackoverflow","questionId":49032551,"title":"Why is the phrase: \"undefined behavior means the compiler can do anything it wants\" true?","tags":["c++","compiler-construction","undefined-behavior"],"text":"Title: Why is the phrase: \"undefined behavior means the compiler can do anything it wants\" true?\nTags: c++, compiler-construction, undefined-behavior\nSource: Stack Overflow\n\nQuestion:\nSpecifically in reference to Raymond Chen's Blogpost The Old New Thing: Undefined behavior can result in time travel (among other things, but time travel is the funkiest).\n\nI'm a new C++ programmer and I'm currently learning about undefined behavior and its effects on a program. I was linked specifically to the above blog, which says that when undefined behavior occurs, anything can happen.\n\nIt mentions several times specifically that the compiler can allow anything to happen when undefined behavior occurs.\n\nWhat specifically causes this to occur, and why does it happen?\n\n========================================\n\nTop Answer:\nFrom the point of view of the C and C++ Standards, the fact that some situation invokes \"Undefined Behavior\" means nothing more nor less than that the Standard imposes no requirements on what an implementation must do in that situation in order to be *conforming*. It does not imply any particular judgment as to whether implementations intended for purposes on any particular platforms should be expected to behave predictably, nor whether predictable behavior in such a situation might be required to make an implementation suitable for a such purposes on such platforms.\n\nFor some reason, some compiler writers have equated \"the Standard does not require X\" with \"there is no need for implementations to do X\", without any particular regard for the purposes to which their compilers would be put, and without regard for what behaviors might be necessary to fulfill those purposes. What caused Undefined Behavior to digress to the \"anything can happen\" was that compiler writers interpreted it not as an acknowledgment that there may be *some* combinations of platform and application field where the cost of ensuring predictable behavior would exceed the benefit, and compiler writers should exercise judgment as to when that is the case, but instead as an indication that the authors of the Standard had already exercised judgment that on *all* combinations of platform and application field, the cost of ensuring predictable behavior would outweigh the benefits, and there's no need for compiler writers to exercise judgment because the authors of the Standard have already done so.\n\n========================================\n\nCode:\n```text\nnullptr\n```\n\n```text\nnullptr\n```\n\n```text\nnullptr\n```\n\n========================================\n\nComments:\n- Because undefined behavior is undefined.\n- That phrase is a little over dramatic. Chandler Carruth has a really good talk on this:youtube.com/watch?v=yG1OZ69H_-o\n- An Operating System typically has its own opinions about this. It will generally enforce its own rules, but those are typically more lax than that of C++. One major exception to that rule would be Linux, which kills processes where C++ mandates a `std::bad_alloc` exception.\n- @MSalters: Interesting. Could you give an example of a case where C++ mandates `std::bad_alloc` but Linux kills the process instead?\n- @LightnessRacesinOrbit I believe MSalters is referring to optimistic memory allocation which is used as the default by many Linux distributions. Allocations always succeed, but accessing that memory for the first time might fail. It's very difficult to reconcile that behavior with the requirements of c++.\n- Undefined behavior *can* be serious, depending on the circumstances. Suppose that later you get a job at the Pentagon and instead of `if (door_is_open)` you have to code `if (incoming_missiles_detected)`. Then just about anything *can* happen.\n- Keep in mind that C is a horribly-designed language and C++ inherited most of C's flaws. The amount of \"undefined behavior\" in C++ is much greater than what one would find in a well-designed language.\n- @user3344003: At the time C was standardized, most implementations would specify behaviors in most cases where the Standard did not. The problem is that the Standard made no distinction between \"Implementations should do X when practical, but implementations on weird hardware where that is impractical may do something else\" and \"Programmers have no basis for any particular expectations, even if they're only targeting non-weird hardware\".\n- @BoPersson my favorite example is programming a CPAP controller. If you get undefined behavior there, you can literally get demons flying from your nose.\n- Both clang and gcc, when configured for C++ mode, interpret the fact that the C++ Standard characterizes endless loops as UB as an invitation to throw ordinary laws of causality out the window. If they determine that a loop has no side effects, but won't terminate unless `x` is less than 256, they'll replace `if (x < 256) arr[x]=1;` with an unconditional `arr[x]=1;`, compete with all the hilarity that may ensue if `x` is exceeds both 256 and the size of `arr`. I'd call that much closer to \"anything can happen\" than most people would expect.\n- @supercat yes, that's what this answer says. Constraint propagation is a powerful tool for an optimiser.\n- @Caleth: The sentence \"Of course it is not actually true that \"anything can happen\" doesn't suggest behaviors not bound by causality. Further, there may be some kinds of programs for which constraint propagation may be useful, but there are many others for which its \"power\" doesn't really translate into \"usefulness\" and may be counter-productive. If a piece of code will sometimes receive valid inputs and sometimes invalid inputs, and any output it could produce *without adverse side effects* in response to invalid inputs would be equally acceptable, permission to produce output...\n- ...in response to invalid inputs that would differ from that produced by a straightforward machine-language translation of the code, but still be free of extraneous side effets, may allow more useful optimizations than would be possible if code had to be written in a way that denied the compiler such freedom.\n- It's not some mysterious reason. It's very well known that assuming UB doesn't occur makes optimising easier, because in the case where UB does occur, whatever happened is still conforming. There is commercial pressure on C++ compiler writers to provide as many optimisations as they can.\n- @Caleth: If one limits the accuracy with which a program can specify an application's requirements, that may make it easier to find the optimal code for a particular program, but the optimal solution to that problem may be inferior to a near-optimal solution to a program that more accurately specifies the application's *actual requirements*. Compiler writers' treatment of UB is analogous to \"solving\" the Traveling Salesman problem by forbidding any edge from having a weight less than the sum of the minimum spanning tree edges connecting its endpoints.","metadata":{"transformedAt":"2026-08-18T18:32:17.784Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":53,"estimatedTokens":1728}}310{"id":"stack-17602546","source":"stackoverflow","questionId":17602546,"title":"How does a compiler compile a compiler?","tags":["compiler-construction","low-level","machine-code"],"text":"Title: How does a compiler compile a compiler?\nTags: compiler-construction, low-level, machine-code\nSource: Stack Overflow\n\nQuestion:\nComing from a high-level programming background, I am interested in learning about low-level programming. I want to know how a compiler is compiled?\n\nAfter looking at some articles in wiki, Numerical machine code is said to be lowest level language, but there has to be a compiler to compile this machine code. What language is that compiler written in?\n\n========================================\n\nTop Answer:\nYou can always use your favourite compiler A to write another compiler, say B. In this B you added some extra functionality, so it can easily become your favourite one, and you will use it for writing compiler C, ...\n\nHow to start then?\nIn the old days people simply filled the memory with the raw numbers to interpret by the CPU directly. This is why source is often referred to as *code*.\nOnce a minimal compiler has been programmed this way, it can be executed to create another one written in the language it compiles.\nThat again can be used to create a higher level one and so forth.\n\nIn fact filling raw instruction codes to memory can itself be treated as a zero level compilation process, where the human is the compiler.\n\nIt is quite usual that a compiler for a given language is written in the same language. This is the case with the C programming language for example. This is somewhat more than coincidal, because who knows a language good enough to dare writing a compiler for it, likely has this language among his favourite ones to use for programming.\nIt is simply a typical case though, not necessary as there are many languages to choose from, including ones especially good for compiler construction.\n\n========================================\n\nCode:\n```text\nFor example, on the Zilog Z80 processor, the machine code 00000101, which causes the CPU to decrement the B processor register, would be represented in assembly language as DEC B.\n```\n\n========================================\n\nComments:\n- Machine-code does not require compilation. It's directly executed by the CPU. You can type it in with a hex-editor.\n- `Numerical machine code (i.e. not assembly code) may be regarded as the lowest-level representation of a compiled and&#47;or assembled computer program`, from the article ,2nd paragraph\n- Yes, the *lowest-level* - there is no lower level. It is what other languages are compiled *to*!\n- Your quote is saying that numerical machine code is the *result* of compilation of a program. There's no compiler that takes machine-code as input.\n- Yes, of course there is a compiler which compiles a machine code: instruction decoder. In a modern CPU (especially of the x86 lineage) decoder can be very complicated, it breaks instructions into sequences of micro-instructions, which are then analysed (to obtain dependencies and potential hazards) and issued into different execution units.\n- **bootstrapping**","metadata":{"transformedAt":"2026-08-18T18:32:17.784Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":40,"estimatedTokens":745}}311{"id":"stack-3848343","source":"stackoverflow","questionId":3848343,"title":"Decode and dispatch interpretation vs Threaded interpretation","tags":["compiler-construction","operating-system","interpretation"],"text":"Title: Decode and dispatch interpretation vs Threaded interpretation\nTags: compiler-construction, operating-system, interpretation\nSource: Stack Overflow\n\nQuestion:\nI am trying to understand the practical difference during the execution of a program in **Decode and dispatch interpretation and Threaded interpretation**.\n\nExample of both will really help. \n\nI understand how Java bytecode works and how an assembly language works. But where does DDI and TI fit in?\n\nContext: Virtual machines: versatile platforms for systems and processes\n\n========================================\n\nCode:\n```text\ntypedef enum {\n  add, /* ... */\n} instruction_t;\n\nvoid interpret() {\n  static instruction_t program[] = { add /* ... */ };\n  instruction_t* pc = program;\n  int* sp = ...; /* stack pointer */\n  for (;;) {\n    switch (*pc++) {\n      case add:\n        sp[1] += sp[0];\n        sp++;\n        break;\n        /* ... other instructions */\n    }\n  }\n}\n```\n\n```text\nvoid interpret() {\n  void* program[] = { &&l_add, /* ... */ };\n  int* sp = ...;\n  void** pc = program;\n  goto **pc; /* jump to first instruction */\n l_add:\n  sp[1] += sp[0];\n  ++sp;\n  goto **(++pc); /* jump to next instruction */\n  /* ... other instructions */\n}\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.784Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":50,"estimatedTokens":305}}312{"id":"stack-262089","source":"stackoverflow","questionId":262089,"title":"Back-end choice for a new dynamic programming language?","tags":["compiler-construction","interpreter","smalltalk","vm-implementation"],"text":"Title: Back-end choice for a new dynamic programming language?\nTags: compiler-construction, interpreter, smalltalk, vm-implementation\nSource: Stack Overflow\n\nQuestion:\nI've been developing a Smalltalk variant for just the fun of it and I wonder what would be a fellow stackoverflowers choice when it comes to targeting a back-end. These were my current considerations:\n\n.NET, JVM: These two VM's are mainly for statically typed languages and I assume it would be quite hard to target such a dynamic language like smalltalk.\n\nPython (as source code): Seems to be the simplest way. Also it would be better if I could emit Python bytecode but it's not well documented as other VM's AFAIK (Need to dig Python's source code for details!). \n\nSelf made interpreter: Out of the question as it's no fun :-)\n\nLLVM, NekoVM, Parrot are other options I'm checking out. What would be your take on this?\n\n========================================\n\nTop Answer:\nI would choose the JVM, but mainly because I'm familiar with it.\n\nObjective reasons for JVM are: main platforms are supported, many libraries and good performance (within the choices you have given it may have the best performance).\n\n.Net works best on Windows. If you choose it, you should test on Mono to be more platform-neutral.\n\nPython seems a good choice as well. But I think for the JVM more libraries are available.\n\nParrot is in my opinion too fresh, it needs some time to mature. But an interesting alternative for the future.\n\nThe other choices are new to me, I will take a look at them.\n\n========================================\n\nComments:\n- Shameless self-plug: And IronScheme :)\n- And .Net 4.0 does have a dynamic type build in. Real dynamic.\n- @Warren: Boo isn't dynamic. From the home page: \"Boo is a new object oriented statically typed programming language\"\n- Why do you believe utku is \"going to need [community and support] soon\"? Don't forget the question stated that the project was \"for just the fun of it.\" Also, there's plenty of .NET community support... have a look at the most popular tags on SO, for example.\n- This would complement MagLev nicely - Ruby running on Gemstone Smalltalk.","metadata":{"transformedAt":"2026-08-18T18:32:17.784Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":38,"estimatedTokens":540}}313{"id":"stack-44870928","source":"stackoverflow","questionId":44870928,"title":"Is there a BNF grammar openly available for JavaScript ES6?","tags":["javascript","ecmascript-6","compiler-construction","grammar","bnf"],"text":"Title: Is there a BNF grammar openly available for JavaScript ES6?\nTags: javascript, ecmascript-6, compiler-construction, grammar, bnf\nSource: Stack Overflow\n\nQuestion:\nI am working on a merge tool for JavaScript programs, and I need to write a grammar for JavaScript (version >= ES6) in JavaCC format.\n\nFor that, I want to use an openly available BNF grammar for ES6, then I would write the grammar in JavaCC format from it.\n\nI was able to find only those (from this Stack Overflow question):\n\n- http://tomcopeland.blogs.com/EcmaScript.html\n\n- http://www.ccs.neu.edu/home/dherman/javascript/\n\nHowever, these are very old BNFs (and the question on StackOverflow is also very old, from 11 years ago). These grammars work only - and in a limited manner - for versions Do you know about a newer publicly available grammar (BNF, JavaCC file, etc)?\n\nThere are parsers available for ES6, such as Esprima, but, since I have to use JavaCC environment, I need the grammar to work on.\n\n========================================\n\nTop Answer:\n### ANTLR\n\nTake a look at the JavaScript grammar from the ANTLR grammars repository.\n\n========================================\n\nComments:\n- I'm no language expert, but I don't think ES6 can be expressed purely in terms of a BNF grammar.\n- It may be an EBNF\n- You may have better luck asking on esdiscuss.\n- Thanks for the advice, @torazaburo!\n- Nice reference! That section (ecma-international.org/ecma-262/8.0/&hellip;), summarising the grammar, is basically an EBNF, which might suit well to my needs! I will work on it. Thanks for the answer!\n- This comment should be the accepted answer!\n- Is there a grammar file available that isn't HTML? Something that can be passed to tooling?","metadata":{"transformedAt":"2026-08-18T18:32:17.784Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":36,"estimatedTokens":429}}314{"id":"stack-49161787","source":"stackoverflow","questionId":49161787,"title":"Which parsers do the modern compilers use?","tags":["parsing","compiler-construction","abstract-syntax-tree"],"text":"Title: Which parsers do the modern compilers use?\nTags: parsing, compiler-construction, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI wasn't able to find any information on the parsers of some of the modern compilers, especially:\n\n- TypeScript\n\n- C#\n\n- Rust\n\nAre they using LL(k), LR(k), a mixture or something different? Is there any site that lists information like this about compilers/parsers for multiple languages? As these compilers are generally considered \"modern\", I'm very interested in their parsing techniques.\n\n========================================\n\nComments:\n- I found these information: youtube.com/watch?v=wSdV1M7n4gQ news.ycombinator.com/item?id=13915150\n- @nikeee: the video with Anders Hjelsberg is interesting, despite the limitations of the interview format. (The ycombinator comment thread is mostly a random collection of emotional opinions, although there are some good comments there, too. Of course, that evaluation could be my emotional opinion :) ). I think his comments about the compiler as an API are really important, and it's basically the design model for Clang, which has been in development for a long time, although it is less IDE-oriented than Roslyn. Continuing the discussion would quickly get out of scope for SO, so I'll only add...\n- that in many ways this is a reflection of the classic confrontation between two styles of programming: \"just get it working and deliver what the user wants\" vs. \"write code that you know will work\". Both extremes are unreasonable, and we all need to learn techniques \"from both sides\". Languages whose parsers do not reveal what the grammar of the language really is are a problem, but then so are IDEs which cannot deliver good error messages fast. In that sense, I am encouraged by Anders' approach, which tries to marry functional programming with pragmatic design.","metadata":{"transformedAt":"2026-08-18T18:32:17.784Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":465}}315{"id":"stack-2693138","source":"stackoverflow","questionId":2693138,"title":"variable scope in statement blocks","tags":["c#","compiler-construction","clr","scope"],"text":"Title: variable scope in statement blocks\nTags: c#, compiler-construction, clr, scope\nSource: Stack Overflow\n\nQuestion:\n```\nfor (int i = 0; i By my understanding of scope, the first example should be fine. The fact neither of them are allowed seems even more odd. Surely 'i' is either in scope or not. \n\nIs there something non-obvious about scope I don't understand which means the compiler genuinely can't resolve this? Or is just a case of nanny-state compilerism?\n\n========================================\n\nTop Answer:\nIt is because the declaration space defines `i` at the method level. The variable `i` is out of scope at the end of the loop, but you still can't redeclare `i`, because `i` was already defined in that method.\n\nScope vs Declaration Space:\n\nhttp://csharpfeeds.com/post/11730/Whats_The_Difference_Part_Two_Scope_vs_Declaration_Space_vs_Lifetime.aspx\n\nYou'll want to take a look at Eric Lippert's answer (who by default is always right concerning questions like these).\n\nhttp://blogs.msdn.com/ericlippert/archive/2009/08/03/what-s-the-difference-part-two-scope-vs-declaration-space-vs-lifetime.aspx\n\nHere is a comment from eric on the above mentioned post that I think talks about why they did what they did:\n\n Look at it this way. It should always\n be legal to move the declaration of a\n variable UP in the source code so long\n as you keep it in the same block,\n right? If we did it the way you\n suggest, then that would sometimes be\n legal and sometimes be illegal! But\n the thing we really want to avoid is\n what happens in C++ -- in C++,\n sometimes moving a variable\n declaration up actually changes the\n bindings of other simple names!\n\n========================================\n\nCode:\n```text\nfor (int i = 0; i < 10; i++)\n{\n    Foo();\n}\nint i = 10; // error, 'i' already exists\n\n----------------------------------------    \n\nfor (int i = 0; i < 10; i++)\n{\n    Foo();\n}\ni = 10; // error, 'i' doesn't exist\n```\n\n```text\nint i = 10;\nint i = 10;\n```\n\n```text\nclass C \n{\n    int i;\n    void M()\n    {\n        string i;\n```\n\n```text\nclass C \n{\n    int i;\n    void M()\n    {\n        int x = i;\n        foreach(char i in ...\n```\n\n```text\nfor (int i = 0; i < 10; i++)\n{\n  // do something\n}\n\nforeach (Foo foo in foos)\n{\n   int i = 42;\n   // do something \n}\n```\n\n```text\ni\n```\n\n```text\ni\n```\n\n```text\ni\n```\n\n```text\ni\n```\n\n```text\nvar i = 0;\n\nfor(i = 0, i < 10, i++){\n}\n\ni = 10;\n```\n\n```text\ni\n```\n\n```text\nfor\n```\n\n```text\nfor\n```\n\n```text\nint i ;\n            for ( i = 0; i < 10; i++)\n            {\n\n            }\n            i = 10;\n```\n\n```text\nfor (int i = 0; i < 10; i++)\n{\n\n}\n\nfor (int i = 0; i < 10; i++)\n{\n\n}\n```\n\n```text\nfor (int i = 0; i < 10; i++)\n{\n\n}\n\nfor (int j = 0; j < 10; j++)\n{\n    var i = 12;                \n}\n```\n\n```text\nfor (int i = 0; i < 10; i++)\n{\n    var x = 2;\n}\n\nvar x = 5;\n```\n\n```text\nfor (int i = 0; i < 10; i++)\n{\n    var k = 12;\n}\n\nfor (int i = 0; i < 10; i++)\n{\n    var k = 13;\n}\n```\n\n```text\n/*\n * doing two different things with the same name is unclear\n */\nfor (var index = 0; index < people.Count; index++)\n{\n    people[index].Email = null;\n}\nvar index = GetIndexForSomethingElse();\n```\n\n```text\n/*\n * Now there is only one meaning for index in this scope\n */\nClearEmailAddressesFor(people); // the method name works like a comment now\nvar index = GetIndexForSomethingElse();\n\n/*\n * Now index has a single meaning in the scope of this method.\n */\nprivate void ClearEmailAddressesFor(IList<Person> people)\n{\n    for (var index = 0; index < people.Count; index++)\n    {\n        people[index].Email = null;\n    }\n}\n```\n\n```text\n{\n    // scope starts here\n    for (int i = 0; i < 10; i++)\n    {\n        Foo();\n    }\n    int i = 10;\n}\n```\n\n```text\n{\n    int i = 10;\n\n    // collision with i\n    for (int i = 0; i < 10; i++)\n    {\n        Foo();\n    }\n}\n```\n\n```text\ni\n```\n\n```text\ni\n```\n\n```text\nfor\n```\n\n```text\ni\n```\n\n```text\nclass Test\n{\n    int i;\n    static int si=9; \n\n    public Test()\n    {\n        i = 199;\n    }\n\n    static void main()\n    {\n        for (int i = 0; i < 10; i++)\n        {\n            var x = 2;\n        }\n\n        { var x = 3; }\n\n        {    // remove outer \"{ }\" will generate compile error\n            int si = 3; int i = 0;\n\n             Console.WriteLine(si);\n             Console.WriteLine(Test.si);\n             Console.WriteLine(i);\n             Console.WriteLine((new Test()).i);\n        }\n    }\n}\n```\n\n========================================\n\nComments:\n- The problem is not in reusing variable names; it is perfectly legal to reuse a variable name several times in one method. It is not legal to use the same *simple name* to refer to *two different things* within a particular local variable declaration space.\n- But then why can you define `i` in scope of another loop in the same method? I still think it's just bureaucracy on the part of the compiler.\n- @Michael: You can do that because the loops are sibling scopes. There is no collision defining identically named variables in sibling scopes because they are out of scope of each other.\n- @Zach: However the first block in the original question shows exactly that: There is no collision. But it still won't compile.\n- wow, I had no idea this existed. I'm going to make sure to add this to my list of reasons I hate C#\n- No reason to hate it for that. It's behaving exactly as it should be: preventing you from shooting yourself in the foot for reasons that are not even worth talking about (reusing variable names).\n- @Foxfire: The first example in the question is a parent-child scope relationship, causing a collision. Sibling scopes are scopes which are both children of the parent scope.\n- Really? I like it that way. I can't think of a reason to re declare a variable with the same name in the same method, and it prevents me (or someone else) from moving a variable around and causing an unexpected behavior in the future.\n- Thanks for the shout-out. Couple things. First, this is a better article: blogs.msdn.com/ericlippert/archive/2009/11/02/&hellip; and second, better to link to the original rather than the copy; that way you get my updates and get to read the comments of others: blogs.msdn.com/ericlippert/archive/2009/08/03/&hellip;\n- @Zach: Well this answer (and the linked documents) say otherwise: They are in different declaration spaces. However in terms of scope both could happily exist because they are both children (non-overlapping) of the method. The i-loop is out of scope at the end of the loop!\n- Obviously my comment above should read (just to correct that): They are in the same declaration space.\n- @Foxfire: I think the question (at least in its current form) is about the scope of local variables. The `i` in the `for` loop is in a child scope and the `i` written after the loop is in the parent scope of the loop, which is causing the collision. See my answer: stackoverflow.com/questions/2693138/&hellip;\n- @meatador You find it a reason to hate a language because it enforces rules that prevent some really nasty, hard-to-spot bugs with virtually no inconvenience to the programmer?\n- It is not inconsistent. The rule is that the same simple name may not be used to denote two different things *throughout the outermost local variable declaration space in which the simple name is directly used*. A for loop defines a local variable declaration space. I think you'll find that this rule is consistently applied in each of your examples.\n- It may be consistent when you parse the language of the specification, but from a consumer's perspective it is inconsistent. If a variable is out of scope and can't be used, then there's no good reason that I shouldn't be able to create another variable of the same name.\n- There is a *very good reason*. The very good reason is because using the same simple name to refer to two different entities in overlapping declaration spaces is *confusing* and *bug prone* and therefore should be *illegal*.\n- @Eric I think your opinion on this is flavored by the fact that you're intimately familiar with what's under the hood. From the perspective of a simple consumer, if I have no ability to access or modify a variable, then it is effectively out of scope. In that case, regardless of how scope is handled *technically*, it should be treated by the language as out of scope. If I can't create a variable outside a loop that was previously declared inside that loop (see my third example), then it *is* confusing, because the variable in the loop is out of context, if not technically out of scope.\n- It *is* treated as out of scope. As I've said several times, this isn't an issue of scope at all. This is an issue of *using the same sequence of letters to mean two different things in overlapping regions of code*. Stop thinking about scope; this isn't a scoping issue in the first place.\n- Ok, so I made a semantic misstep, and I apologize. As a developer, I would prefer to think of a for loop as a completely autonomous block of code. The problem can be solved by composing the loop into another method (the latter of which is preferred anyway since it usually promotes readability). Thanks for the informative comments.","metadata":{"transformedAt":"2026-08-18T18:32:17.784Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":27,"totalLines":308,"estimatedTokens":2285}}316{"id":"stack-6606240","source":"stackoverflow","questionId":6606240,"title":"Translating imperative to functional code","tags":["functional-programming","code-translation","compiler-construction"],"text":"Title: Translating imperative to functional code\nTags: functional-programming, code-translation, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI need to write a program that will translate imperative code to pure functional style. I'm not worried about I/O - I have some solutions in mind for that - but I do need to deal with heap objects as well as local variables.\n\nI suppose it could be done by passing a `TheWorld` object with every function call and return, and then optimizing from there, trying to remove that parameter from functions where it's not used etc. But is there a known better way of doing it?\n\n========================================\n\nTop Answer:\nThere is a number of ways to do such a translation efficiently. First, it worth doing an SSA transform with a consequent CPS transform: this way you'd get a bunch of trivial mutually recursive functions out of an imperative code with variables and branches. Function calls (and even virtual calls) can also be CPS-ed easily, by passing a continuation parameter instead of relying on an implicit stack semantics.\n\nArrays can be handled the same way as variables, prior to an SSA transform all the array access should be replaced with `get` and `update` function calls, which should have an implicit copy semantics (but beware of an aliasing in this case). Same for structures.\n\nAnd only for those cases where it is impossible to maintain a copying semantics you need to have this `TheWorld` object which should keep all the allocated objects and should be copyied entierly each time you're modifying one of them.\n\n========================================\n\nCode:\n```text\nTheWorld\n```\n\n```text\nget\n```\n\n```text\nupdate\n```\n\n```text\nTheWorld\n```\n\n```text\nint example(int a,int b){\n    a += 1;\n    b += 2;\n    if(a == 1){\n        b += 1;\n    }\n    else if(b == 1){\n        a += 1;\n    }\n    return a + b;\n}\n```\n\n```text\nlet example a b =\n    let vars = {a,b} in\n    let vars = vars with a = vars.a + 1 in\n    let vars = vars with b = vars.b + 2 in\n    let vars = (if vars.a == 1 then\n        let vars = vars with b = vars.b + 1 in\n        vars\n    else if b == 1 then\n        let vars = vars with a = vars.a + 1 in\n        vars\n    else\n        vars)\n    in vars.a + vars.b\n```\n\n```text\n:- use_module(prolog_vars_list).\n:- set_prolog_flag(double_quotes, chars).\n:- initialization(main).\n\nmain :- \n    To_solve = (Z=11,\n    Z=Z*2,\n    A=1+A,\n    A=A+2,\n    A = Z+1,\n    A = A * 2,\n    A=A+3+Z+P),\n\n    run_imperative(To_solve,B),\n    \n    %print the input\n    writeln(To_solve),\n    \n    %now print the output\n    writeln(B).\n\nrun_imperative(A,B) :- imperative_to_declarative(A,_=B).\n\nimperative_to_declarative((A,B,C),D1) :- \nimperative_to_declarative((B,C),D),imperative_to_declarative((A,D),D1).\n\nimperative_to_declarative((A=A1,B=B1),(_=C)) :-\n    replace(A,A1,B1,C).\n\nreplace(Subterm0, Subterm, Term0, Term) :-\n        (   Term0 == Subterm0 -> Term = Subterm\n        ;   var(Term0) -> Term = Term0\n        ;   Term0 =.. [F|Args0],\n            maplist(replace(Subterm0,Subterm), Args0, Args),\n            Term =.. [F|Args]\n        ).\n```\n\n```text\nlet\n```\n\n========================================\n\nComments:\n- Out of interest, why do you need to do this?\n- @Marcin, it is a common approach to static analysis, for example. Btw., was it your downvote?\n- What I'm trying to do is automatic code analysis, optimization and parallelization, for which I need to solve the problem 'what function does this chunk of code compute', which it seems to me is equivalent to the problem of translating it into pure functional form.\n- @rwallace: Seems like a reasonable approach. Is this going to be public in any way (because I'd be curious to see what you come up with)?\n- Yes, it's going to be published as open source once I've got something working.\n- have you got something working? :)\n- @ErikAllik Unfortunately not yet; it turns out there are a number of other problems that need to be solved before that can be done in a useful way.\n- I see! like what problems?\n- @ErikAllik Too big a discussion to fit in stackoverflow comments :-) but one issue is that of bootstrapping - it does no good to write a superoptimizer in a style that implicitly assumes it will be compiled with a superoptimizer.\n- oh, right, if superoptimization is your goal, then yes — my goal is simply mapping e.g. a restricted subset of Python code to a Haskell DSL, so that people can effectively write with Haskell semantics but using Python's syntax and some of its more basic idioms.\n- Upvoted, seems like a reasonable approach, though one thing I don't quite get, is there a reason for wanting to CPS everything? Functional programming normally relies on an implicit stack semantics after all, doesn't it? I haven't personally worked with CPS before, but the references I've seen to it, typically suggest it's used for the reverse, compiling functional code to an imperative target platform?\n- @rwallace, it is much easier to reason about an explicit CPS rather than an implicit stack. And SSA is already equivalent to CPS, you only have to infer the values liveness ranges to transform from one to another.\n- CPS is definitely easier to reason about than an implicit stack with all the machinery of an imperative programming language, but is CPS really easier to reason about than pure lambda terms? If so, why?\n- @rwallace, it is mainly a practical observation. Having an explicit continuation at any point allows to easily predict, what values from the current context are captured and going to be used further. And another reason is that you'd need another step to do a reverse transformation if you've already done an SSA memory to register promotion.","metadata":{"transformedAt":"2026-08-18T18:32:17.784Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":127,"estimatedTokens":1426}}317{"id":"stack-27763544","source":"stackoverflow","questionId":27763544,"title":"direct-coded vs table-driven lexer?","tags":["compiler-construction","lexer","table-driven"],"text":"Title: direct-coded vs table-driven lexer?\nTags: compiler-construction, lexer, table-driven\nSource: Stack Overflow\n\nQuestion:\nI'm new in compiler construction world , I want to know what are the differences between direct-coded vs table-driven lexer analyzer ? \n\nPlease use simple source code example if it's possible.\n\nThanks.\n\n**Edit :**\n\nin Engineering a Compiler book, the author divided lexers into three(3) types: table-driven,direct coded, and hand coded.\n\n========================================\n\nTop Answer:\nLook at my lexer generator, it is very simple and easy to uderstand,\nit generates DFA direct code automata, as nested switch instructions. I used such approach for my projects, firstly was hand written and later generated by using this tool.\nThe approach is based on my experience by studying this topic by reading several books and studying the implementations of the more advanced parser generators. There is a project on github - rmjocz/langgen\n\n========================================\n\nCode:\n```text\n#define REJECT -1\n\n/* This table encodes the transitions for a given state and character. */\nconst int transitions[][] = {\n    /* In state 0, if we see an a then go to state 1 (the 1).\n     * Otherwise, reject input.\n     */\n    { /*a*/  1,  /*b*/  REJECT },\n    { /*a*/  2,  /*b*/  3      },\n    { /*a*/ -1,  /*b*/ -1      }, /* Could put anything here. */\n    { /*a*/  2,  /*b*/  3      }\n};\n\n/* This table determines, for each state, whether it is an accepting state. */\nconst int accept[] = { 0, 0, 1, 0 };\n```\n\n```text\nint scan(void) {\n    char ch;\n    int state = 0;\n\n    while (!accept[state]) {\n        ch = getchar() - 'a'; /* Adjust so that a => 0, b => 1. */\n        if (transitions[state][ch] == REJECT) {\n            fprintf(stderr, \"invalid token!\\n\");\n            return 0; /* Fail. */\n        } else {\n            state = transitions[state][ch];\n        }\n    }\n    return 1; /* Success! */\n}\n```\n\n```text\nenum token {\n    ERROR,\n    LPAREN,\n    RPAREN,\n    IDENT,\n    NUMBER\n};\n\nenum token scan(void) {\n    /* Consume all leading whitespace. */\n    char ch = first_nonblank();\n    if (ch == '(') return LPAREN;\n    else if (ch == ')') return RPAREN;\n    else if (isalpha(ch)) return ident();\n    else if (isdigit(ch)) return number();\n    else {\n        printf(\"invalid token!\\n\");\n        return ERROR;\n    }\n}\n\nchar first_nonblank(void) {\n    char ch;\n    do {\n        ch = getchar();\n    } while (isspace(ch));\n    return ch;\n}\n\nenum token ident(void) {\n    char ch;\n    do {\n        ch = getchar();\n    } while (isalpha(ch));\n    ungetc(ch, stdin); /* Put back the first non-alphabetic character. */\n    return IDENT;\n}\n\nenum token number(void) {\n    char ch;\n    do {\n        ch = getchar();\n    } while (isdigit(ch));\n    ungetc(ch, stdin); /* Put back the first non-digit. */\n    return NUMBER;\n}\n```\n\n```text\nint scan(void) {\n    char ch;\n\nstate0:\n    ch = getchar();\n    if (ch == 'a') goto state1;\n    else { error(); return 0; }\n\nstate1:\n    ch = getchar();\n    if (ch == 'a') goto state2;\n    else if (ch == 'b') goto state3;\n    else { error(); return 0; }\n\nstate2:\n    return 1; /* Accept! */\n\nstate3:\n    ch = getchar();\n    if (ch == 'a') goto state2;\n    else if (ch == 'b') goto state3; /* Loop. */\n    else { error(); return 0; }\n}\n```\n\n```text\nab*a\n```\n\n```text\nIDENT\n```\n\n```text\nNUMBER\n```\n\n========================================\n\nComments:\n- Table-driven is the one which is based on lookup-table as mentioned in the below answer. Direct-coded is the approach which simply does the coding for a DFA automaton. It's the common one. And, hand-coded are those which have fixed transitions and is valid for a small-language.!\n- Fortunately I happen to have the Torczon book. Let me take a look.\n- Alright, I've edited my post with an example of a direct-coded (as in the Torczon book) lexer as well.\n- @Noobs-Most welcome buddy! And,cheers.\n- @shekharsuman lex/flex use table-driven,while quex use direct-coded en.wikipedia.org/wiki/Lexical_analysis#Lexer_generator\n- @shekharsuman I don't know,I just read that in Wikipedia, see this link, it says \"..The lex/flex family of generators uses a table-driven approach which is much less efficient than the directly coded approach...\" . anyway it doesn't matter, I will not use them :-)\n- @Noobs- Yeah,they seem to use table-driven approach. THANKS.\n- Pretty happy that people are there to spend this much of time writing such worthy contents. Keep going. +1...\n- SORRY TO DISTURB,but, I guess Lex,etc. use direct-coded approach! They aren't table driven! How come you derive that,,I didn't get any source! They are fast since they are direct-driven and based on states/transitions. Hand coded are the one we code for a very small language as hand-coding a compiler of modern languages like Java,etc. would be a burden. Please convince me,I ain't convinced!\n- I haven't made any claims about how lex or any other lexer generator is actually implemented. Note that these days, lex really refers to a family of somewhat compatible tools, the most prominent of which is probably Flex (flex.sourceforge.net). As far as hand-written lexers are concerned, their use extends far beyond simple languages. For instance, the V8 JavaScript engine uses a hand-written lexer augmented with a table for single-character tokens; see github.com/v8/v8-git-mirror/blob/master/src/scanner.cc.\n- As a concrete example of lexer generators that output table-driven lexers, I present ML-Lex, a lexer generator for the SML/NJ and MLton implementations of Standard ML. :-)\n- Note that both direct-style lexers compile to the same code if your C compiler supports tail call elimination.","metadata":{"transformedAt":"2026-08-18T18:32:17.784Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":163,"estimatedTokens":1417}}318{"id":"stack-3107299","source":"stackoverflow","questionId":3107299,"title":"How do clojure programs/compilers/interpreters actually \"work\"?","tags":["compiler-construction","clojure","jvm","interpreter"],"text":"Title: How do clojure programs/compilers/interpreters actually \"work\"?\nTags: compiler-construction, clojure, jvm, interpreter\nSource: Stack Overflow\n\nQuestion:\nIt seems that to install Clojure in every new IDE, I have to completely re-install it and create a copy of it. And running the REPL seems like it's running a Java program.\n\nI'm coming from a Ruby background, where Ruby programs are run by `ruby program.rb`, and the ruby being a program executed from one place, onto the file (I am aware that this is similar to how it works for java, python, etc. as well).\n\nIs the clojure \"interpreter\" merely a compiled Java file which takes the .clj file as an argument, and does stuff to it?\n\n========================================\n\nCode:\n```text\nruby program.rb\n```\n\n```text\njar\n```\n\n```text\njar\n```\n\n```text\nclojure.main\n```\n\n```text\nmain\n```\n\n```text\nclj\n```\n\n```text\nclojure.main\n```\n\n```text\nclojure.main\n```\n\n```text\nmain\n```\n\n```text\nruby\n```\n\n```text\nmain\n```\n\n```text\njava\n```\n\n```text\njava.exe\n```\n\n========================================\n\nComments:\n- When you load a .clj file into `clojure.main`, what happens to the actual file? You mention it being \"compiled\" into JVM bytecode...is this to a point where that JVM code is outputted as an extra file, and that file run by the JVM itself?\n- Normally no actual `.class` files are produced, though you can ask for them if you want (see `(doc compile)` and `(doc *compile-files*)`). There's no reason to worry about this most of the time. Note that this mode of operation is not particular to Clojure at all; Python does the same thing, compiling `.py` files to Python bytecode and then running it. One difference is that Python outputs the compiled modules as `.pyc` files by default, but this isn't even always possible (e.g. due to permission issues) and has no bearing on the way in which the code is executed.\n- If I create a `.class` file, and handed it to Bob, would he be able to run it without installing clojure?\n- As mentioned in the answer, you don't \"install\" Clojure, you simply put it on the classpath. Having said that, there are fairly straightforward methods of packaging the Clojure runtime together with your code in a single `jar`, so that Bob never needs to know it's been written in Clojure. You can also package your code in its own `jar` and send Bob a collection of `jar`s including `clojure.jar` (put it all in a single archive together with a launcher script and, again, Bob never needs to care what language it's been written in). See Leiningen: github.com/technomancy/leiningen\n- Clojure deployment is basically JVM deployment and there's loads and loads of tools and conventions to do with that. For organisations using Java, getting a bunch of `jar`s (or `war`s, as the case may be) is fantastic, because they know exactly what to do with them (and never need to care there's something called \"Clojure\" doing work inside them). For people coming from Ruby, this represents an area where new things have to be learned, but judging by the success of many prominent members of the Clojure community who used Ruby previously (or indeed still use it now), it isn't too big a problem.\n- To answer your .class file question specifically Bob would need the clojure jars to run it since the class file generated has a dependency on them. But that's no different than if I were to create a .class file in java that had a dependency on some other java library.\n- Thanks; these have been really helpful :) I like the dependency relationship; it really clears things up.","metadata":{"transformedAt":"2026-08-18T18:32:17.784Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":13,"totalLines":76,"estimatedTokens":888}}319{"id":"stack-426239","source":"stackoverflow","questionId":426239,"title":"Parsers and Compilers for Dummies. Where to start?","tags":["parsing","compiler-construction"],"text":"Title: Parsers and Compilers for Dummies. Where to start?\nTags: parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThis is a good listing, but what is the best one for a complete newb in this area. One for someone coming from a higher level background (VB6,C#,Java,Python) - not to familiar with C or C++. I'm much more interested in hand-written parsing versus Lex/Yacc at this stage.\n\nIf I had just majored in Computer Science instead of Psychology I might have taken a class on this in college. Oh well.\n\n========================================\n\nTop Answer:\nIf you're a complete n00b, the most accessible resource (in both senses of the term) is probably Jack Crenshaw's tutorial. It's nowhere near comprehensive but for getting started, I can't think of anything close except for books that are long out of print.\n\n========================================\n\nCode:\n```text\naddOp = '+' | '-';\nmulOp = '*' | '/';\nparLeft = '(';\nparRight = ')';\nnumber = digit, {digit};\ndigit = '0'..'9';\n\nEach token can have different representations: + and = are both addOp and \n23 6643 and 223322 are all numbers.\n```\n\n```text\nexp = term | exp, addOp, term;  \n// an expression is a series of terms separated by addOps.\nterm = factor | term, mulOp, factor;\n// a term is a series of factors separated by mulOps\nfactor = addOp, factor | parLeft, exp, parRight | number;\n// a factor can be an addOp followed by another factor, \n// an expression enclosed in parentheses or a number.\n```\n\n```text\ns00 \n  '+', '-' -> s01       // if a + or - is found, read it and go to state s01.\n  '*', '/' -> s02\n  '('      -> s03\n  ')'      -> s04\n  '0'..'9' -> s05\n  whitespace -> ignore and retry  // if a whitespace is found ignore it\n  else ERROR      // sorry but we don't recognize this character in this state.\ns01\n  found TOKEN addOp     // ok we have found an addOp, stop reading and return token\ns02 \n  found TOKEN mulOp\ns03\n  found TOKEN parLeft\ns04\n  found TOKEN parRight\ns05\n  '0'..'9'     -> s05    // as long as we find digits, keep collecting them\n  else found number      // last digit read, we have a number\n```\n\n```text\nParseExp\n  temp = ParseTerm // start by reading a term\n  while token = addOp do\n    // as long as we read an addop keep reading terms\n    if token('+') then temp = temp + ParseTerm  // + so we add the term\n    if token('-') then temp = temp - ParseTerm  // - so we subtract the term\n  od\n  return temp // we are done with the expression\n\nParseTerm\n  temp = ParseFactor\n  while token = mulOp do\n    if token('*') then temp = temp * ParseFactor\n    if token('/') then temp = temp / ParseFactor\n  od\n  return temp\n\nParseFactor\n  if token = addOp then\n    if token('-') then return - ParseFactor  // yes we can have a lot of these\n    if token('+') then return ParseFactor\n  else if token = parLeft then\n    return ParseExpression\n    if not token = parRight then ERROR\n  else if token = number then\n    return EvaluateNumber   // use magic to translate a string into a number\n```\n\n========================================\n\nComments:\n- BTW, hand-writing parsers instead of using YACC/similar does have its place, but it seems the lexing/scanning stage is very seldom handwritten. Also, most mature programming languages seem to have an analog to both Lex and Yacc.\n- Does CPython? I was looking at that source code and it seemed to be hand-written. PHP seemed to be Yacc'd, I think. I think the Python one is going to be the easiest for me to grok. I'm not sure I any of the generated C code yet.","metadata":{"transformedAt":"2026-08-18T18:32:17.784Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":95,"estimatedTokens":878}}320{"id":"stack-9680295","source":"stackoverflow","questionId":9680295,"title":"import statement byte code significance","tags":["java","compiler-construction","import","bytecode"],"text":"Title: import statement byte code significance\nTags: java, compiler-construction, import, bytecode\nSource: Stack Overflow\n\nQuestion:\nLets say , there are some import statements in a class. When the byte code is generated for that class, what happens to these import statements. \n\nIf the import statements are ignored during runtime, how are the dependencies on that classes methods resolved during runtime.\n\n========================================\n\nTop Answer:\n`import` statements are only there for the compiler so it knows what class names (or static method names) you can access unqualified in your code (i.e. `MyClass` instead of `foo.bar.MyClass`). Behind the scenes this is just used to resolve to the fully-qualified class names which are then used in the bytecode as well.\n\n========================================\n\nCode:\n```text\njava.util.*\n```\n\n```text\njava.util.ArrayList\n```\n\n```text\nArrayList\n```\n\n```text\nimport\n```\n\n```text\nMyClass\n```\n\n```text\nfoo.bar.MyClass\n```\n\n========================================\n\nComments:\n- Why not find out for yourself? Write a few variants of a class: one with reliance on outside classes, one that relies on (for instance) `java.util.List` via import, and one that relies on `List` but uses it fully qualified (without an import). Then use `javap -c` on each, and see what comes out.\n- Complementary to @yshavit comment, during the compilation use `javac -g:none Foo.java` for not generating debug info (e.g. line numbers) at all in the `.class` files, then compare the generated `.class`es and they will be identical at byte level and this can be tested getting the hash for each of them.\n- It is used to resolve unqualified names *to* fully qualified names.\n- That's what I meant. I shouldn't write in foreign languages while still half-asleep ;)","metadata":{"transformedAt":"2026-08-18T18:32:17.785Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":48,"estimatedTokens":450}}321{"id":"stack-1819114","source":"stackoverflow","questionId":1819114,"title":"When can typeid return different type_info instances for same type?","tags":["c++","language-design","rtti","typeid","compiler-construction"],"text":"Title: When can typeid return different type_info instances for same type?\nTags: c++, language-design, rtti, typeid, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nAndrei Alexandrescu writes in Modern C++ Design:\n\n The objects returned by `typeid` have\n static storage, so you don't have to\n worry about lifetime issues.\n\nAndrei continues:\n\n The standard does not guarantee that\n each invocation of, say, `typeid(int)`\n returns a reference to the same\n `type_info` object.\n\nEven though the standard does not guarantee this, how is this implemented in common compilers, such as GCC and Visual Studio?\n\nAssuming `typeid` does not leak (and return a new instance every call), is it one \"table\" per application, per translation unit, per dll/so, or something completely different?\n\nAre there times when `&typeid(T) != &typeid(T)`?\n\nI'm mainly interested in compilers for Windows, but any information for Linux and other platforms is also appreciated.\n\n========================================\n\nTop Answer:\nStandards sometimes leave certain behavior unspecified in order to give implementations some freedom. In this case, how TypeIDs are managed is being left up to the compiler implementation and you're simply being given a set of rules (essentially: don't concern yourself with how memory for this is being allocated).\n\nIs there any particular reason why you need to be able to compare TypeIds based upon their memory address? TypeIds already override == and != in order to provide you with the ability to compare them, and provide a name() that might be used to identify them uniquely.\n\nIf you've got The C++ Programming Language (Bjarne Stroustrup) available, chapter 15 has a lot of details about handling class hierarchies. Maybe you might find another solution there?\n\n========================================\n\nCode:\n```text\ntypeid\n```\n\n```text\ntypeid(int)\n```\n\n```text\ntype_info\n```\n\n```text\ntypeid\n```\n\n```text\n&typeid(T) != &typeid(T)\n```\n\n```text\nstruct foo { virtual ~foo() {} };\n```\n\n```text\n#include \"foo.h\"\n...\nfoo f;\ncout << &typeid(&f) << endl\n```\n\n```text\n#include \"foo.h\"\n...\nfoo f;\ncout << &typeid(&f) << endl\n```\n\n========================================\n\nComments:\n- An important reason for this level of freedom was indeed the concern for DLLs.\n- You have many good points, but this does not answer my question. I don't need to compare the addresses, I'm purely interested in how it is handled internally in compilers. But my main interests in this issue are those of compiler design: How and Why it is implemented in this way. Adding additional tags to the question.","metadata":{"transformedAt":"2026-08-18T18:32:17.785Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":81,"estimatedTokens":649}}322{"id":"stack-71276551","source":"stackoverflow","questionId":71276551,"title":"Why are unused type parameters on Rust structs considered an error and not a warning?","tags":["generics","rust","compiler-construction"],"text":"Title: Why are unused type parameters on Rust structs considered an error and not a warning?\nTags: generics, rust, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nBefore somebody marks this as a duplicate, I know ***how*** to fix it (and that has been answered already), but I'd like to understand ***why*** this limitation exists, and I found no answer on here or in the Rust docs.\n\nFor example, I wrote something that goes something like this:\n\n```\nstruct ItemList {\n items: Vec\n}\nimpl ItemList {\n fn getFirstItem(&self) -> Link { Link { position: 0 } }\n}\n\nstruct Link {\n position: usize\n}\nimpl Link {\n fn getFromList(&self, list: &'a ItemList) -> &'a T {\n &list.items[self.position]\n }\n}\n```\n\nBut rustc rejects my code with this error:\n\n```\nerror[E0392]: parameter `T` is never used\n --> src/main.rs:8:13\n |\n8 | struct Link {\n | ^ unused parameter\n |\n = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`\n = help: if you intended `T` to be a const parameter, use `const T: usize` instead\n\nFor more information about this error, try `rustc --explain E0392`.\n```\n\n**Why is that an error and not a warning**? The type parameters only reduce the performance during compile time (if I understood it correctly), so why would you enforce removing it or using a PhantomData marker? Does it have some implications that I missed?\n\n========================================\n\nTop Answer:\nAdding to what @mcarton said, this was not this way from the beginning: Rust used to infer unused generic parameters are bivariant. This changed in 2014 (before Rust 1.0). The motivation is explained in the RFC:\n\n### Motivation\n\nToday, variance inference for lifetimes includes the notion of *bivariance* -- which essentially amounts to unconstrained. In principle, this can have some use, but in practice it tends to be a vector for bugs. In fact, there is no known Rust code that *intentionally* uses bivariance (though there seems to be plenty that does so accidentally and incorrectly). This RFC proposes that we simply make an inference result of bivariance an error.\n\nAs an example of where this comes up, imagine a `struct` with a \"phantom\" lifetime parameter, meaning one that is not actually *used* in the fields of the `struct` itself. One example of such a type is `Items`, the vector iterator:\n\n```\nstruct Items {\n x: *mut T\n}\n```\n\nHere the lifetime `'vec` is intended to represent the lifetime of the vector being iterated over and hence to prevent the iterator from outliving the container. However, because it does not appear in the body of `Items` at all, the compiler would currently consider it irrelevant to subtyping. This means that you could convert from a `Items` to a `Items`, causing the iterator to outlive the container it is iterating over.\n\nTo prevent this scenario, the `actual` definition of the iterator in the standard library uses a *marker* type. The marker type informs the compiler that, although `'vec` does not appear to be used, it should act *as if* it were. For example, `Items` might be modified as follows:\n\n```\nstruct Items {\n x: *mut T,\n marker: marker::CovariantType,\n}\n```\n\nthe `CovariantType` marker basically informs the compiler that it should act \"as though\" a reference of type `&'vec T` were a member of Items, even thought it is not. Another equivalent option here would be `ContravariantLifetime`.\n\nCurrently, the user must know to insert these markers or else silently get the wrong behavior. This RFC makes it an error to have a type or lifetime parameter that is not (transitively) used somewhere in the type. Nothing else is changed.\n\nThe code is pretty old (e.g. `CovariantType` and `ContravariantLifetime` were replaced by `PhantomData`), but the concept still applies.\n\n========================================\n\nCode:\n```rust\nstruct ItemList<T> {\n  items: Vec<T>\n}\nimpl<T> ItemList<T> {\n  fn getFirstItem(&self) -> Link<T> { Link { position: 0 } }\n}\n\nstruct Link<T> {\n  position: usize\n}\nimpl<T> Link<T> {\n  fn getFromList<'a>(&self, list: &'a ItemList<T>) -> &'a T {\n    &list.items[self.position]\n  }\n}\n```\n\n```text\nerror[E0392]: parameter `T` is never used\n --> src/main.rs:8:13\n  |\n8 | struct Link<T> {\n  |             ^ unused parameter\n  |\n  = help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`\n  = help: if you intended `T` to be a const parameter, use `const T: usize` instead\n\nFor more information about this error, try `rustc --explain E0392`.\n```\n\n```rust\nstruct Items<'vec, T> {\n    x: *mut T\n}\n```\n\n```rust\nstruct Items<'vec, T> {\n    x: *mut T,\n    marker: marker::CovariantType<&'vec T>,\n}\n```\n\n```text\nstruct\n```\n\n```text\nstruct\n```\n\n```text\nItems\n```\n\n```text\n'vec\n```\n\n```text\nItems\n```\n\n```text\nItems<'a, T>\n```\n\n```text\nItems<'static, T>\n```\n\n```text\nactual\n```\n\n```text\n'vec\n```\n\n```text\nItems\n```\n\n```text\nCovariantType\n```\n\n```text\n&'vec T\n```\n\n```text\nContravariantLifetime\n```\n\n```text\nCovariantType\n```\n\n```text\nContravariantLifetime\n```\n\n```text\nPhantomData\n```\n\n========================================\n\nComments:\n- In what way is `struct Foo;` parameterised by `T`? In particular: is `Foo` covariant, contravariant or invariant over `T` (see the Rustonomicon chapter on variance)?","metadata":{"transformedAt":"2026-08-18T18:32:17.785Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":20,"totalLines":193,"estimatedTokens":1313}}323{"id":"stack-5126779","source":"stackoverflow","questionId":5126779,"title":"Parsing Context Sensitive Language","tags":["parsing","compiler-construction","antlr","context-sensitive-grammar"],"text":"Title: Parsing Context Sensitive Language\nTags: parsing, compiler-construction, antlr, context-sensitive-grammar\nSource: Stack Overflow\n\nQuestion:\ni am reading the Definitive ANTLR reference by Terence Parr, where he says:\n\n Semantic predicates are a powerful\n means of recognizing context-sensitive\n language structures by allowing\n runtime information to drive\n recognition\n\nBut the examples in the book are very simple. What i need to know is: can ANTLR parse context-sensitive rules like:\n\n**xAy --> xBy**\n\nIf ANTLR can't parse these rules, is there is another tool that deals with context-sensitive grammars?\n\n========================================\n\nTop Answer:\nIt is comparatively easy to write a context-sensitive parser in Prolog. This program parses the string `[a,is,less,than,b,and,b,is,less,than,c]`, converting it into `[a,Using the same algorithm, I also wrote an adaptive parser that \"learns\" new rewrite rules from its input.\n\n========================================\n\nCode:\n```text\nDO  20, I= ...\n   DO 10, J = ...\n       ...\n20  CONTINUE\n10  CONTINUE\n```\n\n```text\nDO  <number> , <variable> =  ...\n    DO <number> , <variable> = ...\n         ...\n<number> CONTINUE\n<number> CONTINUE\n```\n\n```text\nblock = 'DO' <number> rest_of_do_head newline \n         block_of_statements\n         <number> 'CONTINUE' newline ; CheckMatchingNumbers\n```\n\n```text\n:- initialization(main).\n:- set_prolog_flag('double_quotes','chars').\n\nmain :-\n    rewrite_system([a,is,less,than,b,and,b,is,less,than,c],X),writeln('\\nFinal output:'),writeln(X).\n\nrewrite_rule([[A,<,B],and,[B,<,C]],[A,<,B,<,C]).\nrewrite_rule([A,is,less,than,B],[A,<,B]).\nrewrite_rule([[A,<,B],and,C,than,D],[[A,<,B],and,A,is,C,than,D]).\nrewrite_rule([A,<,B],[[A,<,B]]).\n\nrewritten(A) :- atom(A);bool(A).\nbool(A) :- atom(A).\nbool([A,<,B,<,C]) :- atom(A),atom(B),atom(C).\nbool([A,and,B]) :- bool(A),bool(B).\n\n\n% this predicate is from https://stackoverflow.com/a/8312742/975097\nreplace(ToReplace, ToInsert, List, Result) :-\n    once(append([Left, ToReplace, Right], List)),\n    append([Left, ToInsert, Right], Result).\n\nrewrite_system(Input,Output) :-\n    rewritten(Input),Input=Output;\n    rewrite_rule(A,B),\n    replace(A,B,Input,Input1),\n    writeln(Input1),\n    rewrite_system(Input1,Output).\n```\n\n```text\n[a,is,less,than,b,and,b,is,less,than,c]\n```\n\n```text\n[a,<,b,<,c]\n```\n\n========================================\n\nComments:\n- ok ,this rule mean that we need to save the context where the context free grammar look like A --> BC , for more information : en.wikipedia.org/wiki/Chomsky_hierarchy\n- @Bart: \"In the context of x and y, A can be replaced by B\".\n- `xAy --> xBy` could probably be converted into a definite clause grammar in Prolog.\n- +1 there are no full implementation , and these tools not efficient . thanks for help\n- @Radi: I said I don't *know* of any. I'm sure somebody has implemented one somewhere.","metadata":{"transformedAt":"2026-08-18T18:32:17.785Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":96,"estimatedTokens":722}}324{"id":"stack-8195507","source":"stackoverflow","questionId":8195507,"title":"How to programmatically generate .class files?","tags":["java","compiler-construction","jvm",".class-file"],"text":"Title: How to programmatically generate .class files?\nTags: java, compiler-construction, jvm, .class-file\nSource: Stack Overflow\n\nQuestion:\nI would like to write a compiler for a toy-language for Java. I would like to generate runnable .class files. I was wondering what is the best library or tool available for doing this? I know I could learn the binary format for all the instructions and build my own constant pool etc, but that seems like work that ought to have been already done: no point reinventing the wheel, right?\n\nSearching online I've found two different Java Assembly languages, Jasmin and Jamaica, however only Jasmin looks somewhat maintained. \n\nIs there a Java library for writing byte codes to a stream? Is this what the Apache BCEL is?\n\nIs their a tool for this that is the \"standard\" for byte-code generation, like Antlr is for parsing?\n\nPS- The toy language is Brainf***, I wanted something where I could have a simple \"grammar\" so I could focus on the generation aspect and not the parsing part... that will come later on the next step.\n\n========================================\n\nTop Answer:\nIt sounds like you're looking for Apache BCEL:\n\n The Byte Code Engineering Library (Apache Commons BCEL™) is intended to give users a convenient way to analyze, create, and manipulate (binary) Java class files (those ending with .class).\n\n========================================\n\nCode:\n```text\npublic class JavaSourceFromString extends SimpleJavaFileObject {\n       final String code;\n\n       JavaSourceFromString(String name, String code) {\n           super(URI.create(\"string:///\"\n                            + name.replace('.','/')\n                            + Kind.SOURCE.extension),\n                 Kind.SOURCE);\n           this.code = code;\n       }\n\n       @Override\n       public CharSequence getCharContent(boolean ignoreEncodingErrors) {\n           return code;\n       }\n   }\n\n// Use your favorite template language here, like FreeMarker\nstatic final String sourceCode = \"\"\n        + \"import org.example.MySomethingObject;\"\n        // DynamicStringGetter would define getString as a standard way to get\n        // a String from an object\n        + \"public class GetStringDynamic implements DynamicStringGetter {\\n\" \n        + \"    public String getString(Object o) {\\n\"\n        + \"        MySomethingObject obj = (MySomethingObject) o;\\n\"\n        + \"        return o.getSomething();\\n\"\n        + \"    }\\n\"\n        + \"}\\n\";\n\n   JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();\n   StandardJavaFileManager fileManager = \n       compiler.getStandardFileManager(null, null, null);\n\n   List<JavaFileObject> files = new ArrayList<JavaFileObject>();\n   files.add(new JavaSourceFromString(\"org.example.DynamicClass\", sourceCode));\n\n   compiler.getTask(null, fileManager, null, null, null, files).call();\n```\n\n========================================\n\nComments:\n- I tend to use ASM, but it is likely to be too low level.\n- An ASM for Java byte-code or are you generating native code?\n- Objectweb's ASM library. It's not as easy to use as some libraries, but I like the low level approach.\n- @Ira Baxter Implementing BF is easy, that is why I am starting with it. I'm using this as an excuse to learn the tools.\n- I looked at that and 1) I can't tell if this does what I want, and 2) if this is the preferred tool. I'd really like to hear from someone that has actually done this and can vouch for a tool.\n- @ArtB: It would have helped if you'd said that you'd seen it... I haven't used it myself, but I've certainly heard good things about it. You might also want to look at cglib\n- I had looked at it and CGLib, CGLib looks like its focussed on run-time manipulation. BCEL I'm looking through now\n- This depends if the toy language can be easily translated to Java. There are several compilers for many languages that compile to byte-code but translating them for Java would be a challenging task.\n- Java isn't a particularly expressive language. One of the reason to use other languages, even JVM-based ones, is to get around this limitation.\n- Can this even be used to generate standalone class files, or does it only work for in-memory compilation? Also, the toy language is much more low-level than Java making the translation awkward.\n- @ArtB Yes, it can generate standalone class files, or the classes can be saved in memory depending on what you do with the FileManager. We just store the classes in memory here because for our purposes they are ephemeral. An example of the toy language might help determine suitability. Typically generating Java source will be easier than determining byte codes, but if you're doing something more along the lines of adding a new language to the VM it might be overkill. On the other hand, it's easy to refer to the Java libraries using source code.\n- The whole purpose of this exercise is to learn the tools, so determining byte codes it is.\n- @ArtB Fairy nuff. I suggest ASM or the Scala one I linked above then.\n- Thanks a lot for the code example above! it seems to work for me, but it's generating a .class file in the root of my project. Is there a way to just retrieve the generated .class file as a byte[] rather than writing to file? Or, alternatively, to specify where the .class file should be written?\n- @AlexAverbuch blogs.helion-prime.com/2008/06/13/&hellip; has an example showing how to store the resulting classes in a byte array.","metadata":{"transformedAt":"2026-08-18T18:32:17.785Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":83,"estimatedTokens":1356}}325{"id":"stack-340413","source":"stackoverflow","questionId":340413,"title":"How do C/C++ compilers handle type casting between types with different value ranges?","tags":["c++","c","types","casting","compiler-construction"],"text":"Title: How do C/C++ compilers handle type casting between types with different value ranges?\nTags: c++, c, types, casting, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nHow do type casting happen without loss of data inside the compiler?\n\nFor example:\n\n```\nint i = 10;\n UINT k = (UINT) k;\n\n float fl = 10.123;\n UINT ufl = (UINT) fl; // data loss here?\n\n char *p = \"Stackoverflow Rocks\";\n unsigned char *up = (unsigned char *) p;\n```\n\nHow does the compiler handle this type of typecasting? A low-level example showing the bits would be highly appreciated.\n\n========================================\n\nTop Answer:\nThe two C-style casts in your example are different kinds of cast. In C++, you'd normally write them\n\n```\nunsigned int uf1 = static_cast(fl);\n```\n\nand\n\n```\nunsigned char* up = reinterpret_cast(p);\n```\n\nThe first performs an arithmetic cast, which truncates the floating point number, so there is data loss.\n\nThe second makes no changes to data - it just instructs the compiler to treat the pointer as a different type. Care needs to be taken with this kind of cast: it can be very dangerous.\n\n========================================\n\nCode:\n```text\nint i = 10;\n UINT k = (UINT) k;\n\n float fl = 10.123;\n UINT  ufl = (UINT) fl; // data loss here?\n\n char *p = \"Stackoverflow Rocks\";\n unsigned char *up = (unsigned char *) p;\n```\n\n```text\nint i = 10; \nunsigned int k = (unsigned int) i; // :1\n\nfloat fl = 10.123;\nunsigned int  ufl = (unsigned int) fl; // :2\n\nchar *p = \"Stackoverflow Rocks\"; \nunsigned char *up = (unsigned char *) p; // :3\n```\n\n```text\n-1\n```\n\n```text\nunsigned int\n```\n\n```text\n10.123\n```\n\n```text\nchar*\n```\n\n```text\nchar *\n```\n\n```text\n& __stack[4] = float_to_int_bits(& __stack[0])\n```\n\n```text\nunsigned int uf1 = static_cast<unsigned int>(fl);\n```\n\n```text\nunsigned char* up = reinterpret_cast<unsigned char*>(p);\n```\n\n```text\nchar * cp; \nfloat * fp; \ncp = malloc(100); \nfp = (float *)(cp + 1);\n```\n\n========================================\n\nComments:\n- Note: This is true only in C++ not in C.","metadata":{"transformedAt":"2026-08-18T18:32:17.785Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":109,"estimatedTokens":508}}326{"id":"stack-8577466","source":"stackoverflow","questionId":8577466,"title":"Building a linker","tags":["compiler-construction","linker"],"text":"Title: Building a linker\nTags: compiler-construction, linker\nSource: Stack Overflow\n\nQuestion:\nThere is a lot of information out there about designing and implementing a compiler, but i can't find anything about linkers.\n\nI'm very interested in these subjects but I can't find any information about linkers. \nDoes anyone know a tutorial, book, etc. about linker design and implementation?","metadata":{"transformedAt":"2026-08-18T18:32:17.785Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":9,"estimatedTokens":97}}327{"id":"stack-710459","source":"stackoverflow","questionId":710459,"title":"How method hiding works in C#? (Part Two)","tags":["c#","inheritance","compiler-construction","method-hiding"],"text":"Title: How method hiding works in C#? (Part Two)\nTags: c#, inheritance, compiler-construction, method-hiding\nSource: Stack Overflow\n\nQuestion:\nThe following program prints\n\n```\nA:C(A,B)\nB:C(A,B)\n```\n\n(as it should)\n\n```\npublic interface I\n{\n string A();\n}\n\npublic class C : I\n{\n public string A()\n {\n return \"A\";\n }\n\n public string B()\n {\n return \"B\";\n }\n}\n\npublic class A\n{\n public virtual void Print(C c)\n {\n Console.WriteLine(\"A:C(\" + c.A() + \",\" + c.B() + \")\");\n }\n}\n\npublic class B : A\n{\n public new void Print(C c)\n {\n Console.WriteLine(\"B:C(\" + c.A() + \",\" + c.B() + \")\");\n }\n\n public void Print(I i)\n {\n Console.WriteLine(\"B:I(\" + i.A() + \")\");\n }\n}\n\nclass Program\n{\n public static void Main(string[] args)\n {\n A a = new A();\n B b = new B();\n C c = new C();\n a.Print(c);\n b.Print(c);\n }\n}\n```\n\nhowever, if I change keyword 'new' to 'override' in class B like so:\n\n```\npublic override void Print(C c)\n```\n\nall of a sudden program starts to print:\n\n```\nA:C(A,B)\nB:I(A)\n```\n\nWhy?\n\n========================================\n\nTop Answer:\nOk, so\n\n```\npublic new void Print(C c)\n {\n Console.WriteLine(\"B:C(\" + c.A() + \",\" + c.B() + \")\");\n }\n\n public void Print(I i)\n {\n Console.WriteLine(\"B:I(\" + i.A() + \")\");\n }\n```\n\nThis declares a new method for the print. Now because B inherits from A, you are simly calling the new method twice. When you overide the method, this then changes the method signature when you call for A, but when you call the B signature, then it has its own method signature.\n\nI am not sure if I am explaining clear but good question.\n\nusing new:\n\nA and B get the same implementation of the Print Method.\n\nusing override:\n\nA has a different method signature to B as, you have not changed the method signature in B only in A.\n\nusing the new it basically ignores this:\n\n```\npublic void Print(I i)\n {\n Console.WriteLine(\"B:I(\" + i.A() + \")\");\n }\n```\n\n========================================\n\nCode:\n```text\nA:C(A,B)\nB:C(A,B)\n```\n\n```text\npublic interface I\n{\n    string A();\n}\n\npublic class C : I\n{\n    public string A()\n    {\n        return \"A\";\n    }\n\n    public string B()\n    {\n        return \"B\";\n    }\n}\n\npublic class A\n{\n    public virtual void Print(C c)\n    {\n        Console.WriteLine(\"A:C(\" + c.A() + \",\" + c.B() + \")\");\n    }\n}\n\npublic class B : A\n{\n    public new void Print(C c)\n    {\n        Console.WriteLine(\"B:C(\" + c.A() + \",\" + c.B() + \")\");\n    }\n\n    public void Print(I i)\n    {\n        Console.WriteLine(\"B:I(\" + i.A() + \")\");\n    }\n}\n\nclass Program\n{\n    public static void Main(string[] args)\n    {\n        A a = new A();\n        B b = new B();\n        C c = new C();\n        a.Print(c);\n        b.Print(c);\n    }\n}\n```\n\n```text\npublic override void Print(C c)\n```\n\n```text\nA:C(A,B)\nB:I(A)\n```\n\n```text\nusing System;\n\nclass Base\n{\n    public virtual void Foo(int x)\n    {\n        Console.WriteLine(\"Base.Foo(int)\");\n    }\n}\n\nclass Derived : Base\n{\n    public override void Foo(int x)\n    {\n        Console.WriteLine(\"Derived.Foo(int)\");\n    }\n\n    public void Foo(double d)\n    {\n        Console.WriteLine(\"Derived.Foo(double)\");\n    }\n}\n\nclass Test\n{\n    static void Main()\n    {\n        Derived d = new Derived();\n        d.Foo(10);\n    }\n}\n```\n\n```text\nDerived.Foo(double)\n```\n\n```text\nint\n```\n\n```text\nint\n```\n\n```text\nint\n```\n\n```text\nint\n```\n\n```text\nint\n```\n\n```text\ndouble\n```\n\n```text\nFoo(double)\n```\n\n```text\nDerived\n```\n\n```text\nFoo(int)\n```\n\n```text\nDerived\n```\n\n```text\nFoo\n```\n\n```text\nDerived\n```\n\n```text\nBase.Foo(int)\n```\n\n```text\npublic new void Print(C c)\n    {\n        Console.WriteLine(\"B:C(\" + c.A() + \",\" + c.B() + \")\");\n    }\n\n    public void Print(I i)\n    {\n        Console.WriteLine(\"B:I(\" + i.A() + \")\");\n    }\n```\n\n```text\npublic void Print(I i)\n    {\n        Console.WriteLine(\"B:I(\" + i.A() + \")\");\n    }\n```\n\n```text\nnew\n```\n\n```text\nPrint\n```\n\n```text\nPrint\n```\n\n```text\noverride\n```\n\n```text\nPrint\n```\n\n```text\nPrint(C c)\n```\n\n```text\nPrint(I i)\n```\n\n========================================\n\nComments:\n- Furthermore, if you passed `d` as a parameter to a method which expects `Base`, you would get `Derived.Foo(int)` (same as writing `(d as Base).Foo(10);` inside `Main()`). Although it is clear how this works, I find this pretty smelly and therefore avoid method hiding in all cases.","metadata":{"transformedAt":"2026-08-18T18:32:17.785Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":28,"totalLines":331,"estimatedTokens":1067}}328{"id":"stack-6229061","source":"stackoverflow","questionId":6229061,"title":"Typed vs typeless language","tags":["types","compiler-construction","programming-languages","declaration"],"text":"Title: Typed vs typeless language\nTags: types, compiler-construction, programming-languages, declaration\nSource: Stack Overflow\n\nQuestion:\nI am learning **C** and assembly language together. I noticed that assembly is a type-less language compared to the **C** which requires declaration of the data type before data can be processed. But also I learnt that even a code written in **C** is first compiled into a code written in assembly and then assembled into object code. So that means data type declaration we use in **C** or any high level language is only meant for the ease of **C** compiler. They don't have any special bearing on the object code. Is that correct?\n\nWhat I gathered is that type declaration tells compiler what all operations can be done on the data, size of the data (required to store data in data segment), size of the max and min decimal numbers that can be stored. Am I right to say so? \n\nAre there are any other benefits of type declaration?\n\n========================================\n\nTop Answer:\nThere is a lot that can be said about types and their value for programming. And what you see in C is not even the tip of the iceberg. Rather, it's a dirty snow ball that somebody dumped on the tip of the tip of the iceberg. :) The first couple of pages in the following classic paper explain a few of the basic merits of type systems:\n\nhttp://www.lucacardelli.name/Papers/TypeSystems.pdf\n\nLet me just add two things.\n\nFirst, there is a difference between a language being typed, and requiring (explicit) type declarations. Some modern languages, especially from the functional camp, have sophisticated type systems that yet don't require you to write down a single type most of the time. All types are inferred by the compiler.\n\nSecond, a type system essentially is a logic. A logic that expresses certain properties of a program, which then get checked by the compiler. In principle, there is no limit to how powerful this logic can be made. C is an extremely boring example. On the other end of the spectrum are languages where you can e.g. express the type of sorted lists, and the type of a sorting function, so that the function only type-checks if it actually is a correct implementation of a sorting algorithm. Obviously, it is immensely useful if the compiler can actually check correctness of your program like that. However, there is a trade-off between expressiveness of a type system and ease of use, so in practice, most mainstream languages end up on the simplistic side. But special domains sometimes benefit from more sophisticated type systems enormously.\n\nHere is a recent article in CACM discussing (among other things) the advantages of the type system found in the functional language OCaml:\n\nhttp://cacm.acm.org/magazines/2011/11/138203-ocaml-for-the-masses/\n\n========================================\n\nCode:\n```text\nint\n```\n\n```text\nshort\n```\n\n```text\nint\n```\n\n```text\nfoo.y\n```\n\n```text\nstruct Foo { int x; short y; };\n```\n\n```text\nstruct Foo foo;\n```\n\n```text\nint\n```\n\n```text\nshort\n```\n\n```text\nstruct Foo\n```\n\n```text\nfoo\n```\n\n```text\ny\n```\n\n```text\nstruct Foo\n```\n\n```text\nstruct Foo foo;\n```\n\n```text\nstruct Foo\n```\n\n```text\nsizeof(struct Foo)\n```\n\n```text\niadd\n```\n\n```text\nfadd\n```\n\n```text\np + n\n```\n\n```text\nn * sizeof(*p)\n```\n\n```text\n[a] -> a\n```\n\n```text\nvoid *\n```\n\n```text\nchar var1 = 'a'\n```\n\n```text\nint var2 = 10;\n```\n\n```text\nvar1 + var2\n```\n\n```text\n107\n```\n\n```text\n107\n```\n\n```text\na\n```\n\n```text\n'a' + 7\n```\n\n========================================\n\nComments:\n- quick practical answer: use typeless prog lang for quick stuff, use typed progr lang for complex apps, even if it looks more difficult\n- so data type declarations of a high level language are equivalent of directives of assembly language. they help the compiler but does not get translated into the machine code.. Am I correct to say so?\n- @Saurabh That sounds about right, but I honestly don't know enough about assembler (specifically directives) to say for sure.\n- The C language also defines the result of `'a' + 7`.\n- Yes, and also the value of \"a\"+7 and 7+\"a\" and \"a\"+'a'. Tricky thing, C.","metadata":{"transformedAt":"2026-08-18T18:32:17.785Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":28,"totalLines":151,"estimatedTokens":1033}}329{"id":"stack-1938242","source":"stackoverflow","questionId":1938242,"title":"How would one create a Clojure Lint?","tags":["compiler-construction","clojure","lint"],"text":"Title: How would one create a Clojure Lint?\nTags: compiler-construction, clojure, lint\nSource: Stack Overflow\n\nQuestion:\nOne example of a common Clojure programming error is expecting a lazy expression to be evaluated for side-effects. On the surface it appears checking for unused lazy expressions would be helpful. What would be the best approach to identifying this and other common mistakes? Should the core compiler check for these situations, or should it be the domain of a lint program to detect? What would be a good way to start the implementation?\n\n========================================\n\nTop Answer:\nA couple of ideas just to get things started; it could detect lazy code that can never be realized, or point out areas where reflection will be used. Though in general is clojure a little young as a language to express a common set of provable mistakes?\n\n========================================\n\nCode:\n```text\n:default\n```\n\n```text\n& rest\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.785Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":22,"estimatedTokens":240}}330{"id":"stack-11767724","source":"stackoverflow","questionId":11767724,"title":"CoffeScript'esque language for Objective-C?","tags":["objective-c","cocoa","compiler-construction","coffeescript","uos"],"text":"Title: CoffeScript'esque language for Objective-C?\nTags: objective-c, cocoa, compiler-construction, coffeescript, uos\nSource: Stack Overflow\n\nQuestion:\nSeeing what has been done for Java with Xtend and Mirah I can't help but think someone must be playing around with something similar for Objective-C or even C and C++ for that matter. \n\nAfter some searching I've come up with nil. Is anyone aware of a CoffeeScript like Objective-C implementation?\n\nUpdate:\nGood input so far from the two that have submitted answers, however wouldn't it be superior (realizing that that's a little subjective) to have an intermediate language that compiled directly to Obj-C precisely as per how CoffeScript works? Now, I'm not asking for CoffeeScript mind you, but rather some language that doesn't compile directly, but rather gives you a more readable top layer syntactically a la Xtend.\n\n========================================\n\nTop Answer:\nYes: Eero, which provides a somewhat Python-like syntax for Objective-C, implemented using a modified version of clang. (I haven't tried using it, though, so I can't comment on how useful it is!)\n\n========================================\n\nComments:\n- Objective-C people are actually pretty fond of their syntax, often. Leading to things such as en.wikipedia.org/wiki/Objective-J\n- Yes @RussellMull - I'm quite familiar with Obj-J, however I, frankly, don't like the syntax of Obj-C personally. Coming from a C background it takes some serious \"getting use to\" time to come over to the syntax. I've known quite a few people from academia who really have griped and complained about how it feels to get started on Obj-C. Several have shelved it due to this as they'd prefer to have less syntax to worry about... but yeah, I know what you mean about dyed-in-the-wool ObjC folks.\n- Hi, regarding the update comment about compiling to ObjC, Eero supports this now as well. See: Eero to Objective-C translation (source-to-source)\n- Heh, you get addicted to Obj-C syntax because it's \"self-commenting\" by design. The very long method names with named parameters means simply reading the method calls tells you exactly what the code is doing. (That was only possible to pull off because the very first Obj-c editor back 1989 was the first to integrate auto-complete.) Once you've spent years or months working with easily read code, it's hard to go back. Plus, you get all the LISP-like structure which is hard to go without. Still, it is definitely a steep learning curve.\n- Worth checking out is github.com/kmalakoff/SubjectiveScript.m which aims to make objc more script like using short macros for common things.\n- That's nice to see. I really like what's being done with Eero there. Thanks for sharing that.\n- With @andy-arvanitis's comment above, I have to say that this is looking more like the answer now: compiling to ObjC, Eero supports this now as well. See: Eero to Objective-C translation (source-to-source)\n- Excellent +1 on RubyMotion. I'd not seen that previously. I have been under the impression that MacRuby just hasn't grown as I thought it would / should. I personally felt for the last couple of years that Apple could have made it a first class citizen and allowed for a full dev env within Xcode for Ruby iOS apps... I'm quite sad to not see it come to fruition as of yet.\n- @ylluminate well, given all the effort Apple does to promote native apps, Xcode as the only IDE and Obj-C as the only language (or maybe to demote other alternatives, i don't know... vendor-lock maybe?), i think it's natural that other languages/tools have a hard being adopted. I haven't done any iOS development myself, but having done some Android, it's quite the same thing with Java (fortunately, the JVM is becoming a more polyglot platform each day... so that's slowly changing for good =D). One RubyMotion feature that i wish i had in Android is the interactive REPL.\n- I think at this point MacRuby is either dead or at least on development hiatus. Check into it's current status before committing to it for projects.\n- @TechZen you might want to see the new answer I popped up here which I recently discovered.","metadata":{"transformedAt":"2026-08-18T18:32:17.785Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":31,"estimatedTokens":1036}}331{"id":"stack-691844","source":"stackoverflow","questionId":691844,"title":"How would one go about testing an interpreter or a compiler?","tags":["testing","compiler-construction","interpreter","brainfuck"],"text":"Title: How would one go about testing an interpreter or a compiler?\nTags: testing, compiler-construction, interpreter, brainfuck\nSource: Stack Overflow\n\nQuestion:\nI've been experimenting with creating an interpreter for Brainfuck, and while quite simple to make and get up and running, part of me wants to be able to run tests against it. I can't seem to fathom how many tests one might have to write to test all the possible instruction combinations to ensure that the implementation is proper.\n\nObviously, with Brainfuck, the instruction set is small, but I can't help but think that as more instructions are added, your test code would grow exponentially. More so than your typical tests at any rate.\n\nNow, I'm about as newbie as you can get in terms of writing compilers and interpreters, so my assumptions could very well be way off base.\n\nBasically, where do you even begin with testing on something like this?\n\n========================================\n\nTop Answer:\nI don't think there's anything 'special' about testing a compiler; in a sense it's almost easier than testing some programs, since a compiler has such a basic high-level summary - you hand in source, it gives you back (possibly) compiled code and (possibly) a set of diagnostic messages.\n\nLike any complex software entity, there will be many code paths, but since it's all very data-oriented (text in, text and bytes out) it's straightforward to author tests.\n\n========================================\n\nCode:\n```text\nfunction fail {\n  echo \"Unexpected differences on $1:\"\n  diff $2 $3\n  exit 1\n}\n\nfor testname\ndo\n  tmp1=$(tempfile)\n  tmp2=$(tempfile)\n  brainfuck $testname.bf < $testname.0 > $tmp1 2> $tmp2\n  [ cmp -s $testname.1 $tmp1 ] || fail \"stdout\" $testname.1 $tmp1\n  [ cmp -s $testname.2 $tmp2 ] || fail \"stderr\" $testname.2 $tmp2\ndone\n```\n\n```text\nbrainfuck $testname.bf < $testname.0 > $testname.1 2> $testname.2\n```\n\n```text\ntest001.bf\n```\n\n```text\ntest001.0\n```\n\n```text\ntest001.1\n```\n\n```text\ntest001.2\n```\n\n```text\ntest2\n```\n\n========================================\n\nComments:\n- Yeah, I've been doing this, and it has helped a bit, but I was curious of a more broad scenario as well. For instance, if I create my own programming language, I won't have pre-made apps ready for me to throw at it.\n- It *is* possible to write your own tests ;) Any time you find a bug, you could write a test to ensure that it doesn't reappear.\n- Oh, obviously. :-) That's why I asked the question. But simply throwing already written apps at it is impossible if we're at day 0. That's all. Writing tests, aren't. :D\n- a classical test case for compilers is to compile the compiler itself and check whether the compiler is the same as the compiled compiler. This is called self hosting.\n- -1: The OP asked for techniques for testing interpreters and compilers. Telling him that he should write the code well does not answer his question.","metadata":{"transformedAt":"2026-08-18T18:32:17.785Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":72,"estimatedTokens":726}}332{"id":"stack-2636563","source":"stackoverflow","questionId":2636563,"title":"Question about compilers and how they work","tags":["c++","c","compiler-construction"],"text":"Title: Question about compilers and how they work\nTags: c++, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThis is the C code that frees memory of a singly linked list. It is compiled with Visual C++ 2008 and code works as it should be.\n\n```\n/* Program done, so free allocated memory */\ncurrent = head;\nstruct film * temp;\ntemp = current;\nwhile (current != NULL)\n{\n temp = current->next;\n free(current);\n current = temp;\n}\n```\n\nBut I also encountered ( even in a books ) same code written like this:\n\n```\n/* Program done, so free allocated memory */\ncurrent = head;\nwhile (current != NULL)\n{\n free(current);\n current = current->next;\n}\n```\n\nIf I compile that code with my VC++ 2008, program crashes because I am first freeing current and then assigning current->next to current. But obviously if I compile this code with some other complier ( for example, compiler that book author used ) program will work. So question is, why does this code compiled with specific compiler work? Is it because that compiler put instructions in binary file that remember address of current->next although I freed current and my VC++ doesn't. I just want to understand how compilers work.\n\n========================================\n\nTop Answer:\nAfter you do `free(current)`, the memory pointed to by `current` (where `current->next` is stored) has been returned to the C library, so you shouldn't access it anymore.\n\nThe C library can change the contents of that memory at any time - which will result in `current->next` being corrupted - but it also might not change some or all of it, particularly so soon. That's why it works in some environments, and not others.\n\nIt's kind of like driving through a red traffic light. Sometimes you'll get away with it, but sometimes you'll be run over by a truck.\n\n========================================\n\nCode:\n```text\n/* Program done, so free allocated memory */\ncurrent = head;\nstruct film * temp;\ntemp = current;\nwhile (current != NULL)\n{\n    temp = current->next;\n    free(current);\n    current = temp;\n}\n```\n\n```text\n/* Program done, so free allocated memory */\ncurrent = head;\nwhile (current != NULL)\n{\n    free(current);\n    current = current->next;\n}\n```\n\n```text\ncurrent\n```\n\n```text\ncurrent\n```\n\n```text\nvalgrind\n```\n\n```text\nfree(current)\n```\n\n```text\ncurrent\n```\n\n```text\ncurrent->next\n```\n\n```text\ncurrent->next\n```\n\n```text\nfree()\n```\n\n```text\nstruct film { film* next; };\n\nint main() {\n  film* current = new film();\n  delete current;\n\n  return 0;\n}\n\n;Creation\nmovl    $4, (%esp)   ;the sizeof(film) into the stack (4 bytes)\ncall    _Znwj        ;this line calls the 'new operator' \n                     ;the register %eax now has the pointer\n                     ;to the newly created object\n\nmovl    $0, (%eax)   ;initializes the only variable in film\n\n;Destruction\nmovl    %eax, (%esp) ;push the 'current' point to the stack\ncall    _ZdlPv       ;calls the 'delete operator' on 'current'\n```\n\n========================================\n\nComments:\n- Please tell us the book so we can avoid it and recommend against it.\n- C primer ( all editions 5th, 4th..no errata for this error) look this topic: bytes.com/topic/c/answers/212665-freeing-simple-linked-list\n- What happens is the C run-time filling the bytes of a freed block with some pattern (0xfeeefeee for MSVC). That only happens in debug builds though, if you try it in release it will most probably work. It's still a undefined behavior, scrap any book that recommends this.\n- bytes.com/topic/c/answers/212665-freeing-simple-linked-list C primer plus (all editions, 5th, 4th...)\n- Thanks - I knew that the books that the Indian colleges use for C programming are pretty bad (Kanetkar, Balaguruswami, etc) but I guess that the problem is more widespread.\n- It would probably be easier to put the address of the last 4 frees in the DR0-DR3 registers and put a read breakpoint on all of them.","metadata":{"transformedAt":"2026-08-18T18:32:17.785Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":133,"estimatedTokens":974}}333{"id":"stack-44888163","source":"stackoverflow","questionId":44888163,"title":"Using the TypeScript type checker to see if two types are assignable","tags":["typescript","compiler-construction","typescript-compiler-api"],"text":"Title: Using the TypeScript type checker to see if two types are assignable\nTags: typescript, compiler-construction, typescript-compiler-api\nSource: Stack Overflow\n\nQuestion:\nI'm building a small script that scans for all interfaces that have a member of a given type using the TypeScript Compiler API, of which the source can be found here. I inspect the members of these classes to see how they are interlinked.\n\nMy question is: how do I know when a type is assignable to another type? I searched the `TypeChecker` for a method but I couldn't find one. Does anyone by any chance have any pointers? Here's an example of something that should be able to get analysed:\n\n```\nexport enum ASTKind {\n Number,\n Addition,\n Multiplication,\n}\n\nexport interface AST {\n kind: ASTKind \n}\n\nexport interface BinaryExpression extends AST {\n left: AST\n right: AST\n}\n\nexport interface Addition extends BinaryExpression {\n kind: ASTKind.Addition \n}\n\nexport interface Multiplication extends BinaryExpression {\n kind: ASTKind.Multiplication \n}\n```\n\nEssentially, I want a predicate that says whether `ASTKind.Multiplication` is assignable to `ASTKind` (which is true in this case).\n\n========================================\n\nTop Answer:\nThe `isTypeAssignableTo` method is now exposed on the `TypeChecker` API, see the PR adding it.\n\nCopying the signature reference from the TS source code:\n\n```\n/**\n * Returns true if the \"source\" type is assignable to the \"target\" type.\n *\n */\nisTypeAssignableTo(source: Type, target: Type): boolean\n```\n\nThe docstring also includes some usage examples which I've re-formatted below:\n\n```\ndeclare const abcLiteral: ts.Type; // Type of \"abc\"\ndeclare const stringType: ts.Type; // Type of string\nisTypeAssignableTo(abcLiteral, abcLiteral); // true; \"abc\" is assignable to \"abc\"\nisTypeAssignableTo(abcLiteral, stringType); // true; \"abc\" is assignable to string\nisTypeAssignableTo(stringType, abcLiteral); // false; string is not assignable to \"abc\"\nisTypeAssignableTo(stringType, stringType); // true; string is assignable to string\n```\n\n========================================\n\nCode:\n```js\nexport enum ASTKind {\n  Number,\n  Addition,\n  Multiplication,\n}\n\nexport interface AST {\n  kind: ASTKind  \n}\n\nexport interface BinaryExpression extends AST {\n  left: AST\n  right: AST\n}\n\nexport interface Addition extends BinaryExpression {\n  kind: ASTKind.Addition \n}\n\nexport interface Multiplication extends BinaryExpression {\n  kind: ASTKind.Multiplication \n}\n```\n\n```text\nTypeChecker\n```\n\n```text\nASTKind.Multiplication\n```\n\n```text\nASTKind\n```\n\n```text\nvar x = new ASTKind.Multiplication\nx instanceof ASTKind.Multiplication // will return true\nx instanceof AST // sill also return true\n```\n\n```ts\n/**\n * Returns true if the \"source\" type is assignable to the \"target\" type.\n *\n */\nisTypeAssignableTo(source: Type, target: Type): boolean\n```\n\n```ts\ndeclare const abcLiteral: ts.Type; // Type of \"abc\"\ndeclare const stringType: ts.Type; // Type of string\nisTypeAssignableTo(abcLiteral, abcLiteral); // true; \"abc\" is assignable to \"abc\"\nisTypeAssignableTo(abcLiteral, stringType); // true; \"abc\" is assignable to string\nisTypeAssignableTo(stringType, abcLiteral); // false; string is not assignable to \"abc\"\nisTypeAssignableTo(stringType, stringType); // true; string is assignable to string\n```\n\n```text\nisTypeAssignableTo\n```\n\n```text\nTypeChecker\n```\n\n========================================\n\nComments:\n- This question is similar to: Typescript compiler as a service: how to test if one type is assignable to another?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.\n- I am tinkering with the compiler itself: github.com/Microsoft/TypeScript/tree/master/src/compiler\n- It is possible now, see my answer below: stackoverflow.com/a/79687595/12109043","metadata":{"transformedAt":"2026-08-18T18:32:17.785Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":139,"estimatedTokens":968}}334{"id":"stack-5347042","source":"stackoverflow","questionId":5347042,"title":"Are \"char\" and \"small int\" slower than \"int\"?","tags":["c++","c","compiler-construction","processor"],"text":"Title: Are \"char\" and \"small int\" slower than \"int\"?\nTags: c++, c, compiler-construction, processor\nSource: Stack Overflow\n\nQuestion:\n**Possible Duplicate:**\n\n Performance of built-in types : char vs short vs int vs. float vs. double \n\nHi. Assume, that you have **32-bit** processor. Are 8-bit `char` and 16-bit `short int` types slower than native 32-bit `int`?\nWhat about using 64-bit `long long int`?\n\nAre this datatypes supported by hardware by default, or they are all transformed into 32-bit data anyway, by using additional instructions?\n\nIn case, that I have to store a small amount of chars, isn't it faster to store them as ints?\n\n========================================\n\nTop Answer:\nIt depends on the operations in the instruction set as well as the compiler.\n\n========================================\n\nCode:\n```text\nchar\n```\n\n```text\nshort int\n```\n\n```text\nint\n```\n\n```text\nlong long int\n```\n\n```text\nchar\n```\n\n```text\nint\n```\n\n```text\nlong\n```\n\n```text\nshort\n```\n\n```text\nint\n```\n\n```text\nchar\n```\n\n```text\nshort\n```\n\n```text\nint8_t\n```\n\n```text\nint16_t\n```\n\n========================================\n\nComments:\n- If you're storing a *small amount of chars* why are you bothering with a potential nanosecond difference?\n- @Erik: A single variable could be used millions of times. Your argument doesn't .\n- @R.: And using wider types for chars could increase cache misses. Pointless optimization until the profiler proves otherwise.\n- @Erik: What are cache misses? Just in a few words, I don't need entire definition :) thanks\n- CPUs keep recently used memory location in \"fast RAM\" This RAM is a cache to the much slower main memory.\n- @R.. Don'cha love how Erik changed the subject and addressed a strawman. And what's with people who can't spell your userid.\n- Optimization matters, and learning how to do things the fastest way possible is a good thing! . Please stop with the Premature Pessimization.\n- Stolen directly off the linked duplicate above: Typically, CPUs are fastest at operating on integers of their native word size (with some caveats about 64-bit systems). 32 bit operations are often faster than 8- or 16- bit operations on modern CPUs, but this varies quite a bit between architectures. Also, remember that you can't consider the speed of a CPU in isolation; it's part of a complex system.\n- Even if operating on 16-bit numbers is 2x slower than operating on 32-bit numbers, you can fit twice as much data into the cache hierarchy when you represent it with 16-bit numbers instead of 32-bits. If that makes the difference between having all your data come from cache instead of taking frequent cache misses, then the faster memory access will trump the slower operation of the CPU.\n- Cache matters a lot more then CPU operations if you're iterating over sets of data so I'd wager that the smaller sizes would be faster - cooking up a benchmark now.\n- I can't post code as this is closed, please see my answer on the duplicate: stackoverflow.com/questions/5069489/&hellip;\n- Really? You'd use an int for a boolean instead of a signed char, for example?\n- Of course. Not only is it likely to generate smaller, faster code; it's also more idiomatic. All of the standard C functions that return truth values use `int` as their return type, and the value of all comparison/boolean operators in C has type `int`.\n- I'm too biased toward minimal designs to think that's right. lol. I'll try using this from now on.\n- @Spidey: I recommend against using `int` where you mean `bool` or `char`. Use the type you need.\n- Also note that signedness can matter. Arm for example has had instructions for fast \"load and zero extend\" of bytes for a long time but the support for fast \"load and sign extend\" of bytes was only added in a later version and has more restrictive addressing.","metadata":{"transformedAt":"2026-08-18T18:32:17.786Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":95,"estimatedTokens":951}}335{"id":"stack-27629390","source":"stackoverflow","questionId":27629390,"title":"What is the actual relation between assembly, machine code, bytecode, and opcode?","tags":["c","assembly","compiler-construction","llvm","bytecode"],"text":"Title: What is the actual relation between assembly, machine code, bytecode, and opcode?\nTags: c, assembly, compiler-construction, llvm, bytecode\nSource: Stack Overflow\n\nQuestion:\nWhat is the actual relation between assembly, machine code, bytecode, and opcode?\n\nI have read most of the SO questions about assembly and machine code, such as this, but they are too high level and do not show examples of actual assembly code being transformed into machine code. As a result, I still don't understand how it works at a deeper level.\n\nThe ideal answer to this question would show a specific example of some assembly code, such as the snippet below, and how each assembly instruction gets mapped to machine code, bytecode, and/or opcode. An answer like this would be very helpful to future people learning assembly, because so far in the past few days of digging I haven't found any clear summary.\n\nThe main things I am looking for are:\n\n- **a snippet of assembly code**\n\n- **a snippet of machine code**\n\n- **a mapping between the snippet of assembly and machine code** (how to do that mapping, or at least some general examples, and how do you *know* how to do this, where is all this information on the web)\n\n- **how to interpret the machine code** (like are opcodes somehow related, and where is all the information on the web about what all those numbers *mean*)\n\n*Note: I don't have a computer science background, so I have just been slowly going lower level over the past several years and have now gotten to the point of wanting to understand assembly and machine code.*\n\n**Relation Between Assembly and Machine Code**\n\nMy current understanding is that an \"assembler\" (such as NASM) takes assembly code and creates machine code from it.\n\nSo when you compile some assembly such as this `example.asm`:\n\n```\nglobal main\nsection .text\n\nmain:\n call write\n\nwrite:\n mov rax, 0x2000004\n mov rdi, 1\n mov rsi, message\n mov rdx, length\n syscall\n\nsection .data\nmessage: db 'Hello, world!', 0xa\nlength: equ $ - message\n```\n\n(compile it with `nasm -f macho64 -o example.o example.asm`). It outputs this `example.o` object file:\n\n```\ncffa edfe 0700 0001 0300 0000 0100 0000\n0200 0000 0001 0000 0000 0000 0000 0000\n1900 0000 e800 0000 0000 0000 0000 0000\n0000 0000 0000 0000 0000 0000 0000 0000\n2e00 0000 0000 0000 2001 0000 0000 0000\n2e00 0000 0000 0000 0700 0000 0700 0000\n0200 0000 0000 0000 5f5f 7465 7874 0000\n0000 0000 0000 0000 5f5f 5445 5854 0000\n0000 0000 0000 0000 0000 0000 0000 0000\n2000 0000 0000 0000 2001 0000 0000 0000\n5001 0000 0100 0000 0005 0080 0000 0000\n0000 0000 0000 0000 5f5f 6461 7461 0000\n0000 0000 0000 0000 5f5f 4441 5441 0000\n0000 0000 0000 0000 2000 0000 0000 0000\n0e00 0000 0000 0000 4001 0000 0000 0000\n0000 0000 0000 0000 0000 0000 0000 0000\n0000 0000 0000 0000 0200 0000 1800 0000\n5801 0000 0400 0000 9801 0000 1c00 0000\ne800 0000 00b8 0400 0002 bf01 0000 0048\nbe00 0000 0000 0000 00ba 0e00 0000 0f05\n4865 6c6c 6f2c 2077 6f72 6c64 210a 0000\n1100 0000 0100 000e 0700 0000 0e01 0000\n0500 0000 0000 0000 0d00 0000 0e02 0000\n2000 0000 0000 0000 1500 0000 0200 0000\n0e00 0000 0000 0000 0100 0000 0f01 0000\n0000 0000 0000 0000 0073 7461 7274 0077\n7269 7465 006d 6573 7361 6765 006c 656e\n6774 6800\n```\n\n(that is the entire contents of `example.o`). When you then \"link\" that using `ld -o example example.o`, it gives you more machine code:\n\n```\ncffa edfe 0700 0001 0300 0080 0200 0000\n0d00 0000 7803 0000 8500 0000 0000 0000\n1900 0000 4800 0000 5f5f 5041 4745 5a45\n524f 0000 0000 0000 0000 0000 0000 0000\n0010 0000 0000 0000 0000 0000 0000 0000\n0000 0000 0000 0000 0000 0000 0000 0000\n0000 0000 0000 0000 1900 0000 9800 0000\n5f5f 5445 5854 0000 0000 0000 0000 0000\n0010 0000 0000 0000 0010 0000 0000 0000\n... 523 lines of this\n```\n\nBut how did it go from assembly instructions, to those numbers? Is there some sort of standard reference that lists out all of those numbers, and what they mean, for whatever architecture you are on (I am using x86-64 through NASM on OSX), and how each set of numbers maps to each assembly instruction?\n\n*I understand that machine code is different for every machine, and there are dozens if not hundreds of different types of machines. So I am not currently looking for how assembly gets transformed to every one (that would be complicated). I just am interested in an example that illustrates how the transformation works, and any architecture can serve as the example. And from that point, I could go and research the specific architecture I am interested in and find the mapping.*\n\n**Relation Between Assembly and Bytecode (or is it called \"opcode\"?)**\n\nSo from my reading so far, assembly gets transformed into machine code as demonstrated above.\n\nBut now I get confused. I see people talk about bytecode, such as in this SO answer, showing stuff like this:\n\n```\nvoid myfunc(int a) {\n printf(\"%s\", a);\n}\n```\n\n \n The assembly for this function would look like this:\n\n```\nOP Params OpName Description\n13 82 6a PushString 82 means string, 6a is the address of \"%s\"\n So this function pushes a pointer to \"%s\" on the stack.\n13 83 00 PushInt 83 means integer, 00 means the one on the top of the stack.\n So this function gets the integer at the top of the stack,\n And pushes it on the stack again\n17 13 88 Call 1388 is printf, so this calls the printf function\n03 02 Pop This pops the two things we pushed back off the stack\n02 Return This returns to the calling code.\n```\n\nSo then I get confused. Doing some digging, I can't tell if each of those 2-digit hex numbers like `13 82 6a` are each, individually, called \"opcodes\", and the whole set of them is called \"bytecode\" as a catch-all term. In addition, I can't find a table that lists out all of these 2-digit hex numbers, and what their relation is to machine code, or assembly.\n\nTo summarize, I am very much looking forward to an example showing how assembly instructions map to machine code, and it's relation to bytecode and/or opcode. (I am not looking for how a compiler does this, just how the general mapping works). I think this would clarify it for not only myself but for many people down the road who are interested in learning more about the bare metal.\n\nOne other reason why this would be valuable to know is, so one can understand how the LLVM compiler generates machine code. Do they have some sort of \"complete list\" of 2-digit opcodes or machine code 4-digit sequences, and know exactly how that maps to any architecture-specific assembly? Where did they get that information from? An answer to this overall question would make it much clearer how LLVM implemented its code generation.\n\n**Update**\n\nUpdating from @HansPassant's comment. I actually don't care what the actual distinctions are between the words, sorry if that wasn't clear. I just want to know this: how does assembly map to machine code (and where are places to begin looking for the references that hold that information on the web), and are opcodes or bytecode used anywhere in that process? And if so how?\n\n========================================\n\nTop Answer:\nYou have clearly done some homework of your own on this, and I say good stuff (and voted you up one).\n\nAs you are experiencing, the more you read, the more you say, \"huh ?\"\n\nOkay, first off, when you encounter the word \"bytecode\" just close the window and stop reading, because you are on the wrong path; probably a tangent at best and at worst you could be reading someone trying to sound smarter than he really is by tossing techhy sounding buzzwords into his writing.\n\nNow, as for the word \"opcode\", yes those really do exist, but do understand that those numbers are actually symbolic, for humans to grasp conceptually. In real life, they are super-ultra-tiny switches.\n\nIf you really like history, and technology before the internet (or color TV for that matter) look up phrases like *butterfly switches, vacuum tubes, butterfly girls*, and I forget the other words. This was back before transistors existed. The original huge computers actually used vacuum tubes and generated enough heat to warm an entire floor (or two or three) of an office building in the dead of Winter. The electrical current draws were astounding.\n\nThe thing to keep in your mind about all this is that those computers were \"programmed\" by individually flipping butterfly switches (\"bat handles\" were another term sometimes used) which connected and disconnected individual lines from individual tubes, and I forget what else.\n\nThe facts were: You programmed a computer by flipping the bat handles that were connected to the lines that were connected to various tubes.\n\n### Fast Forward To Today...\n\nWhen you write an opcode of 90h, (I think that's a NOP in x86, somebody correct me and I'll fix it) you are doing (with today's hi-tech wowee-zowee) the same thing that the butterfly girls did back in the stone age of computers.\n\nSpecifically, you are \"throwing\" these \"butterfly switches\"...\n\n- 7 - ON\n\n- 6 - OFF\n\n- 5 - OFF\n\n- 4 - ON\n\n- 3 - OFF\n\n- 2 - OFF\n\n- 1 - OFF\n\n- 0 - OFF\n\nHere's the big difference (and part of today's hi-tech wowee-zowee)...\n\n*They had to throw exactly those switches at exactly one place on the floor. You will be flipping them anywhere you want. Three other programs will cooperate and make those decisions for you.*\n\nThose three programs are\n- The Assembler\n- The Linker\n- The Loader\n\nSo then (I hope) that this has helped lay the foundation for you to understand that the **OPCODE** is a mental representation of a bunch of little switches that will be \"opened\" or \"closed\".\n\n(Actually, the hi-tech wowee-zowee has taken it a step further, but it's the same effect as the butterfly switches of previous gnerations.)\n\nAnyway, it works like this.\n\nHumans decided that there would be an instruction to do nothing; called a `NOP`\n\nSo, you type the letters `NOP` in your text editor like this\n\n```\nNOP ;This is a No operation instruction\n```\n\nYou then save the file.\n\nYou then ask the assembler to assemble that file\n\nWhen the assembler sees the `NOP` he creates the `90` (in hex) in the **Object** file which he is creating for the linker.\n\nThe Linker uses the object file and creates an **executable** file \n\nThe Loader places that executable file wherever it wants. (Note, in olden days of microcomputers, the software writer had to decide where to place that executable file; that was conflict bait like you wouldn't believe.)\n\nAnyway, the `NOP` became `90` in some place in the `EXE` file and the loader stuck it in a good area for you, based on 179 rules you don't have to worry about any longer. \n\nThe loader then gets out of the picture and lets your program have the CPU.\n\nThe CPU fetches your first instruction and starts obeying.\n\nWhen the CPU gets to the byte containing `90` it will be the same thing as the butterfly switches from generations past.\n\nWhile the current will not be traveling a bunch of long wires on the floor, it will be doing highly similar (and functionally equivalent) things inside the ASIC.\n\nNow with all that written (thanks if you're still actually reading) you can understand this boiled down one line explanation of what an opcode actually is...\n\n### The opcode is a paradigmatic representation of butterfly switches of olden days.\n\nNow for your second question about what is machine code.\n\n### Machine code is a bunch of opcodes\n\nIf any of this is unclear, ask in the comments section and I'll try to edit this answer.\n\n========================================\n\nCode:\n```text\nglobal main\nsection .text\n\nmain:\n  call write\n\nwrite:\n  mov rax, 0x2000004\n  mov rdi, 1\n  mov rsi, message\n  mov rdx, length\n  syscall\n\nsection .data\nmessage: db 'Hello, world!', 0xa\nlength: equ $ - message\n```\n\n```text\ncffa edfe 0700 0001 0300 0000 0100 0000\n0200 0000 0001 0000 0000 0000 0000 0000\n1900 0000 e800 0000 0000 0000 0000 0000\n0000 0000 0000 0000 0000 0000 0000 0000\n2e00 0000 0000 0000 2001 0000 0000 0000\n2e00 0000 0000 0000 0700 0000 0700 0000\n0200 0000 0000 0000 5f5f 7465 7874 0000\n0000 0000 0000 0000 5f5f 5445 5854 0000\n0000 0000 0000 0000 0000 0000 0000 0000\n2000 0000 0000 0000 2001 0000 0000 0000\n5001 0000 0100 0000 0005 0080 0000 0000\n0000 0000 0000 0000 5f5f 6461 7461 0000\n0000 0000 0000 0000 5f5f 4441 5441 0000\n0000 0000 0000 0000 2000 0000 0000 0000\n0e00 0000 0000 0000 4001 0000 0000 0000\n0000 0000 0000 0000 0000 0000 0000 0000\n0000 0000 0000 0000 0200 0000 1800 0000\n5801 0000 0400 0000 9801 0000 1c00 0000\ne800 0000 00b8 0400 0002 bf01 0000 0048\nbe00 0000 0000 0000 00ba 0e00 0000 0f05\n4865 6c6c 6f2c 2077 6f72 6c64 210a 0000\n1100 0000 0100 000e 0700 0000 0e01 0000\n0500 0000 0000 0000 0d00 0000 0e02 0000\n2000 0000 0000 0000 1500 0000 0200 0000\n0e00 0000 0000 0000 0100 0000 0f01 0000\n0000 0000 0000 0000 0073 7461 7274 0077\n7269 7465 006d 6573 7361 6765 006c 656e\n6774 6800\n```\n\n```text\ncffa edfe 0700 0001 0300 0080 0200 0000\n0d00 0000 7803 0000 8500 0000 0000 0000\n1900 0000 4800 0000 5f5f 5041 4745 5a45\n524f 0000 0000 0000 0000 0000 0000 0000\n0010 0000 0000 0000 0000 0000 0000 0000\n0000 0000 0000 0000 0000 0000 0000 0000\n0000 0000 0000 0000 1900 0000 9800 0000\n5f5f 5445 5854 0000 0000 0000 0000 0000\n0010 0000 0000 0000 0010 0000 0000 0000\n... 523 lines of this\n```\n\n```text\nvoid myfunc(int a) {\n  printf(\"%s\", a);\n}\n```\n\n```text\nOP Params OpName     Description\n13 82 6a  PushString 82 means string, 6a is the address of \"%s\"\n                     So this function pushes a pointer to \"%s\" on the stack.\n13 83 00  PushInt    83 means integer, 00 means the one on the top of the stack.\n                     So this function gets the integer at the top of the stack,\n                     And pushes it on the stack again\n17 13 88 Call        1388 is printf, so this calls the printf function\n03 02    Pop         This pops the two things we pushed back off the stack\n02       Return      This returns to the calling code.\n```\n\n```text\nexample.asm\n```\n\n```text\nnasm -f macho64 -o example.o example.asm\n```\n\n```text\nexample.o\n```\n\n```text\nexample.o\n```\n\n```text\nld -o example example.o\n```\n\n```text\n13 82 6a\n```\n\n```text\n1                                  global main\n 2                                  section .text\n 3\n 4                                  main:\n 5 00000000 E800000000                call write\n 6\n 7                                  write:\n 8 00000005 B804000002                mov rax, 0x2000004\n 9 0000000A BF01000000                mov rdi, 1\n10 0000000F 48BE-                     mov rsi, message\n11 00000011 [0000000000000000]\n12 00000019 BA0E000000                mov rdx, length\n13 0000001E 0F05                      syscall\n14\n15                                  section .data\n16 00000000 48656C6C6F2C20776F-     message: db 'Hello, world!', 0xa\n17 00000009 726C64210A\n18                                  length: equ $ - message\n```\n\n```text\nnasm\n```\n\n```text\nnasm -l\n```\n\n```text\nB804000002  mov rax, 0x2000004\n```\n\n```text\nB8\n```\n\n```text\n04000002\n```\n\n```text\nsyscall\n```\n\n```text\nsyscall\n```\n\n```text\n0F 05\n```\n\n```text\nAppendix A: Opcode map\n```\n\n```text\nA.1\n```\n\n```text\nFor 2-byte opcodes beginning with 0FH (Table A-3), skip any instruction prefixes, the 0FH byte (0FH may be preceded by 66H, F2H, or F3H) and use the upper and lower 4-bit values of the next opcode byte to index table rows and columns.\n```\n\n```text\n0F\n```\n\n```text\n05\n```\n\n```text\n0\n```\n\n```text\n5\n```\n\n```text\nA-3\n```\n\n```text\nsyscall\n```\n\n```text\n13\n```\n\n```text\n13\n```\n\n```text\nPushString\n```\n\n```text\n13\n```\n\n```text\n13\n```\n\n```text\npushAnything\n```\n\n```text\nAssembler instruction (readable) ->  machine code (binary) \n\nmachine code = opcode + operands\n```\n\n```text\nmov rax, 0x2000004\n```\n\n```text\nMOV reg32, immediate value\n```\n\n```text\nB8\n```\n\n```text\n04 00 00 02\n```\n\n```text\nNOP           ;This is a No operation instruction\n```\n\n```text\nNOP\n```\n\n```text\nNOP\n```\n\n```text\nNOP\n```\n\n```text\n90\n```\n\n```text\nNOP\n```\n\n```text\n90\n```\n\n```text\nEXE\n```\n\n```text\n90\n```\n\n========================================\n\nComments:\n- A good introduction can be read here **Art of Assembly - Encoding x86 Instructions**. This is for older CPU's but 100% applicable for general discussion. And yes, there are some crazy sites out there that map all opcode for each CPU.\n- Focusing on what the *words* mean without having any idea how a processor works will not give you any insight. Get a book, write some assembly code, learn more. There's nothing we can do to hurry that along, nor should we. Be ready to commit a year or two.\n- @HansPassant I actually don't care what the actual distinctions are between the words, sorry if that wasn't clear (I'll add to the question). I just want to know this: how does assembly map to machine code (and where are places to begin looking for the references that hold that information on the web), and are opcodes or bytecode used anywhere in there? And if so how?\n- Before you guys close the question, can you tell me how I can write a better one? I really am interested in seeing the relation between assembly/machine-code, and am not finding anything good that is helping :(. I will study that Art of Assembly section, thanks @DavidC.Rankin that's just a few sections ahead of where I'm currently at in the book haha.\n- Lance, I found a number of sites when I did this same exercise (I ended up about where you are). Here are a couple **x86 Machine Code** (see x86 Opcodes) and **X86 Opcode and Instruction Reference**. There is a lot of information to drink in, but basically, the assembly compiler's job is to do exactly what you are asking. It is CPU dependent and will vary by compiler.\n- Note that a *very large* part of your hex dumps are just linker and executable \"meta\" information. The actually executed part is extremely small (but that's because your sample program is small, not \"per definition\").\n- Related: my answer at stackoverflow.com/a/27611667/2564301\n- You're asking the wrong question. Buy a book or take a course on computer architecture. When you understand how processors work, assembly and machine code will all be self-evident. You'll need nothing more than a reference manual for the processor in question to understand how assembly relates to machine code.\n- @CareyGregory this is the type of question people will have that don't have a computer science degree. School teaches you backwards, starting with the super abstract \"machine architecture\", and after years and years you finally get to JavaScript and building a web app? Haha, should be the reverse, and will be the reverse, as more and more non-computer-science majors learn how to code.\n- \"Opcode is generally considered to be the part of the machine code instruction that specifies the operation to perform. For example, in the above code you have B804000002 mov rax, 0x2000004. There B8 is the opcode, 04000002 is the immediate operand.\" How do you know that `B8` is the opcode, and the rest is the immediate operand? Sorry if that's an obvious question, but this is the first time I've seen it :) Can you explain how/where to learn about that?\n- The difference between *bytecode* and actual assembly is that assembly can run directly on a CPU, and bytecode always needs an interpreter. (Where a plus point for bytecode is that it can run on all possible CPUs, if an interpreter is available. A minus would be it's slightly slower.)\n- @Lance: that's where you need to take a look in the Intel docs. All opcodes have at least one defining byte, some have more (I think the current max is 12). Everything else is 'data'.\n- You have to read the pdf reference. If you are going from assembly to machine code, you just look up the appropriate instruction in the doc. For this example, it says `B8+rd id MOV r32, imm32`. To understand the format you have to read `CHAPTER 2 INSTRUCTION FORMAT`.\n- Thank you for that walkthrough, that is very helpful.\n- This also is a good reference for newcomers - x86asm.net/articles/x86-64-tour-of-intel-manuals\n- Ah, that clarifies the relation between all the 4 pieces (assembly, machine code, bytecode, and opcode). I see now. Thanks! Now wondering more about the details (illustrative examples), updated the question with a list at the top to make it clearer.\n- Technically, what you describe is a SYMBOLIC assembler. The original \"assembler\" code was just numbers.\n- @HotLicks Historically speaking, the first assembler was already mnemonic based, and the term “assembler” was then related to the assembling fields (operation and operand) into a (binary) instruction word (see en.m.wikipedia.org/wiki/&hellip;). But I agree that all modern assemblers are now symbolic. They all manage symbols for labels and addresses, whereas in early times, those had to be hard coded.\n- @Christophe - No, the original \"assembler\" assembled numeric opcodes into the form that was read by the machine. This is the origin of the term. Symbolic assemblers (such as SAP) were a revolutionary development, greatly increasing programmer productivity.\n- The 'pack of cards' are still in use for mechanized organs and pianolas :) In some sense, they *are* \"the program\", literally turning cranks and wheels -- I imagine the computers of yore were pretty much the same. But the punch codes got abstracted, first into numbers (literally, the opcodes) and then into descriptive names.\n- @Jongware - The first step toward modern computers was replacing the external program with one stored inside the computer memory. This was Von Neumann's famous suggestion to Eckert and Mauchly (and why a stored program computer is called a \"Von Neumann machine\").\n- (And the step between the Jacquard loom scheme and the internally programmed computer was the wire-programmed computer -- plug panels containing hundreds of wires were pre-programmed by ladies (being an activity beneath that of the men running the show), then plugged into the computer when a new \"program\" was to be run.)\n- Yup. A logical extension from the punch card idea is storing the cards inside the machine and adding a mechanism that can wind the set of cards forwards and backwards, and fill holes ('erase') and punch new ones ('write'). (Perhaps my \"logical\" seems only so in retrospect...)\n- It should be noted that the output of an assembler, into which you fed all those cards, was another deck of punched cards, though this one with meaningless (to the average human) numeric codes, ready to be read into the program memory.\n- Could you please write a book about programming from the ground up?\n- @MaxHerrmann If you are the guy at CMA CGM Germany, I just looked at your LinkedIN profile. Thanks for your encouragement\n- Wow, very interesting explaination.Do you have that book yet ? haha\n- \"Assembly\" is human readable only if SYMBOLIC assembler. Bytecode may be used as an an \"intermediate code\", fed into another compiler/assembler to produce some form of machine code. Not all computers utilize microcode, and even those that do may not utilize it for the \"hottest\" operations.","metadata":{"transformedAt":"2026-08-18T18:32:17.786Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":49,"totalLines":539,"estimatedTokens":5722}}336{"id":"stack-475915","source":"stackoverflow","questionId":475915,"title":"Would there be any point in designing a CPU that could handle IL directly?","tags":["c#","performance","compiler-construction","cpu","cpu-architecture"],"text":"Title: Would there be any point in designing a CPU that could handle IL directly?\nTags: c#, performance, compiler-construction, cpu, cpu-architecture\nSource: Stack Overflow\n\nQuestion:\nIf I understand this correctly:\n\nCurrent CPU developing companies like AMD and Intel have their own API codes (the assembly language) as what they see as the 2G language on top of the Machine code (1G language) \n\nWould it be possible or desirable (performance or otherwise) to have a CPU that would perform IL handling at it's core instead of the current API calls?\n\n========================================\n\nTop Answer:\nYou seem a bit confused about how CPU's work. Assembly is not a separate language from machine code. It is simply a different (textual) representation of it.\n\nAssembly code is simply a sequential listing of instructions to be executed. And machine code is exactly the same thing. Every instruction supported by the CPU has a certain bit-pattern that cause it to be executed, and it also has a textual name you can use in assembly code.\n\nIf I write `add $10, $9, $8` and run it through an assembler, I get the machine code for the add instruction, taking the values in registers 9 and 8, adding them and storing the result in register 10. \n\nThere is a 1 to 1 mapping between assembler and machine code.\n\nThere also are no \"API calls\". The CPU simply reads from address X, and matches the subsequent bits against all the instructions it understands. Once it finds an instruction that matches this bit pattern, it executes the instruction, and moves on to read the next one.\n\nWhat you're asking is in a sense impossible or a contradiction. IL stands for Intermediate Language, that is, a kind of pseudocode that is emitted by the compiler, but has not yet been translated into machine code. But if the CPU could execute that directly, then it would no longer be intermediate, it would *be* machine code.\n\nSo the question becomes \"is your IL code a better, more efficient representation of a program, than the machine code the CPU supports now?\"\n\nAnd the answer is most likely no. MSIL (I assume that's what you mean by IL, which is a much more general term) is designed to be portable, simple and consistent. Every .NET language compiles to MSIL, and every MSIL program must be able to be translated into machine code for any CPU anywhere. That means MSIL must be general and abstract and not make assumptions about the CPU. For this reason, as far as I know, it is a purely stack-based architecture. Instead of keeping data in registers, each instruction processes the data on the top of the stack. That's a nice clean and generic system, but it's not very efficient, and doesn't translate well to the rigid structure of a CPU. (In your wonderful little high-level world, you can pretend that the stack can grow freely. For the CPU to get fast access to it, it must be stored in some small, fast on-chip memory with finite size. So what happens if your program push too much data on the stack?)\n\nYes, you *could* make a CPU to execute MSIL directly, but what would you gain?\nYou'd no longer need to JIT code before execution, so the first time you start a program, it would launch a bit faster. Apart from that, though? Once your MSIL program has been JIT'ed, it *has* been translated to machine code and runs as efficiently as if it had been written in machine code originally. MSIL bytecode no longer exists, just a series of instructions understood by the CPU.\n\nIn fact, you'd be back where you were before .NET. Non-managed languages are compiled straight to machine code, just like this would be in your suggestion. The only difference is that non-managed code targets machine code that is designed by CPU designers to be suitable for execution on a CPU, while in your case, it'd target machine code that's designed by software designers to be easy to translate to and from.\n\n========================================\n\nCode:\n```text\nadd $10, $9, $8\n```\n\n========================================\n\nComments:\n- +1. Good point, if there were benefits to this approach we would have seen Java byte code processors make more of a splash.\n- I think Chuck Moore actually made quite a bit of money with his FORTH chips.\n- In the old days there were CISC and RISC processors, now some of the CISC chips compile their instructions down to RISC internally. I think this is what the OP is refering to.\n- Yeah, but once again, what would be the point? You'd lose the flexibility/portability that JIT'ed bytecode offers, and you'd require the CPU to do more work for every instruction, just to save a bit of work up front during JIT-compilation.","metadata":{"transformedAt":"2026-08-18T18:32:17.786Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":49,"estimatedTokens":1160}}337{"id":"stack-15084162","source":"stackoverflow","questionId":15084162,"title":"Does Haskell optimizer utilize memoization for repeated function calls in a scope?","tags":["haskell","optimization","compiler-construction","memoization"],"text":"Title: Does Haskell optimizer utilize memoization for repeated function calls in a scope?\nTags: haskell, optimization, compiler-construction, memoization\nSource: Stack Overflow\n\nQuestion:\nConsider this function:\n\n```\nf as = if length as > 100 then length as else 100\n```\n\nSince the function is pure it's obvious that the length will be the same in both calls. My question is does Haskell optimizer turn the code above into equivalent of the following?\n\n```\nf as = \n let l = length as\n in if l > 100 then l else 100\n```\n\nIf it does, then which level setting enables it? If it doesn't, then why? In this scenario a memory waste can't be the reason as explained in this answer, because the introduced variable gets released as soon as the function execution is finished. \n\nPlease note that this is not a duplicate of this question because of the local scope, and thus it may get a radically different answer.\n\n========================================\n\nTop Answer:\nEven in such a local setting, it is still the case that it is not obvious that the introduction of sharing is always an optimization. Consider this example definition\n\n```\nf = if length [1 .. 1000000] > 0 then head [1 .. 1000000] else 0\n```\n\nvs. this one\n\n```\nf = let xs = [1 .. 1000000] in if length xs > 0 then head xs else 0\n```\n\nand you'll find that in this case, the first behaves much better, as each of the computations performed on the list is cheap, whereas the second version will cause the list to be unfolded completely in memory by `length`, and it can only be discarded after `head` has been reduced.\n\n========================================\n\nCode:\n```text\nf as = if length as > 100 then length as else 100\n```\n\n```text\nf as = \n  let l = length as\n  in if l > 100 then l else 100\n```\n\n```text\n-fcse\n```\n\n```text\nf = if length [1 .. 1000000] > 0 then head [1 .. 1000000] else 0\n```\n\n```text\nf = let xs = [1 .. 1000000] in if length xs > 0 then head xs else 0\n```\n\n```text\nlength\n```\n\n```text\nhead\n```\n\n========================================\n\nComments:\n- Despite this problem, ghc could be much more aggressive with CSE. You just have to have a size estimate of the value you are CSEing. A simple estimate is that base types take up negligable space.\n- How is `length [1 .. 1000000] > 0` a cheap operation? Won't \"length\" have to return before \">\" is evaluated? (In ghci, the operation is slowed down noticably when I increase the size of the list)\n- @salty-horse Cheap in terms of space: constant space, linear time.","metadata":{"transformedAt":"2026-08-18T18:32:17.786Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":79,"estimatedTokens":624}}338{"id":"stack-4032147","source":"stackoverflow","questionId":4032147,"title":"Does Java include comments on the compiled code?","tags":["java","compiler-construction"],"text":"Title: Does Java include comments on the compiled code?\nTags: java, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nWell, the title is self-explanatory. I wondered this while compiling a project which has a lot of lines commented. It's a bit silly because the jar file wouldn't increase much (some bytes) but I'm curious if this could affect a program with a lot of code and comments.\n\nThanks\n\n========================================\n\nTop Answer:\nNo, comments normally are stripped out in any language (not just in Java). They have no representative in byte code. What stays in there, is annotated stuff and such\n\n========================================\n\nCode:\n```text\njavadoc\n```\n\n========================================\n\nComments:\n- Not all of them :-) In PHP you can actually reflect over the comments of a class or method. See php.net/manual/en/reflectionclass.getdoccomment.php.\n- Well, PHP is an interpreted language. So your code is not compiled like in Java, C#, C++, ... If you use something like eAccelerator which stored the code in their compiled representative, you won't be able to use this functionality, as the comments got stripped out\n- Thanks all for the answers. I'm just doing some basic stuff without using subversion or anything, but when I use such tools I will remember your advice!\n- The title made me curious about its content. The original was deleted, no idea why. A link to *Do you leave historical code commented out in classes that you update?* can be found here.\n- The link to \"this question\" no longer functions.\n- @mikebabcock looks like the other question is closed now. Thanks for letting me know.\n- @XXL Commented code: if the code is commented, it means that it's not used anymore. So why keep it? (you can find it again with your SCM, that's its purpose). Dead code: same remark: if not used why keeping it?","metadata":{"transformedAt":"2026-08-18T18:32:17.786Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":31,"estimatedTokens":465}}339{"id":"stack-13953834","source":"stackoverflow","questionId":13953834,"title":"How does a compiler know the alignment of a physical address?","tags":["c++","compiler-construction","alignment","memory-alignment"],"text":"Title: How does a compiler know the alignment of a physical address?\nTags: c++, compiler-construction, alignment, memory-alignment\nSource: Stack Overflow\n\nQuestion:\nI know that some CPU architectures don't support unaligned address access(e.g., ARM architectures prior to ARM 4 had no instructions to access half-word objects in memory). And some compiler(e.g., some version of GCC) for that architecture will use a series of memory access when it finds a misaligned address, so that the misaligned access is almost transparent to developers.(Refer to The Definitive Guide to GCC, By William von Hagen)\n\nBut I'm wondering how does a compiler know whether an address is aligned or not? After all, what a compiler sees is the virtual address(effective address, EA), if it can see anything. When the program is run, EA could be mapped to any physical address by OS. Even if virtual address is aligned, the resulting physical address could be misaligned, isn't it? The alignment of physical address is what really matters and transfers on CPU address lines.\n\nBecause a compiler is not aware of the physical address at all, how can it be smart enough to know if a variable's address is aligned?\n\n========================================\n\nTop Answer:\nAlignment is a very useful attribute for object code, partly because some machines insist on \"aligned access\" but in modern computers because cache lines have huge impact on performance and thus cache-alignment of code/loops/data/locks is thus a requirement from your local friendly compiler.\n\nVirtally all the loaders in the world support loading of code at power-of-two aligned boundaries of some modest size and on up. (Assemblers and linkers support this too with various ALIGNMENT directives). Often linkers and loaders just align the first loaded value anyway to a well-known boundary size; OSes with virtual memory often provide a convenient boundary based on VM page size (ties to other answer).\n\nSo a compiler can essentially know what the alignment of its emitted code/data is. And by keeping track of how much code it has emitted, it can know what the alignment of any emitted value is. If it needs alignment, it can issue a linker directive, or for modest sizes, simply pad until the emitted amount of code is suitably aligned. \n\nBecause of this, you can be pretty sure most compilers will not place code or data constructs in ways that cross cache line (or other architecture imposed) boundaries in a way that materially affects performance unless directed to do so.\n\n========================================\n\nComments:\n- Aha, simple, and effective.\n- wait a min. What about a DLL relocation? The base address of a module could be changed. Does that also guarantee some alignment of the base address after relocation? Do you know that?\n- On windows DLLs are loaded in a manner where it's aligned to at-least 512, see SectionAlignment and FileAlignment.","metadata":{"transformedAt":"2026-08-18T18:32:17.786Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":28,"estimatedTokens":728}}340{"id":"stack-6977177","source":"stackoverflow","questionId":6977177,"title":"W3C CSS grammar, syntax oddities","tags":["css","compiler-construction","programming-languages","grammar"],"text":"Title: W3C CSS grammar, syntax oddities\nTags: css, compiler-construction, programming-languages, grammar\nSource: Stack Overflow\n\nQuestion:\nI was having a look at the CSS syntax here and here and I was amazed to see both the token productions and the grammar littered with whitespace declarations. Normally whitespace is defined once in the lexer and skipped, never to be seen again. Ditto comments.\n\nI imagine the orientation towards user-agents rather than true compilers is part of the motivation here, and also the requirement to proceed in the face of errors, but it still seems pretty odd.\n\nAre real-life UAs that parse CSS really implemented according to this (these) grammars?\n\nEDIT: reason for the question is actually the various LESS implementations. `less.js` doesn't understand consecutive comments, and `lessc.exe` doesn't understand comments inside selectors. In this respect they are not even able to parse CSS correctly, however that is defined. So I went to see what the actual grammar of CSS was and ...\n\n========================================\n\nCode:\n```text\nless.js\n```\n\n```text\nlessc.exe\n```\n\n```text\n<html>\n    <head>\n        <style type=\"text/css\">\n            .blueborder { width:200px; height:200px; border: solid 2px #00f; }\n            .redborder  { width:100px; height:100px; margin:50px; border: solid 2px #f00; }\n        </style>\n    </head>\n\n    <body>\n        <div class=\"blueborder\">\n            <div class=\"redborder\"></div>\n        </div>\n    </body>\n\n</html>\n```\n\n```text\n<div class=\"blueborder MyClass\" id=\"MyDiv\">\n    <div class=\"redborder\"></div>\n</div>\n```\n\n```text\n.MyClass#MyDiv { background: #ccc; }\n```\n\n```text\n.MyClass #MyDiv { background: #ccc; }\n```\n\n```text\n.MyClass#MyDiv -> DELIM IDENT HASH\n```\n\n```text\n.MyClass #MyDiv -> DELIM IDENT S HASH\n```\n\n```text\n#\n```\n\n```text\n.\n```\n\n========================================\n\nComments:\n- You know how IE has strange bugs with whitespace and comments? Yeah.\n- Oh and I removed the [programming-languages] tag because CSS isn't one. But, never mind, I'll let you keep it.\n- @BoltClock that seems pretty pedantic to me and I am a compiler writer, pedantic by nature and training. CSS is a language used in association with computers, and it has many of the things that programming languages have, including in this case a formal grammar which can be implemented via the same tools that are used for 'real' programming languages. Which is the whole point of the question actually.","metadata":{"transformedAt":"2026-08-18T18:32:17.786Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":10,"totalLines":78,"estimatedTokens":618}}341{"id":"stack-41236266","source":"stackoverflow","questionId":41236266,"title":"Can I translate an AST to SSA, or do I need to translate to a CFG then to SSA?","tags":["compiler-construction","control-flow","control-flow-graph","ssa"],"text":"Title: Can I translate an AST to SSA, or do I need to translate to a CFG then to SSA?\nTags: compiler-construction, control-flow, control-flow-graph, ssa\nSource: Stack Overflow\n\nQuestion:\nCan I translate an Abstract Syntax Tree directly into SSA form, or will I need to create a control flow graph and then create the Static Single Assignment form from said CFG?\n\nAnd in the context of a control flow graph: how do I represent this for a c-like program? I'm thinking I could store a graph of the CFG for all the basic blocks in every function, but then when I call a function for example, this may complicate things. Another way I can think of is a CFG for the entire program, i.e. all the source files, but then how would I store information about functions? Could I maybe store a pointer to the function in the basic block (i.e. the parent node)?\n\nIf I am generating SSA from a CFG, do I need to worry about having a CFG that represents the control flow of statements? I'm thinking I would only need to represent the basic block control flow.\n\n========================================\n\nComments:\n- I just finished reading the article-- it's great! I'm just wondering whether it constructs minimal or pruned SSA because sometimes it says \"pruned\", other times, \"minimal\", and most of the time \"minimal and pruned\".\n- Is the algorithm really cfg-less? Seems to me that the one presented in the article operates on a cfg.\n- I don't think this is CFG-less at all, even though the abstract says it can go from bytecode directly to SSA. From the paper: \"the algorithm presented in this paper is the first to construct minimal and pruned SSA on reducible CFGs\" and \"First, we consider a single basic block. Then, we extend the algorithm to whole CFGs. Finally, we show how to handle incomplete CFGs, which usually emerge when translating an AST to IR.\" None of those statements are about going from bytecode or linear instructions to SSA.\n- It goes from an AST (for example, but could also be bytecode) to a new CFG which is being constructed on the fly. The paper only really talks about what happens when reading from and writing to local variables and assumes the CFG construction is happening on the side","metadata":{"transformedAt":"2026-08-18T18:32:17.786Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":18,"estimatedTokens":551}}342{"id":"stack-256629","source":"stackoverflow","questionId":256629,"title":"Constructing a simple interpreter","tags":["c#","architecture","compiler-construction","programming-languages"],"text":"Title: Constructing a simple interpreter\nTags: c#, architecture, compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nI’m starting a project where I need to implement a light-weight interpreter.\nThe interpreter is used to execute simple scientific algorithms.\nThe programming language that this interpreter will use should be simple, since it is targeting non- software developers (for example, mathematicians.)\n\nThe interpreter should support basic programming languages features:\n\n- Real numbers, variables, multi-dimensional arrays\n\n- Binary (+, -, *, /, %) and Boolean (==, !=, , =) operations\n\n- Loops (for, while), Conditional expressions (if)\n\n- Functions\n\nMathWorks MatLab is a good example of where I’m heading, just much simpler.\nThe interpreter will be used as an environment to demonstrate algorithms; simple algorithms such as finding the average of a dataset/array, or slightly more complicated algorithms such as Gaussian elimination or RSA.\n\nBest/Most practical resource I found on the subject is Ron Ayoub’s entry on Code Project (Parsing Algebraic Expressions Using the Interpreter Pattern) - a perfect example of a minified version of my problem.\n\nThe Purple Dragon Book seems to be too much, anything more practical?\n\nThe interpreter will be implemented as a .NET library, using C#. However, resources for any platform are welcome, since the design-architecture part of this problem is the most challenging.\n\nAny practical resources?\n\n(please avoid “this is not trivial” or “why re-invent the wheel” responses)\n\n========================================\n\nTop Answer:\nIt might sound odd, but Game Scripting Mastery is a great resource for learning about parsing, compiling and interpreting code.\n\nYou should really check it out:\n\nhttp://www.amazon.com/Scripting-Mastery-Premier-Press-Development/dp/1931841578\n\n========================================\n\nComments:\n- Outstanding book! The scripting language described in the book is implemented in C# at codeproject.com/KB/cs/Conscript.aspx\n- The funny thing is that Alex Varanese and I go way way back, we used to talk on irc in our young teens. Interesting to see that he is an author now. He was a master of x86 assembly.","metadata":{"transformedAt":"2026-08-18T18:32:17.786Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":46,"estimatedTokens":554}}343{"id":"stack-4218721","source":"stackoverflow","questionId":4218721,"title":"Optimizing Java compiler during a Maven build?","tags":["java","performance","maven-2","compiler-construction","build"],"text":"Title: Optimizing Java compiler during a Maven build?\nTags: java, performance, maven-2, compiler-construction, build\nSource: Stack Overflow\n\nQuestion:\nI have a Maven profile for a Java project that is activated when doing a final build on a Hudson CI server.\n\nCurrently this profile's only customization is to the Maven compiler plugin as follows:\n\n```\n\n org.apache.maven.plugins\n maven-compiler-plugin\n \n false\n true\n \n \n```\n\nAre there any other tweaks or optimizations to the Java compiler that a final build should be doing to maximize performance?\n\n========================================\n\nTop Answer:\nYou shouldn't even do that - optimization in `javac` has been disabled for quite a while, IIRC. Basically the JIT is responsible for almost all the optimization, and the javac optimizations actually *hurt* that in some cases.\n\nIf you're looking to tune performance you should look elsewhere:\n\n- Your actual code\n\n- VM options (e.g. GC tuning)\n\n========================================\n\nCode:\n```text\n<plugin>\n                    <groupId>org.apache.maven.plugins</groupId>\n                    <artifactId>maven-compiler-plugin</artifactId>\n                    <configuration>\n                        <debug>false</debug>\n                        <optimize>true</optimize>\n                    </configuration>\n                </plugin>\n```\n\n```text\n<source>1.6</source>\n<target>1.6</target>\n```\n\n```text\n<optimize>true</optimize>\n```\n\n```text\n<debug>false</debug>\n```\n\n```text\njavac\n```\n\n```text\n<debug>false</debug>\n```\n\n```text\n<debuglevel/>\n```\n\n```text\n-g\n```\n\n========================================\n\nComments:\n- I would not recommend optimizing unless performance is an issue, especially since the JIT really is very good.\n- @extraneon: You've missed my point - optimization actively *hurt* performance, so basically it was removed.\n- I actually meant code optimizing and GC optimizing. Code optimization may result in less readable code, and GC options may be surprising to the uninformed.\n- @extraneon: Right, yes. I think it's worth doing *architectural* optimization early, but not low-level micro-optimization.\n- Are you saying that leaving the debug symbols in there doesn't hurt performance? Will do on the v1.6 settings.\n- @HDave - debugging information is stored in the LineNumberTable and LocalVariableTable in the .class file (see java.sun.com/docs/books/jvms/second_edition/html/&hellip;). While this may consume additional memory, it is separate from the bytecode, and therefore does not directly affect execution performance.\n- Hmm, I'm still seeing line numbers in my stack traces with `false`...\n- I tried this out and have a -up question on this: stackoverflow.com/questions/4220083/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.786Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":87,"estimatedTokens":680}}344{"id":"stack-32384901","source":"stackoverflow","questionId":32384901,"title":"Atomic operations, std::atomic<> and ordering of writes","tags":["c++","assembly","compiler-construction","x86"],"text":"Title: Atomic operations, std::atomic<> and ordering of writes\nTags: c++, assembly, compiler-construction, x86\nSource: Stack Overflow\n\nQuestion:\nGCC compiles this: \n\n```\n#include \nstd::atomic a; \nint b(0);\n\nvoid func()\n{\n b = 2; \n a = 1;\n}\n```\n\nto this: \n\n```\nfunc():\n mov DWORD PTR b[rip], 2\n mov DWORD PTR a[rip], 1\n mfence\n ret\n```\n\nSo, to clarify things for me: \n\n- Is any other thread reading ‘a’ as 1 guaranteed to read ‘b’ as 2.\n\n- Why does the MFENCE happen after the write to ‘a’ not before.\n\n- Is the write to ‘a’ guaranteed to be an atomic (in the narrow, non C++ sense) operation anyway, and does that apply for all intel processors? I assume so from this output code.\n\nAlso, clang (v3.5.1 -O3)does this: \n\n```\nmov dword ptr [rip + b], 2\nmov eax, 1\nxchg dword ptr [rip + a], eax\nret\n```\n\nWhich appears more straightforward to my little mind, but why the different approach, what’s the advantage of each?\n\n========================================\n\nCode:\n```text\n#include <atomic>\nstd::atomic<int> a; \nint b(0);\n\nvoid func()\n{\n  b = 2; \n  a = 1;\n}\n```\n\n```text\nfunc():\n    mov DWORD PTR b[rip], 2\n    mov DWORD PTR a[rip], 1\n    mfence\n    ret\n```\n\n```text\nmov dword ptr [rip + b], 2\nmov eax, 1\nxchg    dword ptr [rip + a], eax\nret\n```\n\n```text\n# get pointers and constants into registers\nstr r1, [r3]     # store b=2\ndmb sy           # Data Memory Barrier: full memory barrier to order the stores.\n   #  I think just a StoreStore barrier here (dmb st) would be sufficient, but gcc doesn't do that.  Maybe later versions have that optimization, or maybe I'm wrong.\nstr r2, [r3, #4] # store a=1  (a is 4 bytes after b)\ndmb sy           # full memory barrier to order this store wrt. all following loads and stores.\n```\n\n```text\na+=b\n```\n\n```text\na.store(1, memory_order_release);\n```\n\n```text\na = 1;\n```\n\n```text\na\n```\n\n```text\nint64_t\n```\n\n```text\nb\n```\n\n```text\nstd::atomic\n```\n\n```text\na+=3\n```\n\n```text\na += 3000\n```\n\n```text\na\n```\n\n```text\n</rant>\n```\n\n```text\nstd::atomic\n```\n\n```text\na = 1\n```\n\n```text\nb\n```\n\n```text\nb\n```\n\n```text\na\n```\n\n```text\nMFENCE\n```\n\n```text\nLFENCE/SFENCE\n```\n\n```text\nmovnt\n```\n\n```text\nb=2; a=1;\n```\n\n```text\nop dest, src1 [,src2]\n```\n\n```text\nmovw\n```\n\n```text\nmovt\n```\n\n```text\nrelease\n```\n\n```text\nacquire\n```\n\n```text\nlock xadd\n```\n\n```text\nstd::atomic\n```\n\n```text\nstd::memory_order\n```\n\n```text\na\n```\n\n```text\nstore(memory_order_release)\n```\n\n```text\nstd::atomic_thread_fence(memory_order_release)\n```\n\n```text\na.load(std::memory_order_acquire)\n```\n\n```text\n#LoadStore | #StoreStore\n```\n\n```text\nmemory_order_consume\n```\n\n```text\nmemory_order_consume\n```\n\n```text\nconsume\n```\n\n```text\nb\n```\n\n```text\nconsume\n```\n\n```text\nacquire\n```\n\n```text\nrelease\n```\n\n```text\nconsume\n```\n\n```text\nconsume\n```\n\n```text\nmemory_order_consume\n```\n\n```text\nb=2; a.store(1, MO_release); b=3;\n```\n\n```text\na=1;b=3\n```\n\n```text\nb=3; a=1;\n```\n\n```text\ndmb sy\n```\n\n```text\na=1; a=1;\n```\n\n```text\nmfence\n```\n\n```text\nLFENCE\n```\n\n```text\nmovntdqa\n```\n\n```text\nmovnti\n```\n\n========================================\n\nComments:\n- Answer to your mfence question: x86.renejeschke.de/html/file_module_x86_id_170.html\n- I was just poking around in the intel manuals myself. I'll go and have a look at the C++ specifications explanation of what std::atomic is actually promising as well I think and see if I can reconcile everything in my mind...\n- Ah - AMD clearly states: \"The rules governing write ordering are restrictive: • Generally, out-of-order writes are not allowed. Write instructions executed out-of-order cannot commit (write) their result to memory until all previous instructions have completed in program order. The processor can, however, hold the result of an out-of-order write instruction in a private buffer (not visible to software) until that result can be committed to memory.\"\n- And likewise, Intel \"Writes to memory are not reordered with other writes [ followed by a list of exceptions ]\". So I think that answers my first two bullet points.\n- I believe the default memory ordering in C++11 is sequential consistency, which is more than the release-consistency your first question hints at. The memory-fence is only required for sequential consitency.\n- ref: sequential consistency - yes. So, and I've just checked with the compiler output as well, the MFENCE isn't required for the write-release consistency at all. That's guaranteed by the processor. It's all starting to make sense.\n- @EOF: Yes, the default model **with C++11 atomics** is sequential consistency. The default model for other operations is as weak as DEC Alpha.\n- @PeterCordes: Yeah, my comment was supposed to be read in the context of the question. It's only a comment anyway, and now there's your nice and comprehensive answer so I don't think my brevity is causing any harm.\n- @preshing: I linked to a bunch of your blog posts for this answer. I found them really helpful. esp. preshing.com/20120710/&hellip; was great for me, since I already knew some stuff, but was fuzzy on terminology and the differences between the x86 memory model, ARM/PPC memory model, and the C++11 memory model.\n- @JCx: I'd been picking up bits of stuff about memory models and so on, but hadn't looked into how C++11 did things. I'd wanted to, and you question got me started digging into things and putting together the pieces of what I'd already picked up.\n- Ref. unaligned stores, what do you make of \"8.1.1 Guaranteed Atomic Operations\" in the Intel Systems Programming manual. It says 'fits in a cache line', but I believe cache lines are aligned themselves. Do they mean 'does not cross a cache line boundary' or are cache lines more flexible that I'm thinking...\n- @JCx: I'm sure they mean \"doesn't cross a cache line boundary\". Cache lines are indeed aligned. All recent Intel and AMD x86 CPUs use 64B cache lines. Intel switched sometime around P4. Earlier Intel CPUs, up to early P6 (PII and PIII) used 32B/line. As long as your data is aligned to a multiple of its size, you can load/store it atomically. Atomic RMW is available up to 16B (in 64bit mode) with CMPXCHG16B. Apparently SSE/AVX stores aren't guaranteed to be atomic, even when aligned. :/ This makes sense: Sandybridge does 256b stores in two 128b cycles. Pentium M split 128b ops.","metadata":{"transformedAt":"2026-08-18T18:32:17.787Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":56,"totalLines":310,"estimatedTokens":1563}}345{"id":"stack-3596523","source":"stackoverflow","questionId":3596523,"title":"Is it possible to read an LLVM bitcode file into an llvm::Module?","tags":["c++","compiler-construction","linker","llvm"],"text":"Title: Is it possible to read an LLVM bitcode file into an llvm::Module?\nTags: c++, compiler-construction, linker, llvm\nSource: Stack Overflow\n\nQuestion:\nI'm writing a compiler with LLVM. Each source file is compiled into an LLVM bitcode file. Eventually the linker links and optimizes all the bitcode files into one final binary.\n\nI need a way to read the bitcode files in the compiler in order to access the type information. The LLVM documentation shows a class called `BitcodeReader`, but that appears to be internal to LLVM.\n\nIs there any publicly accessible way to read a bitcode file into an `llvm::Module`?\n\n========================================\n\nCode:\n```text\nBitcodeReader\n```\n\n```text\nllvm::Module\n```\n\n```text\nModule *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,\n                         std::string *ErrMsg = 0);\n```\n\n```text\nllvm-dis\n```\n\n```text\nllvm/Bitcode/ReaderWriter.h\n```\n\n========================================\n\nComments:\n- That's exactly what you need.","metadata":{"transformedAt":"2026-08-18T18:32:17.787Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":5,"totalLines":39,"estimatedTokens":249}}346{"id":"stack-42563900","source":"stackoverflow","questionId":42563900,"title":"How does the Visual Studio Attach to Process work?","tags":["debugging","compiler-construction","remote-debugging"],"text":"Title: How does the Visual Studio Attach to Process work?\nTags: debugging, compiler-construction, remote-debugging\nSource: Stack Overflow\n\nQuestion:\nI've always wanted to know the inner-workings of Visual Studio's debugger and debuggers in general. How does it communicate and control your code, especially when it's running inside a host process or an external network server (attach to process)? Does the compiler or linker patch your code with callbacks so that the debugger is given control? If it indeed works this way, how do interpreted languages such as JavaScript containing no debug code work?\n\n========================================\n\nTop Answer:\nWindows includes support for debuggers. A process has to enable debugger privilege, and once this is done that process can attach to any other process and debug it using windows debugger functions\n\nhttp://msdn.microsoft.com/en-us/library/windows/desktop/ms679303(v=vs.85).aspx\n\nFor something like javascript, seems like you would need the equivalent of a javascript debugger.\n\nIn the case of a Visual Studio multi-process project, you typically have to switch which process the debugger is attached to in order to debug that process. I don't know if there's a way to have pending breakpoints set for multiple processes at the same time. There could be other debuggers that work better with multiple processes, but I haven't used such a tool.\n\n========================================\n\nCode:\n```text\nint 3\n```\n\n========================================\n\nComments:\n- So if an OS is very primitive and doesn't have specific debugging facilities available to a debugger, it won't work? Debugging has been around since DOS and I'm not certain it had any specific support for debuggers.\n- @ATL_DEV - The issue is that some OS's restrict the usage of debuggers. Windows doesn't do this, which allows trainer type debuggers to be used on any process. In the case of DOS in real mode, there aren't any privileged instructions needed, so DOS doesn't need to do anything special to allow debuggers. Posix systems may have an optional debugging kernel, mostly used to debug device drivers. Windows uses remote debugging, using two computers, to debug device drivers.\n- What about Linux and other more archaic OSes? Do they have similar facilities?\n- @ATL_DEV: On non-Windows OSes, debug info is often embedded in the binary using DWARF. I don't know about debugging APIs on other OSes. The question seemed to be specifically about Visual Studio and thus Windows. You can study the code for cross-platform open source debuggers, like LLDB, to learn more about the differences between OSes.\n- Some of the answers for this related question talks about other OSes: stackoverflow.com/questions/216819/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.787Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":33,"estimatedTokens":688}}347{"id":"stack-1995362","source":"stackoverflow","questionId":1995362,"title":"looking for transpiler: php to javascript","tags":["javascript","php","compiler-construction","transpiler"],"text":"Title: looking for transpiler: php to javascript\nTags: javascript, php, compiler-construction, transpiler\nSource: Stack Overflow\n\nQuestion:\ni wonder, if there's any transpiler available, which converts (simple) php code to javascript?\n\nwhat i am *not* looking for is ajax stuff or javascript libraries imitating php functionality, but i am looking for a translater, which is able to generate javascript code from php code (similar of what google is doing with GWT and the java-to-javascript transpiler).\n\nthanks!\n\n========================================\n\nTop Answer:\nYou can use something like this: http://www.harmony-framework.com/\n\nBut notice, that Javascript has lots of security issues PHP avoids. And straight conversion is not always possible for Database-related function calls or some other PHP extensions\n\n========================================\n\nComments:\n- afaik GWT is a compiler, not a transpiler; transpilers in general aren't terribly useful and will most likely only work for special cases because of the differences in type systems/standard libraries\n- I prefer the term cross-compiler, too, but the term transpiler is starting to get used more and more often these days. We'll see if it catches on. At least in the JavaScript world, I think it just might. But I agree with your sentiment. I tend to think CoffeeScript to JavaScript *is* a transpiler, but Java to JavaScript, not so much. Others disagree. We'll see what people are saying a year from now.\n- Yes, I wrote one: gitlab.com/kornelski/babel-preset-php It produces readable source with approximate semantics.\n- @Kornel thanks! that looks quite interesting!\n- maybe help you this link awesome opensource transpilers\n- my comment to the question applies: eg, harmony translates `array($a => 'foo')` to `{a : 'foo'}`; depending on the problem harald is trying to solve, something like haxe.org which can compile to both PHP and JS might be a better solution\n- thanks -- i'll have a close look at both, harmony and haxe ...\n- mmm -- harmony seems to be a little outdated -- the webpages last change was in 2008. do you know, if they are still working on it? i think harmony would fit my needs better than haxe, even though haxe seems to be very advanced. but i don't know how i would integrate haxe with my app, because my app is already implemented in php ... only the php->javascript part is missing, which would help speed up development and make things more convenient ...\n- @harald: if you look at the svn commit feed of harmony ( code.google.com/feeds/p/harmony-framework/svnchanges/basic ), you'll see that development is slow, but I don't think it's dead; the documentation an how to wrap PHP code for haxe seems somewhat... incomplete: haxe.org/doc/php/extern_libraries ; if all that's missing from your application is some JS, porting it to haxe will be overkill\n- i mark this as answered, because this harmony thing seems at least to be a very good starting point for this. thanks you both!\n- Thanks, this looks already very promising!","metadata":{"transformedAt":"2026-08-18T18:32:17.787Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":32,"estimatedTokens":756}}348{"id":"stack-7522955","source":"stackoverflow","questionId":7522955,"title":"Why are modules explicitly named in files?","tags":["compiler-construction","d","language-design"],"text":"Title: Why are modules explicitly named in files?\nTags: compiler-construction, d, language-design\nSource: Stack Overflow\n\nQuestion:\nFrom the D language reference:\n\n Modules have a one-to-one correspondence with source files. The module name is the file name with the path and extension stripped off.\n\nModule names are still specified in files explicitly, though.\n\n```\nmodule foo;\n```\n\nWhat's the point of this? If modules correspond to files, why can't the compiler infer what they're called from the file names?\n\n========================================\n\nTop Answer:\nIt *can* infer it. You don't *have* to give the module name. However, you *can* give the module name, which allows you to give it an entirely different name if you want to. A prime example of this is if the file name isn't a valid module name (e.g. my-module.d). In such a case, you can use the `module` declaration to give it a valid module name (e.g. my_module).\n\nIt's common practice to put the module name at the top of the file, and usually the module name is the same as the file name, but making it possible to have the module name not exactly match the file name increases flexibility.\n\nPersonally, I would generally consider it a bad idea to name the module anything other than the file name, and I'd argue that if the file isn't a valid module name, then the file name should be changed so that it is one, but apparently it was decided that the extra flexibility of making it possible for them not to match was worth having. So, it's in the language.\n\n========================================\n\nCode:\n```text\nmodule foo;\n```\n\n```text\npackage\n```\n\n```text\nmodule\n```\n\n========================================\n\nComments:\n- IIUC, D's official site now is d-programming-language.org.\n- Folders do correspond to packages, the path stripping is just referring to the current working directory. The reason to specify the module name is so that the hierarchy does not need maintained to compile the files if passing all needed files to the compiler (di files were added much later).\n- If you name the module something other than the filename, import statements will not be able to find it.\n- @FeepingCreature It works as long as you compile all of the files at once. I don't know if it's supposed to work with imports or not (you're right that it currently doesn't). But that would be another reason why naming your modules something other than the filename isn't generally a good idea.\n- The compiler don't need to be able to find the .d if it can find a correct .di\n- @BCS Assuming that you're using a .di file, which causes its own set of issues. But if you're renaming the module just because you want to put '-' in the file name instead of '_' (which is the example that TDPL gives), you're going to have exactly the same problem with the .di file, unless you're willing to name *that* file properly, in which case why not name the original file properly?","metadata":{"transformedAt":"2026-08-18T18:32:17.787Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":50,"estimatedTokens":733}}349{"id":"stack-11408792","source":"stackoverflow","questionId":11408792,"title":"How does the C# compiler decide to emit retargetable assembly references?","tags":["c#","compiler-construction","portable-class-library"],"text":"Title: How does the C# compiler decide to emit retargetable assembly references?\nTags: c#, compiler-construction, portable-class-library\nSource: Stack Overflow\n\nQuestion:\nRetargetable assembly references have been introduced for the .NET Compact Framework and are now used to support Portable Class Libraries.\n\nBasically, the compiler emits the following MSIL:\n\n```\n.assembly extern retargetable mscorlib\n{\n .publickeytoken = (7C EC 85 D7 BE A7 79 8E ) \n .ver 2:0:5:0\n}\n```\n\nHow does the C# compiler understand it has to emit a retargetable reference, and how to force the C# compiler to emit such reference even outside of a portable class library?\n\n========================================\n\nTop Answer:\nNot sure if this will help, but the following file was auto-generated and included in the build.\n\n```\nusing System;\nusing System.Reflection;\n[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(\n \".NETPortable,Version=v4.0,Profile=Profile4\", \n FrameworkDisplayName = \".NET Portable Subset\")]\n```\n\nThis might hint to the compiler to do some magic.\n\nEdit:\n\nI think above makes a library portable. From the command line I can see `/nostdlib+` is used, and a portable `mscorlib.dll` is referenced (which I assume has the same attribute as mentioned above). \n\n\"...\\Program Files\\Reference Assemblies\\Microsoft\\Framework.NETPortable\\v4.0\\Profile\\Profile4\\mscorlib.dll\"\n\n========================================\n\nCode:\n```text\n.assembly extern retargetable mscorlib\n{\n    .publickeytoken = (7C EC 85 D7 BE A7 79 8E )                         \n    .ver 2:0:5:0\n}\n```\n\n```text\nusing System;\nusing System.Reflection;\n[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(\n   \".NETPortable,Version=v4.0,Profile=Profile4\", \n   FrameworkDisplayName = \".NET Portable Subset\")]\n```\n\n```text\n/nostdlib+\n```\n\n```text\nmscorlib.dll\n```\n\n```text\n.assembly\n```\n\n========================================\n\nComments:\n- No hints from MSBuild target files? I wonder what you need to pass to the compiler from the command line.\n- The `TargetFrameworkAttribute` is also present for Client and Full Framework builds for v4, it isn't around for v3.5. This might be why they added the attribute. As of v4, it also offers a very easy way to determine if an assembly was built for the full framework or the client profile.\n- @AdamHouldsworth: Thanks, and I guess it is obsolete now given the client profile is gone in 4.5? ;p\n- Even in .NET 4 the difference between the client profile and full download was a couple of MB lol, not worth it - probably why they canned it in favour of PCL.\n- Thanks. The reference to the portable mscorlib.dll may be the point.\n- The TargetFrameworkAttribute is actually meaningless when it comes to portability. Old style portable (ie mscorlib-based), it is based on the retargetable reference to mscorlib. New style portable (ie System.Runtime-based) it soley based on the fact that System.Runtime, et al are present at runtime.\n- We discuss this at depth over here: channel9.msdn.com/Shows/Going+Deep/&hellip;.\n- @DavidKean: Thanks, going to check that out now :)\n- Thank you. This is what I was looking for. I was hoping to get rid of the message `Could not load file or assembly 'PostSharp, Version=3.0.0.0, Culture=neutral, PublicKeyToken=53d2effcf2ee70dc, Retargetable=Yes' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)` when providing (through IHostAssemblyStore) another assembly than what the CLR requested, but I still get the error even with a retargetable reference. Is there any workaround to this?\n- Retargable won't let you key jump a user assembly, like I'm assuming you are attempting. It is entirely for CLR internal purposes. I'm not an expert on the hosting APIs, but I think LoadFile might allow you to do it.\n- Thanks. I will try another solution: same short name, same strong-name key, but different version number. Normal binding policies should do the trick.","metadata":{"transformedAt":"2026-08-18T18:32:17.787Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":85,"estimatedTokens":1005}}350{"id":"stack-8873126","source":"stackoverflow","questionId":8873126,"title":"Zephyr ASDL (Abstract Syntax Description Language)","tags":["python","syntax","compiler-construction","definition","asdl"],"text":"Title: Zephyr ASDL (Abstract Syntax Description Language)\nTags: python, syntax, compiler-construction, definition, asdl\nSource: Stack Overflow\n\nQuestion:\n**Question:**\n\nWhat is the Zephyr ASDL and how does it relate to other compiler technologies like lexers and parser generators?\n\n(I would appreciate it if you were reasonably complete, but point to other references online when it gets rather technical, because most of what I know about compilers come from playing with yacc and flex, writing a simple maximal munch lexer in C, and looking up and reading stuff online)\n\n**Question Background:**\n\nI've been reading http://docs.python.org/devguide/compiler.html and I came across the following line:\n\n The specification of the AST nodes is specified using the Zephyr\n Abstract Syntax Definition Language (ASDL).\n\nI followed the citation at the bottom to find:\nhttp://www.cs.princeton.edu/research/techreps/TR-554-97.\n\nMy first reading through the article has been rather tumultuous, and I was hoping I could first get a better understanding of what the purpose of ASDL was (in context of the compilation process), before trying again.\n\n========================================\n\nTop Answer:\nASDL is used when you need to generate a tree in a module and input the same tree in other module (or almost the same tree, somehow optimised).\n\nFor this, you need to have functions of construction (ideally with type checker), function of printing the tree such that visualising it you are sure you generated it correctly.\n\nASDL takes as input some tree written in a syntax almost identical with the syntax of algebraic data type (like in haskell or ml), or the syntax in BNF but much more simplified, and auto-generates all the contructors, printing functions starting with the simple description of a tree.\n\nFor example, if you have a lexer, it will have to generate lexemes that have a type. You also need to see the output stream of lexemes (this is in linear form, so a very simple tree). Instead of writing functions for printing, constructing lexemes, you define them something like that\n\n```\nlexeme=\n ID(STRING)\n | INT(num_integer)\n | FLOAT(num_float)\n attributes(int coord_x, int coord_y)\n num_integer:\n ....\n num_float:\n ....\n```\n\nand you call constructors ID, INT, FLOAT, etc from your lexer. ASDL will convert this simple syntax in all the functions you need, either to construct nodes for AST, or to print, or whatever you need. ASDL does not impose restrictions on the generated code.\n\nIf you add `attributes` to a type, such as the coordinates of a token, such attributes are appended to the parameters of each contructor from that type.\n\nA more complex tree, created by a parser would look like that\n\n```\nexpr: SUM(expr, expr)\n |PRODUCT(expr, expr)\n |number\nnumber: num_integer\n```\n\nIn this case asdl will check that the call of SUM(_ _) made by the parser will pass to sum nodes created with one of the constructors of expr. `num_integer` is defined externally, maybe by an asdl tree for the lexer.\n\nNote that you are not allowed to define constructors containing regular expressions, such as `number: [0-9]+`. ASDL is simpler than EBNF.\n\nThese constructors will be defined such that to build what you need and more than that, they type check, to be sure that your lexer/parser/code generator outputs trees that conform the language defined by asdl.\n\nTo well understand ASDL you need to write 3-4 parsers and see what is common in the code they generate. That common part is in fact ASDL, so this is an abstraction for the output of the parsers in particular.\n\n========================================\n\nCode:\n```text\nlexeme=\n       ID(STRING)\n     | INT(num_integer)\n     | FLOAT(num_float)\n     attributes(int coord_x, int coord_y)\n   num_integer:\n     ....\n   num_float:\n     ....\n```\n\n```text\nexpr:  SUM(expr, expr)\n      |PRODUCT(expr, expr)\n      |number\nnumber: num_integer\n```\n\n```text\nattributes\n```\n\n```text\nnum_integer\n```\n\n```text\nnumber: [0-9]+\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.787Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":100,"estimatedTokens":993}}351{"id":"stack-8786203","source":"stackoverflow","questionId":8786203,"title":"Compiling Python to native code?","tags":["python","windows","compiler-construction","executable"],"text":"Title: Compiling Python to native code?\nTags: python, windows, compiler-construction, executable\nSource: Stack Overflow\n\nQuestion:\n**Possible Duplicate:**\n\nIs it feasible to compile Python to machine code?\n\nIs it possible to compile Python code (plus its dependencies, plus the interpreter library) into **a single, *native*** Windows executable (with *nothing* else bundled along with it) from a Python file? (Kind of like how the GNU compiler for Java compiles Java into a *native* (humongous) executable, which contains everything in true machine code.)\n\nIf so, how would I go about doing this?\n\n(Specifically, `py2exe` does *not* do what I want -- it includes the libraries inside a *separate* ZIP file, and it includes the interpreter as a *separate* DLL.)\n\n### Note 1:\n\nTo emphasize, I'm ***not*** asking for a \"self-extracting archive\", an \"executable packer\", or some other way of 'cheating' by bundling the files inside an `exe` -- I'm looking for something that genuinely converts Python into a native executable, like what GCJ does for Java.\n\n### Note 2:\n\nOnly if the above *isn't* possible:\n\nIs it possible to at least generate a *single* executable from a Python code containing the interpreter bundled along with all the library dependencies, such that the resulting executable does *not* need to self-extract onto the target disk before running?\n\nIn this scenario, the 'compilation' requirement is relaxed: it doesn't matter if the code is actually compiled into machine code (it could simply be embedded as a text resource into the target executable), but the result must nevertheless be a *single* `exe` file [and nothing else] that can run standalone, specifically *without* needing to unpack/install anything onto the target disk before running.\n\n========================================\n\nTop Answer:\nThe short answer is no, and that is going to go for almost any language: any program you write is going to depend on some external libraries even if just the Windows system DLLs.\n\nIf you wrote a C program and compiled it with Microsoft's compiler you would still need the C runtime libraries to be installed. Chances are they already will be on most systems but it isn't guaranteed. Likewise even if you managed to compile a C Python interpreter statically linked to its libraries you still have to get the C runtime from somewhere.\n\nWhat I suspect you are really asking is whether you can compile to a single .exe that depends only on libraries which you have a reasonable expectation of already being installed. So it all depends on what you are willing to consider part of the base system? Can you assume .Net framework 4 or Silverlight are installed? If so you might want to look at IronPython.\n\nLikewise pypy can be built with either the Visual Studio toolchain or MinGW but I'm pretty sure in both cases you'll still need some external libraries at runtime.\n\n========================================\n\nCode:\n```text\npy2exe\n```\n\n```text\nexe\n```\n\n```text\nexe\n```\n\n========================================\n\nComments:\n- I think you can create native .net binaries with Python, but they will not be monolithic (will depend on .net runtime environment).\n- @PauloScardine: Yeah I'm not looking for that either. :) Thanks for pointing it out.\n- +1 looks like it's exactly what I need; I'll definitely give it a try.\n- Shoot... looks like the resulting executable still depends on `libpcre-0.dll`, `libgcc_s_dw2-1.dll`, and `libstdc++-6.dll`. Unless I can get rid of them by statically linking with GCC, this won't work...\n- *you still have to get the C runtime from somewhere* I'm not sure if I get what you wanted to say here but you can obviously link statically to C runtime like to any other lib.\n- What I was trying to say was that most compilers default these days to linking the libraries dynamically. I think Visual Studio still has an option to link statically to the C runtime but not all compilers will give you that option. Even if they do you there will still be some libraries where you don't get that option if the library is supplied as a DLL plus an import library.\n- @Duncan I'm not a C/C++ guy, but I'm pretty sure you can statically link using GCC too, and I don't see why it wouldn't be possible.","metadata":{"transformedAt":"2026-08-18T18:32:17.787Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":63,"estimatedTokens":1059}}352{"id":"stack-6986143","source":"stackoverflow","questionId":6986143,"title":"How does method call affect the performance in Java?","tags":["java","compiler-construction"],"text":"Title: How does method call affect the performance in Java?\nTags: java, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI do like to write really readable code in Java. For that I use the builder pattern and have a lot of static methods. But at some point I do call or chain a lot of methods, so I came up with this question: Does it have any performance issues calling a lot of methods?\n\n========================================\n\nTop Answer:\nReadability is almost always the way to go. There is impact on performance for function calls but non readable code will probably suffer from other implementation issues that will have higher performance hit.\n\nUse the keyword final where appropriate so that you suggest inlineing for these functions.\n\nA situation where you don't want to use a lot of function calls are tight loops. Something that happens hundred of thousands of times. And usually these places can be optimized at the end if optimizing the software is needed.\n\n========================================\n\nComments:\n- If you, who is using the code, doesn't know whether there is a measurable and serious performance impact, how should we know?\n- Do you have any evidence to support your claim about large recursive calls? The following implies otherwise\n- @nerdytenor: Yes I do from personal experience (I will not bore you with the details and instead encourage you to benchmark it yourself if you have any doubts). Please read the bottom of the post you linked to which explains the performance discrepancy. Here is a more accurate comparison (on the same site): ahmadsoft.org/articles/recursion/ubuntu.html\n- agree here, and to reiterate: unless you are making hundreds of thousands of calls within a few seconds time, this is not something you need to worry about.","metadata":{"transformedAt":"2026-08-18T18:32:17.787Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":23,"estimatedTokens":446}}353{"id":"stack-2226968","source":"stackoverflow","questionId":2226968,"title":"Is C++ built on top of C?","tags":["c++","c","compiler-construction"],"text":"Title: Is C++ built on top of C?\nTags: c++, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nDoes C++ code gets converted to C before compilation ?\n\n========================================\n\nTop Answer:\nNo, but like most myths there's a shred of truth to this. The original compiler for C with classes (which later became C++) was nicknamed CFront and did translate to C.\n\n========================================\n\nComments:\n- It should be noted that your title question is quite different from your body question.\n- There is no requirement in the C++ language specification to convert C++ to C before compilation. The specification leaves the translation up to the implementer (compiler vendor). The answer is that not all compilers translate C++ to C before compilation; some do, some don't.\n- cfront was not a \"preprocessor\", it was a full blown pre-std C++ compiler.\n- Comeau C++ is still implemented this way.\n- The first cfront was compiling the language called \"C with classes\" but later versions compiled real early pre-std C++, not just \"C with classes\".\n- Did the Cfront compiler remove or disallow any feature of C?\n- @ThomasWeller: That depends on what you mean by \"feature\". For example, it wouldn't let you name a variable `class`, but I don't know if you consider that removing a feature or not.\n- This is largely irrelevant to the actual question being asked, and looks a lot like spam if it didn't come from a user with 604 reputation. I suspect you're just trying to be helpful however, so I won't penalize you, but this answer isn't terribly helpful to a C++ user.\n- Most C99 features (the ones not borrowed from C++) are not in the current standard, some will not be in the upcoming standard either. Most notably dynamic arrays. In C++ the size is part of the type of the array, and allowing dynamic arrays would imply that there is an element in the language whose type would not be known at compile time, breaking the type system.\n- That's too strong a statement actually; you can define the type systems such that \"dynamic array of type T\" is the compile-time type. That would imply that the array size is only part of the type when an array is not dynamic, and therefore static and dynamic arrays would have different types. Not a big deal IMO; in fact it sounds obvious. The trickier parts are wrt to `sizeof` - it's not always an ICE anymore.","metadata":{"transformedAt":"2026-08-18T18:32:17.787Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":595}}354{"id":"stack-29197332","source":"stackoverflow","questionId":29197332,"title":"How to find FIRST and sets of a recursive grammar?","tags":["compiler-construction","context-free-grammar"],"text":"Title: How to find FIRST and sets of a recursive grammar?\nTags: compiler-construction, context-free-grammar\nSource: Stack Overflow\n\nQuestion:\nSuppose I have the following CFG.\n\n```\nA -> B | Cx | EPSILON\nB -> C | yA\nC -> B | w | z\n```\n\nNow if I try to find\n\n```\nFIRST(C) = FIRST(B) U FIRST(w) U FIRST(z)\n = FIRST(C) U FIRST(yA) U {w, z}\n```\n\nThat is, I'm going in a loop.\nThus I assume I have to convert it into a form which has immediate left recursion, which I can do as follows.\n\n```\nA -> B | Cx | EPSILON\nB -> C | yA\nC -> C | yA | w | z\n```\n\nNow if I try to calculate FIRST sets, I think I can get it done as follows.\n\n```\nFIRST(C) = FIRST(C) U FIRST(yA) U FIRST(w) U FIRST(z)\n = { y, w, z } // I ignore FIRST(C)\nFIRST(B) = FIRST(C) U FIRST(yA)\n = { y, w, z }\nFIRST(A) = FIRST(B) U FIRST(Cx) U FIRST(EPSILON)\n = { y, w, z, EPSILON }\n```\n\nAm I correct there?\n\nBut even if I'm right there, I still run into a problem when I try to calculate sets from this grammar.\n\n```\n(A) = { $ } U (B) U (C)\n```\n\nI get (B) from 2nd rule and (C) from 3rd rule. But now to calculate (B), I need (A) (from 1st grammar rule) so again I'm stuck in a loop.\n\nAny help?\nThanks in advance!\n\n========================================\n\nCode:\n```text\nA -> B | Cx | EPSILON\nB -> C | yA\nC -> B | w | z\n```\n\n```text\nFIRST(C) = FIRST(B) U FIRST(w) U FIRST(z)\n         = FIRST(C) U FIRST(yA) U {w, z}\n```\n\n```text\nA -> B | Cx | EPSILON\nB -> C | yA\nC -> C | yA | w | z\n```\n\n```text\nFIRST(C) = FIRST(C) U FIRST(yA) U FIRST(w) U FIRST(z)\n         = { y, w, z } // I ignore FIRST(C)\nFIRST(B) = FIRST(C) U FIRST(yA)\n         = { y, w, z }\nFIRST(A) = FIRST(B) U FIRST(Cx) U FIRST(EPSILON)\n         = { y, w, z, EPSILON }\n```\n\n```text\nFOLLOW(A) = { $ } U FOLLOW(B) U FOLLOW(C)\n```\n\n```text\nA → B | Cx | ε\nB → C | yA\nC → B | w | z\n```\n\n```text\nFOLLOW(A) = FOLLOW(B) ∪ {$}\nFOLLOW(B) = FOLLOW(A) ∪ FOLLOW(C)\nFOLLOW(C) = FOLLOW(B) ∪ {x}\n```\n\n```text\nFOLLOW(A) = {} ∪ {$} = {$}\nFOLLOW(B) = {$} ∪ {} = {$}\nFOLLOW(C) = {$} U {x} = {$,x}\n```\n\n```text\nFOLLOW(A) = {$} ∪ {$} = {$}\nFOLLOW(B) = {$} ∪ {$,x} = {$,x}\nFOLLOW(C) = {$,x} U {x} = {$,x}\n```\n\n```text\nFOLLOW(A) = {$,x} ∪ {$} = {$,x}\nFOLLOW(B) = {$} ∪ {$,x} = {$,x}\nFOLLOW(C) = {$,x} U {x} = {$,x}\n```\n\n```text\nFOLLOW(A) = {$,x} ∪ {$} = {$,x}\nFOLLOW(B) = {$,x} ∪ {$,x} = {$,x}\nFOLLOW(C) = {$,x} U {x} = {$,x}\n```\n\n========================================\n\nComments:\n- What IS the \"most efficient technique\" for calculating FIRST? I think it is to start at the goal symbol and recursively each RHS, propagating the FiRST sets up the tree. Loop detection gets tricky, though.\n- @david: The most efficient technique is to compute the image of a transitive closure using some variant of Tarjan's algorithm (see Esko Nuutila, 1994 for some techniques to optimise this algorithm, although I'm sure that there is more recent research as well.) (The image of a transitive closure takes a relation R and a function F and computes R*F; as Nuutila points out, the TC algorithm can be quicker if you find the union of image sets rather than finding the union of node sets and then compute the image.)","metadata":{"transformedAt":"2026-08-18T18:32:17.787Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":11,"totalLines":127,"estimatedTokens":774}}355{"id":"stack-14043060","source":"stackoverflow","questionId":14043060,"title":"How to extend Clang with an additional parser?","tags":["c++","compiler-construction","llvm","clang"],"text":"Title: How to extend Clang with an additional parser?\nTags: c++, compiler-construction, llvm, clang\nSource: Stack Overflow\n\nQuestion:\nHow can I extend Clang with an additional parser for files with a special file ending, i.e. can I develop a `FrontendAction` that says \"Hey! I'll take care of all files with the file ending '.lorem' and return an abstract syntax tree (`clang::ASTContext` ?)\"?\n\nI've read about `clang::FrontendAction`, `clang::Parser` and `clang::driver::Driver` but I haven't been able to figure out where and how I should extend Clang to be able to extend the compiler with an additional parser (*not* extending the current parser).\n\n========================================\n\nCode:\n```text\nFrontendAction\n```\n\n```text\nclang::ASTContext\n```\n\n```text\nclang::FrontendAction\n```\n\n```text\nclang::Parser\n```\n\n```text\nclang::driver::Driver\n```\n\n```text\ntools/clang/lib/Driver/Types.cpp\n```\n\n```text\nlookupTypeForExtension\n```\n\n```text\n.m\n```\n\n```text\nTY_ObjC\n```\n\n```text\nisObjC\n```\n\n```text\nPreprocessor\n```\n\n```text\nLangOptions\n```\n\n```text\ninclude/clang/Basic/LangOptions.def\n```\n\n```text\nObjC1\n```\n\n```text\nObjC2\n```\n\n```text\nCompilerInvocation::setLangDefaults\n```\n\n```text\nCompilerInvocation::CreateFromArgs\n```\n\n========================================\n\nComments:\n- Is your extended language similar to C/C++/Objective-C? Clang is the frontend for these kinds of languages. If we want to reuse the backend (optimization, etc.), we only need LLVM.\n- The language will be similar to Objective-C++ (the semantics, not the syntax). The preprocessor is the same.\n- I dont know much about Clang. But how about transcompiling to ObjC then?\n- I want to get some benefits from using Clang - code completion integration, compiling C/C++/ObjC++ code at the same time, not having to generate code (just parsing and generating a high level AST), etc. Transcompiling to ObjC++ is possible, but then I would also have to be able to parse C/C++/ObjC/ObjC++ (in order to calculate which symbols are referenced) since I want to be able to reference/include files from C etc.\n- Did you get any information on this? Did you find that it was better to use LLVM directly, or were you able to generate an AST for Clang?\n- Hi @finnsson, I've come across the same set of questions. Were you able to figure out how to use a custom parser to generate an AST and process it via the res of Clang's Driver?\n- Clang's CFE Internals Manual is a great resource for understanding how to add statements/expressions: clang.llvm.org/docs/&hellip;\n- Eli, have there been any updates to Clang's architecture since the time of your response that would make it easier to add an extra parser to Clang? Alternatively, is it possible to use a custom parser to create a `clang::TranslationUnitDecl` and process that AST through the rest of the Clang driver? Thanks!\n- @StefanNovak: I'll admit I don't know - it's been years since I worked with Clang intensively.\n- No worries. Thanks for the quick response. I'm a big fan of your blog! :)","metadata":{"transformedAt":"2026-08-18T18:32:17.787Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":17,"totalLines":93,"estimatedTokens":753}}356{"id":"stack-25582530","source":"stackoverflow","questionId":25582530,"title":"How does the C compiler parse the following C statement?","tags":["c","compiler-construction","printf","lexical-analysis"],"text":"Title: How does the C compiler parse the following C statement?\nTags: c, compiler-construction, printf, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nConsider the following lines:\n\n```\nint i;\nprintf(\"%d\",i);\n```\n\nWill the lexical analyzer go into the string to parse `%` and `d` as separate tokens, or will it parse \"%d\" as one token?\n\n========================================\n\nTop Answer:\nA string literal is a single token. The above code will be tokenized like this:\n\n```\nint keyword \"int\"\ni identifier\n; semicolon\nprintf identifier\n( open paren\n\"%d\" string literal\n, comma\ni identifier\n) closing paren\n; semicolon\n```\n\n========================================\n\nCode:\n```text\nint i;\nprintf(\"%d\",i);\n```\n\n```text\n%\n```\n\n```text\nd\n```\n\n```text\n%\n```\n\n```text\nprintf()\n```\n\n```text\nprintf\n```\n\n```text\nprintf\n```\n\n```text\nint     keyword \"int\"\ni       identifier\n;       semicolon\nprintf  identifier\n(       open paren\n\"%d\"    string literal\n,       comma\ni       identifier\n)       closing paren\n;       semicolon\n```\n\n```text\ntoken:\n  keyword\n  identifier\n  constant\n  string-literal\n  punctuator\n```\n\n```text\npreprocessing-token:\n  header-name\n  identifier\n  pp-number\n  character-constant\n  string-literal\n  punctuator\n  each non-white-space character that cannot be one of the above\n```\n\n```text\n\"%d\"\n```\n\n```text\n6.4\n```\n\n```text\n5.1.1.2\n```\n\n```text\n\"%d\" \"\\n\"\n```\n\n```text\n6\n```\n\n========================================\n\nComments:\n- lexical analyzer will identify `\"%d\"` as compete string. It is next phases of compiler \"syntax\" and \"semantic\" those identify missing argument and type check. If you compile code (without `i` in printf) you will get warning `\"format ‘%d’ expects a matching ‘int’ argument\"` due to \"syntax\" and \"semantic\" checks.\n- how can a syntax analysis find an error if the string is not seperately parsed as token?\n- \"syntax analysis\" is next phase after \"lexical\". Compiler fist generates stream of tokens (as given in sepp2k's answer) then stream of tokes are further parsed (using grammar) and semantically checked in next phase.\n- Try this code on your PC. You will get warning for second line print but not for `str = \"%d\"` that means compiler only parse `\"%d\"` when it is in printf - that indicates work is done after lexical phase\n- try this trick\n- Lexical analysers don't parse anything. They scan it. Parsers parse. String literals are scanned without regard to the contents. The compiler doesn't know anything about `printf()` except for its presence, signature, and linkage.\n- I think you might be confusing terms -- when you say \"one token\" do you mean like how \"\\n\" is parsed as a single character (even though you wrote two?) -- that is not the case for \"%d\" -- it gets parsed as two individual characters, since \"%\" is not an escape character that the compiler knows about / honors.\n- I think the OP wanted to know more along the lines of will \"%d\" generate a char array with a length of 1, or 2 (i.e. \"\\n\" would generate a char array with a length of 1) -- I think the answer he's looking for is that it would be a string literal with two distinct characters in it (that is then further parsed at run-time by the *printf method).","metadata":{"transformedAt":"2026-08-18T18:32:17.787Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":15,"totalLines":128,"estimatedTokens":796}}357{"id":"stack-446883","source":"stackoverflow","questionId":446883,"title":"Best Compiler Destination","tags":["compiler-construction","llvm"],"text":"Title: Best Compiler Destination\nTags: compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nI've got a few languages I've been building as interpreters. When I'm ready to take \"that next step\", what options are best for non-native compiled formats... what are the pros and cons of each?\n\nI've been looking at compiling to CLR or LLVM, and contemplated C-midcompile a few times, but I'm not completely certain.\n\nA few features I'm hoping to be able to port are as follows:\n\n- REPL - One of the languages I'm building supports block-level evaluation during runtime.\n\n- Robust Macros - One of the languages I'm building requires the ability to filter through code seperately before tokenizing, and in the midstep between tokenizing and parsing.\n\nOk, not really \"a few\", just two. I like to think I can port any other features my languages support to \"anything\".\n\nWhat are my best options, and their pros/cons?\n\n========================================\n\nTop Answer:\nCode generation is my business :-)\n\nComments on a few options:\n\nCLR: \n\n- Pro: industrial support\n\n- Con: you have to buy into their type system pretty much completely; depending on what you want to do with types, this may not matter\n\n- Con: Only Windows platform is really prime-time quality\n\nLLVM:\n\n- Pro: enthusiastic user community with charismatic leader\n\n- Pro: serious backing from Apple\n\n- Pro: many interesting performance improvements\n\n- Con: somewhat complex interface\n\n- Con: history of holes in the engineering; as LLVM matures expect the holes in the engineering to be plugged by adding to the complexity of the interface\n\nC--\n\n- Pro: target is an actual written language, not an API; you can easily inspect, debug, and edit your C-- code\n\n- Pro: design is reasonably mature and reasonably clean\n\n- Pro: supports accurate garbage collection\n\n- Pro: most users report it is very easy to use\n\n- Con: very small development team\n\n- Con: as of early 2009, supports only three hardware platforms (x86, PPC, ARM)\n\n- Con: does not ship with a garbage collector\n\n- Con: project has no future\n\nC as target language\n\n- Pro: looks easy\n\n- Con: nearly impossible to get decent performance\n\n- Con: will drive you nuts in the long run; ask the long line of people who have tried to compile Haskell, ML, Modula-3, Scheme and more using this technique. At some point every one of these people gave up and built their own native code generator.\n\nSummary: **anything except C** is a reasonable choice. For the best combination of flexibility, quality, and expected longevity, I'd probably recommend LLVM.\n\nFull disclosure: I am affiliated with the C-- project.\n\n========================================\n\nComments:\n- Java ByteCode is something I've always been iffy about. Call it bad past experience. Do any of them have any perks regarding power of their internal VM (other than just library calls?)\n- C code generation looks easy until you've been doing it for 6 to 18 months. Then suddenly things become impossible.\n- Just a little Note: You don't have to use LLVM&#180;s API, you could target its assembler-like language instead.","metadata":{"transformedAt":"2026-08-18T18:32:17.787Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":82,"estimatedTokens":775}}358{"id":"stack-19381162","source":"stackoverflow","questionId":19381162,"title":"Are compilers allowed to optimize-out exception throws?","tags":["c++","exception","compiler-construction","compiler-optimization"],"text":"Title: Are compilers allowed to optimize-out exception throws?\nTags: c++, exception, compiler-construction, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nWe have been discussing this topic today at work, and none of us could come up with a definitive answer about that question. Consider the following situation:\n\n```\nint foo()\n{\n int err;\n\n err = some_call(1);\n\n if (err != 0)\n return err;\n\n err = some_call(2);\n\n if (err != 0)\n return err;\n\n err = some_call(3);\n\n if (err != 0)\n return err;\n\n err = some_call(4);\n\n if (err != 0)\n return err;\n\n bar();\n\n return err;\n}\n```\n\nThere is a lot of code repetition. Obviously, this could be factorized with a macro, sadly not with a template (because of the return clause). Or at least not directly.\n\nNow the question is, if we were to replace those return error codes with exceptions, and catching those exceptions right away, are compilers allowed and smart enough to detect the pattern and avoid throwing exceptions altogether ?\n\nHere is an illustration of what I mean:\n\n```\nint foo()\n{\n try\n {\n // some_call now throws a ErrorReturned exception that contains the error code upon failure.\n some_call(1);\n some_call(2);\n some_call(3);\n some_call(4);\n }\n catch (ErrorReturned& ex)\n {\n return ex.error_code();\n }\n\n bar();\n\n return 0;\n}\n```\n\nNow, there is no current performance issue and so yes, we don't need to optimize or even care about that. This is more to understand what compilers are allowed to do.\n\nIn short so, is it a \"good\" practice and if so, can compilers optimize that by not throwing exceptions at all ? (Assuming the exception construction has no side effect)\n\n========================================\n\nCode:\n```text\nint foo()\n{\n  int err;\n\n  err = some_call(1);\n\n  if (err != 0)\n    return err;\n\n  err = some_call(2);\n\n  if (err != 0)\n    return err;\n\n  err = some_call(3);\n\n  if (err != 0)\n    return err;\n\n  err = some_call(4);\n\n  if (err != 0)\n    return err;\n\n  bar();\n\n  return err;\n}\n```\n\n```text\nint foo()\n{\n  try\n  {\n    // some_call now throws a ErrorReturned exception that contains the error code upon failure.\n    some_call(1);\n    some_call(2);\n    some_call(3);\n    some_call(4);\n  }\n  catch (ErrorReturned& ex)\n  {\n    return ex.error_code();\n  }\n\n  bar();\n\n  return 0;\n}\n```\n\n```text\nOptimizing Away C++ Exception Handling\n```\n\n```text\n-fno-exceptions\n```\n\n```text\nabort()\n```\n\n```text\nsetjmp/longjmp\n```\n\n========================================\n\nComments:\n- \"This is more to understand what compilers are allowed to do.\" Do you know the as-if rule, [intro.execution]/1 (and /8)?\n- How about simply `if ((err = some_call(1))) return err;` repeated for `2`, `3` and `4`?\n- If `some_call` gets inlined then sure, the compiler is allowed to replace the throw/catch with in effect a goto. As to whether your compiler does or not, there's no substitute for staring at (dis-)assembly :-) At a quick look, gcc 4.8.1 with -O3, a function with a throw/catch in it the emitted code still allocates an exception object and unwinds.\n- Note that most sane compilers have zero performance impact on using exceptions as long as they are not thrown, and are usually quite fast in case they are thrown too.\n- @Mgetz The \"standards\" don't define how compilers do optimizations and each does their own their own way; we could scour the manuals of each, possibly even the source of each or even the assembly they generate to determine if they strip exceptions that are immediately used - but I think that's a little overkill unless you're interested in a lot of research. I've linked to a paper that outlines current compiler optimizations for Exception Handling that has done a lot of research already, hopefully it can better answer your question =]\n- I can't find a date in that paper, but it cites a draft of the C++98 Standard -- it seems it's rather old.\n- @newfurniturey hence why I removed my comment, I was more curious to see if the standard explicitly forbid certain optimizations\n- Ignoring whether it is new or old, that paper seems to focus on how to make exceptions faster when they happen. The question at hand is whether the compiler can avoid the exception from happening, which is a different question. Although to be honest I would be utterly surprised if any compiler removed the exception in the code above","metadata":{"transformedAt":"2026-08-18T18:32:17.788Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":153,"estimatedTokens":1075}}359{"id":"stack-402969","source":"stackoverflow","questionId":402969,"title":"What makes the Java compiler so fast?","tags":["c#","java",".net","c++","compiler-construction"],"text":"Title: What makes the Java compiler so fast?\nTags: c#, java, .net, c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI was wondering about what makes the primary Java compiler (javac by sun) so fast at compilation?\n\n..as well as the C# .NET compiler from Microsoft.\n\nI am comparing them with C++ compilers (such as G++), so maybe my question should have been, what makes C++ compilers so slow :)\n\n========================================\n\nTop Answer:\nI think the most difficult part is not the need to compile the header files (unless they are really big, but you can use precompiled headers in that case). The worst part is always the fact that C++'s grammar is too wildly context-sensitive. Despite the fact I like C++, I feel sorry for anybody who has to write a C++ parser.\n\n========================================\n\nComments:\n- What are you comparing? The time that it takes to compile an application from source, or the runtime performance?\n- Could be more interesting with some examples. Some measurements with timings + number of lines of code.\n- I am comparing the compilation time, not the run time.\n- See stackoverflow.com/questions/318398/&hellip; Not sure if this one should be closed as a duplicate question, so just posting the link for now. :)\n- One could use precomiled headers to speed ths up. en.wikipedia.org/wiki/Precompiled_header\n- Don't worry - that's about 10-20 people in the world :)\n- Surely C# and Java are only doing half the job they compile to ByteCode and stop leaving the rest of the job for when the code is run. C++ is doing everything in one go. Other factors such as C++ using huge text based header files for every compile doesn't help either.\n- C# compile + ngen = C++ compile. ngen is fast.","metadata":{"transformedAt":"2026-08-18T18:32:17.788Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":27,"estimatedTokens":436}}360{"id":"stack-1741368","source":"stackoverflow","questionId":1741368,"title":"A language that doesn't use 'C'?","tags":["c","compiler-construction","programming-languages"],"text":"Title: A language that doesn't use 'C'?\nTags: c, compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nJust curious. I may be wrong, but as far as I know, most languages are *created* using **'C'** sources. \n\nFor example: perl , php , python, java(?), go ...\n\nIs there any language that doesn't use **C** as a low level interpreter/compiler ? (fortran ?)\n\n========================================\n\nTop Answer:\nIf you write a language B in A, it's possible to write later a compiler for B in B.\n\nCheck out Bootstrapping\n\n Bootstrapping is a term used in\n computer science to describe the\n techniques involved in writing a\n compiler (or assembler) in the target\n programming language which it is\n intended to compile. This technique is\n also called self-hosting.\n\n \n A large proportion of programming\n languages are bootstrapped, including\n **BASIC, C, Pascal, Factor, Haskell,**\n **Modula-2, Oberon, OCaml, Common Lisp,**\n **Scheme, Clojure, and more.**\n\n**Edit:** taking account of the comments:\n\n- Haskell's runtime system is still a pretty large chunk of C\n\n- Clojure isn't (yet) bootstrapped, but it's implemented in Java\n\n========================================\n\nComments:\n- Yes for Java, I've delved into the OpenJDK sources and they're definitely C.\n- Thank you for all those interesting answers. As there is not one and only one solution, I'll give a point to all the good answers.\n- @Arthur - It think FreeBASIC is self-hosting at the moment, although that will change once it becomes a gcc front-end\n- There is also an Python implementation in Java: Jython\n- However, PyPy generates C code in order to be fast enough for daily usage. ;)\n- All the Python implementations boil down to a C-based interpreter or virtual machine\n- The gcc/java/fortran gcc compiler is written in C, outputs assembler that's assembled with gas(also written in C)\n- And what language was it written in? I mean, the first version, not the bootstrapped.\n- Spice Lisp begat CMUCL, and CMUCL begat SBCL. Spice Lisp, it seems, was written in microcode (for the Perq). From: cons.org/cmucl/doc/cmucl-history.html\n- You need sbcl to compile sbcl. Now thats recursion :)\n- I'm validating the most voted question one year after I asked my question :-)\n- That doesn't really answer the question.\n- Haskell's runtime system is still a pretty large chunk of C\n- Clojure isn't (yet) bootstrapped, it's core is still implemented in Java, not Clojure. Also, the JVM is itself written in C(++).\n- @Robert Gamble, above is a list of bootstrapped languages, aka languages that don't use C.\n- Not true... This only applies if B is sufficiently functional. I could write a language in C whose only commands are print and goto; this language could not be used to write an independent implementation of itself.\n- @Dave Sherohman, sorry I wasn't clear. By \"it's possible to write later\" I meant if you design/evolve B to be sufficiently functional for that, as you mentioned.\n- Delphi was written in Turbo Pascal and that was written in Assembler. The code generator upto version 3 was still in assembler. Even Borlands C++ Builder aka Turbo C was written in assembler.\n- MSVC's compiler is effectively `C1.DLL`, which links against both the C++ CRT and .Net.\n- ghc only generates c if you specifically ask for it (-fvia-C) . Most of the time it just generates object code directly.\n- \"Clojure, Scala and Groovy\" - and of course JRuby and Jython are non-C implementations of Ruby and Python. They just aren't how those two languages were first created.\n- @Steve 'onebyone' Jessop Well, true. I listed languages whose first implementations themselves were not in C.\n- Another example is bootstrapping deliberately gone wrong. See: cm.bell-labs.com/who/ken/trust.html\n- search.cpan.org/perldoc?v6 Perl 6 on Perl 5 never made it past the prototype stage, as far as I can tell. For a while, the best implementation was Pugs: written in Haskell, mainly an interpreter but also able to compile to Perl 5, JavaScript, and Parrot; now efforts are concentrated on Parrot and Rakudo.\n- The fact that there are C headers for what is basically a general procedural API doesn't make having C for this layer necessary.","metadata":{"transformedAt":"2026-08-18T18:32:17.788Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":67,"estimatedTokens":1048}}361{"id":"stack-1419269","source":"stackoverflow","questionId":1419269,"title":"What happens when I compile?","tags":["compiler-construction"],"text":"Title: What happens when I compile?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI wonder what is compiling, what happens when you compile? I mean yes you press compile or type in in the console but what does it actually do in the \"background\"?\n\n========================================\n\nTop Answer:\nSee this\n\nBasically, magic elves and fairies turn human readable code into machine code.\n\n========================================\n\nComments:\n- The target often is, but does not have to be, machine code. The compilers I work on compile to C code - we then use the native C compiler to finish the job of compiling to machine code. Another of them generates a p-code (pseudo machine code - where do the hyphens go?); the result is then interpreted by an interpreter which uses regular C (machine code) functions to execute the p-code instructions. A Java compiler generates byte code for the JVM - that is not machine code either.\n- The 'or' is mildly misleading - compared with what? Your suggested reading is quite good, but you should make your answer stand more nearly on its own. Then you'll get the up-votes...\n- It was in response to the first answer I found for this question, which was simply \"This.\" as a link to Wikipedia's article on compilers.\n- thank you, \"(every student of computer science should study this book).\" I was a student of computer science .. I never really knew about that book and I was more focused on programming .. data structures, objects bla bla .. never really wondered \"beyond\"\n- Dragon book\" is not as gory as it could be. Last time I checked, it mentioned garbage collection no more than couple of times and provided absolutely no information about exception handling. Or I miss them?\n- Don't know what is included in the dragon book, but Modern Compiler Implementation in Java (cs.princeton.edu/~appel/modern/java) has all you mentioned and then some. It was what we used in our course at college.","metadata":{"transformedAt":"2026-08-18T18:32:17.788Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":23,"estimatedTokens":488}}362{"id":"stack-1498714","source":"stackoverflow","questionId":1498714,"title":"Why compiler provides default copy constructor","tags":["c++","compiler-construction"],"text":"Title: Why compiler provides default copy constructor\nTags: c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI wanted to know Why compiler provides default copy constructor..Whats the strategy behind that idea.\n\nThanks in Advance.\n\n========================================\n\nTop Answer:\nFrom The C++ Programming Language, Section 11.3.4 Copying\n\n ...for types where the default copy constructor has the right semantics, I prefer to rely on that default. It is less verbose than anything I can write, and people should understand the default. Also, compilers know about the default and its possible optimization opportunities. Furthermore, writing out the memberwise copy by hand is tedious and error-prone for classes with many data members.\n\nBasically, I read that as the default copy constructor saves you the effort, saves you from making errors caused by tedium, and helps optimize your code by removing the the temptation to optimize it by hand (by letting the compiler do it).\n\n========================================\n\nCode:\n```text\nstruct\n```\n\n```text\nclass C {\npublic:\n  C (C const &) = default;\n};\n```\n\n```text\n=delete\n```\n\n```text\n=default\n```\n\n```text\n=default\n```\n\n========================================\n\nComments:\n- The general answer to why the compiler does anything is that it has to implement the rules of the language. That applies here, too.\n- You could have the compiler complain about the lack of a copy constructor, I suppose.\n- Then I think you would have some backward compatibility issues. A class and a struct are similar. In C++, a struct is nothing but a class with methods and attributes public by default. This means that when you declare a struct from a C program compiled as a C++ one, there's an implicit copy constructor that you have to assume it's there for backward compatibility. Move this to classes, and you have the situation we know.\n- You could say that once any ctor is defined, a copy ctor isn't provided by default anymore. This would still be compatible with C.\n- Stroustrup could have defined an additional difference between class and struct, in that struct has a default copy ctor and class doesn't. Too late for that now though.\n- a base class such as boost::noncopyable achieves the same thing more succinctly\n- I would best like it like this: Providing a default ctor by default, but only if there is no other ctor. If there is, one can do `= default` like Richard shows for c++0x, and have still good performance. What do you think about it?\n- @Phil - I agree, but my snippet currently still uses the 'old-fashioned' method because of inertia/laziness (of the ain't really broke, so no effort goes into fixing variety) and it has no dependecies. Using a private noncopyable base class means the .cpp file needs to #include \"noncopyable.h\". Might not be a great excuse, but it's what I have. Note that I do often clean this up to use a noncopyable base, because I do think it's a better way. I really should fix my snippet.\n- @litb -are you talking about the default ctor or the compiler generated copy ctor? My understanding of C++0x is that that what you're describing will be the behavior for default ctors. Copy ctors will still be generated automatically if needed unless explicitly disabled (using one of the existing techniques or using the `delete` keyword). I imagine that there is too much existing code that depends on this to make a more radical change possible. Then again, I'm no C++0x expert, so I might just plain be wrong.\n- oh i'm sorry i meant \"copy ctor\" -.-\n- @litb: I'm not sure what reason there would be against your idea that auto-genned copy ctors are disabled when there's any explicitly defined ctor (like the default ctor is in that case). I can't think of a reason off the top of my head. Obviously it would be difficult/impossible to do at this point, but it might have been a good middle ground if it had been done initially...\n- The new C++0x `default` and `delete` for compiler-generated constructors and special functions will be welcome. However, note that for the copy-ctor case my understanding is that the compiler will still generate one by default in all the same cases as it does today - the `delete` keyword is a useful, simple way to prevent that (in other words, there will be little need for `default` to be used for the copy-ctor). Pesky backwards compatibility.\n- @Michael Burr: Yes, you're right. The existing behaviour of compilers regarding implicit default members will remain unchanged. As I said in the answer, the only glimmer of hope you have is that '=delete' and '=default' are manna from heaven for coding standards (and of course static analysis tools). Their presence allows tools to highlight classes that the developer did not explicitly intend to copy (ie. using defaulted copy constructor).\n- @Richard: that would indeed be a nice added diagnostic tool.\n- That saying is quite a stretch from what he says in \"Design and Evolution of C++\", hmm.\n- @litb: The two passages do seem to be somewhat at odds. Maybe Stroustup originally included them reluctantly, then embraced them later?\n- I don't think it conflicts with what is said in D&E. Note that the statement here is qualified by \"where the default copy contructor has the right sematics\". What's said in D&E is that he often explicitly disables the auto-generated copy ctor because it often doesn't have the right sematics, and that he would have liked that the default copy ctor wasn't automatically generated (but his hands were tied). In CPL he's saying that *if* you're just going to copy bits, let the compiler do it for you.\n- They don't contradict up-front i think, but these two sentences are quite at odd, i think: \"I personally consider it unfortunate that copy operations are defined by default\" - \"Also, compilers know about the default and its possible optimization opportunities. Furthermore, writing out the memberwise copy by hand is tedious and error-prone for classes with many data members.\". @Michael, in the end i think you are right and in CPL, his sayings are based on status quo, while in the design book he shows us what he really thinks about that status.\n- @Michael Burr: I think the difference in wording between \"I personally consider it **unfortunate** that copy operations are defined by default...\" and \"...I **prefer** to rely on that default.\" (emphasis mine) is what's confusing. I agree that the qualification of \"...where the default copy constructor has the right semantics...\" pretty much nullifies the perceived conflict.\n- The idiom to disable is short, sweet and well understood. One glance and a reader knows what's intended. If defaults *weren't* part of C++, hand-coding the current default behavior *isn't* short and sweet. Readers would need to verify that every field is indeed copied or whatever. IMHO this decision went the more prudent way.","metadata":{"transformedAt":"2026-08-18T18:32:17.788Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":67,"estimatedTokens":1719}}363{"id":"stack-440161","source":"stackoverflow","questionId":440161,"title":"Dll compatibility between compilers","tags":["c++","dll","compiler-construction","binary-compatibility"],"text":"Title: Dll compatibility between compilers\nTags: c++, dll, compiler-construction, binary-compatibility\nSource: Stack Overflow\n\nQuestion:\nIs there some way to make c++ dlls built with diffrent compilers compatible with each other? The classes can have factory methods for creation and destruction, so each compiler can use its own new/delete (since diffrent runtimes have there own heaps).\n\nI tried the following code but it crashed on the first member method:\n\ninterface.h\n\n```\n#pragma once\n\nclass IRefCounted\n{\npublic:\n virtual ~IRefCounted(){}\n virtual void AddRef()=0;\n virtual void Release()=0;\n};\nclass IClass : public IRefCounted\n{\npublic:\n virtual ~IClass(){}\n virtual void PrintSomething()=0;\n};\n```\n\ntest.cpp compiled with VC9, test.exe\n\n```\n#include \"interface.h\"\n\n#include \n#include \n\nint main()\n{\n HMODULE dll;\n IClass* (*method)(void);\n IClass *dllclass;\n\n std::cout PrintSomething();//crash: Access violation writing location 0x00000004\n dllclass->Release();\n FreeLibrary(dll);\n\n std::cout a.cpp compiled with g++\ng++.exe -shared c.cpp -o c.dll\n\n```\n#include \"interface.h\"\n#include \n\nclass A : public IClass\n{\n unsigned refCnt;\npublic:\n A():refCnt(1){}\n virtual ~A()\n {\n if(refCnt)throw \"Object deleted while refCnt non-zero!\";\n std::cout EDIT:\nI added the following line to the GCC CreateClass method, the text was correctly printed to the console, so its defenatly the function call thats killing it. \n\n```\nstd::cout I was wondering, how does COM manage to maintain binary compatibility even across languages, since its basicly all classes with inheritence (although only single) and therefore virtual functions. I'm not massivly bothered if I cant have overloaded operators/functions as long as I can maintain the basic OOP stuff (ie classes and single inheritence).\n\n========================================\n\nTop Answer:\nYou're almost certainly asking for trouble if you do this - while other commenters are correct that the C++ ABI may be the same in some instances, the two libraries are using different CRTs, different versions of the STL, different exception throwing semantics, different optimizations... you're heading down a path towards madness.\n\n========================================\n\nCode:\n```text\n#pragma once\n\nclass IRefCounted\n{\npublic:\n    virtual ~IRefCounted(){}\n    virtual void AddRef()=0;\n    virtual void Release()=0;\n};\nclass IClass : public IRefCounted\n{\npublic:\n    virtual ~IClass(){}\n    virtual void PrintSomething()=0;\n};\n```\n\n```text\n#include \"interface.h\"\n\n#include <iostream>\n#include <windows.h>\n\nint main()\n{\n    HMODULE dll;\n    IClass* (*method)(void);\n    IClass *dllclass;\n\n    std::cout << \"Loading a.dll\\n\";\n    dll = LoadLibraryW(L\"a.dll\");\n    method = (IClass* (*)(void))GetProcAddress(dll, \"CreateClass\");\n    dllclass = method();//works\n    dllclass->PrintSomething();//crash: Access violation writing location 0x00000004\n    dllclass->Release();\n    FreeLibrary(dll);\n\n    std::cout << \"Done, press enter to exit.\" << std::endl;\n    std::cin.get();\n    return 0;\n}\n```\n\n```text\n#include \"interface.h\"\n#include <iostream>\n\nclass A : public IClass\n{\n    unsigned refCnt;\npublic:\n    A():refCnt(1){}\n    virtual ~A()\n    {\n        if(refCnt)throw \"Object deleted while refCnt non-zero!\";\n        std::cout << \"Bye from A.\\n\";\n    }\n    virtual void AddRef()\n    {\n        ++refCnt;\n    }\n    virtual void Release()\n    {\n        if(!--refCnt)\n            delete this;\n    }\n\n    virtual void PrintSomething()\n    {\n        std::cout << \"Hello World from A!\" << std::endl;\n    }\n};\n\nextern \"C\" __declspec(dllexport) IClass* CreateClass()\n{\n    return new A();\n}\n```\n\n```text\nstd::cout << \"C.DLL Create Class\" << std::endl;\n```\n\n```text\nstruct IClassVTable {\n    void (*pfIClassDTOR)           (Class IClass * this) \n    void (*pfIRefCountedAddRef)    (Class IRefCounted * this);\n    void (*pfIRefCountedRelease)   (Class IRefCounted * this);\n    void (*pfIClassPrintSomething) (Class IClass * this);\n    ...\n};\nstruct IClass {\n    IClassVTable * pVTab;\n};\n(((struct IClass *) dllclass)->pVTab->pfIClassPrintSomething) (dllclass);\n```\n\n```text\ndllclass->PrintSomething();\n```\n\n```text\nextern \"C\"\n```\n\n```text\ndelete this\n```\n\n```text\n__declspec(dllexport) void PrintSomething()\n{\n    (A::Instance())->PrintSometing();\n}\n```\n\n```text\nvirtual ~IRefCounted(){}\n    ...\nvirtual ~IClass(){}\n```\n\n========================================\n\nComments:\n- How does COM do it? With lightweight RPC calls - you could build your app using dce-rpc and you'd get the same results. In no case does COM provide a pointer to an external dll's memory, it makes function calls to that dll.\n- The following articles here, here, and here may be helpful. Your code example is almost there except for the inlined virtual destructor. AFAIK, all methods in your abstract interface must be pure virtual `=0`.\n- No, the VC debugger showed a reasomable value for the pointer, also as far as I can tell (Ive never tried to debug a non VC binary with VC before) it never actauly got into the PrintSomething method, at least the Stack Frame indicates it never entered the dll at this point.\n- When you're debugging code that wasn't built with VC -- or even when you are but don't have the debug symbols -- you can't completely trust what the debugger is telling you about the call stack.\n- How can I check if cout it being correctly created in the GCC dll using VC? Also how exactly is COM working through __stdcall, since I thaught even basic classes had to work through __thiscall under VC?\n- Id rather not have to use just plain functions everywhere, tbh Id just tell everyone they must compile the dll's with VC9 before I took that step...\n- You might be able to do what you want with CORBA, but I don't know much about it.\n- @Shy: Application Binary Interface.\n- Ok, is there some way to do this at least party automaticaly, rather than writing like 4 things for every method (loading the C method so the interface can find it, translating the call to the interface to a C method, going from the C method to an oop method in the dll, and finally the dll method?\n- You could partly automate this. You would need to keep a file list of the classes and methods. A script could process that file, generating the files to be #included where needed. Keep in mind though, that the C dll functions would not be able to directly accept C++ instance* parameters.","metadata":{"transformedAt":"2026-08-18T18:32:17.788Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":212,"estimatedTokens":1596}}364{"id":"stack-399792","source":"stackoverflow","questionId":399792,"title":"Inadvertent use of = instead of ==","tags":["c++","c","compiler-construction","programming-languages"],"text":"Title: Inadvertent use of = instead of ==\nTags: c++, c, compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nIt seems that \n\n```\nif (x=y) { .... }\n```\n\ninstead of\n\n```\nif (x==y) { ... }\n```\n\nis a root of many evils.\n\nWhy don't *all* compilers *mark it as error* instead of a configurable warning?\n\nI'm interested in finding out cases where the construct `if (x=y)` is useful.\n\n========================================\n\nTop Answer:\nOne useful construct is for example:\n\n```\nchar *pBuffer;\nif (pBuffer = malloc(100))\n{\n // Continue to work here\n}\n```\n\nAs mentioned before, and downvoted several times now, I might add this is not specially good style, but I have seen it often enough to say it's useful. I've also seen this with `new`, but it makes more pain in my chest.\n\nAnother example, and less controversial, might be:\n\n```\nwhile (pointer = getNextElement(context))\n{\n // Go for it. Use the pointer to the new segment of data.\n}\n```\n\nwhich implies that the function `getNextElement()` returns `NULL` when there is no next element so that the loop is exited.\n\n========================================\n\nCode:\n```text\nif (x=y) { .... }\n```\n\n```text\nif (x==y) { ... }\n```\n\n```text\nif (x=y)\n```\n\n```text\nvoid *ptr = calloc(1, sizeof(array));\n   if (NULL = ptr) {\n       // Some error\n   }\n```\n\n```text\nchar *pBuffer;\nif (pBuffer = malloc(100))\n{\n    // Continue to work here\n}\n```\n\n```text\nwhile (pointer = getNextElement(context))\n{\n    // Go for it. Use the pointer to the new segment of data.\n}\n```\n\n```text\nnew\n```\n\n```text\ngetNextElement()\n```\n\n```text\nNULL\n```\n\n```text\nif ( (x = read(blah)) > 0)\n{\n    // now you know how many bits/bytes/whatever were read\n    // and can use that info. Esp. if you know, say 30 bytes\n    // are coming but only got 10\n}\n```\n\n```text\n~> gcc -c -Wall foo.c\nfoo.c: In function ‘foo’:\nfoo.c:5: warning: suggest parentheses around assignment used as truth value\n```\n\n```text\nif ( x = (X*) malloc( sizeof(X) ) {\n   // 'malloc' worked, pointer != 0\n\nif ( pid = fork() ) {\n   // Parent process as pid != 0\n```\n\n```text\nif ( 0 == variable ) {\n   // The compiler will complaint if you mistakenly\n   // write =, as you cannot assign to a constant\n```\n\n```none\n0 == CurrentItem\n```\n\n```none\nCurrentItem == 0\n```\n\n```text\nx = y; if (x) ...\n```\n\n```text\nif (x = y) ...\n```\n\n```text\nif ((x = y) != 0) ...\n```\n\n```text\np = malloc(4711); if (p != NULL) printf(\"Ok!\");\n```\n\n```text\np = malloc(4711); if (p) printf(\"Ok!\");\n```\n\n```text\nif (p = malloc(4711)) printf(\"Ok!\");\n```\n\n```text\nif ((fp = fopen(\"foo.txt\", \"r\") == NULL))\n```\n\n```text\nif (NULL == (fp = fopen(...)))\n```\n\n```text\n#include <assert.h>\n\n...\nfp = fopen(\"foo.txt\", \"r\");\nassert(fp != NULL);\n...\n```\n\n```text\nif (i = foo)\n```\n\n```text\nif (i == foo)\n```\n\n```text\nassert (i > foo + 1)\n```\n\n```text\nassert()\n```\n\n```text\nassert()\n```\n\n```text\nfp == NULL\n```\n\n```text\nabort()\n```\n\n```text\nlist_elem* curr;\nwhile ( (curr = next_item(list)) != null ) {\n  /* ... */\n}\n```\n\n```text\nvoid my_strcpy(char *dst, const char *src)\n{\n    while((*dst++ = *src++) != '\\0') { // Note the use of extra parentheses, and the explicit compare.\n        /* DO NOTHING */\n    }\n}\n```\n\n```text\nint i;\nfor(i = 0; i < 42; ++i) {\n    printf(\"%d\\n\", i);\n}\n```\n\n```text\nchar *newstring = malloc(strlen(src) * sizeof(char));\nif(newstring == NULL) {\n    fprintf(stderr, \"Out of memory, d00d!  Bailing!\\n\");\n    exit(2);\n}\n\n// Versus:\n\nif((newstring = malloc(strlen(src) * sizeof(char))) == NULL) // ew...\n```\n\n```text\nif(*src == *dst)\n```\n\n```text\nif\n```\n\n```text\n==\n```\n\n```text\nlint\n```\n\n```text\nif( life_is_good() )\n    enjoy_yourself();\n```\n\n```text\nif( tmp = life_is_good() )\n    enjoy_yourself();\n```\n\n```text\nif ( true == $x )\n```\n\n```text\n$x\n```\n\n```text\ntrue\n```\n\n```text\nif(type * t = get_pointer()) {\n    // ....\n}\n```\n\n```text\nbool\n```\n\n```text\nif(int i = some_fn())\n{\n   another_fn(i);\n}\n```\n\n```text\nfor\n```\n\n```text\nif (5 == x) { whatever(); }\n```\n\n```text\nif (5 = x)\n```\n\n```text\nif (x = y) {\n```\n\n```text\nwhile((count = read(foo)) > 0) {\n    //Do stuff\n}\n```\n\n```text\nwhile(1) {\n    count = read(foo);\n    if(!(count > 0))\n        break;\n    //...\n}\n```\n\n```text\ncount = read(foo);\nwhile(count > 0) {\n    //...\n    count = read(foo);\n}\n```\n\n```text\nread()\n```\n\n```text\nif(i >> 3)\n{\n   std::cout << \"i is less than 8\" << std::endl\n}\n```\n\n```text\nif(MyObject * pObject = dynamic_cast<MyInterface *>(pInterface))\n{\n   pObject->doSomething();\n}\n```\n\n```java\nsynchronized(p)\n{\n   // Now, the Java code is synchronized using p as a mutex\n}\n```\n\n```text\n#define synchronized(lock) \\\n   if (auto_lock lock_##__LINE__(lock))\n\nsynchronized(p)\n{\n   // Now, the C++ code is synchronized using p as a mutex\n}\n```\n\n```text\nvoid doSomething(char * p)\n{\n   strcpy(p, \"Hello, World! How are you \\?\\n\");\n}\n\nvoid doSomethingElse()\n{\n   char buffer[16];\n   doSomething(buffer);\n}\n```\n\n```text\nif(0 = p) // ERROR: It should have been if(0 == p). IT WON'T COMPILE!\n```\n\n```text\nvoid doSomething(char * p)\n{\n   if(p == NULL) // POSSIBLE TYPO ERROR\n      return;\n\n   size_t length = strlen(p);\n\n   if(length == 0) // POSSIBLE TYPO ERROR\n      printf(\"\\\"%s\\\" length is %i\\n\", p, length);\n   else\n      printf(\"the string is empty\\n\");\n}\n```\n\n```text\nvoid doSomething(const char * const p) // CONST ADDED HERE\n{\n   if(p == NULL) // NO TYPO POSSIBLE\n      return;\n\n   const size_t length = strlen(p); // CONST ADDED HERE\n\n   if(length == 0) // NO TYPO POSSIBLE\n      printf(\"\\\"%s\\\" length is %i\\n\", p, length);\n   else\n      printf(\"the string is empty\\n\");\n}\n```\n\n```text\nif\n```\n\n```text\nwhile (*dest++ = *src++);\n```\n\n```text\nif ((x = y) != 0)\n```\n\n```text\nif ((x = y))\n```\n\n```text\nif ((k==1) || (k==2)) is a conditional\n if ((k=1)  || (k=2) ) is BOTH a conditional AND an assignment statement\n```\n\n```text\nRegEx r;\n\nif(((r = new RegEx(\"\\w*)).IsMatch()) {\n   // ... do something here\n}\nelse if((r = new RegEx(\"\\d*\")).IsMatch()) {\n   // ... do something here\n}\n```\n\n```text\nint i = 0;\nif((i = 1) == 1) {\n   // 1 is equal to i that was assigned to a int value 1\n}\nelse {\n   // ?\n}\n```\n\n========================================\n\nComments:\n- I think that this is really two different questions, and you should clarify which one of them that you mean to ask: (1) does this construct really mean anything, and in that case what, and (2) if it is more dangerous than useful, why is it still legal.\n- @Thomas : You are right , I'm more interested in the (2). Thanks for verbalizing it.\n- Consider augmenting your compiler with static analysis tools that detect such \"evil\" code, e.g. lint.\n- Should'nt one be doing this instead? : pBuffer = malloc(100 * sizeof(char)); if(ip == NULL) { printf(\"out of memory\\n\"); exit or return }\n- Any seasoned programmer knows about the explicit test for 0/NULL, but you could do this to clarify: if ( (p = malloc(100*...)) != NULL )\n- @Learning: In the nowadays most unusual case where you are really out of memory, where do you think printf() and following calls will get memory from to bail out with the mentioned error message ?\n- people do it so the compiler need to support it. Even if it is not a good idea.\n- @llmbus: now days for a malloc to fail it would normally need to ask for a huge block of ram, I don't think printf needs anywhere near \"huge\"\n- @S.Lott: there are worse, and there are ziggatons of existing code that do so.\n- But this is not the case , the check for if here is \">\" and not \"=\" ? Or am I missing something?\n- he is first assigning to x, which is the same as if (x = 0)\n- That's correct. I didn't realise you meant an assignment only rather than simply somewhere within the construct. It would be an interesting grammar that didn't allow this though :-)\n- (This question was duplicated about 5 years later.)\n- ok ... backward compatibility makes sense. But would it not break code which perhaps was already broken to begin with? (I'm assuming that = was used instead of ==) Would'nt that be a good thing?\n- Most of the time those bugs will be found. You wou ld break a lot of code that has no errors.\n- @Learning - modern compilers issue a warning for `if (x=y)`, which helps\n- @Learning, In addition, you can tell compilers to treat warnings as errors (which is almost always a good thing!), thus if(x = y) is an error. If you mean to do if(x = y), you can wrap the expression in another set of parentheses: if((x = y)). It's clearer on your intent that way.\n- It's not about backwards compatibility, it's about correctness, sticking to how the language is defined. A C++ compiler which flags this as an error *is no longer a C++ compiler*.\n- I think (Relatively) modern languages like ruby allow x=y in conditionals, so it isn't just backwards compatibility.\n- These days, `if (auto foo = bar(); foo == 42) { &#47;* ... *&#47; }` is also an option.\n- +1 for the fork example. I normally keep my malloc on its own line or with the variable declaration, which is much more readable. Then I test using assert or if(x) { ... }.\n- +1 for fork. But are'nt we masking an error condition here where fork() throws an error?\n- @Learning: You can still check if pid == -1 inside the if block.\n- Yes, if you are in the else part you are guaranteed that the fork worked and you are in the child process. In the else part you still need to check for failure but you have the pid variable for it\n- Not only warns, but throws an error at you. Except in the case when you're comparing to lvalues (e.g. *p == *q).\n- That makes sense. But still begs for the question on why allow it? Which is a genuine case where an = within an if construct makes sense?\n- I posted an answer using your variables x and y that explains how it works. And if you look at the answer by lImbus, there is a genuine and very common case where it makes sense to use it, and where it is often used.\n- I've done it. Fortunately, any good compiler will issue a warning.\n- ...and it is a perfectly valid and useful C construct.\n- I think it is a common error for someone that learned Python, BASIC, MATLAB, or similar first.\n- In your \"if (x=y)\" example why do you need an if?\n- I need the \"if\" in my second code snippet if I want it to do the same thing as my first code snippet. The first code snippet contained an \"if\", so there should be one in the second one too. But I've added another example, with memory allocation, to try to clarify.\n- `if ((x = y) != 0)` or alternatively `if (x = y; x != 0)`.\n- ... and as clearer pieces of code, in my opinon. Clarity is more important (for debugging and maintaining) than the number of bytes the source occupies. See golfing competitions. =]\n- Ah, but clarity is in the eye of the beholder. The malloc examples show a common idiom.\n- In addition, if code is occupying fewer overall lines, then more of the code is visible in one \"screenful\", which can sometimes aid comprehension.\n- The malloc \"idiom\" posted increases the indent level (because you now have another block with a valid alloc'd pointer). In what cases would doing something only if there is memory available (and no problem otherwise) be preferred? I haven't seen this used ever...\n- All of the code in the \"normal\" path will tend to float up and group together under this idiom, whilst making slow progress to the right. As opposed to having 4-5 lines of error handling in the middle of the normal path, to let you forget what's being achieved\n- I should really add, code riddled with asserts() needs to be tested with a -DNDEBUG compiler flag prior to being released .. if only to take out useless calls. This is especially true if you use an inline self made assert() function in lieu of the provided macro, in most implementations.\n- You are unfortunately abusing assertions. Assert statements are to detect things that should not happen, and are programming errors. Using assertions to check for success of fopen is broken.\n- I don't use assertions to check for the success of fopen() :) I check the pointer to the stream for NULL. It was simply an example.\n- oops, didn't notice the question was just about an 'if' statement.\n- Same concept really, assignment in a conditional.\n- Completely agree. I mentioned this in my own answer as well.\n- In 20+ years of writing C & C++ code this has bitten me only a handful of times and was fairly easy to spot (especially, once I learned to set the warning level to the max).\n- You can make the while loop even shorter by scrapping the curly braces and adding a semicolon. I prefer that formulation, but some find it confusing.\n- I was about to flame you, but then I realized the usefulness of this. It really helps when things are put into context. However, why would you need to store `tmp` in your example? Perhaps you can adjust it...\n- It's often a false sense of security though. What if you have if ($x == $y)? How do you reorder that to force an error? The only real answer is to just learn not to make this mistake.\n- Not to mention that comparing things to true is error-prone. In most languages that don't confine truth to a simple boolean type, there are multiple true values, and only one can be assigned to \"true\". Consider defining true as 1, and $x is 2, for example.\n- I'd agree this is the best use of assignment in a conditional, but it doesn't really apply to 'if' statements (as asked by the OP) as well as 'while' loops.\n- Oops, I forgot it was about if-statements. Then nothing of this is a problem, of course.\n- The worst part is when you start seeing this kind of thing in languages where using = would be a syntax error anyway, such as C# or Python.\n- As a side note, I did swapped to C-like syntaxed languages exactly because of this kind of \"free\" notation (the \"for\" notation, to be more exact, so I could be somewhat biased)... ^_^","metadata":{"transformedAt":"2026-08-18T18:32:17.788Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":70,"totalLines":513,"estimatedTokens":3427}}365{"id":"stack-34382260","source":"stackoverflow","questionId":34382260,"title":"How is a variable name stored in C?","tags":["c","compiler-construction"],"text":"Title: How is a variable name stored in C?\nTags: c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI want to ask how C the variables are stored in C?\n\nTo be more clear consider the following code:\n\n```\nint main() {\n int a = 1, b;\n b = a + 2;\n return 0;\n}\n```\n\nFor example here in what memory C stores the names of variable places.\n\neg if `&a=0x12A7`(suppose) `&b=0x123B1`, then how and where does c stores the variable names like in which memory name `a` is stored?\n\n========================================\n\nTop Answer:\nVariable names need not be stored at all! The compiler can get rid of them entirely. Imagine, if the compiler is quite clever, it can reduce your entire program to this:\n\n```\nint main(){\n return 0;\n}\n```\n\nNote that the effect of this program is exactly the same as your original, and now there are no variables at all! No need to name them now, is there?\n\nEven if the variables in your code were actually used, their names are purely a convenient notation when you write the program, but aren't needed by the processor when it executes your code. As far as a microprocessor is concerned, a function like this:\n\n```\nint foo(int x, int y) {\n int z = x + y;\n return z * 2;\n}\n```\n\nMight result in compiled code that does this, in some hypothetical simple instruction set architecture (ISA):\n\n```\nADD # consumes top two values on stack (x and y), pushes result (z)\nPUSH 2 # pushes 2 on stack\nMULT # consumes top two values on stack (z and 2), pushes result\nRET\n```\n\nThe longer story is that variable names are sometimes stored for debugging purposes. For example if you're using GCC you can pass the `-g` option to emit a \"symbol table\" which contains things like variable names for debugging. But it isn't needed simply to run a program, and it isn't covered by the language standard--it's an implementation feature which differs by platform.\n\n========================================\n\nCode:\n```text\nint main() {\n    int a = 1, b;\n    b = a + 2;\n    return 0;\n}\n```\n\n```text\n&a=0x12A7\n```\n\n```text\n&b=0x123B1\n```\n\n```text\na\n```\n\n```text\n// Declare an external function\nextern double bar(double x);\n\n// Define a public function\ndouble foo(int count)\n{\n    double  sum = 0.0;\n\n    // Sum all the values bar(1) to bar(count)\n    for (int i = 1;  i <= count;  i++)\n        sum += bar((double) i);\n    return sum;\n}\n```\n\n```text\nint main(){\n  return 0;\n}\n```\n\n```text\nint foo(int x, int y) {\n  int z = x + y;\n  return z * 2;\n}\n```\n\n```text\nADD # consumes top two values on stack (x and y), pushes result (z)\nPUSH 2 # pushes 2 on stack\nMULT # consumes top two values on stack (z and 2), pushes result\nRET\n```\n\n```text\n-g\n```\n\n========================================\n\nComments:\n- A symbol table is a compiletime construct, it doesn't reflect runtime properties. Map files and debug info are sources available to get an idea of symbol names for locations, but they were not meant to be used from within the program.. RTTI and other forms of introspection are, but C doesn't have any form of it","metadata":{"transformedAt":"2026-08-18T18:32:17.788Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":121,"estimatedTokens":753}}366{"id":"stack-461099","source":"stackoverflow","questionId":461099,"title":"General Purpose Language to build a compiler for","tags":["compiler-construction"],"text":"Title: General Purpose Language to build a compiler for\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nInspired by Eric Sink's interview on the stackoverflow podcast I would like to build a full compiler in my spare time for the learning experience. My initial thought was to build a C compiler but I'm not sure whether it would take too much time.\n\nI am wondering if there is a smaller general purpose language that would be more appropriate to implement as a first compiler effort? Or is a C implementation doable on a reasonable timescale (200 hrs)?\n\nIt is my intention to target the CLR.\n\n========================================\n\nTop Answer:\nYou'll be happiest writing compilers for older, smaller languages. Pascal, for example, were designed as learning tools. The Pascal language is small and elegant; the compiler can be written fairly simply. \n\nEven an Oberon or Modula-2 compiler is similar in complexity to Pascal; their design was driven by the same person, Niklaus Wirth.\n\nLanguages like C, which evolved organically, are too full of quirks to be good learning experiences.\n\n========================================\n\nComments:\n- I have to disagree on this one. The challenges involved in implementing a Scheme compiler are drastically different than that of a language like C, Python, etc. If you're wanting to learn the internals of a compiler for anything outside of the Lisp world, Scheme just isn't a good choice.\n- @squadette: Thanks. This paper looks awesome. While the Pascal suggestion is more what I was thinking of I'm going to have a look at doing this as well. If nothing else hopefully I would come out of it know Lisp a lot better.\n- One thing that a Scheme compiler would \"gloss over\" is the lexing and parsing phase. Scheme doesn't have the common problems of more algebraic expression parsers (operator precendence, etc.) and the complicated grammars.\n- Except if your favorite language is C++ ;)\n- Switching this to the accepted answer. A couple of years later after making several attempts I had the most success implementing a subset of the C# language which is what I use at my day job.\n- Thanks Tom. It is my intention to target the CLR initially. I will add a note to the question.\n- contrary to propaganda; CLR is heavily targeted to a subset of languages, mostly imperative. anything far enough from C, Java, Python, and you'll be missing interesting features best done at low level. ie: continuations, or even closures are non-optimal.\n- +1 Heh, you beat me to Pascal by a couple of seconds. A good choice, IMO.\n- Oberon is even simpler than Modula-2, Modula-3 or Pascal. (Modula-3 is from DEC, The rest are from Niklaus Wirth, who has a gift for simplification.)\n- Pascal was indeed designed as a teaching language, but Modula-2 was designed as a systems implementation language for the operating system and application software of a workstation (called Lilith) that was being developed under the lead of Wirth at ETH Zurich. It was not designed as a teaching language.\n- Pascal was designed to teach algorithm description and implementation, not necessarily as an introduction to programming, what is the current context of a teach language.\n- The link to Wirth's book is broken. Here is another one: http://www.ethoberon.ethz.ch/WirthPubl/CBEAll.pdf.","metadata":{"transformedAt":"2026-08-18T18:32:17.789Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":35,"estimatedTokens":827}}367{"id":"stack-4265716","source":"stackoverflow","questionId":4265716,"title":"How do you write a compiler for a language in that language?","tags":["ruby","bootstrapping","rubinius","compiler-construction"],"text":"Title: How do you write a compiler for a language in that language?\nTags: ruby, bootstrapping, rubinius, compiler-construction\nSource: Stack Overflow\n\nQuestion:\n**Possible Duplicates:**\n\n How can a language's compiler be written in that language?\n\n implementing a compiler in &ldquo;itself&rdquo; \n\nI was looking at Rubinius, a Ruby implementation that compiles to bytecode using a compiler written in Ruby. I cannot get my head around this. How do you write a compiler for a language in the language itself? It seems like it would be just text without anything to compile it into an executable that could then compile the future code written in Ruby. I get confused just typing that sentence. Can anyone help explain this?\n\n========================================\n\nTop Answer:\nRegarding the bootstrapping of a compiler it's worth reading about this devilishly clever hack.\n\nhttp://catb.org/jargon/html/B/back-door.html\n\n========================================\n\nComments:\n- Can you expand on this answer a little? Are you talking about people writing stuff in machine code, or something else?","metadata":{"transformedAt":"2026-08-18T18:32:17.789Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":274}}368{"id":"stack-2374569","source":"stackoverflow","questionId":2374569,"title":"C# Empty Statement","tags":["c#","compiler-construction","programming-languages"],"text":"Title: C# Empty Statement\nTags: c#, compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nThe C# language specification defines the *empty-statement* grammar production, which allows me to do something like this:\n\n```\nstatic void Main(string[] args)\n{\n ; ; ;\n}\n```\n\nWhy would Microsoft include this grammar production in the C# language? Does it have a useful purpose?\n\n========================================\n\nTop Answer:\nI personally would never use an empty statement. If I for some bizarre reason wanted to play code golf and write something like\n\n```\nwhile(Foo(x++)) ;\n```\n\nI'd be more inclined to use {} instead of ; as the \"do nothing statement\". I think that is more clear.\n\nPretty much it's there for historic reasons. There's nothing there that you cannot also do with {}.\n\nUPDATE: I just thought of one possible usage case. In a debug build it allows you to put a breakpoint somewhere that you are guaranteed that you can break at which is not going to have any side effect when you step. If the code surrounding the breakpoint is particularly complicated it might be useful. \n\nUPDATE UPDATE: I am WRONG WRONG WRONG. That doesn't work. It just moves the breakpoint to the next statement. How irksome.\n\n========================================\n\nCode:\n```text\nstatic void Main(string[] args)\n{\n    ; ; ;\n}\n```\n\n```text\nwhile (GetWhitespace(textStream))\n    ;\n```\n\n```text\nfor (i = 0; i < n; v[i++] = 1);\n```\n\n```text\nwhile (testSomeCondition());\n```\n\n```text\nwhile(DoSomething()) ;\n```\n\n```text\nvoid M() {\n    if(someCondition) goto exit;\n    // ...\n    exit: ;\n}\n```\n\n```text\nif (b1)\n   if (b2) else;\nelse\n   //code\n```\n\n```text\nwhile (!SomeCondition)\n    ; // <-- body of the while loop is an empty statement\n```\n\n```text\nwhile(Foo(x++)) ;\n```\n\n```text\nif(true){\n#if(DEBUG)\nSystem.Console.WriteLine(\"Debug comment\");\n#endif\n}\n```\n\n```text\nif(true)\n#if(DEBUG)\n   System.Console.WriteLine(\"Debug comment\")\n#endif\n;\n```\n\n```text\nfor(;;)\n```\n\n```text\nwhile(true)\n```\n\n```text\nvoid DoSomething()\n{\n    // ...\n    NewMethod();\n}\n```\n\n```text\npublic void NewMethod() { }\n```\n\n```text\nNewMethod\n```\n\n```text\nvirtual\n```\n\n========================================\n\nComments:\n- Of course its useful, see thedailywtf.com/Articles/The-Speedup-Loop.aspx\n- The funny thing is, the C# language specification said that a compilation unit is an empty file or .... All code is optional!\n- I would remove that goto label and make `goto exit` a `return;`. This is awful C#.\n- goto has its place, maybe not in Jason's context, but it is perfectly valid.\n- agree, if you want to write gotos may be you decomposed your task not that well\n- Sidenote, I am not anti goto, goto is just one of those things which are to be used very, very carefully.\n- weblogs.asp.net/stevewellens/archive/2009/06/01/&hellip;\n- Actually, I've used the empty statement *exactly* like this in a code generator for a state machine. Sometimes code would the label, but not always - the empty statement suppressed a compiler warning that liked to appear in the latter case.\n- goto is fundemental. Nearly all execution flow logic compiles with goto instructions. Purposely avoiding them leads to confusing logic. Look at Microsoft's .NET souce code and you will find goto is used fairly often.\n- Breaking out of deeply nested loops?\n- To all those who parrot NO GOTO - read the above comments please. Goto is fundamental, and has places it's a good option. For example a return statement in deep loop is a GOTO!\n- @tomfanning, exactly. Goto is a clean way to do this.\n- I agree as well. I've run across some awful code that was made needlessly complex because of a dogmatic avoidance of GOTOs. Besides, successful developers who started with assembler or early FORTRAN used GOTOs through careful and thoughtful programming practices.\n- What's a use case of this? Looks pretty useless.\n- Not for every construct there is a really useful use case. Sometimes it might just be convenient when tweaking code and testing stuff.\n- it is useful. if you want to write else it will be one for innermost if, like bracket. but i want to have else in outer most if.\n- But if you format your code properly and have brackets around your if statements, you won't have this problem at all.\n- i didn't say there is a problem. i showed example of using empty statement, i didn't say that you should use it. also C# compiler will give you a warning for that.\n- Though, golfing is usually by character, and `{}` is 1 more character than `;`... :p\n- BTW, you can't set a breakpoint at an empty statement. It skips over the breakpoint and brings you to the next non-empty statement. Just tried it...\n- If you have a second Eric could you add a comment to my answer on this question whether I'm on base or not.\n- Love the intentions behind the corrections.\n- Microsoft say it can be used for null body as i did already, but my code base tool (resharper) say use empty method instead, also people say `while();` is like `while(){;}` which is more code than `while(){}` so what is what and is there any performance overheat?\n- Yes it does, {} looks pretty empty to me, about as empty as {;}, the {} acts as a block marker, so does the ; in the second case.\n- While {} is \"a statement that doesn't do anything\", it's not the C# \"empty statement\" as defined here: msdn.microsoft.com/en-us/library/aa664739%28VS.71%29.aspx\n- Ok, good point, I see he's referring to the grammar rule, thus your correct. Therefore grammar the rule you point to is the perfect answer.\n- First off, the empty set is a *subset* of every set, but not a *member* of every set; \"inside\" is ambiguous as to which you mean. Second, I'm not following your point about how that applies to grammar design.\n- It's been a while since my compiler theory has ever been needed but in I believe the lexical parser that evaluates whether expressions are valid that are like A | [B]A | [B]C[B]A where A in this case is the ;\n- Oops-good catch. Here's a treatment on their assembly level equivalence: stackoverflow.com/questions/2288856/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.789Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":160,"estimatedTokens":1516}}369{"id":"stack-6418199","source":"stackoverflow","questionId":6418199,"title":"How does bootstrapping work for gcc?","tags":["gcc","architecture","compiler-construction","boot","bootstrapping"],"text":"Title: How does bootstrapping work for gcc?\nTags: gcc, architecture, compiler-construction, boot, bootstrapping\nSource: Stack Overflow\n\nQuestion:\nI was looking up the pypy project (Python in Python), and started pondering the issue of what is running the outer layer of python? Surely, I conjectured, it can't be as the old saying goes \"turtles all the way down\"! Afterall, python is not valid x86 assembly! \n\nSoon I remembered the concept of bootstrapping, and looked up compiler bootstrapping. \"Ok\", I thought, \"so it can be either written in a different language or hand compiled from assembly\". In the interest of performance, I'm sure C compilers are just built up from assembly. \n\nThis is all well, but the question still remains, how does the computer get that assembly file?!\n\nSay I buy a new cpu with nothing on it. During the first operation I wish to install an OS, which runs C. What runs the C compiler? Is there a miniature C compiler in the BIOS? \n\nCan someone explain this to me?\n\n========================================\n\nTop Answer:\nIn the interest of performance, I'm sure C compilers are just built up from assembly. \n\nC compilers are, nowadays, (almost?) completely written in C (or higher-level languages - Clang is C++, for instance). Compilers gain little to nothing from including hand-written assembly code. The things that take most time are as slow as they are because they solve very hard problems, where \"hard\" means \"big computational complexity\" - rewriting in assembly brings at most a constant speedup, but those don't really matter anymore at that level.\n\nAlso, most compilers want high portability, so architecture-specific tricks in the front and middle end are out of question (and in the backends, they' not desirable either, because they may break cross-compilation).\n\n Say I buy a new cpu with nothing on it. During the first operation I wish to install an OS, which runs C. What runs the C compiler? Is there a miniature C compiler in the BIOS?\n\nWhen you're installing an OS, there's (usually) no C compiler run. The setup CD is full of readily-compiled binaries for that architecture. If there's a C compiler included (as it's the case with many Linux distros), that's an already-compiled exectable too. And those distros that make you build your own kernel etc. also have at least one executable included - the compiler. That is, of course, unless you have to compile your own kernel on an existing installation of anything with a C compiler.\n\nIf by \"new CPU\" you mean a new architecture that isn't backwards-compatible to anything that's yet supported, self-hosting compilers can the usual porting procedure: First write a backend for that new target, then compile yourself for it, and suddenly you got a mature compiler with a battle-hardened (compiled a whole compiler) native backend on the new platform.\n\n========================================\n\nCode:\n```text\n>>> def hello(x, y, z, q):\n...     print \"Hello, world\"\n...     q()\n...     return x+y+z\n... \n>>> import dis\ndis.dis(hello)\n\n\n  2           0 LOAD_CONST               1 ('Hello, world')\n              3 PRINT_ITEM          \n              4 PRINT_NEWLINE       \n\n  3           5 LOAD_FAST                3 (q)\n              8 CALL_FUNCTION            0\n             11 POP_TOP             \n\n  4          12 LOAD_FAST                0 (x)\n             15 LOAD_FAST                1 (y)\n             18 BINARY_ADD          \n             19 LOAD_FAST                2 (z)\n             22 BINARY_ADD          \n             23 RETURN_VALUE\n```\n\n```text\ninvalid.site.ningefingers.main:();\n  Code:\n   0:   aload_0\n   1:   invokespecial   #1; //Method java/lang/Object.\"<init>\":()V\n   4:   return\npublic static void main(java.lang.String[]);\n  Code:\n   0:   iconst_0\n   1:   istore_1\n   2:   iconst_0\n   3:   istore_1\n   4:   iload_1\n   5:   aload_0\n   6:   arraylength\n   7:   if_icmpge   57\n   10:  getstatic   #2; \n   13:  new #3; \n   16:  dup\n   17:  invokespecial   #4; \n   20:  ldc #5; \n   22:  invokevirtual   #6; \n   25:  iload_1\n   26:  invokevirtual   #7; \n   //.......\n}\n```\n\n```text\njavap -c class\n```\n\n========================================\n\nComments:\n- Ah, so it is a segmented process. Write small compiler in assembly to run a medium compiler, use medium compiler to run a large compiler, use large compiler to run a language, etc. So in actual store bought CPUs are these things preloaded? Which memory are they stored in? Are they flashed right into the CPU? into the bios or some type of boot drive?\n- I think I may have misread what you meant by \"new CPU\" based on the stuff about language bootstrapping at the start of your question. I've updated my answer: I hope it's clearer now.\n- @Razor: ...and at some point you end up with a super compiler that is twice as powerful as three ordinary compilers! But it'll cost more than US$50,000 ;-)","metadata":{"transformedAt":"2026-08-18T18:32:17.789Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":97,"estimatedTokens":1216}}370{"id":"stack-32936723","source":"stackoverflow","questionId":32936723,"title":"How do strings look from the compiler's point of view?","tags":["c#",".net","string","memory","compiler-construction"],"text":"Title: How do strings look from the compiler's point of view?\nTags: c#, .net, string, memory, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn `C`, the compiler has a pointer to the start of the string and has an end-symbol (`'\\0'`). If a user wants to calculate the length of the string, the compiler has to count elements of the string array until it finds `'\\0'`.\n\nIn `UCSD-strings`, the compiler has the length of the string in the first symbols.\n\nAnd what does the compiler think about `C#-strings`? Yes, from the user's point of view *`String`* is an `object` that has a field *`Length`*, I'm not talking about high-level stuff. I want to know deep algorithms; e.g., how does the compiler calculate the length of the string?\n\n========================================\n\nTop Answer:\nIn C# the length of the string is stored in the object in a private field (`[NonSerialized]private int m_stringLength;`), it doesn't have to be calculated at run-time.\n\nThe source code of String class is available online.\n\n========================================\n\nCode:\n```text\nC\n```\n\n```text\n'\\0'\n```\n\n```text\n'\\0'\n```\n\n```text\nUCSD-strings\n```\n\n```text\nC#-strings\n```\n\n```text\nString\n```\n\n```text\nobject\n```\n\n```text\nLength\n```\n\n```text\nstring s = \"123\";\nstring s2 = \"234\";\nstring s3 = s + s2;\nstring s4 = s2 + s3;\nConsole.WriteLine(s + s2);\n```\n\n```text\nint totalLength = str0.Length + str1.Length + str2.Length;\n\nString result = FastAllocateString(totalLength);\nFillStringChecked(result, 0, str0);\nFillStringChecked(result, str0.Length, str1);\nFillStringChecked(result, str0.Length + str1.Length, str2);\n```\n\n```text\ns3\n```\n\n```text\ns3\n```\n\n```text\ns4\n```\n\n```text\nstrings\n```\n\n```text\nstring\n```\n\n```text\nstring\n```\n\n```text\nprivate int  m_stringLength;\n```\n\n```text\nstring\n```\n\n```text\nstring.Concat()\n```\n\n```text\nIEnumerable<char>.Count()\n```\n\n```text\nstring\n```\n\n```text\nICollection<T>\n```\n\n```text\nList<T>\n```\n\n```text\nIEnumerable<char>.Count()\n```\n\n```text\nICollection<T>.Count\n```\n\n```text\n[NonSerialized]private int  m_stringLength;\n```\n\n========================================\n\nComments:\n- Your C assumption is wrong. A C compiler knows **exactly** how long each string is. To see this, call `sizeof` on the string (not on a *pointer to* the string!).\n- Normally, I would close this as a dupe but the duplicates are bad. codeproject.com/Articles/3377/Strings-UNDOCUMENTED\n- @MSalters You are confusing concepts. Arrays and strings different entities and they have different operations associated with them. They are related, but they are different abstractions and they shouldn't be mixed.\n- @MSalters: I think homk means the implementation of the length (`strlen` in C) function, not the compiler.\n- You're mixing up what the compiler sees and what happens at the time of execution. The way the compiler works with strings may bear no resemblance to how functions like `strlen` work with them, and the compile-time and run-time representations may be completely different.\n- \"In С, the compiler has a pointer to the start of the string and has an end-symbol ('\\0')\". No. In C the compiler has a start quote and an end quote. It will deliver the number of characters in between as the `sizeof` result, no matter what they are, including embedded nulls. `strlen()` has nothing to do with it, and neither do pointers. The compiler will also *place* a trailing null into the compiled string. It isn't there in the source text.\n- @PaulManta: Disagree. C doesn't provide a string abstraction; in order to work reliably with strings, one must understand that one is working with character arrays in a conventional manner.\n- @Hurkyl Example of why arrays and strings are not the same: `sizeof(x)` will never be equal to `strlen(x)` for any string `x`.\n- @PaulManta: To the compiler (which is the subject of the question), strings are a special case of arrays. Different syntax, same operations. `strlen` is not a compiler concern.\n- @MSalters Oh, I see. I somehow missed the emphasis on \"compiler\". I was still thinking about run-time.\n- @homk If that's what you wanted to know, your real question has nothing to do with 'from the compiler's point of view' whatsoever.","metadata":{"transformedAt":"2026-08-18T18:32:17.789Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":26,"totalLines":148,"estimatedTokens":1044}}371{"id":"stack-34084913","source":"stackoverflow","questionId":34084913,"title":"Why does java allow only dynamic linking?","tags":["java","compiler-construction","static-linking","dynamic-linking"],"text":"Title: Why does java allow only dynamic linking?\nTags: java, compiler-construction, static-linking, dynamic-linking\nSource: Stack Overflow\n\nQuestion:\nI was following a tutorial video on compilers on YouTube, when I came across the fact that the C Programming Language is faster because it allows both static linking and dynamic linking, but Java allows only dynamic linking and that is why C is much faster than Java.\n\nMy question is, if static linking makes a program run faster, why was it not included in Java? I know there must be some real good reason why this decision was taken by the developers of Java to not include static linking, I just want to know what are the reasons.\n\nNote : I do not know if this question already has an answer on SO, but since I could not find one, so I posted. If the answer does already exist, please provide a link to it.\n\nNote : The link to the tutorial provided is in Hindi Language. Sorry about that.\n\n========================================\n\nTop Answer:\nC is not faster because of static versus dynamic linking. That is a red herring.\n\nJava loading times are typically poor because the VM and standard library are comparatively huge, which means there simply is a lot to load.\n\nIndeed, dynamic linking can be used to **speed up** initial loading times because with `dlopen()` and friends you control when that loading happens instead of paying the cost upfront (during loading). \n\nWithin this context, in general the one major difference in performance is in memory, in particular the ability to control layout of memory to some extent in C. That can yield substantial benefits because with less fragmentation and by reducing things down to 'cache size' CPU caching and speculative hardware optimisations (like prefetching) work much better.\n\n========================================\n\nCode:\n```text\ndlopen()\n```\n\n========================================\n\nComments:\n- `that is why C is much faster than Java` - really?\n- Apart from what Andreas said - why would the support of static **linking** make a programming language \"faster\" **overall**?\n- @AndreasFester, that is what the `tutorial` that I am following says? Is it a wrong statement?\n- @mikolak, am I really following a wrong tutorial with wrong information? :O\n- Related: stackoverflow.com/questions/1993390/&hellip;\n- Performance of C vs. Java is much discussed and highly disputed, so I would not classify that as a statement but as an opinion that has to be looked at in the context it was made.\n- @SajibAcharya : hard to say, I don't know Hindi :). But it sounds like the tutorial might misrepresent some facts and/or confuse some definitions. The best judge on that is yourself, see e.g. this related answer on the benefits of static vs dynamic linking in C++ and decide for yourself.\n- @SajibAcharya The general answer to performance related question is: \"it depends.\". There are much more aspects which affect performance of an application than static versus dynamic linking (like proper selection of algorithms and data structures). Certainly dynamic linking **is** slower since it requires additional lookups, but when does it matter?\n- @mikolak, sorry about the language problem, what the tutor really is saying as translated in english would be - \"Since C supports static linking, the memory consumption at runtime would be more but will run faster, but since Java supports dynamic linking, it would run slower since the linking occurs at runtime.\" Then he concludes several times that \"Thus `C` is much faster than `Java`\". Is this statement wrong?\n- @AndreasFester, so should I take the statement that \"Java is slower than C **because** it uses dynamic linking\" to be wrong?\n- `\"Thus C is much faster than Java\". Is this statement wrong?` - yes, as it stands it is definitely wrong. You can not make such a statement without additional information about the concrete scenario. What he probably meant is that the startup phase is faster - but again, first that might be correct for simple applications but different for complex ones, and second you need to decide whether it matters (as startup only occurs once when you launch the application). Then, this might be an issue for Desktop applications, but less an issue for server applications.\n- See also stackoverflow.com/questions/29662971/java-faster-than-c, stackoverflow.com/questions/18834092/java-faster-than-c\n- Don't use quote formatting for text that isn't quoted. Don't use code formatting for text that isn't code.\n- @AndreasFester, thank you for the information. :) So dynamic linking is used in Java just for the \"Code once, run everywhere\" feature?\n- @EJP, right. will keep that in mind. sorry about that.\n- To be pedantic: Java does include a linker step which is what the resolve parameter in class loading indicates ;) In particular, if you write naive code that crosses classloader boundaries you will encounter exceptions from the linker phase of class loading because classloaders also act as namespaces. (Meaning the name com.acme.A in classloader C1 is different from the name come.acme.A in classloader C2.)\n- So, the **only** reason to go for dynamic linking is for the \"Code once, run everywhere\" feature, or is there something else too? Just asking.\n- @SajibAcharya No, it's just plain wrong. Conceptually, nothing prevents anybody from \"statically linking\" Java programs. Or if something does, this answer does not explain why.\n- I don't understand how static linking negates \"Code once, run everywhere\". There are languages(Go, Rust etc.) using static linking which can be containerized and run even with distroless or scratch images. Java is dynamically linked, has the slogan of \"Code once, run everywhere\", but it still requires JRE to run.\n- @Hmerac I'm not a compilers expert so this is just my reasoning here - Java is compiled to .class files (so called byte code) and not to assembly (machine code). Those class files could then be distributed and *interpret* in any JVM in any OS, yielding the same result. On the opposite side the compilation from C code to machine code means translating the source code to a very specific instructions that are not reusable in different OSs or architectures.","metadata":{"transformedAt":"2026-08-18T18:32:17.789Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":54,"estimatedTokens":1549}}372{"id":"stack-2441721","source":"stackoverflow","questionId":2441721,"title":"Is LLVM suitable for parallel languages?","tags":["compiler-construction","llvm","parallel-processing","distributed-programming"],"text":"Title: Is LLVM suitable for parallel languages?\nTags: compiler-construction, llvm, parallel-processing, distributed-programming\nSource: Stack Overflow\n\nQuestion:\nWhat properties of LLVM makes it good choice for implementation of (parallel, concurrent, distributed)-oriented language, what makes it bad?\n\n========================================\n\nTop Answer:\nFor some practical examples in this space see http://code.google.com/p/gpuocelot/. It allows Cuda code to multitarget on x86.","metadata":{"transformedAt":"2026-08-18T18:32:17.789Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":11,"estimatedTokens":121}}373{"id":"stack-6838428","source":"stackoverflow","questionId":6838428,"title":"Are compiled programs actually in true binary?","tags":["compiler-construction","operating-system","machine-code"],"text":"Title: Are compiled programs actually in true binary?\nTags: compiler-construction, operating-system, machine-code\nSource: Stack Overflow\n\nQuestion:\nFor example, when I compile a C application is the outputted file read as binary or does the OS then interpret the compilation? Is the \"machine language\" pure binary?\n\nEDIT: Yes, everything on a computer is pure binary. Im asking if the processer directly interprets the file outputted by the compiler or does the OS process it first?\n\n========================================\n\nTop Answer:\nI think what you're really asking is, do compiled programs run on bare metal (do they execute independent of the OS). The very short answer is, no. Although the program itself does execute native CPU instructions, the OS is capable of confining it and controlling its behavior. Furthermore, during the loading phase, certain external (dll) symbols need to be resolved. Finally most programs rely on various operating system abstractions (memory access, for example -- writing your own swap functionality is exceedingly difficult and pointless). In this sense, no binaries are not autonomous bare-metal machine code.\n\nHowever they *are* pure binary. Everything on a computer is.\n\n**EDIT**\n\nAnother way to interpret your question is: are compiled programs actually native CPU instructions. The answer is yes (aside from *loading* the binary, which the OS has to help with). Compilers output assembly language in which each line corresponds with exactly one CPU instruction. This is still text. The assembly is compiled by an assembler into actual binary.\n\n========================================\n\nComments:\n- What makes binary \"true\" or \"pure\"?\n- Doesn't the original question sound like it's asking whether at the lowest level computers are binary reading Turing machines? And I think you give the answer: No, it's \"CPU assembly code\". But then \"ultimately\" it must be all binary, right? This is way out of my depth. It would be interesting to learn more about that.","metadata":{"transformedAt":"2026-08-18T18:32:17.789Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":501}}374{"id":"stack-101914","source":"stackoverflow","questionId":101914,"title":"Building effective external DSLs","tags":["compiler-construction","dsl"],"text":"Title: Building effective external DSLs\nTags: compiler-construction, dsl\nSource: Stack Overflow\n\nQuestion:\nWhat tools are there for me to build a real, honest to goodness external DSL. And no, I'm not talking about abusing Ruby, Boo, XML or another existing language or syntax, I mean a REAL external DSL -- my own language for my own purposes.\n\nI know that there are a few language workbenches being developed and I've heard about things like \"Irony\" for .NET. And, of course, there's ANTLR, Lex/Yaac, etc but I'm afraid those are too complicated for what I'm trying to do.\n\nPlease talk about a DSL builder tool you may have used or heard about and your impressions on how it helps and what its downsides are.\n\n========================================\n\nTop Answer:\nIf you're looking into writing stand-alone DSLs, then you're looking into building compilers--no way around it. Compiler construction *is* essential programming knowledge, and it's really not as difficult as commonly thought. Steve Yegge's Righ Programmer Food summarizes the value of knowing how to build compilers quite nicely.\n\nThere are plenty of ways to get started. I recommend checking out the 2 papers mentioned in the article: Want to write a compiler? Just read these Two papers. The first one, Let's build a compiler, is very accessible. It uses Turbo Pascal as an implementation language, but you can easily implement it in any other language--the source code is very clear. Pascal is a simple language.\n\nOnce you get a good feel for how things work and the terminology involved, I recommend delving into something like ANTLR. ANTLR has a nice IDE, ANTLRWorks, that comes with an interpreter and a debugger. It also produces really really good visualizations of your grammars on the fly. I found it invaluable in learning.\n\nANTLR has several good tutorials, although they might be a bit overwhelming at first. This one is nice, although it's against ANTLR 2.0, so you might run into incompatibilities with a more recent version (currently the latest is 3.1).\n\nFinally, there's another approach to DSLs: The Lisp approach. Given Lisp's syntax-less nature (your code is basically abstract syntax trees), you can shape endless languages out of it, provided you get used to the parentheses :).\n\nIf you do go with that approach, you want to use an embeddable Lisp. Under Java, you have Clojure, a Lisp dialect that interoperates flawlessly with JVM and its libraries. I haven't used it personally, but it looks good. For Scheme, there's GNU Guile, which is licensed under LGPL. For Common Lisp, there's ECL, also under the LGPL. Both use a C interface for interoperability, so you can pretty much embed them into any other language. ECL is unique among Lisps in that each Lisp function is implemented as a C function, so you can write Lisp code in C if you want to (say, inside your own extensions methods--you can create C functions that operate on Lisp objects, and then call them from Lisp). I've been using ECL for a side-project of mine for a while, and I like it. The maintainer is quite active and responsive.\n\n========================================\n\nComments:\n- NOTE: I'm not necessarily looking for Turing completeness here, either. Mostly just expression syntax to configure things in my model.\n- Excellent. I'll definitely check this out!\n- You're not helping me here. hahah I've messed with ANTLR before and I know it's capabilities, but I was hoping for something not quite so sophisticated/complicated. I guess I'll have to give it another look.","metadata":{"transformedAt":"2026-08-18T18:32:17.789Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":32,"estimatedTokens":884}}375{"id":"stack-532176","source":"stackoverflow","questionId":532176,"title":"Erlang type system","tags":["compiler-construction","erlang","type-systems"],"text":"Title: Erlang type system\nTags: compiler-construction, erlang, type-systems\nSource: Stack Overflow\n\nQuestion:\nI've been scrounging around the web looking for various typing practices of Erlang programs and there seem to be a few... although its somewhat difficult to find a solid source of info\nnamely Im looking for practical info about:\n\n1.`-specs` - this one looks pretty attractive. a few places mention that the functions that have an associated -specs directive with it are checked at compile time(for correct type usage)... I cant seem to find more info on how to use it (which tool to use - Dialyzer,TypEr?). Im really eager to create a small parser/code-gen that would generate these \"specs\" from function declarations of the form\n\n```\nfunctionName(param1 :List, param2 :Tuple) -> ...\n```\n\nI have not seen if `-spec` supports abstract types (user declared types - \"Car\" type - \n\n```\n{car,{weight,_},{height,_},{maxSpeed,_}}\n```\n\n2.`-deftype` directive mentioned here\n\nErlang would become so much more powerful for me, if I could start typing things and have them be checked at compile time. The run-time the parser/code-gen I mentioned above would generate guard type checks in the output source-code.\n\n========================================\n\nTop Answer:\nTake a look to\nhttp://learnyousomeerlang.com/types-or-lack-thereof\n(very clean explanation) and the reference here:\nhttp://erlang.org/doc/reference_manual/typespec.html\n\n========================================\n\nCode:\n```text\nfunctionName(param1 :List, param2 :Tuple) -> ...\n```\n\n```text\n{car,{weight,_},{height,_},{maxSpeed,_}}\n```\n\n```text\n-specs\n```\n\n```text\n-spec\n```\n\n```text\n-deftype\n```\n\n```text\ndialyzer --help\n```\n\n```text\ntyper --help\n```\n\n========================================\n\nComments:\n- phenomenal!! lots of info, details, thanks so much for the links!\n- isnt edoc being depreciated for code typing in favor of -spec? I think I've seen it someplace as a note with R13 in mind, correct me if Im wrong \\nthanks\n- Your right - I dug up Kostas Sagonas's paper from EUC'08 at erlang.se/euc/08/1400Kostis.pdf and this is what he recommends. Unfortunately I missed EUC this year trying to raise money :(","metadata":{"transformedAt":"2026-08-18T18:32:17.789Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":69,"estimatedTokens":545}}376{"id":"stack-34551747","source":"stackoverflow","questionId":34551747,"title":"Who is responsible for the stack and heap in C++?","tags":["c++","c","compiler-construction","heap-memory","stack-memory"],"text":"Title: Who is responsible for the stack and heap in C++?\nTags: c++, c, compiler-construction, heap-memory, stack-memory\nSource: Stack Overflow\n\nQuestion:\nThis isn't a question of what the purpose of either of them are. Instead it's a question of who or what is responsible for the invention of the stack and heap? \n\nAre these inventions of the C++ compiler? \n\nDoes the os specify memory sections in RAM designated \"stack\" and \"heap\"? \n\nI'm pretty sure they are not built into the hardware but I could be wrong. \n\nAlso, is the compiler responsible for generating assembly code that specify which local or function data will be stored on the stack vs CPU registers?\n\n========================================\n\nTop Answer:\nThis post is about 32-bit Linux on x86. I don't know about other architectures/OSes.\n\n Are these inventions of the C++ compiler? Does the [OS] specify memory\n sections in RAM designated \"stack\" and \"heap\"?\n\n### Heap or free store\n\nA program has different sections, one of them the `.data` section. Dynamic memory allocation is usually implemented using the `brk` system call (`sbrk` builds on top of it). `man brk` says the following:\n\n `brk()` and `sbrk()` change the location of the program break, \n which defines the end of the process's data segment (i.e., the\n program break is the first location after the end of the\n uninitialized data segment). Increasing the program break has\n the effect of allocating memory to the process; decreasing the\n break deallocates memory.\n\nThat means the \"heap\" or \"free store\" is in reality the `.data` section.\n\nAs @kfx said in the comments to this answer, no standard states that `malloc` has to be implemented using `brk`. An implementation using `mmap` is possible as well.\nFrom `man mmap`:\n\nThe `mmap()` function shall establish a mapping between an address space\n of a process and a memory object.\n\nThis basically means that a file (Unix ideology: \"everything is a file\") is mapped into memory.\n\n### Stack\n\nThe stack is also in the `.data` section and grows downward. Technically, x86 enables you to define a downward-growing stack segment but Linux does not use this feature. Dunno why.\n\n I'm pretty sure they are not built into the hardware but I could be\n wrong.\n\nNo, they aren't. Segments are set up at runtime of the OS and not stored in the hardware.\n\nThe following is from Linux 4.2.\n\nWhen the MBR has jumped to the bootloader and the bootloader has executed, this is executed (the path is `/arch/x86/boot/header.S`):\n\n```\n# Normalize the start address\n ljmp $BOOTSEG, $start2\n\nstart2:\n movw %cs, %ax\n movw %ax, %ds\n movw %ax, %es\n movw %ax, %ss\n xorw %sp, %sp\n```\n\nAll those segment registers are initialized to `$BOOTSEG` here, which is `0x7c0`. `sp` is set to `0x00`. No `esp` since we're still in Real Mode!\n\nAfter the initialization stuff is done, the jump into the real kernel is performed. The segment registers are set up as follows:\n\n```\nmovw $__BOOT_DS, %cx\nmovw $__BOOT_TSS, %di\n\nmovl %cr0, %edx\norb $X86_CR0_PE, %dl # Protected mode\nmovl %edx, %cr0\n\n# Transition to 32-bit mode\n.byte 0x66, 0xea # ljmpl opcode\n2: .long in_pm32 # offset\n.word __BOOT_CS # segment\nENDPROC(protected_mode_jump)\n\n.code32\n.section \".text32\",\"ax\"\nGLOBAL(in_pm32)\n# Set up data segments for flat 32-bit mode\nmovl %ecx, %ds\nmovl %ecx, %es\nmovl %ecx, %fs\nmovl %ecx, %gs\nmovl %ecx, %ss\n```\n\nThe segment registers are set to the content of `cs` again.\n\n Also, is the compiler responsible for generating assembly code that\n specify which local or function data will be stored on the stack vs\n CPU registers?\n\nYes. For function invocations, there are different calling conventions: some push their arguments onto the stack, some move them into registers.\n\nLocal variables can be implemented with registers or the stack as well.\n\nUnoptimized C compilers push the arguments onto the stack, call the function, and pop them off afterwards (\"stdcall\" calling convention).\n\nThey use the stack for local variables, in combination with the `ebp` register.\n\n========================================\n\nCode:\n```text\n# Normalize the start address\n    ljmp    $BOOTSEG, $start2\n\nstart2:\n    movw    %cs, %ax\n    movw    %ax, %ds\n    movw    %ax, %es\n    movw    %ax, %ss\n    xorw    %sp, %sp\n```\n\n```text\nmovw    $__BOOT_DS, %cx\nmovw    $__BOOT_TSS, %di\n\nmovl    %cr0, %edx\norb $X86_CR0_PE, %dl    # Protected mode\nmovl    %edx, %cr0\n\n# Transition to 32-bit mode\n.byte   0x66, 0xea      # ljmpl opcode\n2:  .long   in_pm32         # offset\n.word   __BOOT_CS       # segment\nENDPROC(protected_mode_jump)\n\n.code32\n.section \".text32\",\"ax\"\nGLOBAL(in_pm32)\n# Set up data segments for flat 32-bit mode\nmovl    %ecx, %ds\nmovl    %ecx, %es\nmovl    %ecx, %fs\nmovl    %ecx, %gs\nmovl    %ecx, %ss\n```\n\n```text\n.data\n```\n\n```text\nbrk\n```\n\n```text\nsbrk\n```\n\n```text\nman brk\n```\n\n```text\nbrk()\n```\n\n```text\nsbrk()\n```\n\n```text\n.data\n```\n\n```text\nmalloc\n```\n\n```text\nbrk\n```\n\n```text\nmmap\n```\n\n```text\nman mmap\n```\n\n```text\nmmap()\n```\n\n```text\n.data\n```\n\n```text\n/arch/x86/boot/header.S\n```\n\n```text\n$BOOTSEG\n```\n\n```text\n0x7c0\n```\n\n```text\nsp\n```\n\n```text\n0x00\n```\n\n```text\nesp\n```\n\n```text\ncs\n```\n\n```text\nebp\n```\n\n```text\nnew\n```\n\n```text\ndelete\n```\n\n```text\nmalloc/free\n```\n\n```text\nnew\n```\n\n```text\nmalloc\n```\n\n```text\nmalloc()\n```\n\n```text\nfree()\n```\n\n```text\nnear\n```\n\n```text\nfar\n```\n\n```text\nhuge\n```\n\n========================================\n\nComments:\n- A process runs in a virtual address space, there is no special RAM for anything.\n- Note that there's not even any requirement that the stack be linear. A C++ compiler could, for example, generate code that uses a linked list of mini-stacks that are stored on the heap with thunking for external APIs (like what golang does) and it'd still be perfectly conformant.\n- C does not even require a OS - there exists free standing environments. So certainly the \"stack/heap\" can not be the sole responsibility of an OS.\n- The concepts *stack* and *heap* are merely conventions. Automatic objects are destructed in the reverse order of their creation. Dynamically created objects exist until they are destroyed. The language does not restrict how an implementation actually achieves these behaviors.\n- @Jason: your last question \"Also ...\" is really a separate question entirely, but the answer is broadly, yes, the compiler has to use heuristics to do register allocation and decide when values are in registers / on the stack, and that is a very important part of its job towards the end of the compilation process.\n- @jxh: The concept *compiler* is also a convention. The C++ standard only refers to an \"implementation\", which is capable of \"executing\" a conforming program. (1.4/2.1). Of course, most implementations allow the user to aggressively partially evaluate the program so that it can be applied multiple times at less cost. That's useful but not mandated by the standard.\n- The linker has nothing to with register allocation though.\n- I said that information about stack, heap and static memory location and size are in the file header, and produced by the linker. Then the OS, which is responsible of runing programs initialzes registers and memory based on these information in the header.\n- Well, ok, but that's actually not what you wrote in your answer, where you answered \"does the compiler generate assembly code that specifies what information will be stored on the stack vs what maybe stored on a CPU's registers\" with \"the linker does it\", but it doesn't.\n- Okay so then the compiler does generate locations for storage and the OS actually implements it. Correct?\n- The linker. Not the compiler.\n- There doesn't need to be an OS. Many embedded systems can run C and C++ without an OS.\n- I don't want to offend anybody here but your answer is correct but barely explains some details and despite that fact has received four by now (the most) upvotes. Could you please tell me what motivation is behind this? No offense, again, I just want to hear a rational opinion.\n- Maybe C/C++ is no hardware model, but a programming model, establishing standards\n- @cad: Reminder: there are other platforms that exist besides desktops. One group is embedded systems. In smaller and critical safety embedded systems, they don't use heaps. I'm working on a medical device system that doesn't have a heap, but runs C language well.\n- `malloc` can be implemented using `mmap` or other options, there is no requirement to use `brk`.","metadata":{"transformedAt":"2026-08-18T18:32:17.789Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":33,"totalLines":295,"estimatedTokens":2105}}377{"id":"stack-1413204","source":"stackoverflow","questionId":1413204,"title":"How to use indentation as block delimiters with bison and flex","tags":["compiler-construction","bison","flex-lexer"],"text":"Title: How to use indentation as block delimiters with bison and flex\nTags: compiler-construction, bison, flex-lexer\nSource: Stack Overflow\n\nQuestion:\nI wounder how to implement indentation as block delimiters in bison + flex. Just like in python. I'm writing my own programming language ( mostly for fun, but I intend to use it together with a game engine ), I'll try to come up with something special that minimizes boilerplate and maximizes dev speed.\n\nI have already written an compiler ( actually a *`langToy'* to Nasm translator ) in C, but failed. By some reason it was only able to handle one string in the whole source file ( well, I had been awake for more than 48 hours - so... You know, brain meltdown ).\n\nI don't know if curly brackets and/or begin -> end are easier to implement ( I don't have problem doing that ) or if it's just my brain that locks up.\n\nThanks in advance!\n\n**Update:** Okay, I have no clue about how to do it with flex. I have problems with returning multiple DEDENTs to the parser. Flex/Bison are relatively new to me.\n\n**Update 2:**\nThis is the flex-file I've come up with so far; it does not quite get it:\n\n```\n%x t\n%option noyywrap\n\n%{\n int lineno = 0, ntab = 0, ltab = 0, dedent = 0;\n%}\n\n%%\n\n\\n { ntab = 0; BEGIN(t); }\n\\t { ++ntab; }\n. { int i; /* my compiler complains not c99 if i use for( int i=0... */\n if( ntab > ltab )\n printf(\"> indent >\\n\");\n else if( ntab You can try to play around with it, maybe you sees what I'm missing. returning multiple dedents would be an ease in Haxe ( return t_dedent( num ); ).\n\nThis code doesn't always match the indents/dedents correctly.\n\n**Update 3:** I think that I will give up hope on flex and do it my own way, If anyone knows how to do it in flex I would be happy to hear it anyways.\n\n========================================\n\nTop Answer:\nChris' answer goes a long way towards a usable solution, thanks a bunch for this!\nUnfortunately, it is missing a few more important aspects which I needed:\n\nMultiple outdents (unindents) at once. Consider the following code should emit **two** outdents after the call to `baz`:\n\n```\ndef foo():\n if bar:\n baz()\n```\n\nEmit outdents when the end of the file is reached and still is in some indentation level.\n\n- Indentation levels of different size. Chris' current code only works correctly for 1-space indents.\n\nBased on Chris' code, I came up with a solution which works in all the cases I have come across so far. I have created a template project for parsing indentation-based text using flex (and bison) on github: https://github.com/lucasb-eyer/flex-bison-indentation. It is a fully working (CMake-based) project which also tracks the line position and the column range of the current token.\n\nJust in case the link should break for whatever reason, here is the meat of the lexer:\n\n```\n#include \n\nint g_current_line_indent = 0;\nstd::stack g_indent_levels;\nint g_is_fake_outdent_symbol = 0;\n\nstatic const unsigned int TAB_WIDTH = 2;\n\n#define YY_USER_INIT { \\\n g_indent_levels.push(0); \\\n BEGIN(initial); \\\n}\n#include \"parser.hh\"\n\n%}\n\n%x initial\n%x indent\n%s normal\n\n%%\n int indent_caller = normal;\n\n /* Everything runs in the mode and enters the mode\n when a newline symbol is encountered.\n There is no newline symbol before the first line, so we need to go\n into the mode by hand there.\n */\n. { set_yycolumn(yycolumn-1); indent_caller = normal; yyless(0); BEGIN(indent); }\n\\n { indent_caller = normal; yyless(0); BEGIN(indent); } \n\n\" \" { g_current_line_indent++; }\n\\t { g_current_line_indent = (g_current_line_indent + TAB_WIDTH) & ~(TAB_WIDTH-1); }\n\\n { g_current_line_indent = 0; /* ignoring blank line */ }\n> {\n // When encountering the end of file, we want to emit an\n // outdent for all indents currently left.\n if(g_indent_levels.top() != 0) {\n g_indent_levels.pop();\n\n // See the same code below (.) for a rationale.\n if(g_current_line_indent != g_indent_levels.top()) {\n unput('\\n');\n for(size_t i = 0 ; i . {\n if(!g_is_fake_outdent_symbol) {\n unput(*yytext);\n }\n g_is_fake_outdent_symbol = 0;\n // -2: -1 for putting it back and -1 for ending at the last space.\n set_yycolumn(yycolumn-1);\n\n // Indentation level has increased. It can only ever\n // increase by one level at a time. Remember how many\n // spaces this level has and emit an indentation token.\n if(g_current_line_indent > g_indent_levels.top()) {\n g_indent_levels.push(g_current_line_indent);\n BEGIN(indent_caller);\n return TOK_INDENT;\n } else if(g_current_line_indent \\n { g_current_line_indent = 0; indent_caller = YY_START; BEGIN(indent); }\n```\n\n========================================\n\nCode:\n```text\n%x t\n%option noyywrap\n\n%{\n  int lineno = 0, ntab = 0, ltab = 0, dedent = 0;\n%}\n\n%%\n\n<*>\\n  { ntab = 0; BEGIN(t); }\n<t>\\t  { ++ntab; }\n<t>.   { int i; /* my compiler complains not c99 if i use for( int i=0... */\n         if( ntab > ltab )\n           printf(\"> indent >\\n\");\n         else if( ntab < ltab )\n           for( i = 0; i < ltab - ntab; i++ )\n             printf(\"< dedent <\\n\");\n         else\n           printf(\"=        =\\n\");\n\n         ltab = ntab; ntab = 0;\n         BEGIN(INITIAL);\n         /* move to next rule */\n         REJECT;}\n.    /* ignore everything else for now */\n\n%%\n\nmain()\n{\n  yyin = fopen( \"test\", \"r\" );\n  yylex();\n}\n```\n\n```text\n%{\n/* globals to track current indentation */\nint current_line_indent = 0;   /* indentation of the current line */\nint indent_level = 0;          /* indentation level passed to the parser */\n%}\n\n%x indent /* start state for parsing the indentation */\n%s normal /* normal start state for everything else */\n\n%%\n<indent>\" \"      { current_line_indent++; }\n<indent>\"\\t\"     { current_line_indent = (current_line_indent + 8) & ~7; }\n<indent>\"\\n\"     { current_line_indent = 0; /*ignoring blank line */ }\n<indent>.        {\n                   unput(*yytext);\n                   if (current_line_indent > indent_level) {\n                       indent_level++;\n                       return INDENT;\n                   } else if (current_line_indent < indent_level) {\n                       indent_level--;\n                       return UNINDENT;\n                   } else {\n                       BEGIN normal;\n                   }\n                 }\n\n<normal>\"\\n\"     { current_line_indent = 0; BEGIN indent; }\n... other flex rules ...\n```\n\n```text\ndef foo():\n  if bar:\n    baz()\n```\n\n```text\n#include <stack>\n\nint g_current_line_indent = 0;\nstd::stack<size_t> g_indent_levels;\nint g_is_fake_outdent_symbol = 0;\n\nstatic const unsigned int TAB_WIDTH = 2;\n\n#define YY_USER_INIT { \\\n    g_indent_levels.push(0); \\\n    BEGIN(initial); \\\n}\n#include \"parser.hh\"\n\n%}\n\n%x initial\n%x indent\n%s normal\n\n%%\n    int indent_caller = normal;\n\n /* Everything runs in the <normal> mode and enters the <indent> mode\n    when a newline symbol is encountered.\n    There is no newline symbol before the first line, so we need to go\n    into the <indent> mode by hand there.\n */\n<initial>.  { set_yycolumn(yycolumn-1); indent_caller = normal; yyless(0); BEGIN(indent); }\n<initial>\\n { indent_caller = normal; yyless(0); BEGIN(indent); }    \n\n<indent>\" \"     { g_current_line_indent++; }\n<indent>\\t      { g_current_line_indent = (g_current_line_indent + TAB_WIDTH) & ~(TAB_WIDTH-1); }\n<indent>\\n      { g_current_line_indent = 0; /* ignoring blank line */ }\n<indent><<EOF>> {\n                    // When encountering the end of file, we want to emit an\n                    // outdent for all indents currently left.\n                    if(g_indent_levels.top() != 0) {\n                        g_indent_levels.pop();\n\n                        // See the same code below (<indent>.) for a rationale.\n                        if(g_current_line_indent != g_indent_levels.top()) {\n                            unput('\\n');\n                            for(size_t i = 0 ; i < g_indent_levels.top() ; ++i) {\n                                unput(' ');\n                            }\n                        } else {\n                            BEGIN(indent_caller);\n                        }\n\n                        return TOK_OUTDENT;\n                    } else {\n                        yyterminate();\n                    }\n                }\n\n<indent>.       {\n                    if(!g_is_fake_outdent_symbol) {\n                        unput(*yytext);\n                    }\n                    g_is_fake_outdent_symbol = 0;\n                    // -2: -1 for putting it back and -1 for ending at the last space.\n                    set_yycolumn(yycolumn-1);\n\n                    // Indentation level has increased. It can only ever\n                    // increase by one level at a time. Remember how many\n                    // spaces this level has and emit an indentation token.\n                    if(g_current_line_indent > g_indent_levels.top()) {\n                        g_indent_levels.push(g_current_line_indent);\n                        BEGIN(indent_caller);\n                        return TOK_INDENT;\n                    } else if(g_current_line_indent < g_indent_levels.top()) {\n                        // Outdenting is the most difficult, as we might need to\n                        // outdent multiple times at once, but flex doesn't allow\n                        // emitting multiple tokens at once! So we fake this by\n                        // 'unput'ting fake lines which will give us the next\n                        // outdent.\n                        g_indent_levels.pop();\n\n                        if(g_current_line_indent != g_indent_levels.top()) {\n                            // Unput the rest of the current line, including the newline.\n                            // We want to keep it untouched.\n                            for(size_t i = 0 ; i < g_current_line_indent ; ++i) {\n                                unput(' ');\n                            }\n                            unput('\\n');\n                            // Now, insert a fake character indented just so\n                            // that we get a correct outdent the next time.\n                            unput('.');\n                            // Though we need to remember that it's a fake one\n                            // so we can ignore the symbol.\n                            g_is_fake_outdent_symbol = 1;\n                            for(size_t i = 0 ; i < g_indent_levels.top() ; ++i) {\n                                unput(' ');\n                            }\n                            unput('\\n');\n                        } else {\n                            BEGIN(indent_caller);\n                        }\n\n                        return TOK_OUTDENT;\n                    } else {\n                        // No change in indentation, not much to do here...\n                        BEGIN(indent_caller);\n                    }\n                }\n\n<normal>\\n    { g_current_line_indent = 0; indent_caller = YY_START; BEGIN(indent); }\n```\n\n```text\nbaz\n```\n\n========================================\n\nComments:\n- Thanks for the useful link, I will give it a crack and see if I succeeds this time.\n- Well, seems like it's at least easier to write a lexer with special block-begin and block-end symbols. It's **not** easier to write { and } on my localized keyboard ;D\n- Seems like you got it, but I want tabstops to count as 2 spaces. So I guesses that the line should be current_line_indent = (current_line_indent + 2) & ~1;\n- Yes -- when you see a tab, you need to bump current_line_indent to the next tabstop.\n- My code produces an INDENT/UNINDENT for *every* space of indentation. So for your example with 2-space indents, it will produce two INDENT tokens after the first line, another 2 after the second, and 4 UNINDENT at the end. So you'll need to have your parser \"ignore\" extra redundant INDENT/UNINDENT pairs. Collapsing them in the lexer is hard if you want to catch trailing reduced indent properly, but if you don't care about that, you can use a stack of indent levels rather than a single counter.","metadata":{"transformedAt":"2026-08-18T18:32:17.790Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":322,"estimatedTokens":2985}}378{"id":"stack-781718","source":"stackoverflow","questionId":781718,"title":"Thompson's Trojan Compiler","tags":["unix","compiler-construction"],"text":"Title: Thompson's Trojan Compiler\nTags: unix, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm trying to grasp a better understanding of Thompson's Trojan Compiler (discussed in his 1984 ACM Turing Award speech \"Reflections On Trusting Trust\"), and so far this is how I understand it:\n\n\"The original login program for Unix would accept whatever login and password the root instructed it to. It would only accept a certain password, known only by the man who wrote the system. This could let him log in to the system as root.\"\n\nIs this the right concept? I'm not 100% sure if I understand the whole concept.\n\nIf someone could make it clearer, it would help.\n\n(See also Bruce Schneier Countering \"Trusting Trust\")\n\n========================================\n\nTop Answer:\nI had never encountered the concept before, but this is pretty interesting - I found a neat write-up at http://scienceblogs.com/goodmath/2007/04/strange_loops_dennis_ritchie_a.php\n\n========================================\n\nComments:\n- You should probably link to the ACM page for On Trusting Trust, no?\n- This is ... funny, in a disturbed way. It claims something impossible, yet is accepted as the correct answer. That would seem to cause Thompson himself to disappear, in a cloud of logic smoke.\n- You had me up until you said \"halting problem.\"\n- can't we just go through the complier's source code and check for any backdoor, what was the article's point?\n- @coder101, The whole point of the exploit is that the compiler's source code no longer reflects its behavior. The compiler has invisible code that recognizes a vanilla compiler and inserts behavior without changing the source code. To detect it, you'd have to inspect the object code and notice that there is emitted code that doesn't correspond to anything in the source.","metadata":{"transformedAt":"2026-08-18T18:32:17.790Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":28,"estimatedTokens":453}}379{"id":"stack-720714","source":"stackoverflow","questionId":720714,"title":"Making a language, need a good backend","tags":["compiler-construction","backend","intermediate-language"],"text":"Title: Making a language, need a good backend\nTags: compiler-construction, backend, intermediate-language\nSource: Stack Overflow\n\nQuestion:\nI want to make a compiled language. I am currently evaluating backends.\nSo far I am looking at C because of its speed of execution, compiling, and a small, easy to use compiler called TCC.\n\nHaving read the discussions here about using it as an intermediate language, I am trying to think about how to make it compatible with garbage collection, and handling exceptions. So far, I think I can solve both, but with much overhead.\n\nHere are some of my thoughts on the other possible backends:\n\n- Assembly: unportable and a total pain to program in.\n\n- .NET: Feels really slow. 5 seconds to start up and 5 seconds to evaluate 1+2 on Ironpython and Boo. Unable to run without large library.\n\n- JVM: Feels a bit slow. No access to binary libraries. Unable to run without large library.\n\n- LLVM: No windows support. I hear that compiled executable size is 16 mb+\n\n- C--: looks underdeveloped.\n\n- C++: possibly. Can't find a nice small free one I can bundle with.\n\nCan any of you change my mind or have more to add to this list?\n\n**Edit**\n\nI've been experimenting with LLVM recently. I found out that they have precompiled binaries and that it is possible to compile to native assembly. \n\nhttp://www.antlr.org/wiki/display/CS652/Generating+machine+executable+binaries+with+LLVM\n\nHere are the steps:\n\n- Run llvm-as on LLVM Assembly, which yields a LLVM bytecode file.\n\n- Run llc on the LLVM bytecode file to yield an assembly file.\n\n- Run an assembler on the assembly file to yield an object file. (or run llvm-ld which seems to depend on an externally installed c compiler)\n\n- Compile to executable with gcc etc.\n\n========================================\n\nTop Answer:\nIn that case LLVM is probably a better choice. \n\nLLVM has Windows support, it just takes some time to compile\n\n========================================\n\nComments:\n- Duplicates stackoverflow.com/questions/446883/best-compiler-destination\n- @ norman, My question has more information. I'm not really satisfied with the answers there.\n- I like C++ because it already has stack unwinding exceptions and I can encapsulate pointers to make them more garbage unfriendly, unfortunately the compilers available seem to be extremely large and take longer to compile.\n- I read about many languages that used C as the backend, and yours was one of them. Was it hard for you to output C code that could be easily garbage collected?\n- We have our own GC implementation, with specialized buckets for each type (our language is close-world; every type is known at compile time). The only complex part is being sure of marking every live object: we use some markers in the stack + registers saving for those architectures that need it.\n- I've tried to read all the LLVM documents. From what I can understand from it so far, I don't want to force users to install the large runtime, which takes 3 GB to compile.\n- LLVM is not a runtime, it is a compiler framework. Once your application is compiled, it will run as any other native binary on windows\n- I only read documentation that mentions bytecode compilation. Even if it does allow static compilation, I don't even have 3 GB of memory to compile it under windows and can't expect anyone else to either.\n- I've already started using C++ as a backend so I don't have to implement STL containers and OOP. I experimented with TCC and found it small and fast, but I heard the code it compiles is not optimized at all.","metadata":{"transformedAt":"2026-08-18T18:32:17.790Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":61,"estimatedTokens":887}}380{"id":"stack-1451728","source":"stackoverflow","questionId":1451728,"title":"ANTLR Operator Precedence","tags":["compiler-construction","antlr","xtext"],"text":"Title: ANTLR Operator Precedence\nTags: compiler-construction, antlr, xtext\nSource: Stack Overflow\n\nQuestion:\nHow is operator precedence implemented in ANTLR? \n\nI'm using the XText/Antlr package at the moment.\n\nEdit: \n\nI did what sepp2k suggested, and operator precedence works now, but stuff like 3 +* also work now. The operators are basically \"falling through\" the tree.\n\nAlso, I tried the C grammar on ANTLR's website and the same thing happened in ANTLRworks.\n\nAnyone know what the issue is?\n\n```\nBinaryExpression:\n 'or'? AndOp; //or op\n\nAndOp:\n 'and'? ComparisonOp;\n\nComparisonOp:\n ('>'|'='|'<='|'=='|'~=')? ConcatOp;\n\nConcatOp:\n '..'? AddSubOp;\n\nAddSubOp:\n ('+' | '-')? MultDivOp;\n\nMultDivOp:\n ('*' | '/')? ExpOp;\n\nExpOp:\n '^'? expr=Expression;\n```\n\n========================================\n\nTop Answer:\nSince you use Xtext, I'd recommend to use the action concept of Xtext.\nThat is, a simple expression grammar would typically look similar to this one:\n\n```\nSum: Product ({Sum.left=current} operator=('+'|'-') right=Product)*;\nProduct: Atom ({Product.left=current} operator=('+'|'-') right=Atom)*;\nAtom: Number | Paren;\nParen: '(' Sum ')';\nNumber: value=INT;\n```\n\nPlease have a look at the docs for details.\n\n========================================\n\nCode:\n```text\nBinaryExpression:\n  'or'? AndOp; //or op\n\nAndOp:\n  'and'? ComparisonOp;\n\nComparisonOp:\n  ('>'|'<'|'>='|'<='|'=='|'~=')? ConcatOp;\n\nConcatOp:\n  '..'? AddSubOp;\n\nAddSubOp:\n  ('+' | '-')? MultDivOp;\n\nMultDivOp:\n  ('*' | '/')? ExpOp;\n\nExpOp:\n  '^'? expr=Expression;\n```\n\n```text\nExpr:  mult ('+' mult)* ;\nMult:  atom ('*' atom)* ;\nAtom:  INT | '(' expr ')' ;\n```\n\n```text\nSum: Product ({Sum.left=current} operator=('+'|'-') right=Product)*;\nProduct: Atom ({Product.left=current} operator=('+'|'-') right=Atom)*;\nAtom: Number | Paren;\nParen: '(' Sum ')';\nNumber: value=INT;\n```\n\n========================================\n\nComments:\n- The call to Expression should probably be between '(' and ')'. Also your operators all seem to be missing a left operand.\n- I fixed it by using the method found in my comment. Also, the left operand has been moved to the 1st expression to prevent left-recursion.\n- Maybe I could do stuff like AndOp: ('and' Expression) | ComparisonOp\n- To clarify the more detailed a grammar rule will match the greater its precedence. In the example above \"expr -> multi -> atom *\" is more detailed then the path to the plus sign. Therefore the * path has precedence.","metadata":{"transformedAt":"2026-08-18T18:32:17.790Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":103,"estimatedTokens":613}}381{"id":"stack-10700807","source":"stackoverflow","questionId":10700807,"title":"Converting a function to use tail recursion -- a formal study","tags":["compiler-construction","functional-programming","tail-recursion"],"text":"Title: Converting a function to use tail recursion -- a formal study\nTags: compiler-construction, functional-programming, tail-recursion\nSource: Stack Overflow\n\nQuestion:\nHas anyone written a formal paper describing a method to (automatically) convert functions to be tail recursive? I am looking for a university-level formal treatment including the limitations (types of functions that can be converted), procedures for conversion, and, if possible, proofs of correctness? Examples in Haskell would be a bonus.\n\n========================================\n\nTop Answer:\nMercury contains a couple of optimizations for automatically making things tail-recursive. (Mercury is an enforce-purity logic programming language, so it talks about predicates rather than functions, but many of the same ideas apply to Mercury programs as to Haskell ones. A much bigger difference than it being logical rather than functional is that it is strict rather than lazy)\n\n\"Accumulator introduction\" generates specialised versions of predicates with an extra accumulator parameter in order to allow associative operations to be moved before the recursive call. Apparently this optimisation doesn't necessarily result in tail-recursive predicates on its own, but often results in a form which can be optimised by the second optimisation: \n\n\"Last call modulo constructors\" essentially allows a recursive call that is followed only by constructor applications to be rewritten such that the value is constructed first containing a \"hole\" and then the recursive call returns its output directly into the memory address of the \"hole\" rather than using the normal return-value-passing convention. I believe Haskell would get this optimisation for free simply due to laziness, however.\n\nBoth of these optimisations are described in the paper Making Mercury programs tail recursive.\n\n========================================\n\nCode:\n```text\npredicate\n```\n\n```text\npredicate\n```\n\n========================================\n\nComments:\n- I did some Googling, but could not find any specific references. I was hoping that someone could provide a reference or two.\n- A CPS transform would certainly do the job in the most generic case (and some consequent optimisations might eliminate most of the resulting cruft). Tons of papers are published on this topic.\n- I thought the OP was looking for a tail recursion elimination too, but the way the question is worded the OP seems to be looking for the opposite (or even a generalization of the opposite) -- quote: \"convert functions to **be** tail recursive [emphasis mine]\"\n- The OP is asking about automatic conversion of recursive functions to tail-recursive form, in order to benefit from tail call elimination. He's not looking for tail call elimination itself.\n- The question is ambiguous. He isn't clear if he is looking for tail call elimination, or the tail recursion optimization in general. Either way, those papers are the place to start.\n- \"a method to (automatically) convert functions to be tail recursive\" seems pretty unambiguous to me. The mere fact that he's using the term \"tail recursion\" implies that he knows what tail-call elimination is, and that he asks for a university-level formal treatment suggests (although not conclusively) to me that he's not just accidentally misusing terms he doesn't fully understand. I may be wrong.\n- I've added sections on : identifying an expression with a recursive call in the tail position; GHC references for unfolding recursive calls to yield a tail call\n- I'm actually looking for ways to convert general recursive functions to be tail recursive. I know how to do that in most cases using accumulators, but a set of rules that works in general, and can describe functions that **cannot** be converted would be nice. If I can convert most function to be tail recursive, the compiler will optimize the tail-recursive call to a **goto**, at least for GHC (I believe) and Scala.\n- @Don Stewart: While investigating this, I found some implication that all functions can be converted to use CPS (continuation-passing style), and therefore consume heap instead of stack, but I could not find any formal \"proof\" of this. Thanks for the thorough answer.\n- In \"Real World Haskell\" (shop.oreilly.com/product/9780596514983.do) the authors state that most imperative language compilers do **not** do tail-call elimination, and therefore even writing code in a tail-recursive manner will not see benefits.","metadata":{"transformedAt":"2026-08-18T18:32:17.790Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":42,"estimatedTokens":1116}}382{"id":"stack-18051012","source":"stackoverflow","questionId":18051012,"title":"Implementing Closures in a Compiler","tags":["assembly","compiler-construction","lisp","closures"],"text":"Title: Implementing Closures in a Compiler\nTags: assembly, compiler-construction, lisp, closures\nSource: Stack Overflow\n\nQuestion:\nI am attempting to design a basic compiler to pseudo-assembly code. However, I cannot figure out how to implement closures. It seems I would need to associate specific register values with each \"subroutine\". I've considered use of stacks, but once again it seems insufficient. It seems like nothing short of an associative array would work, but how could that, or something similar, be done in assembly?\n\nThe example I have chosen to attempt to represent is the following, communicated as CoffeeScript for conciseness.\n\n```\n((x) -> (y) -> x(y))((x) -> x)(2)\n```\n\nHere is the general structure I have been trying. This is a sample of the pseudo-assembly to which I am compiling.\n\n```\n'((label lam1) ;; (x) -> x\n (cp resp arg)\n (ret)\n\n (label lam2) ;; (y) -> x(y)\n (jmp x)\n\n (label lam3) ;; (x) -> [(y) -> ...]\n (cp x arg) ;; this is the assignment intended for a closure\n (cp resp lam2) ;; this is a returned lambda, needing the closure\n (ret)\n\n (label main)\n (cp arg lam1)\n (call lam3)\n (set arg 2)\n (call resp)))\n```\n\nThis works; however, the value is simply set under the name `x` and then a lambda returned, the `x` value could be easily polluted prior to execution of the lambda.\n\nThe description of implementation in Structure and Interpretation of Computer Programs was the following, which does not seem doable to me in assembly. I don't know what other tactics they could be using.\n\n The procedure object will be constructed at run time by combining the current environment (the environment at the point of definition) with the entry point to the compiled procedure (a newly generated label).\n\nIn summary, **How can register values be associated with \"subroutines\"? Could stacks be sufficient?**\n\n========================================\n\nCode:\n```text\n((x) -> (y) -> x(y))((x) -> x)(2)\n```\n\n```text\n'((label lam1)   ;; (x) -> x\n  (cp resp arg)\n  (ret)\n\n  (label lam2)   ;; (y) -> x(y)\n  (jmp x)\n\n  (label lam3)   ;; (x) -> [(y) -> ...]\n  (cp x arg)     ;; this is the assignment intended for a closure\n  (cp resp lam2) ;; this is a returned lambda, needing the closure\n  (ret)\n\n  (label main)\n  (cp arg lam1)\n  (call lam3)\n  (set arg 2)\n  (call resp)))\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```javascript\nfunction bar(f) {\n    alert(f());\n}\n\nfunction foo(x) {\n    bar(function(){ return x; });\n}\n\nfoo(42);\n```\n\n```javascript\nfunction bar(f) {\n    to_call_later.push(f);\n}\n```\n\n```javascript\nfunction bar(f) {\n    alert(f());\n}\n\nfunction foo(x) {\n    var c1 = function() { return ++x; };\n    var c2 = function() { return x *= 2; };\n    bar(c1);\n    bar(c2);\n}\n\nfoo(42);  // displays 42+1=43 and 43*2=86 (not 42*2=84!)\n```\n\n```c\n#include <stdio.h>\n\ntypedef struct TClosure {\n    int (*code)(struct TClosure *env, int);\n    int state;\n} Closure;\n\nint call(Closure *c, int x) {\n    return c->code(c, x);\n}\n\nint adder_code(Closure *env, int x) {\n    return env->state + x;\n}\n\nint multiplier_code(Closure *env, int x) {\n    return env->state * x;\n}\n\nClosure make_closure(int op, int k) {\n    Closure c;\n    c.state = k;\n    c.code = (op == '+' ? adder_code : multiplier_code);\n    return c;\n}\n\nint main(int argc, const char *argv[]) {\n    Closure c1 = make_closure('+', 10);\n    Closure c2 = make_closure('*', 3);\n    printf(\"c1(3) = %i, c2(3) = %i\\n\",\n           call(&c1, 3), call(&c2, 3));\n    return 0;\n}\n```\n\n```text\nx\n```\n\n```text\nfoo\n```\n\n```text\nfoo\n```\n\n```text\nfoo\n```\n\n```text\nx\n```\n\n```text\nfoo\n```\n\n```text\nx\n```\n\n```text\nfoo\n```\n\n```text\nx\n```\n\n```text\nfoo\n```\n\n```text\nx\n```\n\n```text\nesp\n```\n\n```text\nesi\n```\n\n```text\n(esi)\n```\n\n```text\n(esi+4)\n```\n\n```text\n(esi+8)\n```\n\n```text\nmalloc\n```\n\n```text\nClosure\n```\n\n========================================\n\nComments:\n- Have you studied Structure and Interpretation of Computer Programs?\n- @Barmar SICP says, \"The procedure object will be constructed at run time by combining the current environment (the environment at the point of definition) with the entry point to the compiled procedure (a newly generated label).\" That seems impractical to me. How could the entire environment be cached at *run-time*?\n- BTW: Your problem is called The Funarg problem. You might find this document interesting (PDF)\n- @Sylwester Thanks! It turned out to be pretty easy for exclusively pure functions.\n- @matt3141 Really? This is one of my main concerns when code gets dynamic. One on my test cases: gist.github.com/anonymous/ff4ff297bf9f025c4ceb\n- @Sylwester Since I am compiling a very simple and strictly functional language, I think it was made easier. Your test case includes IO, mine will have none. Perhaps that adds to the complexity? It certainly forces the code to be more dynamic.\n- @matt3141 True. Side effects is what ruins it for functional languages. Without it you could constant fold the whole thing.\n- en.wikipedia.org/wiki/Lisp_In_Small_Pieces\n- Thank you, I had an intuitive sense for the issue of \"lifetime\" but couldn't articulate it. This clears things up. Would you say this becomes less of an issue with exclusively pure functions?\n- @matt3141: in case of pure functions (i.e. closures whose return value depends on parameters and captured state but in which this state is immutable) then things can be simplified and a stack is in theory enough. See edit...\n- Great! The edit is what I needed in my case, but the earlier contents provide helpful information for the future. My goal is to make a compiler of a purely functional language in which I will write an interpreter, within the interpreter I can allow for better closures. I assume this is the reason why good closure implementations are mostly in interpreted languages.\n- @mat3141: Lisp has good closure implementations, and its SBCL implementation is entirely compiled (even for the REPL). So you can implement closures in compilers. BTW, MELT compiles to C and has good (garbage collected) closures","metadata":{"transformedAt":"2026-08-18T18:32:17.790Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":26,"totalLines":234,"estimatedTokens":1491}}383{"id":"stack-2960440","source":"stackoverflow","questionId":2960440,"title":"What are the disadvantages of targeting the JVM instead of x86?","tags":["compiler-construction","jvm"],"text":"Title: What are the disadvantages of targeting the JVM instead of x86?\nTags: compiler-construction, jvm\nSource: Stack Overflow\n\nQuestion:\nI'm developing a new language. My initial target was to compile to native x86 for the Windows platform, but now I am in doubt.\n\nI've seen some new languages target the JVM (most notable Scala and Clojure). Ofcourse it's not possible to port every language *easily* to the JVM; to do so may lead to small changes to the language and it's design.\n\nAfter posing this question, I even doubted more about this decision. I now know some \"pro\" JVM arguments. The original question was: *is targetting the JVM a good idea, when creating a compiler for a new language?*\n\nUpdated the question: **What are the disadvantages of targeting the JVM instead of x86 on Windows?**\n\n========================================\n\nTop Answer:\nYou may want to look at targeting the LLVM instead of the JVM. The LLVM can be used to target a number of architectures, including x86.\n\nThere's more to portability than simple CPU support, but the LLVM can help a lot and still give you native code, if you like.\n\n========================================\n\nComments:\n- No dynamic typing? That'll come as a surprise to the dynamically-typed scripting languages already running on the JVM...\n- Great question, very interesting. I suggest you enhance the title, so that it mentions the intention to use the JVM for a compiler.\n- And there's no such thing as \"native dynamic typing\". The VM runtime either supports it or it doesn't, the native machine is much too low level for that sort of concept.\n- @skaffman, as static typing is a high-level concept for dynamic typed VM's, dynamic typing is high-level for static typed VM's. Let's say: with \"native\" I mean it's implemented at the lowest level, not at a higher level.\n- @skaffman: have you looked at the bizarre contortions those dynamically-typed scripting languages have to go through to make it work?\n- @Michael Borgwardt: they don't go through bizarre contortions to make it *work*, they go to bizarre contortions to make it *fast*. But that is really not something unique to the JVM. Natively implemented dynamic language *also* go to bizarre contortions to make it fast, it's just different contortions. Have you looked into the implementations of any high-performance Smalltalk VMs? Actually, HotSpot *is* a re-branded high-performance Smalltalk VM!\n- How *could* tail recursion be supported at the bytecode level? If an exception occurs within a recursive call, the stack-trace is supposed to include all the nested calls, is it not? If a language specifies that something which looks like a tail-recursive call may not generate a stack-trace entry, then replacing it with a loop would be legitimate, but unless the language specifies that I don't think the JVM should make that inference.\n- I guess the JVM could implement tail recursion in a backwards compatible way where language implementers could indicate to the JVM (perhaps at a method level) that they don't care about the full stack-trace. Languages that want TCO could ask for this, everyone else would get the old behaviour.\n- I think it would be better to have a tail-call construct to the language, and require programs using that feature to employ a newer version of the JVM (which likewise supports it). There is no way a program targeting the old JVM can support million-deep \"recursion\" while having tail calls be as efficient as conventional calls, and having the old JVM convert tail calls to conventional calls would be worse than having it simply refuse outright to load the code.","metadata":{"transformedAt":"2026-08-18T18:32:17.790Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":32,"estimatedTokens":904}}384{"id":"stack-13260695","source":"stackoverflow","questionId":13260695,"title":"What other neat tricks does the SpecialNameAttribute allow?","tags":["c#","vb.net","compiler-construction","attributes"],"text":"Title: What other neat tricks does the SpecialNameAttribute allow?\nTags: c#, vb.net, compiler-construction, attributes\nSource: Stack Overflow\n\nQuestion:\nIn researching a question on implementing the Visual Basic Power (`^`) operator, I learned that the `System.Runtime.CompilerServices.SpecialNameAttribute` class allows one to implement this operator in C# for use in VB.\n\nInterestingly, the documentation states:\n\n The **SpecialNameAttribute** class is not currently used in the .NET Framework, but is reserved for future use.\n\nMy question is: What other neat tricks are available when using this attribute? \n\n(side question - is the documentation inaccurate or is this a semantic issue since the attribute is apparently used by the *compiler* but not the *framework*?)\n\n========================================\n\nCode:\n```text\n^\n```\n\n```text\nSystem.Runtime.CompilerServices.SpecialNameAttribute\n```\n\n========================================\n\nComments:\n- Thanks Hans - your last statement sums up my question - what other hidden gems (other than operators and getters/setters) do the compilers allow by using that attribute?\n- There is no reason whatsoever to assume that compilers use *specialname* for any other purpose than those outlined in ECMA 335.","metadata":{"transformedAt":"2026-08-18T18:32:17.790Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":31,"estimatedTokens":314}}385{"id":"stack-4565334","source":"stackoverflow","questionId":4565334,"title":"Is there a programming language with semantics close to English?","tags":["parsing","compiler-construction","programming-languages","nlp"],"text":"Title: Is there a programming language with semantics close to English?\nTags: parsing, compiler-construction, programming-languages, nlp\nSource: Stack Overflow\n\nQuestion:\nMost languages allow to 'tweek' to certain extend parts of the syntax (C++,C#) and/or semantics that you will be using in your code (Katahdin, lua). But I have not heard of a language that can just completely define how your code will look like. So isn't there some language which already exists that has such capabilities to override **all** syntax & define semantics ? \n\nExample of what I want to do is basically from the C# code below:\n\n```\nforeach(Fruit fruit in Fruits)\n{\n if(fruit is Apple)\n {\n fruit.Price = fruit.Price/2;\n }\n}\n```\n\nI want do be able to to write the above code in my perfect language like this:\n\n```\nCheck if any fruits are Macintosh apples and discount the price by 50%.\n```\n\nThe advantages that come to my mind looking from a coder's perspective in this \"imaginary\" language are:\n\n- It's very clear what is going on (self descriptive) - it's plain English after all even kid would understand my program\nHides all complexities which I have to write in C#. But why should I care to learn that\nif statements, arithmetic operators etc since there are already implemented\n\nThe disadvantages that I see for a coder who will maintain this program are:\n\nMaybe you would express this program differently from me so you may not get all the\ninformation that I've expressed in my sentence\n\n- Programs can be quite verbose and hard to debug but if possible to even proximate this type of syntax above maybe more people would start programming right? That would be amazing I think. I can go to work and just write an essay to draw a square on a winform like this:\n\n`Create a form called MyGreetingForm. Draw a square with in the middle of \nMyGreetingFormwith a side of 100 points. In the middle of the square write \"Hello! Click here to continue\" in Arial font.`\n\nIn the above code the parser must basically guess that I want to use\n the unnamed square from the previous sentence, it'd be hard to write such a smart parser I guess, yet it's so simple what I want to do.\n\n`If the user clicks on square in the middle of MyGreetingForm show MyMainForm.` \n\nIn the above code 'basically' the compiler must: 1)generate an event handler 2) check if there is any square in the middle of the form and if there is - 3) hide the form and show another form \n\nIt looks very hard to do but it doesn't look impossible IMO to me at least approximate this (I can personally generate a parser to perform the 3 steps above np & it's basically the same that it has to do any way when you add even in c# `a.MyEvent=+handler;` so I don't see a problem here) so I'm thinking maybe somebody already did something like this ? Or is there some practical burden of complexity to create such a 'essay style' programming language which I can't see ? I mean what's the worse that can happen if the parser is not that good? - your program will crash so you have to re-word it:)\n\n========================================\n\nTop Answer:\nSome Interactive fiction designers use a language syntax extremely close to the English language. Here's some Inform 7 code, which you can play online:\n\n```\nThe foyer is a room.\n\nThe apple is in the foyer. It is edible. The description is \"This is a ripe, \ngreen granny smith apple.\"\n\nThe apple core is a thing. The description is \"This apple core all that is \nleft of that granny smith apple you just consumed.\"\n\nAfter eating the apple: \n now the apple core is in the player;\n say \"You gobble down the apple careful not to eat any of those cyanide-\n laced seeds you heard about.\"\n```\n\nI tutored a course that used Inform 7. One of the tutors had the impression the assignment was to design, not write a game. So he marked the programs by reading them, without realising they were actual programs.\n\n========================================\n\nCode:\n```text\nforeach(Fruit fruit in Fruits)\n{\n  if(fruit is Apple)\n  {\n    fruit.Price =  fruit.Price/2;\n  }\n}\n```\n\n```text\nCheck if any fruits are Macintosh apples and discount the price by 50%.\n```\n\n```text\nCreate a form called MyGreetingForm. Draw a square with in the middle of \nMyGreetingFormwith a side of 100 points. In the middle of the square write \"Hello! Click here to continue\" in Arial font.\n```\n\n```text\nIf the user clicks on square in the middle of MyGreetingForm show MyMainForm.\n```\n\n```text\na.MyEvent=+handler;\n```\n\n```text\nThe background is a picture.\n\nA button has a box and a name.\n\nTo clear the status:\n  Clear the status' string.\n  Show everything.\n\nTo create the background:\n  Draw the screen's box with the white color.\n  Loop.\n  Pick a spot anywhere in the screen's box.\n  Pick a color between the lightest gray color and the white color.\n  Dab the color on the spot.\n  If a counter is past 80000, break.\n  If the counter is evenly divisible by 1000, refresh the screen.\n  Repeat.\n  Extract the background given the screen's box. \\or Create the background from the screen. Or something.\n```\n\n```text\nThe foyer is a room.\n\nThe apple is in the foyer. It is edible. The description is \"This is a ripe, \ngreen granny smith apple.\"\n\nThe apple core is a thing. The description is \"This apple core all that is \nleft of that granny smith apple you just consumed.\"\n\nAfter eating the apple:  \n  now the apple core is in the player;\n  say \"You gobble down the apple careful not to eat any of those cyanide-\n  laced seeds you heard about.\"\n```\n\n```text\nprint{create a string from the file called \"README.txt\"}\nprint{save the string \"Woohoo!\" to a file called \"ExampleText.txt\"}\nprint{the first 3 letters of \"EngScript\"}\n```\n\n```text\nprint(pythonFunctions.stringFromTextFile(\"README.txt\"))\nprint(pythonFunctions.writeStringToFile(\"ExampleText.txt\", \"Woohoo!\"))\nprint(\"EngScript\"[0:(3 - 1)+1])\n```\n\n========================================\n\nComments:\n- I know a lot of people who talk in LOLspeak\n- There have been many attempts at \"semantic languages\", and only 2 things are consistent: 1.) they looked like a mess, and 2.) they were never really semantic.\n- In my opinion, your \"perfect language\" isn't clear at all. I read the first example as \"if any fruit is Macintosh apples, then discount the total price by 50%,\" not as \"for each fruit that is a Macintosh apple, discount its price by 50%.\" The C# code, on the other hand, is perfectly clear.\n- umm... not sure if you will find many useful answers, as this type of language (if possible, which I don't think it is) would eliminate most of our jobs.\n- @Rafe Kettler: Yeah I suspect, is there anything I can try anyway ?\n- I went into programming to *avoid* writing essays. (Well, not really, but I could have.)\n- What if I have another property in Fruit called PriceWithDiscount and another property called Discount and I want \"apply discount\" to calculate *those* properties instead of altering the original price, which should stay the same? How would the compiler figure all that out from reading \"apply discount\"?\n- @Greg Hewgill: I like both and I'd prefer to write an essay as I have very short memory for syntax - I barely learned to speak English actually and I was young. Maybe I'll never learn to speak another language in my life - very hard for me. So in programming I have to google stuff even if I've done them 10s of times\n- @ivo, English is not clear and it is not deterministic. Trying to figure out operations from a subjective sentence is a brittle process. That is why the communication among humans is so hard and we always have so many misunderstandings: our languages are not really as clear as you think.\n- @jondavidjohn Hehe, no, it wouldn't. To the contrary. Programmers would then be like attorneys, trying to find out what the law (the source code) really means.\n- +1 that's just stupidly awesome. I might use my 'google time' for this. :3\n- Will my computer explode if I start the sample.exe from the zip file I can download from the page you linked to?\n- @Martin: Mine exploded, beware of that ;-)\n- In theory, it would be possible to write an interpreter for this language. It appears to be a kind of self-extending natural language user interface.\n- ...and there is at least one open-source implementation of such a system. It is called EnglishScript.\n- In Plain English, the original poster's example becomes: Loop. Get a fruit from the fruits. If the fruit is nil, break. If the fruit's kind is \"apple\", divide the fruit's price by 2. Repeat. (Each sentence can optionally be a separate line of source code. Capitalization and indentation are optional.)\n- Yeah true, debugging would be crazy difficult. If there is spelling mistakes they can be pointed out though I think ?\n- Google Search uses a natural language user interface, but it isn't a natural language programming system.","metadata":{"transformedAt":"2026-08-18T18:32:17.790Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":171,"estimatedTokens":2203}}386{"id":"stack-4974617","source":"stackoverflow","questionId":4974617,"title":"What is more efficient stack memory or heap?","tags":["c++","memory","compiler-construction"],"text":"Title: What is more efficient stack memory or heap?\nTags: c++, memory, compiler-construction\nSource: Stack Overflow\n\nQuestion:\n**Possible Duplicate:**\n\n C++ Which is faster: Stack allocation or Heap allocation \n\nWhat is more efficient from memory allocation perspective - stack memory or heap memory? What it depends on?\n\nObviously there is an overhead of dynamic allocation versus allocation on the stack. Using heap involves finding a location where the memory can be allocated and maintaining structures. On the stack it is simple as you already know where to put the element. I would like to understand what is the overhead in worst case in milliseconds on supporting structures that allow for dynamic allocation?\n\n========================================\n\nTop Answer:\nAllocating/freeing on the stack is more \"efficient\" because it just involves incrementing/decrementing a stack pointer, typically, while heap allocation is generally much more complicated. That said, it's generally not a good idea to have huge things on your stack as stack space is far more limited than heap space on most systems (especially when multiple threads are involved as each thread has a separate stack).\n\n========================================\n\nComments:\n- efficient for what?\n- Voting to close as NARQ.\n- The obvious answer is the stack but the stack, itself, governs the lifecycle (which could be very limiting) on anything you allocate on.\n- I think that this is a very reasonable question. If you aren't already sure what the answer is (that it depends on what you're doing), there's no way to ask a more nuanced question. I think we should reopen this.\n- @templatetypedef: Agreed. The question could probably use some clarification, but I don't think it needed closing.\n- I also voted to re-open. I don't think that the question is ambiguous or poorly formed at all.\n- This is the wrong question. In 99% of the time the difference should not make any difference to you. The question of dynamic or automatic should solely be done based on its usage. In the 1% of cases where it does matter that fact that you know it matters means that you know how to do the appropriate stuff.\n- Why would having *more* stack space through multiple stacks mean stack space is \"especially\" limited?\n- @Fred per-thread stack space is more limited. On single-threaded systems you could have the stack start at one end of memory and have your heap \"grow\" from the other end, so you could theoretically use your entire virtual address space for stack (if you didn't use heap). Once you have threads each thread needs its own stack. The way that's commonly done is to allocate a relatively small slice of the address space for each stack. The more threads you expect the smaller each slice has to be. This is presumably less of an issue on 64-bit machines given that they have far more address space.\n- I should probably add that I'm assuming you're on a system with a \"flat\" address space. If you're using segmented pointers you could have each thread's stack be on a different segment. Also, some languages/runtimes do fancy stuff with the stack so they can be expandedrelocated. I believe Go may actually do this, though I don't see how you could do this in C++ (or C) without breaking the semantics of pointers to the stack.\n- With a 64-bit address space, you could leave room for a rather large per-thread stack, mapping in actual memory one page at a time, as needed. Having said that, the usual reason people run out of stack space is uncontrolled recursion, not a genuine need for more memory.\n- @Steven: yeah, I mentioned that this is less of an issue on 64 bit machines in a comment above. Uncontrolled recursion is certainly one reason people run out of stack, though I don't know if it's the \"usual reason\". It seems most C and C++ programmers have actually had it drilled into them that \"recursion is inefficient and/or bad\" when one of the real problems is that you just can't recurse very deep with the kind of stack you typically have on a 32-bit platform that supports multi-threading. The tradeoffs are a bit different when a 1MB stack becomes a 4GB stack.\n- Well said. I guess the stack could also be used up by large local arrays and calls to calloc, but in my experience, recursion is it, because any recursion that creates an infinite loop will overflow the stack.\n- The stack size depends on your platform, and your application. If you create a worker thread, you can typically even define the stack size. There's definitely no magic number like \"1MB\".\n- Where a factor of 1000 comes from?\n- @Leonid: I have no idea. It would vary depending on the system, and, frankly, I've never timed it.\n- @Leonid, 1000 times is probably an overstate but: it will require a CAS at least (likely to be a cache-miss as well), few cache-misses for the new memory, a cache miss is like 300cpu clocks. Stack allocation is a single CPU instruction that's necessary for the method body anyways and the stack is likely to be in the CPU cache.\n- @Stephane Rolland Windows' stack size used to default to 1MB at some point (still is?), there is no universal number, though.\n- +1, this is accurate. One cpu instruction to adjust the stack pointer vs acquiring the heap lock and find the right hole to return. Three orders of magnitude is about right.\n- @EboMike @Leonid @bestsss... Twice did I write \"something like\"... you undestand: it's an estimation... something to keep in mind when coding... Where does this figure come from ? talking with a lot of developers, talented most of the time, I learn a lot from them. I never measured it exactly either, but for me it makes sense. You definitely miss the point when arguing about magic numbers and universal numbers...\n- @Stephane Rolland, I am mostly agreeing w/ ya. Reread what I have wrote, plus you will never see any voting from me :)\n- @bestsss no problem, I was still arguing about my point of view :) However it seems the 1000 is really overestimated. cf this SO question I asked after wondering about that: stackoverflow.com/questions/4977726/&hellip;\n- @Stephane Rolland, the 1st heap alloc can be 1000times slower easily, if it misses the cache, the performance depends mostly on the cache misses","metadata":{"transformedAt":"2026-08-18T18:32:17.791Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":44,"estimatedTokens":1557}}387{"id":"stack-10372082","source":"stackoverflow","questionId":10372082,"title":"Porting compiler from x86 Assembly to LLVM","tags":["c++","assembly","compiler-construction","x86","llvm"],"text":"Title: Porting compiler from x86 Assembly to LLVM\nTags: c++, assembly, compiler-construction, x86, llvm\nSource: Stack Overflow\n\nQuestion:\nSo recently, in an attempt to hone my assembly skills, I wrote a VERY simple compiler for a toy language in C++. It runs single pass, and directly emits code during the parsing phase to several string streams, each representing a section of the code (i.e one represents`section .bss`, while others represent `.data` and `.text`). Afterwards, these string streams are written to a file, and I use NASM and gcc to assemble and link them. I know that this single-pass approach is horribly inefficient, but again, this was more of an exercise in understanding the code-generation stage than anything else. Anyway, I would like to modify my code to directly emit LLVM IL instead of raw assembly, again as a learning exercise. Is there any introductory level guide to LLVM IL? Or, even better, a tool to determine the equivalent IL code for a line of assembly? I looked, and I only found the complete spec, which is WAY more information than I need.\n\n========================================\n\nCode:\n```text\nsection .bss\n```\n\n```text\n.data\n```\n\n```text\n.text\n```\n\n========================================\n\nComments:\n- There won't be a one to one mapping. No shortcuts here.\n- I realize this, I'm just looking for a guide on how to emit basic things, like reserving data, cmp and conditonal jumps, etc.\n- The LLVM docs. has a (at least one) tutorial that walks you through creating a compiler for a toy language.\n- I saw that, but it's extremely dated, and most of the example code doesn't work.\n- Is this project available somewhere to look at? Would be interesting...\n- Thank you! I had never seen that demo page, that's exactly what I was looking for.\n- @chameco: The demo page is really useful, personally I often use it to check what kind of optimization Clang can perform (or not). Really helps to make people see the light and avoid having them getting too low-level in their code.\n- I have a slightly modified version of the page that let's you look at assembly language for other LLVM targets as well: ellcc.org/demo","metadata":{"transformedAt":"2026-08-18T18:32:17.791Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":33,"estimatedTokens":539}}388{"id":"stack-11003376","source":"stackoverflow","questionId":11003376,"title":"Extract global variables from a.out file","tags":["gcc","compiler-construction","cygwin","elf","dwarf"],"text":"Title: Extract global variables from a.out file\nTags: gcc, compiler-construction, cygwin, elf, dwarf\nSource: Stack Overflow\n\nQuestion:\n### Edit (updated question)\n\nI have a simple C program: \n\n```\n// it is not important to know what the code does you may skip the code\n```\n\n**main.c**\n\n```\n#include \n\nunsigned int AppCtr;\nunsigned char AppFlag;\nint SOME_LARGE_VARIABLE;\n\nstatic void AppTest (void);\n\nvoid main (void)\n{\n AppCtr = 0;\n AppFlag = 0; \n AppTest();\n}\n\nstatic void Foo(void){\n SOME_LARGE_VARIABLE=15; \n}\n\nstatic void AppTest (void)\n{\n unsigned int i;\n i = 0;\n while (i **bsp.c**\n\n```\nextern int SOME_LARGE_VARIABLE;\nextern unsigned char AppFlag;\n\nunsigned int long My_GREAT_COUNTER;\n\nvoid BSP_Test (void) {\n SOME_LARGE_VARIABLE = 5;\n My_GREAT_COUNTER = 4;\n}\n```\n\n(the program does not do anything useful... **My goal is to extract the variable names their location where they are being declared and their memory address**)\n\nWhen I compile the program I get the file `a.out` which is an elf file containing debug information.\n\nSomeone on the company wrote a program in .net 5 years ago that will get all this information from the a.out file. This is what the code returns:\n\n```\n// Name Display Name Type Size Address\n```\n\nFor this small program it works great and also for other large projects.\n\nThat code is 2000 lines long with several bugs and it does not support .NET version 4. That's why I am trying to recreate it. \n\n**So my question is**, I am lost in the sense that I don't know what approach to take in order to solve this problem. These are the options I have been considering:\n\nOrganize the buggy code of the program I showed on the first image and try to see what it does and how it parses the a.out file in order to get that information. Once I fully understand it try to figure out why it does not support version 3 and 4.\n\nI am ok at creating regex expressions so maybe try to look for the pattern in the a.out file by doing something like: So far I was able to find the pattern where there is just one file (main.c). But when there are several files it get's more complicated. I haven't tried it yet. Maybe it will be not that complicated and it will be possible to find the pattern.\n\nInstall Cygwin so that I can use linux commands on windows such as `objdump`, `nm` or `elfread`. I have't played enough with the commands when I use those commands such as `readelf -w a.out` I get way more information that I need. There are some cons why I have not spend that much time with this approach:\n\nCons: It takes a while to install cygwin on windows and when giving this application to our customers we don't want them to have to install it. Maybe there is a way of just installing the commands objdump and elfread without having to install the whole thing\n\nPros: If we find the right command to use we will not be reinventing the wheel and save some time. Maybe it is a matter of parsing the results of a command such as `objdump -w a.out`\n\nIn case you want to download the a.out file in order to parse it here it is. \n\n### Summary\n\n**I will to be able to get the global variables on a.out file. I will like to know what type each variable is (int, char, ..), what memory address they have and I will also like to know on what file the variable is being declared (main.c or someOtherFile.c). I will appreciate if I don't have to use cygwin as that will make it more easy to deploy.** Since this question asks for a lot, I attempted to split it into more:\n\n- objdump/readelf get variables information\n\n- Get location of symbols in a.out file\n\nperhaps I should delete the other questions. sorry being redundant.\n\n========================================\n\nCode:\n```text\n// it is not important to know what the code does you may skip the code\n```\n\n```text\n#include <bsp.h>\n\nunsigned int   AppCtr;\nunsigned char  AppFlag;\nint SOME_LARGE_VARIABLE;\n\nstatic  void  AppTest (void);\n\nvoid  main (void)\n{\n    AppCtr  = 0;\n    AppFlag = 0;        \n    AppTest();\n}\n\nstatic void Foo(void){\n    SOME_LARGE_VARIABLE=15; \n}\n\n\nstatic  void  AppTest (void)\n{\n    unsigned int  i;\n    i = 0;\n    while (i < 200000) {\n        i++;\n    }\n\n    BSP_Test();      \n    SOME_LARGE_VARIABLE=3;    \n    Foo();\n}\n```\n\n```text\nextern int SOME_LARGE_VARIABLE;\nextern unsigned char  AppFlag;\n\nunsigned int long My_GREAT_COUNTER;\n\nvoid  BSP_Test (void) {\n  SOME_LARGE_VARIABLE = 5;\n  My_GREAT_COUNTER = 4;\n}\n```\n\n```text\n//  Name          Display Name                    Type      Size     Address\n```\n\n```text\na.out\n```\n\n```text\nobjdump\n```\n\n```text\nnm\n```\n\n```text\nelfread\n```\n\n```text\nreadelf -w a.out\n```\n\n```text\nobjdump -w a.out\n```\n\n```text\n// cd \"path where readelf.exe is\"\nreadelf.exe -s a.out\n```\n\n```text\nreadelf.exe\n```\n\n```text\nreadelf.exe -w a.out\n```\n\n========================================\n\nComments:\n- Is your goal to get a list of global variables? Or is it get global variables from an a.out file? Or is it to repair to the tool you have? Clearly, if have more than one compilation unit, looking in *one* a.out file won't give you a list of the all the global variables.\n- ... it also appears that much of your trouble is a lack of understanding of the object file format. If you aren't willing to invest enough time to understand it, how do you expect to get a good result?\n- The a.out file contains dwarf information (debugging information). I know that not all a.out files contain this info. Sorry for not mentioning that....\n- It looks like a duplicate of stackoverflow.com/questions/6082950/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.793Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":13,"totalLines":193,"estimatedTokens":1379}}389{"id":"stack-9248533","source":"stackoverflow","questionId":9248533,"title":"How does a linker know what all libraries to link?","tags":["c++","gcc","compiler-construction","linker"],"text":"Title: How does a linker know what all libraries to link?\nTags: c++, gcc, compiler-construction, linker\nSource: Stack Overflow\n\nQuestion:\nFrom http://www.learncpp.com/cpp-tutorial/19-header-files/\n\nHow does the linker know that it needs to include the standard runtime library and for example the definitions of the functions declared in iostream are not present elsewhere?\n\nIs there some mapping that facilitates the linking process?\n\nTo rephrase - **If I include some file with only function declarations, how does any linker figure out where the function definitions are?**\n\n========================================\n\nTop Answer:\nThis is just one of the default settings for your linker. Generally, you can tell it *not* to link the standard libraries if you don't wish to use them.\n\nAnd you *always* have to tell the linker explicitly to link in any additional libraries that your application makes use of. It isn't going to \"figure out\" where the function definitions based solely on included declarations.\n\n========================================\n\nCode:\n```text\ng++\n```\n\n```text\n-lstdc++\n```\n\n```text\ngcc\n```\n\n========================================\n\nComments:\n- For Linux env, such info is stored in the elf file gcc generated.\n- By the way, is the *whole* standard library always linked? I mean: For example assume that I have used only the `printf` function. In that case, is only the binary containing `printf` linked or the binary of the whole standard library linked?","metadata":{"transformedAt":"2026-08-18T18:32:17.793Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":40,"estimatedTokens":370}}390{"id":"stack-2426696","source":"stackoverflow","questionId":2426696,"title":"How much faster are register based architectures than stack architectures?","tags":["compiler-construction"],"text":"Title: How much faster are register based architectures than stack architectures?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nStudying compilers course, I am left wondering why use registers at all.\nIt is often the case that the caller or callee must **save** the register value and then **restore** it.\n\nIn a way they always end up using the stack anyway. Is creating additional complexity by using registers really worth it?\n\nExcuse my ignorance.\n\nUpdate: Please, I know that registers are faster than RAM and other types of cache. My main concern is that one has to \"save\" the value that is in the register and the \"restore\" it to the register afterwards. In both cases we are accessing some kind of cache. Would it not be better to use cache in the first place?\n\n========================================\n\nTop Answer:\nWell, well it seems the answer to this was also in the book (modern compiler implementation in java).\nThe book presents 4 answers:\n\n- Some procedures don't call other procedures. If you draw the diagram of procedure calls, and assume that each procedure calls on average 1-2 other procedures, you come up with a tree, in which the \"leafs\" (the procedures that don't call others) outnumber the tree non-left nodes. So you win that way. Some compilers don't allocate a stack frame at all for these leaf nodes.\n\n- Some optimizing compilers use \"interprocedural register allocation\" - which basically means they analyse all of your source code and make smart ways of storing arguments to procedures ahead of time, thus minimizing writing to stack.\n\n- Some procedures are done with a variable before they call another function - in which case that register can be just overwritten.\n\n- Some architectures use \"register windows\", so that each function invocation can allocate fresh set of registers without memory traffic.\n\n========================================\n\nComments:\n- I don't understand what you mean by saving and restoring here, because you're being far too vague about when it happens and why.\n- Caller-saved vs. calle-save registers. When caller calls a calle function, it can choose to put values (like variables or parameters) into registers. If it is a caller-save register - the caller must take measures to \"save\" the value before it calls the \"callee\" and than restore them once the \"collee\" returns.\n- However, most programs tend to do a lot of processing in functions, and don't spend all of their time calling and returning.\n- You cannot leave the values on the stack all the time, as when passing them into the function, they need to be a the top of the stack, and in a given order, as expected by the function. This means the complexity of the parameter passing is always there, even if you are not using registers. To make it even more complicated, sometimes you can pass a few arguments in registers.\n- Most CPU instructions (like add, multiply, or compare) can not operate on general memory addresses, which of course is where the stack is located. That is why you have to move the value into a register first. The swapping in/out is due to the flow of the program like function calls where you need to restore the state of the CPU upon return.\n- Two times speedup is nothing. You get that for switching from Java to C++. The actual differences are orders of magnitudes (if talking of not using registers at all).\n- Yes I understand that. But we often-times must \"save\" and \"restore\" values into and from registers. That is my main concern. Updated question.\n- See added paragraph in an answer above.\n- But why could not CPUs just use the register file as a \"CPU 0 level cache\", specifically targeted to work with stack?\n- Even without any interprocedural analysis, if procedures are allowed to 'trash' certain 'scratch' registers but are required to preserve others, then provided enough registers exist, leaf routines won't have to save any registers (they can just use the 'scratch' registers), and non-leaf routines will use non-scratch registers only for values they actually need to have preserved. Unless an inner loop calls function calls two-deep, there will likely end up being zero register save/restore operations within the inner loop.","metadata":{"transformedAt":"2026-08-18T18:32:17.793Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":41,"estimatedTokens":1052}}391{"id":"stack-7247306","source":"stackoverflow","questionId":7247306,"title":"Is it possible to create C# language modifications as did LINQ?","tags":["c#","linq","compiler-construction","language-design"],"text":"Title: Is it possible to create C# language modifications as did LINQ?\nTags: c#, linq, compiler-construction, language-design\nSource: Stack Overflow\n\nQuestion:\nI've been looking quite a bit at Mr. Skeet's blog on how to re-implement LINQ.\n\nIn particular, he states that the code:\n\n```\nvar list = (from person in people\n where person.FirstName.StartsWith(\"J\")\n orderby person.Age\n select person.LastName)\n .ToList();\n```\n\nis translated to methods that are extension methods that are provided by the LINQ library:\n\n```\npeople.Where(person => person.FirstName.StartsWith(\"J\"))\n .OrderBy(person => person.Age)\n .Select(person => person.LastName)\n```\n\nBY THE COMPILER.\n\nMy question is, how does one impress the bigwigs enough with a library to cause them to allow the language to change to support the library? Or were those words already reserved before LINQ came along?\n\n========================================\n\nTop Answer:\nI highly doubt the compiler developers would implement syntax extensions for just any library. If you really wanted language-level integration, you could develop a pre-processor that transforms your custom syntax into valid C#. That's essentially what the compiler does with LINQ anyway (as you pointed out in your question).\n\nOf course, you would lose things like auto-complete and syntax highlighting in Visual Studio, but that could be fixed with an extension.\n\n========================================\n\nCode:\n```text\nvar list = (from person in people\n        where person.FirstName.StartsWith(\"J\")\n        orderby person.Age\n        select person.LastName)\n       .ToList();\n```\n\n```text\npeople.Where(person => person.FirstName.StartsWith(\"J\"))\n  .OrderBy(person => person.Age)\n  .Select(person => person.LastName)\n```\n\n```text\npublic ref int Max(ref int x, ref int y) { if (x>y) return ref x; else return ref y; }\n```\n\n========================================\n\nComments:\n- become Jon Skeet?\n- Microsoft \"big wigs\" *love* cookies....\n- You realize LINQ was developed by Microsoft, the same company that dictated C#, .NET and the CLR, right?\n- I'm not sure why this was closed. Obviously OP won't be able to convince the compiler devs to implement new syntax for his or her library, but this question can easily be answered with \"write a pre-processor that runs before the compiler.\"\n- @BoltClock: I suspected as much but it seemed odd that you would have to include the library if it was part of the language to begin with.\n- @David Brown: I like that idea.\n- @user420667 There is nothing odd that in Microsoft different projects are compiled in different dll's. .NET FCL contains many dll.\n- See stackoverflow.com/questions/5780648/&hellip; for the canonical \"why not add syntax\" answer.\n- @VMAtm: Yes, but you don't expect to have to import a library to be able to use access modifiers or other parts that you treat as the language. I'm not saying it's at all that it's a bad design choice, it just struck me as odd.\n- @user420667 You don't have to import the library for the syntax. You have to import the library to use the syntax *with `IEnumerable`*. You could write classes that provide the methods to which the special syntax compiles.\n- You can't do what every you want by changing the compiler. Since it just turns C# into CLR, you are still restricted to the rules of CLR. You'll need to modify the .NET framework itself *and* the compiler.\n- @Stefan, you can “fake” a lot of things CLR doesn't support. Either by implementing them yourself (like C# closures) or by pretending they do exist, but don't include them in the generated code (like F# units of measure or Java generics).\n- @Stefan: That's what I said - \" modifications you want and which .net supports\". So if you want stuff like \"Deterministic Finalization\", you're out of luck of course. But there are tons of features that .net supports that C# doesn't.\n- @Michael: ok, sorry, didn't notice this.\n- Also interesting. I think I'll have to try this.","metadata":{"transformedAt":"2026-08-18T18:32:17.793Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":75,"estimatedTokens":987}}392{"id":"stack-10199940","source":"stackoverflow","questionId":10199940,"title":"Is there a compiler as service for c++?","tags":["c#","c++","compiler-construction","modeling"],"text":"Title: Is there a compiler as service for c++?\nTags: c#, c++, compiler-construction, modeling\nSource: Stack Overflow\n\nQuestion:\n**In short: I am looking for something like Roslyn but for C++.**\n\nIn detail:\n\nI am dealing with a c# project where I have to work with c++ files. I have a bunch of .h (about 250 files) and .cpp and I would like to map the classes and functions and what not, that are defined in these files.\n\nIt is for a modeling task. I do not wish to use the actual c++ library.\n\nI did not write the c++ code, so I cannot be sure if there are nested classes anywhere. So this is not a \"simple\" regex task.\n\nThe project is in c#, and the library I'd like to use is in c++. I cannot change that.\n\n========================================\n\nComments:\n- Even if there were no nested classes, that still would not be a \"simple\" thing to do. What about template instantiations? For what it's worth, this is what the Clang guys are trying to do, but AFAIK it's a big pain to get working on some platforms.\n- possible duplicate of How to parse/simple analyze C/C++ code from C# to get a list of methods\n- You convinced me. :) I started to work with libclang. The video was a very good start for me. Any more tutorials/walkthroughs you recommend?","metadata":{"transformedAt":"2026-08-18T18:32:17.793Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":23,"estimatedTokens":313}}393{"id":"stack-29576896","source":"stackoverflow","questionId":29576896,"title":"Synthesized vs Inherited Attributes","tags":["parsing","compiler-construction","abstract-syntax-tree","context-free-grammar","semantic-analysis"],"text":"Title: Synthesized vs Inherited Attributes\nTags: parsing, compiler-construction, abstract-syntax-tree, context-free-grammar, semantic-analysis\nSource: Stack Overflow\n\nQuestion:\nHow can I find if an attribute is synthesized or inherited from the productions of a grammar?\n\nI guess for that the attribute must be predefined in the problem -- if its value depends on child or parent nodes. But is there a way to analyse if an attribute is inherited or synthesized from grammar productions.\n\n========================================\n\nTop Answer:\nThe attribute which takes data values from its child nodes is called a synthesized attribute.\n\nThese are also called s-attributed production.\nThe attribute which takes values from parents or sibling nodes is called inherited attributes. The production rule having inherited attribute(Each inherited attributes is restricted to inherit either from a parent or left sibling only) are called L-attributed productions.\n\n========================================\n\nCode:\n```text\n**PRODUCTION**                             **SEMANTIC RULES**\n\n             T->FT’                                    T’.inh=F.val\n                                                       T.val=T’.syn\n\n           T’->*FT1’                              T1’.inh=T’.inh*F.val\n                                                      T’.syn=T1’.syn\n\n             T’->Ɛ                                    T’.syn=T’.inh\n\n             F->id                                   F.val=id.lexval\n```\n\n========================================\n\nComments:\n- i.e. An attribute t must be predefined to be S or I. Could I tell from the semantic rules associated with productions or not. Please give an example.\n- @user2047167-Yes, val is a syn. attribute as T.val is taking values from its child nodeT'.syn; you're absolutely correct!\n- What if there is an attribute that in one rule uses parent's attribute for its evaluation and in another rule it uses its children value for evaluation\n- @user2047167- You should check [L-attributed SDT] from (tutorialspoint.com/compiler_design/&hellip;). If you've further problem, comment here.\n- Is there a quick thumb rule by which I can classify an attributes. I know syn attributes have evaluate only by using the values in their production body..I guess most of the rules that involve syn attributes have the LHS nonterminal of producion on the LHS of the semantic rule and rules involving Inherited attributes have RHS non terminals of the production on the LHS of the semantic rule. I think by this I can figure very easily the nature of attributes. Am I right ?\n- @user2047167-Again ,you aren't clear to me. I request you to ask a new question with an example of your's as a up question to this. I'll help you there.\n- you answer is unclear, either add more details or change it to make it more directed towards the question.","metadata":{"transformedAt":"2026-08-18T18:32:17.793Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":44,"estimatedTokens":716}}394{"id":"stack-400895","source":"stackoverflow","questionId":400895,"title":"How does a Java compiler parse typecasts?","tags":["java","parsing","compiler-construction","casting"],"text":"Title: How does a Java compiler parse typecasts?\nTags: java, parsing, compiler-construction, casting\nSource: Stack Overflow\n\nQuestion:\nA simple expression like\n\n```\n(x) - y\n```\n\nis interpreted differently depending on whether `x` is a type name or not. If `x` is not a type name, `(x) - y` just subtracts `y` from `x`. But if `x` is a type name, `(x) - y` computes the negative of `y` and casts the resulting value to type `x`.\n\nIn a typical C or C++ compiler, the question of whether `x` is a type or not is answerable because the parser communicates such information to the lexer as soon as it processes a typedef or struct declaration. (I think that such required violation of levels was the nastiest part of the design of C.)\n\nBut in Java, `x` may not be defined until later in the source code. How does a Java compiler disambiguate such an expression? \n\nIt's clear that a Java compiler needs multiple passes, since Java doesn't require declaration-before-use. But that seems to imply that the first pass has to do a very sloppy job on parsing expressions, and then in a later pass do another, more accurate, parse of expressions. That seems wasteful.\n\nIs there a better way?\n\n========================================\n\nTop Answer:\nI just tested it out, and this code:\n\n```\nDouble y = new Double(0.1);\nSystem.out.println((Double)-y);\n```\n\ngives a compile error: \n\n `operator - cannot be applied to Double, java.lang.Double`.\n\nPutting parentheses around the -y makes it compile fine. So apparently Java solves this problem by simply not allowing it in the grammar (if that's the right terminology; I'm not an expert on compilers).\n\n========================================\n\nCode:\n```text\n(x) - y\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\n(x) - y\n```\n\n```text\ny\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\n(x) - y\n```\n\n```text\ny\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\nCastExpression:\n        ( PrimitiveType Dimsopt ) UnaryExpression\n        ( ReferenceType ) UnaryExpressionNotPlusMinus\n```\n\n```text\n+\n```\n\n```text\n-\n```\n\n```text\n'+'\n```\n\n```text\n'-'\n```\n\n```text\n')'\n```\n\n```text\nDouble y = new Double(0.1);\nSystem.out.println((Double)-y);\n```\n\n```text\noperator - cannot be applied to Double, java.lang.Double\n```\n\n========================================\n\nComments:\n- I did not downvote, but if instead of Double you had used double the compiler would have allowed it, since *operator -* can be applied to doubles. I believe that it was downvoted for that imprecission.\n- Since Java 1.5, you can use any arithmetic operator with Doubles, Floats, etc. (because of autoboxing). As I said, adding parentheses made it compile without errors.\n- +1 because I don't see any reason this answer should be voted into the negatives\n- I was too lazy to actually look up the spec (and I didn't know where to look anyway). +1","metadata":{"transformedAt":"2026-08-18T18:32:17.794Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":20,"totalLines":130,"estimatedTokens":709}}395{"id":"stack-14437895","source":"stackoverflow","questionId":14437895,"title":"compiler memory barrier and mutex","tags":["c++","multithreading","compiler-construction"],"text":"Title: compiler memory barrier and mutex\nTags: c++, multithreading, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nposix standard says that things like mutex will enforce a memory sync.\nHowever, the compiler may reorder the memory access.\nSay we have\n\n```\nlock(mutex);\nsetdata(0);\nready = 1;\nunlock(mutex);\n```\n\nIt might be changed to code below by compiler reordering, right?\n\n```\nready = 1;\nlock(mutex);\nsetdata(0);\nunlock(mutex);\n```\n\nSo how can mutex sync the memory access? To be more precise, how do compilers know that reordering should not happen across lock/unlock? \n\nactually here for single thread aspect, ready assignment reorder is totally safe since ready is not used in function call lock(mutex).\n\nEDITED:\nSo if function call is something that compiler will not get across, \ncan we regard it as a compiler memory barrier like \n\n```\nasm volatile(\"\" ::: \"memory\")\n```\n\n========================================\n\nTop Answer:\nCompilers will not reorder things where it is not clear that it is safe. In your \"what if\" example, you are not proposing a reordered memory access, you're asking what if the compiler totally changes the code ordering -- and it won't. Something the compiler might do is change the order of actual memory reads/writes but not function calls (with or without respect to those memory accesses).\n\nAn example of where the compiler might reorder memory access... lets say you have this code:\n\n```\na = *pAddressA;\nb = *pAddressB;\n```\n\nand lets consider the case where the value of `pAddressB` is in a register while `pAddressA` is not. It's fair game for the compiler to read address B first, then move the value of `pAddressA` into that same register so that the new location can be received. If there happens to be a function call between these accesses, the compiler cannot do this.\n\n========================================\n\nCode:\n```text\nlock(mutex);\nsetdata(0);\nready = 1;\nunlock(mutex);\n```\n\n```text\nready = 1;\nlock(mutex);\nsetdata(0);\nunlock(mutex);\n```\n\n```text\nasm volatile(\"\" ::: \"memory\")\n```\n\n```text\nlock\n```\n\n```text\nunlock\n```\n\n```text\npthread_self\n```\n\n```text\n__const__\n```\n\n```text\ngcc\n```\n\n```text\npthread_self()\n```\n\n```text\nlock\n```\n\n```text\nunlock\n```\n\n```text\na = *pAddressA;\nb = *pAddressB;\n```\n\n```text\npAddressB\n```\n\n```text\npAddressA\n```\n\n```text\npAddressA\n```\n\n========================================\n\nComments:\n- Do you have a sample where such a reordering happens? Since `lock()` and `unlock()` are simply function calls for the compiler, I doubt that such a reordering will happen\n- No actually. I was just wondering...\n- actually here for single thread aspect, ready assignment reorder is totally safe since ready is not used in function call lock(mutex).\n- @user1192878 the compiler cannot always be certain that the function call does not hold the address of a variable with intention of changing it, thus the compiler must not consider what you suggest to be safe (as a blanket statement). Compilers generally cannot optimize across functions (that are in different source files), which is what your suggestion would require.\n- @user1192878 We were not told whether `ready` is a global or a local variable and whether or not it's a `volatile` variable. The compiler may not know what's inside `lock()` and `unlock()`, if those are in a separate compilation unit, especially if they are implemented as assembly code in a separate file.\n- `POSIX` is an API definition. As far as the compiler goes, there's nothing to support beyond making a particular library of functions available. That is... compiler tpoics such as optimization (which memory access reordering is) are fully unrelated to POSIX.\n- @mah See this post for what Dave Butenhof has to say about POSIX vs. compilers relation.\n- wow..the posts is exactly about my question! and nice explanation!","metadata":{"transformedAt":"2026-08-18T18:32:17.794Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":15,"totalLines":132,"estimatedTokens":958}}396{"id":"stack-25607272","source":"stackoverflow","questionId":25607272,"title":"Compiling a Java Class in memory with `lombok` annotations and Java JDK 8","tags":["compiler-construction","java-8","lombok"],"text":"Title: Compiling a Java Class in memory with `lombok` annotations and Java JDK 8\nTags: compiler-construction, java-8, lombok\nSource: Stack Overflow\n\nQuestion:\nI'm trying to retrieve the description of a few Java Beans from an XML file.\nI'd like to annotate them with `@Data` from project `lombok` to automatically include constructor, equals, hashCode, getters, setters and toString.\nI'd like to compile them in memory, generate a few instances (with data from the same XML file) and add them to Drools to eventually do some reasoning on that data.\n\nUnfortunately, I cannot compile those classes and so I am asking for your help!\n\nThe following code shows how to programmatically compile Java classes in memory:\n\n```\npackage example;\n\nimport java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.List;\n\nimport javax.tools.JavaCompiler;\nimport javax.tools.JavaFileManager;\nimport javax.tools.JavaFileObject;\nimport javax.tools.ToolProvider;\n\npublic class Simple {\n\n public static void main(String[] args) throws Exception {\n String name = \"Person\";\n String content = //\n \"public class \" + name + \" {\\n\" + //\n \" @Override\\n\" + //\n \" public String toString() {\\n\" + //\n \" return \\\"Hello, world!\\\";\\n\" + //\n \" }\\n\" + //\n \"}\\n\";\n System.out.println(content);\n\n JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();\n JavaFileManager manager = new MemoryFileManager(compiler.getStandardFileManager(null, null, null));\n\n List options = new ArrayList();\n options.addAll(Arrays.asList(\"-classpath\", System.getProperty(\"java.class.path\")));\n\n List files = new ArrayList();\n files.add(new MemoryJavaFileObject(name, content));\n\n compiler.getTask(null, manager, null, options, null, files).call();\n\n Object instance = manager.getClassLoader(null).loadClass(name).newInstance();\n System.out.println(instance);\n }\n\n}\n```\n\nwhere `MemoryFileManager` is:\n\n```\npackage example;\n\nimport java.io.IOException;\nimport java.security.SecureClassLoader;\n\nimport javax.tools.FileObject;\nimport javax.tools.ForwardingJavaFileManager;\nimport javax.tools.JavaFileObject;\nimport javax.tools.JavaFileObject.Kind;\nimport javax.tools.StandardJavaFileManager;\n\npublic class MemoryFileManager extends ForwardingJavaFileManager {\n\n private MemoryJavaClassObject object;\n\n public MemoryFileManager(StandardJavaFileManager manager) {\n super(manager);\n }\n\n @Override\n public ClassLoader getClassLoader(Location location) {\n return new SecureClassLoader() {\n @Override\n protected Class findClass(String name) throws ClassNotFoundException {\n byte[] b = object.getBytes();\n return super.defineClass(name, object.getBytes(), 0, b.length);\n }\n };\n }\n\n @Override\n public JavaFileObject getJavaFileForOutput(Location location, String name, Kind kind, FileObject sibling) throws IOException {\n object = new MemoryJavaClassObject(name, kind);\n return object;\n }\n\n}\n```\n\nand `MemoryJavaClassObject` is: \n\n```\npackage example;\n\nimport java.io.ByteArrayOutputStream;\nimport java.io.IOException;\nimport java.io.OutputStream;\nimport java.net.URI;\n\nimport javax.tools.SimpleJavaFileObject;\n\npublic class MemoryJavaClassObject extends SimpleJavaFileObject {\n\n protected final ByteArrayOutputStream stream = new ByteArrayOutputStream();\n\n public MemoryJavaClassObject(String name, Kind kind) {\n super(URI.create(\"string:///\" + name.replace('.', '/') + kind.extension), kind);\n }\n\n public byte[] getBytes() {\n return stream.toByteArray();\n }\n\n @Override\n public OutputStream openOutputStream() throws IOException {\n return stream;\n }\n\n}\n```\n\nand finally `MemoryJavaFileObject` is:\n\n```\npackage example;\n\nimport java.net.URI;\n\nimport javax.tools.SimpleJavaFileObject;\n\npublic class MemoryJavaFileObject extends SimpleJavaFileObject {\n\n private CharSequence content;\n\n protected MemoryJavaFileObject(String className, CharSequence content) {\n super(URI.create(\"string:///\" + className.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);\n this.content = content;\n }\n\n @Override\n public CharSequence getCharContent(boolean ignoreEncodingErrors) {\n return content;\n }\n\n}\n```\n\nIf I run the example in the first code block, I get the following output, as expected:\n\n```\npublic class Person {\n @Override\n public String toString() {\n return \"Hello, world!\";\n }\n}\n\nHello, world!\n```\n\nNow, if I add the `lombok.jar` into my project and I include the following example:\n\n```\npackage example;\n\nimport java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.List;\n\nimport javax.tools.JavaCompiler;\nimport javax.tools.JavaFileManager;\nimport javax.tools.JavaFileObject;\nimport javax.tools.ToolProvider;\n\npublic class Lombok {\n\n public static void main(String[] args) throws Exception {\n String name = \"Person\";\n String content = //\n \"import lombok.Data;\\n\" + //\n \"public @Data class \" + name + \" {\\n\" + //\n \" private String name;\\n\" + //\n \"}\\n\";\n System.out.println(content);\n\n JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();\n JavaFileManager manager = new MemoryFileManager(compiler.getStandardFileManager(null, null, null));\n\n List options = new ArrayList();\n options.addAll(Arrays.asList(\"-classpath\", System.getProperty(\"java.class.path\")));\n\n List files = new ArrayList();\n files.add(new MemoryJavaFileObject(name, content));\n\n compiler.getTask(null, manager, null, options, null, files).call();\n\n Object instance = manager.getClassLoader(null).loadClass(name).newInstance();\n System.out.println(instance);\n }\n\n}\n```\n\nunfortunately I don't get the expected output but rather:\n\n```\nimport lombok.Data;\npublic @Data class Person {\n private String name;\n}\n\n/Person.java:2: warning: Can't initialize javac processor due to (most likely) a class loader problem: java.lang.NoClassDefFoundError: com/sun/tools/javac/processing/JavacProcessingEnvironment\npublic @Data class Person {\n ^\n at lombok.javac.apt.Processor.init(Processor.java:84)\n at lombok.core.AnnotationProcessor$JavacDescriptor.want(AnnotationProcessor.java:87)\n at lombok.core.AnnotationProcessor.init(AnnotationProcessor.java:141)\n at com.sun.tools.javac.processing.JavacProcessingEnvironment$ProcessorState.(JavacProcessingEnvironment.java:500)\n at com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors$ProcessorStateIterator.next(JavacProcessingEnvironment.java:597)\n at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:690)\n at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)\n at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)\n at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)\n at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1173)\n at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:859)\n at com.sun.tools.javac.main.Main.compile(Main.java:523)\n at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)\n at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)\n at example.Lombok.main(Lombok.java:42)\n Caused by: java.lang.ClassNotFoundException: com.sun.tools.javac.processing.JavacProcessingEnvironment\n at java.net.URLClassLoader$1.run(URLClassLoader.java:372)\n at java.net.URLClassLoader$1.run(URLClassLoader.java:361)\n at java.security.AccessController.doPrivileged(Native Method)\n at java.net.URLClassLoader.findClass(URLClassLoader.java:360)\n at java.lang.ClassLoader.loadClass(ClassLoader.java:424)\n at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)\n at java.lang.ClassLoader.loadClass(ClassLoader.java:357)\n ... 15 more\n1 warning\nPerson@39aeed2f\n```\n\nNotice that the class gets compiled and the default `toString()` method is executed since the typical output is displayed.\nAlso notice that if I run the former example, now I get the following: \n\n```\npublic class Person {\n @Override\n public String toString() {\n return \"Hello, world!\";\n }\n}\n\n/Person.java:1: warning: Can't initialize javac processor due to (most likely) a class loader problem: java.lang.NoClassDefFoundError: com/sun/tools/javac/processing/JavacProcessingEnvironment\npublic class Person {\n ^\n at lombok.javac.apt.Processor.init(Processor.java:84)\n at lombok.core.AnnotationProcessor$JavacDescriptor.want(AnnotationProcessor.java:87)\n at lombok.core.AnnotationProcessor.init(AnnotationProcessor.java:141)\n at com.sun.tools.javac.processing.JavacProcessingEnvironment$ProcessorState.(JavacProcessingEnvironment.java:500)\n at com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors$ProcessorStateIterator.next(JavacProcessingEnvironment.java:597)\n at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:690)\n at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)\n at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)\n at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)\n at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1173)\n at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:859)\n at com.sun.tools.javac.main.Main.compile(Main.java:523)\n at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)\n at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)\n at example.Simple.main(Simple.java:44)\n Caused by: java.lang.ClassNotFoundException: com.sun.tools.javac.processing.JavacProcessingEnvironment\n at java.net.URLClassLoader$1.run(URLClassLoader.java:372)\n at java.net.URLClassLoader$1.run(URLClassLoader.java:361)\n at java.security.AccessController.doPrivileged(Native Method)\n at java.net.URLClassLoader.findClass(URLClassLoader.java:360)\n at java.lang.ClassLoader.loadClass(ClassLoader.java:424)\n at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)\n at java.lang.ClassLoader.loadClass(ClassLoader.java:357)\n ... 15 more\n1 warning\nHello, world!\n```\n\nApparently, by looking at the warning message passed by the exception, `lombok` doesn't hook the given compiler properly. Unfortunately I was not able to find any useful bit of information. I can only think it could be `lombok` not dealing properly with Java JDK 8. Am I right? \n\nDo you know any other way to work around this problem?\n\n========================================\n\nCode:\n```text\npackage example;\n\nimport java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.List;\n\nimport javax.tools.JavaCompiler;\nimport javax.tools.JavaFileManager;\nimport javax.tools.JavaFileObject;\nimport javax.tools.ToolProvider;\n\npublic class Simple {\n\n    public static void main(String[] args) throws Exception {\n        String name = \"Person\";\n        String content = //\n            \"public class \" + name + \" {\\n\" + //\n            \"    @Override\\n\" + //\n            \"    public String toString() {\\n\" + //\n            \"        return \\\"Hello, world!\\\";\\n\" + //\n            \"    }\\n\" + //\n            \"}\\n\";\n        System.out.println(content);\n\n        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();\n        JavaFileManager manager = new MemoryFileManager(compiler.getStandardFileManager(null, null, null));\n\n        List<String> options = new ArrayList<String>();\n        options.addAll(Arrays.asList(\"-classpath\", System.getProperty(\"java.class.path\")));\n\n        List<JavaFileObject> files = new ArrayList<JavaFileObject>();\n        files.add(new MemoryJavaFileObject(name, content));\n\n        compiler.getTask(null, manager, null, options, null, files).call();\n\n        Object instance = manager.getClassLoader(null).loadClass(name).newInstance();\n        System.out.println(instance);\n    }\n\n}\n```\n\n```text\npackage example;\n\nimport java.io.IOException;\nimport java.security.SecureClassLoader;\n\nimport javax.tools.FileObject;\nimport javax.tools.ForwardingJavaFileManager;\nimport javax.tools.JavaFileObject;\nimport javax.tools.JavaFileObject.Kind;\nimport javax.tools.StandardJavaFileManager;\n\npublic class MemoryFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> {\n\n    private MemoryJavaClassObject object;\n\n    public MemoryFileManager(StandardJavaFileManager manager) {\n        super(manager);\n    }\n\n    @Override\n    public ClassLoader getClassLoader(Location location) {\n        return new SecureClassLoader() {\n            @Override\n            protected Class<?> findClass(String name) throws ClassNotFoundException {\n                byte[] b = object.getBytes();\n                return super.defineClass(name, object.getBytes(), 0, b.length);\n            }\n        };\n    }\n\n    @Override\n    public JavaFileObject getJavaFileForOutput(Location location, String name, Kind kind, FileObject sibling) throws IOException {\n        object = new MemoryJavaClassObject(name, kind);\n        return object;\n    }\n\n}\n```\n\n```text\npackage example;\n\nimport java.io.ByteArrayOutputStream;\nimport java.io.IOException;\nimport java.io.OutputStream;\nimport java.net.URI;\n\nimport javax.tools.SimpleJavaFileObject;\n\npublic class MemoryJavaClassObject extends SimpleJavaFileObject {\n\n    protected final ByteArrayOutputStream stream = new ByteArrayOutputStream();\n\n    public MemoryJavaClassObject(String name, Kind kind) {\n        super(URI.create(\"string:///\" + name.replace('.', '/') + kind.extension), kind);\n    }\n\n    public byte[] getBytes() {\n        return stream.toByteArray();\n    }\n\n    @Override\n    public OutputStream openOutputStream() throws IOException {\n        return stream;\n    }\n\n}\n```\n\n```text\npackage example;\n\nimport java.net.URI;\n\nimport javax.tools.SimpleJavaFileObject;\n\npublic class MemoryJavaFileObject extends SimpleJavaFileObject {\n\n    private CharSequence content;\n\n    protected MemoryJavaFileObject(String className, CharSequence content) {\n        super(URI.create(\"string:///\" + className.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);\n        this.content = content;\n    }\n\n    @Override\n    public CharSequence getCharContent(boolean ignoreEncodingErrors) {\n        return content;\n    }\n\n}\n```\n\n```text\npublic class Person {\n    @Override\n    public String toString() {\n        return \"Hello, world!\";\n    }\n}\n\nHello, world!\n```\n\n```text\npackage example;\n\nimport java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.List;\n\nimport javax.tools.JavaCompiler;\nimport javax.tools.JavaFileManager;\nimport javax.tools.JavaFileObject;\nimport javax.tools.ToolProvider;\n\npublic class Lombok {\n\n    public static void main(String[] args) throws Exception {\n        String name = \"Person\";\n        String content = //\n            \"import lombok.Data;\\n\" + //\n            \"public @Data class \" + name + \" {\\n\" + //\n            \"    private String name;\\n\" + //\n            \"}\\n\";\n        System.out.println(content);\n\n        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();\n        JavaFileManager manager = new MemoryFileManager(compiler.getStandardFileManager(null, null, null));\n\n        List<String> options = new ArrayList<String>();\n        options.addAll(Arrays.asList(\"-classpath\", System.getProperty(\"java.class.path\")));\n\n        List<JavaFileObject> files = new ArrayList<JavaFileObject>();\n        files.add(new MemoryJavaFileObject(name, content));\n\n        compiler.getTask(null, manager, null, options, null, files).call();\n\n        Object instance = manager.getClassLoader(null).loadClass(name).newInstance();\n        System.out.println(instance);\n    }\n\n}\n```\n\n```text\nimport lombok.Data;\npublic @Data class Person {\n    private String name;\n}\n\n/Person.java:2: warning: Can't initialize javac processor due to (most likely) a class loader     problem: java.lang.NoClassDefFoundError: com/sun/tools/javac/processing/JavacProcessingEnvironment\npublic @Data class Person {\n             ^\n      at lombok.javac.apt.Processor.init(Processor.java:84)\n      at lombok.core.AnnotationProcessor$JavacDescriptor.want(AnnotationProcessor.java:87)\n      at lombok.core.AnnotationProcessor.init(AnnotationProcessor.java:141)\n      at com.sun.tools.javac.processing.JavacProcessingEnvironment$ProcessorState.<init>(JavacProcessingEnvironment.java:500)\n      at com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors$ProcessorStateIterator.next(JavacProcessingEnvironment.java:597)\n      at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:690)\n      at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)\n      at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)\n      at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)\n      at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1173)\n      at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:859)\n      at com.sun.tools.javac.main.Main.compile(Main.java:523)\n      at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)\n      at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)\n      at example.Lombok.main(Lombok.java:42)\n  Caused by: java.lang.ClassNotFoundException: com.sun.tools.javac.processing.JavacProcessingEnvironment\n      at java.net.URLClassLoader$1.run(URLClassLoader.java:372)\n      at java.net.URLClassLoader$1.run(URLClassLoader.java:361)\n      at java.security.AccessController.doPrivileged(Native Method)\n      at java.net.URLClassLoader.findClass(URLClassLoader.java:360)\n      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)\n      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)\n      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)\n      ... 15 more\n1 warning\nPerson@39aeed2f\n```\n\n```text\npublic class Person {\n    @Override\n    public String toString() {\n        return \"Hello, world!\";\n    }\n}\n\n/Person.java:1: warning: Can't initialize javac processor due to (most likely) a class loader problem: java.lang.NoClassDefFoundError: com/sun/tools/javac/processing/JavacProcessingEnvironment\npublic class Person {\n       ^\n      at lombok.javac.apt.Processor.init(Processor.java:84)\n      at lombok.core.AnnotationProcessor$JavacDescriptor.want(AnnotationProcessor.java:87)\n      at lombok.core.AnnotationProcessor.init(AnnotationProcessor.java:141)\n      at com.sun.tools.javac.processing.JavacProcessingEnvironment$ProcessorState.<init>(JavacProcessingEnvironment.java:500)\n      at com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors$ProcessorStateIterator.next(JavacProcessingEnvironment.java:597)\n      at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:690)\n      at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)\n      at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)\n      at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)\n      at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1173)\n      at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:859)\n      at com.sun.tools.javac.main.Main.compile(Main.java:523)\n      at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)\n      at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)\n      at example.Simple.main(Simple.java:44)\n  Caused by: java.lang.ClassNotFoundException: com.sun.tools.javac.processing.JavacProcessingEnvironment\n      at java.net.URLClassLoader$1.run(URLClassLoader.java:372)\n      at java.net.URLClassLoader$1.run(URLClassLoader.java:361)\n      at java.security.AccessController.doPrivileged(Native Method)\n      at java.net.URLClassLoader.findClass(URLClassLoader.java:360)\n      at java.lang.ClassLoader.loadClass(ClassLoader.java:424)\n      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)\n      at java.lang.ClassLoader.loadClass(ClassLoader.java:357)\n      ... 15 more\n1 warning\nHello, world!\n```\n\n```text\n@Data\n```\n\n```text\nlombok\n```\n\n```text\nMemoryFileManager\n```\n\n```text\nMemoryJavaClassObject\n```\n\n```text\nMemoryJavaFileObject\n```\n\n```text\nlombok.jar\n```\n\n```text\ntoString()\n```\n\n```text\nlombok\n```\n\n```text\nlombok\n```\n\n```text\ndependencies {\n    compile files(\"${System.properties['java.home']}/../lib/tools.jar\")\n    compile 'org.projectlombok:lombok:1.14.4'\n    testCompile 'junit:junit:4.11'\n}\n```\n\n```text\ntools.jar\n```\n\n```text\ntools.jar\n```\n\n```text\n$JAVA_HOME/lib/tools.jar\n```\n\n```text\n$JAVA_HOME/Classes/classes.jar\n```\n\n```text\ntools.jar\n```\n\n```text\nclasses.jar\n```\n\n========================================\n\nComments:\n- Are you sure you have a **JDK** rather than a JRE and the `tools.jar` is in the class path?\n- Hi @Holger and thanks for your reply! Yes, I have Java SDK 8 but I'm on a Mac! And apparently `tools.jar` is missing on Mac (see stackoverflow.com/questions/5616318/&hellip;). Thanks for pointing me towards the right direction!\n- This is weird... I just checked and I have `tools.jar` in `$JAVA_HOME&#47;lib&#47;tools.jar`. However, it is not present within Eclipse... It's actually Eclipse's fault: the reference Java Environment is configured as a JRE. Including `tools.jar` makes everything work. Thanks again!\n- Maybe you have to add it to the Eclipse project classpath manually…\n- Just for reference, in the end I told `gradle` to add it to the project as a dependency: dependencies { compile files(\"${System.properties['java.home']}/../lib/tools.jar\") compile 'org.projectlombok:lombok:1.14.4' testCompile 'junit:junit:4.11' }\n- You can (and should) put the solution into an *answer* and accept that answer so that others can easily see that there’s a solution from the question overview.\n- You're right! I couldn't find the `Answer` button... now I have! Thanks again!\n- Just in case if something changed within these 3 years: have there been developed a cleaner method to resolve this issue? I have a similar problem on IntelliJ right now, have posted a question referencing yours.\n- No idea, I've moved to Python in a meanwhile... nice language, by the way!\n- In my dreams I am C#ing at work again. Never had such problems there.\n- By the way, common patches like adding a dependency didn't work for me.\n- What about when we move to java 11 ? we don't have tools.jar there.\n- That was my case, somehow installer did put c:\\Program Files\\Common Files\\Oracle\\Java\\javapath\\java.exe to PATH. Changing to real java path fixed the problem.","metadata":{"transformedAt":"2026-08-18T18:32:17.794Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":24,"totalLines":649,"estimatedTokens":5679}}397{"id":"stack-2933434","source":"stackoverflow","questionId":2933434,"title":"Strengths and weaknesses of JIT compilers for Python","tags":["python","compiler-construction","project"],"text":"Title: Strengths and weaknesses of JIT compilers for Python\nTags: python, compiler-construction, project\nSource: Stack Overflow\n\nQuestion:\nI'm currently aware of the following Python JIT compilers: Psyco, PyPy and Unladen Swallow.\n\nBasically, I'd like to ask for your personal experiences on the strengths and weaknesses of these compilers - and if there are any others worth looking into.\n\nThanks in advance,\n\nAz\n\n========================================\n\nTop Answer:\nSome other tools you might investigate to speed up python are \n\n- Cython, which requires type specification of all variables in the relevant method and then statically compiles the method\n\n- Numba, which requires LLVM but is JIT (methods must be decorated with argument types for compilation to occur).\n\n========================================\n\nCode:\n```text\nPyPy\n```\n\n========================================\n\nComments:\n- This is a pretty broad question. Have you experimented with each of those projects? If not, I would recommend trying that. If so, then do you have any specific questions? We can't decide which is the best *for you*.\n- I would love to, but I'm on a deadline and my supervisor isn't budging on an extension. So, if you can recommend a good starting point I'd really appreciate that. Or, even the easiest to start with would be good. I can probably update once I get more information then.\n- I'm not sure that *any* of those projects are suitable for production use at this time. Have you considered py2exe?\n- @GregHewgill: AFAIK py2exe does not do a JIT/AOT compilation. It just bundles vm and code in the same file, so the perf depends on the underlying python implementation\n- wow, good update! interesting to see JIT performance improves it over CPython, I will start testing it too :)\n- there're some negative results in comments. Did you try PyPy on your own project? Can you compare it to CPython performance?\n- PyPy is nice, I just wish they supported Py3K, not forcing a choice between fast Python 2.x and slow Python 3.x\n- jnnnnn when you are compiling in numba, do you have to manually configure the 'ast' llvm before hand? I've been trying to get it to work, but i keep ending in the same spot (Key Error ) So i figured it was something to do with the LLVM itself.","metadata":{"transformedAt":"2026-08-18T18:32:17.794Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":40,"estimatedTokens":567}}398{"id":"stack-7025286","source":"stackoverflow","questionId":7025286,"title":"Why does this V8/Javascript code perform so badly?","tags":["javascript","performance","compiler-construction","node.js","v8"],"text":"Title: Why does this V8/Javascript code perform so badly?\nTags: javascript, performance, compiler-construction, node.js, v8\nSource: Stack Overflow\n\nQuestion:\nI've been looking at some interesting programming benchmarks to see how well node.js might perform compared to other languages: http://benchmarksgame.alioth.debian.org/u32/compare.php?lang=node&lang2=php\n\nWhile the results deal primarily with algorithmic problems that you would normally prefer to solve with a variant of C or Fortran, one test stands out as incredibly bad for V8: \n\n**pidigits** - 52x slower than PHP \n\nSince v8 performs better across the board than PHP in all the other tests I presume there is either something wrong with the code or some specific to the implementation of V8/Javascript that makes it perform so badly. What is it?\n\n**Code 1: V8** \n\n```\n// The Computer Language Benchmarks Game\n// http://shootout.alioth.debian.org\n//\n// Contributed by Matthew Wilson \n// biginteger derived from Tom Wu's jsbn.js\n\nvar compareTo, multiply, divide, addTo, add, intValue, shiftLeft, nbv;\n\nfunction main($n) {\n var $i=1, $s=\"\", $d, neg10=nbv(-10), three=nbv(3), ten=nbv(10), g = 1, $g,\n digits=Array(10), $z0=nbv(1), $z1=nbv(0), $z2=nbv(1), negdigits=Array(10),\n k = 0, $k, l = 2, $l, a;\n\n for(var i=0; i 0\n || ($d = intValue(divide(add(multiply($z0,three),$z1),$z2))) != \n intValue(divide(add(shiftLeft($z0,2),$z1),$z2))\n ) {\n $z1 = multiply($z1,$g = nbv(g+=2));\n $z2 = multiply($z2,$g);\n addTo($z1, multiply($z0,$l = nbv(l+=4)), $z1);\n $z0 = multiply($z0,$k = nbv(++k));\n }\n $z0 = multiply($z0,ten);\n $z1 = multiply($z1,ten);\n addTo($z1, multiply($z2,negdigits[$d]), $z1);\n $s += $d;\n\n if ($i % 10 == 0) { print($s+\"\\t:\"+$i); $s=\"\" }\n } while (++$i 0) { print($s+\"\\t:\"+$n) }\n}\n\nvar functions;\nload('/home/dunham/shootout/bench/Include/javascript/biginteger.js');\n\ncompareTo=functions[0];\nmultiply=functions[1];\ndivide=functions[2];\naddTo=functions[3];\nadd=functions[4];\nnbv=functions[5];\nshiftLeft=functions[6];\nintValue=functions[7];\n\nmain.call(this, 1*arguments[0]*1)\n```\n\n**Code 2: PHP**\n\n```\nq = $q;\n $this->r = $r; \n $this->s = $s;\n $this->t = $t; \n }\n\n function Unity(){\n return new Transformation(\"1\", \"0\", \"0\", \"1\"); \n } \n\n function Zero(){\n return new Transformation(\"0\", \"0\", \"0\", \"0\"); \n } \n\n function Compose($a){\n $qq = bcmul($this->q, $a->q);\n $qrrt = bcadd(bcmul($this->q, $a->r), bcmul($this->r, $a->t));\n $sqts = bcadd(bcmul($this->s, $a->q), bcmul($this->t, $a->s));\n $srtt = bcadd(bcmul($this->s, $a->r), bcmul($this->t, $a->t)); \n return new Transformation($qq, $qrrt, $sqts, $srtt);\n }\n\n function Extract($j){\n $bigj = strval($j);\n $qjr = bcadd(bcmul($this->q, $bigj), $this->r);\n $sjt = bcadd(bcmul($this->s, $bigj), $this->t);\n $d = bcdiv($qjr, $sjt);\n return floor($d);\n }\n\n function Next(){ \n $this->k = $this->k + 1;\n $this->q = strval($this->k);\n $this->r = strval(4*$this->k + 2);\n $this->s = \"0\";\n $this->t = strval(2*$this->k + 1);\n return $this; \n } \n}\n\nclass PiDigitStream {\n var $z, $x, $inverse;\n\n function PiDigitStream(){\n $this->z = Transformation::Unity();\n $this->x = Transformation::Zero(); \n $this->inverse = Transformation::Zero(); \n }\n\n function Produce($j){\n $i = $this->inverse;\n $i->q = \"10\";\n $i->r = strval(-10*$j);\n $i->s = \"0\";\n $i->t = \"1\";\n return $i->Compose($this->z);\n } \n\n function Consume($a){\n return $this->z ->Compose($a); \n }\n\n function Digit(){\n return $this->z ->Extract(3); \n } \n\n function IsSafe($j){\n return $j == ($this->z ->Extract(4)); \n } \n\n function Next(){\n $y = $this->Digit();\n if ($this->IsSafe($y)){\n $this->z = $this->Produce($y);\n return $y;\n } else {\n $this->z = $this->Consume($this->x ->Next());\n return $this->Next(); \n }\n } \n}\n\n$n = $argv[1];\n$i = 0;\n$length = 10;\n$pidigit = new PiDigitStream;\n\nwhile ($n > 0){\n if ($n Next());\n for ($j=$n; $jNext());\n $i += $length; \n }\n print \"\\t:$i\\n\";\n $n -= $length;\n}\n?>\n```\n\n========================================\n\nTop Answer:\nOut of curiosity, I wrote an alternate version using node-bigint (which wraps libgmp). I compared the fastest C implementation to my version of the benchmark. Performance is as you'd expect, around that of the other language implementations using libgmp.\n\n### Results\n\n*C (compiled with gcc -pipe -Wall -O3 -fomit-frame-pointer pidigits.c -o pidigits -lgmp)*\n\n```\n./pidigits-c 10000 1.11s user 0.00s system 99% cpu 1.116 total\n```\n\n*node (0.6.18)*\n\n```\nnode pidigits-gmp.js 10000 3.61s user 3.15s system 100% cpu 6.712 total\n```\n\n### Source\n\n```\nvar bigint = require('bigint');\n\nfunction calculatePi(N) {\n var i = 0,\n k = 0,\n k1 = 1,\n ns = 0,\n\n a = bigint(0),\n d = bigint(1),\n m = bigint(0),\n n = bigint(1),\n t = bigint(0),\n u = bigint(0);\n\n while (1) {\n k += 1;\n k1 += 2;\n t = n.shiftLeft(1);\n n = n.mul(k);\n a = a.add(t).mul(k1);\n d = d.mul(k1);\n\n if (a.cmp(n) >= 0) {\n m = n.mul(3).add(a);\n t = m.div(d);\n u = m.mod(d).add(n);\n\n if (d.cmp(u) > 0) {\n ns = ns * 10 + t.toNumber();\n i += 1;\n\n if (i % 10 === 0) {\n console.log(ns + '\\t:' + i);\n ns = 0;\n }\n\n if (i >= N) break;\n\n a = a.sub(d.mul(t)).mul(10);\n n = n.mul(10);\n }\n }\n }\n}\n\ncalculatePi(process.argv[2] || 10);\n```\n\n========================================\n\nCode:\n```text\n// The Computer Language Benchmarks Game\n//  http://shootout.alioth.debian.org\n//\n//  Contributed by Matthew Wilson \n//  biginteger derived from Tom Wu's jsbn.js\n\n\nvar compareTo, multiply, divide, addTo, add, intValue, shiftLeft, nbv;\n\nfunction main($n) {\n  var $i=1, $s=\"\", $d, neg10=nbv(-10), three=nbv(3), ten=nbv(10), g = 1, $g,\n  digits=Array(10), $z0=nbv(1), $z1=nbv(0), $z2=nbv(1), negdigits=Array(10),\n  k = 0, $k, l = 2, $l, a;\n\n  for(var i=0; i<10; ++i) { negdigits[i] = multiply(digits[i] = nbv(i),neg10) }\n\n  do {\n    while ( compareTo($z0,$z2) > 0\n         || ($d = intValue(divide(add(multiply($z0,three),$z1),$z2))) != \n             intValue(divide(add(shiftLeft($z0,2),$z1),$z2))\n    ) {\n      $z1 = multiply($z1,$g = nbv(g+=2));\n      $z2 = multiply($z2,$g);\n      addTo($z1, multiply($z0,$l = nbv(l+=4)), $z1);\n      $z0 = multiply($z0,$k = nbv(++k));\n    }\n    $z0 = multiply($z0,ten);\n    $z1 = multiply($z1,ten);\n    addTo($z1, multiply($z2,negdigits[$d]), $z1);\n    $s += $d;\n\n    if ($i % 10 == 0) { print($s+\"\\t:\"+$i); $s=\"\" }\n  } while (++$i <= $n)\n\n  if (($i = $n % 10) != 0) { $s += Array(11-$i).join(' ') }\n  if ($s.length > 0) { print($s+\"\\t:\"+$n) }\n}\n\nvar functions;\nload('/home/dunham/shootout/bench/Include/javascript/biginteger.js');\n\ncompareTo=functions[0];\nmultiply=functions[1];\ndivide=functions[2];\naddTo=functions[3];\nadd=functions[4];\nnbv=functions[5];\nshiftLeft=functions[6];\nintValue=functions[7];\n\nmain.call(this, 1*arguments[0]*1)\n```\n\n```text\n<?php /* The Great Computer Language Shootout \n   http://shootout.alioth.debian.org/\n   contributed by Isaac Gouy \n   php -q pidigits.php 27\n*/\n\nclass Transformation {\n   var $q, $r, $s, $t, $k;\n\n   function Transformation($q, $r, $s, $t){\n      $this->q = $q;\n      $this->r = $r;      \n      $this->s = $s;\n      $this->t = $t;               \n   }\n\n   function Unity(){\n      return new Transformation(\"1\", \"0\", \"0\", \"1\");              \n   }   \n\n   function Zero(){\n      return new Transformation(\"0\", \"0\", \"0\", \"0\");              \n   }      \n\n   function Compose($a){\n      $qq = bcmul($this->q, $a->q);\n      $qrrt = bcadd(bcmul($this->q, $a->r), bcmul($this->r, $a->t));\n      $sqts = bcadd(bcmul($this->s, $a->q), bcmul($this->t, $a->s));\n      $srtt = bcadd(bcmul($this->s, $a->r), bcmul($this->t, $a->t));   \n      return new Transformation($qq, $qrrt, $sqts, $srtt);\n   }\n\n   function Extract($j){\n      $bigj = strval($j);\n      $qjr = bcadd(bcmul($this->q, $bigj), $this->r);\n      $sjt = bcadd(bcmul($this->s, $bigj), $this->t);\n      $d = bcdiv($qjr, $sjt);\n      return floor($d);\n   }\n\n   function Next(){ \n      $this->k = $this->k + 1;\n      $this->q = strval($this->k);\n      $this->r = strval(4*$this->k + 2);\n      $this->s = \"0\";\n      $this->t = strval(2*$this->k + 1);\n      return $this;      \n   }                \n}\n\nclass PiDigitStream {\n   var $z, $x, $inverse;\n\n   function PiDigitStream(){\n      $this->z = Transformation::Unity();\n      $this->x = Transformation::Zero();      \n      $this->inverse = Transformation::Zero();   \n   }\n\n   function Produce($j){\n      $i = $this->inverse;\n      $i->q = \"10\";\n      $i->r = strval(-10*$j);\n      $i->s = \"0\";\n      $i->t = \"1\";\n      return $i->Compose($this->z);\n   }   \n\n   function Consume($a){\n      return $this->z ->Compose($a);  \n   }\n\n   function Digit(){\n      return $this->z ->Extract(3);  \n   }  \n\n   function IsSafe($j){\n      return $j == ($this->z ->Extract(4));  \n   }    \n\n   function Next(){\n      $y = $this->Digit();\n      if ($this->IsSafe($y)){\n         $this->z = $this->Produce($y);\n         return $y;\n      } else {\n         $this->z = $this->Consume($this->x ->Next());\n         return $this->Next();      \n      }\n   } \n}\n\n\n$n = $argv[1];\n$i = 0;\n$length = 10;\n$pidigit = new PiDigitStream;\n\nwhile ($n > 0){\n   if ($n < $length){\n      for ($j=0; $j<$n; $j++) printf(\"%d\",$pidigit->Next());\n      for ($j=$n; $j<$length; $j++)  print \" \";\n      $i += $n;\n   } else {\n      for ($j=0; $j<$length; $j++) printf(\"%d\",$pidigit->Next());\n      $i += $length;   \n   }\n   print \"\\t:$i\\n\";\n   $n -= $length;\n}\n?>\n```\n\n```text\ndc\n```\n\n```text\nbc\n```\n\n```text\n./pidigits-c 10000  1.11s user 0.00s system 99% cpu 1.116 total\n```\n\n```text\nnode pidigits-gmp.js 10000  3.61s user 3.15s system 100% cpu 6.712 total\n```\n\n```text\nvar bigint = require('bigint');\n\nfunction calculatePi(N) {\n  var i = 0,\n      k = 0,\n      k1 = 1,\n      ns = 0,\n\n      a = bigint(0),\n      d = bigint(1),\n      m = bigint(0),\n      n = bigint(1),\n      t = bigint(0),\n      u = bigint(0);\n\n  while (1) {\n    k += 1;\n    k1 += 2;\n    t = n.shiftLeft(1);\n    n = n.mul(k);\n    a = a.add(t).mul(k1);\n    d = d.mul(k1);\n\n    if (a.cmp(n) >= 0) {\n      m = n.mul(3).add(a);\n      t = m.div(d);\n      u = m.mod(d).add(n);\n\n      if (d.cmp(u) > 0) {\n        ns = ns * 10 + t.toNumber();\n        i += 1;\n\n        if (i % 10 === 0) {\n          console.log(ns + '\\t:' + i);\n          ns = 0;\n        }\n\n        if (i >= N) break;\n\n        a = a.sub(d.mul(t)).mul(10);\n        n = n.mul(10);\n      }\n    }\n  }\n}\n\ncalculatePi(process.argv[2] || 10);\n```\n\n========================================\n\nComments:\n- I'd guess the javascript code is passing the actual (bigint?) values being computed, and the PHP version is only passing references.\n- PHP uses BC Math extension which implements arbitrary precision arithmetic in C or C++ while JavaScript code relies on some biginteger.js which I suppose implements arbitrary precision arithmetic in pure JavaScript. Unfortunately I could not find biginteger.js anywhere, so I wrote to Isaac asking for source.\n- See the updated JavaScript program: benchmarksgame.alioth.debian.org/u64q/&hellip;\n- Thanks Matthew. Someone has also written gmp bindings for node.js. While it would be interesting to see how optimized versions of PHP and node.js perform, I think this highlights the futility of judging languages by performance benchmarks - it's not impossible to glue different languages together thereby benefiting from the best of both worlds.\n- The PHP program that's 52x faster **already does** use GMP. shootout.alioth.debian.org/u32/&hellip;\n- @jond3k - What it highlights is that performance measurements measure the performance of **programs**.\n- @jond3k I decided to see how well the gmp bindings for node would perform, so I rewrote the javascript version using node-bigint. It does as well as you'd expect (see my answer for details).\n- @igouy Feel free to use my benchmark. Assuming you have Node.js and GMP building the wrapper is as simple as `npm install bigint`.\n- The benchmarks game actually uses Node.js now, so it would be great if you worked through the bureaucracy and contributed your program -- benchmarksgame.alioth.debian.org/play.html\n- Sadly lots of errors -- make: Entering directory '/home/dunham/node_modules/bigint/build' CXX(target) Release/obj.target/bigint/bigint.o ../bigint.cc:57:27: error: expected class-name before ‘{’ token class BigInt : ObjectWrap { ^ ../bigint.cc:74:34: error: ‘Arguments’ does not name a type static Handle New(const Arguments& args);\n- See the updated JavaScript program: benchmarksgame.alioth.debian.org/u64q/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.794Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":485,"estimatedTokens":3082}}399{"id":"stack-1693842","source":"stackoverflow","questionId":1693842,"title":"Is there any way to determine the available stack space at run time?","tags":["c","linux","compiler-construction","stack","stack-overflow"],"text":"Title: Is there any way to determine the available stack space at run time?\nTags: c, linux, compiler-construction, stack, stack-overflow\nSource: Stack Overflow\n\nQuestion:\nI know that stack size is fixed. So we can not store large objects on stack and we shift to dynamic allocations (e.g. malloc). Also, stack gets used when there is nesting of function calls so we avoid recursive functions as well for this reason. Is there any way at runtime to determine how much stack memory is used so far and how much is left ? \n\nHere, I am assuming linux environment (gcc compiler) with x86 architecture.\n\n========================================\n\nTop Answer:\nThere is a pthread API to determine where the stack lies:\n\n```\n#include \n\nvoid PrintStackInfo (void)\n { pthread_attr_t Attributes;\n void *StackAddress;\n int StackSize;\n\n // Get the pthread attributes\n memset (&Attributes, 0, sizeof (Attributes));\n pthread_getattr_np (pthread_self(), &Attributes);\n\n // From the attributes, get the stack info\n pthread_attr_getstack (&Attributes, &StackAddress, &StackSize);\n\n // Done with the attributes\n pthread_attr_destroy (&Attributes);\n\n printf (\"Stack top: %p\\n\", StackAddress);\n printf (\"Stack size: %u bytes\\n\", StackSize);\n printf (\"Stack bottom: %p\\n\", StackAddress + StackSize);\n }\n```\n\nOn i386, the stack starts at the bottom and grows towards the top.\n\nSo you know you have ($ESP - StackAddress) bytes available.\n\nIn my system, I have a wrapper around pthread_create(), so each thread starts in my private function. In that function, I find the stack as described above, then find the unused portion, then initialize that memory with a distinctive pattern (or \"Patton\", as my Somerville, MA-born father-in-law would say).\n\nThen when I want to know how much of the stack has been used, I start at the top and search towards the bottom for the first value that doesn't match my pattern.\n\n========================================\n\nCode:\n```text\n/proc/<pid>/smaps\n```\n\n```text\n%esp\n```\n\n```text\nsmaps\n```\n\n```text\nulimit\n```\n\n```text\n#include <pthread.h>\n\nvoid PrintStackInfo (void)\n   {   pthread_attr_t Attributes;\n       void *StackAddress;\n       int StackSize;\n\n   // Get the pthread attributes\n   memset (&Attributes, 0, sizeof (Attributes));\n   pthread_getattr_np (pthread_self(), &Attributes);\n\n   // From the attributes, get the stack info\n   pthread_attr_getstack (&Attributes, &StackAddress, &StackSize);\n\n   // Done with the attributes\n   pthread_attr_destroy (&Attributes);\n\n   printf (\"Stack top:     %p\\n\", StackAddress);\n   printf (\"Stack size:    %u bytes\\n\", StackSize);\n   printf (\"Stack bottom:  %p\\n\", StackAddress + StackSize);\n   }\n```\n\n========================================\n\nComments:\n- what about heap segment? I heard that stack and heap segments grow opposite to each other? Will that affect the effective size of stack? Correct me if I am wrong here.\n- for your problem you can treat it as orthogonal.. free store or heap is not the stack space, and 'growing opposite' is not a helpful way to think about it. at the point of heap allocation (new-ing your types) you stack size is likely to be briefly/temporarily affected but that's not the problem you're facing..\n- It is not as simple as \"they grow opposite to each other\". glibc can simply use mmap() to ask for an additional virtual memory area, and the may in theory live anywhere on the virtual memory address space, so malloc() won't necessarily will use space that would be used by the stack. The allocation method used by glibc for malloc() may vary depending on many factors (e.g. it may use sbrk(), or it may use mmap()).\n- @diciu, I wanted to know about memory on stack not dynamically allocated memory. Stack is allocated by system and its of fix size.\n- Not it's not of fixed size. See ulimit - it allows you to control the stack size an OS assigns a process.\n- Note, I am talking about Linux on x86 only.","metadata":{"transformedAt":"2026-08-18T18:32:17.794Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":98,"estimatedTokens":974}}400{"id":"stack-8550119","source":"stackoverflow","questionId":8550119,"title":"CoffeeScript-like language written in Python","tags":["javascript","python","compiler-construction","coffeescript"],"text":"Title: CoffeeScript-like language written in Python\nTags: javascript, python, compiler-construction, coffeescript\nSource: Stack Overflow\n\nQuestion:\nAre there any languages targeting JavaScript (like CoffeeScript) and written in Python? I found Pyjamas, but it’s GWT of Python as I see. I want a language that doesn’t need heavy runtime library and is able to be compiled to JavaScript. I found Mascara also, and it very satisfies my requirements except it’s license. CoffeeScript is ideal for me except it’s written in CoffeeScript itself. I have to compile [CoffeeScript-like language] source codes into JavaScript statically in Python application.\n\n========================================\n\nTop Answer:\nYou might want to have a look at pyjaco (python to javascript compiler).\n\nHere's an example to get you started with manipulating the DOM in Python using jQuery:\nhttps://github.com/chrivers/pyjaco/tree/devel/examples/jquery\n\n========================================\n\nComments:\n- @Lion Oh, thanks. I removed it.\n- \"CoffeeScript-like\" as in \"it's just JavaScript\"? Because that's the primary purpose of CoffeeScript: Do what JS does, just in a nicer way. Please elaborate on what defined this \"non-JavaScript-language\". If the sole problem with CoffeeScript is that it's not written in CoffeeScript, you can run from Python via V8.\n- @delnan I wanted to say a language do what JavaScript does in a nicer way.\n- @Lion They are not \"strictly prohibited\" throughout the site, that is ludicrous. However, the majority of the community seems to discourage them. meta.stackexchange.com/questions/5029/&hellip; and meta.stackexchange.com/questions/2950/&hellip;\n- stackoverflow.com/questions/683462/&hellip; -This might be helpful.\n- Do you exclude running a javascript engine from your python app?","metadata":{"transformedAt":"2026-08-18T18:32:17.794Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":449}}401{"id":"stack-51306411","source":"stackoverflow","questionId":51306411,"title":"JVM language interoperability","tags":["java","scala","compiler-construction","programming-languages","java-bytecode-asm"],"text":"Title: JVM language interoperability\nTags: java, scala, compiler-construction, programming-languages, java-bytecode-asm\nSource: Stack Overflow\n\nQuestion:\nRecently I've been writing a compiler for a JVM programming language and I've realised a problem. \n\nI would like to access a Java method from my programming language and also allow a Java method to access a method in my language. The problem is that I need to know the Java methods signature to call it in the bytecode I generate and vice versa. \n\nI've been trying to think of any methods for how Scala does this. Here are my thoughts. \n\n- Scala accesses the `.java` files on the class path and parses them, extracting the method signatures from there.\n\n- `.java` files are compiled to `.class` files. The Java ASM library is then used to access the `.class` files and get the method signatures. The problem with this method is that the `.java` files must be compiled first.\n\n- `.java` files are loaded dynamically using reflection. The problem with this is I believe that the JVM doesn't allow for loading classes that are outside of the compilers class path.\n\nLooking into Scala it works well with other JVM languages but I can't find information on exactly how it does it.\n\nHow does Scala get method signatures of other JVM language methods?\n\n========================================\n\nCode:\n```text\n.java\n```\n\n```text\n.java\n```\n\n```text\n.class\n```\n\n```text\n.class\n```\n\n```text\n.java\n```\n\n```text\n.java\n```\n\n```text\n.java\n```\n\n```text\n.scala\n```\n\n```text\n.class\n```\n\n```text\n.jar\n```\n\n========================================\n\nComments:\n- You should add info that you want to have access to java methods from your language and from java to your methods. (as that other answer was deleted)\n- Having mixed java-yourLanguage projects it a pretty advanced use case.\n- It may be advanced but languages such as Scala/Java can do this. It's not uncommon to mix the two languages and I will have to implement this feature in the future.\n- Reflection gives you only java -> yourLang interoperability, that is: you can call java stuff from your code, but not the other way round. To make it work the other way round, you have to generate something that `javac` can work with, i.e. compile your stuff to jvm bytecode and give the `.class` to the `javac`.\n- That makes sense! So this means if a new JVM language was made Scala would have to write new parsers get the information? Also doesn't this mean that Java has parsers for Scala to be able to get Scala method signatures?\n- Java does not need to know about scala, scala can just generate stubs for own classes, so simple classes that only contains method signatures that are used for compilation of java. So javac will see compiled scala classes, normal ones, or stubs if it is not possible to compile them yet.\n- @Michael Javac doesn't allow to compile mixed projects. Scalac also doesn't allow to compile projects that contain other languages than Java or Scala.\n- I think I understand @GotoFinal. So if I repeat what you've said. Scala will generate `.class` files with stub methods. Then after Java has compiled its classes it would then run the Scala compiler and generate the methods fully?\n- @Michael Yep, just see how compilation of C/Cpp works, you have special header files that you include so you don't need to include full code as this might not always be possible/easy. Or you need to define method at top of the file but implement it somewhere lower - so compiler will already know about this method. But here you need to generate them instead. And then compile again to normal full classes.\n- This makes sense now thank you! Very interesting how the compilers deal with things like this in the background!","metadata":{"transformedAt":"2026-08-18T18:32:17.794Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":10,"totalLines":77,"estimatedTokens":930}}402{"id":"stack-27578507","source":"stackoverflow","questionId":27578507,"title":"Why does the async keyword generate an enumerator & additional struct when compiled?","tags":["c#","asynchronous","compiler-construction","ndepend"],"text":"Title: Why does the async keyword generate an enumerator & additional struct when compiled?\nTags: c#, asynchronous, compiler-construction, ndepend\nSource: Stack Overflow\n\nQuestion:\nIf I create a simple class like the following:\n\n```\npublic class TestClass\n{\n public Task TestMethod(int someParameter)\n {\n return Task.FromResult(someParameter);\n }\n\n public async Task TestMethod(bool someParameter)\n {\n await Task.FromResult(someParameter);\n }\n}\n```\n\nand examine it within NDepend, it shows that the `TestMethod` taking a bool and being `async Task` has a struct generated for it with an enumerator, the enumerator state machine and some additional stuff.\n\nWhy does the compiler generate a struct called `TestClass+d__0` with an enumerator for the async method?\n\nIt seems to generate more IL than what the actual method produces. In this example, the compiler generates 35 lines of IL for my class, while it generates 81 lines of IL for the struct. It's also increasing the complexity of the compiled code and causing NDepend to flag it for several rule violations.\n\n========================================\n\nTop Answer:\nThe original code generation for `async` was closely related to that of enumerator blocks, so they started off using the same code in the compiler for those two code transformations. It has changed quite a bit since then, but it still has some holdovers from the original design (such as the name `MoveNext`).\n\nFor more about the compiler-generated parts, Jon Skeet's blog series is the best source.\n\n========================================\n\nCode:\n```text\npublic class TestClass\n{\n    public Task TestMethod(int someParameter)\n    {\n        return Task.FromResult(someParameter);\n    }\n\n    public async Task TestMethod(bool someParameter)\n    {\n        await Task.FromResult(someParameter);\n    }\n}\n```\n\n```text\nTestMethod\n```\n\n```text\nasync Task\n```\n\n```text\nTestClass+<TestMethod>d__0\n```\n\n```text\n[TestClass]\npublic class AsyncTest\n{\n    [TestMethod]\n    public async Task RunTest_1()\n    {\n        var result = await GetStringAsync();\n        Console.WriteLine(result);\n    }\n\n    private async Task AppendLineAsync(StringBuilder builder, string text)\n    {\n        await Task.Delay(1000);\n        builder.AppendLine(text);\n    }\n\n    public async Task<string> GetStringAsync()\n    {\n        // Code before first await\n        var builder = new StringBuilder();\n        var secondLine = \"Second Line\";\n\n        // First await\n        await AppendLineAsync(builder, \"First Line\");\n\n        // Inner synchronous code\n        builder.AppendLine(secondLine);\n\n        // Second await\n        await AppendLineAsync(builder, \"Third Line\");\n\n        // Return\n        return builder.ToString();\n    }\n}\n```\n\n```text\n[TestMethod]\npublic async Task RunTest_2()\n{\n    var result = await GetStringAsyncWithoutAwait();\n    Console.WriteLine(result);\n}\n\npublic Task<string> GetStringAsyncWithoutAwait()\n{\n    // Code before first await\n    var builder = new StringBuilder();\n    var secondLine = \"Second Line\";\n\n    return new StateMachine(this, builder, secondLine).CreateTask();\n}\n\nprivate class StateMachine\n{\n    private readonly AsyncTest instance;\n    private readonly StringBuilder builder;\n    private readonly string secondLine;\n    private readonly TaskCompletionSource<string> completionSource;\n\n    private int state = 0;\n\n    public StateMachine(AsyncTest instance, StringBuilder builder, string secondLine)\n    {\n        this.instance = instance;\n        this.builder = builder;\n        this.secondLine = secondLine;\n        this.completionSource = new TaskCompletionSource<string>();\n    }\n\n    public Task<string> CreateTask()\n    {\n        DoWork();\n        return this.completionSource.Task;\n    }\n\n    private void DoWork()\n    {\n        switch (this.state)\n        {\n            case 0:\n                goto state_0;\n            case 1:\n                goto state_1;\n            case 2:\n                goto state_2;\n        }\n\n        state_0:\n            this.state = 1;\n\n            // First await\n            var firstAwaiter = this.instance.AppendLineAsync(builder, \"First Line\")\n                                        .GetAwaiter();\n            firstAwaiter.OnCompleted(DoWork);\n            return;\n\n        state_1:\n            this.state = 2;\n\n            // Inner synchronous code\n            this.builder.AppendLine(this.secondLine);\n\n            // Second await\n            var secondAwaiter = this.instance.AppendLineAsync(builder, \"Third Line\")\n                                            .GetAwaiter();\n            secondAwaiter.OnCompleted(DoWork);\n            return;\n\n        state_2:\n            // Return\n            var result = this.builder.ToString();\n            this.completionSource.SetResult(result);\n    }\n}\n```\n\n```text\nasync\n```\n\n```text\nawait\n```\n\n```text\nGetStringAsync\n```\n\n```text\nStringBuilder\n```\n\n```text\nawait\n```\n\n```text\nAsyncTest\n```\n\n```text\nawait\n```\n\n```text\ngoto\n```\n\n```text\nforeach\n```\n\n```text\nyield\n```\n\n```text\nawait\n```\n\n```text\nasync\n```\n\n```text\nMoveNext\n```\n\n========================================\n\nComments:\n- The code in async/await is based on Jeff Richter's Async Enumerator pattern. There is already a voice suggesting a fix for this ndepend.uservoice.com/forums/226344-ndepend-user-voice/&hellip;.\n- Because that's how async/await works. What you see is the implementation detail of the feature.\n- This really underlines the importance of making sure you're not blindly using async all over the place when it's not needed. That's more overhead than what I guess I was expecting.\n- @JohnathonSullinger Use async only when you need it.\n- @Aron: I could be wrong, but I don't think `async` is based on Async Enumerator. While they appear similar at first glance, `async` is capable of much more complex transformations.\n- @StephenCleary much much much more complex yes. But creating an enumerator to store state of a function between yielding control back to the caller.\n- Alternate (newer) link, in case the old one breaks at some point: codeblog.jonskeet.uk/2011/05/08/eduasync-part-1-introduction\n- Thanks for the details answer, this really helped make it clear. I will be sure to check out the link as well\n- In this example, your DoWork() method is a simplified variation of .MoveNext() in the async state machine right?\n- That's right. Minus optimization, cancellation, exception handling, internal framework methods.","metadata":{"transformedAt":"2026-08-18T18:32:17.794Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":19,"totalLines":249,"estimatedTokens":1604}}403{"id":"stack-50049272","source":"stackoverflow","questionId":50049272,"title":"Calculating administrative normal form","tags":["compiler-construction","ssa","anf"],"text":"Title: Calculating administrative normal form\nTags: compiler-construction, ssa, anf\nSource: Stack Overflow\n\nQuestion:\nAdministrative Normal Form is an intermediate representation of code, suitable for use by compilers, that is logically equivalent to Single Static Assignment but has some advantages. For example, checking whether a program is a valid SSA form is an existential question about the set of possible paths through a graph. However, checking whether a program is a valid ANF expression is just a question of local syntax.\n\nIt is quite easy to generate ANF from strictly functional code, but I'm interested in producing it from imperative code that contains variable updates, loops, etc.\n\nThere are straightforward algorithms for converting SSA to ANF. However, generating SSA in the first place becomes nontrivial if you want to do it quickly. It seems intuitive that if what you want to end up with is the more straightforward, more transparent format, it should be more efficient to generate it directly rather than going via the more opaque form.\n\nIs there a published algorithm for generating ANF directly from imperative code?","metadata":{"transformedAt":"2026-08-18T18:32:17.795Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":12,"estimatedTokens":286}}404{"id":"stack-33145147","source":"stackoverflow","questionId":33145147,"title":"What is an LLVM virtual section in the context of object files?","tags":["compiler-construction","llvm","object-files"],"text":"Title: What is an LLVM virtual section in the context of object files?\nTags: compiler-construction, llvm, object-files\nSource: Stack Overflow\n\nQuestion:\nWhilst looking at a bugfix in the LLVM source code, I came across the term, \"virtual section\" and wondered what it meant.\n\nI tried Googling a few different terms and browsing the source code further, but all I managed to find was that the implementations for each object file format's `isSectionVirtual` member function appear to express that a section is virtual if it has no contents (such as a .bss section, but the source code clearly expresses that these are two different concepts). The implementation varies depending on the specific object format involved.\n\nI am fairly new to understanding the innards of object files, so I am not sure if this is an LLVM thing or a more general concept present outside of LLVM.\n\nCould somebody please tell me what a virtual section is in an object file?\n\n========================================\n\nCode:\n```text\nisSectionVirtual\n```\n\n```text\nif (Sec.isBSS() || Sec.isVirtual())\n```\n\n```text\nbool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const {\n  // FIXME: Unimplemented.\n  return false;\n}\n```\n\n```text\n.bss\n```\n\n```text\n.bss\n```\n\n```text\nisBSS\n```\n\n```text\nisVirtual\n```\n\n========================================\n\nComments:\n- Surely the separate checks indicate that these are in fact different concepts? Otherwise, there'd just be `isBSS` and `isVirtual` wouldn't need to exist. Thanks for your input regardless!\n- Thanks, that's much better!","metadata":{"transformedAt":"2026-08-18T18:32:17.795Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":7,"totalLines":52,"estimatedTokens":388}}405{"id":"stack-5349562","source":"stackoverflow","questionId":5349562,"title":"understanding c++, compilation etc","tags":["c++","compiler-construction"],"text":"Title: understanding c++, compilation etc\nTags: c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nDo you know a good, concise (not a book, possibly free documentation) reference that explains the basics of how a c++ compiler works? I think it'd be beneficial for better understanding some rules of c++ programming.\n\n========================================\n\nTop Answer:\nI found this university site which explains the steps of the compilation process. It covers how to compile your code into an executable, a shared library, or a dynamically linked library using GCC. It also covers how the compiler knows where to look for header files and libraries, and other topics.","metadata":{"transformedAt":"2026-08-18T18:32:17.795Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":11,"estimatedTokens":170}}406{"id":"stack-6979570","source":"stackoverflow","questionId":6979570,"title":"Compiling functional languages to C","tags":["c","compiler-construction","functional-programming","garbage-collection","language-implementation"],"text":"Title: Compiling functional languages to C\nTags: c, compiler-construction, functional-programming, garbage-collection, language-implementation\nSource: Stack Overflow\n\nQuestion:\nSuppose you're compiling a functional language to portable C, and suppose also that for various reasons you want precise rather than conservative garbage collection. There is no portable way (perhaps no way at all in the general case) for the garbage collector to figure out what is and isn't a pointer on the C stack. It seems to me there are two solutions to this problem:\n\nShadow stack. Make each C function maintain bookkeeping information about what is and isn't a pointer. This is the approach recommended by e.g. LLVM.\n\nTake advantage of the fact that you are compiling a functional language, which means mainline code has no side effects. When the allocator detects out of memory, instead of calling the garbage collector itself, it aborts the current operation with a longjmp back to the main loop, which calls the garbage collector (in a context where the set of variables that may contain pointers is known in advance) then restarts the operation.\n\nIt seems to me that, if you are dealing with a pure functional language where the second approach is applicable, it must be more efficient than the first approach, as well as easier to mix with handwritten C.\n\nAre there any problems I'm overlooking? Any references to existing discussion or implementations of this technique?\n\n========================================\n\nTop Answer:\nIt is possible to design a pure FP language using a single data structure:\n\n```\ntypedef enum record_type { RT_SYMBOL, RT_NUMBER, RT_PAIR };\n\nstruct record\n{\n record_type type;\n void *value; \n};\n```\n\nPrograms and data can be represented using `pairs` of `records`:\n\n```\nstruct pair\n{\n record *car;\n record *cdr;\n};\n```\n\nHere is how a simple expression - `2 * 3` - could be represented using `records`:\n\n```\nrecord r1;\nr1.type = RT_NUMBER;\nr1.value = &two; \n\nrecord r2;\nr1.type = RT_NUMBER;\nr1.value = &three; \n\nrecord opr1;\nopr1.type = RT_NUMBER;\nopr1.value = &OP_MULT; /* A machine op-code for multiplication. */\n\npair p_oprnds;\np_oprnds.car = &r1;\np_oprnds.cdr = &r2;\n\npair p;\np.car = opr1;\np.cdr = p_oprnds;\n```\n\nThis is the same as the Lisp expression: `(* 2 3)`. Now you can define a machine that operates on `pairs`, treating the `car` as an operator and the `cdr` as operands. As we deal with only one data structure, precise GC is possible. See Lispkit Lisp for the architecture of such a VM. \n\nAlso read Lisp in Small Pieces before starting off with a serious attempt on writing an FP -> C compiler.\n\n========================================\n\nCode:\n```text\ntypedef enum record_type { RT_SYMBOL, RT_NUMBER, RT_PAIR };\n\nstruct record\n{\n  record_type type;\n  void *value;  \n};\n```\n\n```text\nstruct pair\n{\n  record *car;\n  record *cdr;\n};\n```\n\n```text\nrecord r1;\nr1.type = RT_NUMBER;\nr1.value = &two; \n\nrecord r2;\nr1.type = RT_NUMBER;\nr1.value = &three; \n\nrecord opr1;\nopr1.type = RT_NUMBER;\nopr1.value = &OP_MULT; /* A machine op-code for multiplication. */\n\npair p_oprnds;\np_oprnds.car = &r1;\np_oprnds.cdr = &r2;\n\npair p;\np.car = opr1;\np.cdr = p_oprnds;\n```\n\n```text\npairs\n```\n\n```text\nrecords\n```\n\n```text\n2 * 3\n```\n\n```text\nrecords\n```\n\n```text\n(* 2 3)\n```\n\n```text\npairs\n```\n\n```text\ncar\n```\n\n```text\ncdr\n```\n\n========================================\n\nComments:\n- Possibly not helpful, but I tried the first whilst writing mark-sweep for my scheme interpreter. The performance sucked, so I ended up with a purely virtual stack outside the stack of the C runtime, mainly as cross-runtime stack introspection is virtually impossible. The performance also sucked but it was easier to debug without gdb/ddd. I decided to make do as this was the interpreter and tackle it when I got to the compiler stage of implementation (which never got finished typically).\n- How do you plan to restart the current operation? Save checkpoints from time to time, then restore the last good one (how?)\n- @n.m.: the important part of the question in that respect is \"code has no side effects\". The questioner is assuming a pure functional language, so no state is ever modified. There's no need to \"take\" a checkpoint, and when you jump to a previous state you don't need to \"undo\" any changes because the language is not capable of making changes. In principle, your position in the code tells you everything you need to know about the state of the program.\n- @n.m. That's a good question. It's easy to imagine for a byte-code interpreter, just `longjmp` back to `eval()`. But for compiled, I'm not sure. Surely you don't want to put `setjmp`s around every allocation!\n- @luser droog: Or you could in effect put a `setjmp` after each return from a function in the functional language. That's when variables go out of scope, so anything collectable now was collectable at the last such point. The questioner seems to suggest only `setjmp` in the main interpret loop, I presume because that's at the top of the stack and he has in mind that therefore he doesn't need to worry about accurate vs conservative marking on the stack.\n- I don't want to be constantly issuing setjmp calls, that could easily eat up much of the performance gain. I'm hoping to do something along the lines of having just one setjmp before the main loop, and then each time around the loop, only having to save the current program counter in a global variable.\n- @Steve Jessop: the stack contains some information that is lost in `longjmp`. You need to save and restore it somehow, or else why have it in the first place?\n- My thinking is that you don't need to save it, because you can recreate it from scratch, just restart the entire operation. Let's say that involves redoing a millisecond of work, but garbage collection only runs once per second, the time spent on redoing work will still be negligible.\n- What if your entire program is one function that runs for 10 seconds and hits OOM in the middle? Will you restart your entire program endlessly?","metadata":{"transformedAt":"2026-08-18T18:32:17.795Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":155,"estimatedTokens":1509}}407{"id":"stack-9014958","source":"stackoverflow","questionId":9014958,"title":"How do I use a binary prefix in accordance with C11?","tags":["c","compiler-construction","standards","standards-compliance","c11"],"text":"Title: How do I use a binary prefix in accordance with C11?\nTags: c, compiler-construction, standards, standards-compliance, c11\nSource: Stack Overflow\n\nQuestion:\nI am currently starting out with programming micro controllers using `C30` (A `C` compiler based on `GCC` from microchip for their `PIC24` devices) and I enabled `Strict ANSI warnings` out of curiosity. First off, I did not know that in C11 comment markings like // are \"wrong\" and instead I should use /* blah blah */, but what really surprised me is this warning for a line of code.\n\n\"warning: use of non-standard binary prefix\"\n\nThe line of code is:\n\n`OSCCONbits.COSC = 0b000;`\n\nI have looked online at one of the drafts of C11 (ISO/IEC 9899:2011) and can't find anything about binary prefixes in C. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf\n\nWhat is the correct binary notation for C according to C11?\n\n========================================\n\nTop Answer:\nRegarding standards:\n\n- ANSI C / \"Strict ANSI\" typically refers to the first standard version of C, standardized only in the USA 1989. Sometimes it is referred to as \"C89\".\n\n- ANSI C/89 became obsolete in 1990 when C became an international C standard, ISO/IEC 9899:1990, referred to as \"C90\". C89 and C90 are equivalent when it comes to technical details.\n\n- C90 became obsolete in 1999, when ISO C was updated. The new standard is referred to as \"C99\".\n\n- C99 became obsolete in 2011. The new standard is referred to as \"C11\".\n\nRegarding your compiler problems:\n\n- C89/C90 does not allow // comments. They were introduced in C99. They have not been removed in C11.\n\n- Binary notation has never been part of any C standard.\n\nConclusion:\n\n- You are most likely compiling the code on a C90 compiler, with some non-standard extensions available.\n\n========================================\n\nCode:\n```text\nC30\n```\n\n```text\nC\n```\n\n```text\nGCC\n```\n\n```text\nPIC24\n```\n\n```text\nStrict ANSI warnings\n```\n\n```text\nOSCCONbits.COSC = 0b000;\n```\n\n```text\ngcc\n```\n\n```text\ngcc\n```\n\n```text\n0\n```\n\n```text\n1\n```\n\n```text\n0b\n```\n\n```text\n0B\n```\n\n```text\ngcc\n```\n\n```text\n0b000\n```\n\n```text\n0\n```\n\n```text\n0\n```\n\n```text\n0x\n```\n\n```text\n//\n```\n\n========================================\n\nComments:\n- Convert it to a hexadecimal number. As for setting something to (binary) `0000` why not just use a normal decimal `0` or hexadecimal `0x00`?\n- Who told you // comments are \"wrong\"?\n- I think I may have worded my question wrong, so here are some corrections. I know that I can just use hex or decimal instead of binary, I just wanted to know what was the \"right\" way to use binary literals. Regarding me saying // comments are \"wrong\", I got that from a compiler warning, no one told me that but the compiler. Anyways, thanks for all the help guys! I got some awesome answers and I wish I could mark them all as answers but I could do that to only one sadly.\n- @hak8or: then it was not a c99 or c11 compiler.\n- @JoachimPileborg: To be completely pedantic, `0` is an *octal* constant, not decimal(!)\n- This is not a correct interpretation of C11&#167;6.6p10. &#167;6.6 is intended to carve out a subset of valid expressions that are guaranteed to evaluate to constants. It does not extend the parser or lexer (which would be needed to support binary literals).\n- @SamElliott I'm inclined to agree. I'll have to think about this a bit more. If you're right, it implies that binary literals can be supported as an extension under C11 4p6, but a conforming compiler must still issue a diagnostic for `0b000` because it violates a syntax rule.\n- Bummer they felt it wasn't of sufficient utility. I use `0b` all the time in code that does bit-twiddling for high-speed integer operations (having nothing to do with microcontrollers). `0b10101010` is much more readable to me than `0xAA`, for example. Also a bummer they felt there wasn't precedent; Java has had `0b` for a while now.\n- Binary integer constants (`0b`) are now supported in C23","metadata":{"transformedAt":"2026-08-18T18:32:17.795Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":18,"totalLines":127,"estimatedTokens":989}}408{"id":"stack-24964836","source":"stackoverflow","questionId":24964836,"title":"Compiling high-level language to machine code","tags":["c++","assembly","compiler-construction","machine-code"],"text":"Title: Compiling high-level language to machine code\nTags: c++, assembly, compiler-construction, machine-code\nSource: Stack Overflow\n\nQuestion:\nAfter reading some answers from the site and viewing some sources, I thought that the compiler converts high-level language (C++ as an example) to machine code directly as the computer itself doesn't need to convert it to assembly, it only converts it to assembly for the user to view the code and can have more control over the code if needed.\n\nBut this was found in one of my lecture sheets, so can I would appreciate if someone could explain further and correct me if I am wrong, or the screenshot below.\n\n========================================\n\nTop Answer:\nThis slide is confusing bytecode with textual assembly.\nAssembly is a human readable version of either bytecode or machine code. Machine code is what the hardware can run directly. Bytecode is further compiled to machine code, it is low level, but generic.\n\nSome languages use byte code which is translsted during runtime into even lower level machine code. One example of this is java, where class files will sometimes be compiled to machine code asa runtime optimization. Another is cuda, where each nvidia gpu has a different instruction set but the cuda compiler generates bytecode that the cuda driver for each gpu can then translate.\n\nAnother option is that he is talking about how intel processors translate machine code during runtime into internal microcode and then run it, this is completely invisible to software though, including the OS.\n\n========================================\n\nCode:\n```text\n#include\n```\n\n```text\n#define\n```\n\n========================================\n\nComments:\n- Simple. That explanation is blatantly wrong.\n- Where did you get that slide? it's completely wrong.\n- Out of interest... Who gave you those slides? They are so wrong it's almost funny\n- The lecturer might have had managed languages (like Java or C#) in mind, which compile to a machine-independent bytecode that is then translated to native machine code at runtime. This is of course still no excuse for such an utterly misleading slide.\n- @ComicSansMS: But then they shouldn't call out C++ as the example in the slide.\n- @sharth Of course. The slides now are just complete nonsense. I was just trying to reconstruct what might have driven the lecturer to make such absurd claims.\n- Assembly is arbitrary for computers, it's nothing more than a bunch of `ctrl-h MOV 10110110` for the computers, it's the same thing to them. Assebly as a tool and concept is just for humans.\n- If you got the slide from your lecturer, drop out immediately.\n- @n.m. Not just immediately. Punch the lecturer before leaving :P ... @_OP Well seriously, your lecture might refer to c++-cli rather than plain c++.\n- Unfortunately I got this from my \"Doctor\" that I now really doubt that he is a doctor, at the university... @πάνταῥεῖ ῥεῖ I am working on a bomb, no need to worry about wrong slides anymore!\n- @KarimK. Be cautious throwing a bomb or trying to slap your teacher! You've presented this slide without any additional context. It may make (some) sense within this context. But I'd still say, it's not a very good explanation, for whatever the doc wanted to teach you.\n- Try applying this slide to interpretive languages like BASIC or LISP. It doesn't work because the OS doesn't touch your program, the interpreter does.\n- @πάνταῥεῖ ῥεῖ i.imgur.com/vfpXVFn.png ... Do you still think so? I obtained this from another slide by the way.\n- \"Every machine like Toshiba, Dell or HP has its own machine code blabla\" (on the left) is also complete bs.\n- Alright, that cleared so many doubts! But I've another question that I didn't find an answer for, this part \"Every Machine like, Toshiba and Dell or HP, has it own machine codes\" lets say I write a C++ code, at which step is it converted to the machine-code related to (Intel/AMD processor)? Don't each of processor family or brand has it's own machine-code?\n- @KarimK. Like sharth explained, each **processor architecture** has it's own set of assembly instructions. An Apple Macintosh and HP could be using the same architecture with the same instructions such that you would compile the same C++ code on each and end up with the same Assembly when you compile.\n- @Josh, do you mean \"output file\" when you said \"Assembly\" there at the end of your comment? That usage might add a slight bit of confusion.\n- @KarimK. In addition, once code is compiled into assembly, it is then assembled by an assembler, which creates a binary file your processor can read and execute.\n- @Namfuak: Your operating system reads the binary file. Your processor doesn't know what to do with ELF, COFF, etc.\n- @Namfuak: The assembly part isn't even necessary -- it just makes it so that the compiler can leverage an existing assembler (read: be lazy), and makes it easier for humans to see what's being generated. But it's entirely possible to skip that step, and a number of compilers do skip it unless you ask them to translate to assembly.\n- @Josh So a C++ compiler for lets say x64 processor is different from a C++ compiler for a x32 processor?\n- @KarimK. *'So a C++ compiler for ...'* Yes the machine code generating backends are different and need to be specified. The frontend handling the parsing of the language used may still be the same.\n- @cHao: Assembly language listings are very useful, especially in the Embedded Systems Domain. They can be used when optimizing, to see how the compiler generated code. They are also useful for debugging when the compiler generated code that doesn't match the source listing.\n- +1 That very well explains, what I'm suspecting about this particular slide just now.\n- A minor comment: the *translation* phase can convert high level language statements directly into an object file or machine code, skipping the assembly language phase. Many compilers and translators do this (because of impatient people and build processes).\n- Sure, but this is the classical breakdown in a form that seems likely to be appropriate to the level of the OP. If I added every “if”, “and”, and “but”, and started talking about translation units and IPO, it would occupy at least one bookshelf rather than merely several paragraphs.","metadata":{"transformedAt":"2026-08-18T18:32:17.795Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":59,"estimatedTokens":1570}}409{"id":"stack-8762234","source":"stackoverflow","questionId":8762234,"title":"Why doesn't this obvious infinite recursion give a compiler warning?","tags":["c#","c++","python","compiler-construction","recursion"],"text":"Title: Why doesn't this obvious infinite recursion give a compiler warning?\nTags: c#, c++, python, compiler-construction, recursion\nSource: Stack Overflow\n\nQuestion:\nMany months back, I had to fix up some code that caused some problems. The code looked basically like this: \n\n`int badFun() { return badFun(); }`\n\nThis obviously caused a stack overflow even in the high level language I was working with (4Test in SilkTest). There's no way this code could be seen as beneficial. The first sign of problems were warnings seen after the script finished, but no compile errors or warnings. Curiously, I tried writing programs in C++, C# and Python with the same structure, and all of them compiled/interpreted with no syntax errors or warnings, even through there were runtime errors in all cases. I didn't even see any warnings in any of these cases. Why isn't this seen as a possible problem by default? \n\nEDIT: I tried writing the equivalent of that function in all three languages, so I added those function tags. I'm more interested in overall reasons why code like this gets through with no warnings. Please retag if necessary.\n\n========================================\n\nTop Answer:\nWhy isn't this seen as problem by default?\n\nThe error is a run time error, not a compile time error. The code is perfectly valid, it just does something stupid. The very simple case that you show could certainly be detected, but many cases that would be only slightly more complicated would be difficult to detect:\n\n```\nvoid evil() {\n if (somethingThatTurnsOutToAlwaysBeTrue)\n evil();\n}\n```\n\nIn order to determine whether that's a problem, the compiler has to try to figure out whether the condition will always be true or not. In the general case, I don't think this is any more computable than determining whether the program will eventually stop (i.e. it's provably not computable).\n\n========================================\n\nCode:\n```text\nint badFun() { return badFun(); }\n```\n\n```text\nResharper\n```\n\n```text\nVisual Studio\n```\n\n```text\nvoid evil() {\n    if (somethingThatTurnsOutToAlwaysBeTrue)\n        evil();\n}\n```\n\n========================================\n\nComments:\n- Is your question itself about C#, C++ *or* Python? The code sample looks a whole lot like C# to me.\n- It's not the compiler's job to keep you from doing something stupid. In most languages, it's possible for some external event to cause a construct like this to exit.\n- In MSVC (C++), it will give you a warning if you call the function: `warning C4717: 'badFun' : recursive on all control paths, function will cause runtime stack overflow`\n- Because it's not the computer's responsibility to prevent you from intentionally shooting yourself in your own foot: fullduplex.org/humor/2006/10/&hellip; See Lisp.\n- Some IDE tools will warn you, such as Resharper for C# and VB.NET in Visual Studio.\n- But it's the compiler's job to tell me I have a single unused int variable? As opposed to something that can crash my entire app if called?\n- Tail recursion like this may be implemented as a loop by the compiler, and so this code may not be a runtime error, instead similar to `while(true);`\n- @joshin4colours: That isn't the compiler's job either (at least in C++). It is purely a quality of implementation question / balancing the cost of detecting this problem (compile-time overhead, programming effort) vs the gains (catching a few problems). - Basically, it is impossible to miss an infinite loop/recursion at runtime, whereas \"unused\" variables resulting from typos can lead to much more subtle problems.\n- @casperOne: Why was this closed?\n- @NeilG: Just guess but at a minimum the \"why\" could be different for every single implementation out there (for *four* languages none the less). Rotten question. The right answer is *\"Someone decided the `odds of a programer doing that &#47; work required to detect it` made it too low a priority to bother with. End of story.\n- +1. Compilers don't bother because the halting problem is uncomputable in the general case.\n- By the same logic, a compiler wouldn't bother to warn about unreachable code. And yet it does - in a particular subset of cases.\n- @harold As Eric Lippert explained nicely, it boils down to a decision about the cost and benefit of producing the warning. What I was trying to say here is that detecting the trivial case doesn't seem very useful, and the nontrivial cases where a warning would be useful are likely to be difficult or impossible to detect.\n- The compiler knows a lot about what your function is doing. It knows all code paths in the function. It uses this semantic knowledge to do analysis like detecting unreachable code, ensuring a variable is initialized before read on all code paths,...\n- The compiler understands a lot about the semantics of code. It would be quite easy to implement this warning on a reasonable level. But I don't know if its worth the work to specify, implement, document and maintain that feature.\n- +1 Excellent answer and really gets at what I was asking about. I know compilers shouldn't keep you from writing awful code, but good compilers should at least throw a \"hey, whatcha doin there?\" sometimes.\n- There is the related problem with `int BadProperty{get{return BadProperty;}}` and its setter equivalent, where the mistake is very easy to make, and not that easy to spot. But this problem is clearly easy to detect when you actually run the code, which reduces the benefit of this feature a lot.\n- @CodeInChaos: Indeed, that is one place where the warning might be justified.\n- I don't agree with the inobvious -- you should also get warnings for obvious mistakes.","metadata":{"transformedAt":"2026-08-18T18:32:17.795Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":73,"estimatedTokens":1411}}410{"id":"stack-8251645","source":"stackoverflow","questionId":8251645,"title":"Generational GC source code","tags":["c","compiler-construction","resources","garbage-collection"],"text":"Title: Generational GC source code\nTags: c, compiler-construction, resources, garbage-collection\nSource: Stack Overflow\n\nQuestion:\nI am studying GC implementations, and I'm currently looking for references and good open-source GC examples to base in.\n\nIs there any good and simple generational GC implementation ? The second best thing would be good resources and guidelines!\n\nThank you!\n\n========================================\n\nTop Answer:\n### Java's HotSpot GC\n\nYou can look at the various GC implementations provided by the JVM here.\n\nThe Memory Management white paper gives an overview of the different garbage collectors implemented in the JVM. Its from 2006 so its missing the new G1 collector details but its a good starting point.\n\n### Mon's SGen GC\n\nMono's new SGen is on github too. Check out the sgen files.\n\n========================================\n\nCode:\n```text\nGgc\n```\n\n========================================\n\nComments:\n- @Waneck: out of curiosity, what kind of GC do you want, and what kind of software motivates your question? (if it is a compiler, for what language or kind of language?)\n- I'm currently finishing the HaXe (haxe.org) targets for C# and Java, and I intend to apply everything I learned from optimizing dynamic accesses to an ANSI C target. I was looking for generational GC's because haxe borrows much from funcional programming, and I really wanted to be able to have mainly short lived objects.\n- But after having a look right now at the alternatives, I still have doubts over which path to , since my primary two applications would benefit very much from soft real-time and multi-threading: games and servers. I'm having a look right now at java's G1 architecture, but at first I'm probably going to implement something in the likes of Qish. I didn't feel too comfortable with volatiles everywhere, but I guess it's a fair requirement.\n- The Boehm-GC does not use generational garbage collection.\n- @silentbicycle: The collector uses a mark-sweep algorithm. It provides incremental and **generational collection**.\n- You're right, I stand corrected. I don't remember that from the paper. The conservative aspect gets more emphasis.\n- Qish looks pretty cool, though I wish you finished multithreading too (I imagine it doesn't play well with explicit `qish_write_notify()` though). If I remember, I might port this to Windows at some point...\n- multi-threading GC is difficult, and multi-threading generational GC is even more difficult. I've read perhaps a thousand papers on GC without having a good idea of how to do a good multi-threaded generational GC.\n- Oh I don't doubt, it wasn't a request - literally a wish in this case :). If you could run a GC instance per-thread, it would probably be fine for a lot of cases since you're already requiring a lot of the user, and object references crossing threads is a Bad Idea in general. (though you might need more robust object rooting?)\n- wow, I really really liked your code. And it's really what I've been looking for, since I'm generating c code from a compiler. Thanks! Just bought the book, by the way\n- It would be great, though, to have it implemented really for concurrent access. Though maybe as you've said, a generational gc might not be the best option for concurrency. Anyway your code gives me a good start! Thanks\n- Look also at my MELT GC and my DSL11 paper; it is for generated code... Some people told me about Qish that it is using not very portable tricks (I forgot which, perhaps va_arg related)..\n- I will look at them too, thank you very much! I'd love to talk better about the gc design, if you want I can post my findings here or we can get in touch by mail (don't know SO's policy on comments flood :) )?\n- As you want; my email is publicly available.\n- Thanks for your answer! Unfortunately it seems very complex to be able to get the gist of it... Maybe when I'm more versed into the algorithms it will be a great resource!\n- +1 ! Really one of the cleanest implementations ! Thanks for the link! If only the 'big' vm's could comment their code like this!","metadata":{"transformedAt":"2026-08-18T18:32:17.795Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":50,"estimatedTokens":1019}}411{"id":"stack-1832621","source":"stackoverflow","questionId":1832621,"title":"C++ CPU Register Usage","tags":["c++","compiler-construction","assembly","mips","cpu-registers"],"text":"Title: C++ CPU Register Usage\nTags: c++, compiler-construction, assembly, mips, cpu-registers\nSource: Stack Overflow\n\nQuestion:\nIn C++, local variables are always allocated on the stack. The stack is a part of the allowed memory that your application can occupy. That memory is kept in your RAM (if not swapped out to disk). Now, does a C++ compiler always create assembler code that stores local variables on the stack?\n\nTake, for example, the following simple code:\n\n```\nint foo( int n ) {\n return ++n;\n}\n```\n\nIn MIPS assembler code, this could look like this:\n\n```\nfoo:\naddi $v0, $a0, 1\njr $ra\n```\n\nAs you can see, I didn't need to use the stack at all for n. Would the C++ compiler recognize that, and directly use the CPU's registers?\n\n**Edit:** Wow, thanks a lot for your almost immediate and extensive answers! The function body of foo should of course be `return ++n;`, not `return n++;`. :)\n\n========================================\n\nTop Answer:\nYes. There is no rule that \"variables are always allocated on the stack\". The C++ standard says nothing about a stack.It doesn't assume that a stack exists, or that registers exist. It just says how the code should behave, not how it should be implemented.\n\nThe compiler only stores variables on the stack when it has to - when they have to live past a function call for example, or if you try to take the address of them.\n\nThe compiler isn't stupid. ;)\n\n========================================\n\nCode:\n```text\nint foo( int n ) {\n   return ++n;\n}\n```\n\n```text\nfoo:\naddi $v0, $a0, 1\njr $ra\n```\n\n```text\nreturn ++n;\n```\n\n```text\nreturn n++;\n```\n\n```text\nint foo1 (int n) { return ++n; }\nint foo2 (int n) __attribute__((fastcall));\nint foo2 (int n) {\n    return ++n;\n}\n```\n\n```text\nmov eax,DWORD PTR [esp+0x4]\nadd eax,0x1\nret\n```\n\n```text\nlea eax,[ecx+0x1]\nret\n```\n\n```text\nn\n```\n\n```text\nfoo\n```\n\n```text\nfastcall\n```\n\n```text\ntest.cpp:\n```\n\n```text\ng++ -O3 -fomit-frame-pointer -c test.cpp\n```\n\n```text\nfoo1\n```\n\n```text\nfoo2\n```\n\n```text\nregister int x = 10;\n```\n\n```text\nfoo()\n```\n\n```text\nregister\n```\n\n```text\nx\n```\n\n```text\nfoo:\n    .frame  $sp,0,$ra\n    .mask   0x00000000,0\n    .fmask  0x00000000,0\n\n    addu    $2, $zero, $4\n    jr      $ra\n    nop\n```\n\n```text\nint foo( int n ) {\n   return ++n;\n}\n```\n\n```text\nlea edx, [eax+1]\n```\n\n```text\nfoo\n```\n\n========================================\n\nComments:\n- The compiler would optimize. Try `gcc -fverbose-asm -O2 -S yoursource.c` then look inside `yoursource.s`\n- -O disables stack frame setup on machines where it doesn't interfere with debugging - x86 isn't one of them, you need a seperate -fomit-frame-pointer to eliminate 'redundant' stack frame setup (which is actually useful for debugging, i.e. in stack frame unwinding)\n- yeah, I totally forgot about that. I'll fix it. But the difference remains.\n- A compiler that does link time optimizations can also recognize that a call can be turned into a fast call all on its own because it can see and fix all the call sites.\n- Which \"difference remains\"? The one matja pointed out was the *only* ineffiicency, wasn't it? And that was caused by a missing optimization flag. -1 for saying you can't assume the compiler will store intermediates in registers. You're right with more complex optimizations, of course, but for that one?\n- The difference would be that the compiler would do the equivalent of a fast call without the programmer having to use the non-standard **attribute**.\n- @jalf: did you read the updated post? I added the optimization flag quite a while before you posted that comment. There's obviously still one more instruction in the non-fastcall version. @Richard: I linked it together and it still produced a non-fast-call.\n- @Richard: admittedly my compiler doesn't have the new gcc link time optimizer.. I wonder if that will make a difference. But under more 'common' compilation options, there's a definitely a difference.\n- Yes, there is one instruction more. That is because it has to use the same calling convention as the caller. Otherwise actually *calling* the function is impossible. That extra instruction is obviously removed if the function is inlined (which it will typically be). But honestly, I think you've gotten way sidetracked. Read the actual question. It was not \"does my compiler ignore calling conventions in order to generate optimal code\". It was \"is my compiler able to store local variables in registers\". And a function parameter isn't exactly a local variable, which is why it's not in a register\n- Non-local variables, variables that have to be accessible to other functions, have to additional constraints, such as \"following the right calling convention\". That is not \"inefficiency\", and it is not a lack of optimization. It is creating a function that *works*.\n- @jalf: I'm not sure what you mean by 'creating a function that works'; surely a `fastcall` function works as well, so long as we tell the caller what convention it uses? But yes, now that you've pointed it out, I realize that the OP's question in his examples differed from the question he stated in words, and I should have addressed both issues.\n- @jalf: I think we have been arguing over a different idea of the question at hand. Sorry for the confusion.\n- Yes, again with a mips example: static int foo( int n ) { return n++; } int fee() { return foo(5); } gives: .text .align 2 .globl fee .ent fee fee: .frame $sp,0,$ra .mask 0x00000000,0 .fmask 0x00000000,0 addiu $2, $zero, 5 jr $ra nop .set macro .set reorder .end fee .size fee, .-fee","metadata":{"transformedAt":"2026-08-18T18:32:17.795Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":22,"totalLines":163,"estimatedTokens":1381}}412{"id":"stack-18713254","source":"stackoverflow","questionId":18713254,"title":"Is there a digest for different \"RnRS\" Scheme standards?","tags":["compiler-construction","lisp","scheme","r5rs","r6rs"],"text":"Title: Is there a digest for different \"RnRS\" Scheme standards?\nTags: compiler-construction, lisp, scheme, r5rs, r6rs\nSource: Stack Overflow\n\nQuestion:\nI need to choose a Scheme standard for an academic compiler implementation. It has to be simple enough, but should still be a powerful language. There are several (RRS–R7RS) standards of Scheme. Is there a public comparison of these reports for choosing? If no, which one would you recommend for a first compiler?\n\n========================================\n\nTop Answer:\nThe easies to start with is a R5RS subset and it would be not too hard to make it a `R7RS-small` later. The R5RS is not very big so it's almost a digest on it's own.\n\nYou can compare them by their language changes part of the different reports. R7RS-small even have a list both against R5RS and R6RS.\n\n========================================\n\nCode:\n```text\nR7RS-small\n```\n\n========================================\n\nComments:\n- \"If no, which one would you recommend for a first compiler?\" => the last, R7RS (the first part is achieved now). R6RS was a mistake, and R5RS is still nice.\n- @arbautjc, what would you say about R4RS, isn't it easier to start from it, and then go to R5RS?\n- @Necto: If you want to go towards R5RS anyway, it should be easier to start with growing subsets of R5RS rather than with R4RS. That way, you won't have to rewrite incompatible changes or implement R4RS features that have been removed.\n- How far are you planning to take this compiler? If it is just going to be a semester project or such, you will probably end up implementing the core of all these standards. So it would be best to pick a simple one and start with that. R5RS is going to be the simplest modern scheme (there are no libraries or `syntax-case` to worry about, although it does have `syntax-rules` which is not trivial to implement). The only reason to pick R4RS would be if you wanted to avoid `syntax-rules` macros entirely for some reason.","metadata":{"transformedAt":"2026-08-18T18:32:17.795Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":28,"estimatedTokens":492}}413{"id":"stack-705598","source":"stackoverflow","questionId":705598,"title":"More difficult to build: Emulator or compiler?","tags":["compiler-construction","comparison","emulation"],"text":"Title: More difficult to build: Emulator or compiler?\nTags: compiler-construction, comparison, emulation\nSource: Stack Overflow\n\nQuestion:\nGiven a proficient developer with 10-20 years of experience that has never built either a compiler or an emulator, which would be more of challenge?\n\nCould you compare the issues that would be road blocks for either.\n\nThanks.\n\n========================================\n\nTop Answer:\nI have written both and would say that other things being equal (complexity of language or instruction set), it is *way* easier to write an emulator, especially if you are trying to write an *interesting* emulator or compiler.\n\nThe reason is that with an emulator you are trying to simulate a low-level thing with another, similar low-level thing. It's not too bad. With a compiler, you may be trying to implement very high-level ideas (e.g., objects, first-class functions, managed memory, string scanning) with very low level tools (machine words and machine instructions). This task is just a lot harder.\n\nOf course, for gangs of fun, you can write an emulator that works by *dynamic binary translation*, which is the compilation of machine code for the emulated architecture into the machine code for the native architecture. This way you get to have all the fun of both---and yo produce really fast emulators like QEMU or the late lamented Digital FX!32.\n\n========================================\n\nComments:\n- You are not considering CPU JIT recompilers (used in emulators).\n- \"writing a FAST emulator is MUCH harder.\"\n- A full C++ compiler will be very hard to write, even for a single target!\n- @Colin: Definitely; I was trying to illustrate both ends of the spectrum, and find something that would be very, *very* hard!\n- Don't see a reason for a -1. +1.\n- Emulating an unknown platform requires knowing maths, encryption algos, network/disk protocols etc. I wouldn't say it requires less theory than a compiler.\n- Thank you for you detailed and thoughtful response. It looks like the general consensus is that an emulator would be more difficult.\n- No, it looks like the consensus is that emulators are easier.","metadata":{"transformedAt":"2026-08-18T18:32:17.795Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":31,"estimatedTokens":535}}414{"id":"stack-813657","source":"stackoverflow","questionId":813657,"title":"Can a language be turing complete but incomplete in other ways?","tags":["language-agnostic","compiler-construction","programming-languages","language-theory","turing-complete"],"text":"Title: Can a language be turing complete but incomplete in other ways?\nTags: language-agnostic, compiler-construction, programming-languages, language-theory, turing-complete\nSource: Stack Overflow\n\nQuestion:\nFor example, are there certain things when writing an operating system that cannot be accomplished in a turing complete language?\n\n========================================\n\nTop Answer:\nYes, you can have a language that does not allow you to manipulate the hardware directly. It would be hard to write an operating system using the Bourne shell, for example. But these limitations are less than you think. Operating systems have been written in Standard ML, Scheme, and even Haskell!\n\n========================================\n\nCode:\n```text\nfor(var i=0;i<10;i++){\n mov(\"ax\",i);\n int(0x21);\n}\n```\n\n========================================\n\nComments:\n- Hmm... turing machine is turing complete, right? and how does it allow to \"manipulate the hardware directly\"?\n- I mean \"manipulating the hardware directly\" is not a question of language completeness. Language is incomplete if, for example, it does not have goto, loop operators and subroutines, so you are unable to write cyclic program.\n- In bash you can write cat, ls, grep, ping, etc. So why can't you have commands for reading/writing data from/to hardware port. It will still be bash.\n- @Pavel: The question was, \"are there other interesting notions of completeness\". My answer was, \"yes, access to the MMU page tables and other hardware.\" cat, ls, grep, ping are written in C not bash. If you only have bash, and you have no Unix system calls, I don't think you can implement Unix system calls.\n- Interesting idea. Although I'm not sure if access to hardware should be counted as a property of a language. Most languages I know have hardware/os access implemented as external libraries written in another language (mostly c/c++) and can be run without those libraries. Look at most dynamic languages, java/.net etc.\n- My answer was \"access to the MMU page tables and other hardware\" is not a primary language concern and is not related to language completeness. It is incompleteness of something else. Of course to keep it simple you may say it is incompletenes of language, and you would be understood, but this kind of incompleteness is far different from the one Turing was thinking about. You write \"mov ax,bx\" and it's up to CPU to perform this operation. You write \"cat /etc/passwd\" and it's up to OS to perform this. It's environment/compiler concern. You write for(int i=0;i<10;i++){} - then it is language.\n- bash without system calls is useless. If you have system calls - you may have anything because bash is Turing-complete. If bash was not T-complete, for example did not allow to make loops, you can not do certain cyclic things even with system calls. Language needs a way to interact with environment, otherwise it is not T-complete - without any interactions you can not compute every Turing-computable function because you can not read parameters. Just don't mess incompleteness of ecosystem(lang,tools,compiler,environment,etc) with language incompleteness.\n- @Pavel: Right. Check the question. The question is about \"incompleteness of something else.\" The horse is dead; you can stop beating it...\n- Turing-completeness is pure language property. You can analyze language isolated and decide if it is T-complete or not. This sort of completeness if very different from the one you are talking about - hardware access, etc. Sure, you can say \"bash does not have hardware access and c++ does\" and you would be understood. And that's true in a context of modern IT industry. However, if you imagine runnig c++ program on computer that does not have cpu with registers, but have something else, then - c++ language still is T-complete, but there is not hardware access support.\n- If I read t right, question is about incompleteness of language and inability to accomplish things in T-complete language. And the answer is - in T-complete language you can do anything, you just need compiler. If there is no compiler - it's not a language incompleteness. And language is incomplete in other sense if it does not help doing OOP, functional programming and other useful things that don't affect T-completeness.\n- Ability to manipulate the hardware is a result of the mapping of that hardware into the memory. Putting it another way, if you put the right numbers in the right places, you can get the hardware (more or less) to do what you want. This is not a comentary of how you use the language to drop the number in the right places, only that you where able to get them there.\n- \"graphics capabilities, ability to spawn background processes, ability to persist state, and ability to connect to the network are all useful features\" - in most cases they are not language features at all, they are in libraries. Ability to make your class field protected or private, add attributes to method, etc - these are features that are useful, but not necessary for completeness.\n- Oh, guys, how do you think the libraries are written?\n- For some reason the link to Church-Turing isn't surviving formatting. en.wikipedia.org/wiki/Church–Turing_thesis\n- Fixed the link for you. For some reason, certain browsers don't properly escape URLs when you copy them...\n- Groovy, thanks. The text in the link looks right when I save it, so that was non-obvious.\n- Yeah, that happens a fair bit - the client-side Markdown preview engine is much more forgiving than the server-side rendering engine.\n- -1 -- there are formal languages that are not recursively enumerable.\n- So, Dave, I take it you're suggesting that there are things that are \"reasonable computations\" that can't be computed by a TM? Would you care to exhibit one?\n- I will donate one shiny dollar to the first person to write a functioning bootloader in brainfuck\n- NOTE: Despite that it was accepted, this answer is wrong. When writing an operating system, you need to be able to do things other than read input, do universal computation, and write output. You'll need to be able to interact directly with the hardware in order to manage a filesystem, say, or print a document to a printer. These are all things that an OS needs to be able to do that many Turing Complete languages just don't have provisions for.\n- @sblom don't make mathematical statements you don't understand. What you've just done is restate my second paragraph. Proof: you can certainly do those things in machine code. Any Turing-complete language can then be used to write an assembler to generate the needed machine code, or to insert that code in memory. Thus *with enough effort* any Turing-complete language can indeed interact directly with the hardware, which suffices to disprove your assertion.\n- \"You can imagine a programming language with no ability to read or write files\". Yes, c++ for example. fopen, fread, fclose are not part of language, they are in library. x86 assembler for example - no files at all. just hardware interruptions.\n- Any turing-complete language can operate your toaster, you just need compiler.\n- \"Just because a language can't operate my toaster doesn't mean it's not Turing-complete, but it does mean there are things it cannot do\" - if language is Turing-complete, there are no things it can not do. There are things you need better compiler and environment to do. Language can or can not do loops, classes, functions, attributes, templates, data structures. Compiler can or can not translate this language to CPU (or other environment, like JVM) instructions.\n- @Pavel: You need the toaster to be (software-)programmable in first place. Only in that case you can say that \"you just need a compiler\".\n- \"If language is Turing-complete, there are no things it can not do.\" That's not quite it. If a language is Turing-complete, there are no decidable problems it cannot solve. It cannot necessarily interact with the toaster, but it can emulate a toaster, mathematically speaking. It also cannot decide the halting problem, as no Turing machine can.","metadata":{"transformedAt":"2026-08-18T18:32:17.796Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":52,"estimatedTokens":2022}}415{"id":"stack-33708817","source":"stackoverflow","questionId":33708817,"title":"Parser errors - pattern for generating error handling automatically","tags":["parsing","compiler-construction","grammar"],"text":"Title: Parser errors - pattern for generating error handling automatically\nTags: parsing, compiler-construction, grammar\nSource: Stack Overflow\n\nQuestion:\nIs there any known way to implement good error handling for machine generated parsers? \nDoes a \"pattern\" or a known algorithm exist for this kind of problem? \n\nFor \"good\" I mean something which resembles results obtainable with hand crafted recursive descent parsers and modern compilers: \nParser does not stop at first error, can be made to emit \"meaningful\" errors and not just \"unrecognized token in line xyz\" one error at a time.\n\nIdeally this approach should be automated as well, not \n handcrafted. \n\nI am not searching for a library, I need an approach, which can be used in different platforms and ideally would be as language independent as possible.\n\n========================================\n\nTop Answer:\nI have a rather different perspective on this problem, which is that you shouldn't treat syntax errors as internal compiler errors. Any practical compiler is actually implementing **three** languages:\n\n- The language *L* that is the designated target language. Correct programs are members of this language.\n\n- The language *M* that consist of *L* plus all the errors that are recognized by the compiler. Members of *M* \\ *L* receive informative errors.\n\n- The language *Z* that compiler terminates normally on. This set should be the set of all possible input strings, but if the compiler crashes on some input, it's not. Members of *Z* \\ *M* receive generic messages about how the compiler failed, typically of the form \"parser failed at line x, char y\".\n\nYou can use automatic parser generator tools, as you are looking for, if you specify the language *M* in your parser instead of the language *L*. The trouble with this approach is that language designers always specify *L* and not *M*. I can't think of a single case where there's anything like a standard for *M*.\n\nThis isn't just abstract nonsense. There's a recent change to C++ that illustrates this distinction pretty well. It used to be that\n\n```\ntemplate class X;\ntemplate class Y;\nX> foo; // syntax in M\n```\n\nhad an error in line three because the characters \">>\" were the token for the right shift operator. That line had to be written\n\n```\nX > foo; // syntax in L\n```\n\nThe standard was changed not to require the extra space. The reason was that all major compilers had already written code to recognize this case in order to generate a meaningful error message. In other words, they found out that the *M* language was already implemented everywhere. Once the committee determined that, they transferred the *M*-syntax into the new version of *L*.\n\nWe would have better language design overall if designers considered the *M* language at the same time as they're working on the *L* language. Simply for their own sanity, they'd make some effort to minimize the size of the specification for *M*, which would be a good thing for everyone. Alas, the world is not there yet.\n\nThe upshot is that you need to design your own language *M*. That's the hard problem. Whether you use an automated tool for it is somewhat beside this point. It helps, but it doesn't get rid of the most time-consuming part.\n\n========================================\n\nCode:\n```text\nrule = e1 e2 e3 ~{ error(\"e[12] ok; e3 has failed\"); }\n         | ...\n\n  rule = (e1 e2 e3) ~{ error(\"one of e[123] has failed\"); }\n         | ...\n```\n\n```text\n~{}\n```\n\n```text\nvoid p {\n x = y ++\n     z = 0;\n<EOF>\n```\n\n```text\ntemplate< class T > class X;\ntemplate< class T > class Y;\nX<Y<int>> foo; // syntax in M\n```\n\n```text\nX<Y<int> > foo; // syntax in L\n```\n\n========================================\n\nComments:\n- This is probably not what you want to hear, but your better off hand writing the parser and lexer. It's not a particularly hard task (especially when compared with writing the semantics analyzer and code generator), and will produce the best results when it comes to error handling. But don't trust me, trust Walter Bright the author of the first native C++ compiler and inventor of the D programming language. he has an article on exactly this on Dr.Dobbs here. (error recovery is on page 2)\n- That is precisely what i *feared* to hear, that no practical automated approach is possibile. Please add this as an answer, even if I don't like the answer, I would up vote it as it is useful. From what I understand, with my limited experience, automated tools are useful in order to analyze machine generated syntax, on which very limited error handling is expected. E.g.: virtual machine decompiler, encoded messages, etc. While I am aware that all professional compilers are handwritten.\n- drdobbs.com is not showing any content. Here's an archived copy: web.archive.org/web/20170430054359/http://www.drdobbs.com/&hellip;\n- What I forgot to write and you asked specifically: Of course all those frameworks assume that you push the found errors to a global list. You have to print this list of max N parser errors at the end of your parse by yourself. All this is very straightforward, but you have to write this code by yourself.","metadata":{"transformedAt":"2026-08-18T18:32:17.796Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":88,"estimatedTokens":1285}}416{"id":"stack-2652060","source":"stackoverflow","questionId":2652060,"title":"Removing left recursion","tags":["compiler-construction","grammar"],"text":"Title: Removing left recursion\nTags: compiler-construction, grammar\nSource: Stack Overflow\n\nQuestion:\nThe following grammar has left recursion\n\n```\nE= E+T|T\nT= T*F|F\nF= a|b|c\n```\n\nHow to remove it? Is there any general procedure for it?\n\n========================================\n\nTop Answer:\nThe general procedure is found at Wikipedia, for example. I'll demonstrate for `E = E+T|T`:\n\n`E` is the left-recursive non-terminal.\n`+T` is the non-null sequence (alpha).\n`T` is the sequence which doesn't start with E (beta).\n\nWe create a new nonterminal for the \"rest\", ie. the non-null sequence. This handles the recursion.\n\n`E' = epsilon|+TE'`\n\nAnd we modify the original rule to use the new nonterminal to handle the recursion.\n\n`E = TE'`\n\n========================================\n\nCode:\n```text\nE= E+T|T\nT= T*F|F\nF= a|b|c\n```\n\n```text\nE = TE'\nE'= (e) | +TE'\nT = FT'\nT'= (e) | *FT'\nF = a | b | c\n\n// (e) is \"epsilon\" which is defined as the empty string\n```\n\n```text\n+\n```\n\n```text\n*\n```\n\n```text\na + b + c\n```\n\n```text\n(a + b) + c\n```\n\n```text\na + (b + c)\n```\n\n```text\nE= T+E|T\nT= F*T|F\nF= a|b|c\n```\n\n```text\nE = E+T|T\n```\n\n```text\nE\n```\n\n```text\n+T\n```\n\n```text\nT\n```\n\n```text\nE' = epsilon|+TE'\n```\n\n```text\nE = TE'\n```\n\n```text\nE = E+T\n  | T\nT = T*F\n  | F\nF = a|b|c\n```\n\n```text\nE= T ('+' T)*\nT= F ('*' F)*\nF= a|b|c\n```\n\n```text\nret = T1;\nwhile(set.more()) ret = A(ret, set.pop_front().T);\n```\n\n```text\nA(E,T)\n```\n\n========================================\n\nComments:\n- So you're actually saying that your proposed grammar in left-recursion free ? i am wondering because i came across it in a course in Formal Languages and it was stated left-recursive ?\n- So how to resolve that problem? (of reversing the associativity) I see many copies (direct of indirect) of this algorithm of removing left recursion, and they always write the warning about breaking associativity, but I hadn't seen yet any solution to that problem in any of such articles. Is there any solution? Or only the problem? And the other question is: Why does this work?\n- I ask because I haven't seen any reliable PROOF of that algorithm. There were \"proofs\" which showed that the language obtained from the transformed grammar is still the same, but for me there's an ERROR in such proofs: an assumption that if the syntax generates the same output, it's the same. Because it's NOT! It's a whole different language, because it has a different parse tree, so it's \"understood\" differently by the machine. The same difference as between interpreting \"Abstract Syntax Tree\" as \"Abstract (Syntax Tree)\" and \"(Abstract Syntax) Tree\" - two different things.\n- The same goes for left-recursive and right-recursive syntaxes. One is left-associative, the other is right-associative. And I don't know about any way to have left-recursive syntax with right-associativity. Do you know?\n- In math (which I consider as a superset of computer science), we are careful to define \"same,\" \"similar,\" \"equal,\" \"equivalent,\" \"isomorphic,\" etc. before we use the terms. Languages are *sets* of strings, which are finite ordered sequences of symbols from an alphabet. Set-equality is well-defined. We say two sets `A` and `B` are equal if `A` is a subset of `B` and `B` is a subset of `A`. And we can show that this is the case, apparently, with these algorithms. Thus two grammars which generate different parse trees can be rigorously shown to describe equal languages, in terms of the sets.\n- If you wish to say that: `A -> aA | @` is not the same as `A -> Aa | @` then by all means do so. You have the liberty to define your terms as you see fit. Give me a rigorous, precise definition of \"sameness,\" though, when it comes to grammars, though. I suspect your notion may be something along the lines of: \"Two grammars are structurally isomorphic if there is a bijection of parse trees for each element of the language. Two parse trees are isomorphic if their naturally-defined directed graphs are isomorpic.\" This is a beast to do anything with, but start doing proofs with something\n- like this and I'll be interested :)\n- This answer could be improved by explaining `e`. Without this, it's pretty opaque to those who aren't already familiar with parser theory. (Sure, you provided a link to a Wikipedia page, but said page begins with \"This article may be too technical for most readers to understand\", which is certainly the case for me).","metadata":{"transformedAt":"2026-08-18T18:32:17.796Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":18,"totalLines":135,"estimatedTokens":1098}}417{"id":"stack-13374121","source":"stackoverflow","questionId":13374121,"title":"Non-left-recursive PEG grammar for an \"expression\"","tags":["parsing","compiler-construction","programming-languages","lexer"],"text":"Title: Non-left-recursive PEG grammar for an \"expression\"\nTags: parsing, compiler-construction, programming-languages, lexer\nSource: Stack Overflow\n\nQuestion:\nIt's either a simple identifier (like `cow`) something surrounded by brackets (`(...)`) something that looks like a method call (`...(...)`) or something that looks like a member access (`thing.member`):\n\n```\ndef expr = identifier | \n \"(\" ~> expr expr It's given in Scala Parser Combinator syntax, but it should be pretty straightforward to understand. It's similar to how expressions end up looking in many programming languages (hence the name `expr`) However, as it stands, it is left-recursive and causes my nice PEG parser to explode. \n\nI have not succeeded in factoring out the left-recursion while still maintaining correctness for cases like `(cow.head).moo(dog.run(fast))`. How can I refactor this, or would I need to shift to some parser-generator that can tolerate left recursive grammars?\n\n========================================\n\nCode:\n```text\ndef expr = identifier | \n           \"(\" ~> expr <~ \")\" | \n           expr ~ (\"(\" ~> expr <~ \")\") | \n           expr ~ \".\" ~ identifier\n```\n\n```text\ncow\n```\n\n```text\n(...)\n```\n\n```text\n...(...)\n```\n\n```text\nthing.member\n```\n\n```text\nexpr\n```\n\n```text\n(cow.head).moo(dog.run(fast))\n```\n\n```scala\ndef expr              = method_call\ndef method_call       = member_access ~ ( \"(\" ~> expr <~ \")\" ).*\ndef member_access     = atomic_expression ~ ( \".\" ~> identifier).*\ndef atomic_expression = identifier |\n                        \"(\" ~> expr  <~ \")\"\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.796Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":8,"totalLines":54,"estimatedTokens":391}}418{"id":"stack-5768400","source":"stackoverflow","questionId":5768400,"title":"What are synthesized attributes in the context of creating an abstract syntax tree?","tags":["parsing","compiler-construction","abstract-syntax-tree"],"text":"Title: What are synthesized attributes in the context of creating an abstract syntax tree?\nTags: parsing, compiler-construction, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nCompilers parse source code and build an abstract syntax tree. The functions used to construct an abstract syntax tree return pointers which constitute **synthesized attributes**. What are they and how do they differ from **inherited attributes**.?\n\nedit: I don't know if this can help, but I originally heard of these terms in a French context: Attributs synthétisés, attributs hérités.\n\n========================================\n\nComments:\n- Thank you for the examples. This is a key thing that helped me understand it. Something both my professors' slides and wikipedia left out.","metadata":{"transformedAt":"2026-08-18T18:32:17.796Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":13,"estimatedTokens":192}}419{"id":"stack-8292305","source":"stackoverflow","questionId":8292305,"title":"Is \"for(;;)\" idiom for infinite loop correctly attributed to PDP-11 C compiler?","tags":["c++","c","compiler-construction","history","pdp-11"],"text":"Title: Is \"for(;;)\" idiom for infinite loop correctly attributed to PDP-11 C compiler?\nTags: c++, c, compiler-construction, history, pdp-11\nSource: Stack Overflow\n\nQuestion:\nRecently I found this article that claims that the idea to prefer `for(;;)` over `while(1)` for infinite loop came because the C compiler originally available on PDP-11 generated an extra machine instruction for `while(1)`.\n\nBtw now even Visual C++ warnings tend to favor the former.\n\nHow realistic is such attribution of `for(;;)` idiom?\n\n========================================\n\nTop Answer:\nThe \"for(;;)\" idiom is explicitly mentioned in the original K&R. That's attribution enough for me :)\n\n========================================\n\nCode:\n```text\nfor(;;)\n```\n\n```text\nwhile(1)\n```\n\n```text\nwhile(1)\n```\n\n```text\nfor(;;)\n```\n\n```text\n$ cat>a.c\nmain(){\n while(1);\n}\n$ cat>b.c\nmain(){\n for(;;);\n}\n$ cc -S a.c\n$ cc -S b.c\n```\n\n```text\n.globl  _main\n.text\n_main:\n~~main:\njsr     r5,csv\njbr     L1\nL2:L4:tst       $1\njeq     L5\njbr     L4\nL5:L3:jmp       cret\nL1:jbr  L2\n.globl\n.data\n```\n\n```text\n.globl  _main\n.text\n_main:\n~~main:\njsr     r5,csv\njbr     L1\nL2:L4:jbr       L4\nL5:L3:jmp       cret\nL1:jbr  L2\n.globl\n.data\n```\n\n```text\n.globl  _main\n.text\n_main:\n~~main:\njsr     r5,csv\nL4:jbr  L4\n.globl\n.data\n```\n\n```text\ncc\n```\n\n```text\na.c\n```\n\n```text\nwhile\n```\n\n```text\nb.c\n```\n\n```text\nfor\n```\n\n```text\nfor(;;)\n```\n\n```text\n-O\n```\n\n```text\nprintf(\"Hello\");\n```\n\n```text\nfor(;;)\n```\n\n```text\nwhile(1)\n```\n\n```text\nfor(;;)\n```\n\n```text\nwhile(true)\n```\n\n```text\nfor(;;)\n```\n\n```text\nwhile(true)\n```\n\n```text\ntrue||a\n```\n\n```text\nfor(;;)\n```\n\n```text\nwhile(true)\n```\n\n========================================\n\nComments:\n- There is no such thing as `true` in C...\n- Poor article. He sets out to prove that exception handling doesn't cost anything and then tests that assertion with code that doesn't throw any exceptions. Borland have a far better paper on the same topic.\n- @FredOverflow: Incorrect. `true` is a macro that is defined in `stdbool.h` (C99 standard, section 7.16)\n- @JeremyP: more accurate would have been to say, \"at the time you're talking about, there was no such thing as `true` in C\".\n- Ok guys, we all know the usual `for(;;)` vs `while(1)` routine. But now I am genuinely curious if anyone can come up with a good hystorical quote for the PDP-11 factoid.\n- @EJP: The point of the article is that adding try/catch blocks has essentially zero cost for those cases where no exception is thrown, and this is made quite clear in the article (e.g. \"catch code (never executed)\", \"A decision was made [by Microsoft] to implement an exception mechanism that imposes as close to zero as possible cost during normal execution, but is in fact fairly expensive when an exception is actually thrown.\"). A program that throws exceptions often enough to be worried about the cost of dispatching those exceptions is using exceptions the wrong way.\n- Keep in mind that K&R wrote the PDP-11 compiler, which was the working prototype that led to the book…\n- It explicitly mentions `for(;;)`, but not the PDP-11 or anything about generated code. It just says that if the condition is omitted, it's always assumed to be true.\n- @Potatoswatter: R wrote the PDP-11 compiler, K didn't. He was busy with other bits of Unix at the time.\n- -1. The appearance of `for(;;)` in K&R1 does not prove the idiom's origin in the PDP-11 machine language in any way.\n- Actually, it's not. Even though `while(1)` has more characters, `for(;;)` requires you to press the same button twice which can really slow typing down.\n- As well as being shorter to type, it avoids all-important wear and tear on the `e` and `l` keys, shifting it over to `;`. This prolongs the useful life of the keyboard.\n- @Steve: Hw inteesting! I need t peseve all the lettes I have let.\n- By the time you've read this whole thread trying to decide which one to use, you'll probably have wasted more time than you ever would have gained by typing the faster one for the rest of your life.\n- You can be extra cool and add `#define ever (;;)`. Then you can write literally `for ever`.\n- @rodrigo: +1 for irony. But I don't suggest it. If I see 'for ever' I wonder WTF 'ever' may hide (especially if it is hidden in 50 depth include files). 'for(;;)' is just what the language is. More \"secure\" to anyone.\n- @rodrigo: for that matter, you can `#define universe_exists (true)`.\n- I was joking, of course. Any sane C programmer would write `#define EVER (;;)`, but `for EVER` doesn't look so nice [joking again].\n- Just `#define forever` and let it be `for (;;)` on odd weeks and `while (1)` on even.\n- @rodrigo brilliant! im surprised nobody wrote a language with this as a keyword.\n- If you're programming a machine that ideally never stops, and C is used to program many such machines, then an infinite loop is the perfect model.\n- What you write is true, but it does not address the history at all, which is what is this question about.\n- @Suma: But the question as strictly specified wasn't about programming, it was about the history of C.\n- @Potatoswatter: there is no such machine. There is always the possibility that the machine needs to be taken offline for maintenance or to be thrown away.\n- Going offline for maintenance doesn't necessarily mean exiting the main loop. Such a machine already has a catch-all for fault conditions, which is to reboot automatically if an internal watchdog timer isn't reset. The best way to install an update is to activate that well-tested code path, using the same well-tested command dispatch system as does everything else in the main loop.\n- Also, since this is C++, you can also exit a loop by an exception, which is more elegant than adding a global `is_exiting` condition and plays well with what I just mentioned.\n- @Potatoswatter: Exiting a loop by an exception is a horrible idea unless an exceptional condition has actually occurred. Normal shutdown is not an exceptional condition. It is far better to declare what your loop exit condition is in the place where it is most easily found - i.e. in the loop condition - than to hide it somewhere in the loop's body.\n- For a machine which is programmed to never stop, stopping to install an update is *always* an exceptional condition. Returning from `main` will be defined to cause an exception and a hard reboot just like any other reasonable alternative. Anyway… I guess you have to see it to believe it. It's a big world out there.\n- the HOWEVER part is religion, not programming. Loops can also be exited by break. Programmers knows that, hence for(;;) does not mean \"infinite\", just \"not yet known at loop entrance, and not related to an expression evaluation of a state variable\".\n- I imagine this has been voted down so much because it does not address the question. However, \"while(true) will be more understandable to inexperienced programmers\" is a good and useful point.\n- If you don't want to guess, you can read Dennis Ritchie's own description of what happened in The Development of the C Language.","metadata":{"transformedAt":"2026-08-18T18:32:17.796Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":25,"totalLines":191,"estimatedTokens":1761}}420{"id":"stack-733082","source":"stackoverflow","questionId":733082,"title":"Questions for compiling to LLVM","tags":["c","compiler-construction","llvm","backend"],"text":"Title: Questions for compiling to LLVM\nTags: c, compiler-construction, llvm, backend\nSource: Stack Overflow\n\nQuestion:\nI've been playing around with LLVM hoping to learn how to use it.\n\nHowever, my mind is boggled by the level of complexity of the interface.\n\nTake for example their Fibonacci function\n\n```\nint fib(int x) {\n if(xTo get this to output LLVM IR, it takes **61 lines of code**!!!\n\nThey also include BrainFuck which is known for having the smallest compiler (200 bytes).\nUnfortunately, with LLVM, it is over **600 lines** (18 kb).\n\nIs this the norm for compiler backends? \nSo far it seems like it would be far easier to do an assembly or C backend.\n\n========================================\n\nTop Answer:\nDoesn't LLVM then optimise the IR depending on the specific architecture implemented in the back-end? The IR code is not directly translated 1:1 into the final binary. As far as I understand it, that's how it works. However, I have only started to play around with the back-end (I'm porting it over to a custom processor).\n\n========================================\n\nCode:\n```text\nint fib(int x) {\n    if(x<=2) \n        return 1;\n    return fib(x-1) + fib(x-2);\n   }\n```\n\n========================================\n\nComments:\n- I'm not talking about the end size. I'm talking about the code needed to MAKE the IR.\n- What about compiling to LLVM IR? Do you know if it is stable enough?\n- LLVM IR works, but it has many of the same problems as compiling to C. If you are using C++ for the compiler, using the libraries is a lot easier.\n- Thanks for the suggestion Jon. Unfortunately programming in OCaml is still difficult for me to get the hang of since I am mostly a procedural programmer.\n- Even if you include the time taken to learn OCaml, it will still be faster to write a production-quality compiler in OCaml rather than using C++. I cannot recommend OCaml strongly enough for this purpose.\n- OCaml is basically the perfect language for writing compilers. Take a look at F# as well, I see it as a better version of OCaml.","metadata":{"transformedAt":"2026-08-18T18:32:17.796Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":46,"estimatedTokens":510}}421{"id":"stack-13777112","source":"stackoverflow","questionId":13777112,"title":"IL & stack implementation in .net?","tags":["c#",".net","compiler-construction","il"],"text":"Title: IL & stack implementation in .net?\nTags: c#, .net, compiler-construction, il\nSource: Stack Overflow\n\nQuestion:\nI wrote a simple program to examine how IL works : \n\n```\nvoid Main()\n{\n\n int a=5;\n int b=6;\n if (aThe IL : \n\n```\nIL_0000: ldc.i4.5 \nIL_0001: stloc.0 \nIL_0002: ldc.i4.6 \nIL_0003: stloc.1 \nIL_0004: ldloc.0 \nIL_0005: ldloc.1 \nIL_0006: bge.s IL_0012\nIL_0008: ldstr \"333\"\nIL_000D: call System.Console.Write\nIL_0012: call System.Console.ReadLine\n```\n\n*I'm trying to understand the implemented efficiency :* \n\nat line #1 (IL code) it pushes the value 5 onto the stack ( 4 bytes which is int32)\n\nat line #2 (IL code) it POPs from the stack into a local variable.\n\n*same goes for the next 2 lines.*\n\nand then , it loads those local variables onto the stack and *THEN* it evaluate `bge.s`.\n\n**Question #1**\n\nWhy does he loads the local variables to the stack ? the values *has already been* in the stack. but he poped them in order to put them in a local variables . isn't it a waste ? \n\nI mean , why the code couldn't be something like : \n\n```\nIL_0000: ldc.i4.5\nIL_0001: ldc.i4.6 \nIL_0002: bge.s IL_0004\nIL_0003: ldstr \"333\"\nIL_0004: call System.Console.Write\nIL_0005: call System.Console.ReadLine\n```\n\nmy sample of code is just 5 lines of code. what about 50,000,000 lines of code ? there will be plenty of extra code emitted by IL\n\n**Question #2**\n\nLooking at the code address : \n\n- where is the IL_0009 address ? isnt it supposed to be sequential ?\n\n*p.s. Im with Optimize flag on + release mode*\n\n========================================\n\nTop Answer:\nI can answer the second question easily. The instructions are variable-length. For example the `ldstr \"333\"` consists of the opcode for `ldstr` (at address `8`) followed by the data representing the string (a reference to the string in the user string table).\n\nSimilarly with the `call` statements following that - you need the `call` opcode itself plus the information on the functions to call.\n\nThe reason the instructions for pushing small values like 4 or 6 onto the stack don't have extra data is because the values are encoded into the opcode itself.\n\nSee here for the instructions and encodings.\n\nAs to the first question, you may want to look at this blog entry by Eric Lippert, one of the C# developers, which states:\n\n The /optimize flag does not change a huge amount of our emitting and generation logic. We try to always generate straightforward, verifiable code and then rely upon the jitter to do the heavy lifting of optimizations when it generates the real machine code.\n\n========================================\n\nCode:\n```text\nvoid Main()\n{\n\n int a=5;\n int b=6;\n if (a<b) Console.Write(\"333\");\n Console.ReadLine();\n}\n```\n\n```text\nIL_0000:  ldc.i4.5    \nIL_0001:  stloc.0     \nIL_0002:  ldc.i4.6    \nIL_0003:  stloc.1     \nIL_0004:  ldloc.0     \nIL_0005:  ldloc.1     \nIL_0006:  bge.s       IL_0012\nIL_0008:  ldstr       \"333\"\nIL_000D:  call        System.Console.Write\nIL_0012:  call        System.Console.ReadLine\n```\n\n```text\nIL_0000:  ldc.i4.5\nIL_0001:  ldc.i4.6    \nIL_0002:  bge.s       IL_0004\nIL_0003:  ldstr       \"333\"\nIL_0004:  call        System.Console.Write\nIL_0005:  call        System.Console.ReadLine\n```\n\n```text\nbge.s\n```\n\n```text\nldstr\n```\n\n```text\nldstr\n```\n\n```text\nldstr\n```\n\n```text\n72\n```\n\n```text\n72\n```\n\n```text\nldstr \"333\"\n```\n\n```text\nldstr\n```\n\n```text\n8\n```\n\n```text\ncall\n```\n\n```text\ncall\n```\n\n```text\nldc.i4.5\nstloc.0\n```\n\n```text\nldc.i4.6\nstloc.1\n```\n\n```text\nint a=5;\n```\n\n```text\nint b=6;\n```\n\n========================================\n\nComments:\n- Is this debug or optimized compiler ?\n- @spender release mode+optimize on\n- For question 1, it is compiling your code quite literally, but I think the real question is why isn't the optimizer getting rid of your local variables 'a' and 'b' and just compiling it to `if (5 < 6) ...` Or for that matter, why isn't it just getting rid of the conditional all together since it's always `true`? Maybe the optimizer just isn't that great.\n- I disabled the optimize flag , still , same result.\n- Well, yes, it *would* give the same result if what Eric said is true, and I have no reason to doubt him given he's one of the main C# bods.\n- BTW, your assumption is wrong. `ldstr \"333\"` actually consists of one byte for the opcode and then 4 bytes of a reference to the user strings table, which actually contains the `333` string.\n- Thanks, @svick, I wasn't certain of the representation - I've updated the answer based on your information.\n- What do you mean *there is no point in reasoning* . I want to see and learn how things work. I do believe I have a lot of reasons to learn it if I want to be smarter - so next time i will be able to understand things better.\n- @RoyiNamir, I mean that you cannot reason about your code efficiency based on its stack representation. It is still a highly abstract intermediate language, not reflecting the real native code.\n- @SK has a point there. IL is never meant to be executed (with the exception micro framework I guess). It's just an intermediate code to produce a native binary. As long as all inefficiencies are eliminated when generating native code, optimizing IL is meaningless (aka wasted effort).\n- @SK-logic I get your point , but why to write those commands *from the beginning* ? **why did the IL supply extra code to JIT to handle from the beginning**? Do you get my point ?\n- @RoyiNamir, since it's going to be optimised by JIT anyway, what's the point in pre-optimising. There's a downside to doing that, since it makes the IL generation a more complex process but I'm not sure I see much upside.\n- @RoyiNamir, think of the stack semantics of the IL as of a simple way to serialise expression trees. Just a high level intermediate representation, in a flattened form. Those expression trees are later deserialised back into tree form, re-compiled into three address form and then all the variable access is unified with the virtual register access and an SSA transform is applied. It is a relatively cheap sequence of transforms, which justifies keeping a dense, easy-to-generate \"stack\" representation.\n- @svick, of course there might be interpreting implementations, but the original design implied compilation only. Therefore there are no rich stack instructions (you won't find any swap instructions and alike), therefore severe limitations on CFG and such.\n- Old question, but has interesting implications for .NET being run through a WASM interpreter without JIT (i.e. Blazor). So the statement \"IL is never meant to be executed\" is no longer true. One has to wonder if there are performance benefits to be gained if one no longer assumes JIT will take care of everything.\n- @PeterMoore, in such a case, IL is just a very poor choice of an immediate execution VM, there is not much you can do to make it efficient. At least some degree of precompilation is still required - and there are many options. E.g., you can quickly turn it to a 3-address intermediate code that is faster to interpret (see Lua for an inspiration).\n- @SK-logic very interesting. So are you saying that the Mono WASM interpreter is just a fundamentally flawed approach then? Because I'm almost certain it's not JITting anything. And it wouldn't surprise me, because it really is slow as a mule and the AOT situation (which translates IL to C++ and then compiles) improves things but not necessarily to where they need to be.\n- @PeterMoore if performance is important, I'd say yes, it's a flawed approach, and an intermediate translation step could have improved performance quite a bit. Sadly, WASM is very limited, you cannot do all the usual VM performance tricks such as direct threaded code, but at least an interpreter of a 3-address VM can perform better than a deliberately un-optimised stack VM such as IL. It was designed for an ease of decompilation and further translation into another representations, not for an immediate execution.\n- why did the IL supply extra code to JIT to handle at first place ?\n- Because writing code that makes this optimization in the C# compiler is completely useless work. The CIL compiler already does that well, why should the C# compiler try to do the same thing?\n- @RoyiNamir, because it is easier to compile it this way, and there is no point in optimising out this \"extra code\", because JIT will do it later. Most of the .NET languages compiler frontends are pretty straightforward.\n- guys , maybe I don't see the whole picture. but my sample of code is just 5 lines of code. what about 50,000,000 lines of code ? there will be plenty of extra code emitted by IL to the jit.\n- @RoyiNamir And why exactly would this “extra code” be any problem? Why do you care about that? What do you think the C# developers should focus on: creating new awesome features like `async`-`await` or making pointless optimizations?\n- @svick cause if somene had told **me** : I wrote a meachanism to calc `a+b` , but in the middle I calc `a+b` and do `e=>f` and `j=>k` but in the end it will do a+b. - **I** will be telling him : *hey so why did you do e=>f and j=>k from the first place*?\n- p.s. sorry to be annoying , but I'd really love to understand it. ( and not by : \"why do you care about it\" ). :-)\n- @RoyiNamir, why are you calling it \"extra code\"? It is a direct, straightforward translation from the source code. It is an abstract, intermediate representation, and it does not have an operational semantics itself. There are no `e=>f` and `j=>k` operations in reality, they're all abstract. Have you ever seen an LLVM IR emitted directly by Clang frontend? It's full of \"extra\" code. Same for the GCC - its first level of an intermediate code is extremely verbose and not \"efficient\" at all. Lower level optimisations would take care of all the cruft.\n- @SK-logic Am I that completly misunderstood ? `:-)` *I guess the problem is with me ...* Ok. you say it is extremely verbose. I agree. but someone will eventually have to analyze this extremely verbose code - **which could have been less verbose !!!**\n- @RoyiNamir, yes, you can optimise it in the frontend - but then you will have to optimise it in a backend too, after all the transformations. Your frontend will be much more complicated, with absolutely no advantage over an unoptimised one. \"Stack\" representation is great for generating code, but it is not suitable for analysis - SSA is much better, but then it's more complicated to generate such a code straight from the source. I had only one case in my practice when I had to do a kind of register allocation in IL level - a generated pattern matching code was too big for a JIT.\n- @RoyiNamir It seems you think that not producing what you call “extra code” would be simpler than producing it. But it's the other way around: to remove the “extra code”, the C# compiler would have to be made more complicated for no benefit.\n- It would probably help understand the reasoning if you think of the C# 'compiler' more as a a WYSIWYG for IL. The IL is what is 'compiled' to machine language, and anyone is certainly free to code in 'native' IL, and in fact there are times where that is the appropriate thing to do, perhaps because profiling shows that certain optimizations 'need' to be made that the JIT compiler wasn't able to perform. However, by first targeting IL through the WYSISYG (eg, any mix of C#, VB, Managed C, etc), the code is often the most portable, and the most retargetable.\n- Actually, I don't think the C# compiler is *that* simple. For example, if you have `int a=5;` and then never use `a`, it does not produce any IL code for that statement.\n- I made a simplification based on the knowledge I thought the OP would have. The compiler has a symbol table that probably keeps track of if the variable is used or not but not where in the code it is used (where in the code in a C# way, it has to keep track of where in respect to the IL).","metadata":{"transformedAt":"2026-08-18T18:32:17.796Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":18,"totalLines":211,"estimatedTokens":2974}}422{"id":"stack-267351","source":"stackoverflow","questionId":267351,"title":"Convert integers to roman numerals using a syntax-directed translation scheme?","tags":["compiler-construction","roman-numerals","translation-scheme"],"text":"Title: Convert integers to roman numerals using a syntax-directed translation scheme?\nTags: compiler-construction, roman-numerals, translation-scheme\nSource: Stack Overflow\n\nQuestion:\nThe Dragon Book includes an exercise on converting integers to roman numerals using a syntax-directed translation scheme.\n\nHow can this be completed?\n\n========================================\n\nTop Answer:\nNext is grammar to represent syntax directed translation from number in format 1xxx into roman numerals.\n\n*number = OneThousand digit3 digit2 digit1 | nzdigit3 digit2 digit1 | nzdigit2 digit1 | nzdigit1*\n\n OneThousand -> 1 {print('M')}\n\n \n digit3 -> 0 digit3 -> nzdigit3\n\n \n nzdigit3 -> 1 print('C') nzdigit3 -> 2 print('CC') nzdigit3 -> 3\n print('CCC') nzdigit3 -> 4 print('CCCC') nzdigit3 -> 5 print('D')\n nzdigit3 -> 6 print('DC') nzdigit3 -> 7 print('DCC') nzdigit3 -> 8\n print('DCCC') nzdigit3 -> 9 print('DCCCc')\n\nIn similar manner write definition for digits in 2 and 1 position and you will have needed translation.\n\n========================================\n\nCode:\n```text\n0 -> ''\n1 -> 'I'\n2 -> 'II'\n3 -> 'III'\n4 -> 'IV'\n...\n9 -> 'IX'\n```\n\n```text\n0 -> ''\n1 -> 'X'\n2 -> 'XX'\n...\n9 -> 'XC'\n```\n\n```text\n$roman = array(\n  [0] = array( 1=>\"I\", 5=>\"V\", 10=>\"X\" ),\n  [1] = array( 1=>\"X\", 5=>\"L\", 10=>\"C\" ),\n  [2] = array( 1=>\"C\", 5=>\"D\", 10=>\"M\" ),\n  [3] = array( 1=>\"M\", 5=>\"^V\", 10=>\"^X\" ),\n);\n```\n\n```text\n1 => $roman[$level][1]\n2 => $roman[$level][1].$roman[$level][1]\n3 => $roman[$level][1].$roman[$level][1].$roman[$level][1]\n4 => $roman[$level][1].$roman[$level][5]\n5 => $roman[$level][5]\n6 => $roman[$level][5].$roman[$level][1]\n7 => $roman[$level][5].$roman[$level][1].$roman[$level][1]\n8 => $roman[$level][5].$roman[$level][1].$roman[$level][1].$roman[$level][1]\n9 => $roman[$level][1].$roman[$level][10]\n```\n\n```text\n5 => $roman[0][5] = \"V\"\n4 => $roman[1][1].$roman[1][5] = \"XL\"\n9 => $roman[2][1].$roman[2][10] = \"CM\"\n1 => $roman[3][1] = \"M\"\n```\n\n========================================\n\nComments:\n- looks like a homework question, smells like a homework question... ;-)\n- Yep, I know... I wish I could prove I'm not cheating. It IS actually a homework question, for CS students... Just, not for me, I'm just reading the book on my own, and have no teacher (or knowledgeable enough friend) to go ask.\n- This means that making a context-free grammar that would then allow me to convert using a sytanx-directed translation scheme, I need to create 10 rules for each \"column\". (so, about 34 rules to be able to get to 3999) Am I correct?\n- I actually thought about something like this, I was kind of expecting there to be a more elegant method... Is there?\n- Yes, that means 10 rules per column. I guess you could write a single function that works for each of the columns, and takes the letters as parameters... So, for 219, you would output f(2,'C', 'D', 'M') + f(1. 'X', 'L', 'C') + f(9, 'I', 'V', 'X') Doesn't 'feel' context-free though.\n- This is wrong answer, because no grammar here. No function at all must be present. Translation must give the result during tree traversal and its not possible do determine how deep in the tree you are.","metadata":{"transformedAt":"2026-08-18T18:32:17.797Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":87,"estimatedTokens":787}}423{"id":"stack-12886628","source":"stackoverflow","questionId":12886628,"title":"Generating JVM bytecode for a unary not expression","tags":["java","compiler-construction","jvm","javac","bytecode"],"text":"Title: Generating JVM bytecode for a unary not expression\nTags: java, compiler-construction, jvm, javac, bytecode\nSource: Stack Overflow\n\nQuestion:\nLet's say you're writing a Java (or subset-of-Java) compiler and you want to generate bytecode for a unary not expression, `!E`. You're past type checking so you know `E` has type `boolean`, i.e. it will push a `1` or a `0` on to the operand stack.\n\nOne way to do it is something like (in Jasmin syntax):\n\n```\nE\nifeq truelabel\niconst_0\ngoto stoplabel\ntruelabel:\niconst_1\nstoplabel:\n```\n\ni.e. if there's a 0 on the stack push 1, else push 0. Another way to do it, taking advantage of the fact that a `boolean` is just an `int` with value `1` or `0`, is to say `!E = (E + 1) % 2` and generate\n\n```\nE\niconst_1\niadd\niconst_2\nirem\n```\n\nIs there an advantage to using one over the other? Or something else entirely?\n\n========================================\n\nTop Answer:\nI once tried to write a Java decompiler, so I used to know what code javac generated. As I recall, javac 1.0.x used `!E = E ? false : true` while javac 1.1 used `!E = E ^ 1` (bitwise XOR).\n\n========================================\n\nCode:\n```text\nE\nifeq truelabel\niconst_0\ngoto stoplabel\ntruelabel:\niconst_1\nstoplabel:\n```\n\n```text\nE\niconst_1\niadd\niconst_2\nirem\n```\n\n```text\n!E\n```\n\n```text\nE\n```\n\n```text\nboolean\n```\n\n```text\n1\n```\n\n```text\n0\n```\n\n```text\nboolean\n```\n\n```text\nint\n```\n\n```text\n1\n```\n\n```text\n0\n```\n\n```text\n!E = (E + 1) % 2\n```\n\n```text\ntrue == 1\n```\n\n```text\nfalse == 0\ntrue != 0\n```\n\n```text\n!E = E ? false : true\n```\n\n```text\n!E = E ^ 1\n```\n\n```text\nE ^ 1\n```\n\n========================================\n\nComments:\n- I hadn't thought of `^ 1`; that's definitely better than the second one. Do you know the rationale behind switching from the former to the latter, or why `^ 1` wasn't used in the first place?\n- Actually I just tried with `javac 1.6.0_26` and it generated the first one (except `ifne` instead of `ifeq`), so I guess they switched back. I guess I'm still wondering about the benefits of one vs. the other.\n- This is just a wild guess but perhaps they moved these sorts of micro-optimisations to the JIT, which is probably where they belong.\n- ^1 is definetly *much* less computationally intensive than div/rem and conditional branches are also somewhat costly (since they can cause branch misprediction penalties with most current CPU architectures). The bigger question is if its valid to assume the int serving to implement the boolean is strictly constricted to the values 0/1.\n- I believe in Java, it is. In any case, you could just do `(E & 1) ^ 1` if you didn't know.\n- (&1) ^ 1 converts 0x2 to 0x1 (for example). true == !true isn't really what one expects. I don't know of any simple expression to get the logical OR of all 32 bits of an int to collapse into a single bit. Either booleans are *strictly* constrainted, or the ^-method can't be used.\n- The JVM spec says: \"The Java virtual machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java virtual machine type int, the compilers must use the same encoding.\" docs.oracle.com/javase/specs/jvms/se7/html/&hellip;\n- @isbadawi That describes how the JVM encodes *arrays* of booleans and how Java compilers must encode booleans as ints *when and if they do that at all.* It isn't immediately relevant to the question.\n- @EJP It seems kind of ambiguous to me. The first sentence is about boolean arrays, but the second is about boolean values. I'm probably wrong though.\n- @isdabawi I find the wording in the JVM spec very confusing. First, it makes a comment about how the Oracle VM *does* implement boolean[] as byte[] (but I regard this comment *not part of the spec*). Then, the last two sentences talk about what a *java compiler* is supposed to do when implementing boolean[] as int[] - again this doesn't really say how the VM must implement boolean. In fact I see nothing that says the VM has to implement it in a certain way, it only gives constraints how it will interpret certain constructs that a compiler may generate for boolean[].\n- I doesn’t look confusing to me. The sentence “*The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false*” clearly addresses the JVM. So it implies that even if a particular implementation uses byte arrays under the hood, this *must not* show through, i.e. reading from a boolean array must produce either zero or one, nothing else. Interestingly, the recent version I experimented with, does *not* implement a “nonzero means true” rule, but rather truncates all but the lowest bit when using an int value other than zero or one as a boolean.","metadata":{"transformedAt":"2026-08-18T18:32:17.797Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":132,"estimatedTokens":1193}}424{"id":"stack-9360027","source":"stackoverflow","questionId":9360027,"title":"Data structure for storing variables in an interpreted language","tags":["memory-management","data-structures","global-variables","language-design","compiler-construction"],"text":"Title: Data structure for storing variables in an interpreted language\nTags: memory-management, data-structures, global-variables, language-design, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am designing my own experimental scripting language for the purpose of embedding it in my bigger application.\n\nAlmost everything I wanted to do was programmed smoothly, but the \"simple\" act of storing variables in memory appeared to be the hardest part here. I don't know how to store them to allow all type checking, global variables and special flags on them. First look at a sample code:\n\n```\na = 1\nb = 2\n\nsomeFunction()\n print(a) --> This should read the global variable and print `1`\n a = 3 --> Now `a` should become a local variable of this function\n and the global `a` remain unchanged\n x = 4 --> `x` should always be local of this function\nend\n```\n\nI call the \"locality\" of variables their `level`s so variables in nested blocks have a higher level. In the above code, `a` and `b` are level 1 variables. Local variables of someFunction will have level 2. The first line of the function should read the global variable `a` (level 1) but the second line should create a variable again called `a` but with level 2 that shadows the global `a` from that point onwards. The third line should create the variable `x` with level 2. How to store and keep track of all these in memory?\n\n**What I tried so far:**\n\nMethod 1: Storing maps of `variable=>value` in array of levels:\n\n```\nvariables\n{\n level=1 //global variables\n {\n a => 1,\n b => 2\n },\n level=2 //function variables\n {\n a => 3,\n x => 4\n }\n}\n```\n\nBut that will make variable look-up really slow since one has to search all the levels for a given variable.\n\nMethod 2: Storing the (variable, level) pairs as keys of a map:\n\n```\nvariables\n{\n (a, 1) => 1, //global\n (b, 1) => 2, //global\n (a, 2) => 3, //function\n (x, 2) => 3 //function\n}\n```\n\nThis has the same problem as before since we have to try the pair (variable, level) with all possible levels for a given variable.\n\nWhat method should I use for optimal memory usage and fastest access time?\n\n**Additional notes:**\n\nI know about how variables are managed on stack and heap on other \"real\" languages, but I find it tricky to do this on an interpreted language. \"This mustn't be how Lua and Python do that,\" I always think. Correct me if I'm wrong. I'm trying to store the variable in maps and internal C++ structures.\n\nAnd finally, this is how I represent a variable. Do you think it's big and there can be more memory-efficient representations? (I've also tried to put the \"Level\" as a member here but it had the same problem as the other too.)\n\n```\nstruct Member\n{\n uchar type; //0=num, 1=str, 2=function, 3=array, etc\n uchar flags; //0x80 = read-only, 0x40 = write-only, etc\n union {\n long double value_num;\n char* value_str;\n int value_func;\n //etc\n };\n};\n```\n\n========================================\n\nTop Answer:\nIts worth noting that if, inside a function, you don't have access to any variables from the caller function, it lowers the number of levels you need to look at. For example:\n\n```\nvariable a;\n\nfunction one() {\n variable b;\n // in this function, we can see the global a, local b\n two();\n}\n\nfunction two() {\n // in this function, we can see the global a, local c\n // we cannot see the local b of our caller\n variable c;\n while (true) {\n variable d;\n // here we can see local d, local c, global a\n }\n}\n```\n\nThe idea being that function boundaries limit the visibility of variables, with the global scope being \"special\".\n\nThat being said, you can consider removing the specialness of global variables, but allowing the code to specify that they want access to non-local variables\n\n```\nvariable a;\n\nfunction one() {\n global a; // or upvar #0 a;\n variable b;\n // in this function, we can see the global a, local b\n two();\n}\n\nfunction two() {\n // in this function, we can see the local c\n // and the local b of our caller\n // (since we specifically say we want access to \"b\" one level up)\n upvar 1 b;\n variable c;\n}\n```\n\nIt looks complicated at first, but it's really easy to understand once you get used to it (upvar is a construct from the Tcl programming language). What it allows you is access to variables in your caller's scope, but it avoids some of the costly lookup involved by requiring that you specify exactly where that variable comes from (with 1 being one level up the call stack, 2 being two levels up, and #0 being \"special\" in saying \"the uppermost call stack, the global)\n\n========================================\n\nCode:\n```text\na = 1\nb = 2\n\nsomeFunction()\n  print(a)   --> This should read the global variable and print `1`\n  a = 3      --> Now `a` should become a local variable of this function\n                 and the global `a` remain unchanged\n  x = 4      --> `x` should always be local of this function\nend\n```\n\n```text\nvariables\n{\n    level=1 //global variables\n    {\n        a => 1,\n        b => 2\n    },\n    level=2 //function variables\n    {\n        a => 3,\n        x => 4\n    }\n}\n```\n\n```text\nvariables\n{\n    (a, 1) => 1, //global\n    (b, 1) => 2, //global\n    (a, 2) => 3, //function\n    (x, 2) => 3  //function\n}\n```\n\n```text\nstruct Member\n{\n    uchar type;  //0=num, 1=str, 2=function, 3=array, etc\n    uchar flags; //0x80 = read-only, 0x40 = write-only, etc\n    union {\n        long double value_num;\n        char* value_str;\n        int value_func;\n        //etc\n    };\n};\n```\n\n```text\nlevel\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\na\n```\n\n```text\na\n```\n\n```text\na\n```\n\n```text\nx\n```\n\n```text\nvariable=>value\n```\n\n```text\nEnvironment\n```\n\n```text\nvariable a;\n\nfunction one() {\n    variable b;\n    // in this function, we can see the global a, local b\n    two();\n}\n\nfunction two() {\n    // in this function, we can see the global a, local c\n    // we cannot see the local b of our caller\n    variable c;\n    while (true) {\n        variable d;\n        // here we can see local d, local c, global a\n    }\n}\n```\n\n```text\nvariable a;\n\nfunction one() {\n    global a; // or upvar #0 a;\n    variable b;\n    // in this function, we can see the global a, local b\n    two();\n}\n\nfunction two() {\n    // in this function, we can see the local c\n    // and the local b of our caller\n    // (since we specifically say we want access to \"b\" one level up)\n    upvar 1 b;\n    variable c;\n}\n```\n\n========================================\n\nComments:\n- I would use a map that maps each variable name to a stack of variable instances. When you create a new variable on a level you push this variable on the stack and the top most element on the stack holds the currently visible variable instance with this name. You also have to maintain a list of allocated variables for each level to pop the variables off the stack when leaving the level. Hope that makes sense, I never designed a language myself.\n- You can simply use a stack of arrays and some form of the de Bruijn indexes. Perform a primitive \"compilation\" step before interpreting, by resolving all the names.\n- It looks like this is how Python does things: see Wikipedia here","metadata":{"transformedAt":"2026-08-18T18:32:17.797Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":15,"totalLines":267,"estimatedTokens":1771}}425{"id":"stack-47726404","source":"stackoverflow","questionId":47726404,"title":"How to put header file to .tab.h in Bison?","tags":["c++","compiler-construction","bison"],"text":"Title: How to put header file to .tab.h in Bison?\nTags: c++, compiler-construction, bison\nSource: Stack Overflow\n\nQuestion:\nI wrote bison code header:\n\n```\n%{\n#include \"foo.h\"\n%}\n```\n\nAnd I defined a struct named 'Foo' in header. I'd like to use it as token type in Bison.\n\n```\n%define api.value.type union\n%token bar\n```\n\nThen I use `-d` option to generate `bison.tab.h` file.\n\n```\nbison -d bison.y\n```\n\nBut there is no `#include foo.h` in `bison.tab.h`, and it use struct Foo to define the union YYSTYPE. \n\n```\n//bison.tab.h\nunion YYSTPE {\n Foo* bar;\n ...\n};\n```\n\nIt caused error when compile this program: `error: ‘Foo’ does not name a type`\n\nIs there a way to include header file in `bison.tab.h` or another solution of this case?\n\n========================================\n\nTop Answer:\nI needed to use 2.3 bison version, which doesn't have %code directive, so I just added a command which insert my include into top of bison output header, when I compile program\n\n```\necho #include \\\"my_include.hpp\\\" | cat - ${BISON_HEADER_OUTPUT} > tmp && mv tmp ${BISON_HEADER_OUTPUT}\n```\n\n========================================\n\nCode:\n```text\n%{\n#include \"foo.h\"\n%}\n```\n\n```text\n%define api.value.type union\n%token <Foo*> bar\n```\n\n```text\nbison -d bison.y\n```\n\n```text\n//bison.tab.h\nunion YYSTPE {\n    Foo* bar;\n    ...\n};\n```\n\n```text\n-d\n```\n\n```text\nbison.tab.h\n```\n\n```text\n#include foo.h\n```\n\n```text\nbison.tab.h\n```\n\n```text\nerror: ‘Foo’ does not name a type\n```\n\n```text\nbison.tab.h\n```\n\n```text\n%union\n```\n\n```text\n%code requires { ... }\n```\n\n```text\n%{ ...  }\n```\n\n```text\n%code\n```\n\n```text\necho #include \\\"my_include.hpp\\\" | cat - ${BISON_HEADER_OUTPUT} > tmp && mv tmp ${BISON_HEADER_OUTPUT}\n```\n\n========================================\n\nComments:\n- can you your bison.y ?\n- What you put between `%{` and `%}` is not processed by Bison, it is copied straight into the generated C (or C++) source file. You can not put Bison-specific statements in a header file that way.\n- \"*But there is no #include foo.h in bison.tab.h*\" <= please show (at least the top of) your `foo.h` and `bison.y`. Because from what's currently given, this is how it's done.","metadata":{"transformedAt":"2026-08-18T18:32:17.797Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":15,"totalLines":125,"estimatedTokens":538}}426{"id":"stack-1130114","source":"stackoverflow","questionId":1130114,"title":"List of known bugs in C# compiler","tags":["c#","compiler-construction"],"text":"Title: List of known bugs in C# compiler\nTags: c#, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIs there such a list? I don't expect to get a complete one, but the list of most well-known ones must be enough.\n\n========================================\n\nTop Answer:\nTry http://connect.microsoft.com/feedback/default.aspx?SiteID=210\n\nWhich version of the .Net framework btw?\n\nI believe that the CLR has been largely stable and bug free since .Net 1.1 SP 1, certainly if in doubt, assume that its a bug in your code not .Net!\n\n========================================\n\nCode:\n```text\npublic static readonly decimal MaxValue = 79228162514264337593543950335M;\n```\n\n========================================\n\nComments:\n- If some bug is 'buggin' you, try gmcs.exe, which comes with the Mono framework.\n- @Dykam - what, and that is bug free? I've reported multiple compiler bugs in gmcs. They are just as prone to error.\n- Surely not bugfree, but if a csc bug bugs you, you can try it with gmcs.\n- Why do you want to know?\n- Because we use it. I know it's quite stable, on the other hand, I was sure there are some issues, and I remember we've been facing some of them. I was pretty sure if there is such a list, there must be rather tricky cases related to C# syntax. So the question is more educational then really practical.\n- Btw, I'm one of readers of russian version of your blog, Eric ;)\n- The compiler has some real bugs, one related to enums. That bug is going to be a feature btw.\n- We're using .NET 3.5 SP1 now.\n- Like so many of M$ documentation links, this one is broken, too.","metadata":{"transformedAt":"2026-08-18T18:32:17.797Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":35,"estimatedTokens":398}}427{"id":"stack-14416472","source":"stackoverflow","questionId":14416472,"title":"What is the difference between subtracting `x` and adding `-x` on an x86 machine?","tags":["assembly","compiler-construction","x86"],"text":"Title: What is the difference between subtracting `x` and adding `-x` on an x86 machine?\nTags: assembly, compiler-construction, x86\nSource: Stack Overflow\n\nQuestion:\nAre there any semantics differences? Is one of them likely to be faster under specific circumstances?\n\n========================================\n\nTop Answer:\nIn addition to the flags being different, if `x` is a constant, choosing one or the other may lead to a shorter encoding (rarely).\n\nAdd and subtract both have a `op r/m32, imm8` form where the immediate operand is a byte, but that byte is sign-extended. So `add edx, 128` would have to be encoded with a dword immediate, but `sub edx, -128` could be encoded with a sign-extended byte (saving 3 bytes).\n\n========================================\n\nCode:\n```text\n0x01 - 0x01 = 0x00, CF = 0\n0x01 + 0xFF = 0x00, CF = 1\n```\n\n```text\n-x\n```\n\n```text\nsub smth, x\n```\n\n```text\nadd smth, -x\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\na + b = 0\n```\n\n```text\nx\n```\n\n```text\nop r/m32, imm8\n```\n\n```text\nadd edx, 128\n```\n\n```text\nsub edx, -128\n```\n\n========================================\n\nComments:\n- Sorry I was wrong before. Check this answer.\n- are you talking about how a given language will compile (a+b) vs (a+(-b)) or are you talking about direct assembly? Most compilers, I imagine would compile (a+(-b)) directly to a single fsub, but that, I guess, would depend on which language and compiler we're talking about. In direct assembly adding a negative would take two instructions (in the x87 unit this would be fchs followed by fadd instead of a direct fsub. fadd and fsub both execute in the same time so the former would be slower.\n- Actually in hardware they are both implemented the same (via a logical ADD unit). If you send SUBTRACT to the processor, it twos-compliments it before it hits the processor, but since the hardware is all in parallel you get no performance hit so they are exactly as fast as each other :)\n- @SecurityMatt I know.\n- Cool. It just wasn't obvious from your post that's all.\n- If the implemented hardware is the same, how does it know when to set the flag appropriately?\n- @TungNguyen See the code in this answer.\n- @ Alexey Frunze: Suppose I substract 126 from 0. If I understand correctly, 126 would be 1000 0000 in computer and the 2 complement is the same. So the 126 operand would go through a gate to be negated and then goes to the addition unit in the ALU as usual, right ?. Then it would be 0000 0000 + 1000 0000. Certainly there is no carry at the 8th bit here. But as I test it, the carry flag is still set on.\n- ^: typo, I meant 128, not 126\n- @TungNguyen You completely ignored the carry/borrow. Look at Sbb() again. It inverts carry-in before Adc() and inverts carry-out after Adc(). See, it correctly produces the line ` 0( 0) - 128(-128) - 0 = 128(-128) CY=1 OV=1`. Your gates invert 3 things: the subtrahend and the carry before and after the addition.\n- Subtracting 0x80 is going to be the same as adding 0x80 (with 8-bit add/sub) unless we look at flags. In 8 bits 0x80 and -0x80 are the same bit pattern 10000000B. Could you elaborate on the advantage/disadvantage?\n- @Alexey Frunze: Consider the smallest representable value in a signed 32 bit integer: -2147483248. Now compare that to the largest representable value, 2147483647. It is clear that the magnitude of the former is one larger than the magnitude of the latter. This means that you can subtract a slightly larger value by adding -2147483248 than by subtracting 2147483647.\n- You can subtract -2147483248 or +2147483248 (in modulo-2^32 arithmetic) and arrive at the same result (except flags) as with adding -2147483248.\n- +2147483248 doesn't fit in a 32-bit signed integer. -2147483248 does.\n- The CPU does not distinguish signed from unsigned in addition and subtraction.\n- True. Usually numbers are regarded as either signed or unsigned when we reason about programs though.\n- let us continue this discussion in chat","metadata":{"transformedAt":"2026-08-18T18:32:17.797Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":82,"estimatedTokens":988}}428{"id":"stack-377529","source":"stackoverflow","questionId":377529,"title":"Execution speed of references vs pointers","tags":["pointers","compiler-construction","reference","jit","vm-implementation"],"text":"Title: Execution speed of references vs pointers\nTags: pointers, compiler-construction, reference, jit, vm-implementation\nSource: Stack Overflow\n\nQuestion:\nI recently read a discussion regarding whether managed languages are slower (or faster) than native languages (specifically C# vs C++). One person that contributed to the discussion said that the JIT compilers of managed languages would be able to make optimizations regarding references that simply isn't possible in languages that use pointers.\n\nWhat I'd like to know is what kind of optimizations that are possible on references and not on pointers?\n\nNote that the discussion was about execution speed, not memory usage.\n\n========================================\n\nTop Answer:\nIn C++ there are two advantages of references related to optimization aspects:\n\nA reference is constant (refers to the same variable for its whole lifetime)\n\nBecause of this it is easier for the compiler to infer which names refer to the same underlying variables - thus creating optimization opportunities. There is no guarantee that the compiler will do better with references, but it might...\n\nA reference is assumed to refer to something (there is no null reference)\n\nA reference that \"refers to nothing\" (equivalent to the NULL pointer) can be created, but this is not as easy as creating a NULL pointer. Because of this the check of the reference for NULL can be omitted.\n\nHowever, none of these advantages carry over directly to managed languages, so I don't see the relevance of that in the context of your discussion topic.\n\n========================================\n\nComments:\n- Well, if you aren't managing memory properly that can certainly hurt execution speed.\n- One concrete big project proof was made with IronPython vs CPython. The latest versions of IronPython beat CPython in every benchmark, with a small or big margin, while retaining 100% compatbility. That's with a several years of optimisations of CPython and a young IronPython project.\n- You haven't answered the question... \"What I'd like to know is what kind of optimizations that are possible on references and not on pointers?\"...\n- I think the point of the question might be based on assumptions about how references and pointers are used in actual practice (is your reference to a specific object? is your pointer merely to void?) as opposded to anything intrinsic to the way they work.","metadata":{"transformedAt":"2026-08-18T18:32:17.797Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":33,"estimatedTokens":601}}429{"id":"stack-9351698","source":"stackoverflow","questionId":9351698,"title":"How does a C++ compiler compile variable names?","tags":["c++","variables","compiler-construction"],"text":"Title: How does a C++ compiler compile variable names?\nTags: c++, variables, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI understand I did not make myself clear. My doubt, I think, could be summed up in this:\n\nIn an executable file(machine code) how are \"variables\" represented? Are they static memory addresses? Does the compiler gives each one a specific \"name\" (or just keeps the one you gave them)?\n\nExpressed in code:\n\n```\nint x=5;\n //Bunch of code\n cin>>y;\n coutHow does the program in each and every machine knows which address is going to hold the value 5, to hold the inputed value, to add 1 to the value it now holds and finally print that same value.\n\n--João\n\n========================================\n\nTop Answer:\nIt's implementation-specific.\n\nTypically, the location of variables will be based on all sorts of factors and optimizations. They may not live in RAM at all, as they may be optimised to live entirely within registers, or optimised away entirely.\n\nVariable names don't exist at run-time; they're discarded during compilation. However, the compiler may emit debug information that's stored in the application binary, to allow the developers to debug the application. This is usually removed in release versions, though.\n\nI have no idea about the specifics of Gameshark. But in many cases, the location of a particular variable can be figured out by taking a look at the machine code for the application.\n\n========================================\n\nCode:\n```text\nint x=5;\n //Bunch of code\n cin>>y;\n cout<<x+1;\n```\n\n```text\nint main() {\n    int a = 5;\n    int b = 7;\n\n    int c = a + b;\n\n    return 0;\n}\n```\n\n```text\n.file   \"main.c\"\n    .text\n    .globl  main\n    .type   main, @function\nmain:\n.LFB0:\n    /* %ebp is a Base Pointer Register */\n    pushl   %ebp\n    movl    %esp, %ebp\n\n    /* Here we reserve space for our variables */\n    subl    $16, %esp\n\n    /* a's address is %ebp - 4 */\n    movl    $5, -4(%ebp)\n\n    /* b's address is %ebp - 8 */\n    movl    $7, -8(%ebp)\n\n    /* a + b */\n    movl    -8(%ebp), %eax\n    movl    -4(%ebp), %edx\n    addl    %edx, %eax\n\n    /* c's address is %ebp - 12 */\n    movl    %eax, -12(%ebp)\n\n    /* return 0 */\n    movl    $0, %eax\n    leave\n    ret\n```\n\n```text\ngcc -m32 -S -O0 -o main.s main.c\n```\n\n========================================\n\nComments:\n- Your whole post wasn't showing. I'm sorry. About the variables: How what does the compiler do to var names. About Cheat Database question: You say the location of a variable can be figured out by the machine code? does that mean a variable is always given the same address?\n- @Jo&#227;oSilva: Your original question doesn't really make sense, the compiler doesn't \"compile variable names\". My answer is an attempt at explaining what I think you want to know.\n- @Jo&#227;oSilva The compiler throws away variable names.\n- @DavidHeffernan not always, the linker may need them\n- @DavidHeffernan then how does the what to print in a case similar to this: int a; cin>>a; //Bunch of code cout<<a;\n- @Seth Alright then. Let's try again. The compile/link phase throws away names.\n- @Jo&#227;oSilva That question does not make any sense\n- I pressed enter by mistake read it again.\n- @Jo&#227;oSilva: Re \"is a variable always given the same address?\". It's complicated. On older platforms, if you and I both run the exact same application, then a particular variable will live at the same location in (virtual) memory. On modern systems, features such as ASLR affect things.\n- Damn! That was hard! That was exactly the kind of answer i was looking for. Thank you!\n- the program that runs on the machine knows where to find the variable that **you** call a, but it does not know that you called it a.\n- @DavidHeffernan the question is how...\n- So your question is, how does a compiler work?\n- Thank you for your answer. Sorry to be so confused but imagine this: int a=5; a=6;your first explanation doesn't tell me how the program knows which value to change. On the other hand your second answer explains the process of searching for values in memory like cheatengine does. That I already knew. My question was about cheat databases which hold what look like adresses like if the same address always holds the same variable in any console(video game console) everytime the program runs. Sorry if I'm not clear. My english is just not helping me.\n- @Jo&#227;oSilva: Many systems are implementing ASLR which would make that difficult.\n- THANK YOU SO MUCH. You went through the trouble of explaining the inside asm code to me. You did not only answer my question but satisfied my curiosity and made me want to learn assembly. Kudos to you Sr.\n- Just one more thing if it is not too much to ask for. Do those offsets mean for example: %ebp -12 = less 12 addresses counting from the base one?\n- @Jo&#227;oSilva if %ebp is 0xffffcfe8, c's address will be 0xffffcfe8 - 4 = 0xffffcfdc, if that's what you're asking.\n- @Jo&#227;oSilva I'd also recommend this article en.wikipedia.org/wiki/Call_stack\n- @kharvd did you actually compile it to assembly, or just produce that ASM yourself? If you wrote the ASM, you sir, are a legend.\n- @Brett Surely I just used GCC and did some cleanup afterwards. It's not so difficult to write such simple logic in assembly language by hand, though.","metadata":{"transformedAt":"2026-08-18T18:32:17.797Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":113,"estimatedTokens":1324}}430{"id":"stack-15564011","source":"stackoverflow","questionId":15564011,"title":"OOP Access Modifiers: Compile-time or Run-time","tags":["java","c++","oop","compiler-construction"],"text":"Title: OOP Access Modifiers: Compile-time or Run-time\nTags: java, c++, oop, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI've heard that *Access Modifiers* `Public, Private and Protected` are just some Compiler Stuff, and they're not actually exists in the compiled *Binary Code*.\n\nNow I'm wondering how much it's correct? And if it's correct, does it mean that *Encapsulation* is not exist in the binary code at run-time? So, if you modify the binary to access a `Private` method illegally, in theory, there isn't anything to check your rights, neither any OOP mechanism nor Operating System, right?\n\nI've also tagged the question for both C++ and Java. I'm aware of the difference between them, just curious to see how different they handle *Access Modifiers*.\n\n========================================\n\nTop Answer:\nPlease note this answer is with respect to Java \n\nBoth. If you try to compile code which tries to access inaccessible object or method, you'll get compile-time error:\n\nand at run time, JVM checks the access:\n\nYou will get following exception if you try to access at run time. With improper access level java.lang.IllegalAccessError:\n\nHope it helps\n\n========================================\n\nCode:\n```text\nPublic, Private and Protected\n```\n\n```text\nPrivate\n```\n\n```text\nclass A\n{\npublic:\n  void DoSomething()\n  {\n    // use private member mPrivMember to do something\n  }\nprivate:\n  int mPrivMember;\n}\n```\n\n```text\nA a_obj;\n```\n\n```text\nA\n```\n\n```text\nA\n```\n\n```text\na_obj.DoSomething()\n```\n\n```text\na.mPrivMember\n```\n\n```text\nA\n```\n\n```text\nDoSomething()\n```\n\n```text\nmPrivMember\n```\n\n```text\nmPrivMember\n```\n\n```text\nmPrivMember\n```\n\n```text\nDoSomething\n```\n\n```text\nmPrivMember\n```\n\n```text\nmPrivMember\n```\n\n```text\nDoSomething\n```\n\n```text\nmPrivMember\n```\n\n```text\nprivate\n```\n\n```text\nprotected\n```\n\n```text\npublic\n```\n\n```text\nprivate\n```\n\n```text\nprotected\n```\n\n```text\nPrivate\n```\n\n```text\nPublic\n```\n\n```text\nProtected\n```\n\n========================================\n\nComments:\n- Thanks, that's why I tagged both `C++` and `Java`. So, `JVM` will do the magic there? I'm thinking how `C++` will handle that! :)\n- Do you think it could be helpful if we have them in binary also? Could it be useful in any case?\n- No, since binaries aren't supposed to be modified in that form. And a binary won't be generated if access rights are being violated in source code in the first place, because of compile time errors, so putting such enforcement mechanisms in a binary would needlessly bloat it and add overhead and be redundant.\n- @Mahdi note that in Java the access modifiers exist in the binary form. I have updated my answer's Recap section to explain why.\n- I thought there might be a mechanism like an `Access Table` included in the binary, checking the rights ...\n- @Mahdi Nope :). It would add a huge performance hit to add something like that at that low of level. You'd basically have to do an extra lookup for *all* code. That would mean a huge performance decrease. (Also, it would literally have to be done at a hardware level. Otherwise how do you stop code from just jumping to a 'private' address? Actually, now that I think about it... I have no idea at all how you could implement true private/public/protected without different address spaces or crazy-huge lookup tables.)\n- oh yeah! I see what you mean! we need `OOP Processors` ... :D","metadata":{"transformedAt":"2026-08-18T18:32:17.797Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":26,"totalLines":150,"estimatedTokens":846}}431{"id":"stack-2300151","source":"stackoverflow","questionId":2300151,"title":"Closure conversion and separate compilation of higher-order function calls","tags":["compiler-construction","programming-languages","functional-programming","closures"],"text":"Title: Closure conversion and separate compilation of higher-order function calls\nTags: compiler-construction, programming-languages, functional-programming, closures\nSource: Stack Overflow\n\nQuestion:\nIs there a standard way of dealing with the interaction between separate compilation and different kinds of closure conversion when compiling higher-order function calls?\n\nI know of three function-like constructs that are distinctly compiled in most programming languages: closures, (top-level) functions, and C++-style function objects. Syntactically they are called the same way, but a compiler would optimally generate distinctly-shaped call sites:\n\n```\nSyntax: | clo(args) | func(args) | obj(args)\n--------------------------------------------------------------------------------\nCodegen: | clo.fnc(&clo.env, args) | func(args) | cls_call(&obj, args)\n ^ ^ ^ ^ ^\n fn ptr | +--\"top level\" fn --+ |\n +--- \"extra\" param, compared to source type -----+\n```\n\n(In C++, `cls_call` would be `T::operator()` for `obj`'s class `T`. C++ also allows virtual functors, but that's essentially the closure case with an extra indirection.)\n\nAt this point, calls to `map (x => x > 3) lst` and `map (x => x > y) lst` should invoke different `map` functions, because the first is a simple function pointer after hoisting, and the second is a closure.\n\nI can think of four ways of dealing with this issue:\n\nThe C++ (98) approach, which forces the callee to either pick a call-site shape (via formal parameter type: virtual functor, function pointer, or non-virtual functor) or drop separate compilation by using a template, effectively specifying solution #2 below.\n\nOverloading: the compiler could do multiple instantiation of `map`, and all other higher-order functions, with appropriate name-mangling. In effect, there is a separate internal function type per call site shape, and overload resolution picks the right one.\n\nMandate a globally uniform call-site shape. This means that all top-level functions take an explicit `env` argument, even if they don't need it, and that \"extra\" closures must be introduced to wrap non-closure arguments. \n\nRetain the \"natural\" signature for top-level functions, but mandate that all handling of higher-order function params be done through closures. The \"extra\" closures for already-closed functions call a wrapper trampoline function to discard the unused `env` parameter. This seems more elegant than option 3, but harder to implement efficiently. Either the compiler generates a multitude of calling-convention-indepedent wrappers, or it uses a small number of calling-convention-sensitive thunks...\n\nHaving an optimized closure-conversion/lambda lifting hybrid scheme, with a per-function choice of whether to stick a given closure argument in the env or the parameter list, seems like it would make the issue more acute.\n\nAnyways, questions:\n\n- Does this issue have an explicit name in the literature?\n\n- Are there other approaches besides the four above?\n\n- Are there well-known tradeoffs between approaches?\n\n========================================\n\nCode:\n```text\nSyntax:  | clo(args)                 |   func(args)     |   obj(args)\n--------------------------------------------------------------------------------\nCodegen: | clo.fnc(&clo.env, args)   |   func(args)     |   cls_call(&obj, args)\n              ^      ^                      ^                   ^     ^\n            fn ptr   |                      +--\"top level\" fn --+     |\n                     +--- \"extra\" param, compared to source type -----+\n```\n\n```text\ncls_call\n```\n\n```text\nT::operator()\n```\n\n```text\nobj\n```\n\n```text\nT\n```\n\n```text\nmap (x => x > 3) lst\n```\n\n```text\nmap (x => x > y) lst\n```\n\n```text\nmap\n```\n\n```text\nmap\n```\n\n```text\nenv\n```\n\n```text\nenv\n```\n\n```text\nmapints :: (Integer -> a) -> [a]\nmapints f = map f [1..]\n```\n\n```text\ninclist :: [Integer] -> [Integer]\ninclist = map (1+)\n```\n\n```text\ncompose :: (b -> c) -> (a -> c) -> (a -> c)\ncompose f g x = f (g x)\n```\n\n```text\nmap\n```\n\n```text\nmap\n```\n\n```text\nf\n```\n\n```text\ng\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.797Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":18,"totalLines":124,"estimatedTokens":1012}}432{"id":"stack-5198860","source":"stackoverflow","questionId":5198860,"title":"Help understanding LR(1) parsers, table generation? Any other resources?","tags":["parsing","compiler-theory","compiler-construction"],"text":"Title: Help understanding LR(1) parsers, table generation? Any other resources?\nTags: parsing, compiler-theory, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am currently taking a compilers class and I am having a hard time understanding LR(1) parsing algorithms using the action/goto table and also how to hand generate these tables. Right now we are using Engineering a Compiler by Cooper and Torczon as our class text book and I have also read the wikipedia pages on table generation but I still do not understand the concepts. If possible can anyone recommend any other book that explains parsing well or an online resource? I would think many universities would have good online resources/slides on the subject but I have no idea on where to start looking. Thanks!\n\n========================================\n\nTop Answer:\nThe books are always hard to read because of the algorithm details. Greek symbols and abstract operations are hard to interpret unless you already know what they mean.\n\nThe way I learned how to do this, was to write a tiny grammar (simple expression,\nassignment statement, if then statement, sequence of statements), and then *hand simulate the algorithm*. Get a really big piece of paper. Draw the starting configuration state with just the goal symbol and dot [ G = DOT RHS1 ... RHSM ]. Then process the unprocessed states, following the algorithm in detail; write down what each greek symbol represents at that moment. As you gain confidence, you'll get a better feeling and it will go faster.\n\nEssentially what you are going to do is, for each item I\n\n```\n[LHS RHS1 DOT RHS2 RHS3 ... RHSN]\n```\n\nin a state, push the dot in item one place to right to produce a new item\n\n```\n[LHS RHS1 RHS2 DOT RHS3 ... RHSN ]\n```\n\ndraw a new state on your paper new state with that item as the seed, fill out the item core with lookahead sets based on FIRST(RHS3), expand the state, and repeat.\n\nThis will take you several hours the first time you try it. Worth every second.\nUse a pencil!\n\n========================================\n\nCode:\n```text\n[LHS RHS1 DOT RHS2 RHS3 ... RHSN]\n```\n\n```text\n[LHS RHS1 RHS2 DOT RHS3 ... RHSN ]\n```\n\n========================================\n\nComments:\n- +1. I and a couple of friends did this in our office when we took a compiler course a few years ago. The state machine filled up the entire whiteboard, so we had to draw the action/goto table on the adjacent blackboard. Then, we had run out of surface space for writing down the actual execution of the algorithm (stack contents and actions being performed) - until we discovered that the *windows* were an excellent source of writing surface! We got quite a few perplexed looks from bypassers... :-)\n- @Aasmund Eldhuset: +1 for a constructive use of Windows :-}\n- I should have seen that one coming :p","metadata":{"transformedAt":"2026-08-18T18:32:17.797Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":49,"estimatedTokens":704}}433{"id":"stack-33928481","source":"stackoverflow","questionId":33928481,"title":"How are memory references located in a moving garbage collection implementation?","tags":["assembly","compiler-construction","garbage-collection"],"text":"Title: How are memory references located in a moving garbage collection implementation?\nTags: assembly, compiler-construction, garbage-collection\nSource: Stack Overflow\n\nQuestion:\nIn a moving Garbage Collector, it is imperative that a precise method of distinguishing between which values on the stack and heap are references, and which are immediate values. This is a detail that seems to be glossed over in most of the literature I have read on garbage collection.\n\nI have investigated whether assigning some preamble to each stackframe would work, for example, describing each argument before it is called. But surely all this does is move the problem to an upper level of indirecton. How does one then distinguish the preamble from the stack frame when traversing it for immediate values or references during a GC cycle? \n\nCould somebody explain how this is implemented in the real world? \n\nHere is an example program of this problem using a first class function lexical closure and a diagram of its stack frame and and parent's environment located on the heap:\n\n**An example program**\n\n```\ndef foo(x) = {\n def bar(y,z) = {\n return x + y + z\n }\n return bar\n}\n\ndef main() = {\n let makeBar = foo(1)\n makeBar(2,3)\n}\n```\n\n**Bar's stackframe at point of invocation**:\n\nhttps://i.sstatic.net/xN48G.png\n\nIn this example, bar's stack frame has a local variable, x, which is a pointer to a value on the heap, where as the arguments y and z are immediate integer values.\n\nI read that Objective CAML uses a tag bit for each value placed on the stack which prefixes each value. Allowing a binary ref-or-imm check to be made on each value during a GC cycle. But this can have some unwanted side-effects. Integers are restricted to 31 bit and dynamic code generation for primitive calculations would need to be adjusted to compensate for this. In short - it feels a bit too dirty. There must be a more elegant solution.\n\nIs it possible to know, and access this information statically? Such as passing the type information to the garbage collector somehow?\n\n========================================\n\nTop Answer:\nThe answer above identifies the three main alternatives. There is a variation of the 3rd alternatives that has been tried:\n\n- Have the compiler partition / reorder the variables in the stack and object frames so that (for example) the reference variables come before the scalar variables.\n\nThat means that the type information that needs to be retained at runtime is a single number. This could be stored in the frame itself, or as type information associated with the class or method ... in the normal way. However, this introduces other overheads; e.g. the need for dual stacks and stack pointers. Empirically, it is not a win.\n\nSome other points:\n\nThe problem of identifying references exists for all kinds of GC.\n\nIf you go down the \"conservative\" approach, (where reference identification may be inaccurate), then you cannot safely compact the heap. This includes all kinds of copying collector.\n\nMark bits (unless they are hardware supported) can be problematic for efficient arithmetical operations. (If you need to \"steal\" a bit to distinguish pointers and non-pointers, then arithmetical operations require additional instructions to compensate. FWIW, the MIT CLU compiler used to do this ... back in the 1980's. The CLU GC was an accurate mark/sweep/compact collector, but integer arithmetic was slow ... and I can't recall how they dealt with floating point.)\n\n========================================\n\nCode:\n```text\ndef foo(x) = {\n    def bar(y,z) = {\n        return x + y + z\n    }\n    return bar\n}\n\n\ndef main() = {\n    let makeBar = foo(1)\n    makeBar(2,3)\n}\n```\n\n========================================\n\nComments:\n- As interesting study, see the development of the garbage collector in the `mono` framework.\n- Thank you, I will investigate this.\n- The stop-and-copy garbage collection algorithm is just one kind of tracing garbage collection method, all of which determine whether an object is live by tracing references to it back to certain root objects. How these root objects are determined is not part of the garbage collection algorithm itself. There's lots of possible ways a value on the stack can be determined to be a root. An implementation can assume any value is a reference to an object. Or it could assume there aren't any at all, either by not having any objects on the stack or requiring that they be rooted elsewhere.\n- @RossRidge, Good point, I understand. I just thought i'd include this in my question to give it some context.\n- \"An implementation can assume any value is a reference to an object.\" So what happens when it encounters an immediate integer value on the stack which has the same value as an address in memory which contains a value? How is this ambiguity resolved?\n- It's not, it's assumed to be a valid reference to the object.\n- I see. So not deallocating items on the heap perfectly between both heap fragments is considered a trade-off in order to favour fast and easy implementation of pointer chasing during each GC cycle? Couldn't this have the detrimental effect of actually increasing the number of items to copy to the next fragment during a GC cycle causing a crash. (Esp during the first cycle)\n- A moving garbage collector would have to not move an object ambiguously referenced from the stack. You probably wouldn't want to use that method of determining roots with a moving garbage collector. (Or alternatively you probably wouldn't want to use a moving garbage collector if you're determining roots impricely.)\n- yes, imprecise scanning is a tradeoff and yes it does have an impact on performance because you can't implement a semi-space GC that way / or have to maintain freelists around pinned objects instead of bump pointer allocation.\n- @Jake As to \"How does one then distinguish the preamble from the stack frame\", why would that be a problem is the preamble also contains, at fixed offset, the size of the stack frame?\n- In the case of mainstream JVMs, the third alternative is the one that applies.\n- w.r.t The first point. If an immediate value is considered to be a potential pointer and the data on the heap at that address is moved onto the other fragment and the 'pointer' is updated to represent this new address. Wouldn't this no longer preserve the meaning of the program, as an immediate value would have its data changed by the GC during the program life-cycle.\n- @Jake, i didn't say anything about moving, but i'll update my answer to clarify\n- @the8472 Why is the second alternative still imprecise?\n- @MartinBerger - It depends on how you implement the mark / tag bit. Some ways are precise (and expensive), others are not.\n- @StephenC Thanks. Have you got a pointer to the relevant literature?\n- Not off the top of my head, no.\n- One problem with this scheme is interaction with the outside world: even something as simple as `println(4)` should not really be executed twice. You could contact Emery and ask him if he's actually implemented it, or knows of an implementation.","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":95,"estimatedTokens":1767}}434{"id":"stack-4175632","source":"stackoverflow","questionId":4175632,"title":"Is every LL(1) grammar also an LR(1)?","tags":["compiler-construction","lr-grammar"],"text":"Title: Is every LL(1) grammar also an LR(1)?\nTags: compiler-construction, lr-grammar\nSource: Stack Overflow\n\nQuestion:\nIs every LL(1) grammar also an LR(1)?\n\n========================================\n\nCode:\n```text\nA -> \"(\" A \")\" | \"(\" \")\"\n```\n\n```text\n(()) -> A\n     -> \"(\" A \")\"\n     -> \"(\" \"(\" \")\" \")\"\n```\n\n```text\nInput  Stack          Action\n(())   0       \n())    0 '('\n))     0 '(' '(' \n)      0 '(' '(' ')'  Reduce using A -> \"(\" \")\"\n)      0 '(' A\n-      0 '(' A ')'    Reduce using A -> \"(\" A \")\"\n-      0  A           Accept\n```\n\n```text\n(())\n```\n\n========================================\n\nComments:\n- I think I had this question in college one day many moons ago ;)\n- It's worth mentioning here that LL(1) is NOT contained in LR(0) (because of epsilon rules).\n- but the sequence(of productions) used by LL(1) to parse is not always in the opposite sequence(of productions)used by LR(1) to parse. Even though former being the top down and latter being bottom up parser so your reason for all LL(1) to be LR(1) does not seem enough.\n- Something being LR does not mean that the parse tree with be identical to the inverse LL parse tree, and the thus parser will not necessarily use the productions in the opposite order. What it does mean is that an LR parser can correctly parse the same set of strings given the same grammar.\n- Does this fact contradicts? cs.stackexchange.com/questions/60763/&hellip;⊆-lr1/60764#60764","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":43,"estimatedTokens":357}}435{"id":"stack-6394937","source":"stackoverflow","questionId":6394937,"title":"LLVM's calling convention for GHC","tags":["haskell","compiler-construction","llvm","x86-64","ghc"],"text":"Title: LLVM's calling convention for GHC\nTags: haskell, compiler-construction, llvm, x86-64, ghc\nSource: Stack Overflow\n\nQuestion:\nHere is the text in LLVM's doc \"langref\":\n\n\"cc 10\" - GHC convention\n\nThis calling convention has been implemented specifically for use by the Glasgow Haskell Compiler (GHC). It passes everything in registers, going to extremes to achieve this by disabling callee save registers. This calling convention should not be used lightly but only for specific situations such as an alternative to the **register pinning** performance technique often used when implementing functional programming languages.At the moment only X86 supports this convention and it has the following limitations:\n\n- On X86-32 only supports up to **4 bit type parameters**. No floating point types are supported.\n\n- On X86-64 only supports up to **10 bit type parameters and 6 floating point parameters**.\n\nQ:\n\ndoes \"register pinning\" means or refers to \"passes everything in registers\", likely.\n\nwhat is \"4 bit type parameters\"? I just searched in the Intel's IA manual, but didn't find anything. does it a feature of Intel's CPU?\n\n========================================\n\nCode:\n```text\nCC_X86_64_GHC\n```\n\n```text\nCC_X86_32_GHC\n```\n\n========================================\n\nComments:\n- Hm. I'd read that as “up to 4 (bit type) parameters“, not “(4 bit type) parameters“ I'd wager, this would refer to integer values up to machine word size, though I've never heard the term before, either\n- @Dirk:I think you are right. I should update my Lexer for English in my brain asap...","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":37,"estimatedTokens":395}}436{"id":"stack-3682620","source":"stackoverflow","questionId":3682620,"title":"Compile Time Code Generation in D","tags":["reflection","compiler-construction","code-generation","d"],"text":"Title: Compile Time Code Generation in D\nTags: reflection, compiler-construction, code-generation, d\nSource: Stack Overflow\n\nQuestion:\nI'm currently learning D, and one of the things I've wondered about is whether D has a way of duplicating the code generation capabilities of JVM languages.\n\nHere's a scenario:\nI have an object, and I want to generate a string based on that object that gives it's name and all its fields. In Java/Scala, I *could* just use reflection, but suppose speed is an issue. I could use a bytecode generation library to dynamically generate and compile a class that does this without reflection. The implementation would break down to iterating through the object's fields and getting it's name through recursion, and using that information to dynamically generate Java (or bytecode) that accesses the fields directly.\n\nIf you don't like that scenario because it's weak and/or unrealistic, another one that might be more realistic is optimized object serialization.\n\nI've seen examples where D's compile time evaluation and/or template metaprogramming is used for things like precalculating the fibonacci sequence at compile time and other recursive algorithms, but is there a way of doing things like this with just the language and a compiler, or would you need to develop a separate code-generator and run it before the compiler to get this sort of functionality?\n\n========================================\n\nTop Answer:\nNot only can this be done, it's practically done for you in D2. All you need is a small mixin to turn these from compile time features into runtime features. \n\nFor the class name, all you need to do is evaluate `typeof(this).stringof` inside the class's scope. \n\nFor a list of all fields, try `__traits(allMembers, typeof(this))` and then filter out the stuff that's not a field (`std.traits` will be useful here).\n\n========================================\n\nCode:\n```text\nthis.tupleof\n```\n\n```text\nclass Class : ISerializable { int foo; Forble bar; mixin(genSerialize(\"foo, bar\")); ... }\n```\n\n```text\ntypeof(this).stringof\n```\n\n```text\n__traits(allMembers, typeof(this))\n```\n\n```text\nstd.traits\n```\n\n========================================\n\nComments:\n- Please, can you consider this post ?","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":50,"estimatedTokens":560}}437{"id":"stack-4551044","source":"stackoverflow","questionId":4551044,"title":"Strategy for desugaring Haskell","tags":["compiler-construction","haskell","lambda-calculus"],"text":"Title: Strategy for desugaring Haskell\nTags: compiler-construction, haskell, lambda-calculus\nSource: Stack Overflow\n\nQuestion:\nI'm developing a virtual machine for purely functional programs, and I would like to be able to test and use the the wide variety of Haskell modules already available. The VM takes as input essentially terms in the untyped lambda calculus. I'm wondering what would be a good way to extract such a representation from modern Haskell modules (eg. with MPTC's, pattern guards, etc.). I did a little research and there doesn't seem to be a tool that does this already (I would be delighted to be mistaken), and that's okay. I'm looking for an approach.\n\nGHC Core seems too operationally focused, especially since one of the things the VM does is to change the evaluation order significantly. Are there any accessible intermediate representations that correspond more closely to the lambda calculus?\n\n========================================\n\nTop Answer:\nEHC used to have this flow between representations:\n\nHS -> EH -> Core -> Grin -> Silly\n\nHS is obviously Haskell, EH is less sugary and used by the type checker, Core is the analogue to GHC's Core and Grin and Silly are lower level than that. \n\nThere is a paper describing EHC's architecture by Jeroen Fokker, where I've cribbed the above from, but it's probably out of date (I don't know that EHC still uses the Silly representation). I think that EHC provides parsers and pretty printers for each of its internal representations, which would be handy. \n\nThere's also York.Core from YHC which I've seen people speak more favorably of then GHC Core, but I don't know if there is a means of compiling \"modern\" Haskell into York.Core.\n\n========================================\n\nComments:\n- The core route means that you're driven by case statements -- but you could transform those back out into church encodings and then you're 90% of the way there, no?\n- @sclv, hmm that's a good point.\n- Not sure if I'm going to go with it, but this is a great answer for getting started with Core processing, something I thought daunting in the past.\n- Yeah I found EHC right after I asked the question. That ain't Haskell though, it's some bastard aspect oriented attribute grammar thing that I find very hard to read. Oh well, may have to bite the bullet, depending on how far I can get with the tools Antoine suggested...","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":29,"estimatedTokens":597}}438{"id":"stack-30562134","source":"stackoverflow","questionId":30562134,"title":"Jison global variables","tags":["compiler-construction","flex-lexer","jison"],"text":"Title: Jison global variables\nTags: compiler-construction, flex-lexer, jison\nSource: Stack Overflow\n\nQuestion:\nIn previous versions of Jison, it was possible to have a Flex-like feature that allowed defining variables accessible in both the lexer and parser contexts, such as:\n\n```\n%{\nvar chars = 0;\nvar words = 0;\nvar lines = 0;\n%}\n\n%lex\n%options flex\n\n%%\n\\s\n[^ \\t\\n\\r\\f\\v]+ { words++; chars+= yytext.length; }\n. { chars++; }\n\\n { chars++; lines++ }\n/lex\n\n%%\nE : { console.log(lines + \"\\t\" + words + \"\\t\" + chars) ; };\n```\n\n**Ref.:** Flex like features?\n\nAlthough, in the latest version of Jison, this isn't valid. `chars`, `words` and `lines` cannot be reached from the parser context, generating an error.\n\nSearching more about the new version, I found that it should be possible by defining output, on parser's context, inside of `%{ ... %}`, but it doesn't work, although it is used for multi-line statements. I'm generating code from a source to a target language and I'll prettify this code, applying the correct indentation, controlled by the scope and generating directly from parser, without building an AST. \n\nHow do global definitions currently work in Jison?\n\n========================================\n\nTop Answer:\nAs a suggestion for Govind Mantri, instead of using 'chars' in the 'if' you should use a variable than if it is used, for example 'cities'. The same thing happened to me with the concatenation problems, but with that I solved it.\n\nif (!('chars' in yy)) {\nyy.cities = [\"Austin\",\"New_York\",\"Chicago\",\"Las_Vegas\"];\n\n=>\n\nif (!('cities' in yy)) {\nyy.cities = [\"Austin\",\"New_York\",\"Chicago\",\"Las_Vegas\"];\n\n========================================\n\nCode:\n```text\n%{\nvar chars = 0;\nvar words = 0;\nvar lines = 0;\n%}\n\n%lex\n%options flex\n\n%%\n\\s\n[^ \\t\\n\\r\\f\\v]+ { words++; chars+= yytext.length; }\n. { chars++; }\n\\n { chars++; lines++ }\n/lex\n\n%%\nE : { console.log(lines + \"\\t\" + words + \"\\t\" + chars) ; };\n```\n\n```text\nchars\n```\n\n```text\nwords\n```\n\n```text\nlines\n```\n\n```text\n%{ ... %}\n```\n\n```text\n%lex\n%options flex\n\n%{\nif (!('chars' in yy)) {\n  yy.chars = 0;\n  yy.words = 0;\n  yy.lines = 1;\n}\n%}\n\n%%\n[^ \\t\\n\\r\\f\\v]+ { yy.words++; yy.chars += yytext.length; }\n. { yy.chars++; }\n\\n { yy.chars++; yy.lines++ }\n/lex\n\n%%\nE : { console.log( yy.lines + \"\\t\" + yy.words + \"\\t\" + yy.chars); };\n```\n\n```text\nyy\n```\n\n```text\nyy\n```\n\n========================================\n\nComments:\n- for multiple Expressions this global variable is initialized different for every other E. in that scenario if I want to store values by concating a string its not working. Here is my grammar if u can suggest what changes do i have to make so that my variable `$x` can store all the value across all E. jsfiddle.net/Lnukko75/1","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":123,"estimatedTokens":680}}439{"id":"stack-9703465","source":"stackoverflow","questionId":9703465,"title":"Last basic block of a function in LLVM","tags":["compiler-construction","llvm"],"text":"Title: Last basic block of a function in LLVM\nTags: compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nDoes the back() of a Function guarantee to return the terminator basic block of CFG in LLVM?\n\n========================================\n\nTop Answer:\nNo. There could be multiple terminator basic blocks of a function, for instance a function containing multiple return statements. each basic block that contains a return statement from the function will then be called a terminator block or terminator basic block. To detect all basic blocks that are terminator basic blocks (i.e. contain a return statement) do the following:\n\n```\nrunOnFunction {\n for BB in F:\n for I in BB:\n if (ReturnInst *RI = dyn_cast I)\n BB is terminator Basic Block\n endif\n endfor\n endfor\n}\n```\n\n========================================\n\nCode:\n```text\nrunOnFunction {\n   for BB in F:\n      for I in BB:\n          if (ReturnInst *RI = dyn_cast<ReturnInst> I)\n             BB is terminator Basic Block\n          endif\n      endfor\n   endfor\n}\n```\n\n========================================\n\nComments:\n- Can you give me an example of a function with more than one terminator BBs? Even if there are multiple returns, there should be a BB with a phi to \"collect\" all the return BBs, right?\n- Nope, in fact there's even a pass called UnifyFunctionExitNodes that does exactly what its name says, i.e. getting rid of multiple exits from a function","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":44,"estimatedTokens":356}}440{"id":"stack-593358","source":"stackoverflow","questionId":593358,"title":"SSA for stack machine code","tags":["compiler-construction","ssa","stack-machine"],"text":"Title: SSA for stack machine code\nTags: compiler-construction, ssa, stack-machine\nSource: Stack Overflow\n\nQuestion:\nI'm working on a compiler for a stack machine (specifically CIL) and I've parsed the code into a graph of basic blocks. From here I'm looking to apply SSA to the methods, but it's not going too well. My first attempt (while working with a flat listing, rather than the graph) was to iterate over the code and keep a stack of SSA ids (that is, for the assign targets), pushing them when I produce an assignment, popping them when they're used. This works just fine for a single basic block, but I simply can't figure out how to handle producing Φ functions.\n\nThe idea I've been tossing around is to attach a stack position to the SSA ids and then look at what's still on the stack when the code paths converge, but this doesn't seem like the Right Way (TM) of doing things.\n\nIs there a simple algorithm for tracking the stack manipulations across multiple code paths and determining the collisions when they converge?\n\n========================================\n\nCode:\n```text\nif (bExp)                  if (bExp)\n   x := 1                    x1 := 1\n else            SSA:       else\n   x := 2                    x2 := 2\n y := x;                    y := Phi(x1,x2)\n```\n\n```text\nif (bExp)\n  stackframe[y_index] = 1     (y_index being some offset)\nelse\n  stackframe[y_index] = 2\nnop\n```\n\n========================================\n\nComments:\n- Did anything ever come of the compiler? I'm thinking about doing precisely the same thing.\n- I was working on converting LLVM IR in SSA to stack machine code. Could u please comment on your experience here? Github link would be even better.\n- Thanks very much. I had 99% of this, but for some reason the stack position didn't seem to be enough. Between your answer and MS Research's Marmot compiler paper, I have it all now :)","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":36,"estimatedTokens":470}}441{"id":"stack-29588141","source":"stackoverflow","questionId":29588141,"title":"How do purely functional compilers annotate the AST with type info?","tags":["scala","haskell","compiler-construction","f#","functional-programming"],"text":"Title: How do purely functional compilers annotate the AST with type info?\nTags: scala, haskell, compiler-construction, f#, functional-programming\nSource: Stack Overflow\n\nQuestion:\nIn the syntax analysis phase, an imperative compiler can build an AST out of nodes that already contain a `type` field that is set to `null` during construction, and then later, in the semantic analysis phase, fill in the types by assigning the declared/inferred types into the `type` fields.\n\nHow do purely functional languages handle this, where you do not have the luxury of assignment? Is the type-less AST mapped to a different *kind* of type-enriched AST? Does that mean I need to define two types per AST node, one for the syntax phase, and one for the semantic phase?\n\nAre there purely functional programming tricks that help the compiler writer with this problem?\n\n========================================\n\nTop Answer:\nI usually rewrite a source (or an already several steps lowered) AST into a new form, replacing each `expression` node with a pair `(tag, expression)`. \n\nTags are unique numbers or symbols which are then used by the next pass which derives type equations from the AST. E.g., `a + b` will yield something like { `numeric(Tag_a).` `numeric(Tag_b).` `equals(Tag_a, Tag_b).` `equals(Tag_e, Tag_a).`}.\n\nThen types equations are solved (e.g., by simply running them as a Prolog program), and, if successful, all the tags (which are variables in this program) are now bound to concrete types, and if not, they're left as type parameters.\n\nIn a next step, our previous AST is rewritten again, this time replacing tags with all the inferred type information.\n\nThe whole process is a sequence of pure rewrites, no need to replace anything in your AST destructively. A typical compilation pipeline may take a couple of dozens of rewrites, some of them changing the AST datatype.\n\n========================================\n\nCode:\n```text\ntype\n```\n\n```text\nnull\n```\n\n```text\ntype\n```\n\n```hs\ndata Exp = Var Name (Maybe Type) | ...\nparse :: String -> Maybe Exp     -- types are Nothings here\ntypeCheck :: Exp -> Maybe Exp    -- turns Nothings into Justs\n```\n\n```hs\ndata Exp ty = Var Name ty | ...\nparse :: String -> Maybe (Exp ())\ntypeCheck :: Exp () -> Maybe (Exp Type)\n```\n\n```text\nexpression\n```\n\n```text\n(tag, expression)\n```\n\n```text\na + b\n```\n\n```text\nnumeric(Tag_a).\n```\n\n```text\nnumeric(Tag_b).\n```\n\n```text\nequals(Tag_a, Tag_b).\n```\n\n```text\nequals(Tag_e, Tag_a).\n```\n\n```text\ntype 'type AST;\n| MethodInvoke of 'type * Name * 'type list\n| ....\n```\n\n========================================\n\nComments:\n- How does lacking assignment cause a problem, exactly? I'm not sure I ...\n- The fact that it's an AST is nothing special; this \"problem\" is no different to any other situation where an imperative programmer would use mutation to update a structure.\n- @Ben Right, but the interesting twist is it's a recursive structure based on nodes.\n- The new C# and VB.NET compilers by Microsoft (codename Roslyn) are purely functional and open-source, in addition to being industrial-strength production-ready \"real-world\" compilers. They are also compilers for an imperative language written in an imperative language in a purely functional style. (Well, at least the data structures are immutable.)\n- @fredoverflow Yes, but I'm afraid that's not actually a twist. Recursive data structures are *incredibly* common in in functional programming, and the need to \"update\" them is a problem functional programmers encounter very early in their learning, in almost any domain (not just compilers). So to a certain extent, this question is yet another iteration of \"how does pure functional programming work without mutation?\".\n- @Ben Yes, adding a number to a tree of numbers is very common and well-understood. But how common is it that you need to add another field to the element type of the tree later? I'd imagine there are lots of possible solutions to this problem, but I have no practical experience.\n- @J&#246;rgWMittag It would be awesome if you could finde the time to condense Roslyn's approach into an answer :)\n- @fredoverflow Ah, I misunderstood then! But in that case you're making a false comparison between imperative and functional; you can only assign the `type` field to `null` in the imperative compiler because you already prepared for the need to later add type annotation, and included a `type` field in your ADT to initially be `null`. You can do *exactly* the same thing in a pure functional approach, by adding a type field that can be set to a value that indicates \"not yet tagged with a type\" (probably `Maybe TypeInfo` initially set to `Nothing`, in Haskellish terms).\n- @fredoverflow There are also lots of other ways (external tags, using generics to support an arbitrary \"extra\" field in your records). But those also have very little to do with imperative vs functional programming, and could be just as well used by an imperative compiler.\n- @Ben Sure, but the \"functional null\" approach seems super hacky to me. At least way more hacky than the \"imperative null\" approach :)\n- @fredoverflow Really? I find nulls *way* more hacky than than maybes! And imperative-ness has nothing to do with it. Anyway, I've diverged quite a way from commenting on the original question. :)","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":99,"estimatedTokens":1325}}442{"id":"stack-365012","source":"stackoverflow","questionId":365012,"title":"Where can I get material for learning EBNF?","tags":["compiler-construction","boost","parsing","bnf","ebnf"],"text":"Title: Where can I get material for learning EBNF?\nTags: compiler-construction, boost, parsing, bnf, ebnf\nSource: Stack Overflow\n\nQuestion:\nExtended Backus–Naur Form: **EBNF** \n\nI'm very new to parsing concepts. Where can I get sufficiently easy to read and material for writing a grammar for the boost::spirit library, which uses a grammar similar to EBNF?\n\nCurrently I am looking into EBNF from Wikipedia.\n\n========================================\n\nTop Answer:\nBNF itself is simple, but you need to get used to the way compiler writers think. They are not necessarily easy read, but following are lecture notes from UC Berkeley and Stanford.\n\n- Berkeley CS 164: Lecture Notes\n\n- Stanford CS143: Lecture Notes\n\n========================================\n\nCode:\n```text\nwhile\n```\n\n```text\nif\n```\n\n```text\nif-then-else\n```\n\n```text\ncase\n```\n\n========================================\n\nComments:\n- Do a search for \"Compiler Construction Niklaus Wirth\", it ought to return a link to where you can freely download the latest version of his excellent book. Or check out his home page cs.inf.ethz.ch/~wirth","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":43,"estimatedTokens":275}}443{"id":"stack-5819376","source":"stackoverflow","questionId":5819376,"title":"Java compiler API ClassLoader","tags":["java","compiler-construction","classloader"],"text":"Title: Java compiler API ClassLoader\nTags: java, compiler-construction, classloader\nSource: Stack Overflow\n\nQuestion:\nI am trying to use Java Compiler API to compile some java class. That class imports some packages from the jar files which can be loaded by context ClassLoader, let's call him X, which is NOT the system classloader. When I run the compilation, the compiler complains about not recognizing the imports. I have tried to specify the fileManager to pass the classloader, but it does not help. \n\nWhen compile method is called, it first prints \"CLASS LOADED\", so the context ClassLoader CAN find the dependency class. However, the compilation itself fails (I get \"Compilation FAILED\" message) and during the compilation I get errors like this:\n\n/path/to/my/Source.java:3: package my.dependency does not exist\nimport my.dependency.MyClass;\n^\n\nWhat am I doing wrong? What's the correct way to pass custom classloader to the compilationTask? I can't extract the URLs from the ClassLoader since it's not URLClassLoader.\n\nMy methods are here:\n\n```\npublic void compile(List filesToCompile) {\n JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();\n\n StandardJavaFileManager stdFileManager =\n compiler.getStandardFileManager(null, null, null);\n Iterable fileObjects = stdFileManager\n .getJavaFileObjectsFromFiles(filesToCompile);\n\n FileManagerImpl fileManager = new FileManagerImpl(stdFileManager);\n\n CompilationTask task = compiler.getTask(null, fileManager, null, null, null, fileObjects);\n Boolean result = task.call();\n if (result == true) {\n System.out.println(\"Compilation has succeeded\");\n } else {\n System.out.println(\"Compilation FAILED\");\n }\n}\n\nprivate final class FileManagerImpl extends ForwardingJavaFileManager {\n\n public FileManagerImpl(JavaFileManager fileManager) {\n super(fileManager);\n }\n\n @Override\n public ClassLoader getClassLoader(JavaFileManager.Location location) {\n ClassLoader def = getContextClassLoader();\n try {\n def.loadClass(\"my.dependency.MyClass\");\n System.out.println(\"CLASS LOADED\");\n } catch (ClassNotFoundException ex) {\n System.out.println(\"NOT LOADED\");\n }\n return def;\n }\n}\n```\n\n========================================\n\nTop Answer:\nThe main point is that, while a class loader loads classes, javac will call `JavaFileManager#list()` to get a listing of all the files in a package.\n\nSo to use a custom class loader you need to modify (or extend) it to override `JavaFileManager#list()`. Hopefully you can reuse some of the logic used for class loading.\n\nYou might want to use your own implementations of `JavaFileObject` to represent class objects. You will then need to override `JavaFileManager#inferBinaryName()` (else the javac version will crash). Your implementations of `JavaFileObject` also needs to override (at least) `JavaFileObject#openInputStream`.\n\nHere are some pointers: http://atamur.blogspot.be/2009/10/using-built-in-javacompiler-with-custom.html\n\nAlso, don't make your life harder than it should and extend `ForwardingJavaFileManager` and `SimpleJavaFileObject`.\n\nFor reference, here is an example implementation:\n\n```\n@Override public Iterable list(Location location,\n String packageName, Set kinds, boolean recurse)\n throws IOException\n {\n Iterable stdResults =\n fileManager.list(location, packageName, kinds, recurse);\n\n if (location != StandardLocation.CLASS_PATH\n || !kinds.contains(JavaFileObject.Kind.CLASS))\n {\n return stdResults;\n }\n\n Set additional = pkgObjects.get(packageName);\n\n if (additional == null || additional.isEmpty()) {\n return stdResults;\n }\n\n List out = new ArrayList<>();\n\n for (JavaFileObject obj : additional) {\n out.add(obj);\n }\n for (JavaFileObject obj : stdResults) {\n out.add(obj);\n }\n\n return out;\n }\n```\n\nWhere `pkgObjects` is a map from package names to `JavaFileObject`. The way you fill this map depends on how your class loader works.\n\n========================================\n\nCode:\n```text\npublic void compile(List<File> filesToCompile) {\n       JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();\n\n       StandardJavaFileManager stdFileManager =\n               compiler.getStandardFileManager(null, null, null);\n       Iterable<? extends JavaFileObject> fileObjects = stdFileManager\n               .getJavaFileObjectsFromFiles(filesToCompile);\n\n       FileManagerImpl fileManager = new FileManagerImpl(stdFileManager);\n\n       CompilationTask task = compiler.getTask(null, fileManager, null, null, null, fileObjects);\n       Boolean result = task.call();\n       if (result == true) {\n           System.out.println(\"Compilation has succeeded\");\n       } else {\n           System.out.println(\"Compilation FAILED\");\n       }\n}\n\nprivate final class FileManagerImpl extends ForwardingJavaFileManager<JavaFileManager> {\n\n      public FileManagerImpl(JavaFileManager fileManager) {\n           super(fileManager);\n      }\n\n      @Override\n      public ClassLoader getClassLoader(JavaFileManager.Location location) {\n          ClassLoader def = getContextClassLoader();\n          try {\n               def.loadClass(\"my.dependency.MyClass\");\n               System.out.println(\"CLASS LOADED\");\n          } catch (ClassNotFoundException ex) {\n               System.out.println(\"NOT LOADED\");\n          }\n          return def;\n      }\n}\n```\n\n```text\ngetTask()\n```\n\n```text\n@Override public Iterable<JavaFileObject> list(Location location,\n    String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse)\n  throws IOException\n  {\n    Iterable<JavaFileObject> stdResults =\n      fileManager.list(location, packageName, kinds, recurse);\n\n    if (location != StandardLocation.CLASS_PATH\n    ||  !kinds.contains(JavaFileObject.Kind.CLASS))\n    {\n        return stdResults;\n    }\n\n    Set<JavaFileObject> additional = pkgObjects.get(packageName);\n\n    if (additional == null || additional.isEmpty()) {\n      return stdResults;\n    }\n\n    List<JavaFileObject> out = new ArrayList<>();\n\n    for (JavaFileObject obj : additional) {\n      out.add(obj);\n    }\n    for (JavaFileObject obj : stdResults) {\n      out.add(obj);\n    }\n\n    return out;\n  }\n```\n\n```text\nJavaFileManager#list()\n```\n\n```text\nJavaFileManager#list()\n```\n\n```text\nJavaFileObject\n```\n\n```text\nJavaFileManager#inferBinaryName()\n```\n\n```text\nJavaFileObject\n```\n\n```text\nJavaFileObject#openInputStream\n```\n\n```text\nForwardingJavaFileManager\n```\n\n```text\nSimpleJavaFileObject\n```\n\n```text\npkgObjects\n```\n\n```text\nJavaFileObject\n```\n\n========================================\n\nComments:\n- Thanks for the answer. But that's not exactly what I need. I need to compile *.java source and let the compiler know how to load the dependency package.\n- Thanks a lot, I'll check it out. However, isn't there any way to set just the existing classLoader?\n- @Pavel S. - I doubt it, as it makes sense, to provide the compiler tool with a classpath that is separate from the actual application classpath. Imagine, you have one version of a class loaded but want to use another one for compiling the classes. This *requires* class separation.","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":233,"estimatedTokens":1750}}444{"id":"stack-3373817","source":"stackoverflow","questionId":3373817,"title":"Building a compiler or interpreter using Python","tags":["java","python","compiler-construction","programming-languages","interpreter"],"text":"Title: Building a compiler or interpreter using Python\nTags: java, python, compiler-construction, programming-languages, interpreter\nSource: Stack Overflow\n\nQuestion:\nRight now I'm writing my PhD proposal to build a language processor for a new specification language for Java (cf. JML, or Spec# for C#) and need to nail down an implementation tool to start development. The research aspects of the language (syntax, semantics, theoretical results) are orthogonal to my choice of implementation, so I'd like to use Python (2.6+) for my own reasons. The end-product will be *either* a compiler or interpreter capable of verifying some specified properties for programs written in Java.\n\nWhat's the best framework/library for building compilers/interpreters in Python? Are the \"batteries included\" for this problem?\n\n**Bonus points awarded to solutions that have reference compilers for Java 6+.**\n\n========================================\n\nTop Answer:\nI personally can't stand antlr, I use lex/yacc as my parser generator. Here is a Python implementation http://www.dabeaz.com/ply/ that you could use.\n\nThat just deals with parsing though, that really doesn't even begin to construct your *interpreter*. For that, you'll probably be building it from the ground up - I've never heard of a library specifically geared towards this (I would be excited to see some of them, please link me there in the comments if you know of any).\n\nCheck out this SO post how to start writing a very simple programming language it has good ideas.il.\n\n========================================\n\nComments:\n- I know you want to use Python, but if you're looking for the \"batteries included\" you're going to be best off with F#... that language seems built for building compilers/interpreters.\n- @gnucom: Not only F#, but every language of the ML family is well-suited for language implementation. In fact, \"ML\" stands for \"Meta Language\". Haskell has some cool parser libraries (parsec), too.\n- @delnan: Absolutely. I stand corrected. :)\n- Are you \"verifying some specified properties\" at runtime or compile time? Just curious.\n- While it is probably not directly responsive, I'll note that the canonical compiler references question is Learning to write a compiler.\n- @uncle brad: the proof of concept that gets me a PhD may end up doing neither, but I'm hoping for some compile-time (interpretation-time?) results first\n- Why do you recommend the ANTLRv2 Python runtime when one also exists for ANTLRv3?\n- Particularly, what tradeoffs are there for Python that aren't already covered in this question? stackoverflow.com/questions/633224/antlr2-vs-antlr3\n- Ok, neither seem robust enough for this research. Thanks for pointing me to ANTLR+Python, though!\n- I'm picky about the quality of error reporting, so I prefer Anders' ANTLR-based suggestions to these.","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":33,"estimatedTokens":709}}445{"id":"stack-34306069","source":"stackoverflow","questionId":34306069,"title":"When writing code compiled by LLVM backend, does architecture matter?","tags":["linux","compiler-construction","llvm"],"text":"Title: When writing code compiled by LLVM backend, does architecture matter?\nTags: linux, compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nMy question is actually more general than the title:\n\nAt what point does the architecture matter when writing code that will eventually be compiled to LLVM intermediary code, and then from there to the machine language?\n\nLet's say I'm writing Rust (which uses LLVM as a backend). Am I automatically capable of compiling my Rust code to every architecture that LLVM can target (assuming there's an OS on that machine that can run it)?\n\nOr could it be that the Rust standard library hasn't been made \"ARM compatible\" yet, so I couldn't compile to ARM even if the LLVM targets it?\n\nWhat if I don't use any of the standard library, my entire program is just a program that returns right away? Could it be the case that even without any libraries, Rust (or what have you) can't compile to ARM (or what have you) even if the LLVM targets it?\n\nIf all the above examples compile just fine, what do I have to do to get my code to break on one architecture not compile to a certain architecture?\n\nBonus question of the same variety:\n\n- Let's say the standard library makes use of OS system calls (which is surely does). Do you have to care about architecture when making system calls? Or does the OS (Linux, for example) abstract away architecture as well?\n\nThanks.","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":22,"estimatedTokens":352}}446{"id":"stack-22776391","source":"stackoverflow","questionId":22776391,"title":"Why does Clang coerce struct parameters to ints","tags":["compiler-construction","struct","clang","llvm","abi"],"text":"Title: Why does Clang coerce struct parameters to ints\nTags: compiler-construction, struct, clang, llvm, abi\nSource: Stack Overflow\n\nQuestion:\nWhen using struct parameters in a function, clang will change the function signature. Instead of using a struct type, the signature will be a coerced int of equal size. In my compiler project, I use the llvm struct type for the method signature (which seems more logical).\n\nThis wouldn't be a problem, except for the fact that resulting assembly produced by LLVM when using the struct or coerced types are different and not *call compatible*. This results in my compiler not being ABI compatible with C functions with structs.\n\n*Why does clang do this? Is this something specified in the C ABI?*\n\nHere's a simple example C source file:\n\n```\nstruct TwoInt { int a, b; };\n\nstruct EightChar { char a, b, c, d, e, f, g, h; };\n\nvoid doTwoInt(struct TwoInt a) {}\n\nvoid doEightChar(struct EightChar a) {}\n\nint main()\n{\n struct TwoInt ti;\n struct EightChar fc;\n\n doTwoInt(ti);\n doEightChar(fc);\n\n return 0;\n}\n```\n\nResulting LLVM-IR from Clang\n\n```\n%struct.TwoInt = type { i32, i32 }\n%struct.EightChar = type { i8, i8, i8, i8, i8, i8, i8, i8 }\n\ndefine void @doTwoInt(i64 %a.coerce) nounwind uwtable {\n %a = alloca %struct.TwoInt, align 8\n %1 = bitcast %struct.TwoInt* %a to i64*\n store i64 %a.coerce, i64* %1, align 1\n ret void\n}\n\ndefine void @doEightChar(i64 %a.coerce) nounwind uwtable {\n %a = alloca %struct.EightChar, align 8\n %1 = bitcast %struct.EightChar* %a to i64*\n store i64 %a.coerce, i64* %1, align 1\n ret void\n}\n\ndefine i32 @main() nounwind uwtable {\n %1 = alloca i32, align 4\n %ti = alloca %struct.TwoInt, align 4\n %fc = alloca %struct.EightChar, align 1\n store i32 0, i32* %1\n %2 = bitcast %struct.TwoInt* %ti to i64*\n %3 = load i64* %2, align 1\n call void @doTwoInt(i64 %3)\n %4 = bitcast %struct.EightChar* %fc to i64*\n %5 = load i64* %4, align 1\n call void @doEightChar(i64 %5)\n ret i32 0\n}\n```\n\nWhat I would've expected (and what my compiler outputs):\n\n```\n%TwoInt = type { i32, i32 }\n%EightChar = type { i8, i8, i8, i8, i8, i8, i8, i8 }\n\ndefine void @doTwoInt(%TwoInt %a) {\n %1 = alloca i32\n %2 = alloca %TwoInt\n store %TwoInt %a, %TwoInt* %2\n ret void\n}\n\ndefine void @doEightChar(%EightChar %a) {\n %1 = alloca i32\n %2 = alloca %EightChar\n store %EightChar %a, %EightChar* %2\n ret void\n}\n\ndefine i32 @main() {\n %1 = alloca i32\n %ti = alloca %TwoInt\n %fc = alloca %EightChar\n %2 = load %TwoInt* %ti\n call void @doTwoInt(%TwoInt %2)\n %3 = load %EightChar* %fc\n call void @doEightChar(%EightChar %3)\n ret i32 0\n}\n```\n\n========================================\n\nCode:\n```text\nstruct TwoInt { int a, b; };\n\nstruct EightChar { char a, b, c, d, e, f, g, h; };\n\nvoid doTwoInt(struct TwoInt a) {}\n\nvoid doEightChar(struct EightChar a) {}\n\nint main()\n{\n        struct TwoInt ti;\n        struct EightChar fc;\n\n        doTwoInt(ti);\n        doEightChar(fc);\n\n        return 0;\n}\n```\n\n```text\n%struct.TwoInt = type { i32, i32 }\n%struct.EightChar = type { i8, i8, i8, i8, i8, i8, i8, i8 }\n\ndefine void @doTwoInt(i64 %a.coerce) nounwind uwtable {\n  %a = alloca %struct.TwoInt, align 8\n  %1 = bitcast %struct.TwoInt* %a to i64*\n  store i64 %a.coerce, i64* %1, align 1\n  ret void\n}\n\ndefine void @doEightChar(i64 %a.coerce) nounwind uwtable {\n  %a = alloca %struct.EightChar, align 8\n  %1 = bitcast %struct.EightChar* %a to i64*\n  store i64 %a.coerce, i64* %1, align 1\n  ret void\n}\n\ndefine i32 @main() nounwind uwtable {\n  %1 = alloca i32, align 4\n  %ti = alloca %struct.TwoInt, align 4\n  %fc = alloca %struct.EightChar, align 1\n  store i32 0, i32* %1\n  %2 = bitcast %struct.TwoInt* %ti to i64*\n  %3 = load i64* %2, align 1\n  call void @doTwoInt(i64 %3)\n  %4 = bitcast %struct.EightChar* %fc to i64*\n  %5 = load i64* %4, align 1\n  call void @doEightChar(i64 %5)\n  ret i32 0\n}\n```\n\n```text\n%TwoInt = type { i32, i32 }\n%EightChar = type { i8, i8, i8, i8, i8, i8, i8, i8 }\n\ndefine void @doTwoInt(%TwoInt %a) {\n  %1 = alloca i32\n  %2 = alloca %TwoInt\n  store %TwoInt %a, %TwoInt* %2\n  ret void\n}\n\ndefine void @doEightChar(%EightChar %a) {\n  %1 = alloca i32\n  %2 = alloca %EightChar\n  store %EightChar %a, %EightChar* %2\n  ret void\n}\n\ndefine i32 @main() {\n  %1 = alloca i32\n  %ti = alloca %TwoInt\n  %fc = alloca %EightChar\n  %2 = load %TwoInt* %ti\n  call void @doTwoInt(%TwoInt %2)\n  %3 = load %EightChar* %fc\n  call void @doEightChar(%EightChar %3)\n  ret i32 0\n}\n```\n\n```text\nclang -O0 -emit-llvm\n```\n\n========================================\n\nComments:\n- How clang was started? What is your OS and ABI? The ABI is defined not in C/C++ standard, but by OS and its libs, and there is possibility, that your compiler works not according to ABI.\n- Good overview with links to more detailed pages: What is an application binary interface (ABI)?\n- I guess the real question is why is the \"type lowering\" done at the llvm-ir level and not the assembly? Is there a reason that front-end compiler writers have to manage the ABI and not LLVM's native code generators?\n- @Justin: LLVM IR has no way to represent the ABI rules required to properly lower calls. So the task is left to the front-end to produce precise ABI-specific IR sequences.\n- @EliBendersky so the Calling Conventions IR attributes are not enough to specify how parameters are passed? The docs aren't entirely clear on this point.\n- @Justin: nope. The front-end performs full ABI lowering like the Itanium ABI\n- @EliBendersky doesn't that break the \"intermediate\" concept of llvm-ir? I would expect it to have no knowledge of ABI, and generated code to be platform-independant.","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":202,"estimatedTokens":1392}}447{"id":"stack-3616950","source":"stackoverflow","questionId":3616950,"title":"Formally constructing Control Flow Graph","tags":["language-agnostic","compiler-construction","scala","compiler-theory"],"text":"Title: Formally constructing Control Flow Graph\nTags: language-agnostic, compiler-construction, scala, compiler-theory\nSource: Stack Overflow\n\nQuestion:\nIm writing a compiler for university project, and I would like to transform my Abstract Syntax Tree into a Control Flow Graph(CFG).\n\nIm thinking that the nodes(`V`) in the CFG should be nodes from the AST. I know algorithmically how to construct the edge set (`G=(V,E)`) but Im having a hard time writing the process a bit more formally\n\nI've created this scala style pattern matching (Pseudo):\n\n```\ndef edges(n:Node)(nestedin_next: Node) : List[(Node,Node)] = \n n match {\n case (c_1 :: c_2::tl) => (c1,c2) :: edges(c2::tl)(nestedin_next)++\n edges(c_1)(c_2)//recurse\n case c_1 :: Nil => (c_1,nestedin_next)::Nil\n case i@ IF(_,c1,c2) => (i,c1)::(i,c2)::edges(c1)(nestedin_next)++\n edges(c2)(nestedin_next)\n case _ => Nil\n }\n```\n\nWhich should match an AST structure like:\n\n```\n( IF(1,\n ASSIGN(x,1), // ia1\n ASSIGN(x,2) // ia2\n ) :: // i1\n ASSIGN(y,2) :: // a1\n ASSIGN(z,ADD(x,y)) :: //a2 \n IF(z, \n RET(z), //i2r1\n assign(z,0):: // i2a1\n ret(z) // i2r2\n ) :://i2\n Nil\n)\n```\n\nand provide an edgeset like:\n\n```\n{ i1 -> ia1,\n i1 -> ia2,\n ia1 -> a1,\n ia2 -> a1,\n a1 -> a2,\n a2 -> i2,\n i2 -> i2r1\n i2-> i2a1\n i2a1 -> i2r2\n i2r2 -> _|_\n i2r1 -> _|_ \n}\n```\n\nDotSrc\n\nAnyone got any hints on how to do this a bit more formally than scala \"pseudocode\"?\n\nIm thinking something inductive like:\n\n```\ne[[ IF(_,b1,b2) ]] = (if -> b1) + (if -> b2) \\cup e[[ b1 ]] \\cup e[[ b2 ]]\ne[[ b1, b2 ]] = e[[b1]] \\cup e[[b2]]\n```\n\n(the above would only give a tree and not a graph though. No edge from edge of then-branch to next statement for example)\n\nEDIT:\n\nI've been reading up on kiama and dataflows for scala, and I like the \"succ\" and \"following\" approach they use. Nevertheless, I'm having a hard time boiling that down into a more formal description, mostly because of the nifty `childAttr`, `s.next` which hides some of the details that turns ugly when I try to specify it formally.\n\nEDIT2: \n\nI've been through the Dragon Book and \"Modern Compiler Implementation in ML\" as well as some of the other material from Learning to write a compiler and some/most mentions data flow and control flow, but never touches much upon HOW to create the CFG in any formal way.\n\nEDIT3:\n\nVia Kiama author, Associate Professor Dr. Tony Sloane I recieved some additional book references to look up.\n\nAs far as I can see the \"way to do it\" as per those books is based on a \"per statement\" of the program more than over the AST and is based on Basic Blocks. Great input nevertheless!\n\n========================================\n\nTop Answer:\nGoogle's Closure Compiler implements a Control-Flow Analysis which transforms an AST for JavaScript into a Control-Flow Graph. The ideas for this implementation are inspired from the paper: Declarative Intraprocedural Flow Analysis of Java Source Code.\n\n========================================\n\nCode:\n```text\ndef edges(n:Node)(nestedin_next: Node) : List[(Node,Node)] = \n    n match {\n       case (c_1 :: c_2::tl) => (c1,c2) :: edges(c2::tl)(nestedin_next)++\n                                   edges(c_1)(c_2)//recurse\n       case c_1 :: Nil => (c_1,nestedin_next)::Nil\n       case  i@ IF(_,c1,c2) => (i,c1)::(i,c2)::edges(c1)(nestedin_next)++\n                                edges(c2)(nestedin_next)\n       case _ => Nil\n     }\n```\n\n```text\n( IF(1,\n       ASSIGN(x,1), // ia1\n       ASSIGN(x,2) // ia2\n     ) ::  // i1\n  ASSIGN(y,2) ::  // a1\n  ASSIGN(z,ADD(x,y)) :: //a2 \n  IF(z, \n       RET(z), //i2r1\n         assign(z,0):: // i2a1\n         ret(z) // i2r2\n  ) :://i2\n   Nil\n)\n```\n\n```text\n{ i1 -> ia1,\n   i1 -> ia2,\n   ia1 -> a1,\n   ia2 -> a1,\n   a1 -> a2,\n   a2 -> i2,\n   i2 -> i2r1\n   i2-> i2a1\n   i2a1 -> i2r2\n   i2r2 -> _|_\n   i2r1 -> _|_ \n}\n```\n\n```text\ne[[ IF(_,b1,b2) ]] = (if -> b1) + (if -> b2) \\cup e[[ b1 ]] \\cup e[[ b2 ]]\ne[[ b1, b2 ]] = e[[b1]] \\cup e[[b2]]\n```\n\n```text\nV\n```\n\n```text\nG=(V,E)\n```\n\n```text\nchildAttr\n```\n\n```text\ns.next\n```\n\n========================================\n\nComments:\n- I hope you don't mind that I added \"scala\" to the tags.\n- @Randall Not at all :) I almost did so my self\n- Very good input! Regarding the operational semantics (and inference rules), they have been on my mind a lot lately, so it's interesting that you mention it.\n- Aha! Kiama is based JastAdd, and this paper uses JastAdd. The dataflow examples of Kiama looks very much related to the approach used in the paper. Thanks","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":164,"estimatedTokens":1122}}448{"id":"stack-46022919","source":"stackoverflow","questionId":46022919,"title":"What are [Yield, Await, In, Return] in EcmaScript grammar","tags":["javascript","ecmascript-6","compiler-construction","context-free-grammar"],"text":"Title: What are [Yield, Await, In, Return] in EcmaScript grammar\nTags: javascript, ecmascript-6, compiler-construction, context-free-grammar\nSource: Stack Overflow\n\nQuestion:\nMany productions in EcmaScript are given with the following \"modifiers\":\n\n```\n[Yield, Await, In, Return]\n```\n\nHere are a few examples:\n\n```\nArrayLiteral[Yield, Await]:\n ...\n\nElementList[Yield, Await]:\n ... AssignmentExpression[+In, ?Yield, ?Await]\n```\n\nI've searched through the spec for the explanation, specifically `Grammar Notation` section, but can't find it. It should be there. Can someone please point me to the relevant paragraph and maybe provide a short explanation?\n\n========================================\n\nCode:\n```text\n[Yield, Await, In, Return]\n```\n\n```text\nArrayLiteral[Yield, Await]:\n  ...\n\nElementList[Yield, Await]:\n ... AssignmentExpression[+In, ?Yield, ?Await]\n```\n\n```text\nGrammar Notation\n```\n\n```text\nStatementList[Return]:\n   ReturnStatement\n   ExpressionStatement\n```\n\n```text\nStatementList:\n   ReturnStatement\n   ExpressionStatement\nStatementList_Return:\n   ReturnStatement\n   ExpressionStatement\n```\n\n```text\nStatementList[Return, In]:\n   ReturnStatement\n   ExpressionStatement\n```\n\n```text\nStatementList:\n   ReturnStatement\n   ExpressionStatement\nStatementList_Return:\n   ReturnStatement\n   ExpressionStatement\nStatementList_In:\n   ReturnStatement\n   ExpressionStatement\nStatementList_Return_In:\n   ReturnStatement\n   ExpressionStatement\n```\n\n```text\n[Return]\n```\n\n```text\n[Yield, Await, Return]\n```\n\n```text\n[?Yield, ?Await, ?Return]\n```\n\n```text\nin\n```\n\n```text\nin\n```\n\n```text\nfor\n```\n\n========================================\n\nComments:\n- Section 5.1.5, \"Grammar Notation\".\n- See stackoverflow.com/a/44829697/1566221\n- @rici, thanks a lot for the link!\n- thanks, can you also please explain what this means `AssignmentExpression[+In, ?Yield, ?Await]`? Why is there `+` and `?` when referencing the non-terminal `AssignmentExpression`?\n- @AngularInDepth.com: That's all explained in Section 5.1.5.\n- ok, thanks, I'll read it thoroughly and come back with a clarifying questions if any\n- I'm not a native English speaker. To understand the above 5.1 introduces it's so difficult for me....\n- @toffee - It can be difficult even for native English speakers! :-) I suggest posting a question that refers to the section quoted above (maybe even quoting it) and perhaps also refers to this answer, and saying what specifically you don't understand. Hopefully someone can explain that part in a way that helps you -- and others after you. :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.798Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":13,"totalLines":113,"estimatedTokens":636}}449{"id":"stack-5120320","source":"stackoverflow","questionId":5120320,"title":"AST traversal in visitor or in the nodes?","tags":["c++","compiler-construction","abstract-syntax-tree","visitor-pattern"],"text":"Title: AST traversal in visitor or in the nodes?\nTags: c++, compiler-construction, abstract-syntax-tree, visitor-pattern\nSource: Stack Overflow\n\nQuestion:\n**Update** accepted Ira Baxter's answer since it pointed me into the right direction: I first figured out what I actually needed by starting the implementation of the compiling stage, and it became obvious pretty soon that traversal within the nodes made thie an impossible approach. Not all nodes should be visited, and some of them in reverse order (for example, first the rhs of an assignment so the compiler can check if the type matches with the rhs/operator). Putting traversal in the visitor makes this all very easy.\n\nI'm playing around with ASTs and the likes before deciding a major refactory of the handling of a mini-language used in an applicaiton.\nI've built a Lexer/Parser and can get the AST just fine. There's also a Visitor and as concrete implementation I made an ASTToOriginal which just recreates the original source file. Eventually there's goin to be some sort of compiler that also implements the Vsisitor and creates the actual C++ code at runtime so I want to make sure everything is right from the start.\nWhile everything works fine now, there is some similar/duplicate code since the traversal order is implemented in the Visitor itself.\n\nWhen looking up more information, it seems that some implementations prefer keeping the traversal order in the visited objects themselves instead, in order not to repeat this in each concrete visitor.\nEven the GoF only talks briefly about this, in the same way. So I wanted to give this approach a try as well but got stuck pretty soon.. Let me explain.\n\nSample source line and corresponding AST nodes:\n\n```\nif(t>100?x=1;sety(20,true):x=2)\nConditional\n BinaryOp\n left=Variable [name=t], operator=[>], right=Integer [value=100]\n IfTrue\n Assignment\n left=Variable [name=x], operator=[=], right=Integer [value=1] \n Method\n MethodName [name=sety], Arguments( Integer [value=20], Boolean [value=true] )\n IfFalse\n Assignment\n left=Variable [name=x], operator=[=], right=Integer [value=1]\n```\n\nSome code:\n\n```\nclass BinaryOp {\n void Accept( Visitor* v ){ v->Visit( this ); }\n Expr* left;\n Op* op;\n Expr* right;\n}; \nclass Variable {\n void Accept( Visitor* v ){ v->Visit( this ); }\n Name* name;\n};\nclass Visitor { //provide basic traversal, terminal visitors are abstract\n void Visit( Variable* ) = 0;\n void Visit( BinaryOp* p ) {\n p->left->Accept( this );\n p->op->Accept( this );\n p->right->Accept( this ); \n }\n void Visit( Conditional* p ) {\n p->cond->Accept( this );\n VisitList( p->ifTrue ); //VisitList just iterates over the array, calling Accept on each element\n VisitList( p->ifFalse );\n }\n};\n```\n\nImplementing ASTToOriginal is pretty straightforward: all abstract Visitor methods just print out the name or value member of the terminal.\nFor the non-terminals it depends; printing an Assignment works ok with the default Visitor traversal, for a Conditional extra code is needed:\n\n```\nclass ASTToOriginal {\n void Visit( Conditional* p ) {\n str cond->Accept( this );\n str ifTrue, AppendText( str, \";\" ) );\n VisitListWithPostOp( p->ifFalse, AppendText( str, \";\" ) );\n str So as one can see both the Visit methods for a Conditional in Visitor and ASTToOriginal are indeed very similar.\nHowever trying to solve this by putting traversal into the nodes made things not just worse, but rather a complete mess.\nI tried an approach with PreVisit and PostVisit methods which solved some problems, but just introduced more and more code into the Nodes.\nIt also started to look like I would have to keep track of a number of states inside the Visitor to be able to know when to add closing brackets etc.\n\n```\nclass BinaryOp {\n void Accept( Conditional* v ) { \n v->Visit( this );\n op->Accept( v )\n VisitList( ifTrue, v );\n VisitList( ifFalse, v );\n};\nclass Vistor {\n //now all methods are pure virtual\n};\nclass ASTToOriginal {\n void Visit( Conditional* p ) {\n str **Question**: is this approach just not suited for my case, or am I overlooking something essential? Is there a common design to cope with these problems? What if I also need traversal in a different direction?\n\n========================================\n\nTop Answer:\nFor recursive generic traversing of trees, Visitor and Composite are usually used together, like in (first relevant google link) there. I first read about this idea there. There are also visitor combinators which are a nice idea.\n\nAnd by the way...\n\nthis is where functional languages shine, with their Algebraic Data Types and pattern matching. If you can, switch to a functional language. Composite and Visitor are only ugly workarounds for lack of language support for respectively ADT and pattern matching.\n\n========================================\n\nCode:\n```text\nif(t>100?x=1;sety(20,true):x=2)\nConditional\n  BinaryOp\n    left=Variable [name=t], operator=[>], right=Integer [value=100]\n  IfTrue\n    Assignment\n      left=Variable [name=x], operator=[=], right=Integer [value=1] \n    Method\n      MethodName [name=sety], Arguments( Integer [value=20], Boolean [value=true] )\n  IfFalse\n    Assignment\n      left=Variable [name=x], operator=[=], right=Integer [value=1]\n```\n\n```text\nclass BinaryOp {\n  void Accept( Visitor* v ){ v->Visit( this ); }\n  Expr* left;\n  Op* op;\n  Expr* right;\n};    \nclass Variable {\n  void Accept( Visitor* v ){ v->Visit( this ); }\n  Name* name;\n};\nclass Visitor { //provide basic traversal, terminal visitors are abstract\n  void Visit( Variable* ) = 0;\n  void Visit( BinaryOp* p ) {\n    p->left->Accept( this );\n    p->op->Accept( this );\n    p->right->Accept( this );        \n  }\n  void Visit( Conditional* p ) {\n    p->cond->Accept( this );\n    VisitList( p->ifTrue ); //VisitList just iterates over the array, calling Accept on each element\n    VisitList( p->ifFalse );\n  }\n};\n```\n\n```text\nclass ASTToOriginal {\n  void Visit( Conditional* p ) {\n    str << \"if(\";\n    p->cond->Accept( this );\n    str << \"?\";\n      //VisitListWithPostOp is like VisitList but calls op for each *except the last* iteration\n    VisitListWithPostOp( p->ifTrue, AppendText( str, \";\" ) );\n    VisitListWithPostOp( p->ifFalse, AppendText( str, \";\" ) );\n    str << \")\";\n  }\n};\n```\n\n```text\nclass BinaryOp {\n  void Accept( Conditional* v ) { \n    v->Visit( this );\n    op->Accept( v )\n    VisitList( ifTrue, v );\n    VisitList( ifFalse, v );\n};\nclass Vistor {\n  //now all methods are pure virtual\n};\nclass ASTToOriginal {\n  void Visit( Conditional* p ) {\n    str << \"if(\";\n    //now what??? after returning here, BinaryOp will visit the op automatically so I can't insert the \"?\"\n    //If I make a PostVisit( BinaryOp* ), and call it it BinaryOp::Accept, I get the chance to insert the \"?\",\n    //but now I have to keep a state: my PostVisit method needs to know it's currently being called as part of a Conditional\n    //Things are even worse for the ifTrue/ifFalse statement arrays: each element needs a \";\" appended, but not the last one,\n    //how am I ever going to do that in a clean way?\n  }\n};\n```\n\n```text\nmethod = declarations statements ;\n  <<ResolveSymbols>>: { declarations.parentsymbols=method.symboltable;\n                        statements.symboltable = declarations.symboltable;\n                      }\n```\n\n========================================\n\nComments:\n- wouldn't that totally destroy the entire concept of the visitor pattern? Also, visitor has nothing to do with creating objects. I think you completely misunderstood the question.\n- Thanks for the comment. In thinking about the problem, it seemed easier to use a Walkabout pattern. I updated my answer accordingly.\n- It seems that the performance of the Walkabout pattern is terrible. Visitors are not perfect, but they solve the \"locality of the definitions\" problem, which would have much better been solved in the first place by using a **functional language** and **pattern matching**.\n- @stijn: I think it could be done by putting member fields in a list or some other type of data structure within the class. However, that seems a solution where \"when all I have is a hammer, everything looks like a nail.\" So it would probably not be a good fit for a C++ solution.\n- as an answer to your questions: all nodes should be visited, but it's not clear to me yet in what order, as I also am not sure what I'm going to do with the AST exactly. Maybe for that reason alone I should stick with the order in the Visitor for now. At least then I can later, without having to modify a node, still change order.\n- @Puchacz yes see the update I added to the question itself","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":194,"estimatedTokens":2148}}450{"id":"stack-4468086","source":"stackoverflow","questionId":4468086,"title":"Any tools can randomly generate the source code according to a language grammar?","tags":["random","compiler-construction","context-free-grammar"],"text":"Title: Any tools can randomly generate the source code according to a language grammar?\nTags: random, compiler-construction, context-free-grammar\nSource: Stack Overflow\n\nQuestion:\nA C program source code can be parsed according to the C grammar(described in CFG) and eventually turned into many ASTs. I am considering if such tool exists: it can do the reverse thing by firstly randomly generating many ASTs, which include tokens that don't have the concrete string values, just the types of the tokens, according to the CFG, then generating the concrete tokens according to the tokens' definitions in the regular expression.\n\nI can imagine the first step looks like an iterative non-terminals replacement, which is randomly and can be limited by certain number of iteration times. The second step is just generating randomly strings according to regular expressions.\n\nIs there any tool that can do this?\n\n========================================\n\nTop Answer:\nGiven a context-free grammar of a language, it is possible to generate a random string that matches the grammar. \n\nFor example, the *nearley* parser generator includes an implementation of an \"unparser\" that can generate strings from a grammar.\n\nThe same task can be accomplished using *definite clause grammars* in Prolog. An example of a sentence generator using definite clause grammars is given here.\n\n========================================\n\nCode:\n```text\nLHS = RHS1 RHS2 ...  RHSn ;\n```\n\n```text\nRepeat until no nonterminals are left:\n    Pick a nonterminal N in the tree;\n       Expand by adding children for the right hand side of any rule\n       whose left-hand side matches the nonterminal N\n```\n\n========================================\n\nComments:\n- But.... why? :p I suppose you could - if you the CFG specification you are guaranteed to come up with at-least-syntactically valid C code. But I don't think anybody's ever tried to do such a thing - you'd probably need to write some sort of reverse parser...\n- I've worked with a few ... (this is a joke; I love all my colleages).\n- is there any reason one would want random, nonsensical source code that would (most likely) do nothing?\n- Yes, eg. a random test case generator for compiler/interpreter, or just for fun.\n- Note that beyond fuzz testing, it becomes hard to maintain the correctness of the test cases to ensure contextual correctness (e.g. no repeated variable declarations in the same scope) or type correctness\n- nearley unparser: github.com/kach/nearley/blob/master/lib/unparse.js","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":43,"estimatedTokens":630}}451{"id":"stack-6276976","source":"stackoverflow","questionId":6276976,"title":"C compiler written in java","tags":["java","c","compiler-construction"],"text":"Title: C compiler written in java\nTags: java, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI want to integrate a c language compiler in to java application to compile c sources without file creation (Like Java Compiler Api). Is there any c compiler that has entirely written in java?\n\n========================================\n\nTop Answer:\nANTRL has a grammar for C. The problem of generating assembly code for a particular platform isn't solved for you, but you can walk the AST and emit the instructions provided you know what they should be.\n\n========================================\n\nComments:\n- Parsing the source into an AST is usually the easiest bit of a compiler. Semantic analysis and code generation would be a massive task.\n- I agree with that, Paul. Code generation is platform-specific and difficult.\n- Actually. I don't want to implement this a platform independent application. I will use this in linux systems. I think ANTLER is a good solution but hard to implement as expected features.\n- By definition, a C compiler is platform dependent, because the generated assembly code is tied to the platform. ANTLR won't help with assembly code generation, but neither will anything else. This is a difficult problem if you understand it.\n- @duffymo: How is the machine code tied to the platform? That is an attribute of the current implementations, not of the actual C standard. Also, if you consider the x86 to be your VM-language, it is platform-independent already.\n- Your platform independent conclusion is based on the assumption \"consider the x86 to be your VM-language\". I didn't make the assumption when I said that machine code was tied to the platform. I realize that C the language is independent of the machine code that is generated for it. I'm talking about the second step - machine code generation - after the C standard grammar has been used to parse the source into an AST.\n- You can choose to generate a platform independent compiled program, by choosing a virtual target instruction set, e.g., roll-you-own, possibly the JVM, possibly CIL. That would make code generation some easier (except for pointer arithmetic which is likely to be hell for the JVM and CIL).\n- I don't see how this helps. You have to write machine code sometime. We're talking C here. Why would I want to emit JVM or CIL instructions, which emit VMWare instructions, which emits OS instructions? C is supposed to be close to the metal. If I'm going to do all that I might as well just abandon C and write Java or C#.\n- No, you don't have to write machine code; the interpreter can do machine specific I/O, memory access, and OS calls. Agreed, if you want to use C for arbitrary low-level metal, you have to be clear about what the *metal* is. OP is pretty unclear about his goal here.\n- It's always been clear to me what metal is in the case of C: right down at the hardware level. I would not put anything virtual between it and the hardware. If I thought that was a good idea, I'd write Java or C# or Python.\n- Your link to github C compiler is not pointing to github, and the link is broken.\n- Fixed, thanks for the notification! (It's been a long time)","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":27,"estimatedTokens":794}}452{"id":"stack-8888189","source":"stackoverflow","questionId":8888189,"title":"Cascade In-Memory Compilation with javax.tool","tags":["java","compiler-construction","classpath","classloader","in-memory"],"text":"Title: Cascade In-Memory Compilation with javax.tool\nTags: java, compiler-construction, classpath, classloader, in-memory\nSource: Stack Overflow\n\nQuestion:\nEclipse's JDT compiler provide an interface `INameEnvironment` which defines method `findType(...)` enable you to do cascade compilation. Curiously I would like to know if there are any means to do it using standard JDK compiler toolkit?\n\n**Note**, the scenario is a template engine which do in memory compilation for template file generated classes which have inter-dependencies, and it cannot forecast the order you encountered a template file, thus `Foo` might needs to be compiled first before it's parent `Bar` compiled already, therefore you need a mechanism to do cascade compilation, meaning during compilation of `Foo` you need to generate another source `Bar` and compile it first in order to continue `Foo`'s compilation: some code like the follows:\n\n```\nprivate NameEnvironmentAnswer findType(final String name) {\n try {\n if (!name.contains(TemplateClass.CN_SUFFIX)) {\n return findStandType(name);\n }\n\n char[] fileName = name.toCharArray();\n TemplateClass templateClass = classCache.getByClassName(name);\n\n // TemplateClass exists\n if (templateClass != null) {\n\n if (templateClass.javaByteCode != null) {\n ClassFileReader classFileReader = new ClassFileReader(templateClass.javaByteCode, fileName, true);\n return new NameEnvironmentAnswer(classFileReader, null);\n }\n // Cascade compilation\n ICompilationUnit compilationUnit = new CompilationUnit(name);\n return new NameEnvironmentAnswer(compilationUnit, null);\n }\n\n // So it's a standard class\n return findStandType(name);\n } catch (ClassFormatException e) {\n // Something very very bad\n throw new RuntimeException(e);\n }\n}\n```\n\n========================================\n\nTop Answer:\nTry reading through this HelloWorld example to see if it solves your problem. Without posting the code, it's hard to say what your specific issue is.\n\n========================================\n\nCode:\n```text\nprivate NameEnvironmentAnswer findType(final String name) {\n    try {\n        if (!name.contains(TemplateClass.CN_SUFFIX)) {\n            return findStandType(name);\n        }\n\n        char[] fileName = name.toCharArray();\n        TemplateClass templateClass = classCache.getByClassName(name);\n\n        // TemplateClass exists\n        if (templateClass != null) {\n\n            if (templateClass.javaByteCode != null) {\n                ClassFileReader classFileReader = new ClassFileReader(templateClass.javaByteCode, fileName, true);\n                return new NameEnvironmentAnswer(classFileReader, null);\n            }\n            // Cascade compilation\n            ICompilationUnit compilationUnit = new CompilationUnit(name);\n            return new NameEnvironmentAnswer(compilationUnit, null);\n        }\n\n        // So it's a standard class\n        return findStandType(name);\n    } catch (ClassFormatException e) {\n        // Something very very bad\n        throw new RuntimeException(e);\n    }\n}\n```\n\n```text\nINameEnvironment\n```\n\n```text\nfindType(...)\n```\n\n```text\nFoo\n```\n\n```text\nBar\n```\n\n```text\nFoo\n```\n\n```text\nBar\n```\n\n```text\nFoo\n```\n\n```text\nJavaFileManager.list(...)\n```\n\n```text\nForwardingJavaFileManager\n```\n\n========================================\n\nComments:\n- Here is the case. Suppose you have 2 string of source code to be compiled. One is \"package foo; public class One {...}\" and the other is \"package bar; import foo.One; public class Two {...}\". Everything is okay until it starts to compile the second class \"Two\". Compiler will report that foo.One could not be located.\n- I see. I'll leave this up and look for a better solution. It sounds like you need some sort of URL classloader.\n- So I give up the standard sun JDT compiler and end up with ECJ. Do you have any new findings?\n- The point of JDT's `INameEnvironment.findType()` is it allows me to do cascade compilation, say, I have class `Foo` which depends on class `Bar`. And my app tries to compile `Foo` before `Bar` is compiled, thus during compilation of `Foo` I can implement a logic to generate the source code of `Bar` and compile it and then continue compilation of `Foo`. Is this kind of stuff doable with `ForwardJavaFileManager` ?\n- You might be able to implement that type of thing on top of what I describe. I do something kind of similar. If I get certain errors, I use regex to find out what is missing and generate/compile a stub for it. But if you're saying that JDT gives you hooks right in the middle of compilation to find missing symbols, I don't think Javac has that available. You either have to figure it out in advance, or with the error, or the `list(...)` call is for a package, so if you know that a request for a certain package means you have to generate something, then you could put in the cascade compile there.\n- hmm... probably not the way for me to go. The point is when I compiling B, I found A is missing, not only the class A is missing, but the source code is also not ready, I need to generate the source code A out from a template file [a]. JDT's `findType` call fits perfect for the needs.\n- Yes, I'm in the same situation. But with Javac, I have to examine the error after trying to compile B, discover that A is missing, generate code for A, compile A, then recompile B. If JDT gives you a hook right in the middle of compilation to resolve dependencies, that would be cleaner.\n- definitely, I don't have to catch the exception and do a lot of work then come back to recompile B. The workflow with JDT becomes compile A -> looking for B -> if B is not ready, then generate and compile B -> continue compilation of A.\n- I grant the bounty to you as this is the closest answer :)","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":127,"estimatedTokens":1429}}453{"id":"stack-9613392","source":"stackoverflow","questionId":9613392,"title":"What need to be done to implement \"import ... as ...\" in java","tags":["java","compiler-construction","syntax"],"text":"Title: What need to be done to implement \"import ... as ...\" in java\nTags: java, compiler-construction, syntax\nSource: Stack Overflow\n\nQuestion:\nIf this is possible, what part of java compiler need to be re-engineered to has an \"import as\" statement, so codes can look like this way:\n\n```\nimport java.util.Date;\nimport mypackage.Date as MyDate;\n//...\njavaDate = new Date();\nmyDate = new MyDate();\n```\n\nEven more, what need to get this kind of syntax:\n\n```\nimport java.util.Date;\nimport path.to.mypackage as MP;\n//...\njavaDate2 = new Date();\nmyDate2 = new MP.Date();\n```\n\nAnd what trouble it will cause for existing codes?\n\n========================================\n\nCode:\n```text\nimport java.util.Date;\nimport mypackage.Date as MyDate;\n//...\njavaDate = new Date();\nmyDate = new MyDate();\n```\n\n```text\nimport java.util.Date;\nimport path.to.mypackage as MP;\n//...\njavaDate2 = new Date();\nmyDate2 = new MP.Date();\n```\n\n========================================\n\nComments:\n- Are you implying that you intend to \"re-engineer\" the Java compiler?\n- Just try to understand *java* way.\n- Note: The entire `import` mechanism exists only at compile-time. Once you arrive at `.class` files it's all fully-quallified class names. So that change would *only* require changes to the compiler, no other part of the system (JVM, libraries, ...) would need to be changed.\n- This feature has been there in scala forever, I hoped I would have been introduced in Java 8.\n- I vote this post as the best and simple answer. Though, I don't understand usage of this feature if this mechanism propose to JCP. It is easier and readable to Everyone to write `Date` instead of `MyDate`\n- In guessing, there must be a lot of JCPs exist for this kind of *syntax sugar*.\n- @wahyu.fahmy There are examples of where this could be useful. There's for example `java.util.Date` and `java.sql.Date`. If you import them both, then Java doesn't know which one you mean if you use use the name `Date`. It would be useful if you could rename it to `SqlDate`, for example.\n- Yes wahyu.fahmy, @Jesper point the direction to the answer, and it's right. And he also kindly point out the question could has it own usefullness.","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":57,"estimatedTokens":545}}454{"id":"stack-14419614","source":"stackoverflow","questionId":14419614,"title":"DFAs vs Regexes when implementing a lexical analyzer?","tags":["regex","compiler-construction","lexical-analysis","dfa"],"text":"Title: DFAs vs Regexes when implementing a lexical analyzer?\nTags: regex, compiler-construction, lexical-analysis, dfa\nSource: Stack Overflow\n\nQuestion:\n(I'm just learning how to write a compiler, so please correct me if I make any incorrect claims)\n\nWhy would anyone still implement DFAs in code (goto statements, table-driven implementations) when they can simply use regular expressions? As far as I understand, lexical analyzers take in a string of characters and churn out a list of tokens which, in the languages' grammar definition, are terminals, making it possible for them to be described by a regular expression. Wouldn't it be easier to just loop over a bunch of regexes, breaking out of the loop if it finds a match?\n\n========================================\n\nCode:\n```text\nlex\n```\n\n```text\nflex\n```\n\n========================================\n\nComments:\n- The main reason is that table driven DFAs can be easily generated by programs (eg. lex).\n- Also Regex patterns tend to be slower than using a good lexer and only good regex systems can handle things like matching muliple nested pairs of delimiters like parens.\n- @GuyCoder In a compiler the parser handles the parentheses, not the lexer.\n- @EJP Your right. I have my head in parser combinators right now and am not thinking lexer/parser.","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":27,"estimatedTokens":327}}455{"id":"stack-2625261","source":"stackoverflow","questionId":2625261,"title":"How is a lattice used by a compiler","tags":["math","compiler-construction"],"text":"Title: How is a lattice used by a compiler\nTags: math, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn my graduate class on compiler construction we've been introduced to the concept of a lattice. Three lectures have been devoted to lattices and so far it seems like an interesting tangent, but the dilemma is that it doesn't really help explain how a compiler **uses** a lattice to solve a concrete problem.\n\nWe have already covered parsing and typechecking. We're about to start liveness analysis and register allocation.\n\n**Note**, I'm not looking for resources on building compilers. The following list of links have that covered pretty well. What I'm looking for is an explanation on the relationship between compilers and lattices, bonus points for the most examples.\n\nLearning Resources on Parsers, Interpreters, and Compilers\n\nHow much of the compiler should we know?\n\nLearning to write a compiler\n\n========================================\n\nComments:\n- Make sure to attend the upcoming class on liveness analysis, and you will now of at least one important use of lattices in compilers ;)\n- That's almost how it went. \"Next class is on liveness analysis.\" A week later, \"Let's discuss lattices, meet, join, greatest lower bounds, least upper bounds, ...\" The following class, \"Now that you understand everything you need to know about liveness analysis...\" I'd plead that I slept through class except I have 10+ pages of notes.\n- Heh, I remember those exact classes from my first compiler course. The teacher presented the topic as \"Extraterrestrial Mathematics\" :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":23,"estimatedTokens":397}}456{"id":"stack-21945891","source":"stackoverflow","questionId":21945891,"title":"How do I check whether all code paths return a value","tags":["compiler-construction","semantics"],"text":"Title: How do I check whether all code paths return a value\nTags: compiler-construction, semantics\nSource: Stack Overflow\n\nQuestion:\nI am writing a compiler for an embedded scripting language that will run in my application. I am currently working on the semantic analysis portion of the compiler. I would like to know, in theory, how to check that all code paths in a given script will return a value.\n\nDoing a Google search on `how to check if all code paths return a value` yields only results about people seeing the error in their own code when not all code paths return a value (mostly SO questions), so I have been unable to find sources that explain how the actual checking can be done. Can anyone point me in the right direction?\n\n**NOTE:**\nI am specifically looking for an authoritative source outlining a rigorous algorithm, if at all possible.\n\n========================================\n\nCode:\n```text\nhow to check if all code paths return a value\n```\n\n========================================\n\nComments:\n- An alternative approach that I think should be mentioned is to have *all* statements return a value. Then the problem vanishes by design.\n- @KubaOber I had considered this, but I prefer the convention of forcing the programmer to use a return statement in order to reduce the likelihood of programming errors.\n- That's helpful, thank you. But I was hoping for a more rigorous algorithm, perhaps something out of a CS textbook. I couldn't find anything in the Dragon Book, and other compiler textbooks I've picked up.\n- What I've described should be pretty easy to convert to a top-down recursive descent over an AST, the same way you'd compute types for expressions.\n- I think it would be simple, but I'm looking for rigorous. I want to know that what I'm implementing is absolutely rock solid.\n- If you're looking for an authoritative reference, you should probably update the question to ask for a link to an existing trusted resource.\n- @MrCodeMnky If you want rigorous, you probably want to be describing your language in terms of formal semantics, and then the properties you're after become proofs in some logic. Your problem then reduces to one of doing proofs in a logic, and there are multiple techniques for such proofs, depending on what logic is involved. Most likely you'd be doing structural induction on the syntax tree, see e.g. here.\n- That's much more labor intensive than I am looking for, and I don't need such strong guarantees. Maybe I should just accept @templatetypedef's answer and be glad I got a helpful hint :)\n- @templatetypedef For while loops, what if it's a do-while? Can I say that it returns on all code paths if it's a do-while and it has a return statement, even if the condition is not always true? Thanks.\n- @MrCodeMnky definitely.\n- Regarding while trues being counted as \"returns on all control paths\", what if there is a break or something in the while true loop that ends up making a de-facto while(condition that isn't always true). Any advice regarding the analysis in this specific case?","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":33,"estimatedTokens":763}}457{"id":"stack-3131987","source":"stackoverflow","questionId":3131987,"title":"How long does Glibc take to compile?","tags":["linux","compiler-construction","glibc"],"text":"Title: How long does Glibc take to compile?\nTags: linux, compiler-construction, glibc\nSource: Stack Overflow\n\nQuestion:\nI've been compiling it in a virtual machine for over 8 hours now and it still hasn't finished.\n\nThe terminal is still printing things so I know it's still compiling.\n\nThe host system is a 2.10Ghz Intel Core2Duo with 4GB RAM and the guest is Linux on a PowerPC virtual machine (QEMU) one with 1GB RAM.\n\nI know the dynamic instruction translation can slow things down a bit but even so, Glibc shouldn't take longer than 3 hours or so?\n\nIs there something wrong or should I just continue to let do it's thing overnight?\n\n========================================\n\nComments:\n- Dynamic translation slows down such processes **a lot**. And Glibc is a huge piece of software. Therefore the result will be near endless compile times. Why don't you simply cross-compile on the Intel host for a PowerPC target? That's probably much faster.\n- Thing is. I'm building a cross Linux from scratch system and it says I must continue the installation on a PowerPC machine. Since the only one I have at hand is a 15 year old PowerBook I figured it'll be faster if I ran it from a virtual machine.\n- over 4 hours for glibc 2.24 on nehalem\n- From the same link, I looked up the compile times for uClibc and they were way shorter. Would using uClibc be a better option in this instance?","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":22,"estimatedTokens":346}}458{"id":"stack-4339863","source":"stackoverflow","questionId":4339863,"title":"Why isn't there \"is not\" keyword in c#?","tags":["c#","compiler-construction","types","keyword"],"text":"Title: Why isn't there \"is not\" keyword in c#?\nTags: c#, compiler-construction, types, keyword\nSource: Stack Overflow\n\nQuestion:\nIt just makes sense sometimes to check if an object is not type of X, so you need to do this instead:\n\n```\nif(this.GetType() != typeof(X)) \n {\n //Do my thing.\n\n }\n```\n\nWhich is a bit cumbersome to my opinion, would not something like this be nicer:\n\n```\nif(this is not X) \n {\n //Do my thing\n }\n```\n\n========================================\n\nTop Answer:\nAdding a keyword to a language adds complexity. Adding a keyword to a language after the initial specification could cause breaking changes for people upgrading. So keywords generally only get added if there's a very strong case for them. In this case, as the other answers point out, it is very easy to use the bang operator:\n\n```\nif (!(pero is Human)) ...\n```\n\n... which a typical C# (/C/C++/Java) developer would read \"if not (pero is human)\". So there's not much justification for a special keyword.\n\n========================================\n\nCode:\n```text\nif(this.GetType() != typeof(X)) \n {\n      //Do my thing.\n\n }\n```\n\n```text\nif(this is not X) \n {\n     //Do my thing\n }\n```\n\n```text\nif (!(this is X)) \n{\n    //Do my thing\n}\n```\n\n```text\n!\n```\n\n```text\nis\n```\n\n```text\nGetType()\n```\n\n```text\nnot\n```\n\n```text\nif (!(pero is Human)) \n{\n\n}\n```\n\n```text\nclass Human: ICanSpeak, Mamal\n{\n...\n}\n\nHuman h;\n\nif (h is Human)  {   will be true  }\nif (h is ICanSpeak)  {  will be true  }\nif (h is Mamal) {  will also be true  }\n```\n\n```text\nis\n```\n\n```text\npublic static bool IsNot<T>(this object obj)\n{\n  return !(obj is T);\n}\n```\n\n```text\nthis.GetType()\n```\n\n```text\ntypeof(X)\n```\n\n```text\nthis is X\n```\n\n```text\n!(a is X)\n```\n\n```text\nnot is\n```\n\n```text\nif (!(pero is Human)) ...\n```\n\n========================================\n\nComments:\n- Those would not be functionally equivalent if `X` was a subtype of `this.GetType()`.\n- We wouldn't need to make `not` a keyword. It probably would be enough making `is not` a keyword similar to `yield return`. Might be a bit confusing since most C# programmers aren't used to two word keywords(or should I call them keyphrases?). So one would have to use `isnot` which IMO looks ugly.\n- I'd like to see \"!\" allowed in a few special cases in C where it would have clear meaning, but not presently be legal. One case would be outside the parentheses for an if, while, etc. (in such contexts, it should strictly invert the condition, regardless of operator overloads). Another such case might be good here: allow \"this !is x\", so as to avoid extra parentheses.\n- Aside from the fact that extension methods on object are a questionable programming practice, that's actually not a bad little extension method there. I'm not saying I'd use it, but I think it is pretty reasonable.\n- @Eric: I really wished C# had also have a not operator \"not\" in addition to !. One might say this is useless but ! is really hard to see/spot in code where as \"if not (vector.IsNormalized)\" is much easier to parse visually than \"if !(vector.IsNormalized)\", but I guess C# did it this way to not alienate C++ people, right?\n- @Eric, may I ask why object extensions methods are viewed as bad practice?\n- @Joan: Correct. I agree that the use of \"!\" to mean \"not\" is unnecessarily terse; basically it is a \"secret code\" that you have to know about in order to understand the meaning of the text. But as you note, this is idiomatic in C-like languages. In VB there is an \"IsNot\" operator, incidentally.\n- @Max: Because it is quite rare that it is sensible to have a method that works on strings, integers, tigers, documents, lists of bananas, types... Odds are good that any extension method on \"object\" is actually only sensible on a small subset of all possible objects. It's not a hard-and-fast rule, but it is a bit of a code smell.\n- @Eric: Thanks Eric. I never used VB but their Not operator looks really good. I can also imagine C# people thinking \"Not\" is more verbal so less elite, than having terse operators. I have actually been told this. Personally I don't like way verbose languages but for some of these things like \"not\" \"and\" \"or\" I think they are better than !, &&, ||. I also like when they are all lower caps like other C# keywords. But a change like this would certainly never be considered, right? Because of all the complications it might introduce.\n- I don't think it's stupid. There are lots of features done and still being added just to improve the readability of the code.\n- For example we have no `yield` keyword in C# because that would have been a breaking change. `yield return x` on the other hand was illegal syntax before and thus no breaking change.\n- @CodeInChaos: Yes, but an `is not` keyword could technically fall in the same category as `yield return`. The difference is that it takes a lot of work to emulate `yield return`, whereas the bang operator and parentheses suffice to emulate `is not`.\n- I believe an `isnot` operator (no spaces) would be very easy and safe to implement. I would definitely vote for it to be part of the C# syntax. So many times while coding fast I have almost written it. It comes very natural. It would also have intellisense and it would save all the bracket and negation operator typing. In short it would have been handy.","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":147,"estimatedTokens":1323}}459{"id":"stack-33032768","source":"stackoverflow","questionId":33032768,"title":"Compiler Integration Tests in Haskell","tags":["haskell","testing","compiler-construction","integration-testing"],"text":"Title: Compiler Integration Tests in Haskell\nTags: haskell, testing, compiler-construction, integration-testing\nSource: Stack Overflow\n\nQuestion:\nI am planning to write a little toy compiler in Haskell for a very simple language in order to reinforce my Haskell skills and have fun designing a new language. I am still thinking about some general decisions and one of the biggest open points is how I would do integration tests. I have the following requirements:\n\nIt should be possible to create a list of triples (input, program, output), such that my integration test runs my compiler to compile the program, runs the compiled binary, passes the input to it and verifies that the output of the run is equal to the given output.\n\n- It should be possible to extend the integratiom test at a later point to test more complex interactions with the program, e.g. that it changes a file or sleeps for at least 20 seconds or something like that.\n\nI also have the following optional requirements:\n\n- It should be as much an \"end to end\" as possible, i.e. it should treat the wholencompiler as a blackbox as much as possible and it shouldn't have to access the internals of the compiler or anything like that.\n\n- All code should be written in Haskell.\n\n- It would be nice if I would get typical testing framework functionality for free, i.e. without implementing it myself. E.g. a green \"SUCCESS\" message or a collection of error messages describing failures.\n\nI have tried to find something that fulfills my needs, but I wasn't successful so far. The alternatives I considered are the following:\n\n- shunit would satisfy everything except the condition that I would like to write the code in Haskell.\n\n- QuickCheck would allow me to write everything in Haskell, but as I understand it, it seems to be mostly suited for tests that involve just a Haskell function and its result. So I would need to test functions in the compiler and relax my \"end to end\" requirement.\n\n- I could just write a Haskell program that starts the compiler in another process, passes it the input program and then starts the compiled code in another process, passes it the inpit and checks the output. This would however involve a lot of coding on my side in ordee to implement all the features that one gets for free when using a testing framework.\n\nI am not sure yet which option I should choose and I still hope that I am missing a good solution. Do you have any idea on how I could create an integration test that fulfills all my requirements?\n\n========================================\n\nCode:\n```hs\ntype Source = String\n\ncompile :: Source -> Program\n```\n\n```hs\ndata Report = Report Output TimeTaken OtherStats ...\n\nexecute :: Program -> IO Report\n```\n\n```hs\nexecute' :: Program -> Report -- or perhaps 'executeUnsafe'\nexecute' = unsafePerformIO . execute\n```\n\n```hs\ncompileAndExec :: Source -> Report\ncompileAndExec = compile . execute'\n```\n\n```text\nunsafePerformIO\n```\n\n```text\nunsafePerformIO\n```\n\n```text\nexecute\n```\n\n========================================\n\nComments:\n- Ok, thanks a lot, that is actually a good possibility. I haven't used unsafePerformIO yet and when I heard of it, I decided to avoid it as much as possible, but I guess this is exactly one of the cases where it is useful. The operation is kind of pure, but Haskell doesn't know that. And to get the QuickCheck framework to understand it, I have to use this. And besides, my fear of using unsafePerformIO should not apply to test code anyways.\n- *\"but Haskell doesn't know that\"* — exactly! I actually even thought of adding that idea to the answer! The same goes about `unsafeCoerce` — it's when you *know* for a fact something is correct but orthodox Haskell just doesn't seem to fit your fact in its belief system.","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":7,"totalLines":71,"estimatedTokens":941}}460{"id":"stack-650652","source":"stackoverflow","questionId":650652,"title":"At what level C# compiler or JIT optimize the application code?","tags":["c#",".net","optimization","compiler-construction","jit"],"text":"Title: At what level C# compiler or JIT optimize the application code?\nTags: c#, .net, optimization, compiler-construction, jit\nSource: Stack Overflow\n\nQuestion:\nI want to know this info to reduce my code size so I will not waste my time optimize things that will be done by compiler or JIT.\n\nfor example: \n\nif we assume the compiler inline the call to the get function of a property so I do not have to save the return value in a local variable to avoid function call.\n\nI want to recommend a good reference that describes what is going on?\n\n========================================\n\nTop Answer:\nIf you are worried about performance, run a profiler. **Then** change code. Chances are that you will never in a million years guess 100% correctly where the time is going. You could be changing the 0.02% timing, and leaving the method that contributes 62% of the burden. You could also be making it worse. Without a profiler and evidence, you are blind.\n\nYou can't **assume** that the JIT will inline a property getter. There are many reasons it may or may not do so; size of the method body, virtual, value vs reference type, architecture, debugger attached, etc.\n\n\"Hoisting\" still has a place, and can still achieve savings *if* the code is called repeatedly in a tight loop; for example:\n\n```\nvar count = list.Count;\nfor(int i = 0 ; i (forget the `for` vs `foreach` debate fr the above - this is an orthogonal discussion). In the above, the \"hoist\" will help performance. But just to be *really* confusing - with arrays, it is the opposite, and it is more efficient to *not* hoist it:\n\n```\nfor(int i = 0 ; i The JIT recognises this and removes the bounds check (as arrays are fixed size).\n\n========================================\n\nCode:\n```text\nvar count = list.Count;\nfor(int i = 0 ; i < count ; i++) {...}\n```\n\n```text\nfor(int i = 0 ; i < arr.Length ; i++) {...}\n```\n\n```text\nfor\n```\n\n```text\nforeach\n```\n\n========================================\n\nComments:\n- Thanks for information, but I am asking about a good reference for this information\n- My point is that *no* reference will really help you with this. A **profiler** will.\n- plusitty plus plus. the reference will help you *after* the profiler shows you what's taking time. No reference can cover the broad spectrum of possible combinations of things that exist in the real world. Note that many profilers don't allow (or must be told to allow) inlining of functions so that can skew your profiling information. For most people I doubt that this is significant. If you think inlining is the big deal you shoudl really know to be using a profiler...","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":55,"estimatedTokens":653}}461{"id":"stack-44770962","source":"stackoverflow","questionId":44770962,"title":"Difference between if as an expression and if as a statement","tags":["go","compiler-construction","language-design"],"text":"Title: Difference between if as an expression and if as a statement\nTags: go, compiler-construction, language-design\nSource: Stack Overflow\n\nQuestion:\nSo I was watching this video on the Go language - https://www.youtube.com/watch?v=p9VUCp98ay4 , and at around 6:50 a guy asks a question about why they implemented if's as statements and not expressions. What is the difference between those two implementations? As far as I know, I've never had to change the way I use a conditional based on the language.\n\nEdit: and what does he mean that \"you need values rather than variables\" in his question?\n\n========================================\n\nTop Answer:\nYou can get the same code elegance that you would get from if expression by using an instantly invoked function (IIF) combined with if statements in GO. I'm not a GO programmer (mainly typescript) so please let me know if this is bad for performance for any reason.\n\n```\nfunc main() {\n amIHungry := true\n didMyPaycheckComeInYet := false\n\n choice := func() string {\n if(!amIHungry && !didMyPaycheckComeInYet){\n return \"stay-home\"\n }\n if(!amIHungry && didMyPaycheckComeInYet){\n return \"buy-new-keyboard\"\n }\n if(amIHungry && !didMyPaycheckComeInYet){\n return \"make-ramen\"\n }\n return \"taco-bell-time\"\n }()\n\n println(choice)\n\n}\n```\n\nand then later on in your program, rather than having a bunch of out of context states, you can have a simplified \"choice\" to choose your app logic.\n\n```\nif(choice == \"taco-bell-time\"){\n println(\"Order a bean burrito, but with black beans,\")\n}\n```\n\nis a little easier to reason about than\n\n```\nif(amIHungry && didMyPaycheckComeInYet){\n println(\"Order a bean burrito, but with black beans,\")\n}\n```\n\n========================================\n\nCode:\n```text\nconst myValue = if condition { value1 } else { value2 }\n```\n\n```text\nvar myValue\nif condition {\n    myValue = value1\n} else {\n    myValue = value2\n}\n```\n\n```text\nval\n```\n\n```text\nif\n```\n\n```text\nif\n```\n\n```text\nvar\n```\n\n```text\nconst\n```\n\n```golang\nfunc main() {\n    amIHungry := true\n    didMyPaycheckComeInYet := false\n\n    choice := func() string {\n        if(!amIHungry && !didMyPaycheckComeInYet){\n            return \"stay-home\"\n        }\n        if(!amIHungry && didMyPaycheckComeInYet){\n            return \"buy-new-keyboard\"\n        }\n        if(amIHungry && !didMyPaycheckComeInYet){\n            return \"make-ramen\"\n        }\n        return \"taco-bell-time\"\n    }()\n\n    println(choice)\n\n}\n```\n\n```golang\nif(choice == \"taco-bell-time\"){\n   println(\"Order a bean burrito, but with black beans,\")\n}\n```\n\n```golang\nif(amIHungry && didMyPaycheckComeInYet){\n     println(\"Order a bean burrito, but with black beans,\")\n}\n```\n\n========================================\n\nComments:\n- Haven't watched in order to be able to answer the second question you asked, but to the first question, if as-an-expression would be where `x = if (condition) then { value1 } else { value2 }` can assign different values to x. Rust and Kotlin do it, for example, and it's what C's ternary operator (`?:`) does (same example is written `condition ? value1 : value2`).\n- It's a language design decision despite the asker's use of the word 'implementation'. If I had to guess why the Gophers designed it that way, it's that Go generally sticks to the imperative tradition of C, and blocks under `if` behaving as statements rather than like expressions is more C-like.\n- You might want to take a look at the BLISS programming language. You can find documentation on line. Everything is an expression in BLISS.","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":130,"estimatedTokens":881}}462{"id":"stack-709740","source":"stackoverflow","questionId":709740,"title":"How method hiding works in C#?","tags":["c#","inheritance","compiler-construction","method-hiding"],"text":"Title: How method hiding works in C#?\nTags: c#, inheritance, compiler-construction, method-hiding\nSource: Stack Overflow\n\nQuestion:\nWhy the following program prints\n\n```\nB\nB\n```\n\n(as it should)\n\n```\npublic class A\n {\n public void Print()\n {\n Console.WriteLine(\"A\");\n }\n }\n\n public class B : A\n {\n public new void Print()\n {\n Console.WriteLine(\"B\");\n }\n\n public void Print2()\n {\n Print();\n }\n }\n\n class Program\n {\n static void Main(string[] args)\n {\n var b = new B();\n b.Print();\n b.Print2();\n }\n }\n```\n\nbut if we remove keyword 'public' in class B like so:\n\n```\nnew void Print()\n {\n Console.WriteLine(\"B\");\n }\n```\n\nit starts printing\n\n```\nA\nB\n```\n\n?\n\n========================================\n\nTop Answer:\nYou're making the `Print` method `private`, so the only available `Print` method is the inherited one.\n\n========================================\n\nCode:\n```text\nB\nB\n```\n\n```text\npublic class A\n    {\n        public void Print()\n        {\n            Console.WriteLine(\"A\");\n        }\n    }\n\n    public class B : A\n    {\n        public new void Print()\n        {\n            Console.WriteLine(\"B\");\n        }\n\n        public void Print2()\n        {\n            Print();\n        }\n    }\n\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            var b = new B();\n            b.Print();\n            b.Print2();\n        }\n    }\n```\n\n```text\nnew void Print()\n    {\n        Console.WriteLine(\"B\");\n    }\n```\n\n```text\nA\nB\n```\n\n```text\npublic\n```\n\n```text\nnew Print()\n```\n\n```text\nMain\n```\n\n```text\nprivate\n```\n\n```text\nPrint\n```\n\n```text\nprivate\n```\n\n```text\nPrint\n```\n\n========================================\n\nComments:\n- good tricky scenario. thanks for sharing.","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":155,"estimatedTokens":423}}463{"id":"stack-1175475","source":"stackoverflow","questionId":1175475,"title":"What is Symbol Resolution?","tags":["compiler-construction","symbols"],"text":"Title: What is Symbol Resolution?\nTags: compiler-construction, symbols\nSource: Stack Overflow\n\nQuestion:\nThis seems to be one of those things that every talks about but no one defines...I can't seem to find any information on this topic. What is symbol resolution? This is the best thing I've found: http://docs.oracle.com/cd/E23824_01/html/819-0690/chapter2-90421.html#chapter2-93321\n\nDoes it have something to do with how your program is compiled?\n\n========================================\n\nTop Answer:\nAs mentioned, it can refer to run-time or link-time symbol resolution. However you shouldn't forget *compile-time symbol resolution*.\n\nThis is the rules a language uses to map symbols to \"things\". Symbols being just about anything that looks like a name (local, members and global variables, functions, methods, types, etc.) and \"things\" being the compilers understanding of what the name refers to.\n\nThe rules for doing this can be fairly simple (for instance, IIRC in C it's little more than an ordered list of places to look) or complex (C++ has all sorts of case with overloading, templates and whatnot). Generally, these rules interact with the semantics of the program and sometimes they can even result in (potentially) ambiguities:\n\nC++:\n\n```\nint First(int i) { return i; }\nfloat First(float f) { return f; }\n\nvoid Second(int (*fn)(int)) { printf(\"int\"); }\nvoid Second(float (*fn)(float); { printf(\"float\"); }\n\n...\n\nSecond(&First); // What will be printed?\n```\n\n========================================\n\nCode:\n```text\ngcc\n```\n\n```text\n-static\n```\n\n```text\nint First(int i) { return i; }\nfloat First(float f) { return f; }\n\nvoid Second(int (*fn)(int)) { printf(\"int\"); }\nvoid Second(float (*fn)(float); { printf(\"float\"); }\n\n...\n\nSecond(&First); // What will be printed?\n```\n\n========================================\n\nComments:\n- Makes sense, thank you very much. I will look up \"shared libraries\" as well. Do you have any books you could recommend for learning more about this?\n- The best book I know of on the topic is *Linkers and Loaders* by John R. Levine.\n- I mean it in the context of using it with the nm command on unix. Does that help? I'm not sure what dlopen is either, so I can't say if you're close.","metadata":{"transformedAt":"2026-08-18T18:32:17.799Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":61,"estimatedTokens":557}}464{"id":"stack-6540386","source":"stackoverflow","questionId":6540386,"title":"Assembly Performance Tuning","tags":["c++","compiler-construction","assembly","compiler-theory"],"text":"Title: Assembly Performance Tuning\nTags: c++, compiler-construction, assembly, compiler-theory\nSource: Stack Overflow\n\nQuestion:\nI am writing a compiler (more for fun than anything else), but I want to try to make it as efficient as possible. For example I was told that on Intel architecture the use of any register other than `EAX` for performing math incurs a cost (presumably because it swaps into `EAX` to do the actual piece of math). Here is at least one source that states the possibility (http://www.swansontec.com/sregisters.html).\n\nI would like to verify and measure these differences in performance characteristics. Thus, I have written this program in C++:\n\n```\n#include \"stdafx.h\"\n#include \n#include \n\nusing namespace std;\n\nint _tmain(int argc, _TCHAR* argv[])\n{\n __int64 startval;\n __int64 stopval;\n unsigned int value; // Keep the value to keep from it being optomized out\n\n startval = __rdtsc(); // Get the CPU Tick Counter using assembly RDTSC opcode\n\n // Simple Math: a = (a \" > i;\n\n return 0;\n}\n```\n\nI tried this code swapping `eax` and `ebx` but I'm not getting a \"stable\" number. I would hope that the test would be deterministic (the same number every time) because it's so short that it's unlikely a context switch is occurring during the test. As it stands there is no statistical difference but the number fluctuates so wildly that it would be impossible to make that determination. Even if I take a large number of samples the number is still impossibly varied.\n\nI'd also like to test `xor eax, eax` vs `mov eax, 0`, but have the same problem.\n\nIs there any way to do these kinds of performance tests on Windows (or anywhere else)? When I used to program Z80 for my TI-Calc I had a tool where I could select some assembly and it would tell me how many clock cycles to execute the code -- can that not be done with our new-fangeled modern processors?\n\nEDIT: There are a lot of answers indicating to run the loop a million times. To clarify, this actually makes things worse. The CPU is much more likely to context switch and the test becomes about everything but what I am testing.\n\n========================================\n\nTop Answer:\nI'd suggest taking a look at Agner Fog's \"Software optimization resources\" - in particular, the assembly and microarchitecture manuals (2 and 3), and the test code, which includes a rather more sophisticated framework for measurements using the performance monitor counters.\n\n========================================\n\nCode:\n```text\n#include \"stdafx.h\"\n#include <intrin.h>\n#include <iostream>\n\nusing namespace std;\n\nint _tmain(int argc, _TCHAR* argv[])\n{\n    __int64 startval;\n    __int64 stopval;\n    unsigned int value; // Keep the value to keep from it being optomized out\n\n    startval = __rdtsc(); // Get the CPU Tick Counter using assembly RDTSC opcode\n\n    // Simple Math: a = (a << 3) + 0x0054E9\n    _asm {\n        mov ebx, 0x1E532 // Seed\n        shl ebx, 3\n        add ebx, 0x0054E9\n        mov value, ebx\n    }\n\n    stopval = __rdtsc();\n    __int64 val = (stopval - startval);\n    cout << \"Result: \" << value << \" -> \" << val << endl;\n\n    int i;\n    cin >> i;\n\n    return 0;\n}\n```\n\n```text\nEAX\n```\n\n```text\nEAX\n```\n\n```text\neax\n```\n\n```text\nebx\n```\n\n```text\nxor eax, eax\n```\n\n```text\nmov eax, 0\n```\n\n```text\nXOR EAX, EAX\nCPUID\nXOR EAX, EAX\nCPUID\nXOR EAX, EAX\nCPUID            ; Intel says by the third execution, the timing will be stable.\nRDTSC            ; read the clock\npush eax         ; save the start time\npush edx\n\n    mov ebx, 0x1E532 // Seed // execute test sequence\n    shl ebx, 3\n    add ebx, 0x0054E9\n    mov value, ebx\n\nXOR EAX, EAX      ; serialize\nCPUID   \nrdtsc             ; get end time\npop ecx           ; get start time back\npop ebp\nsub eax, ebp      ; find end-start\nsbb edx, ecx\n```\n\n```text\nrdtsc\n```\n\n```text\nrdtsc\n```\n\n```text\ncpuid\n```\n\n```text\nrdtsc\n```\n\n========================================\n\nComments:\n- I doubt your z80 processor had branch prediction, predictive execution, pipelined execution, or such gigantically expensive cache misses compared to cache hits. (I might be wrong on that last one.) Processors are immensely complicated these days. :)\n- @sarnold: of course, for the last one the speed ratio of hist-vs-misses is immense, since z80 don't have cache, you can assume the 0-sized cache gives instantaneous results :)\n- \"I was told that on Intel architecture the use of any register other than EAX for performing math incurs a cost (presumably because it swaps into EAX to do the actual piece of math).\" <-- If this was ever true, it for sure isn't now: x86es all the way back to the Pentium Pro have done comprehensive register renaming.\n- @Zack: indeed, also, since the 386, the ISA is much more orthogonal, so advantages due to not having to move data to EAX to perform some operations were minimized even longer ago.\n- I tried the rdtscp instruction (I'm running on an i7) and it seems to have the same effect as using the CPUID instruction, but because that instruction is variable and costly I do get better results.\n- And that is the conclusion I am coming to on the EAX/Math issue. But still I don't have a good way to test and measure.\n- @Nate - I think it is **very** hard to measure anything at this level. If you find an instruction that has a separate form for the A/AX/EAX/RAX register, it might be a good idea to use that. Otherwise it probably doesn't matter. I haven't worked at this level since the 386, so I have obviously no current info.\n- I've repeated it many millions of times and still get overlaps in which side wins (about 50% of the time even). The numbers are so erratic that the test just has to be invalid.\n- Good comment on Turbo Boost. When I've added millions of reps in the loop it increases the chance of context switching during my test. At that point the results have more to do with everything else BUT what I am testing.\n- @Nate Zaugg I don't know how the timing mechanism you are using works. If you use a timing mechanism that counts the number of cpu clock cycles you are getting on that process, then context switches shouldn't be a problem. You won't record the time where your code isn't running this way. Context switches may impact your numbers slightly, but if you run enough repetitions then both of the two options you are comparing will receive about the same number of switches on average.\n- The RDTSC instruction gets the number of \"ticks\" since the CPU reset. Therefore it will count the time doing other processes. en.wikipedia.org/wiki/Time_Stamp_Counter\n- You could use the C clock function instead. It's not so accurate, but if you run enough repetitions then that is not much of a problem. There may also be a more accurate way to record per-process time.\n- Per-process time tends to be inaccurate because incomplete timeslices (e.g. if the OS receives an interrupt) are still assigned to you.\n- I'm within 6-8% with the changes you suggest. Thank you!\n- On second glace, make that around 20%\n- `lfence` instead of `cpuid` still serializes instruction execution but is faster (especially in a VM) and doesn't disturb registers. (It also doesn't drain the store buffer). Also, for short intervals you might as well ignore the high half in EDX and just take the low 32 bits of the TSC interval by subtracting `end_eax - start_eax`.\n- @PeterCordes: Really? That *certainly* doesn't fit with Intel's documentation. `lfence` should serialize instructions that load from memory, `sfence` instructions that store to memory, and `fence` instructions that either load or store--but `rdtsc` doesn't touch memory, so if the docs are at all accurate, none of these affects it at all. Even if they currently do, Intel has always documented it as having no effect on register-only instructions, so even if it does currently work, they'd be free to break what you're assuming.\n- It doesn't fit with the *name* chosen, but it certainly is documented by Intel these days at least. felixcloutier.com/x86/lfence. *Specifically, LFENCE does not execute until **all** prior instructions have completed locally, and no later instruction begins execution until LFENCE completes.* Perhaps at one point just an implementation-detail of `lfence` (which is pointless for almost all other purposes, except ordering SSE4.1 `movntdqa` weakly-ordered loads from WC memory), it's now architectural. It's also well known and mentioned in various Intel white-papers on using rdtsc.\n- \"completed locally\" = \"retired from the ROB\". Note that AMD is potentially different, but OSes that want LFENCE for Spectre mitigation enable the control bit so LFENCE is an execution barrier and can be used for serializing RDTSC. Is LFENCE serializing on AMD processors?. `lfence` is nearly useless for anything else; if Intel ever had plans to support a weaker memory model, they abandoned those plans leaving `lfence` basically orphaned of purpose other than this. Does the Intel Memory Model make SFENCE and LFENCE redundant?\n- Hmm...I'm reading the Intel docs, and it does look like wording is considerably stronger than the last time I'd looked (though even now, the documentation is rather inconsistent). I'll probably have to do a bit of editing to take this into account. Thanks for the heads-up.\n- If you're going to edit, preemptive terminology note: `lfence` is not a \"serializing instruction\". That term has a specific technical meaning for x86, and includes draining the store buffer as well as the ROB. (AFAIK that's all it implies on current implementations.) Also related: How to get the CPU cycle count in x86_64 from C++? is my attempt at a canonical answer about RDTSC behaviour and related CPU feature (what Linux calls constant_tsc / nonstop_tsc and so on, some of which have CPUID feature bits).\n- Hmm....looking at that, it almost looks like one of these should really be closed as a dupe of the other.\n- Awesome resource! It's interesting that he did his performance stuff in the Kernel. I thought that it might be necessary to test this stuff in the highest dispatch levels of the Kernel to get predictable results.","metadata":{"transformedAt":"2026-08-18T18:32:17.800Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":12,"totalLines":171,"estimatedTokens":2518}}465{"id":"stack-42445522","source":"stackoverflow","questionId":42445522,"title":"Compiling an AST to Assembly","tags":["assembly","compiler-construction","vm-implementation"],"text":"Title: Compiling an AST to Assembly\nTags: assembly, compiler-construction, vm-implementation\nSource: Stack Overflow\n\nQuestion:\nI have an abstract syntax tree that I need to convert to assembly for a virtual machine. I don't know how best to do this so I started using a chain of string templates. Pseudo-code example of what I mean, say a simplistic if-statement with one condition needs to be compiled:\n\n```\nstd::string compile_if(Node* n) {\n std::string str = \"\";\n\n curLabel = nLabels++;\n\n str += compile_comparison(n->getChild(0));\n\n str += \".true\"+curLabel+\":\";\n str += compile_block(n->getChild(1));\n\n str += \".false\"+curLabel+\":\";\n\n return str;\n}\n```\n\nWhere each compile_* generates an assembly string based on the current/next AST nodes. Then the final string is run through an assembler. This seems sloppy and hard to maintain, surely this isn't what most compilers do. Is this a bad idea, should I change it? How do most other compilers generate virtual assembly code / machine code?\n\n========================================\n\nCode:\n```text\nstd::string compile_if(Node* n) {\n    std::string str = \"\";\n\n    curLabel = nLabels++;\n\n    str += compile_comparison(n->getChild(0));\n\n    str += \".true\"+curLabel+\":\";\n    str += compile_block(n->getChild(1));\n\n    str += \".false\"+curLabel+\":\";\n\n    return str;\n}\n```\n\n```d\nInstruction[] compile_if(IfNode n) {\n    Instruction[] code;\n\n    code ~= compile_condition(n.condition);\n\n    Instruction skip_taken = new JumpInstruction(`jz`);\n    code ~= skip_taken;\n\n    code ~= compile_block(n.taken_block);\n\n    Instruction skip_nottaken = new JumpInstruction(`jmp`);\n    code ~= skip_nottaken;\n\n    Instruction[] nottaken_code = compile_block(n.nottaken_block);\n    skip_taken.destination = nottaken_code[0];\n    code ~= nottaken_code;\n\n    Instruction end = new NopInstruction();\n    skip_nottaken.destination = end;\n    code ~= end;\n\n    return code;\n};\n```\n\n```d\nbyte[2] assemble_jz(Instruction[] code, int idx) {\n    // assemble the jz instruction at code[idx]\n\n    JumpInstruction jump = code[idx];\n    ++idx;\n\n    byte jump_offset = 0;\n    while (code[idx] != jump.destination) {\n        jump_offset += size_of_instruction(code[idx]);\n        ++idx;\n    };\n\n    byte[2] machinecode = [\n        0x74, // jz short\n        jump_offset\n    ];\n    return machinecode;\n};\n```\n\n```text\nif\n```\n\n```text\nif/else\n```\n\n```text\nskip_taken.destination = nottaken_code[0]\n```\n\n```text\nJumpInstruction\n```\n\n```text\nNopInstruction\n```\n\n```text\nskip_nottaken\n```\n\n========================================\n\nComments:\n- \"Then the final string is run through an assembler.\" - It may or may not be what \"most compilers\" do, but it's a perfectly valid approach. I don't see what this has to do with C++, though, so I'm removing the tag.\n- What aspect of what you're doing are you asking about?\n- the template strings\n- You say `assembly` from which I expect the relation to be with a machine architecture, e.g. `x86` but then you say `virtual machine` for which I expect the relation to be with an instruction set, e.g. `java bytecode`. Please explain?\n- a virtual assembly language to make it easier to write compilers for and easier to modify after compilation if ever needed\n- stringstream might be nicer","metadata":{"transformedAt":"2026-08-18T18:32:17.800Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":9,"totalLines":126,"estimatedTokens":810}}466{"id":"stack-4842213","source":"stackoverflow","questionId":4842213,"title":"How exactly an access violation exception is triggered","tags":["c++","windows","visual-c++","compiler-construction","cpu"],"text":"Title: How exactly an access violation exception is triggered\nTags: c++, windows, visual-c++, compiler-construction, cpu\nSource: Stack Overflow\n\nQuestion:\nSometimes bugs can cause memory access violation exception.\n\nHow exactly this exception is triggered? What mechanism works behind the scenes?\n\nDoes it need support from the CPU (starting at what CPU?) / from the OS (starting at what version?) / from the compiler (starting at what version?)\n\n**Edit:**\n\nOne specific scenario I want to understand better:\n\nThe following code may cause an access violation exception.\n\n```\nTCHAR* czXXX= _T(\"ABCDEFG\");\nczXXX[0]= 'A';\n```\n\nI guess czXXX points to a read-only memory block, but what exactly happens?\n\n========================================\n\nTop Answer:\nIn x86 architecture (and most others as well), this is started from the MMU - the Memory Management Unit. The MMU is used to translate virtual memory addresses to physical memory addresses. If a request is made to access an invalid address (0x00000000 or something too high) the MMU will trap (interrupt) to the OS (this is in fact done for every access not in the TLB (Translate Lookaside Buffer - the MMU translation \"cache\")). Here the OS will be able to tell that this is an illegal memory access, and propagate to the user application via the OS-dependent mechanism (signals in Linux (SIGSEGV), I'm not familiar with Windows enough to say how it is done in it).\n\nThis feature is available for any modern CPU, OS and compiler. The most basic requirement is an MMU, which is present in all but the most basic embedded CPUs. I doubt there is any PC currently operating that doesn't support this.\n\n**Edit:**\n\nFollowing the OP edit, when a literal string is used, the memory is placed in the .text segment of the executable. This is where the binary code and constant values sit. Naturally, in most OSs this is read-only (especially under Linux with various security enhancements). When you try to change a value of a literal string you are basically trying to write to a read-only memory, causing an access violation. Again, this is caught by the MMU that sees a write command to a read-only memory address.\n\n========================================\n\nCode:\n```text\nTCHAR* czXXX= _T(\"ABCDEFG\");\nczXXX[0]= 'A';\n```\n\n```text\nbase:base+bound\n```\n\n```text\n0x0\n```\n\n```text\nmmap\n```\n\n```text\nvoid Kaboom()\n{\n    int* certain_death = NULL;\n    *certain_death = 0;\n}\n```\n\n```text\ndelete pSample;\n\n//again deleting the same memory!\ndelete pSample;\n```\n\n```text\n__try\n{\n  //Usual code\n}\n__except(EXCEPTION_EXECUTE_HANDLER)\n{\n  //Handling code\n}\n__finally\n{\n  //Cleanup\n}\n```\n\n========================================\n\nComments:\n- Actually your example has undefined behaviour according to standard ;)\n- The OS-dependent mechanism is called \"structured exceptions\" in Windows: msdn.microsoft.com/en-us/library/ms680657%28v=vs.85%29.aspx\n- what if RAM and Disk both full?\n- @Scott混合理论 it is normal for RAM to be full, or very nearly full -- unused RAM is wasted RAM. Since performance degrades the more the computer must use swap space, normally people will try to run only as much software as will fit into memory. Once swap space is full, the kernel will start returning errors for memory allocations or will kill entire processes to free up space. Look for \"OOM Killer\" for details on that.","metadata":{"transformedAt":"2026-08-18T18:32:17.800Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":92,"estimatedTokens":835}}467{"id":"stack-14091246","source":"stackoverflow","questionId":14091246,"title":"What is the difference between two empty main{} programs with and without OS?","tags":["c++","c","compiler-construction","operating-system","runtime"],"text":"Title: What is the difference between two empty main{} programs with and without OS?\nTags: c++, c, compiler-construction, operating-system, runtime\nSource: Stack Overflow\n\nQuestion:\nWhat is the difference between two empty programs `main {}` when compiled using a c/c++ compiler for target with OS (say linux) and without an OS (say for an embedded DSP target)? I am specifically interested to know what the compiler does differently when there is an OS and otherwise. How is the compiler/language runtime differ in two cases?\n\n========================================\n\nTop Answer:\nIt is actually the linker that does a different job when packaging a\n program to run on an operating system versus building a program that\n runs on the bare hardware alone. The compiler merely produces object\n files consisting of instructions targeted for the host architecture,\n and these chunks are later combined and packaged by the linker.\n\n \n A program that is meant to run on an operating system has to have a\n certain binary structure -- this is where executable formats come into\n play. Such a format may dictate that the program should have a few\n header sections in the beginning and then the code should , for\n example. It is the job of the OS loader to interpret this structure\n and then feed the CPU with stream of instructions that the code\n section contains.\n\n \n In contrast, a program that is meant to run on the bare hardware\n usually doesn't have a special structure and can be feeded directly to\n the CPU.\n\nI'd like to build on this very well written answer by Blagovest. Indeed as he suggests there's a difference of executable container formats and binary interfaces and whatnot. However, possibly the biggest difference is the *actual* main entry point to the execution of the application code as well as the presence of a startup code along with a runtime library; although, if you know what you're doing you can avoid linking against the latter on a full-fledged OS as well.\n\nOftentimes with the presence of startup routines, a runtime library, such as crt0, the actual entry point of your application is not `main` but something else (usually `_start`). Before this actual entry point hands the control down to your `main` it might perform a bunch of very specific tasks, usually related to initialisation.\n\nThere's always Wikipedia for more information on crt0.\n\nHowever, on a bare-metal platform there might not be such routines that come bundled with your compiler. As a result the control might be handed down straight to your `main` and the first code that's to execute on the platform would be yours.\n\nThere you go, this is the most fundamental difference between the two kinds of `main`s. However, I have to say your question is a bit vague as you can do away without a startup script if you initialise the stack etc. yourself and you can also do with a runtime library that does all of these on some (most?) bare-metal platforms. In fact this all depends on your compiler suite, the platform you're targeting, etc.\n\n========================================\n\nCode:\n```text\nmain {}\n```\n\n```text\nmain\n```\n\n```text\n_start\n```\n\n```text\nmain\n```\n\n```text\nmain\n```\n\n```text\nmain\n```\n\n```text\nmain\n```\n\n```text\nmain\n```\n\n```text\nmain\n```\n\n```text\nint\n```\n\n```text\nmain\n```\n\n========================================\n\nComments:\n- since when DSPs do not have an OS ?\n- the file when you have a OS should have some kind of explnaition about it - what kind the file is, and premmissions.\n- Related answer: stackoverflow.com/a/4519407/17034\n- There's no answer to your question in general. Without an OS, you're looking at a freestanding C++ implementation. In such implementations, even whether `main` is the entry point is implementation-defined.\n- Tried using verbose mode to see the differences?\n- Great fundamental question. This answer shifted the discussion focus (correctly IMO) away from compiler. Great references for more info 1) Balau's articles on Bare Metal (balau82.wordpress.com/2010/02/14/&hellip;). 2) \"executable format\" see Teensy ELF Executables for Linux (muppetlabs.com/~breadbox/software/tiny/teensy.html)","metadata":{"transformedAt":"2026-08-18T18:32:17.800Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":96,"estimatedTokens":1033}}468{"id":"stack-2170752","source":"stackoverflow","questionId":2170752,"title":"Why Does Test Condition of \"for(;;)\" Succeed?","tags":["c","compiler-construction","for-loop","infinite-loop"],"text":"Title: Why Does Test Condition of \"for(;;)\" Succeed?\nTags: c, compiler-construction, for-loop, infinite-loop\nSource: Stack Overflow\n\nQuestion:\nInsomuch as I understand \"for(;;)\" has no initial condition, no test condition and no increment condition, and therefore loops forever, I am curious why the test condition succeeds each loop.\n\nDoes the empty expression \";\" evaluate as true in C? Or is this a special case handled by compilers?\n\nA similar, but unrelated question.\n\n========================================\n\nTop Answer:\nC language has no such thing as \"empty expression\". If any expression is missing from the `for` statement, syntactically it means that the entire expression is *omitted*, not that it is there, but empty.\n\nA `for` loop with an omitted second expression loops forever because the specification of `for` statement says so. I.e. it is a dedicated feature specific to `for` alone, not something more generic.\n\nAdditionaly (a terminological nitpick) only the second expression is really a *condition*. The first and the third are not really \"conditions\".\n\n========================================\n\nCode:\n```text\nfor\n```\n\n```text\nfor\n```\n\n```text\nfor\n```\n\n```text\nfor\n```\n\n```text\nfor\n```\n\n```text\nfor\n```\n\n========================================\n\nComments:\n- Here is a link to the current draft C99 specification: open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf . The for-loop specification is on page 136.","metadata":{"transformedAt":"2026-08-18T18:32:17.800Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":51,"estimatedTokens":357}}469{"id":"stack-29235967","source":"stackoverflow","questionId":29235967,"title":"Bison one or more occurrences in grammar file","tags":["parsing","compiler-construction","bison"],"text":"Title: Bison one or more occurrences in grammar file\nTags: parsing, compiler-construction, bison\nSource: Stack Overflow\n\nQuestion:\nMy program that needs to be parsed should be of the form:\n\n```\nprogram : [declaration]+\n ;\n```\n\nWhich should mean: The program consists of one or more declarations.\nDeclaration on its turn is of course defined in a similar way, and so on...\n\nCurrently, I'm getting an error on the **+** from the Bison parser.\nHow do I define the *one or more* condition in a correct way with bison?\n\n========================================\n\nTop Answer:\nApparently,\n\nBison does not support the + or * symbols to denote these things.\n\nHow I solved it:\n\n```\nprogram : declarations\n ;\n\ndeclarations : declaration declarations\n | declaration\n ;\n```\n\n========================================\n\nCode:\n```html\nprogram   : [declaration]+\n          ;\n```\n\n```text\ndeclarations\n    : declaration\n    | declarations declaration\n    ;\n```\n\n```text\ndeclarations\n    : /* empty */\n    | declarations declaration\n    ;\n```\n\n```html\nprogram     : declarations\n        ;\n\ndeclarations    : declaration declarations\n        | declaration\n        ;\n```\n\n========================================\n\nComments:\n- That's not one or more, that's zero or more. For one or more you want to replace `&#47;* empty *&#47;` with declaration.\n- This solution is right-recursive, which is not the correct way to use an LALR(1) parser. It should be left-recursive.","metadata":{"transformedAt":"2026-08-18T18:32:17.800Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":72,"estimatedTokens":361}}470{"id":"stack-2442278","source":"stackoverflow","questionId":2442278,"title":"Question about Objective C calling convention and argument passing on ARM","tags":["iphone","compiler-construction","assembly","arm","calling-convention"],"text":"Title: Question about Objective C calling convention and argument passing on ARM\nTags: iphone, compiler-construction, assembly, arm, calling-convention\nSource: Stack Overflow\n\nQuestion:\nI want to know how objective C runtime handle arguments when I call a objective C method like\n\n```\n[NSString stringWithFomat:@\"%@, %@\", @\"Hello\", @\"World\"]\n```\n\nThere are three arguments for this objective C call, how does it work compared to typical way on a ARM system. I have known register r0, r1, r2, r3 will hold first 4 arguments, how about there are additional arguments? How does it put them on a stack and pop them later?\n\n========================================\n\nCode:\n```text\n[NSString stringWithFomat:@\"%@, %@\", @\"Hello\", @\"World\"]\n```\n\n```text\nr0 = self (NSString)\nr1 = _cmd (@selector(stringWithFormat:))\nr2 = 1st argument (@\"%@, %@\")\nr3 = 2nd argument (@\"Hello\")\n```\n\n```text\n[sp,#0] = 3rd argument (@\"World\")\n[sp,#4] = 4th argument (does not exist in your example)\n...\n```\n\n```text\n-[UIView initWithFrame:rect];\n\nr0 = self\nr1 = _cmd\nr2 = rect.origin.x\nr3 = rect.origin.y\n[sp,#0] = rect.size.width\n[sp,#4] = rect.size.height\n```\n\n```text\nNSRange retval = [self rangeOfString:string options:options range:range]\n\nr0 = &retval (of type NSRange*)\nr1 = self\nr2 = _cmd (@selector(rangeOfString:options:range:))\nr3 = string\n[sp,#0] = options\n[sp,#4] = range.location\n[sp,#8] = range.length\n```\n\n```text\nr0\n```\n\n========================================\n\nComments:\n- This is exactly what I need to know, thanks for the heads up!\n- um, I realize this is a stupid question but... how do you access the registers?","metadata":{"transformedAt":"2026-08-18T18:32:17.800Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":65,"estimatedTokens":402}}471{"id":"stack-3294360","source":"stackoverflow","questionId":3294360,"title":"Writing a compiler; which VM?","tags":["compiler-construction"],"text":"Title: Writing a compiler; which VM?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm going to try to write a compiler for a dynamic language. Preferably to some existing virtual machine --- I don't (yet) want to deal with garbage collection and the myriad other concerns a good VM handles for you. What VMs do you suggest?\n\nI'm on Linux, so I don't know if .NET (via Mono) is that good an idea. I've heard that Parrot is good for dynamic languages, but I haven't heard of *any* language use that. Should I invent my own? Does LLVM even count as a VM I should compile against, or is it as hard as straight x86?\n\nAlso, what pros and cons are there to stack-based vs register-based VMs?\n\nPerformance and tool support would be important. I'll be writing the compiler in Haskell, so a good interface with that is a plus.\n\n========================================\n\nTop Answer:\nLLVM gives you a much better programming model than straight x86 assembly. Yes, it's low-level. But you don't have to worry about register schedulign or fully optimizing your output. Also, while you're still writing your front-end, you can take advantage of its type system to catch mistakes you might make.\n\nThat said, you'll have to develop your own runtime layer to take care of the \"dynamic\" parts of your language. Just for that part alone, I might tend to stick with CLR.\n\n========================================\n\nComments:\n- What about the \"Mythical Machine\" that the famous Donald E. Knuth designed to use for explaining his algorithms? There are several MIX emulators still around.\n- But do dynamic languages, a la Python, have to use evil workarounds to work? .NET was, after all, designed for C#... Also, how is tool support for non-C# languages? I mean, I'm sure there will be byte-code level debug, but how easy would it be to write higher-level tools?\n- @pavpanchekha: The CLR wasn't really designed specifically for C# - it had other languages (such as VB.NET) in mind right from the start. With the DLR, too, it's even nicer - see IronPython, IronRuby, VB.NET, C#, and all of the languages here: en.wikipedia.org/wiki/Microsoft_.NET_Languages\n- That's the main reason I said the CLR has some advantages here - it was designed with being \"language neutral\" from day 1 - where the JVM can be done this way, it was designed for Java from the start...\n- you *still* need to worry about fully optimizing your output. And it's questionable whether register scheduling is of any use on x86. What performance benefits do you get from its 6 \"general purpose\" registers?\n- LLVM's instruction set exposes an unbounded number of SSA registers. As the programmer, you'd don't have to worry about fitting all your variables into the number of registers a platform actually offers or wondering how best to spill them to the stack.\n- In LLVM, getting to the IR representation is only the first step done by the compiler front end. Most optimization is done on that IR in the back end. If you write a tool to output IR, you can take advantage of the existing back end optimizer so you don't have to, for instance, design your system to perform loop invariant code motion.","metadata":{"transformedAt":"2026-08-18T18:32:17.800Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":30,"estimatedTokens":790}}472{"id":"stack-9561972","source":"stackoverflow","questionId":9561972,"title":"Writing unit tests in my compiler (which generates IL)","tags":["c#","unit-testing","compiler-construction","compilation","tiger"],"text":"Title: Writing unit tests in my compiler (which generates IL)\nTags: c#, unit-testing, compiler-construction, compilation, tiger\nSource: Stack Overflow\n\nQuestion:\nI'm writing a `Tiger` compiler in `C#` and I'm going to translate the `Tiger` code into `IL`.\n\nWhile implementing the semantic check of every node in my AST, I created lots of unit tests for this. That is pretty simple, because my `CheckSemantic` method looks like this:\n\n```\npublic override void CheckSemantics(Scope scope, IList errors) {\n...\n}\n```\n\nso, if I want to write some unit test for the semantic check of some node, all I have to do is build an AST, and call that method. Then I can do something like:\n\n```\nAssert.That(errors.Count == 0);\n```\n\nor\n\n```\nAssert.That(errors.Count == 1);\nAssert.That(errors[0] is UnexpectedTypeError);\nAssert.That(scope.ExistsType(\"some_declared_type\"));\n```\n\nbut I'm starting the code generation in this moment, and I don't know what could be a good practice when writing unit tests for that phase.\n\nI'm using the `ILGenerator` class. I've thought about the following:\n\n- Generate the code of the sample program I want to test\n\n- Save generated code as `test.exe`\n\n- Execute `text.exe` and store the output in `results`\n\n- Assert against `results`\n\nbut I'm wondering if there is a better way of doing it?\n\n========================================\n\nTop Answer:\nI've created a post-compiler in C# and I used this approach to test the mutated CIL:\n\n- Save the assembly in a temp file, that will be deleted after I'm done with it.\n\n- Use PEVerify to check the assembly; if there's a problem I copy it to a known place for further error analysis.\n\n- Test the assembly contents. In my case I'm mostly loading the assembly dynamically in a separate AppDomain (so I can tear it down later) and exercising a class in there (so it's like a self-checking assembly: here's a sample implementation).\n\nI've also given some ideas on how to scale integration tests in this answer.\n\n========================================\n\nCode:\n```text\npublic override void CheckSemantics(Scope scope, IList<Error> errors) {\n...\n}\n```\n\n```text\nAssert.That(errors.Count == 0);\n```\n\n```text\nAssert.That(errors.Count == 1);\nAssert.That(errors[0] is UnexpectedTypeError);\nAssert.That(scope.ExistsType(\"some_declared_type\"));\n```\n\n```text\nTiger\n```\n\n```text\nC#\n```\n\n```text\nTiger\n```\n\n```text\nIL\n```\n\n```text\nCheckSemantic\n```\n\n```text\nILGenerator\n```\n\n```text\ntest.exe\n```\n\n```text\ntext.exe\n```\n\n```text\nresults\n```\n\n```text\nresults\n```\n\n========================================\n\nComments:\n- It is good to know that. I'll do it that way then. I was just a little bit worried about the performance of lots of tests running, and creating, reading and deleting file(s) on the disk.\n- @OscarMederos: If the performance is not good enough then either (1) do profiling to figure out what is slow and fix it if you can, or (2) change your test strategy so that some tests run on every checkin, some tests run overnight, and some run over the weekend. That way you get a good balance of rapidly discovering problems and still doing thorough testing.","metadata":{"transformedAt":"2026-08-18T18:32:17.800Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":120,"estimatedTokens":778}}473{"id":"stack-1558481","source":"stackoverflow","questionId":1558481,"title":"self-taught compiler courses / good introductory compiler books?","tags":["compiler-construction","context-free-grammar","dfa"],"text":"Title: self-taught compiler courses / good introductory compiler books?\nTags: compiler-construction, context-free-grammar, dfa\nSource: Stack Overflow\n\nQuestion:\nDoes anyone know of online course / university lectures that comprise a typical compiler course? I've had theory of computing but unfortunately my school didn't offer a course in compiler construction. \n\nI know there are lectures out there; I was hoping for recommendations for particularly good offerings.\n\nAlso, are there books for newbies to the field? Something besides the dragon book, at least. Beginners level is fine, I know there are a lot of intermediate-advanced texts on the market.\n\nThanks!\n\n========================================\n\nTop Answer:\nYou can check out this course: http://ecee.colorado.edu/ecen4553\n\n **Course Information**\n\n \n High-level programming languages like\n Python make programming a breeze, but\n how do they work? There's a big gap\n between Python and machine\n instructions for modern computers.\n Learn how to translate Python programs\n all the way to Intel x86 assembly\n language.\n\n \n Most compiler courses teach one phase\n of the compiler at a time, such as\n parsing, semantic analysis, and\n register allocation. The problem with\n that approach is it is difficult to\n understand how the whole compiler fits\n together and why each phase is\n designed the way it is. Instead, each\n week we implement a successively\n larger subset of the Python language.\n The very first subset is a tiny\n language of arithmetic statements, and\n by the time we are done the language\n includes objects, inheritance, and\n first-class functions.\n\n \n Prerequisites: Fluency in at least one\n programming language (Java, C, C++,\n Python, etc.). Students will do a lot\n of programming in Python, but prior\n knowledge of Python is not required.\n The course will start with a crash\n course on Python and Python is one of\n the easiest languages to learn. Prior\n knowledge of an assembly language\n helps, but is not required.\n\n========================================\n\nComments:\n- Duplicate (many times over) stackoverflow.com/questions/1669/learning-to-write-a-compile&zwnj;&#8203;r and others\n- i've never seen a better explanation of compilation than in the Dragon book. You could do worse than persevere with it.\n- I disagree. I think that Modern Compiler Implementation In {ML,Java,C} by Andrew Appel is better than the Dragon book. The dragon book is fine as a reference, but I think if you are looking for purely self-directed learning, Modern Compiler Implementation is a more complete and coherent work.\n- possible duplicate of Learning Resources on Parsers, Interpreters, and Compilers\n- Thanks for the link. I'll have to see if he has more course info online than the website indicates-- looks like a lot of lecture material is either not there or hidden from public view.\n- @prasopes, thanks. I added a link without the year, because it might break again in the future.\n- Does someone have the answers from that duplicate SO posting? It's been deleted due to moderation reasons.","metadata":{"transformedAt":"2026-08-18T18:32:17.800Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":69,"estimatedTokens":764}}474{"id":"stack-16724593","source":"stackoverflow","questionId":16724593,"title":"Why is dynamic binary translation more practical than static binary translation?","tags":["c","assembly","compiler-construction","binaryfiles"],"text":"Title: Why is dynamic binary translation more practical than static binary translation?\nTags: c, assembly, compiler-construction, binaryfiles\nSource: Stack Overflow\n\nQuestion:\nWhen it comes to Binary Translation (Recompilation), I have always heard that dynamic binary translation is often a much better alternative to static binary translation, but I can't ever seem to grasp why behind this. Why is it always considered that static binary translation is impossible to implement in emulation? Why is dynamic binary translation **always** considered more practical?\n\nOften people compare this to the relationship between JIT (Just-In-Time) and static compilation, but this comparison would often confuse me, as both have more than practical implementations.\n\n========================================\n\nComments:\n- This answer sounds a bit odd. Why is this complex to identify parts of the program if a loader (as well as along with runtime libraries - such as ctr for C) does it pretty well? Isn't the latency incurred in realizing DTB considerable?\n- The OS loader gets all the help it needs to get its job done from metadata in the executable file. Like the relocation table and the import and export tables. There is no equivalent table that says with bytes in the text segment are code vs data.","metadata":{"transformedAt":"2026-08-18T18:32:17.800Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":14,"estimatedTokens":325}}475{"id":"stack-5267917","source":"stackoverflow","questionId":5267917,"title":"When does whitespace impact on performance?","tags":["performance","compiler-construction","whitespace","interpreter","comments"],"text":"Title: When does whitespace impact on performance?\nTags: performance, compiler-construction, whitespace, interpreter, comments\nSource: Stack Overflow\n\nQuestion:\nThis is something I've always wondered about, so here goes.\n\nWhen writing code, I was/am taught to space out lines, comment them, etc... to improve the readibility (as I guess most of us are). I obviously don't see this as any kind of problem, but it got me thinking, if all of this whitespace and commented sections are being ignored by the compiler/interpreter or whatever else, how much does this impact on the its performance?\n\nAdmittedly, I don't know a lot about how a compiler operates - only the basic concepts. However, I have a fair idea that for one to be able to \"ignore whitespace\", it would first need to identify it (at least), and that takes work, and therefore time.\n\nSo then I thought, what about whitespace or comments at extreme levels? Say, millions or billions of sections of them?\n\nI guess the question I'm asking is: At what point (ie. extreme level) will ignored sections of code impact a compiler's/interpreter's ability to produce a timely result and therefore impact on a user's experience?\n\nThanks.\n\n========================================\n\nTop Answer:\nIt will not affect the compiled data as the word implies. However please dont go for comment diarrhea, it will affect other programmers performance.\n\n========================================\n\nCode:\n```text\nnetbsd1# ls -l file* \n-rw-r--r--  1 root  wheel        1061 Mar 11 00:01 file.out\n-rw-r--r--  1 root  wheel      102041 Mar 11 00:01 file1.out\n-rw-r--r--  1 root  wheel    10200041 Mar 11 00:01 file2.out\n-rw-r--r--  1 root  wheel  1020000041 Mar 11 00:03 file3.out\nnetbsd1# for i in file*\n> do\n> echo $i\n> time ./scm $i\n> done\nfile.out\nhello world\n    0.06s real     0.01s user     0.01s system\nfile1.out\nhello world\n    0.03s real     0.01s user     0.02s system\nfile2.out\nhello world\n    0.64s real     0.28s user     0.30s system\nfile3.out\nhello world\n   61.36s real    11.78s user    41.10s system\nnetbsd1#\n```\n\n========================================\n\nComments:\n- Which language? Whitespace and comments are guaranteed to have *no* measurable impact on the performance of a compiled language.\n- \"Admittedly, I don't know a lot about how a compiler operates...\" Try taking a look at the excellent \"Compilers: Principles, Techniques, and Tools\" (amazon.com/Compilers-Principles-Techniques-Alfred-Aho/dp/&hellip;)&zwnj;&#8203;. It's old, but it's a classic text on the subject and nicely done.\n- @Cody Gary Any language, really. Also, thanks for that link nithins.\n- This was the kind of answer I was after. The final line in your answer is what interests me the most, Will. \"Will they affect it noticably? Unlikely.\" So then the point I'd like to raise is, if it's unlikely, at what point does it become 'likely'?\n- Thanks for the insight into that. It's really interesting.","metadata":{"transformedAt":"2026-08-18T18:32:17.800Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":59,"estimatedTokens":732}}476{"id":"stack-3782362","source":"stackoverflow","questionId":3782362,"title":"Using adaptive grammars","tags":["compiler-construction","grammar","dsl"],"text":"Title: Using adaptive grammars\nTags: compiler-construction, grammar, dsl\nSource: Stack Overflow\n\nQuestion:\nI'm trying to implement a language (or family of languages) whose grammar can be changed dynamically. I have found no examples that serve as study cases.\n\nCan you give me some reference to any that are actually used in the real world (even from the academic world)?\n\nDoes it make sense to implement a Domain-Specific Languages with a dynamic grammar?\n\n========================================\n\nTop Answer:\nThe Katahdin programming language offers syntax and semantic changes at runtime. It's an unsupported thesis language so don't expect to use it in production. Still, Chris Seaton's thesis may be a good resource for implementing similar ideas in your own language.\n\nSeaton is cited in this paper describing a macro system for the Fortress programming language. The authors' goal is to make syntactic extension indistinguishable from core syntax.\n\n========================================\n\nComments:\n- You mean a language in which you can just define new control structures on the fly? That's kind of strange. Do you have an example of the kinds of things you want to do for which you think you need this kind of functionality?\n- Would Lisp fit your description?\n- Since all compilers are built with tools (like Yacc and Lex and Bison) than all compiled languages are \"adaptive\" according to this definition. This question is too vague to answer.\n- @Starkey: It is often emphasized that Lisp barely has any syntax at all... @S.Lott: Quite a few language implementations use handwritten parsers (including gcc agaik). And methinks the OP is referring to parts of the program *currently parsed* changing how *the rest of the program* is parsed.\n- @delnan: How's that different from a script which includes lex, yacc and the resulting program built by yacc? It seems to vague to be answered except with a trivial \"Yup, it can be done and is done all the time.\"\n- @S.Lott: I was exploring the option of using a script pipeline to represent an adaptive grammar, but I do not think that can be formalized as a grammar must be (web.cs.wpi.edu/~jshutt/adapt/adapt.html). The reference to OMeta (tinlizzie.org/ometa) is pretty much closer to what I'm looking for, which is the possibility that one's grammar evolve over time. It does not seem to me to be equal to rebuild again and again, at least not from the standpoint of object-oriented programming.\n- EnglishScript also has a dynamic syntax, but it's still a work-in-progress.\n- This answer needs some clarification. Why is the syntax of Perl 6 considered to be \"dynamic\"?\n- How could I forget about Katahdin! There's also a related, similar one, whose name escapes me at the moment.\n- Agda has syntax declarations, which allow user-defined syntax. This is similar to the syntax extensions in the Coq proof assistant.\n- The link to PFront appears to be broken now.\n- @AndersonGreen, edited: a link to an article still works, not sure where is the download link now.","metadata":{"transformedAt":"2026-08-18T18:32:17.800Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":33,"estimatedTokens":756}}477{"id":"stack-24930887","source":"stackoverflow","questionId":24930887,"title":"Using Roslyn in F#","tags":["compiler-construction","f#","functional-programming","roslyn"],"text":"Title: Using Roslyn in F#\nTags: compiler-construction, f#, functional-programming, roslyn\nSource: Stack Overflow\n\nQuestion:\nSince Roslyn's syntax trees are immutable, and working with compilers is essentially all about transformations ( which is exactly what functional programming are good at ), I thought to myself using Roslyn in F# might be a great idea.\n\nSo, Is it feasible to combine Roslyn and F# ? Has anyone tried it before ? Does it have any advantages over using C# ? And if its possible, does it worth I start learning F# ?\n\nPS : I have some experience in functional programming with Scheme and I'm currently learning Haskell, but I haven't yet tasted F#.\n\n========================================\n\nComments:\n- I know `Roslyn` applies `C#`, but how does that tag(`C#`) apply to this question?\n- Cause in first place it's all about manipulating C# code and syntax tree! Although I can see your point.","metadata":{"transformedAt":"2026-08-18T18:32:17.800Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":16,"estimatedTokens":228}}478{"id":"stack-5729937","source":"stackoverflow","questionId":5729937,"title":"Custom programming language: how?","tags":["parsing","compiler-construction","language-design","vm-implementation","mud"],"text":"Title: Custom programming language: how?\nTags: parsing, compiler-construction, language-design, vm-implementation, mud\nSource: Stack Overflow\n\nQuestion:\nHopefully this question won't be too convoluted or vague. I know what I want in my head, so fingers crossed I can get this across in text.\n\nI'm looking for a language with a syntax of my own specification, so I assume I will need to create one myself. I've spent the last few days reading about compilers, lexers, parsers, assembly language, virtual machines, etc, and I'm struggling to sort everything out in terms of what I need to accomplish my goals (file attached at the bottom with some specifications). Essentially, I'm deathly confused as to what tools specifically I will need to use to go forward.\n\nA little background: the language made would hopefully be used to implement a multiplayer, text-based MUD server. Therefore, it needs easy inbuilt functionality for creating/maintaining client TCP/IP connections, non-blocking IO, database access via SQL or similar. I'm also interested in security insofar as I don't want code that is written for this language to be able to be stolen and used by the general public without specialist software. This probably means that it should compile to object code\n\nSo, what are my best options to create a language that fits these specifications\n\nMy conclusions are below. This is just my best educated guess, so please contest me if you think I'm heading in the wrong direction. I'm mostly only including this to see how very confused I am when the experts come to make comments.\n\nFor code security, I should want a language that compiles and is run in a virtual machine. If I do this, I'll have a hell of a lot of work to do, won't I? Write a virtual machine, assembler language on the lower-level, and then on the higher-level, code libraries to deal with IO, sockets, etc myself, rather than using existing modules?\n\nI'm just plain confused.\n\nI'm not sure if I'm making sense.\n\nIf anyone could settle my brain even a little bit, I'd sincerely appreciate it! Alternatively, if I'm way off course and there's a much easier way to do this, please let me know!\n\n========================================\n\nTop Answer:\nWriting your own language and tool chain to solve what seems to be a standard problem sounds like the wrong way to go. You'll end up developing yet another language, not writing your MUD.\n\nMany game developers take an approach of using scripting languages to describe their own game world, for example see: http://www.gamasutra.com/view/feature/1570/reflections_on_building_three_.php\n\nAlso see: https://stackoverflow.com/questions/356160/which-game-scripting-language-is-better-to-use-lua-or-python for using existing languages (Pythong and LUA) in this case for in-game scripting.\n\n========================================\n\nCode:\n```text\ndomain specific languages\n```\n\n```text\nlanguage-oriented programming\n```\n\n========================================\n\nComments:\n- This would be a great undertaking, Why not go with an interpreter instead and add security and make use of existing programming language?\n- \"For code security, I should want a language that compiles and is run in a virtual machine.\": How a virtual machine is going to help with security?\n- @Albireo, forgive him. @Sam To explain a bit, you may want to learn from Java: It's easily decompiled because it uses a virtual machine.\n- Tell me if I'm wrong! That's just the conclusion I made, and I'm rather confused right now heh. My understanding is that if I use an interpreter and interpret to, say, C, then anyone could just run away with any files I produce for that game (after they're interpreted to C), and they'd have a useable copy of my game. However, if I have files that only run in a virtual machine/runtime environment, they files and code would be useless to anyone without the software.\n- Okay, sure, I get that the code itself would be easily read... But you wouldn't find it very easy to run a java source code file without the virtual machine, would you? This is more what I meant by security than seeing the actual raw code.\n- Sam, since you need to provide your user with the virtual machine, they can simple tamper the VM and obtain the same result. To obtain security you must check every action to check if it's legal or not *server side*, not *client side*. BTW, not passing informations not needed by the client (e.g. other players positions if they can't \"see\" them) is another thing you must do.\n- @Sam: Unless your game is great, no one will steal it. If it is great, you will be able to afford some experts to help you make it hard to steal. And if you just offer access to the game via a web server, no one can steal the code unless you allow to download the code. The HTML pages which your code renders != your code.\n- What you're trying to achieve is security through obscurity, which will (not if, but when) results in a fsckup. Build your system in a way that even if the user tampers with the client, the user will not be able to get any gain.\n- I would recommend putting the security in the game itself. Since most of the people already have internet, the best way I can think of is to use a key that is verified online and focus more on making your online verification secure.\n- I think the real question the comments should be asking is, what is it that you want from a custom language that you can't get from ANY other existing language?\n- Typical CS students are able to tackle the compilation after a one week introduction course. There is literally *nothing* complicated in it. Some complex things may appear if you do a really harsh optimisation, or implement languages with unusual semantics. All the rest is just a commodity. No need in implementing your own GC, your own standard library, thinking of FFI implementation, etc. Its all available for free. You just have to combine the existing stuff.\n- Literally nothing complicated in the same way that calculus isn't complicated. If you study it, the details are clear. Most people haven't, so the details are not. Yes, there are tons of machinery lying around, some free, some commercial. But knowing how to *design* a *good* langauge is only driven somewhat by knowing how to *implement* one. And knowing how to do a good implementation is not something a CS grad with 1 week of Aho/Ullman can do. Sorry.\n- If OP wants to do it for fun, that's fine. He should be aware that it will take a huge amount of effort to get something that can run what other people would consider to be a real program.\n- 3 years seems a bit exaggerated. I managed to make a JVM language with a ton of features (custom operators, lambda expressions, pattern matching, ...) in less than a year *on my own* while going to school. So compiler construction is not a big deal (even if you don't use any frameworks except for ASM).\n- @Clashsoft: How many people use your language? Do you have a good debugger? How many parts of your language could be better or which would you do different if you started again? I have designed many \"simple\" languages and then many people started using them and all kinds of surprising problems came up. I stand my be 3 years for a real, Java/Python/C# level language which is used by thousands of people all over the globe.\n- I never stated that it was finished, only that it currently has many usable features :). github.com/Clashsoft/Dyvil\n- Interesting. Nowdays I develop in the .net world often but for that, I think will check LLVM first. I'll add more after creating first my own language :)\n- @QMaster, if performance or latency requirements are important, you'd certainly have to look at LLVM first - even before considering generating C. If you need batteries included (such as GC, for example), .net or jvm would be a bit easier to deal with.\n- I develop with Delphi and .net for years and continued nowadays. Exactly performance, clear coding, manageability and cross platforming are important for me. In the second level, I'll look at GC, etc. Thanks for your guides.","metadata":{"transformedAt":"2026-08-18T18:32:17.801Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":65,"estimatedTokens":2016}}479{"id":"stack-3650919","source":"stackoverflow","questionId":3650919,"title":"Exact difference between Compiler and Interpreter?","tags":["compiler-construction"],"text":"Title: Exact difference between Compiler and Interpreter?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\n**Possible Duplicate:**\n\n How does an interpreter/compiler work \n\nHi what is the exact difference between the Compiler and interpreter ?\n\n========================================\n\nTop Answer:\n**Compiler**\n\nA compiler generates machine-dependent assembly code which can then be assembled and linked to into the appropriate machine op-codes to allow the program to execute. This process can only be run at build time.\n\n**Interpreter**\n\nAn interpreter generates machine-independent code which can then be on-the-fly compiled to assembly code (e.g. Just-in-Time compilation). This process can be executed at build time and / or run time.\n\nHope this helps!\n\n========================================\n\nComments:\n- Another extant version: *what is the difference between compiler and interpreter?*, and similar topics *What is the difference between implementing a compiler and an interpreter?*, *There is no such thing as a “compiled language” or “interpreted language”*, *Compiled vs. Interpreted Languages*. It is a fine question, but it has been done several times already.\n- Few interpreters ever produce machine code. Unless the machine code is going to be persisted and reused (in which case the thing doing the conversion is a compiler) most interpreters figure out what needs to be done and just do it without generating any machine code first. The only case where an interpreter would normally generate machine code would be for something like the 8080's IN/OUT instruction which requires a hard-coded address. To handle an \"OUT 100,5\" instruction, the interpreter would store \"OUT 64h\" and \"RET\" instructions at some fixed spot, load A with 5, and CALL that spot.\n- @supercat Can you please explain \"most interpreters figure out what needs to be done and just do it without generating any machine code first\" bit more? how does interpreter execute the code with out generating machine code\n- @Trident: Generally, they use the equivalent of a giant `if`/`else` and/or `switch` statement. For example, the MS-BASIC interpreter that replaces keywords with tokens in the range 128-255 but leaves other parts of program text as-is would say \"if (first byte of statement is a token) use a switch to select an action. Otherwise, scan until an `=` is found, making note of the first two characters one saw, as well as whether one saw a `%` or `$`. Then evaluate whatever follows the `=` as an expression, and store that to the variable identified before the `=`'.\n- then how about the left out parts of the program, how will they be handled?\n- my basic question is actually around JIT compiler, JIT does two jobs, 1. it executes the bytecode directly. 2. it also saves the machine code for some components in an anticipation of future reuse. I understood the compilation part. But did not understand how JIT executes the bytecode with out compiling it to machine code\n- There is too much of garbage on internet, i think i got the answer - JIT compiles only those methods that are called at runtime. These methods are compiled the first time they are called, and then they are stored in a cache. When the same methods are called again, the compiled code from a cache is used for execution.","metadata":{"transformedAt":"2026-08-18T18:32:17.801Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":34,"estimatedTokens":826}}480{"id":"stack-45722082","source":"stackoverflow","questionId":45722082,"title":"What does `InputElementDiv` stand for in ECMAScript lexical grammar","tags":["javascript","ecmascript-6","compiler-construction","lexical-analysis"],"text":"Title: What does `InputElementDiv` stand for in ECMAScript lexical grammar\nTags: javascript, ecmascript-6, compiler-construction, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nThe lexical grammar of ECMAScript lists the following token classes for lexical analyzer (lexer):\n\n```\nInputElementDiv::\n WhiteSpace\n LineTerminator\n Comment\n CommonToken\n DivPunctuator\n RightBracePunctuator\nInputElementRegExp::\n WhiteSpace\n LineTerminator\n Comment\n CommonToken\n RightBracePunctuator\n RegularExpressionLiteral\nInputElementRegExpOrTemplateTail::\n WhiteSpace\n LineTerminator\n Comment\n CommonToken\n RegularExpressionLiteral\n TemplateSubstitutionTail\nInputElementTemplateTail::\n WhiteSpace\n LineTerminator\n Comment\n CommonToken\n DivPunctuator\n TemplateSubstitutionTail\n```\n\nWhile I understand the nested classes like `WhiteSpace`, `LineTerminator`, I don't understand what the top level classes are: `InputElementDiv`, `InputElementRegExp`, `InputElementRegExpOrTemplateTail` and `InputElementTemplateTail`. Can anyone please clarify?\n\n========================================\n\nCode:\n```text\nInputElementDiv::\n    WhiteSpace\n    LineTerminator\n    Comment\n    CommonToken\n    DivPunctuator\n    RightBracePunctuator\nInputElementRegExp::\n    WhiteSpace\n    LineTerminator\n    Comment\n    CommonToken\n    RightBracePunctuator\n    RegularExpressionLiteral\nInputElementRegExpOrTemplateTail::\n    WhiteSpace\n    LineTerminator\n    Comment\n    CommonToken\n    RegularExpressionLiteral\n    TemplateSubstitutionTail\nInputElementTemplateTail::\n    WhiteSpace\n    LineTerminator\n    Comment\n    CommonToken\n    DivPunctuator\n    TemplateSubstitutionTail\n```\n\n```text\nWhiteSpace\n```\n\n```text\nLineTerminator\n```\n\n```text\nInputElementDiv\n```\n\n```text\nInputElementRegExp\n```\n\n```text\nInputElementRegExpOrTemplateTail\n```\n\n```text\nInputElementTemplateTail\n```\n\n```text\n/foo/g\n```\n\n```text\n\"/\" \"foo\" \"/\" \"g\"\n```\n\n```text\n\"/foo/g\"\n```\n\n```text\n}foo${\n```\n\n```text\n\"}\" \"foo$\" \"{\"\n```\n\n```text\n\"}foo${\"\n```\n\n```text\n| expectRegex | expectTemplate | InputElement                     |\n| ----------- | -------------- | -------------------------------- |\n| false       | false          | InputElementDiv                  |\n| false       | true           | InputElementTemplateTail         |\n| true        | false          | InputElementRegExp               |\n| true        | true           | InputElementRegExpOrTemplateTail |\n```\n\n```text\nexpectRegex\n```\n\n```text\nInputElementRegExpOrTemplateTail\n```\n\n```text\nInputElementRegExp\n```\n\n```text\nInputElementTemplateTail\n```\n\n```text\nInputElementDiv\n```\n\n========================================\n\nComments:\n- Each top level class represents any one of the productions that follows its `::`. Is that what you meant? Does this help? ecma-international.org/ecma-262/8.0/&hellip;\n- Did you even read the note at the spec section you linked?\n- @Bergi I'm doing a writeup. I think that part is hard to if you don't already know what it's saying.\n- thanks a lot for you answer! I've just started looking at the spec from the compilation perspective. Hope you're active on stackoverflow and will help with deciphering :). I have a question though. Why is `CommonToken` in both `InputElementDiv` and `InputElementRegExp`? If you think I should better ask another question about that, let me know. Or maybe point me to where I should read about that. Appreciate!\n- I guess the short answer is because it can be? There's no conflict in that case, so it doesn't make a difference.\n- I'm one of the maintainers of Babel :D\n- @loganfsmyth So where is it indicated when you need to select a concrete grammar from the listed?\n- @MaximPro I don't understand. Doesn't the text I quoted make that fairly clear? \"There are several situations where the identification of lexical input elements is sensitive to the syntactic grammar context that is consuming the input elements. This requires multiple goal symbols for the lexical grammar.\"\n- @loganfsmyth Well, I mean the specification, which specifically should say what grammar to choose. I did not find this in the specification, so I'm asking.\n- @MaximPro That text is from the specification. It's explaining in words specifically how to choose which to use.\n- @loganfsmyth I understand that when one grammar does not fit, another one is used. But how to understand when this or that context occurs when you need to change the grammar? From your example, I realized only one thing: when using `\"\"` this creates a special context for RegExp or TemplateTail. Tell me, I do not understand when and under what conditions the grammar changes. For example: `This goal is used in syntactic grammar contexts where a RegularExpressionLiteral, a TemplateMiddle, or a TemplateTail is permitted.` What does it mean when permitted? When is it permitted and where?\n- \"Tell me, I do not understand when and under what conditions the grammar changes\" That is defined by the grammar of the language. So for instance `InputElementRegExp` or `InputElementRegExpOrTemplateTail` would be used anywhere ecma-international.org/ecma-262/8.0/#prod-PrimaryExpression would be valid, since that is the syntaxtic context where a `RegularExpressionLiteral` would occur. Then between those two token grammars it would depend on if you were currently processing the `https:&#47;&#47;www.ecma-international.org&#47;ecma-262&#47;8.0&#47;#prod-Templa&zwnj;&#8203;teLiteral` grammar or not.\n- @loganfsmyth Can you give some examples. It's hard enough to imagine. I just never faced this. If this (InputElementRegExp or InputElementRegExpOrTemplateTail) will be used everywhere, then when will InputElementDiv be used? Need more information, it's really difficult to understand.","metadata":{"transformedAt":"2026-08-18T18:32:17.801Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":19,"totalLines":167,"estimatedTokens":1424}}481{"id":"stack-36652221","source":"stackoverflow","questionId":36652221,"title":"Relationship between LR(0), LL(0), LALR(1), etc?","tags":["parsing","compiler-construction","ll-grammar","lr-grammar"],"text":"Title: Relationship between LR(0), LL(0), LALR(1), etc?\nTags: parsing, compiler-construction, ll-grammar, lr-grammar\nSource: Stack Overflow\n\nQuestion:\nI'm really struggling to unterstand the relationship between:\n\n- LR(0)\n\n- LL(0)\n\n- LALR(1)\n\n- SLR(1)\n\n- LR(1)\n\n- LL(1)\n\nI'm pretty sure LALR(1) and SLR(1) are subsets of LR(1), but I'm lost about the others. Are they all exclusive? Is LL(0) a subset of LL(1)?\n\nThanks\n\n========================================\n\nComments:\n- Please see this answer on the CompSci SE site (which is where this question should have been asked, except that it would have been marked as a duplicate): cs.stackexchange.com/a/48/4416\n- I'm voting to close this question as off-topic because it has already been answered on cs.stackexchange.com\n- I'm voting to close this question as off-topic because it has already been answered on cs.stackexchange.com and because it asks for a well-documented result in parsing theory.\n- sir thanks for your answer .please clear my doubt :Is LR(0) always LALR(1) ?\n- @laura Yep, that follows from the first two inclusions.\n- sir one last doubt ,just want to confirm that -:if there is An LALR(1) parser for a grammar G can have shift-reduce (S-R) conflicts if and only if -:option a)The LR(1) parser for G has S-R conflicts b)The LR(0) parser for G has S-R conflicts..so sir answer to this should be both ? sorry if i violated any protocol , i mean i know i have to make a new thread for this question ..but the question is indirectly related so i asked here ..sorry:)","metadata":{"transformedAt":"2026-08-18T18:32:17.801Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":32,"estimatedTokens":383}}482{"id":"stack-8843771","source":"stackoverflow","questionId":8843771,"title":"Learning gcc internals","tags":["c","gcc","compiler-construction"],"text":"Title: Learning gcc internals\nTags: c, gcc, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI've recently been very interested in compilers and how they work. Since gcc has it's source available, I figured it would be the best material to study.\n\nThe first thing I realized is that it would be pointless to study gcc if I didn't have a basic understand of simple compiler design principles. I have since been diligently reading the \"Dragon Book\" which, from what I have seen, is the de facto book on compiler implementation. \n\nNone-the-less, reading that book has only furthered my desire to learn about compilers such as gcc.\n\nAdditionally, I find it pertinent to say that I do have a intermediate understanding of c/c++ (aka, I'm not trying to study gcc without knowing c). I am hoping that studying gcc will help me improve upon that as well.\n\nI have downloaded the latest build I could find; however, I get lost when perusing the source code. \n\nWhat I'm looking for are suggestions on how to proceed. Is there a similar project, which is not so massive, I could use as a stepping stone to gcc? Is there a particular module of gcc which one would recommend studying first? Are there any books which go into gcc's implementation, rather than it's use? Perhaps I should stop whining and just keep reading the source until it clicks?\n\nAny and all feedback will be greatly appreciated.\n\nEDIT: If you think I should study a different compiler/interpreter, I would greatly appreciate suggestions as to which ones.\n\n========================================\n\nTop Answer:\nI would definitely look at clang/LLVM. I think the code base is very readable. One very viable option you'd have is to use LLVM as a back end and write your own simple lexer and parser.\n\n========================================\n\nCode:\n```text\nAlgorithms + Data Structures = Programs\n```\n\n========================================\n\nComments:\n- You should read the dragon book then write your own compiler. It helps tremendously.\n- I've heard GCC is a pain to hack. Maybe LLVM would be an alternative?\n- 1. You should understand the language completely at an expert level 2. Write your own compiler for a small language first 3. GCC is a mess. There are much better alternatives\n- I have been studying the dragon book. I'll look into writing my own compiler as well though. I would also like to mentions that I found gcc has documentation on their internals gcc.gnu.org/onlinedocs/gccint\n- Don't expect to learn anything, unless you are proficient in reading and understanding C. GCC is mature code (and very big). There are lots of \"tricks\" in there that you will probably not understand. Also: given its size, it will be difficult to see the overall picture (multiple target platforms, cross-compilers, bootstrapping: it's all in there ...) . For learning about compiler theory, you should choose a smaller compiler as a study object, IMHO. But you could try...\n- @cs_student: You can also read the GCC internals manual on your own system if you've installed the right package. On Debian-based systems, including Ubuntu, you should be able to install `gcc-doc` or something similar. On Red Hat based systems, it seems to be part of the \"gcc\" package. Type `info gccint` to read the documentation.\n- `8c` from plan9 is pretty easy to read, too.\n- I agree, LLVM is much better for learning/hacking than gcc. In grad school, compiler course used LLVM for projects.\n- Disclaimer: I am a little biased. My compiler project is using LLVM: ellcc.org :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.801Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":45,"estimatedTokens":880}}483{"id":"stack-13317319","source":"stackoverflow","questionId":13317319,"title":"Regular expressions - Matching whitespace","tags":["regex","compiler-construction","flex-lexer","lex","lexical-analysis"],"text":"Title: Regular expressions - Matching whitespace\nTags: regex, compiler-construction, flex-lexer, lex, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nI am having a big problem to write a regexp that will trim all the whitespace in my input.\n\nI have tried `\\s+` and `[ \\t\\t\\r]+` but that don't work.\n\nI need this because I am writing a scanner using flex, and I am stuck at matching whitespace.\nThe whitespace should just be matched and not removed.\n\nExample input:\n\n```\nprogram \n3.3 5 7 \n{ comment }\nstring\npanic: cant happen\n```\n\n========================================\n\nTop Answer:\nI'm not a specialist in flex, but have you should use /g and /m flags in your regular expression, to work with multiline srings.\n\n========================================\n\nCode:\n```text\nprogram \n3.3 5 7 \n{ comment }\nstring\npanic: cant happen\n```\n\n```text\n\\s+\n```\n\n```text\n[ \\t\\t\\r]+\n```\n\n```text\n%{\n\n#include <stdio.h>\n\n%}\n\n%option noyywrap\n\n%%\n\n[ \\n\\t\\r]+  { printf(\"Whitespace: '%s'\\n\", yytext); }\n[^ \\n\\t\\r]+ { printf(\"Non-whitespace: '%s'\\n\", yytext); }\n\n%%\n\nint main(void)\n{\n    yylex();\n    return 0;\n}\n```\n\n```text\n$ flex lexer.l\n$ gcc -o test lex.yy.c\n$ ./test < input.txt\nNon-whitespace: 'program'\nWhitespace: ' \n'\nNon-whitespace: '3.3'\nWhitespace: ' '\nNon-whitespace: '5'\nWhitespace: ' '\nNon-whitespace: '7'\nWhitespace: ' \n'\nNon-whitespace: '{'\nWhitespace: ' '\nNon-whitespace: 'comment'\nWhitespace: ' '\nNon-whitespace: '}'\nWhitespace: '\n'\nNon-whitespace: 'string'\nWhitespace: '\n'\nNon-whitespace: 'panic:'\nWhitespace: ' '\nNon-whitespace: 'cant'\nWhitespace: ' '\nNon-whitespace: 'happen'\nWhitespace: '\n'\n```\n\n```text\nflex\n```\n\n```text\n\\s\n```\n\n```text\n[ \\t\\t\\r]+\n```\n\n```text\n\\n\n```\n\n```text\n[ \\n\\t\\r]+\n```\n\n```text\nlexer.l\n```\n\n```text\ninput.txt\n```\n\n========================================\n\nComments:\n- Yes I meant \\n instead of to t:s in [ \\t\\t\\r]+ Thanks for your answer, its correct :)\n- Use `[ \\n\\t\\r\\f]+` for matching all line ending. Omitting `\\f` won't match Windows/DOS file endings. Source: web.eecs.utk.edu/~bvz/cs461/notes/flex","metadata":{"transformedAt":"2026-08-18T18:32:17.801Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":12,"totalLines":135,"estimatedTokens":509}}484{"id":"stack-247538","source":"stackoverflow","questionId":247538,"title":"Which standard c++ classes cannot be reimplemented in c++?","tags":["c++","compiler-construction"],"text":"Title: Which standard c++ classes cannot be reimplemented in c++?\nTags: c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI was looking through the plans for C++0x and came upon `std::initializer_list` for implementing initializer lists in user classes. This class could not be implemented in C++\nwithout using itself, or else using some \"compiler magic\". If it could, it wouldn't be needed since whatever technique you used to implement `initializer_list` could be used to implement initializer lists in your own class.\n\nWhat other classes require some form of \"compiler magic\" to work? Which classes are in the Standard Library that could not be implemented by a third-party library?\n\nEdit: Maybe instead of implemented, I should say instantiated. It's more the fact that this class is so directly linked with a language feature (you can't use initializer lists without `initializer_list`). \n\nA comparison with C# might clear up what I'm wondering about: IEnumerable and IDisposable are actually hard-coded into language features. I had always assumed C++ was free of this, since Stroustrup tried to make everything implementable in libraries. So, are there any other classes / types that are inextricably bound to a language feature.\n\n========================================\n\nTop Answer:\nThe only other one I could think of was the type_info class returned by typeid. As far as I can tell, VC++ implements this by instantiating all the needed type_info classes statically at compile time, and then simply casting a pointer at runtime based on values in the vtable. These are things that could be done using C code, but not in a standard-conforming or portable way.\n\n========================================\n\nCode:\n```text\nstd::initializer_list\n```\n\n```text\ninitializer_list\n```\n\n```text\ninitializer_list\n```\n\n```text\nnamespace std {\n     template<typename T> class initializer_list {\n         T internal_array[];\n         public:\n         initializer_list(T other_array[]) : internal_array(other_array) { };\n\n         // ... other methods needed to actually access internal_array\n     }\n}\n```\n\n```text\nstruct my_class {\n    ...\n    my_class(std::initializer_list<int>) ...\n}\n```\n\n```text\nmy_class m = {1, 2, 3};\n```\n\n```text\nint arry[] = {1, 2, 3};\nmy_class = arry;\n```\n\n```text\nstd::type_info\n```\n\n```text\ntypeinfo\n```\n\n```text\nstd::initializer_list\n```\n\n```text\nstd::initializer_list<typename T>\n```\n\n```text\nstd::initializer_list<typename T>\n```\n\n```text\nT[]\n```\n\n```text\nstd::initializer_list\n```\n\n```text\nstd::initializer_list\n```\n\n```text\nmy_class\n```\n\n```text\nmy_class\n```\n\n```text\nstd::initializer_list<int>\n```\n\n```text\nint[]\n```\n\n```text\nint[]\n```\n\n```text\nstd::initializer_list<int>\n```\n\n```text\nmy_class\n```\n\n```text\ninitializer_list\n```\n\n```text\nObject\n```\n\n========================================\n\nComments:\n- This may be a religious issue - Stroustrup designed C++ to allow functionality to be implemented in \"libraries\" instead of \"language features\". The extent to which this holds for implementers as well as users depends on how many of those libraries need to use secret compiler hooks to actually work.\n- So there may be resistance to admitting that you can't just implement the core language specification in \"the compiler\" and then bung the standard libraries in as pure user-mode code that works anywhere. Although Boost has a good go at it, and runtime hooks are no breach of Stroustrup's intentions.\n- How would you implement either of those without some compiler help?\n- If the code relies on compiler constructs that are not part of the standard, it is not C++.\n- Think std::string as an example. At face value, it looks magic, but you could actually implement it yourself without too much headache.\n- That's why initializer_list and type_info are so weird. As far as I can tell, everything else can be implemented by any user.\n- Untrue, and unsourced. Type_traits in fact will require compiler help. And std::printf()/std::cout somehow need to access the OS, which often is not C++ code either.\n- Even `offsetof` can't be implemented using only C++. It must use compiler builtins on strict compilers.\n- printf/cout don't need access to the OS. The standard says pretty much nothing about what those streams must be connected to. I'm pretty sure the meaning of \"standard output\" is implementation-defined. So you could write a compliant implementation of printf which just threw away its output.\n- I'm wondering which situations those are, obviously type_info and initializer_list do have special meaning hard-coded into the compiler.\n- you can implement threads using C++, it's not even that hard. Of course it's nicer to let the OS do the work but just about any language can implement some form of threads without OS support, outside of calls to processor-level interrupts and timers.\n- Maybe I'm just being dim, but I don't see how it can be done without support from the runtime, or calls to POSIX or other OS functions. Obviously you can implement a threaded OS in C++, complete with its own C++ compiler, but that's not what the questioner means.\n- You can implement threads in C/C++ without using operating system threads; you end up putting wrappers around the system calls and multiplexing the higher level threads on a single OS thread. See early Unix threading libraries before threads were in the underlying OS (eg. FreeBSD 4)\n- \"putting wrappers around the system calls\" sounds to me like something non-portable. Or it could be that we aren't understanding the same thing by the question: did FreeBSD really implement threads without jigging the compiler at all?\n- e.g, how do you know a hypothetical \"C++0x-without-threads\" compiler won't reorder instructions in a way that would, in \"C++0x-with-threads\", invalidly create a data race? Calls with memory barriers (or permit deschedule if no pre-emption) need compiler magic to prevent re-ordering across them, no?\n- No, reordering and races are not a problem. There is only one real thread (ie: the old fashioned process). The additional code is just there to make it look like threads to the application developer.\n- On portability: Yes, it is non-portable, but not because of the wrapping. And now that I recall the implementation, there was some non-C++ code that manipulated the stack pointer when a \"context switch\" occurred.\n- My basic point is that you can implement something that looks, to a programmer, like threading without OS support. All the work can be done in the user process; of course this includes the possibility of changing the runtime.\n- If you make system calls, change the runtime, and rely on implementation details like stack pointer, you aren't doing what I think the questioner asked, which is to implement X using only \"C++ without X\". If I ask \"how do I do X in C++\", I don't expect to hear \"oh, you rewrite the compiler\"...\n- So while what you say is true (it's possible to implement threads for C++ programmers, on an OS that doesn't support threads, by doing X, Y and Z to the C++ implementation), I don't think that's the same as \"implementing the thread classes in a third-party library\".\n- By the way, I have in the past worked on a system which implemented a multi-tasking environment on top of a single host process/thread. It had its own kernel, scheduler, etc. We couldn't have written the whole thing in pure C/C++ even if we'd wanted to, and we needed our own compiler for it.\n- ... admittedly for reasons other than just to support threading - for instance it implemented processes as well as threads, so obviously needed complete control of the C runtime in order to free resources at process exit. Native-compiled code simply couldn't fully inter-operate.\n- The compiler and the runtime are not the same thing. The questioner didn't mention threads, and threads are not part of the current C++ standard. What thread classes do you mean? If you push it you could come close to replacing the stack manipulations with calls to setjmp and longjmp.\n- There is no need to \"rewrite the compiler\" to implement threads. The first commenter (tloach) said that it is easy because it has been done many times before. There are existence proofs. If you ended up rewriting the compiler to support threads, you probably got it wrong.\n- ... Or you were working in a particular environment. If you have to write a \"kernel\" I have to ask what there means and in what context that was written. For a long time threads (in a Unix context) meant threads to the application rather than concurrent operation at the OS level.\n- Note that when _exit (or its Win32 equivalent TerminateProcess(), or whatever equivalent on whatever other OS) is called, stacks are not unwound and destructors are not called. There is no need to deal with that stuff on process termination on a modern OS.\n- I *know* the questioner didn't mention threads, I was suggesting something else in the C++0x standard that fits the bill, as he asked in the question: \"what *other* classes can't be implemented\".\n- And as for \"has been done many times before\", no, I don't think it has, because there's never a practical need to implement this stuff in pure portable C++.\n- And you do have to do cleanup on pseudo-process exit, just not stack unwinding. For example you must free malloced blocks (perhaps by releasing a whole heap) and flush file descriptors and streams. I don't think this conversation is really getting anywhere, sorry: we're at complete cross purposes.\n- No. Threads are not in the current C++ standard. User mode threads have been done many times before, regardless of your assertion. Pseudo process exit? On a real process exit, WIN32, Unix and others, memory and handles are released; no need for the application to do anything. Where is this not so?\n- AAAARGH! Because the system implemented multiple processes on top of a single underlying process, as I said quite clearly. So there was no \"real process exit\" when one of our processes exited, any more than in a thread implementation there's a real context switch at a pseudo-reschedule.\n- And I'm obviously not talking about the current C++ standard, and neither is the questioner, since we both clearly mention we're talking about draft C++0x. If you insist on talking only about C++03, why are you doing it in this thread?\n- And I have never asserted that threads haven't been implemented. That would be dumb. I have asserted that they haven't been implemented in pure portable C++, which is what I understand the questioner to mean by doing something without any \"compiler magic\".\n- Having a discussion like this in 300 character blocks is extremely frustrating. Real points get lost in the attempt to be extremely brief and clarity is also a casualty. On current vs. C++0x, yes I forgot the context.\n- Agreed - I think this is the second discussion I've been in where I really haven't been able to explain myself properly. I guess that's because we aren't supposed to be having the discussion on SO in the first place :-)\n- Throwing exceptions are also magic. But the exception classes are as normal as they come.\n- Indeed. But 'throw' is a language feature, not part of the standard library, so there's perhaps more expectation that it would be orthogonal to the rest of the language.\n- I should perhaps be clear that by \"exception code\" in the above I don't mean the code of the exceptions classes. I mean the code which ensures that the registered unexpected function is called when an unexpected exception is thrown. That's hidden away in the compiler out of reach of regular code.\n- This is the thing - I think you can implement atexit() using \"C++, without atexit(), but plus a bunch of compiler-specific magic\". You can't implement it using \"C++, without atexit, full stop\". But it's a very simple observation that you can implement *anything* with compiler magic to support you...\n- `cout` is initialized by means of a Schwarz counter. Given a working printf, it’s rather straightforward to implement std::cout.\n- Good point, but for hitch-free portable operation, the counter should be given a reserved name, so it doesn't clash with anything the user might define. Using reserved names is a (very mild) form of compiler magic.\n- Of course by that argument, defining anything in std:: is compiler magic, so it's impossible to implement *any* standard library feature in pure user code ;-). I guess you just draw a line in the sand what \"magic\" means.","metadata":{"transformedAt":"2026-08-18T18:32:17.801Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":24,"totalLines":175,"estimatedTokens":3111}}485{"id":"stack-5497210","source":"stackoverflow","questionId":5497210,"title":"Parsing Java Source Code","tags":["java","algorithm","parsing","compiler-construction","logic"],"text":"Title: Parsing Java Source Code\nTags: java, algorithm, parsing, compiler-construction, logic\nSource: Stack Overflow\n\nQuestion:\nI am asked to develop a software which should be able to create Flow chart/ Control Flow of the input Java source code. So I started researching on it and arrived at following solutions:\n\nTo create flow chart/control flow I have to recognize controlling statements and function calls made in the given source code Now I have two ways of recognizing:\n\n- Parse the Source code by writing my own grammars (A complex solution I think). I am thinking to use Antlr for this.\n\n- Read input source code files as text and search for the specific patterns (May become inefficient)\n\nAm I right here? Or I am missing something very fundamental and simple? Which approach would take less time and do the work efficiently? Any other suggestions in this regard will be welcome too. Any other efficient approach would help because the input source code may span multiple files and can be fairly complex.\n\nI am good in .NET languages but this is my first big project in Java. I have basic knowledge of Compiler Design so writing grammars should not be impossible for me.\n\nSorry If I am being unclear. Please ask for any clarifications.\n\n========================================\n\nTop Answer:\nAll tools handling Java code usually decide first whether they want to process the language Java or Java byte code files. That is a strategic decision and depends on your use case. I could image both for flow chart generation. When you have decided that question. There are already several frameworks or libraries, which could help you on that. For byte code engineering there are: ASM, JavaAssist, Soot, and BCEL, which seems to be dead. For Java language parsing and analyzing, there are: Polyglot, the eclipse compiler, and javac. All of these include a complete compiler frontend for Java and are open source. \n\nI would try to avoid writing my own parser for Java. I did that once. Java has a rather complex grammar, but which can be found elsewhere. The real work begins with name and type resolution. And you would need both, if you want to generate graphs which cover more than one method body.\n\n========================================\n\nComments:\n- Sounds like an interesting project. You might save yourself some work on the graphical end by using frameworks like Eclipse GMP eclipse.org/modeling/gmp If you're going for the text based approach (which might be sufficient, depending of the input complexity) you could make use of Java annotations download.oracle.com/javase/tutorial/java/javaOO/&hellip;\n- You don't want to build grammars, and try to construct this on you own. If you are good and have good tools this will take you a year. There are many Java parsers, some of which have control flow analysis as an option; use one of those and get on with your life.\n- I would be nice to select an answer.\n- @jmg: well that is tough.... cause all the approaches are different but equally applicable..this makes the choice of answer a subjective one..\n- went through the link provided by you...downloaded the .g file...now do I need to open it using Antlr???\n- well I would say...I am familiar with the language...already done some smaller projects in it.db acesss and all.......familiar a bit with the swings......I want to show the control flow in my graphs...also the class structure of the source code...\n- well..the use case says that it should be able to do it from source code files....cause they have future plans to extend it to other languages too.....So...\n- Can these language becompiled to .class files as well. e.g. Groovy, SCala, JRuby, Jython etc.? A large percentage of Java code in applications is already compiled. You will need some way to analyse it.\n- by that I meant they have future plans to extend this application for the analysis of source codes written in other languages like C/C++, JAVASCRIPT etc....so analyzing compiled code will not be an efficient solution....\n- @sudh: After rereading the question i understand that it should not analyse the classes but the single control structures. I guess this is not possible with reflection... Maybe this is the reason for the downvote...\n- I am not sure whether I understood the article properly...In that they were creating a class from the compilation unit and all...but dont I need to do the reverse?? I mean I wil have source code of a JAVA program and what I need to do is creating its class structure and control flow..I may be wrong here...\n- The link is presently dead\n- This link is dead, please remove the post or fix the link!\n- Agree with you.....can I get more info about this nJAVA 1.5 grammar I mean how to use it and all.......\n- @Sudh: Sorry to say this but if you aren't familiar with the concept of grammars and compiler generators you aren't equipped to do this project. This is not something you are going to learn in a forum.\n- I dont know from where you got the idea....but I am familiar with the concept of grammars ..have worked on basic flex/bison for some time...but that was basic....and I understand that it is advanced stuff.but there is no fun without a challange isnt it??\n- @Sudh: I got the idea about your unfamiliarity with grammars from you. You can find JavaCC at javacc.java.net. I think you could have found that for yourself frankly.\n- well we need to parse the source code...Polygot seems a good idea...but isnt it ll be a complex solution...provided our use case states that the input source code will be error free...another point is ow do I include it in my application??...hack into its source code and see whats happening?/ or something else??\n- @Sudh: It all comes down to the question, what do you need? You said, you can assume correct input. Well that's a good thing. But if you still need name and type resolution, e.g. to see which are the possible targets of a method call, then I'd go for a ready made compiler frontend. E.g. Polyglot, javac, or the eclipse java compiler. To the question of integration, is it possible that you integrate your tool into the other framework. Could your tool be a eclipse oder intellij plugin? Perhaps it's possible to configure a eclipse instance without gui. Polyglot is designed to be extended.\n- hmm....will give it a try...but how do I include (possibly merge) with my application....\n- You haven't been clear what you wanted to do with the flowgraph if you had it. In the absence of any specific requirements, the simplest scheme is to configure DMS to extract that information and simply launch it as a subprocess.\n- Its looks like an old unmaintained library though.\n- How on earth did you draw that conclusion? DMS has been under active development and enhancement for 25+ years. We build all kinds of tools with it as well as carrying active migrations of million line systems in COBOL to Java.","metadata":{"transformedAt":"2026-08-18T18:32:17.801Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":52,"estimatedTokens":1719}}486{"id":"stack-18135871","source":"stackoverflow","questionId":18135871,"title":"Calling convention on x64","tags":["compiler-construction","64-bit","x86-64","calling-convention"],"text":"Title: Calling convention on x64\nTags: compiler-construction, 64-bit, x86-64, calling-convention\nSource: Stack Overflow\n\nQuestion:\nI saw in several places that Microsoft calling conventions for `x64` platforms were narrowed down to just one: Caller cleans stack (`cdecl`), and parameters are passed in a combination of stack and registers (I am not going into the exact details here). I assume that if this is the calling convention of the OS, then probably all other compilers targeting Windows (e.g. mingw-w64) it, too.\n\nIs this calling convention true also on other major platforms (`x64` Linux, etc.)? Or does Linux still use multiple calling conventions, or even just one but a different one? Do `GCC` or `Clang` allow for multiple calling conventions on `x64` Linux?\n\n========================================\n\nCode:\n```text\nx64\n```\n\n```text\ncdecl\n```\n\n```text\nx64\n```\n\n```text\nGCC\n```\n\n```text\nClang\n```\n\n```text\nx64\n```\n\n```text\nx86\n```\n\n========================================\n\nComments:\n- Those happy days are about to end. Microsoft is adding the __vectorcall calling convention. Up to six `__m128` values passed through SSE2 registers.\n- Not pointing a finger at either side, but you'd think that after all the pain x86 calling conventions have caused, agreeing on a standard would've been a priority with x64","metadata":{"transformedAt":"2026-08-18T18:32:17.801Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":7,"totalLines":45,"estimatedTokens":331}}487{"id":"stack-487833","source":"stackoverflow","questionId":487833,"title":"Recursive Descent vs. Generated Parsers - Efficiency","tags":["compiler-construction","recursive-descent"],"text":"Title: Recursive Descent vs. Generated Parsers - Efficiency\nTags: compiler-construction, recursive-descent\nSource: Stack Overflow\n\nQuestion:\nHow do hand-written recursive descent parsers (which are inevitably LL(k)) compare to generated LALR parsers in terms of performance?\n\nI know that LALR parsers are able to handle far more grammars than LL(k); however it's my intention to write my parser by hand, and recursive descent seems the most appropriate choice. Is it possible to write any other kind by hand (reasonably readably) out of interest?\n\n**N.B.** I am using a functional language with tail-call optimisation (F#), so [well-tailored] recursion won't be as much of an issue as in other languages.\n\n========================================\n\nTop Answer:\nDeciding between LALR and LL for *performance* reasons at this point sounds like a premature optimization. Parsing time is rarely the bottleneck in a compiler. If I were you, I'd choose based on whether you are more comfortable defining your grammar bottom-up or top-down.\n\nPersonally, I find LALR grammars easy to work with, and F#'s fsyacc integration (which is how I learned parsing) makes it very easy to integrate yacc into your project.","metadata":{"transformedAt":"2026-08-18T18:32:17.801Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":17,"estimatedTokens":301}}488{"id":"stack-14544398","source":"stackoverflow","questionId":14544398,"title":"How to target LLVM from a compiler written in F#?","tags":[".net","compiler-construction","f#","llvm"],"text":"Title: How to target LLVM from a compiler written in F#?\nTags: .net, compiler-construction, f#, llvm\nSource: Stack Overflow\n\nQuestion:\nI'm part way through writing a toy language in F#. I want to target LLVM, but it looks like I'll have to do some work to make that happen.\n\nIt looks like I might be able to do some interop with the IRBuilder libraries, or I might be able to roll my own IR emitter. As far as I've searched, I couldn't find any solution that anyone else has made.\n\nHow might I do this?\n\n========================================\n\nTop Answer:\nLLVMSharp seems to be a viable solution.\nThey have a C# tutorial to build a language on top of LLVM that should be easy to convert to F#.\n\n========================================\n\nComments:\n- Whoever voted to close this question, please explain.\n- SO is not the place for What is the best way type questions - this is probably better suited to the LLVM mailing list\n- @John So if I asked how I would do this, without making note of any of the ways that I thought might work, that would make this question better? i.e. if it were \"How would I target LLVM from a compiler written in F#?\"\n- Even then that is probably to broad - SO questions are best when they are like: I tried to do `x` - here is my code `...` but this part gives me a compiler/runtime error\n- @JohnPalmer: Gotta disagree with that statement. Promoting questions like that turns SO into a group debugging center, with little chance of the questions being useful for others. Questions more along these lines, on the other hand, will serve as useful sources of information for future searchers with similar goals.\n- I sincerely think the paragraph you've removed greatly improves the quality of this question. I'm not going to make my reopen vote provisional on its inclusion (I've already cast the vote), but I'd like to urge you to replace that info, along with any other relevant details. Who knows but that you may attract even more info despite your already-accepted answer?\n- @JoshCaswell Done, though I'm hesitant considering the discussion happening right now regarding the quality of the question as it was initially.\n- MLIR is now an interesting option github.com/speakez-llc/fsharp-mlir-hello\n- Didn't think to search with [tags]; thanks also for the Github link.\n- See also Basic use of LLVM from F# – it's not free, but it sounds like exactly what is being looked for.","metadata":{"transformedAt":"2026-08-18T18:32:17.802Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":30,"estimatedTokens":601}}489{"id":"stack-21642115","source":"stackoverflow","questionId":21642115,"title":"How does the Clojure compiler generates JVM bytecode?","tags":["java","compiler-construction","clojure","compilation"],"text":"Title: How does the Clojure compiler generates JVM bytecode?\nTags: java, compiler-construction, clojure, compilation\nSource: Stack Overflow\n\nQuestion:\nWhat library/api does the Clojure compiler (which is just a jar/java application) uses to generate the JVM bytecode? I'm curious since the bytecode is being generated at runtime by java and not javac, so its a java program that outputs jvm bytecode.\n\n========================================\n\nTop Answer:\nHow does the Clojure compiler generates JVM bytecode?\n\nIt is just Java programming (unless they have bootstrapped their code generator to Clojure ... which seems unlikely). As a gross simplification1, is opening a file, writing out the code in \"classfile\" format as specified by the JVM spec, and then closing the file. (Or writing the code to a ByteArrayOutputStream or equivalent.)\n\nFor the record, there is nothing qualitatively different between what the Clojure compiler is doing and the Java compiler *can* do if you call it at runtime. But obviously there will be differences in the fine-grain details, because of differences in the languages, and different ideas and priorities for the respective developers.\n\n1 - If you want to know how code generators in general work, read a textbook on compiler writing. For all of the gory details of how Clojure does it, look at the source code ...\n\n========================================\n\nComments:\n- The one exception I know of regarding Clojure and Java bytecode generation is Clojure locals clearing, which has no Java analog iirc.\n- There is a CinC (Clojure in Clojure) project, that would generate Clojure byte code without using any Java code, but it is not yet mature.\n- It's actually a subset of ASM. As part of Clojure 1.6, the subset was recently updated to ASM 4.1 in master.","metadata":{"transformedAt":"2026-08-18T18:32:17.802Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":448}}490{"id":"stack-10473824","source":"stackoverflow","questionId":10473824,"title":"Output language/format for toy compiler","tags":["assembly","compiler-construction","language-design","vm-implementation"],"text":"Title: Output language/format for toy compiler\nTags: assembly, compiler-construction, language-design, vm-implementation\nSource: Stack Overflow\n\nQuestion:\nI took a compilers course in university, and it was very informative and a lot of fun, although also a lot of work. Since we were given a language specification to implement, one thing I didn't learn much about was language design. I'm now thinking of creating a simple toy language for fun, so that I can play around and experiment with different language design principles.\n\nOne thing I haven't decided as of yet is what language or format I'd like my compiler to output. Ideally, I'd like to output bytecode for a virtual machine which is easy to use and also has some facilities for debugging (e.g. being able to pause execution and look at the stack at any point.) I've not found one that struck my fancy yet, though. To give you an idea of what I'm looking for, here are some of the options I've considered, along with their pros and cons as I see them:\n\nI could **output textual x86 assembly language and then invoke an assembler** like NASM or FASM. This would give me some experience compiling for actual hardware, as my previous compiler work was done on a VM. I could probably debug the generated programs using gdb, although it might not be as easy as using a VM with debugging support. The major downside to this is that I have limited experience with x86 assembly, and as a CISC instruction set it's a bit daunting.\n\nI could **output bytecode for a popular virtual machine** like the JVM or Lua virtual machine. The pros and cons of these are likely to vary according to which specific VM I choose, but in general the downside I see here is potentially having to learn a bytecode which might have limited applicability to my future projects. I'm also not sure which VM would be best suited to my needs.\n\nI could **use the same VM used in my compilers course,** which was designed at my university specifically for this purpose. I am already familiar with its design and instruction set, and it has decent debugging features, so that's a huge plus. However, it is extremely limited in its capabilities and I feel like I would quickly run up against those limits if I tried to do anything even moderately advanced.\n\nI could **use LLVM and output LLVM Intermediate Representation.** LLVM IR seems very powerful and being familiar with it could definitely be of use to me in the future. On the other hand, I really have no idea how easy it is to work with and debug, so I'd greatly appreciate advice from someone experienced in that area.\n\nI could **design and implement my own virtual machine.** This has a huge and obvious downside: I'd essentially be turning my project into *two* projects, significantly decreasing the likelihood that I'd actually get anything done. However, it's still somewhat appealing in that it would allow me to make a VM which had \"first-class\" support for the language features I want—for instance, the Lua VM has first-class support for tables, which makes it easy to work with them in Lua bytecode.\n\nSo, to summarize, I'm looking for a VM or assembler I can target which is relatively easy to learn and work with, and easy to debug. As this is a hobby project, ideally I'd also like to minimize the chance that I spend a great deal of time learning some tool or language that I'll never use again. The main thing I hope to gain from this exercise is some first-hand understanding of the complexities of language design, though, so anything that facilitates a relatively quick implementation will be great.\n\n========================================\n\nTop Answer:\nHave a look at my Programming Languages ZOO. It has a number of toy implementations of languages, including some virtual machines and made-up assembly (a stack machine). It should help you get started.\n\n========================================\n\nComments:\n- A good compiler design would allow you to convert your IR into anything if you changed your code generation. I would simply start by converting your language to some IR and then create a backend that converted it to C\\C++. That way you can learn about different components of the compiler and not have to worry about verifying some lower-level language constructs in asm or bytecode.\n- Thanks for the very thorough response! I'm very much leaning towards LLVM IR now. I'll maybe look at separately doing my own x86 backend later. I don't plan to do professional work in language design or compilers, but x86 assembly is a valuable knowledge for any programmer, I figure.\n- Wouldn't that just completely ruin the fun factor? You can't get the thrill of Man over Machine by copying somebody else's compiler.\n- I never said he should copy it, but what is the point of reinventing the wheel? These implementations are very short, on the order of 500 lines, including a lot of comments. They are not real programming languages.\n- I think these are great! Being completely inexperienced at language design except insofar as having a simple grasp of what I think is a reasonably wide variety of paradigms and principles, I am certainly not expecting to invent anything completely novel on my first try. I'm more interested in playing around with different ideas, seeing which ones mesh together well, and seeing what it takes to implement them. To that end, I think these examples could be very helpful. Thanks, Andrej!\n- @Hans Passant: What precisely is your problem here? If the OP thinks it is fun to reinvent 50 years of experience with programming languages, he need not look at what other people did, and neither does he have to use the Internet. The PL Zoo is a useful *resource* becaause it demonstrates basic points about programming language design: how to implement closures, how to translate a functional language to bytecode, an imperative language to a stack machine, how to evaluate lazily, how to implement type inference, how to implement record subtyping, how to implement prolog-style search, etc.\n- Absolutely awesome resource! Brilliant! Also how beautiful is ML, honestly. Love it.","metadata":{"transformedAt":"2026-08-18T18:32:17.802Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":36,"estimatedTokens":1528}}491{"id":"stack-20850153","source":"stackoverflow","questionId":20850153,"title":"Why does inserting characters into an executable binary file cause it to \"break\"?","tags":["c","linux","compiler-construction","linker","hex-editors"],"text":"Title: Why does inserting characters into an executable binary file cause it to \"break\"?\nTags: c, linux, compiler-construction, linker, hex-editors\nSource: Stack Overflow\n\nQuestion:\nWhy does inserting characters into an executable binary file cause it to \"break\" ?\n\n**And, is there any way to add characters without breaking the compiled program?**\n\n### Background\n\nI've known for a long time that it is possible to use a hex editor to change code in a compiled executable file and still have it run as normal...\n\n### Example\n\nAs an example in the application below, `Facebook` could be changed to `Lacebook`, and the program will still execute just fine:\n\nhttps://i.sstatic.net/5VicK.png\n\nhttps://i.sstatic.net/7D2Z5.png\n\n### But it Breaks with new Characters\n\nI'm also aware that if new characters are added, it will break the program and it won't run, or it will crash immediately. For example, adding `My` in front of `Facebook` would achieve this:\n\nhttps://i.sstatic.net/DcgRa.png\n\n### What I know\n\n- I've done some work with `C` and understand that code is written in human readable, compiled, and linked into an executable file.\n\n- I've done introductory studies of assembly language and understand the concepts about data, commands, and pointers being moved around\n\n- I've written small programs for Windows, Mac and Linux\n\n### What I don't know\n\n- I don't quite understand the relationship between the operating system and the executable file. I'd guess that when you type in the name of the program and press return you are basically instructing the operating system to \"execute\" that file, which basically means loading the file into memory, setting the processor's pointer to it, and telling it 'Go!'\n\n- I understand why having extra characters in a text string of the binary file would cause problems\n\n### What I'd like to know\n\n- Why do the extra characters cause the program to break?\n\n- What thing determines that the program is broken? The OS? Does the OS also keep this program sandboxed so that it doesn't crash the whole system nowadays?\n\n- Is there any way to add in extra characters to a text string of a compiled program via a hex editor and not have the application break?\n\n========================================\n\nTop Answer:\nWhen a program is compiled into machine code, it includes many references to the addresses of instructions and data in the program memory. The compiler determines the layout of all the memory of the program, and puts these addresses into the program. The executable file is also organized into sections, and there's a table of contents at the beginning that contains the number of bytes in each section.\n\nIf you insert something into the program, the address of everything after that is shifted up. But the parts of the program that contain references to the program and data locations are not updated, they continue to point to the original addresses. Also, the table that contains the sizes of all the sections is no longer correct, because you increased the size of whatever section you modified.\n\n========================================\n\nCode:\n```text\nFacebook\n```\n\n```text\nLacebook\n```\n\n```text\nMy\n```\n\n```text\nFacebook\n```\n\n```text\nC\n```\n\n========================================\n\nComments:\n- The program counter is typically absolute, so if you move stuff around, everything breaks.\n- Very good question but it requires a big answer. If still around I will attempt to answer, in full, later. For now, think about functions as being in memory. Every time you call a function you are telling the code to *jump* to a particular location. If you add extra bytes before, you *shift* the function's code by X bytes and hence the instructions will no longer be valid and the function call will definitely not do what you think it will. All function calls are hard-coded pointers.\n- On Q3: No. Suppose you insert \"hello\" before that string \"Facebook\". Then *every* string after that shifts up by 5 positions. You'd need to find every string *pointer* that points to a string after your change and increment it. (And \"data\" is not only easy-recognizable text strings!) You'd also may have to increase the \"data section\" size, at various points if you're unlucky.\n- If you look at the bit pattern of F (46) and L (4C), you will notice that they have the same number of bits so it looks like there is a checksum somewhere that works on bits. If you try, 34($), 43 (C) or 64 (d) it might work too. Anything else will break the checksum","metadata":{"transformedAt":"2026-08-18T18:32:17.802Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":86,"estimatedTokens":1121}}492{"id":"stack-7671866","source":"stackoverflow","questionId":7671866,"title":"How does a compiler decide whether it's worth making my functions inline or not?","tags":["c++","compiler-construction"],"text":"Title: How does a compiler decide whether it's worth making my functions inline or not?\nTags: c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am wondering if anyone knows the logic which C++ compilers generally use to decide whether or not to inline a function at compilation (assuming inline has been requested).\n\nIs this type of thing public knowledge?\n\n========================================\n\nTop Answer:\nYes, this kind of information is public knowledge. Especially since there are tons of open source compilers out there.\n\nI believe the book to read is the Dragon Book. They go through all of this there, non?\n\nBut basically: A function call has a cost - setting up the registers, jumping, collecting the results. This can be counted in cycles. The function body also has a cost measured in cycles. Compare the two. Extra points for taking cache locality into account.\n\n========================================\n\nCode:\n```text\nif\n```\n\n```text\nfoo(T* t) { if (!t) { return; } <many many things> }\n```\n\n```text\ninline\n```\n\n```text\ninline\n```\n\n========================================\n\nComments:\n- Might be worth mentioning if you have a particular compiler in mind, as there are flags that you can use to influence the compiler's decisions.\n- For microsoft compiler: stackoverflow.com/questions/1204975/&hellip;\n- Whether you've requested inlining is not one of the more significant factors.\n- @AlanStokes: indeed, the `inline` keyword in C++ is mostly used to change the linkage to *weak* in order to avoid duplicated symbols error at link-time when definining simple functions in headers. Of course, the functions will probably be inlined too, but the matters are (surprisingly) orthogonal.\n- Another parameter in the cost metric is the effect of inlining on the size of the executable. It's particulary important in the domain of embedded systems.\n- Thanks guys, I like thinking about the cost in terms of clock cycles.\n- But as others point out, there are other terms to think about cost in. Space, for instance.\n- Thanks, accepted because the linked answer was very helpful :)\n- You should try to make some paragraphs... Regarding the *called only once* claim, yes it's definitely a factor. However even a `static` function called once may not be inlined. Inlining it may increase the pressure on registers.\n- @MatthieuM.: made a few paragraphs for the ease of reading, thanks.","metadata":{"transformedAt":"2026-08-18T18:32:17.802Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":50,"estimatedTokens":601}}493{"id":"stack-10613728","source":"stackoverflow","questionId":10613728,"title":"Run dynamically compiled C# code at native speed... how?","tags":["c#","dynamic","compiler-construction","assemblies"],"text":"Title: Run dynamically compiled C# code at native speed... how?\nTags: c#, dynamic, compiler-construction, assemblies\nSource: Stack Overflow\n\nQuestion:\nI have read several posts on SO about writing and compiling dynamic C# code. For example, this post. I understand it can be done several ways.\n\nHowever, calling the code invoker is slow. I did a simple benchmark, and it's some 500 X slower than calling a native method.\n\nWhat I want to be able to do is the equivalent of loading a DLL and calling one of its methods directly (\"natively\"), which will give the speed benefits I want.\n\nWhat is the easiest way to go about this? Compile the dynamic code to a dll and then load it? Can it be done in memory?\n\n**EDIT**\n\nI don't care about compilation time. Only execution.\n\n**EDIT 2, 3**\n\nHere is the benchmark code I wrote:\n\n```\npublic static int Execute(int i) { return i * 2; }\n\n private void button30_Click(object sender, EventArgs e)\n {\n CSharpCodeProvider foo = new CSharpCodeProvider();\n\n var res = foo.CompileAssemblyFromSource(\n new System.CodeDom.Compiler.CompilerParameters()\n {\n GenerateInMemory = true,\n CompilerOptions = @\"/optimize\", \n },\n @\"public class FooClass { public static int Execute(int i) { return i * 2; }}\"\n );\n\n var type = res.CompiledAssembly.GetType(\"FooClass\");\n var obj = Activator.CreateInstance(type);\n var method = type.GetMethod(\"Execute\");\n int i = 0, t1 = Environment.TickCount, t2;\n //var input = new object[] { 2 };\n\n //for (int j = 0; j ) Delegate.CreateDelegate(typeof (Func), method);\n\n t1 = Environment.TickCount;\n\n for (int j = 0; j funcL = Execute;\n\n t1 = Environment.TickCount;\n\n for (int j = 0; j < 100000000; j++)\n {\n i = funcL(j);\n }\n\n t2 = Environment.TickCount;\n\n MessageBox.Show(\"Delegate: \" + (t2 - t1).ToString() + Environment.NewLine + i.ToString());\n }\n```\n\n========================================\n\nTop Answer:\nBesides Marc's advice you could improve speed by specifying the \"optimize\" compiler option:\n\n```\nvar res = foo.CompileAssemblyFromSource(\n new System.CodeDom.Compiler.CompilerParameters()\n {\n GenerateInMemory = true,\n CompilerOptions = \"/optimize\"\n },\n```\n\n========================================\n\nCode:\n```text\npublic static int Execute(int i) { return i * 2; }\n\n    private void button30_Click(object sender, EventArgs e)\n    {\n        CSharpCodeProvider foo = new CSharpCodeProvider();\n\n        var res = foo.CompileAssemblyFromSource(\n            new System.CodeDom.Compiler.CompilerParameters()\n            {\n                GenerateInMemory = true,\n                CompilerOptions = @\"/optimize\",                    \n            },\n            @\"public class FooClass { public static int Execute(int i) { return i * 2; }}\"\n        );\n\n        var type = res.CompiledAssembly.GetType(\"FooClass\");\n        var obj = Activator.CreateInstance(type);\n        var method = type.GetMethod(\"Execute\");\n        int i = 0, t1 = Environment.TickCount, t2;\n        //var input = new object[] { 2 };\n\n        //for (int j = 0; j < 10000000; j++)\n        //{\n        //    input[0] = j;\n        //    var output = method.Invoke(obj, input);\n        //    i = (int)output;\n        //}\n\n        //t2 = Environment.TickCount;\n\n        //MessageBox.Show((t2 - t1).ToString() + Environment.NewLine + i.ToString());\n\n        t1 = Environment.TickCount;\n\n        for (int j = 0; j < 100000000; j++)\n        {\n            i = Execute(j);\n        }\n\n        t2 = Environment.TickCount;\n\n        MessageBox.Show(\"Native: \" + (t2 - t1).ToString() + Environment.NewLine + i.ToString());\n\n        var func = (Func<int, int>) Delegate.CreateDelegate(typeof (Func<int, int>), method);\n\n        t1 = Environment.TickCount;\n\n        for (int j = 0; j < 100000000; j++)\n        {\n            i = func(j);\n        }\n\n        t2 = Environment.TickCount;\n\n        MessageBox.Show(\"Dynamic delegate: \" + (t2 - t1).ToString() + Environment.NewLine + i.ToString());\n\n        Func<int, int> funcL = Execute;\n\n        t1 = Environment.TickCount;\n\n        for (int j = 0; j < 100000000; j++)\n        {\n            i = funcL(j);\n        }\n\n        t2 = Environment.TickCount;\n\n        MessageBox.Show(\"Delegate: \" + (t2 - t1).ToString() + Environment.NewLine + i.ToString());\n    }\n```\n\n```text\nAction doSomething = (Action)Delegate.CreateDelegate(typeof(Action), method);\n```\n\n```text\nIFoo foo = (IFoo)Activator.CreateInstance(...);\n```\n\n```text\nfoo.Bar();\n```\n\n```text\nMethodInfo\n```\n\n```text\nDelegate\n```\n\n```text\nAction\n```\n\n```text\nFunc<object[], object>\n```\n\n```text\nDelegate.CreateDelegate\n```\n\n```text\nExpression\n```\n\n```text\n.Compile()\n```\n\n```text\nDynamicMethod\n```\n\n```text\nCreateDelegate()\n```\n\n```text\n.DynamicInvoke\n```\n\n```text\nsomeDelegate.DynamicInvoke(...)\n```\n\n```text\nsomeMethod.Invoke(...)\n```\n\n```text\nvar res = foo.CompileAssemblyFromSource(\n        new System.CodeDom.Compiler.CompilerParameters()\n        {\n            GenerateInMemory = true,\n            CompilerOptions = \"/optimize\"\n        },\n```\n\n```text\npublic void Test(Func<int> func)\n{        \n    var watch = new Stopwatch();\n    watch.Start();\n    for (var i = 0; i <= 1000000; i++)\n    {\n        var test = func();\n    }\n    Console.WriteLine(watch.ElapsedMilliseconds);\n}\n\npublic class FooClass { public int Execute() { return 1;}}\n```\n\n```text\nusing (Microsoft.CSharp.CSharpCodeProvider foo = \n       new Microsoft.CSharp.CSharpCodeProvider())\n{\n    var res = foo.CompileAssemblyFromSource(\n        new System.CodeDom.Compiler.CompilerParameters() \n        {  \n            GenerateInMemory = true \n        }, \n        \"public class FooClass { public int Execute() { return 1;}}\"\n    );\n\n    var real = new FooClass();\n    Test(() => real.Execute());                   // benchmark, direct call\n\n    var type = res.CompiledAssembly.GetType(\"FooClass\");\n    var obj = Activator.CreateInstance(type);    \n    var method = type.GetMethod(\"Execute\");\n    var input = new object[] { };                \n    Test(() => (int)method.Invoke(obj, input));   // reflection invoke  \n\n    dynamic dyn = Activator.CreateInstance(type);  \n    Test(() => dyn.Execute());                    // dynamic object invoke\n\n    var action = (Func<int>)Delegate.CreateDelegate(typeof(Func<int>), null, method); \n    Test(() => action());                         // delegate\n}\n```\n\n```text\n8     // direct\n771   // reflection invoke\n41    // dynamic object invoke\n7     // delegate\n```\n\n```text\ndynamic\n```\n\n========================================\n\nComments:\n- Have you taken into account the compilation time? how does your benchmark look?\n- executing C# build runtime is not 500x times slower than executing C# build prior. They are identical however there's overheads to be considered. What's your benchmark like, are you using Reflection.Emit or some compiler service to compile?\n- The Invoke is what's slow, which is the center of my question: how can you call the method at native speed?\n- As a final note, the test above, when more accurately timed, is actually 1100 X slower. The delegate solution Marc gave is 8.4 X slower than native.\n- I fully agree with your conclusion but don't you think that a difference of magnitude of 500 is related to something else than dynamic invokation (E.g. benchmarking errors)\n- @RuneFS the method being tested is `return i*2;` - I think that is perfectly reasonably to expect a magitude 500 - i.e. the code being tested is vanishingly small, and all that is being tested is invoke speed.\n- @RuneFS For more information, have a look here: msmvps.com/blogs/jon_skeet/archive/2008/08/09/&hellip;\n- Guess I was unclear then that's my point exactly it's not the execution speed of the compiled code being measured.\n- Thanks much @MarcGravell :) That solves it... I'll look up those solutions in MSDN.\n- @IanC for concrete example: `var func = (Func) Delegate.CreateDelegate(typeof (Func), method);` ... `i = func(j);`\n- @MarcGravell wouldn't it still be faster to compile to a DLL and link the DLL? This would negate the need for the delegate, which from my tests is 8.4 X slower (in this example) than a native method call.\n- @IanC 8x sounds unlikely; in most tests the difference would be absolutely trivial. However, see my earlier point: you could also implement an interface (declared in a shared dll that both can see) and use static-binding to the interface. Typed delegate invoke is not slow - sounds like a bad test rig, to be honest.\n- @MarcGravell this is the test rig: var func = (Func) Delegate.CreateDelegate(typeof (Func), method);\n- @MarcGravell I also used a delegate that pointed to a local static method (not dynamically compiled), and got the same speed results.\n- @MarcGravell I updated the code to reflect my tests. I am still getting 8.4 X slower on delegates. I'm compiling to .Net 4, 64 bit.\n- @MarcGravell I want your computer, then. We're running the same code.\n- @IanC I assume you're in release, command-line, optimized, etc? I'm on a quad-core (8 with HT) i7.\n- I'm running the EXE (WinForm, not console) outside of VS. It's compiled for Any CPU, .Net 4.0, with optimizations on. I am running Win 7 64 bit, Core 2 Duo CPU.\n- @MarcGravell I actually posted this as a separate question. Some posters reported a factor of 4. Not sure why there is such a wild difference between systems.\n- @MarcGravell out of pure interest, I re-tested this on an i7 and under .Net 4.5, and it's a little over 1.4 X difference now. Same code.\n- @IanC yes, interestingly reflection seems to have gotten faster, iirc\n- I get different timings on my test. For mine, direct was 7.8, delegate was 43.7.","metadata":{"transformedAt":"2026-08-18T18:32:17.802Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":21,"totalLines":300,"estimatedTokens":2385}}494{"id":"stack-6020435","source":"stackoverflow","questionId":6020435,"title":"Compiler as a service: How to build Refactoring tools?","tags":["c#","compiler-construction","mono","compiler-as-a-service","roslyn"],"text":"Title: Compiler as a service: How to build Refactoring tools?\nTags: c#, compiler-construction, mono, compiler-as-a-service, roslyn\nSource: Stack Overflow\n\nQuestion:\nLisa Feigenbaum from Microsoft talks here about \"Compiler as a service\". I have read this would make it easier to build refactoring tools. How? Mono's CAAS is great but if Microsoft version is similar I don't see how this specific use case is done.\n\n========================================\n\nTop Answer:\nThe Roslyn CTP includes a walkthrough for building s \"Code Action\" which is our terminology for something that can be either \"quick fix\", if it is linked to something wrong with the code, or a refactoring if it is offered contextually.\n\nAlso take a look at the CodeRefactoring project template that you will see in Visual Studio if you install the Roslyn CTP.\n\n========================================\n\nComments:\n- MSFT is a ticker symbol.\n- Thanks @Martinho, I knew there was a term I didn't remember there :)\n- Since you *italicized* it yourself, I have to ask: what is elapsed engineering? As in elapsed time?\n- That's phrased badly, but yes, it is 15 elapsed years of engineering. I changed the word to \"continuous\"","metadata":{"transformedAt":"2026-08-18T18:32:17.802Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":297}}495{"id":"stack-1868569","source":"stackoverflow","questionId":1868569,"title":"Compiling to idiomatic C","tags":["compiler-construction","haskell","lisp"],"text":"Title: Compiling to idiomatic C\nTags: compiler-construction, haskell, lisp\nSource: Stack Overflow\n\nQuestion:\nAre there any compilers out there for function or lisp-ish languages that compile to idiomatic C? Most compilers out there seem to provide something resembling a machine language composed of C macros. I'm wondering if there is anything out there that can produce readable C code based on a higher-level language.\n\n========================================\n\nTop Answer:\nGiven that you tagged this question with Haskell I'll give a Haskell answer. JHC is a Haskell compiler which produces fairly idiomatic ansi-C. It's an optimizing whole-program compiler that can produce incredibly fast binaries. I'm not sure how stable it is at the moment though, and there seems to be limits to how big programs it can handle. But don't take my word for it, check it out yourself.\n\n========================================\n\nComments:\n- But... why would you want it to do that?\n- The problem is that many high-level constructs are messy on C level no matter how you look. Consider nested closures, for example.\n- I'm wondering if there are compilers that are smart enough to figure out how to convert a tail recursive loop to a for loop, for example, or to convert things to CPS and pass data structures around using a trampoline. At least this would be generally readable and understandable.\n- GCC is smart enough to spot tail recursive loops (or even nearly almost-tail recursive loops) and turn it into iteration -- from C to C, which makes doing the same at a higher level seem like extraneous work.\n- Steve: Sure, every Scheme compiler is required to spot tail-recursion.\n- Ken: Yes, but have you looked at the output of say Gambit-C? It's not readable, even if it technically does that. I'm wondering if any compilers do that in a *readable* way. ephemient: That's a good point, converted to for-loops is not required, it was just an example of what I was thinking about. Readability is the main point here.\n- There are several Haskell compilers and more than one that can compile to C, so it would be helpful if you stated which one you where talking about. If you're referring to GHC, then yes, its generated C is not pretty and far from idiomatic. JHC is much better in this respect. GHC is trying hard these days to move away from compiling via C though. They're working on a new fancy backend which will be able to do all the low level optimizations they've used the C compiler for before.\n- I fail to see why I deserve a downvote, as my answer is not wrong?\n- I didn't downvote you, but sorry, it *is* wrong. I specifically asked for \"idiomatic C\", and you answered with something that generates C that \"isn't that pretty looking\", the opposite of what i asked for. (But thanks anyway!)\n- My bad, I literally didn't see that word there at all.\n- I gave the point to Lisp-to-C, since this project was designed specifically for my question, but it's unfortunate that it's no longer available. I was really considering giving you the point, since JHC seems to generate pretty friendly code as you suggested. If only I could give multiple points. Anyways, thanks for pointing me to JHC, it's a very nice project. I generated a simple executable that was only 16K!","metadata":{"transformedAt":"2026-08-18T18:32:17.802Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":26,"estimatedTokens":816}}496{"id":"stack-9772508","source":"stackoverflow","questionId":9772508,"title":"the expression of label in llvm IR code","tags":["compiler-construction","llvm","llvm-ir"],"text":"Title: the expression of label in llvm IR code\nTags: compiler-construction, llvm, llvm-ir\nSource: Stack Overflow\n\nQuestion:\nSometimes I found the label identifier in llvm IR is started with comma ';', such as \n`; 6`\nhowever as I kown, the comma is used for comments. So how can llvm retrive the label info in comments? Am I missing something?\n\nOne simple test is followed.\n\nthe C source file:\n\n```\n#include \n\nint main()\n{\n int a;\n scanf(\"%d\", &a);\n if ( a > 3)\n a *= 2;\n return 0;\n}\n```\n\nthe llvm IR code generated by http://llvm.org/demo/index.cgi (same as clang -c -emit-llvm main.c) is following:\n\n```\n; ModuleID = '/tmp/webcompile/_13654_0.bc'\n\n@.str = private unnamed_addr constant [3 x i8] c\"%d\\00\", align 1\n\ndefine i32 @main() nounwind uwtable {\n %a = alloca i32, align 4\n %1 = call i32 (i8*, ...)* @__isoc99_scanf(i8* getelementptr inbounds ([3 x i8]* @.str, i64 0, i64 0), i32* %a) nounwind\n %2 = load i32* %a, align 4, !tbaa !0\n %3 = icmp sgt i32 %2, 3\n br i1 %3, label %4, label %6\n\n; :4 ; preds = %0\n %5 = shl nsw i32 %2, 1\n store i32 %5, i32* %a, align 4, !tbaa !0\n br label %6\n\n; :6 ; preds = %4, %0\n ret i32 0\n}\n```\n\n========================================\n\nTop Answer:\nDespite the wording, the `%4` in `label %4` is NOT a label, it is just a reference to the block. You are right, this is very confusing, see this issue for discussion.\n\n========================================\n\nCode:\n```text\n#include <stdio.h>\n\nint main()\n{\n int a;\n scanf(\"%d\", &a);\n if ( a > 3)\n  a *= 2;\n return 0;\n}\n```\n\n```text\n; ModuleID = '/tmp/webcompile/_13654_0.bc'\n\n@.str = private unnamed_addr constant [3 x i8] c\"%d\\00\", align 1\n\ndefine i32 @main() nounwind uwtable {\n  %a = alloca i32, align 4\n  %1 = call i32 (i8*, ...)* @__isoc99_scanf(i8* getelementptr inbounds ([3 x i8]* @.str, i64 0, i64 0), i32* %a) nounwind\n  %2 = load i32* %a, align 4, !tbaa !0\n  %3 = icmp sgt i32 %2, 3\n  br i1 %3, label %4, label %6\n\n; <label>:4                                       ; preds = %0\n  %5 = shl nsw i32 %2, 1\n  store i32 %5, i32* %a, align 4, !tbaa !0\n  br label %6\n\n; <label>:6                                       ; preds = %4, %0\n  ret i32 0\n}\n```\n\n```text\n; <label> 6\n```\n\n```text\nbr i1 %3...\n```\n\n```text\nbr\n```\n\n```text\n%4\n```\n\n```text\nlabel %4\n```\n\n========================================\n\nComments:\n- Thanks for your help. It seems a little strange. Could you give me some advice for me to learn LLVM?","metadata":{"transformedAt":"2026-08-18T18:32:17.802Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":117,"estimatedTokens":601}}497{"id":"stack-731639","source":"stackoverflow","questionId":731639,"title":"Adding a pass to gcc?","tags":["c","gcc","compiler-construction"],"text":"Title: Adding a pass to gcc?\nTags: c, gcc, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nHas anybody added a pass to gcc ? or not really a pass but adding an option to do some nasty things... :-) ...\n\nI still have the same problem about calling a function just before returning from another...so I would like to investigate it by implementing something in gcc...\n\nCheers.\n\n**EDIT**: Adding a pass to a compiler means revisiting the tree to perform some optimizations or some analysis. I would like to emulate the behavior of __cyg_profile_func_exit but only for some functions and be able to access the original return value.\n\nSo I'm going to try to enhance my question. I would like to emulate really basic AOSD-like behavior. AOSD or Aspect oriented programming enables to add crosscutting concerns (debugging is a cross-cutting concern).\n\n```\nint main(int argc, char ** argv) {\n return foo(argc);\n}\n\nint foo(int arg_num) { \n int result = arg_num > 3 ? arg_num : 42;\n return result;\n}\n\nint dbg(int returned) {\n printf(\"Return %d\", returned);\n}\n```\n\nI would like to be able to say, I'd like to trigger the dbg function after function foo has been executed. The problem is how to tell the compiler to modify the control flow and execute dbg. dbg should be executed between return and foo(argc) ...\n\nThat's really like __cyg_profile_function_exit but only in some cases (and the problem in __cyg_profile_function_exit is that you cannot easily see and modify the returned value).\n\n========================================\n\nTop Answer:\nJust for future reference: Upcoming versions of gcc (4.4.0+) will provide support for plugins specifically meant for use cases such as adding optimization passes to the compiler without having to bootstrap the whole compiler.\n\n May 6, 2009:GCC can now be extended using a generic plugin framework on host platforms that support dynamically loadable objects.\n (see gcc.gnu.org)\n\n========================================\n\nCode:\n```text\nint main(int argc, char ** argv) {\n  return foo(argc);\n}\n\nint foo(int arg_num) { \n   int result = arg_num > 3 ? arg_num : 42;\n   return result;\n}\n\nint dbg(int returned) {\n   printf(\"Return %d\", returned);\n}\n```\n\n```text\n#include <stdio.h>\nvoid a(void) {\n   b();\n}\nvoid b(void) {\n   printf(\"Hello World\\n\");\n}\nint main(int ac, char *av[]) {\n   a();\n   return 0;\n}\n```\n\n```text\n-std=c99 -Wall -pedantic\n```\n\n```text\nint main(int argc, char ** argv) {\n    return dbg(foo(argc));\n}\n```\n\n```text\nvoid __cyg_profile_func_enter (void *this_fn,\n                                         void *call_site);\n          void __cyg_profile_func_exit  (void *this_fn,\n                                         void *call_site);\n```\n\n========================================\n\nComments:\n- Err... What with the who now?\n- Didn't someone once demonstrate a hack that put malicious code in when it compiled itself from sources (and those sources could be without the bad code)?\n- I have no idea what you're talking about, and I do know something about C, gcc, and compilers.\n- Could you be a little bit more specific? What do you want to achieve?\n- Adding a pass to a compiler means revisiting the tree to perform some optimizations or some analysis. I would like to emulate the behavior or __cyg_profile_func_exit but only for some functions and be able to access the original return value.\n- @brian you are thinking of none other than Ken Thompson\n- LB: edit your question and add what you wrote in the comment.\n- @brian: Sounds like the classic essay, \"Reflections on Trusting Trust\" by Ken Thompson cm.bell-labs.com/who/ken/trust.html\n- I'm pretty sure someone has done it before :-) ... I only wanted to avoid some painful moments by asking this community. Actually the documentation of gcc provides a bunch of patches representing trivial passes. But I wanted to find some more precisions. My question was really too generic. sorry..\n- the -finstrument-function enables to use __cyg_profile_function_exit, I think I'm going to add an option and an attribute to gcc to do my own version. I've used interposing before for malloc and free but the problem is that return is a keyword, so you cannot capture it.\n- Hmm... I don't really understand. You're saying you need to execute dbg() after foo() ONLY when \"return foo();\" and not during other, say, \"if (foo()){}\" calls?\n- I dunno, I think interposing would work well for you. What's the difference between inserting code to modify the 'return' statement of X() and interposing another X() that calls the first X() but modifies the return it received? It's all the same to the caller, right?\n- yes... I know this work... I'm trying to minimize the impact of my modification on the developers (but even recompiling gcc can be tricky)...I am going to try LLVM to see...thanks...\n- thanks...i knew these references though... I wanted something better-documented more like a tutorial... Looking at patches implementing passes is quite cumbersome...thanks though\n- Hmm... To the best of my knowledge (I occasionally contribute to GCC) if any such tutorial existed, it'd be immediately linked from those pages.\n- just for your info: the infrastructure is already in place, so if you don't mind getting the sources from their repository, then you can get started immediately","metadata":{"transformedAt":"2026-08-18T18:32:17.802Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":110,"estimatedTokens":1320}}498{"id":"stack-4177918","source":"stackoverflow","questionId":4177918,"title":"Why compilers don't translate in simpler languages?","tags":["language-agnostic","compiler-construction","programming-languages"],"text":"Title: Why compilers don't translate in simpler languages?\nTags: language-agnostic, compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nUsually compilers translate from the language they support to assembly. Or at most to an assembly-like language (bytecode), like GIMPLE/GENERIC for GCC or Python/Java/.NET bytecode.\n\nWouldn't it be simpler for a compiler translate to a simpler language, which already implement a big subset of their grammar?\n\nFor example an Objective-C compiler, which is 100% compatible with C, could add the semantics only for the syntax it extends to C's, translating it into C. I can see many advantages of doing this; one could use this Objective-C compiler to translate its code into C in order to compile the generated C code with a different compiler that doesn't support C++ (but that optimizes more, or that compiles quicker, or able to compile for more architectures). Or one would be able to use the generated C code in a project where only C is allowed.\n\nI guess/hope that if things were working like this, it would have been a lot easier to write *extensions* for current languages (eg: adding to C++ keywords to ease the implementation of common patterns, or, still in C++, removing the *declare before use* rule by moving inline member functions to the end of header files)\n\nWhat kind of penalties would there be? Generated code would be very difficult to be understood by humans? Compilers wouldn't be able to optimize as much as they can now? What else?\n\n========================================\n\nTop Answer:\nIncidentally, as a counterexample, Tcl is one language that is known to be very-very hard (if not totally impossible) to translate to C. Over the last 20 years there have been a couple of projects that tried this, even one promise of a commercial product but none have materialized.\n\nIn part it is because Tcl is a very dynamic language (as any language with an eval function is). In part it is because the only way to know if something is code or data is to run the program.\n\n========================================\n\nComments:\n- Incidentally, the first C++ compiler worked that way- Compiling C++ code to C code which was then compiled via a C Compiler. They ran into trouble, however, as the C++ language got more complex: en.wikipedia.org/wiki/Cfront\n- Indeed, Ratfor was designed to be just this: en.wikipedia.org/wiki/Ratfor\n- See also the question: Bootstrapping a compiler: why?\n- all fun languages are hard to translate into C :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.802Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":29,"estimatedTokens":629}}499{"id":"stack-23718027","source":"stackoverflow","questionId":23718027,"title":"Code generation for expressions with fixed/preassigned register","tags":["c","algorithm","compiler-construction","code-generation","compiler-theory"],"text":"Title: Code generation for expressions with fixed/preassigned register\nTags: c, algorithm, compiler-construction, code-generation, compiler-theory\nSource: Stack Overflow\n\nQuestion:\nI'm using this (see below) algorithm(take idea from this answer) to code generation from a tree. I'm targeting x86 arch, now I need to deal with mul/div instructions which uses registers eax/ebx as argument. \n\nMy question is: \n\nHow do I modify this to load operands of a certain instruction to load at fixed register? say, for `mul` instruction load left and right subtree on `eax` and `ebx` registers. My current implementation is: pass current node begin evaluated as argument and if it's `MUL` or `DIV` set `reg` to `R0` or `R1` according to tree's side, if it's `LEFT` or `RIGHT` respectively. If `reg` is `in_use`, push `reg` on stack and mark it as begin free(not implmented yet). The current implementation doesn't work because it does assert in `assert(r1 != r2)` in `emit_load()` function (meaning both registers passed as argument are equals like `r1 = REG_R0` and `r2 = REG_R0`)\n\n```\nvoid gen(AST *ast, RegSet in_use, AST *root) {\n if(ast->left != 0 && ast->right != 0) {\n Reg spill = NoRegister; /* no spill yet */\n AST *do1st, *do2nd; /* what order to generate children */\n if (ast->left->n >= ast->right->n) {\n do1st = ast->left;\n do2nd = ast->right;\n } else {\n do1st = ast->right;\n do2nd = ast->left; }\n gen(do1st, in_use);\n in_use |= 1 reg;\n if (all_used(in_use)) {\n spill = pick_register_other_than(do1st->reg);\n in_use &= ~(1 reg = ast->left->reg\n emit_operation(ast->type, ast->left->reg, ast->right->reg);\n if (spill != NoRegister)\n emit_operation(POP, spill);\n } else if(ast.type == Type_id || ast.type == Type_number) {\n if(node->type == MUL || node->type == DIV) {\n REG reg;\n if(node_side == ASTSIDE_LEFT) reg = REG_R0; \n if(node_side == ASTSIDE_RIGHT) reg = REG_R1;\n if(is_reg_in_use(in_use, reg)) {\n emit_operation(PUSH, reg);\n }\n\n } else {\n ast->reg = pick_unused_register(in_use);\n emit_load(ast);\n }\n } else {\n print(\"gen() error\");\n // error\n }\n }\n\n// ershov numbers\nvoid label(AST ast) {\n if(ast == null)\n return;\n\n label(ast.left);\n label(ast.right);\n\n if(ast.type == Type_id || ast.type == Type_number)\n ast.n = 1;\n // ast has two childrens\n else if(ast.left not null && ast.right not null) { \n int l = ast.left.n;\n int r = ast.right.n;\n\n if(l == r)\n ast.n = 1 + l;\n else\n ast.n = max(1, l, r);\n }\n // ast has one child\n else if(ast.left not null && ast.right is null)\n ast.n = ast.left.n;\n else\n print(\"label() error!\");\n}\n```\n\n========================================\n\nCode:\n```text\nvoid gen(AST *ast, RegSet in_use, AST *root) {\n            if(ast->left != 0 && ast->right != 0) {\n                Reg spill = NoRegister; /* no spill yet */\n                AST *do1st, *do2nd;     /* what order to generate children */\n                if (ast->left->n >= ast->right->n) {\n                    do1st = ast->left;\n                    do2nd = ast->right;\n                } else {\n                    do1st = ast->right;\n                    do2nd = ast->left; }\n                gen(do1st, in_use);\n                in_use |= 1 << do1st->reg;\n                if (all_used(in_use)) {\n                    spill = pick_register_other_than(do1st->reg);\n                    in_use &= ~(1 << spill);\n                    emit_operation(PUSH, spill); \n                }\n                gen(do2nd, in_use);\n                ast->reg = ast->left->reg\n                emit_operation(ast->type, ast->left->reg, ast->right->reg);\n                if (spill != NoRegister)\n                    emit_operation(POP, spill);\n            } else if(ast.type == Type_id || ast.type == Type_number) {\n                if(node->type == MUL || node->type == DIV) {\n                    REG reg;\n                    if(node_side == ASTSIDE_LEFT)  reg = REG_R0; \n                    if(node_side == ASTSIDE_RIGHT) reg = REG_R1;\n                    if(is_reg_in_use(in_use, reg)) {\n                        emit_operation(PUSH, reg);\n                    }\n\n                } else {\n                  ast->reg = pick_unused_register(in_use);\n                  emit_load(ast);\n             }\n            } else {\n                print(\"gen() error\");\n                // error\n            }\n    }\n\n// ershov numbers\nvoid label(AST ast) {\n    if(ast == null)\n        return;\n\n    label(ast.left);\n    label(ast.right);\n\n    if(ast.type == Type_id || ast.type == Type_number)\n        ast.n = 1;\n    // ast has two childrens\n    else if(ast.left not null && ast.right not null) {      \n        int l = ast.left.n;\n        int r = ast.right.n;\n\n        if(l == r)\n            ast.n = 1 + l;\n        else\n            ast.n = max(1, l, r);\n    }\n    // ast has one child\n    else if(ast.left not null && ast.right is null)\n        ast.n = ast.left.n;\n    else\n        print(\"label() error!\");\n}\n```\n\n```text\nmul\n```\n\n```text\neax\n```\n\n```text\nebx\n```\n\n```text\nMUL\n```\n\n```text\nDIV\n```\n\n```text\nreg\n```\n\n```text\nR0\n```\n\n```text\nR1\n```\n\n```text\nLEFT\n```\n\n```text\nRIGHT\n```\n\n```text\nreg\n```\n\n```text\nin_use\n```\n\n```text\nreg\n```\n\n```text\nassert(r1 != r2)\n```\n\n```text\nemit_load()\n```\n\n```text\nr1 = REG_R0\n```\n\n```text\nr2 = REG_R0\n```\n\n```text\ngen_code_for(ast_node) {\n  if (ast_node->left_first) {\n    gen_code_for(ast_node->left_operand)\n    gen_code_for(ast_node->right_operand)\n  } else {\n    gen_code_for(ast_node->right_operand)\n    gen_code_for(ast_node->left_operand)\n    swap_stack_top_2()  // get stack top 2 elements in correct order\n  }\n  emit_code_for(ast_node)\n}\n```\n\n```text\nemit_code_for(ast_node) {\n  switch (ast_node->kind) {\n    case DIV:  // An operation that needs specific registers\n      pin_register(EAX) // Might generate some code to make EAX available\n      pin_register(EDX) // Might generate some code to make EDX available\n      emit_instruction(XOR, EDX, EDX) // clear EDX\n      emit_instruction(MOV, EAX, stack(1)) // lhs to EAX\n      emit_instruction(DIV, stack(0)) // divide by rhs operand\n      pop(2) // remove 2 elements and free their PLACES\n      free_place(EDX) // EDX not needed any more.\n      mark_busy(EAX)  // EAX now only busy, not pinned.\n      push(EAX) // Push result on operand stack\n      break;\n    case ADD: // An operation that needs no specific register.\n      PLACE result = emit_instruction(ADD, stack(1), stack(0))\n      pop(2)\n      push(result)\n      break;\n    ... and so on\n  }\n}\n```\n\n```text\nemit_instruction(op, lhs, [optional] rhs) {\n  switch (op) {\n    case DIV:\n      assert(RAX->state == PINNED && RDX->state == PINNED)\n      print_instruction(DIV, lhs)\n      return RAX;\n    case ADD:\n      if (lhs->kind == REGISTER) {\n        print_instruction(ADD, lhs, rhs)\n        return lhs\n      }\n      if (rhs->kind == REGISTER) {\n        print_instruction(ADD, rhs, lhs)\n        return rhs\n      }\n      // Both operands are MEMORY\n      R = allocate_register // Get a register; might emit some code.\n      print_instruction(MOV, R, lhs)\n      print_instruction(ADD, R, rhs) \n      return R\n      ... and so on ...\n```\n\n```text\nmov esi, [ebp + x]\nmov edi, 1\nadd esi, edi\nmov [ebp + x], esi\n```\n\n```text\nmov esi, [ebp + x]\nadd esi, 1\nmov [ebp + x], esi\n```\n\n```text\nadd [ebp + x], 1\n```\n\n```text\ninc [bp + x]\n```\n\n```text\nallocate_register\n```\n\n```text\npin_register(R)\n```\n\n```text\nstack(n)\n```\n\n```text\nn\n```\n\n```text\nx = x + 1\n```\n\n```text\nx\n```\n\n```text\nPLACE *target\n```\n\n```text\ntarget\n```\n\n```text\ntarget\n```\n\n```text\nx = x + 1\n```\n\n```text\nASSIGNMENT\n```\n\n```text\nemit_code_for\n```\n\n```text\ntarget\n```\n\n```text\nx\n```\n\n```text\nx + 1\n```\n\n```text\nx\n```\n\n```text\nemit_code_for\n```\n\n```text\nADD\n```\n\n```text\nemit_code_for\n```\n\n```text\nx\n```\n\n```text\n1\n```\n\n```text\nADD\n```\n\n```text\nadd L, C\n```\n\n========================================\n\nComments:\n- First of all, thank you ver much! first I accepted your answer because it's what I'm looking for and bounty is ending. When I back home I will read it carefully and give you my feedback and my questions.\n- I'm really sorry, I removed my last two comments. I didn't see you had already answered. I removed because I had solved this using the follwoing approach: a new tmember in enum Kind called 'node' and then modified print_instruction() to deal with Place of this kind. So, stack(n) might return a register,memory (value to put on stack) and a node. Place of Kind.node means it's either a constant number or a identifier(not implemented yet). A new member in the struct Place has beed added and hold if kind = node, the index of this node in the node stack table.\n- Valeus gets pushed on snode tack table by emit_code_for() if type is either number of identifier. Something like this:`case Type_number: int i = push_node(node); Place p = new Place(); p.kind = Kind.node; p.nodeoffset = i; push(p);` break;`\n- @TheMask I guess this can work. But it's likely to lead to tricky code. A memory location is an l-value. A literal is an r-value. This difference means that they're really two distinctly different kinds of PLACEs. Using a single kind of PLACE for both (what you are calling node) isn't a very clean design, even though it may work okay. You can actually use the same kind of PLACE that you use for temporaries in memory to also hold user variables: Kind.MEMORY. A stack frame offset covers both cases. So you only need one additional, the literal constant.\n- It's really a bad design. I'm facing a problem now: I can't determine the last register used if it's of node. I haven't handled Kind.memory yet. But if one identifier(variable) is seen, print_instruction() see it's of node type and then call print_node() which see a Type_id node and then print its name enclosed [ and ]. As I've said, I haven't handled identifiers yet and then nor stack. First I want to make a version with constants working and then implemente variables. Should I handle differently? my emit_instruction() doesn't differ much from the one you described.\n- Actually, I have two stacks for the nodes and places, respectively. I made a change in PLACE to it hold a new kind, the node. This hold the node's offset in the stack of nodes. Aren't really mixed into a single stack. stack(N) does manage PLACE only. But I feel it isn't what you said to I do... but I don't know how...\n- This is my last question: I don't handle nonbinary operators in gen_code_for() and call it recusively if there's non-null left and right sides but emit_code_for() is called anyway. Is this right?\n- Update: I solved problem to determine last register used. Actually, I change a bit the designer and I don't need this anymore. But I made assumption now I want to know if it's Always true(Although it might be obvious for you). I'm getting moves like MOV EAX, EAX and MOV EBX, EBX with div operation and I want to make sure the follwing code is a real fix for this and willn't break anything on the code generation. pastebin.com/4S4tuE2u\n- Well, sure, it's useless to emit a move from any place to itself, so you should just skip. The `print_instruction` routine in my code can take care of that. Your if statements can do that as well.\n- I thought actually it's a was bug\n- Could I give you more 50? :)\n- In your code, you do have two stacks: one of registers and another to places, right?\n- No. A place can be a register, a memory location (which is either a temporary allocated by the compiler to hold a sub-expression value or a user-defined variable or parameter), or a literal value. When the emitter looks at the top two stack positions (and the target operand if you're using that), it must be able to handle all the possible pairwise combinations - up to 3 (or 6 with the target) for each op. There is only one stack. If you were making an interpreter, the stack would hold the values themselves. In the code generator, it contains a description of where the value has been stored.\n- (I'm sorry for my bad english; not my native language speaker)\n- Shouldn't a pinned register be pushed on opstack too?\n- Not automatically. For a 32 bit DIV, you pin eax and edx in order to ensure they're available as the result target, but only push eax after code is emitted because that's where the answer is. Pinning is just a form of allocation where we force the particular register we want.\n- Check out the assembly output: pastebin.com/dEwwEaw5 and how I'm dealing with it: pastebin.com/Xn4Pg7PG I put comments to try make clean as possible what I'm trying. Thanks for your attention.\n- @TheMask Looks like it's coming along pretty well. Congratulations.\n- In the of the code generation I should end up with zero values on stack, right?\n- The generator invariant is that places of sub-expression values are stored are on the stack. Depending on the language you're generating code for, this means zero are left at the end if all values have been assigned in a statement and the statement has a `void` type. In a language like C where statements can have values, you'll generally end with one place on the stack after processing the last one (because its value is available but has not been used), which you can pop manually before terminating.\n- Thanks. I understood why I've Always one value left on the end of code generation. I didn't still make it working 100%. When I fix one error I make another. It's really hard. :\\ Check out it this if you can help me pastebin.com/fn9pfvwx\n- I feel I'm requesting you too much. But very hardly I will find some other place/someone to get help like this.\n- Something I'm missing on this explanation: when/how did the values get on the operando stack? say, I have tree from expression `2+x` and deal with the operator `+` on `emit_code_for()` but where did I deal with the left and right value of that operator? i.e, where/in which function did `2` and `x` pushed on the operando stack so that I get them by `stack(0)` and `stack(1)` on `emit_code_for()` function? I know you have answered it more than one year ago but I hope you're still around and can answer. Thanks\n- The case for AST node(2) pushes a PLACE of type immediate. For x it pushes a PLACE that's a reference to the location where x is stored (frame pointer offset or absolute address if x is static). For +, emit_code always deals with the top two stack positions. Here it sees that it must add an immediate and a stack reference and chooses the best instruction(s) for this purpose. For x86, it would load the value of x into a register in one instruction and then add the immediate value to the register. It would pop the args from the stack and push a PLACE for the register. @Jack\n- Thanks for your explanation. I added a type member to place struct to I know if it's a immediate value/memory address/register. All the immediate values gets pushed on operand stack before the operators are proccessed.","metadata":{"transformedAt":"2026-08-18T18:32:17.803Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":48,"totalLines":421,"estimatedTokens":3689}}500{"id":"stack-22595922","source":"stackoverflow","questionId":22595922,"title":"Is it possible to optimize a compiled binary?","tags":["optimization","assembly","compiler-construction"],"text":"Title: Is it possible to optimize a compiled binary?\nTags: optimization, assembly, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThis is more of a curiosity I suppose, but I was wondering whether it is possible to apply compiler optimizations post-compilation. Are most optimization techniques highly-dependent on the IR, or can assembly be translated back and forth fairly easily?\n\n========================================\n\nTop Answer:\nThere's been some recent research interest in this space. Alex Aiken's STOKE project is doing exactly this with some pretty impressive results. In one example, their optimizer found a function that is twice as fast as `gcc -O3` for the Montgomery Multiplication step in OpenSSL's RSA library. It applies these optimizations to already-compiled ELF binaries.\n\nHere is a link to the paper.\n\n========================================\n\nCode:\n```text\ngcc -O3\n```\n\n========================================\n\nComments:\n- Binary-to-binary compiles that I kinow of have been created by DEC for the alpha NT \"port\" and by transmeta. Both more than ten years ago.\n- What do you call the IR ?\n- Post-optimization could be a good opportunity for a company willing to specialize in language-independent optimizers. I suspect anyway that retrieving program semantics from assembly code is more difficult than from source code, and this is why you don't find such products on the market.\n- The link 2 is broke :(\n- ^ It's not broken, it's just behind a paywall. I get it fine at my university.","metadata":{"transformedAt":"2026-08-18T18:32:17.803Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":29,"estimatedTokens":381}}501{"id":"stack-6311512","source":"stackoverflow","questionId":6311512,"title":"Creating unique labels in Haskell","tags":["haskell","code-generation","compiler-construction"],"text":"Title: Creating unique labels in Haskell\nTags: haskell, code-generation, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm writing a compiler for a simple imperative language in Haskell, outputting Java bytecode. I've gotten to the point where I'm emitting an abstract representation of bytecodes. \n\nWhile writing code for compiling if-statements I ran in to some trouble. To implement if-statements I need labels to jump to. Therefore I need to generate a name for that label, and that name needs to be unique. \n\nMy first thought was to thread some state through `compileStatement`, i.e \n\n```\ncompileStatement :: Statement -> UniqueIDState -> [AbstractInstruction]\n```\n\nOf course, `compilerStatement` is recursive, so using this method would require me to pass the state of the unique ID generator back upp from the recursive calls:\n\n```\ncompileStatement :: Statement -> UniqueIDState -> (UniqueIdState, [AbstractInstruction])\n```\n\nThis seems a bit clumsy, especially if I realize I need to carry around more state in the future; is there a more elegant way?\n\n========================================\n\nTop Answer:\n*I suppose it is tempting, if the only tool you have is a hammer, to treat everything as if it were a nail.*\n\nAbraham Maslow.\n\nHow about something different - a unique-supply that **isn't** a member of the `Monad` class. As it happens, you were almost there with your original type signature:\n\n```\ncompileStatement :: Statement -> UniqueIDState -> [AbstractInstruction]\n```\n\nIf the *only* requirement is that each label is unique - no need to count how many were used, providing the same identifiers given the same circumstances, etc - there's a less-invasive technique you can use.\n\nIn IO-free spillable splittable supplies, Luke Palmer shows how value supplies can be *encapsulated*:\n\n```\nrunSupply :: (forall a . Eq a => Supply a -> b) -> b\n```\n\nThis avoids having the monadic `IO` type taint large parts of the programs which use them: *nice!* But that isn't the only problem - depending on how they're defined, the onus is on you to use such supplies correctly. For example, assuming:\n\n```\ndata Statement =\n ... | If Statement Statement Statement | ...\n```\n\nthen if:\n\n```\ncompileStatement (If c t e) s =\n case split s of\n s1 : s2 : s3 : _ -> buildCondJump (compileStatement c s1)\n (compileStatement t s2)\n (compileStatement e s3)\n```\n\nis mistakenly changed to:\n\n```\ncompileStatement (If c t e) s =\n case split s of\n s1 : s2 : s3 : _ -> buildCondJump (compileStatement c s)\n (compileStatement t s)\n (compileStatement e s)\n```\n\nnot only are `UniqueSupply` and `Unique` values being erroneously reused, there's the potential for a space leak if any of the recursive calls to `compileStatement` uses the supply intensively.\n\nUnlike Clean, Haskell has no standard way of marking types as *monousal*. That leaves checks at runtime as the only option: definitely a job for an abstract data type!\n\nHere's a thought - if that ADT was also spittable splittable, we could be able to use it to define an alternate value-supply type. All going well, the values of this new type would then have both properties: slippable splittable and monousal.\n\nLooking at `Data.Supply` reveals the use of a binary-tree type - the module and definitions seem to be based on the following example from the functional pearl [On generating unique names], written by Lennart Augustsson, Mikael Rittri and Dan Synek - from page 4 of 7:\n\n```\nmodule HideGensym(\n Name, NameSupply, initialNameSupply, getNameDeplete, splitNameSupply)\n where\n\n gensym :: a -> Int -- implemented in assembler\n\n data Name = MkName Int deriving (Eq)\n data NameSupply = MkNameSupply Name NameSupply NameSupply\n\n initialNameSupply = gen ()\n where gen x = MkNameSupply (MkName (gensym x)) (gen x) (gen x)\n getNameDeplete (MkNameSupply n s1 _) = (n, s1)\n splitNameSupply (MkNameSupply _ s1 s2) = (s1, s2)\n```\n\n...`gensym`: we'll leave that one for now. Let's look at how we can insert the new ADT into `NameSupply`...after we attend to a more mundane matter: - two identical calls to `gen`- `MkNameSupply ... (gen x) (gen x)` - to an optimising Haskell implementation, they're the same value:\n\n```\n-- same function, same argument, same result: what's the matter? \n initialNameSupply = gen ()\n where gen x = let s = gen x in\n MkNameSupply (MkName (gensym x)) s s\n```\n\nThen again, maybe we can solve both problems at once:\n\n```\ninitialNameSupply = runUO gen\n where gen u = let !(u1, u2) = splitUO2 u in\n MkNameSupply (MkName (gensym ())) (gen u1) (gen u2)\n```\n\nwhere `UO` will be our new use-once split-ready abstract-data type:\n\n```\nmodule UO(\n UO, initUO, splitUO, splitUO2, ...\n ) where\n\n data UO s ...\n runUO :: (forall s . UO s -> a) -> a\n splitUO :: UO s -> [UO s]\n splitUO2 :: UO s -> (UO s, UO s) \n ⋮\n```\n\n...which can also be encapsulated.\n\n(Surely there must be a better word than *spiltable* *splittable* in the English language...)\n\nNow for the `gensym` problem - let's start with this cautionary remark about that `HideGensym` module, also on page 4 of 7:\n\nThe `gensym` [thing] must be coded in assembler, and possibly also the `gen` function.\n\n...otherwise that single call to `gensym` might be lifted all the way out: remember `gen ()`?\n\n```\n{- WRONG! -}\n initialNameSupply = runUO gen\n where gen u = let !(u1, u2) = splitUO2 u in\n MkNameSupply (MkName x) (gen u1) (gen u2)\n x = gensym ()\n```\n\nSince `gensym` (supposedly!) accepts any type of input:\n\n```\ngensym :: a -> Int -- implemented in assembler\n```\n\nthis shouldn't break anything:\n\n```\ninitialNameSupply = runUO gen\n where gen u = let !(u1:u2:u3:_) = splitUO u in\n MkNameSupply (MkName (gensym u1)) (gen u2) (gen u3)\n```\n\nAs a bonus, we can make a slightly-more generic version of `initialNameSupply`:\n\n```\ninitialNameSupply = initialSupply gensym\n\n initialSupply :: (UO s -> Int) -> NameSupply\n initialSupply g = runUO gen\n where gen u = let !(u1:u2:u3:_) = splitUO u in\n MkNameSupply (MkName (g u1)) (gen u2) (gen u3)\n```\n\n(...alright, so `gensym` is still there - at least now it's isolated.)\n\nBy now you've probably noticed the other example module `OneTimeSupplies`, with its own cautionary remark:\n\nIt is referentially transparent only if each supply is used at most once.\n\nIn addition, back on page 3 of 7:\n\nIf a compile-time analysis of a program can guarantee that every name supply is used at most once, either to do `getNameDeplete` or `splitNameSupply`, the tree becomes unnecessary [...]\n\nSince we're relying on `UO` to provide the same guarantee, can we also go tree-free in our implementation and save some work?\n\nTo do that, `supplyValue` and the `split`s will need an upgrade:\n\nthe simplest option for `supplyValue` is to provide it with the generator (`g` in `initialSupply`).\n\n```\ndata NameSupply = forall s . Supply (UO s -> Int) ...\n\n supplyValue :: NameSupply -> Name \n supplyValue (Supply g ...) = MkName (g ...)\n```\n\nas for the `split`s, they require an `UO` value so they can obtain the new `UO` values needed by the new supplies:\n\n```\ndata NameSupply = forall s . Supply (UO s) ...\n\n split :: NameSupply -> [NameSupply]\n split (Supply u ...) = [ Supply v ... | v (NameSupply, NameSupply)\n split2 (Supply u ...) = let !(u1, u2) = splitUO2 u in\n (Supply u1 ..., Supply u2 ...)\n```\n\nThat clearly suggests:\n\n```\ndata NameSupply = forall s . Supply (UO s -> Int) (UO s)\n\n supplyValue (Supply g u) = MkName (g u)\n\n split (Supply g u) = [ Supply g v | v But does it also work for `initialNameSupply`?\n\n```\ninitialNameSupply = initialSupply gensym\n\n initialSupply :: (UO s -> Int) -> NameSupply\n initialSupply = runUO . Supply\n```\n\nIt gets better:\n\n```\ntype NameSupply = Supply Name\n data Name = MkName Int deriving (Eq)\n initialNameSupply = initialSupply (MkName . gensym)\n\n -- NameSupply --\n -- ================ --\n -- Supply --\n\n data Supply a = forall s . Supply (UO s -> a) (UO s) \n\n instance Functor Supply where\n fmap f (Supply g u) = Supply (f . g) u\n \n supplyValue :: Supply a -> a\n supplyValue (Supply g u) = g u\n\n split :: Supply a -> [Supply a]\n split (Supply g u) = [ Supply g v | v (Supply a, Supply a)\n split2 (Supply g u) = let !(u1, u2) = splitUO2 u in\n (Supply g u1, Supply g u2)\n\n initialSupply :: (UO s -> a) -> NameSupply\n initialSupply = runUO . Supply\n```\n\nThis is *very* promising, **if** `UO` and associates can be defined as intended...\n\nIf you've read the post by Luke Palmer, you already know that he uses an `ugly` `unsafe` entity to define `runSupply`. Well, right now (2022 Jan) `runST` is defined in similar fashion:\n\n```\nrunST :: (forall s. ST s a) -> a\n runST (ST st_rep) = case runRW# st_rep of (# _, a #) -> a\n```\n\nwhere:\n\n```\nnewtype ST s a = ST (STRep s a)\n type STRep s a = State# s -> (# State# s, a #\n runRW# :: STRep RealWorld a -> (# State# RealWorld, a #)\n```\n\nCan `UO` be defined without resorting to such measures? That's probably worthy of a separate answer - for now, we'll just tolerate the ugliness:\n\n```\n{-# LANGUAGE BangPatterns, RankNTypes, UnboxedTuples, MagicHash #-}\n module UO(\n UO, runUO, splitUO, splitUO2,\n useUO, asUO,\n ) where\n\n import Prelude (String, Eq(..))\n import Prelude ((.), ($), (++), error, all)\n import Data.Char (isSpace)\n import GHC.Base (State#, MutVar#)\n import GHC.Base (runRW#, newMutVar#, noDuplicate#)\n import GHC.Exts (atomicModifyMutVar#)\n import GHC.ST (ST(..), STRep)\n\n data UO s = UO (UO# s)\n\n runUO :: (forall s . UO s -> a) -> a\n runUO g = let (# _, r #) = runRW# (useUO# (g . UO)) in r\n\n splitUO :: UO s -> [UO s]\n splitUO u = let !(u1, u2) = splitUO2 u in u1 : splitUO u\n\n splitUO2 :: UO s -> (UO s, UO s)\n splitUO2 (UO h) = let (# h1, h2 #) = splitUO2# h in (UO h1, UO h2)\n\n useUO :: (UO s -> a) -> ST s a\n useUO g = ST (\\s -> useUO# (g . UO) s)\n\n asUO :: Eq a => String -> ST s a -> UO s -> a\n asUO name (ST act) (UO h)\n = asUO# name act h\n\n -- local definitions --\n type UO# s = String -> State# s\n\n splitUO2# :: UO# s -> (# UO# s, UO# s #)\n splitUO2# h = let !s = h \"splitUO2\"\n (# s', h1 #) = dispense# s\n (# _, h2 #) = dispense# s'\n in (# h1, h2 #)\n\n useUO# :: (UO# s -> a) -> STRep s a\n useUO# g s = let (# s', h #) = dispense# s\n !r = g h\n in (# s', r #)\n\n dispense# :: STRep s (UO# s)\n dispense# s = let (# s', r #) = newMutVar# () s\n in (# s', expire# s' r #)\n\n expire# :: State# s -> MutVar# s () -> String -> State# s\n expire# s r name = let (# s', () #) = atomicModifyMutVar# r use s\n in s'\n where\n use x = (error nowUsed, x)\n nowUsed = name' ++ \": already expired\"\n name' = if all isSpace name then \"(unknown)\"\n else name\n\n asUO# :: Eq a => String -> STRep s a -> UO# s -> a\n asUO# name act h = let (# _, t #) = act (noDuplicate# (h name)) in t\n```\n\nIt's a little more complicated than strictly necessary (e.g. rudimentary reuse-error reporting) but in exchange for that, `UO`-based definitions can now manipulate local state...\n\nThere's one other definition in `Data.Supply` to implement:\n\n```\nnewSupply :: a -> (a -> a) -> IO (Supply a)\n newSupply start next = gen =as it would end the need for `gensym`. It is vaguely similar to `initialSupply` - can that be made more apparent?\n\n`gen` in the original `initialNameSupply` doesn't have a reference parameter `r`:\n\n```\nnewSupply start next = do r \nthe value-action `unsafeInterleaveIO (atomicModifyIORef r upd)` performs the role of `gensym` in the original `initialNameSupply`:\n\n```\nnewSupply start next = do r \n`gen` in the original `initialNameSupply` had no need of `do`-notation:\n\n```\nnewSupply start next = do r \ndoes `genval` have to be in that `let`-binding?\n\n```\nnewSupply start next = do r \n`upd` is only used in `genval`:\n\n```\nnewSupply start next = do r \ncan some content in `genval` be moved to a separate definition?\n\n```\nnewSupply start next = do r (a -> a) -> IO a\n nextValue r next = let upd a = let b = next a in seq b (b, a)\n in atomicModifyIORef r upd\n```\n\nNow that it more clearly resembles the original `initialNameSupply`, re-implementing `newSupply` using our new `Supply` type is relatively simple - first, a change of monadic type:\n\n```\nnewSupply start next = do r (a -> a) -> ST s a\n nextValue r next = let upd a = let b = next a in seq b (b, a)\n in atomicModifyST r upd\n```\n\nNo other changes are needed for `nextValue`. As for `newSupply`:\n\n```\nnewSupply :: Eq a => a -> (a -> a) -> ST s (Supply a)\n newSupply start next = do r which can then be used to define our version of `runSupply`:\n\n```\nrunSupply :: (forall a . Eq a => Supply a -> b) -> b\n runSupply f = f (runST (newSupply (0 :: Int) succ))\n```\n\nCan we now, finally, expel `gensym` from the `NameSupply` type?\n\n```\ninitialNameSupply :: NameSupply\n initialNameSupply = fmap MkName (initialSupply 0 succ)\n\n initialSupply :: Eq a => a -> (a -> a) -> Supply a\n initialSupply start next = runST (newSupply start next)\n```\n\n**Yes.**\n\nHere are all the pertinent definitions, arranged into modules:\n\n`ExpelGensym`, the replacement for `HideGensym` on page 4 of 7:\n\n```\n{-# LANGUAGE BangPatterns #-}\n module ExpelGensym(\n Name, NameSupply, initialNameSupply, getNameDeplete, splitNameSupply\n ) where\n import Control.Monad (liftM)\n import Control.Monad.ST (runST)\n import Supply (Supply, newSupply, supplyValue, split2)\n\n data Name = MkName Int deriving (Eq)\n type NameSupply = Supply Name\n\n initialNameSupply :: Supply Name\n initialNameSupply = fmap MkName (initialSupply 0 succ)\n\n getNameDeplete :: NameSupply -> (Name, NameSupply) \n getNameDeplete s = let !(s1, s2) = split2 s in (supplyValue s1, s2)\n\n splitNameSupply :: NameSupply -> (NameSupply, NameSupply)\n splitNameSupply = split2\n\n -- local definitions --\n initialSupply :: Eq a => a -> (a -> a) -> Supply a\n initialSupply start next = runST (newSupply start next)\n```\n\n`Supply`, our miniature implementation of `Data.Supply`:\n\n```\n{-# LANGUAGE BangPatterns, ExistentialQuantification, RankNTypes #-}\n module Supply(\n Supply, newSupply, runSupply, supplyValue, split, split2\n ) where\n import Control.Monad.ST\n import Data.STRef\n import UO\n\n data Supply a = forall s . Supply (UO s -> a) (UO s) \n\n instance Functor Supply where\n fmap f (Supply g u) = Supply (f . g) u\n\n newSupply :: Eq a => a -> (a -> a) -> ST s (Supply a)\n newSupply start next = do r Supply a -> b) -> b\n runSupply f = f (runST (newSupply (0 :: Int) succ))\n\n supplyValue :: Supply a -> a\n supplyValue (Supply g u) = g u\n\n split :: Supply a -> [Supply a]\n split (Supply g u) = [ Supply g v | v (Supply a, Supply a)\n split2 (Supply g u) = let !(u1, u2) = splitUO2 u in\n (Supply g u1, Supply g u2)\n\n -- local definitions --\n nextValue :: STRef s a -> (a -> a) -> ST s a\n nextValue r next = let upd a = let b = next a in seq b (b, a)\n in atomicModifySTRef r upd\n\n {-\n -- if your Haskell installation doesn't define it --\n atomicModifySTRef :: STRef s a -> (a -> (a, b)) -> ST s b\n atomicModifySTRef r f = do x \n`UO`, that use-once split-ready abstract-data type:\n\n```\n{-# LANGUAGE BangPatterns, RankNTypes, UnboxedTuples, MagicHash #-}\n module UO(\n UO, runUO, splitUO, splitUO2,\n useUO, asUO,\n ) where\n\n import Prelude (String, Eq(..))\n import Prelude ((.), ($), (++), error, all)\n import Data.Char (isSpace)\n import GHC.Base (State#, MutVar#)\n import GHC.Base (runRW#, newMutVar#, noDuplicate#)\n import GHC.Exts (atomicModifyMutVar#)\n import GHC.ST (ST(..), STRep)\n\n data UO s = UO (UO# s)\n\n runUO :: (forall s . UO s -> a) -> a\n runUO g = let (# _, r #) = runRW# (useUO# (g . UO)) in r\n\n splitUO :: UO s -> [UO s]\n splitUO u = let !(u1, u2) = splitUO2 u in u1 : splitUO u\n\n splitUO2 :: UO s -> (UO s, UO s)\n splitUO2 (UO h) = let (# h1, h2 #) = splitUO2# h in (UO h1, UO h2)\n\n useUO :: (UO s -> a) -> ST s a\n useUO g = ST (\\s -> useUO# (g . UO) s)\n\n asUO :: Eq a => String -> ST s a -> UO s -> a\n asUO name (ST act) (UO h)\n = asUO# name act h\n\n -- local definitions --\n type UO# s = String -> State# s\n\n splitUO2# :: UO# s -> (# UO# s, UO# s #)\n splitUO2# h = let !s = h \"splitUO2\"\n (# s', h1 #) = dispense# s\n (# _, h2 #) = dispense# s'\n in (# h1, h2 #)\n\n useUO# :: (UO# s -> a) -> STRep s a\n useUO# g s = let (# s', h #) = dispense# s\n !r = g h\n in (# s', r #)\n\n dispense# :: STRep s (UO# s)\n dispense# s = let (# s', r #) = newMutVar# () s\n in (# s', expire# s' r #)\n\n expire# :: State# s -> MutVar# s () -> String -> State# s\n expire# s r name = let (# s', () #) = atomicModifyMutVar# r use s\n in s'\n where\n use x = (error nowUsed, x)\n nowUsed = name' ++ \": already expired\"\n name' = if all isSpace name then \"(unknown)\"\n else name\n\n asUO# :: Eq a => String -> STRep s a -> UO# s -> a\n asUO# name act h = let (# _, t #) = act (noDuplicate# (h name)) in t\n```\n\n========================================\n\nCode:\n```text\ncompileStatement :: Statement -> UniqueIDState -> [AbstractInstruction]\n```\n\n```text\ncompileStatement :: Statement -> UniqueIDState -> (UniqueIdState, [AbstractInstruction])\n```\n\n```text\ncompileStatement\n```\n\n```text\ncompilerStatement\n```\n\n```text\ncompileStatement :: Statement -> UniqueIDState -> [AbstractInstruction]\n```\n\n```text\nrunSupply :: (forall a . Eq a => Supply a -> b) -> b\n```\n\n```text\ndata Statement =\n        ... | If Statement Statement Statement | ...\n```\n\n```text\ncompileStatement (If c t e) s =\n        case split s of\n          s1 : s2 : s3 : _ -> buildCondJump (compileStatement c s1)\n                                            (compileStatement t s2)\n                                            (compileStatement e s3)\n```\n\n```text\ncompileStatement (If c t e) s =\n        case split s of\n          s1 : s2 : s3 : _ -> buildCondJump (compileStatement c s)\n                                            (compileStatement t s)\n                                            (compileStatement e s)\n```\n\n```text\nmodule HideGensym(\n        Name, NameSupply, initialNameSupply, getNameDeplete, splitNameSupply)\n    where\n\n        gensym :: a -> Int  -- implemented in assembler\n\n        data Name = MkName Int deriving (Eq)\n        data NameSupply = MkNameSupply Name NameSupply NameSupply\n\n        initialNameSupply = gen ()\n            where gen x = MkNameSupply (MkName (gensym x)) (gen x) (gen x)\n        getNameDeplete (MkNameSupply n s1 _) = (n, s1)\n        splitNameSupply (MkNameSupply _ s1 s2) = (s1, s2)\n```\n\n```text\n-- same function, same argument, same result: what's the matter? \n        initialNameSupply = gen ()\n            where gen x = let s = gen x in\n                          MkNameSupply (MkName (gensym x)) s s\n```\n\n```text\ninitialNameSupply = runUO gen\n            where gen u = let !(u1, u2) = splitUO2 u in\n                          MkNameSupply (MkName (gensym ())) (gen u1) (gen u2)\n```\n\n```text\nmodule UO(\n        UO, initUO, splitUO, splitUO2, ...\n    ) where\n\n    data UO s ...\n    runUO    :: (forall s . UO s -> a) -> a\n    splitUO  :: UO s -> [UO s]\n    splitUO2 :: UO s -> (UO s, UO s)   \n               ⋮\n```\n\n```text\n{- WRONG! -}\n        initialNameSupply = runUO gen\n            where gen u = let !(u1, u2) = splitUO2 u in\n                          MkNameSupply (MkName x) (gen u1) (gen u2)\n        x = gensym ()\n```\n\n```text\ngensym :: a -> Int  -- implemented in assembler\n```\n\n```text\ninitialNameSupply = runUO gen\n            where gen u = let !(u1:u2:u3:_) = splitUO u in\n                          MkNameSupply (MkName (gensym u1)) (gen u2) (gen u3)\n```\n\n```text\ninitialNameSupply = initialSupply gensym\n\n        initialSupply :: (UO s -> Int) -> NameSupply\n        initialSupply g = runUO gen\n            where gen u = let !(u1:u2:u3:_) = splitUO u in\n                          MkNameSupply (MkName (g u1)) (gen u2) (gen u3)\n```\n\n```text\ndata NameSupply = forall s . Supply (UO s -> Int) ...\n\n  supplyValue :: NameSupply -> Name \n  supplyValue (Supply g ...) = MkName (g ...)\n```\n\n```text\ndata NameSupply = forall s . Supply (UO s) ...\n\n  split  :: NameSupply -> [NameSupply]\n  split  (Supply u ...) = [ Supply v ... |  v <- splitUO u ]\n\n  split2 :: NameSupply -> (NameSupply, NameSupply)\n  split2 (Supply u ...) = let !(u1, u2) = splitUO2 u in\n                          (Supply u1 ..., Supply u2 ...)\n```\n\n```text\ndata NameSupply = forall s . Supply (UO s -> Int) (UO s)\n\n    supplyValue (Supply g u) = MkName (g u)\n\n    split  (Supply g u) = [ Supply g v |  v <- splitUO u ]\n\n    split2 (Supply g u) = let !(u1, u2) = splitUO2 u in\n                          (Supply g u1, Supply g u2)\n```\n\n```text\ninitialNameSupply = initialSupply gensym\n\n    initialSupply :: (UO s -> Int) -> NameSupply\n    initialSupply = runUO . Supply\n```\n\n```text\ntype NameSupply = Supply Name\n    data Name = MkName Int deriving (Eq)\n    initialNameSupply = initialSupply (MkName . gensym)\n\n     --    NameSupply    --\n     -- ================ --\n     --      Supply      --\n\n    data Supply a = forall s . Supply (UO s -> a) (UO s)      \n\n    instance Functor Supply where\n        fmap f (Supply g u) = Supply (f . g) u\n    \n    supplyValue :: Supply a -> a\n    supplyValue (Supply g u) = g u\n\n    split  :: Supply a -> [Supply a]\n    split  (Supply g u) = [ Supply g v |  v <- splitUO u ]\n\n    split2 :: Supply a -> (Supply a, Supply a)\n    split2 (Supply g u) = let !(u1, u2) = splitUO2 u in\n                          (Supply g u1, Supply g u2)\n\n    initialSupply :: (UO s -> a) -> NameSupply\n    initialSupply = runUO . Supply\n```\n\n```text\nrunST :: (forall s. ST s a) -> a\n    runST (ST st_rep) = case runRW# st_rep of (# _, a #) -> a\n```\n\n```text\nnewtype ST s a = ST (STRep s a)\n    type STRep s a = State# s -> (# State# s, a #\n    runRW# :: STRep RealWorld a -> (# State# RealWorld, a #)\n```\n\n```text\n{-# LANGUAGE BangPatterns, RankNTypes, UnboxedTuples, MagicHash #-}\n    module UO(\n        UO, runUO, splitUO, splitUO2,\n        useUO, asUO,\n    ) where\n\n    import Prelude   (String, Eq(..))\n    import Prelude   ((.), ($), (++), error, all)\n    import Data.Char (isSpace)\n    import GHC.Base  (State#, MutVar#)\n    import GHC.Base  (runRW#, newMutVar#, noDuplicate#)\n    import GHC.Exts  (atomicModifyMutVar#)\n    import GHC.ST    (ST(..), STRep)\n\n    data UO s               =  UO (UO# s)\n\n    runUO                   :: (forall s . UO s -> a) -> a\n    runUO g                 =  let (# _, r #) = runRW# (useUO# (g . UO)) in r\n\n    splitUO                 :: UO s -> [UO s]\n    splitUO u               =  let !(u1, u2) = splitUO2 u in u1 : splitUO u\n\n    splitUO2                :: UO s -> (UO s, UO s)\n    splitUO2 (UO h)         =  let (# h1, h2 #) = splitUO2# h in (UO h1, UO h2)\n\n    useUO                   :: (UO s -> a) -> ST s a\n    useUO g                 =  ST (\\s -> useUO# (g . UO) s)\n\n    asUO                    :: Eq a => String -> ST s a -> UO s -> a\n    asUO name (ST act) (UO h)\n                            =  asUO# name act h\n\n     -- local definitions --\n    type UO# s              =  String -> State# s\n\n    splitUO2#               :: UO# s -> (# UO# s, UO# s #)\n    splitUO2# h             =  let !s           = h \"splitUO2\"\n                                   (# s', h1 #) = dispense# s\n                                   (# _,  h2 #) = dispense# s'\n                               in  (# h1, h2 #)\n\n    useUO#                  :: (UO# s -> a) -> STRep s a\n    useUO# g s              =  let (# s', h #) = dispense# s\n                                   !r           = g h\n                               in  (# s', r #)\n\n    dispense#               :: STRep s (UO# s)\n    dispense# s             =  let (# s', r #) = newMutVar# () s\n                               in  (# s', expire# s' r #)\n\n    expire#                 :: State# s -> MutVar# s () -> String -> State# s\n    expire# s r name        =  let (# s', () #) = atomicModifyMutVar# r use s\n                               in  s'\n                               where\n                                   use x   =  (error nowUsed, x)\n                                   nowUsed =  name' ++ \": already expired\"\n                                   name'   =  if all isSpace name then \"(unknown)\"\n                                              else name\n\n    asUO#                   :: Eq a => String -> STRep s a -> UO# s -> a\n    asUO# name act h        =  let (# _, t #) = act (noDuplicate# (h name)) in t\n```\n\n```text\nnewSupply :: a -> (a -> a) -> IO (Supply a)\n    newSupply start next = gen =<< newIORef start\n      where gen r = unsafeInterleaveIO\n                  $ do v  <- unsafeInterleaveIO (atomicModifyIORef r upd)\n                       ls <- gen r\n                       rs <- gen r\n                       return (Node v ls rs)\n            upd a = let b = next a in seq b (b, a)\n```\n\n```text\nnewSupply start next = do r <- newIORef start\n                              let gen = unsafeInterleaveIO $\n                                        do v  <- unsafeInterleaveIO (atomicModifyIORef r upd)\n                                           ls <- gen\n                                           rs <- gen\n                                           return (Node v ls rs)\n                              gen\n      where upd a = let b = next a in seq b (b, a)\n```\n\n```text\nnewSupply start next = do r <- newIORef start\n                              let gen    = unsafeInterleaveIO $\n                                           do v  <- genval\n                                              ls <- gen\n                                              rs <- gen\n                                              return (Node v ls rs)\n                                  genval = unsafeInterleaveIO (atomicModifyIORef r upd)\n                              gen\n      where upd a = let b = next a in seq b (b, a)\n```\n\n```text\nnewSupply start next = do r <- newIORef start\n                              let gen    = unsafeInterleaveIO (liftM3 Node genval gen gen)\n                                  genval = unsafeInterleaveIO (atomicModifyIORef r upd)\n                              gen\n      where upd a = let b = next a in seq b (b, a)\n```\n\n```text\nnewSupply start next = do r <- newIORef start\n                              let gen = unsafeInterleaveIO (liftM3 Node (genval r) gen gen)\n                              gen\n      where genval r = unsafeInterleaveIO (atomicModifyIORef r upd)\n            upd a    = let b = next a in seq b (b, a)\n```\n\n```text\nnewSupply start next = do r <- newIORef start\n                              let gen = unsafeInterleaveIO (liftM3 Node (genval r) gen gen)\n                              gen\n      where genval r = let upd a = let b = next a in seq b (b, a)\n                       in unsafeInterleaveIO (atomicModifyIORef r upd)\n```\n\n```text\nnewSupply start next = do r <- newIORef start\n                              let gen = unsafeInterleaveIO (liftM3 Node (genval r) gen gen)\n                              gen\n      where genval r = unsafeInterleaveIO (nextValue r next)\n\n    nextValue :: IORef a -> (a -> a) -> IO a\n    nextValue r next = let upd a = let b = next a in seq b (b, a)\n                       in atomicModifyIORef r upd\n```\n\n```text\nnewSupply start next = do r <- newSTRef start\n                              let gen = unsafeInterleaveST (liftM3 Node (genval r) gen gen)\n                              gen\n      where genval r = unsafeInterleaveST (nextValue r next)\n\n    nextValue :: STRef s a -> (a -> a) -> ST s a\n    nextValue r next = let upd a = let b = next a in seq b (b, a)\n                       in atomicModifyST r upd\n```\n\n```text\nnewSupply :: Eq a => a -> (a -> a) -> ST s (Supply a)\n    newSupply start next = do r <- newSTRef start\n                              let g = asUO \"genval\" (genval r)\n                              useUO (Supply g)\n      where genval r = nextValue r next\n```\n\n```text\nrunSupply :: (forall a . Eq a => Supply a -> b) -> b\n    runSupply f = f (runST (newSupply (0 :: Int) succ))\n```\n\n```text\ninitialNameSupply :: NameSupply\n    initialNameSupply = fmap MkName (initialSupply 0 succ)\n\n    initialSupply :: Eq a => a -> (a -> a) -> Supply a\n    initialSupply start next = runST (newSupply start next)\n```\n\n```text\n{-# LANGUAGE BangPatterns #-}\n  module ExpelGensym(\n      Name, NameSupply, initialNameSupply, getNameDeplete, splitNameSupply\n  ) where\n  import Control.Monad    (liftM)\n  import Control.Monad.ST (runST)\n  import Supply           (Supply, newSupply, supplyValue, split2)\n\n  data Name = MkName Int deriving (Eq)\n  type NameSupply = Supply Name\n\n  initialNameSupply :: Supply Name\n  initialNameSupply = fmap MkName (initialSupply 0 succ)\n\n  getNameDeplete :: NameSupply -> (Name, NameSupply)    \n  getNameDeplete s = let !(s1, s2) = split2 s in (supplyValue s1, s2)\n\n  splitNameSupply :: NameSupply -> (NameSupply, NameSupply)\n  splitNameSupply = split2\n\n   -- local definitions --\n  initialSupply :: Eq a => a -> (a -> a) -> Supply a\n  initialSupply start next = runST (newSupply start next)\n```\n\n```text\n{-# LANGUAGE BangPatterns, ExistentialQuantification, RankNTypes #-}\n  module Supply(\n      Supply, newSupply, runSupply, supplyValue, split, split2\n  ) where\n  import Control.Monad.ST\n  import Data.STRef\n  import UO\n\n  data Supply a = forall s . Supply (UO s -> a) (UO s)      \n\n  instance Functor Supply where\n      fmap f (Supply g u) = Supply (f . g) u\n\n  newSupply :: Eq a => a -> (a -> a) -> ST s (Supply a)\n  newSupply start next = do r <- newSTRef start\n                            let g = asUO \"genval\" (genval r)\n                            useUO (Supply g)\n    where genval r = nextValue r next\n\n  runSupply :: (forall a . Eq a => Supply a -> b) -> b\n  runSupply f = f (runST (newSupply (0 :: Int) succ))\n\n  supplyValue :: Supply a -> a\n  supplyValue (Supply g u) = g u\n\n  split  :: Supply a -> [Supply a]\n  split  (Supply g u) = [ Supply g v |  v <- splitUO u ]\n\n  split2 :: Supply a -> (Supply a, Supply a)\n  split2 (Supply g u) = let !(u1, u2) = splitUO2 u in\n                        (Supply g u1, Supply g u2)\n\n   -- local definitions --\n  nextValue :: STRef s a -> (a -> a) -> ST s a\n  nextValue r next = let upd a = let b = next a in seq b (b, a)\n                     in atomicModifySTRef r upd\n\n  {-\n   -- if your Haskell installation doesn't define it --\n  atomicModifySTRef :: STRef s a -> (a -> (a, b)) -> ST s b\n  atomicModifySTRef r f = do x <- readSTRef r\n                             let !(x', y) = f x\n                             writeSTRef r x'\n                             return y\n  -}\n```\n\n```text\n{-# LANGUAGE BangPatterns, RankNTypes, UnboxedTuples, MagicHash #-}\n  module UO(\n      UO, runUO, splitUO, splitUO2,\n      useUO, asUO,\n  ) where\n\n  import Prelude   (String, Eq(..))\n  import Prelude   ((.), ($), (++), error, all)\n  import Data.Char (isSpace)\n  import GHC.Base  (State#, MutVar#)\n  import GHC.Base  (runRW#, newMutVar#, noDuplicate#)\n  import GHC.Exts  (atomicModifyMutVar#)\n  import GHC.ST    (ST(..), STRep)\n\n  data UO s               =  UO (UO# s)\n\n  runUO                   :: (forall s . UO s -> a) -> a\n  runUO g                 =  let (# _, r #) = runRW# (useUO# (g . UO)) in r\n\n  splitUO                 :: UO s -> [UO s]\n  splitUO u               =  let !(u1, u2) = splitUO2 u in u1 : splitUO u\n\n  splitUO2                :: UO s -> (UO s, UO s)\n  splitUO2 (UO h)         =  let (# h1, h2 #) = splitUO2# h in (UO h1, UO h2)\n\n  useUO                   :: (UO s -> a) -> ST s a\n  useUO g                 =  ST (\\s -> useUO# (g . UO) s)\n\n  asUO                    :: Eq a => String -> ST s a -> UO s -> a\n  asUO name (ST act) (UO h)\n                          =  asUO# name act h\n\n   -- local definitions --\n  type UO# s              =  String -> State# s\n\n  splitUO2#               :: UO# s -> (# UO# s, UO# s #)\n  splitUO2# h             =  let !s           = h \"splitUO2\"\n                                 (# s', h1 #) = dispense# s\n                                 (# _,  h2 #) = dispense# s'\n                             in  (# h1, h2 #)\n\n  useUO#                  :: (UO# s -> a) -> STRep s a\n  useUO# g s              =  let (# s', h #) = dispense# s\n                                 !r           = g h\n                             in  (# s', r #)\n\n  dispense#               :: STRep s (UO# s)\n  dispense# s             =  let (# s', r #) = newMutVar# () s\n                             in  (# s', expire# s' r #)\n\n  expire#                 :: State# s -> MutVar# s () -> String -> State# s\n  expire# s r name        =  let (# s', () #) = atomicModifyMutVar# r use s\n                             in  s'\n                             where\n                                 use x   =  (error nowUsed, x)\n                                 nowUsed =  name' ++ \": already expired\"\n                                 name'   =  if all isSpace name then \"(unknown)\"\n                                            else name\n\n  asUO#                   :: Eq a => String -> STRep s a -> UO# s -> a\n  asUO# name act h        =  let (# _, t #) = act (noDuplicate# (h name)) in t\n```\n\n```text\nMonad\n```\n\n```text\nIO\n```\n\n```text\nUniqueSupply\n```\n\n```text\nUnique\n```\n\n```text\ncompileStatement\n```\n\n```text\nData.Supply\n```\n\n```text\ngensym\n```\n\n```text\nNameSupply\n```\n\n```text\ngen\n```\n\n```text\nMkNameSupply ... (gen x) (gen x)\n```\n\n```text\nUO\n```\n\n```text\ngensym\n```\n\n```text\nHideGensym\n```\n\n```text\ngensym\n```\n\n```text\ngen\n```\n\n```text\ngensym\n```\n\n```text\ngen ()\n```\n\n```text\ngensym\n```\n\n```text\ninitialNameSupply\n```\n\n```text\ngensym\n```\n\n```text\nOneTimeSupplies\n```\n\n```text\ngetNameDeplete\n```\n\n```text\nsplitNameSupply\n```\n\n```text\nUO\n```\n\n```text\nsupplyValue\n```\n\n```text\nsplit\n```\n\n```text\nsupplyValue\n```\n\n```text\ng\n```\n\n```text\ninitialSupply\n```\n\n```text\nsplit\n```\n\n```text\nUO\n```\n\n```text\nUO\n```\n\n```text\ninitialNameSupply\n```\n\n```text\nUO\n```\n\n```text\nugly\n```\n\n```text\nunsafe\n```\n\n```text\nrunSupply\n```\n\n```text\nrunST\n```\n\n```text\nUO\n```\n\n```text\nUO\n```\n\n```text\nData.Supply\n```\n\n```text\ngensym\n```\n\n```text\ninitialSupply\n```\n\n```text\ngen\n```\n\n```text\ninitialNameSupply\n```\n\n```text\nr\n```\n\n```text\nunsafeInterleaveIO (atomicModifyIORef r upd)\n```\n\n```text\ngensym\n```\n\n```text\ninitialNameSupply\n```\n\n```text\ngen\n```\n\n```text\ninitialNameSupply\n```\n\n```text\ndo\n```\n\n```text\ngenval\n```\n\n```text\nlet\n```\n\n```text\nupd\n```\n\n```text\ngenval\n```\n\n```text\ngenval\n```\n\n```text\ninitialNameSupply\n```\n\n```text\nnewSupply\n```\n\n```text\nSupply\n```\n\n```text\nnextValue\n```\n\n```text\nnewSupply\n```\n\n```text\nrunSupply\n```\n\n```text\ngensym\n```\n\n```text\nNameSupply\n```\n\n```text\nExpelGensym\n```\n\n```text\nHideGensym\n```\n\n```text\nSupply\n```\n\n```text\nData.Supply\n```\n\n```text\nUO\n```\n\n========================================\n\nComments:\n- Threading state is the sort of thing the State monad was invented for.\n- You've just invented the State monad :-)\n- Theres an example on the haskell.org wiki.\n- This solved my problem, and the State monad (monads in general, really) seem quite elegant. I used the mtl1 instead of the current mtl2 though.\n- `mtl 2.*` is what is standard now. It has pretty much the same interface.","metadata":{"transformedAt":"2026-08-18T18:32:17.803Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":109,"totalLines":1312,"estimatedTokens":8642}}502{"id":"stack-14089916","source":"stackoverflow","questionId":14089916,"title":"Resolving edge-case Haskell module imports and exports","tags":["haskell","compiler-construction","compilation"],"text":"Title: Resolving edge-case Haskell module imports and exports\nTags: haskell, compiler-construction, compilation\nSource: Stack Overflow\n\nQuestion:\nI'm writing a little Haskell compiler, and I want to implement as much Haskell 2010 as possible. My compiler can parse a module, but completing modules to a program seems to be a non-trivial task. I made up some examples of tricky, but maybe valid, Haskell modules:\n\n```\nmodule F(G.x) where\n import F as G\n x = 2\n```\n\nHere the module `F` exports `G.x`, but `G.x` is the same as `F.x`, so module `F` exports `x` if, and only if, it exports `x`.\n\n```\nmodule A(a) where\n import B(a)\n a = 2\n\nmodule B(a) where\n import A(a)\n```\n\nIn this example, to resolve the exports of module `A` the compiler has to check if `a` imported from `B` is the same as the declared `a = 2`, but `B` exports `a` if, and only if, `A` exports `a`.\n\n```\nmodule A(f) where\n import B(f)\n\nmodule B(f) where\n import A(f)\n```\n\nDuring resolving module `A`, the compiler may've assumed that `f` imported from `B` exists, implying that `A` exports `f`, thus `B` can import `A(f)` and export `f`. The only problem is that there's no `f` defined anywhere :).\n\n```\nmodule A(module X) where\n import A as X\n import B as X\n import C as X\n a = 2\n\nmodule B(module C, C.b) where\n import C\n b = 3\n\nmodule C(module C)\n import B as C\n c = 4\n```\n\nHere, the `module` exports cause that export lists are dependent on each other and on themselves.\n\nAll these examples should be valid Haskell, as defined by the Haskell 2010 spec.\n\nI want to ask if there is any idea how to correctly and completely implement Haskell modules?\n\nAssume that a module contains just (simple) variable bindings, `import`s (possibly with `as` or `qualified`), and exports list of possibly qualified variables and `module ...` abbreviations. The algorithm has to be able to:\n\n- compute finite list of exported variables of each module\n\n- link every exported variable to its binding\n\n- link every (maybe qualified) variable used in every module to its binding\n\n========================================\n\nCode:\n```text\nmodule F(G.x) where\n  import F as G\n  x = 2\n```\n\n```text\nmodule A(a) where\n  import B(a)\n  a = 2\n\nmodule B(a) where\n  import A(a)\n```\n\n```text\nmodule A(f) where\n  import B(f)\n\nmodule B(f) where\n  import A(f)\n```\n\n```text\nmodule A(module X) where\n  import A as X\n  import B as X\n  import C as X\n  a = 2\n\nmodule B(module C, C.b) where\n  import C\n  b = 3\n\nmodule C(module C)\n  import B as C\n  c = 4\n```\n\n```text\nF\n```\n\n```text\nG.x\n```\n\n```text\nG.x\n```\n\n```text\nF.x\n```\n\n```text\nF\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\nA\n```\n\n```text\na\n```\n\n```text\nB\n```\n\n```text\na = 2\n```\n\n```text\nB\n```\n\n```text\na\n```\n\n```text\nA\n```\n\n```text\na\n```\n\n```text\nA\n```\n\n```text\nf\n```\n\n```text\nB\n```\n\n```text\nA\n```\n\n```text\nf\n```\n\n```text\nB\n```\n\n```text\nA(f)\n```\n\n```text\nf\n```\n\n```text\nf\n```\n\n```text\nmodule\n```\n\n```text\nimport\n```\n\n```text\nas\n```\n\n```text\nqualified\n```\n\n```text\nmodule ...\n```\n\n========================================\n\nComments:\n- Wow, thank you, I didn't even hope there are actually papers and libraries targeting this problem :)","metadata":{"transformedAt":"2026-08-18T18:32:17.803Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":33,"totalLines":228,"estimatedTokens":779}}503{"id":"stack-14736194","source":"stackoverflow","questionId":14736194,"title":"LLVM JIT Parser writing with Bison / Antlr / Packrat / Elkhound /","tags":["c++","parsing","compiler-construction","llvm","bison"],"text":"Title: LLVM JIT Parser writing with Bison / Antlr / Packrat / Elkhound /\nTags: c++, parsing, compiler-construction, llvm, bison\nSource: Stack Overflow\n\nQuestion:\nIn the LLVM tutorials there is instruction how to write simple JIT compiler. Unfortunatelly, lexer and parser in this tutorial is written manually. I was thinking, such solution is good for learning purposes but it is not suitable for writing complex, production ready compilers. It seems that GCC and few other \"big compilers\" are hand written though. But I think, that all these parser generators give a big boost when writing own compiler (especially when youre doing it alone, without team of people).\n\nIs it possible to use any existing parser generator like Bison / Antlr / Packrat / Elkhound etc. together with LLVM to create JIT compiler? I want to be able to \"feed\" the parser constantly (not once on the beginning) with expressions and compile them in runtime.\n\nAdditional I've found a lot of questions about \"best, modern\" parser generator (like this one: https://stackoverflow.com/questions/428892/what-parser-generator-do-you-recommend). If it is possible to use these tools to create LLVM JIT compiler, I would be thankful for any additional hints and recomendation, which tool would be best in terms of performance and flexibility in this particular case.\n\n========================================\n\nComments:\n- \"Such solution is good for learning purposes but it is not suitable for writing complex, production ready compilers\" - Hm. I always thought GCC was a complex and production-ready compiler. Whatever...\n- GCC was using bison on the beginning, but you're right - I'm fixing it in my question. But really, I would love to use a generator to simplify this task if it is possible.\n- If anything, I would say rather the opposite is true: yacc, Bison, et al, are suitable for learning purposes and such, but for serious production work, a hand-written parser may be the only way to meet requirements.\n- What this guy said. `^^` - When I wanted to make my first parser, I know little about it, so I learned flex and yacc. After trying to get my parser well, I just decided to write it by hand.\n- @JerryCoffin: What requirements are you talking about? JIT? Is using such parser generator really bad thing while writing custom language compiler? Cannot we get what we really need with these tools? Do they not give us some \"boost\" while writing compilers? (I'm not total noob, I was writing compilers with bison and ply, but never such big I am planning right now)\n- @danilo2: I'm not saying Bison (etc.) can't be used for anything big or complex. I'm saying the progression isn't \"hand written parser if possible, parser-generator if the job is too big or complex\", but the opposite of that -- parser generator if possible, hand-written if that can't meet your requirements.\n- As to what requirements those would be: could be class of grammar, incremental parsing, contextual tokenizing, speed, memory usage, or almost any number of others.\n- @JerryCoffin: Thnak you, so what would you suggest for writing a JIT compiler for language with complexity of for example Python? Should I start with hand written or use a parser generator?\n- I wouldn't suggest writing a JIT for a language as complex as Python. Historically, JITs for Python end up *extremely* restricted in what they support (efficiently or sometimes, at all), immature, unreliable, plain wrong, and/or slow. PyPy is different because they didn't write a JIT, they automatically generate it from an interpreter. Perhaps you're underestimating the complexity of Python. Or the complexity of JIT compilers. Or you're overestimating the complexity of your language.\n- @danilo2: Are you talking about compiling from Python source to byte code, or byte code to machine code, or Python source directly to machine code? Do you just want JIT, or do you want incremental parsing (for a REPL, for example)? Ultimately, I don't think it matters a lot though. Python has fairly simple (LL(1)) syntax, and complex semantics, so if your language is similar, the parser is probably the least of your problems.\n- Ahh I'm sorry for this - I'm not talking about Python, I'm talking about language, whose syntax is simmilar to Python but it is staticaly typed. Maybe better example would be Java - sorry for the confusion.\n- And because of the \"fairly simple\" syntax of my language I wanted to use parser genearator - so In such case - should I stick with hand written parser or maybe is it possible to use generator together with LLVM JIT?\n- It is possible to build really sophisticated parsers with parser generators (we use GLR) with very high productivity and maintainability (including the famously hard-to-parse C++; in our case, full C++11 for ANSI/MS/GCC). I suspect one can produce pretty good error messages with such parser generators by extending them explicitly with error-handling productions (see recent paper by Visser in TOPLAS). What you can't do with a hand-written parsers is to build grammar analysis tools, incremental editors, syntax-directed pattern matchers and transform tools, etc. That seems like a huge loss.\n- Why it is \"not clear\" what is the actual language? PEG is well-defined, even with all the cool hacks that packrat allows to do (high-order parsing and such).\n- @SK-logic: well-defined is not the same as clear. A hand-crafted parser written in C++ is well-defined. A Turing machine is well-defined. Yes, PEG is well-defined. But for all of them, the only way to see if a given string is in the language is to execute the code. (Of those three alternatives, PEG is the least bad, imo. But I still prefer formal context free grammars. However, as I said, other people like PEG, and whatever works for you is cool with me.)\n- From my practical experience, PEGs are the most clear and easy to read grammars. I can translate a language spec straight into a PEG with very little modifications. It is possible to obfuscate it, of course, but I have not seen a really bad grammar yet. Whereas there are many unreadable beyond any hope Yacc grammars.","metadata":{"transformedAt":"2026-08-18T18:32:17.803Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":30,"estimatedTokens":1525}}504{"id":"stack-6226063","source":"stackoverflow","questionId":6226063,"title":"Run a simple text file as Java","tags":["java","text","compiler-construction"],"text":"Title: Run a simple text file as Java\nTags: java, text, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have a simple .txt file which has pure Java code inside it like\n\n```\npublic class C {\n public static void main(String[] args ) {\n System.out.println(\"This is executed\");\n }\n }\n```\n\nThe file is named C.txt. Now I want to write Java code that will read the code in C.txt and will compile and run the read code as a pure Java file. Note, I can easily rename C.txt to C.java and compile and run the code manually. However, this is not my intention. I want to read the .txt file as is and execute the code directly. Is this possible somehow?\n\n========================================\n\nTop Answer:\nYou can use the javax.tools api form Java 6 to compile the code on the fly. However since your extension is illegal it will complain with a **error: C.txt Class names are only accepted if annotation processing is explicitly requested**. \n\nTo get around this (as mentioned in the comments) you must first load the code into a String and then execute it:\n\n```\nimport javax.tools.JavaCompiler;\nimport javax.tools.JavaFileObject;\nimport javax.tools.SimpleJavaFileObject;\nimport javax.tools.ToolProvider;\nimport java.io.BufferedReader;\nimport java.io.FileReader;\nimport java.io.IOException;\nimport java.lang.reflect.Method;\nimport java.net.URI;\nimport java.util.Iterator;\nimport java.util.NoSuchElementException;\n\npublic class MyCompiler2 {\n public static void main(String[] args) throws Exception {\n String program = \"\";\n try {\n BufferedReader in = new BufferedReader(new FileReader(\"C.txt\"));\n String str;\n while ((str = in.readLine()) != null) {\n program += str;\n }\n in.close();\n } catch (IOException e) {\n }\n\n JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();\n Iterable fileObjects;\n fileObjects = getJavaSourceFromString(program);\n\n compiler.getTask(null, null, null, null, null, fileObjects).call();\n\n Class clazz = Class.forName(\"C\");\n Method m = clazz.getMethod(\"main\", new Class[]{String[].class});\n Object[] _args = new Object[]{new String[0]};\n m.invoke(null, _args);\n }\n\n static Iterable getJavaSourceFromString(String code) {\n final JavaSourceFromString jsfs;\n jsfs = new JavaSourceFromString(\"code\", code);\n return new Iterable() {\n public Iterator iterator() {\n return new Iterator() {\n boolean isNext = true;\n\n public boolean hasNext() {\n return isNext;\n }\n\n public JavaSourceFromString next() {\n if (!isNext)\n throw new NoSuchElementException();\n isNext = false;\n return jsfs;\n }\n\n public void remove() {\n throw new UnsupportedOperationException();\n }\n };\n }\n };\n }\n}\n\nclass JavaSourceFromString extends SimpleJavaFileObject {\n final String code;\n\n JavaSourceFromString(String name, String code) {\n super(URI.create(\"string:///\" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);\n this.code = code;\n }\n\n public CharSequence getCharContent(boolean ignoreEncodingErrors) {\n return code;\n }\n}\n```\n\nNotice how you need to explicitly provide the **method** and **class** name in order for reflection to execute your code.\n\n========================================\n\nCode:\n```text\npublic class C {\n      public static void main(String[] args ) {\n        System.out.println(\"This is executed\");\n        }\n    }\n```\n\n```text\nimport javax.tools.JavaCompiler;\nimport javax.tools.JavaFileObject;\nimport javax.tools.SimpleJavaFileObject;\nimport javax.tools.ToolProvider;\nimport java.io.BufferedReader;\nimport java.io.FileReader;\nimport java.io.IOException;\nimport java.lang.reflect.Method;\nimport java.net.URI;\nimport java.util.Iterator;\nimport java.util.NoSuchElementException;\n\npublic class MyCompiler2 {\n    public static void main(String[] args) throws Exception {\n        String program = \"\";\n        try {\n            BufferedReader in = new BufferedReader(new FileReader(\"C.txt\"));\n            String str;\n            while ((str = in.readLine()) != null) {\n                program += str;\n            }\n            in.close();\n        } catch (IOException e) {\n        }\n\n        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();\n        Iterable<? extends JavaFileObject> fileObjects;\n        fileObjects = getJavaSourceFromString(program);\n\n        compiler.getTask(null, null, null, null, null, fileObjects).call();\n\n        Class<?> clazz = Class.forName(\"C\");\n        Method m = clazz.getMethod(\"main\", new Class[]{String[].class});\n        Object[] _args = new Object[]{new String[0]};\n        m.invoke(null, _args);\n    }\n\n    static Iterable<JavaSourceFromString> getJavaSourceFromString(String code) {\n        final JavaSourceFromString jsfs;\n        jsfs = new JavaSourceFromString(\"code\", code);\n        return new Iterable<JavaSourceFromString>() {\n            public Iterator<JavaSourceFromString> iterator() {\n                return new Iterator<JavaSourceFromString>() {\n                    boolean isNext = true;\n\n                    public boolean hasNext() {\n                        return isNext;\n                    }\n\n                    public JavaSourceFromString next() {\n                        if (!isNext)\n                            throw new NoSuchElementException();\n                        isNext = false;\n                        return jsfs;\n                    }\n\n                    public void remove() {\n                        throw new UnsupportedOperationException();\n                    }\n                };\n            }\n        };\n    }\n}\n\nclass JavaSourceFromString extends SimpleJavaFileObject {\n    final String code;\n\n    JavaSourceFromString(String name, String code) {\n        super(URI.create(\"string:///\" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);\n        this.code = code;\n    }\n\n    public CharSequence getCharContent(boolean ignoreEncodingErrors) {\n        return code;\n    }\n}\n```\n\n========================================\n\nComments:\n- does writing a shell script work for you? it'd be a good bit easier than trying to implement this in java.\n- Thanks for your answers. I understood that we can compile and files. But I would also like to execute the compiled .class files . How to do it ?\n- The `JavaCompiler` *can* compile source in a `String`. There is an example in the JavaDocs using `JavaSourceFromString`. I use it in STBC.","metadata":{"transformedAt":"2026-08-18T18:32:17.804Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":203,"estimatedTokens":1565}}505{"id":"stack-1291153","source":"stackoverflow","questionId":1291153,"title":"Building own C# compiler using ANTLR: Compilation Unit","tags":["c#","compiler-construction","antlr","abstract-syntax-tree","visitor-pattern"],"text":"Title: Building own C# compiler using ANTLR: Compilation Unit\nTags: c#, compiler-construction, antlr, abstract-syntax-tree, visitor-pattern\nSource: Stack Overflow\n\nQuestion:\n```\n// Create a scanner that reads from the input stream passed to us\n CSLexer lexer = new CSLexer(new ANTLRFileStream(f));\ntokens.TokenSource = lexer;\n\n// Create a parser that reads from the scanner\nCSParser parser = new CSParser(tokens);\n\n// start parsing at the compilationUnit rule\nCSParser.compilation_unit_return x = parser.compilation_unit();\nobject ast = x.Tree;\n```\n\nWhat can I do with the x which is of compilation_unit_return type, to extract its root, its classes, its methods etc? Do I have to extract its Adaptor out? How do I do that? Note that the compilation_unit_return is defined as such in my CSParser (which is automatically generated by ANTLR):\n\n```\npublic class compilation_unit_return : ParserRuleReturnScope\n {\n private object tree;\n override public object Tree\n {\n get { return tree; }\n set { tree = (object) value; }\n }\n };\n```\n\nHowever the tree I am getting is of the type object. I run using the debugger and seemed to see that it is of the type BaseTree. But BaseTree is an interface! I don't know how it relates to BaseTree and don't know how to extract details out from this tree.\n\nI need to write a visitor which has visit to its class, method, variables, etc. The ParserRuleReturn class extends from RuleReturnScope and has a start and stop object, which I don't know what it is.\n\nFurthermore, there is this TreeVisitor class provided by ANTLR which looks confusing. It requires an Adaptor to be pass as a parameter to its constructor (if not it will use the default CommonTreeAdaptor), tt's why I asked about the how to obtain the Adaptor eariler on. And other issues too. For the API, you can refer to http://www.antlr.org/api/CSharp/annotated.html\n\n========================================\n\nTop Answer:\nYou can set the AST tree type in your grammar options at the top of the file like so: \n\n```\ntree grammar CSharpTree;\noptions { \n ASTLabelType = CommonTree\n}\n```\n\nI would build a 3rd grammar or work it into your existing parser grammar that turns the tree into classes that you create. For example assume you've got a rule that matches the plus operator and it's 2 arguments. You can define a rule matching that tree that creates a class that you've written, let's call it PlusExpression like this:\n\n```\nplusExpr returns [PlusExpression value]\n : ^(PLUS left=expr right=expr) { $value = new PlusExpression($left.value, $right.value); }\n```\n\nexpr would be another rule in your grammar matching expressions. left and right are just aliases given to the tree values. The part in between the { }'s is pretty much turned into C# code verbatim with the exception of replacing the variable references. The .value property off of $left and $right comes from the return specified off of the rules that they were created from.\n\n========================================\n\nCode:\n```text\n// Create a scanner that reads from the input stream passed to us\n CSLexer lexer = new CSLexer(new ANTLRFileStream(f));\ntokens.TokenSource = lexer;\n\n// Create a parser that reads from the scanner\nCSParser parser = new CSParser(tokens);\n\n// start parsing at the compilationUnit rule\nCSParser.compilation_unit_return x = parser.compilation_unit();\nobject ast = x.Tree;\n```\n\n```text\npublic class compilation_unit_return : ParserRuleReturnScope\n    {\n        private object tree;\n        override public object Tree\n        {\n            get { return tree; }\n            set { tree = (object) value; }\n        }\n    };\n```\n\n```text\nBaseTree\n```\n\n```text\nType\n```\n\n```text\nText\n```\n\n```text\nChildren\n```\n\n```text\nmember_declaration\n```\n\n```text\nmember_declaration\n```\n\n```text\nTypeBuilder\n```\n\n```text\nExpression<TDelegate>\n```\n\n```text\nCompileToMethod\n```\n\n```text\nstring\n```\n\n```text\nExpression\n```\n\n```text\ntree grammar CSharpTree;\noptions { \n    ASTLabelType = CommonTree\n}\n```\n\n```text\nplusExpr returns [PlusExpression value]\n   : ^(PLUS left=expr right=expr) { $value = new PlusExpression($left.value, $right.value); }\n```\n\n========================================\n\nComments:\n- It is an abstract class... public abstract class BaseTree : ITree\n- Well yes, and why would it stop you? You have the root node of the tree, which you know to be of a type which has all methods necessary to retrieve its children (and thus walk the tree to any depth).\n- Unfortunately, `CompileToMethod` cannot truly be used in this scenario because of its inherent limitations - there's no way to compile in a code to another method that you're generating alongside, and the target `MethodBuilder` has to be for a static method only. See connect.microsoft.com/VisualStudio/feedback/&hellip; for more details.\n- Nice. Well, then I'd start by building an `Expression` compiler that works in those cases, and then use that instead. :) I'd keep the `Expression` compiler as an independent module so it could be used with other projects (and maybe I'll do it for fun anyway).\n- Perhaps just to deviate a bit from my qn above: I hv the source code for antlr-3.1.3. N under the runtime folder, there are 2 folders - CSharp n CSharp3. Since u mentioned abt CSharp3, u know why there are 2 folders? However the API provided online seemed to be for CSharp only not CSharp3, also the compiled dll too. If someone wants to use CSharp3, he has to compile the codes there himself? To me it seems like CSharp3 has more advanced features, but not really relevant in my context.\n- I am actually not too sure what is going on in the discussion btn u and Pavel Minaev. So the conclusion of ur discussion is tt the steps u provided above are not workable?\n- Initally I just want to use an API equivalent to \"help.eclipse.org/help33/index.jsp?topic=/&hellip;\" in Java to achieving the transversal of C# AST. Unfortuntably from what I researched, the current API provided by C# compiler is a black box, doesn't hv the feature tt allows one to manipulate its AST. So I had to come out with my own compiler, tt's why I chose to use ANTLR... Hopefully u or someone else do hv ways to solve my doubts. I do welcome ways faster than using ANTLR (i.e. an existing C# compiler tt).\n- This doesn't really answer the question about ANTLR, it's just a set of steps of how you would approach building a compiler, which is an entirely different question.","metadata":{"transformedAt":"2026-08-18T18:32:17.804Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":15,"totalLines":154,"estimatedTokens":1597}}506{"id":"stack-8439893","source":"stackoverflow","questionId":8439893,"title":"How do I reverse engineer a compiler?","tags":["compiler-construction","reverse-engineering"],"text":"Title: How do I reverse engineer a compiler?\nTags: compiler-construction, reverse-engineering\nSource: Stack Overflow\n\nQuestion:\nI have a compiler that compiles to undocumented bytecode for an undocumented VM. I'd like to be able to compile to the same VM, but I'm not sure how to go about doing this. How do I learn to do this? Has anyone published a log or journal of them doing the same thing?\n\n**EDIT**: I neglected to mention that this is the RobotC 3.0 compiler for LEGO Mindstorms. Before anyone suggests something else, I know all about nXc and similar projects, and they aren't an option since I'm helping out a FIRST FTC robotics team, which is only allowed to use RobotC or LabView.\n\nAs for those of you who suggested that I target the language of the compiler, I'm not doing that yet because I hope that certain features of the hardware that aren't exposed by the compiler will be exposed by the firmware, and because I want to do more custom memory management than permitted by RobotC.\n\n========================================\n\nTop Answer:\nThere appears to be a ton of resources that you can look at here:\n\nhttp://en.wikipedia.org/wiki/Lego_Mindstorms_NXT_2.0\n\n========================================\n\nComments:\n- A compiler is just another program and can be debugged as so.\n- This could take months. How about having your compiler target the input language of the compiler you already have?\n- Yeah, we're only required to use the firmware (VM), not the actual compiler. I'm a PL person, so compilers are like second nature to me, as long as I have some kind of spec about the bytecode.\n- I don't believe you understand my issue -- none of those resources apply since RobotC uses custom firmware, and I am limited to the use of RobotC (LabView is really not an option here).\n- Unfortunately \"RobotC ... requires custom firmware in order to run.\" BTW great suggestion about targeting the input language - I'd suggest moving that comment into your answer. Although that won't help the OP it might help others in a similar situation.","metadata":{"transformedAt":"2026-08-18T18:32:17.804Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":26,"estimatedTokens":512}}507{"id":"stack-4392518","source":"stackoverflow","questionId":4392518,"title":"Interpreters and Dynamically Typed Languages","tags":["compiler-construction","programming-languages","interpreter","dynamic-languages"],"text":"Title: Interpreters and Dynamically Typed Languages\nTags: compiler-construction, programming-languages, interpreter, dynamic-languages\nSource: Stack Overflow\n\nQuestion:\nWhy are programs that have dynamically typed languages usually interpreted rather than compiled?\n\n========================================\n\nTop Answer:\nYou are observing a non-causal correlation:\n\n- Dynamic typing and interpretation correlate because both are easy to implement.\n\n- Static typing and compilation correlate because both are conducive of predictably-good performance.\n\nCompilers are usually retrofitted onto dynamically-typed languages in an attempt to improve performance (because performance is often very poor). For example, here's how long some major dynamically-typed languages were interpreted for before their first compiler was written: Lisp (1958-1962), Mathematica (1988-2004), Lua (1993-2004), Python (1991-2002) and Javascript (1995-2009). In contrast, languages like OCaml (1996) and F# (2001) were released first as compilers.\n\n========================================\n\nComments:\n- What languages do you have in mind?\n- @TomasxK: How about: Erlang, Lisp, Lua, Prolog, Ruby, and others that have interpreters.\n- They aren't necessarily; many Common Lisp systems compile to native code. Also, what's your definition of \"interpreted\" vs. \"compiled\"? Where do Python and Java fall on that?\n- @caveman: I carefully worded my question to say \"usually.\" I am well aware that they can be compiled, but dynamic typing and interpreters is the preferred combination, no?\n- Preferred by whom, and for what? Many widely used languages are dynamically typed and only compiled down to some sort of bytecode, but that's as far as it goes.\n- Some dynamically typed languages are compiled to native code, although you seem to think that impossible.\n- I am not going to say you're wrong but I can't think of an example of a fully dynamic typed language that is compiled.\n- Many Common Lisp implementations are fully compiled. It is possible to specify types statically if you want to, but that's an optimization.\n- Forth! A bit tongue in cheek, but true.\n- Ok, I changed the *impossible* to *hard* though I think the two examples given, *Common Lisp* and *Forth* kinda \"cheat\" in that they are carry around the code to compile more code on-the-fly which is kinda fuzzy in regards to Interpreters vs Compilers. I will concede the point though.\n- -1 Both of your definitions are wrong. \"majority of its type checking\". Static type systems reject at compile time programs that have type errors. Nothing to do with \"majority\". \"eval functions, which execute arbitrary data as code, become possible\". Several statically-typed languages have eval. MetaOCaml even provides statically type checked eval!\n- @JonHarrop: these are not my definitions, they are from Wikipedia. The case about evals was an example not an absolute.\n- @AndrewWhite: I appreciate they are from Wikipedia but the definitions and the example are incorrect.\n- This is an, um, really non-standard definition of \"compiler\". Most of us use the word to refer to a program that translates a computer language to a more basic one.\n- A type checker is not a compiler, but rather a part of one (or step of a compilation pipeline).\n- No, even worse: It's *can* be part of a compiler, but it's totally optional. Even if you (wrongly) only count the compilers that compile to \"real\" target languages like x86 machine code as compilers, e.g. Lisp is dynamically-typed and there are many native compilers for Scheme and CL.\n- The Haskell and ML crowd loves static typing not only mainly for performance (although sometimes being as fast as C is quite cool), but for the safety it can provide. That being said, static FP typesystems are ten times as powerful and expressive as your average OOP language's type system.\n- I totally agree. It's really a reason for *not* picking dynamic typing.\n- Most modern \"interpreted\" languages actually go through a \"compilation phase\" of sorts -- CPython goes to an intermediate byte-code (cached in `pyc`) files and clojure will actually compile to Java Byte-code. The issues \"compiling\" (going from source-code to an intermediate, weather it is \"machine code\" -- which is interpreted by a CPU -- or some byte-code such as JBC, which is interpreted by a JVM) and \"dynamic typing\" are somewhat orthogonal.","metadata":{"transformedAt":"2026-08-18T18:32:17.804Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":40,"estimatedTokens":1093}}508{"id":"stack-39948257","source":"stackoverflow","questionId":39948257,"title":"ELF sections not being mapped to segments as expected","tags":["compiler-construction","x86-64","elf"],"text":"Title: ELF sections not being mapped to segments as expected\nTags: compiler-construction, x86-64, elf\nSource: Stack Overflow\n\nQuestion:\nI am writing a compiler and am just starting to generate ELF executables from scratch. I am creating a `.text` section (although it doesn't have a name because I am not creating a string table yet), and trying to put it within a `PT_LOAD` segment. However, `readelf` isn't reporting that the section is mapped into the segment and `objdump` refuses to disassemble the code in the `.text` section. This is the readout of `readelf`, with some bits omitted for brevity:\n\n```\nELF Header:\n Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 \n Class: ELF64\n Data: 2's complement, little endian\n Version: 1 (current)\n OS/ABI: UNIX - System V\n ABI Version: 0\n Type: EXEC (Executable file)\n Machine: Advanced Micro Devices X86-64\n Version: 0x1\n Entry point address: 0x0\n Start of program headers: 64 (bytes into file)\n Start of section headers: 122 (bytes into file)\n Flags: 0x0\n Size of this header: 64 (bytes)\n Size of program headers: 56 (bytes)\n Number of program headers: 1\n Size of section headers: 64 (bytes)\n Number of section headers: 2\n Section header string table index: 0\n\nSection Headers:\n [Nr] Name Type Address Offset\n Size EntSize Flags Link Info Align\n [ 0] NULL 0000000000000000 00000000\n 0000000000000000 0000000000000000 0 0 0\n [ 1] PROGBITS 0000000008048000 00000078\n 0000000000000002 0000000000000000 AX 0 0 16\nKey to Flags:\n W (write), A (alloc), X (execute), M (merge), S (strings), l (large)\n I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)\n O (extra OS processing required) o (OS specific), p (processor specific)\n\nThere are no section groups in this file.\n\nProgram Headers:\n Type Offset VirtAddr PhysAddr\n FileSiz MemSiz Flags Align\n LOAD 0x0000000000000078 0x0000000008048000 0x0000000008048000\n 0x0000000000000002 0x0000000000000002 R E 1000\n```\n\nAt offset `0x78`, I am just emitting two `push ebx`s (opcode `0x53`) for testing. Here is the hex dump:\n\n```\n00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|\n00000010 02 00 3e 00 01 00 00 00 00 00 00 00 00 00 00 00 |..>.............|\n00000020 40 00 00 00 00 00 00 00 7a 00 00 00 00 00 00 00 |@.......z.......|\n00000030 00 00 00 00 40 00 38 00 01 00 40 00 02 00 00 00 |....@.8...@.....|\n00000040 01 00 00 00 05 00 00 00 78 00 00 00 00 00 00 00 |........x.......|\n00000050 00 80 04 08 00 00 00 00 00 80 04 08 00 00 00 00 |................|\n00000060 02 00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 |................|\n00000070 00 10 00 00 00 00 00 00 53 53 00 00 00 00 00 00 |........SS......|\n00000080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|\n*\n000000b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 |................|\n000000c0 00 00 06 00 00 00 00 00 00 00 00 80 04 08 00 00 |................|\n000000d0 00 00 78 00 00 00 00 00 00 00 02 00 00 00 00 00 |..x.............|\n000000e0 00 00 00 00 00 00 00 00 00 00 10 00 00 00 00 00 |................|\n*\n000000fa\n```\n\nEdit: Question - Why isn't a section to segment mapping shown between the two, and why is no disassembly shown by `objdump`?\n\n========================================\n\nCode:\n```text\nELF Header:\n  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 \n  Class:                             ELF64\n  Data:                              2's complement, little endian\n  Version:                           1 (current)\n  OS/ABI:                            UNIX - System V\n  ABI Version:                       0\n  Type:                              EXEC (Executable file)\n  Machine:                           Advanced Micro Devices X86-64\n  Version:                           0x1\n  Entry point address:               0x0\n  Start of program headers:          64 (bytes into file)\n  Start of section headers:          122 (bytes into file)\n  Flags:                             0x0\n  Size of this header:               64 (bytes)\n  Size of program headers:           56 (bytes)\n  Number of program headers:         1\n  Size of section headers:           64 (bytes)\n  Number of section headers:         2\n  Section header string table index: 0\n\nSection Headers:\n  [Nr] Name              Type             Address           Offset\n       Size              EntSize          Flags  Link  Info  Align\n  [ 0] <no-name>         NULL             0000000000000000  00000000\n       0000000000000000  0000000000000000           0     0     0\n  [ 1] <no-name>         PROGBITS         0000000008048000  00000078\n       0000000000000002  0000000000000000  AX       0     0     16\nKey to Flags:\n  W (write), A (alloc), X (execute), M (merge), S (strings), l (large)\n  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)\n  O (extra OS processing required) o (OS specific), p (processor specific)\n\nThere are no section groups in this file.\n\nProgram Headers:\n  Type           Offset             VirtAddr           PhysAddr\n                 FileSiz            MemSiz              Flags  Align\n  LOAD           0x0000000000000078 0x0000000008048000 0x0000000008048000\n                 0x0000000000000002 0x0000000000000002  R E    1000\n```\n\n```text\n00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|\n00000010  02 00 3e 00 01 00 00 00  00 00 00 00 00 00 00 00  |..>.............|\n00000020  40 00 00 00 00 00 00 00  7a 00 00 00 00 00 00 00  |@.......z.......|\n00000030  00 00 00 00 40 00 38 00  01 00 40 00 02 00 00 00  |....@.8...@.....|\n00000040  01 00 00 00 05 00 00 00  78 00 00 00 00 00 00 00  |........x.......|\n00000050  00 80 04 08 00 00 00 00  00 80 04 08 00 00 00 00  |................|\n00000060  02 00 00 00 00 00 00 00  02 00 00 00 00 00 00 00  |................|\n00000070  00 10 00 00 00 00 00 00  53 53 00 00 00 00 00 00  |........SS......|\n00000080  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|\n*\n000000b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 01 00  |................|\n000000c0  00 00 06 00 00 00 00 00  00 00 00 80 04 08 00 00  |................|\n000000d0  00 00 78 00 00 00 00 00  00 00 02 00 00 00 00 00  |..x.............|\n000000e0  00 00 00 00 00 00 00 00  00 00 10 00 00 00 00 00  |................|\n*\n000000fa\n```\n\n```text\n.text\n```\n\n```text\nPT_LOAD\n```\n\n```text\nreadelf\n```\n\n```text\nobjdump\n```\n\n```text\n.text\n```\n\n```text\nreadelf\n```\n\n```text\n0x78\n```\n\n```text\npush ebx\n```\n\n```text\n0x53\n```\n\n```text\nobjdump\n```\n\n```text\nreadelf\n```\n\n```text\nobjdump\n```\n\n```text\nELF\n```\n\n```text\nELF\n```\n\n```text\nobjdump\n```\n\n========================================\n\nComments:\n- Did you have a question? You state a bunch of facts, and then...? If your question is \"why doesn't readelf show section to segment mapping, the answer is likely going to be: don't create a section with no name\".\n- @EmployedRussian - added a question. So is a string table with section names required for disassembling with `objdump` and showing mappings? I'll try adding a string table when I get home, and see if it makes a difference, thanks!\n- @EmployedRussian yup - it seems like a string table is needed to report the mappings for some reason. If you want to post that as an answer, I'll accept it.\n- To elaborate, for a section to be valid it's 'name' field must point to a non-zero entry in a valid string table.","metadata":{"transformedAt":"2026-08-18T18:32:17.804Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":17,"totalLines":205,"estimatedTokens":1825}}509{"id":"stack-11452791","source":"stackoverflow","questionId":11452791,"title":"What's the use case for D's compile time evaluation","tags":["compiler-construction","ocaml","d"],"text":"Title: What's the use case for D's compile time evaluation\nTags: compiler-construction, ocaml, d\nSource: Stack Overflow\n\nQuestion:\nI integrated this feature in my favoriate language OCaml, I know that this is the sexy feature in D, but what's the use case for compile time evaluation except some optimizations?\nThe more the better, the geeker the better :-)\n\n========================================\n\nTop Answer:\nSome suggestions:\n\n- How about parsing a PEG grammar, assembling a PEG parser, and then using said parser, **all at compile time**? Here's the Pegged project.\n\n- Construction of large static lookup tables.\n\n- Generating optimal native code for a regular expression: std.regex.StaticRegex\n\n- Implementing protobufs from a definition file.\n\n- Many functions, if their inputs are known at compile time, can be evaluated then instead of at runtime--all without changing the implementation of the function.\n\n========================================\n\nCode:\n```text\nmixin(import_c(\"header.h\"))\n```\n\n```text\n\"%0.2f\"\n```\n\n```text\ntemplate StructFromFile(string fileName){\n    string makeStructMembersCode(string[] s){\n        if(0==s.length){\n            return null;\n        }\n        string memberName=s[0].strip;\n        return q{\n            string }~memberName~q{;\n            }~makeStructMembersCode(s[1..$]);\n    }\n    struct StructFromFile{\n        mixin(makeStructMembersCode(import(fileName).splitLines));\n    }\n}\n```\n\n```text\nmixin\n```\n\n```text\nStructFromFile\n```\n\n```text\nmixin( f( import(\"my_code_file\") ) );\n```\n\n```text\nstring f(string code)\n```\n\n```text\nmain.d\n```\n\n```text\nmain.d\n```\n\n========================================\n\nComments:\n- What do you mean by you \"integrated this feature in\" your \"favorite programming language OCaml\"? Why did you do it when you have no use cases for it?\n- it's a side effect of my other projects. As I said optimization is one use case. But I think there should be more\n- Similar question: stackoverflow.com/questions/3555456/&hellip;\n- @bobzhang, take a look at MetaOCaml, it's much more fun (and more flexible than anything you can do with D limited metaprogramming). And, use case, as always with any metaprogramming facility, is in implementing eDSLs efficiently.\n- @SK-logic I have played with it before. But it does not catch up with latest ocaml :-(. My integration is a library which also support the whole language compile time evaluation, so there's no patch compiler issue. IMHO, there's a lot of things to explore if you have compile time evaluation in a really convenient way :-)\n- Thanks for your suggestions. I am not familiar with D, if the compile time evaluation returns a custom defined data structure, how does D handle this? would you shed some light on this?\n- or be more precise, what's the D's limitation for the function making use of compile time evaluation?\n- Here is the full list of restrictions.\n- I have found one important limitations in D Compile time evaluation that all sources should appear in a file which makes very hard to write very powerful compile time evaluation function, since you can not use any library then. How do you think of it\n- That's basically the same limitation that C++ templates face. There is no easy fix for that. Some people want to embed source in library object files to get around this but nobody has attempted it yet.","metadata":{"transformedAt":"2026-08-18T18:32:17.804Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":88,"estimatedTokens":835}}510{"id":"stack-3314270","source":"stackoverflow","questionId":3314270,"title":"Is C inefficient compared to Assembly?","tags":["c","compiler-construction","assembly"],"text":"Title: Is C inefficient compared to Assembly?\nTags: c, compiler-construction, assembly\nSource: Stack Overflow\n\nQuestion:\nThis is purely a theory question, so, given an \"infinite\" time to make a trivial program, and an advanced knowledge of C and Assembly, is it really better to do something in Assembly? is \"performance\" lost when compiling C into Assembly (to machine code)?\n\nBy performance I mean, do modern C compilers do a bad job at certain tasks that programming directly in Assembly speeds up?\n\n========================================\n\nTop Answer:\nC is not inefficient compared to anything. C is a language, and we don't describe languages in terms of efficiency. We compare *programs* in terms of efficiency. C doesn't write programs; *programmers* write programs.\n\nAssembly gives you immense flexibility when comparing with C, and that is at the cost of time programming. If you are a guru C programmer and a guru Assembly programmer, then chances are you might be able to squeeze some more juice with Assembly for writing any given program, but the price for that is virtually certain to be prohibitive.\n\nMost of us aren't gurus in either of these languages. For most of us, giving the responsibility of performance tuning to a C compiler is a double win: you get the wisdom of a number of Assembly gurus, the people who wrote the C compiler, along with an immense amount of time in your hands to further correct and enhance your C program. You also get portability as a bonus.\n\n========================================\n\nComments:\n- @Mark: I don't disagree with the sentiment of this answer, but why can't an assembler make the same optimizations regarding instruction scheduling as a C compiler?\n- @indiv: They can, in theory. In practice, it can be insanely difficult, particularly for RISC and RISC-like CPU's (which, these days, is essentially all of them).\n- Yes, you can do anything that the C compiler can do, it's just that *you* have to do it. Have fun =)\n- @Steven Out of curiosity, do you have a link with statistics about how many RISC CPUs there are? It was my understanding that most desktops/laptops are, for the most part, some variant of x86 and thus CISC\n- I couldn't find any info on the current state of assemblers (probably it's pretty bleak), but I did find a paper on Intel's IA-64 Assembly Assistant that would optimize instruction scheduling (page 8). The paper also discusses some limitations of optimizing assembly vs C if anyone is interested. download.intel.com/technology/itj/q41999/pdf/assemble.pdf. And @Ed, I was referring to optimizations that can be done by the assembler when translating assembly to machine code.\n- @docgnome: Even supposed CISC chips, like the x86, have adapted many RISC techniques. For example, it used to be faster to use complex instructions to move bytes en-masse (MOVSW, etc) but now it's faster to use RISC-like load/store techniques.\n- @Steven Sudit: That was true on the Pentium, but by the time of the Pentium Pro `rep movsw` was faster again. Those instructions are still being improved - eg see here: lkml.org/lkml/2009/11/6/66\n- @caf: Thank you for the interesting link. Looks like things have come around in at least that regard. I suspect my point about the difficulty of hand-scheduling ops to take full advantage of pipelining still stands, though.\n- @caf Do you know how much of this is because of modern x86 and x86-64 chips being actually implemented as mostly RISC architectures underneath the covers, with a lot of the complex instructions implemented in bytecode in terms of those basic instructions?\n- +1, and to add emphasis to this: A modern compiler generally knows what's most efficient for thousands of different CPUs, including ones implementing the same instruction set. That compiler also knows how to convert conventional, easier-for-humans-to-understand C idioms into each specific CPU's most efficient code, taking into account things like cache sizes, pipeline depths, etc. Compilers don't always get this perfectly or thoroughly, but they contain within them deep optimization knowledge and can tap computing power to do in seconds optimizations that human brains need hours to verify.\n- You can compile the C and look at the Assembly Language output in the debugger. This lets you tweak the C and repeat the process until you've gotten the compiler to generate the code you want.\n- You can also generate assembly from arbitrary object code with objdump. Compiler support is not necessary.\n- Agreed. A friend and I were diddling around with square rooty things the other day, and they were able to write some assembly to take advantage of the XMM intrinsics: it blew the compiled code out of the water.\n- This was a theoretical question asked, not a practical one.\n- I agree with your point. On a lighter note - **\"all of your source code than you could possibly hold in your head\" ** And then they say this about human memory effective-mind-control.com/human-memory-capacity.html\n- I see your point, however I meant that statement from a slightly different angle. It's not so much a matter of memory as it is an intuitive awareness of all the different interactions among the various systems. Think L1 cache instead of Flash memory. :)\n- +1. I think it's also worth adding that one does not simply become a guru with *assembly* by itself. One becomes a guru with what assembly performs best for a program *on a given CPU model*, which varies not just by workload but also by \"transparent\" CPU details like cache size and cache line size, branch predictor performance, pipeline depth, and all sorts of other details that change \"invisibly\" below the instruction set itself. Compilers bring that per-CPU knowledge to all so long as one contributor takes the time to add support for it. A human guru has to learn it personally for each one.\n- Also a great point. x86 compilers are good. Other architectures, maybe not so good.\n- Not all assembly has a 1:1 match with the bytecode - I have been working with a cpu that has a 'high level assembly' that the assembler takes.\n- @Paul I understand that you can use a high level assembly language if you want, on many processors including x86 - but I think that this is more properly a programming language in itself, not assembly as the question indicated. Is there a 'low level assembly' available for your processor? Even if there is no such assembler provided by the manufacturer, the output of your current assembler is just encoded low-level assembly.\n- It does translate into a low-level assembly - but that's not really supported for users. Most of the high-level stuff involves collapsing similar instructions into a single instruction with easier syntax. It doesn't have macro assembly type stuff.","metadata":{"transformedAt":"2026-08-18T18:32:17.804Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":42,"estimatedTokens":1688}}511{"id":"stack-489783","source":"stackoverflow","questionId":489783,"title":"Is Scalas/Haskells parser combinators sufficient?","tags":["compiler-construction","scala","haskell","parsing","parser-combinators"],"text":"Title: Is Scalas/Haskells parser combinators sufficient?\nTags: compiler-construction, scala, haskell, parsing, parser-combinators\nSource: Stack Overflow\n\nQuestion:\nI'm wondering if Scalas/Haskells parser combinators are sufficient for parsing a programming language. More specifically the language MiniJava. I'm currently reading compiller construction and jflex and java cup is quite painful to work with so I'm wondering if I could/should use parser combinators instead.\nThe MiniJava syntax is very small.\nMiniJavas BNF: http://www.cambridge.org/us/features/052182060X/grammar.html\n\n========================================\n\nTop Answer:\nI've never used Scala, but the existence of a definitive BNF makes this easy.\n\nTrivially translated into Haskell's Text.ParserCombinators.Parsec:\n\n```\ngoal = do c etc. The PArrows translation is pretty trivial too. You'll probably find it easier to have a distinct lexing phase before the parser, but you can do without too.\n\n========================================\n\nCode:\n```text\ngoal = do c <- mainClass\n          cs <- many classDeclaration\n          eof\n          return $ c:cs\nmainClass = do token \"class\"\n               name <- identifier\n               ...\n```\n\n```text\nlexer = makeTokenParser javaStyle\n```\n\n========================================\n\nComments:\n- I'm happy to announce that it certainly was!\n- Ah yes, sorry the blog is in Spanish. I've also added support for C/Java parsing to the app.","metadata":{"transformedAt":"2026-08-18T18:32:17.804Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":41,"estimatedTokens":363}}512{"id":"stack-4318345","source":"stackoverflow","questionId":4318345,"title":"Is \"parsing\" a subset of \"compiling\"?","tags":["parsing","compiler-construction","terminology"],"text":"Title: Is \"parsing\" a subset of \"compiling\"?\nTags: parsing, compiler-construction, terminology\nSource: Stack Overflow\n\nQuestion:\nWhen I think of \"compiling\" I think of turning C++ code into a binary. Or perhaps C# into CLR byte code. But \"parsing\" could be something like parsing Python, or a web template language, where it doesn't need to produce any binaries, but can either execute the code immediately, statement-by-statement, or output HTML directly.\n\nWould you basically be doing the same task in either case? Ignoring the language syntax, would compiling C++ be equally as difficult as parsing a website template file (Django, Smarty, whatever), or Python?\n\nWhat I'm trying to allude at, is **if I study \"compiling\" or read a book on \"compiling\" will I necessarily pick up the skills to parse non-compiled languages?**\n\n========================================\n\nTop Answer:\nNo, parsing and compiling can be completely independent.\n\n- A parser may not be emitting any code at all. It could be parsing some data object (JSON, XML, whatever)\n\n- A compiler may not have source code to start with - it could be presented with an abstract syntax tree, already parsed, and just have to emit the relevant code\n\nMost compilers *include* a parsing step, but I don't think it's necessarily a \"subset\" of compiling, and parsing certainly doesn't have to have anything to do with compilation.\n\n========================================\n\nCode:\n```text\nlet x = 0\nwhile x < 10\n    print x\n    x := x + 1\n```\n\n```text\n[LET; String(\"x\"); EQ; Int(0); NEWLINE; WHILE; String(\"x\");\n LT; VAL(10); ... ]\n```\n\n```text\n// AST definition\ntype expr =\n    | Block of expr list\n    | Assign of string * expr\n    | While of expr * expr\n    | Call of string * expr list\n    | Add of expr * expr\n    | Var of string\n    | Int of int\n\n// AST instance created from token stream\nBlock\n    [\n        Assign(\"x\", Int(10));\n        While\n        (\n            LessThan(Var(\"x\"), Int(10)),\n            Block\n                [\n                    Call(\"print\", [Var(\"x\")]);\n                    Assign(\"x\", Add(Var(\"x\"), Int(1)));\n                ]\n        );\n    ]\n```\n\n```text\nVar(\"x\")\n```\n\n```text\nVar(\"y\")\n```\n\n========================================\n\nComments:\n- compiling requires parsing, but parsing does not require compiling. There is a relationship, but not an is-a relationship.\n- Adding another piece to the puzzle, in asp.net we can deploy the source code files directly without compilation due to Dynamic Compilation!\n- If you are compiling source code, then I believe parsing is necessary.\n- @jjnguy: I'd agree. However, I'd argue that a compiler doesn't always have source code as its input. A JIT compiler is one example.\n- Wow. Wasn't expecting such a detailed answer. Thank you! Didn't know this :)","metadata":{"transformedAt":"2026-08-18T18:32:17.804Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":80,"estimatedTokens":697}}513{"id":"stack-4258994","source":"stackoverflow","questionId":4258994,"title":"Online Exe Builder","tags":["c#",".net","visual-studio","compiler-construction"],"text":"Title: Online Exe Builder\nTags: c#, .net, visual-studio, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI generally use Visual Studio for creating Windows exe in C# .NET 2.0 / 3.5\nBut right now I dont have VS or any IDE / Compiler to build exe on my system and suppose I don't have admin rights to install them\nTell me please, how Can I build an exe online, something like coderun.com or codepad.org\nBut these sites are not helpful to me, as I need to build exe & download to my PC and use it.\n\nI need some online solution to build EXE's online & run them on my PC.(Windows XP SP3)\n\n========================================\n\nTop Answer:\nThe .NET Framework which ships with every modern version of Windows (and will probably be installed even on older versions) includes a command-line C# compiler called `csc.exe`. You can build your exe with this.\n\nHere are some instructions for using it:\n\n- MSDN: Command-line Building With csc.exe\n\n The csc.exe executable is usually located in the `Microsoft.NET\\Framework\\` folder under the system directory.\n\nAlso, if you have the full version of .NET 4.0 (not client profile), you will probably have msbuild.exe in the same place. This is even easier to use, as you can simply pass it a Visual Studio solution file and it will build the whole solution for you, the same way Visual Studio does (apart from some special project types like installers).\n\n========================================\n\nCode:\n```text\ncsc.exe\n```\n\n```text\nMicrosoft.NET\\Framework\\<version>\n```\n\n========================================\n\nComments:\n- Can you request admin rights or get an admin to install VS for you?\n- I do not have admin rights neither do i Have .NET framework installed or even if it is then it is Version 1.1 which comes with Windows XP SP2 or SP3 packs.Please suggest me a online site or service, payable or free to build exe on Windows ( particularly Windows XP SP3 ). Thanks\n- @user517780 if you want to download and run your .NET 2.0 exe as you said in your original question then you will need .NET Framework 2.0 installed to run it.\n- ya thats true.But I am also talking In case where I don't have .NET framework installed and I am required to build exe ?? What should I do in such case.. or else compilr.com is great !! Thanks Oded.\n- ideone is great for testing codes, I can run scripts :)","metadata":{"transformedAt":"2026-08-18T18:32:17.804Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":44,"estimatedTokens":585}}514{"id":"stack-676503","source":"stackoverflow","questionId":676503,"title":"What does casting do at compiler/machine level?","tags":["java","compiler-construction","casting"],"text":"Title: What does casting do at compiler/machine level?\nTags: java, compiler-construction, casting\nSource: Stack Overflow\n\nQuestion:\nI have often wondered what exactly does casting do at compiler or machine level. What does it do with the 0 and 1s in memory?\n\nCan anyone point me at some good literature.\n\n========================================\n\nTop Answer:\nOthers have covered the basics, but I'd like to say a few words about how a compiler is implemented that may be enlightening in his case.\n\nThe compiler maintains a list (called the *symbol table*) of variable *names* in use at any particular point in the program and some information about the variables. The list of information includes:\n\n- their assigned storage (in this register, at that memory location, etc...)\n\n- what type they are (i.e. integer or string or `SubWhatsitObj`) including any restriction (like for instance constantness)\n\n- any linkage information the compiler needs\n\nThe compiler uses this information to decide how to treat expressions involving the variables. The kind of meta-information that is stored in the symbol table can also be derived for any expression from it's components.\n\nExcept in the special case of numeric type conversion, a cast just tells the compiler to use *different* meta-information for a variable or expression than would usually be the case. No bits in memory are affected at all, but the outcome of a calculation may be.\n\n========================================\n\nCode:\n```text\nClassCastException\n```\n\n```text\nfloat a;\nint b = 3;\n\na = *((float*)&b);\n```\n\n```text\nSubWhatsitObj\n```\n\n========================================\n\nComments:\n- This is only true for non-numeric casts\n- The class cast exception check must be inserted into the byte code somehow, surely?","metadata":{"transformedAt":"2026-08-18T18:32:17.804Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":49,"estimatedTokens":443}}515{"id":"stack-17601984","source":"stackoverflow","questionId":17601984,"title":"Dart vs JavaScript - Are they compiled or interpreted languages?","tags":["javascript","compiler-construction","dart","interpreter"],"text":"Title: Dart vs JavaScript - Are they compiled or interpreted languages?\nTags: javascript, compiler-construction, dart, interpreter\nSource: Stack Overflow\n\nQuestion:\nIs Dart considered to be a compiled or an interpreted language? \nThe same question holds for JavaScript.\n\nThe reason for the question:\n\nI've been watching an interview with the founders of dart, and in 7:10 Lars Bak said that: \n\n \"When you [...] in a JavaScript program, you actually execute JavaScript before you start running the real program. In Dart, you don't execute anything before the first instruction in main is being executed\".\n\nIt sounded to me that he's saying that JavaScript is a compiled language while Dart is an interpreted language. Is it true? \n\nIsn't the Dart VM a compiler?\n\n========================================\n\nTop Answer:\nDart as programming language in its primary implementation can be presented as a `virtual machine` (VM), which is the runtime of programs written in that language.\n\nCurrent `virtual machine` implemented as the `\"just-in-time\"` (JIT) runtime environment engine.\n\nThis means that program `not interpreted` but `compiled`. But this compilation process (translating `source code` to `machine instructions`) is `stretched in time` for an unknown period.\n\nThis allows virtual machine to `defer performing certain operations indefinitely` or `never performing them`.\n\nAssume you have a very `big and complex program` with a `lot of classes` which `may be never be` used in current `short lifetime session` of program execution.\n\nJIT compilation allow not compile all unused classes but just parse it to special `tokens`.\nThese tokens later will be used (`on demand`) for translating them to `intermadiate language` for constructing `machine code`.\n\nThis process is `transparent for user of program`. Compiled (to machine code) only that source code that required for the correct working of the program.\n\nSome source code can be never compiled what `save a lot of time`.\n\n**Conclusion:**\n\nIf Dart language used in its primary state as virtual machine then it `compiled to machine code`.\n\n========================================\n\nCode:\n```text\nclass A {\n  final x;\n  A(this.x);\n  foo(y) => y + x;\n}\n```\n\n```text\nfunction A(x) { this.x = x; }\nA.prototype.foo = function(y) { return y + this.x; }\n```\n\n```text\nA\n```\n\n```text\nclass\n```\n\n```text\nA\n```\n\n```text\nA\n```\n\n```text\nA\n```\n\n```text\nA\n```\n\n```text\nA.prototype.foo = ...\n```\n\n```text\nA\n```\n\n```text\nvirtual machine\n```\n\n```text\nvirtual machine\n```\n\n```text\n\"just-in-time\"\n```\n\n```text\nnot interpreted\n```\n\n```text\ncompiled\n```\n\n```text\nsource code\n```\n\n```text\nmachine instructions\n```\n\n```text\nstretched in time\n```\n\n```text\ndefer performing certain operations indefinitely\n```\n\n```text\nnever performing them\n```\n\n```text\nbig and complex program\n```\n\n```text\nlot of classes\n```\n\n```text\nmay be never be\n```\n\n```text\nshort lifetime session\n```\n\n```text\ntokens\n```\n\n```text\non demand\n```\n\n```text\nintermadiate language\n```\n\n```text\nmachine code\n```\n\n```text\ntransparent for user of program\n```\n\n```text\nsave a lot of time\n```\n\n```text\ncompiled to machine code\n```\n\n========================================\n\nComments:\n- Thanks Wraychus, but firstly: from what I understood, Dart could be either interpreted/compiled by the Dart VM *or* translated to JS. So my question is: when using the Dart VM, is it compiled or interpreted? Secondly, if both languages are interpreted, then the sentence I quoted by Lars Bak makes no sense to me. Any other explanation?\n- Dart VM executes Dart source code, there is no need to first compile Dart code to byte code (as opposed to Java for instance).\n- So it was a long time ago. Currently exists at least four technologies. 1. Interpreted (primitive languages). 2. Compiled to binary files. 3. Translated to byte code + executed in VM + compiled before execution. 4. Distributed as scripts + executed in VM + compiled before execution. Javascript and Dart is number #4.\n- Thanks @Florian Loitsch , but can you please explain what you meant by \"In Dart, the global state is described by the language syntax and thus needs only parsing\". Doesn't every language need parsing? I'd appreciate it if you could please explain it again.\n- `As a consequence Dart programs can start executing \"real\" code faster than JavaScript programs`. This isn't entirely correct in the browser, since `main()` is called *after* the DOM has been loaded. JS can execute before the DOM is ready, so in a practical application, JS can execute sooner. Source. But in terms of the language, you're correct.\n- Sorry, @mezoni, I didn't entirely get that: are you saying that Dart is compiled using JIT? Thanks.\n- Dart source can be translated to `tokens` file forcibly. This process called creation of `snapshot` file. Snapshot not a byte code file but just parsed tokens. This snapshot may be executed in VM. Internally VM executes source code or snapshots using JIT technology.\n- Although this question is from 7 years ago, but since I started learning Flutter and Dart, thus I have to ask my doubts. Let's say I have 3 classes A, B, C and I have instantiated only A. Does this mean that Class B and C are dead codes, which got tokenised but were never used, or they never were token either?\n- @YashasviBhatt Not a `dead code` but an unused code. Of course, all code tokenized and even precompiled to the intermediate language (not a machine dependent language).","metadata":{"transformedAt":"2026-08-18T18:32:17.805Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":31,"totalLines":186,"estimatedTokens":1358}}516{"id":"stack-9420673","source":"stackoverflow","questionId":9420673,"title":"Is it possible to compile c code using python?","tags":["python","c","compiler-construction","compilation","compiler-errors"],"text":"Title: Is it possible to compile c code using python?\nTags: python, c, compiler-construction, compilation, compiler-errors\nSource: Stack Overflow\n\nQuestion:\nI want to build a python program that get as input a path to .c file and then it compile its.\n\nThe program will output OK to the screen if the compilation is sucessful, and BAD otherwise.\n\nI'm been trying to google it, but could not find anything. I've been also trying to run cmd within python with an argument of the compiling program but it didn't work.\n\nTo clarify - I've already got a very specific compiler in my machine which I want to run. I dont want python to act as a compiler. Just get a code, run my compiler over it, and see what's the answer.\n\n- It should work on Linux server with python 2.4.\n\nThanks\n\n========================================\n\nTop Answer:\nYou can compile C code using only the standard library, and it will work on every platform and with every Python version (assuming you actually have a C compiler available). Check out the `distutils.ccompiler` module which Python uses to compile C extension modules. A simple example:\n\n```\n// main.c\n\n#include \n\nint main() {\n printf(\"Hello world!\\n\");\n return(0);\n}\n```\n\nCompilation script:\n\n```\n# build.py\nfrom distutils.ccompiler import new_compiler\n\nif __name__ == '__main__':\n compiler = new_compiler()\n compiler.compile(['main.c'])\n compiler.link_executable(['main.o'], 'main')\n```\n\nEverything else (include paths, library paths, custom flags or link args or macros) can be passed via various configuration options. Check out the above link to the module documentation for more info.\n\n========================================\n\nCode:\n```c\n// main.c\n\n#include <stdio.h>\n\nint main() {\n    printf(\"Hello world!\\n\");\n    return(0);\n}\n```\n\n```text\n# build.py\nfrom distutils.ccompiler import new_compiler\n\nif __name__ == '__main__':\n    compiler = new_compiler()\n    compiler.compile(['main.c'])\n    compiler.link_executable(['main.o'], 'main')\n```\n\n```text\ndistutils.ccompiler\n```\n\n========================================\n\nComments:\n- You might look in distutils but all that will be doing is providing a python layer to gcc or similar compiler, in the end python will no be doing the compiling and will have dependencies.\n- There is a nice Pycon talk Using Python to debug C and C++ code (using gdb) may be it can help you.\n- If you want to run a compiler, you're just running a subprocess with Python. In that case, this question is a total duplicate of every other question seeking to run a subprocess under Python. Close it. Here are all the related questions which all provide approximately the same answer. stackoverflow.com/search?q=python+subprocess. They all answer the \"clarified\" question.\n- possible duplicate of subprocess with timeout\n- how does Scons help in this. Could you please elaborate?\n- why so short instruction? start from \"solder your own processor\"\n- `:1: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives`","metadata":{"transformedAt":"2026-08-18T18:32:17.805Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":85,"estimatedTokens":767}}517{"id":"stack-2507395","source":"stackoverflow","questionId":2507395,"title":"Detecting Infinite recursion in Python or dynamic languages","tags":["python","gcc","compiler-construction"],"text":"Title: Detecting Infinite recursion in Python or dynamic languages\nTags: python, gcc, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nRecently I tried compiling program something like this with GCC:\n\n```\nint f(int i){\n if(iand it ran just fine. When I inspected the stack frames the compiler optimized the program to use only one frame, by just jumping back to the beginning of the function and only replacing the arguments to f. And - the compiler wasn't even running in optimized mode.\n\nNow, when I try the same thing in Python - I hit maximum recursion wall (or probably stack overflow if i set recursion depth too high).\n\nIs there way that a dynamic language like python can take advantage of these nice optimizations?\nMaybe it's possible to use a compiler instead of an interpreter to make this work?\n\nJust curious!\n\n========================================\n\nTop Answer:\nWhen I inspected the stack frames the compiler optimized the program to use only one frame, by just jumping back to the beginning of the function and only replacing the arguments to f.\n\nWhat you're describing is called \"tail recursion\". Some compilers/interpreters support it, some don't. Most don't, in fact. As you noticed, gcc does. And in fact, tail recursion is a part of the spec for the Scheme programming language, so all Scheme compilers/interpreters *must* support tail recursion. On the other hand, the compilers for languages like Java and Python (as well as most other languages, I'd wager) don't do tail recursion.\n\n Is there way that a dynamic language like python can take advantage of these nice optimizations?\n\nDo you mean, *right now*, or are you asking in more abstract terms? Speaking abstractly, yes! It would absolutely be possible for dynamic languages to take advantage of tail recursion (Scheme does, for example). But speaking concretely, no, CPython (the canonical Python interpreter) doesn't have a flag or other parameter to enable tail recursion.\n\n========================================\n\nCode:\n```text\nint f(int i){\n    if(i<0){ return 0;}\n    return f(i-1);\nf(100000);\n```\n\n========================================\n\nComments:\n- Nice question. Something that I have forgotten all about whilst comparing static to dynamic languages.\n- Really? I thought one of the strong points of compilation was for looking for such optimisation strategies? I agree that it doesn't matter if the program is compiled or interpreted, but the optimisation is a strong candidate for the strengths of compilation, one being that you can look for tail recursion.\n- @WeNeed Even if the language is interpreted it can still go through an optimization phase it just can't be as long as with something like C.\n- does that mean then that it is a lot nicer and easier for the interpreter, if you tell it before hand that it needs to do some optimising, like in F# and the \"rec\" keyword?\n- Are functional programs already looking for the tail recursive problem because of the nature of using the stack so much whilst imperatives such as C#, Java, are using mainly heap storage. Curious enough though, 64 bit .net doesn't blow the stack, although 32 bit version does. I know that in F#, the work around is making the function recursive by declaring it so in syntax (rec).\n- What about the stackless python? Does it implement tail call optimization?\n- @drozzy: Not entirely sure. I think it may. I have tried it out on pypy (including the stackless version), but it doesn't look like it implements it (at least currently)","metadata":{"transformedAt":"2026-08-18T18:32:17.805Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":49,"estimatedTokens":877}}518{"id":"stack-17932928","source":"stackoverflow","questionId":17932928,"title":"Is it possible to build Lisp-like macros into an imperative language?","tags":["c","compiler-construction","macros","lisp","language-design"],"text":"Title: Is it possible to build Lisp-like macros into an imperative language?\nTags: c, compiler-construction, macros, lisp, language-design\nSource: Stack Overflow\n\nQuestion:\nWhat is preventing a language like C from having Lisp macros? At what point in the compilation process does C forego the ability to manipulate its code tree?\n\nAnd, is this specifically an interpreted vs. compiled issue?\n\n========================================\n\nTop Answer:\nRust, which is certainly a C-like language for some definitions of \"C-like\", has a Scheme-like macro system.\n\n========================================\n\nCode:\n```text\n[e| ... |] -- expression\n[d| ... |] -- declaration\n[t| ... |] -- type\n[p| ... |] -- pattern\n```\n\n```text\ne\n```\n\n```text\nd\n```\n\n```text\nt\n```\n\n```text\np\n```\n\n```text\ncpp\n```\n\n```text\ngpp\n```\n\n```text\ncpp\n```\n\n========================================\n\nComments:\n- \"What is preventing a language like C from having Lisp macros?\" - the sanity of its creators.\n- @H2CO3 We can always substitute those. :)\n- You mean, like this: programmers.stackexchange.com/questions/53441/&hellip;\n- Perl6 has Algol syntax like C and it's supposed to have parse time subs and operator overloading\n- Nemerle is an example of a language more similar syntactically to C, which has Lisp-like macros. But I don't know much about it. HaXe also has similar macro system. There are however, inherent problems with C, especially those related to maintenance, which make complex macros undesirable. Debugging is one such problem, original ambiguity of syntax and plethora of less known but still valid ways of write the same thing is another one. This may explain why early attempts of improve upon C fought against the very idea of having macros (think D, ADA etc.)\n- It's perfectly possible - take a look at Nemerle, Converge, PFront, Template Haskell, MetaOCaml, MetaLua and other similar languages.\n- Common Lisp is an imperative language with Lisp-style macros. (Like many imperative languages, Common Lisp has first class functions and a syntax for anonymous functions so it also allows functional programming, but idiomatic Common Lisp includes many imperative techniques such as using global variables for configuration and `push`ing values onto a list and then `nreverse`-ing it.)\n- While I liked the answer, I think that using Haskel as an example of imperative language is a bit farfetched. :)\n- @wvxvw: I was using it as an example of a language with complicated syntax (compared to Lisp) but which has sophisticated templates. However, it is also a great imperative language. See: stackoverflow.com/questions/6622524/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.805Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":64,"estimatedTokens":656}}519{"id":"stack-19869163","source":"stackoverflow","questionId":19869163,"title":"Algorithm for computing FIRST and sets for context-free grammars","tags":["parsing","compiler-construction","grammar","ll-grammar"],"text":"Title: Algorithm for computing FIRST and sets for context-free grammars\nTags: parsing, compiler-construction, grammar, ll-grammar\nSource: Stack Overflow\n\nQuestion:\nI need an algorithm to computing FIRST and sets for a grammar. \n\nIs there a simple algorithm or simple code for computing these?\n\n========================================\n\nTop Answer:\nThis can be solved with Graphical calculation of first and sets of a grammar by ASR Murty.\n\nA graphical method has been given to calculate the First and sets of a grammar which is easier to apply for a learner or a student... \n\nThis graphical method is applied to the Expression Grammar to find the First and sets and we also check its correctness with the help of derivations.","metadata":{"transformedAt":"2026-08-18T18:32:17.805Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":17,"estimatedTokens":182}}520{"id":"stack-53596580","source":"stackoverflow","questionId":53596580,"title":"Is the Python's grammar LL(1)?","tags":["python","parsing","compiler-construction","grammar"],"text":"Title: Is the Python's grammar LL(1)?\nTags: python, parsing, compiler-construction, grammar\nSource: Stack Overflow\n\nQuestion:\nPossible duplicate for this question however for me it's not specific enough.\n\nThe python grammar is claimed to be LL(1), but I've noticed some expressions in the Python grammar that really confuse me, for example, the arguments in the following function call:\n\n```\nfoo(a)\nfoo(a=a)\n```\n\ncorresponds to the following grammar:\n\n```\nargument: ( test [comp_for] |\n test '=' test |\n '**' test |\n '*' test )\n```\n\n`test` appears twice in the first position of the grammar. It means that by only looking at `test` Python cannot determine it's `test [comp_for]` or `test '=' test`. \n\nMore examples:\n\n```\ncomp_op: ''|'=='|'>='|''|'!='|'in'|'not' 'in'|'is'|'is' 'not'\n```\n\nNote `'is'` and `'is' 'not'`\n\n```\nsubscript: test | [test] ':' [test] [sliceop]\n```\n\n`test` also appears twice.\n\nIs my understanding of LL(1) wrong? Does Python do some workaround for the grammar during lexing or parsing to make it LL(1) processable? Thank you all in advance.\n\n========================================\n\nTop Answer:\nYou're correct that constructs like `'is' | 'is' 'not'` aren't LL(1). They can be left-factored to LL(1) quite easily by changing it to `'is' notOpt` where `notOpt: 'not' | ϵ` or, if you allow EBNF syntax, just `'is' 'not'?` (or `'is' ['not']` depending on the flavor of EBNF).\n\nSo the language is LL(1), but the grammar technically is not. I assume the Python designers decided that this was okay because the left-factored version would be more difficult to read without much benefit and the current version can still be used as the basis for an LL(1) parser without much difficulty.\n\n========================================\n\nCode:\n```text\nfoo(a)\nfoo(a=a)\n```\n\n```text\nargument: ( test [comp_for] |\n            test '=' test |\n            '**' test |\n            '*' test )\n```\n\n```text\ncomp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'\n```\n\n```text\nsubscript: test | [test] ':' [test] [sliceop]\n```\n\n```text\ntest\n```\n\n```text\ntest\n```\n\n```text\ntest [comp_for]\n```\n\n```text\ntest '=' test\n```\n\n```text\n'is'\n```\n\n```text\n'is' 'not'\n```\n\n```text\ntest\n```\n\n```text\nsimple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE\n```\n\n```text\nsimple_stmt: small_stmt rest_A\nrest_A     : ';' rest_B\n           | NEWLINE\nrest_B     : small_stmt rest_A\n           | NEWLINE\n```\n\n```text\n[a]\n```\n\n```text\n(a b c)*\n```\n\n```text\n(';' small_stmt)*\n```\n\n```text\n';'\n```\n\n```text\nwhile\n```\n\n```text\n'is' | 'is' 'not'\n```\n\n```text\n'is' notOpt\n```\n\n```text\nnotOpt: 'not' | ϵ\n```\n\n```text\n'is' 'not'?\n```\n\n```text\n'is' ['not']\n```\n\n========================================\n\nComments:\n- @PatrickHaugh I don't think it's fair to label this a duplicate when neither the linked question nor answer address OP's specific concerns regarding `is not` and `test`.\n- Doesn't the `(1)` mean the parser has one token of lookahead? So if it's \"on\" the `test`, it can look at the next token (but no farther). (My compilers class was many moons ago, so grain of salt)\n- @JETM @sepp2k Fair enough. I think this question boils down to a misunderstanding about the nature of `LL(1)` grammars/parsers though.\n- @PatrickHaugh It counts the current token as lookahead (if you can look at it without consuming it). So LL(1) means you can look at the current token only and LL(2) would mean that you can look one token further. LL(0) (which isn't really a thing) would mean that you can never look at a token without consuming it, so it wouldn't be possible to select an alternative based on the current token.\n- As per en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form , a EBNF describes a *language* rather than *grammar.* There's no 1-1 correspondence between a EBNF and the resulting grammar, there are multiple possible grammars for a given EBNF. So it only matters if some of them can be LL(1).\n- Just an additional thought from Wikipedia: `A ε-free LL(1) grammar is also a SLR(1) grammar.` So Python is not SLR(1)\n- stackoverflow.com/questions/53596580/&hellip;\n- \"Similarly, the entire EBNF can be transformed into an LL(1) grammar.\" How do we know this is true? Is there a theoretical result about EBNF grammars in general, or is it just true for Python's grammar specifically (based on the fact that Python's parser generator handles the grammar without errors)?\n- @user200783: it's specifically about Python. It is certainly not true of all grammars. And yes, the demonstration is being able to produce a one-token lookahead LL- parser.","metadata":{"transformedAt":"2026-08-18T18:32:17.805Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":23,"totalLines":163,"estimatedTokens":1138}}521{"id":"stack-36920427","source":"stackoverflow","questionId":36920427,"title":"How is match implemented in a language like Rust?","tags":["compiler-construction","language-design"],"text":"Title: How is match implemented in a language like Rust?\nTags: compiler-construction, language-design\nSource: Stack Overflow\n\nQuestion:\nI'm not a functional programmer. So I'm not very familiar with pattern matching, patterns, or any of that stuff. To me I only understand the concept of the good old `switch` statement.\n\nHow would a compiler implement a match statement? What exactly is the difference between match and a switch? There's a GNU C99 extension that allows you to have ranges in cases of a switch, is there a difference between:\n\n```\nmatch x {\n 0 ... 9 => ...,\n _ => ...,\n}\n```\n\nand \n\n```\nswitch (x) {\ncase 0 ... 9: ...; break;\ndefault: ...; break;\n}\n```\n\nNote that the second snippet is a simple C switch with this GNU extension.\n\n========================================\n\nCode:\n```text\nmatch x {\n    0 ... 9 => ...,\n    _ => ...,\n}\n```\n\n```text\nswitch (x) {\ncase 0 ... 9: ...; break;\ndefault: ...; break;\n}\n```\n\n```text\nswitch\n```\n\n```text\nenum Result {\n  SingleResult(i32),\n  TwoResults(i32, i32),\n  Error\n}\n\nmatch someResult {\n  Result::SingleResult(res) => f(res),\n  Result::TwoResults(res1, res2) => g(res1, res2),\n  Result::Error => error()\n}\n```\n\n```text\nstruct Result {\n  enum {\n    SingleResult, TwoResults, Error\n  } tag;\n  union {\n    struct {\n      int arg1;\n    } singleResult;\n    struct {\n      int arg1;\n      int arg2;\n    } twoResults;\n  } value;\n};\n\nswitch(someResult.tag) {\n  case SingleResult: {\n    int res = someResult.value.singleResult.arg1;\n    f(res);\n    break;\n  }\n  case TwoResults: {\n    int res1 = someResult.value.twoResults.arg1;\n    int res2 = someResult.value.twoResults.arg2;\n    g(res1, res2);\n    break;\n  }\n  case Error: {\n    error();\n    break;\n  }\n}\n```\n\n========================================\n\nComments:\n- That makes perfect sense. Thanks!\n- do you have any references or relevant source code to back this up?\n- @TylerDavis No, but I did look at the generated code to verify.","metadata":{"transformedAt":"2026-08-18T18:32:17.805Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":5,"totalLines":103,"estimatedTokens":484}}522{"id":"stack-40127664","source":"stackoverflow","questionId":40127664,"title":"Why Do Compilers Insert INT3 Instructions Between Subroutines?","tags":["assembly","compiler-construction","x86"],"text":"Title: Why Do Compilers Insert INT3 Instructions Between Subroutines?\nTags: assembly, compiler-construction, x86\nSource: Stack Overflow\n\nQuestion:\nWhile debugging some software I noticed INT3 instructions are inserted in between subroutines in many cases. \n\nhttps://i.sstatic.net/0tQMl.png\n\nI assume these are not technically inserted 'between' functions, but instead after them, in order to pause execution if a subroutine does not execute `retn` at the end for whichever reason.\n\nAre my assumptions correct? What's the purpose of these instructions, if not?\n\n========================================\n\nTop Answer:\nIncorrect assumptions.\n\nThey're padding between functions, not after. And a CPU that randomly decides to skip instructions is broken and should be thrown away.\n\nThe reason for `INT 3` is twofold. It's a single-byte instruction, which means you can use it even if there's just a single byte of space. The vast majority of instructions is unsuitable because they're too long. Furthermore, it's the \"debug break\" instruction. This means a debugger can catch the attempt to execute code between functions. That's not caused by ignoring `retn`, but for more simple reasons such as using an uninitialized function pointer.\n\n========================================\n\nCode:\n```text\nretn\n```\n\n```text\n.o\n```\n\n```text\njmp [rsp]\n```\n\n```text\nadd rsp, 8\n```\n\n```text\nINT 3\n```\n\n```text\nretn\n```\n\n```text\nint3\n```\n\n```text\nret\n```\n\n========================================\n\nComments:\n- It most certainly is empty space between functions. A very basic x86 optimization is to have functions start at an address that is a multiple of 16. If you have to come up with *some* byte value to fill up the gaps then 0xcc is by far the best choice. Catches the corner-case of a program jumping into oblivion.\n- It sometimes amazes me how billions of tiny transistors can work so reliably that it's safe to write loop conditions like `dec &#47; jnz` (`do{}while(--i)`) instead of `dec &#47; jg` (`do{}while(--i > 0)`). I guess it would be \"safer\" to write code that might still work if a bit flipped in the counter, but apparently it's not necessary. (And of course, a flipped bit inside an out-of-order execution CPU is unlikely to simply flip a bit in the architectural state; more likely you'll get something more weird.)\n- Since it should never executed, in theory `0x00` would be fine. But in practice, CPUs just decode bytes as x86 instructions without knowing where function boundaries are, so having the padding between functions be valid instructions that won't slow the CPU down while decoding (or speculatively executing) is also an advantage. But good point that INT3 causes early / noisy failure in the rare case where an indirect jump or corrupted return address takes you into the padding; that's probably usually better than silently falling into the next function with NOP padding (like is typical on Linux).\n- Minor comment: `int3` is one byte, `int 3` is two bytes. Both instructions behave slightly different.\n- You'd hope that the speculative execution stops at the `INT 3` so it blocks speculative execution of the next function. Executing the prologue of the next function probably is mostly harmless, but a waste.\n- @Peter Cordes, along a similar topic, why does branch prediction continue to decode instructions after an unconditional branching instruction is encountered (i.e. RET, JMP)? Many times you'd expect these bytes to be garbage and padding and thus a waste of cycles.\n- @byteptr: That's not branch prediction, that's just parallel decoding. Instruction-length marking happens in blocks of 16B in Intel CPUs, before any of the real decoders look at the block and detect an unconditional branch.\n- @MSalters: Yes, IIRC `INT` is a serializing instruction, or at least will stop speculative execution.\n- UD2 and INT3 are probably similar in stopping speculation. Fog doesn't have much to say on this (that I could find) but from the Intel Optimization Reference Manual: *Assembly/Compiler Coding Rule 14. (M impact, L generality) When indirect branches are present, try to put the most likely target of an indirect branch immediately following the indirect branch. Alternatively, if indirect branches are common but they cannot be predicted by branch prediction hardware, then the indirect branch with a UD2 instruction, which will stop the processor from decoding down the fall-through path.*\n- BTW, does this speculation only happen from the decoders? Once the branch has been decoded+cached in the ustore wouldn't it also have a (non-default) prediction? Also, I should add that UB2 is 2 bytes and INT3 is a single byte.\n- @Olsonist: I expect that it's possible for an indirect branch to still be hot in the uop cache but have had its target-prediction data evicted from the BTB by other branches that are exactly 4k away or something. My guess is that Intel's manual should have said \"stop the processor from *speculating* down the fall-through path\", not \"decoding\". Thanks for digging up that quote, that's exactly what I was talking about.\n- Thanks. BTW, the best info I can find on BTB size+ways is from Godbolt, *Inside the Ivy Bridge and Haswell BTB*, link 4096 entries 4x1024.\n- In your `ret` effects example you used `add rsp` but `add` would have an effect on the arithmetic flags. May I suggest `lea` ?\n- @ecm: I'd rather just write \"without modifying FLAGS\" instead of using a more obfuscated `lea rsp, [rsp+8]` that will take beginners longer to understand. But sure, I sympathize with `add` seeming sloppy to some readers so I also linked a Q&A about actually emulating `ret`.","metadata":{"transformedAt":"2026-08-18T18:32:17.805Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":74,"estimatedTokens":1404}}523{"id":"stack-7186623","source":"stackoverflow","questionId":7186623,"title":"Where exactly is the boundary between a preprocessor and a compiler?","tags":["c++","compiler-construction","preprocessor","terminology"],"text":"Title: Where exactly is the boundary between a preprocessor and a compiler?\nTags: c++, compiler-construction, preprocessor, terminology\nSource: Stack Overflow\n\nQuestion:\nAccording to various sources (for example, the SE radio episode with Kevlin Henney, if I remember correctly), \"C with classes\" was implemented with preprocessor technology (with the output then being fed to a C compiler), whereas C++ has always been implemented with a compiler (that just happened to spit out C in the early days). This seems to cause some confusion, so I was wondering:\n\nWhere exactly is the boundary between a preprocessor and a compiler? When do you call a piece of software that implements a language \"a preprocessor\", and when do you call it \"a compiler\"?\n\nBy the way, is \"a compiled language\" an established term? If so, what exactly does it mean?\n\n========================================\n\nTop Answer:\nThe language of the output of the preprocessor is a subset of the language of the input.\n\nThe language of the output of the compiler is (usually) very different (machine code) then the language of the input.\n\n========================================\n\nCode:\n```text\ncfront\n```\n\n```text\nC++11\n```\n\n========================================\n\nComments:\n- Regarding \"compiled language\": It's used, but rarely accompanied by a definition and frequently by people who aren't aware that there is non-AOT compilation and compilation to targets other than machine code, or have a too restrictive definition of compilation that excludes the above - I wouldn't take texts using the term without defining it too serious ;)\n- @Fred & Janusz: Hence \"usually\"\n- What about dynamic language compilers?\n- @delnan: Do you mean script interpreters? They're not compilers, are they?\n- No. Compilers. A compiler is simply a program translating from on programming language to another (or, if you feel like being particular restrictive, a lower-level one). Nobody ever said anything about type checking etc. - it's a check that some languages require, but it doesn't decide if a program is a compiler. Most widely-used dynamically-typed languages are compiled (to bytecode, but again, the exact output format doesn't matter). There are even dozens of compilers (mostly for Lisp dialects) that compile to machine code (and Lisp is dynamically typed too).","metadata":{"transformedAt":"2026-08-18T18:32:17.805Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":37,"estimatedTokens":582}}524{"id":"stack-185584","source":"stackoverflow","questionId":185584,"title":"Dynamic (?) parser","tags":["parsing","compiler-construction","interpreter","lex"],"text":"Title: Dynamic (?) parser\nTags: parsing, compiler-construction, interpreter, lex\nSource: Stack Overflow\n\nQuestion:\nDoes there exist a parser that generates an AST/parse tree at runtime? Kind of like a library that would accept a string of EBNF grammar or something analogous and spit out a data structure? \n\n- I'm aware of antlr, jlex and their ilk. They generate source code which could do this. (like to skip the compile step)\n\n- I'm aware of Boost::Spirit, which uses some black magic with C++ syntax to generate such things at execution time (definitely much closer to what I want, but I'm a wuss when it comes to C++. And it's still somewhat limiting, because your grammar is hardcoded)\n\n- I'm not aware of anything in python or ruby, although a compiler compiler might very well be effective in such a language...\n\nNow I'm aware of parser combinators. (thanks, Jonas) And some libraries (thanks eliben)\n incidentally, I also noticed Parsing Expression Grammars lately, which sounds cool were someone to implement it (they say Perl 6 will have it, but Perl evades my understanding)\n\n========================================\n\nTop Answer:\nIf Java is better for you, there is a port of the Haskell Parsec library - JParsec. Very powerful, though documentation isn't great. \n\nYou can coerce it to do a straight forward lex then parse phase, but you can do some interesting things with dynamic lexing and dynamic grammars.\n\nHead twisting stuff. \n\nBecause it's all in Java (your Parser is a POJO), you can refactor, and do TDD, and whatever you're used to doing in Java. This is a major advantage to a more traditional ANTLR/JavaCC/JJTree approach.\n\n========================================\n\nCode:\n```text\n{syntax: while (condition) do code}\nwhile (condition, code) => // actual execution\n```\n\n========================================\n\nComments:\n- I think that you're interested in parsers, not just lexical analyzers. Generally, a lexical analyzer produces a stream a tokens from a stream of input characters. A parser creates an AST from a stream of tokens. You might consider changing the wording of your question to get to the right audience.\n- ayup. don't know what I'm talking about. parsers. does make more sense that way.\n- I'm interested in your Lex&Yacc solution. Is there a reference for the steps you mentioned?\n- Python indenting meets html? Not a bad idea.","metadata":{"transformedAt":"2026-08-18T18:32:17.805Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":42,"estimatedTokens":593}}525{"id":"stack-20355112","source":"stackoverflow","questionId":20355112,"title":"Why do we use intermediate languages instead of AST?","tags":["optimization","gcc","compiler-construction","abstract-syntax-tree"],"text":"Title: Why do we use intermediate languages instead of AST?\nTags: optimization, gcc, compiler-construction, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nWhat is the difference between an intermediate language and an AST? As far as I can tell, they both offer flow analysis information which a compiler can use for optimization purposes. I know GCC uses two intermediate representations - an AST and an IL. What is the reason for this?\n\n========================================\n\nTop Answer:\nDifferent representations permit different optimizations.\n\nAn AST is a type of intermediate representation, it's not the string you typed and it's not machine code. AST's are useful for some optimizations\n\n- Constant folding\n\n- Some Inlining\n\n- Rewrite rules\n\nBut it's horrible for some other things, for example, imagine trying to figure out register spilling in an AST, or in the machine code itself? A compiler is usually structured as a pipeline, each step with it's own IL and it's own set of tasks to perform.\n\nThis way, each IL can be custom fitted to be easy to compile from the previous IL and easy to optimize in whatever way. GCC for example IIRC has an IL that's basically like assembly, this is great for doing register-based optimizations, like jiggering with what's loaded when. And this is also trivial to turn into real assembly or just straight machine code.\n\nGCC is composed of many of these little ILs, they exist only as datastructures in the compiler and are created, messed with a bit, and then compiled to a lower level IL.\n\n========================================\n\nComments:\n- When did GCC begin using an IL? I have been scouring Google trying to find a date because I am trying to figure out when compiler developers began utilizing AST/IR structures to perform optimization. Any ideas?\n- @RouteMapper Forever, no one sane tries to go straight from C to Assembly with optimizations, it's just too hard to understand and incredibly brittle.\n- I can't find any literature which explicitly says that. I know you're right, but I need a citation.\n- @RouteMapper: That's because people have been doing this in compilers since the early 1960s (if not before) when hardly anything was documented; people were too busy getting stuff to work. These days it is an axiom that you do it, and nobody questions the axiom. If you want a citation, start digging in the earliest compiler papers you can find and chase the references backwards. I'd suggest checking IBM Journals.","metadata":{"transformedAt":"2026-08-18T18:32:17.805Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":33,"estimatedTokens":622}}526{"id":"stack-17219697","source":"stackoverflow","questionId":17219697,"title":"Why some compilers optimize if(a>0) and not if(*(&a)>0)?","tags":["c","optimization","compiler-construction","rvds"],"text":"Title: Why some compilers optimize if(a>0) and not if(*(&a)>0)?\nTags: c, optimization, compiler-construction, rvds\nSource: Stack Overflow\n\nQuestion:\nLet's say I have declared in the global scope:\n\n```\nconst int a =0x93191;\n```\n\nAnd in the main function I have the following condition:\n\n```\nif(a>0)\n do_something\n```\n\nAn awkward thing I have noticed is that the `RVDS` compiler will drop the `if` statement and there is no branch/jmp in the object file.\n\n**but** If I write:\n\n```\nif(*(&a)>0)\n do_something\n```\n\nThe if (`cmp` and `branch`) will be in the compiled object file.\n\nIn contrast, `GCC` do optimizes both with (`-O1` or `-O2` or `-O3`) :\n\n```\n#include \nconst a = 3333;\n\nint main()\n{\n if (a >333)\n printf(\"first\\n\");\n\nreturn 0;\n}\n```\n\n**compiled with -O3:**\n\n```\n(gdb) disassemble main\nDump of assembler code for function main:\n0x0000000100000f10 : push %rbp\n0x0000000100000f11 : mov %rsp,%rbp\n0x0000000100000f14 : lea 0x3d(%rip),%rdi # 0x100000f58\n0x0000000100000f1b : callq 0x100000f2a \n0x0000000100000f20 : xor %eax,%eax\n0x0000000100000f22 : pop %rbp\n0x0000000100000f23 : retq \nEnd of assembler dump.\n```\n\nAnd for\n\n```\n#include \nconst a = 3333;\n\nint main()\n{\n if (*(&a) >333)\n printf(\"first\\n\");\n\nreturn 0;\n}\n```\n\nwill give:\n\n```\n(gdb) disassemble main\nDump of assembler code for function main:\n0x0000000100000f10 : push %rbp\n0x0000000100000f11 : mov %rsp,%rbp\n0x0000000100000f14 : lea 0x3d(%rip),%rdi # 0x100000f58\n0x0000000100000f1b : callq 0x100000f2a \n0x0000000100000f20 : xor %eax,%eax\n0x0000000100000f22 : pop %rbp\n0x0000000100000f23 : retq \nEnd of assembler dump.\n```\n\n***GCC treat both as same (as should be) and RVDS doesn't ?***\n\nI tried to examine the affect of using `volatile` and in the RVDS it did drop the the `if(a>333)` but gcc didn't:\n\n```\n#include \nvolatile const a = 3333;\n\nint main()\n{\n if (a >333)\n printf(\"first\\n\");\n\nreturn 0;\n}\n\n(gdb) disassemble main\nDump of assembler code for function main:\n0x0000000100000f10 : push %rbp\n0x0000000100000f11 : mov %rsp,%rbp\n0x0000000100000f14 : cmpl $0x14e,0x12a(%rip) # 0x100001048 \n0x0000000100000f1e : jl 0x100000f2c \n0x0000000100000f20 : lea 0x39(%rip),%rdi # 0x100000f60\n0x0000000100000f27 : callq 0x100000f36 \n0x0000000100000f2c : xor %eax,%eax\n0x0000000100000f2e : pop %rbp\n0x0000000100000f2f : retq \nEnd of assembler dump.\n```\n\nProbably there are some bugs in the compiler version I used of RVDS.\n\n========================================\n\nTop Answer:\nThe level of complexity the compiler will go through to find out \"is this something I can figure out what the actual value is\", is not unbounded. If you write a sufficiently complex statement, the compiler will simply say \"I don't know what the value is, I'll generate code to compute it\". \n\nThis is perfectly possible for a compiler to figure out that it's not going to change. But it's also possible that some compilers \"give up\" in the process - it may also depends on where in the compilation chain this analysis is done. \n\nThis is probably a fairly typical example of \"as-if\" rule - the compiler is allowed to perform any optimisation that generates the result \"as-if\" this was executed. \n\nHaving said all that, this should be fairly trivial (and as per comments, the compiler should consdier `*(&a)` the same as `a`), so it seems strange that it then doesn't get rid of the comparison.\n\n========================================\n\nCode:\n```text\nconst int a =0x93191;\n```\n\n```text\nif(a>0)\n    do_something\n```\n\n```text\nif(*(&a)>0)\n    do_something\n```\n\n```text\n#include <stdio.h>\nconst a = 3333;\n\nint main()\n{\n    if (a >333)\n        printf(\"first\\n\");\n\nreturn 0;\n}\n```\n\n```text\n(gdb) disassemble main\nDump of assembler code for function main:\n0x0000000100000f10 <main+0>:    push   %rbp\n0x0000000100000f11 <main+1>:    mov    %rsp,%rbp\n0x0000000100000f14 <main+4>:    lea    0x3d(%rip),%rdi        # 0x100000f58\n0x0000000100000f1b <main+11>:   callq  0x100000f2a <dyld_stub_puts>\n0x0000000100000f20 <main+16>:   xor    %eax,%eax\n0x0000000100000f22 <main+18>:   pop    %rbp\n0x0000000100000f23 <main+19>:   retq   \nEnd of assembler dump.\n```\n\n```text\n#include <stdio.h>\nconst a = 3333;\n\nint main()\n{\n        if (*(&a) >333)\n                printf(\"first\\n\");\n\nreturn 0;\n}\n```\n\n```text\n(gdb) disassemble main\nDump of assembler code for function main:\n0x0000000100000f10 <main+0>:    push   %rbp\n0x0000000100000f11 <main+1>:    mov    %rsp,%rbp\n0x0000000100000f14 <main+4>:    lea    0x3d(%rip),%rdi        # 0x100000f58\n0x0000000100000f1b <main+11>:   callq  0x100000f2a <dyld_stub_puts>\n0x0000000100000f20 <main+16>:   xor    %eax,%eax\n0x0000000100000f22 <main+18>:   pop    %rbp\n0x0000000100000f23 <main+19>:   retq   \nEnd of assembler dump.\n```\n\n```text\n#include <stdio.h>\nvolatile const a = 3333;\n\nint main()\n{\n    if (a >333)\n        printf(\"first\\n\");\n\nreturn 0;\n}\n\n(gdb) disassemble main\nDump of assembler code for function main:\n0x0000000100000f10 <main+0>:    push   %rbp\n0x0000000100000f11 <main+1>:    mov    %rsp,%rbp\n0x0000000100000f14 <main+4>:    cmpl   $0x14e,0x12a(%rip)        # 0x100001048 <a>\n0x0000000100000f1e <main+14>:   jl     0x100000f2c <main+28>\n0x0000000100000f20 <main+16>:   lea    0x39(%rip),%rdi        # 0x100000f60\n0x0000000100000f27 <main+23>:   callq  0x100000f36 <dyld_stub_puts>\n0x0000000100000f2c <main+28>:   xor    %eax,%eax\n0x0000000100000f2e <main+30>:   pop    %rbp\n0x0000000100000f2f <main+31>:   retq   \nEnd of assembler dump.\n```\n\n```text\nRVDS\n```\n\n```text\nif\n```\n\n```text\ncmp\n```\n\n```text\nbranch\n```\n\n```text\nGCC\n```\n\n```text\n-O1\n```\n\n```text\n-O2\n```\n\n```text\n-O3\n```\n\n```text\nvolatile\n```\n\n```text\nif(a>333)\n```\n\n```text\na\n```\n\n```text\na\n```\n\n```text\na\n```\n\n```text\n*(&a)\n```\n\n```text\na\n```\n\n========================================\n\nComments:\n- What compiler and what compiler options?\n- @0x90 `armvct` I haven't heard of that compiler before? Can you post the link so that we can better mock them?\n- GCC 4.5.3 optimizes out the branch in both cases at `-O1` and higher for me.\n- @Mikhail I meant `rvds`, sorry for the typo.\n- I understand the answer, but this question isn't making much sense to me - it says there is a failure to optimize, then both disassemblies produce the same output - then the last bold line. Further, the compiler mentioned in the comments isn't what is being shown in the question. what is going on?\n- The gcc example shown that if you enable optimization it will drop both `if(a)` and `if(*(&a))`, but in rvds It drop only the first one and the second one he left as his. That is the awkward thing. why rvds doesn't treat both as identical and gcc does ?\n- Seems like the conclusion is that \"it's a compiler bug\" - despite my answer.\n- Why are you comparing an *ARM* assembler to an *x86-gcc*? Did you try `static const int a =0x93191;`? It is possible for something to change this depending on your platform. See const volatile. Making it *static* should make it clear to the compiler that it can be optimized.\n- Why you don't publish ARM assembly dump as well? Where is the output from RVDS? This doesn't seem to relate to ARM except the word RVDS.\n- @artlessnoise why noting it as static will make it clear to compiler it can be optimized ?\n- Another module can take the address of the `const` and change it. In C++, there is no global `const`, but in *C* there is. Ie, the compiler doesn't have to allocate space for a variable. For instance, a `const` hardware register may mean it is read-only to software; the behaviour makes more sense with `extern const`. Some people may conclude by reading the standard that a compiler should treat these the same. As the compiler needs to optimize for all these cases, the information `(*(&a))` may not make it through to the optimizer phase, especially as you gave the variable global scope.\n- @artlessnoise can you add your thoughts as an answer ? thanks\n- Undoubtedly this is a failure of the compiler to recognize an optimization opportunity, but it is not as simple as crossing some threshold of complexity in the compiler. `*(&a)` is not only a very simple expression, but it is explicitly called out in a footnote in the C standard as equal to `a` (Note 83 in the 1999 standard, 84 in 1999 TC2 draft n1124, 102 in 2011). We know this compiler optimizes `a > 0` given the visible value of `a`. So the fact that it fails to optimize `*(&a) > 0` suggests it has missed a clear and explicitly stated aspect of the C semantics.\n- I am down-voting this post because something else is going on. No reasonable compiler would quit.\n- @EricPostpischil: In C++ it is not exactly the same. Integral constants are slightly different in C and C++. In the case of C++, `a` does not constitute *odr-use* of the constant `a`, while `*&a` is *odr-use*, for example. While in the example code this does not matter too much (the symbol **is** defined), it could matter for static members.\n- @Mikhail A *reasonable* compiler may give up as the variable is not **static**.\n- @EricPostpischil What the standard says is equivalent doesn't apply to the optimizer. Your statement *We know this compiler optimizes..*, doesn't mean anything when put together with the standard. This seems like a non sequitur, the context of the standards seems to be in relation to the `*` operator and doesn't imply anything about code generation.\n- @artlessnoise: The job of the optimizer is to take advantage of anything it can to improve program execution (or space, depending on the optimization goal). If there is a simple, well-documented property and the optimizer fails to take advantage of it, that is a shortcoming of the optimizer. In other words, the fact that `*&a` and `a` are equivalent does not compel the optimizer to reduce `*&a` to `a`, but that fact combined with the specification that the optimizer should optimize implies it should. In fact, this simple reduction should be performed early in the semantic processing.\n- @EricPostpischil Sure. Your previous comment seems to say that it is mandated by the standard. I think we agree; mainly. This applies well to compilers for computing environments. It might not be true that a `const *` will not change ever. A compiler need not even allocate `static const a = 1;`, but it has to have an address if we have `const a;` and take pointers to this. Ie, there is a real physical address. Depending on the tools, some one *could* alter this physical address by having it `const` in one module and not in another; that maybe non-standard, but supported.\n- @artlessnoise: What does “non-standard, but supported” mean? The C standard does not support defining an object to be const in one place but declaring it non-const in another. Do you mean this particular C implementation supports it?\n- @EricPostpischil I mean not all code is 'C'. That is what you open the compiler up to when you don't use `static`.","metadata":{"transformedAt":"2026-08-18T18:32:17.806Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":23,"totalLines":311,"estimatedTokens":2699}}527{"id":"stack-4352425","source":"stackoverflow","questionId":4352425,"title":"What should I learn first before heading to C++?","tags":["c++","c","design-patterns","data-structures","compiler-construction"],"text":"Title: What should I learn first before heading to C++?\nTags: c++, c, design-patterns, data-structures, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm learning C, but after that or in the meanwhile, what should I learn first and subsequently before getting into C++ ? Compilers, Data Structures, UML or Design Patterns ?(also when start to learn Win32 API ?) I'm not in a hurry at all, so I can grasp the requirements from the deepest beginning. I just don't want to get lost for being cursory and negligent.\n\nIn addition to this, which subjects on mathematics has most impact on coding very well ? Linear Algebra, Discrete Mathematics, Calculus ?\n\nI'll be thankful if someone guides me through this journey. There are many questions I would like to know the answers. Thanks.\n\n========================================\n\nTop Answer:\nCompilers, Data Structures, UML or Design Patterns ?\n\nData Structures and Algorithms.\n\n In addition to this, which subjects on mathematics has most impact on coding very well ? Linear Algebra, Discrete Mathematics, Calculus ?\n\nDiscrete Mathematics and Number Theory.\n\n========================================\n\nComments:\n- As absurdly as it might sound, but learning what's nowadays considered good C++ is usually hindered by experience in C. If you want to learn C++ then skip C. If you want to learn both, be prepared to having to unlearn a lot of C in order to write good C++.\n- Could you elaborate on `Do not require OS-support`, `Are simpler, and easier to implement` and why C is a good stepping stone to C++ when good C code is completely different from good C++ code?\n- Also, I think `First you should learn that C++ is not a superior language` is pure POV. I would generalise that to `Whether a language is superiour always and at any time totally depends on what you like and want to achieve`. E.g., Haskell is totally superiour to C because you can write many complex algorithms within 2 or 3 lines of code. At the same C is superiour to C++ because it is available for more platforms and it is easier to hire C programmers. C++ is superior to Haskell because on average it leads to faster code.\n- You certainly learn stuff about the computer, and your operating system however. I would argue this is equally as important. Programmers stuck in user space with a spoon fed platform are not particularly useful.","metadata":{"transformedAt":"2026-08-18T18:32:17.806Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":29,"estimatedTokens":591}}528{"id":"stack-22733132","source":"stackoverflow","questionId":22733132,"title":"Purpose for .class files in Java","tags":["java","compiler-construction","jvm","interpreter"],"text":"Title: Purpose for .class files in Java\nTags: java, compiler-construction, jvm, interpreter\nSource: Stack Overflow\n\nQuestion:\nI am trying to understand how .class files work in java and what's their purpose. I found some information online, but I get unsatisfying explanations. \n\nAs soon as we run the compiler we get the .class file, which is bytecode. Is this machine readable or not? And if not, this is why we need the interpreter for the program to run successfully? \n\nAlso, since the .class file is the equivalent of our .java programs, why can't somebody run a java program straight away by just running the .class file using VM and they would need to have the .java file as well?\n\n========================================\n\nTop Answer:\nThe `.class` file is machine-readable. The machine that reads it is the Java Virtual Machine, which interprets it and compiles it to native code (executable by your computer).\n\nYou don't need the `.java` files to run Java code. The `.class` files are all you need.\n\n========================================\n\nCode:\n```text\n.class\n```\n\n```text\n.java\n```\n\n```text\n.class\n```\n\n```text\n.class\n```\n\n```text\n.java\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.806Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":40,"estimatedTokens":289}}529{"id":"stack-7431588","source":"stackoverflow","questionId":7431588,"title":"Why isn't an enum checked by the C compiler?","tags":["c","compiler-construction","enums"],"text":"Title: Why isn't an enum checked by the C compiler?\nTags: c, compiler-construction, enums\nSource: Stack Overflow\n\nQuestion:\nThe following text is an excerpt from *C Programming Language, 2nd Edition*, written by the creator of the C language (so I presume it is correct):\n\n Although variables of enum types may be declared, compilers need not check that what you store in such a variable is a valid value for the enumeration.\n\nI have some doubts:\n\n- For what cases in the C language doesn't the compiler check the value of an `enum`?\n\n- `enum` constants are not checked for some reason. Why not? What are the reasons?\n\n- Since `enum` is not checked by the compiler, is using `enum` error-prone? Please explain.\n\n========================================\n\nTop Answer:\nYou can do a enum like\n\n```\nenum status {\n ST_READY = 1 Then define an object of that type\n\n```\nenum status status;\n```\n\nand set it to the bitwise OR of some 'simple' statuses\n\n```\nstatus = ST_WAIT | ST_ERROR; /* recoverable error */\n```\n\nNote that the value `ST_WAIT | ST_ERROR` is `6` and that that value is not part of the enum.\n\nTo answer your questions:\n\n- C compiler lets the programmer shoot himself in the foot.\n\n- C compiler lets the programmer shoot himself in the foot.\n\n- C compiler lets the programmer shoot himself in the foot.\n\n========================================\n\nCode:\n```text\nenum\n```\n\n```text\nenum\n```\n\n```text\nenum\n```\n\n```text\nenum\n```\n\n```text\nenum status {\n    ST_READY = 1 << 0, /*  1 */\n    ST_WAIT  = 1 << 1, /*  2 */\n    ST_ERROR = 1 << 2, /*  4 */\n    ST_HALT  = 1 << 3, /*  8 */\n    ST_ETC   = 1 << 4, /* 16 */\n};\n```\n\n```text\nenum status status;\n```\n\n```text\nstatus = ST_WAIT | ST_ERROR; /* recoverable error */\n```\n\n```text\nST_WAIT | ST_ERROR\n```\n\n```text\n6\n```\n\n```text\nenum E { A, B } x;\nx = 10000;\n```\n\n```text\nenum E { END_OF_EVERYTHING = 5 };\n```\n\n```text\nenum E { A = 1, B = -1 };\n```\n\n```text\nswitch\n```\n\n========================================\n\nComments:\n- Someone thought this was a duplicate of /questions/432937/net-why-arent-enums-range-value-checked. They probably changed their mind (the answer's the same, but the question is about a different language after all), but it's not possible to remove a close vote.\n- +1 C just assumes you know what you are doing, and lets you get on with it in as efficient way as possible - also :g/foot/s//head/\n- @asc99c: I'd do that as `:%s&#47;foot&#47;head&#47;g`\n- @pmg Can you say `1 << 0` what value this result in?\n- @Ant's: any value shifted left (or right) `0` bits is the initial value itself. I just use it for simmetry with the other lines.\n- The other great thing about `enum`s is that some compilers warn you if you have a `switch` block that switches on an `enum`, and you leave one out of the `case` clauses.\n- I consider that an annoying thing about some compilers, not a great thing about `enum`. It's very possible and even likely that a certain `switch` only handles a subset of possible `enum` values by virtue of earlier logic having ruled out other values...\n- @R..: In that *ahem* case, you can just add `default: &#47;* error handlign code *&#47;` to silence the warning.\n- in this code `enum E { A = 1, B = -1 };`, how could you say the values are -2,-1,0,1?","metadata":{"transformedAt":"2026-08-18T18:32:17.806Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":120,"estimatedTokens":809}}530{"id":"stack-26910493","source":"stackoverflow","questionId":26910493,"title":"What's the difference among Expression,Statements and Declaration from the view of compiler?","tags":["compiler-construction","go"],"text":"Title: What's the difference among Expression,Statements and Declaration from the view of compiler?\nTags: compiler-construction, go\nSource: Stack Overflow\n\nQuestion:\nI am going through the Go source code of ast.go at here,and there are 3 types of interfaces that are Expression,Statement and Declaration. But only with the source code I couldn't figure out the difference between them.What I could figure out is that expression results in a object that could be assigned or compared or used as parameter,while statements are some flow control like if-else or for loop.\n But I found some definitions like \n\n```\n// An IncDecStmt node represents an increment or decrement statement.\n IncDecStmt struct {\n X Expr\n TokPos token.Pos // position of Tok\n Tok token.Token // INC or DEC\n }\n```\n\nshouldn't it be a expression?I feel confused how to distinguish expressions and statements,are there any rules?\n\n========================================\n\nTop Answer:\nThose are common terms in CS. An expression produces a value while a statement often doesn't (this last point depends of the language, in fact, some languages consider expressions as a subcategory of statements).\n\nFrom Wikipedia :\n\n In most languages, statements contrast with expressions in that\n statements do not return results and are executed solely for their\n side effects, while expressions always return a result and often do\n not have side effects at all.\n\nIn Go an expression may be used as statement.\n\nYour confusion seems to come from the increment and decrement operators. In Go, contrary to most C-based languages, the increment and decrement statements don't produce a value, they're statements and not expressions. That is, you may not write\n\n```\nb := a++\n```\n\n========================================\n\nCode:\n```text\n// An IncDecStmt node represents an increment or decrement statement.\n    IncDecStmt struct {\n            X      Expr\n            TokPos token.Pos   // position of Tok\n            Tok    token.Token // INC or DEC\n    }\n```\n\n```text\nIncDecStmt = Expression ( \"++\" | \"--\" ) .\n```\n\n```text\ni := 0 # int8? uint8? int16? ...\n```\n\n```text\nIncDecStmt\n```\n\n```text\nbool\n```\n\n```text\nrune\n```\n\n```text\nint\n```\n\n```text\nfloat64\n```\n\n```text\ncomplex128\n```\n\n```text\nstring\n```\n\n```text\nb := a++\n```\n\n========================================\n\nComments:\n- I think you mean \"statements and not expressions\"?\n- what is an untyped contstant?\n- @MargachChris I have edited the answer to address your question.","metadata":{"transformedAt":"2026-08-18T18:32:17.806Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":97,"estimatedTokens":620}}531{"id":"stack-3053904","source":"stackoverflow","questionId":3053904,"title":"Is a C++ compiler allowed to emit different machine code compiling the same program?","tags":["c++","compiler-construction","compilation"],"text":"Title: Is a C++ compiler allowed to emit different machine code compiling the same program?\nTags: c++, compiler-construction, compilation\nSource: Stack Overflow\n\nQuestion:\nConsider a situation. We have some specific C++ compiler, a specific set of compiler settings and a specific C++ program.\n\nWe compile that specific programs with that compiler and those settings two times, doing a \"clean compile\" each time.\n\nShould the machine code emitted be the same (I don't mean timestamps and other bells and whistles, I mean only real code that will be executed) or is it allowed to vary from one compilation to another?\n\n========================================\n\nTop Answer:\nThere is no guarantee that they will be the same. Also according to http://www.mingw.org/wiki/My_executable_is_sometimes_different \n\n **My executable is sometimes different, when I compile and recompile the same source. Is this normal?**\n\n \n Yes, by default, and by design, ~MinGW's GCC does not produce ConsistentOutput, unless you patch it.\n\nEDIT: Found this post that seems to explain how to make them the same.\n\n========================================\n\nComments:\n- There was an answer by @Neil Butterworth about the same issue. IIRC, he explains why a compiler could produce different output even if everything *seems* equal. I am trying to find it :)\n- This almost sounds like a trick question ;)\n- if the compiler uses statistical algorithms in some cases, then yes it could produce slightly different code, ie use alternative registers or code layout.\n- Are you comparing binaries or assembler code?\n- Possible dupe: stackoverflow.com/questions/1221185/&hellip;\n- @Moron: actually not. That question specifies that function order in the produced binary differs between compiles - that's not what bothers me.\n- Does the standard say anything about the machine code? No? Well, then *any* output which preserves the semantics specified in the standard is allowed. Ergo: **Yes.**\n- Does your compiler have a setting to randomize function addresses to prevent targeted buffer overflow attacks?\n- @Martin Beckett: I see your point, but that's not exactly what I'm asking about. Yes, functions can be located at different addresses, but I'm asking about what is inside the functions.\n- @dmckee: I like this concise explanation. Shouldn't it be an answer instead of comment?\n- @sharptooth - I thought you might be simply diff'ing the executables produced by 2 builds\n- dmckee is absolutely right. However, I *despise* the word \"ergo.\"\n- What I would give for a non deterministic compiler that invented optimizations on the fly.\n- Visual Studio offers Profile Guided Optimization.\n- I followed the link, but there was no explanation of why!\n- @martin York updated my post with another link to make them the same.\n- For a number of purposes, it's useful to have compilers whose output is completely deterministic even if it's not optimal. For example, if one is compiling code for an open-source voting machine, one should use an open-source cross compiler which will always yield bit-identical output regardless of the environment in which the compiler itself is run. If versions of that compiler which are bootstrapped from source via several independent means all produce identical code, that would pretty strongly imply that the compiler didn't hide any \"gotchas\" which aren't in the source.","metadata":{"transformedAt":"2026-08-18T18:32:17.806Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":43,"estimatedTokens":841}}532{"id":"stack-4213107","source":"stackoverflow","questionId":4213107,"title":"Does it ever make sense for a compiler to pass a structure like this in a cpu register to a function?","tags":["c++","function","compiler-construction","parameter-passing","cpu-registers"],"text":"Title: Does it ever make sense for a compiler to pass a structure like this in a cpu register to a function?\nTags: c++, function, compiler-construction, parameter-passing, cpu-registers\nSource: Stack Overflow\n\nQuestion:\nI'd like to know if some kind of structure contains more than one primitive but its total size is less than or equal to size of a single cpu register like a 4-byte register, does it ever make sense for a compiler to put it in one of those 4-byte registers when passing it by value or reference to a function instead of making a copy of it on the callee stack or passing a pointer to it and in general when passing something more than a single primitive to a function like an array or an structure would passing in a cpu register ever come in handy?\n\nsample of such structure:\n\n```\nstruct sample{\n public:\n char char1;\n char char2;\n};\n```\n\nsample of passing the structure to a function:\n\n```\nvoid someFunc(const sample input){\n //whatever\n}\nvoid someFunc(sample input){\n //whatever\n}\nvoid someFunc(sample & input){\n //whatever\n}\nvoid someFunc(const sample & input){\n //whatever\n}\n```\n\n========================================\n\nTop Answer:\nThis is defined in the *application binary interface (ABI)* of your execution environment. The standard does not say anything about processor registers when a function is called, so it is legal to create an environment where small structs are packed into a single processor register.\n\nFor the reference part, they are very likely to be passed as pointers anyway, since when inside the called function the address of a reference is taken, it must resolve to the address of the referenced object.\n\n========================================\n\nCode:\n```text\nstruct sample{\n public:\n  char char1;\n  char char2;\n};\n```\n\n```text\nvoid someFunc(const sample input){\n //whatever\n}\nvoid someFunc(sample input){\n //whatever\n}\nvoid someFunc(sample & input){\n //whatever\n}\nvoid someFunc(const sample & input){\n //whatever\n}\n```\n\n```text\ntypedef struct {\n    int          a;\n    float        f;\n    char         c;\n} __attribute__ ((d64_abi)) Thingy;\n\nThingy foo( Thingy t );\n```\n\n```text\nstruct\n```\n\n========================================\n\nComments:\n- You might want to break down that tapeworm of a sentence. I have a very hard time digesting it.\n- I wish I knew which they were!\n- @Crashworks: Actually, I'd expect all mainstream compilers (VC, GCC) to do this.\n- Not so much, I'm afraid. For example, MSVC turns codepad.org/oJoKVbJb into codepad.org/PV7vpM8O : it passes a two-char struct by pushing each char onto the stack individually, even at \"full optimization.\" It does better when both Foo() and Bar() are statics in the same compilation unit and it can ignore the calling convention.\n- @Crashworks_but putting two chars in a single int register is pointless right?\n- @Pooria: Not at all -- it might be much faster than making two round-trips to the stack during a function call.\n- @Crashworks_I mean since the final intention is to do some operation on each of those two bytes in the structure in my question,they should be in separate registers and if they are residing in a single int register they should be separated later to do operations on each of them.\n- @Pooria: Sure -- it's up to your compiler and ABI whether you can have a \"pass this struct in consecutive registers\" attribute, a \"pack this struct onto one register\" attrib, neither, or both. On one chip, there might not be enough spare registers to pass more than one value; on another, you might be able to pass eight registers at once.","metadata":{"transformedAt":"2026-08-18T18:32:17.806Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":92,"estimatedTokens":890}}533{"id":"stack-20674854","source":"stackoverflow","questionId":20674854,"title":"Advantages of compiling a language vs Executing the AST as soon as it is constructed","tags":["compiler-construction","interpreter","abstract-syntax-tree","machine-code"],"text":"Title: Advantages of compiling a language vs Executing the AST as soon as it is constructed\nTags: compiler-construction, interpreter, abstract-syntax-tree, machine-code\nSource: Stack Overflow\n\nQuestion:\nWhat are the benefits/drawbacks of compiling a program to machine code instead of simply constructing the AST from the source and executing operations as you traverse the tree?\n\nAre there certain reasons you would want to do one over the other?\n\n========================================\n\nTop Answer:\nAnother advantage of compilation not mentioned yet is that it is often much easier than a direct ad hoc interpretation. Often an unprocessed source language is not very suitable for a direct interpretation, and dumbing it down to a simpler language will facilitate a much more efficient and straightforward interpretation.\n\nFor example, a language might feature a lexical scope, which would require a name lookup for each time a variable or a function argument is dereferenced. But a simple transformation pass which will enumerate the variables and insert implicit storage management constructions will make interpretation much simpler and much more efficient - an array access is a way much faster than a hash table with a text key. Another such example is closure handling - a lambda lifting pass makes is much simpler than any possible ad hoc approach.\n\nIt is also much easier to interpret a flat \"bytecode\" than a tree. There are many well known optimisation techniques (e.g., threaded code) for bytecode interpreters, while an AST walking interpreter is doomed to be dead slow.\n\nAnd, unless you have to do some heavy-weight optimisations (like dead code elimination, constant folding, register allocation, efficient instruction scheduling), compilation is extremely trivial and can be split into ridiculously obvious small steps. A straightforward interpretation of any non-trivial language, on the other hand, is always complicated and cannot be split into anything simple and obvious.\n\n========================================\n\nComments:\n- Where do get the 20 from (i.e. can you provide a citation)? I am curious, because the factor seems to largely differ: `w+x+y+z` -> `load, add, add, add`, but `(load (add (add (add))))` -> `load node, load value, add, load node, load value, add, load node, load value, add, load node, load value, add` with probably many cache misses. I have the feeling this would take much more time than 20x. On the other hand, `x=y` -> `load x, store y`, but `(store (load))` -> `load node, load value, load node, store value`, much less than 20x (or with the cache misses, more again).\n- @phresnel A fine question. I added some information to my post.","metadata":{"transformedAt":"2026-08-18T18:32:17.806Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":673}}534{"id":"stack-8675490","source":"stackoverflow","questionId":8675490,"title":"Why does JIT'ed code consume so much more memory than either compiled or interpreted code?","tags":["compiler-construction","programming-languages","jit","pypy"],"text":"Title: Why does JIT'ed code consume so much more memory than either compiled or interpreted code?\nTags: compiler-construction, programming-languages, jit, pypy\nSource: Stack Overflow\n\nQuestion:\nCompiled code such as `C` consumes little memory.\n\nInterpreted code such as `Python` consumes more memory, which is understandable.\n\nWith JIT, a program is (selectively) compiled into machine code at run time. So shouldn't the memory consumption of a JIT'ed program be somewhere between that of a compiled and an interpreted program? \n\nInstead a JIT'ed program (such as `PyPy`) consume several times more memory than the equivalent interpreted program (such as `Python`). Why?\n\n========================================\n\nTop Answer:\nBe careful about what *kind* of memory usage you're talking about.\n\nCode compiled to C uses comparatively little memory for the *compiled machine code itself*.\n\nI would expect Python bytecode for a given algorithm to actually be smaller than the compiled C code for a similar algorithm, because Python bytecode operations are much higher level so there's often fewer of them to get a given thing done. But a Python program will also have the compiled code of the Python interpreter in memory, which is quite a large and complex program in itself. Plus a typical Python program will have much more of the standard library in memory than a typical C program (and a C program can strip out all the functions it doesn't actually use if it's statically linked, and if it's dynamically linked then it shares the compiled code with any other process in memory that uses it).\n\nPyPy then has on top of this the machine code of the JIT compiler, as well as the machine code generated from the Python bytecode (which doesn't go away, it has to be kept around as well). So your intuition (that a JITed system \"should\" consume memory somewhere between that of a compiled language and a fully interpreted language) isn't correct anyway.\n\nBut on top of all of those you've got the actual memory used by the data structures the program operates on. This varies immensely, and has little to do with whether the program is compiled ahead of time, or interpreted, or interpreted-and-JITed. Some compiler optimisations will reduce memory usage (whether they're applied ahead of time or just in time), but many actually trade off memory usage to gain speed. For programs that manipulate any serious amount of data it will completely dwarf the memory used by the code itself, anyway.\n\nWhen you say:\n\n Instead a JIT'ed program (such as PyPy) consume several times more\n memory than the equivalent interpreted program (such as Python). Why?\n\nWhat programs are you thinking of? If you've actually done any comparisons, I'm guessing from your question that they would be between PyPy and CPython. I know many of PyPy's data structures are actually smaller than CPython's, but again, that has nothing to do with the JIT.\n\nIf the dominant memory usage of a program is the code itself, then a JIT compiler adds huge memory overhead (for the compiler itself, and the compiled code), and can't do very much at all to \"win back\" memory usage through optimisation. If the dominant memory usage is program data structures, then I wouldn't be at all surprised to find PyPy using significantly less memory than CPython, whether or not the JIT was enabled.\n\nThere's not really a straightforward answer to your \"Why?\" because the statements in your question are not straightforwardly true. Which system uses more memory depends on many factors; the presence or absence of a JIT compiler is one factor, but it isn't always significant.\n\n========================================\n\nCode:\n```text\nC\n```\n\n```text\nPython\n```\n\n```text\nPyPy\n```\n\n```text\nPython\n```\n\n========================================\n\nComments:\n- Everyone talks about the JIT being the cause of the increased memory usage in Pypy, but that isn't the whole story. While some of Pypy's memory structures are more compact (lists of all ints for instance), Pypy has a variety of garbage collectors that can be used with it and they will definitely affect the amount of memory in use. The current default garbage collector in Pypy does not do reference counting in the interest of speed. Due to this, objects can remain in memory longer than they would in CPython and thereby programs can have a bigger memory footprint in Pypy.\n- Also, JIT is real-time compilation, so it must trade off optimization for speed. An offline compiler can afford to spend 5 seconds optimizing a function. A JIT not so much.\n- @RaymondChen: totally depends on the JIT, if its written correctly, you don't need to trade off *that* much optimization, its only really the expensive analysis techniques that are forgone.\n- Also, JITs are usually *specialising* compilers. The whole point of doing compilation \"just in time\" is that the compiler can wait and see what is being used at runtime and optimise specifically for that. Which means (depending on the JIT) that there may be several different versions of the machine code for a given section of bytecode in memory at any one time, along with the original byte code in case a new situation comes up that doesn't fit any of the compiled versions.\n- Add the size of JIT itself. Some (like Mono) are tolerably small, but some (like HotSpot) are huge and bulky.\n- I disagree with your sentence \"If the dominant memory usage of a program is the code itself, then a JIT compiler adds huge memory overhead...\" - this is false for a well-designed hotspot JIT compiler. I also disagree with your sentence \"If the dominant memory usage is program data structures, then [...] PyPy using significantly less memory than CPython\" - PyPy is NOT that good.\n- @Atom A JIT compiler translates code to code at runtime. A specialising JIT compiler *must* also keep the original code around (a non-specialising JIT compiler isn't terribly interesting, as it could be implemented as an ahead-of-time compiler, gaining efficiency of not having to run it at runtime, and the ability to spend more time on optimisations). Therefore it must add *at minimum* O(n) space overhead, where n is the amount of code compiled by the JIT compiler. Plus the memory of the JIT itself...\n- @Atom It's true that usually the JIT doesn't compile most of the program, so I was possibly overstating the case. But how much of the code of a program is JIT-compiled is always going to be dependent on the program. It is certainly the case that for any given JIT system there are programs that will pay a significant amount of memory overhead compared to the same program executing in a non-JITed environment.\n- @Atom Re PyPy memory usage: PyPy's implementation of class instances uses about the same memory usage as if you'd used `__slots__` everywhere in CPython. See morepypy.blogspot.com.au/2010/11/&hellip;. This is a VERY significant memory saving in some cases. They have some other space-wins as well. I have no idea how overall memory usage stacks up against CPython for whole real programs, but certainly PyPy is \"that good\" for some parts of the picture at least. Hence \"I wouldn't be surprised...\", not \"PyPy definitely uses less memory\".","metadata":{"transformedAt":"2026-08-18T18:32:17.806Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":68,"estimatedTokens":1795}}535{"id":"stack-25899105","source":"stackoverflow","questionId":25899105,"title":"Do you have to build a new compiler for a new operating system?","tags":["c","linux","compiler-construction","operating-system"],"text":"Title: Do you have to build a new compiler for a new operating system?\nTags: c, linux, compiler-construction, operating-system\nSource: Stack Overflow\n\nQuestion:\nI would like to build an OS some time in the future, and now thinking of some light sketches on how it would be. I have pretty much been coding in C compiled for the Windows environment (and some little Java). I would have to recompile any of my C programs should I want to run it under Linux. So the binaries, the product of compilation, must be different for each operating system. If I design a totally new OS from scratch, for both hobby and academic purpose, without using the Linux kernel or any known base code of an OS, what I understand as to happen is that I cannot compile my C programs with GCC since my OS will not be among its target systems. Here my question written on the title emerges. Thanks in advance for any hints.\n\n========================================\n\nTop Answer:\n**(1) No, you usually don't have to** write your own compiler. Writing a good optimizing compiler can be actually big task which I would better avoid.\n\nBut in order to enable writing applications for your OS in some higher level language you will either need to provide\n\n- some **(2.1) API emulation layer** (so that code written and compiled for other OS can be run on your OS)\n\n- **or** you'll have to **(2.2) port some existing compiler** to your OS\n\n- **or** at least make your OS a new available **(2.3) target platform** in an existing compiler\n\n- or some other option I don't know about\n\nThe choices are multiple each with its own pros/cons.\n\nSome examples (other then the obvious GCC already mentioned by @dietrich-epp, @sevenbits) to help you decide which way you want to :\n\n**(3.1) Free Pascal** (see http://www.freepascal.org) compiler can be extended with another target platform\n\n \n Free Pascal is a 32,64 and 16 bit professional Pascal compiler. It can target multiple processor architectures: Intel x86, AMD64/x86-64, PowerPC, PowerPC64, SPARC, and ARM. Supported operating systems include Linux, FreeBSD, Haiku, Mac OS X/iOS/Darwin, DOS, Win32, Win64, WinCE, OS/2, MorphOS, Nintendo GBA, Nintendo DS, and Nintendo Wii. Additionally, JVM, MIPS (big and little endian variants), i8086 and Motorola 68k architecture targets are available in the development versions\n ...\n\n \n Source: http://www.freepascal.org\n\n \n\n**(3.2) Inferno Operating System** (see http://www.vitanuova.com/inferno) has its own application language (see Limbo) with OS specific words, own compiler etc. Applications run in virtual machine (see Dis)\n\n \n Inferno® is a compact operating system designed for building distributed and networked systems on a wide variety of devices and platforms. With many advanced and unique features, Inferno puts an unrivalled set of tools into your hands...Inferno can run as a user application on top of an existing operating system or as a stand alone operating system...\n\n \n Source: http://www.vitanuova.com/inferno\n\n \n\n**(3.3) Squeak** (see http://en.wikipedia.org/wiki/Squeak) is a self contained OS with graphics and everything. It uses Smalltalk-80 as the language. Compiler included, applications run in virtual machine (see Cog VM). The VM could be emitted as portable C code and then ported to a bare-bone hardware.\n\n \n Squeak is a modern, open source, full-featured implementation of the powerful Smalltalk programming language and environment. Squeak is highly-portable, running on almost any platform you could name and you can really truly write once run anywhere. Squeak is the vehicle for a wide range of projects from multimedia applications and educational platforms to commercial web application development...\n\n \n Source: http://www.squeak.org\n\n \n\n**(3.4) MenuetOS** (see http://www.menuetos.net/) is 64bit OS written in assembly language. Flat Assembler (see FASM) compiler which can emit native binaries was ported to the OS including OS API and is included in basic installation. Later on C library was also ported\n\n \n MenuetOS is an Operating System in development for the PC written entirely in 32/64 bit assembly language...supports 32/64 bit x86 assembly programming for smaller, faster and less resource hungry applications...Menuet isn't based on other operating system nor has it roots within UNIX or the POSIX standards. The design goal, since the first release in year 2000, has been to remove the extra layers between different parts of an OS, which normally complicate programming and create bugs...\n\n \n Source: http://www.menuetos.net\n\n \n\n**(3.5) Google's Android OS** (see Wikipedia: Android (operating system)) ported Java Virtual Machine (see Dalvik later replaced by Android Runtime) and provided OS APIs for the `Java` programming language, reusing existing compilers and IDEs just consuming the produced binaries\n\n \n Android Runtime (ART) is an application runtime environment used by the Android mobile operating system. ART replaces Dalvik, which is the process virtual machine originally used by Android, and performs transformation of the application's bytecode into native instructions that are later executed by the device's runtime environment...\n\n \n Source: http://en.wikipedia.org/wiki/Android_Runtime\n\n \n\nThere are many more useful examples available. Whether you have to or don't have to basically depends on the programming paradigm your new OS will introduce. Why you want to build it and how will it differ from the existing ones.\n\nExamples for **no** are: **(3.1), (3.4), (3.5)**\n\nExamples for **yes** are: **(3.2), (3.3)**\n\n========================================\n\nCode:\n```text\nmalloc\n```\n\n```text\nJava\n```\n\n========================================\n\nComments:\n- I think by the time you've completed writing your own OS the answer will have presented itself :) But you might as well, yeah.\n- @cirrus after failing the 1st (and 2nd) attempt OP will know. But the head first approach requires to thing about it ahead - do it right the 1st time\n- Of course if you really want to go to the trouble of writing a new OS, why do something that's already been done. Rather than the layered kernel architecture & c-compiler model designed for a decades old paradigm, why not do something *truly* different like a functional or immutable OS (google Midori, immutable OS). With an immutable OS you could see great benefits in terms of security, size, performance and ease of concurrency. You'll almost certainly discover that C is a poor fit for your new platform and you'll want another language and possibly even a new type of language compiler.\n- @cirrus +1 for the Midori OS. From the same category of *truly* different there are examples (3.2) and **mainly (3.3)** in my answer below\n- Thank you for a great list of examples!","metadata":{"transformedAt":"2026-08-18T18:32:17.806Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":102,"estimatedTokens":1687}}536{"id":"stack-3412227","source":"stackoverflow","questionId":3412227,"title":"Writing an assembler , need help","tags":["compiler-construction","x86","assembly"],"text":"Title: Writing an assembler , need help\nTags: compiler-construction, x86, assembly\nSource: Stack Overflow\n\nQuestion:\nI'm writing an 8086 assembler for a college project . I've gone through some material on compiler design on the internet and am reading the 'dragon book' of compilers . But the problem is I couldn't find much about the assembling (generating object code) part here . Are there any good books or links for assembler design . Where do I start ?, I've gone through lexical analysis,parsing and intermediate code generation .\n\n========================================\n\nTop Answer:\nThe dragon book is totally the wrong source because an assembler is not a compiler. The \"grammer\" of an assembler line is extremly simple (regular grammer - not context sensitive). \n\nThe code generation part for an assembler is almost non existing. Get a good reference book about the opcodes of your target CPU and just generate the bytes as explained.\n\nThere a few nice simple assemblers out there and you should look at them. Learning by code reading is a very effective way for a college project. You will need it because the Intel Assembler code is extremely ugly at least if you want target some of the extensions as well.\n\n========================================\n\nCode:\n```text\nmov dx, 110\nmov ah,9\nint 21h\nmov ax,4c00\nint 21\n\nat address 110:\n\"Hello, World!\" 0d 0a 24\n```\n\n========================================\n\nComments:\n- Hmm. Memories from the past just passed by in my mind while reading your code. It has been a long time...\n- @Yves: Same here, written the same program in 80886, z80, 6301, 6502 and 1802 over the years, but none in the last decade.\n- great answer, but you should perhaps point out that the above example requires either MS-DOS or an MS-DOS shell under Windows. God only knows where int 21 would take you in other OS's\n- Right, as MacLaughlin implies, you cannot use interrupts in real mode Windows, meaning that the code above will not work in any OS after Windows 98.\n- LOL! The days of programs that are 5 instructions, long gone.\n- @doug65536: Yeah, it's 6 or maybe even 7 instructions now. Push, push, push, push, `call MessageBoxA`, `ret`. (Iczelion's tuts recommend `push 0`, `call ExitProcess` rather than `ret`.) The horror!\n- The link seems broken. Is this the same one: cs.smith.edu/~thiebaut/ArtOfAssembly/artofasm.html\n- Believe it or not, I have a similar area I'm working -- it's for an education effort, and the students will write assembly directly, so I need an assembler and linker/loader for the virtual CPU. In practice, assembly is just a lot of string processing -- I didn't need ANTLR etc. It's just read a line, tokenize it, and parse the line. Since assembly doesn't usually have multiline statements, that's about it. Linker loaders are a bit more complex!","metadata":{"transformedAt":"2026-08-18T18:32:17.806Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":41,"estimatedTokens":703}}537{"id":"stack-70460557","source":"stackoverflow","questionId":70460557,"title":"Why would I use a lexer and not directly parse code?","tags":["parsing","compiler-construction","interpreter","lexer"],"text":"Title: Why would I use a lexer and not directly parse code?\nTags: parsing, compiler-construction, interpreter, lexer\nSource: Stack Overflow\n\nQuestion:\nI am trying to create a simple programming language from scratch (interpreter) but I wonder why I should use a lexer.\nFor me, it looks like it would be easier to create a parser that directly parses the code. what am I overlooking?\n\n========================================\n\nTop Answer:\nWell, for intelligently simplified programing language you can get away without either lexer or parser :-) Not kidding. Look up **Forth**. You can start with tags here on SO (gforth is GNU's) and then go to the **Standard's site** which has pointers to a few interpreters, sites and its Glossary.\n\nThen you can check out Win32Forth and that should keep you busy for quite a while :-)\n\nInterpreter also compiles (when you invoke words that switch system to compilation context). All without a distinct parser. Lookahead is actually lookbehind :-) - not kidding. It rarely absorbs one following word (== lookahead is max 1). The \"words\" (aka tokens) are at the same time keywords and variable names and they all live in a Dictionary. There's a whole **online book** at that site (plus pdf).\n\nControl structures are also just words (they compile a few addresses and jumps on the fly).\n\nYou can find old **Journals** there as well, covering a wide spectrum from machine code generation to object oriented extensions. Yes still without parser - believe it or not.\n\nThere used to be more sophisticated (commercial) Forth systems which were reducing words to machine call instructions with immediate addressing (makes the engine run 2-4 times faster) but even plain interpreters were always considered to be fast. One is apparently still active - SwiftForth, but don't expect any freebies there.\n\nThere's one Forth on GitHub CiForth which is quite spartanic but has builds and releases for Win, Linux and Mac, 32 and 64 so you can just download and run. Claims to have a 16-bit build as well :-) For embedded systems I suppose.\n\n========================================\n\nComments:\n- For almost all real-world languages, scannerless parsing requires unlimited lookahead / backtracking. So that rules out many parsing algorithms and may not give you the desired performance. Also whitespace handling becomes more cumbersome without a lexer. As does properly distinguishing between keywords and identifiers. Good error messages become harder as well if you don't want to replace \"unexpected keyword 'if'; expected 'as' or 'var'\" with \"unexpected letter 'i'; expected 'a' or 'v'\". That said, scannerless parsing is a thing and may well fit your use case.\n- It's unclear what you mean by \"use a lexer\". If you're not using a lexer-generator (e.g., flex), then there may not be much difference between the two alternatives you have in mind.","metadata":{"transformedAt":"2026-08-18T18:32:17.806Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":30,"estimatedTokens":717}}538{"id":"stack-9064292","source":"stackoverflow","questionId":9064292,"title":"Brainfuck compiler in scala","tags":["scala","compiler-construction","interpreter","brainfuck"],"text":"Title: Brainfuck compiler in scala\nTags: scala, compiler-construction, interpreter, brainfuck\nSource: Stack Overflow\n\nQuestion:\nWant to make some Domain Specific Language(DSL) for practice, first idea it is to write interpreter or compiler of Brainfuck. \nFirst idea was to override functions such as they will behave as Brainfuck commands: `\">\"`, `\"Is there better solution to write it in Scala?\n\n========================================\n\nTop Answer:\nI suppose that you are aware of this example.\n\nAlso this example suggested by Mikaël Mayer in comments.\n\n========================================\n\nCode:\n```text\n\">\"\n```\n\n```text\n\"<\"\n```\n\n```text\n\"+\"\n```\n\n```text\n\"-\"\n```\n\n```text\n\".\"\n```\n\n```text\n\",\"\n```\n\n```text\n\"[\"\n```\n\n```text\n\"]\"\n```\n\n```text\n\".\"\n```\n\n========================================\n\nComments:\n- I don't know, I just recall that particular example from the Scala official site.","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":58,"estimatedTokens":223}}539{"id":"stack-3191387","source":"stackoverflow","questionId":3191387,"title":"Fast Standard ML compiler or bytecode interpreter, with read-eval-print loop, for Linux?","tags":["compiler-construction","functional-programming","interpreter","sml"],"text":"Title: Fast Standard ML compiler or bytecode interpreter, with read-eval-print loop, for Linux?\nTags: compiler-construction, functional-programming, interpreter, sml\nSource: Stack Overflow\n\nQuestion:\nFor use with a class I'll be teaching, I'm looking for a fast compiler or bytecode interpreter for Standard ML. **I'm looking for fast compile times;** any reasonable run time will do. Bonus if the compilation model is simple and clear. Students in the class will also be using MLton to generate good binaries, but MLton is slow to compile and there are times when students need something they can interact with. \n\nHere's what I know already:\n\nStandard ML of New Jersey has an interactive read-eval-print-loop, but its compilation rules are a bit strange, and it's a bit slow. Still, it may be the leading contender.\n\nMoscow ML used to be ideal, but it *still* has not been brought up to date with the 2004 Standard Basis Library. Most unfortunate, because in addition to its many other fine properties, Moscow ML also has an interactive help system&mdash;but I can't inflict on my students a compiler whose libraries do something different from what all the online documentation says.\n\nPoly/ML might well fit the bill, except that looking at the documentation online, I can't figure out how to get it to compile. That might be OK.\n\nAt the moment it looks as if either SML/NJ or Poly/ML would be the best compromise. It has been many years since I did any serious work in Standard ML, and I would welcome information about other compilers or which of these alternatives is, in your experience, the fastest to interact and the easiest to learn to use.\n\n========================================\n\nTop Answer:\nI appreciate that this is a very late answer to the question, but there is a Moscow ML project on github with an updated Basis Library. It's at https://github.com/kfl/mosml/ (with an update in November 2012!)\n\nI have also got code that implements much of the new Basis Library for Moscow ML so that I can use both it and Poly/ML to implement HOL4. The code for that is all inside http://github.com/mn200/HOL, and could be dug out if you wanted it.\n\n========================================\n\nCode:\n```text\n[mtf@fenrir hamlet-1.3.1.polyml]$ /usr/bin/time make with-poly\n...\n        2.92 real         2.31 user         0.55 sys\n[mtf@fenrir hamlet-1.3.1.smlnj]$ /usr/bin/time make with-smlnj\n...\n       11.98 real        11.08 user         0.78 sys\n[mtf@fenrir hamlet-1.3.1.mlton]$ /usr/bin/time make with-mlton\n...\n       24.51 real        21.04 user         3.05 sys\n```\n\n```text\n[mtf@fenrir mlton.polyml]$ /usr/bin/time make polyml-mlton\n...\n      117.67 real       112.12 user         4.87 sys\n[mtf@fenrir mlton.smlnj]$ /usr/bin/time make smlnj-mlton\n...\n      123.31 real       116.24 user         6.38 sys\n[mtf@fenrir mlton.mlton]$ /usr/bin/time make mlton-compile\n...\n      238.44 real       232.01 user         5.49 sys\n```\n\n```text\nstdIn\n```\n\n```text\nuse\n```\n\n```text\nPolyML.make\n```\n\n```text\n./configure ; make ; make install\n```\n\n```text\nPolyML.export\n```\n\n========================================\n\nComments:\n- I've had no difficulties compiling Poly/ML; I just installed the Debian package. It's the \"yet another compilation system\" that I'm having trouble figuring out. I very much want the students to be able to use a model they're familiar with---compile source to get an executable binary---but with something a little faster than MLton. Perhaps I should download HaMLet and see how the Makefile is set up. +1\n- Both HaMLet and MLton (and Isabelle AFAICT) drive Poly/ML via the `use` function. I don't know of any project (besides Poly/ML itself) that uses the PolyML.make facility. You could ask for pointers on the Poly/ML mailing list; its low traffic, but responsive.\n- Isabelle/ML uses the very convenient PolyML.Compiler structure to implement its own variant of `use`, such that the effect on the ML enfironment is managed internally, e.g. to allow undo/rollback of the static environment.\n- Library modules `TextIO`, `Array`, and `Vector` don't conform to the standard. I have been after the Moscow ML people about this for *years*, and they basically don't care. If they don't care about improving their software, why should I? On another note, maybe my question wasn't clear, but I care about **fast compile times**. Any plausible run time is just fine with me.\n- I will see what I can do about the standard conformance of the standard libraries. It seems to be a useful job. I think the main problem with Moscow ML is that it seems to be abandoned by its original maintainers. With the LLVM port it gained a bit of momentum when two Danish students began touching it after nearly a decade, but this also seems to have stalled. I think Standard ML lost a lot of (potential) users and researchers to Haskell. Maybe this is why nobody cares that much anymore.\n- I actually gave the wrong link to the exsml repository. I corrected it.\n- Can you look at github.com/jlouis/exsml/tree/master/exsmllib and see whether the problems with the standard conformance were solved?\n- They were not. In fact, that project is currently rotting like hell because I don't have time to build an LLVM backend. Nowadays, I'd probably start from Poly/ML which is a better base.","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":82,"estimatedTokens":1321}}540{"id":"stack-3212176","source":"stackoverflow","questionId":3212176,"title":"C: Compiler info at runtime","tags":["c","compiler-construction"],"text":"Title: C: Compiler info at runtime\nTags: c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIs there a way to print the name of the compiler and the version that was used to compile a program, something like; \n\n```\nprintf(\"This is compiled with %s version %s\\n\", COMPILER, COMPILER_VERSION);\n```\n\n?\n\n========================================\n\nTop Answer:\nNo, the way to get the name of the compiler is itself compiler-specific :-P.\n\ngcc provides the `__VERSION__` macro, though.\n\n========================================\n\nCode:\n```text\nprintf(\"This is compiled with %s version %s\\n\", COMPILER, COMPILER_VERSION);\n```\n\n```text\n#if defined(__GNUC__)\n# if defined(__GNUC_PATCHLEVEL__)\n#  define __GNUC_VERSION__ (__GNUC__ * 10000 \\\n                            + __GNUC_MINOR__ * 100 \\\n                            + __GNUC_PATCHLEVEL__)\n# else\n#  define __GNUC_VERSION__ (__GNUC__ * 10000 \\\n                            + __GNUC_MINOR__ * 100)\n# endif\n#endif\n```\n\n```text\n_MSC_FULL_VER\n```\n\n```text\n__VERSION__\n```\n\n========================================\n\nComments:\n- Err... What if I used microsoft's compilers?\n- _MSC_FULL_VER Take a look at the link. It's a reference for compiler specific pre processor defines.","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":53,"estimatedTokens":305}}541{"id":"stack-3720901","source":"stackoverflow","questionId":3720901,"title":"What is the precise definition of a lookahead set?","tags":["compiler-construction","compiler-theory"],"text":"Title: What is the precise definition of a lookahead set?\nTags: compiler-construction, compiler-theory\nSource: Stack Overflow\n\nQuestion:\nI'm toying around with writing compilers and learning about the theory behind syntax analysis. I've found that even though it's a key concept for understanding recognition algorithms, information about it on the net is fairly poor. It seems StackOverflow is in a unique position to fix this problem.\n\n========================================\n\nCode:\n```text\nLOOKAHEAD(X -> α) = FIRST(α) U FOLLOW(X), if NULLABLE(α)\nLOOKAHEAD(X -> α) = FIRST(α), if not NULLABLE(α)\nLOOKAHEAD(X) = LOOKAHEAD(X -> α) U LOOKAHEAD(X -> β) U LOOKAHEAD(X -> γ)\n```\n\n```text\nNULLABLE(ε) = true\nNULLABLE(x) = false, if x is a terminal\nNULLABLE(αβ) = NULLABLE(α) and NULLABLE(β)\nNULLABLE(P) = NULLABLE(α_1) or NULLABLE(α_2) or ... or NULLABLE(α_n),\n               if P is a non-terminal and the right-hand-sides\n               of all its productions are α_1, α_2, ..., α_n.\n```\n\n```text\nFIRST(ε) = Ø\nFIRST(x) = {x}, assuming x is a terminal\nFIRST(αβ) = FIRST(α) U FIRST(β), if NULLABLE(α)\n          = FIRST(α), if not NULLABLE(α)\nFIRST(P) = FIRST(α_1) U FIRST(α_2) U ... U FIRST(α_n),\n               if P is a non-terminal and the right-hand-sides\n               of all its productions are α_1, α_2, ..., α_n.\n```\n\n```text\nE -> n A\nA -> E B\nA -> ε\nB -> + A\nB -> * A\n```\n\n```text\nNULLABLE(E) = NULLABLE(n A) = NULLABLE(n) ∧ NULLABLE(A) = false\nNULLABLE(A) = NULLABLE(E B) ∨ NULLABLE(ε) = true\nNULLABLE(B) = NULLABLE(+ A) ∨ NULLABLE(* A) = false\n\nFIRST(E) = FIRST(n A) = {n}\nFIRST(A) = FIRST(E B) U FIRST(ε) = FIRST(E) U Ø = {n} (because E is not NULLABLE)\nFIRST(B) = FIRST(+ A) U FIRST(* A) = FIRST(+) U FIRST(*) = {+, *}\n```\n\n```text\nFOLLOW(E): Let β = $, so add the constraint that FIRST($) = {$} ⊆ FOLLOW(E)\n           Let β = B, so add the constraint that FIRST(B) = {+, *} ⊆ FOLLOW(E)\nFOLLOW(A): Let β = ε, so add the constraint that FIRST(ε) = Ø ⊆ FOLLOW(A).\n           Because NULLABLE(ε), add the constraint that FOLLOW(E) ⊆ FOLLOW(A).\n           Let β = ε, so add the constraint that FIRST(ε) = Ø ⊆ FOLLOW(A).\n           Because NULLABLE(ε), add the constraint that FOLLOW(B) ⊆ FOLLOW(A).\n           Let β = ε, so add the constraint that FIRST(ε) = Ø ⊆ FOLLOW(A).\n           Because NULLABLE(ε), add the constraint that FOLLOW(B) ⊆ FOLLOW(A).\nFOLLOW(B): Let β = ε, so add the constraint that FIRST(ε) = Ø ⊆ FOLLOW(B).\n           Because NULLABLE(ε), add the constraint that FOLLOW(A) ⊆ FOLLOW(B).\n```\n\n```text\n{+, *, $} ⊆ FOLLOW(E)\n    FOLLOW(E) ⊆ FOLLOW(A)\n    FOLLOW(A) = FOLLOW(B)\n\n    FOLLOW(E) = FOLLOW(A) = FOLLOW(B) = {+, *, $}.\n```\n\n```text\nLOOKAHEAD(E -> n A) = FIRST(n A) = {n}     because ¬NULLABLE(n A)\nLOOKAHEAD(A -> E B) = FIRST(E B)           because ¬NULLABLE(E B)\n                    = FIRST(E) = {n}       because ¬NULLABLE(E)\nLOOKAHEAD(A -> ε)   = FIRST(ε) U FOLLOW(A) because NULLABLE(ε)\n                    = Ø U {+, *, $} = {+, *, $}\nLOOKAHEAD(B -> + A) = FIRST(+ A)           because ¬NULLABLE(+ A)\n                    = FIRST(+) = {+}       because ¬NULLABLE(+)\nLOOKAHEAD(B -> * A) = {*}                  for the same reason\n```\n\n```text\nLOOKAHEAD(E) = LOOKAHEAD(E -> n A) = {n}\nLOOKAHEAD(A) = LOOKAHEAD(A -> E B) U LOOKAHEAD(A -> ε)   = {n} U {+, *, $}\nLOOKAHEAD(B) = LOOKAHEAD(B -> + A) U LOOKAHEAD(B -> * A) = {+, *}\n```\n\n```text\nA -> foo X\n```\n\n```text\nA -> foo X B\n```\n\n```text\nB -> ε\n```\n\n```text\nFOLLOW(A) ⊆ FOLLOW(X)\n```\n\n```text\nE' -> E $\n```\n\n```text\n$\n```\n\n========================================\n\nComments:\n- The simple answer is, \"the set of tokens that you expect next in some context\".","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":15,"totalLines":119,"estimatedTokens":911}}542{"id":"stack-15194103","source":"stackoverflow","questionId":15194103,"title":"what is meant by left most derivation?","tags":["parsing","language-agnostic","compiler-construction"],"text":"Title: what is meant by left most derivation?\nTags: parsing, language-agnostic, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nPlease help me understand what is meant by `Left Most Derivation` the second `L` in `LL Parser`.\n\nExplain it with a simplest example.\n\nI saw the following picture explaining *Left most derivation* but I do not understand it :\n\n========================================\n\nCode:\n```text\nLeft Most Derivation\n```\n\n```text\nL\n```\n\n```text\nLL Parser\n```\n\n```text\nN\n=> N D   --Replaces the first/left most/only (which is \"N\") with the N => N D rule\n=> N D D --Replaces the first/left most nonterminal (which is \"N\") with the N => N D rule\n=> D D D --Replaces the first nonterminal (which is \"N\") with the N => D rule\n=> 1 D D --Replaces the first nonterminal (\"D\") with the D => 1 rule(our first terminal character!)\n=> 1 2 D --Replaces the first nonterminal (\"D\") with the D => 2 rule\n=> 1 2 3 --Replaces the first nonterminal (\"D\") with the D => 3 rule\n-- Only terminal characters remain, derivation/reduction is complete.\n```\n\n========================================\n\nComments:\n- I think left most derivation means you always apply a rule # to the leftmost place you can apply it. So I can just say 'Rule N -> N D' and you know to apply it in the leftmost place you can. If it was an RR Parser it'd apply it on the rightmost place it could.","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":41,"estimatedTokens":343}}543{"id":"stack-33349790","source":"stackoverflow","questionId":33349790,"title":"PHP operator precedence bug?","tags":["php","parsing","compiler-construction","operators"],"text":"Title: PHP operator precedence bug?\nTags: php, parsing, compiler-construction, operators\nSource: Stack Overflow\n\nQuestion:\nThe result of:\n\n```\nvar_dump(null != $a = 15);\nvar_dump($a);\n```\n\nis:\n\n```\nbool(true)\nint(15)\n```\n\nWhy is this script not triggering an error?\nSince `!=` (not equal operator) has a higher precedence than `=` (assignment operator), `$a` should be compared to `null` first?\n\n========================================\n\nTop Answer:\nThis is not about operator precedence but about: operator precedence lists don't tell you the details (really, they never do), e.g. about the bison rules and the resulting pattern matching and stack reducing.\n\nLet's take the statement `null != $a = 15;`, for simplicity without the var_dump.\n\nThis is how the parser \"sees\" this statement - or: sees that it *is* a statement.\n\n(I hope this will be rendered with a fix-width font everywhere...)\n\n```\nnull != $a = 15 ;\n T_VARIABLE \nidentifier compound_variable T_LNUMBER\nnamespace_name reference_variable common_scalar\ngeneral_constant base_variable scalar\nscalar base_variable_with_functions_calls expr_without_variable\nexpr_without_variable variable = expr\nexpr T_IS_NOT_EQUAL \\______ expr_without_variable _________/\n \\__________________ expr ____________________________________________/ ;\n \\_________________ unticked_statement _______________________________________________/\n statement\n```\n\n( You can look up the rules at https://github.com/php/php-src/blob/PHP-5.6.15/Zend/zend_language_parser.y )\n\nThere's no special rule for the assignment operator in this case; there simply isn't another way for the parser to match the statement, so precedence doesn't apply.\n\n========================================\n\nCode:\n```text\nvar_dump(null != $a = 15);\nvar_dump($a);\n```\n\n```text\nbool(true)\nint(15)\n```\n\n```text\n!=\n```\n\n```text\n=\n```\n\n```text\n$a\n```\n\n```text\nnull\n```\n\n```text\nnull                       !=                    $a                                  =   15                           ;\n                                                 T_VARIABLE                           \nidentifier                                       compound_variable                       T_LNUMBER\nnamespace_name                                   reference_variable                      common_scalar\ngeneral_constant                                 base_variable                           scalar\nscalar                                           base_variable_with_functions_calls      expr_without_variable\nexpr_without_variable                            variable                            =   expr\nexpr                       T_IS_NOT_EQUAL             \\______ expr_without_variable _________/\n   \\__________________    expr ____________________________________________/                                          ;\n                           \\_________________       unticked_statement _______________________________________________/\n                                                     statement\n```\n\n```text\nnull != $a = 15;\n```\n\n========================================\n\nComments:\n- definitely weird. `$a == null = 15` gives false dumps too. And putting in `(null != $a) = 15` does spit out an unexpected `=` error.\n- What alternative to interpreting it as `null != ($a=15)` is there? With `(null !=$a)=15` you'd try to assign the value 15 to a boolean value; that doesn't work.\n- @VolkerK this is an example, this script has no real life purpose! My reflection starts with this kind of expression that I see (and use) a lot `if (!$a = foo())`.\n- and then the same thing applies. How would you you assign a value to (!$a) ?\n- I think one other thing to note is the following comment on readability: \"Use of parentheses, even when not strictly necessary, can often increase readability of the code by making grouping explicit rather than relying on the implicit operator precedence and associativity.\"\n- It's not a \"deliberate exception\"; there is no addional lexer rule to explicitly \"allow\" this. It's just the normal way it works. The example in the manual is just a reminder for developers who wonder.\n- But it does seem to be an exception from the table at the top of the page, is it not? Even if there is no explicit rule in the parser/lexer, it seems to be an exception to the previous explanation.\n- @VolkerK Adjusted my wording, would you consider it more correct now?\n- Not really. There is no exception involved; it's merely something developers wonder about.\n- There is a rule of operator precedence, this expression clearly breaks the rule, this is called an exception. No big deal though, I'm moving on...","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":111,"estimatedTokens":1153}}544{"id":"stack-27627234","source":"stackoverflow","questionId":27627234,"title":"How does JIT compilation actually execute the machine code at runtime?","tags":["compiler-construction","compilation","llvm","jit","machine-code"],"text":"Title: How does JIT compilation actually execute the machine code at runtime?\nTags: compiler-construction, compilation, llvm, jit, machine-code\nSource: Stack Overflow\n\nQuestion:\nI understand the gist of how JIT compilation works (after reading such resources as this SO question). However, I am still wondering how does it actually *execute* the machine code at runtime?\n\nI don't have a deep background in operating systems or compiler optimizations, and haven't done anything with machine code directly, but am starting to explore it. I have started playing around in assembly, and see how something like NASM can take your assembly code and compile it to machine code (the executable), and then you can \"invoke\" it from the command line like `./my-executable`.\n\nBut how is a JIT compiler actually doing that at runtime? Is it like streaming machine code into stdin or something, or how does it work? If you could provide an example or some pseudocode of how some assembly (or something along those lines, not as high level as C though) might look to demonstrate the basic flow, that would be amazing too.\n\n========================================\n\nTop Answer:\nWhen code is executed, it all boils down to the code being loaded into a known part of memory, and the program counter being set to the start of the code, either by a direct register setting, or a jmp instruction, or similar. So what the JIT compiler will do is build the machine code in a known part of memory, and then execute from there.\n\n========================================\n\nCode:\n```text\n./my-executable\n```\n\n```text\nvoid myfunc(int a) {\n    printf(\"%s\", a);\n}\n```\n\n```text\nOP Params OpName     Description\n13 82 6a  PushString 82 means string, 6a is the address of \"%s\"\n                     So this function pushes a pointer to \"%s\" on the stack.\n13 83 00  PushInt    83 means integer, 00 means the one on the top of the stack.\n                     So this function gets the integer at the top of the stack,\n                     And pushes it on the stack again\n17 13 88 Call        1388 is printf, so this calls the printf function\n03 02    Pop         This pops the two things we pushed back off the stack\n02       Return      This returns to the calling code.\n```\n\n```text\n%s\n```\n\n```text\n6a\n```\n\n```text\nprintf\n```\n\n```text\n1388\n```\n\n```text\nvoid myfunc(int a) {printf(\"%s\", a);}\n```\n\n```text\n13 82 6a 13 83 00 17 13 88 03 02 02\n```\n\n```text\nnamespace Hello\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            Console.WriteLine(\"Hello, world!\");\n        }\n    }\n}\n```\n\n```text\nmov     edx,len                             ;message length\n    mov     ecx,msg                             ;message to write\n    mov     ebx,1                               ;file descriptor (stdout)\n    mov     eax,4                               ;system call number (sys_write)\n    int     0x80                                ;call kernel\n\n    mov     eax,1                               ;system call number (sys_exit)\n    int     0x80                                ;call kernel\n\n\nmsg     db  'Hello, world!',0xa                 ;our dear string\nlen     equ $ - msg                             ;length of our dear string\n```\n\n========================================\n\nComments:\n- The jitter only *generates* machine code, it doesn't execute it. That's the job of the processor.\n- Could you show a basic example (maybe along the lines of an assembly snippet, just updated the original question)? I am sorta starting to see what you're describing, but don't yet get how the assembly might look, and how the processor plays into it.\n- @LancePollard: I haven't actually messed with any assembly in ages, and even then its unlikely we knew the same one. I put in a \"sample\" from an bytecode I'm familiar with though.\n- Just curious about what bytecode is it? Is it developed for learning/teaching purpose?\n- The bytecode is an unnamed proprietary code at my company, for use in localization. I cant really give any details.","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":101,"estimatedTokens":1003}}545{"id":"stack-22894520","source":"stackoverflow","questionId":22894520,"title":"Compiler code optimization: AST vs. IR","tags":["optimization","compiler-construction","abstract-syntax-tree"],"text":"Title: Compiler code optimization: AST vs. IR\nTags: optimization, compiler-construction, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\n, where I define IR as a 3-address code type representation (I realize that one can mean by it an AST representation as well).\n\nIt is my understanding that, when writing a best-practice compiler for an *imperative language*, code optimization happens both on the AST (probably best using a Visitor Pattern), and on the IR produced from the AST. \n\n(a) Is that correct?\n\n(b) Which type of optimization steps are best handled on the AST before even producing an IR? (reference to an article/a list online welcome too as long as it deals with an imperative language) \n\nThe compiler I'm working on is for Decaf (which some might know) which has a fairly deep CFG up to (single) class inheritance; I'll add features not part of it such as type coercion. It will be completely hand-coded (using no tools whatsoever). This is not homework; writing it for fun.\n\n========================================\n\nTop Answer:\nIR is a form of an AST (often it is \"flattened\", but there are deep tree IRs as well), it may not be easy to distinguish one from another, especially if compiler is implemented as a sequence of very small rewrites from an original AST all the way down to a final IR suitable for instruction selection. \n\nOptimisations may happen anywhere on this chain, but some representations are more suitable for a wide range of optimisations, most notably, an SSA form, used by most of the modern compilers to do nearly all the optimisations.\n\n========================================\n\nComments:\n- It's exceptionally hard to do a proper constant folding on an expression tree level. SSA is much better suited for it. With SSA you still have all your expressions for free, plus a proper flow information. You have to do constant folding in conjunction with a DCE, otherwise it won't be complete, and DCE is much easier to do on top of SSA.\n- All of the optimisations you've mentioned are very easy to do on an SSA level, and nearly impossible to do them properly on a tree level. The only kind of optimisations worth doing on the high level trees is domain-specific optimisations which rely on an information which might be already lost during translation, de-sugaring, etc.\n- My plan is to go all the way to x86 32b assembly, from a basic AST/Visitor pair to an SSA-style IR (possibly several intermediate ASTs and IRs) What's completely unknown to me at this point is which and how many optimizations I'll perform, on which object, and how many passes. As to Decaf, that's a pun for Java light, and a language I've seen used in classes at Brown, MIT, and Stanford. (also, thanks :))\n- Compiler language is C++.\n- My concern is that tree rewriting is relatively cheap to get into, but SSA takes quite a bit of investment before you get the payoff, with no clear end point. Perhaps that's what you had in mind, but my preference is for languages someone might actually use. I kind of got the Decaf reference, and I wonder why you wouldn't pick a target where a new compiler might actually solve someone's problem.\n- @SK-logic: SSA is relatively new, the literature is mixed and the effort uncertain, but I defer to your experience. My answer was intended more towards what can reasonably be done at the tree level if there are other reasons to limit optimisation at later stages (eg VM target, common backend, lack of interest).","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":31,"estimatedTokens":867}}546{"id":"stack-7951854","source":"stackoverflow","questionId":7951854,"title":"Do one-pass compilers still exist?","tags":["compiler-construction","compilation","compiler-optimization"],"text":"Title: Do one-pass compilers still exist?\nTags: compiler-construction, compilation, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nAccording to http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_14.html, GCC 2.95 had around 24 different passes, back in the days.\n\nLooking at http://llvm.org/docs/Passes.html, LLVM has about a hundred different passes.\n\nAre there any compiler in use today (maybe in embedded environments) that does all of parsing to code generation in one single pass?\n\n========================================\n\nTop Answer:\nThere are several one-pass Cobol compilers. RM-Cobol is certainly one of them, and ACE Cobol too if it still exists anywhere. Cobol is quite a good candidate for a one-pass compiler as it is mostly just hooking runtime library calls together, very little scope for optimization during compilation. I believe ACU-Cobol is also one-pass but I'm open to correction.\n\n========================================\n\nComments:\n- Relax, LLVM does not actually run all the hundreds of its passes for each function or module.\n- @SK-logic I'm not actually worried or anything :) This is a question out of sheer curiosity.\n- Well, Fabrice Bellard's TCC is an interesting beast, but I am not sure it is used much in the industry - we had a funny time trying to target it because of its partial support for C99, 64-bit architectures and the such. While this is a valid answer I would love to learn about other ones.\n- I consider it already as pretty difficult to define a pass: Only count the numbers of the times the sources are read? Only count a compilation step as a pass if an intermediate presentation is written to disk (for me the most usefull definition due to history)? Or each time some intermediate presentation is walked fully? How to count partial walks of an intermediate presentation then? How to count additional compilations due to WPO?\n- Yes, that is a legitimate question in itself. It would seem TCC doesn't use any intermediate representation other than value registers. I was mostly thinking of passes as AST transformations (either node swaps in an existing AST or translating an AST to another completely different AST, for example SSA).\n- I would say the number of passes in a compiler refers to the number of *complete* traversals of the source program: the input text, the parse tree, the RTL, ... Legend has it that IBM had a compiler project with *N* guys on it and it came in with *N* passes, where *N* was something like 36.","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":621}}547{"id":"stack-21732826","source":"stackoverflow","questionId":21732826,"title":"Is it possible to explicitly free memory with JVM Bytecode?","tags":["compiler-construction","jvm","interpreter","bytecode"],"text":"Title: Is it possible to explicitly free memory with JVM Bytecode?\nTags: compiler-construction, jvm, interpreter, bytecode\nSource: Stack Overflow\n\nQuestion:\nThere are several computer programming languages using JVM bytecode as, lets say, target language for their interpreter/compilers. It seems to me that many new programming languages (less than 15 years old) run over JVM and I wonder whether explicit memory deallocation is forbidden for all of them:\n\nIs it possible to explicitly allocate-deallocate memory using in bytecode through any instruction? Is, on de contrary, the garbage collector always resposible for memory liberation?\n\n========================================\n\nTop Answer:\nOne of the absolute tenets of the JVM is that objects are absolutely positively guaranteed to last at least as long as any references to them. If the objects in a group hold strong references to each other, but the only references to them outside the group are encapsulated in `WeakReference` objects, then all objects in the group, and all references to them, will cease to exist, simultaneously (the `WeakReference` objects may continue to exist, but they won't hold references to anything anymore).\n\nA consequence of this is that the memory for an object cannot be reused unless or until the JVM can be certain that no references to it exist. Because it would take just about as long to ensure that no references existed to one particular object as it would take to perform a garbage-collection on all objects that were about the same age or newer, there's really no benefit to trying to recycle memory sooner. This is especially true if one considers that the GC runs when memory is needed. Until memory is needed, there's no advantage freeing it up.\n\n========================================\n\nCode:\n```text\nWeakReference\n```\n\n```text\nWeakReference\n```\n\n```text\npublic DirectIntArray(long size) {\n  startIndex = unsafe.allocateMemory(size * INT_SIZE_IN_BYTES);\n  unsafe.setMemory(startIndex, size * INT_SIZE_IN_BYTES, (byte) 0);\n  }\n}\n\npublic void setValue(long index, int value) {\n  unsafe.putInt(index(index), value);\n}\n\npublic int getValue(long index) {\n  return unsafe.getInt(index(index));\n}\n\nprivate long index(long offset) {\n  return startIndex + offset * INT_SIZE_IN_BYTES;\n}\n\npublic void destroy() {\n  unsafe.freeMemory(startIndex);\n}\n```\n\n```text\nClassLoader\n```\n\n========================================\n\nComments:\n- Yes. You can confirm this by looking at the full set of bytecode instructions docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html\n- There are some instructions as 'new' or 'newarray' which implicitly alloc memory. I understand from your answers and the bytecode instructions set listings that there is not such a thing as \"del\". Is it possible to implement something like \"del\" instruction in native code and then call it form bytecode? That way you could compile a hypothetical programming language memory deallocation instruction to bytecode + your little native code.\n- Not in any useful way. The implementation of 'new' etc. is implementation-dependent, so even if you could write a 'del' that hacks into the heap, it wouldn't be stable or portable. There are C to bytecode compilers. They they simulate malloc, free, pointer arithmetic etc. using java arrays.\n- @pablo The JVM isn't even guaranteed to allocate memory for new or newarray, let alone create an actual heap object. It's just an abstraction.","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":62,"estimatedTokens":860}}548{"id":"stack-15012865","source":"stackoverflow","questionId":15012865,"title":"How do I work with the AST in Irony now?","tags":["c#","compiler-construction","abstract-syntax-tree","irony"],"text":"Title: How do I work with the AST in Irony now?\nTags: c#, compiler-construction, abstract-syntax-tree, irony\nSource: Stack Overflow\n\nQuestion:\nI have a grammar that works and parses in the Irony console just fine, but I don't get anything in the AST treeview. I was following along with the BASIC->Javascript article found here: http://www.codeproject.com/Articles/25069/JSBasic-A-BASIC-to-JavaScript-Compiler, but it seems that the Ast stuff has all been moved/removed. I found the Irony.Interpreter .dll, which has some Ast stuff in it, but it seems all tied up in the Expression sample implementation.\n\nWhat am I missing here? I want to walk my tree and generate source code, and I'm not sure where to start. \n\nI've seen some mention of using the visitor pattern, which I'm fine with, but I don't know how to implement it and run it in a way that Irony likes.\n\n========================================\n\nTop Answer:\nIn Irony parsing is done in 2 phases. First it creates a parse tree and then it creates your AST tree.\n\nYou are only seeing the first step. In order for Irony to create the AST you can:\n\nTell it how to to map your NonTerminals to AST nodes:\n\nE.g. looking at the Irony sample grammer ExpressionEvaluatorGrammar we see:\n\n```\nvar BinExpr = new NonTerminal(\"BinExpr\", typeof(BinaryOperationNode));`\n```\n\nHere the we are telling Irony to map the BinExpr NonTerminal to a BinaryOperationNode which is our AST node.\n\nMake it generate the AST when parsing:\n\nWhen you set this flag the AST tree will be generated when you parse.\n\n```\nthis.LanguageFlags = LanguageFlags.CreateAst;\n```\n\nThe root of your AST tree will then be:\n\n```\nparseTree.Root.AstNode\n```\n\nI found this source a great starting point.\n\n========================================\n\nCode:\n```text\nMyBaseNode\n```\n\n```text\nAstNode\n```\n\n```text\nMyBaseNode\n```\n\n```text\nAccept\n```\n\n```text\npublic override void Accept(IMyNodeVisitor visitor) { visitor.Visit(this); }\n```\n\n```text\nInit\n```\n\n```text\nInitChildren\n```\n\n```text\nIMyNodeVisitor\n```\n\n```text\nVisit\n```\n\n```text\nvoid Visit(MyDerivedNode1 node);\n```\n\n```text\nASTNodeType\n```\n\n```text\nMyTerminal1.AstConfig.NodeType = typeof(MyDerivedNode1);\n```\n\n```text\nvar MyNonTerminal2 = new NonTerminal(\"MyNonTerminal2\", typeof(MyDerivedNode2));\n```\n\n```text\nLanguageFlags = LanguageFlags.CreateAst;\n```\n\n```text\nvar BinExpr = new NonTerminal(\"BinExpr\", typeof(BinaryOperationNode));`\n```\n\n```text\nthis.LanguageFlags = LanguageFlags.CreateAst;\n```\n\n```text\nparseTree.Root.AstNode\n```\n\n========================================\n\nComments:\n- I just switched over to ANTLR, so I guess this is all moot now. So little help, it hurts. Oh well, thanks for any that took the time to look!\n- Holy smokes. Sarcasm isn't just a reference implementation, it is `Sarcasm, an EBNF-like DSL that generates Irony`! This is like inception mixed with ouroboros.\n- The link to the Sarcasm project is dead. (Or is that some sort of intentional \"who's on first\" joke that I didn't get?)","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":121,"estimatedTokens":745}}549{"id":"stack-5586747","source":"stackoverflow","questionId":5586747,"title":"Design Pattern For Making An Assembler","tags":["c++","design-patterns","assembly","compiler-construction"],"text":"Title: Design Pattern For Making An Assembler\nTags: c++, design-patterns, assembly, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm making an 8051 assembler.\n\nBefore everything is a tokenizer which reads next tokens, sets error flags, recognizes EOF, etc.\n\nThen there is the main loop of the compiler, which reads next tokens and check for valid mnemonics:\n\n```\nmnemonic= NextToken();\nif (mnemonic.Error)\n{\n //throw some error\n}\nelse if (mnemonic.Text == \"ADD\")\n{\n ...\n}\nelse if (mnemonic.Text == \"ADDC\")\n{\n ...\n}\n```\n\nAnd it continues to several cases. Worse than that is the code inside each case, which checks for valid parameters then converts it to compiled code. Right now it looks like this:\n\n```\nif (mnemonic.Text == \"MOV\")\n{\n arg1 = NextToken();\n if (arg1.Error) { /* throw error */ break; }\n arg2 = NextToken();\n if (arg2.Error) { /* throw error */ break; }\n\n if (arg1.Text == \"A\")\n {\n if (arg2.Text == \"B\")\n output For each of the mnemonics I have to check for valid parameters then create the correct compiled code. Very similar codes for checking the valid parameters for each mnemonic repeat in each case.\n\nSo is there a design pattern for improving this code?\n\nOr simply a simpler way to implement this?\n\nEdit: I accepted plinth's answer, thanks to him. Still if you have ideas on this, i will be happy to learn them. Thanks all.\n\n========================================\n\nTop Answer:\nYes. Most assemblers use a table of data which describes the instructions: mnemonic, op code, operands forms etc.\n\nI suggest looking at the source code for `as`. I'm having some trouble finding it though. Look here. (Thanks to Hossein.)\n\n========================================\n\nCode:\n```text\nmnemonic= NextToken();\nif (mnemonic.Error)\n{\n    //throw some error\n}\nelse if (mnemonic.Text == \"ADD\")\n{\n    ...\n}\nelse if (mnemonic.Text == \"ADDC\")\n{\n    ...\n}\n```\n\n```text\nif (mnemonic.Text == \"MOV\")\n{\n    arg1 = NextToken();\n    if (arg1.Error) { /* throw error */ break; }\n    arg2 = NextToken();\n    if (arg2.Error) { /* throw error */ break; }\n\n    if (arg1.Text == \"A\")\n    {\n        if (arg2.Text == \"B\")\n            output << 0x1234; //Example compiled code\n        else if (arg2.Text == \"@B\")\n            output << 0x5678; //Example compiled code\n        else\n            /* throw \"Invalid parameters\" */\n    }\n    else if (arg1.Text == \"B\")\n    {\n        if (arg2.Text == \"A\")\n            output << 0x9ABC; //Example compiled code\n        else if (arg2.Text == \"@A\")\n            output << 0x0DEF; //Example compiled code\n        else\n            /* throw \"Invalid parameters\" */\n    }\n}\n```\n\n```text\n[label:] [instruction|directive][newline]\n```\n\n```text\nplain-mnemonic|mnemonic-withargs\n```\n\n```text\nplain-directive|directive-withargs\n```\n\n```text\n.define kMagicNumber 0xdeadbeef\nCMPA #(2 * kMagicNumber + 1)\n```\n\n```text\nenum {\n    Implied = 1, Direct = 2, Extended = 4, Indexed = 8 // etc\n} AddressingMode; \n\n/* for a 4 char mnemonic, this struct will be 5 bytes.  A typical small processor\n * has on the order of 100 instructions, making this table come in at ~500 bytes when all\n * is said and done.\n * The time to binary search that will be, worst case 8 compares on the mnemonic.\n * I claim that I/O will take way more time than look up.\n * You will also need a table and/or a routine that given a mnemonic and addressing mode\n * will give you the actual opcode.\n */\n\nstruct InstructionInfo {\n    char Mnemonic[4];\n    char AddessingMode;\n}\n\n/* order them by mnemonic */\nstatic InstructionInfo instrs[] = {\n    { {'A', 'D', 'D', '\\0'}, Direct|Extended|Indexed },\n    { {'A', 'D', 'D', 'A'}, Direct|Extended|Indexed },\n    { {'S', 'U', 'B', '\\0'}, Direct|Extended|Indexed },\n    { {'S', 'U', 'B', 'A'}, Direct|Extended|Indexed }\n}; /* etc */\n\nstatic int nInstrs = sizeof(instrs)/sizeof(InstrcutionInfo);\n\nInstructionInfo *GetInstruction(char *mnemonic) {\n   /* binary search for mnemonic */\n}\n\nint InstructionSize(AddressingMode mode)\n{\n    switch (mode) {\n    case Inplied: return 1;\n    / * etc */\n    }\n }\n```\n\n```text\nchar *line = ReadLine();\nint nextStart = 0;\nint labelLen;\nchar *label = GetLabel(line, &labelLen, nextStart, &nextStart); // may be empty\nint mnemonicLen;\nchar *mnemonic = GetMnemonic(line, &mnemonicLen, nextStart, &nextStart); // may be empty\nif (IsOpcode(mnemonic, mnemonicLen)) {\n    AddressingModeInfo info = GetAddressingModeInfo(line, nextStart, &nextStart);\n    if (IsValidInstruction(mnemonic, info)) {\n        GenerateCode(mnemonic, info);\n    }\n    else throw new BadInstructionException(mnemonic, info);\n}\nelse if (IsDirective()) { /* etc. */ }\n```\n\n```text\nas\n```\n\n```text\nInstruction\n```\n\n```text\nInstruction\n```\n\n```text\nArithmetic_Instruction\n```\n\n```text\nBranch_Instruction\n```\n\n========================================\n\nComments:\n- @wallyk: I already thought of a look-up table which contains mnemonics, number of parameters for each mnemonic, their types, etc. But speed and memory are important for me here, as it's going to be run on a machine with little ram and slow processor. So i think it will be my last choice.\n- @Hossein: A lookup table would probably take no more memory than having multiple copies of the case code that you current have. As for performance, you probably wouldn't notice any difference. Your code is already using string comparisons and function calls, so it's not like you are optimizing a tight inner loop. Remember the 80/20 rule.\n- The table-driven approach is heavily tried and true for small memory, slow processor implementations. Consider how much code it takes to set up each comparison in your approach. In the table-driven approach, there is only one bit of code which does the comparison for, say, the mnemonic. It's inside a loop which is no slower than a sequence of literal comparisons. Try both: I am confident you'll find the table is superior in most ways.\n- If speed and memory are your concerns you should definitely use a table. One of the fastest assemblers for the Apple II came in at around 8K total and ran at several thousand lines per minute on a 1MHz machine. What are your memory limitations? I'd be surprised if you couldn't describe the entire instruction set and its addressing modes in under 1K.\n- \"Smart data structures and dumb code works a lot better than the other way around.\" -- *The Cathedral and the Bazaar* (en.wikipedia.org/wiki/The_Cathedral_and_the_Bazaar)\n- Are you sure? I see Visitors mostly used in trees (like the T in AST). You don't exactly need/have a tree in an assembler, as opposed to a compiler for a larger language.\n- No but you could have a Mnemonic class that could define its output code through a visitor pattern. You loop the Mnemonics, pass them the visitor, they return their own output. I never designed an assembler, so it's just a guess heh.\n- I'd rather stay away from \"too much\" object-oriency because of performance issues.\n- Does the Dragon Book discuss assemblers too? I'm still trying to find a downloadable version of it or get a translated version but having succeeded.\n- My memory is about 200 KiB and I'm so happy you replaced the previous object-oriented code with this one! Now a -up (I know...) question: Was your previous way using `Instruction8051 { public string Mnemonic { get; set; } public List Info { get; set; } }` the Command Pattern? And are there any reasons/situations I prefer Command Pattern to Look-up Tables?\n- Command Pattern? No - both of them would've been geared around look up tables. The earlier C# code is just one that takes advantage of the language's support for generic collections and auto properties.","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":213,"estimatedTokens":1901}}550{"id":"stack-6482578","source":"stackoverflow","questionId":6482578,"title":"Can LLVM jitter emit the native code in continus memory addresses?","tags":["compiler-construction","llvm","jit"],"text":"Title: Can LLVM jitter emit the native code in continus memory addresses?\nTags: compiler-construction, llvm, jit\nSource: Stack Overflow\n\nQuestion:\nI have question relating to LLVM Jitter:\nCan i obligue the LLVM Jitter to emit the native code in continuous memory addresses ? and to be PIC ?\nwhat i want to do is to move save the JIT code in a file and load it for execution later ..\n\nwhat i mean by \"load\" is to simply read the bits from file into buffer i don't want to generate elf or something like this.\n\nHere's an example:\nsuppose i have C source file which contain:\n\n```\nGlobal variables\n----------------\nFunction Foo()\n----------------\nFunction Too()\n```\n\nwhen i request the JIT code i want the JIT to be in continus memory addresses:\n\n```\n0x100: Global Vars (take 16 Byte)\n0x110: Foo() Code (take 32 Byte)\n0x130: Too() Code (take 32 Byte)\n0x150: end.\n```\n\n========================================\n\nCode:\n```text\nGlobal variables\n----------------\nFunction Foo()\n----------------\nFunction Too()\n```\n\n```text\n0x100: Global Vars (take 16 Byte)\n0x110: Foo() Code (take 32 Byte)\n0x130: Too() Code (take 32 Byte)\n0x150: end.\n```\n\n```text\ninclude/llvm/ExecutionEngine/JITMemoryManager.h\n```\n\n```text\nlib/ExecutionEngine/JIT/JITMemoryManager.cpp\n```\n\n```text\nunittests/ExecutionEngine/JIT/JITTest.cpp\n```\n\n```text\nRecordingJITMemoryManager\n```\n\n```text\nisPIC\n```\n\n========================================\n\nComments:\n- Why do you want to use JIT to do this? It sounds like a job for regular compiler. Why are you avoiding ELfs? Creating shared library would save you a lot of work. NSAM can output Flat-Form Binary but I doubt executing them would be that easy.\n- i don't see full support of elf in LLVM for X86, am i wrong ? the 2nd problem that if i choose to generate elf i need loader in order to execute the code - and this what i am trying to avoid. the problem is that i want to execute the jit in another Process so i need to move the JIT code to the execution process.\n- IIRC, x86 ELF should be one of the best-supported code generators, along with PowerPC. llvm.org seems to have a downtime right now, so I can't check. (Edit: Found 2.9 Release notes in the Google cache, search for \"known problems with the x86 backend\" and observe how small it is: webcache.googleusercontent.com/&hellip;)\n- yes i see that, the problem is that i still use LLVM2.8 rightnow and it seems there's no support for ELF. Morever as i said assume that i have an ELF file i still need loader, so i am still avoiding this way and turn to something like \"JIT\"","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":7,"totalLines":76,"estimatedTokens":636}}551{"id":"stack-4735995","source":"stackoverflow","questionId":4735995,"title":"What is hierarchy for these grammars: LL1,SLR1,LR0,LR1 and LALR1?","tags":["parsing","compiler-construction"],"text":"Title: What is hierarchy for these grammars: LL1,SLR1,LR0,LR1 and LALR1?\nTags: parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nHow are these grammars related to each other? e.g. SLR is a subset of LALR1. Is there any set methods for working out the relationships between the others?\n\n========================================\n\nComments:\n- Nice answer. Is LL(1) < LALR(1)? If yes, then is LL(1) < SLR(1) too?\n- Answer to comment above: stackoverflow.com/questions/6487588/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":12,"estimatedTokens":124}}552{"id":"stack-16405558","source":"stackoverflow","questionId":16405558,"title":"Can a left associative operator be expressed in a way such that top-down LL(1) parsers can understand?","tags":["parsing","context-free-grammar","compiler-construction"],"text":"Title: Can a left associative operator be expressed in a way such that top-down LL(1) parsers can understand?\nTags: parsing, context-free-grammar, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI was trying to implement a LL(1) top-down parser for a calculator language. It only allows us to sum, subtract, divide and multiply numbers. No parentheses.\n\n```\nS -> A\n\nA -> B + A\n | B - A\n | B\n\nB -> int * B\n | int / B\n | int\n```\n\nAs this grammar is not suited to a LL(1) parser, I had to change it quite a bit:\n\n```\nS -> A\n\nA -> B A'\nA'-> + A\n | - A\n | λ\n\nB -> int B'\nB'-> * B\n | / B\n | λ\n```\n\nThe problem is that now the grammar is not left associative for the 4 shown operators, and I need it to be so. How to solve this problem? Is it even possible to accomplish so?\n\n========================================\n\nCode:\n```text\nS -> A\n\nA -> B + A\n   | B - A\n   | B\n\nB -> int * B\n   | int / B\n   | int\n```\n\n```text\nS -> A\n\nA -> B A'\nA'-> + A\n   | - A\n   | λ\n\nB -> int B'\nB'-> * B\n   | / B\n   | λ\n```\n\n```text\nfunction A() {\n   val = B();\n   t = peek();\n   while (t=='+' || t=='-') {\n     match(t);\n     val1 = B();\n     if (t=='+')\n       val = val + val1;\n     else\n       val = val - val1;\n     t = peek();\n   }\n   return(val)\n}\n```\n\n```text\npeek()\n```\n\n```text\nmatch()\n```\n\n========================================\n\nComments:\n- I suppose that you're not looking for the answer \"don't use an LL(1) parser, then\" :). But that's the reality: `LL(1)` parsers are not a good match for parsing expressions; if you don't want to use `LR(1)` for some reason, write a Pratt parser or a operator precedence parser (see \"Shunting Yard algorithm\")\n- Well, I'm just learning about parsers. I intended in trying to implement a simple calculator language for several kinds of parsers. Are you stating that it's not possible to accomplish a calculator with a LL(1)?\n- I'm not stating that it's impossible, just that it's not trivial. You can do it by using the LL(1) parser to generate a parse tree for the modified grammar, and then reverse the transformation on the parse tree to create the parse tree for the original grammar.\n- Oh, I was asking whether it was possible without that kind of gimmick.\n- Are you aiming for a table-driven parser or a handwritten recursive-descent parser? If the latter, there's a fairly straightforward way to implement it (by replacing recursion with iteration).\n- @ebohlman: Hand-written.\n- Hi, I need to generate a LL(1) **parse table**, instead of ad-hoc code. Is it possible? Thanks!","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":5,"totalLines":101,"estimatedTokens":629}}553{"id":"stack-16423930","source":"stackoverflow","questionId":16423930,"title":"Mixing mark and sweep with reference counting","tags":["compiler-construction","garbage-collection","runtime"],"text":"Title: Mixing mark and sweep with reference counting\nTags: compiler-construction, garbage-collection, runtime\nSource: Stack Overflow\n\nQuestion:\nI'm designing a very simple compiler (sort of academic research) and I'm thinking on implementing a simple reference counting GC and also a Mark and sweep. The idea is that reference counting can free dead objects very early when there are no cycles and also this leaves to the following idea: Mark and sweep usually involves some pauses because of the mark and sweep process has to iterate through a lot of references so: Shouldn't a previuos ref-counting make the mark and sweep faster (less elements)?\nIs this idea nonsense? I've never implemented such a complex thing before and I don't want to work a lot just to find it was a very bad idea.\n\n========================================\n\nTop Answer:\nI intuitively agreed at first, but I came to the conclusion that this intuition is wrong. While ref-counting will remove some garbage before the mark and sweep GC (I'll call it msgc for short), this does not help msgc performance much. The marking phase never even looks at garbage, so early removal of garbage via refcounting doesn't speed up marking. I'm not too sure about the sweeping phase, as this depends on how you implement it, but I can imagine several strategies which are not affected by the amount of garbage enough to make this worthwhile.\n\nConsidering the added complexity, it's probably not worth it. You won't get too much performance out of a simple msgc anyway, and the added costs of refcounts (larger object header, slower assignment, etc.) diminish the gain if there even is one.\n\n========================================\n\nCode:\n```text\nIDisposable\n```\n\n```text\nreadonly\n```\n\n========================================\n\nComments:\n- I think this is more or less what CPython does. docs.python.org/2/library/gc.html\n- @larsmans I never dove into the details, but from lurking on python-dev I get the impression that there are some significant differences between CPython's cycle collector and textbook mark and sweep GCs. Moreover, this doesn't necessarily mean it's better than plain mark and sweep: CPython may have both because it started with just refcounting, and when collecting cycles became important, removing refcounting was already out of reach (it certainly is now).\n- Thanks, that makes perfect sense. And what's more important, can make development easier.\n- Also, refcounting has a spread out overhead of, you know, counting the refs. So, I would imagine a hybrid system would be a net loss in any case.","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":33,"estimatedTokens":646}}554{"id":"stack-30512879","source":"stackoverflow","questionId":30512879,"title":"Register Allocation in Compilers","tags":["compiler-construction","code-generation","cpu-registers"],"text":"Title: Register Allocation in Compilers\nTags: compiler-construction, code-generation, cpu-registers\nSource: Stack Overflow\n\nQuestion:\nWhat is meant by spilling of registers or spill code which appears in Register allocation phase of Code generation where compiler backend must allocate variables to memory or registers?.\n\n========================================\n\nCode:\n```text\na := 1   ; liveout: {a}\nb := 2   ; liveout: {a,b}\nc := 3   ; liveout: {a,b,c}\nd := a + b + c\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\nd\n```\n\n```text\nc\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\nc\n```\n\n```text\nd\n```\n\n```text\nrax\n```\n\n```text\nxmm0\n```\n\n========================================\n\nComments:\n- Awesome explanation, so compiler too uses heuristics to estimate spilling of registers? Amazing,can you point to some resource about the heuristic-algorithm used there. Thanks,and upvoted.\n- I haven't been able to find any accessible resources on this. Cooper & Torczon suggest that loop nesting can be used as an estimate. Basically, we assume each loop executes N times (we might choose N=10). A variable referenced in a loop is therefore assigned a cost of N times the number of memory operations required to store and load it. Similarly, a variable referenced in a doubly nested loop would have a cost of N^2 times the number of operations.","metadata":{"transformedAt":"2026-08-18T18:32:17.807Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":11,"totalLines":62,"estimatedTokens":333}}555{"id":"stack-10679632","source":"stackoverflow","questionId":10679632,"title":"Creating ELF binaries without using libelf or other libraries","tags":["c","linux","compiler-construction","elf"],"text":"Title: Creating ELF binaries without using libelf or other libraries\nTags: c, linux, compiler-construction, elf\nSource: Stack Overflow\n\nQuestion:\nRecently I tried to write a simple compiler on the linux platform by myself.\n\nWhen it comes to the backend of the compiler, I decided to generate ELF-formatted binaries without using a third-party library, such as `libelf`.\n\nInstead I want to try to write machine code directly into the file coresponding to the ELF ABI just by using the `write()` function and controlling all details of the ELF file.\n\nThe advantage of this approach is that I can control everything for my compiler.\n\nBut I am hesitating. Is that way feasible, considering how detailed the ELF ABI is?\n\nI hope for any suggestions and pointers to good available resources available.\n\n========================================\n\nCode:\n```text\nlibelf\n```\n\n```text\nwrite()\n```\n\n```text\nPT_LOAD\n```\n\n========================================\n\nComments:\n- Ah yes, the old \"I want to write my own encyclopedia, because then I don't have to worry about any inaccuracies in the one that's been peer vetted, proofread by many thousands of people - I can do it better\" argument.\n- @Marc - For production software I totally agree. But if you're already writing your own compiler to learn about compiler writing, why not go all the way and do the code generation and output yourself as well. Could be a very interesting learning experience.\n- That would be the job of the linker, not the compiler.\n- Thannks for your suggestion. Can you recommend any info for me to study ELF file format?\n- Added one link that should be helpful.","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":40,"estimatedTokens":407}}556{"id":"stack-6515358","source":"stackoverflow","questionId":6515358,"title":"What instruction set is used by Tilera microprocessors?","tags":["compiler-construction","assembly","multicore","cpu-architecture"],"text":"Title: What instruction set is used by Tilera microprocessors?\nTags: compiler-construction, assembly, multicore, cpu-architecture\nSource: Stack Overflow\n\nQuestion:\nIs there any documentation on this? I'm trying to get a handle on the feasibility of writing a compiler for the Tilera architecture.\n\n========================================\n\nTop Answer:\nRephrasing this answer, for clarity.\n\nMany references on the web claim they're supposedly MIPS derivates; on the other hand, neither has MIPS' own licensee/partner list any mention of Tilera, nor does Tilera itself mention MIPS in any of their product documentation. Were there some sort of (cross-)licensing agreement, this avoidance would be hard to understand.\n\nWhen you look into the Linux kernel which supports both MIPS and Tile (32bit only as of this writing) architectures, the differences in assembly language are significant, see for example the kernel start entry points for MIPS and Tile architectures. The Tilera assembly mnemonics to me personally look closer to PowerPC than to MIPS.\n\nTilera themselves make a GCC / binutils port available at http://www.tilera.com/scm/ which includes the sourcecode tarballs; the architecture there is called `tile-unknown-linux`.\n\nFrom this, it rather looks like they've come up with something different.\n\n========================================\n\nCode:\n```text\ntile-unknown-linux\n```\n\n========================================\n\nComments:\n- \"Close\" flag? This is fine techical question with an extremely precise answer.","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":31,"estimatedTokens":380}}557{"id":"stack-3357326","source":"stackoverflow","questionId":3357326,"title":"Objective-C preprocessor available?","tags":["c","objective-c","compiler-construction"],"text":"Title: Objective-C preprocessor available?\nTags: c, objective-c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nDoes anyone know if the source code for Objective-C is still available from when it was just a pre-processor? Would be curious to see how it was implemented back then.\n\nThanks.\n\n========================================\n\nTop Answer:\nThe only source-available implementation of Objective-C is GCC. The GCC archive at ftp.gnu.org/pub/gnu/gcc contains versions back to 1.42, while ftp.gnu.org/pub/old-gnu/gcc has some versions slightly older than that. The earliest implementations within GCC were, I believe, preprocessors, so that's probably your best bet.","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":13,"estimatedTokens":169}}558{"id":"stack-16477768","source":"stackoverflow","questionId":16477768,"title":"Why won't javac accept `x = x+++++y`?","tags":["java","compiler-construction","javac"],"text":"Title: Why won't javac accept `x = x+++++y`?\nTags: java, compiler-construction, javac\nSource: Stack Overflow\n\nQuestion:\nFrom the perspective of Compiler Theory, why would the javac compiler not accept a statement of the form `x = x+++++y` but accept `x = x+++ ++y` ?\n\n========================================\n\nCode:\n```text\nx = x+++++y\n```\n\n```text\nx = x+++ ++y\n```\n\n```text\n(x)(++)(+)( )(++)(y)\n```\n\n```text\n(x)(++)(++)(+)(y)\n```\n\n```text\n++\n```\n\n```text\nx+++ ++y\n```\n\n```text\nx+++++y\n```\n\n```text\n++\n```\n\n```text\n(x++)\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":9,"totalLines":45,"estimatedTokens":131}}559{"id":"stack-3690721","source":"stackoverflow","questionId":3690721,"title":"Creating programming languages and compiler designing. Are they related?","tags":["programming-languages","compiler-construction"],"text":"Title: Creating programming languages and compiler designing. Are they related?\nTags: programming-languages, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nAlright, I guess this question has been asked a lot of times here.\n\nI want to create a programming language, not necessarily starting today, but over a span of 2-3 yrs. I'm not a very good programmer, but I'm improving. What I wanted to ask is how closely creating a language and writing a compiler related?\n\nSince, a compiler translates a language from one form into another, I guess it's all about writing a compiler for a particular piece of text. SO if I learn compiler design, will I be able to write my own programming language?\n\n========================================\n\nTop Answer:\nNaruto, it depends on what kind of \"Language\" you want to create. If it is a simple, just-for-learning language, and you choose the grammar, etc, etc, you won't need to know a lot about programming. BUT, if you are going to deal with a serious one, you will have to study at least one computer programming language deep not only to use it, but to try to reach several of its concepts, for example, like OO, generics, lambda expressions, etc, etc.\n\nBelieve me, this is not a task of months, but a serious journey. Anyway, I wish you luck ;)\n\n========================================\n\nComments:\n- This is what I was thinking to do. I've bought Compilers:principles,techniques and tools by Aho,Lam,Sethi and Ullman. Let's see how far I get from there.","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":22,"estimatedTokens":376}}560{"id":"stack-30374904","source":"stackoverflow","questionId":30374904,"title":"Left recursion parsing","tags":["parsing","recursion","compiler-construction","recursive-descent","left-recursion"],"text":"Title: Left recursion parsing\nTags: parsing, recursion, compiler-construction, recursive-descent, left-recursion\nSource: Stack Overflow\n\nQuestion:\n### Description:\n\nWhile reading Compiler Design in C book I came across the following rules to describe a context-free grammar:\n\n a grammar that recognizes a list of one or more statements, each of\n which is an arithmetic expression followed by a semicolon. Statements are made up of a\n series of semicolon-delimited expressions, each comprising a series of numbers\n separated either by asterisks (for multiplication) or plus signs (for addition).\n\nAnd here is the grammar:\n\n```\n1. statements ::= expression;\n2. | expression; statements\n3. expression ::= expression + term\n4. | term\n5. term ::= term * factor\n6. | factor\n7. factor ::= number\n8. | (expression)\n```\n\nThe book states that this recursive grammar has a major problem. The right hand side of several productions appear on the left-hand side as in production 3 (And this property is called ***left recursion***) and certain parsers such as recursive-descent parser can't handle left-recursion productions. They just loop forever.\n\n You can understand the problem by considering how the parser decides to apply a particular production when it is replacing a non-terminal that has more than one right hand side. The simple case is evident in Productions 7 and 8. The parser can choose which production to apply when it's expanding a factor by looking at the next input symbol. If this symbol is a number, then the compiler applies Production 7 and replaces the factor with a number. If the next input symbol was an open parenthesis, the parser\n would use Production 8. The choice between Productions 5 and 6 cannot be solved in this way, however. In the case of Production 6, the right-hand side of term starts with a factor which, in tum, starts with either a number or left parenthesis. Consequently, the\n parser would like to apply Production 6 when a term is being replaced and the next input symbol is a number or left parenthesis. Production 5-the other right-hand side-starts with a term, which can start with a factor, which can start with a number or left parenthesis, and these are the same symbols that were used to choose Production 6.\n\n### Question:\n\nThat second quote from the book got me completely lost. So by using an example of some statements as (for example) `5 + (7*4) + 14`:\n\n- What's the difference between factor and term? using the same example\n\n- Why can't a recursive-descent parser handle left-recursion productions? (Explain second quote).\n\n========================================\n\nTop Answer:\nThe rule factor matches the string \"1*3\", the rule term does not (though it would match \"(1*3)\". In essence each rule represents one level of precedence. `expression` contains the operators with the lowest precedence, `factor` the second lowest and `term` the highest. If you're in term and you want to use an operator with lower precedence, you need to add parentheses.\n\nIf you implement a recursive descent parser using recursive functions, a rule like `a ::= b \"*\" c | d` might be implemented like this:\n\n```\n// Takes the entire input string and the index at which we currently are\n// Returns the index after the rule was matched or throws an exception\n// if the rule failed\nparse_a(input, index) {\n try {\n after_b = parse_b(input, index)\n after_star = parse_string(\"*\", input, after_b)\n after_c = parse_c(input, after_star)\n return after_c\n } catch(ParseFailure) {\n // If one of the rules b, \"*\" or c did not match, try d instead\n return parse_d(input, index)\n }\n}\n```\n\nSomething like this would work fine (in practice you might not actually want to use recursive functions, but the approach you'd use instead would still behave similarly). Now, let's consider the left-recursive rule `a ::= a \"*\" b | c` instead:\n\n```\nparse_a(input, index) {\n try {\n after_a = parse_a(input, index)\n after_star = parse_string(\"*\", input, after_a)\n after_b = parse_c(input, after_star)\n return after_b\n } catch(ParseFailure) {\n // If one of the rules a, \"*\" or b did not match, try c instead\n return parse_c(input, index)\n }\n}\n```\n\nNow the first thing that the function `parse_a` does is to call itself again at the same index. This recursive call will again call itself. And this will continue ad infinitum, or rather until the stack overflows and the whole program comes crashing down. If we use a more efficient approach instead of recursive functions, we'll actually get an infinite loop rather than a stack overflow. Either way we don't get the result we want.\n\n========================================\n\nCode:\n```text\n1. statements ::= expression;\n2.                | expression; statements\n3. expression ::= expression + term\n4.                | term\n5. term       ::= term * factor\n6.                | factor\n7. factor     ::= number\n8.                | (expression)\n```\n\n```text\n5 + (7*4) + 14\n```\n\n```text\nterm       ::= term * factor | factor\nfactor     ::= number | (expression)\n```\n\n```text\n(2*3)*4\n```\n\n```text\nA-> Ab\n```\n\n```text\nA-> AAb -> AAAb -> ... -> infinite loop of A.\n```\n\n```text\n// Takes the entire input string and the index at which we currently are\n// Returns the index after the rule was matched or throws an exception\n// if the rule failed\nparse_a(input, index) {\n  try {\n    after_b = parse_b(input, index)\n    after_star = parse_string(\"*\", input, after_b)\n    after_c = parse_c(input, after_star)\n    return after_c\n  } catch(ParseFailure) {\n    // If one of the rules b, \"*\" or c did not match, try d instead\n    return parse_d(input, index)\n  }\n}\n```\n\n```text\nparse_a(input, index) {\n  try {\n    after_a = parse_a(input, index)\n    after_star = parse_string(\"*\", input, after_a)\n    after_b = parse_c(input, after_star)\n    return after_b\n  } catch(ParseFailure) {\n    // If one of the rules a, \"*\" or b did not match, try c instead\n    return parse_c(input, index)\n  }\n}\n```\n\n```text\nexpression\n```\n\n```text\nfactor\n```\n\n```text\nterm\n```\n\n```text\na ::= b \"*\" c | d\n```\n\n```text\na ::= a \"*\" b | c\n```\n\n```text\nparse_a\n```\n\n========================================\n\nComments:\n- On the other side, I would like to suggest you to keep on querying The Dragon Book as a reference for theoretical topics of compiler. That is a must read book for every compiler-enthusiasts.\n- Can you think of an expression (of decimal numbers) that might cause the infinite looping (as an example) ??\n- No,it's not like that. It's actually that \"while expanding the terms, if the non-terminal doesn't find any match, it keeps on expanding which may lead to infinite looping\". You can yourself assume such grammar first and then think about the expression for allowing infinite looping. Like the one I mentioned in my answer. Complete that grammar with an extra production. @Algo","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":14,"totalLines":179,"estimatedTokens":1702}}561{"id":"stack-5359391","source":"stackoverflow","questionId":5359391,"title":"Generating intermediate code in a compiler. Is an AST or parse tree always necessary when dealing with conditionals?","tags":["parsing","language-design","compiler-construction","intermediate-code"],"text":"Title: Generating intermediate code in a compiler. Is an AST or parse tree always necessary when dealing with conditionals?\nTags: parsing, language-design, compiler-construction, intermediate-code\nSource: Stack Overflow\n\nQuestion:\nI'm taking a compiler-design class where we have to implement our own compiler (using flex and bison). I have had experience in parsing (writing EBNF's and recursive-descent parsers), but this is my first time writing a compiler.\n\nThe language design is pretty open-ended (the professor has left it up to us). In class, the professor went over generating intermediate code. He said that it is not necessary for us to construct an Abstract Syntax Tree or a parse tree while parsing, and that we can generate the intermediate code as we go.\n\nI found this confusing for two reasons:\n\nWhat if you are calling a function *before* it is defined? How can you resolve the branch target? I guess you would have to make it a rule that you have to define functions before you use them, or maybe pre-define them (like C does?)\n\nHow would you deal with conditionals? If you have an `if-else` or even just an `if`, how can you resolve the branch target for the `if` when the condition is `false` (if you're generating code as you go)?\n\nI planned on generating an AST and then walking the tree after I create it, to resolve the addresses of functions and branch targets. Is this correct or am I missing something?\n\n========================================\n\nTop Answer:\nThe Crenshaw tutorial is a concrete example of *not* using an AST of any kind. It builds a working compiler (including conditionals, obviously) with immediate code generation targeting m68k assembly.\n\nYou can read through the document in an afternoon, and it is worth it.\n\n========================================\n\nCode:\n```text\nif-else\n```\n\n```text\nif\n```\n\n```text\nif\n```\n\n```text\nfalse\n```\n\n========================================\n\nComments:\n- Alternately you can emit assembly code and let the assembler worry about that part of the problem. Or is that cheating?\n- Sure, then the assembler will need to make two passes (or one and a half if it uses patching) over the code. Many compilers, FORTH in particular, target binary machine code directly. This could be for performance (both speed and space) or other pragmatic reasons (such as no available assembler).\n- Some of us push the patching onto the linker. This means you can spit an object code stream (with placeholders for patches; I actually link the patch values together when I can) and issue the patches when the final location is known, at least for a very fast, one-pass compiler I built.","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":49,"estimatedTokens":660}}562{"id":"stack-37611247","source":"stackoverflow","questionId":37611247,"title":"x86 instruction encoding how to choose opcode","tags":["assembly","compiler-construction","x86-64","disassembly"],"text":"Title: x86 instruction encoding how to choose opcode\nTags: assembly, compiler-construction, x86-64, disassembly\nSource: Stack Overflow\n\nQuestion:\nWhen encode instruction`cmpw %ax -5` for x86-64, from Intel-instruction-set-reference-manual, I have two opcodes to choose:\n\n```\n3D iw CMP AX, imm16 I Valid Valid Compare imm16 with AX.\n83 /7 ib CMP r/m16, imm8 MI Valid Valid Compare imm8 with r/m16.\n```\n\nSo there will be two encoding results:\n\n```\n66 3d fb ff ; this for opcode 3d\n66 83 f8 fb ; this for opcode 83\n```\n\nThen which one is better? \n\nI tried some online-disassembler below\n\nhttps://defuse.ca/online-x86-assembler.htm#disassembly2\nhttps://onlinedisassembler.com/odaweb/\n\nBoth can disassemble to origin instruction. But why `6683fb00` also works and `663dfb` doesn't.\n\n========================================\n\nCode:\n```text\n3D iw CMP AX, imm16 I Valid Valid Compare imm16 with AX.\n83 /7 ib CMP r/m16, imm8 MI Valid Valid Compare imm8 with r/m16.\n```\n\n```text\n66 3d fb ff ; this for opcode 3d\n66 83 f8 fb ; this for opcode 83\n```\n\n```text\ncmpw %ax -5\n```\n\n```text\n6683fb00\n```\n\n```text\n663dfb\n```\n\n```text\nimm16\n```\n\n```text\n66\n```\n\n```text\n3D imm32\n```\n\n```text\nimm8\n```\n\n```text\n663d\n```\n\n```text\ncmp ax, imm16\n```\n\n```text\n663dfb\n```\n\n```text\n66 3D\n```\n\n========================================\n\nComments:\n- There can be no \"better\" unless you say what's important to you. I can think of three dimensions: code size, execution speed, and compatibility/portability. Size seems to be the same, so it's not better there. There's probably more. What do you want to achieve?\n- Without looking into this too far, one instruction seems to compare AX (a 16-bit register) with a 16-bit value, whereas the other compares a different (16-bit) register with an 8-bit value.\n- In the second variant, the prefix isn't length-changing.\n- @unwind maybe execution speed is more important for me since I am working on generate object file in a compiler directly.\n- @IraBaxter From the perspective of assembler, which should choose?\n- @harold so the second is better? I think gas and llvm will choose the second one.\n- In this case don't use a 16-bit immediate value if you don't have to. There is quite a penalty for the Length Changing prefix in 64-bit code. The Intel optimization manual has a rule to avoid an LCP stall like this: *Assembly/Compiler Coding Rule 21. (MH impact, MH generality) Favor generating code using imm8 or imm32 values instead of imm16 values.*\n- @Neil that doesn't matter if he's using -5 as the operand though\n- @MichaelPetch thanks. I will refer to the manual.\n- Awesome answer for the extra reference!","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":13,"totalLines":98,"estimatedTokens":657}}563{"id":"stack-10691035","source":"stackoverflow","questionId":10691035,"title":"Method Type Inference in the Java Specification","tags":["java","compiler-construction","type-inference","specifications"],"text":"Title: Method Type Inference in the Java Specification\nTags: java, compiler-construction, type-inference, specifications\nSource: Stack Overflow\n\nQuestion:\nI am currently writing a Java compiler and have implemented section 15.12.2.7. of the JLS7 (http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.7) , one of the most annoying sections of the spec. I still have one problem since the spec somehow seems underspecified or ambiguous. My problem is this line:\n\n*lcta(U) = ? if U's upper bound is Object, otherwise ? extends lub(U,Object)*\n\nU is an arbitrary type expression. What is the upper bound of a type expression? In addition, why is the lcta always a wildcard?\n\nThe spec defines\n\n*CandidateInvocation(G) = lci(Inv(G))*.\n\nNow, for example, consider the case that Inv(G) = { List }, i.e., the only possible candidate invocation is a single parameterized type. Now, due to the rule\n\n*lci(G) = G*,\n\nthe result of CandidateInvocation( G ) =\nlci( { List } ) would be defined as:\n\nList\n\nin my opinion, lcta should simply return String here, because if List is the only possible invocation, it is a good idea to infer List as the argument. However, the definition of lcta(U) dictates that the result is either ? or ? extends lub(...), so the result IS ALWAYS a wildcard. This seems strange. What am I misinterpreting here?\n\n========================================\n\nTop Answer:\nThis looks like a bug of the spec. The clause of `lcta(U)` didn't exist in JSL3. Apparently JLS3's definition of `lci(e1..en)` is incomplete when `n=1`, and the new spec tries to fix it. But the fix seems gibberish, as you reasoned. \n\nJavac7 calculates `lci( { List } )` as `List`, ignoring the added clauses.\n\nThis problem should be raised to spec maintainers; not sure how to contact them. You could try openjdk *compiler-dev* mailing list; there are some knowledgeable people on it.\n\n========================================\n\nCode:\n```text\nlcta(U)\n```\n\n```text\nlci(e1..en)\n```\n\n```text\nn=1\n```\n\n```text\nlci( { List<String> } )\n```\n\n```text\nList<String>\n```\n\n========================================\n\nComments:\n- I have now implemented lcta(U) = U which seems to work fine. I will report if I find any cases where this implementation produces surprising results. By the way: Isn't lub(U,Object) always U, since Object is always a super class of U and thus the minimized erased candidate MEC of {U, Object} should always yield U or one of its subclasses? Thus, the rule is really complete crap. The only thing which it might be good for is transforming ? extends Object to ?. However, since U is a type expression, it cannot contain wildcards so this case may never arise... really strange.\n- and good idea with the mailing list, I think I will try it there\n- lub = least upper bound. I think any type should be lower,i.e.,\"less\", than object. Otherwise, the call of lub(U,Object) would be even more moronic since it could simply be replaced by Object.","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":64,"estimatedTokens":738}}564{"id":"stack-1512117","source":"stackoverflow","questionId":1512117,"title":"Would a cloud-based compiler be feasible?","tags":["compiler-construction","cloud","cloud-platform"],"text":"Title: Would a cloud-based compiler be feasible?\nTags: compiler-construction, cloud, cloud-platform\nSource: Stack Overflow\n\nQuestion:\nWould it be any practical benefit to write a cloud-based compiler, that would spread compiled units of code on different machines in the cloud? Could there be a benefit from obtaining a software-as-a-service architecture right within the app just after compiling, or would the inherent latency make such an approach impractical?\n\n========================================\n\nTop Answer:\nI've used such a system, but it worked on a local cluster, not a cloud. But the principle would be exactly the same. Unfortunately I can't remember what it was called - but it was cool, watching your source files get farmed out to the other PCs in your department.\n\nEdit: It was called IncrediBuild.\n\n========================================\n\nCode:\n```text\ndistcc\n```\n\n```text\nmake -j\n```\n\n```text\ndistcc\n```\n\n========================================\n\nComments:\n- I have used IncrediBuild and it works quite well on LAN. Theoretically such a thing could be done in the cloud, just having the CPU resources lying around to make it work could be expensive. Incredibuild uses spare CPU from the other machines on LAN which makes a lot of sense and is free.\n- YES... that's what I was asking, if a compiler that targets distributed architectures would be feasible... writing code in one place and have it run in the cloud, as a series of weakly-linked, reusable services. I imagine it would be useful, I'm inquiring if latency would make it inoperable in the wild.\n- \"Cloud-based compiler\" as stated in the title implies a compiler that runs in the cloud, not a compiler that generates cloud-based programs. I guess I misunderstood.","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":35,"estimatedTokens":437}}565{"id":"stack-3093049","source":"stackoverflow","questionId":3093049,"title":"Different behavior of compilers with array allocation","tags":["c++","optimization","compiler-construction","compile-time-constant"],"text":"Title: Different behavior of compilers with array allocation\nTags: c++, optimization, compiler-construction, compile-time-constant\nSource: Stack Overflow\n\nQuestion:\nI recently found a interesting behaviour of g++ when compared with MSVC++ 2008. Consider this tiny program:\n\n```\n#include \n\nconst int ARR_LENGTH = 512;\n\nvoid doSomething( int iLen );\n\nint main( int argc, char** argv )\n{\n doSomething( ARR_LENGTH );\n return 0;\n}\n\nvoid doSomething( int iLen )\n{\n int iTest[iLen];\n return;\n}\n```\n\nWill it compile? What do you think? According to my knowledge of C (or C++ for that matter), this should NOT compile, since i can call the function doSomething() with any integer i want, so the size of iTest array cannot be determined at compile time. However, when i try to compile this with g++, it works just fine. Now i can understand what probably happened here - the compiler noticed that i call this function only once passing a compile-time constant as a parameter. Some serious optimizations going on here... But when i try to compile this using MSVC++ 2008, i get this:\n\n```\n1>c:\\prj\\test\\test.cpp(15) : error C2057: expected constant expression\n1>c:\\prj\\test\\test.cpp(15) : error C2466: cannot allocate an array of constant size 0\n1>c:\\prj\\test\\test.cpp(15) : error C2133: 'iTest' : unknown size\n```\n\n**My question is:** how does this comply with the definition of the language (the C standard (C++ standard))? Is it just fine for g++ to do such an optimization (which in this case is easy to see, but the first time i encountered it, it was in a large project and it did not make much sense at first sight).\n\n========================================\n\nTop Answer:\nDynamically sized arrays are a feature of C99. If your compiler supports C99 (GCC does, VC doesn't fully) - and if you throw the C99 switch -, then this will compile.\n\n========================================\n\nCode:\n```text\n#include <cstdlib>\n\nconst int ARR_LENGTH = 512;\n\nvoid doSomething( int iLen );\n\nint main( int argc, char** argv )\n{\n    doSomething( ARR_LENGTH );\n    return 0;\n}\n\nvoid doSomething( int iLen )\n{\n    int iTest[iLen];\n    return;\n}\n```\n\n```text\n1>c:\\prj\\test\\test.cpp(15) : error C2057: expected constant expression\n1>c:\\prj\\test\\test.cpp(15) : error C2466: cannot allocate an array of constant size 0\n1>c:\\prj\\test\\test.cpp(15) : error C2133: 'iTest' : unknown size\n```\n\n========================================\n\nComments:\n- possible duplicate of Variable length arrays in C++?\n- Why will it never be valid in C++? The compiler just has to adjust the stack pointer, initialize everything with the default constructor, and make sure that it gets cleaned up on scope exit, right? Not easy, but not impossible, either. It sure beats using `alloca()`.\n- Jalf said \"probably never will be valid\", and a good reason to think that is that the C++ committee just finished writing the standard for the next version of C++, they definitely considered all the features that C added in C99, and VLAs weren't accepted. See also stackoverflow.com/questions/1887097/variable-length-arrays-i&zwnj;&#8203;n-c\n- As @Ben said, it was rejected for the upcoming C++0x, so they'd have to reverse that decision in order to add it later. And it's just not as necessary in C++ as it was in C. C++ already has `std::vector` which solves many of the same problems. There's just less of a need for VLAs in C++.","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":78,"estimatedTokens":843}}566{"id":"stack-12432998","source":"stackoverflow","questionId":12432998,"title":"ILGenerator.DeclareLocal() takes a type of a class not yet compiled","tags":["c#","compiler-construction","cil","reflection.emit"],"text":"Title: ILGenerator.DeclareLocal() takes a type of a class not yet compiled\nTags: c#, compiler-construction, cil, reflection.emit\nSource: Stack Overflow\n\nQuestion:\nToying with making a compiler for my own language, I'm trying to generate some MSIL code using the Reflection.Emit framework. It works fine when using `int` when I declare local variables. However, when I want to declare a local variable of a type I have not yet compiled I get into trouble since the `DeclareLocal()` takes a `Type` as argument. That is my uncompiled class, say `A`, still needs to be defined using \n\n```\nassemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemName, AssemblyBuilderAccess.RunAndSave);\n module = assemblyBuilder.DefineDynamicModule(Filename); \n module.DefineType(name, TypeAttributes.Public | TypeAttributes.Class)\n```\n\nSo how will I ever be able to compile the following program\n\n```\nclass A {\n void M() { B b = new B(); }\n}\nclass B\n void M() { A a = new A(); }\n}\n```\n\n========================================\n\nCode:\n```text\nassemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemName, AssemblyBuilderAccess.RunAndSave);\n module = assemblyBuilder.DefineDynamicModule(Filename); \n module.DefineType(name, TypeAttributes.Public | TypeAttributes.Class)\n```\n\n```text\nclass A {\n    void M() { B b = new B(); }\n}\nclass B\n    void M() { A a = new A(); }\n}\n```\n\n```text\nint\n```\n\n```text\nDeclareLocal()\n```\n\n```text\nType\n```\n\n```text\nA\n```\n\n```text\nstatic void Main()\n{\n    var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(\n        new AssemblyName(\"foo\"), AssemblyBuilderAccess.RunAndSave);\n    var module = assemblyBuilder.DefineDynamicModule(\"foo.dll\");\n    var aType = module.DefineType(\n        \"A\", TypeAttributes.Public | TypeAttributes.Class);\n    var bType = module.DefineType(\n        \"B\", TypeAttributes.Public | TypeAttributes.Class);\n    var aCtor = aType.DefineDefaultConstructor(MethodAttributes.Public);\n    var bCtor = bType.DefineDefaultConstructor(MethodAttributes.Public);\n    CreateMethodM(aType, bType, bCtor);\n    CreateMethodM(bType, aType, aCtor);\n    aType.CreateType();\n    bType.CreateType();\n    assemblyBuilder.Save(\"foo.dll\");\n}\n\nstatic void CreateMethodM(\n    TypeBuilder thisType, Type otherType, ConstructorInfo otherCtor)\n{\n    var method = thisType.DefineMethod(\n        \"M\", MethodAttributes.Private, typeof(void), Type.EmptyTypes);\n    var il = method.GetILGenerator();\n    var local = il.DeclareLocal(otherType);\n    il.Emit(OpCodes.Newobj, otherCtor);\n    il.Emit(OpCodes.Stloc, local);\n    il.Emit(OpCodes.Ret);\n}\n```\n\n```text\nTypeBuilder\n```\n\n```text\nType\n```\n\n```text\nCreateType()\n```\n\n```text\nGetConstructor()\n```\n\n```text\nTypeBuilder\n```\n\n```text\nConstructorBuilder\n```\n\n========================================\n\nComments:\n- @phoog: he might as well wait for someone to do his work, then..\n- Im not actually doing C# here.. it was just an example of circular dependency\n- Languages often require that a declaration is known before usage. Pascal and C++ are good examples. If you don't want this restriction then you'll need to create a multi-pass compiler.\n- @Hans Im not sure how this would be possible using the Reflection.Emit framework. I need to generate a class such that I can refer to its TYPE, when doing calls and assignment. But with circular definitions as above none of the two classes will ever be fully constructed before the other.\n- You can fix it by generating the code for a type when you encounter it and it isn't emitted yet. You may end up nesting several levels deep.\n- Argh! My solution is so close to yours! I was blinded by wanting to call \"t.DeclaringType\" rather than simply \"t\"\n- `DeclaringType` is something different, it represents the type this type was declared in, for example when it's a nested type.","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":13,"totalLines":124,"estimatedTokens":951}}567{"id":"stack-14617067","source":"stackoverflow","questionId":14617067,"title":"Collecting LLVM Edge Profiling with llvm-prof","tags":["compiler-construction","profiling","llvm"],"text":"Title: Collecting LLVM Edge Profiling with llvm-prof\nTags: compiler-construction, profiling, llvm\nSource: Stack Overflow\n\nQuestion:\nI'm using these commands to compile the code below in order to collect\nedge/blocks profiling in trunk-llvm:\n\n```\nclang -emit-llvm -c sort.c -o sort.bc\nopt -insert-edge-profiling sort.bc -o sort_prof.bc\nclang sort_prof.bc -lprofile_rt -L/llvms/lib -o sort_prof\n```\n\nthen I run the program and display the profiling information using\nllvm-prof sort_prof.bc, and the result is:\n\n```\n===-------------------------------------------------------------------------===\nFunction execution frequencies:\n\n ## Frequency\n 1. 4.3e+05/708539 main\n 2. 2.8e+05/708539 quickSort\n\n NOTE: 2 functions were never executed!\n.....\n```\n\nMy question is regarding the execution frequencies. Does make any\nsense main executing 4.3e+05 times?\nWhy so? The code I'm compiling is below.\n\n```\n###################### sort.c ########################\n#include \n#include \n#include \n\nconst int MAX = 1000000;\n\nvoid swap(int* a, int* b) {\n int tmp;\n tmp = *a;\n *a = *b;\n *b = tmp;\n}\n\nint partition(int vec[], int left, int right) {\n int i, j;\n\n i = left;\n for (j = left + 1; j left) {\n r = partition(vec, left, right);\n quickSort(vec, left, r - 1);\n quickSort(vec, r + 1, right);\n }\n}\n\nint main(void) {\n\n int vet[MAX], i=0;\n\n srand(time(NULL));\n\n for (i=0; i 2) {\n printf(\"Num$[%d] = %d\\n\", i, vet[i]);\n }\n else if ((rand() % 4) > 2) {\n printf(\"Num@[%d] = %d\\n\", i, vet[i]);\n }\n else if ((rand() % 2) > 1) {\n printf(\"Num#[%d] = %d\\n\", i, vet[i]);\n }\n }\n\n return 0;\n}\n```\n\n========================================\n\nCode:\n```text\nclang -emit-llvm -c sort.c -o sort.bc\nopt -insert-edge-profiling sort.bc -o sort_prof.bc\nclang sort_prof.bc -lprofile_rt -L/llvms/lib -o sort_prof\n```\n\n```text\n===-------------------------------------------------------------------------===\nFunction execution frequencies:\n\n ##   Frequency\n  1. 4.3e+05/708539 main\n  2. 2.8e+05/708539 quickSort\n\n  NOTE: 2 functions were never executed!\n.....\n```\n\n```text\n###################### sort.c ########################\n#include <stdio.h>\n#include <stdlib.h>\n#include <time.h>\n\nconst int MAX = 1000000;\n\nvoid swap(int* a, int* b) {\n  int tmp;\n  tmp = *a;\n  *a = *b;\n  *b = tmp;\n}\n\nint partition(int vec[], int left, int right) {\n  int i, j;\n\n  i = left;\n  for (j = left + 1; j <= right; ++j) {\n    if (vec[j] < vec[left]) {\n      ++i;\n      swap(&vec[i], &vec[j]);\n    }\n  }\n  swap(&vec[left], &vec[i]);\n\n  return i;\n}\n\nvoid quickSort(int vec[], int left, int right) {\n  int r;\n\n  if (right > left) {\n    r = partition(vec, left, right);\n    quickSort(vec, left, r - 1);\n    quickSort(vec, r + 1, right);\n  }\n}\n\nint main(void) {\n\n        int vet[MAX], i=0;\n\n        srand(time(NULL));\n\n        for (i=0; i<MAX; i++) {\n                vet[i] = rand() % 654321;\n        }\n\n        quickSort(vet, 0, MAX-1);\n\n        for (i=0; i<MAX; i++) {\n                if ((rand() % 7) > 2) {\n                        printf(\"Num$[%d] = %d\\n\", i, vet[i]);\n                }\n                else if ((rand() % 4) > 2) {\n                        printf(\"Num@[%d] = %d\\n\", i, vet[i]);\n                }\n                else if ((rand() % 2) > 1) {\n                        printf(\"Num#[%d] = %d\\n\", i, vet[i]);\n                }\n        }\n\n        return 0;\n}\n```\n\n```text\nllvm-prof sort.bc\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":172,"estimatedTokens":834}}568{"id":"stack-25676595","source":"stackoverflow","questionId":25676595,"title":"gcc intrinsic vs inline assembly : which is better?","tags":["c","gcc","assembly","compiler-construction","inline-assembly"],"text":"Title: gcc intrinsic vs inline assembly : which is better?\nTags: c, gcc, assembly, compiler-construction, inline-assembly\nSource: Stack Overflow\n\nQuestion:\nIf I want to expose a single machine specific instruction to the programmer, there are two ways I can do so : \n\n- Define a new builtin / intrinsic\n\n- Expose the same as inline assembly asm() [As its a single arithmetic type instruction, I believe there is no need for asm volatile()]\n\nI have read that builtins allow the compiler to take care of the type checking, register allocation and \"other optimizations\" etc. But the compiler will need to do this even in case of asm (), right ? So what precisely is the performance benefit of using intrinsic over asm () for a single instruction ?\n\nHow does the equation change if there are multiple machine instructions involved ?\n\nThe \"portability\" argument in favor of intrinsic is understandable, but I am curious to understand the performance advantage, if any, of one over the other.\n\n========================================\n\nComments:\n- For a single instruction I doubt there will be a noticeable difference.\n- Builtins. GCC pastes verbatim your inline assembly into the code stream, and about the only analysis it does on inline asm is the feasibility of the register constraints you place. This means no opportunity to convert the instruction to something else if it were more optimal in a given situation, or its deletion if it could be proven that the instruction is useless.\n- Inline assembly is for when you know better than the compiler, and want it to get out of the way so you can execute exactly the instructions you want, in exactly the order you want, on exactly the registers you want. This is generally a bad idea.","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":23,"estimatedTokens":434}}569{"id":"stack-8534789","source":"stackoverflow","questionId":8534789,"title":"How to make a symbol table","tags":["java","compiler-construction","symbol-table"],"text":"Title: How to make a symbol table\nTags: java, compiler-construction, symbol-table\nSource: Stack Overflow\n\nQuestion:\nWe have as an assignment to make a compiler. We have already made the lexical and syntax analysis but we are stuck at generation of intermediate code. We realized that we have to implement a symbol table in order to proceed to intermediate code generation and we don't know, how to do it and what it contains.\n\nGiven the code below, what should the symbol table contain? (The code is written in an educational language which is described below)\n\nAlso how can we implement scopes in our symbol table?\n\n```\n ::= PROGRAM ID ENDPROGRAM\n ::= { }\n ::= ε | DECLARE ENDDECLARE\n ::= ε | ID ( , ID )*\n ::= ( ) *\n ::= PROCEDURE ID ENDPROCEDURE |\nFUNCTION ID ENDFUNCTION\n ::= \n ::= ε | ( )\n ::= ( , )*\n ::= IN ID | INOUT ID\n ::= ( ; )*\n ::= ε | |\n |\n |\n |\n |\n |\n\n ::= ID := \n ::= IF THEN ENDIF\n ::= ε | ELSE \n ::= DO {} WHILE ()\n ::= (; ;;)\n{}\n ::= EXIT\n ::= CALL ID \n ::= ( ) | ε\n ::= ( , )*\n ::= IN | INOUT ID\n ::= RETURN \n ::= (OR )*\n ::= (AND )*\n ::= NOT [] | [] |\n |\nTRUE | FALSE\n ::= ( )*\n ::= ( )*\n ::= CONSTANT | () | ID \n ::= ε | \n ::= = | ) | > ( ε | = )\n ::= + | -\n ::= * | /\n ::= ε | \n```\n\n```\nPROGRAM MULTIPLY\n {\n DECLARE\n A, B, C\n ENDDECLARE\n PROCEDURE Aop(INOUT A)\n {\n A=A+1;\n }\n ENDPROCEDURE\n FUNCTION Bop(IN B){\n IF [NOT[[TRUE AND FALSE]OR[TRUE]]] THEN B := 100 / 2;\n ELSE B := 100;\n ENDIF;\n RETURN B;\n }\n ENDFUNCTION\n CALL Aop(INOUT A);\n CALL Bop(IN B);\n A := 40;\n C := A * B;\n }\nENDPROGRAM\n```\n\n========================================\n\nCode:\n```text\n<PROGRAM> ::= PROGRAM ID <BLOCK> ENDPROGRAM\n<BLOCK> ::= {<DECLARATIONS> <SUBPROGRAMS> <SEQUENCE>}\n<DECLARATIONS> ::= ε | DECLARE <VARLIST> ENDDECLARE\n<VARLIST> ::= ε | ID ( , ID )*\n<SUBPROGRAMS> ::= ( <PROCORFUNC> ) *\n<PROCORFUNC> ::= PROCEDURE ID <PROCORFUNCBODY> ENDPROCEDURE |\nFUNCTION ID <PROCORFUNCBODY> ENDFUNCTION\n<PROCORFUNCBODY> ::= <FORMALPARS> <BLOCK>\n<FORMALPARS> ::= ε | ( <FORMALPARLIST> )\n<FORMALPARLIST> ::= <FORMALPARITEM> ( , <FORMALPARITEM> )*\n<FORMALPARITEM> ::= IN ID | INOUT ID\n<SEQUENCE> ::= <STATEMENT> ( ; <STATEMENT> )*\n<STATEMENT> ::= ε | <ASSIGNMENT-STAT> |\n<IF-STAT> |\n<WHILE-STAT> |\n<FOR-STAT> |\n<EXIT-STAT> |\n<CALL-STAT> |\n<RETURN-STAT>\n<ASSIGNMENT-STAT> ::= ID := <EXPRESSION>\n<IF-STAT> ::= IF <CONDITION> THEN <SEQUENCE> <ELSEPART> ENDIF\n<ELSEPART> ::= ε | ELSE <SEQUENCE>\n<WHILE-STAT> ::= DO {<SEQUENCE>} WHILE (<CONDITION>)\n<FOR-STAT> ::= (<ASSIGNMENT-STAT>; <CONDITION>;<ASSIGNMENT-STAT>;)\n{<SEQUENCE>}\n<EXIT-STAT> ::= EXIT\n<CALL-STAT> ::= CALL ID <ACTUALPARS>\n<ACTUALPARS> ::= ( <ACTUALPARLIST> ) | ε\n<ACTUALPARLIST> ::= <ACTUALPARITEM> ( , <ACTUALPARITEM> )*\n<ACTUALPARITEM> ::= IN <EXPRESSION> | INOUT ID\n<RETURN-STAT> ::= RETURN <EXPRESSION>\n<CONDITION> ::= <BOOLTERM> (OR <BOOLTERM>)*\n<BOOLTERM> ::= <BOOLFACTOR> (AND <BOOLFACTOR>)*\n<BOOLFACTOR> ::= NOT [<CONDITION>] | [<CONDITION>] |\n<EXPRESSION> <RELATIONAL-OPER> <EXPRESSION> |\nTRUE | FALSE\n<EXPRESSION> ::= <OPTIONAL-SIGN> <TERM> ( <ADD-OPER> <TERM>)*\n<TERM> ::= <FACTOR> (<MUL-OPER> <FACTOR>)*\n<FACTOR> ::= CONSTANT | (<EXPRESSION>) | ID <IDTAIL>\n<IDTAIL> ::= ε | <ACTUALPARS>\n<RELATIONAL-OPER> ::= = | < ( ε | = | > ) | > ( ε | = )\n<ADD-OPER> ::= + | -\n<MUL-OPER> ::= * | /\n<OPTIONAL-SIGN> ::= ε | <ADD-OPER>\n```\n\n```text\nPROGRAM MULTIPLY\n    {\n    DECLARE\n    A, B, C\n    ENDDECLARE\n    PROCEDURE Aop(INOUT A)\n    {\n        A=A+1;\n    }\n    ENDPROCEDURE\n    FUNCTION Bop(IN B){\n        IF [NOT[[TRUE AND FALSE]OR[TRUE]]] THEN B := 100 / 2;\n        ELSE B := 100;\n        ENDIF;\n        RETURN B;\n        }\n    ENDFUNCTION\n    CALL Aop(INOUT A);\n    CALL Bop(IN B);\n    A := 40;\n    C := A * B;\n    }\nENDPROGRAM\n```\n\n```text\nMULTIPLY.A -> {\"LOCAL\", \"INT\", 0, 4}\nMULTIPLY.B -> {\"LOCAL\", \"INT\", 1, 4}\nMULTIPLY.C -> {\"LOCAL\", \"INT\", 2, 4}\nMULTIPLY.Aop -> {\"FUNCTION\", \"INT\", 3, 4}\nMULTIPLY.Aop.A -> {\"INOUTPARAM\", \"INT\", 0, 6}\n```\n\n```text\nA := A + 1\n```\n\n```text\nMULTIPLY.Aop\n```\n\n```text\nA\n```\n\n```text\nINT\n```\n\n========================================\n\nComments:\n- I hate it if my programming language is always shouting back at me...\n- @Bobby, then you're lucky you have not used `VT50`.\n- Have a look at cactus stacks","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":7,"totalLines":190,"estimatedTokens":1048}}570{"id":"stack-26762713","source":"stackoverflow","questionId":26762713,"title":"How can fixed-point combinators make recursive constructs terminate?","tags":["parsing","haskell","recursion","compiler-construction","combinators"],"text":"Title: How can fixed-point combinators make recursive constructs terminate?\nTags: parsing, haskell, recursion, compiler-construction, combinators\nSource: Stack Overflow\n\nQuestion:\nFixed-point combinators provide a way for anonymous functions to refer to themselves or to build mutually recursive structures. Although useful in lambda-calculus, they are essentially superfluous in modern programming languages because most if not all support recursion, lambdas and closures.\n\nAlso, fixed-point combinators can make recursive constructs like left-recursive grammar parsers terminate. Consider Lickman 1995, who proves termination for his implementation but never actually mentions *how* it works (it's just a step-by-step derivation from lattice theory to the haskell implementation) and *why* he needs fixed-point combinators in a language that already supports recursion natively.\n\nHow does it work and why does he require a fixed-point combinator?\n\n========================================\n\nTop Answer:\nSure thing. Here is a simple right-recursive grammar in three rules:\n\n```\nS -> b T\nT -> a S\nT -> a\n```\n\nThese three rules let us build a parser for recognizing these strings:\n\n```\ntype Parser = String -> (Bool, String)\ns :: Parser\ns \"\" = (False, \"\")\ns (c : cs) = if c == 'b' then t cs else (False, cs)\n\nt :: Parser\nt \"\" = (False, \"\")\nt (c : cs) \n | c == 'a' && cs == \"\" = (True, \"\") \n | c /= 'a' = (False, cs)\n | otherwise = s cs\n```\n\nIf you want to do more general parsing, just specialize the `Bool` to instead have some data structure, perhaps stored in a `Maybe` to indicate failure. Returning `(False, ___)` on a failed parse would help if S had some other rules too, like e.g. `S -> T T` and `T -> b b`. Then when we get a 'b' followed by (False, ___) we rewind to try `S -> T T`. These sorts of grammars can be done with a bit of elbow-grease and recursion.\n\nThe three rules above will successfully match strings like \"ba\", \"baba\", and so on. We could also write these strings left-recursively as:\n\n```\nS -> T a\nT -> S b\nT -> b\n```\n\nWhat happens if you try to write the same parsers above? An infinite loop, if you're looking at the front of the string. The problem is that the function S will call the function T first thing, and then T will call S first thing, and they will mutually recurse ad infinitum. The computer's not smart enough to know that the postconditions (\"followed by an a\", \"followed by a b\") make further solution impossible; it just descends into your functions and trusts that you know what you're doing.\n\nHow does a good fixed-point combinator help? Well, think of these rules as describing a tree: then function evaluation traverses that tree depth-first, and this particular tree is infinite in that direction. A breadth-first traversal, on the other hand, can be based on these rules and can pick up the result which uses the fewest of these functions possible, and that is the 'least fixed point' for a certain function based on this grammar. So that's why the *right* fixed-point combinator (based either on the `diag` in the paper or the lattice-theory combinator) can terminate when describing these rules, while naive recursion will not.\n\n========================================\n\nCode:\n```text\nfix :: (a -> a) -> a\n```\n\n```text\na\n```\n\n```text\nSet a\n```\n\n```text\nParser a\n```\n\n```text\nh :: Parser a -> Parser a\n```\n\n```text\nfixP h\n```\n\n```text\nS -> b T\nT -> a S\nT -> a\n```\n\n```text\ntype Parser = String -> (Bool, String)\ns :: Parser\ns \"\" = (False, \"\")\ns (c : cs) = if c == 'b' then t cs else (False, cs)\n\nt :: Parser\nt \"\" = (False, \"\")\nt (c : cs) \n  | c == 'a' && cs == \"\" = (True, \"\") \n  | c /= 'a' = (False, cs)\n  | otherwise = s cs\n```\n\n```text\nS -> T a\nT -> S b\nT -> b\n```\n\n```text\nBool\n```\n\n```text\nMaybe\n```\n\n```text\n(False, ___)\n```\n\n```text\nS -> T T\n```\n\n```text\nT -> b b\n```\n\n```text\nS -> T T\n```\n\n```text\ndiag\n```\n\n========================================\n\nComments:\n- Thank you for your answer. There's something I don't understand regarding your statement about the output of the fixpoint operator, though: as I understand it's a single value `x` such that `f(x) = x` for some function `f`, how is that *enough* or *not enough* for parsing to continue?\n- Very good question! Consider for a simplified example the function `f = (1 :) :: [Int] -> [Int]`. The least fixed point of `f` is an infinite lists of ones, and as such can not be computed *in its entirety*. But you don't need the whole list to be able to compute `take 5 (fix f)`! What complicates matters in the thesis is that `Parser` (and `Set`, to a lesser extent) aren't as simple as `[Int]` so laziness doesn't help automagically. Does this make sense to you?\n- I think it does: `fixP` gets me the parser that produces the biggest set of unique parse trees so that `h(p) = p` holds, where `h` is `fixP`'s first argument and `p` my parser. So `h` is just used to \"check\" that a given parser is actually equivalent to the original, unproductive parser?\n- Thank you for your answer, it does make a lot of sense. Yet, if the combinator only changes the traversal strategy from depth-first to breadth-first then why would the author not be explicit about it? Breadth-first was already used in '83.\n- Well he actually rejects `diag` (which I think does do an explicit BFS) for a better combinator based on lattice theory, so that's why there's more math. The idea of fixed points is captured by `fix f = let a = f a in a`, `fix :: (x -> x) -> x`. Call this a fixer on `x`. You can use this e.g. on `fix (const 0)` or `fix (1:)` or `fix (\\f n -> if n [B]` since `[B]` is a \"lattice\", so we get better fixers on `s -> [(a, s)]`. (Is his approach the same as the MonadFix instance for []? I don't know.)","metadata":{"transformedAt":"2026-08-18T18:32:17.808Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":141,"estimatedTokens":1428}}571{"id":"stack-40795291","source":"stackoverflow","questionId":40795291,"title":"How does the compiler know which catch block to take?","tags":["c++","exception","compiler-construction"],"text":"Title: How does the compiler know which catch block to take?\nTags: c++, exception, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nSuppose I have the following two files, `main.cpp`:\n\n```\n#include \n\nclass A {}; \nvoid foo();\n\nint main(void)\n{\n try {\n foo();\n }\n catch(const A& e) {\n std::cout and `foo.cpp`:\n\n```\nclass A {};\nclass B : public A {};\n\nvoid foo()\n{\n B b;\n throw b;\n}\n```\n\nNow, when I compile each of these files separately, link the resulting object files, and run the resulting executable, I get the expected result:\n\n```\n$ clang++ --std=c++14 -c main.cpp\n$ clang++ --std=c++14 -c foo.cpp\n$ clang++ --std=c++14 main.o foo.o\n$ ./a.out \nCaught an A.\n```\n\nAnd that boggles my mind! Class `A` has no virtual methods. Therefore, it is not polymorphic and its instances should carry no type information at runtime. The `main.o` object file is unaware of what is being thrown, since the actual throwing takes place inside `foo()`, whose body is defined in a separate compilation unit. The `foo.o` object file has more information, but is equally unaware of any catch statements and the expected types of caught exceptions.\n\nIn short: I do not see how the two source files compiled separately and then linked can produce the above input without having some runtime type information at disposal. Neither file compiled separately should have enough information to take the right catch block.\n\n========================================\n\nTop Answer:\nIt's a combination of **RTTI** (Run-time type information) and the implementation specific encoded type data that is generated for comparing types when trying to assess which catch block gets what.\n\nBut simply comparing two types at face value might not yield the proper result (such as in your case with base and derived classes). In case of Windows & SEH, a special additional extended type information structure (`etype_info` or something like that) exists that contains all the classes in the hierarchy which need to be traversed to determine a potential match (as a base class can be a limiting view into the derived class). If after traversing a match is found, catch block is invoked.\n\n**Addendum**\n\nException handling requires special runtime support provided by the OS and therefore the way this happens is implementation defined (such as Structured Exception Handling in Windows) as long as the end result satisfies the standard.\n\nUpstairs is the Windows-flavored gist of it.\n\n========================================\n\nCode:\n```cpp\n#include <iostream>\n\nclass A {};    \nvoid foo();\n\nint main(void)\n{\n    try {\n        foo();\n    }\n    catch(const A& e) {\n        std::cout << \"Caught an A.\" << std::endl;\n    }\n    return 0;\n}\n```\n\n```cpp\nclass A {};\nclass B : public A {};\n\nvoid foo()\n{\n    B b;\n    throw b;\n}\n```\n\n```text\n$ clang++ --std=c++14 -c main.cpp\n$ clang++ --std=c++14 -c foo.cpp\n$ clang++ --std=c++14 main.o foo.o\n$ ./a.out \nCaught an A.\n```\n\n```text\nmain.cpp\n```\n\n```text\nfoo.cpp\n```\n\n```text\nA\n```\n\n```text\nmain.o\n```\n\n```text\nfoo()\n```\n\n```text\nfoo.o\n```\n\n```text\ncall    __cxa_allocate_exception\n    mov     edx, 0\n    mov     esi, OFFSET FLAT:typeinfo for B   ; <== !! \n    mov     rdi, rax\n    call    __cxa_throw\n```\n\n```text\ntypeinfo\n```\n\n```text\nfoo()\n```\n\n```text\netype_info\n```\n\n========================================\n\nComments:\n- B is subclass of A, so you can view on any instance of B with \"lens\" of A.\n- why you are redefining `class A`?\n- Because of covariance\n- @Raindrop7 This behaves the same way as if it was declared in a .h file and then included in multiple files.\n- @arturx64, @freestyle – I get why it works from the perspective of the language semantics. I am interested in how the compiler actually does it behind the scenes. Since both `foo.cpp` and `main.cpp` are compiled separately, neither should have enough information to take the right catch block.\n- Hopelessly broad topic. In a nutshell: you are assuming that `throw b` throws an \"object\". Just a pointer. But are overlooking that it can also provide a *description* of the object. One that's compatible with the way `catch` checks for an object type match.\n- It depends on implementation. For instance: msvc calls the CxxThrowException method on the \"throw\" statement. The second parameter of the function is _ThrowInfo. It's a struct holding various information about the type of exception that was thrown.\n- There are a few options for Exceptions implementation. Visual C++: Structured Exception Handling (SEH), C++ Exception Handling (EH). GCC : RTTI, SjLj exceptions, Zero-cost (table based)\n- @arturx64: Visual C++ implements table-based exception handling for x64 code as well. Plus, SEH is just mechanics the CRT and compiler use to transfer control. The actual object type lookup is implemented separately and unrelated to SEH.\n- I see. I failed to consider that just because the actual object doesn't carry any type information doesn't mean that they can't be statically determined at the site of throwing and passed along with the object.\n- @Witiko It was a really interesting question. I generated code making `A` polymorphic, thinking that it would then use RTTI. But no: the compiler continues to pass its additional `typeinfo`. This is because the catching code can't know either if a polymorphic object or a non polymorphic object is thrown, and therefore a uniform interface is required.\n- How does the RTTI come into play, then, if the complete type information are passed along with the thrown object?\n- It provides type_info that is used in the comparison ops and is integrated into the exception information structure (`excpt_info` or something along those lines). Unwinding the stack while going back to the catch block, that structure is carried and used to test against the catch block type (whose type information is recorded in yet another structure recorded at compile time once it sees try-catch).\n- If you'd like to know more about the subject, you might want to explore one of the implementations like SEH, it is well documented on the web. RTTI in general as well. Hope this helps.\n- @BenjaminSisko: Reading up on SEH is of no use to answer this question. SEH are C exceptions. There are no classes or even class hierarchies in C. SEH is really just the tool used to transfer control. C++ exception handling is implemented on top of SEH in both the compiler as well as the CRT. Learning about SEH teaches you as much about C++ exception handling, as inspecting a road would give away hints about race car engines.\n- SEH is not \"C exceptions\". C++ exception handling is built on top of SEH, which means SEH contains the information pertinent to this specific question (exactly how it happens, which I provided) and is one avenue of investigation that may provide additional insights.\n- Also, your analogy is flawed. Which is hilarious as you literally explain why in the sentence before.\n- A race car engine, being C++ exception handling, can be built from more basic building blocks provided by different manufacturers (such as Microsoft's SEH). So inspecting of these building blocks can give you a insight which can be extended to others.\n- ... and I do find the information valuable. Welcome to StackOverflow, by the way. :-)\n- SEH exceptions are commonly called *\"C exceptions\"*, and while C++ exceptions are built on top of SEH, it's completely opaque. SEH exceptions carry an EXCEPTION_RECORD structure, but the C++ specific information is hidden behind, what is essentially a `void*` (*ExceptionInformation*). So reading about SEH exception handling doesn't help you understand, how C++ exceptions are implemented. For example, it doesn't tell you one thing about stack unwinding.\n- Matt Pietrek's article A Crash Course on the Depths of Win32™ Structured Exception Handling, on the other hand, does explain, how Visual Studio implements C++ exceptions. At least for x86 architectures.","metadata":{"transformedAt":"2026-08-18T18:32:17.809Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":167,"estimatedTokens":1973}}572{"id":"stack-12659117","source":"stackoverflow","questionId":12659117,"title":"How do I list all local variables within a Java method / function?","tags":["java","reflection","compiler-construction","profiling","antlr"],"text":"Title: How do I list all local variables within a Java method / function?\nTags: java, reflection, compiler-construction, profiling, antlr\nSource: Stack Overflow\n\nQuestion:\nMy main question: I know you can generically output class fields with reflection, even if you do not know the variable names, types, or even how many there are. However, is there a way to list all variables within the current function or current scope, assuming I do not know what the variable names are?\n\nIn other words:\n\n```\nint x = 5;\nint y = 42;\n// some more code\n//Now I want to println x and y, but assuming I cannot use \"x\" or \"y\".\n```\n\nI'd also be happy with an answer to this question:\nLet's say I'm allowed to store the names of all variables, does that help? e.g.:\n\n```\nSet varNames = new HashSet();\nint x = 5;\nvarNames.add(\"x\");\nint y = 42;\nvarNames.add(\"y\");\n// some more code\n//Now with varNames, can I output x and y without using \"x\" or \"y\"?\n```\n\nWhy am I asking this? I am translating XYZ language(s) to java using ANTLR, and I would like to provide a simple method to output the entire state of the program at any point in time. \n\nThird possible solution I'd be happy with: If this is not possible in Java, is there any way I can write byte-code for a function that visits the calling function and examines the stack? This would also solve the problem.\n\nWhat would be amazing is if Java had the equivalent of Python's `eval()` or php's `get_defined_vars()`. \n\nIf it makes a difference, I'm using Java 6, but anything for Java 5, 6, or 7 should be good.\n\nThanks!\n\n========================================\n\nTop Answer:\nIf you do not want to use this as part of the normal execution path of your program but just for debugging, then use the Java platform debugger architecture (JPDA). Essentially, you would write your own debugger, set a breakpoint and use the JDI API to query the state of the program. Local variables can be listed with StackFrame#visibleVariables().\n\nIf the above is not an option, it will be very difficult to achieve. To get the variables names, you could parse the class file and read the local variable table attribute of the method. However, the only way to get the value of a local variable is via the aload/iload/etc. bytecode instructions. These have to be present in the method that you want to analyze, so you cannot put this functionality into a different helper method.\n\n========================================\n\nCode:\n```text\nint x = 5;\nint y = 42;\n// some more code\n//Now I want to println x and y, but assuming I cannot use \"x\" or \"y\".\n```\n\n```text\nSet<String> varNames = new HashSet<String>();\nint x = 5;\nvarNames.add(\"x\");\nint y = 42;\nvarNames.add(\"y\");\n// some more code\n//Now with varNames, can I output x and y without using \"x\" or \"y\"?\n```\n\n```text\neval()\n```\n\n```text\nget_defined_vars()\n```\n\n========================================\n\nComments:\n- maybe this question is related (stackoverflow.com/questions/2946279/&hellip;)\n- Interesting. How does something like the eclipse debugger inspect the stack? Does it need to connect via a local network pipe to the JVM, or is it sitting in a thread that has access to the program memory?\n- @Arcymag - I don't think the debugger needs to inspect the stack; it relies on symbol files (or something similar) created at compile time.\n- could you tell me where to find examples for JPDA and active online community for JPDA ?","metadata":{"transformedAt":"2026-08-18T18:32:17.809Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":81,"estimatedTokens":849}}573{"id":"stack-340353","source":"stackoverflow","questionId":340353,"title":"Plugging in to Java compilers","tags":["java","compiler-construction","annotations","instrumentation"],"text":"Title: Plugging in to Java compilers\nTags: java, compiler-construction, annotations, instrumentation\nSource: Stack Overflow\n\nQuestion:\nI have a post-compilation step that manipulates the Java bytecode of generated classes. I'd like to make life as painless as possible for library consumers, so I'm looking at ways I can make this process automatic and (if possible) compiler agnostic.\n\nThe Annotation Processing API provides many of the desired features (automatic service discovery; supported by Eclipse). Unfortunately, this is aimed at code generators and doesn't support manipulation of existing artefacts:\n\n The initial inputs to the tool are\n considered to be created by the zeroth\n round; therefore, attempting to create\n a source or class file corresponding\n to one of those inputs will result in\n a FilerException.\n\nThe Decorator pattern recommended by the API is not an option.\n\nI can see how to perform the step with a runtime agent/instrumentation, but this is a worse option than a manual build step as it would require anyone even peripherally touched by the API to configure their JVMs in a non-obvious manner.\n\nIs there a way to plug into or wrap the compiler tool as invoked by javac? Has anyone successfully subverted the annotation processors to manipulate bytecode, no matter what the doc says?\n\n========================================\n\nTop Answer:\nIt can be done.\n\nTake a look at my blog post Roman Numerals, in our Java where an annotation processor is used to rewrite code. Limitation being that it works with Sun's javac only.\n\n========================================\n\nCode:\n```text\nExample\n```\n\n```text\nExampleTpl\n```\n\n========================================\n\nComments:\n- Your link is dead.","metadata":{"transformedAt":"2026-08-18T18:32:17.809Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":44,"estimatedTokens":430}}574{"id":"stack-7625765","source":"stackoverflow","questionId":7625765,"title":"How do I generate new source code in text form in a Scala compiler plugin?","tags":["scala","plugins","compiler-construction"],"text":"Title: How do I generate new source code in text form in a Scala compiler plugin?\nTags: scala, plugins, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have just finished the first version of a Java 6 compiler plugin, that automatically generates wrappers (proxy, adapter, delegate, call it what you like) based on an annotation.\n\nSince I am doing mixed Java/Scala projects, I would like to be able to use the same annotation inside my Scala code, and get the same generated code (except of course in Scala). That basically means starting from scratch.\n\nWhat I would like to do, and for which I haven't found an example yet, is how do I generate the code inside a Scala compiler plugin in the same way as in the Java compiler plugin. That is, I match/find where my annotation is used, get the AST for the annotated interface, and then *ask the API to give me a Stream/Writer in which I output the generated Scala source code, using String manipulation*.\n\nThat last part is what I could not find. So how do I tell the API to create a new Scala source file, and give me a Stream/Writer/File/Handle, so I can just write in it, and when I'm done, the Scala compiler compiles it, within the same run in which the plugin was invoked?\n\nWhy would I want to do that? Firstly, because than both plugins have the same structure, so maintenance is easy. Secondly, I want to open source it, and there is just no way to support every option that anyone would want, so I expect potential users to want to extend the generation with their own code. This will be a lot easier for them if they just have to do some printf(), instead of learning the AST API (this also applies to me).\n\n========================================\n\nTop Answer:\nKevin is entirely correct, but just for completeness it's worth mentioning that there is another alternative - write a compiler plugin that generates source. This is the approach that I've adopted in Borachio. It's not a very satisfactory solution, but it can be made to work.\n\n**Edit - I just reread your question and realised that you're actually asking about generating source anyway**\n\nSo there is no support for this directly, but it's basically just a question of opening a file and writing the relevant \"print\" statements. There's no way to invoke the compiler \"inside\" a plugin AFAIK, but I've written an sbt plugin which hides most of the complexity of invoking the compiler twice.\n\n========================================\n\nCode:\n```text\nTreeDSL\n```\n\n========================================\n\nComments:\n- So you can generate \"code\" as AST which the compiler will treat properly?\n- TreeDSL makes it very easy to achieve much the same effect as writing code. But be aware of the issues surrounding class/method synthesis and how this can't be done after type checking.\n- Are there conceptual reasons not to allow plugins to issue actions of the kind \"Forget name/type analysis result and redo\", or is it just not implemented?\n- Internally, the compiler uses a mutable symbol table that's shared between units. I did try a technique or running an \"early\" typer (with suppressed errors) before the plugin. Unfortunately though, this left the symbol table in an inconsistent state. I remember that class/singleton companions were especially tricky here.\n- Now I get it. It wasn't a \"compiler plugin\", but an \"sbt plugin\". I suppose it could be an ant or maven plugin instead, but the point is that it cannot be a compiler plugin, so I still can't do it like that.","metadata":{"transformedAt":"2026-08-18T18:32:17.809Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":39,"estimatedTokens":875}}575{"id":"stack-33518474","source":"stackoverflow","questionId":33518474,"title":"evaluate expression at compile time","tags":["c#","visual-studio","compiler-construction"],"text":"Title: evaluate expression at compile time\nTags: c#, visual-studio, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI know this has been asked a lot, but only for C/C++ and Java.\nThe question is related to the performance benefits of using constant expressions:\n\nWhen I call a static function with only constants as arguments, is there a way to tell the compiler that it should evaluate the call already at compile time and replace the call by the result?\n\nExample: \n\n```\nconst double pi = Math.PI; //works as Math.PI is a constant \nconst double spi = Math.Sin(Math.PI); //compiler error, because expression must be constant\n```\n\nAre there no directives (better: Attributes) to tell the compiler explicitely that a static method like Math.Sin() is not modifying nor reading any data internally, so that it was technically possible to evaluate the call at compile time?\n\nOh, and please don't answer like \"just do `const double spi = 0`\" :), because my example is just a simplified version of the problem I have: Improving code maintainability while keeping maximum performance.\n\nThanks for any help - it is really appreciated!\n\n========================================\n\nTop Answer:\nThere is the [Pure] attribute for methods which have no side effects. However, this is only used for code analysis and not by the compiler (at the moment). However, this might change in the future.\n\nJetBrains ReSharper provides a similar [Pure] attribute for the same purpose (code analysis).\n\nSo, for the moment, you need a workaround like a value pre-calculated by you, best with a comment for someone else to know the source of the value:\n\n```\nconst double spi = 0.0; // Math.Sin(Math.PI)\n```\n\nor\n\n```\nstatic readonly double spi = Math.Sin(Math.PI);\n```\n\nwhich of course calculates the value to runtime, which you don't want.\n\n========================================\n\nCode:\n```text\nconst double pi = Math.PI; //works as Math.PI is a constant  \nconst double spi = Math.Sin(Math.PI); //compiler error, because expression must be constant\n```\n\n```text\nconst double spi = 0\n```\n\n```text\nclass MyCalc\n{\n    private static readonly double spi = Math.Sin(Math.PI);\n    private static readonly double pi = Math.PI;\n\n    public void Execute()\n    {\n        // .. whatever\n    }\n}\n```\n\n```text\nclass MyCalc\n{\n    // Math.Sin(Math.Pi)\n    private const double spi = 0;\n    // Math.Pi\n    private const double pi = 3.141592653589793;\n\n    public void Execute()\n    {\n        // .. whatever\n    }\n}\n```\n\n```text\ninline\n```\n\n```text\nconst double spi = 0.0; // Math.Sin(Math.PI)\n```\n\n```text\nstatic readonly double spi = Math.Sin(Math.PI);\n```\n\n========================================\n\nComments:\n- There is no way to specify a method as **pure** function. Compiler has no way to determine if Math.Sin() doens't side effect.\n- As far as I'm aware, no. What you can do is generate the resulting code (using T4 or a custom tool), or use an IL-weaving tool to post-process the generated assembly, or create a customized compiler using Roslyn. If only C# were a Lisp this wouldn't be so hard. ;)\n- Are you trying to solve an *actual* problem or are you assuming a specific syntax related to C++ is needed here? In C# const means **const**, ie never changing after compilation. If you want an unchanging result, there are *other* ways to do it, eg a `static readonly` value, or a get-only property that returns a constant\n- @PieterWitvoet actually it is possible *and* easy. It simply isn't done the C++ way. C# treats constants as *real* constants, ie never changing ever-ever again. Which allows the compiler to embed them at the caller's site, without having to check the declaring assembly. It's actually a C++ trait that const isn't quite const.\n- @PanagiotisKanavos: I'm talking about executing code at compile-time, not about the specifics of `const`.\n- Lol, I explicitely asked to not propose solutions like `const double spi = 0` and the first two answers I got contain exactly that... :)\n- @PanagiotisKanavos: My concrete problem is that I need to implement a network interface with high focus on performance, which serializes objects into a byte-stream, including type conversions to save bandwidth. Currently I have the property to byte[] conversions for each class implemented hard-coded (sequence and type conversion) for both, the sending and the receiving side. Of course it would be much more convenient to have one list of meta-information containing the mapping and then have re-usable mehods to do the work. Reflection is unfortunately much slower than the hard-coded variant.\n- @Pieter Witvoet: I think, the code-generation approach is what I'll be going for. I could even use Excel to do it... ;)\n- @Reinski: your use-case reminds me of Google's Protocol Buffers.\n- @Pieter: I think that definitely addresses my issue, thanks for the hint! Need to check if I can use it...\n- You gave an example about numeric compile-time-constants, you are looking for an efficient way for a serialization/ deserialization and you are now looking into a language-independent protocol-definition - and you complain about unhelpful answers?\n- @Peter Schneider: I wasn't complaining - it was more sort of amusement, because I thought I made clear, it was a simplified example... And yes, despite the background of de-/serialization I was interested in an answer for the question exactly as I posed it. In fact, I am really thankful for both answers and I am only hesistating to mark yours as solution because I miss a clear \"No, this is not possible\" statement. So if you could add something like that...\n- @Reinski: maybe it was oversimplified. But: making a good question is often harder than answering one. I expanded my response in a way which might answer your question: this is a rough path to go in C#.\n- A +1 for the reference to the Pure-attribute, which might potentially be of use in the future...\n- Rules of optimization! +1\n- A +1 for the option \"static readonly fields\" which is the best workaround as far as performance is concerned.\n- ... or change language and use rust's macros, or zig's \"comptime\" expressions =)","metadata":{"transformedAt":"2026-08-18T18:32:17.809Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":116,"estimatedTokens":1527}}576{"id":"stack-10135900","source":"stackoverflow","questionId":10135900,"title":"JIT Optimizations at their finest","tags":[".net","performance","compiler-construction","jvm","jit"],"text":"Title: JIT Optimizations at their finest\nTags: .net, performance, compiler-construction, jvm, jit\nSource: Stack Overflow\n\nQuestion:\nI have read and heard a lot about how JIT compilers can make optimizations that are impossible for Native Code Compilers and that these optimizations can give huge performance boosts.\n\nSo I was wondering, what are the most important optimizations that, say, the .NET Framework or the JVM do that a native compiler cannot do? Also, how do these give huge performance boosts?\n\nI don't know whether I've phrased this question properly, guess I may have a lot of explaining to do in the comments\n\n========================================\n\nTop Answer:\nJavascript is a much better example as it isn't as friendly towards compilers as the JVM or CLR are.\n\nJIT compilers can generate concrete specialized representations for Javascript classes which is hard for static compilers as the classes can modified at any point during program execution. You can also speculatively inline the called functions based on the actual types (Dietrich Epp already explained that in his answer).\n\nThe videos at http://code.google.com/p/v8/ nicely explain those optimizations.\n\n========================================\n\nCode:\n```text\nvoid function(MyClass x)\n{\n    x.doSomething();\n    for (obj in x.getWidgets())\n        obj.doSomethingElse();\n}\n```\n\n```text\nMyClass\n```\n\n```text\nMyConcreteClass\n```\n\n```text\ndoSomething\n```\n\n```text\ngetWidgets\n```\n\n```text\ngetWidgets\n```\n\n```text\ndoSomethingElse\n```\n\n```text\nMyClass\n```\n\n```text\nMyClass\n```\n\n```text\nfunction\n```\n\n```text\nx\n```\n\n```text\nMyConcreteClass\n```\n\n```text\nMyClass\n```\n\n========================================\n\nComments:\n- @TimMedora: The link appears to talk about optimizations performed by the C# ahead-of-time compiler, not the CLR just-in-time compiler.\n- -1 for \"These are the only kinds of optimizations...\" JIT Compilers use profile guided optimization to improve callsite performance on hot program paths. (Both the MSFT CLR and Hotspot do this). That is not the \"state\" of the program but rather the dynamics of the program. JIT Compilers can use information about the machine on which they run to guide optimization. The MSFT CLR does this, and Hotspot does this. That has nothing to do with the program. JIT compilers can also use information *state* of the machine on which they run to guide optimization.\n- I don't know that any current VMs do that last one, but it is certainly possible.\n- @MichaelGraczyk: The reason I said \"these are the only kind of optimizations\" is because the optimizations you mention can be, and in fact are, supported by non-JIT compilers. GCC *will* do profile-driven optimization. The question is only about optimizations which ahead-of-time compilers can't do.\n- I'd also consider it fair to consider ahead-of-time compilers to compile code for specific machines. After all, you see it a lot in the wild, at least on Linux.\n- Some comments on this answer: 1. a C++ could inline doSomething and doSomethingElse and function if it had the source code available - 2. In c++ all classes are final/sealed by default - you need to explicitly declare a function as virtual, where as in Java all methods are virtual by default. 3. It is correct that with a c++ compiler, if doSomething and friends were virtual methods and not marked as final (per c++11), they would not be inlined, and in fact in order to get the address of the method, the compiler would have to look up in the virtual method table, (as java does without jit).\n- @matiu: It is not possible to make sealed/final classes in C++ (you can approximate them, but it's a hack). Maybe you are thinking of methods? But the discussion here is about pseudocode, and we are talking about virtual methods specifically, so it is not really relevant that C++ can inline non-virtual methods.\n- Thanks @DietrichEpp - I was thinking of the 'final' identifier for virtual methods in C++11 - en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final - I'm not sure how much/if any optimisation it allows the modern compilers to provide though.\n- @matiu: It allows compilers to replace virtual function calls with direct function calls in certain situations, and then possibly inline those same function calls. However, this is nothing that you can't already do in Java or C#, and it's a little off-topic since we are talking about differences between JIT compilers and ahead-of-time compilers.","metadata":{"transformedAt":"2026-08-18T18:32:17.809Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":92,"estimatedTokens":1113}}577{"id":"stack-36647765","source":"stackoverflow","questionId":36647765,"title":"How to transform a flow chart into an implementation?","tags":["algorithm","math","compiler-construction","build-automation"],"text":"Title: How to transform a flow chart into an implementation?\nTags: algorithm, math, compiler-construction, build-automation\nSource: Stack Overflow\n\nQuestion:\n### EDIT: INTRODUCTION\n\nTo reach out towards a broader readership, I have reformulated my original question through an elaborate (and somewhat tedious) real-life example. The original question is shown (far) below.\n\nTom has just been hired (depending on his performance during his first two working days) to Acme Inc. as a junior software engineer. His job is to implement algorithms designed by the senior software developers, in the programming language Acme++. The company follows a strict \"no `goto`\" policy by the exclusive order of the CEO. In case Tom can perform exceptionally on his probation time, he will be offered a full time job at the company.\n\nOn day 1, Tom receives the following algorithm to be implemented.\n\n```\nStep 1. START\nStep 2. Input x\nStep 3. In case xTom feels like that the task is very complicated, and he thinks that he would benefit from studying the abstract structure of the program, by representing it as a flow chart. After drawing the following diagram Flow chart of day one he quickly realizes, that he was asked to compute the absolute value of x, and he can implement it with a simple if-then-else statement. Tom is very happy, and he finishes his task by the end of the day.\n\nOn day 2, Tom receives the following algorithm to be implemented.\n\n```\nStep 1. START\nStep 2. Input x\nStep 3. In case xTom, being a newbie, feels like again it would be better to understand the algorithm in an abstract way, so he draws the following flow chart Flow chart of day two.\n\nInspection of the flow chart reveals that Tom was asked to implement a while loop which waits for the first nonnegative input. Tom is very happy, and finishes his task by the end of the day.\n\nBased on his outstanding performance, Tom has been hired to the company. \n\nOn day 3, however, Tom is being thrown in at the deep end as he receives a 1000 line algorithm with 1996 `goto` jumps, designed by a former employee of the company, and there is noone else left there who would know what the algorithm does, how it does it, and why was it designed in such a way in the first place. However, this does not concerns Tom at all, as his sole task is to implement the algorithm irrespectively of what it is. Armed with the previous two day's of expertise, he draws the flow graph on 1000 nodes with 1997 directed edges. Tom, being very desperate, asks on stackoverflow what the heck to do with such a mess, where experienced programmers repeatedly advise him to\n\n- break up the program into smaller pieces; and\n\n- he got reassured that in certain cases it is actually ok to use `goto`; and\n\n- he is told to \"restructure\" the program.\n\nTom, being very diligent, considers these advices, and his idea is as follows:\n\n- he realizes that if a connected component of the flow graph has exactly one in-degree, and exactly one out-degree, then that can be considered as a \"sub-algorithm\" which can be developed independently, so he could break up his task in this manner. He, however, has no idea how to find such components in the first place, and whether there are other intelligent ways to break up the problem further.\n\n- Tom doesn't really care whether using `goto` is a good or a bad programming practice (see GOTO still considered harmful?), what he is concerned about is that there are certain programming guidelines at his company what he needs to at all times.\n\n- Tom can indeed touch the algorithm in the sense that he might replace certain instructions which lead to an equivalent algorithm at his own discretion. Tom, however, has no idea which part of the program requires restructuring, and more importantly, he doesn't understand why the restructuring is necessary. Tom nervously stares at his 1000-vertex graph, and doesn't really know how to begin implementing it in the first place.\n\n### The questions regarding this (edited) post are as follows:\n\nCan you help Tom to figure out how to begin implementing something which is not \"two-line-dead-obvious\"? In particular, is it clear that in what order should one implement the tasks described by the nodes of the flow chart? Is it clear that in what order should come certain nested loops one after another?\n\nWhat are the smallest \"atoms\" of the flow chart which cannot be further broken up into smaller pieces? That is, when can Tom confidently respond to stackoverflow that \"I have broken up my algorithm into smaller parts already\"? Is it true that everything is essentially a while loop and/or a binary branch point (the tasks of days one and two)?\n\nHow to implement such an \"atom\" automatically, more-or-less in the same way as Tom did it on days one and day two already?\n\nCan Tom argue with the CEO that using `goto` in certain cases is essential, that is, either they use it to implement certain algorithm, or there is absolutely no other way to implement it according to the company's guidelines (that is, without the use of `goto`)?\n\nWhat parts of the flow-graph are problematic and requires restructuring, and why? For example, a three-way branch could be replaced by a nested two-way if-then-else statement, that is, Tom could safely assume that each node on his flow chart has out degree at most two. But what other restructurings should be done to deal with all the nested loops caused by the `goto` statements? What graph-property makes the restructuring necessary? Perhaps, high in-degree?\n\n**What is the mathematical (graph) theory behind the flow chart of an originally proposed algorithm (by the software development team), and the restructured and broken up flow chart(s) of the algorithm(s) which one (say Tom) actually more-or-less *automatically* implements?**\n\n### ORIGINAL QUESTION\n\nSuppose that I have some algorithm which uses binary decisions and `goto` statements. The algorithm is described in N>=2 (finite) steps in the following high-level way, and it should be executed sequentially (one step after another):\n\nALGORITHM WHATEVER\n\n```\nStep 1. START\nStep 2. Do something. If condition in Step 2 holds goto Step X else goto Step Y.\nStep 3. Do something. If condition in Step 3 holds goto Step U else goto Step V.\nStep 4. Do something.\nStep 5. Do something. If condition in Step 5 holds goto...\nStep 6. ...\n...\nStep N. END\n```\n\nYou get the idea. For example, Knuth describes the algorithms in his books in such a programming-language independent, high level way.\n\nThe question is now how to transform such a high-level description with `goto` statements into an actual implementation with while loops and if/else statements? Is it possible to completely eliminate all the `goto` statements, and replacing them by a while loop? If so, how should one do this *in general*?\n\nBased on the description of the algorithm it is possible to construct the corresponding flow chart, and hence the (directed) flow graph. So the question in other words is \"How to implement a code based on its flow chart without `goto` statements *in general*?\".\n\nThere are two ways to answer this question. Preferably, and quite hopefully, I am looking for an algorithmic way to implement ALGORITHM WHATEVER. If ALGORITHM WHATEVER is *very* simple, then it is intuitively clear what one should do, but it seems to me that things get quite complicated once a step is visited frequently (there are many goto statements jumping there), or, in other words, when one of the nodes of the flow graph has a large in-degree. Then I don't quite see in what particular order the while loops should be nested. On the other hand, it might very well be possible that one simply cannot do what I want in general, and such an answer should be backed up by a high-level description of ALGORITHM IMPOSSIBLE which clearly demonstrates that no matter what, one simply cannot avoid using `goto` jumps in an actual implementation.\n\nIt seems to me that transforming implementation into flowcharts were asked several times: Automatic flowchart tool and here Algorithm to create flow chart [A little guidance??]. The program code2flow seems to be a good starting point for visualising a code.\n\nHowever, here I am interested in the other direction. A simple search revealed that DRAKON (see also https://en.wikipedia.org/wiki/DRAKON and http://drakon-editor.sourceforge.net/) might be doing exactly what I am asking about. From this perspective, the question is, how might such an automatic flowchart-to-code program work under the extra assumption that it does not use the `goto` statement?\n\n========================================\n\nTop Answer:\nIf we treat each of the \"Do something\" statements in ALGORITHM WHATEVER as a function returning TRUE or FALSE, you can do something like the following (in pseudocode):\n\n`Let N be an int\n curr_state be an int\n T[1..N] be an array of ints\n F[1..N] be an array of ints\n Func[1..N] be an array of functions returning booleans`\n\n`1. curr_state := 1\n 2. while curr_state != N+1 do /* end state */\n 3. if (Func[curr_state] == TRUE) then\n 4. curr_state := T[curr_state]\n 5. else \n 6. curr_state := F[curr_state]\n 7. fi\n 8. od`\n\nOne might ask, \"But what if the functions take arguments or need to modify some shared state?\" In principle, function arguments and shared state can all be stored in global variables (within the scope of your algorithm). In practice, you'd likely do something slightly different.\n\n========================================\n\nCode:\n```text\nStep 1. START\nStep 2. Input x\nStep 3. In case x<0 goto Step 4 otherwise goto Step 5\nStep 4. Set x=-x\nStep 5. Output x\nStep 6. END\n```\n\n```text\nStep 1. START\nStep 2. Input x\nStep 3. In case x<0 goto Step 2 otherwise goto Step 4\nStep 4. Output x\nStep 5. END\n```\n\n```text\nStep 1. START\nStep 2. Do something. If condition in Step 2 holds goto Step X else goto Step Y.\nStep 3. Do something. If condition in Step 3 holds goto Step U else goto Step V.\nStep 4. Do something.\nStep 5. Do something. If condition in Step 5 holds goto...\nStep 6. ...\n...\nStep N. END\n```\n\n```text\ngoto\n```\n\n```text\ngoto\n```\n\n```text\ngoto\n```\n\n```text\ngoto\n```\n\n```text\ngoto\n```\n\n```text\ngoto\n```\n\n```text\ngoto\n```\n\n```text\ngoto\n```\n\n```text\ngoto\n```\n\n```text\ngoto\n```\n\n```text\ngoto\n```\n\n```text\ngoto\n```\n\n```text\ngoto\n```\n\n```text\nLet N be an int\n     curr_state be an int\n     T[1..N] be an array of ints\n     F[1..N] be an array of ints\n     Func[1..N] be an array of functions returning booleans\n```\n\n```text\n1. curr_state := 1\n 2. while curr_state != N+1 do /* end state */\n 3.     if (Func[curr_state] == TRUE) then\n 4.         curr_state := T[curr_state]\n 5.     else \n 6.         curr_state := F[curr_state]\n 7.     fi\n 8. od\n```\n\n========================================\n\nComments:\n- Use the 'step' description to construct a state machine. The steps are the states; the conditions are the transitions (where there are no conditions specified, have state 'step n' have a single transition to state 'step n+1'.\n- Note that of course the engine that 'executes' the state machine *is* doing `goto`s; it's just that they're prettied-up.\n- @AakashM The flow chart (or the state diagram) is given already, since it is equivalent with the step-by-step algorithm specification. My question asks *how* do you implement something based on its specification, or flow chart.\n- Draw the flow chart in flowgrid.org and run it? :) i.imgur.com/8NueBvC.png\n- Yes, this is quite clever. Actually I was thinking of something very similar solution in the form where the `goto` statements are replaced by function calls. So in essence, instead of the specification rule \"Do something and goto X\" I would do \"Do something and then do something what you would do at X\".The reason I am not quite happy with these two approaches is that somehow the function `Func` should understand the entire algorithm\n- @Matsmath There is not just one Func; there are N different Funcs, each one corresponding to the N different \"Do something\"s.\n- ...or more precisely, the functions within `Func` should understand the whole algorithm at once, which might be quite difficult to implement in practice. (I accidentally hit return, and then ran out of the 5 minute of allowed editing...).\n- @Matsmath OK. But if the \"Do something\"s are hard to implement, I'm not sure there's anything you can do that isn't equally hard.\n- Good point about the shared memory. I forgot to emphasize that ALGORITHM WHATEVER may use global variables. While I cannot say I deeply understand the relation between a finite state machine (FSM) and an expert system, I feel like at this point I would just do a down-to-earth implementation based on the program specification (=flow chart=FSM) instead of relying on more advanced tools, like clever use of artificial intelligence and branch prediction. Thank you.\n- I believe that your post nicely addresses almost all of Tom's concerns: the Structured program theorem guarantees that the `goto` jumps can be eliminated, and Sue Grahams's paper (which I will fetch from ACM later) reduces the flow graph to smaller components, so that Tom then knows which part of the implementation requires his attention. I will keep an eye on SO to see if you will have something else to say later. Thank you for the great references.","metadata":{"transformedAt":"2026-08-18T18:32:17.809Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":18,"totalLines":225,"estimatedTokens":3318}}578{"id":"stack-2260107","source":"stackoverflow","questionId":2260107,"title":"Character position in scanner using Lex/Flex","tags":["parsing","compiler-construction","lexical-analysis"],"text":"Title: Character position in scanner using Lex/Flex\nTags: parsing, compiler-construction, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nIn Lex/Flex is there a way to get the position in the character stream (from the start of the file) that a token appears at? Kind of like yylineno except that it returns the character position as an integer?\n\nIf not, what's the best way to get at this? Do I need to keep my own counter?\n\nThanks!\n\n========================================\n\nComments:\n- position from the start of the file or from the start of the line?\n- Position from the start of the file","metadata":{"transformedAt":"2026-08-18T18:32:17.809Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":16,"estimatedTokens":149}}579{"id":"stack-60474","source":"stackoverflow","questionId":60474,"title":"Using the DLR for (primarily) static language compilation","tags":[".net","compiler-construction","cil","dynamic-language-runtime"],"text":"Title: Using the DLR for (primarily) static language compilation\nTags: .net, compiler-construction, cil, dynamic-language-runtime\nSource: Stack Overflow\n\nQuestion:\nI'm building a compiler that targets .NET and I've previously generated CIL directly, but generating DLR trees will make my life a fair amount easier. I'm supporting a few dynamic features, namely runtime function creation and ducktyping, but the vast majority of the code is completely static.\n\nSo now that that's been explained, I have the following questions:\n\n- Has the DLR been used for static compilation, outside of small examples on MSDN blogs?\n\n- If so, what sort of performance was achieved?\n\n- If not, is there anything fundamentally preventing this?\n\n- Are there any better mechanisms of generating code than either using the DLR or emitting IL directly?\n\nAny insight into this or references to blogs/code/talks would be greatly appreciated.\n\n========================================\n\nComments:\n- Thanks for your response, I've started building some static compilation prototypes, so we'll see how things go :) I will say, I wasn't expecting a response from someone at MS on a fairly obscure site -- good to see :)","metadata":{"transformedAt":"2026-08-18T18:32:17.809Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":23,"estimatedTokens":298}}580{"id":"stack-700916","source":"stackoverflow","questionId":700916,"title":"Is it possible for the Vb.Net compiler to switch on an \"Unreachable code\" warning?","tags":["vb.net","compiler-construction"],"text":"Title: Is it possible for the Vb.Net compiler to switch on an \"Unreachable code\" warning?\nTags: vb.net, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI've been mostly working with VB.Net for over a year and just noticed this\n\nAm I going insane, or does VB.Net **NOT** have an \"Unreachable code\" warning?\n\nThe following compiles quite happily with nary a warning or error, even though there is a return between the two writeline calls.\n\n```\nSub Main()\n Console.WriteLine(\"Hello World\")\n Return\n Console.WriteLine(\"Unreachable code, will never run\")\nEnd Sub\n```\n\nAm I missing something? Is there some way to switch this on that I can't find.\n\nIf not, is there a good reason for its omission? (i.e. or am I right in thinking this is a woeful state of affairs)\n\nForgive the air of rant about this question, it's not a rant, I would like an answer.\n\nThanks\n\nI've raised this on MS Connect, as bug# 428529\n\n**Update**\n\nI received the following from the VB Teams program manager\n\nThanks for taking the time to report\nthis issue. The compiler has limited\nsupport for this scenario, and as you\npoint out we don't have warnings for\nunreachable code. There are some\nscenarios that our flow analysis\nalgorithm does handle, such as the\nfollowing:\n\n```\nSub Main()\n Dim x As Integer\n Return\n x = 4\nEnd Sub\n```\n\nIn this case you'll get a warning that\nx has never been assigned. For the\ncase you mentioned however we'll have\nto look at implementing that for a\nfuture release.\n\n========================================\n\nTop Answer:\nThey mention this in the following post:\n\nhttps://stackoverflow.com/questions/210187/usage-statistics-c-versus-vb-net\n\nSee the last post.\n\nI guess you could use FXCop to check your code instead or get a copy of Resharper from:\n\nhttp://www.jetbrains.com/resharper/\n\n========================================\n\nCode:\n```text\nSub Main()\n    Console.WriteLine(\"Hello World\")\n    Return\n    Console.WriteLine(\"Unreachable code, will never run\")\nEnd Sub\n```\n\n```text\nSub Main()\n    Dim x As Integer\n    Return\n    x = 4\nEnd Sub\n```\n\n========================================\n\nComments:\n- (flame)you're using VB so the compiler is mocking you(/flame) sorry.\n- Ian, if I could vote for a comment :)\n- I know what your saying, but . . . C# has goto, allows returns in the middle of functions, has try / finally blocks . . . AND it can still spot unreachable code. I have opened a bug, thanks for the link\n- @Binary, VB's use of control flow though is **much** less restrictive than C#'s. Much of it is hold over from VB6 days but these artifacts make it significantly harder to do proper code flow analysis. Can you post the connect bug number here please?\n- Jared, anyone that's still using Goto, On error etc deserve what they get. We run everything with Option Strict On and expect \"more\" :) Bug number is 428529. Thanks mate\n- P.S. If it were up to me I'd spend 30 minutes every morning porting different projects to C# and be done with it . . . but it's not up to me\n- Select as correct answer purly for advice to raise this as a bug\n- Jared, I just read my last comment and thought it could be read as sounding a little sour, which wasn't my intention at all. Thanks for your help mate.\n- @Binary no worries. Thanks again for filing the bug!","metadata":{"transformedAt":"2026-08-18T18:32:17.809Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":100,"estimatedTokens":815}}581{"id":"stack-19351423","source":"stackoverflow","questionId":19351423,"title":"LLVM/Clang special case for allocators","tags":["c","compiler-construction","clang","llvm"],"text":"Title: LLVM/Clang special case for allocators\nTags: c, compiler-construction, clang, llvm\nSource: Stack Overflow\n\nQuestion:\nIf you have the following code in \"alloc.c\":\n\n```\ntypedef __typeof__(sizeof(int)) size_t;\n\nextern void *calloc (size_t __nmemb, size_t __size)\n __attribute__ ((__nothrow__ )) __attribute__ ((__malloc__)) ;\nextern void free (void *__ptr) __attribute__ ((__nothrow__ ));\n\nstruct _astruct {\n int l;\n};\n\nint main() {\n struct _astruct *f = (struct _astruct*)calloc(1, sizeof(struct _astruct));\n f->l = 99;\n int x = f->l;\n free(f);\n return x;\n}\n```\n\n(I know that's not the preferred way to declare calloc/free, but it's for simplicity of output below.)\n\nThen you run \"`clang -O3 -S -emit-llvm alloc.c`\" with Clang/LLVM 3.3, you get:\n\n```\n; ModuleID = 'alloc.c'\ntarget datalayout = \"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128\"\ntarget triple = \"x86_64-unknown-linux-gnu\"\n\n; Function Attrs: nounwind readnone uwtable\ndefine i32 @main() #0 {\nentry:\n ret i32 99\n}\n\nattributes #0 = { nounwind readnone uwtable \"less-precise-fpmad\"=\"false\" \"no-frame-pointer-elim\"=\"false\" \"no-frame-pointer-elim-non-leaf\"=\"false\" \"no-infs-fp-math\"=\"false\" \"no-nans-fp-math\"=\"false\" \"unsafe-fp-math\"=\"false\" \"use-soft-float\"=\"false\" }\n```\n\nYou can see that it has **eliminated the call to calloc** altogether. Awesome. In general, though, a compiler can't eliminate calls to functions that it does not have the source to (there may be side-effects). So it seems that Clang has special case code for calloc/free. This is supported by the fact that if you chanage all occurrences to \"calloc\" in the code to be \"_calloc\", the \"allocation\" comes back.\n\nSo my question is, is there **any way to tell Clang/LLVM that a particular function is an \"allocation only\" function**? Perhaps some annotation I could put on the definition of one of my own functions that would allow LLVM to optimize away allocations just as it can for calloc/malloc.\n\n========================================\n\nTop Answer:\nIt's quite obvious in the header file - it's the \"**attribute ((malloc))**\" in the declaration of calloc. The whole thing says \"this function never throws exceptions, and it returns a pointer that was allocated with a new malloc call\". This allows several optimisations: \n\nThe pointer returned is guaranteed not to be an alias of any other pointer. If you have two pointers int* p and int* q, then *p = 1; *might* change *q. If p or q was just freshly allocated with a malloc function that cannot happen. \n\nIf the compiler finds the matching free () call, then the compiler can sometimes allocate the data on the stack and eliminate both the malloc and free calls.\n\n========================================\n\nCode:\n```text\ntypedef __typeof__(sizeof(int)) size_t;\n\nextern void *calloc (size_t __nmemb, size_t __size)\n  __attribute__ ((__nothrow__ )) __attribute__ ((__malloc__)) ;\nextern void free (void *__ptr) __attribute__ ((__nothrow__ ));\n\nstruct _astruct {\n  int l;\n};\n\n\nint main() {\n  struct _astruct *f = (struct _astruct*)calloc(1, sizeof(struct _astruct));\n  f->l = 99;\n  int x = f->l;\n  free(f);\n  return x;\n}\n```\n\n```text\n; ModuleID = 'alloc.c'\ntarget datalayout = \"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128\"\ntarget triple = \"x86_64-unknown-linux-gnu\"\n\n; Function Attrs: nounwind readnone uwtable\ndefine i32 @main() #0 {\nentry:\n  ret i32 99\n}\n\nattributes #0 = { nounwind readnone uwtable \"less-precise-fpmad\"=\"false\" \"no-frame-pointer-elim\"=\"false\" \"no-frame-pointer-elim-non-leaf\"=\"false\" \"no-infs-fp-math\"=\"false\" \"no-nans-fp-math\"=\"false\" \"unsafe-fp-math\"=\"false\" \"use-soft-float\"=\"false\" }\n```\n\n```text\nclang -O3 -S -emit-llvm alloc.c\n```\n\n```text\ncalloc\n```\n\n========================================\n\nComments:\n- From the question: “This is supported by the fact that if you change all occurrences to \"calloc\" in the code to be \"_calloc\", the \"allocation\" comes back.” `attribute ((malloc))` is a GCC thing.","metadata":{"transformedAt":"2026-08-18T18:32:17.809Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":108,"estimatedTokens":1028}}582{"id":"stack-7878444","source":"stackoverflow","questionId":7878444,"title":"Create new *package* in a Scala Compiler Plugin","tags":["scala","plugins","compiler-construction","package"],"text":"Title: Create new *package* in a Scala Compiler Plugin\nTags: scala, plugins, compiler-construction, package\nSource: Stack Overflow\n\nQuestion:\nIn my quest to generate new code in a Scala compiler plugin, I have now created working classes. The next logical step is to put those classes in a new, non-existing package. In Java, a package is basically a directory name, but in Scala a package seems much more complicated. So far I haven't found/recognized an example where a compiler plugin creates a new package.\n\nAt my current level of understanding, I would think that I would need to create first a package symbol with:\n\n```\nparentPackage.newPackage(...)\n// ...\n```\n\nand than later create a Tree for the package with PackageDef. But PackageDef doesn't take the symbol as parameter, as one would expect, and searching for:\n\n```\nScala newPackage PackageDef\n```\n\nreturned nothing useful. So it seems that I don't need to do those two steps together. Possibly one is done for my by the compiler, but I don't know which one. So far, what I have looks like this:\n\n```\nval newPkg = parentPackage.newPackage(NoPosition, newTermName(name))\nnewPkg.moduleClass.setInfo(new PackageClassInfoType(new Scope,\n newPkg.moduleClass))\nnewPkg.setInfo(newPkg.moduleClass.tpe)\nparentPackage.info.decls.enter(newPkg)\n// ...\nval newPkgTree = PackageDef(Ident(newPkg.name), List(ClassDef(...)))\n```\n\n========================================\n\nCode:\n```text\nparentPackage.newPackage(...)\n// ...\n```\n\n```text\nScala newPackage PackageDef\n```\n\n```text\nval newPkg = parentPackage.newPackage(NoPosition, newTermName(name))\nnewPkg.moduleClass.setInfo(new PackageClassInfoType(new Scope,\n    newPkg.moduleClass))\nnewPkg.setInfo(newPkg.moduleClass.tpe)\nparentPackage.info.decls.enter(newPkg)\n// ...\nval newPkgTree = PackageDef(Ident(newPkg.name), List(ClassDef(...)))\n```\n\n========================================\n\nComments:\n- Where did you get stuck? It looks good so far. You probably want to add the tree to an existing compilation unit, otherwise no code will be generated for your trees.\n- To be honest, I can't remember. I also asked on the Scala mailing list, and did not get a concrete answer there either, so I basically gave up on writing the plugin. I just wrote more and more code, and it kept getting uglier, but nothing worked.\n- Mailing List link: groups.google.com/group/scala-language/browse_thread/thread/&zwnj;&#8203;&hellip; What I need is the simplest example where one is in transform() or transformStats(), and from there creates a totally unrelated class in a totally unrelated package. There must be a way of adding trees at the root package level, instead of only inside the current package.","metadata":{"transformedAt":"2026-08-18T18:32:17.810Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":60,"estimatedTokens":671}}583{"id":"stack-8203221","source":"stackoverflow","questionId":8203221,"title":"compiler vs interpreter ( on basis of construction and design )","tags":["python","compiler-construction","interpreter","bytecode"],"text":"Title: compiler vs interpreter ( on basis of construction and design )\nTags: python, compiler-construction, interpreter, bytecode\nSource: Stack Overflow\n\nQuestion:\nAfter viewing lots of posts about the difference between compilers and interpreters, I'm still not able to figure out the difference in their construction and internal mechanism.\n\nThe most common difference I read was that a compiler produces a target program which is executable { means machine code as its output } which can run on a system and than be fed with input.\nWhereas an interpreter simply runs the input line by line { what exactly is happening here ?} and produces the output.\n\nMy main doubts are :\n\n1) A compiler consists of a lexical analyzer, parser, intermediate code generator and code generator but what are the parts of an interpreter?\n\n2) Who gives the run-time support to interpreted languages, I mean who manages the heap and stacks for recursive functions?\n\n3) This is specific to the Python language:\n\nPython comprises of a compiler stage and than interpreter stage as well\ncompiler produces some byte-code and and than this byte-code is interpreted by its Virtual Machine.\n\nif I were to design only the compiler for Python (Python -> bytecode) \n\na) will I have to manage memory { write code to manage stack and heap } for it?\n\nb) how will this compiler differ from the traditional compiler or say interpreter? \n\nI know this is a whole lot to ask here but I really want to understand these minute details. \n\nI'm referring the compiler book by Alfred V. Aho \n\nBased on the feedback and some further study I think I should modify my question\n\n**A compiler need not produce only machine code as its output**\n\nBut one question is still bugging me \nLet say I want to design a ( Python->bytecode ) compiler and then bytecode will be interpreted by the virtual machine.. (correct me if I'm wrong ).\n\nThen I'll have to write a lexical analyzer for Python and then a parser which will generate some sort of abstract syntax tree.. after this do I have to generate some intermediate code (3 address code as mentioned in the dragon book) or direct bytecode instructions ( which I suppose will be given in the VM's documentation ) ?\n\n**Will I have to write code for handling stack as well to provide support for recursion and scope ?**\n\n========================================\n\nComments:\n- You ask too many questions in one post, but regarding (2) \"who gives the run-time support to interpreted languages,\" the answer is often libc, i.e. the C runtime library is used by many if not most interpreters.\n- \"line by line\" - i believe that is not true of all interpreters. some may compile multiple lines that form a logical single statement into byte code and then execute. and others actually interpret the raw text and immediately execute it, like `bash`.\n- sorry @john Zwinck, what I meant regarding (2) was that , who does the memory managing stuff for the interpreted language.\n- Compiler translates a language into another. Most interpreters consist of a compiler that outputs some form of bytecode and a virtual machine that executes that bytecode.\n- @CatPlusPlus: The latter point (which is important) may be clearer when phrased like this: Many language implementations called interpreters are really compilers targeting a virtual machine and an interpreter for said virtual machine's instruction set.\n- stackoverflow.com/questions/3265357/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.810Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":51,"estimatedTokens":858}}584{"id":"stack-2440033","source":"stackoverflow","questionId":2440033,"title":"LALR(1) or GLR on Windows - Alternatives to Bison++ / Flex++ that are current?","tags":["c++","parsing","compiler-construction","bison","lalr"],"text":"Title: LALR(1) or GLR on Windows - Alternatives to Bison++ / Flex++ that are current?\nTags: c++, parsing, compiler-construction, bison, lalr\nSource: Stack Overflow\n\nQuestion:\n**UPDATE: This question is out of date, but left for informational purposes.**\n\nOriginal Question\n\nI have been using the same version of bison++ (1.21-8) and flex++ (2.3.8-7) since 2002.\n\nI'm not looking for an alternative to LALR(1) or GLR at this time, just looking for the most current options. Is anyone aware of any later ports of these than the original that aren't Cygwin dependent?\n\nWhat are other folks using in Windows environments for C++ compiler development (besides ANTLR or Boost.spirit)? Commercial options are ok, if you have firsthand experience. I do need to compile on Linux as well.\n\nUPDATE: This old question was asked when I wasn't aware of the policies about tool recommendations (not sure if policy existed in 2010 but regardless...\n\nI since updated to Bison 3.0 which has GLR capabilities and have begun experimenting with that.\n\nI eventually decided that any rewrite of my parser would be a recursive descent, to improve error reporting and allow easier use in tools besides the compiler, so for now I will complete the reference version in Bison. I see little point in converting to a different PG tool at this time.\n\n========================================\n\nTop Answer:\nI like ANTLR a lot. Boost Spirit is a bit \"out there\" for serious production applications.\n\n========================================\n\nComments:\n- Why not ANLTR? IMO it works well on Windows.\n- Mainly because I didn't want to port my grammar from Yacc/Bison to ANTLR. Nothing against ANTLR, I've played with it, but never \"adopted\" it. I work on my compiler in my spare time, as a hobby, and any porting to a different dialect might tie up several weekends.\n- So you're asking for a tool that does the exact same thing as flex + bison, but refuse to accept any syntax changes from flex/bison? Why not just stick with what you're using then?\n- What's the matter with modern bison? v2.4.1? gnu.org/software/bison/manual/bison.html\n- @Ira Baxter: I'm using 2.4 on Linux. Does 2.4.1 compile and run on Windows now without any Cygwin dependency? If so, I missed that. I'll do the research. This was really a question of keeping up the most currently developed Yacc/Bison codebase, not about needing a new generator. Thanks.\n- @mrjoltcola: No, I guess I wasn't paying close enough attention to the \"no cygwin\" part. We use the cygwin stuff so thoughtlessly under Windows that I almost forget it is there; havent' checked Bison to see if it does.\n- After experimenting with ANTLR, bison and yacc, I've come to the conclusion, that it's best to write your own lexer/parser pair.\n- None of the answers were specific to my question, but helped nonetheless.\n- @mrjoltcola: ANTLR has a C target and is completely usable in C++. There's no separate C++ target because it would be redundant -- most of the C components are only used by ANTLR itself. You can use C++ in the semantic actions as much as you like.\n- Ok, +1 to your followup as it is helpful.","metadata":{"transformedAt":"2026-08-18T18:32:17.810Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":39,"estimatedTokens":779}}585{"id":"stack-2825351","source":"stackoverflow","questionId":2825351,"title":"'Lexical' scoping of type parameters in C#","tags":["c#","generics","compiler-construction","compiler-errors"],"text":"Title: 'Lexical' scoping of type parameters in C#\nTags: c#, generics, compiler-construction, compiler-errors\nSource: Stack Overflow\n\nQuestion:\nI have 2 scenarios.\n\nThis fails:\n\n```\nclass F\n{\n public X X { get; set; }\n}\n```\n\n*error CS0102: The type '`F`' already contains a definition for '`X`'* \n\nThis works:\n\n```\nclass F\n{\n class G\n {\n public X X { get; set; }\n }\n}\n```\n\nThe only logical explanation is that in the second snippet the type parameter X is out of scope, which is not true...\n\nWhy should a type parameter affect my definitions in a type?\n\nIMO, for consistency, either both should work or neither should work.\n\nAny other ideas?\n\nPS: I call it 'lexical', but it is probably not the correct term.\n\n**Update:**\n\nAs per Henk's answer, here is a non-generic version displaying the same behavior, but perhaps easier to grok.\n\nFails:\n\n```\nclass F\n{\n class X { }\n public X X { get; set; }\n}\n```\n\nWorks:\n\n```\nclass X { }\nclass F\n{\n public X X { get; set; }\n}\n```\n\nFrom what I can see, the C# compiler creates a lexical scope at type definition boundries.\n\nIt also implies that types and member names live in the same 'location' (or `namespace` in terms of LISP).\n\n========================================\n\nTop Answer:\n`X` is defined as a type in the scope of `F`. It is similar to this:\n\n```\nclass F\n{\n public void X();\n\n public int X(); // Bad, trying to redefine X.\n\n class G\n {\n public string X(); // OK, different scope\n }\n}\n```\n\n`F.X` isn't out of scope in `G`, but this doesn't stop `G` from defining a new `X`.\n\n========================================\n\nCode:\n```text\nclass F<X>\n{\n  public X X { get; set; }\n}\n```\n\n```text\nclass F<X>\n{\n  class G\n  {\n    public X X { get; set; }\n  }\n}\n```\n\n```text\nclass F\n{\n  class X { }\n  public X X { get; set; }\n}\n```\n\n```text\nclass X { }\nclass F\n{\n  public X X { get; set; }\n}\n```\n\n```text\nF<X>\n```\n\n```text\nX\n```\n\n```text\nnamespace\n```\n\n```text\npublic F<X>.X F<X>.X { get; set; }    // error\npublic F<X>.X F<X>.G.X { get; set; }  // OK\n```\n\n```text\nclass F\n{\n  public void X();\n\n  public int X(); // Bad, trying to redefine X.\n\n  class G\n  {\n    public string X(); // OK, different scope\n  }\n}\n```\n\n```text\nX\n```\n\n```text\nF\n```\n\n```text\nF.X\n```\n\n```text\nG\n```\n\n```text\nG\n```\n\n```text\nX\n```\n\n========================================\n\nComments:\n- I dont see why the first shouldnt work, anyone able to reproduce the compiler error?\n- According to your update, it looks like Marcelo answered your question - he demonstrated the same thing.\n- @Kobi: No, not quite :) I didn't downvote him either, as it is partially applicable.\n- not related, but can you check out my question leppie? stackoverflow.com/questions/4444708/&hellip;\n- Sorry, your X is a member, mine is a type parameter. I can understand that.\n- Sorry to be obtuse, @leppie. Are you agreeing or disagreeing with me?\n- Thanks, that seems to make a little sense :) And why: class F { class X { } public X X { get; set; } } also fails.","metadata":{"transformedAt":"2026-08-18T18:32:17.810Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":15,"totalLines":190,"estimatedTokens":736}}586{"id":"stack-3407318","source":"stackoverflow","questionId":3407318,"title":"Mono Compiler as a Service (MCS)","tags":["c#",".net","compiler-construction","mono","gmcs"],"text":"Title: Mono Compiler as a Service (MCS)\nTags: c#, .net, compiler-construction, mono, gmcs\nSource: Stack Overflow\n\nQuestion:\nI'd like to consume Mono's compiler as a service from my regular .NET 3.5 application.\n\nI've downloaded the latest bits (2.6.7), created a simple console application in Visual Studio and referenced the Mono.CSharp dll.\n\nThen, in my console app (straight out of a sample online):\n\n```\nEvaluator.Run(\"using System; using System.Linq;\");\n bool ress;\n object res;\n Evaluator.Evaluate(\n \"from x in System.IO.Directory.GetFiles (\\\"C:\\\\\\\") select x;\",\n out res, out ress);\n\n foreach (var v in (IEnumerable)res)\n {\n Console.Write(v);\n Console.Write(' ');\n }\n```\n\nThis throws an exception at Evaluator.Run (the first line): \n\n```\nIllegal enum value: 2049.\nParameter name: access\n```\n\nThis is because the dll was compiled using Mono.exe, not csc.exe, I believe.\n\nI've tried downloading the Mono.CSharp dll directly from http://tirania.org/blog/archive/2010/Apr-27.html in the demo-repl.zip file...and that does not throw an exception...However the out parameter (res) after calling Evaluator.Evaluate is null...so I'm not sure what's going wrong. No exception is thrown...\n\nSo, I'd like to figure out why the dll I downloaded from the demo-repl.zip returns null. \n\nEDIT: I figured out why it returns null. It seems like for some reason the compiler isn't picking up the System.Linq namespace...though I can't tell why...If I just Evaluate \"System.IO.Directory.GetFiles (\\\"C:\\\\\")\", it works fine.\n\nUPDATE: It definitely seems like there's something wrong with the Mono compiler picking up referenced System assemblies. If I directly copy the sample of their csharp console tool:\n\n```\ncsharp> var list = new int [] {1,2,3};\ncsharp> var b = from x in list\n > where x > 1\n > select x;\ncsharp> b;\n```\n\nI get the exception:\n\n```\n{interactive}(1,25): error CS1935: An implementation of `Select' query expressio\nn pattern could not be found. Are you missing `System.Linq' using directive or `\nSystem.Core.dll' assembly reference?\n```\n\nAlso, in order for the MCS to actually be a feasible solution, I'll need to modify the compiler so that it emits to one single dynamic assembly, instead of emitting one assembly per evaluate call (otherwise it presents a major memory leak, which I've dealt with before in the form of the CSharpCodeProvider). Does anyone have an idea of how difficult this will be or can anyone point me in the right direction here?\n\nThanks.\n\n========================================\n\nTop Answer:\nAccording to Miguel's blog page you linked, you have to add a reference to System.Core in order to use LINQ on .Net.\n\n```\ncsharp> using System.Linq;\ncsharp> from x in \"Foo\" select x;\n```\n\n========================================\n\nCode:\n```text\nEvaluator.Run(\"using System; using System.Linq;\");\n    bool ress;\n    object res;\n    Evaluator.Evaluate(\n         \"from x in System.IO.Directory.GetFiles (\\\"C:\\\\\\\") select x;\",\n         out res, out ress);\n\n    foreach (var v in (IEnumerable)res)\n    {\n        Console.Write(v);\n        Console.Write(' ');\n    }\n```\n\n```text\nIllegal enum value: 2049.\nParameter name: access\n```\n\n```text\ncsharp> var list = new int [] {1,2,3};\ncsharp> var b = from x in list\n   >    where x > 1\n   >    select x;\ncsharp> b;\n```\n\n```text\n{interactive}(1,25): error CS1935: An implementation of `Select' query expressio\nn pattern could not be found. Are you missing `System.Linq' using directive or `\nSystem.Core.dll' assembly reference?\n```\n\n```text\nAppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);\n\n        private static bool isResolving;\n        static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)\n        {\n            if (!isResolving)\n            {\n                isResolving = true;\n                var a = Assembly.LoadWithPartialName(args.Name);\n                isResolving = false;\n                return a;\n            }\n            return null;\n        }\n```\n\n```text\n/// <summary>\n/// Gets or sets a value indicating whether to auto reset when evaluations are performed and create a new assembly.\n/// </summary>\n/// <value><c>true</c> if [auto reset]; otherwise, <c>false</c>.</value>\npublic static bool AutoReset { get; set; }\n```\n\n```text\nstatic void Init ()\n    {\n        Init (new string [0]);\n        Reset();\n    }\n```\n\n```text\nstatic CSharpParser ParseString (ParseMode mode, string input, out bool partial_input)\n        {\n.\n.\n.\n            if (AutoReset) Reset ();\n```\n\n```text\ncsharp> using System.Linq;\ncsharp> from x in \"Foo\" select x;\n```\n\n========================================\n\nComments:\n- why not to use msdn.microsoft.com/en-us/library/&hellip; ?\n- CSharpCodeProvider emits and loads an assembly per compile (even if you use in-memory only option). I will be performing thousands of evaluations and thus would have thousands of assemblies loaded into the executing AppDomain (memory leak). Also, CSharpCodeProvider internally uses csc.exe, which is far more processor intensive than Reflection.Emit. In a previous project I used CSharpCodeProvider, and ran evaluations in a separate app domain that was recycled based on assembly count, but this proved to be an enormous maintenance overhead and very error prone, so I'd like avoid that approach.\n- Also, I realize that by default MCS also does a single assembly per evaluate, but since it relies on Reflection.Emit internally, I'm hoping I can change that behavior to emit to a single assembly as defined by AppDomain.DefineDynamicAssembly.\n- Ah...I see, but adding a reference isn't enough because it doesn't copy GAC assemblies or framework assemblies to the bin directory.\n- So, I'm having this same issue. Is there a compiled version of Mono.CSharp.dll that has the changes you describe above? I'd like to use this in an application but I'm finding it very unstable.\n- What issue are you having? Have you tried making the changes I mention above? If you really need it, I can provide a dll, but you're probably better off just making these changes and compiling the dll yourself.\n- Also, on my end, after making these changes I haven't really encountered any other problems...have you?\n- Have you got a compiled version of this modified DLL you could ? I'm trying to do the same thing (as per my other question you answered about LINQ compilation) using the Mono 2.7 versioned DLL, but even evaluating 1+1 gives the Illegal enum value 2049 error.\n- The 2049 error definitely means you're using the version that's compiled for the Mono CLR, not the MS CLR (see the very top of my post). I used Mono's source control, opened two projects, compiled them in VS and that fixed my 2049 error. I can upload the dll and my modified sources if you post a location, but I'd still suggest compiling it yourself, in case you want to make some additional changes (for example implementing method call security for your LINQ queries, so someone can't run File.Delete(...)\n- Ok, got the code out and recompiled it and I can evaluate c# strings now. Thanks! Now I just need to figure out how to inject a variable into the evaluator and get the IQueryable back from a LINQ query.\n- What kind of variable? Make sure you the steps regarding the AppDomain assembly resolution so that you can use IQueryable.\n- I've got that part working hundreds. If I have a dbContext variable that I want to run a LINQ query against, I need to get that into the scope of the Mono.CSharp evaluator right? Otherwise how will I address the dbContext? edit -> Opened a new question: stackoverflow.com/questions/3788096/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.810Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":172,"estimatedTokens":1906}}587{"id":"stack-6893994","source":"stackoverflow","questionId":6893994,"title":"Testing a compiler","tags":["java","unit-testing","testing","compiler-construction","sablecc"],"text":"Title: Testing a compiler\nTags: java, unit-testing, testing, compiler-construction, sablecc\nSource: Stack Overflow\n\nQuestion:\nI'm currently working on kind of compiler that was built making use of sablecc. \n\nLong story short, the compiler will take as input both specification files(this is what we're parsing) and .class files and will instrument the .class files bytecode so to make sure that when running the .class files, any of the specifications is not being violated (this is a bit like jml/code contracts! but way more powerful).\n\nWe have some dozens of system tests that cover a large part of the analysis phase (related with making sure the specifications make sense, and that they also are in concordance with the .class files they are supposed to specify).\n\nWe divided them in two sets: the valid tests and the invalid tests. \n\nThe valid tests are comprised of source code files that when compiled by our compiler should should pop up no compiler errors / warnings.\n\nThe invalid tests are comprised of source code files that when compiled by our compiler should should pop up at least one compiler error / warning.\n\nThis has served us well while we were in the analysis phase. The question now is on how to test the code generation phase. I've done, in the past, system tests over a little compiler I've developed on a compilers course. Each test would consist of a couple of source files of that language and a `output.txt`. When running the test, I'd compile the source files and then run its main method, checking that the output result would be equal to `output.txt`. All of this was automated, of course.\n\nNow, dealing with this bigger compiler/bytecode-instrumentator, things are not so easy. It's no easy task to replicate what I've done with my simple compiler. I guess the way to go is to lean back from system tests at this stage, and focus on unit-tests.\n\nAs any compiler developer knows, a compiler consists of lots of visitors. I am not too sure on how to proceed with unit-testing them. From what I've seen, most of the visitors are calling a counterpart class that has methods related with that visitor (I guess the idea was to keep the SRP for the visitors).\n\nThere are a couple of techniques I can take to unit-test my compiler:\n\nUnit testing each one of the visitor's methods separately. This seems to be a good idea for a stackless visitor, but looks like a terrible idea for visitors that use one (or more) stacks. I then go about also unit-testing each of the other methods from standard(read, non-visitors) classes the traditional way.\n\nUnit testing the whole visitor in one go. That is, I create tree that I then visit. In the end, I verify if the symbol table was correctly updated or not. I do not care about mocking its dependencies.\n\nThe same as 2), but now mocking the visitor's dependencies.\n\nWhat others?\n\nI still have the problem that the unit-tests will be very tightly coupled with sabbleCC's AST (which tbh is really ugly).\n\nWe are currently not making any new tests, but I'd like to bring the train back on track, as I am sure that not testing the system is the same as feeding a monster that sooner or later will come back to bite us in the butt when we least expect it ;-(\n\nHas anyone had any experience with compiler testing that could give some awwweeeesome advice on how to proceed now? I'm kinda lost here !\n\n========================================\n\nCode:\n```text\noutput.txt\n```\n\n```text\noutput.txt\n```\n\n========================================\n\nComments:\n- \"I also heavily rely on integration tests - tests that actually compile and run the generated code in the target language\" What did these tests actually do? How are they different than the output.txt tests?\n- \"As for visitor testing, again I do more integration-style testing with them - get a really small/specific Java source file, load it up with Eclipse compiler, run one of my visitors with it and check results. \" Do you mean run with one of your visitors, or run all the visitors up to the visitor you wanna test?\n- Other question, do you use mocks -- at all, in this project? Moreover, do you regularly use mocks in other projects? I'm just trying to get a clear picture about the person I'm talking with :P\n- @devoured elysium answers to questions put into the response (easier to format)\n- Thanks. I think the most striking difference between our work is that all my visitors must be called sequentially. Visitor X needs to be run only after the other X-1 were run.\n- \"Rather than just generate source code and compare that to expected output which I did initially, the integration tests generate OpenCL code, compile it and run it. All of the generated code produces output and that output is then compared. \" I still don't get it. That approach seems exactly like my output.txt one. Example: source.src with contents \"printf hello world\", would be compiled and then run. I'd have a output.txt with \"hello world\", and I'd compare main's output with output.txt's contents.\n- @devoured elysium Sorry I totally misunderstood you. Now that I re-read your question I realize our approaches are the same.\n- Hmmm. Thanks! Just a question, though. The first phases of our program are just like of a regular compiler. We extract info from the source files and we fill up a (big and complex!) symbol table. How would you go about system testing this? In theory, I could create a test with the source files and also a symbolTable.txt (or .xml or whatever) that contains all the info about the symbolTable, but that would, I think, be a bit complex to do. Each one of those integration tests would be a complex thing to accomplish!\n- I thought that maybe I could create a pretty printer that would take on the Symbol Table and output the correspondent source files (that, if everything was ok, would be just like the original source files). The problem is that the original files can have things in different order than what my pretty printer prints. I'm afraid that with this approach I might just be opening another can of worms. I've been relentless refactoring parts of the code and the bugs are starting to show off. I really need some integration tests to keep me on track.\n- The integration test method in itself should be something simple, that is, I don't want to have to create a complex thing, otherwise I'll start having 2 problems to solve instead of one.\n- @devoured elysium added more answers in the response\n- Thanks! Yesterday I have been playing a bit and I think that I'll stick to your small integration testing idea. I will make small source code files and then check that the correct items show up in the symbol table. I'll do that directly by code, JUnit style, instead of doing pretty prints, though. Thanks a lot!","metadata":{"transformedAt":"2026-08-18T18:32:17.810Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":65,"estimatedTokens":1688}}588{"id":"stack-6722195","source":"stackoverflow","questionId":6722195,"title":"Converting Abstract Syntax Tree to Byte code","tags":["compiler-construction","jvm","antlr","bytecode","abstract-syntax-tree"],"text":"Title: Converting Abstract Syntax Tree to Byte code\nTags: compiler-construction, jvm, antlr, bytecode, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI am trying to learn to build a simple compiler as a hobby. I am targeting the Java virtual machine. \n\nI have written a simple grammar using ANTLR plugin for Eclipse .\n\nSomeone told me that there is something known as a method node on the AST generated by Antlr, and that has to be called.\nI am planning to use ASM to generate the bytecode. \nSo what is the method node and How do I call it from ASM and make it visit method instructions?\n\nAlso what about the semantic analyzer of a compiler. Should that be manually written or are there any generators for it?\n\n========================================\n\nCode:\n```text\nmain(String[])\n```\n\n```text\nfor i := 1 to 10\n```\n\n========================================\n\nComments:\n- Thanks a lot , I was given almost the same answer by stephen chin recently, Let's Build a Compiler, by Jack Crenshaw seems very good , I'll start reading it .","metadata":{"transformedAt":"2026-08-18T18:32:17.810Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":30,"estimatedTokens":260}}589{"id":"stack-33278056","source":"stackoverflow","questionId":33278056,"title":"Assigning a value to a constant syntax or semantic error?","tags":["c++","compiler-construction","syntax-error","lvalue"],"text":"Title: Assigning a value to a constant syntax or semantic error?\nTags: c++, compiler-construction, syntax-error, lvalue\nSource: Stack Overflow\n\nQuestion:\nIs the second line of code considered as a syntax error or a semantic error in C++?\n\n```\nint a = 7;\n 3 = a;\n```\n\nIn standard C++ context-free grammar I found this statement syntactically valid.\n\n========================================\n\nCode:\n```text\nint a = 7;\n 3 = a;\n```\n\n========================================\n\nComments:\n- it's a semantic error.\n- It's a conceptual error.","metadata":{"transformedAt":"2026-08-18T18:32:17.810Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":27,"estimatedTokens":133}}590{"id":"stack-10393280","source":"stackoverflow","questionId":10393280,"title":"Optimization of \"static\" loops","tags":["optimization","language-agnostic","compiler-construction","theory"],"text":"Title: Optimization of \"static\" loops\nTags: optimization, language-agnostic, compiler-construction, theory\nSource: Stack Overflow\n\nQuestion:\nI'm writing a compiled language for fun, and I've recently gotten on a kick for making my optimizing compiler very robust. I've figured out several ways to optimize some things, for instance, 2 + 2 is always 4, so we can do that math at compile time, if(false){ ... } can be removed entirely, etc, but now I've gotten to loops. After some research, I think that what I'm trying to do isn't exactly loop unrolling, but it is still an optimization technique. Let me explain.\n\nTake the following code.\n\n```\nString s = \"\";\nfor(int i = 0; i As a human, I can sit here and tell you that this is 100% of the time going to be equivalent to\n\n```\noutput(\"xxxxx\");\n```\n\nSo, in other words, this loop can be \"compiled out\" entirely. It's not loop unrolling, but what I'm calling \"fully static\", that is, there are no inputs that would change the behavior of the segment. My idea is that anything that is fully static can be resolved to a single value, anything that relies on input or makes conditional output of course can't be optimized further. So, from the machine's point of view, what do I need to consider? What makes a loop \"fully static?\"\n\nI've come up with three types of loops that I need to figure out how to categorize. Loops that will always end up with the same machine state after every run, regardless of inputs, loops that WILL NEVER complete, and loops that I can't figure out one way or the other. In the case that I can't figure it out (it conditionally changes how many times it will run based on dynamic inputs), I'm not worried about optimizing. Loops that are infinite will be a compile error/warning unless specifically suppressed by the programmer, and loops that are the same every time should just skip directly to putting the machine in the proper state, without looping.\n\nThe main case of course to optimize is the static loop iterations, when all the function calls inside are also static. Determining if a loop has dynamic components is easy enough, and if it's not dynamic, I guess it has to be static. The thing I can't figure out is how to detect if it's going to be infinite or not. Does anyone have any thoughts on this? I know this is a subset of the halting problem, but I feel it's solvable; the halting problem is a problem due to the fact that for some subsets of programs, you just can't tell it may run forever, it may not, but I don't want to consider those cases, I just want to consider the cases where it WILL halt, or it WILL NOT halt, but first I have to distinguish between the three states.\n\n========================================\n\nCode:\n```text\nString s = \"\";\nfor(int i = 0; i < 5; i++){\n    s += \"x\";\n}\noutput(s);\n```\n\n```text\noutput(\"xxxxx\");\n```\n\n```text\nfor (var i = S; E(i); i = U(i))\n```\n\n```text\nU(i) = i + CONSTANT\n```\n\n```text\nn\n```\n\n```text\ni\n```\n\n```text\nS + n * CONSTANT\n```\n\n```text\nU(i) = i * CONSTANT\n```\n\n```text\nn\n```\n\n```text\ni\n```\n\n```text\nS * CONSTANT^n\n```\n\n```text\nU(i) = i / CONSTANT\n```\n\n```text\nn\n```\n\n```text\ni\n```\n\n```text\nS * CONSTANT^-n\n```\n\n```text\nU(i) = (i + CONSTANT) % M\n```\n\n```text\nn\n```\n\n```text\ni\n```\n\n```text\n(S + n * CONSTANT) % M\n```\n\n```text\nn\n```\n\n```text\nE(i(n))\n```\n\n```text\nfor(int i = 0; i < 5; i++)\n```\n\n```text\ni(n) = 0 + n * 1 = n\n```\n\n```text\nE(i(n))\n```\n\n```text\nnot(n < 5)\n```\n\n```text\nn >= 5\n```\n\n```text\nn = 5\n```\n\n```text\nfor(int i = 0; i < 5; i--)\n```\n\n```text\ni(n) = 0 + n * -1 = -n\n```\n\n```text\nE(i(n))\n```\n\n```text\nnot(-n < 5)\n```\n\n```text\n-n >= 5\n```\n\n```text\nn < -5\n```\n\n```text\nn\n```\n\n```text\nfor(int i = 0; i < 5; i = (i + 1) % 3)\n```\n\n```text\nE(i(n))\n```\n\n```text\nnot(n % 3 < 5)\n```\n\n```text\nn % 3 >= 5\n```\n\n```text\nfor(int i = 10; i + 10 < 500; i = i + 2 * i)\n```\n\n```text\nfor(int i = 10; i < 480; i = 3 * i)\n```\n\n```text\ni(n) = 10 * 3^n\n```\n\n```text\nE(i(n))\n```\n\n```text\nnot(10 * 3^n < 480)\n```\n\n```text\n10 * 3^n >= 480\n```\n\n```text\n3^n >= 48\n```\n\n```text\nn >= log3(48)\n```\n\n```text\nn >= 3.5...\n```\n\n```text\nn = 4\n```\n\n========================================\n\nComments:\n- To get an idea of what's actually supported along this line at the present time, you might want to read about the limitations on `constexpr` in the new C++ standard.\n- If you can statically determine that the loop condition is always true, and there is no other way to exit the loop, then you know the loop won't terminate.\n- In your example, you don't necessarily know that String s is not also modified by another file referencing it via extern and modifying it in a parallel thread.\n- @JerryCoffin from wikipedia: \"the function body cannot declare variables\". That rules out most loops then. I wonder if that's due to a theoretical limit or a practical one. I feel like there's again, a subset of problems that can still be programmatically detected and solved, so should be.\n- @TJD: Er, yes I do. I'm writing the language. ;)\n- @wraithguard01, so your language doesn't support multiple source files?\n- @VaughnCato Hmm. How does one do that? for(int i = 0; i < 5; i--){} is infinite, obviously, but what are we doing in our head to know that? What about for(int i = 0; i < 5; i--){ if(i%2==0){ i+=2; }} is not infinite, though it looks like it would be at first.\n- @TJD: It does, but there's no way for another thread or source to get a reference to s, at least at that point in the code.\n- @wraithguard01: The limitations they decided on are based on both theoretical and practical limits, though I'm not sure there's a lot of documentation about exactly which was which. You can undoubtedly do more (especially if the rest of your language is simpler), but exactly how much I'm not sure.\n- It would be neat if static loops could be used like a preprocessor mechanism, then send generated code to an eval function; as long as the compiler knew it could be statically resolved, including the eval(), then hey, why not?\n- `for(int i = 0; i < 5; i--){}` doesn't look very infinite to me, unless you have weird ints that don't wrap. Anyway, it's easy enough to detect the \"simple cases\", so how far do you want to go with that?\n- @wraithguard01 Fun fact: Your loop you claim to be infinite, clearly is not (i goes `0,1,0,1` for ever) while the first one will usually end when the int wraps around ;)\n- Oh, oops. Well, you get my point, right? haha. @harold: The idea is that anything that can be detected should. In other words, C++'s constexpr should be able to happen automatically, based on certain rules.\n- That's not even a solved problem you know, it's only known that *not all* cases can be detected, not how many *can* be. C++'s constexpr doesn't allow everything either, in fact it is so restrictive that for your examples it would be completely useless - it can only handle recursion (no loops).\n- What usually screws this up is indirect addressing (or indexing into arrays which is equivalent) causing possible aliasing between values. Where you don't if aliasing occurs or not, you can't apply your algebraic laws. So you need really good flow analysis and alias resolution to optimize loops, unless they operate on \"whole\" values like OP's example.\n- yes, it should have been mentioned earlier that this is certainly true for most of the languages we use now. However, since this is for a new language that @wraithguard01 is making, there is an open field for some design compromises and restrictions, although I am not sure what those can be now.\n- If you have mutable arrays and indexes you've got aliasing troubles (think of array base + index as a pointer and should be obvious).\n- So, it appears as though all the cases you have listed here are convertable to a (python) for + range (for i in range(10)) construct. I can easily see how to optimize that, I wonder if that's the key here? If it can be converted to a foreach (or is already a foreach with static inputs), it can always be optimized out.","metadata":{"transformedAt":"2026-08-18T18:32:17.810Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":48,"totalLines":243,"estimatedTokens":1986}}591{"id":"stack-988623","source":"stackoverflow","questionId":988623,"title":"When C# compiler is written in C#, will one be able to modify it?","tags":["c#",".net","compiler-construction"],"text":"Title: When C# compiler is written in C#, will one be able to modify it?\nTags: c#, .net, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am wondering about it in terms of whether one can implement some new functionality for convenience.\n\nWill this be possible, when it's out?\n\nC# 5.0: compiler as a service:\n\nhttp://www.matthewlefevre.com/blog/entry.php/c-40-and-c-50/368\n\n========================================\n\nTop Answer:\nThat really depends on a lot of different things. Including but not limited too ...\n\n- Will Microsoft write a C# compiler in C# ?\n\n- Is the compiler delivered in source and binary form or just binary?\n\n- Do you want to write a binary extension or just modify the source?\n\n- What license will the source and binary be listed under?\n\n- Do you want to use the binary in a commercial or hobby project?\n\nNow as to what the answers to these questions are, I have no idea. Nor do I believe there is a stated answer one way or the other to the fundamental question #1. So the overall answer is a big \"Don't know\"\n\n========================================\n\nComments:\n- Please clarify your thinking.\n- Somebody please help me dislodge the \"H\" key from my right eye socket.\n- This follows right on the heels of his last questions that assumed there is only one C# compiler (stackoverflow.com/questions/988514/is-c-compiler-open-sourc&zwnj;&#8203;e). ::sigh::\n- These are the people and the questions that make me lose faith in reputation on this site.\n- Reputation on this site isn't supposed to reflect cleverness, just level of participation.\n- Reputation is supposed to reflect *helpfulness*. I used to defend the idea that \"Good questions add as much value as good answers.\" Not anymore, the worse, more general, or less answerable a question is the more upvotes it receives. I could just scream.\n- @mquander: It should have a correlation to the value you add to the site. But obviously this question/user breaks any possibility of that.\n- en.wikipedia.org/wiki/Grandfather_paradox Perhaps that will help\n- This question as well as a lot of others asked by this guy are one WTF after another.\n- Oh, well, if you don't APPROVE, then we'll stop talking.\n- It's not a matter of approval, but a level of respect which some of you guys lack. Let me tell you, you can't survive in the industry with an attitude like that.\n- I dont see anything wrong with the question, except the title is little misleading. But the other question is..\n- To offer C# as a service functionality.\n- What on Earth is \"C# as a service functionality?\"\n- The answer to 1 is yes. See my answer for info. The answer to 2 is binary. 3 - don't know. 4 - probably the same as the current license. 5 - don't know. Meta-programming will be possible, but not adding to the C# language and compiler directly.\n- @Richard, the key is \"future release\". Future can be a long or short time. There is no decision on when this may or may not happen.\n- And then you could write a compiler to parse the code from your compiler and create a meta compiler ad infinitum until your eyes bleed out of their sockets\n- Yes you could do that. There's a nice demo where you can see how easy that would be ... the link is in my answer.\n- They are however, porting the core of the C# compiler to managed code and are going to expose the APIs as a service. Straight from Anders Hejlberg. See my answer for the links.\n- FYI to whoever downvoted me: this question has been significantly changed from the original question which was \"When the C# compiler is written in C# will we be able to edit it?\" My answer was to that question, not whatever is has currently been edited into.","metadata":{"transformedAt":"2026-08-18T18:32:17.810Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":53,"estimatedTokens":912}}592{"id":"stack-43450179","source":"stackoverflow","questionId":43450179,"title":"vtable: Underlying algorithm","tags":["c++","compiler-construction","multiple-inheritance","vtable","vptr"],"text":"Title: vtable: Underlying algorithm\nTags: c++, compiler-construction, multiple-inheritance, vtable, vptr\nSource: Stack Overflow\n\nQuestion:\nMy understanding of vtables is that, if I have a class Cat with a virtual function speak() with subclasses Lion and HouseCat, there is a vtable which maps speak() to the correct implementation for each Subclass. So a call\n\n```\ncat.speak()\n```\n\nCompiles to\n\n```\ncat.vtable[0]()\n```\n\nThat is, a look-up in the vtable position 0 and a call of the function pointer in this position. \n\nMy question is: What happens on multiple inheritance?\n\nLet's add a class Pet. Pet has virtual functions speak() and eat(). HouseCat extends Pet, while Lion does not. Now, I need to make sure that\n\n```\npet.eat()\n```\n\nCompiles as \n\n```\npet.vtable[1]()\n```\n\nThat is vtable[0] needs to be speak(). Pet.eat needs to be slot 1. That is because cat.speak() needs to access slot 0 in the vtable, and if, for a HouseCat, slot 0 happens to be eat, this will go horribly wrong. \n\nHow does the compiler ensure that the vtable indexes fit together?\n\n========================================\n\nCode:\n```text\ncat.speak()\n```\n\n```text\ncat.vtable[0]()\n```\n\n```text\npet.eat()\n```\n\n```text\npet.vtable[1]()\n```\n\n```text\n[vptr | Cat fields]\n [0]: speak()\n```\n\n```text\n[vptr | Pet fields]\n [0]: eat()\n```\n\n```text\n[vptr | Cat fields | Lion fields]\n [0]: speak()\n```\n\n```text\n[vptr | Cat fields | vptr | Pet fields | HouseCat fields]\n [0]: speak()        [0]: eat()\n```\n\n```text\ncat.speak()\n```\n\n```text\nobj[0][0]()\n```\n\n```text\npet.eat()\n```\n\n```text\nobj[0][0]()\n```\n\n```text\nlion.speak()\n```\n\n```text\nobj[0][0]()\n```\n\n```text\nhouseCat.speak()\n```\n\n```text\nobj[0][0]()\n```\n\n```text\nhouseCat.eat()\n```\n\n```text\nobj[Cat size][0]()\n```\n\n```text\n(Cat)houseCat\n```\n\n```text\nobj\n```\n\n```text\n(Pet)houseCat\n```\n\n```text\nobj + Cat size\n```\n\n========================================\n\nComments:\n- The short answer is that the compiler ensures it because that's the compiler's job. That's what it is supposed to do. So it does it. The compiler creates separate vtables for the superclass when it is instantiated by itself, and when it is instantiated as part of the subclass, and assigns the appropriate vtable to the superclass instance, at instantiation time.\n- The even shorter answer is that vtables aren't specified and are implementation-dependent if present. For the gory details of at least one implementation see Stanley Lippman, *Inside the C++ Object Model.*\n- If you want all the details there's a description of one way of doing it in the Itanium C++ ABI.\n- The object may contain more than one pointer to a vtable - indeed it will have one per base class with virtual functions.\n- If `B` derives from `A`, then `B` is-a `A`: it means every detail of `A` is reproduced in `B`, notably the vptr and vtable. Then if `B` derives from `A1`, `A2`, `A3`, then every detail of `Ax` is reproduced in `B`, so it will end up with (at least) as many vptr as the base subobjects.\n- As a up to the other comments, the fact that there can be more than one vptr means it is not necessarily the case that `eat` must be in position 1. `Pet` could have a vtable specifying `{eat, speak}` while `Cat` keeps a vtable specifying `{speak}`.\n- @aschepler Most often, the order in the vtable follows the order in the class definition.\n- \"*then the vtable for the first base class and the vtable for the derived class will be merged*\" the \"first [non-virtual] base class\" is usually called the primary base class","metadata":{"transformedAt":"2026-08-18T18:32:17.810Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":22,"totalLines":143,"estimatedTokens":872}}593{"id":"stack-3629336","source":"stackoverflow","questionId":3629336,"title":"Why is it so hard to make 64-bit versions of software?","tags":["compiler-construction","64-bit","32bit-64bit"],"text":"Title: Why is it so hard to make 64-bit versions of software?\nTags: compiler-construction, 64-bit, 32bit-64bit\nSource: Stack Overflow\n\nQuestion:\nWhat are all the aspects must be taken into account when designing your software into 64-bit environment, and why wouldn't the same code work as 32-bit and 64-bit (when talking about applications)?\n\nDrivers obviously are a different beast, missing 64-bit drivers are infamous problem for almost all hardware. What's so different in that domain that it's next to impossible to find drivers?\n\n**Why is it so hard to make 64-bit versions of software?**\n\nEdit: Let's forget the basic flaws of old, buggy software with magic numbers, etc. and think you'd create the software yourself, to be compatible with both. What aspects do you need to take into account, and are there things you just can't overcome with current compiler design? All the missing 64-bit software can not simply be because people like code with magic numbers?! :)\n\n**Conclusion**: It seems to be all about human laziness and historical reasons, instead of technical reasons.\n\n========================================\n\nTop Answer:\nThe nutshell version: In the most popular family of languages — C and its children — the size and structure of data types is both very important and *implementation-defined*. In fact, C has a lot of implementation-dependent features. This means it's easy to write nonportable code. It's not impossible to write code that doesn't make assumptions about the underlying architecture, but it is really easy to depend on x86-specific behaviors without realizing what you've done until you try running the code in a different environment.\n\nIt's mainly these low-level features that make architecture independence hard. In higher-level languages like Python and C#, it's much easier.\n\n========================================\n\nCode:\n```text\nint\n```\n\n```text\nreinterpret_cast\n```\n\n```text\nint\n```\n\n```text\n4\n```\n\n```text\nsizeof(void*)\n```\n\n```text\n0xffffffff\n```\n\n```text\nINT_MAX\n```\n\n```text\nSetWindowLong\n```\n\n```text\nSetWindowLongPtr\n```\n\n```text\n0x80000\n```\n\n```text\n0x0\n```\n\n```text\n0x80000\n```\n\n```text\n0x200000000\n```\n\n```text\nx86\n```\n\n```text\namd64\n```\n\n```text\namd64\n```\n\n```text\namd64\n```\n\n```text\nx86\n```\n\n```text\nx86\n```\n\n```text\namd64\n```\n\n```text\namd64\n```\n\n```text\namd64\n```\n\n========================================\n\nComments:\n- You should be more specific if you want a good answer. For example, it is brainless to make 64 bit versions of .NET applications - set it to compile as Any CPU or x64. So obviously, you are not talking about .NET applications, but what *are* you talking about? :)\n- I'm asking in general what is the technical reason which apparently makes it impossible to use exact same codebase to build 32-bit and 64-bit apps. Since if it would be just about recompiling, all libraries would also be available as 64-bit etc, then all apps could too be just recompiled to 64-bit -> ie. no problem.\n- \"All the missing 64-bit software can not simply be because people like code with magic numbers?!\" - a lot of missing 64 bit software might be because it is not needed. Someone please correct me if I am wrong, but the rule of thumb I have heard is: unless you are potentially addressing more than 4 GB of memory, you should compile as 32 bit.\n- @Nate - At least on Windows, 32-bit programs run on an emulation layer (WOW64). Also, 32-bit programs on Windows don't see Windows the same way 64-bit programs do, because of things like registry/file redirection and other things for backwards compatibility.\n- @In silice: WOW64 is not (always) an emulation layer. On the most common AMD64 architecture the CPU is simply switched into 32-bit mode for 32-bit threads and no emulation is needed. See en.wikipedia.org/wiki/WoW64\n- @Nate Pinchot: 4GB limit is not really relevant here. I'm just trired of SW that doesn't work on 64-bit (despite WOW64) and when speaking of drivers memory isn't issue anyways. But I guess it all comes to human lazyness after all :(\n- Well of course--even if it does take an hour or less to make an application 64-bit ready, if nothing is gained from it, what's the point? That's an hour that you could have used to add a new feature to your product. There is also still the issue of dependencies not being available in 64-bit, which is actually more common than you think.\n- @musicfreak: I guess you have no idea how many systems are 64-bit now. If it was a one hour job, all software would be done 64-bit as well. The dependencies part is true as stated here already, thats what I'm referring as \"historical reasons\".\n- @Tuminoid: Not necessarily, due to the same laziness that you cited. :) For most applications, there is absolutely nothing to gain from writing a 64-bit version. 64-bit versions of Windows run 32-bit applications as well--same with Mac OS X as far as I'm aware. Linux is a different story, in which case you're right, but for Windows/Mac there's just no point.\n- But why is that a problem? Shouldn't compiler know better?\n- It does, but if the code uses `reinterpret_cast`, the compiler won't emit a diagnostic (which is why typecasting needs to be done carefully).\n- And I'm talking about reasonable quality software not filled with magic numbers etc. If it helps, think about creating software yourself to be compatible on both worlds.\n- Thanks for updating your answer, but that library issue would be a non-issue if it was all about just recompiling. All libs would also then exist as 32- and 64-bit versions. I'm searching for an answer why that isn't the case :)\n- @Tuminoid - that depends on the library authors actually taking the time to do that. :-) The library source code may not even be available anymore.\n- Even a perfectly portable program could be subject to stack overflows because the x64 calling convention can make a 64-bit program take more than twice as much stack space as the same 32-bit program.\n- I've got a 6month old Canon color laser printer and they didn't provide 64-bit drivers, major pain in the ***. So buying new hardware doesn't provide happiness...","metadata":{"transformedAt":"2026-08-18T18:32:17.811Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":22,"totalLines":132,"estimatedTokens":1523}}594{"id":"stack-2982735","source":"stackoverflow","questionId":2982735,"title":"every language eventually compiled into low-level computer language?","tags":["performance","compiler-construction","interpreter"],"text":"Title: every language eventually compiled into low-level computer language?\nTags: performance, compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nIsn't every language compiled into low-level computer language?\n\nIf so, shouldn't all languages have the same performance?\n\nJust wondering...\n\n========================================\n\nTop Answer:\nIt is easier and more efficient to map some languages into machine language than others. There is no easy analogy that I can think of for this. The closest I can come to is translating Italian to Spanish vs. translating a Khoisan language into Hawaiian.\n\nAnother analogy is saying \"Well, the laws of physics are what govern how every animal moves, so why do some animals move so much faster than others? Shouldn't they all just move at the same speed?\".\n\n========================================\n\nCode:\n```text\nlcc\n```\n\n```text\ntcc\n```\n\n========================================\n\nComments:\n- I think, maybe, you should write up programs in a couple of different compiled languages and look at the resulting machine code. You'll see that the machine code translation of the program, even for programs that perform the exact same task in approximately the same way, is going to be pretty different for different languages.\n- Even languages that are interpreted are translated into machine code in some sense.\n- It's becoming *more* possible to write programs in C that run faster than their assembly equivalents and JITed-Java programs that run faster than their C equivalents. Modern compilers are pretty damn good; they regularly do stuff that humans simply can't. Of course, humans regularly do stuff they simply can't, so...\n- @Omnifarious: true, I guess I meant that some languages never get run as machine code without having the interpreter's machine code interspersed. @Vuntic: I have to disagree. It's possible in some cases to write an assembly program that runs faster than any C program which has the same function, but not the other way around - for instance, copy the output of the C compiler and you get an assembly program which runs just as fast.\n- @Omnifarious: interpreted languages are translated into actions and declarations. These may or may not be implemented as machine code.\n- but if php is interpreted and compiled once and cached, then it shouldn't be slower than C if we are assuming that it compiles into the same machine code? this is the part i have difficulties to understand.\n- @gayer - php is NOT compiled into machine code. It is interpreted by php interpreter, dynamically. I'm not 100% what gets cached (I am guessing the parsed grammar tree but don't know enough about php to be sure) but it is certainly NOT the machine code.\n- @fayer, PHP doesn't exactly that model, but even if it did, it would never compile into the same machine code as an equivalent C program. Yes, they all compile into machine code, but not the same machine code, and, practically speaking, they never will.\n- +1: this is a much better answer than @Omnifarious provided, should have more votes too.\n- @High I bet at least 50% of all votes arrive in the first hour :-)\n- I agree, I think it's a technically better answer too. But people also like simple and seemingly profound analogies, and I think good analogies are sometimes better than answers that are more technically accurate in detail because they allow you to apply the same thought process to other problems. I still think your answer is better, but I can see why people voted more for mine.","metadata":{"transformedAt":"2026-08-18T18:32:17.811Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":43,"estimatedTokens":881}}595{"id":"stack-36378244","source":"stackoverflow","questionId":36378244,"title":"How is standard library for programming language implemented?","tags":["compiler-construction","linker","language-agnostic","object-files"],"text":"Title: How is standard library for programming language implemented?\nTags: compiler-construction, linker, language-agnostic, object-files\nSource: Stack Overflow\n\nQuestion:\nI have problem understanding how are standard libraries for programming languages, other than C, written.\n\nAs far as i understand, C standard libraries can be implemented in mixture of C and assembler, where assembler is needed so system calls can be invoked and thus \nfopen, fscanf ... can be used.\n\nHow do the other programming languages accomplish this functionality(working with i/o, files, all other stuff for which system calls are needed) with their standard libary? Do they all allow inlining of assembler like C or is there some other way?\n\nI have read that C and its standard library can be used, for implementing other languages libraries, but i am not sure how this is done.\n\n**edit1.** Trying to be more specific.\n\n(Language for which standard library is implemented is referred to as **new_lang**.)\n\nIf someone can elaborate how second approach is done(using C runtime) at the object code level and implementation level, because somethings i cant get my head around are:\n\n- Is C runtime invoked using C syntax or new_lang syntax? How do we call ssize_t write(int fd, const void *buf, size_t count) from somewhere within new_lang library?\n\n- What happens if new_lang doesn't have pointers as data types, how is second argument, **const void *buf** to write passed from new_lang? How does new_lang **C runtime api** if it doesn't have C data types?\nIf some function from new_lang library calls C runtime, does it mean that it must obey its **abi**? Data sizes for types of integer, char, must match in new_lang and C for given platform(and other stuff which is specified by abi, are arguments passed by stack or registers etc.)? \nIsn't this little overrestricting, for example what if new_lang needs more bytes to be reserved for char?\n\nI tried to be as general as possible, but i am not sure how to explain the problem without going into a little detail.\n\n========================================\n\nComments:\n- I'm not flagging this question, but it strikes me as being too broad.\n- How is the linking phase done, if C runtime is used? How can object code from, lets say pascal and c be compatible. Even if they same abi, data sizes of some data types can differ and thus c runtime code calculates differently where arguments to function are. Exampple: If i wanted to implement writeln from pascal i would use printf for example and when WriteLn('something') is found in pascal file it will ultimately call printf(\"something\"). But what if char in pascal is 2 bytes and in c 1 byte. When code for printf calculates where its arguments are using ebp register it will \"miss\".\n- Yes, so used types must match. But that is if you link code between two different C compilers too, and is simply the reality of using hybrid systems. printf is no substitute to writeln. Writeln is internally quite complex and routes to Pascal file I/O. (taking advantage of buffering if need be), on *nix systems ultimately routed to write(2). Worse trying to would cause valid code like writeln('%s%d%s') to crash\n- I realize WriteLn is more complex, i tried to emphasize the passing of arguments(following api and abi) which i don't understand and so i made my comment about printf and writeln unrealistically simple(and i only had 600 chars to explain it :)).\n I edited the question so it explains the question more precisely. (If we are on *nix system, how are arguments passed to that write(2) call...). Btw. Voted your answer up but my reputation isn't high enough for change to be updated immediately.\n- I tried to write up an answer. It will probably be very unsatisfactory, but the reality is simply that very little is guaranteed for other languages to interface with the C system, specially if you are not based on the C system (e.g. your compiler is written in C or something compatible (e.g. C++)). Free Pascal is a nice example. The only recommendation I can give you is to not try to solve all problems directly. Better start with a few good targets, (e.g. Linux/x86 /arm and Windows) and prepare them well and in an userfriendly way, and forget about catering to everybody in 1.0","metadata":{"transformedAt":"2026-08-18T18:32:17.811Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":37,"estimatedTokens":1064}}596{"id":"stack-696769","source":"stackoverflow","questionId":696769,"title":"Why does the order affect the rounding when adding multiple doubles in C#","tags":["c#",".net","compiler-construction","rounding","precision"],"text":"Title: Why does the order affect the rounding when adding multiple doubles in C#\nTags: c#, .net, compiler-construction, rounding, precision\nSource: Stack Overflow\n\nQuestion:\nConsider the following C# code:\n\n```\ndouble result1 = 1.0 + 1.1 + 1.2;\ndouble result2 = 1.2 + 1.0 + 1.1;\n\nif (result1 == result2)\n{\n ...\n}\n```\n\nresult1 should always equal result2 right? The thing is, it doesn't. result1 is 3.3 and result2 is 3.3000000000000003. The only difference is the order of the constants.\n\nI know that doubles are implemented in such a way that rounding issues can occur. I'm aware that I can use decimals instead if I need absolute precision. Or that I can use Math.Round() in my if statement. I'm just a nerd who wants to understand what the C# compiler is doing. Can anyone tell me?\n\n**Edit:**\n\nThanks to everyone who's so far suggested reading up on floating point arithmetic and/or talked about the inherent inaccuracy of how the CPU handles doubles. But I feel the main thrust of my question is still unanswered. Which is my fault for not phrasing it correctly. Let me put it like this:\n\nBreaking down the above code, I would expect the following operations to be happening:\n\n```\ndouble r1 = 1.1 + 1.2;\ndouble r2 = 1.0 + r1\ndouble r3 = 1.0 + 1.1\ndouble r4 = 1.2 + r3\n```\n\nLet's assume that each of the above additions had a rounding error (numbered e1..e4). So r1 contains rounding error e1, r2 includes rounding errors e1 + e2, r3 contains e3 and r4 contains e3 + e4.\n\nNow, I don't know how exactly how the rounding errors happen but I would have expected e1+e2 to equal e3+e4. Clearly it doesn't, but that seems somehow wrong to me. Another thing is that when I run the above code, I don't get any rounding errors. That's what makes me think it's the C# compiler that's doing something weird rather than the CPU.\n\nI know I'm asking a lot and maybe the best answer anyone can give is to go and do a PHD in CPU design, but I just thought I'd ask.\n\n**Edit 2**\n\nLooking at the IL from my original code sample, it's clear that it's the compiler not the CPU that's doing this:\n\n```\n.method private hidebysig static void Main(string[] args) cil managed\n{\n.entrypoint\n .maxstack 1\n .locals init (\n [0] float64 result1,\n [1] float64 result2)\n L_0000: nop \n L_0001: ldc.r8 3.3\n L_000a: stloc.0 \n L_000b: ldc.r8 3.3000000000000003\n L_0014: stloc.1 \n L_0015: ret \n}\n```\n\nThe compiler is adding up the numbers for me!\n\n========================================\n\nTop Answer:\nThe c# compiler isn't doing anything. The CPU is.\n\nif you have A in a CPU register, and you then add B, the result stored in that register is A+B, approximated to the floating precision used\n\nIf you then add C, the error adds up. This error addition is not a transitive operation, thus the final difference.\n\n========================================\n\nCode:\n```text\ndouble result1 = 1.0 + 1.1 + 1.2;\ndouble result2 = 1.2 + 1.0 + 1.1;\n\nif (result1 == result2)\n{\n    ...\n}\n```\n\n```text\ndouble r1 = 1.1 + 1.2;\ndouble r2 = 1.0 + r1\ndouble r3 = 1.0 + 1.1\ndouble r4 = 1.2 + r3\n```\n\n```text\n.method private hidebysig static void Main(string[] args) cil managed\n{\n.entrypoint\n    .maxstack 1\n    .locals init (\n        [0] float64 result1,\n        [1] float64 result2)\n    L_0000: nop \n    L_0001: ldc.r8 3.3\n    L_000a: stloc.0 \n    L_000b: ldc.r8 3.3000000000000003\n    L_0014: stloc.1 \n    L_0015: ret \n}\n```\n\n```text\nfloor( 5/3 ) + floor( 2/3 + 1 )\n```\n\n```text\nfloor( 5/3 + 2/3 ) + floor( 1 )\n```\n\n```text\n1.0            =  1.00000000000\n1.1            =  1.00011001100\n1.2            =  1.00110011001\n\n1.0 + 1.1      = 10.00011001100 // extended during sum\nr1 = 1.0 + 1.1 = 10.0001100110  // truncated to 12 bit\nr1  + 1.2      = 11.01001100101 // extended during sum\nr2 = r1  + 1.2 = 11.0100110010  // truncated to 12 bit\n\n1.1 + 1.2      = 10.01001100110 // extended during sum\nr3 = 1.1 + 1.2 = 10.0100110011  // truncated to 12 bit\nr3 + 1.0       = 11.01001100110 // extended during sum\nr4 = r3  + 1.0 = 11.0100110011  // truncated to 12 bit\n```\n\n```text\n1\n```\n\n```text\n0\n```\n\n```text\ndouble epsilon = 0.0000001;\nif (abs(result1 - result2) <= epsilon)\n{\n    ...\n}\n```\n\n```text\ndouble result1 = 2.1 + 1.2;\ndouble result2 = 2.2 + 1.1;\n```\n\n```text\n1 + 1 + 1 + 10 = 12 (or 14, depending on rounding)\n10 + 1 + 1 + 1 = 10\n```\n\n```text\nstep 1: 3.00000000003 + 3.00000000003 = 6.00000000005\nstep 2: 6.00000000005 + 2.99999999997 = 9.00000000002\n```\n\n```text\nstep 1: 3.00000000003 + 2.99999999997 = 6.00000000001\nstep 2: 6.00000000001 + 3.00000000003 = 9.00000000004\n```\n\n========================================\n\nComments:\n- But if A+B produces a rounding error and A+C doesn't, wouldn't (A+C)+B still have the same rounding error as A+B? I get that rounding errors add up, I'm asking why is the order important.\n- no, A+B produces an error e1, and then A+B +C produces another error e2, so the final error is e1+e2. If A+C provides no error, A+C +B provides a single error e3, which has no reason to match e1+e2. You should google for floating point arithmetic for more info.\n- I guess I'm not making myself clear, let me rephrase \"if A+B makes e1, (A+B)+C makes e2, A+C makes e3 and (A+C)+B makes e4, why does e1+e2 not equal e3+e4? So far, you've told me that it doesn't but not why it doesn't.\"\n- e3 and e4 may be zero, where e1 and e2 aren't.\n- @dant : try to compute 1/3*3 : the result is 1 because of the obvious simplification. now try to compute 2/3 (result is 0.6666...), add 0.3333,the result is now 0.999999...instead of 1). The same kind of reasoning goes for floating point arithmetic. It's well explained on wikipedia (see link below)\n- I've always assumed that you can't compare two floating point numbers for equality. Whenever I find such a thing in code, unless it's comparing to 0, I get chills.\n- I admit that floating point arithmetic scares me. As a rule of thumb, I refuse to use it unless I must.\n- Use them when useful: where approximations are OK. Physical simulation, externally acquired measurements, graphic layout (for varying levels of permissible approximation). Money -- not so much! There is no tool so simple you can't possibly hurt yourself with it... (hm, fatal tweezer accidents?).\n- The example is good, but you have changed the order of the operations compared to the original code. The OP did the same in the first edit.","metadata":{"transformedAt":"2026-08-18T18:32:17.812Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":181,"estimatedTokens":1580}}597{"id":"stack-6445553","source":"stackoverflow","questionId":6445553,"title":"Why compiling into intermediate code?","tags":["c#","java","actionscript","compiler-construction"],"text":"Title: Why compiling into intermediate code?\nTags: c#, java, actionscript, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nWhy do Actionscript, Java, C#, etc. compile into intermediate code?\nI'm aware of cross-platform benefits of using intermediate code.\n\nThe question is: What is the benefit of compiling to intermediate code, compared to scripts (JS, Python, PHP, Perl, etc.) that are interpreted?\n\nIs it just for code obfuscation? Or what?\n\nIn addition what is the benefit compared to compiling to native code?\n\n========================================\n\nTop Answer:\nIt is much faster to parse and JIT-compile IL code than to parse a high-level language like Java or especially C# (which has more features).\n\nIt also allows developers to use new language features without updating anything on the end-users' machines. (eg, LINQBridge)\n\n========================================\n\nCode:\n```text\nz = a + b - (c * d)\n```\n\n========================================\n\nComments:\n- It isn't obfuscation. Not in the slightest\n- ASP.NET is a platform, not programming language.","metadata":{"transformedAt":"2026-08-18T18:32:17.812Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":33,"estimatedTokens":269}}598{"id":"stack-2740994","source":"stackoverflow","questionId":2740994,"title":"What is the language of compilers? Are they written with different languages?","tags":["compiler-construction"],"text":"Title: What is the language of compilers? Are they written with different languages?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nAre compilers in different languages?\n\n========================================\n\nTop Answer:\nA compiler could probably be written in any language. In its most basic form, a compiler merely converts code from one language to another. In the sense that most people use the term \"compiler\" today, they are referring to something that takes in source code of some higher level language and converts it to either assembly or some low level intermediate language (CIL).\n\n========================================\n\nComments:\n- Not always: en.wikipedia.org/wiki/Bootstrapping_(compilers)\n- possible duplicate of stackoverflow.com/questions/193560/&hellip;\n- possible duplicate of When someone writes a new programming language, what do they write it IN?","metadata":{"transformedAt":"2026-08-18T18:32:17.812Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":18,"estimatedTokens":223}}599{"id":"stack-323407","source":"stackoverflow","questionId":323407,"title":"What's the Magic Behind Escape(\\) Character","tags":["c++","c","compiler-construction","escaping","backslash"],"text":"Title: What's the Magic Behind Escape(\\) Character\nTags: c++, c, compiler-construction, escaping, backslash\nSource: Stack Overflow\n\nQuestion:\nHow does the C/C++ compiler manipulate the escape character [\"\\\"] in source code? How is compiler grammar written for processing that character? What does the compiler do after encountering that character?\n\n========================================\n\nTop Answer:\nAn interesting note on this subject is On Trusting Trust [PDF link].\n\nThe paper describes one way a compiler could handle this problem exactly, shows how the c-written-in-c compiler does not have an explicit translation of the codes into ASCII values; and how to bootstrap a new escape code into the compiler so that the understanding of the ASCII value for the new code is also implicit.\n\n========================================\n\nCode:\n```text\n\\n\n```\n\n```text\n\\a\n```\n\n```text\n\\n\n```\n\n```text\n\\xNUM\n```\n\n========================================\n\nComments:\n- at least try to spell your created tags correctly...\n- sorry for that, question was typed hastily\n- This character (?) is missing in your questions. What's the rush, though?\n- Had urgent meeting minute back question popped up so typed liked that. thanks for modifying :)\n- Anyone care to comment on why this question is being pushed down? Is there something I am missing? To me it seems as a fair and correct question to ask.\n- Maybe I'm late, but I don't see this question being pushed down at all.\n- Don't forget \\uXXXX and \\UXXXXXXXX in C99.\n- \\NNN (where N are digits 0-7) represents a byte-number in Octal. \\0, commonly referred to as a Null-Terminator, is really the same as \\000. I think 255 is \\377\n- Aaron, that's right. there are other uses of \\ too. i thought i would list the most important (by subjective measuring).\n- That is a very interesting paper which I'd not read in a while. I'd forgotten the section on boot-strapping a compiler to understand a new escape character sequence such as '\\v' - so I didn't immediately see its relevance. I'm glad I checked it out.","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":46,"estimatedTokens":511}}600{"id":"stack-5289827","source":"stackoverflow","questionId":5289827,"title":"What does it mean to say that C was compiled in C?","tags":["c","compiler-construction","programming-languages","abap"],"text":"Title: What does it mean to say that C was compiled in C?\nTags: c, compiler-construction, programming-languages, abap\nSource: Stack Overflow\n\nQuestion:\nI've seen stuff like this written... *C was written in C*, or *ABAP was written in ABAP*..\n\nI've even seen one guy saying that this is one of the criteria to sort languages into *true* programming languages.\n\nI'd like someone to explain to me what this means.\n\nThanks in advance.\n\nEDIT:Changed title from: \"*C was made using C*\" to \"*C was **compiled** using C*\".\n\n========================================\n\nTop Answer:\nIt means that the C compiler was written in C.\n\nInitially, that's a problem because how can you write a compiler in C if not compiler exists yet for C.\n\n**Update:**\n\nIf you write a compiler in a differnt language, people ask themselves whether the language is too limited for building a compiler or whether the inventors of the language aren't convinced of their language and preferred another one.\n\nBut there are good reasons for writing a compiler in a different language.\n\n========================================\n\nComments:\n- If Firefox did not reject your link as an \"untrusted connection\", I'd take a look.\n- +1 for not saying 'self hosting' and giving a link.\n- Exactly. How was it possible?\n- @Felipe: If the question about how to self-host/bootstrap, this questions is a dupe of e.g. stackoverflow.com/questions/193560/&hellip;.\n- @Felipe: nowadays the simplest way is to cross-compile the compiler for the target architecture from a machine for which the compiler already exists. Other methods are illustrated here.\n- @Felipe: No, we'll just close it as duplicate. Every dupe makes it more likely that people finds the answer before asking again (if they search at all - a debatable assumption)\n- @delnan Who's **we** in *we'll close it as duplicate*?\n- @Felipe Almeida: As your reputation goes up you get more privileges, one of those being the ability to close questions. Many SO users have this level of reputation and will see this and add their vote to close. Once the vote-to-close total reaches 5 the question is closed.\n- @GregS Thanks for the info. Sorry for my ignorance.\n- I think the questioner wants to know something of the bootstrap process. Obviously the very first compiler for \"X\" can't have been written in X.\n- It is true. At that time, computers have very limited memory. Nobody believes that a high-level language can compile to a small enough footprint to write large complex programs that fit in memory. C, however, is designed to map almost one-to-one to the PDP assembly language instructions, make the compiled code extremely compact. And, as we say, the rest is history. +1 on this bit of history.\n- I'm no Lisp user, but of course you can write a compiler in it. It has an unusual syntax, but that doesn't make it less powerful. And the way Lisp treats code and data as closely related might make it even easier to write a Lisp compiler in Lisp.","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":43,"estimatedTokens":739}}601{"id":"stack-14065337","source":"stackoverflow","questionId":14065337,"title":"What does the unary operator \"-\" do on unsigned data types in C/C++ (and on different compilers)?","tags":["c++","c","compiler-construction","unsigned"],"text":"Title: What does the unary operator \"-\" do on unsigned data types in C/C++ (and on different compilers)?\nTags: c++, c, compiler-construction, unsigned\nSource: Stack Overflow\n\nQuestion:\nFor example:\n\n```\nunsigned int numA = 66; // or anything really\nunsigned int numB = -numA;\nunsigned int numC = numA & numB\n```\n\nI understand that the bitwise complement operator can be used to get the two's complement (in conjunction with a +1).\n\nThe reason I ask is because I stumbled upon this in some code for a chess engine. Chess engines do a lot of 'hacky' things to get absolute speed, especially in the move generation functions that are called millions of times per second. (It doesn't help that it was an example of magic bitboard move generation - the most optimized of them all). This chess engine code in particular only works correctly under gcc compilation (I suspect).\n\nHow do different compilers treat this? In particular, how does gcc handle this compared to the C++ compiler in VS Studio 2012 Express.\n\nThanks.\n\n========================================\n\nTop Answer:\nHere's what C++ standard says under section 4.7.2 (Integral conversions):\n\n If the destination type is unsigned, the resulting value is the least\n unsigned integer congruent to the source integer (modulo 2n where n\n is the number of bits used to represent the unsigned type). [ Note: In\n a two’s complement representation, this conversion is conceptual and\n there is no change in the bit pattern (if there is no truncation).\n —end note ]\n\nHope this answers your question.\n\n========================================\n\nCode:\n```text\nunsigned int numA = 66; // or anything really\nunsigned int numB = -numA;\nunsigned int numC = numA & numB\n```\n\n```text\n-num\n```\n\n```text\nunsigned int numA = 66;\nunsigned int numB = -numA;\n```\n\n```text\nUINT_MAX - 66U + 1U\n```\n\n```text\nnumB\n```\n\n```text\nU\n```\n\n========================================\n\nComments:\n- That doesn't directly answer the question. There is no conversion in the OP's sample code (other the conversion of `66` from `int` to `unsigned int`). The unary \"-\" is being applied to an `unsigned` value, and yields an `unsigned` result. (It does explain the meaning of `unsigned int max = -1;`, but that wasn't the question.)\n- The \"number of bits\" doesn't include any padding bits (of which there are none in most implementations), so the expression involving `CHAR_BIT` (which I can't reproduce in a comment) isn't *always* accurate.\n- @KeithThompson Yes, I actually wondered about this. The Standard explicitly says in 3.9.1/1 that in integer types other than `char`, not all bits necessarily participate in the value representation. But in the section quoted above, it says `n` is the number of bits (not the number of bits that participate in the value representation). Clearly, padding bits can't possibly count (otherwise it doesn't make sense), but the wording in the Standard isn't quite precise here.\n- Check precedence table, unary `-` has higher precedence than `&#47;`\n- Thanks. And only too late, I did. The real surprise, of course, was that \"-(unsigned)\" is of type unsigned.","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":73,"estimatedTokens":776}}602{"id":"stack-336755","source":"stackoverflow","questionId":336755,"title":"Left to right expression evaluation","tags":["c#","language-agnostic","compiler-construction","expression","expression-evaluation"],"text":"Title: Left to right expression evaluation\nTags: c#, language-agnostic, compiler-construction, expression, expression-evaluation\nSource: Stack Overflow\n\nQuestion:\nIn C# is it guaranteed that expressions are evaluated left to right? \n\nFor example:\n\n```\nmyClass = GetClass(); \nif (myClass == null || myClass.Property > 0) \n continue;\n```\n\nAre there any languages that do not comply?\n\n========================================\n\nTop Answer:\nShort-circuiting is described in section 7.11 of the C# 3.0 spec:\n\n The operation x || y corresponds to\n the operation x | y, except that y is\n evaluated only if x is not true.\n\nSo yes, you're fine.\n\nAs for other languages - I never like to speak for *all* languages. In VB.NET, you can use OrElse and AndAlso which are short-circuited, but plain Or and And aren't.\n\n========================================\n\nCode:\n```text\nmyClass = GetClass();  \nif (myClass == null || myClass.Property > 0)  \n    continue;\n```\n\n```text\nMyClass == null\n```\n\n```text\nmyClass.Property > 0\n```\n\n```text\nMyClass == null\n```\n\n```text\n||\n```\n\n```text\nor\n```\n\n```text\nand\n```\n\n========================================\n\nComments:\n- In C# the precise order is defined.","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":67,"estimatedTokens":295}}603{"id":"stack-5358190","source":"stackoverflow","questionId":5358190,"title":"Moving \"if\" statement condition to a local variable makes C# compiler unhappy","tags":["c#","compiler-construction"],"text":"Title: Moving \"if\" statement condition to a local variable makes C# compiler unhappy\nTags: c#, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nCould you please explain me the reason of the following situation.\n\nToday I wrote the code (only variables names are changed):\n\n```\nprivate void Foo() \n{\n int firstInteger, secondInteger;\n const string firstStringValue = \"1\", secondStringValue = \"2\";\n\n if (!string.IsNullOrWhiteSpace(firstStringValue) && int.TryParse(firstStringValue, out firstInteger) &&\n !string.IsNullOrWhiteSpace(secondStringValue) && int.TryParse(secondStringValue, out secondInteger))\n {\n // Using firstInteger and secondInteger here\n firstInteger++;\n secondInteger++;\n }\n}\n```\n\nEverything was fine until I decided to move the ***if*** condition to a variable:\n\n```\nprivate void Foo()\n{\n int firstInteger, secondInteger;\n const string firstStringValue = \"1\", secondStringValue = \"2\";\n\n bool firstIntegerAndSecondIntegerAreSpecified = \n !string.IsNullOrWhiteSpace(firstStringValue) && int.TryParse(firstStringValue, out firstInteger) &&\n !string.IsNullOrWhiteSpace(secondStringValue) && int.TryParse(secondStringValue, out secondInteger);\n\n if (firstIntegerAndSecondIntegerAreSpecified)\n {\n // Use firstInteger and secondInteger here\n firstInteger++;\n secondInteger++;\n }\n}\n```\n\nNow the compiler underlines *firstInteger* and *secondInteger* variables with error \"Local variable might not be initialized before accessing\".\n\nBut why? The only thing I made is refactored the code a bit. And as I see it the logic is the same.\n\n========================================\n\nTop Answer:\nThe compiler is not built to be clever enough to figure out that a true value in the boolean variable ensures that the values in the integer are set. The compiler only tracks execution paths, not varaible values.\n\nIn the first case the compiler knows that it's impossible to enter the if statement without the `TryParse` calls setting the variables. In the second case the if statement is detached from the `TryParse` calls, so the compiler would have to track the variable value to figure out the relation.\n\n========================================\n\nCode:\n```text\nprivate void Foo() \n{\n    int firstInteger, secondInteger;\n    const string firstStringValue = \"1\", secondStringValue = \"2\";\n\n    if (!string.IsNullOrWhiteSpace(firstStringValue) && int.TryParse(firstStringValue, out firstInteger) &&\n        !string.IsNullOrWhiteSpace(secondStringValue) && int.TryParse(secondStringValue, out secondInteger))\n    {\n        // Using firstInteger and secondInteger here\n        firstInteger++;\n        secondInteger++;\n    }\n}\n```\n\n```text\nprivate void Foo()\n{\n    int firstInteger, secondInteger;\n    const string firstStringValue = \"1\", secondStringValue = \"2\";\n\n    bool firstIntegerAndSecondIntegerAreSpecified = \n        !string.IsNullOrWhiteSpace(firstStringValue) && int.TryParse(firstStringValue, out firstInteger) &&\n        !string.IsNullOrWhiteSpace(secondStringValue) && int.TryParse(secondStringValue, out secondInteger);\n\n    if (firstIntegerAndSecondIntegerAreSpecified)\n    {\n        // Use firstInteger and secondInteger here\n        firstInteger++;\n        secondInteger++;\n    }\n}\n```\n\n```text\nfirstIntegerAndSecondIntegerAreSpecified\n```\n\n```text\nint.TryParse\n```\n\n```text\nint.TryParse\n```\n\n```text\nout\n```\n\n```text\nif\n```\n\n```text\nfirstIntegerAndSecondIntegerAreSpecified\n```\n\n```text\nif\n```\n\n```text\nTryParse\n```\n\n```text\nTryParse\n```\n\n```text\nif\n```\n\n```text\nTryParse\n```\n\n```text\nout\n```\n\n```text\nif\n```\n\n```text\nbool\n```\n\n```text\nint.TryParse(firstStringValue, out firstInteger)\n    firstInteger++;\n\nint.TryParse(secondStringValue, out secondInteger)\n    secondInteger++;\n```\n\n```text\nNullOrWhiteSpace\n```\n\n========================================\n\nComments:\n- Why are you checking for NullOrWhitespace? int.TryParse takes care of it for you (i.e. whitespace results in a false return value), thereby making your code so much more readable.\n- Thanks for the tip, **spender**. You're right, better to use int.TryParse() here only.\n- Yes, now I get it. Thanks for helping!\n- I think, there would be some 'logical' demand because of which questioner is incrementing both the integers together, when both the IFs returns true after parsing integer value. If this is the case, your suggestion would break the logic. Otherwise, it's definitely OK. Anyway, there is no need of NullOrWhiteSpace - you are right!","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":18,"totalLines":170,"estimatedTokens":1107}}604{"id":"stack-6720776","source":"stackoverflow","questionId":6720776,"title":"Parser Generator for Objective C?","tags":["objective-c","parsing","compiler-construction","programming-languages","parser-generator"],"text":"Title: Parser Generator for Objective C?\nTags: objective-c, parsing, compiler-construction, programming-languages, parser-generator\nSource: Stack Overflow\n\nQuestion:\nI've been advised to use a Parser Generator to create a parser for my domain specific language. \n\nIdeally I'd like it to output an objective-C parser and take BNF input to describe the language.\n\nThere seems to be very little choice.. is it possible to use one of the more popular Parser Generators with objective C?\n\nThanks!\n\n========================================\n\nTop Answer:\nThere's (of course) your expected answers of things like Bison or YACC or whatever, but if you want a native Objective-C solution, there are the couple that have already been mentioned. (Hammer and ParseKit) Another one that I know of is \"CoreParse\": https://github.com/beelsebob/CoreParse\n\n========================================\n\nComments:\n- Looks great thanks! Do you know if the BNF-like grammer for ParseKit suppports recursion? it's just I didn't see it mentioned..\n- I'm pretty sure it does, but if you are encoding left-recursive grammars that way, you'll end up in an infinite loop unless you're very careful. So, I'd suggest you try to factor your recursion into repetition, which will probably serve you better.","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":23,"estimatedTokens":318}}605{"id":"stack-1825298","source":"stackoverflow","questionId":1825298,"title":"Your favourite Abstract Syntax Tree optimization","tags":["compiler-construction","compiler-optimization","abstract-syntax-tree"],"text":"Title: Your favourite Abstract Syntax Tree optimization\nTags: compiler-construction, compiler-optimization, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nIf you were constructing a compiler, what optimization at the AST level would be the nicest to have?\n\n========================================\n\nTop Answer:\nMostly you can't do interesting optimizations at the AST level, because you need information how how data flows from one part of the program to another. While data flow is implicit in the meaning of the AST, it isn't easily determined by inspecting just the AST, which is why people building compilers and optimizers build other program representations (including symbol tables, control flow graphs, reaching definitions, data flow and SSA forms, etc.).\n\nHaving a parser for a language is the easy part of analyzing/manipulating that language.\nYou need all that other stuff to do a good job.\n\nIf you *do* have all those other representations, you can think about doing optimizations at the AST level. Most folks building compilers don't bother; they convert to a data flow representation and simply optimize that. But if you want to reproduce source code with changes, you need the AST. You'll also need a prettyprinter to enable you to regenerate the source code. If you go this far, you'll end up with a source-to-source\nprogram transformation system.\n\nThe DMS Software Reengineering Toolkit is a system that transforms ASTs, using all these other representations to enable the analyses needed by the transforms.\n\n========================================\n\nCode:\n```text\nRETURN\n    CALL f\n        ARGS x, y, ...\n```\n\n```text\na = x; b = y\nJUMP to root of tree\n```\n\n```text\nf\n```\n\n```text\nf(a, b)\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":41,"estimatedTokens":430}}606{"id":"stack-510309","source":"stackoverflow","questionId":510309,"title":"The compilation process","tags":["visual-c++","compiler-construction","lua","visual-c++-6"],"text":"Title: The compilation process\nTags: visual-c++, compiler-construction, lua, visual-c++-6\nSource: Stack Overflow\n\nQuestion:\nCan anyone explain how compilation works?\n\nI can't seem to figure out how compilation works..\n\nTo be more specific, here's an example.. I'm trying to write some code in MSVC++ 6 to load a Lua state..\n\nI've already:\n\n- set the additional directories for the library and include files to the right directories\n\n- used extern \"C\" (because Lua is C only or so I hear)\n\n### include'd the right header files\n\nBut i'm still getting some errors in MSVC++6 about unresolved external symbols (for the Lua functions that I used).\n\nAs much as I'd like to know how to solve this problem and move on, I think it would be much better for me if I came to understand the underlying processes involved, so could anyone perhaps write a nice explanation for this? What I'm looking to know is the process.. It could look like this:\n\nStep 1:\n\n- Input: Source code(s)\n\n- Process: Parsing (perhaps add more detail here)\n\n- Output: whatever is output here..\n\nStep 2:\n\n- Input: Whatever was output from step 1, plus maybe whatever else is needed (libraries? DLLs? .so? .lib? )\n\n- Process: whatever is done with the input\n\n- Output: whatever is output\n\nand so on..\n\nThanks..\n\nMaybe this will explain what symbols are, what exactly \"linking\" is, what \"object\" code or whatever is..\n\nThanks.. Sorry for being such a noob..\n\nP.S. This doesn't have to be language specific.. But feel free to express it in the language you're most comfortable in.. :)\n\n**EDIT**: So anyway, I was able to get the errors resolved, it turns out that I have to manually add the .lib file to the project; simply specifying the library directory (where the .lib resides) in the IDE settings or project settings does not work..\n\nHowever, the answers below have somewhat helped me understand the process better. Many thanks!.. If anyone still wants to write up a thorough guide, please do.. :)\n\n**EDIT**: Just for additional reference, I found two articles by one author (Mike Diehl) to explain this quite well.. :)\nExamining the Compilation Process: Part 1\nExamining the Compilation Process: Part 2\n\n========================================\n\nTop Answer:\nStep 1 - Compiler:\n\n- Input: Source code file[s]\n\n- Process: Parsing source code and translating into machine code\nOutput: Object file[s], which consist[s] of:\n\n- The names of symbols which are defined in this object, and which this object file \"exports\"\n\n- The machine code associated with each symbol that's defined in this object file\n\n- The names of symbols which are not defined in this object file, but on which the software in this object file depends and to which it must subsequently be linked, i.e. names which this object file \"imports\"\n\nStep 2 - Linking:\n\nInput:\n\n- Object file[s] from step 1\n\n- Libraries of other objects (e.g. from the O/S and other software)\n\nProcess:\n\n- For each object that you want to link\n\n- Get the list of symbols which this object imports\n\n- Find these symbols in other libraries\n\n- Link the corresponding libraries to your object files\n\n- Output: a single, executable file, which includes the machine code from all all your objects, plus the objects from libraries which were imported (linked) to your objects.\n\n========================================\n\nCode:\n```text\n#include <bob.h>\nint x = bob_fn(7);\n\ncc -c -o xx.obj xx.c\n```\n\n```text\ncc -o xx.exe xx.obj -L/bob/libs;/usr/lib -lbob\n```\n\n```text\nxx.c\n```\n\n```text\nxx.c\n```\n\n```text\nxx.obj\n```\n\n```text\nbob.h\n```\n\n```text\nbob_fn()\n```\n\n```text\n-c\n```\n\n```text\n-o xx.obj\n```\n\n```text\nbob_fn()\n```\n\n```text\n/bob/libs/libbob.so\n```\n\n```text\nxx.exe\n```\n\n```text\nxx.obj\n```\n\n```text\nlibbob.so\n```\n\n```text\n-L\n```\n\n```text\n-l\n```\n\n```text\n-L\n```\n\n```text\nxx.obj\n```\n\n```text\nlibbob.a\n```\n\n```text\nlibbob.dll\n```\n\n```text\nlibbob.so\n```\n\n```text\nbob.a\n```\n\n```text\nbob.dll\n```\n\n```text\nbob.so\n```\n\n========================================\n\nComments:\n- steve-yegge.blogspot.com/2007/06/rich-programmer-food.html I found this somewhere, that somehow explains it to some extent..\n- The directory is not enough - you need to specify the library file as well.\n- ChrisW, do you know how exactly the linker finds the symbols in the other libraries? Does it only search through a specified lib file, or can it be made to scan a directory and try all lib files? Thanks..\n- You give it an explicit list of lib file names (and a list of directories in which those specific lib files may be found): so you need to specify/know the names of the lib files which contains the symbols on which your source depends.\n- Google found for me a sample program which uses Lua: codeproject.com/KB/library/lua.aspx ... according to this sample, the filename of the lua library file is \"lua.lib\".\n- Wow this is good.. I'm accepting this as the answer.. :) BTW, can you explain a bit more about the syntax you used? what does -lbob stand for in the second command?\n- Done, although the commands I mentioned are UNIX-type commands. It's done differently in the MSVC IDE, where you set library paths and library names in the project configuration dialog boxes.\n- No problem pax, you've been quite helpful :) One last thing.. So in an MSVC environment, .lib files are static libraries that must be included in the project, and .dll files are dynamic libraries that are loaded through a LoadLibrary command or similar? Is that the only difference between the two?\n- Aside from that, are dll and lib files essentially the same? (i.e. they contain the code for the functions used in your code)\n- From a conceptual viewpoint, I think you're right, krebstar. They just contain code/data to be added at link time (LIB) and load time (DLL). The internal file formats may be vastly different. I *suspect* DLLs may already be half-way converted into an EXE-type format but I may be wrong.\n- Thanks :) might be useful sometime :)","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":24,"totalLines":209,"estimatedTokens":1474}}607{"id":"stack-2475828","source":"stackoverflow","questionId":2475828,"title":"Haskell compile time function calculation","tags":["compiler-construction","haskell","static","code-generation","metaprogramming"],"text":"Title: Haskell compile time function calculation\nTags: compiler-construction, haskell, static, code-generation, metaprogramming\nSource: Stack Overflow\n\nQuestion:\nI would like to precalculate values for a function at compile-time.\n\nExample (real function is more complex, didn't try compiling):\n\n```\nbase = 10\nmymodulus n = n `mod` base -- or substitute with a function that takes\n -- too much to compute at runtime\nprintmodules 0 = [mymodulus 0]\nprintmodules z = (mymodulus z):(printmodules (z-1))\n\nmain = printmodules 64\n```\n\nI **know** that `mymodulus n` will be called only with `n < 64` and I would like to precalculate `mymodulus` for `n` values of `0..64` at compile time. The reason is that `mymodulus` would be really expensive and will be reused multiple times.\n\n========================================\n\nTop Answer:\nI don't know any way to precompile it down to a table lookup (though you may have some luck with TH). An alternative is to generate an a lookup table at runtime with something like\n\n```\nmymodulus' x = lt ! x\n where lt = array (0, 64) [(i, mymodulus i) | i <- [0..64]]\n```\n\n========================================\n\nCode:\n```text\nbase = 10\nmymodulus n = n `mod` base -- or substitute with a function that takes\n                            -- too much to compute at runtime\nprintmodules 0 = [mymodulus 0]\nprintmodules z = (mymodulus z):(printmodules (z-1))\n\nmain = printmodules 64\n```\n\n```text\nmymodulus n\n```\n\n```text\nn < 64\n```\n\n```text\nmymodulus\n```\n\n```text\nn\n```\n\n```text\n0..64\n```\n\n```text\nmymodulus\n```\n\n```text\n{-# LANGUAGE TemplateHaskell #-}\n\nimport Table\n\nmymodulus n = $(genmodulus 64)\n\nmain = mapM_ (print . mymodulus) [0..64]\n```\n\n```text\n{-# LANGUAGE TemplateHaskell #-}\n\nmodule Table where\n\nimport Language.Haskell.TH\nimport Language.Haskell.TH.Syntax\n\ngenmodulus :: Int -> Q Exp\ngenmodulus n = return $ CaseE (VarE (mkName \"n\"))\n                              [ Match (LitP (IntegerL i))\n                                      (NormalB (LitE (IntegerL (i `mod` base))))\n                                      []\n                              | i <- [0..fromIntegral n] ]\n    where\n        base = 10\n```\n\n```text\ngenmodulus 64\n  ======>\n    case n of {\n      0 -> 0\n      1 -> 1\n      2 -> 2\n      3 -> 3\n      4 -> 4\n      ...\n      64 -> 4 }\n```\n\n```text\nmymodulus' x = lt ! x\n    where lt = array (0, 64) [(i, mymodulus i) | i <- [0..64]]\n```\n\n```text\nprimes = 2 : 3 : filter isPrime [5, 7 .. 1000000]\nisPrime x = walk (tail primes) where\n    walk (y:ys) | (y*y > x) = True\n                | (x `mod` y) /= 0 = walk ys\n    walk _ = False\nmain = do\n    print $ last primes\n    print . last $ init primes\n```\n\n========================================\n\nComments:\n- I think, lookup table would be basically same as just calling the function. Haskell stores called functions and their return values.\n- There is definitely a difference between this and an ordinary function. It is a common misconception that Haskell memoizes all functions. See eg. data-memocombinators on Hackage for ways to tell Haskell to memoize.\n- I've got a table of `[Integer -> Integer]`. Basically that, given a value, generates a new list of values that have been made using that function from that list. I can construct those function lists automatically. Each list in the table can contain any number of functions. Basically based on a `mod` operation it chooses a list to use. But that means I can already construct them at compile time.","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":12,"totalLines":131,"estimatedTokens":863}}608{"id":"stack-693457","source":"stackoverflow","questionId":693457,"title":"How do languages like C# and Java avoid C/C++-like independent compilation?","tags":["c#","java","compiler-construction","programming-languages"],"text":"Title: How do languages like C# and Java avoid C/C++-like independent compilation?\nTags: c#, java, compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nFor my programming languages class, I'm writing a research paper on some papers by some important people in the history of language design. One by CAR Hoare struck me as odd because it speaks against independent compilation techniques used in C and later C++ before C even became popular.\n\nSince this is primarily an optimization to speed up compilation times, what is it about Java and C# that make them able to avoid reliance on independent compilation? Is it a compiler technique or are there elements of the language that facilitate this? And are there any other compiled languages that used these techniques before them?\n\n========================================\n\nTop Answer:\nIMO, one of the biggest factors here is that both java and .NET use intermediate languages; that means that the compiled unit (jar/assembly) contains, as a pre-requisite, a lot of expressive metadata about the types, methods, etc; meaning that it is already laid out conveniently for reference checking. The runtime still checks anyway, in case you are pulling a fast one ;-p\n\nThis isn't very far removed from the MIDL that underpins COM, although there the TLB is often a separate entity.\n\nIf I've misunderstood your meaning, please let me know...\n\n========================================\n\nCode:\n```text\n.jar\n```\n\n```text\n.class\n```\n\n```text\n.dll\n```\n\n```text\n.dll\n```\n\n```text\n.class\n```\n\n========================================\n\nComments:\n- I *believe* you understood me properly. I'm basically meaning the idea in C/C++ that each source file is its own individual compilation unit. This doesn't as much seem to be the case in C# or Java.\n- The design of the C languages was based more upon trying to ensure compilation would be feasible on memory-limited machines, than on trying to make it fast. Today, there are relatively few systems whose entire source code wouldn't fit easily in the RAM of a typical desktop machine, but C was designed in an era when a large machine would have less than 1/16,000th as much RAM as today's everyday workstation. Development environments had to make severe tradeoffs between build speed and the largest-practical build size.","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":46,"estimatedTokens":582}}609{"id":"stack-26471876","source":"stackoverflow","questionId":26471876,"title":"How to tell the precedence of operators in a context free grammar","tags":["compiler-construction","grammar","context-free-grammar"],"text":"Title: How to tell the precedence of operators in a context free grammar\nTags: compiler-construction, grammar, context-free-grammar\nSource: Stack Overflow\n\nQuestion:\nHow can we know which of the following logical operations ( or, and, not ) in the bellow context free grammar have higher precedence? Is there a general approach to this kind of problems?\n\nX → X or Y | Y\n\nY → Y and Z | Z\n\nZ → not Z | (X) | true | false\n\n========================================\n\nCode:\n```text\nexpr -> addExpr;\naddExpr -> multExpr (('+'|'-') multExpr)*;\nmultExpr -> terminalExpr (('*'|'/') terminalExpr)*;\nterminalExpr -> integer | variable | '(' expr ')';\n```\n\n```text\nexpr -> addExpr;\naddExpr -> addExpr '+' multExpr | addExpr '-' multExpr | multExpr;\nmultExpr -> multExpr '*' terminalExpr | multExpr '/' terminalExpr | terminalExpr;\nterminalExpr -> integer | variable | '(' expr ')';\n```\n\n```text\n*\n```\n\n```text\n/\n```\n\n```text\n+\n```\n\n```text\n-\n```\n\n========================================\n\nComments:\n- good example. thanks. so what it means is that the operation with higher precedence should be lower in the parsing tree. maybe u can add that to your answer and id plus one it!\n- @rici huh I don't know what I was thinking :-\\ Thanks for noticing this, I've fixed the answer.\n- Thanks. So if I want to add an exponential(**) operator to the above grammar(which is having the highest precedence) then I need to add this operation in the second last level of your grammar right?\n- @Prasanna Since exponentiation has a higher precedence than multiplication, you need to add a separate level to the grammar (between `multExpr` and `terminalExpr`) in order to handle it correctly.","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":53,"estimatedTokens":416}}610{"id":"stack-33286509","source":"stackoverflow","questionId":33286509,"title":"C++ compiler 'shallow' copies and assignments","tags":["c++","compiler-construction","shallow-copy"],"text":"Title: C++ compiler 'shallow' copies and assignments\nTags: c++, compiler-construction, shallow-copy\nSource: Stack Overflow\n\nQuestion:\nI'm taking a class on object oriented programming using C++.\n\nIn our text it says,\n\nIf we do not declare a copy constructor, the compiler inserts code\nthat implements a shallow copy. If we do not declare an assignment\noperator, the compiler inserts code that implements a shallow\nassignment.\n\nWhat I want to know, is whether this is in fact true, what the alluded to compiler mechanism is actually called, and how it works.\n\nThis is **not** a question about copy constructors, it is about compiler behavior.\n\n**EDIT> More context**\n\n*Copy Constructor* as defined by the text:\n\nThe definition of a copy constructor contains logic that\n\n- performs a shallow copy on all of the non-resource instance variables\n\n- allocates memory for each new resource\n\n- copies data from the source resource(s) to the newly created resource(s)\n\n*Resource* as defined by the text\n\nMemory that an object allocates at run-time represents a resource of that\nobject's class.\n\nThe management of this resource requires additional logic that was unnecessary for simpler classes that do not access resources. This additional logic\nensures proper handling of the resource and is often called deep copying and\nassignment.\n\n========================================\n\nTop Answer:\nYes it's true, and it's indeed called shallow copying. As for how it works, lets say you have a pointer variable, and you assign it to another pointer variable. This only copies the *pointer* and not what it points to, this is a shallow copy. A *deep* copy would have created a new pointer, and copied the actual contents that the first pointer points to.\n\nSomething like this:\n\n```\nint* a = new int[10];\n\n// Shallow copying\nint* b = a; // Only copies the pointer a, not what it points to\n\n// Deep copying\nint* c = new int[10];\nstd::copy(a, a + 10, c); // Copies the contents pointed to by a\n```\n\nThe problem with shallow copying in regards to pointers should be quite obvious: After the initialization of `b` in the above example, you have *two* pointers both pointing to the same memory. If one then does `delete[] a;` then both pointers become invalid. If the two pointers are in different objects of some class, then there is no real connection between the pointers, and the second object won't know if the first object have deleted its memory.\n\n========================================\n\nCode:\n```text\nint\n```\n\n```text\nfloat\n```\n\n```text\nstd::unique_ptr\n```\n\n```text\nint* a = new int[10];\n\n// Shallow copying\nint* b = a;   // Only copies the pointer a, not what it points to\n\n// Deep copying\nint* c = new int[10];\nstd::copy(a, a + 10, c);  // Copies the contents pointed to by a\n```\n\n```text\nb\n```\n\n```text\ndelete[] a;\n```\n\n```text\nclass S {\n  T f;\n};\nS s1, s2;\n```\n\n```text\nclass S {\n    T f;\n  public:\n    S &operator=(const S&s) {\n      this->f = s.f; // and such for every field, whatever T is\n    }\n};\nS s1, s2;\ns1=s2;\n```\n\n```text\ns1=s2;\n```\n\n```text\nclass Student sealed {\nprivate:\n    std::string m_strFirstName;\n    std::string m_strLastName;\n\n    std::vector<unsigned short> m_vClassNumbers;\n    std::vector<std::string> m_vTeachers;\n\n    std::vector<unsigned short> m_vClassGrades;\n\npublic:\n    Student( const std::string& strFirstName, const std::string& strLastName );\n\n    std::string getFirstName() const;\n    std::string getLastName() const;\n\n    void setClassRoster( std::vector<unsigned short>& vClassNumbers );\n    std::vector<unsigned short>& getClassRoster() const;\n\n    void setClassTeachers( std::vector<std::string>& vTeachers );\n    std::vector<std::string>& getClassTeachers() const;\n\n    void setClassGrades( std::vector<unsigned short>& vGrades );\n    std::vector<unsigned short>& getGrades() const;\n\n    // Notice That These Are Both Commented Out So The Compiler Will\n    // Define These By Default. And These Will Make Shallow / Stack Copy\n    // Student( const Student& c ); // Default Defined \n    // Student& operator=( const Student& c ); // Default Defined\n};\n```\n\n```text\nclass Student sealed {\nprivate:\n    std::string m_strFirstName;\n    std::string m_strLastName;\n\n    std::vector<unsigned short> m_vClassNumbers;\n    std::vector<std::string> m_vTeachers;\n\n    std::vector<unsigned short> m_vClassGrades;\n\npublic:\n    Student( const std::string& strFirstName, const std::string& strLastName );\n\n    std::string getFirstName() const;\n    std::string getLastName() const;\n\n    void setClassRoster( std::vector<unsigned short>& vClassNumbers );\n    std::vector<unsigned short>& getClassRoster() const;\n\n    void setClassTeachers( std::vector<std::string>& vTeachers );\n    std::vector<std::string>& getClassTeachers() const;\n\n    void setClassGrades( std::vector<unsigned short>& vGrades );\n    std::vector<unsigned short>& getGrades() const;     \n\nprivate:\n    // These Are Not Commented Out But Are Defined In The Private Section\n    // These Are Not Accessible So The Compiler Will No Define Them\n    Student( const Student& c ); // Not Implemented\n    Student& operator=( const Student& c ); // Not Implemented\n};\n```\n\n```text\nclass Foo {\nprivate:\n    int   m_idx;\n    float m_fValue;\n\npublic:\n    explicit Foo( float fValue );\n\n    // Foo( const Foo& c ); // Default Copy Constructor\n    // Foo& operator=( const Foo& c ); // Default Assignment Operator\n};\n```\n\n```text\nclass Foo {\nprivate:\n    int   m_idx;\n    float m_fValue;\n\npublic:\n    explicit Foo( float fValue );\n\nprivate:\n    Foo( const Foo& c ); // Not Implemented\n    Foo& operator=( const Foo& c ); // Not Implemented\n};\n```\n\n========================================\n\nComments:\n- Without any context, the quote sounds at best confusing.\n- According to \"en.wikipedia.org/wiki/Object_copying\" shallow-copy is properly used (Agreed, it may confuse C++ programmers)\n- The statement is TRUE.\n- @hyde The `std::string` class (as well as other container classes like `std::vector`) have implemented copy-constructor and assignment operators to do deep copying.\n- I understand what you are saying, and its a very clear answer... but I'm still not sure what code the compiler is 'inserting'.\n- @bigcodeszzer Read about the implicitly default-declared constructor , as well as about the implicitly declared copy-constructor and implicitly declared copy-assignment operator.\n- @hyde Copying an array is still shallow. It copies all elements of the array as-is, which means that if the array is an array of pointers it work just like with other pointer, it copies the pointers and not what they point to.\n- @JoachimPileborg After thinking about it, I have to concede the point. The whole concept of shallow versus deep copy only applies to reference types, so talking about string or array members only confuses the issue... Cleaning up comments above now.\n- Plenty of reasons? Which ones? If you find yourself implementing the special member functions, chances are that you made a design mistake somewhere.\n- *then the default copy assignment/ctor will fail* not the default will be explicitly marked as `delete` if the class is not copyable. coliru.stacked-crooked.com/a/40da9a87d79923e9\n- 1. I'd paraphrase the part about pointers: it's not always \"bad news\". It's bad (will cause undefined behaviour and runtime errors) only if the object is responsible for freeing the resource (e.g. calls `delete` in it's destructor). 2. I'd also mention that for types that define custom copy operations (e.g std::vector) it invokes their custom behaviour. For example vector's constructor copies it's contents.\n- While your answer is insightful, I'm still not sure what code the compiler is 'inserting'. I'm not sure what you mean by 'default constructor'\n- So what code does the compiler insert in the case of a default constructor?","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":228,"estimatedTokens":1951}}611{"id":"stack-2828239","source":"stackoverflow","questionId":2828239,"title":"Dynamically generating high performance functions in clojure","tags":["java","compiler-construction","dynamic","code-generation","clojure"],"text":"Title: Dynamically generating high performance functions in clojure\nTags: java, compiler-construction, dynamic, code-generation, clojure\nSource: Stack Overflow\n\nQuestion:\nI'm trying to use Clojure to dynamically generate functions that can be applied to large volumes of data - i.e. a requirement is that the functions be compiled to bytecode in order to execute fast, but their specification is not known until run time.\n\ne.g. suppose I specify functions with a simple DSL like:\n\n```\n(def my-spec [:add [:multiply 2 :param0] 3])\n```\n\nI would like to create a function compile-spec such that:\n\n```\n(compile-spec my-spec)\n```\n\nWould return a compiled function of one parameter x that returns 2x+3.\n\nWhat is the best way to do this in Clojure?\n\n========================================\n\nTop Answer:\nEven if you don't AOT compile your code, as soon as you define a function it gets compiled to bytecode on the fly.\n\n========================================\n\nCode:\n```text\n(def my-spec [:add [:multiply 2 :param0] 3])\n```\n\n```text\n(compile-spec my-spec)\n```\n\n```text\n(defn make-affine-function [a b]\n  (fn [x] (+ (* a x) b)))\n\n((make-affine-function 31 47) 5)\n; => 202\n```\n\n```text\n(defmacro make-primitive-affine-function [t a b]\n  (let [cast #(list (symbol (name t)) %)\n        x (gensym \"x\")]\n    `(fn [~x] (+ (* ~(cast a) ~(cast x)) ~(cast b)))))\n\n((make-primitive-affine-function :int 31 47) 5)\n; => 202\n```\n\n```text\n(require '[clojure.walk :as walk])\n\n(defn compile-spec [spec]\n  (let [params (atom #{})]\n    (walk/prewalk\n     (fn [item]\n       (if (and (symbol? item) (.startsWith (name item) \"param\"))\n         (do (swap! params conj item)\n             item)\n         item))\n     spec)\n    (eval `(fn [~@(sort @params)] ~@spec))))\n\n(def my-spec '[(+ (* 31 param0) 47)])\n\n((compile-spec my-spec) 5)\n; => 202\n```\n\n```text\neval\n```\n\n```text\nfn\n```\n\n```text\nfn*\n```\n\n```text\n:int\n```\n\n```text\n:long\n```\n\n```text\n:float\n```\n\n```text\n:double\n```\n\n```text\n#^Foo bar\n```\n\n```text\n^Foo bar\n```\n\n```text\nwith-meta\n```\n\n```text\n'{:tag Foo}\n```\n\n```text\nlet\n```\n\n```text\n(list 'fn ['x] (a-magic-function-to-generate-some-code some-args ...))\n```\n\n```text\neval\n```\n\n```text\nparamFOO\n```\n\n========================================\n\nComments:\n- This is a great answer: helped me understand what is going on under the hood and solves the problem perfectly. Many thanks Michal!","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":20,"totalLines":141,"estimatedTokens":591}}612{"id":"stack-10371212","source":"stackoverflow","questionId":10371212,"title":"Flex and Bison on Mac OS X Lion with Xcode 4.3.2","tags":["c","compiler-construction","xcode4.3","flex-lexer"],"text":"Title: Flex and Bison on Mac OS X Lion with Xcode 4.3.2\nTags: c, compiler-construction, xcode4.3, flex-lexer\nSource: Stack Overflow\n\nQuestion:\nI want to develop my own C compiler to get deeper into flex and bison for my compiler lessons at University, but I can't find flex and bison, which I've seen described in some other posts. Do I need to install extra packages for Xcode or is there any other way?\n\nI'm running Mac OS X Lion and have Xcode 4.3.2.\n\n========================================\n\nTop Answer:\nInstall macports and do:\n\n```\n$ sudo port install flex bison\n```\n\nHowever you might need to *rig-up* the `/Developer` directory using a symbolic link first:\n\n```\n$ sudo ln -s /Applications/Xcode.app/Contents/Developer /Developer\n```\n\n(There is no longer any need to *rig-up* anything).\n\n========================================\n\nCode:\n```text\n$ sudo port install flex bison\n```\n\n```text\n$ sudo ln -s /Applications/Xcode.app/Contents/Developer /Developer\n```\n\n```text\n/Developer\n```\n\n========================================\n\nComments:\n- Should this be tagged by Adobe Flex? Or is gnu-flex more appropriate?\n- Tagged as gnu-flex now. Thanks for the little hint!\n- The same can be done with homebrew; `brew install flex bison`","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":47,"estimatedTokens":309}}613{"id":"stack-17610193","source":"stackoverflow","questionId":17610193,"title":"Creating a small programming language for beginners","tags":["compiler-construction","programming-languages","language-design","bison","yacc"],"text":"Title: Creating a small programming language for beginners\nTags: compiler-construction, programming-languages, language-design, bison, yacc\nSource: Stack Overflow\n\nQuestion:\nI would like to create my own programming language. Maybe not exactly a programming language from scratch but maybe base it on another language. \n\nI've heard of Yacc. So, I installed Flex and Bison. But I do not understand how to make a compiler with it. I have made the `Hello world` project in it, but how would I make a compiler in it?\n\nAre there any easy ways of creating a small programming language, I have heard of translating a language as in taking, e.g., `Write()` and making the computer understand it as `Print()`.\n\nIs this be possible?.\n\n========================================\n\nTop Answer:\nYou might consider learning to build a compiler from a fabulous 1964 (yes, you read that right) paper *META II: A Syntax-Oriented Compiler Writing Language* on how to build \"meta compilers\".\n\nThis paper contains, in *10 pages*, a compiler writing philosophy, a definition of a virtual compiler instruction set that is easy to implement, a compiler-compiler, and an example compiler built using the compiler-compiler.\n\nI learned initially how to build compilers from this paper back in 1970 odd. It is astonishing how *clever* and conceptually simple it is.\n\nIf there was a paper I'd make every computer science student read, this would be it.\n\nYou can get the paper, see a tutorial and an implementation of MetaII in JavaScript here. The guy behind the tutorial is Dr. James Neighbors, source of the term \"domain analysis\".\n\n========================================\n\nCode:\n```text\nHello world\n```\n\n```text\nWrite()\n```\n\n```text\nPrint()\n```\n\n```text\n#tcl\n```\n\n```text\nirc.freenode.net\n```\n\n```text\ncomp.lang.tcl\n```\n\n```text\nwrite Hello world\nwrite Bye!\n```\n\n```text\npublic class Main\n{\nSystem.out.println(\"Hello world\");\nSystem.out.println(\"Bye!\");\n}\n```\n\n========================================\n\nComments:\n- Why don't you create a [Meta-Circular Evaluator][1]? A meta-circular is an evaluator written in the language that it evaluates. [1]:en.wikipedia.org/wiki/Meta-circular_evaluator\n- Here is comprehensive up to date answer for your question using C#: codeproject.com/Articles/272494/&hellip;\n- Gabe, here is a very good answer for a very similar question.... programmers.stackexchange.com/questions/84278/&hellip;. also check....ericlippert.com i found it very useful...hope it will help you.\n- you may check this vnev.me/implementing-a-programming-language-in-c-part-1\n- Ira, that paper is paywalled. ($15 for a pdf seems a bit steep to me, but I've seen much worse.)\n- I believe there's a copy at the tutorial web site.\n- I believe you believe that but I can't find it. So either me eyes or your belief structure need to be re-examined. (And I know the weakness of my eyes; maybe they just need some help.)\n- And i am just 15 . Its is not that hard .","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":77,"estimatedTokens":735}}614{"id":"stack-13515844","source":"stackoverflow","questionId":13515844,"title":"How can I print to a string in LLVM","tags":["c++","compiler-construction","llvm"],"text":"Title: How can I print to a string in LLVM\nTags: c++, compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nI want to print an instruction in LLVM to a string instead of the screen. I use `I->print( errs() )` to print to the screen. How can I instead put the instruction in a string?\n\n========================================\n\nCode:\n```text\nI->print( errs() )\n```\n\n```text\nstd::string str;\nllvm::raw_string_ostream rso(str);\nI->print(rso);\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.813Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":19,"estimatedTokens":113}}615{"id":"stack-14632870","source":"stackoverflow","questionId":14632870,"title":"Creating a Compiler: Learn OCaml or Stick With Java/C/C++","tags":["compiler-construction","programming-languages","ocaml"],"text":"Title: Creating a Compiler: Learn OCaml or Stick With Java/C/C++\nTags: compiler-construction, programming-languages, ocaml\nSource: Stack Overflow\n\nQuestion:\nI have to create a new language and a compiler for that language for a class I'm taking this semester - this means the deadline is in about 3.5 months. I have read that OCaml is one of the better languages to write a compiler in, but I have no previous experience with it. My question is thus as follows: would it be a better idea for me to try to create the compiler with languages I already know well (Java, C, C++), or devote some time to learning OCaml and use it instead?\n\n========================================\n\nComments:\n- The only thing that I can add to gasche's answer is that OCaml might relatively badly documented but there is no shortage of toy compiler implementations online so you do have enough material to bootstrap yourself with.","metadata":{"transformedAt":"2026-08-18T18:32:17.814Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":11,"estimatedTokens":227}}616{"id":"stack-6431942","source":"stackoverflow","questionId":6431942,"title":"Undefined reference to `yylex' in yyparse() while compiling using g++, bison and flex","tags":["c++","compiler-construction","g++","bison","flex-lexer"],"text":"Title: Undefined reference to `yylex' in yyparse() while compiling using g++, bison and flex\nTags: c++, compiler-construction, g++, bison, flex-lexer\nSource: Stack Overflow\n\nQuestion:\nI have a problem while compiling my code (`undefined reference to 'yylex'`). Let's start off with the snippet and I'll describe the details below:\n\nFlex:\n\n```\n%{\n#include \nextern int yylex();\n%}\n%%\n\"=\" {return EQ;}\n\"!=\" {return NE;}\n\"\" {return GT;}\n\":=\" {return ASSIGN;}\n\";\" {return SEMICOLON;}\n\"IF\" {return IF;}\n\"THEN\"{return THEN;}\n\n\"END\" {return END;}\n[_a-z]+ {yylval.text = strdup(yytext); return IDENTIFIER;}\n[ \\t]+\n[0-9]+ {\n yylval.var = atoi (yytext);\n return NUMBER;\n }\n[-+/^*'%'()] {\n return *yytext;\n }\n\\n return RESULT;\n%%\n```\n\nBison:\n\n```\n%{\n extern \"C\"\n {\n int yyparse();\n int yylex(void);\n void yyerror(char *s){}\n int yywrap(void){return 1;}\n }\n\n #include \n #include \n #include \n #include \n #include \n\n using namespace std;\n\n vector instructions;\n map <> vars;\n%}\n\n%union{\n char* text;\n int var;\n}\n\n%token EQ\n%token NE\n%token ASSIGN\n%token SEMICOLON\n%token IF\n%token THEN\n%token IDENTIFIER\n%token NUMBER\n%token RESULT\n\n%left '+' '-'\n%left '*' '/' '%'\n%right '^'\n\n%%\n\nexp: NUMBER\n| IDENTIFIER\n| IDENTIFIER \"+\" IDENTIFIER\n| IDENTIFIER \"-\" IDENTIFIER\n;\n%%\n\nint main(void){\n yyparse();\n}\n```\n\nAnd here's the bash script I use to compile & run the program:\n\n```\n#!/bin/bash\nclear\nrm launcher lex.yy.cpp *.tab.cpp *.tab.hpp\nbison -d -o list.tab.cpp *.y\nflex -o lex.yy.cpp *.l\ng++ -o launcher *.cpp -lfl\n```\n\nJust for clear visibility, I'm posting here only the most important part of the code, because the rest is not really necessary here. Anyway, if someone would like to see the whole code, it's posted here: http://pastebin.com/1rS2FBJj. It's a bigger chunk of code and takes more place though.\n\nWhen I try to compile all files to *.c files and then use gcc, it is all fine. But when I attempt to switch the compiler to g++ and therefore the files to cpp ones, I keep getting this error:\n\nlist.tab.cpp: In function ‘int yyparse()’:\nlist.tab.cpp:1397: warning: deprecated conversion from string constant to ‘char*’\nlist.tab.cpp:1540: warning: deprecated conversion from string constant to ‘char*’\n/tmp/ccdqpQVx.o: In function `yyparse':\nlist.tab.cpp:(.text+0x252): undefined reference to `yylex'\ncollect2: ld returned 1 exit status\n\nCould anyone give me a hint please, how would be the best to approach it?\n\n========================================\n\nCode:\n```text\n%{\n#include <stdlib.h>\nextern int yylex();\n%}\n%%\n\"=\" {return EQ;}\n\"!=\"    {return NE;}\n\"<\" {return LT;}\n\">\" {return GT;}\n\":=\"    {return ASSIGN;}\n\";\" {return SEMICOLON;}\n\"IF\"    {return IF;}\n\"THEN\"{return THEN;}\n\n\"END\" {return END;}\n[_a-z]+ {yylval.text = strdup(yytext); return IDENTIFIER;}\n[ \\t]+\n[0-9]+          {\n                yylval.var = atoi (yytext);\n                return NUMBER;\n                }\n[-+/^*'%'()]    {\n                return *yytext;\n                }\n\\n              return RESULT;\n%%\n```\n\n```text\n%{\n  extern \"C\"\n  {\n    int yyparse();\n    int yylex(void);\n    void yyerror(char *s){}\n    int yywrap(void){return 1;}\n  }\n\n  #include <iostream>\n  #include <vector>\n  #include <string>\n  #include <stdlib.h>\n  #include <map>\n\n  using namespace std;\n\n  vector <string> instructions;\n  map <> vars;\n%}\n\n%union{\n  char* text;\n  int var;\n}\n\n\n%token EQ\n%token NE\n%token ASSIGN\n%token SEMICOLON\n%token IF\n%token THEN\n%token <text> IDENTIFIER\n%token <var> NUMBER\n%token <var> RESULT\n\n%left '+' '-'\n%left '*' '/' '%'\n%right '^'\n\n%%\n\nexp: NUMBER\n| IDENTIFIER\n| IDENTIFIER \"+\" IDENTIFIER\n| IDENTIFIER \"-\" IDENTIFIER\n;\n%%\n\nint main(void){\n  yyparse();\n}\n```\n\n```text\n#!/bin/bash\nclear\nrm launcher lex.yy.cpp *.tab.cpp *.tab.hpp\nbison  -d -o list.tab.cpp *.y\nflex -o lex.yy.cpp *.l\ng++ -o launcher *.cpp -lfl\n```\n\n```text\nlist.tab.cpp: In function ‘int yyparse()’:\nlist.tab.cpp:1397: warning: deprecated conversion from string constant to ‘char*’\nlist.tab.cpp:1540: warning: deprecated conversion from string constant to ‘char*’\n/tmp/ccdqpQVx.o: In function `yyparse':\nlist.tab.cpp:(.text+0x252): undefined reference to `yylex'\ncollect2: ld returned 1 exit status\n```\n\n```text\nundefined reference to 'yylex'\n```\n\n```text\nextern int yylex();\n```\n\n```text\nextern \"C\" {\n  int yylex();\n}\n```\n\n========================================\n\nComments:\n- Not exactly a fix for the problem, but -fno-const-strings will get rid of the warning.\n- That was this!:) Now it's all fine. Thanks a lot!","metadata":{"transformedAt":"2026-08-18T18:32:17.814Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":7,"totalLines":243,"estimatedTokens":1114}}617{"id":"stack-6870483","source":"stackoverflow","questionId":6870483,"title":"Translating G-Machine source to LLVM IR","tags":["haskell","compiler-construction","translation","llvm"],"text":"Title: Translating G-Machine source to LLVM IR\nTags: haskell, compiler-construction, translation, llvm\nSource: Stack Overflow\n\nQuestion:\nI'm implementing a simple lazy functional language with LLVM as its backend in Haskell. I've read two books written by Simon Peyton Jones (\"The implementation of functional programming languages\", as well as \"Implementing functional languages: the tutorial\") and based on that I managed to implement the G-Machine compiler and interpreter.\n\nI'm now currently stuck on the problem of generating LLVM IR code from G-Machine instructions. The main problem is that G-Machine is a stack machine whereas LLVM IR is a register machine. Thus in order to translate G-Machine into LLVM IR I have to maintain some sort of run-time stack in LLVM IR (please correct me if I'm wrong). I was thinking of allocating subsequent stack nodes on the LLVM stack using its IR instructions, but then I'd have to create that stack in a linked-list manner, where each stack element has a pointer to the previous one and the first has a null pointer. This approach however is not very optimal and in case of \"Push n\" operation from G-Machine it would have a complexity of O(n) instead of preferred O(1). Other idea might be to allocate whole blocks of memory instead of single cells.\n\nMy question is whether you see a better/different way of solving my problem.\n\n========================================\n\nTop Answer:\nthere is simple tutorial \n\nhttp://llvm.org/releases/2.0/docs/Stacker.html\n\nHTH\n\n========================================\n\nComments:\n- Any reasons for not implementing an STG machine instead? Needless to mention that there is already a compiler from STG to LLVM which you can refer to.\n- Well, the G-machine is nice and simple. And a classic.","metadata":{"transformedAt":"2026-08-18T18:32:17.814Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":443}}618{"id":"stack-55813582","source":"stackoverflow","questionId":55813582,"title":"Can a C# statement generate non connected MSIL","tags":["c#","compiler-construction","cil"],"text":"Title: Can a C# statement generate non connected MSIL\nTags: c#, compiler-construction, cil\nSource: Stack Overflow\n\nQuestion:\nThe question is about C# language specification and CIL language specification, as well as Microsoft's and Mono's C# compiler behavior. \n\nI'm building some code analysis tools (no matter what), which operate on CIL. \n\nConsidering a few code samples, I notice that code statements (try/catch, ifelse, ifthen, loops, ...) generate connected blocks of MSIL. \n\nBut I'd like to be sure that I can't write C# code construct which yields non-connected MSIL. More specifically, can I write any C# statement which translates to (something similar to): \n\n```\nIL_0000: \nIL_0001: \nIL_0002: \n\n// hole\n\nIL_001a: \nIL_001b:\n```\n\nI already tried some weird stuff using `goto` and nested loops, but maybe I'm not as mad as some users would be.\n\n========================================\n\nTop Answer:\nIn theory yes (this comes from my experience) . Your analysis tool does not deal with c# directly, but works on IL code only. IL can be produced by anybody, not only by visual studio, but also by other language compilers like visual basic, python. Net... and obfuscators! Obfuscators are the real culprit:while other compilers try to adhere to the specs, obfuscators do their best to exploit the specs and the target runtime.\n\nObfuscated code might violate certain common sense patterns. Consider this case: certain smart obfuscators produce illegal msil, but the jitter digest it because it happens that the invalid portions are in the end not executed.\n\nWhen building an analysis tool, you can't handle these cases unless your target is to build a deobfuscator.\n\n========================================\n\nCode:\n```text\nIL_0000: \nIL_0001: \nIL_0002: \n\n// hole\n\nIL_001a: \nIL_001b:\n```\n\n```text\ngoto\n```\n\n```text\nstatic void M(bool x)\n{\n    if (x)\n        return;\n    else\n        M(x);\n    return;\n}\n```\n\n```text\nIL_0000: nop\n    IL_0001: ldarg.0\n    IL_0002: stloc.0\n    IL_0003: ldloc.0\n    IL_0004: brfalse.s IL_0008\n    IL_0006: br.s IL_0011\n    IL_0008: ldarg.0\n    IL_0009: call void A::M(bool)\n    IL_000e: nop\n    IL_000f: br.s IL_0011\n    IL_0011: ret\n```\n\n```text\nif\n```\n\n```text\n0001\n```\n\n```text\n0009\n```\n\n```text\nif\n```\n\n```text\n0011\n```\n\n```text\nreturn\n```\n\n```text\nnop\n```\n\n```text\nif\n```\n\n========================================\n\nComments:\n- @Hans From a comment on my (now-deleted) answer, he said that the `&#47;&#47; hole` referred to other IL instructions, not related to the C# statement in question. I asked him to edit the question to clarify this.\n- The only thing about IL statements you need to worry about is that each individual statement is emitted correctly and that, when control leaves a method, the stack state is valid. Other than that, you can do whatever you want in terms of ordering instructions. (This implies, that when an instruction pulls something off the stack, it is the expected type.)\n- An obfuscator that produces illegal IL is skating on thin ice; the jitter is permitted to run an IL verifier before verifying the method, and reject the method if it fails verification. **The jitter does so if the method is in a low-trust context because low trust code is required to be verifiable.**\n- Thank you Eric! You pointed out a critical bit: low trust. That explains why an obfuscator we used in the past, worked only on particular cases when all the \"optimizations\" were set.","metadata":{"transformedAt":"2026-08-18T18:32:17.814Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":12,"totalLines":117,"estimatedTokens":857}}619{"id":"stack-19633961","source":"stackoverflow","questionId":19633961,"title":"How to calculate FIRST sets by hand","tags":["parsing","compiler-construction","grammar"],"text":"Title: How to calculate FIRST sets by hand\nTags: parsing, compiler-construction, grammar\nSource: Stack Overflow\n\nQuestion:\nI don't understand one of the examples provided by my tutor.\n\nExample\n\n```\nS ::= aBA | BB | Bc\nA ::= Ad | d\nB ::= ε\n```\n\nWe have\n\n```\nFIRST(B) = FIRST(ε)\n = {ε}\n\nFIRST(A) = FIRST(Ad) ∪ FIRST(d)\n = FIRST(A) ∪ {d}\n = {d}\n\nFIRST(S) = FIRST(aBA) ∪ FIRST(BB) ∪ FIRST(Bc)\n = FIRST(a) ∪ (FIRST(B)\\{ε}) ∪ FIRST(B) ∪ (FIRST(B)\\{ε) ∪ FIRST(c)\n = {a, ε, c}\n```\n\nWhy is there a FIRST(B) in the FIRST(S) calculation? Shouldn't it be\n\n```\n(FIRST(B)\\{ε)?\n```\n\nWhy is A missing from FIRST(S) calculation?\n\n========================================\n\nCode:\n```text\nS ::= aBA | BB | Bc\nA ::= Ad | d\nB ::= ε\n```\n\n```text\nFIRST(B) = FIRST(ε)\n         = {ε}\n\nFIRST(A) = FIRST(Ad) ∪ FIRST(d)\n         = FIRST(A) ∪ {d}\n         = {d}\n\nFIRST(S) = FIRST(aBA) ∪ FIRST(BB) ∪ FIRST(Bc)\n         = FIRST(a) ∪ (FIRST(B)\\{ε}) ∪ FIRST(B) ∪ (FIRST(B)\\{ε) ∪ FIRST(c)\n         = {a, ε, c}\n```\n\n```text\n(FIRST(B)\\{ε)?\n```\n\n```text\nFIRST(Bc)\n-> FIRST(εc)\n=  FIRST(c)\n=  c\n```\n\n```text\nFIRST(BBB)\n= (FIRST(B)\\{ε}) ∪ FIRST(BB)\n= (FIRST(B)\\{ε}) ∪ (FIRST(B)\\{ε}) ∪ FIRST(B)\n```\n\n```text\nS\n-> BB\n-> εε\n-> ε\n```\n\n```text\nFIRST(u)\n```\n\n```text\nu\n```\n\n```text\nu\n```\n\n```text\nFIRST(u)\n```\n\n```text\nu\n```\n\n```text\nFIRST(aBA)\n```\n\n```text\nFIRST(a)\n```\n\n```text\na\n```\n\n```text\nA\n```\n\n```text\nB\n```\n\n```text\na\n```\n\n```text\naBA\n```\n\n```text\na\n```\n\n```text\nFIRST(BB)\n```\n\n```text\nFIRST(Bc)\n```\n\n```text\nB\n```\n\n```text\nFIRST(B)\n```\n\n```text\nFIRST(S)\n```\n\n```text\nFIRST(B)\n```\n\n```text\nε\n```\n\n```text\nFIRST(Xu)\n```\n\n```text\nε\n```\n\n```text\nFIRST(X)\n```\n\n```text\n(FIRST(X)\\{ε}) ∪ FIRST(u)\n```\n\n```text\nFIRST(BB)\n```\n\n```text\n(FIRST(B)\\{ε}) ∪ FIRST(B)\n```\n\n```text\nFIRST(BBB)\n```\n\n```text\nFIRST(B)\n```\n\n```text\nFIRST(A)\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.814Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":35,"totalLines":199,"estimatedTokens":447}}620{"id":"stack-10888156","source":"stackoverflow","questionId":10888156,"title":"How is an empty class and an empty struct compiled?","tags":["c++","visual-c++","gcc","compiler-construction","clang"],"text":"Title: How is an empty class and an empty struct compiled?\nTags: c++, visual-c++, gcc, compiler-construction, clang\nSource: Stack Overflow\n\nQuestion:\nDoes the C++ standard dictate the compilation layout of the class and struct? How are they compiled differently especially if they are empty?\n\n========================================\n\nCode:\n```text\nstruct\n```\n\n```text\nclass\n```\n\n========================================\n\nComments:\n- Absolutelu \"no\" on both counts to the first question. A class has an implicit \"this\" pointer in answer to the second question.\n- @paulsm4: Your answer to the second question is wrong. there is no dofference between a `class` and a `struct` in this regard. To wit, a `struct` can have `virtual` members.\n- Classes and structs are the exact same thing, the only difference is with default access specification (for members and bases).\n- @paulsm4 If there is no code there is nowhere for the implicit 'this' pointer to be generated.\n- Is it specified anywhere how much space is allocated? 1 byte?\n- @Paranaix: As far as I know, is not specified. It could be more than 1 byte to satisfy alignment requirements.\n- The size of reference would be architecture dependant. One address on x86 would be 32 bits.\n- I thought they added char to an empty class and performed alignments depending on the architecture?\n- @kunj2aan: Sounds like a sensible implementation, but the standard does not mandate it.\n- @Paranaix: Take a look at my similar question, stackoverflow.com/questions/8271673/&hellip;\n- @kunj2aan Who is this **they** you refer to?","metadata":{"transformedAt":"2026-08-18T18:32:17.814Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":32,"estimatedTokens":392}}621{"id":"stack-2907797","source":"stackoverflow","questionId":2907797,"title":"Where does the compiler store methods for C++ classes?","tags":["c++","optimization","compiler-construction"],"text":"Title: Where does the compiler store methods for C++ classes?\nTags: c++, optimization, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThis is more a curiosity than anything else...\n\nSuppose I have a C++ class Kitty as follows:\n\n```\nclass Kitty\n{\n void Meow()\n {\n //Do stuff\n }\n}\n```\n\nDoes the compiler place the code for Meow() in every instance of Kitty?\n\nObviously repeating the same code everywhere requires more memory. But on the other hand, branching to a relative location in nearby memory requires fewer assembly instructions than branching to an absolute location in memory on modern processors, so this is potentially faster.\n\nI suppose this is an implementation detail, so different compilers may perform differently.\n\nKeep in mind, I'm not considering static or virtual methods here.\n\n========================================\n\nTop Answer:\nIn the usual implementation, there's only one copy of any given function. The association between the code and the data for a given object instance is established by passing a hidden parameter (referred to a `this` in the function) that's a pointer to the object instance (and its data).\n\nFor virtual functions, things get a bit more convoluted: each class gets a vtable that holds a set of pointers to the virtual functions, and each object gets a pointer to the vtable for its class. The virtual functions are invoked by finding the vtable pointer, looking at the correct offset, and invoking the function pointed to by that pointer.\n\n========================================\n\nCode:\n```text\nclass Kitty\n{\n    void Meow()\n    {\n        //Do stuff\n    }\n}\n```\n\n```text\nthis\n```\n\n```text\nMeow\n```\n\n```text\nKitty\n```\n\n```text\nKitty\n```\n\n```text\nthis\n```\n\n```text\nvirtual\n```\n\n```text\nthis\n```\n\n```text\nvirtual\n```\n\n```text\nstatic\n```\n\n```text\nobj                        class\n+------------+            +----------+\n| ptrToClass |----------->| method1  | ----------> toSomewhere(ptrToObj)\n|------------|            |----------|\n| field1     |            | method2  | ----------> toSomewhereElse(ptrToObj)\n+------------+            +----------+\n```\n\n```text\nMeow\n```\n\n```text\nMeow\n```\n\n```text\nMeow\n```\n\n========================================\n\nComments:\n- Get ready for the onslaught of answers to a commonly-known question (though +1, it *is* a good question :) )..\n- To clarify, I'm not interested in inlining. I'm aware of how that works.\n- If I understand you correctly, under the hood it is doing what Python explicitly does on every member function, requiring the reference to the instance. Yes?\n- Regardless of whetheer you explicitly use the `this` pointer when accessing a member function or member variable, it is always used. In the case of member functions, that means passing `this` to the member function as its first (invisible) parameter. If it's a virtual function, the virtual table complicates things, but essentially, you're always passing the `this` pointer to member functions as their first paramater. That's why certain operator overloads (like `<<`) have to be friend functions rather than member functions.\n- @Mashmagar: yes, the two are fairly similar.\n- Actually, most implementations place a *pointer* to the vtable in the object and not the vtable itself. This pointer is often called the *vptr*.","metadata":{"transformedAt":"2026-08-18T18:32:17.814Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":14,"totalLines":113,"estimatedTokens":821}}622{"id":"stack-23621165","source":"stackoverflow","questionId":23621165,"title":"Responsibilities of the Lexer and the Parser","tags":["parsing","compiler-construction","tokenize","lexical-analysis"],"text":"Title: Responsibilities of the Lexer and the Parser\nTags: parsing, compiler-construction, tokenize, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nI'm currently implementing a lexer for a simple programming language. So far, I can tokenize identifiers, assignment symbols, and integer literals correctly; in general, whitespace is insignificant.\n\nFor the input `foo = 42`, three tokens are recognized:\n\n- `foo` (identifier)\n\n- `=` (symbol)\n\n- `42` (integer literal)\n\nSo far, so good. However, consider the input `foo = 42bar`, which is **invalid** due to the (significant) missing space between `42` and `bar`. My lexer incorrectly recognizes the following tokens:\n\n- `foo` (identifier)\n\n- `=` (symbol)\n\n- `42` (integer literal)\n\n- `bar` (identifier)\n\nOnce the lexer sees the digit `4`, it keeps reading until it encounters a non-digit. It therefore consumes the `2` and stores `42` as an integer literal token. Because whitespace is insignificant, the lexer discards any whitespace (if there is any) and starts reading the next token: It finds the identifier `bar`.\n\n**Now, here's my question:** Is it still the lexer's responsibility to recognize that an identifier is not allowed at that position? Or does that check belong to the responsibilities of the parser?\n\n========================================\n\nTop Answer:\nI disagree with other answers here. It should be done by the lexer. If the character following the digits isn't whitespace or a special character, you're in the middle of an illegal token, specifically an identifier that doesn't start with a letter.\n\nOr else just return the 45 and the 'bar' separately and let the parser handle it as a syntax error.\n\n========================================\n\nCode:\n```text\nfoo = 42\n```\n\n```text\nfoo\n```\n\n```text\n=\n```\n\n```text\n42\n```\n\n```text\nfoo = 42bar\n```\n\n```text\n42\n```\n\n```text\nbar\n```\n\n```text\nfoo\n```\n\n```text\n=\n```\n\n```text\n42\n```\n\n```text\nbar\n```\n\n```text\n4\n```\n\n```text\n2\n```\n\n```text\n42\n```\n\n```text\nbar\n```\n\n```text\n$ python -c 'print 42and False'\nFalse\n\n$ lua -e 'print(42and false)'\nlua: (command line):1: malformed number near '42a'\n\n$ perl -le 'print 42and 0'\n42\n\n# Not an idiosyncracy of tcc; it's defined by the standard\n$ tcc -D\"and=&&\" -run - <<<\"main(){return 42and 0;}\"\nstdin:1: error: invalid number\n\n# gcc has better error messages\n$ gcc -D\"and=&&\" -x c - <<<\"main(){return 42and 0;}\" && ./a.out\n<stdin>: In function ‘main’:\n<stdin>:1:15: error: invalid suffix \"and\" on integer constant\n<stdin>:1:21: error: expected ‘;’ before numeric constant\n\n$ ruby -le 'print 42and 1'\n42\n\n# And now for something completely different (explained below)\n$ awk 'BEGIN{print 42foo + 3}'\n423\n```\n\n```text\n$ gcc -D\"c_(x,y)=x##y\" -D\"c(x,y)=c_(x,y)\"  -x c - <<<\"int main(){return c(12E,1F);}\"\n$ ./a.out; echo $?\n120\n```\n\n```text\n$ awk 'BEGIN{print 42foo + 3}'\n423\n```\n\n```text\n42foo\n```\n\n```text\n42and\n```\n\n```text\n42 + 1\n```\n\n```text\n42+1\n```\n\n```text\n42+ 1\n```\n\n```text\n42and\n```\n\n```text\n12E\n```\n\n```text\n1F\n```\n\n```text\n##\n```\n\n```text\n##\n```\n\n```text\n12E\n```\n\n```text\n1F\n```\n\n```text\nc(12E+,1F)\n```\n\n```text\nc(12E0,1F)\n```\n\n```text\n+\n```\n\n```text\n0x1E+2\n```\n\n```text\nBEGIN{print 42 foo + 3}\n```\n\n```text\nBEGIN{print (42)(foo + 3);}\n```\n\n```text\n0\n```\n\n```text\n\"\"\n```\n\n```text\nfoo = 42bar\n```\n\n```text\nfoo = 42bar\n```\n\n```text\nid = <number>\n```\n\n```text\nid = <number> + <number> - <number>\n```\n\n========================================\n\nComments:\n- Alright, that sounds reasonable. My description might have been misleading: The statement is invalid in the language, but my lexer happily recognized the tokens anyway. I just wanted to make sure that it's okay for the lexer to **not** flag invalid tokens at that point.\n- It is. They are not invalid *as tokens* - it is the syntax that makes this particular combination of tokens invalid - hence the parser flagging (when you have a traditional lexer/parser architecture).\n- Unless `42bar` is a legal token in the language, e.g. an identifier, it should fail at the lexer stage.\n- I'm building a handwritten lexer for educational purposes, but when I implement the parser I might choose to go with a framework; I'm not sure yet, it's mostly about learning. Your answer clarified a few things for me, +1!\n- I just realized I made a mistake, and rewrote some of this. Essentially what your lexer will look like is a list of regular expressions, with an associated number for the token type. For each group of characters between whitespace, you try each regex in turn and when one matches, you return the character range tagged with the corresponding token type. If none match, it fails.\n- That’s not how hand lexers are written. They switch on the next character and have little loops in each case.\n- This is the concern I was having, too. I'm by no means an expert in lexer/parser generation, but it didn't really feel right for the lexer to accept this.\n- I actually misread the description, despite the bold text on \"invalid.\" You're right, it would fail before it got to the parser.\n- +1, very detailed answer. I also like the real-world-examples.","metadata":{"transformedAt":"2026-08-18T18:32:17.814Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":42,"totalLines":246,"estimatedTokens":1267}}623{"id":"stack-33005823","source":"stackoverflow","questionId":33005823,"title":"How does the compiler know which entry in vtable corresponds to a virtual function?","tags":["c++","compiler-construction","virtual-functions","vtable"],"text":"Title: How does the compiler know which entry in vtable corresponds to a virtual function?\nTags: c++, compiler-construction, virtual-functions, vtable\nSource: Stack Overflow\n\nQuestion:\nLet's say we have more than one virtual function in the parent class and derived class. There will be a vtable created for these virtual functions in the vtable for both the parent derived class.\n\nHow will the compiler know which entry in the vtable correspond to which virtual function?\n\nExample:\n\n```\nclass Animal{\npublic:\n void fakeMethod1(){}\n virtual void getWeight(){}\n void fakeMethod2(){}\n virtual void getHeight(){}\n virtual void getType(){}\n};\n\nclass Tiger:public Animal{\npublic:\n void fakeMethod3(){}\n virtual void getWeight(){}\n void fakeMethod4(){}\n virtual void getHeight(){}\n virtual void getType(){}\n};\nmain(){\nAnimal a* = new Tiger();\na->getHeight(); // A will now point to the base address of vtable Tiger\n//How will the compiler know which entry in the vtable corresponds to the function getHeight()?\n}\n```\n\nI have not found exact explanation in my research -\n\nhttps://stackoverflow.com/a/99341/437894 = \n\n \"This table is used to resolve the function call as it contains the\n addresses of all the virtual functions of that class.\"\n\nHow exactly is the table used to resolve the function call?\n\nhttps://stackoverflow.com/a/203136/437894 =\n\n \"So at runtime, the code just uses the object's vptr to locate the\n vtbl, and from there the address of the actual overridden function.\"\n\nI am not able to understand this. Vtable holds the address of the virtual function not the address of actual overridden function.\n\n========================================\n\nTop Answer:\nIt can help to implement something similar yourself.\n\n```\nstruct Bob;\nstruct Bob_vtable {\n void(*print)(Bob const*self) = 0;\n Bob_vtable(void(*p)(Bob const*)):print(p){}\n};\ntemplate\nBob_vtable const* make_bob_vtable(void(*print)(Bob const*)) {\n static Bob_vtable const table(+print);\n return &table;\n}\nstruct Bob {\n Bob_vtable const* vtable;\n void print() const {\n vtable->print(this);\n }\n Bob():vtable( make_bob_vtable([](Bob const*self){\n std::cout ([](Bob const*self){\n std::cout (self)->x live example.\n\nHere we have an explicit vtable stored in `Bob`. It points to a table of functions. The non-virtual member function `print` uses it to dynamically dispatch to the correct method.\n\nThe constructor of `Bob` and derived class `Alice` set the vtable to a different value (created as a static local in this case) with different values in the table.\n\nWhich pointer to use is baked into the definition of what `Bob::print` means -- it knows the offset into the table.\n\nIf we add another virtual function in Alice, it just means that the vtable pointer will point to a `struct Alice_vtable:Bob_vtable` in actuality. Static/reinterpret casting will get us the \"real\" table out, and we can access the extra function pointers easily.\n\nThings get stranger when we talk about virtual *inheritance* as well as virtual functions. I am not qualified to describe how that works.\n\n========================================\n\nCode:\n```text\nclass Animal{\npublic:\n void fakeMethod1(){}\n virtual void getWeight(){}\n void fakeMethod2(){}\n virtual void getHeight(){}\n virtual void getType(){}\n};\n\nclass Tiger:public Animal{\npublic:\n void fakeMethod3(){}\n virtual void getWeight(){}\n void fakeMethod4(){}\n virtual void getHeight(){}\n virtual void getType(){}\n};\nmain(){\nAnimal a* = new Tiger();\na->getHeight(); // A  will now point to the base address of vtable Tiger\n//How will the compiler know which entry in the vtable corresponds to the function getHeight()?\n}\n```\n\n```c++\n#include <iostream>\n\nstruct Animal\n{\n  int age;\n  Animal(int a) : age {a} {}\n  virtual int setAge(int);\n  virtual void sayHello() const;\n};\n\nint\nAnimal::setAge(int a)\n{\n  int prev = this->age;\n  this->age = a;\n  return prev;\n}\n\nvoid\nAnimal::sayHello() const\n{\n  std::cout << \"Hello, I'm an \" << this->age << \" year old animal.\\n\";\n}\n\nstruct Tiger : Animal\n{\n  int stripes;\n  Tiger(int a, int s) : Animal {a}, stripes {s} {}\n  virtual void sayHello() const override;\n  virtual void doTigerishThing();\n};\n\nvoid\nTiger::sayHello() const\n{\n  std::cout << \"Hello, I'm a \" << this->age << \" year old tiger with \"\n            << this->stripes << \" stripes.\\n\";\n}\n\nvoid\nTiger::doTigerishThing()\n{\n  this->stripes += 1;\n}\n\n\nint\nmain()\n{\n  Tiger * tp = new Tiger {7, 42};\n  Animal * ap = tp;\n  tp->sayHello();         // call overridden function via derived pointer\n  tp->doTigerishThing();  // call child function via derived pointer\n  tp->setAge(8);          // call parent function via derived pointer\n  ap->sayHello();         // call overridden function via base pointer\n}\n```\n\n```c\nstruct animal\n{\n  const void * vptr;\n  int age;\n};\n```\n\n```c\nstatic int\nanimal_set_age(void * p, int a)\n{\n  struct animal * this = (struct animal *) p;\n  int prev = this->age;\n  this->age = a;\n  return prev;\n}\n```\n\n```c\nstatic void\nanimal_say_hello(const void * p)\n{\n  const struct animal * this = (const struct animal *) p;\n  printf(\"Hello, I'm an %d year old animal.\\n\", this->age);\n}\n```\n\n```c\nstruct animal_vtable_type\n{\n  int (*setAge)(void *, int);\n  void (*sayHello)(const void *);\n};\n```\n\n```c\nstatic const struct animal_vtable_type animal_vtable = {\n  .setAge = animal_set_age,\n  .sayHello = animal_say_hello,\n};\n```\n\n```c\nvoid\nanimal_ctor(void * p, int age)\n{\n  struct animal * this = (struct animal *) p;\n  this->vptr = &animal_vtable;\n  this->age = age;\n}\n```\n\n```c\nvoid *\nanimal_new(int age)\n{\n  void * p = malloc(sizeof(struct animal));\n  if (p != NULL)\n    animal_ctor(p, age);\n  return p;\n}\n```\n\n```c\nstruct tiger\n{\n  struct animal base;\n  int stripes;\n};\n```\n\n```c\nvoid\ntiger_say_hello(const void * p)\n{\n  const struct tiger * this = (const struct tiger *) p;\n  printf(\"Hello, I'm an %d year old tiger with %d stripes.\\n\",\n         this->base.age, this->stripes);\n}\n\nvoid\ntiger_do_tigerish_thing(void * p)\n{\n  struct tiger * this = (struct tiger *) p;\n  this->stripes += 1;\n}\n```\n\n```c\nstruct tiger_vtable_type\n{\n  int (*setAge)(void *, int);\n  void (*sayHello)(const void *);\n  void (*doTigerishThing)(void *);\n};\n```\n\n```c\nstatic const struct tiger_vtable_type tiger_vtable = {\n  .setAge = animal_set_age,\n  .sayHello = tiger_say_hello,\n  .doTigerishThing = tiger_do_tigerish_thing,\n};\n```\n\n```c\nvoid\ntiger_ctor(void * p, int age, int stripes)\n{\n  struct tiger * this = (struct tiger *) p;\n  animal_ctor(this, age);\n  this->base.vptr = &tiger_vtable;\n  this->stripes = stripes;\n}\n```\n\n```c\nvoid *\ntiger_new(int age, int stripes)\n{\n  void * p = malloc(sizeof(struct tiger));\n  if (p != NULL)\n    tiger_ctor(p, age, stripes);\n  return p;\n}\n```\n\n```c\n#define INVOKE_VIRTUAL_ARGS(STYPE, THIS, FUNC, ...)                     \\\n  (*((const struct STYPE ## _vtable_type * *) (THIS)))->FUNC( THIS, __VA_ARGS__ )\n```\n\n```c\n#define INVOKE_VIRTUAL(STYPE, THIS, FUNC)                               \\\n  (*((const struct STYPE ## _vtable_type * *) (THIS)))->FUNC( THIS )\n```\n\n```c\n#include <stdio.h>\n#include <stdlib.h>\n\n/* Insert all the code from above here... */\n\nint\nmain()\n{\n  struct tiger * tp = tiger_new(7, 42);\n  struct animal * ap = (struct animal *) tp;\n  INVOKE_VIRTUAL(tiger, tp, sayHello);\n  INVOKE_VIRTUAL(tiger, tp, doTigerishThing);\n  INVOKE_VIRTUAL_ARGS(tiger, tp, setAge, 8);\n  INVOKE_VIRTUAL(animal, ap, sayHello);\n  return 0;\n}\n```\n\n```c\nINVOKE_VIRTUAL_ARGS(tiger, tp, setAge, 8);\n```\n\n```text\nvirtual\n```\n\n```text\nvirtual\n```\n\n```text\nvirtual\n```\n\n```text\nstruct animal\n```\n\n```text\nage\n```\n\n```text\nvptr\n```\n\n```text\nvoid\n```\n\n```text\nvoid *\n```\n\n```text\nthis\n```\n\n```text\nvoid *\n```\n\n```text\nthis\n```\n\n```text\nsayHello\n```\n\n```text\nthis\n```\n\n```text\nconst\n```\n\n```text\nAnimal\n```\n\n```text\nvirtual\n```\n\n```text\nNULL\n```\n\n```text\nanimal_set_age\n```\n\n```text\nanimal_say_hello\n```\n\n```text\nstatic\n```\n\n```text\nvptr\n```\n\n```text\nstatic\n```\n\n```text\nAnimal\n```\n\n```text\noperator new\n```\n\n```text\nvptr\n```\n\n```text\nTiger\n```\n\n```text\nAnimal\n```\n\n```text\nstruct tiger\n```\n\n```text\nstruct animal\n```\n\n```text\nvptr\n```\n\n```text\nstruct animal\n```\n\n```text\nstruct tiger\n```\n\n```text\nthis\n```\n\n```text\nstruct tiger\n```\n\n```text\nthis\n```\n\n```text\nanimal_vtable_type\n```\n\n```text\nstruct animal_vtable_type\n```\n\n```text\nvptr\n```\n\n```text\n&animal_vtable\n```\n\n```text\nvirtual\n```\n\n```text\nvptr\n```\n\n```text\noperator new\n```\n\n```text\nSTYPE\n```\n\n```text\nthis\n```\n\n```text\nTHIS\n```\n\n```text\nFUNC\n```\n\n```text\n##\n```\n\n```text\nSTYPE\n```\n\n```text\nanimal\n```\n\n```text\nSTYPE ## _vtable_type\n```\n\n```text\nanimal_vtable_type\n```\n\n```text\nTHIS\n```\n\n```text\nvptr\n```\n\n```text\nvptr\n```\n\n```text\nFUNC\n```\n\n```text\n__VA_ARGS__\n```\n\n```text\nTHIS\n```\n\n```text\nsetAge\n```\n\n```text\nAnimal\n```\n\n```text\nTiger\n```\n\n```text\nstruct tiger\n```\n\n```text\nvoid\n```\n\n```text\nthis\n```\n\n```text\nanimal_set_age\n```\n\n```text\nstruct animal\n```\n\n```text\nstruct animal\n```\n\n```text\nstruct tiger\n```\n\n```text\nstruct tiger\n```\n\n```text\nstruct animal\n```\n\n```text\nvptr\n```\n\n```text\nstruct Bob;\nstruct Bob_vtable {\n  void(*print)(Bob const*self) = 0;\n  Bob_vtable(void(*p)(Bob const*)):print(p){}\n};\ntemplate<class T>\nBob_vtable const* make_bob_vtable(void(*print)(Bob const*)) {\n  static Bob_vtable const table(+print);\n  return &table;\n}\nstruct Bob {\n  Bob_vtable const* vtable;\n  void print() const {\n    vtable->print(this);\n  }\n  Bob():vtable( make_bob_vtable<Bob>([](Bob const*self){\n    std::cout << \"Bob\\n\";\n  })) {}\nprotected:\n  Bob(Bob_vtable const* t):vtable(t){}\n};\nstruct Alice:Bob {\n  int x = 0;\n  Alice():Bob( make_bob_vtable<Alice>([](Bob const*self){\n    std::cout << \"Alice \" << static_cast<Alice const*>(self)->x << '\\n';\n  })) {}\n};\n```\n\n```text\nBob\n```\n\n```text\nprint\n```\n\n```text\nBob\n```\n\n```text\nAlice\n```\n\n```text\nBob::print\n```\n\n```text\nstruct Alice_vtable:Bob_vtable\n```\n\n========================================\n\nComments:\n- There's nothing that prescribes the vtbl layout. But a natural way is for the compiler to number the virtual functions in the class in consecutive order. These numbers serve as indices into the vtbl, which is effectively an array of function pointers.\n- The compiler knows what's in the vtable because it created the vtable. Unclear what you're really asking here.\n- @Gene annoyingly, MSVC also groups overloads together even if they aren't declared in that order. (Oh, and naturally things get strange with virtual inheritance etc)\n- @EJP I am not able to understand how the compiler maps the virtual functions in the class to the entries in vtable. Hope this makes my question more clear.\n- But it doesn't matter, does it? It puts entries into the vtable, and it generates code to read them out again. What mapping it uses is irrelevant. Stroustrup used the order of declaration, but it could be anything as long as it was consistent.\n- NB \"VTable holds the address of the virtual function not the address of actual overridden function\" isn't correct. It holds the address of the override for the current class. Otherwise it would be pointless.\n- Thanks for the detailed explanation. This helped. Also found another example. Here they talk about an example with two base classes and the derived classes over riding a virtual function from both the base classes. link\n- For your next class, please cover `virtual` inheritance and how to emulate it in C. :) +1 As an aside, an advantage of this system is you can divorce the vtable from the data (don't store it contiguously), which can permit some tricks. These techniques are useful in C++ when you need to do that splitting (say, if you want to store your data in an internal buffer somewhere, but act on it polymorphically: or, tiny lightweight objects packed in one array, with run-length-encoded vtables elsewhere, which you might use in text processing)\n- Thank you very much for the answer. A question: How does the compiler know in the first place that it has to place a virtual function call? If I had: X* z = new z(); Such that X has no virtual functions, Y inherits from X and has a virtual function, and Z inherits from Y. All function calls from \"z\" above should be through a vptr, but the type is X*, so how does the compiler know if calls to functions on \"z\" should pass through a vptr or not?\n- @DeanLeitersdorf In your example, the static type of the variable will be `X*`. And since (you said that) `X` has no virtual functions, there is none you could possibly call. Where this becomes a problem is when (in C++) you use `delete` to free the object again. Indeed, the compiler has no way to know at this point that it should go through a vtable in order to find the destructor of the dynamic type. What will happen in C++ is that the destructor of the static type (`X`) will be called and your program will invoke undefined behavior. tl;dr Don't do it.","metadata":{"transformedAt":"2026-08-18T18:32:17.814Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":96,"totalLines":682,"estimatedTokens":3187}}624{"id":"stack-14155327","source":"stackoverflow","questionId":14155327,"title":"Better way for parser combinators in C?","tags":["c","parsing","compiler-construction","parser-combinators"],"text":"Title: Better way for parser combinators in C?\nTags: c, parsing, compiler-construction, parser-combinators\nSource: Stack Overflow\n\nQuestion:\nI'm trying to bootstrap (a subset of) C from scratch, without using extra dependencies (parser generators, libraries, etc.). Also I want to make use of the idea of **parser combinators**, which is a fantastic technique in functional programming. I would like to borrow this idea from the functional world to procedural C, in a concise and practical way.\n\nI tried to implement some necessary parser combinators for the following toy grammar, which is also an example from the book, *Implementing Functional Languages - a tutorial*, of Simon Peyton Jones.\n\n```\ngreeting -> hg person \"!\"\nhg -> \"hello\"\n | \"goodbye\"\n```\n\nwhere `person` is any token beginning with a letter. For example, the token list\n\n```\n[\"goodbye\", \"James\", \"!\"]\n```\n\nis parsed into\n\n```\n[((\"goodbye\", \"James\"), [\"!\"])]\n```\n\n(The book uses Haskell, and it's hard to make it language-agnostic, but you get the idea :-)\n\nI implemented this in C, and you can view the code here: https://gist.github.com/4451478\n\nThis implementation costs 200+ lines of C code, which is far more than the ~20 lines of Haskell as written in the book. So I'm not sure whether I'm on the right track of doing parser combinators in C, and if there's any possible improvements. Any suggestions are welcomed. Thanks in advance.\n\n========================================\n\nTop Answer:\nI'm looking into the subject myself and I'm following the work of Daniel Holden, author of **mpc** , a very well written **parser combinator** library for C, which allows, among other things, to embed **EBNF** and **Regex** inside C code:\n\n```\nmpc_parser_t *Expr = mpc_new(\"expression\");\n mpc_parser_t *Prod = mpc_new(\"product\");\n mpc_parser_t *Value = mpc_new(\"value\");\n mpc_parser_t *Maths = mpc_new(\"maths\");\n\n mpca_lang(MPCA_LANG_PREDICTIVE,\n \" expression : (('+' | '-') )*; \"\n \" product : (('*' | '/') )*; \"\n \" value : /[0-9]+/ | '(' ')'; \"\n \" maths : /^/ /$/; \"\n Expr, Prod, Value, Maths, NULL);\n```\n\nDaniel Holden, also, has written an online book where he demonstrated how it's easy to write a new language using his library. The book is entitled \"Build your own Lisp\". I think you will find this really useful for your project. Last but not least, in the examples of the library, there is a ready-made program which generate a parser for a subset of C. **;-)**\n\n========================================\n\nCode:\n```text\ngreeting -> hg person \"!\"\nhg       -> \"hello\"\n          | \"goodbye\"\n```\n\n```text\n[\"goodbye\", \"James\", \"!\"]\n```\n\n```text\n[((\"goodbye\", \"James\"), [\"!\"])]\n```\n\n```text\nperson\n```\n\n```text\nmpc_parser_t *Expr  = mpc_new(\"expression\");\n  mpc_parser_t *Prod  = mpc_new(\"product\");\n  mpc_parser_t *Value = mpc_new(\"value\");\n  mpc_parser_t *Maths = mpc_new(\"maths\");\n\n  mpca_lang(MPCA_LANG_PREDICTIVE,\n    \" expression : <product> (('+' | '-') <product>)*; \"\n    \" product : <value>   (('*' | '/')   <value>)*;    \"\n    \" value : /[0-9]+/ | '(' <expression> ')';         \"\n    \" maths : /^/ <expression> /$/;                    \"\n    Expr, Prod, Value, Maths, NULL);\n```\n\n```text\nparser_dp_return test_parser7_rest_dp(dynamic_parser_closure* ctx, input_t input) {\n  parser_dp_return dp_ret;\n  dp_ret.obj = ctx->objs[1]->obj;\n  dp_ret.status = PARSER_NORMAL;\n  dp_ret.i = input;\n  dp_ret.discard_obj_callback = NULL;\n  return dp_ret;\n}\n\nparser_dp_return test_parser7_full_dp(dynamic_parser_closure* ctx, input_t input) {\n  parser_dp_return dp_ret;\n  list* result = list_new();\n  list_push_back(result, ctx->objs[0]->obj);//num\n  if(ctx->objs[1] && ctx->objs[1]->obj) {\n    list_append(result, ctx->objs[1]->obj);\n  }\n  dp_ret.status = PARSER_NORMAL;\n  dp_ret.i = input;\n  dp_ret.discard_obj_callback = (void (*)(void *))&list_delete;\n\n  dp_ret.obj = result;\n  return dp_ret;\n}\n\nbool test_parser7() {//comma separated values\n  parser* number = num();\n  parser* comma = symbol(',');\n  parser* rest_parser = parser_chain_final(test_parser7_rest_dp);\n  list* parser_chain_list = list_new();\n  list_push_back(parser_chain_list, comma);//ctx 0\n  list_push_back(parser_chain_list, number);//ctx 1\n  list_push_back(parser_chain_list, rest_parser);\n\n  parser* rest = parser_chain(parser_chain_list);\n  list_delete(parser_chain_list);\n  parser* many_rest = many(rest);\n\n  list* parser_chain_full = list_new();\n  list_push_back(parser_chain_full, number);//ctx 0\n  list_push_back(parser_chain_full, many_rest);//ctx 1\n  parser* full_parser = parser_chain_final(test_parser7_full_dp);\n  list_push_back(parser_chain_full, full_parser);\n  parser* final = parser_chain(parser_chain_full);\n\n  const char* input = \"1,20,300,4000,50000\";\n  input_t i;\n  input_init(&i, input);\n  parser_dp_return dp_ret = parse(final, i);\n  parser_delete(number);\n  parser_delete(comma);\n  parser_delete(rest_parser);\n  parser_delete(rest);\n  parser_delete(many_rest);\n  parser_delete(full_parser);\n  parser_delete(final);\n  bool result = true;\n  test_true(&result, dp_ret.status == PARSER_NORMAL);\n  list* l = dp_ret.obj;\n  list_item* li = l->head;\n  test_true(&result, ptr_to_int(li->item) == 1);\n  li = li->next;\n  test_true(&result, ptr_to_int(li->item) == 20);\n  li = li->next;\n  test_true(&result, ptr_to_int(li->item) == 300);\n  li = li->next;\n  test_true(&result, ptr_to_int(li->item) == 4000);\n  li = li->next;\n  test_true(&result, ptr_to_int(li->item) == 50000);\n  return result;\n}\n```\n\n========================================\n\nComments:\n- You could start using some library that provides higher-level data structures. I favor glib.\n- @unwind I've updated my question with more details. I'm aware of the data structures, but that's apparently not what I'm asking for in this question. The keyword is **parser combinators** here.\n- I realize that, which is why I only commented instead of posting an answer. You did spend some words in the question pointing out the number of lines of code, which made it sound like something you consider to be a problem, too.\n- I you are aware of the limitations of the core of C and don't want to hear about implementations of lists etc, then what is your *question*. As your \"question\" stands, it looks more like \"I am not happy with C\". This is not a question.\n- @unwind Thanks anyway, since glib is really good :-)\n- @AmigableClarkKant Sorry, I was in a hurry writing this question. I have cleaned it up and hope it can be understood better now.\n- I've been using these tools for years. What I'm asking for is whether I can implement **parser combinators**, which is an idea from **functional programming**, better in C, instead of using those old tools.\n- I've updated my question with more details. I'm aware of the data structures, but that's apparently not what I'm asking for in this question. The keyword is parser combinators here.\n- @XiaoJia, I updated my answer, but not sure a library is what you want?\n- I've done something similar with: github.com/leblancmeneses/NPEG/tree/master/Languages/npeg_c","metadata":{"transformedAt":"2026-08-18T18:32:17.814Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":175,"estimatedTokens":1751}}625{"id":"stack-8981915","source":"stackoverflow","questionId":8981915,"title":"Constants and compile time evaluation - Why change this behaviour","tags":["c#","compiler-construction","compiler-optimization","inference"],"text":"Title: Constants and compile time evaluation - Why change this behaviour\nTags: c#, compiler-construction, compiler-optimization, inference\nSource: Stack Overflow\n\nQuestion:\nIf you forward to approximately 13 minutes into this video by Eric Lippert he describes a change that was made to the C# compiler that renders the following code invalid (Apparently prior to and including .NET 2 this code would have compiled).\n\n```\nint y;\nint x = 10;\nif (x * 0 == 0)\n y = 123;\n\nConsole.Write(y);\n```\n\nNow I understand that clearly any execution of the above code actually evaluates to \n\n```\nint y;\nint x = 10;\ny = 123;\nConsole.Write(y);\n```\n\nBut what I dont understand is why it is considered \"desirable\" to make the following code in-compilable? IE: What are the risks with allowing such inferences to run their course?\n\n========================================\n\nTop Answer:\nThe specification states that the definite assignment of something that is only assigned inside an `if` block is undetermined. The spec says nothing about compiler magic that removes the unnecessary `if` block. In particular, it makes for a very confusing error message as you change the `if` condition, and suddenly get an error about `y` not being assigned \"huh? I haven't changed when y is assigned!\".\n\nThe compiler is free to perform any obvious code removal it wants to, but **first** it needs to the specification for the rules.\n\nSpecifically, section 5.3.3.5 (MS 4.0 spec):\n\n 5.3.3.5 If statements\n For an if statement stmt of the form:\n\n \n `if (` *expr* `)` *then-stmt* `else` *else-stmt*\n\n \n \n \n- v has the same definite assignment state at the beginning of expr as at the beginning of stmt.\n \n- If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to then-stmt and to either else-stmt or to the end-point of stmt if there is no else clause.\n \n- If v has the state “definitely assigned after true expression” at the end of expr, then it is definitely assigned on the control flow transfer to then-stmt, and not definitely assigned on the control flow transfer to either else-stmt or to the end-point of stmt if there is no else clause.\n \n- If v has the state “definitely assigned after false expression” at the end of expr, then it is definitely assigned on the control flow transfer to else-stmt, and not definitely assigned on the control flow transfer to then-stmt. It is definitely assigned at the end-point of stmt if and only if it is definitely assigned at the end-point of then-stmt.\n \n- Otherwise, v is considered not definitely assigned on the control flow transfer to either the then-stmt or else-stmt, or to the end-point of stmt if there is no else\n \n \n For an initially unassigned variable to be considered definitely assigned at a certain location, an assignment to the variable must occur in every possible execution path leading to that location.\n\n*technically*, the execution path exists where the `if` condition is false; if `y` was also assigned in the `else`, then fine, but... the specification explicitly makes no demand of spotting the `if` condition is always true.\n\n========================================\n\nCode:\n```text\nint y;\nint x = 10;\nif (x * 0 == 0)\n    y = 123;\n\nConsole.Write(y);\n```\n\n```text\nint y;\nint x = 10;\ny = 123;\nConsole.Write(y);\n```\n\n```text\nint x = 123;\nint y;\nif (x * 0 == 0) \n    y = 345;\nConsole.WriteLine(y);\n```\n\n```text\nint x = 123;\nint y;\nif (true) \n    y = 345;\nConsole.WriteLine(y);\n```\n\n```text\nint x = 123;\nint y;\ny = 345;\nConsole.WriteLine(y);\n```\n\n```text\n(int x)=>x * 0 == 0\n```\n\n```text\n(int x)=>true\n```\n\n```text\n()=>2 + 3\n```\n\n```text\nx * 0 == 0\n```\n\n```text\nx\n```\n\n```text\nif\n```\n\n```text\ntrue\n```\n\n```text\ny\n```\n\n```text\nif\n```\n\n```text\nif\n```\n\n```text\nif\n```\n\n```text\ny\n```\n\n```text\nif (\n```\n\n```text\n)\n```\n\n```text\nelse\n```\n\n```text\nif\n```\n\n```text\ny\n```\n\n```text\nelse\n```\n\n```text\nif\n```\n\n========================================\n\nComments:\n- The way I understand it, Eric is saying that it was fixed primarily (if not exclusively) to bring the behavior of the compiler into compliance with the specification.\n- @CodyGray, I think the question was why it's in the specification.\n- @Walkerneo - Spot on. I understand why the change was made (as Eric explained) but I dont understand why it's there in the first place. Although arguably undesirable it is a valid series of statements in my mind.\n- The reason the code should not compile is due to uninitialized usage of `y` within the usage scope.\n- Why is *what* in the specification? This question is very unclear. Are you asking why a local variable must be definitely assigned? Or why flow analysis considers only constant expressions? Or what?\n- I do apologize if my question is unclear I've done my best at expressing it. But yes I am basically asking both why (given the fact that there are clearly no side effects in your example) it would be wrong to label y as a constant (or however you wanna call it) and therefore \"know\" that y is in-fact always initialized and as a result remove the out of scope error?\n- * Sorry I mean uninitialized usage error.\n- Actually the more relevant section of the spec to this question is: *The definite assignment state of v at the beginning of any other statement is determined by checking the definite assignment state of v on all control flow transfers that target the beginning of that statement... The set of possible control flow transfers is determined in the same way as for checking statement reachability.* If the condition is always true then the usage of y can only be reached via the body of the \"if\", and after that body, y is definitely assigned.\n- @Eric on the subject of specifications - it is **not** critical or pressing etc, but did you have time to think about that optional parameters issue?\n- Eric, you may want to link your blog posts on this topic: Root Of All Evil Part 1: blogs.msdn.com/b/ericlippert/archive/2006/03/28/&hellip; Part - 2: blogs.msdn.com/b/ericlippert/archive/2006/03/29/&hellip;\n- Can I just say thank you so much for taking the time to answer questions like mine so comprehensively and also helping to create such a wonderful product. Its sincerely appreciated.","metadata":{"transformedAt":"2026-08-18T18:32:17.814Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":24,"totalLines":192,"estimatedTokens":1549}}626{"id":"stack-11068508","source":"stackoverflow","questionId":11068508,"title":"What is the advantage of lexical addressing in Chapter 5 of SICP?","tags":["compiler-construction","scheme","sicp","lexical"],"text":"Title: What is the advantage of lexical addressing in Chapter 5 of SICP?\nTags: compiler-construction, scheme, sicp, lexical\nSource: Stack Overflow\n\nQuestion:\nI am reading SICP now and don't really understand the necessity of lexical addressing described in 5.5.6 Lexical addressing of SICP.\n\nSince it says\n\"Because our language is lexically scoped, the run-time environment for any expression will have a structure that parallels the lexical structure of the program in which the expression appears\", I think it costs same to search a variable in run-time environment as to search in compile environment.\nWhy do we bother to implement a compile environment?\nI think the compile environment will have the same structure that parallels the lexical structure of the program and this is same as run-time environment, isn't it?\n\n========================================\n\nCode:\n```text\nlexical-address-lookup\n```\n\n```text\nLexical-address-lookup\n```\n\n```text\nlexical-address-lookup\n```\n\n```text\nlookup-variable-value\n```\n\n========================================\n\nComments:\n- Thank you very much! Think got the point. Save run time searching cost by using the information namely, lexical address that can be acquired at compile time.\n- @user1461328 you're welcome! Please don't forget to accept the answer to your questions that you consider to be correct, by clicking on the check mark to its left.\n- Assuming a linked-list based environment, If I'm getting it right, the optimization is to navigate an exactly known number of nodes of the list, versus navigating each node of the list checking one-by-one to see if you found the binding, aren't both of these O(n)? (even though in practice the lexical addressing should be faster). The advantage would seem more significative if there was an O(1) way of reading the environment address...","metadata":{"transformedAt":"2026-08-18T18:32:17.814Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":37,"estimatedTokens":459}}627{"id":"stack-3088372","source":"stackoverflow","questionId":3088372,"title":"Strange compiler behavior with float literals vs float variables","tags":["c#","compiler-construction","floating-point"],"text":"Title: Strange compiler behavior with float literals vs float variables\nTags: c#, compiler-construction, floating-point\nSource: Stack Overflow\n\nQuestion:\nI have noticed an interesting behavior with float rounding / truncation by the C# compiler. Namely, when a float literal is beyond the guaranteed representable range (7 decimal digits), then a) explicitly casting a float result to float (a semantically unnecessary operation) and b) storing intermediate calculation results in a local variable both change the output. An example:\n\n```\nusing System;\n\nclass Program\n{\n static void Main()\n {\n float f = 2.0499999f;\n var a = f * 100f;\n var b = (int) (f * 100f);\n var c = (int) (float) (f * 100f);\n var d = (int) a;\n var e = (int) (float) a;\n Console.WriteLine(a);\n Console.WriteLine(b);\n Console.WriteLine(c);\n Console.WriteLine(d);\n Console.WriteLine(e);\n }\n}\n```\n\nThe output is:\n\n```\n205\n204\n205\n205\n205\n```\n\nIn the JITted debug build on my computer, b is calculated as follows:\n\n```\nvar b = (int) (f * 100f);\n0000005a fld dword ptr [ebp-3Ch] \n0000005d fmul dword ptr ds:[035E1648h] \n00000063 fstp qword ptr [ebp-5Ch] \n00000066 movsd xmm0,mmword ptr [ebp-5Ch] \n0000006b cvttsd2si eax,xmm0 \n0000006f mov dword ptr [ebp-44h],eax\n```\n\nwhereas d is calculated as\n\n```\nvar d = (int) a;\n00000096 fld dword ptr [ebp-40h] \n00000099 fstp qword ptr [ebp-5Ch] \n0000009c movsd xmm0,mmword ptr [ebp-5Ch] \n000000a1 cvttsd2si eax,xmm0 \n000000a5 mov dword ptr [ebp-4Ch],eax\n```\n\nFinally, my question: why is the second line of the output different from the fourth? Does that extra fmul make such a difference? Also note that if the last (already unrepresentable) digit from the float f is removed or even reduced, everything \"falls in place\".\n\n========================================\n\nTop Answer:\nIn a comment you asked\n\n Are these rules different? \n\nYes. Or, rather, the rules allow for different behaviour.\n\n And if yes, am I supposed to know this, either from the C# language reference doc or MSDN, or is this just an occasional discrepancy between the compiler and the runtime\n\nIt's implied by the specification. Floating point operations have a certain minimum level of precision that must be met, but the compiler or runtime is permitted to use *more* precision if it sees fit. That can cause large, observable changes when you do operations that magnify small changes. Rounding, for example, can turn an extremely small change into an extremely large one.\n\nThis fact leads to fairly frequently asked questions here. For some background on this situation and other situations that can produce similar discrepancies, see the following:\n\nWhy does this floating-point calculation give different results on different machines?\n\nC# XNA Visual Studio: Difference between \"release\" and \"debug\" modes? \n\nCLR JIT optimizations violates causality?\n\nhttps://stackoverflow.com/questions/2494724\n\n========================================\n\nCode:\n```text\nusing System;\n\nclass Program\n{\n    static void Main()\n    {\n        float f = 2.0499999f;\n        var a = f * 100f;\n        var b = (int) (f * 100f);\n        var c = (int) (float) (f * 100f);\n        var d = (int) a;\n        var e = (int) (float) a;\n        Console.WriteLine(a);\n        Console.WriteLine(b);\n        Console.WriteLine(c);\n        Console.WriteLine(d);\n        Console.WriteLine(e);\n    }\n}\n```\n\n```text\n205\n204\n205\n205\n205\n```\n\n```text\nvar b = (int) (f * 100f);\n0000005a  fld         dword ptr [ebp-3Ch] \n0000005d  fmul        dword ptr ds:[035E1648h] \n00000063  fstp        qword ptr [ebp-5Ch] \n00000066  movsd       xmm0,mmword ptr [ebp-5Ch] \n0000006b  cvttsd2si   eax,xmm0 \n0000006f  mov         dword ptr [ebp-44h],eax\n```\n\n```text\nvar d = (int) a;\n00000096  fld         dword ptr [ebp-40h] \n00000099  fstp        qword ptr [ebp-5Ch] \n0000009c  movsd       xmm0,mmword ptr [ebp-5Ch] \n000000a1  cvttsd2si   eax,xmm0 \n000000a5  mov         dword ptr [ebp-4Ch],eax\n```\n\n```text\nfloat f = 2.0499999f;\nvar a = f * 100f;\nvar b = (int)(f * 100f);\nvar d = (int)a;\nConsole.WriteLine(b);\nConsole.WriteLine(d);\n```\n\n```text\nfloat f = 2.05f;\nfloat a = f * 100f;\nint b = (int) (f * 100f);\nint d = (int) a;\nConsole.WriteLine(b);\nConsole.WriteLine(d);\n```\n\n```text\nConsole.WriteLine((int) (2.0499999f * 100f));\nConsole.WriteLine((int)(float)(2.0499999f * 100f));\n```\n\n```text\nConsole.WriteLine(204);\nConsole.WriteLine(205);\n```\n\n```text\n2.05 * 100f\n```\n\n```text\nfloat f = (Math.Sin(0.5) < 5) ? 2.0499999f : -1;\n    var a = f * 100f;\n    var b = (int) (f * 100f);\n    var c = (int) (float) (f * 100f);\n    var d = (int) a;\n    var e = (int) (float) a;\n    Console.WriteLine(a);\n    Console.WriteLine(b);\n    Console.WriteLine(c);\n    Console.WriteLine(d);\n    Console.WriteLine(e);\n```\n\n```text\n205\n204\n205\n204\n205\n```\n\n```text\n2.0499999f\n```\n\n```text\n2.049999f\n```\n\n========================================\n\nComments:\n- i saw answer to this question here but can't find it\n- So you say the reason is that (int) is done by truncation and (float) means rounding. If that's the case, then why is the output different for Console.WriteLine((int) (2.0499999f * 100f)) and Console.WriteLine((int) (float) (2.0499999f * 100f)) ?\n- @Alan, check my answer. reason is that float can hold only 7 digits. log (2^23) = 6.9\n- @Alan: When you use hard-coded constants the calculations are done entirely in the compiler and using the compiler's rules, not in the .NET runtime.\n- @Andrey, thanks. I know that the float exceeds the representable range (see question, I did not edit that part), but then, with your confirmation, this looks a little scary to me - casting a float to float should not make a difference, but in this case, it does.\n- @Mark: Are these rules different? And if yes, am I supposed to know this, either from the C# language reference doc or MSDN, or is this just an occasional discrepancy between the compiler and the runtime?\n- @Alan: Apparently they are different as your example shows. I think in general though relying on the least significant digit of a floating point calculation is an incredibly bad idea.\n- Thanks Andrey, I chose Mark's reply on the basis of the compiler vs runtime info, but yours is also relevant.\n- Eric, thank you very much. Your last link was especially enlightening. I actually searched for similar scenarios before I posted the question, but apparently my scope was too narrow.","metadata":{"transformedAt":"2026-08-18T18:32:17.815Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":213,"estimatedTokens":1589}}628{"id":"stack-19730548","source":"stackoverflow","questionId":19730548,"title":"C/C++ Code Compiler in C#","tags":["c#","c++",".net","c","compiler-construction"],"text":"Title: C/C++ Code Compiler in C#\nTags: c#, c++, .net, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn C#, I am able to compile VB and C# Code, using the code below, but I have no way of compiling C/C++ code. Is there any way of doing this?\n\nC# Compiler:\n\n```\npublic void Compile(string ToCompile)\n {\n string Result = null;\n string errors = null;\n Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();\n System.CodeDom.Compiler.ICodeCompiler icc = codeProvider.CreateCompiler();\n string Output = @\"mypath\";\n System.CodeDom.Compiler.CompilerParameters parameters = new System.CodeDom.Compiler.CompilerParameters();\n parameters.GenerateExecutable = true;\n parameters.OutputAssembly = Output;\n System.CodeDom.Compiler.CompilerResults results = icc.CompileAssemblyFromSource(parameters, ToCompile);\n if (ReturnErrors == true)\n {\n if (results.Errors.Count > 0)\n {\n foreach (System.CodeDom.Compiler.CompilerError CompErr in results.Errors)\n {\n errors +=\n \"Line number \" + CompErr.Line +\n \", Error Number: \" + CompErr.ErrorNumber +\n \", '\" + CompErr.ErrorText + \";\" +\n Environment.NewLine + Environment.NewLine;\n }\n Result += \"Errors have been found in your code: \" + Environment.NewLine + errors;\n }\n else\n {\n Result += \"Success!\";\n System.Diagnostics.Process.Start(Output);\n }\n }\n```\n\nAnd to create a VB compiler, I simply replace `Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();` with `Microsoft.VisualBasic.VBCodeProvider codeProvider = new Microsoft.VisualBasic.VBCodeProvider();`\n\n========================================\n\nTop Answer:\nI'm assuming you've got a chunk of source, say containing a function with a known prototype, which you want to compile, and run within your currently running application.\n\nIn order to do this in native (not managed) C++, you'd need to do the following:\n\n- Dynamically store your code into a boilerplate dll source project (i.e. everything written with a gap for the function's code, which you'd insert)\n\n- Spawn a C++ compiler (which the end user would have to have pre-installed) to output a dll\n\n- Build up a C++/Cli wrapper that wraps the c++ dll that you built above, and compile that too (see Redwan's answer)\n\n- Dynamically load your wrapper Dll, and call the function.\n\nIf you can work with just managed c++/CLI, then Redwan's answer should be adequate on its own.\n\n========================================\n\nCode:\n```text\npublic void Compile(string ToCompile)\n        {\n            string Result = null;\n            string errors = null;\n            Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();\n            System.CodeDom.Compiler.ICodeCompiler icc = codeProvider.CreateCompiler();\n            string Output = @\"mypath\";\n            System.CodeDom.Compiler.CompilerParameters parameters = new System.CodeDom.Compiler.CompilerParameters();\n            parameters.GenerateExecutable = true;\n            parameters.OutputAssembly = Output;\n            System.CodeDom.Compiler.CompilerResults results = icc.CompileAssemblyFromSource(parameters, ToCompile);\n            if (ReturnErrors == true)\n            {\n                if (results.Errors.Count > 0)\n                {\n                    foreach (System.CodeDom.Compiler.CompilerError CompErr in results.Errors)\n                    {\n                        errors +=\n                                    \"Line number \" + CompErr.Line +\n                                    \", Error Number: \" + CompErr.ErrorNumber +\n                                    \", '\" + CompErr.ErrorText + \";\" +\n                                    Environment.NewLine + Environment.NewLine;\n                    }\n                    Result += \"Errors have been found in your code: \" + Environment.NewLine + errors;\n                }\n                else\n                {\n                    Result += \"Success!\";\n                    System.Diagnostics.Process.Start(Output);\n                }\n            }\n```\n\n```text\nMicrosoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();\n```\n\n```text\nMicrosoft.VisualBasic.VBCodeProvider codeProvider = new Microsoft.VisualBasic.VBCodeProvider();\n```\n\n========================================\n\nComments:\n- Are you asking if there is a way to compile C/C++ code from C#?\n- c# and vb are managed codes means which runs under CLR.but C/C++ code is called unmanaged code, i think marshalling is what you are looking for.\n- @Sudhakar No that really isn't what he's after. Managed C++ can be compiled to run on the CLR.\n- Nice, I didn't know about that. So deleted my answer as it's plain wrong. Here's an English link to that class BTW: msdn.microsoft.com/en-us/library/&hellip;\n- @DavidArno My bad. Forgot about russian language of article. Updated my answer.\n- @Redwan - My Copy of Visual Studio 2010 does not have Microsoft.VisualC - What should I do?\n- @Joe this class is shipped within Windows SDK as mentioned in this article msdn.microsoft.com/en-us/library/&hellip;\n- @Redwan - I am going to download Windows SDK - Is this a good idea?\n- @Joe Why it can be a bad idea? It adds headers, libraries and tools to build apps which would run on Windows. You can simply ship needed dlls from sdk with your app.","metadata":{"transformedAt":"2026-08-18T18:32:17.815Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":119,"estimatedTokens":1324}}629{"id":"stack-3606188","source":"stackoverflow","questionId":3606188,"title":"The stack is created by compiler or OS/architecture","tags":["java","c","compiler-construction","operating-system"],"text":"Title: The stack is created by compiler or OS/architecture\nTags: java, c, compiler-construction, operating-system\nSource: Stack Overflow\n\nQuestion:\nThis question is a up of my previous question \n\nstack growth direction\n\nI would like to know whether stack is created by a compiler or OS/architecture ? Also how does OS knows about these compiler specific things ? For ex: C++ allows variables to create data on stack and heap while java allows only heap. \n\nAlso if stack is created on heap as mentioned in the post how can system know about it because the system knows only about stack pointer and base pointer.\n\n========================================\n\nTop Answer:\nThe stack is most definitely defined by the compiler, the OS allocates the space for it but that is relatively trivial. The stack is a dedicated place in memory that is used by the compiler (in so much as the compiler defines the instructions that use it) to control program execution flow and store local variables etc.\n\nSo the OS doesn't know about the compiler specific stuff. The stack is still stored in main memory it is just not part of memory that you (the programmer) can directly control.\n\n========================================\n\nCode:\n```text\nesp\n```\n\n========================================\n\nComments:\n- possible duplicate of What and where are the stack and heap\n- I can't see how the two questions are connected from the point of view of Java. You do realize that the JVM effectively acts the platform/architecture in the Java world, do you?\n- @Byron This is just a up of my previous question. Also the question is not about knowing about the stack. From my previous post I was said that stack can be created on heap to prevent things like buffer overflows. So if created on a heap how can system know about this ?\n- @brett, Yes and it is obvious you need to *get a basic understanding of what the stack and heap is*. Read the dupe answers carefully.You can create A particular stack data structure in the heap, but that isn't the fundamental stack structure we are talking about.\n- @brett, I just read the comments on your last accepted answer. Memory is all the same but conceptually, stack = memory allocated at compile time, and heap = dynamically allocated memory. I hope that is clear.\n- @byron we are talking about logical addresses right.For ex: 2 frames for stack are created on a heap. they both have different logical addresses and are not contiguous as created on a stack right ?\n- @brett, 2 frames for a stack are created in **memory**. It is an implementation detail where the are actually located. Remember the Heap isn't a location, it is a data structure, just like a stack. And you will never have your stack frames being allocated in the heap datastructure. This doesn't mean they cant exist next to each other in **memory**. Where they are actually stored is up to the compiler , OS and architecture.\n- @Byron: I believe that some virtual machines may actually create stack-frame type structures via heap allocation, but in the C world the compiler just does math on the stack pointer.\n- @ByronWhitlock: \"never have stack frames allocated in the heap\". Wrong. There's 3 concepts here: \"stack\", \"activation record\", and \"heap\". The heap is place where one can get storage at runtime. A called function needs storage (\"activation record\") to manage its computation; it may be allocated from stack, or allocated from the heap. A stack is data structure that makes evaluating computations easy, by enabling LIFO access to values. A function may use a stack *within* its activation record; or it may use a globally available stack. See stackoverflow.com/a/1053159/120163\n- for Java, it should read **a stack**, as we have one jvm stack for each running thread.\n- In the executables header there is often a field for the mainthreads stacksize (field is set by the compiler and can normally be set with a linker option). Also, when creating stacks, one can often set the threads stacksize.\n- JVM thread stacks may or may not be the same memory as the actual java program. This is complicated to talk about, but the JVM could keep up with a separate stack for each java thread from the its system stacks that it is using for it's native code.","metadata":{"transformedAt":"2026-08-18T18:32:17.815Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":42,"estimatedTokens":1060}}630{"id":"stack-45279434","source":"stackoverflow","questionId":45279434,"title":"How programs written in interpreted languages are executed if they are never translated into machine language?","tags":["programming-languages","interpreter","dynamic-languages","compiler-construction"],"text":"Title: How programs written in interpreted languages are executed if they are never translated into machine language?\nTags: programming-languages, interpreter, dynamic-languages, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nComputers can only understand machine language. Then how come interepreters execute a program directly without translating it into machine language? For example:\n\n```\nIt's a simple Hello World program written in PHP. How does it execute in machine while the machine has no idea what `echo` is? How does it output what's expected, in this case, the string Hello, World!?\n\n========================================\n\nTop Answer:\nStrictly speaking, the interpreter is being executed and the code that the interpreter is interpreting just determines what actions the interpreter takes. (If it *was* just compiled to machine code, what would you need the interpreter for?).\n\nFor example, I built an automation framework awhile back where we captured reflection metadata on what was occurring at runtime during QA tests. We serialized that metadata to JSON. The JSON was never compiled to anything - it just told the automation engine what methods to call and what parameters to pass. No machine code involved. It wouldn't be exactly correct to say that we were \"executing\" the JSON - we were executing the automation engine, which was then following the \"directions\" found in the JSON, but it was certainly *interpreting* the JSON.\n\n========================================\n\nCode:\n```text\n<?php\n    echo \"Hello, World!\" ;\n```\n\n```text\necho\n```\n\n```text\ndef execute_program(prog)\n  for statement in prog.toplevel_statements:\n    execute_statement(statement)\n\ndef execute_statement(statement):\n  if statement is an echo statement:\n    print( evaluate_expression(statement.argument) )\n  else if statement is a for loop:\n    execute_statement(statement.init)\n    while evaluate_expression(statement.condition).is_truthy():\n      for inner_statement in statement.body:\n        execute_statement(inner_statement)\n      execute_statement(statement.increment)\n  else if ...\n```\n\n```text\nif user_input == rectangle: [code to draw a rectangle]\n```\n\n========================================\n\nComments:\n- Possible duplicate of How exactly is a PHP script executed?\n- The code is interpreted by a program which is most likely compiled into machine code. In the example of an `echo`-like instruction, it might cause the interpreter to call a function it its own program that writes the output.\n- In short: there is always module (for php)/framework (like in .net)/platform (Java)/etc - layer that know how to translate interpreted to machine codes. For more details - check documentation for programming language you are interested in.","metadata":{"transformedAt":"2026-08-18T18:32:17.815Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":56,"estimatedTokens":688}}631{"id":"stack-35867507","source":"stackoverflow","questionId":35867507,"title":"How does the C# compiler handle overloading explicit cast operators?","tags":["c#","compiler-construction"],"text":"Title: How does the C# compiler handle overloading explicit cast operators?\nTags: c#, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThe compiler should translate this code:\n\n```\npublic static explicit operator Int64(MyNumber n)\n{\n return n.ToInteger();\n}\n\npublic static explicit operator Double(MyNumber n)\n{\n return n.ToDouble();\n}\n```\n\nto two methods that have the same name and signature but differ only by their return type, e.g.\n\n```\npublic static Int64 ExplicitCast(MyNumber n)\n...\n\npublic static Double ExplicitCast(MyNumber n)\n...\n```\n\nHowever, we are not allowed to have methods that differ only by their return type. What happens behind the curtains?\n\n========================================\n\nTop Answer:\nI'm not sure the purpose of your question, so this is the best answer I can give you. The above code compiles to something like this \n\n```\nMyNumber.op_Explicit:\nIL_0000: nop \nIL_0001: ldarg.0 \nIL_0002: callvirt UserQuery+MyNumber.ToInteger\nIL_0007: stloc.0 \nIL_0008: br.s IL_000A\nIL_000A: ldloc.0 \nIL_000B: ret \n\nMyNumber.op_Explicit:\nIL_0000: nop \nIL_0001: ldarg.0 \nIL_0002: callvirt UserQuery+MyNumber.ToDouble\nIL_0007: stloc.0 \nIL_0008: br.s IL_000A\nIL_000A: ldloc.0 \nIL_000B: ret\n```\n\nThe reason it appears \"inconsistent\" is because of the keywords `explicit operator`. It tells the compiler to generate code differently from the way it does in an explicit cast.\n\nYou'd get something like this without it:\n\n```\nMyNumber.Double2:\nIL_0000: nop \nIL_0001: ldarg.0 \nIL_0002: callvirt UserQuery+MyNumber.ToDouble\nIL_0007: stloc.0 \nIL_0008: br.s IL_000A\nIL_000A: ldloc.0 \nIL_000B: ret\n```\n\n========================================\n\nCode:\n```text\npublic static explicit operator Int64(MyNumber n)\n{\n    return n.ToInteger();\n}\n\npublic static explicit operator Double(MyNumber n)\n{\n    return n.ToDouble();\n}\n```\n\n```text\npublic static Int64 ExplicitCast(MyNumber n)\n...\n\npublic static Double ExplicitCast(MyNumber n)\n...\n```\n\n```text\ncall int64 MyNumber::op_Explicit(class MyNumber)\n```\n\n```text\ncall float64 MyNumber::op_Explicit(class MyNumber)\n```\n\n```text\nop_Explicit\n```\n\n```text\ncall\n```\n\n```text\nop_Implicit\n```\n\n```text\nop_Explicit\n```\n\n```text\nMyNumber.op_Explicit:\nIL_0000:  nop         \nIL_0001:  ldarg.0     \nIL_0002:  callvirt    UserQuery+MyNumber.ToInteger\nIL_0007:  stloc.0     \nIL_0008:  br.s        IL_000A\nIL_000A:  ldloc.0     \nIL_000B:  ret         \n\nMyNumber.op_Explicit:\nIL_0000:  nop         \nIL_0001:  ldarg.0     \nIL_0002:  callvirt    UserQuery+MyNumber.ToDouble\nIL_0007:  stloc.0     \nIL_0008:  br.s        IL_000A\nIL_000A:  ldloc.0     \nIL_000B:  ret\n```\n\n```text\nMyNumber.Double2:\nIL_0000:  nop         \nIL_0001:  ldarg.0     \nIL_0002:  callvirt    UserQuery+MyNumber.ToDouble\nIL_0007:  stloc.0     \nIL_0008:  br.s        IL_000A\nIL_000A:  ldloc.0     \nIL_000B:  ret\n```\n\n```text\nexplicit operator\n```\n\n========================================\n\nComments:\n- *\"The compiler should translate\"* - says who?\n- The limitation is in the C# compiler, not in the IL assembly code. So the C# compiler can generate IL code that would be illegal to write in C#.\n- Might want to add that the CLR doesn't do any 'real' overload resolution. It simply uses the exact signature to lookup the member - and then calls the member (virtual if necessary). The actual *overload resolution* happens in the compiler (or in `dynamic` if you like); overloaded members are *defined* in the CIL (with the specified rules) and merely validated.\n- :-) It is exactly what atlast said :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.815Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":160,"estimatedTokens":877}}632{"id":"stack-34236034","source":"stackoverflow","questionId":34236034,"title":"How to track down LLVM verifyFunction error \"Expected no forward declarations!\"?","tags":["c++","debugging","compiler-construction","llvm","llvm-ir"],"text":"Title: How to track down LLVM verifyFunction error \"Expected no forward declarations!\"?\nTags: c++, debugging, compiler-construction, llvm, llvm-ir\nSource: Stack Overflow\n\nQuestion:\nI'm developing a compiler for a new language of mine in LLVM and have run into an issue while generating debug information.\n\nI have not yet found much documentation on how to actually generate the debug information using the DIBuilder so it is very possible I'm doing something terribly wrong.\n\nI have been mostly looking at the Kaleidoscope example as it is the only one I have found that uses debug information. I have yet to crack open Clang to look at how they have been using it, but I would love to hear from someone who has.\n\nI have been able to compile and run my language with some more complex examples, but I started back at some basics for adding the debugging support. Here is the simple script I am trying to compile:\n\n```\ndouble my_main()\n{\n return 0.0;\n}\n```\n\nHere is my output from verifyFunction, verifyModule, and dumping the module.\n\n***Edit: note in edits below I point out that the dump is after a call to finalize which correctly removes the temporary.***\n\n```\nFailed To Verify Function: my_main error: Expected no forward declarations!\n!8 = !{}\n\nFailed To Verify Module: test.str error: Expected no forward declarations!\n!8 = !{}\n\n; ModuleID = 'test.str'\n\ndefine double @my_main() !dbg !6 {\nentry:\n br label %block, !dbg !10\n\nblock: ; preds = %entry\n ret double 0.000000e+00, !dbg !10\n}\n\n!llvm.module.flags = !{!0, !1}\n!llvm.dbg.cu = !{!2}\n\n!0 = !{i32 2, !\"Debug Info Version\", i32 3}\n!1 = !{i32 2, !\"Dwarf Version\", i32 2}\n!2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: \"Test Compiler\", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !4, subprograms: !5)\n!3 = !DIFile(filename: \"test.str\", directory: \".\")\n!4 = !{}\n!5 = !{!6}\n!6 = distinct !DISubprogram(name: \"my_main\", scope: !3, file: !3, line: 10, type: !7, isLocal: false, isDefinition: true, scopeLine: 10, isOptimized: false, variables: !4)\n!7 = !DISubroutineType(types: !8)\n!8 = !{!9}\n!9 = !DIBasicType(name: \"double\", size: 64, align: 8, encoding: DW_ATE_float)\n!10 = !DILocation(line: 9, column: 11, scope: !6)\n```\n\nSearching for the error message in the LLVM codebase reveals the source in Verifier.cpp:\n\n```\nvoid Verifier::visitMDNode(const MDNode &MD) {\n // Only visit each node once. Metadata can be mutually recursive, so this\n // avoids infinite recursion here, as well as being an optimization.\n if (!MDNodes.insert(&MD).second)\n return;\n\n switch (MD.getMetadataID()) {\n default:\n llvm_unreachable(\"Invalid MDNode subclass\");\n case Metadata::MDTupleKind:\n break;\n#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \\\n case Metadata::CLASS##Kind: \\\n visit##CLASS(cast(MD)); \\\n break;\n#include \"llvm/IR/Metadata.def\"\n }\n\n for (unsigned i = 0, e = MD.getNumOperands(); i != e; ++i) {\n Metadata *Op = MD.getOperand(i);\n if (!Op)\n continue;\n Assert(!isa(Op), \"Invalid operand for global metadata!\",\n &MD, Op);\n if (auto *N = dyn_cast(Op)) {\n visitMDNode(*N);\n continue;\n }\n if (auto *V = dyn_cast(Op)) {\n visitValueAsMetadata(*V, nullptr);\n continue;\n }\n }\n\n // Check these last, so we diagnose problems in operands first.\n Assert(!MD.isTemporary(), \"Expected no forward declarations!\", &MD);\n Assert(MD.isResolved(), \"All nodes should be resolved!\", &MD);\n}\n```\n\nI'm assuming by the assertion that I have some metadata that is still considered \"Temporary\", but I would like to know how to track down what is creating it.\n\nI'm creating my types much like how the example does:\n\n```\n// here dbuilder is a DIBuilder* and alignment is coming from \n// my Module's getDataLayout().getABITypeAlignment(t);\n// where t is Type::getDoubleTy(context.getLLVMContext());\n// the context object is my own type\ndbuilder->createBasicType(\"double\", 64, alignment, dwarf::DW_ATE_float);\n```\n\nMy debug function creation logic uses this type, among others, in another call from the example:\n\n```\n// the argument is of type: SmallVector returnPlusParams;\ndbuilder->createSubroutineType(dbuilder->getOrCreateTypeArray(returnPlusParams));\n```\n\nI am also setting line and column numbers on my IRBuilder from my AST nodes:\n\n```\n_mBuilder->SetCurrentDebugLocation(DebugLoc::get(node->line, node->column, currentDebugScope()));\n```\n\nI was also reading the page on SourceLevelDebugging, but this doesn't talk about the C++ API to LLVM as much as the debug IR format.\n\nIf anyone notices something obvious in my module dump or has any further suggestions I would appreciate it greatly.\n\n### Edit: Adding example IR\n\nI did a few more tests and wanted to post the output of a similar function output from Clang with the following command:\n\n```\nclang -cc1 hello_llvm.c -emit-llvm\n```\n\n***Edit: I also found this post to add debug info to the output.***\n\nThis code:\n\n```\ndouble main() {\n return 0.0;\n}\n```\n\nCompiles to this:\n\n```\n; ModuleID = 'hello_llvm.c'\ntarget datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\ntarget triple = \"x86_64-apple-darwin15.0.0\"\n\n; Function Attrs: nounwind\ndefine double @main() #0 {\nentry:\n ret double 0.000000e+00\n}\n\nattributes #0 = { nounwind \"disable-tail-calls\"=\"false\" \"less-precise-fpmad\"=\"false\" \"no-frame-pointer-elim\"=\"false\" \"no-infs-fp-math\"=\"false\" \"no-nans-fp-math\"=\"false\" \"stack-protector-buffer-size\"=\"8\" \"target-features\"=\"+mmx,+sse,+sse2\" \"unsafe-fp-math\"=\"false\" \"use-soft-float\"=\"false\" }\n\n!llvm.ident = !{!0}\n\n!0 = !{!\"clang version 3.8.0 (http://llvm.org/git/clang.git 80803f026ba7160f7cfa122c7ef829ab42abc3bf) (http://llvm.org/git/llvm.git 1bb03c5884405c428c3ab54631c0528b6cedeb54)\"}\n```\n\nIt gives the obvious warn to change return type of `main` to int. I produced an int version as well which produces an alloca:\n\n```\n; ModuleID = 'hello_llvm.c'\ntarget datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\ntarget triple = \"x86_64-apple-darwin15.0.0\"\n\n; Function Attrs: nounwind\ndefine i32 @main() #0 {\nentry:\n %retval = alloca i32, align 4\n store i32 0, i32* %retval, align 4\n ret i32 0\n}\n\nattributes #0 = { nounwind \"disable-tail-calls\"=\"false\" \"less-precise-fpmad\"=\"false\" \"no-frame-pointer-elim\"=\"false\" \"no-infs-fp-math\"=\"false\" \"no-nans-fp-math\"=\"false\" \"stack-protector-buffer-size\"=\"8\" \"target-features\"=\"+mmx,+sse,+sse2\" \"unsafe-fp-math\"=\"false\" \"use-soft-float\"=\"false\" }\n\n!llvm.ident = !{!0}\n\n!0 = !{!\"clang version 3.8.0 (http://llvm.org/git/clang.git 80803f026ba7160f7cfa122c7ef829ab42abc3bf) (http://llvm.org/git/llvm.git 1bb03c5884405c428c3ab54631c0528b6cedeb54)\"}\n```\n\n***NOTE:*** In my example `double` was chosen as an arbitrary return type, but `int` also fails. In some of my initial tests I actually wrap my_main with a proper `main` with argv/argc and was able to compile and run from the terminal.\n\n### Edit 2: 'finalize'\n\nI didn't see anything too obvious in the previous IR so I decided to run verifyModule after the module `finalize` call. This was successful and reflected in the IR dump we see above.\n\nI then decided to dump the module prior to finalize. This time you can see the temp it was complaining about.\n\n```\nFailed To Verify Module: test.str error: Expected no forward declarations!\n!8 = !{}\n\n; ModuleID = 'test.str'\n\ndefine i32 @my_main() !dbg !4 {\nentry:\n br label %block, !dbg !9\n\nblock: ; preds = %entry\n ret i32 0, !dbg !9\n}\n\n!llvm.module.flags = !{!0, !1}\n!llvm.dbg.cu = !{!2}\n\n!0 = !{i32 2, !\"Debug Info Version\", i32 3}\n!1 = !{i32 2, !\"Dwarf Version\", i32 2}\n!2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: \"Test Compiler\", isOptimized: false, runtimeVersion: 0, emissionKind: 1)\n!3 = !DIFile(filename: \"test.str\", directory: \".\")\n!4 = distinct !DISubprogram(name: \"my_main\", scope: !3, file: !3, line: 10, type: !5, isLocal: false, isDefinition: true, scopeLine: 10, isOptimized: false, variables: !8)\n!5 = !DISubroutineType(types: !6)\n!6 = !{!7}\n!7 = !DIBasicType(name: \"int32\", size: 32, align: 4, encoding: DW_ATE_signed)\n!8 = !{}\n!9 = !DILocation(line: 9, column: 11, scope: !4)\n```\n\nSo I suppose the question is...\n\nIs there something that must be done prior to verify to clean up this temporary? or am I just creating the type incorrectly? What operations implicitly create temporaries?\n\n========================================\n\nTop Answer:\nI got the same issue by using IRBuilder and DIBuilder. The problem on my side was that I was calling `llvm::ExecutionEngine::getFunctionAddress` (which triggers code generation) before finalizing the DIBuilder (`llvm::DIBuilder::finalize()`). \nSo ensure you are calling `dbuilder->finalize()` before any code generation of your llvm::Module (in fact before the verify module is invoked).\n\n========================================\n\nCode:\n```text\ndouble my_main()\n{\n    return 0.0;\n}\n```\n\n```text\nFailed To Verify Function: my_main error: Expected no forward declarations!\n!8 = <temporary!> !{}\n\nFailed To Verify Module: test.str error: Expected no forward declarations!\n!8 = <temporary!> !{}\n\n; ModuleID = 'test.str'\n\ndefine double @my_main() !dbg !6 {\nentry:\n  br label %block, !dbg !10\n\nblock:                                            ; preds = %entry\n  ret double 0.000000e+00, !dbg !10\n}\n\n!llvm.module.flags = !{!0, !1}\n!llvm.dbg.cu = !{!2}\n\n!0 = !{i32 2, !\"Debug Info Version\", i32 3}\n!1 = !{i32 2, !\"Dwarf Version\", i32 2}\n!2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: \"Test Compiler\", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !4, subprograms: !5)\n!3 = !DIFile(filename: \"test.str\", directory: \".\")\n!4 = !{}\n!5 = !{!6}\n!6 = distinct !DISubprogram(name: \"my_main\", scope: !3, file: !3, line: 10, type: !7, isLocal: false, isDefinition: true, scopeLine: 10, isOptimized: false, variables: !4)\n!7 = !DISubroutineType(types: !8)\n!8 = !{!9}\n!9 = !DIBasicType(name: \"double\", size: 64, align: 8, encoding: DW_ATE_float)\n!10 = !DILocation(line: 9, column: 11, scope: !6)\n```\n\n```text\nvoid Verifier::visitMDNode(const MDNode &MD) {\n  // Only visit each node once.  Metadata can be mutually recursive, so this\n  // avoids infinite recursion here, as well as being an optimization.\n  if (!MDNodes.insert(&MD).second)\n    return;\n\n  switch (MD.getMetadataID()) {\n  default:\n    llvm_unreachable(\"Invalid MDNode subclass\");\n  case Metadata::MDTupleKind:\n    break;\n#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS)                                  \\\n  case Metadata::CLASS##Kind:                                                  \\\n    visit##CLASS(cast<CLASS>(MD));                                             \\\n    break;\n#include \"llvm/IR/Metadata.def\"\n  }\n\n  for (unsigned i = 0, e = MD.getNumOperands(); i != e; ++i) {\n    Metadata *Op = MD.getOperand(i);\n    if (!Op)\n      continue;\n    Assert(!isa<LocalAsMetadata>(Op), \"Invalid operand for global metadata!\",\n           &MD, Op);\n    if (auto *N = dyn_cast<MDNode>(Op)) {\n      visitMDNode(*N);\n      continue;\n    }\n    if (auto *V = dyn_cast<ValueAsMetadata>(Op)) {\n      visitValueAsMetadata(*V, nullptr);\n      continue;\n    }\n  }\n\n  // Check these last, so we diagnose problems in operands first.\n  Assert(!MD.isTemporary(), \"Expected no forward declarations!\", &MD);\n  Assert(MD.isResolved(), \"All nodes should be resolved!\", &MD);\n}\n```\n\n```text\n// here dbuilder is a DIBuilder* and alignment is coming from \n// my Module's getDataLayout().getABITypeAlignment(t);\n// where t is Type::getDoubleTy(context.getLLVMContext());\n// the context object is my own type\ndbuilder->createBasicType(\"double\", 64, alignment, dwarf::DW_ATE_float);\n```\n\n```text\n// the argument is of type: SmallVector<Metadata *, 8> returnPlusParams;\ndbuilder->createSubroutineType(dbuilder->getOrCreateTypeArray(returnPlusParams));\n```\n\n```text\n_mBuilder->SetCurrentDebugLocation(DebugLoc::get(node->line, node->column, currentDebugScope()));\n```\n\n```text\nclang -cc1 hello_llvm.c -emit-llvm\n```\n\n```text\ndouble main() {\n  return 0.0;\n}\n```\n\n```text\n; ModuleID = 'hello_llvm.c'\ntarget datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\ntarget triple = \"x86_64-apple-darwin15.0.0\"\n\n; Function Attrs: nounwind\ndefine double @main() #0 {\nentry:\n  ret double 0.000000e+00\n}\n\nattributes #0 = { nounwind \"disable-tail-calls\"=\"false\" \"less-precise-fpmad\"=\"false\" \"no-frame-pointer-elim\"=\"false\" \"no-infs-fp-math\"=\"false\" \"no-nans-fp-math\"=\"false\" \"stack-protector-buffer-size\"=\"8\" \"target-features\"=\"+mmx,+sse,+sse2\" \"unsafe-fp-math\"=\"false\" \"use-soft-float\"=\"false\" }\n\n!llvm.ident = !{!0}\n\n!0 = !{!\"clang version 3.8.0 (http://llvm.org/git/clang.git 80803f026ba7160f7cfa122c7ef829ab42abc3bf) (http://llvm.org/git/llvm.git 1bb03c5884405c428c3ab54631c0528b6cedeb54)\"}\n```\n\n```text\n; ModuleID = 'hello_llvm.c'\ntarget datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\ntarget triple = \"x86_64-apple-darwin15.0.0\"\n\n; Function Attrs: nounwind\ndefine i32 @main() #0 {\nentry:\n  %retval = alloca i32, align 4\n  store i32 0, i32* %retval, align 4\n  ret i32 0\n}\n\nattributes #0 = { nounwind \"disable-tail-calls\"=\"false\" \"less-precise-fpmad\"=\"false\" \"no-frame-pointer-elim\"=\"false\" \"no-infs-fp-math\"=\"false\" \"no-nans-fp-math\"=\"false\" \"stack-protector-buffer-size\"=\"8\" \"target-features\"=\"+mmx,+sse,+sse2\" \"unsafe-fp-math\"=\"false\" \"use-soft-float\"=\"false\" }\n\n!llvm.ident = !{!0}\n\n!0 = !{!\"clang version 3.8.0 (http://llvm.org/git/clang.git 80803f026ba7160f7cfa122c7ef829ab42abc3bf) (http://llvm.org/git/llvm.git 1bb03c5884405c428c3ab54631c0528b6cedeb54)\"}\n```\n\n```text\nFailed To Verify Module: test.str error: Expected no forward declarations!\n!8 = <temporary!> !{}\n\n; ModuleID = 'test.str'\n\ndefine i32 @my_main() !dbg !4 {\nentry:\n  br label %block, !dbg !9\n\nblock:                                            ; preds = %entry\n  ret i32 0, !dbg !9\n}\n\n!llvm.module.flags = !{!0, !1}\n!llvm.dbg.cu = !{!2}\n\n!0 = !{i32 2, !\"Debug Info Version\", i32 3}\n!1 = !{i32 2, !\"Dwarf Version\", i32 2}\n!2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: \"Test Compiler\", isOptimized: false, runtimeVersion: 0, emissionKind: 1)\n!3 = !DIFile(filename: \"test.str\", directory: \".\")\n!4 = distinct !DISubprogram(name: \"my_main\", scope: !3, file: !3, line: 10, type: !5, isLocal: false, isDefinition: true, scopeLine: 10, isOptimized: false, variables: !8)\n!5 = !DISubroutineType(types: !6)\n!6 = !{!7}\n!7 = !DIBasicType(name: \"int32\", size: 32, align: 4, encoding: DW_ATE_signed)\n!8 = <temporary!> !{}\n!9 = !DILocation(line: 9, column: 11, scope: !4)\n```\n\n```text\nmain\n```\n\n```text\ndouble\n```\n\n```text\nint\n```\n\n```text\nmain\n```\n\n```text\nfinalize\n```\n\n```text\nCODEGENOPT(VerifyModule      , 1, 1) ///< Control whether the module should be run\n                                     ///< through the LLVM Verifier.\n```\n\n```text\nverifyFunction\n```\n\n```text\nverifyModule\n```\n\n```text\nverifyModule\n```\n\n```text\nllvm::ExecutionEngine::getFunctionAddress\n```\n\n```text\nllvm::DIBuilder::finalize()\n```\n\n```text\ndbuilder->finalize()\n```\n\n========================================\n\nComments:\n- I got the same issue when learning the Kaleidoscope example. @Juicebox's answer is right, but incomplete. If we are generating a function, calling `dbuilder->finalize()` is not convenient. We could call `DBuilder->finalizeSubprogram` (before calling `verifyFunction`) instead.","metadata":{"transformedAt":"2026-08-18T18:32:17.815Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":23,"totalLines":467,"estimatedTokens":3787}}633{"id":"stack-2944357","source":"stackoverflow","questionId":2944357,"title":"Why call-by-value evaluation strategy is not Turing complete?","tags":["compiler-construction","programming-languages","computer-science","turing-complete"],"text":"Title: Why call-by-value evaluation strategy is not Turing complete?\nTags: compiler-construction, programming-languages, computer-science, turing-complete\nSource: Stack Overflow\n\nQuestion:\nI'm reading an article about different evaluation strategies (I linked article in wiki, but I'm reading another one not in English). And it says that unlike to `call-by-name` and `call-by-need` strategies, `call-by-value` strategy is *not* Turing complete.\n\nCan anybody explain, please, why is it so? If it's possible, add an example pls.\n\n========================================\n\nTop Answer:\nYour question doesn't make a lot of sense without reference to some specific language, but I'll try my best to answer with respect to the Untyped Lambda calculus.\n\nThe existence of a call-by-value fixed point combinator (i.e. \"Y combinator\") for the untyped lambda calculus seems to refute the basic claim (see: Fixed Point Combinator). The existence of such a combinator breaks strong normalization, which suggests that there is at least one language that is turing complete that uses a call-by-value evaluation strategy.\n\nMuch more likely to affect the turing-completeness of a language is the existence (or lack of) a type system. For example, the simply-typed lambda calculus cannot encode a fixed point combinator, and is strongly normalising (i.e. all well-typed terms reduce to a value), however, this is true irrespective of the evaluation strategy employed. Rather, it is a consequence of the type system.\n\n========================================\n\nCode:\n```text\ncall-by-name\n```\n\n```text\ncall-by-need\n```\n\n```text\ncall-by-value\n```\n\n========================================\n\nComments:\n- @KennyTM: I'm trying to find the source in references at the end of the article. I can give you a link if you'd like but it's in Russian.\n- A Russian article is better than nothing. Not me, but someone may be able to read Russian.\n- There are of course many languages that use call-by-value that are turing complete. I suspect that the article talks about a specific language that would not be turing complete if it used call-by-value (though I can't quite imagine what such a language would look like).\n- So called call-by-value languages are not totally call-by-value: they use special forms for control structures. I'm not aware of any language based on CBV lambda-calculus without special forms. Is there any?","metadata":{"transformedAt":"2026-08-18T18:32:17.815Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":40,"estimatedTokens":599}}634{"id":"stack-33560448","source":"stackoverflow","questionId":33560448,"title":"Why is vptr stored as the first entry in the memory of a class with virtual functions?","tags":["c++","performance","memory-layout","compiler-construction","vptr"],"text":"Title: Why is vptr stored as the first entry in the memory of a class with virtual functions?\nTags: c++, performance, memory-layout, compiler-construction, vptr\nSource: Stack Overflow\n\nQuestion:\nFor some compilers, if a class has virtual functions then its vptr can be accessed with the address of the first byte of its object. For instance,\n\n```\nclass Base{\npublic:\n virtual void f(){coutI know that it is dependent on different compiler behaviors. Since there is the case where vptr is stored as the very first entry, what is the advantage of doing this? Does that help improve performance or simply because it's easier to access vbtl using &b?\n\n========================================\n\nTop Answer:\nAlthough this is implementation defined there doesn't seem to be much of a real choice.\n\nFirst of all we can see that you ether have to have a `vptr` or an embedded `vtable`. The later means that you will have to copy the `vtable` on construction and it consumes more memory, but will have the advantage of avoiding one pointer dereference on each method call. There's probably good arguments for both of them depending on the situation - most implementations have chosen to lower construction time and overall memory consumption instead of saving dispatch time.\n\nWhen chosen `vptr` approach we see that we must keep binary compatibility of the layout of base and derived classes. First of all we can achieve this by (often) using one `vptr`, this `vptr` must for compatibility reasons live in the most basic class.\n\nWhen dealing with simple inheritance the most straight forward way of converting between derived to base class is to keep the pointer value which will mean that the layout has to be first the fields for the base class followed by the addition derived classes contributes to it.\n\nNow we're quite near the reason why to put the `vptr` first. It simply has to be near the beginning of the object as it has to live within the most basic part of the object.\n\nThen for the reason we put it on offset 0 may be that it's a consistent offset that's available for all classes. You simply has no guarantee that there's any data that could be placed before the `vptr`.\n\nPutting the `vptr` at offset 0 has some advantages too. If you know the object to have a `vptr` you know that you would have to look at offset 0 without needing to know the type of the object (more than it has a `vptr`). This can come handy for some debugging purposes (the `vtable` often contains enough information to deduce the actual type). Especially this makes the `typeid` and similar simpler to implement since you only have to look at the same offsets to retrieve the `type_info` node via predefined offsets - which means that you can the actual code for `typeid`.\n\n========================================\n\nCode:\n```text\nclass Base{\npublic:\n    virtual void f(){cout<<\"f()\"<<endl;};\n    virtual void g(){cout<<\"g()\"<<endl;};\n    virtual void h(){cout<<\"h()\"<<endl;};\n};\n\nint main()\n{   \n   Base b;\n\n   cout<<\"Address of vtbl:\"<<(int *)(&b)<<endl;\n\n   return 0;\n}\n```\n\n```text\nsizeof(void*)\n```\n\n```text\nvptr\n```\n\n```text\nvtable\n```\n\n```text\nvtable\n```\n\n```text\nvptr\n```\n\n```text\nvptr\n```\n\n```text\nvptr\n```\n\n```text\nvptr\n```\n\n```text\nvptr\n```\n\n```text\nvptr\n```\n\n```text\nvptr\n```\n\n```text\nvptr\n```\n\n```text\nvtable\n```\n\n```text\ntypeid\n```\n\n```text\ntype_info\n```\n\n```text\ntypeid\n```\n\n========================================\n\nComments:\n- Is the behaviour of \"vptr accessed by first byte of object\" vary on OS?\n- It would be implementation dependent.\n- *\"We know that if a class has virtual functions then its vptr can be accessed with the address of the first byte of its object\"*. No, we don't.\n- Counter-example: early versions of GCC (prior to 3.2 I think) stored the vptr at the end of the class (unless it inherited a vptr from a base class). The reason was that this way, the start of the object had the same layout as a C struct with the same fields.","metadata":{"transformedAt":"2026-08-18T18:32:17.815Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":121,"estimatedTokens":985}}635{"id":"stack-8016325","source":"stackoverflow","questionId":8016325,"title":"Prolog-based interpreter","tags":["prolog","interpreter","dcg","compiler-construction"],"text":"Title: Prolog-based interpreter\nTags: prolog, interpreter, dcg, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI've already gotten my feet wet with functional programming; I am familiar (though not proficient) in Haskell and PLT Scheme. I've used PLT Scheme to build little interpreters for toy languages (referencing PLAI)--I'm better with imperative languages.\n\nCould anyone direct me to resources I could use to build a small interpreter of a toy language of my choosing with Prolog?\n\n========================================\n\nTop Answer:\nMarkus Triska (here his homepage) show several things could be interesting to you: for instance a toy LISP, or some toughts to meta interpreters.\n\n========================================\n\nCode:\n```text\nassert/1\n```\n\n```text\nmaplist/\\[2-6\\]\n```\n\n```text\nassert/1\n```\n\n```text\ncompile_predicates/1\n```\n\n```text\n:- initialization(main).\n:- set_prolog_flag('double_quotes','chars').\n\nmain :- functional_syntax((\n            writeln(factorial(3)+factorial(4)),\n            Concatenated_string = \"hello\" + \" \" + \"world\",\n            writeln(Concatenated_string),\n            writeln(length(Concatenated_string)),\n            writeln(type(Concatenated_string)),\n            writeln(nth0(0,Concatenated_string)),\n            writeln(msort([1,3,2,15,-1]))\n        ),true).\n\nfactorial(N,Output) :-\n    functional_syntax((\n        (N=1 -> Output = 1);\n        Output = N*factorial(N-1)\n    )).\n\ntype(A,B) :-\n    functional_syntax(A,A1),\n    (number(A),B='number';\n    is_list(A),B='list';\n    atom(A),B='atom').\n\nfunctional_syntax(A) :- functional_syntax(A,true).\nfunctional_syntax(A,A) :- number(A);var(A);atom(A).\nfunctional_syntax(not(X),Output) :-\n    functional_syntax((X = false),Output).\nfunctional_syntax(writeln(A),true) :-\n    functional_syntax(A,A1),writeln(A1).\nfunctional_syntax(A+B,C) :-\n    functional_syntax([A,B],[A1,B1]),\n    ((number(A1),number(B1)) ->\n        C is A1+B1;\n    (is_list(A1),is_list(B1)) ->\n        append(A1,B1,C)).\nfunctional_syntax(A-B,C) :-\n    functional_syntax([A,B],[A1,B1]),C is A1-B1.\nfunctional_syntax(A*B,C) :-\n    functional_syntax([A,B],[A1,B1]),C is A1*B1.\nfunctional_syntax(A/B,C) :-\n    functional_syntax([A,B],[A1,B1]),C is A1/B1.\nfunctional_syntax(A=B,Result) :-\n    functional_syntax(B,B1),\n    (A=B1,Result=true;dif(A,B1),Result=false).\nfunctional_syntax(A->B,Result) :-\n    (functional_syntax(A,A1),A1=true) -> (functional_syntax(B,B1),Result=true,B1=true);\n    Result=false.\nfunctional_syntax([],[]).\nfunctional_syntax([A|B],[A1|B1]) :-\n    functional_syntax(A,A1),functional_syntax(B,B1).\nfunctional_syntax((A,B),Result) :-\n    functional_syntax([A,B],[A1,B1]),\n    (A1,B1,Result=true;([A1,B1]=[true,false];[A1,B1]=[false,true]),Result=false).\nfunctional_syntax((A;B),Result) :-\n    (functional_syntax(A,A1),call(A1);\n    functional_syntax(B,B1),call(B1)) -> (Result = true);\n    (functional_syntax(A,A1),A1=false,Result=false).\nfunctional_syntax(Input,Output1) :-\n    not(number(Input)),\n    Input =.. [Name|Params],\n    \\+member(Name,['=','->',not,'[|]',',',';',+,-,*,/]),\n    length(Params,Params_length),\n    Params_length > 0,\n    functional_syntax(Params,Params1),\n    append([Name|Params1],[Output1],Input0),\n    Input1 =.. Input0,\n    call(Input1).\n```\n\n========================================\n\nComments:\n- Would you like to create a language where you implement the runtime for it by some byte code, or do you aspire for some meta interpreter approach?\n- @Countably Infinite, I was going for a metainterpreter approach. The alternative you've mentioned seems a bit too much given my lack of experience.\n- And due to the nature of the internet this answer is dead, archive org link ... web.archive.org/web/20121221085328/http://&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.815Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":111,"estimatedTokens":936}}636{"id":"stack-52506603","source":"stackoverflow","questionId":52506603,"title":"What can a JIT compiler do that an AOT compiler cannot?","tags":["optimization","compiler-construction","compiler-optimization","jit"],"text":"Title: What can a JIT compiler do that an AOT compiler cannot?\nTags: optimization, compiler-construction, compiler-optimization, jit\nSource: Stack Overflow\n\nQuestion:\nA Just-in-Time (JIT) compiler can optimize a program based on runtime information that is unavailable to an Ahead-of-Time (AOT) compiler.\n\nThe most obvious example of this runtime information is the target platform, e.g. the exact CPU on which the program is running, or any accelerators such as GPUs that might be available. This is the sense in which OpenCL is JIT-compiled.\n\nBut suppose we do know ahead of time what the target platform is: we know which SIMD extensions will be available, etc. What other runtime information can a JIT-compiler exploit that is unavailable to an AOT-compiler?\n\nA HotSpot-style JIT-compiler will automatically optimize a program's hot spots... but can't an AOT-compiler just optimize the whole program, hot spots and all?\n\nI would like some examples of specific optimizations that a JIT-compiler can perform which an AOT-compiler cannot. Bonus points if you can provide any evidence for the effectiveness of such optimizations in \"real world\" scenarios.\n\n========================================\n\nTop Answer:\nWhat can a JIT compiler do that an AOT compiler cannot?\n\nIn theory; nothing, because the AOT compiler can insert a JIT compiler into the resulting code if it wants to (and/or can generate self-modifying code, generate 123 alternative versions and select which version to use based on run-time information, ...).\n\nIn practice; an AOT compiler is limited by how much complexity the compiler designer felt like dealing with, the language it's compiling, and how the compiler is used. For example, some compilers (Intel's ICC) will generate multiple versions of the code and (at run-time) decide which version to use based on which CPU it's running on but most compilers aren't designed to do this; lots of languages don't provide any way to control \"locality\" (and reduce the chance of TLB misses and cache misses); and often a compiler is used in a way that creates barriers that prevent optimisation (e.g. separate \"compilation units\"/object files that are linked together later, possibly including dynamic linking, where it's impossible for an AOT compiler to do whole program optimisation and only possible to optimise parts in isolation). All of these things are implementation details and not a restriction of AOT.\n\nIn other words; in practice \"AOT vs. JIT\" is a comparison of implementations and not a true comparison of \"AOT vs. JIT\" itself; and in practice AOT gives poor performance because of implementation details, and JIT gives sightly worse than poor performance because JIT itself is bad (expensive optimisations aren't viable at all because they're being done at run-time); and the only reason that JIT seems \"almost as good\" is that it's only \"almost as good as bad\".\n\n========================================\n\nComments:\n- Have you read this: stackoverflow.com/questions/2106380/&hellip;\n- @AnubhavSrivastava Thanks for that link. It is a similar question but neither the top-rated answer nor the accepted answer actually answer my question. There are a couple of examples of JIT-only optimizations in the other answers though: optimization across libraries and dynamic inlining with trace trees. I'd love to know how much difference those make in practice.\n- Reflection is the notorious problem, can't statically determine what type is needed from just a string.\n- If an AOT compiler inserts a JIT compiler into the resulting code, I think we can say the result is JIT compiled. The point of the question is to ask why it would do that - what can a JIT compiler do that the AOT compiler couldn't just do itself? Peter A. Schneider gave some examples in his answer that aren't merely implementation details.\n- @c--: If the AOT compiler actually inserts a JIT (for some parts of a program where it decided its beneficial) then it'd be practically indistinguishable from AOT compiled code that used other tricks to get the same benefits. In other words, it's still AOT compiled, and everything after that is splitting hairs over hypothetical implementation details. For Peter A. Schneider's answer I can find a single example that isn't wrong - an AOT compiler can do all of the things listed (without inserting a JIT into the AOT compiled code) and do it all more efficiently than using a JIT.\n- I'd love to know how an AOT can \"eliminate a test for a condition which can never occur ... based on the current values of parameters\", given that it doesn't know the \"current values of parameters\". The best an AOT can do is eliminate unreachable code based on constant propagation, surely?\n- @c--: I'd love to know how people can be stupid enough to believe \"test/s to determine if (based on current values) a test can be eliminated\" makes sense. Do they think you can decide if a test can/can't be eliminated without an additional equal or worse test? Do they realise the branch prediction in modern CPUs makes eliminating the test a futile exercise in destroying performance (even without the idiocy of \"test/s to potentially avoid a test\")?\n- @c--: A sufficiently advanced AOT can determine if a test is always unnecessary, but can also determine more complex patterns (simple example; a \"write once\" variables that cause a test to go from \"initially always fails\" to \"eventually always passes\", where the compiler can modify a function pointer when the write-once variable is set so that there's never a test). These things only depend on the complexity of the compiler, not delusional fantasies of JIT advocates that aren't smart enough to realise most of the performance of JIT comes from AOT compiled native code in libraries, etc.\n- There's no need to call people stupid or rant about \"delusional fantasies\". This question had two parts: (a) what can a JIT compiler do that an AOT compiler cannot; and (b) what evidence is there that the answers to (a) are actually effective in \"real world\" usage. It is valid to provide answers to (a) but concede that they rarely help in practice.\n- Thank you, this is exactly the sort of thing I was after.","metadata":{"transformedAt":"2026-08-18T18:32:17.815Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":39,"estimatedTokens":1544}}637{"id":"stack-3080213","source":"stackoverflow","questionId":3080213,"title":"Does a compiler have an assembler too?","tags":["compiler-construction","assembly"],"text":"Title: Does a compiler have an assembler too?\nTags: compiler-construction, assembly\nSource: Stack Overflow\n\nQuestion:\nMy understanding is that a compiler converts the high level language into machine code. I have a question as to whether a compiler(say VC++) in-turn uses an assembler too? I remember seeing assembly code, whenever there is a crash or something like that.\n\n========================================\n\nTop Answer:\nIn general, a compiler translates one language into another. Most compilers target machine code; here are the most common ways of doing this, in order from lowest-level to highest-level:\n\n- Produce machine code directly, by including back-ends for each instruction set.\n\n- Produce assembly language, which is passed to an external assembler. This is what gcc does. After all, why not use existing assemblers? Generating opcodes directly is a pain.\n\n- Generate a low-level intermediate form of the code, such as LLVM bytecode, which gets transformed into machine code. LLVM is a godsend for compiler-writers, because it handles a vast number of optimizations and supports a lot of back-ends, while letting you write at a higher level than assembly.\n\n- Transform the code into a language like C, and then pass it to another compiler. Facebook does this to PHP: they compile it to C++, and then use gcc to compile that to assembly, which is assembled to native code by GNU as.\n\nIf you'd like to try writing a compiler, I'd recommend starting off with something like C as the back-end. If you'd like to get more elaborate later, try LLVM. If you try to produce assembly language or machine code directly, it'll probably be less efficient than what a C compiler or LLVM would produce. I speak from embarrassing personal experience on this one.\n\n========================================\n\nCode:\n```text\ngcc -S -o test.S test.c\n```\n\n========================================\n\nComments:\n- Assembler is just a one-to-one mapping of machine instructions (bits of code) to some mnemonic codes (like `mov ax, bx`). That's why you can translate assembly codes to machine codes and back without any big effort. Compiler doing much more complicated work and there is no one-to-one relation between source code and produced code (no matter is it direct machine code or assembly instructions). That's why you usually can't see source code from binary code.\n- ony: that goes for a non symbolic, non macro backend assembler. But even GAS output is symbolic.\n- not correct.... I could program a compiler that outputs only assembly source, to be assembled apart with an assembler... moreover, compiler hardly goes so *directly* to machine code... internal representation (different from target machine cod) of the code (!) could be very useful to do optimization and so on... ok, everything likely is hidden, but \"*directly*\" is too much anyway! :D\n- You could but to answer the OP's question Visual Studio doesn't output assembler and then call MASM to generate the exe\n- Well, actually, mainly GCC does that :-) Most commercial ones have it buildin to avoid the slow generating/reparsing text step, with only an option to generate assembler if needed.","metadata":{"transformedAt":"2026-08-18T18:32:17.815Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":37,"estimatedTokens":790}}638{"id":"stack-1173780","source":"stackoverflow","questionId":1173780,"title":"Programming language and compiler","tags":["compiler-construction","programming-languages"],"text":"Title: Programming language and compiler\nTags: compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nI was having this discussion with my friend and realized this might be the best place to ask this question ?\n\nHow is a new language born ? This new language NEW must be written in some old language OLD (eg C++ was written in C in initial stages), or how is this created ??\nAnd, how does this language NEW can work itself if there is no compiler for it ? So, there must be some compiler for it ? Who writes compiler for it ?\n\nSo, how does all this work together, new language and its compiler, relation of new language with its old base language ?\n\n========================================\n\nTop Answer:\n**Bootstrapping** is a term used in computer science to describe the techniques involved in writing a compiler (or assembler) in the target programming language which it is intended to compile. This technique is also called self-hosting.\n\n========================================\n\nComments:\n- Thanks to all for such quick replies. A course in compiler design is what I need, I guess. But, I was wondering/curious how you all can reply so fast, I mean within 1-2 minutes, which is very amazing ?\n- We're all dying for reputation points. Upvote us, please! :-)\n- @Norman : I am new to SO and don't know much about reputations. Have to check out all that when I am free. I guess you are happy now. :)\n- possible duplicate of How was the first compiler written?, when-someone-writes-a-new-language-what-do-they-write-it-in\n- Yup that's it in a nutshell. There are those that don't believe that it's a real language unless it can be used to write it's own compiler :)\n- To add to Scyllinice's answer: Not all languages can go this route, of course - a lot of LISP variations are pure interpreters without the ability to create an executable per se, and a LISP compiler can be written in pretty much any language. The OLD and NEW languages don't really have to have any real connection to each other.\n- though, you don't necessarily have to migrate off of the original implementation language. For example, Tcl and many (most?) scripting languages use C or C++ implementation language long after the new language has become mature and stable.\n- I don't agree with the statement \"the organizational skills required to write a working compiler are immense\". Back when I was just a handful of years out of college and never having taking a compiler class, I was able to create a special purpose language using lex and yacc. While difficult, it was far from being immensely difficult. It was actually quite rewarding.\n- @Bryan: It depends. With modern compiler-generation tools it can be reasonably easy to create a small language (like many domain specific languages), but @Sprague is assuming something meatier. Once you add in all the optimization, code generation, etc. needed for a major programming language like Python, Java, or C# the work can get very demanding. Then there are ancillary tasks like VM design, GC algorithms, standard libraries, ...","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":29,"estimatedTokens":769}}639{"id":"stack-9977530","source":"stackoverflow","questionId":9977530,"title":"Default parameters and reflection: if ParameterInfo.IsOptional then is DefaultValue always reliable?","tags":[".net","reflection","compiler-construction"],"text":"Title: Default parameters and reflection: if ParameterInfo.IsOptional then is DefaultValue always reliable?\nTags: .net, reflection, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm looking at how `ParameterInfo.IsOptional` is defined (I'm adding default parameter support to an internal IOC framework), and it seems to me that, when true, there is no guarantee that `ParameterInfo.DefaultValue` (or indeed `ParameterInfo.RawDefaultValue`) are *actually* the default values that are to be applied.\n\nIf you look at the MSDN example given for `IsOptional`, it seems possible in IL to define a parameter that is optional but for which no default is supplied (given that the `ParameterAttributes.HasDefault` must be explicitly supplied). I.e. potentially leading to a situation that a parameter type is, say, `Int32`, `ParameterInfo.IsOptional` is true, but `ParameterInfo.DefaultValue` is null.\n\nMy language is C#, therefore I can work on what *that* compiler will do. Based on that I can have a simple test as follows (`parameter` here is a `ParameterInfo` instance, and the method is meant to return an instance to be used as the runtime argument for the parameter):\n\n```\nif(no_config_value)\n{\n if(!parameter.IsOptional) throw new InvalidOperationException();\n //it's optional, so read the Default\n return parameter.DefaultValue;\n}\nelse\n return current_method_for_getting_value();\n```\n\nBut I'm thinking that some languages (and I want to get this right at the IL-level, rather than just based on what one particular compiler does) can place the onus on the *caller* to determine the default value to be used, if so, a `default(parameter.ParameterType)` would need to be in order.\n\nThis is where it gets a little more interesting, because `DefaultValue` is, apparently `DBNull.Value` (according to the documentation for `RawValue`) if there is no default. Which is no good if the parameter is of type `object` and `IsOptional==true`!\n\nHaving done a bit more digging, I'm hopeful that the reliable way to solve this is to physically read the `ParameterInfo.Attributes` member, reading the bitflags individually **first** to check for `ParameterAttributes.Optional` and *then* check for `ParameterAttributes.Default`. Only if ***both*** are present, then reading `ParameterInfo.DefaultValue` will be correct.\n\nI'm going to start coding and writing tests around this, but I'm asking in the hope that there's someone with more IL knowledge that can confirm my suspicions and hopefully confirm that this'll be correct for any IL-based language (thus avoiding the need to mock up loads of libraries in different languages!).\n\n========================================\n\nTop Answer:\nAs you stated, there is a difference and is not reliable. Well, .NET 4.5 has HasDefaultValue, which checks if a parameter is optional (`IsOptional`) as well has a default value (`DefaultValue`) - same as\n\n```\n(p.Attributes & ParameterAttributes.HasDefault) == ParameterAttributes.HasDefault\n```\n\nin versions earlier. That should be the correct approach. Another approach is replacing the *invalid* default value depending on what the *invalid value* is in such cases (when parameter is not optional and when parameter is optional but without default value). For eg, you could just do:\n\n```\nif(p.DefaultValue != DBNull.Value)\n{\n if(p.DefaultValue != Type.Missing)\n return p.DefaultValue; //use the supplied default\n else\n return FastDefault(p.ParameterType); //use the FastDefault method\n}\nelse //parameter requires an argument - throw an exception\n throw new InvalidOperationException(\"Parameter requires an argument\");\n```\n\nThis works because `p.DefaultValue` is `DBNull` when parameter is not optional and `Type.Missing` when optional parameter but not supplied with default value. \n\nSince this is undocumented, I dont recommend it. Better would be to replace `p.DefaultValue != DBNull.Value` with `p.IsOptional`. Even better would be to replace `p.DefaultValue != Type.Missing` with what you already answered: `(p.Attributes & ParameterAttributes.HasDefault) == ParameterAttributes.HasDefault`\n\n========================================\n\nCode:\n```text\nif(no_config_value)\n{\n  if(!parameter.IsOptional) throw new InvalidOperationException();\n  //it's optional, so read the Default\n  return parameter.DefaultValue;\n}\nelse\n  return current_method_for_getting_value();\n```\n\n```text\nParameterInfo.IsOptional\n```\n\n```text\nParameterInfo.DefaultValue\n```\n\n```text\nParameterInfo.RawDefaultValue\n```\n\n```text\nIsOptional\n```\n\n```text\nParameterAttributes.HasDefault\n```\n\n```text\nInt32\n```\n\n```text\nParameterInfo.IsOptional\n```\n\n```text\nParameterInfo.DefaultValue\n```\n\n```text\nparameter\n```\n\n```text\nParameterInfo\n```\n\n```text\ndefault(parameter.ParameterType)\n```\n\n```text\nDefaultValue\n```\n\n```text\nDBNull.Value\n```\n\n```text\nRawValue\n```\n\n```text\nobject\n```\n\n```text\nIsOptional==true\n```\n\n```text\nParameterInfo.Attributes\n```\n\n```text\nParameterAttributes.Optional\n```\n\n```text\nParameterAttributes.Default\n```\n\n```text\nParameterInfo.DefaultValue\n```\n\n```text\n/* wrapper around a generic FastDefault<T>() that returns default(T) */\npublic object FastDefault(Type t) { /*elided*/ }\n```\n\n```text\npublic object GetParameterValue(ParameterInfo p, IDictionary<string, object> args)\n{\n  /* null checks on p and args elided - args can be empty though */\n  object argValue = null;\n  if(args.TryGetValue(p.Name, out argValue))\n    return argValue;\n  else if(p.IsOptional)\n  {\n    //now check to see if a default is supplied in the IL with the method\n    if((p.Attributes & ParameterAttributes.HasDefault) == \n        ParameterAttributes.HasDefault)\n      return p.DefaultValue;  //use the supplied default\n    else\n      return FastDefault(p.ParameterType); //use the FastDefault method\n  }\n  else  //parameter requires an argument - throw an exception\n    throw new InvalidOperationException(\"Parameter requires an argument\");\n}\n```\n\n```text\npublic class Test\n{\n  public readonly string Message;\n  public Test(string message = \"hello\") { Message = message; }\n}\n```\n\n```text\npublic class Test2\n{\n  public readonly string Message;\n  public Test2([OptionalAttribute]string message) { Message = message; }\n}\n```\n\n```text\nIsOptional\n```\n\n```text\nDefaultValue\n```\n\n```text\nAttributes\n```\n\n```text\nParameterInfo.DefaultValue\n```\n\n```text\nOptionalAttribute\n```\n\n```text\nFastDefault\n```\n\n```text\n(p.Attributes & ParameterAttributes.HasDefault) == ParameterAttributes.HasDefault\n```\n\n```text\nif(p.DefaultValue != DBNull.Value)\n{\n    if(p.DefaultValue != Type.Missing)\n        return p.DefaultValue;  //use the supplied default\n    else\n        return FastDefault(p.ParameterType); //use the FastDefault method\n}\nelse  //parameter requires an argument - throw an exception\n    throw new InvalidOperationException(\"Parameter requires an argument\");\n```\n\n```text\nIsOptional\n```\n\n```text\nDefaultValue\n```\n\n```text\np.DefaultValue\n```\n\n```text\nDBNull\n```\n\n```text\nType.Missing\n```\n\n```text\np.DefaultValue != DBNull.Value\n```\n\n```text\np.IsOptional\n```\n\n```text\np.DefaultValue != Type.Missing\n```\n\n```text\n(p.Attributes & ParameterAttributes.HasDefault) == ParameterAttributes.HasDefault\n```\n\n========================================\n\nComments:\n- I am going to test that `Test2` one!\n- Well not all languages require that you specify a default value in the function signature when declaring an optional parameter. I'm no expert, but it appears that in the case of F#, default values are applied within the function body - the benefit of which being that you can keep defaults consistent as you deploy new versions of assemblies (whereas with the C# and VB model, you can compile a library with a different default today, but assemblies compiled against yesterday's version will still send the old default). The F# Docs are here\n- ...So the F# example gives us a good pattern for why you'd use the `[Optional]` pattern instead of `int p1 = 0` in C#: possibly because you always expect the default to be the default for the type, and that you're then going to apply another default in the function body anyway.\n- By the way, `HasDefaultValue` crashes if the parameter is of type `System.DateTime`. This seems to be a bug in the .NET implementation. See github.com/dotnet/corefx/issues/12338\n- That's interesting @pierre - I'm using code just like this in my IOC container, Rezolver (on github, Dev branch is most up to date at the mo), and hadn't noticed that bug yet. I'll be sure to try and replicate so I can have the case covered for when they fix it :)","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":42,"totalLines":277,"estimatedTokens":2125}}640{"id":"stack-4688342","source":"stackoverflow","questionId":4688342,"title":"Concept of \"dead code stripping\" in compiler optimization options","tags":["compiler-construction"],"text":"Title: Concept of \"dead code stripping\" in compiler optimization options\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nWhen i gone through my compiler(starcore,scc) optimization options, i can see one \"dead code stripping\".\n\nWhat actually does when this option is enabled?\n\n/Kanu\n\n========================================\n\nTop Answer:\nDead-code stripping is the process of removing unreferenced code from the executable file. If the code is unreferenced, it must not be used and therefore is not needed in the executable file. Removing dead code reduces the size of your executable and can help reduce paging.\n\n========================================\n\nComments:\n- at least that is according to code analysis which can not be performed on complex code ( self modifying for example)\n- Self-modifying C code is just asking for all kinds of trouble anyway... :)","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":219}}641{"id":"stack-31389295","source":"stackoverflow","questionId":31389295,"title":"How does the java compiler know of inherited methods?","tags":["java","inheritance","compiler-construction"],"text":"Title: How does the java compiler know of inherited methods?\nTags: java, inheritance, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nWe use inheritance in Java to abstract out similar behavior in a superclass and let all sub classes inherit it. One of the advantages of this is that , we now have only one copy of the method to maintain (i.e in the superclass).\n\n```\nClass Animal\n{\n public void makeNoise()\n {\n\n }\n\n public void sleep()\n {\n\n } \n} \n\nClass Cat extends Animal\n{\n // Override the makeNoise method\n public void makeNoise()\n {\n\n }\n}\n\nClass someClass\n{\n public static void main(String args[])\n {\n Cat fluffy = new Cat();\n\n fluffy.sleep();\n }\n}\n```\n\nI am trying to understand how the Java compiler knows of the `sleep()` method for a `Cat` type reference. There can't be a copy of the method in the `Cat` subclass (it defeats the purpose of having it in the superclass and letting all subclasses inherit from it). Is this information stored in some other place ?\n\n========================================\n\nTop Answer:\nIn case of interface we are able to call the Object class methods on them without interface extending Object.\nFor example:-\n\n```\npublic interface Test { \n}\n\npublic class MyClass extends Object implements Test {\n\npublic static void main() {\nTest test = new MyClass();\ntest.hashCode();\n// You can call Object Class methods on Test interface and Test interface\n//does not extends Object.\n\n}\n```\n\n//In Java Specification 9.2:- \nIf an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless an abstract method\nwith the same signature, same return type, and a compatible throws clause is\nexplicitly declared by the interface.\nIt is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.\n\n========================================\n\nCode:\n```text\nClass Animal\n{\n   public void makeNoise()\n   {\n\n   }\n\n   public void sleep()\n   {\n\n   }   \n} \n\nClass Cat extends Animal\n{\n     // Override the makeNoise method\n     public void makeNoise()\n     {\n\n     }\n}\n\nClass someClass\n{\n     public static void main(String args[])\n     {\n          Cat fluffy = new Cat();\n\n          fluffy.sleep();\n     }\n}\n```\n\n```text\nsleep()\n```\n\n```text\nCat\n```\n\n```text\nCat\n```\n\n```text\nfluffy.sleep()\n```\n\n```text\nCat\n```\n\n```text\nsleep\n```\n\n```text\nAnimal\n```\n\n```text\nAnimal\n```\n\n```text\npublic interface Test { \n}\n\npublic class MyClass extends Object implements Test {\n\npublic static void main() {\nTest test = new MyClass();\ntest.hashCode();\n// You can call Object Class methods on Test interface and Test interface\n//does not extends Object.\n\n}\n```\n\n========================================\n\nComments:\n- When it compiles the code , it creates some sort of a class heirarchy/ data structure . Right ?\n- @Twister Yes. And when objects are created at runtime, they (or at least the runtime VM) store what types they are.\n- i am talking about compile time not runtime.\n- @Twister Yeah, I just answered the compile-time question with \"yes\". I can expand on that by saying that when compilers look at the code, they do form trees of types that the code defines, so that when the rest of the code is compiled, it knows what all of the types are. Otherwise, it wouldn't know what is an undefined reference and what isn't.","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":161,"estimatedTokens":883}}642{"id":"stack-25548864","source":"stackoverflow","questionId":25548864,"title":"In C++, how does strict bottom-up analysis imply that the return type is not used in overloading resolution?","tags":["c++","compiler-construction","compilation"],"text":"Title: In C++, how does strict bottom-up analysis imply that the return type is not used in overloading resolution?\nTags: c++, compiler-construction, compilation\nSource: Stack Overflow\n\nQuestion:\nIn Bjarne's book, he said,\n\n The insistence on strict bottom-up analysis implies that the return type is not used in overloading resolution.\n\nIt looks like that \"bottom-up analysis\" has sth to do with how the compiler parse the C++ codes. \n\nWhat does he mean by saying this?\n\nRegards.\n\n========================================\n\nCode:\n```text\nint f();\nfloat f();\n\nvoid g(float);\n\n// Even though g() accepts only float, bottom-up analysis implies that \n// this information is not available during resolution of f().\ng(f());\n```\n\n```text\ng(f())\n```\n\n```text\nf()\n```\n\n```text\ng()\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":40,"estimatedTokens":194}}643{"id":"stack-24174458","source":"stackoverflow","questionId":24174458,"title":"LR(1) parser state size still an issue?","tags":["parsing","compiler-construction","grammar","lalr","lr-grammar"],"text":"Title: LR(1) parser state size still an issue?\nTags: parsing, compiler-construction, grammar, lalr, lr-grammar\nSource: Stack Overflow\n\nQuestion:\nHistorically, LALR(1) parsers were preferred over LR(1) parsers because of resource requirements required by the large number of states generated by LR(1) parsers. It's hard to believe that this continues to be an issue in today's computing environment. Is this still the case or are modern compilers now built with canonical LR parsers, since LALR grammars are a proper subset of LR grammars?\n\n========================================\n\nTop Answer:\nMinimal LR(1) parsers solve this problem. Dr Pager was the first one to write a paper on how to do this in 1977. Minimal LR(1) parser have all the power of canonical LR(1) parsers, recognizing the same language defined by an LR(1) grammar. However, minimal LR(1) parsers have parser tables almost as small as LALR(1) parser tables. \n\nThe trick required is to merge compatible states while building the canonical LR(1) state machine. This is complicated and the lookahead-set computation is just as complicated as it is for LALR(1). But the end result is beautitul. \n\nBTW, the LRSTAR Parser Generator creates minimal LR(1) and minimal LR(k) parsers, very powerful.\n\n========================================\n\nComments:\n- Still LALR(1) as far as I know. There's really no reason to pay the extra space cost, which is an order of magnitude or so, considering the tiny differences in the grammar classes.\n- Is the extra space cost an actual issue? True, the number of states is an order of magnitude greater, but the memory costs are puny compared to what they were in the past. Considering that it's actually easier to construct an LR algorithm than LALR, wouldn't the programmers time be a stronger consideration towards favoring LR?\n- The *relative* space cost is an issue. I was in class with Frank DeRemer (ahem) some *(many)* years ago and he was asked this question as futorology - what happens when memory becomes so big etc. - and he said it still wouldn't be worth it. He also said that using huge LR tables would be bad for cache coherency, so a performance issue.\n- Ok, in server environments supporting multiple clients that especially makes sense. Thanks.\n- Unless you're talking about a NFA regex based parser, you absolutely don't have to worry about states. Any other parser that uses too much resources is either not handwritten or the format is naturally complex.\n- @LeopoldAsperger You're confusing parsers with scanners, but they both have states, and parsers typically many more of them. Your remark about 'either not handwritten or the format is naturally complex' is basically meaningless, and in any case the question is about generated parsers.\n- @ESP I'm not confusing anything. You may say that a scanner and a parser are always separate programs, but in my experience most real implementations have both characteristics at the same time, and that is how it should be to reach optimal performance. But I'm not going to enter a parser/lexer/scanner discussion.\n- @EJP is trying to clarify the issue around Generated states from parsers. There is no implication about handwritten anything. LR(1) parsers generate an order of magnitude more states than LALR(1) parsers. So yes, the number of states generated IS the central issue around the question.\n- @LeopoldAsperger When you talk about 'a NFA regex based parser' you are indeed confusing parsers with scanners. Scanners use regular expressions and NFAs and DFAs. Parsers use DPDAs. Putting irrelevant nonsense into my mouth about how 'a scanner and a parser are always separate programs' doesn't constitute rational argument.\n- Yes, we already know there are lots and lots of states generated by LR(1) parsers. The point of the question is whether that's a practical issue today. Modern laptops and computers have upwards of 8GB of memory and more. And they are really fast. So time and space shouldn't be an issue in a single user environment. That's what prompted my question in the first place. Server environments, on the other hand, have to resources with lots of users, making it more compelling to be efficient with resources and therefore to implement LALR, instead.\n- @tgonelli Why would we be running compilers in server environments?\n- @tgoneil I've updated my answer to address your question. Can you review it and decide whether this helps?\n- @templatetypedef, thank you for the tip on alternative parsing algorithms like IELR and GLR that I can investigate.","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":32,"estimatedTokens":1135}}644{"id":"stack-20313122","source":"stackoverflow","questionId":20313122,"title":"Generating register based bytecode from an abstract syntax tree?","tags":["compiler-construction","code-generation","bytecode","abstract-syntax-tree"],"text":"Title: Generating register based bytecode from an abstract syntax tree?\nTags: compiler-construction, code-generation, bytecode, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nWhat are some well known strategies for generating register based bytecode from a given anstract syntax tree (AST)?\n\nConsider this expression `1 + 2 - 3 * 4 / 5` and its AST form:\n\n```\nbin_exp(-)\n bin_exp(+)\n num_exp(1)\n num_exp(2)\n bin_exp(/)\n bin_exp(*)\n num_exp(3)\n num_exp(4)\n num_exp(5)\n```\n\nI'm struggling to convert the AST into its corresponding bytecode procedurally.\nSo far I have only found one article, in which it only briefly talks about it. My interpretation of what it's trying to say...\n\n```\nint ridx; // register index\n\nfunction visit_exp(exp)\n{\n switch (exp)\n {\n case bin_exp:\n visit_exp(exp.left);\n visit_exp(exp.right);\n\n printf(\"add %i, %i -> %i\\n\", ridx - 2, ridx - 1, ridx);\n\n // save ridx, as it contains the result\n break;\n case num_exp:\n printf(\"mov %i -> %i\\n\", ridx, exp.value);\n break;\n }\n}\n```\n\nPlease give me a hand, thanks.\n\n========================================\n\nCode:\n```text\nbin_exp(-)\n    bin_exp(+)\n        num_exp(1)\n        num_exp(2)\n    bin_exp(/)\n        bin_exp(*)\n            num_exp(3)\n            num_exp(4)\n        num_exp(5)\n```\n\n```text\nint ridx; // register index\n\nfunction visit_exp(exp)\n{\n    switch (exp)\n    {\n        case bin_exp:\n            visit_exp(exp.left);\n            visit_exp(exp.right);\n\n            printf(\"add %i, %i -> %i\\n\", ridx - 2, ridx - 1, ridx);\n\n            // save ridx, as it contains the result\n                    break;\n        case num_exp:\n            printf(\"mov %i -> %i\\n\", ridx, exp.value);\n            break;\n    }\n}\n```\n\n```text\n1 + 2 - 3 * 4 / 5\n```\n\n========================================\n\nComments:\n- What's so hard about this? If you don't care about optimization, it's trivial.","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":93,"estimatedTokens":466}}645{"id":"stack-23329441","source":"stackoverflow","questionId":23329441,"title":"Implementing a lexer identifying tokens after creating DFA","tags":["compiler-construction","lexer"],"text":"Title: Implementing a lexer identifying tokens after creating DFA\nTags: compiler-construction, lexer\nSource: Stack Overflow\n\nQuestion:\nI want to understand something about implementing a lexer, and I dont want to use scanner generators. From what I have read is that I identify the specification of the language with regular expressions each for different token. Then I should make a big regular expression ORing all of the token's expressions, right?! Then create NFA then DFA of this big regular expression, right?! If so then when a word get matched by the final DFA how will I know which token this word represents?!\n\n========================================\n\nTop Answer:\nWhat you're describing here is implementing a generated scanner by hand. That's not how you do it. Just write a loop containing a large switch statement whose cases are the initial letters of each token type, and each case is a loop to consume the rest of the token and return its type. The whitespace case is identical except that it doesn't return. The case for identifiers also needs to lookup a keyword table.\n\n========================================\n\nCode:\n```text\n_tokens = [\n    (re.compile('\\\\s+'), WhiteSpace),\n    (re.compile('[a-zA-Z_][a-zA-Z0-9_]*'), Identifier),\n    (re.compile('[0-9]+'), Integer),\n]\n```\n\n```text\n-1\n```\n\n```text\ntoken_type tokens[N_STATES];\n```\n\n```text\ntoken = tokens[current_state]\n```\n\n========================================\n\nComments:\n- once a word has been matched, you know where you started, and you know where you ended, right?\n- I think I am going to use the transition table, though I have a question abut keywords, In my DFA I dont have states for keywords so I am wondering if I finished in an identifier state should I check if it is a keyword by having all keywords in a table and then looping to find the variable in the table or create a regex that have all the keywords ORed, which shall be faster?\n- @Doggynub If you want to use a DFA, you need to mark each accepting state with the token type. That's not quite the same as the textbook DFA, but it's not actually that difficult. In the original patterns, each accepting state was distinct, so there's no problem; in the closure construction when you combine two groups of states including two different accepting states, you just keep the one with the smaller number (assuming you numbered the patterns in order). During the scan, you also need to remember the last accepting state's position and token type, because you might need to fallback.","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":41,"estimatedTokens":631}}646{"id":"stack-2360012","source":"stackoverflow","questionId":2360012,"title":"Generating Assembly For an x86 Processor","tags":["compiler-construction","assembly","code-generation","x86"],"text":"Title: Generating Assembly For an x86 Processor\nTags: compiler-construction, assembly, code-generation, x86\nSource: Stack Overflow\n\nQuestion:\nI'm currently working my way through Andrew Appel's Modern Compiler Implementation in Java, and I'm right around the point where I build the low-level intermediate representation.\n\nInitially, I had decided to target the JVM and ignore all of the low-level machine stuff, but in the interest of learning things that I don't know much about I've had a change of heart. This changes my IR, because targeting the JVM allows me to (more or less) wave my hands at making a method call or constructing an object.\n\nThe Appel book doesn't go into detail about any specific machine architecture, so I'd like to know where I can find out everything I need to know to go farther.\n\nThe things that I'm currently aware that I need to know are:\n\nWhich instruction set to use. I have two laptops I could develop on; both have Core 2 Duo processors. My current understanding is that x86 processors mostly use the same instruction set, but they are not all exactly the same. \n\nWhether the operating system affects the code generation step of compilation, or whether it is completely dependent on the processor. For example, I know something is different about generating code to run on a 32-bit vs. a 64-bit platform.\n\nHow stack frames and such are organized. When to use registers vs. putting parameters on the stack, caller-save vs. callee-save, all of that. I'd have thought that this would be described along with the instruction set but so far I haven't seen this particular info anywhere. Maybe I'm misunderstanding something here?\n\nLinks to resources in lieu of answers are perfectly welcomed.\n\n========================================\n\nTop Answer:\nHow stack frames and such are\n organized. When to use registers vs.\n putting parameters on the stack,\n caller-save vs. callee-save, all of\n that. I'd have thought that this would\n be described along with the\n instruction set but so far I haven't\n seen this particular info anywhere.\n Maybe I'm misunderstanding something\n here?\n\nIn general, there are no right answers to these questions. You can use whatever calling conventions you want...unless you want to interoperate with other people's code. For interoperability, compilers standardize on Application Binary Interfaces. My understanding is that the Itanium C++ ABI has become a popular standard in recent years. Try starting there.\n\n========================================\n\nCode:\n```text\nrax\n```\n\n```text\nwrite\n```\n\n```text\nrdi\n```\n\n```text\nrsi\n```\n\n```text\nrdx\n```\n\n```text\nsyscall\n```\n\n```text\nwrite\n```\n\n```text\nrax\n```\n\n```text\nwrite\n```\n\n```text\nrdi\n```\n\n```text\nrsi\n```\n\n```text\nrdx\n```\n\n```text\nr10\n```\n\n```text\nr8\n```\n\n```text\nr9\n```\n\n```text\nrax\n```\n\n```text\nrdi\n```\n\n```text\nrsi\n```\n\n```text\nrdx\n```\n\n```text\nrcx\n```\n\n```text\nr8\n```\n\n```text\nr9\n```\n\n```text\nrcx\n```\n\n```text\nr10\n```\n\n```text\nrax\n```\n\n```text\nrbp\n```\n\n```text\nrbx\n```\n\n```text\nr12\n```\n\n```text\nr15\n```\n\n========================================\n\nComments:\n- Voting to close as too broad.\n- Thanks, Nathan. I don't quite understand the purpose of the Itanium C++ ABI as it relates to my purposes (for example, what role does C++ play when developing a compiler for another language?); however, this link eventually led me to the various x86 calling conventions (cdecl, etc) which is what I was looking for.\n- Thanks, Michael - this answer was also very helpful. I wish I could accept it as well; my own fault for combining too many questions. +1, though.\n- Actually, reading this for the second time I think that this answers all of my questions most thoroughly.","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":29,"totalLines":163,"estimatedTokens":919}}647{"id":"stack-11488554","source":"stackoverflow","questionId":11488554,"title":"How exactly does the Scala implementation of <:<, <%<, =:= work in the compiler?","tags":["scala","compiler-construction","operators","type-equivalence"],"text":"Title: How exactly does the Scala implementation of <:<, <%<, =:= work in the compiler?\nTags: scala, compiler-construction, operators, type-equivalence\nSource: Stack Overflow\n\nQuestion:\nI see now that there's a related question asking what these operators (What do But I'm still confused about their implementation. In particular, I assume that once you've placed an implicit parameter that asserts a particular relationship, then you can use variables as if they've automatically been cast properly, e.g. this will compile:\n\n```\nclass Foo[T](a: T) {\n def splitit(implicit ev: T But how does this actually work in the compiler? Is there some magic compiler support for these operators, and if not, what's the underlying mechanism that allows it to infer this sort of relationship from the definition? (Was this mechanism added specifically to allow these operators to work, and how specific is it to these particular operators?) It seems a little magical that you can place an extra implicit parameter like this which somehow changes the compiler's interpretation of a type.\n\n========================================\n\nCode:\n```text\nclass Foo[T](a: T) {\n  def splitit(implicit ev: T <:< String) = a split \" \"\n}\n```\n\n```text\nimplicit def conforms[A]: A <:< A\n```\n\n```text\nsealed abstract class <:<[-From, +To]\n```\n\n```text\nimplicit def conformsOrViewsAs[A <% B, B]: A <%< B\n```\n\n```text\nPredef\n```\n\n```text\nA <:< A\n```\n\n```text\nA\n```\n\n```text\nT <:< String\n```\n\n```text\nconforms[T]\n```\n\n```text\nT\n```\n\n```text\nNothing\n```\n\n```text\nNothing <:< Nothing\n```\n\n```text\n<:<\n```\n\n```text\nFrom\n```\n\n```text\nTo\n```\n\n```text\nNothing <:< Nothing\n```\n\n```text\nNothing <:< String\n```\n\n```text\nNothing\n```\n\n```text\nString\n```\n\n```text\nString <:< String\n```\n\n```text\nNothing <:< String\n```\n\n```text\nString\n```\n\n```text\nNothing\n```\n\n```text\nString\n```\n\n```text\n<:<\n```\n\n```text\n=>\n```\n\n```text\nFunction1\n```\n\n```text\nT\n```\n\n```text\nString\n```\n\n```text\n=:=\n```\n\n```text\n<%<\n```\n\n```text\n<:<\n```\n\n========================================\n\nComments:\n- The source for them github.com/scala/scala/blob/v2.9.2/src/library/scala/&hellip;\n- Thanks! The following is the key to explaining how the compiler lets you call String methods (or whatever): *You can call methods of String on it because aka Function1 and serves as an implicit conversion from T to String, which basically ends up doing a safe cast.*","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":32,"totalLines":149,"estimatedTokens":595}}648{"id":"stack-2423728","source":"stackoverflow","questionId":2423728,"title":"Compiling the compiler - how many times?","tags":["language-agnostic","compiler-construction","compilation"],"text":"Title: Compiling the compiler - how many times?\nTags: language-agnostic, compiler-construction, compilation\nSource: Stack Overflow\n\nQuestion:\nIf you compile a new version of compiler, how many times should you recompile it iteratively?\n\nFirst: compile the new version of compiler[1] using the old version[0].\n\nSecond: compile the new version[2] using the newly compiled one[1], to apply new optimizations and fix bugs to the binary, not present in old[0] compiler.\n\nNow third? Compile again, using [2] to remove any potential bugs that could have resulted from bugs in [1] as result of faulty compilation by buggy [0]...?\n\n...but if you're paranoid, maybe the bugs introduced by [0] into [1] caused [2] to be somewhat buggy too...?\n\nDoes going beyond [2] make sense at all?\n\n========================================\n\nTop Answer:\nThe instructions for bootstrapping OCaml invite you to continue until you have reached a fixpoint, *if you wish*. Assuming the new compiler only fixes bugs and does not introduce new ones, you may need to bootstrap several times to make self-inducing compiler bugs disappear.\n\nYou may also reach a fixpoint without the bug you were trying to fix being fixed, because the presence of the bug in the bootstrap compiler perpetuates it in the bootstrapped version (a sort of involuntary Ken Thompson). This happened at least once in the history of OCaml. When they noticed this, the developers fixed the assembly code by hand in order to obtain a correct compiler that translated correct source into a correct compiler.\n\n========================================\n\nCode:\n```text\nn\n```\n\n```text\nn - 1\n```\n\n========================================\n\nComments:\n- If this doesn't happen on the third version, there's probably a bug. The new compiler should produce the same output on own sources regardless of the version it was compiled with.\n- @Rafal: Agreed. @SF: Yes, comparisons would need to handle that sort of thing; not an uncommon requirement.","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":40,"estimatedTokens":493}}649{"id":"stack-504389","source":"stackoverflow","questionId":504389,"title":"How would you re-use C opcode implementations when writing a JIT with LLVM?","tags":["compiler-construction","jit","llvm","language-implementation"],"text":"Title: How would you re-use C opcode implementations when writing a JIT with LLVM?\nTags: compiler-construction, jit, llvm, language-implementation\nSource: Stack Overflow\n\nQuestion:\nIn the llvm tutorials and examples, the compiler outputs LLVM IR by making calls like this\n\n```\nreturn Builder.CreateAdd(L, R, \"addtmp\");\n```\n\nbut many interpreters are written like this:\n\n```\nswitch (opcode) {\n case ADD:\n result = L + R;\n break;\n ...\n```\n\nHow would you extract each of these code snippets to make a JIT with LLVM without having to re-implement each opcode in LLVM IR?\n\n========================================\n\nCode:\n```text\nreturn Builder.CreateAdd(L, R, \"addtmp\");\n```\n\n```text\nswitch (opcode) {\n     case ADD:\n             result = L + R;\n             break;\n     ...\n```\n\n```text\nvoid addOpcode(uint32_t *result, uint32_t L, uint32_t R) {\n    *result = L + R;\n}\n\nswitch (opcode) {\n    case ADD:\n            addOpcode(&result, L, R);\n            break;\n     ....\n```\n\n========================================\n\nComments:\n- Wonderful! I thought it would be something like that, with LLVM inlining all the functions.\n- How does this compare to a call-threaded interpreter where you only JIT a series of CALL instructions to each bytecode implementation, inline the implementation of only a few opcodes most likely BRANCH opcodes, and each opcode implementation ends with RET?\n- I don't quite this. Are you saying to pass all the opcode functions into LLVM, and when you output it back to C, it will automatically have a JIT built in?\n- You are not outputing it as C, you are outputing C++ code that instantiates the in memory byte code compiled representation of the function.","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":57,"estimatedTokens":419}}650{"id":"stack-4697359","source":"stackoverflow","questionId":4697359,"title":"Just in Time compilation always faster?","tags":["jit","software-design","compiler-construction","runtime-environment"],"text":"Title: Just in Time compilation always faster?\nTags: jit, software-design, compiler-construction, runtime-environment\nSource: Stack Overflow\n\nQuestion:\nGreetings to all the compiler designers here on Stack Overflow.\n\nI am currently working on a project, which focuses on developing a new scripting language for use with high-performance computing. The source code is first compiled into a byte code representation. The byte code is then loaded by the runtime, which performs aggressive (and possibly time consuming) optimizations on it (which go much further, than what even most \"ahead-of-time\" compilers do, after all that's the whole point in the project). Keep in mind the result of this process is still byte code.\n\nThe byte code is then run on a virtual machine. Currently, this virtual machine is implemented using a straight-forward jump table and a message pump. The virtual machine runs over the byte code with a pointer, loads the instruction under the pointer, looks up an instruction handler in the jump table and jumps into it. The instruction handler carries out the appropriate actions and finally returns control to the message loop. The virtual machine's instruction pointer is incremented and the whole process starts over again. The performance I am able to achieve with this approach is actually quite amazing. Of course, the code of the actual instruction handlers is again fine-tuned by hand.\n\nNow most \"professional\" run-time environments (like Java, .NET, etc.) use Just-in-Time compilation to translate the byte code into native code before execution. A VM using a JIT does usually have much better performance than a byte code interpreter. Now the question is, since all an interpreter basically does is load an instruction and look up a jump target in a jump table (remember the instruction handler itself is statically compiled into the interpreter, so it is already native code), will the use of Just-in-Time compilation result in a performance gain or will it actually *degrade* performance? I cannot really imagine the jump table of the interpreter to degrade performance *that* much to make up the time that was spent on compiling that code using a JITer. I understand that a JITer can perform additional optimization on the code, but in my case very aggressive optimization is already performed on the byte code level prior to execution. Do you think I could gain more speed by replacing the interpreter by a JIT compiler? If so, why?\n\nI understand that implementing both approaches and benchmarking will provide the most accurate answer to this question, but it might not be worth the time if there is a clear-cut answer.\n\nThanks.\n\n========================================\n\nTop Answer:\nJIT can theoretically optimize better, since it has information not available at compile time (especially about typical runtime behavior). So it can for example do better branch prediction, roll out loops as needed, et.c.\n\nI am sure your jumptable approach is OK, but I still think it would perform rather poor compared to straight C code, don't you think?\n\n========================================\n\nComments:\n- If `very aggressive optimization` is done before compiling I doubt it. Depends on how good your optimalizations are though.\n- Interesting premise for a Computer Science doctoral thesis. Let us know when the results are published.\n- Or on the other hand, the overhead of collecting the information for then making those optimisations may prove more significant than the then-faster native code.... The theory isn't really one-sided - but you can get some statistical confidence from experience in a particular problem domain.\n- @Tony: You are right - often simpler is better - but this is turning to VM implementation practice, and in many cases works well especially for long running programs.\n- True. But, if the gains were really significant, then compiled languages would put in native instrumentation code and the already-compiled alternative and mutate themselves when warranted (which of course requires getting the OS to allow writes to executable memory pages). That that's not typically pursued suggests to some extent the VM languages are waving this around more as an theoretical jab at their precompiled counterparts, rather than it being particularly effective...? Or am I too suspicious? ;-)\n- @Tony: I don't think you are following development - LLVM is becoming important and already has frontends for C/C++/Obj-C/Fortran et.c.","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":33,"estimatedTokens":1119}}651{"id":"stack-9554418","source":"stackoverflow","questionId":9554418,"title":"Can Coco/R turn a parsed file into bytecode?","tags":["delphi","compiler-construction","cocor"],"text":"Title: Can Coco/R turn a parsed file into bytecode?\nTags: delphi, compiler-construction, cocor\nSource: Stack Overflow\n\nQuestion:\nI want to write a simple compiler for educational purposes in Delphi. I have read about Coco/R and found this implementation for Delphi: http://code.google.com/p/dcocor/ . From what I have read, this is a parser for the Delphi 2009 syntax.\n\nWhat would I have to do to turn the parsed file into a bytecode? Can Coco/R do this?\n\nI know about scripting languages like FastScript or DWS, but I'd like to try and write my own for my own purposes. \n\nPlease give me some advice or clarify things a little.\n\n========================================\n\nComments:\n- Have you read the Coco/R manual? I'd say to start there. ssw.jku.at/Coco/Doc/UserManual.pdf\n- I would like to simply convert my scripts to an executable code - x86 EXE are perfectly fine. I found a LLVM parser for Delphi (code.google.com/p/llvm-pascal), but it doesn't seem to generate bytecode. Am I missing something?\n- What this answer is telling you is that you will need to write your own code generator.\n- @Pateman: that project is a Pascal compiler with LLVM backend, not an arbitrary script to executable/bytecode. No such thing exists however. Read Coco/R documentation if you want to use it, what the parser generates, and after deciding your target arch, map what the parser generates to your target arch native language (i.e. x86 asm, llvm ir, etc.)\n- @LeleDumbo Oh, so you're saying that I can \"bind\" the project to LLVM? Will I be able to generate bytecode that way? Sorry if I'm being ignorant right now, but it's all confusing to me, that's why I asked for clarification.\n- @Pateman You can take what Coco/R spits out (an AST I assume) and run with it, for instance generate LLVM IR code from it. If you have compiled the programs to LLVM IR, you'll need a way to tell the set of libraries that are LLVM that they shall take this code, optimize it, and pass it to its x86 backend to generate machine code.\n- More or less like what @delnan said. LLVM IR could be compiled to its own bitcode (yep, it's bitcode because it's encoded in bits instead of bytes) format, which could be executed with its JIT execution engine, or compiled to native architecture format (still in assembly) and later, to native executable.\n- I see... It's more complicated than I thought.\n- Have you ever learned compiler construction anyway? The process is actually trivial for any compiler/interpreter, aside from the compiler specific features. input -> lexer -> parser -> ast -> code generator -> output","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":645}}652{"id":"stack-807368","source":"stackoverflow","questionId":807368,"title":"Where can I find a full parenthesizer for Haskell?","tags":["compiler-construction","haskell","ghc"],"text":"Title: Where can I find a full parenthesizer for Haskell?\nTags: compiler-construction, haskell, ghc\nSource: Stack Overflow\n\nQuestion:\nIs there a way in GHCI to show a fully parenthesized version of a statement? I've found myself wanting to do that sometimes to help me understand a piece of code that I'm not familiar with. Sometimes the conciseness of the masters obscure things for us n00bs, and anything to help me break these beasts apart seems to help.\n\n========================================\n\nTop Answer:\nA number of approaches are documented on the wiki: http://haskell.org/haskellwiki/Indent#Indenting_Haskell_Mechanically\n\n========================================\n\nCode:\n```text\nghc -ddump-parsed\n```\n\n```text\n> unpl const . (1 +)\n (\\ e _ -> 1 + e)\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":23,"estimatedTokens":191}}653{"id":"stack-19184412","source":"stackoverflow","questionId":19184412,"title":"Online Compiler to check execution time","tags":["c++","compiler-construction","editor"],"text":"Title: Online Compiler to check execution time\nTags: c++, compiler-construction, editor\nSource: Stack Overflow\n\nQuestion:\nI want to know any available Code compiler (C++ specifically) which gives the total execution time of submitted code.\n\nTo my knowledge, **Ideone** being good online compiler **doesn't** provide this feature. \nIt'd very great if there exists a service to check the execution time (Total Running time) for a code.\n\n========================================\n\nCode:\n```text\nset -x ; g++-4.8 -std=c++11 -O2 -Wall -pedantic -pthread main.cpp && time ./a.out\n                                                                     ^^^^\n```\n\n```text\nreal    0m0.005s\nuser    0m0.000s\nsys     0m0.008s\n```\n\n```text\n5\n```\n\n```text\ntime\n```\n\n========================================\n\nComments:\n- You could just add a couple of calls to `gettimeofday()` at the start and end of `main()` and generate the elapsed time yourself ?\n- Yeah! That's possible on the system. But I wanted to know if there any service available along with the compiler. @ShafikYaghmour: Upto 2 decimal places.\n- What's the point? You have absolutely no idea of the spec of the server, what else it is doing at the time, what kind of sandbox your code runs in, ...\n- Basic idea behind this is to test the execution time of the code before submitting to the online judges for programming contests.\n- Why can't you do that locally? liveworkspace did this, by the way, but that has been under \"temporary maintenance\" for months. Shame, it was my favourite for posting examples.\n- If it is just for a rough heuristic than rextester provides this information I don't think other do but you could use `time` command in front of command on Coliru.\n- @BoBTFish: I mentioned already , locally it could be calculated but i was in a search for an online solution.\n- @ShafikYaghmour: Thanks man ! rextester does it :)\n- You may also want to check out Online C++ compiler and evaluator which pretty much covers all the online compilers out there that I know of.","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":44,"estimatedTokens":507}}654{"id":"stack-140241","source":"stackoverflow","questionId":140241,"title":"Will the Javascript performance improvements from Trace Trees find their way into other interpreted languages?","tags":["javascript","performance","compiler-construction","interpreter"],"text":"Title: Will the Javascript performance improvements from Trace Trees find their way into other interpreted languages?\nTags: javascript, performance, compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nIt sounds like Mozilla is having good luck improving JavaScript performance with TraceMonkey. See also Andreas Gal's paper on Trace Trees.\n\nAre these improvements available to other interpreters/compilers and if so, does this mean we'll see a cascade of improvements in other interpreted languages?\n\n========================================\n\nTop Answer:\nThe only possible answer to this is: **Hopefully!**\n\n========================================\n\nComments:\n- I'd like to know too, can't really find any references to the use of trace trees in other languages ... the only think I can think of is to look his main paper up on citeseer to see anyone is citing it in research on other languages ;)\n- Well, other than ActionScript, of course\n- Jaykul: the research is new, so its unlikely to show up in many other places.\n- I hope that \"BTW\" is a joke :)","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":268}}655{"id":"stack-3449127","source":"stackoverflow","questionId":3449127,"title":"How do languages/runtimes based on JVM generate Java bytecode?","tags":["java","compiler-construction","programming-languages","jvm","bytecode"],"text":"Title: How do languages/runtimes based on JVM generate Java bytecode?\nTags: java, compiler-construction, programming-languages, jvm, bytecode\nSource: Stack Overflow\n\nQuestion:\nThere are several languages/runtimes atop the JVM: such as **JRuby**, **Groovy**, **Jython**, **Clojure**, **Rhino**, **Scala**, and so on...\n\n- How do these **generate** Java bytecode?\n\n- Are there standardized **libraries** for doing this?\n\n- How the generated bytecode gets **executed**?\n\n(Or is my assumption wrong, and some of mentioned languages do **not** generate bytecode?)","metadata":{"transformedAt":"2026-08-18T18:32:17.816Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":14,"estimatedTokens":140}}656{"id":"stack-21100387","source":"stackoverflow","questionId":21100387,"title":"What are the benefits of using smaller registers, e.g. al vs eax / rax","tags":["assembly","compiler-construction"],"text":"Title: What are the benefits of using smaller registers, e.g. al vs eax / rax\nTags: assembly, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI noticed that GHC's code generator does not currently output assembly that uses any of the lower machine registers like `al`. Even byte-size operations are implemented using `rax` on 64 bit and `eax` on 32 bit machines. GCC, however, frequently makes use of these smaller registers.\n\n**Are there any real performance benefits of using small registers** like `al`?\n\nOne suggestion I've heard so far is that the opcode for `inc al` is smaller than `inc rax` (but not smaller than `inc eax`). Are there other, non-performance considerations why to use small registers?\n\n========================================\n\nTop Answer:\nIf you only use al for an 8-bit value, it leaves ah free for a second one.\n\nLoading ax vs. rax *may* offer memory bandwidth advantages. However, it could also possibly cause problems too. Gotta be careful there.\n\n========================================\n\nCode:\n```text\nal\n```\n\n```text\nrax\n```\n\n```text\neax\n```\n\n```text\nal\n```\n\n```text\ninc al\n```\n\n```text\ninc rax\n```\n\n```text\ninc eax\n```\n\n========================================\n\nComments:\n- Some of the shift instructions take only `cl`. In that case, you're *required* to use a small register.\n- In addition to smaller encoding, some instructions are simply faster with 8 bits than 32.\n- I think the answer as everyone has been implying is in the encoding. I wouldnt use GCC output as a reference necessarily. The intel documentation can/will show the encoding for various instructions and you can see for example if you want to add two small numbers you dont necessarily need 64 bits worth of immediates when maybe 16 will do. Likewise one byte of opcode may do per instruction rather than two or more. Beyond the binary fetching real estate bandwidth, number of instructions and size of each, then it becomes microcode issues which are hidden to us.\n- 64 bit assembly, when to use smaller size registers\n- Subregisters cannot be renamed, which cramps the processor's ability to parallelize operations. Most modern compilers will zero-extend bytes to fill the target register to avoid the false dependency.","metadata":{"transformedAt":"2026-08-18T18:32:17.817Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":57,"estimatedTokens":558}}657{"id":"stack-3421087","source":"stackoverflow","questionId":3421087,"title":"Compiler design and construction class","tags":["compiler-construction"],"text":"Title: Compiler design and construction class\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm taking several classes this fall for my masters and one of them is Compiler Design and Construction. I am pretty well versed in most things related to computer technology, but I have not had much experience with how compilers do the dirty, I just use them when I need to. I am not usually nervous about classes, but I kind of feel like I am walking into this one naked. If anyone can recommend some good reading or provide a short list of basic principles that I can research to bring me up to speed **quickly** I would be most grateful.\n\n**UPDATE:**\n\nWell I great in the class, and the text book we used was actually very good. This site also helped me visualize and test my regular expressions (which I now believe is the best thing to master when learning about compilers). I picked up the basics of LEX pretty quickly but YACC (for some reason) was a bit harder for me. Simply looking up examples online helped with both of them.\n\n========================================\n\nTop Answer:\nCompilers are a bit of a head-spinning exercise. However, if you have solid grasp of data structures, algorithms, and assembly code, and a penchant for thinking through the details, you'll probabaly find\nthe class fascinating because it is *so* different than most of conventional business data processing and/or embedded computing.\n\nI'd make sure you had the assembly code background down pretty well. Most procedural coders aren't used to machine instructions, registers, address modes and pointers. If you get those, the instructor will likely explain the rest pretty well.\n\nIf you aren't used to pointers, then *run* don't walk, to your nearest C compiler and write a bunch of programs that use them.\n\nFor some background on what compilers are not, see\nWriting compilers ... what's right and what's wrong?\n\nIf you want a really interesting exercise, you can learn about MetaII, which is how\nto build a compiler that builds a compiler with an insanely small amount of effort. There is a 10 page paper on how to do\nthis that has a mind-blowing moment in the middle where you suddenly see how the\ncompiler can compile itself. There's a MetaII tutorial (with the paper) at:\n\nhttp://www.bayfronttechnologies.com/mc_tutorial.html\n\n========================================\n\nComments:\n- Any of the resources listed in Learning to write a compiler might be useful. Since you don't say which part of writing a compiler concerns you, it's hard to recommend a specific resource.\n- Thanks for the link. I wish I could be more specific, but that goes to show you how little I know about compilers, and the course syllabus is fairly vague :)\n- I don't think this is the basic background he needs. If he can understand the Dragon book, he doesn't need to take the class :-}\n- Downloading it now. I am a fast learner so I'll give it a look anyhow.\n- @Ira, there are many ways of learning. Those truly interested in subjects can learn from intermediate materials. Being excited or interested in something usually means you want to learn about that thing. If you come up upon things that you don't know, and aren't well explained in your document, you generally seek the answers out. That said, there's not much in that book which should be unfamiliar to anyone who is \"well versed in most things related to computer technology\" if that implies knowledge and understanding in computer science.\n- Its a fine book, I've had it on my shelf since the first edition. Yes, you can look through it, and the serious can study it. But getting some foundation ideas and a bit of experience makes that book far, far more interesting and approachable.\n- The preface of that book suggest the first half of the book should be covered in an undergrad course and the second half should be covered in a grad course. Classes start in 10 day so I should get reading :)\n- While everything you say has value, some of it is seriously limited to having a particular class of languages in mind. Compilers exist for many different types of languages, those that involve knowledge of assembly, and many more that do not. I believe what you're trying to say is that you should know what it is you want to do, and what you want to end up with.\n- @Jer: yes, it is, but I doubt he's going to compile to a Prolog target as the starting place. Most traditional compiler classes target some kind of simplified assembler, so its a good bet that knowing that will be helpful.\n- Not what I meant. Look at all the systems that exist today, even just in programming languages, you have far more interpreters which themselves, contain a compiler coupled with the read-eval loop. Plus there are other systems, like syntax highlighting engines, all the way down to configuration files.\n- @Jer, agreed, you can use \"compiler technology\" for an incredible variety of tasks. (See my bio...). He's still taking an *intro* to-compilers class :-}\n- (+1) \"solid grasp of data structures,\" Many students / developers I met, have problems to built a compiler, because, they haven't fully understand the concept or usage of required tools...\n- I'm elbow deep in the first chapter and I think this is exactly what I was looking for. From my reading I can already see that I also need to brush up on my assembly skills, but I feel better about the class already. Thanks!","metadata":{"transformedAt":"2026-08-18T18:32:17.817Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":47,"estimatedTokens":1351}}658{"id":"stack-5238582","source":"stackoverflow","questionId":5238582,"title":"Is there a binutils for llvm?","tags":["gcc","compiler-construction","llvm","binutils"],"text":"Title: Is there a binutils for llvm?\nTags: gcc, compiler-construction, llvm, binutils\nSource: Stack Overflow\n\nQuestion:\nThe LLVM compiler toolchain has a gcc that is compatible with normal gcc. The advantage of using llvm-gcc is that is goes to an arbitrary target, meaning normal gcc will say no such target when you try to compile to a random architecture, say mips-apple-darwin. llvm-gcc will, however, actually build a compiler for Mac OS X on a mips processor.\n\nHere's the catch however: to build to a random target, you need the binutils built for that target already. So if you have a target that llvm would compile to but binutils can't, then you can't make the compiler because the GNU Binutils doesn't support that target.\n\nSo... Here's the question: Is there an equivalent llvm-binutils like an llvm-gcc that is compatable with GNU Binutils? (meaning one that builds to an arbitrary target, not one from a list.)\n\nEDIT:\n\nBy arbitrary, I mean I don't pick the target when I run llvm-gcc, I pick the target when I compile llvm-gcc. Meaning: If I try to compile GCC for mips-apple-darwin, I get a target not supported. But If I build llvm-gcc for mips-apple-darwin, it works as long as I have mips-apple-darwin-as and mips-apple-darwin-ld.\n\n========================================\n\nTop Answer:\nllvm-gcc doesn't support arbitrary targets. llvm-gcc has to be built specifically for any given target. clang + LLVM can. I'm trying to do exactly that with ellcc: http://ellcc.org. I'm using binutils for assembly and linking.\n\n========================================\n\nComments:\n- Note: LLVM 2.9 is on the tracks, and it should be the last version to support llvm-gcc. From 3.0 onward LLVM will use either CLang or Dragon Egg.","metadata":{"transformedAt":"2026-08-18T18:32:17.817Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":433}}659{"id":"stack-9059265","source":"stackoverflow","questionId":9059265,"title":"Are compiler optimizations safe?","tags":["compiler-construction","compiler-errors"],"text":"Title: Are compiler optimizations safe?\nTags: compiler-construction, compiler-errors\nSource: Stack Overflow\n\nQuestion:\nI recently discovered at work that it is the policy not to use compiler optimizations for hard real time embedded systems because of the risk of compiler bugs (we mainly use gcc but the policy extends to other compilers as well). Apparently this policy started because someone was burnt in the past by a bug with an optimizer. My gut feeling is that this is being overly paranoid so I've started looking for data on this issue but the problem is I can't find any hard data on this.\n\nDoes anyone know of a way to actually get this type of data? Can the gcc bugzilla page be used to generate some statistics of bugs vs compiler optimization level? Is it even possible to get unbiased data like this?\n\n========================================\n\nTop Answer:\nYou are assuming the compiler is bug free without optimizations and only the optimizations are dangerous. the compilers themselves are programs and very often have bugs with or without using certain features. Sure the features might make it better or they might make it worse.\n\nLlvm was mentioned in another answer, there is a well know llvm optimization bug that they appear to have zero interest in fixing\n\n```\nwhile(1) continue;\n```\n\ngets optimized out, just goes away...sometimes...and other similar but not completely infinite loops also disappear in the llvm optimizer. Leaving you with a binary that doesnt match your source code. This is one I know there are probably many more in both gcc and llvm's compilers.\n\ngcc is a monster that is barely held together with duct tape and bailing wire. It is like watching one of those faces of death movies or something like that once you have had those images in your head one time, you cant unwatch them, they are burned in there for life. So it is worth finding out for yourself how scary gcc is, by looking behind the curtain. But you might not be able to forget what you had seen. For various targets -O0 -O1 -O2 -O3 can and have all failed miserably with some code at some point in time. Likewise the fix sometime is to optimize more not less.\n\nWhen you write a program the hope is the compiler does what it says it does, just like you hope your program does what you say it does. But it is not always the case, your debugging does not end when the source code is perfect, it ends when the binary is perfect, and that includes whatever binary and operating system you hope to target (different minor versions of gcc make different binaries, different linux targets react differently to programs).\n\nThe most important advise is develop and test using the target optimization level. if you develop and test by always building for a debugger, well you have a created a program that works in a debugger, you get to start over when you want to make it work somewhere else. gcc's -O3 does work often but folks are afraid of it and it doesnt get enough usage to be debugged properly, so it is not as reliable. -O2 and no optimization -O0 get a lot of mileage, lots of bug reports, lots of fixes, choose one of those or as another answer said, go with what Linux uses. Or go with what firefox uses or go with what chrome uses. \n\nNow hard realtime embedded systems. Man mission systems, systems where life or property are directly affected. First why are you using gcc? Second, yes, optimizers are often NOT used in these environments, it creates too much risk and/or greatly increases the testing and validation effort. Normally you want to use a compiler that has been through a lot of testing itself and its warts and traps are well known. Do you want to be the person who turned on the optimizer, and as a result the flight computer crashed the airplane into an elementary school on a school day? There is a lot to be learned from the old timers. yes they have a lot of war stories, and a lot of fear of new fangled things. dont repeat history, learn from it. \"They dont build em like they used to\" means something it is not just a saying. those legacy systems were stable and reliable and still running for a reason, partly those old timers and what they learned the hard way, and partly because newer stuff is built cheaper and with lower quality components.\n\nFor this class of environment you definitely dont stop at the source code, your money and time is poured into validating the BINARY. Each time you change the binary you need to start validation over again. No different than the hardware it runs on, you change one component you, warm up one solder joint, you start validation testing over again from the beginning. One difference perhaps is that in some of these environments each solder joint is only allowed a maximum number of cycles before you scrap the whole unit. But it can be the case in software, only so many burn cycles on the prom before you scrap the prom and only so many rework cycles on the prom pads/holes before you scrap the board/unit. Leave the optimizer off and find a better, more stable, compiler and/or programming language.\n\nNow if this hard real time environment is not going to hurt people or property (other than what it runs on) when it crashes, then that is another story. Maybe its a blue ray player and it skips a frame here and there or displays a few bad pixels, big deal. Turn the optimizer on, the masses dont care about that level of quality anymore, they are content with youtube quality images, compressed video formats, etc. Cars that have to be turned off and on again for the radio or bluetooth to work. Doesnt bother them one bit, turn the optimizer on and claim performance gain over your competitor. If the software is too buggy to tolerate the customers will work around it or just buy someone elses, when that one fails they will come back to you and buy your new model with the newer firmware. They will continue to do this because they want to dancing baloney, they dont want stability nor quality. That stuff costs too much.\n\nYou should collect your own data, try the optimizers on the software in your environment and run the product through a full validation suite. If it doesnt break then either the optimizer for that code that day is okay or the test system needs more work. If you cannot do that then you can at least disassemble and analyze what the compiler is doing with your code. I would assume (and know from personal experience) that both gcc and llvm bug systems do have bugs that are tied to optimization levels, does that mean you can sort them based on optimization level? Dont know, these are open source, largely uncontrolled interfaces, so you cant rely on the masses to accurately and completely define the input fields, if there were an optimization field on the bug report form, it is probably always set to the default for the form/web page. You have to examine the problem report itself to see if the user had problems related to the optimizer. If this were a closed system for a corporation where an employees performance review might be negatively reflected for not following procedure like filling out forms correctly, you would have better searchable databases to draw information from.\n\nThe optimizer does increase your risk. Lets say 50% of the compiler is used to get an output with no optimization, another 10% to get -O1, you have increased your risk, more compiler code used, more risk of having a bug, more risk in the output being bad. and more code is used to get to -O2 and -O3. Reducing optimization doesnt eliminate the risk completely but does reduce the odds.\n\n========================================\n\nCode:\n```text\n-O2\n```\n\n```text\n-Os\n```\n\n```text\nwhile(1) continue;\n```\n\n========================================\n\nComments:\n- occasionally -O3 will produce incorrect code through some of the optimizations it does. Other than that I don't think there's any bugs that can appear from optimization alone.\n- Like you, I am slightly frustrated. Two example problems. From the page bugzilla.redhat.com/show_bug.cgi?format=multiple&id=734175 , if I click duplicate link bugzilla.redhat.com/show_bug.cgi?id=735304 , I get an Accessed Denied message. And a verified bug gcc.gnu.org/bugzilla/show_bug.cgi?id=49915 4.1.2 is solved via the statement 'is no longer supported upstream', which seems odd to me as 4.1.2 may be the key to the $1B RHEL5.\n- Just a note that I don't think \"occasionally -O3 will produce incorrect code\" is really fair. See Ian's gcc.gnu.org/ml/gcc-help/2010-07/msg00190.html. What people think are optimization bugs are often non-standard code.\n- Unfortunately the argument has been \"if it isn't real time it probably isn't critical if it crashes\". I've been looking for examples of critical systems that would be compiled with optimizations (if Linux is compiled with -Os something that runs Linux would suffice). Something like a root nameserver or a core router on the internet would be good. Really anything that would be catastrophic if it failed even remotely often, the bigger the impact the better. Unfortunately finding specifics on systems like that have been (understandably) hard to find.\n- Every thing Android is running Linux, plenty of consumer routers, PlayStations. \"if it isn't real time it probably isn't critical if it crashes\" is a pretty irrational definition. As such, I can see it could be hard to disuade. Arguably, impossible.\n- And I gotta wonder: if you're not using optimizations, does that mean you're hand-optimizing code? That's gotta be more error prone.\n- I think @smparkes comment deserves more attention. All programs have a certain acceptable minimum speed. One way to achieve that speed is by turning on optimization settings. Another way is rewriting code to be 'hand-optimized'. If it comes down to one or the other, I would pick turning on optimization settings, personally.\n- Do you have any reference for LLVM optimizing out infinite loops? And what exactly do you mean by LLVM optimizing out this loop? That it executes code coming after the code?\n- IIRC the LLVM optimization is allowed by C standard as it does not have side effects.\n- what optimization, the bug I pointed out, that has major side effects, it completely changes the flow of code.\n- @dwelch: Suppose one had a function `void foo(void) { long long exp=3; while(!FermatCounterexampleExistsWithExponent(exp) exp++; someGlobal = exp;}` and some other function called `foo()` and then did some other stuff. If the `Fermat...` method had no side-effects, a compiler would be free to split off a separate thread to handle `foo` if it ensured that the primary thread would wait for the secondary thread to complete before the next access to `someGlobal`. If everything useful the program was going to do could be accomplished without ever actually accessing `someGlobal`...\n- ...the spec would allow the program to do all those things without the loop ever having completed. While I would posit that a compiler should probably squawk if it sees a `while` loop that has no `break`, doesn't access any volatile variables, doesn't write to any pointers, and can't modify the looping condition, I have no problem with the spec saying that the time required to execute a piece of code (even if infinite) should not be considered a \"side-effect\".\n- What's ironic is that compiler writers eagerly exploit \"optimization opportunities\" posed by cases where the Standard made allowances for weird hardware, than in defining directives that are based upon the actual needs of modern compilers.","metadata":{"transformedAt":"2026-08-18T18:32:17.817Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":69,"estimatedTokens":2883}}660{"id":"stack-719058","source":"stackoverflow","questionId":719058,"title":"Live Range vs Reaching Definitions","tags":["compiler-construction","range","analysis","definition"],"text":"Title: Live Range vs Reaching Definitions\nTags: compiler-construction, range, analysis, definition\nSource: Stack Overflow\n\nQuestion:\nIn compiler data flow analysis, what is the difference between a live range of a variable and it's reaching definition? Both seem to refer to the same thing...\n\n========================================\n\nTop Answer:\nReaching definitions are defined with respect to a particular place in a program. For example, in line 10, with code \"a = b + c\" it is useful for the compiler to know what the reaching definition of \"c\" is, or in other words answer the question \"in what statement did variable c acquire a value that can reach line 10?\".\n\nLive range is defined with respect to a variable. The live range of variable c starts from a definition of variable c and goes until the next definition of the variable (in which place the variable gets killed, or defined as a different variable, if you are using SSA), or the end of the scope (block, function or program) that variable c exists.\n\nThe two concepts are similar, but they are not the same thing.\n\n========================================\n\nComments:\n- To add to @Anthony's answer. Live range is also dependent on the part of the program where the variable is used. If a variable is defined and then used a couple of times. Then for a long range of the program it is not used until it gets killed I don't think live range should include the part of the program where it is not used.\n- Walking the crontrol flow graph backwards, a variable is considered alive between a use of that variable and its last definition (assignment) before that use. It is the range (def, use) not the range (def, def). A variable that is never used, is not considered alive at all even though it may be defined a million times.","metadata":{"transformedAt":"2026-08-18T18:32:17.817Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":447}}661{"id":"stack-72341945","source":"stackoverflow","questionId":72341945,"title":"How does a compiler store information about an array's size?","tags":["c++","arrays","compiler-construction","internals"],"text":"Title: How does a compiler store information about an array's size?\nTags: c++, arrays, compiler-construction, internals\nSource: Stack Overflow\n\nQuestion:\nRecently I read on IsoCpp about how compiler known size of array created with `new`. The FAQ describes two ways of implementation, but so basically and without any internal information. I tried to find an implementation of these mechanisms in STL sources from Microsoft and GCC, but as I see, both of them just call the `malloc` internally. I tried to go deeper and found an implementation of the `malloc` function in GCC, but I couldn't figure out where the magic happens.\nIs it possible to find how this works, or it implemented in system runtime libraries?\n\n========================================\n\nTop Answer:\nAt least for GCC targeting x86_64, it is possible to investigate this question by looking at the assembly GCC generates for this simple program:\n\n```\n#include \n\nstruct Foo\n{\n int x, y;\n ~Foo() { std::cout Using Compiler Explorer, we see this code generated for the `create` function:\n\n```\ncreate():\n sub rsp, 8\n mov edi, 72\n call operator new[](unsigned long)\n mov QWORD PTR [rax], 8\n add rax, 8\n add rsp, 8\n ret\n```\n\nIt looks to me like the compiler is calling `operator new[]` to allocate 72 bytes of memory, which is 8 bytes more than is needed for the storage of the objects (8 * 8 = 64). Then it is storing the object count (8) at the beginning of this allocation, and adding 8 bytes to the pointer before returning it, so the pointer points to the first object.\n\nThis is one of the methods what was listed in the document you linked to:\n\nOver-allocate the array and put n just to the left of the first Fred object.\n\nI searched a little bit in the source code of libstdc++ to see if this was implmented by the standard library or the compiler, and I think it's actually implemented by the compiler itself, though I could be wrong.\n\n========================================\n\nCode:\n```text\nnew\n```\n\n```text\nmalloc\n```\n\n```text\nmalloc\n```\n\n```text\nN\n```\n\n```text\nnew T[N]\n```\n\n```text\nnew T[N]\n```\n\n```text\noperator new[]\n```\n\n```text\noperator new[](std::size_t x)\n```\n\n```text\nx\n```\n\n```text\nnew T[N]\n```\n\n```text\noperator new[](sizeof(T) * N + cookie_size)\n```\n\n```text\ndelete[] p\n```\n\n```cpp\n#include <iostream>\n\nstruct Foo\n{\n  int x, y;\n  ~Foo() { std::cout << \"Delete foo \" << this << std::endl; }\n};\n\nFoo * create()\n{\n  return new Foo[8];\n}\n\nvoid destroy(Foo * p)\n{\n  delete[] p;\n}\n\nint main()\n{\n  destroy(create());\n}\n```\n\n```cpp\ncreate():\n        sub     rsp, 8\n        mov     edi, 72\n        call    operator new[](unsigned long)\n        mov     QWORD PTR [rax], 8\n        add     rax, 8\n        add     rsp, 8\n        ret\n```\n\n```text\ncreate\n```\n\n```text\noperator new[]\n```\n\n========================================\n\nComments:\n- Does the compiler need to store the array size?\n- @MichaelChourdakis You don't provide any parameters in delete[] call, but your program knows how many elements need to destroy. So, this information must be stored somewhere...\n- *I tried to find an implementation of these mechanisms in STL sources* -- Well, you won't find it there -- STL is nothing more than complex C++ code, no different than the code you would write if you were experienced enough. You need to look at the actual compiler's heap management system and how it is implemented.\n- From your link, emphasis added: \"The **run-time system** stores the number of objects…\" That's not the compiler.\n- You can see that GCC stores the count: `mov QWORD PTR [rax], 8`. If you also looked at the code generated for `delete[]`, you'd see a loop around a call to `~Foo()`. However, that doesn't prove anything: it could be in libstdc++, and then inlined by the compiler.\n- The Standard is pretty loose when it comes to implementation details. This allows C++ to be implemented by a level 17 Magic User who has access to the horn of a unicorn, some pixie dust, and the shoes of a Smurf.","metadata":{"transformedAt":"2026-08-18T18:32:17.817Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":144,"estimatedTokens":988}}662{"id":"stack-63069595","source":"stackoverflow","questionId":63069595,"title":"How to transpile python Compare ast nodes to c?","tags":["python","c","compiler-construction","abstract-syntax-tree","transpiler"],"text":"Title: How to transpile python Compare ast nodes to c?\nTags: python, c, compiler-construction, abstract-syntax-tree, transpiler\nSource: Stack Overflow\n\nQuestion:\nLet's start by considering python3.8.5's grammar, in this case I'm interested to figure out how to transpile python Comparisons to c.\n\nFor the sake of simplicity, let's assume we're dealing with a very little python trivial subset and we just want to transpile trivial Compare expressions:\n\n```\nexpr = Compare(expr left, cmpop* ops, expr* comparators)\n```\n\nIf I'm not mistaken, in python an expression such as `aUnfortunately I don't know how to put that into code, so far this is what I've got:\n\n```\nimport ast\nimport shutil\nimport textwrap\nfrom subprocess import PIPE\nfrom subprocess import Popen\n\nclass Visitor(ast.NodeVisitor):\n def visit(self, node):\n ret = super().visit(node)\n if ret is None:\n raise Exception(\"Unsupported node\")\n return ret\n\n def visit_Expr(self, node):\n return f\"{self.visit(node.value)};\"\n\n def visit_Eq(self, node):\n return \"==\"\n\n def visit_Lt(self, node):\n return \"({rhs})\"\n\n def visit_Call(self, node):\n func = self.visit(node.func)\n args = [self.visit(x) for x in node.args]\n # keywords = [self.visit(x) for x in node.keywords]\n return f\"{func}({','.join(args)})\"\n\n def visit_Module(self, node):\n return f\"{''.join([self.visit(x) for x in node.body])}\"\n\n def visit_Num(self, node):\n return node.n\n\nif __name__ == \"__main__\":\n out = Visitor().visit(\n ast.parse(\n textwrap.dedent(\n \"\"\"\n 1 == 1As you can see, the output will be some sort of non compilable c output:\n\n```\ncmp(1, 3);\n(1 == (1 ((0 (bar(), 3), baz())), (4 (bar(), 3), baz());\n```\n\nQuestion, what'd be the algorithm (a python working example would be ideal here but just some general pseudocode that allowed me to improve the provided snippet would be also fine) that'd allowed me to convert python Compare expressions to c?\n\n========================================\n\nTop Answer:\nThe correct translation for:\n\n```\nif 1 == 2 is:\n\n```\nint i1 = 1;\nint i2 = 2;\nint i3 = 3;\nif(i1 == i2 && i2 **(edit: this is still not correct because it doesn't short-circuit)**\n\nAlternatively, the last one doesn't have to be a temporary variable:\n\n```\nint i1 = 1;\nint i2 = 2;\nif(i1 == i2 && i2 Alternatively: (this version keeps the expressions being compared inside the comparison expression)\n\n```\nint i1;\nif(1 == (i1 = 2) && i2 Your compilers needs to know that the values being compared are `int`s so that it can declare the temporary variables. And it also needs to choose temporary variable names which are different every time, so if you have two of these comparisons then it doesn't try to generate multiple variables with the same name.\n\nYou might realise that it's okay to evaluate the expression `2` more than once, so the compiler can generate this code:\n\n```\nif(1 == 2 && 2 but that's an optional extra.\n\nNote that there could be more than one of these in the same expression:\n\n```\nif 1 translates to something like this:\n\n```\nint i1 = 1;\n int i2 = 6;\n int i3 = 7;\n int i4 = 8;\nint i5 = (i2 or:\n\n```\nint i1;\nint i2;\nif(1 < (i1 = (6 < (i2 = 7) && i2 < 8 ? 2 : 3)) && i1 < 4) {\n// ^^^^^^^^^^^^^^^^^^^^^^ inside\n// ^^^^^^^^^^^ ^^^^^^^^^^^ outside\n```\n\n========================================\n\nCode:\n```text\nexpr = Compare(expr left, cmpop* ops, expr* comparators)\n```\n\n```text\nimport ast\nimport shutil\nimport textwrap\nfrom subprocess import PIPE\nfrom subprocess import Popen\n\n\nclass Visitor(ast.NodeVisitor):\n    def visit(self, node):\n        ret = super().visit(node)\n        if ret is None:\n            raise Exception(\"Unsupported node\")\n        return ret\n\n    def visit_Expr(self, node):\n        return f\"{self.visit(node.value)};\"\n\n    def visit_Eq(self, node):\n        return \"==\"\n\n    def visit_Lt(self, node):\n        return \"<\"\n\n    def visit_LtE(self, node):\n        return \"<=\"\n\n    def visit_Load(self, node):\n        return \"//load\"\n\n    def visit_Name(self, node):\n        return f\"{node.id}\"\n\n    def visit_Compare(self, node):\n        left = self.visit(node.left)\n        ops = [self.visit(x) for x in node.ops]\n        comparators = [self.visit(x) for x in node.comparators]\n\n        if len(ops) == 1 and len(comparators) == 1:\n            return f\"({left} {ops[0]} {comparators[0]})\"\n        else:\n            lhs = \",\".join([f\"'{v}'\" for v in ops])\n            rhs = \",\".join([f\"{v}\" for v in comparators])\n            return f\"cmp<{lhs}>({rhs})\"\n\n    def visit_Call(self, node):\n        func = self.visit(node.func)\n        args = [self.visit(x) for x in node.args]\n        # keywords = [self.visit(x) for x in node.keywords]\n        return f\"{func}({','.join(args)})\"\n\n    def visit_Module(self, node):\n        return f\"{''.join([self.visit(x) for x in node.body])}\"\n\n    def visit_Num(self, node):\n        return node.n\n\n\nif __name__ == \"__main__\":\n    out = Visitor().visit(\n        ast.parse(\n            textwrap.dedent(\n                \"\"\"\n            1 == 1<3\n            1 == (1<3)\n            1 == (0 < foo(0 <= bar() < 3, baz())) < (4 < 5)\n            foo(0 <= bar() < 3, baz())\n        \"\"\"\n            )\n        )\n    )\n\n    if shutil.which(\"clang-format\"):\n        cmd = \"clang-format -style webkit -offset 0 -length {} -assume-filename None\"\n        p = Popen(\n            cmd.format(len(out)), stdout=PIPE, stdin=PIPE, stderr=PIPE, shell=True\n        )\n        out = p.communicate(input=out.encode(\"utf-8\"))[0].decode(\"utf-8\")\n        print(out)\n    else:\n        print(out)\n```\n\n```text\ncmp<'==', '<'>(1, 3);\n(1 == (1 < 3));\ncmp<'==', '<'>((0 < foo(cmp<'<=', '<'>(bar(), 3), baz())), (4 < 5));\nfoo(cmp<'<=', '<'>(bar(), 3), baz());\n```\n\n```text\na<b<c\n```\n\n```text\na<b && b<c\n```\n\n```text\nbool v0=a<b; bool v1=v0<c\n```\n\n```text\nimport ast\nimport itertools\nimport textwrap\n\n\ndef pairwise(iterable):\n    \"\"\"s -> (s0,s1), (s1,s2), (s2, s3), ...\"\"\"\n    a, b = itertools.tee(iterable)\n    next(b, None)\n    return zip(a, b)\n\n\nclass Visitor(ast.NodeVisitor):\n    def __init__(self):\n        self.varCounter = 0\n        self.varTable = []\n\n    def visit_Expr(self, node):\n        code = self.visit(node.value)\n        variables = '\\n'.join(self.varTable)\n        self.varTable = []\n        return f'{variables}\\nprintf(\"%d\\\\n\", {code});\\n'\n\n    def visit_Eq(self, node):\n        return \"==\"\n\n    def visit_Lt(self, node):\n        return '<'\n\n    def visit_LtE(self, node):\n        return '<='\n\n    def visit_Gt(self, node):\n        return \">\"\n\n    def visit_GtE(self, node):\n        return \">=\"\n\n    def visit_Name(self, node):\n        return str(node.id)\n\n    # see http://hackflow.com/blog/2015/04/12/metaprogramming-beyond-decency-part-2/\n    def visit_Compare(self, node):\n        ops = node.ops\n        operands = [node.left] + node.comparators\n        variables = []\n        for o in operands:\n            self.varCounter += 1\n            num = self.varCounter\n            op = self.visit(o)\n            variables.append((num, op))\n            self.varTable.append(f'int t{num} = {op};')\n\n        pairs = pairwise(variables)  # adjacent pairs of operands\n\n        return ' && '.join('%s(%s %s %s)' %\n                             ('!' if isinstance(op, ast.NotIn) else '',\n                              f't{l[0]}', self.visit(op), f't{r[0]}')\n                             for op, (l, r) in zip(ops, pairs))\n\n    def visit_Call(self, node):\n        args = [self.visit(x) for x in node.args]\n        return self.visit(node.func) + \"(\" + \", \".join(args) + \")\"\n\n    def visit_Num(self, node):\n        return str(node.n)\n\n\ndef main():\n    analyzer = Visitor()\n    tree = ast.parse(\n        textwrap.dedent(\n            \"\"\"\n            1 == 1<3\n            1 == (1<3)\n            1 == (0 < foo(0 <= bar() < 3, baz())) < (4 < 5)\n            foo(0 <= bar() < 3, baz())\n            \"\"\"\n        )\n    )\n\n    # print(ast.dump(tree))\n\n    for node in ast.iter_child_nodes(tree):\n        c = analyzer.visit(node)\n        print(c)\n\n\nif __name__ == '__main__':\n    main()\n```\n\n```text\nint t1 = 1;\nint t2 = 1;\nint t3 = 3;\nprintf(\"%d\\n\", (t1 == t2) && (t2 < t3));\n\nint t4 = 1;\nint t6 = 1;\nint t7 = 3;\nint t5 = (t6 < t7);\nprintf(\"%d\\n\", (t4 == t5));\n\nint t8 = 1;\nint t10 = 0;\nint t12 = 0;\nint t13 = bar();\nint t14 = 3;\nint t11 = foo((t12 <= t13) && (t13 < t14), baz());\nint t9 = (t10 < t11);\nint t16 = 4;\nint t17 = 5;\nint t15 = (t16 < t17);\nprintf(\"%d\\n\", (t8 == t9) && (t9 < t15));\n\nint t18 = 0;\nint t19 = bar();\nint t20 = 3;\nprintf(\"%d\\n\", foo((t18 <= t19) && (t19 < t20), baz()));\n```\n\n```text\nif 1 == 2 < 3:\n```\n\n```text\nint i1 = 1;\nint i2 = 2;\nint i3 = 3;\nif(i1 == i2 && i2 < i3) {\n```\n\n```text\nint i1 = 1;\nint i2 = 2;\nif(i1 == i2 && i2 < 3) {\n```\n\n```text\nint i1;\nif(1 == (i1 = 2) && i2 < 3) {\n```\n\n```text\nif(1 == 2 && 2 < 3) {\n```\n\n```text\nif 1 < (2 if (6 < 7 < 8) else 3) < 4:\n```\n\n```text\nint i1 = 1;\n    int i2 = 6;\n    int i3 = 7;\n    int i4 = 8;\nint i5 = (i2 < i3 && i3 < i4 ? 2 : 3);\nint i6 = 4;\nif(i1 < i5 && i5 < i6) {\n```\n\n```text\nint i1;\nint i2;\nif(1 < (i1 = (6 < (i2 = 7) && i2 < 8 ? 2 : 3)) && i1 < 4) {\n//            ^^^^^^^^^^^^^^^^^^^^^^ inside\n// ^^^^^^^^^^^                               ^^^^^^^^^^^ outside\n```\n\n```text\nint\n```\n\n```text\n2\n```\n\n```text\nC++\n```\n\n```text\nCompare.c\n```\n\n```text\nstruct Compare\n```\n\n```text\nop\n```\n\n```text\noperand\n```\n\n```text\ncomparator\n```\n\n```text\nunion\n```\n\n```text\ncomparator\n```\n\n========================================\n\nComments:\n- There is no standard C expression that will get you the behavior you want.\n- \"so I guess in c you should do something like bool v0=a<b; bool v1=v0<c in order to prevent b being evaluated more than once in case the first clause is true.\" I would start with this insight, and generate multiple statements.\n- Turns out that transpiling is not as easy as just changing the syntax!\n- It should be something like `int i1 = 1; int i2 = 1; int i3 = 3; if(i1 == i2 && i2 < i3)`. If you make the compiler even smarter, it might be able to tell that it doesn't matter if the 1 is evaluated twice, so it can change it to `if(1 == 1 && 1 < 3)`\n- What's the purpose of `return f\"cmp({rhs})\"`? That's not what C code looks like.\n- @sepp2k No purpose really, consider it some pseudo c/c++ call to prove my point... If such a \"magical\" instruction existed in c/c++ the problem would be a non-brainer and i wouldn't be asking about it in the first place... Now I think of... I see i've tagged this thread as c and asked explicitely about c when I wouldn't mind coming with an algorithm suitable for c & c++ ... I guess now it's too late to change it? tsk :(\n- If you're targetting C++, you *can* define a variadic template that will make the code you're generating work once you fix the fact that you're dropping the left-most operand (i.e. it'd need to be `cmp('==', '(1, 1, 3);` for `1 == 1 < 3`), but I think it makes a lot more sense to just generate code in the form that user253751 suggested.\n- I agree, an algorithm providing the output user253751 suggested would be a very good solution... As for variadic templates... I'll read about them to see if I can easily test it out. But again, the goal should be providing readable c++ code, so... :)\n- @BPL Can you put into words what's stopping you from producing the output described by user253751? Is it the generation of the unique identifiers? You can solve that using a counter member variable in the visitor. The fact that the code for the generated variables needs to go before the code of the expression? You can solve that by putting the code for the variables into a list that is prepended to the code of the expression whenever an expression is used from a statement. Something else? I feel like as long as you describe your specific problem, you'll get a simple answer that solves it.\n- @sepp2k Nothing is stopping me to write the algorithm myself producing the output provided by user253751 (which btw is not answering what i've asked for but it's been upvoted nevertheless). I'm writing a full transpiler over here and the task is huge... I'm still not dealing with this subsection yet as it's low prio. The main goal was getting answers here that could help overall and not just examples or answers not addressing my question. Also, the explanation from that answer was something i was aware of but i had decided not to add to my question to make it simpler, so...\n- @BPL Have you considered the possibility that maybe NodeVisitor doesn't do the thing you want? You can just *look at the nodes directly*, you don't have to go through NodeVisitor.\n- `And of course there are many more details to consider` - the main is that without short-circuit sideeffects will differ.","metadata":{"transformedAt":"2026-08-18T18:32:17.817Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":26,"totalLines":449,"estimatedTokens":3144}}663{"id":"stack-17434561","source":"stackoverflow","questionId":17434561,"title":"How to find gc roots in a stack machine?","tags":["compiler-construction","garbage-collection","code-generation","stack-machine","gc-roots"],"text":"Title: How to find gc roots in a stack machine?\nTags: compiler-construction, garbage-collection, code-generation, stack-machine, gc-roots\nSource: Stack Overflow\n\nQuestion:\nI am writing a compiler for a fairly standard stack machine. Now I want to add a garbage collector. I can see that I could generate some sort of 'stack maps' to know which variables are gc roots in each activation record. However, I have no idea how to deal with intermediate values pushed in the stack during execution. The language I am compiling is Pascal-like so I don't need and I don't want to use tags to identify pointers from other data types.\n\nI would appreciate any hints/pointers on how to\n\n- Find gc roots in the stack at any point in time (i.e., how to identify which of the intermediate values which have been pushed in the stack are gc roots).\n\n- Usual forms of encoding this information (i.e., how to generate and encode 'stack maps')\n\nThank you very much!\nNicolas\n\n========================================\n\nTop Answer:\nAnother option is to use a shadow stack: a stack of references you maintain yourself. This is by far the easiest option to implement.","metadata":{"transformedAt":"2026-08-18T18:32:17.817Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":20,"estimatedTokens":286}}664{"id":"stack-2130248","source":"stackoverflow","questionId":2130248,"title":"What are good heuristics for inlining functions?","tags":["gcc","gnu","compiler-construction","icc","inlining"],"text":"Title: What are good heuristics for inlining functions?\nTags: gcc, gnu, compiler-construction, icc, inlining\nSource: Stack Overflow\n\nQuestion:\nConsidering that you're trying solely to optimize for speed, what are good heuristics for deciding whether to inline a function or not? Obviously code size should be important, but are there any other factors typically used when (say) gcc or icc is determining whether to inline a function call? Has there been any significant academic work in the area?\n\n========================================\n\nTop Answer:\nA function call implies some additional code (the function prologue, where the new stack frame is set up, and the function epilogue, where it's cleaned up). If your compiler sees that the function code is small in comparison to the prologue and epilogue, it can decide it's not worth it to make an actual call, and will inline the function.\n\nThe only benefit I see of calling a function instead of inlining it are size-related. I guess inlining a function then unrolling a loop can result in a significant size increase.\n\n========================================\n\nComments:\n- @Mitch: Of course, but I'm curious about how the compiler decides whether to inline or not.\n- mainly number of instructions I believe...\n- Wouldn't inlining also affect caching, so that (if you didn't inline) the function code would be placed on a separate cache line from the \"main\" code?\n- How does .NET deal with multiple inlining possibilities(i.e. if func1 is called in 3 different places, does .NET try all combinations of inlining, or just all-or-nothing)?\n- .NET is JIT compiled so it can make decisions with information at runtime about different inlining possibilities. This is just my intuition, but I'd be surprised if .NET DID NOT inline func1 differently in 3 different places since it should have the information to make an informed decision. In that case you'd want a book on JIT technology.\n- Honestly, I don't know the details that well. I've only read one article on it.","metadata":{"transformedAt":"2026-08-18T18:32:17.817Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":23,"estimatedTokens":505}}665{"id":"stack-13188750","source":"stackoverflow","questionId":13188750,"title":"Type checking in compilers","tags":["compiler-construction","typechecking"],"text":"Title: Type checking in compilers\nTags: compiler-construction, typechecking\nSource: Stack Overflow\n\nQuestion:\nI'm currently trying to create a TypeChecker that will successfully type check a MiniJava program. I've been working on it, staring at it, for the past 10 hours, but I have no idea even where to begin. I have given up getting the project done in time, but I want to still learn how it's done. We are given complete parser for MiniJava and a set of classes used for traversing the Abstract Syntax Tree as well as two different default Visitors, DepthFirstVisitor and GJDepthFirst. We are supposed to extend these visitors to get the project done.\n\nI understand the VERY basic concept of what needs to get done: we need to catch errors in the code that the parser couldn't catch. We need to run through the code in 2 passes. The first pass is building the symbol table (?) and the 2nd, to use the symbol table to check. Is this correct?\nBut then I have no idea where or how to begin to implement this in code.\n\nI realize this isn't really a question.......but any kind of guidance or help will be greatly appreciated. I have a few friends in my class who are in the exact same boat as me.\n\nThank you!\n\n========================================\n\nCode:\n```text\nvar\n```\n\n```text\nauto\n```\n\n========================================\n\nComments:\n- It's basically a traversal of the AST but it's too big a topic for a question here. Surely you must have already been educated as to how this is done?\n- We are trying to educate ourselves now :( But I understand what you mean by traversing the AST. I know what an AST is, but the code implementation...we have no idea.\n- Unfortunately, we don't have any idea about your code implementation either. It's pretty hard to help without knowing what you're starting with.\n- As @EJP said it's a tree traversal. On the first traversal you build the symbol table--you can think of it as a dictionary--by looking at all the tree nodes that introduce new identifiers--e.g. field definitions, local variables. In the symbol table you remember the type associated with the identifier, its scope, and other info that you might need later. On the second pass, using the symbol table and some rules--e.g. adding two ints will give you an int--you can check if your program is well typed. This is just rough description.\n- @Alex, you do not need two passes (and if `auto` is present, you *cannot* have two passes). Any arbitrarily complex type system can be handled in one pass - first you annotate all the expressions with *type variables*, then you generate a set of *equations* using simple rules for each expression and statement node. And then solve the equations separately (e.g., using a Prolog-like engine).\n- @SK-logic I know that type checking (and inference for that matter) algorithms work in one pass, but the OP said that his homework required two passes so I presented it like that.\n- @Alex, I see, have not noticed that. This requirement excludes `auto` then, good.","metadata":{"transformedAt":"2026-08-18T18:32:17.817Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":35,"estimatedTokens":753}}666{"id":"stack-13094001","source":"stackoverflow","questionId":13094001,"title":"Intelligent solution to computing jump addresses in a bytecode compiler?","tags":["compiler-construction","bytecode"],"text":"Title: Intelligent solution to computing jump addresses in a bytecode compiler?\nTags: compiler-construction, bytecode\nSource: Stack Overflow\n\nQuestion:\nLet's say I'm implementing a bytecode compiler, similar to Lua's/Python's... so on.\n\nI'm traversing the AST, generating bytecode instructions, and I run into a `break` inside of an `if-else` inside of a `while` loop:\n\n```\nwhile (cond1)\n if (cond2)\n ...\n else\n break\n```\n\n(I tried writing out equivalent bytecode but it didn't look too helpful.)\n\nThe point is, there are at least 4 \"jump\" instructions in that example, and I\nwant to find an elegant solution to filling in the jump addresses as I compile the AST...\nI don't know the jump address for the `while` loop or the `break` until after I've fully\n\"compiled\" the inner statements.\n\n- A pseudocode solution would be ideal\n\n- A solution shouldn't depend on whether I'm implementing a register- or stack-based bytecode compiler (I'm playing with both)\n\nI'm not reading the dragon book just yet.\n\nIf I'm recursively compiling the AST, when I reach a `break` statement inside of some arbitrary number of loops and `if-else` blocks, how should the compiler know which empty label to jump to? I'm guessing some type of label-name stack external to the recursive AST walking function.\n\n========================================\n\nCode:\n```text\nwhile (cond1)\n    if (cond2)\n        ...\n    else\n        break\n```\n\n```text\nbreak\n```\n\n```text\nif-else\n```\n\n```text\nwhile\n```\n\n```text\nwhile\n```\n\n```text\nbreak\n```\n\n```text\nbreak\n```\n\n```text\nif-else\n```\n\n```text\n# start of codegen for 'while'\nL1:\n  [evaluate cond1]\n  jne L2   # forward reference: use a dummy value for now\n\n# start of codegen for 'if ... else'\nL3:\n  [evaluate cond2]\n  jne L4   # another forward reference: use a dummy value for now\n  [code in 'if' branch]\n  j L5     # another forward reference: use a dummy value for now\nL4:\n  [code in 'else' branch before 'break']\n  j L2\n  [code in 'else' branch after 'break']\nL5:   # end of 'if ... else'\n  # now go back and fill in references to L4, L5 inside the 'if ... else' block\n  # end of codegen for 'if ... else'\n\n  # codegen for 'while' continues...\n  j L1   # loop\nL2:   # end of 'while' loop\n  # now go back and fill in references to L2 inside the 'while' block\n  # end of codegen for 'while'\n```\n\n```text\n...\nwhile (outer)\n    ...\n    if (outer_done)\n        break\n    ...\n\n    while (inner)\n        ...\n        if (inner_done)\n            break\n        ...\n    [break from inner 'while' ends up here]\n\n    ...\n    if (outer_done_2)\n        break\n    ...\n[break from outer 'while' ends up here]\n...\n```\n\n```text\ncodegen for while:\n    save previous break backpatch list\n    initialise break backpatch list as empty\n    perform codegen for evaluating condition\n    perform codegen for body statements\n    apply backpatches\n    restore previous break backpatch list\n```\n\n```text\nbreak\n```\n\n```text\nif-else\n```\n\n```text\nbreak\n```\n\n```text\nwhile\n```\n\n```text\nwhile\n```\n\n```text\nwhile\n```\n\n```text\nbreak\n```\n\n```text\nwhile\n```\n\n```text\nbreak\n```\n\n```text\nbreak\n```\n\n```text\nwhile\n```\n\n========================================\n\nComments:\n- The break inside the else block should be a `j L2`. When recursively traversing AST nodes, the break needs to be converted to the correct label.\n- this is really helpful, but I've pretty much gotten this far. I came across the 'backpatching' terminology on a NYU CS class page, but unfortunately it didn't go into much detail. I added a tiny bit more to my question to clarify what I'm looking for.\n- Great, that makes sense. Any `break` or `continue` statements only apply to the current enclosing loop construct of course. I just need to pass some state-structure recursively as well. Thanks for your help.","metadata":{"transformedAt":"2026-08-18T18:32:17.817Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":22,"totalLines":181,"estimatedTokens":939}}667{"id":"stack-5448548","source":"stackoverflow","questionId":5448548,"title":"Is it likely ever to be possible to compile from D 2.0 to Javascript?","tags":["javascript","ajax","web-applications","compiler-construction","d"],"text":"Title: Is it likely ever to be possible to compile from D 2.0 to Javascript?\nTags: javascript, ajax, web-applications, compiler-construction, d\nSource: Stack Overflow\n\nQuestion:\nLong-term C++ programmer (that's me) exploring other languages and is wondering whether it is worth learning much about D 2.0. (That is the clean slate re-write of D.) So far, I like what I see - many pragmatic, wise choices. Now, I want to learn a little more and want to know whether it is likely ever to be possible to do the following:\n\n- Write some code in D 2.0. (Maybe it is a well-defined subset of D 2.0.)\n\n- Compile the code from 1.\n\n- Receive Javascript source code from the compiler.\n\nIn summary, I would like to have a D 2.0 compiler with a backend capable of generating Javascript.\n\nSo my question has two parts:\n\n- Does such a compiler exist already?\n\n- If such a compiler does *not* exist, and *if* I chose to implement one, where would be the best place to start?\n\nFor context, the reason that I would like the above is so that I could implement a bunch of business logic in D 2.0, and then use that business logic in a pure Javascript (that is no Flash/Silverlight/Java) Web Application.\n\n**Update**\n\nI have had three answers, two of which are on-topic, and one that is not and is therefore ignored.\n\n@FeepingCreature reckons that it will never happen because \"it's too incongruous with D's targeted environment\". I am interested to know what that means, and have asked.\n\n@Nekuromento , in contrast, points out that it is already theoretically possible using LDC, followed by the LLVM Javascript backend. I checked out both links. LDC support for D 2.0 looks like a work in progress. But I would guess it will get there at some point. The LLVM Javascript is also not optimised or minified, but it looks from the demos that they work. So I think that is enough to be useful at the very least.\n\n**Update 2**\n\nOK - this seems to be the best solution to date.\n\n- Invoke LDC. (@Nekuromento) LDC combines the Digital Mars front-end with an LLVM backend.\n\n- Pipe the output from 1. through the LLVM Javscript backend. (@Nekuromento)\n\n- Pipe the output from 2. though Closure (@BCS)\n\nI will choose a best answer fairly soon, but I would like some more-informed opinion really.\n\n**Update 3**\n\n*Some more information on why I want to do this.*\n\nHere is a concrete example of one project that I wish to implement. It will be a geometric modelling library, probably written by just me, taking several years to write, and ending up at about 100,000 lines of code. I would like it to contain as close to zero bugs as possible, and, therefore, I do not wish to implement it in Javascript. I would like it to run on the server, on the desktop, and in the browser too. (By in \"in the browser\" I mean without the assistance of any plugin such as Flash, Silverlight or Java.) The truth is that code in the browser running on Javascript is here to stay for a while. Projects will increasingly need a strategy to deal with that.\n\n**Update 4**\n\nI was hoping for some more answers, but didn't get them. Ah well, never mind. Thanks for all the feedback, including @dsimicha 's stimulating comments. (I hope it is OK to disagree technically sometimes, while at the same time keeping things civil.) I have accepted @Nekuromento 's answer.\n\n**Update 5** 2018-07-18\n\nSeven years on, LDC v1.11 supports compiling and linking to WebAssembly.3\n\n========================================\n\nTop Answer:\nKeep your eye on the D newsgroups over the next few days, I'm working on something you might be interested in (or rather, exactly what you want). It can currently compile:\n\n```\nint foo()\n{\n if (true)\n {\n return 3;\n }\n}\n```\n\nI'm hoping to get an alpha/beta of some sort out within the next week.\n\n========================================\n\nCode:\n```cs\nint foo()\n{\n    if (true)\n    {\n        return 3;\n    }\n}\n```\n\n========================================\n\nComments:\n- Out of curiosity, why do you want to do this? It seems like a huge abstraction inversion, since the source language is lower level than the target language.\n- D 2.0 is a lower level language than Javascript? That's a pretty strong assertion to make. From the small amount I have read so far, D 2.0 has many high level features that Javascript lacks - a static type system (yes the absence of a static type system is not a feature), deep immutables, message-based concurrency, unit tests in the language, ...\n- One thing I did not make clear - I want the code to be able to run in a WebApp on the client, and elsewhere - on the server for example.\n- @Paul: D is definitely lower level than JavaScript, though this is not a criticism of D. D supports pointers and manual memory management. Even if you don't like duck typing, it's still higher level than nominative static typing with primitive mapping closely to machine types. I'll grant that the concurrency issue is an exception, though. Generally implementing low-level stuff on top of high-level stuff is a bad idea (for example implementing static typing on top of duck typing). Compatibility with standard web browsers is a good reason for doing this, though.\n- @dsmicha - you are conflating having a static type system with being a low-level language. Right now I am evaluating Haskell and D2.0, both of which have static type systems and support for deep immutability. In fact, although D 2.0 is billed as a systems language, it looks much closer to a multi-paradigm language to me.) Haskell has an unbounded precision Integer type and an Int type with at least 29 bits of precision. Does that make Haskell a low-level language?\n- @dsmicha - and what of Javascript and integers? Javascript, being a high-level language must surely have unbounded precision Integers right? Unfortunately stackoverflow.com/questions/307179/&hellip; Javascript integers seem to be usually 64-bit floating bit values, except sometimes when they revert to being signed 32 bit integers. Sorry, but that does not seem very high-level to me.\n- @dsmicha - here is one personal project I have in mind - a geometric modeller. It will take me several years, and end up at about 100,000 lines of code. Javascript as an implementation language? Well that would not be my 1st choice. Of course I want the code to be portable - on the desktop, on the server and in the browser. Increasingly, the reality is that if projects do not have some strategy to run in the browser on top of Javascript they will not get implemented at all.\n- +1: Thanks for the reply. Don't worry about being snarky - all informed opinion is welcome. I was wondering whether you could expand a little on the phrase \"too incongruous with D's targeted environment\"? Remember, a well-defined subset would of D 2.0 would still be useful to me. (Anybody who has used C++ for real know that you learn parts of the language to avoid.) In fact, if my compiler could only compile the programs that I wrote it still might be valuable to me.\n- Well it's a systems programming language. Static typing, compilation to objects and binary, but I suppose my point is more that the work involved in writing a D-to-Javascript compiler is not significantly less than writing a CustomLanguage-to-Javascript compiler, so there's no reason to start with D in the first place.\n- OK - I thought you had a deeper point than the fact that D 2.0 is statically typed.\n- +1: Thanks for the reply. Ah, that's interesting. Your answer somewhat contradicts @FeepingCreature . From what you write, what I want may already be possible. I visited the links that you kindly provided. There are some cavats. (1) LDC D 2.0 support is a work in progress. (2) javascript LLVM is no optimised or even minified. But so what! That might not matter too much.\n- I had no idea that Javascript LLVM was a thing.\n- You should be able to run the output of that stage through something like Google's JS compiler: code.google.com/closure/compiler\n- @BCS +1 - I had heard of Closure and was wondering whether there was any value in doing just what you suggest. Thanks for the opinion that it might be worth my while.\n- See also: digitalmars.com/pnews/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.818Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":105,"estimatedTokens":2035}}668{"id":"stack-40142168","source":"stackoverflow","questionId":40142168,"title":"Left Associativity vs Left Recursion","tags":["parsing","compiler-construction","grammar","left-recursion"],"text":"Title: Left Associativity vs Left Recursion\nTags: parsing, compiler-construction, grammar, left-recursion\nSource: Stack Overflow\n\nQuestion:\nI'm trying to write a compiler for C (simpler grammar though).\n\nThere is something that I've been stuck on for a while. If I understated correctly, all binary operations are left associative. So if we have we \"x+y+z\", x+y occurs first and then followed by plus z.\n\nHowever, doesn't enforcing left associativity causes infinite left recursion?\n\nSo far all solutions that I've checked are either left associative or don't have left recursion, but not **both**. Is it possible to have a grammar that have both of these properties?\n\nExample:\n\n**Left Associative:**\n\n```\nExpr = Term | Expr + Term\nTerm = Element | Term ∗ Element\nElement = x|y|z|(Expr)\n```\n\n**Left Recursion Eliminated:**\n\n```\nExpr = Term ExprTail\nExprTail = epsilon | + Term ExprTail\n\nTerm = Element TermTail\nTermTail = epsilon | * Element TermTail\n\nElement = x|y|z|(Expr)\n```\n\nAny ideas?\n\n========================================\n\nCode:\n```text\nExpr = Term | Expr + Term\nTerm = Element | Term ∗ Element\nElement = x|y|z|(Expr)\n```\n\n```text\nExpr = Term ExprTail\nExprTail = epsilon | + Term ExprTail\n\nTerm = Element TermTail\nTermTail = epsilon | * Element TermTail\n\nElement = x|y|z|(Expr)\n```\n\n========================================\n\nComments:\n- You can eliminate left recusrion without losing left-associativity, and it is a standard procedure in LL parsers. The exponentiation operator, if you have one, is binary and right-associative.\n- In C the assignment operators are right-associative, but that's it.\n- @EJP Are you saying there's a way to rewrite the grammar in the question without left-recursion in such a way that the generated parse tree will be left-associative without having to do any post-processing on the tree? If so: how?\n- \"you can parse a repeated pattern using a loop\" In case of LL parser generators like ANTLR that translates to using repetition operators like `*` in the grammar. That will give you a parse tree with a list in it, which you can then loop over in whichever direction you want.\n- Thank you. Yes I am using recursive descent parsing. I am sensing using a loop is the right idea. Could you guide me a bit on where (how) to use the loop exactly? Is it as @sepp2k mentioned, so that for example, as long I have '+', I just add whatever element I have to a list, and then when addition is over, I return that list and recurse on it? Am I understanding this right?\n- @Babak If you're hand-writing the code, you don't need to go through a list. You can build the tree directly in the loop. Something like: `result = parsePrimaryExpression; if(result) while(parseToken(STAR)) { result = new Multiplication(result, parsePrimaryExpression()); } return result;`. You know, plus error handling and also handling the division operator etc.\n- @babak: basically as sepp2k says. Note that the reduction does nkt, strictly speaking, correspond to the grammar, since if you did a real tailcall, you would end up right associating. So passing the accumulator (`result` in sepp2k's example) into the loop effectively inverts the tree as you construct it. But it's a subtle point probably of little interest in practical coding. Personally, I would use an LR parser generator, but tastes differ.\n- @sepp2k Thank you so much. I might be misunderstanding here, but isn't the code you wrote is right associating? So say we are parsing \"x * y * z\". Result will be x, and after going to the while loop, we are going to get: new Multiplication(\"x\", parsePrimaryExpression()); Which I believe at the end will result in Multiplication(\"x\", Multiplication(\"y\",\"z\")), no?\n- @Babak No, after the first iteration, result will be `Multiplication(x,y)`, so when it does `result = new Mult(result, parsePrimaryExpression())` a second time in the second iteration, it will end up as `Mult(Mult(x,y), z)`.\n- @sepp2k Ofcourse. Sorry, I was being stupid :) Yes, that did the trick! thank you so much both :)\n- A practical example of mixing bottom-up method in recursive descent can be found in C in 4 Functions Repo expr function in c4.c It can take some patience to read because of the way the code is minified and divided into only 4 functions. This expr() function implements precedence climbing for a big subset of C expressions.","metadata":{"transformedAt":"2026-08-18T18:32:17.818Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":70,"estimatedTokens":1084}}669{"id":"stack-533076","source":"stackoverflow","questionId":533076,"title":"Understanding C++ compilers from a Java / C# perspective","tags":["c#","java","c++","compiler-construction","transition"],"text":"Title: Understanding C++ compilers from a Java / C# perspective\nTags: c#, java, c++, compiler-construction, transition\nSource: Stack Overflow\n\nQuestion:\nI'm a moderately experienced Java / C# programmer, and I've recently started learning C++. The problem is, I'm having trouble understanding how to structure the various header and code files. This seems mostly due to my lack of understanding as to how the compiler links everything together. I've tried reading some textbooks, but my preconceptions are heavily colored by my Java and C# knowledge. For example, I'm having a hard time coming to grips with the fact that methods and the like can be defined in a namespace rather than only in a class definition. \n\nI've found plenty of C++ -> Java/C# guides, but practically nothing to go the other way. Are there any good resources out there for easing the Java/C# -> C++ transition, particularly with respect to understanding the compiling process?\n\n========================================\n\nTop Answer:\nThis is something that confused me when I first started using C as well. Books don't do a good job of describing the correct use of headers vs. code files. \n\nThe compiler works by loading each .cpp file and compiling it independent of all the others. The first step in compilation is to load all of the headers referred to by #include statements. You can think of it doing a textual insert of the whole foo.h wherever there is a #include \"foo.h\".\n\nWhat are the implications of this for how to structure your files? Header files should have whatever parts of the program are needed for other .cpp files to refer to. As a general rule, implementations should not be in header files. This will cause problems. Header files should include declarations of classes, functions, and global variables (if you must use them).\n\n========================================\n\nCode:\n```text\nthis\n```\n\n========================================\n\nComments:\n- Edit: Thanks to all who answered, all answers were useful and informative.\n- For any in a similar situation, I've found this page very helpful, particularly section A3.3 (Classes): horstmann.com/ccj2/ccjapp3.html\n- \"\"Header files should include declarations of classes\"\" well in fact, you *define* classes and *declare* its member functions in header files.However, *defining* those member functions/static members is done in source files. while, class foo; is a decleration, class foo{} is definition.","metadata":{"transformedAt":"2026-08-18T18:32:17.818Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":31,"estimatedTokens":612}}670{"id":"stack-12766808","source":"stackoverflow","questionId":12766808,"title":"Why is javac source code is written in java?","tags":["java","compiler-construction","javac"],"text":"Title: Why is javac source code is written in java?\nTags: java, compiler-construction, javac\nSource: Stack Overflow\n\nQuestion:\nI have downloaded the `javac` source code from here and I found that the it itself is written in java language. However, I was expecting that it was written in c/c++.\n\nAnyway, how does this java compiler source code written in java compiled when there was no java compiler?\n\n========================================\n\nTop Answer:\nA compiler that is written in the language it compiles is called a bootstrapping compiler.\n\nThe way they are made is kind of a head trip, but just think: when the original language was written, there was no java, and so they had to create the compiler in another language, which, actually, was written in C/C++. Check it out, here: In which language are the Java compiler and JVM written?\n\nAlso, the way that Java works, I don't know if you know, is that the compiler (`javac`) actually doesn't generate machine code files, it creates bytecode files that are then **interpreted** by the JVM.\n\n========================================\n\nCode:\n```text\njavac\n```\n\n```text\njavac\n```\n\n========================================\n\nComments:\n- en.wikipedia.org/wiki/Bootstrapping_%28compilers%29\n- It is a common practice that a compiler to a language is written in its language, to demonstrate its abilities. AFAIK, for these reasons - many don't consider VB as a \"real\" language.\n- This is the bootstrap question. Answer is that the *initial* compiler was written in something else, and then rewritten in Java when powerful enough.\n- @amit: Seriously? VB could easily compile itself. That's definitely not why it's 'not considered a real language'.\n- @nneonneo: 'it can' and 'that how it was first implemented' are two different things. This is at least the impression I got from my programming languages lecturer a few years ago in UNI.\n- As Ravn said, java compiler was originally written in C, later moved to Java when powerful enough. JVM from Sun is developed in C too. This link can help to explore few things stackoverflow.com/questions/1220914/&hellip;\n- @nneonneo, Isn't `.class` file which runs on JVM?\n- @amit: No language can possibly be first implemented in itself. VB's no different. Your PL lecturer is biased, probably as he does not consider VB suitable for 'real' programming tasks. Nonetheless, it is in fact a real programming language, and people use it to solve real problems (even if you wish they didn't).\n- @Starx: and why did you not reply to my comment on your answer? Anyway, the JVM compiles nothing (unless you count JIT); `javac` takes human-readable textual source code (\"Java\" code) and turns it into a binary form. It is therefore a compiler for Java source, and a compiler implementing the Java programming language.\n- @nneonneo Actually, I consider the JVM runtime's JIT to be the only \"real\" compiler there is. `javac` does not optimise the bytecode that is emitted, and for good reason: such optimisations are best left for the JIT compiler.\n- @ChrisJester-Young: Some JVMs don't do JIT (e.g. because nobody bothered to do it for that architecture yet). The *Java language specification* tells you what compilation means. What the JVM does is up to the JVM implementer.\n- @nneonneo In theory, perhaps. But in practice, any practical JVM implementation will JIT, because otherwise the execution will be horribly inefficient.\n- Or, some JVMs could just skip JIT and execute bytecode right on the processor.\n- @nneonneo In that article, it explains that the bytecode actually undergoes binary translation to native ARM instructions. So, yes, that is a form of JIT, albeit a somewhat more lightweight version compared to what you get with HotSpot's `-server` mode.\n- If we're going to get really technical...x86 is also \"JIT\" by that definition, because real processors execute x86 instructions as sequences of microcode. Would you then call C compilers \"not real\" because they don't generate microcode?\n- @ChrisJester-Young: What definition of \"compiler\" are you using that would exclude javac? Take the Wikipedia entry, for example (not claiming this as an authority, just an example): \"A compiler is a computer program (or set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language, often having a binary form known as object code).\" `javac` certainly counts by that definition.\n- @JonSkeet I didn't say that javac isn't a compiler. I'm saying that it's not appropriate to discount HotSpot's JIT as a compiler, since it's got much more complicated compilation machinery compared to javac.\n- @nneonneo, I was not sure if that was the right answer. I thought and turn out what you are saying is the same thing. `javac` compiles source to executable code that runs on JVM.\n- @ChrisJester-Young: You said (to quote): \"Actually, I consider the JVM runtime's JIT to be the only \"real\" compiler there is.\" That explicitly excludes `javac` from being a \"real\" compiler. That's not *at all* the same statement as saying \"the JIT is a compiler as well\". If you're going to start creating distinctions between \"compiler\" and \"real compiler\" then we'll need definitions for *two* terms...\n- @JonSkeet That was indeed badly-worded on my part. I had issue with nneonneo's assertion that \"the JVM compiles nothing (unless you count JIT)\", which I felt diminished the JIT compiler's (much more intense than javac) compilation, so my response was basically saying, hey, if you want to say which one is the \"realer compiler\", well....\n- @ChrisJester-Young: They're simply *different* compilers. If we're talking about the compiler for the Java *language*, javac is what there is. The JIT compiler *wouldn't* count there. If we're talking about \"all the compilers used in the process of executing code which starts out as Java\" then I'd definitely include the JIT, in systems which use one.\n- @JonSkeet Yes, that's a better way of looking at it, I agree. Then you can say, for the \".java-to-.class phase\", javac is the compiler in use, but for the \".class execution phase\", then the JIT compiler kicks in, etc. And then there are other phases, like microcode, etc.\n- @ChrisJester-Young Actually, the CPU-instruction-to-microcode translation cannot be termed \"compilation\" is it is not ahead-of-time. The term that fits that process would be \"interpretation\".\n- @MarkoTopolnik Well, JIT compilation is not ahead-of-time, either. I don't actually know the specifics of the instruction-to-microcode translation, but I understood that it has a lot of smarts behind it. My choice of terminology here, of course, is that compilation == smart and interpretation == dumb. And not everyone is going to think of those terms quite that way. :-)\n- @ChrisJester-Young JIT is ahead-of-time because it compiles a whole method at once and basically substitutes the native code for the bytecode in later invocations. CPU is a classic interpreter---it only works on the exact instruction(s) it is going to execute next. Also, to qualify as a compiler, the CPU would have to save the compiled code and later refer only to it, skipping the actual machine instructions.\n- @MarkoTopolnik Fair enough (and of course, at the instruction level, there's no concept of methods). To my very limited understanding (since I currently know next to nothing below the instruction level), with the instruction cache, the possibility of \"[referring] only to [the compiled microcode], skipping the actual machine instructions\" is, in theory, there, even if current processors don't actually do it. But like I said, I don't know what actually happens in reality, so I'm happy to accept what you've said.\n- No modern JVM does interpretation any more. Dynamic (JIT) compilation is the norm and should be assumed to be the case. (You can specify JVM flags to force interpretation, to track down whether there's JIT-related bugs. But, no normal deployment should ever use such flags.)\n- It's my first question on stackoverflow, and I didn't expect this much reply so soon. I know how java works, and the reason I downloaded the compiler source from openjdk is that I want to develop a java compiler myself in java language for study. Thank you all.\n- @bobzhou: exciting, eh? watching a bunch of programming geeks discuss these details :P\n- @nneonneo It's a fantastic place, I'm loving it.\n- @Chris, I understand the differences between interpretation modes and JIT compilation, but the inference given by the term \"compilation\" is that code is executed on the machine directly, and I didn't want to give that impression. It's easy to get lost in semantics, but as far as programming languages are concerned, I believe that JIT is just an optimizing interpreter -- whose optimization step consists of **greedy** machine-compilation of bytecode -- not a compiler.\n- @Bob, if you want to begin to learn about writing a compiler, then you need to take a look at ANTLR and learning about lexers & parsers. Oh, and, you'll need to get a nice-sized bottle of Tylenol, because grammars will give you a headache -- guaranteed.\n- @alvonellos, I'm reading jvm specification, java language specification and some books about principles of compiler recently. I wanted a try if I can do it on my own. Thank you for your advice.\n- No problem. If you liked my answer, show it! Also, I'd recommend that, before you get started on writing a compiler, that you learn regular expressions and what they're about. Regular expressions are a chomsky type 3 language (regular grammars) and so it's not going to get you very far in terms of grammars, but it'll help with writing a lexer.\n- @bobzhou, in fact it was a good question,made me/others to think bit differently.\n- @ChrisJester-Young The statement \"No modern JVM does interpretation any more.\" is clearly false. To take a random example, HotSpot **does interpretation**. Not only that, **it does it most of the time**. The default compilation threshold is 10,000 iterations over a piece of code before it gets JITted. As the very name of the JVM says it, HotSpot detects **hot spots** in your code as JIT targets and merrily interprets the rest.\n- Till the first generic is encountered of course.... And probably more recent constructs. (from 1.5 up)\n- @MarkoTopolnik Thank you---between you and Jon Skeet, I am having to learn to be more nuanced in what I say. Actually, yes, I think in particular HotSpot's `-client` mode does interpretation even more frequently than `-server` mode, so of course your point holds. My main point was that, if you don't specify `-Xint`, there's (usually---as you've pointed out, not \"always\") much more going on under the covers that just interpretation, so I was responding to that. I would edit my earlier comment, but SO doesn't permit that. :-)\n- @ChrisJester-Young The fairest thing to say would be that, with the advent of virtual machines employing JIT compilers, the distinction between an interpreter and a compiler is losing its conceptual ground and should be de-emphasized in discussion.\n- @MarkoTopolnik Yes, JIT/dynamic compilation does indeed blur the line between (static, ahead-of-time) compilation and interpretation. It's a particular shade of grey, and I wonder if there are other shades of grey. :-)\n- As a matter of fact, the Wikipedia article is disputed at this point, since it says that there should be a citation or reference that can demonstrate that this statement is true. Although I do not doubt it, the reference is, perhaps, not the best.\n- Not all JVMs are written in C++, though.\n- @nneonneo well in this case, since the compiler is mentioned, it is c++.\n- JVM != Java compiler. Two totally separate pieces of code.\n- @nneonneo Of course, and we are talking about the other piece here...\n- Cool. Just thought you should mention that not all JVMs are Oracle, or written in C++ :)\n- @EdwinDalorzo In fact I'm not aware of any *primary* evidence that the Oracle JVM is written in C++, and I've been looking on and off, mostly off, since 1997. All the evidence I've seen, including some source code, suggests C.\n- Part of Hotspot is written in C, not C++.","metadata":{"transformedAt":"2026-08-18T18:32:17.818Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":80,"estimatedTokens":3043}}671{"id":"stack-57703398","source":"stackoverflow","questionId":57703398,"title":"Is there any example of language grammar that possible for Yacc to express but impossible for Antlr4?","tags":["compiler-construction","antlr4","yacc","ll-grammar","lalr"],"text":"Title: Is there any example of language grammar that possible for Yacc to express but impossible for Antlr4?\nTags: compiler-construction, antlr4, yacc, ll-grammar, lalr\nSource: Stack Overflow\n\nQuestion:\nI try to learn about language parser recently and always seen the review about difference in Yacc and Antlr (about LALR and LL). It was always some concluded wording like \"LALR is more powerful\". But I can't understand what its really means\n\nSo could anyone please enlighten me what is the meaning of the word powerful here?\n\nI just assume that it would be meant \"Yacc can do something Antlr can't do\", if it is I wish I could see the exact example about it\n\n========================================\n\nTop Answer:\nHere's an example grammar, for which there are examples that neither ANTLR nor YACC can parse without hackery:\n\n```\nstmt = declaration ';' ;\n stmt = expression ';' ;\n declaration = type ID ;\n type = ID '*' ;\n expression = product ;\n product = ID ;\n product = product '*' ID ;\n```\n\nHere's an example that neither ANTLR or L(AL)R can parse:\n\n```\nx * y ;\n```\n\nusing this grammar.\n\nThere are two possible parses: 1) as a statement, 2) as a declaration.\n\nThis example is taken from the C language. (You can make a smaller\ngrammar; I tried to leave this in a form that was easy to understand its realism).\n\nL(ALR) parsers and ANTLR will provide you at most one derivation. Which means\nthey will each miss any alternative.\n\nOne can hack the parsing machinery to resolve this (as GCC famously used to do)\nby bringing in symbol table information. That tangles parsing and symbol table\nconstruction which IMHO simply makes a mess out of the parser. In particular, you can't decide what it accepts by just looking at the grammar.\n\nThe problem is that ANTLR and L(AL)R will not parse all context free langauges.\nThey parse only (different) subsets; this means one can parse certain instances that the other cannot and vice versa. This means one is not more powerful than the other.\n\nIf you want a more powerful parser, e.g., handles fully context free languages, you need to look at Earley parsers, or GLR or GLL parsers. Earley parsers aren't very efficient. GLR and GLL tend to be efficient parsers on parts of the grammar which don't introduce ambiguities (multiple parses) or require large amounts of lookahead. But most programming language construct you might want to parse tend not to be confusing because people have a hard time reading them in that case, too.\n\nBecause of the limits of L(AL)R and ANTLR, my company (Semantic Designs) uses GLR parsers for some 40 languages. This includes full C++17 which has more ambiguous parses and long-lookahead cases than you can shake a stick at, including the above example, most of them a lot more obscure. As far as I know, we have the only C++17 parser that uses a grammar directly; GCC and Clang use hand-coded recursive descent with lots of tangled symbol table checks.\n\n[The author of the other answer, Scott McPeak, did build C++ parsers for older dialects of C++ using GLR, more or less at the same time we started to do this. Hats off to him.]\n\n========================================\n\nCode:\n```text\n{ a^i b^j | i≥j }\n```\n\n```text\nS ::= a S | P\nP ::= a P b | <empty>\n```\n\n```text\nvar x = { console.log(\"I am a code block\"); /*result is*/ 6; };\n  var x = { a:1, b:2 };\n```\n\n```text\n{                    // hmmm, code or object?\n  { a                  // possible object literal key\n  { a :                // a-ha! definitely object literal\n```\n\n```text\n{                    // start of object literal?\n  { (                  // uh-oh ...\n  { (a                 // I'm\n  { (a ?               //     getting\n  { (a ? b             //             lost\n  { (a ? b :           // is this the ':' after a key? help!\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\na\n```\n\n```text\na,b\n```\n\n```text\nP\n```\n\n```text\nb\n```\n\n```text\na\n```\n\n```text\nS\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\na\n```\n\n```text\nS\n```\n\n```text\nb\n```\n\n```text\na\n```\n\n```text\na\n```\n\n```text\n{ a^i b^i }\n```\n\n```text\nk\n```\n\n```text\n{\n```\n\n```text\n:\n```\n\n```text\n:\n```\n\n```text\n{\n```\n\n```text\nstmt = declaration ';' ;\n   stmt = expression ';' ;\n   declaration = type  ID ;\n   type = ID '*' ;\n   expression = product ;\n   product = ID ;\n   product = product '*' ID ;\n```\n\n```text\nx * y ;\n```\n\n========================================\n\nComments:\n- It is a vague way of saying that the set of LALR(1) grammars is a superset of the set of LL(1) grammars, so the answer is 'yes'. You will find examples in any good compiler textbook.\n- But ANTLR is LL(*) (more precisely ALL) parser, not LL(1).\n- Do you mean example of a language? Ore example of a grammar? Or both? For instance, some grammars are not LL(1), but describe a language for which a LL(1) grammar exists. That happens when we \"refactor\" a grammar to make it LL(1) without changing the language.\n- @Kaz I mean both. The example of grammar and the language it would produce that Yacc could express but antlr4 couldn't\n- @IvanKochurkin There are thereoms that state that any LALR(k) grammar can be rewritten as LALR(1), and similarly for LR(k) and LL(k), so that is a distinction without a difference.\n- You can add arbitrary predicates to YACC reductions too, so that isn't a differentiating feature.\n- @IraBaxter I wasn't intending to say it was, but I can see how it might appear that way. So that I can add it to my answer, do you have a reference for Yacc predicates? I don't see semantic predicates in classic Yacc, and it appears Bison only has them for the GLR parser.\n- I haven't done this with bison or yacc, but have done with my own LALR parsers and I assume you can do that same with their engines, after all they are open source. You hack the parsing engine code; find a place where a reduction or shift is going to proposed, and add your semantic check there. it isn't pretty or fun, but then no ad hoc change to a standard parsing engine is. Granted, this is easier when the designer of the parsing engine adds hooks for it.\n- Same McPeak of Elkhound fame? You should have written my answer :-}\n- @IraBaxter Yep, that was my MS thesis. :)","metadata":{"transformedAt":"2026-08-18T18:32:17.818Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":30,"totalLines":206,"estimatedTokens":1540}}672{"id":"stack-46166191","source":"stackoverflow","questionId":46166191,"title":"What kind of lexer/parser was used in the very first C compiler?","tags":["c","parsing","compiler-construction"],"text":"Title: What kind of lexer/parser was used in the very first C compiler?\nTags: c, parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn the early 1970s, Dennis Ritchie wrote the very first C compiler.\n\nIn the year 2017, I wanted to write a C compiler. Books like *Deep C Secrets* (Peter Van Der Linden) say that C was, above all else, designed to be easy to compile. But I've been having an inordinate amount of trouble with it. \n\nFor starters, it's already relatively difficult to come up with Lex/Yacc specifications for the C language, and these tools didn't even exist yet when Ritchie made *his* compiler!\n\nPlus, there are a great many examples of surprisingly small C compilers that do not use any help from Lex & Yacc. (Check out this tiny obfuscated C compiler from Fabrice Bellard. Note that his \"production\" tinycc source is actually quite a bit longer, most likely in an effort to accommodate more architectures, and to be more readable)\n\nSo **what am I missing here?** What kind of lexer/parser did Ritchie use in his compiler? Is there some easier way of writing compilers that I just haven't stumbled onto?\n\n========================================\n\nCode:\n```text\ntree\n```\n\n```text\nYY_INPUT\n```\n\n```text\n#if\n```\n\n```text\npragma\n```\n\n========================================\n\nComments:\n- At the time of the first C compiler, there were already compilers for much more complex languages like PL/I and Algol. Lexing and parsing were coded ad hoc in those days.\n- The original C language was simpler than the language we have now. I suspect the compiler was written in B, the language that inspired C.\n- For a learning project, C is not well suited. The Wirth languages Pascal, Modula-2 or Oberon are much better suited, as they can be implemented by an LR-1 parser. I definitively would go with one of them (I wrote one for Oberon-2 once, maybe Module would be the most easy). Wrt lex/yacc: I'm seriously not sure they really make things that more simple. A straight-forward direct coding approach is not really complicated, if the language suits it (see above). Or you could use a more modern compiler-generator. lexx/yacc are quite old. Typical Linux systems like Ubuntu have some in their repos.\n- @Barmar: Indeed. IIRC, it did not even have a seperate, hierarchical namespace for `struct` members or `typedef`, etc.\n- You might try to find a copy an early edition of the \"Dragon Book\" on compilers by Aho and Ullman from the 70's. This would predate lex/yacc, so would explain how compilers were written in those days.\n- en.wikipedia.org/wiki/Principles_of_Compiler_Design Don't confuse it with the later book by Aho, Sethi, and Ullman; this was written in the 80's, and probably uses lex/yacc.\n- Check out this\n- Ritchie's original C compiler used a recursive descent parser. See also github.com/mortdeus/legacy-cc and bell-labs.com/usr/dmr/www/primevalC.html\n- @torek: Yeah, that's basically what I thought. Edited the answer to confirm. (I'd already seen Alexey's comment but I didn't have time to edit the answer until now.)\n- This is exactly the kind of answer I was looking for! Not only did you answer the question directly, you left me with a lot of new keywords (shunting yard, left corner) which allowed me to find some really neat stuff. Also, the library at my school happens to have Appel's book, so I'll pick it up today. I wish it was possible to vote something up more than once. Cheers!","metadata":{"transformedAt":"2026-08-18T18:32:17.818Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":47,"estimatedTokens":858}}673{"id":"stack-6448705","source":"stackoverflow","questionId":6448705,"title":"Adding a function in LLVM (haskell bindings) when the number of parameters is not known at compile time","tags":["haskell","compiler-construction","language-design","llvm"],"text":"Title: Adding a function in LLVM (haskell bindings) when the number of parameters is not known at compile time\nTags: haskell, compiler-construction, language-design, llvm\nSource: Stack Overflow\n\nQuestion:\n**Background**: I have written a toy Lisp interpreter that I am trying to add LLVM JIT functionality to. For the moment, have imposed the following limitations:\n\n- Only integer values are allowed in functions\n\n- Variables may only reference formal parameters\n\n**Given**:\n\n```\ncompile :: [Value] -- List of Formal Parameters\n -> [Value] -- Body of function\n -> CodeGenModule(Function a)`\n```\n\n**Question**: How do I generate a function where the number of parameters equals the length of the Formal Parameters list?\n\n========================================\n\nCode:\n```text\ncompile :: [Value] -- List of Formal Parameters\n        -> [Value] -- Body of function\n        -> CodeGenModule(Function a)`\n```\n\n```text\nLLVM.Core\n```\n\n```text\nLLVM.FFI.Core\n```\n\n```text\nTypeRef\n```\n\n```text\nfunctionType\n```\n\n```text\naddFunction\n```\n\n```text\nappendBasicBlock\n```\n\n```text\ninsertBasicBlock\n```\n\n```text\ngetParam\n```\n\n```text\nLLVM.Core.Util\n```\n\n========================================\n\nComments:\n- A good start might be to either help me understand `forall f g r . FunctionArgs f g (CodeGenFunction r ())`\n- would you like to you experience of using haskell binding of LLVM? which version do you used? :)\n- @Wu: LLVM 2.9 with the llvm binding from commit 729514. See also","metadata":{"transformedAt":"2026-08-18T18:32:17.818Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":10,"totalLines":72,"estimatedTokens":367}}674{"id":"stack-19718352","source":"stackoverflow","questionId":19718352,"title":"How to write a compiler in Haskell? In C# I use a lot of state","tags":["haskell","compiler-construction","state"],"text":"Title: How to write a compiler in Haskell? In C# I use a lot of state\nTags: haskell, compiler-construction, state\nSource: Stack Overflow\n\nQuestion:\nI'm doing a compiler in C# for a school project and I can't stop wondering how I would do *that* in Haskell.\n\nFor example:\n\nMy code generation for a While loop is:\n\n```\npublic override void generateCode(Compiler compiler)\n {\n int jumpToTheBeginningInstructionIndex = compiler.getIndexOfNextActionInCurrentFunction();\n MachineInstructions.JMP jumpTotheBeginning = new MachineInstructions.JMP(jumpToTheBeginningInstructionIndex);\n MachineInstructions.JMPF jumpToTheEnd = new MachineInstructions.JMPF();\n\n booleanExpression.generateCode(compiler);\n\n //I insert the jump to the end here:\n compiler.addAction(jumpToTheEnd);\n\n foreach(IAction action in this.thenActions)\n {\n action.generateCode(compiler);\n }\n compiler.addAction(jumpTotheBeginning);\n\n //...But is here where I know where should it jump to:\n jumpToTheEnd.whereToJump = compiler.getIndexOfNextActionInCurrentFunction();\n }\n```\n\nYou can see how I insert the code for the jumpToTheEnd at the very middle of the method, but is not until the end where I know the line where the jump jumps. Fortunately, I keep a pointer to that jump and I'm easily able to set its whereToJump attribute at the very end of the method.\n\nHow would you do that in Haskell!?\nAny *recommended* tutorial?\n\n========================================\n\nCode:\n```text\npublic override void generateCode(Compiler compiler)\n    {\n        int jumpToTheBeginningInstructionIndex = compiler.getIndexOfNextActionInCurrentFunction();\n        MachineInstructions.JMP jumpTotheBeginning = new MachineInstructions.JMP(jumpToTheBeginningInstructionIndex);\n        MachineInstructions.JMPF jumpToTheEnd = new MachineInstructions.JMPF();\n\n        booleanExpression.generateCode(compiler);\n\n        //I insert the jump to the end here:\n        compiler.addAction(jumpToTheEnd);\n\n        foreach(IAction action in this.thenActions)\n        {\n            action.generateCode(compiler);\n        }\n        compiler.addAction(jumpTotheBeginning);\n\n        //...But is here where I know where should it jump to:\n        jumpToTheEnd.whereToJump = compiler.getIndexOfNextActionInCurrentFunction();\n    }\n```\n\n```text\n{-# LANGUAGE FlexibleContexts #-}\nimport Control.Monad.State\nimport Control.Monad.Writer\n\ndata Instruction = Nop | Jump Int | Label Int deriving (Eq, Ord, Show, Read)\ndata AST = While AST AST | Rest               deriving (Eq, Ord, Show, Read)\ntype Compiler = StateT Int (Writer [Instruction])\n\ngenerateLabel :: Compiler Int\ngenerateLabel = do\n    v <- get\n    put (v+1)\n    tell [Label v]\n    return v\n\ncompile :: AST -> Compiler ()\ncompile Rest = tell [Nop]\ncompile (While b c) = do\n    start <- generateLabel\n    compile b\n    mfix $ \\end -> do\n        tell [Jump end] -- here we generate a forward jump\n        compile c\n        tell [Jump start]\n        generateLabel   -- here we allocate the label we're forward-jumping to\n    return ()\n\nrunCompiler :: Compiler () -> [Instruction]\nrunCompiler = execWriter . flip evalStateT 0\n```\n\n```text\nmfix\n```\n\n```text\nrunCompiler (compile (While Rest Rest))\n```\n\n========================================\n\nComments:\n- One simple method is to build a structured table of functions and then call them in order (exploting the HOFs).\n- Here's some reading that might interest you en.wikibooks.org/wiki/Write_Yourself_a_Scheme_in_48_Hours\n- @PedroRodrigues That's an interpeter and so lacks the code-gen phase, which is the subject of the question","metadata":{"transformedAt":"2026-08-18T18:32:17.818Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":111,"estimatedTokens":889}}675{"id":"stack-18209959","source":"stackoverflow","questionId":18209959,"title":"Run dynamically generated assembly in C (GNU/Linux)","tags":["c","assembly","compiler-construction","jit","machine-code"],"text":"Title: Run dynamically generated assembly in C (GNU/Linux)\nTags: c, assembly, compiler-construction, jit, machine-code\nSource: Stack Overflow\n\nQuestion:\nI'm writing a proof-of-concept JIT compiler in C, which at the moment is generating strings of assembly code. The inline assembly functionality in C only deals with string literals that are known at compile time, so I can't use it to run my generated-at-runtime code.\n\nI've read about using mmap() to execute generated *machine code* at runtime, but I'd like to avoid working with machine code if possible.\n\nDoes anyone know of any solutions? I've thought of writing it to a file and invoking the assembler & linker on said file, but that'd be messy and slow.\n\n========================================\n\nTop Answer:\nWhat I've done is generate C/C++/Fortran code, compile it on the fly, link it into a DLL, and dynamically load the DLL, all of which takes on the order of a few seconds at most.\nYou could do the same, except generate ASM.\nIt's a very effective technique when you need speed of the resulting code, plus the flexibility of the code (and run-time libraries) of the language you're generating.\n\n========================================\n\nComments:\n- So you're saying you generate assembly source code? Why not just generate the machine code itself?\n- @DrewMcGowen I'd prefer not to as it's less readable, harder to test, and time is of the essence so if I'd like to avoid refactoring if possible. Although it looks as if I may have to.\n- There is many lib out there for emitting assembly. gnu-lightning llvm, ...\n- Generating both machine code to run, and assembly to debug/test against, is a great idea. I think this is what I will do. Many thanks.\n- I've decided to go with the machine code option, but I find this to be a very interesting idea, I will be considering it for future projects. Are you aware of any relevant articles/tutorials etc?\n- @AlexJ136: Articles? The key things to learn are 1) how to write a DLL (any example will do), 2) how to \"shell out\" to run the compiler+linker under control of your program, and 3) how to load a DLL and get the procedure addresses it exports.","metadata":{"transformedAt":"2026-08-18T18:32:17.818Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":27,"estimatedTokens":539}}676{"id":"stack-49493005","source":"stackoverflow","questionId":49493005,"title":"Is every LL(1) grammar also a LALR(1) grammar?","tags":["parsing","compiler-construction"],"text":"Title: Is every LL(1) grammar also a LALR(1) grammar?\nTags: parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nSo one source says it is and another says its not\n\none source says this : \n\nhttps://i.sstatic.net/tzDy2.jpg\n\nanother says this : \n\nhttps://i.sstatic.net/TqAkP.png\n\nthe closest answer i found is this :\n\nRelationship between LR(0), LL(0), LALR(1), etc?\n\nbut this doesn't answer the relation between LL(1) and LALR(1)\n\n**also** if you can answer the more general question, which is what is the relation between LL(k) and LALR(k) it would be even more helpful \n\nthanks.\n\n========================================\n\nComments:\n- I don't know where the first chart comes from, but it's totally unreliable. Operator precedence *grammars* are LR(1). There are people (and Wikipedia) who seem to think that the word \"precedence\" is not relevant, but that's untrue. **Operator** grammars may be ambiguous and thus not LR(1), but if a precedence relationship can be derived, the grammar is LR(1).\n- @rici how can Operator precedence grammars be LR(1) when Operator precedence grammars can be ambiguous ? does that mean if a grammar is Operator precedence then i can 100% be sure that it is LR(1) as well ? also do you have a source for that? thanks\n- an operator grammar is operator precedence if there is at most one precedence relation for any point in a parse. A `` is a reduce. So there can be no shift-reduce conflicts. Proving the absence of reduce-reduce conflicts is only slightly more complicated.\n- arbitrary operator grammars can be ambiguous and you could build an operator precedence parser not based on the grammar's precedence relations. That doesn't make the grammar operator-precedence, though (imho). That's the issue with the Wikipedia page, for example.\n- And if you want sources, ask on an academic site like Computer Science. Here we're pragmatic programmers.\n- @rici alright thanks man","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":34,"estimatedTokens":480}}677{"id":"stack-4580654","source":"stackoverflow","questionId":4580654,"title":"time complexity trade offs of nfa vs dfa","tags":["compiler-construction","time","time-complexity","dfa","nfa"],"text":"Title: time complexity trade offs of nfa vs dfa\nTags: compiler-construction, time, time-complexity, dfa, nfa\nSource: Stack Overflow\n\nQuestion:\nI am looking for a discussion on which is better used and in what circumstances in a compiler an nfa or dfa. what are the time complexity trade-offs of simulating an nfa vs dfa and which one is more suitable during what circumstances in a compiler??\n\n========================================\n\nComments:\n- i found the answer for anyone else looking..\n- Time-Space Tradeoffs Goal: Given reg. exp.r and input stringx, determine whetherx is in L(r) Method #1: Build NFAN fromr using Thompson's construction, then run previous algorithm &#161; Can construct NFA inO(|r|) time. &#161; N has at most twice as many states as |r|, and at most two transitions from each state, so transition table isO(|r|) space. &#161; Previous algorithm accepts or rejectsx inO(|r|&#215;|x|) time\n- Method #2: Build NFAN fromr using Thompson's construction, then DFAD fromN using subset construction; then use DFA algorithm from last time for accepting/rejectingx &#161; D can have up to 2k states, where k = # states in N. ''Worst- case'' string (a |b)*a (a |b)(a |b)...(a |b) : why? &#161; DFA acceptance algorithm accepts or rejectsx inO(|x|)\n- Summary: Automaton Build Run NFA O(|r|) O(|r|&#215;|x|) DFA O(2|r|) O(|x|) So use first method (NFA) for quick search over short text strings (e.g., emacs r.e. search) Use second method (DFA) for longer searches over long text strings (e.g., Unix grep on multiple files) ''Lazy'' DFA method builds DFA transition table on the fly, caching transitions as state/input pairs are encountered\n- I understand that the number of states in a DFA constructed from an NFA with $r$ states could be up to $2^r$, but I don't understand how the cost of building a DFA from an NFA with $r$ states costs $O(2^r)$. What about the cost of adding edges on each input symbol the the DFA? Is $O(vertices) = O(edges)$ for the constructed DFA?\n- Running an NFA via backtracking, as you seem to suggest, will take O(2^n) in the worst case. Luckily there are smarter algorithms that take O(mn) time, though they don't support backreferences. (One implementation is Google's re2.)\n- Very interesting, thank you. I wonder why the same reasoning doesn't apply to Turing Machines vs Non-deterministic Turing Machines.","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":17,"estimatedTokens":589}}678{"id":"stack-31224643","source":"stackoverflow","questionId":31224643,"title":"Variant vs Inheritance","tags":["c++","inheritance","boost","compiler-construction","abstract-syntax-tree"],"text":"Title: Variant vs Inheritance\nTags: c++, inheritance, boost, compiler-construction, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nLet's suppose I'm writing a compiler for some programming language. It is common to use abstract syntax tree (AST) as an internal representation. I can see two possible ways to design it:\n\n- using `boost::variant`\n\n- using inheritance\n\nAs hierarchy of nodes is fixed - `boost::variant` will suffice.\n\nMy question is what are the advantages and disadvantages of each approach from points of maintability and runtime efficiency?\n\n========================================\n\nCode:\n```text\nboost::variant\n```\n\n```text\nboost::variant\n```\n\n```text\nboost::variants\n```\n\n========================================\n\nComments:\n- Largely opinion based. I'd use variants. But that's because I don't mind the visitors that come with it. And also because Boost Spirit happens to work very very nicely with them. But then again, YMMV and you might not have this \"accidental\" benefit of library compatibility\n- I do no agree this question is opinion based at all. It is about best practices when designing the code. Nothing of what has been asked here cannot be measured, both the advantages and the disadvantages of each of the two approaches can be analyzed from a technical point of view. Another great question closed for no reason.\n- I guess first is better when new operations are added often and new types are added rarely, and the second is better when new operations are added rarely and new types are added often?\n- @justanothercoder Interesting thought ! The first is better if the types are rather unrelated, and it would be difficult to design a common base. In this case new operations are indeed easier to add for variant.\n- Inheritance also has the disadvantage that operations are hard to understand as the code for them is scattered across all the different classes in the type hierarchy. You can avoid that by gathering them into visitor classes, but that requires multiple dynamic dispatch which is poorly supported in C++.","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":38,"estimatedTokens":516}}679{"id":"stack-4611053","source":"stackoverflow","questionId":4611053,"title":"Designing an intermediate representation for a compiler","tags":["compiler-construction","language-design"],"text":"Title: Designing an intermediate representation for a compiler\nTags: compiler-construction, language-design\nSource: Stack Overflow\n\nQuestion:\nI've been looking at compiler design. I've done a one semester course on it at University and have been reading *Modern Compiler Design* by Grune et al, the book seems to advocate an annotated Abstract Syntax Tree as the intermediate code, and this is what we used in the course.\n\nMy question is what are the benefits of this approach versus producing some kind of stack-machine language or low level pseudo code , particularly with regard to having a compiler which can target many machines.\n\nIs it a good idea to simply target an already existing low level representation such as LLVM and use that as the intermediate representation?\n\n========================================\n\nTop Answer:\nAn AST and low-level pseudo-code are two different abstractions of a program in the journey a compiler takes from a high-level language to object code.\n\nAs with any complete data representation, you can do everything you need to with either representation. Some things are just easier to do with one than the other.\n\nFor example, it's easier to do semantic and syntax analysis on an AST. It's easier to do instruction scheduling on pseudo-code.\n\nCompiler front-ends developers tend to like ASTs. Back end developers tend to like pseudo code.\n\n========================================\n\nComments:\n- having many transforms has a major disadvantage: it becomes very difficult to report errors which refer to the original source code. This is the big difference between micky-mouse academic translators and production compilers. It would actually be good if researchers would invent a tracing concept which could be adjoined with proposed semantic transforms so that one could not just prove properties of the semantics, but also the back-trace-ability of errors.\n- @Yttrill, I can't see any disadvantages there: it is trivial to keep all the source location information all the way down to the assembly. You have to do it anyway, otherwise you won't be able to emit debugging information. There is nothing to invent here.","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":27,"estimatedTokens":538}}680{"id":"stack-6104254","source":"stackoverflow","questionId":6104254,"title":"Compile Ruby with its C API","tags":["ruby","compiler-construction","interpreter"],"text":"Title: Compile Ruby with its C API\nTags: ruby, compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nIf I take a ruby code\n\n```\nputs \"Hello World!\"\n```\n\nand rewrite it using the C API of Ruby\n\n```\n#include \"ruby.h\"\n\nint main() {\n ruby_init();\n\n rb_funcall(Qnil, rb_intern(\"puts\"), 1, rb_str_new2(\"Hello World!\"));\n\n ruby_finalize();\n return 0;\n}\n```\n\nand compile it, is this a way to compile Ruby code?\n\nIf I create a program that uses `Ripper` to parse the Ruby code and rewrite it as C, can I call it as a \"Ruby compiler\"? There're some ruby code that can't be rewrited in Ruby in this way? Did someone tried to write this kind of \"compiler\" before?\n\n========================================\n\nTop Answer:\nyes that is \"c-ified\" ruby code, as it were.\n\nThe closest things to \"ruby to c\" have been http://ruby2cext.rubyforge.org, rubinius with its JIT compiler, and ruby2c\n\nhttp://betterlogic.com/roger/2009/08/how-to-use-the-ruby2c-gem\n\nAnother option would be to write a JIT compiler for 1.9's bytecode, that might speed things up a bit.\n\nAlso see the mirah language, which is like static, compile time ruby.\n\nTheoretically it should be possible.\n\n========================================\n\nCode:\n```text\nputs \"Hello World!\"\n```\n\n```text\n#include \"ruby.h\"\n\nint main() {\n  ruby_init();\n\n  rb_funcall(Qnil, rb_intern(\"puts\"), 1, rb_str_new2(\"Hello World!\"));\n\n  ruby_finalize();\n  return 0;\n}\n```\n\n```text\nRipper\n```\n\n========================================\n\nComments:\n- Any language can be compiled, just as any language can be interpreted. Some languages (Perl, Python, probably Ruby) would need an interpreter embedded in the compiled program in order to fully support undisciplined use of `eval` and such, but that's more or less what your code is doing.","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":73,"estimatedTokens":443}}681{"id":"stack-49134312","source":"stackoverflow","questionId":49134312,"title":"Hindley Milner type inference for mutually recursive functions","tags":["haskell","functional-programming","ocaml","compiler-construction","hindley-milner"],"text":"Title: Hindley Milner type inference for mutually recursive functions\nTags: haskell, functional-programming, ocaml, compiler-construction, hindley-milner\nSource: Stack Overflow\n\nQuestion:\nI'm making a strongly typed toy functional programming language. It uses the Hindley Milner algorithm as type inference algorithm.\n\nImplementing the algorithm, I have a question on how to infer types of the mutually recursive functions.\n\n```\nlet rec f n = if n == 0 then 0 else g (n - 1)\nlet rec g n = if n == 0 then 0 else f (n - 1)\n```\n\n`f` and `g` are mutually recursive functions. Now, when the type checker is inferring the type of function `f`, it should also be able to infer the type of function `g`, since it is a subexpression.\n\nBut, in that moment, function `g` is not defined yet. Therefore, the type checker doesn't even know the existence of function `g`, as well as the type of function `g`, obviously.\n\nWhat are some solutions that real world compilers/intepreters use?\n\n========================================\n\nCode:\n```text\nlet rec f n = if n == 0 then 0 else g (n - 1)\nlet rec g n = if n == 0 then 0 else f (n - 1)\n```\n\n```text\nf\n```\n\n```text\ng\n```\n\n```text\nf\n```\n\n```text\ng\n```\n\n```text\ng\n```\n\n```text\ng\n```\n\n```text\ng\n```\n\n```text\nand\n```\n\n```text\nlet rec\n```\n\n```text\n'a\n```\n\n```text\n'a\n```\n\n========================================\n\nComments:\n- This problem (family of problems?) in general is called unification, and there are several known algorithms for solving it. This applies to both types and logical formulas because the two are quite similar\n- @Cubic I didn't know this problem is related to unification algorithm. Actually, I already implemented `unify` function, maybe without actually deeply understanding it.\n- Thank you very much. However, how does type system add the recursive names to the environment, although it does't know the actual type of the function yet?\n- Typically, it assigns them a fresh type variable and lets the regular unification process take care of the rest. This is the same way you'd handle a single recursive function.\n- @K.A.Buhr Thank you. Do I have to assign fresh type variables to all the mutually recursive functions except the function which is directly inferred?\n- Ah, but start with the simpler problem of inferring the type of a single recursive function (think `factorial`). How will you \"directly\" infer its type when it depends on a function (namely itself) whose type is unknown to the compiler at the time of definition? Again, the answer is to assign it a fresh type variable while it's being defined. So, for a block of mutually recursive functions (including the special case of a single recursive function), you actually want to assign fresh type variables to all of the functions and then start inferring their types.\n- @K.A.Buhr I really appreciate your kind help. Thank you very much. Perhaps the last question, what does the word \"generalize\" mean in the answer? I can find the meaning by searching the term(adding free type variables which do not appear in the current type environment to universal quantification), but I don't see what does this *actually* do(why it doesn't add type variables that appear in the current type environment?) and why this is needed here.\n- This answer may help: stackoverflow.com/a/904715/7203016. Note that even though the comments say that answer is wrong, they mean that it's not the correct answer to the question being asked, but it does address your question (and explains @PatJ's note about getting the timing of generalisation right). However, this is getting too complicated for comments. You might want to ask this as a separate question.","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":12,"totalLines":83,"estimatedTokens":914}}682{"id":"stack-53011395","source":"stackoverflow","questionId":53011395,"title":"What does a parser for C++ do until it can differentiate between comparisons and template instantiations?","tags":["c++","parsing","gcc","compiler-construction","clang"],"text":"Title: What does a parser for C++ do until it can differentiate between comparisons and template instantiations?\nTags: c++, parsing, gcc, compiler-construction, clang\nSource: Stack Overflow\n\nQuestion:\nAfter reading this question I am left wondering what happens (regarding the AST) when major C++ compilers parse code like this:\n\n```\nstruct foo \n{\n void method() { ac; }\n\n // a b c may be declared here\n};\n```\n\nDo they handle it like a GLR parser would or in a different way? What other ways are there to parse this and similar cases?\n\nFor example, I think it's possible to postpone parsing the body of the method until the whole struct has been parsed, but is this really possible and practical?\n\n========================================\n\nTop Answer:\nThe answer will obviously depend on the compiler, but the article How Clang handles the type / variable name ambiguity of C/C++ by Eli Bendersky explains how Clang does it. I will simply note some key points from the article:\n\nClang has no need for a lexer hack: the information goes in a single direction from lexer to parser\n\nClang knows when an identifier is a type by using a symbol table\n\nC++ requires declarations to be visible throughout the class, even in code that appears before it\n\nClang gets around this by doing a full parsing/semantic analysis of the declaration, but leaving the definition for later; in other words, it's lexed but parsed after all the declared types are available\n\n========================================\n\nCode:\n```text\nstruct foo \n{\n  void method() { a<b>c; }\n\n  // a b c may be declared here\n};\n```\n\n```text\n{}\n```\n\n```text\nOR\n```\n\n```text\na = b OR c\n```\n\n```text\nc\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\nc\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\nc\n```\n\n========================================\n\nComments:\n- \"Clang has no need for a lexer hack\". OK, so that's an improvement over having a lexer hack, but it sounds from your description like they replaced with it with a correspondingly icky parser hack. One man's opinion.\n- Although I accepted another answer, because it is more complete on its own, the article you provided is really insightful and I recommend reading it.\n- Very informative answer, though it could use some reference(s) about gcc/clang deferring the parse.\n- @panoskj: both compilers are open source; you can read the code yourself. I'm not going to do a full description of the parsing code here, particularly since I haven't looked at them in a while.\n- @rici: it wasn't clear you have read gcc/clang code, in which case I'll take your word for it.","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":93,"estimatedTokens":642}}683{"id":"stack-65017019","source":"stackoverflow","questionId":65017019,"title":"How to generate and manipulate JavaScript ESTree AST in TypeScript?","tags":["javascript","typescript","compiler-construction","code-generation","abstract-syntax-tree"],"text":"Title: How to generate and manipulate JavaScript ESTree AST in TypeScript?\nTags: javascript, typescript, compiler-construction, code-generation, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nThere is a ESTree spec describing JavaScript AST format, which is used in several libraries for JS code transformations (refactoring, transpiling, minification, etc).\n\nAre there any tools/libraries with API focused on **generating** the ESTree AST from scratch (not from existing JS code) and manipulate it? Preferably in TypeScript to get the advantage of static typing of ESTree nodes.\n\n========================================\n\nCode:\n```js\nimport { Program } from 'estree'\n\nconst ast: Program = {\n  type: 'Program'\n}\n```\n\n```text\n@types/estree\n```\n\n```text\nacorn\n```\n\n```text\nastring\n```\n\n========================================\n\nComments:\n- Have a look at compiler api github.com/microsoft/TypeScript/wiki/&hellip;\n- @AlekseyL. thanks, it says \"TypeScript AST\", but since it's a \"superset\", I should be able to use it for pure JS too, right?\n- I think so. Just don't generate \"type\" nodes if you don't need them.\n- TS shouldn't be ESTree compatible. To get it so, you might look at `@typescript-eslint`, though it seems it is focused more on parsing from source.\n- You might look into github.com/jamiebuilds/babel-handbook/blob/master/translatio&zwnj;&#8203;ns/&hellip; to conveniently generate Babel AST, and then use npmjs.com/package/babel-to-estree to convert it to ESTree, but it seems per their roadmap that Babel still has work to align their types fully with TS, so unless babel-to-estree does it, TS generation might be lacking.\n- Should be pretty easy with `estree-toolkit` - npmjs.com/package/estree-toolkit","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":41,"estimatedTokens":431}}684{"id":"stack-19906360","source":"stackoverflow","questionId":19906360,"title":"Static analysis of virtual generic method calls","tags":[".net","generics","static-analysis","compiler-construction"],"text":"Title: Static analysis of virtual generic method calls\nTags: .net, generics, static-analysis, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nXamarin is a system that compiles .NET code fully ahead-of-time (AOT) for platforms which disallow data execution and so can't have a JIT. This question is not about Xamarin, but is about an assertion that its documentation makes. It states here:\n\n Generic virtual methods support is limited, it's not possible to determine statically what method will be called in all circumstances so the compiler might leave out a few of them.\n\nI could be mistaken but they seem to be implying a broad statement about what is possible here through static analysis, not just what they've chosen to implement in their own software.\n\nAs Hans Passant aptly pointed out, the example they give doesn't actually demonstrate the problem they're referring to, so I've excluded it.\n\nSo, aside from these definitely intractable special cases:\n\nReflection;\n\ndynamically generated code (illegal in this scenario anyway);\n\npathological circular references in type arguments involving value types; and\n\nforeign assemblies;\n\nwhat would make an AOT compiler unable to handle the virtual generic method case?\n\n========================================\n\nTop Answer:\nI fundamentally agree with your analysis.\n\nXamarin and similar tools could go about this in reverse order, so to speak, by including every conceivable combination of instance type and virtual method signature and then go on to remove only the ones that are provably not needed. \n\nThey probably tried that, though, and found that the resulting code does tend to become quite large/slow for complex object hierarchies.","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":33,"estimatedTokens":425}}685{"id":"stack-14176883","source":"stackoverflow","questionId":14176883,"title":"How does each browser expose intermediary representation of JavaScript?","tags":["javascript","compiler-construction","interpreter"],"text":"Title: How does each browser expose intermediary representation of JavaScript?\nTags: javascript, compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\n**Question:**\n\nI've heard people use the terms dynamic compilation and interpretation interchangeably. This isn't a question about semantics.\n\nObviously by the time the code hits the processor it is in some binary form.\n\nHow can I look at intermediate represenation?\n\nFor example, it is pretty easy to look at your C code in assembly by using compiler options.\n\nIs there a similar way to do this with JavaScript? I'm not sure what the intermediary representation would be called but here are some general references.\n\n**Research**\n\nInterpreter (Wikipedia)\n\nDynamic Compilation (Wikipedia)\n\nCompilation (Wikipedia)\n\n**Clarification:**\n\nThis question has nothing to do with minification.\n\n========================================\n\nTop Answer:\nSee if these slides from Vyacheslav Egorov's mö.js JSConf talk get you any closer to what you need.\n\n========================================\n\nCode:\n```text\nd8 --print_code\n```\n\n```text\nd8 --print_opt_code\n```\n\n========================================\n\nComments:\n- If I may ask, why do you want to find a way to do this?\n- So, you mean optimization?\n- For V8, you can run `d8 --print_code` or `d8 --print_opt_code`\n- There is no one standard, universally agreed-upon internal representation for JavaScript and each JS engine interprets or compiles JS in its own way. You are probably best off picking a nice JS engine like Rhino and checking the docs to see if it can do what you want it to do.\n- In what environment do you wish to do this? What makes you think the intermediate representation will always be available across JS implementations? There's no reason a JS engine would need to expose anything a person could look at; a JS engine may be implemented in any number of ways.\n- @templatetypedef - that comment seems like an answer to me :)\n- I would assume that each browser compiles Javascript source into some kind of internal tree-like data structure, or even possibly something resembling proprietary bytecode. In any case, it's in an in-memory format that probably does not make for easy viewing or decoding by humans.\n- Newer versions of Safari compile directly to \"native machine code\" - en.wikipedia.org/wiki/WebKit#JavaScriptCore\n- Given there's lot of internal possible representations, what do you hope to learn by looking at a randomly chosen one?","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":57,"estimatedTokens":619}}686{"id":"stack-23309082","source":"stackoverflow","questionId":23309082,"title":"Global type inference in the Scheme compiler Stalin","tags":["types","compiler-construction","scheme","hindley-milner"],"text":"Title: Global type inference in the Scheme compiler Stalin\nTags: types, compiler-construction, scheme, hindley-milner\nSource: Stack Overflow\n\nQuestion:\nI am looking into the Scheme compiler Stalin. It is big and complex. Also, if I understood correctly, the author was planning to write a series of papers detailing aspects of the implementation, but never got around to doing that.\n\nThe aspect of Stalin I am interested in is global type inference: deducing the types of things based on their usage in other places in the program. Does Stalin indeed do that? If yes, how, and where in its code base? Does it use a variant / extension of a Hindley-Milner algorithm?\n\n========================================\n\nCode:\n```text\nsource/stalin3b.sc\n```\n\n========================================\n\nComments:\n- Have you seen this Q/A pair over at cstheory.SE? It basically suggests that Stalin doesn't need to build \"up\" from types as such, it already infers *everything* about the value and its usage.\n- @Leushenko thanks! I think you are right: it seems that this compiler \"skips\" the concept of types, and works on primitive data type dispatches.","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":21,"estimatedTokens":285}}687{"id":"stack-1742688","source":"stackoverflow","questionId":1742688,"title":"How do modern optimizing compilers determine when to optimize?","tags":["optimization","gcc","compiler-construction","compiler-optimization"],"text":"Title: How do modern optimizing compilers determine when to optimize?\nTags: optimization, gcc, compiler-construction, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nHow do modern optimizing compilers determine when to apply certain optimizations such as loop unrolling and code inlining?\n\nSince both of these affect caching, naively inlining functions with less than X lines, or whatever other simple heuristic, is likely to generate worse performing code. So, how do modern compilers deal with this?\n\nI'm having a hard time finding information on this (especially information thats reasonably easy to understand..), about the best I could find is the wikipedia article. Any details, links to books/articles/papers are greatly appreciated!\n\nEDIT: Since answers are talking mainly about the two optimizations I mentioned (inlining and loop unrolling) I just wanted to clarify that I'm interested in all and any compiler optimizations, not just those two. I'm also more interested in the optimizations which can be performed during ahead-of-time compilation, though JIT optimization is of interest too (though to a slightly lesser extent).\n\nThanks!\n\n========================================\n\nTop Answer:\nYou can look a the Spiral project.\n\nOn top of that, optimizing is a tough thing to do generically. This is, in part, why there are so many options to the gcc compiler. If you know something about cache and pages you can do some things by hand and request that others be done through the compiler but no two machines are the same so the approach must be adhoc.\n\n========================================\n\nComments:\n- JIT does not always have the time for some massive optimizations, and codepaths sometimes vary massively on the data, so profile guided optimizations are not even always the right path.\n- Good link, but this only discusses micro optimizations.","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":27,"estimatedTokens":468}}688{"id":"stack-50667375","source":"stackoverflow","questionId":50667375,"title":"Does a Symbol Table store AST (Declaration)Nodes or are the \"Symbols\" different objects/classes?","tags":["c#","compilation","abstract-syntax-tree","symbol-table","compiler-construction"],"text":"Title: Does a Symbol Table store AST (Declaration)Nodes or are the \"Symbols\" different objects/classes?\nTags: c#, compilation, abstract-syntax-tree, symbol-table, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have a few things about the AST / Symbol Table relation that i don't understand.\n\nI currently have a AST implemented in C# which has nodes for variable declarations (these contain informations about the name, type, source position, a possible constant value as expression node, etc).\n\nNow i want to fill a symbol table (using the visitor pattern on my AST) but my question is: are the \"symbols\" new classes for example VariableSymbol or does the symbol table directly store the VariableDeclarationNode from the AST? \n\nIF the symbols are new classes, then who would store the evaluated expression value for constant variables, the VariableDeclarationNode or the VariableSymbol or somewhere else?\n\n(I have seen some interpreter examples and they store all variable values, including constants, in a additional hash table, but im working on a source-to-source compiler and not a interpreter, so im not sure where you store the evaluated constants in this case. Sorry i know these a kinda multiple questions)\n\n========================================\n\nCode:\n```text\nclass Symbol : ISymbol {\n    ASTNode DeclaringNode;\n    SymbolType RuntimeType;\n    bool InitializeAsConstant;\n    RuntimeValue ConstantValue;\n\n    ...\n}\n```\n\n```text\n/* fantasy source language */\nConstant $$IAMCONSTANT :=> /03\\ MUL /02\\ KTHXBYE\n\n/* target language */\nconst int IAMCONSTANT = 3 * 2;\n\n/* as opposed to compilation stage 1 precomputed */\nconst int IAMCONSTANT = 6;\n```\n\n========================================\n\nComments:\n- if you are working on a source-to-source compiler, why would you even want to store the evaluated expression value of constant expressions? it's about translating to the syntax of the target language, not interpreting or maintaining any runtime state. could you give an example on what your code does/is supposed to do once you have the declaration/initialization node of a variable? Storing a reference to the node that declares a symbol in the object you use as a basis for target code emission is not right or wrong per se, it depends on the architecture you have in mind.\n- My current architecture is a scanner and parser to generate the AST, my goal is to convert the AST to C code, but only if the code is \"valid\" for example not multiple variables with the same name. I saw some blogs/tutorials and they describe that i can build a scope/symbol table out of the AST to do the semantic checks. The examples that i found do both have a visitor pattern to visit all variable declarations and add them to the scope/symbol table but some examples store the AST node (VariableDeclarationNode in this example) and other examples create a new class for example VariableSymbol\n- That's why im not sure which solution is better, if i should directly add the AST nodes to the scope/table or if i should create new classes for the symbols. You are right about the constant expressions, sadly i need to evaluate them for a kinda \"stupid\" / legacy reason, it's not allowed to have multiple variables with the same name but there is a special rule, if both of the variables are constants and have the same value then there should be no error message (but only one variable is used) that's why i have to evaluate and store the value of constant variables for this special legacy case\n- I wanted to add that this is currently for a source-to-source compiler but the AST might be used for other things in the future because we maybe add a interpreter later, that's why i want to make sure that im using a good architecture, because this is the first time that im doing anything with a AST / Compiler\n- I see. still probably a) a matter of opinion or b) driven by design considerations not sufficiently clear from your post: if the information in the AST node is *sufficient for the task* then you're good with just storing references in scope tree/table. if you interpret from syntax tree instead of just emitting code, then you need sophisticated data structures where the existence of a reference to original AST nodes is a secondary concern (c'ued)\n- I did both and both work. Not keeping references to \"primitive\" AST nodes at stages beyond lexing & parsing is a cleaner approach; may also depend on which library you use and show code otherwise it gets closed as off topic sooner or later.\n- We created the scanner/parser without a library (following blogs/book tutorials). Btw thanks for your comments so far! I think you answered my main question, can you maybe post your previous comment (were you said that both is possible and it depends on the task etc) as answer then i can accept it? I have one last offtopic question, after you comments i think that i will seperate them and create additional symbol classes, do you think it would be dirty to store the evaluated constant values (for the special case) in the symbol classes or should i create an additonal table for these?","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":50,"estimatedTokens":1275}}689{"id":"stack-15322958","source":"stackoverflow","questionId":15322958,"title":"Creating a Programming Language","tags":["compiler-construction","programming-languages"],"text":"Title: Creating a Programming Language\nTags: compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nI was wondering how professional programmers create their own programming languages.\n\nDo they only create a compiler which reads a text file and makes an executable out of it (considering there are no syntax errors etc)?\n\nI'm not planning on creating my own programming language (I'm obviously too unexperienced for that) , I just wanna know how they do it.\n\n========================================\n\nTop Answer:\nYou should read up on compiler construction. Some of the main areas include:\n\n- Lexical analysis\n\n- Syntax analysis\n\n- Semantic analysis\n\n- Code optimisation\n\n========================================\n\nComments:\n- I reckon the first 3 points can be omitted by going for a \"syntax free\" programming language, which allows the developer to define program structure directly instead of the compiler based on analysis. I've been trying to do something similar myself, but I am pretty green when it comes to programming.\n- Wait, you list code *optimization* but neither code *generation* nor *interpretation*? As in, the things that make the program do anything at all?\n- I noticed, but why leave off absolutely vital topics yet list other, completely optional (sometimes not even applicable) parts?\n- @delnan - the way I learned compiler construction was by using the techniques I have mentioned in my answer. If you disagree or have a different view on how to approach this, feel free to post your own answer.\n- Thank you! I watched a few videos now and I think that will help me understand it. First I thought \"why the heck a browser? I wanna learn / understand how to make programming languages\", but then I thought about it and realized just what you said.","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":32,"estimatedTokens":448}}690{"id":"stack-15912621","source":"stackoverflow","questionId":15912621,"title":"Optimization of subsequent calls to integer division and modulo (remainder)","tags":["c","optimization","compiler-construction","compiler-optimization"],"text":"Title: Optimization of subsequent calls to integer division and modulo (remainder)\nTags: c, optimization, compiler-construction, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nInteger division `/` and modulo `%` operations are often used together in programming, sometimes even on the same operands and in subsequent lines. For example, the following C function, which is a simple function that sums the result of a `/` of 2 numbers with the result of their `%`, does just that: \n\n```\nint sum2digits(int x, int base) {\n int n, m;\n n = x / base;\n m = x % base;\n return n + m;\n}\n```\n\nAs I know, both `/` and `%` are performed by the same machine instruction (in x86). Say, if you perform a machine instruction for integer division (`div` or `idiv`) of two numbers, `a` and `b`, then afterwards the value of `a / b` will be stored in the register EAX and the remainder `a % b` in EDX.\n\nI wondered whether the compiler takes advantage of this quality and took a look at the assembly code. It turns out that normal compilation with gcc doesn't optimize this: \n\n```\npush %rbp\nmov %rsp,%rbp\nmov %edi,-0x14(%rbp)\nmov %esi,-0x18(%rbp)\nmov -0x14(%rbp),%eax\nmov %eax,%edx\nsar $0x1f,%edx\nidivl -0x18(%rbp)\nmov %eax,-0x8(%rbp)\nmov -0x14(%rbp),%eax\nmov %eax,%edx\nsar $0x1f,%edx\nidivl -0x18(%rbp)\nmov %edx,-0x4(%rbp)\nmov -0x4(%rbp),%eax\nmov -0x8(%rbp),%edx\nadd %edx,%eax\npop %rbp\nretq\n```\n\nThis assembly code does 2 subsequent calls to idivl, but each time reads the result from another register (EAX for quotient, EDX for remainder).\nHowever, compiling with the `-O` changes the picture: \n\n```\nmov %edi,%eax\nmov %edi,%edx\nsar $0x1f,%edx\nidiv %esi\nadd %edx,%eax\nretq\n```\n\nThis code calls `idiv` only once, and uses its value for both computations.\n\nWhy isn't this kind of optimization a default? What is the use of calling `div` twice in a row? Can this optimization change the behaviour of a program in any way?\n\n**Also, and perhaps even more important,** is there a way, as a programmer, to manually extract these 2 values (quotient and remainder) guaranteeing that only 1 integer division is performed by the CPU?\n\n========================================\n\nTop Answer:\nYou could always implement your own division:\n\n```\n#include \n#include \n\nvoid mydiv(int dividend, int divisor, int* quotient, int* remainder)\n{\n *quotient = dividend / divisor;\n *remainder = dividend - *quotient * divisor;\n}\n\nint testData[][2] =\n{\n { +5, +3 },\n { +5, -3 },\n { -5, +3 },\n { -5, -3 },\n};\n\nint main(void)\n{\n unsigned i;\n for (i = 0; i Output (ideone):\n\n```\n+5/+3 = +1:+2 == +1:+2\n+5/-3 = -1:+2 == -1:+2\n-5/+3 = -1:-2 == -1:-2\n-5/-3 = +1:-2 == +1:-2\n```\n\nThis does have one division. However, it looks like gcc isn't smart enough to eliminate the multiplication and so you have one of each.\n\n========================================\n\nCode:\n```text\nint sum2digits(int x, int base) {\n    int n, m;\n    n = x / base;\n    m = x % base;\n    return n + m;\n}\n```\n\n```text\npush   %rbp\nmov    %rsp,%rbp\nmov    %edi,-0x14(%rbp)\nmov    %esi,-0x18(%rbp)\nmov    -0x14(%rbp),%eax\nmov    %eax,%edx\nsar    $0x1f,%edx\nidivl  -0x18(%rbp)\nmov    %eax,-0x8(%rbp)\nmov    -0x14(%rbp),%eax\nmov    %eax,%edx\nsar    $0x1f,%edx\nidivl  -0x18(%rbp)\nmov    %edx,-0x4(%rbp)\nmov    -0x4(%rbp),%eax\nmov    -0x8(%rbp),%edx\nadd    %edx,%eax\npop    %rbp\nretq\n```\n\n```text\nmov    %edi,%eax\nmov    %edi,%edx\nsar    $0x1f,%edx\nidiv   %esi\nadd    %edx,%eax\nretq\n```\n\n```text\n/\n```\n\n```text\n%\n```\n\n```text\n/\n```\n\n```text\n%\n```\n\n```text\n/\n```\n\n```text\n%\n```\n\n```text\ndiv\n```\n\n```text\nidiv\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\na / b\n```\n\n```text\na % b\n```\n\n```text\n-O\n```\n\n```text\nidiv\n```\n\n```text\ndiv\n```\n\n```text\ndiv\n```\n\n```text\n#include <stdlib.h>\n#include <stdio.h>\n\nvoid mydiv(int dividend, int divisor, int* quotient, int* remainder)\n{\n  *quotient = dividend / divisor;\n  *remainder = dividend - *quotient * divisor;\n}\n\nint testData[][2] =\n{\n  { +5, +3 },\n  { +5, -3 },\n  { -5, +3 },\n  { -5, -3 },\n};\n\nint main(void)\n{\n  unsigned i;\n  for (i = 0; i < sizeof(testData)/sizeof(testData[0]); i++)\n  {\n    div_t res1, res2;\n    res1 = div(testData[i][0], testData[i][1]);\n    mydiv(testData[i][0], testData[i][1], &res2.quot, &res2.rem);\n    printf(\"%+d/%+d = %+d:%+d %c= %+d:%+d\\n\",\n           testData[i][0], testData[i][1],\n           res1.quot, res1.rem,\n           \"!=\"[res1.quot == res2.quot && res1.rem == res2.rem],\n           res2.quot, res2.rem);\n  }\n  return 0;\n}\n```\n\n```text\n+5/+3 = +1:+2 == +1:+2\n+5/-3 = -1:+2 == -1:+2\n-5/+3 = -1:-2 == -1:-2\n-5/-3 = +1:-2 == +1:-2\n```\n\n========================================\n\nComments:\n- By default, GCC disables ***all*** optimizations unless you specify a `-O`. Not just this one...\n- I've deleted my answer, since I couldn't get it to work. On the other hand, if you say `return x &#47; base + x % base;` and enable compiler optimizations, you should get the efficient implementation.\n- With GCC you can use inline assembly to guarantee that you get both results from one division, but I can't be bothered to look up the details, and I haven't used inline assembly for quite a while...\n- You definitely want this function to inline so `base` will be a compile-time constant, letting the compiler use a multiplicative inverse instead of an expensive `idiv` at all. Also, are you sure you want to use signed integers? Does a negative result make sense?\n- \"However, it looks like gcc isn't smart enough to eliminate the multiplication\" - and this, folks, is why naive \"inefficient\" source code always wins over hand-optimized \"efficient\" source code (over long enough time scales). The simpler, clearer code directly expresses its intent/behavior and lowers the bar for optimization heuristics, while the optimized code obfuscates the intent in the side-effects of whatever is currently available in the language and is most efficient in modern hardware at the time and presents a higher bar for optimization heuristics.\n- The specific reasons GCC `-O0` doesn't optimize to one `idiv` here is that `-O0` stops it from optimizing across C statements. If you were single-stepping with a debugger, you could change the value of `base` between the two statements. See Why does clang produce inefficient asm with -O0 (for this simple floating point sum)? for more about the \"consistent debugging\" that `-O0` gives, including being able to jump to any source line within the function.\n- Josh, I've upvoted your answer - though you're probably not still annoyed 18 months later... I've arrived here looking for talk about `div(.,.)`. I'm cool with the arguments that modern optimized compilers make it rather/potentially unnecessary but it's there and it's not harmful. You answered the original question. I like answers that say 'here's your answer but how about these other thoughts that might be useful'. I get annoyed with 'You don't want to be doing that' or 'Why would you do that?' answers unless what you're doing is positively bad.\n- I think an important point in favor of this answer is that it is much easier to write a compiler optimization that replaces calls to a standard function which implements a specific behavior with efficient inline machine code than it is to write a compiler optimization that detects that two or more operations next to each other together implement that same behavior.","metadata":{"transformedAt":"2026-08-18T18:32:17.819Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":21,"totalLines":258,"estimatedTokens":1825}}691{"id":"stack-44141686","source":"stackoverflow","questionId":44141686,"title":"How to make C language context-free?","tags":["c","parsing","compiler-construction","lexical-analysis"],"text":"Title: How to make C language context-free?\nTags: c, parsing, compiler-construction, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nI know that **C** is not a context-free language, a famous example is:\n\n```\nint foo;\ntypedef int foo;\nfoo x;\n```\n\nIn this case the lexer doesn't know, whether `foo` in the 3rd line, is an identifier, or `typedef`.\n\nMy question is, is this the only reason that makes **C** a *Context-Sensitive Language*?\n\nI mean, if we get rid of `typedef`, would it become context-free language? Or there are other reasons (examples) that prevent it from being so?\n\n========================================\n\nTop Answer:\nNo. C cannot be a strict context independent language. For that, you should describe a syntax that doesn't allow to use a nondeclared variable (this is context) in a similar way as what you describe in your question. The language authors always describe syntax using some kind of context free grammar, but just to describe the main syntactic constructs of the language. The case you describe (making a type identifier to fit in a different token class to be able to go in places where it shouldn't) is only an example. If you look for example, the freedom in the order for things like `static unsigned long long int variable` simplifies the syntax remembering by programmers, but complicates things to the compiler authors.\n\n========================================\n\nCode:\n```text\nint foo;\ntypedef int foo;\nfoo x;\n```\n\n```text\nfoo\n```\n\n```text\ntypedef\n```\n\n```text\ntypedef\n```\n\n```text\ncheck_type()\n```\n\n```text\ncomment()\n```\n\n```text\ntypedef\n```\n\n```text\ntypedef\n```\n\n```text\nstatic unsigned long long int variable\n```\n\n========================================\n\nComments:\n- If you remove such things, you no longer have C, but a completely new language that is *similar* to (but not compatible with) C.\n- `+` depends on context: in `40 + 2` it means integer addition; in `40 + 2.0` it means floating point addition.\n- @pmg The parser doesn't care what the types of things are.\n- @sepp2k On the other hand `+` (and `-`) are context-sensitive since they can both mean two different operators depending on context: Unary or binary plus/minus. So from the lexers point of view it's impossible to say which operator is meant.\n- @BiteBytes The example doesn't really work. If `foo` weren't a typedef, `foo x;` would simply be an error, so there's no syntactic ambiguity there. I mean sure, the lexer can't tell whether `foo` is an identifier or a typename, but the example doesn't explain why it would need to know that (in fact the lexer doesn't necessarily have to know that - that's just one possible way of solving the problem). The common example would be the expression `(foo)(x)`, which could either be a function call or a cast, depending on whether `foo` is a type or not.\n- @Someprogrammerdude That doesn't depend on context. I can easily parse unary and binary operators with a context free grammar.\n- @Someprogrammerdude Still, any construct involving these operators can be described using CFG\n- @sepp2k True, but on the other hand the OP seems more worried about it from a lexical point of view (which doesn't really involve the grammar). Some clarification from the OP would be nice...\n- @Someprogrammerdude \"OP seems more worried about it from a lexical point of view\" The distinction between unary and infix operators matters even less on a lexical level. A plus sign is just a plus sign. Also the term \"context free\" doesn't really have a meaning at the lexical level, so I see no way to interpret the question like that. The way I see it, the OP talked about the lexer because the cast-vs.-function-call ambiguity is often solved with a lexer hack that distinguishes typenames from other identifiers, not because the question is about lexing per se.\n- I don't think the words you've used accurately reflect the question you really mean to ask. Nothing in the formal definition of a context-free language requires that every valid sentence in the language have a unique production from terminal symbols. You seem to be looking for constructs whose interpretation in terms of grammar rules is unambiguous, but the existence of such constructs does not make the language context-sensitive.\n- @JohnBollinger: It's true (and important) that a CFG can be ambiguous. But the question is whether C is described by a CFG, and for that we need to be clear on what we mean by \"C\". For example, `int foo; int bar = (foo)(3);` is not valid C, while `typedef int foo; int bar = (foo)(3);` is valid. There is an argument (which I don't totally agree with) that we can ignore that error as \"semantic\", but there is no doubt that the C standard forbids the first excerpt, so that the best we can say is that the (potential) CFG describes a *superset* of C. (Ignoring the preprocessor.) ...\n- @JohnBollinger: ... that ends up with the claim that C is (kind of) context free because a superset of C can be described with a CFG. What is the superset? Why, it is exactly the smallest language which includes C and is context-free (because we've eliminated all the \"semantic\" errors which are nonetheless inherent in the program text). The fact that the smallest context-free superset of C is context-free is tautological. But is it interesting? (Perhaps I should edit that pair of comments into an answer, but I feel like I've written it all before.)\n- If you ignore the preprocessor :-)\n- It is wrong answer. In C when you call function you need to check argument list to match declaration. This is context-sensitive. In any CFG of C including cited, call with wrong number of arguments is accepted.\n- @KonstantinVladimirov Yes, typechecking (semantic check) is context sensitive. The syntaxt is almost context-free.\n- Even checking number of arguments is context-sensitive.\n- @KonstantinVladimirov That falls under type checking.","metadata":{"transformedAt":"2026-08-18T18:32:17.820Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":85,"estimatedTokens":1466}}692{"id":"stack-13311029","source":"stackoverflow","questionId":13311029,"title":"Writing an extremely simple parser","tags":["c#","parsing","compiler-construction","scripting"],"text":"Title: Writing an extremely simple parser\nTags: c#, parsing, compiler-construction, scripting\nSource: Stack Overflow\n\nQuestion:\nI'm writing a very basic web server that has to support an extremely limited special server side scripting language. Basically all I need to support is \"echo\", addition/subtraction/multiplication (no division) with only 2 operands, a simple \"date()\" function that outputs the date and the use of the \"&\" operator to concatenate strings.\n\nAn example could be:\n\n```\necho \"Here is the date: \" & date();\necho \"9 x 15 = : & 9*15;\n```\n\nI've gone through and created the code necessary to generate tokens, but I'm not sure I'm using the right tokens.\n\nI created tokens for the following:\n\n```\nECHO - The echo command\nWHITESPACE - Any whitespace\nSTRING - A string inside quotations\nDATE - The date() function\nCONCAT - the & operator for concatenation\nMATH - Any instance of binary operation (5+4, 9*2, 8-2, etc)\nTERM - The terminal character (;)\n```\n\nThe MATH one I am particularly unsure about. Typically I see people create a token specifically for integers and then for each operator as well, but since I ONLY want to allow binary operations, I thought it made sense to group it into one token. If I were to do everything separately, I would have to do some extra work to ensure that I never accepted \"5+4+1\".\n\nSo question 1 is am I on the right track with which tokens to use?\n\nMy next question is what to I do with these tokens next to ensure correct syntax? The approach that I had thought of was to basically say, \"Okay I know I have this token, here is a list of tokens that are allowed to come next based on the current token. Is the next token in the list?\"\n\nBased on that, I made a list of all of my tokens as well as what tokens are valid to appear directly after them (didn't include whitespace for simplicity).\n\n```\nECHO -> STRING|MATH|DATE\nSTRING -> TERM|CONCAT\nMATH -> TERM|CONCAT\nDATE -> TERM|CONCAT\nCONCAT -> STRING|MATH|DATE\n```\n\nThe problem is I'm not sure at all how to best implement this. Really I need to keep track of whitespace as well to make sure there are spaces between the tokens. But that means I have to look ahead two tokens at a time which is getting even more intimidating. I also am not sure how to manage the \"valid next tokens\" stuff without just some disgusting section of if blocks. Should I be checking for valid syntax before trying to actually execute the script, or should I do it all at once and just throw an error when I reach an unexpected token? In this simple example, everything will always work just fine parsing left to right, there's no real precedence rules (except the MATH thing, but that's part of why I combined it into one token even though it feels wrong.) Even so, I wouldn't mind designing a more scalable and elegant solution.\n\nIn my research about writing parsers, I see a lot of references to creating \"accept()\" and \"expect()\" functions but I can't find any clear description of what they are supposed to do or how they are supposed to work.\n\nI guess I'm just not sure how to implement this, and then how to actually come up with a resulting string at the end of the day.\n\nAm I heading in the right direction and does anybody know of a resource that might help me understand how to best implement something simple like this? I am required to do it by hand and cannot use a tool like ANTLR.\n\nThanks in advance for any help.\n\n========================================\n\nTop Answer:\n`expect` is what your parser does to get the next token, and fails if the token isn't a proper following token. To begin with, your parser `expects` *ECHO* or *WHITESPACE*. Those are the only valid starting terms. Having seen \"ECHO\", your parser `expects` one of *WHITESPACE|STRING|MATH|DATE*; anything else is an error. And so on.\n\n`accept` is when your parser has seen a complete \"statement\" - *ECHO*, followed by a valid sequence of tokens, followed by *TERM*. Your parser now has enough information to process your *ECHO* command.\n\nOh, and hand-written parsers (especially simple ones) are very often disgusting collections of `if` blocks (or moral equivalents like `switch` statements) :) Further up the line of elegant-ness would be some kind of state machine, and further up from that is a grammar generator like yacc or GOLD Parser Generator (which in turn churn out ugly `if`, `switch`, and state machines for you).\n\n**EDIT** to provide more details.\n\nTo help sort out responsibilities, create a \"lexer\" whose job is to read the input and produce tokens. This involves deciding what tokens look like. An easy token is the word \"echo\". A less easy token is a math operation; the token would consist of one or more digits, an operator, and one or more digits, with no whitespace between. The lexer would take care of skipping whitespace, as well as understanding a quoted string and the characters that form the date() function. The lexer would return two things - the type of token read and the value of the token (e.g., \"MATH\" and \"9*15\").\n\nWith a lexer in hand to read your input, the parser consumes the tokens and ensures they're in a proper order. First you have to see the *ECHO* token. If not, fail with an error message. After that, you have to see *STRING*, *DATE*, or *MATH*. If not, fail with an error message. After that, you loop, watching for either *TERM*, or else *CONCAT* followed by another *STRING*, *DATE*, or *MATH*. If you see *TERM*, break the loop. If you see neither *TERM* nor *CONCAT*, fail with an error message.\n\nYou can process the ECHO command as you're parsing, since it's a simple grammar. Each time you find a *STRING*, *DATE* or *MATH*, evaluate it and concatenate it to what you already have. When you find *TERM*, exit the function and return the built-up string.\n\nQuestions? Comments? Omelets? :)\n\n========================================\n\nCode:\n```text\necho \"Here is the date: \" & date();\necho \"9 x 15 = : & 9*15;\n```\n\n```text\nECHO - The echo command\nWHITESPACE - Any whitespace\nSTRING - A string inside quotations\nDATE - The date() function\nCONCAT - the & operator for concatenation\nMATH - Any instance of binary operation (5+4, 9*2, 8-2, etc)\nTERM - The terminal character (;)\n```\n\n```text\nECHO        ->      STRING|MATH|DATE\nSTRING      ->      TERM|CONCAT\nMATH        ->      TERM|CONCAT\nDATE        ->      TERM|CONCAT\nCONCAT      ->      STRING|MATH|DATE\n```\n\n```text\necho \"Here is the date: \" & date();\n```\n\n```text\nexpect\n```\n\n```text\nexpects\n```\n\n```text\nexpects\n```\n\n```text\naccept\n```\n\n```text\nif\n```\n\n```text\nswitch\n```\n\n```text\nif\n```\n\n```text\nswitch\n```\n\n========================================\n\nComments:\n- You are in luck my friend, someone already did the hard part. irony.codeplex.com\n- You may also use Javascript stackoverflow.com/questions/12118077/&hellip;\n- @asawyer, I think you missed the part *\"I am required to do it by hand and cannot use a tool like ANTLR\"*, so Irony is most probably also not allowed...\n- @BartKiers Maybe, but since it generates a normal c# assembly I thought it might be workable.\n- Is this homework? It's fine if it is, but you should use the homework tag.\n- Yeah it's part of a project in school. I just went to add the tag but it says the tag is obsolete and should not be used so I haven't added it yet. If it's still in use though I will certainly add it!\n- See my answer on how to build recursive descent parsers. They're actually pretty easy to build. See stackoverflow.com/questions/2245962/&hellip;\n- Why are you going to make it complicated? Use seperate tokens, keep it simple and keep it smart, easy as pie.\n- Thank you David that is definitely helpful. What I'm still struggling with is how this loop that iterates across the tokens works. What would an actual implementation of \"expect\" look like for example? After WHITESPACE, what I'm expecting really depends on what the token before WHITESPACE was, which is where it gets confusing. How is \"expect\" actually used, is it a function that takes a list of possible tokens as a parameter and returns true/false? Does it also change my \"current\" token to be whatever \"expect\" just read, then I call expect again? Just confused about the implementation :/\n- Thank you very much this is definitely helpful. I am going to take a stab at it this afternoon and if I'm still having problems I'll take you up on the sample code offer. Thanks again!","metadata":{"transformedAt":"2026-08-18T18:32:17.820Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":12,"totalLines":148,"estimatedTokens":2092}}693{"id":"stack-9333726","source":"stackoverflow","questionId":9333726,"title":"How do I use an exisiting function pass from my LLVM - pass?","tags":["c++","compiler-construction","llvm","clang"],"text":"Title: How do I use an exisiting function pass from my LLVM - pass?\nTags: c++, compiler-construction, llvm, clang\nSource: Stack Overflow\n\nQuestion:\nI have been using LLVM and I was confused how to use a different already present pass from my own pass ? Precisely my program needs Dominance Frontier Calculation for any given instruction. LLVM already has the Dominance function Class that is implemented as a function pass. How can i invoke it/make use of it in my Module Pass ?\n\n========================================\n\nCode:\n```text\n#include \"llvm/Analysis/DominanceFrontier.h\"\n```\n\n```text\nvirtual void getAnalysisUsage(AnalysisUsage &AU) const { }\n```\n\n```text\nAU.addRequired<DominanceFrontier>();\n```\n\n```text\nDominanceFrontier *DF = &getAnalysis<DominanceFrontier>();\n```\n\n```text\nBasicBlock *BB = /* some BB */;\n    DominanceFrontier::iterator DFI = DF->find(BB);\n```\n\n```text\nrunOnFunction\n```\n\n========================================\n\nComments:\n- Have you checked the docs? Because the last time I checked, the answer was there: llvm.org/docs/WritingAnLLVMPass.html\n- Add `AU.addRequired();` to your pass `getAnalysisUsage(AnalysisUsage &AU)` method.\n- The documentation is really obscure and I had real problems deciphering it. An example would have helped.\n- What is your version of LLVM? What kind of information do you want to get?","metadata":{"transformedAt":"2026-08-18T18:32:17.820Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":42,"estimatedTokens":337}}694{"id":"stack-7048268","source":"stackoverflow","questionId":7048268,"title":"Where can I find the explicit grammar for C90 and C99?","tags":["c","compiler-construction"],"text":"Title: Where can I find the explicit grammar for C90 and C99?\nTags: c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm looking to write a small C compiler, and I can't seem to find the grammar for C99 anywhere. I've found the C90 and C99 standards document, but I can't find the grammar itself anywhere.\n\n========================================\n\nComments:\n- Indeed there are. Typedef names effectively become keywords after they've been defined, but they stop being keywords when they go out of scope. That means that you need some sort of feedback from the symbol table to the parser, something that (I think) most other languages don't require. Just one example: you can't parse `sizeof (foo[10])` without knowing whether `foo` is a typedef or not. (Historically, this is because typedefs were a relatively late addition to the language -- pre-K&R, but after the type system had been defined.)\n- ... and even after handling the typedef issue, parsing possibly-abstract declarators is *surprisingly* complex. As a graduate student, I wrote a C parser for a research project, and as I attempted to figure out how do do it correctly I discovered counterexamples for each and every of the existing implementations I could dig up, including GCC's parser at that time. Not that I can remember any of the counterexamples now, nor other details. They were hairy.\n- Well I'll be damned, now I feel dumb. As for the context sensitivity of the grammar, I'm going to go ahead and ignore that for now... I'm not writing a fully featured compiler yet...\n- Wikipedia no longer links to the C99 standard.","metadata":{"transformedAt":"2026-08-18T18:32:17.820Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":14,"estimatedTokens":401}}695{"id":"stack-9124893","source":"stackoverflow","questionId":9124893,"title":"Add keyword to Objective-C using Clang","tags":["objective-c","compiler-construction","llvm","keyword","clang"],"text":"Title: Add keyword to Objective-C using Clang\nTags: objective-c, compiler-construction, llvm, keyword, clang\nSource: Stack Overflow\n\nQuestion:\nHow would I go about adding a relatively trivial keyword to Objective-C using the Clang compiler? For example, adding a literal `@yes` which maps to `[NSNumber numberWithBool:YES]`.\n\nI have looked at the (excellent) source code for Clang and believe that most of the work I would need to do is in `lib/Rewrite/RewriteObjC.cpp`. There is the method `RewriteObjC::RewriteObjCStringLiteral` (see previous link) which does a similar job for literal `NSString *` instances. \n\nI ask this question as Clang is very modular and I'm not sure which `.td` (see `tablegen`) files, `.h` files and AST visitor passes I would need to modify to achieve my goal.\n\n========================================\n\nCode:\n```text\n@yes\n```\n\n```text\n[NSNumber numberWithBool:YES]\n```\n\n```text\nlib/Rewrite/RewriteObjC.cpp\n```\n\n```text\nRewriteObjC::RewriteObjCStringLiteral\n```\n\n```text\nNSString *\n```\n\n```text\n.td\n```\n\n```text\ntablegen\n```\n\n```text\n.h\n```\n\n```text\nExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {\n...\n        return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));\n...\n}\n```\n\n========================================\n\nComments:\n- Why would you want to do it? You want your code compilable by your tweaked compiler only?\n- @Krizz To learn more about the inner workings of Clang, that's why! It's an educational exercise :)\n- If you just want @yes without diving into these Clang's internals, see this: stackoverflow.com/a/17837531/598057","metadata":{"transformedAt":"2026-08-18T18:32:17.820Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":9,"totalLines":60,"estimatedTokens":399}}696{"id":"stack-34407845","source":"stackoverflow","questionId":34407845,"title":"Can JavaCC distinguish token by its context?","tags":["java","compiler-construction","token","javacc"],"text":"Title: Can JavaCC distinguish token by its context?\nTags: java, compiler-construction, token, javacc\nSource: Stack Overflow\n\nQuestion:\nBasic requirement is use keyword as identifier, so I want to distinguish the token from it's context.(e.g.`class` is a keyword, but we allowed a variable named `class`).\n\nIn java, this is possible, but it's so hard, here is how I do it\n\n```\nTOKEN :\n{\n : DO_CAL\n | \n | \n | \n | \n | \n | : NEED_CMP_OP\n | : NEED_DATA_TYPE\n | \n | \n | \n | \n | \n | : DO_DATA\n | \n}\n\n// T prefix for Token\nTOKEN :\n{\n \n// We need below TOKEN in special context, other wise they are just IDENTIFIER\n// | \n// | \n// | \n// | \n | ([\" \"])* >\n}\n\n// Now we need a CMP OP\n TOKEN:\n{\n : DEFAULT\n}\n// Now we need a DATA TYPE\n TOKEN:\n{\n // EXTENSION Add char to data type\n {\n if(curLexState == DO_CAL){\n SwitchTo(NEED_CAL_OP);\n }else{\n SwitchTo(DEFAULT);\n }\n }\n}\n// We need a CAL OP\n TOKEN:\n{\n : DEFAULT\n}\n// Aslo need to skip the empty\n SKIP:\n{\n \" \"\n| \"\\t\"\n| \"\\r\"\n| \"\\f\"\n}\n```\n\nSource is here, I can distinguish the token from context by `curLexState`.\n\nIt is works, but fussy to do, need to add a lot extra state, and maintain a lot states.Is there any easy way to achieve this ?\n\n========================================\n\nTop Answer:\nIf you merge the lexer and parser into a character-oriented parser, then it is relatively easy to distinguish keywords in context, because the parser is all about retaining context. You could operate JavaCC on character tokens to achieve this effect, but its LL nature would probably make it impossible to write practical grammars for other reasons. \n\nIf you separate lexer and parser, this isn't easy.\n\nYou are asking the lexer to know when something is an identifier or a keyword,\nwhich it can only do by knowing the context which the Id/keyword is found.\n\nIdeally the lexer would simply ask the parser for its state, and that would identify the contexts in which the choice is made. That's hard to organize; most parsers aren't designed to reveal their state easily or in a form easy to interpret for extracting the context signal needed. JavaCC isn't obviously organized this way.\n\nYour other obvious choice is to model the different contexts as states in the lexer,\nwith transitions between lexing states corresponding to transitions between interesting contexts. This may or may not be easy depending the context. If you can do it, you have to code the states and the transitions in your lexer and keep them up to date. When you can do this \"easily\", it is not a bad solution.\nThis can be hard or impossible depending the specific contexts.\n\nFor OPs purpose (apparantly a parser for an assembler), the context is usually determined by the position within the source line. One can qualitatively divide assembler input into Label, Opcode, Operand, Comment contexts by watching whitespace: A newline sets the context to Label, whitespace in Label mode sets context to Opcode, whitespace in Opcode sets Operand context, and whitespace in Operand context sets Comment context. With these state transitions, one can write different sublexers for each context, thus having different keywords in each subcontext.\n\nThis trick doesn't work for languages like PL/I, which have vast numbers of keywords in context (for PL/I, in fact, every keyword is only in context!).\n\nA non-obvious choices is to not try to differentiate at all. When an Id/Keyword is found, feed *both* tokens to the parser, and let it sort out which one leads to a viable parse. (Note: it may have handle the cross product of multiple ambiguous tokens, thus many possible parses while sorting this out.) This requires a parser that can handle ambiguity, both while parsing, and in the tokens it accepts (or it can't accept both an ID and a Keyword token at the same time). This is a beautifully simple solution to use when you have the right parsing machinery. JavaCC isn't that machinery.\n\n[See my bio for a GLR parsing engine in which all 3 solutions are easily accessible. It handles Pl/I easily.\n\n========================================\n\nCode:\n```text\nTOKEN :\n{\n    <I_CAL:     \"CAL\">  : DO_CAL\n    | <I_CALL:  \"CALL\">\n    | <I_CMP:   \"CMP\">\n    | <I_EXIT:  \"EXIT\">\n    | <I_IN:    \"IN\">\n    | <I_JMP:   \"JMP\">\n    | <I_JPC:   \"JPC\">  : NEED_CMP_OP\n    | <I_LD:    \"LD\">   : NEED_DATA_TYPE\n    | <I_NOP:   \"NOP\">\n    | <I_OUT:   \"OUT\">\n    | <I_POP:   \"POP\">\n    | <I_PUSH:  \"PUSH\">\n    | <I_RET:   \"RET\">\n    | <I_DATA:  \"DATA\"> : DO_DATA\n    | <I_BLOCK:  \".BLOCK\">\n}\n\n// T prefix for Token\nTOKEN :\n{\n    <T_REGISTER : \"R0\" | \"R1\" | \"R2\" | \"R3\" | \"RP\" | \"RF\" |\"RS\" | \"RB\">\n// We need below TOKEN in special context, other wise they are just IDENTIFIER\n//    | <DATA_TYPE: \"DWORD\" | \"WORD\" | \"BYTE\" | \"FLOAT\" | \"INT\">\n//    | <PSEUDO_DATA_TYPE: \"CHAR\" >\n//    | <CAL_OP: \"ADD\" | \"SUB\" | \"MUL\" | \"DIV\" | \"MOD\">\n//    | <CMP_OP: \"Z\" | \"B\" | \"BE\" | \"A\" | \"AE\" | \"NZ\">\n    | <T_LABEL: <IDENTIFIER> ([\" \"])* <COLON>>\n}\n\n// Now we need a CMP OP\n<NEED_CMP_OP> TOKEN:\n{\n    <CMP_OP: \"Z\" | \"B\" | \"BE\" | \"A\" | \"AE\" | \"NZ\"> : DEFAULT\n}\n// Now we need a DATA TYPE\n<NEED_DATA_TYPE,DO_CAL> TOKEN:\n{\n    // EXTENSION Add char to data type\n    <DATA_TYPE: \"DWORD\" | \"WORD\" | \"BYTE\" | \"FLOAT\" | \"INT\" | \"CHAR\"> {\n        if(curLexState == DO_CAL){\n            SwitchTo(NEED_CAL_OP);\n        }else{\n            SwitchTo(DEFAULT);\n        }\n    }\n}\n// We need a CAL OP\n<NEED_CAL_OP> TOKEN:\n{\n    <CAL_OP: \"ADD\" | \"SUB\" | \"MUL\" | \"DIV\" | \"MOD\"> : DEFAULT\n}\n// Aslo need to skip the empty\n<NEED_DATA_TYPE,NEED_CAL_OP,NEED_CMP_OP,DO_CAL,DO_DATA> SKIP:\n{\n    \" \"\n|   \"\\t\"\n|   \"\\r\"\n|   \"\\f\"\n}\n```\n\n```text\nclass\n```\n\n```text\nclass\n```\n\n```text\ncurLexState\n```\n\n```text\nTOKEN: { <CLASS: \"class\"> }\nTOKEN: { < VARNAME: [\"a-\"z\",\"A\"-Z\"]([\"a-\"z\",\"A\"-Z\"])* > } // Or what you will\n```\n\n```text\nToken varName() { Token t ; } : {\n{\n    (t = <CLASS> | t = <VARNAME>)\n    {return t ;}\n}\n```\n\n```text\nJPC Z fred\n```\n\n```text\nJPC Z JPC\n```\n\n```text\nJPC Z Z\n```\n\n```text\nTOKEN : // Opcodes\n{\n    <I_CAL: \"CAL\"> \n|   <I_JPC: \"JPC\"> \n|   ... // other op codes\n    <CMP_OP: \"Z\" | \"B\" | \"BE\" | \"A\" | \"AE\" | \"NZ\">\n|   <T_REGISTER : \"R0\" | \"R1\" | \"R2\" | \"R3\" | \"RP\" | \"RF\" |\"RS\" | \"RB\">\n}\n... // Other lexical rules.\n\nTOKEN : // Be sure this rule comes after all keywords.\n{\n    < IDENTIFIER: <LETTER> (<LETTER>|<DIGIT>)* >\n}\n```\n\n```text\nInstruction Instruction():{\n    Instruction inst = new Instruction();\n    Token o = null,dataType = null,calType = null,cmpType = null;\n    Operand a = null,b = null; }\n{\n    ...\n    o = <I_JPC> cmpType = <CMP_OP> a = Operand()\n    ...\n}\n\nOperand Operand():{\n    Token t ; ... }\n{\n     t = <T_REGISTER> ...\n|    t = Identifier()  ...\n    ...\n}\n\nToken Identifier : {\n    Token t ; }\n{\n    t = <IDENTIFIER> {return t ;}\n|   t = <I_CAL>      {return t ;}\n|   t = <I_JPC>      {return t ;}\n|   t = <CMP_OP>     {return t ;}\n| ... // All other keywords\n}\n```\n\n```text\nOperand Operand():{\n    Token t ; ... }\n{\n     t = <T_REGISTER> ...\n|    t = Identifier()  ...\n    ...\n}\n```\n\n```text\nJPC Z R0\n```\n\n```text\nOperand Operand():{\n    Token t ; ... }\n{\n     LOOKAHEAD(1) t = <T_REGISTER> ...\n|    t = Identifier()  ...\n    ...\n}\n```\n\n```text\nP -> Block <EOF>\nBlock -> [S Block]\nS -> Assignment | IfElse\nAssignment -> LHS \":=\" Exp\nLHS -> VarName\nIfElse -> \"if\" Exp Block [\"else\" Block] \"end\"\nExp -> VarName\nVarName -> <ID> | if | else | end\n```\n\n```text\na := b\nif := a\n```\n\n```text\na := b\nif q\n    b := c\nend\n```\n\n```text\nvoid S() : {} {\n    LOOKAHEAD( LHS() \":=\" ) Assignment()\n|\n    IfElse() \n}\n```\n\n```text\nif x\n    end := y\n    else := z\nend\n```\n\n```text\nvoid Block() : {} {\n    LOOKAHEAD( LHS() \":=\" | \"if\" ) S() Block()\n|\n    {}\n}\n```\n\n```text\nvarName()\n```\n\n```text\n<T_REGISTER>\n```\n\n```text\nOperand\n```\n\n```text\nAssignment\n```\n\n```text\nIfElse\n```\n\n========================================\n\nComments:\n- Separating the lexer and the parser (which makes grammars much easier) requires that you have keywords. Your solution is basically undoing that distinction by putting knowledge of the grammar of the language back into the lexer. I don't think that there is an \"easier\" solution.\n- This is always going to be very difficult with any form of generated parser. You can accomplish it by hand but even then it's really not easy. Try to remove this requirement, it's decades out of date.\n- @EJP I try to implement a parse for exists simple ASM.\n- @ErwinBolwidt Maybe I should change the grammar-ly token to grammar, that make a lot sense ?\n- @wener Keywords in assembly languages are distinguished by the column they are in.\n- How does ANTLR handle the implied ambiguity? What parse tree does it build?\n- @IraBaxter I'm not sure about ANTLR 3 and 4, although they are probably similar to JavaCC. In JavaCC, there is no ambiguity at the lexical level because of the the \"first match rule\", so \"class\" is always a `` and never a ``. Ambiguity at the syntactic level could arise if you had a choice between `` and `varName()`. JavaCC would warn of the ambiguity. The programmer can resolve the ambiguity with a syntactic lookahead specification (syntactic predicate for ANTLR) or live with the warning. If you use JJTree, the parse tree (by default) follows the grammar.\n- I don't understand how the answer you provided chooses the correct interpretation of the token in the general case. Arbitrary lookahead might be required; but to distinguish in that case ANTLR must carry the possibilities of two different parses up to the point where the predicate can run. How can it do that?\n- I might not understand the question. As I said above, there in no ambiguity at the lexical level. There is only one interpretation of each token. I.e. for any given input string there is at most one way to break it into tokens and each token will only be assigned one kind. This is true of any JavaCC lexical specification unless you get really crazy with lexical states. I believe ANTLR is similar. So I'm not sure what you mean by the \"correct interpretation\".\n- If the \"token\" is interpreted always by the lexer as the keyword \"class\", then it cannot be used as an identifier, and vice-versa. One might (I think your code does this) ask the parser for keyword or identifier for class in its present state, and if only one is acceptable with the present left context, it might be able to choose correctly. ...\n- ... But if the correct interpretation of the token depends upon some right context, then ANTLR must choose one or the other at the point where it sees the token, and then one can easily construct an adversary input stream where that choice is always wrong. Maybe I misunderstand how ANTLR works, but it seems to me based on this argument that it can't choose correctly. What am I missing? [GLR parsers succeed here because they can carry both interpretations for arbitrarily long right context.]\n- The OP points out that their present problem can be solved with lexical states, i.e. left context, and is asks for an easy way to solve the same problem. My answer provides another way to solve the same problem; I consider it an easy way. @IraBaxter is looking for something else: a solution when right context also needs to be considered. In JavaCC the way to do this is with syntactic lookahead. In ANTLR it is with syntactic predicates. Does this solution work in every conceivable situation? Probably not. But in most situations that come up in practice it seems to work well.\n- So to on my last comment (I ran out of room): Dealing with right context is really a different question. If anyone really wants a particular example illustrated, they should ask a new question. Trying to give a full example and its solution in a comment is very awkward.\n- I note that OP's title question does not restrict the context in which his keywords can be used. His actual question detail content happens to restrict the context quite a bit.\n- Since the title might lead people to expect right as well as left context, I've added a section to the answer that addresses right context with a simple example. There are still two other approaches that I know of: lexical states and semantic lookahead that this answer doesn't address. So it's still not a complete answer. One could write a lot on the topic.","metadata":{"transformedAt":"2026-08-18T18:32:17.820Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":25,"totalLines":360,"estimatedTokens":3046}}697{"id":"stack-10563422","source":"stackoverflow","questionId":10563422,"title":"How does the dart javascript compiler (dart2js) work?","tags":["compiler-construction","dart"],"text":"Title: How does the dart javascript compiler (dart2js) work?\nTags: compiler-construction, dart\nSource: Stack Overflow\n\nQuestion:\nDart runs in its own Dart VM, but you can compile it to modern optimized JavaScript. But how does that work? Are there any articles or papers online which explain that process?\n\nI am wondering if that is an easy straight forward matching element to element, only time consuming to develop, process or are there some elements/aspects of Dart which could not be compiled to JavaScript.\n\nhttp://www.dartlang.org\n\nEDIT:thx for your 2 answers. The point is, that i told a collegue of mine about dart and that dart can be compiled to JavaScript. He accepted that it could be compiled to JavaScript but it would result in mumbojumba code and will only work for simple stuff. In the end he said: '''ok, explain me. how is this compiling going to work on nontrivial code?'''\n\nThat's why i am asking. I was hoping that there is some material online i could link him.\n\n========================================\n\nTop Answer:\nKasper lund on the intermediate representation and the implemented optimizations. https://www.youtube.com/watch?v=GwBb_nqQLuc\n\n========================================\n\nComments:\n- Where I can find the source code for the lexer and parser. I searched in dart github repo but I can't find any thing ?\n- The location is a little strange because we are in the process of unifying the front ends for our various compilers, but its current home is here: github.com/dart-lang/sdk/tree/master/pkg/_fe_analyzer_shared&zwnj;&#8203;/&hellip;\n- Thank you. but there is something I cant understand. How parser the parser for dart is implemented using dart?","metadata":{"transformedAt":"2026-08-18T18:32:17.820Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":26,"estimatedTokens":422}}698{"id":"stack-7667332","source":"stackoverflow","questionId":7667332,"title":"Can a statically typed language be developed with the DLR?","tags":["c#","compiler-construction","language-design","dynamic-language-runtime"],"text":"Title: Can a statically typed language be developed with the DLR?\nTags: c#, compiler-construction, language-design, dynamic-language-runtime\nSource: Stack Overflow\n\nQuestion:\nI have to develop a compiler for a statically typed language for .NET I'm considering using the DLR for this instead of building all the parts (Lexer/Parser, Syntax, Semantic, Code Generation).\n\nWould the DLR fit well for this scenario? Or it would be better to build the compiler directly for .NET and avoid all the dynamic calls.\n\nEDIT: I did implemented the language without using any of the dynamic expressions that the DLR offers, instead I used the other expressions.\n\nAfter that I can say that is much better when implementing a compiler to target the DLR instead of IL directly. The generated code still will be very clean.\n\nIf you want to see some code check: tigerconverters.codeplex.com/\n\n========================================\n\nTop Answer:\nThe DLR provides a lot of infrastructure that is useful for static languages as well. For example, it has default implementations for binding method calls and overload resolution. This is fine if the semantics of your language matches the default behaviour.\n\nError handling might be a bit tricky, however. If a method lookup fails, for example, the default binders will still return a valid expression, but it will be the code to throw an exception...\n\nThe DLR won't help you with parsing or lexing.\n\nThere are other options. For example, you may want to look at the Common Compiler Infrastructure project created by Herman Vitter of MSR. This may actually be a better overall match.\n\n========================================\n\nCode:\n```text\nDSL\n```\n\n```text\nCLR\n```\n\n```text\nDLR\n```\n\n```text\nCLR\n```\n\n========================================\n\nComments:\n- What advantage would you get? You need a lexer and parser anyway or you'll have no idea what the program tries to do. You have to define the syntax and semantics, and you have to check both during executing even if you make a from-source-interpreter - far earlier in a compiler. And chances are you have to generate *some* code for some reasons, although I've never created anything targeting the CLR or the DLR so I can't judge this.\n- I'm greatly interested in this stuff. I've created a few toy compilers from scratch, tokeniser->parser->planner->compiler. One even built pretty comprehensive C#-like expression trees and chunky method parsing. What kind of syntax are you looking at?\n- @KierenJohnstone the languaje that i'm trying to implement is Tiger, but a simpler version\n- @delnan this isn't about what adventages, if making a new languaje for .NET doesn't mean anything to you, then what's the reason for the DLR to exist. New languajes could handle better some scenarios, like Python, Ruby, Lisp already ported.\n- @Ariel: The DLR exists for implementing dynamic languages. If it was meant to ease creating **compilers** for *static* languages, it wouldn't be the *Dynamic* Language **Runtime**. At least that's what I gathered, hence a comment instead of an answer.\n- @delnan But you can implement a static language compiled with the DLR if in the semantic phase do all the analisys, the problem is the DLR generated code vs code generated specifically for a static languaje.\n- Yeah. You can also compile Python code to a series of CPython API calls. But neither makes much sense. The main (or even sole) advantage of the DLR, dynamicness, isn't needed for such a project.\n- The DLR is build on top of the CLR, so it offers a higher level of abstraction when generating code. The CCI also does this but i think that the DLR offers a higer level of abstraction, at the cost of generating Dynamic Code. What i want to know is: is this really worth it?? how would this affect the performance of the generated program?? Compared to generate the code knowing it is static.\n- Also the semantic analisys could be done before the code generation, and we could check things like method M() belongs to type T, and others. and then generate the code using the DLR.\n- The DLR works with expression trees. Any dynamic expressions are resolved when needed. (This used to be when the code was compiled, but now there's also an interpreter.) If everything is known statically, then you might as well generate static expression trees and compile those. You have to do the binding at some point anyway. An advantage of the DLR is that it makes it easy to do things like build a REPL.\n- I never talked about building a **DSL**, and there are tools that help with the Lexer and the Parser. ANTLR is one of them, with it you could build the AST and then use the DLR for the following phases of the compiler.\n- @Ariel: me too:) I'm saying that you can not avoid using a parser and do what you want only with DSL power. My last sentence saying actually that.","metadata":{"transformedAt":"2026-08-18T18:32:17.820Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":60,"estimatedTokens":1208}}699{"id":"stack-20828906","source":"stackoverflow","questionId":20828906,"title":"Creating an a.out executable from scratch","tags":["c","unix","compiler-construction","executable","machine-code"],"text":"Title: Creating an a.out executable from scratch\nTags: c, unix, compiler-construction, executable, machine-code\nSource: Stack Overflow\n\nQuestion:\nI have created a programming language, from scratch with C. I have built a compiler which processes the code in the input file and converts it to tokens and checks that the tokens are in the correct order. I am on the final step of the compiler: Output/Executable. I want to create an output that can run in terminal. I want to create an a.out output but the only resource I could find was this from nasm which doesn't really help me.\n\nSo my question is, how do I create an a.out file (unix executable) that I can run in terminal?\n\n========================================\n\nComments:\n- The \"easiest\" way is to generate assembly code, and then use an existing assembler + linker to generate the final executable.\n- I think you should read some ELF documentation.\n- @OliCharlesworth I want to do all of the processing an compilation inside of my application\n- Then you *do* need to check out the nasm source you mentioned. The function `aout_write` creates a new executable from scratch, and as you can see it's not as simple as \"just output the raw code\".\n- .. although I suppose you could try another route. Write a bytecode parser for your own language, and append your bytecode to that.\n- pick a format, a.out, elf, intel hex, etc, find the details about that format, and just create that file. It is that simple, in the time it took me to write this I googled the a.out format and found details.\n- @dwelch Could you those details, because I haven't found any.\n- google.com/search?btnG=1&pws=0&q=a.out+file+format\n- If you don't mind C++, there are many great resources on using LLVM to compile things for you.\n- This question is essentially about how to write a linker for an unspecifed output format. It is not only far too broad for SO but unanswerable in its present form. -1","metadata":{"transformedAt":"2026-08-18T18:32:17.820Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":22,"estimatedTokens":482}}700{"id":"stack-11480422","source":"stackoverflow","questionId":11480422,"title":"How do programmers ensure that compilers create correct code?","tags":["algorithm","assembly","compiler-construction"],"text":"Title: How do programmers ensure that compilers create correct code?\nTags: algorithm, assembly, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nReading this fascinating (and highest voted question) on SO, Why is it faster to process a sorted array than an unsorted array? made me wonder about compiler code correctness.\n\nFor example, the answer states that:\n\n Intel Compiler 11 does something miraculous. It interchanges the two loops...\n\nHow does a compiler programmer know when it's OK to interchange loops? \n\nAnd, in general, do they use mathematical proofs to demonstrate conclusions?\n\nHow does a compiler programmer know that their compiler will generate correct code? How do they test their conclusion? Do they have to write a test suite that runs the compiler, and checks that ***the generated code*** is correct?\n\n========================================\n\nTop Answer:\nHow does a compiler programmer know when it's OK to interchange loops?\n\nWhen the modification doesn't alter program's behavior according to the language standard, when the change doesn't go against the standard itself.\n\nFor example, C and C++ standards say in a few places that the order of the evaluation of function parameters and sub-expressions is unspecified. This grants the compiler the freedom to generate code to evaluate them in any order it sees fit. If your program depends on a particular order, it's not conforming to the standard and you have no right to blame the compiler for \"breaking\" it.\n\nCompilers may and often do use code analysis, logic and math with all those theorems to optimize code.\n\nIn practice, testing shows whether or not the compiler did the right job.\n\n========================================\n\nCode:\n```text\ngcc\n```\n\n```text\nllvm\n```\n\n```text\nicc\n```\n\n```text\nmachine learning\n```\n\n========================================\n\nComments:\n- +1 just for citing that incredible question and its even better answer.\n- The answer you accepted is full of nonsense. The answer by borisov is full of sense.\n- Passing tests is no guarantee of correctness. Correctness is a mathematical concept and I'm not aware of any modern compilers using proofs to ensure correctness. I posted a link in my answer to this question.\n- @James: There are compilers that use proofs to ensure correctness, however, they're a long way from being able to compile real-world code in a language as complex as C or C++. And that's with no attempt whatsoever put into optimizing the resulting machine code at all.\n- @DavidSchwartz this answer is simply wrong. \"They just work out an optimization and a set of conservative tests to ensure that optimizations is safe.\" That statement is false. The algorithms found in the book by Muchnik (as an example) are mathematically sound methods of optimization which will never change the meaning of the code being optimized. It's apparent that you aren't familiar with these algorithms, and just made up an answer.\n- \"For example, if the code doesn't fully inline, it probably won't be able to interchange the loops.\" -- this is fiction.","metadata":{"transformedAt":"2026-08-18T18:32:17.820Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":58,"estimatedTokens":766}}701{"id":"stack-10610772","source":"stackoverflow","questionId":10610772,"title":"Crenshaw's \"Let's Build a Compiler\": Transcription to C and x86 Assembler?","tags":["c","assembly","compiler-construction","x86","pascal"],"text":"Title: Crenshaw's \"Let's Build a Compiler\": Transcription to C and x86 Assembler?\nTags: c, assembly, compiler-construction, x86, pascal\nSource: Stack Overflow\n\nQuestion:\nI am trying to work through Jack Crenshaw's excellent compiler programming tutorial \"Let's Build a Compiler\" and have bumped into two hurdles. First the code is written in Pascal, a great language for which I have both respect and fondness from a brief exposure to it in the mid 1980s, but which I have not touched since. I currently program mostly in C.\n\nThe other hurdle is that the generated assembler is for the Motorola 68000 line of processors. While I may be able to find a translator for it, it would be preferable if I could generate Intel x86 assembler (either Intel or AT&T style).\n\nI can work through the code on Linux, Mac OsX or Windows 7, if it makes anything easier. I have access to all of the named operating systems.\n\nI do not feel fully qualified to do the transcription myself. Has anybody worked this out already, or do you have other suggestions?\n\nAny ideas, feedback or suggestions welcome.\n\n========================================\n\nTop Answer:\nIt might be easier to target the JVM instead of a native processor. as for translating Pascal to C... it'shouldn't be *that* much of a hurdle really.\n\n========================================\n\nComments:\n- I wrote a C/x86 conversion of Jack's code. I would read each installment as he released it, convert the code, and post it to Compuserve. I do not know if I still have that stuff. Contact me directly (my email address is in my profile) if you would like me to search through my archives. I make no promises, as that was something more than 20 years ago.\n- Dragging in complex frameworks is contraproductive. The goal of the tutorial is to provide insight, not be \"easy\".","metadata":{"transformedAt":"2026-08-18T18:32:17.820Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":454}}702{"id":"stack-12272563","source":"stackoverflow","questionId":12272563,"title":"How can a compiler apply function elimination to impure functions?","tags":["function","optimization","language-agnostic","compiler-construction","compiler-optimization"],"text":"Title: How can a compiler apply function elimination to impure functions?\nTags: function, optimization, language-agnostic, compiler-construction, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nOften times when writing code, I find myself using a value from a particular function call multiple times. I realized that an obvious optimization would be to capture these repeatedly used values in variables.\nThis (pseudo code):\n\n```\nfunction add1(foo){ foo + 1; }\n...\ndo_something(foo(1));\ndo_something_else(foo(1));\n```\n\nBecomes:\n\n```\nfunction add1(foo){ foo + 1; }\n...\nbar = foo(1);\ndo_something(bar);\ndo_something_else(bar);\n```\n\nHowever, doing this explicitly makes code less readable in my experience. I assumed that compilers could not do this kind of optimization if our language of choice allows functions to have side-effects.\n\nRecently I looked into this, and if I understand correctly, this optimization is/can be done for languages where functions must be pure. That does not surprise me, but supposedly this can also be done for impure functions. With a few quick Google searches I found these snippets:\nGCC 4.7 Fortran improvement\n\nWhen performing front-end-optimization, the -faggressive-function-elimination option allows the removal of duplicate function calls even for impure functions.\n\nCompiler Optimization (Wikipedia)\n\nFor example, in some languages functions are not permitted to have side effects. Therefore, if a program makes several calls to the same function with the same arguments, the compiler can immediately infer that the function's result need be computed only once. In languages where functions are allowed to have side effects, another strategy is possible. The optimizer can determine which function has no side effects, and restrict such optimizations to side effect free functions. This optimization is only possible when the optimizer has access to the called function.\n\nFrom my understanding, this means that an optimizer can determine when a function is or is not pure, and perform this optimization when the function is. I say this because if a function always produces the same output when given the same input, and is side effect free, it would fulfill both conditions to be considered pure.\n\nThese two snippets raise two questions for me.\n\n- How can a compiler be able to safely make this optimization if a function is not pure? (as in -faggressive-function-elimination)\n\n- How can a compiler determine whether a function is pure or not? (as in the strategy suggested in the Wikipedia article)\n\nand finally:\n\n- Can this kind of optimization be applied to any language, or only when certain conditions are met?\n\n- Is this optimization a worthwhile one even for extremely simple functions?\n\n- How much overhead does storing and retrieving a value from the stack incur?\n\nI apologize if these are stupid or illogical questions. They are just some things I have been curious about lately. :)\n\n========================================\n\nCode:\n```text\nfunction add1(foo){ foo + 1; }\n...\ndo_something(foo(1));\ndo_something_else(foo(1));\n```\n\n```text\nfunction add1(foo){ foo + 1; }\n...\nbar = foo(1);\ndo_something(bar);\ndo_something_else(bar);\n```\n\n```text\nextern volatile int RW_A = 0;  // see note below\n\nint foo(int a)  { return a * a; }\nvoid bar(int x) { RW_A = x; }\n\nint _tmain(int argc, _TCHAR* argv[])\n{\n   bar(foo(2));\n   bar(foo(2));\n}\n```\n\n```text\n<register> = 4;\nRW_A = register;\nRW_A = register;\n```\n\n```text\nextern volatile int RW_A = 0;\nextern volatile int RW_B = 0;\nint accu = 1;\n\nint foo(int a)  { accu *= 2; return a * a; }\nvoid bar(int x) { RW_A = x; }\n\nint _tmain(int argc, _TCHAR* argv[])\n{\n   bar(foo(2));\n   bar(foo(2));\n\n   RW_B = accu;\n   return 0;\n}\n```\n\n```text\nregisterA = accu;\nregisterA += registerA;\naccu = registerA;\n\nregisterA += registerA;\nregisterC = 4;\naccu = registerA;\n\nRW_A = registerC;\nRW_A = registerC;\n\nRW_B = registerA;\n```\n\n```text\nfoo\n```\n\n```text\naccu\n```\n\n========================================\n\nComments:\n- This is called **common subexpression elimination** en.wikipedia.org/wiki/Common_subexpression_elimination\n- The documentation for that GNU Fortran option seems to be lacking, and I suspect it just generates wrong code if the function has non-idempotent side effects. A quick reading of the code generation options manual for GNU Fortran has me seriously doubting the quality of their implementation -- especially things like silently putting large local variable in static memory...\n- Awesome answer! I see now how a compiler would not need to test purity. In regards to the overhead of the memory access I expected it would be pretty low, very cool.","metadata":{"transformedAt":"2026-08-18T18:32:17.820Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":8,"totalLines":138,"estimatedTokens":1162}}703{"id":"stack-672577","source":"stackoverflow","questionId":672577,"title":"How can I parse code to build a compiler in Java?","tags":["java","parsing","compiler-construction","parser-generator"],"text":"Title: How can I parse code to build a compiler in Java?\nTags: java, parsing, compiler-construction, parser-generator\nSource: Stack Overflow\n\nQuestion:\nI need to write a compiler. It's homework at the univ. The teacher told us that we can use any API we want to do the parsing of the code, as long as it is a good one. That way we can focus more on the JVM we will generate.\n\nSo yes, I'll write a compiler in Java to generate Java. \n\nDo you know any good API for this? Should I use regex? I normally write my own parsers by hand, though it is not advisable in this scenario.\n\nAny help would be appreciated.\n\n========================================\n\nTop Answer:\nI would recommend ANTLR, primarily because of its output generation capabilities via StringTemplate.\n\nWhat is better is that Terence Parr's book on the same is by far one of the better books oriented towards writing compilers with a parser generator.\n\nThen you have ANTLRWorks which enables you to study and debug your grammar on the fly.\n\nTo top it all, the ANTLR wiki + documentation, (although not comprehensive enough to my liking), is a good place to start off for any beginner. It helped me refresh knowledge on compiler writing in a week.\n\n========================================\n\nComments:\n- I'm not sure about 1.6, but up to 1.4 the parser for javac in the Sun JDK was written by hand.\n- Mmm and mayby a Semantic Analyser? For checking things that cannot be checked in parser?\n- In my mind, a parser was a syntax and semantic analyzer. I have updated the answer to separate the two concepts.","metadata":{"transformedAt":"2026-08-18T18:32:17.821Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":30,"estimatedTokens":391}}704{"id":"stack-30387921","source":"stackoverflow","questionId":30387921,"title":"How are `tagbody` and `go` implemented under the hood in Common Lisp?","tags":["compiler-construction","common-lisp"],"text":"Title: How are `tagbody` and `go` implemented under the hood in Common Lisp?\nTags: compiler-construction, common-lisp\nSource: Stack Overflow\n\nQuestion:\nHow are `tagbody` and `go` implemented in Common Lisp? Is it some form of setjmp/longjmp or is there a more elegant way of handling this?\n\nI'm writing a lispy language implemented in C and would like to have something like this.\n\n========================================\n\nTop Answer:\nFrom an implementation standpoint, if you're **interpreting** a Lisp-like program, you might do something a bit like this:\n\n- Upon entering a `tagbody`, begin a table of destinations. (a map of symbol→address pairs)\n\n- Iterate each form within the `tagbody`\n\n- `if (symbolp this-element)`, then store the address (a pointer to that form) into the table\n\n- otherwise, `(eval this-element)` as usual\n\n- When encountering a `go` form, look up the destination symbol, and (destructively) change your program's \"current instruction\" pointer to that value. Then, jump to your routine to fetch the next instruction.\n\n- When exiting the `tagbody`, just discard the destination table.\n\nThe destination tables will (ultimately) need to be a stack (referred-to in older Lisp documentation as a \"push-down list\" or PDL), since you'll search upwards through dynamic scope to find the tag in question. Keep in mind, in Common Lisp, `go` tags are a separate namespace from variables, functions, classes, et al.\n\n@jlahd is correct, it's effectively identical to a (limited-range) `goto` in C, but if you're interpreting the code, you'll actually be overwriting the \"program counter\" pointer with the stored value.\n\n========================================\n\nCode:\n```text\ntagbody\n```\n\n```text\ngo\n```\n\n```text\n(tagbody\n    (mapc #'(lambda (el1 el2)\n              (format t \"el1: ~a, el2: ~a~%\" el1 el2)\n              (when (or (null el1) (null el2))\n                (go stop)))\n          list1\n          list2)\n  stop)\n```\n\n```text\ngo\n```\n\n```text\ngoto\n```\n\n```text\ngo\n```\n\n```text\ngo\n```\n\n```text\ngoto\n```\n\n```text\ngo\n```\n\n```text\nsetjmp\n```\n\n```text\nlongjmp\n```\n\n```text\ngoto\n```\n\n```text\nsetjmp\n```\n\n```text\nunwind-protect\n```\n\n```text\nsetjmp\n```\n\n```text\nlongjmp\n```\n\n```text\nlongjmp\n```\n\n```text\nunwind-protect\n```\n\n```text\ntagbody\n```\n\n```text\ntagbody\n```\n\n```text\nif (symbolp this-element)\n```\n\n```text\n(eval this-element)\n```\n\n```text\ngo\n```\n\n```text\ntagbody\n```\n\n```text\ngo\n```\n\n```text\ngoto\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.821Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":26,"totalLines":143,"estimatedTokens":606}}705{"id":"stack-29626076","source":"stackoverflow","questionId":29626076,"title":"How's Roslyn C# compiler bootstrapped?","tags":["c#","compiler-construction","compilation","roslyn"],"text":"Title: How's Roslyn C# compiler bootstrapped?\nTags: c#, compiler-construction, compilation, roslyn\nSource: Stack Overflow\n\nQuestion:\nI've started reading about \"Roslyn\" the new C# compiler and was wondering how was the Roslyn compiler compiled?\n\nI understand that boostrapping is needed in order to have \"self hosting compiler\", and I wondered how was the Roslyn compiler \"bootstrapped\"?\n\n========================================\n\nTop Answer:\nBootstrapping is only a problem if there isn't already an existing implementation of the language. In this case there was, so it's easy.\n\n- Compile the first version of Roslyn using the existing C# compiler\n\n- Then re-compile it using the freshly compiled Roslyn build.\n\n- Done.\n\n========================================\n\nComments:\n- I don't understand your question. You already seem know what is bootstrapping. Then what you don't know?\n- Seems like opening github.com/dotnet/roslyn/blob/master/src/Roslyn.sln in Visual Studio and pushing the \"compile\" button would do the trick\n- When new keywords are added to the language, how it will be compiled? because we don't have the compiler(yet) which understands the new tokens ? Originally asked here\n- @SriramSakthivel Just don't use those new features in the compiler (at first).\n- Silly me :) It was very easy one :)\n- This was somewhat challenging for us -- even though the C# compiler that supported await had shipped, the Roslyn compiler didn't support it yet. We couldn't use it in the IDE codebase because we were trying to get to self-hosting. Now we can use features that we haven't shipped to the entire world yet, but it used to be the reverse!","metadata":{"transformedAt":"2026-08-18T18:32:17.821Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":29,"estimatedTokens":412}}706{"id":"stack-1866316","source":"stackoverflow","questionId":1866316,"title":"Assembly language and compiled languages","tags":["performance","compiler-construction","assembly"],"text":"Title: Assembly language and compiled languages\nTags: performance, compiler-construction, assembly\nSource: Stack Overflow\n\nQuestion:\nHow is assembly faster than compiled languages if both are translated to machine code?\n\nI'm talking about truly compiled languages which are translated to machine code. Not C# or Java which are compiled to an intermediate language first and then compiled to native code by a software interpreter, etc.\n\nOn Wikipedia, I found something which I'm not sure if it's in any way related to this. Is it because that translation from a higher level language generates extra machine code? Or is my understanding wrong?\n\n A utility program called an assembler is used to translate assembly language statements into the target computer's machine code. The assembler performs a more or less isomorphic translation (a one-to-one mapping) from mnemonic statements into machine instructions and data. **This is in contrast with high-level languages, in which a single statement generally results in many machine instructions**.\n\n========================================\n\nTop Answer:\nAssembly *may sometimes* be faster than a compiled language if an assembly programmer writes better assembly than that generated by the compiler.\n\nA compiled language is *often* faster than assembly because programmers who write compilers usually know the CPU architecture better than programmers who are utilizing assembly in a one-off, limited-case, situation.\n\n========================================\n\nCode:\n```text\nnull\n```\n\n```text\nfoo.cc\n```\n\n```text\ng++ -fverbose-asm -Wall -S -O2 -march=native foo.cc\n```\n\n```text\nfoo.s\n```\n\n========================================\n\nComments:\n- Note that most high level language compilers compile first to assembly code, which is then compiled by an separate assembler. So an (optimal) assembler program can never be slower than a compiled source. If your (non optimal) assembly is slower, you could just exchange your code with the compiler generated code and do something more useful than trying to write efficient assembler.\n- Moreover, an assembly export will be able to fully use the processor's registers and instruction extensions, to perform most of the computation without accessing external memory, thus providing a noticeable boost. Of course, it depends if you target one or more processor...\n- @Laurent: and it also depends on your compiler. There's no reason a specialized compiler couldn't do that.\n- I thought about linking those slides. That's actually a nice collection of clever tricks compilers know about but many people who try writing assembly for performance reasons don't.\n- The trouble (and reason for writing assembly) is that there are lots of things the compiler could do but doesn't.\n- The trouble with a lot of people writing assembly is that they often do it even before profiling their application :)\n- @LaurentEtiemble. `gcc -march=native`, `clang -march=native` or `icc -xHOST` already does that. They do know how/when to use most of the x86 instruction-set extensions. gcc makes pretty good use of BMI1/BMI2 for example, and of course can use SSE-whatever and AVX1/2/512 when autovectorizing. Some compilers can even notice some kinds of popcount C implementations and compile them into a `popcnt` instruction, without having to use an intrinsic.\n- That article on compiler tricks is nice. It misses the best sequence for testing if sign bits are equal, though (for CPUs with fast `setcc`, i.e. anything from the last 10 years). xor-zero eax / `xor %edi, %esi` / `setns %al`. Unfortunately no modern compilers generate it either. That has 2 cycle latency from either input to the result (any recent Intel or AMD CPU), and is only 3 total instructions. The xor-zero is off the critical path. (Surprised the comment on SunCC's `test %esi,%esi` calls it \"smarter\" when `xor` already set flags according to the result so it's totally redundant.)\n- That means that assembly code may not be usually faster than compiled code (C/C++) ?\n- Hand-written assembly code can be faster than compiled code if you *know what you're doing*. Most of the time, it won't.\n- Depends on the person who's writing it. See yu_sha's answer which nicely summarizes that. Compilers actually have fixed sets of rules how to write certain things more efficiently; those were ultimately created by people. People who may eb able to adapt those things to other situations as well and produce more efficient code than a compiler can. But in many many cases the compiler is much better at such things. For those snippets where it fails you can still resort to inline assembler but don't be surprised if it runs slower than the orignal code if you switch to a better/newer compiler.\n- I usually compare it to the difference between a car with a manual transmission or an automatic transmission. People say that a manual transmission gets better fuel economy, and that is true *if the driver is very skilled*, because it gives him better control, and that control allows a skilled driver to shift in the best way. If the driver does not know exactly what he is doing, then that finer control means he will actually do a worse job than the automatic would have.\n- @Crashworks: Great analogy. I do feel like writing bad assembly when I'm driving :(\n- @Crashworks: But driving with an automatic transmission is not nearly as exciting...\n- It's fairly easy btw to take a disassembly of compiled code, handoptimize the assembler a bit while benchmarking and outdo the compiler. It is harder to do this for all 40000 procedures.\n- I've been playing with code since I was 11 (28 years ago) and I remember assembly code always produced a much smaller file which ran exponentially faster than an equivalent written in, turbo c++. I think that was because the headers bloated the resulting machine code. I'm not sure if that's still the case though.\n- @iuppiter: Modern compilers are *much* better than 28 years ago, and we're not compiling for segmented x86 anymore. Flat memory models are easier to optimize for. You can still often beat compilers on a local scale (for a hot loop), though; missed optimizations are still unfortunately very common, some of them important and some of them not. See Why is this C++ code faster than my hand-written assembly for testing the Collatz conjecture? :)\n- And pointing out that \"If experience is high enough\" is rarer than most people think: compilers are smarter than most people who think themselves smarter than the compiler.\n- It's they other way around: Handwritten assembly is often slower than a compiled source, because the wannabe assembler programmer just don't has a clue.\n- No, some programmers know the CPU architecture as well as compiler writers do. However, the optimization is so difficult a problem that the computer (running the compiler) will often do better than clever programmers.\n- Beating the compiler with hand-written asm is often possible for a single hot loop. But at a large scale, constant propagation and various inlining possibilities make it unmaintainable to use asm for more than that. Ideally you can hand-hold a compiler into making nice asm by tweaking the source, giving you the best of both worlds. (Good asm now, *and* in the future for different CPUs, or different use-cases or surrounding code). See C++ code for testing the Collatz conjecture faster than hand-written assembly - why? for discussion of that.","metadata":{"transformedAt":"2026-08-18T18:32:17.821Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":63,"estimatedTokens":1858}}707{"id":"stack-25395251","source":"stackoverflow","questionId":25395251,"title":"Detecting and skipping line comments with Flex","tags":["c","compiler-construction","flex-lexer"],"text":"Title: Detecting and skipping line comments with Flex\nTags: c, compiler-construction, flex-lexer\nSource: Stack Overflow\n\nQuestion:\nHow can I detect one line comments like `//` in Flex and skip those lines?\n\nAlso, for `/*` comments, will the following snippet be enough?\n\n```\n\"/*\" { comment(); }\n %%\n\n comment()\n {\n char c, c1;\n loop:\n while ((c = input()) != '*' && c != 0)\n putchar(c);\n\n if ((c1 = input()) != '/' && c != 0)\n {\n unput(c1);\n goto loop;\n }\n\n if (c != 0)\n putchar(c1);\n }\n```\n\n========================================\n\nTop Answer:\nFor `//` you can read until you find the end of line `\\n` or `EOF`, in case if the comment was at the end of file, for example:\n\n```\nstatic void\nskip_single_line_comment(void)\n{\n int c;\n\n /* Read until we find \\n or EOF */\n while((c = input()) != '\\n' && c != EOF)\n ;\n\n /* Maybe you want to place back EOF? */\n if(c == EOF)\n unput(c);\n}\n```\n\nas for multiple lines comments `/* */`, you can read until you see `*` and peek the next character, if it's `/` this means this is the end of comment, if not just skip it with any other character. You shouldn't expect `EOF`, means unclosed comment:\n\n```\nstatic void\nskip_multiple_line_comment(void)\n{\n int c;\n\n for(;;)\n {\n switch(input())\n {\n /* We expect ending the comment first before EOF */\n case EOF:\n fprintf(stderr, \"Error unclosed comment, expect */\\n\");\n exit(-1);\n goto done;\n break;\n /* Is it the end of comment? */\n case '*':\n if((c = input()) == '/')\n goto done;\n unput(c);\n break;\n default:\n /* skip this character */\n break;\n }\n }\n\ndone:\n /* exit entry */ ;\n}\n```\n\nComplete file:\n\n```\n%{\n#include \n\nstatic void skip_single_line_comment(void);\nstatic void skip_multiple_line_comment(void);\n\n%}\n\n%option noyywrap\n\n%%\n\"//\" { puts(\"short comment was skipped \");\n skip_single_line_comment();}\n\n\"/*\" { puts(\"long comment begins \");\n skip_multiple_line_comment();\n puts(\"long comment ends\");}\n\n\" \" { /* empty */ }\n[\\n|\\r\\n\\t] { /* empty */ }\n. { fprintf(stderr, \"Tokenizing error: '%c'\\n\", *yytext);\n yyterminate(); }\n%%\n\nstatic void\nskip_single_line_comment(void)\n{\n int c;\n\n /* Read until we find \\n or EOF */\n while((c = input()) != '\\n' && c != EOF)\n ;\n\n /* Maybe you want to place back EOF? */\n if(c == EOF)\n unput(c);\n}\n\nstatic void\nskip_multiple_line_comment(void)\n{\n int c;\n\n for(;;)\n {\n switch(input())\n {\n /* We expect ending the comment first before EOF */\n case EOF:\n fprintf(stderr, \"Error unclosed comment, expect */\\n\");\n exit(-1);\n goto done;\n break;\n /* Is it the end of comment? */\n case '*':\n if((c = input()) == '/')\n goto done;\n unput(c);\n break;\n default:\n /* skip this character */\n break;\n }\n }\n\ndone:\n /* exit entry */ ;\n}\n\nint main(int argc, char **argv)\n{\n yylex();\n return 0;\n}\n```\n\n========================================\n\nCode:\n```text\n\"/*\"         { comment(); }\n  %%\n\n    comment()\n    {\n        char c, c1;\n    loop:\n        while ((c = input()) != '*' && c != 0)\n            putchar(c);\n\n        if ((c1 = input()) != '/' && c != 0)\n        {\n            unput(c1);\n            goto loop;\n        }\n\n        if (c != 0)\n            putchar(c1);\n    }\n```\n\n```text\n//\n```\n\n```text\n/*\n```\n\n```text\n\"//\".*                                    { /* DO NOTHING */ }\n```\n\n```text\n[/][*][^*]*[*]+([^*/][^*]*[*]+)*[/]       { /* DO NOTHING */ }\n```\n\n```text\n[/][*][^*]*[*]+([^*/][^*]*[*]+)*[/]       { /* DO NOTHING */ }\n[/][*]                                    { fatal_error(\"Unterminated comment\"); }\n```\n\n```text\nlex/flex\n```\n\n```text\n/*\n```\n\n```text\nputchar()\n```\n\n```text\n/*\n```\n\n```text\nECHO\n```\n\n```text\n.\n```\n\n```text\n//\n```\n\n```text\n[*]\n```\n\n```text\n\"*\"\n```\n\n```text\n*/\n```\n\n```text\nstatic void\nskip_single_line_comment(void)\n{\n  int c;\n\n  /* Read until we find \\n or EOF */\n  while((c = input()) != '\\n' && c != EOF)\n    ;\n\n  /* Maybe you want to place back EOF? */\n  if(c == EOF)\n    unput(c);\n}\n```\n\n```text\nstatic void\nskip_multiple_line_comment(void)\n{\n  int c;\n\n  for(;;)\n  {\n    switch(input())\n    {\n      /* We expect ending the comment first before EOF */\n      case EOF:\n        fprintf(stderr, \"Error unclosed comment, expect */\\n\");\n        exit(-1);\n        goto done;\n      break;\n      /* Is it the end of comment? */\n      case '*':\n        if((c = input()) == '/')\n          goto done;\n        unput(c);\n        break;\n      default:\n        /* skip this character */\n        break;\n    }\n  }\n\ndone:\n  /* exit entry */ ;\n}\n```\n\n```text\n%{\n#include <stdio.h>\n\nstatic void skip_single_line_comment(void);\nstatic void skip_multiple_line_comment(void);\n\n%}\n\n%option noyywrap\n\n%%\n\"//\"              { puts(\"short comment was skipped \");\n                    skip_single_line_comment();}\n\n\"/*\"              { puts(\"long comment begins \");\n                    skip_multiple_line_comment();\n                    puts(\"long comment ends\");}\n\n\" \"               { /* empty */ }\n[\\n|\\r\\n\\t]       { /* empty */ }\n.                 { fprintf(stderr, \"Tokenizing error: '%c'\\n\", *yytext);\n                    yyterminate(); }\n%%\n\nstatic void\nskip_single_line_comment(void)\n{\n  int c;\n\n  /* Read until we find \\n or EOF */\n  while((c = input()) != '\\n' && c != EOF)\n    ;\n\n  /* Maybe you want to place back EOF? */\n  if(c == EOF)\n    unput(c);\n}\n\nstatic void\nskip_multiple_line_comment(void)\n{\n  int c;\n\n  for(;;)\n  {\n    switch(input())\n    {\n      /* We expect ending the comment first before EOF */\n      case EOF:\n        fprintf(stderr, \"Error unclosed comment, expect */\\n\");\n        exit(-1);\n        goto done;\n      break;\n      /* Is it the end of comment? */\n      case '*':\n        if((c = input()) == '/')\n          goto done;\n        unput(c);\n        break;\n      default:\n        /* skip this character */\n        break;\n    }\n  }\n\ndone:\n  /* exit entry */ ;\n}\n\nint main(int argc, char **argv)\n{\n  yylex();\n  return 0;\n}\n```\n\n```text\n//\n```\n\n```text\n\\n\n```\n\n```text\nEOF\n```\n\n```text\n/* */\n```\n\n```text\n*\n```\n\n```text\n/\n```\n\n```text\nEOF\n```\n\n```text\n^\"//\"    printf(\"This is a comment line\\n\");\n```\n\n```text\n^\"/*\"[^*]*|[*]*\"*/\" printf(\"This is a Multiline Comment\\n\");\n```\n\n```text\n%{\n#include <stdio.h>\nint v=0;\n%}\n%%\n^\"//\"    printf(\"This is a comment line\\n\");\n^\"/*\"[^*]*|[*]*\"*/\" printf(\"This is a Multiline Comment\\n\");\n.|\\n {}\n%%\nint yywrap()\n{\n    return 1;\n}\nmain()\n{\n    yylex();\n}\n```\n\n```text\n^\"/*\"\n```\n\n```text\n[^*]*\n```\n\n```text\n[*]*\n```\n\n```text\n[^*]|[*]*\n```\n\n```text\n\"*/\"\n```\n\n========================================\n\nComments:\n- You solution for multiline comments is simple and great but i still have a problem... I need to increment a line counter for each line commented, so I need to consider the \\n inside comments... Is it possible maintaining the main idea of your regex solution? Thanks\n- flex will count lines for you. See yylineno\n- Hmmm.. Thats strange. Flex is returning wrong lines for my entry... Its counting just the line with the initial comment symbol\n- I have just created another question to discuss this point. stackoverflow.com/questions/25416437/&hellip;\n- The single line comment solution doesn't appear to handle line splicing.\n- @DanO: Guilty as charged. Handling line-splicing inline in (f)lex patterns is possible, and it will become less of a pain once trigraphs really vanish. But it's still ugly and IMHO doesn't really contribute to learning (f)lex. In most applications, the overhead of doing phase 2 as a separate phase is not noticeable, and the code is much simpler. You can do it in `YY_INPUT`, for example, where the entire input is copied into the flex buffer anyway. I'd be happy to write that up if anyone ever asked about implementing line splicing in (f)lex.\n- @rici is `\"&#47;&#47;\"(\\\\\\n|\\\\\\r|\\\\\\r\\n|[^\\r\\n])*` not correct for the line splicing case? I'm not sure if this inhibits learning c grammar or flex or both or neither :)\n- @DanO: Not correct. The line splice could happen between the initial `&#47;&#47;`. Also, if you support trigraphs, the line splice can be spelt `??&#47;`.\n- (Some) real-world compilers do accept `\\r` but that's an extension. Other real-world compilers (gcc & clang, for example) allow trailing whitespace after the backslash, which does save a certain amount of grief. So it's not a simple question.\n- `{ &#47;* DO NOTHING *&#47; }` — this wouldn't work for IDEs, where you need not to just ignore comments, but properly highlight them.\n- Multiline comment here clearly doesn't work. Consider: /* foo bar* */","metadata":{"transformedAt":"2026-08-18T18:32:17.821Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":34,"totalLines":468,"estimatedTokens":2087}}708{"id":"stack-933129","source":"stackoverflow","questionId":933129,"title":"What's a good lightweight programming language that compiles to native windows code?","tags":[".net","windows","winapi","compiler-construction","programming-languages"],"text":"Title: What's a good lightweight programming language that compiles to native windows code?\nTags: .net, windows, winapi, compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nI'm a .Net developer but I have a situation where I can't guarantee that the .Net Framework will be installed on the target machine.\n\nI've seen a few questions (here and here) that suggest using NGen to compile to native code. Both point out that you still need the framework though.\n\nI want to write a fairly lightweight app (basic GUI, no database, nothing complicated) but for various reasons, I don't want to have to install any frameworks or bytecode interpreters.\n\nThis question suggests Python and py2exe. I'm not familiar with Python, but if it's the best tool for the job, I can probably learn enough to write this app (I'd prefer not to though). I've also thought about writing it in DHTML and javascript - I can rely on the machine having a relatively new browser.\n\nI'm familiar with the C family of languages and I've dabbled in Java, PHP, and Ruby.\n\n**Can anyone suggest a language that I can easily compile to native Win32 code?**\n\n*Edit: Clarification*\n\nThere have been a few answers suggesting that if I'm familiar with the C family of languages, why not just use C? I should probably clarify. I use C# every day and I was brought up on C+ and C++ (but haven't used either for quite a few years).\n\nIt does look like C/C++ might be the way to go, but I'll have to refresh my memory on them :)\n\n========================================\n\nTop Answer:\nI would try C++ with Qt... I think it is a very important framework to learn, considering it just turned LGPL.\n\n========================================\n\nComments:\n- Kind of duplicate: stackoverflow.com/questions/867263/&hellip;\n- @kotlinski - thanks for that, I didn't see that question - it's fairly similar. I'll have a look there as well.\n- Clarified RE my knowledge of C. I guess I really want something a bit quicker to write in.\n- I've actually had a long history with VB6 as well which I should have mentioned... That's not a bad suggestion.\n- VB6 no longer has support, so its probably not worth even learning anymore unless you work on legacy systems.\n- @Steve: I'm not suggesting VB6 in a vacuum. I think based on the question, it's a reasonable response. What is the relevance of \"support\" in this case?\n- Actually in this case it's a useful suggestion - it's more or less a throwaway app. I do take your point on support though Steve. For a long-term application, it would probably not be the best choice.\n- I actually ended up trying this last night (Australian time) - not too bad :)\n- My god, powerbasic is still around? I bought a copy in 1996, but I assumed Visual Basic would be a stake through its heart.\n- in 1996 powerbasic didn't even have their 32 bit compiler out. they were still 16 bit (for windows) and had a dos compiler. they've come a long way, have a strong following. their forums are among the best i've encountered. it is far from dead.\n- To me the combo of FireFly and PowerBasic is unbeatable for getting small fast fully featured apps with the only run time requirement being windows.\n- firefly 3 is on the way:) planetsquires.com ... paul squires is doing a fantastic job","metadata":{"transformedAt":"2026-08-18T18:32:17.821Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":43,"estimatedTokens":819}}709{"id":"stack-7890329","source":"stackoverflow","questionId":7890329,"title":"Functional Language for Untyped Lambda Calculus","tags":["compiler-construction","functional-programming","interpreter","lambda-calculus","untyped-variables"],"text":"Title: Functional Language for Untyped Lambda Calculus\nTags: compiler-construction, functional-programming, interpreter, lambda-calculus, untyped-variables\nSource: Stack Overflow\n\nQuestion:\nIs there an interpreter (or compiler) for untyped lambda calculus? (According to this thread it's possible.) I recognize that it would be of little use as a programming language, particularly if much of the language (such as numerals and boolean operators) were implemented (either by the user or by a library) in the language itself. However, I still think it would be a fun tool useful for learning and exploring the calculus. For this an interpreter would be preferable to a compiler, tho either would work. Does anyone know of such a program?\n\n========================================\n\nTop Answer:\nYou can use any untyped language that has lambda abstractions. For example Python or JavaScript. There are two main downsides:\n\n- These languages don't have lazy evaluation. This means that not all lambda terms will converge, even though they have a normal form. You have to take this into account and modify the task accordingly.\n\n- You won't see the result as a lambda-term in normal form. You have to know what to expect from the result and use the language to evaluate it to something that can be displayed.\n\nKnowing this, let's make an example in Python:\nFirst we create helper functions to convert between numbers and Church numerals:\n\n```\n# Construct Church numeral from an integer\ndef int2church(n):\n def repeat(f, m, x):\n if (m == 0): return x\n else: return f(repeat(f, m-1, x))\n return lambda f: (lambda x: repeat(f, n, x))\n\ndef church2int(l):\n return l(lambda x: x + 1)(0)\n```\n\nnow we can define standard operations on numerals:\n\n```\nzero = int2church(0)\none = int2church(1)\n\npred = lambda n: lambda f: lambda x: n(lambda g: lambda h: h(g(f)))(lambda u: x)(lambda u: u)\n\nmul = lambda m: lambda n: (lambda f: m(n(f)))\n\nexpn = lambda n: lambda m: m(n)\n\ntetra = lambda n: lambda m: m(expn(n))(one)\n```\n\nand compute for example *43*:\n\n```\nexpn = lambda n: (lambda m: m(n))\n\na = int2church(4)\nb = int2church(3)\nprint church2int(expn(a)(b))\n```\n\nor tetration:\n\n```\na = int2church(5)\nb = int2church(2)\nprint church2int(tetra(a)(b))\n```\n\nTo be able to express even more interesting stuff, we can define the Y combinator:\n\n```\ny = lambda f: (lambda x: f(lambda v: x(x)(v))) (lambda x: f(lambda v: x(x)(v)))\n```\n\nand compute for example factorials:\n\n```\ntrue = lambda x: (lambda y: x)\nfalse = lambda x: (lambda y: y)\n\niszero = lambda n: n(lambda x: false)(true)\n\nfact = y(lambda r: lambda n: iszero(n)(one)(mul(n)(lambda x: r(pred(n))(x))))\nprint church2int(fact(int2church(6)))\n```\n\nNote that the Y combinator had to be adapted for strict evaluation using η-expansion, as well as the factorial function to avoid infinite recursion due to strict evaluation.\n\n========================================\n\nCode:\n```text\ntype lambda = lambda -> lambda\n```\n\n```text\ntype lambda = L of lambda -> lambda\n```\n\n```text\nlambda churchnum: churchnum(lambda x: x+1)(0)\n```\n\n```text\nlambda churchbool: churchbool(True)(False)\n```\n\n```text\nply\n```\n\n```py\n# Construct Church numeral from an integer\ndef int2church(n):\n    def repeat(f, m, x):\n        if (m == 0): return x\n        else: return f(repeat(f, m-1, x))\n    return lambda f: (lambda x: repeat(f, n, x))\n\ndef church2int(l):\n    return l(lambda x: x + 1)(0)\n```\n\n```py\nzero = int2church(0)\none = int2church(1)\n\npred = lambda n: lambda f: lambda x: n(lambda g: lambda h: h(g(f)))(lambda u: x)(lambda u: u)\n\nmul = lambda m: lambda n: (lambda f: m(n(f)))\n\nexpn = lambda n: lambda m: m(n)\n\ntetra = lambda n: lambda m: m(expn(n))(one)\n```\n\n```py\nexpn = lambda n: (lambda m: m(n))\n\na = int2church(4)\nb = int2church(3)\nprint church2int(expn(a)(b))\n```\n\n```py\na = int2church(5)\nb = int2church(2)\nprint church2int(tetra(a)(b))\n```\n\n```py\ny = lambda f: (lambda x: f(lambda v: x(x)(v))) (lambda x: f(lambda v: x(x)(v)))\n```\n\n```py\ntrue = lambda x: (lambda y: x)\nfalse = lambda x: (lambda y: y)\n\niszero = lambda n: n(lambda x: false)(true)\n\nfact = y(lambda r: lambda n: iszero(n)(one)(mul(n)(lambda x: r(pred(n))(x))))\nprint church2int(fact(int2church(6)))\n```\n\n```text\n>>>S(K)(K) is I\nTrue\n```\n\n========================================\n\nComments:\n- Of interest: λ-Calculus extensions: meaning of extension symbols\n- Mine is in JavaScript and it includes a compiler (into JavaScript functions), an evaluator (alpha/beta/eta conversion), and a virtual machine with 13 instructions. There are some known bugs in the VM implementation, but the compiler and evaluator seem to work fine.\n- This is a high-quality answer. +1","metadata":{"transformedAt":"2026-08-18T18:32:17.821Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":12,"totalLines":171,"estimatedTokens":1160}}710{"id":"stack-479647","source":"stackoverflow","questionId":479647,"title":"How to compare compilers","tags":["c","compiler-construction"],"text":"Title: How to compare compilers\nTags: c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nWhat pointers do you use to compare between compilers?\n\nI'm told gcc is the best C compiler, is this true? If so, why?\n\nI mean this generally, so you can state which compiler is more appropriate for which architecture.\n\n(I hear igc would be more appropriate for Intel for instance, but I don't know why)\n\nPersonally I intend to use AMD 64 bit, develop both in Linux and Windows, GUI and non GUI apps.\n\n========================================\n\nTop Answer:\nIn general: first of all, the most important aspect of compiler quality is **correctness**. A compiler with bugs or unexpected behaviour can really wreck your day.\nThe **quality of the resulting code**, like speed, size and memory usage, is also at the top of the list.\n\nThe **speed** of compilation is another aspect, especially when compiling large projects. \n\nOne thing I find particularly important is **error handling**, the quality of messages you get when the compiler encounters stuff it can't (or won't) handle.\n\n========================================\n\nComments:\n- Please specify more information - i.e. operating system, target architecture, your needs (what kind of programs), cost constraints, and so on\n- GCC 4 does if you tell it to.","metadata":{"transformedAt":"2026-08-18T18:32:17.821Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":30,"estimatedTokens":326}}711{"id":"stack-1410444","source":"stackoverflow","questionId":1410444,"title":"Checking Python code correctness","tags":["python","compiler-construction","correctness"],"text":"Title: Checking Python code correctness\nTags: python, compiler-construction, correctness\nSource: Stack Overflow\n\nQuestion:\nIn C++ I have compiler that tell me if something wrong with my code after refactoring. How to make sure that Python code is at least correct after changes? There may be some stupid error like wrong function name etc. that pretty easy to find in compile time.\n\nThanks\n\n========================================\n\nTop Answer:\nuse editor / IDE that supports code highlighting. E.g., Notepad++ has word-highlighting feature that I find very useful.\n\nuse unit tests\n\nstupid errors will be weeded out first, so I wouldn't worry to much about this type of errors. it's \"smart\" error you should be afraid of.\n\n========================================\n\nCode:\n```text\npython -m py_compile script.py\n```\n\n========================================\n\nComments:\n- Eclipse/PyDev. That's what I use, and I am a satisfied customer.\n- I think IDLE might have one of those built in (can't remember exactly).\n- While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review","metadata":{"transformedAt":"2026-08-18T18:32:17.821Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":31,"estimatedTokens":307}}712{"id":"stack-6079603","source":"stackoverflow","questionId":6079603,"title":"Infinite compilation with templates","tags":["c++","templates","compiler-construction","recursion"],"text":"Title: Infinite compilation with templates\nTags: c++, templates, compiler-construction, recursion\nSource: Stack Overflow\n\nQuestion:\nThis question is just out of curiosity. In recursive templates if we forget to put one particular specialization, then compiler will do large number of iterations and then stop at sometime and gives error such as,\n\n```\nerror: incomplete type ‘X’ used in nested name specifier\n```\n\nIn certain cases, the compilation goes **infinite**. For example, see the below code (just for illustration; compiled with gcc 4.4.1):\n\n```\ntemplate\nstruct Infinite\n{\n enum { value = (I & 0x1)? Infinite::value : Infinite::value };\n};\n\nint main ()\n{\n int i = Infinite::value;\n}\n```\n\nShould not be compiler smart enough to stop at some time ?\n\n**Edit**: The compilation error shown above is for other code. For the sample code, compilation never stops (however, I get to see such errors in between)\n\n========================================\n\nTop Answer:\nGetting the parser into an infinite loop using template is not new.\n\n```\n// Stresses the compiler infinitely\n// from: http://www.fefe.de/c++/c%2b%2b-talk.pdf\ntemplate struct Loop { Loop operator->(); };\nLoop i, j = i->hooray;\n```\n\n========================================\n\nCode:\n```text\nerror: incomplete type ‘X<-0x000000000000001ca>’ used in nested name specifier\n```\n\n```text\ntemplate<int I>\nstruct Infinite\n{\n  enum { value = (I & 0x1)? Infinite<I+1>::value : Infinite<I-1>::value };\n};\n\nint main ()\n{\n  int i = Infinite<1>::value;\n}\n```\n\n```text\nD\n```\n\n```text\nint\n```\n\n```text\ntemplate<>\nstruct Infinite<100> //this is to stop template with <I+1> argument\n{\n  enum { value = 678678 }; //see, it doesn't use Infinite<> any further!\n};\n\ntemplate<>\nstruct Infinite<-100> //this is to stop template with <I-1> argument\n{\n  enum { value = -67878 }; //see, it too doesn't use Infinite<> any further!\n};\n```\n\n```text\n// Stresses the compiler infinitely\n// from: http://www.fefe.de/c++/c%2b%2b-talk.pdf\ntemplate<class T> struct Loop { Loop<T*> operator->(); };\nLoop<int> i, j = i->hooray;\n```\n\n========================================\n\nComments:\n- This does terminate in g++ 4.8.4 at least... `main.cpp:4:27: error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum) instantiating ‘struct Infinite’`\n- @Nawaz, actually the question is in more compilation perspective. Why should not compilation stop at sometime, when it know that the compilation going infinite.\n- @iammilind: How would it know? Also, At what point should it stop?\n- @iammilind: \"Why should not compilation stop at sometime, when it know that the compilation going infinite.\" - It did stop. In your case, it stopped when it gave you the compile error.\n- @iammilind: \"Know that the compilation going infinite\"? In general case \"knowing\" that the compilation is going to be infinite is an extremely difficult (or even unsolvable) task. The compiler is not required to concern itself with such matters. It is a QoUI issue: if the compiler wishes to spend its efforts to catch such situations, it can. But nobody is requiring it to.\n- On top of that, it is very unlikely that the compilation goes infinite in your case. It simply takes a very long time. In any case it will probably either abort eventually, or bottom-out eventually due to wrap-around on overflow. You simply didn't wait long enough.\n- @Nawaz, first i haven't downvoted your answer :). 2nd if you take the example of factorial, compiler does stop if you dont specialize Fact. should not it happen in this case also.\n- @iammilind: What factorial example are we talking about? The way you implement non-infinite recursion with templates is to define a specialization of the general template that doesn't instantiate itself. For example, this `Factorial<>` example has both the general template that does the actual recursion and a specialzed template that implements the base case.\n- @iammilind: No. It doesn't. Even factorial needs *specialization*. Without it, it cannot stop!\n- @In silico @Nawaz, ideone.com/FU0Au .. the compilation stops after sometime\n- @iammilind: So what's your point? I already know that it has to stop because we don't have machines with infinte amount of memory. If we had such machines, then the recursion will go on forever unless the inputs satisfy some kind of a base case.\n- @In silico, exactly that's my point. that the above test code doesn't stop compilation. It goes on and on\n- @iammilind: Yes that stops BY GIVING COMPILATION ERROR (scroll down and see the ERROR). Its not a normal stop in the sense all of us are talking about here. Also, such stop by giving error would also happen in your code \"at some time\" , only you've to wait long enough.\n- @iammilind: Not \"maybe\". It IS true (I guarantee :P). You just have to wait long enough. Maybe, 20 mins, 30 mins, 1 hr or 2 hr or even more!\n- @Nawaz, true. anyways +1 for the efforts.\n- In other words, the OP's code doesn't have a base case. For every possible value of the integer `I` the template will still instantiate itself, so it will recurse forever (assuming there is an infinite amount of computing resources of course).","metadata":{"transformedAt":"2026-08-18T18:32:17.821Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":110,"estimatedTokens":1290}}713{"id":"stack-33796890","source":"stackoverflow","questionId":33796890,"title":"How should I parse keywords when writing a C Compiler?","tags":["c","assembly","compiler-construction","lexer"],"text":"Title: How should I parse keywords when writing a C Compiler?\nTags: c, assembly, compiler-construction, lexer\nSource: Stack Overflow\n\nQuestion:\nI am currently in the process of writing a C to Assembly compiler, it is not meant to be practical, but I would like to do it for the educational value. I was wondering when I am testing for keywords, is there any more efficient way rather than just reading in the next word in the file and then running it through a bunch of nested if statements that test for the keywords. Is there any better way?\n\n========================================\n\nTop Answer:\nAre you doing this for part of a class? If so, there should be guidelines on parsing and lexing. If not, you're in for a lot of work!\n\nWriting an actual compiler is much more complicated than just going through a bunch of if statements, because you need to keep track of the environment. You'll need to think about how you allow classes, functions, function calls, class instantiations, recursive functions... the list goes on.\n\nTake a look at course lectures from UC Berkeley on the subject, i.e. parsing, lexing, code generation, and the tools you'll need:\n\nhttp://www-inst.eecs.berkeley.edu/~cs164/fa13/\n\nNote that this course in particular used C++ to write a Python2.5 to Assembly compiler, but the concepts in the Lectures and Readings and some of the tools are not language-restricted.\n\n========================================\n\nComments:\n- You can try perfect hashing, but it's unlikely that this phase is going to be your performance bottleneck.\n- I change the tag [parsing] to [scanning]. Identifying individual tokens is done by the first phase of the compiler, the scanner, and not by the second phase, the parser.\n- And now I noticed that [scanning] is the wrong tag. Changed it again, to [lexer].\n- You fall victim to the \"No premature optimization\" principle. There's nothing wrong with searching the sorted list of keywords with bsearch(). Even a linear search may only be negligibly slower. Testing code for tokens in a C like language is never a bottleneck. You are waaaay overthinking this little problem.","metadata":{"transformedAt":"2026-08-18T18:32:17.821Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":27,"estimatedTokens":531}}714{"id":"stack-3221861","source":"stackoverflow","questionId":3221861,"title":"Does a JIT compiler have any disadvantages compared to a traditional compiler?","tags":["compiler-construction","jit"],"text":"Title: Does a JIT compiler have any disadvantages compared to a traditional compiler?\nTags: compiler-construction, jit\nSource: Stack Overflow\n\nQuestion:\n**Possible Duplicate:**\n\n JIT compiler vs offline compilers \n\nSo until a few minutes ago I didn't really understand what the difference between a JIT compiler and an interpreter is. Browsing through SO, I found the answer, which brought up the question in the title. As far as I've found, JIT compilers have the benefit of being able to use the specific processor it's running on and can thus make better optimized programs. Could somebody please give me a comparison of the pros and cons of each?\n\n========================================\n\nTop Answer:\nJIT compilation doesn't inherently mean it is easy to disassemble. That is more implementation-dependent, such as with Java binaries. Note, however, that JIT can be applied to any kind of executable, whether it is Java, Python or even an already-compiled binary from C++ or similar. (IIRC, the Dynamo project involved re-compiling such binaries on-the-fly to increase performance.)\n\nThe trade-off for JIT compilation is that while the process's goal is to increase runtime performance, the process actually occurs at runtime as well, and so it incurs overhead while analyzing, compiling, and validating code fragments. If the implementation is inefficient or not enough optimizations occur, then it actually produces a performance degradation.\n\nThe other trade-off is that in some cases the JIT compilation can be very wasteful. For example, consider a self-modifying executable. If you compile a fragment of code, and then the executable modifies that fragment, you have to throw away the compiled fragment and then re-analyze that segment to determine if it is worth re-compiling. If this happens frequently, there is a significant performance hit.\n\nFinally, there is a hit in memory consumption, as compiled code fragments must reside in memory in order to be effective. This can make it impractical for devices with limited amounts of memory, or else extremely difficult to implement well.\n\n========================================\n\nCode:\n```text\n-XX:CompileThreshold=10000\n```\n\n```text\n-XX:-CITime\n```\n\n========================================\n\nComments:\n- See JIT compiler vs offline compilers\n- I don't think this is at all a duplicate of the other question, given that the other question asks the exact opposite of this question: \"Are there scenarios where JIT compiler is faster than other compilers like C++?\" (I will vote to reopen if this gets closed.)\n- So basically a JIT might produce a faster executable but take way longer to compile?\n- I wouldn't say that it is way longer, but there is overhead while the program is running.\n- Oh I think I misunderstood what a JIT compiler is. I has assumed it would just fully compile the program when it was first run. Does it also function like an interpreter?\n- @Maulrus: it depends on the goals of the JIT compiler, and the kind of optimizations the designers wanted to support. Some JITs do a full recompilation at startup, others compile parts as they determine what needs the optimization the most.\n- Okay, I think I understand well enough now. Thanks!\n- is that possible/why i don't see any solutions to pre-compile JIT? Like i have a spec machine, i want boost the speed of my application (NodeJs) to maximum, so can I let the machine pre-compile and then use it later?\n- Not so! The Java language and Java bytecodes demonstrate this property, but JRuby programs compiled to Java bytecode can't be comprehensibly decompiled. Ditto for PowerPC programs JITted into x86 machine code using Apple's Rosetta.\n- It's also not the *only* disadvantage.\n- Scala programs go through several levels of syntactic desugaring (which makes them harder, though not impossible, to read) before being compiled into Java bytecode.\n- @Ken Bloom: sure, there are obfuscating programs for .NET, but not as vanilla.\n- JITs don't have to be for IL. HP Labs' Dynamo project was basically a JIT for HP/UX machine code. The JIT and the underlying architecture are completely independent.\n- @Ken: I don't recall saying that \"The JIT and the underlying architecture were dependent\"? If you are JIT'ing then you have an intermediate language (IL)\n- Mitch: I can't figure out what you're implying. When Dynamo was created, PA-RISC machine code *became* an IL? Or Dynamo is not a JIT because PA-RISC chips exist? (Though hardware JVM chips exist, and I don't think anybody claims they make the Hotspot JIT not-a-JIT.) Or something else?\n- Apple's Rosetta JIT-compiles PowerPC code to x86.\n- Apple's Mac 68K emulator (on PCI PowerMacs) also uses JIT compilation.\n- Both examples are a special form JIT compilation known as Binary Translation. (See en.wikipedia.org/wiki/Binary_translation ). I'm not particularly familiar with either (\"I'm a PC\") but I imagine both employ Dynamic Binary Translation.","metadata":{"transformedAt":"2026-08-18T18:32:17.822Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":54,"estimatedTokens":1234}}715{"id":"stack-51780158","source":"stackoverflow","questionId":51780158,"title":"Why do we even need assembler when we have compiler?","tags":["assembly","compiler-construction","compiler-optimization"],"text":"Title: Why do we even need assembler when we have compiler?\nTags: assembly, compiler-construction, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nIf compiler converts high-level language to machine code, why do we even need assembler?\nAre there any assembly level language and we can't use compiler for that?\n\n========================================\n\nTop Answer:\nQuoting from @TylerAndFriends's answer on Why do we need assembly language? on cs.SE (a duplicate of this):\n\n Assembly language was created as an exact shorthand for machine level\n coding, so that you wouldn't have to count 0s and 1s all day. It works\n the same as machine level code: with instructions and operands.\n\n \n \n Though it's true, you probably won't find yourself writing your next\n customer's app in assembly, there is still much to gain from learning\n assembly.\n\n \n Today, assembly language is used primarily for direct hardware\n manipulation, access to specialized processor instructions, or to\n address critical performance issues. Typical uses are device drivers,\n low-level embedded systems, and real-time systems.\n\n \n Assembly language is as close to the processor as you can get as a programmer so a well designed algorithm is blazing -- assembly is\n great for speed optimization. It's all about performance and\n efficiency. Assembly language gives you complete control over the\n system's resources. Much like an assembly line, you write code to push\n single values into registers, deal with memory addresses directly to\n retrieve values or pointers. (source: codeproject.com)\n\n========================================\n\nCode:\n```text\nmov r32, imm32\n```\n\n```text\nB8+rd imm32\n```\n\n```text\n__invlpg()\n```\n\n```text\na++\n```\n\n```text\n++a\n```\n\n```text\n/* a,b are two parts of one number.  \n   c,d are two parts of another to be added.\n */\nvoid add_big(uint *a, uint *b, const uint c,const uint d) {\n  unsigned long long tmp;\n  tmp = *b + d;\n  if(tmp & CARRY_BIT)\n    *a += c + 1;\n  else\n    *a += c;\n  *b = (uint)tmp;\n}\n```\n\n========================================\n\nComments:\n- Assembler is just a human readable representation of the machine code. What would the compiler compile to without it?\n- most modern compilers compile to assembly except tcc which outputs binary directly\n- See also cs.stackexchange.com/questions/13287/&hellip;\n- @phuclv: clang and MSVC, and I think ICC, all create object files directly, effectively using a built-in assembler. Only gcc *actually* creates a `.s` file and runs a separate program on it. Other compilers have an option to emit asm, though.\n- @PeterCordes yeah, they still need the final assembling stage even internally. OTOH tcc emits binary directly and you have no way to stop at the asm stage\n- @phuclv But AFAIK they don't literally have ASCII strings internally, just an internal representation of instructions using a `struct` or something that they turn directly into machine code, or into asm text. I wouldn't quite call it *actually* internally assembling, except for inline asm.\n- AFAIK recent MSVC isn't even able to emit syntactically correct assemblable code even if you ask for it.\n- Without assembly language to make the job of creating machine code sane, explain how you would create a test a new processor or new modifications to existing instruction sets so that down the road someone can create/tune a compiler? Without the egg there is no chicken. Likewise you want to debug a processor, a human readable version of the machine code makes that much easier. You want to test/debug a compiler, a human readable version of the machine code makes that much easier thus the reason why compilers often compile to asm, not to mention the assembler comes before the compiler.\n- Seriously, your post is almost a complete copy pasta from this answer. Please add this as a reference in your post because plagiarism is a big no-no.\n- Nice answer except for the quote from codeproject. \"push values into registers\"? The `push` instruction reads registers and writes the stack, on all architectures that have such an instruction. You use various methods for putting values into registers, but `push 1 &#47; pop rax` is not used unless you're optimizing for code-size over speed. (Tips for golfing in x86/x64 machine code)\n- @PeterCordes: presumably they're using \"push\" informally here, or talking about, e.g., writing control registers (Vax MTPR instruction, the MSR on PowerPC, Sparc `WR` instruction, and so on).\n- Knowing asm to help your debugging efforts is mostly relevant in \"unsafe\" languages like C and C++, where buffer overruns, wild pointers, use-after-free, uninitialized data, and other kinds of bugs have effects that make sense in asm but are totally opaque (Undefined Behaviour) in terms of the higher level language. I think that's the really key difference between C and Python here. And it would apply even to C vs. Java. You can use references in safe languages to do basically anything you can with pointers in C, except that overhead may be huge in Python.\n- @PeterCordes Yes, there is truth to that. Someone writes 'C' code via swig, etc that interfaces with Python. So if you want to write libraries for Python, it will help to know assembler. Also, writing an interpreter will benefit from assembler. People end up making various synthetic opcodes. Like BSD filters, python and java byte code, JIT, etc and all of these developments for those language borrow concepts from assembly languages. I think it is much the same as you can program without knowing mathematics, but you may not be as good of a coder. `sed -e \"s&#47;mathematics&#47;assembler&#47;g\"`\n- Sure, agreed with all of those points. Understanding how computers truly run programs is pretty useful at least for performance in any language. My quibble was limited to the specific point about practical benefit *for debugging*. Otherwise nice answer, already upvoted.\n- @PeterCordes I wouldn't say it is totally useless for Python, etc. Suppose a developer notices that `r = sqrt(A * 1&#47;pi)` is faster than `r =sqrt(A&#47; pi)` in a language. Assembler knowledge makes it pretty obvious why. Otherwise, it just seems mysterious. I agree that the strength of the statement is much stronger for 'C' than most other languages. You have a great answer too, I was just trying to add some complementary points that may apply.\n- Maybe you misread my last comment. I said it *is* useful, even for high level languages like Python. Although insane interpreter overhead in CPython makes some things counter-intuitive (e.g. integer division has an interpreter fast-path for 1-limb small integers and is faster than bitwise-and). So yeah, I think we 100% agree with each other. :) And yeah, good additional points, agreed it's a nice complement to my answer.","metadata":{"transformedAt":"2026-08-18T18:32:17.822Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":94,"estimatedTokens":1694}}716{"id":"stack-8188045","source":"stackoverflow","questionId":8188045,"title":"LLVM no-op instruction?","tags":["compiler-construction","llvm","compiler-optimization"],"text":"Title: LLVM no-op instruction?\nTags: compiler-construction, llvm, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nI am writing a compiler for an assignment for a language that has empty statements.\nFor structures like if-then-else it could be convenient if I could use a no-op statement in llvm but I didnt find any \"official\" nop. I have some ideas but ideally I would like to have llvm to optimize that part of the code and remove the instruction; any advice?\n\n========================================\n\nTop Answer:\nLook at this intrinsic:\n\n```\ndeclare void @llvm.donothing() nounwind readnone\n```\n\nfrom LLVM Language Reference Manual:\n\nThe llvm.donothing intrinsic doesn’t perform any operation. It’s one of only two intrinsics (besides llvm.experimental.patchpoint) that can be called with an invoke instruction.\n\n========================================\n\nCode:\n```text\n%nop = add i1 0, 0\n```\n\n```text\n%nop = alloca i1, i1 0\n```\n\n```text\ndeclare void @llvm.donothing() nounwind readnone\n```\n\n```text\n%2 = icmp sgt i32 %0, %1\n    br i1 %2, label %3, label %7\n; <label>:3\n;   ;; [instructions in block redacted for simplicity]\n    br label %8\n; <label>:7\n    br label %8\n; <label>:8\n    %9 = load i32* %x, align 4 ;; continue on our merry way\n```\n\n========================================\n\nComments:\n- the prefetch and redundant type conversions can be achieved to get no-op effects (the LangRef)\n- why do you need no-ops ?\n- I do not really *need* them; but it would be an easy/quick fix","metadata":{"transformedAt":"2026-08-18T18:32:17.822Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":54,"estimatedTokens":375}}717{"id":"stack-559025","source":"stackoverflow","questionId":559025,"title":"Compiler-Programming: What are the most fundamental ingredients?","tags":["c++","c","compiler-construction","low-level"],"text":"Title: Compiler-Programming: What are the most fundamental ingredients?\nTags: c++, c, compiler-construction, low-level\nSource: Stack Overflow\n\nQuestion:\nI am interested in writing a very minimalistic compiler.\n\nI want to write a small piece of software (in C/C++) that fulfills the following criteria:\n\n- output in ELF format (*nix)\n\n- input is a single textfile\n\n- C-like grammar and syntax\n\n- no linker\n\n- no preprocessor\n\n- very small (max. 1-2 KLOC)\n\nLanguage features:\n\n- native data types: char, int and floats\n\n- arrays (for all native data types)\n\n- variables\n\n- control structures (if-else)\n\n- functions\n\n- loops (would be nice)\n\n- simple algebra (div, add, sub, mul, boolean expressions, bit-shift, etc.)\n\n- inline asm (for system calls)\n\nCan anybody tell me how to start? I don't know what parts a compiler consists of (at least not in the sense that I just could start right off the shelf) and how to program them. Thank you for your ideas.\n\n========================================\n\nTop Answer:\nWith all that you hope to accomplish, the most challenging requirement might be \"very small (max. 1-2 KLOC)\". I think your first requirement alone (generating ELF output) might take well over a thousand lines of code by itself.\n\nOne way to simplify the problem, at least to start with, is to generate code in assembly language text that you then feed into an existing assembler (nasm would be a good choice). The assembler would take care of generating the actual machine code, as well as all the ELF specific code required to build an actual runnable executable. Then your job is reduced to language parsing and assembly code generation. When your project matures to the point where you want to remove the dependency on an assembler, you can rewrite this part yourself and plug it in at any time.\n\nIf I were you, I might start with an assembler and build pieces on top of it. The simplest \"compiler\" might take a language with just a few very simple possible statements:\n\n```\nprint \"hello\"\na = 5\nprint a\n```\n\nand translate that to assembly language. Once you get that working, then you can build a lexer and parser and abstract syntax tree and code generator, which are most of the parts you'll need for a modern block structured language.\n\nGood luck!\n\n========================================\n\nCode:\n```text\nprint \"hello\"\na = 5\nprint a\n```\n\n========================================\n\nComments:\n- possible duplicate of Learning to write a compiler\n- 'look into language design': Do you mean a specific resource or paradigm? Or just something I need to revolve in my head?\n- You'll have to create a language grammar that is compatible with the type of parser you want to use. I'd take a look at top-down vs bottom-up parsers to get started.\n- +1 for Brinch Hansen. It strikes the best balance between technical and practical information on compiler design.\n- Even easier, have it generate C as its output. Lots of successful compilers have gone this route.\n- Note that NASM is written in C, so you might be able to use code from NASM in your translation to machine code.\n- Since he wants to write it in C/C++ (whatever that means), I would go with NASM. FASM is good, but is written in assembly, whereas NASM is written in C. NASM may provide more useful code.","metadata":{"transformedAt":"2026-08-18T18:32:17.822Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":79,"estimatedTokens":817}}718{"id":"stack-1941244","source":"stackoverflow","questionId":1941244,"title":"Compiler to translate Java bytecode to platform-independent C code before runtime?","tags":["java","c","compiler-construction"],"text":"Title: Compiler to translate Java bytecode to platform-independent C code before runtime?\nTags: java, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm looking for a compiler to translate Java bytecode to platform-independent C code before runtime (Ahead-of-Time compilation). \n\nI should then be able to use a standard C compiler to compile the C code into an executable for the target platform. I understand this approach is suitable only for certain Java applications that are modified infrequently.\n\nSo what Java-to-C compilers are available?\n\n========================================\n\nTop Answer:\nWhy do that? The Java virtual machine includes a runtime Java-to-assembly compiler.\n\nCompilation at runtime can yield better performance, since all information about runtime values is available. While ahead-of-time compilation has to take assumptions about runtime values and thus may emits less fast code. Please refer to Java vs C performance by Cliff Click for more details.\n\n========================================\n\nComments:\n- Found this link for the specific case of Oracle Database: download.oracle.com/docs/cd/B28359_01/java.111/b31225/&hellip;\n- What is the exact goal you want to accomplish? An EXE file?\n- Well, not an executable, but the source code in portable C.\n- Most of the complexity and functionality of of Java is in its libraries. Without these libraries as well what is the point?\n- The hard part is not making C out of the byte code (even if the C will be a horrible read), the hard part is to make something out of it that will either work with a GC suitable for C or without a GC and to translate every single piece of byte code in there for collections, exception handling etc. I really don't see the point in even considering it.\n- To Mr. Lawrey's point, I had the same question. It seems one can use Ahead-of-Time compilation to deliver core Java class libraries in natively compiled form.\n- Compilation at runtime *can* yield[] better performance. Depends on your use case, just like it says in the link. Not that that is a reason to compile java to c.\n- @dmckee true enough, added weasel worlds :)\n- \"Why do that?\". One possible reason could be that GCC, and other C compilers, target more platforms and architectures than any JIT, and probably more than the union of all JITs in existence. That said, if your target doesn't have a suitable JIT-enhanced JVM, that might be because it's too feeble for any practical Java code, and the Java->C->native route won't actually work either.\n- If your platform doesn't support Java, you're probably better off writing it in C or C++ than relying on a Java-to-native compiler. It'll run faster and be easier to work with. Also, I've heard of **no case** where a Java-to-native compiler beats Hotspot in anything but startup time. I don't think you need the weasel words.\n- @bobmcgee also never heard of that. In any case, the weasel words cover the case of equals speed, don't they? :)\n- Except the native-compiled Java is *slower*, rather than equal... :P\n- \"you're probably better off writing it in C or C++ than relying on a Java-to-native compiler\" - sure, but the questioner is asking how to compile Java bytecode (not Java source). Re-writing Java bytecode by hand in C doesn't sound much fun to me.\n- I may stand corrected about performance... but the differences usually aren't that huge, anyway, barring startup times. As for rewriting: Java and C++ aren't *that* different, and the C++ result has more room for hand optimizations, assembly, etc. If you're that focused on speed, you should do it the right way rather than relying on a java-to-native AOT compiler.\n- I believe this is one of the modes for GCJ, although it is normally compiled from source to bytecode or source to native. I agree about the poor performance though.\n- @BobMcGee Could you give please a link to the place where the capability to compile **into C** is documented for GCC/GCJ?\n- Could you give please a link to the place where the capability to compile **into C** is documented for GCC/GCJ?\n- The GCJ compiler is no longer available. Alternatively, it might be possible to compile Java bytecode to WebAssembly using TeaVM or JWebAssembly, and then decompile it to C using wasm2c.\n- Oh, damn. I could have sworn Excelsior JET was just doing a bytecode optimizer and proprietary JIT VM for that test. This is what you get for not reading carefully enough.\n- @BobMcGee: No problem. :) By the way, many customers buy Excelsior JET to protect their apps against Java decompilers; they do not care about performance as long as it is about the same as on HotSpot.\n- I'll take a look; I'm glad to see a recent product for this.","metadata":{"transformedAt":"2026-08-18T18:32:17.822Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":42,"estimatedTokens":1174}}719{"id":"stack-27587725","source":"stackoverflow","questionId":27587725,"title":"Where in the GCC source code does it compile to the different assembly languages?","tags":["c","gcc","assembly","compiler-construction"],"text":"Title: Where in the GCC source code does it compile to the different assembly languages?\nTags: c, gcc, assembly, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nWhere is the code in the GCC source code that actually constructs the assembly for the different architectures?\n\nWondering how many different assembly languages it compiles to, and how it actually does this (by taking a look at the source code).\n\nIs it in the gcc repo somewhere, or in another repo? I have started to dig around but haven't found anything.\n\nhttps://github.com/gcc-mirror/gcc\n\nFor example, here is some of the assembly generating code in V8:\n\nhttps://github.com/v8/v8-git-mirror/tree/master/src/x64\n\nIs there anything equivalent for GCC?\n\nI am wondering because it's a mystery how GCC does this, and it would be a great way to learn how compilers are actually implemented down to the assembly level.\n\n========================================\n\nTop Answer:\nThe actual source code for GCC is most accessible from here:\n\nhttps://gcc.gnu.org/svn.html\n\nThe software is accessible via SVN (subversion), a source code control system. This would be installed on many versions of Linux/UNIX, but if not on your platform, you can install the svn kit and then fetch the source using the following command:\n\n```\nsvn checkout svn://gcc.gnu.org/svn/gcc/trunk SomeLocalDir\n```\n\nGCC is complex and would take significant experience to understand the nature of how the application actually compiles to different architectures.\n\nIn a nutshell, GCC has three major components - front-end, middle and back-end processing. The front-end processor has the component of the language parsing to understand the syntax of languages (like C, C++, Objective-C, etc). The front-end deconstructs the code to a portable construct which is then passed to the back-end for compilation to the target environment.\n\nThe middle part performs code analysis and optimisation, attempting to prioritise the code to generate the best possible output at the end of the full process. Technically, optimisation can occur at any part of the process as patterns are discovered during analysis.\n\nThe back-end processor compiles the code to a tree-style output format (not actually final executable code). Based on what the expected output is designed to be, the \"pseudo-code\" is optimised for using registers, bit-sizes, endian-ness, and so on. The final code is then generated during the assembly phase, which converts the back-end code into machine executable instructions.\n\nIt's important to note that the compiler has many options to deal with output formats so you can create output to many classes of architecture, usually out of the box. For cross-compiling and target compiler options, try checking out this link:\n\nhttps://gcc.gnu.org/install/configure.html\n\n========================================\n\nCode:\n```text\n.md\n```\n\n```text\n.md\n```\n\n```text\nsvn checkout svn://gcc.gnu.org/svn/gcc/trunk SomeLocalDir\n```\n\n========================================\n\nComments:\n- `gcc` is very powerful, but at the same time it's extremely complex and unfriendly. I'd recommend to approach learning this area from the LLVM side. It's nice, clean and compact, with each step nicely isolated so it's much easier to comprehend. I'd suggest to start by reading this: eli.thegreenplace.net/2012/11/24/life-of-an-instruction-in-l&zwnj;&#8203;lvm\n- You forgot the middle-end part of GCC, which is bigger than front-end and back-end combined!\n- There are many locations where you can see the entire process - the Wiki page is a good start. The middle part didn't directly address the question, but I've added it in to the answer regardless.","metadata":{"transformedAt":"2026-08-18T18:32:17.822Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":67,"estimatedTokens":916}}720{"id":"stack-27348937","source":"stackoverflow","questionId":27348937,"title":"GHC pipeline: Core, STG - ASTs or text?","tags":["haskell","compiler-construction","compilation","ghc","abstract-syntax-tree"],"text":"Title: GHC pipeline: Core, STG - ASTs or text?\nTags: haskell, compiler-construction, compilation, ghc, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nIn the pipeline of GHC there is a stage of translating Haskell source code to Core and then (not necessarily as an immediate next step) translating Core to STG.\nHowever, one issue escapes me from my understanding - when do we have a \"normal\" code (i.e. as plain text), and when something actually living in memory, like abstract syntax trees (ASTs)?\n\nAnd to make my question a bit more precise, I'll divide it into parts:\n\n1) in the parsing of Haskell source file phase, do we immediately construct ASTs of Core language? If not, then it seems to me that we have to construct ASTs of full Haskell (which seems strange) and then either transform them to ASTs of Core, or firstly to textual representation of them in Core and again invoking parsing to obtain Core's ASTs.\n\n2) almost the same question applies to Core to STG transition (but in this case I think I can assume that what we have is Core's ASTs - correct?)\n\n========================================\n\nComments:\n- Take a look here, in particular here, then here, and here.\n- Fixed the links. That’s what I get for trying to produce clean `https:&#47;&#47;www.haskell.org&#47;ghc&#47;` links instead of weird `https:&#47;&#47;downloads.haskell.org&#47;~ghc&#47;` links.\n- I would avoid saying the compiler \"desugars to Core\". First, in ghc \"desugaring\" is a separate Haskell to Haskell phase before Core. Secondly, Core is a System-F derived language, and the translation from Haskell to Core is IMHO more involved than just desugaring.\n- I believe that `desugar` is the term used in GHC to convert from Haskell to Core: “The Desugarer (compiler/deSugar/Desugar.lhs) converts from the massive HsSyn type to GHC's intermediate language, CoreSyn.” (ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/HscMain)","metadata":{"transformedAt":"2026-08-18T18:32:17.822Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":480}}721{"id":"stack-3115971","source":"stackoverflow","questionId":3115971,"title":"Interpreter in Python: Making your own programming language?","tags":["python","compiler-construction","parsing","interpreter"],"text":"Title: Interpreter in Python: Making your own programming language?\nTags: python, compiler-construction, parsing, interpreter\nSource: Stack Overflow\n\nQuestion:\nRemember, this is using python.\nWell, I was fiddling around with an app I made called Pyline, today. It is a command line-like interface, with some cool features. However, I had an idea while making it: Since its like a \"OS\", wont it have its own language?\n\nWell, I have seen some articles online on how to make a interpreter, and parser, and compiler, but it wasn't really readable for me. All I saw was a crapload of code. I am one of those guys who need comments or a readme or SOME form or communication towards the user without the code itself, so I think that Stack Overflow would be great for a teenager like me. Can I get some help?\n\n========================================\n\nTop Answer:\nI just came by Xtext, a language development framework. Perhaps that's something you might want to take a look at.\n\nConsidering Python you might find it instructive to implement a version of Logo. If you want, you can skip the parsing/lexing stage for now and come up with a object oriented version first to get you going if your OOP skills are up to it. Later on you can hook it up with some graphics library to actually draw something.\n\nIn addition to Logo you might want to check out L-systems. See particularly The Algorithmic Beauty of Plants for inspiration.\n\n========================================\n\nComments:\n- What does *\"Since its like a \"OS\", wont it have its own language?\"* mean, btw?\n- if the question is \"Can I get some help?\" the answer is yes, provided you ask the right questions.\n- @Bart K.: Well, C++ made Windows, correct? That makes it the...if I may say \"Native\" (correct me if I am wrong) language, correct? Same thing here.\n- @Galilsnap, not quite. But I still don't understand what you mean... What is this *\"OS\"* you mention (your command line app?)? And by *\"it's own language\"* , what other language than Python could you mean?\n- @Galilsnap: All (99%) of operating systems have a foundation in C, with some guts of assembler to make system level stuff work. This is a completely different topic than creating a programming language.\n- If you refer to the application by \"OS\", then \"its own language\" is Python, of course, since you coded the app in that language. Presumably that's the \"native\" language you mean.\n- An interactive interpreter that takes commands as input is called a Shell. On Windows this is the CMD shell, on Linux it's usualy Bash. These are actually just applications that run on the operating system. They have built-in commands and also provide a convenient way to run other programs. GUI desktops are graphical shells that allow you to do things using a mouse instead of typing commands. I'd suggest reading up on OS architecture. Wikipedia is a great place to start.\n- Does this answer your question? Python parser for Python-like language\n- I wouldn't recommend *Language Implementation Patterns* to someone new to the subject.","metadata":{"transformedAt":"2026-08-18T18:32:17.822Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":31,"estimatedTokens":760}}722{"id":"stack-31637435","source":"stackoverflow","questionId":31637435,"title":"Is Python 3.5's grammar LL(1)?","tags":["python","parsing","compiler-construction","grammar"],"text":"Title: Is Python 3.5's grammar LL(1)?\nTags: python, parsing, compiler-construction, grammar\nSource: Stack Overflow\n\nQuestion:\nI saw http://matt.might.net/teaching/compilers/spring-2015/ saying Python 3.4 is LL(1)\n\nIs Python 3.5's grammar still LL(1) so one can write a recursive descent parser?\n\n========================================\n\nComments:\n- CPython 3.9 introduced a PEG parser which 'lift[s] the LL(1) restriction on the current Python grammar' and also that PEP says it 'allow[s] the elimination of multiple “hacks” that exist in the current grammar to circumvent the LL(1)-limitation', so I think this answer isn't quite true in practice either.\n- Subjectively, the new PEG grammar is quite awful though, and PEG as the standard is infectious and forces all Python implementations to use it. The PEP itself explicitly states that the new maintainers did not pick any of the better approaches because they did not understand parsing theory: \"We did not seriously consider alternative ways to implement the new parser, but [...] Other variants of LR were not considered, nor was LL (e.g. ANTLR). PEG was selected because it was easy to understand given a basic understanding of recursive-descent parsing.\"","metadata":{"transformedAt":"2026-08-18T18:32:17.822Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":14,"estimatedTokens":304}}723{"id":"stack-18917717","source":"stackoverflow","questionId":18917717,"title":"Advantages of using truncation towards minus infinity vs towards zero","tags":["haskell","compiler-construction","truncate"],"text":"Title: Advantages of using truncation towards minus infinity vs towards zero\nTags: haskell, compiler-construction, truncate\nSource: Stack Overflow\n\nQuestion:\nI was wondering which are the benefits of using truncation towards minus infinity (Haskell, Ruby) instead of truncation towards zero (C, PHP), from the perspective of programming languages/compilers implementation. \n\nIt seems that truncating towards minus infinity is the right way to go, but I haven’t found a reliable source for such claiming, nor how such decision impact the implementation of compilers. I’m particularly interested in possible compilers optimizations, but not exclusively.\n\n**Related sources:**\n\nDivision in Haskell\n\nWhen is the difference between quotRem and divMod useful?\n\n========================================\n\nTop Answer:\nHere is a quote from the (informative) Rationale in Annex C of ISO/IEC 10967-1:2012 *Language independent arithmetic* (vl. LIA-1) C.5.1.2.2. Ellipsis ... inserted by me.\n\n... Two rounding rules are in common use: *round toward minus infinity* (*quotI*), and *round toward zero*. The latter is not specified in LIA-1, due to proneness for erroneous use, when the arguments are of different signs. For example,\n\n*quotI*(-3,2) = -2    round toward minus infinity, specified in LIA-1\n\n*divtI*(-3,2) = -1     round toward zero, no longer specified by any part of LIA\n\n*quotI* ... as well as ... all satisfy a broadly useful translation invariant:\n\n   *quotI*(*x + i * y*, y) = *quotI*( *x*, *y*) + *i*    if *y* ≠ 0, and no overflow occurs\n\n... *quotI* is the form of integer division preferred by many mathematicians. *divtI* (no longer specified by LIA) is the form of division introduced by Fortran.\n\nInteger division is frequently used for grouping. For example, if a series of indexed items are to be partitioned into groups of *n* items, it is natiural to put item `i` into group `i/n`. This works fine if *quotI* is used for integer division. However if *divtI* (no longer specified in LIA) is used, and `i` can be negative, group 0 will get 2 ⋅ *n*-1 items rather than the desired *n*. This uneven behaviour for negative `i` can cause subtle program errors, and is a strong reason against the use of *divtI* ...\n\n========================================\n\nCode:\n```text\nquotient * divisor + remainder = dividend\n```\n\n```text\n(-5) % 3 = -2\n```\n\n```text\n(-5) / 3 = -1\n```\n\n```text\n(-1) * 3 + (-2) = -5\n```\n\n```text\n5 % (-3) = 2\n```\n\n```text\n5 / (-3) = -1\n```\n\n```text\n(-1) * (-3) + 2 = 5\n```\n\n```text\n(-5) % 3 = 1\n```\n\n```text\n(-5) / 3 = -2\n```\n\n```text\n(-2) * 3 + 1 = -5\n```\n\n```text\n5 % (-3) = -1\n```\n\n```text\n5 / (-3) = -2\n```\n\n```text\n(-2) * (-3) + (-1) = 5\n```\n\n```text\nx % 2\n```\n\n```text\nx\n```\n\n```text\ni\n```\n\n```text\ni/n\n```\n\n```text\ni\n```\n\n```text\ni\n```\n\n========================================\n\nComments:\n- The paper's conclusion is a bit more concrete. It states that truncation towards minus infinity (called F-definition) is the **only** comparable (in desirable properties) to the E-definition.\n- And both truncation towards minus infinity and this \"Euclidean\" definition are identical for positive divisors (that is, both `div` and `mod` are the same). The difference shows only for negative divisors.\n- There isn't an x86 instruction for integer division by arbitrary divisors that rounds toward negative infinity, but there is an instruction for dividing signed numbers by powers of two which does so (arithmetic right shift). Further, for many constant divisors, round-toward-negative-infinity division could be performed with four fast instructions (load, multiply, add, shift), but round-toward-zero makes things slower and more complicated.\n- Clearly just a small typo, but where you wrote `5 % (-3) = 1` I think you meant `5 % (-3) = -1` (tested in python)\n- @JoshuaPerrett: fixed\n- I can't think of any time I've found round-toward-zero, nor the associated remainder function, useful. Further, the only code I can think of where the a==(a/b)*b + a%b guarantee was useful was code which was trying to work around the fact that division uses round-toward-zero. Another place where round-toward-negative-infinity wins, btw: computing things like the rounded average of three numbers. The expression `(a+b+c+1)&#47;3` works nicely if the average is positive, or if one is using round-toward-negative-infinity division. How would one do the equivalent with round-toward-zero?\n- @supercat: It needs to be repeated over and over again :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.822Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":19,"totalLines":123,"estimatedTokens":1116}}724{"id":"stack-16327483","source":"stackoverflow","questionId":16327483,"title":"What's the difference between a boolean literal and a boolean value?","tags":["javascript","compiler-construction","computer-science"],"text":"Title: What's the difference between a boolean literal and a boolean value?\nTags: javascript, compiler-construction, computer-science\nSource: Stack Overflow\n\nQuestion:\nI can't seem to find a clear explanation as to what the difference is between these two. I'd also like to point out that I don't really understand the difference between literals and values either. \n\nDo boolean literals use the Boolean object?\n\n========================================\n\nTop Answer:\nValues are expressions that can't be evaluated any longer. That means, these are values:\n\n- x\n\n- 123\n\n- true\n\n- \"asdqwe\"\n\nNow, literals are fixed value expressions. From the above list, the following are literals:\n\n- 123\n\n- true\n\n- \"asdqwe\"\n\nSo, `x` has a value but not fixed.\n\nAnswering the main question, booleans can only have two literals: `false` and `true`, and every boolean variable is a boolean value.\n\nYou will see this in college in a compilers or computer semantics course, but the wikipedia pages linked here are very good if you still don't understand the difference.\n\n========================================\n\nCode:\n```text\n1; // 1 is a literal\nvar x = 2; // x takes the value of the literal 2\nx = x + 3; // Adds the value of the literal 3 to x. x now has the value 5, but 5 is not a literal.\n```\n\n```text\ntrue; // this is a `boolean` primitive\nnew Boolean(true); // This results in an object, but the literal `true` is still a primitive\n(true).toString(); // The literal true is converted into a Boolean object and its toString method is called\nif(new Boolean(false)) { alert('Eh?'); }; // Will alert, as every Boolean object that isn't null or undefined evaluates to true (since it exists)\n```\n\n```text\nboolean\n```\n\n```text\nnumber\n```\n\n```text\n(152).toString()\n```\n\n```text\nboolean\n```\n\n```text\nBoolean\n```\n\n```text\nBoolean\n```\n\n```text\nx\n```\n\n```text\nfalse\n```\n\n```text\ntrue\n```\n\n```text\n[true, false, null, undefined, 1, 0, NaN, Infinity, \"true\", \"false\", \"\", [], {}, new Boolean(false)]\n.forEach(e => console.debug(\n   [ !!e, Boolean(e), (new Boolean(e)).valueOf() ], e\n))\n\n// !!e, Boolean, valueOf\n[true,  true,    true]   true\n[false, false,   false]  false\n[false, false,   false]  null\n[false, false,   false]  undefined\n[true,  true,    true]   1\n[false, false,   false]  0\n[false, false,   false]  NaN\n[true,  true,    true]   Infinity\n[true,  true,    true]   \"true\"\n[true,  true,    true]   \"false\"\n[false, false,   false]  \"\"\n[true,  true,    true]   []\n[true,  true,    true]   Object {}\n[true,  true,    true]   Boolean {[[PrimitiveValue]]: false} // new Boolean(false)\n```\n\n```text\nBoolean(x) === !!x\n\ntypeof true == \"boolean\"\ntypeof Boolean(x) == \"boolean\"\ntypeof new Boolean(x) == \"object\" // not boolean!\ntypeof Boolean == \"function\"\n\nBoolean(new Boolean(false)) == true // <- any object converted to boolean is true!\nBoolean(new Boolean(false).valueOf()) == false\n```\n\n```text\n!!x\n```\n\n```text\nBoolean(x)\n```\n\n```text\nnew Boolean(x).valueOf()\n```\n\n========================================\n\nComments:\n- boolean value or variable?","metadata":{"transformedAt":"2026-08-18T18:32:17.822Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":139,"estimatedTokens":758}}725{"id":"stack-4091887","source":"stackoverflow","questionId":4091887,"title":"How would you go about writing a simple programming language?","tags":["compiler-construction","language-design"],"text":"Title: How would you go about writing a simple programming language?\nTags: compiler-construction, language-design\nSource: Stack Overflow\n\nQuestion:\n**Possible Duplicates:**\n\n Methodologies for designing a simple programming language\n\n Learning to write a compiler \n\nI would like to write a programming language with a syntax similar to QBasic but even simpler. I want it to be for beginning programmers. Its simplicity will encourage aspiring programmers not to give up and get them interested in programming. For example:\nInstead of QBasic's\nPRINT \"Hello World!\"\n\nI would use\n\nWrite \"Hello World!\"\n\nor a little more like VB\n\nWrite (\"Hello World\")\n\nHow would I go about adapting the basic syntax to make my language?\n\n========================================\n\nTop Answer:\nI think the up shot of this is:\n\n- Simple to use.\n\n- Simple to design/implement.\n\n- Strong expressive abilities.\n\nPick 1.9 of them.\n\nIt's very possible to get a reasonable degree of any two of those. Doing any two fully is **very** hard and trying to get all three leaves you in a no-mans-land where you don't do any well.\n\np.s. I speek from experiance for #1+#3\n\n========================================\n\nComments:\n- Possible duplicate of stackoverflow.com/q/1208338/246069\n- \"Its simplicity will encourage aspiring programmers not to give up and get them interested in programming.\" -- Not to discourage you, but simplistic languages tend to get into one's way after a short time. I prefer languages that are *simple*, but don't fall short for larger/more sophisticated tasks (in particular, Python). Apart from that, are you asking for input how the syntax should be like, or do you want hints on how to actually parse it?\n- You're right. I started last year learning BASIC and I really enjoyed it so now I'm attempting to learn C so I can do some deeper programming. I also learned visual basic. I do think however, that If I can somehow modify the basic syntax, I could possibly add my own commands as well making it a more powerful version of BASIC but with a simpler syntax.\n- Also what do you mean by parse. I want to make a language that I can write a compiler (or modify an existing one) for and adapt Qbasic syntax to my language.\n- It seems like you are asking two questions at once - which is as lethal as trying to tackle two problems at once instead of solving them seperately. Are you (1) asking how to write a compiler for a programming language? There are a few questions on this topic on SO, and unless you have a specific question not covered by those, this question is a duplicate. Or are you (2) asking for ideas on a QBasic-like syntax?\n- I am asking 1. How would I write a compiler for a QBasic like syntax and 2. If possible, could I modify an existing QBasic compiler to compile my language. I would gladly accept any ideas on syntax though!\n- Well, you could modify an existing compiler. But even if it is a very well-written piece of code, hacking it will require the same knowledge needed to write a compiler out of thin air (even more if the original author(s) felt like being clever and wrote messy code).\n- How would I modify an existing compiler and which one (If possible I'd prefer QBasic because I like the programming environment)\n- This is a big subject, but does *not* involve much of (at least what most people would think of as) math.\n- Thank you very much for this thorough answer. I have a few questions about your answer. What does parsing mean? What category would a language like BASIC fall under?\n- A few remarks: (1) No, it's not math - but highly abstract stuff nonetheless, yeah. (2) JavaScript isn't interpreted since forever (most implementations even JIT compile now). These days, no serious language solely interprets the source code or even the AST directly (older Ruby implementations did, languages with compiletime metaprogramming propably do). (3) (E)BNF is useful to know, but the DSLs parser generators use either differ or are completely unrelated, so it's not the most important things. Not to mention that that's only the grammar, you still have to build an AST and make it run.\n- Parsing is the process of taking a sequence of \"tokens\" (which are usually the words, symbols, etc of a language), and finding out what that sequence logically represents. In computer languages, parsing is what happens when a compiler breaks up source code into it's parts, and then analyzes what those parts are supposed to mean.\n- @delnan: I guess you're right. I haven't studied this stuff in about 7 years or so, so I guess I might not be up to snuff on the current state of things.\n- Thank you. I have a few Questions 1. What is a DSLs parser generator 2. What is (E)BNF? 3. What is AST? Thanks\n- @RCProgramming: DSL stands for \"domain specific language\", parser generators (like the already mentioned lex/yacc) define their own little language one can specify the grammar relatively easily. Of course the language is specific to that domain - it's not usful for anything else, but it does this one thing well. BNF is the Backus-Naur Form Bryan mentioned, the E is for extended BNF - which is, well, an extension of BNF with a few advantages. AST stands for abstract syntax tree, the data structure the compiler build during/after parsing (this is what all other parts work on).\n- Thank you very much. Have you ever written a language before? If so could you give me an example of it because I might be interested in programming in it because I would like to try something non mainstream\n- @RCProgramming: Me? No, nothing which is complete enough to count as (usable) programming language. I only play around with various parts of the big picture. If you want to try a great non-mainstream language, there are several. If you got a few years to have your mind blown again and again, try Haskell ;)\n- Ill look into that. Can you give me an example of your \"non usable\" language?\n- For the most part, it's a parser without backend (code generator) - e.g. a Lisp - or a backend without parser - a minimalistic language for arithmetic. Incidentally, both are in Haskell because Haskell has a neat parsing library, parsec, and is great for handling trees. I also have an incomplete/work-in-progress \"compiler\" for an utterly limited pascal-like language I want to extend with a few advanced features, but at the time being, it can't even parse expressions.","metadata":{"transformedAt":"2026-08-18T18:32:17.823Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":64,"estimatedTokens":1594}}726{"id":"stack-15259131","source":"stackoverflow","questionId":15259131,"title":"Static, global and multiple translation units","tags":["c++","compiler-construction","static","global"],"text":"Title: Static, global and multiple translation units\nTags: c++, compiler-construction, static, global\nSource: Stack Overflow\n\nQuestion:\nThis consists of three questions which are linked:\n\n1) How exactly do you end up with multiple translation units? Surely all the source and header files would \"join\" together because otherwise you would end up with code calling code which isn't part of that \"program\". Isn't a translation unit the same as a program?\n\n2) How do static objects/classes work with regards to multiple translation units? If several source files in different translation units include a header which would create an object (that had a static data member) would there be a separate static data member per translation unit? \n\n3) Similar to question 2, do global variables get shared across multiple translation units (a bit like me suggesting they were separate programs- just compiled together???) or do global variables still act.... global across all translation units?\n\n========================================\n\nTop Answer:\nNo, they each are compiled into an object file which are subsequently linked together.\n\nIf the object is declared but not defined in the header, which is the better practice, then there will just be one object defined (*assuming* it is in fact defined in exactly one source file) and everyone who includes the header will have a way of programming with it.\n\n`static` objects in a source file that would otherwise be in what\nappears to be global scope will not be accessible outside that\ntranslation unit. Outside of any class or function, `static` limits\nscope to the translation unit.\n\nNon-statically-scoped objects, yes, they do.\n\n========================================\n\nCode:\n```text\nstatic\n```\n\n```text\nstatic\n```\n\n========================================\n\nComments:\n- en.wikipedia.org/wiki/Linker_(computing) may help you.\n- Regarding number 1, if the answer is \"No\" then what is the purpose of needing to consider a \"translation unit\" when mixing inline and static code?","metadata":{"transformedAt":"2026-08-18T18:32:17.823Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":43,"estimatedTokens":505}}727{"id":"stack-41811353","source":"stackoverflow","questionId":41811353,"title":"Why is ebx saved in the stack frame of a simple function, calling gets?","tags":["c","gcc","assembly","compiler-construction","reverse-engineering"],"text":"Title: Why is ebx saved in the stack frame of a simple function, calling gets?\nTags: c, gcc, assembly, compiler-construction, reverse-engineering\nSource: Stack Overflow\n\nQuestion:\nI am trying to write a buffer overflow exercise in c for students.\n\nNormally the stack frame consists of the function parameters, the return address, the base pointer and local variables. But I detected, that sometimes additional registers are saved along with the base pointer. I remember from class that calee saved registers have to be saved before they are used. But there are cases where the compilation of C code produces assembly, which saves and uses registers apperently without purpose. Please explain this behaviour to me.\n\nAssume the main function\n\n```\nint main (int argc, char** argv) {\n func();\n\n return 0;\n}\n```\n\nand the function\n\n```\nvoid func() {\n char buf[5];\n strcpy(buf,\"AAAA\");\n strcpy(buf,\"BBBB\");\n}\n```\n\nIf I debug the resulting executable with gdb\n\n```\nbreak func\nrun\ninfo frame\n```\n\neverything is fine and the stack frame only contains ebp and eip.\n\nIf I use\n\n```\nvoid func() {\n char buf[5];\n gets(buf);\n}\n```\n\nI get\n\n```\nSaved registers:\n ebx at 0xffffd1cc, ebp at 0xffffd1d0, eip at 0xffffd1d4\n```\n\nSo ebx is additionally saved in the stack frame? Why? If I run\n\n```\ndisas func\n```\n\nI get\n\n```\nDump of assembler code for function func:\n 0x56555730 : push %ebp\n 0x56555731 : mov %esp,%ebp\n 0x56555733 : push %ebx\n 0x56555734 : sub $0x8,%esp\n 0x56555737 : call 0x5655576e \n 0x5655573c : add $0x18c4,%eax\n=> 0x56555741 : lea -0x9(%ebp),%edx\n 0x56555744 : push %edx\n 0x56555745 : mov %eax,%ebx\n 0x56555747 : call 0x56555590 \n 0x5655574c : add $0x4,%esp\n 0x5655574f : nop\n 0x56555750 : mov -0x4(%ebp),%ebx\n 0x56555753 : leave \n 0x56555754 : ret \nEnd of assembler dump.\n```\n\nSo ebx is saved. Ok. But what is it used for? eax is moved in ebx before calling gets(). But it is not used afterwards. The old ebx is just restored from the stack before leaving and returning. It seems useless.\nBtw. Whats the whole `call get_pc_thunk` stuff?\n\nComparable behaviour, if I use printf instead of gets:\n\n```\nvoid func() {\n char buf[5];\n strcpy(buf, \"AAAA\");\n printf(\"%s\",buf);\n}\n```\n\ngdb output:\n\n```\n(gdb) info frame\nStack level 0, frame at 0xffffd1d8:\n eip = 0x56555741 in func (/home/mischa/stuff/test/test.c:35); saved eip = 0x56555779\n called by frame at 0xffffd1e0\n source language c.\n Arglist at 0xffffd1d0, args: \n Locals at 0xffffd1d0, Previous frame's sp is 0xffffd1d8\n Saved registers:\n ebx at 0xffffd1cc, ebp at 0xffffd1d0, eip at 0xffffd1d4\n(gdb) disas func\nDump of assembler code for function func:\n 0x56555730 : push %ebp\n 0x56555731 : mov %esp,%ebp\n 0x56555733 : push %ebx\n 0x56555734 : sub $0x8,%esp\n 0x56555737 : call 0x56555780 \n 0x5655573c : add $0x18c4,%eax\n=> 0x56555741 : movl $0x41414141,-0x9(%ebp)\n 0x56555748 : movb $0x0,-0x5(%ebp)\n 0x5655574c : lea -0x9(%ebp),%edx\n 0x5655574f : push %edx\n 0x56555750 : lea -0x17f0(%eax),%edx\n 0x56555756 : push %edx\n 0x56555757 : mov %eax,%ebx\n 0x56555759 : call 0x565555a0 \n 0x5655575e : add $0x8,%esp\n 0x56555761 : nop\n 0x56555762 : mov -0x4(%ebp),%ebx\n 0x56555765 : leave \n 0x56555766 : ret \nEnd of assembler dump.\n```\n\nCan someone please explain this to me?\n\nI use cmake for compiling with the following CMakeLists.txt:\n\n```\ncmake_minimum_required (VERSION 2.8)\n\n# projectname is the same as the main-executable\nproject(test)\n\n# compile with 32 bit\nadd_definitions('-m32')\n\n# Disable compiler optimization\nadd_definitions('-O0')\n\n# include debugging information\nadd_definitions('-g')\n\n# Align items on the stack to 4 bytes. This makes stuff easier.\n# See https://stackoverflow.com/questions/1061818/stack-allocation-padding-and-alignment\nadd_definitions('-mpreferred-stack-boundary=2')\n\n# disable compiler buffer overflow protection\nadd_definitions('-z execstack -z norelro -fno-stack-protector')\n\n# executable source code\nadd_executable(test test.c)\n```\n\ncmake seems to use gcc.\n\n========================================\n\nCode:\n```text\nint main (int argc, char** argv) {\n    func();\n\n    return 0;\n}\n```\n\n```text\nvoid func() {\n    char buf[5];\n    strcpy(buf,\"AAAA\");\n    strcpy(buf,\"BBBB\");\n}\n```\n\n```text\nbreak func\nrun\ninfo frame\n```\n\n```text\nvoid func() {\n    char buf[5];\n    gets(buf);\n}\n```\n\n```text\nSaved registers:\n  ebx at 0xffffd1cc, ebp at 0xffffd1d0, eip at 0xffffd1d4\n```\n\n```text\ndisas func\n```\n\n```text\nDump of assembler code for function func:\n   0x56555730 <+0>: push   %ebp\n   0x56555731 <+1>: mov    %esp,%ebp\n   0x56555733 <+3>: push   %ebx\n   0x56555734 <+4>: sub    $0x8,%esp\n   0x56555737 <+7>: call   0x5655576e <__x86.get_pc_thunk.ax>\n   0x5655573c <+12>:    add    $0x18c4,%eax\n=> 0x56555741 <+17>:    lea    -0x9(%ebp),%edx\n   0x56555744 <+20>:    push   %edx\n   0x56555745 <+21>:    mov    %eax,%ebx\n   0x56555747 <+23>:    call   0x56555590 <gets@plt>\n   0x5655574c <+28>:    add    $0x4,%esp\n   0x5655574f <+31>:    nop\n   0x56555750 <+32>:    mov    -0x4(%ebp),%ebx\n   0x56555753 <+35>:    leave  \n   0x56555754 <+36>:    ret    \nEnd of assembler dump.\n```\n\n```text\nvoid func() {\n    char buf[5];\n    strcpy(buf, \"AAAA\");\n    printf(\"%s\",buf);\n}\n```\n\n```text\n(gdb) info frame\nStack level 0, frame at 0xffffd1d8:\n eip = 0x56555741 in func (/home/mischa/stuff/test/test.c:35); saved eip = 0x56555779\n called by frame at 0xffffd1e0\n source language c.\n Arglist at 0xffffd1d0, args: \n Locals at 0xffffd1d0, Previous frame's sp is 0xffffd1d8\n Saved registers:\n  ebx at 0xffffd1cc, ebp at 0xffffd1d0, eip at 0xffffd1d4\n(gdb) disas func\nDump of assembler code for function func:\n   0x56555730 <+0>: push   %ebp\n   0x56555731 <+1>: mov    %esp,%ebp\n   0x56555733 <+3>: push   %ebx\n   0x56555734 <+4>: sub    $0x8,%esp\n   0x56555737 <+7>: call   0x56555780 <__x86.get_pc_thunk.ax>\n   0x5655573c <+12>:    add    $0x18c4,%eax\n=> 0x56555741 <+17>:    movl   $0x41414141,-0x9(%ebp)\n   0x56555748 <+24>:    movb   $0x0,-0x5(%ebp)\n   0x5655574c <+28>:    lea    -0x9(%ebp),%edx\n   0x5655574f <+31>:    push   %edx\n   0x56555750 <+32>:    lea    -0x17f0(%eax),%edx\n   0x56555756 <+38>:    push   %edx\n   0x56555757 <+39>:    mov    %eax,%ebx\n   0x56555759 <+41>:    call   0x565555a0 <printf@plt>\n   0x5655575e <+46>:    add    $0x8,%esp\n   0x56555761 <+49>:    nop\n   0x56555762 <+50>:    mov    -0x4(%ebp),%ebx\n   0x56555765 <+53>:    leave  \n   0x56555766 <+54>:    ret    \nEnd of assembler dump.\n```\n\n```text\ncmake_minimum_required (VERSION 2.8)\n\n# projectname is the same as the main-executable\nproject(test)\n\n# compile with 32 bit\nadd_definitions('-m32')\n\n# Disable compiler optimization\nadd_definitions('-O0')\n\n# include debugging information\nadd_definitions('-g')\n\n# Align items on the stack to 4 bytes. This makes stuff easier.\n# See https://stackoverflow.com/questions/1061818/stack-allocation-padding-and-alignment\nadd_definitions('-mpreferred-stack-boundary=2')\n\n# disable compiler buffer overflow protection\nadd_definitions('-z execstack -z norelro -fno-stack-protector')\n\n# executable source code\nadd_executable(test test.c)\n```\n\n```text\ncall get_pc_thunk\n```\n\n```text\nebx\n```\n\n```text\nebx\n```\n\n```text\n-fno-plt\n```\n\n```text\nebx\n```\n\n========================================\n\nComments:\n- Because you disabled optimizations?\n- `ebx` is used as a pointer to the GOT, but that's really only relevant for position independent code. It's not clear from your cmake stuff whether you compiled it thusly. To reproduce the behavior I had to pass `-fPIC` command line option to `gcc`.\n- Worth noting that `strcpy(buf,\"AAAA\");` is being inlined into a `movl` followed by a `movb`, so no function is being called at all to execute it. (See the code pointed to in your last disassembly.)\n- Thank you! How can I disable PIE creation? I would like to compare the created code.\n- @MichaelPalm: Use `-fno-PIE` when compiling and `-no-pie` or `-nopie` when linking, or both for compiling and linking if you like. Whether you need `-no-pie` or `-nopie` depends on whether you're using GCC 6+ with upstream pie-by-default support (the former) or the default-pie patches for older gcc that originated from Gentoo (the latter form).\n- Worked for me :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.823Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":15,"totalLines":322,"estimatedTokens":2029}}728{"id":"stack-5607762","source":"stackoverflow","questionId":5607762,"title":"What does \"code motion\" mean for \"loop-invariant code motion\"?","tags":["compiler-construction"],"text":"Title: What does \"code motion\" mean for \"loop-invariant code motion\"?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn compilers, the phrase \"loop-invariant code motion\" describes expressions or statements of code in a loop that don't change from iteration to iteration and hence can be moved outside of the loop to be computed once. \n\nI understand the \"loop-invariant\" piece of the phrase, but what does the \"code motion\" mean?\n\n========================================\n\nTop Answer:\nSeems as though it means from the Wikipedia article that the loop-invariant code is actually moved outside the loop as an optimization step.\n\n========================================\n\nCode:\n```text\nfor ( int x=0; x < string.length(); x++) {\n    //other code here\n}\n```\n\n```text\nlength()\n```\n\n========================================\n\nComments:\n- Perhaps I'm being too picky. I understand that the code is moved outside the loop, but \"motion\" implies continuing, ongoing displacement, whereas \"movement\" implies a single, one-time change of position. Perhaps I'm reading into the phrase too much.\n- @Ross I think you are reading into it too much. code-movement would be a better name for it as its definitely not continuous. :)","metadata":{"transformedAt":"2026-08-18T18:32:17.823Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":32,"estimatedTokens":307}}729{"id":"stack-5281070","source":"stackoverflow","questionId":5281070,"title":"Compiler optimization about elimination of pointer operation on inline function in C?","tags":["c","optimization","pointers","compiler-construction"],"text":"Title: Compiler optimization about elimination of pointer operation on inline function in C?\nTags: c, optimization, pointers, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIf this function `Func1` is inlined,\n\n```\ninline int Func1 (int* a)\n{\n return *a + 1;\n}\n\nint main ()\n{\n int v = GetIntFromUserInput(); // Unknown at compile-time. \n return Func1(&v);\n}\n```\n\nCan I expect a smart compiler to eliminate the pointer operations? (`&a` and `*a`)\nAs I guess, the function will be transformed into something like this,\n\n```\nint main ()\n{\n int v = GetIntFromUserInput(); // Unknown at compile-time.\n int* a = &v;\n return *a + 1;\n}\n```\n\nand finally, \n\n```\nint main ()\n{\n int v = GetIntFromUserInput(); // Unknown at compile-time.\n return v + 1;\n}\n```\n\nPointer operations look easily being eliminated. But I heard that pointer operation is something special and cannot be optimized.\n\n========================================\n\nTop Answer:\nYes the compiler, as said by Wallyk, is able to remove useless operations in this case. \n\nHowever you must remember that when you specify a function signature something is lost in the translation from your problem domain to C. Consider the following function:\n\n```\nvoid transform(const double *xyz, // Source point\n double *txyz, // Transformed points\n const double *m, // 4x3 transformation matrix\n int n) // Number of points to transform\n{\n for (int i=0; iI think that the intent is clear, however the compiler must be paranoid and consider that the generated code must behave exactly as described by the C semantic even in cases that are of course not part of the original problem of transforming an array of points like:\n\n- `txyz` and `xyz` are pointing to the same memory address, or maybe they are pointing to adjacent doubles in memory\n\n- `m` is pointing inside the `txyz` area\n\nThis means that for the above function the C compiler is forced to assume that after each write to `txyz` any of `xyz` or `m` could change and so those values cannot be loaded in free order. The resulting code consequently will not be able to take advantage of parallel execution for example of the computations of the tree coordinates even if the CPU would allow to do so.\n\nThis case of aliasing was so common that C99 introduced a specific keyword to be able to tell the compiler that nothing so strange was intended. Putting the `restrict` keyword in the declaration of `txyz` and `m` reassures the compiler that the pointed-to memory is not accessible using other ways and the compiler is then allowed to generate better code.\n\nHowever this \"paranoid\" behavior is still necessary for all operations to ensure correctness and so for example if you write code like\n\n```\nchar *s = malloc(...);\n char *t = malloc(...);\n ... use s and t ...\n```\n\nthe compiler has no way to know that the two memory areas will be non-overlapping or, to say it better, there is no way to define a signature in the C language to express the concept that returned values from `malloc` are \"non overlapping\". This means that the paranoid compiler (unless some non-standard declarations are present for malloc and the compiler has a special handling for it) will think in the subsequent code that any write to something pointed by `s` will possibly overwrite data pointed by `t` (even when you're not getting past the size passed to `malloc` I mean ;-) ).\n\nIn your example case even a paranoid compiler is allowed to assume that\n\n- no one will know the address of a local variable unless getting it as a parameter\n\n- no unknown external code is executed between the reading and computation of addition\n\nIf both those points are lost then the compiler must think to strange possibilities; for example\n\n```\nint a = malloc(sizeof(int));\n*a = 1;\nprintf(\"Hello, world.\\n\");\n// Here *a could have been changed\n```\n\nThis crazy thought is necessary because `malloc` knows the address of `a`; so it could have passed this information to `printf`, which after printing the string could use that address to change the content of the location. This seems clearly absurd and maybe the library function declaration could contain some special unportable trick, but it's necessary for correctness in general (imagine `malloc` and `printf` being two user defined functions instead of library ones).\n\nWhat does all this blurb mean? That yes, in your case the compiler is allowed to optimize, but it's very easy to remove this possibility; for example\n\n```\ninline int Func1 (int* a) {\n printf(\"pointed value is %i\\n\", *a);\n return *a + 1;\n}\n\nint main () {\n int v = GetIntFromUserInput(); // Assume input value is non-determinable.\n printf(\"Address of v is %p\\n\", &v);\n return Func1(&v);\n}\n```\n\nis a simple variation of your code, but in this case the compiler cannot avoid assuming that the second `printf` call could have changed the pointed memory even if it's passed just the pointed value and not the address (because the first call to `printf` was passed the address and so the compiler must assume that potentially that function could have stored the address to use it later to alter the variable).\n\nA very common misconception in C and C++ is that liberal use of the keyword `const` with pointers or (in C++) references will help the optimizer generating better code.\nThis is completely false:\n\n- In the declaration `const char *s` the nothing is said about that the pointed character is going to be constant; it's simply said that it is an error to change the pointed character **using that pointer**. In other words `const` in this case simply means that the pointer is \"readonly\" but doesn't tell that, for example, other pointers could be used to changed the very same memory pointed to by `s`.\n\n- It is legal in C (and C++) to \"cast away\" const-ness from a pointer (or reference) to constant. So the paranoid compiler must assume that even a function has been only handed a `const int *` the function could store that pointer and later can use it to change the memory pointed to.\n\nThe `const` keyword with pointers (and C++ references) is only meant as an aid for the programmer to avoid unintentional writing use of a pointer that was thought as being used only for reading. Once this check is performed then this `const` keyword is simply forgotten by the optimizer because it has no implications in the semantic of the language.\n\nSometimes you may find another silly use of the `const` keyword with parameters that tells that the value of the parameter cannot be changed; for example `void foo(const int x)`.\nThis kind of use has no real philosophical meaning for the signature and simply puts some little annoyance on the implementation of the called function: a parameter is a copy of a value and caller shouldn't care if the called function is going to change that copy or not... the called function can still make a copy of the parameter and change that copy so nothing is gained anyway.\n\nTo recap... when the compiler sees\n\n```\nvoid foo(const int * const x);\n```\n\nmust still assume that foo will potentially store away a copy of the passed pointer and that can use this copy to change the memory pointed to by `x` immediately or later when you call any other unknown function.\n\nThis level of paranoia is required because of how the language semantic is defined.\n\nIt is very important to understand this \"aliasing\" problem (there can be different ways to alter the same writable area of memory), especially with C++ where there is a common anti-pattern of passing around const references instead of values even when logically the function should accept a value. See this answer if you are also using C++.\n\nAll these are the reasons for which when dealing with pointers or references the optimizer has much less freedom than with local copies.\n\n========================================\n\nCode:\n```text\ninline int Func1 (int* a)\n{\n    return *a + 1;\n}\n\nint main ()\n{\n    int v = GetIntFromUserInput(); // Unknown at compile-time.   \n    return Func1(&v);\n}\n```\n\n```text\nint main ()\n{\n    int v = GetIntFromUserInput(); // Unknown at compile-time.\n    int* a = &v;\n    return *a + 1;\n}\n```\n\n```text\nint main ()\n{\n    int v = GetIntFromUserInput(); // Unknown at compile-time.\n    return v + 1;\n}\n```\n\n```text\nFunc1\n```\n\n```text\n&a\n```\n\n```text\n*a\n```\n\n```text\n.globl main\n        .type   main, @function\nmain:\n        pushl   %ebp\n        movl    %esp, %ebp\n        andl    $-16, %esp\n        call    GetIntFromUserInput\n        movl    %ebp, %esp\n        popl    %ebp\n        addl    $1, %eax\n        ret\n```\n\n```text\ngcc -O3\n```\n\n```text\ninline\n```\n\n```text\nstatic\n```\n\n```text\nstatic\n```\n\n```text\ninline\n```\n\n```text\nvoid transform(const double *xyz, // Source point\n               double *txyz,      // Transformed points\n               const double *m,   // 4x3 transformation matrix\n               int n)             // Number of points to transform\n{\n    for (int i=0; i<n; i++) {\n        txyz[0] = xyz[0]*m[0] + xyz[1]*m[3] + xyz[2]*m[6] + m[9];\n        txyz[1] = xyz[0]*m[1] + xyz[1]*m[4] + xyz[2]*m[7] + m[10];\n        txyz[2] = xyz[0]*m[2] + xyz[1]*m[5] + xyz[2]*m[8] + m[11];\n        txyz += 3; xyz += 3;\n    }\n}\n```\n\n```text\nchar *s = malloc(...);\n char *t = malloc(...);\n ... use s and t ...\n```\n\n```text\nint a = malloc(sizeof(int));\n*a = 1;\nprintf(\"Hello, world.\\n\");\n// Here *a could have been changed\n```\n\n```text\ninline int Func1 (int* a) {\n    printf(\"pointed value is %i\\n\", *a);\n    return *a + 1;\n}\n\nint main () {\n    int v = GetIntFromUserInput();   // Assume input value is non-determinable.\n    printf(\"Address of v is %p\\n\", &v);\n    return Func1(&v);\n}\n```\n\n```text\nvoid foo(const int * const x);\n```\n\n```text\ntxyz\n```\n\n```text\nxyz\n```\n\n```text\nm\n```\n\n```text\ntxyz\n```\n\n```text\ntxyz\n```\n\n```text\nxyz\n```\n\n```text\nm\n```\n\n```text\nrestrict\n```\n\n```text\ntxyz\n```\n\n```text\nm\n```\n\n```text\nmalloc\n```\n\n```text\ns\n```\n\n```text\nt\n```\n\n```text\nmalloc\n```\n\n```text\nmalloc\n```\n\n```text\na\n```\n\n```text\nprintf\n```\n\n```text\nmalloc\n```\n\n```text\nprintf\n```\n\n```text\nprintf\n```\n\n```text\nprintf\n```\n\n```text\nconst\n```\n\n```text\nconst char *s\n```\n\n```text\nconst\n```\n\n```text\ns\n```\n\n```text\nconst int *\n```\n\n```text\nconst\n```\n\n```text\nconst\n```\n\n```text\nconst\n```\n\n```text\nvoid foo(const int x)\n```\n\n```text\nx\n```\n\n========================================\n\nComments:\n- Did you look at the output from your compiler? Your question seems to be an exact duplicate of stackoverflow.com/questions/2021141/&hellip;.\n- @Carl Norum: While the question itself might be similar the answers here seem very very useful.\n- Sorry, but to me, the time spent wondering if this would work would be more than the time to write `#define Func1(a)(*(a)+1)` which you can be sure would be optimized.\n- @Mike I did thought about it, however macros are not visible to compiler and debugger, it's too hard to debug. And inline functions can be not inlined easily by compiler options. So I have decided to evade using macros to replace functions which are important semantically. (macros only for textual replacement) However I'm feeling your opinion is pretty attractive and considerable.\n- For a macro this size, seems to me it's a non-issue. OTOH sometimes I've written some pretty big multi-line macros (which save a lot of code and can't be made into functions). Then, if I need to debug, I instantiate the macro by hand and step through it, fix it, and put the macro back. I know that's dirty, but nobody said software is always clean.\n- Next time, write a correct function, put a unit test on it, and turn it into a macro after your profiling shows that the function call is too expensive ;-) Skipping the convert-debug-convert phase is pretty useful...\n- Thanks. I checked result with `otool`. Can I know the what your tool is? Print result looks differently a little.\n- I used `gcc -S -O3 t.c` and examined t.s, the generated assembly language. `-S` requests compilation to assembly language only—it does not assemble or link. This was from `gcc (GCC) 4.4.4 20100630 (Red Hat 4.4.4-10)` targeting x86.\n- Note that removing `inline` makes no difference in the code that gcc produces. `inline` is ignored and the compiler produces the best code it can according to constraints imposed by visibility.\n- Wow! Thanks for details. It's far more complicated than I thought... How do you think about if the function `printf` was accept single argument `int const * const a`?\n- @Eonil: It makes no difference. I extended the answer with a section discussing `const`-ness of pointers.\n- Thanks for answer. I stopped depending on optimization assumption :)\n- The gcc malloc attribute allow the compiler to assume that malloc's returned memory is not aliased by printf, another malloc, or anything else.\n- @EdwardBrey: That's why I wrote > and why I also added that compilers can use unportable tricks to do that >. Things like that or the type checking done at compile time on `printf` format strings are very useful, but non-standard.","metadata":{"transformedAt":"2026-08-18T18:32:17.823Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":48,"totalLines":402,"estimatedTokens":3228}}730{"id":"stack-3798017","source":"stackoverflow","questionId":3798017,"title":"May a compiler ever generate code to unload parts of the code segment during execution?","tags":["c++","compiler-construction","segment"],"text":"Title: May a compiler ever generate code to unload parts of the code segment during execution?\nTags: c++, compiler-construction, segment\nSource: Stack Overflow\n\nQuestion:\nApart from Dll concept that provides ability of loading/unloading methods or functions at run-time, I'm wondering if a compiler may ever say something like, ok as this particular part of the code takes considerable amount of space in code segment and is never gonna be used again after this point during program execution, it'd be good to generate some code to unload that part of code segment after reaching that particular point during program execution so that overall space took by code segment gets smaller. Is it something just fictional or may that happen?\n\n========================================\n\nTop Answer:\nSure. There's a technique called overlaying that loads different code into the same bit of address space at different times. Sometimes it was done manually, other times compilers helped. Sometimes the loading is done in software, sometimes in hardware (with address multiplexing, so that e.g. during boot time one bit of address space reads from a ROM chip, but after boot it switches to address RAM or a different ROM instead).\n\nOverlaying was much more common when computers had less memory, e.g. in the early days of DOS where you had 640K at best and often not even that. These days it still has applications for embedded systems where memory and/or address space are at a premium.\n\n========================================\n\nComments:\n- Discardable code segments also exists in the more common PE format used by EXE's and DLL's. The compiler certainly has a role here; it places functions in specific segments. But there's little point in it, as you noted: an OS can discard all read-only pages that are reloadable, and will discard pages that haven't been touched lately.","metadata":{"transformedAt":"2026-08-18T18:32:17.823Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":18,"estimatedTokens":467}}731{"id":"stack-65751457","source":"stackoverflow","questionId":65751457,"title":"How could one possibly bootstrap a C compiler(from source)?","tags":["c","compilation","compiler-construction","bcpl"],"text":"Title: How could one possibly bootstrap a C compiler(from source)?\nTags: c, compilation, compiler-construction, bcpl\nSource: Stack Overflow\n\nQuestion:\nI was looking into compiler bootstrapping, and I looked at how Golang implements bootstrapping from source, i.e., by building the last version of Golang implemented in C and using the generated executable to compile newer Go releases. This made me curious as to how the same could be done with C. Can you construct a C compiler on a computer with literally nothing present on it? If not, then how can I trust that the binary of the compiler I use doesn't automatically fill the binaries it compiles with spyware?\n\nRelated question, since the first C compiler was written in B and B was written in BCPL, what was BCPL written in?\n\n========================================\n\nTop Answer:\nAs Some programmer dude stated in a comment, since C is a portable programming language, you can use a compiler for a different platform to produce a cross-compiler that on that platform would produce executables for the target platform.\n\nYou then compile that same C compiler for the *target platform* on *that host platform* so that the result is an executable for the target platform.\n\nThen you copy that compiler binary onto the target machine and from thereon it is self-hosting.\n\nNaturally at some point in early history someone really **had to** write something in assembler or machine code **somewhere**. Today, it is no longer a necessity but a \"life choice\".\n\nAs for the \"how can I trust that the binary of the compiler I use doesn't automatically fill the binaries it compiles with spyware?\" problem has been solved - you can use two independent compilers to compile the cross-compiler from the *same source base* and the target and both of those cross-compilers should produce bitwise-identical results for the target executable. Then you would know that the result is either free of spyware, or that the two independent compilers you used in the beginning would infect the resulting executable with exact **same** spyware - which is exceedingly unlikely.\n\n========================================\n\nCode:\n```text\ngcc\n```\n\n```text\nld\n```\n\n```text\ngas\n```\n\n========================================\n\nComments:\n- You can bootstrap using cross-compilation on a different host system.\n- @Someprogrammerdude But then, I could say that the compiler you're using to cross-compile wasn't compiled by you but by some package maintainer. Even if you did construct your compiler yourself, i.e. by cross-compiling your host system compiler, it would have been compiled with a precompiled binary.\n- When implementing a compiler for a brand new language in the language itself, one must always use a second language for the very first compiler. If that language is raw binary machine code, assembly, C or something else doesn't matter. This is a very tedious and time-consuming process, but thankfully a process that only needs to be done *once!* Once there is a compiler that can compile itself, you no longer need the first primary bootstrap compiler written in another language. So, why do it if you don't need to? [to be continued...]\n- If you want to implement a C compiler using C, then use the tools available to you. And those tools include any possible existing C compiler! With that said, it *is* possible to do it from scratch, if there's any suitable language available, but don't expect it to be done quickly or easily. And your time is probably better spent elsewhere.\n- Yep, flip a lot of switches, press store to save that one instruction - repeat, then go.\n- When I wrote programs for the IBM 360, we used Hollerith cards, not panel switches! :)\n- @MarkBenningfield: How did you powered on that IBM 360? IIRC, you need to boot from tape (unless your disks had some OS). And booting from tape required to move some panel switches\n- @BasileStarynkevitch I have no doubt turning the thing on required flipping switches !! I'm not altogether sure they ever turned the dang thing off. I can remember running batch jobs at 2:00 in the morning\n- But then, wouldn't you be using 'something' instead of 'nothing' to build your compiler? If the compiler binary you're using is not compiled by you from the start, then how can you trust it? What if the binary to cross-compile is actually built with a specific purpose, i.e. to cleverly embed some form of malware onto the binaries it produces?","metadata":{"transformedAt":"2026-08-18T18:32:17.823Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":49,"estimatedTokens":1108}}732{"id":"stack-5322956","source":"stackoverflow","questionId":5322956,"title":"Few questions about C++ compilers : GCC, MSVC, Clang, Comeau etc","tags":["c++","visual-c++","gcc","compiler-construction","clang"],"text":"Title: Few questions about C++ compilers : GCC, MSVC, Clang, Comeau etc\nTags: c++, visual-c++, gcc, compiler-construction, clang\nSource: Stack Overflow\n\nQuestion:\nI've few questions about C++ compilers\n\nAre C++ compilers required to be one-pass compiler? Does the Standard talk about it anywhere?\n\nIn particular, is GCC one-pass compiler? If it is, then why does it generate the following error *twice* in this example (though the template argument is different in each error message)? \n\n error: declaration of **‘adder item’** shadows a parameter\n\n error: declaration of **‘adder item’** shadows a parameter\n\nA more general question\n\n- What are the advantages and disadvantages of one-pass compiler and multi-pass compiler?\n\nUseful links: \n\n- A List of C/C++ compilers (wikipedia)\n\n- An incomplete list of C++ compilers (Bjarne Stroustrup's site)\n\n========================================\n\nTop Answer:\nFrom what I know, 30 years ago it was important for a compiler to be one-pass, because reads and writes to disk (or magnetic tape) were very slow and there was not enough memory to hold whole code (thanks James Kanze). Also, a single-pass is a requirement for scripting/interactive languages.\n\nNowdays compilers are usually not one-pass, there are several intermediate representations (e.g Abstract Syntax Tree or Static Single Assignment Form) that the code is transformed into and then analised/optimised.\n\nSome elements in C++ cannot be solved without some intermediate steps, e.g. in a class you can reference members which are defined only later in the class body. Also, all templates need to be somehow remembered for further access during instantiation.\n\nWhat does *not* happen usually, is that the source code is not parsed several times --- there is no need for that. So you should not experience same syntactic error being reported several times.\n\n========================================\n\nComments:\n- This might interest you: en.wikipedia.org/wiki/Comeau_C/C%2B%2B. I think it uses a multipass approach to be able to support the export keyword for templates.\n- The reason single pass was important in the past was because main memory wasn't large enough to hold a complete internal representation of a function; if the compiler wasn't single pass, it would have to reread the data from disk. Or mag tape, which was still largely used then.\n- You keep asking what \"one pass\" or \"multi pass\" is, yet these are well defined terms. Links are provided in the original question and no additional information is needed. Obviously, multi-pass does not require you to parse the source code several times, but it requires to hold whole program in some kind of form and then restart the transformation from the beginning. One-pass forbits that. One pass does not allow, for example, to use variables/functions which are declared only later in the code. But in C++, in class scope, it permits that.\n- Are they? The Wikipedia contradicts itself in the first paragraph. Historically, one-pass meant one pass over the source, but in a context where the compiler couldn't save much more than the symbol table in memory. So a program compiled with a one-pass compiler couldn't refer to variables declared later in the code. C++ allows using variables which are first declared later in certain cases. But all of the compilers I know will only read the source code once.\n- As I said: \"multi-pass does not require you to parse the source code several times, but it requires to hold whole program in some kind of form and then restart the transformation from the beginning.\" On the contrary, single-pass does not require you to hold whole program in memory in whatever form.\n- Which doesn't get us very far? What do you mean by \"whole program\"? In particular, C++ requires the compiler to keep templates and class definitions available, but once a function has been parsed, the memory image for it can be thrown out. And it's possible to parse an ordinary function by just visiting the tree once (although typically, small parts of the tree may be visited more than once to resolve ambiguities).\n- Agreed... the terms \"one-pass\" or \"multi-pass\" are artifacts from an older era of computing, but old terms like these stick around on Wikipedia long after they lose their importance in textbooks.","metadata":{"transformedAt":"2026-08-18T18:32:17.823Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":46,"estimatedTokens":1072}}733{"id":"stack-10323451","source":"stackoverflow","questionId":10323451,"title":"Transform any program into a semantically equivalent one","tags":["c","performance","optimization","compiler-construction"],"text":"Title: Transform any program into a semantically equivalent one\nTags: c, performance, optimization, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI recently found this theorem here, (at the bottom):\n\n`Any program can be transformed into a semantically equivalent program of one procedure containing one switch statement inside a while loop.`\n\nThe Article went on to say :\n\n`A corollary to this theorem is that any program can be rewritten into a program consisting of a single recursive function containing only conditional statements`\n\nMy questions are, are both these theorems applicable today ? Does similarly transforming a program reap any benefits ? I mean to say, is such a code optimized ? (Although recursion calls are slower, I know)\n\nI read, from here, that switch-cases are *almost always* faster when optimized by the compiler. Does that make a difference. ?\n\nPS: I'm trying to get some idea about compiler optimizations from here\n\nAnd I've added the `c` tag as that's the only language I've seen optimized.\n\n========================================\n\nTop Answer:\nThis is basically what happens when a compiler translates a program into machine code. The machine code runs on a processor, which executes instructions one-by-one in a loop. The complex structure of the program has become part of the data in memory.\n\n========================================\n\nCode:\n```text\nAny program can be transformed into a semantically equivalent program of one procedure containing one switch statement inside a while loop.\n```\n\n```text\nA corollary to this theorem is that any program can be rewritten into a program consisting of a single recursive function containing only conditional statements\n```\n\n```text\nc\n```\n\n```text\nint opcode[] {\n   PUSH,\n   ADD\n   ....\n};\n\nwhile (true) {\n    switch (*opcode++) {\n    case PUSH:\n        *stack++ = <var>;\n        break;\n    case ADD:\n        stack[-1] += stack[0];\n        --stack;\n        break;\n     ....\n    }\n}\n```\n\n========================================\n\nComments:\n- The first article you cite is a little academic joke. Note that it says \"a **folk** theorem\" and not \"a theorem\". I would not try to learn anything about optimization from the first article.\n- @markgz From wikipedia : `folk theorem refers generally to any theorem that is believed and discussed, but has not been published`. You're right : it is bit of a troll. But the conclusions seem to be true nevertheless.\n- So, aside from obfuscation, such transforms serve no purpose at all. What if I use only `switch` and `while`, without using recursion at all ? Will there be a benefit ?\n- Generally, no. The only point of doing a semantics-preserving transformation is to a) optimize some aspect of the transformed program with respect to the original, [and the type of transformations being discussed generally pessimize time or space, or b) to enable some *other* transformation to take place that provides such an optimization. For an example of the latter, a complicated set of conditional reduced to a set of cases may all turn out to have the same case --> simpler switch --> possibly faster execution and definitely smaller code. ...\n- ... but there are other ways to determine such optimizations (e.g., common subexpression elimination, partial redundancy elimination) which tend to be useful in many cases, and these optimizations tend to already be built into compilers.\n- Is such optimization done by the developer ? It would seem to be *very* involved. Or are they done automagically by the compiler ?\n- Common subexpression/Partial redundancy are involved and hard in general. They and many other optimizations are built into many compilers. Go read a standard compiler text (Aho and Ullman, \"Compilers\" is a classic) to get a better understanding of the kind of transformations built into compilers.\n- @RaunakS What could be a \"practical\" application would be that you can transform any recursive computation into an iterative one. In some languages practical limitations (stacksize, no tail recursion, no recursion at all [early fortran]) may make that necessary and there exists a simple algorithm to do that in general. Other than that? Theoretical CS it is :)\n- @Voo: most practical optimizations require a series of transformations to achieve them. Each transformation by itself has to be \"semantics preserving\" or the application won't produce the same result. All that matters is the chain of transformations leads to a good result. Very modern compilers may try alternative chains to achieve various good results. Bleeding-edge software engineering research is pushing \"search through transformation space\" to find \"good solutions\" in terms of code structure.\n- @Ira Yeah after writing it I thought that that was a \"bit\" too general formulated. Clearly tail recursion is basically the application of the stated theorem in modern compilers, though I wouldn't put other transformations such as CSE or all different kinds of loop transformations there. The part about searching through transformation space instead of the usual stages of compiler optimization sounds interesting though - any paper for that? Don't really keep up with academical research (or even state of the art optimizations in gcc, hotspot), but once in a while that's fun.\n- @Voo Might it also be implementation environment dependent ? Such as for embedded systems : most of which are probably many years away from the bleeding edge. For instance, I remember Windows 3.1 being used as an embedded OS on aircraft till 2008. Any computation there would need to be *heavily* optimized.\n- @Voo: Check out \"future of search based software engineering\" cs.ucl.ac.uk/staff/K.Lakhotia/readingGroup/FoSE-SBSE.pdf. For a view of transformations for modifying code (not semantics preserving!), check out my paper on \"Design maintenance systems\" semdesigns.com/Company/Publications/DMS-CACM-1992-baxter.pdf\n- @RaunakS: Embedded systems may contain heavily optimized, because often they are produced in large quantity so cost per unit must be low, and that is achieved by using low performance parts pushed to the limit, and trading engineering optimization time to get some of the performance back.\n- Would this be how modern virtual machines are written ? (for instance, the JVM)\n- The vm I have illustrated is similar to how a language like Python would be interpreted. That is the vm is designed to interpret, or emulate, the bytecode of a different machine. These would be more optimised though (e.g. a table of function pointers may be faster than a switch statement). VMs that virtualise windows or similar use different technologies altogether involving CPU extensions and low-level hackery.\n- Fascinating. I never realized how much machinery is hidden behind the compiler veil (and we never seem to learns such matters in college).\n- \"We\"? Most computer science curricula have compiler classes, or at least they used to. If you don't know how your programming language \"works\", you will likely not understand how to build big, reasonably efficient systems.\n- @IraBaxter We don't, unfortunately. We do have one semester of automata ... but no compiler design. Our syllabi seem to be more focused on IT.\n- Welcome to a lifetime of learning. Really good engineers do a lot of learning on their own beyond their school education. your nose and enjoy it.\n- @IraBaxter Good Advice, thank you. I've already begun the Aho- Compilers/Principle/Techniques text you had mentioned. That's a start.","metadata":{"transformedAt":"2026-08-18T18:32:17.823Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":85,"estimatedTokens":1874}}734{"id":"stack-43789590","source":"stackoverflow","questionId":43789590,"title":"Does it make sense to implement a Garbage Collector in a Language with Garbage Collection","tags":["java","compiler-construction","garbage-collection","interpreter","vm-implementation"],"text":"Title: Does it make sense to implement a Garbage Collector in a Language with Garbage Collection\nTags: java, compiler-construction, garbage-collection, interpreter, vm-implementation\nSource: Stack Overflow\n\nQuestion:\nI want to implement a garbage collector (GC) in Java or Haskell, but does this make sense? \n\nWill I be able to control when my own implementation of the GC kicks in and not the GC of the implementation language?\n\nI guess that there can be three kinds of answers for this question:\n\n- Set a flag when running the virtual machine (VM) that disables the GC of the implementation language.\n\n- Use a special construct that lets me manage my own memory, like using continuation-passing style (CPS) to manage my own evaluation strategy.\n\n- No, use a language with no GC.\n\nLooking at these:\n\n- I prefer the second option since I will be able to use that construct in all languages.\n\n- I don't like the first option because I'll have to manage the memory of my interpreter as well.\n\n- (And I don't prefer the third option neither, but I can't control that)\n\nThis is not a duplicate of I build a interpreter on a language with a garbage collector. I need a garbage collector for the interpreter? as I do **not** want to bootstrap my interpreter with the underlying GC.\n\n========================================\n\nTop Answer:\nAs so often in IT, the answer is: *it depends*.\n\nWhen *your* interpreter keeps control of all \"objects\" within *your* context; then of course, your interpreter will probably keep a list of all \"its\" objects. So even when those objects would somehow be visible to the JVM, they would all be reachable, thus alive; so they could not be subject to JVM gc.\n\nBut when you somehow \"embed\" the objects that your interpreter deals with into the surrounding java context - then the JVM gc could be responsible for them.\n\nComing from there, the answer would go into the direction: yes, this *could* be possible. But beyond that; it really depends on your *goal* and driving requirements. Are you implementing this for pure education purposes; or are you interested in creating some sort of \"real\" product that is of \"real\" value to other people? \n\nIf the later is the case, then you probably want to embed your interpreter very tightly with the JVM - in order to benefit from the massive investment that turned the JVM into the great platform it is today. You see, almost 20 years of research went into current JVM JIT compiler and GC technology. Do you want to take advantage of that in order to \"do your own thing\" ...\n\nSo, as outlined: it very much depends on your \"real\" goals.\n\nFinally: you might find this SE-Radio podcast on JRuby and the JVM platform helpful in the context of your question. The JRuby folks were simply not happy with the performance of the standard Ruby engine; and they choose the JVM as platform to build a *better* Ruby engine ...\n\n========================================\n\nComments:\n- Probably a bit broad; but still a nice question for a newbie; with some interesting food for thought. Have my upvote for that ... and just in case you want to practice your newly gained upvote privilege; I will be glad to assist practicing ;-)\n- I totally agree to @GhostCat 's comment. Nice question ;)\n- Where I have to admit: I also tend to agree on Lew's answer - you might try to work on your mental model of the world. It isn't really helpful to already *restrict* potential answers while outlining your question. *Avoid* shaping the ideas of the people you ask for input. You want their thoughts, not *your* view on their ideas!\n- Aren't I suppose to present what thoughts I have had on beforehand? I see that it might limit the potential answers but I hoped that it would also clarify the question.\n- It is probably more about wording. You could say for example: \"I see the following options... But are there others?\" or something alike.\n- I feel from your question that you don't understand what a GC is. From the perspective of the language, GC is a system that keeps track of objects in your languages runtime. A runtime in simple words is a large hash which has a pool of primordial objects and objects created during parsing. GC just removes it from the runtime. So if your question is would creating a GC for your your interpreted language runtime help ? The answer is yes. Anything not stored in your runtime essentially means it can be GC'ed by the underlying Java Runtime. So it does make sense.\n- Thank you very much sir!\n- The second and third paragraph answers my question, thank you. The project is for educational purposes of a non-optimized GC. So, no need for embedding the objects of the interpreted program into the main Java context. But it's an interesting thought.\n- Strange. I saw this other comment given to you today about often being so \"negative\" and not constructive ... and this is a nice example of that. Still you are correct, so have my vote for that.\n- But I don't want the underlying GC to handle the interpreted program's memory. I want to do that myself. So, your summary is that no, I cannot make my own GC handle the memory. But according to GhostCat, I just have to keep all the references alive, and my own GC's job is to kill the references when they're done.\n- Yes. For example, you implement GC for a specific language named S. You write code in S, all objects live in memory managed by your own GC. Now, your own GC manipulate a reference graph for all objects. GC of JVM will manage your own GC, not objects managed by your own GC. If your own GC will not trigger GC of JVM, that's ok. But why? If your own GC will trigger GC of JVM run, it will stop your own GC, indirectly decrease performance of your own GC.","metadata":{"transformedAt":"2026-08-18T18:32:17.824Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":58,"estimatedTokens":1425}}735{"id":"stack-827489","source":"stackoverflow","questionId":827489,"title":"Compiler Optimizations Questions","tags":["optimization","compiler-construction","compiler-development"],"text":"Title: Compiler Optimizations Questions\nTags: optimization, compiler-construction, compiler-development\nSource: Stack Overflow\n\nQuestion:\n- What are some of the ways a compiler eliminates repeated subexpressions recomputations? How do you keep track of the sub-expressions? And How do you identify the repeated ones?\n\n- Besides the usage of bitwise operators, what are some of the strength reduction techniques common compilers use?\n\n========================================\n\nTop Answer:\nFor 1, The name of the optimization you're looking for is common subexpression elimination (CSE). Depending on your representation, this can be fairly easy. Usually, a compiler will have some intermediate representation of a program where operations are broken down as much as possible and linearized. So for example, the expression `c = a * b + a * b` might be broken down as:\n\n```\nv1 = a * b\nv2 = a * b\nc = v1 + v2\n```\n\nSo you could do CSE at a very low level by looking for operations with the same operator and operands. When you encounter a duplicate (v2 in this case), you replace all instances of it with the original. So we could simplify the code above to be:\n\n```\nv1 = a * b\nc = v1 + v1\n```\n\nThis generally assumes that you only assign each variable once (single static assignment form), but you can implement something like this without that restriction. This gets more complicated when you try and perform this optimization across branches. As Zifre mentions, look into Partial Redundancy Elimination.\n\nEither way, you get some basic improvement, and all you need to keep track of are basic expressions. You may want to take this a step further and look for arithmetic identities. For instance, `a * b` is the same as `b * a`. Also, `x * (y + z) = x * y + x * z`. This makes your optimization more complicated, and it's not clear that it would give you that much performance improvement. Anecdotally, most of the benefit from a CSE optimization comes from address computations like array accesses, and you won't need complicated identities like the ones above.\n\nFor 2, what strength reductions are useful really depends on the architecture you compile for. Usually this just involves transforming multiplications and divisions into shifts, additions, and subtractions.\n\n========================================\n\nCode:\n```text\nv1 = a * b\nv2 = a * b\nc = v1 + v2\n```\n\n```text\nv1 = a * b\nc = v1 + v1\n```\n\n```text\nc = a * b + a * b\n```\n\n```text\na * b\n```\n\n```text\nb * a\n```\n\n```text\nx * (y + z) = x * y + x * z\n```\n\n========================================\n\nComments:\n- You should split this question into two parts, and fix the title. (I dont have the power yet).\n- WOW Thank you. Those links were really helpful.\n- Definitely agree on LLVM. We use it in my research group. It's great.\n- Don't recommend SSAPRE. It requires HSSA, which your compiler almost certainly won't provide.\n- what if the repeated ones are not in the same expression? For example x= a * b and somewhere down y = a* b. Is there someway I can detect the repeated a*b?\n- It is a repeat though. In that case, you replace all the future uses of y with x.\n- do you know why GCC has moved away from HSSA, and why LLVM decided not to use it? Current versions of GCC (7.0) mark variables with `VDEF` and `VUSE`, this looks similar to HSSA to me. Do you know if they are related?\n- Sorry, it's been a long time and I wasn't involved. I literally don't remember anything about this :(\n- Know that feel bro.","metadata":{"transformedAt":"2026-08-18T18:32:17.824Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":75,"estimatedTokens":867}}736{"id":"stack-3222328","source":"stackoverflow","questionId":3222328,"title":"What ways exists to generate MSIL from unmanaged code","tags":[".net","delphi","compiler-construction","backend"],"text":"Title: What ways exists to generate MSIL from unmanaged code\nTags: .net, delphi, compiler-construction, backend\nSource: Stack Overflow\n\nQuestion:\nI am trying to create a backend of my language for .NET platform.\nThe front-end and interpreter written in Delphi.\nUnmanaged API just allows type definitions but no emiting of MSIL.\n\nWhat ways exist to generate MSIL from unmanaged code?\nWithout using Reflection.Emit and using ILasm to attain this?\nThank you.\n\n========================================\n\nTop Answer:\nFrom *unmanaged* code? TBH, the best suggestion I have is either \"use P/Invoke\", or \"figure out what it is meant to do, and re-implement it\".\n\nEven if you *can* find something to port the unmanaged code, and even if it works - it isn't exactly going to leverage the framework. And it isn't exactly 1:1 between unmanaged and managed.\n\n========================================\n\nCode:\n```text\nIMetaDataEmit\n```\n\n```text\nIMetaDataEmit::SetRVA\n```\n\n```text\nIMetaDataEmit::SaveToMemory\n```\n\n```text\nilasm\n```\n\n========================================\n\nComments:\n- I need an API what allow me to emit MSIL directly(read from native Delphi) with out using unmanaged-managed marshaling(Pinvoke,reverse pinvoke, IJW, COM). Do you suggest me to generate PE Files assembles myself?\n- I translated(cor.h corhdr.h) to pas files manually 2 days ago. So Unmanaged API is available to native side front end of my compiler. But API does not support for emiting MSIL for method implementation.\n- ICeeGen is obsolete. I don't know of a replacement.\n- Rob, I have read the ECMA335 as well as \"Expert .NET 2.0\" by Serge Lidin 3 years ago. (So ilAsm is just my an other friend). Rob, can you ask Barry Kelly or may be Remobjects guys how they solve this problem. Or may be they all use mixed mode C++.\n- Prism compiler is built with Prism and is a 100% .NET application.","metadata":{"transformedAt":"2026-08-18T18:32:17.824Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":47,"estimatedTokens":465}}737{"id":"stack-3231316","source":"stackoverflow","questionId":3231316,"title":"What are function epilogues and prologues?","tags":["assembly","compiler-construction","terminology","cpu-architecture","calling-convention"],"text":"Title: What are function epilogues and prologues?\nTags: assembly, compiler-construction, terminology, cpu-architecture, calling-convention\nSource: Stack Overflow\n\nQuestion:\nWhile reading some calling convention in some CPU architecture I read something like \n\n\"epilogue and prologue\", when a function is called from another function.\n\nWhat is a function prologue or epilogue?\n\n========================================\n\nTop Answer:\nWikipedia FTW: https://en.wikipedia.org/wiki/Function_prologue\n\nThis seemed to explain it fairly well, in my opinion. If there is anything that is unclear, let me know and I can try and clear things up.","metadata":{"transformedAt":"2026-08-18T18:32:17.824Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":17,"estimatedTokens":159}}738{"id":"stack-3165385","source":"stackoverflow","questionId":3165385,"title":"Make macros and functions integrate more seamlessly","tags":["compiler-construction","functional-programming","macros","clojure","lisp"],"text":"Title: Make macros and functions integrate more seamlessly\nTags: compiler-construction, functional-programming, macros, clojure, lisp\nSource: Stack Overflow\n\nQuestion:\nOK, I understand pretty well how to use both function and macros.\n\nWhat I'm curious about is why the compiler can't be a bit more clever when integrating the two, e.g. consider the Clojure code:\n\n```\n(defmacro wonky-add [a b] `(+ ~a (* 2 ~b)))\n\n(defn wonky-increment [a] (apply wonky-add a 1))\n=> Error: can't take value of a macro\n```\n\nYes, I know I can make this work by taking out the \"apply\" - but why is it that the compiler can't figure out how to do this itself?\n\nWould it be possible in Clojure / other LISPs to create a version of apply or other higher order functions that work equally well with both functions and macros as parameters?\n\n========================================\n\nTop Answer:\nWhat you're describing sounds like a first class macro (ie: a macro that you can manipulate like a function). Some lisps have these (Arc for instance), but `apply`, on account of not being a macro itself, would probably still not work.\n\n========================================\n\nCode:\n```text\n(defmacro wonky-add [a b] `(+ ~a (* 2 ~b)))\n\n(defn wonky-increment [a] (apply wonky-add a 1))\n=> Error: can't take value of a macro\n```\n\n```text\napply\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.824Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":38,"estimatedTokens":330}}739{"id":"stack-9558200","source":"stackoverflow","questionId":9558200,"title":"What is the best way to perform branching using Intel SSE?","tags":["assembly","compiler-construction","intel","sse"],"text":"Title: What is the best way to perform branching using Intel SSE?\nTags: assembly, compiler-construction, intel, sse\nSource: Stack Overflow\n\nQuestion:\nI'm writing a compiler and I have to output code for branching conditions on float values. For example, to compile this kind of code: \n\n```\nif(a When a and b are float variables. I just need to jump to 2 if the condition is not true, else fall to 1. I'm considering here optimization at the compiler level considering what's in 1 and 2. \n\nI need something that works with all the comparison operators >, >=, A way I found to make the comparison is to use CMPLTSD (and other equivalent instructions for other relational operators). But with that, I have to use a SSE register especially for the result and then I have to move its value on a general purpose register (eax for example) and finally compare the value with 0. \n\nI also saw that the UCOMISD instruction should set the flags correctly, but apparently it doesn't work the way I thought. \n\nSo, what's the best way to handle code like that? Is there better instructions than the first solution I have? \n\nBy best, I mean, the general solution to this problem. If possible, I would like to have code behave the same way as when doing comparisons on integers (cmp a, b; jge label). Of course, I would prefer the fastest instructions to achieve that.\n\n========================================\n\nTop Answer:\n**Important**: @harold's answer is almost exactly right but has a subtle wrong aspect which may drive you crazy in a very important edge case later on -- the NaN treatment is backwards from most languages (like c++).\n\nAs @harold says correctly, the unordered compare result is stored in the parity flag.\n\nHowever, unordered compare **is true when any operand is NaN** as detailed in this stack overflow post. That means `NaN` will be less than, equal to *and greater than* absolutely every number *including* `NaN`.\n\nSo if you want your language to match c++'s behavior of where any comparison with NaN returns false, you want:\n\nFor `For `Confirmed in the following gcc disassembly, where I `return a >= b`:\n\n```\n144e: 66 0f 2e c8 ucomisd %xmm0,%xmm1\n1452: 0f 93 c0 setae %al\n```\n\nHere it uses `setae` which is the register-modifying equivalent to `jae`. It then immediately returns without inspecting the parity flag.\n\nFor why its `ja` and not `jg`, @harold's answer is still a clear and correct explanation.\n\nAnd of course, you don't have to use ordered compare, you can use unordered compare as shown in the previous answer if you want absolutely every number to be less than, greater than, *and* equal to `NaN` in your program/language (where even `NaN < NaN` is true!). And of course, as you can see, it may be a little bit slower since it requires additional checks.\n\n========================================\n\nCode:\n```text\nif(a <= b){\n    //1. DO something\n} else {\n    //2. Do something else\n}\n```\n\n```text\nucomisd a,b\n  ja else     ; greater\n  jp else     ; unordered\n  ; code for //1 goes here\n  jmp end\nelse:\n  ; code for //2 goes here\nend:\n```\n\n```text\njae else   ; greater or equal\njp else    ; unordered\n```\n\n```text\nucomisd\n```\n\n```text\n<=\n```\n\n```text\n<\n```\n\n```text\nucomisd\n```\n\n```text\nucomisd xmm0, xmm1\njbe else_label\n```\n\n```text\nucomisd xmm0, xmm1\njb else_label\n```\n\n```text\n144e:       66 0f 2e c8             ucomisd %xmm0,%xmm1\n1452:       0f 93 c0                setae  %al\n```\n\n```text\nNaN\n```\n\n```text\nNaN\n```\n\n```text\n<=\n```\n\n```text\n<\n```\n\n```text\nreturn a >= b\n```\n\n```text\nsetae\n```\n\n```text\njae\n```\n\n```text\nja\n```\n\n```text\njg\n```\n\n```text\nNaN\n```\n\n```text\nNaN < NaN\n```\n\n========================================\n\nComments:\n- The best way to do it *depends on what you are doing*. As in, what is inside the `&#47;&#47;DO something` block? \"The best way\" often depends on looking at the whole picture, not trying to translate your code line by line.\n- I added details to the post to answer your two questions.\n- If you actually want to branch, UCOMISD (which is actually SSE2) does appear to be the answer, what's the problem with it? The Unordered result?\n- The problem with UCOMISD is that I don't know how to jump according to the result of the comparison. I tried jumping with jle, but I didn't get the expected result. Do I have to use special conditional jump instructions ?\n- This is indeed strange... But I think I will got it with the documentation. Thanks a lot.\n- @BaptisteWicht: See Why do x86 FP compares set CF like unsigned integers, instead of using signed conditions? for the history: it sets FLAGS the same as x87 `fcom` / `fstsw ax` / `sahf`.","metadata":{"transformedAt":"2026-08-18T18:32:17.824Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":21,"totalLines":153,"estimatedTokens":1151}}740{"id":"stack-76285877","source":"stackoverflow","questionId":76285877,"title":"Why is @JVMStatic annotation necessary for Kotlin main function?","tags":["kotlin","jvm","compiler-construction"],"text":"Title: Why is @JVMStatic annotation necessary for Kotlin main function?\nTags: kotlin, jvm, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nRecently started learning Kotlin. I noticed an interesting behaviour when declaring the main function in Kotlin. It uses a @JvmStatic annotation even when in an object class. I looked around and did not find a relevant post.\n\n```\nobject Main {\n @JvmStatic\n fun main(args: Array) {\n println(\"Hello World\")\n }\n}\n```\n\nWhy does the main function in Kotlin require a @JvmStatic annotation?\n\nComing from a Java and Scala background, it seems redundant and unnecessary, so I am curious what the background context and decision making was that made Kotlin adopt this convention.\n\n========================================\n\nTop Answer:\nKotlin has no direct equivalent of Java static methods. The closest it has are top level functions (ones not in any class). For these to be callable in Java, they are automatically compiled as a Java static method in a generated class named after the file with “Kt” appended.\n\nKotlin `object`s have no direct equivalent in Java. An `object` is an actual instance of a class. Its functions are just regular, non-static methods. A Kotlin object can be accessed via a generated `INSTANCE` static field in Java. This simply returns the class instance of the Kotlin object. Through it, you can access its methods.\n\n`@JvmStatic` tells the compiler to compile the function as static. The reason this isn’t automatically done is that it puts a bunch of restrictions on the function that normally wouldn’t exist in Kotlin so it’s therefore necessary to require an opt-in. For instance, it cannot reference or call anything specific to the class instance, such as functions/properties defined in its superclass, and it cannot override anything.\n\n========================================\n\nCode:\n```text\nobject Main {\n    @JvmStatic\n    fun main(args: Array<String>) {\n        println(\"Hello World\")\n    }\n}\n```\n\n```text\nfun main(args: Array<String>) {\n    println(\"Hello World\")\n}\n```\n\n```text\n// the Kotlin main gets converted to something like this...\npublic static void main$1() {\n    // your kotlin code here...\n}\n\n// A proper Java main generated by Kotlin\npublic static void main(String[] args) {\n    main$1();\n}\n```\n\n```text\nobject MyObject {\n    fun main(args: Array<String>) {\n\n    }\n}\n```\n\n```text\npublic final class MyObject {\n    private MyObject() {}\n\n    public static final MyObject INSTANCE = new MyObject();\n\n    public void main(String[] args) { }\n}\n```\n\n```text\n@JvmStatic\n```\n\n```text\nmain\n```\n\n```text\n@JvmStatic\n```\n\n```text\nXXXKt\n```\n\n```text\nXXX\n```\n\n```text\nmain\n```\n\n```text\nmain\n```\n\n```text\nString[] args\n```\n\n```text\n@JvmStatic\n```\n\n```text\nmain\n```\n\n```text\nobject\n```\n\n```text\nobject\n```\n\n```text\nobject\n```\n\n```text\nMyObject.main(...)\n```\n\n```text\nMyObject.INSTANCE.main(...)\n```\n\n```text\nmain\n```\n\n```text\nobject\n```\n\n```text\nobject\n```\n\n```text\nINSTANCE\n```\n\n```text\n@JvmStatic\n```\n\n========================================\n\nComments:\n- That's interesting. Any recommended resources on understanding Kotlin to Java/JVM compilations?\n- @SomebodyNEW Some of that is described here, but not how `main` functions translate though. In general, you can just look at the bytecode output. You can do that in IntelliJ in View -> View Bytecode.","metadata":{"transformedAt":"2026-08-18T18:32:17.824Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":25,"totalLines":162,"estimatedTokens":833}}741{"id":"stack-50775127","source":"stackoverflow","questionId":50775127,"title":"From compiler perspective, how is reference for array dealt with, and, why passing by value(not decay) is not allowed?","tags":["c++","arrays","pointers","assembly","compiler-construction"],"text":"Title: From compiler perspective, how is reference for array dealt with, and, why passing by value(not decay) is not allowed?\nTags: c++, arrays, pointers, assembly, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nAs we know, in C++, we can pass an array's reference as an argument like `f(int (&[N])`. Yes, it is syntax guaranteed by the iso standard, but I am curious about how the compiler works here. I found this thread, but unfortunately, this doesn't answer my question -- How is this syntax implemented by the compiler?\n\nI then wrote a demo and hoped to see something from the assembly language: \n\n```\nvoid foo_p(int*arr) {}\nvoid foo_r(int(&arr)[3]) {}\ntemplate\nvoid foo_t(int(&arr)[length]) {}\nint main(int argc, char** argv)\n{\n int arr[] = {1, 2, 3};\n foo_p(arr);\n foo_r(arr);\n foo_t(arr);\n return 0;\n}\n```\n\nOriginally, I **guess** it will still decay to the pointer, but will pass length implicitly via a register, then turn back into an array in the function body. But the assembly code tells me this is not true\n\n```\nvoid foo_t(int (&) [3]):\n push rbp #4.31\n mov rbp, rsp #4.31\n sub rsp, 16 #4.31\n mov QWORD PTR [-16+rbp], rdi #4.31\n leave #4.32\n ret #4.32\n\nfoo_p(int*):\n push rbp #1.21\n mov rbp, rsp #1.21\n sub rsp, 16 #1.21\n mov QWORD PTR [-16+rbp], rdi #1.21\n leave #1.22\n ret #1.22\n\nfoo_r(int (&) [3]):\n push rbp #2.26\n mov rbp, rsp #2.26\n sub rsp, 16 #2.26\n mov QWORD PTR [-16+rbp], rdi #2.26\n leave #2.27\n ret #2.27\n\nmain:\n push rbp #6.1\n mov rbp, rsp #6.1\n sub rsp, 32 #6.1\n mov DWORD PTR [-16+rbp], edi #6.1\n mov QWORD PTR [-8+rbp], rsi #6.1\n lea rax, QWORD PTR [-32+rbp] #7.15\n mov DWORD PTR [rax], 1 #7.15\n lea rax, QWORD PTR [-32+rbp] #7.15\n add rax, 4 #7.15\n mov DWORD PTR [rax], 2 #7.15\n lea rax, QWORD PTR [-32+rbp] #7.15\n add rax, 8 #7.15\n mov DWORD PTR [rax], 3 #7.15\n lea rax, QWORD PTR [-32+rbp] #8.5\n mov rdi, rax #8.5\n call foo_p(int*) #8.5\n lea rax, QWORD PTR [-32+rbp] #9.5\n mov rdi, rax #9.5\n call foo_r(int (&) [3]) #9.5\n lea rax, QWORD PTR [-32+rbp] #10.5\n mov rdi, rax #10.5\n call void foo_t(int (&) [3]) #10.5\n mov eax, 0 #11.11\n leave #11.11\n ret #11.11\n```\n\n**`live demo`**\n\nI admit that I am not familiar with the assembly language, but clearly, the three function's assembly codes are the same! So, something must happen before the assembler codes. Anyway, unlike the array, the pointer knows nothing about the length, right?\n\n### Questions:\n\n- how does the compiler work here?\n\n- Now that the standard allows to pass an array by reference, does that mean it is trivial to implement? If so, why isn't passing by value allowed?\n\nFor Q2, my guess is for the complexity of the former C++ and C codes. After all, `int[]` being equal to `int*` in function parameters has been a tradition. Maybe one hundred years later, it will be deprecated?\n\n========================================\n\nTop Answer:\nIt's all about backward compatibility. C++ got arrays from C, which got it from the B language. And in B an array variable actually *was* a pointer. Dennis Ritchie has written about this.\n\nArray parameters decaying to pointers helped Ken Thompson reuse his old B sources when moving UNIX to C. :-)\n\nWhen later it was seen as perhaps not the best decision, it was instead deemed too late to change the C language. So the array decay was kept, but structs - added later - is passed by value.\n\nThe introduction of structs also offered kind of a workaround for the case where you really *wanted* to pass an array by value:\n\nWhy declare a struct that only contains an array in C?\n\n========================================\n\nCode:\n```text\nvoid foo_p(int*arr) {}\nvoid foo_r(int(&arr)[3]) {}\ntemplate<int length>\nvoid foo_t(int(&arr)[length]) {}\nint main(int argc, char** argv)\n{\n    int arr[] = {1, 2, 3};\n    foo_p(arr);\n    foo_r(arr);\n    foo_t(arr);\n   return 0;\n}\n```\n\n```text\nvoid foo_t<3>(int (&) [3]):\n  push rbp #4.31\n  mov rbp, rsp #4.31\n  sub rsp, 16 #4.31\n  mov QWORD PTR [-16+rbp], rdi #4.31\n  leave #4.32\n  ret #4.32\n\nfoo_p(int*):\n  push rbp #1.21\n  mov rbp, rsp #1.21\n  sub rsp, 16 #1.21\n  mov QWORD PTR [-16+rbp], rdi #1.21\n  leave #1.22\n  ret #1.22\n\nfoo_r(int (&) [3]):\n  push rbp #2.26\n  mov rbp, rsp #2.26\n  sub rsp, 16 #2.26\n  mov QWORD PTR [-16+rbp], rdi #2.26\n  leave #2.27\n  ret #2.27\n\nmain:\n  push rbp #6.1\n  mov rbp, rsp #6.1\n  sub rsp, 32 #6.1\n  mov DWORD PTR [-16+rbp], edi #6.1\n  mov QWORD PTR [-8+rbp], rsi #6.1\n  lea rax, QWORD PTR [-32+rbp] #7.15\n  mov DWORD PTR [rax], 1 #7.15\n  lea rax, QWORD PTR [-32+rbp] #7.15\n  add rax, 4 #7.15\n  mov DWORD PTR [rax], 2 #7.15\n  lea rax, QWORD PTR [-32+rbp] #7.15\n  add rax, 8 #7.15\n  mov DWORD PTR [rax], 3 #7.15\n  lea rax, QWORD PTR [-32+rbp] #8.5\n  mov rdi, rax #8.5\n  call foo_p(int*) #8.5\n  lea rax, QWORD PTR [-32+rbp] #9.5\n  mov rdi, rax #9.5\n  call foo_r(int (&) [3]) #9.5\n  lea rax, QWORD PTR [-32+rbp] #10.5\n  mov rdi, rax #10.5\n  call void foo_t<3>(int (&) [3]) #10.5\n  mov eax, 0 #11.11\n  leave #11.11\n  ret #11.11\n```\n\n```text\nf(int (&[N])\n```\n\n```text\nlive demo\n```\n\n```text\nint[]\n```\n\n```text\nint*\n```\n\n```text\n__attribute__((noinline, noclone)) \nvoid foo_p(int*arr) {(void)arr;}\nvoid foo_r(int(&arr)[3]) {arr[4] = 41;}\n\ntemplate<int length>\nvoid foo_t(int(&arr)[length]) {arr[4] = 42;}\n\nvoid usearg(int*); // stop main from optimizing away arr[] if foo_... inline\n\nint main()\n{\n    int arr[] = {1, 2, 3};\n    foo_p(arr);\n    foo_r(arr);\n    foo_t(arr);\n    usearg(arr);\n   return 0;\n}\n```\n\n```text\n<source>: In function 'int main()':\n<source>:14:10: warning: array subscript is above array bounds [-Warray-bounds]\n     foo_t(arr);\n     ~~~~~^~~~~\n```\n\n```text\nvoid foo_t<3>(int (&) [3]) [clone .isra.0]:\n    mov     DWORD PTR [rdi], 42       # *ISRA.3_4(D),\n    ret\nfoo_p(int*):\n    rep ret\nfoo_r(int (&) [3]):\n    mov     DWORD PTR [rdi+16], 41    # *arr_2(D),\n    ret\n\nmain:\n    sub     rsp, 24             # reserve space for the array and align the stack for calls\n    movabs  rax, 8589934593     # this is 0x200000001: the first 2 elems\n    lea     rdi, [rsp+4]\n    mov     QWORD PTR [rsp+4], rax    # MEM[(int *)&arr],  first 2 elements\n    mov     DWORD PTR [rsp+12], 3     # MEM[(int *)&arr + 8B],  3rd element as an imm32\n    call    foo_r(int (&) [3])\n    lea     rdi, [rsp+20]\n    call    void foo_t<3>(int (&) [3]) [clone .isra.0]    #\n    lea     rdi, [rsp+4]      # tmp97,\n    call    usearg(int*)     #\n    xor     eax, eax  #\n    add     rsp, 24   #,\n    ret\n```\n\n```text\nint foo(int arr[static 3])\n```\n\n```text\nstatic\n```\n\n```text\ncmov\n```\n\n```text\nif\n```\n\n```text\nconst int *arr\n```\n\n```text\ngcc -O3 -fno-inline-functions -fno-inline-functions-called-once -fno-inline-small-functions\n```\n\n```text\n-O0\n```\n\n```text\ninline\n```\n\n```text\n-fno-inline-small-functions\n```\n\n```text\n__attribute__((noinline))\n```\n\n```text\nstatic\n```\n\n```text\narr[]\n```\n\n```text\narr[4]\n```\n\n```text\n-Wall -Wextra\n```\n\n```text\nfoo_r\n```\n\n```text\nfoo_p()\n```\n\n```text\nnoinline\n```\n\n```text\nnoclone\n```\n\n```text\n*arr=0;\n```\n\n```text\nmain\n```\n\n```text\nrdi\n```\n\n```text\nclone .isra.0\n```\n\n```text\narr[4]\n```\n\n```text\nlea rdi, [rsp+20]\n```\n\n```text\n[rdi]\n```\n\n```text\n__attribute__((noclone))\n```\n\n```text\ndisp8\n```\n\n```text\nvoid foo_clone(int *p) { *p = 42; }\n```\n\n```text\nmain\n```\n\n```text\nmain\n```\n\n```text\nmov rdi, rsp\n```\n\n```text\nlea rdi, [rsp+4]\n```\n\n```text\n&arr[-1]\n```\n\n```text\nmov dword ptr [rdi+20], 42\n```\n\n```text\nmain\n```\n\n```text\nrsp\n```\n\n```text\nboost::type_index<T>()::pretty_name()\n```\n\n```text\nreturn 0;\n```\n\n```text\nmain()\n```\n\n```text\nmain()\n```\n\n========================================\n\nComments:\n- `foo_r` and `foo_t` are really the same if `length == 3`. And no the length doesn't need to be passed, as it's handled internally by the compiler at compilation-time.\n- Compile with `-O3 -fno-inline-functions`, or `-O3` with `__attribute__((noinline))` to remove the noise of `-O0`. Hmm, I should probably update my answer on How to remove \"noise\" from GCC/clang assembly output? with those suggestions.\n- @PeterCordes -O3 will eliminate these calls completely.\n- @陳力: That's why you use ` __attribute__((noinline))` instead of just `-O3`. Without that, it turns out you also need `-fno-inline-small-functions`. See updates in my answer.\n- Awesome updates and help me learn much about assembly language! thanks ;)\n- So, can we say \"all assembly codes serve for run-time\"?\n- @陳力: I'm not sure what you're asking, but yes of course the asm implements only the run-time behaviour. Array size is only checked at compile time, so the compiler can warn about out-of-bounds accesses if it can statically prove it happens.\n- I still have one question: *gcc made a definition of the function that takes a pointer to arr[4] rather than to the base element* How does the conclusion make?\n- @陳力: The addressing mode it uses is `[rdi]`, not `[rdi + 4*4]`, so like I said it implements `foo_clone(int*p){ p[0] = 42; }`, not `p[4] = 42;`. It requires the caller to pass a pointer to the element it will actually store to, and we can see that's what the caller is doing with `lea`.\n- Thanks for your answer. I know as-if ruler(just mentioned hours ago) and have tested it on several different platforms and assemblers. What confuses me is what compiler does here before generating assembly code(like where the length is stored)\n- Each compiler does it differently. Some of them have ways for you to view their intermediate steps and state, e.g. GCC has GIMPLE and Clang has LLVM IR.\n- @陳力 the internal intermediate representation is not needed at runtime at all, so if you are really curious about how the compiler does it internally, there's not much point to check the final machine code, you will have to debug the compiler during compilation, the result is pretty much completely stripped down from the intermediate stages information. (BTW that's common mistake of new assembler programmers, to verify correctness of their new code by checking the results, which often hides major bugs from their sight :) ... while it's completely different thing, I still decided to mention it;)\n- Ah, thank for offering the history. So the original sin is the B language ;P","metadata":{"transformedAt":"2026-08-18T18:32:17.824Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":49,"totalLines":421,"estimatedTokens":2520}}742{"id":"stack-2828623","source":"stackoverflow","questionId":2828623,"title":"How a JIT compiler helps performance of applications?","tags":["android","jit","compiler-construction"],"text":"Title: How a JIT compiler helps performance of applications?\nTags: android, jit, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI just read that **Android has a 450% performance improvement because it added a JIT compiler**, I know what JIT is, but I don't really understand why is it faster than normal compiled code? or what's the difference with the older approach from the Android platform (the Java like run compiled bytecode).\n\nThanks!\n\nEDIT: This is hugely interesting, thanks!, I wish I could pick every answer as correct :)\n\n========================================\n\nTop Answer:\nThe VM needs to turn compiled byte code into machine instructions to run. Previously this was done using an interpreter which is fine for code that is only invoked once but is suboptimal for functions that are called repeatedly.\n\nThe Java VM saw similar speedups when asa JIT-versions of the VM replaced the initial interpreter versions.\n\n========================================\n\nComments:\n- Where did you read that 450% increase in performance because of JIT?\n- it's all over the web: providingnews.com/android-2-2-450-faster.html\n- JIT compiler is not faster than compiled code. It is only faster than interpreted code. Because JIT compiler can not spend as much time for optimizing as static compiler, the code is not as fast as normal compiled code. In addition there is the time needed for compiling each time the code is run.\n- To clarify, Dalvik, the \"Java-like\" VM on Android was a bytecode interpreter. In Android 2.2, Google has created a bytecode to machine code compiler. For code that the overhead of doing the native compilation is less than the cost of executing it, Dalvik with JIT should give a noticeable performance improvement.","metadata":{"transformedAt":"2026-08-18T18:32:17.824Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":437}}743{"id":"stack-3342678","source":"stackoverflow","questionId":3342678,"title":"Will F# ever be open-sourced?","tags":["open-source","compiler-construction","f#"],"text":"Title: Will F# ever be open-sourced?\nTags: open-source, compiler-construction, f#\nSource: Stack Overflow\n\nQuestion:\nThere was discussion in early 2009 about whether Microsoft would release the source for the F# compiler under the MS-PL/another license. A StackOverflow thread mentioned the state as of then.\n\nSince then a lot has happened. We've seen an official release of F# with the .NET Framework 4.0 (and Visual Studio 2010), and for all I know, it's still completely closed-source. Have Microsoft just been quiet on the subject, or have they explicitly stated that they no longer intend to open-source the compiler? Perhaps things are in the process already. Basically, any news/considerations?\n\n========================================\n\nTop Answer:\nAs Robert's comment on your question indicates, the source code is already available as part of each installation, though it does not come with an open source license. Additionally, reading between the lines, I think that things like this blog post by Don Syme still point to an open source release as a priority for the team.\n\n========================================\n\nCode:\n```text\nRecommendedDotNETNamingConventions\n```\n\n```text\nold_legacy_ones\n```\n\n========================================\n\nComments:\n- Interesting question and I would mark this as community wiki.\n- Indeed, community wiki is probably best.\n- The F# source base is not close, it has been available with the distribe for most of the project's lifetime. It's in C:\\Program Files\\FSharp-2.0.0.0\\source on my current machine. Building it is not straight forward, but some help can be found at: cs.hubfs.net/forums/thread/13456.aspx . The project is not license under an approved open source license, and they do not accept patches, so in that sense it's not open source. This may change in future, but then who knows what the future holds?\n- See also cs.hubfs.net/forums/thread/13751.aspx\n- Thanks for the clarification. So as I understand, going 'open-source' will effectively consist of the publishing of a new permissive license that applies to the existing code for the F# compiler?\n- ESP ERROR: Damnit Jim, I'm a web site not a crystal ball!\n- The question police have closed yet another one they've deemed unfit.\n- It's stupidity, indeed. *Cough* Where's the respect I should be getting for my rep? Upstarts.\n- There is an open source edition available: fsharp.github.com/fsharp (Apache 2.0 license).\n- Well no, it's clearly a request for related news. By considerations I mean will it ever happen, why (not), will there be any alternatives?\n- We have the source code to large parts of the base class library\n- @Tim, sorry I was not aware of it, thanks for the info\n- The source for the core of the .Net framework has been available since 1.0, but it has the same license as the as F#, which makes it useless for anything other than personal/academic use. (although it's great for learning from). I think it's quite arguable that MS don't open source - not long ago they opened up the entire DLR and IronPython/IronRuby under the Apache license.\n- You make a fair point, though I'm not sure it really sheds light on anything...\n- Good find there on the blog post. Indeed it does hint of an upcoming open-source release.\n- Thanks Brian, that's interesting information and speculation... We will have to wait for any affirmative answer it seems, but at least the code is there to tinker with for now.\n- Thank you for your hypothetical and non-official, yet very informative answer.\n- There is an open source edition available: fsharp.github.com/fsharp (Apache 2.0 license).","metadata":{"transformedAt":"2026-08-18T18:32:17.824Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":46,"estimatedTokens":900}}744{"id":"stack-20779631","source":"stackoverflow","questionId":20779631,"title":"Arithmetic Expression Evaluation using Reverse Polish Notation (RPN)","tags":["compiler-construction","infix-notation","expression-evaluation","shunting-yard"],"text":"Title: Arithmetic Expression Evaluation using Reverse Polish Notation (RPN)\nTags: compiler-construction, infix-notation, expression-evaluation, shunting-yard\nSource: Stack Overflow\n\nQuestion:\nA mathematical expression is usually expressed in infix notation. For evaluation purposes, we can change it to postfix (reverse polish) notation (using algorithms like Shunting-Yard) and then evaluate the postfix notation using stack.\n\nI found out that calculators use this technique, but do today's modern compilers use this for arithmetic expression evaluation? Is it efficient enough or other techniques (or algorithms) are being used?\n\n========================================\n\nCode:\n```text\n+ \n  / \\ \n 1   2\n```\n\n```text\n+ \n   /   \\\n  ^     *\n / \\   / \\\n3   4 1   2\n```\n\n```text\ninfix notation\n```\n\n```text\nShunting-Yard\n```\n\n```text\nevaluation\n```\n\n```text\n1 2 +\n```\n\n```text\n1\n```\n\n```text\n2\n```\n\n```text\n+\n```\n\n```text\n3 4 ^ 1 2 * +\n```\n\n```text\n3^4\n```\n\n```text\n3\n```\n\n```text\n4\n```\n\n```text\n^\n```\n\n```text\n3^4\n```\n\n```text\n1*2\n```\n\n```text\n1\n```\n\n```text\n2\n```\n\n```text\n*\n```\n\n```text\n1*2\n```\n\n```text\n+\n```\n\n```text\n81+2\n```\n\n```text\n83\n```\n\n========================================\n\nComments:\n- Which part of the compiler are you talking about? The parser? The intermediate representation (IR) of the code? The compile-time evaluation of the IR in optimizers? The process of generating code from IR? Or the way non-constant expressions are evaluated at run time in the evaluated code? There are different answers for each of these options. Please add some context.","metadata":{"transformedAt":"2026-08-18T18:32:17.824Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":23,"totalLines":114,"estimatedTokens":392}}745{"id":"stack-8451939","source":"stackoverflow","questionId":8451939,"title":"Compiling NASM on Mac OSX","tags":["macos","gcc","compiler-construction","linker","nasm"],"text":"Title: Compiling NASM on Mac OSX\nTags: macos, gcc, compiler-construction, linker, nasm\nSource: Stack Overflow\n\nQuestion:\nWriting a compiler in school, last milestone is generating assembly code. Trying to learn NASM. Starting at the beginning, http://www.cs.lmu.edu/~ray/notes/nasmexamples/, trying to compile a Hello World.\n\n```\n; ----------------------------------------------------------------------------\n; helloworld.asm\n;\n; This is a Win32 console program that writes \"Hello, World\" on one line and\n; then exits. It needs to be linked with a C library.\n; ----------------------------------------------------------------------------\n\n global _main\n extern _printf\n\n section .text\n_main:\n push message\n call _printf\n add esp, 4\n ret\nmessage:\n db 'Hello, World', 10, 0\n```\n\nTo assemble, link and run this program under Windows:\n\n```\nnasm -fwin32 helloworld.asm\ngcc helloworld.obj\na\n```\n\nUnder Linux, you'll need to remove the leading underscores from function names, and execute\n\n```\nnasm -felf helloworld.asm\ngcc helloworld.o\n./a.out\n```\n\nBut I'm on OSX. Found this little resource: http://salahuddin66.blogspot.com/2009/08/nasm-in-mac-os-x.html. In Mac OS X we should use format macho...\n\n```\nnasm -f macho -o hello.o hello.asm\n```\n\n...and for the linker (we need to specify the entry point)...\n\n```\nld -e main -o hello hello.o\n```\n\nBut when I do this...\n\n```\nUndefined symbols:\n \"printf\", referenced from:\n _main in hello.o\nld: symbol(s) not found for inferred architecture i386\n```\n\nSorry, I know it's a lot to read. And I doubt there are many NASM coders around these parts, but worth a try right? I'd appreciate any help I can get.\n\n========================================\n\nTop Answer:\nThe program in your example is a 32-bit Windows program. These days, it's probably better to write a 64-bit program.\n\nTo covert this to 64-bit macOS program, you should make sure you have a recent version of nasm, and have gcc installed.\n\nThe program should now look like this:\n\n```\n; ----------------------------------------------------------------------------------------\n; This is an macOS console program that writes \"Hola, mundo\" on one line and then exits.\n; It uses puts from the C library. To assemble and run:\n;\n; nasm -fmacho64 hola.asm && gcc hola.o && ./a.out\n; ----------------------------------------------------------------------------------------\n\n global _main\n extern _puts\n\n section .text\n_main: push rbx ; Call stack must be aligned\n lea rdi, [rel message] ; First argument is address of message\n call _puts ; puts(message)\n pop rbx ; Fix up stack before returning\n ret\n\n section .data\nmessage: db \"Hola, mundo\", 0 ; C strings need a zero byte at the end\n```\n\nYou'll note a few differences:\n\n- In 64 bit land, the first parameter is in RDI, not on the stack\n\n- The stack must be aligned on a 16-byte boundary before calling. When main is entered, the operating system has placed the (8 byte) return address of main on the stack, so pushing `rbx` before calling `puts` serves to get the stack realigned.\n\n- Also, nasm on macOS needs `rel`.\n\n========================================\n\nCode:\n```text\n; ----------------------------------------------------------------------------\n; helloworld.asm\n;\n; This is a Win32 console program that writes \"Hello, World\" on one line and\n; then exits.  It needs to be linked with a C library.\n; ----------------------------------------------------------------------------\n\n    global  _main\n    extern  _printf\n\n    section .text\n_main:\n    push    message\n    call    _printf\n    add     esp, 4\n    ret\nmessage:\n    db      'Hello, World', 10, 0\n```\n\n```text\nnasm -fwin32 helloworld.asm\ngcc helloworld.obj\na\n```\n\n```text\nnasm -felf helloworld.asm\ngcc helloworld.o\n./a.out\n```\n\n```text\nnasm -f macho -o hello.o hello.asm\n```\n\n```text\nld -e main -o hello hello.o\n```\n\n```text\nUndefined symbols:\n    \"printf\", referenced from:\n        _main in hello.o\nld: symbol(s) not found for inferred architecture i386\n```\n\n```text\nprintf\n```\n\n```text\n/lib/libc.so.6\n```\n\n```text\n/lib/x86_64-linux-gnu/libc.so.6\n```\n\n```text\n; ----------------------------------------------------------------------------------------\n; This is an macOS console program that writes \"Hola, mundo\" on one line and then exits.\n; It uses puts from the C library.  To assemble and run:\n;\n;     nasm -fmacho64 hola.asm && gcc hola.o && ./a.out\n; ----------------------------------------------------------------------------------------\n\n          global    _main\n          extern    _puts\n\n          section   .text\n_main:    push      rbx                     ; Call stack must be aligned\n          lea       rdi, [rel message]      ; First argument is address of message\n          call      _puts                   ; puts(message)\n          pop       rbx                     ; Fix up stack before returning\n          ret\n\n          section   .data\nmessage:  db        \"Hola, mundo\", 0        ; C strings need a zero byte at the end\n```\n\n```text\nrbx\n```\n\n```text\nputs\n```\n\n```text\nrel\n```\n\n========================================\n\nComments:\n- That page of notes has been updated to cs.lmu.edu/~ray/notes/nasmtutorial. Has more information on macOS now.\n- Note that the GUI and its tools called MacOS X is proprietary, but the real system, called Darwin, is open source. I agree that Linux has better documentation for these, but assembly on Mac is by no means a \"closed\" thing. The specs are there, Maybe it just happens that the lack of popular *tutorials* is a result of the little popular interest in assembly. Most of the time it's not worth it (unless you're writing a compiler :-) If you're comfortable with assembly in BSDs (which have many differences to Linux kernel) MacOS X assembly is an easy jump, sometimes just a matter of checking headers.\n- Sorry I'm ignorant in the matter. Is the library stdio.h? How would I link the library if I found it?\n- `stdio.h` is a header file, not a library. I'll guess you want some `lib*.dylib` on MacOSX.\n- `stdio.h` is useful and meaningful only in C and C++ (then the compiler should know how to find, perhaps with additional `-I` options). You don't need it in assembly\n- To find out what `gcc` is doing when compiling a `helloworld.c`, run it with `gcc -v`\n- OS X uses free software for such tasks, namely LLVM and its frontend clang.\n- +1 For showing how to use gcc instead of ld. For whatever reason, gcc works far better for me at compiling xxx.o than ld.\n- On Mac ARM silicon, you can compile this to x64 asm too: `nasm -fmacho64 helloworld.asm && clang -arch x86_64 helloworld.o -o helloworld` - note that this will emit a warning `ld: warning: no platform load command ...` which can be ignored.","metadata":{"transformedAt":"2026-08-18T18:32:17.824Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":215,"estimatedTokens":1666}}746{"id":"stack-2484973","source":"stackoverflow","questionId":2484973,"title":"How to Work Around Limitations in Generic Type Constraints in C#?","tags":["c#","generics","compiler-construction","type-constraints"],"text":"Title: How to Work Around Limitations in Generic Type Constraints in C#?\nTags: c#, generics, compiler-construction, type-constraints\nSource: Stack Overflow\n\nQuestion:\nOkay I'm looking for some input, I'm pretty sure this is not currently supported in .NET 3.5 but here goes. \n\nI want to require a generic type passed into my class to have a constructor like this:\n\n```\nnew(IDictionary)\n```\n\nso the class would look like this\n\n```\npublic MyClass where T : new(IDictionary)\n{\n T CreateObject(IDictionary values)\n {\n return new T(values);\n }\n}\n```\n\nBut the compiler doesn't support this, it doesn't really know what I'm asking.\n\nSome of you might ask, why do you want to do this? Well I'm working on a pet project of an ORM so I get values from the DB and then create the object and load the values.\n\nI thought it would be cleaner to allow the object just create itself with the values I give it. As far as I can tell I have two options:\n\n1) Use reflection(which I'm trying to avoid) to grab the PropertyInfo[] array and then use that to load the values.\n\n2) require T to support an interface like so:\n\npublic interface ILoadValues\n{\n void LoadValues(IDictionary values);\n}\n\nand then do this\n\n```\npublic MyClass where T:new(),ILoadValues\n{\n T CreateObject(IDictionary values)\n {\n T obj = new T();\n obj.LoadValues(values);\n return obj;\n }\n}\n```\n\nThe problem I have with the interface I guess is philosophical, I don't really want to expose a public method for people to load the values. Using the constructor the idea was that if I had an object like this\n\n```\nnamespace DataSource.Data\n{\n public class User\n {\n protected internal User(IDictionary values)\n {\n //Initialize\n }\n }\n}\n```\n\nAs long as the `MyClass` was in the same assembly the constructor would be available. I personally think that the Type constraint in my opinion should ask (Do I have access to this constructor? I do, great!)\n\nAnyways any input is welcome.\n\n========================================\n\nTop Answer:\nAs stakx has said, you can't do this with a generic constraint. A workaround I've used in the past is to have the generic class constructor take a factory method that it can use to construct the T:\n\n```\npublic class MyClass\n{\n public delegate T Factory(IDictionary values);\n\n private readonly Factory _factory;\n\n public MyClass(Factory factory)\n {\n _factory = factory;\n }\n\n public T CreateObject(IDictionary values)\n {\n return _factory(values);\n }\n}\n```\n\nUsed as follows:\n\n```\nMyClass instance = new MyClass(dict => new Bob(dict));\nBob bob = instance.CreateObject(someDictionary);\n```\n\nThis gives you compile time type safety, at the expense of a slightly more convoluted construction pattern, and the possibility that someone could pass you a delegate which doesn't actually create a new object (which may or may not be a major issue depending on how strict you want the semantics of CreateObject to be).\n\n========================================\n\nCode:\n```text\nnew(IDictionary<string,object>)\n```\n\n```text\npublic MyClass<T>  where T : new(IDictionary<string,object>)\n{\n  T CreateObject(IDictionary<string,object> values)\n  {\n    return new T(values);\n  }\n}\n```\n\n```text\npublic MyClass<T> where T:new(),ILoadValues\n{\n  T CreateObject(IDictionary<string,object> values)\n  {\n    T obj = new T();\n    obj.LoadValues(values);\n    return obj;\n  }\n}\n```\n\n```text\nnamespace DataSource.Data\n{\n  public class User\n  {\n    protected internal User(IDictionary<string,object> values)\n    {\n      //Initialize\n    }\n  }\n}\n```\n\n```text\nMyClass<T>\n```\n\n```text\ninternal interface ILoadValues\n{\n    void LoadValues<TKey, TValue>(IDictionary<TKey, TValue> values);\n}\n\npublic class Base : ILoadValues\n{\n    void ILoadValues.LoadValues<TKey, TValue>(IDictionary<TKey, TValue> values)\n    {\n        // Load values.\n    }\n}\n\npublic class MyClass<T>\n    where T : Base, new()\n{\n    public T CreateObject(IDictionary<string,object> values)\n    {\n        ILoadValues obj = new T();\n        obj.LoadValues(values);\n        return (T)obj;\n    }\n}\n```\n\n```text\npublic class MyClass<T>\n{\n  public delegate T Factory(IDictionary<string, object> values);\n\n  private readonly Factory _factory;\n\n  public MyClass(Factory factory)\n  {\n    _factory = factory;\n  }\n\n  public T CreateObject(IDictionary<string, object> values)\n  {\n    return _factory(values);\n  }\n}\n```\n\n```text\nMyClass<Bob> instance = new MyClass<Bob>(dict => new Bob(dict));\nBob bob = instance.CreateObject(someDictionary);\n```\n\n```text\npublic virtual void InitializeClass(DataRow dr)\n    {\n        Type type = this.GetType();\n        PropertyInfo[] propInfos = type.GetProperties();\n\n        for (int i = 0; i < dr.ItemArray.GetLength(0); i++)\n        {\n            if (dr[i].GetType() != typeof(DBNull))\n            {\n                string field = dr.Table.Columns[i].ColumnName;\n                foreach (PropertyInfo propInfo in propInfos)\n                {\n                    if (field.ToLower() == propInfo.Name.ToLower())\n                    {\n                        // get data value, set property, break\n                        object o = dr[i];\n                        propInfo.SetValue(this, o, null);\n                        break;\n                    }\n                }\n            }\n        }\n    }\n```\n\n```text\npublic abstract class GenericDataList<T> : List<T> where T : BaseDataClass\n{\n    protected void InitializeList(string sql)\n    {\n        DataHandler dh = new DataHandler(); // my general database class\n        DataTable dt = dh.RetrieveData(sql); \n        if (dt != null)\n        {\n            this.InitializeList(dt);\n            dt.Dispose();\n        }\n        dt = null;\n        dh = null;\n    }\n\n    protected void InitializeList(DataTable dt)\n    {\n        if (dt != null)\n        {\n            Type type = typeof(T);\n            MethodInfo methodInfo = type.GetMethod(\"InitializeClass\");\n\n            foreach (DataRow dr in dt.Rows)\n            {\n                T t = Activator.CreateInstance<T>();\n                if (methodInfo != null)\n                {\n                    object[] paramArray = new object[1];\n                    paramArray[0] = dr;\n                    methodInfo.Invoke(t, paramArray);\n                }\n\n                this.Add(t);\n            }\n        }\n    }\n}\n```\n\n```text\nprotected void InitializeList(DataTable dt)\n        {\n            if (dt != null)\n            {\n                Type type = typeof(T);\n\n                foreach (DataRow dr in dt.Rows)\n                {\n                    T t = Activator.CreateInstance<T>();\n                    (t as BaseDataClass).InitializeClass(dr);\n\n                    this.Add(t);\n                }\n            }\n        }\n```\n\n========================================\n\nComments:\n- +1 (more if I could). This is a bit above my pay grade and I can't actually state with confidence that this is correct (which I'm sure it is). However, your example is extremely clear and quite easy to . Well said, sir!\n- I set you as the answer because I never knew you could hide an interface implementation by explicitly declaring it. Or at least it didn't click until you wrote it. Thanks!\n- If your using DataSets or Typed DataSets, why wouldn't you use the MetaData in these, If your using a Typed dataSet, you sort of know what the data is before you start reflecting. Why use an ORM if your going to use DataSets? DataSets also have problems with large data, better off using a DataReader and streaming the data slowly keeping the memory allocation down to a minimum.\n- @Anthony Nice answer though, good to see that people care enough to give so complete an answer :)\n- I usually do not use DataSets, but rather read straight into a DataTable. I suppose I could use a reader, but I've always thought it was simpler if I needed to pass the object around to use a DataTable/DataRow rather than keep a database connection open. But, like I said, I'm open to criticism, and thanks for your feedback.\n- @Anthony, not criticizing, sorry if it comes across as that, and certainly not patronising either. I get into trouble using Data as an Object, I usually run out of memory, especially when not connecting to an SQL DB (with filter in select) and using something like a csv file. Came across it once with a very large Spreadsheet, kept blowing the memory footprint. So switched to Readers and never looked back.\n- I don't use \"criticism\" as a negative term! I was thinking more along the lines of constructive criticism. Like I said, I'm looking for it.\n- To answer your question on how I would load values without reflection. We have a code generation framework that generates classes based on a db schema. It's template based so I could generate the LoadValues method via the code generation.\n- by the way. (typeof(T))Activator.CreateInstance(typeof(T)); is about 4-5X faster than Activator.CreateInstance();","metadata":{"transformedAt":"2026-08-18T18:32:17.825Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":298,"estimatedTokens":2204}}747{"id":"stack-32011751","source":"stackoverflow","questionId":32011751,"title":"Does Symbol table for C++ code contain function names along with class names?","tags":["c++","function","compiler-construction","symbol-table","class-names"],"text":"Title: Does Symbol table for C++ code contain function names along with class names?\nTags: c++, function, compiler-construction, symbol-table, class-names\nSource: Stack Overflow\n\nQuestion:\nI have been searching through various posts regarding whether symbol table for a C++ code contains functions' name along with the class name. Something which i could find on a post is that it depends on the type of compiler,\n\nif it compiles code in one-pass then it will not need to store class name and subroutine names in your symbol table \n\nbut if it is a multi-pass compiler, it could add information about the class(es) it encounters and their subroutines so that it could do argument type checking and issue meaningful error messages.\n\nI could not understand whether it is actually compiler dependent or not? I was assuming that compiler(for C++ code) would put function names with class names in the table whether it is single pass or multi pass compiler. How is it dependent on the passes? I don't have such a great/deep knowledge.\nMoreover, could anyone show a sample symbol table for a simple C++ class, how would it look like (function names with class name)?\n\n========================================\n\nTop Answer:\nA symbol table maps names to constructs within the program. As such it is used to record the names of classes, functions, variables, and anything else that has a user-specified name within the program.\n\n(There are two common kinds of symbol table - one that the compiler maintains when it is compiling your program, and another that exists in object file so that it can be linked to other objects. The two are strongly related, but need not have similar representation internally. Typically only some of the symbols from the compiler's symbol table will be output into the object).\n\nPart of what you say makes no sense:\n\n if it compiles code in one-pass then it will not need to store class name and subroutine names in your symbol table \n\nHow can the compiler determine to what construct a name refers if it cannot look it up in the symbol table?\n\n but if it is a multi-pass compiler, it could add information about the class(es) it encounters and their subroutines so that it could do argument type checking and issue meaningful error messages.\n\nThere's no reason it could not do this in a single pass.\n\n I could not understand whether it is actually compiler dependent or not?\n\nAll compilers are going to use a symbol table, but its use will be hidden inside the implementation.\n\n I was assuming that compiler(for C++ code) would put function names with class names in the table whether it is single pass or multi pass compiler. How is it dependent on the passes?\n\nHow is *what* dependent on the passes? All names go in the symbol table - that's what it's for - and usually symbol resolution is important for just about everything else the compiler does, so it needs to be done early (i.e. in the first pass - and in fact the main purpose of the first pass in a multi-pass compiler compiler may well be just to build the symbol table!).\n\n Moreover, could anyone show a sample symbol table for a simple C++ class, how would it look like (function names with class name)?\n\nI'll give it a stab:\n\n```\nclass A\n{\n int a;\n void f(int, int);\n};\n```\n\nWill yield a symbol table containing symbols \"A\", \"a\", and \"f\". Typically \"a\" and \"f\" would be marked with a scope to simplify lookup, eg:\n\n```\n\"A\" -> (class)\n\"A::a\" -> (class variable member)\n\"A::f(int,int)\" -> (class function member)\n```\n\nIt's also possible that the `a` and `f` symbols will not be stored in the top-level symbol table, but rather that each name space (including C++ namespaces and classes) will have its own symbol table, containing the symbols defined inside it. But this is, arguably, just a data structure choice. You can still abstractly view the symbol table as a flat table, where a name maps to a construct.\n\nIn general the \"A::a\" symbol would not be output to the object file, since it is not required for linking.\n\n========================================\n\nCode:\n```text\nclass A\n{\n    int a;\n    void f(int, int);\n};\n```\n\n```text\n\"A\"  -> (class)\n\"A::a\"  ->  (class variable member)\n\"A::f(int,int)\"  ->  (class function member)\n```\n\n```text\na\n```\n\n```text\nf\n```\n\n========================================\n\nComments:\n- Err... we're talking about the *symbol table* here, the one that has to *unambiguously* identify an entity in order to resolve external dependencies? How, do you think, would \"unambiguously\" work *unless* the function name were included?\n- Read this en.m.wikipedia.org/wiki/Name_mangling\n- \"if it compiles code in one-pass then it will not need to store class name and subroutine names in your symbol table\" - I can't imagine what you mean by this. What do you suppose a symbol table is for? I think you've got some fundamental misunderstanding that lies below the level of this question.\n- Basically the symbol table contains the fully qualified names, including the parameter types.\n- Note that any kind of database table with an enumerated column can be split up in multiple tables. If I have one `FOO` table with a `COLOR=RED | GREEN | BLUE` column, I can split that table in three tables `FOO_RED`, `FOO_GREEN` and `FOO_BLUE`. I can split a symbol table with `TYPE = FUNCTION | CLASS | VARIABLE` into 3 separate tables for functions, classes and variables. Logically it's all the same, just a matter of convenience.\n- Mangled name does not typically include the return value type.\n- You're confusing the symbols embedded in object files with the symbol table built by the compiler, which itself has no use for mangled names.\n- what is a \"precise symbol table\". All linkers I know were able to work with the default \"precise\" symbol table... So what exactly is the difference?\n- There are a lot of sloppy reverse engineering tools out there that build sloppy symbol tables: they are incomplete, they fail to model inheritance let alone overload lookup correctly, etc. Doxygen I think now uses Clang, but earlier versions used a complete (sloppy) hack for parsing C++, and then built such a sloppy table. Tools that actually manipulate real programs (GCC, Clang, linkers, DMS can't avoid being precise, or they wouldn't work).","metadata":{"transformedAt":"2026-08-18T18:32:17.825Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":100,"estimatedTokens":1559}}748{"id":"stack-24836530","source":"stackoverflow","questionId":24836530,"title":"Compiling local variables for a stack machine","tags":["variables","compiler-construction","vm-implementation","stack-machine"],"text":"Title: Compiling local variables for a stack machine\nTags: variables, compiler-construction, vm-implementation, stack-machine\nSource: Stack Overflow\n\nQuestion:\nI'm building a toy compiler from a C like language to a stack machine and I'm at the point where I need to figure out what to do with functions and block local variables. Thinking through it abstractly it looks like I have two options at opposite ends of the spectrum: 1) Preprocess and preallocate stack space for each variable, 2) Add special instructions to the VM to walk the stack.\n\n### Pre-process and preallocate stack space for each variable\n\nThis has the advantage of giving me all the addresses for the variables ahead of time so I don't have to be very clever or add any extra instructions to the VM for walking the stack. The downside is that it is potentially very wasteful because conditional code that never executes but declares a whole bunch of variables will take up a lot of unnecessary space. For example,\n\n```\na : t1 = value;\nif (test) {\n b : t2; c : t3; d : t4; ...;\n}\n```\n\nIn the above code even if `test` is always false I will still allocate space for all those variables within the conditional branch.\n\n### Add special instructions to the VM to walk the stack\n\nThe other approach I could come up with was to generate code for each variable declaration and then add some special VM instructions to figure out the address of those variables at runtime. This solves the problem of wasted stack space but then adds a computational overhead that I can potentially solve by some caching methods.\n\nSo what's the correct approach and is there another approach I didn't think of that is better?\n\n========================================\n\nCode:\n```text\na : t1 = value;\nif (test) {\n  b : t2; c : t3; d : t4; ...;\n}\n```\n\n```text\ntest\n```\n\n```text\ntotal = apples + oranges\n```\n\n```text\nldloc 'apples'   # load a local onto stack\n ldloc 'oranges'  # load a local onto stack\n add              # add 2 operands on stack\n stloc 'total'    # store local from stack\n```\n\n========================================\n\nComments:\n- I was thinking along similar lines. Can you elaborate on what you mean by pre-allocating all the local variables at the beginning of each method. More specifically how do you deal with dead code? Are the local variables in the dead section pre-allocated as well if you didn't have dead code elimination.\n- Let me answer WRT dead-code first: Don't do anything with dead code, it isn't your concern (as a VM / interpreter). Leave that up to the compiler / programmers. Pre-allocation makes the implementation simple, but also allows the compiler some flexibility to map its high level variables or temporary variables (compiler generated variables for things that don't map to an explicit variable declaration). So yes it means that you can't necessarily use the high-level variable name for your intermediate code (for example, when 2 variables of the same name exists in the source).\n- Well, in this instance I'm all three: vm implementer, compiler writer, programmer so there are some cross-cutting concerns I'm trying to balance. In any case, thanks. This was very helpful.\n- When compiling your method, you can decide how many high level variables are stored in locals, and you can also decide how many temporaries to allocate (for keeping values in fast register memory). Allocate them all on some sort of random access stack or structure that gets saved/restored during method calls. You use opcodes like storelocal or loadlocal to retrieve the values from globals, fields, etc. into your locals and to save the values after expressions have completed. Locals are purely symbolic, and a JIT may map them to registers.\n- @davidk01 - Feel free to email me, my contact info is in my profile, I am happy to answer questions to help you with your VM. One of the best VMs to learn from is the CLR. Use visual studio to study how it compiles high level code. Once you learn the IL, you will get a feel for how the VM works, and it will give you ideas for your own. But that doesn't mean yours has to work the same way. There are many ways to design a VM around the same basic ideas.\n- Will do. Really appreciate the help.","metadata":{"transformedAt":"2026-08-18T18:32:17.825Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":60,"estimatedTokens":1053}}749{"id":"stack-12519738","source":"stackoverflow","questionId":12519738,"title":"C# dynamic type causes Console.WriteLine to be resolved with reflection in IL","tags":["c#","dynamic","reflection","compiler-construction","il"],"text":"Title: C# dynamic type causes Console.WriteLine to be resolved with reflection in IL\nTags: c#, dynamic, reflection, compiler-construction, il\nSource: Stack Overflow\n\nQuestion:\nI've been playing around with some C# statements in LINQPad with a view to understanding what intermediate language code is emitted.\n\nI first tried the following code:\n\n```\nvar Container = new {Name = \"James\"};\nConsole.WriteLine(Container.Name);\n```\n\nAnd saw the following six lines of IL emitted:\n\n```\nIL_0001: ldstr \"James\"\nIL_0006: newobj <>f__AnonymousType0..ctor\nIL_000B: stloc.0 \nIL_000C: ldloc.0 \nIL_000D: callvirt <>f__AnonymousType0.get_Name\nIL_0012: call System.Console.WriteLine\n```\n\nWhich, is broadly what I expect, and is quite a nice demonstration of how anonymous types are read-only/immutable, seeing as there is no set_Name property.\n\nNext I tried the statements:\n\n```\ndynamic Container = new System.Dynamic.ExpandoObject();\nContainer.Name = \"James\";\nConsole.WriteLine(Container.Name);\n```\n\nWhich causes a huge amount of IL to be emitted. I'll not paste it here, but you can find it in this pastebin. \n\nI understand there is quite a bit of overhead with regard to managing the dynamic type and ExpandoObject, but I don't understand why it appears that the call to `System.Console.WriteLine` is in this case performed through internal reflection. \n\n```\nIL_0072: ldstr \"WriteLine\"\n....\nIL_00BF: ldtoken System.Console\n```\n\nIn the first segment of code, after the property was retrieved and stored, it was a one-line IL statement that invoked `System.Console.WriteLine`.\n\nSo why is all this extra required for the call with a `dynamic` type?\n\n========================================\n\nTop Answer:\nWhat's happening is the compiler is creating your code in such a way that it can be \"late-binded\". Late binding means that rather then resolve your objects during compilation, as with traditional data types and objects, the object is being resolved at run time, while your assembly is actually in memory and running.\n\nIf you were to look at your code in Reflector or dotPeek, you would see that your dynamic objects were decorated with a `[Dynamic]` attribute. While your program is running in memory, when it comes to an object that has been decorated with this attribute, the call to this object is piped through a dynamic `Container` (or whatever your object is called). This `Container` is initialized with the Binder responsible for run time binding. That is what all the called to `Microsoft.CSharp.RuntimeBinder` does. This `RuntimeBinder` is used later to invoke properties or methods or whatever is dynamic.\n\nI hope this clears things up a bit. I'm typing on my android, so the explanation might be less than ideal. I will clean it up later.\n\n========================================\n\nCode:\n```text\nvar Container = new {Name = \"James\"};\nConsole.WriteLine(Container.Name);\n```\n\n```text\nIL_0001:  ldstr       \"James\"\nIL_0006:  newobj      <>f__AnonymousType0<System.String>..ctor\nIL_000B:  stloc.0     \nIL_000C:  ldloc.0     \nIL_000D:  callvirt    <>f__AnonymousType0<System.String>.get_Name\nIL_0012:  call        System.Console.WriteLine\n```\n\n```text\ndynamic Container = new System.Dynamic.ExpandoObject();\nContainer.Name = \"James\";\nConsole.WriteLine(Container.Name);\n```\n\n```text\nIL_0072:  ldstr       \"WriteLine\"\n....\nIL_00BF:  ldtoken     System.Console\n```\n\n```text\nSystem.Console.WriteLine\n```\n\n```text\nSystem.Console.WriteLine\n```\n\n```text\ndynamic\n```\n\n```text\ndynamic\n```\n\n```text\nWriteLine\n```\n\n```text\ndynamic\n```\n\n```text\ndynamic\n```\n\n```text\nobject\n```\n\n```text\nstring\n```\n\n```text\nToString\n```\n\n```text\nExpandoObject\n```\n\n```text\nWriteLine\n```\n\n```text\nWriteLine\n```\n\n```text\n[Dynamic]\n```\n\n```text\nContainer\n```\n\n```text\nContainer\n```\n\n```text\nMicrosoft.CSharp.RuntimeBinder\n```\n\n```text\nRuntimeBinder\n```\n\n========================================\n\nComments:\n- Actually, your `ToString()` trick won't work (unless you also add an explicit cast to `string`).\n- You wouldn't see any attribute in this case, because local variables can't have attributes.\n- @svick -- No, but properties can have attributes and in his example `Name` is a property\n- But the type of that property is `string`, not `object` with `DynamicAttribute`.\n- Oh you are right....the `Name` property is string. But the `Container` object would be decorated with a `[dynamic]` attribute tag.\n- No, it wouldn't. Objects don't have attributes and neither do locals. The attribute is only necessary if you access the `dynamic` object from the outside, which can't happen if it's a local variable.","metadata":{"transformedAt":"2026-08-18T18:32:17.825Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":22,"totalLines":168,"estimatedTokens":1142}}750{"id":"stack-1045827","source":"stackoverflow","questionId":1045827,"title":"Interpret something, and run the generated bytecode in Java?","tags":["java","compiler-construction","jvm","interpreter","bytecode"],"text":"Title: Interpret something, and run the generated bytecode in Java?\nTags: java, compiler-construction, jvm, interpreter, bytecode\nSource: Stack Overflow\n\nQuestion:\nI'm writing a toy interpreter with a REPL in Java. I'd like to generate bytecode from the language and run that, instead of interpreting an AST and running that instead.\n\nSince my Java is a bit rusty, is it possible to run generated bytecode on the fly on the JVM?\n\n========================================\n\nTop Answer:\nHave a look at Javassist which contains a snippet compiler allowing you to compile Java snippets to bytecode and define them as a method in a class which you can then invoke.","metadata":{"transformedAt":"2026-08-18T18:32:17.825Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":13,"estimatedTokens":165}}751{"id":"stack-24938018","source":"stackoverflow","questionId":24938018,"title":"Is generation of three address code compulsory in a compiler?","tags":["compiler-construction"],"text":"Title: Is generation of three address code compulsory in a compiler?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIs generation of three address code compulsory in a compiler? My compiler already converts expressions to AST.","metadata":{"transformedAt":"2026-08-18T18:32:17.825Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":6,"estimatedTokens":61}}752{"id":"stack-6579931","source":"stackoverflow","questionId":6579931,"title":"How are interpreted languages (like Ruby) run?","tags":["ruby","compiler-construction","programming-languages","interpreter"],"text":"Title: How are interpreted languages (like Ruby) run?\nTags: ruby, compiler-construction, programming-languages, interpreter\nSource: Stack Overflow\n\nQuestion:\nI am going to learn Ruby. I know it is a interpreted language. I know that compiled languages are translated to machine code eventually, but what does the ruby interpreter do? I read that the interpreter was written in C, but does each line of ruby convert to c, which again compiles to machine code? I also heard of JIT, but if that adds much of complexity to the answer you don't need to answer that. What I am looking for is what happens to my Ruby code.","metadata":{"transformedAt":"2026-08-18T18:32:17.825Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":6,"estimatedTokens":154}}753{"id":"stack-18656320","source":"stackoverflow","questionId":18656320,"title":"Could C++ also be interpreted instead of compiled?","tags":["c++","compiler-construction","theory","interpreter","interpreted-language"],"text":"Title: Could C++ also be interpreted instead of compiled?\nTags: c++, compiler-construction, theory, interpreter, interpreted-language\nSource: Stack Overflow\n\nQuestion:\nI know that interpreting C++ code might not hold practical value and this question is just for entertainment and learning purpose.\n\nIs it possible to interpret C++ code statement by statement instead of compiling it? Please also explain the reason for the answer.\n\nIf it is not possible, is there a subset of the language that can be interpreted?\n\n========================================\n\nTop Answer:\nThere's no clear-cut boundary between compilation and interpretation. Most languages that are usually thought of as interpreted are actually compiled for some kind of VM. The same can be done for C++.\n\n========================================\n\nCode:\n```text\n#include <iostream>\n\nstruct C\n{\n  void foo() { std::cout << i << '\\n'; }\n  int i;\n};\n\nint main()\n{\n  C c;\n  c.i = 0;\n  c.foo();\n}\n```\n\n```text\n#include <iostream>\n\ninline void foo();\n\nint main()\n{\n  foo();\n}\n\ninline void foo()\n{\n  std::cout << \"x\\n\";\n}\n```\n\n```text\nextern int i;\n\nint main()\n{\n  return i;\n}\n\nint i = 0;\n```\n\n```text\ninline\n```\n\n========================================\n\nComments:\n- root.cern.ch/drupal/content/cint: \"CINT is an interpreter for C and C++ code\"\n- ...which will supersede CINT at some point.\n- If it can be compiled, there is not technical reason it cannot be interpreted.\n- For a slightly different angle, take a look at code.google.com/p/c-semantics - which is, in essence, a C interpreter implemented on top of a term rewriting engine. It deliberately does not lower the source language down to some simpler form (i.e., no \"compilation\" whatsoever), and is supposed to define C operational semantics in a clean, readable and yet formal way.\n- interestingly, debuggers like `gdb` do the opposite: they take compiled code (with debug symbols) and allow you to step line by line, exposing all variables, like an interpreter.\n- Okay, could C++ be interpreted *statement by statement*? I know that even most of the languages considered as interpreted, do more that that. For example JavaScript scans an entered scope for function definitions. But with scanning more than the current line, every compiled language could be interpreted. Therefore this is where I would like to set the boundary.\n- @danijar: depends on what you mean by that. You can write an extremely stupid C++ interpreter that doesn't really compile anything but just inspects each statement every time to figure out what it's supposed to do.\n- @danijar: if, by contrast, you mean an interactive toplevel like the one for Python, I guess that would be possible too but it wouldn't really be C++ (which executes whole functions, starting with `main`, not loose statements), so you'd be extending the language.\n- What I mean is the interpreter stats in `main()` and then reads one statement, executes it and continues with the statement below. And when a statement is a function call, the interpreter will jump to the first statement in the function body, of course. It is kind of how you may have learned programming.\n- In other words, I mean the interpreter cannot lookout for coming lines. It can store anything necessary from previous lines. That member functions can access class members would not be a problem as long as the class header file is included before, which contains the member definitions.\n- Thanks a lot for the update containing the code example. It is clear now, that only a subset of C++ can be interpreted with this underlying definition. Classes can't because their member order is not guaranteed. But what if we use a header file to define members and method signatures.\n- @danijar You'd have to make sure the definition of `main` is the last declaration in the translation unit, and that all function definitions precede any namespace-scope variables (because functions could be called from their constructors).\n- `goto` also violates the top-to-bottom execution rule. Its labels may occur anywhere in the present function body.\n- @larsmans Very good point. It's probably the most straightforward counterexample there is.\n- I may be missing something, but I don't see why these would cause a problem.. Obviously a C interpreter would need different semantics than a compiler, but I don't see how your examples would make interpretation impossible.. In the last example, it could know that i had not been defined, and throw a runtime error if you call main before defining i.. This would *not* make it impossible to implement without some kind of lookahead. It would require the interpreter to behave slightly differently than a compiler would, but it already does since it can't look ahead to resolve symbols.\n- Basically, functions are not executed at the same time as they're defined, so this doesn't have to be a problem.","metadata":{"transformedAt":"2026-08-18T18:32:17.825Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":86,"estimatedTokens":1218}}754{"id":"stack-29049554","source":"stackoverflow","questionId":29049554,"title":"Isn't an LR(0) parser using lookaheads as well?","tags":["parsing","compiler-construction","ll-grammar","lr-grammar","shift-reduce"],"text":"Title: Isn't an LR(0) parser using lookaheads as well?\nTags: parsing, compiler-construction, ll-grammar, lr-grammar, shift-reduce\nSource: Stack Overflow\n\nQuestion:\nAn LL(1)-parser needs a lookahead-symbol for being able to decide which production to use. This is the reason why I always thought the term \"lookahead\" is used, when a parser looks at the next input token without \"consuming\" it (i.e. it can still be read from the input by the next action). LR(0) parsers, however, made me doubt that this is correct:\n\nEvery example of LR(0)-parsers that I've seen also uses the next input token for deciding whether to shift or to reduce.\nIn case of reduction the input token is not consumed.\n\nI used the freeware tool \"ParsingEmu\" for generating an LR-table and performing an LR evalutation below for the word \"aab\". As you can see the column head contain tokens. From the evaluation you can see that the parser is deciding which column to use by looking at the next input token. But when the parser reduces in steps 4 - 6 the input doesn't change (although the parser needs to know the next input token \"$\" when performing a transition to the next state).\n\nGrammar:\n\n```\nS -> A\nA -> aA\nA -> b\n```\n\nTable:\n\nEvaluation:\n\nNow I made following assumptions for the reason of my confusion:\n\nMy assumption for the definition of \"lookahead\" (lookahead = input token not being consumed) is wrong. Lookahead just means two different things for either LL-parsers or LR-parsers. If so, how can \"lookahead\" be defined then?\n\nLR-parsers have (from the theoretical point of view when you would use push-down automaton) additional internal states where they consume the input token by putting it on the stack and therefore are able to make the shift- reduce- decision by just looking on the stack.\n\nThe evaluation shown above is LR(1). If true, what would an LR(0) evaluation look like?\n\nNow what is correct, 1, 2 or 3 or something completely different?\n\n========================================\n\nCode:\n```text\nS -> A\nA -> aA\nA -> b\n```\n\n========================================\n\nComments:\n- possible duplicate of How can an LR(0) parser ever leave state 0? (my own question)\n- could you the link for Parsing EMU?\n- I'm sorry, it seems that ParsingEmu completely disappeared from the web. I can only find a paper where it is mentioned.\n- Is the parsing shown in the images of my question a LR(1) parser then?\n- But the grammar would be LR(0)-parsable as in one state it has to either shift (states 0 and 1) or to reduce(states 3 and 4)? It would not be LR(0)-parsable if one state-row contained both shift and reduce actions depending on the column (= token)?\n- @fishbone: Right. It's LR(0). No state JJ as both shift and reduce act&#237;ons, or two reduce actions for different productions.\n- \"the decision to shift (or error) must be made before reading the next input token\" Why? I am still confused... If we haven't read the next token, how to make sure shift is successful? For example: In a given parser state and no reduce is possible, if the next input token is 't' then it can be shifted, otherwise it can't and report error. So it still need read next token to decide whether shift or not.\n- @chansey: the parser decides to \"shift or error\" before it sees the following token. If the shift subsequently fails, an error will be produced. Perhaps I could have phrased that better. The point is that an LR(0) grammar must make the decision to reduce or shift using 0 lookahead symbols. But that doesn't stop it from failing once it does read the next symbol. (Or indeed even before it reads the next symbol; a reduction action can place the parser into a state with no applicable actions, although the algorithm for creating LR(0) machines will never generate such a case.)\n- I can understand \"LR(0) grammar must make the decision to **reduce** using 0 lookahead symbols\". For example: We can not use Set to resolve reduce/reduce confliction in LR(0). But my major confusion is about **shift**. For example: in some particular state of viable prefix DFA, we have only one item: T -> .int, but the next token is ')' instead of 'int', it cause parse error. So we read the **next token ')'** to make sure parse error. (But LR(0) shouldn't read next token, because it is 0 lookahead)\n- @chansey: shift *consumes* a token, so it has to read it :) The LR(0) parser does not need to know which state it will shift into before it reads the token; it only needs to know that it will shift. Once it makes that decision, it's irrevocable; it cannot decide that a reduction made more sense. I suppose this seems unsymmetrical, and it is: the process of parsing is entirely about reduction actions, since the goal is to discover a derivation. Shifting is just a way of saying, \"no reduction here, try the next input point.\" That's also true of the parser state, which is...\n- ...an implementation detail, if you like. The observable actions of the parser are \"emit a derivation step\" and \"consume an input token\", and the LR(0) parser must de the first before the second. More generally, an LR(k) parser can do at most `k` consumes before it emits a derivation ending at a consumed token.\n- This answer is the best it helped me clear confusion regarding lookahead. I have just one doubt in LL(0) parser when is the input symbol consumed?\n- @kapil: an LL parser has a prediction stack, which is a representation of what it expects to see next. If the top of the prediction stack is a non-terminal, the parser needs to decide which of the non-terminal's productions to predict. It then pops the non-terminal and pushes the production's right-hand side. If the top of the prediction stack is a terminal, the parser consumes a token which must be the same as the predicted token at the top of the stack. If it's the right token, it pops the prediction stack; otherwise it declares an error.","metadata":{"transformedAt":"2026-08-18T18:32:17.825Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":59,"estimatedTokens":1466}}755{"id":"stack-7923489","source":"stackoverflow","questionId":7923489,"title":"Language for LLVM-based compiler","tags":["compiler-construction","llvm"],"text":"Title: Language for LLVM-based compiler\nTags: compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nI'm looking at doing some initial tests and experiments with a toy language and building an LLVM-based compiler to get some benchmarkable results. \n\nI realize that C++ is LLVM's \"native\" and best-supported language, but I *really* don't want to use C or C++. So I'm looking for a higher-level language that has stable and established support for working with the LLVM API.\n\nMy initial work with the Python LLVM library was a bit of a disappointment as the project seems to have been abandoned, though I hear promising news about PyPy integrating LLVM support into their framework, but I'd like to know what people's experience is before sinking a lot of time into any given route.\n\nSo my question is, have any of you had a positive experience working with LLVM using a higher-level language and/or framework? And if so, which? Or are there any that you would say to avoid?\n\n========================================\n\nCode:\n```text\nP/Invoke\n```\n\n========================================\n\nComments:\n- PyPy has abandoned LLVM quite a while ago. And even if PyPy used LLVM, it would only be used as *backend* for the interpreters and JIT-compilers created by the translation toolchain, which takes an *interpreter* written in RPython and compiles it to low-level code (optionally adding JIT and other bells and whistles along the way).","metadata":{"transformedAt":"2026-08-18T18:32:17.825Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":24,"estimatedTokens":360}}756{"id":"stack-15064980","source":"stackoverflow","questionId":15064980,"title":"In CUP: How to make something optional to parse?","tags":["parsing","compiler-construction","jflex"],"text":"Title: In CUP: How to make something optional to parse?\nTags: parsing, compiler-construction, jflex\nSource: Stack Overflow\n\nQuestion:\n```\nPROC_DECL -> \"proc\" [ \"ret\" TYPE ] NAME\n \"(\" [ PARAM_DECL { \",\" PARAM_DECL } ] \")\"\n \"{\" { DECL } { STMT } \"}\"\n```\n\nThis is the grammar for a Procedure declaration.\n\nHow do you say that the \"ret\" TYPE is optional without making multiple cases?\n\n========================================\n\nCode:\n```text\nPROC_DECL -> \"proc\" [ \"ret\" TYPE ] NAME\n                  \"(\" [ PARAM_DECL { \",\" PARAM_DECL } ] \")\"\n                  \"{\" { DECL } { STMT } \"}\"\n```\n\n```text\nret_stmt ::= // empty \n                    {: /*your action for empty return statement*/ :}\n                 // Single return statement          \n                 | \"ret\":r TYPE:t\n                    {: /*your action for single return statement*/ :}\n\nPROC_DECL ::= \"proc\":p ret_stmt:r NAME:n\n                  \"(\" param_list:pl \")\"\n                  \"{\" { DECL } { STMT } \"}\"\n                   {: /*your action for procedure declaration statement*/ :}\n```\n\n========================================\n\nComments:\n- I have made a post regarding a problem I've encountered while trying your solution here: stackoverflow.com/questions/52862424/&hellip; Would you know how to solve this?","metadata":{"transformedAt":"2026-08-18T18:32:17.825Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":41,"estimatedTokens":319}}757{"id":"stack-6388932","source":"stackoverflow","questionId":6388932,"title":"Do production compilers use parser generators?","tags":["parsing","compiler-construction","parser-generator"],"text":"Title: Do production compilers use parser generators?\nTags: parsing, compiler-construction, parser-generator\nSource: Stack Overflow\n\nQuestion:\nI've heard that \"real compiler writers\" roll their own handmade parser rather than using parser generators. I've also heard that parser generators don't cut it for real-world languages. Supposedly, there are many special cases that are difficult to implement using a parser generator. I have my doubts about this:\n\n- Theoretically, a GLR parser generator should be able to handle most programming language designs (except maybe C++...)\n\n- I know of at least one production language that uses a parser generator: Ruby [1].\n\n- When I took my compilers class in school, we used a parser generator.\n\nSo my question: Is it reasonable to write a production compiler using a parser generator, or is using a parser generator considered a poor design decision by the compiler community?\n\n[1] https://github.com/ruby/ruby/blob/trunk/parse.y\n\n========================================\n\nTop Answer:\nI worked for a company for a few years where we were more or less writing compilers. We weren't concerned much with performance; just reducing the amount of work/maintenance. We used a combination of generated parsers + handwritten code to achieve this. The ideal balance is to automate the easy, repetitive parts with the parser generator and then tackle the hard stuff in custom functions.\n\n========================================\n\nComments:\n- real programmers use bread boards.\n- I thought they used butterflies xkcd.com/378\n- GLR parsers handle C++ just fine. Our tools use GLR to parse a variety of C++ dialects, and some other 30 languages. (See my bio for \"our tools\").\n- It's not really that difficult to write all parser rules for C++, even C++0x, and use parser generator, but you will never get the performance of handcrafted parser, even if you use best C++ parser generators. And performance in parsing C++ is extremely important.\n- @Gene Why is it that a handcrafted parser will beat a C++ parser generator? Considering that writing a hand-crafted parser is a lot of code, isn't it possible that the hand-crafted parser could be much slower?\n- @Matt: done both writing parsers by hand, writing parser generators and writing rules for them. Parser generators practically always create suboptimal code, it's not difficult to do better by hand, but since writing rules for parser generator is so much easier and faster I would do it first before resorting to hand-written parser.","metadata":{"transformedAt":"2026-08-18T18:32:17.825Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":31,"estimatedTokens":630}}758{"id":"stack-37274571","source":"stackoverflow","questionId":37274571,"title":"Which is more useful at an assembly level, 64 registers or three operand instructions?","tags":["c","assembly","compiler-construction"],"text":"Title: Which is more useful at an assembly level, 64 registers or three operand instructions?\nTags: c, assembly, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThis question is in the context of writing a C compiler for a 16 bit homebrew CPU.\n\nI have 12 bits of operand for ALU instructions (such as ADD, SUB, AND, etc.).\n\nI could give instructions three operands from 16 registers or two operands from 64 registers.\n\ne.g.\n\n```\nSUB A vs\n\n```\nSUB A Are sixteen registers, with three-operand instructions, more useful than 64 registers with two-operand instructions, to C compilers and their authors?\n\n========================================\n\nTop Answer:\nRegarding the amount of registers, in general I think most C can compile to good efficient machine code when only 16 general purpose registers are available (like AMD64). However, it might be beneficial to have a couple of registers dedicated for function arguments and some marked as volatile - meaning they can be used inside any function but could be clobbered by any called function. Increasing to 32 registers might be beneficial, but I doubt a lot will improve if you'd have 64 general purpose registers for a regular 16-bit CPU. You will have to save the original content of most registers you are going to use in your C function to the stack anyway. Limiting a function to only use 7 registers simultaneously (rather than 37) might still be more (stack) efficient to a C compiler, even when there are a lot more registers available. \n\nA lot depends on the C calling convention you will be using. Which registers are to be used to pass values from caller to callee, which registers are to be considered volatile, what is the cost of pushing to/popping from the stack, etc. You might win more by using a Register Window for managing your registers and stack usage across function calls. Sun Sparc for example has a register window of 8 completely \"local\" registers, 8 registers that are shared with the caller and 8 registers that will be shared with any callee function. (Furthermore 8 global registers can be addressed as well.) That way you don't have to worry about pushes to the stack, there will always be a single push of 16 registers for every function call simultaneously to changing the execution pointer and a 16 register pop for every return. Intel ia64 has something similar but with a configurable register window size.\n\nHowever, `SUB C,A,B` only has a slight advantage over `SUB A,B` when preserving intermediate results is really important (A needs to be preserved often) and a simple register to register copy is considerably expensive. This seems unlikely in most cases.\n\nAnd will you be using separate floating or fixed point registers?\n\n========================================\n\nCode:\n```text\nSUB A <- B - C  (registers r0-r15)\n```\n\n```text\nSUB A <- A - B  (registers r0-r63)\n```\n\n```text\nint32_t\n```\n\n```text\nmov\n```\n\n```text\nlea\n```\n\n```text\nSUB C,A,B\n```\n\n```text\nSUB A,B\n```\n\n========================================\n\nComments:\n- On just a first though (in x86 sorry only assembly I know). Most programs I've put through things like IDA usually use registers Eax through Edx so that's 4. Then you have Ebp and Esp so 6. Eip should not need the ALU. Eflags (again no need for ALU) ESI and EDI makes 8. So just from a first thought I don't think most programs use more then 16 registers. I may be missing a few but I think a good first sanity test for determining this would be looking at what gcc compiles and finding if it even uses more then 16 registers on the ALU.\n- Where are you going to code the addressing mode?\n- @WeatherVane It's RISC - loads and stores are explicit operations with their own opcode. It's a homebrew-cpu - the only addressing modes for loads and stores are 8 bit immediate offsets from zero, from PC or from another register.\n- 16 is usually plenty, and 2-operand instructions are slightly annoying to do codegen for.\n- if risc you want lots of registers and lots of register based instructions. x86 is cisc, dont use it as a design reference.\n- Have a look at something like Knuth's MMIX. Might give you plenty of ideas, even if it's a clean 64-bit 'RISC' ISA. For a 16-bit CPU, you might look at Atmel's 8-bit AVR, to see how it handles code density. Unless you *have* to implement a 16-bit ISA, there are plenty of well-designed 32-bit ISAs designed from the ground up. e.g., PowerPC, MIPS, etc.\n- Also - just because you have named registers, doesn't mean an architecture isn't free to use register files internally. That's what modern x86[-64] does.\n- Uniform 3-register operations can certainly simplify compilation significantly. More registers would potentially (on some subset of programs) reduce spilling, but you'd still have to do register spills anyway. 2-reg compilation is significantly more difficult and may involve more scratch registers and therefore more spilling, so the tradeoff is questionable. I'd stick to 3-address instructions.\n- P.S., if you really want more registers, consider using register file windows. It only adds a bit of complexity to the register allocation.\n- @BrettHale: pretty much every high performance out-of-order design renames the architectural registers onto a larger physical register file. Wikipedia says POWER1 was the first microprocessor to do it, in 1990. MIPS and Alpha also had renaming early on. e.g. Alpha 21264 renamed 32 architectural integer registers onto 80 physical regs. Renaming lets you break dependency chains when code reuses the same register with a write-only instruction. It's prob. not worth renaming if you don't implement OOO execution though.\n- Very interesting to see Fog distill his knowledge into an architectural concept. Be nice if he could formalize it to the point where simulators could be realized, like Knuth's MMIX. Along with cache / debug / fault registers, etc. It still appears to lack a definitive document...\n- @BrettHale: I haven't looked over the current version of the proposal. One of the recent posts on the discussion thread was that Agner is working on assembler and simulator support for it and stuff like that, but that he doesn't have much time to spend on that work. x86 might not last forever, and it would be really need if an \"open source\" architecture with vectors designed in from the start took over.","metadata":{"transformedAt":"2026-08-18T18:32:17.825Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":76,"estimatedTokens":1583}}759{"id":"stack-26059547","source":"stackoverflow","questionId":26059547,"title":"How to create AST parser which allows syntax errors?","tags":["algorithm","compiler-construction","language-agnostic","abstract-syntax-tree"],"text":"Title: How to create AST parser which allows syntax errors?\nTags: algorithm, compiler-construction, language-agnostic, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nFirst, what to read about parsing and building AST?\n\nHow to create parser for a language (like SQL) that will build an AST and allow syntax errors?\n\nFor example, for \"3+4*5\":\n\n```\n+\n / \\\n3 *\n / \\\n 4 5\n```\n\nAnd for \"3+4*+\" with syntax error, parser would guess that the user meant:\n\n```\n+\n / \\\n3 *\n / \\\n 4 +\n / \\\n ? ?\n```\n\nWhere to start?\n\nSQL:\n\n```\nSELECT_________________\n / \\ \\\n . FROM JOIN\n / \\ | / \\\na city_name people address ON\n |\n =______________\n / \\\n .____ .\n / \\ / \\\n p address_id a id\n```\n\n========================================\n\nTop Answer:\nThere are two things the parser could do:\n\n- Report the error and have the user try again.\n\n- Repair the error and proceed.\n\nGenerally speaking the first one is easier (and safer). There may not always be enough information for the parser to infer the intent when the syntax is wrong. Depending on the circumstances, it may be dangerous to proceed with a repair that makes the input syntactically correct but semantically wrong.\n\nI've written a few hand-rolled recursive descent parsers for little languages. When writing code to interpret the grammar rules explicitly (as opposed to using a parser-generator), it's easy to detect errors, because the next token doesn't fit the production rule. Generated parsers tend to spit out a simplistic \"expected $(TOKEN_TYPE) here\" message, which isn't always useful to the user. With a hand-written parser, it's often easy to give a more specific diagnostic message, but it can be time consuming to cover every case.\n\nIf your goal is the report the problem but to keep parsing (so that you can see if there are additional problems), you can put a special AST node in the tree at the point of the error. This keeps the tree from falling apart.\n\nYou then have to resync to some point beyond the error in order to continue parsing. As Ira Baxter mentioned in his answer, you might look for a token, like ';', that separates statements. The correct token(s) to look for depends on the language you're parsing. Another possibility is to guess what the user meant (e.g., infer an extra token or a different token at the point the error was detected) and then continue. If you encounter another syntax error within the next few tokens, you could backtrack, make a different guess, and try again.\n\n========================================\n\nCode:\n```text\n+\n / \\\n3   *\n   / \\\n  4   5\n```\n\n```text\n+\n / \\\n3   *\n   / \\\n  4   +\n     / \\\n    ?   ?\n```\n\n```text\nSELECT_________________\n   /           \\           \\\n  .           FROM        JOIN\n / \\           |         /    \\\na city_name  people   address  ON\n                                |\n                                =______________\n                               /               \\\n                              .____             .\n                             /     \\           / \\\n                            p  address_id     a  id\n```\n\n========================================\n\nComments:\n- ASTs always allow semantic errors, this is a syntax error.\n- Fixed type of error in question. Thanks)\n- Does your language have a way of delimiting statements?\n- They all \"allow\" syntax errors by virtue of it being unavoidable in practice. What you want is syntax error recovery/repair.\n- @IraBaxter I stand corrected. (Sorry, I deleted my original comment before seeing your reply.)\n- I'm trying to parse SQL.","metadata":{"transformedAt":"2026-08-18T18:32:17.825Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":110,"estimatedTokens":881}}760{"id":"stack-51922996","source":"stackoverflow","questionId":51922996,"title":"LLVM IR nested phi instruction","tags":["compiler-construction","llvm","llvm-ir"],"text":"Title: LLVM IR nested phi instruction\nTags: compiler-construction, llvm, llvm-ir\nSource: Stack Overflow\n\nQuestion:\nim working on a my own programming language. Im currently generating the code in LLVM IR. I got a question on nested If statement with phi. So lets say I have this in my language :\n\n```\nif n I generated this in llvm ir :\n\n```\n; If\n; ObjectIdentifier\n%15 = load %struct.Main*, %struct.Main** %2\n%16 = getelementptr inbounds %struct.Main, %struct.Main* %15, i32 0, i32 1\n%17 = load i32, i32* %16\n\n; VarValue\n%18 = alloca i32\nstore i32 0, i32* %18\n%19 = load i32, i32* %18\n\n; Lower\n%20 = icmp slt i32 %17, %19\n\nbr i1 %20, label %condIf1, label %condElse1 \n\ncondIf1:\n ; Call Method\n %21 = load %struct.Main*, %struct.Main** %2\n %22 = getelementptr inbounds %struct.Main, %struct.Main* %21, i32 0, i32 0\n ; Arguments\n ; VarValue\n %23 = alloca [6 x i8]\n store [6 x i8] c\"n 100\\00\", [8 x i8]* %34\n %35 = bitcast [8 x i8]* %34 to i8*\n\n %36 = call %struct.IO* @IO_print(%struct.IO* %33, i8* %35)\n\n br label %condEnd2\n\n condElse2:\n ; Call Method\n %37 = load %struct.Main*, %struct.Main** %2\n %38 = getelementptr inbounds %struct.Main, %struct.Main* %37, i32 0, i32 0\n ; Arguments\n ; VarValue\n %39 = alloca [2 x i8]\n store [2 x i8] c\"\\0a\\00\", [2 x i8]* %39\n %40 = bitcast [2 x i8]* %39 to i8*\n\n %41 = call %struct.IO* @IO_print(%struct.IO* %38, i8* %40)\n\n br label %condEnd2\n\n condEnd2:\n %42 = phi %struct.IO* [%36, %condIf2], [%41, %condElse2]\n\n br label %condEnd1\n\ncondEnd1:\n %43 = phi %struct.IO* [%25, %condIf1], [%43, %condElse1]\n```\n\nEverything compiled, but i get those errors :\n\n```\nPHI node entries do not match predecessors!\n %43 = phi %struct.IO* [ %25, %condIf1 ], [ %42, %condElse1 ]\n label %condElse1\n label %condEnd2 \n Instruction does not dominate all uses!\n %42 = phi %struct.IO* [ %36, %condIf2 ], [ %41, %condElse2 ]\n %43 = phi %struct.IO* [ %25, %condIf1 ], [ %42, %condElse1 ]\n```\n\nI can't figure out exactly what is the problem with the phi. Do you have any hint on how to solve this issue or use something else then phi ?\nThank you !\n\n========================================\n\nCode:\n```text\nif n < 0 then\n        print(\"n < 0\")\n    else\n        if 100 < n then\n            print(\"100\")\n        else\n            print(\"\\n\")\n```\n\n```text\n; If\n; ObjectIdentifier\n%15 = load %struct.Main*, %struct.Main** %2\n%16 = getelementptr inbounds %struct.Main, %struct.Main* %15, i32 0, i32 1\n%17 = load i32, i32* %16\n\n; VarValue\n%18 = alloca i32\nstore i32 0, i32* %18\n%19 = load i32, i32* %18\n\n; Lower\n%20 = icmp slt i32 %17, %19\n\nbr i1 %20, label %condIf1, label %condElse1 \n\ncondIf1:\n    ; Call Method\n    %21 = load %struct.Main*, %struct.Main** %2\n    %22 = getelementptr inbounds %struct.Main, %struct.Main* %21, i32 0, i32 0\n    ; Arguments\n    ; VarValue\n    %23 = alloca [6 x i8]\n    store [6 x i8] c\"n < 0\\00\", [6 x i8]* %23\n    %24 = bitcast [6 x i8]* %23 to i8*\n\n    %25 = call %struct.IO* @IO_print(%struct.IO* %22, i8* %24)\n\n    br label %condEnd1\n\ncondElse1:\n    ; If\n    ; VarValue\n    %26 = alloca i32\n    store i32 100, i32* %26\n    %27 = load i32, i32* %26\n\n    ; ObjectIdentifier\n    %28 = load %struct.Main*, %struct.Main** %2\n    %29 = getelementptr inbounds %struct.Main, %struct.Main* %28, i32 0, i32 1\n    %30 = load i32, i32* %29\n\n    ; Lower\n    %31 = icmp slt i32 %27, %30\n\n    br i1 %31, label %condIf2, label %condElse2     \n\n    condIf2:\n        ; Call Method\n        %32 = load %struct.Main*, %struct.Main** %2\n        %33 = getelementptr inbounds %struct.Main, %struct.Main* %32, i32 0, i32 0\n        ; Arguments\n        ; VarValue\n        %34 = alloca [8 x i8]\n        store [8 x i8] c\"n > 100\\00\", [8 x i8]* %34\n        %35 = bitcast [8 x i8]* %34 to i8*\n\n        %36 = call %struct.IO* @IO_print(%struct.IO* %33, i8* %35)\n\n        br label %condEnd2\n\n    condElse2:\n        ; Call Method\n        %37 = load %struct.Main*, %struct.Main** %2\n        %38 = getelementptr inbounds %struct.Main, %struct.Main* %37, i32 0, i32 0\n        ; Arguments\n        ; VarValue\n        %39 = alloca [2 x i8]\n        store [2 x i8] c\"\\0a\\00\", [2 x i8]* %39\n        %40 = bitcast [2 x i8]* %39 to i8*\n\n        %41 = call %struct.IO* @IO_print(%struct.IO* %38, i8* %40)\n\n        br label %condEnd2\n\n    condEnd2:\n        %42 = phi %struct.IO* [%36, %condIf2], [%41, %condElse2]\n\n    br label %condEnd1\n\ncondEnd1:\n    %43 = phi %struct.IO* [%25, %condIf1], [%43, %condElse1]\n```\n\n```text\nPHI node entries do not match predecessors!\n %43 = phi %struct.IO* [ %25, %condIf1 ], [ %42, %condElse1 ]\n label %condElse1\n label %condEnd2 \n Instruction does not dominate all uses!\n %42 = phi %struct.IO* [ %36, %condIf2 ], [ %41, %condElse2 ]\n %43 = phi %struct.IO* [ %25, %condIf1 ], [ %42, %condElse1 ]\n```\n\n```text\ncondEnd1:\n    %43 = phi %struct.IO* [%25, %condIf1], [%test, %condElse1]\n```\n\n```text\ncondEnd1\n```\n\n```text\ncondEnd1\n```\n\n```text\ncondIf1\n```\n\n```text\ncondEnd2\n```\n\n```text\ncondElse1\n```\n\n```text\nfoo\n```\n\n```text\nbar\n```\n\n```text\nfoo\n```\n\n```text\n%test\n```\n\n```text\n%condElse1\n```\n\n```text\n%condElse1\n```\n\n```text\n%test\n```\n\n```text\n%condElse1\n```\n\n```text\n%condElse1\n```\n\n```text\n%test\n```\n\n```text\n%condElse1\n```\n\n```text\n%condEnd2\n```\n\n```text\n%condEnd2\n```\n\n```text\ncondEnd1\n```\n\n```text\n%test\n```\n\n```text\nalloca\n```\n\n========================================\n\nComments:\n- My LLVM is a bit rusty :). Before I try to answer, why is the definition of %43 in the generated code and the error message different? are you sure this is not a typo\n- @knightrider Youre right. I was doing some test after the initial error message. I put it back like it should be\n- Thank you, your answer solved my issue. I decided to stay with phi and I manage to generate the good labels.","metadata":{"transformedAt":"2026-08-18T18:32:17.826Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":25,"totalLines":282,"estimatedTokens":1427}}761{"id":"stack-45411174","source":"stackoverflow","questionId":45411174,"title":"ANTLR4.7: rule XXX contains a closure with at least one alternative that can match an empty string'","tags":["compiler-errors","compiler-construction","antlr","antlr4"],"text":"Title: ANTLR4.7: rule XXX contains a closure with at least one alternative that can match an empty string'\nTags: compiler-errors, compiler-construction, antlr, antlr4\nSource: Stack Overflow\n\nQuestion:\nI am trying to create a grammar to match content like below:\n\n(For a simple grammar to repro this issue please see **ADD 1**)\n\n```\n[Defines]\n INF_VERSION = 0x00010005\n BASE_NAME = WebServer\n FILE_GUID = 99E87DCF-6162-40c5-9FA1-32111F5197F7\n MODULE_TYPE = SEC\n UEFI_SPECIFICATION_VERSION = 0x00010005\n```\n\n**The `UEFI_SPECIFICATION_VERSION = 0x00010005` part is optional.**\n\n(for brevity, I omitted some of the grammar).\n\nMy grammar 1 looks like this:\n\n```\ndefines : '[Defines]'\n define_statement+\n ;\n\ndefine_statement : 'INF_VERSION' EQ SpecVersion_VersionVal \n | 'BASE_NAME' EQ BaseName\n | 'FILE_GUID' EQ RegistryFormatGUID\n | 'MODULE_TYPE' EQ Edk2ModuleType\n | ('UEFI_SPECIFICATION_VERSION' EQ SpecVersion_VersionVal)?\n ;\n```\n\nANTLR 4.7 reports this error:\n\n message: 'rule defines contains a closure with at least one\n alternative that can match an empty string'\n\nBut if I changed grammar like this:\n\n```\ndefines : '[Defines]'\n define_statement+\n | ('UEFI_SPECIFICATION_VERSION' EQ SpecVersion_VersionVal)? // The error is gone.\n\nMy question is, what does the `closure` mean? Which part is the `closure`? The `define_statement`?\n\nAfter I move the potentially empty alternative, the `defines` rule can alternate between `'[Defines]' define_statement+` and `('UEFI_SPECIFICATION_VERSION' EQ SpecVersion_VersionVal)?`, which means `defines` can **still** match empty string. How could the error be gone?\n\n### ADD 1\n\nTo make things more clear, I repro this error with a simplified grammar:\n\n```\ngrammar test;\n\nrule : alternate+; // If I use `+` or `*` at `HERE`, ANTLR will report an **error**:\n\n 'rule rule contains a closure with at least one alternative that can\n match an empty string'\n\nIf I use `?` at `HERE`, ANTLR will report a **warning**:\n\n 'rule rule contains an optional block with at least\n one alternative that can match an empty string'\n\nI am still not sure why.\n\n### ADD 2\n\nEach of the `alternate` WILL be a child node of `rule`, so if `alternate` can be empty string, then it is logically possible to lead to endless child nodes for `rule`. So I guess this may explain why ANTLR forbids me to do that with `alternate+` or `alternate*`. But if it is with `alternate?`, at most there will be **one** child node. It's only a performance issue. So ANTLR just generate a warning.\n\n========================================\n\nTop Answer:\nJust add my solution. Credit goes to @JLH.\n\nThe important thing to get is to have **2 separate rules** for lines with **different natures**.\n\n- `linesGroup1_Defines`\n\n- `linesGroup2_Defines`.\n\nThis way, the `optional nature` of a line can be reached through `|` (choice) instead of `?`(optional).\n\n```\ngrammar inf;\n\nstart : configSections;\n\nconfigSections: configSection+\n EOF;\n\nconfigSection: section_Defines\n | bSection\n ;\n\nsection_Defines : '[Defines]'\n sectionLine_Defines*;\n\nsectionLine_Defines : linesGroup1_Defines | linesGroup2_Defines;\n\nlinesGroup1_Defines : 'INF_VERSION' EQ SpecVersion_VersionVal \n | 'BASE_NAME' EQ BaseName\n | 'FILE_GUID' EQ RegistryFormatGUID\n | 'MODULE_TYPE' EQ Edk2ModuleType\n ;\nlinesGroup2_Defines : 'UEFI_SPECIFICATION_VERSION' EQ SpecVersion_VersionVal\n | 'PI_SPECIFICATION_VERSION' EQ SpecVersion_VersionVal\n ;\n\nbSection : '[b]'\n SectionLine_b+;\n```\n\n(Some necessary token definitions are omitted for brevity)\n\nhttps://i.sstatic.net/DwGfq.png\n\n### ADD 1\n\nOn a second thought, with the above solution, I didn't cover the semantic that `linesGroup1_Deines` is mandatory and `linsGroup2_Defines` is optional. **Actually both are optional now**. It can accept input with only optional lines like below:\n\n```\n[Defines]\n UEFI_SPECIFICATION_VERSION = 0x00010005\n```\n\nI am not sure if this semantic **can/should** be covered in the grammar. Maybe I need to further refine it.\n\n========================================\n\nCode:\n```text\n[Defines]\n  INF_VERSION                    = 0x00010005\n  BASE_NAME                      = WebServer\n  FILE_GUID                      = 99E87DCF-6162-40c5-9FA1-32111F5197F7\n  MODULE_TYPE                    = SEC\n  UEFI_SPECIFICATION_VERSION     = 0x00010005\n```\n\n```text\ndefines : '[Defines]'\n         define_statement+\n         ;\n\ndefine_statement  : 'INF_VERSION' EQ SpecVersion_VersionVal \n                  | 'BASE_NAME' EQ BaseName\n                  | 'FILE_GUID' EQ RegistryFormatGUID\n                  | 'MODULE_TYPE' EQ Edk2ModuleType\n                  | ('UEFI_SPECIFICATION_VERSION' EQ SpecVersion_VersionVal)?\n                  ;\n```\n\n```text\ndefines : '[Defines]'\n         define_statement+\n         | ('UEFI_SPECIFICATION_VERSION' EQ SpecVersion_VersionVal)? // <<< HERE\n         ;\n\ndefine_statement  : 'INF_VERSION' EQ SpecVersion_VersionVal\n                  | 'BASE_NAME' EQ BaseName\n                  | 'FILE_GUID' EQ RegistryFormatGUID\n                  | 'MODULE_TYPE' EQ Edk2ModuleType\n```\n\n```text\ngrammar test;\n\nrule : alternate+; // <<<<< HERE\nalternate : '1'?;\n```\n\n```text\nUEFI_SPECIFICATION_VERSION     = 0x00010005\n```\n\n```text\nclosure\n```\n\n```text\nclosure\n```\n\n```text\ndefine_statement\n```\n\n```text\ndefines\n```\n\n```text\n'[Defines]' define_statement+\n```\n\n```text\n('UEFI_SPECIFICATION_VERSION' EQ SpecVersion_VersionVal)?\n```\n\n```text\ndefines\n```\n\n```text\n+\n```\n\n```text\n*\n```\n\n```text\nHERE\n```\n\n```text\n?\n```\n\n```text\nHERE\n```\n\n```text\nalternate\n```\n\n```text\nrule\n```\n\n```text\nalternate\n```\n\n```text\nrule\n```\n\n```text\nalternate+\n```\n\n```text\nalternate*\n```\n\n```text\nalternate?\n```\n\n```text\ndefines : '[Defines]'\n         define_statement+\n         ;\n\ndefine_statement  : 'INF_VERSION' EQ SpecVersion_VersionVal \n                  | 'BASE_NAME' EQ BaseName\n                  | 'FILE_GUID' EQ RegistryFormatGUID\n                  | 'MODULE_TYPE' EQ Edk2ModuleType\n                  | ('UEFI_SPECIFICATION_VERSION' EQ SpecVersion_VersionVal)?\n                  ;\n```\n\n```text\ndefine_statement  : 'INF_VERSION' EQ SpecVersion_VersionVal \n                  | 'BASE_NAME' EQ BaseName\n                  | 'FILE_GUID' EQ RegistryFormatGUID\n                  | 'MODULE_TYPE' EQ Edk2ModuleType\n                  | \n                  ;\n```\n\n```text\ndefine_statement  : 'INF_VERSION' EQ SpecVersion_VersionVal \n                  | 'BASE_NAME' EQ BaseName\n                  | 'FILE_GUID' EQ RegistryFormatGUID\n                  | 'MODULE_TYPE' EQ Edk2ModuleType\n                  ;\n```\n\n```text\ndefines : '[Defines]'\n         define_statement+\n         | ('UEFI_SPECIFICATION_VERSION' EQ SpecVersion_VersionVal)? // <<< HERE\n         ;\n```\n\n```text\ndefines : ('[Defines]' define_statement+)?\n\ndefine_statement  : 'INF_VERSION' EQ SpecVersion_VersionVal \n                  | 'BASE_NAME' EQ BaseName\n                  | 'FILE_GUID' EQ RegistryFormatGUID\n                  | 'MODULE_TYPE' EQ Edk2ModuleType\n                  | 'UEFI_SPECIFICATION_VERSION' EQ SpecVersion_VersionVal\n```\n\n```text\ngrammar Uefi;\ndefines : '[Defines]' statement+ ;\nstatement : define_statement | uefi_statement ;      \nuefi_statement : 'UEFI_SPECIFICATION_VERSION' EQ SpecVersion_VersionVal ;\ndefine_statement  : 'INF_VERSION' EQ SpecVersion_VersionVal \n                  | 'BASE_NAME' EQ BaseName\n                  | 'FILE_GUID' EQ RegistryFormatGUID\n                  | 'MODULE_TYPE' EQ Edk2ModuleType\n                  ;\n// DUMMY VALUES               \nSpecVersion_VersionVal : '0x00010005';\nBaseName : 'WebServer';\nRegistryFormatGUID : '99E87DCF-6162-40c5-9FA1-32111F5197F7';\nEdk2ModuleType : 'SEC';\nEQ : '=';\nWS : [ \\t\\r\\n]+ -> skip;\n```\n\n```text\n?\n```\n\n```text\n|\n```\n\n```text\n[Defines]\n```\n\n```text\ndefine_statement\n```\n\n```text\n[Defines]\n```\n\n```text\ngrammar inf;\n\nstart : configSections;\n\nconfigSections: configSection+\n                EOF;\n\nconfigSection: section_Defines\n             | bSection\n             ;\n\nsection_Defines : '[Defines]'\n                 sectionLine_Defines*;\n\nsectionLine_Defines  : linesGroup1_Defines | linesGroup2_Defines;\n\nlinesGroup1_Defines : 'INF_VERSION' EQ SpecVersion_VersionVal \n           | 'BASE_NAME' EQ BaseName\n           | 'FILE_GUID' EQ RegistryFormatGUID\n           | 'MODULE_TYPE' EQ Edk2ModuleType\n           ;\nlinesGroup2_Defines : 'UEFI_SPECIFICATION_VERSION' EQ SpecVersion_VersionVal\n              | 'PI_SPECIFICATION_VERSION' EQ SpecVersion_VersionVal\n              ;\n\n\nbSection : '[b]'\n           SectionLine_b+;\n```\n\n```text\n[Defines]\n  UEFI_SPECIFICATION_VERSION     = 0x00010005\n```\n\n```text\nlinesGroup1_Defines\n```\n\n```text\nlinesGroup2_Defines\n```\n\n```text\noptional nature\n```\n\n```text\n|\n```\n\n```text\n?\n```\n\n```text\nlinesGroup1_Deines\n```\n\n```text\nlinsGroup2_Defines\n```\n\n========================================\n\nComments:\n- Can't answer the question itself, but `define_statement` *can* match an empty string in your example, which looks pointless to me. I'd guess \"closure\" is `define_statement+`.\n- To think, in your grammar `define_statement+` can match an arbitrary number of empty strings, so I guess that's why ANLTR4 complains.\n- Yes, I agree with you . But I did that because the `UEFI_SPECIFICATION_VERSION = 0x00010005` part is optional. Maybe I need to re-design the grammar.\n- About `UEFI...`: then your grammar seems completely wrong. What `defines` would match as you wrote is e.g. `[Defines]` plus 10 `INF_VERSION` lines. Sounds like you want something different.\n- Thanks. But I don't understand why the `defines` rule in the second grammar *is guaranteed to match something*? `defines` rule still has 2 alternatives, one is non-empty and the other `UEFI...` string can **still** be empty. Please see my updated question.\n- `[Defines]`, as it is listed in single quotes, is what is referred to as an implied lexer token. If present, it will be lexed. As part of a parser rule, the rule will be parsed. That may be the only thing there, depending on the scenarios I laid out, but if present it'll be parsed, hence no error message. I've been through this before with grammars where almost everything is optional. It's just a warning -- only we as language designers can say whether it's okay or not.\n- In my scenario, the `[Defines]` is mandatory, but **only** the `UEFI...` string line is optional. I am still trying to figure out a grammar for this.\n- @smwikipedia If that's the case, then your original grammar was spot on. Ignore the warning and just realize when you make your Listener or Visitor there may be nothing there to parse when you do your overrides, so check for content in the context before trying to parse something that might not be there. Yours is a very common pattern. Well done.\n- But that's an error not a warning. I am using ANTLR 4.7. Please see my ADD 1 and 2.\n- @smwikipedia You're right I didn't read that closely enough. The \"?\" makes it optional and that's all. When I read your original question about \"warning\" I had deja vu, been there many times. Let me take a bit of a closer look, do you still need help or are you good to go now? So the DEFINES section is mandatory (the header) and only optional one is the UEFI thing, and if present, there can be only one?\n- Yes, the [Defines] is mandatory. Only the UEFI thing is optional. And if it is present, there can be only one.\n- Let us continue this discussion in chat.\n- btw, have you tried when there's only the header `[DEFINES]`. Just curious. I have turned off my computer so I can only try it out tomorrow.\n- As I just tried it, in case of only the header `[DEFINES]` exists, simply change the `statement+` to `statement*` will do the trick.","metadata":{"transformedAt":"2026-08-18T18:32:17.826Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":44,"totalLines":425,"estimatedTokens":2902}}762{"id":"stack-6299406","source":"stackoverflow","questionId":6299406,"title":"Code generation for compiler in Haskell","tags":["haskell","code-generation","compiler-construction"],"text":"Title: Code generation for compiler in Haskell\nTags: haskell, code-generation, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am writing a compiler for a small imperative language. The target language is Java bytecode, and the compiler is implemented in Haskell.\n\nI've written a frontend for the language - i.e I have a lexer, parser and typechecker. I'm having trouble figuring out how to do code generation. \n\nI keep a data structure representing the stack of local variables. I can query this structure with the name of a local variable and get its position in the stack. This data structure is passed around as I walk the syntax tree, and variables are popped and pushed as I enter and exit new scopes.\n\nWhat I having trouble figuring out is how to emit the bytecode. Emitting strings at terminals and concatenating them at higher levels seems like a poor solution, both clarity- and performance-wise.\n\n**tl;dr** How do I emit bytecode while waling the syntax tree?\n\n========================================\n\nTop Answer:\nIf you haven't done this before, you can do it in small passes:\n 1) for every statement produce some byte code (with out properly addressed memory locations)\n 2) after that is done, if you have looping, gotos, etc, put in the real addresses (you know them\n now that you have it all layed out)\n 3) replace the memory fetches/stores with the correct locations\n 4) dump it out to a JAR file\n\nNote that this is very simplified and doesn't try to do any performance optimisation. It will give you a functional program which will execute. This also assumes you know the codes for the JVM (which is where I am presuming you are going to execute it.)\n\nTo start, just have a subset of the language which does sequential arithmetic statements. This will allow you to figure out how to map variable memory locations to statements via the parse tree. Next add some looping to get jumps to work. Similarly add conditionals. Finally, you can add the final parts of your language.\n\n========================================\n\nCode:\n```text\ndata LIRInst = LIRRegAssignInst LIRReg LIRExpr\n             | LIRRegOffAssignInst LIRReg LIRReg LIRSize LIROperand\n             | LIRStoreInst LIRMemAddr LIROperand\n             | LIRLoadInst LIRReg LIRMemAddr\n             | LIREnterInst LIRInt\n             | LIRJumpLabelInst LIRLabel\n             | LIRIfInst LIRRelExpr LIRLabel LIRLabel -- false, then true\n             | LIRCallInst LIRLabel LIRLabel -- method label, return label\n             | LIRCalloutInst String\n             | LIRRetInst [LIRLabel] String -- list of successors, and the name of the method returning from\n             | LIRLabelInst LIRLabel\n             deriving (Show, Eq, Typeable)\n```\n\n```text\nnewtype LIRTranslator a = LIRTranslator\n    { runLIR :: Namespace -> (a, Namespace) }\n\ninstance Monad LIRTranslator where\n    return a = LIRTranslator (\\s -> (a, s))\n    m >>= f = LIRTranslator (\\s ->\n        let (a, s') = runLIR m s\n        in runLIR (f a) s')\n```\n\n```text\ndata Namespace = Namespace\n    { temp         :: Int                       -- id's for new temporaries\n    , labels       :: Int                       -- id's for new labels\n    , scope        :: [(LIRLabel, LIRLabel)]    -- current program scope\n    , encMethod    :: String                    -- current enclosing method\n    , blockindex   :: [Int]                     -- index into the SymbolTree\n    , successorMap :: Map.Map String [LIRLabel]\n    , ivarStack    :: [(LIRReg, [CFGInst])]     -- stack of ivars (see motioned code)\n    }\n```\n\n```text\n-- |Increment our translator's label counter\nincLabel :: LIRTranslator Int\nincLabel = LIRTranslator (\\ns@(Namespace{ labels = l }) -> (l, ns{ labels = (l+1) }))\n```\n\n```text\ntranslateBlock :: SymbolTree -> ASTBlock -> LIRTranslator [LIRInst]\ntranslateBlock st (DecafBlock _ [] _) = withBlock (return [])\ntranslateBlock st block =\n    withBlock (do b <- getBlock\n                  let st' = select b st\n                  declarations <- mapM (translateVarDeclaration st') (blockVars block)\n                  statements <- mapM (translateStm st') (blockStms block)\n                  return (concat declarations ++ concat statements))\n```\n\n```text\n-- | Given a SymbolTree, Translate a single DecafMethodStm into [LIRInst]\ntranslateStm st (DecafMethodStm mc _) =\n    do (instructions, operand) <- translateMethodCall st mc\n       final <- motionCode instructions\n       return final\n```\n\n```text\ntranslateMethodPrologue :: SymbolTree -> DecafMethod -> LIRTranslator [LIRInst]\ntranslateMethodPrologue st (DecafMethod _ ident args _ _) =\n    do let numRegVars = min (length args) 6\n           regvars = map genRegVar (zip [LRDI, LRSI, LRDX, LRCX, LR8, LR9] args)\n       stackvars <- mapM genStackVar (zip [1..] (drop numRegVars args))\n       return (regvars ++ stackvars)\n  where\n    genRegVar (reg, arg) =\n        LIRRegAssignInst (symVar arg st) (LIROperExpr $ LIRRegOperand reg)\n    genStackVar (index, arg) =\n        do let mem = LIRMemAddr LRBP Nothing ((index + 1) * 8) qword -- ^ [rbp] = old rbp; [rbp + 8] = ret address; [rbp + 16] = first stack param\n                                  return $ LIRLoadInst (symVar arg st) mem\n```\n\n```text\nState Monad\n```\n\n========================================\n\nComments:\n- This isn't worth a full answer, and obviously involves a very different style of language, but there is a compiler written in Haskell you may be familiar with whose source code you could look at for inspiration.\n- Does your intermediate representation map one to one to jvm opcodes? If not, then that's a place to start: create one or more data types to represent (a subset of) the JVM opcodes that you are targeting. Then walk your higher-level IR and create the low-level JVM-centric IR.\n- Just a question: Where did you get the JVM assumption? (Someone else assumed it too, and I can't find it for the life of me)\n- @mathepic: Second sentence of the question, \"The target language is Java bytecode (...)\".\n- @camccann Ah, okay. I must be blind :D","metadata":{"transformedAt":"2026-08-18T18:32:17.826Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":122,"estimatedTokens":1506}}763{"id":"stack-21025099","source":"stackoverflow","questionId":21025099,"title":"LLVM alloca causes stack overflow on while statement","tags":["compiler-construction","llvm","llvm-ir"],"text":"Title: LLVM alloca causes stack overflow on while statement\nTags: compiler-construction, llvm, llvm-ir\nSource: Stack Overflow\n\nQuestion:\nI am implementing a frontend compiler for a toy language targeting LLVM-IR and I encounter a stack overflow when running compiled `while` statements:\n\nFor example, this code should run forever but our compiled version stack-overflows after some time.\n\n```\ndef run(): Void = {\n i = 0;\n while(true) {\n i = i + 1;\n }\n}\n```\n\nAnd here is the comipled LLVM-IR:\n\n```\ndefine i32 @run() nounwind ssp {\n ; i = 0\n %i = alloca i32, align 4\n %1 = alloca i32, align 4\n store i32 0, i32* %1, align 4\n %2 = load i32* %1, align 4\n store i32 %2, i32* %i, align 4\n br label %3\n\n; : %3\n ; while(true)\n ; Generated by compileExpression(condition)\n %4 = alloca i1, align 4\n store i1 true, i1* %4, align 4\n %5 = load i1* %4, align 4\n br i1 %5, label %6, label %11\n\n; : %6\n ; i = i + 1\n ; Generated by compileExpression(body)\n %7 = load i32* %i, align 4\n %8 = alloca i32, align 4\n store i32 1, i32* %8, align 4\n %9 = load i32* %8, align 4\n %10 = add nsw i32 %7, %9\n store i32 %10, i32* %i, align 4\n br label %3\n\n; : %11\n %12 = load i32* %i, align 4\n ret i32 %12\n}\n```\n\nWe think our problem comes from every `alloca` that are not released because we are still in the same function.\n\nLLVM Documentation:\n\n 'alloca'd memory is automatically released when the function returns.\n\nHow should we compile the while loop?\n\nCan we avoid this problem?\n\n========================================\n\nTop Answer:\nUse the mem2reg pass to conver the allocas to register values. Register values are released when they reach there last use.\n\n========================================\n\nCode:\n```text\ndef run(): Void = {\n    i = 0;\n    while(true) {\n        i = i + 1;\n    }\n}\n```\n\n```text\ndefine i32 @run() nounwind ssp {\n    ; i = 0\n    %i = alloca i32, align 4\n    %1 = alloca i32, align 4\n    store i32 0, i32* %1, align 4\n    %2 = load i32* %1, align 4\n    store i32 %2, i32* %i, align 4\n    br label %3\n\n; <label>: %3\n    ; while(true)\n    ; Generated by compileExpression(condition)\n    %4 = alloca i1, align 4\n    store i1 true, i1* %4, align 4\n    %5 = load i1* %4, align 4\n    br i1 %5, label %6, label %11\n\n; <label>: %6\n    ; i = i + 1\n    ; Generated by compileExpression(body)\n    %7 = load i32* %i, align 4\n    %8 = alloca i32, align 4\n    store i32 1, i32* %8, align 4\n    %9 = load i32* %8, align 4\n    %10 = add nsw i32 %7, %9\n    store i32 %10, i32* %i, align 4\n    br label %3\n\n; <label>: %11\n    %12 = load i32* %i, align 4\n    ret i32 %12\n}\n```\n\n```text\nwhile\n```\n\n```text\nalloca\n```\n\n```text\nalloca\n```\n\n```text\nalloca\n```\n\n```text\nload\n```\n\n```text\nadd\n```\n\n```text\nstore\n```\n\n```text\nalloca\n```\n\n```text\nload\n```\n\n```text\nstore\n```\n\n```text\nphi\n```\n\n```text\nalloca\n```\n\n```text\nstore\n```\n\n```text\ndefine i32 @run() nounwind ssp {\n    ; i = 0\n    %i = alloca i32, align 4\n    %1 = alloca i32, align 4\n    store i32 0, i32* %1, align 4\n    %2 = load i32* %1, align 4\n    store i32 %2, i32* %i, align 4\n    %3 = alloca i1, align 4\n    store i1 true, i1* %3, align 4\n    %4 = alloca i32, align 4\n    br label %whilecond\n\nwhilecond:\n    ; while(true)\n    ; Generated by compileExpression(condition)\n    %5 = load i1* %3, align 4\n    br i1 %5, label %whilebody, label %whileexit\n\nwhilebody:\n    ; i = i + 1\n    ; Generated by compileExpression(body)\n    %6 = load i32* %i, align 4\n    store i32 1, i32* %4, align 4\n    %7 = load i32* %4, align 4\n    %8 = add nsw i32 %6, %7\n    store i32 %8, i32* %i, align 4\n    br label %whilecond\n\nwhileexit:\n    %9 = load i32* %i, align 4\n    ret i32 %9\n}\n```\n\n```text\ndefine i32 @run() #0 {\n       br label %whilecond\n\nwhilecond:                                        ; preds = %whilebody, %0\n       %i.0 = phi i32 [ 0, %0 ], [ %1, %whilebody ]\n       br i1 true, label %whilebody, label %whileexit\n\nwhilebody:                                        ; preds = %whilecond\n       %1 = add nsw i32 %i.0, 1\n       br label %whilecond\n\nwhileexit:                                        ; preds = %whilecond\n       ret i32 %i.0\n}\n```\n\n========================================\n\nComments:\n- Will this work when while statements have bigger conditions/body expressions ?\n- Yes, it works at the ir level so just takes longer with more instructions.\n- We tried `opt -S -mem2reg test.ll -o test.ll` we still have some `alloca` in the body, so we still have a stack overflow.","metadata":{"transformedAt":"2026-08-18T18:32:17.826Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":227,"estimatedTokens":1103}}764{"id":"stack-4233630","source":"stackoverflow","questionId":4233630,"title":"Unit testing for a compiler output","tags":["unit-testing","compiler-construction","haskell"],"text":"Title: Unit testing for a compiler output\nTags: unit-testing, compiler-construction, haskell\nSource: Stack Overflow\n\nQuestion:\nAs part of a university project, we have to write a compiler for a toy language. In order to do some testing for this, I was considering how best to go about writing something like unit tests. As the compiler is being written in haskell, Hunit and quickcheck are both available, but perhaps not quite appropriate.\n\nHow can we do any kind of non-manual testing?\nThe only idea i've had is effectively compiling to haskell too, seeing what the output is, and using some shell script to compare this to the output of the compiled program - this is quite a bit of work, and isn't too elegant either. \n\nThe unit testing is to help us, and isn't part of assessed work itself.\n\n========================================\n\nTop Answer:\nThe unit tests shall test small piece of code, typically one class or one function. The lexical and semantic analysis will each have their unit tests. The Intermediate Represetation generator will also have its own tests. \n\nA unit test covers a simple test case: it invokes the function to be unit tested in a controlled environment and verify (assert) the result of the function execution. A unit test usually test one behavior only and has the following structure, called AAA : \n\n- Arrange: create the environment the function will be called in\n\n- Act: invoke the function\n\n- Assert: verify the result\n\n========================================\n\nCode:\n```text\ngrep\n```\n\n```text\nexpect\n```\n\n========================================\n\nComments:\n- You don't need to compile to Haskell -- you can also just write a simple reference implementation of an interpreter.","metadata":{"transformedAt":"2026-08-18T18:32:17.826Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":40,"estimatedTokens":428}}765{"id":"stack-13202808","source":"stackoverflow","questionId":13202808,"title":"Compiler Design : Is \"variable not declared\" a syntactic error or semantic error?","tags":["parsing","compiler-construction","semantics","syntactic-sugar","typechecking"],"text":"Title: Compiler Design : Is \"variable not declared\" a syntactic error or semantic error?\nTags: parsing, compiler-construction, semantics, syntactic-sugar, typechecking\nSource: Stack Overflow\n\nQuestion:\nIs such type of an error produced during type checking or when input is being parsed?\nUnder what type should the error be addressed?\n\n========================================\n\nTop Answer:\nThe program conforms to the language grammar, so it is syntactically correct. A language grammar doesn't contain any statements like 'the identifier must be declared', and indeed doesn't have any way of doing so. An attempt to build a two-level grammar along these lines failed spectacularly in the Algol-68 project, and it has not been attempted since to my knowledge.\n\nThe *meaning,* if any, of each is a *semantic* issue. Frank deRemer called issues like this 'static semantics'.\n\n========================================\n\nCode:\n```text\n(a)-b;\n```\n\n```text\n#include <stdio.h>\n\n#if TYPEDEF\ntypedef double a;\n#else\ndouble a = 3.0;\n#endif\n\nint main() {\n  int b = 3;\n  printf(\"%g\\n\", (a)-b);\n  return 0;\n}\n```\n\n```text\n<block>Hello, world</blob>\n```\n\n```text\nThe sheep is in the meadow.\nThe sheep are in the meadow.\n```\n\n```text\nThe cow is in the meadow.\n(*) The cow are in the meadow.\n```\n\n```text\nThe caribou are in the meadow.\nThe antelope are in the meadow.\nThe buffalo are in the meadow.\n```\n\n```text\n(*) The mouse are in the meadow.\n(*) The bird are in the meadow.\n```\n\n```text\na\n```\n\n```text\na\n```\n\n```text\nsheep\n```\n\n```text\na\n```\n\n```text\n(a)-b\n```\n\n```text\na\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\na\n```\n\n========================================\n\nComments:\n- It depends on the language I think. For a statically-typed language (e.g. C, Scala), I would consider it a Syntax/\"Syntactic\" Error and expect it to happen during compilation; in a language with dynamic variable bindings (e.g. JavaScript, Ruby), I would consider it a Semantic error and expect it to happen during runtime. In any case, I voted \"Not Constructive\".\n- So in statically typed language, type checking is done during parsing?\n- Parsing and Type-Checking really should be considered two *different* phases of the entire Compilation process. However, this will depend upon implementation; it is imaginable - ick! - that scoping rules are enforced during the parsing. Type-Checking is more than just \"scope checking\": not only must the binding be defined (perhaps even assigned a value), but the types must be compatible.\n- Oops, clarification on my first comment: I would classify \"variable not declared\" as Syntax, while I could say \"incompatible type\" is Semantic in a static environment.\n- So far I've learnt that syntax analysis cannot check for declarations. So if this error is caught by semantic analysis why is it a syntax error? Also, isn't the separation between syntax and semantic analysis just logical?\n- Good retort - I have no solid justification other than it is how it fits *my* classification. (Errors that occur due to invalid grammar are clearly Syntax errors, but I do not feel comfortable making a stronger assertion for errors generated by code that conforms to the grammar, even if logically unsound and catchable early on.)\n- Why do you think syntax analysis cannot check for declarations? A context free grammar cannot do the check, but syntax analysis is more than just a CFG. I would definitely called an undeclared variable a syntax error *in languages which requrie variables to be declared*.\n- Huhh, interesting question. I don't agree with whoever voted to close it, this is programming related and specific. (By the way, I was stumbling upon the exact same problem when designing my scripting language - I haven't yet finished it, but I found that this is easier to implement as a \"semantic\", run-time type of error.)\n- Hey @rici can you give an example where I can use any other grammar than CFG in Syntax analysis. From what I've studied if such additions are made to CFG, compiler might allow something that is not acceptable by the language.\n- @H2CO3 I also think that it would be beneficial if type checking is done at run time for scripting languages but what about imperative languages? And could you clarify one thing, if language is statically typed will semantic errors would be produced under compile time errors? From lower classes I've been taught that there are basically two types of errors : compile time(syntax error) and runtime (logical). So where does semantic errors fit in?\n- @arg21 as far as I'm concerned, this is not strictly a syntax error - if I were to implement this for a statically typed language, like C, then I would **not** put the check into the parser (because the parser is incapable of checking this), rather into the code generator (the logic that walks the AST and turns it into assembly). So in my opinion, it lies between syntax and semantic errors - it's a syntax-related error that can anly be checked by performing semantic analysis. If we consider a primitive scripting language, where the AST is directly executed without compilation to bytecode\n- and without JIT, then it's the evaluator/executor function that walks the AST and finds the undeclared variable - in this case, it will be a runtime error. The difference lies between the AST-walk routine being in different parts of the program lifecycle (compilation time and runtime) should the language be a scripting or a compiled one.\n- @arg21: I didn't say that you should use something other than a CFG. I said that the CFG was not the totality of the syntactic analysis. For example, it's normal (and in C/C++ and many other languages necessary) to use a symbol table, without which it is impossible to correctly parse the language.\n- I also agree that it is a semantic error but but is there any classification in a language (like in C,Java) between syntactic and semantic errors produced at compile time or are they just produced as a list of errors?\n- I think that's an internal implementation detail. GCC, for example, seems to just report a list of errors without taking into account if they are syntactic or semantic.\n- Thanks Alex that helps a lot. I used to think that semantic errors were only reported at runtime. My bad.\n- That's the case with most latently typed languages--also known as dynamic languages.\n- I think that when the C++ (or whatever) compiler creates the AST (abstract syntax tree) then it finds out that the variable does not exist, so it treats such things as syntax error. In C++ there are some situations where a type or a variable name are both valid things at a given point. There are others where only type is valid, so if you give a name of a variable or a mistyped name then you will have syntax error. I still consider not finding a symbol a semantic error, but I understand the rationale behind the \"Syntax error\" message in such cases.\n- I don't think you can write a C++ compiler without taking account of declarations while building the AST. See my answer for a simple case in C, but there are much more complicated issues in C++.\n- @rici well, why couldn't you? I think you're confusing the abstract syntax tree with the abstract semantic graph.\n- How do you draw an AST for (a)-b, then? It fits into two completely different productions in the grammar.\n- @rici `SUBTRACT(EXPRESSION(TERM a), TERM b)`, if that's what you mean.\n- What if it's `CAST(TYPE a, UNARY_MINUS(TERM b))` ?\n- @rici Then it's just `CAST(TYPE a, UNARY_MINUS(term b))`. And that's what the AST will show. There'll be an error when *trying to evalue this.*\n- How will the AST show that if you don't know what `a` is when you parse it? And it's not an evaluation error: it's perfectly legal.\n- @rici the AST can contain an undefined type, why not? It's just that the compiler won't know what it means when trying to evaluate it.\n- I think you're missing the point. `(a)-b` can be either a SUBTRACT or a CAST (but not both), depending on whether `a` is a type or a variable. The parser cannot build the AST (which requires knowing whether to insert a CAST or a SUBTRACT) unless it knows what `a` is. So if it doesn't know, the parse must fail.\n- @rici Now I get what you meant. Yes, this is absolutely correct. I didn't consider this type of ambiguity.\n- let us continue this discussion in chat\n- So are you saying though the **syntax** is ill formed (syntactically incorrect not semantically), error will be caught during semantic analysis due to incapability of parser and it will be considered as syntax error?\n- @arg21: edited my answer to resopnd, rather than writing a huge long comment.\n- In your example of (a)-b what will happen if cast is not possible (if a is a type) ? Will it produce a compile time syntax error due to wrong semantics? Also after studying few basic things about YACC, I get what you mean by parser not being restricted to CFG.\n- @arg21, C++ has a much wider repertoire of possible bad casts. You could experiment with c++ compilers and see what results you get. In C, one possible error is: `typedef int a[4];` which will generate `error: used type 'a' (aka 'int [4]') where arithmetic or pointer type is required` (at least, with clang, which usually has better error messages than gcc).\n- Semantic feedback to the parser is undoubtedly is required in many languages, but its presence doesn't convert semantic errors into syntax errors.\n- @EJP: Of course, well-meaning people may not a dictionary. But I believe I use the word \"syntax\" with respect to C++ in a way similar to the way it is used in the C++ standard. For example, 6.8(3) \"The disambiguation is purely syntactic; that is, the meaning of the names occurring in such a statement, beyond whether they are type-names or not, is not generally used in or changed by the disambiguation.\" makes it clear that whether an identifier names a type is syntactic. Similarly, 7.1.3(1) \"Within the scope of its declaration, a typedef-name is syntactically equivalent to a keyword\".\n- @nci That use of 'syntactic' specifically refers to type names, and it does so because type names appear in the grammar and so need to be disambiguated from identifiers for syntax analysis purposes. The statement you have quoted specifically excludes all other aspects of the 'meaning of the names', which includes whether it has been declared. If a name hasn't been declared it certainly can't be a type name. I don't know what the remark about the dictionary is supposed to mean.\n- @EJP: 3.3.1(1) \"Every name is introduced in some portion of program text called a declarative region, which is the largest part of the program in which that name is valid, that is, in which that name may be used as an unqualified name to refer to the same entity. In general, each particular name is valid only within some possibly discontiguous portion of program text called its scope.\" That seems to me to be a purely syntactic description. Of course, we might disagree about the precise meaning of \"syntactic\", which is what I meant by my statement about well-meaning people and dictionaries.\n- @nci Syntax is defined by the grammar. You can change the subject all you want but you cannot get away from that fact. Typedefs are in the grammar, which is why the parser needs semantic feedback about them. Declaration of all variables is not in the grammar. Scope is not in the grammar. None of your quotations is to be found in the grammar. See my answer for further discussion.\n- @ejp. Grammar productions include `class-name: identifier` `enum-name: identifier` `template-name: identifier` and various others. It's quite clear that the C++ parser needs to figure out what type of identifier each identifier is during the parse. Take a look at the paragraph at Appendix A, and in particular A.1: \"New context dependent keywords are introduced...\"\n- @rici I agree, for the second time, but that doesn't make undeclared identifiers a syntax error. Where in the grammar does it say an identifier has to be declared?","metadata":{"transformedAt":"2026-08-18T18:32:17.826Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":142,"estimatedTokens":2995}}766{"id":"stack-16026358","source":"stackoverflow","questionId":16026358,"title":"How to make lex/flex recognize tokens not separated by whitespace?","tags":["parsing","compiler-construction","lex","lexer"],"text":"Title: How to make lex/flex recognize tokens not separated by whitespace?\nTags: parsing, compiler-construction, lex, lexer\nSource: Stack Overflow\n\nQuestion:\nI'm taking a course in compiler construction, and my current assignment is to write the lexer for the language we're implementing. I can't figure out how to satisfy the requirement that the lexer must recognize concatenated tokens. That is, tokens not separated by whitespace. E.g.: the string `39if` is supposed to be recognized as the number `39` and the keyword `if`. Simultaneously, the lexer must also `exit(1)` when it encounters invalid input.\n\nA simplified version of the code I have:\n\n```\n%{\n#include \n%}\n\n%option main warn debug\n\n%%\n\nif |\nthen |\nelse printf(\"keyword: %s\\n\", yytext);\n\n[[:digit:]]+ printf(\"number: %s\\n\", yytext);\n\n[[:alpha:]][[:alnum:]]* printf(\"identifier: %s\\n\", yytext);\n\n[[:space:]]+ // skip whitespace\n[[:^space:]]+ { printf(\"ERROR: %s\\n\", yytext); exit(1); }\n\n%%\n```\n\nWhen I run this (or my complete version), and pass it the input `39if`, the error rule is matched and the output is `ERROR: 39if`, when I'd like it to be:\n\n```\nnumber: 39\nkeyword: if\n```\n\n(I.e. the same as if I entered `39 if` as the input.)\n\nGoing by the manual, I have a hunch that the cause is that the error rule matches a longer possible input than the number and keyword rules, and flex will prefer it. That said, I have no idea how to resolve this situation. It seems unfeasible to write an explicit regexp that will reject all non-error input, and I don't know how else to write a \"catch-all\" rule for the sake of handling lexer errors.\n\n**UPDATE:** I suppose I could just make the catch-all rule be `. { exit(1); }` but I'd like to get some nicer debug output than \"I got confused on line 1\".\n\n========================================\n\nTop Answer:\nThis is a full solution, incorporating the suggestion of @rici. I noted a minor error in the call to yyless and also gave a solution that meets the needs to the original poster.\n\nPut this into a file yyin\n\n```\n%x LEXING_ERROR\n%%\n\\n { yylineno++; } // Track line number.\nif |\nthen |\nelse printf(\"keyword: %s\\n\", yytext);\n\n[[:digit:]]+ printf(\"number: %s\\n\", yytext);\n[[:alpha:]][[:alnum:]]* printf(\"identifier: %s\\n\", yytext);\n[[:space:]] ;// skip whitespace\n\n. { BEGIN(LEXING_ERROR); yyless(0); }\n.+ { fprintf(stderr,\n \"Invalid character '%c' found at line %d,\"\n \" just before '%s'\\n\",\n *yytext, yylineno, yytext+1);\n exit(1);\n }\n%%\nint yywrap(void) { return 1;}\n\nint main(void) {\nyylex();\nreturn 0;\n}\n```\n\nTo run, go to terminal, cd to the directory holding yyin and run\n\n```\nflex yyin \n gcc lex.yy.c -ll \n ./a.out where atest.txt is the text file that you are parsing.\n\nTo explain: first line '%x LEXING_ERROR' is a declaration that the term LEXING_ERROR is for an exclusive start condition. See the link in Rici's answer. The action for each new line is to update the yylinenum.\n\nThe final condition to capture any character, the dot, then triggers the start condition LEXING_ERROR. Note the call to yyless, where the argument is 0. Passing 1 points to the wrong character.\n\nThe skip whitespace has to be a single character match. If multicharacter it will swallow some new lines and the line update does not work correctly.\n\nIn this form, the original posters problem is solved. The single character exception produces a nice error message with the correct line number.\n\nI don't know if the details might differ on another platform. This runs in terminal on a Mac running Sonoma 14.4.1 with gcc and flex installed.\n\n========================================\n\nCode:\n```text\n%{\n#include <stdio.h>\n%}\n\n%option main warn debug\n\n%%\n\nif      |\nthen    |\nelse    printf(\"keyword: %s\\n\", yytext);\n\n[[:digit:]]+    printf(\"number: %s\\n\", yytext);\n\n[[:alpha:]][[:alnum:]]*     printf(\"identifier: %s\\n\", yytext);\n\n[[:space:]]+    // skip whitespace\n[[:^space:]]+   { printf(\"ERROR: %s\\n\", yytext); exit(1); }\n\n%%\n```\n\n```text\nnumber: 39\nkeyword: if\n```\n\n```text\n39if\n```\n\n```text\n39\n```\n\n```text\nif\n```\n\n```text\nexit(1)\n```\n\n```text\n39if\n```\n\n```text\nERROR: 39if\n```\n\n```text\n39 if\n```\n\n```text\n. { exit(1); }\n```\n\n```text\n%x LEXING_ERROR\n%%\n// all your rules; the following *must* be at the end\n.                 { BEGIN(LEXING_ERROR); yyless(1); }\n<LEXING_ERROR>.+  { fprintf(stderr,\n                            \"Invalid character '%c' found at line %d,\"\n                            \" just before '%s'\\n\",\n                            *yytext, yylineno, yytext+1);\n                    exit(1);\n                  }\n```\n\n```text\n--bison-bridge\n```\n\n```text\nbison\n```\n\n```text\n.+\n```\n\n```text\nyyless(n)\n```\n\n```text\nn\n```\n\n```text\n.\n```\n\n```text\n%x\n```\n\n```text\nBEGIN\n```\n\n```text\n%x LEXING_ERROR\n%%\n\\n      { yylineno++; }  // Track line number.\nif      |\nthen    |\nelse    printf(\"keyword: %s\\n\", yytext);\n\n[[:digit:]]+    printf(\"number: %s\\n\", yytext);\n[[:alpha:]][[:alnum:]]*     printf(\"identifier: %s\\n\", yytext);\n[[:space:]]    ;// skip whitespace\n\n.                 { BEGIN(LEXING_ERROR); yyless(0); }\n<LEXING_ERROR>.+  { fprintf(stderr,\n                            \"Invalid character '%c' found at line %d,\"\n                            \" just before '%s'\\n\",\n                            *yytext, yylineno, yytext+1);\n                    exit(1);\n                  }\n%%\nint yywrap(void) { return 1;}\n\nint main(void) {\nyylex();\nreturn 0;\n}\n```\n\n```text\nflex yyin       \n gcc lex.yy.c -ll  \n ./a.out <atest.txt\n```\n\n========================================\n\nComments:\n- a) Have you run your simplified version? b) what does it do that is wrong?\n- @IraBaxter Sorry, seems I forgot to be explicit about my test case while lost in the speculation in the last paragraph. The answers are **a)** yes; and **b)** reports the lexer error instead of two tokens. (I've also added them into the question.)\n- Ah. OK, yes, your \"^space\" rule will eat any sequence of non-space, and thus consume \"39if\". Secret: avoid rules whose regexes overlap, unless the longer rule comes safely first. In your case, I'd use (I'm not a lex-pert) something to replace :^space: that was \"not a digit, not a letter, not a space\". ...\n- With really good lexical specifications, you can write things like [any]*-[digit]+-[alpha][alnum]+-[space]+ to easily specify anything that doesn't look like your legal tokens. I don't believe lex will let your write this.\n- @IraBaxter Unfortunately, for the assignment, I'm stuck to lex and a requirement that I'd never practically have to bother with. (If I ever actually had to write a language I'd probably stick to ANTLR, based on the intuition that if what I'm doing can't be LL(1) I'm the wrong person to be doing it anyway.)\n- What *should* happen to white space? What tokens should be returned for `39 if` (i.e. separated by white space), or is that illegal?\n- @Bryan The second-to-last rule is to ignore whitespace. It's legal and should return the same as if they were concatenated. (Unless of course the concatenated word has a longer match, e.g. is a valid identifier.)\n- Then you should Ira Baxter's advice and just loose the last rule all together. Lex will always give the longest match and otherwise the first match. If white space is just ignored, then the absence of white space is not going to hurt.\n- If you want to handle inputs like `ifa<bthenx=4` (no spaces between names and keywords), you'll need something more complex.\n- I read up on start conditions but couldn't really put the pieces together, thanks!\n- It's a lot simpler to just return yytext[0] to the parser in the . rule and let the parser's error recovery deal with it. No start states required. This also eliminates all the rules for single special characters.\n- @EJP: The OP specifically states that one of the requirements is that the lexer must `exit(1)` when it encounters invalid input. There's no indication that there is a parser at all, with or without error recovery.\n- I wonder if it would be better to use `yymore()` or `unput()` instead of `yyless()`. I've never used any of them but `yyless()` strikes me as designed to some slightly different purpose. (some docs)","metadata":{"transformedAt":"2026-08-18T18:32:17.826Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":21,"totalLines":255,"estimatedTokens":2021}}767{"id":"stack-17936913","source":"stackoverflow","questionId":17936913,"title":"How do I reduce my parse tree into an abstract syntax tree?","tags":["parsing","compiler-construction","grammar","abstract-syntax-tree","concrete-syntax-tree"],"text":"Title: How do I reduce my parse tree into an abstract syntax tree?\nTags: parsing, compiler-construction, grammar, abstract-syntax-tree, concrete-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nWhat are the general strategies for reducing a parse tree (ie. concrete syntax tree) into an abstract syntax tree?\n\nFor example, I have the following grammar rule:\n\n```\nstatement_list : statement\n | statement_list statement\n```\n\nwhich, if left as a parse tree, will generate fanning output that looks like\n\n```\nprogram\n statement_list\n statement_list\n statement\n definition\n p_type\n assignment\n statement\n definition\n statement\n assign\n assignment\n```\n\nIf I concatenate the children of each node (since a statement list has no inherent meaning after parsing), I can achieve the following\n\n```\nprogram\n definition\n p_type\n assignment\n definition\n assign\n assignment\n```\n\nThis worked well - however, I'm unaware of any \"rules\" for doing this. Are there specific grammar rules I should be looking to simplify? Is it a matter of feel, or is there a more mechanistic process?\n\n========================================\n\nCode:\n```text\nstatement_list : statement\n               | statement_list statement\n```\n\n```text\nprogram\n        statement_list\n                statement_list\n                        statement\n                                definition\n                                        p_type\n                                        assignment\n                statement\n                        definition\n        statement\n                assign\n                        assignment\n```\n\n```text\nprogram\n        definition\n                p_type\n                assignment\n        definition\n        assign\n                assignment\n```\n\n========================================\n\nComments:\n- You can go for squishy \"look and feel\" but that mostly a lot of work. You can do this essentially mechanically by removing the nodes that you can regenerate using the grammar. See stackoverflow.com/a/1916687/120163\n- I appreciate the answer, but it feels like you're contradicting yourself. You say that \"It's not a matter of 'feel'\" but your rules depend on the language. If there were some metrics to go by then I would agree it's not a matter of feel, but as it stands it certainly feels that way.\n- I'll admit any day that design (language design in particular) is 50/50 engineering/feel. Is that any good?\n- Yes, of course, sorry - I forgot to come back to this question. Thanks for the help.","metadata":{"transformedAt":"2026-08-18T18:32:17.826Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":85,"estimatedTokens":621}}768{"id":"stack-21564859","source":"stackoverflow","questionId":21564859,"title":"Big-O for a compiler","tags":["algorithm","compiler-construction"],"text":"Title: Big-O for a compiler\nTags: algorithm, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nDoes anyone have insight into the typical big-O complexity of a compiler?\n\nI know it must be `>= n` (where n is the number of lines in the program), because it needs to scan each line at least once.\n\nI believe it must also be `>= n.logn` for a procedural language, because the program can introduce O(n) variables, functions, procedures, and types etc., and when these are referenced within the program it will take O(log n) to look up each reference.\n\nBeyond that my very informal understanding of compiler architecture has reached its limits and I am not sure if forward declarations, recursion, functional languages, and/or other tricks will increase the algorithmic complexity of the compiler.\n\nSo, in summary:\n\nFor a 'typical' procedural language (C, pascal, C#, etc.) is there a limiting big-O for an efficiently designed compiler (as a measure of number of lines)\n\nFor a 'typical' functional language (lisp, Haskell, etc.) is there a limiting big-O for an efficiently designed compiler (as a measure of number of lines)\n\n========================================\n\nTop Answer:\nReaching back to what I can remember from my compilers class... some of the details here may be a bit off, but the general gist should be pretty much correct.\n\nMost compilers actually have multiple phases that they go through, so it'd be useful to narrow down the question somewhat. For example, the code is usually run through a *tokenizer* that pretty much just creates objects to represent the smallest possible units of text. `var x = 1;` would be split into tokens for the var keyword, a name, an assignment operator, and a literal number, followed by a statement finalizer (';'). Braces, parentheses, etc. each have their own token type.\n\nThe tokenizing phase is roughly O(n), though this can be complicated in languages where keywords can be contextual. For example, in C#, words like `from` and `yield` can be keywords, but they could also be used as variables, depending on what's around them. So depending on how much of that sort of thing you have going on in the language, and depending on the specific code that's being compiled, just this first phase could conceivably have O(n²) complexity. (Though that would be highly uncommon in practice.)\n\nAfter tokenizing, then there's the parsing phase, where you try to match up opening/closing brackets (or the equivalent indentations in some languages), statement finalizers, and so forth, and try to make sense of the tokens. This is where you need to determine whether a given name represents a particular method, type, or variable. A wise use of data structures to track what names have been declared within various scopes can make this task pretty much O(n) in most cases, but again there are exceptions. \n\nIn one video I saw, Eric Lippert said that correct C# code can be compiled in the time between a user's keystrokes. But if you want to provide meaningful error and warning messages, then the compiler has to do a great deal more work.\n\nAfter parsing, there can be a number of extra phases including optimizations, conversion to an intermediate format (like byte code), conversion to binary code, just-in-time compilation (and extra optimizations that can be applied at that point), etc. All of these *can* be relatively fast (probably O(n) most of the time), but it's such a complex topic that it's hard to answer the question even for a single language, and practically impossible to answer it for a genre of languages.\n\n========================================\n\nCode:\n```text\n>= n\n```\n\n```text\n>= n.logn\n```\n\n```text\nO(n)\n```\n\n```text\nn\n```\n\n```text\nO(2^n)\n```\n\n```text\nn\n```\n\n```text\nf(n)\n```\n\n```text\nvar x = 1;\n```\n\n```text\nfrom\n```\n\n```text\nyield\n```\n\n========================================\n\nComments:\n- Number of lines is not a precise indicator of the amount of work to be done. A piece of code can be golfed into a single line and have the complexity of ungolfed version that is hundred of lines.\n- Why do you assume it would take O(log n) to look up the reference to a given variable/function/type name?\n- Agree, using hash table can look it up in O(1)\n- Voting to close as \"too broad\" as a compiler consists of many parts, each may have a different complexity, and each may differ between compilers.\n- @Dukeling \"Too broad\" does not mean \"involves some moving parts\" it means \"too large a subject to cover in Q&A format\", and I think the latter does not necessarily apply. There are literally thousands of good questions about compilers where answers need to at least touch on the major phases of compilation. Are those too broad too?\n- Scanning and parsing are both O(N) unless incompetently implemented. Symbol table lookup is O(1) ditto. AST processing if any, optimization if any, register allocation if any, and code generation can be anything from O(N) onwards depending on internal complexity.\n- @delnan It would just be silly to say **all** such questions are too broad, but I think this particular one is. I think a great answer to this question would be quite a few pages long - it wouldn't just say \"the complexity of this part is X\", but rather explain what exactly that part does, and how the complexity of X can be obtained in sufficient detail. In addition, it would probably have to pick a language for this said complexity and discuss the chosen language in sufficient detail to be able to understand the answer for someone not familiar with it.\n- Indeed, this question is ill-posed but there is an interesting question in here somewhere. Lexical and syntactic analysis of C# is O(n) in the number of characters but semantic analysis can be much harder because it is possible to encode problems into the program that the compiler must solve. C#, for example, is NP-HARD to analyze because it is possible to encode 3SAT problems that the overload resolution algorithm must demonstrate a solution to. I demonstrate how here: blogs.msdn.com/b/ericlippert/archive/2007/03/28/&hellip;\n- Moreover, any language that has a combination of nominal subtyping, and covariant/contravariant generic types could be *undecidable* depending on the exact details of the language; the decidability of some languages is still an open question. See research.microsoft.com/en-us/um/people/akenn/generics/&hellip; for details.\n- I am curious why you believe that it takes O(lg n) to look up a string. It takes O(1) to look up a string in a hash table **in the side of a hash table**, but it takes O(n) to look up a string in a hash table **in the length of the string**.\n- Oh, one other: in C# the method type inference algorithm is typically n squared in the number of type arguments to be inferred but there is a rare worst case in which it is n to the fourth. Since the number of type arguments to be inferred is in practice never more than five or six, this is irrelevant. (I could have written the implementation to be n squared always, but that would have been a huge amount of work for absolutely no practical gain.)\n- The parsing is just one small part of a compiler. Regardless, any decent parser is linear time, so this doesn't give much insight. LR is always linear time, LL if it's based on lookahead rather than backtracking, and likewise for most other practical options. Parsing won't be the bottleneck, asymptotically speaking (constant factors may differ)\n- If lexing is O(n&#178;), someone didn't do their job - either the compiler writer (by being actively stupid about implementing it) or the language designer (by making a highly ambigiuous language). Non-regular lexical featues such as contextual keywords (which I would always lex as keyword and sort out uses as identifiers during parsing) or significant whitespace (indentation stack) *can* be resolved with a little bit of extra on-line maintaince, leaving complexity at O(n). Many optimizations, on the other hand, *do* have worse-than-linear complexity in practice.\n- \"C# code can be compiled in the time between a user's keystrokes\" In C# or Java, you have the luxury of skipping most of the optimizations, because you can always rely on the JIT compiler to kick in during run time. For \"real\" compiled languages, the time needed for optimization probably dominates the parsing time by a large margin (I think most of these have near-linear runtime in practice as well, but could break down on certain artificial input instances).\n- @NiklasB. Luckily, for use cases where you need to react to keystrokes, you either don't need to generate code *at all* (intellisense, catching errors) or don't need any optimizations (debugging). Also, compiling C++ does not only take time because it targets machine code, language design plays a significant part.\n- @delnan: Sure, Go is a perfect example to show that you can have good code in short time.\n- I note that Roslyn has to be very clever about skipping all unnecessary work in order to achieve the performance goal of analysis between keystrokes. The Roslyn lexer and parser were both designed carefully to ensure that they only re-lex and re-parse the portion of the program that actually changed on the last keystroke, so that previous analysis can be re-used. Similarly the semantic analysis is very \"lazy\"; if you type `foo.` then only the parts of the program necessary to work out \"what does `foo` mean and what are its members?\" are analyzed.\n- What kind of AST transformation causes exponential blowup in the number of leaves? That'd make even very modest programs completely impractical to compile. In my expeirence, the size of the AST and the intermediate language programs will be roughly proportional to the size of the input file. Depending on the details of the program the AST may be larger or smaller (e.g. when expanding syntactic sugar), with potentially large constant factors, but I don't think you can meaningfully expand the AST to size O(n log n), let alone worse.\n- How is compiling Java and C# undecidable? I'll buy that Scala's type system is turing complete (though whether \"compiling it is undecidable\" is the correct term is up for debate), but Java and C# are positively tame.\n- @delnan It lies in variance. Ben Pierce and Andrew Kennedy have a paper on it (which also pops up with a simple google btw :)\n- Now that you mention it, I've come across that before. Still, \"there is no 'reasonable complexity'\" is a very fishy conclusion.\n- @delnan Also, inlining in small programs is at least `O(n^2)`. This is a worse case complexity measure so we're supposed to examine the pathological cases :)\n- @delnan If something is undecidable, there isn't a complexity for it since it loops forever. If you want a complexity for a correct compiler for these languages, there isn't one.\n- @delnan I'm assuming you're the downvoter, nothing in the answer is incorrect so I'm curious why the downvote.\n- It's simply not a useful statement. Yes, it might loop indefinitely, but that's not the cases we're interested in when asking for the performance of a compiler. I downvoted primarily to counter the upvote while I still have my doubts about the core points of this answer being useful in the context of this answer.\n- @delnan It's exactly what we're interested in when we're examing the `O` of a compiler, which is the **worst** possible complexity. The answer asked for the worst case complexity of \"a compiler\". Even taking generously to mean \"a reasonable language\" there isn't a worst case complexity. In any arbitrary compiler this question is also absurd. And even given a compiler for a trivial language like STLC you're still at the mercy of whatever optimizations/transformations you have to run. For example register allocation is `O(2^n)`.\n- (You're conflating the kind of bound with the case for which the bound applies. But anyway:) That the problem is undecidable is interesting, but the complexity of those instances which *are* decidable (however they may be distinguished) is also of interest. Given that the undecidability was discovered by theory rather than actual programs, it's also the metric more likely to be significant in practice.\n- let us continue this discussion in chat\n- Large programs wouldn't compile if the complexity were exponential. Since enterprise systems are able to execute code, it implies they use more efficient algorithms.","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":109,"estimatedTokens":3094}}769{"id":"stack-9034979","source":"stackoverflow","questionId":9034979,"title":"Can a control dependence graph have loops?","tags":["compiler-construction","graph","controls"],"text":"Title: Can a control dependence graph have loops?\nTags: compiler-construction, graph, controls\nSource: Stack Overflow\n\nQuestion:\nI'm trying to understand precisely the notion of a control dependence graph. Suppose I have the following control flow graph (in DOT notation) :\n\n```\ngraph g {\n1 -> 2;\n2 -> 3;\n3 -> 2;\n2 -> 4;\n1 -> 4\n}\n```\n\nIt has a unique entry node (1) and a unique exit node (4), and a loop 2 -> 3 -> 2.\n\nMy question is: does the control dependence graph for this CFG contain a loop edge from 2 to itself? \n\nAllen & Kennedy's \"Optimizing compilers for modern architectures\" has an algorithm that produces such a loop edge. However, Muchnick's \"Compiler design & implementation\"'s algorithm for control dependence does not produce such an edge. Besides, I couldn't find any examples in the literature where a CDG is drawn with such a loop edge. I tend to believe there is no such edge, but according to the formal definition of control dependence and according to Allen & Kennedy's algorithm, it should!\n\nIf you can please point me to an example where there is such a loop in a CDG (preferably in a peer-reviewed paper, or some professor's lecture notes, etc), or if you can argue why Allen & Kennedy's algorithm should be incorrect, I'd be glad to know.\n\n========================================\n\nTop Answer:\nThe utility of such a dependence graph is determine how to order the operations, right? In that sense, it is not helpful to know that an element depends on itself. You can draw the loops if you like, but what's really important is all the other edges.\n\n========================================\n\nCode:\n```text\ngraph g {\n1 -> 2;\n2 -> 3;\n3 -> 2;\n2 -> 4;\n1 -> 4\n}\n```\n\n========================================\n\nComments:\n- The utility of such a dependence graph is determine how to order the operations, right? In that sense, it is not helpful to know that an element depends on itself. You can draw the loops if you like, but what's really important is all the other edges.\n- Yes, I think I was expecting some \"canonical definition\" which could be used as an oracle to test multiple implementations, but it's true that both versions are equivalent for all practical purposes... thanks!\n- @mitchus You should move your comment to an answer so that it can be accepted as the answer.\n- You're right, I had accepted @mitchus' answer, but yours is more precise. I still find mitchus' comment quite relevant.","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":50,"estimatedTokens":606}}770{"id":"stack-53434947","source":"stackoverflow","questionId":53434947,"title":"Partial function in Coq / underdefined?","tags":["compiler-construction","isabelle","agda"],"text":"Title: Partial function in Coq / underdefined?\nTags: compiler-construction, isabelle, agda\nSource: Stack Overflow\n\nQuestion:\nI have been trying to write and verify a compiler in Agda, using Concrete Semantics (which is written for Coq Isabelle/HOL) as a reference point. I am defining compilation for the same languages used in that text.\n\nFor context I have finished writing the compiler, and am now in the verification stage, however I had to make a significant difference to Concrete Semantics in the definition of machine instruction execution. This difference seemed necessary in Agda, but now is making the verification stage incredibly complex.\n\nIn trying to do the simpler version of instruction execution given in Concrete Semantics, I've come across this line, which may explain why I am having trouble directly translating this into Agda:\n\nAlso useful are the head of a list, its first element, and the tail, the rest of the list:\n\n```\nfun hd :: 'a list ⇒ 'a\nhd (x # xs) = x\n```\n\nNote that since HOL is a logic of total functions, `hd []` is defined, but we do not know what the result is. **That is, `hd []` is not undefined but underdefined.**\n\n**What is does it mean for `hd []` to be underdefined? Is this the equivalent of having an incomplete pattern in Agda?**\n\nThe assembly instruction execution function relies heavily on `hd`. In my implementation of it in Agda I gave indices to multiple types to allow me to build proofs that the stack always has the minimum number of elements, in order to avoid the incomplete pattern matching problem. Now that I am trying to verify the compiler the proofs are magnitudes more complex than the proofs in Concrete Semantics, since I have to work with these indices.\n\n**Am I missing something or are the proofs in Concrete Semantics incomplete with `hd []` not being defined?**\n\n========================================\n\nCode:\n```text\nfun hd :: 'a list ⇒ 'a\nhd (x # xs) = x\n```\n\n```text\nhd []\n```\n\n```text\nhd []\n```\n\n```text\nhd []\n```\n\n```text\nhd\n```\n\n```text\nhd []\n```\n\n```text\nhd []\n```\n\n```text\nhd [] = hd []\n```\n\n```text\nx = x\n```\n\n```text\nhd []\n```\n\n```text\nhd\n```\n\n```text\nhd\n```\n\n========================================\n\nComments:\n- This blogpost might be useful to understand undefinedness in Isabelle joachim-breitner.de/blog/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":12,"totalLines":82,"estimatedTokens":576}}771{"id":"stack-1208338","source":"stackoverflow","questionId":1208338,"title":"Methodologies for designing a simple programming language","tags":["language-design","compiler-construction","bytecode"],"text":"Title: Methodologies for designing a simple programming language\nTags: language-design, compiler-construction, bytecode\nSource: Stack Overflow\n\nQuestion:\nIn my ongoing effort to quench my undying thirst for more programming knowledge I have come up with the idea of attempting to write a (at least for now) simple programming language that compiles into bytecode. The problem is I don't know the first thing about language design. Does anyone have any advice on a methodology to build a parser and what the basic features every language should have? What reading would you recommend for language design? How high level should I be shooting for? Is it unrealistic to hope to be able to include a feature to allow one to inline bytecode in a way similar to gcc allowing inline assembler? Seeing I primarily code in C and Java which would be better for compiler writing?\n\n========================================\n\nTop Answer:\nYou might want to read a book on compilers first. \n\nFor really understanding what's going on, you'll likely want to write your code in C. \n\nJava wouldn't be a bad choice if you wanted to write an interpreted language, such as Jython. But since it sounds like you want to compile down to machine code, it might be easier in C.\n\n========================================\n\nComments:\n- Dupe of stackoverflow.com/questions/479013/&hellip; among many others. Also, you are asking too many questions - one at a time is a good rule.\n- And this stackoverflow.com/questions/1669/learning-to-write-a-compile&zwnj;&#8203;r is the definitive SO answer on the subject area.\n- ok I'm sorry I didn't see it was a duplicate should it just be closed as a duplicate or should I delete the question?\n- possible duplicate of creating-your-own-language\n- *\"Targetting a bytecode is not simply a good idea\"* As opposed to targeting a real machine (e.g. x86), writing an interpreter, or something else? On the subject, does writing a compiler that targets even an \"ideal\" virtual machine (as opposed to a CPU where you have to worry about register allocation et al.) tend to be significantly more difficult than writing an interpreter? I'd imagine one could make compilation fairly easy by compiling to a tree instead of a flat byte string, but I've never done it before, and I'd like to know what other people have to say about it.\n- @Joey As opposed to targetting a real machine, indeed. Even compilers that generate machine code often generate an intermediary bytecode output (though the high end compilers may avoid doing so for maximum gains in compilation speed and available optimizations). Writing an interpreter is easier, indeed, particularly if you choose to write a dynamic language. As for compiling into a tree, a tree is the *output* of parsing, so it is definitely easier -- though not really all that much.\n- Oops, I got confused by your sentence (looked like you said targeting bytecode is insane and useless, but you said the opposite). Also, I was talking more along the lines of a tree tuned for execution, distinct from basic parser output (though in simple settings, they might be pretty much the same structure).\n- @Joey The point I was making is that the execution tree can be obtained through a transformation on the AST.","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":27,"estimatedTokens":812}}772{"id":"stack-6659998","source":"stackoverflow","questionId":6659998,"title":"How does hardware and compiler deal with an x86 instruction that crosses into a non-executable page?","tags":["memory","compiler-construction","assembly","x86"],"text":"Title: How does hardware and compiler deal with an x86 instruction that crosses into a non-executable page?\nTags: memory, compiler-construction, assembly, x86\nSource: Stack Overflow\n\nQuestion:\nUnlike (most) RISC arch, x86 instructions have variable length. The start/end of an instruction doesn't have to aligned. If the compiler doesn't one instruction could be just lying across the page margin.\n\nAssume that if the first byte of an instruction is at the last byte of a page, which is marked as executable. The rest bytes of the instruction are at the second page, which is marked as non-executable. \n\nIn such case, what will happen in the CPU when the execution reaches this instruction?\n\nDoes compiler need to care such cases?\n\n========================================\n\nComments:\n- Upvoted unexplained downvote.\n- When the next page *is* executable, it just works. Fetch is done in aligned chunks, feeding a queue for the decoders. Combining bytes from separate pages happens some time after fetch, after TLB checking, so it's not like a page-split data load.\n- I seem to recall a bug relating to that with early iapx286 chips in protected mode. Either it would fail to GPF on an instruction going over a boundary, or would erroneously GPF on an instruction that should just fit inside. Can't recall at the moment... #trivia :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":19,"estimatedTokens":333}}773{"id":"stack-7619874","source":"stackoverflow","questionId":7619874,"title":"How to make boost.build use a specific compiler installation?","tags":["boost","compiler-construction"],"text":"Title: How to make boost.build use a specific compiler installation?\nTags: boost, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am trying to build boost 1.45 using a local GCC compiler installation. I can't make it use a different compiler command that the default \"g++\". Here what happened so far:\n\nIn boost_1_45_0 source directory:\n\n```\n./bootstrap.sh --with-toolset=gcc --prefix=$INSTALL/boost-$TYPE\n```\n\nThen added the following line to \"projct-config.jam\":\n\nusing gcc : 4.4.6 : [absolute path]/install/gcc-4.4.6/bin/g++ : ;\n\n```\n./bjam install --prefix=$INSTALL/boost-$TYPE\n```\n\nWhen bringing up the process list during building, I can see that the system's default compiler command g++ gets used.\n\n========================================\n\nTop Answer:\nThat should be `toolset=gcc-4.4.6` rather than `--with-toolset=gcc` (features are not specified with leading dashes).\n\n========================================\n\nCode:\n```text\n./bootstrap.sh --with-toolset=gcc --prefix=$INSTALL/boost-$TYPE\n```\n\n```text\n./bjam install --prefix=$INSTALL/boost-$TYPE\n```\n\n```text\nif ! gcc in [ feature.values <toolset> ]\n{\n    using gcc : 4.4.6 : [absolute path]/install/gcc-4.4.6/bin/g++ : -L[absolute path]/install/gcc-4.4.6/lib64 -I[absolute path]/install/gcc-4.4.6/include ;\n}\n```\n\n```text\ntoolset=gcc-4.4.6\n```\n\n```text\n--with-toolset=gcc\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":53,"estimatedTokens":337}}774{"id":"stack-4938179","source":"stackoverflow","questionId":4938179,"title":"Resources on converting syntax tree to assembly?","tags":["parsing","compiler-construction","assembly","abstract-syntax-tree"],"text":"Title: Resources on converting syntax tree to assembly?\nTags: parsing, compiler-construction, assembly, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nPrimarily as a learning exercise, I am writing a virtual machine, an assembler, and a compiler from scratch, depending on no external tools.\n\nI believe I have a decent conceptual understanding of how the virtual machine and assembler will work, as well as some parts of the compiler.\n\nHere's what I want to know:\n**In the compiler, suppose I have turned the source code into a syntax tree. What process do I go through to then convert this syntax tree to assembly?**\n\n(Let's assume some simple language constructs, like if and while. I'm looking for a minimal and simple explanation here.)\n\nI am not particularly interested in complex solutions, or solutions based on existing tools. Rather, I'd like something on the order of a 1-page, broad sweeping description of the ideas behind going from syntax tree to assembly.\n\nAnyone know of such a resource?\n\nThanks :)\n\n========================================\n\nComments:\n- Sure. The 2nd and 3rd pages of chapter 1 of any compiler book. But I think you are hoping that is all you need, and isn't by a long shot. The problem is that translating procedural langauges to *good* machine code (even for a virtual machine) requires lots of machinery, and while those 2 pages will give a vague sense of what is needed, you aren't going to be able to build any kind of sensible code generator without reading the other 200 pages of the compiler book.","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":387}}775{"id":"stack-6903361","source":"stackoverflow","questionId":6903361,"title":"Lexer that recognizes indented blocks","tags":["python","compiler-construction","whitespace","lexer"],"text":"Title: Lexer that recognizes indented blocks\nTags: python, compiler-construction, whitespace, lexer\nSource: Stack Overflow\n\nQuestion:\nI want to write a compiler for a language that denotes program blocks with white spaces, like in Python. I prefer to do this in Python, but C++ is also an option. Is there an open-source lexer that can help me do this easily, for example by generating INDENT and DEDENT identifiers properly like the Python lexer does? A corresponding parser generator will be a plus.\n\n========================================\n\nTop Answer:\nIf you're using something like lex, you can do it this way:\n\n```\n^[ \\t]+ { int new_indent = count_indent(yytext);\n if (new_indent > current_indent) {\n current_indent = new_indent;\n return INDENT;\n } else if (new_indent You may need a little additional logic, for example to ignore blank lines, and to automatically add a DEDENT at the end of the file if needed.\n\nPresumably count_indent would take into account converting tabs to spaces according to a tab-stop value.\n\nI don't know about lexer/parser generators for Python, but what I posted should work with lex/flex, and you can hook it up to yacc/bison to create a parser. You could use C or C++ with those.\n\n========================================\n\nCode:\n```text\n^[ \\t]+              { int new_indent = count_indent(yytext);\n                       if (new_indent > current_indent) {\n                          current_indent = new_indent;\n                          return INDENT;\n                       } else if (new_indent < current_indent) {\n                          current_indent = new_indent;\n                          return DEDENT;\n                       }\n                       /* Else do nothing, and this way\n                          you can essentially treat INDENT and DEDENT\n                          as opening and closing braces. */\n                     }\n```\n\n========================================\n\nComments:\n- This question is from almost ten years ago. I can't say I remember much. From the description though, this question prefers python and the other one C.\n- To provide context for the above out of the blue comment, there was a question about this being a duplicate of stackoverflow.com/questions/1413204/&hellip;. The person closing this has deleted their question. FWIW, I don't think this is a duplicate.\n- You have to be careful with this because you may need to add multiple DEDENT tokens at the start of a line, not just one. Python suggests having a stack to maintain this.\n- Thanks. This seems to be the perfect tool for me.","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":48,"estimatedTokens":644}}776{"id":"stack-10687899","source":"stackoverflow","questionId":10687899,"title":"Static Single Assignment: not all possible paths define a variable - how to insert PHI?","tags":["compiler-construction","ssa"],"text":"Title: Static Single Assignment: not all possible paths define a variable - how to insert PHI?\nTags: compiler-construction, ssa\nSource: Stack Overflow\n\nQuestion:\nI am implementing *SSA construction* for a compiler I'm writing. There is something I don't understand in the SSA algorithms (using the Cytron paper and the book *Modern Compiler Implementation in Java, second edition* by A.W. Appel). What if a variable `y` is defined for the first time (and used) in one straight control flow path but never defined in another parallel path. Do I have to insert a PHI-function at the join point (the dominance frontier of the block defining `y`)?\n\n```\nx = 1; // A\nif (P) // A\n y = x + 1; // B\n y = y + 1; // B\nx = x + 1; // C\nreturn; // C\n```\n\nFor example, in block B there is the first definition of `y`. Do I have to insert a PHI instruction at the start of block C, with two operands (one for each incoming control flow path)? Then on SSA renaming: how would I name the operand coming from the path `A -> C` (not through B) where `y` is never defined?\n\n```\nEntry --- A --------- C --- Exit\n \\ /\n \\-- B --/\n```\n\n========================================\n\nCode:\n```text\nx = 1;            // A\nif (P)            // A\n    y = x + 1;    // B\n    y = y + 1;    // B\nx = x + 1;        // C\nreturn;           // C\n```\n\n```text\nEntry --- A --------- C --- Exit\n           \\         /\n            \\-- B --/\n```\n\n```text\ny\n```\n\n```text\ny\n```\n\n```text\ny\n```\n\n```text\nA -> C\n```\n\n```text\ny\n```\n\n```text\nx1 = 1;           // A\nif (P)            // A\n    y1 = x1 + 1;  // B\n    y2 = y1 + 1;  // B\ny3 = φ(y0, y2)    // C\nx2 = x1 + 1;      // C\nreturn;           // C\n```\n\n```text\nc0\n```\n\n========================================\n\nComments:\n- No, if the variable never escapes the block where it is defined, then you don't need a phi-function. I dont understand why you think you need one?\n- I am also trying to implement SSA, and following the 'Modern Compiler Implementation in Java, second edition by A.W' book I don't understand if i do y3 = φ(y0, y2) , where do I define φ and how? if you can give some hint it will be really helpful. thank you.","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":9,"totalLines":81,"estimatedTokens":533}}777{"id":"stack-34108131","source":"stackoverflow","questionId":34108131,"title":"What jobs does a typical C++ compiler handle?","tags":["c++","compiler-construction","linker","machine-translation"],"text":"Title: What jobs does a typical C++ compiler handle?\nTags: c++, compiler-construction, linker, machine-translation\nSource: Stack Overflow\n\nQuestion:\nAfter researching a bit on compilers and how they work I learned that the process is often broken up into 4 steps: Preprocessor, Compiler, Assembler and Linker. The way I envisioned these steps was each being it's own separate program; A preprocessor program, a compiler program, an assembler program and a linker program. However, you learn that sometimes the process of creating assembly code and generating object files is all handled by the compiler program and sometimes its not. It seems to depend very much on the context and programming language used. My question is then how is the typical translation process broken up for translating C++ source code into machine code? \n\n- Is the preprocessor a separate program from the compiler? Or is that process usually a part of the compiler program?\n\n- What is the compiler typically responsible for? Generating assembly code and then conversion to machine code?\n\n- Is the linker it's own separate program that is run after the compiler finishes?\n\nSide note: My question is different from other C++ compiler threads because I'm asking not only how a compiler works but if certain other processes, such as linking, are there own executable programs or if they are typically built into a compiler program.\n\n========================================\n\nCode:\n```text\n-S\n```\n\n```text\nwhile\n```\n\n```text\nfor\n```\n\n```text\ngoto\n```\n\n========================================\n\nComments:\n- Possible duplicate of How do C/C++ compilers work?\n- Preprocessor, front-end, back-end (code generator and optimizer), linker. Still using an assembler is rare today, too slow. Not actually that straight-forward anymore, the compiler can stop at the front-end and produce intermediate code. Consumed by the linker which can start the back-end to implement whole-program optimization.\n- \"using an assembler is rare today\" nice, clearly nobody is using gcc today. Or llvm with a target where it does not come with an integrated assembler. Or several others...\n- Note that that sequence is only for one compilation/translation unit. Build control (e.g. make) is step one.\n- Wow fantastic answer. Exactly what I was looking for. Thank you!","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":42,"estimatedTokens":578}}778{"id":"stack-8439964","source":"stackoverflow","questionId":8439964,"title":"How to interact with the compiler in Scala code itself?","tags":["scala","compiler-construction","programming-languages","compilation","abstract-syntax-tree"],"text":"Title: How to interact with the compiler in Scala code itself?\nTags: scala, compiler-construction, programming-languages, compilation, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI wonder how many ways exist to interact with the Scala compiler outside of the normal “invoke it on the command line to compile my sources”.\n\nIs there is some way to parse code, build an abstract syntax tree or use a library to compile code at runtime?\n\n========================================\n\nTop Answer:\nI have done this in the past by creating a new CompilerCommand instance to parse command line arguments and, more importantly, by extending the ever-terrifying Global class.\n\nBy overriding the `computeInternalPhases` method, you can use only some phases of the compiler (e.g. up to `refchecks`, to use it as a parser/typechecker only), and you can add your own phases (plugins) as you would expect. You can of course also go all the way to generating class files.\n\nSo yes, it is definitely possible. After all, the compiler itself also runs on the JVM.\n\n========================================\n\nCode:\n```text\nbind\n```\n\n```text\ncompileSources\n```\n\n```text\ncompileString\n```\n\n```text\ncomputeInternalPhases\n```\n\n```text\nrefchecks\n```\n\n```text\ntreeFrom\n```\n\n========================================\n\nComments:\n- possible duplicate of Can I get AST from live scala code?","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":49,"estimatedTokens":342}}779{"id":"stack-17018463","source":"stackoverflow","questionId":17018463,"title":"How to simplify a C-style arithmetical expression containing variables during code generation?","tags":["c++","algorithm","compiler-construction","code-generation"],"text":"Title: How to simplify a C-style arithmetical expression containing variables during code generation?\nTags: c++, algorithm, compiler-construction, code-generation\nSource: Stack Overflow\n\nQuestion:\nI am trying to optimize expression evaluation in a compiler.\n\nThe arithmetical expressions are all C-style, and they may contain variables. I hope to simplify the expressions as much as possible.\n\nFor example, `(3+100*A*B+100)*3+100` may be simplified to `409+300*A*B`. \n\nIt mainly depends on the distributive law, the associative law and the commutative law.\n\nThe main difficulty I encounter is how to combine these arithmetical laws and traditional stack-scan evaluating algorithms.\n\nCan anyone experiences related to this or similar problems in the context of compiler building?\n\n========================================\n\nTop Answer:\nCompilers usually have some internal normalization rules like \"constants to the left\". This means `a + 3` would be transformed into `3 + a`, but not vice versa.\n\nIn your example,\n`(3+100*A*B+100)*3+100` would be normalized into\n`(3+100+(100*A*B))*3+100`.\nNow it is clear to optimize `3+100`.\n\nAnother transformation might be `a*C1+C2` into `(a+(C2/C1))*C1` under the condition that `C1` and `C2` are constants. Intuitively, this normalizes \"add before multiply\".\n\nThose normalizations are not optimizations. The intention is mostly to group constants together, so constant folding is more effective.\n\n========================================\n\nCode:\n```text\n(3+100*A*B+100)*3+100\n```\n\n```text\n409+300*A*B\n```\n\n```text\na + 3\n```\n\n```text\n3 + a\n```\n\n```text\n(3+100*A*B+100)*3+100\n```\n\n```text\n(3+100+(100*A*B))*3+100\n```\n\n```text\n3+100\n```\n\n```text\na*C1+C2\n```\n\n```text\n(a+(C2/C1))*C1\n```\n\n```text\nC1\n```\n\n```text\nC2\n```\n\n========================================\n\nComments:\n- Only `+-*&#47;` and parentheses?\n- @CaseyChu In fact, all C operators may appears. But I think only considering +-*/() is also acceptable. I am `trying my best` to simplified them.\n- You probably need to develop a rewriting system, which would successively apply rewriting rules to the expression. Before doing that, you could have a look at some existing compiler source code, to see how it handles such optimizations. I heard that LLVM source code is very readable.\n- The key terms you want to look up are \"transitive closure\" and \"normal form\". Note, that there is no such thing as \"simple\" or \"complex\", just different forms. You need to decide what forms you want to start with, and what forms you want to translate into to.\n- You should also look up 'constant folding'.","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":86,"estimatedTokens":646}}780{"id":"stack-12017504","source":"stackoverflow","questionId":12017504,"title":"Go as a backend for my compiler?","tags":["compiler-construction","code-generation","go","backend"],"text":"Title: Go as a backend for my compiler?\nTags: compiler-construction, code-generation, go, backend\nSource: Stack Overflow\n\nQuestion:\nI want to make a compiler for my own programming language. Popular backend choices seem to be C, Java, LLVM, JVM bytecode, .Net bytecode, gcc, assembly... Here, I am considering **the possibility of Go as a backend**.\n\nGo is apparently a fast language, with garbage collection, and fast compile times. It is also portable and free (BSD-style licence). All those would make Go a good choice as a target of code generation, I think, maybe even better than the other options... So I am surprised I can't find anybody doing that already.\n\nWould Go be a good choice for code generation? Can you point at existing projects doing so, or explain why there are none? Or even better, do you have experience with using the Go language as a backend? Are there any downside I am unaware of?\n\n(I'm specifically interested in Go here. Don't just point at alternative backend options, there are questions answering that already.)\n\n========================================\n\nTop Answer:\nThere is this project called GoGo which is described to be a compiler written in Go and assembly for a subset of Go. Basically like a stripped down version of Go. I think you could start by modifying it to parse your own language.\n\nI also remember a scripting-language-like subset of Go with its own compiler. I thought it was called GoScript but it seems like there are at least 3 different project with that same name so I wasn't able to find it.\n\nI'd say do it and your experience. Rather than a backend though, Go is going to be your intermediate language. At least that's what I think you want to do.\n\nCheers!\n\n========================================\n\nComments:\n- One interesting thing about using Go as a backend is that the standard library has packages for manipulating Go abstract syntax trees. So one option would be for you to build the AST of your output program in memory, and then convert it to text.\n- Go can be a backend for your language in the same way that C was a backend for C front.\n- Go is not Virtual Machine. Go generates native code. Virtual Machines are not better or worst then native code apps.\n- If he will implement new language and use Go as backend then threads would be unavailable to him without using native C that is out of topic of question.\n- A language that simply translates to Go, will have access to every single aspect of Go itself. I do not see how threads would be any different. The limits of what his language can do is entirely up to how much he decides to implement.\n- Thread t1 = new Thread(); is impossible in Go. So it would be impossible in language that will build over Go. Internally app may create threads But new language will have to use goroutines only. Gorotines are not threads.\n- @Max: Not true. You can explicitly create a thread of execution in Go with LockOSThread.\n- I was not aware of LockOSThread(). Does it make sense to create language that will use Go as backend and will provide Thread API with go func() { LockOSThread() ..... } (). I think if you have C, C++, Pascal as backend then you can implement one set of features. If you have Java then your language has GC if you use Go as backend you get goroutines and no threads and GC.\n- FWIW: #1 may be partially due to being quite young and not terribly mature (until the 1.0 release *at least*). #2 is also true for C, and JavaScript, and C++, and probably a few other languages used as backend for *something*.\n- You appear to mix things up. OP is talking about a compiler, which may be written in *any* language and use any set of intermediate representations (even none at all), which generates go code, period.\n- Yes I did understand that part. The compilers in the examples I provided just so happen to also be written in Go.\n- At least one of your examples, GoGo, compiles go to something entirely else (unless they are terrible at writing readmes and their code is intentionally misleading). I can't judge the other one. And then there's your second-to-last sentence which is at odds with my observation about IRs.\n- you're right, I did mix things up to some extent. still I'll leave the answer - maybe it'll help someone in the future.","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":40,"estimatedTokens":1068}}781{"id":"stack-3988244","source":"stackoverflow","questionId":3988244,"title":"what's the ELF object file size limitation on 64bit platform?","tags":["gcc","compiler-construction","64-bit","elf","relocation"],"text":"Title: what's the ELF object file size limitation on 64bit platform?\nTags: gcc, compiler-construction, 64-bit, elf, relocation\nSource: Stack Overflow\n\nQuestion:\nAssume the x86 64 and linux platform.\n\nIf you look into the ELF header, the offset are all 64bit.\n\nSo it's easy to create an object file larger than 4G with relocatoin R_X86_64_64. This means a static .o file and executable can be as large as 64bit can represent.\n\nHowever, for shared library, like gcc , we only have R_X86_64_PLT32 relocation. This means that it's impossible to create a shared library large than 4GB, right?\n\nSo if I want to build a huge program, one of the .o file is huge enough (>4GB) , the only choice is static linking?\n\nAny comment is welcome.\n\nThanks,\nlimi\n\n========================================\n\nComments:\n- Thanks a lot. I read the ABI and noticed that gcc starts to support it from 4.3. I am trying it now.","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":225}}782{"id":"stack-11510792","source":"stackoverflow","questionId":11510792,"title":"Is the semantic analysis step in Clang an essential part of the compiler?","tags":["compiler-construction","clang","semantic-analysis"],"text":"Title: Is the semantic analysis step in Clang an essential part of the compiler?\nTags: compiler-construction, clang, semantic-analysis\nSource: Stack Overflow\n\nQuestion:\nI'm trying to understand the ins and outs of Clang, and I'm not really sure about the \"Sema\" library. Is the semantic analysis in the path the compiler takes to compile a program? Or is it only used by the programmer to analyze his/her code?\n\nFrom what I gather, the parser builds an AST, then there are \"AST consumers\" that use the AST to do different things. So, the code generation library turns the AST into IR. And the semantic analysis library uses the AST to analyze the code. Is this understanding correct, or is the semantic analyzer also used for compiling?\n\n========================================\n\nComments:\n- Clang is somewhat a weird thing: Sema does not only check the AST and insert implicit casts/declarations/whatever, but it is also responsible for building the AST itself. It is not a very typical arrangement for a compiler.\n- @SK-logic It's responsible for building the AST? But what does the parser do? And how does the parser communicate its findings with the semantic analyzer?\n- parser is calling Sema straight away, for each complete expression or statement. There are some intermediate structures involved (e.g., for representing parsed but not yet resolved types), but the final Clang AST is produced by Sema.\n- @SK-logic That's a pretty neat idea. So if the parser alone builds the AST, would \"parse tree\" be a better term?\n- yes, it would often be called a \"parse tree\", with all the further transformed intermediate representations being called \"ASTs\". Although this difference is quite vague. In clang, there is no distinct parse tree, it builds a semantically verified (and somewhat transformed) AST straight away (supposedly for performance reasons).\n- @SK-logic So in a more conventional compiler, the semantic analyzer would get a parse tree, then resolve the identifiers to variables, insert the implicit casts, etc? Then the resulting AST would go to the code generator?\n- Yes, most of the compilers will have separate parsing and semantic analysis passes, at least with a top level statements granularity (structures/functions in C).\n- @SK-logic Okay, thanks for answering my questions, it's been really helpful.\n- That would be correct if C++ syntax was completely independent of types. Unfortunately, you cannot even parse `a * b;` correctly without knowing if `a` is a type or not.","metadata":{"transformedAt":"2026-08-18T18:32:17.827Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":624}}783{"id":"stack-27703952","source":"stackoverflow","questionId":27703952,"title":"How to prove left-recursive grammar is not in LL(1) using parsing table","tags":["parsing","compiler-construction","automation","formal-languages","ll-grammar"],"text":"Title: How to prove left-recursive grammar is not in LL(1) using parsing table\nTags: parsing, compiler-construction, automation, formal-languages, ll-grammar\nSource: Stack Overflow\n\nQuestion:\nI have a grammar and would like to prove that it is not in LL(1):\n\n```\nS->SA|A\nA->a\n```\n\nAs it is a left-recursive grammarm, to find the first and sets I eliminated the left recursion and got:\n\n```\nS->AS'\nS'->AS'|Empty\nA->a\n\nfirst of A={a} of S={$}\nfirst of s'={a,ε} of S'={$}\nfirst of S={a} of A={a,$}\n```\n\nBut when I filled in the parsing table, I did not get any cell with 2 entries. Then how is one to prove that the given grammar is not in LL(1)?\n\n========================================\n\nTop Answer:\nFor this Left recursive grammar:\n\n```\nS->SA|A\nA->a\n```\n\nWe can eliminate left recursion because it will give the same result as Previous Left recursive grammar does.\n\n```\nS->AS'\nS'->AS'|Empty\nA->a\n\nfirst of A={a} of S={$}\nfirst of s'={a,ε} of S'={$}\nfirst of S={a} of A={a,$}\n```\n\nSo, for above case actually, we are checking `LL(1)` for modified Left recursive grammar (as it is same).\nBut for following Left-Recursive Grammar:-\n\n```\nE -> E+n/n\n```\n\nWe can not modify that grammar, it will Change associativity of `+` operator.\n\nSo, the only thing we will have to do is checking LL(1) without modifying \n\n```\n(E->E+n/n ).\n```\n\nSo, we can say `E->E+n/n` is not `LL(1)`.\n\n========================================\n\nCode:\n```text\nS->SA|A\nA->a\n```\n\n```text\nS->AS'\nS'->AS'|Empty\nA->a\n\nfirst of A={a}      follow of S={$}\nfirst of s'={a,ε}   follow of S'={$}\nfirst of S={a}       follow of A={a,$}\n```\n\n```text\na\n------------ \nS   S->SA\n    S->A\n-------------\nA   A->a\n```\n\n```text\nS->SA|A\nA->a\n```\n\n```text\nS->AS'\nS'->AS'|Empty\nA->a\n\nfirst of A={a}      follow of S={$}\nfirst of s'={a,ε}   follow of S'={$}\nfirst of S={a}       follow of A={a,$}\n```\n\n```text\nE -> E+n/n\n```\n\n```text\n(E->E+n/n ).\n```\n\n```text\nLL(1)\n```\n\n```text\n+\n```\n\n```text\nE->E+n/n\n```\n\n```text\nLL(1)\n```\n\n========================================\n\nComments:\n- If the grammar is ambiguous (at least one sentence has more than one parse tree), then the grammar is not in LL(1). Now How should I represent the parsing table here ?\n- I know left recursive grammar , ambiguous grammar do not give ll(1) language .but i need to show this using parsing table...How?\n- of (A)={ first of S'} ={a,replacing epsilon with S' i have to write of S and S'} which is {a,$} please suggest me where i am wrong .\n- Your `FIRST()` and `()` computation is correct as per corrected CFG. I'll have to examine the grammar as well as such rules for LL(1) parsing table.\n- Can we say whether it will be ELL(k) or not?","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":139,"estimatedTokens":664}}784{"id":"stack-16634168","source":"stackoverflow","questionId":16634168,"title":"How does object oriented code translate into machine code?","tags":["oop","compiler-construction","compilation"],"text":"Title: How does object oriented code translate into machine code?\nTags: oop, compiler-construction, compilation\nSource: Stack Overflow\n\nQuestion:\nHow exactly does the oop code that I write in C++ or C# for example translate into machine code or in the case of C# into bytecode? I mean, how do the objects translate? \n\nIn procedural programming it's pretty obvious how it looks like after compilation because there is acually a real support for functions in the machine language. But in the case of oop programing there are no objects in machine language.\n\nMy theory is that it compiles the object itself to some sort of C like struct which contains only data (no member functions) and when a memeber function is called, if accepts an addional parameter which is the data struct of the object itself. Am I right?\n\n========================================\n\nTop Answer:\nNo need to guess, just look at the generated \"assembly\" (MSIL, in fact) code:\n\nHow can I view MSIL / CIL generated by C# compiler? Why is it called assembly?\n\n========================================\n\nComments:\n- And what about the c++ case? Both of them should look in the same structure or memory layout in the end no? There can be more than one way to do this? Because for example you can have this struct with the functions as well but I thinm it's unlikely because it costs more memory if objects duplicate the same functions over ans over.\n- Write a \"hallo world\" C program. Compile it with gcc -o hw hw.c. Issue objdump -d hw. It's the same story, what changes are just the tools names and the specific instructions. IOW the target cpu is different: it's virtual in .net, real in C++.","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":23,"estimatedTokens":415}}785{"id":"stack-14997414","source":"stackoverflow","questionId":14997414,"title":"Obfuscating python bytecode through interpreter mutation","tags":["python","compiler-construction","ctypes","packaging","cpython"],"text":"Title: Obfuscating python bytecode through interpreter mutation\nTags: python, compiler-construction, ctypes, packaging, cpython\nSource: Stack Overflow\n\nQuestion:\nActually, Dropbox made it very well, they were able to secure their desktop application made in python; I researched this a lot, but no good solution better than obfuscation, which is not very secure way to go, and you will end up seeing your code uploaded somewhere.\n\nI listened to a session made by **Giovanni Bajo** (the PyInstaller founder), he said Dropbox does this:\n\nBytecode-scrambling by recompiling your CPython's interpreter, and\nby this, standard CPython interpreter will not be able to run it,\nonly the recompiled cpython interpreter.\nAll what you need to do is to shuffle the numbers below the `define\nloadup 8`.\n\nI've never gone through Python's source code, so, I will not claim that I fully understand the above words. \n\nI need to hear the voice of experts: How to do such a thing? And if after recompilation I will be able to package my application using the available tools like PyInstaller?\n\n**Update:**\n\nI made some research regarding how Dropbox does this type of obfuscation/mutation, and I found this:\n\nAccording to Hagen Fritsch, they do it in two stages:\n\nThey use TEA cipher along with an RNG seeded by some values in the\ncode object of each python module. They adjusted the interpreter\naccordingly so that it \n\na) Decrypts the modules and\n\nb) Prevents access to the decrypted code-objects.\n\nThis would have been the straightforward path just letting dropbox decrypt everything and dump the modules using the builtin marshaller.\n\nAnother trick used is the manual scrambling of the opcodes.\nUnfortunately this could only be fixed semiautomatically thus their\nmonoalphabetic substitution cipher proved quite effective in terms\nof winning some time.\n\nI still want more insights on how this could be done, more over, I don't know how the decryption happens in this process... I want all the experts' voice here ... common guys where are you.\n\n========================================\n\nCode:\n```text\ndefine\nloadup 8\n```\n\n```text\ninclude/opcode.h\n```\n\n```text\n#define loadup\n```\n\n```text\n.pyc\n```\n\n```text\n.pyc\n```\n\n========================================\n\nComments:\n- Similar, more recent question: Undecompilable Python\n- Thanks Janne. Very similar to what you said, adding to that, the removal of introspection modules in the newly created/shuffled interpreter.\n- at the end of the day, the bytecode will be extractable from the memory, then, the attacker can do the comparison between the new shuffled bytecode and the standard one to know how the shuffling was; but, let's be fair, couldn't the same attacker decompile a program written in C to get the source code, nothing is secure against reversing, it's a trade-off ... that's how I see things, may be I'm wrong.\n- Well, is that all? We only change the numbers in `opcode.h`, recompile python interpreter, package my app, then ship it to the customer? Will that produce a Dropbox-like app with the same level of security?\n- @securecurve Expanded my answer. I don't know what level of security the Dropbox app has.\n- At this point, the extraction of those opcodes will not be straightforward, the attacker will have to use disassembles/decompilers and debugging tools to watch those opcodes in memory ... Am I talking correctly or will this happen differently?\n- +1 for updating the answer 2 times for improvement, yet I won't check-mark it now for more answers ...\n- @securecurve Yeah, I'm also interested if anyone has more to say on the subject.\n- Hey dude, check my answer and let me know what do you think","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":5,"totalLines":78,"estimatedTokens":913}}786{"id":"stack-3609110","source":"stackoverflow","questionId":3609110,"title":"standalone grammar and parser for php","tags":["php","c++","c","parsing","compiler-construction"],"text":"Title: standalone grammar and parser for php\nTags: php, c++, c, parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\ni'm looking for a ready-made grammar and parser for php (at least 5.2), ideally an utility/library that can parse php code into a readable AST, e.g. xml. The parser itself doesn't have to be written in php, the source language doesn't matter much.\n\n========================================\n\nTop Answer:\nOur DMS Software Reengineering Toolkit is generalized compiler technology used to parse/analyzer/transform arbitrary computer langauges.\nIt parses to ASTs, and has support for building symbol tables, and various types of flow graphs.\n\nIt has a PHP Front End that is fully PHP 5.x compliant, automatically builds full ASTs, using DMS as a foundation. It can export XML, but our experience (and the design of DMS) says you get a lot more milage by staying \"inside\" DMS with the AST data structure, doing your work there, with DMS's huge library of AST manipulation and pattern matching facilities, and then generating your result, rather than trying to handle the huge amounts of XML that you will get.\n\nThis front end has been used in a number of production tools.\n\nEDIT: October 2016: Now handles PHP 5.6 and PHP 7.\n\n========================================\n\nCode:\n```text\nphc --dump-xml=ast foo.php > bar.xml\n```\n\n========================================\n\nComments:\n- I guess there is no official grammar of PHP.\n- This appears to produce only a stream of tokens wrapped in XML tags. It does not look like a parse tree in spite of what the target web site says.","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":31,"estimatedTokens":399}}787{"id":"stack-5278909","source":"stackoverflow","questionId":5278909,"title":"C++/CLI array initializer compilation error","tags":["compiler-construction","c++-cli"],"text":"Title: C++/CLI array initializer compilation error\nTags: compiler-construction, c++-cli\nSource: Stack Overflow\n\nQuestion:\nCan someone explain why the following code won't compile (formatted oddly to make it a touch easier to see the problem):\n\n```\nListView ^ listview = gcnew ListView();\nlistview->Items->AddRange( gcnew array { \n gcnew ListViewItem( gcnew array { L\"red\", L\"fish\" } ), \n gcnew ListViewItem( gcnew array { L\"green\", L\"eggs\" } ) \n});\n```\n\nThis gives a compile error of\n\n**error C2440: 'initializing' : cannot convert from 'const wchar_t[4]' to 'System::Windows::Forms::ListViewItem ^'**\n\nIf the code is broken into two lines as follows, then all is well:\n\n```\nListView^ listview = gcnew ListView();\nListViewItem^ lvi1 = gcnew ListViewItem( gcnew array { L\"red\", L\"fish\" } );\nListViewItem^ lvi2 = gcnew ListViewItem( gcnew array { L\"green\", L\"eggs\" } );\nlistview->Items->AddRange( gcnew array { \n lvi1, \n lvi2 \n});\n```\n\nIgnoring why someone wants to make a monolithic one-liner to populate a ListView, why does the compiler have trouble instatiating the ListViewItems in the original code, and how would such a one liner be written?\n\n========================================\n\nCode:\n```text\nListView ^ listview = gcnew ListView();\nlistview->Items->AddRange( gcnew array<ListViewItem^> { \n    gcnew ListViewItem( gcnew array<String^> { L\"red\", L\"fish\" } ), \n    gcnew ListViewItem( gcnew array<String^> { L\"green\", L\"eggs\" } ) \n});\n```\n\n```text\nListView^ listview = gcnew ListView();\nListViewItem^ lvi1 = gcnew ListViewItem( gcnew array<String^> { L\"red\", L\"fish\" } );\nListViewItem^ lvi2 = gcnew ListViewItem( gcnew array<String^> { L\"green\", L\"eggs\" } );\nlistview->Items->AddRange( gcnew array<ListViewItem^> { \n    lvi1, \n    lvi2 \n});\n```\n\n```text\n1>c:\\projects\\cpptemp26\\Form1.h(77) : error C2552: '$S4' : non-aggregates cannot be initialized with initializer list\n1>        'System::Windows::Forms::ListViewItem ^' is not an array or class : Types which are not array or class types are not aggregate\n1>c:\\projects\\cpptemp26\\Form1.h(78) : error C2440: 'initializing' : cannot convert from 'const wchar_t [6]' to 'System::Windows::Forms::ListViewItem ^'\n1>        Reason: cannot convert from 'const wchar_t *' to 'System::Windows::Forms::ListViewItem ^'\n1>        No user-defined-conversion operator available, or\n1>        Cannot convert an unmanaged type to a managed type\n```\n\n========================================\n\nComments:\n- Really, it ought to get fixed when support for C++0x aggregate syntax is added. It would have been better if Microsoft hadn't struck out on their own and tried to make this work without a consensus from ISO C++.\n- Okay, it's good that I'm not missing something silly. I've taken the advice and posted at microsoft's bug reporting site and will update here if anything noteworthy materializes. connect.microsoft.com/VisualStudio/feedback/details/650984/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":68,"estimatedTokens":728}}788{"id":"stack-12729828","source":"stackoverflow","questionId":12729828,"title":"Difference between runtime dynamic binding and class inheritance","tags":["java","c++","python","ruby","compiler-construction"],"text":"Title: Difference between runtime dynamic binding and class inheritance\nTags: java, c++, python, ruby, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am trying to clarify the concept of runtime dynamic binding and class inheritance in dynamic languages (Python, ruby) and static type languages (java, C++). I am not sure I am right or not.\n\nIn dynamic languages like Python and Ruby, runtime dynamic binding is implemented as duck typing. When the interpreter checks the type of an object, it checks whether the object has the specific method (or behaviour) rather than check the type of the object; and runtime dynamic binding does not mean class inheritence. Class inheritance just reduce code copy in Python and Ruby.\n\nIn static typed languages like Java and C++, runtime dynamic binding can be obtained only class inheritance. Class inheritance not only reduces code copy here, but is also used to implement runtime dynamic binding.\n\n**In summary, class inheritance and runtime dynamic binding are two difference concepts. In Python and Ruby, they are totally different; in Java and C++ they are mixed together.**\n\nAm I right?\n\n========================================\n\nComments:\n- I mean that in Java and C++, runtime dynamic binding can be obtained only via class inheritance.\n- Still disagree with that. A given class might implement runtime dynamic binding by nothing more than loading a third-party library, obtaining a function pointer, and calling a method across it. That's about the most primitive runtime binding you can get, but it has nothing to do inherently with class inheritance. That said, I have qualified my answer above with a note about Java.\n- Cool, good question, made me think about it, read a little bit :)","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":437}}789{"id":"stack-42272029","source":"stackoverflow","questionId":42272029,"title":"Writing languages for the JVM","tags":["java","compiler-construction","jvm","jvm-bytecode"],"text":"Title: Writing languages for the JVM\nTags: java, compiler-construction, jvm, jvm-bytecode\nSource: Stack Overflow\n\nQuestion:\nSuppose I write a programming language; for namesake, I'll call it **lang**.\n\nTo begin the long journey of writing **lang**, I decide to begin, by writing lang in itself. I can't actually *run* it, because theres nothing to run the program that runs itself.\n\nSo I begin by writing another compiler for **lang** in Java. This time, when I am done, I decide to convert it to Bytecode, and leave it at that. I now have a working compiler, which will convert all my **lang** code into Bytecode.\n\nSo I decide to plug in my self-compiler for the language, into the compiler I just made in Java. I then convert the self-compiler to Bytecode, and chuck out the Java compiler. I now have a **lang** compiler, purely written in itself, converted into Bytecode, ready for use.\n\nThis creates a solid program, and I understand all of this, but my question is, relative to compiler design for the JVM, **what if I decide to release an update for my language? How do I go about updating the Bytecode? Do I simply re-write the updated version of the language in the older one?**\n\nI ask this because this is what I want to do. Write a non-existing language in itself, and then bootstrap it to the JVM by firstly creating a compiler in Java.\n\nIt's the same as what was done with C++. C with Classes was written, and then C++ in it, and finally C with Classes was abandoned for the bootstrapped C++. But then how on earth did they ever go about updating the language?\n\n========================================\n\nTop Answer:\nI don't think your compiler is the best way to go about this.\n\nI'd start with a grammar for my language.\n\nNext comes the lexer/parser to turn expressions in my language to an abstract syntax tree (AST). The AST is a correct intermediate representation of the expression. \n\nYou would emit bytecode or assembly language instructions for the virtual machine or processor of your choice by writing a code generator that traverses the AST. \n\nWhere does your update happen? \n\nIf it's language fundamentals, you have to modify both the grammar and the bytecode emission.\n\nIf you're optimizing the bytecode or porting to a new processor you have to modify the code generator.\n\n========================================\n\nComments:\n- Debugging byte code can be pretty painful. I suggest you write a translator of `lang` into `java` This way you c an see what it is doing and debug the translated Java. Later you can write a more efficient compiler straight to byte code.\n- @Peter Lawrey: “Debugging byte code” is rarely needed. If your generated class file has debug attributes specifying the source file name and mapping of instructions to line numbers, you can debug it as smooth as Java source code, regardless of which language it actually was written in.\n- If you upgrade your language, you have to implement the newer features using the features of the previous language only. Only after the completion of that, you can start using the new features within the compiler.\n- Thankyou! However when you specify language fundamentals, as I modify the bytecode emission, should I be writing the emitter in Java, or the previous version or a commit of the language in question?\n- A grammar also is a good basis for a language reference. However then you tie yourself to the java parser implementation language until the parser is written in lang. But +1\n- I didn't say it had to be a Java parser. Why cares if the parser is written in the original language? Use bison if you wish.","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":45,"estimatedTokens":899}}790{"id":"stack-20683692","source":"stackoverflow","questionId":20683692,"title":"LR(k) to LR(1) grammar conversion","tags":["parsing","compiler-construction","bison","lr-grammar"],"text":"Title: LR(k) to LR(1) grammar conversion\nTags: parsing, compiler-construction, bison, lr-grammar\nSource: Stack Overflow\n\nQuestion:\nI am confused by the following quote from Wikipedia:\n\n In other words, if a language was reasonable enough to allow an\n efficient one-pass parser, it could be described by an LR(k) grammar.\n And that grammar could always be mechanically transformed into an\n equivalent (but larger) LR(1) grammar. So an LR(1) parsing method was,\n in theory, powerful enough to handle any reasonable language. In\n practice, the natural grammars for many programming languages are\n close to being LR(1).[citation needed]\n\nThis means that a parser generator, like `bison`, is very powerful (since it can handle `LR(k)` grammars), if one is able to convert a `LR(k)` grammar to a `LR(1)` grammar. Do some examples of this exist, or a recipe on how to do this? I'd like to know this since I have a shift/reduce conflict in my grammar, but I think this is because it is a `LR(2)` grammar and would like to convert it to a `LR(1)` grammar. Side question: is `C++` an unreasonable language, since I've read, that `bison`-generated parsers cannot parse it.\n\n========================================\n\nCode:\n```text\nbison\n```\n\n```text\nLR(k)\n```\n\n```text\nLR(k)\n```\n\n```text\nLR(1)\n```\n\n```text\nLR(2)\n```\n\n```text\nLR(1)\n```\n\n```text\nC++\n```\n\n```text\nbison\n```\n\n```text\nLR(1)\n```\n\n```text\nLR(k)\n```\n\n```text\nLR(k)\n```\n\n```text\n(a)*b\n```\n\n```text\na\n```\n\n```text\nc/(a)*b\n```\n\n```text\nx<y>(z)\n```\n\n```text\nx<y<z>>(3)\n```\n\n```text\nx\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":17,"totalLines":87,"estimatedTokens":383}}791{"id":"stack-9856152","source":"stackoverflow","questionId":9856152,"title":"Coding in x86 - compiler code generation","tags":["compiler-construction","x86"],"text":"Title: Coding in x86 - compiler code generation\nTags: compiler-construction, x86\nSource: Stack Overflow\n\nQuestion:\nIs there a good reference for writing code in x86 assembly. I am writing a compiler and i am currently in the code-generation phase. The language i am implementing is Object Oriented. For example now i am having trouble writing assembly for class declarations, object layouts etc... is there any book or reference that covers this topic ?\n\n========================================\n\nTop Answer:\nThere can be \"direct\" and \"useful\" answers to your question and they are likely incompatible.\n\nThe direct answer is that you should combine architecture reference (see link in previous answer) with details of the exact assembler you selected for this - e.g. gas, nasm, yasm, fasm, masm[32], tasm, etc.; all they have references on instruction syntax, pseudoinstructions (as segmentation, memory allocation, etc.), implementation details, object file format...\n\nThe useful (jIMHO) answer is that duplicating of results of industry leaders, as GNU Compiler Collection, Microsoft Developer Studio, etc. requires thousands of man-years so this is quite weird way; instead you should reuse existing measures as much as possible. For example, to verify the concept of your language you can implement convertor to C code and support library for this. Or, write for a well-known virtual machine and its language as Java or C#. On advanced level, you can replace compiler frontend with your own one (as GCC and LLVM allow) and reuse their code generating backends which are well written and tuned for multiple targets.\n\nI quite doubt you are designing something that can't be converted to C and support library calls. The intermediate code shan't be fine looking, it shall just work.\n\nIf you still want to do sunset manually, please edit the question with more details on target platform (Windows/Linux/etc., 16- 32- or 64-bit...)\n\n========================================\n\nComments:\n- Your question is a big vague. There's should be no assembly involved for class declarations or object layouts.\n- Maybe you can read the clang/llvm source code? In my experience it's been very easy to understand.\n- What do you mean no assembly involved for class declarations. What if i have a class with multiple methods and fields? if i dont code the methods in assembly then where/when will the be declared if they are used later on ?\n- @MikeG: Class declarations are the whole enchilada; code (that is, executable statements) within them is more pertinent to assembly generation.\n- This is pretty straight forward. You can oop up to high heaven with your language but the cpu is completely agnostic of language design. It just implements the same tired old instruction set that has been around for the past 34 years, nothing oopy about it. The investment here is in your language compiler first, your code generator next.\n- OOP shouldn't exist anymore in the code generation stage.\n- Can you please be more specific, for example is there a reference that explains how method decelerations are translated into assembly etc ... that would be great\n- @MikeG: Method declaration translation to code is not a CPU architecture issue, which is what the documents are for. The job of a compiler writer is to create code which compiles source code into object code. Since you have chosen the challenge of writing a compiler, we expect that you *already* understand how to generate parse trees from source and translate those into pseudo instructions (or real instructions) targeting a particular architecture.\n- @MikeG: As stated already, stuff like laying out stack frames; putting args on the stack for method calls; and how to layout fields, dispatch table pointers, etc. in memory objects, aren't tied to an instruction set. There's just a way of doing it. You should be looking into the Dragon Book.\n- @MikeG: Or \"Modern Compiler Implementation in C/Java/ML\"","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":33,"estimatedTokens":986}}792{"id":"stack-8898049","source":"stackoverflow","questionId":8898049,"title":"How to convert a regular grammar to regular expression?","tags":["regex","compiler-construction","bnf"],"text":"Title: How to convert a regular grammar to regular expression?\nTags: regex, compiler-construction, bnf\nSource: Stack Overflow\n\nQuestion:\nIs there an algorithm or tool to convert regular grammar to regular expression?\n\n========================================\n\nTop Answer:\nThe algorithm is pretty straightforward if you can compute an automaton from your regular expression. Once you have your automaton. For instance for `(aa*b|c)`, an automaton would be (arrows go to the right): \n\n```\na\n / \\\n a \\ / b\n-> 0 ---> 1 ---> 2 ->\n \\___________/\n c\n```\n\nThen just \"enumerate\" your transitions as rules. Below, consider that 0, 1, and 2 are nonterminal symbols, and of course a, b and c are the tokens.\n\n```\n0: a1 | c2\n1: a1 | b2\n2: epsilon\n```\n\nor, if you don't want empty right-hand sides.\n\n```\n0: a1 | c\n1: a1 | b\n```\n\nAnd of course, the route in the other direction provides one means to convert a regular grammar into an automaton, hence a rational expression.\n\n========================================\n\nCode:\n```text\na\n         / \\\n      a  \\ / b\n-> 0 ---> 1 ---> 2 ->\n    \\___________/\n          c\n```\n\n```text\n0: a1 | c2\n1: a1 | b2\n2: epsilon\n```\n\n```text\n0: a1 | c\n1: a1 | b\n```\n\n```text\n(aa*b|c)\n```\n\n```text\nP:\n   S -> aA | cS | a  | c\n   A -> aA | a  | bS\n```\n\n```text\nS = aA + cS + a + c\nA = aA + bS + c\n```\n\n```text\nA = a(aA + bS + c) + bS + c\nA = a⁺bS + a⁺c + bS + c  \n\nS = aA + c(aA + cS + a + c)\nS = aA + c⁺aA + c⁺a + c⁺\n\nS = a(a⁺bS + a⁺c + bS + c) + c⁺a(a⁺bS + a⁺c + bS + c) + c⁺a + c⁺\nS = a⁺bS + a⁺c + c⁺a⁺bS + c⁺a⁺c + c⁺a + c⁺\n\nS = (c⁺ + ε)a⁺bS + a⁺c + c⁺(a⁺c + a + ε)\n\nsubstitution: x = (c⁺ + ε)a⁺b\n\nS = x(xS + a⁺c + c⁺(a⁺c + a + ε)) + a⁺c + c⁺(a⁺c + a + ε)\nS = x⁺a⁺c + x⁺c⁺(a⁺c + a + ε) + a⁺c + c⁺(a⁺c + a + ε)\nS = x*(a⁺c + c⁺(a⁺c + a + ε))\n\nS = ((c⁺ + ε)a⁺b)*(⁺a⁺c + c⁺(a⁺c + a + ε))\n```\n\n```text\n({S,A},{a,b,c},P,S)\n```\n\n```text\nS\n```\n\n```text\n((c⁺ + ε)a⁺b)*(⁺a⁺c + c⁺(a⁺c + a + ε))\n```\n\n```text\nS = xS | x\n```\n\n```text\nS = x⁺\n```\n\n```text\nright_linear_grammar_to_nfa\n```\n\n========================================\n\nComments:\n- You can look into regexmagic.com if easily creating the expression is your purpose.\n- My goal is to convert regular grammer to DFA. Finally, I found an excellent tool : jflap.org/jflaptmp .\n- JFLAP looks very nice indeed. Thanks for the link.\n- @dalibocai: could you update your title '.. to DFA', answer it and mark as answer for better search results ? TY\n- The link is broken.\n- @BenediktS.Vogler Link fixed. Thanks for your comment.\n- Regular grammars can provide loops that cannot be easily translated backwards: `({A, B, C, D}, {a, b, c, d}, {A -> aB, B -> bC, C -> cA, C -> cD, D -> dB, D -> d}, A)`, where you have the loop `A -> B -> C -> A` and the loop `B -> C -> D -> A` that overlap.\n- @CodenameLambda I don't understand what you mean. It is straightforward to build an automaton from your grammar, and the state elimination method, or just any aut-to-exp method will give you a result. Loops, overlapping or not, are irrelevant.\n- Your answer only gives a rough description for an algorithm from regex to regular grammar, which can be followed backwards most of the time, but not always. So while there are methods that can properly translate every regular grammar to a regular expression, reversing the process you described can't handle all regular grammars.","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":133,"estimatedTokens":830}}793{"id":"stack-9476379","source":"stackoverflow","questionId":9476379,"title":"How to generate code for AST tree parsed from a fictive language?","tags":["ruby","parsing","compiler-construction"],"text":"Title: How to generate code for AST tree parsed from a fictive language?\nTags: ruby, parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI read the article over at http://parsingintro.sourceforge.net/ and decided to try to rewrite it as an exercise in Ruby. Two reasons made me do this, I wanted to learn more about how to code Ruby (background in Java, PHP, C and some Python) and I wanted to learn more about parsers / compilers.\n\nI have all the code posted at https://github.com/parse/boatcaptain. The AST tree is being generated, unfortunatly the author of the article doesn't get into concepts such as code generation and optimizations.\n\nCan anyone help me by pointing me in the right direction on how to achieve this AST tree into \"code\"? This is the AST tree that is generated\n\nI wrote a calculator in Java a few years ago, it uses a lot of similar terminology and techniques as I used in this parser. But in the calculator I had methods for eval()-ing my \"classes\" and therefore getting output, should I aim for doing something similar here? Source for calculator: https://github.com/parse/Uppsala-University-Courses/blob/master/ImpOOP-Calculator/src/Calculator.java\n\nI would love feedback on my way of writing Ruby as well, I believe I still write Ruby like I would write Python, missing some nice advantages of Ruby.\n\n========================================\n\nCode:\n```text\n=\ndelta       /\n      alpha   beta\n```\n\n```text\nload alpha\nload beta\ntmp = div alpha beta\nstore delta tmp\n```\n\n========================================\n\nComments:\n- If you want to learn how to do code generation, read a book on how to do code generation. Aho and Ullman \"Compilers\" is a classic. Don't expect this to be easy, if you want a good code generation; you aren't going to code your way to nirvana without significant knowledge.","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":35,"estimatedTokens":459}}794{"id":"stack-12204947","source":"stackoverflow","questionId":12204947,"title":"Compile-file with in-memory data instead of real file in Common Lisp","tags":["compiler-construction","common-lisp"],"text":"Title: Compile-file with in-memory data instead of real file in Common Lisp\nTags: compiler-construction, common-lisp\nSource: Stack Overflow\n\nQuestion:\nIs there a way to achieve the effects of `compile-file` but not with a regular file on disk, but with a stream or just an in-memory string? (I.e. if I don't have a file and don't want to create temporary files out of this in-memory data)\n\n**EDIT**\n\nI'm thinking of the following use case: loading code from some other places, than the file system. For example from archives (similar to Java's jars or Python zip handling capabilities) or from the network. Maybe there may be alternative approaches to this, than just bending `compile-file` machinery.\n\n========================================\n\nCode:\n```text\ncompile-file\n```\n\n```text\ncompile-file\n```\n\n```text\n(let* ((*package* *package*)\n       (*readtable* *readtable*)\n       (eof (copy-symbol 'eof))\n       (form nil))\n  (loop\n    (setf form (read stream nil eof))\n    (when (eq form eof)\n      (return))\n    (funcall (compile nil `(lambda () ,form)))))\n```\n\n```text\ncompile-file\n```\n\n```text\ncompile-file\n```\n\n```text\nload\n```\n\n```text\nwith-compilation-unit\n```\n\n```text\nmacroexpand\n```\n\n```text\nprogn\n```\n\n```text\nlocally\n```\n\n```text\nmacrolet\n```\n\n```text\nsymbol-macrolet\n```\n\n```text\neval-when\n```\n\n```text\nmake-load-form\n```\n\n```text\nstandard-object\n```\n\n```text\nstructure-object\n```\n\n```text\ncondition\n```\n\n```text\nin-package\n```\n\n```text\nload\n```\n\n```text\n*package*\n```\n\n```text\nload-time-value\n```\n\n```text\ncompile-file\n```\n\n```text\nload\n```\n\n```text\ncompile-file\n```\n\n```text\ncompile-file\n```\n\n```text\ncompile-and-load\n```\n\n```text\ncompile-file\n```\n\n```text\noutput-file\n```\n\n```text\nload\n```\n\n========================================\n\nComments:\n- @RainerJoswig I'm also interested in nonportable approaches\n- You probably know about this (xach.livejournal.com/131456.html), but I'm mentioning it here just in case. Peter Seibel's approach (mentioned in the above post - gigamonkeys.com/blog/2007/07/27/&hellip;) doesn't use `compile-file`, just `compile`, but maybe you can adapt your solution to use this? Or better, use trees of closures?\n- @MironBrezuleanu thanks for the link, but I'm really interested in compilation of certain units of code, as if they were in a file. I've updated the question to clarify this\n- @VsevolodDyomkin from your edit I gather you want `load`, not `compile-file` (you mention 'loading code from some other places'). The CLHS entry for `compile-file` doesn't seem to imply that compiling a file also means loading. The CLHS entry for `load` mentions that the first argument can be a stream - isn't this what you need?\n- @MironBrezuleanu actually, I'm actually interested in both, because I was looking for ways to hook into ASDF. But you're right, that I didn't pay enough attention to the semantics of `load` being different from that of `compile-file`. At least for this use case, I think, it might actually be enough. Thanks a lot for the hint!\n- perhaps with-compilation-unit can help?","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":29,"totalLines":147,"estimatedTokens":759}}795{"id":"stack-4810657","source":"stackoverflow","questionId":4810657,"title":"Compile String to AST inside CompilerPlugin?","tags":["scala","compiler-construction","scala-2.8"],"text":"Title: Compile String to AST inside CompilerPlugin?\nTags: scala, compiler-construction, scala-2.8\nSource: Stack Overflow\n\nQuestion:\nI would like to create a templating plugin and as the first step convert an arbitrary string to it's \"compiled\" AST representation (as the scala interpreter does, I guess). So a compiler plugin could e.g assign someString to \"HELLO WORLD\":\n\n```\n@StringAnnotation(\"\"\"(\"hello world\").toString.toUpperCase\"\"\")\n var someString = \"\"\n```\n\nMy current first shot plugin does in short:\n\n- runafter parser\n\n- create a new representation only compiler and a VirtualFile with the annotation content\n\n- compile and print unit.body\n\nsee: http://paste.pocoo.org/show/326025/\n\na)\nRight now, `\"object o{val x = 0}\"` returns an AST, but e.g. `\"var x = 1+ 2\"` doesn't because it wouldn't be a valid .scala file. How can I fix this?\n\nb)\nIs onlyPresentation a good choice? Should I instead overriding computeInternalPhases with the appropriate phases or use -Ystop:phase?\n\nc)\nIs it possible to bind the environment of the outer compiler to the inner one, so that e.g.\n\n```\nvar x = _\n (...)\n @StringAnnotation(\"x += 3\")\n```\n\nwould work?\n\nI found following code[1] using an interpreter and one variable which does something similar:\n\n```\nInterpreter interpreter = new Interpreter(settings);\n String[] context = { \"FOO\" };\n interpreter.bind(\"context\", \"Array[String]\", context);\n interpreter\n .interpret(\"de.tutorials.scala2.Test.main(context)\");\n context[0] = \"BAR\";\n interpreter\n .interpret(\"de.tutorials.scala2.Test.main(context)\");\n```\n\n[1] http://www.tutorials.de/java/320639-beispiel-zur-einbindung-des-scala-interpreters-kompilierte-scala-anwendungen.html#post1653884\n\nthanks\n\nComplete Code:\n\n```\nclass AnnotationsPI(val global: Global) extends Plugin {\n import global._\n val name = \"a_plugins::AnnotationsPI\" //a_ to run before namer\n val description = \"AST Trans PI\"\n val components = List[PluginComponent](Component)\n\n private object Component extends PluginComponent with Transform with TypingTransformers with TreeDSL {\n val global: AnnotationsPI.this.global.type = AnnotationsPI.this.global\n val runsAfter = List[String](\"parser\");\n val phaseName = AnnotationsPI.this.name\n\n def newTransformer(unit: CompilationUnit) = {\n new AnnotationsTransformer(unit)\n }\n\n val SaTpe = \"StringAnnotation\".toTypeName\n\n class AnnotationsTransformer(unit: CompilationUnit) extends TypingTransformer(unit) {\n\n /** When using `preTransform`, each node is\n * visited before its children.\n */\n def preTransform(tree: Tree): Tree = tree match {\n case anno@ValDef(Modifiers(_, _, List(Apply(Select(New(Ident(SaTpe)), _), List(Literal(Constant(a))))), _), b, c, d) => //Apply(Select(New(Ident(SaTpe)), /*nme.CONSTRUCTOR*/_), /*List(x)*/x)\n val str = a.toString\n val strArr = str.getBytes(\"UTF-8\")\n import scala.tools.nsc.{ Global, Settings, SubComponent }\n import scala.tools.nsc.reporters.{ ConsoleReporter, Reporter }\n\n val settings = new Settings()\n val compiler = new Global(settings, new ConsoleReporter(settings)) {\n override def onlyPresentation = true\n }\n\n val run = new compiler.Run\n val vfName = \"Script.scala\"\n var vfile = new scala.tools.nsc.io.VirtualFile(vfName)\n\n val os = vfile.output\n os.write(strArr, 0, str.size) // void write(byte[] b, int off, int len) \n os.close\n new scala.tools.nsc.util.BatchSourceFile(vfName, str)\n run.compileFiles(vfile :: Nil)\n for (unit \n tree\n }\n\n override def transform(tree: Tree): Tree = {\n super.transform(preTransform(tree))\n }\n }\n }\n```\n\n========================================\n\nCode:\n```text\n@StringAnnotation(\"\"\"(\"hello world\").toString.toUpperCase\"\"\")\n  var someString = \"\"\n```\n\n```text\nvar x = _\n  (...)\n  @StringAnnotation(\"x += 3\")\n```\n\n```text\nInterpreter interpreter = new Interpreter(settings);\n  String[] context = { \"FOO\" };\n  interpreter.bind(\"context\", \"Array[String]\", context);\n  interpreter\n    .interpret(\"de.tutorials.scala2.Test.main(context)\");\n  context[0] = \"BAR\";\n  interpreter\n    .interpret(\"de.tutorials.scala2.Test.main(context)\");\n```\n\n```text\nclass AnnotationsPI(val global: Global) extends Plugin {\n  import global._\n  val name = \"a_plugins::AnnotationsPI\" //a_ to run before namer\n  val description = \"AST Trans PI\"\n  val components = List[PluginComponent](Component)\n\n  private object Component extends PluginComponent with Transform with TypingTransformers with TreeDSL {\n    val global: AnnotationsPI.this.global.type = AnnotationsPI.this.global\n    val runsAfter = List[String](\"parser\");\n    val phaseName = AnnotationsPI.this.name\n\n    def newTransformer(unit: CompilationUnit) = {\n      new AnnotationsTransformer(unit)\n    }\n\n    val SaTpe = \"StringAnnotation\".toTypeName\n\n    class AnnotationsTransformer(unit: CompilationUnit) extends TypingTransformer(unit) {\n\n      /** When using <code>preTransform</code>, each node is\n       *  visited before its children.\n       */\n      def preTransform(tree: Tree): Tree = tree match {\n        case anno@ValDef(Modifiers(_, _, List(Apply(Select(New(Ident(SaTpe)), _), List(Literal(Constant(a))))), _), b, c, d) => //Apply(Select(New(Ident(SaTpe)), /*nme.CONSTRUCTOR*/_), /*List(x)*/x)\n          val str = a.toString\n          val strArr = str.getBytes(\"UTF-8\")\n          import scala.tools.nsc.{ Global, Settings, SubComponent }\n          import scala.tools.nsc.reporters.{ ConsoleReporter, Reporter }\n\n          val settings = new Settings()\n          val compiler = new Global(settings, new ConsoleReporter(settings)) {\n            override def onlyPresentation = true\n          }\n\n          val run = new compiler.Run\n          val vfName = \"Script.scala\"\n          var vfile = new scala.tools.nsc.io.VirtualFile(vfName)\n\n          val os = vfile.output\n          os.write(strArr, 0, str.size) // void  write(byte[] b, int off, int len) \n          os.close\n          new scala.tools.nsc.util.BatchSourceFile(vfName, str)\n          run.compileFiles(vfile :: Nil)\n          for (unit <- run.units) {\n            println(\"Unit: \" + unit)\n            println(\"Body:\\n\" + unit.body)\n          }\n          tree\n\n        case _ =>\n          tree\n      }\n\n      override def transform(tree: Tree): Tree = {\n        super.transform(preTransform(tree))\n      }\n    }\n  }\n```\n\n```text\n\"object o{val x = 0}\"\n```\n\n```text\n\"var x = 1+ 2\"\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":206,"estimatedTokens":1566}}796{"id":"stack-2622686","source":"stackoverflow","questionId":2622686,"title":"Grammar that is LR(1) but not LL(1)","tags":["compiler-construction","grammar"],"text":"Title: Grammar that is LR(1) but not LL(1)\nTags: compiler-construction, grammar\nSource: Stack Overflow\n\nQuestion:\nThis might look like a basic question to some of you but I expect intelligent replies here.\n\nWhy can't a LR(1) grammar with `left recursion` or the LR(1) grammar that is **not** `left factored` be LL(1)?\n\n========================================\n\nCode:\n```text\nleft recursion\n```\n\n```text\nleft factored\n```\n\n========================================\n\nComments:\n- do someone expect replies which are not intelligent??\n- @Francesco: That is a better question. xD","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":25,"estimatedTokens":144}}797{"id":"stack-1498621","source":"stackoverflow","questionId":1498621,"title":"Compile C# code extension at runtime","tags":["c#","compiler-construction"],"text":"Title: Compile C# code extension at runtime\nTags: c#, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have a system that compiles C# code at runtime. I would like the generated assemblies to be linked to the system itself. Here's some example code that I am using:\n\n```\nCSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary { { \"CompilerVersion\", \"v3.5\" } });\nCompilerParameters compilerparams = new CompilerParameters();\ncompilerparams.GenerateExecutable = false;\ncompilerparams.GenerateInMemory = true;\nforeach (string name in linkedreferences)\n compilerparams.ReferencedAssemblies.Add(name + \".dll\");\nAssembly result = provider.CompileAssemblyFromFile(compilerparams, filename);\n```\n\nWhat I would like to do is also add a reference to the main compiler program as well, so the newly compiled extension can use library routines from the compiler program.\n\n```\nAssembly entryasm = Assembly.GetEntryAssembly();\n```\n\nSo the question is this: How do I add a reference to entryasm in the compiled Assembly result?\n\n========================================\n\nCode:\n```text\nCSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<String, String> { { \"CompilerVersion\", \"v3.5\" } });\nCompilerParameters compilerparams = new CompilerParameters();\ncompilerparams.GenerateExecutable = false;\ncompilerparams.GenerateInMemory = true;\nforeach (string name in linkedreferences)\n    compilerparams.ReferencedAssemblies.Add(name + \".dll\");\nAssembly result = provider.CompileAssemblyFromFile(compilerparams, filename);\n```\n\n```text\nAssembly entryasm = Assembly.GetEntryAssembly();\n```\n\n```text\ncompilerparams.ReferencedAssemblies.Add(entryasm.Location);\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":45,"estimatedTokens":419}}798{"id":"stack-29352957","source":"stackoverflow","questionId":29352957,"title":"How do I parse children nodes?","tags":["parsing","compiler-construction","grammar","ebnf"],"text":"Title: How do I parse children nodes?\nTags: parsing, compiler-construction, grammar, ebnf\nSource: Stack Overflow\n\nQuestion:\nSo lets say I have the following grammar:\n\n```\nlet_stat = \"let\" iden [ \"=\" expr ];\nif_stat = \"if\" expr \"->\" stat;\nstat = let_stat | if_stat;\n```\n\nThis would be the following psuedo-ish code:\n\n```\nlet_stat parseLetStat() {\n\nif token is \"let\" {\n consume token\n\n if token is identifier {\n char *value = consumetoken.value\n let_stat let = new let_stat;\n let.name = value;\n\n if token is \"=\" {\n let.value = parseExpression;\n }\n\n return let\n }\n }\n}\n\nif_stat parseIfStat() {\n if token is \"if\" {\n consume token\n\n expression expr = parseExpression;\n block block = parseBlock;\n\n if_stat ifstmt = new if_stat\n ifstmt.expr = expr\n ifstmt.block = block\n return ifstmt\n }\n}\n\nstat parseStatement() {\n\n}\n```\n\nWhat would the `parseStatement` function do? How would it choose which function to call, the if_stat function, or the let_stat function? Or would I throw all the code into one function? I don't quite understand, any help would be great since I'm confused.\n\n========================================\n\nCode:\n```text\nlet_stat = \"let\" iden [ \"=\" expr ];\nif_stat = \"if\" expr \"->\" stat;\nstat = let_stat | if_stat;\n```\n\n```text\nlet_stat parseLetStat() {\n\n\nif token is \"let\" {\n        consume token\n\n        if token is identifier {\n            char *value = consumetoken.value\n            let_stat let = new let_stat;\n            let.name = value;\n\n            if token is \"=\" {\n                let.value = parseExpression;\n            }\n\n            return let\n        }\n    }\n}\n\nif_stat parseIfStat() {\n    if token is \"if\" {\n        consume token\n\n        expression expr = parseExpression;\n        block block = parseBlock;\n\n        if_stat ifstmt = new if_stat\n        ifstmt.expr = expr\n        ifstmt.block = block\n        return ifstmt\n    }\n}\n\nstat parseStatement() {\n\n}\n```\n\n```text\nparseStatement\n```\n\n========================================\n\nComments:\n- Oh I see, perfect. And of course, answered from the one and only Ira Baxter, you seem to answer a lot of my questions :) Thanks! :)\n- @user3839220: You're asking questions on my turf :-}","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":114,"estimatedTokens":541}}799{"id":"stack-27700626","source":"stackoverflow","questionId":27700626,"title":"confusion in finding first and in left recursive grammar","tags":["parsing","recursion","compiler-construction","automata","pushdown-automaton"],"text":"Title: confusion in finding first and in left recursive grammar\nTags: parsing, recursion, compiler-construction, automata, pushdown-automaton\nSource: Stack Overflow\n\nQuestion:\nRecently I faced the problem for finding first and \n\n```\nS->cAd\nA->Ab|a\n```\n\nHere I am confused with first of A\nwhich one is correct {a} , {empty,a} as there is left recursion in A's production .\nI am confused whether to include empty string in first of A or not \nAny help would be appreciated.\n-------------edited---------------\n\nwhat wil be the first and of this ,,This is so confusing grammar i have ever seen\n\n```\nS->SA|A\nA->a\n```\n\nI need to prove this grammar is not in LL(1) using parsing table but unable to do because i didnot get 2 entry in single cell.\n\n========================================\n\nCode:\n```text\nS->cAd\nA->Ab|a\n```\n\n```text\nS->SA|A\nA->a\n```\n\n```text\nS -> cAd\nA -> aA'\nA' -> bA' | epsilon\n```\n\n```text\nFIRST(A) = a         // as a is the only terminal nderived first from A.\n```\n\n```text\nS -> AS'\nS' -> AS' | epsilon\nA -> a\n\nFIRST(A) = a\nFIRST(S) = a\nFIRST(S') = {a,epsilon}.\n```\n\n```text\nEDIT :-\n```\n\n```text\nFIRST()\n```\n\n```text\nFOLLOW()\n```\n\n========================================\n\nComments:\n- I already mentioned to ask another question as it's too broad to include second part. Ask second afresh so that a broad answer for why this is not in LL(1) can be given! By the way, I have added left-recursion for you for the 2nd question...\n- Thanks @shekar suman .please help in this stackoverflow.com/questions/27703952/&hellip;\n- Thanks shekhar suman for your nice answer . I understand your explanation.\n- Is grammar LL(1)? Can anyone explain??\n- Hi @John, sorry for the late reply. Please go through this question: stackoverflow.com/q/8496642/3482140, and let me know if you've difficulty evaluating whether the aforementioned grammar is LL(1) or not!\n- Thanks @Am_I_Helpful for your effort but I got my answer already.","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":8,"totalLines":81,"estimatedTokens":481}}800{"id":"stack-10663004","source":"stackoverflow","questionId":10663004,"title":"Why does gcc create redundant assembly code?","tags":["c","optimization","gcc","assembly","compiler-construction"],"text":"Title: Why does gcc create redundant assembly code?\nTags: c, optimization, gcc, assembly, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI wanted to look into how certain C/C++ features were translated into assembly and I created the following file:\n\n```\nstruct foo {\n int x;\n char y[0];\n};\n\nchar *bar(struct foo *f)\n{\n return f->y;\n}\n```\n\nI then compiled this with `gcc -S` (and also tried with `g++ -S`) but when I looked at the assembly code, I was disappointed to find a trivial redundancy in the bar function that I thought `gcc` should be able to optimize away:\n\n```\n_bar:\nLeh_func_begin1:\n pushq %rbp\nLtmp0:\n movq %rsp, %rbp\nLtmp1:\n movq %rdi, -8(%rbp)\n movq -8(%rbp), %rax\n movabsq $4, %rcx\n addq %rcx, %rax\n movq %rax, -24(%rbp)\n movq -24(%rbp), %rax\n movq %rax, -16(%rbp)\n movq -16(%rbp), %rax\n popq %rbp\n ret\nLeh_func_end1:\n```\n\nAmong other things, the lines\n\n```\nmovq %rax, -24(%rbp)\n movq -24(%rbp), %rax\n movq %rax, -16(%rbp)\n movq -16(%rbp), %rax\n```\n\nseem pointlessly redundant. Is there any reason gcc (and possibly other compilers) cannot/does not optimize this away?\n\n========================================\n\nTop Answer:\nThe code the compiler generates without optimization is typically a straight instruction-by-instruction translation, and the instructions are not those of the program but those of an intermediate representation in which redundancy may have been introduced.\n\nIf you expect assembly without such redundant instructions, use `gcc -O -S`\n\nThe kind of optimization you were expecting is called peephole optimization. Compilers usually have plenty of these, because unlike more global optimizations, they are cheap to apply and (generally) do not risk making the code worse—if applied towards the end of the compilation, at least.\n\nIn this blog post, I provide an example where both GCC and Clang may go as far as generating shorter 32-bit instructions when the integer type in the source code is 64-bit but only the lowest 32-bit of the result matter.\n\n========================================\n\nCode:\n```text\nstruct foo {\n    int x;\n    char y[0];\n};\n\nchar *bar(struct foo *f)\n{\n    return f->y;\n}\n```\n\n```text\n_bar:\nLeh_func_begin1:\n        pushq   %rbp\nLtmp0:\n        movq    %rsp, %rbp\nLtmp1:\n        movq    %rdi, -8(%rbp)\n        movq    -8(%rbp), %rax\n        movabsq $4, %rcx\n        addq    %rcx, %rax\n        movq    %rax, -24(%rbp)\n        movq    -24(%rbp), %rax\n        movq    %rax, -16(%rbp)\n        movq    -16(%rbp), %rax\n        popq    %rbp\n        ret\nLeh_func_end1:\n```\n\n```text\nmovq    %rax, -24(%rbp)\n        movq    -24(%rbp), %rax\n        movq    %rax, -16(%rbp)\n        movq    -16(%rbp), %rax\n```\n\n```text\ngcc -S\n```\n\n```text\ng++ -S\n```\n\n```text\ngcc\n```\n\n```text\nbar:\n.LFB0:\n        .cfi_startproc\n        leaq    4(%rdi), %rax\n        ret\n        .cfi_endproc\n```\n\n```text\n-O3\n```\n\n```text\ngcc -O -S\n```\n\n========================================\n\nComments:\n- Please run gcc with -O switch to enable standard optimizations.\n- which version of gcc are you using?\n- More recent duplicate Why does clang produce inefficient asm with -O0 (for this simple floating point sum)? which goes into more detail than the answers here about why some of this makes sense for consistent debugging. (The basic point is the same, the answers here are correct.) Related: How to remove \"noise\" from GCC/clang assembly output?\n- Oh, I assumed standard optimizations would be on by default. Why aren't they?\n- @Matt: To quote the manual, \"Without any optimization option, the compiler's goal is to reduce the cost of compilation and to make debugging produce the expected results.\"\n- @Matt And because the implementors so chose. Unless you score an answer from one of them here it's a futile question.","metadata":{"transformedAt":"2026-08-18T18:32:17.828Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":143,"estimatedTokens":938}}801{"id":"stack-288061","source":"stackoverflow","questionId":288061,"title":"Do there exist any compilers with localized versions of programming languages?","tags":["compiler-construction","programming-languages"],"text":"Title: Do there exist any compilers with localized versions of programming languages?\nTags: compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nI have often wondered why it is that non-English speaking programmers are forced to use a different language when programming when it would seem to be so easy to offer an IDE that could replace keywords with localized versions. Why can't the Germans use a \"während..macht\" loop?\n\nDo programmers in Japan, Germany, France, Spain, Botswana just make extensive use of macros/define statements to make life more tolerable or do they just get used to the functional significance of print, if, then, while, do, begin, end, var, double, function, etc?\n\nPerhaps the increased use of frameworks (J2EE/.NET) makes this more complicated, but it still seems worthwhile. On the other hand, if Klingon became the de-facto programming language in the US I'd probably regret suggesting this.\n\n========================================\n\nTop Answer:\nThis sort of thing isn't unique to computer languages. All the control words for music are in Italian (andante, allegro, crescendo, etc.). This has worked well for a long time.\n\nBesides, it allows programs to be shared more easily. Suppose you were trying to integrate a function from a Finnish programmer into a module in French, and wanted to send it for further integration to your Australian colleague.\n\n========================================\n\nCode:\n```text\nif\n```\n\n```text\nfor\n```\n\n========================================\n\nComments:\n- Funny you should mention Klingon. There's actually a language called \"var'aq\" (geocities.com/connorbd/varaq) that is normally written in Klingon, but also supports English.\n- This appears to be a dupe of stackoverflow.com/questions/202723/&hellip;. I'm not closing this, though, as I'm not sure.\n- Great point. I hadn't considered the identifiers issue. I guess I should just consider myself lucky that programming was invented in the same language that I grew up with. Would be a bear to learn programming if they keywords were meaningless strings of characters!\n- \"I guess I should just consider myself lucky that programming was invented in the same language that I grew up with\". I doesn't matter that much, programming it's not English, it's computer language, it merely uses English words. Math and logic are universal... so is programming.\n- @Pop: I take your point, but it really helps to be able to communicate very clearly, and that takes a certain amount of mastery in the language. I expect some/many users of SO are non-native English speakers - and that's *bound* to make it harder for them to get their points across.\n- @Jon Skeet - I can give so many examples of non native English speakers that are great programmers, and this fact didn't seem to affect them as programmers:Bjarne Stroustrup, Edsger Dijkstra, David Heinemeier Hansson, Anders Hejlsberg, Linus Torvalds, Miguel de Icaza, Jerry Yang, Andrei Alexandrescu\n- learning English isn't hard at all compared to being a great programmer, or mathematician, or physicist ...\n- @Pop: Beethoven was deaf, but it still helps *most* musicians to be able to hear, doesn't it?\n- One of the advantages of Algol 60 was that there were no 'reserved words' - instead there was a 'reserved typeface' (typically underlined, boldface in publications, but systems with impoverished character sets had to approximate that somehow). This meant that transliterated keywords -- **si** instead of **if**, say -- would not clash with identifiers. Sad that now we have much more capable IO devices, we still have to mess with 'reserved words'.\n- I feel like there could be a future where we could code in any language. Then it will be trivial to download code and select a different language from a menu, converting all reserved words and variables as necessary. Not yet, but maybe soon.\n- I have seen this. I worked for a company which was own by a french corporation, and they developed software in WinDev. Talk about another barrier when they tried to explain how some of their system worked and gave us printouts of the sourcecode...\n- To be honest I don't like this language. My brother is currently using a lot. It's a good language to develop fast but you feel quickly blocked when you try to do custom things.\n- Actually, they didn't save bytecode, at least in some versions. I once spent days porting a small Access application I had written for a friend, because while he was using the same version, he had a German installation. One of the worst ideas Microsoft ever shipped, pretty close to the registry itself. (Yes, I do realize I'm answering to something three and a half years old. And? :-))\n- Yep, I cringe every time I have to do something in excel, writing things like SI() instead of IF() (I speak spanish). It's awful, awful and painful. Be careful what you wish for.","metadata":{"transformedAt":"2026-08-18T18:32:17.829Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":46,"estimatedTokens":1221}}802{"id":"stack-7775252","source":"stackoverflow","questionId":7775252,"title":"Why don't compilers generate microinstructions rather than assembly code?","tags":["optimization","architecture","compiler-construction","assembly","low-level"],"text":"Title: Why don't compilers generate microinstructions rather than assembly code?\nTags: optimization, architecture, compiler-construction, assembly, low-level\nSource: Stack Overflow\n\nQuestion:\nI would like to know why, in the real world, compilers produce **Assembly code**, rather than **microinstructions**.\n\nIf **you're already bound to one architecture**, why not go one step further and free the processor from having to turn **assembly-code into microinstructions** at Runtime?\n\nI think perhaps there's a **implementation bottleneck** somewhere but I haven't found anything on Google.\n\n**EDIT** by microinstructions I mean: if you assembly instruction is ADD(R1,R2), the microinstructions would be. Load R1 to the ALU, load R2 to the ALU, execute the operation, load the results back onto R1. Another way to see this is to equate one microinstruction to one clock-cycle.\n\nI was under the impression that microinstruction was the **'official'** name. Apparently there's some mileage variation here.\n\nFA\n\n========================================\n\nTop Answer:\nBecause an x86 CPU doesn't execute micro operations, it executes opcodes. You can not create a binary image that contains micro operations since there is no way to encode them in a way that the CPU understands.\n\nWhat you are suggesting is basically a new RISC-style instruction set for x86 CPUs. The reason that isn't happening is because it would break compatibility with the vast amount of applications and operating systems written for the x86 instruction set.\n\n========================================\n\nComments:\n- Define what you mean by micro-instruction. The binary? Or the decoded micro-ops within the processor?\n- the micro operations.. I call them microinstructions.\n- Whats with the down-vote and close vote? This is a perfectly legitimate question.\n- You mean the decoded micro-ops in the processor? (Just double-checking.) If so, the answer is it's not possible because the decoded micro-ops will vary even within the same ISA and processor line.\n- Every processor has different op-codes or instruction set that they understand. Assembly is needed to bridge the gap, its like having the JVM framework to run your java app on different OS. I cant explain this well enough to give an answer, so a comment will have to do.\n- I think the question is RISC vs. CISC again.\n- the compilers produce the machine code that the processor is able to execute. If there were a processor that could execute microcode directly AND users had access to it then you might see something like that. Not all processors are microcoded so you are already generating and executing the lowest level machine code. Except perhaps for the transmeta experiment the low level code is likely not what you are expecting and not something can write apps in.\n- microcoded processors are designed specifically to convert one instruction set into state machine controls, etc for manipulating the rest of the processor. Again except perhaps for transmeta and perhaps some others they are specifically not designed to allow microcode to be run directly.\n- I dont think this is a RISC vs CISC thing again I think this is a \"why cant I see or get at the microcode\" thing again.","metadata":{"transformedAt":"2026-08-18T18:32:17.829Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":36,"estimatedTokens":803}}803{"id":"stack-260472","source":"stackoverflow","questionId":260472,"title":"Compiler optimizations: Where/how can I get a feel for what the payoff is for different optimizations?","tags":["optimization","compiler-construction"],"text":"Title: Compiler optimizations: Where/how can I get a feel for what the payoff is for different optimizations?\nTags: optimization, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn my independent study of various compiler books and web sites, I am learning about many different ways that a compiler can optimize the code that is being compiled, but I am having trouble figuring out how much of a benefit each optimization will tend to give.\n\nHow do most compiler writers go about deciding which optimizations to implement first? Or which optimizations are worth the effort or not worth the effort? I realize that this will vary between types of code and even individual programs, but I'm hoping that there is enough similarity between most programs to say, for instance, that one given technique will usually give you a better performance gain than another technique.\n\n========================================\n\nTop Answer:\nTongue in cheek:\n\n- Hubris\n\n- Benchmarks\n\n- Embarrassment\n\nMore seriously, it depends on your compiler's architecture and goals. Here's one person's experience...\n\nGo for the \"big payoffs\":\n\n- native code generation\n\n- register allocation\n\n- instruction scheduling\n\nGo for the remaining \"low hanging fruit\":\n\n- strength reduction\n\n- constant propagation\n\n- copy propagation\n\nKeep bennchmarking.\n\nLook at the output; fix anything that looks stupid.\n\nIt is usually the case that combining optimizations, or even repeating optimization passes, is more effective than you might expect. The benefit is more than the sum of the parts.\n\nYou may find that introduction of one optimization may necessitate another. For example, SSA with Briggs-Chaitin register allocation really benefits from copy propagation.\n\n========================================\n\nComments:\n- I hadn't looked at the Wikipedia article, but it seems to have the same problem as most other optimization discussions. It tells me what an optimization is, and how it's done, but not how much good it would do me.\n- Yes. Profiling tells you how much good it will do you. And you won't know until you try.\n- \"Everything is a tradeoff\" - we certainly don't see that statement enough on stackoverflow. +1","metadata":{"transformedAt":"2026-08-18T18:32:17.829Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":52,"estimatedTokens":548}}804{"id":"stack-1622077","source":"stackoverflow","questionId":1622077,"title":"Why use integers smaller than 32bit?","tags":["c#","memory","compiler-construction","memory-management"],"text":"Title: Why use integers smaller than 32bit?\nTags: c#, memory, compiler-construction, memory-management\nSource: Stack Overflow\n\nQuestion:\nI always like to use the variable with the smallest size that will work just fine, But would this really gain me if I used short byte integers instead of integer, and the memory is 32bit word addressable, Does the compiler do something to enhance memory usage ?\n\n========================================\n\nTop Answer:\nNo, `int` was chosen to be the fastest int type for mopdern 32/64 bit architectures, using shorter (`short`, `sbyte`) types will only cost you performance. \n\nYou can sometimes save on memory, but only when using large arrays or lists. And even then it usually doesn't pay. \n\nCalculation with 8 bits:\n\n```\nsbyte a, b, c;\na = (sbyte) (b + c);\n```\n\nThe typecast is required and carries a runtime cost.\n\n========================================\n\nCode:\n```text\nsbyte a, b, c;\na = (sbyte) (b + c);\n```\n\n```text\nint\n```\n\n```text\nshort\n```\n\n```text\nsbyte\n```\n\n```text\nint\n```\n\n```text\nchecked\n```\n\n========================================\n\nComments:\n- But we don't run (directly) on x86. C# widens bytes and shorts for every calculation.\n- BTW, I didn't down or up vote you.... I'm merely commenting... but my guess on the downvote is that your answer was not entirely correct on the performance assertions. Also, you can save lots of memory on structures that are not just in arrays or lists. For example, we work on games with large 3D worlds with literally tens or hundreds of thousands of objects loosely connected in a directed graph (one of our games loads about 200,000 objects). Variable sizes there really do matter for performance and memory.\n- @Henk: I stand corrected on the sbyte assertion. It's good to learn something new every day.\n- FWIW, on a PC in 32-bit mode, 16-bit ints can actually be a tiny bit slower than 32-bit ints because the machine opcodes actually require an extra byte to specify that they use 16-bit operands. So for local variables use the native int type if it works for you. Again, for structures, to save memory, use whatever the smallest size works. You may want to group member variables by like sizes though so you don't waste space with padding between different sized member variables.\n- BTW, here's an article called 'Mastering Structs in C#' describing in depth *EXACTLY* what I was saying about grouping like-sized variables: vsj.co.uk/articles/display.asp?id=501\n- I liked the link, very informative and I would like to know, How will I have a similar gain with arrays, will it be something like Structures ?\n- @Mohamed: Yes, if you have large arrays, then using smaller ints will save memory.","metadata":{"transformedAt":"2026-08-18T18:32:17.829Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":61,"estimatedTokens":672}}805{"id":"stack-839407","source":"stackoverflow","questionId":839407,"title":"Replicating C struct padding in Java","tags":["java","c","compiler-construction","padding"],"text":"Title: Replicating C struct padding in Java\nTags: java, c, compiler-construction, padding\nSource: Stack Overflow\n\nQuestion:\nAccording to here, the C compiler will pad out values when writing a structure to a binary file. As the example in the link says, when writing a struct like this:\n\n```\nstruct {\n char c;\n int i;\n} a;\n```\n\nto a binary file, the compiler will usually leave an unnamed, unused hole between the char and int fields, to ensure that the int field is properly aligned. \n\nHow could I to create an exact replica of the binary output file (generated in C), using a different language (in my case, Java)?\n\nIs there an automatic way to apply C padding in Java output? Or do I have to go through compiler documentation to see how it works (the compiler is g++ by the way).\n\n========================================\n\nTop Answer:\nDon't do this, it is brittle and will lead to alignment and endianness bugs.\n\nFor external data it is much better to explicitly define the format in terms of bytes and write explicit functions to convert between internal and external format, using shift and masks (not union!).\n\n========================================\n\nCode:\n```text\nstruct {\n char c;\n int i;\n} a;\n```\n\n```text\nput_char(out, a.c);\nput_int(out, a.i);\n```\n\n```text\nshort\n```\n\n```text\nsizeof (short) == 2\n```\n\n```text\ndouble\n```\n\n```text\nput\n```\n\n```text\nbyte\n```\n\n```text\nshort\n```\n\n```text\nByteBuffer bb = ByteBuffer.allocate(8);\nbb.order(ByteOrder.LITTLE_ENDIAN);\nbb.put(0, someChar);\nbb.put(4, someInteger);\nbyte[] rawBytes = bb.array();\n```\n\n```text\nint readInt(InputStream is,int len)\n  throws PrematureEndOfDataException\n{\n  int n=0;\n  while (len-->0)\n  {\n    int i=is.read();\n    if (i==-1)\n      throw new PrematureEndOfDataException();\n    byte b=(byte) i;\n    n=(n<<8)+b;\n  }\n  return n;\n}\n```\n\n========================================\n\nComments:\n- Its worth noting that char in java is a 16-bit value rather than an 8-bit value as it is in C. I suggest you use something like ByteBuffer.putInt(0, c & 0xFF); BTW do you know if the data is Big endian or little endian. In C it depends on the architecture of the machine you are running.\n- Thanks, unfortunately I'm not in a position to change how the C structs are outputted to files. I was able to get the same output using java by applying simple padding in the form you mentioned. Do you know if there tends to be much variation between how C compilers implement padding??\n- It depends mostly on the processor architecture, so the \"fun\" really starts once you port it to other architectures (we had that recently). Which doesn't mean it's guaranteed to be the same between compilers on the same architecture.\n- This talks a bit about struct packing and how and why there is padding applied: catb.org/esr/structure-packing\n- XML has a place in this somewhere perhaps?\n- @starblue XML has to be a solution to every problem!!1\n- @starblue Thanks for the answer. Writing explicit file conversions is not really possible, since there are many C structs that need to be outputted using Java (and the format of these structs may change from time to time). I feel I may have to implement the same form of padding as g++ uses when outputting in Java (I agree with you that this is brittle). The only saving grace is that the platform and compiler will not change, so the endianness bytesize etc. shouldn't change either\n- You could also try to make the structure packed by adding **attribute__((__packed**)) . Another solution would be to write a code generator for proper I/O functions.\n- @Bombe Did you ever consider a 10 megapixel HDR image in xml LOL. A good file format should consider the same endiannes as the majority of the target processors. This is also true for the padding rules. A scanline in a windows bitmap always starts on a 4 byte boundary. Being binary compatible the entire way will speed things up.\n- Yes, I have been using ByteBuffer. The issue I am really having is in finding out how much to pad the bytes when writing/reading..","metadata":{"transformedAt":"2026-08-18T18:32:17.829Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":104,"estimatedTokens":1003}}806{"id":"stack-8633769","source":"stackoverflow","questionId":8633769,"title":"How are backslash escape sequences implemented in compilers?","tags":["c","compiler-construction","operating-system","embedded-linux"],"text":"Title: How are backslash escape sequences implemented in compilers?\nTags: c, compiler-construction, operating-system, embedded-linux\nSource: Stack Overflow\n\nQuestion:\nI just wanted to know how backslash escape sequences are implemented in compilers?\nIf we write \"\\n\" in a string, how does a compiler come to replace it with a new line character? How does a compiler come to replace \"\\b\" with a backspace character?\n\nI ask because I wrote the code:\n\n```\n#include\nmain()\n{\n printf(\"Hello \\c\");\n}\n```\n\nThe output was:\n\nHello \nExited: ExitFailure 7 \n\nI ran it in codepad, I was going through KnR book question number 1.2.\n\nThanks in Advance\n\n========================================\n\nTop Answer:\nThe classic explanation is given in the famous article by Ken Thompson called 'Reflections on Trusting Trust' (also available from many other sources, including the book ACM Turing Award Lectures: The First Twenty Years 1966-1985) which was his acceptance speech when he received the ACM Turing Award along with Dennis Ritchie.\n\nAmongst other things, it describes how to add `\\v` to a compiler that does not recognize it:\n\n C allows a string construct to specify an initialized character array.\n The individual characters in the string can be escaped to represent\n unprintable characters. For example,\n\n```\n\"Hello world\\n\"\n```\n\n \n represents a string with the character \"`\\n`\", representing the new line\n character.\n\n \n Figure 2.1 is an idealization of the code in the C compiler that\n interprets the character escape sequence. This is an amazing piece of\n code. It \"knows\" in a completely portable way what character code is\n compiled for a new line in any character set. The act of knowing then\n allows it to recompile itself, thus perpetuating the knowledge.\n\n \n Suppose we wish to alter the C compiler to include the sequence \"`\\v`\" to\n represent the vertical tab character. The extension to Figure 2.1 is\n obvious and is presented in Figure 2.2. We then recompile the C\n compiler, but we get a diagnostic. Obviously, since the binary version\n of the compiler does not know about \"`\\v`\", the source is not legal C. We\n must \"train\" the compiler. After it \"knows\" what \"`\\v`\" means, then our\n new change will become legal C. We look up on an ASCII chart that a\n vertical tab is decimal 11. We alter our source to look like Figure\n 2.3. Now the old compiler accepts the new source. We install the\n resulting binary as the new official C compiler and now we can write the\n portable version the way we had it in Figure 2.2.\n\n \n This is a deep concept. It is as close to a \"learning\" program as I\n have seen. You simply tell it once, then you can use this\n self-referencing definition.\n\n \n \n\n### Figure 2.1\n\n```\nc = next();\nif (c != '\\\\')\n return(c);\nc = next();\nif (c == '\\\\')\n return('\\\\');\nif (c == 'n')\n return('\\n');\n```\n\n \n \n\n### Figure 2.2\n\n```\nc = next();\nif (c != '\\\\')\n return(c);\nc = next();\nif (c == '\\\\')\n return('\\\\');\nif (c == 'n')\n return('\\n');\nif (c == 'v')\n return('\\v');\n```\n\n \n \n\n### Figure 2.3\n\n```\nc = next();\nif (c != '\\\\')\n return(c);\nc = next();\nif (c == '\\\\')\n return('\\\\');\nif (c == 'n')\n return('\\n');\nif (c == 'v')\n return(11);\n```\n\n========================================\n\nCode:\n```text\n#include<stdio.h>\nmain()\n{\n    printf(\"Hello \\c\");\n}\n```\n\n```text\nHello \nExited: ExitFailure 7\n```\n\n```text\n\"Hello\"\n```\n\n```text\n\"([^\\\"]|\\\"|\\\\|\\n|\\b)*\"\n```\n\n```text\n\\\"\n```\n\n```text\n\"\n```\n\n```text\n\\\\\n```\n\n```text\n\\\n```\n\n```text\n\\n\n```\n\n```text\n\\b\n```\n\n```text\n\"Hello world\\n\"\n```\n\n```text\nc = next();\nif (c != '\\\\')\n    return(c);\nc = next();\nif (c == '\\\\')\n    return('\\\\');\nif (c == 'n')\n    return('\\n');\n```\n\n```text\nc = next();\nif (c != '\\\\')\n    return(c);\nc = next();\nif (c == '\\\\')\n    return('\\\\');\nif (c == 'n')\n    return('\\n');\nif (c == 'v')\n    return('\\v');\n```\n\n```text\nc = next();\nif (c != '\\\\')\n    return(c);\nc = next();\nif (c == '\\\\')\n    return('\\\\');\nif (c == 'n')\n    return('\\n');\nif (c == 'v')\n    return(11);\n```\n\n```text\n\\v\n```\n\n```text\n\\n\n```\n\n```text\n\\v\n```\n\n```text\n\\v\n```\n\n```text\n\\v\n```\n\n========================================\n\nComments:\n- Hi, Welcome to StackOverflow. if you have some code in your post, you can format it, by marking the code and pressing **ctrl+k**\n- \"Object not found The object /who/ken/trust.html does not exist on this server.\" I usually tend to -1 link only answers for this reason.\n- @Petr: I'm sorry Google doesn't work for you. Thanks for letting me know that the original link was broken. I've provided a number of alternative sources, including a link to the ACM web site as the primary source. Hope that helps!\n- Stack Overflow isn't a collection of keywords you are supposed to feed to google. It's one of basics that link only answers are not very welcome.","metadata":{"transformedAt":"2026-08-18T18:32:17.829Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":19,"totalLines":239,"estimatedTokens":1183}}807{"id":"stack-2027729","source":"stackoverflow","questionId":2027729,"title":"Why C is the language of compilers- when a Scheme subset would seem to be a better fit?","tags":["c","gcc","scheme","compiler-construction"],"text":"Title: Why C is the language of compilers- when a Scheme subset would seem to be a better fit?\nTags: c, gcc, scheme, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI was just listening to episode 57 of Software Engineering Radio \n(TRANSCRIPT: http://www.se-radio.net/transcript-57-compiletime-metaprogramming )\nI'm only 40 minutes in, but I'm wondering why C is the language of compilers- when a Scheme subset would seem to be a better fit? (or some other HLL)\n(excluding the obvious reason of not wanting to rewrite gcc)\n\nPS originally posted this at LtU http://lambda-the-ultimate.org/node/3754\n\n========================================\n\nTop Answer:\nC need not be the language for compilers, but it does have some advantages. C is available on almost all platforms and that makes it easy to port and bootstrap the compiler. C is closer to the hardware and makes possible many optimizations that will be difficult to achieve in other languages. It is easy for a compiler written in C to co-exist with other languages, libraries and systems as most of them provide a C interface. It is also easy for others to extend the compiler as C is the Esperanto of system programmers.\n\n========================================\n\nComments:\n- Ah well, the folks at LtU are pretty hardcore: \"If you can't program it in Scheme, it's not worth programming\" ;) Lisps are great languages that bring a lot of great features to the table with tiny amounts of syntax and keywords, but because they don't offer transparent support for underlying datatypes (byte, char, short, int...) most people don't trust them to be tops in performance.\n- The concept of a Scheme subset intrigues me. I thought it was already a pretty minimal language.\n- @David: R5RS and its descendants (I'm thinking ERR5RS) are pretty minimal, yes. The others, well, depending on whom you talk to, are either non-minimal or non-existent. :-P xkcd.com/566\n- I don't have stats to refute you, but it seems unlikely because of the chicken-and-egg problem: With no existing compiler in that language available, how do those compilers ever get built on new architecture?\n- @Carl Smotricz: Well, the ubiquitous C compiler gcc is written in C. Similarly the Erlang compiler is written in Erlang. And the Lisp eval function, which IS the Lisp compiler was written in Lisp when Lisp was merely a theory until a grad student looked at the code and realised he could transliterate most of it directly in assembly. So I guess the first practical Lisp compiler was compiled by a human.\n- It's called \"bootstrapping\" and it's mentioned in the most fundamental of compiler textbooks, the dragon book. Basically you implement a small part of a language compiler in another language first, then use the new language to build the next compiler for that langauge (with more language features) and iterate. How do you think the first compilers (or even assemblers) were written? In machine code of course. Same goes for the first editors and other utilities.\n- As far as I know, first versions are normally written in other languages (such as plain assembler)\n- \"a minority of compilers are written in C\". Counting compilers might be misleading anyway. The vast majority of compilers are probably Scheme or ML compilers, written by CS undergraduates learning Scheme or ML ;-)\n- Any language who's origins I've ever heard of was first implemented as a -to-c translator. The real compiler for -to-machine-code came as a second step using the c-translator.\n- @Alan: Lisp, Pascal, FORTRAN and COBOL were all compiled languages whose original compiled implementations did not use C. Of course, in 3 of 4 cases C didn't exist at the time. For example the first Lisp compiler was written in Lisp, and bootstrapped using a Lisp interpreter written in machine code. I remember when all this was fields, you know (well, I don't, but I've read about what it was like when all this was fields...)\n- More recently, I don't believe that the first Java JIT was initially written as a Java-to-C translator. I could be wrong, though, and I guess you could argue that a JIT isn't a \"proper\" compiler.\n- @Steve: A JIT is a dynamic compiler, as opposed to traditional static compilers. For dynamic fans, dynamic compilers are even more of a \"real compiler\" than static compilers, because of the greater opportunities for optimisation.\n- Sure, I'm just trying to speculate what Alan actually means by \"any language whose origins I've ever heard of\". Obviously he's deliberately excluding languages all of whose implementations are interpreted, since of course they don't translate to C. So I don't know whether it's reasonable or not to raise Java as a counter-example alongside Lisp etc. He may be aware of its origins, but discounted it deliberately, rather than just having forgotten it or been unaware of its origins (like Lisp etc). In other contexts I'm entirely with you, a JIT is a proper compiler.\n- If we accept (2), why would anyone write a compiler for a different language in the first place? Also if they write a compiler, we can assume they know more than just one language, so (3) doesn't sound like a convincing reason either.\n- we can accept 2, and not remove all other compilers. I don't undersand your logic. The Beatles are popular, that doesn't negate the need and desire for other musicians.\n- @nikie: Just because a language is popular doesn't mean others aren't used at all. If 1 million programmers use C, and 1 thousand like Scheme, and some random programmer decided to implement a new compiler for Scheme, it's more likely to be a C programmer by shear numbers. Besides which, a lot of compilers aren't written for use by the people themselves, but for other reasons (commercial use, learning a new language, fun, etc).\n- There are many variants and successors of YACC that can output to a wide variety of languages.\n- I've seen similar functionality in a lot of different languages, but not Scheme. I'm not saying there isn't a Scheme parser generator out there, but the only Lispy one I've seen was for Common Lisp.","metadata":{"transformedAt":"2026-08-18T18:32:17.829Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":38,"estimatedTokens":1516}}808{"id":"stack-1615800","source":"stackoverflow","questionId":1615800,"title":"C# Operators and readability","tags":["c#","compiler-construction","operators"],"text":"Title: C# Operators and readability\nTags: c#, compiler-construction, operators\nSource: Stack Overflow\n\nQuestion:\nI was just working on some code and caught myself making this error\n\n```\nif (stringName == \"firstName\" || \"lastName\")\n // Do code\n```\n\nobviously this is wrong and should be \n\n```\nif (stringName == \"firstName\" || stringName == \"lastName\")\n // Do code\n```\n\nbut it just got me thinking in regards to readability would the first be easier? Maybe having some logic that could say unless a new stringName is specified, use the first one?\n\nReally not a question, Im just curious if there is something I dont fully comprehend on the logic behind compiling a statement like this.\n\n========================================\n\nTop Answer:\nI think your proposal would muddy the rules of expression parsing - now, the '==' becomes a quadreny (?) operator, rather than a binary one. I've found myself missing SQL's 'IN' operator, though, and've used something like this:\n\n```\nif (stringName.In(\"foo\", \"bar\", \"baz\"))\n{\n\n}\n\n// in an extension method class\npublic static bool In(this T value, params T[] values)\n{\n return values.Contains(value);\n}\n```\n\n========================================\n\nCode:\n```text\nif (stringName == \"firstName\" || \"lastName\")\n   // Do code\n```\n\n```text\nif (stringName == \"firstName\" || stringName == \"lastName\")\n   // Do code\n```\n\n```text\nif (stringName == firstName || lastName)\n```\n\n```text\nconst bool lastName = false;\n```\n\n```text\nstringName == \"firstName\"\n```\n\n```text\n\"lastName\"\n```\n\n```text\n||\n```\n\n```text\n||\n```\n\n```text\n&&\n```\n\n```text\n||\n```\n\n```text\nstringName == firstName && lastName\n```\n\n```text\nstringName\n```\n\n```text\n\"firstName\"\n```\n\n```text\n\"lastName\"\n```\n\n```text\nif(new string[]{\"firstName\",\"lastName\"}.Contains(stringName))\n    // code\n```\n\n```text\noperator||(string,string)\n```\n\n```text\nEquals\n```\n\n```text\nif (stringName.In(\"foo\", \"bar\", \"baz\"))\n{\n\n}\n\n// in an extension method class\npublic static bool In<T>(this T value, params T[] values)\n{\n    return values.Contains(value);\n}\n```\n\n```text\nstring[] ValidNames = new string[] { \"firstName\", \"lastName\"};\n\nif(ValidNames.Contains(stringName))\n{\n    //Do Code\n}\n```\n\n```text\nif(stringName in (\"firsName\", \"lastName\"))\n{\n}\n```\n\n```text\n[\"firstName\", \"lastName\"].include? stringName\n```\n\n```text\nstringName.EqualsOneOf(new[] {\"firstName\", \"lastName\"})\n```\n\n```text\nstringName == (\"firstName\" || \"lastName\")\n```\n\n```text\nstringName\n```\n\n```text\n(stringName == \"firstName\") || \"lastName\"\n```\n\n```text\n\"lastName\"\n```\n\n```text\nstringName\n```\n\n```text\n\"firstName\"\n```\n\n```text\nContains\n```\n\n```text\nif ((stringName == \"firstName\") || (stringName == \"lastName\"))\n   // Do code\n```\n\n```text\nbool found = Array.Exists(\n            // array of strings to search\n            new[] { \"c#\", \".net\", \"programming\", \"design patterns\", \"work\", \"play\", \"bits\", \"bytes\", \"break\" },\n            // criteria - can even satisfy multiple conditions simultaneously if desired\n            str => (str == \".NET\" || str == \"work\") //look for \".NET\" or \"work\"\n            );\n```\n\n========================================\n\nComments:\n- You can use Operator Overloading. For example, you can make a class that contains a list of strings(We will call it foo), use operator overloading : make string || string return foo(Obviously, with the string in the container) and make foo || string should add the string and return foo.(So you can use string||string||string) then you can overload string == foo so you can finally compare them. So, short answer? Operator Overloading Rocks!\n- That's a matter of taste. If you said it's wasteful, then I'd agree, it creates an array just to compare some values.\n- The same technique is definitely more readable in other languages: `if stringName in (\"firstName\", \"lastName\") ...` (Python)\n- Agreed that its wrong and having the compiler guess is wrong but how else could this be intended?\n- Perhaps you intended to use another variable `otherStringName` -- how is the compiler going to know?\n- I see what you saying, it just seems redundant. Thanks!\n- or make it generic: `In(this T value, params T[] values)`\n- Your answer addresses the intent of the question, instead of the flaw in the OP's suggested syntax, so +1.\n- see Petrotta's answer above: stackoverflow.com/questions/1615800/c-operators-and-readabil&zwnj;&#8203;ity/&hellip;\n- I can easily see Petrotta's code being what gets generated by the compiler from my syntax.\n- That last line is exactly what I was missing.","metadata":{"transformedAt":"2026-08-18T18:32:17.829Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":31,"totalLines":207,"estimatedTokens":1120}}809{"id":"stack-9529571","source":"stackoverflow","questionId":9529571,"title":"C++: Compiler and Linker functionality","tags":["c++","compiler-construction","linker"],"text":"Title: C++: Compiler and Linker functionality\nTags: c++, compiler-construction, linker\nSource: Stack Overflow\n\nQuestion:\nI want to understand exactly which part of a program compiler looks at and which the linker looks at. So I wrote the following code:\n\n```\n#include \nusing namespace std;\n#include \n\nclass Test {\nprivate:\n int i;\n\npublic:\n Test(int val) {i=val ;}\n void DefinedCorrectFunction(int val);\n void DefinedIncorrectFunction(int val);\n void NonDefinedFunction(int val);\n\n template \n void FunctionTemplate (paramType val) { i = val }\n};\n\nvoid Test::DefinedCorrectFunction(int val)\n{\n i = val;\n}\n\nvoid Test::DefinedIncorrectFunction(int val)\n{\n i = val\n}\n\nvoid main()\n{\n Test testObject(1);\n //testObject.NonDefinedFunction(2);\n //testObject.FunctionTemplate(2);\n\n}\n```\n\nI have three functions:\n\nDefinedCorrectFunction - This is a normal function declared and defined correctly.\n\nDefinedIncorrectFunction - This function is declared correctly but the implementation is wrong (missing ;)\n\nNonDefinedFunction - Only declaration. No definition.\n\nFunctionTemplate - A function template.\n\nNow if I compile this code I get a compiler error for the missing ';'in DefinedIncorrectFunction.\n\nSuppose I fix this and then comment out testObject.NonDefinedFunction(2). Now I get a linker error.\nNow comment out testObject.FunctionTemplate(2). Now I get a compiler error for the missing ';'.\n\nFor function templates I understand that they are not touched by the compiler unless they are invoked in the code. So the missing ';' is not complained by the compiler until I called testObject.FunctionTemplate(2).\n\nFor the testObject.NonDefinedFunction(2), the compiler did not complain but the linker did. For my understanding, all compiler cared was to know that is a NonDefinedFunction function declared. It didn't care for the implementation. Then linker complained because it could not find the implementation. So far so good.\n\nWhere I get confused is when compiler complained about DefinedIncorrectFunction. It didn't look for implementation of NonDefinedFunction but it went through the DefinedIncorrectFunction.\n\nSo I'm little unclear as to what the compiler does exactly and what the linker does. My understanding is linker links components with their calls. So for when NonDefinedFunction is called it looked for the compiled implementation of NonDefinedFunction and complained. But compiler didn't care about the implementation of NonDefinedFunction but it did for DefinedIncorrectFunction.\n\nI'd really appreciate if someone can explain this or provide some reference.\n\nThank you.\n\n========================================\n\nTop Answer:\nI believe this is your question:\n\n Where I get confused is when compiler complained about DefinedIncorrectFunction. It didn't look for implementation of NonDefinedFunction but it went through the DefinedIncorrectFunction.\n\nThe compiler tried to parse `DefinedIncorrectFunction` (because you provided a definition in this source file) and there was a syntax error (missing semicolon). On the other hand, the compiler never saw a definition for `NonDefinedFunction` because there simply was no code in this module. You might have provided a definition of `NonDefinedFunction` in another source file, but the compiler doesn't know that. The compiler only looks at *one* source file (and its included header files) at a time.\n\n========================================\n\nCode:\n```text\n#include <iostream>\nusing namespace std;\n#include <string>\n\nclass Test {\nprivate:\n    int i;\n\npublic:\n    Test(int val) {i=val ;}\n    void DefinedCorrectFunction(int val);\n    void DefinedIncorrectFunction(int val);\n    void NonDefinedFunction(int val);\n\n    template <class paramType>\n    void  FunctionTemplate (paramType val) { i = val }\n};\n\nvoid Test::DefinedCorrectFunction(int val)\n{\n    i = val;\n}\n\nvoid Test::DefinedIncorrectFunction(int val)\n{\n    i = val\n}\n\nvoid main()\n{\n    Test testObject(1);\n    //testObject.NonDefinedFunction(2);\n    //testObject.FunctionTemplate<int>(2);\n\n}\n```\n\n```text\n#include<string>\n#include<iostream>\n\n\nclass Test {\nprivate:\n    int i;\n\npublic:\n    Test(int val) {i=val ;}\n    void DefinedCorrectFunction(int val);\n    void DefinedIncorrectFunction(int val);\n    void NonDefinedFunction(int val);\n\n    template <class paramType>\n    void  FunctionTemplate (paramType val) { i = val; }\n};\n```\n\n```text\n#include \"try.h\"\n\n\nvoid Test::DefinedCorrectFunction(int val)\n{\n    i = val;\n}\n\nvoid Test::DefinedIncorrectFunction(int val)\n{\n    i = val;\n}\n\nint main()\n{\n\n    Test testObject(1);\n    testObject.NonDefinedFunction(2);\n    //testObject.FunctionTemplate<int>(2);\n    return 0;\n}\n```\n\n```text\n$g++ -c try.cpp -o try.o\n$\n```\n\n```text\n$g++ try.o\ntry.o: In function `main':\ntry.cpp:(.text+0x52): undefined reference to `Test::NonDefinedFunction(int)'\ncollect2: ld returned 1 exit status\n```\n\n```text\n#include \"try.h\"\n\nvoid Test::NonDefinedFunction(int val)\n{\n    i = val;\n}\n```\n\n```text\n$ g++ -c try1.cpp -o try1.o\n$\n```\n\n```text\n$ g++ try1.o\n/usr/lib/gcc/x86_64-redhat-linux/4.4.5/../../../../lib64/crt1.o: In function `_start':\n(.text+0x20): undefined reference to `main'\ncollect2: ld returned 1 exit status\n```\n\n```text\n$ g++ try.o try1.o\n$\n```\n\n```text\n;\n```\n\n```text\nundefined reference to NonDefinedFunction\n```\n\n```text\nDefinedIncorrectFunction\n```\n\n```text\nNonDefinedFunction\n```\n\n```text\nNonDefinedFunction\n```\n\n```text\n.cpp\n```\n\n```text\nDefinedIncorrectFunction()\n```\n\n```text\nNonDefinedFunction()\n```\n\n```text\nDefinedIncorrectFunction\n```\n\n```text\nNonDefinedFunction\n```\n\n```text\ntypename\n```\n\n========================================\n\nComments:\n- Thank you Als. The translation unit concept cleared it for me. If it is included in it will checked by the compiler.\n- Thats a nice way to put it Luchian. Thanks.\n- No, templates don't work that way. They're compiled in two phases. In the first phase, everything that does not depend on the template parameters is compiled, in the second phase the template parameters are filled in. Unbalanced parentheses (to name an obvious syntax error) are found in phase 1, as no substitution can fix them.\n- @MSalters I think the standard leaves a lot of leeway with regards to how a compiler processes templates. It must do enough processing up front to know where the template ends (which requires processing parentheses), and it must resolve names in the template according to very specific rules, but I don't think that the compiler is required to issue a diagnostic if it cannot find a non dependent symbol if the template is not instantiated. (In practice, g++ does, VC++ doesn't.)\n- @JamesKanze: It's fairly well-known that VC++ only has one-phase instantiation of templates in violation of the standard. In particular, it fails to look up symbols in the scope where the template is defined, even though this is required. Obviously, that means it has to parse the templates to the point where it knows that an identifier needs to be looked up in a surrounding scope.\n- @MSalters It's well known that VC++ doesn't detect a number of errors that g++ does when the template isn't instantiated. I've not tried a case where I would get incorrect name resolution in a legal program, so I don't know about that. I'm not sure that the error that g++ picks up requires a diagnostic---I know that at one point in the standardization process, the intent was that it wouldn't (provided the compiler picked up the correct function when the template was instantiated). IOW: two phased lookup is required, but I think both phases can be left until instantiation.\n- That means with a compiler that does two-stage compilation of template function should pick an error such as a missing ';' during compilation? I use VC++ so that's the reason I didn't get a compiler error for that?\n- You nailed it RedBaron! Thank you very much for this great explanation.","metadata":{"transformedAt":"2026-08-18T18:32:17.829Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":20,"totalLines":259,"estimatedTokens":1961}}810{"id":"stack-4199286","source":"stackoverflow","questionId":4199286,"title":"Compiler with built-in AV = No virus development?","tags":["compiler-construction","integration","antivirus","malware-detection"],"text":"Title: Compiler with built-in AV = No virus development?\nTags: compiler-construction, integration, antivirus, malware-detection\nSource: Stack Overflow\n\nQuestion:\nIs it possible to produce compilers that heuristically check for malware behaviour? If it is possible why has not it been implemented? Wouldn't that strongly help preventing the production of such viruses, I mean why wait to stop them once they are out there?\n\nEven if these people use a compiler that does not use the \"proposed\" built in AV, personal AV could detect that and grade the file as risky (sort of like SSL Certificates)\n\n========================================\n\nTop Answer:\nExisting AV generally works with a black-list approach. (Comparing threat signatures against files.) That would be, by definition, almost useless on an entirely new threat.\n\nEvery operation you could try to classify would end up blocking a legitimate program; if the operations didn't have a legitimate use, the OS designers would remove them for safety reasons.\n\n========================================\n\nComments:\n- Fair points buddy. With the first 4 points - thats why i said - if the final \"program\" is not scanned then they dont get some sort of encrypted \"pass certificate\". If that file is released \"unchecked\" then the certificate will be missing or inaccurate, which could then be flagged `risky` by another AV on the receiving end, resulting in thorough-er scanning.\n- Even if you solved the certificate-distribution problem and the who-to-trust problem, you'd still be left with the problem of virus writers disabling the check-for-viruses part of the compiler but not the mark-as-clean part.\n- yeah that's actually another good point, thanks for the constructive criticism though :)\n- One possible workaround for much of the preceding concerns is for the compiler to run it's analyses of the code and then embed into the executable a machine readable proof that the code fits some criteria. No certs needed, the user-AV just checks the proof. This would work for a wide variety of attributes that a program can claim to exhibit. If a standard set of attributes became prevalent, malware would generally be conspicuous by ommition.\n- How would you prevent people from writing a compiler (or altering an existing one) that embeds the required proof without running any analysis?\n- @Mark, no need. It would be virtually impossible to generate a valid proof without doing the analysis. BTW, the proof isn't that the original source has some attribute, but that the exact representation of it in the binary does. In effect, the binary it self is part of the proof.\n- Why would it be impossible to generate a valid proof without doing an analysis? Why not analyze notepad.exe and embed that proof into your virus?\n- If you included a proof of correctness for notepad, the AV's proof checker would very quickly detect that something is wrong when the proof starts referring to parts of the binary that don't do what it says they do. For example, the proof would make assertions like: \"if condition X is true when code starts executing the basic block at location 0x95872398, then the branch at the end at 0x95872583 will always be followed\". The proof checker would start by examining the binary and verifying that the ASM starting at location 0x95872398 and running to 0x95872583 is in fact a basic block.\n- The premise of the system is that there is a wide variety of trivial properties that can be easily verified about binaries and a similar set of identities that can be used to construct complex properties from those trivial bits. The proof ends up being a list the trivial properties that the proof asserts and the proof checker checks and a sequence of steps that the checker applies to the assertions. When all is said an done, if some specific pattern is constructed, then you know that the code you are about to run exhibits some specific property.\n- So why hasn't this PCC principal been put into practice? Everything I can find on it was written by the originators of the idea.\n- Generating proofs of correctness are HARD. As a single data point: I ran across one project that did a proof of correctness of a single 10 KLOC program and it took something like 10 man years. In theory it works and is even secure. In practice, it's actually cheaper (way, way cheaper) to rebuild your system every 6-12 months.\n- I am aware on your first point, as am developing a genetic algorithm AV at the moment for my undergrad project and that is one of the reasons why i decided to do it (static signature comparison being the main method used today for detection which IMO is rather poor). Yet there is heuristics.\n- Thanks for the resource buddy, although Mark also posted it. Ill give it a read","metadata":{"transformedAt":"2026-08-18T18:32:17.829Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":32,"estimatedTokens":1189}}811{"id":"stack-4989363","source":"stackoverflow","questionId":4989363,"title":"What happens if function inlining is too aggressive?","tags":["c++","performance","visual-c++","compiler-construction","inline"],"text":"Title: What happens if function inlining is too aggressive?\nTags: c++, performance, visual-c++, compiler-construction, inline\nSource: Stack Overflow\n\nQuestion:\nEach time I read about `inline` keyword in C++ there's a long explanation that the compiler makes a \"speed versus code volume\" analysis and then decided whether to inline a function call in each specific case.\n\nNow Visual C++ 9 has a `__forceinline` keyword that seems to make the compiler inline the call to the function unless such inlining is absolutely impossible (like a call is virtual).\n\nSuppose I look through some project without understanding what goes inside it and decide myself that one third of functions are small enough and good for inlining and mark them with `__forceinline` and the compiler does inline them and now the executable has become say one hundred times bigger.\n\nWill it really matter? What effect should I expect from having functions inlined overly aggressively and having one hundred times bigger executable?\n\n========================================\n\nTop Answer:\nThe main impact will be to the cache. Inlining goes against the principal of locality; the CPU will have to fetch the instructions from the main memory far more often. So what was intended to make the code faster may actually make it slower.\n\n========================================\n\nCode:\n```text\ninline\n```\n\n```text\n__forceinline\n```\n\n```text\n__forceinline\n```\n\n========================================\n\nComments:\n- i hear you might stress instruction cache too much\n- You might want to look into this question: stackoverflow.com/questions/2130248/&hellip;\n- `__forceinline` may not always inline something, even where it is possible, this can be due to stack alignment requires, or the inline nesting depth limit: msdn.microsoft.com/en-us/library/cx053bca(v=vs.80).aspx\n- If you do this, you have violated two tenants of wise optimization. Don't optimize until you understand the code, and don't optimize until you profile against a repeatable benchmark of the code. Once you have tools to tell you how much faster it is/isn't, then you can empirically answer a question like this yourself.\n- Satan himself will reach up out of the pit to drag you down to hell.\n- Why will it have to fetch instructions far more often?\n- @zneak: Because the program is bigger, less of it fits in cache at any one moment.\n- @zneak I said, \"the CPU will have to fetch the instructions **from the main memory** far more often\". That's because the instructions won't be in the cache.\n- @Marcelo Cantos Does it really have that much of an impact? I mean, caches are usually pretty small, and your CPU being multitasking, aren't there pretty good chances that anyways you'll have to fetch often?\n- Would be worth listing the principal and how inlining relates to it...? i.e. \"Temporal Locality (locality in time): If an item is referenced, it will tend to be referenced again soon.\" - but with inlining we're diluting the benefit.\n- @zneak: Chip designers don't build caches with the expectation that they won't provide any benefit. They do it fully hoping and expecting to achieve large hit rates on common real-world workloads. Otherwise, why would they bother? So if you take a normal everyday program and balloon it out 100-fold, there's a good chance that your program will run about 100-times more slowly.\n- Also note that most systems have a multi-level caching architecture, with L1, L2 and often L3 caches, with each level being slower, but larger, than the previous. Making your program larger across the board compromises every level. Inner loops become bigger, meaning they are less likely to fit in L1, groups of functions that call each other frequently won't fit together in L2, and so on.\n- @Marcelo Cantos There's no doubt that cache works. In fact, what I doubt is that code size is a serious issue for it. Saying that making code 100 times its original time will make it a hundred times slower is quickly dismissing a lot of facts, and quickly dismissing that C++ tends to be a very real-world case of code that gets huge and yet runs fast.\n- Also, any library call is obviously out of cache bounds, and yet programs still heavily rely on them. I know there's a tradeoff between performance and convenience, but to me it looks like it's much less worse than what you're describing. Scattering code across a larger area doesn't invalidate all caches at once, and it's not hard to imagine instruction-fetching algorithms that would work so well for extremely sequential code that I'd be surprised they aren't already implemented in most chips. Do you have any empirical example of a program that's impaired by its size?\n- @zneak: I am not dismissing anything; my comments are prefaced with qualifiers like, \"there's a good chance...\", because I know full well that many, many factors contribute to performance. Also, C++ doesn't \"get huge\" compared to other languages. There may be some overheads, but they are generally quite modest (usually negligible, in fact) in terms of overall impact to code size.\n- @zneak: How did you arrive at the notion that library calls are out of cache bounds? A cache can hold many distinct chunks of memory. If two small pieces of code call each other frequently, they will both be in cache, no matter that one is in the main program and the other in a library \"miles\" away. If this is your understanding, then I'm not at all surprised that you are struggling to grasp (and demanding empirical evidence) that a 100-fold increase in program size will destroy performance.","metadata":{"transformedAt":"2026-08-18T18:32:17.830Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":52,"estimatedTokens":1390}}812{"id":"stack-4254887","source":"stackoverflow","questionId":4254887,"title":"What does back slash \"\\\" really mean?","tags":["compiler-construction"],"text":"Title: What does back slash \"\\\" really mean?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm wondering about Java's backslash. How does the computer or the compiler see this backslash and how is it stored in computer?\n\nI read that backslash removes the special meaning of the following character. But how does a computer treat this one and in what conditions treat it in some other ways?\n\nFor example the null character `\\0` in C programming, is the end of the string, but is it a single character or two characters, i.e., backslash + zero? \n\nThe objective of back slash is to indicate for humans or to indicate for 0-1 computer?\n\n========================================\n\nTop Answer:\nIt really depends *entirely* on the context. Backslashes can mean many different things, depending on where you see them used. For example, in Windows, backslashes are commonly found as path separators.\n\nIn C-based programming languages (as well as other scripting languages), they are *escape characters*. They indicate that the character to their right should be interpreted differently from the way it normally would. In your example, `\\0` is a null-terminator, and the backslash indicates that the `0` should be interpreted by the compiler (and the human!) as the null character instead of as the number zero. It would be seen as just *one* character because the backslash is dropped off once its function is served—it has no meaning in that sequence beyond its use as an escape character.\n\n========================================\n\nCode:\n```text\n\\0\n```\n\n```text\n\\\n```\n\n```text\nA\n```\n\n```text\n,\n```\n\n```text\n4\n```\n\n```text\n'\\a'\n```\n\n```text\nprintf(\"%c\", '\\a')\n```\n\n```text\n\\\n```\n\n```text\na\n```\n\n```text\ngrep '\\<foo\\>' file.txt\n```\n\n```text\n<\n```\n\n```text\n>\n```\n\n```text\n\\<\n```\n\n```text\n\\>\n```\n\n```text\n\\0\n```\n\n```text\n0\n```\n\n========================================\n\nComments:\n- Another source for escapes: msdn.microsoft.com/en-us/library/h21280bw(VS.80).aspx It is interesting to note that it doesn't explicitly include \\0\n- @BCS: I'm sure that's just another not-so-interesting anomaly in the MSDN documentation. It is explicitly mentioned here.\n- It's interesting because it's *impliedly* mentioned as part of octal literals.\n- hi, Marcelo, thank you for your detailed answer. Just one thing, when we make a c program like string processing, we, as human beings, use \"\\0\" in the program to express our operation with the end of the string. then, what the compiler does? Is \"\\\" stored or the following character \"0\" (or \"a\" in your example) stored? then, when the program executes, how does the code responds or proceeds with this stored value?\n- @pepero: For the case of ending a string with \\0, the value that is stored is a byte with the value of zero (no bits set). This happens to be yet another C conversion used to allow finding the end of strings.\n- @pepero: I'm guessing that you made your comment before I saved my edited answer, which covers this point explicitly in the second paragraph. I've just tweaked it slightly to eliminate any possible ambiguity.","metadata":{"transformedAt":"2026-08-18T18:32:17.830Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":96,"estimatedTokens":769}}813{"id":"stack-3617312","source":"stackoverflow","questionId":3617312,"title":"how a source code is converted into 1's and 0's?","tags":["compiler-construction","compilation","binary"],"text":"Title: how a source code is converted into 1's and 0's?\nTags: compiler-construction, compilation, binary\nSource: Stack Overflow\n\nQuestion:\ncomputers do not understand anything except one's and zero's.But I want to know the details how a source code or an instruction set is converted into 1's and 0's.Is the exe file only contains 1's and 0's?\n\n========================================\n\nTop Answer:\nYou need to research Compilers\n\n========================================\n\nCode:\n```text\nx = 50;\n```\n\n```text\nmov ax, 0x32\n```\n\n```text\n10100000 00001011 00110010\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\nMOV\n```\n\n```text\n2 + 3\n```\n\n```text\n2 + 3\n```\n\n========================================\n\nComments:\n- In fact the source code file is represented using ones and zeros too.\n- Proof that there are 10 types of people in the world. Those that understand binary and those that don't\n- related: Where and how does software meet hardware?\n- I think the term you are look for instead of \"1's and 0's\" is machine code: en.wikipedia.org/wiki/Machine_code\n- @BeRecursive you forgot those who read Gray Code\n- @danio: machine code is still 1 step higher level than \"ones and zeroes\", but yea it's worth mentioning that it's part of the chain.\n- @tenfour: Machine code instructions are patterns of bits\n- so please give some sources from where I can research?\n- Click on the link....the word \"compilers\" links to the wikipedia article about them and is an excellent place to start. A simple google search for \"compilers\" will also yield a wealth of information.\n- The processor doesn't actually convert assembly to binary, that's the job of the assembler which functions in a similar manner to a compiler. Actually if you want to get technical, because these are both programs, the processor does all of the work from writing the code to storing it to converting it to...\n- Yes. You are corrected. I edited. The assembler converts to binary not the processor.\n- so is it true that compiler may be different for different languages but the assembler is processor dependent?\n- compiler is different for different languages, for same languages different platform (OS, ISA). Assembly is different for different ISA.\n- The assembly code must be generated for a specific architecture to run in the processor. In GCC you have flags to create binaries for PPC, IA32 and other architectures.\n- so you mean to say assembler are processor dependent while compilers are language dependent?\n- you forgot to mention that assembly language maps to binary operation codes.\n- @Radheshyam yes every CPU has an instruction set such as i386 or x64. These define what and how you use instructions (called assembly language, but really there is no such thing). Instructions map directly to operation codes which are really just 0s and 1s sent to a correct place (instruction decoder in CPU). Compilers compile high level language (such as c++) into operation codes directly. Some other languages use intermediate codes (such as Java's bytecode).\n- An instruction is not implemented as an opcode. An instruction is made of an opcode and some operand identifiers. I think you're confusing the term *instruction* with opcode, or vice versa.\n- I reworded it but originally by \"instruction\" I just meant the operation denoted by the opcode, as opposed to a specific instance of the operation.","metadata":{"transformedAt":"2026-08-18T18:32:17.830Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":69,"estimatedTokens":839}}814{"id":"stack-65348623","source":"stackoverflow","questionId":65348623,"title":"In C++, what is the difference between new and new[] for array allocations","tags":["c++","arrays","memory-management","compiler-construction","dynamic-memory-allocation"],"text":"Title: In C++, what is the difference between new and new[] for array allocations\nTags: c++, arrays, memory-management, compiler-construction, dynamic-memory-allocation\nSource: Stack Overflow\n\nQuestion:\nI am aware of the differences between free and delete in C++. But one thing I never understood is why in C malloc/free can allocate de-allocate both single 'objects' and arrays but in C++ we need to use the correct new/delete vs new[]/delete[] pair.\n\nSearching on Stackoverflow, it seems that in C++, new[] allocates extra memory to hold the size of the allocated array and new only allocates the memory to the object itself. And because of that, you should be aware of this extra overhead.\n\nIf the previous paragraph is indeed the case, then how malloc/free handles this overhead? Or they just accept this overhead? And if it is tolerable in C, why not in C++?\n\nOn the other hand, in case it's not because of memory overhead, but because of calling constructors and destructors, couldn't the compiler be smart enough to generate the appropriate code under the hood and let the programmer just write new/delete for both single objects and arrays of objects?\n\nI am writing a compiler for a toy language whose semantics is similar to C++ and it seems that it is possible to let the compiler decides how to allocate and de-allocate only using new/delete, but as C++ uses new/delete and new[]/delete[], maybe there's a catch that I am not seeing right now. Maybe something related to polymorphism and virtual tables?\n\nIf you're curious, my naive idea is to simple allocate an integer together with the object/array where this integer is the size of the array or simple 1 in case of being an object. Then, when calling delete, it checks the value of the integer, if it is 1, it calls the destructor. If it greater than 1, then it iterates the array calling the destructor to each object in the array. As I said, it seems to work and would let the programmer just write new/delete instead of new/delete vs new[]/delete. But then again, maybe there's a catch that I am not seeing.\n\nEdited part:\n\nAfter some answers, I decided to try to provide some pseudo-code and a better background.\n\nIn C language, memory allocations are usually made with malloc() and de-allocations with free(). Either if you are allocating a single POD, a single struct or an array, malloc() fits all these cases. There is no need for different versions of malloc() if you are allocating a single struct vs a malloc_array() version if you are allocating an array. At least at the public API level. In other words, it seems it doesn't matter if you are allocating a few bytes or many bytes, there will be no overhead for bookkeeping the allocation size information.\n\nAs many of you are aware, including myself, new and delete do more than just allocate and de-allocate memory. New allocate memory and call the constructor and delete calls the destructor and then de-allocate memory. But in C++, you need to be aware if you are allocating just a single object or an array of objects. In case you are allocating an array, you need to use the new[]/delete[] pair.\n\nIn C, if you implement a binary tree, nodes will be allocated with malloc and de-allocated with free and in C++ with new and delete. But if you are implementing something like the vector class in C++, in C you still would use malloc/free, but now in C++ you would need to use new[]/delete[] (considering a sane implementation without too much black magic).\n\nConsider the following pseudo-code that is executed by the compiler. In this pseudo-code, the delete function somehow gets access to the malloc internals and knows how many bytes there are, which in turn can be easily used to calculate how many objects there are. As this delete implementation is using malloc internals to know how much memory is allocated, in theory there should be no overhead of bookkeeping.\n\n```\n// ClassType is a meta type only know by the compiler\n// it stores a class info such as name, size, constructors and so on\nvoid *new(ClassType c) {\n // allocates memory with malloc. Malloc() do the storage bookkeeping\n // note that somehow malloc is allocating just a single object\n c *ptr = malloc(sizeof(c));\n\n // now, call the constructor of the requested class\n c.constructor(ptr);\n\n // return the new object\n return ptr;\n}\n\nvoid *new(ClassType c, size_t n) {\n c *ptr = malloc(sizeof(c) * n);\n\n // iterate over the array and construct each object\n for (i = 0; i < n; ++i) {\n c.constructor(ptr[i]);\n }\n\n return ptr;\n}\n\n// this delete version seems to be able to de-allocate both single\n// objects and arrays of objects with no overhead of bookkeeping because\n// the bookkeeping is made by malloc/free. So I would need \n// just a new/delete pair instead of new/delete vs new[]/delete[]\n// Why C++ doesn't use something like my proposed implementation? \n// What low-level details prohibits this implementation from working?\nvoid delete(ClassType c, void *ptr) {\n // get raw information of how many bytes are used by ptr;\n size_t n = malloc_internals_get_size(ptr);\n\n // convert the number of bytes to number of objects in the array\n n = c.bytesToClassSize(n);\n\n c* castedPointer = (c*) ptr;\n\n // calls the destructor\n for (i = 0; i < n; ++i) {\n c.destructor(castedPointer[i]);\n }\n\n // free memory chunk\n free(ptr);\n}\n```\n\n========================================\n\nTop Answer:\nwhy in C malloc/free can allocate de-allocate both single 'objects'\n\nMalloc doesn't create any objects. It allocates \"raw memory\" which doesn't contain any objects. Correspondingly, `free` doesn't destroy any objects. `new` expressions do create objects, and `delete` destroys an object, while `delete[]` destroys an array of objects.\n\nIn order for the language implementation to know how many objects need to be destroyed by `delete[]`, that number has to be stored somewhere. In order for the language implementation to know how many objects need to be destroyed by `delete`, that number does **not** need to be stored anywhere because it is always one.\n\nStoring a number is not free, and storing an unused number is an unnecessary overhead. The different forms of deletion exist so that the language implementation can destroy the correct number of objects without having to store the number of objects created by a non-array `new`.\n\nthen how malloc/free handles this overhead?\n\nmalloc/free doesn't have this overhead since it doesn't create or destroy objects. As such, there is nothing that needs to be handled.\n\nThere is an analogous issue of storing the number of allocated bytes that malloc does need to deal with. There is no analogous separate function for allocation or freeing of a single byte. This may be because such use case is probably rare. Malloc has more clever ways of dealing with storing this because allocating more memory than is needed is not observable, while such trick is not possible with number of objects because creation and destruction of objects is observable (at least in case of non-trivial types).\n\n`new` typically deals with the issue of storing the number of allocated bytes through using malloc internally.\n\ncouldn't the compiler be smart enough to generate the appropriate code under the hood\n\nNot without some kind of overhead, no. With overhead yes, it could.\n\nBut then again, maybe there's a catch that I am not seeing.\n\nI'm not sure if it is a catch that you haven't seen, but the catch with your idea is the overhead of the integer that is to be allocated even when a single object is allocated.\n\n========================================\n\nCode:\n```cpp\n// ClassType is a meta type only know by the compiler\n// it stores a class info such as name, size, constructors and so on\nvoid *new(ClassType c) {\n    // allocates memory with malloc. Malloc() do the storage bookkeeping\n    // note that somehow malloc is allocating just a single object\n    c *ptr = malloc(sizeof(c));\n\n    // now, call the constructor of the requested class\n    c.constructor(ptr);\n\n    // return the new object\n    return ptr;\n}\n\nvoid *new(ClassType c, size_t n) {\n    c *ptr = malloc(sizeof(c) * n);\n\n    // iterate over the array and construct each object\n    for (i = 0; i < n; ++i) {\n        c.constructor(ptr[i]);\n    }\n\n    return ptr;\n}\n\n// this delete version seems to be able to de-allocate both single\n// objects and arrays of objects with no overhead of bookkeeping because\n// the bookkeeping is made by malloc/free. So I would need \n// just a new/delete pair instead of new/delete vs new[]/delete[]\n// Why C++ doesn't use something like my proposed implementation? \n// What low-level details prohibits this implementation from working?\nvoid delete(ClassType c, void *ptr) {\n    // get raw information of how many bytes are used by ptr;\n    size_t n = malloc_internals_get_size(ptr);\n\n    // convert the number of bytes to number of objects in the array\n    n = c.bytesToClassSize(n);\n\n    c* castedPointer = (c*) ptr;\n\n    // calls the destructor\n    for (i = 0; i < n; ++i) {\n        c.destructor(castedPointer[i]);\n    }\n\n    // free memory chunk\n    free(ptr);\n}\n```\n\n```text\nnew\n```\n\n```text\ndelete\n```\n\n```text\ndelete\n```\n\n```text\ndelete[]\n```\n\n```text\nstd::size_t\n```\n\n```text\nnew[]\n```\n\n```text\ndelete[]\n```\n\n```text\nfree\n```\n\n```text\nnew\n```\n\n```text\ndelete\n```\n\n```text\ndelete[]\n```\n\n```text\ndelete[]\n```\n\n```text\ndelete\n```\n\n```text\nnew\n```\n\n```text\nnew\n```\n\n```text\n#include <iostream>\n// class-specific allocation functions\nstruct X {\n    static void* operator new(std::size_t sz)\n    {\n        std::cout << \"custom new for size \" << sz << '\\n';\n        return ::operator new(sz);\n    }\n    static void* operator new[](std::size_t sz)\n    {\n        std::cout << \"custom new[] for size \" << sz << '\\n';\n        return ::operator new(sz);\n    }\n};\nint main() {\n     X* p1 = new X;\n     delete p1;\n     X* p2 = new X[10];\n     delete[] p2;\n}\n```\n\n========================================\n\nComments:\n- free will not call the destructor, delete will. If there's a side effect in the destructor you are basically adding a bug which will be hard to find.\n- If you look at `malloc` declaration, it accepts size in bytes and returns an unitialized chunk of memory of type `void *`, which `free` later releases. On the contrary, `new` constructs obects and `delete` destructs them so it needs to know it should act on each element of the array. It could be made array-aware, but they chose such a boilerplate approach, I have no idea why.\n- I am aware of this, but it doesn't explain why free can handle both single 'object' and arrays but in C++ we need delete/delete[]. If my question is not clear on this topic, please help me improve it\n- *And because of that, you should be aware of this extra overhead.* `new` could be doing a lot of things. So could `malloc`. Or whatever is beneath `malloc`. You could ask for 2 bytes and get a 4K allocation if that what the source of memory has available to give.\n- Your \"naive idea\" implements `delete[]`. To implement `delete` it is not necessary to have that integer at all, hence lesser overhead\n- @HadleySiqueira \"*it doesn't explain why free can handle both single 'object' and arrays*\" - `malloc()`/`free()` has no concept of arrays at all. `malloc()` just allocates a single chunk of memory, and `free()` just deallocates it. They don't care what is stored inside that memory, that is your responsible to handle. `new`/`new[]`, on the other hand, do care what is in the memory, so they need to differentiate.\n- @RemyLebeau what I meant is that it seems that there is no overhead to malloc allocate a single byte vs many bytes and free still be able to de-allocate properly having access to how many bytes were allocated. On the other hand, considering the other answers, it seems that the overhead for new[]/delete[] seems to be considerable. I know malloc just allocates memory, but somehow knows how much was allocated and there is no overhead. So why can't new[]/delete[] uses the same storage scheme as malloc/free? Which in turn would be superfluous and lead to only use new/delete\n- @HadleySiqueira the overhead of `malloc()`/`free()` is *implementation-defined*. What you describe is just 1 possible implementation. The standard doesn't dictate any implementation. And anyway, `malloc()` and `new[]` are *semantically* different, as `new[]`/`delete[]` have to loop through array elements individually for non-trivial types, while `malloc()`/`free()` don't. So the *element* count has to be stored somewhere no matter what, whereas the *byte* count can be optimized. And FYI, many implementations of `new`/`delete` and `new[]`/`delete[]` use `malloc()`/`free()` internally\n- You've answered the title, but the body of the question is far more complex and your answer does not answer the real question, which is why are there non-array forms of `new` and `delete`\n- @MooingDuck it seems to me the core issue of the question is answered by noting that `delete[]` has to call several destructors\n- @M.M: The OP is aware of that, and thus why `delete[]` is needed. OP seems to want to know why there is `delete` which *can't* do that.\n- \"seems to want to know why there is delete which can't do that\". That's right. I would like to know the reason for this\n- Then perhaps there were some use cases when these operators were proposed that could benefit from 'radical' different implementations? It could be. But as I am just a regular programmer, I never came across with such situation where I could benefit of having different implementations\n- @HadleySiqueira In specialized systems where small few objects might be less important than big long arrays but pose threat of defragmenting heap: you eventually loose ability to allocate a large array. Also, afaik on Windows allocation for array implemented differently because how memory model of OS works, at least in MS runtime. Java's memory model is object oriented and WM defragments and able move things around, the \"pointer\" there would be an ID of created memory object. On certain level Windows memory model acts in same way, App \"lock\" object in place and receives its address.","metadata":{"transformedAt":"2026-08-18T18:32:17.830Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":261,"estimatedTokens":3526}}815{"id":"stack-4424231","source":"stackoverflow","questionId":4424231,"title":"The type of the conditional expression can not be determined?","tags":["c#",".net","compiler-construction"],"text":"Title: The type of the conditional expression can not be determined?\nTags: c#, .net, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI just encountered this (made up code to demonstrate the \"problem\"):\n\n```\npublic ICollection CreateCollection(int x)\n{\n ICollection collection = x == 0 \n ? new List() \n : new LinkedList();\n return collection;\n}\n```\n\nThe compiler complains:\n\n Fehler CS0173: Der Typ des bedingten Ausdrucks kann nicht bestimmt werden, weil keine implizite Konvertierung zwischen \"System.Collections.Generic.List\" und \"System.Collections.Generic.LinkedList\" erfolgt.\n\nWhich translates roughly to:\n\n The type of the conditional operator can not be determined, because there is no implicit conversion between List and LinkedList.\n\nI can see why the compiler complains, but hey, come on. It is trying to play stupid. I can see that both expressions are not of the same type but have one common ancestor and as a bonus the type of the left side is also a common ancestor. I am sure the compiler can see it too. I could understand the error if the left side was declared as `var`.\n\nWhat am I missing here?\n\n**Edit:** \n\nI am accepting James Gaunt's explanation. Maybe Just to make it clear. I can read the compiler spec just fine. I wanted to understand why. Why did someone make the decision to write the spec that way. There must be a reason behind that design. According to James the design principle is 'no surprises'. Also CodeInChaos explains what surprises you might encounter if the compiler would try to deduce the type from common ancestors.\n\n========================================\n\nTop Answer:\nBecause of interfaces they can have multiple different common ancestors.\n\nOne could add a requirement that it only auto-converts if the ancestor is unambiguous. But then adding additional interfaces a class implements suddenly becomes a breaking change. And that might not be desirable.\n\nFor example suppose you make these types implement `ISerializeable`. This shouldn't change the behavior of your code, but if you supported that casting to common interface it would.\n\nedit: Thought a bit more about it and noticed that this function already has exactly the same problem:\n\n```\nT MyFunc(T left,T right)\n```\n\nAnd this code doesn't compile:\n\n```\nICollection r=MyFunc(new List() , new LinkedList());\n```\n\nbecause it can't decide which type to use as the type-parameter `T`. So the behavior of the ?: operator is consistent with overload resolution.\n\n========================================\n\nCode:\n```text\npublic ICollection<string> CreateCollection(int x)\n{\n    ICollection<string> collection = x == 0 \n                                   ? new List<string>() \n                                   : new LinkedList<string>();\n    return collection;\n}\n```\n\n```text\nvar\n```\n\n```text\nICollection\n```\n\n```text\nIEnumerable\n```\n\n```text\nICollection<T>\n```\n\n```text\nIEnumerable<T>\n```\n\n```text\nObject\n```\n\n```text\n? (ICollection<string>) new List<string>() \n: (ICollection<string>) new LinkedList<string>();\n```\n\n```text\n?:\n```\n\n```text\nif/else\n```\n\n```text\nb ? x : y\n```\n\n```text\ndouble a = 7 / 2; // a becomes 3.0\n```\n\n```text\nICollection<string>\n```\n\n```text\nT MyFunc<T>(T left,T right)\n```\n\n```text\nICollection<string> r=MyFunc(new List<string>() , new LinkedList<string>());\n```\n\n```text\nISerializeable\n```\n\n```text\nT\n```\n\n========================================\n\nComments:\n- Try sticking an explicit cast to `ICollection` on one of the conditional results.\n- I know how to circumvent it, I just want to understand it.\n- what if... the compiler would create an anonymous type that would (A) implement all the common interfaces (B) act as an wrapper for the selected object?\n- @m0sa That wouldn't work well because suddenly the result pointed to a different instance.\n- A machine is complaining to you. I'm pretty sure you are smarter than that machine, you know what to do. The rule in the language spec is just as mechanical, it stops you from aiming a gun at your foot and pulling the trigger. It doesn't blow your foot off, it blows up in your face.\n- I am not complaining about the complaining. I want to understand the reason.\n- But still do both expressions on the right side have an common ancestor (type-wise).\n- they have many common ancestors, which makes the compiler throw an error... would you expect this to work as well? (var obj = true ? 1 : \"0\"), what type would obj be now? according to your logic it would be of type Object, but thats pretty far fetched.\n- Well, at least the compiler could try to be smart about it. As a customer always complains: \"Why do I have to do that, let the computer do that...\" ;-)\n- @Eric: If the compiler tried to be smart then it would probably get it wrong in a significant number of cases. Which would you prefer: a compiler that silently produces incorrect logic half of the time, or a compiler that acts dumb but always generates the correct code?\n- I know that. The F# compiler IS smart about it and it does figure it out correctly. I don't know if the F# syntax forbids any ambiguous situations...\n- Well with `var` the compiler tries to guess...\n- No it doesn't guess. var can only be used if the RHS type is known. var doesn't make the compiler do anything different - it just saves you typing.\n- This kind of question comes up regularly on Eric Lippert's blog. At the end of the day it's a core design principle of C#, sometimes he refers to it as 'no surprises'.\n- OK. That's what I wanted to know.\n- I understand that. I want to know why.\n- @EricSch, OK, I quoted the rules.\n- I can read the rules. Again: Why? It's a design decision which was made for a reason. I wanted to know the reason...\n- Now this makes sense (especially the part about later addition of new ancestors).","metadata":{"transformedAt":"2026-08-18T18:32:17.830Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":156,"estimatedTokens":1439}}816{"id":"stack-67594976","source":"stackoverflow","questionId":67594976,"title":"Example assembly/machine instruction from lambda calculus","tags":["assembly","functional-programming","compilation","compiler-construction","lambda-calculus"],"text":"Title: Example assembly/machine instruction from lambda calculus\nTags: assembly, functional-programming, compilation, compiler-construction, lambda-calculus\nSource: Stack Overflow\n\nQuestion:\nI'm learning a bit of lambda calculus and one of the things that I'm quite curious about is how the totally-abstract functions might actually be applied in instructions. Let's take the following example, where I'm allowing small natural numbers (as a given) and a `TRUE` `FALSE` definition.\n\nFor example, let's use the following code, which should evaluate to `5`:\n\n```\n# using python for the example but anything would suffice\nTRUE = lambda x: lambda y: x # T = λab.a\nFALSE = lambda x: lambda y: y # F = λab.b\nTRUE(5)(2) # T('5')('2')\n```\n\nHow might this be implemented in a lambda-calculus-like instruction to evaluate this? One thing I thought of is to \"un-lambda-ify\" it, so it just comes out to something like:\n\n```\n// uncurried\nint TRUE(int x, int y) {\n return x;\n}\nint FALSE(int x, int y) {\n return y;\n}\nTRUE(2, 5);\n```\n\nWhich might come out to something along the lines of:\n\n```\nSYS_EXIT = 60\n.globl _start\n\nTRUE: # faking a C calling convention\n mov %edi, %eax\n ret\n\n_start:\n mov $5, %edi\n mov $2, %esi\n call TRUE\n mov %eax, %edi\n mov $SYS_EXIT, %eax\n syscall\n```\n\nIs that how this is done, or what's a closer explanation of how a functional/lambda-like language might be compiled into instructions?\n\n========================================\n\nTop Answer:\nThere are many evaluation and compilation strategies for functional languages based on the lambda calculus. For simplicity, I'll assume you're talking about how one would treat your example as if it were really a strict functional language (where we have applicative-order call-by-value application and a suitable encoding of integer literals).\n\nIf we consider the program you've given:\n\n```\n(λx.λy.x) 5 2\n```\n\nThe first step is usually to perform some kind of unique-renaming where we ensure lexical scope is apparent (as you may know, `λx.λx.x` is alpha-equivalent to `λy.λx.x` - although the latter is more clear) and that potential code motion optimisations don't accidentally lead to scoping conflicts. The program above already has unique names so I'll avoid it as a step here.\n\nThe second step is to perform some kind of conversion into a normal form representation (where intermediary values produced by computations become bound to variables given unique names). For strict functional languages, generally one decides between ANF (A Normal Form) and CPS (Continuation Passing Style). I'll go with a variant of ANF for simplicity (you can find thorough treatment of the ideas of CPS compilation in Andrew Appel's \"Compiling with Continuations\" if you're interested).\n\nHere's a potential result of this kind of transformation:\n\n```\nlet f x = \n let g y = x in g\nin\nlet x0 = f 5 in\nlet x1 = x0 2 in\nx1\n```\n\nAs you can see, the previously-anonymous functions (lambda abstractions) are now represented by named functions and the intermediary results of the two applications are now bound to variables. There are quite a few transformations one can do within this form but this answer focuses more on introducing this representation for structural clarity rather than any optimisations we could apply.\n\nThe next step is to perform closure conversion. This step addresses a problem that occurs when compiling code with higher-order functions whose bodies capture values outwith their own lexical scope. Since we have not performed an uncurrying transformation, a partial application is present in our code (`f 5`). This must return a function that, when applied, returns the `x` we provided to `f` (regardless of the scope). To solve this without runtime code generation, compiler developers opt to represent these higher-order functions using a \"closure\" data structure\n(so-called because it *closes* over the function by providing values to free variables).\n\nThe transformation itself adapts functions to take a supplementary argument (to their environment) and the returning of higher-order functions to return a closure data structure (in our case we'll use a \"flat\" closure: a pair consisting of a code pointer and a pointer to an environment that will be represented in memory by a record). On top of this, all call sites must be adapted to destructure the returned closure in order to apply it.\n\nThat sounds like a lot but here's some pseudo-code to demonstrate this transformation performed naively:\n\n```\nlet f(x, e) =\n let g(y, e') = e'.x in (g, {x})\nin\nlet x0 = f(5, {}) in\nlet g_ptr = x0.0 in\nlet g_env = x0.1 in\nlet x1 = g_ptr(2, g_env) in\nx1\n```\n\nHere, I've used brace syntax to represent the construction of environments (so `{}` is an empty environment - an artefact left over from this naive transformation - and `{x}` represents a heap-allocated record storing the value of `x`). The pair syntax `(g, {x})` heap-allocates a pair (as a structure) where the first component is the code pointer for `g` and the second component is the pointer to the heap-allocated record for `{x}`. The `.0` and `.1` projections represent accessing these components. You may also notice that I've adopted multi-argument application syntax `f(x,y,...)` - this should not be confused with the syntax I've used for pairs.\n\nAfter closure conversion, the program is closed and, therefore, we can safely perform the so-called \"hoisting\" transformation where we lift nested functions into the global scope (so they appear more like C-like functions which are simpler to compile - a lot of the compilation process is a kind of linearisation).\n\nThe result of hoisting could look like:\n\n```\nlet g(y, e') = e'.x \n\nlet f(x, e) = (g, {x})\n\nlet entry() =\n let x0 = f(5, {}) in\n let g_ptr = x0.0 in\n let g_env = x0.1 in\n let x1 = g_ptr(2, g_env) in\n x1\n```\n\nFrom here, generally compilers may go to some kind of three-address code IR that has some notion of aggregate and pointers types (to conveniently deal with the auxilliary structures introduced by closure conversion). It is entirely possible to lower the above representation into LLVM (with a few cheap `i64*` casting tricks - notice that each function is conveniently 2-argument so we don't need to book-keep any typing information to produce a `call`). As an exercise, you may wish to convert the above into C code (which is also straightforward). But, since your answer wanted some assembly, here's a very naive (leaking) implementation:\n\n```\n.data\nfmt: .asciz \"result = %lld\\n\"\n \n .text\n .globl main\ng:\n mov 0(%rsi), %rax # get and return x from environment\n ret\n \nf:\n sub $24, %rsp\n mov %rdi, (%rsp) # preserve x\n mov $8, %edi # allocate space for {x}\n call malloc@plt\n mov (%rsp), %rcx # load and store x into environment\n mov %rcx, 0(%rax)\n mov %rax, 8(%rsp) # preserve environment\n mov $16, %edi # allocate closure pair (2 pointers)\n call malloc@plt\n lea g(%rip), %rcx\n mov %rcx, 0(%rax) # store g's code pointer as first component\n mov 8(%rsp), %rcx\n mov %rcx, 8(%rax) # store g's environment, {x}, as second component\n add $24, %rsp\n ret\nmain:\n push %rbx\n mov $5, %edi\n xor %esi, %esi # {} = null, for simplicity\n call f # f(5, {})\n mov 0(%rax), %rbx # extract code pointer\n mov 8(%rax), %rsi # extract environment\n mov $2, %edi\n call *%rbx # g(2, {x})\n # print the result\n mov %rax, %rsi\n lea fmt(%rip), %rdi\n xor %eax, %eax\n call printf@plt\n xor %eax, %eax\n pop %rbx\n ret\n```\n\nNow I'll go into some important, practical, details that I neglected to mention throughout this answer:\n\n- The \"extent\" (or lifetime) of a closure cannot always be deduced so, by default, we choose to allocate them on the heap. If we can deduce that a closure does not *escape upwards* (a process known as \"escape analysis\" is used to discover these cases), we can be more economical about where we allocate these closures (such as on the stack). The cases we want to optimise for are where closures only escape *downwards* (in other words, a higher-order function is only passed down the call-stack but never escapes upwards - in the call-chain's result - or by being assigned to some other location that outlives the closure's creation site).\n\n- The representation of a closure's environment as a record is very important as we'll be relying on garbage collection to collect them. This has a huge bearing on the representations we choose for how we represent unboxed literal values (such as the `5` and `2` in the above example) alongside pointers to heap-allocated *things* (such as closure environments that, themselves, store both integer literals *and* pointers to closure pairs). The way that languages such as OCaml choose to do this is to perform least-significant-bit tagging (by exploiting the alignment of pointers, we can differentiate between unboxed 63 bit integers and 64 bit pointers by checking the lowest bit - this does, however, mean that all arithmetic must be adapted to work on values that are shifted left by 1 place and incremented by 1; so `34` looks like `69` in compiled OCaml programs). On top of this differentiation, garbage collectors must traverse the data structures to find pointers and so we require a fairly homogeneous structure to efficiently do this without compiling in layout information (hence records have strict alignment requirements and store 64 bit values - you can see a diagram of the homogeneous \"block\" layout used by OCaml here).\n\nI hope this answer gives a kind of understanding of how strict, functional, languages that find their roots in the lambda calculus are generally tackled.\n\nTo summarise, the steps are basically:\n\n- Unique renaming (scope-checking can be done at the same time)\n\n- Selective or whole-program transformation into ANF or CPS (involves creating fresh names)\n\n- ANF or CPS-specific optimisations (general things like uncurrying, inline expansion, dead code removal, constant folding, tuple argument flattening, etc.)\n\n- Closure conversion (followed, potentially, by elimination strategies such as lambda lifting of known closures, fix-minimisation using SCCs, etc.)\n\n- Hoisting\n\n- Further lowering into a more machine-like three (or two) address code IR that gives more explicit detail to treatment of auxiliary structures introduced by transformations\n\n- Lowering of that IR into target-specific assembly language\n\nThis is definitely not the whole story when it comes to compilation of strict, functional, languages. For example, if we extended our language to have named, mutually-recursive, functions, it would be desirable to work closure sharing into our closure conversion transformation (and also eliminate the cases where closures aren't necessary).\n\n========================================\n\nCode:\n```text\n# using python for the example but anything would suffice\nTRUE = lambda x: lambda y: x      # T = λab.a\nFALSE = lambda x: lambda y: y     # F = λab.b\nTRUE(5)(2)                        # T('5')('2')\n```\n\n```text\n// uncurried\nint TRUE(int x, int y) {\n    return x;\n}\nint FALSE(int x, int y) {\n    return y;\n}\nTRUE(2, 5);\n```\n\n```text\nSYS_EXIT = 60\n.globl _start\n\nTRUE:                      # faking a C calling convention\n    mov %edi, %eax\n    ret\n\n_start:\n    mov $5, %edi\n    mov $2, %esi\n    call TRUE\n    mov %eax, %edi\n    mov $SYS_EXIT, %eax\n    syscall\n```\n\n```text\nTRUE\n```\n\n```text\nFALSE\n```\n\n```text\n5\n```\n\n```text\n(define true (λ x (λ y x)))\n(define false (λ x (λ y y)))\n```\n\n```text\n(define true (lambda (x) (lambda (y) x)))\n(define false (lambda (x) (lambda (y) y)))\n```\n\n```text\n(define cond (λ p (λ a (λ b ((p a) b)))))\n```\n\n```text\n(define cond (lambda (p)\n               (lambda (a)\n                 (lambda (b)\n                   (((release* p) a) b)))))\n```\n\n```text\n(((cond p) a) b)\n```\n\n```text\n(((cond\n    (hold p))\n  (hold a))\n (hold b))\n```\n\n```text\n> (disassemble (λ x x))\n       0: 4883fd01                       (cmp rbp #x1)\n       4: 7507                           (jnz (+ rip #x7)) ; => d\n       6: 4c89c5                         (mov rbp r8)\n       9: 41ff6500                       (jmp (mem64+ r13 #x0))\n       d: e96ecc35f8                     (jmp (+ rip #x-7ca3392)) ; #<code doargerr> ; <=\n      12: 0f1f8000000000                 (data)\n```\n\n```text\n> (disassemble (λ x 1))\n       0: 4883fd01                       (cmp rbp #x1)\n       4: 750b                           (jnz (+ rip #xb)) ; => 11\n       6: 48c7c508000000                 (mov rbp #x8)\n       d: 41ff6500                       (jmp (mem64+ r13 #x0))\n      11: e96acc0cc7                     (jmp (+ rip #x-38f33396)) ; #<code doargerr> ; <=\n      16: 0f1f8000000000                 (data)\n```\n\n```text\n(λ x y)\n```\n\n```text\n(x y)\n```\n\n```text\n(hold x)\n```\n\n```text\nx\n```\n\n```text\ndelay\n```\n\n```text\ndelay\n```\n\n```text\nhold\n```\n\n```text\nhold\n```\n\n```text\n(release* x)\n```\n\n```text\nhold\n```\n\n```text\nrelease*\n```\n\n```text\nforce\n```\n\n```text\nhold\n```\n\n```text\nrelease*\n```\n\n```text\nhold\n```\n\n```text\nλ\n```\n\n```text\nlambda\n```\n\n```text\np\n```\n\n```text\n(λ x y)\n```\n\n```text\n(lambda (x) y)\n```\n\n```text\n(x y)\n```\n\n```text\n((release* x) (hold y))\n```\n\n```text\n(λ x x)\n```\n\n```text\nfactorial\n```\n\n```text\noa/normal/pure\n```\n\n```text\nrbp\n```\n\n```text\nr8\n```\n\n```text\n(mov rbp #x8)\n```\n\n```text\n1\n```\n\n```text\nrbp\n```\n\n```text\n(λx.λy.x) 5 2\n```\n\n```text\nlet f x = \n  let g y = x in g\nin\nlet x0 = f 5 in\nlet x1 = x0 2 in\nx1\n```\n\n```text\nlet f(x, e) =\n  let g(y, e') = e'.x in (g, {x})\nin\nlet x0 = f(5, {}) in\nlet g_ptr = x0.0 in\nlet g_env = x0.1 in\nlet x1 = g_ptr(2, g_env) in\nx1\n```\n\n```text\nlet g(y, e') = e'.x \n\nlet f(x, e) = (g, {x})\n\nlet entry() =\n  let x0 = f(5, {}) in\n  let g_ptr = x0.0 in\n  let g_env = x0.1 in\n  let x1 = g_ptr(2, g_env) in\n  x1\n```\n\n```text\n.data\nfmt:    .asciz \"result = %lld\\n\"\n    \n    .text\n    .globl main\ng:\n    mov 0(%rsi), %rax # get and return x from environment\n    ret\n    \nf:\n    sub $24, %rsp\n    mov %rdi, (%rsp)  # preserve x\n    mov $8, %edi      # allocate space for {x}\n    call malloc@plt\n    mov (%rsp), %rcx  # load and store x into environment\n    mov %rcx, 0(%rax)\n    mov %rax, 8(%rsp) # preserve environment\n    mov $16, %edi     # allocate closure pair (2 pointers)\n    call malloc@plt\n    lea g(%rip), %rcx\n    mov %rcx, 0(%rax) # store g's code pointer as first component\n    mov 8(%rsp), %rcx\n    mov %rcx, 8(%rax) # store g's environment, {x}, as second component\n    add $24, %rsp\n    ret\nmain:\n    push %rbx\n    mov $5, %edi\n    xor %esi, %esi    # {} = null, for simplicity\n    call f            # f(5, {})\n    mov 0(%rax), %rbx # extract code pointer\n    mov 8(%rax), %rsi # extract environment\n    mov $2, %edi\n    call *%rbx        # g(2, {x})\n    # print the result\n    mov %rax, %rsi\n    lea fmt(%rip), %rdi\n    xor %eax, %eax\n    call printf@plt\n    xor %eax, %eax\n    pop %rbx\n    ret\n```\n\n```text\nλx.λx.x\n```\n\n```text\nλy.λx.x\n```\n\n```text\nf 5\n```\n\n```text\nx\n```\n\n```text\nf\n```\n\n```text\n{}\n```\n\n```text\n{x}\n```\n\n```text\nx\n```\n\n```text\n(g, {x})\n```\n\n```text\ng\n```\n\n```text\n{x}\n```\n\n```text\n.0\n```\n\n```text\n.1\n```\n\n```text\nf(x,y,...)\n```\n\n```text\ni64*\n```\n\n```text\ncall\n```\n\n```text\n5\n```\n\n```text\n2\n```\n\n```text\n34\n```\n\n```text\n69\n```\n\n========================================\n\nComments:\n- How do numbers work in your lambda calculus? Are they supposed to be syntactic sugar for church encodings and translating them to `int` is part of your \"unlambdafication\"? Or are they a separate type? In the latter case, does that mean that your LC is dynamically typed or are `TRUE` and `FALSE` supposed to have their types inferred as `int -> int -> int` based on how they are used? That is, if you added `TRUE(FALSE)(TRUE)` as another expression, would that be a type error? Because if that's a valid way to call `TRUE`, you obviously can't make its arguments ints in the translation.\n- @sepp2k to be honest, I'm not really sure, but was more curious how \"it's been done before\". The only thing I can think of is the nonsense of storing the number `32,768` as (at least) 32k instructions, so this isn't so much a theoretical question as something like \"How is a practical FP language that is based on lambda calculus implemented in machine instructions, using the above as the simplest example I can think of?\" if that helps...\n- There's a big difference between a \"practical FP language that is based on lambda calculus\" and the actual lambda calculus, but even so it still makes a huge difference whether your practical FP language is dynamically or statically typed. But even for a statically typed language it'd be unlikely that a definition like `myTrue x y = x` would lead to generated code with an `int` type without any type annotations unless the optimizer specializes the type at the call site. But of course you wouldn't actually define church-encoded booleans in a practical FP language except as an exercise.\n- Been digging into lambda calculus and brainfuck lately while building redstone computers based off the Commodore 64 architecture. Yes I got odd hobbies, but can say that from one point of view. Lambda Calc is like the ALU to a turing machine's CPU. One can use both to calculate but Lambda skips the need for machine code (e.g. move pointer to x, read/write register).","metadata":{"transformedAt":"2026-08-18T18:32:17.833Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":69,"totalLines":579,"estimatedTokens":4280}}817{"id":"stack-8203139","source":"stackoverflow","questionId":8203139,"title":"Any Java Bytecode Generation Guide?","tags":["java","compiler-construction","jvm","pascal","bytecode"],"text":"Title: Any Java Bytecode Generation Guide?\nTags: java, compiler-construction, jvm, pascal, bytecode\nSource: Stack Overflow\n\nQuestion:\nwe're writing some sort of **compiler** from **Pascal** to **JVM Bytecode**. And we've already implemented an expression tree generation, so the next step should be the creation of `.class` file. Can you suggest any guide/tutorial of how to generate any `.class` file at least from some static data? Because I've googled for 2 hours already and read JVM specification, but I really need some even simplest example to start developing the whole stuff.\n\n========================================\n\nTop Answer:\nSomeone has already written a widely-used byte code generation library: CGLIB. \n\nYou'd have it knocked if you could figure out how to get your AST into CGLIB.\n\n========================================\n\nCode:\n```text\n.class\n```\n\n```text\n.class\n```\n\n```text\nexamples\n```\n\n```text\n.class\n```\n\n```text\n.class\n```\n\n========================================\n\nComments:\n- thank you! the only sad thing is that there are no tutorials about code generation :( only about `.class` files manipulations. Anyways I'll try to come up with some solution\n- Would it work if you could figure out how to map your Pascal AST onto Java Beans? That's the hard part, since Pascal doesn't support objects as far as I know. CGLIB can't help you with that; no library will do that automagically. That's where you and your domain knowledge come in.\n- Pascal doesn't support objects? It's not true for decades.\n- Been decades since I wrote it, so that's my mistake. I have no idea of how object-oriented the source code is, and neither does anyone else except the OP.\n- avra: pascal doesn't, object Pascal (Apple, later turbo pascal and Delphi) does\n- @avra object pascal is crappy, it seams that it's pre-delphi state of pascal :) Moreover when working with non-OOP it's quite easy to gen java `.class`, you just make one file and put all the procedures/functions to methods, and all the app code in **main** method and all the variables as class fields :)\n- +1. Note that it is GPL though, not an ideal source if you want to sell the result.\n- Thans, it's really interesting stuff. I won't sell it. It's a project made during the compilers course in the university. But as all out schools teach **pascal** maybe I'll make some **NetBeans** module, so the pupils will have some nice IDE\n- Why not directly use Lazarus? It is customizable, and they are working on an educational edition? lazarus.freepascal.org wiki.freepascal.org/Lazarus_for_education","metadata":{"transformedAt":"2026-08-18T18:32:17.833Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":49,"estimatedTokens":641}}818{"id":"stack-47655590","source":"stackoverflow","questionId":47655590,"title":"Should a descendant class' method's variable that is identical to Self, have access to its ancestor's protected methods?","tags":["class","delphi","compiler-construction","encapsulation","protected"],"text":"Title: Should a descendant class' method's variable that is identical to Self, have access to its ancestor's protected methods?\nTags: class, delphi, compiler-construction, encapsulation, protected\nSource: Stack Overflow\n\nQuestion:\nThis question arose from an issue that surfaced when using method chaining (fluent interface), and I suppose that's one of the only reasons it might be an issue at all.\n\nTo illustrate, I'll use an example using method chaining:\n\nIn unit A:\n\n```\nTParent = class\nprotected\n function DoSomething: TParent;\nend;\n```\n\nIn unit B:\n\n```\nTChild = class(TParent)\npublic\n procedure DoAnotherThing;\nend;\n\nimplementation\n\nprocedure TChild.DoAnotherThing;\nbegin\n DoSomething.DoSomething\nend;\n```\n\nI want to keep the DoSomething procedure protected and visible only to class descendants.\n\nThis won't compile, throwing a \n\n cannot access protected symbol TParent.DoSomething\n\nbecause DoSomething returns a TParent and the subsequent DoSomething call is issued from a TParent object in another unit (so the protection kicks in and the function is inaccessible). (thanks, David Heffernan, for the explanation)\n\nTo reduce it to its bare essence, something like TParent(Self).DoSomething is not possible inside the TChild class.\n\nMy question:\n\nsince the compiler does know that a copy of the Self parameter is being accessed from within the child class, would there be instances in which the ability to access the ancestor's protected methods breaks encapsulation? I'm only talking about dereferencing the typecasted Self *from inside* a descendant's class method. I'm aware that outside of this class, that parameter should not have access to the ancestor's protected methods (in another unit), of course.\n\nAgain, in short: when a variable, that is identical to the Self parameter, is dereferenced INSIDE one of its own class methods, would it be unsafe for the compiler to allow it access to its parent's protected methods (just like the Self parameter itself)?\n\nIt's a pretty theoretical question, but I'd be interested if it would have any negative impact on compiled code or encapsulation, if the compiler would allow this.\n\nThanks.\n\n========================================\n\nTop Answer:\nWell, the child *might* be identical, but it might not.\n\nConsider a slight expansion to your example.\n\n```\nTParent = class\nprotected\n function DoSomething: TParent;\nend;\n```\n\nand unit 2\n\n```\nTChild = class(TParent)\npublic\n procedure DoAnotherThing;\nend;\n\nTChild2 = class(TParent)\npublic\n procedure DoAnotherThing;\n function DoSomething: Child2;\nend;\n\nimplementation\n\nprocedure TChild.DoAnotherThing;\nbegin\n DoSomething.DoSomething\nend;\n\nprocedure TChild2.DoAnotherThing;\nvar\n p : TParent;\nbegin\n p := DoSomething;\n p.DoSomething;\nend;\n```\n\nNow the value returned by DoSomething certainly is a TParent. It might be a TChild or indeed a TChild2 or even something that you have very little knowledge of (other than that it is descended from TParent).\n\nIf the returned value is really a TChild, then certainly access is no problem - it is knows how to handle its own data type, but if not then it has no right to access the protected functions of the other object. If the returned value is a TChild you can do what you want, like this\n\n```\nprocedure TChild.DoAnotherThing;\nvar\n p : TParent;\nbegin\n p := DoSomething;\n if p is TChild then (p as TChild).DoSomething;\nend;\n```\n\n========================================\n\nCode:\n```text\nTParent = class\nprotected\n  function DoSomething: TParent;\nend;\n```\n\n```text\nTChild = class(TParent)\npublic\n  procedure DoAnotherThing;\nend;\n\nimplementation\n\nprocedure TChild.DoAnotherThing;\nbegin\n  DoSomething.DoSomething\nend;\n```\n\n```text\ntype\n  TChild = class(TParent)\n  public\n    procedure DoAnotherThing;\n  end;\n\nimplementation\n\ntype\n  TParentAccess = class(TParent);\n\nprocedure TChild.DoAnotherThing;\nbegin\n  TParentAccess(DoSomething).DoSomething;\nend;\n```\n\n```text\ntype\n  TChild = class(TParent)\n  private\n    type\n      TParentAccess = class(TParent);\n    function DoSomething: TParentAccess; inline;\n  public\n    procedure DoAnotherThing;\n  end;\n\nimplementation\n\nfunction TChild.DoSomething: TParentAccess;\nbegin\n  Result := TParentAccess(inherited DoSomething);\nend;\n\nprocedure TChild.DoAnotherThing;\nbegin\n  DoSomething.DoSomething;\nend;\n```\n\n```text\ntype\n  TParentHelper = class helper for TParent\n  public\n    function DoSomething: TParent; inline;\n  end;\n\nimplementation\n\nfunction TParentHelper.DoSomething: TParent;\nbegin\n  Result := inherited DoSomething;\nend;\n```\n\n```text\nprotected\n```\n\n```text\nDoSomething\n```\n\n```text\nTChild\n```\n\n```text\nSelf\n```\n\n```text\nDoSomething\n```\n\n```text\nSelf\n```\n\n```text\nTParent\n```\n\n```text\nTChild\n```\n\n```text\nDoSomething\n```\n\n```text\nTParent\n```\n\n```text\nTChild\n```\n\n```text\nTParent\n```\n\n```text\nTParent = class\nprotected\n  function DoSomething: TParent;\nend;\n```\n\n```text\nTChild = class(TParent)\npublic\n  procedure DoAnotherThing;\nend;\n\nTChild2 = class(TParent)\npublic\n  procedure DoAnotherThing;\n  function DoSomething: Child2;\nend;\n\nimplementation\n\nprocedure TChild.DoAnotherThing;\nbegin\n  DoSomething.DoSomething\nend;\n\nprocedure TChild2.DoAnotherThing;\nvar\n  p : TParent;\nbegin\n  p := DoSomething;\n  p.DoSomething;\nend;\n```\n\n```text\nprocedure TChild.DoAnotherThing;\nvar\n  p : TParent;\nbegin\n  p := DoSomething;\n  if p is TChild then (p as TChild).DoSomething;\nend;\n```\n\n========================================\n\nComments:\n- Bad question? Downvoter care to elaborate?\n- Nothing stops you from accessing a protected member if you must, it's just a bit awkward in the latest Delphi versions.\n- @LURD I don't want to access a protected member forcibly or with workarounds, just wanted to know if in this particular case, the compiler should allow it, or what negative impact it would have.\n- For good or bad, the compiler architects decided the rules. Your question is not a good fit here, since it invites speculation.\n- This is a classic opinion based question and should be closed. If you want a discussion, ask on the Google+ delphi devs page.\n- FWIW Stackoverflow is full of similar questions for other languages (like C++ and C# and they are not being closed for whatever reason) with incredibly good answers - valid question and possible to answer without being opinion based.\n- @StefanGlienke I doubt that's true. But I can also see that there is a technical answer to be written here, which you have done, which renders the opinion based part of the question moot.\n- You argue that the question is \"primarily opinion-based\" which I argued it is not but close it as \"unclear what you're asking\"? It is totally clear what he is asking.\n- @stefan there are 5 close votes, you can't know which reason I personally selected.\n- That's something I don't get. Three of those voters have nothing to do with Delphi but want clarification wrt the question? Will clarification give them knowledge into the subject matter?\n- @DavidHeffernan I saw the votes when it was 3 and all of them were \"unclear what you're asking\" making it the majority of required votes.\n- @domus The close voters aren't looking for subject matter knowledge. They are voting to maintain the site.\n- @stefan Personally I think that a case could be made for unclear, too broad or opinion based.\n- @DavidHeffernan You mean like on this question: stackoverflow.com/q/46012021/587106 ?\n- @Stefan I would agree\n- @DavidHeffernan I appreciate that they are voting to maintain the site, but how do they judge the completeness/clarity of a question in a subject matter that is alien to them?\n- @Domus I'm not sure you can judge their subject matter, but even so I think it is perfectly possible to cast such votes without detailed knowledge of Delphi itself. You don't need to know Pascal syntax to understand the nature of this question.\n- @Domus Delphi is not the only tag only the question. So people with no interest in Delphi could be drawn to your question based on other interests; and could quite understandably evaluate your question on its non-Delphi merits (with probably less than a quarter of your question being Delphi-specific; *it's not the only OO language to implement **protected** scope*).\n- @CraigYoung So the lesson is to use as few tags as possible in order to not attract any suspicious individuals. *I'm kidding!* These Chinese++ and Javascript lads are of course welcome to cast their judgement on other-language topics. I'm off to molest some Logo rascals. (kidding again)\n- @Domus: you may be kidding, but that's at least 50% true. The tags define how broad an answer you're looking for. By tagging with class compiler-construction protected you're opening the question to *how should any object oriented language be designed* (could be technical if you're looking for technical tradeoffs, but leans towards opinion) and therefore to the consequential *is this Delphi design choice a good one* (pure opinion).\n- @torek - Personally, I would have expected the use of tags to narrow down the context of the question? More tags = more specific?\n- @LarsFosdal: that's not an unreasonable theory, but in practice tags seem to get \"or\"ed rather than \"and\"ed: you get the eyeballs of anyone with an interest in any one of these things, rather than all of these things combined.\n- @torek I always use them with an \"and\" interpretation. But you're right, humans don't function like compilers (to stay on-topic).\n- Thanks for your answer, Stefan. I don't want access to methods of other instances, though. I am specifically limiting the access to variables that are identical to Self *and* are used *inside* a descendant class' method. I probably didn't make myself clear, or I'm misreading your answer. I realize that what I'm trying to get at is only useful in method chaining.\n- @Domus, if it duck typing yoau are after, look Duck typing in Delphi 2007?\n- @LURD Not exactly, but thanks for that reference! Had read about duck typing a decade or so ago, but it was a nice refresher!\n- @Domus As I wrote in my second paragraph - it would define an exception to OOP rules just for one specific value which the compiler could only find with static code analysis - i.e. finding out that DoSomething returns Self. It just sees the return type and applies standard OOP rules / static typing to it.\n- @StefanGlienke I'll accept it as not possible for the compiler. Of course, I'm sure it *is* possible to do, just not worth the effort. Thanks.\n- Stefan says that it is possible, but argues that it is not worth the downsodes\n- @StefanGlienke, I don't think I tested something similar before... But in that context, I believe you could also redeclare `TChild.DoSomething` as `Result := TParentAccess(inherited DoSomething)`, and do the same with `TParentAccess`. Unless my morning coffee isn't working yet, that would get rid of all the typecasting.\n- @KenBourassa Nice, added that and another possibility that just came to my mind to the answer.\n- @StefanGlienke, I edited to make it closer to what I had in mind. Feel free to revert if you don't like it... Just occured to me we could also remove `TChild.DoSomething`and just do `TChild = class(TParentAccess)`\n- I did *not* want to a) expose the TParentAccess cracker class in the interface part of the unit or b) modify the inheritance chain.\n- can still have a `TChild.TParentAccess.DoSomething` method.\n- Left as an exercise for the reader - I would favor the helper anyway. Implement exactly one method, be done.","metadata":{"transformedAt":"2026-08-18T18:32:17.834Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":20,"totalLines":324,"estimatedTokens":2857}}819{"id":"stack-256220","source":"stackoverflow","questionId":256220,"title":"Register allocation rules in code generated by major C/C++ compilers","tags":["c++","c","assembly","compiler-construction","cpu-registers"],"text":"Title: Register allocation rules in code generated by major C/C++ compilers\nTags: c++, c, assembly, compiler-construction, cpu-registers\nSource: Stack Overflow\n\nQuestion:\nI remember some rules from a time ago (pre-32bit Intel processors), when was quite frequent (at least for me) having to analyze the assembly output generated by C/C++ compilers (in my case, Borland/Turbo at that time) to find performance bottlenecks, and to safely mix assembly routines with C/C++ code. Things like using the SI register for the *this* pointer, AX being used for return values, which registers should be preserved when an assembly routine returns, etc.\n\nNow I was wondering if there's some reference for the more popular C/C++ compilers (Visual C++, GCC, Intel...) and processors (Intel, ARM, ...), and if not, where to find the pieces to create one. Ideas?\n\n========================================\n\nTop Answer:\nAgner Fog's \"Calling Conventions\" document summarizes, amongst other things, the Windows and Linux 64 and 32-bit ABIs: http://www.agner.org/optimize/calling_conventions.pdf. See Table 4 on p.10 for a summary of register usage.\n\nOne warning from personal experience: don't embed assumptions about the ABI in inline assembly. If you write a function in inline assembly that assumes return and/or parameter transfer in particular registers (e.g. eax, rdi, rsi), it will break if/when the function is inlined by the compiler.","metadata":{"transformedAt":"2026-08-18T18:32:17.834Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":15,"estimatedTokens":356}}820{"id":"stack-2937708","source":"stackoverflow","questionId":2937708,"title":"How can a language be interpreted by itself (like Rubinius)?","tags":["ruby","compiler-construction","language-design","rubinius","self-interpreter"],"text":"Title: How can a language be interpreted by itself (like Rubinius)?\nTags: ruby, compiler-construction, language-design, rubinius, self-interpreter\nSource: Stack Overflow\n\nQuestion:\nI've been programming in Ruby for a while now with just the standard MRI implementation of Ruby, but I've always been curious about the other implementations I hear so much about.\n\nI was reading about Rubinius the other day, a Ruby interpreter written in Ruby. I tried looking it up in various places, but I was having a hard time figuring out exactly how something like this works. I've never had much experience in compilers or language writing but I'm really interested to figure it out.\n\nHow exactly can a language be interpreted by itself? Is there a basic step in compiling that I don't understand where this makes sense? Can someone explain this to me like I'm an idiot (because that wouldn't be too far off base anyways)\n\n========================================\n\nTop Answer:\nThe concept you are looking for is **compiler bootstrapping**.\n\nBasically bootstrapping means writing a compiler (or an interpreter) for language *x* in language *x*. This is done either by writing a basic compiler on a lower level by hand (i.e. writing a C compiler in Assembly), or by using a different high-level language.\n\nRead more about bootstrapping on wikipedia. Greg's answer regarding meta-circular evaluators is also highly recommended, including the relevant chapter in SICP.\n\n========================================\n\nComments:\n- Is this just in the case of Rubinius, or do all self interpreted compilers have a little bit of another language involved just to get things started?\n- Rubinius is not alone. I think the question you're asking in the up comment hits on a deeper topic. So, rather than answer it directly, I would point you to this article on wikipedia regarding compilers. en.wikipedia.org/wiki/Compiler Specifically, for this question, take a look at the sections \"Compilation\" and \"Compiler Output\".\n- I think you're missing the point a bit. Yes, if imp is a compiler, I can just compile another compiler and the user can run it on his machine (though if imp-compiled programs require some runtime environment users of the circ-compiler would also need to install the imp-runtime). However if imp and circ are interpreters, anybody who would want to use the circ-interpreter would have to install the imp-interpreter first and then run his lisp-apps by using imp to interpret circ and circ to interpret the app. If this was the case for rubinius nobody would use it.\n- Oh, I see. Thought the OP was asking a slightly different question.","metadata":{"transformedAt":"2026-08-18T18:32:17.834Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":27,"estimatedTokens":658}}821{"id":"stack-1358662","source":"stackoverflow","questionId":1358662,"title":"How .NET differentiates reference vs primitive and value types","tags":[".net","compiler-construction"],"text":"Title: How .NET differentiates reference vs primitive and value types\nTags: .net, compiler-construction\nSource: Stack Overflow\n\nQuestion:\n.NET we have primitive datatypes like int and value types like struct. \n\nAnd also we have reference types. All of them seem to be derived from object class.\n\nHow .NET determine primitive, value type against the reference type?\n\nWhere it is done? At compiler or at JIT?\n\nDoes this belongs to the capabilities of the compilers?\n\n========================================\n\nTop Answer:\nValue types are actually derived from System.ValueType which itself is derived from System.Object.\n\n========================================\n\nCode:\n```text\npublic Enum Shipper {FedEx, Aerborne, USPS, Stork}\n```\n\n```text\nSystem.ValueType\n```\n\n```text\nGetHashCode\n```\n\n```text\nEquals\n```\n\n```text\nint\n```\n\n```text\nbox\n```\n\n```text\nunsafe\n```\n\n```text\nobject\n```\n\n========================================\n\nComments:\n- I'm sorry, but I don't quite understand your question.\n- Except for `System.Enum`, which derives from `System.ValueType` but is not a value type itself. But all `enum` types *are* value types which derive from `System.Enum` making their base type not `System.ValueType`. So value types are types derived from `System.ValueType` except for `System.Enum`, although the internal implementation of this test can be performed more efficiently than this.\n- @280Z28: I'm not sure what that means, as System.Enum is not a type you can directly use. In fact, it's not even real enough to use as a constraint on a generic type (see bottom of msdn.microsoft.com/en-us/library/d5x73970.aspx).\n- @280Z28 Please calrify 'Except for System.Enum, which derives from System.ValueType but is not a value type itself.'\n- @280Z28. Enums are an exception, in that they must derive from System.Enum, which is just a specialization of an integral type (short, int, long, etc.) that does not add any new fields to the integral type but simply restricts the value space which is allowed. Any enumeration type must specify a second type which will be used as the core integral type, to hold the data representation. This second core integral type, of course, derives from System.ValueType. And YES, enums ARE value types.\n- Enums are not an exception as System.Enum derives from System.ValueType so the same type checking applies event to them.\n- @Matthew, Perhaps we are hitting the level of irelevance of detail here, but although they do indirectly derive from System.ValueType, because (and frankly I am not sure of the distinction here...) either System.Enum derives from System.ValueType, or the CTS Integral type that they extend (int, short etc.) derives from System.ValueType. But the enum itself does not derive directly from System.ValueType, it derives from System.Enum...\n- @Steven, the limitation on using `System.Enum` is an artificial C# limitation. You can happily define such a constraint in C++/CLI, for example, or in MSIL directly.\n- @PK: `System.Enum` is not a value type in a sense that you can write `System.Enum x = null`.\n- From CLR perspective, enums do not derive from their underlying integral value types. If you look at a disassembly of an enum, you'll see that it rather wraps a field of that integral type. Where enums are special is that CLR guarantees that they are \"assignment compatible\" (i.e. bitwise representation is exactly the same) as their underlying integral type. However, enum is a value type strictly because it derives (indirectly) from `System.ValueType` - its underlying type has nothing to do with this.\n- Yes, they're completely unrelated. For one thing, `IntPtr` is a struct, and you can't derive from a struct.","metadata":{"transformedAt":"2026-08-18T18:32:17.834Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":69,"estimatedTokens":919}}822{"id":"stack-10912786","source":"stackoverflow","questionId":10912786,"title":"Compiling Java Generics with Wildcards to C++ Templates","tags":["java","c++","templates","generics","compiler-construction"],"text":"Title: Compiling Java Generics with Wildcards to C++ Templates\nTags: java, c++, templates, generics, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am trying to build a Java to C++ trans-compiler (i.e. Java code goes in, semantically \"equivalent\" (more or less) C++ code comes out).\n\nNot considering garbage collection, the languages are quite familiar, so the overall process works quite well already. One issue, however, are generics which do not exist in C++. Of course, the easiest way would be to perform erasure as done by the java compiler. However, the resulting C++ code should be nice to handle, so it would be good if I would not lose generic type information, i.e., it would be good, if the C++ code would still work with `List` instead of `List`. Otherwise, the C++ code would need explicit casting everywhere where such generics are used. This is bug-prone and inconvenient.\n\nSo, I am trying to find a way to somehow get a better representation for generics. Of course, templates seem to be a good candidate. Although they are something completely different (metaprogramming vs. compile-time only type enhancement), they could still be useful. As long as no wildcards are used, just compiling a generic class to a template works reasonably well. However, as soon as wildcards come into play, things get really messy.\n\nFor example, consider the following java constructor of a list:\n\n```\nclass List{\nList(Collection c){\n this.addAll(c);\n}\n}\n\n//Usage\nCollection c = ...; \nList l = new List(c);\n```\n\nhow to compile this? I had the idea of using chainsaw reinterpret cast between templates. Then, the upper example could be compiled like that:\n\n```\ntemplate\nclass List{\nList(Collection c){\n this.addAll(c);\n}\n}\n\n//Usage\nCollection c = ...; \nList l = new List(reinterpret_cast>(c));\n```\n\nhowever, the question is whether this reinterpret cast produces the expected behaviour. Of course, it is dirty. But will it work? Usually, `List` and `List` should have the same memory layout, as their template parameter is only a pointer. But is this guaranteed?\n\nAnother solution I thought of would be replacing methods using wildcards by template methods which instanciate each wildcard parameter, i.e., compile the constructor to\n\n```\ntemplate\nclass List{\n\ntemplate\nList(Collection c){\n this.addAll(c);\n}\n}\n```\n\nof course, all other methods involving wildcards, like `addAll` would then also need template parameters. Another problem with this approach would be handling wildcards in class fields for example. I cannot use templates here.\n\nA third approach would be a hybrid one: A generic class is compiled to a template class (call it `T`) and an erased class (call it `E`). The template class `T` inherits from the erased class `E` so it is always possible to drop genericity by upcasting to E. Then, all methods containing wildcards would be compiled using the erased type while others could retain the full template type.\n\nWhat do you think about these methods? Where do you see the dis-/advantages of them?\nDo you have any other thoughts of how wildcards could be implemented as clean as possible while keeping as much generic information in the code as possible?\n\n========================================\n\nTop Answer:\nNot considering garbage collection, the languages are quite familiar, so the overall process works quite well already.\n\nNo. While the two languages actually *look* rather similar, they are *significantly* different as to \"how things are done\". Such 1:1 trans-compilations as you are attempting will result in terrible, underperforming, and most likely faulty C++ code, *especially* if you are looking not at a stand-alone application, but at something that might interface with \"normal\", manually-written C++.\n\nC++ requires a completely different programming *style* from Java. This begins with *not* having all types derive from `Object`, touches on avoiding `new` unless absolutely necessary (and then restricting it to constructors as much as possible, with the corresponding `delete` in the destructor - or better yet, Potatoswatter's advice below), and doesn't end at \"patterns\" like making your containers STL-compliant and passing `begin`- and `end`-iterators to another container's constructor instead of the whole container. I also didn't see const-correctness or pass-by-reference semantics in your code.\n\nNote how many of the early Java \"benchmarks\" claimed that Java was faster than C++, because Java evangelists took Java code and translated it to C++ 1:1, just like you are planning to do. There is nothing to be won by such transcompilation.\n\n========================================\n\nCode:\n```text\nclass List<T>{\nList(Collection<? extends T> c){\n    this.addAll(c);\n}\n}\n\n//Usage\nCollection<String> c = ...; \nList<Object> l = new List<Object>(c);\n```\n\n```text\ntemplate<class T>\nclass List{\nList(Collection<T*> c){\n    this.addAll(c);\n}\n}\n\n//Usage\nCollection<String*> c = ...; \nList<Object*> l = new List<Object*>(reinterpret_cast<Collection<Object*>>(c));\n```\n\n```text\ntemplate<class T>\nclass List{\n\ntemplate<class S>\nList(Collection<S*> c){\n    this.addAll(c);\n}\n}\n```\n\n```text\nList<X>\n```\n\n```text\nList\n```\n\n```text\nList<Object*>\n```\n\n```text\nList<String*>\n```\n\n```text\naddAll\n```\n\n```text\nT<X>\n```\n\n```text\nE\n```\n\n```text\nT<X>\n```\n\n```text\nE\n```\n\n```text\nreinterpret_cast\n```\n\n```text\nList\n```\n\n```text\nList<String>\n```\n\n```text\nObject\n```\n\n```text\nnew\n```\n\n```text\ndelete\n```\n\n```text\nbegin\n```\n\n```text\nend\n```\n\n```text\nCollection<? extends T>\n```\n\n```text\nCollection<T>\n```\n\n```text\nCollection<?>\n```\n\n```text\nreinterpret_cast\n```\n\n```text\nString\n```\n\n```text\nString*\n```\n\n```text\nObject*\n```\n\n```text\nString*\n```\n\n```text\nObject*\n```\n\n```text\nT\n```\n\n```text\nList<? extends T>\n```\n\n```text\nT\n```\n\n```text\n?\n```\n\n========================================\n\nComments:\n- *\" Although they are something completely different (metaprogramming vs. compile-time only type enhancement),\"* C++ templates provide both.\n- Whether it works or not depends on the implementation of `Collection`. If it has any virtual functions, which, being translated from Java, I suspect it has, this has undefined behaviour (and this should not be taken to imply that if there are no virtual functions it is fine). As I mentioned in another question `reinterpret_cast` is not terribly useful.\n- why? Of course, it has virtual methods. However, calling a method of `Collection` onto an object which is actually a `Collection` should be fine if they the same memory layout.\n- First, because the result of the `reinterpret_cast` is unspecified. Second, because it causes violations of the aliasing rules, resulting in undefined behaviour.\n- I was sure that such an answer would be posted... I am more familiar with C++ as the code might look like. Of course, I start with producing running code rather than clean, elegant, high performance code. It is much harder to make a program write good C++ than writing it myself. I know about all the problems that you have posted, I have dealt with them and they are out of the scope of this question. Of course, the generated code will not look like natively written C++ code and will not use STL compliant containers. This is not the point here.\n- @gexicide: It's at the *core* of the problem. If you had \"dealt\" with \"all the problems\", you wouldn't *have* this particular one. I'm not talking about \"lcean, elegant, high performance\" code. I'm talking about *meaningful* code, something that serves a purpose beyond \"it can be done, *technically*\". If you disagree, just ignore this answer.\n- +1, but `new` is harder to make exception-safe than simply putting a `delete` in the destructor. It's best to use smart pointer and container classes for everything.\n- The question was not about whether it is a good idea to write a Java to C++ compiler but about this specific issue. And where do you see an condescending undertone about C++'s capabilities in my text?\n- @gexicide What is your goal? Unless this is purely for practice in translating between language semantics, it's a waste of time. You need to use a real Java JIT with a real native interface.\n- @Potatoswatter: It's been years since I last wrote a \"new\", so you're probably right. ;-)\n- @gexicide: \"Generics do not exist in C++\"? Templates being \"something completely different (metaprogramming vs. compile-time only type enhancement)\", but \"they could still be useful\"?\n- different is not worse. And (Java) generics do not exist in C++, that is a fact. I regard templates, i.e., metaprogramming, as feature which is a lot mightier than (Java) generics, and it is indeed. \"Still be useful\" means \"still useful for my application although they are something different\". I see the power of C++'s features, they are just not applicable to the problem at hand.\n- You're probably right. My answer tries to salvage that the underlying C++ collection knows the type, but I suspect that after a certain amount of development I'd find some edge case of \"ill-formed access that Java can express\", give up, and switch to what you say -- enforce the type only in the wrappers, and the \"real\" collection is always of `Object*`.\n- @SteveJessop: Regarding ill-formed access... you can, for example, create a Java function that takes a generic List, pass it a List, and within the function, add a String to the List... ?:-)\n- @DevSolar: yes, there's going to be a `dynamic_cast` in there somewhere, for normal usage. But if Java gives you the means to bypass the checks completely, and successfully add a `String` to a collection object that was created as `ArrayList`, then of course the underlying C++ container must be of `Object` just like the Java one is. There's no way around it.\n- Yes, this answer seems reasonable (together with the previous from Steve). I do use reference counting already, so it would nicely fit in there. Also the idea of creating covariant and contravariant views of generic classes and only passing these to methods with co or contravariant wildcards sounds like a good idea.\n- Good luck… I'm no expert in Java but from my initial read-up on generics, you are allowed to do things like get a completely dynamic type at runtime and bind it to an unparameterized (terminology?) container. Such hackery won't work with any use of C++ templates at all… but you might not be interested in supporting *all* Java code. Or I don't know, maybe the semantics of actually using such a container are restrictive enough that you can test `typeid` at some critical point, and either throw an exception or proceed with a static type and no loss of functionality. Maybe. Else things get hairy.\n- yes, I maintain a typeid at runtime, so I can perform a runtime check of type parameters when someone tries to downcast for example, from Object to List. And yes, I do not support all the nonsense of java generics, especially not the ones that make the complete type system unsound.","metadata":{"transformedAt":"2026-08-18T18:32:17.834Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":33,"totalLines":253,"estimatedTokens":2728}}823{"id":"stack-6196737","source":"stackoverflow","questionId":6196737,"title":"Generating IL for .Net Platform","tags":["c#","compiler-construction","il"],"text":"Title: Generating IL for .Net Platform\nTags: c#, compiler-construction, il\nSource: Stack Overflow\n\nQuestion:\nI’m writing a small compiler in C# and planning to generate IL instructions for .Net platform using `System.Reflection.Emit`. My question is, it is advisable to use `System.Reflection.Emit` for generating IL for production compilers. \n\nIf it is not advisable to use `System.Reflection.Emit` for generating IL for production compilers, do we have alternative libraries/tools for that purpose?\n\n========================================\n\nTop Answer:\nHere is a blog article describing some problems with `Reflection.Emit` that probably aren't serious limitations for your project but you can be the judge:\n\n- The limitations of Reflection.Emit\n\nIf those issues don't bother you, then you can use this SO question for tips on the generation using `Reflection.Emit` and writing the assembly to disk:\n\n- Writing a Compiler for .net - IL or Bytecode?\n\n========================================\n\nCode:\n```text\nSystem.Reflection.Emit\n```\n\n```text\nSystem.Reflection.Emit\n```\n\n```text\nSystem.Reflection.Emit\n```\n\n```text\nSystem.Reflection.Emit\n```\n\n```text\nReflection.Emit\n```\n\n```text\nReflection.Emit\n```\n\n```text\nSystem.Reflection.Emit\n```\n\n```text\nReflection.Emit\n```\n\n```text\nIKVM.Reflection.Emit\n```\n\n========================================\n\nComments:\n- +1. Microsoft.CCI is also an option. It seems like performance is a strong consideration in that library (explicit name tables being one example), so it might allow you to make a faster compiler. But in my limited use, I much preferred Cecil's API.\n- mono is awesome in every way. They build lots of cool stuff everybody .NET developer should be aware of. I did not know about `IKVM`.","metadata":{"transformedAt":"2026-08-18T18:32:17.834Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":64,"estimatedTokens":435}}824{"id":"stack-2061265","source":"stackoverflow","questionId":2061265,"title":"Are there compilers that optimise floating point operations for accuracy (as opposed to speed)?","tags":["optimization","compiler-construction","floating-point","floating-accuracy"],"text":"Title: Are there compilers that optimise floating point operations for accuracy (as opposed to speed)?\nTags: optimization, compiler-construction, floating-point, floating-accuracy\nSource: Stack Overflow\n\nQuestion:\nWe know that compilers are getting better and better at optimising our code and make it run faster, but my question are there compilers that can optimise floating point operations to ensure greater accuracy.\n\nFor example a basic rule is to perform multiplications before addition, this is because multiplication and division using floating point numbers does not introduce inaccuracies as great as that of addition and subtraction but can increase the magnitude of inaccuracies introduced by addition and subtraction, so it should be done first in many cases.\n\nSo a floating point operation like \n\n```\ny = x*(a + b); // faster but less accurate\n```\n\nShould be changed to \n\n```\ny = x*a + x*b; // slower but more accurate\n```\n\nAre there any compilers that will optimise for improved floating point accuracy at the expense of speed like I showed above? Or is the main concern of compilers speed with out looking at accuracy of floating point operations?\n\nThanks\n\nUpdate: The selected answer, showed a very good example where this type of optimisation would not work, so it wouldn't be possible for the compiler to know before hand what is the more accurate way to evaluate y. Thanks for the counter example.\n\n========================================\n\nTop Answer:\nCompilers typically \"optimize\" for accuracy over speed, accuracy defined as exact implementation of the IEEE 754 standard. Whereas integer operations can be reordered in any way that doesn't cause overflow, FP operations need to be performed exactly as the programmer specifies. This may sacrifice numerical accuracy (ordinary C compilers are not equipped to optimize for that) but faithfully implements the what the programmer asked.\n\nA programmer who is sure he hasn't manually optimized for accuracy may enable compiler features like GCC's `-funsafe-math-optimizations` and `-ffinite-math-only` to possibly extract extra speed. But usually there isn't much gain.\n\n========================================\n\nCode:\n```text\ny = x*(a + b); // faster but less accurate\n```\n\n```text\ny = x*a + x*b; // slower but more accurate\n```\n\n```text\nx = 3.1415926535897931\na = 1.0e15\nb = -(1.0e15 - 1.0)\n```\n\n```text\nx*(a + b) = 3.1415926535897931\n```\n\n```text\nx*a + x*b = 3.0\n```\n\n```text\nx*(a + b)\n```\n\n```text\nx*a + x*b\n```\n\n```text\nx\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\nx*(a+b)\n```\n\n```text\ndouble\n```\n\n```text\n-funsafe-math-optimizations\n```\n\n```text\n-ffinite-math-only\n```\n\n========================================\n\nComments:\n- How would the compiler know whether you wanted accuracy or speed, other than you choosing between the forms `y = x*(a + b)` and `y = x*a + x*b`?\n- There are no universal rules about which order of operations will be \"most accurate\". It depends on the expected values for x, a and b. It may also depend on your use case. Sometimes, you may *want* floating point arithmetics, rather than the closest possible approximation to real number arithmetics. And then the compilers reordering would give you answers that were further away from the desired values -- it'd *introduce* inaccuracies.\n- @Robert Harvey, simple the compiler would provide different optimisation flags :)\n- @jalf, Ofcourse there are no universal rules, that even applies to normal optimisation, thats why compilers come with so many options so you can fine tune which optimisations you wish to turn on or off, I can envisage a compiler that would have these types of FP heuritics as optimisation flags. And the one who really needs not to be close to real numbers can then just not turn on the optimisation\n- My premise is not faulty, the first example has two additions and one multiplication while the second has two additions and two multiplications. Multiplications in FP operations introduce no accuracies. That is a fact :) However addition of FP with greatly different magnitudes introduces inaccuracies, so perform the multiplication before after these inaccuracies are introduced increases there magnitude as opposed to doing it before hand.\n- Also your point about \"better to do what programmer writes, not what you think he might have intended\" is true sometimes but not always, that is the whole premise of optimisation. The compiler does something equivalent but faster, however equivalent is not truely 100% equivalent as we know otherwise it would not be faster, so to say do what the programmer says always, means turn off all optimisation. Turning of optimisation is valid in many cases but as a general rule, that can be disputed.\n- When talking about optimization, we usually define \"equivalent\" to mean \"produces the exact same result\". This transformation does not - hence it's not \"equivalent\".\n- The claim that floating point multiplication never incurs rounding is just blatantly false.\n- +1. The premise is faulty unless you know the expected magnitudes of the variables. More operations means more truncation or rounding (even with multiplication). I'm not sure why hhafez claims there are two additions in each example.\n- point taken, but I can give a counter example, I guess what you have demonstrated is that there are no golden rules\n- Yes, there are cases where (for example), `x*a + x*b` delivers the correctly rounded result, while `x*(a + b)` differs by an ulp in the last place. That's why I used the phrase \"often be more accurate\", not \"always be more accurate\".\n- `x*a + x*b` can be done with 2 roundings using FMA, but maybe it's still slower and not more correct than `x*(a+b)`\n- @LưuVĩnhPh&#250;c: FMA does indeed get you two roundings, but it actually makes the transform *more* dangerous in some ways: if a = -b, then `x*(a+b)` and `x*a + x*b` are exactly zero, but `fma(x, a, x*b)` is often not.","metadata":{"transformedAt":"2026-08-18T18:32:17.834Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":14,"totalLines":111,"estimatedTokens":1477}}825{"id":"stack-9223938","source":"stackoverflow","questionId":9223938,"title":"Java: Is method name/signature resolution done statically (compile-time)?","tags":["java","compiler-construction","compilation"],"text":"Title: Java: Is method name/signature resolution done statically (compile-time)?\nTags: java, compiler-construction, compilation\nSource: Stack Overflow\n\nQuestion:\nI encountered an interesting problem today which I thought was not possible in Java. I compiled my java code against version 2.6 of jgroups but used version 2.12 at runtime (tomcat web app deployment). I got the following error\n\n```\norg.jgroups.Message.(Lorg/jgroups/Address;Lorg/jgroups/Address;Ljava/io/Serializable;)\n```\n\nAssuming that the API would have change since then, I thought of porting my code to jgroups-2.12, but to my surprise the code compiled fine with jgroups-2.12 and when I replaced the new jar (without changing a single line in my code, just compiling against jgroups-2.12 instead of jgroups-2.6), it worked perfectly fine.\n\nI later realized that the constructor `Message(Address, Address, Serializable)` in 2.6 was changed to Message(Address, Address, Object) in 2.12. This means at runtime, the JVM was trying to locate the exact same method and was failing to do so.\n\nDoes this mean that Java compiler embeds the exact method name and precise arguments while compiling and a method with broader arguments won't work?\n\n========================================\n\nTop Answer:\nDoes this mean that Java compiler embeds the exact method name and precise arguments while compiling and a method with broader arguments won't work?\n\nExactly. You can also see this from the error message you got:\n\n```\norg.jgroups.Message.(Lorg/jgroups/Address;Lorg/jgroups/Address;Ljava/io/Serializable;)\n```\n\nThe complete signature is contained here, and the runtime looks for a perfect match.\n\nThere are also several other occasions when changing an API breaks binary compatibility in Java, but not source compatibility, for example when you change a primitive type to its boxed variant or vice versa. As pointed out by Jon, only changes in Generics (but not even all changes) and using the VarArgs syntax do not affect runtime method resolution, as both are only compiler features and do not affect the bytecode.\n\nThis also means that when you introduce an overload of a method in a new library version, this overload will only be used by callers compiled with the new version. Old binaries will still call the old method, even if their argument types would better fit the new overload.\n\nFor library designers it is thus sometimes advisable to do not change the signatures of existing methods, but only add new overloads (and let the old methods forward to the new ones so that it doesn't matter which one is called). Of course the disadvantage is that all these overloads obscure the real API and and make understanding the API more difficult.\n\n========================================\n\nCode:\n```text\norg.jgroups.Message.<init>(Lorg/jgroups/Address;Lorg/jgroups/Address;Ljava/io/Serializable;)\n```\n\n```text\nMessage(Address, Address, Serializable)\n```\n\n```text\nString[]\n```\n\n```text\nString...\n```\n\n```text\norg.jgroups.Message.<init>(Lorg/jgroups/Address;Lorg/jgroups/Address;Ljava/io/Serializable;)\n```\n\n========================================\n\nComments:\n- Even adding generics can mess things up, if the generic you add has a different bound than the old code. That's why generics have the somewhat obscure `` option -- so you can \"manually\" specify the erasure, and thus bytecode compatibility, of the generic.\n- @yshavit: Yes, you'd have to be careful - will edit to make that clearer.","metadata":{"transformedAt":"2026-08-18T18:32:17.834Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":64,"estimatedTokens":863}}826{"id":"stack-6682438","source":"stackoverflow","questionId":6682438,"title":"Is it possible to manually calculate the byte-offset of a class member?","tags":["c++","compiler-construction","class-members"],"text":"Title: Is it possible to manually calculate the byte-offset of a class member?\nTags: c++, compiler-construction, class-members\nSource: Stack Overflow\n\nQuestion:\nThat is, what is the standard a compiler uses to generate a class? For example, let's say that I have class `C` with members `x`, `y`, and `z`, and I want to know the offset of `z` within that class. Can I just add up the data-type sizes of the other members, like I would for a structure?\n\n========================================\n\nTop Answer:\nYou can do it programatically like this in a method of the class. Not generic but works.\n\n```\noffset = (unsigned char*)&(this->z) - (unsigned char*)this;\n```\n\nFull working example\n\n```\n#include \n\nclass C\n{\npublic:\n int x;\n char y;\n int z;\n size_t offset() const\n {\n return (unsigned char*)&(this->z) - (unsigned char*)this;\n }\n};\n\nint main()\n{\n C c;\n std::cerr << \"Offset(cast): \" << c.offset() << \"\\n\";\n}\n```\n\n========================================\n\nCode:\n```text\nC\n```\n\n```text\nx\n```\n\n```text\ny\n```\n\n```text\nz\n```\n\n```text\nz\n```\n\n```text\n#define _OFFSET(p_type, p_member) (size_t)(&((p_type *)NULL)->p_member)\n\n    struct a\n   {\n       int a, b;\n   };\n\n   cout << _OFFSET(struct a, b); // output is your offset\n```\n\n```text\noffset = (unsigned char*)&(this->z) - (unsigned char*)this;\n```\n\n```text\n#include <iostream>\n\nclass C\n{\npublic:\n    int x;\n    char y;\n    int z;\n    size_t offset() const\n    {\n        return (unsigned char*)&(this->z) - (unsigned char*)this;\n    }\n};\n\nint main()\n{\n    C c;\n    std::cerr << \"Offset(cast): \" << c.offset() << \"\\n\";\n}\n```\n\n```text\n#pragma pack(push,1)\nstruct Foo {\n  uint8 a;\n  uint32 b;\n};\n#pragma pack(pop)\n```\n\n```text\n&(class_name::member_name)\n```\n\n========================================\n\nComments:\n- You can't do that even for a struct. Ever heard of padding?\n- You might want to explain why you think you need to do this.\n- @delnan: If by struct you mean POD, then sure, *you* can't do this, but offsetof can, and I doubt the OP is opposed to pre-existing easy solutions.\n- @Jefromi: I wasn't referring to `offsetof` in general, but to the technique OP was suggesting (\"Can I just add up the data-type sizes of the other members, like I would for a structure?\").\n- Is this mentioned somewhere in the C++ standard?\n- +1 and within a single compiler alignment rules may differ depending on compilation options and pragmas...\n- I'm sorry, but could you tell us what `POD` stands for?\n- @Fezvez: I am sorry, I should have added that link earlier, POD stands for Plain Old Data Type, Please read the link I added in the answer.\n- C++0x specifies alignment requirements. Even the previous standard specified a multitude of alignment requirements. so it isn't true to say the compiler is free to align as they choose. (C++0x Section 3.11 Alignment is a good start)\n- @edA-qa mort-ora-y: The Q is not tagged `C++0x` & Can you elaborate on how standard `C++03` enforces alignment requirements?\n- The standard (pre C++0x) has many rules about which types must alignment requirements, how compound alignment may be performed, and fundamental alignment of core types. If you read all the rules together there appears to be the implicit detail that the compiler should not be added arbitrary padding, but should only be padding to fulfill the alignment requirements of the target platform. C++0x then codifies some of these implicit rules and the common practice, in particular to provide layout guarantees which were always assumed/implied. Thus the compiler can't really *do as it chooses*.\n- @edA-qa mort-ora-y: So do you mean one can find the byte alignment in NON POD types in a portable way?\n- Prior to C++0x, it's tricky, since there are no explicit layout guarantees for non-POD types, even those without virtuals. But, common convention (most, if not all compilers) is that classes without virtual functions have a clean layout like POD types, thus `offsetof` should work on these types. C++0x defines something called \"Standard Layout\" which enforces this type of layout -- and indeed the wording on `offsetof` is changed include \"Standard Layout\" types.\n- @edA-qa mort-ora-y: Correct me if I am wrong, but If there is no **guaranteed** **portable** way of doing this, then what is the disagreement?\n- Well, offsetof is portable for Standard Layout types (by convention now, and guaranteed in C++0x). But my disagreement is about the compiler being free to choose alignment of members. It has some say in the padding it applies, but less choice than your first sentence would imply.\n- As that site says: \"Because of the extended functionality of structs in C++, in this language, the use of offsetof is restricted to POD types\".\n- 1) Why did you have to reinvent the wheel? There is a standard way to do what you did here. 2) Why did you have to use a reserved name?\n- However voted this down, why wouldn't this work, if the offset was calculated at run-time? Once the compiler has generated the class template for instantiation, I can't imagine why the offset would change.\n- This won't work because it's not valid C++: `_OFFSET` is a reserved identifier. Nevermind the undefined behavior.\n- I am not dereferencing it. Taking the address will not access the memory the address points to!\n- What do you think `->` does? `a->b` is semantically equivalent to `(*a).b`.\n- I am not reinventing the wheel. This wheel did not exist, until ANSI C++ came and put it here. It takes 0 and adds the offset to it.\n- @bert-jan let us continue this discussion in chat\n- error: invalid use of non-static data member 'MyClass::my_member'","metadata":{"transformedAt":"2026-08-18T18:32:17.834Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":140,"estimatedTokens":1395}}827{"id":"stack-23552985","source":"stackoverflow","questionId":23552985,"title":"What is absolute code and why is it known at compile time?","tags":["compiler-construction","operating-system","compile-time"],"text":"Title: What is absolute code and why is it known at compile time?\nTags: compiler-construction, operating-system, compile-time\nSource: Stack Overflow\n\nQuestion:\nWhile studying for my Operating System Concepts final, I stumbled across \n\n```\nCompile time: If memory location knows, **absolute code** can be generated;\nmust recompile code if starting location changes\n```\n\nAnd the example gives for absolute code is the interrupt vector table. I was wondering what exactly that statement means and why is the interrupt vector table known at compile time?\n\n========================================\n\nTop Answer:\nThe answer given is correct, but there is another case. In embedded systems, and when compiling for a ROM, it is usual that the final address for the code is a decision taken relatively early. For a small chip the designer decides where many parts of the code will be placed within the memory space of the chip, and there are often memory-mapped I/O locations as well. For a ROM the target address of the ROM will be known.\n\nIn this case there are still two possibilities. One is that the compiler can generate code that directly addresses these locations (and features like placement new in C++ help). The other is that the compiler generates a mix of absolute addressing and relocatable code. In each case a special linker with a 'locater' capability outputs the final code with absolute addressing. In each case the final addresses are absolute and need no runtime fixups.\n\n========================================\n\nCode:\n```text\nCompile time: If memory location knows, **absolute code** can be generated;\nmust recompile code if starting location changes\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.834Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":28,"estimatedTokens":417}}828{"id":"stack-1087983","source":"stackoverflow","questionId":1087983,"title":"Definition of fix-up?","tags":["compiler-construction","linker","assembly","fixup"],"text":"Title: Definition of fix-up?\nTags: compiler-construction, linker, assembly, fixup\nSource: Stack Overflow\n\nQuestion:\nI've seen this term in the Python Lisp compiler and some C linker's sources.\n\nMy guess is that a fix-up is just some wrapper around an Assembly routine that makes sure the alignment is right, but I'm not sure at all about anything here.\n\n========================================\n\nTop Answer:\nLinker and Loaders is an interesting inker resource that explains a lot of jargon, and includes non x86 cpus here and there too:\n\nhttp://www.iecc.com/linker/\n\nfrom the comp.compilers moderator.","metadata":{"transformedAt":"2026-08-18T18:32:17.834Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":17,"estimatedTokens":151}}829{"id":"stack-4265281","source":"stackoverflow","questionId":4265281,"title":"How can I create a language like PHP and how does it work?","tags":["php","compiler-construction","language-design"],"text":"Title: How can I create a language like PHP and how does it work?\nTags: php, compiler-construction, language-design\nSource: Stack Overflow\n\nQuestion:\nI want to know how to create a language like PHP. How does the server know how to translate a PHP file? How does it work? I want to do this for educational purposes. A simple language with basic features like echo, etc.\n\n========================================\n\nTop Answer:\nDivide your task in at-least two top level parts:\n\nPHP as just another programming language - you need a lexical analyzer, parser and interpreter.\n\nLibraries and modules for web servers likt Apache/TomCat to inteface with PHP.\n\n========================================\n\nCode:\n```text\n<?php\n\n$srctokens=token_get_all(file_get_contents(__FILE__));\nprint \"<pre>\\n\";\nforeach ($srctokens as $tok) {\n   print token_name($tok[0]) . ' -&gt; ' . $tok[1]; \n}\n?>\n```\n\n========================================\n\nComments:\n- Not a full answer, but you can download PHP's source code from php.net. It's written in C\n- stackoverflow.com/questions/366998/&hellip; gives a few hints on how to go about designing a programming language\n- How does the webserver automatically know to process this request.\n- Throu config file. For example in apache config AddHandler php5-script .php AddType text/html .php\n- This is wrong. The version of PHP at php.net compiles to bytecode for execution by the Zend VM. PHP is usually either interpreted or compiled to intermediate code. Compilation to machine code is possible but not the rule.\n- 3) a cgi-bin(or alternatives) module or an apache module","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":39,"estimatedTokens":399}}830{"id":"stack-36658726","source":"stackoverflow","questionId":36658726,"title":"Link C in llvmlite","tags":["python","c","compiler-construction","llvm","llvmlite"],"text":"Title: Link C in llvmlite\nTags: python, c, compiler-construction, llvm, llvmlite\nSource: Stack Overflow\n\nQuestion:\nIm writing an compiler in Python, using `llvmlite` to generate intermediate LLVM IR. Lexer and parser are finished, now im doing code generation. The compiler will be dynamic and weakly typed, so i will need to do somethings at runtime, like allocation. For this, i've already implemented some functions in C, and now i want to call these functions using `builder.call` from llvmlite. \n\nI have not found documentation or examples of how to do this.\n\nThis function its just an simple example, the real ones are much larger.\n\n**C:**\n\n```\nint some_function(int a)\n{\n return a + 4;\n}\n```\n\n**Python:**\n\n```\n...\n\n main_ty = ir.FunctionType(ir.IntType(32), [])\n func = ir.Function(module, main_ty, 'main')\n block = func.append_basic_block('entry')\n builder = ir.IRBuilder(block)\n\n # I want to do something like this...\n\n ret = builder.call(some_function, [ir.Constant(ir.IntType(32), 34)]);\n\n...\n```\n\nI could write the functions directly using llvmlite builders, but will be much quick, cleaner and easy do it in C. Any help are welcome!\n\n========================================\n\nTop Answer:\nOn the LLVM IR side you can just declare the functions with the right signature (and no body), and insert calls to them like any other function. This is just like how in C you might call a function which is defined in another file.\n\nFrom there, you would have to somehow link against your C functions. The details here depend on how you intend to use your generated IR code. For example, you could use clang to turn it into object files, and then link it like any other program. Or you could use the llvm JIT, in which case @Coder3000's answer (`llvmlite.binding.load_library_permanently`) should work to let LLVM resolve your function calls.\n\n========================================\n\nCode:\n```text\nint some_function(int a)\n{\n     return a + 4;\n}\n```\n\n```text\n...\n\n    main_ty = ir.FunctionType(ir.IntType(32), [])\n    func = ir.Function(module, main_ty, 'main')\n    block = func.append_basic_block('entry')\n    builder = ir.IRBuilder(block)\n\n    # I want to do something like this...\n\n    ret = builder.call(some_function, [ir.Constant(ir.IntType(32), 34)]);\n\n...\n```\n\n```text\nllvmlite\n```\n\n```text\nbuilder.call\n```\n\n```text\nllvmlite.binding.load_library_permanently(\"runtime.so\")\n```\n\n```text\nllvmlite.binding.load_library_permanently\n```\n\n```text\n#!/usr/bin/env python\n    # coding=utf-8\n    from __future__ import print_function\n    from ctypes import CFUNCTYPE, c_double,cdll,c_int\n    import llvmlite.binding as llvm\n    import llvmlite.ir as  ir\n\n    llvm.initialize()\n    llvm.initialize_native_target()\n    llvm.initialize_native_asmprinter()\n    #load shared library\n    llvm.load_library_permanently('./TestLib.so')\n    # Create some useful types\n    double = ir.DoubleType()\n    fnty = ir.FunctionType(double, (double, double))\n\n    # Create an empty module...\n    module = ir.Module(\"fdadd\")\n    # and declare a function named \"fpadd\" inside it\n    func = ir.Function(module, fnty, name=\"fpadd\")\n\n    # Now implement the function\n    block = func.append_basic_block(name=\"entry\")\n    builder = ir.IRBuilder(block)\n    a, b = func.args\n    result = builder.fadd(a, b, name=\"res\")\n    builder.ret(result)\n    #function call in llvm ir internal\n    func2=ir.Function(module,fnty,name=\"fdadd\")\n    block2=func2.append_basic_block(name=\"entry\")\n    builder=ir.IRBuilder(block2)\n    a,b=func2.args\n    result2=builder.call(func,(a,b))\n    builder.ret(result2)\n    # llvm IR call external C++ function\n    outfunc=ir.Function(module,fnty,name=\"SampleAddDouble\")\n    #just declare shared library function in module\n    outaddfunc=ir.Function(module,fnty,name=\"outadd\")\n    builder=ir.IRBuilder(outaddfunc.append_basic_block(name=\"entry\"))\n    a,b=outaddfunc.args\n    outresult=builder.call(outfunc,(a,b))\n    builder.ret(outresult)\n    strmod=str(module)\n    # Print the module IR\n    print(strmod)\n    print(\"-----------------------------------------\")\n    #assembly llvm ir\n    assmod = llvm.parse_assembly(strmod)\n    assmod.verify()\n    print(\"--parse assembly\")\n    target = llvm.Target.from_default_triple()\n    target_machine = target.create_target_machine()\n    engine = llvm.create_mcjit_compiler(assmod, target_machine)\n    engine.finalize_object()\n    print(engine)\n\n    # Look up the function pointer (a Python int) \n    #llvm execution engine call llvm IR function(or dsl function)\n    func_ptr = engine.get_function_address(\"fpadd\")\n    print('func_ptr is:',func_ptr)\n\n    # Run the function via ctypes\n    cfunc = CFUNCTYPE(c_double, c_double, c_double)(func_ptr)\n    res = cfunc(1.0, 3.5)\n    print(\"fpadd(...) =\", res)\n\n    #llvm binding layer call shared library function\n    add_int_addr = llvm.address_of_symbol(\"SampleAddInt\")\n    print(add_int_addr)\n    add_int_func=CFUNCTYPE(c_int,c_int,c_int)(add_int_addr)\n    res2=add_int_func(23,34)\n    print(res2)\n\n    #llvm execution engine call shared library function ,by llvm IR \n    add_double_addr=engine.get_function_address(\"outadd\")\n    print(add_double_addr)\n    add_double_func=CFUNCTYPE(c_double,c_double,c_double)(add_double_addr)\n    res3=add_double_func(1.21,1.12)\n    print(res3)\n```\n\n```text\nllvmlite.binding.load_library_permanently(\"runtime.so\")\n```\n\n```text\nclang -c -emit-llvm src/main.c -o main.bc\n```\n\n```text\nclang [*.bc] -o executable\n```\n\n========================================\n\nComments:\n- What tools did you use to develop Lexer & parser ?\n- Do you have an example of how to perform the call? Are the functions within the module once loaded? And if so, how do I then find the function to call it?","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":186,"estimatedTokens":1424}}831{"id":"stack-29381117","source":"stackoverflow","questionId":29381117,"title":"Which exponentiation algorithms do CPU/programming languages use?","tags":["c","algorithm","math","compiler-construction","cryptography"],"text":"Title: Which exponentiation algorithms do CPU/programming languages use?\nTags: c, algorithm, math, compiler-construction, cryptography\nSource: Stack Overflow\n\nQuestion:\nI've been learning about faster exponentiation algorithms (k-ary, sliding door etc.), and was wondering which ones are used in CPUs/programming languages? (I'm fuzzy on whether or not this happens in the CPU or through the compiler)\n\nAnd just for kicks, which is the fastest?\n\nEdit regarding the broadness: It's intentionally broad because I know there are a bunch of different techniques to do this. The checked answer had what I was looking for.\n\n========================================\n\nCode:\n```c\n#include <math.h> /* import fmaf(), ldexpf(), INFINITY */\n\n/* Like rintf(), but -0.0f -> +0.0f, and |a| must be < 2**22 */\nfloat quick_and_dirty_rintf (float a)\n{\n    const float cvt_magic = 0x1.800000p+23f;\n    return (a + cvt_magic) - cvt_magic;\n}\n\n/* Approximate exp(a) on the interval [log(sqrt(0.5)), log(sqrt(2.0))]. */\nfloat expf_poly (float a)\n{ \n    float r;\n\n    r =             0x1.694000p-10f;  // 1.37805939e-3\n    r = fmaf (r, a, 0x1.125edcp-07f); // 8.37312452e-3\n    r = fmaf (r, a, 0x1.555b5ap-05f); // 4.16695364e-2\n    r = fmaf (r, a, 0x1.555450p-03f); // 1.66664720e-1\n    r = fmaf (r, a, 0x1.fffff6p-02f); // 4.99999851e-1\n    r = fmaf (r, a, 0x1.000000p+00f); // 1.00000000e+0\n    r = fmaf (r, a, 0x1.000000p+00f); // 1.00000000e+0\n    return r;\n}\n\n/* Approximate exp2() on interval [-0.5,+0.5] */\nfloat exp2f_poly (float a)\n{ \n    float r;\n\n    r =             0x1.418000p-13f;  // 1.53303146e-4\n    r = fmaf (r, a, 0x1.5efa94p-10f); // 1.33887795e-3\n    r = fmaf (r, a, 0x1.3b2c6cp-07f); // 9.61833261e-3\n    r = fmaf (r, a, 0x1.c6af8ep-05f); // 5.55036329e-2\n    r = fmaf (r, a, 0x1.ebfbe0p-03f); // 2.40226507e-1\n    r = fmaf (r, a, 0x1.62e430p-01f); // 6.93147182e-1\n    r = fmaf (r, a, 0x1.000000p+00f); // 1.00000000e+0\n    return r;\n}\n\n/* Approximate exp10(a) on [log(sqrt(0.5))/log(10), log(sqrt(2.0))/log(10)] */\nfloat exp10f_poly (float a)\n{ \n    float r;\n\n    r =             0x1.a56000p-3f;  // 0.20574951\n    r = fmaf (r, a, 0x1.155aa8p-1f); // 0.54170728\n    r = fmaf (r, a, 0x1.2bda96p+0f); // 1.17130411\n    r = fmaf (r, a, 0x1.046facp+1f); // 2.03465796\n    r = fmaf (r, a, 0x1.53524ap+1f); // 2.65094876\n    r = fmaf (r, a, 0x1.26bb1cp+1f); // 2.30258512\n    r = fmaf (r, a, 0x1.000000p+0f); // 1.00000000\n    return r;\n}\n\n/* Compute exponential base e. Maximum ulp error = 0.86565 */\nfloat my_expf (float a)\n{\n    float t, r;\n    int i;\n\n    t = a * 0x1.715476p+0f;            // 1/log(2); 1.442695\n    t = quick_and_dirty_rintf (t);\n    i = (int)t;\n    r = fmaf (t, -0x1.62e400p-01f, a); // log_2_hi; -6.93145752e-1\n    r = fmaf (t, -0x1.7f7d1cp-20f, r); // log_2_lo; -1.42860677e-6\n    t = expf_poly (r);\n    r = ldexpf (t, i);\n    if (a < -105.0f) r = 0.0f;\n    if (a >  105.0f) r = INFINITY;     // +INF\n    return r;\n}\n\n/* Compute exponential base 2. Maximum ulp error = 0.86770 */\nfloat my_exp2f (float a)\n{\n    float t, r;\n    int i;\n\n    t = quick_and_dirty_rintf (a);\n    i = (int)t;\n    r = a - t;\n    t = exp2f_poly (r);\n    r = ldexpf (t, i);\n    if (a < -152.0f) r = 0.0f;\n    if (a >  152.0f) r = INFINITY;     // +INF\n    return r;\n}\n\n/* Compute exponential base 10. Maximum ulp error = 0.95588 */\nfloat my_exp10f (float a)\n{\n    float r, t;\n    int i;\n\n    t = a * 0x1.a934f0p+1f;            // log2(10); 3.321928\n    t = quick_and_dirty_rintf (t);\n    i = (int)t;\n    r = fmaf (t, -0x1.344140p-2f, a);  // log10(2)_hi // -3.01030159e-1\n    r = fmaf (t, 0x1.5ec10cp-23f, r);  // log10(2)_lo //  1.63332601e-7\n    t = exp10f_poly (r);\n    r = ldexpf (t, i);\n    if (a < -46.0f) r = 0.0f;\n    if (a >  46.0f) r = INFINITY;      // +INF\n    return r;\n}\n\n#include <string.h>\n#include <stdint.h>\n\nuint32_t float_as_uint32 (float a)\n{\n    uint32_t r;\n    memcpy (&r, &a, sizeof r);\n    return r;\n}\n\nfloat uint32_as_float (uint32_t a)\n{\n    float r;\n    memcpy (&r, &a, sizeof r);\n    return r;\n}\n\nuint64_t double_as_uint64 (double a)\n{\n    uint64_t r;\n    memcpy (&r, &a, sizeof r);\n    return r;\n}\n\ndouble floatUlpErr (float res, double ref)\n{\n    uint64_t i, j, err, refi;\n    int expoRef;\n    \n    /* ulp error cannot be computed if either operand is NaN, infinity, zero */\n    if (isnan (res) || isnan (ref) || isinf (res) || isinf (ref) ||\n        (res == 0.0f) || (ref == 0.0f)) {\n        return 0.0;\n    }\n    /* Convert the float result to an \"extended float\". This is like a float\n       with 56 instead of 24 effective mantissa bits.\n    */\n    i = ((uint64_t)float_as_uint32(res)) << 32;\n    /* Convert the double reference to an \"extended float\". If the reference is\n       >= 2^129, we need to clamp to the maximum \"extended float\". If reference\n       is < 2^-126, we need to denormalize because of the float types's limited\n       exponent range.\n    */\n    refi = double_as_uint64(ref);\n    expoRef = (int)(((refi >> 52) & 0x7ff) - 1023);\n    if (expoRef >= 129) {\n        j = 0x7fffffffffffffffULL;\n    } else if (expoRef < -126) {\n        j = ((refi << 11) | 0x8000000000000000ULL) >> 8;\n        j = j >> (-(expoRef + 126));\n    } else {\n        j = ((refi << 11) & 0x7fffffffffffffffULL) >> 8;\n        j = j | ((uint64_t)(expoRef + 127) << 55);\n    }\n    j = j | (refi & 0x8000000000000000ULL);\n    err = (i < j) ? (j - i) : (i - j);\n    return err / 4294967296.0;\n}\n\n#include <stdio.h>\n#include <stdlib.h>\n\nint main (void)\n{\n    double ref, ulp, maxulp;\n    float arg, res, reff;\n    uint32_t argi, resi, refi, diff, sumdiff;\n\n    printf (\"testing expf ...\\n\");\n    argi = 0;\n    sumdiff = 0;\n    maxulp = 0;\n    do {\n        arg = uint32_as_float (argi);\n        res = my_expf (arg);\n        ref = exp ((double)arg);\n        ulp = floatUlpErr (res, ref);\n        if (ulp > maxulp) maxulp = ulp;\n        reff = (float)ref;\n        refi = float_as_uint32 (reff);\n        resi = float_as_uint32 (res);\n        diff = (resi < refi) ? (refi - resi) : (resi - refi);\n        if (diff > 1) {\n            printf (\"!! expf: arg=%08x res=%08x ref=%08x\\n\", argi, resi, refi);\n            return EXIT_FAILURE;\n        } else {\n            sumdiff += diff;\n        }\n        argi++;\n    } while (argi);\n    printf (\"expf   maxulp=%.5f  sumdiff=%u\\n\", maxulp, sumdiff);\n    \n    printf (\"testing exp2f ...\\n\");\n    argi = 0;\n    maxulp = 0;\n    sumdiff = 0;\n    do {\n        arg = uint32_as_float (argi);\n        res = my_exp2f (arg);\n        ref = exp2 ((double)arg);\n        ulp = floatUlpErr (res, ref);\n        if (ulp > maxulp) maxulp = ulp;\n        reff = (float)ref;\n        refi = float_as_uint32 (reff);\n        resi = float_as_uint32 (res);\n        diff = (resi < refi) ? (refi - resi) : (resi - refi);\n        if (diff > 1) {\n            printf (\"!! expf: arg=%08x res=%08x ref=%08x\\n\", argi, resi, refi);\n            return EXIT_FAILURE;\n        } else {\n            sumdiff += diff;\n        }\n        argi++;\n    } while (argi);\n    printf (\"exp2f  maxulp=%.5f  sumdiff=%u\\n\", maxulp, sumdiff);\n\n    printf (\"testing exp10f ...\\n\");\n    argi = 0;\n    maxulp = 0;\n    sumdiff = 0;\n    do {\n        arg = uint32_as_float (argi);\n        res = my_exp10f (arg);\n        ref = exp10 ((double)arg);\n        ulp = floatUlpErr (res, ref);\n        if (ulp > maxulp) maxulp = ulp;\n        reff = (float)ref;\n        refi = float_as_uint32 (reff);\n        resi = float_as_uint32 (res);\n        diff = (resi < refi) ? (refi - resi) : (resi - refi);\n        if (diff > 1) {\n            printf (\"!! expf: arg=%08x res=%08x ref=%08x\\n\", argi, resi, refi);\n            return EXIT_FAILURE;\n        } else {\n            sumdiff += diff;\n        }\n        argi++;\n    } while (argi);\n    printf (\"exp10f maxulp=%.5f  sumdiff=%u\\n\", maxulp, sumdiff);\n    \n\n    return EXIT_SUCCESS;\n}\n```\n\n```text\nexp()\n```\n\n```text\nexp2()\n```\n\n```text\nexp10()\n```\n\n```text\npow()\n```\n\n```text\nexpf()\n```\n\n```text\nexp2f()\n```\n\n```text\nexp10f()\n```\n\n```text\npowf()\n```\n\n```text\nmath.h\n```\n\n```text\ncmath\n```\n\n```text\nexp()\n```\n\n```text\nlog(0.0)\n```\n\n```text\nexpf(float)\n```\n\n```text\na\n```\n\n```text\nexp(a)\n```\n\n```text\ni\n```\n\n```text\nr\n```\n\n```text\nfmaf()\n```\n\n```text\na*b+c\n```\n\n```text\na*b\n```\n\n```text\nfmaf()\n```\n\n```text\nldexp()\n```\n\n```text\nfloat\n```\n\n```text\nF2XM1\n```\n\n```text\nexp()\n```\n\n```text\nexp2()\n```\n\n```text\nFSCALE\n```\n\n```text\nF2XM1\n```\n\n```text\npow(x,y)\n```\n\n```text\nexp(y*log(x))\n```\n\n```text\nx\n```\n\n```text\ny\n```\n\n```text\nlog(x)\n```\n\n```text\ny*log(x))\n```\n\n```text\npow(double,int)\n```\n\n```text\npowf(float, int)\n```\n\n========================================\n\nComments:\n- You can't reasonably ask about \"which algorithm\" when you're asking about different CPUs or programming languages. Maybe different systems use different algorithms. You'll have to be more specific.\n- @GregHewgill sorry, grammar mistake. I'm just looking for an example, like \"x86 uses sliding door\" or something.\n- In C/C++ math libraries, `pow(double,double)` and `pow(double,int)` are often different code paths, while `exp(double)` is yet another separate function. Which of these are you interested in? `pow(double,int)` often employs a variant of the square-and-multiply approach based on bit-wise scanning of the exponent. The old x87 FPU inside x86 processors has an `F2XM1` instruction that can be used to impement `exp()` and `pow()`. x86-based system usually use SSE instructions these days, the x87 FPU is mostly for legacy support. I can show code for `expf(float)` if that helps as an example.","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":37,"totalLines":416,"estimatedTokens":2375}}832{"id":"stack-18835833","source":"stackoverflow","questionId":18835833,"title":"Is the book Linkers and Loaders by Levine still relevant?","tags":["compiler-construction","linker"],"text":"Title: Is the book Linkers and Loaders by Levine still relevant?\nTags: compiler-construction, linker\nSource: Stack Overflow\n\nQuestion:\nI'm an applications programmer trying to learn systems programming and therefore I don't have a great understanding of these topics. *Linkers and Loaders* seems like a popular reference quoted in many online articles which talk about file formats, their linking and execution.\n\nHowever the book was published back in 1999. Therefore, before purchasing and committing time to it, I want to know if the text is still relevant. That is, are the formats, protocols etc discussed in the book still in use? If not, what can one gain by reading it?","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":8,"estimatedTokens":169}}833{"id":"stack-3481902","source":"stackoverflow","questionId":3481902,"title":"Infix vs Postfix","tags":["data-structures","programming-languages","compiler-construction"],"text":"Title: Infix vs Postfix\nTags: data-structures, programming-languages, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nHad this question in the interview yesterday.\nWhich is better to use? Infix(with parenthesis) or Postfix? State with reason..\n\nI only could tell them that:\n\n- it is easier for the compilers to process postfix expression for arithmetic evaluations and operator precedence.\n\n- More memory is used for storing and processing the parenthesis.\n\nPlease throw some light on whether I am right on this?\n\n========================================\n\nTop Answer:\nMemory is not usually an issue here. Compilers spend tons of memory on other things. Postfix usually entails an operand stack, which involves a certain amount of memory (still small) at runtime.\n\nPostfix is useful for low-level interpretable code, such as Java, Forth, Postscript. These all have hardware or super-lightweight software implementations.\n\nInfix is better for readability.\n\nOf course, there's no way to know how the issue affects your field without knowing what kind of job it is.\n\n========================================\n\nComments:\n- The question is meaningless without some definition of 'better'. Infix is natural for humans, postfix for computers. Yet another pointless interview question. The memory used for parentheses exists only in the source file BTW.\n- Postfix doesn't require any *operator precedence.* Not the same thing as 'order of operations' at all. Postfix is *nothing but* an order of operators.\n- The analogy between postfix and English grammar is very enlightening.\n- Infix also tells what the exxression has to do. It if didn't, it would be impossible to convert it to postfix.\n- @EJP : Thanks for helping me correct the response. :)","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":35,"estimatedTokens":437}}834{"id":"stack-9863623","source":"stackoverflow","questionId":9863623,"title":"Debugging a .net Compiler","tags":[".net","debugging","compiler-construction"],"text":"Title: Debugging a .net Compiler\nTags: .net, debugging, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm currently working on a compiler for a language (an external byte-code), and am using `System.Reflection.Emit`. \n\nNow, everything looks fine when I open the generated assembly in reflector, and it re-compiles as C# just fine as well (and runs), but when I try to run the main function which gets generated, I get an `InvalidProgramException`:\n\n \"Common Language Runtime detected an invalid program.\" \n\nIt is probably just a single op-code that is causing the issue, but, because that main function uses 100+ op-codes, I can't determine which op-code is causing the issue. \n\nEach op-code is very involved, so manually testing every op-code is a no-go.\n\nIs there a way to get .NET to tell me where in the function it's detecting an invalid program? \n\nAnd if not, is there some other tool I can use to determine the source of the issue?\n\n========================================\n\nCode:\n```text\nSystem.Reflection.Emit\n```\n\n```text\nInvalidProgramException\n```\n\n========================================\n\nComments:\n- Have you tried peverify.exe?\n- This was exactly what I was looking for. It turns out I wasn't thinking when I did the And & Or instructions, and ended up with different branches leaving a different number of objects on the stack.\n- PEVerify certainly doesn't perform the same checks as the jitter. For example, it seems the jitter doesn't mind if the `.maxstack` is wrong, but PEVerify shows it as an error.\n- @svick - Yes, I did note some discrepancies too when I played with it. That's why I put the disclaimer in my answer, not otherwise clear from the Microsoft docs. Key point is that it doesn't *promise* to perform the exact same checks. Static code analysis is otherwise definitely a harder problem and the jitter can let things slide if they are not detrimental.","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":37,"estimatedTokens":474}}835{"id":"stack-50546898","source":"stackoverflow","questionId":50546898,"title":"When / where type checking occurs in the compilation process","tags":["ruby","haskell","compiler-construction","jit","typechecking"],"text":"Title: When / where type checking occurs in the compilation process\nTags: ruby, haskell, compiler-construction, jit, typechecking\nSource: Stack Overflow\n\nQuestion:\nWondering at a high level when type check typically occurs (textbook vs. in practice) in the compilation process (at a high level). Roughly my understanding of the compilation process is:\n\n- Parse the source code into an AST\n\n- Convert the AST into an Intermediate Representation IR\n\n- Optimize the IR (i.e. SSA Form, Register Allocation, etc.)\n\n- Simplify the IR\n\n- Generate final output code\n\nWondering if the typechecking occurs in between (1) and (2), (2) and (3), or after (4), or if it occurs sprinkled throughout the whole process, or something else. I'd be interested to know the answer for object oriented, functional, and logic programming (in that order of priority), but if I had to pick one then OO such as a dynamically typed language like Ruby, or statically typed functional language like Haskell.\n\n========================================\n\nComments:\n- Dependently typed languages (e.g., Agda, Coq, Idris) and macro systems (Template Haskell) add a whole extra level of complication to this, because some parts of the program may have to be compiled *and executed* in order to type check other parts.\n- \"The code that performs these dynamic type checks is either part of the interpreter (if there is an interpreter) or inserted by the JIT compiler (i.e. step 5 in your list).\" – And what about AOT compiled implementations of dynamically typed languages? E.g. most CommonLisps are AOT compiled and there was at least one AOT compiled Ruby implementation (BlueRuby by SAP for the ABAP platform). In that case, type checking code is injected into the compiled executable.\n- @J&#246;rgWMittag Right, I don't know why I limited the statement to JIT compilers.","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":459}}836{"id":"stack-40042502","source":"stackoverflow","questionId":40042502,"title":"How does the compiler determine the needed stack size for a function with compiler generated temporaries?","tags":["c++","compiler-construction","stack","temporaries"],"text":"Title: How does the compiler determine the needed stack size for a function with compiler generated temporaries?\nTags: c++, compiler-construction, stack, temporaries\nSource: Stack Overflow\n\nQuestion:\nConsider following code:\n\n```\nclass cFoo {\n private:\n int m1;\n char m2;\n public:\n int doSomething1();\n int doSomething2();\n int doSomething3();\n}\n\nclass cBar {\n private:\n cFoo mFoo;\n public:\n cFoo getFoo(){ return mFoo; }\n}\n\nvoid some_function_in_the_callstack_hierarchy(cBar aBar) {\n int test1 = aBar.getFoo().doSomething1();\n int test2 = aBar.getFoo().doSomething2();\n ...\n}\n```\n\nIn the line where getFoo() is called the compiler will generate a temporary object of cFoo, to be able to call doSomething1().\nDoes the compiler reuse the stack memory which is used for these temporary objects?\nHow many stack memory will the call of \"some_function_in_the_callstack_hierarchy\" reservate? Does it reservate memory for every generated temporary?\n\nMy guess was that the compiler only reserve memory for one object of cFoo and will reuse the memory for different calls, but if I add \n\n```\nint test3 = aBar.getFoo().doSomething3();\n```\n\nI can see that the needed stack size for \"some_function_in_the_callstack_hierarchy\" is way more and its not only because of the additional local int variable.\n\nOn the other hand if i then replace\n\n```\ncFoo getFoo(){ return mFoo; }\n```\n\nwith a reference (Only for testing purpose, because returning a reference to a private member is not good)\n\n```\nconst cFoo& getFoo(){ return mFoo; }\n```\n\nit needs way less stack memory, than the size of one cFoo.\n\nSo for me it seems that the compiler reserves extra stack memory for every generated temporary object in the function. But this would be very inefficient.\nCan someone explain this?\n\n========================================\n\nTop Answer:\nAttempting to analyse *how* a compiler is going to treat a particular piece of code is getting progressively more difficult as optimisation strategies get more aggressive.\n\nAll a compiler has to do is implement the C++ standard and compile the code without introducing or cancelling any side-effects (with some exceptions such as return and named return value optimisation).\n\nYou can see from your code that, since `cFoo` is not a polymorphic type and has no member data, a compiler could optimise out the creation of an object altogether and call what are essentially therefore `static` functions directly. I'd imagine that even at the time of my writing, some compilers are already doing that. You could always check the output assembly to be sure.\n\nEdit: The OP has now introduced class members. But since these are never initialised and are `private`, the compiler can remove them without thinking too hard about that. This answer therefore still applies.\n\n========================================\n\nCode:\n```text\nclass cFoo {\n    private:\n        int m1;\n        char m2;\n    public:\n        int doSomething1();\n        int doSomething2();\n        int doSomething3();\n}\n\nclass cBar {\n    private:\n        cFoo mFoo;\n    public:\n        cFoo getFoo(){ return mFoo; }\n}\n\nvoid some_function_in_the_callstack_hierarchy(cBar aBar) {\n    int test1 = aBar.getFoo().doSomething1();\n    int test2 = aBar.getFoo().doSomething2();\n    ...\n}\n```\n\n```text\nint test3 = aBar.getFoo().doSomething3();\n```\n\n```text\ncFoo getFoo(){ return mFoo; }\n```\n\n```text\nconst cFoo& getFoo(){ return mFoo; }\n```\n\n```text\ngetFoo\n```\n\n```text\ng++\n```\n\n```text\n-Wstack-usage=48\n```\n\n```text\n-fstack-usage\n```\n\n```text\nyourcode.cc\n```\n\n```text\ng++ -S -fverbose-asm -O yourcode.cc\n```\n\n```text\nyourcode.s\n```\n\n```text\n-O\n```\n\n```text\n-O2\n```\n\n```text\n-O3\n```\n\n```text\ng++ -O -fdump-tree-all -c yourcode.cc\n```\n\n```text\ncFoo\n```\n\n```text\nstatic\n```\n\n```text\nprivate\n```\n\n========================================\n\nComments:\n- It's pretty much implementation-defined. It is not even guaranteed that objects with automatic storage will be created on the stack in the first place.\n- Just in case you did not make sure you compile with something like `-O2` enabled. Analyzing a non-optimized build is not very useful.\n- It is compiled with -O1\n- I forgot to add some members to cFoo. I've edited the question, so that a instance of cFoo needs memory for the member variables.\n- Thanks for invalidating my answer ;-). It still applies since the variables are never initialised.\n- That would only be true if they are never used. But they are used in the doSomething function. Keep in mind that this code is only for the question. I cannot show you my real code, because it belongs to my employer","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":18,"totalLines":177,"estimatedTokens":1143}}837{"id":"stack-11376089","source":"stackoverflow","questionId":11376089,"title":"What is the purpose of a lexer?","tags":["compiler-construction","lexical-analysis"],"text":"Title: What is the purpose of a lexer?\nTags: compiler-construction, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nI was reading the answer to this question.\nI can't seem to find the answer to why someone would **need a lexer** separately\n\nIs it one of the steps a program goes through during compilation? \nCan someone please explain in simple terms why I would need a lexer, and what purpose it would serve?\n\n========================================\n\nTop Answer:\nA good example is in the Wikipedia http://en.wikipedia.org/wiki/Lexical_analysis.\n\nFor example if you want to evaluate the expression \"(33+3)*2\" the first step is to split the string into tokens \"(\", \"33\", \"+\", \"3\", \")\", \"*\", \"2\". As far as I remember my course about compilers this is done by longest-match word-automata.\n\n========================================\n\nComments:\n- yes, it is the first step of a compilation process, splitting a plain text of a program into a sequence of tokens.","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":22,"estimatedTokens":240}}838{"id":"stack-45450156","source":"stackoverflow","questionId":45450156,"title":"How does ANTLR decide which lexer rule to apply? The longest matching lexer rule wins?","tags":["compiler-construction","antlr","antlr4","lexer"],"text":"Title: How does ANTLR decide which lexer rule to apply? The longest matching lexer rule wins?\nTags: compiler-construction, antlr, antlr4, lexer\nSource: Stack Overflow\n\nQuestion:\nThe input content:\n\nhttps://i.sstatic.net/mYQPB.png\n\nThe grammar:\n\n```\ngrammar test;\n\np : EOF;\n\nChar : [a-z];\n\nfragment Tab : '\\t';\nfragment Space : ' ';\nT1 : (Tab|Space)+ ->skip;\n\nT2 : '#' T1+ Char+;\n```\n\nThe matching result is this:\n\n```\n[@0,0:6='# abc',,1:0] ',,1:7]\nline 1:0 extraneous input '# abc' expecting \n```\n\nPlease ignore the error in the last line. I am wondering why the token matched at **PLACE 1** is `T2`. \n\nIn the grammar file, the `T2` lexer rule goes **after** the `T1` lexer rule. So I expect `T1` rule should get applied first. So why the spaces in `# abc` is not skipped?\n\nDoes ANTLR uses some greedy strategy to match current character stream with the longest lexer rule?\n\n========================================\n\nTop Answer:\nIn response to ...\n\nDoes ANTLR uses some greedy strategy to match current character stream with the longest lexer rule?\n\n... I will quote from ANTLR4's Wildcard Operator and Nongreedy Subrules documentation.\n\nHere is how the lexer chooses token rules:\n\n- The primary goal is to match the lexer rule that recognizes the most input characters.\n\n```\nINT : [0-9]+ ;\nDOT : '.' ; // match period\nFLOAT : [0-9]+ '.' ; // match FLOAT upon '34.' not INT then DOT\n```\n\n- If more than one lexer rule matches the same input sequence, the priority goes to the rule occurring first in the grammar file.\n\n```\nDOC : '/**' .*? '*/' ; // both rules match /** foo */, resolve to DOC\nCMT : '/*' .*? '*/' ;\n```\n\n- Nongreedy subrules match the fewest number of characters that still allows the surrounding lexical rule to match.\n\n```\n/** Match anything except \\n inside of double angle brackets */\nSTRING : '>' ; // Input '>>>' matches STRING then END\nEND : '>>' ;\n```\n\n- After crossing through a nongreedy subrule within a lexical rule, all decision-making from then on is \"first match wins.\"\n\nFor example, literal `ab` in rule right-hand side (grammar fragment) `.*? ('a'|'ab')` is dead code and can never be matched. If the input is ab, the first alternative, 'a', matches the first character and therefore succeeds. ('a'|'ab') by itself on the right-hand side of a rule properly matches the second alternative for input `ab`. This quirk arises from a nongreedy design decision that’s too complicated to go into here.\n\nIf you understand rules 1, 2, and 3, you will likely be fine. The fourth rule is esoteric.\n\nBased only the information quoted above, I don't see a definitive answer as to where the implicit token rule applies. As I find more information, I will update this answer.\n\nI encourage you to also review TomServo's answer, which talks more about the implicit token rule.\n\n(Aside: in my opinion, the content quoted above probably would be more discoverable and understandable if incorporated into the lexer rules docs.)\n\n========================================\n\nCode:\n```text\ngrammar test;\n\np : EOF;\n\nChar : [a-z];\n\nfragment Tab : '\\t';\nfragment Space : ' ';\nT1 : (Tab|Space)+ ->skip;\n\nT2 : '#' T1+ Char+;\n```\n\n```text\n[@0,0:6='#   abc',<T2>,1:0]    <<<<<<<< PLACE 1\n[@1,7:6='<EOF>',<EOF>,1:7]\nline 1:0 extraneous input '#   abc' expecting <EOF>\n```\n\n```text\nT2\n```\n\n```text\nT2\n```\n\n```text\nT1\n```\n\n```text\nT1\n```\n\n```text\n#   abc\n```\n\n```text\n#\n```\n\n```text\n#\n```\n\n```text\nT2\n```\n\n```text\nT2\n```\n\n```text\nT1\n```\n\n```text\nINT : [0-9]+ ;\nDOT : '.' ; // match period\nFLOAT : [0-9]+ '.' ; // match FLOAT upon '34.' not INT then DOT\n```\n\n```text\nDOC : '/**' .*? '*/' ; // both rules match /** foo */, resolve to DOC\nCMT : '/*' .*? '*/' ;\n```\n\n```text\n/** Match anything except \\n inside of double angle brackets */\nSTRING : '<<' ~'\\n'*? '>>' ; // Input '<<foo>>>>' matches STRING then END\nEND    : '>>' ;\n```\n\n```text\nab\n```\n\n```text\n.*? ('a'|'ab')\n```\n\n```text\nab\n```\n\n========================================\n\nComments:\n- Did I overlook these rules in TDAR: The Definitive ANTLR 4 Reference? I searched for \"longest match\" and only found \"Lexers ... just look for the longest match and make decisions after they’ve seen the entire token.\" in Chapter 11.\n- @DavidJ. The quote I'm referring to, if memory serves (it's been a long time) was one Sam made here on SO to clear up some confusion about lexing. I didn't even know there was an online version of the book; I have a paper copy on my shelf. I don't frequent this tag as much as I used to, working on other more compact lexers and parsers now for embedded systems.\n- Thanks. I also found documentation about this tucked away in the wildcard section of the official docs: github.com/antlr/antlr4/blob/master/doc/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":18,"totalLines":182,"estimatedTokens":1174}}839{"id":"stack-54955861","source":"stackoverflow","questionId":54955861,"title":"How compiler and interpreter both are used in one language?","tags":["compiler-construction","interpreter"],"text":"Title: How compiler and interpreter both are used in one language?\nTags: compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nI have read enough explanation about the definition of compiler, interpreter and \"things\" that use both. I didn't find how compiler and interpreter both are used in one language.\n\n========================================\n\nTop Answer:\nJava is one of the first machine independent programming languages; it uses both compiler and interpreter.\nJava compilers are designed in such a way that converts source code into platform independent form i-e byte codes.\nThese byte codes are then converted to machine code by interpreter. This is how compiler and interpreter both used in one language. Any system having JVM will run these byte codes.\n\n```\nJava program  byte code  interpreted by VM  machine language\n```\n\nsummary :\n\njava compiler convert source code in to an intermediate language known as bytecode. This bytecode only can be executed in a virtual environment called JVM.\nJava virtual machine. JVM is an interpreter to java bytecode. It converts bytecode into machine language and executes line by line.\n\nthis is how both compiler and interpreter used in one language..if it found useful you may mention\n\n========================================\n\nCode:\n```text\nJava program  byte code  interpreted by VM  machine language\n```\n\n========================================\n\nComments:\n- which language are you talking about?\n- it can be any language.\n- Have you read about JIT?\n- Interpreters generally execute a compiled p-code, not the source code.\n- Java is not the first machine-independent programming language. Python is also platform-independent and it was released prior to Java. Other languages like LISP and FORTRAN were one of the first platform-independent languages. Please correct your answer","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":40,"estimatedTokens":463}}840{"id":"stack-29749979","source":"stackoverflow","questionId":29749979,"title":"How does assembler compute segment and offset for symbol addresses?","tags":["assembly","compiler-construction","masm","x86-16","memory-segmentation"],"text":"Title: How does assembler compute segment and offset for symbol addresses?\nTags: assembly, compiler-construction, masm, x86-16, memory-segmentation\nSource: Stack Overflow\n\nQuestion:\nI have learned about compilers and assembly language, so I'd like to write my own assembler as an exercise. But there I have some questions;\n\nHow can I compute the address for segments such as @DATA or like OFFSET/ADDR VarA?\n\nTake an easy assembly program as an example:\n\n```\n.model small\n .stack 1024\n .data\n msg db 128 dup('A')\n .code\n start:\n mov ax,@data\n mov ax,ds\n mov dx, offset msg\n ; DS:DX points at msg\n mov ah,4ch\n int 21h ; exit program without using msg\n end\n```\n\nSo how does the assembler calculate the segment address for the `@data` segment?\n\nAnd how does it know what to put into the immediate for `mov dx, offset msg`?\n\n========================================\n\nTop Answer:\nHow can I compute the address for segments such as @DATA or like OFFSET/ADDR VarA?\n\nThere are 2 cases:\n\na) the assembler is generating a flat binary or executable file itself, and no linker is involved\n\nb) the assembler is generating an object file to be sent to a linker later\n\nNote that you can have a mixture. For example, in some assemblers (e.g, NASM) there's keywords to create a temporary section (e.g. `absolute`) and structures are supported by internally using a temporary section (a field in a structure is an offset into a temporary section that begins at address zero).\n\nFor both cases; the assembler converts the source code into some kind of internal representation (e.g. maybe an \"instruction data, operand 1 data, operand 2 data, ...\" thing) where the internal representation for instructions like \"`jmp foo`\" and \"`mov eax,bar/5+33`\" can be simplified too much and needs to include some reference to a symbol in the symbol table.\n\nFor the symbol table itself, each entry has a symbol name (e.g. \"foo\"), which section it is in, the lowest possible offset within the section and the highest possible offset within the section. When the lowest possible offset and highest possible offset match, and the section has a known address, the assembler can replace references to that symbol in the internal representation with an actual value.\n\nNote that there are cases where you can't know how large an instruction will be until later (e.g. for 80x86; \"`jmp foo`\" could be a 2 byte instruction if the target address is close, but may need to be a 3 byte instruction or 5 byte instruction if the target address isn't close, and you can't decide until you know something about the value that \"foo\" will have); and when you can't know how large an instruction will be you can't know the offset of any symbols that occur later in the same section. This is why you end up wanting symbols to have both lowest possible offset and highest possible offset - so that even when you don't know the actual offset of a symbol you can still know that the offset will be small enough or too large and can still determine out how big an instruction will be (and get a better idea of the values of later symbols in that section).\n\nMore specifically; while assembling you want to do multiple passes, where each pass tries to convert the intermediate representations of each instruction into more specific/complete versions and tries to improve the lowest possible offset and highest possible offset values for symbols (so that you have more/better information that the next pass can use).\n\nWhen you have finished doing the \"multiple passes\" and the assembler is generating a flat binary and no linker is involved; everything will be known (including the address of sections and offset of all symbols within sections, and will have converted all instructions into actual bytes) and you can generate the final file.\n\nWhen you have finished doing the \"multiple passes\" and the assembler is generating an object file; some things will not be known (the address of sections) and some things will be known (the offset of all symbols within sections, the size of all instructions); and the object file format will provide a way for you to provide details of things you don't/can't know (e.g. a list of things that need fixing, and information the linker can use to fix them) that you can provide from what's left of the intermediate representation of instructions and the symbol table.\n\nNote that there can be cases that are too complex for an object file format to support (e.g. probably the \"`mov eax,bar/5+33`\" from earlier), where an instruction that can be assembled without any problem (if the assembler is generating a flat binary) has to be treated as an error (if the assembler is generating an object file). You will discover these cases (and generate appropriate error messages) when trying to create the object file.\n\nNote that this all fits into a nice \"3 phases\" arrangement, where the \"front-end\" converts the \"plain text\" input into the intermediate representation, the \"middle-end\" (the multiple passes) refines the intermediate representation as much as possible, and the \"back-end\" generates a file. Only the back-end needs to care what the target file format is.\n\n========================================\n\nCode:\n```text\n.model small\n    .stack 1024\n    .data\n          msg db 128 dup('A')\n    .code\n    start:\n        mov ax,@data\n        mov ax,ds\n        mov dx, offset msg\n                           ; DS:DX points at msg\n        mov ah,4ch\n        int 21h            ; exit program without using msg\n    end\n```\n\n```text\n@data\n```\n\n```text\nmov dx, offset msg\n```\n\n```text\n.model small\n.stack 1024\n.data\n    msg db 'Hello, World!,'$'\n.code\nstart:\n    mov ax,SEG msg\n    mov ds,ax\n    mov dx,OFFSET msg\n    mov ah,09h\n    int 21h              ; write string in DS:DX to stdout\n    mov ah,4ch\n    int 21h              ; exit(AL)\nend start\n```\n\n```text\n; listing of the .obj assembler output, before linking\n 0000               start:\n 0000  B8 ---- R            mov ax,SEG msg\n 0003  8E D8                mov ds,ax\n 0005  BA 0000 R            mov dx,OFFSET msg\n 0008  B4 09                mov ah,09h\n```\n\n```text\n@data\n```\n\n```text\nmsg\n```\n\n```text\nmsg\n```\n\n```text\nR\n```\n\n```text\nmsg\n```\n\n```text\n.obj\n```\n\n```text\n.exe\n```\n\n```text\nmsg\n```\n\n```text\nmov ax,@data\n```\n\n```text\nmov ax,SEG msg\n```\n\n```text\nabsolute\n```\n\n```text\njmp foo\n```\n\n```text\nmov eax,bar/5+33\n```\n\n```text\njmp foo\n```\n\n```text\nmov eax,bar/5+33\n```\n\n========================================\n\nComments:\n- The first variable has offset 0 in data segment, if the variable is 128 bytes long, the second variable will start at offset 128 (because the first one takes bytes from 0 to 127 = 128 bytes). If the second variable starts at offset 128 of the data segment, and its size is DW (2 bytes), it will take bytes 128 and 129, and so on. Variables names are nothing but friendly names for offsets.\n- so u mean \"mov ax,@data\" will actually recognized by assembler as \"mov ax, 0x0h\"?\n- No. One thing is offset and other thing is segment. Offset is an address inside a segment. @data gets the data segment address from the operating system. Most of programs have three segments : stack, data and code. Each of them have different segment address, but they all have offsets starting at 0 inside of them to address their contents. If you assign 0 to data segment, you will probably point the data segment to interrupt vector and your program will halt. Segments are assigned by operating system.\n- Ok, so how does assembler do when it sees \"mov ax, @data\"? does it use some other command to replace this one? or it calculate @data when compiling?\n- @user152531: The segment isn't know until run-time and the assembler/linker uses a dummy constant in place of the unknowable `@data` segment address. In addition the linker emits a relocation entry in the metadata of the `EXE` file pointing out that this particular immediate constant within the code needs to be patched up by the `DOS` loader with the actual address of the data segment at run-time.\n- @doynax can u explain a bit more detail about this immediate constant?\n- @user152531: `MOV AX,01234h` looks the same as `MOV AX,@data` to the CPU, an opcode and immediate constant. The base segment where `DOS` loads a program isn't know at compile-time. Instead the assembler pretends the base segment is zero while including a relocation table listing all the places making absolute segment referencing. During load `DOS` goes walks the list adding the base segment to each. Forget about the funky x86 segmentation and imagine you're writing a multitasking OS with a shared linear address space. How do you go about fixing-up the addresses in the programs once loaded?\n- Suggestion: writing an assembler that understands segmentation is a potentially-significant extra complication on top of just writing an assembler at all as an exercise. Segmentation is basically a dead technology that's obsoleted by CPUs with registers wide enough for a full address for a useful amount of memory (32 or 64 bits). x86 machine code is complex enough on its own. (Although the complexity of segmentation is mostly separate from the machine-encoding.)\n- The compact-encoding phases need to know if symbol offsets are assemble-time constants (and thus can maybe use `[reg+disp8]` addressing modes, or `add reg, imm8` immediates) or whether it needs to leave a 16 or 32-bit slot with a relocation. But I guess that happens fairly naturally, and you're excluding that from the middle end needing to know about the file format.\n- \"a field in a structure is an offset into a temporary section that begins at address zero\" -- In NASM I actually added support for structures with non-zero starting offset: sourceforge.net/p/nasm/feature-requests/160","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":20,"totalLines":186,"estimatedTokens":2431}}841{"id":"stack-29444312","source":"stackoverflow","questionId":29444312,"title":"What is the lookahead symbol?","tags":["compiler-construction","grammar"],"text":"Title: What is the lookahead symbol?\nTags: compiler-construction, grammar\nSource: Stack Overflow\n\nQuestion:\nIn the grammars (for example **LL(1)**), **1** denotes the lookahed symbol. In practice i don't understand what is this symbol. To understand, i need a simple and practical example.\n\n========================================\n\nCode:\n```text\nS -> F (Rule1)\n\nS -> ( S + F ) (Rule2)\n\nF -> a (Rule3)\n```\n\n```text\n(   )   a   +   $\nS   2   -   1   -   -\nF   -   -   3   -   -\n```\n\n```text\nS\n        / / | \\ \\\n       (  S + F  )\n```\n\n```text\nS\n        / / | \\ \\\n       (  S + F  )\n          |\n          F\n          |\n          a\n```\n\n```text\nS\n        / / | \\ \\\n       (  S + F  )\n          |   |\n          F   a\n          |\n          a\n```\n\n```text\nO(n)\n```\n\n```text\n%\n```\n\n```text\n( a + a )\n```\n\n```text\n%( a + a )\n```\n\n```text\n(\n```\n\n========================================\n\nComments:\n- Such a lookahead is a symbol that is interpreted \"command like\" by some processors. It allows to peek ahead, so to read and evaluate a portion of the input stream *without actually forwarding the location of the stream*. As an effect the next read operation will read the same sequence. The benefit: you can see in advance what you have to expect from the input to come. Sorry, no example at hand currently...\n- Seems like a question for Programmer's StackExchange\n- Nitpick: the unit of lookahead is tokens, not characters. A token may be comprised of more than one character.","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":10,"totalLines":76,"estimatedTokens":367}}842{"id":"stack-13179102","source":"stackoverflow","questionId":13179102,"title":"How do the standard C library and system calls work together?","tags":["c","compiler-construction","kernel","glibc","standard-library"],"text":"Title: How do the standard C library and system calls work together?\nTags: c, compiler-construction, kernel, glibc, standard-library\nSource: Stack Overflow\n\nQuestion:\nI recently got interested about inner workings of compilers, standard libraries and kernels. While I was searching for the source code of the standard C library, I came across with Glibc. But what it says in Glibc's official website is: `the library which defines the ''system calls'' and other basic facilities such as open, malloc, printf, exit...` \n\nSo I guess that Glibc actually doesn't provide the source code of the standard C library, but instead provides the system calls for those functions, and then the kernel takes care of them, am I right? \n\nI would like to learn more about those things. For example, how do the `sin`, `printf`, and `strlen`, functions get executed in C programs? If Glibc provides just the system calls, where are the actual source codes of those functions? How does the kernel execute them? Where can be found the source code of the part of the kernel that executes those functions?\n\n========================================\n\nTop Answer:\nYou can get a feeling for the system calls at this page. It's old, but gives you the idea and a pointer to files in the kernel where the calls are implemented. There are low hundreds of system calls in a typical unix system. For example you can see sys_open, or sys_write. When you look at calls like fopen or open, at the end of the day they all map down to sys_open. Things like printf or write map down to sys_write. \n\nThe kernel mediates things like access to files, so needs to be a choke point, but the library makes it easier to access these calls by wrapping them in helpful abstractions, often more than one for a given system calls. The libraray doesn't provide the system calls themselves, it sets things up and then traps to the kernel, which implements them. The system calls are effectively the API of the kernel that libc programs against. \n\nSome calls, like strlen, don't need kernel access at all, that call can just look at memory that's already been allocated.\n\nA good tool for exploring this from user space is strace. Given a program it will show all the system calls the program makes. You can use this to tease out how libc calls map to system calls.\n\n========================================\n\nCode:\n```text\nthe library which defines the ''system calls'' and other basic facilities such as open, malloc, printf, exit...\n```\n\n```text\nsin\n```\n\n```text\nprintf\n```\n\n```text\nstrlen\n```\n\n========================================\n\nComments:\n- The source code for glibc is used to build the glibc library. The source code is translated into executable code by the compiler.\n- Do you know about `.so` and `.a` files?\n- Yes, I do know about .so and .a\n- It's quite a stretch to claim that Android phones use the OpenBSD C library, though the Bionic C library used on Android does contain code from the 3 major *BSDs.\n- I agree with @nos, it should be noted that Android uses Bionic libc which is a bastardized version...\n- Just look at the CVS headers in most of the files found in the official repository. Example: github.com/android/platform_bionic/tree/master/libc/stdlib\n- I feel like OP was looking for what should have been in section 2 of that link you posted. Too bad its \".. not ready yet .. \"","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":51,"estimatedTokens":839}}843{"id":"stack-70738522","source":"stackoverflow","questionId":70738522,"title":"Is LLVM IR a machine independent language?","tags":["c","compiler-construction","llvm","llvm-ir"],"text":"Title: Is LLVM IR a machine independent language?\nTags: c, compiler-construction, llvm, llvm-ir\nSource: Stack Overflow\n\nQuestion:\nWhen I was reading LLVM IR code (transformed from C), I saw an instruction like this:\n\n```\n%div = sdiv i32 %add, %32\n```\n\nThe C code of this instruction may be like this:\n\n```\na = c / b;\n```\n\nWe can see that there is a type `i32` in this instruction. This is because of the type of variables `a, b, c` are `int`. And my computer is x86.\n\nNow, if I have a machine where the integer is about 8 bits and I write the C code same as above and transform C to LLVM IR on this machine, the LLVM IR instruction might be `%div = sdiv i8 %add, %32`.\n\nIs this right? If yes, how can we say that the LLVM IR is a **machine independent language**?\n\nI thought that if a piece of code is in a machine independent language, then this code can run on any machine in this world, and when the code is running, the virtual machine or other things will handle the differences between architectures.\n\n========================================\n\nTop Answer:\nLLVM IR is a machine independent *language*, but that's not enough for machine independent *programs*. Programs require more than just a language. LLVM has classes called TargetMachine and DataLayout, for example, that collect target-specific details such as how structs are laid out in memory and whether pointers to odd-numbered addresses are legal. You'll often see code in that machine-independent language using target-dependent numbers. On this platform the code adds 4, on that other, 8.\n\nBut in both cases the language is the same and the `add` instruction is the same, which simplifies life for compiler maintainers.\n\n========================================\n\nCode:\n```text\n%div = sdiv i32 %add, %32\n```\n\n```c\na = c / b;\n```\n\n```text\ni32\n```\n\n```text\na, b, c\n```\n\n```text\nint\n```\n\n```text\n%div = sdiv i8 %add, %32\n```\n\n```text\nadd\n```\n\n========================================\n\nComments:\n- What do you mean by “a machine [where] the integer is about 8 bits”? If you’re referring to `int`, the answer is that it’s a C type, not a machine type. If you compile with a 32-bit `int`, LLVM will implement 32-bit integer operations for whatever supported platform you target. (Note: an `int` can’t be 8 bits in standard C, but it can be as low as 16.)\n- @Ry-♦ I mean, if I want to run the instruction` %div = sdiv i32 %add, %32` on a machine that the size of `int is 8 bits`, how will the virtual machine or other things (like code generation) do to handle such case?\n- So, again, `int` is a C type. Its size depends on the C compiler, not the machine the compiled program runs on. The same C code compiling to different LLVM IR depending on which compiler configuration you use doesn’t really say anything about LLVM IR.\n- It seems that there is some confusion here. C standard doesn't define the `int` size, that can vary from machine to machine, and compiler. Rewrite your source defining `a`, `b` and `c` as `int32_t` then check again the IR.\n- So, if I want to run the instruction `%div = sdiv i32 %add, %32` on a machine that the size of `int` is 8 bits, what is the process to retarget?\n- A possible solution of the retarget: Use a loop to do 32bit math operations with 8 bit accumulator and registers. Similar to the 32bit C Language statements being compiled to 8bit machine code.\n- Thanks. I did leave out details, though… let's say that I tried to balance clarity against detail.","metadata":{"transformedAt":"2026-08-18T18:32:17.835Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":73,"estimatedTokens":864}}844{"id":"stack-37691097","source":"stackoverflow","questionId":37691097,"title":"Circular function calls when evaluating AST nodes for interpreter","tags":["compiler-construction","f#","functional-programming"],"text":"Title: Circular function calls when evaluating AST nodes for interpreter\nTags: compiler-construction, f#, functional-programming\nSource: Stack Overflow\n\nQuestion:\nI have the following situation:\n\n```\nlet private runStatement (vars : Map) stmt =\n match stmt with\n | Assignment (id, expr) -> runAssignment vars id expr\n | Print exprs -> runPrint vars exprs\n | Read id -> runRead vars id\n | If (cond, stmts) -> runIf vars cond stmts\n\nlet rec private runStatements vars stmts =\n match stmts with\n | stmt::rest ->\n let newVars = runStatement vars stmt\n runStatements newVars rest\n | [] -> vars\n\nlet private runIf vars conditionalValue statements =\n match conditionalValue with\n | Boolean v when v -> runStatements vars statements\n | Boolean v -> vars\n | _ -> failwith \"Not a boolean expression in if statement\"\n```\n\nAs you can see, function `runStatement` calls `runIf`, and `runIf` calls `runStatement`, because an if-statement is formed by some general statements, and a general statement can be an if-statement.\n\nHow can I solve this situation?\n\nPS.: I have similar situations with other functions like `runWhile`, `runIfElse` et cetera.\n\n========================================\n\nCode:\n```text\nlet private runStatement (vars : Map<identifier, value>) stmt =\n    match stmt with\n    | Assignment (id, expr) -> runAssignment vars id expr\n    | Print exprs -> runPrint vars exprs\n    | Read id -> runRead vars id\n    | If (cond, stmts) -> runIf vars cond stmts\n\nlet rec private runStatements vars stmts =\n    match stmts with\n    | stmt::rest ->\n        let newVars = runStatement vars stmt\n        runStatements newVars rest\n    | [] -> vars\n\nlet private runIf vars conditionalValue statements =\n    match conditionalValue with\n    | Boolean v when v -> runStatements vars statements\n    | Boolean v -> vars\n    | _ -> failwith \"Not a boolean expression in if statement\"\n```\n\n```text\nrunStatement\n```\n\n```text\nrunIf\n```\n\n```text\nrunIf\n```\n\n```text\nrunStatement\n```\n\n```text\nrunWhile\n```\n\n```text\nrunIfElse\n```\n\n```text\nlet rec runx () = \n    printf \"runx\"\n    runy ()\nand runy () =\n    printf \"runy\"\n    runx ()\n\nrunx () |> ignore\n```\n\n```text\nrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxrunyrunxruny\n```\n\n========================================\n\nComments:\n- Please always include all code (types, functions, `open` statements) required to make the example compile. That allows others to focus on providing a solution without first having to fix the code.\n- Pass the functions in as arguments instead of coupling to them.\n- As a side note: I take it this is part of your compiler project since you used the tag `compiler-construction`. You should drop the `run` from the function names because people use to working with parsers expect the name of the function to be the same as the term they are parsing. So for this the terms are `statement`, `statements`, and `if`. I know you will get an error if you name a function `if` so what I would do is to name it `ifParser`, but not use run and that leads me to believe that it is a function that can be called from the main program and while that is possible is not custom.\n- @TeaDrivenDev, the accepted answer contains a piece of code that reproduces my problem integrally, so now maybe I don't need include more code in my question anymore.\n- @GuyCoder, you are right, this is part of my compiler project. This code is from my `Interpreter` module, it takes an AST genereted by my parser and interprets it.\n- Ahh. I see why you have run now. As I don't know exactly how you structured your AST so this may not be correct, however walking the AST is typically how one interprets the AST. e.g. instead of having functions that call other functions, you walk the AST and as you visit each node you interpret what that node is to do. See: AST interpreter? Also I view answers by Ira Baxter for compilers in the same light as F# answers by Tomas.\n- Also take a look at: Arithmetic Expression Evaluation using Reverse Polish Notation (RPN)\n- When the interpreter got done, I'll post it in codereview.stackexchange.com, and I would appreciate an review by you @GuyCoder.\n- Phillip, you are right, this the solution for my question. -- As a side note: After reviewing my code I've noticed that my language has left recursion and that's why I'm facing this kind of problem. I will refactor my language to remove this left recursion. (I'm saying it because it can help others).","metadata":{"transformedAt":"2026-08-18T18:32:17.836Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":9,"totalLines":111,"estimatedTokens":1112}}845{"id":"stack-20405333","source":"stackoverflow","questionId":20405333,"title":"Java Compiler: how should Java language be mapped to JVM instructions?","tags":["java","compiler-construction"],"text":"Title: Java Compiler: how should Java language be mapped to JVM instructions?\nTags: java, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThe semantics of constructs in Java language are defined by the Java language specification, but they're not in terms of JVM instructions. So, is there possiblity that the specification is not strictly defined to prevent mis-implementation of Java compilers? Or the mapping between Java language and JVM instructions is pretty straightforward, there's no need to worry about this?\n\n========================================\n\nTop Answer:\nIt would defeat much of the point of separating the language from the bytecode for there to be a clear mapping of Java code to JVM instructions. Instead the compiler has the freedom to make determinations of which instructions best represent the provided source, in order to optimize the efficiency of what's actually run.\n\nThis does mean it's possible for a compiler to do the wrong thing, however generally speaking you can trust major compilers like Oracle's are doing the right thing; they get tested *a lot*.\n\n========================================\n\nCode:\n```text\njavac\n```\n\n========================================\n\nComments:\n- At one time IBM had a java compiler that not only was fast as hell, but it produced better code that Sun's `javac`. So my guess would be no, there is no strict mapping of java code to JVM instructions, otherwise no compiler could do a better job than any other.\n- @PaulTomblin, it's reasonable that a compiler can do optimization about the generated instructions, but I would like to know if there's some STRICT constraints in terms of JVM instructions.\n- There are almost infinite ways which any given Java program can be converted to equivalent JVM instructions (the same would be true if you were compiling to assembly language). As long as the resulting bytecode does what the program is supposed to do (according to the Java language spec), that is all that matters.\n- correction, there *is* an infinite number of ways :-)\n- @Todd, it seems to me that \"a compiler can do optimization about the generated instructions\" and \"strict constraints in terms of JVM instructions\" are contradictory. Either the language defines \"for java statement `foo` the compiler will generate jvm instructions `bar`\" or it doesn't, and if it does, there is no scope for compiler optimization.","metadata":{"transformedAt":"2026-08-18T18:32:17.836Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":29,"estimatedTokens":599}}846{"id":"stack-34254103","source":"stackoverflow","questionId":34254103,"title":"Shift/reduce conflict in java cup - dangling else issue","tags":["java","parsing","compiler-construction","cup"],"text":"Title: Shift/reduce conflict in java cup - dangling else issue\nTags: java, parsing, compiler-construction, cup\nSource: Stack Overflow\n\nQuestion:\nI am getting the following error: \n\n```\nWarning : *** Shift/Reduce conflict found in state #116\nbetween Statement ::= Matched (*) \nand Unmatched ::= IF LPAREN Condition RPAREN Matched (*) ELSE Unmatched \nand Matched ::= IF LPAREN Condition RPAREN Matched (*) ELSE Matched \nunder symbol ELSE\nResolved in favor of shifting.\n```\n\nNow, i am aware of the dangling else problem, and i have tried making the grammar unambiguous: \n\n```\nStatement ::= Matched | Unmatched ;\n\nMatched ::= IF LPAREN Condition RPAREN Matched ELSE Matched\n |\n Others\n ;\n\nUnmatched ::= IF LPAREN Condition RPAREN Statement \n | \n IF LPAREN Condition RPAREN Matched ELSE Unmatched\n ;\n```\n\nIs there any way to resolve this problem without the precedence operator, or is there something else wrong with the grammar?\n\n========================================\n\nCode:\n```text\nWarning : *** Shift/Reduce conflict found in state #116\nbetween Statement ::= Matched (*) \nand     Unmatched ::= IF LPAREN Condition RPAREN Matched (*) ELSE Unmatched \nand     Matched ::= IF LPAREN Condition RPAREN Matched (*) ELSE Matched \nunder symbol ELSE\nResolved in favor of shifting.\n```\n\n```text\nStatement ::= Matched | Unmatched ;\n\n\nMatched ::= IF LPAREN Condition RPAREN Matched ELSE Matched\n            |\n            Others\n             ;\n\nUnmatched ::= IF  LPAREN Condition RPAREN Statement \n              | \n              IF  LPAREN Condition RPAREN Matched ELSE Unmatched\n              ;\n```\n\n```text\nStatement ::= Matched | Unmatched ;\n```\n\n```text\nUnmatched_If ::= IF LPAREN Condition RPAREN Statement\n               | IF LPAREN Condition RPAREN Matched ELSE Unmatched ;\nMatched_If   ::= IF LPAREN Condition RPAREN Matched ELSE Matched ;\n```\n\n```text\nWhile        ::= WHILE LPAREN Condition RPAREN Statement ; /* Wrong! */\n```\n\n```text\nwhile (x) if (y) do_x_and_y;\n```\n\n```text\nWHILE LPAREN Condition RPAREN Unmatched_If\n-> WHILE LPAREN Condition RPAREN Statement\n-> Matched\n```\n\n```text\nif (w)\n  while (x) if (y) do_this;\nelse do_that;\n```\n\n```text\nIF ( Condition:[w] ) Matched:[while(x)if(y)do_this;] ELSE Statement:[do_that;]\n```\n\n```text\nif (w)\n  while (x)\n    if (y) do_this; else do_that;\n```\n\n```text\nUnmatched_While ::= WHILE LPAREN Condition RPAREN Unmatched ;\nMatched_While   ::= WHILE LPAREN Condition RPAREN Matched ;\n```\n\n```text\nMatched   ::= Matched_If\n            | Matched_While\n            | Matched_For\n            | ...\n            | Simple_Statement\n            ;\nUnmatched ::= Unmatched_If\n            | Unmatched_While\n            | Unmatched_For\n            | ...\n            ;\n```\n\n```text\nMatched\n```\n\n```text\nUnmatched\n```\n\n```text\nMatched\n```\n\n```text\nUnmatched\n```\n\n```text\nUnmatched\n```\n\n```text\nelse\n```\n\n```text\nIf\n```\n\n```text\nMatched\n```\n\n```text\nUnmatched\n```\n\n```text\nelse\n```\n\n```text\nelse\n```\n\n```text\nUnmatched\n```\n\n```text\nwhile\n```\n\n```text\nwhile\n```\n\n```text\nUnmatched\n```\n\n```text\nif...else\n```\n\n```text\nStatement\n```\n\n```text\nUnmatched\n```\n\n```text\nWhile\n```\n\n```text\nUnmatched\n```\n\n```text\nMatched\n```\n\n```text\nMatched\n```\n\n```text\nUnmatched_If\n```\n\n```text\nWhile\n```\n\n```text\nIf\n```\n\n```text\nwhile (x) if (y) do_x_and_y;\n```\n\n```text\nUnmatched_While\n```\n\n```text\nIF LPAREN Condition RPAREN Matched ELSE...\n```\n\n```text\nfor\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.836Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":41,"totalLines":235,"estimatedTokens":843}}847{"id":"stack-29903416","source":"stackoverflow","questionId":29903416,"title":"How to call function in LLVM","tags":["c++","compiler-construction","llvm"],"text":"Title: How to call function in LLVM\nTags: c++, compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nI would like to ask you on the proper way to call/bind own c++ function into LLVM.\n\nI have coded simple function:\n\n```\nvoid writeSomething() {\n std::cout In LLVM I am trying to register the function. I have created the external linkage to it.\n\n```\n// Void type\nllvm::FunctionType* fccType =\n llvm::FunctionType::get(\n llvm::Type::getVoidTy(getGlobalContext()), false\n );\n\n// External - c++\nFunction *fcc = (Function*) module->getOrInsertFunction(\"writeSomething\",\n fccType\n );\n\n// Call\nstd::vector emptyArgs;\nCallInst::Create(fcc, makeArrayRef(emptyArgs));\n```\n\nLLVM Output for just calling this function is \n( // comments are mine input how do I understand the output )\n\n```\n// External linkage\ndeclare void @writeSomething()\n\ndefine internal void @main() {\nentry:\n // Call my function\n call void @writeSomething()\n ret void\n}\n```\n\nThe program ends with message: *LLVM ERROR: Program used external function 'writeSomething' which could not be resolved!*\n\n========================================\n\nCode:\n```text\nvoid writeSomething() {\n    std::cout << \"Awesome\" << std::endl;\n}\n```\n\n```text\n// Void type\nllvm::FunctionType* fccType =\n        llvm::FunctionType::get(\n            llvm::Type::getVoidTy(getGlobalContext()), false\n        );\n\n// External - c++\nFunction *fcc = (Function*) module->getOrInsertFunction(\"writeSomething\",\n        fccType\n        );\n\n// Call\nstd::vector<Value*> emptyArgs;\nCallInst::Create(fcc, makeArrayRef(emptyArgs));\n```\n\n```text\n// External linkage\ndeclare void @writeSomething()\n\ndefine internal void @main() {\nentry:\n  // Call my function\n  call void @writeSomething()\n  ret void\n}\n```\n\n```text\n_Z14writeSomethingv\n```\n\n```text\nextern \"C\" void writeSomething() { ... }\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.836Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":5,"totalLines":92,"estimatedTokens":454}}848{"id":"stack-9221912","source":"stackoverflow","questionId":9221912,"title":"Is the compiler of Java Bootstrapped?","tags":["java","compiler-construction","javac","bootstrapping"],"text":"Title: Is the compiler of Java Bootstrapped?\nTags: java, compiler-construction, javac, bootstrapping\nSource: Stack Overflow\n\nQuestion:\nIs the compiler of Java Bootstrapped ?\n\nHow was the first compiler of java written if it is bootstrapped ?\n\n========================================\n\nTop Answer:\nAccording to http://openjdk.java.net/groups/compiler/:\n\n Note that the code for the native launcher for javac is shared with\n all the other JDK tools, and lives in the JDK repository. If you just\n download and build the langtools repository, you will get an\n executable jar file which you can invoke directly with the java\n command, or with a simple shell script that is provided.\n\nTherefore the compiler is written in Java. So yes, it is bootstrapped in the sense that you need to compile it with an earlier version of the Java compiler.\n\n========================================\n\nCode:\n```text\njavac\n```\n\n========================================\n\nComments:\n- Please define \"bootstrapped\".\n- @ Paul *that proceeds without external help*\n- 'Please explain' what? Compilers are almost always bootstrapped. If you don't know what that means you're not likely to understand that the Java compiler is bootstrapped too.\n- @EJP that is not necessarily true. Java compiler jikes, for example, is written (or at least was when I last checked) in C .\n- The JDK classes are almost entirely written in Java.\n- downvote for terrifying claim that JDK libraries are written in C++.\n- Crikes, I can't believe I wrote that. I meant the *JVM!* Guess I shouldn't answer questions in the middle of a discussion :-)\n- *\"... the JRE requires a C++ compiler.\"* Sure ?\n- @grassPro: yes: hg.openjdk.java.net/jdk7/hotspot/hotspot/file/9b0ca45cd756/s&zwnj;&#8203;rc/&hellip;\n- @paxdiablo That's the JVM. Can we please be precise about these terms.\n- Isn't that a chicken and egg problem? How did they write the first Java compiler if it needed Java to compile to Java which requires Java to compile to Java...\n- en.wikipedia.org/wiki/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.836Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":44,"estimatedTokens":504}}849{"id":"stack-10233035","source":"stackoverflow","questionId":10233035,"title":"Developing a java program to compile .java files","tags":["java","compiler-construction","java-compiler-api"],"text":"Title: Developing a java program to compile .java files\nTags: java, compiler-construction, java-compiler-api\nSource: Stack Overflow\n\nQuestion:\nI am researching for a project that will involve my project to compile simple enough java files. From my research I have decided not to try and build my own compiler as this would just take too much time. \n\nSo does anyone know what would be the best compiler to implement in this project so that I would be able to compile java files that are submitted to the program?\n\n========================================\n\nTop Answer:\nThis almost sounds like you really need a script-y approach that does three things:\n\n- Compile program source with proper language compiler\n\n- Execute resulting program to produce some set of results\n\n- Compare results to expected results to produce a grade\n\nSince you say you want to start with Java, but move to other languages, I would suggest investigating ANT. It will do builds in many languages and can facilitate executing/grading the resulting object code.\n\n========================================\n\nComments:\n- The most obvious solution to this is simply to invoke javac with Runtime.exec. Is there a reason why you can't do this? More information about your situation would help people give better answers.\n- Im really only in the research stage of the project at the moment and trying to find out what the different ways that i could compile .java files from a java program. This is because I am going to build a tool that will compile and grade java programs(for the moment and expand to other programming languages later). It will have alot more features but im just focusing on finding the best way to actually compile the programs first to make sure that they have no errors\n- If you plan on expanding to different languages then I definitely think calling the command-line interface to the compiler is the way to go. That way, your program can be expanded to work with any language that has a command-line interface (i.e. nearly all languages). One problem you will face, though, is how to make sure the environment is set up correctly and your program is integrated with the various build tools that may be necessary. (For example, your Java tool may really want to support Ant builds, as students might want to use Ant for more complex projects.)\n- i was looking at the javac, would you know what the advantage of using this over javac?\n- The advantage is simply that you don't have to spawn off a new process for each compilation.\n- I have to research this so a bit more but looks like at first glance anyway a good way of doing it","metadata":{"transformedAt":"2026-08-18T18:32:17.836Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":31,"estimatedTokens":655}}850{"id":"stack-38969151","source":"stackoverflow","questionId":38969151,"title":"How to write an LLVM backend for stack machine?","tags":["compilation","compiler-construction","llvm"],"text":"Title: How to write an LLVM backend for stack machine?\nTags: compilation, compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nWhen I'm trying to build an LLVM backend without defining `RegisterClass` instances in TableGen file, it throws the following error:\n\n```\nerror:No 'RegisterClass' subclasses defined\n```\n\nHow to define a stack machine target for LLVM (i.e. that doesn't use registers)?\n\n========================================\n\nTop Answer:\nWhile I agree with the admonition to just don't, you should take a look at the WebAssembly backend.\n\nhttps://github.com/llvm/llvm-project/tree/main/llvm/lib/Target/WebAssembly\n\n========================================\n\nCode:\n```text\nerror:No 'RegisterClass' subclasses defined\n```\n\n```text\nRegisterClass\n```\n\n========================================\n\nComments:\n- related question\n- Thank you for your answer, but my goal is to discover if it is possible without writing custom backend. So if you know **any** way to do that - write an update to your answer, please.\n- I'm still learning LLVM and it's not clear for me why DAG is bad for stack based machine. Docs say that SelectionDAG looks like this: `(fadd:f32 (fmul:f32 (fadd:f32 W, X), Y), Z)` - just like lisp ;) So it doesn't seem hard to translate that to stack-based machine code.\n- And for example X86 **is** stack-based, because as I understand if the arguments number is more than registers number, arguments are passed on stack. So, if my understanding is right, I should just avoid passing arguments to registers and pass the directly to stack. Now I created one register that I don't use, and backend isn't working (because it's incomplete yet), but I don't see a big problem.\n- And there's another option - registers can be mapped to memory. It's slower, but should work.\n- @KolesnichenkoDS Edges in the DAG are virtual registers. Stack machine requires a certain order in those edges (and may require some complicated spilling). Your option is just to spill everything, which is very inefficient. You can take a look at how I implemented LLVM IR to a stack machine conversion here: github.com/combinatorylogic/soc/blob/master/backends/small1/&zwnj;&#8203;&hellip; - with a tree reordering and spilling.","metadata":{"transformedAt":"2026-08-18T18:32:17.836Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":40,"estimatedTokens":557}}851{"id":"stack-14113809","source":"stackoverflow","questionId":14113809,"title":"Position of functions in executable","tags":["c","compiler-construction","linker"],"text":"Title: Position of functions in executable\nTags: c, compiler-construction, linker\nSource: Stack Overflow\n\nQuestion:\nIs there a requirement in the C standard that functions in the compiled (and linked) binary will appear in the ordered they are written in the C file?\n\n Please assume that in the example below the compiler did not remove / inline any function, and they all exist in the binary. the question is not about what the compiler might do with empty function, but about the order of the functions.\n\nFor example, if I compile example.c:\n\n```\nvoid bar() { }\nvoid foo() { bar(); }\nint main() { foo(); }\n```\n\nCan I be sure that `foo` will come after `bar` in the output file?\n\n========================================\n\nTop Answer:\nThere is no rule for this in the language. Typically, they do come in the order you expect from looking at the code, but there is nothing saying the compiler can't build a stack of functions, and output them in the completely opposite order - certainly, a function that isn't called can be deleted, and similarly, a function that is inlined and the compiler can determine that it doesn't need an external reference can be deleted in its original form. \n\nYou can find out where a function is by `char *ptr = (char *)bar;`.\n\nEdit: Note, by taking the address of a function, you may alter the inlining of the function, so don't expect this to be a good way to determine what the compiler does \"under normal circumstances\".\n\n========================================\n\nCode:\n```text\nvoid bar() {  }\nvoid foo() {  bar();  }\nint main() {   foo();  }\n```\n\n```text\nfoo\n```\n\n```text\nbar\n```\n\n```text\nchar *ptr = (char *)bar;\n```\n\n```text\nhot\n```\n\n```text\ncold\n```\n\n========================================\n\nComments:\n- I don't think there's *anything* in the C standard about object file layout (or even about object files). Why would that be an interesting guarantee even if there was?\n- @Mat - I was discussing with someone regarding the option of finding the size of a function (approx.) by substracting its pointer from the pointer of the next function. It comes handy in some cases.\n- @BinyaminSharet That's a reasonable use case, but it's something that should be done using the platform's ABI library (libelf, libmacho, etc.). If an implementation properly documents these details, that itself is (should be?) enough to perform such a calculation properly.\n- The compiler can change your code (in-memory) to `int main() { }` and compile this instead. Note that there is no function `foo()` or `bar()` now :)\n- @H2CO3 - thanks, I did not mention a specific format or tool chain because the platforms that I am interested in are not standard and may not have such libraries, in fact, since some of them are embedded, they may not have available libraries at all, as the whole image is a single binary file.\n- @pmg - sure, I just wanted to supply a short example of what I am talking about. I will edit the question to be more clear.\n- @BinyaminSharet Oh, I see. (Just curious: any chance the implementation is opensource and you can extract the necessary parts from the binutils in order to make your calculations?)\n- @pmg, while simplifying `main` to nothing is completely legitimate, the compiler will not delete the symbols because they are exported with visible linkage. This means they could theoretically be called even without any code path leading to them (for instance with `dlsym`).\n- @H2CO3 - most of the times I will be able to do that, but not always, and unfortunatly, the implementation is usually not open source at all.\n- @BinyaminSharet, your problem sounds like an X-Y problem. Shouldn't your question rather be \"how can I find the approximate size a function occupies in memory\"?\n- Any sophisticated compiler can do code motion. This means it may move parts of the function body outside of the \"intuitively obvious\" way of generating the code in a contiguous region of code space. (One reason to do this is to place code blocks in cache lines that do not conflict; in this case, your function is scattered across cache lines). Bottom line: you don't have any guarantee that the function has dense range of addresses that it occupies, unless your compiler provides such a guarantee.\n- As a specific experience, older Microsoft C compilers place functions in source code order. The MS 2010 C compiler rearranges function order. It has a way of optionally specifying function order as a linker command, and it happens to place code contiguously. [I happened to need something pretty close to what you need. In this case, I got what you want :-} I worry about what happens when I go to MS 2012.]\n- Thanks, I know how to find the function pointer, and that the compiler may omit / inline some functions, the question was regarding the order of the functions that actually exist in the compiled binary.\n- There is no rule about it in the language itself. Since compilers will have to keep a lot of data about the code in memory, it makes sense to output \"in the order it came in\", but there's absolutely no guarantee [just like there's no guarantee which order if-statements come, or that the else of one if is immediately after the if - I certainly have debugged Microsoft compiled code where the else is at the end of the function in some cases, despite the original if being the first statement in the function, and there being a lot of other if/else statement at the same \"level\" after that.\n- Only if there is no forward declaration of `bar()`.\n- There is not even a guarantee that a function will occupy continuous set of addresses (newer GCC versions may split a function into a \"hot\" and \"cold\" regions, MSVC compilers also do that). See also gcc.gnu.org/ml/gcc-patches/2011-09/msg01440.html\n- @EmployedRussian Yes, that's correct, just like the storage of the old `std::string` in C++03.","metadata":{"transformedAt":"2026-08-18T18:32:17.836Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":77,"estimatedTokens":1460}}852{"id":"stack-6618611","source":"stackoverflow","questionId":6618611,"title":"Why are some programming languages faster than others?","tags":["performance","compiler-construction","assembly","theory"],"text":"Title: Why are some programming languages faster than others?\nTags: performance, compiler-construction, assembly, theory\nSource: Stack Overflow\n\nQuestion:\nI know that ASM is basically the fastest one can get, but is what makes HLLs slower than ASM the abstraction? What I mean by abstraction is that for instance in C++ you have a class, data needs to be stored about what is stored in the class, what it derives from, private/public accessors, and other things. When this code is compiled, is there actual assembly code that does work to figure out information about the class? Like CPython is built upon C so there is even more abstraction and instructions to be run at run-time than C. Is any of what I am saying true? I think I have answered my own question but I would like to get an answer from a more experienced individual other than myself.\n\nEDIT: I understand that Python is interpreted but wouldn't it still be slower than C if it were compiled?\n\n========================================\n\nTop Answer:\nActually, your premise isn't necessarily true.\n\nMany would say that a good optimizing compiler can outperform hand-coded assembly.\n\nOthers might say that just-in-time compilers like those for Java and .Net can take advantage of runtime heuristics and hence outperform any statically compiled code.\n\nAmong compilers and interpreters, I assure you there is *not* necessarily any correlation\nbetween how high-level the language is and runtime efficiency. *Very* high level languages\ncan produce *extremely* efficient code.\n\nIMHO ...\n\n========================================\n\nComments:\n- So it really comes down to how the compiler or interpreter chooses to create/optimize the actual machine code. Which would make my point about Python being slower if it were a compiled language because the compiler (made in C) would probably take similar steps for creating the machine code as it would in C's compiler.\n- It's easy for many to say such things. Finding objective support for their claims would be much more difficult.\n- Are you too lazy to Google, Jerry Coffin? Or just too obnoxious to contribute anything positive to the discussion?\n- @paulsm4: If you'd bother to do a bit of Googling yourself you'd find that both your accusations are false and insupportable.\n- How come people say that these days JAVA isn't that much slower than C++ anymore?\n- Thank you. Yeah like I said in my comment to paulsm4 the language can be the most convoluted language available but still the fastest if it has a great compiler. This makes much more sense to me now so thanks again!","metadata":{"transformedAt":"2026-08-18T18:32:17.836Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":33,"estimatedTokens":645}}853{"id":"stack-34773528","source":"stackoverflow","questionId":34773528,"title":"Finding the list of successors of a basic block in LLVM","tags":["compiler-construction","llvm","compiler-optimization","llvm-clang"],"text":"Title: Finding the list of successors of a basic block in LLVM\nTags: compiler-construction, llvm, compiler-optimization, llvm-clang\nSource: Stack Overflow\n\nQuestion:\nIn LLVM the BasicBlock has the properties getSinglePredecessor() and getSingleSuccessor(), but I need to get the whole list of successors and predecessors of a basic block. How can I achieve this in llvm?\n\nMy code is \n\n```\nvirtual bool runOnFunction(Function &F) {\n\n for (Function::iterator b = F.begin(), be = F.end(); b != be; ++b) { \n //Here I need to get the predecessor and successsor of the basic block b \n }\n}\n```\n\n========================================\n\nTop Answer:\nSlightly less code if you'd prefer that:\n\n```\n#include \"llvm/IR/CFG.h\"\nBasicBlock *BB = ...;\n\nfor (BasicBlock *Pred : predecessors(BB)) {\n // ...\n}\n```\n\nThe snippet is taken from LLVM's Programmers Handbook.\n\n========================================\n\nCode:\n```text\nvirtual bool runOnFunction(Function &F) {\n\n        for (Function::iterator b = F.begin(), be = F.end(); b != be; ++b) { \n        //Here I need to get the predecessor and successsor of the basic block b \n    }\n}\n```\n\n```text\nfor (Function::iterator b = F.begin(), be = F.end(); b != be; ++b)\n{\n    BasicBlock* bb = dyn_cast<BasicBlock>(&*b);\n    for (pred_iterator pit = pred_begin(bb), pet = pred_end(bb); pit != pet; ++pit)\n```\n\n```text\n#include \"llvm/IR/CFG.h\"\nBasicBlock *BB = ...;\n\nfor (BasicBlock *Pred : predecessors(BB)) {\n  // ...\n}\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.836Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":61,"estimatedTokens":363}}854{"id":"stack-32234737","source":"stackoverflow","questionId":32234737,"title":"How can compiler compute automatically co- and contravariance?","tags":["c#","compiler-construction","covariance","contravariance"],"text":"Title: How can compiler compute automatically co- and contravariance?\nTags: c#, compiler-construction, covariance, contravariance\nSource: Stack Overflow\n\nQuestion:\n*Please note this is a question about internals of compilers.*\n\nI just read [1] that when introducing variance for generic types C# team was thinking whether they should automatically compute if the type is co- or contravariant. Of course this is a history now, but nevertheless I am wondering how could this be done?\n\nIs taking all methods (excluding constructors) and checking if the type is in `in` or `out` position enough?\n\n[1] Jeffrey Richter, CLR via C#, 4th edition, p.281.\n\n========================================\n\nTop Answer:\nIn the example:\n\n```\ninterface I\n{\n S M(T t, R r);\n T N();\n}\n```\n\nyou can use the current C# compiler to see if it is allowed to put `out` (covariance marker) in front of `T`, `S`, and `R`, respectively, and the same for `in` (contravariance marker). Since `T` is used both as a parameter type (first parameter of `M` method) and as a return type (of `N` method), it can have neither `out` nor `in` (current C# compiler can tell, it complains if you try either of them). For `S`, it is used as a return type, so it cannot have `in` (current C# compiler knows). And for `R`, it is used as a parameter type, so it cannot have `out` (current C# compiler knows).\n\nThe designers of C# decided to let the programmer choose if he wanted generic variance or not. So with this example, there are four legal ways to write the `I` interface with variance markers:\n\n```\n// 1\ninterface I\n{\n S M(T t, R r);\n T N();\n}\n\n// 2\ninterface I\n{\n S M(T t, R r);\n T N();\n}\n\n// 3\ninterface I\n{\n S M(T t, R r);\n T N();\n}\n\n// 4\ninterface I\n{\n S M(T t, R r);\n T N();\n}\n```\n\nThe other alternative the designers had, was to *always* make this interface type covariant in `S` and contravariant in `R`, giving the programmer no chance to \"disable\" this. In that case each type parameter would automatically get the \"best\" generic variance possible. The keywords `out` and `in` would not be needed in this context.\n\nSimilarly for generic delegate types.\n\n========================================\n\nCode:\n```text\nin\n```\n\n```text\nout\n```\n\n```text\ninterface I<V, W> \n{ \n     I<V, W> M(I<W, V> x);\n}\n```\n\n```text\ninterface I<T, S, R>\n{\n  S M(T t, R r);\n  T N();\n}\n```\n\n```text\n// 1\ninterface I<T, S, R>\n{\n  S M(T t, R r);\n  T N();\n}\n\n// 2\ninterface I<T, out S, R>\n{\n  S M(T t, R r);\n  T N();\n}\n\n// 3\ninterface I<T, S, in R>\n{\n  S M(T t, R r);\n  T N();\n}\n\n// 4\ninterface I<T, out S, in R>\n{\n  S M(T t, R r);\n  T N();\n}\n```\n\n```text\nout\n```\n\n```text\nT\n```\n\n```text\nS\n```\n\n```text\nR\n```\n\n```text\nin\n```\n\n```text\nT\n```\n\n```text\nM\n```\n\n```text\nN\n```\n\n```text\nout\n```\n\n```text\nin\n```\n\n```text\nS\n```\n\n```text\nin\n```\n\n```text\nR\n```\n\n```text\nout\n```\n\n```text\nI<,,>\n```\n\n```text\nS\n```\n\n```text\nR\n```\n\n```text\nout\n```\n\n```text\nin\n```\n\n========================================\n\nComments:\n- By `in` and `out` do you just mean the keywords, or are you also considering return types and parameters?\n- @31eee384, I mean for param type is in `in` position, and for the result in `out` position.\n- @greenoldman: That makes sense, I overthought it, thanks. On another note, could you maybe add the doc you read about this to the post? I think it would be a good addition (and I'm interested).\n- @31eee384, I can add only reference (see updated post), sorry about that.\n- Well you could never ever have a field of that type, because you can always get and set any field, which would mean that that type would need to be invariant..\n- @Servy, good catch, thank you, but I think **exposed** field. Anyway, now I am reading how Scala handles that (Programing in Scala, 2ed) and there are somewhat more rules even concerning just methods.\n- Eric Lippert explains the exact variance rules much better than I could on his old blog here.\n- @LucasTrzesniewski, could you please post your comment as an answer, it is not possible to accept comment at SO. Thank you.\n- @greenoldman unfortunately a link-only answer isn't acceptable on SO, and I'm not going to copy/paste the whole blog post in here. If you're willing to, you can write a summary as a self-answer and accept that, but good luck with writing that summary :P\n- 1/3 of your \"answer\" is rewritten my question, other 2/3 of your \"answer\" is patronizing me reminding about the syntax of variance in C#. What your \"answer\" brings **new** to my question?\n- @greenoldman I did not intend to be patronizing. I may have misunderstood the question. I tried to answer **How can compiler compute automatically co- and contravariance?** by saying, \"it is more or less the same as the compiler does already, just for each type parameter see if `out`, or `in`, or neither, would be allowed\". The automatic part would consist in the compiler automatically applying `out` or `in` whenever it turns out to be legal to apply them. So the very same logic that is in the current C# specification and current C# compiler, could also have been used to \"infer\" auto-variance.\n- ... Now, Eric Lippert's answer makes it clear that in some cases you cannot consider each type parameter (`T`, `S`, `R`) in isolation, and so things become more complicated. I had not thought about that when I wrote my answer.\n- \"expectation should be encoded into the program text,\" also, it allows the developer to ask the compiler to check this in the event of a mistake - something that you totally expect a statically typed language to do.","metadata":{"transformedAt":"2026-08-18T18:32:17.836Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":24,"totalLines":212,"estimatedTokens":1374}}855{"id":"stack-36985312","source":"stackoverflow","questionId":36985312,"title":"Calculating cost of an instruction in assembly language","tags":["assembly","compiler-construction"],"text":"Title: Calculating cost of an instruction in assembly language\nTags: assembly, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am reading about code generation from the dragon book. It gives a naive method to associate a cost with each target language association.\n\n We shall assume each target-language instruction has an associated cost. For simplicity, we take the cost of an in­struction to be one\n plus the costs associated with the addressing modes of the operands.\n **This cost corresponds to the length in words of the instruction.**\n Addressing modes involving registers have zero additional cost, while\n those in­volving a memory location or constant in them have an\n additional cost of one, because such operands have to be stored in the\n words following the instruction.\n\nSome examples:\n\nThe instruction **LD R0,R1** copies the contents of register R1 into register R0. This\ninstruction has a cost of one because no additional memory words are\nrequired.\nThe instruction **LD R0,M** loads the contents of memory location M into register R0. The cost is two since the address of\nmemory location M is in the word following the instruction.\nThe\ninstruction **LD R1,*100(R2)** loads into register R1 the value given\nby *contents(contents(100+contents(R2)))*. The cost is three\nbecause the constant 100 is stored in the word following the instruction. Here *contents(x)* denotes the contents of the register or memory location represented by x.\n\nI understood the cost calculation for the first 2 examples. I don't get the third one. How is the cost 3? Also I don't understand the bold part in the quoted text above.\n\nOf what I partially understood I supposed the cost of **BLTZ *R3,R0** to be 3 as it is so for the similar third example above. But the cost of this is 1. How?\n\nNote BLTZ r, L causes a jump to label L if the value in register r is\nless than zero, and allows control to pass to the next machine instruction\nif not.\n\n========================================\n\nTop Answer:\nI am sure others could explain this better. But however, here are some inspirations:\n\n**The costs are *NOT* real, they are *assumed* by definition of the Dragon book.** To get informed about the *real* costs, you have to check the tables of the target architecture. See the SO wiki for the corresponding links.\n\nSo coming to the three examples by relating to your quote above:\n\n we take the cost of an in­struction to be one\n\nTherefore every instruction is assumed to have *a cost of one*!\n\n those in­volving a memory location or constant in them have an additional cost of one\n\nSo the examples 1 to 3 have\n\n- 1 instruction + 0 mem/const = 1\n\n- 1 instruction + 1 mem = 2\n\n- 1 instruction + 1 mem + 1 one const = 3\n\nThe memory addresses and the constants are encoded in the instruction OpCode separately in the additional ***words of the instruction*** prolonging the, to be fetched, OpCode bytes affecting the CPU Op/microOp processing.\n\n========================================\n\nCode:\n```text\nLD R1,*100(R2)\n```\n\n```text\nLD\n```\n\n```text\nR1\n```\n\n```text\nR2\n```\n\n```text\n100\n```\n\n```text\nLD R1, *R2\n```\n\n```text\nBLTZ *R3, R0\n```\n\n```text\n*R3\n```\n\n========================================\n\nComments:\n- In my copy of the book, the cost of `LD R1, *100(R2)` is given as two: \"The cost is two because the constant 100 is stored in the word following the instructions\".\n- @chill I rechecked, it's 3 in my book - 2nd edition.\n- +1, the cost model suggested in the question is nothing like the cost model on x86 (a popular variable-length instruction set), where short instructions are nice, but things like uop caches often avoid bottlenecks on the front-end. See also agner.org/optimize, and other links in stackoverflow.com/tags/x86/info. ARM thumb2 is another very widely used instruction set, but any given instruction has fixed length, and can't be longer to accommodate a longer addressing mode the way x86 can.\n- But integer multiply throughput is typical 1 per clock, with 3 cycle latency on Ryzen and Sandybridge. AFAIK, no x86 CPUs have better than 1 per clock throughput for scalar integer multiply. (But Skylake has 2-per-clock vector integer multiply for some element widths.) For FP mul, Haswell and later have 2-per-clock multiply and FMA, with 5 or 4 cycle latency. (For vectors up to 256-bits: 4 `double`s or 8 `float` per vector, or for scalar).","metadata":{"transformedAt":"2026-08-18T18:32:17.836Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":102,"estimatedTokens":1090}}856{"id":"stack-33351626","source":"stackoverflow","questionId":33351626,"title":"Can I make my own JIT\\Interpreter\\Compiler for C# and use it in Visual Studio?","tags":["c#","c++","visual-studio","compiler-construction","visual-studio-debugging"],"text":"Title: Can I make my own JIT\\Interpreter\\Compiler for C# and use it in Visual Studio?\nTags: c#, c++, visual-studio, compiler-construction, visual-studio-debugging\nSource: Stack Overflow\n\nQuestion:\nI am currently writing a compiler that produces JIT-like EXE from C# (rewrites itself), is there anyway to make Visual Studio and it's debugger recognize the way I want to build it (with my compiler) and debug the output?\n\nMy compiler's output is EXE, but it doesn't contain MSIL, it contains my intermediate language, and the rest of the content is the JIT written in C++. (The C++ reads itself\\EXE and executes)\n\nI am well-aware it's not magic to make it compatible; I am here to get a straight answer whatever it's possible and hints to start; e.g. write a C++ DLL with such functions and parameters and give the DLL path to Visual Studio's debugger parameters, or just point me to reference at MSDN. (I got zero promising results from Google)\n\n========================================\n\nCode:\n```text\nLanguage Services\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.836Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":17,"estimatedTokens":256}}857{"id":"stack-8713613","source":"stackoverflow","questionId":8713613,"title":"Semantic analysis in compilers","tags":["compiler-construction","semantics","compiler-theory","semantic-analysis"],"text":"Title: Semantic analysis in compilers\nTags: compiler-construction, semantics, compiler-theory, semantic-analysis\nSource: Stack Overflow\n\nQuestion:\nHow is the semantic analysis done by a compiler (generally)?\n\nI had to answer to this question during my last exam, it wasn't enough for the professor.\n\nI included BNF (with an example) and syntactic cards in my answer, to which he asked me: \"What happens when the compiler finds a statement like `int i;`?\"\n\n========================================\n\nCode:\n```text\nint i;\n```\n\n```text\n\"abc\" * MyClass\n```\n\n========================================\n\nComments:\n- Didn't he cover this material in your class?\n- @IraBaxter: Yes, but superficially than what he asked during the exam. (bad bad english, sorry)\n- This is more like a \"human issue\" that a \"technical issue\", sometimes teachers expect an answer to be much specific to what they speak in class ...\n- It is reasonable for a teacher to sketch a high level, coherent overview of a topic in a class, and then insist that the students read the associated material for details. If they ask you a question on an exam that is in assigned reading material you better know that material. There are also just plain bad teachers.\n- @umlcat: Yes :/ When it happens you feel like: \"WTF I studied in the last two weeks?!\"\n- @IraBaxter: Of course. But when you a course of study with a professor (a good professor), and then you have to do the exam with a different teacher, it's a bit difficult to be prepared on all possible thing that he could ask you. :/\n- @uNaturhal,@Atwood: I don't kwow why Atwood closed this question. I thought it was perfectly clear, and others seemed to think so to based on my response. Sometimes the SO staff goes overboard, and it often happens with relatively new users, which I think is a disservice. The discussion on teaching here is a bit off topic, but that might be a complaint about the comments, not the question.\n- @IraBaxter, Atwood: \"This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form.\" Atwood, are you sure to know what does it mean what you wrote? I think that Ira Baxter has understood my question, in fact his answer is exactly what I needed. So it doesn't seems that it's difficult to ask. It's a pity that no one else can contribute to the post, but I had my answer so it's no longer my problem. Have fun!\n- I think to have understood. Could you just give me some example please? For instance, when a compiler found a declaration in a function (like `long double f;`), it puts in a table that `f` is a symbol, that occupy 8bytes, that can contain float numbers, that is defined only in the scope of the function, and his range of values?\n- @unNatural: Yes, you have it right. The key is the association between the scope, the identifer, and the *type* of identifier. Other data (such as float takes 8 bytes) don't necessarily have to be in the symbol table, because that fact is likely true across the compiler for any float value. It is useful for later stages of the compiler to capture range information if it can determine it, and this is arguably a semantic analysis, but most people don't think of range analysis as \"compiler semantic analysis\" in the narrow range of this discussion.\n- Ok, it's clear :) Excuse me if I abuse of your availability, but I have another question. According to what you said, it seems that the BNF has nothing to do with the process of semantic analysis. It's right, or I'm wrong?\n- @unNatural: The BNF has everything to do with semantic analysis. The BNF tells you the *shape* of language constructs. This in turn defines the structure of the langauge, thus the declarations, scopes, expressions, you name it. You will find that the program representation (e.g., the syntax tree) and the \"visit\" to all of its elements are directly controlled by the BNF. In fact, entire compilers have been written using the BNF to *directly* control the parsing and semantic analysis, that is, the BNF plus some additional annotations are used to generate the compiler...\n- @unNatural: ... in fact, the tool which process such annotate BNF has exactly the same problem as the target compiler has, e.g., must parse, do semantic analysis of the BNF/annotations, etc. thus are called \"meta compilers\". The reason I bring this up is because syntactic and semantic analysis are modelled directly in such compiler compiler languages. If you understand how these work, the details of compiling become much clearer. See en.wikipedia.org/wiki/Compiler-compiler But you should still read the Aho&Ull compiler book.\n- Maybe you will not believe me, but I'd really like to learn how to develop a compiler or a programming language. I'm attracted by low level programming! However, thank you for your time. You have been very very clear :)\n- Did you wrote this article too en.wikibooks.org/wiki/Compiler_Construction/Semantic_Analysi&zwnj;&#8203;s? both text seems to be the same\n- I didn't write the wiki entry. Looks like \"Pir Fahim Shah\" did, using my text from this answer to scribble on the existing wikipedia entry (whose quality is unknown to me). I'm not sure he did us all a favor. Technically I wrote it, but SO claims rights to it. I'll let SO and Wikipedia sort it out.\n- @IraBaxter You said \"some representation of the program is constructed (syntax trees are very popular)\". What other data structures are good candidates to represent the structure of a program? I can only think of trees.\n- \"Good\" representation depends on what questions you want to ask. Frankly, I can only think of 4 basic representations of programs: 1) Raw text. 2) Sequence of Lexemes 3) Trees 4) Data flow graphs.\n- The BNF only possesses enough information to build a parser accepting semantically-invalid nonsense like `int x = “abcd”;`. Semantic analysis is absolutely required to do type checking, identifier resolution, etc. Think of semantic actions as semantic constraints or guards applied to the parser.","metadata":{"transformedAt":"2026-08-18T18:32:17.837Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":44,"estimatedTokens":1506}}858{"id":"stack-74510664","source":"stackoverflow","questionId":74510664,"title":"Does strict typing increase Python program performance?","tags":["python","c","compiler-construction","interpreter"],"text":"Title: Does strict typing increase Python program performance?\nTags: python, c, compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nBased on questions like this What makes C faster than Python? I've learned that dynamic/static typing isn't the main reason that C is faster than Python. It appears to be largely because python programs are interpreted, and c programs are compiled.\n\nI'm wondering if strict typing would close the gap in performance for interpreted vs compiled programs enough that strict typing would be a viable strategy for improving interpreted Python program performance post facto?\n\nIf the answer is yes, is this done in pro-dev contexts?\n\n========================================\n\nComments:\n- No; everything is still dynamically typed at run-time. The static type hints are only used to verify *correctness* of the source code itself.\n- So then would static typing have the opposite effect and get in the way Python doing its thing?\n- @Mustafa, run-time type checking adds work to what Python already has to do. I wouldn't describe that as \"get[ting] in the way\", but I would expect any measurable impact on performance to be negative.\n- Type hints are *ignored* at run-time, aside from them being preserved in `__annotation__` attributes on `function` and `class` objects. If the one-time cost of creating those attributes on startup negatively impacts your performance, you probably shouldn't be using Python in the first place.\n- Thanks @chepner - docs.python.org/3/library/typing.html looks like I could've found out that hints were ignored here and saved a thread. Appreciate the help!\n- Interestingly in C a standard way to get \"performance\" is also to use libraries of preoptimised routines. That's what Intel's MKL/IPL is all about, and other examples such as FFTW, Mercury's SAL, VSIPL, etc. All the C ends up doing is marshalling calls in/out of such libraries. It's also why messing around with core affinity buys so little especially when the range of CPUs on which a binary might have to run can vary so much in built-in opcode set and core count. Us die-hard embedded high performance types have been using libraries and trusting OS schedulers to get it right for decades now :-)\n- In my field there is now a serious question as to whether or not to ditch C. So many scientific routines now are produced for Python; there's not necessarily a C version of such a library, even if the Python package had been written in C in the first place. Sure, calling in/out of packages from Python adds overhead, but not too much, and it's probably always more expedient (especially for a developer) to suffer that overhead and throw one more CPU core at the problem to compensate, if needs be. And TBH, modern CPUs are so fast that people are running out of problems that can tax them!","metadata":{"transformedAt":"2026-08-18T18:32:17.837Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":707}}859{"id":"stack-9456448","source":"stackoverflow","questionId":9456448,"title":"When to use an abstract or concrete syntax tree?","tags":["compiler-construction","abstract-syntax-tree","concrete-syntax-tree"],"text":"Title: When to use an abstract or concrete syntax tree?\nTags: compiler-construction, abstract-syntax-tree, concrete-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI've been doing research on compilers. The lexer seems to be very straight forward: Take a \"sentence\" and break it up into words (or tokens). To ensure correct grammar a parser is needed. The parser generally takes the tokens and builds a tree that results in a root node (words into sentences, paragraphs, pages, etc...).\n\nFrom this question it would seem a parser would build an AST. The AST only contains what is necessary to execute the code, so things like parentheses would be unnecessary since operator precedence is built into an AST. An AST is probably all a compiler needs.\n\nBut what about converting code from one language to another? Taking a made-up language (grammar) or an existing grammar and converting it into another where operator precedence rules may or may not be different? Is operator precedence \"built in\" to CST as well?\n\nAs an example lets say I made up a language and wanted to translate it into PHP code. The ternary operator on most languages has right-to-left associativity. PHP incorrectly uses left-to-right associativity (see more about this here). I want \"my language\" to use right-to-left but the resulting PHP code has to apply parenthesis to get the correct result in PHP (with the link to Wikipedia, result needs to be \"train\" instead of \"horse\").\n\nSo for language translation would a CST be better? Is operator precedence usually built into a CST? Is there anything in-between? Are there any examples comparing both trees with a simple algebra equation? Any examples illustrating a ternary operator?\n\n(Is \"transcode\" the correct term for \"programming language translation\"? A Google search brings up converting media.)\n\nWhat I'm trying to figure out is: When is it more appropriate to use one over the other?\n\n========================================\n\nComments:\n- I don't see why you would need the concrete syntax tree for a language-to-language translation. Concrete syntax is precisely what's most likely to differ. You want to create a program with similar *semantics* in another language, for that you need only the *semantics* of the original program, and the AST gives you just that with less clutter.\n- Ah, I see what you mean. So when would a concrete tree be used and considered more appropriate than an abstract tree, and does a concrete tree care about precedence?\n- +1 Thanks you for the amount of high-quality compiler knowledge you with your every answer, Sir.\n- Thank you for your detailed answer! This helps a lot. Syntax wise the source language is a combination of PHP and other languages (vars start with $, JSON array/object, vars must be declared, vars must be initially typed). It doesn't have all the features of PHP. Example: Dynamic function calling ($func(...)), and anonymous functions (or blocks) aren't supported. For the most part it should be a 1-to-1 translation. The challenge is \"fixing\" the ternary operator and \"+\" being both addition and concat (which I should be able to discern because of strict/semi-strict typing).","metadata":{"transformedAt":"2026-08-18T18:32:17.837Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":26,"estimatedTokens":790}}860{"id":"stack-5754265","source":"stackoverflow","questionId":5754265,"title":"Disadvantage of bootstrapping?","tags":["compiler-construction","programming-languages"],"text":"Title: Disadvantage of bootstrapping?\nTags: compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nI have seen many links on bootstrapping, I was wondering what are the main drawbacks of bootstrapping a compiler for a language say X using C programming instead of using assembly language? I was wondering if using C would restrict whatever I do to a 'C' like assembly language creation (i.e how C's compiler does it).\n\nFor example whatever I write in Python will eventually be taken care by CPython, making it eventually in a C like manner in the hardware - which might not be optimum?\n\nOf course C is probably very good language, but for some other general language it might not be so. Wouldn't bootstrapping eventually have some bottlenecks, restrictions specific to the language I use for making a bootstrapping compiler? Like machine code generation would be like how C generates the code and not some random manner.\n\nMain reason for using C is that it does a good job of mapping our code to machine language but it not be as good as assembly right? So C has some performance problems, now I use C to create a compiler for another language, so I have to pass on those performance issues right? After all C doesn't do a 1-1 mapping to assembly - hope you get my question.\n\n========================================\n\nTop Answer:\nYes. There are some cases where mapping a feature of your language onto a similar feature of C will cost you. Two examples:\n\nIf you implement a *YourLang* function call as a C function call---that is, you're naively using the C/native stack---you lose the ability to support deep recursion, proper tail recursion, continuations, and possibly stack inspection. (Or at least, you have to think harder about them.)\n\nIf you map a single *YourLang* thread to a single POSIX/Windows/whatever thread, you won't be able to support massive concurrency like Erlang.\n\nThat doesn't mean you can't write your interpreter or compiler and runtime in C, though.\n\n========================================\n\nComments:\n- The myth that \"higher level languages can't be as good as assembly\" has been thoroughly debunked for 20 years at least, maybe 30 years. Also, performance problems are likely to come from your algorithm and implementation, not from the limitations of a specific language (do you have a specific case where \"C has performance problems\", or are you just wondering what-if?).\n- I am not an expert so I am in the 'what if' category . But my doubt is whether a compiler just uses C's string parsing features and create a ASSEMBLY like mapping for the new programming language . Hence C's other inbuilt features are not even touched upon .\n- That depends on what you actually *do* in the programming language; but it is quite possible to use C as a set of macros for assembly.\n- Yes that is precisely what I was thinking. It seems the main purpose of using C as a compiler is that it has a very good set of macros for assembly and only basics of the language are required .","metadata":{"transformedAt":"2026-08-18T18:32:17.837Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":31,"estimatedTokens":756}}861{"id":"stack-704908","source":"stackoverflow","questionId":704908,"title":"Compilers and negative numbers representations","tags":["c++","c","compiler-construction","representation","negative-number"],"text":"Title: Compilers and negative numbers representations\nTags: c++, c, compiler-construction, representation, negative-number\nSource: Stack Overflow\n\nQuestion:\nRecently I was confused by this question. Maybe because I didn't read language specifications (it's my fault, I know).\n\nC99 standard doesn't say which negative numbers representation should be used by compiler. I always thought that the only right way to store negative numbers is two's complement (in most cases).\n\nSo here's my question: do you know any present-day compiler that implements by default one's complement or sign-magnitude representation? Can we change default representation with some compiler flag?\n\nWhat is the simplest way to determine which representation is used?\n\nAnd what about C++ standard?\n\n========================================\n\nTop Answer:\nWhile twos-complement representation is by far the most common, it is not the only one (see some). The C and C++ standardisation committees did not want to require non-twos-complement machines to emulate a non-native representation. Therefore neither C not C++ require a specific negative integer format.\n\nThis leads to the undefined behaviour of bitwise operations on signed types.\n\n========================================\n\nComments:\n- i guess you could determine which implementation is used by checking the binary of a signed value for its representation, similar to how endianness checks are done. this could probably be done in a #define and evaluated at compile time.\n- i mean, if you haven't solved this problem in the last 6 years. :o\n- Is one's complement a real-world issue, or just a historical one?\n- Possible duplicate of Is one's complement a real-world issue, or just a historical one?\n- Are there any C compilers for \"network accelerator\"-type CPUs?\n- @klew: Not that I could find after a few intense minutes of Googling, but that doesn't prove much. :)\n- Yes, but it's not what I am asking about. Do you know any C compiler that by default would use non-two's complemet?\n- Those for systems which use non-tows-complement... Which, apparently, includes some Unisys box which is still supported (this might be listed in the second link).","metadata":{"transformedAt":"2026-08-18T18:32:17.837Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":33,"estimatedTokens":545}}862{"id":"stack-22819687","source":"stackoverflow","questionId":22819687,"title":"Are there any supervision on implementations of C++?","tags":["c++","compiler-construction","iso"],"text":"Title: Are there any supervision on implementations of C++?\nTags: c++, compiler-construction, iso\nSource: Stack Overflow\n\nQuestion:\nC++ code standard is defined by the International Organization of Standardization (ISO). There are many different implementations of C++ like GCC/G++, libc/libc++, clang (++ too), Microsoft Visual C++, Intel C++ Compiler etc. so Anyone can make his own implementations (compiler etc, runtime library, ...) and call it C++.\n\nI wanted to know:\n\n- Are there any disciplines relating the implementations of C++?\n\n- Does any organization (like ISO) control these implementations to make sure they conform to standards?\n\n========================================\n\nComments:\n- AFAIK, there's no implementation that conforms to the standard 100%... It'd really be impossible, since the standard has bugs itself.\n- The standard is there as a guideline for vendors to meet, but nobody implements 100% of the standard, and only 100% of the standard. Most compilers have non-standard additions, and none include everything from the standard. GNU/C++, for example, hasn't fully implemented C++11 regexes (at least last time I checked). There is no enforcement; nobody has the authority to say, \"You can't call it C++ if it's not fully compliant, no more no less.\" The market tends to dictate that implementations be mostly compliant, or they simply will not be as successful as others that are more compliant.\n- @DavidO: FYI, regex is implemented in trunk and will be in GCC 4.9 (ref). (not trying to \"correct\" you or anything (because you're not wrong); just thought I'd point it out)","metadata":{"transformedAt":"2026-08-18T18:32:17.837Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":19,"estimatedTokens":401}}863{"id":"stack-55880423","source":"stackoverflow","questionId":55880423,"title":"Free Monad for AST > 1-arity?","tags":["haskell","compiler-construction","category-theory"],"text":"Title: Free Monad for AST > 1-arity?\nTags: haskell, compiler-construction, category-theory\nSource: Stack Overflow\n\nQuestion:\nWhen I'm saying `1-arity | 2-arity | n-arity`, I'm referring to tree in grap theory k-ary tree : \n\n a k-ary tree is a rooted tree in which each node has no more than k children\n\nI have been using Free Monad in my project to create a small eDSL in haskell... but all the example I have seen are only 1-ary tree (Linear AST) like this one : \n\nhttps://i.sstatic.net/abYdY.png \n\nthis datatype lift on `Free` Monad :\n\n```\ndata Toy b next =\n Output b next\n | Bell next\n | Done\n```\n\nI would like to implement a more complex eDSL than a Linear one... Is Free Monad a solution for that ? and if yes, do you have examples of Free Monad > 1-Ary ?\n\n========================================\n\nCode:\n```text\ndata Toy b next =\n    Output b next\n  | Bell next\n  | Done\n```\n\n```text\n1-arity | 2-arity | n-arity\n```\n\n```text\nFree\n```\n\n```text\ndata BinF a = Node a a\ntype Bin = Free BinF\n\nnode :: Bin a -> Bin a -> Bin a\nnode l r = Free (Node l r)\n\nexample :: Bin Int\nexample = node (node (pure 0)\n                     (pure 1))\n               (pure 2)\n{-\n  +---+---0\n   \\   \\--1\n    \\-2\n -}\n```\n\n```text\ndata BinF a = Node (Bool -> a)\n{- The product type (a, a) is isomorphic to (Bool -> a). -}\n```\n\n```text\ntype Bin = Free BinF\n\nnextBit :: Bin Bool\nnextBit = Free (Node (\\b -> Pure b))\n\nexample :: Bin Int\nexample = do\n  b1 <- nextBit\n  if b1 then do\n    b2 <- nextBit\n    if b2 then\n      pure 0\n    else\n      pure 1\n  else\n    pure 2\n```\n\n```text\nBool\n```\n\n```text\nBool\n```\n\n```text\nNode\n```\n\n========================================\n\nComments:\n- By '> 1-Arity`, what exactly do you mean? I haven't seen the word 'arity' used in this context before.\n- I have added some definition @bradrn :-) Thanks\n- Thanks! So you mean like e.g. `data Toy b next = ... | Operation next next`, where `next` appears twice?\n- yeah exactly :-)\n- I'm honestly not sure what that would do. I tried experimenting in GHCi but couldn't get the `Show` instance to work properly. Then again I've never used free monads so I'm probably not the best person to answer this question. (By the way, could you edit in an example of a 2-arity eDSL? I know I was able to figure it out but I feel it's still not too obvious from the question...)\n- Can you clarify: are you trying to figure out how to represent conditionals and loops in an eDSL expressed as a free monad?","metadata":{"transformedAt":"2026-08-18T18:32:17.837Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":9,"totalLines":105,"estimatedTokens":612}}864{"id":"stack-6518436","source":"stackoverflow","questionId":6518436,"title":"Where/how to declare the unique key of variables in a compiler written in Ocaml?","tags":["parsing","compiler-construction","syntax","ocaml"],"text":"Title: Where/how to declare the unique key of variables in a compiler written in Ocaml?\nTags: parsing, compiler-construction, syntax, ocaml\nSource: Stack Overflow\n\nQuestion:\nI am writing a compiler of mini-pascal in Ocaml. I would like my compiler to accept the following code for instance:\n\n```\nprogram test;\nvar\n a,b : boolean;\n n : integer;\nbegin\n ...\nend.\n```\n\nI have difficulties in dealing with the declaration of variables (the part following `var`). At the moment, the type of variables is defined like this in sib_syntax.ml:\n\n```\ntype s_var =\n { s_var_name: string;\n s_var_type: s_type; \n s_var_uniqueId: s_uniqueId (* key *) }\n```\n\nWhere `s_var_uniqueId` (instead of `s_var_name`) is the unique key of the variables. My first question is, where and how I could implement the mechanism of generating a new id (actually by increasing the biggest id by 1) every time I have got a new variable. I am wondering if I should implement it in sib_parser.mly, which probably involves a static variable `cur_id` and the modification of the part of `binding`, again don't know how to realize them in `.mly`. Or should I implement the mechanism at the next stage - the `interpreter.ml`? but in this case, the question is how to make the `.mly` consistent with the type `s_var`, what `s_var_uniqueId` should I provide in the part of `binding`?\n\nAnother question is about this part of `statement` in `.mly`:\n\n```\nid = IDENT COLONEQ e = expression\n { Sc_assign (Sle_var {s_var_name = id; s_var_type = St_void}, e) }\n```\n\nHere, I also need to provide the next level (the `interpreter.ml`) a variable of which I only know the `s_var_name`, so what could I do regarding its `s_var_type` and `s_var_uniqueId` here?\n\nCould anyone help? Thank you very much!\n\n========================================\n\nTop Answer:\nHow to create a global id generator:\n\n```\nlet unique =\n let counter = ref (-1) in\n fun () -> incr counter; !counter\n```\n\nTest:\n\n```\n# unique ();;\n- : int = 0\n# unique ();;\n- : int = 1\n```\n\nRegarding your more general design question: it seems that your data representation does not faithfully represent the compiler phases. If you must return a type-aware data-type (with this field `s_var_type`) after the parsing phase, something is wrong. You have two choices:\n\ndevise a more precise data representation for the post-parsing AST, that would be different from the post-typing AST, and not have those `s_var_type` fields. Typing would then be a conversion from the untyped to the typed AST. This is a clean solution that I would recommend.\n\nadmit that you must break the data representation semantics because you don't have enough information at this stage, and try to be at peace with the idea of returning garbage such as `St_void` after the parsing phase, to reconstruct the correct information later. This is less typed (as you have an implicit assumption on your data which is not apparent in the type), more pragmatic, ugly but sometimes necessary. I don't think it's the right decision in this case, but you will encounter situation where it's better to be a bit less typed.\n\nI think the specific choice of unique id handling design depends on your position on this more general question, and your concrete decisions about types. If you choose a finer-typed representation of post-parsing AST, it's your choice to decide whether to include unique ids or not (I would, because generating a unique ID is dead simple and doesn't need a separate pass, and I would rather slightly complexify the grammar productions than the typing phase). If you choose to hack the type field with a dummy value, it's also reasonable to do that for variable ids if you wish to, putting `0` as a dummy value and defining it later; but still I personally would do that in the parsing phase.\n\n========================================\n\nCode:\n```text\nprogram test;\nvar\n   a,b : boolean;\n   n : integer;\nbegin\n   ...\nend.\n```\n\n```text\ntype s_var =\n    { s_var_name: string;\n      s_var_type: s_type; \n      s_var_uniqueId: s_uniqueId (* key *) }\n```\n\n```text\nid = IDENT COLONEQ e = expression\n  { Sc_assign (Sle_var {s_var_name = id; s_var_type = St_void}, e) }\n```\n\n```text\nvar\n```\n\n```text\ns_var_uniqueId\n```\n\n```text\ns_var_name\n```\n\n```text\ncur_id\n```\n\n```text\nbinding\n```\n\n```text\n.mly\n```\n\n```text\ninterpreter.ml\n```\n\n```text\n.mly\n```\n\n```text\ns_var\n```\n\n```text\ns_var_uniqueId\n```\n\n```text\nbinding\n```\n\n```text\nstatement\n```\n\n```text\n.mly\n```\n\n```text\ninterpreter.ml\n```\n\n```text\ns_var_name\n```\n\n```text\ns_var_type\n```\n\n```text\ns_var_uniqueId\n```\n\n```text\nlet unique = \n  let last = ref 0 in \n  fun () -> incr last ; !last\n\nlet one = unique ()  (* 1 *)\nlet two = unique ()  (* 2 *)\n```\n\n```text\ntype s_context = {\n  s_ctx_parent : s_context option ;\n  s_ctx_bindings : (string * (int * s_type)) list ;\n  s_ctx_size : int ;\n}\n\nlet empty_context parent = {\n  s_ctx_parent = parent ;\n  s_ctx_bindings = [] ;\n  s_ctx_size = 0\n}\n\nlet bind v_name v_type ctx = \n  try let _ = List.assoc ctx.s_ctx_bindings v_name in\n      failwith \"Variable is already defined\"\n  with Not_found -> \n    { ctx with \n      s_ctx_bindings = (v_name, (ctx.s_ctx_size, v_type)) \n        :: ctx.s_ctx_bindings ;\n      s_ctx_size = ctx.s_ctx_size + 1 }\n\nlet rec find v_name ctx =       \n  try 0, List.assoc ctx.s_ctx_bindings v_name\n  with Not_found -> \n    match ctx.s_ctx_parent with \n      | Some parent -> let depth, found = find v_name parent in\n                       depth + 1, found\n      | None -> failwith \"Variable is not defined\"\n```\n\n```text\ntype s_var = {\n  s_var_deBruijn: int;\n  s_var_type: s_type;\n  s_var_pos: int \n}\n\nlet find v_name ctx = \n   let deBruijn, (pos, typ) = find v_name ctx in \n   { s_var_deBruijn = deBruijn ;\n     s_var_type = typ ;\n     s_var_pos = pos }\n```\n\n```text\ntype s_fun =\n{ s_fun_name: string;\n  s_fun_type: s_type;\n  s_fun_params: context; \n  s_fun_body: s_block; }\n\nlet context_of_paramlist parent paramlist = \n  List.fold_left \n    (fun ctx (v_name,v_type) -> bind v_name v_type ctx) \n    (empty_context parent)\n    paramlist\n```\n\n```text\nint_expression:\n  (* Constant : ignore the context *)\n| c = INT { fun _ -> Se_const (Sc_int c) }\n  (* Variable : look for the variable inside the contex *)\n| id = IDENT { fun ctx -> Se_var (find id ctx) }\n  (* Subexpressions : pass the context to both *)\n| e1 = int_expression o = operator e2 = int_expression \n  { fun ctx -> Se_binary (o, e1 ctx, e2 ctx) }\n;\n```\n\n```text\n| function_definition_expression (args, body) \n  { fun ctx -> let ctx = context_of_paramlist (Some ctx) args in\n               { s_fun_params = ctx ; \n                 s_fun_body = body ctx } }\n```\n\n```text\nprog:\n  PROGRAM IDENT SEMICOLON\n  globals = variables\n  main = block\n  DOT\n    { let ctx = context_of_paramlist None globals in \n      { globals = ctx;\n        main = main ctx } }\n```\n\n```text\ntype stack = value array list\n```\n\n```text\nlet read stack x = \n  (List.nth stack x.s_var_deBruijn).(x.s_var_pos)\n\nlet write stack x value = \n  (List.nth stack x.s_var_deBruijn).(x.s_var_pos) <- value\n```\n\n```text\nlet inner_stack = args :: stack in\n(* Evaluate f.s_fun_body with inner_stack here *)\n```\n\n```text\n{ ... ; s_var_uniqueId = unique () }\n```\n\n```text\nx\n```\n\n```text\nList.assoc\n```\n\n```text\nbind\n```\n\n```text\nfind\n```\n\n```text\nfind 'x' ctx\n```\n\n```text\n0, (3, St_int)\n```\n\n```text\n0\n```\n\n```text\n3\n```\n\n```text\nSt_int\n```\n\n```text\nblock\n```\n\n```text\nvalue\n```\n\n```text\nx\n```\n\n```text\nf\n```\n\n```text\nargs\n```\n\n```text\nlet unique =\n  let counter = ref (-1) in\n  fun () -> incr counter; !counter\n```\n\n```text\n# unique ();;\n- : int = 0\n# unique ();;\n- : int = 1\n```\n\n```text\ns_var_type\n```\n\n```text\ns_var_type\n```\n\n```text\nSt_void\n```\n\n```text\n0\n```\n\n========================================\n\nComments:\n- Thank you for this complete introduction of a way of variable binding... D'ailleurs, ce sont les questions qui viennent de mon stage MPRI :-)\n- Don't they teach you DeBruijn indices in the Logique course at the MPRI anymore ? Tsss ;-)","metadata":{"transformedAt":"2026-08-18T18:32:17.837Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":51,"totalLines":369,"estimatedTokens":1990}}865{"id":"stack-10232871","source":"stackoverflow","questionId":10232871,"title":"Reverse engineering C++","tags":["c++","compiler-construction","reverse-engineering","ida"],"text":"Title: Reverse engineering C++\nTags: c++, compiler-construction, reverse-engineering, ida\nSource: Stack Overflow\n\nQuestion:\nToday I decided to decompile a simple \"Hello world\" program written in visual C++, using IDA Pro.\n\nWith my previous knowledge I was sure I would not find the immediate call to printf at the executable entry point, and I was right.\nI found a lot of code that was not written by me and added by the compiler during the compilation process.\n\nI would like a better understanding of what code is added during the compilation process.\nWhat does it do?\nAre there any \"tricks\" to quickly find \"main\" and skip all unnecessary code generated by disassembly?\n\nThe best I could find was in this post :\nhttp://www.codeproject.com/Articles/4210/C-Reverse-Disassembly,\nsaying the execution order of an executable compiled using visual c++ is as follows:\n\nCrtlStartUp\n\nmain\n\nCrtlCleanUp\n\nCould I please get a more detailed answer?\n\n========================================\n\nTop Answer:\nToday's compilers create massive executables so even if you'll find entrypoint it will take you a while to understand and get to the section you actually need. \n\nIn your case with hello world app you can use IDA find entry point in function list dialog (I don't remember exact name). But again I don't recommend this approach, unless app is very small. \n\nThe approach I'm using I call \"down to top approach\" (c)\n\nI would start from analysing current behaviour of the application without involving any tools. It's very important step that will save lot of time as you'll know what are you looking for and when it happens. Then determine \"weak points\" like strings, constant values that you can find with static analysis tools (IDA). \n\nNext step is to disassemble the app and look for those \"weak points\" (strings module in IDA) then find references to them from what functions they used (you can use graphics hierarchy view in new IDA versions)\n\nIf you still cant get how it's working or this code called from many places you don't know which you need. You can start with runtime analysis and use debugger (softice? :)) like ollydbg. This will show you things that not visible with static analysis like virtual functions / function pointers for example: call EAX. \n\nThen you just processing step by step till you get what you need.\n\n========================================\n\nCode:\n```text\natexit\n```\n\n========================================\n\nComments:\n- Very compiler and platform specific. I doubt you'll get the exact answer you want.\n- I recommend this post as a roadmap to any reverse-engineer-guru-wannabe person.\n- I have no experience in reverse engineering, but couldn't you simply set a debugger break point at the start of main to get the relative address? Or, alternatively, look up main in an object dump of the executable?\n- Reverse engineering in C++? Microsoft made c++ virtual machine or what? You can reverse-engineer from platform's native code, the tool used to generate the code doesn't matter.","metadata":{"transformedAt":"2026-08-18T18:32:17.837Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":57,"estimatedTokens":752}}866{"id":"stack-47240899","source":"stackoverflow","questionId":47240899,"title":"How to create branchless code for this piece of code?","tags":["compiler-construction","computer-science","compiler-optimization","cpu-architecture","micro-optimization"],"text":"Title: How to create branchless code for this piece of code?\nTags: compiler-construction, computer-science, compiler-optimization, cpu-architecture, micro-optimization\nSource: Stack Overflow\n\nQuestion:\nI need to generate branchless code for the if statement in the inner loop \nif(i != j). I am confused how to generate branchless code.\n\n```\nfor (int i = start; i < n; i++)\n {\n results[i] = 999;\n for (int j = 0; j < n; j++)\n {\n if (i != j)\n {\n d = myfunction(x, y, i, j);\n if (d < results[i])\n results[i] = d;\n }\n }\n }\n```\n\n========================================\n\nTop Answer:\nIf I understand correctly, you need to operate on an `n` by `n` matrix, but excluding the values in a diagonal, i.e:\n\n```\nX O O O\nO X O O\nO O X O\nO O O X\n```\n\nYou could reframe the problem by \"unrolling\" the matrix like so:\n\n```\n. O O O\n. . O O O\n. . . O O O\n. . . . O O O\n```\n\nThen you could correct `j` in the inner loop:\n\n```\nfor (int i = 0; i < n; i++) {\n // ...\n for (int j = i + 1; j < i + n; j++) {\n d = myfunction(x, y, i, j % n);\n // ...\n }\n}\n```\n\n========================================\n\nCode:\n```text\nfor (int i = start; i < n; i++)\n  {\n        results[i] = 999;\n        for (int j = 0; j < n; j++)\n        {\n            if (i != j)\n            {\n                d = myfunction(x, y, i, j);\n                if (d < results[i])\n                    results[i] = d;\n            }\n        }\n    }\n```\n\n```text\nint condition = d < results[i] \nresults[i] = d * condition + results[i] * !condition;\n```\n\n```text\n...\nfor (int j = 0; j < n - 1; j++) {\n    d = myfunction(x, y, i, j + (j >= i));\n    int condition = d < results[i] \n    results[i] = d * condition + results[i] * !condition;\n}\n...\n```\n\n```text\nfor (int j = 0; j < i; j++) {\n   ...\n}\nfor (int j = i + i; j < n; j++) {\n   ...\n}\n```\n\n```text\nX O O O\nO X O O\nO O X O\nO O O X\n```\n\n```text\n. O O O\n. . O O O\n. . . O O O\n. . . . O O O\n```\n\n```text\nfor (int i = 0; i < n; i++) {\n    // ...\n    for (int j = i + 1; j < i + n; j++) {\n        d = myfunction(x, y, i, j % n);\n        // ...\n    }\n}\n```\n\n```text\nn\n```\n\n```text\nn\n```\n\n```text\nj\n```\n\n========================================\n\nComments:\n- `j%n` is branchless, but slow unless the compiler transforms it into a compare/cmov instead of an actual division. (Or if `n` is a compile-time constant, it's not bad.) In the source, you could do `jmod++; jmod = (jmod == n) ? 0 : jmod;`\n- @PeterCordes: good point, incidentally, since j is bounded, we can just replace `j%n` with `j-n` to achieve the same result :D (answer updated)\n- Wont this make the argument negative for j < n?\n- Oops. Yes. this is what I get for writing the code in Python (which is happy to accept negative indices).\n- in this case j=i+1, j will never be zero?\n- I wanted to start j from zero every time the inner loop starts, but skip when i==j\n- Why is it important that `j` starts at zero?\n- because I have two one-D arrays, one array has x, other has y values from which I am calculating distance. it is necessary to start from zero every time except i==j\n- While it is true that `j` does not start at 0, `j % n` does “start at 0”; this code is functionally equivalent to the `if (i!=j)` case.\n- The program will crash, suppose I have an array of size=5, when i=4, j=4 then j+=(i==j); make j=5 this exceed index of the array.\n- How / why will it crash?\n- The loop condition is a branch, so splitting up the inner loop does reduce branching compared to putting an extra branch inside the loop body. But it's not exactly branchless. Some kinds of branch-prediction hardware might have predicted the loop-termination for many repeats of an `n` iteration loop, but less well for different lengths. So it may be a wash for mispredicts, but still a win for instruction count / throughput. (For real use I'd definitely recommend loop splitting, especially for large `n`. The branchless way is adding a lot of work for a rare condition)\n- Ooops, have adjusted the loop condition to account for that\n- p.s. got rid of the extra assignment to j\n- NP. If this solves your question, you may want to accept the answer (set the checkmark), so this issue gets closed O:)","metadata":{"transformedAt":"2026-08-18T18:32:17.837Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":154,"estimatedTokens":1030}}867{"id":"stack-13124501","source":"stackoverflow","questionId":13124501,"title":"Why does the VC++ compiler MOV+PUSH args instead of just PUSH them? x86","tags":["visual-c++","assembly","compiler-construction","x86"],"text":"Title: Why does the VC++ compiler MOV+PUSH args instead of just PUSH them? x86\nTags: visual-c++, assembly, compiler-construction, x86\nSource: Stack Overflow\n\nQuestion:\nIn this disassembly from VC++ a function call is being made. The compiler MOVs the local pointers to a register before pushing them:\n\n```\nmemcpy( nodeNewLocation, pNode, sizeCurrentNode );\n0041A5DA 8B 45 F8 mov eax,dword ptr [ebp-8] \n0041A5DD 50 push eax \n0041A5DE 8B 4D 0C mov ecx,dword ptr [ebp+0Ch] \n0041A5E1 51 push ecx \n0041A5E2 8B 55 D4 mov edx,dword ptr [ebp-2Ch] \n0041A5E5 52 push edx \n0041A5E6 E8 67 92 FF FF call 00413852 \n0041A5EB 83 C4 0C add esp,0Ch\n```\n\nWhy not just push them directly? ie\n\n```\npush dword ptr [ebp-8]\n```\n\nAlso, if you are going to do a separate push, why not do it manually. In other words, instead of doing \"push eax\" above, do\n\n```\nmov [esp], eax\n```\n\nEtc. the advantage of this is that after doing the 3 movs you can do a single subtract to set the new stack pointer, instead of implicitly subtracting three times with the pushes.\n\nUPDATE---Release version\n\nThis is the same code compiled for release:\n\n```\n; 741 : memcpy( nodeNewLocation, pNode, sizeCurrentNode );\n\n 00087 8b 45 f8 mov eax, DWORD PTR _sizeCurrentNode$[ebp]\n 0008a 8b 7b 04 mov edi, DWORD PTR [ebx+4]\n 0008d 50 push eax\n 0008e 56 push esi\n 0008f 57 push edi\n 00090 e8 00 00 00 00 call _memcpy\n 00095 83 c4 0c add esp, 12 ; 0000000cH\n```\n\nDefinitely more efficient than the debug version, but it is still doing a MOV/PUSH combo.\n\n========================================\n\nTop Answer:\nThis is an optimization. It is explicitly mentioned in the Intel processor manuals, volume 4, section 12.3.3.6:\n\n In Intel Atom microarchitecture, using PUSH/POP instructions to manage stack space\n and address adjustment between function calls/returns will be more optimal than\n using ENTER/LEAVE alternatives. This is because PUSH/POP will not need MSROM\n flows and stack pointer address update is done at AGU.\n When a callee function need to return to the caller, the callee could issue POP instruction\n to restore data and restore the stack pointer from the EBP.\n\n \n Assembly/Compiler Coding Rule 19. (MH impact, M generality) For Intel\n Atom processors, **favor register form of PUSH/POP** and avoid using LEAVE; Use LEA\n to adjust ESP instead of ADD/SUB.\n\nThe rest of the manual isn't that clear about the reason, but it does mention a possible 3 cycle AGU stall on implicit ESP adjustments.\n\n========================================\n\nCode:\n```text\nmemcpy( nodeNewLocation, pNode, sizeCurrentNode );\n0041A5DA 8B 45 F8             mov         eax,dword ptr [ebp-8]  \n0041A5DD 50                   push        eax  \n0041A5DE 8B 4D 0C             mov         ecx,dword ptr [ebp+0Ch]  \n0041A5E1 51                   push        ecx  \n0041A5E2 8B 55 D4             mov         edx,dword ptr [ebp-2Ch]  \n0041A5E5 52                   push        edx  \n0041A5E6 E8 67 92 FF FF       call        00413852  \n0041A5EB 83 C4 0C             add         esp,0Ch\n```\n\n```text\npush  dword ptr [ebp-8]\n```\n\n```text\nmov [esp], eax\n```\n\n```text\n; 741  :    memcpy( nodeNewLocation, pNode, sizeCurrentNode );\n\n  00087 8b 45 f8     mov     eax, DWORD PTR _sizeCurrentNode$[ebp]\n  0008a 8b 7b 04     mov     edi, DWORD PTR [ebx+4]\n  0008d 50       push    eax\n  0008e 56       push    esi\n  0008f 57       push    edi\n  00090 e8 00 00 00 00   call    _memcpy\n  00095 83 c4 0c     add     esp, 12            ; 0000000cH\n```\n\n```text\nmov eax, [indirect]\nmov esi, [indirect]\npush eax\npush esi\n```\n\n```text\npush ebx          ; mthd\n push dword ptr [ebp+place+4]\n push dword ptr [ebp+place] ; pos\n push [ebp+filedes]   ; fh\n call __lseeki64_nolock\n```\n\n```text\nesi\n```\n\n```text\nesp\n```\n\n```text\n\"push eax\"\n```\n\n```text\n\"mov [esp-8], eax\"\n```\n\n```text\nmov\n```\n\n```text\npush\n```\n\n```text\n-maccumulate-outgoing-args\n```\n\n========================================\n\nComments:\n- Is that actually compiled in release mode? It looks vaguely debuggish\n- It is compiled for debug. Why would that make a difference in this case?\n- Because the compiler is not going to care about such things in debug mode.\n- In your final example, is it safe to leave the stack temporarily unbalanced by deferring the sub? I know that would be bad news in real mode (interrupt \"borrows\" part of your stack at an inopportune time), but I am not certain in protected mode.\n- By decoupling the instructions, you reduce the number of register stalls.\n- I show the release build ASM, which has similar MOVs. In looking through the code for the project, the compiler never does an indirect push, only a MOV. My suspicion is that this may be to support a very old version of the x86 like an 286 or 386 or something.\n- @TylerDurden: Every x86 family member with 32-bit registers has also had \"PUSH memory\" instructions. But they are often slower to execute than a MOV-PUSH combination because modern microarchitectures don't really like instructions with more than one memory access.\n- This is not really an Atom issue. I am using VS 10, the compiler for which was written before the Atom existed. Also, Atom is mostly a low-power architecture for notebooks and such.\n- It is a micro-architecture issue, Atom is just one chip of many that implements it.\n- They actually wouldn't get paired here, because they had to be adjacent back in the PMMX. I would also be very surprised if MSVC is still optimizing for PMMX..","metadata":{"transformedAt":"2026-08-18T18:32:17.837Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":161,"estimatedTokens":1353}}868{"id":"stack-30312976","source":"stackoverflow","questionId":30312976,"title":"ANTLR 4 Parser Grammar","tags":["parsing","compiler-construction","antlr","antlr4"],"text":"Title: ANTLR 4 Parser Grammar\nTags: parsing, compiler-construction, antlr, antlr4\nSource: Stack Overflow\n\nQuestion:\nHow can I improve my parser grammar so that instead of creating an AST that contains couple of `decFunc` rules for my testing code. It will create only one and `sum` becomes the second root. I tried to solve this problem using multiple different ways but I always get a left recursive error.\nThis is my testing code : \n\n```\nf :: [Int] -> [Int] -> [Int]\nf x y = zipWith (sum) x y\nsum :: [Int] -> [Int]\nsum a = foldr(+) a\n```\n\nThis is my grammar: \nThis is the image that has two `decFunc`in this link \nhttp://postimg.org/image/w5goph9b7/\n\n```\nprog : stat+;\n\nstat : decFunc | impFunc ;\n\ndecFunc : ID '::' formalType ( ARROW formalType )* NL impFunc\n ;\n\nanotherFunc : ID+;\n\nformalType : 'Int' | '[' formalType ']' ;\n\nimpFunc : ID+ '=' hr NL\n\n ;\n\nhr : 'map' '(' ID* ')' ID*\n | 'zipWith' '(' ('*' |'/' |'+' |'-') ')' ID+ | 'zipWith' '(' anotherFunc ')' ID+\n | 'foldr' '(' ('*' |'/' |'+' |'-') ')' ID+\n | hr op=('*'| '/' | '.&.' | 'xor' ) hr | DIGIT\n | 'shiftL' hr hr | 'shiftR' hr hr\n | hr op=('+'| '-') hr | DIGIT\n | '(' hr ')'\n | ID '(' ID* ')'\n | ID\n ;\n```\n\n========================================\n\nCode:\n```text\nf :: [Int] -> [Int] -> [Int]\nf x y = zipWith (sum) x y\nsum :: [Int] -> [Int]\nsum a = foldr(+) a\n```\n\n```text\nprog        : stat+;\n\nstat        : decFunc  | impFunc ;\n\n\ndecFunc     : ID '::' formalType ( ARROW formalType )* NL impFunc\n            ;\n\nanotherFunc : ID+;\n\n\nformalType  : 'Int' | '[' formalType ']' ;\n\n\nimpFunc     : ID+ '=' hr NL\n\n            ;\n\n\nhr          : 'map' '(' ID* ')'  ID*\n                | 'zipWith' '(' ('*' |'/' |'+' |'-') ')' ID+ | 'zipWith' '(' anotherFunc ')' ID+\n                | 'foldr'   '(' ('*' |'/' |'+' |'-') ')' ID+\n                | hr  op=('*'| '/' | '.&.' | 'xor' ) hr | DIGIT\n                | 'shiftL' hr hr | 'shiftR' hr hr\n                | hr  op=('+'| '-') hr | DIGIT\n                | '(' hr ')'\n                | ID '(' ID* ')'\n                | ID\n                ;\n```\n\n```text\ndecFunc\n```\n\n```text\nsum\n```\n\n```text\ndecFunc\n```\n\n```text\ndecFunc\n```\n\n```text\ndeFunc\n```\n\n```text\nf\n```\n\n```text\nsum\n```\n\n```text\nf\n```\n\n```text\nsum\n```\n\n========================================\n\nComments:\n- What is the desired output? It doesn't make much sense to say \"creating one instead of two `decFunc`\". Try creating an AST similar to what you want.\n- @Mephy I improved my question. I am basically trying to make sum refer to another stat that can be a declaration or implementation. However sum is only the first word of a declaration or implementation How can I do that? this is what I am asking for. Some suggest lookahead. But I don't know how to do it\n- Still not clear what you are asking. Your test input contains two instances of content that will match the `decFunc` rule. The generated **parse-tree** shows exactly that: two sub-trees, each having a `deFunc` instance as the root. Antlr v4 will not produce a true AST where `f` and `sum` are the roots of separate sub-trees, if that is what you are looking for.\n- @GRosenberg Thank you for your answer. Is there any thing can I do with the grammar to make both f and sum roots.\n- Thank you for your precious time and your answer. It helped me to recognize that if I am going to continue with ANTLR4, I should accept what the AST as it is. I would like to ask you which one do you prefer for implementing a translator a Listener or a Visitor. The translate will generate another code no calculation required.\n- The only practical difference between the visitor and listener is that the visitor passes a return object, the listener nothing. In some cases, the return object can be useful. Really up to you to decide if it helps in your specific design.","metadata":{"transformedAt":"2026-08-18T18:32:17.837Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":11,"totalLines":134,"estimatedTokens":944}}869{"id":"stack-20435282","source":"stackoverflow","questionId":20435282,"title":"Standard ML Proof of soundness?","tags":["c","compiler-construction","sml","ml"],"text":"Title: Standard ML Proof of soundness?\nTags: c, compiler-construction, sml, ml\nSource: Stack Overflow\n\nQuestion:\nRegarding the Standard ML compiler, my question is,even though ML itself is formally defined making it possible to prove deterministic evaluations of programs, isnt the compiler itself written in C, which is not formally defined, at least not all of it? I guess my question is say we write a program in Standard ML and can prove its correctness, how do we know the C written compiler is not performing in a way that could possibly alter the results?\n\nThanks\n\n========================================\n\nTop Answer:\nJust because you *can* write bad C programs doesn't mean you *have* to. It's perfectly possible to write a correct C program and deduce from the C language specification that the program performs correctly.\n\n========================================\n\nComments:\n- Note that most ML compilers are *not* written in C, except some small parts of their runtime systems. Most (all?) of them are actually bootstrapped in ML. Nevertheless, a good question.\n- Bugs can appear anywhere in any language. What if formal verifier has a bug? What if the compiler which compiled the formal verifier introduced a bug into it? It's ultimately hard to be mathematically certain about the behavior of complex systems. All of this is implemented using electronics vulnerable to cosmic rays, manufacturing defects, and good ol' quantum uncertainty.\n- @Potatoswatter: “What if formal verifier has a bug?”: that's a good question, ant that's to answer this there are provers relying on small trustable kernel (the smallest, the more trustable), like LCF, Isabelle, and some others LCF variants. All higher level proof must obviously pass though this small kernel and never bypass it.\n- There is a formally verified implementation of SML called CakeML, check out cakeml.org\n- I like this answer it makes complete sense, but I guess my question is all of the theorem proving software that runs with SML under the hood, isnt that all depending on the correctness of the C compiler it is using? So sure the SML used to prove something may be theoretically sound, but we are relying on C to compile successfully which cant be proven correct.\n- @SJP You will like this blog post then, if you haven't seen it already: blog.regehr.org/archives/903 . But we do not need absolute confidence in our programs anyway, because there is no machine we have absolute confidence in to execute them on. It only takes one alpha particle to change a bit in a computation. Formal methods of all kinds provide confidence way, way above traditional techniques. The problem that people who try to use them see is not so much the possibility of a compiler bug making the verification system unsound, but of the cost, and this is what we should focus on first\n- Additionally, there exist at least one formally proved C compiler, which is CompCert.","metadata":{"transformedAt":"2026-08-18T18:32:17.837Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":731}}870{"id":"stack-39777118","source":"stackoverflow","questionId":39777118,"title":"Data structure for program scope?","tags":["data-structures","tree","compiler-construction"],"text":"Title: Data structure for program scope?\nTags: data-structures, tree, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm trying to parse through an AST of a program for a made up language, to be specific I'm trying to emulate the scope, so you enter a function for example and you push a new scope, and when the function is finished being visited by the visitor, it pops the scope. One important aspect is that when we push a new scope, there is a pointer `currentScope` that is set, which points to the scope we're currently looking at. When we pop the scope, this currentScope is set to be the \"outer\":\n\n```\nclass Scope:\n outer : Scope\n inner : Scope\n```\n\nThis is going to happen in multiple passes, but the first pass it's important that it constructs the general tree of scopes.\nThe question I'm asking though is how can I traverse this tree in the same order it was created?\nFor example:\n\n```\n{ // global scope\n { // a\n { // aa\n\n }\n { // ab\n }\n }\n { // b\n }\n}\n```\n\nWhen I pass over the exact same set of nodes again, in theory they will give me the same tree of scope, but I want to preserve all of the data we collect and store each scope over each pass. In other words, when the second or third pass happens over the AST, when we visit a, currentScope = a, and when we visit aa, then currentScope = aa. Is this possible? I'm really confused with this idea, the whole recursive-y aspect is really messing with my head and I can't seem to figure out how to do this.\n\nHere's what I've tried:\n\n```\nclass Scope\n outer : Scope\n inner : Scope\n siblings : []Scope\n\n Scope(outer):\n this.outer = outer\n\npush_idx = 0\n\npush_scope()\n // set global scope\n if current is null\n global = new Scope(null)\n current = global\n return\n\n if current.inner is not null:\n // first pass over the AST\n if current_pass == 0:\n new_scope = new Scope(current)\n current.siblings.push(new_scope)\n current = new_scope\n return\n current = current.siblings[push_idx++]\n else:\n new_scope = new Scope(current)\n current.inner = new_scope\n current = current.inner\n\npop_scope()\n push_idx = 0\n current = current.outer\n```\n\nThough the order doesn't seem correct, and I'm fairly certain this is the wrong approach to this.\n\n========================================\n\nTop Answer:\n\"Scope\" is really a region of the program where all the identifiers in that region have constant meaning.\n\nIf your language has pure nested lexical scopes, you can model the set of scopes with a tree (\"spaghetti\" stack if you like), where each leaf contains a mapping from symbols introduced in that scope to their corresponding type information. This is what is classically taught in compiler classes.\n\nBut with more complex scoping rules (namespaces, using constructs, ...) in general you may need a graph whose leaves are the individual scopes with graph arcs representing relations between the scopes. Yes, one of those relations is usually \"lexical parent\". Other may include \"inherits from\", etc. You may also find that a name in a leaf mapping may by a type, it may in fact be an access path to an arbitrary other (leaf) scope in the graph.\n\n(I build generic program analysis tool infrastructure [see bio]. We defined a graph-style symbol table API to support all the different scoping rules we have encountered. An interesting class of arc is \"inherits from with priority N\" for arbitary integer N; this lets us easily model ordered multiple inheritance offered by C++).\n\n========================================\n\nCode:\n```text\nclass Scope:\n    outer : Scope\n    inner : Scope\n```\n\n```text\n{ // global scope\n    { // a\n        { // aa\n\n        }\n        { // ab\n        }\n    }\n    { // b\n    }\n}\n```\n\n```text\nclass Scope\n    outer : Scope\n    inner : Scope\n    siblings : []Scope\n\n    Scope(outer):\n        this.outer = outer\n\npush_idx = 0\n\npush_scope()\n    // set global scope\n    if current is null\n        global = new Scope(null)\n        current = global\n        return\n\n    if current.inner is not null:\n        // first pass over the AST\n        if current_pass == 0:\n            new_scope = new Scope(current)\n            current.siblings.push(new_scope)\n            current = new_scope\n            return\n        current = current.siblings[push_idx++]\n    else:\n        new_scope = new Scope(current)\n        current.inner = new_scope\n        current = current.inner\n\npop_scope()\n    push_idx = 0\n    current = current.outer\n```\n\n```text\ncurrentScope\n```\n\n========================================\n\nComments:\n- See my answer to functionally the same question here: stackoverflow.com/a/76101535/58668\n- Oh wow, I figured there was some partially obscure data structure for this. So this will work once I've created a spaghetti stack for the AST, and I can reuse it and check all the existing data stored in each scope later on?\n- Yep, that's precisely what it's designed to do. Hope this helps!\n- That's wonderful, thank you so much! I've been scratching my head over this issue for a day or so now. Can I ask if you know this from personal experience or if there's some book or resource that talks more about these mystical data structures?\n- I first heard about spaghetti stacks when I taught a compilers course a few years ago. I have some slides on scope checking that might be a good starting point.\n- Hmm, I don't know how useful would be representing the scope with a beginning and a end, since that the job the AST.","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":156,"estimatedTokens":1347}}871{"id":"stack-45522210","source":"stackoverflow","questionId":45522210,"title":"where is the `__class__` variable stored in python, or how does the compiler know where to find it","tags":["python","compiler-construction","cell","free","abstract-syntax-tree"],"text":"Title: where is the `__class__` variable stored in python, or how does the compiler know where to find it\nTags: python, compiler-construction, cell, free, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\npython relies on the `__class__` variable to be in a `cell` for a `super()` call. It gets this cell from the `free` variables in the first stack frame. \n\nThe odd thing is though that this variable isn't in `locals()`, and it is when you just reference it from the `__init__` method.\n\nTake for example this bit of code:\n\n```\nclass LogicGate:\n def __init__(self,n):\n print(locals())\n a = __class__\n print(locals())\n```\n\nWhen you disassemble this you can see it somehow knows that `print` and `locals` are globals and `__class__` is a `LOAD_DEREF`. How does the compiler know this, before running the code. `locals`, `print` and `__class__` are just variable names to the compiler as far as I know. Also this way `__class__` is all of a sudden in the `locals()` even before it's copied into `a`.\n\n```\n4 10 LOAD_DEREF 0 (__class__)\n```\n\nwhile `locals`: \n\n```\n2 LOAD_GLOBAL 1 (locals)\n```\n\nI'm asking because I'm working on skulpt a python to javascript compiler. And currently that compiler doesn't differentiate between `print` or `__class__` and attempts to get them both from the global scope. \n\nAs you can see from a printout of the ast of the above bit of code, the parser doesn't differentiate between `locals` or `__class__`:\n\n```\nModule(body=[ClassDef(name='LogicGate',\n bases=[],\n keywords=[],\n body=[FunctionDef(name='__init__',\n args=arguments(args=[arg(arg='self',\n annotation=None),\n arg(arg='n',\n annotation=None)],\n vararg=None,\n kwonlyargs=[],\n kw_defaults=[],\n kwarg=None,\n defaults=[]),\n body=[Expr(value=Call(func=Name(id='print',\n ctx=Load()),\n # here's the load for locals\n args=[Call(func=Name(id='locals',\n ctx=Load()),\n args=[],\n keywords=[])],\n keywords=[])),\n Assign(targets=[Name(id='a',\n ctx=Store())],\n # here's the load for __class__\n value=Name(id='__class__',\n ctx=Load())),\n Expr(value=Call(func=Name(id='print',\n ctx=Load()),\n args=[Call(func=Name(id='locals',\n ctx=Load()),\n args=[],\n keywords=[])],\n keywords=[]))],\n decorator_list=[],\n returns=None)],\n decorator_list=[])])\n```\n\n========================================\n\nCode:\n```text\nclass LogicGate:\n    def __init__(self,n):\n        print(locals())\n        a = __class__\n        print(locals())\n```\n\n```text\n4          10 LOAD_DEREF               0 (__class__)\n```\n\n```text\n2 LOAD_GLOBAL              1 (locals)\n```\n\n```text\nModule(body=[ClassDef(name='LogicGate',\n    bases=[],\n    keywords=[],\n    body=[FunctionDef(name='__init__',\n        args=arguments(args=[arg(arg='self',\n                                 annotation=None),\n                             arg(arg='n',\n                                  annotation=None)],\n                       vararg=None,\n                       kwonlyargs=[],\n                       kw_defaults=[],\n                       kwarg=None,\n                       defaults=[]),\n        body=[Expr(value=Call(func=Name(id='print',\n                                        ctx=Load()),\n                                              # here's the load for locals\n                              args=[Call(func=Name(id='locals',\n                                                   ctx=Load()),\n                                         args=[],\n                                         keywords=[])],\n                              keywords=[])),\n              Assign(targets=[Name(id='a',\n                                   ctx=Store())],\n                           # here's the load for __class__\n                     value=Name(id='__class__',\n                                ctx=Load())),\n              Expr(value=Call(func=Name(id='print',\n                                        ctx=Load()),\n                              args=[Call(func=Name(id='locals',\n                                                   ctx=Load()),\n                                         args=[],\n                                         keywords=[])],\n                              keywords=[]))],\n        decorator_list=[],\n        returns=None)],\n   decorator_list=[])])\n```\n\n```text\n__class__\n```\n\n```text\ncell\n```\n\n```text\nsuper()\n```\n\n```text\nfree\n```\n\n```text\nlocals()\n```\n\n```text\n__init__\n```\n\n```text\nprint\n```\n\n```text\nlocals\n```\n\n```text\n__class__\n```\n\n```text\nLOAD_DEREF\n```\n\n```text\nlocals\n```\n\n```text\nprint\n```\n\n```text\n__class__\n```\n\n```text\n__class__\n```\n\n```text\nlocals()\n```\n\n```text\na\n```\n\n```text\nlocals\n```\n\n```text\nprint\n```\n\n```text\n__class__\n```\n\n```text\nlocals\n```\n\n```text\n__class__\n```\n\n```text\n>>> class A:\n    def __init__(self):\n        super().__init__()\n\n>>> A.__init__.__code__.co_freevars\n('__class__',)\n>>> A.__init__.__closure__\n(<cell at 0x03EEFDF0: type object at 0x041613E0>,)\n>>> A.__init__.__closure__[\n        A.__init__.__code__.co_freevars.index('__class__')\n    ].cell_contents\n<class '__main__.A'>\n```\n\n```text\nclass A:\n    def greet(self, person):\n        print('hello', person)\n\ndef create_B(___person__):\n    class B(A):\n        def greet(self):\n            super().greet(___person__)\n    return B\n\nB = create_B('bob')\nB().greet() # prints hello bob\n\nassert [c.cell_contents for c in B.greet.__closure__] == ['bob', B]\n```\n\n```text\n__class__\n```\n\n```text\nsuper\n```\n\n```text\nsuper(<current class>, self)\n```\n\n```text\n__class__\n```\n\n```text\n<function>.__closure__\n```\n\n```text\n__class__\n```\n\n```text\n<function>.__code__.co_freevars\n```\n\n```text\nco_freevars\n```\n\n```text\n__closure__\n```\n\n```text\nNone\n```\n\n```text\n__class__\n```\n\n```text\n__class__\n```\n\n```text\nsuper\n```\n\n```text\nsuper = print; super()\n```\n\n```text\n__class__\n```\n\n```text\n__class__\n```\n\n```text\n__class__\n```\n\n========================================\n\nComments:\n- I found part if the issue, in python2 you can't reference `__class__` from nowhere. And it doesn't to a DEREF.\n- Are you using python 2 or python 3? The `__class__` cell is a Python 3 hack to allow `super` to be called without any args.\n- Great thanks! This definitely clears up how this all stuck together. And why all of a sudden that `__class__` var shows up. This is the PR I'm working on to add `super` to skulpt. github.com/skulpt/skulpt/pull/694 it's a python2 implementation with some added py3 stuff. And finding the type of the class is turning out to be interesting.\n- Like I said, the implementation of `super` in py 3 is very hacky. It revolves around the compiler detecting that a function defined in a class calls a function called `super`, it then creates that function with a closure around the class currently being defined.\n- `__class__` (and subsequently `super()` calls) seem to break when wrapping classes with decorators that return new classes.\n- @Coburn I've never had a problem with with that. Maybe you should ask a new question with an example that shows the problem. If you do, please tag me in a comment on the question.","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":44,"totalLines":325,"estimatedTokens":1732}}872{"id":"stack-9540117","source":"stackoverflow","questionId":9540117,"title":"How to left factor a context-free grammar?","tags":["compiler-construction","grammar","ll-grammar"],"text":"Title: How to left factor a context-free grammar?\nTags: compiler-construction, grammar, ll-grammar\nSource: Stack Overflow\n\nQuestion:\nAs I understand, in the following case left factoring is required to build top-down parser.\nBut it's hard to understand how to do that? Can someone help me here? Thanks.\n\n```\ns = a | b\nb = c d\nc = (e | f) g\ne = a | h\n```\n\n========================================\n\nCode:\n```text\ns = a | b\nb = c d\nc = (e | f) g\ne = a | h\n```\n\n```text\ns = a | ((a | h | f) g d)\n```\n\n```text\ns =  b' | c'\nb' = a | a g d\nc' = (h | f) g d\n```\n\n```text\ns =  b' | c'\nb' = a | a e'\nc' = (h | f) e'\ne' = g d\n```\n\n```text\ns =  b'' | c'\nb'' = a (e' | E)\nc' = (h | f) e'\ne' = g d\n```\n\n========================================\n\nComments:\n- Well, it depends on what the productions `a`, `d`, `f`, `g` and `h` are. If they're \"simple\" terminals, no left factoring is needed, AFAIK.\n- @BartKiers : Did you notice that in my example, `b also contains a in its left if you go through b->c->e->a` ? that means it could be `s = a | a + something`. Do you still say left factoring is not required? Thanks.\n- @Bhathiya: Left factoring is applied to transform the grammar so that control can't loop without consuming any tokens, which would lead to an endless loop when parsing. That's not the case here. The issue here is that this grammar can't be parsed with an LL(1) (single-symbol look-ahead) parser.\n- @500-InternalServerError: What is the solution you suggest to avoid the loop scenario in this case?","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":5,"totalLines":56,"estimatedTokens":375}}873{"id":"stack-4007479","source":"stackoverflow","questionId":4007479,"title":"Top-Down Parser want to have decent case example left-recursion in a 'Code'","tags":["compiler-construction","compiler-theory","parser-generator","ll-grammar"],"text":"Title: Top-Down Parser want to have decent case example left-recursion in a 'Code'\nTags: compiler-construction, compiler-theory, parser-generator, ll-grammar\nSource: Stack Overflow\n\nQuestion:\nHello fellow stack over flow members.\n\nI'm studying for compiler class.\nI did understand Top-Down Parser should avoid left-recursion, and transform into right-recursion way.\n\nQuestions are, \n\na) am I understanding right Top-Down Parser is equal to LL and Bottom-Up Parser is equal to LR ?\n\nb) I've found out left-recursion is Rule that calls itself ex) Expr :== Expr '+' Term | Term which can cause infinite loop to find Expr. But anyhow, any example code of consider input in C or Java? ( I don't want the parser or scanner code ) what I need is case code example with sentential form that occur infinite loop by left recursion.\n\nc) What actually makes difference in a way using Right Recursion in Top-Down Parser?\n\nANS c) Eliminating the need to backtrack. but something else?\n\nANS b) `x - 2 * y` but also something else? because this one works with backtrack way of parsing.\n\nCase example that I have found out the both non-left recursion and left recursion.\n\n### Left Recursion Grammar\n\n```\nA -> Ax\n```\n\n### Non-Left Recursion Grammar\n\n```\nA -> Bx\nB -> Ay\n```\n\nBoth are getting into infinite loop.\n\nThank you and appreciated for all your expert.\n\n========================================\n\nCode:\n```text\nA -> Ax\n```\n\n```text\nA -> Bx\nB -> Ay\n```\n\n```text\nx - 2 * y\n```\n\n```text\nA() {\n  A(); match(x);\n}\n```\n\n```text\nA -> Bx | z\nB -> Ay\n```\n\n```text\nA -> Ayx | z\n```\n\n```text\nE -> E + T | T\n```\n\n```text\nE -> T B\nB -> + T B | Lambda\n```\n\n========================================\n\nComments:\n- +10000 you just saved me several hours of work! this is brilliant. :)","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":8,"totalLines":85,"estimatedTokens":439}}874{"id":"stack-10805406","source":"stackoverflow","questionId":10805406,"title":"Data flow optimisation using HOOPL","tags":["haskell","compiler-construction","hoopl"],"text":"Title: Data flow optimisation using HOOPL\nTags: haskell, compiler-construction, hoopl\nSource: Stack Overflow\n\nQuestion:\nI am Haskell programmer ( I generally implement algorithms in Haskell ) and trying to understand HOOPL library but I am not able to decode it. I don't have compiler background ( currently learning from Coursera and Compilers: Principles, Techniques, and Tools ) and It would be great if you can suggest me a systematic way to proceed for understanding HOOPL library ( what is the prerequisite ). Lets say I have a small Haskell code on which I want to apply data flow optimization using HOOPL\n\n```\nadd :: Int -> Int -> Int\nadd x y = z where\n x' = 1\n y' = 1 -- this will be dead code elimination \n z = x' + 1\n```\n\nHow to write HOOPL code to optimise this. It would be great if you can give a bit better example and pardon me if I sound stupid.\n\n========================================\n\nCode:\n```text\nadd :: Int -> Int -> Int\nadd x y = z where\n   x' = 1\n   y' = 1 -- this will be dead code elimination \n   z = x' + 1\n```\n\n========================================\n\nComments:\n- you don't sound stupid. This paper looks quite readable and appears to address dead-code elimination.","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":32,"estimatedTokens":299}}875{"id":"stack-6939474","source":"stackoverflow","questionId":6939474,"title":"Embedding C compiler to a webpage","tags":["php","compiler-construction"],"text":"Title: Embedding C compiler to a webpage\nTags: php, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am developing a site for online C coding competition. The coding will be done in an editor that will be embedded in the webpage, and this must also be able to compile the program online. Is there any available compiler that can be embedded in the webpage? The server can be either linux or windows server. If anyone has any idea please .\n\n========================================\n\nComments:\n- I think, you should send a text to server, start a compiler (e.g gcc) on server (smth like `system()`) and post results (compilererrors and warnings) back to the page.\n- Why don't you use system() to call a traditional compiler?\n- Not concrete code but some info of how it has been done: codepad.org/about\n- +1 Also take a look @ stackoverflow.com/questions/3695858/&hellip; for sandboxing info\n- Sanitizing up-front is impossible with every programming language save incredibly simplistic toy languages. Limiting what the program can actually do (blocking most if not all syscalls, sandboxing, timeouts, etc.) should be way more effective.\n- You are absolutely right delnan, the OP probably wants to restrict the set of allowable C functions to a subset that is safe to use. Will also need to implement some sort of maximum execution timeout for not only the whole script but also for loops and so on... it's not an easy task.","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":16,"estimatedTokens":358}}876{"id":"stack-5586775","source":"stackoverflow","questionId":5586775,"title":"Control flow graph of a program","tags":["computer-science","compiler-theory","control-flow","compiler-construction","control-flow-graph"],"text":"Title: Control flow graph of a program\nTags: computer-science, compiler-theory, control-flow, compiler-construction, control-flow-graph\nSource: Stack Overflow\n\nQuestion:\nI'm taking a compiler class right now and we're at the point where we have to build a CFG in order to implement optimizations. One thing I can't figure out is how many CFGs are there for a program? Every example I ever see seems to be the CGF of a simple code segment. So, if you have a program that has say three functions. Do you have a separate CFG for each function or is there one big CFG for the entire program?\n\n========================================\n\nTop Answer:\nWell, you can construct a CFG for each function and then - if desirable for what you want to do - combine them into a complete one. Whole program CFGs can be pretty big, however, so they usually don't work well as examples.\n\n========================================\n\nCode:\n```text\n0  void foo() { /* do stuff */ }\n1  void bar() { /* do stuff */ }\n2\n3  void baz() {\n4     foo();  // Callsite for foo. Control transfers to foo, then foo returns here.\n5     bar();  // Callsite for bar. Control transfers to bar, then bar returns here.\n6  }\n```\n\n```text\nbaz\n```\n\n```text\nfoo\n```\n\n```text\nfoo\n```\n\n```text\nbaz\n```\n\n```text\nfoo\n```\n\n```text\nfoo\n```\n\n```text\nbaz\n```\n\n```text\nbar\n```\n\n```text\nbar\n```\n\n```text\nbar\n```\n\n```text\nbar\n```\n\n```text\nbaz\n```\n\n```text\nbar\n```\n\n```text\nbar\n```\n\n```text\nbaz\n```\n\n```text\nfoo\n```\n\n========================================\n\nComments:\n- Would this be better on Programmers?\n- Humm... I guess one thing that confuses me is that in class the professor said that basic block should only have one in edge and at most two out edges (one to the next block the second to a label elsewhere if there is a branch). So if I connected the different graphs then the first block for a function could have more than one in edge and the last black could have more than two out edges. Any thoughts? Would you say that the 1 edge in 2 edge out statement is wrong?\n- The way basic blocks are usually defined is so that they only have one entry point, but you can have multiple edges pointing to the entry point. You just can't jump into the *middle* of the block, because that's not how BB's are defined. For easy analysis, you want your blocks set up so that if *any* statement in the block is executed, *all* statements in the block are executed.\n- You can still have more than one edge into the block, as long as it jumps to that first statement. This happens for functions, which can be called from multiple places, and for loop entries, where the block can be entered either from the code before the loop or from the branch at the end of the loop, if the loop isn't done yet. You usually call that edge from the end of the loop back to the beginning a back edge.\n- As for the out-degree, you usually only have two edges out of basic blocks. If you had a three-way branch instruction, I suppose you could have more out edges, but I can't think of an architecture that has this off the top of my head. Maybe your prof is just trying to keep things simple for now.\n- It's fairly easy to find examples of control flow graphs online -- just google for it. Note the number of in edges (>1) here: cs.arizona.edu/~collberg/Teaching/453/2009/Handouts/&hellip;\n- Per function graphs usually will *not* include edges to the cfg of the functions being called. That would be equivalent to inlining the function. And how would you represent calls to already compiled library functions and what about recursive functions? Function calls are instead usually represented by a special node that contains the name of the function and the arguments it is being called with.","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":99,"estimatedTokens":929}}877{"id":"stack-59918968","source":"stackoverflow","questionId":59918968,"title":"Distinguishing between right shift (>>) and Java generics","tags":["java","compiler-construction","flex-lexer","lexer"],"text":"Title: Distinguishing between right shift (>>) and Java generics\nTags: java, compiler-construction, flex-lexer, lexer\nSource: Stack Overflow\n\nQuestion:\nI am writing a lexer for java in flex. \nThe java spec says:\n\n \"The longest possible translation is used at each step, even if the result does not ultimately make a correct program while another lexical translation would. There is one exception: **if lexical translation occurs in a type context (§4.11) and the input stream has two or more consecutive > characters that are followed by a non-> character, then each > character must be translated to the token for the numerical comparison operator >.\"**\n\nSo how can I distinguish between right shift operator and something like in `>`**?\n\n========================================\n\nCode:\n```text\n<List<List\n```\n\n```text\n>>\n```\n\n```text\n>/>     { return CONJUNCTIVE_GT; }\n>       { return INDEPENDENT_GT; }\n  /* These two don't need to be changed. */\n>>=     { return SHIFT_ASSIGN; }\n>>>=    { return LONG_SHIFT_ASSIGN; }\n```\n\n```text\nshift_op     : CONJUNCTIVE_GT INDEPENDENT_GT\nlong_shift_op: CONJUNCTIVE_GT CONJUNCTIVE_GT INDEPENDENT_GT\nclose_angle  : CONJUNCTIVE_GT | INDEPENDENT_GT\ngt_op        : INDENPENDENT_GT /* This unit production is not really necessary */\n```\n\n```text\n>>\n```\n\n```text\n>>>\n```\n\n```text\n>>\n```\n\n```text\n>>>\n```\n\n```text\n>\n```\n\n```text\n>\n```\n\n```text\n>>\n```\n\n```text\n>>>\n```\n\n```text\n>\n```\n\n========================================\n\nComments:\n- \"*So how can I distinguish between right shift operator and something like in `>`?*\" - By context. A rightshift is part of an arithmetic expression, while generics are part of either constructor calls, method signatures, type- or class-definitions.\n- Here I am asking about how can I distinguish between them in my lexer(lex code).\n- If your scanner does only tokenize, then it is irrelevant which `>` is encountered. A `>` is a `>`, no differentiation. The syntactical correctness is then assured by the parser. If the scanner does, however, have an evaluator, then the scanner needs context information since the grammar of Java is context-sensitive.\n- @Turing85: Not really. In most languages, `>>` is a single token (like `>=`). If `>>` were two tokens, the grammar would accept expressions like `2> >3` or even `2 >&#47;* This is a right shift* &#47;> 3`, which are generally frowned upon.\n- `long_shift_op` should be `CONJUNCTIVE_GT CONJUNCTIVE_GT INDEPENDENT_GT`, right?","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":13,"totalLines":82,"estimatedTokens":612}}878{"id":"stack-19075189","source":"stackoverflow","questionId":19075189,"title":"Syntax analysis and semantic analysis","tags":["parsing","syntax","compiler-construction","semantics","recursive-descent"],"text":"Title: Syntax analysis and semantic analysis\nTags: parsing, syntax, compiler-construction, semantics, recursive-descent\nSource: Stack Overflow\n\nQuestion:\nI am wondering how the syntax analysis and semantic analysis work.\n\nI have finished the lexer and the grammar construction of my interpreter.\n\nNow I am going to implement a recursive descent (top down) parser for this grammar\n\nFor example, I have the following grammar:\n\n```\n ::= ASSIGN \n```\n\nso i coded it like this (in java):\n\n```\npublic void declaration(){\n data_type();\n identifier();\n if(token.equals(\"ASSIGN\")){\n lexer(); //calls next token\n value();\n } else {\n error();\n }\n}\n```\n\nAssuming I have three data types: Int, String and Boolean. Since the values for each data types are different, (ex. true or false only in Boolean) how can I determine if it fits the data type correctly? What part of my code would determine that? \n\nI am wondering where would I put the code to:\n\n```\n1.) call the semantic analysis part of my program. \n2.) store my variables into the symbol table.\n```\n\nDo syntax analysis and semantic analysis happen at the same time? \nor do i need to finish the syntax analysis first, then do the semantic analysis?\n\nI am really confused. Please help.\n\nThank you.\n\n========================================\n\nTop Answer:\nThe short answer is: **it depends on the definition of your programming language**. And, since you only specified one derivation rule and three native types, there is no way of knowing. For example, if your programming language allows forward declarations like the c++ code below, then handling the derivation rule for function declaration (foo) is done **without knowing** the type of the variable serial\n\n```\nclass Tree {\npublic:\n int foo(void)\n {\n return serial;\n }\n int serial;\n};\n```\n\nIndeed, modern compilers separate the syntax analysis phase from the semantic analysis phase. The syntax analysis phase is performed first, making sure the input program agrees with the context free grammar of the language. And, in addition, produces an **A**bstract **S**yntax **T**ree (**AST**). Note the difference between an AST and a parse tree, as discussed in this SO post. The semantic analysis phase then traverses the AST and checks for type mismatches among other things.\n\nHaving said that, toy programming languages can sometimes couple semantic and syntax analysis together. When a recursive descent parser is used,\nyou should have relevant recursive calls return a type.\n\n========================================\n\nCode:\n```text\n<declaration>  ::=   <data_type> <identifier> ASSIGN <value>\n```\n\n```text\npublic void declaration(){\n    data_type();\n    identifier();\n    if(token.equals(\"ASSIGN\")){\n        lexer();   //calls next token\n        value();\n    } else {\n        error();\n    }\n}\n```\n\n```text\n1.) call the semantic analysis part of my program. \n2.) store my variables into the symbol table.\n```\n\n```text\n<data_type>\n```\n\n```text\n<value>\n```\n\n```text\nclass Tree {\npublic:\n    int foo(void)\n    {\n        return serial;\n    }\n    int serial;\n};\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":112,"estimatedTokens":764}}879{"id":"stack-13867525","source":"stackoverflow","questionId":13867525,"title":"boolean and arithmetic expression grammar in ANTLR","tags":["compiler-construction","antlr","grammar","antlr3"],"text":"Title: boolean and arithmetic expression grammar in ANTLR\nTags: compiler-construction, antlr, grammar, antlr3\nSource: Stack Overflow\n\nQuestion:\nI'm trying to write a grammar for arithmetic and boolean expressions. I don't understand what I'm doing wrong. For my grammar, ANTLR says:\n\n [fatal] rule logic_atom has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2. Resolve by left-factoring or using syntactic predicates or using backtrack=true option.\n\nBut I can't do a left-factoring. And I don't want to touch `arith_expr`, because for this I have a code. \n\nError in `logic_atom : LBR logic_expr RBR | cmp_expr ;`\n\nMy code:\n\n```\ngrammar ArithmeticInterpreter;\n\noptions { \n output = AST;\n language = C;\n}\n//options{greedy=true;}:\n\naxiom : lines EOF! ;\nlines : line (SEP! line)* ;\nline : (def_var | print_expr | scan_expr)? ;\n\ndef_var : VARIABLE ASSIGMENT^ logic_expr ;\nprint_expr : PRINT_KEYW^ arith_expr ;\nscan_expr : SCAN_KEYW^ VARIABLE ;\n\narith_expr : ((PLS | MNS)^)? term ((PLS | MNS)^ term)*;\nterm : power ((MLP | DIV)^ power )*;\npower : atom (options{greedy=true;}: PWR^ power )*;\natom : INT | FLOAT | VARIABLE | LBR arith_expr RBR -> ^(arith_expr);\n\nlogic_expr : logic_atom ((OR | AND)^ logic_atom)*;\nlogic_atom : LBR logic_expr RBR | cmp_expr ;\ncmp_expr: arith_expr (LSS | LSQ | GRT | GRQ | EQL | NEQ) arith_expr;\n\nWS : ( ' '| '\\t'| '\\r') {$channel=HIDDEN;};\n\nLBR : '(' ;\nRBR : ')' ;\nPLS : '+' ;\nMNS : '-' ;\nMLP : '*' ;\nDIV : '/' ;\nPWR : '^' ;\n\nLSS : '' ;\nGRQ : '>=' ;\nEQL : '==' ;\nNEQ : '!=' ;\nAND : '&&' ;\nOR : '||' ;\nNOT : '!' ;\n\nASSIGMENT : '=' ;\nPRINT_KEYW : 'print' ;\nSCAN_KEYW : 'scan' ;\n\nSEP : '\\n' | ';' ;\n\nINT : ('0'..'9')+;\n\nFLOAT : INT '.' INT* EXP? | '.' INT EXP? | INT EXP;\nfragment EXP : ('e'|'E') (PLS | MNS)? INT;\n\nVARIABLE : SS (SS | '0'..'9')* ;\nfragment SS : 'a'..'z' | 'A'..'Z' | '_' ;\n```\n\n// `(LBR arith_expr)=>` isn't work.\n\n========================================\n\nTop Answer:\nMy quick suggestion is to combine arith and logical expressions. See any sample grammar such as Java.g or whatever. ANTLR v4 would handle this no problem, btw.\n\n========================================\n\nCode:\n```text\ngrammar ArithmeticInterpreter;\n\noptions { \n    output = AST;\n    language = C;\n}\n//options{greedy=true;}:\n\naxiom : lines EOF! ;\nlines : line (SEP! line)* ;\nline  : (def_var | print_expr | scan_expr)? ;\n\ndef_var    : VARIABLE ASSIGMENT^ logic_expr ;\nprint_expr : PRINT_KEYW^ arith_expr ;\nscan_expr  : SCAN_KEYW^ VARIABLE ;\n\narith_expr : ((PLS | MNS)^)? term ((PLS | MNS)^ term)*;\nterm       : power ((MLP | DIV)^ power )*;\npower      : atom  (options{greedy=true;}: PWR^ power )*;\natom       : INT | FLOAT | VARIABLE | LBR arith_expr RBR -> ^(arith_expr);\n\nlogic_expr    : logic_atom ((OR | AND)^ logic_atom)*;\nlogic_atom :   LBR logic_expr  RBR |  cmp_expr  ;\ncmp_expr: arith_expr (LSS | LSQ | GRT | GRQ | EQL | NEQ) arith_expr;\n\nWS  : ( ' '| '\\t'| '\\r') {$channel=HIDDEN;};\n\nLBR :  '(' ;\nRBR :  ')' ;\nPLS :  '+' ;\nMNS :  '-' ;\nMLP :  '*' ;\nDIV :  '/' ;\nPWR :  '^' ;\n\nLSS :  '<'  ;\nLSQ :  '<=' ;\nGRT :  '>'  ;\nGRQ :  '>=' ;\nEQL :  '==' ;\nNEQ :  '!=' ;\nAND :  '&&' ;\nOR  :  '||' ;\nNOT :  '!'  ;\n\nASSIGMENT : '=' ;\nPRINT_KEYW : 'print' ;\nSCAN_KEYW  : 'scan' ;\n\nSEP : '\\n' | ';' ;\n\nINT :  ('0'..'9')+;\n\nFLOAT : INT '.' INT* EXP? | '.' INT EXP? | INT EXP;\nfragment EXP : ('e'|'E') (PLS | MNS)? INT;\n\nVARIABLE : SS (SS | '0'..'9')* ;\nfragment SS : 'a'..'z' | 'A'..'Z' | '_' ;\n```\n\n```text\narith_expr\n```\n\n```text\nlogic_atom :   LBR logic_expr  RBR |  cmp_expr  ;\n```\n\n```text\n(LBR arith_expr)=>\n```\n\n```text\nlogic_expr : cmp_expr ((OR | AND)^ cmp_expr)*;\ncmp_expr   : (arith_expr (LSS | LSQ | GRT | GRQ | EQL | NEQ))=> arith_expr (LSS | LSQ | GRT | GRQ | EQL | NEQ)^ arith_expr\n           | LBR logic_expr RBR -> logic_expr\n           ;\n```\n\n```text\ngrammar ArithmeticInterpreter;\n\noptions { \n    output = AST;\n}\n//options{greedy=true;}:\n\naxiom : lines EOF! ;\nlines : line (SEP! line)* ;\nline  : (def_var | print_expr | scan_expr)? ;\n\ndef_var    : VARIABLE ASSIGMENT^ logic_expr ;\nprint_expr : PRINT_KEYW^ arith_expr ;\nscan_expr  : SCAN_KEYW^ VARIABLE ;\n\narith_expr : ((PLS | MNS)^)? term ((PLS | MNS)^ term)*;\nterm       : power ((MLP | DIV)^ power )*;\npower      : atom  (PWR^ atom)*;  //<-- changed\natom       : INT | FLOAT | VARIABLE \n           | LBR arith_expr RBR -> arith_expr //<-- changed\n           ;\n\nlogic_expr : cmp_expr ((OR | AND)^ cmp_expr)*;\ncmp_expr   : (arith_expr (LSS | LSQ | GRT | GRQ | EQL | NEQ))=> arith_expr (LSS | LSQ | GRT | GRQ | EQL | NEQ)^ arith_expr\n           | LBR logic_expr RBR -> logic_expr\n           ;\n\nWS  : ( ' '| '\\t'| '\\r') {$channel=HIDDEN;};\n\nLBR :  '(' ;\nRBR :  ')' ;\nPLS :  '+' ;\nMNS :  '-' ;\nMLP :  '*' ;\nDIV :  '/' ;\nPWR :  '^' ;\n\nLSS :  '<'  ;\nLSQ :  '<=' ;\nGRT :  '>'  ;\nGRQ :  '>=' ;\nEQL :  '==' ;\nNEQ :  '!=' ;\nAND :  '&&' ;\nOR  :  '||' ;\nNOT :  '!'  ;\n\nASSIGMENT : '=' ;\nPRINT_KEYW : 'print' ;\nSCAN_KEYW  : 'scan' ;\n\nSEP : '\\n' | ';' ;\n\nINT :  ('0'..'9')+;\n\nFLOAT : INT '.' INT* EXP? | '.' INT EXP? | INT EXP;\nfragment EXP : ('e'|'E') (PLS | MNS)? INT;\n\nVARIABLE : SS (SS | '0'..'9')* ;\nfragment SS : 'a'..'z' | 'A'..'Z' | '_' ;\n```\n\n```text\n(= x (< 2 3))\n```\n\n```text\n(= x (|| (< (+ 2 3) 4) (&& (> (^ 5 5) 30) (== 3 (+ 10 2)))))\n```\n\n```text\nlogic_expr\n```\n\n```text\ncmp_expr\n```\n\n```text\nlogic_atom\n```\n\n```text\ncmp_expr\n```\n\n```text\narith_expr\n```\n\n```text\narith_expr\n```\n\n```text\nlogic_expr\n```\n\n```text\narith_expr\n```\n\n```text\nlanguage\n```\n\n```text\nx=(2<3)\n```\n\n```text\nx = 2 + 3 < 4 || (5 ^ 5 > 30 && 3 == 10 + 2)\n```\n\n========================================\n\nComments:\n- I'm tryed this too. For this antlrworks drawed a bug tree. For example: `x=(1 cmp_expr -> NoViableAltExpression)`\n- @AlexanderLavrukov I updated the answer with the grammar I used for testing and some example output. When you get time, try running this grammar yourself to see if it gives you errors.\n- @AlexanderLavrukov, make sure you're **not** using the interpreter while testing. Use the debugger instead.","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":19,"totalLines":285,"estimatedTokens":1499}}880{"id":"stack-7781044","source":"stackoverflow","questionId":7781044,"title":"Operations on 64 bits operands in 32 bit architectures?","tags":["compiler-construction"],"text":"Title: Operations on 64 bits operands in 32 bit architectures?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nhow does most compilers implement operations on 64 bit operands (e.g long int) in 32 bit environments ? in other words, is there a way to implement these operations in a single step or we need to access multiple memory locations to implement these operations ?\n\n========================================\n\nCode:\n```text\nadd   %eax, (lower 32-bits of operand)\nadc   %edx, (upper 32-bits of operand)\n```\n\n========================================\n\nComments:\n- What about 128-bit integers on 32-bit (x86) hardware?\n- It gets ugly. At this point it isn't too different from multi-precision arithmetic. For addition, you'd be chaining 3 `adc` instructions. (don't forget you have a dependency on the carry-flag that could kill your pipeline...) For multiplication, you'd have to do 16 x 32-bit multiplies and a bunch of `add` and `adc`... I'm not aware of any compiler that supports this.","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":20,"estimatedTokens":252}}881{"id":"stack-8931064","source":"stackoverflow","questionId":8931064,"title":"Tracking source position of AST nodes in a compiler (ocaml)","tags":["compiler-construction","ocaml","yacc","lex","abstract-syntax-tree"],"text":"Title: Tracking source position of AST nodes in a compiler (ocaml)\nTags: compiler-construction, ocaml, yacc, lex, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI'm writing a compiler in ocaml, using ocamllex/yacc. Things are going well, but I've a design problem. For each AST node I create, it'd be good to have information about line/character position of that node in the source code. That would be useful for providing error messages to the user later.\n\nNow, I can add some kind of meta type to my nodes:\n\n```\ntype node = Node1 of ... * meta | Node2 of ... * meta\n```\n\nbut that seems redundant. Later, when I'm done with verifying the AST, I'll have to write\n\n```\nmatch n with \n| NodeX(..., _) -> ...\n```\n\nin every `match` which is a waste of space. \n\nWhat's the best way to solve this?\n\n========================================\n\nCode:\n```text\ntype node = Node1 of ... * meta | Node2 of ... * meta\n```\n\n```text\nmatch n with \n| NodeX(..., _) -> ...\n```\n\n```text\nmatch\n```\n\n```text\ntype node_exp = Node1 of ... | Node2 of ...\nand node = { exp: node_exp; meta: meta }\n```\n\n```text\nmatch n.exp with\n  | NodeX ... -> ...\n```\n\n========================================\n\nComments:\n- For your next project you should consider menhir, a superior alternative to ocamlyacc.","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":5,"totalLines":54,"estimatedTokens":319}}882{"id":"stack-9104133","source":"stackoverflow","questionId":9104133,"title":"Compiling object file from an intermediate file of gcc","tags":["c","linux","gcc","compiler-construction","backend"],"text":"Title: Compiling object file from an intermediate file of gcc\nTags: c, linux, gcc, compiler-construction, backend\nSource: Stack Overflow\n\nQuestion:\nBy using the **-fdump-tree-*** flag , one can dump some intermediate format file during compilation of a source code file. My question is if one can use that intermediate file as an input to gcc to get the final object file. \n\nI'm asking this because I want to add some code to the intermediate file of the **gimple** (obtained by using the flag **-fdump-tree-gimple**) format. Sure I can use hooks and add my own pass, but I don't want to get to that level of complexity yet. I just want to give gcc my modified intermediate file, so it can start its compilation from there and give me the final object file. Any ideas how to achieve this?\n\n========================================\n\nTop Answer:\nThere's no built-in facility to translate the text GIMPLE representation back to original GIMPLE internal representation. \n\nYou'll need to use custom front-end (such as suggested GIMPLE FE) to make sense of dumped GIMPLE.\n\n========================================\n\nCode:\n```text\nopt\n```\n\n========================================\n\nComments:\n- (+1) No answer either, but, want to achieve the same.\n- Have you seen this?\n- I know such project for gcc which is named gcc-xil and which is infected with GPL (free to download). // I ll delete this comment a bit later.\n- So in short, you are saying LLVM is much easier to work with?","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":30,"estimatedTokens":368}}883{"id":"stack-26465305","source":"stackoverflow","questionId":26465305,"title":"Position independent code: what is the difference at compile time?","tags":["dynamic-linking","linker","compiler-construction","dynamic-loading"],"text":"Title: Position independent code: what is the difference at compile time?\nTags: dynamic-linking, linker, compiler-construction, dynamic-loading\nSource: Stack Overflow\n\nQuestion:\nReading through the libtool docs I wondered why we need to tell the compiler to produce position independent code. After all, the object file which is produced does not yet know where in address space it will be linked at, so one single object file should be position independent at all times, shouldn't it? And further, to produce shared object libs, wouldn't that simply mean baking together all the object files which make out that lib and possibly setting all relative references which have become \"intern\" to the baked-together lib, while leaving all other references (absolute and relative) to be filled out later by the linker-loader? Why is this a concern also for the compiler?\n\n========================================\n\nComments:\n- Possibly more ontopic on SO. Is this really unix-specific?\n- Ok, I think I know now, where I missed the point of shared libs: shared lib foolib.so can appear at address X for one process and at address Y for another process, is that right?\n- Actually each process has its own virtual address space, so if the shared library like foolib.so is build with PIC then it can be anywhere within the processes virtual address space.\n- Shouldn't we say: it MUST be built with PIC, because otherwise it could collide with code from the process which is actually trying to use the lib?\n- Yes, it should be built with PIC as libtool recommends.Though in theory you could build a shared library without PIC. But the whole purpose of having shared library in a system is to make the use of shared library dynamic because a programmer/compiler in advance doesn't have the knowledge that where the required/needed shared library could be located, and that is where PIC is useful it provides location in-dependency.Regarding collision of addresses: with PIC same library code can be loaded in each program address space so that it won't overlap any other uses of memory.","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":15,"estimatedTokens":519}}884{"id":"stack-45922001","source":"stackoverflow","questionId":45922001,"title":"Is `goal symbol` the same thing as `start symbol` in context-free-grammar","tags":["javascript","ecmascript-6","compiler-construction"],"text":"Title: Is `goal symbol` the same thing as `start symbol` in context-free-grammar\nTags: javascript, ecmascript-6, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nContext free grammar define the four constituent parts:\n\n \n A set of non-terminals (V)...\n\n A set of tokens, known as terminal symbols (Σ)...\n\n A set of productions (P)...\n\n One of the non-terminals is designated as **the start symbol** (S); from\n where the production begins.\n\n \n\nThe EcmaScript specification doesn't mention **start symbol**, instead it mentions a **goal symbol**:\n\n **Starting from a sentence** consisting of a single distinguished\n nonterminal, called the **goal symbol**, a given context-free grammar\n specifies a language, namely, the (perhaps infinite) set of possible\n sequences of terminal symbols that can result from repeatedly\n replacing any nonterminal in the sequence with a right-hand side of a\n production for which the nonterminal is the left-hand side.\n\nFrom this definition I can probably conclude **that goal symbol is actually just another name for a start symbol**, but the `goal symbol` name is used because there are different start symbols \"categories\":\n\n There are several situations where the identification of lexical input\n elements is sensitive to the syntactic grammar context that is\n consuming the input elements. This requires multiple goal symbols for\n the lexical grammar.\n\nSo is goal symbol is another name for start symbol in the context of CFG?\n\n========================================\n\nCode:\n```text\ngoal symbol\n```\n\n========================================\n\nComments:\n- what is the reason for a close vote?\n- probably because its exceeding the knowledge of the common SO answerer? ;)\n- @Jonasw, yeah, I'm still hoping there are people who understand the EcmaScript on that level\n- Thanks, Max (@AngularInDepth.com), I was just wondering the same thing. In case you are still diving into the spec, I have posted a few related questions, including this: stackoverflow.com/questions/49604170/&hellip;\n- thanks for the confirmation! *appears to be literally copied from the Java Language Specification* - that's what I noticed as well. I started to look at Java spec as @Pointy suggested and noticed full paragraphs similar word in word.","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":52,"estimatedTokens":565}}885{"id":"stack-34539366","source":"stackoverflow","questionId":34539366,"title":"System.Reflection.Emit - If Statement Generation","tags":["c#","reflection","compiler-construction","clr","il"],"text":"Title: System.Reflection.Emit - If Statement Generation\nTags: c#, reflection, compiler-construction, clr, il\nSource: Stack Overflow\n\nQuestion:\nI'm trying to learn how `If` statements are generated with ILGenerator in C#.\n\nHere's my code: (ilg is an ILGenerator)\n\n```\nilg.Emit(OpCodes.Ldc_I4_1);\nLabel start = ilg.DefineLabel();\nilg.Emit(OpCodes.Brfalse, start);\nilg.Emit(OpCodes.Ldstr, \"Hello\");\nilg.Emit(OpCodes.Call, typeof(Console).GetMethod(\"WriteLine\", new Type[] { typeof(string) }));\nilg.MarkLabel(start);\n```\n\nSome notes:\n\n- Alone, calling Console.WriteLine works perfectly.\n\n- This is the error that I get when I run the generated exe:\n\n Unhandled Exception: System.InvalidProgramException: Common Language Runtime detected an invalid program. at Testing.Test.Main(String[] )\n\n========================================\n\nCode:\n```text\nilg.Emit(OpCodes.Ldc_I4_1);\nLabel start = ilg.DefineLabel();\nilg.Emit(OpCodes.Brfalse, start);\nilg.Emit(OpCodes.Ldstr, \"Hello\");\nilg.Emit(OpCodes.Call, typeof(Console).GetMethod(\"WriteLine\", new Type[] { typeof(string) }));\nilg.MarkLabel(start);\n```\n\n```text\nIf\n```\n\n```text\npublic static void Main(string[] args)\n{\n    var dynamicMethod = new DynamicMethod(\"PrintHello\", typeof(void), null);\n    var ilGenerator = dynamicMethod.GetILGenerator();\n    ilGenerator.Emit(OpCodes.Ldc_I4_1);\n    var toEnd = ilGenerator.DefineLabel();\n    ilGenerator.Emit(OpCodes.Brfalse, toEnd);\n    ilGenerator.Emit(OpCodes.Ldstr, \"Hello\");\n    ilGenerator.Emit(OpCodes.Call, typeof(Console).GetMethod(\"WriteLine\", new Type[] { typeof(string) }));\n    ilGenerator.MarkLabel(toEnd);\n    ilGenerator.Emit(OpCodes.Ret);\n\n    var @delegate = (Action)dynamicMethod.CreateDelegate(typeof(Action));\n    @delegate();\n}\n```\n\n```text\npublic static void Main(string[] args)\n{\n}\n```\n\n```text\n.method public hidebysig static void Main (string[] args) cil managed \n{\n    // Method begins at RVA 0x2050\n    // Code size 2 (0x2)\n    .maxstack 8\n    .entrypoint\n\n    IL_0000: nop\n    IL_0001: ret\n} // end of method Program::Main\n```\n\n```text\n.method public hidebysig static void Main (string[] args) cil managed \n{\n    // Method begins at RVA 0x2050\n    // Code size 1 (0x1)\n    .maxstack 8\n    .entrypoint\n\n    IL_0000: ret\n} // end of method Program::Main\n```\n\n```text\nOpCodes.Ret\n```\n\n```text\nilGenerator.Emit(OpCodes.Ret);\n```\n\n```text\nSystem.InvalidProgramException\n```\n\n========================================\n\nComments:\n- Related: What does an if look like in IL?\n- @SplittyDev thanks for edit :D\n- Thanks! :) Works flawlessly.\n- Thanks @DaniloLekovic I am glad that this answer was useful to you.","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":9,"totalLines":110,"estimatedTokens":653}}886{"id":"stack-40320237","source":"stackoverflow","questionId":40320237,"title":"Semantic vs Syntax analyzing in Programming","tags":["syntax","compiler-errors","compiler-construction","syntax-error","semantics"],"text":"Title: Semantic vs Syntax analyzing in Programming\nTags: syntax, compiler-errors, compiler-construction, syntax-error, semantics\nSource: Stack Overflow\n\nQuestion:\nI am new to compiler construction and I was trying to make a CFG(Context Free Grammar) of Assignment Statement in programming for Syntax Analyzer in Compiler Construction and I want to know whether this illegal statement is a semantic error or a syntax error ?\n\n5=a;\n\nthanks!\n\n========================================\n\nComments:\n- Quite often, BNF for an assignment statement would look like `assign := \"=\" `, where syntax for the `` is a subset of an expression syntax and does not include literals, binary operations, etc. But, yet, it is totally legitimate approach to expect the same expression syntax on both sides, and then check if a destination expression is legal in one of the consequent semantic passes. Do whatever you like, both ways are ok.\n- It should be noted that rejecting `5=a` at the syntax level is not possible with an LL(k) parser (unless I'm missing something major) and requires arbitrary backtracking in a recursive descent parser. The official C grammar also does not reject it (`assignment-expression: unary-expression assignment-operator assignment-expression`), so I don't think it's as common as you make it sound.\n- @sepp2k: For an LL parser, in general surely I can write a grammar rule \"assignment_stmt = target '=' expression; \" and then write specialized rules to define target. This would be problematic only if the language also allowed \"some_stmt = expression;\" which only makes sense if you mix assignment operators in the middle of the expression. That is true of C, and many language copy C's style but not necessarily everything about C, and there are oodles of languages that don't copy C's style. So we can argue about how common. The official C grammar likewise could have been written differently\n- Let's say you do that. For an LL grammar, you'd end up with something like `target : primary_target ('[' expression ']')?; primary_target : identifier | '(' target ')'`, right? Now you'll also have `postfix_expression : primary_expression ('[' expression ']' | ...)?` and `primary_expression : identifier;` and `expression_statement : expression ';'` where `expression` can be derived to `postfix_expression` and `statement` to `assignment_statement` and `expression_statement`. Now both `assignment_statement` and `expression_statement` can both start with a prefix of the form ...\n- ... `identifier '[' expression ']'` and you won't be able to know which production to take until you encounter a `=` or `;`. Since the index part can be arbitrarily long, a constant amount of lookahead won't suffice to make this decision. Therefore it's not LL(k) and I'm pretty sure you can't rewrite it a way that is.\n- You could also try to restrict expression statement to only expressions that could contain side effects, so `arr[i];` would no longer be a valid statement. But you'd still need to allow something like `arr[i]()` or `arr[i].m()`, so you'd still run into the common prefix problem.\n- Yes, I agree, that was exactly my point in 2nd sentence of my first response to you. But there are many, many languages that insist on keywords as prefixes to assignment statements, or which have keywords starting every statement except assignment statements, and in these cases there is no conflict. (PS: all these problems of LL/recursive descent parsing are why I chose to build our tools around GLR parsers. So many stupid parsing problems *just vanish*. See stackoverflow.com/questions/243383/&hellip;)\n- For some reason I misread your second sentence as saying \"It's only problematic if assignments are also expressions\". Sorry for the noise then.\n- That's ok, this is good reading for those that haven't been down this path.","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":23,"estimatedTokens":958}}887{"id":"stack-23034295","source":"stackoverflow","questionId":23034295,"title":"Partial application in LLVM","tags":["compiler-construction","llvm","llvm-ir","compiler-development"],"text":"Title: Partial application in LLVM\nTags: compiler-construction, llvm, llvm-ir, compiler-development\nSource: Stack Overflow\n\nQuestion:\nI am trying to create a function \"add\" that can be applied to a single argument, and subsequently another. I can't figure out how to represent this with LLVM IR, as I don't understand how to call a function with a single value, then save the value somewhere in memory and return another function that is applied to that val. I would need some sort of closure mechanism in LLVM.\n\nI have searched for implementations of this in C so that I could view the emitted LLVM through clang, but the solutions I found were wildly complex, so I thought I might just investigate LLVM directly.\n\nThis would be the uncurried version\n\n```\ndefine i8 @add(i8 %a, i8 %b) {\nentry:\n %res = add i8 %a, %b\n ret i8 %res\n}\n```\n\nAnd somehow I'd like for `add(1)` to return an `i8 (i8)` type. I figure I'll have to split the function up somehow.\n\nps. I am looking into this because I'm working on a compiler for a small functional language, so I'm looking for any advice concering the implementation of partial application/currying in compiler design in general.\n\nUpdate:\nI now have the following code working, but it's a quite complicated and I don't think it'll be easy to generate automatically\n\n```\ndeclare i32 @printf(i8* noalias nocapture, ...)\n\ndefine { i8, i8 (i8, i8) * } @add1(i8 %a) {\n ; allocate the struct containing the supplied argument \n ; and a function ptr to the actual function\n %nextPtr = alloca { i8, i8 (i8, i8) * }\n store { i8, i8 (i8, i8) * } { i8 undef, i8 (i8, i8) * @add2 }, { i8, i8 (i8, i8) * } * %nextPtr\n %next0 = load { i8, i8 (i8, i8) * } * %nextPtr\n\n ; insert the supplied arg into the struct\n %next1 = insertvalue { i8, i8 (i8, i8) * } %next0, i8 %a, 0\n ret { i8, i8 (i8, i8) * } %next1\n}\n\ndefine i8 @add2(i8 %a, i8 %b) {\n %res = add i8 %a, %b\n ret i8 %res\n}\n\ndefine i8 @main() {\n ; call add(35) resulting in 'fn' of type {35, &add2}\n %res1 = call { i8, i8 (i8, i8) * } @add1(i8 35)\n\n ; get the arg of the first call, ie element 0 of the resulting struct\n %arg = extractvalue { i8, i8 (i8, i8) * } %res1, 0\n ; similarily get the function ptr\n %fn = extractvalue { i8, i8 (i8, i8) * } %res1, 1\n\n ; apply the argument to the function\n %res = call i8 %fn(i8 %arg, i8 30)\n\n ; print result \n %ptr = alloca i8\n store i8 %res, i8* %ptr\n call i32 (i8*, ...)* @printf(i8* %ptr)\n\n ret i8 0\n}\n```\n\n========================================\n\nCode:\n```text\ndefine i8 @add(i8 %a, i8 %b) {\nentry:\n  %res = add i8 %a, %b\n  ret i8 %res\n}\n```\n\n```text\ndeclare i32 @printf(i8* noalias nocapture, ...)\n\ndefine { i8, i8 (i8, i8) * } @add1(i8 %a) {\n  ; allocate the struct containing the supplied argument \n  ; and a function ptr to the actual function\n  %nextPtr = alloca { i8, i8 (i8, i8) * }\n  store { i8, i8 (i8, i8) * } { i8 undef, i8 (i8, i8) * @add2 }, { i8, i8 (i8, i8) * } * %nextPtr\n  %next0 = load { i8, i8 (i8, i8) * } * %nextPtr\n\n  ; insert the supplied arg into the struct\n  %next1 = insertvalue { i8, i8 (i8, i8) * } %next0, i8 %a, 0\n  ret { i8, i8 (i8, i8) * } %next1\n}\n\ndefine i8 @add2(i8 %a, i8 %b) {\n  %res = add i8 %a, %b\n  ret i8 %res\n}\n\ndefine i8 @main() {\n  ; call add(35) resulting in 'fn' of type {35, &add2}\n  %res1 = call { i8, i8 (i8, i8) * } @add1(i8 35)\n\n  ; get the arg of the first call, ie element 0 of the resulting struct\n  %arg = extractvalue { i8, i8 (i8, i8) * } %res1, 0\n  ; similarily get the function ptr\n  %fn = extractvalue { i8, i8 (i8, i8) * } %res1, 1\n\n  ; apply the argument to the function\n  %res = call i8 %fn(i8 %arg, i8 30)\n\n  ; print result  \n  %ptr = alloca i8\n  store i8 %res, i8* %ptr\n  call i32 (i8*, ...)* @printf(i8* %ptr)\n\n  ret i8 0\n}\n```\n\n```text\nadd(1)\n```\n\n```text\ni8 (i8)\n```\n\n```text\nclang -S -emit-llvm partial.c\n```\n\n========================================\n\nComments:\n- Are you set on the `i8 (i8)` type or was that an example? To actually get a honest-to-LLVM function pointer you'd have to dynamically generate machine code at run time. It's easier if you create an aggregate that stores already-supplied arguments together with a function pointers. AFAIK that's how Rust implements closures. I'd tell you to look at their output, but in my limited experience that IR is also rather hard to read.\n- That worked (see above), but would be much easier if I could get the `i8 (i8)` type back from the first call :D\n- I really doubt it would be easier. As I said, to get an actual function pointer, you'd need to generate thunks for each partial application. That entails generating code to (1) allocate memory, (2) set permissions accordingly, (3) write out machine code that's not only platform-specific but also argument-type-specific, and possibly some more. Even if you use libffi for that, you still need to communicate with libffi and embed all that into LLVM IR. Mechanically generating fat closure pointers won't be trivial, but should be feasible.\n- That makes sense, I'll look into using this method. Thanks\n- I imagine you've solved this in the meantime, just a note to say I just completed this in a clean room and see that I ended up with what @delnan indicated this is how Rust works. Let me know, happy to .\n- @FrankC. I've left this project long ago, but I'd love to see it your work!\n- OK, I'll probably setup a gist to go through it with pseudo code or something. It is a combo of compile time constructs along with RTL support but does exploit the notion of a function reference that has storage for arguments which will keep accumulating to the function signature threshold before invoking the function. This is how I get the curry/partial capability. As a side note I use a slight variation for lambda/closures.","metadata":{"transformedAt":"2026-08-18T18:32:17.838Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":5,"totalLines":141,"estimatedTokens":1430}}888{"id":"stack-5751150","source":"stackoverflow","questionId":5751150,"title":"How is scala.util.parsing.ast.Binders supposed to be used?","tags":["scala","compiler-construction","binding","functional-programming","parser-combinators"],"text":"Title: How is scala.util.parsing.ast.Binders supposed to be used?\nTags: scala, compiler-construction, binding, functional-programming, parser-combinators\nSource: Stack Overflow\n\nQuestion:\nI am currently implementing a small compiler in Scala and while I was doing the component for context analysis I discovered the trait `Binders` in package `scala.util.parsing.ast` (I am using Scala 2.9 RC), which is Documented to enable name binding during parse time. That sounds very interesting and I have been googling around a lot, but I still have no clue how to use it. While I am of course able to let my abstract syntax derrive from `Binders` I dont see how to proceed from there. Are there any examples of the usage on the net?\n\n========================================\n\nTop Answer:\nYou're not supposed to use it. In fact, it has recently been deprecated.\n\nSee\ndeprecation candidate: scala.parsing.ast\n\n========================================\n\nCode:\n```text\nBinders\n```\n\n```text\nscala.util.parsing.ast\n```\n\n```text\nBinders\n```\n\n========================================\n\nComments:\n- I guess you are right. The fact that i could not find any library that uses `Binders` made me a bit suspicious in the first place. It just sounded so interesting.","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":34,"estimatedTokens":311}}889{"id":"stack-55743494","source":"stackoverflow","questionId":55743494,"title":"Where does SWIFT ABI come into picture?","tags":["ios","swift","compiler-construction","llvm","abi"],"text":"Title: Where does SWIFT ABI come into picture?\nTags: ios, swift, compiler-construction, llvm, abi\nSource: Stack Overflow\n\nQuestion:\nThe title might not be sufficient to give an overview of the question context. So here goes the description:\n\n**SWIFT Compilation process**\nThe Swift compiler undergoes the following steps to compile a Swift file\n\nhttps://i.sstatic.net/sfdzn.png \n\nAs per Apple, \n\n IR generation (implemented in lib/IRGen) lowers SIL to LLVM IR, at\n which point LLVM can continue to optimize it and generate machine code.\n\n***Query Number 1 -*** We all know that compiler turns our source code to Assembly Language and Assembler(mostly embedded in OS, at least Swift compiler doesn't have an assembler in it) converts that into Machine code. So as per the quoted statement above, the LLVM in compiler changes the LLVM IR to machine code. ***So if this is the case then the assembler would be having no role to play in a Swift program and execution?***\n\n***Query Number 2 -*** LLVM in Swift changes the LLVM IR directly to machine code. So that means my compiled executable binary has machine code, not assembly code. And as per my understanding machine code doesn't need any specific calling convention as an assembly language has, and ABI is all about calling conventions, memory layout representations, etc via which the communication between two binaries is defined. ***So where does ABI comes in the picture then because the binary executable already has the machine code?***\n\nSo is there something I am missing or Apple has kept it quite abstract?\n\n========================================\n\nCode:\n```text\nself\n```\n\n========================================\n\nComments:\n- The difference between assembly and machine code is that assembly is a text format and machine code is binary. It has nothing to do with calling conventions. Also where did you get that assemblers are \"mostly embedded in OS\"?\n- did you look at en.wikipedia.org/wiki/Application_binary_interface ?\n- @sepp2k Yeah, we agree on the fact that machine code has nothing to do with calling conventions. So when iOS apps executable binary gets installed in the OS, why would it require an ABI to communicate with Swift standard library in OS. \"mostly embedded in OS\", at least this happens with iOS I can guarantee.\n- @Sulthan I did go through that, but I am trying to visually interpret the concepts of ABI correlating with the communication between iOS apps executable binary and the binary of swift standard library embedded in iOS. Means why do u even need ABI if iOS apps executable contains machine code?\n- @G.Abhisek How is function declaration stored in machine code? function names, parameter names, default values, attributes? How does the memory layout of a struct looks like? If a Swift application has to call a function from the standard library (runtime), it has to know how such things are stored. That's the ABI stability of runtime. A library is not just a set of machine code instructions.\n- @G.Abhisek I'm saying the difference between assembly and machine code has nothing to do with calling conventions. Whether LLVM produces assembly that is fed into an assembler or whether it emits machine code directly, makes absolutely no difference with regards to calling conventions (or really with regards to anything).\n- Also please read github.com/apple/swift/blob/master/docs/&hellip; which provides a nice explanation.\n- @Sulthan So putting my questions straight: a) Does Swift compiler convert source code to Machine code or Assembly? b) So who respects the ABI conventions, the Machine code or Assembly? Pardon If I am confused\n- Assembly is just another type of intermediate code and it's completely irrelevant for ABI. ABI is only necessary during runtime. It's a communication layer for runtime. Or in other words, assembler is a low level programming language. It has probably no place in the Swift compiler, maybe only as intermediate point of translation.\n- @sepp2k en.wikipedia.org/wiki/Application_binary_interface , says that calling convention is quite an important part of ABI stability. So if the compiled version of my source code(whether the result is Machine code or assembly) does not respect it, so how can I say its ABI stable?\n- @Sulthan I think I got this, probably I was confusing between runtime and the applications executable binary. One more query, ABI is only necessary during runtime, i.e when the programs are getting executed in the processor, ABI defines how the processor would store data types. How function calls would be laid out in the stack frame, etc. Am I correct?\n- @G.Abhisek It is also needed during linking, e.g. when linking against a static library. The compiler has to know which functions are inside the static library and you need stable ABI for that too. That's why you get an error \"Cannot import module compiled by another compiler version\" in current Swift compilers.","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":45,"estimatedTokens":1230}}890{"id":"stack-16311867","source":"stackoverflow","questionId":16311867,"title":"How \"Hello World\" gets printed? - a complete life cycle","tags":["java","architecture","compiler-construction","operating-system"],"text":"Title: How \"Hello World\" gets printed? - a complete life cycle\nTags: java, architecture, compiler-construction, operating-system\nSource: Stack Overflow\n\nQuestion:\nI took compiler, operating system, computer architecture course in graduate school. But I want to see those concepts in a simple but real example (not too theoretical). HelloWorld is a good choice for simple example. \n\nNot only the life cycle of this little program is interesting, but also knowing how JVM, OS and Architecture works can help us to become a better programmer. \n\nIn short, my question is: do I describe the steps correctly and completely?\n\nIn detail, I need your help with the following:\n\n- Point out any part that I am wrong\n\n- I'm sure I miss a lot of valuable points, so please add any point that makes sense in the life cycle.\n\nWhat I want to know is how HelloWorld goes through each layer, and then come back from it. Assume this program runs on an **Intel** CPU with **Ubuntu**. \n\n**I will keep this post updated by combining good answers until one answer is finally accepted.** \n\nThe following is THE HelloWorld program that everybody knows. \n\nIt is compiled to bytecode like the following. \n\nThe bytecode is not readable, but we can use `javap -classpath . -c HelloWorld` to see mnemonics like the following. \n\nThen it gets loaded, linked and initialized in JVM. \n\nSince it has only one thread, let's assume it is the the left thread in the following JVM run-time data area. \n\nJVM threads are user-level threads, so it will be mapped to kernel. In Ubuntu, it is one-to-one mapping like the following:\n\nNow JVM makes the x86 instructions? (update) \n\nWhat is operating system's role for this particular program? \n\nWhat is next in Architecture?\n\nFetch instruction, decode, execute, memory access, write back in 5-steps MIPS. \n\nReferences: \n\n- OS basics\n\n- Diagrams\n\nThis could be an extremely difficult question. Unlike questions of how to use a certain API, this requires understanding of almost everything.\n\n========================================\n\nCode:\n```text\njavap -classpath . -c HelloWorld\n```\n\n========================================\n\nComments:\n- The JVM makes x86 instructions.\n- Cool, but I would say its home is somewhere else.\n- I believe we have a wiki or something somewhere around here. If someone with enough rep comes by, they have the ability to move it there.","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":62,"estimatedTokens":592}}891{"id":"stack-8525760","source":"stackoverflow","questionId":8525760,"title":"global offset table structure","tags":["linux","compiler-construction","kernel","elf"],"text":"Title: global offset table structure\nTags: linux, compiler-construction, kernel, elf\nSource: Stack Overflow\n\nQuestion:\nHow does structure of global offset table(ELF) look. I am trying to understand dynamic linking. Can any one tell how got data structure looks. I somehow never found it online.\n\n========================================\n\nComments:\n- how can you see where are they allocated? Can I read them with HEX editor? thanks.\n- you can read it from a debugger, with an hex editor I think you will read all zeros since is at runtime that is filled with real data.","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":12,"estimatedTokens":143}}892{"id":"stack-5280659","source":"stackoverflow","questionId":5280659,"title":"What is the size of a program using LLVM/CLANG for a custom bytecode VM?","tags":["compiler-construction","clang","llvm","bytecode","vm-implementation"],"text":"Title: What is the size of a program using LLVM/CLANG for a custom bytecode VM?\nTags: compiler-construction, clang, llvm, bytecode, vm-implementation\nSource: Stack Overflow\n\nQuestion:\nI'm evaluating different possibilities for a custom VM, and I left out LLVM from another question. Since I'm still working on the evaluation of embedded language VMs I can't test/check this myself for now. I would like to know the following information about LLVM/CLANG:\n\n- Is it supported well on Windows? Or Solaris? (cross-platform is a plus for me)\n\n- If I want to write my own/custom VM with a C-like language, what would I need to include in the project? (LLVM/CLANG sections or components, etc)\n\n- I would keep the compiler separate from the VM for obvious reasons (not writing an interpreter). What would be the size of the required components? Could I build them 'in' the program instead of dynamically linking to them?\n\n- Can I avoid JIT? I would like to have a bytecode VM which does not necessarily translate to native code. This would help when JIT is not supported on the platform (ex. systems with restrictive memory permissions that do not allow wx/rwx memory mappings).\n\nI know the clamav antivirus for instance uses bytecode backed by LLVM/clang to support dynamic/runtime signatures. However I do not know if there is an existent facility to implement this and/or tutorials or documentation which guides you through the process of implementing such a thing.\n\nThanks! :)\n\n========================================\n\nComments:\n- Thanks, I didn't this answer earlier than today...!","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":23,"estimatedTokens":395}}893{"id":"stack-1742171","source":"stackoverflow","questionId":1742171,"title":"Is there a C header parser tool for wrapper generation like gccxml?","tags":["c","compiler-construction","parsing","wrapper"],"text":"Title: Is there a C header parser tool for wrapper generation like gccxml?\nTags: c, compiler-construction, parsing, wrapper\nSource: Stack Overflow\n\nQuestion:\nI need to write a few c header wrappers for a new programming language and would like something like gccxml but without the full dependency on gcc and the problems it gives on a windows system.\n\nJust needs to read C not C++. Output in any format is okay as long it is fully documented.\n\nNeed it for Curl, SQLite, GTK2, SDL, OpenGL, Win32 API and C posix API's on Linux/Solaris/FreeBSD/MacOSX.\n\n========================================\n\nTop Answer:\nSee our SD C Front End for DMS. Full C parsing, symbol table construction, post parsing dump of any information you like. Can dump code and symbol tables in XML format.\n\n========================================\n\nComments:\n- Try to use ABI Compliance Checker tool as a parser of C header files. See this answer for instructions on how to do this.\n- Wow, haven't seen this answer I gave since I wrote it 4 years ago. Added standard SO indication of self-interest, *our*, that was not a requirement when I first gave this answer.","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":283}}894{"id":"stack-18820624","source":"stackoverflow","questionId":18820624,"title":"Compilers: How to parse function calls and function definitions","tags":["python","parsing","compiler-construction","interpreter","frontend"],"text":"Title: Compilers: How to parse function calls and function definitions\nTags: python, parsing, compiler-construction, interpreter, frontend\nSource: Stack Overflow\n\nQuestion:\nUpfront, I'm making an interpreter in Python, not an actual compiler that compiles to machine code. I've been skimming quite a few compiler construction guides lately and I understand the basics of generating tokens for your parser and building a syntax tree to evaluate arithmetic expressions, but I don't quite understand how to parse expressions with function calls inside them, things like\n\nFig. (a)\n\n```\n1 + pow(1, 1)\n```\n\nor how to parse lines when the user is defining a function like this\n\nFig. (b)\n\n```\nfunction newFunction( someArgs ){\n ... some magic ...\n}\n```\n\nIn Fig. (a), how should I tokenize this expression? After reading the reserved word \"pow\" should I grab everything up to the closing parenthesis and pass that to a parser? or should I include \"pow\", \"(\", \"1\", \"1\", and \")\" each as seperate tokens and add them to my parse tree?\n\nIn Fib. (b) I don't have any idea where to even start when it comes to compiling function definitions. Any information to put me in the right direction would be appreciated.\n\n**Edit:** I'm using a Backus-Naur form grammar:\n\nS ::= expression\n\nexpression ::= term | term ([+-] term)+\n\nterm ::= factor | factor ([*/] factor)+\n\nfactor ::= number | ( expression )\n\nnumber ::= [0-9]+\n\n========================================\n\nTop Answer:\nAnother poster suggests adding the names of functions to your grammar.\n\nThat works for toy languages, but not for practical ones where there may be huge libraries as well as large sets of user defined functions.\n\nYou can handle the latter by adding function calls to the BNF,\nin a way that leaves the function name out of the grammar:\n\n```\nS ::= expression\n\nexpression ::= term | term ([+-] term)+\n\nterm ::= factor | factor ([*/] factor)+\n\nfactor ::= number | ( expression ) | identifier | functioncall\n\nfunctioncall ::= identifier [(] arguments [)]\n\narguments ::= empty | arguments \n\narguments ::= expression | arguments [,] expression\n\nnumber ::= [0-9]+\n\nidentifier ::= [a-z]+\n```\n\nNow your parser can pick up function calls. (Left out of the grammer is a way to *define* functions... but that's just more syntax which I leave it for you to add).\n\nThe price for this is that after parsing, something has decide for each function name, precisely which bit of code it represents. You will need a symbol table to do this.\nThat's another question.\n\n========================================\n\nCode:\n```text\n1 + pow(1, 1)\n```\n\n```text\nfunction newFunction( someArgs ){\n   ... some magic ...\n}\n```\n\n```text\n<digit>   ::= \"0\"|\"1\"|...|\"9\"\n<integer> ::= <digit>*\n<expr>    ::= <integer> | <add> | <pow>\n<add>     ::= \"add(\" <expr> \",\" <expr> \")\"\n<sub>     ::= \"sub(\" <expr> \",\" <expr> \")\"\n<pow>     ::= \"pow(\" <expr> \",\" <expr> \")\"\n```\n\n```text\nadd(1,2)\n```\n\n```text\npow(2,add(2,1))\n```\n\n```text\nadd(1)\n```\n\n```text\nadd\n```\n\n```text\nS ::= expression\n\nexpression ::= term | term ([+-] term)+\n\nterm ::= factor | factor ([*/] factor)+\n\nfactor ::= number | ( expression ) | identifier |  functioncall\n\nfunctioncall ::= identifier [(]  arguments [)]\n\narguments ::= empty | arguments \n\narguments ::= expression |  arguments [,] expression\n\nnumber ::= [0-9]+\n\nidentifier ::= [a-z]+\n```\n\n========================================\n\nComments:\n- Wow... I can't believe it never occurred to me to add the reserved words to my grammar. Let's say I did this for all predefined functions in my program, by which I mean adding them to my grammar table so that they can be recursively defined. How would I go about adding function definitions that the programmer using my language creates? should I dynamically add those to my grammar somehow or store them somewhere else?\n- In most languages, a function essentially is a pointer to a sequential list of statements/expressions. How you manage this is up to you. The simplest might be to store the name of the function in a symbol table as a pointer to this list, with your interpreter knowing to execute the list of instructions when a token with that id is found in the source code.\n- What about function calls such as `list_of_funcs[0]()`?\n- If you allow a special expression to compute the function to be called, then you modify the grammar accordingly. To handle your specific example first you need to extend the definition of factor as factor = ... | factor '[' expression ']' ; Then you change fuctioncall: functioncall = factor '[' arguments ']'. BNF is pretty expressive.","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":139,"estimatedTokens":1140}}895{"id":"stack-4148627","source":"stackoverflow","questionId":4148627,"title":"Is IronScheme interpreted or compiled? Does it benefit from .NET Framework optimizations?","tags":[".net","optimization","compiler-construction","scheme","ironscheme"],"text":"Title: Is IronScheme interpreted or compiled? Does it benefit from .NET Framework optimizations?\nTags: .net, optimization, compiler-construction, scheme, ironscheme\nSource: Stack Overflow\n\nQuestion:\nIn the book *\"IronPython in Action,\"* the author states that IronPython, unlike CPython, benefits from certain optimizations, both in the JIT and in the framework itself, that CPython cannot take advantage of. Consequently, IronPython is potentially faster than CPython is, especially for multithreading scenarios. \n\nDoes **IronScheme** benefit from such optimizations? Is it an interpreter (not a compiler), and is it an interpreter because that's the nature of Lisp, that it must be interpreted to provide the Lisp-like flexibility? If it is an interpreter, can it still benefit from optimizations in the jitter?\n\n========================================\n\nCode:\n```text\nironscheme.boot.dll\n```\n\n```text\nvectors\n```\n\n```text\nbyte-vectors\n```\n\n```text\nbinary-ports\n```\n\n```text\nhash-tables\n```\n\n```text\nobject[]\n```\n\n```text\nbyte[]\n```\n\n```text\nStream\n```\n\n```text\nHashtable\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":9,"totalLines":47,"estimatedTokens":270}}896{"id":"stack-20634639","source":"stackoverflow","questionId":20634639,"title":"How does the compiler determine when is it safe to RVO?","tags":["c++","compiler-construction","return-value-optimization"],"text":"Title: How does the compiler determine when is it safe to RVO?\nTags: c++, compiler-construction, return-value-optimization\nSource: Stack Overflow\n\nQuestion:\nHow does the compiler determine when it is safe to RVO? And no, I don't mean the rvalue, but the lvalue - if I understand correctly RVO works by \"forwarding\" the target address to the method, so it returns into the target address instead of the address of a temporary and then copy/assign to the target.\n\nBut how does the compiler know it is safe to perform a RVO? What if the lvalue already has some data in it, including dynamically allocated resource? A RVO in such a case may result in a leaked resource. Mayhaps there are some rules which specify whether it is applicable to perform the optimization or stick to using copy or assignment?\n\n========================================\n\nTop Answer:\nReturn value optimization is a particular case of copy elision. It may occur in the following situation as described by the standard:\n\n in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv- unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value\n\nThere is no reason this should result in a memory leak. If the class performs some dynamic allocation in its constructor, this will happen when the object is constructed directly into the function's return value.\n\nIn response to your comment (where `foo1` and `foo2` both construct `T` objects and return them):\n\n```\nT a = foo1();\na = foo2();\n```\n\nWe're not only looking at RVO here, but another kind of copy elision that occurs when attempting to construct an object from a temporary.\n\nIn the first line, two copies/moves can be elided:\n\n- Returning the constructed object from `foo1`\n\n- Copying the returned object into `a`\n\nThat is, the object constructed in `foo1` can be directly created in the location of `a`. If the constructor dynamically allocates some object, that will only be done once, for the `a` object.\n\nIn the second line, a single copy/move can be elided - only the return from the function. So the object that `foo2` constructs will be created directly in the return value of the function, then it will be *copy/move assigned* into `a`. Copy/move assignments aren't elided.\n\nIt is then up to the copy/move assignment operator to ensure that the original allocated resource is discarded safely and the only remaining resource is the one that was created inside `foo2`.\n\n========================================\n\nCode:\n```text\nThing thing = make_thing();\n```\n\n```text\nthing = make_thing();\n```\n\n```text\nthing\n```\n\n```text\nthing\n```\n\n```text\nT a = foo1();\na = foo2();\n```\n\n```text\nfoo1\n```\n\n```text\nfoo2\n```\n\n```text\nT\n```\n\n```text\nfoo1\n```\n\n```text\na\n```\n\n```text\nfoo1\n```\n\n```text\na\n```\n\n```text\na\n```\n\n```text\nfoo2\n```\n\n```text\na\n```\n\n```text\nfoo2\n```\n\n========================================\n\nComments:\n- RVO cannot result in a leaked resource (for a sane type). The object is constructed in the same place the called expects the return value to be. The exact rules may depend on the implementation though, they are not mandated by the standard.\n- How can it result in a leaked resource? It behaves like if you passed the variable you're copying to as a reference parameter\n- If it is a \"constructor\" type of function which allocates dynamic resource to a member pointer, running that function twice on the address of the same object will allocate the resource twice, I assume a \"constructor type\" function will directly allocate instead of checking if the pointer already allocates. Which would make sense for a \"constructor type\" function. So the first resource will leak...\n- Why would the constructor be called twice?\n- @sftrabbit - well, I explicitly put it in \"\" and called it a \"constructor\" type of function, not a constructor per se. Let's say ` T a = foo1(); .... a = foo2()` - it is safe for `foo1` to apply RVO, but if `foo2` applies it, dynamic resources will leak.\n- @user2341104 But it is only constructed once, directly into the function's return value.\n- see my comment to the question\n- @user2341104 Response in my answer.\n- So, there is a rule \"rvo can only be applied to \"new\" objects\" but how does the compiler determine when an object is new? If it is only instantiated and nothing else? Does that mean that even operations that do not technically obstruct rvo will disable the optimization if used with the object between the instantiation and point of rvo?\n- @user2341104: The rules for when copies and moves can be elided are quite complicated, but that's a reasonable simplification. The compiler can see that the object is new because the function call is part of its initialiser. If it's being reassigned (as in my second example), then (in general) RVO is used to initialise a temporary to assign from, as I described.\n- So, `T t = foo()` will use RVO, but `T t; t = foo()` will not (with nothing else happening in between the two statements)? In other words, does the compiler need the assignment to be part of the instantiation or maybe it has other ways of determine if assignment is the first method being called after the point of instantiation?\n- @user2341104: The first case isn't assignment - it's initialisation. RVO can be used for initialisation, but not (in general) for assignment.\n- @user2341104 It will (probably) use RVO, but it won't elide the copy/move assignment from the return value to `t`. The reason that assignment is not elided is because some work is needed to ensure the object moves from one state to another without leaking. A copy/move can only be elided when it's during construction, because nothing has yet been allocated.\n- Note that the assignment case will still benefit from move constructors, as the unnamed temporary can be moved from. @user2341104:\n- @MikeSeymour - but is there such a difference, you say it is not assignment but initialization, but it still uses the assignment operator, does the compiler distinguish between the two contexts of the assignment operator? Isn't the assignment sill an \"initialization\" if it is used on a non-initialized object, but not on the line of its instantiation?\n- @user2341104 It's not the assignment operator. In a declaration, like `T t = ...;` the `=` is part of the initialisation, and not the assignment operator. The assignment operator can only appear inside an expression, and `T t = ...;` is not an expression.\n- @sftrabbit - but it uses the assignment operator (in the case of no rvo)? Also, I wonder how does the compiler even know what the `=` operator is doing, for primitive types - sure, but rvo doesn't really make sense there, but for user types, the `=` operator can do anything the developer wishes.\n- @user2341104 It will never use the assignment operator - it uses the copy/move constructor. Don't let the `=` fool you. In this case, `T t = foo();` is equivalent to `T t(foo());`. You can't call the assignment operator on an object that isn't constructed yet. On your second point, you're really asking about the copy/move constructors - and the compiler doesn't care what they do. The compiler can elide them regardless. It is expected that a responsible C++ programmer will not implement copy/move constructors with side effects.\n- @sftrabbit - let me make sure I got this right. `T t1(t2)` uses the copy constructor, and so does `T t1 = t2` and NOT the assignment operator?\n- @user2341104 Correct.\n- Hmm, this seems counter-intuitive, I expected that typing the assignment operator would result in an assignment regardless of the context.\n- @user2341104: Indeed, C++ syntax is often counter-intuitive and context-dependent, as a result of evolving from older languages over several decades. `=` is only an assignment operator in an assignment expression. The same token is also used in a declaration to indicate initialisation (and in other contexts like function declarations and using directives with different meanings again).\n- There seems to be some confusion here as to what RVO is. RVO is independent of what the caller is doing; it occurs in the function doing the return. (Other optimizations, like the ones described in the section on initialization, do apply.)\n- @user2341104 It's *not* the assignment operator. There are several symbols in C++ which can be both operators and punctuation: `=`, `,`, ``, '(' and `)` at least. In the case of a declaration `=` is not an operator, but punctuation (except in cases like `T t = a = b;`, where the first `=` is punctuation, but the second an operator).\n- @MikeSeymour `=` has signified both assignment and initialization since the earliest days of C. Without user defined constructors, of course, the distinction is really not very significant; the compiler generates exactly the same code for `int i = 42;` and `int i; i = 42;`, even though the `=` signifies initialization in the first case, and assignment in the second. But the two cases have been distinct, grammatically, since the very beginnings of C.\n- @JamesKanze: Yes, I'm sure that aspect of C has always been the same, and I'm fairly sure I didn't say otherwise. I just said that it's (also) the case in modern C++ (due to its evolution from C), which is rather more relevant to this question.","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":136,"estimatedTokens":2356}}897{"id":"stack-48569578","source":"stackoverflow","questionId":48569578,"title":"Converting an AST to bytecode","tags":["c","compiler-construction","abstract-syntax-tree","bytecode","interpreter"],"text":"Title: Converting an AST to bytecode\nTags: c, compiler-construction, abstract-syntax-tree, bytecode, interpreter\nSource: Stack Overflow\n\nQuestion:\nSo I'm writing a little interpreter in C at the moment for a language I have created (which is pretty similar to Python). I have written the lexer and parser and currently my program outputs an AST, an now I am attempting to turn this AST into bytecode. Currently my algorithm traverses the AST (depth-first) and can generate bytecode for simple arithmetic, and now I am trying to implement if statements.\n\nI cannot copy all my code in here because it's a pretty large amount of code, but currently the program takes an AST which might look something like\n\n```\nADD\n|-- 1\n|-- MUL\n |-- 2\n |-- 3\n```\n\nand turns this into\n\n```\nLOAD 1 //the real code doesn't put the value here, but a number representing the position of this value in an array\nLOAD 2\nLOAD 3\nMUL\nADD\n```\n\nThis is easy for simple expressions but I really don't know how to generate the bytecode for an if statement. I know that I will have to jump to the else clause if the comparison is false, and also jump from the end of every if/else if block, but how do I deal with this if the jump is more than 256 bytes of bytecode?\n\n========================================\n\nCode:\n```text\nADD\n|-- 1\n|-- MUL\n    |-- 2\n    |-- 3\n```\n\n```text\nLOAD 1 //the real code doesn't put the value here, but a number representing the position of this value in an array\nLOAD 2\nLOAD 3\nMUL\nADD\n```\n\n```text\nFARJUMP\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\nc\n```\n\n```text\nd\n```\n\n```text\nuint8_t\n```\n\n```text\n(a<<24) + (b<<16) + (c<<8) +d\n```\n\n```text\nBACKFARJUMP\n```\n\n========================================\n\nComments:\n- Why is your jump distance limited to 256?\n- @usr2564301 because it is bytecode, an each byte is 8bits, allowing 256 bytes to jump over.\n- If i use multiple bytes that will waste space for jumps less than 256 bytes\n- Not necessarily. Some bytecode are 16 bits.\n- @BasileStarynkevitch but then again, if I have less than 256 instructions, which I do, if I use 16bit bytecode I will waste at least 8 bits per instruction\n- You are then also limiting yourself to <256 items for all other items – number of literals and variables, number of functions, and so on. I think you may be taking the \"byte\" in *byte-code* a bit too literal.\n- @usr2564301 is there an easy way to make it variable, kind of like unicode?\n- ... If you *must*, then \"less than 256\" should be -128..127. Consider loops! And for larger distances, add more jumps in between. (Whose \"size\", in turn, you then must include in yet other jumps. Enjoy working that out.)\n- @DanielGee: yes, you can have variable-length opcodes or operands. For instance a relative distance of `0` is clearly useless, so you can have the range -128..127 excluding 0, and treat 0 as meaning \"read two more bytes to get a 16 bit number\". If the first byte of that 16 bits is zero, there was no point in encoding it this way, so you could treat that as a flag meaning \"read 3 or 4 bytes to get a 24 or 32 bit number\" (depending on whether you think 24 bit numbers are good), and so on. Or, you can just have FARJUMP, etc.\n- You may use an existing bytecode as inspiration…\n- *Engineering a Compiler* by K. Cooper,‎ and L. Torczon is a great addition to your list too!","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":10,"totalLines":94,"estimatedTokens":828}}898{"id":"stack-37174361","source":"stackoverflow","questionId":37174361,"title":"How to modify all items of a crate in a compiler plugin?","tags":["compiler-construction","rust","rust-compiler-plugin"],"text":"Title: How to modify all items of a crate in a compiler plugin?\nTags: compiler-construction, rust, rust-compiler-plugin\nSource: Stack Overflow\n\nQuestion:\nI'm trying to build a syntax extension that expands an attribute into calls. Before:\n\n```\n#[flame]\nfn flamed() {\n ..\n}\n```\n\nAfter:\n\n```\nfn flamed() {\n flame::start_guard(\"flamed\");\n ..\n}\n```\n\nThis already works. However, I'd also like it to work if I have the `#[flame]` attribute at the crate level (like `#![flame]`). Is this possible and if so, how?\n\n========================================\n\nCode:\n```text\n#[flame]\nfn flamed() {\n    ..\n}\n```\n\n```text\nfn flamed() {\n    flame::start_guard(\"flamed\");\n    ..\n}\n```\n\n```text\n#[flame]\n```\n\n```text\n#![flame]\n```\n\n```text\nfn flame_item(i: &Item) -> Item {\n    let base = i.clone();\n    Item {\n        node: match i.node {\n            ItemKind::Mod(ref m) =>\n                ItemKind::Mod(flame_mod(m)),\n            ItemKind::Trait(unsafety, ref generic, ref bounds, ref tis) =>\n                ItemKind::Trait(unsafety,\n                                generic.clone(),\n                                bounds.clone(),\n                                flame_items(tis)),\n        .. // other item types as usual: items, traitimpls, implitems\n\n            _ => return base\n        },\n        ..base\n    }\n}\n\nfn flame_mod(m: &Mod) -> Mod {\n    Mod {\n        inner: m.inner,\n        items: m.items.iter().map(|i| P(flame_item(i))).collect()\n    }\n}\n\nfn flame_items(items: &[TraitItem]) -> Vec<TraitItem> {\n    items.iter().map(flame_trait_item).collect()\n}\n```\n\n========================================\n\nComments:\n- What exactly do you expect the crate attribute to do? Insert the call into every function of the crate?\n- Did you try catching `ItemKind::Mod` in github.com/llogiq/flamer/blob/&hellip; and iterating over its contents (recursively)?\n- @Lukas Kalbertodt: exactly.\n- @huon: No I didn't try that, because I thought it would walk automatically.\n- The compiler walks the AST to find all the things with syntax extension attributes, and passes each annotated one into the syntax extension, but doesn't do anything automatically for the children (unless those children have attributes, of course). If the syntax extension wants to handle children without attributes on each one, it has to do the walking itself.","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":5,"totalLines":90,"estimatedTokens":579}}899{"id":"stack-9853752","source":"stackoverflow","questionId":9853752,"title":"Building an AST using Bison","tags":["c++","compiler-construction","bison","abstract-syntax-tree"],"text":"Title: Building an AST using Bison\nTags: c++, compiler-construction, bison, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI am working with Bison to build an AST for a compiler I am writing. What is the best way to build up the nodes in the AST? My question might be more clear with an example.\n\nGiven the following snippet:\n\n```\nfield\n : modifier type TOK_IDENT TOK_SEMICOLON\n {\n // I want to return a pointer to a node of type Field\n // i.e. $$ = new Field(name, isVisible, isStatic, type);\n }\n ;\n\nmodifier\n : visibility_opt static_opt\n {\n // Should I make the new Field here and pass it up?\n // Or a new type that contains both vis and static options? \n }\n ;\n\nvisibility_opt\n : /* default */ { $$ = true; }\n | TOK_PUBLIC { $$ = true; }\n | TOK_PRIVATE { $$ = false; }\n ;\n\nstatic_opt\n : /* default */ { $$ = false; }\n | TOK_STATIC { $$ = true; }\n ;\n```\n\nIn the above example I want the field rule to return a Field node, but I need some of the attributes of the modifier rule that will be passed up during parsing (i.e. these are synthesized attributes).\n\nI can think of two ways to do this without changing the grammar.\n\n- Make the non-terminal modifier have type Field, create the new Field here, fill in what I can, and pass it up to field to fill in the rest.\n\n- Let modifier have its own type that holds two bool values and pass this up extracting the data when creating the new Field in the field rule.\n\nIn situations like this what is the preferred way to go?\n\n========================================\n\nCode:\n```text\nfield\n  : modifier type TOK_IDENT TOK_SEMICOLON\n    {\n      // I want to return a pointer to a node of type Field\n      // i.e. $$ = new Field(name, isVisible, isStatic, type);\n    }\n  ;\n\nmodifier\n    : visibility_opt static_opt\n    {\n      // Should I make the new Field here and pass it up?\n      // Or a new type that contains both vis and static options?      \n    }\n  ;\n\nvisibility_opt\n  : /* default */ { $$ = true; }\n  | TOK_PUBLIC    { $$ = true; }\n  | TOK_PRIVATE   { $$ = false; }\n  ;\n\nstatic_opt\n  : /* default */ { $$ = false; }\n  | TOK_STATIC    { $$ = true; }\n  ;\n```\n\n```text\nstatic struct { boolean vis_opt; boolean static_opt; } mod;\n\nfield\n  : modifier type TOK_IDENT TOK_SEMICOLON\n    {\n      $$ = new Field(..., mod.vis_opt, mod.static_opt, ...);\n    }\n  ;\n\nmodifier\n    : visibility_opt static_opt\n    {\n      mod.vis_opt = $1;\n      mod.static_opt = $2;\n    }\n  ;\n\nvisibility_opt\n  : /* default */ { $$ = true; }\n  | TOK_PUBLIC    { $$ = true; }\n  | TOK_PRIVATE   { $$ = false; }\n  ;\n\nstatic_opt\n  : /* default */ { $$ = false; }\n  | TOK_STATIC    { $$ = true; }\n  ;\n```\n\n========================================\n\nComments:\n- I would personally go with alternative two. Mostly because `modifier` isn't really a `field` but something separate.\n- Yeah I chose to go with the second way for now letting modifier have type std::pair. It seems like it depends though because in another case it made more sense to go with the first approach...","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":115,"estimatedTokens":749}}900{"id":"stack-913871","source":"stackoverflow","questionId":913871,"title":"How to Represent Classes in an Abstract Syntax Tree Based Interpreter","tags":["java","php","compiler-construction","interpreter","abstract-syntax-tree"],"text":"Title: How to Represent Classes in an Abstract Syntax Tree Based Interpreter\nTags: java, php, compiler-construction, interpreter, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI have read the related questions, but none of them appears to address the question directly. I am working on writing a PHP script interpreter. I have the AST generating proper nodes for everything except classes. Handling classes is a bit different than handling functions, so I am looking for how to handle classes that are standalone, and that extend other classes.\n\nI have looked at ANTLR, but I can't afford the overhead as this is for an embedded platform. What I am looking for is the theory conceptually behind classes in ASTs so they can be executed by the executor portion of the interpreter. Good links with specific answers to this issue are definitely appreciated.\n\n========================================\n\nTop Answer:\nPerhaps you need some structures for interpretation that are a bit more removed from the source grammar? I don't know much about PHP, but it sound like you're really asking how structure the program model in a way that's convenient for interpretation. I'd regard an AST generated via ANTLR as a bit too close to the source for what you want.\n\n========================================\n\nCode:\n```text\nClass_def ::= Class_mod CLASS_NAME extends:CLASS_NAME? implements:INTERFACE_NAME* Member* ;\nClass_mod ::= \"abstract\"? \"final\"? ;\n\nInterface_def ::= INTERFACE_NAME extends:INTERFACE_NAME* Member* ;\n\nMember ::= Method | Attribute ;\n\nMethod ::= Signature Statement*? ;\nSignature ::= Method_mod is_ref:\"&\"? METHOD_NAME Formal_parameter* ;\nMethod_mod ::= \"public\"? \"protected\"? \"private\"? \"static\"? \"abstract\"? \"final\"? ;\nFormal_parameter ::= Type is_ref:\"&\"? var:Name_with_default ;\nFormal_parameter ::= Type is_ref:\"&\"? var:Name_with_default ;\nType ::= CLASS_NAME? ;\nName_with_default ::= VARIABLE_NAME Expr? ;\n\nAttribute ::= Attr_mod vars:Name_with_default* ;\nAttr_mod ::= \"public\"? \"protected\"? \"private\"? \"static\"? \"const\"?  ;\n```\n\n========================================\n\nComments:\n- Right, but that leads to re-interpreting the tokens for that object each time the class is instantiated right? I guess that is the difference between Google's V8 and other JavaScript implementations, and that I need to figure it out. Thanks for the info.\n- Cool, I'll take a look at it, one thing that I have to watch is that it is compatible with Dalvik bytecode, I've found that most java .class files are, but some are not\n- JavaCC outputs just a couple of Java class files. These would get compiled with your regular compiler. FreeCC has better support for generics, so I would use that since your coding Java 1.5.\n- Thanks for the help, I'll see if I can use it\n- Thanks Paul, I'm still looking at using PHC for C / C++ based clients, but how can I use it on Android ( Java / Dalvik )?\n- I'll assume native apps aren't possible? Then maybe as web service using the XML interface?","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":46,"estimatedTokens":747}}901{"id":"stack-65288033","source":"stackoverflow","questionId":65288033,"title":"How TVM is different from MLIR?","tags":["deep-learning","compiler-construction","apache-tvm"],"text":"Title: How TVM is different from MLIR?\nTags: deep-learning, compiler-construction, apache-tvm\nSource: Stack Overflow\n\nQuestion:\nAs per my understanding, both TVM and MLIR are used as compiler infrastructure for deep learning neural networks. Is my understanding correct?.\n\nAnd Which would be better if we are building a compiler for custom hardware that runs deep learning inferences?\n\n========================================\n\nComments:\n- tvm appears to be meant for an Amazon web service thingy, rather than Apache TVM.\n- links can go dead. please copy over relevant info from the link","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":14,"estimatedTokens":147}}902{"id":"stack-39959328","source":"stackoverflow","questionId":39959328,"title":"Automatic Code Optimization Techniques","tags":["java","optimization","compilation","compiler-construction","compiler-optimization"],"text":"Title: Automatic Code Optimization Techniques\nTags: java, optimization, compilation, compiler-construction, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nI am working on a project to automatically convert a custom language to Java and have been asked to do some basic optimizations of the code during the conversion process. For example, the custom code may have something like:\n\n```\nif someFunction(a, b) > x:\n do something\nelse:\n return someFunction(a, b) + y\n```\n\nin this instance, someFunction is called multiple times with the same inputs, so additional performance can be obtained by caching the value of someFunction() and only calling it once. Thus, an \"optimized\" version of the above code may look something like:\n\n```\nvar1 = someFunction(a, b)\n\nif var1 > x:\n do something\nelse:\n return var1 + y\n```\n\nCurrently, this is done by hand during the conversion process. I run a program to convert the code in the custom language to Java and then manually examine the converted code to see what can be optimized. I want to automate the optimization process since these problems creep up again and again. The people who are writing the code in the custom language do not want to worry about such things, so I can't ask them to just make sure that the code they give me is already optimized.\n\nWhat are some tutorials, papers, etc... that details how such things are done in modern compilers? I don't want to have to re-invent the wheel too much. Thanks in advance.\n\nEdit 1:\n\nIt can be assumed that the function is pure.\n\n========================================\n\nCode:\n```text\nif someFunction(a, b) > x:\n    do something\nelse:\n    return someFunction(a, b) + y\n```\n\n```text\nvar1 = someFunction(a, b)\n\nif var1 > x:\n    do something\nelse:\n    return var1 + y\n```\n\n========================================\n\nComments:\n- This optimization will only work if you are guaranteed that `someFunction` will always return the same value for the same input; do you have guarantees of any kind about that?\n- You have to infer the fact that your function is pure (does not have side effects). How easy it is to do depends on your language. For a *mostly* functional language the purity inference is trivial, otherwise you can only have very conservative inference rules. The details also depend on your intermediate representation. If it is an SSA/ArraySSA (i.e., you have eliminated all the local-only memory transfers), you can mark a function as pure if it does not do any loads and stores and only calls the other pure functions.\n- It can be assumed that the function is pure.\n- There isn't any point in you doing optimizatinons that the Java compiler will do. Do you actually know what optimizations it does?\n- In this case it's just a trivial common subexpression elimination.\n- If you don’t want to reinvent the wheel and are targeting Java as execution environment anyway, well, just let the JVM’s optimizer do the job.\n- more technics can be found here en.m.wikipedia.org/wiki/&hellip;\n- This isn't quite common subexpression elimination because the function call might have side effects and therefore it might not be possible to eliminate it.\n- He has stated the the functions he wants to evaluate are pure","metadata":{"transformedAt":"2026-08-18T18:32:17.839Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":64,"estimatedTokens":803}}903{"id":"stack-3481872","source":"stackoverflow","questionId":3481872,"title":"__decorated__ for python decorators","tags":["python","c","parsing","compiler-construction","decorator"],"text":"Title: __decorated__ for python decorators\nTags: python, c, parsing, compiler-construction, decorator\nSource: Stack Overflow\n\nQuestion:\nAs of 2.4 (2.6 for classes), python allows you to decorate a function with another function:\n\n```\ndef d(func): return func\n\n@d\ndef test(first): pass\n```\n\nIt's a convenient syntactic sugar. You can do all sorts of neat stuff with decorators without making a mess. However, if you want to find out the original function that got decorated you have to jump through hoops (like `Cls.method.__func__.__closure__[0].cell_contents` or worse).\n\nI found myself wishing for a better way and found that there had been some discussion on python-dev about adding a variable called `__decorated__` to the [new] function returned by the decorator. However, it appears that didn't go anywhere.\n\nBeing an adventuresome person, and having had pretty heavy python experience for about 4 years, I thought I would look into implementing `__decorated__` in the python compiler source, just to see how it goes. \n\nTo be honest I have never delved into the C underneath the hood, so my first hours have been just trying to make sense of how the underlying C code works. So firstly, **what would be the best resources to get my head around what I would have to change/add for `__decorator__`?**\n\nSecondly, if a decorator returns a new function then `__decorated__` would just return the original, decorated function. However, if the decorator returns the original function, what should happen? Here are three options I could think of (the third is my favorite):\n\n- Don't add `__decorator__`.\n\n- Add `__decorator__` but set it to None.\n\n- Add `__decorator__` and set it to the original function anyway.\n\nSo if it were to happen, **what do you think would be the best option?** \n\nUPDATE:\n\nSomeone else brought to my attention a scenario that I had missed. What happens when the decorator returns neither the original function nor a function that wraps the original? At that point nothing is holding a reference to the original function and it will get garbage collected. (Thanks Oddthinking!)\n\nSo in that case, I think that I would still go with the third option. The object returned by the decorator would gain a `__decorated__` name that references the original function. This would mean that it would not be garbage-collected. \n\nIt seems weird to me that the function from a class definition would utterly disappear because you decorated it. In my mind that is even more reason to have a `__decorated__` attribute applied for every decorator. However, it's more likely that my intuition is faulty and that the current behavior is what most people would expect. **Any thoughts?**\n\np.s. this is an extension of an earlier, more general question I had. I also went for more info on the first part with a separate post.\n\n========================================\n\nTop Answer:\ni feel free to use some single underscored \"private\" attribute like `_mydecor`\n\npossible multi-decorator solution:\n\n```\ndef decorate(decoration): \n def do_decor(func): \n if hasattr(func, '_mydecor'): \n func._mydecor.add(decoration) \n else: \n func._mydecor = set([decoration]) \n return func \n return do_decor \n\ndef isDecorated(func, decoration): \n return (decoration in getattr(func, '_mydecor', set())) \n\n@decorate('red') \n@decorate('green') \ndef orangefunc(): pass \n\nprint isDecorated(orangefunc, 'green') # -> True \nprint isDecorated(orangefunc, 'blue') # -> False\n```\n\n========================================\n\nCode:\n```text\ndef d(func): return func\n\n@d\ndef test(first): pass\n```\n\n```text\nCls.method.__func__.__closure__[0].cell_contents\n```\n\n```text\n__decorated__\n```\n\n```text\n__decorated__\n```\n\n```text\n__decorator__\n```\n\n```text\n__decorated__\n```\n\n```text\n__decorator__\n```\n\n```text\n__decorator__\n```\n\n```text\n__decorator__\n```\n\n```text\n__decorated__\n```\n\n```text\n__decorated__\n```\n\n```text\ndef test(first): pass\ntest = d(test)\n```\n\n```text\nNone\n```\n\n```text\ndef decorate(decoration): \n    def do_decor(func): \n        if hasattr(func, '_mydecor'): \n            func._mydecor.add(decoration) \n        else: \n            func._mydecor = set([decoration]) \n        return func \n    return do_decor \n\ndef isDecorated(func, decoration): \n    return (decoration in getattr(func, '_mydecor', set())) \n\n@decorate('red') \n@decorate('green') \ndef orangefunc(): pass \n\nprint isDecorated(orangefunc, 'green') # -> True \nprint isDecorated(orangefunc, 'blue')  # -> False\n```\n\n```text\n_mydecor\n```\n\n========================================\n\nComments:\n- so you are thinking about the next step, i.e., how would the decorated function notice that it is decorated? stackoverflow.com/questions/3109289/&hellip; :)\n- How would this work with `@property`? As in, decorators don't have to return functions at all.\n- @Aaron, I suppose you would add `__decorated__` to whatever is returned, function or not.\n- Note that, as of Python 3.2, functools.wraps automatically adds a **wrapped** attribute pointing to the original function (docs.python.org/dev/library/functools#functools.update_wrap&zwnj;&#8203;per)\n- That works well if you control the decorator. I suppose you could wrap the decorator to get this done when you don't control it.\n- I would think you would have to manually add the `__decorated__` attribute to the new function, which would be even uglier.","metadata":{"transformedAt":"2026-08-18T18:32:17.840Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":15,"totalLines":163,"estimatedTokens":1333}}904{"id":"stack-52022998","source":"stackoverflow","questionId":52022998,"title":"How does wee_alloc (a malloc alternative) know where to start the heap in WebAssembly?","tags":["compiler-construction","malloc","webassembly"],"text":"Title: How does wee_alloc (a malloc alternative) know where to start the heap in WebAssembly?\nTags: compiler-construction, malloc, webassembly\nSource: Stack Overflow\n\nQuestion:\nI'm trying to utilize wee_alloc in a custom language, running in WebAssembly. However, I need to fully grok how it knows where to start the heap at so that my stack and static allocations do not clobber it and vice versa.\n\nIt's my understanding that how malloc, et al. know where to start the heap is platform dependent and often just a convention, or in some cases not applicable. However in WebAssembly we can only have a single contiguous piece of linear memory, so we have to it and a convention needs to be used.\n\nReading through the code it appears that what wee_alloc does is make the assumption that whatever memory we start with is off-limits completely, and instead will use the `grow_memory` instruction to create the first piece of memory needed for the heap. That effectively means that the index/address of the start of the heap is highest index of what ever the initial size is, plus one. (Edit: It's actually not + 1, I forgot that indices are zero based; off-by-one error ☠️)\n\ne.g. if we start off with an initial memory size of 1 page:\n\n```\ncurrent_memory = 1 page = 64KiB = 65,536 bytes\n```\n\nthen the heap starts at index 65537.\n\nIs my understanding correct?\n\n========================================\n\nCode:\n```text\ncurrent_memory = 1 page = 64KiB = 65,536 bytes\n```\n\n```text\ngrow_memory\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.840Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":31,"estimatedTokens":372}}905{"id":"stack-9950819","source":"stackoverflow","questionId":9950819,"title":"Converting a grammar into LL(1) grammar: some problems","tags":["compiler-construction","grammar","compiler-theory"],"text":"Title: Converting a grammar into LL(1) grammar: some problems\nTags: compiler-construction, grammar, compiler-theory\nSource: Stack Overflow\n\nQuestion:\nThis is not exactly homework but it is related to my studies:\n\nA grammar for example is like:\n\n E -> E+E|E*E|-E|(E)|id\n\nAfter removing ambiguity it becomes (starting from lowest precedence operator)\n\n```\nE->-F|F\nF->F+G|G\nG->G*H|H\nH->(E)|id\n```\n\nAnd after removing the left recursion and left factoring (not needed in this case) the final LL1 grammar is:\n\n```\nE->-F|F\nF->GF'\nF'->+GF'|e\nG->HG'\nB->*HG'|e\nH->(E)|id\n```\n\nWhich gives an error free parser table which works fine.\nNow about the problem I am facing, suppose the grammar is like this:\n\n E -> E+E|E*E|id=E|(E)|id\n\nNow I am not able to generate a parsing table without conflicts, which means my final grammar is not LL1. Here are the steps:\n\nafter removing ambiguity:\n\n```\nE->id=F|F\nF->F+G|G\nG->G*H|H\nH->(E)|id\n```\n\nAnd after removing the left recursion and left factoring, the grammar becomes:\n\n```\nE->id=F|F\nF->GF'\nF'->+GF'|e\nG->HG'\nB->*HG'|e\nH->(E)|id\n```\n\nBut there is a conflict in the Parser table that I am not able to remove, which means that there is some step that I have missed, or there is some mistake in the steps that I am not able to find. Please tell me what I have done wrong, and how to fix this. I have been working on this problem for a long time now.\n\n========================================\n\nCode:\n```text\nE->-F|F\nF->F+G|G\nG->G*H|H\nH->(E)|id\n```\n\n```text\nE->-F|F\nF->GF'\nF'->+GF'|e\nG->HG'\nB->*HG'|e\nH->(E)|id\n```\n\n```text\nE->id=F|F\nF->F+G|G\nG->G*H|H\nH->(E)|id\n```\n\n```text\nE->id=F|F\nF->GF'\nF'->+GF'|e\nG->HG'\nB->*HG'|e\nH->(E)|id\n```\n\n```text\nE -> E+E|E*E|id=E|(E)|id\n```\n\n```text\nE -> E+E|E*E|(E)|E'\nE' -> id=E|id\n```\n\n```text\nE -> GF       FIRST(E) = FIRST(G)\nF -> +GF|e\nG -> HG'      FIRST(G) = FIRST(H)\nG' -> *HG'|e\nH -> (E)|E'   FIRST(H) = {(} + FIRST(E') = {(, id} \nE' -> idE''   FIRST(E') = {id}\nE'' -> =E|e   FIRST(E'') = {=} + {#}\n```\n\n```text\nE -> E+E|E*E|id=E|(E)|id\n```\n\n```text\nN\n```\n\n```text\nFIRST(N -> a)\n```\n\n```text\nFIRST(N -> b)\n```\n\n```text\nN -> a\n```\n\n```text\nN -> b\n```\n\n```text\nN\n```\n\n```text\nN -> id=\n```\n\n```text\nLL(2)\n```\n\n```text\nid=E\n```\n\n```text\nFIRST2\n```\n\n========================================\n\nComments:\n- Unary Minus operator precedence is not lowest, it's highest always on other binary operators","metadata":{"transformedAt":"2026-08-18T18:32:17.840Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":18,"totalLines":162,"estimatedTokens":590}}906{"id":"stack-37094477","source":"stackoverflow","questionId":37094477,"title":"C# Dragon Book (Lexical analysis) How to handle literals","tags":["c#","dictionary","compiler-construction","lexer"],"text":"Title: C# Dragon Book (Lexical analysis) How to handle literals\nTags: c#, dictionary, compiler-construction, lexer\nSource: Stack Overflow\n\nQuestion:\n*This project is for educational use and I am very well aware that excellent compilers already exist.*\n\nI am currently fighting my way through the famous Dragon Book and just started to implement my own Lexer. It works surprisingly well except for literals. I do not understand how to handle literals using symbol (lookup) tables and the book doesn't seem to cover that very well:\n\nIn the following code `60` is a numeric literal:\n\n```\nint myIdentifier = 60;\n```\n\nThe Dragon Book says:\n\nTechnically speaking, for the lexeme 60 we should make up a token like\n(number,4), where 4 points to the symbol table for the internal\nrepresentation of integer 60 [...]\n\nUnderstood - I created the following Token:\n\n```\n \n//TokenType could be 'number' and lookupIndex could be any int\n```\n\nAnd stored the literal in a dictionary like this:\n\n```\nDictionary \n//literal could be '60' and index could be anything\n```\n\nSince the literal itself is the key in the Dictionary, that allows me to quickly check if future literals have already been added to the symbol table (or not).\n\nThe Parser then receives the Tokens from the Lexer and should be able to identify the literals in the symbol table.\n\n**Questions:**\n\nWhy should my Token contain a lookup-index instead of containing the literal itself?\n\nWouldn't that be quicker...\nHow should the Parser be able to quickly find the literal values inside the symbol-table when the lookup-index is the value of the dictionary?\n\n(I cannot make the lookup-index the dictionary-key because the Lexer would then have to check against the value of the dictionary which is not very performant as well)\n\nCould a multi-indexed-dictionary be a solution? I guess not...\nMust I create a symbol-table for every type of literal then?\n\nF.e.:`Dictionary`\n\nand `Dictionary`\n\nand `Dictionary` etc.\n\n- Maybe I am completely on the wrong track with literals. Feel free to post any better solutions.\n\n========================================\n\nCode:\n```text\nint myIdentifier = 60;\n```\n\n```text\n<enum TokenType, int lookupIndex> \n//TokenType could be 'number' and lookupIndex could be any int\n```\n\n```text\nDictionary<int literal, int index> \n//literal could be '60' and index could be anything\n```\n\n```text\n60\n```\n\n```text\nDictionary<int literal, int index>\n```\n\n```text\nDictionary<double literal, int index>\n```\n\n```text\nDictionary<char literal, int index>\n```\n\n```text\n\"this longish string\"\n```\n\n```text\n1\n```\n\n```text\na += 1\n```\n\n```text\nmap<literalType, int>\n```\n\n```text\nvector<literalType>\n```\n\n```text\n1\n```\n\n========================================\n\nComments:\n- Hmm, this might be the point where I'll switch to C++. Thank you for the info and the new topics I'll first have to read up on, also for stopping me from running circles here. (At least for a while :D)\n- Does point 1 really apply to integers? I mean why would integer constants even have storage locations? Wouldn't a constant integer in the source code always correspond to a constant in the assembly? Like if the source said `x += 1` the assembly would say `ADD someReg, 1` and in cases where the corresponding instruction doesn't have an \"overload\" for constants, it'd be `MOV tmpReg, 1; INSTR someReg, tmpReg`. Either way no memory locations would be involved (except `x`'s when storing the value back to memory).\n- @sepp2k: depends on the architecture of the machine or VM you are targetting. And maybe the width of the integer.","metadata":{"transformedAt":"2026-08-18T18:32:17.840Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":13,"totalLines":122,"estimatedTokens":889}}907{"id":"stack-18006833","source":"stackoverflow","questionId":18006833,"title":"xtext dealing with left recursion grammar","tags":["eclipse","parsing","compiler-construction","xtext"],"text":"Title: xtext dealing with left recursion grammar\nTags: eclipse, parsing, compiler-construction, xtext\nSource: Stack Overflow\n\nQuestion:\nI am using xtext 2.4 and want to support both map and set, my grammar looks like this\n\n```\n ::- \n ::- | \n ::- '{' ( ':' (',' ':' )*)? '}'\n ::- '{' ( (',' )* )+ '}'\n```\n\nso it could be simplified as\n\n```\n ::- '{' ( ':' (',' ':' )*)? '}' |\n '{' ( (',' )* )+ '}'\n```\n\nantlr complains about this grammar begin left recursion, I'm not sure why, since it has '{' in the right hand side.\n\nEven if I want to factor the same part out, I don't know how to do that in xtext, since a EObject now being split into two production rules, and the parsed data cannot store in the same object.\n\nany help?\n\n============================================================================\nCorresponding xtext grammar I wrote is\n\n```\ngrammar org.xtext.problem.Term with org.eclipse.xtext.common.Terminals\n\ngenerate term \"http://www.xtext.org/problem/Term\"\n\nTerm:\n term = Collection\n;\n\nCollection:\n MyMap | MySet\n; \n\nMyMap:\n {MyMap} '{'( keys+= Term ':' values += Term ( ',' keys+=Term ':' values +=Term)* )?'}'\n;\n\nMySet:\n {MySet} '{'( values += Term ( ',' values +=Term)* )?'}'\n;\n```\n\nError messages (starts with *error(211)*):\n\n```\n0 [main] INFO lipse.emf.mwe.utils.StandaloneSetup - Registering platform uri '/Users/jcwu/opensource/xtext-problemistic'\n116 [main] INFO lipse.emf.mwe.utils.StandaloneSetup - Adding generated EPackage 'org.eclipse.xtext.xbase.XbasePackage'\n454 [main] INFO clipse.emf.mwe.utils.GenModelHelper - Registered GenModel 'http://www.eclipse.org/Xtext/Xbase/XAnnotations' from 'platform:/resource/org.eclipse.xtext.xbase/model/Xbase.genmodel'\n459 [main] INFO clipse.emf.mwe.utils.GenModelHelper - Registered GenModel 'http://www.eclipse.org/xtext/xbase/Xtype' from 'platform:/resource/org.eclipse.xtext.xbase/model/Xbase.genmodel'\n479 [main] INFO clipse.emf.mwe.utils.GenModelHelper - Registered GenModel 'http://www.eclipse.org/xtext/xbase/Xbase' from 'platform:/resource/org.eclipse.xtext.xbase/model/Xbase.genmodel'\n479 [main] INFO clipse.emf.mwe.utils.GenModelHelper - Registered GenModel 'http://www.eclipse.org/xtext/common/JavaVMTypes' from 'platform:/resource/org.eclipse.xtext.common.types/model/JavaVMTypes.genmodel'\n1660 [main] INFO ipse.emf.mwe.utils.DirectoryCleaner - Cleaning /Users/jcwu/opensource/xtext-problemistic/org.xtext.problem.term/../org.xtext.problem.term/src-gen\n1665 [main] INFO ipse.emf.mwe.utils.DirectoryCleaner - Cleaning /Users/jcwu/opensource/xtext-problemistic/org.xtext.problem.term/../org.xtext.problem.term.ui/src-gen\n1666 [main] INFO ipse.emf.mwe.utils.DirectoryCleaner - Cleaning /Users/jcwu/opensource/xtext-problemistic/org.xtext.problem.term/../org.xtext.problem.term.tests/src-gen\n2033 [main] INFO ipse.xtext.generator.LanguageConfig - generating infrastructure for org.xtext.problem.Term with fragments : ImplicitRuntimeFragment, ImplicitUiFragment, GrammarAccessFragment, EcoreGeneratorFragment, SerializerFragment, ResourceFactoryFragment, XtextAntlrGeneratorFragment, ValidatorFragment, ImportNamespacesScopingFragment, QualifiedNamesFragment, BuilderIntegrationFragment, GeneratorFragment, FormatterFragment, LabelProviderFragment, OutlineTreeProviderFragment, QuickOutlineFragment, QuickfixProviderFragment, ContentAssistFragment, XtextAntlrUiGeneratorFragment, Junit4Fragment, RefactorElementNameFragment, TypesGeneratorFragment, XbaseGeneratorFragment, CodetemplatesGeneratorFragment, CompareFragment\n4115 [main] INFO clipse.emf.mwe.utils.GenModelHelper - Registered GenModel 'http://www.xtext.org/problem/Term' from 'platform:/resource/org.xtext.problem.term/src-gen/org/xtext/problem/Term.genmodel'\nerror(211): ../org.xtext.problem.term/src-gen/org/xtext/problem/parser/antlr/internal/InternalTerm.g:119:1: [fatal] rule ruleCollection has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2. Resolve by left-factoring or using syntactic predicates or using backtrack=true option.\n5918 [main] ERROR enerator.CompositeGeneratorFragment - java.io.FileNotFoundException: ../org.xtext.problem.term/src-gen/org/xtext/problem/parser/antlr/internal/InternalTermLexer.java (No such file or directory)\norg.eclipse.emf.common.util.WrappedException: java.io.FileNotFoundException: ../org.xtext.problem.term/src-gen/org/xtext/problem/parser/antlr/internal/InternalTermLexer.java (No such file or directory)\n at org.eclipse.xtext.util.Files.readFileIntoString(Files.java:129)\n at org.eclipse.xtext.generator.parser.antlr.AbstractAntlrGeneratorFragment.suppressWarningsImpl(AbstractAntlrGeneratorFragment.java:132)\n at org.eclipse.xtext.generator.parser.antlr.AbstractAntlrGeneratorFragment.suppressWarnings(AbstractAntlrGeneratorFragment.java:142)\n at org.eclipse.xtext.generator.parser.antlr.AbstractAntlrGeneratorFragment.suppressWarnings(AbstractAntlrGeneratorFragment.java:138)\n at org.eclipse.xtext.generator.parser.antlr.XtextAntlrGeneratorFragment.generate(XtextAntlrGeneratorFragment.java:49)\n at org.eclipse.xtext.generator.CompositeGeneratorFragment.generate(CompositeGeneratorFragment.java:92)\n at org.eclipse.xtext.generator.LanguageConfig.generate(LanguageConfig.java:113)\n at org.eclipse.xtext.generator.Generator.generate(Generator.java:361)\n at org.eclipse.xtext.generator.Generator.invokeInternal(Generator.java:128)\n at org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent.invoke(AbstractWorkflowComponent.java:126)\n at org.eclipse.emf.mwe.core.lib.Mwe2Bridge.invoke(Mwe2Bridge.java:34)\n at org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent.invoke(AbstractWorkflowComponent.java:201)\n at org.eclipse.emf.mwe2.runtime.workflow.AbstractCompositeWorkflowComponent.invoke(AbstractCompositeWorkflowComponent.java:35)\n at org.eclipse.emf.mwe2.runtime.workflow.Workflow.run(Workflow.java:19)\n at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:102)\n at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:62)\n at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:52)\n at org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.run(Mwe2Launcher.java:74)\n at org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.main(Mwe2Launcher.java:35)\nCaused by: java.io.FileNotFoundException: ../org.xtext.problem.term/src-gen/org/xtext/problem/parser/antlr/internal/InternalTermLexer.java (No such file or directory)\n at java.io.FileInputStream.open(Native Method)\n at java.io.FileInputStream.(FileInputStream.java:138)\n at java.io.FileInputStream.(FileInputStream.java:97)\n at org.eclipse.xtext.util.Files.readFileIntoString(Files.java:126)\n ... 18 more\nerror(211): ../org.xtext.problem.term.ui/src-gen/org/xtext/problem/ui/contentassist/antlr/internal/InternalTerm.g:176:1: [fatal] rule rule__Collection__Alternatives has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2. Resolve by left-factoring or using syntactic predicates or using backtrack=true option.\n6617 [main] ERROR enerator.CompositeGeneratorFragment - java.io.FileNotFoundException: ../org.xtext.problem.term.ui/src-gen/org/xtext/problem/ui/contentassist/antlr/internal/InternalTermLexer.java (No such file or directory)\norg.eclipse.emf.common.util.WrappedException: java.io.FileNotFoundException: ../org.xtext.problem.term.ui/src-gen/org/xtext/problem/ui/contentassist/antlr/internal/InternalTermLexer.java (No such file or directory)\n at org.eclipse.xtext.util.Files.readFileIntoString(Files.java:129)\n at org.eclipse.xtext.generator.parser.antlr.AbstractAntlrGeneratorFragment.suppressWarningsImpl(AbstractAntlrGeneratorFragment.java:132)\n at org.eclipse.xtext.generator.parser.antlr.AbstractAntlrGeneratorFragment.suppressWarnings(AbstractAntlrGeneratorFragment.java:142)\n at org.eclipse.xtext.generator.parser.antlr.AbstractAntlrGeneratorFragment.suppressWarnings(AbstractAntlrGeneratorFragment.java:138)\n at org.eclipse.xtext.generator.parser.antlr.XtextAntlrUiGeneratorFragment.generate(XtextAntlrUiGeneratorFragment.java:53)\n at org.eclipse.xtext.generator.CompositeGeneratorFragment.generate(CompositeGeneratorFragment.java:92)\n at org.eclipse.xtext.generator.LanguageConfig.generate(LanguageConfig.java:113)\n at org.eclipse.xtext.generator.Generator.generate(Generator.java:361)\n at org.eclipse.xtext.generator.Generator.invokeInternal(Generator.java:128)\n at org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent.invoke(AbstractWorkflowComponent.java:126)\n at org.eclipse.emf.mwe.core.lib.Mwe2Bridge.invoke(Mwe2Bridge.java:34)\n at org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent.invoke(AbstractWorkflowComponent.java:201)\n at org.eclipse.emf.mwe2.runtime.workflow.AbstractCompositeWorkflowComponent.invoke(AbstractCompositeWorkflowComponent.java:35)\n at org.eclipse.emf.mwe2.runtime.workflow.Workflow.run(Workflow.java:19)\n at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:102)\n at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:62)\n at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:52)\n at org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.run(Mwe2Launcher.java:74)\n at org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.main(Mwe2Launcher.java:35)\nCaused by: java.io.FileNotFoundException: ../org.xtext.problem.term.ui/src-gen/org/xtext/problem/ui/contentassist/antlr/internal/InternalTermLexer.java (No such file or directory)\n at java.io.FileInputStream.open(Native Method)\n at java.io.FileInputStream.(FileInputStream.java:138)\n at java.io.FileInputStream.(FileInputStream.java:97)\n at org.eclipse.xtext.util.Files.readFileIntoString(Files.java:126)\n ... 18 more\n6650 [main] INFO text.generator.junit.Junit4Fragment - generating Junit4 Test support classes\n6682 [main] INFO text.generator.junit.Junit4Fragment - generating Compare Framework infrastructure\n7118 [main] INFO .emf.mwe2.runtime.workflow.Workflow - Done.\n```\n\n========================================\n\nCode:\n```text\n<term> ::- <collection>\n<collection> ::- <map> | <set>\n<map> ::- '{' (<term> ':' <term> (',' <term> ':' <term> )*)? '}'\n<set> ::- '{' (<term> (',' <term>)* )+ '}'\n```\n\n```text\n<term> ::- '{' (<term> ':' <term> (',' <term> ':' <term> )*)? '}' |\n           '{' (<term> (',' <term>)* )+ '}'\n```\n\n```text\ngrammar org.xtext.problem.Term with org.eclipse.xtext.common.Terminals\n\ngenerate term \"http://www.xtext.org/problem/Term\"\n\nTerm:\n    term = Collection\n;\n\nCollection:\n    MyMap | MySet\n; \n\nMyMap:\n    {MyMap} '{'( keys+= Term ':' values += Term ( ',' keys+=Term ':' values +=Term)*  )?'}'\n;\n\nMySet:\n    {MySet} '{'( values += Term ( ',' values +=Term)*  )?'}'\n;\n```\n\n```text\n0    [main] INFO  lipse.emf.mwe.utils.StandaloneSetup  - Registering platform uri '/Users/jcwu/opensource/xtext-problemistic'\n116  [main] INFO  lipse.emf.mwe.utils.StandaloneSetup  - Adding generated EPackage 'org.eclipse.xtext.xbase.XbasePackage'\n454  [main] INFO  clipse.emf.mwe.utils.GenModelHelper  - Registered GenModel 'http://www.eclipse.org/Xtext/Xbase/XAnnotations' from 'platform:/resource/org.eclipse.xtext.xbase/model/Xbase.genmodel'\n459  [main] INFO  clipse.emf.mwe.utils.GenModelHelper  - Registered GenModel 'http://www.eclipse.org/xtext/xbase/Xtype' from 'platform:/resource/org.eclipse.xtext.xbase/model/Xbase.genmodel'\n479  [main] INFO  clipse.emf.mwe.utils.GenModelHelper  - Registered GenModel 'http://www.eclipse.org/xtext/xbase/Xbase' from 'platform:/resource/org.eclipse.xtext.xbase/model/Xbase.genmodel'\n479  [main] INFO  clipse.emf.mwe.utils.GenModelHelper  - Registered GenModel 'http://www.eclipse.org/xtext/common/JavaVMTypes' from 'platform:/resource/org.eclipse.xtext.common.types/model/JavaVMTypes.genmodel'\n1660 [main] INFO  ipse.emf.mwe.utils.DirectoryCleaner  - Cleaning /Users/jcwu/opensource/xtext-problemistic/org.xtext.problem.term/../org.xtext.problem.term/src-gen\n1665 [main] INFO  ipse.emf.mwe.utils.DirectoryCleaner  - Cleaning /Users/jcwu/opensource/xtext-problemistic/org.xtext.problem.term/../org.xtext.problem.term.ui/src-gen\n1666 [main] INFO  ipse.emf.mwe.utils.DirectoryCleaner  - Cleaning /Users/jcwu/opensource/xtext-problemistic/org.xtext.problem.term/../org.xtext.problem.term.tests/src-gen\n2033 [main] INFO  ipse.xtext.generator.LanguageConfig  - generating infrastructure for org.xtext.problem.Term with fragments : ImplicitRuntimeFragment, ImplicitUiFragment, GrammarAccessFragment, EcoreGeneratorFragment, SerializerFragment, ResourceFactoryFragment, XtextAntlrGeneratorFragment, ValidatorFragment, ImportNamespacesScopingFragment, QualifiedNamesFragment, BuilderIntegrationFragment, GeneratorFragment, FormatterFragment, LabelProviderFragment, OutlineTreeProviderFragment, QuickOutlineFragment, QuickfixProviderFragment, ContentAssistFragment, XtextAntlrUiGeneratorFragment, Junit4Fragment, RefactorElementNameFragment, TypesGeneratorFragment, XbaseGeneratorFragment, CodetemplatesGeneratorFragment, CompareFragment\n4115 [main] INFO  clipse.emf.mwe.utils.GenModelHelper  - Registered GenModel 'http://www.xtext.org/problem/Term' from 'platform:/resource/org.xtext.problem.term/src-gen/org/xtext/problem/Term.genmodel'\nerror(211): ../org.xtext.problem.term/src-gen/org/xtext/problem/parser/antlr/internal/InternalTerm.g:119:1: [fatal] rule ruleCollection has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.\n5918 [main] ERROR enerator.CompositeGeneratorFragment  - java.io.FileNotFoundException: ../org.xtext.problem.term/src-gen/org/xtext/problem/parser/antlr/internal/InternalTermLexer.java (No such file or directory)\norg.eclipse.emf.common.util.WrappedException: java.io.FileNotFoundException: ../org.xtext.problem.term/src-gen/org/xtext/problem/parser/antlr/internal/InternalTermLexer.java (No such file or directory)\n    at org.eclipse.xtext.util.Files.readFileIntoString(Files.java:129)\n    at org.eclipse.xtext.generator.parser.antlr.AbstractAntlrGeneratorFragment.suppressWarningsImpl(AbstractAntlrGeneratorFragment.java:132)\n    at org.eclipse.xtext.generator.parser.antlr.AbstractAntlrGeneratorFragment.suppressWarnings(AbstractAntlrGeneratorFragment.java:142)\n    at org.eclipse.xtext.generator.parser.antlr.AbstractAntlrGeneratorFragment.suppressWarnings(AbstractAntlrGeneratorFragment.java:138)\n    at org.eclipse.xtext.generator.parser.antlr.XtextAntlrGeneratorFragment.generate(XtextAntlrGeneratorFragment.java:49)\n    at org.eclipse.xtext.generator.CompositeGeneratorFragment.generate(CompositeGeneratorFragment.java:92)\n    at org.eclipse.xtext.generator.LanguageConfig.generate(LanguageConfig.java:113)\n    at org.eclipse.xtext.generator.Generator.generate(Generator.java:361)\n    at org.eclipse.xtext.generator.Generator.invokeInternal(Generator.java:128)\n    at org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent.invoke(AbstractWorkflowComponent.java:126)\n    at org.eclipse.emf.mwe.core.lib.Mwe2Bridge.invoke(Mwe2Bridge.java:34)\n    at org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent.invoke(AbstractWorkflowComponent.java:201)\n    at org.eclipse.emf.mwe2.runtime.workflow.AbstractCompositeWorkflowComponent.invoke(AbstractCompositeWorkflowComponent.java:35)\n    at org.eclipse.emf.mwe2.runtime.workflow.Workflow.run(Workflow.java:19)\n    at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:102)\n    at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:62)\n    at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:52)\n    at org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.run(Mwe2Launcher.java:74)\n    at org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.main(Mwe2Launcher.java:35)\nCaused by: java.io.FileNotFoundException: ../org.xtext.problem.term/src-gen/org/xtext/problem/parser/antlr/internal/InternalTermLexer.java (No such file or directory)\n    at java.io.FileInputStream.open(Native Method)\n    at java.io.FileInputStream.<init>(FileInputStream.java:138)\n    at java.io.FileInputStream.<init>(FileInputStream.java:97)\n    at org.eclipse.xtext.util.Files.readFileIntoString(Files.java:126)\n    ... 18 more\nerror(211): ../org.xtext.problem.term.ui/src-gen/org/xtext/problem/ui/contentassist/antlr/internal/InternalTerm.g:176:1: [fatal] rule rule__Collection__Alternatives has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2.  Resolve by left-factoring or using syntactic predicates or using backtrack=true option.\n6617 [main] ERROR enerator.CompositeGeneratorFragment  - java.io.FileNotFoundException: ../org.xtext.problem.term.ui/src-gen/org/xtext/problem/ui/contentassist/antlr/internal/InternalTermLexer.java (No such file or directory)\norg.eclipse.emf.common.util.WrappedException: java.io.FileNotFoundException: ../org.xtext.problem.term.ui/src-gen/org/xtext/problem/ui/contentassist/antlr/internal/InternalTermLexer.java (No such file or directory)\n    at org.eclipse.xtext.util.Files.readFileIntoString(Files.java:129)\n    at org.eclipse.xtext.generator.parser.antlr.AbstractAntlrGeneratorFragment.suppressWarningsImpl(AbstractAntlrGeneratorFragment.java:132)\n    at org.eclipse.xtext.generator.parser.antlr.AbstractAntlrGeneratorFragment.suppressWarnings(AbstractAntlrGeneratorFragment.java:142)\n    at org.eclipse.xtext.generator.parser.antlr.AbstractAntlrGeneratorFragment.suppressWarnings(AbstractAntlrGeneratorFragment.java:138)\n    at org.eclipse.xtext.generator.parser.antlr.XtextAntlrUiGeneratorFragment.generate(XtextAntlrUiGeneratorFragment.java:53)\n    at org.eclipse.xtext.generator.CompositeGeneratorFragment.generate(CompositeGeneratorFragment.java:92)\n    at org.eclipse.xtext.generator.LanguageConfig.generate(LanguageConfig.java:113)\n    at org.eclipse.xtext.generator.Generator.generate(Generator.java:361)\n    at org.eclipse.xtext.generator.Generator.invokeInternal(Generator.java:128)\n    at org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent.invoke(AbstractWorkflowComponent.java:126)\n    at org.eclipse.emf.mwe.core.lib.Mwe2Bridge.invoke(Mwe2Bridge.java:34)\n    at org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent.invoke(AbstractWorkflowComponent.java:201)\n    at org.eclipse.emf.mwe2.runtime.workflow.AbstractCompositeWorkflowComponent.invoke(AbstractCompositeWorkflowComponent.java:35)\n    at org.eclipse.emf.mwe2.runtime.workflow.Workflow.run(Workflow.java:19)\n    at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:102)\n    at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:62)\n    at org.eclipse.emf.mwe2.launch.runtime.Mwe2Runner.run(Mwe2Runner.java:52)\n    at org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.run(Mwe2Launcher.java:74)\n    at org.eclipse.emf.mwe2.launch.runtime.Mwe2Launcher.main(Mwe2Launcher.java:35)\nCaused by: java.io.FileNotFoundException: ../org.xtext.problem.term.ui/src-gen/org/xtext/problem/ui/contentassist/antlr/internal/InternalTermLexer.java (No such file or directory)\n    at java.io.FileInputStream.open(Native Method)\n    at java.io.FileInputStream.<init>(FileInputStream.java:138)\n    at java.io.FileInputStream.<init>(FileInputStream.java:97)\n    at org.eclipse.xtext.util.Files.readFileIntoString(Files.java:126)\n    ... 18 more\n6650 [main] INFO  text.generator.junit.Junit4Fragment  - generating Junit4 Test support classes\n6682 [main] INFO  text.generator.junit.Junit4Fragment  - generating Compare Framework infrastructure\n7118 [main] INFO  .emf.mwe2.runtime.workflow.Workflow  - Done.\n```\n\n```text\nModel: term += Term*;\n\nTerm: c=Collection;\n\nCollection: ( => Map | Set );\n\nMap: '{' {Map} ( entries += MapEntry ( ',' entries += MapEntry )* )? '}';\nMapEntry: key=Term ':' value=Term;\n\nSet: '{' ( values += Term ( ',' values += Term )* )+ '}';\n```\n\n```text\n// Maps\n{}\n{ {} : {} }\n{ {} : {}, {} : {}}\n\n// Sets\n{ {} }\n{ {} {} {} {} }\n{ {}, {}, {}, {} }\n\n// nested / mixed\n{ { { {}:{} } } : {}, {} : { {}:{} }}\n{ { { {}:{} } } : {}, {} : { {}:{ {}{}{} } }}\n```\n\n```text\n=>\n```\n\n```text\nCollection\n```\n\n```text\n{Map}\n```\n\n```text\nMap\n```\n\n```text\nMapEntry\n```\n\n```text\n{}{}{}\n```\n\n```text\n{}, {}, {}\n```\n\n```text\nSet\n```\n\n```text\n=>\n```\n\n========================================\n\nComments:\n- I don't understand why you write an ANTLR grammar when you want to use Xtext. The Xtext grammar is completly different and has also differt requirements regarding what you can put into one rule and what not.\n- @A.H. Xtext uses ANTLR to parse its own DSL. Ray Wu probably sees an ANTLR flavored error message on his console/log.\n- RayWu, the left recursive rule might originate somewhere else (the one you posted is indeed not left recursive). Could you post a small Xtext grammar that demonstrates the problem?\n- @BartKiers: I know that ANTLR is used internally. But this is not relevant for a Xtext user and in terms of a sscce.org example :-)\n- @A.H., ah, okay. I know little Xtext but thought a more complete Xtext grammar/file would help...\n- Hi @Bart I post the complete xtext grammar and error messages.\n- Hi @A.H., I thought this problem is related to grammar instead of xtext, so I post in a cleaner way...\n- @RayWu: To bad tah I already wrote my own grammar in my answer;-)\n- The {}{}{} issue is introduced by term += Term*, but I do handle that in other rules. I think syntactic predicate is the key for my problem. Let me dig in that a little bit. (accepted as answer)\n- Can you update your links for the docs please?","metadata":{"transformedAt":"2026-08-18T18:32:17.840Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":15,"totalLines":315,"estimatedTokens":5327}}908{"id":"stack-12645106","source":"stackoverflow","questionId":12645106,"title":"Flex Metadata Compiler Extension in FlashDevelop tutorial","tags":["actionscript-3","compiler-construction","metadata"],"text":"Title: Flex Metadata Compiler Extension in FlashDevelop tutorial\nTags: actionscript-3, compiler-construction, metadata\nSource: Stack Overflow\n\nQuestion:\nI've been googling around for the past 2 hours looking for some simple instructions on how to add my own custom metadata tag in as3 with no success. I'm starting to think that I'm probably not searching for the correct terms.\n\n### The Problem\n\nUltimately, what I want to do is add a custom metadata on a function that takes a function as a parameter and makes sure that the given function has the correct parameters. \n\nI.e. I have the following function:\n\n```\npublic function testCallback(callback:Function):void\n{\n callback(\"test\");\n}\n```\n\nand I want to get a compiler error when I call it like this:\n\n```\npublic function doNothing():void\n{\n // doing nothing\n}\n\npublic function someRandomFunction():void\n // ...\n testCallback(doNothing);\n}\n```\n\nThe way I'm thinking of doing this is by having this metadata:\n\n```\n[Callback(paramName=\"callback\",callbackParams=\"string\")]\npublic function testCallback(callback:Function):void\n{\n callback(\"test\");\n}\n```\n\nThe extension would run *during compilation* and, if the function passed does not contain the correct parameters, **a compile-time error will be thrown**. I *THINK* by using flex2.compiler.util.ThreadLocalToolkit.logError(path, line, errorMessage); I can accomplish this.\n\n### The Search\n\nI've been googling for a couple of hours now and couldn't find a simple tutorial that could get me started. I found some SDK bug reports (SDK-18718, SDK-26041), an unfinished forum post, a tutorial (?) on how to add a custom metadata in FlexBuilder (I'm using FlashDevelop), a not-so-useful answer in StackOverflow and many many more dead ends.\n\n### The Help\n\nSo far, as far as I could understand, I will use Java to create the extension and then, using a compiler command, I'll add it into my project. However, I don't know what do I need to get started. \n\n**My two main questions are:**\n\nA) What do I need to create the extension in Java? (do I need Flex Builder? Eclipse? what libraries do I need in my classpath?)\n\nB) Once I have compiled this into something (a swc?), how do I include this in FlashDevelop in my AS3 Project?\n\nThanks in advance!\n\n### Update\n\nI've been able to create a Java project in eclipse, add the Flex libraries, implement the IMxmlcExtension interface and compile the project into a jar with the correct MANIFEST file. Unfortunately, adding the extra -extension=MyTest.jar in FlashDevelop, did nothing. \n\nIn case it is useful, the resulting command line for the compiler was \n\n mxmlc -load-config+=obj\\MyProject.xml -debug=true -incremental=true\n -swf-version=10 -extension=flex_test.jar -o obj\\MyProject634846490611881374\n\n### Update 2\n\nTimofei Davydik helped me narrow down the problem. It seems that FlashDevelop is the main problem. Creating an extension and compiling it in command line works. I started a thread in FlashDeveloper's forums. In case you're interested, the thread is: -extension compiler option\n\n### Update 3\n\nPilippe is correct, it seems that the problem comes from the fact that FlashDevelop uses Flex Compiler SHell (fcsh). I am now looking into how can I switch compilers.\n\n========================================\n\nTop Answer:\nReally interesting question. I've done some research. Yes, we can write mxmlc extensions and add some custom functionality. But processing custom metadata is really complicated, and much time is needed debug and explore flex compiler sources. I've created a post in my blog about extensions, you can check it:\n\nhttp://tim-davydik.blogspot.com/2012/09/flex-compiler-mxmlc-extensions-forcing.html\n\n========================================\n\nCode:\n```text\npublic function testCallback(callback:Function):void\n{\n    callback(\"test\");\n}\n```\n\n```text\npublic function doNothing():void\n{\n    // doing nothing\n}\n\npublic function someRandomFunction():void\n    // ...\n    testCallback(doNothing);\n}\n```\n\n```text\n[Callback(paramName=\"callback\",callbackParams=\"string\")]\npublic function testCallback(callback:Function):void\n{\n    callback(\"test\");\n}\n```\n\n========================================\n\nComments:\n- Assume you could write extension and generate some code, then what would it be? Do you want to throw some custom exception? But you it will throw exception in case of incorrect number of parameters of incorrect parameters type without any additional code. Could you please just describe the idea of handling it? Thanks\n- @TimofeiDavydik Updated my question with my goals.\n- Can you check that it works when using the Flex Compiler SHell (FCSH) which is what FlashDevelop uses internally for optimal incremental compilation.\n- @Philippe I tried to compile using fcsh but the extension didn't work. So I think you are right, the problem is fcsh.\n- Thanks for the blog post. I created an Eclipse project and added exactly what you written. I then added the line in the MANIFEST file and finally went to FlashDevelop and added the line -extension=MyTest.jar in my compiler. I compiled my project but nothing happened. :/\n- @pek Ok. Let's try to find out what's the problem. Could you your jar somehow?\n- @pek, What compiler are you using? It could be a problem if you're using built-in FlashDevelop compiler. Could you try pointing at original one inside SDK folder? I'm using IntelliJ IDEA for my flex projects and it's also doesn't work until I point at the original mxmlc at settings.\n- I tried all three: FlashDevelop compiler, the newest 4.x SDK, and even compiled one from SVN. None of them worked :/\n- @pek and what do you mean by \"nothing happened\"? Did it compiled without \"hello world\" message? Or didn't it compiled at all with error?\n- let us continue this discussion in chat","metadata":{"transformedAt":"2026-08-18T18:32:17.840Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":129,"estimatedTokens":1438}}909{"id":"stack-13415660","source":"stackoverflow","questionId":13415660,"title":"LLVM IR to Python Compiler","tags":["python","compiler-construction","code-generation","llvm","converters"],"text":"Title: LLVM IR to Python Compiler\nTags: python, compiler-construction, code-generation, llvm, converters\nSource: Stack Overflow\n\nQuestion:\nIs there any tool to convert the LLVM IR code to Python code?\nI know it is possible to convert it to Javascript (https://github.com/kripken/emscripten/wiki), to Java (http://da.vidr.cc/projects/lljvm/) and I would love to convert it to Python also.\n\nAdditionaly if such tool does not exist, could you provide any information, what is the best tool to base on (maybe I should extend the emscripten with other language - Javascript and Python are similar to each other in some terms ;) )\n\n========================================\n\nCode:\n```text\nlib/Target/CBackend\n```\n\n========================================\n\nComments:\n- I'm not aware of such a tool. Although, I would not recommend implementing it as a complete platform backend (it will be an overkill). A simple pass will be sufficient. See the `cbe` pass, for example.\n- You are right, it is a good idea. Could you elaborate a little bit more why for example such tool like Emscripten is not written as LLVM pass? It would be a lot easier to use it or embed it in custom compiler.\n- probably, they wanted to host the whole thing inside JavaScript. Or they just like coding in JS more than in C++. There are many possible reasons for doing it one way or another. Alternatively, you can do something in between these two options: implement your code generation pass in, say, OCaml, Haskell or Python itself with LLVM bindings.\n- Thank you. Do you know any projects that use LLVM Pass (in C++) to convert LLVM IR to some specific language code? I want to base on some existing code unless I'm not an LLVM expert :)\n- `cbe` is by far the simplest pass of this kind, I'd recommend to start with it.\n- Additional, I would be very thankful for any link describing the cbe pass - I was searching the web for it and I found anything. In the LLVM src I cannot find source of cbe pass also.\n- `cbe` had been removed from the recent llvm versions, although it is pretty straightforward to port it from `llvm-3.0`. It is in `lib&#47;Target&#47;CBackend`.\n- Out of curiosity, why do you want to go from LLVM IR to Python code?\n- I want simly to write such backend :)","metadata":{"transformedAt":"2026-08-18T18:32:17.840Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":29,"estimatedTokens":562}}910{"id":"stack-9473247","source":"stackoverflow","questionId":9473247,"title":"Tell LLVM optimizer contents of variables","tags":["optimization","compiler-construction","llvm"],"text":"Title: Tell LLVM optimizer contents of variables\nTags: optimization, compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nI'm writing a compiler using LLVM as backend and have a lot of reference counting. When I borrow an object, I increment the object's reference counter. When I release an object, I decrement the reference counter, and free the object if it goes to zero.\nHowever, if I only do a small piece of code, like this one:\n\n```\n++obj->ref;\nglobal_variable_A = obj->a;\nif (--obj->ref == 0)\n free_object(obj);\n```\n\nLLVM optimizes this to (in IR but this is the equal code in C):\n\n```\nglobal_variable_A = obj->a;\nif (obj->ref == 0)\n free_object(obj);\n```\n\nBut since I know that a reference counter is always positive before the first statement, it could be optimized to only\n\n```\nglobal_variable_A = obj->a;\n```\n\nMy question: is there any way to tell the LLVM optimizer that a register or some memory, at a the time of reading it, is known to contain non-zero data?\n\nAn other equal question would be if I can tell the optimizer that a pointer is non-null, that would also be great.\n\n========================================\n\nCode:\n```text\n++obj->ref;\nglobal_variable_A = obj->a;\nif (--obj->ref == 0)\n    free_object(obj);\n```\n\n```text\nglobal_variable_A = obj->a;\nif (obj->ref == 0)\n    free_object(obj);\n```\n\n```text\nglobal_variable_A = obj->a;\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.840Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":52,"estimatedTokens":342}}911{"id":"stack-2138110","source":"stackoverflow","questionId":2138110,"title":"How does a compiler deal with inlined exported functions?","tags":["c++","compiler-construction","export","inline"],"text":"Title: How does a compiler deal with inlined exported functions?\nTags: c++, compiler-construction, export, inline\nSource: Stack Overflow\n\nQuestion:\nIf a header file contains a function definition it can be inlined by the compiler. If the function is exported, the function's name and implementation must also be made available to clients during linkage. How does a compiler achieve this? Does it both inline the function and provide an implementation for external callers?\n\nConsider Foo.h:\n\n```\nclass Foo\n{\n int bar() { return 1; }\n};\n```\n\nFoo::bar may be inlined or not in library foo.so. If another piece of code includes Foo.h does it always create its own copy of Foo::bar, whether inlined or not?\n\n========================================\n\nTop Answer:\nIt usually means that it ends up creating a separate inlined method for every obj file that uses it at link time. It can also fail or refuse to inline many things, so this can cause a problem because you can wind up with bloated objs without getting the performance benefitting of inlining. The same thing can happen with virtual method inlining so it can be worth forcing inining and setting warning for inline failure (about the only useful warning message compilers give).\n\n========================================\n\nCode:\n```text\nclass Foo\n{\n    int bar() { return 1; }\n};\n```\n\n```text\n#include\n```\n\n```text\ninline\n```\n\n```text\ninline\n```\n\n```text\ninline\n```\n\n```text\ninline\n```\n\n```text\ninline extern\n```\n\n========================================\n\nComments:\n- The standard requires that an extern inline function have the same address in all translation units, ie no separate copies in different obj files.\n- The fact that the result of the *address-of* operator returns the same address in all translation units does not mean that the function cannot be inlined. In fact the linker can inline functions that were not declared `inline` and the address-of operator should still work and report the same (single) address in all translation units.\n- I am not 100% sure to be honest when it is done inlining, but I do know you can wind up with inlines that are separate copies for each obj is some circumstances, especially when virtual inlines fail.\n- Of course inline functions exist. You can take the address of one, and `extern inline` is perfectly valid and well-defined. See my answer.\n- -1 Misleading; if an inline function is exported from a DLL, for instance, the compiler *will* produce an out-of-line version of it.\n- @Martin, that is actually what I meant with \"If you managed to export an inlined function then it is sure that it is not inline anymore\"! :-)\n- @Potatoswatter, I think that what the standard defines really smells because it might do either the one or the other depending on what happens in one specific compilation unit. Maybe it is just a matter of definition of what you call inline. For me if the compiler generates a function then it is not inline anymore.\n- @jdehaan: It's still inline after you export it. It's inline in every translation unit where it's declared inline, and it must be declared inline in every translation unit. I'm not sure what the standard intends for DLL's, whether they're considered more translation units for the same program, but you can certainly end up with an `extern` function in your binary that never directly gets called.\n- @jdehaan: Dead functions are stripped by the linker. If all calls to a function are inlined, and it's not exported, it will be erased from the binary. Does that make it inline \"for you\"?\n- @Potatoswatter, gosh, now I got it. I would even downrank myself :-D. I only saw half of the problem, I would tend to delete my post. I still dislike this duality but now understand better why this functionality is provided (avoid mismatching implementations and code duplication). Thanks, Regards.\n- I was reading that some time ago, and I did not conclude that the compiler will ignore the `inline` keyword. It can still inline the code in all call places but generate the non-inlined version and use that for the address-of. That was, of course, just a personal conclusion and I am not sure whether it holds true in any compiler. One other thing is that I know of no compiler that actually enforces de ODR, they mostly assume it --the linker will remove all but one of the definitions and assume that all others were exactly the same.\n- BTW, the *inline* keyword does make the compiler behave differently, quoting 7.1.2 [dcl.fct.spec]/2, at the end of the paragraph: 'An implementation is not required to perform this inline substitution at the place of call; however, even if this inline substitution is omitted, the other rules for inline functions defined in 7.2.1 shall still be respected.', which basically refer to what you already quoted - the function will be defined in all translation units and that will not be a violation of the ODR. With templates and inlined functions the ODR rules are different than for regular functions\n- @David: I doubt any compiler will ignore `inline` outright, but it's discretionary whether an `inline` is called normally or a locally-defined `extern` is inlined. The only thing 7.1.2 actually requires is the alternative ODR allowing multiple identical definitions. Addresses of inline functions are quite common so I imagine most compilers are 100% compliant on this issue. But that's just recapping what I already said… do you disagree or have a question… ?\n- I was just trying to clarify that while the function can be non-inlined, the compiler cannot (literally) 'ignore the inline and use the One Definition Rule' -- the ODR is different for 'inline'd functions than for non-inlined, so I find that sentence a little misleading. In particular, the compiler will generate many definitions (the standard requires it) of the inlined function and then discard all but one in the link step, and I don't know of any compiler that actually enforces the rule (gcc was working on it) that all definitions of the inline/template are the same. +1 anyway","metadata":{"transformedAt":"2026-08-18T18:32:17.840Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":74,"estimatedTokens":1508}}912{"id":"stack-48212351","source":"stackoverflow","questionId":48212351,"title":"How to get LLVM global variable constant value?","tags":["c++","compiler-construction","llvm"],"text":"Title: How to get LLVM global variable constant value?\nTags: c++, compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nI'm trying to get the float value from a global variable and set it as an instruction's operand.\n\nHere is what I want to do:\n\n```\n@a = private constant float 0x3FB99999A0000000\n\n...\n%1 = load float, float* @a ---> removed\n\n%3 = fmul fast %1, %2 ---> %3 = fmul fast float 0x3FB99999A0000000, %2\n```\n\nBelow is what I haved tried so far:\n\n```\nfor (auto gv_iter = llvm_module.global_begin();gv_iter != llvm_module.global_end(); gv_iter++){ \n\n llvm::GlobalVariable* gv = &*gv_iter;\n\n for(auto user_of_gv : gv->users()){\n\n llvm::Instruction *instr_ld_gv = llvm::dyn_cast(user_of_gv);\n llvm::Value *val_gv = llvm::cast(instr_ld_gv);\n\n llvm::Constant *const_gv = gv->getInitializer(); \n llvm::ConstantFP *constfp_gv = llvm::dyn_cast(const_gv);\n float gv_fpval = (constfp_gv->getValueAPF()).convertToFloat(); \n\n llvm::Constant *const_gv_opd = llvm::ConstantFP::get(llvm::Type::getFloatTy(llvm_context),gv_fpval); \n\n for(auto user_of_load : val_gv->users()){ \n\n llvm::Instruction *instr_exe_gv = llvm::dyn_cast(user_of_load);\n\n //P\n for(int operand_num = 0;operand_num getNumOperands();operand_num++){ \n\n llvm::Value *val_instr_op = instr_exe_gv->getOperand(operand_num);\n\n if(val_instr_op == val_gv){\n\n instr_exe_gv->setOperand(operand_num,const_gv_opd);\n instr_ld_gv->removeFromParent();\n\n } \n }\n }\n }\n}\n```\n\nHowever, it'll cause segmentation fault when I tried to run my code.\n\nI'm sure that I have accessed the global variable and instruction I wanted\nby printing the value of \n\n`gv_fpval` which is `0.1` because 0x3FB99999A0000000 equals 0.10000000149011612 in double \n\nprecision. It seems that the program crashes at `setOperand()`.\n\n========================================\n\nCode:\n```text\n@a = private constant float 0x3FB99999A0000000\n\n...\n%1 = load float, float* @a ---> removed\n\n%3 = fmul fast %1, %2  ---> %3 = fmul fast float 0x3FB99999A0000000, %2\n```\n\n```text\nfor (auto gv_iter = llvm_module.global_begin();gv_iter != llvm_module.global_end(); gv_iter++){ \n\n    llvm::GlobalVariable* gv = &*gv_iter;\n\n    for(auto user_of_gv : gv->users()){\n\n        llvm::Instruction *instr_ld_gv = llvm::dyn_cast<llvm::Instruction>(user_of_gv);\n        llvm::Value *val_gv = llvm::cast<llvm::Value>(instr_ld_gv);\n\n        llvm::Constant *const_gv = gv->getInitializer();    \n        llvm::ConstantFP *constfp_gv = llvm::dyn_cast<llvm::ConstantFP>(const_gv);\n        float gv_fpval = (constfp_gv->getValueAPF()).convertToFloat(); \n\n        llvm::Constant *const_gv_opd = llvm::ConstantFP::get(llvm::Type::getFloatTy(llvm_context),gv_fpval); \n\n        for(auto user_of_load : val_gv->users()){ \n\n            llvm::Instruction *instr_exe_gv = llvm::dyn_cast<llvm::Instruction>(user_of_load);\n\n            //P\n            for(int operand_num = 0;operand_num < instr_exe_gv->getNumOperands();operand_num++){ \n\n                llvm::Value *val_instr_op =  instr_exe_gv->getOperand(operand_num);\n\n                if(val_instr_op == val_gv){\n\n                    instr_exe_gv->setOperand(operand_num,const_gv_opd);\n                    instr_ld_gv->removeFromParent();\n\n                }            \n            }\n        }\n    }\n}\n```\n\n```text\ngv_fpval\n```\n\n```text\n0.1\n```\n\n```text\nsetOperand()\n```\n\n```text\n#include <stdio.h>\n// Global Constant value\nfloat a=1.4f;\n\nfloat Multiply(){\n    float b=2.2f;\n    float c=4.32f;\n    float d= a*c;\n    return d;\n}\n\nint main(int argc, char const *argv[])\n{\n\n    printf(\"%f\\n\",Multiply());\n\n    return 0;\n}\n```\n\n```text\n#include \"llvm/Pass.h\"\n#include \"llvm/IR/Function.h\"\n#include \"llvm/Support/raw_ostream.h\"\n#include \"llvm/IR/LegacyPassManager.h\"\n#include \"llvm/IR/InstrTypes.h\"\n#include \"llvm/Transforms/IPO/PassManagerBuilder.h\"\n#include \"llvm/IR/IRBuilder.h\"\n#include \"llvm/IR/Module.h\"\n#include \"llvm/Transforms/Utils/BasicBlockUtils.h\"\n#include \"llvm/DebugInfo/DWARF/DWARFDebugLine.h\"\n#include \"llvm/IR/DebugLoc.h\"\n#include \"llvm/IR/DebugInfo.h\"\nusing namespace llvm;\n\n/* StackOverflow : https://stackoverflow.com/questions/48212351/how-to-get-llvm-global-variable-constant-value* /\n/**Bernard Nongpoh */\n\nnamespace {\n    class ConstantReplacementPass : public ModulePass {\n\n\n    public:\n        static char ID;\n\n\n        ConstantReplacementPass() : ModulePass(ID) {\n            srand (time(NULL));\n        }\n\n        virtual bool runOnModule(Module &M) {\n\n            // list to collect instruction\n            /*\n             * You cannot change an iterator while iterating over it\n            • To remove instructions or modify, first collect the instructions to remove/modify\n            •\n             *\n             * **/\n            // This are the list of load to delete\n            SmallVector<Instruction*,128> *WorkListLoad=new SmallVector<Instruction*,128>();\n            // This is the list of instruction to modify the source operand\n            SmallVector<Instruction*,128> *WorkListUserOfLoad=new SmallVector<Instruction*,128>();\n\n\n            for (auto gv_iter = M.global_begin();gv_iter != M.global_end(); gv_iter++) {\n                   /* GLOBAL DATA INFO*/\n                    GlobalVariable *gv = &*gv_iter;\n                    Constant *const_gv = gv->getInitializer();\n                    ConstantFP *Fvalue;\n                    if(!const_gv->isNullValue()) {\n\n                        if (ConstantFP *constfp_gv = llvm::dyn_cast<llvm::ConstantFP>(const_gv)) {\n                            float gv_fpval = (constfp_gv->getValueAPF()).convertToFloat();\n                            Fvalue = constfp_gv;\n                            errs() << gv_fpval; // Value retrieved here\n                            // Collect Instruction to modify\n\n\n                        }\n\n                        for (auto user_of_gv: gv->users()) {\n                            // Collect in a worklist\n                            if (llvm::Instruction *instr_ld_gv = llvm::dyn_cast<Instruction>(user_of_gv)) {\n\n                                if (LoadInst *loadInst = dyn_cast<LoadInst>(instr_ld_gv)) {\n\n                                    WorkListLoad->push_back(loadInst);\n                                    for (auto user_of_load:loadInst->users()) {\n                                        user_of_load->dump();\n                                        Instruction *instruction1 = dyn_cast<Instruction>(user_of_load);\n                                        instruction1->dump();\n                                        //instruction1->setOperand(0, Fvalue);\n                                        //instruction1->dump();\n                                        // if(Instruction *instruction1 = dyn_cast<Instruction>(user_of_load))\n                                        WorkListUserOfLoad->push_back(instruction1);\n                                        //instruction1->setOperand(0, Fvalue);\n                                        //instruction1->dump();\n                                    }\n\n                                }\n                            }\n                        }\n\n\n                    // Modify Here\n                        while (!WorkListUserOfLoad->empty()) {\n                            Instruction *instruction = WorkListUserOfLoad->pop_back_val();\n                            instruction->setOperand(0, Fvalue);\n                            instruction->dump();\n                        }\n\n                        // Removing all loads that are used by the global variable\n                        while (!WorkListLoad->empty()) {\n                            Instruction *instruction = WorkListLoad->pop_back_val();\n                            instruction->eraseFromParent();\n                        }\n\n\n\n                    }\n                }\n\n\n\n\n\n\n\n\n             return true;\n    }\n  };\n}\n\nchar ConstantReplacementPass::ID = 0;\n\n\nstatic RegisterPass<ConstantReplacementPass> F0(\"constantREP\", \"Constant Replacement Pass \"\n                                         , false,true);\n```\n\n```text\nentry:\n%b = alloca float, align 4\n%c = alloca float, align 4\n%d = alloca float, align 4\ncall void @llvm.dbg.declare(metadata float* %b, metadata !14, metadata !15),\n... !dbg !16\nstore float 0x40019999A0000000, float* %b, align 4, !dbg !16\ncall void @llvm.dbg.declare(metadata float* %c, metadata !17, metadata !15),\n... !dbg !18\nstore float 0x401147AE20000000, float* %c, align 4, !dbg !18\ncall void @llvm.dbg.declare(metadata float* %d, metadata !19, metadata !15),\n... !dbg !20\n%0 = load float, float* %c, align 4, !dbg !21\n%mul = fmul float 0x3FF6666660000000, %0, !dbg !22\nstore float %mul, float* %d, align 4, !dbg !20\n%1 = load float, float* %d, align 4, !dbg !23\nret float %1, !dbg !24\n```\n\n```text\nhello.cpp\n```\n\n```text\nConstantReplacementPass.cpp\n```\n\n```text\nhello.cpp\n```\n\n========================================\n\nComments:\n- Can you include what you used to initialize `instr_exe_gv` so others don’t have to search it up?\n- `llvm::Instruction *instr_exe_gv = llvm::dyn_cast(user_of_load);`\n- That doesn't look right. The `dyn_cast` type isn't a pointer like the variable's type is. Can you update your answer with a complete, minimal example that others can compile and execute?\n- Sorry for the unclear code. I have updated the code and the example above.\n- Thank you! It works like a charm. In addition to the improper using of iterator, I think that I shouldn't use the float value converted from `APFloat` to construct a `Constant` object because of the data type conversion in C++. Is that correct ?\n- Great ! .. Have a look at the precision, if it the value change from the value you specify in the source code, maybe you can try something else, I haven't try other way of converting from APFloat. Let me know if you find some better way!!","metadata":{"transformedAt":"2026-08-18T18:32:17.840Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":11,"totalLines":311,"estimatedTokens":2436}}913{"id":"stack-1553816","source":"stackoverflow","questionId":1553816,"title":"Is there any none .NET/CLI based implementation of a C# compiler?","tags":["c#","compiler-construction"],"text":"Title: Is there any none .NET/CLI based implementation of a C# compiler?\nTags: c#, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI just want the ECMA language translated into native code with the fundamental runtime (garbage collector) etc. I'm not talking about .NET just the language specification of C#.\n\nUsing C# like any other native compiling language as a langauge alternative for Delphi, D or C++ because it offers generics, expanded types, garbage collection and many other nice features. As a langauge it's pretty nice.\n\nBut i don't like .NET nor do i find it very portable (.NET for PA-RISC's HP-UX anyone?).\n\nSo a compilation to C99 would be much, much better then compilation to native (that’s how it works very well for Eiffel).\n\n========================================\n\nTop Answer:\nThe Mono framework has support for native compilation, which they call Ahead Of Time (AOT) compilation. More here: http://www.mono-project.com/AOT\n\n========================================\n\nComments:\n- Right but this like Bartok does not work without CLI and all this stuff. I just want someone who is implementing the language specification, like Tower Java was doing it a long time ago for Java.\n- I don't want .NET assemblies compiled. I'm just looking for an language alternative. I like the C# language more then D and for much much more then this braindead C++. But i need an optimizing, static typed, imperative garbage collected language with generics and an implementation which is generating native compiling for very fast startup implementation and execution of this language. I have all this with my hacked Eiffel compiler but i worry about the future and have to port it to another (alive) language sooner and later.\n- @Lothar: By all means, it seems like you *are* looking to compile .NET assemblies. Please clarify your question if possible.","metadata":{"transformedAt":"2026-08-18T18:32:17.840Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":466}}914{"id":"stack-53463422","source":"stackoverflow","questionId":53463422,"title":"How to check that a function always return a value (aka \"doesn't fall off the end\")?","tags":["swift","compiler-construction"],"text":"Title: How to check that a function always return a value (aka \"doesn't fall off the end\")?\nTags: swift, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm building a didactic compiler, and I'd like to check if the function will always return a value. I intend to do this in the semantic analysis step (as this is not covered by the language grammar).\n\nOut of all the flow control statements, this didactic language only has `if`, `else`, and `while` statements (so no `do while`, `for`, `switch` cases, etc). Note that `else if` is also possible. The following are all valid example snippets:\n\na)\n\n```\nif (condition) {\n // non-returning commands\n}\nreturn value\n```\n\nb)\n\n```\nif (condition) {\n return value\n}\nreturn anotherValue\n```\n\nc)\n\n```\nif (condition) {\n return value1\n} else {\n return value2\n}\n// No return value needed here\n```\n\nI've searched a lot about this but couldn't find a pseudoalgorithm that I could comprehend. I've searched for software path testing, the white box testing, and also other related Stack Overflow questions like this and this.\n\nI've heard that this can be solved using graphs, and also using a stack, but I have no idea how to implement those strategies.\n\nAny help with pseudocode would be very helpful!\n\n(and if it matters, I'm implementing my compiler in Swift)\n\n========================================\n\nTop Answer:\nSo, after 5 hours thinking how to implement this, I came up with a decent solution (at least I haven't been able to break it so far). I actually spent most of the time browsing the web (with no luck) than actually thinking about the problem and trying to solve it on my own.\n\nBelow is my implementation (in Swift 4.2, but the syntax is fairly easy to pick up), using a graph:\n\n```\nfinal class SemanticAnalyzer {\n private var currentNode: Node!\n private var rootNode: Node!\n\n final class Node {\n var nodes: [Node] = []\n var returnsExplicitly = false\n let parent: Node?\n var elseNode: Node!\n var alwaysReturns: Bool { return returnsExplicitly || elseNode?.validate() == true }\n\n init(parent: Node?) {\n self.parent = parent\n }\n\n func validate() -> Bool {\n if alwaysReturns {\n return true\n } else {\n return nodes.isEmpty ? false : nodes.allSatisfy { $0.alwaysReturns }\n }\n }\n }\n\n /// Initializes the components of the semantic analyzer.\n func startAnalyzing() {\n rootNode = Node(parent: nil)\n currentNode = rootNode\n }\n\n /// Execute when an `if` statement is found.\n func handleIfStatementFound() {\n let ifNode = Node(parent: currentNode)\n let elseNode = Node(parent: currentNode)\n // Assigning is not necessary if the current node returns explicitly.\n // But assigning is not allowed if the else node always returns, so we check if the current node always returns.\n if !currentNode.alwaysReturns {\n currentNode.elseNode = elseNode\n }\n currentNode.nodes += [ ifNode, elseNode ]\n currentNode = ifNode\n }\n\n /// Execute when an `else` statement is found.\n func handleElseStatementFound() {\n currentNode = currentNode.elseNode\n }\n\n /// Execute when a branch scope is closed.\n func handleBranchClosing() {\n currentNode = currentNode.parent! // If we're in a branch, the parent node is never nil\n }\n\n /// Execute when a function return statement is found.\n func handleReturnStatementFound() {\n currentNode.returnsExplicitly = true\n }\n\n /// Determine whether the function analyzed always returns a value.\n ///\n /// - Returns: whether the root node validates.\n func validate() -> Bool {\n return rootNode.validate()\n }\n}\n```\n\nBasically what it does is:\n\n- When it finds an `if` statement is create 2 new nodes and point the current node to both of them (as in a binary tree node).\n\n- When the `else` statement is found, we just switch the current node to the else node created previously in the `if` statement.\n\n- When a branch is closed (e.g. in an `if` statement's `}` character), it switches the current node to the parent node.\n\n- When it finds a function `return` statement, it can assume that the current node will always have a return value.\n\nFinally, to validate a node, either the node has an explicit return value, or all of the nodes must be valid.\n\nThis works with nested if/else statements, as well as branches without return values at all.\n\n========================================\n\nCode:\n```text\nif (condition) {\n    // non-returning commands\n}\nreturn value\n```\n\n```text\nif (condition) {\n    return value\n}\nreturn anotherValue\n```\n\n```text\nif (condition) {\n    return value1\n} else {\n    return value2\n}\n// No return value needed here\n```\n\n```text\nif\n```\n\n```text\nelse\n```\n\n```text\nwhile\n```\n\n```text\ndo while\n```\n\n```text\nfor\n```\n\n```text\nswitch\n```\n\n```text\nelse if\n```\n\n```text\nalwaysReturns( {} ) = false\nalwaysReturns( {return exp; ...rest} ) = true\nalwaysReturns( { if(exp) thenBlock else elseBlock; ...rest}) =\n    (alwaysReturns(thenBlock) && alwaysReturns(elseBlock)) || alwaysReturns(rest)\nalwaysReturns( {otherStatement; ...rest} ) = alwaysReturns(rest)\n```\n\n```text\nif(cond) return x; if(!cond) return y;\n```\n\n```text\nif(cond) return x; else return y;\n```\n\n```text\ngoto\n```\n\n```text\nbreak\n```\n\n```text\ncontinue\n```\n\n```text\nif\n```\n\n```text\nelse\n```\n\n```text\nif\n```\n\n```text\nelse\n```\n\n```text\nif\n```\n\n```text\nfinal class SemanticAnalyzer {\n    private var currentNode: Node!\n    private var rootNode: Node!\n\n    final class Node {\n        var nodes: [Node] = []\n        var returnsExplicitly = false\n        let parent: Node?\n        var elseNode: Node!\n        var alwaysReturns: Bool { return returnsExplicitly || elseNode?.validate() == true }\n\n        init(parent: Node?) {\n            self.parent = parent\n        }\n\n        func validate() -> Bool {\n            if alwaysReturns {\n                return true\n            } else {\n                return nodes.isEmpty ? false : nodes.allSatisfy { $0.alwaysReturns }\n            }\n        }\n    }\n\n    /// Initializes the components of the semantic analyzer.\n    func startAnalyzing() {\n        rootNode = Node(parent: nil)\n        currentNode = rootNode\n    }\n\n    /// Execute when an `if` statement is found.\n    func handleIfStatementFound() {\n        let ifNode = Node(parent: currentNode)\n        let elseNode = Node(parent: currentNode)\n        // Assigning is not necessary if the current node returns explicitly.\n        // But assigning is not allowed if the else node always returns, so we check if the current node always returns.\n        if !currentNode.alwaysReturns {\n            currentNode.elseNode = elseNode\n        }\n        currentNode.nodes += [ ifNode, elseNode ]\n        currentNode = ifNode\n    }\n\n    /// Execute when an `else` statement is found.\n    func handleElseStatementFound() {\n        currentNode = currentNode.elseNode\n    }\n\n    /// Execute when a branch scope is closed.\n    func handleBranchClosing() {\n        currentNode = currentNode.parent! // If we're in a branch, the parent node is never nil\n    }\n\n    /// Execute when a function return statement is found.\n    func handleReturnStatementFound() {\n        currentNode.returnsExplicitly = true\n    }\n\n    /// Determine whether the function analyzed always returns a value.\n    ///\n    /// - Returns: whether the root node validates.\n    func validate() -> Bool {\n        return rootNode.validate()\n    }\n}\n```\n\n```text\nif\n```\n\n```text\nelse\n```\n\n```text\nif\n```\n\n```text\nif\n```\n\n```text\n}\n```\n\n```text\nreturn\n```\n\n========================================\n\nComments:\n- That's an interesting way of thinking! 5 hours after posting this question, I figured out a solution (that I couldn't break until now so I assume it's right haha), but it's still great to see other points of view. What I would just correct in your analysis, is that the `while` loops matter if the condition is always true, e.g. `while (true) { return value }; &#47;&#47; Dead code from now on`, do you agree? :)\n- @RogerOba Yeah, my simplifying assumption was basically that we treat all conditions as if they could always be true or false. If you want to handle `while(true)` specially, then yes, you'll need to care whether the loop contains a return. Note that then you'll also need to care about `break` and `continue` (which I assume your language must have for `while(true)` to even make sense).\n- It actually doesn't haha and I didn't handle the `while(true)` because I can't tell if the condition is always true at compile-time (with the current implementation). 😁\n- @RogerOba If it's literally `while(true)`, you know that it's always true, but if it's `while(someExp)`, checking whether `someExp` is always true would be undecidable in the general case.\n- This can be actually very complicated the more complicated is the language syntax. Possibly it has to understand inner functions and closures, it has to understand enums to see whether all cases in a switch are covered. It has understand functions that never return (`fatalError`). It's not an easy job to do without actual syntax/semantic parsing.\n- @Sulthan you mean expression evaluation to check if it's always true? Yeah we can only assume constant values, so it's also only possible if the language has a distinction between constants and variables (like Swift's `let` VS `var`). Definitely all those scenarios add a lot more extra complexity to the analyzer, luckily my language only has if/else statements! Swift's `Never` would be interesting to analyze as well 😉\n- @Sulthan I'm not sure what you mean by \"semantic parsing\", but obviously this check would be performed on the AST after parsing (and if it gets complicated enough, you'll probably want to use a CFG after all). You're right that it gets more complicated, the more features the language has (though I'm not sure how closures would complicate anything, unless it's like in Ruby where the inner function can return from the outer one) - here I've just covered the features listed in the question.","metadata":{"transformedAt":"2026-08-18T18:32:17.841Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":28,"totalLines":339,"estimatedTokens":2456}}915{"id":"stack-27819139","source":"stackoverflow","questionId":27819139,"title":"How print syntax tree using Javac","tags":["java","compiler-construction","javac","static-analysis"],"text":"Title: How print syntax tree using Javac\nTags: java, compiler-construction, javac, static-analysis\nSource: Stack Overflow\n\nQuestion:\nI implemented a program TreeScanner, to print information about all nodes in AST. The program supports all types (all visit methods are implemented). However, the problem is that for the statement, `System.out.println(object.YYY);` the program does not visit field reference **YYY**. \n\nIt detects **object** as identifier, but can not detect **YYY** as identifier. However, when I have System.out.println(YYY); then visitIdentifier will visit **YYY**.\n\nPlease let me know what is the difference between the both above lines, while in one YYY is visited by visitidentifier, in the another case it is not visited. \n\nHow can I visit **YYY** in **object.YYY**? \n\nIn class org.eclipse.jdt.core.dom we have FieldAccess which is called in both above cases for **YYY**, but seems TreeScanner in Javac has no similar method.\n\n========================================\n\nCode:\n```text\nSystem.out.println(object.YYY);\n```\n\n```text\nVoid visitMemberSelect(MemberSelectTree memberSelect, Void p) {\n    // Print the object\n    memberSelect.getExpression().accept(this, p);\n    System.out.print(\".\");\n    // Print the name of the member\n    System.out.print(memberSelect.getIdentifier());\n}\n```\n\n```text\nvisitIdentifier\n```\n\n```text\n<expression>.<identifier>\n```\n\n```text\n<expression>.<expression>\n```\n\n```text\nYYY\n```\n\n```text\nobject.YYY\n```\n\n```text\nMemberSelectTree\n```\n\n```text\nobject.YYY\n```\n\n```text\nYYY\n```\n\n```text\nName\n```\n\n```text\ngetIdentifier()\n```\n\n```text\nvisitName\n```\n\n```text\nTreeScanner\n```\n\n```text\nYYY\n```\n\n```text\nvisitMemberSelect\n```\n\n```text\nobject.YYY\n```\n\n```text\nvisitMemberSelect\n```\n\n========================================\n\nComments:\n- It might help to post some code...\n- Is `YYY` a `static final` (i.e. constant) field?\n- I checked all these cases: static, static final, and normal field. In all three cases it could not detect YYY as identifier.\n- Odd. If it was a constant, it might be an optimization where javac returns the value of the constant. But if it always happens ...\n- Can you post a working example?","metadata":{"transformedAt":"2026-08-18T18:32:17.841Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":18,"totalLines":104,"estimatedTokens":541}}916{"id":"stack-244115","source":"stackoverflow","questionId":244115,"title":"How do I tokenize input using Java's Scanner class and regular expressions?","tags":["java","regex","compiler-construction","tokenize"],"text":"Title: How do I tokenize input using Java's Scanner class and regular expressions?\nTags: java, regex, compiler-construction, tokenize\nSource: Stack Overflow\n\nQuestion:\nJust for my own purposes, I'm trying to build a tokenizer in Java where I can define a regular grammar and have it tokenize input based on that. The StringTokenizer class is deprecated, and I've found a couple functions in Scanner that hint towards what I want to do, but no luck yet. Anyone know a good way of going about this?\n\n========================================\n\nTop Answer:\nIf I understand your question well then here are two example methods to tokenize a string. You do not even need the Scanner class, only if you want to pre-cast the tokens, or iterate through them more sofistically than using an array. If an array is enough just use String.split() as given below. \n\nPlease give more requirements to enable more precise answers. \n\n```\nimport java.util.Scanner;\n\n public class Main { \n\n public static void main(String[] args) {\n\n String textToTokenize = \"This is a text that will be tokenized. I will use 1-2 methods.\";\n Scanner scanner = new Scanner(textToTokenize);\n scanner.useDelimiter(\"i.\");\n while (scanner.hasNext()){\n System.out.println(scanner.next());\n }\n\n System.out.println(\" **************** \");\n String[] sSplit = textToTokenize.split(\"i.\");\n\n for (String token: sSplit){\n System.out.println(token);\n }\n }\n\n}\n```\n\n========================================\n\nCode:\n```text\nimport java.util.*;\nimport java.util.regex.*;\n\npublic class RETokenizer\n{\n  static List<Token> tokenize(String source, List<Rule> rules)\n  {\n    List<Token> tokens = new ArrayList<Token>();\n    int pos = 0;\n    final int end = source.length();\n    Matcher m = Pattern.compile(\"dummy\").matcher(source);\n    m.useTransparentBounds(true).useAnchoringBounds(false);\n    while (pos < end)\n    {\n      m.region(pos, end);\n      for (Rule r : rules)\n      {\n        if (m.usePattern(r.pattern).lookingAt())\n        {\n          tokens.add(new Token(r.name, m.start(), m.end()));\n          pos = m.end();\n          break;\n        }\n      }\n      pos++;  // bump-along, in case no rule matched\n    }\n    return tokens;\n  }\n\n  static class Rule\n  {\n    final String name;\n    final Pattern pattern;\n\n    Rule(String name, String regex)\n    {\n      this.name = name;\n      pattern = Pattern.compile(regex);\n    }\n  }\n\n  static class Token\n  {\n    final String name;\n    final int startPos;\n    final int endPos;\n\n    Token(String name, int startPos, int endPos)\n    {\n      this.name = name;\n      this.startPos = startPos;\n      this.endPos = endPos;\n    }\n\n    @Override\n    public String toString()\n    {\n      return String.format(\"Token [%2d, %2d, %s]\", startPos, endPos, name);\n    }\n  }\n\n  public static void main(String[] args) throws Exception\n  {\n    List<Rule> rules = new ArrayList<Rule>();\n    rules.add(new Rule(\"WORD\", \"[A-Za-z]+\"));\n    rules.add(new Rule(\"QUOTED\", \"\\\"[^\\\"]*+\\\"\"));\n    rules.add(new Rule(\"COMMENT\", \"//.*\"));\n    rules.add(new Rule(\"WHITESPACE\", \"\\\\s+\"));\n\n    String str = \"foo //in \\\"comment\\\"\\nbar \\\"no //comment\\\" end\";\n    List<Token> result = RETokenizer.tokenize(str, rules);\n    for (Token t : result)\n    {\n      System.out.println(t);\n    }\n  }\n}\n```\n\n```text\nscanf()\n```\n\n```text\nsplit()\n```\n\n```text\nsplit()\n```\n\n```text\nusePattern()\n```\n\n```text\nlookingAt()\n```\n\n```text\nimport java.util.Scanner;\n\n\n  public class Main {    \n\n    public static void main(String[] args) {\n\n        String textToTokenize = \"This is a text that will be tokenized. I will use 1-2 methods.\";\n        Scanner scanner = new Scanner(textToTokenize);\n        scanner.useDelimiter(\"i.\");\n        while (scanner.hasNext()){\n            System.out.println(scanner.next());\n        }\n\n        System.out.println(\" **************** \");\n        String[] sSplit = textToTokenize.split(\"i.\");\n\n        for (String token: sSplit){\n            System.out.println(token);\n        }\n    }\n\n}\n```\n\n========================================\n\nComments:\n- Yeah, I should have elaborated more. That's helpful for splitting a string **around** matches to a regex, but not for finding the tokens that actually match the regex.\n- I would also highly recommend JFlex for anything non-trivial. Writing scanner specifications takes some practice but JFlex has good starter files and is a great skill to acquire.\n- Your pos < end loop will need to increment pos before the 'for rules' loop in case no rules are matched right? Otherwise nice example and thanks for the lookingAt() suggestion.\n- Good catch. Yes, it there should be a 'pos++' right after the for-loop. This may be a bare-bones example with no error checking, but I should at least have made sure it didn't have any potential infinite loops.\n- I really like this approach and used this as an example for my own code just yesterday. I did notice the order of the Rules list can affect results, however. In my solution, I attempt to match on all rules instead of breaking after the first match. Then I select the longest match.","metadata":{"transformedAt":"2026-08-18T18:32:17.841Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":176,"estimatedTokens":1260}}917{"id":"stack-4142088","source":"stackoverflow","questionId":4142088,"title":"understanding the need for compilers for different platforms","tags":["compiler-construction","operating-system"],"text":"Title: understanding the need for compilers for different platforms\nTags: compiler-construction, operating-system\nSource: Stack Overflow\n\nQuestion:\nI am trying to learn how the whole build chain works so I can better understand what goes on when I do build/link/compile etc.\n\nOne point I am having trouble with is this: If the compiler turns the source into native assembly, why can't the same program run on different OSs? Isn't assembly run directly by the CPU? So the same machine code should run on every OS, as long as it is the same architecture, no? Why not?\n\n**EDIT:** Most of the answers so far are about calling the OS's *APIs*. That obviously is a problem. My question is about the **straight machine code**. Does it get passed straight to the CPU or not? If I wrote a program in assembly, would I still need to compile separately for each OS? (side point: If I used standard c++ cin/cout, is that OS dependent, get compiled to direct assembly I/O, or does the answer depend on the compiler?)\n\n========================================\n\nTop Answer:\nIt comes down to the operating system's API and ABI.\n\nDifferent operating systems provide different system calls, as well as different mechanisms to invoke those system calls. For example, while POSIX provides `fork` and `execv` to create a new process, Windows provides `CreateProcess`.\n\nFurthermore, there are differences at the assembly level. What assembly code do you use to call a function? Different operating systems expect different calling conventions. Operating systems also do not necessarily agree on the formatting of the executable binary, nor do they agree on other mechanisms such as dynamic linking.\n\nAnother point to consider is concurrency and how the OS handles that. Some operating systems recognize threads at the kernel level, while others do not. Some might just prefer using multiple processes, and some might use a completely different model. The APIs are different, and the abstractions might be different. For example, one OS might use locks and semaphores, another might use message passing.\n\n========================================\n\nCode:\n```text\nbrk\n```\n\n```text\nsbrk\n```\n\n```text\nWin32GetMem\n```\n\n```text\nfork\n```\n\n```text\nexecv\n```\n\n```text\nCreateProcess\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.841Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":48,"estimatedTokens":563}}918{"id":"stack-2067524","source":"stackoverflow","questionId":2067524,"title":"what are the differences between an executable generated by windows and linux","tags":["c","linux","compiler-construction"],"text":"Title: what are the differences between an executable generated by windows and linux\nTags: c, linux, compiler-construction\nSource: Stack Overflow\n\nQuestion:\n**Possible Duplicate:**\n\n Why an executable program for a specific CPU does not work on Linux and Windows? \n\nWhy can't programs written in linux be executd in windows ? Suppose I compile a simple C program containing function calls that are common to both windows and linux, Does the compiler generate different binary under windows and linux ?\n\n========================================\n\nTop Answer:\nHere are some of the reasons I can think of off the top of my head:\n\n- Different container formats (which so far seems to be the leading differentiator in this answer -- however its not the only reason).\n\n- different dynamic linker semantics.\n\n- different ABI.\n\n- different exception handling mechanisms -- windows has SEH -- upon which C++ exception handling is built\n\n- different system call semantics and different system calls -- hence different low-level libraries.\n\n========================================\n\nCode:\n```text\nvoid f()\n{\n    *((char*)0) = 0;\n}\n```\n\n========================================\n\nComments:\n- dup stackoverflow.com/questions/2059605/&hellip;\n- Back in the day, Windows supported `COM` executables which was basically just the raw assembly - no header whatsoever. Anyone know if it still does so?\n- @lpthnc: Mac OS X uses the Mach-O format, inherited from NeXTSTEP. Classic Mac OS used the PEF format.\n- That's why he wrote, `Suppose I compile a simple C program containing function calls that are common to both windows and linux`\n- Well, even if you don't call any functions, the compiler has to generate API calls to set up a stack\n- @andomar the compiler doesn't make the calls the process does....\n- The code can be built to compile on both platforms with compiler instructions (ie. to include the Windows API and call WinMain) if you are compiling for that platform.\n- It doesn't matter what function calls he writes into his program. The startup sequence (the stuff that runs before his `main()` ever starts) is likely to be quite machine and OS dependent, and quite different between Windows and Linux.\n- @Hassan: Andomar never said it did. However, the compiler still has to *generate* those API calls.\n- API calls to set up a stack? Is that like creating a GUI in Visual Basic to track IPs? The stack is set by the time the entry point is reached on both systems. The completely different ABI and executable formats are far more relevant.","metadata":{"transformedAt":"2026-08-18T18:32:17.841Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":49,"estimatedTokens":633}}919{"id":"stack-7046414","source":"stackoverflow","questionId":7046414,"title":"Compiler Trivia: What is consequence of this code","tags":["c#","compiler-construction","behavior"],"text":"Title: Compiler Trivia: What is consequence of this code\nTags: c#, compiler-construction, behavior\nSource: Stack Overflow\n\nQuestion:\nI was reviewing some code today and came across some code (accurately portrayed by this snippet)...\n\n```\npublic abstract class FlargBase{\n public FlargBase(){\n this.DoSomething();\n }\n\n public abstract void DoSomething();\n}\n\npublic class PurpleFlarg: FlargBase{\n public PurpleFlarg()\n : base(){\n }\n\n public override void DoSomething(){\n // Do something here;\n }\n}\n```\n\nThe compiler gives no errors or warnings, but CodeAnalysis warns that the call chain contains a call to a virtual method and may produce unintended results.\n\nI was curious because, as I see it, two things can happen.\n\n- Creating an instance of the base class will make a call to a method with no defined implementation. I would expect the compiler to error, or the runtime to throw an exception due to a missing implementation. I'm assuming the compiler is providing an implementation of {} I mis-typed the original code; it did contain the abstract keyword on the class.\n\n- Creating an instance of a derived class will cause a call to a method on a class that has not actually been constructed yet. I would have expected this to throw an exception.\n\nThis code has been in a production environment for several months. It is apparently working correctly enough that no one has noticed any strange behavior.\n\nI'm hoping the incredible talent here at StackOverflow can give me some insight into the behavior and consequences of this code.\n\n========================================\n\nTop Answer:\nYour `PurpleFlarg.DoSomething()` is executed before the `PurpleFlarg()` constructor body. \n\nThat can lead to surprises as the general assumption always is that the constructor is the first method to operate on an object. \n\nHere is the MSDN page with an example of an 'error' condition.\n\n========================================\n\nCode:\n```text\npublic abstract class FlargBase{\n    public FlargBase(){\n        this.DoSomething();\n    }\n\n    public abstract void DoSomething();\n}\n\npublic class PurpleFlarg: FlargBase{\n    public PurpleFlarg()\n      : base(){\n    }\n\n    public override void DoSomething(){\n        // Do something here;\n    }\n}\n```\n\n```text\nPurpleFlarg.DoSomething()\n```\n\n```text\nPurpleFlarg()\n```\n\n```text\nusing System;\n\nclass A\n{\n   public A() {\n      PrintFields();\n   }\n\n   public virtual void PrintFields() {}\n\n}\n\nclass B: A\n{\n   int x = 1;\n   int y;\n\n   public B() {\n      y = -1;\n   }\n\n   public override void PrintFields() {\n      Console.WriteLine(\"x = {0}, y = {1}\", x, y);\n   }\n}\n```\n\n```text\nx = 1, y = 0\n```\n\n```text\nabstract class MyBase\n{\n    public object CustomObject { get; private set; }\n\n    public MyBase()\n    {\n        this.CustomObject = this.CreateCustomObject();\n    }\n\n    protected abstract object CreateCustomObject();\n}\n\nclass MyBaseList : MyBase\n{\n    protected override object CreateCustomObject()\n    {\n        return new List<int>();\n    }\n}\n\nclass MyBaseDict : MyBase\n{\n    protected override object CreateCustomObject()\n    {\n        return new Dictionary<int, int>();\n    }\n}\n```\n\n========================================\n\nComments:\n- I am getting an error: `'ConsoleApplication1.FlargBase.DoSomething()' is abstract but it is contained in non-abstract class 'ConsoleApplication1.FlargBase'`. Also, stepping through the code when creating an instance of the derived class shows it calls DoSomething() in the derived class.\n- This is closely related to the brittle base class problem. See blogs.msdn.com/b/ericlippert/archive/2004/01/07/&hellip;\n- You should maybe change the terminology a bit in your first sentence, since right now it is tautologically false. The object exists prior to execution of the constructor, but it is not constructed prior to being constructed.\n- And how does this address the actual question?\n- @Henk, it means that the code will never do something undefined (like throw an AV), but it may not do something you expect.","metadata":{"transformedAt":"2026-08-18T18:32:17.841Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":149,"estimatedTokens":997}}920{"id":"stack-1489089","source":"stackoverflow","questionId":1489089,"title":"How to create a compiler in vb.net","tags":["vb.net","compiler-construction"],"text":"Title: How to create a compiler in vb.net\nTags: vb.net, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nBefore answering this question, understand that I am not asking how to create my own programming language, I am asking how, using vb.net code, I can create a compiler for a language like vb.net itself. Essentially, the user inputs code, they get a .exe. By NO MEANS do I want to write my own language, as it seems other compiler related questions on here have asked. I also do not want to use the vb.net compiler itself, nor do I wish to duplicate the IDE. \n\nThe exact purpose of what I wish to do is rather hard to explain, but all I need is a nudge in the right direction for writing a compiler (from scratch if possible) which can simply take input and create a .exe. I have opened .exe files as plain text before (my own programs) to see if I could derive some meaning from what I assumed would be human readable text, yet I was obviously sorely disappointed to see the random ascii, though it is understandable why this is all I found.\n\nI know that a .exe file is simply lines of code, being parsed by the computer it is on, but my question here really boils down to this: What code makes up a .exe? How could I go about making one in a plain text editor if I wanted to? (No, I do not want to do that, but if I understand the process my goals will be much easier to achieve.) What makes an executable file an executable file? Where does the logic of the code fit in?\n\nThis is intended to be a programming question as opposed to a computer question, which is why I did not post it on SuperUser. I know plenty of information about the System.IO namespace, so I know how to create a file and write to it, I simply do not know what exactly I would be placing inside this file to get it to work as an executable file.\n\nI am sorry if this question is \"confusing\", \"dumb\", or \"obvious\", but I have not been able to find any information regarding the actual contents of an executable file anywhere.\n\nOne of my google searches\n\nSomething that looked promising\n\nEDIT: The second link here, while it looked good, was an utter fail. I am not going to waste hours of my time hitting keys and recording the results. \"Use the \"Alt\" and the 3-digit combinations to create symbols that do not appear on the keyboard but that you need in the program.\" (step 4) How the heck do I know what symbols I need???\n\nThank you very much for your help, and my apologies if this question is a nooby or \"bad\" one.\n\nTo sum this up simply: I want to create a program in vb.net that can compile code in a particular language to a single executable file. What methods exist that can allow me to do this, and if there are none, how can I go about writing my own from scratch?\n\n========================================\n\nTop Answer:\n```\nProcess.Start(String.Format(\"vbc.exe {0}\", sourceFilePath))\n```\n\n========================================\n\nCode:\n```text\nProcess.Start(String.Format(\"vbc.exe {0}\", sourceFilePath))\n```\n\n```text\nx = 5\n```\n\n```text\nConsole.Readline();\n```\n\n========================================\n\nComments:\n- I think you vastly underestimate the complexity of compilers. There are entire courses in college over this single topic.\n- Hence my apologies if this was a dumb question. Clearly those were not without warrant. I did google it, as you can see, and I was not expecting a small answer.\n- According to Joel, writing a compiler is two months work for a talented person who read the dragon book: joelonsoftware.com/items/2006/09/01b.html\n- It's definitely a nooby question, but I wouldn't call it a bad one. Clearly you know nothing of what you're asking about, and have no idea what it entails, but you're not going to find out about that without asking somebody. BTW, the second link is indeed useless, and your question about what symbols you need is dead on: it's like enrolling in a sonnet-writing course and being taught how to touch-type.\n- Yep, I know absolutely nothing about this, and it is definitely nooby. And yeah, that second link failed.\n- People overestimate how hard it is to write a compiler. You don't need a degree, you dont need the dragon book. You just need to start small, use existing tools like Antlr, and a bit of hard work. You can have a trivial interpreter in a few days, and slowly build it up. That said, its not clear how good a coder you are. If you're only starting to code, this might not be the best first project.\n- I think Mono has got a vb.net compiler written in vb.net somewhere\n- \"how can I go about writing my own from scratch?\" I cannot assume the user has the vb ide installed on their computer.\n- You don't need the IDE to get the compiler...only the SDK which is free to download.\n- @Cyclone - In all seriousness, re-writing a VB.NET compiler in VB.NET is a massive undertaking and not one that I (or, I'm guessing, anybody else here would want to undertake). Using the freely distributable .NET SDK would be MUCH easier.\n- K, can I have a link? Also, this is for vb.net correct? With the string \"sourceFilePath\", what should be the contents of the source file? This is vb.net, not vb6, so a vbscript file is not a part of this at all, and a vbscript file is the only file I could think of that would be compilable into it as a single file.\n- @Cyclone - microsoft.com/downloads/&hellip;\n- k, now what goes into the file at sourceFilePath?\n- The path to the .vb files you want to compile into the .exe ...but if you're going this route, don't even worry about writing the app. Just call the compiler directly...\n- There is the issue though, I do not want it to compile a normal project though....thanks anyway.\n- Look at my edit about the second link, that is why I said it looked promising, yet in reality it made no sense. I don't intend on specifically getting a degree just so I can make a compiler, or purchase any books on the subject. This seems really more complex than it should be lol...\n- Saw the edit after I posted - honestly that link is a travesty :) True, at its core all you're doing is taking a certain pattern of text and converting it into a certain pattern of 1s and 0s - if you define it that way, then yeah it's definitely easy. Of course, if we that path: - Basketball is really just putting the ball in the hoop - Building a house is really just combining wood and nails - Speaking a foreign language is really just memorization Some people spend years learning to write compilers - it's deceptively complex :)\n- I have become convinced at this point that it would nearly be simpler for me to write my own language entirely, and simply write all logic myself as a parser. Of course, this is also ridiculous, as I have no need for an entirely new language (though I did once attempt to write a Lolcode parser in vb.net, it is the only language that uses \"OH NOES\" as an exception handler.\n- @Cyclone: It would not be simpler for you to write your own language, since the complexity is in compiling anything. Writing any sort of compiler is going to be a massive learning experience for you and a tremendous amount of work, and getting a computer science degree may well be the easiest way to do it.\n- @Cyclone: \"This seems really more complex than it should be...\" How simple do you think it *should* it be to translate human-understandable text into a binary something that can be mashed by a series of electronic logic gates to produce some result that's still human-sensible?\n- I really dislike this answer. It looks like its trying to make it seem really difficult. You dont explain what an interpreter is; your link to parsing discusses the theory of it, instead of linking to a parser generator; and the end just says \"you aren't good enough, get a degree, or at least a book\". Really, a travesty of an answer. -1.\n- Did you the link I provided? That should be enough to get you to scrap the idea of writing it in Notepad...\n- I never intended to write an executable in notepad?\n- Ah... I guess this part of your original post confused me: \"How could I go about making one in a plain text editor if I wanted to?\"\n- I was asking more about the actual structure of the .exe file itself than going out and MAKING one in notepad.\n- It's binary machine code that contains instructions for the CPU to execute, regardless of what language it's written in. (OK, not quite true - if you're writing in VB script, for example, it's text that the VB script runtime compiles into something it interprets and turns into machine code. However, you asked about .EXE files.)\n- Ill add that to my shopping cart and see if I can find any cheaper solutions till then. Thanks!\n- Yes, I have found the necessary code in the namespace you mentioned.","metadata":{"transformedAt":"2026-08-18T18:32:17.841Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":78,"estimatedTokens":2178}}921{"id":"stack-4458047","source":"stackoverflow","questionId":4458047,"title":"Adding Annotation Syntax to C++ Source","tags":["c++","compiler-construction","annotations"],"text":"Title: Adding Annotation Syntax to C++ Source\nTags: c++, compiler-construction, annotations\nSource: Stack Overflow\n\nQuestion:\nI want to create my own custom annotation (just like the Java style annotation) tags in my C++ source. Since standard C++ syntax doesn't allow annotation, I'd like to modify/enhance the compiler for my own needs. \n\nBut does Visual Studio expose its compiler internals for users to modify (e.g. output of its lexer, abstract syntax tree, etc)? If not, are there any third party tools for parsing c++ syntax and allow me to emit my own customized c++ source base on my own annotation?\n\n========================================\n\nTop Answer:\nDoes Visual Studio expose its compiler internals for users to modify (e.g. output of its lexer, abstract syntax tree, etc.)?\n\nNo\n\n Are there any third party tools for parsing C++ syntax and allow me to emit my own customized C++ source base on my own annotation?\n\nClang\n\n========================================\n\nCode:\n```text\ntemplate<bool b, class T> void foo(T &t){if(b) T.do_this(); else T.do_that();}\nclass foobar { public: virtual void do_this(); virtual void do_that(); };\nclass barfoo : public foobar { public: virtual void do_this(); virtual void do_that(); };\n```\n\n```text\nvoid bar(float l){ barfoo t; foo<fsqrt(l) > 1, foobar>(t); }\n```\n\n========================================\n\nComments:\n- Possible duplicate: stackoverflow.com/questions/4689756/java-like-annotations-in&zwnj;&#8203;-c","metadata":{"transformedAt":"2026-08-18T18:32:17.841Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":37,"estimatedTokens":366}}922{"id":"stack-10045340","source":"stackoverflow","questionId":10045340,"title":"c# compiler: get intermediate output","tags":["c#","compiler-construction"],"text":"Title: c# compiler: get intermediate output\nTags: c#, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIs there a way to instruct c# compiler to output source at different stages of compilation: source after syntactic sugar removed, ..., IL.\n\nPerhaps some tools (Resharper?) do this, but I'd like to know how they do that.\n\n========================================\n\nTop Answer:\nIs there a way to instruct c# compiler to output source at different stages of compilation: source after syntactic sugar removed, ..., IL.\n\nAs Jared correctly points out, you can certainly see the IL. The compiler does not at this time expose any more information about its analysis.\n\nThe Roslyn compiler will, as Jon correctly notes. Roslyn will expose the complete *syntactic* analysis in the form of a tree. However, we do not have plans to expose the *syntactic or semantic transformations* in the form of a \"lowered tree\". (For example, query comprehensions lowered to method calls, foreach loops lowered to while loops, using statements lowered to try-finally statements, and so on.)\n\nWe do have plans to expose an API which will allow you to do *queries* of our semantic analysis engine. For example, you'll be able to say \"give me the syntactic analysis of this program\", and then say \"OK, this node right here in the syntactic analysis is an expression; what is its type?\" Or \"what did overload resolution decide about which method this call actually invokes?\" And so on. \n\nWe also plan to expose APIs that allow you to do queries of our flow analysis engine. For example, you'll be able to take a syntax tree and then ask \"given this block of code in this method, what local variables were not assigned before control entered the block, but will be assigned after control leaves?\"\n\nI encourage you to get the preview release of Roslyn if this is the sort of analysis you require. We would love to get as much early feedback as possible. The download is here:\n\nhttp://www.microsoft.com/download/en/details.aspx?id=27746\n\nand the feedback forum is here:\n\nhttp://social.msdn.microsoft.com/forums/en-us/roslyn\n\nMore information on Roslyn can be found here:\n\nhttp://msdn.microsoft.com/en-us/roslyn\n\n Perhaps some tools (Resharper?) do this, but I'd like to know how they do that.\n\nThey write their own syntactic and semantic analyzers.\n\n========================================\n\nComments:\n- I'd also look into NRefactory.\n- It would be helpful to know what some features like lambda expressions transformed to. And I wanted to build simple tool for that.\n- @ren If that is what you want, then I suggest reading Jon Skeet's C# In Depth, Second Edition as it explains some of this better. Or, you could read the CLR Specs? Last, you might find a tool like LinqPad useful?\n- I'll try LinqPad, thanks. It's always better to see then to read about:)\n- Thanks for providing a more detailed explanation of Roslyn. It looks to be a fascinating window into the internals of the system.","metadata":{"transformedAt":"2026-08-18T18:32:17.841Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":46,"estimatedTokens":742}}923{"id":"stack-3421131","source":"stackoverflow","questionId":3421131,"title":"Which backend languages should my compiler target?","tags":["c++","compiler-construction","programming-languages","c-preprocessor"],"text":"Title: Which backend languages should my compiler target?\nTags: c++, compiler-construction, programming-languages, c-preprocessor\nSource: Stack Overflow\n\nQuestion:\nI've written a compiler for a general-purpose programming language that produces an optimised parse tree of its input. This intermediate format is then run through a preprocessor to translate it into a target language for subsequent compilation to a native executable.\n\nAt present the only target language is C++, but I'd like to offer other solutions as well, in case certain programs might benefit from being compiled with a backend that better supports certain constructs in the source language.\n\n**Are there any languages that are designed for or very well suited to the role of compiler target?**\n\nI'm aware of LLVM, and though it's an exciting project in its own right, I believe it's too low-level to target directly. I'm looking for generic, mid- to high-level languages with high-quality implementations, whose syntax is capable of being produced by a **C** preprocessor &mdash; thus nothing like Python or Ruby. Support for lambdas would be nice, but isn't strictly necessary.\n\n========================================\n\nTop Answer:\nIf your backend would be just plain C you wouldn't have support for several constructs of your base language, but you would gain compatibility to other languages (nearly all languages can be easyly linked to C libraries). You could write some parts of your program in your language and some parts in other languages. And its fast to compile. \n\nMany parser generators are generating C code, so its also not uncommon.\n\n========================================\n\nComments:\n- It might be useful to target C# and Java.\n- @Steven: Java is a distinct possibility, but for now I'd like to stay away from .NET, so C# is out.\n- Your call, of course. I mention these because they're on the two most popular VM's and have syntax based on C++. The other idea I'll toss out is Objective C, if only for Mac/iP*d support.\n- Go is definitely a good choice, since it's basically a high-level systems language, but I am worried about the value of targeting it because it's still in its infancy and doesn't yet seem to have a firm hold.\n- Ooh. The ability to generate hype with an in-browser demo is reason enough to use it. I can't believe I didn't think of it.\n- The browser compatibility is definitely nice, as is the ability to attract a large population of developers that cannot really interact with C code easily.\n- I'm accepting this on account of upvotes, even though @6502's answer is more like what I originally wanted.\n- C is a quite reasonable suggestion, but I'm already targeting C++, which amounts to the same thing in terms of binary compatibility with other languages while also providing loads of handy features such as temporaries, exceptions, and templates.","metadata":{"transformedAt":"2026-08-18T18:32:17.841Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":31,"estimatedTokens":716}}924{"id":"stack-46780413","source":"stackoverflow","questionId":46780413,"title":"Why Babel uses a top down parser?","tags":["javascript","parsing","compiler-construction","babeljs","babel-babylon"],"text":"Title: Why Babel uses a top down parser?\nTags: javascript, parsing, compiler-construction, babeljs, babel-babylon\nSource: Stack Overflow\n\nQuestion:\nI'm studying compiler construction and naturally I'm also studying real world implementations of these concepts. One example of this is Babel's parser: Babylon.\n\nI went through Babylon's code and it appears to be using a Top Down parser with embedded ad hoc semantic rules. src\n\nI was expecting Babel to be using a member of the LR parsers and probably a definition file where the grammar productions are coupled together with semantic rules. Why? Well mostly because a bunch of other real world langs use lr parser generators such as Yacc, Bison, et al, that give you this exact interface, and seems to be a clearer and more maintainable way of representing these rules, and even more when you consider that Babel lives on the edge of the Javascript standard, implementing new things all the time.\n\nI also have constructed both top down and bottom up (lr) parsers and I don't see a big implementation difficulty difference between the two (both are equally difficult :) )\n\nSo, why does Babel's parser uses a top down ad hoc syntax directed translations instead of what I see as a more structured approach? What are the design decisions behind that? What am I missing?\n\nThanks!\n\n========================================\n\nComments:\n- Because English is read top to bottom.\n- @PHPglue That’s not what “top-down” refers to. What does English have to do with this?\n- This question probably isn't a good fit for SO since generally the answer is \"because someone decided it should be\". Also in this case Babylon started as a fork of github.com/ternjs/acorn so most architectural questions aren't Babylon-specific.\n- Thanks a lot for your reply, probably the section about hand written parsers is exactly what I was looking for, the grammar is too complicated to fit good enough with parser generators. Thanks a lot!","metadata":{"transformedAt":"2026-08-18T18:32:17.841Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":490}}925{"id":"stack-3709620","source":"stackoverflow","questionId":3709620,"title":"Are printf/sprintf compiler warnings a conceptual break?","tags":["c","compiler-construction","warnings","printf"],"text":"Title: Are printf/sprintf compiler warnings a conceptual break?\nTags: c, compiler-construction, warnings, printf\nSource: Stack Overflow\n\nQuestion:\nI have noticed that a large number of C compilers issue warnings when the conversion specifiers in the format string of the printf/sprintf functions do not match the type or the count of the corresponding arguments.\n\nThat seems to me like a conceptual break since C doesn't have built-in functions according to the language specification.\n\nAll the compiler should know about printf/sprintf is their prototypes and not their semantics. I know that printf/sprintf are standard C functions, but yet they reside in a separate library, libc, and you have to include stdio.h to import their prototypes.\n\nWhat many compilers do instead is analyze the format string which could as well be supplied at runtime.\n\nDoes the above make sense?\n\n========================================\n\nTop Answer:\nIf I read your question correctly, I agree with your premise that verification of `printf` and friends' format strings by the compiler is an activity conceptually unlike the other sorts of static checking (syntax, type, etc.) done by the compiler.\n\nHowever, it is permitted by the standard, and helps us poor programmers out greatly.\n\n========================================\n\nCode:\n```text\nprintf\n```\n\n```text\nmemcpy\n```\n\n```text\n<stdio.h>\n```\n\n```text\n__printf_format_warning\n```\n\n```text\nprintf\n```\n\n```text\nprintf\n```\n\n```text\nprintf()\n```\n\n```text\n#include <stdio.h>\n```\n\n```text\nprintf\n```\n\n```text\nprintf\n```\n\n```text\nprintf\n```\n\n```text\n<stdio.h>\n```\n\n```text\nprintf\n```\n\n```text\nsqrt(144)\n```\n\n```text\nprintf(\"%f\\n\", 144)\n```\n\n========================================\n\nComments:\n- A conceptual break from what?\n- One word: QOI (well: 3 in fact)\n- @Martin: As far as I know, C doesn't have built-in functions. How then the compiler understands the semantics of the printf/sprintf functions when they are defined in an outside library?\n- Nope, no sense. What's a conceptual break in this context?\n- @Blagovest: the semantics aren't defined in any library, they're defined in the standard (mostly: the implementation can add new format specifiers). Your implementation might use some linked shared object to supply the actual code, but that doesn't change the defined behaviour of the the function.\n- @Blagovest Buyukliev: Much of the time these things are enforced by compiler-specific attributes in the function declarations (e.g. `__attribute__((format(printf,m,n)))` in gcc) that say whether a function has `printf`-like semantics.\n- @jamesdlin: +1 for hitting the nail on the head! That really answers my question.\n- \"The Standard requires diagnostics under some circumstances\" -- no it doesn't, see quotation in my answer (it's from ISO/IEC 9899:TC2). Or can you point me to a place (possibly in some other ISO or ANSI C standard?) where it does?\n- I have the draft C99 standard here (don't own a real copy), and in 5.1.1.3 I find \"A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined.\" Is this omitted from the final version?\n- @Roman: Also, it looks to me that that's from the start of Annex I (informative), titled \"Common Warnings\". It refers to warning messages and not diagnostic messages (in standards, the exact wording can be important) and apparently isn't normative anyway.\n- okay, now I see what you're referring to, thanks for explanation.\n- You're not allowed to write your own version of `printf`, period, unless you make it `static`. Redefining external symbols, not to mention ones defined by the standard, results in undefined behavior. Whether you included `stdio.h` or not is a much more minor issue.","metadata":{"transformedAt":"2026-08-18T18:32:17.841Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":15,"totalLines":100,"estimatedTokens":984}}926{"id":"stack-25999540","source":"stackoverflow","questionId":25999540,"title":"How can I create my own programming language targeting the JVM?","tags":["compiler-construction","jvm","programming-languages"],"text":"Title: How can I create my own programming language targeting the JVM?\nTags: compiler-construction, jvm, programming-languages\nSource: Stack Overflow\n\nQuestion:\nI would like to create my own programming language targeting the JVM. I am unsure how to do this. Must I create my own compiler? Do all programming languages have unique compilers, or are there existing ones that can be adapted?\n\nI have found some information about targeting the .NET CLI.\n\nI've also found the Dragon Book on compiler design.\n\n========================================\n\nTop Answer:\nChris K. gave quite a nice answer, however, in one point I (as a person that at least has already written a working compiler for a non-trivial JVM language) must strictly disagree:\n\nThe code generator should indeed generate just Java (or, if you like, Scala, Ceylon, Kotlin, Clojure, ... whatever you like) code in the beginning, for the following reasons:\n\n- the other tasks (lexing, parsing, maintaining the compiler state a.k.a symbol table, semantic analysis, etc.) are already demanding enough. Therefore, learning yet another library is overdoing it, and will delay your first results substantially.\n\n- Once you have everything including code generation and compile your first program, you'll find that your compiler is **full of bugs**, literally. It is much easier to see those bugs manifest themselves in non-sensical, or erronous, Java code, rather than erronous class files. Would you rather get a cryptic message from the byte code verifier or look at the generated code in text form?)\n\n- Code generation should be a separate module anyways, nothing in the rest of the compiler depends (or should depend) on code generation. So it is comparatively easy to replace it once you can be sure that your compiler indeed can make sense of its input (proof of which is compilable java code that stands some tests, etc. To be sure, as long as the class-file generation is not 100% fool proof, it should be an option whether code is generated in Java or in binary. This way, you can compile test programs to Java and to byte code, and run tests with both outcomes. This makes error analysis in cases where a generated class file suddenly fails quite easier.)\n\nI personally would not even begin to generate class files until your compiler, written in your own language, can compile itself into java, and the resulting program can compile the compiler source to exactly the same java code.\n\n========================================\n\nComments:\n- This question is too broad. But in general you will have to first write a parser to parse your language and perhaps compile it into some intermediate representation (like an AST for example, or some intermediate language). After that you will have to translate your intermediate representation into bytecode.\n- Lots of languages target the JVM, other than Java: Scala, Clojure, Groovy, Jython...you should take a look at the source code for their compilers.\n- The llvm tutorial is excellent, it is quite short and very well written. I know that you said that you wanted to target the JVM, however nearly everything that this tutorial covers will help you in understanding the parts required (llvm.org/docs/tutorial)\n- I also found this on stackoverflow: stackoverflow.com/questions/1669/learning-to-write-a-compile&zwnj;&#8203;r\n- @ChrisK I referred that link before. But frankly, wasn't too sure it was a place to get things started.\n- @RohanSethi no problem. Here is another for you, this library is the best parser library that I have come across that runs on the JVM. github.com/sirthias/parboiled. I would start there, and create a recogniser from it for a simple language that recognises '1+2' only. Then add an interpreter, and then generate either java source code to be compiled separately via javac or jvm byte code. Keeping the language simple, and going depth first will give you faster feed back and knowledge of each of the layers before you grow the language. Good luck.\n- @RohanSethi learning to write compilers is quite a dark art, I was lucky enough to learn it at University. You will hit two problems, 1) the theory is very dense and difficult to penetrate and 2) your first twenty attempts will be a speghetti mess. :)\n- @ChrisK I do have the subject in next sem at university. But it's to long to hang on.Really appreciate your efforts.Thanks.\n- @Rohan: You'll waste a lot of time trying to build a compiler without the class. Suggest you hold your breath and take that class. You're not likely to die of old age soon; you have some time.\n- Will consider you suggestion @IraBaxter . Thanks :)\n- @rohan-sethi another important aspect in creation of a programming language is that you should 1st **design** your language. Think about it, try to write a few programs on-paper and \"compile\" them to Java on-paper manually. Once you're happy with the language design, another important and useful step is writing a en.wikipedia.org/wiki/Bootstrapping_(compilers)\n- IDE support is a big win with MPS, the tutorial videos are also quite informative. It also comes with a Java grammer, which one can modify to jump start their own language. It is well worth a look.","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":41,"estimatedTokens":1296}}927{"id":"stack-442325","source":"stackoverflow","questionId":442325,"title":"Chomsky Hierarchy and LL(*) parsers","tags":["parsing","compiler-construction","antlr","theory","chomsky-hierarchy"],"text":"Title: Chomsky Hierarchy and LL(*) parsers\nTags: parsing, compiler-construction, antlr, theory, chomsky-hierarchy\nSource: Stack Overflow\n\nQuestion:\nI want to parse a programming language. I read a lot about formal languages and the Chomsky hierarchy and ANTLR. But I could not find information on how to relate the languages ANTLR v3 as an LL(*) recursive descent parser accepts to the chomsky hierarchy.\n\nHow do the Chomsky types mix with LL(*)? Any information (online, books, papers) are greatly appreciated.\n\nEdit: How do syntactic / semantic predicates and backtracking of ANTLR map into this?\n\n========================================\n\nTop Answer:\nLL(*) is a subset of context-free languages. However, a different question is what antlr can parse, given predicates and backtracking, which extend its abilities.\n\nNote that if we talk about LL(*), that means ANTLR v3, not 2.","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":17,"estimatedTokens":220}}928{"id":"stack-6469954","source":"stackoverflow","questionId":6469954,"title":"Are standard library required to be standard conformant?","tags":["c++","gcc","compiler-construction","standards-compliance","standard-library"],"text":"Title: Are standard library required to be standard conformant?\nTags: c++, gcc, compiler-construction, standards-compliance, standard-library\nSource: Stack Overflow\n\nQuestion:\nAre standard library *required* to be standard conformant? I've this feeling that standard library aren't standard conformant. The basis of this feeling is the error messages generated by the compiler(s). For example, sometime GCC gives error messages which starts with prefix `__gxx` and many others which I don't remember as of now. But seeing them gives me feeling that these are very compiler specific messages, and different compilers wouldn't be able to compile standard library provided by GCC, and vice-versa. Is it true?\n\nThe question can be asked in other words as:\n\n- Can standard library provided by one compiler be compiled with other compilers?\n\n- When we say a particular compiler is standard conformant, does it *automatically* mean that the stdlib which comes with it is also standard-conformant? Or it simply means that this compiler can compile *standard-conformant code* written by us, programmers?\n\n- **Can I use standard library provided by one compiler, in my project which uses a *different* compiler to compile the project?** Is portability same as standard-conformance?\n\nThese questions are different angles to look at the same *big* question. So, please help me understanding what does it exactly mean when we say *compiler X is standard-conformant*.\n\n========================================\n\nTop Answer:\nThe standard library is a detail of implementation. It may not even be 'compiled' in the sense that the standard doesn't require it to consist of 'files' [headers]:\n\n 174) A header is not necessarily a source file, nor are the sequences delimited by in header names necessarily valid\n source file names (16.2).\n\nThe standard carefully eases the requirements on the implementation, so that the library may be 'built-in' to the compiler (a.k.a intrinsics). For example, extending the `std` namespace, or #defining a name used in the standard library gives you undefined behavior.\n\n========================================\n\nCode:\n```text\n__gxx\n```\n\n```text\nshared_ptr\n```\n\n```text\n__gxx\n```\n\n```text\n__gxx\n```\n\n```text\n[lib.requirements]\n```\n\n```text\n[requirements]\n```\n\n```text\nstd\n```\n\n```text\n__MACRO__NAME__\n```\n\n```text\noffsetof\n```\n\n```text\nboost::is_pod\n```\n\n```text\n__\n```\n\n========================================\n\nComments:\n- What do compiler error message formats have to do with libraries and standardization?\n- In fact, the whole reason that `__` is reserved for the implementation is exactly to allow such `__gxx` symbols.\n- The quote is from N3290, it present in C++03/\"17.4.1.2 Headers [lib.headers]\" but *not* in C++98. Interesting...\n- Doesn't really matter; it's a non-normative note. It's there only to make explicit what was already implicit. Quick recipe for SO rep: go through the notes in C++0x and ask the questions that are answered by them ;)","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":81,"estimatedTokens":744}}929{"id":"stack-42373254","source":"stackoverflow","questionId":42373254,"title":"Is there some way to use pointers inside C switch statements?","tags":["c","compiler-construction","interpreter","vm-implementation"],"text":"Title: Is there some way to use pointers inside C switch statements?\nTags: c, compiler-construction, interpreter, vm-implementation\nSource: Stack Overflow\n\nQuestion:\nUsually in a dynamically typed programming language, the object struct has a tag field used to identify the object type.\nFor example:\n\n```\nstruct myObject {\n int tag;\n ...\n}\n```\n\nSo it is easy to perform different actions using a switch statement based on the tag field.\nFor example:\n\n```\nswitch (obj->tag) {\n case OBJ_INTEGER: ...\n case OBJ_STRING: ...\n case OBJ_FUNC:...\n}\n```\n\nIn my case instead of the int tag field I used a void *isa pointer that points to the class that represents that object. Everything worked fine expect that instead of using an elegant switch statement I am forced to use a series of if/else statements. For example:\n\n```\nif (obj->isa == class_integer) {\n ...\n} else if (obj->isa == class_string) {\n ...\n} else if (obj->isa == class_func) {\n ...\n}\n```\n\nI know that I can't use pointers inside a C switch statements but I wondering if I can use some clever trick in order to speedup the series of if statements.\n\n========================================\n\nCode:\n```text\nstruct myObject {\n    int tag;\n    ...\n}\n```\n\n```text\nswitch (obj->tag) {\n    case OBJ_INTEGER: ...\n    case OBJ_STRING: ...\n    case OBJ_FUNC:...\n}\n```\n\n```text\nif (obj->isa == class_integer) {\n    ...\n} else if (obj->isa == class_string) {\n    ...\n} else if (obj->isa == class_func) {\n    ...\n}\n```\n\n```text\nswitch (obj->tag) {\n    case OBJ_INTEGER: do_something_int(obj); break;\n    case OBJ_STRING: do_something_str(obj); break;\n    case OBJ_FUNC: do_something_func(obj); break;\n}\n```\n\n```text\ntypedef void (*ProcessPtr)(dyn_obj * obj);\n\nstruct dyn_type {\n    ... // fields of your current struct\n    ProcessPtr process;\n};\n```\n\n```text\n((struct dyn_type*)obj->isa)->process(obj)\n```\n\n```text\nisa\n```\n\n```text\nisa\n```\n\n```text\nstruct\n```\n\n```text\nstruct dyn_type\n```\n\n```text\nstruct\n```\n\n```text\nprocess\n```\n\n```text\ndo_something_int\n```\n\n```text\nOBJ_INTEGER\n```\n\n```text\ndo_something_str\n```\n\n```text\nOBJ_STRING\n```\n\n```text\nswitch\n```\n\n========================================\n\nComments:\n- What sort of \"speedup\"? Isn't your compiler doing that for you already?\n- You can't use non-constant expressions as the `case` labels.\n- @EugeneSh. I guess OP is already aware of that? :)\n- @SouravGhosh Perhaps he needs a formal description of the problem? Otherwise the answer will be just \"No\".\n- what is class_integer? A constant, an int, A char\n- C doesn't have classes.\n- A quick but dirty solution is to add an integer `tag` field to the `struct` to which `isa` pointer is pointing. However, this would defeat the purpose of having `isa` pointer in the first place.\n- Very soon you will have invented C++. :)\n- This is a general OO pattern by the way: If you find yourself using a conditional/switch statement that looks at an object's class/isa, that's a code smell. You should just be calling a method on it in 99% of the cases.","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":17,"totalLines":145,"estimatedTokens":749}}930{"id":"stack-5367655","source":"stackoverflow","questionId":5367655,"title":"Lexical Analysis","tags":["c++","c","regex","visual-c++","compiler-construction"],"text":"Title: Lexical Analysis\nTags: c++, c, regex, visual-c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nMy target is to make a very simple and basic C syntax checker. (Not a full Compiler but just a basic Program which take a source code as Input and would print out the code back showing the errors). I want to use C++ as the language for this.\n\nCan anyone guide me to write regular expression in 'c++' language for the following:\n\n- Assignment\n\n- For\n\n- switch\n\nAny details/suggestions/guidance further would be much appreciated.\n\n========================================\n\nTop Answer:\nChecking the syntax of C code requires a lot more than regular expressions. You'll need a tool that supports parsing algebraic gammars instead, I'd suggest looking at http://www.gnu.org/software/bison/ which will generate the \"skeleton\" of a parser in C++ so you can edit it and add your syntax-checking code.\n\n========================================\n\nCode:\n```text\nC\n```\n\n```text\nC\n```\n\n```text\nyacc\n```\n\n```text\nlex\n```\n\n```text\nclang\n```\n\n========================================\n\nComments:\n- Note that lexical and syntactic analysis are very different things. Lexical analysis (scanning) divides your input in tokens, syntactic analysis (parsing) makes a syntax tree out of those tokens.\n- LRSTAR is at sourceforge.net/projects/lrstar","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":50,"estimatedTokens":334}}931{"id":"stack-22597619","source":"stackoverflow","questionId":22597619,"title":"Do compilers remove unnecessary parentheses","tags":["java","compiler-construction"],"text":"Title: Do compilers remove unnecessary parentheses\nTags: java, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nFor example if I compile this in Java in Eclipse\n\n```\nintvar = (int)(((var1 * var3))*(7));\n```\n\nwill the compiler clean it up to\n\n```\nintvar = (int) var1 * var3 * 7;\n```\n\nand then would the compiler would do its thing of making it to machine code and stuff.\n\nOr do I have to do something special to the compiler to make it optimize the code?\n\nAlso, would this apply to all other languages that are compiled?\n\n========================================\n\nTop Answer:\nCompiler translates java code to byte code. Byte code does not have parentheses at all. It works with stacks.\n\n========================================\n\nCode:\n```text\nintvar = (int)(((var1 * var3))*(7));\n```\n\n```text\nintvar = (int) var1 * var3 * 7;\n```\n\n```text\nintvar = (int)(((var1 * var3))*(7));\n```\n\n```text\n4: iload_1       \n   5: iload_2       \n   6: imul          \n   7: bipush        7\n   9: imul          \n  10: istore_3\n```\n\n```text\nintvar = (int) var1 * var3 * 7;\n```\n\n```text\n4: iload_1       \n   5: iload_2       \n   6: imul          \n   7: bipush        7\n   9: imul          \n  10: istore_3\n```\n\n```text\njavap -c\n```\n\n```text\nintvar = (int)((var1*(var2*7)))\n```\n\n========================================\n\nComments:\n- The second line is different from the first line. If `var3` is `double` for example, then the result of second line is `double`. In first line, the result with the same condition, will be `int`\n- lets said I never use doubles and just floats but mostly ints\n- then there is no point of having second line with (int)\n- oops i was just thinking of examples with paratheses not about that stuff\n- This is wrong answer. First, compiler transform code into tree, then it optimizes the code (like removing unnecessary casting due to types it produces) and THEN it is transformed into the byte code.\n- @libik Can we say this answer is an abstraction over the entire complex process compiler follows behind the scene? And then can we say, this abstraction is not wrong?\n- @RohitJain - sure, did you ever try to do the following : create your own language, then create your own compilator to transform it into your own bytecode transformation and then create virtual machine which is able to run it? I did. And I can tell you - even if it is transformed into the byte code, it can contain redundancies if not optimized.\n- libik what was the name of the language you created?\n- @ibik There is nothing wrong with this answer. You've provided a bit more detail about it, some of which may or may not apply to a specific Java compiler. However there is nothing incorrect about the answer as stated.\n- @androidmaster - it does not have a name, it was a school project (very interesting one). However, it is much more easier than I ever imagined (two people, 4 credits, one semester and it had garbage collector, abstract class and interfaces (we inspired by Java a lot) )\n- @libik to me thats mind blowing, am only 15 so am limited to knownledge of programming that I get from tutorials on the internet. I want to take classes when am older to learn C++\n- @EJP - there is a LOT wrong with an answer. Question in fact was : \"does Java compiler optimize code\". And then he added example. That example was not about \"real\" parenthesis, it was about if it is transformed to something as simple as possible instead of additional casting. AlexR answered in very confusing way that there are not parentheses and it looks like, because of that, the code is \"optimized\", because it does not have \"parenthesis\". Im sorry, it is wrong and it always will be.\n- @androidmaster - maybe better to start with Java or C#, it is more common these days and it is much more user-friendly. Especially Java, which is very \"strict\" in many ways. And I like that, because it helps you to not be confused and to do less mistakes.\n- @libik I already know lots of Java, am not a beginner. I already know about most of the swing, android dev and other stuff with java. Am been using it the past 2 years :D I also like how Java is strict, that s why I dont like python or javascript very much...\n- @ibik I don't see anything wrong with the answer, or anything confusing about it either. He is quite correct to state that there are no parentheses in byte codes. Your detail about parse trees and optimization isn't actually relevant. A one-pass compiler would behave like this with no parse tree or optimisation phase at all. Removal of redundant parentheses isn't an 'optimisation'. Rather, it happens naturally during parsing. And you've mis-stated the question. It is about removal of redundant parentheses, not whether the Java compiler optimises code.\n- @androidmaster - if you want, I can send you everything I told you about (it is written in Java), - libor.vilimek at gmail\n- @ibik In fairness to this poster you should therefore remove all your comments saying this answer is wrong. Future readers won't necessarily read all the way to the bottom of this exchange to see your retraction.\n- Cool, I wish I could see the code after its compiled like that\n- @androidmaster But you can! Just use `javap -c SomeFile`.\n- How is that possible??? Do I have to use the sdk or is there a option in eclipse? So we use which ever one that is easier to read, right?\n- @androidmaster Run that command from terminal or command line whichever you have.\n- @androidmaster You use `javap` on the command line. If you're on Eclipse, try the ASM plugin (which is what I use, it's quite handy).\n- Well in those cases the parentheses aren't 'unnecessary'.","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":99,"estimatedTokens":1403}}932{"id":"stack-11794324","source":"stackoverflow","questionId":11794324,"title":"Can the CPU understand programming languages?","tags":["compiler-construction","cpu"],"text":"Title: Can the CPU understand programming languages?\nTags: compiler-construction, cpu\nSource: Stack Overflow\n\nQuestion:\nProgramming books sometimes point out that the CPU doesn’t understand the C++ language (or any high level programming language). All the C++ statements must be translated into machine code before they can be executed. But who does the translation?\n\nOh, that’s no mystery, they say; the translation is done by the compiler—which itself is a computer program. But in that case, the computer is doing the translation.\n\nThis seems to me an insolvable paradox. The CPU (the “brain” at the heart of the computer) doesn’t understand a word of C++, yet it performs the translation between C++ and its own internal language. Isn’t that a contradiction?\n\n========================================\n\nComments:\n- But it: idc.ac.il/tecs","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":15,"estimatedTokens":211}}933{"id":"stack-10560701","source":"stackoverflow","questionId":10560701,"title":"Branch prediction in compiler level","tags":["compiler-construction","branch-prediction"],"text":"Title: Branch prediction in compiler level\nTags: compiler-construction, branch-prediction\nSource: Stack Overflow\n\nQuestion:\nI have been reading about branch prediction but the only implementation I find are mostly in the hardware side of computer. processors seem to take care of most of the prediction. My question is that, can a compiler do a branch prediction? \nThe only thing I found is 2 methods, function inlining and loop unrolling. Are these considered correct? Are they still used?\n\n========================================\n\nCode:\n```text\nif (exp1 relop exp2)\n      X = Y\n  endif\n```\n\n```text\n<compute exp1 and exp2>\n  CMP  exp1,exp2 ; sets condition code\n  MOVif_relop  X,Y\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":22,"estimatedTokens":172}}934{"id":"stack-1084975","source":"stackoverflow","questionId":1084975,"title":"Looking for ideas on a computer science course project","tags":["php","xml","parsing","compiler-construction","computer-science"],"text":"Title: Looking for ideas on a computer science course project\nTags: php, xml, parsing, compiler-construction, computer-science\nSource: Stack Overflow\n\nQuestion:\nHey. I'm taking a course titled Principles of Programming Languages, and I need to decide on a project to do this summer. Here is a short version of what the project needs to accomplish:\n\n\"The nature of the project is language processing. Writing a Scheme/Lisp processor is a project of this type. A compiler for a language like C or Pascal is also a potential project of this type. Some past students have done projects related to databases and processing SQL. Another possible project might relate to pattern matching and manipulating XML. Lisp, Pascal, and C usually result in the most straight forward projects.\"\n\nI am very interested in web technologies, and have some experience with PHP, MySql, JavaScript, etc. and I would like to do something web oriented, but I'm having trouble coming up with any ideas. I also want this to be a worthwhile project that could have some significance, instead of just doing the same thing as everyone else in class.\n\nAny ideas? Thanks!\n\nEDIT: I really like the idea of a Latex to XHTML/MathML translator, and I passed the idea to my instructor, in which he wrote back:\n\n\"I think the idea is interesting, my question (and yours) is whether it is appropriate.\n\nI think of LateX as a low-level mark-up language. I'm wondering if converting this to XHTML or MathML is really a change in levels and complexity. I think you can make your point with a little more discussion and some examples. You might also think of some other mark-up constructs which made it easier to describe equations.\"\n\nAny ideas on how to convince him this may be appropriate, or any extensions of this idea that could work for the goals of my project?\n\nThanks for all the responses so far!\n\n========================================\n\nTop Answer:\nHm, neat! Maybe:\n\n`1.` A web-based language interpreter. eg, a very simple assembly interpreter in javascript, or a PHP-based C interpreter (PHP script reads C code, and executes it in some sort of sandboxed kind of way. Obviously it would only be able to implement a small subset of the C language)\n\n`2.` Maybe some automated way to transform PHP data structures (like PHP arrays) into SQL queries, and vice versa. That kind of stuff has already been done, but you might be able to do something which (for example) takes an SQL query and creates the array datastructure that would be needed to \"hold\" the information returned by the SQL. It could support complex things like JOINS and GROUP BYs.\n\n`3.` Maybe a C-to-PHP compiler? (or a PHP-to-C compiler, to be able to run simple PHP code natively. Use this with any combination of languages)\n\nedit:\n\n`4.` Maybe a regex-to-C parser. That is, something that takes a regex, and generates C code to match that pattern. Or something which takes a regex, and converts it into an FSM which represents the \"mathematical\" translation of that expression. Or the opposite - something which takes an FSM for a CFL and generates the perl-syntax regex for it.\n\n`5.` Maybe an XML-to-PHP/MySQL parser. eg, an XML file might contain information about a database and fields, and then your program creates the SQL to create those tables, or the HTML/PHP code for the forms.\n\nBest of luck!\n\n========================================\n\nCode:\n```text\n1.\n```\n\n```text\n2.\n```\n\n```text\n3.\n```\n\n```text\n4.\n```\n\n```text\n5.\n```\n\n```text\ndet = (b*b - 4*a*c); det_sqrt = sqrt(det); etc\n```\n\n```text\n\\begin{eqnarray}\n```\n\n========================================\n\nComments:\n- See also stackoverflow.com/questions/907856/&hellip;\n- +1 make a tokenizer that given a SQL query parses it in a php array structure.. and of course when changing the array... it generatens the SQL... this would be useful and has a good level of complexity\n- I've expanded on my answer in which I proposed the LaTeX-to-MathML translator. I don't know if my elaboration addresses the issues your instructor raised, but I argue more passionately and in greater detail for why such a translator would be of significant real-world benefit. I would really love to see this happen. Good luck!\n- You might want to take a look at an existing (in-progress) project for converting LaTeX to MathML: hackage.haskell.org/package/texmath-0.1.0.1\n- A Python-to-C compiler isn't that simple, considering Python is a dynamically typed language and C is not. It's theoretically possible, but not a summer-only project IMO. (PyPy did it, but they cheated by using a strict subset of the language.)\n- 4 is what excites me\n- i don't think writing a webserver will qualify as language processing.\n- Ooh, or he could write a code-obfuscater. There seem to be a dearth of good open-source ones out there anyway. (aside from the merits of such a program, that would be an interesting thing to write - a number of lexicographical \"gotchas\" would surely crop up.)\n- This is going to be pretty close to what I'm going to end up doing, thanks for the response!\n- ohman, there are so many good things that would happen with LaTeX-to-anything translators. I know that there are already latex-to-html things out there, but they're all perl-shell-scripts. Nothing that you could easily use as a PHP module or C module or something.\n- although a really cool idea, it just didn't fit the project description, although I will keep this in the back of my mind as a possible side project someday, thank you for your answer","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":87,"estimatedTokens":1374}}935{"id":"stack-6185346","source":"stackoverflow","questionId":6185346,"title":"Parsing calculation tree from string","tags":["c#","parsing","compiler-construction"],"text":"Title: Parsing calculation tree from string\nTags: c#, parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI there any built in library in c# or automatic code generator which recieves configuration file and builds parser of calculaton tree from string if there is no could you please help me with advice\nExample:\n\"-2+5>3\" I would like to build calculation tree where Thank you.\n\n========================================\n\nTop Answer:\nTake a look at sprache. It's a simple parser which would allow you to build the expression tree and then evaluating it should be the easy bit.\n\n========================================\n\nComments:\n- Great solution , from different point of view you just let the c# compiler to do the parsing!!!! Thank you very much!","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":18,"estimatedTokens":191}}936{"id":"stack-23407229","source":"stackoverflow","questionId":23407229,"title":"ghci compiler optimization: calling a function with same parameter twice","tags":["haskell","optimization","compiler-construction","ghci"],"text":"Title: ghci compiler optimization: calling a function with same parameter twice\nTags: haskell, optimization, compiler-construction, ghci\nSource: Stack Overflow\n\nQuestion:\nIn the simple code below, part of the definition of a function that deletes an element from a binary search tree:\n\n```\ndeleteB x (Node n l r) | x == n = Node (leastB r) l (deleteB (leastB r) r)\n```\n\ndoes the compiler optimize the code so that it calls (least B r) only once as if it were:\n\n```\ndeleteB x (Node n l r) | x == n = Node k l (deleteB k r)\n where k = leastB r\n```\n\n?\n\nIn other words, is the compiler able to understand that since parameter r isn't changed within the body of the function deleteB, the result of the call of the same function (leastB) on it can't give different results, hence it is useless to compute it twice?\n\nMore generally, how would I be able to understand if the compiler does this optimization or not in case amazing stackoverflow did not exist? thanks\n\n========================================\n\nTop Answer:\nIf you want to know what GHC \"really did\", you want to look at the \"Core\" output.\n\nGHC takes your Haskell source code, which is extremely high-level, and transforms it into a sequence of lower and lower-level languages:\n\nHaskell &rArr; Core &rArr; STG &rArr; C&minus;&minus; &rArr; assembly language &rArr; machine code\n\nAlmost all of the high-level optimisations happen in Core. The one you're asking about is basically \"common subexpression elimination\" (CSE). If you think about it, this is a time / space tradeoff; by saving the previous result, you're using less CPU time, but also using more RAM. If the result you're trying to store is tiny (i.e., an integer), this can be worth it. If the result is huge (i.e., the entire contents of that 17GB text file you just loaded), this is probably a Very Bad Idea.\n\nAs I understand it (&rArr; not very well!), GHC tends not to do CSE. But if you want to know for sure, in your specific case, you want to look at the Core that your program has actually been compiled into. I *believe* the switch you want is `--ddump-prep`.\n\nhttp://www.haskell.org/ghc/docs/7.0.2/html/users_guide/options-debugging.html\n\n========================================\n\nCode:\n```text\ndeleteB x (Node n l r) | x == n = Node (leastB r) l (deleteB (leastB r) r)\n```\n\n```text\ndeleteB x (Node n l r) | x == n = Node k l (deleteB k r)\n                          where k = leastB r\n```\n\n```text\nn = 1000000\nx = (length $ map succ [1..n], length $ map pred [1..n])\n```\n\n```text\nn = 1000000\nl = [1..n]\nx = (length $ map succ l, length $ map pred l)\n```\n\n```text\n[1..n]\n```\n\n```text\nsucc\n```\n\n```text\npred\n```\n\n```text\nmap\n```\n\n```text\nlength\n```\n\n```text\nsucc\n```\n\n```text\npred\n```\n\n```text\nlength\n```\n\n```text\nl\n```\n\n```text\nx\n```\n\n```text\nn\n```\n\n```text\n--ddump-prep\n```\n\n========================================\n\nComments:\n- GHC will not do this: haskell.org/haskellwiki/GHC/&hellip;\n- I think GHC will actually go out of its way to not do it - as you have tools that let you explicitly say what you want (`where`, `let`). +1 for Core inspection advice tho\n- Actually there was recent discussion on Haskell-Cafe where GHC was floating specifically list literals... hmm\n- @jozefg Interesting. I confess I am not up to date with the GHC optimizations. Yet, floating and CSE are different. Floating seems as dangerous in certain cases, though.","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":117,"estimatedTokens":843}}937{"id":"stack-19366272","source":"stackoverflow","questionId":19366272,"title":"what is the initial step to write a bootstrapping compiler for a new language?","tags":["compiler-construction"],"text":"Title: what is the initial step to write a bootstrapping compiler for a new language?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nSuppose you were creating a compiler for a new language, say Big-Lang .If you were going to bootstrap a compiler for Big-Lang, you would write a compiler for Big-Lang-lite, the smallest possible subset of the Big-Lang. Now , I want to know that the compiler for Big-Lang-lite can be written in Big-Lang or we will have to use some other language, if we are making a bootstrap compiler?\n\n========================================\n\nTop Answer:\n(very interesting question, but perhaps border-line on stackoverflow; https://softwareengineering.stackexchange.com/ is probably a better place for that question)\n\nIn addition of Jozefg's answer, I would also add that in practice the steps are rather a loop: \n\n- you design a \"small\" language X\nyou code a poor compiler (a *primordial compiler*, conceptually used only once) PC for a subset of the above X language in a *different* language Y; this compiler *PC* should be a quick-and-dirty effort, because you'll use it conceptually once and you'll be the single user of *PC*: you don't mind about good diagnostics (so aborting on first error is ok); you don't mind about performance; and you don't care much about the target machine (you could have PC targetting some poor C++ generated code, or whatever you like). By the way, PC could be an interpreter.\n\nyou code a better compiler BC in X to compile X (here the target language T is important). A this point, you probably realize that your design of X language is lacking some features; in that case, goto step 1 (by enhancing the design of X)\n\n- You also add more features into BC, notably better diagnostics, better generated code, etc. Again, you might goto step 3 (improve the code of BC) or even goto step 1 (design better X) then to step 3 (improve the code of BC, in particular to let it handle the new features of X, and later on use the new features of X)\n\n- You test your work by compiling BC with BC\n\n- You probably want to rewrite some code in BC by taking advantage of new features of X. Again, iterating on step 1 and 3.\n\nAlso, you could bootstrap a language by first making an interpreter of that language and then coding a compiler (to be running on that interpreter first).\n\nYou may (or not) want to work on various versions of PC and BC. In particular it could happen that the current version of BC is not compilable by BC (same or immediately previous version); then you have to temporarily play with various versions - or even add temporary hacks inside BC.\n\nOnce you have a BC able to compile itself, you can throw PC.\n\nThe whole point is that designing and implementing the language is a circular work (and by implementing your language, you understand better what features you want inside and how to implement them).\n\nOf course, you need to keep a working version of BC. This means that e.g. you backup (or even version control) the snapshot of the \"target\" code of BC compiled by BC. In Ocaml it is the bytecode file `bootstrap/ocamlc` (and the fact that Ocaml has a portable bytecode VM helps a lot); a similar approach is taken by Scheme 48 and perhaps Chicken Scheme; in MELT (a lisp-like domain specific language I'm working on to extend and customize the GCC compiler), it is the generated C++ files `melt/generated/*.cc`; both Ocaml and MELT keep the \"compiled compiler\" under version control (and distribute it...). In MELT the primordial compiler PC was a Common-Lisp program (accepting a quite small subset of what current MELT language is), and BC is the bootstrapped MELT compiler (files `melt/warmelt*.melt` for the MELT source code and `melt/generated/warmelt*.cc` for the generated C++ code). Feel free to ask questions about MELT bootstrap on its googlegroup list. The Rust language has a slightly different approach: the bootstrap build of it fetches some executable file (of some older version of the compiler) on the Web.\n\nBootstrapping a language is more an art than a science. By experience, it is usually worthwhile to go in small steps. You might want to read J.Pitrat artificial beings : the conscience of a conscious machine (J.Pitrat's favorite theme is that strong artificial intelligence is a bootstrapping process: you need strong AI to implement strong AI, see also this paper). You could also read C.Queinnec Lisp in Small Pieces (if you read french, read the latest french version) explaining how Lisp-like implementations can be bootstrapped.\n\n### additional reference\n\nYou definitely should also read J.Pitrat's blog (since J.Pitrat worked half of his career on bootstrapping artificial intelligence).\n\n========================================\n\nCode:\n```text\nbootstrap/ocamlc\n```\n\n```text\nmelt/generated/*.cc\n```\n\n```text\nmelt/warmelt*.melt\n```\n\n```text\nmelt/generated/warmelt*.cc\n```\n\n========================================\n\nComments:\n- So basically we need to first write compiler for X in some other language and then in X. Won't it be doubling our efforts?\n- It is not really doubling the effort (and it is a necessary step, unless you can write the entire compiler in the target language). In particular, it makes you understand the details of the implementation of a compiler for X (and the evil is in the details)\n- @Surya Just think, if no implementation for a language exists, and you then write a program in that language, how would you run such a language. You necessarily must implement a language in another existing language\n- Or hardwire it into the computer\n- What's a \"kernel\" language? What differentiates it from the \"full\" language?\n- +100 this is the way it is done for sure (I've done it more than once but for porting an existing language to a new architecture - you do it in steps)","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":69,"estimatedTokens":1452}}938{"id":"stack-21815372","source":"stackoverflow","questionId":21815372,"title":"What happens to floating point numbers in the absence of an FPU?","tags":["c++","c","compiler-construction","floating-point","embedded"],"text":"Title: What happens to floating point numbers in the absence of an FPU?\nTags: c++, c, compiler-construction, floating-point, embedded\nSource: Stack Overflow\n\nQuestion:\nIf you are programming with the C language for a microprocessor that does not have an FPU, does the compiler signal errors when floating point literals and keywords are encountered (0.75, float, double, etc)?\n\nAlso, what happens if the result of an expression is fractional?\n\nI understand that there are software libraries that are used so you can do floating-point math, but I am specifically wondering what the results will be if you did not use one. \n\nThanks.\n\n========================================\n\nTop Answer:\nFloating-point is a required part of the C language, according to the C standard. If the target hardware does not have floating-point instructions, then a C implementation must provide floating-point operations in some other way, such as by emulating them in software. (All calculations are just functions of bits. If you have elementary operations for manipulating bits and performing tests and branches, then you can compute any function that a general computer can.)\n\nA compiler could provide a subset of C without floating-point, but then it would not be a standard-compliant C compiler.\n\n========================================\n\nCode:\n```text\nfloat\n```\n\n```text\ndouble\n```\n\n```text\nfloat\n```\n\n```text\ndouble\n```\n\n```text\nfor\n```\n\n```text\nwhile\n```\n\n```text\n1.2 * 2.5\n```\n\n```text\nfmul( 1.2, 2.5 )\n```\n\n========================================\n\nComments:\n- I see. So, even if a processor cannot handle floating point calculations natively, the C compiler MUST provide the software to do this. Am I correct? When you say provide the software library, does that mean that I, as the programmer, must make explicit function calls to do floating point math, or can the compiler handle float a = 3.0f/4.0f for me 'behind-the-scenes'?\n- @jrahhali: The compiler is required to understand and implement `float a = 3.0f&#47;4.0f;`.\n- This answers the question in my comment to Steve Jessop. Thanks","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":61,"estimatedTokens":520}}939{"id":"stack-3232614","source":"stackoverflow","questionId":3232614,"title":"how does an optimizing c++ compiler reuse stack slots of a function?","tags":["c++","optimization","memory","compiler-construction","stack"],"text":"Title: how does an optimizing c++ compiler reuse stack slots of a function?\nTags: c++, optimization, memory, compiler-construction, stack\nSource: Stack Overflow\n\nQuestion:\nHow does an optimizing c++ compiler determine when a stack slot of a function(part of stack frame of a function) is no longer needed by that function, so it can reuse its memory? .\n\nBy stack slot I mean a part of stack frame of a function, not necessarily a whole stack frame of a function and an example to clarify the matter is, suppose we have a function that has six integer variables defined in its scope, when it's time to use sixth variable in the function, fifth variable's become useless so compiler can use same memory block for fifth and sixth variables.\n\nany information on this subject is appreciated.\n\n========================================\n\nTop Answer:\nEDIT: I interpreted the question to mean, \"how does the compiler reuse a particular memory word in the stack?\" Most of the following answers that question, and a note a the end answers the question, \"how does the compiler reuse all the stack space needed by a function?\".\n\nMost compilers don't assign stack slots first. Rather, what they do, for each function body, is treat each update to a variable, and all accesses to that variable that can see *that* particular assignment, as a so-called *variable lifetime*. A variable which is assigned multiple times will thus cause the compiler to create multiple lifetimes. \n\n(There are complications with this idea that occur when multiple assignments can reach an access through different control paths; this is solved by using a clever enhancement to this idea called static single assignment, which I'm not going to discuss here). \n\nAt any point in the code, there are a set of variable lifetimes that are valid; as you choose differnt code points, you have different valid variable lifetimes. The compiler's actual problem is to assign different registers or stack slots of each of the lifetimes. One can think of this as a graph-coloring problem: each lifetime is a node, and if two lifetimes can overlap at a point in the code, there is an \"interference\" arc from that node to the other node representing the other lifetime. You can color the graph (or equivalently use numbers instead of colors), such that no two nodes connected by an interference arc have the same color (number); you may have to use arbitarily large numbers to do this, but for most functions the numbers don't have to be very large. If you do this, the colors (numbers) will tell you a safe stack slot to use for the assigned value of the particular variable lifetime. (This idea is normally used in roughly two phases: once to allocate registers, and once to allocate stack slots for those lifetimes that don't fit into the registers).\n\nBy determining the largest number used as a color on the graph, the compiler knows how many slots are needed in the worst case, and can reserve that much storage at function entry time.\n\nThere's lots of complications: different values take different amounts of space, etc., but the basic idea is here. Not all compilers use the graph coloring technique, but almost all of them figure out how to assign stack slots and registers in a way to avoid the implied interference. And thus they know stack slot numbers and the size of the stack frame.\n\nEDIT... while typing, it appears that the question has been interpreted as \"when does the stack frame for a function vanish\"? The answer is, at function exit. The compiler already knows how big it is. It has no need to push or pop onto the stack during function execution; it knows where to put everything based on the stack slot numbering determined by the graph coloring.\n\n========================================\n\nCode:\n```text\njump\n```\n\n```text\ncall\n```\n\n========================================\n\nComments:\n- what do you mean by stack slot?\n- You will have to explain what you mean by a \"stack slot of a function\".\n- I would guess by \"stack slot\" he means \"stack frame.\"\n- Does it? Use _alloca() or whatever it is called on your CRT to make it more interesting.\n- \"Stack slot\" isn't that ambiguous. It's part of a simplified, RISC-y view of the stack. Basically the simplification is that all data that can be pushed on the stack is the same size - pointer, integer or float. Assume it's 4 bytes. A 12 byte stack frame would then contain 3 stack slots, i.e. would be able to hold three variables.\n- Nothing wrong with the answer, except you haven't stated when the stack space is reserved. Of course it depends on the compiler but generally there's no reason not to reserve all the space immediately in the function prologue. It's not like sub esp, 0x10 erases 16 bytes or something, so there's no advantage in not reserving it all up front.\n- A note to \"flow analysis\" part of your comment. Usually they can't be re-ordered or freed up before leaving the scope, since it might have break RAII.\n- The \"flow analysis\" would have to include destructors. But destructors can be called in the middle of a function (whenever any scope ends) or may be inline and not actually use the object (if trivial or simply counting live objects).\n- @wj32, @doc, @Ben Voigt: You're both right, of course. It's barely conceivable that some compiler writer might strive to minimize stack useage under some very constrained conditions, e.g. a rarely entered IF block in mid-function using up many megabytes of local storage might negatively affect cache locality... but I agree, in general I'd expect what wj32 describes. Especially if you're not willing to be VERY careful about what doc mentions.\n- The compilers I know do reuse stack variables. Also modern compilers allocate the whole stack frame on function entry, rather than allocating individual stack space where it's needed. This also includes parameters to subfunctions that are being called. For MS x64 ABI this is actually mandatory, due to the way stack unwinding on exceptions is handled. When you disassemble code generated by MSVC you will often see function parameters getting reused as local variables whose address is required, after loading the original parameter value into a register.\n- nice technical description though since I'm not an expert in compiler field I couldn't understand that graph algorithm well, thanks anyway.\n- That graph stuff is just a way to be precise. Use a piece of paper, and simulate what this says for a 1 page algorithm. That will make the intuition clearer. Once you see that, the graph scheme will seem pretty obvious. If you really want to dig into it, go read about \"Register Allocation by Graph Coloring\". That'll be *really* precise, but it will have pretty graph pictures all drawn in it for you.","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":54,"estimatedTokens":1684}}940{"id":"stack-6359427","source":"stackoverflow","questionId":6359427,"title":"Is this the job of the lexer?","tags":["parsing","compiler-construction","tokenize","lexical-analysis"],"text":"Title: Is this the job of the lexer?\nTags: parsing, compiler-construction, tokenize, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nLet's say I was lexing a ruby method definition: \n\n```\ndef print_greeting(greeting = \"hi\") \nend\n```\n\nIs it the lexer's job to maintain state and emit relevant tokens, or should it be relatively dumb? Notice in the above example the `greeting` param has a default value of `\"hi\"`. In a different context, `greeting = \"hi\"` is variable assignment which sets `greeting` to `\"hi\"`. Should the lexer emit generic tokens such as `IDENTIFIER EQUALS STRING`, or should it be context-aware and emit something like `PARAM_NAME EQUALS STRING`?\n\n========================================\n\nTop Answer:\nI think that lexer should be \"dumb\" and in your case should return something like this: DEF IDENTIFIER OPEN_PARENTHESIS IDENTIFIER EQUALS STRING CLOSE_PARENTHESIS END.\nParser should do validation - why split responsibilities.\n\n========================================\n\nCode:\n```text\ndef print_greeting(greeting = \"hi\")  \nend\n```\n\n```text\ngreeting\n```\n\n```text\n\"hi\"\n```\n\n```text\ngreeting = \"hi\"\n```\n\n```text\ngreeting\n```\n\n```text\n\"hi\"\n```\n\n```text\nIDENTIFIER EQUALS STRING\n```\n\n```text\nPARAM_NAME EQUALS STRING\n```\n\n```text\nIDENTIFIER EQUALS STRING\n```\n\n========================================\n\nComments:\n- Without context sensitivity, how do you pull off syntax such as `new Map>()`?","metadata":{"transformedAt":"2026-08-18T18:32:17.842Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":64,"estimatedTokens":352}}941{"id":"stack-3508628","source":"stackoverflow","questionId":3508628,"title":"Meaningful diagnostic messages","tags":["c++","compiler-construction","standards","diagnostics"],"text":"Title: Meaningful diagnostic messages\nTags: c++, compiler-construction, standards, diagnostics\nSource: Stack Overflow\n\nQuestion:\nLooking at several posts, I get a feel that many of the questions arise because compilers/implemenetation do not emit a very meaningful message many times (but not always). This is especially true in the case of templates where error messages could be at the least very daunting. A case in point *could* be the discussion topic\n\nTherefore, I would like to understand a few things:\n\na) Why is it that compilers are sometimes unable to give more meaningful/helpful error messages? Is the reason purely practical or technical or is there something else. (I don't have a compiler background)\n\nb) Why can't they give a reference to the *most* relevant *conforming* C++ Standard Verse/section, so that developer community can learn C++ better?\n\nEDIT: \n\nRefer the thread here for another example.\n\nEDIT:\n\nRefer the thread here for another example.\n\n========================================\n\nTop Answer:\nThe fundamental problem is that compiler diagnostics deal with things you **haven't** written.\n\nIn order to give you a meaningful error message, the compiler has to guess what you *meant*, and then tell you how your code differs from that.\n\nIf you're missing a semicolon, the compiler obviously can't see that semicolon anywhere. Of course, one of the things it can do is to guess \"maybe the user is missing a semicolon. That's a common mistake, after all\". But where should that semicolon have been? Because you made an error, the code can't be parsed into a syntax tree, so there's no clear indicator that \"this node is missing from the tree\". And there might be more than one place where a semicolon could be inserted so that the surrounding code would parse correctly. And moreover, how much code are you going to try to parse/recompile once you've found what might be the error? The compiler could insert the semicolon, but then at the very least it has to restart parsing of that block of code. But maybe it introduced errors further down in the code. So maybe the entire program should be recompiled, just to make sure the fix the compiler came up with was actually the right one. But that's hardly an option either. It takes too long.\n\nSay you have some code like this:\n\n```\nstruct foo {\n ...\n}\n\nvoid bar();\n```\n\nwhat is the error here? Looking at it, you and I would say \"you're missing the semicolon after the class definition\". But how can the compiler tell? `void` could be a typo. Perhaps you actually intended to write the name of an instance of type `foo`. then the real error would be that it is followed by what now looks like a function call.\n\nSo the compiler has to guess. \"This looks like it could have been a class definition, and what comes after it looks like it the name of a type. If that is true, the user is missing a semicolon to separate them\".\n\nAnd guessing isn't a very precise science. And matters are further complicated because every time the compiler tries to be clever and makes a guess, it's only going to add confusion if the guess is wrong.\n\nSo sometimes, it might be better to output a short, terse message saying only what we're sure of (say, that a class definition cannot be followed by a type name). That's not as helpful as saying \"you're missing a semicolon after the class definition\", but it's less harmful if the compiler guesses wrong.\n\nIf it tells you you're missing a semicolon, and the error was actually something else, it's just misleading you. So maybe a terse and less helpful error message is better in the worst case, even if it isn't as nice in the best case.\n\nWriting good compiler errors isn't easy, especially not in a messy language like C++.\nBut when that is said, some compilers (including MSVC and GCC) could be a lot better. I believe that better compiler diagnostics are one of the primary goals of Clang.\n\n========================================\n\nCode:\n```text\n;\n```\n\n```text\nstruct foo {\n ...\n}\n\nvoid bar();\n```\n\n```text\nvoid\n```\n\n```text\nfoo\n```\n\n========================================\n\nComments:\n- The question is pretty general… often messages *are* helpful. Maybe you could quote that other discussion and suggest how that message could be better?\n- he he :-). compiler errors are meaningful, but understanding the meaning takes practice. after doing some meta-programming this year, I am able more or less to track g++ errors due to templates. hopefully sometime I will speak fluent \"compiler\"\n- I asked somewhat related question months ago, stackoverflow.com/questions/2886984/&hellip;, may it may help you if you do meta-programming with templates\n- The clearest error message I've ever seen comes from gcc when you use `std::type_info` without `#include `. It says something along the lines of \"You must include before using this type.\" Its the only time I ever get annoyed with the compiler for doing what its supposed to do instead of what I want it to do.\n- Actually, offering better diagnosis messages is one of the stated goals of CLang. We can only hope they succeed.\n- +1 for STLFilt I meant diagnosable conditions, conditions which should be diagnosed by the implementation. The point about throwing up Reference to the Standard could be controlled by some sort of a switch/flag for the more interested users\n- Most of the time, when examining code, the compiler has to pull in more than one section of the standard. The sheer amount of jugglery that you'd have to do with text is reason enough not to even think about it. Other reasons, well, I have mentioned them. The moot point is to get things done as easily as it can be. The compiler authors don't want you to ruminate on how accurately they can quote the standard but rather how accurately they have implemented the wordings! YMMV.\n- I'll have to disagree about the semi-colon or curly brace issue. Crimson Editor (not a compiler - mind you) can tell you which curly brace goes where and if one is missing simply by placing your cursor. I imagine any good compiler could do the same thing.","metadata":{"transformedAt":"2026-08-18T18:32:17.843Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":87,"estimatedTokens":1516}}942{"id":"stack-839975","source":"stackoverflow","questionId":839975,"title":"How does c++ by-ref argument passing is compiled in assembly?","tags":["c++","assembly","compiler-construction"],"text":"Title: How does c++ by-ref argument passing is compiled in assembly?\nTags: c++, assembly, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn the late years of college, I had a course on Compilers. We created a compiler for a subset of C. I have always wondered how a pass-by-ref function call is compiled into assembly in C++.\n\nFor what I remember, a pass-by-val function call follows the following procedure:\n\n- Store the address of the PP\n\n- Push the arguments onto the stack\n\n- Perform the function call\n\n- In the function, pop from stack the parameters\n\nWhat's different for pass-by-reference? (int void(int&);)\n\n**EDIT:**\n\nI may sound totally lost but, if you could help me I'd really appreciate it.\n\nEveryone's answer is basically that it passes the address instead of the value. I understood that to be basically what passing a pointer is. So how come, these two functions, behave differently?:\n\n```\nstruct A {\n int x;\n A(int v){\n x = v;\n }\n};\n\nint byRef(A& v){\n v = A(3);\n return 0;\n}\n\nint byP (A* v){\n v = &A(4); //OR new A(4)\n return 0;\n}\n\nint _tmain(int argc, _TCHAR* argv[])\n{\n A a (1); A b (2);\n byRef(a); byP (&b);\n cout I know that in byP(A*), v is being passed by value, thus, it won't affect the caller's argument. Then, how would you implement byRef(A&) in terms of A*?\n\n========================================\n\nTop Answer:\nYou pass a pointer to the referand, exactly as you would any other pointer, and the callee knows how to use it. Depending on the implementation it therefore might not be on the stack - some parameters are passed in registers in some calling conventions.\n\nThere may be other ways to do it, since the C++ standard doesn't specify how references are implemented afaik, and even if they are implemented as pointers I suppose they might be distinguished in the calling convention. Pointers are the most obvious implementation, though.\n\n========================================\n\nCode:\n```text\nstruct A {\n    int x;\n    A(int v){\n        x = v;\n    }\n};\n\nint byRef(A& v){\n    v = A(3);\n    return 0;\n}\n\nint byP   (A* v){\n    v = &A(4); //OR new A(4)\n    return 0;\n}\n\nint _tmain(int argc, _TCHAR* argv[])\n{\n    A a (1); A b (2);\n    byRef(a); byP  (&b);\n    cout << a.x << \" \" << b.x;\n\n    system(\"pause\");\n\n    return 0;\n}\n```\n\n```text\nint byRef(A& v){\n  v = A(3);\n  return 0;\n}\n```\n\n```text\nint byP   (A* v){\n  v = &A(4); //OR new A(4)\n  return 0;\n}\n```\n\n```text\nstruct A {\n  int x;\n  A(int v){\n    x = v;\n  }\n  A &operator = (A &rhs){\n    cout << \"assignment!\";\n  }\n};\n```\n\n```text\nint byRefUsingP (A *v)\n{\n  *v = A(3);\n  // or you could do:\n  // v->operator = (A(3));\n  // if an operator = is defined (don't know if it will work without one defined)\n  return 0;\n}\n```\n\n```text\nbyRef\n```\n\n```text\nbyP\n```\n\n```text\n&\n```\n\n```text\nbyRef\n```\n\n```text\nint byP (A* v) {\n    * v = A(4);    // modify thing referenced by the pointer\n    return 0;\n}\n```\n\n========================================\n\nComments:\n- Pushing all parameers onto the stack is the C ABI. C++ can use alternative ABI's where parameters are passed in registers.\n- Re: your edit, those two functions WILL behave the same when you change the first line of byP to \"*v = A(3);\"\n- Thank you, guys! It was really easy at the end. I think I wanted to complicate it :)\n- It's easy enough to look at the asm compilers actually make, especially for your tiny test functions with optimization enabled. godbolt.org and How to remove \"noise\" from GCC/clang assembly output?\n- @PeterCordes that is absolutely right. At the time I had very little knowledge of tools like that or even, that I can setup my compiler to do it. Thanks","metadata":{"transformedAt":"2026-08-18T18:32:17.843Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":157,"estimatedTokens":906}}943{"id":"stack-5277166","source":"stackoverflow","questionId":5277166,"title":"Loading a InMemory compiled assembly into current domain","tags":["c#","compiler-construction","assemblies","assembly-loading"],"text":"Title: Loading a InMemory compiled assembly into current domain\nTags: c#, compiler-construction, assemblies, assembly-loading\nSource: Stack Overflow\n\nQuestion:\nI'm using `CSharpCodeProvider` to compile an assembly and I have the `CompileParameters` `GenerateInMemory` property set to `true` because I don't want to create a physical file.\n\nAfter the compile I can take the `CompilerResults` and do something like this:-\n\n```\nobject x = cr.CompiledAssembly.CreateInstance(\"MyGeneratedClass\");\n Console.WriteLine(x);\n```\n\nI get the expected output, the `CreateInstance` has worked.\n\nHowever I need to be able to access types in the current `AppDomain` without such knowledge of the assembly. I need to do something like this:-\n\n```\nType t = Type.GetType(\"MyGeneratedClass\");\n object x = Activator.CreateInstance(t);\n```\n\nThe problem is in this code `t` ends up being null. Now I suspect that although the assembly is compiled it isn't loaded. I can't seem to find away to load this assembly into the domain so that its type names can be resolved. \n\nCan anyone enlighten me?\n\n========================================\n\nTop Answer:\nThe GenerateInMemory property is a parlor trick. The C# compiler doesn't know how to write to your program's memory. It is faked, the compiler is asked to actually write the assembly to the TEMP directory. From where it is loaded into memory after compilation succeeds with a FileStream into a byte[], then Assembly.Load(byte[]). Have a look-see with Reflector, Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch() method.\n\nSince it creates a file anyway, just not very visible, solve your problem by just letting it create a file and loading it in the AppDomain.\n\n========================================\n\nCode:\n```text\nobject x = cr.CompiledAssembly.CreateInstance(\"MyGeneratedClass\");\n Console.WriteLine(x);\n```\n\n```text\nType t = Type.GetType(\"MyGeneratedClass\");\n object x = Activator.CreateInstance(t);\n```\n\n```text\nCSharpCodeProvider\n```\n\n```text\nCompileParameters\n```\n\n```text\nGenerateInMemory\n```\n\n```text\ntrue\n```\n\n```text\nCompilerResults\n```\n\n```text\nCreateInstance\n```\n\n```text\nAppDomain\n```\n\n```text\nt\n```\n\n========================================\n\nComments:\n- Thanks, I've got rid of GenerateInMemory but I'm now stuck on how to load the assembly, I'm using `Assembly.LoadFile` on `cr.PathToAssembly`. However my call to `GetType` still returns null.\n- Avoid Assembly.LoadFile() like the plague, always use LoadFrom(). Note that you have the assembly now, you could open it in Reflector to have a look-see. Otherwise hard to guess why GetType doesn't work. Maybe you forgot the namespace or didn't make it public? Also see what you get out of GetTypes(). And don't forget that you did this to get it loaded in another AppDomain.\n- Cancel the AppDomain remark, I was confuzzled about the question. After you got it working you can set GenerateInMemory back to true.\n- Selecting this as the answer since it lead me to the correct solution. The name in GetType needs to be quailifed with the assembly name, which can be defined in the CompileParameters.\n- @AnthonyWJones: Cool! Glad I could be of help. :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.843Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":87,"estimatedTokens":786}}944{"id":"stack-1620930","source":"stackoverflow","questionId":1620930,"title":"Elimination left recursion for E := EE+|EE-|id","tags":["parsing","compiler-construction","grammar","context-free-grammar","lexical-analysis"],"text":"Title: Elimination left recursion for E := EE+|EE-|id\nTags: parsing, compiler-construction, grammar, context-free-grammar, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nHow to eliminate left recursion for the following grammar?\n\n```\nE := EE+|EE-|id\n```\n\nUsing the common procedure:\n\n```\nA := Aa|b\n```\n\ntranslates to:\n\n```\nA := b|A'\nA' := ϵ| Aa\n```\n\nApplying this to the original grammar we get:\n\n```\nA = E, a = (E+|E-) and b = id\n```\n\nTherefore:\n\n```\nE := id|E'\nE' := ϵ|E(E+|E-)\n```\n\nBut this grammar seems incorrect because\n\n```\nϵE+ -> ϵ id +\n```\n\nwould be valid\nbut that is an incorrect postfix expression.\n\n========================================\n\nCode:\n```text\nE := EE+|EE-|id\n```\n\n```text\nA := Aa|b\n```\n\n```text\nA := b|A'\nA' := ϵ| Aa\n```\n\n```text\nA = E, a = (E+|E-) and b = id\n```\n\n```text\nE := id|E'\nE' := ϵ|E(E+|E-)\n```\n\n```text\nϵE+ -> ϵ id +\n```\n\n```text\nA := Aα | β\n```\n\n```text\nA  := βA′\nA′ := αA′ | ϵ\n```\n\n```text\nE  := id E′\nE′ := (E + | E -) E′ | ϵ\n```\n\n========================================\n\nComments:\n- You should perhaps mention that `e` is really `ϵ`. Fooled me, at any rate.\n- You've got a problem in your \"translates to\" definition: you've introduced an undefined term 'e'. You can probably do something with regrouping the original as 'E := (EE(+|-))|id'. Your final comment 'that is an incorrect postfix expression' is somewhat sweeping; why is 'e id +' incorrect? It looks like 'push e; push id; evaluate +' which is usually OK.\n- @Konrad: ah - 'e' is empty?...that makes a difference.\n- Wasn't sure how to input epsilon. :)\n- @Absolute0: no problem - as long as you explain the notation you have used.\n- Shouldn't such questions be migrated to Theoretical Computer Science?\n- How do you input math symbols??\n- Absolute0: simple trick: I use a character table. I’m on OS X which has got a tool for that. On Windows, you can use `charmap.exe` which is hidden in the “Accessories” main menu (must be switched to Unicode, though).","metadata":{"transformedAt":"2026-08-18T18:32:17.843Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":9,"totalLines":100,"estimatedTokens":490}}945{"id":"stack-1962315","source":"stackoverflow","questionId":1962315,"title":"Abstract syntax tree question","tags":["c","compiler-construction","abstract-syntax-tree"],"text":"Title: Abstract syntax tree question\nTags: c, compiler-construction, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI am currently working on a compiler under C and I am abit lost at the part where we construct the data structure for AST, especially for the part where we construct stucture for IDs, it is called \"Symbol Table Entry\"\n\nI see structures over net such as:\n\n```\nstruct ste {\n struct id *name; /* pointer into hash table for assoc. id */\n struct decl *decl; /* pointer into symbol table for its decl */\n struct ste *prev; /* pointer to previous entry in symbol table */\n};\n```\n\nIt looks like a linked list as it holds a pointer to the previous entry (*prev) but what is the logic behind this ?\n\n========================================\n\nTop Answer:\nYou're seeing the leftovers of a pernicious habit from C programmers long ago: it is assumed that symbols will be on some lists, and instead of allocating list structures separately, the list pointers are included as part of the symbol structure. This trick saves one allocation per list element, but at a cost: the set of lists that a symbol can be on is fixed, and this structure confuses programmers. If the application is compilers, there is *no* reason to use this trick any more. It is much clearer to have a separate list structure that is defined something like this:\n\n```\nstruct ste_list {\n struct ste *symbol_table_entry;\n struct str_list *next;\n};\n```\n\nYou can have as many of these as you like, and nobody is the wiser. And the internal pointers you find confusing go away.\n\nYou ask\n\n what is the logic behind this?\n\nPart of the answer is simply that it's useful to have symbols on a distinguished list. I can't answer the question definitively without knowing more about the particular compiler. My best guess is that the `prev` entry is going to be used to implement nested scopes (the `{ ... }` brackets in C), but that's a guess based on compilers I've seen or worked on. So perhaps the logic is that when a closing brace is encountered, the compiler might that link until it gets to an `ste` in an enclosing scope. People with a bit more experience than the author of the compiler you're studying will generally put this logic in a \"symbol-table abstraction\" which will include functions like `enterscope()` and `exitscope()`, and the details of these operations will be hidden from the internal representation of individual symbol-table entries.\n\n========================================\n\nCode:\n```text\nstruct ste {\n  struct id   *name;  /* pointer into hash table for assoc. id */\n  struct decl *decl;  /* pointer into symbol table for its decl */\n  struct ste  *prev;  /* pointer to previous entry in symbol table */\n};\n```\n\n```text\n{\nint x;\n  {\n   int x;\n  }\n}\n```\n\n```text\nint main (void) {\n    int x = 1;\n    int y = 1;\n    if (x == 1) {\n        int y = 2;\n        printf (\"y = %d\\n\", y);\n    }\n    return 0;\n}\n```\n\n```text\nprev\n```\n\n```text\nnext\n```\n\n```text\nstruct ste_list {\n    struct ste *symbol_table_entry;\n    struct str_list *next;\n};\n```\n\n```text\nprev\n```\n\n```text\n{ ... }\n```\n\n```text\nste\n```\n\n```text\nenterscope()\n```\n\n```text\nexitscope()\n```\n\n========================================\n\nComments:\n- What is the context of this definition? It would be helpful to see a little more context to see how this is being used.\n- If you want to understand the reason for a linked list, read Knuth?\n- List of compiler related resources: stackoverflow.com/questions/1669/learning-to-write-a-compile&zwnj;&#8203;r\n- Is the problem here that you don't understand what the symbol table is for, or that you don't understand why it is constructed in this particular way? If the later are you confused because it seems too complicated, or because it seems insufficiently powerful?","metadata":{"transformedAt":"2026-08-18T18:32:17.843Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":113,"estimatedTokens":942}}946{"id":"stack-881709","source":"stackoverflow","questionId":881709,"title":"Difference between a bytecode parsed instruction and machine language?","tags":["compiler-construction","programming-languages","interpreter","bytecode"],"text":"Title: Difference between a bytecode parsed instruction and machine language?\nTags: compiler-construction, programming-languages, interpreter, bytecode\nSource: Stack Overflow\n\nQuestion:\n\"A bytecode program is normally executed by parsing the instructions one at a time. This kind of bytecode interpreter is very portable. Some systems, called dynamic translators, or \"just-in-time\" (JIT) compilers, translate bytecode into machine language as necessary at runtime: this makes the virtual machine unportable.\"\n\nA question about this paragraph is that: After the bytecode gets processed,\nwhat's the difference between a parsed instruction and machine language (or machine code)?\n\n========================================\n\nTop Answer:\nIn a bytecode interpreter, the instruction format is usually designed for very fast \"parsing\" using shift and mask operators. The interpreter, after \"parsing\" (I prefer \"decoding\") the instruction, immediately updates the state of the virtual machine and then begins decoding the next instruction. So after the bytecode gets processed in an *interpreter*, no remnant remains.\n\nIn a JIT compiler, bytes are processed in units larger than a single instruction. The minimum unit is the basic block, but modern JITs will convert larger paths to machine code. This is a *translation* step, and the output of the translation step *is* machine code. The original bytecode may remain in memory, but it is not used for implementation&mdash;so there is no real difference. (Although it is still typical that the machine code for a JITted virtual machine does different things from the machine code emitted by a native-code compiler.)\n\n========================================\n\nCode:\n```text\nint sum() {\n   return 5 + 6;\n}\n```\n\n```text\nfor(;;) {\n   switch(*currentInstruction++) {\n   case OP_PUSHINT:\n      *stack++ = nextInt(currentInstruction);\n      break;\n   case OP_ADD:\n      --stack;\n      stack[-1].add(*stack);\n      break;\n   case OP_RETURN:\n      return stack[-1];\n   }\n}\n```\n\n```text\nOP_PUSHINT (5)\nOP_PUSHINT (6)\nOP_ADD\nOP_RETURN\n```\n\n========================================\n\nComments:\n- it really depends on what you mean by \"parsed instruction\" - are you referring to the representation in the abstract syntax tree?","metadata":{"transformedAt":"2026-08-18T18:32:17.843Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":53,"estimatedTokens":563}}947{"id":"stack-13995659","source":"stackoverflow","questionId":13995659,"title":"Can modern compilers optimize constant expressions where the expression is derived from a function?","tags":["c++","performance","compiler-construction","compiler-optimization"],"text":"Title: Can modern compilers optimize constant expressions where the expression is derived from a function?\nTags: c++, performance, compiler-construction, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nIt is my understanding that modern c++ compilers take shortcuts on things like:\n\n```\nif(true)\n{do stuff}\n```\n\nBut how about something like:\n\n```\nbool foo(){return true}\n...\nif(foo())\n{do stuff}\n\nOr:\n\nclass Functor\n{\n\n public:\n bool operator() () { return true;}\n\n}\n\n...\n\nFunctor f;\n\nif(f()){do stuff}\n```\n\n========================================\n\nTop Answer:\nI've just tried `g++ 4.7.2` with `-O3`, and in both examples it optimizes out the call. Without `-O`, it doesn't.\n\n========================================\n\nCode:\n```text\nif(true)\n{do stuff}\n```\n\n```text\nbool foo(){return true}\n...\nif(foo())\n{do stuff}\n\nOr:\n\nclass Functor\n{\n\n public:\n        bool operator() () { return true;}\n\n}\n\n...\n\nFunctor f;\n\nif(f()){do stuff}\n```\n\n```text\nfoo()\n```\n\n```text\nfoo()\n```\n\n```text\nfoo()\n```\n\n```text\nif (true)\n```\n\n```text\nfoo()\n```\n\n```text\ng++ 4.7.2\n```\n\n```text\n-O3\n```\n\n```text\n-O\n```\n\n========================================\n\nComments:\n- Modern optimizers optimize constructs that are *a lot* more difficult than that...\n- This falls under branch prediction.\n- \"..like this\" is often a very poor title.\n- @ahenderson, no this is a step beyond branch prediction. This is the compiler eliminating the branch entirely. Probably more obvious when the condition is false, as the enclosed code will be omitted entirely.\n- @MarkRansom That's really interesting.","metadata":{"transformedAt":"2026-08-18T18:32:17.843Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":113,"estimatedTokens":392}}948{"id":"stack-28399245","source":"stackoverflow","questionId":28399245,"title":"C creating a new type of int","tags":["c","gcc","compiler-construction"],"text":"Title: C creating a new type of int\nTags: c, gcc, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI know that the compiler supports some types like `__int128_t` on 64 bit architectures. It basically stores a number in 2 registers. Now, is there any way to create something like `__int256_t`, on a 64 bit machine?\n\nBasically, I'm wondering if there is a way to extend the compiler to support a data type that stores a number on 4 registers, for example, and overload somehow the + operator to add them properly.\n\n========================================\n\nTop Answer:\nNot in C. Numeric data types are implemented in the compiler, and cannot be extended by applications.\n\nThis would be doable in C++, although the object would typically end up stored on the stack, not in registers.\n\n========================================\n\nCode:\n```text\n__int128_t\n```\n\n```text\n__int256_t\n```\n\n```text\ngcc/wide-int.h\n```\n\n```text\ngcc/config/i386/\n```\n\n```text\n*.md\n```\n\n```text\nstruct myint256_t\n```\n\n```text\nint256_t\n```\n\n```text\nstruct myint256_t\n```\n\n```text\noperator +\n```\n\n```text\na + b\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\nclass\n```\n\n========================================\n\nComments:\n- Why exactly do you ask? Why can't you use GMPlib? For what processor, what compiler (I guess GCC, since you tagged it), what operating system? For what source code? Please edit your question to improve it!\n- The question is meant just to improve my knowledge/understanding of the language and the compiler, without any practical result (it will actually be impractical to have a primitive data type stored on 4 registers or more). So i'm asking more from a theoretical point of view. Also, this can easily be achieved using a structure with 4 ints and functions to add/multiply. But again, the question is about making my new type to behave like a built-in one.\n- Are you prepared to spend weeks or months of work on that?\n- @BasileStarynkevitch: I'm prepared to spend a few days to understand how it might be done.\n- A few days won't be enough to understand....\n- Another way would be to use `__int128_t [2]` or a 2 members struct but of course all the arithmetic operators would have to be re-implemented as functions or macros.\n- And one could help the optimization of these macros with some ad-hoc optimization pass coded in MELT...","metadata":{"transformedAt":"2026-08-18T18:32:17.843Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":81,"estimatedTokens":582}}949{"id":"stack-3019729","source":"stackoverflow","questionId":3019729,"title":"How to access comments from the java compiler tree api generated ast?","tags":["java","compiler-construction","abstract-syntax-tree"],"text":"Title: How to access comments from the java compiler tree api generated ast?\nTags: java, compiler-construction, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI've used the java compiler tree api to generate the ast for java source files. However, i'm unable to access th comments in the source files. \n\nSo far, i've been unable to find a way to extract comments from source file .. is there a way using the compiler api or some other tool ?\n\n========================================\n\nTop Answer:\nOur SD Java Front End is a Java parser that builds ASTs (and optionally symbol tables). It captures comments directly on tree nodes. \n\nThe Java Front End is a member of a family of compiler langauge front ends (C, C++, C#, COBOL, JavaScript, ...) all of which are supported by DMS Software Reengineering Toolkit. DMS is designed to process languages for the purposes of transformation, and thus can capture comments, layout and formats to enable regeneration of code preserving the original layout as much as possible.\n\nEDIT 3/29/2012: (in contrast to answer posted for doing this with ANTLR)\n\nTo get a comment on an AST node in DMS, one calls the DMS (lisp-like) function \n\n```\n(AST:GetComments )\n```\n\nwhich provide access to the array of comments associated with the AST node. One can inquire about the length of this array (may be null), or for each array element, ask for any of these properties: (AST:Get... FileIndex, Line, Column, EndLine, EndColumn, String (exact Unicode comment content).\n\n========================================\n\nCode:\n```text\njavac\n```\n\n```text\n(AST:GetComments <node>)\n```\n\n```text\nfor (Comment comment : (List<Comment>) compilationUnit.getCommentList()) {\n\n    comment.accept(new CommentVisitor(compilationUnit, classSource.split(\"\\n\")));\n}\n```\n\n```text\nimport org.eclipse.jdt.core.dom.ASTNode;\nimport org.eclipse.jdt.core.dom.ASTVisitor;\nimport org.eclipse.jdt.core.dom.BlockComment;\nimport org.eclipse.jdt.core.dom.CompilationUnit;\nimport org.eclipse.jdt.core.dom.LineComment;\n\npublic class CommentVisitor extends ASTVisitor {\n\n    CompilationUnit compilationUnit;\n\n    private String[] source;\n\n    public CommentVisitor(CompilationUnit compilationUnit, String[] source) {\n\n        super();\n        this.compilationUnit = compilationUnit;\n        this.source = source;\n    }\n\n    public boolean visit(LineComment node) {\n\n        int startLineNumber = compilationUnit.getLineNumber(node.getStartPosition()) - 1;\n        String lineComment = source[startLineNumber].trim();\n\n        System.out.println(lineComment);\n\n        return true;\n    }\n\n    public boolean visit(BlockComment node) {\n\n        int startLineNumber = compilationUnit.getLineNumber(node.getStartPosition()) - 1;\n        int endLineNumber = compilationUnit.getLineNumber(node.getStartPosition() + node.getLength()) - 1;\n\n        StringBuffer blockComment = new StringBuffer();\n\n        for (int lineCount = startLineNumber ; lineCount<= endLineNumber; lineCount++) {\n\n            String blockCommentLine = source[lineCount].trim();\n            blockComment.append(blockCommentLine);\n            if (lineCount != endLineNumber) {\n                blockComment.append(\"\\n\");\n            }\n        }\n\n        System.out.println(blockComment.toString());\n\n        return true;\n    }\n\n    public void preVisit(ASTNode node) {\n\n    }\n}\n```\n\n```text\ngetCommentList\n```\n\n```text\naccept\n```\n\n========================================\n\nComments:\n- As far as i know Netbeans uses java tree api for syntax highlighting, code completion, checking etc.\n- Check out its formatter. That's going to use a tree parser to correctly handle the code, but it also formats comments. Also, ANTLR has special support for comment nodes, so I'm pretty sure its Java grammar will handle this case.\n- @Code freak - If this is helpful, you should actually give Ira Baxter an upvote, not just comment \"+1\".\n- This is really the answer? So, for each comment, you blast the source text into N lines (I suppose you could do this just once for a set) and pick out the Nth? Does that really get the comment, or just the raw line containing the comment? What happens if there are 2 comments in the same line?\n- You are right, the code is giving you the line which contains the comment. But you can still implement the logic to extract the comment / comments out of that raw line. Having something, is better than nothing. Also to avoid splitting the source everytime, source can be splitted once and the resultant array can be passed to the Visitor.\n- An arcane example: /* c1 * / foo /* c2 */ bar // c3 ... so if I have the tree for bar, how do I know which of these comments apply?\n- I agree this process is not effective when we have both comments and code on the same line. May be we can do another string mainpulation searching for '//' and '/*' instances in that line, but that now seems highly buggy.\n- Our experience is BIBSEH: \"Because In Big Systems, Everything Happens\". Agreed this arcane example is likely rare. But, BIBSEH, so it will occur, usually at the most inconvenient momement.","metadata":{"transformedAt":"2026-08-18T18:32:17.843Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":120,"estimatedTokens":1267}}950{"id":"stack-1048400","source":"stackoverflow","questionId":1048400,"title":"Unable to understand a statement about compilers' optimization","tags":["compiler-construction","runtime","compile-time","vm-implementation"],"text":"Title: Unable to understand a statement about compilers' optimization\nTags: compiler-construction, runtime, compile-time, vm-implementation\nSource: Stack Overflow\n\nQuestion:\nI am interested in optimization at runtime by a VM and at compile-time. I have had the idea that optimizations are most efficient and easiest at compile-time.\n\nHowever, my thought seems to be false in certain circumstances. This is evident in Steve Yeggie's statement quoted by Daniel\n\n [O]ptimization is often easier when performed at runtime by a clever\n virtual machine - -.\n\n**Why is the optimization easier when performed at runtime by a VM than at compile-time?**\n\n========================================\n\nTop Answer:\nBecause there's more information available at runtime. For instance exact CPU, operating system and other environment details are known. This information has an effect on how the optimization should be done.\n\n========================================\n\nComments:\n- I think Steve Yeggie meant \"easier to get better results\" rather than \"easier to do\", which is how I think you're interpreting it. After all, I imagine rearranging a program is harder to do whilst it's running than at compile time.\n- \"You can do every optimization at runtime that is possible at compile-time\" - I don't believe it. A profile-guided optimizer can rearrange the order of data members in an object, e.g. to improve locality of reference or to move the most-used object to offset 0. That's nigh impossible if you already have actual objects in memory.\n- Modern garbage-collectors rearrange the objects in the memory all the time to reduce fragmentation. To do this, all references/pointers have to be rewritten (or some clever indirection is used). Rearranging data-members doesn't seem so complicated in this context. But I have to admit, that I don't know an example of a VM that does it actually.\n- It's actually very easy, particularly since a VM that uses mark-sweep garbage collection will traverse the entire object tree on a regular basis. And again, runtime wins, because you see what fields really are accessed together, on a regular basis, versus in one method that's rarely called. (note: I have no idea whether the .Net or Java VMs actually do this, and personally doubt that they do)\n- @Mnementh: comment about the last sentence: Do you mean that optimization at run-time is more complicated than at compile-time and faults are more relevant at run-time than at compile-time.\n- @Masi: Yes, I meant it this way. I edited the sentence and added it explicitly for clarity.\n- I would like to get my hands dirty. --- Could someone give me an example or a homework about how I can see practically what Mnementh means with the last statement.\n- @Masi: Faults are more relevant, because a fault on a compiler doesn't necessarily breaks the compiled program. A fault on a VM is a fault of the program executed on the VM.\n- I mean to get an concrete example about a fault in some compiler, and to get another example of a fault in a VM.\n- Yup - compilers in general can't take advantage of SSE3, for instance.","metadata":{"transformedAt":"2026-08-18T18:32:17.843Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":32,"estimatedTokens":772}}951{"id":"stack-5207284","source":"stackoverflow","questionId":5207284,"title":"Isn't inline premature optimization?","tags":["c++","optimization","compiler-construction","inline"],"text":"Title: Isn't inline premature optimization?\nTags: c++, optimization, compiler-construction, inline\nSource: Stack Overflow\n\nQuestion:\n**Possible Duplicate:**\n\n Inline functions in C++ \n\nModern compilers are better than programmers at deciding what should be inlined and what should not. Just like, `register`, shouldn't inlining functions be a job for the compiler only, and be considered premature optimization ?\n\n========================================\n\nTop Answer:\n`inline` is only tangentially related to optimization.\n\nYou should choose to apply `inline` to a function if you need the exceptions to the *one definition rule* that it gives you, and leave it out if you don't. Most of the time you can rely on the compiler to perform the appropriate optimizations independent of whether a function is declared `inline` or not.\n\n========================================\n\nCode:\n```text\nregister\n```\n\n```text\ninline\n```\n\n```text\ninline\n```\n\n```text\ninline\n```\n\n```text\ninline\n```\n\n```text\ninline\n```\n\n========================================\n\nComments:\n- ...but you can apply it into your code *after* the logic is finished. Then it isn't premature :P\n- as far as i know the inline keyword is only considered a hint to the compiler and the compiler may descide to ignore it anyway.\n- The `inline` keyword is not only for inlining functions. Actually, when you think about it, it is not for inlining functions at all! :-)\n- Possible duplicate: stackoverflow.com/questions/3540931/inline-functions-in-c","metadata":{"transformedAt":"2026-08-18T18:32:17.843Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":52,"estimatedTokens":375}}952{"id":"stack-24505543","source":"stackoverflow","questionId":24505543,"title":"What are some common strategies different compilers use to deal with overflow in numeric conversions?","tags":["c++","compiler-construction","type-conversion","integer-overflow","floating-point-conversion"],"text":"Title: What are some common strategies different compilers use to deal with overflow in numeric conversions?\nTags: c++, compiler-construction, type-conversion, integer-overflow, floating-point-conversion\nSource: Stack Overflow\n\nQuestion:\nI understand that, in C++, when I convert a `float`/`double` into an `int`, whereby the floating-point number is beyond the range that the `int` can hold, the result is not defined as part of the C++ language. The result depends on the implementation/compiler. **What are some strategies common compilers use to deal with this?**\n\nConverting `7.2E12` to an `int` can yield the values `1634811904` or `2147483647`. For example, does anyone know what the compiler is doing in each of these cases?\n\n========================================\n\nCode:\n```text\nfloat\n```\n\n```text\ndouble\n```\n\n```text\nint\n```\n\n```text\nint\n```\n\n```text\n7.2E12\n```\n\n```text\nint\n```\n\n```text\n1634811904\n```\n\n```text\n2147483647\n```\n\n```text\nint printf(const char *, ...);\n\nvolatile double v = 0;\n\nint main()\n{\n  int i1 = 2147483648.0;\n  int i2 = 2147483648.0 + v;\n  printf(\"%d %d\\n\", i1, i2);\n}\n```\n\n```text\n$ cat t.c\n#include <stdio.h>\n#include <stdlib.h>\nint main(int c, char **v)\n{\n  unsigned int i = 4294967296.0 + strtod(v[1], 0);\n  printf(\"%u\\n\", i);\n}\n$ gcc -m64 -S -std=c99 -O t.c && cat t.s\n…\naddsd LCPI1_0(%rip), %xmm0 ; this is the + from the C program\ncvttsd2siq %xmm0, %rsi ; one-instruction conversion\n…\n```\n\n```text\n$ gcc -m32 -S -std=c99 -O t.c && cat t.s\n…\naddsd LCPI1_0-L1$pb(%esi), %xmm0 ; this is the + from the C program\nmovsd LCPI1_1-L1$pb(%esi), %xmm1 ; conversion to unsigned int starts here\nmovapd %xmm0, %xmm2\nsubsd %xmm1, %xmm2\ncvttsd2si %xmm2, %eax\nxorl $-2147483648, %eax\nucomisd %xmm1, %xmm0\ncvttsd2si %xmm0, %edx\ncmovael %eax, %edx\n…\n```\n\n```text\nif (d < 231)\nthen (unsigned int)(int)d\nelse (231 + (unsigned int)(int)(d - 231))\n```\n\n```text\n$ gcc -m32 -std=c99 -O t.c && ./a.out 123456\n0\n```\n\n```text\ni1\n```\n\n```text\ni2\n```\n\n```text\ni1\n```\n\n```text\ni2\n```\n\n```text\ndouble\n```\n\n```text\nunsigned int\n```\n\n```text\ncvttsd2siq\n```\n\n```text\ndouble\n```\n\n```text\ndouble\n```\n\n```text\nunsigned int\n```\n\n```text\nunsigned int\n```\n\n```text\ncvttsd2si\n```\n\n```text\n%eax\n```\n\n```text\n%edx\n```\n\n```text\n%xmm0\n```\n\n```text\n%xmm1\n```\n\n========================================\n\nComments:\n- gcc's implementation.\n- Note I am not asking what strategies the programmer should use to avoid/deal with this overflow, I'm asking what different strategies a compiler may employ to deal with it\n- @user657267 Your link does not describe the behavior of GCC-generated programs for the overflow in the conversion from floating-point to integer. In fact, that links describes implementation-defined behaviors in GCC, whereas this overflow is undefined behavior (you cannot expect a compiler to document what it does for it).\n- Thank you for you answer Pascal! I'm still having trouble bending to the fact that there are undefined behaviour in C/C++. There's still a bit of confusion and I want to clarify one concept: If the compiler doesn't define the instruction for a specific type of conversion that leads to overflow, it may a) find an alternative set of instructions where the result doesn't overflow and then truncates it (like only the 'bottom-32 bit' of a 64-bit integer will be used), or b) ignore it completely, where it is up to the assembly instruction(s) what results it comes back with. Am I close?\n- @dayuloli I think you have it. One single assembly instruction usually has a defined behavior (assembly is more thoroughly defined than C or C++). If the compiler always generated this single assembly instruction, you could expect the behavior of the underlying instruction to apply to the source-level overflow. Unfortunately, the translation of source code to assembly is more complex than this. “Constant folding” optimization may mean that the conversion is done at compile-time, and in this case the compiler does not need to match the run-time behavior. And sometimes a single instruction does…\n- @dayuloli …not exist for the conversion, in which case the compiler produces a sequence of instruction that does not have to have a clean, easily described behavior for overflows.\n- I had to read through your comments a few times, but it clicks! I actually understand everything you said in the comments now, and thanks for making the point about 'Constant folding', that is new to me! Thanks again!\n- +1 nice answer ... @dayuloli there is indeed undefined behavior, there is a whole tag dedicated to it.\n- Hi Shafik, thanks again for your answer to my previous question. But as a person who deals with PHP and Javascript/CSS, getting my head around undefined behaviour isn't as easy as I'd first hoped. But this clears things up even more now.\n- *The compiler does not “deal with” overflows so much as completely ignore them.* That's true for most implementations (all that I know, in fact), but because it is undefined behavior, a compiler could generated code to detect and handle them in some specific way.\n- Pascal compilers generally have options to generate code for such runtime checks, because such runtime checks are common on Pascal.","metadata":{"transformedAt":"2026-08-18T18:32:17.843Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":29,"totalLines":177,"estimatedTokens":1293}}953{"id":"stack-52044858","source":"stackoverflow","questionId":52044858,"title":"What is the most efficient way to create a lexer?","tags":["compiler-construction","lexer","finite-automata"],"text":"Title: What is the most efficient way to create a lexer?\nTags: compiler-construction, lexer, finite-automata\nSource: Stack Overflow\n\nQuestion:\nI am currently trying to learn how to create my own lexical analyser, by hand. I had been using Flex (along with Bison) a lot to practice and learn how it works internally, but I am currently seeing at least 3 different solutions to develop my own.\n\n- Using a list of REs, going through each and when one matches, simply return the associated token (see python docs about REs)\n\n- Creating a DFA from REs (as does Flex for example: based on REs, create a big state machine)\n\n- Creating my own 'state machine' with lots of switch cases or if statements (I think Lua does this for example)\n\nI am confident that I can try each solution, but:\n\n- Is there a case that one solution can't solve ?\n\n- In what case would you use one solution rather than another ?\n\n- And as the title says: which one produces the most efficient code ?\n\nThanks in advance!\n\n========================================\n\nCode:\n```text\ndo {         => First token is keyword do\ndouble d;    => First token is keyword double\ndoubt = 0.9; => First token is identifier doubt\n```\n\n```text\n\"do\"       {  return T_FOR; }\n\"double\"   {  return T_FOREACH; }\n[[:alpha:]_][[:alnum:]_]*  { yyval.str = strdup(yytext); return T_ID; }\n```\n\n```text\n>>> pat = re.compile(\"(wee|week)(night|knight)\")\n>>> pat.match(\"weeknight\").group(1)\n'wee'\n>>> pat.match(\"weekknight\").group(1)\n'week'\n```\n\n```text\ndo\n```\n\n```text\ndouble\n```\n\n```text\ndo\n```\n\n```text\nfor\n```\n\n```text\nwhile\n```\n\n```text\ndone\n```\n\n```text\ndouble\n```\n\n```text\nint\n```\n\n```text\nchar\n```\n\n```text\ndouble\n```\n\n```text\ndo\n```\n\n```text\n|\n```\n\n```text\nre2c\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.843Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":16,"totalLines":97,"estimatedTokens":428}}954{"id":"stack-71254325","source":"stackoverflow","questionId":71254325,"title":"Do Compilers Un-Inline?","tags":["c++","optimization","compiler-construction"],"text":"Title: Do Compilers Un-Inline?\nTags: c++, optimization, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIt is fairly common knowledge that the most powerful tool in a compilers tool-belt is the inlining of functions into their call sites. But what about doing the reverse? If so, is it done? And when? For example given:\n\n```\nvoid foo(int x)\n{\n auto y = bar(x);\n baz(y);\n}\n\nvoid bop()\n{\n int x;\n auto y = bar(x);\n baz(y);\n}\n```\n\nDoes it ever make sense for the compiler to abstract this out to\n\n```\nvoid qux(int x)\n{\n auto y = bar(x);\n baz(y);\n}\n\nvoid foo(int x)\n{\n qux(x);\n}\n\nvoid bop()\n{\n int x;\n qux(x);\n}\n```\n\n========================================\n\nTop Answer:\nOutlining makes sense even without repeated code, when the outlined section is `[[unlikely]]`. The function call is a loss, but unlikely, while on the other hand more likely code can fit in cache.\n\nCompilers might also assume that an exception is unlikely, and outline the `catch`.\n\n========================================\n\nCode:\n```text\nvoid foo(int x)\n{\n  auto y = bar(x);\n  baz(y);\n}\n\nvoid bop()\n{\n  int x;\n  auto y = bar(x);\n  baz(y);\n}\n```\n\n```text\nvoid qux(int x)\n{\n  auto y = bar(x);\n  baz(y);\n}\n\nvoid foo(int x)\n{\n  qux(x);\n}\n\nvoid bop()\n{\n  int x;\n  qux(x);\n}\n```\n\n```text\n[[unlikely]]\n```\n\n```text\ncatch\n```\n\n========================================\n\nComments:\n- Most compilers have an option to optimize to reduce binary size instead of optimizing for speed or memory usage. They could in theory apply this approach if it helps. Compilers can make any changes they want to your code that doesn't change the behavior of the program, and since the two examples shown have equivalent behavior there is nothing preventing a compiler from making that transformation. See The as-if rule.\n- I've never seen this done. It is probably allowed under the as-if rule, but adding a function call is most likely a performance loss, so an optimizer would not do that.\n- I can't see why they should. Optimizers are meant to reduce instruction count or to generate more efficient instructions. \"Outlining\" as in this example only adds additional instructions and an indirection which requires modifying the stack and a jump, for no reason. The only situation it could be helpful would be if the compiler recognized many separate code snippets in the program as being the same and put them into a function (and thus reducing code size) but that is very rare to ever happen and immensely complex.\n- There are compiler that perform this kind of outlining on the instruction level when optimizing for low code size.\n- The compiler is not required to inline anything - even the `inline` keyword is considered a hint that the compiler is permitted to ignore. I remember documentation for one older compiler that listed criteria for a function to be \"too complicated to inline\" (and one of those criteria was containing conditional statements), and another that treated inlining as an optimisation so would never inline when compiling without optimisation. There is nothing stopping modern compilers from doing similar things (although, presumably, the basis for \"too complicated to inline\" will have evolved).\n- > when the outlined section is [[unlikely]] This is the main motivation for my question actually. I have a C-style library returning error-codes where the error-checking must be abstracted to a macro not a function-call (which can be manually out-lined by the programmer). I'm hoping the compiler is smart enough to not generate the same instructions at each of the thousands of callsites...\n- @JacobFaib You can look at the assembly to know for sure. If it's *literally* the same instructions, they should all jump to a common block (but, probably not to a separate function). It's pretty normal to have a no-inline cold function for fallback rare error handling (i.e. `coldcc` in llvm), but you opt into that with human knowledge beyond the compiler.\n- Yeah, although it is kind of tough to look at the assembly for this :(. In the cases where I care about these operations being hoisted, compiler optimizations jumble things up to where I can't make heads or tails of things. Each macro uses __LINE__, __FILE__, and __func__ so not *identical* steps but more or less identical procedure. Hence why the compiler hoisting it into a function would be best.","metadata":{"transformedAt":"2026-08-18T18:32:17.843Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":106,"estimatedTokens":1084}}955{"id":"stack-26203619","source":"stackoverflow","questionId":26203619,"title":"How does a linker work exactly (microcontroller context)?","tags":["c","compiler-construction","linker","embedded","microcontroller"],"text":"Title: How does a linker work exactly (microcontroller context)?\nTags: c, compiler-construction, linker, embedded, microcontroller\nSource: Stack Overflow\n\nQuestion:\nI've been programming in C and C++ for quite a long time now, so I'm familiar with the linking process as a user: the preprocessor expands all prototypes and macros in each .c file which is then compiled separately into its own object file, and all object files together with static libraries are linked into an executable. \n\nHowever I'd like to know more about this process: how does the linker link the object files (what do they contain anyway?)? Matching declared but undefined functions with their definitions in other files (how?)? Translating into the exact content of the program memory (context: microcontrollers)?\n\n### Application example\n\nIdeally, I'm looking for a detailed step-by-step description of what the process is doing, based on the following simplistic example. Since it doesn't appear to be said anywhere, fame and glory to whoever answers in this way.\n\nmain.c\n\n```\n#include \"otherfile.h\"\n\nint main(void) {\n otherfile_print(\"Foo\");\n\n return 0;\n}\n```\n\notherfile.h\n\n```\nvoid otherfile_print(char const *);\n```\n\notherfile.c\n\n```\n#include \"otherfile.h\"\n#include \n\nvoid otherfile_print(char const *str) {\n printf(str);\n}\n```\n\n========================================\n\nCode:\n```text\n#include \"otherfile.h\"\n\nint main(void) {\n   otherfile_print(\"Foo\");\n\n   return 0;\n}\n```\n\n```text\nvoid otherfile_print(char const *);\n```\n\n```text\n#include \"otherfile.h\"\n#include <stdio.h>\n\nvoid otherfile_print(char const *str) {\n   printf(str);\n}\n```\n\n```text\nunsigned int glob;\nunsigned int two ( unsigned int a, unsigned int b )\n{\n    glob=5;\n    return(a+b+7);\n}\n```\n\n```text\nextern unsigned int glob;\nunsigned int two ( unsigned int, unsigned int );\nunsigned int one ( void )\n{\n    return(two(5,6)+glob);\n}\n```\n\n```text\n.globl _start\n_start:\n    bl one\n    b .\n```\n\n```text\n% arm-none-eabi-gcc -O2 -c one.c -o one.o\n% arm-none-eabi-gcc -O2 -c two.c -o two.o\n% touch start.s\n% arm-none-eabi-gcc -Wall -O2 -nostdlib -nostartfiles -ffreestanding -c one.c -o one.o\n% arm-none-eabi-gcc -Wall -O2 -nostdlib -nostartfiles -ffreestanding -c two.c -o two.o\n% arm-none-eabi-as start.s -o start.o\n% arm-none-eabi-ld -Ttext=0x10000000 start.o one.o two.o -o onetwo.elf\n```\n\n```text\narm-none-eabi-objdump -D start.o\n...\n00000000 <_start>:\n   0:   ebfffffe    bl  0 <one>\n   4:   eafffffe    b   4 <_start+0x4>\n```\n\n```text\n00000000 <one>:\n   0:   e92d4008    push    {r3, lr}\n   4:   e3a00005    mov r0, #5\n   8:   e3a01006    mov r1, #6\n   c:   ebfffffe    bl  0 <two>\n  10:   e59f300c    ldr r3, [pc, #12]   ; 24 <one+0x24>\n  14:   e5933000    ldr r3, [r3]\n  18:   e0800003    add r0, r0, r3\n  1c:   e8bd4008    pop {r3, lr}\n  20:   e12fff1e    bx  lr\n  24:   00000000    andeq   r0, r0, r0\n```\n\n```text\n00000000 <two>:\n   0:   e59f3010    ldr r3, [pc, #16]   ; 18 <two+0x18>\n   4:   e2811007    add r1, r1, #7\n   8:   e3a02005    mov r2, #5\n   c:   e0810000    add r0, r1, r0\n  10:   e5832000    str r2, [r3]\n  14:   e12fff1e    bx  lr\n  18:   00000000    andeq   r0, r0, r0\n```\n\n```text\n10000000 <_start>:\n10000000:   eb000000    bl  10000008 <one>\n10000004:   eafffffe    b   10000004 <_start+0x4>\n\n10000008 <one>:\n10000008:   e92d4008    push    {r3, lr}\n1000000c:   e3a00005    mov r0, #5\n10000010:   e3a01006    mov r1, #6\n10000014:   eb000005    bl  10000030 <two>\n10000018:   e59f300c    ldr r3, [pc, #12]   ; 1000002c <one+0x24>\n1000001c:   e5933000    ldr r3, [r3]\n10000020:   e0800003    add r0, r0, r3\n10000024:   e8bd4008    pop {r3, lr}\n10000028:   e12fff1e    bx  lr\n1000002c:   1000804c    andne   r8, r0, ip, asr #32\n\n10000030 <two>:\n10000030:   e59f3010    ldr r3, [pc, #16]   ; 10000048 <two+0x18>\n10000034:   e2811007    add r1, r1, #7\n10000038:   e3a02005    mov r2, #5\n1000003c:   e0810000    add r0, r1, r0\n10000040:   e5832000    str r2, [r3]\n10000044:   e12fff1e    bx  lr\n10000048:   1000804c    andne   r8, r0, ip, asr #32\n\nDisassembly of section .bss:\n\n1000804c <__bss_start>:\n1000804c:   00000000    andeq   r0, r0, r0\n```\n\n```text\nldr r1,locdat\n   nop\n   nop\n   nop\n   nop\n   nop\n   b over\nlocdat: .word 0x12345678\ntop:\n    nop\n    nop\n    nop\n    nop\n    nop\n    nop\nover:\n    b top\n```\n\n```text\n00000000 <locdat-0x1c>:\n   0:   e59f1014    ldr r1, [pc, #20]   ; 1c <locdat>\n   4:   e1a00000    nop         ; (mov r0, r0)\n   8:   e1a00000    nop         ; (mov r0, r0)\n   c:   e1a00000    nop         ; (mov r0, r0)\n  10:   e1a00000    nop         ; (mov r0, r0)\n  14:   e1a00000    nop         ; (mov r0, r0)\n  18:   ea000006    b   38 <over>\n\n0000001c <locdat>:\n  1c:   12345678    eorsne  r5, r4, #120, 12    ; 0x7800000\n\n00000020 <top>:\n  20:   e1a00000    nop         ; (mov r0, r0)\n  24:   e1a00000    nop         ; (mov r0, r0)\n  28:   e1a00000    nop         ; (mov r0, r0)\n  2c:   e1a00000    nop         ; (mov r0, r0)\n  30:   e1a00000    nop         ; (mov r0, r0)\n  34:   e1a00000    nop         ; (mov r0, r0)\n\n00000038 <over>:\n  38:   eafffff8    b   20 <top>\n```\n\n========================================\n\nComments:\n- This was the first thing I read, this is basically what I'm saying (more crudely I must admit) in my first paragraph. I'm looking for more details.\n- See: Assemblers and Loader, by David Solomon.\n- Thanks for the exhaustive answer, that's pretty much what I was expecting. I totally agree with not using printf(), this was only to have a function from external libraries as well. It was quite dense though, so to clarify: so .o files are really only assembly files with incomplete branch and variables addresses; I didn't quite get how the linker found the right address in the other .o files, is it related to \"symbols\"?\n- depends on the object file format and how the compiler/assembler and linker communicate that kind of info between them. libraries are pretty much the same except from my experience with a library it doesnt include the whole library but the linker extracts the functions it needs, so printf would result in a bunch of others. Also the library can also be such that instead of the code being static, all the code needed added, instead stubs that can find the shared library (.so, .dll, etc)\n- same deal though there has to be resolution, the printf function for example may at runtime try to find all the other libraries it needs rather than compile time (if linked with dynamic libraries), but that may vary with operating system or toolchain. There are no universal rules for how you have to solve the problem, within a compiler family or operating system that combination may have rules you have to or should conform to, but how linkers and compilers/assemblers communicate with each other has varied over time and among toolchains.","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":13,"totalLines":221,"estimatedTokens":1695}}956{"id":"stack-28035655","source":"stackoverflow","questionId":28035655,"title":"GameBoy compiler with system registers and interrupts","tags":["c","compiler-construction","z80","gameboy"],"text":"Title: GameBoy compiler with system registers and interrupts\nTags: c, compiler-construction, z80, gameboy\nSource: Stack Overflow\n\nQuestion:\nI have been spending a lot of time learning GameBoy programming, as I was already familiar with Z80 Assembly I wasn't afraid of jumping into using it. I would (of course) find it much more productive to program in C or C++ however cannot find a full compiler for the GameBoy, the compilers I can find manage everything themselves and do not give access to system registers to the programmer and also have some horrible drawbacks such as 100% CPU utilization and no interrupt support.\n\nWould it be possible to address system registers much like Arduino's AVR compiler? being able to address a CPU or system register simply with its name such as `DDRD = %10101011`\n\nWhat would I have to do to add interrupts and system registers into a compiler? All but one system register are only one byte memory addresses and interrupts vectors are of course memory locations, the only one system register that isn't a memory address can only be modified with two Assembly instructions `EI` and `DI` but that could be inline functions correct?\n\n========================================\n\nTop Answer:\nFirst off, you can use GBDK, which is a C compiler and library for the Gameboy. It does provide access to the registers in `gb/hardware.h` (but that isn't listed in the doc file, since there's no comment for each individual register). It also supplies access to interrupts via methods in `gb/gb.h`: `add_VBL`, `add_LCD`, `add_TIM`, `add_SIO`, and `add_JOY`. (There's also remove methods named `remove_`).\n\nFor reference and/or your own use, here's the contents of `gb/hardware.h`:\n\n```\n#define __REG volatile UINT8 *\n\n#define P1_REG (*(__REG)0xFF00) /* Joystick: 1.1.P15.P14.P13.P12.P11.P10 */\n#define SB_REG (*(__REG)0xFF01) /* Serial IO data buffer */\n#define SC_REG (*(__REG)0xFF02) /* Serial IO control register */\n#define DIV_REG (*(__REG)0xFF04) /* Divider register */\n#define TIMA_REG (*(__REG)0xFF05) /* Timer counter */\n#define TMA_REG (*(__REG)0xFF06) /* Timer modulo */\n#define TAC_REG (*(__REG)0xFF07) /* Timer control */\n#define IF_REG (*(__REG)0xFF0F) /* Interrupt flags: 0.0.0.JOY.SIO.TIM.LCD.VBL */\n#define NR10_REG (*(__REG)0xFF10) /* Sound register */\n#define NR11_REG (*(__REG)0xFF11) /* Sound register */\n#define NR12_REG (*(__REG)0xFF12) /* Sound register */\n#define NR13_REG (*(__REG)0xFF13) /* Sound register */\n#define NR14_REG (*(__REG)0xFF14) /* Sound register */\n#define NR21_REG (*(__REG)0xFF16) /* Sound register */\n#define NR22_REG (*(__REG)0xFF17) /* Sound register */\n#define NR23_REG (*(__REG)0xFF18) /* Sound register */\n#define NR24_REG (*(__REG)0xFF19) /* Sound register */\n#define NR30_REG (*(__REG)0xFF1A) /* Sound register */\n#define NR31_REG (*(__REG)0xFF1B) /* Sound register */\n#define NR32_REG (*(__REG)0xFF1C) /* Sound register */\n#define NR33_REG (*(__REG)0xFF1D) /* Sound register */\n#define NR34_REG (*(__REG)0xFF1E) /* Sound register */\n#define NR41_REG (*(__REG)0xFF20) /* Sound register */\n#define NR42_REG (*(__REG)0xFF21) /* Sound register */\n#define NR43_REG (*(__REG)0xFF22) /* Sound register */\n#define NR44_REG (*(__REG)0xFF23) /* Sound register */\n#define NR50_REG (*(__REG)0xFF24) /* Sound register */\n#define NR51_REG (*(__REG)0xFF25) /* Sound register */\n#define NR52_REG (*(__REG)0xFF26) /* Sound register */\n#define LCDC_REG (*(__REG)0xFF40) /* LCD control */\n#define STAT_REG (*(__REG)0xFF41) /* LCD status */\n#define SCY_REG (*(__REG)0xFF42) /* Scroll Y */\n#define SCX_REG (*(__REG)0xFF43) /* Scroll X */\n#define LY_REG (*(__REG)0xFF44) /* LCDC Y-coordinate */\n#define LYC_REG (*(__REG)0xFF45) /* LY compare */\n#define DMA_REG (*(__REG)0xFF46) /* DMA transfer */\n#define BGP_REG (*(__REG)0xFF47) /* BG palette data */\n#define OBP0_REG (*(__REG)0xFF48) /* OBJ palette 0 data */\n#define OBP1_REG (*(__REG)0xFF49) /* OBJ palette 1 data */\n#define WY_REG (*(__REG)0xFF4A) /* Window Y coordinate */\n#define WX_REG (*(__REG)0xFF4B) /* Window X coordinate */\n#define KEY1_REG (*(__REG)0xFF4D) /* CPU speed */\n#define VBK_REG (*(__REG)0xFF4F) /* VRAM bank */\n#define HDMA1_REG (*(__REG)0xFF51) /* DMA control 1 */\n#define HDMA2_REG (*(__REG)0xFF52) /* DMA control 2 */\n#define HDMA3_REG (*(__REG)0xFF53) /* DMA control 3 */\n#define HDMA4_REG (*(__REG)0xFF54) /* DMA control 4 */\n#define HDMA5_REG (*(__REG)0xFF55) /* DMA control 5 */\n#define RP_REG (*(__REG)0xFF56) /* IR port */\n#define BCPS_REG (*(__REG)0xFF68) /* BG color palette specification */\n#define BCPD_REG (*(__REG)0xFF69) /* BG color palette data */\n#define OCPS_REG (*(__REG)0xFF6A) /* OBJ color palette specification */\n#define OCPD_REG (*(__REG)0xFF6B) /* OBJ color palette data */\n#define SVBK_REG (*(__REG)0xFF70) /* WRAM bank */\n#define IE_REG (*(__REG)0xFFFF) /* Interrupt enable */\n```\n\nThese are done in the same way as George Phillips's answer, and thus can be used like normal variables.\n\nThe code that is used by GBDK to add and remove interrupts is found in `libc\\gb\\crt0.s`, but I don't know enough of assembly to include the relevant sections in this post. \n\nI'm not sure about how to avoid the busy loop either.\n\n========================================\n\nCode:\n```text\nDDRD = %10101011\n```\n\n```text\nEI\n```\n\n```text\nDI\n```\n\n```text\nvolatile unsigned char *reg_DDRD = (unsigned char *)0xE000;\n*reg_DDRD = 0xAB;\n```\n\n```text\n#define DDRD (*reg_DDRD)\nDDRD = 0xAB;\n```\n\n```text\norg 38h   ; or wherever the gameboy CPU jumps to on interrupt\n jp _intr_function\n```\n\n```text\nvoid intr_function()\n{\n     asm(\" push af\");\n     asm(\" push bc\");\n     asm(\" push de\");\n     asm(\" push hl\");\n\n     // ... now do what you like here.\n\n     asm(\" pop hl\");\n     asm(\" pop de\");\n     asm(\" pop bc\");\n     asm(\" pop af\");\n }\n```\n\n```text\nintr_function()\n```\n\n```text\norg\n```\n\n```c\n#define __REG   volatile UINT8 *\n\n#define P1_REG      (*(__REG)0xFF00)    /* Joystick: 1.1.P15.P14.P13.P12.P11.P10 */\n#define SB_REG      (*(__REG)0xFF01)    /* Serial IO data buffer */\n#define SC_REG      (*(__REG)0xFF02)    /* Serial IO control register */\n#define DIV_REG     (*(__REG)0xFF04)    /* Divider register */\n#define TIMA_REG    (*(__REG)0xFF05)    /* Timer counter */\n#define TMA_REG     (*(__REG)0xFF06)    /* Timer modulo */\n#define TAC_REG     (*(__REG)0xFF07)    /* Timer control */\n#define IF_REG      (*(__REG)0xFF0F)    /* Interrupt flags: 0.0.0.JOY.SIO.TIM.LCD.VBL */\n#define NR10_REG    (*(__REG)0xFF10)    /* Sound register */\n#define NR11_REG    (*(__REG)0xFF11)    /* Sound register */\n#define NR12_REG    (*(__REG)0xFF12)    /* Sound register */\n#define NR13_REG    (*(__REG)0xFF13)    /* Sound register */\n#define NR14_REG    (*(__REG)0xFF14)    /* Sound register */\n#define NR21_REG    (*(__REG)0xFF16)    /* Sound register */\n#define NR22_REG    (*(__REG)0xFF17)    /* Sound register */\n#define NR23_REG    (*(__REG)0xFF18)    /* Sound register */\n#define NR24_REG    (*(__REG)0xFF19)    /* Sound register */\n#define NR30_REG    (*(__REG)0xFF1A)    /* Sound register */\n#define NR31_REG    (*(__REG)0xFF1B)    /* Sound register */\n#define NR32_REG    (*(__REG)0xFF1C)    /* Sound register */\n#define NR33_REG    (*(__REG)0xFF1D)    /* Sound register */\n#define NR34_REG    (*(__REG)0xFF1E)    /* Sound register */\n#define NR41_REG    (*(__REG)0xFF20)    /* Sound register */\n#define NR42_REG    (*(__REG)0xFF21)    /* Sound register */\n#define NR43_REG    (*(__REG)0xFF22)    /* Sound register */\n#define NR44_REG    (*(__REG)0xFF23)    /* Sound register */\n#define NR50_REG    (*(__REG)0xFF24)    /* Sound register */\n#define NR51_REG    (*(__REG)0xFF25)    /* Sound register */\n#define NR52_REG    (*(__REG)0xFF26)    /* Sound register */\n#define LCDC_REG    (*(__REG)0xFF40)    /* LCD control */\n#define STAT_REG    (*(__REG)0xFF41)    /* LCD status */\n#define SCY_REG     (*(__REG)0xFF42)    /* Scroll Y */\n#define SCX_REG     (*(__REG)0xFF43)    /* Scroll X */\n#define LY_REG      (*(__REG)0xFF44)    /* LCDC Y-coordinate */\n#define LYC_REG     (*(__REG)0xFF45)    /* LY compare */\n#define DMA_REG     (*(__REG)0xFF46)    /* DMA transfer */\n#define BGP_REG     (*(__REG)0xFF47)    /* BG palette data */\n#define OBP0_REG    (*(__REG)0xFF48)    /* OBJ palette 0 data */\n#define OBP1_REG    (*(__REG)0xFF49)    /* OBJ palette 1 data */\n#define WY_REG      (*(__REG)0xFF4A)    /* Window Y coordinate */\n#define WX_REG      (*(__REG)0xFF4B)    /* Window X coordinate */\n#define KEY1_REG    (*(__REG)0xFF4D)    /* CPU speed */\n#define VBK_REG     (*(__REG)0xFF4F)    /* VRAM bank */\n#define HDMA1_REG   (*(__REG)0xFF51)    /* DMA control 1 */\n#define HDMA2_REG   (*(__REG)0xFF52)    /* DMA control 2 */\n#define HDMA3_REG   (*(__REG)0xFF53)    /* DMA control 3 */\n#define HDMA4_REG   (*(__REG)0xFF54)    /* DMA control 4 */\n#define HDMA5_REG   (*(__REG)0xFF55)    /* DMA control 5 */\n#define RP_REG      (*(__REG)0xFF56)    /* IR port */\n#define BCPS_REG    (*(__REG)0xFF68)    /* BG color palette specification */\n#define BCPD_REG    (*(__REG)0xFF69)    /* BG color palette data */\n#define OCPS_REG    (*(__REG)0xFF6A)    /* OBJ color palette specification */\n#define OCPD_REG    (*(__REG)0xFF6B)    /* OBJ color palette data */\n#define SVBK_REG    (*(__REG)0xFF70)    /* WRAM bank */\n#define IE_REG      (*(__REG)0xFFFF)    /* Interrupt enable */\n```\n\n```text\ngb/hardware.h\n```\n\n```text\ngb/gb.h\n```\n\n```text\nadd_VBL\n```\n\n```text\nadd_LCD\n```\n\n```text\nadd_TIM\n```\n\n```text\nadd_SIO\n```\n\n```text\nadd_JOY\n```\n\n```text\nremove_\n```\n\n```text\ngb/hardware.h\n```\n\n```text\nlibc\\gb\\crt0.s\n```\n\n========================================\n\nComments:\n- GBDK is a C compiler that does let you directly interact with registers - use `gb&#47;hardware.h`. However, the documentation provided isn't good, so you can't actually see what the names are in the doc. Just open up the file under `include&#47;gb&#47;hardware.h` and you can see everything. It also has interrupt support - see `add_VBL`, `add_LCD`, `add_TIM`, `add_SIO`, and `add_JOY` in `gb&#47;gb.h`.\n- Wow thanks @Pokechu22. Sorry you didn't answer in time.\n- This is in-fact how all of those vendor-provided header files do it for you anyway. I'm not intimately familiar with Z80 internals, but do be aware of architectures where a pointer isn't necessarily just any pointer. Bank-switching and separate address spaces which can only be accessed via special instructions are out there which make C's view of a flat memory address space invalid.\n- @JonathonReinhart The GameBoy does use bank switching, but luckily just for 16K of ROM only so system registers never move.\n- @LeeAllan Thanks for clarifying. I didn't expect that they would - that would make a system quite difficult to interact with.\n- Any tricks or tips on removing the 100% CPU load (wait loop)? Any idea on how to add interrupts vectors? The GB has a few fixed location vectors so surely its just a matter of adding a jump to my function there..","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":20,"totalLines":248,"estimatedTokens":2726}}957{"id":"stack-67094664","source":"stackoverflow","questionId":67094664,"title":"How exactly is Rust programming language implemented?","tags":["rust","compiler-construction"],"text":"Title: How exactly is Rust programming language implemented?\nTags: rust, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIf you check languages percentage in github rust lang compiler repository it says that 97.6% of the rust lang compiler is written in rust. So how does this exactly works?. How you can create a programming language (I think this is related to a compiler, since it's whom read the code, doesn't it?) written in itself.\n\n========================================\n\nComments:\n- Read for bootstrapping. That's how almost all programming languages/compilers are made.\n- rustc-dev-guide.rust-lang.org/building/bootstrapping.html\n- In case the OP cares about history, step #1 was originally done in OCAML, many years ago. Since then each new Rust compiler was simply compiled using the previous compiler. Rust is ported to new architectures by cross-compiling it from an existing architecture. (This requires that LLVM supports the new architecture, but that is currently a requirement anyway for rustc to work there anyway.) This reddit thread provides additional context.","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":13,"estimatedTokens":273}}958{"id":"stack-26987531","source":"stackoverflow","questionId":26987531,"title":"What does GNU flex library libfl provide?","tags":["compiler-construction","compilation","gnu","bison","flex-lexer"],"text":"Title: What does GNU flex library libfl provide?\nTags: compiler-construction, compilation, gnu, bison, flex-lexer\nSource: Stack Overflow\n\nQuestion:\nI could compile a program from flex and bison generated files by\n\n`cc lex.yy.c program.tab.c -o output`\n\nand also by\n\n`cc lex.yy.c program.tab.c -lfl -o output`\n\nThey both run smoothly and without any problem.\n\nSo what did `libfl` provide here? I tried to search the documentation but couldn't find a detailed explanation.\n\n========================================\n\nCode:\n```text\ncc lex.yy.c program.tab.c -o output\n```\n\n```text\ncc lex.yy.c program.tab.c -lfl -o output\n```\n\n```text\nlibfl\n```\n\n```text\n$ nm /usr/lib/x86_64-linux-gnu/libfl.a\nlibmain.o:\n0000000000000000 T main\n                 U yylex\nlibyywrap.o:\n0000000000000000 T yywrap\n```\n\n```text\nmain\n```\n\n```text\nyywrap\n```\n\n========================================\n\nComments:\n- So why isn't there a duplicate definition of `main` error from the linker? `T main` doesn't look like a weak symbol.\n- If you don't need the `main` symbol, the `libmain.o` doesn't get linked into your executable, and there is no the multiple definition error.\n- Strange. Where/how is it specified that `libmain.o` can just be dropped from the library if it clashes with something already in the program?\n- Aha! If I use `-Wl,--whole-archive -lfl -Wl,--no-whole-archive` around it, then I get the multiple definitions errors. It seems to be a behavior of how the linker treats archives w.r.t. the executable. Objects providing symbols that are already satisfied (or providing only unneeded symbols) are simply dropped. That wouldn't be the case if we explode the archive into `.o` files and present them individually at the command line (more or less like the `--whole-archive` behavior).\n- Yep. If an object file is listed in linker command explicitly, it gets linked into the executable unconditionally. If an object file is a part of a static library, it gets linked in only if it provides definitions for some undefined symbols.","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":57,"estimatedTokens":504}}959{"id":"stack-16443056","source":"stackoverflow","questionId":16443056,"title":"yylineno has always the same value in yacc file","tags":["c","compiler-construction","yacc"],"text":"Title: yylineno has always the same value in yacc file\nTags: c, compiler-construction, yacc\nSource: Stack Overflow\n\nQuestion:\nfor one project in compilers i have one problem in the syntax analyzer,\nwhen i go to add a symbol in a symbol table, i take always the same value in yylineno...\n\ni did this in the begining:\n\n```\n%{\n\n int yylex(void);\n int yyerror(char* yaccProvidedMessage); \n extern int yylineno; //i declare yylineno from the lexical analyzer\n extern char *yytext;\n extern FILE *yyin; \n\n int scope=0; \n int max_scope;\n%}\n```\n\nand in the grammar when i go to add something in symbol table:\n\ni.e\n\n```\nlvalue: ID {\n\n printf(\"in the output when i give an input \nwith different lines it doesnt recognize the new line\n\n```\nif(xthe lineNO never change,always have 1 as value...\n\nany ideas?\n\n========================================\n\nCode:\n```text\n%{\n\n    int yylex(void);\n    int yyerror(char* yaccProvidedMessage);    \n    extern int yylineno;     //i declare yylineno from the lexical analyzer\n    extern char *yytext;\n    extern FILE *yyin;       \n\n    int scope=0;  \n    int max_scope;\n%}\n```\n\n```text\nlvalue: ID {\n\n        printf(\"<-ID\");     \n        add_data_to_symbol_table((char*)($1),scope,yylineno);\n        printf(\"lineNO:%d\",yylineno);\n\n        }\n        ;\n```\n\n```text\nif(x<=2)\n{\n\n    if(t<1)\n    {\n        k=2;   \n    }\n}\n```\n\n```text\n%option yylineno\n```\n\n```text\nyylineno\n```\n\n```text\nflex\n```\n\n```text\nflex\n```\n\n```text\nyylineno\n```\n\n```text\nyylineno\n```\n\n```text\nyylineno\n```\n\n```text\nyylval\n```\n\n```text\nbison\n```\n\n```text\n@1\n```\n\n```text\n@@\n```\n\n```text\nflex\n```\n\n```text\nbison\n```\n\n```text\nyacc\n```\n\n```text\nyacc\n```\n\n```text\nyylval\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":19,"totalLines":141,"estimatedTokens":416}}960{"id":"stack-8076537","source":"stackoverflow","questionId":8076537,"title":"How to write a compiler back-end to generate assembly for user defined hw architecture, from C code","tags":["c++","c","compiler-construction","assembly","instruction-set"],"text":"Title: How to write a compiler back-end to generate assembly for user defined hw architecture, from C code\nTags: c++, c, compiler-construction, assembly, instruction-set\nSource: Stack Overflow\n\nQuestion:\nI am working on a project where I have to define a new processor hardware architecture.\nI need a compiler to generate assembly code for this target (it has its own instruction set).\n\nPrograms for this processor will be written in C.\n\nMy idea to do this is to parse the C code and generate an Abstract Syntax Tree (AST), then from the AST generate the assembly.\n\nCertainly I'd like to reuse existing components (no need to rewrite a C parser I hope), but what tools or frameworks may I use to accomplish this task?\n\nThanks.\n\n========================================\n\nTop Answer:\nI think the GNU GCC 4.5.x toolchain is excellent, as it can now have plugins as well. Create a foo.c and have a look at raw tree dumps from gcc:\n\n```\ngcc -fdump-tree-original-raw ./foo.c\n```\n\n### Biased opinion\n\nI prefer it over LLVM for porting because it's widely adopted and porting. LLVM puts in an extra level of abstraction that you may not need for your project. However, do study both, there are pros and cons.\n\n### More fun stuff\n\nhttp://dragonegg.llvm.org/\n\n========================================\n\nCode:\n```text\ngcc -fdump-tree-original-raw ./foo.c\n```\n\n========================================\n\nComments:\n- And, as a bonus, you also get other language front-ends.\n- Trying to get any tips or tricks, that's cool anyway here is a massive code base for you to search through and decipher good luck :]\n- I didn't find writing a GCC backend all that hard, even in 1990. Just one file.\n- That's encouraging. I don't have any first hand experience with gcc myself. My evaluation was based on hearsay.","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":47,"estimatedTokens":448}}961{"id":"stack-10149477","source":"stackoverflow","questionId":10149477,"title":"LR(0) or SLR(1) or LALR(1)","tags":["parsing","compiler-construction","context-free-grammar"],"text":"Title: LR(0) or SLR(1) or LALR(1)\nTags: parsing, compiler-construction, context-free-grammar\nSource: Stack Overflow\n\nQuestion:\nI am badly stuck on a question i am attempting from a sample final exam of compilers. I will really appreciate if someone can help me out with an explanation. Thanks\n\nConsider the grammar G listed below\n\n- S = E `$`\n\n- E = E `+` T | T\n\n- T = T `*` F | F\n\n- F = `ident` | `(` E `)`\n\nWhere + * ident ( ) are terminal symbols and `$` is end of file.\na) is this grammar LR( 0 )? Justify your answer.\nb) is the grammar SLR( 1 ) ? Justify your answer.\nc) is this grammar LALR( 1 )? Justify your answer.\n\n========================================\n\nCode:\n```text\n$\n```\n\n```text\n+\n```\n\n```text\n*\n```\n\n```text\nident\n```\n\n```text\n(\n```\n\n```text\n)\n```\n\n```text\n$\n```\n\n```text\nS -> E . $\n```\n\n```text\nS -> E . $\nE -> E . + T\nT -> T . * F\n```\n\n```text\n+\n```\n\n```text\n*\n```\n\n```text\nE\n```\n\n```text\n+\n```\n\n```text\n*\n```\n\n```text\n+\n```\n\n```text\n*\n```\n\n```text\n$\n```\n\n```text\nS -> E\n```\n\n```text\nS\n```\n\n```text\nS\n```\n\n```text\n$\n```\n\n```text\n$\n```\n\n```text\n+\n```\n\n```text\nSLR(1)\n```\n\n========================================\n\nComments:\n- are you just looking for the answer? or are you asking for the approach that you should take?\n- i am looking both for an approach and answer too. Thanks\n- I found some nice lecture notes on this stuff just now: cs.umd.edu/class/spring2011/cmsc430/lectures/lec06.pdf . Increment to lec07.pdf, lec08.pdf, etc.\n- @user976078 Start asking such questions on CSTheory\n- well played - I couldn't for the life of me remember the specifics of this grammar stuff, and I was wondering who could. then I checked out your user page, and saw the link to the language you're doing, and it made sense...","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":24,"totalLines":131,"estimatedTokens":433}}962{"id":"stack-21427806","source":"stackoverflow","questionId":21427806,"title":"How to store tokens while lexical analysis","tags":["java","c","compiler-construction","token","lexical-analysis"],"text":"Title: How to store tokens while lexical analysis\nTags: java, c, compiler-construction, token, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nI'm trying to design a compiler, and am at lexical analysis. \nSay I take a simple \"Hello World!\" program as a file of strings and extract tokens from it.\nWhat is the best way to store these tokens? In a single data structure, or two or more data structures depending on the type of token?\n\n========================================\n\nTop Answer:\nActually, you don't want to store all the tokens, period.\n\nWhat you want to do is produce the tokens, one at a time, and hand them to the parser. After the parser inspects the token, the *token* isn't needed anymore. The parser may copy data from the token and use it to build a node in an AST. You can arguably get by with a single global token, although that isn't considered to be good practice, and if your language has a preprocessor that operates over token streams this won't work.\n\nPerhaps the question you intended to ask is, how are the AST nodes stored long term? The answer is that they can be dynamically allocated from the heap, and they are tied together by parent/child links in the AST. That's enough to track them all reliably.\n\nYou might consider indexing the AST nodes according to type. For most compiling tasks, this is unnecessary. For some tools, this is useful, as it allows the tool to find various node types in very large trees quickly. YMMV.\n\n========================================\n\nComments:\n- `List` is an obvious way to go. Are you asking about the data structure to represent individual tokens, or the data structure allowing iteration over token objects?\n- I think i meant individual tokens. Should all the tokens of a program be stored in one data structure or in separate data structures depending on the type of token. And I reckon i should think in terms of further processing of the tokens during translation and compilation process...","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":492}}963{"id":"stack-14345237","source":"stackoverflow","questionId":14345237,"title":"Programming Language Idea: Avoiding vtable lookups","tags":["c++","compiler-construction","programming-languages","compiler-optimization","vtable"],"text":"Title: Programming Language Idea: Avoiding vtable lookups\nTags: c++, compiler-construction, programming-languages, compiler-optimization, vtable\nSource: Stack Overflow\n\nQuestion:\nI have been toying with an idea for a programming language for a while now: It would essentially be C++ and Java-like in syntax, meant for systems programming (or really any programming that requires high-performance), but with, in my opinion, a more enjoyable syntax than C++. I was thinking about how I would handle virtual methods in hierarchical class structures (my language would not include multiple inheritance), and ways of avoiding vtable lookups. My question is twofold:\n\n- To my understanding, the reason vtable lookups are such a performance-hit (in time-critical scenarios like game-development, at least), is because it requires deferencing the objects vtable pointer, and this vtable is generally a cache-miss. Is this correct, or am I missing some part of the problem?\n\n- My idea for a partial solution is this: If the compiler can determine an object's type completely (ie. it cannot be a type derived from the type it thinks it is), and this object is passed to a function as an argument whose type is a superclass of the object's type, then the location of the virtual method called in the function can be passed as a sort of \"hidden\" argument, which is added at compile-time. Perhaps an example would help:\n\nConsider the following pseudo-code for a class hierarchy:\n\n```\nclass Animal {\n public void talk() { /* Generic animal noise... */ }\n // ...\n}\n\nclass Dog extends Animal {\n public void talk() { /* Override of Animal::talk(). */ }\n // ...\n}\n\nvoid main() {\n Dog d = new Dog();\n doSomethingWithAnimal(d);\n}\n\nvoid doSomethingWithAnimal(Animal a) {\n // ...\n a.talk();\n // ....\n}\n```\n\nKeep in mind that this is pseudocode, not C++ or Java or similar. Also, assume that the Animal argument is implicitly passed by reference, not value. Because the compiler can see that `d` is definitely of type `Dog`, it could translate the `doSomethingWithAnimal` definition into something like this:\n\n```\nvoid doSomethingWithAnimal(Animal a, methodptr talk = NULL) {\n // ...\n if ( talk != NULL ) {\n talk(a);\n } else {\n a.talk();\n }\n // ...\n}\n```\n\nThen `main` would look be translated by the compiler to something like this:\n\n```\nvoid main() {\n Dog d = new Dog();\n doSomethingWithAnimal(d, Dog::talk);\n}\n```\n\nObviously this wouldn't completely eradicate the need for a vtable, and one would probably still need to be provided for the cases when the objects exact type cannot be determined, but what are your thoughts on this as a performance optimization? I plan on using registers to pass arguments whenever possible, and even if the arguments had to spill onto the stack, it is more likely that the methodptr argument on the stack will be a cache-hit than the vtable values, right? Any and all thoughts are much appreciated.\n\n========================================\n\nCode:\n```text\nclass Animal {\n    public void talk() { /* Generic animal noise... */ }\n    // ...\n}\n\nclass Dog extends Animal {\n    public void talk() { /* Override of Animal::talk(). */ }\n    // ...\n}\n\nvoid main() {\n    Dog d = new Dog();\n    doSomethingWithAnimal(d);\n}\n\nvoid doSomethingWithAnimal(Animal a) {\n    // ...\n    a.talk();\n    // ....\n}\n```\n\n```text\nvoid doSomethingWithAnimal(Animal a, methodptr talk = NULL) {\n    // ...\n    if ( talk != NULL ) {\n        talk(a);\n    } else {\n        a.talk();\n    }\n    // ...\n}\n```\n\n```text\nvoid main() {\n    Dog d = new Dog();\n    doSomethingWithAnimal(d, Dog::talk);\n}\n```\n\n```text\nd\n```\n\n```text\nDog\n```\n\n```text\ndoSomethingWithAnimal\n```\n\n```text\nmain\n```\n\n```text\nvirtual\n```\n\n========================================\n\nComments:\n- this technique is already implemented in most all production c++ compilers and goes by the name of \"devirtualization\".\n- You might want to revisit the premise that virtual dispatch is slow until you measure it. Also note that `if` (branches) have a different impact in performance, which might be even larger than the impact of the virtual dispatch. When the compiler *knows* the object type at compile time, it won't use dynamic dispatch anyway, but in your proposal the functions would have to take a number of hidden parameter (another cost) for each of the possible functions that it needs. You might end up incurring a larger cost than you are trying to remove...\n- @KyleLutz - can it handle the case described by Zach though? I thought that when compiling `doSomethingWithAnimal` as a compilation unit, the compiler cannot infer the static type of `a` and so cannot do anything about the problem at hand.\n- @DavidRodr&#237;guez-dribeas True, though the branch can be eliminated (at the cost of code bloat) by generating an overload.\n- @delnan: Yes, you can avoid the `if` by providing suitable overloads, but how many will you need? How many functions can be called on the argument? How many arguments do you want to pass? Or will you have to see the definition of the function to infer the needs and pass only those needed? --uhm... don't you get exactly that when the compiler inlines?\n- @DavidRodr&#237;guez-dribeas That's another problem, yes. And yes, inlining can solve this to a degree, but requires either LTO or giving up separate compilation -- not to mention that inlining *also* adds code bloat.\n- furthermore, `doSomethingWithAnimal()` is flawed in that it accepts `Animal a` by value and thus `Animal::talk()` will always be called due to type-slicing. this can be fixed by changing the signature to `void doSomethingWithAnimal(Animal &a, methodptr talk = NULL)`.\n- @delnan: The question is whether the set of questions in my last comment can be answered without doing exactly what compilers do today. In an oversimplified example in the question there is a pointer to the final overrider for the single member function, but in real code there would be multiple such members. Passing all them as arguments would be more costly than the virtual dispatch, knowing which ones to pass would be hard in a separate compilation model and it would push the implementation of the function into the interface (at least into the interface that the compiler sees)\n- Can I be the annoying one? `Dog d = new Dog();` <-- `new Dog()` is a `Dog*`.\n- @AlexChamberlain, KyleLutz: OP says he's thinking about a new language and that his code is pseudocode. His idea may well be to implement java-like syntax/semantics for these things, so your comments about `*` and type slicing may not apply.\n- I couldn't agree more with @DavidRodr&#237;guez-dribeas: Unless you have very solid *hard evidence* that virtual dispatch is a critical performance bottleneck, going all the way to design and implement a *new language* is one of the most extreme reactions I've seen yet.\n- Just to clear up some confusion, my examples are in pseudocode, not C++, as @us2012 mentioned, and the `Animal` (or `Dog`) argument is implicitly passed by reference, not value. I will update the question to reflect that better. Also, as far as actual measurements of virtual dispatch performance, I started thinking about this after reading a code review of the Doom3 source, which mentions a few techniques the developers used to avoid vtable lookups. From this I assumed that id software had run into performance issues, but I have not seen actual measurements.","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":8,"totalLines":141,"estimatedTokens":1844}}964{"id":"stack-26188276","source":"stackoverflow","questionId":26188276,"title":"Why doesn't %prec have an effect in this bison grammar?","tags":["parsing","compiler-construction","bison"],"text":"Title: Why doesn't %prec have an effect in this bison grammar?\nTags: parsing, compiler-construction, bison\nSource: Stack Overflow\n\nQuestion:\nConsider the following Bison grammar (this is stripped down from a much larger grammar I'm working on):\n\n```\n%token ident\n%left '+'\n%left CALLPREC\n\n%%\n\nstart: add ';' ;\nexpr: ident | call | add ;\ncall: expr '(' ')' %prec CALLPREC ;\nadd: expr '+' expr ;\n```\n\nObviously without precedence there's a s/r conflict when parsing an expression like `foo + bar()`. I'm trying to understand why the `%prec` declaration doesn't resolve that conflict. I'm using Bison 3.0.2, which seems to think the directive is useless:\n\n```\n$ bison -r state,solved -Wall ambigram.y \nambigram.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]\nambigram.y:5.1-5: warning: useless precedence and associativity for CALLPREC [-Wprecedence]\n```\n\nOddly, eliminating the `%prec CALLPREC` and declaring `%left '('` resolves the conflict, but declaring `%left ')'` does not. This is the opposite of what I'd expect from the Bison docs, which say that [by] default, the precedence of a rule is that of its last token.\n\n========================================\n\nCode:\n```text\n%token ident\n%left '+'\n%left CALLPREC\n\n%%\n\nstart: add ';' ;\nexpr: ident | call |  add ;\ncall: expr '(' ')' %prec CALLPREC ;\nadd: expr '+' expr ;\n```\n\n```text\n$ bison -r state,solved -Wall  ambigram.y \nambigram.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]\nambigram.y:5.1-5: warning: useless precedence and associativity for CALLPREC [-Wprecedence]\n```\n\n```text\nfoo + bar()\n```\n\n```text\n%prec\n```\n\n```text\n%prec CALLPREC\n```\n\n```text\n%left '('\n```\n\n```text\n%left ')'\n```\n\n```text\nident '+' ident '(' ')'\n```\n\n```text\n%prec\n```\n\n```text\nadd\n```\n\n```text\n'('\n```\n\n```text\nexpr + expr\n```\n\n```text\nadd\n```\n\n```text\n'('\n```\n\n```text\ncall\n```\n\n========================================\n\nComments:\n- Very clear, thank you! I had exactly the misconception you described. And now that I've posted, I see you answered an almost identical question four years ago, too.\n- @ChrisDodd: *I've toyed with the idea of writing a yacc variant ... it would have predecence only on rules.* Can you do this in bison already, by declaring every token produced by the lexer only as `%token` and declaring extra `%precedence` tokens to use for `%prec` to resolve conflicts?\n- @BenC That won't work, as it won't have precedence for any token, so won't be able to resolve any shift/reduce conflicts. What is needed is to infer the precedence of the token to be shifted from the rules that might be reduced after shifting that token -- the rules in the post-shift item set before closure when computing the shift/reduce state machine from the grammar.","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":15,"totalLines":110,"estimatedTokens":678}}965{"id":"stack-2354560","source":"stackoverflow","questionId":2354560,"title":"Creating an executable file without a compiler","tags":["compiler-construction","binary","executable"],"text":"Title: Creating an executable file without a compiler\nTags: compiler-construction, binary, executable\nSource: Stack Overflow\n\nQuestion:\nI came across an article a long while ago on how to write out a .com file directly without using any external tools. The method was to basically copy con myfile.com and then hit ctrl+alt+number for each instruction.\n\nI've lost the url for the guide... Google isn't helping much either.\n\nIf you have the link, please could you post it.\n\n========================================\n\nTop Answer:\nInsipred by Ignacio Vazquez-Abrams I found this link, which uses `ECHO` in stead of `COPY CON`.\n\n========================================\n\nCode:\n```text\nECHO\n```\n\n```text\nCOPY CON\n```\n\n========================================\n\nComments:\n- I know I probably shouldn't ask, but why?? Do you miss entering bootstrapping code on your IMSAI with the front panel toggle switches?\n- I don't have a good reason :) . It just seemed like an interesting thing to learn.\n- Well, it's just a nice 'party trick'... If I'm not able to find the answer in another couple of days, I'll just mark your answer as accepted :- ).","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":33,"estimatedTokens":284}}966{"id":"stack-13506607","source":"stackoverflow","questionId":13506607,"title":"Replacing the operand of an LLVM instruction","tags":["c++","compiler-construction","llvm"],"text":"Title: Replacing the operand of an LLVM instruction\nTags: c++, compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nIn the following code, I try to replace operand(s) of an LLVM instructions. However it doesn't work and nothing is changed. Any idea how to solve this?\n\n```\nfor (OI = insn->op_begin(), OE = insn->op_end(); OI != OE; ++OI)\n{\n Value *val = *OI;\n iter = mapClonedAndOrg.find( val );\n\n if( iter != mapClonedAndOrg.end( ) )\n {\n // Here I try to replace the operand, to no effect!\n val = (Value*)iter->second.PN;\n }\n}\n```\n\n========================================\n\nTop Answer:\nWhat you are doing is simply making a local pointer point to something else, you don't actually change *what* it points to. For that you need to use the dereferencing operator `*`:\n\n```\n*val = *((Value*) iter->second.PN);\n```\n\n========================================\n\nCode:\n```text\nfor (OI = insn->op_begin(), OE = insn->op_end(); OI != OE; ++OI)\n{\n    Value *val = *OI;\n    iter = mapClonedAndOrg.find( val );\n\n    if( iter != mapClonedAndOrg.end( ) )\n    {\n        // Here I try to replace the operand, to no effect!\n        val = (Value*)iter->second.PN;\n    }\n}\n```\n\n```text\nfor (OI = insn->op_begin(), OE = insn->op_end(); OI != OE; ++OI)\n{\n    Value *val = *OI;\n    iter = mapClonedAndOrg.find( val );\n\n    if( iter != mapClonedAndOrg.end( ) )\n    {\n        *OI = (Value*)iter->second.PN;\n    }\n}\n```\n\n```text\nOI\n```\n\n```text\nval\n```\n\n```text\n*val = *((Value*) iter->second.PN);\n```\n\n```text\n*\n```\n\n========================================\n\nComments:\n- Well this doesn't work in context of LLVM, infact it doesn't even compile, as = operator is private member of Value class. In LLVM, what I think is that every operand is linked to the instruction through a pointer, so just changing the pointer should replace the operand, but anyhow that is also not working, so I'm not really sure what is the right way to solve this. At least it worked for me for other things like instructions, but for operands the same method is not working.\n- @user1018562 You can't change the pointer `val`, as that will just make the *local* variable `val` point to something else. A possible solution then might be to change the *iterator*: `*OI = (Value*) iter->second.PN;`","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":81,"estimatedTokens":564}}967{"id":"stack-4320166","source":"stackoverflow","questionId":4320166,"title":"How to stream program code?","tags":["compiler-construction","streaming","lisp"],"text":"Title: How to stream program code?\nTags: compiler-construction, streaming, lisp\nSource: Stack Overflow\n\nQuestion:\nI've been learning Lisp recently (Scheme, Racket and Clojure to various extents) and have read the corresponding literature on famous Playstation developer Naughty Dog.\n\nThis Gamasutra article mentions that their streaming engine actually streams in game code in addition to game data.\nNow, they have a dialect of Lisp called GOAL which they use extensively.\nI know of Lisp's \"Code as Data\", although I have yet to grok this concept fully. So, I was wondering if the ability to stream code is a property of their code being Lisp data or compiled machine code, as opposed to being a generic design pattern that can be used in many languages?\n\nCan someone point me to literature on this topic?\n\nBonus: Can someone point me to literature on long-term-listening-compilers as mentioned in the Wikipedia article?\n\n========================================\n\nTop Answer:\nTheir streaming engine more or less replicates what DLL (windows), SO(Linux), or Dylib loading does on more general OSes, and isn't all that interesting outside of the fact that they implemented it themselves. It doesn't have anything to do with the language they use. As far as long term listening compilers go that is the first time I have heard edit and continue ascribed to the compiler it is usually a feature of the debugger.\n\n========================================\n\nCode:\n```text\non connection:\n   read Lisp form\n   eval Lisp form\n   close connection\n```\n\n========================================\n\nComments:\n- That's right, thanks for reminding me of that! A friend who's been doing some profiling projects of his own was telling me to look into how debuggers do it. The particular wording here of \"long term listening compiler\" has really thrown me off track.\n- Just for reference, I found this link: gamedev.stackexchange.com/questions/221/&hellip; which goes into a little more detail about edit-and-continue or hot-swapping type systems for C and C++ type langauges.","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":35,"estimatedTokens":514}}968{"id":"stack-11072580","source":"stackoverflow","questionId":11072580,"title":"Specifically what does a compiler do to aggressively optimize generated bytecode?","tags":["c#","flash","optimization","compiler-construction","llvm"],"text":"Title: Specifically what does a compiler do to aggressively optimize generated bytecode?\nTags: c#, flash, optimization, compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nI have been reading up on the functionality of various compilers and I've come across the term \"aggressive optimization\" that many compilers are reported to perform. LLVM, for example cites the following compile-time optimization features:\n\n- Memory/pointer specific\n\n- Loop transforms\n\n- Data flow\n\n- Arithmetic\n\n- Dead code elimination\n\n- Inlining\n\nWhat does this mean specifically? Say you had the following code snippet, how could you optimize the generated byte code to run any faster than what the compiler generated? I'm specifically interested in optimizing the bytecode of JIT-powered runtimes such as C#, Java and Flash. This is tricky because the JIT only supports a subset of the opcodes that the processor usually does, which limits the amount of optimization you can do. Still, I'm interested to see whats possible and exactly what transformations could push the limits of the VM.\n\nFictitious block of code:\n\n```\nfor (i = 0; i > 16) - 10;\n }else{\n out = ((in Approximate pseudo code generated by the compiler, for a stack-based JIT VM such as Flash Player: (forgive me for any mistakes, this is entirely handwritten!)\n\n```\n// i = 0\nlabel: \"forInit\"\n push 0\n writeTo \"i\"\n\n// while i > 16) - 10;\n push \"in\"\n push 2\n divide\n push 16\n rightshift\n push 10\n minus\n writeTo \"out\"\n goto \"ifEnd\"\n\n // else\n label: ifPart2\n\n // out = ((in << 5) / 2) * 50 + 10;\n push \"in\"\n push 5\n leftshift\n push 2\n divide\n push 50\n multiply\n push 10\n add\n writeTo \"out\"\n\n // dataOut[i] = out;\n label: ifEnd\n push \"out\"\n push \"i\"\n push \"dataOut\"\n writeProp\n\n // i++\n push \"i\"\n increment\n writeTo \"i\"\n\n // while i < 100\n goto \"forStart\"\nlabel: \"forEnd\"\n```\n\n========================================\n\nTop Answer:\nI've also been working on this, the full list of transformations that LLVM performs, organized under headers:\n\n**Dead code removal**\n\n- Aggressive Dead Code Elimination\n\n- Dead Code Elimination\n\n- Dead Argument Elimination\n\n- Dead Type Elimination\n\n- Dead Instruction Elimination\n\n- Dead Store Elimination\n\n- Dead Global Elimination\n\n- Delete dead loops\n\n**Unwanted data removal**\n\n- Strip all symbols from a module\n\n- Strip debug info for unused symbols\n\n- Strip Unused Function Prototypes\n\n- Strip all llvm.dbg.declare intrinsics\n\n- Strip all symbols, except dbg symbols, from a module\n\n- Merge Duplicate Global Constants\n\n- Remove unused exception handling info\n\n**Inlining functions**\n\n- Merge Functions\n\n- Partial Inliner\n\n- Function Integration/Inlining\n\n**Loop optimization**\n\n- Loop-Closed SSA Form Pass\n\n- Loop Invariant Code Motion\n\n- Extract loops into new functions\n\n- Extract at most one loop into a new function\n\n- Loop Strength Reduction\n\n- Rotate Loops\n\n- Canonicalize natural loops\n\n- Unroll loops\n\n- Unswitch loops\n\n**Misc**\n\n- Promote 'by reference' arguments to scalars\n\n- Combine instructions to form vector instructions within basic blocks\n\n- Profile Guided Basic Block Placement\n\n- Break critical edges in CFG\n\n- Optimize for code generation\n\n- Simple constant propagation\n\n- Deduce function attributes\n\n- Global Variable Optimizer\n\n- Global Value Numbering\n\n- Canonicalize Induction Variables\n\n- Insert instrumentation for edge profiling\n\n- Insert optimal instrumentation for edge profiling\n\n- Combine redundant instructions\n\n- Internalize Global Symbols\n\n- Interprocedural constant propagation\n\n- Interprocedural Sparse Conditional Constant Propagation\n\n- Jump Threading\n\n- Lower atomic intrinsics to non-atomic form\n\n- Lower invoke and unwind, for unwindless code generators\n\n- Lower SwitchInst's to branches\n\n- Promote Memory to Register\n\n- MemCpy Optimization\n\n- Unify function exit nodes\n\n- Reassociate expressions\n\n- Demote all values to stack slots\n\n- Scalar Replacement of Aggregates (DT)\n\n- Sparse Conditional Constant Propagation\n\n- Simplify well-known library calls\n\n- Simplify the CFG\n\n- Code sinking\n\n- Promote sret arguments to multiple ret values\n\n- Tail Call Elimination\n\n- Tail Duplication\n\n========================================\n\nCode:\n```text\nfor (i = 0; i < 100; i++){\n    in = dataIn[i];\n    if ((in % 5) == 0){\n        out = ((in / 2) >> 16) - 10;\n    }else{\n        out = ((in << 5) / 2) * 50 + 10;\n    }\n    dataOut[i] = out;\n}\n```\n\n```text\n// i = 0\nlabel: \"forInit\"\n   push 0\n   writeTo \"i\"\n\n// while i < 100\nlabel: \"forStart\"\n   push \"i\"\n   push 100\n   jumpIfMoreThan \"forEnd\"\n\n       // in = dataIn[i];\n       push \"i\"\n       push \"dataIn\"\n       readProp\n       saveTo \"in\"\n\n       // if ((in % 5) == 0)\n       push \"in\"\n       push 5\n       mod\n       push 0\n       jumpIfNotEquals \"ifPart2\"\n       label: ifPart1\n\n           // out = ((in / 2) >> 16) - 10;\n           push \"in\"\n           push 2\n           divide\n           push 16\n           rightshift\n           push 10\n           minus\n           writeTo \"out\"\n           goto \"ifEnd\"\n\n       // else\n       label: ifPart2\n\n           // out = ((in << 5) / 2) * 50 + 10;\n           push \"in\"\n           push 5\n           leftshift\n           push 2\n           divide\n           push 50\n           multiply\n           push 10\n           add\n           writeTo \"out\"\n\n       // dataOut[i] = out;\n       label: ifEnd\n           push \"out\"\n           push \"i\"\n           push \"dataOut\"\n           writeProp\n\n       // i++\n       push \"i\"\n       increment\n       writeTo \"i\"\n\n   // while i < 100\n   goto \"forStart\"\nlabel: \"forEnd\"\n```\n\n```text\nout = ((i / 2) >> 16) - 10;\n```\n\n```text\nout = (i >> 17) - 10;\n```\n\n```text\nout = ((i << 5) / 2) * 50 + 10;\n```\n\n```text\nout = (i << 4) * 50 + 10;\n```\n\n```text\n// i = 0\nlabel: \"forInit\"\n   push 0\n   writeTo \"i\"\n\n// while i < 100\nlabel: \"forStart\"\n   push \"i\"\n   push 100\n   jumpIfMoreThan \"forEnd\"\n\n       // in = dataIn[i];\n       push \"i\"\n       push \"dataIn\"\n       readProp\n       saveTo \"in\"\n\n       // if ((in % 5) == 0)\n       push \"in\"\n       push 5\n       mod\n       push 0\n       jumpIfNotEquals \"ifPart2\"\n       label: ifPart1\n           // optimization: remove unnecessary /2\n           // out = ((in / 2) >> 16) - 10;\n           push \"in\"\n           push 17\n           rightshift\n           push 10\n           minus\n           // optimization: don't need out var since value on stack\n           // dataOut[i] = out;\n           push \"i\"\n           push \"dataOut\"\n           writeProp\n           // optimization: avoid branch to common loop end \n           // i++\n           push \"i\"\n           increment\n           writeTo \"i\"\n           goto \"forStart\"\n\n       // else\n       label: ifPart2\n           // optimization: remove unnecessary /2\n           // out = ((in << 5) / 2) * 50 + 10;\n           push \"in\"\n           push 4\n           leftshift\n           push 50\n           multiply\n           push 10\n           add\n           // optimization: don't need out var since value on stack\n           // dataOut[i] = out;\n           push \"i\"\n           push \"dataOut\"\n           writeProp\n           // optimization: avoid branch to common loop end \n           // i++\n           push \"i\"\n           increment\n           writeTo \"i\"\n           goto \"forStart\"\nlabel: \"forEnd\"\n```\n\n========================================\n\nComments:\n- As a sidenote: It's basically impossible for the optimizer to run on the actual bytecode - most optimizations are too complex for that. It will usually generate some (actual usually more than one) intermediate language first (often: CFG, SSA) do the optimizations on those and then emit the optimized code. For Java/C# that's native code not bytecode so we really aren't limited by what the bytecode allows (indeed javac for example does no interesting optimizations whatsoever).\n- What you said is valid considering \"i\" is an `int` - If it were a `float` those optimizations wouldn't work, would they?\n- I don't think there would be an advantage in doing that if i were a float.\n- Thanks for the responses hatchet, but that was an error in the code itself. I should have written \"out = in / 2 ...\"\n- I had a feeling that was the case, but didn't want to assume that, and figured you'd edit if it was.\n- Assuming the new edited code snippet, with the if/else being unique per test, based on the `in` variable, are there any optimizations in the byte code itself that are possible?\n- I've edited to match your edit. Is the i % 5 line right, or is it in % 5?\n- I tried to use a real world example, so to keep it complex - `in % 5`... So do you mean that improving the way the code is written would give you about the same performance as if the compiler had optimized it? So the compiler does not perform any complicated bytecode level optimizations (I assumed reordering the push/pop sequence) or anything like that, but simply changes the way the code is written? (loops, if/else, etc)\n- @Geotarget - optimizations can be large, such as unrolling loops, or small such as taking advantage of something already on the stack. See my recent edit.","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":393,"estimatedTokens":2255}}969{"id":"stack-33624392","source":"stackoverflow","questionId":33624392,"title":"How to get Register-Transfer-Language (RTL) Code from C and JAVA Code?","tags":["c","gcc","compiler-construction"],"text":"Title: How to get Register-Transfer-Language (RTL) Code from C and JAVA Code?\nTags: c, gcc, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nWhen any compiler like GCC compiles a C Program it generates intermediate code. Just like we can get Assembly code by applying -s option to any .c file, similarly I want to get Register-Transfer-Language (RTL) for C and JAVA file . How to get it ?\n\n========================================\n\nCode:\n```text\ngcc  -fdump-tree-gimple <file>\n```\n\n========================================\n\nComments:\n- Sorry, but i was just searching for any script or any tool for it, but didn't found anything.\n- You didn't search very hard: calypto.com/en/products/catapult/overview\n- well i tried using Verilog tool and bambu but with no luck.\n- It is not nevessary to generate intermediate code.\n- Why did you roll back the clarifications that were added to your question? They were not totally unreasonable, and actually clarified your post for those interested in the subject.\n- sorry i was unaware that RTL stands for things other than Register Transfer Language.\n- thanks, this was exactly what i was looking for. Do we also have such option for java ? We use javac for compilation in it.\n- Sorry, I haven't done anything with Java, except in coffeecups since I left school 15 years ago.","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":25,"estimatedTokens":330}}970{"id":"stack-28389568","source":"stackoverflow","questionId":28389568,"title":"How to traverse typed abstract syntax tree in OCaml compiler","tags":["compiler-construction","ocaml","abstract-syntax-tree","ocamlbuild"],"text":"Title: How to traverse typed abstract syntax tree in OCaml compiler\nTags: compiler-construction, ocaml, abstract-syntax-tree, ocamlbuild\nSource: Stack Overflow\n\nQuestion:\nI'm trying to dump type information of all identifiers in an OCaml project, basically it's the same as traversing the typed abstract syntax tree(https://github.com/ocaml/ocaml/blob/trunk/typing/typedtree.mli). Since I'm new to OCaml compiler's codebase, I'm not sure whether the Compiler has provided apis so we could easily write a plugin to do the job or we have to hack the compiler code? Also how does this interact with OCamlbuild? Thanks for any hints or advices.\n\n========================================\n\nTop Answer:\nOCaml provides its own compiler as a library named `compiler-libs`. It has everything in it, allowing one to move from a concrete syntax towards executable, with all intermidiate steps under your control, including typedtree, of course. \n\nThe bad news is that it is not documented. I would suggest you, to use `utop` or `merlin` to explore this library. \n\nYou do not need anything special to do with ocamlbuild to use `compiler-libs`, it is a regular library.\n\n========================================\n\nCode:\n```text\n(* ocamlfind ocamlc -package compiler-libs.common -c example.ml *)\nopen Typedtree\nopen TypedtreeIter\n\nmodule MyIteratorArgument = struct\n  include DefaultIteratorArgument\n\n  let enter_pattern p = match p.pat_desc with\n    | Tpat_var (id, _) ->\n        Format.printf \"@[<2>%s@ : %a@]@.\"\n          (Ident.name id)\n          Printtyp.type_scheme p.pat_type\n    | _ -> ()\nend\n\nmodule Iterator = TypedtreeIter.MakeIterator(MyIteratorArgument)\n```\n\n```text\nstructure\n```\n\n```text\nTypedtreeIter\n```\n\n```text\ncompiler-libs\n```\n\n```text\nTypedtreeIter\n```\n\n```text\nTypedtreeIter.IteratorArgument\n```\n\n```text\npattern\n```\n\n```text\nenter_pattern\n```\n\n```text\nexit_pattern\n```\n\n```text\nMakeIterator\n```\n\n```text\nIteratorArgument\n```\n\n```text\nenter_*\n```\n\n```text\nexit_*\n```\n\n```text\nDefaultIteratorArgument\n```\n\n```text\nenter_*\n```\n\n```text\nexit_*\n```\n\n```text\nIteratorArgument\n```\n\n```text\nDefaultIteratorArgument\n```\n\n```text\nTypedtreeMap\n```\n\n```text\nTypedtreeIter\n```\n\n```text\nTypedtreeMap\n```\n\n```text\ncompiler-libs\n```\n\n```text\nutop\n```\n\n```text\nmerlin\n```\n\n```text\ncompiler-libs\n```\n\n========================================\n\nComments:\n- Hi, @ivg Thanks for the reply. Before I dive into the source code, one quick question is that does this library exposes APIs for a whole OCaml project processing? Or due to OCaml's great support for separate compilation, the problem of typing whole project reduces to typing each files one by one? Thanks.\n- as far as I understand it exposes only the compiler part, so that you can build your own driver using the libraries. If you really need to take ocaml project and compile it with your own tweaked compiler, then it would be easier just to tweak the compiler. But for testing, and investigation, compiler is still a good choice.\n- Hi, @camlspotter, thanks for the great example! The `TypedtreeIter` is neat! One thing I think is missing the iterator functor does not provide current `Env` we are in. So we may not be able to track a variable's definition site. Have you met the problem before and know other utility functions in the code base solving this?\n- Each typed AST node has the type environment which was used to build the node.","metadata":{"transformedAt":"2026-08-18T18:32:17.844Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":25,"totalLines":141,"estimatedTokens":847}}971{"id":"stack-23857711","source":"stackoverflow","questionId":23857711,"title":"Which part of the dragon book is outdated?","tags":["compiler-construction"],"text":"Title: Which part of the dragon book is outdated?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI've encountered this review, where the reviewer says:\n\n Given multi processors and the now proven success of LH parsing and\n much more, much of the old dragon series is out of date and Modern\n Compiler Design (MCD) is well positioned to fill the void, along with\n Cooper.\n\nGiven that the review is very long and detailed, I assume he is knowledgeable about the subject.\nSo specifically, in which part of the dragon book is outdated?\n\n(What is LH parsing anyway? I tried googling but couldn't find anything relevant. Maybe a typo?)\n\n========================================\n\nComments:\n- I suppose the OP could be talking about LR parsing (as opposed to LL). However, I'm not sure that the jury is out on LL v LR.","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":206}}972{"id":"stack-15465958","source":"stackoverflow","questionId":15465958,"title":"Using `GCCs` pre-processor as an assembler","tags":["c","gcc","assembly","compiler-construction"],"text":"Title: Using `GCCs` pre-processor as an assembler\nTags: c, gcc, assembly, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThere are various open source assemblers such as gas, nasm, and yasm. They have different `pseudo-ops` and `macro` syntaxes. For many open source projects, assembler is pre-processed to replace constants and platform conditionals. \n\nWhat limitations would `gcc` have creating assembler assuming you can use all current `attributes` and `#pragmas`, excluding translation performance (compile/assemble to binary time)? \n\nI am not talking about inline-assembly. \n\n```\n#define MOV(RA,RB) (OXFEB10000UL | RA I believe that using pointer arithmetic can allow simulation of `.` and other `labels`. Perhaps this is an XY problem; I am trying to understand why there are so many assemblers at all. It seems like everything can be done by the pre-processor and the assembler is really a programmer preference; or there is a technical limitation I am missing.\n\nI guess this might be related to 'Something you can do with an assembler that you can't do with shell code'.\n\n**Edit:** I have re-tagged this from *C* to *compiler*. I am interested in the technical details of an assembler. Is it simply a `1-1` translation and emitting relocations (as a compiler will) or is there more? I don't mean for people to code assembler as I have outlined above. I am trying to understand what the assemblers are doing. I don't believe there is a **Dragon book** for assemblers. Of course, the pre-processor can not create a `binary` by itself and needs additional machinery; it only translates text.\n\n========================================\n\nTop Answer:\nWhat limitations would gcc have creating assembler [...] ?\n\n**A lot.** There's a reason we use assemblers for assembling and C preprocessors for preprocessing.\n\nFirstly, as you just just have shown it yourself, you can't use the normal assembler syntax be it in style Intel or AT&T. You have to use those ugly parentheses.\n\nSecond, those `__attribute__` directives you're talking about have nothing to do with the preprocessor, it doesn't even recognize them. They're hints for **the compiler,** and the compiler will in turn produce assembly code guided by these attrbutes (or not).\n\n Perhaps this is an XY problem\n\nIt is for sure.\n\n I am trying to understand why there are so many assemblers at all.\n\nFor the same reason there are various types of programming languages, compilers, cars and clothes out there: one tool doesn't fit everyone's needs. People are different, they do different things with their toolchain, they find the one easier to use than the other (personally I'd use the GNU assembler if it didn't require the AT&T syntax, which I just can't support), etc.\n\n========================================\n\nCode:\n```text\n#define MOV(RA,RB)  (OXFEB10000UL | RA << 16 | RB)  \n #define ADD(RA,RB)  (OXFEB20000UL | RA << 16 | RB)  \n #define RET         (OXFEB7ABCDUL)  \n\n unsigned long add4[] __attribute(section(\".text\")) =\n {\n    ADD(R0,R1),\n    ADD(R2,R3),\n    MOV(R1,R2),\n    ADD(R0,R1),\n    RET()\n };\n```\n\n```text\npseudo-ops\n```\n\n```text\nmacro\n```\n\n```text\ngcc\n```\n\n```text\nattributes\n```\n\n```text\n#pragmas\n```\n\n```text\n.\n```\n\n```text\nlabels\n```\n\n```text\n1-1\n```\n\n```text\nbinary\n```\n\n```text\n1-1\n```\n\n```text\n#define\n```\n\n```text\nobjcopy\n```\n\n```text\n<templates>\n```\n\n```text\n__attribute__\n```\n\n========================================\n\nComments:\n- I don't see this working for x86. It's non-trivial to select the best encoding for a particular instruction, alignment would be tricky, and optimizing branch size would be completely impossible as far as I can tell. And in any case you'd be using a hammer on a screw.\n- @Harold: Can you elaborate on how it won't work? For the x86, it would be much more complex and would require `byte` alignments. Does an x86 assembler have to keep large basic blocks in memory to produce op-codes?\n- @Bill - The x86 has variable length instructions, and there is not a 1-1 relation between mnemonic and opcode. For example a \"simple\" MOV has over 20 different variations, like that `MOV AX,1` is different from `MOV other_reg,1`.\n- @BillPringlemeir no, but it does end up keeping large blocks in memory in order to satisfy alignment constraints while optimizing branch size. It only *requires* byte-alignment but it really *likes* 16-alignment for some branch targets.\n- You might find this manual of an ancient assembler useful. bitsavers.org/pdf/dec/pdp1/PDP-1_Macro.pdf\n- I don't really think you (or maybe anyone) understood my question. Maybe that is my problem?\n- An important point missed is relocations. Assemblers write object files where things are noted to be relocatable by a linker or loader. The concept of using the pre-processor is limited to PC-relative leaf functions (or code with no externals).\n- Another book on the topic eli.thegreenplace.net/2010/01/25/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":15,"totalLines":122,"estimatedTokens":1224}}973{"id":"stack-51737002","source":"stackoverflow","questionId":51737002,"title":"How does linker handle variables with different linkages?","tags":["c++","c","linker","compiler-construction","operating-system"],"text":"Title: How does linker handle variables with different linkages?\nTags: c++, c, linker, compiler-construction, operating-system\nSource: Stack Overflow\n\nQuestion:\nIn C and C++ we can manipulate a variable's linkage. There are three kinds of linkage: no linkage, internal linkage, and external linkage. My question is probably related to why these are called \"linkage\" (How is that related to the linker).\n\nI understand a linker is able to handle variables with external linkage, because references to this variable is not confined within a single translation unit, therefore not confined within a single object file. How that actually works under the hood is typically discussed in courses on operating systems.\n\nBut how does the linker handle variables (1) with no linkage and (2) with internal linkage? What are the differences in these two cases?\n\n========================================\n\nTop Answer:\nAs far as C++ itself goes, this does not matter: the only thing that matters is the behavior of the system as a whole. Variables with no linkage should not be linked; variables with internal linkage should not be linked across translation units; and variables with external linkage should be linked across translation units. (Of course, as the person writing the C++ code, you must obey all of *your* constraints as well.)\n\nInside a compiler and linker suite of programs, however, we certainly *do* have to care about this. The method by which we achieve the desired result is up to us. One traditional method is pretty simple:\n\nIdentifiers with no linkage are never even passed through to the linker.\n\nIdentifiers with internal linkage are not passed through to the linker either, or *are* passed through to the linker but marked \"for use within this one translation unit only\". That is, there is no `.global` declaration for them, or there is a `.local` declaration for them, or similar.\n\nIdentifiers with external linkage are passed through to the linker, and if internal linkage identifiers are seen by the linker, these external linkage symbols are marked differently, e.g., have a `.global` declaration or no `.local` declaration.\n\nIf you have a Linux or Unix like system, run `nm` on object (`.o`) files produced by the compiler. Note that some symbols are annotated with uppercase letters like `T` and `D` for text and data: these are global. Other symbols are annotated with lowercase letters like `t` and `d`: these are local. So these systems are using the \"pass internal linkage to the linker, but mark them differently from external linkage\" method.\n\n========================================\n\nCode:\n```text\nint f() { \n    static int x; // no linkage\n}\n```\n\n```text\nstatic int x;  // a namespace scope, so `x` has internal linkage\n\nint f() { \n    extern int x; // declaration in one scope\n}\n\nint g() { \n    extern int x; // declaration in another scope\n}\n```\n\n```text\n#include <iostream>\n\nstatic int x; // a namespace scope, so `x` has internal linkage\n\nint f()\n{\n    extern int x;\n    ++x;\n}\n\nint g()\n{\n    extern int x;\n    std::cout << x << '\\n';\n}\n\nint main() {\n    g();\n    f();\n    g();\n}\n```\n\n```text\n0\n1\n```\n\n```text\nmovl    _ZL1x(%rip), %eax\n    addl    $1, %eax\n    movl    %eax, _ZL1x(%rip)\n```\n\n```text\nmovl    _ZL1x(%rip), %eax\n    [...]\n    call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c@PLT\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\nf()\n```\n\n```text\ng()\n```\n\n```text\nx\n```\n\n```text\nstatic\n```\n\n```text\nx\n```\n\n```text\nf()\n```\n\n```text\nx\n```\n\n```text\ng()\n```\n\n```text\nx\n```\n\n```text\nf()\n```\n\n```text\ng()\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\nf()\n```\n\n```text\ng()\n```\n\n```text\nx\n```\n\n```text\n_ZL1x\n```\n\n```text\nx\n```\n\n```text\nstd::cout\n```\n\n```text\n_ZL1x\n```\n\n```text\nf\n```\n\n```text\nf\n```\n\n```text\ng\n```\n\n```text\n.global\n```\n\n```text\n.local\n```\n\n```text\n.global\n```\n\n```text\n.local\n```\n\n```text\nnm\n```\n\n```text\n.o\n```\n\n```text\nT\n```\n\n```text\nD\n```\n\n```text\nt\n```\n\n```text\nd\n```\n\n```text\nstatic void internal(void);\n\n// ...\n\nstatic void internal(void) {\n    // do something\n}\n```\n\n```text\nstatic\n```\n\n========================================\n\nComments:\n- The linker does not handle variables with internal or no linkage. The compiler can generate code to access those types of variables without any help from the linker. (Linkage and linker are not closely related concepts.)","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":43,"totalLines":246,"estimatedTokens":1081}}974{"id":"stack-36514063","source":"stackoverflow","questionId":36514063,"title":"In computer science, what is NOT a formal language?","tags":["math","compiler-construction","computer-science"],"text":"Title: In computer science, what is NOT a formal language?\nTags: math, compiler-construction, computer-science\nSource: Stack Overflow\n\nQuestion:\nOn the wikipedia https://www.wikiwand.com/en/Formal_language, I found the definition of a formal language:\n\n In mathematics, computer science, and linguistics, a formal language\n is a set of strings of symbols that may be constrained by rules that\n are specific to it.\n\nThis looks quite abstract to me. And I can't image any language which doesn't fit to this definition. Does anyone have ideas about what a **informal language** looks like and how it doesn't fit the definition?\n\n========================================\n\nTop Answer:\nEnglish isn't a formal language. It's not just a set of strings; it has a spoken form, and evolution over time, and dialects, and all sorts of other things a formal language doesn't have. A formal language couldn't gain the word \"email\" from one decade to the next.\n\n========================================\n\nCode:\n```text\n(), ()(), ((()))()(()), ...\n```\n\n```text\nk\n```\n\n```text\n1\n```\n\n========================================\n\nComments:\n- I'm voting to close this question as off-topic because it is not about programming within the limits of the application of that term here on SO.\n- @HighPerformanceMark, that's too bad because in my opinion there is a major shortage of interesting questions on SO lately.\n- You can't awlays describe a formal language using context-free grammars... you sometimes need more powerful machinery.\n- I never said that the left hand side of the production system must be a single symbol. It can even be epsilon if you wish.\n- Ok, let `T` be the set of all deterministic Turing machines that always halt after a finite number of steps (on every input). Give me a description. I'll wait. :)\n- When you say you'll wait do you mean you aren't even going to eat :)? I added 'sometimes' to the statement on formalisability to reflect that not all sets are amenable.\n- I just want to stress that a finite alphabet [for a language] may yet correspond to an infinite language. For example, the grammar consisting of `S -> Sa` defines a language that consists of an infinite set of strings (each a sequence of `a`'s of unique, potentially infinite length), by definition an infinite language. Yet the alphabet is finite, consisting solely of [the symbol] `a`. None of this invalidates any of your answer, it's just that I myself had to refresh the concepts in my head and imagined someone else may find this useful too.","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":43,"estimatedTokens":631}}975{"id":"stack-36482846","source":"stackoverflow","questionId":36482846,"title":"Is there a practical reason for a compiler to be self-hosting?","tags":["compiler-construction","bootstrapping"],"text":"Title: Is there a practical reason for a compiler to be self-hosting?\nTags: compiler-construction, bootstrapping\nSource: Stack Overflow\n\nQuestion:\nIf your compiler in the bootstrap language works well and is maintainable, why change it? Go, for example, re-wrote its compiler to be self-hosting in version 1.5, which caused compile times to become much slower: an obvious detriment when Go's goal is fast compilation.\n\n========================================\n\nCode:\n```text\nGo\n```\n\n```text\nRite of Passage\n```\n\n========================================\n\nComments:\n- en.wikipedia.org/wiki/Eating_your_own_dog_food\n- Any external tool also has its own versioning issues, and the community effects are very noticable, not just in the developers, but even more so in the circle directly outside developers. The knowledgable users that suddenly need dual (or triple) language skills to isolate a bug and create a thorough report. (triple if the runtime library is written in yet another language)","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":23,"estimatedTokens":248}}976{"id":"stack-5372153","source":"stackoverflow","questionId":5372153,"title":"Assembly language output in a C++ compiler","tags":["c++","compiler-construction","assembly"],"text":"Title: Assembly language output in a C++ compiler\nTags: c++, compiler-construction, assembly\nSource: Stack Overflow\n\nQuestion:\nDoes a C++ compiler generate machine code via assembly language code (i.e., c++ compiler first converts C++ code to assembly language code and then uses assembly language compiler to convert it to machine code), or is assembly language output generation just an option for reference or debugging purposes?\n\n========================================\n\nTop Answer:\nThey used to, a long time ago, although that was typical only for C compilers. The first one I used worked that way, a long time ago. Not unusual for ones that generated code for unusual hardware and operating systems, they saved the cost of writing the object file generator and leveraged existing linkers.\n\nModern compilers don't bother with the extra step of generating machine code as text and running an assembler afterward, they generate the machine code directly in binary form. The compile speed advantage is fairly significant, text isn't cheap. The option to generate it in textual form from binary is pretty simple to implement.\n\n========================================\n\nCode:\n```text\n-S\n```\n\n```text\n/FAs\n```\n\n========================================\n\nComments:\n- An \"assembly language compiler\" is called simply an *assembler*.\n- Yup, and that way any optimizations implemented at the assembly language level can benefit multiple languages, and sometimes even code written in a mixture of languages.\n- @Ben: Assemblers don't optimize much, if anything. About each compiler has some (often source language independent) intermediate language though, which is indeed subject to many optimizations and is sometimes quite similar to machine-agnostic assembly code (e.g. LLVM).\n- thanks for your reply. does someone know if JAVA/.NET JIT compilers also emit an assembly language, or directly the machine code?\n- @quant: they emit an intermediary language. Named IL in .NET, bytecode in Java. Its translated to machine code on the target machine by a just-in-time compiler. Imagine the assembly language of a fictional CPU. You can buy CPU cores that execute Java bytecode directly so it's not *that* fictional.","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":33,"estimatedTokens":552}}977{"id":"stack-4735543","source":"stackoverflow","questionId":4735543,"title":"Tokenizer efficiency question","tags":["c++","parsing","compiler-construction","tokenize"],"text":"Title: Tokenizer efficiency question\nTags: c++, parsing, compiler-construction, tokenize\nSource: Stack Overflow\n\nQuestion:\nI'm writing a compiler front end for a project and I'm trying to understand what's the best method of tokenize the source code.\nI can't choose between two ways:\n\n1) the tokenizer read all tokens:\n\n```\nbool Parser::ReadAllTokens()\n{\n Token token;\n while( m_Lexer->ReadToken( &token ) )\n {\n m_Tokens->push_back( token );\n token.Reset(); // reset the token values..\n }\n\n return !m_Tokens->empty();\n}\n```\n\nand then the parsing phase begins, operating on the m_`Tokens` list. In this way the methods getNextToken(), peekNextToken() and ungetToken() are relatively easy to implement by iterator, and the parsing code is well written and clear ( not broken by getNextToken() i.e. : \n\n```\ngetNextToken();\n useToken();\n getNextToken();\n peekNextToken();\n if( peeked is something )\n ungetToken();\n ..\n ..\n```\n\n)\n\n2) the parsing phase begins and when needed, the token is created and used ( the code seems not so clear )\n\nWhat's the best method??and why??and the efficiency??\nthanks in advance for the answers\n\n========================================\n\nTop Answer:\nIt would be better to use something like Boost::Spirit to tokenise. Why reinvent the wheel?\n\n========================================\n\nCode:\n```text\nbool Parser::ReadAllTokens()\n{\n  Token token;\n  while( m_Lexer->ReadToken( &token ) )\n  {\n    m_Tokens->push_back( token );\n    token.Reset(); // reset the token values..\n  }\n\n  return !m_Tokens->empty();\n}\n```\n\n```text\ngetNextToken();\n useToken();\n getNextToken();\n peekNextToken();\n if( peeked is something )\n  ungetToken();\n ..\n ..\n```\n\n```text\nTokens\n```\n\n========================================\n\nComments:\n- There would be little or no difference for the actual parser, and the lexer would be marginally more complex. Marginally. Using buffered streams shouldn't be a mystery to any programmer, and thus peek() (or whatever your stream mechanism's preferred name is) should be quite easy to remember, even years from now...","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":85,"estimatedTokens":514}}978{"id":"stack-6396738","source":"stackoverflow","questionId":6396738,"title":"How to trace error of OCaml programs?","tags":["debugging","compiler-construction","emacs","ocaml"],"text":"Title: How to trace error of OCaml programs?\nTags: debugging, compiler-construction, emacs, ocaml\nSource: Stack Overflow\n\nQuestion:\nI am writing a compiler written in OCaml. Sometimes when there is an error of execution, it shows the line of error, but it does not show the context, for instance, how the function is called, with which values...\n\nIn order to help debugging, does anyone know a way to show the steps of execution till the error with real value of the relevant variables?\n\nBy the way, I am using Emacs as editor.\n\n========================================\n\nCode:\n```text\nocamlc -g -o foo foo.ml\nexport OCAMLRUNPARAM=b\n./foo\n```\n\n```text\n-g\n```\n\n```text\nOCAMLRUNPARAM\n```\n\n```text\nb\n```\n\n========================================\n\nComments:\n- Thank you very much, that works. Do you know what is the value of `OCAMLRUNPARAM` by default so that I could set it back from time to time?\n- @SoftTimur: I'm not sure if that's what you're asking, but the default value of `OCAMLRUNPARAM` is empty (i.e. do nothing special).\n- OK... Thank you... I just created another thread about traceback in Emacs: traceback\n- Link no longer points to relevant info, I think this may be the current equivalent: ocaml.org/manual/runtime.html#s:ocamlrun-options","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":39,"estimatedTokens":313}}979{"id":"stack-62603729","source":"stackoverflow","questionId":62603729,"title":"Java compiler optimizations with final local variables","tags":["java","variables","methods","compiler-construction","final"],"text":"Title: Java compiler optimizations with final local variables\nTags: java, variables, methods, compiler-construction, final\nSource: Stack Overflow\n\nQuestion:\nI always thought final keyword has no effect, performancewise, on local method variables or parameters. So, I tried to test the following code and it seems I was wrong:\n\n```\nprivate static String doStuffFinal() {\n final String a = \"A\";\n final String b = \"B\";\n final int n = 2;\n return a + b + n;\n}\n\nprivate static String doStuffNotFinal() {\n String a = \"A\";\n String b = \"B\";\n int n = 2;\n return a + b + n;\n}\n```\n\nI checked the bytecode and they are not the same for these 2 methods. Decompiled code in idea looks like this:\n\n```\nprivate static String doStuffFinal() {\n String a = \"A\";\n String b = \"B\";\n int n = 2;\n return \"AB2\";\n}\n\nprivate static String doStuffNotFinal() {\n String a = \"A\";\n String b = \"B\";\n int n = 2;\n return a + b + n;\n}\n```\n\nWhy is there a difference between these 2 methods? Can't javac optimize such a trivial case? The compiler could see that a, b and n don't change in doStuffNotFinal and optimize the code in the same way. Why doesn't that happen?\n\nMore importantly, does that mean we'd better put the final keyword all over the place just to be sure to get the best optimizations?\n\n========================================\n\nCode:\n```text\nprivate static String doStuffFinal() {\n    final String a = \"A\";\n    final String b = \"B\";\n    final int n = 2;\n    return a + b + n;\n}\n\nprivate static String doStuffNotFinal() {\n    String a = \"A\";\n    String b = \"B\";\n    int n = 2;\n    return a + b + n;\n}\n```\n\n```text\nprivate static String doStuffFinal() {\n    String a = \"A\";\n    String b = \"B\";\n    int n = 2;\n    return \"AB2\";\n}\n\nprivate static String doStuffNotFinal() {\n    String a = \"A\";\n    String b = \"B\";\n    int n = 2;\n    return a + b + n;\n}\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\nn\n```\n\n```text\ndoStuffFinal()\n```\n\n```text\ndoStuffNotFinal\n```\n\n```text\na + b\n```\n\n```text\na + b + n\n```\n\n```text\na + b + n\n```\n\n```text\nfinal\n```\n\n========================================\n\nComments:\n- @AndyTurner, yes they are, in both cases. Generated bytecode is different though\n- by reading the link you provided, I understand that the variables in `doStuffNotFinal()` are also constant variables: A local variable ... is effectively final if all of the following are true: It is not declared final. It never occurs as the left hand side in an assignment expression It never occurs as the operand of a prefix or postfix increment or decrement operator (&#167;15.14, &#167;15.15).\n- The spec says \"A constant variable is a `final` variable\", not \"A constant variable is a `final` or effective-final variable\". It also says \"Certain variables that are not declared `final` **are instead** considered effectively final\" (emphasis mine), making it clear that `final` and effectively final are different things.","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":11,"totalLines":122,"estimatedTokens":721}}980{"id":"stack-13440329","source":"stackoverflow","questionId":13440329,"title":"Can a Java Compiler or JVM swap instruction order of independent instructions?","tags":["java","compiler-construction","jvm","instructions"],"text":"Title: Can a Java Compiler or JVM swap instruction order of independent instructions?\nTags: java, compiler-construction, jvm, instructions\nSource: Stack Overflow\n\nQuestion:\nLet's take following statements:\n\n```\nint d0, d1;\nint[] ds = {0, 0};\n```\n\nNow one thread has following instructions:\n\n```\nd0++;\nd1++;\n```\n\nwhile the other thread has this instruction:\n\n```\nds[1] = d1;\nds[0] = d0;\n```\n\nIf we run these threads in parallel, there are obviously three combinations that `ds` can look like: {0, 0}, {1, 1}, and {1, 0}.\n\nNow the big question is: Can there also be {0, 1}? Can the Compiler/JVM simply swap instruction because it thinks they are unrelated? If yes, what exactly are the \"rules\" for such behaviour and is it up to the compiler or the JVM?\n\n========================================\n\nTop Answer:\nIn the absence of proper synchronization, this is indeed possible.\n\nThe Java Language Specification defines the semantics of multi-threaded Java programs in chapter 17. That chapter is rather hard to understand, but it does contain the official rules one can rely on. In particular, it writes:\n\nA memory model describes, given a program and an execution trace of that program, whether the execution trace is a legal execution of the program. The Java programming language memory model works by examining each read in an execution trace and checking that the write observed by that read is valid according to certain rules.\n\nThe memory model describes possible behaviors of a program. An implementation is free to produce any code it likes, as long as all resulting executions of a program produce a result that can be predicted by the memory model.\n\nTo give a rough overview, the memory model defines a happens-before relation any reorderings must be consistent with. The usual way to establish happens-before for actions performed by different threads is to synchronize these actions, for instance with a `synchronized` block or writing to or reading from a volatile variable.\n\nIn the absence of such synchronization, the runtime will execute threads independently, permitting any reordering the *current* thread can not observe.\n\nThat is, if you have mutable shared state, you'll usually need to synchronize the threads accessing it.\n\n========================================\n\nCode:\n```text\nint d0, d1;\nint[] ds = {0, 0};\n```\n\n```text\nd0++;\nd1++;\n```\n\n```text\nds[1] = d1;\nds[0] = d0;\n```\n\n```text\nds\n```\n\n```text\n{0, 1}\n```\n\n```text\nsynchronized\n```\n\n========================================\n\nComments:\n- Usually optimizations are considered safe if they behave the same as the unoptimized code in a serial (iow, one thread) model. Unless the relevant language constructs have explicit provisions for threading","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":81,"estimatedTokens":680}}981{"id":"stack-2325824","source":"stackoverflow","questionId":2325824,"title":"Haskell: syntax error when adding new line in pattern matching","tags":["parsing","haskell","compiler-construction"],"text":"Title: Haskell: syntax error when adding new line in pattern matching\nTags: parsing, haskell, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nBasically I'm modifying a parser to handle additional operators. Before my changes, one part of the parser looked like this:\n\n```\nparseExpRec e1 (op : ts) = \n let (e2, ts') = parsePrimExp ts in\n case op of\n T_Plus -> parseExpRec (BinOpApp Plus e1 e2) ts'\n T_Minus -> parseExpRec (BinOpApp Minus e1 e2) ts'\n T_Times -> parseExpRec (BinOpApp Times e1 e2) ts'\n T_Divide -> parseExpRec (BinOpApp Divide e1 e2) ts'\n _ -> (e1, op : ts)\n```\n\nT_Plus etc. are members of the Token datatype, and Plus, Minus etc. are part of BinOp which BinOpApp applies to two operands. I have updated the Token and BinOpApp datatypes to handle the Power (exponentiation) token. This is the resulting code:\n\n```\nparseExpRec e1 (op : ts) = \n let (e2, ts') = parsePrimExp ts in\n case op of\n T_Plus -> parseExpRec (BinOpApp Plus e1 e2) ts'\n T_Minus -> parseExpRec (BinOpApp Minus e1 e2) ts'\n T_Times -> parseExpRec (BinOpApp Times e1 e2) ts'\n T_Divide -> parseExpRec (BinOpApp Divide e1 e2) ts'\n T_Power -> parseExpRec (BinOpApp Power e1 e2) ts'\n _ -> (e1, op : ts)\n```\n\nThis seems simple but it's now giving the following error:\n\n TXL.hs:182:13: parse error on input '->'\n\nLine 182 is the line where I added \"T_Power -> parseExpRec...\" - I don't see how it's any different from the other lines, which parse fine. I'm using GHCi as my environment.\n\n========================================\n\nTop Answer:\nThis is almost with 100% certainty an indentation error. I've had similar problems in the past, also when writing a parser. What's probably happened is the lines before the problematic line are indented with tabs, and you've used spaces on the T_Power line (or something similar). Can you turn on non-printed characters in your editor?\n\n========================================\n\nCode:\n```text\nparseExpRec e1 (op : ts)  = \n let (e2, ts') = parsePrimExp ts in\n   case op of\n     T_Plus    ->  parseExpRec (BinOpApp Plus   e1 e2) ts'\n     T_Minus   ->  parseExpRec (BinOpApp Minus  e1 e2) ts'\n     T_Times   ->  parseExpRec (BinOpApp Times  e1 e2) ts'\n     T_Divide  ->  parseExpRec (BinOpApp Divide e1 e2) ts'\n     _         ->  (e1, op : ts)\n```\n\n```text\nparseExpRec e1 (op : ts)  = \n let (e2, ts') = parsePrimExp ts in\n   case op of\n     T_Plus    ->  parseExpRec (BinOpApp Plus   e1 e2) ts'\n     T_Minus   ->  parseExpRec (BinOpApp Minus  e1 e2) ts'\n     T_Times   ->  parseExpRec (BinOpApp Times  e1 e2) ts'\n     T_Divide  ->  parseExpRec (BinOpApp Divide e1 e2) ts'\n     T_Power   ->  parseExpRec (BinOpApp Power  e1 e2) ts'\n     _         ->  (e1, op : ts)\n```\n\n```text\nscanner (’^’ : cs) = T_Power : scanner cs\n```\n\n========================================\n\nComments:\n- don't know anything about Haskell, but is `T_Power` defined somewhere?\n- T_Power is defined in the same type as T_Plus etc and Power is in the same type as Plus, Minus etc.","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":78,"estimatedTokens":743}}982{"id":"stack-2826541","source":"stackoverflow","questionId":2826541,"title":"What does it mean for a language to be ‘interpreted’?","tags":["ruby","compiler-construction","programming-languages","interpreter","interpreted-language"],"text":"Title: What does it mean for a language to be ‘interpreted’?\nTags: ruby, compiler-construction, programming-languages, interpreter, interpreted-language\nSource: Stack Overflow\n\nQuestion:\nDo languages like e.g. Ruby (if running MRI, I mean not compiled to byte-code) run actually parsed everytime when an execution of, e.g., method or loop body is needed? I mean, to execute a loop, you need to parse its body *N* times?\n\nI just always thought that all these programs are being parsed one time at the bootstrap, transformed in a ‘strongly-typed’ statements tree, etc. Is that not true?\n\n========================================\n\nTop Answer:\nInterpreted is a word with a very loose definition. Even machine code instructions are interpreted by the processor.\n\nIn general a distinction is made between languages which are compiled before they are run and languages which do not have a compilation process and are run inside another program, called the interpreter. The latter types of languages are often referred to as interpreted languages.\n\nThe line is not that clear in some cases:\n\n- Some languages can be either compiled or interpreted, e.g. PHP.\n\n- Some interpreted code might be compiled at runtime into native machine instructions (JIT compilation).\n\n- Some compiled languages may have an `exec` functionality which allows code to be generated and executed at run-time, bypassing the normal compilation process.\n\n========================================\n\nCode:\n```text\nexec\n```\n\n========================================\n\nComments:\n- \" they use some intermediate representation (you can see it nicely when you want to write C-extensions for it) \" - assuming it is stored in-memory, what could this representation look alike?\n- For the original implementation written in C, it's a series of struct's called \"VALUE\". Here's a little insight: rubyinside.com/&hellip;\n- \"While not using bytecode in the original implementation (or did they migrate already?)\" The official ruby interpreter uses bytecode since version 1.9.","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":37,"estimatedTokens":506}}983{"id":"stack-73713205","source":"stackoverflow","questionId":73713205,"title":"How does AVR support with LLVM work for different languages?","tags":["compiler-construction","llvm","d","avr","ldc"],"text":"Title: How does AVR support with LLVM work for different languages?\nTags: compiler-construction, llvm, d, avr, ldc\nSource: Stack Overflow\n\nQuestion:\nApologies in advance since I am not too knowledgeable on the topic yet and still in the process of learning about the inner workings of compilers.\n\nI am currently investigating the possibility of using D in embedded systems and found an article mentioning how the AVR backend has been merged into the upstream LLVM Project, so developers can work on creating AVR support for their languages.\nD has the LDC compiler, which uses the LLVM backend. The article also mentions the use of the avr-gcc though, which leaves me a bit confused about which tools are used at which stage to go from D-sourcecode to an AVR binary. I would have assumed LDC converts the sourcecode to LLVM IR and then to binary, so I'm not sure what the avr-gcc is used for.\n\nCan anyone explain this to me in more details perhaps?\n\n========================================\n\nCode:\n```text\n$ PATH=/opt/arduino/arduino-1.8.10/hardware/tools/avr/bin:$PATH ldc2 -betterC -Oz -mtriple=avr -mcpu=atmega328p -gcc=avr-gcc delay.d\n$ file delay\ndelay: ELF 32-bit LSB executable, Atmel AVR 8-bit, version 1 (SYSV), statically linked, with debug_info, not stripped\n$ ls -l delay\n-rwxr-xr-x 1 me users 2440 Sep 18 08:28 delay\n```\n\n```text\n$ ldc2 -betterC -Oz -mtriple=avr -mcpu=atmega328p --link-internally delay.d\nlld: warning: cannot find entry symbol _start; defaulting to 0x110B4\n$ file delay\ndelay: ELF 32-bit LSB executable, Atmel AVR 8-bit, version 1 (SYSV), statically linked, not stripped\n$ ls -l delay\n-rwxr-xr-x 1 me users 708 Sep 18 08:31 delay\n```\n\n```text\n-gcc=avr-gcc\n```\n\n```text\n--link-internally\n```\n\n```text\n-gcc\n```\n\n```text\n_start\n```\n\n```text\nextern(C) main\n```\n\n========================================\n\nComments:\n- avr-gcc is another toolchain for AVR that has existed for a long time, using GCC rather than LLVM infrastructure. They are not related.","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":7,"totalLines":56,"estimatedTokens":495}}984{"id":"stack-8375391","source":"stackoverflow","questionId":8375391,"title":"Common tokens for flex and bison","tags":["c","compiler-construction","bison","flex-lexer"],"text":"Title: Common tokens for flex and bison\nTags: c, compiler-construction, bison, flex-lexer\nSource: Stack Overflow\n\nQuestion:\nI have one file with declarations of my tokens declarations.h:\n\n```\n#define ID 257\n#define NUM 258\n...\n```\n\nIn my flex code i return one of this values or symbol(for example '+', '-', '*'). And everything works.\n\nThe problem in bison file.\nIf i write something like that:\nexp: ID '+' ID\ni'll get error, because bison doesn't know anything about ID. \nAdding the line %token ID will not help, because in that case i'll have compilation error(preprocessor will change ID by 257 and i'll get 257=257)\n\n========================================\n\nCode:\n```text\n#define ID 257\n#define NUM 258\n...\n```\n\n```text\nbison -d grammar.y\n# Generates grammar.tab.c and grammar.tab.h\n```\n\n```text\n$ cat grammar.y\n%token ID\n%%\nprogram:    /* Nothing */\n    |       program ID\n    ;\n%%\n$ cat lexer.l\n%{\n#include \"grammar.tab.h\"\n%}\n%%\n[a-zA-Z][A-Za-z_0-9]+   { return ID; }\n[ \\t\\n]                 { /* Nothing */ }\n.                       { return *yytext; }\n%%\n$ bison -d grammar.y\n$ flex lexer.l\n$ gcc -o testgrammar grammar.tab.c lex.yy.c -ly -lfl\n$ ./testgrammar\nid est\nquod erat demonstrandum\n$\n```\n\n```text\ngrammar.tab.h\n```\n\n```text\nenum\n```\n\n```text\n#define\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":72,"estimatedTokens":318}}985{"id":"stack-5451683","source":"stackoverflow","questionId":5451683,"title":"How are compilation and ABI related?","tags":["compiler-construction","abi"],"text":"Title: How are compilation and ABI related?\nTags: compiler-construction, abi\nSource: Stack Overflow\n\nQuestion:\nHow are compilation and ABI related?\n\nIs a compiler's solely job to build Application Binary Interface (ABI) to OS and/or other applications?\n\nAbout ABI, quoted from Wikipedia:\n\n ABIs cover details such as data type,\n size, and alignment; the calling\n convention, which controls how\n functions' arguments are passed and\n return values retrieved; the system\n call numbers and how an application\n should make system calls to the\n operating system; and in the case of a\n complete operating system ABI, the\n binary format of object files, program\n libraries and so on.\n\nThanks and regards!\n\n========================================\n\nTop Answer:\nWell, compiler may not the ABI completely. It had to emit valid machine code but for example it may not calling conventions for inner function calls. But of course, on the border between compiled code and OS the compiler should ABI.\n\n========================================\n\nComments:\n- Thanks! So ABI is some specification belonging to OS, not to anything else including CPU and its instruction set, compiler, the language in which the source code is written in, and the program/executable?\n- Yes. It's fixed for the OS. An individual program does not contribute to it at all -- it only obeys it.\n- Thanks. I just added to my previous comments \"not (belong) to anything else including CPU and its instruction set, compiler, the language in which the source code is written in, and the program/executable?\".\n- CPU/instruction set would be considered a part of the ABI; the programming language would not, as the ABI is an interface used by all programs on a given system.\n- Thanks! If a compiler does not ABI of a system completely, will the code compiled by it on the system be runnable on the system?\n- Depends on what you mean saying \"completely\". If compiled application doesn't conform the calling conventions at all even if calling library functions then calling such function will cause some error. Another case is if application doesn't conform to the executable/library format. That means the application will not run because loader fail loading such component. In other words, if compiled application doesn't ABI in places where it interacts with OS this will cause problems. It could be runnable but not working. Or working but not absolutely.","metadata":{"transformedAt":"2026-08-18T18:32:17.845Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":39,"estimatedTokens":602}}986{"id":"stack-19106622","source":"stackoverflow","questionId":19106622,"title":"Compiler: limitation of lexical analysis","tags":["c++","parsing","compiler-construction"],"text":"Title: Compiler: limitation of lexical analysis\nTags: c++, parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn classic Compiler theory, the first 2 phases are Lexical Analysis and Parsing. They're in a pipeline. Lexical Analysis recognizes tokens as the input of Parsing. \n\nBut I came across some cases which are hard to be correctly recognized in Lexical Analysis. For example, the following code about C++ template:\n\n`map>`\n\nthe `>>` would be recognized as bitwise right shift in a \"regular\" Lexical Analysis, but it's not correct. My feeling is it's hard to divide the handling of this kind of grammars into 2 phases, the lexing work has to be done in the parsing phase, because correctly parsing the `>>` relies on the grammar, not only the simple lexical rule.\n\nI'd like to know the theory and practice about this problem. Also, I'd like to know how does C++ compiler handle this case?\n\n========================================\n\nTop Answer:\nYou are right, the theoretical clean distinction between lexer and parser is not always possible. I remember a porject I worked on as a student. We were to implement a C compiler, and the grammar we used as a basis would treat typedefined names as types in some cases, as identifiers in others. So the lexer had to switch between these two modes. The way I implemented this back then was using special empty rules, which reconfigured the lexer depending on context. To accomplish this, it was vital to know that the parser would *always* use exactly one token of look-ahead. So any change to lexer behaviour would have to occur at least one lexiacal token before the affected location. In the end, this worked quite well.\n\nIn the C++ case of `>>` you mention, I don't know what compilers actually do. willj quoted how the specification phrases this, but implementations are allowed to do things differently internally, as long as the visible result is the same. So here is how I'd try to tackle this: upon reading a `>`, the lexer would emit token `GREATER`, but also switch to a state where each subsequent `>` *without a space in between* would be lexed to `GREATER_REPEATED`. Any other symbol would switch the state back to normal. Instead of state switches, you could also do this by lexing the regular expression `>+`, and emitting multiple tokens from this rule. In the parser, you could then use rules like the following:\n\n```\nrightAngleBracket: GREATER | GREATER_REPEATED;\nrightShift: GREATER GREATER_REPEATED;\n```\n\nWith a bit of luck, you could make template argument rules use rightAngleBracket, while expressions would use rightShift. Depending on how much look-ahead your parser has, it might be neccessary to introduce additional non-terminals to hold longer sequences of ambiguous content, until you encounter some context which allows you to eventually make the decision between these cases.\n\n========================================\n\nCode:\n```text\nmap<int, vector<int>>\n```\n\n```text\n>>\n```\n\n```text\n>>\n```\n\n```text\n>\n```\n\n```text\n=\n```\n\n```text\n>>\n```\n\n```text\n>\n```\n\n```text\n>>\n```\n\n```text\n>\n```\n\n```text\n<\n```\n\n```text\n<\n```\n\n```text\n>\n```\n\n```text\n>>\n```\n\n```text\n>\n```\n\n```text\n>\n```\n\n```text\n<\n```\n\n```text\n<\n```\n\n```text\n<\n```\n\n```text\n>\n```\n\n```text\n>>\n```\n\n```text\n>\n```\n\n```text\nrightAngleBracket: GREATER | GREATER_REPEATED;\nrightShift: GREATER GREATER_REPEATED;\n```\n\n```text\n>>\n```\n\n```text\n>\n```\n\n```text\nGREATER\n```\n\n```text\n>\n```\n\n```text\nGREATER_REPEATED\n```\n\n```text\n>+\n```\n\n========================================\n\nComments:\n- I dont know for sure, but I always assumed they had some special case code for this particular occurrence. ie something like `if(input == \">>\") check_for_bitwise_right; if(!bitwise_right) check_for_double_template_close;` But I have nothing to back this up.\n- Seems like this should be covered by any compiler course or textbook.\n- I think that many (pre-C++0x) *do* interpret it as bitwise right shift, actually.\n- This is a special case, it's explicitly mentioned by the standard since it's so annoying\n- What's wrong with lexing \">>\" as '>' '>' and letting the parser sort it out?\n- @IraBaxter You forget the left hand side: `map<int, vector<int`\n- @nightcracker: What about the left hand side? The parser can propose \"id \" as one parse, and \"id > ...\" as another. This requires some ability to look ahead (C++ must look arbitrarily far ahead to resolve this) and most \"standard\" pure parsing mechanisms (LL(1), LALR(k)) can't do it. GLR has no problem with us. (We build a C++11 parser that works this way, just fine.) But complaining about weak parsers is just that: a complaint about using the wrong technology to solve a problem. (I believe Clang has some funny lookahead hack).\n- Lexing used to be defined as returning the longest match ie '+=' instead of '+' and '=', and standard automata does exactly that, so you suggestion would break that rule. But of course, there is nothing wrong with rewriting the tree during parsing. Also, viewing the parser as a function P : [Token] -> Tree, list of tokens to tree, it seems conceptually simpler to take out the '>>' as one token and push the second '>' back as opposed to look at two to see if you can 'fuse' them.\n- @user1666959: If you don't define a token \"+=\", you don't have find a longest match for it. Similarly for the non-token (if you care to define it that way) \">>\". You can try to bend the parsing machinery to rewrite \">>\" as \">\", but it doesn't want to do that. Simply processing \">>\" as \">\" \">\" makes it possible to solve all these problems by writing straightforward grammar rules. Why make it hard and confusing?\n- @IraBaxter It's a good idea that lex \">>\" as '>' '>' and let the parser sort it out. In theory, we can pass all the letters directly to the parser. Parser has the full grammar info to decide what to do, right?\n- @Todd: In theory, you can parse at the character level. (Some tools actually do that; go find out about \"scannerless parsers\"). As a practical technique, you want the lexer to collect \"as big a chunk\" as practical to maximize the value of lightning-fast FSA processing of characters vs. somewhat slower parsing transitions per character. (My limited experience indicates scannerless parsers are significantly slower). But you can decide how big a lexeme is, on a per-lexeme basis. In this case, having a \">>\" lexeme just causes grief. \"Doctor, doctor, it hurts when I do X\". Doctor: \"Dont do X\".\n- @IraBaxter: there are some problems with this approach. You need to handle `>>` and `> >` differently. The latter is OK in the template context, but is an error otherwise. You also need to handle `>>=`. All is doable if you try hard enough, but why bother? The C++ lexer (and the C lexer) needs feedback from the parser anyway, on a very fundamental level, because of the ambiguity between names, type names and template names.\n- @n.m. Easy enough; you can add a semantic check to the grammar rule \"shift_operator = '>' '>' ; \" which checks that there's no intervening whitespace. And no, C and C++ parsers do not NECESSARILY need feedback between lexer and parser; one can build real, production parsers without it. See my SO discussion of this: stackoverflow.com/a/4173543/120163\n- See stackoverflow.com/a/15785583/192359\n- Asaf, Thanks for your information. I think it's the same idea as that of @IraBaxter, lex \">>\" as '>' '>', then handle it in parsing phase.\n- @IraBaxter: yes, one can, but traditionally it's done with feedback, it's easier that way.\n- @n.m.: \"Its easier that way...\" Well, our experience with our approach is pretty good. We have a very small team and a very good front end made possible by the choice to explicitly separate parsing and name resolution issues. The other front ends I know about (GCC and EDG) have gone through conniptions over the years, including GCC throwing parsing with Bison off a cliff and EDG having always hand-built their parsers. These surely can't be \"easier\" than building a parser directly from grammar rules. Your opinion may vary.\n- I like the idea of making lexing and parsing as 2 phases of a pipeline. This model is much simpler to understand. Mixing them with feedback complicates the model.\n- @IraBaxter: that's certainly an interesting approach, but don't you have to deal with far more grammar ambiguities this way?\n- @n.m.: the parser has to handle ambiguities, which GLR does with aplomb. We use attribute grammars to walk the parse tree (with ambiguity nodes) and perform name resolution. Anyplace the name resolution decides that a particular child tree of an ambiguous node isn't sanely typeable, it simply deletes that child tree. The remaining tree is the typed version of the program with the ambiguities gone. This is clean and sane and thus emininently engineerable. The attribute grammars make it even almost pretty. ...\n- ... An ideal solution would, using program transformations, compose the attribute grammars (at least the left-to-right part :) with parsing process, giving something like the \"parser feedback hack\" implemented in a really clean way.\n- @IraBaxter Ah, attribute grammars, that's nice. I should try them one day. But I don't think they teach them in Compilers 101 or whatever the OP is taking. I'm not sure an undergrad student would find them easier to understand than the feedback hack...\n- I'm not sure an undergrad would understand (or should need to understand) the need for the feedback hack in C++. He should be learning the basics of building a parser, not the dark corners of hacks needed to make C++ work. My push for GLR grammars and simple token schemes means he can focus on learning about grammars and parsers, and leave all the dark corner trash for when he really needs it.\n- GLR parsing probably deserves a mention, at least from a theoretical perspective. It's not used by any production-ready compiler that I know of. I'm also unaware of a GLR parser for C++ that beats the performance of existing hand-coded parsers (e.g. GCC/Clang). I would love to be enlightened!\n- For example, marking a `<` token as the beginning of a *template-argument-list* may be achieved by changing its token-id from `LESS_THAN` to `LEFT_ANGLE_BRACKET`, and defining *template-argument-list* in terms of `LEFT_ANGLE_BRACKET`.\n- Our GLR parser doesn't beat the hand-coded ones in speed. But we're not building a compiler; we're building a program transformation system, and optimizing on parse speed hasn't been a real need. There is plenty of work showing how to make L(AL)R parsers blisteringly fast, and most of the GLR states act like L(AL)R states. People have even build GLR parsers the act L(ALR) where the states are L(AL)R. So it should be straightforward engineering to build blistering fast GLR parsers for most of the parse states, and the rest shouldn't matter much. Agreed, nobody has done that yet.","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":28,"totalLines":174,"estimatedTokens":2707}}987{"id":"stack-10519914","source":"stackoverflow","questionId":10519914,"title":"How can I print out the string \"\\b\" in Python","tags":["python","string","compiler-construction"],"text":"Title: How can I print out the string \"\\b\" in Python\nTags: python, string, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn my compilers class, I decided to write my compiler in Python since I enjoy programming in Python, though I encountering an interesting issue with how characters are printed. The lexer I'm writing requires that strings containing the formfeed and backspace characters be printed to stdout in a very particular way: enclosed in double quotes, and printed as \\f and \\b, respectively. The closest I've gotten:\n\n```\nprint(\"{0!r}\".format(\"\\b\\f\"))\n```\n\nwhich yields\n\n```\n'\\x08\\x0c'\n```\n\nNote the single quotes, and utf8 coding. The same command with two other characters I'm concerned with almost works:\n\n```\nprint(\"{0!r}\".format(\"\\n\\t\"))\n```\n\ngives:\n\n```\n'\\n\\t'\n```\n\nTo be clear, the result (including quotes) that I need to conform to the spec is\n\n```\n\"\\b\\f\"\n```\n\nSimple approaches like finding \\b and \\f and replacing them with \"\\b\" and \"\\f\" don't seem to work...the \"\\\" is just the way Python prints a backslash, so I can never seem to get just \"\\b\\f\" as one might expect.\n\nPlaying with various string encodings doesn't seem to help. I've concluded that I need to write a custom string.Formatter, but I was wondering if there is another approach that I'd missed.\n\nEDIT: Thanks for all the answers. I don't think I did that good of a job asking the question though. The underlying issue is that I'm formatting the strings as raw because I want literal newlines to appear as \"\\n\" and literal tabs to appear as \"\\t\". However, when I move to print the string using raw formatting, I lose the ability to print out \"\\b\" and \"\\f\" as all the answers below suggest.\n\nI'll confirm this tonight, but based on these answers, I think the approach I should be using is to format the output normally, and trap all the literal \"\\n\", \"\\t\", \"\\b\", and \"\\f\" characters with escape sequences that will print them as needed. I'm still hoping to avoid using string.Formatter.\n\nEDIT2: The final approach I'm going to use is to use non-raw string formatting. The non-abstracted version looks something like:\n\n```\nprint('\"{0!s}\"'.format(a.replace(\"\\b\", \"\\\\b\").replace(\"\\t\", \"\\\\t\").replace(\"\\f\", \"\\\\f\").replace(\"\\n\",\"\\\\n\")))\n```\n\n========================================\n\nTop Answer:\nUse raw string:\n\n```\n>>> print(r'\\b')\n \\b\n```\n\n========================================\n\nCode:\n```text\nprint(\"{0!r}\".format(\"\\b\\f\"))\n```\n\n```text\n'\\x08\\x0c'\n```\n\n```text\nprint(\"{0!r}\".format(\"\\n\\t\"))\n```\n\n```text\n'\\n\\t'\n```\n\n```text\n\"\\b\\f\"\n```\n\n```text\nprint('\"{0!s}\"'.format(a.replace(\"\\b\", \"\\\\b\").replace(\"\\t\", \"\\\\t\").replace(\"\\f\", \"\\\\f\").replace(\"\\n\",\"\\\\n\")))\n```\n\n```text\nprint(\"{0!r}\".format(\"\\b\\f\".replace(\"\\b\", \"\\\\b\").replace(\"\\f\", \"\\\\f\")))\n```\n\n```text\ndef escape_bs_and_ff(s):\n    return s.replace(\"\\b\", \"\\\\b\").replace(\"\\f\", \"\\\\f\")\n\nprint(\"{0!r}\".format(escape_bs_and_ff(\"\\b\\f\"))\n```\n\n```text\n>>> print(r'\\b')\n    \\b\n```\n\n```text\n>>> print(r'\"\\b\\f\"')\n\"\\b\\f\"\n```\n\n```text\nr\n```\n\n```text\n\\n\n```\n\n```text\n\\n\n```\n\n========================================\n\nComments:\n- You can always define a \"verbatim\" string using `r` just before the string as follows: `print r'\\b\\f'`.\n- It's a classroom language called \"Cool\", designed by the professor Alex Aiken. The whole problem is a bit contrived, since the different parts of the compiler communicate over unix pipes, so the exact formatting of the output matters a whole lot. Obviously, that is an artifact of the classroom setting.\n- Right. But given a string that contains \"\\b\\f\", how do I print it a raw string? I've found that print(r'\"\\b\\f\"') gives the right result, but given a string that is not raw, how would I do it?\n- @R.P.Dillon: You should control this at the time the string is created.\n- @R.P.Dillon check this out code.activestate.com/recipes/&hellip;\n- @StevenRumbalski, it seems to me that the goal is to change the way the string prints out, not the contents of the string. I assume it needs to be the literal characters for other reasons.\n- @MarkRansom That's right, though based on these answers, I think I'm going to have to change the contents of the string, even if only for printing.\n- String *literals* can be raw; there's no difference between a string created with a raw literal and a string created with a non-raw literal. `r'\\b' == '\\\\b'`.\n- I understand the types are the same, but there don't seem to be good semantics for talking about the string \"\\b\" would have been if it had been declared r\"\\b\". In the general case, I suppose I could say \"escaped\", but that's perhaps more general than is useful in this discussion.\n- When I execute the first like you mentioned, I still get the same result: '\\x08\\x0c'. But this answer looks the best because it can accept an existing, non-raw string, which is what I have to deal with.\n- @R.P.Dillon, sorry I had the parentheses in the wrong place. I'm on Python 2.7 so I don't have the same syntax for `print`.","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":133,"estimatedTokens":1231}}988{"id":"stack-6087377","source":"stackoverflow","questionId":6087377,"title":"How JIT Compilers Operate","tags":["compiler-construction","jit"],"text":"Title: How JIT Compilers Operate\nTags: compiler-construction, jit\nSource: Stack Overflow\n\nQuestion:\nJIT compilers, by definition, generate code on the fly for execution. But in, say, Windows, we have all kinds of protection that prevent self modifying code or executing from data memory (DEP).\n\nSo how is it possible for JIT compilers to generate code on the fly?\n\n========================================\n\nCode:\n```text\nmmap()\n```\n\n```text\nPROT_READ | PROT_WRITE | PROT_EXEC\n```\n\n```text\nVirtualAlloc()\n```\n\n```text\nPAGE_EXECUTE_READWRITE\n```\n\n```text\nllvm::sys::Memory::AllocateRWX\n```\n\n========================================\n\nComments:\n- possible duplicate of JIT compilation and DEP (Truly *possible* because this may be meant broader and that question only answers it indirectly)\n- See also this question.\n- Whewt, I already thought I wouldn't find an answer to this. So far I only found \"you can use this for self-modifying code, and that is evil, go away devil!\" But of course, if that were the case, languages like java would be rather slow. :)","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":5,"totalLines":38,"estimatedTokens":264}}989{"id":"stack-16920884","source":"stackoverflow","questionId":16920884,"title":"Custom Scala REPL Issues","tags":["scala","compiler-construction","read-eval-print-loop"],"text":"Title: Custom Scala REPL Issues\nTags: scala, compiler-construction, read-eval-print-loop\nSource: Stack Overflow\n\nQuestion:\nI'm trying to write a basic Scala REPL using some information I found on various sites. My most basic REPL implementation looks like this,\n\n```\nimport scala.tools.nsc.Settings\nimport scala.tools.nsc.interpreter._\n\nobject BillyREPL extends App {\n val settings = new Settings\n settings.usejavacp.value = true\n settings.deprecation.value = true\n\n new ILoop().process(settings)\n}\n```\n\nWith the following build settings,\n\n```\nimport sbt._\nimport sbt.Keys._\n\nobject BillyREPLBuild extends Build {\n\n lazy val billyrepl = Project(\n id = \"billyrepl\",\n base = file(\".\"),\n settings = Project.defaultSettings ++ Seq(\n name := \"BillyREPL\",\n organization := \"tv.yobriefcasts\",\n version := \"0.1-SNAPSHOT\",\n scalaVersion := \"2.10.1\",\n libraryDependencies ++= Seq(\n \"org.scala-lang\" % \"scala-compiler\" % \"2.10.1\",\n \"org.scala-lang\" % \"scala-library\" % \"2.10.1\"\n )\n )\n )\n}\n```\n\nAttempting to run this however leads to some warnings and eventual error (which I presume are caused by the initial warning),\n\n```\nWelcome to Scala version 2.10.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_12-ea).\nType in expressions to have them evaluated.\nType :help for more information.\n\nscala>\nFailed to initialize compiler: object scala.annotation.Annotation in compiler mirror not found.\n** Note that as of 2.8 scala does not assume use of the java classpath.\n** For the old behavior pass -usejavacp to scala, or if using a Settings\n** object programatically, settings.usejavacp.value = true.\n```\n\nAnd the error when trying to read/evaluate ANYTHING is below. I'm not sure if this is due to some extra missing dependency and I do realise what the error message says suggests this is not common but I wondered if before I open an issue if anyone has dealt with this before?\n\n```\n2\n\nFailed to initialize the REPL due to an unexpected error.\nThis is a bug, please, report it along with the error diagnostics printed below.\njava.lang.NullPointerException\n at scala.tools.nsc.interpreter.ExprTyper$codeParser$.applyRule(ExprTyper.scala:24)\n at scala.tools.nsc.interpreter.ExprTyper$codeParser$.stmts(ExprTyper.scala:35)\n at scala.tools.nsc.interpreter.ExprTyper$$anonfun$parse$2.apply(ExprTyper.scala:43)\n at scala.tools.nsc.interpreter.ExprTyper$$anonfun$parse$2.apply(ExprTyper.scala:42)\n at scala.tools.nsc.reporters.Reporter.withIncompleteHandler(Reporter.scala:51)\n at scala.tools.nsc.interpreter.ExprTyper$class.parse(ExprTyper.scala:42)\n at scala.tools.nsc.interpreter.IMain$exprTyper$.parse(IMain.scala:1074)\n at scala.tools.nsc.interpreter.IMain.parse(IMain.scala:1078)\n at scala.tools.nsc.interpreter.IMain$$anonfun$showCodeIfDebugging$1.apply(IMain.scala:1168)\n at scala.tools.nsc.interpreter.IMain$$anonfun$showCodeIfDebugging$1.apply(IMain.scala:1168)\n at scala.tools.nsc.interpreter.IMain.beSilentDuring(IMain.scala:238)\n at scala.tools.nsc.interpreter.IMain.showCodeIfDebugging(IMain.scala:1168)\n at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.compileAndSaveRun(IMain.scala:800)\n at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.compile(IMain.scala:761)\n at scala.tools.nsc.interpreter.IMain.bind(IMain.scala:618)\n at scala.tools.nsc.interpreter.IMain.bind(IMain.scala:661)\n at scala.tools.nsc.interpreter.IMain$$anonfun$quietBind$1.apply(IMain.scala:660)\n at scala.tools.nsc.interpreter.IMain$$anonfun$quietBind$1.apply(IMain.scala:660)\n at scala.tools.nsc.interpreter.IMain.beQuietDuring(IMain.scala:232)\n at scala.tools.nsc.interpreter.IMain.quietBind(IMain.scala:660)\n at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1$$anonfun$apply$mcZ$sp$2.apply$mcV$sp(ILoop.scala:838)\n at scala.tools.nsc.interpreter.ILoopInit$class.runThunks(ILoopInit.scala:122)\n at scala.tools.nsc.interpreter.ILoop.runThunks(ILoop.scala:42)\n at scala.tools.nsc.interpreter.ILoopInit$class.postInitialization(ILoopInit.scala:95)\n at scala.tools.nsc.interpreter.ILoop.postInitialization(ILoop.scala:42)\n at scala.tools.nsc.interpreter.ILoopInit$$anonfun$createAsyncListener$1.apply$mcV$sp(ILoopInit.scala:63)\n at scala.tools.nsc.interpreter.ILoopInit$$anonfun$createAsyncListener$1.apply(ILoopInit.scala:60)\n at scala.tools.nsc.interpreter.ILoopInit$$anonfun$createAsyncListener$1.apply(ILoopInit.scala:60)\n at scala.tools.nsc.io.package$$anon$3.call(package.scala:40)\n at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)\n at java.util.concurrent.FutureTask.run(FutureTask.java:166)\n at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)\n at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)\n at java.lang.Thread.run(Thread.java:722)\n.\n```\n\n========================================\n\nTop Answer:\nMaybe you can get it to work with this hint:\n\nHow do I use the Scala interpreter in my code?\n\nSee also:\n\nHow to call the scala interpreter in a Simple Build Tool project?\n\n========================================\n\nCode:\n```text\nimport scala.tools.nsc.Settings\nimport scala.tools.nsc.interpreter._\n\nobject BillyREPL extends App {\n  val settings = new Settings\n  settings.usejavacp.value = true\n  settings.deprecation.value = true\n\n  new ILoop().process(settings)\n}\n```\n\n```text\nimport sbt._\nimport sbt.Keys._\n\nobject BillyREPLBuild extends Build {\n\n  lazy val billyrepl = Project(\n    id = \"billyrepl\",\n    base = file(\".\"),\n    settings = Project.defaultSettings ++ Seq(\n      name := \"BillyREPL\",\n      organization := \"tv.yobriefcasts\",\n      version := \"0.1-SNAPSHOT\",\n      scalaVersion := \"2.10.1\",\n      libraryDependencies ++= Seq(\n        \"org.scala-lang\" % \"scala-compiler\" % \"2.10.1\",\n        \"org.scala-lang\" % \"scala-library\" % \"2.10.1\"\n      )\n    )\n  )\n}\n```\n\n```text\nWelcome to Scala version 2.10.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_12-ea).\nType in expressions to have them evaluated.\nType :help for more information.\n\nscala>\nFailed to initialize compiler: object scala.annotation.Annotation in compiler mirror not found.\n** Note that as of 2.8 scala does not assume use of the java classpath.\n** For the old behavior pass -usejavacp to scala, or if using a Settings\n** object programatically, settings.usejavacp.value = true.\n```\n\n```text\n2\n\nFailed to initialize the REPL due to an unexpected error.\nThis is a bug, please, report it along with the error diagnostics printed below.\njava.lang.NullPointerException\n    at scala.tools.nsc.interpreter.ExprTyper$codeParser$.applyRule(ExprTyper.scala:24)\n    at scala.tools.nsc.interpreter.ExprTyper$codeParser$.stmts(ExprTyper.scala:35)\n    at scala.tools.nsc.interpreter.ExprTyper$$anonfun$parse$2.apply(ExprTyper.scala:43)\n    at scala.tools.nsc.interpreter.ExprTyper$$anonfun$parse$2.apply(ExprTyper.scala:42)\n    at scala.tools.nsc.reporters.Reporter.withIncompleteHandler(Reporter.scala:51)\n    at scala.tools.nsc.interpreter.ExprTyper$class.parse(ExprTyper.scala:42)\n    at scala.tools.nsc.interpreter.IMain$exprTyper$.parse(IMain.scala:1074)\n    at scala.tools.nsc.interpreter.IMain.parse(IMain.scala:1078)\n    at scala.tools.nsc.interpreter.IMain$$anonfun$showCodeIfDebugging$1.apply(IMain.scala:1168)\n    at scala.tools.nsc.interpreter.IMain$$anonfun$showCodeIfDebugging$1.apply(IMain.scala:1168)\n    at scala.tools.nsc.interpreter.IMain.beSilentDuring(IMain.scala:238)\n    at scala.tools.nsc.interpreter.IMain.showCodeIfDebugging(IMain.scala:1168)\n    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.compileAndSaveRun(IMain.scala:800)\n    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.compile(IMain.scala:761)\n    at scala.tools.nsc.interpreter.IMain.bind(IMain.scala:618)\n    at scala.tools.nsc.interpreter.IMain.bind(IMain.scala:661)\n    at scala.tools.nsc.interpreter.IMain$$anonfun$quietBind$1.apply(IMain.scala:660)\n    at scala.tools.nsc.interpreter.IMain$$anonfun$quietBind$1.apply(IMain.scala:660)\n    at scala.tools.nsc.interpreter.IMain.beQuietDuring(IMain.scala:232)\n    at scala.tools.nsc.interpreter.IMain.quietBind(IMain.scala:660)\n    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1$$anonfun$apply$mcZ$sp$2.apply$mcV$sp(ILoop.scala:838)\n    at scala.tools.nsc.interpreter.ILoopInit$class.runThunks(ILoopInit.scala:122)\n    at scala.tools.nsc.interpreter.ILoop.runThunks(ILoop.scala:42)\n    at scala.tools.nsc.interpreter.ILoopInit$class.postInitialization(ILoopInit.scala:95)\n    at scala.tools.nsc.interpreter.ILoop.postInitialization(ILoop.scala:42)\n    at scala.tools.nsc.interpreter.ILoopInit$$anonfun$createAsyncListener$1.apply$mcV$sp(ILoopInit.scala:63)\n    at scala.tools.nsc.interpreter.ILoopInit$$anonfun$createAsyncListener$1.apply(ILoopInit.scala:60)\n    at scala.tools.nsc.interpreter.ILoopInit$$anonfun$createAsyncListener$1.apply(ILoopInit.scala:60)\n    at scala.tools.nsc.io.package$$anon$3.call(package.scala:40)\n    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)\n    at java.util.concurrent.FutureTask.run(FutureTask.java:166)\n    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)\n    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)\n    at java.lang.Thread.run(Thread.java:722)\n.\n```\n\n========================================\n\nComments:\n- Are you starting your project via `sbt run`—because launching a custom interpreter from sbt doesn't work AFAIK. If that's the case, try building a standalone with sbt-assembly and see if that starts up correctly.\n- Ah that was it. Do you want to post this as the answer so I can accept it?\n- ugh! can this be fixed by making the Runner fork?\n- This isn't the best answer. I see in the linked answer in the other response that you also recommend forking. After following the faq instructions, to use `settings.embeddedDefaults`, the other rule is don't set `usejavacp`.","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":216,"estimatedTokens":2449}}990{"id":"stack-1822797","source":"stackoverflow","questionId":1822797,"title":"Regex, writing a toy compiler, parsing, comment remover","tags":["regex","parsing","compiler-construction"],"text":"Title: Regex, writing a toy compiler, parsing, comment remover\nTags: regex, parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm currently working my way through this book:\nhttp://www1.idc.ac.il/tecs/\n\nI'm currently on a section where the excersize is to create a compiler for a very simple java like language.\nThe book always states *what* is required but not the how the how (which is a good thing). I should also mention that it talks about yacc and lex and specifically says to avoid them for the projects in the book for the sake of learning on your own.\n\nI'm on chaper 10 which and starting to write the tokenizer.\n\n1) Can anyone give me some general advice - are regex the best approach for tokenizing a source file?\n\n2) I want to remove comments from source files before parsing - this isn't hard but most compilers tell you the line an error occurs on, if I just remove comments this will mess up the line count, are there any simple strategies for preserving the line count while still removing junk?\n\nThanks in advance!\n\n========================================\n\nTop Answer:\n1- Yes regex are good to implement the tokenizer. If using a generated tokenizer like lex, then you describe the each token as a regex. see Mark's answer.\n\n2- The lexer is what normally tracks line/column information, as tokens are consumed by the tokenizer, you track the line/column information with the token, or have it as current state. Therefore when a problem is found the tokenizer knows where you are. Therefore when processing comments, as new lines are processed the tokenizer just increments the line_count. \n\nIn Lex you can also have parsing states. Multi-line comments are often implemented using these states, thus allowing simpler regex's. Once you find the match to the start of a comment eg '/*' you change into comment state, which you can setup to be exclusive from the normal state. Therefore as you consume text looking for the end comment marker '*/' you do not match normal tokens. \n\nThis state based process is also useful for process string literals that allow nested end makers eg *\"test\\\"more text\"*.\n\n========================================\n\nCode:\n```text\nA-Z       space\n->(Start)----->(I1)------->((Identifier))\n     |         | ^\n     |         +-+\n     |        A-Z0-9\n     |\n     |          space   \n     +---->(N1)---+--->((Number)) <----------+\n      0-9  | ^    |                          |\n           | |    | .       0-9       space  |\n           +-+    +--->(N2)----->(N3)--------+\n           0-9                   | ^\n                                 +-+\n                                 0-9\n```\n\n```text\nI1            Identifier  N1      N2      N3      Number\nstart        letter        nothing     number  nothing nothing nothing\nI1           letter+number space       nothing nothing nothing nothing\nIdentifier   nothing       SUCCESS     nothing nothing nothing nothing\nN1           nothing       nothing     number  dot     nothing space\nN2           nothing       nothing     nothing nothing number  nothing\nN3           nothing       nothing     nothing nothing number  space\nNumber       nothing       nothing     nothing nothing nothing SUCCESS\n```\n\n```text\n((Number))\n```\n\n```text\n((Identifier))\n```\n\n========================================\n\nComments:\n- Unfortunately I didn't do a computer science degree, I take it a DFA table is Deterministic Finite Automata table? did a bit of googling and found this: cs.odu.edu/~toida/nerzic/390teched/regular/fa/&hellip; Could you expand a bit on the \"The way i built mine was to identify all the regular expressions my tokenizer will accept, transform them into DFA's and combine them.\" thanks in advance!\n- Hi there, thank you very much for the detailed answer. Very kind of you to bother explaining this stuff.","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":74,"estimatedTokens":953}}991{"id":"stack-3812670","source":"stackoverflow","questionId":3812670,"title":"What are the internal processes involved for a C compilation?","tags":["c","compiler-construction","embedded","linker"],"text":"Title: What are the internal processes involved for a C compilation?\nTags: c, compiler-construction, embedded, linker\nSource: Stack Overflow\n\nQuestion:\nI have a set of *.C files (embedded related).\n\nWhat are the steps/processes (internal information) involved while compiling followed by linking to create the final executable? (Information/steps regarding what a preprocessor/compiler generally performs to a C src code.)\n\nWhat is the general structure of the final executable (eg: headers followed by symbol tables etc.)?\n\n========================================\n\nTop Answer:\nThat's probably too in-depth for an SO question. If you really need to know how it all works, I suggest your read A Retargetable C Compiler. It'll go through all the steps to building a C compiler (I believe this book covers the `lcc` compiler).\n\n========================================\n\nCode:\n```text\na = a + 1;\n```\n\n```text\nLoad variable named a, size of blah, type unsigned foo\nload immediate 1, size blah, unsigned\nadd\nstore result a\n```\n\n```text\nunsigned int bob ( unsigned int a )\n{\n    return(a+1);\n}\n```\n\n```text\nclang -c -o bob.bc -emit-llvm bob.c\n```\n\n```text\nllvm-dis bob.bc\n```\n\n```text\ndefine i32 @bob(i32 %a) nounwind {\nentry:\n  %a.addr = alloca i32, align 4\n  store i32 %a, i32* %a.addr, align 4\n  %tmp = load i32* %a.addr, align 4\n  %add = add i32 %tmp, 1\n  ret i32 %add\n}\n```\n\n```text\nopt -std-compile-opts bob.bc -o bob_opt.bc\nllvm-dis bob_opt.bc\n```\n\n```text\ndefine i32 @bob(i32 %a) nounwind readnone {\nentry:\n  %add = add i32 %a, 1\n  ret i32 %add\n}\n```\n\n```text\nllc -march=arm bob.bc\ncat bob.s\n...\nbob:                                    @ @bob\n@ BB#0:                                 @ %entry\n    str r0, [sp, #-4]!\n    add r0, r0, #1\n    add sp, sp, #4\n    bx  lr\n...\nllc -march=arm bob_opt.bc\ncat bob_opt.s\n...\nbob:                                    @ @bob\n@ BB#0:                                 @ %entry\n    add r0, r0, #1\n    bx  lr\n...\n```\n\n```text\nlcc\n```\n\n```text\na\n```\n\n```text\na()\n```\n\n```text\na\n```\n\n========================================\n\nComments:\n- Ok. But we can go through some general steps?","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":110,"estimatedTokens":528}}992{"id":"stack-3663754","source":"stackoverflow","questionId":3663754,"title":"How is compiling c++/c#/java different?","tags":["c#","java","c++","compiler-construction"],"text":"Title: How is compiling c++/c#/java different?\nTags: c#, java, c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm trying to understand how these languages work under the hood. Unfortunately I only ever read very superficial things. \nI'll summarize what I know already, I would be really happy if you could correct me, and most of all, help me enhance my little bits of half-knowledge.\n\nC++:\n\nThe C++ compiler preprocesses all source files. This means, that it actually inserts strings into the places where macros where originally. After that, it creates an .obj file for each source file containing machine independant bytecode. \nThe linker then links all external .obj files from libraries with the custom made .obj files together, and compiles it into an .exe.\n\nJava:\n\nJava code is compiled into machine independant \"bytecode\" which sits in .class files, which in turn can sit in .JAR files, which get run on the JRE. The virtual machine is just doing garbage cleanup then. Java code is compiled just-in-time like C#, but with hotspot optimization developed by SUN.\n\nC#:\n\nPractically the same as Java? C# source code gets compiled into CIL (Common Intermediate Language) code, which is still human readable. This code will be run by the CLR Just-in-Time. This compilation turns methods into machine specific code just when they are first called.\n\nI'm actually interested in pretty much every language...but Java and C# are almost the same, and I always wondered how the differentiate. And C++ is the \"classic\" so to speak. The father of both without any kind of virtual machine. Appreciate the help!\n\nedit: I know that this is a broad subject, but I really couldn't find any solid knowledge. If you have links or books that explain this sort of thing I'm happy to go to work. I tried to read the SUN specifications/whitepapers for the java virtual machine, but that is all a little too deep for me right now.\n\n========================================\n\nTop Answer:\nPretty good.\n\nC++'s .obj files are machine dependent but generally do not have memory addresses resolved. A Linker just takes the .obj files and links them together and resolves many of the addresses to absolute values.\n\nIt's not really correct to say that the virtual machine is just doing garbage cleanup--not even sure what that means. The VM reads the bytes of code and decodes each one, so the VM is like a CPU. When it finds a bunch of code that is executed repeatedly it can replace that bytecode with real highly optimized machine code--that is JIT Compiling.\n\nI think the rest is pretty correct--although I can't honestly say if C#'s CIL is human readable.\n\n========================================\n\nComments:\n- not sure what the question is....\n- Most that is right. But the question appears to be lost in the description. Care to clarify?\n- What happens during the process of compiling until running c#/java/c++ code? @Vineet: What would be wrong? You said \"most\"\n- What exactly are you asking? The difference doesn't really lie in how they are compiled, it's in the language itself, and in case of Java/C#, how they are interpreted.\n- So there is no difference? that would be an answer already. It's just confusing to read about java bytecode, and then CIL, and not know what the difference is.\n- They are simply different bytecode languages, but they fulfill the same purpose.\n- @Blub, the Java bytecode is a standardized representation of the executable emitted by the Java compiler. It is standardized to the extent that any JVM can pick up the byte code and execute it. Not sure on how much standardization exists in the Microsoft world.\n- As far as I know the CIL is standardized, as well as the CLR. There is a lot of standardization and cross platform in .NET. :)\n- You mean it resolves the machine independant addresses (like an imaginary pointer to I/O) to something real on Windows right? Would be on par with something I read earlier.\n- @Blub Actually it resolves addresses that might point into another object file--the object files don't know about each other at compile time and so they don't know where the entry points will actually end up. when the linker puts all the object files together it resolves addresses that it can't know about until then (What if they object files linked in a different order?). C# and Java don't need this because they actually store the name of the method in the bytecode and let the runtime figure it all out.\n- Note there is nothing in the Java Language Specification that says you have to compile Java to JVML. And in fact, there are many implementations of Java that don't. They compile to CIL, native code, Parrot bytecode, ECMAScript, Dalvik bytecode or C. Some Java implementations are pure interpreters. Similarly for C#: there are compilers that target native as well. And for C++, there are interpreters which don't compile at all, and there are compilers which compile to Java or ECMAScript. Really, what a compiler compiles to is that compiler's business and has nothing to with the language.","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":50,"estimatedTokens":1261}}993{"id":"stack-10419101","source":"stackoverflow","questionId":10419101,"title":"How can I find the statements in a Scala program from within a compiler plugin?","tags":["scala","plugins","compiler-construction"],"text":"Title: How can I find the statements in a Scala program from within a compiler plugin?\nTags: scala, plugins, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm writing a Scala compiler plugin and have gotten to the point of writing the \"apply(unit:CompilationUnit\" method. The syntax within that method is beyond me however. The following code is from http://www.scala-lang.org/node/140\n\n```\nfor ( tree @ Apply(Select(rcvr, nme.DIV), List(Literal(Constant(0)))) This expression finds all division by zero. I can't figure out how to do something similar to find all executable statements (TermTrees??).\n\n========================================\n\nCode:\n```text\nfor ( tree @ Apply(Select(rcvr, nme.DIV), List(Literal(Constant(0)))) <- unit.body;\n         if rcvr.tpe <:< definitions.IntClass.tpe) {\n        unit.error(tree.pos, \"definitely division by zero\")\n      }\n```\n\n```text\nclass TwoStatements {\n  def f {\n    println(\"first statement\")\n    println(\"second statement\")\n  }\n}\n```\n\n```text\nscalac -Yshow-trees -Xprint:typer TwoStatements.scala \nscalac -Yshow-trees-compact -Xprint:typer TwoStatements.scala \nscalac -Yshow-trees-stringified -Xprint:typer TwoStatements.scala \nscalac -Ybrowse:typer TwoStatements.scala\n```\n\n```text\nApply(\n   Select(\n     Select(This(newTypeName(\"Test\")), newTermName(\"five\")), // assigned to rcvr\n     newTermName(\"$div\")                                     // compared to nme.DIV\n   ), \n   List(Literal(Constant(0))))                               // as is\n )\n```\n\n```text\n// A standard pattern match\n  case EmptyTree =>\n  case PackageDef(pid, stats) =>\n     // package pid { stats }\n  case ClassDef(mods, name, tparams, impl) =>\n     // mods class name [tparams] impl   where impl = extends parents { defs }\n  case ModuleDef(mods, name, impl) =>                             (eliminated by refcheck)\n     // mods object name impl  where impl = extends parents { defs }\n  case ValDef(mods, name, tpt, rhs) =>\n     // mods val name: tpt = rhs\n     // note missing type information is expressed by tpt = TypeTree()\n  case DefDef(mods, name, tparams, vparamss, tpt, rhs) =>\n     // mods def name[tparams](vparams_1)...(vparams_n): tpt = rhs\n     // note missing type information is expressed by tpt = TypeTree()\n  case TypeDef(mods, name, tparams, rhs) =>                       (eliminated by erasure)\n     // mods type name[tparams] = rhs\n     // mods type name[tparams] >: lo <: hi,  where lo, hi are in a TypeBoundsTree,\n                                              and DEFERRED is set in mods\n  case LabelDef(name, params, rhs) =>\n     // used for tailcalls and like\n     // while/do are desugared to label defs as follows:\n     // while (cond) body ==> LabelDef($L, List(), if (cond) { body; L$() } else ())\n     // do body while (cond) ==> LabelDef($L, List(), body; if (cond) L$() else ())\n  case Import(expr, selectors) =>                                 (eliminated by typecheck)\n     // import expr.{selectors}\n     // Selectors are a list of pairs of names (from, to).\n     // The last (and maybe only name) may be a nme.WILDCARD\n     // for instance\n     //   import qual.{x, y => z, _}  would be represented as\n     //   Import(qual, List((\"x\", \"x\"), (\"y\", \"z\"), (WILDCARD, null)))\n  case Template(parents, self, body) =>\n     // extends parents { self => body }\n     // if self is missing it is represented as emptyValDef\n  case Block(stats, expr) =>\n     // { stats; expr }\n  case CaseDef(pat, guard, body) =>                               (eliminated by transmatch/explicitouter)\n    // case pat if guard => body\n  case Alternative(trees) =>                                      (eliminated by transmatch/explicitouter)\n    // pat1 | ... | patn\n  case Star(elem) =>                                              (eliminated by transmatch/explicitouter)\n    // pat*\n  case Bind(name, body) =>                                        (eliminated by transmatch/explicitouter)\n    // name @ pat\n  case UnApply(fun: Tree, args)                                   (introduced by typer, eliminated by transmatch/explicitouter)\n    // used for unapply's\n  case ArrayValue(elemtpt, trees) =>                              (introduced by uncurry)\n    // used to pass arguments to vararg arguments\n    // for instance, printf(\"%s%d\", foo, 42) is translated to after uncurry to:\n    // Apply(\n    //   Ident(\"printf\"),\n    //   Literal(\"%s%d\"),\n    //   ArrayValue(<Any>, List(Ident(\"foo\"), Literal(42))))\n  case Function(vparams, body) =>                                 (eliminated by lambdaLift)\n    // vparams => body  where vparams:List[ValDef]\n  case Assign(lhs, rhs) =>\n    // lhs = rhs\n  case AssignOrNamedArg(lhs, rhs) =>                              (eliminated by typer, resurrected by reifier)\n    // @annotation(lhs = rhs)\n  case If(cond, thenp, elsep) =>\n    // if (cond) thenp else elsep\n  case Match(selector, cases) =>\n    // selector match { cases }\n  case Return(expr) =>\n    // return expr\n  case Try(block, catches, finalizer) =>\n    // try block catch { catches } finally finalizer where catches: List[CaseDef]\n  case Throw(expr) =>\n    // throw expr\n  case New(tpt) =>\n    // new tpt   always in the context: (new tpt).<init>[targs](args)\n  case Typed(expr, tpt) =>                                        (eliminated by erasure)\n    // expr: tpt\n  case TypeApply(fun, args) =>\n    // fun[args]\n  case Apply(fun, args) =>\n    // fun(args)\n    // for instance fun[targs](args)  is expressed as  Apply(TypeApply(fun, targs), args)\n  case ApplyDynamic(qual, args)                                   (introduced by erasure, eliminated by cleanup)\n    // fun(args)\n  case Super(qual, mix) =>\n    // qual.super[mix]     qual is always This(something), if mix is empty, it is tpnme.EMPTY\n  case This(qual) =>\n    // qual.this\n  case Select(qualifier, selector) =>\n    // qualifier.selector\n  case Ident(name) =>\n    // name\n    // note: type checker converts idents that refer to enclosing fields or methods\n    // to selects; name ==> this.name\n  case ReferenceToBoxed(ident) =>                                 (created by typer, eliminated by lambdalift)\n    // synthetic node emitted by macros to reference capture vars directly without going through ``elem''\n    // var x = ...; fun { x } will emit Ident(x), which gets transformed to Select(Ident(x), \"elem\")\n    // if ReferenceToBoxed were used instead of Ident, no transformation would be performed\n  case Literal(value) =>\n    // value\n  case TypeTree() =>                                              (introduced by refcheck)\n    // a type that's not written out, but given in the tpe attribute\n  case Annotated(annot, arg) =>                                   (eliminated by typer)\n    // arg @annot  for types,  arg: @annot for exprs\n  case SingletonTypeTree(ref) =>                                  (eliminated by uncurry)\n    // ref.type\n  case SelectFromTypeTree(qualifier, selector) =>                 (eliminated by uncurry)\n    // qualifier # selector, a path-dependent type p.T is expressed as p.type # T\n  case CompoundTypeTree(templ: Template) =>                       (eliminated by uncurry)\n    // parent1 with ... with parentN { refinement }\n  case AppliedTypeTree(tpt, args) =>                              (eliminated by uncurry)\n    // tpt[args]\n  case TypeBoundsTree(lo, hi) =>                                  (eliminated by uncurry)\n    // >: lo <: hi\n  case ExistentialTypeTree(tpt, whereClauses) =>                  (eliminated by uncurry)\n    // tpt forSome { whereClauses }\n```\n\n```text\nTwoStatements.scala\n```\n\n```text\ntyper\n```\n\n```text\n-Yshow-trees-compact\n```\n\n```text\n-Yshow-trees-stringified\n```\n\n```text\n-Yshow-trees\n```\n\n```text\n-Ybrowse:typer\n```\n\n```text\n-Yshow-trees-compact\n```\n\n```text\nDefDef\n```\n\n```text\nBlock\n```\n\n```text\nList\n```\n\n```text\nApply\n```\n\n```text\nSelect\n```\n\n```text\nAssign\n```\n\n```text\nIf\n```\n\n```text\nValDef\n```\n\n```text\ntree @ Statement(...)\n```\n\n```text\nTermTree\n```\n\n```text\nisTerm\n```\n\n```text\nscala/reflecti/api/Trees.scala\n```\n\n========================================\n\nComments:\n- The commands you suggest do work, but I'm looking for Scala code to put in my compiler plugin that will let me get at the statements from the CompilationUnit tree. Thanks.\n- @BrianTarbox Define \"statement\", and make that definition in terms of the Scala Specification. Only then I can give a precise answer, otherwise you'll have to look up actual code and identify how the things you want to grab are represented.\n- @BrianTarbox Just added something I saw on another presentation. It's what a full pattern match would look like.\n- Thank you so much! Would you have any interest in working on this project to develop a checkstyle/findbugs like tool for Scala? I'm working on two fronts: develop a tool that can look for patterns in the code, possibly flagging constructs that a Development Shop dislikes and possibly generating a Complexity Metric. The second front is getting community ideas about what to measure.\n- @BrianTarbox I've no time for anything else atm. Hell, I don't have time for my own responsibilities. Anyway, there are at least two existing efforts: Foursquare's Linter and Scalastyle.","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":24,"totalLines":244,"estimatedTokens":2290}}994{"id":"stack-50579999","source":"stackoverflow","questionId":50579999,"title":"What is difference between two f# compiler projects","tags":["f#","compiler-construction"],"text":"Title: What is difference between two f# compiler projects\nTags: f#, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI want to learn f# compiler's internals. It turns out that there is two repositories on github. Here are links to these repos:\n\nhttps://github.com/Microsoft/visualfsharp\n\nhttps://github.com/fsharp/fsharp\n\nWhat is difference between these projects? Which should I use to learn compiler's design? I really cannot understand difference.\n\n========================================\n\nComments:\n- well, there is a readme\n- Contributing to the F# Compiler, Core Library and Tools\n- If you want to learn the compiler internals, I'd suggest going through the repository issues and trying to tackle a small one. Make a PR, and don't be afraid to ask questions in it -- the community there is super welcoming. Also, check out the F# Software Foundation slack channel!","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":19,"estimatedTokens":220}}995{"id":"stack-21084939","source":"stackoverflow","questionId":21084939,"title":"How does the compiler control help in allocating & deallocating memory in C++?","tags":["c++","compiler-construction","dynamic-memory-allocation"],"text":"Title: How does the compiler control help in allocating & deallocating memory in C++?\nTags: c++, compiler-construction, dynamic-memory-allocation\nSource: Stack Overflow\n\nQuestion:\nI read in a C++ book that `malloc()` & `free()` are liabrary functions, and thus are outside the control of the compiler. \n\nHowever, if you have an operator to perform the combined act of dynamic storage allocation & initialization (`new`)and another operator to perform the combined act of clean up & releasing storage (`delete`), the compiler can still guarantee that constructors & destructors will be called for all objects. \n\nSo, I want to know that How does this is carried out by compiler?\nAny Example or demo will be appriciated.\n\nThanks in advance.\n\n========================================\n\nTop Answer:\nThe compiler doesn't really directly \"help\" allocate and deallocate memory; your code has to explicitly do this. The C++ language provides deterministic execution of code when a thread leaves scope (destructors). That in turn is often used to free heap-allocated memory.\n\n========================================\n\nCode:\n```text\nmalloc()\n```\n\n```text\nfree()\n```\n\n```text\nnew\n```\n\n```text\ndelete\n```\n\n```text\nint *ptr = malloc(sizeof(char) * 4);\n```\n\n```text\nint *ptr = new char[4];\n```\n\n```text\nmalloc\n```\n\n```text\nnew\n```\n\n```text\nnew\n```\n\n```text\nvoid*\n```\n\n```text\nnew\n```\n\n```text\nchar\n```\n\n```text\nint\n```\n\n```text\nmalloc\n```\n\n```text\nptr\n```\n\n```text\nint *ptr = new int;\n```\n\n```text\nstruct Test() { }\nTest *a = new Test();\n```\n\n```text\nTest *a = (Test *)malloc(sizeof(Test));\nif (a == nullptr) { throw std::bad_alloc; }\ntry\n{\n    a.Test(); //call constructor\n}\ncatch (...)\n{\n     //constructor exception, free the memory first, then re-throw\n     free(a);\n     throw;\n}\n```\n\n```text\nstruct Test() { }\nTest *a = new Test[10];\n```\n\n```text\nTest *a = (Test *)malloc(sizeof(Test) * 10);\nif (a == nullptr) { throw std::bad_alloc; }\nint i;\ntry\n{\n    for (i = 0; i < 10, i++)\n        a[i].Test(); //call constructor\n}\ncatch (...)\n{\n     //call destructor for all constructed objects\n     for (int j = 0; j < i; j++)\n         a[j].~Test();\n\n     free(a);\n     throw;\n}\n```\n\n========================================\n\nComments:\n- I should mention that malloc() and free() aren't the \"preferred\" way to allocate memory in C++. They're mostly for plain C support. C++ you would normally use new and delete.","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":20,"totalLines":135,"estimatedTokens":599}}996{"id":"stack-464722","source":"stackoverflow","questionId":464722,"title":"Adding a language to the AVM2","tags":["actionscript-3","compiler-construction","language-design","bytecode"],"text":"Title: Adding a language to the AVM2\nTags: actionscript-3, compiler-construction, language-design, bytecode\nSource: Stack Overflow\n\nQuestion:\nI'm interested in making a language to run on the AVM2 and I'm looking for advice on where to start. I do realize that this is by no means a trivial task, but I would like to give it a try and at the very least learn more about implementing a language along the way.\n\nI have messed around with ANTLR and have been reading up on syntax issues for language development. What I'm looking for is advice on a path to take or useful references/books.\n\nFor instance I would like to generate (script/manually) some very simple AVM2 bytecode and get that to run on the VM as a start.\n\nThanks\n\n========================================\n\nTop Answer:\nTake a look at Haxe: it is an open source language that can target different platforms, including the AVM. You can dig into the SWF compiler source code to get some inspiration.\n\n========================================\n\nComments:\n- Thanks Richard, I've just picked those docs up a while back, now to sit down with them. I'll try use the ASMock code to output a simple SWF file that performs some simple arithmetic operations for a start.\n- The portion of asmock that generates bytecode (flemit) is not actually documented/supported yet, but if you have any questions feel free to shoot them over.\n- I looked into it and found it too much work, although I am still interested in it. Im not interested in translating languages but more making a modified as3.","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":385}}997{"id":"stack-57962476","source":"stackoverflow","questionId":57962476,"title":"GCC/Clang lexer and parser","tags":["compiler-construction"],"text":"Title: GCC/Clang lexer and parser\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am curious about how C/C++ lexers and parsers work together. I know the parser generally needs lookahead of at least one token. My question though is, in production compilers (say gcc or clang):\n\n1) Does the lexer run first, lex the entire file, and then let the parser generate the AST. This would mean the lexer generates a list of tokens.\n\nOR\n\n2) Does the lexer only generate a small set of tokens enough for the parser to do its job. This would mean the lexer and parser take turns running.\n\nI definitely think option 1 is used since languages like C++ sometimes need arbitrary lookahead as the grammar isn't context free, but this would take a lot of memory.\n\n========================================\n\nCode:\n```text\nfor all input characters:\n    feed character to tokenizer\n```\n\n========================================\n\nComments:\n- Some info here for clang: clang.llvm.org/docs/InternalsManual.html ; it's more your option 2 (parser requests tokens from the lexer), but I'm not nearly familiar enough to actually answer.\n- Part 1. A couple of things: While it's true that the language C++ is not context free (for example, a variable needs to be defined before it can be used), the grammars used to parse the language are context free. How can this be? For example, a parser based on an LALR(1) grammar will perform *semantic actions* when it performs reductions. Specifically, when it recognizes a variable declaration, it will enter the variable definition into a symbol table. When it recognizes an expression that uses a variable, it can check the symbol table to see if the variable exists.\n- Part 2. In general, lexical analysis and parsing run in parallel with the parser calling the lexer for the \"next token\" as it needs it. But, this can involve a lot of reading and processing on the part of the lexer. Consider the handling of include files, which may be nested to many levels and contain macro definitions. We know that one of the C/C++ compile-time switches is the ability to only preprocess the input and output a new source file without compiling. But this is not an actual stream of tokens that the parser deals with. You would still need lexical analysis on this output.\n- Neither (1) nor (2). The lexer isn't 'run' at all: it is called as a procedure by the parser every time the parser needs a new token. The lexer returns one token at a time. C++ does not need arbitrary lookahead, and the fact that the grammar at isn't context-free has nothing to do with that.\n- Thank you very very much for this response! It was very helpful :-) !","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":31,"estimatedTokens":665}}998{"id":"stack-34915330","source":"stackoverflow","questionId":34915330,"title":"How does the java compiler assign index's in the local variables table?","tags":["java","compiler-construction","bytecode"],"text":"Title: How does the java compiler assign index's in the local variables table?\nTags: java, compiler-construction, bytecode\nSource: Stack Overflow\n\nQuestion:\nAlright so I'm writing a compiler and I'm trying to use the information in the local variables table to figure out names/types for variables.\n\nI have the following code:\n\n```\npublic void noob() {\n try {\n int hello = 0;\n short yo = 1;\n byte y = 2;\n int[] e = new int[9];\n\n System.out.println(y + \", \" + hello + \", \" + yo+\", \"+e);\n\n } catch (Exception var6) {\n var6.printStackTrace();\n }\n}\n```\n\nWhen reading the variables table I get the following:\n\n```\nLocalVariable{uid=-1, start=0, end=69, nameIndex=30, typeIndex=31, varIndex=0, name='this', typeName='LMain;'}\nLocalVariable{uid=-1, start=2, end=60, nameIndex=37, typeIndex=18, varIndex=1, name='hello', typeName='I'}\nLocalVariable{uid=-1, start=4, end=60, nameIndex=38, typeIndex=39, varIndex=2, name='yo', typeName='S'}\nLocalVariable{uid=-1, start=6, end=60, nameIndex=40, typeIndex=41, varIndex=3, name='y', typeName='B'}\nLocalVariable{uid=-1, start=12, end=60, nameIndex=42, typeIndex=43, varIndex=4, name='e', typeName='[I'}\nLocalVariable{uid=-1, start=64, end=68, nameIndex=44, typeIndex=45, varIndex=1, name='var6', typeName='Ljava/lang/Exception;'}\n```\n\n(ignore the uid column).\n\nI noticed that varIndex is not unqiue for every variable. Why do `hello (int)` and `var6(Exception)` the same varIndex?\n\n========================================\n\nCode:\n```text\npublic void noob() {\n    try {\n        int hello = 0;\n        short yo = 1;\n        byte y = 2;\n        int[] e = new int[9];\n\n        System.out.println(y + \", \" + hello + \", \" + yo+\", \"+e);\n\n    } catch (Exception var6) {\n        var6.printStackTrace();\n    }\n}\n```\n\n```text\nLocalVariable{uid=-1, start=0, end=69, nameIndex=30, typeIndex=31, varIndex=0, name='this', typeName='LMain;'}\nLocalVariable{uid=-1, start=2, end=60, nameIndex=37, typeIndex=18, varIndex=1, name='hello', typeName='I'}\nLocalVariable{uid=-1, start=4, end=60, nameIndex=38, typeIndex=39, varIndex=2, name='yo', typeName='S'}\nLocalVariable{uid=-1, start=6, end=60, nameIndex=40, typeIndex=41, varIndex=3, name='y', typeName='B'}\nLocalVariable{uid=-1, start=12, end=60, nameIndex=42, typeIndex=43, varIndex=4, name='e', typeName='[I'}\nLocalVariable{uid=-1, start=64, end=68, nameIndex=44, typeIndex=45, varIndex=1, name='var6', typeName='Ljava/lang/Exception;'}\n```\n\n```text\nhello (int)\n```\n\n```text\nvar6(Exception)\n```\n\n```text\nhello\n```\n\n```text\nvar6\n```\n\n========================================\n\nComments:\n- Ok that makes sense. Is their anything that will be unique to every variable? such as nameIndex, typeIndex?\n- @JonathanBeaudoin: Probably not. TypeIndex would certainly not be unique, since variables can have the same type. Try making two variables with the same name in different scopes and look at nameIndex.\n- Nope it reuses that too. Okay I guess I will have to find another way to make a unique id. Regardless, thanks for your help!\n- @Jonathan Beaudoin: what is the purpose of the unique id you want to create?","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":91,"estimatedTokens":769}}999{"id":"stack-23827531","source":"stackoverflow","questionId":23827531,"title":"Reassociation according to Muchnick","tags":["optimization","compiler-construction","program-transformation"],"text":"Title: Reassociation according to Muchnick\nTags: optimization, compiler-construction, program-transformation\nSource: Stack Overflow\n\nQuestion:\nI’m reading Muchnick’s “Advanced Compiler Design & Implementation”, where Fig 12.6 lists 20 transformation rules which, if applied in order, do constant folding and reassociation to move constants together. The rules (leaving out distributitvity rules) are (my syntax: `c` are literals, `t` terms, operators with spaces around are in in the source, whereas operators without spaces indicate a compile-time calculation involving literals):\n\nR1: c1 + c2 = c1+c2\nR3: c1 * c2 = c1*c2\nR5: c1 - c2 = c1-c2\n\nR2: t + c = c + t\nR4: t * c = c * t\nR6: t - c = (-c) + t\n\nR7: t1 + (t2 + t3) = (t1 + t2) + t3\nR8: t1 * (t2 * t3) = (t1 * t2) * t3 \n\nR9: (c1 + t) + c2 = (c1+c2) + t\nR10: (c1 * t) * c2 = (c1*c2) * t\n\nHe writes “recursively apply the tree transformation rules [..] in the order given“, but I fail to see how that works out. Given `((c1 + t1) + t2) + c2`, how would I have to apply the rules to get to `(c1+c2 + t1) + t2` or something similar?\n\n(I could come up with a different set of rules that would work, but I’d like to understand what’s in the book, in case I’m just reading it wrong).\n\n========================================\n\nCode:\n```text\nR1:  c1 + c2 = c1+c2\nR3:  c1 * c2 = c1*c2\nR5:  c1 - c2 = c1-c2\n\nR2:  t + c  = c + t\nR4:  t * c  = c * t\nR6:  t - c  = (-c) + t\n\nR7:  t1 + (t2 + t3) = (t1 + t2) + t3\nR8:  t1 * (t2 * t3) = (t1 * t2) * t3 \n\nR9:  (c1 + t) + c2 = (c1+c2) + t\nR10: (c1 * t) * c2 = (c1*c2) * t\n```\n\n```text\nc\n```\n\n```text\nt\n```\n\n```text\n((c1 + t1) + t2) + c2\n```\n\n```text\n(c1+c2 + t1) + t2\n```\n\n```text\n((c1 + t1) + t2) + c2\n```\n\n```text\nc2 + ((c1 + t1) + t2)\n```\n\n```text\n(c2 + (c1 + t1)) + t2\n```\n\n```text\n(c2 + c1) + t1\n```\n\n```text\nc2+c1\n```\n\n========================================\n\nComments:\n- Hmm. Now I don’t see my problem any more... thanks :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":10,"totalLines":84,"estimatedTokens":480}}1000{"id":"stack-17244083","source":"stackoverflow","questionId":17244083,"title":"Conditions for loop unrolling to be beneficial and the point at which margins of return decrease?","tags":["c++","c","performance","optimization","compiler-construction"],"text":"Title: Conditions for loop unrolling to be beneficial and the point at which margins of return decrease?\nTags: c++, c, performance, optimization, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nCould someone please explain firstly at what point does unrolling a loop (in C/C++) become a useful optimization to do? \n\nSecondly, linked to the first question, at what point should unrolling not be performed any further? Should the unrolling always be performed splitting operations into batches a power of 2? Or does it relate to how many calculations can be performed by your CPU? A ratio which is a multiplier of your cache line size? etc\n\nFor example if I had a for-loop from 0 to 99 which would be better/how do I determine which (besides just trial and error)- is there a scientific method:\n\n- 0 to 49 and two \"operations\" per loop\n\n- 0 to 24 and four \"operations\" per loop\n\n- 0 to 19 and five \"operations\" per loop\n\n- 0 to 9 and ten \"operations\" per loop\n\n========================================\n\nCode:\n```text\nwhile (n-- > 0) {\n    *ptr++ &= mask;\n}\n```\n\n```text\nswitch (n % 4) do {\ncase 0: *ptr++ &= mask;\ncase 3: *ptr++ &= mask;\ncase 2: *ptr++ &= mask;\ncase 1: *ptr++ &= mask;\n} while ((n -= 4) > 0);\n```\n\n========================================\n\nComments:\n- Is it a problem? If yes, profile, change, and repeat until satisfied. Otherwise leave it.\n- Often the compiler can do this for you. It is a micro optimisation, and whether it is useful will depend on the exact situation. Therefore, benchmark.\n- (If you can combine loop unrolling with architecture-specific vectorization, it's a different story. That's usually a big boost to performance, and is used by many high-performance libraries. But again it can only be verified by benchmarking)\n- Basically, you're just wasting resources if you continue to unroll past the point where the loop overhead (time spent in stepping, condition check, and branch instructions) is small relative to the actual work done in the loop body in one (unrolled) iteration. There's no reason to prefer powers of two or particular divisions, but if the loop has a fixed number of iterations, it may be preferable to make sure the number of unrolled steps divides the number of iterations evenly so you don't need special fixup code for the tail.\n- @RalphJohnson if it wasn't a problem I wouldn't be asking.\n- Just to echo what @R.. said, I've seen people write `for(...){f(...);}` and ask about unrolling. Unrolling only makes sense if the body of the loop spends *very few* cycles. Anything as costly as a function call almost certainly makes loop unrolling not worthwhile.\n- Yeah. Basically the only operations where loop unrolling make sense are the standard string functions (`strlen`, `strchr`, `memcpy`, etc.) and the inner loops of signal processing (audio/video/etc.), cryptography, and so on.\n- @MikeDunlavey: But if the loop body is that simple, the compiler (at sufficient optimization level) should be unrolling for you.\n- @jxh: That's what's heard in classrooms or echoed across blogs. Maybe it will, and maybe it won't. You have to check the assembly code to find out. There's a big difference between \"should\" and \"does\".","metadata":{"transformedAt":"2026-08-18T18:32:17.846Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":49,"estimatedTokens":797}}1001{"id":"stack-22746361","source":"stackoverflow","questionId":22746361,"title":"S-expression Tree to Abstract Syntax Tree in OCaml","tags":["compiler-construction","ocaml","abstract-syntax-tree","s-expression"],"text":"Title: S-expression Tree to Abstract Syntax Tree in OCaml\nTags: compiler-construction, ocaml, abstract-syntax-tree, s-expression\nSource: Stack Overflow\n\nQuestion:\nI'm implementing a symbolic language in OCaml and have been struggling to translate my s-expression tree into an abstract syntax tree. \n\nThe s-expression tree is \n\n```\n(* sexpr.mli *)\ntype atom =\n | Atom_unit\n | Atom_int of int\n | Atom_sym of string\n\ntype expr =\n | Expr_atom of atom\n | Expr_list of expr list\n```\n\nThe abstract syntax tree is \n\n```\n(* ast.ml *)\nopen Sexpr\n\ntype sym = string\n\n(* abstract syntax tree nodes *)\ntype expr =\n | Expr_unit\n | Expr_int of int\n | Expr_sym of sym\n\n(* Sexp.atom -> Ast.expr *)\nlet ast_of_atom a =\n match a with\n | Atom_unit -> Expr_unit\n | Atom_int n -> Expr_int n\n | Atom_sym s -> Expr_sym s\n\n(* Sexp.expr -> Ast.expr *)\nlet rec ast_of_sexpr sx = match sx with\n | Expr_atom a -> ast_of_atom a\n | Expr_list l -> \n match l with\n | [] -> ast_of_atom Atom_unit\n | [x] -> ast_of_sexpr x\n | h::t -> ignore ( ast_of_sexpr h ); ast_of_sexpr ( Expr_list t )\n```\n\nThe function `ast_of_sexpr` needs to conform with the type signature\n\n`val ast_of_sexpr : Sexpr.expr -> expr`. \n\n*This is my challenge; I can't figure out a way, that conforms with the type signature, to recurse into the s-expression tree (i.e. nested lists) and translate s-expression tree nodes to abstract syntax tree nodes.* \n\nIn an ideal world, I could evaluate the list head and recurse over the tail in one expression. I've tried to emulate this ideal using sequencing. But this, of course, ignores the left-side value and will only output the last value when printing a parsed stream of tokens. \n\nCan anyone suggest a method for evaluating the list head, *without ignoring the value*, and recursing deeper into the s-expression tree? I'm even open to reading better solutions for translating between the two trees.\n\n========================================\n\nTop Answer:\nVery generally, here's how to calculate some values \"without ignoring them\":\n\n```\nlet v = in\nlet w = in\n\n```\n\nI'm not sure this is what you're asking, but this is a conceptual difficulty for people starting with OCaml (IMHO).\n\n========================================\n\nCode:\n```text\n(* sexpr.mli *)\ntype atom =\n   | Atom_unit\n   | Atom_int  of int\n   | Atom_sym  of string\n\ntype expr =\n   | Expr_atom of atom\n   | Expr_list of expr list\n```\n\n```text\n(* ast.ml *)\nopen Sexpr\n\ntype sym = string\n\n(* abstract syntax tree nodes *)\ntype expr =\n   | Expr_unit\n   | Expr_int    of int\n   | Expr_sym    of sym\n\n(* Sexp.atom -> Ast.expr *)\nlet ast_of_atom a =\n   match a with\n      | Atom_unit  -> Expr_unit\n      | Atom_int n -> Expr_int n\n      | Atom_sym s -> Expr_sym s\n\n(* Sexp.expr -> Ast.expr *)\nlet rec ast_of_sexpr sx = match sx with\n    | Expr_atom a -> ast_of_atom a\n    | Expr_list l -> \n    match l with\n       | []   -> ast_of_atom Atom_unit\n       | [x]  -> ast_of_sexpr x\n       | h::t -> ignore ( ast_of_sexpr h ); ast_of_sexpr ( Expr_list t )\n```\n\n```text\nast_of_sexpr\n```\n\n```text\nval ast_of_sexpr : Sexpr.expr -> expr\n```\n\n```text\ntype expr =\n   | Expr_unit\n   | Expr_int    of int\n   | Expr_sym    of sym\n   | Expr_call   of expr list\n```\n\n```text\nAst.expr\n```\n\n```text\nSexp.expr\n```\n\n```text\nlet v = <calculate> in\nlet w = <calculate> in\n<expression using v and w>\n```\n\n========================================\n\nComments:\n- This is an interesting observation. I think it may be the way to solve this issue too. Before accepting the answer, I want to understand you clearly. In essence, your suggestion is to create a one-to-one mapping between the two trees? Seems reasonable. But why is a recurive AST strictly necessary to define a tree structure? I was under the impression that, for symbolic grammars, the AST nodes are all atoms. And I thought the s-expression list types serve only as directives to recurse deeper into the AST. Am I misunderstanding this point?\n- Defining the nodes is not the same thing as defining the tree. Recursion is needed because you need to store an unbounded amount of information in a syntax tree, and a non recursive type can store only a bounded amount. Of course, you can define a parametric `tree` type separately and then use `node tree`, in which `node` is not recursive and `'a tree` is. This is what `Sexp.expr` is doing having two different type declarations.","metadata":{"transformedAt":"2026-08-18T18:32:17.942Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":154,"estimatedTokens":1088}}1002{"id":"stack-46745654","source":"stackoverflow","questionId":46745654,"title":"How can `AssignmentExpression` be derived from `Statement` in EcmaScript grammar","tags":["javascript","ecmascript-6","context-free-grammar","compiler-construction"],"text":"Title: How can `AssignmentExpression` be derived from `Statement` in EcmaScript grammar\nTags: javascript, ecmascript-6, context-free-grammar, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nSuppose I have the following part of JS code:\n\n```\nconst v = 3;\n```\n\nAs I understand it can be parsed as AssignmentExpression:\n\n```\nAssignmentExpression :\n LeftHandSideExpression = AssignmentExpression\n```\n\nNow I'm wondering how it can be derived from the Statement? One possible route is:\n\n```\nStatement -> ExpressionStatement -> Expression -> AssignmentExpression\n```\n\nBut I'm not sure about that. Is it correct? \n\nHere is how I found it: \n\n`AssignmentExpression` is part Expression:\n\n```\nExpression :\n AssignmentExpression\n Expression, AssignmentExpression\n```\n\nthen `Expression` is part of ExpressionStatement is \n\n```\nExpressionStatement:\n\n [lookahead ∉ { {, function, ..., let [ }] Expression;\n```\n\nand then `ExpressionStatement` is part of Statement:\n\n```\nStatement :\n ExpressionStatement\n```\n\n========================================\n\nCode:\n```text\nconst v = 3;\n```\n\n```text\nAssignmentExpression :\n    LeftHandSideExpression = AssignmentExpression\n```\n\n```text\nStatement -> ExpressionStatement -> Expression -> AssignmentExpression\n```\n\n```text\nExpression :\n    AssignmentExpression\n    Expression, AssignmentExpression\n```\n\n```text\nExpressionStatement:\n\n    [lookahead ∉ { {, function, ..., let [ }] Expression;\n```\n\n```text\nStatement :\n    ExpressionStatement\n```\n\n```text\nAssignmentExpression\n```\n\n```text\nExpression\n```\n\n```text\nExpressionStatement\n```\n\n```text\nScript\n               |\n          ScriptBody\n               |\n         StatementList\n               |\n       StatementListItem\n               |\n          Declaration\n               |\n      LexicalDeclaration\n     /         |        \\\nLetOrConst BindingList  ';'\n    |           |\n 'const'   LexicalBinding\n             /          \\\n     BindingIdentifier  Initializer\n            |                |\n         Identifier    AssignmentExpression\n            |                  |\n       IdentifierName         ...\n            |                  |\n           'v'                '5'\n```\n\n```text\nStatement\n                         |\n               ExpressionStatement\n                     /        \\\n                   Expression ';'\n                        |\n              AssignmentExpression\n             /          |         \\\nLeftHandSideExpression '=' AssignmentExpression\n         |                         |\n    NewExpression                ...\n         |                         |\n   MemberExpression               '5'\n         |\n   PrimaryExpression              \n         |\nIdentifierReference\n         |\n     Identifier\n         |\n   IdentifierName\n         |\n        'v'\n```\n\n```text\nconst v = 3;\n```\n\n```text\nStatement\n```\n\n```text\nStatementList\n```\n\n```text\nconst v = 3;\n```\n\n```text\nStatement\n```\n\n```text\nStatementList\n```\n\n```text\nconst v = 3;\n```\n\n```text\nv = 5;\n```\n\n```text\nExpressionStatement\n```\n\n```text\nStatement\n```\n\n========================================\n\nComments:\n- First: `const v = 3;` is **not** an assignment expression. It's a `const` declaration. The initialization part *looks* like an assignment expression, but it's a separate part of the grammar.\n- JavaScript grammar relies pretty heavily on keywords and tokens at the beginning of grammar constructs, and declarations are a good example of that. If the first token in the statement is `const` or `let` or `var`, then it's a declaration, not an expression statement.\n- @Pointy, thanks for pointing that out. Const declaration you're talking about seems to be part of `LexicalDeclaration: LetOrConst BindingList`. If I now the grammar it's these derivations that I should use: `StatementListItem -> Declaration -> LexicalDeclaration`. Can you post it as an answer?\n- @Pointy, and `AssignmentExpression` based on the grammar seems to be these cases `o.a = 3`, `()=>{}`. Correct?\n- Right. The answer below was given by somebody much less lazy than me :)\n- @Pointy, okay, thanks!\n- This is amazing.\n- I took the liberty of adding `MemberExpression` that was missing in you diagram. This diagram is such a great help. I'm wondering what are these tree dots `...`?\n- @AngularInDepth.com The three dots are just me not wanting to list all the derivations from `AssignmentExpression` to '5' ;-)\n- @sepp2k, I see :). But now I'm trying to see how I can get to `5` from `AssignmentExpression` and don't find a way. Can you take a look please?\n- @AngularInDepth.com You always go down the production that just goes to the next-lower rule until you arrive at PrimaryExpression.\n- @sepp2k, that's what I did. None of the productions of the `AssignmentExpression` seem to lead to the `NumericLiteral`.\n- @AngularInDepth.com Assignment -> Conditional -> LogicalOr -> LogicalAnd -> Bitwise(Or -> Xor -> And) -> Equality -> Relational -> Shift -> Add -> Mult -> Exponential -> Unary -> Update -> LHS -> New -> Member -> Primary -> Literal -> Numeric.\n- @sepp2k, thanks! that's so long, indirect and I'd say unexpected path.\n- @AngularInDepth.com That's why I just wrote \"...\" ;-) The basic idea is that the rules are order by precedence starting with the lowest precedence. Then each rule uses the one with the next-higher precedence as its base case. So, say, an additive expression is a multiplicative expression followed by zero or more additive operators+operands. So if you want to get to the lowest precedence rule (i.e. primary expression), you'll have to go through all the other operators in between.\n- @sepp2k, thanks for the hint! Indeed, just checked this operator precedence table and the order is similar to the productions if one goes upwards from the `Assignment`. Will practice parsing complex assignment expression to understand the idea better. Good luck! :)\n- @sepp2k, I'm back with new questions :). Can you please take a look at this one? And do you know if EcmaScript uses some arbitrary notation for the grammar (single semicolon, multiple semicolon, alternations specified in the new row instead of using or `|` symbol) or it's some kind of existing standard?\n- Let us continue this discussion in chat.","metadata":{"transformedAt":"2026-08-18T18:32:17.942Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":21,"totalLines":209,"estimatedTokens":1542}}1003{"id":"stack-16918612","source":"stackoverflow","questionId":16918612,"title":"Dynamically use runtime compiled assemlby","tags":["c#",".net","compiler-construction","code-injection","dynamic-language-runtime"],"text":"Title: Dynamically use runtime compiled assemlby\nTags: c#, .net, compiler-construction, code-injection, dynamic-language-runtime\nSource: Stack Overflow\n\nQuestion:\nI compile code dynamically from code:\n\n```\nstring code = @\"\n namespace CodeInjection\n {\n public static class DynConcatenateString\n {\n public static string Concatenate(string s1, string s2){\n return s1 + \"\" ! \"\" + s2;\n }\n }\n }\";\n\n // http://stackoverflow.com/questions/604501/generating-dll-assembly-dynamically-at-run-time\n Console.WriteLine(\"Now doing some injection...\");\n\n Console.WriteLine(\"Creating injected code in memory\");\n\n CSharpCodeProvider codeProvider = new CSharpCodeProvider();\n ICodeCompiler icc = codeProvider.CreateCompiler();\n CompilerParameters parameters = new CompilerParameters();\n parameters.GenerateExecutable = false;\n parameters.GenerateInMemory = true;\n //parameters.OutputAssembly = \"DynamicCode.dll\"; // if specified creates the DLL\n CompilerResults results = icc.CompileAssemblyFromSource(parameters, code);\n```\n\nThen I can invoke the method with reflection:\n\n```\nConsole.WriteLine(\"Input two strings, and I will concate them with reflection:\");\n var s1 = Console.ReadLine();\n var s2 = Console.ReadLine();\n var result = (string)results.CompiledAssembly.GetType(\"CodeInjection.DynConcatenateString\").GetMethod(\"Concatenate\").Invoke(null, new object[] { s1, s2 });\n\n Console.WriteLine();\n Console.WriteLine(\"Result:\");\n Console.WriteLine(result);\n```\n\nBut I would like to invoke something like this:\n\n```\nConsole.WriteLine(\"Input two strings, and I will concate them with dynamic type:\");\n var s1 = Console.ReadLine();\n var s2 = Console.ReadLine();\n\n dynamic type = results.CompiledAssembly.GetType(\"CodeInjection.DynConcatenateString\");\n var resultA = (string)type.Concatenate(s1, s2); // runtime error\n // OR\n var resultB = (string)CodeInjection.DynConcatenateString.Concatenate(s1, s2); // compile error (cannot find assembly)\n\n Console.WriteLine();\n Console.WriteLine(\"Result:\");\n Console.WriteLine(resultA);\n Console.WriteLine(resultB);\n```\n\nThe resultB would be better. Any ideas how to do it? I need strictly .NET 4.0, we have not updated to 4.5 yet (because the half of the team uses VS 2010). (I can invoke with reflection, I know, I am just looking for another way, because we need to test dyn. code)\n\n========================================\n\nTop Answer:\nYou're calling a `static` method. With `dynamic` you bypass the check but actually you're trying to call `Concatenate()` on the `System.Type` for `CodeInjection.DynConcatenateString`. \n\nFirst of all make it an instance method:\n\n```\npublic class DynConcatenateString\n{\n public string Concatenate(string s1, string s2){\n return s1 + \"\" ! \"\" + s2;\n }\n}\n```\n\nNow let's see your code:\n\n```\ndynamic type = results.CompiledAssembly.GetType(\"CodeInjection.DynConcatenateString\");\n```\n\nThis is a `System.Type`, not an object of type `CodeInjection.DynConcatenateString`. If you change `dynamic` to `var` you'll see the right type at compile time. You have then to create an instance of that type, like this:\n\n```\nvar type = results.CompiledAssembly.GetType(\"CodeInjection.DynConcatenateString\");\ndynamic instance = Activator.CreateInstance(type);\n```\n\nFor the syntax B there is no hope in C# because `CodeInjection.DynConcatenateString` doesn't exist at compile time then that line will fail.\n\nIf you **must** keep it static then all you can do is to use reflection to invoke that method (`dynamic` is then useless). Don't worry about performance hit...DLR isn't much faster than plain Reflection (as AFAIK it's how it's implemented with a touch of caching).\n\n========================================\n\nCode:\n```text\nstring code = @\"\n                namespace CodeInjection\n                {\n                    public static class DynConcatenateString\n                    {\n                        public static string Concatenate(string s1, string s2){\n                            return s1 + \"\" ! \"\" + s2;\n                        }\n                    }\n                }\";\n\n            // http://stackoverflow.com/questions/604501/generating-dll-assembly-dynamically-at-run-time\n            Console.WriteLine(\"Now doing some injection...\");\n\n            Console.WriteLine(\"Creating injected code in memory\");\n\n\n            CSharpCodeProvider codeProvider = new CSharpCodeProvider();\n            ICodeCompiler icc = codeProvider.CreateCompiler();\n            CompilerParameters parameters = new CompilerParameters();\n            parameters.GenerateExecutable = false;\n            parameters.GenerateInMemory = true;\n            //parameters.OutputAssembly = \"DynamicCode.dll\"; // if specified creates the DLL\n            CompilerResults results = icc.CompileAssemblyFromSource(parameters, code);\n```\n\n```text\nConsole.WriteLine(\"Input two strings, and I will concate them with reflection:\");\n                var s1 = Console.ReadLine();\n                var s2 = Console.ReadLine();\n                var result = (string)results.CompiledAssembly.GetType(\"CodeInjection.DynConcatenateString\").GetMethod(\"Concatenate\").Invoke(null, new object[] { s1, s2 });\n\n                Console.WriteLine();\n                Console.WriteLine(\"Result:\");\n                Console.WriteLine(result);\n```\n\n```text\nConsole.WriteLine(\"Input two strings, and I will concate them with dynamic type:\");\n                var s1 = Console.ReadLine();\n                var s2 = Console.ReadLine();\n\n                dynamic type = results.CompiledAssembly.GetType(\"CodeInjection.DynConcatenateString\");\n                var resultA = (string)type.Concatenate(s1, s2); // runtime error\n                // OR\n                var resultB = (string)CodeInjection.DynConcatenateString.Concatenate(s1, s2); // compile error (cannot find assembly)\n\n\n                Console.WriteLine();\n                Console.WriteLine(\"Result:\");\n                Console.WriteLine(resultA);\n                Console.WriteLine(resultB);\n```\n\n```text\npublic class StaticMethodInvoker : DynamicObject\n{\n    Type _containingType;\n\n    public StaticMethodInvoker(Type containingType)\n    {\n        _containingType = containingType;\n    }\n\n    public override bool TryInvokeMember(\n        InvokeMemberBinder binder, Object[] args, out Object result)\n    {\n        result = _containingType.InvokeMember\n            binder.Name,\n            BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public,\n            null, null, args);\n        return true;\n    }\n}\n```\n\n```text\nvar type = results.CompiledAssembly.GetType(\"CodeInjection.DynConcatenateString\");\ndynamic DynConcatenateString = new StaticMethodInvoker(type);\nstring result = DynConcatenateString.Concatenate(s1, s2);\n```\n\n```text\ndynamic\n```\n\n```text\ndynamic\n```\n\n```text\ndynamic\n```\n\n```text\npublic class DynConcatenateString\n{\n    public string Concatenate(string s1, string s2){\n        return s1 + \"\" ! \"\" + s2;\n    }\n}\n```\n\n```text\ndynamic type = results.CompiledAssembly.GetType(\"CodeInjection.DynConcatenateString\");\n```\n\n```text\nvar type = results.CompiledAssembly.GetType(\"CodeInjection.DynConcatenateString\");\ndynamic instance = Activator.CreateInstance(type);\n```\n\n```text\nstatic\n```\n\n```text\ndynamic\n```\n\n```text\nConcatenate()\n```\n\n```text\nSystem.Type\n```\n\n```text\nCodeInjection.DynConcatenateString\n```\n\n```text\nSystem.Type\n```\n\n```text\nCodeInjection.DynConcatenateString\n```\n\n```text\ndynamic\n```\n\n```text\nvar\n```\n\n```text\nCodeInjection.DynConcatenateString\n```\n\n```text\ndynamic\n```\n\n```text\npublic interface IDynConcatenateString \n    {\n        string Concatenate(string s1, string s2);\n    }\n```\n\n```text\npublic T GetInstanceOf<T>(string code, string typename)\n    {\n        Console.WriteLine(\"Now doing some injection...\");\n\n        Console.WriteLine(\"Creating injected code in memory\");\n\n\n        CSharpCodeProvider codeProvider = new CSharpCodeProvider();\n        ICodeCompiler icc = codeProvider.CreateCompiler();\n        CompilerParameters parameters = new CompilerParameters();\n        parameters.GenerateExecutable = false;\n        parameters.GenerateInMemory = true;\n        //parameters.OutputAssembly = \"DynamicCode.dll\"; // if specified creates the DLL\n        CompilerResults results = icc.CompileAssemblyFromSource(parameters, code);\n\n        //type name = \"CodeInjection.DynConcatenateString\"\n\n        T codeclass = (T)results.CompiledAssembly.CreateInstance(typename);\n\n        return codeclass;\n\n    }\n```\n\n```text\npublic void Exec() \n    {\n        string code = @\"\n            namespace CodeInjection\n            {\n                public class MyDynConcatenateString : IDynConcatenateString \n                {\n                    public string Concatenate(string s1, string s2){\n                        return s1 + \"\" ! \"\" + s2;\n                    }\n                }\n            }\";\n        IDynConcatenateString writer = GetInstanceOf<IDynConcatenateString>(\n            code, \"CodeInjection.MyDynConcatenateString\");\n\n        var s1 = Console.ReadLine();\n        var s2 = Console.ReadLine();\n        var result = writer.Concatenate(s1, s2);\n\n        Console.WriteLine(result);\n\n    }\n```\n\n```text\ndynamic DynConcatenateString = new DynamicObjects.LateType(cr.CompiledAssembly, \"CodeInjection.DynConcatenateString\");\n\nAssert.That(\"1 ! 2\", Is.EqualTo(DynConcatenateString.Concatenate(\"1\",\"2\")));\n```\n\n```text\nLateType(Assembly assembly, String typename)\n```\n\n========================================\n\nComments:\n- thank you, now I can simplify it with one static method for testing (like dynamic d = MyStaticInvoker.Create(results, \"MyClassName\"); d.UseMyFunc(with, parameters))\n- I was looking for static solution but I will also need for instances later. Thank you very much.\n- Have you tried compiling this? The problem remains: `MyDynConcatenateString` is a static class. As such it can't implement an interface and it can't be instantiated.","metadata":{"transformedAt":"2026-08-18T18:32:17.942Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":27,"totalLines":332,"estimatedTokens":2460}}1004{"id":"stack-13307142","source":"stackoverflow","questionId":13307142,"title":"How to remove a successor from a terminator instruction in LLVM","tags":["c++","c","compiler-construction","llvm"],"text":"Title: How to remove a successor from a terminator instruction in LLVM\nTags: c++, c, compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nFor a basic block I want to change conditional jump to an unconditional jump. So if a basic block had two successors I want to remove the edge to one of the successor. I want the basic block to directly jump to one of the successor. How can I do that?\n\nTo illustrate my point, I want to change\n\n```\nA\n / \\\n / \\\n B C\n```\n\nto \n\n```\nA\n \\\n \\\n C\n```\n\n========================================\n\nCode:\n```text\nA\n    /  \\\n   /    \\\n  B      C\n```\n\n```text\nA\n      \\\n       \\\n        C\n```\n\n```text\n#include \"llvm/Transforms/Utils/BasicBlockUtils.h\"\n\nBranchInst* Old = ...\nBranchInst* New = BranchInst::Create(Old->getSuccessor(X));\nReplaceInstWithInst(Old, New);\n```\n\n```text\nX\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.942Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":53,"estimatedTokens":205}}1005{"id":"stack-13790526","source":"stackoverflow","questionId":13790526,"title":"How does .NET resolve types?","tags":[".net","c#-4.0","compiler-construction","types","runtime"],"text":"Title: How does .NET resolve types?\nTags: .net, c#-4.0, compiler-construction, types, runtime\nSource: Stack Overflow\n\nQuestion:\nI am curious how .net resolves types. I know there is a compile-time component because you can't compile a program that has using statements or fully qualified types which aren't in the current assembly or a referenced assembly. I wonder if it's the compiler that throughs an error or if it's visual studio that throws a build error if it can't find a reference in the proj file? \n\nI am also wondering how .net finds the assembly at runtime. I know .net uses a JIT compiler that loads assemblies the first time it runs into a type from that assembly. I am curious about how it goes about looking for the type and the search order. Does the assembly manifest tell the runtime exactly where to look for referenced assemblies (like the ASP.NET web.config file)? Any links to blog posts, books, specs, etc would be much appreciated.\n\n========================================\n\nComments:\n- Um, Visual Studio *is* essentially your compiler. `cl` is the executable that VS executes to perform compilation.\n- @JonathonReinhart Well, there is an (occasionally useful) distinction between the C# compiler and the other components VS uses.\n- Agreed, but here he was asking if it's VS or the compiler that throws a build error. In this case `cl` probably prints it, and VS picks that up and shows it in the build errors box.\n- you can configure VS to throw build errors that are unrelated to the compilation build step. for instance a build step that returns status fail after running unit tests. that's kind of what i was talking about when i said i don't know if it's VS or the csc.exe compiler (but now i know it's the compiler in this case)\n- thank you so much for the detailed and informative answer and the link. this is exactly what I wanted to know and you put me on the right track with some other questions I had.","metadata":{"transformedAt":"2026-08-18T18:32:17.942Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":17,"estimatedTokens":485}}1006{"id":"stack-5944914","source":"stackoverflow","questionId":5944914,"title":"How to write another Debugger for .NET using CLR","tags":["c#",".net","compiler-construction","clr","debugging"],"text":"Title: How to write another Debugger for .NET using CLR\nTags: c#, .net, compiler-construction, clr, debugging\nSource: Stack Overflow\n\nQuestion:\nI want to implement another **debugger** (language) for .NET (it's just for *academic reason*, so that it can implement just a part of a language). I myself like to implement **NS2** (network Simlator 2) `script for .NET in which anybody can write ns2 script and debug it with .NET`\n\nI read this article in stackoverflow and it is far from what I'm looking for.\n\nHere is the requirement\n\n- have some predefined keywords (e.g: for, while, if ...)\n\n- check the correct form of the statements (e.g: for(start;end;counter){commands} ...)\n\n- diffferent colour for different types of statements\n\n- ability to add to any IDE (e.g: implementatin like add-in or as a dll or ...(I have no idea))\n\n- many other thing that is not necessary for now\n\nHow can I do this?\n\n**Update :** I'm not sure that you got my point, take a look at this, it is very close to what I am looking for.\n\n========================================\n\nTop Answer:\nThere is more and more interest in this area and in fact there is an active project by Microsoft Research that is looking at this on building a common infrastructure to build compiler (and debugger) for custom languages targetting .NET\n\nhttp://cciast.codeplex.com/\n\nI have used the infrastructure myself but not an expert in compiler technology. Hope this gives you a good starting point and you may find the discussion forum useful to idea with like minded people.\n\n========================================\n\nCode:\n```text\nscript for .NET in which anybody can write ns2 script and debug it with .NET\n```\n\n========================================\n\nComments:\n- @BoltClock, as I told, it's just for academic reason and I can neglect some part of it (including hard sections), I just want to get some input from user and compile|debug it with clr. is it really hard?\n- Got here wit the same bloody question for a completely different reason. I want to attach as debugger and wait for an unhandled exception and do something useful with the process state before it goes away.\n- I suppose you could use some .NET language to write a .NET app that acts as an interpreter for NS2 (rather than compiling down to CIL), but you'll still need to do your lexing and parsing...","metadata":{"transformedAt":"2026-08-18T18:32:17.942Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":47,"estimatedTokens":583}}1007{"id":"stack-8682465","source":"stackoverflow","questionId":8682465,"title":"What are the differences between the various WG14 C standards for freestanding implementations?","tags":["c","compiler-construction","standards","freestanding"],"text":"Title: What are the differences between the various WG14 C standards for freestanding implementations?\nTags: c, compiler-construction, standards, freestanding\nSource: Stack Overflow\n\nQuestion:\nHow would compilers implementing the freestanding portion of each different standard below have to differ? What would be the fewest number of modes (specified by command line flags, for example) required to support all of them?\n\n- ISO/IEC 9899:1990\n\n- ISO/IEC 9899:1990 + ISO/IEC 9899 TCOR1\n\n- ISO/IEC 9899:1990 + ISO/IEC 9899 TCOR1 + ISO/IEC 9899 AM1\n\n- ISO/IEC 9899:1990 + ISO/IEC 9899 TCOR1 + ISO/IEC 9899 AM1 + ISO/IEC 9899 TCOR2\n\n- ISO/IEC 9899:1999\n\n- ISO/IEC 9899:1999 + ISO/IEC 9899:1999 Cor. 1:2001(E)\n\n- ISO/IEC 9899:1999 + ISO/IEC 9899:1999 Cor. 1:2001(E) + ISO/IEC 9899:1999 Cor. 2:2004(E)\n\n- ISO/IEC 9899:1999 + ISO/IEC 9899:1999 Cor. 1:2001(E) + ISO/IEC 9899:1999 Cor. 2:2004(E) + ISO/IEC 9899:1999 Cor. 3:2007(E)\n\n- ISO/IEC 9899:2011\n\n========================================\n\nCode:\n```text\n-std=c90\n```\n\n```text\n-std=c95\n```\n\n```text\n-std=c99\n```\n\n```text\n-std=c11\n```\n\n```text\n-std=c1990\n```\n\n```text\n-std=c1995\n```\n\n```text\n-std=c1999\n```\n\n```text\n-std=c2011\n```\n\n```text\n<stddef.h>\n```\n\n```text\n<limits.h>\n```\n\n```text\n<float.h>\n```\n\n```text\n<stdarg.h>\n```\n\n```text\nva_copy()\n```\n\n```text\n<stdarg.h>\n```\n\n```text\n<float.h>\n```\n\n```text\n<iso646.h>\n```\n\n```text\n<limits.h>\n```\n\n```text\n<stdarg.h>\n```\n\n```text\n<stdbool.h>\n```\n\n```text\n<stddef.h>\n```\n\n```text\n<stdint.h>\n```\n\n```text\nlong long\n```\n\n```text\nint\n```\n\n```text\nint\n```\n\n```text\nmain()\n```\n\n```text\nint main()\n```\n\n```text\nint main(void)\n```\n\n```text\nmain()\n```\n\n```text\nint\n```\n\n========================================\n\nComments:\n- Did any of the changes for freestanding implementations break backwards-compatability? In other words, if I have a strictly freestanding C{1990,1995,1999} conforming program, will it necessarily compile and work as expected on a conforming C11 implementation?\n- Please update this answer \"c95\" isn't a thing according to GCC docs: gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html\n- @user894319twitter — I quoted which document was considered C95; it was the AM1 document. Back around 2011, GNU C compilers recognized C95. They may not do so now, but that isn’t a good reason to go editing this answer. Your comment acts as a footnote that 8 years later, things have changed. So does this response. If I did edit the answer, I’d simply note that in 2011, GCC recognized C95, but in 2019, it doesn’t, probably because no one used it back then. C99 was already more than a decade old and C11 was almost ready.\n- @JonathanLeffler you have to read actual GCC docs in order to learn about \"iso9899:199409\" value for ISO C90 as modified in amendment 1. It is hard to keep up with such active projects. Just leave pointers to docs","metadata":{"transformedAt":"2026-08-18T18:32:17.942Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":29,"totalLines":151,"estimatedTokens":707}}1008{"id":"stack-9462051","source":"stackoverflow","questionId":9462051,"title":"Short circuit evaluation using procedures","tags":["compiler-construction","language-design","short-circuiting"],"text":"Title: Short circuit evaluation using procedures\nTags: compiler-construction, language-design, short-circuiting\nSource: Stack Overflow\n\nQuestion:\nI am currently developing a compiler for a very limited object oriented language. I want to treat all values as objects and operators on those values will be implemented as methods. The compiler transforms the program into assembler for a stack-based virtual machine. \n\nDuring compilation I transform integer literals into objects of a special \"Integer\" class. Arithmetic operators are implemented as methods of that class (using inline assembler). So that `4 + 5` basically equals to `4.add(5)`.\n\nThe problem I am facing right now is the special case for boolean values. If there is an `if` statement:\n\n```\nif(10 > 5 || 12 this would currently be transformed into: `10.greaterThan(5).or(12.lessThan(10))`\n\nNow obviously those integer literals can also be calls to a function with side-effects. Implementing those binary operators as method calls yields a problem in this case as short-circuit evaluation gets impossible. \n\nSo my questions are:\n\nHow do other languages achieve short-circuit evaluation but still treating every value as an object? \n\nAccording to Wikipedia \"ALGOL 68 used \"proceduring\" to achieve user defined short-circuit operators & procedures.\" - How does this work?\n\n========================================\n\nTop Answer:\nIIRC ALGOL uses call-by-name parameters, and that is why that solution work. \n\nThe `||` operator can be implemented as (pseudo code):\n\n```\nif (cond1) goto label;\nif (cond2) goto label;\n\nlabel:\n \n\nnomatch:\n ...\n```\n\nFor `&&` the inverse of above can be done.\n\n========================================\n\nCode:\n```text\nif(10 > 5 || 12 < 10)\n```\n\n```text\n4 + 5\n```\n\n```text\n4.add(5)\n```\n\n```text\nif\n```\n\n```text\n10.greaterThan(5).or(12.lessThan(10))\n```\n\n```text\nor\n```\n\n```text\nif (cond1) goto label;\nif (cond2) goto label;\n\nlabel:\n  <body>\n\nnomatch:\n  ...\n```\n\n```text\n||\n```\n\n```text\n&&\n```\n\n```text\nMODE MAT = FLEX[0,0]REAL;\nOP ISZERO = (MAT a)BOOL: ¢ insert actual code here ¢ ~;\n\nPRIO TIMESF = 7;\nOP TIMESF = (MAT a, PROC MAT in b)MAT: \n  IF ISZERO a THEN a ELSE MAT b = in b; ¢ insert actual code here ¢ ~ FI;\n\nMAT a = 0, b = 16, c = 25;\nprint(a TIMESF b TIMESF c) ¢ would print 0 without calculating 16*25 ¢\n```\n\n```text\nPRIO TIMESPAR = 7;\nOP TIMESPAR = (MAT a, MAT b)MAT: ¢ insert actual code here ¢ ~;\n```\n\n```text\nPRIO TIMESSEQ = 7;\nOP TIMESSEQ = (MAT a; MAT b)MAT: ¢ insert actual code here ¢ ~;\n```\n\n```text\nMODE MAT = FLEX[0,0]REAL;\nPRIO TIMESF = 7;\nOP TIMESF = (MAT a, PROC MAT in b)MAT: \n  IF ISZERO a THEN a ELSE MAT b = in b; ¢ insert actual code here ¢ ~ FI;\n\nMAT a = 0, b = 16, c = 25; ¢ 3 1x1 matrices are \"widening\" from REAL numbers ¢\n```\n\n```text\nprint(a TIMESF MAT:b TIMESF MAT:c) ¢ would print 0 without calculating 16*25 ¢\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.942Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":14,"totalLines":122,"estimatedTokens":709}}1009{"id":"stack-70515582","source":"stackoverflow","questionId":70515582,"title":"Can the compiler I made still be called a compiler?","tags":["compiler-construction","terminology"],"text":"Title: Can the compiler I made still be called a compiler?\nTags: compiler-construction, terminology\nSource: Stack Overflow\n\nQuestion:\nThe compiler I'm making works by generating C code which later on will be compiled again to an executable file by another compiler (default is tcc).\n\nIs this still a compiler?\n\n========================================\n\nComments:\n- I would call this a \"transpiler\": a source-to-source translator.\n- @marco-a so, haxe is transpiler too?\n- yes it could be called a compiler.","metadata":{"transformedAt":"2026-08-18T18:32:17.942Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":15,"estimatedTokens":127}}1010{"id":"stack-4516474","source":"stackoverflow","questionId":4516474,"title":"How is the \">\" operator implemented (on 32-bit integers)?","tags":["x86","compiler-construction","integer","language-agnostic","comparison"],"text":"Title: How is the \">\" operator implemented (on 32-bit integers)?\nTags: x86, compiler-construction, integer, language-agnostic, comparison\nSource: Stack Overflow\n\nQuestion:\nLet's say that the environment is x86.\n\nHow do compilers compile the \">\" operator on 32-bit integers. Logically, I mean. Without any knowledge of Assembly.\n\nLet's say that the high level language code is:\n\n```\nint32 x, y;\nx = 123;\ny = 456;\nbool z;\nz = x > y;\n```\n\nWhat does the compiler do for evaluating the expression `x > y`?\n\nDoes it perform something like (assuming that x and y are positive integers):\n\n```\nw = sign_of(x - y);\nif (w == 0)\n // expression is 'false'\nelse if (w == 1)\n // expression is 'true'\nelse\n // expression is 'false'\n```\n\nIs there any reference for such information?\n\n========================================\n\nTop Answer:\nWell, logically it's *something* like that. The thing is that \"compare two integers\" is generally a primitive operation on almost any machine, so from the standpoint of the machine if just does a compare. The exact details differ from architecture to architecture, but in general you'd have something like a CMP instruction that leaves a condition code or other such control signal for \"less than\" \"equal\" and \"greater than\". So the generated code would be something like\n\n```\nLD A, X ; value of x into register A\n LD B, y ;\n CMP ; compare reg A with reg B\n```\n\nat which point the result code has the answer. If you want to *store* the result, then you use the sort of conditional you have --\n\n```\nJMPGE @FALSE ; goto label FALSE if >=\n STO Z, 0x1 ; put a hex 1 into location Z\n JMP @END ; jump to the end\nFALSE: STO Z, 0x0\nEND:\n```\n\n========================================\n\nCode:\n```c\nint32 x, y;\nx = 123;\ny = 456;\nbool z;\nz = x > y;\n```\n\n```c\nw = sign_of(x - y);\nif (w == 0)\n   // expression is 'false'\nelse if (w == 1)\n   // expression is 'true'\nelse\n   // expression is 'false'\n```\n\n```text\nx > y\n```\n\n```text\nx > y if y - x < 0\n```\n\n```text\nCMP\n```\n\n```text\n>\n```\n\n```text\n<=>\n```\n\n```text\nLD A, X    ; value of x into register A\n         LD B, y    ;\n         CMP        ; compare reg A with reg B\n```\n\n```text\nJMPGE @FALSE ; goto label FALSE if >=\n         STO   Z, 0x1 ; put a hex 1 into location Z\n         JMP   @END   ; jump to the end\nFALSE:   STO   Z, 0x0\nEND:\n```\n\n```text\nJE      Jump if equal                           ZF = 1\nJZ      Jump if zero\n\nJNE     Jump if not equal                       ZF = 0\nJNZ     Jump if not zero\n\nJB      Jump if below               unsigned    CF = 1 \nJNAE    Jump if not above or equal\nJC      Jump if carry\n\nJNB     Jump if not below           unsigned    CF = 0 \nJAE     Jump if above or equal\nJNC     Jump if not carry\n\nJBE     Jump if below or equal      unsigned    CF = 1 or ZF = 1\nJNA     Jump if not above\n\nJA      Jump if above               unsigned    CF = 0 and ZF = 0\nJNBE    Jump if not below or equal\n```\n\n========================================\n\nComments:\n- In the case of x86, I'm pretty sure there is a CPU instruction that does this, and the compiler will just use that directly. How the CPU implements it is probably more to do with bit arithmetic than any logical comparisons.\n- Some ISAs have an instruction to materialize a 0 or 1 integer from a compare result in FLAGS. Like x86 `setcc r&#47;m8` (felixcloutier.com/x86/setcc), or AArch64 `cset reg, condition`. So it can be branchless. But yes, having compare/branch and compare/set built in to the ISA makes signed comparison efficiently possible even for numbers where `x - y` would overflow if done at the same width as the inputs. e.g. the less-than condition is SF≠ OF (sign-flag != overflow-flag)\n- many errors here. Your definition does not looks recursive, it looks like a tautology. to change `x > y` to `y - x ` at all**. To get the equivalent of `` you must have a processor where cmp computes several bits at once: underflow and equality to zero (for returning -1, 0, 1) or perform the real substraction and has branching instruction depending on sign and zero. That probably explains why C does not have as it does not map well to most assemblers.\n- @kriss: Of course it is a tautology: if it wasn't, then we wouldn't be able to use it as the basis for `CMP`! As for the rest, that is the reason I used the wording 'more directly' instead of 'directly'. It doesn't map directly, but `CMP` is a heck of a lot closer to `` than to simply `>`.\n- basically CMP returns a boolean : one bit (even if quite often several booleans at once in a flag register) this boolean will usually be used by branching instruction later. As \">\" returns a boolean, but not that returns either three values, either an integer depending on languages, this instruction is clearly very, very different. It's result is not even of the same logical type! What is true is that some old assembler instructions sets does not allow for storing CMP result and the compiled program has to introduce a conditional jump to store it and that is quite inefficient.\n- Only if `y - x` is done at larger width than the inputs, because signed-overflow is possible e.g. when subtracting a negative from a positive, or subtracting a positive from a negative. On x86 for example, (with JCC to branch or SETCC to materialize a boolean: felixcloutier.com/x86/setcc , or CMOVCC to conditional-select), the signed-less-than condition is SF≠ OF .","metadata":{"transformedAt":"2026-08-18T18:32:17.942Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":142,"estimatedTokens":1340}}1011{"id":"stack-2007762","source":"stackoverflow","questionId":2007762,"title":"Compiler that recognize different-different languages and send them to their corresponding compilers. Possible?","tags":["language-agnostic","compiler-construction","programming-languages","compiler-development"],"text":"Title: Compiler that recognize different-different languages and send them to their corresponding compilers. Possible?\nTags: language-agnostic, compiler-construction, programming-languages, compiler-development\nSource: Stack Overflow\n\nQuestion:\nI was thinking whether it is possible to bridge asp.net, php and java to form a single page.\n\nActually i dont need any such thing as of now. It was just an idea that stiked to my mind as some features of some languages are good and some features or some other languages are good, so i was thinking what if i combine all these features into one \n\nI mean, I m creating a page that has code from all the 3 languages asp.net php and java.\n\n```\n\n```\n\nor\n\n```\n\n \n \n \n \n\n```\n\nor something like that complier recognize different segments of code and send them to run on their compilers to execute. And output can be recognized and used by other languages in XML \n\ni m not saying that all the languages to interact with each other. Although they can interact with each other through XML. But I only mean to say that the file is compiled as a single entity having different- different programming languages code which are send to their respective compiler to get executed and finally returning back to the parent compiler\n\nI am thinking of a compiler that can be developed whcih recognize different languages code and send them to their compiler as done by .net framework eg MSIL\n\n========================================\n\nTop Answer:\nIt's not, no. The scripts being server side, the entire file would be passed to each of the servers (asp.net/php/java) in turn and I believe the other code would cause a parse error.\n\nIt'd also be horrendously inefficient.\n\n========================================\n\nCode:\n```text\n<asp code></asp code>\n<php code></php code>\n<java code></java code>\n```\n\n```text\n<html>\n    <asp code>\n    <php code></php code>\n    <java code></java code>\n    </asp code>\n</html>\n```\n\n```text\n<html>\n    <script type=\"text/javascript\">...</script>\n    <script type=\"text/vbscript\">...</script>\n    <script type=\"text/someothersupportedscript\">...</script>\n\n    <body>\n        ...\n    </body>\n</html>\n```\n\n```text\ntype\n```\n\n```text\n<aspX>\n<asp: Textbox....../>\n<C#X>\nxyz=abc();\n</C#X>\n</asp>\n```\n\n```text\n<aspX>\n```\n\n```text\n<php>\n```\n\n```text\n<C#X>\n```\n\n========================================\n\nComments:\n- i'm usually glad there is only one turing-complete thing embedded in HTML ;)\n- Could you elaborate as to why you need this? It sounds like a recipe for an unholy mess to me, especially if you want the languages to interact with each other...\n- actually i dont need it as this time, I was just thinking if it is possible to send different section of code to different different compilers to get executed.\n- plz correct out the tag if any one understood the question. I dont know whether these tags r the best match according to the question asked\n- That's really a out-of-box thinking.\n- Look at stackoverflow.com/questions/1912191/&hellip;\n- @Timo Geusch If you r asking why i need- Actually i dont need as of now, it was just an idea that stiked to my mind as some features of some languages are good and some features or some other languages are good, so i was thinking what if i combine all these features into 1\n- i was also thinking about ajax. that could it be possible through ajax. But can it be done on a single page ? bit confused. Plz post an eg if possible\n- If you mean that you've utilised webservices written in several languages using AJAX, that's not the same as a webservice is effectively language-neutral.\n- I dont know about webservice at all. I m new to web development and only know little bit about AJAX, javascript and all these things\n- @adam : plz see my answer. I m thinking in that way, plz comment how correct or wrong I am. I m curious to know about it\n- I m taking about server side languages not client side languages. As client side languages are surely possible. That i know\n- @Darien Ford: thx for answer. I m very much satisfied with your answer. I would like to know whether a compiler can communicate with other existing compiler for sendind and receiving their code execution related request. How can I make some compiler to make understand you have to execute this piece of code and return me an output. ? for eg what i mean to say. check my answer\n- @Darien Ford : different languages can communicate only through XML only or some other common platform. If different languages interact with each other then ofcourse it will become a 100 year project for me as i m a started in programming just spent 1 year in programming on my own not in any indrustry yet. But i was thinking it as if these can be used as an scripts. Just paste a php or java or .net script that will provide me certain output.\n- @Shantanu: If you are comfortable leaving them separate, and just use their resulting html output, it will make your life easier. E.g. Assuming you do your \"shim\" in .Net, you can take the different sections of code, create a web request for each one and send them to the different servers (IIS, Apache, etc) running on your machine, giving you the resultant HTML, and then combine them to output to the user.\n- I didn't downvote, but my guess is that they feel this is more of a wish and less of an answer.","metadata":{"transformedAt":"2026-08-18T18:32:17.943Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":113,"estimatedTokens":1327}}1012{"id":"stack-51805690","source":"stackoverflow","questionId":51805690,"title":"Compiler: is implicit type conversion considered in parsing or semantic analysis?","tags":["parsing","compilation","compiler-construction","clang","semantic-analysis"],"text":"Title: Compiler: is implicit type conversion considered in parsing or semantic analysis?\nTags: parsing, compilation, compiler-construction, clang, semantic-analysis\nSource: Stack Overflow\n\nQuestion:\nIn my understanding implicit conversion is done in semantic analysis, but clang AST command `clang -Xclang -ast-dump -fsyntax-only file.cpp` produces type conversion information in AST, like these two:\n\n...\n\n`- ImplicitCastExpr 0x7fdc27050558 'int' \n\n...\n\n...\n\nImplicitCastExpr 0x7f878884c2d0 'unsigned int' \n\n...\n\nhence the question.\n\n========================================\n\nCode:\n```text\nclang -Xclang -ast-dump -fsyntax-only file.cpp\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.943Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":27,"estimatedTokens":161}}1013{"id":"stack-5349812","source":"stackoverflow","questionId":5349812,"title":"Could C++ or C99 theoretically be compiled to equally-portable C90?","tags":["c++","c","compiler-construction","c-preprocessor"],"text":"Title: Could C++ or C99 theoretically be compiled to equally-portable C90?\nTags: c++, c, compiler-construction, c-preprocessor\nSource: Stack Overflow\n\nQuestion:\nThis is a big question, so let me get a few things out of the way:\n\n- Let's ignore the fact that some C++ features cannot be implemented in C (for example, supporting pre-main initialization for any global static object that is linked in).\n\n- This is a thought experiment about what is theoretically possible. Please do not write to say how hard this would be (I know), or that I should do X instead. It's not a practical question, it's a fun theoretical one. :)\n\nThe question is: is it theoretically possible to compile C++ or C99 to C89 that is *as portable* as the original source code?\n\nCfront and Comeau C/C++ do compile C++ to C already. But for Comeau the C they produce is not portable, according to Comeau's sales staff. I have not used the Comeau compiler myself, but I speculate that the reasons for this are:\n\n- Macros such as INT_MAX, offsetof(), etc. have already been expanded, and their expansion is platform-specific.\n\n- Conditional compilation such as `#ifdef` has already been resolved.\n\nMy question is whether these problems could possibly be surmounted in a robust way. In other words, could a *perfect* C++ to C compiler be written (modulo the unsupportable C++ features)?\n\nThe trick is that you have to expand macros enough to do a robust parse, but then fold them back into their unexpanded forms (so they are again portable and platform-independent). But are there cases where this is fundamentally impossible?\n\nIt would be very difficult for anyone to categorically say \"yes, this is possible\" but I'm very interested in seeing any specific counterexamples: code snippets that could not be compiled in this way for some deep reason. I'm interested in both C++ and C99 counterexamples.\n\nI'll start out with a rough example just to give a flavor of what I think a counterexample might look like.\n\n```\n#ifdef __SSE__\n#define OP \n#endif\n\nclass Foo {\n public:\n bool operator (const Foo& other) { return false; }\n};\n\nbool f() { return Foo() OP Foo(); }\n```\n\nThis is tricky because the value of `OP` and therefore the method call that is generated here is platform-specific. But it seems like it would be possible for the compiler to recognize that the statement's parse tree is dependent on a macro's value, and expand the possibilities of the macro into something like:\n\n```\nbool f() {\n#if __SSE__\n return Foo_operator_lessthan(...);\n#else\n return Foo_operator_greaterthan(...);\n#endif\n}\n```\n\n========================================\n\nTop Answer:\nTheoretically all Turing-complete languages are equivalent.\n\nYou can compile C++ to an object code, and then decompile it to plain C or use an interpreter written in plain C.\n\n========================================\n\nCode:\n```text\n#ifdef __SSE__\n#define OP <\n#else\n#define OP >\n#endif\n\nclass Foo {\n public:\n  bool operator <(const Foo& other) { return true; }\n  bool operator >(const Foo& other) { return false; }\n};\n\nbool f() { return Foo() OP Foo(); }\n```\n\n```text\nbool f() {\n#if __SSE__\n   return Foo_operator_lessthan(...);\n#else\n   return Foo_operator_greaterthan(...);\n#endif\n}\n```\n\n```text\n#ifdef\n```\n\n```text\nOP\n```\n\n```text\ncbe\n```\n\n```text\nbool isLess(const struct Foo * left, const struct Foo * right );\n```\n\n```text\nstruct A\n{\n   virtual int method( const std::string & str );\n};\n\nstruct A\n{\n   int (*method)( struct A*, const struct string *);\n};\n\na.method( \"Hello\" );\n\n\na.method( &a, create_String( \"hello\" ) ); \n          // and take care of the pointer returned by create_String\n```\n\n```text\nint n; int array[n];\n```\n\n```text\nalloca\n```\n\n```text\nint i = UINT_MAX;\n```\n\n```text\nsetjmp\n```\n\n```text\nlongjmp\n```\n\n========================================\n\nComments:\n- It's possible, but it would not be pretty. In particular C99 has VLA's, and the only way to implement them in C90 is with `malloc`, `free`, and adding extra invisible `jmp_buf`-type arguments to every single function to handle the case where the code performs a `longjmp` out of functions containing VLA.\n- -1: Your strawman does not satisfy the requirements, because decompiled object code would most certainly not have the same semantics as the original source if you take it across platforms, since all preprocessor resolution has already happened. Also, Turing-completeness does not say anything about what kind of program transformations are possible. Clearly any program that can be written in C++ has a functional equivalent in C, but that does not *necessarily* mean that an algorithm can be written to do the translation.\n- \"it is not practical to do so, specifically for C++.\" - well, the OP specifically said that this isn't about what is *practical*, only whether it would be *possible* to do.\n- \"It just translates your c++ code into c code that can be compiled with different C backends.\" This is not true, at least according to Comeau's own sales staff. They wrote to me: \"So, in short, just having the generated C code in insufficient, as it is only targeting one platform (where platform includes not just a CPU, but OS, C compiler, etc. too)\"\n- Your C++ to C compiler could just always use the unsigned types and emulate signed comparisons.\n- @R..: indeed, integer values could be treated in the compiler as neither signed nor unsigned, just \"wraparound\", and for comparison and division you have separate signed/unsigned operations on them according to the type. Or it could use raw `char` storage for everything and implement all arithmetic on top of that. The question seems to assume that the similarities between C89, C99 and C++ affect the answer, but really they don't, and I'm just trying to present examples to dispel the idea that the way to go about it is to \"tweak the code into shape\", as it were.\n- Well I think the similarities *do* affect the answer. They're the only thing that give this sort of \"compile to C90\" approach any hope of being efficient. If you take care of the `longjmp`/exception issues and signed arithmetic issues, the rest of the \"translation\" can remain pretty straightforward, I believe.\n- +1 for the reference, but the docs advise \"The C backend has numerous problems and is not being actively maintained. Depending on it for anything serious is not advised.\" So I would not consider it a solution. Also I see no indication of whether its output is portable or not.\n- @Josh Haberman, I've used `cbe` in practice a lot, the resulting code had been portable at least in between arm, x86_32 and x86_64. The only troubles (easily fixable) I've encountered were related to vectors.","metadata":{"transformedAt":"2026-08-18T18:32:17.943Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":12,"totalLines":154,"estimatedTokens":1661}}1014{"id":"stack-1118145","source":"stackoverflow","questionId":1118145,"title":"how can a compiler that recognizes the iterators be implemented?","tags":["iterator","compiler-construction","yield-keyword"],"text":"Title: how can a compiler that recognizes the iterators be implemented?\nTags: iterator, compiler-construction, yield-keyword\nSource: Stack Overflow\n\nQuestion:\nI have been using iterators for a while and I love them.\n\nBut although I have thought hard about it, I could not figure out \"how a compiler that recognizes the iterators\" be implemented. I have also researched about it, but could not find any resource explaining the situation in the compiler-design context.\n\nTo elaborate, most of the articles about Iterators imply there is some sort of 'magic' implementing the desired behaviour. They suggest the compiler maintains a state machine in order to where the execution is (where the last 'yield return' is seen). I am especially interested in this property of Iterators that enables the lazy evaluation.\n\nBy the way, I know what state machines are, have already taken a compiler design course, studied the Dragon Book. But appearently, I cannot relate what I have studied to the 'magics' of csc.\n\nAny knowledge or differential thoughts are appreciated.\n\n========================================\n\nTop Answer:\nOne thing I would try would be to write a short example in C#, compile it, and then use Reflector on it. I think that this \"yield return\" thing is just syntax sugar, so you should be able to see how the compiler handles it in the output of the disassembler.\n\nBut, well, I don't really know much about these things so maybe I'm completely wrong.\n\n========================================\n\nCode:\n```text\nyield\n```\n\n```text\nyield\n```\n\n```text\ngoto\n```\n\n```text\nyield\n```\n\n```text\nfinally\n```\n\n```text\nyield\n```\n\n========================================\n\nComments:\n- can you explain that a little more? For example with a source code example that you think would be very difficult for a compiler to recognize?\n- and also what language you are talking about - what does \"yeild\" do, for example?\n- C# has a \"yield return\" syntax.\n- Sorry, my referance language is c#.","metadata":{"transformedAt":"2026-08-18T18:32:17.943Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":56,"estimatedTokens":494}}1015{"id":"stack-63224403","source":"stackoverflow","questionId":63224403,"title":"How to use LLVM as backend in other language (not in C++)?","tags":["compiler-construction","llvm"],"text":"Title: How to use LLVM as backend in other language (not in C++)?\nTags: compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nI'm trying to write a programming language and being stuck at code generation phase.\n\nAfter thorough consideration, I decide to use LLVM as my back-end because I don't want to deal with obscure low-level stuff (generating assembly is fine to me, but I need more knowledge on linking to accomplish my work).\n\nOne stumbling block is that my work is not on C++. It means I could not use ready LLVM classes in my code.\n\nCould I generate LLVM IR code in the form of characters string, save it to file (or no need?) and then compile it?\nIn the case I could, is there any other form that I can generate to help LLVM run faster?\n\nSpecial thanks to any advice.\n\n========================================\n\nTop Answer:\nCould I generate LLVM IR code in the form of characters string, save it to file (or no need?) and then compile it?\n\nThis approach is being used by some projects (GHC, for instance), but it is not recommended. Instead, you can use LLVM C bindings. Interfacing with C is a common feature for many languages, so it shouldn't be a problem.\n\n========================================\n\nComments:\n- Another question. In the option number 2, should I include pre-built clang compiler (with a copy of its license) in my project or ask user to install it themselves?","metadata":{"transformedAt":"2026-08-18T18:32:17.943Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":27,"estimatedTokens":349}}1016{"id":"stack-41810346","source":"stackoverflow","questionId":41810346,"title":"Why do we have to use depth-first traversal for a parse tree?","tags":["algorithm","parsing","tree","compiler-construction","antlr"],"text":"Title: Why do we have to use depth-first traversal for a parse tree?\nTags: algorithm, parsing, tree, compiler-construction, antlr\nSource: Stack Overflow\n\nQuestion:\nDuring my learning of parsing technology, it seems the parse tree is always traversed in a ***depth-first*** manner.\n\n The leftmost derivation corresponds to a preorder traversal of the\n parse tree, while the rightmost derivation corresponds to the reverse\n of a postorder traversal of the parse tree.\n [1]\n\nAnd pre-order and post-order traversals are just 2 specific types of\n depth-first tree traversal[2].\n\nI think the reason lies in the difference between a *plain tree* and a *parse tree*. A plain tree ***only*** records the topology structure among nodes, while a parse tree records more than that. A parse tree ***further implies*** that the parent node is ***built upon*** the child nodes because a parent node ***derives*** into a collection of child nodes. If we want to compute the root node of the parse tree, which is the ultimate goal of creating a parse tree, we have to compute all the prerequisites. So a depth-first traversal is a natural must.\n\nIs my understanding correct? Or is there any other scenario where other ways of traversal of a parse tree are necessary/mandatory?\n\n========================================\n\nComments:\n- The way you traverse a tree depends on exactly what you are doing while traversing. If the goal is, say, to count the nodes, any order will do.\n- While there are different ways to traverse the already built parse tree, if you want to \"execute\" a node, you do indeed need to do it bottom-up as OP says, evaluating the lower level nodes first. But as you say, that is just one possible reason for traversing a parse trees.\n- @biziclop: That depends on what you mean by \"execute\". You might be writing a lazy evaluator (as per Haskell, for example), in which case that would certainly *not* be the case.","metadata":{"transformedAt":"2026-08-18T18:32:17.943Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":25,"estimatedTokens":479}}1017{"id":"stack-45153284","source":"stackoverflow","questionId":45153284,"title":"ANTLR: How to avoid re-parsing entire file when user modifies text","tags":["c#","parsing","compiler-construction","antlr","antlr4"],"text":"Title: ANTLR: How to avoid re-parsing entire file when user modifies text\nTags: c#, parsing, compiler-construction, antlr, antlr4\nSource: Stack Overflow\n\nQuestion:\n**edit:** For those interested/who want to see exactly what I'm doing, the source code of my app can be found here.\n\nI'm building a code editor app with C# that offers syntax highlighting. I'm currently using ANTLR for C# to parse the code in order to highlight it. So far, my app can highlight the code really fast when the user initially opens the file. However, I haven't written any code to re-highlight the text when the user starts editing it.\n\nI want the editor to perform well for large files, so I don't want to re-parse the entire file each time the user types a character. I did a bit of research, and it seems like what I'm looking for is an incremental parser. Unfortunately, it seems like ANTLR v4 can't do incremental parsing, so I'm unsure what to do.\n\nMy question is: is there another approach I can take, using ANTLR, to not freeze the app whenever the user types? I'm really hesitant to give up on ANTLR since there are a bunch of free grammars available for it, so it's not much work to add support for a new language. I've looked into TextMate grammars, VSCode uses lots of them, but I don't understand them and there are no C# libraries available to manipulate them.\n\nThanks for helping!\n\n========================================\n\nTop Answer:\n@JLH basically stated the approach I'm using as well but I wanted to add some things you might want to watch out for:\n\nFirst fo all I would do the parsing in a different Thread than the UI thread of your editor in order to prevent the situation in which the parser started at the same time the user decided to continue coding. If you are using a different thread the user can continue coding without even noticing that the parser is running in the background. of course you need to use some sort of mechanism that will either cancel the parsing in such a situation or at least don't do anything in the editor with the produced parse tree as it is already obsolte. \n\nThe next thing is that I have found is that ANTLR can get really slow on medium sized files (~100 lines). And with really slow I mean that the parsing can take up to 20 seconds! That can be prevent by switchung ANTLR's default parsing algorithm to `SLL` and using a BailoutStrategy so that it doen't try to recover from syntax errors. If that occurs you have to parse the whole thing again with the normal `LL` mode in order to check if it is really a syntax error or just something that `SLL` can't deal with.\n\nWith that approach I reduced the parse time in some of my files from ~20s to ~1s. \n\nYou can habe a look at a working (for sure not perfect) implementation of this in the source code of mine (though it's in java but the principle should be the same).\n\n========================================\n\nCode:\n```text\npublic override void Recover(Parser recognizer, RecognitionException e)\n    {\n        IToken token = recognizer.CurrentToken;\n        string message = string.Format(\"parse error at line {0}, position {1} right before {2} \", token.Line, token.Column, GetTokenErrorDisplay(token));\n        BasicEnvironment.SyntaxError = message;\n```\n\n```text\nSLL\n```\n\n```text\nLL\n```\n\n```text\nSLL\n```\n\n========================================\n\nComments:\n- Are you sure you even need to parse the file for syntax highlighting? Often all you need for syntax highlighting is the token stream, which is not only faster to calculate, but it's also easier to just re-tokenize a small part of a file. This isn't enough if you want to use semantic information like if you want to highlight instance variables differently than local variables for example. But if all you want is one color for Identifiers, another for this set of keywords etc., token-based highlighting is easier and more performant.\n- Are you aware of Roslyn ?\n- @sepp2k You're correct, I could just tokenize the source code if I wanted all identifiers to be the same color, but then the code will look almost monotone. I want to color type names differently than local variable identifiers, for example.\n- @GuyCoder Yes, that's what I'm going to use to highlight C# in fact. However, that doesn't help me highlight other languages like Java (I'm *writing* the app in C#, but I'm not only dealing with C# files).\n- JLH, thanks so much for your answer. I thought I'd hit an impasse with this issue, but you've made me see that there are alternatives to incremental parsing. One question-- what was your value for n? Also does m = 400?\n- @JamesKo I edited my answer with the current values. Had to check to refresh my memory. ;) Hope this is adequate for an \"Accept\" Cheers.\n- JLH, I will accept your answer in a few days. Since this is such a broad question, I don't want to deter other people from answering. I am deeply grateful for your answer, however, which is why I've already upvoted you.\n- JLH, just curious why did you choose to trigger a re-parse once the user enters a line terminator/separator?\n- @JamesKo Only for line-oriented languages. Again, in real use, the timer usually kicks off the parse. But line ending? Think assembly language or other line-oriented grammar. Line terminator is a perfect time to parse.\n- Raven: Thanks for the extra info on switching the parsing algorithm, I had no idea about that problem (I haven't tested much with files that contain grammatical errors yet). I have one question-- what mechanism can I use to \"cancel parsing\"? .NET has a `CancellationToken` type that allows you to cancel an asynchronous method before it's finished, and I think that'd be just what we're looking for here. However, since ANTLR is written in Java (and ANTLR for C# mirrors the API of the original), I don't think the parser supports cancellation like that. Have you come up with such a mechanism?\n- Nope I haven*'t found a way of cancelling the ANTLR parser. What I do is setting a flag in the parsing thread and after ANTLR has finished it's parsing I check that flag and if it is true I don't process the parseTree. It's not the best solutionbut it does the job...\n- @JamesKo Perhaps you should look into the Bailout strategy or a custom lexer that tolerates no errors and stops at the first one. Search here on SO or ask a specific question. I use these techniques in my domain-specific languages to avoid continuation of lexing/parsing after even the tiniest error.","metadata":{"transformedAt":"2026-08-18T18:32:17.943Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":66,"estimatedTokens":1613}}1018{"id":"stack-68843440","source":"stackoverflow","questionId":68843440,"title":"Is the type of variable stored somewhere? Where is it?","tags":["c","compiler-construction","typeinfo"],"text":"Title: Is the type of variable stored somewhere? Where is it?\nTags: c, compiler-construction, typeinfo\nSource: Stack Overflow\n\nQuestion:\nFor example,\n\n```\nfloat f = 2.4;\nint n = f + 1;\n```\n\nn = 3\n\nvariable f is 0x4019999a in memory,\nSo I thought f + 1 = 0x4019999a + 1\nbut Computer doesn't.\nHow to know f is 'float'? Even if f is just 0x4019999a in memory.\nIs the type of variable stored somewhere?\n\n========================================\n\nTop Answer:\nTypes of variables are not stored. The compiler emits the machine code instructions accordingly to the code you write.\n\nIn your example:\n\n```\nfloat f = 2.4;\nint n = f + 1;\n```\n\n`float` value `2.4` is being converted to `int` and its value is `2`. `1` is added to the integer value `2`. The result is `3`. The physical representation of `f` in memory does not matter.\n\nIf you want to add `1` to the physical memory representation of the `f` treated as an unsigned integer you need to:\n\n```\nfloat f = 2.4f;\nunsigned n,m;\n\nmemcpy(&n, &f, sizeof(n));\nm = n + 1;\n```\n\n========================================\n\nCode:\n```text\nfloat f = 2.4;\nint n = f + 1;\n```\n\n```text\nf = f + 1\n```\n\n```text\nn = n + 1\n```\n\n```text\nf\n```\n\n```text\nn\n```\n\n```text\nfloat f = 2.4;\nint n = f + 1;\n```\n\n```text\nfloat f = 2.4f;\nunsigned n,m;\n\nmemcpy(&n, &f, sizeof(n));\nm = n + 1;\n```\n\n```text\nfloat\n```\n\n```text\n2.4\n```\n\n```text\nint\n```\n\n```text\n2\n```\n\n```text\n1\n```\n\n```text\n2\n```\n\n```text\n3\n```\n\n```text\nf\n```\n\n```text\n1\n```\n\n```text\nf\n```\n\n```text\nfloat f = 2.4;\nint n = f + 1;\n```\n\n```text\nmovss   LCPI0_0(%rip), %xmm0    ## xmm0 = mem[0],zero,zero,zero\nmovss   LCPI0_1(%rip), %xmm1    ## xmm1 = mem[0],zero,zero,zero\n...\nmovss   %xmm1, -8(%rbp)\naddss   -8(%rbp), %xmm0\ncvttss2si   %xmm0, %eax\nmovl    %eax, -12(%rbp)\n```\n\n```text\n*\n```\n\n```text\n+\n```\n\n```text\naddss\n```\n\n```text\nint\n```\n\n```text\naddl\n```\n\n```text\nint\n```\n\n```text\nn\n```\n\n```text\n3.4\n```\n\n```text\n3\n```\n\n```text\nmovss\n```\n\n```text\n2.4\n```\n\n```text\n1.0\n```\n\n```text\n%xmm0\n```\n\n```text\n%xmm1\n```\n\n```text\n%xmm1\n```\n\n```text\n2.4\n```\n\n```text\n-8(%rpb)\n```\n\n```text\nf\n```\n\n```text\n%xmm0\n```\n\n```text\n1.0\n```\n\n```text\n%xmm0\n```\n\n```text\n3.4\n```\n\n```text\n3\n```\n\n```text\ncvttss2si\n```\n\n```text\n%eax\n```\n\n```text\nn\n```\n\n```text\n-12(%rbp)\n```\n\n```text\n1\n```\n\n```text\n0x00000001\n```\n\n```text\n1.0\n```\n\n```text\n0x3f800000\n```\n\n========================================\n\nComments:\n- Once the compiler is finished, the types are effectively lost. They can be deduced by examining the assembly instructions generated by the compiler, but the types themselves aren't stored anywhere.\n- While the compiler is doing its thing, it keeps information about the types of identifiers in an internal table (that does not necessarily get outputted to the produced binary). With `gcc` you can try adding debugging options to the generation of the binary.\n- Everything in the computer is a number. What is means is from its *context*. At processor level, many arithmetic instructions do not know whether a number is signed or unsigned, but the programmer uses different *flags* that it sets. In your example, the compiler generates the appropriate instructions, so the data type is inherent in that.\n- duplicates: How and where in memory does the compiler store the information about what type a variable is?, Variable types in C and who keeps track of it, how data types are processed by Compiler, where the info about type is stored and how type safety check is performed?\n- Does this answer your question? how data types are processed by Compiler\n- I actually wanted to create on a TypeInfo Engine that would take preprocessed output and create a file that contains the type info, but if you want something more simple for your projects you can simply use macros for defining your variables like: `#define define(type, name) const char* name ## Type = #type; type name` and use it like this: `define(int, foo) = 5; printf(\"%s foo = %d\", fooType, foo);`","metadata":{"transformedAt":"2026-08-18T18:32:17.943Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":50,"totalLines":269,"estimatedTokens":981}}1019{"id":"stack-32857081","source":"stackoverflow","questionId":32857081,"title":"Does a language compiler use a complex DFA to accept programs?","tags":["compiler-construction","automata"],"text":"Title: Does a language compiler use a complex DFA to accept programs?\nTags: compiler-construction, automata\nSource: Stack Overflow\n\nQuestion:\nI am reading up on Theory of Computation. And I have no practical experience of programming compiler.\n\nSo it occurred to me, does a C or Java compiler use a huge DFA to Validate a program (String in TOC parlance)?\n\nAre compilers practical implementations of DFA?\n\n========================================\n\nTop Answer:\nIt's rare to find a language pure enough that a simple two-phase DFA scan / PDA parse will actually correctly create a parse tree. There seems to always be a temptation to add a syntactic construct which can only be parsed using a Turing-complete formalism. So in practical compilers, there will be places where the potentially elegant theoretical model has small holes drilled into it with spaghetti threaded through them.Of course, a DFA will only take you so far (up to a regular language, in fact). No practical programming languages can be described by a regular expression, since regular expressions cannot handle recursive structures like parenthesized expressions or nested control-flow blocks. So the DFA, if any, will only be used to break the input into a sequence of tokens. The tokens will then be parsed by some kind of pushdown automaton, or by a recursive descent parser, or by pure black magic on the part of a coder. Again, the PDA (if any) may well be generated automatically, using a tool like bison, ANTLR, and many others.\n\n========================================\n\nComments:\n- maybe if anyone feels it should go to some other stackexchange site, please migrate it. @Admin\n- Some are, some aren't. I've written both kinds, they both exist. DFA only applies at the lexical analysis phase: you need DPDA for parsing.\n- nice explanation, and it piqued my interest, what discipline of engineering mathematics is used other than Graph Theory?\n- @Spandan: formal language theory\n- \"or a very large etc\" I think you're missing a","metadata":{"transformedAt":"2026-08-18T18:32:17.943Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":501}}1020{"id":"stack-35939139","source":"stackoverflow","questionId":35939139,"title":"Writing a COFF object file... how to reference external symbols","tags":["compiler-construction","language-agnostic","coff"],"text":"Title: Writing a COFF object file... how to reference external symbols\nTags: compiler-construction, language-agnostic, coff\nSource: Stack Overflow\n\nQuestion:\nI'm going to write my first compiler (ok... most of the work was done by using ANTLR, but it still required effort)... I have already done the following things:\n\n- Implemented a working interpreter for very, very simple calculations (+, -, *, /, ^ - with proper precedence, of course, which was not that hard to implement)\n\n- Created a basic assembler code emitter (which was actually almost easier than writing the interpreter, because I used a rather simple \"stack\" based approach to handle the calculations - like: \"push var1, push var2, add\" - which could be then easily transformed into simple mov / add instructions\n\n- Was able to assemble to .exe (using ml64) and execute the resulting executable\n\nNow my question is - if I want to create the COFF object files myself, how do I reference external symbols in libraries / other object files?\n\nI mean, for example (if I want to call printf) it works like this, when I do it in ml64:\n\n```\nlea rcx, \"address of string\"\ncall printf\n```\n\nBut what exactly do I write into the COFF file instead of \"printf\"?\n\nEverything else is not THAT hard to implement, but I have absolutely no idea how to go about referencing libraries / other object files... hm...\n\nEdit: fixed formatting (sorry) + clarifications\n\n========================================\n\nCode:\n```text\nlea rcx, \"address of string\"\ncall printf\n```\n\n```text\n0x00\n```\n\n```text\n0xe8 0x00 0x00 0x00 0x00\n```\n\n```text\n0x00\n```\n\n========================================\n\nComments:\n- Thank you... I seriously wonder why people who write specifications for file formats or API documentation don't always provide simple examples. Your two sentences answered question perfectly for me and should be part of COFF specification documents. :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.943Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":52,"estimatedTokens":474}}1021{"id":"stack-18880618","source":"stackoverflow","questionId":18880618,"title":"What are the subphases of the semantics analysis compiler phase?","tags":["java","c++","c","compiler-construction"],"text":"Title: What are the subphases of the semantics analysis compiler phase?\nTags: java, c++, c, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI took an interest in finding out how a compiler really works. I looked through several books and all of them agree on the fact that the compiler phases are roughly as this(correct me if I'm wrong): lexical analysis, syntax analysis, semantic analysis, intermediate code, code optimization, code generation. The lexical and syntax phases look pretty clear and straightforward as methods(but this does not mean easy of course). However, I'm still not able to find what the semantic phase really consist of. For one, I know that there should be some subphases like scope checking, declaration checking and type checking but question that has been bothering me is: are there other things that have to be done. Can you tell me what are the mandatory steps that have to taken during this phase. I know this strongly depends on the programming language and the compiler implementation but could you give me some examples concerning C/C++, Java. And could you please point me to a book/page/article where can I read those things in-depth. Thanks.\n\n**Edit:**\nThe books I look through were \"Compilers: Principles, Techniques, and Tools\",Aho and \"Modern Compiler Design\", Grune, Reeuwijk. I haven't been able to answer this question using them. If you find this question too broad could you please give an answer considering an compiler implementation of your choice for either C,C++ or Java.\n\n========================================\n\nTop Answer:\nAs already mentioned by templatetypedef, semantic analysis is language specific. For C++ it would among other things involve what template instantiations are required (the C++ language tends towards more and more semantic analysis), and for Java there would need to be some checked exception analysis.\n\nEven for C the GNU C compiler can be configured to check arguments given to string-interpolations. I guess there are hundres of semi semantic analysis-related options for GCC to choose from. If you are doing a paper on the subject, you could spend an afternoon counting them :)\n\nBesides availability, I find that the semantic analysis is what differentiates the statically typed imperative object-oriented languages of today.\n\n========================================\n\nComments:\n- This is a really interesting question, but I think it's too broad to post here. Different languages have different requirements in semantic analysis - for example, some languages like Scheme might not even have a semantic analysis phase and defer all checks to runtime, while other languages like Haskell have enormously complicated rules to during semantic analysis. You might want to rephrase this question to be a bit more targeted so that you can get better feedback, even if that means asking several similar questions.\n- I had the fear that might indeed be a broad question and that's why I mentioned C/C++/Java (or imperative and object-oriented languages) as a main focus. Correct me if I'm wrong but I think that those language should similar traits. If not do you think is still a good idea to post multiple questions for every one of them?\n- It is my understanding that lexical is the same thing as syntax. Are you sure that \"intermediate code\" is a phase?\n- Lexical is different from syntax. Lexical refers to tokenization (characters -> tokens) and is done by a scanner. Syntax refers to parsing (tokens -> parse tree) and is done by a parser. Of course, there do exist scannerless front-ends but even these have some notion of tokens vs. syntax categories.\n- Is there somewhere a list with the things that have to be checked? Let's say for GCC.\n- I will not be the judge of what *has* to be checked. But the options you can give for GCC are found in its documentation. Say gcc.gnu.org/onlinedocs/gcc-4.8.1/gcc/&hellip; In particular, the section about warning options: gcc.gnu.org/onlinedocs/gcc-4.8.1/gcc/&hellip; many of the warnings relate to the semantics of the code.\n- Can you recommend some book? Thanks.\n- The standard response is \"Compilers\" by Aho/Ullman/Sethi, aka \"the Dragon book\". Easily found at Amazon and other technical book stores.","metadata":{"transformedAt":"2026-08-18T18:32:17.943Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":30,"estimatedTokens":1059}}1022{"id":"stack-18058937","source":"stackoverflow","questionId":18058937,"title":"How do compilers know when not to recompile?","tags":["c++","compiler-construction"],"text":"Title: How do compilers know when not to recompile?\nTags: c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nHow do compilers know when it is not necessary to recompile certain parts of code especially in larger projects?\n\nFor example, let's say in C++ we have two C++ files and two header files. The header files depend on one another. (They use the classes specified in each others files.)\n\nDoes a compiler always need to parse both header files, (and maybe C++ files for method implementation,) to obtain the class information in order to generate either of the two C++ files?\n\nI always thought that when you run the compiler at the command prompt, it closes immediately after outputting the object files - so it would be impossible to cache the Abstract Syntax Trees or intermediate code. Do most C++ compilers know when a certain file doesn't need to output to an object file, and is therefore skipped?\n\n========================================\n\nTop Answer:\nAs said above, compilers will compiler every file that it is asked to compile. It is up to tools like make to decide what needs to be compiled.\n\nIn `make` one sets up rules. Each rule has a target, list of dependencies followed by the commands to run if those dependencies are not met. For example\n\n```\ntarget.o : target.c\n gcc -c -o target.o target.c\n```\n\nOn most file systems, each file has a timestamp. If target.o has a newer timestamp than target.c (the rule dependency) then make does not run the gcc command below. This is because one firsts edits a source file and then compiles the source file into an object file.\n\nIf however the dependent source file is newer than the target, then we know the source file was edited after the compile took place and another compile is in order. `make` will therefore execute the build command for the rule.\n\nIt gets a lot more complex when rules are dependent on other rules but the same principle applies.\n\n========================================\n\nCode:\n```text\ntarget.o : target.c\n    gcc -c -o target.o target.c\n```\n\n```text\nmake\n```\n\n```text\nmake\n```\n\n========================================\n\nComments:\n- This is typically the build system's job (e.g., make), not the compiler's.\n- This is the job of e.g. make with the help of a MakeFile\n- Normally it's the Make that does the checks, and it's quite complex to tell the make which files depend on which other files...\n- @xanatos It may be make, or it may be some other tool; the generic name, I think, is the build system. And good build systems collaborate with the compiler, which \"tells\" them which headers are included each time. (How well they collaborate depends, but I've gotten the VC++ compiler to tell GNU make about the dependencies.)\n- I've never heard of a compiler or a build system doing it this way. The usual build systems depend on compiler output to determine dependencies, and the time stamps on the file to determine which files actually need recompiling.\n- Sounds like `ccache` actually. Distributed builds are a bit harder than local builds, if only because of clock synchronization.","metadata":{"transformedAt":"2026-08-18T18:32:17.943Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":56,"estimatedTokens":771}}1023{"id":"stack-23618268","source":"stackoverflow","questionId":23618268,"title":"Code generator for expressions using Sethi-Ullman algorithm","tags":["c","algorithm","parsing","compiler-construction","code-generation"],"text":"Title: Code generator for expressions using Sethi-Ullman algorithm\nTags: c, algorithm, parsing, compiler-construction, code-generation\nSource: Stack Overflow\n\nQuestion:\nGive a AST tree, I want to generate an assembly-like language. I'm trying using Sethi-Ullman algorithm but I have some questions in the algorithm implemetation.\n\n### What should I do when I run out of registers?\n\ncurrently I do the following:\n\nEmit a `push REG` where `REG` is the register of right subtree, evaluate left subtree, get one free register assign as register of right subtree and then emit a `POP REG` operation where `REG` is the register of right subtree too.\n\n### How should I implement the function to get a free register? currently I'm using an implementation like this instead of a stack-based:\n\n```\nenum Reg { Reg_r0, Reg_r1 };\nReg regs[] = { Reg_r0, Reg_r1 };\n Reg getreg() {\n static int c;\n if(c == sizeof(regs) / sizeof(int))\n c = 0;\n return regs[c++];\n }\n```\n\nHere's a pseudo-code (from C-language) how to implement it from what I unsertood(including the `label()` function)\n\n```\n// K is the number of registers available\nint K = 2;\nvoid gen(AST ast) {\n if(ast.left != null && ast.right != null) {\n int l = ast.left.n;\n int r = ast.right.n;\n \n if(l >= K && r >= K) {\n gen(ast.right);\n ast.n -= 1;\n emit_operation(PUSH, ast.right.reg);\n gen(ast.left);\n ast.reg = getreg();\n emit_operation(POP, ast.right.reg);\n } else if(l >= r) {\n gen(ast.left);\n gen(ast.right);\n ast.n -= 1;\n } else if(l **EDIT:** Tell me please if more context is needed to understand this.\n\n========================================\n\nCode:\n```text\nenum Reg { Reg_r0, Reg_r1 };\nReg regs[] = { Reg_r0, Reg_r1 };\n    Reg getreg() {\n             static int c;\n        if(c == sizeof(regs) / sizeof(int))\n        c = 0;\n        return regs[c++];\n    }\n```\n\n```text\n// K is the number of registers available\nint K = 2;\nvoid gen(AST ast) {\n    if(ast.left != null && ast.right != null) {\n        int l = ast.left.n;\n        int r = ast.right.n;\n        \n        if(l >= K && r >= K) {\n            gen(ast.right);\n            ast.n -= 1;\n            emit_operation(PUSH, ast.right.reg);\n            gen(ast.left);\n            ast.reg = getreg();\n            emit_operation(POP, ast.right.reg);\n        } else if(l >= r) {\n            gen(ast.left);\n            gen(ast.right);\n            ast.n -= 1;\n        } else if(l < r) {\n            gen(ast.right);\n            gen(ast.left);\n            ast.n -= 1;\n        }\n        \n        ast.reg = getreg();\n        Reg r1 = ast.left.reg;\n        Reg r2 = ast.right.reg;\n        emit_operation(ast.type, r1, r2);\n    } else if(ast.type == Type_id || ast.type == Type_number) {\n        ast.n += 1;\n        ast.reg = getreg();\n        emit_load(ast);\n    } else {\n        print(\"gen() error\");\n        // error\n    }\n}\n\n// ershov numbers\nvoid label(AST ast) {\n    if(ast == null)\n        return;\n    \n    label(ast.left);\n    label(ast.right);\n    \n    if(ast.type == Type_id || ast.type == Type_number)\n        ast.n = 1;\n    // ast has two childrens\n    else if(ast.left not null && ast.right not null) {      \n        int l = ast.left.n;\n        int r = ast.right.n;\n        \n        if(l == r)\n            ast.n = 1 + l;\n        else\n            ast.n = max(1, l, r);\n    }\n    // ast has one child\n    else if(ast.left not null && ast.right is null)\n        ast.n = ast.left.n;\n    else\n        print(\"label() error!\");\n}\n```\n\n```text\npush REG\n```\n\n```text\nREG\n```\n\n```text\nPOP REG\n```\n\n```text\nREG\n```\n\n```text\nlabel()\n```\n\n```text\ntypedef unsigned RegSet; /* using a simple bitmask for a set -- assuming that\n                          * unsigned is big enough to have a bit per register */\n\nvoid gen(AST *ast, RegSet in_use) {\n    if(ast->left != 0 && ast->right != 0) {\n        if (ast->left->n >= ast->right->n) {\n            gen(ast->left, in_use);\n            gen(ast->right, in_use | (1 << ast->left->reg));\n        } else {\n            gen(ast->right, in_use);\n            gen(ast->left, in_use | (1 << ast->right->reg)); }\n        ast->reg = ast->left->reg\n        emit_operation(ast->type, ast->left->reg, ast->right->reg);\n    } else if(ast->type == Type_id || ast->type == Type_number) {\n        ast->reg = pick_unused_register(in_use);\n        emit_load(ast);\n    } else ....\n```\n\n```text\nvoid gen(AST *ast, RegSet in_use) {\n    if(ast->left != 0 && ast->right != 0) {\n        Reg spill = NoRegister; /* no spill yet */\n        AST *do1st, *do2nd;     /* what order to generate children */\n        if (ast->left->n >= ast->right->n) {\n            do1st = ast->left;\n            do2nd = ast->right;\n        } else {\n            do1st = ast->right;\n            do2nd = ast->left; }\n        gen(do1st, in_use);\n        in_use |= 1 << do1st->reg;\n        if (all_used(in_use)) {\n            spill = pick_register_other_than(do1st->reg);\n            in_use &= ~(1 << spill);\n            emit_operation(PUSH, spill); }\n        gen(do2nd, in_use);\n        ast->reg = ast->left->reg\n        emit_operation(ast->type, ast->left->reg, ast->right->reg);\n        if (spill != NoRegister)\n            emit_operation(POP, spill);\n    } else ...\n```\n\n```text\ngen\n```\n\n```text\nin_use\n```\n\n```text\nn\n```\n\n```text\nn\n```\n\n========================================\n\nComments:\n- You don't seem to need `Reg l, r` or `return l`, since the function has return type `void` and you never set `l` or use `r`.\n- @rici: Leftovers from before I realized that the OP is storing the registers allocated in the AST. An alternative implementation would have `gen` return the register allocated for the result and not need the `reg` field in `ast`.\n- Thanks very much! helped a lot! So `label()` do same job as `SethiUllman()` function? I was confusing because from where I get this it was named something like \"register allocation for trees\"... and I assumed `label()` to set `n` and `SethiUllman()` to generate the code. I will remove `reg` member and return in `gen()` function instead of as you mentioned.\n- I have another question I can't figure it out myself: how do I make sure to resulting expression value be always in same register? no matter how complex is the AST. I mean, to a tree from expression `2 + (2 + 2)` the result is in `R1` and not `R0`. The assembly-like code from this is something like this: LOAD R0,2 ` LOAD R1,2 ADD R0,R1 LOAD R1,2 ADD R1,R0 `\n- The code above assumes the destination of a binary op is the same register as the left operand (thus the `ast->reg = ast->left-reg` assignment). So while the final result ends up in R1, its labelled as being in R1.\n- The code above is right. However, final result might be labelled as begin in R0 but actual is in R0. I wanted to swap it but it's topic for another thread. Thanks again. It helped a lot.\n- If you want gen to have the result end up in a particular register, you could add another argument to it (dest register) and arrange your register choices so that works out.\n- @ChrisDodd: Shouldn't be `ast->reg = do1st->reg` instead of `ast->reg = ast->left->reg` ?\n- No -- I'm assuming you are generating code for a CPU where the dest and left src must be the same register (such as x86, or just about any 2-register ISA, rather than a 3-register ISA)","metadata":{"transformedAt":"2026-08-18T18:32:17.943Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":13,"totalLines":223,"estimatedTokens":1804}}1024{"id":"stack-6407410","source":"stackoverflow","questionId":6407410,"title":"Comparison of two Java classes","tags":["java","compiler-construction","syntax","semantics"],"text":"Title: Comparison of two Java classes\nTags: java, compiler-construction, syntax, semantics\nSource: Stack Overflow\n\nQuestion:\nI have two java classes that are very similar in semantics but differ in syntax. The differences are minor, like - \n\nChanges in variable names,\n\nChanges in position of some statements (with no dependent lines in between),\n\nExtra imports, etc.\n\nI need to compare these two classes to prove that they are indeed semantically identical. The same needs to be done for a large number of java file pairs.\n\nThe first approach of reading from the two files and comparing the lines, with logic to deal with the differences mentioned above seems inefficient. Is there some other way that I can achieve this task? Any helpful APIs out there?\n\n========================================\n\nTop Answer:\nIf you want to examine the changes in the code try Araxis Merge or WinMerge.\n\nBut if you want **logical** differences, I am afraid you might have to do it manually.\n\nI would advise to use one of these tools to look for textual changes and then look for logical differences.\n\n========================================\n\nCode:\n```text\npublic String m1(String a, int b) { ... }\n```\n\n```text\npublic String m2(String x, int y) { ... }\n```\n\n```text\nm1\n```\n\n```text\nb\n```\n\n```text\na\n```\n\n```text\nm2\n```\n\n```text\ny\n```\n\n```text\nx\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.943Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":60,"estimatedTokens":334}}1025{"id":"stack-7060604","source":"stackoverflow","questionId":7060604,"title":"C# csc compile source code from memory","tags":["c#","compiler-construction"],"text":"Title: C# csc compile source code from memory\nTags: c#, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI would like to compile c# *source code* from a string. I know this is possible using CodeDom, and I would like to know if it's possible to do this using the command line compiler. For example, say I load the following code onto a string:\n\n```\nstatic void Main(string[] args)\n { \n\n }\n```\n\nIs there some way to compile this code into a c# executable (.exe) file **without** first writing this code to my hard drive in the form of a C# class (.cs) file?\n\nThat was the first part of my question. The second part assumes that the above is in fact impossible. Let's say I had three difference classes (loaded onto three separate strings). How would I go about using the Command line compiler to compile each of these three classes into one common executable file? I have found some examples online, but they seem to be very vague, unfortunately.\n\nAny help, or pointers in the right direction is much appreciated.\n\nThank you,\nEvan\n\n========================================\n\nTop Answer:\nI was looking to see if you could direct CSC to compile from STDIN but I can't seem to see that, so I think Part one isn't likely to be the case.\n\nHowever, part two is easier.\n\na command such as\n\n```\ncsc /out:test.exe *.cs\n```\n\nCompiles all .CS files in the current directory into test.exe.\nYou can find other examples here:\n\nhttp://msdn.microsoft.com/en-us/library/78f4aasd.aspx\n\nEdit: I just thought of a horrible hack that might help. This is horrible however!\nWhat about creating your file as some kind of UNC/Service url. My thinking is that you could associate a NetworkStream to a UNC or an endpoint of some HttpHandler or webservice; once read from you would simply return the string to the stream.\n\nThis is really hacky though and I have no idea if it'll work!\n\n========================================\n\nCode:\n```text\nstatic void Main(string[] args)\n        {           \n\n        }\n```\n\n```text\nSystem.CodeDom.Compiler.CompilerParameters\n```\n\n```text\nAnyCPU\n```\n\n```text\nx86\n```\n\n```text\ncorflags\n```\n\n```text\ncsc /out:test.exe *.cs\n```\n\n========================================\n\nComments:\n- Why do you want to use the command line compiler instead of the compiler classes?\n- I don't want to use CodeDom because it has been causing a few bugs lately. Also, I'm slightly curious at this point.\n- Use CodeCompiler.FromSource(). It takes a string.\n- @Hans - is this not part of CodeDom? I want to use CSC.exe\n- Color me surprised, it is the System.CodeDom.CodeCompiler class. There has to be a real question behind that comment, I can't see it yet.\n- I stated in my question that I did not want to use CodeDom. Why the attitude? I did not mean anything by my comment.\n- Attitude does seem to be the problem, System.CodeDom was *made* to do what you want. It uses csc.exe, there's only one C# compiler. Why don't you want to use it?\n- @Hans: I think you have that backwards: csc.exe uses CodeDOM.\n- @Ben Why not just make your suggestion an answer? It solved my problem... Also, don't bother with Hans, it seems no matter what you tell him he's going to have something to say about it. Wasting your breath.\n- @Evan: I added it as a clarification on my existing answer.\n- Do you know how I can add embedded resources to test.exe?\n- Yes, well kind of; you would add /res:myResource.ext - to clarify, I think this is how you do it, I can't test as I'm on my mac at the moment.\n- Just updated my answer with a horribly hacky suggestion; I would love to know if it works!\n- Aww :( I did answer the posted question!\n- grr I know - but his answer truly did solve my problem in the end. Sorry\n- Yes, I have a project which uses pointers, and invokes data. For some reason when I compile this source using CodeDom, the output fails on Windows 7 64 bit. However, when I compile the EXACT source code using Visual Studio, the program works on windows 7 64 bit.\n- @Evan: Maybe one is using `AnyCPU` and the other is `x86`? Use `corflags` to check.\n- Yes, Visual Studio is using x86, CodeDom is using any CPU. Shouldn't it be the opposite though based on my findings?\n- @Evan: Nope, the `x86` one is compatible with a 32-bit DLL even on Windows x64. On the other hand, `AnyCPU` loads as a 64-bit process on Windows x64, and then the 32-bit DLL cannot be used. So now you just need to find the `x86` option for CodeDOM, or run `corflags` on the resulting executable.\n- If you made this an answer I would accept it. Just solved my issue!","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":99,"estimatedTokens":1130}}1026{"id":"stack-6833524","source":"stackoverflow","questionId":6833524,"title":"At which level is Compiler Optimization applied?","tags":["c","optimization","gcc","compiler-construction"],"text":"Title: At which level is Compiler Optimization applied?\nTags: c, optimization, gcc, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nMy question is at which level compiler applies optimization. Is it at the level of different code files? If that is the case then isn't it more inefficient than say if it were applied across the whole code? Secondly, what happens when one of the source file was compiled with no optimization and then linked with one with -O3 level optimization? \n\nI am especially interested in knowing how gcc handle such things.\n\n========================================\n\nCode:\n```text\ngcc -o prog *.c -O3 -fwhole-program -s\n```\n\n```text\ngcc -c module1.c -flto -O2\ngcc -c module2.c -flto -O3 -fno-strict-aliasing\ngcc -c module3.c -flto -O0\ngcc -c module4.c -O1\ngcc -o prog module*.o -flto -s\n```\n\n```text\n-fwhole-program\n```\n\n```text\n-flto\n```\n\n```text\n-O*\n```\n\n========================================\n\nComments:\n- Read the compiler optimization article on Wikipedia. As you can see, optimization is applied at several stages.\n- This is LLVM not GCC, but you'll find it a very interesting read, I promise. aosabook.org/en/llvm.html\n- Can I inline functions which are in an object file, that is, whose code I don't have access to?\n- No, inlining can only happen at the compile stage, and the inlined function definition must be visible. That's why you traditionally put inlined functions in the header files.\n- Inlining can happen whenever calling and called code are both visible to a program capable of (and allowed to) try optimizing. With LTO (don't know if this is true for today's GCC in particular, but it's definitely true for LLVM), optimizations like inlining can still happen right before linking/naitve code generation (long after the frontend is done). But again, the called code has to be available in some form (obviously ).\n- Yeah, LTO adds some new possibilites. Not sure if it's genuine \"inlining\", I thought it's mainly static duplicate removal. But I don't know LTO very well.\n- Again, I can't speak for GCC's LTO passes, but LLVM definitely sees C/C++-level functions and can do all the usual optimizations with them, e.g. \"genuine \" function inlining and also very clever ones like loop invariant code motion. In fact, nearly all of Clang's optimizations (save perhaps some shortcuts in code generation requring language-specific knowledge) are frontend-independent passes over LLVM IR, the equivalent to object code (nearly as low-level and converted to machine code at the end). And yet it's usually no worse than 10% slower than GCC, often better.\n- @delnan: Cool, that's interesting. (The OP's tag was \"GCC\", but no worries, it's good to expand.) I always thought that Clang was generally not optimizing nearly as well as GCC, but it may well have advantages coming from the IL stage. After all GCC's LTO is done with some intermediate bytecode (I think it's called \"gimple\").","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":5,"totalLines":47,"estimatedTokens":732}}1027{"id":"stack-10609944","source":"stackoverflow","questionId":10609944,"title":"Mapping a Java program to an Operating System process","tags":["java","compiler-construction","process","operating-system","callstack"],"text":"Title: Mapping a Java program to an Operating System process\nTags: java, compiler-construction, process, operating-system, callstack\nSource: Stack Overflow\n\nQuestion:\nI am wondering how this happens: how is a Java program mapped to an OS process (like the one shown for Linux below):\n\nIn C, it's a straightforward association in how a program is written and how the whole call stack proceeds in the OS. I was wondering how is the mapping achieved in Java? Does a method meth(), called on an object: obj, just translate to locating the address of obj.meth() & from then on stack is used the way it is in C?\n\nThanks in advance! \n\nEdit: I'd also be curious to know the model that other OOP languages use in general (C++, Python etc).\n\n========================================\n\nTop Answer:\nMost Java JVMs are plain C programs. So the picture will be the same write up to the first class file being interpreted/executed.\n\nAfter that it depends on the JVM implementation. Typically they would use the stack storage to keep track of control type information such as which classes are loaded, which threads are running etc. For the actual \"program\" storage the interpreter and garbage collector will use plain \"malloc\"/\"mfree\" to allocatate and free memory plus some fairly complex control structures to enable the garbage collector to function.\n\n========================================\n\nComments:\n- Note that even for C the given picture is already a serious simplification. For example there's not necessarily only one heap (having one heap is quite a bad idea if you have lots of threads that want to allocate memory), then there's the thread specific data and so on. The devil's in the details and if you think about how complicated even the simple C runtime gets, just imagine how the JVM looks like :)\n- Of course, I just wanted to discuss a simple case of a single thread process :)\n- Well I wouldn't say it is \"well-discussed\" in those other two threads. But then memory layout of the JVM is extremely complex as you say, which means a) not many people can seriously answer this question (I couldn't; I know what memory spaces exist and what they basically do, but layout or other details? bite me) and b) even if someone wanted to it would be a pretty unwieldy post. Not sure if it's a good fit for SO.","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":26,"estimatedTokens":576}}1028{"id":"stack-9261356","source":"stackoverflow","questionId":9261356,"title":"Hints to the compiler using llvm","tags":["c","compiler-construction","llvm","clang"],"text":"Title: Hints to the compiler using llvm\nTags: c, compiler-construction, llvm, clang\nSource: Stack Overflow\n\nQuestion:\nI am working on a tool that takes the LLVM IR and modifies it. I'm interested in allowing the programmer to give hints to the compiler. For example, he can give the hint that a particular loop is compute intensive. For this purpose, one thing that comes to my mind is to use a pragma. So my question is, how can we make the pragmas work? Can I have the pragma information there in the LLVM IR? What are the options for such kind of task?\n\n========================================\n\nCode:\n```text\npragma\n```\n\n```text\npragma\n```\n\n```text\ninlinehint\n```\n\n```text\nbyval\n```\n\n========================================\n\nComments:\n- There are \"`#pragma hot`\" and \"`__attribute__ ((hot))`\" in gcc. Is there something like it in llvm/clang? Gcc uses they to get \"profile feedback\" without doing a \"-fprofile-generate\"/\"-fprofile-use\".\n- @osgx: I don't know the answer off-hand. Would have to dig in the sources to see... Generally, though, clang compiles pretty much everything gcc compiles these days. However, not sure if it replicates the functionality of all gcc-specific pragmas","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":31,"estimatedTokens":298}}1029{"id":"stack-12485643","source":"stackoverflow","questionId":12485643,"title":"Efficient ways to reuse a Java compiler's backend for a new JVM language","tags":["java","compiler-construction","programming-languages"],"text":"Title: Efficient ways to reuse a Java compiler's backend for a new JVM language\nTags: java, compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nI'm writing a language targeted at the JVM, and I'm currently putting the\ncompiler together. It becomes apparent to me that, logically, my new language\nhas many of the same needs as Java when it comes to creating the bytecode. Just\nto give a few examples:\n\nFind out what a dot-separated sequence of identifiers refers to: a.b.c could\nrefer to local variable a, field a, class a or class a.b\nConvert primitive types for arithmetic operations: for intVar*doubleVar,\nintVar has to be cast to double\nFind the matching method for a set of parameters: With proper inheritance and\nthe same primitive casting as above\n\n- on a related note, autoboxing\ngeneric type checking, e.g. whether `List> l = new List>();`\nis compatible\n\nThere's probably much more, and I somehow don't want to reinvent these wheels\n(making a new language, of course I'm reinventing *some* wheels already...), so\nI wondered what would be the best ways to reuse (parts of) an existing Java\ncompiler, passing it the pieces of the AST it needs to figure out the above.\n\nI do already have lexer and parser (ANTLR) in place, so I'm really looking for\nadvice on what compilers are out there that would make it relatively easy for\nme to work with them (for example, I have looked a little into ECJ, but if\nsomeone told me that it's not capable of what I want or another compiler would\nbe easier to use that would be great).\n\nto sum it up with a definite question: Which Java compilers out there have an\neasily accessible backend that is suiteable to be used with a frontend for a\nnon-Java JVM language?\n\n========================================\n\nTop Answer:\nAt the risk of being a little obtuse, my answer is Scala.\n\nScala is a JVM-based language which has, as of the latest release, a macro system. Macros (when they're more powerful than the ones associated with C) are a technique for building domain-specific languages on top of existing languages, without having to start from scratch. A macro system lets you write code in the existing language, enhanced by new constructs, and will compile the new constructs down into the base language.\n\nIf you want to build straight off of Java, it might be worth considering the Java Syntax Extender, but JSE is not extensively used, and may be pretty rough. (There may, in fact, be more mature Java macro systems I haven't heard of.)\n\n========================================\n\nCode:\n```text\nList<X<? super Y>> l = new List<X<Z>>();\n```\n\n========================================\n\nComments:\n- thanks for your input! I have used Scala a few times, but It's not what I'm looking for here. Academically, just wanting to try another way is enough a reason for me. technically, I think there are some feature I wouldn't get with Scala, e.g. closures accessing local variables from the enclosing method. I will look into JSE and try it out though!\n- I think this hits exactly what I'm looking for. that their grammars aren't ANTLR based can't be helped, I guess. It will give me a peek into LL vs LR parsing in the process.","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":57,"estimatedTokens":797}}1030{"id":"stack-16486797","source":"stackoverflow","questionId":16486797,"title":"construct AST from string with libclang","tags":["compiler-construction","llvm","clang","jit","libclang"],"text":"Title: construct AST from string with libclang\nTags: compiler-construction, llvm, clang, jit, libclang\nSource: Stack Overflow\n\nQuestion:\nI generate some c++ codes, and store it in string, I want to construct an AST tree from the string using libclang. How to do it? I don't want to string into extra files.\n\n========================================\n\nTop Answer:\nyou can try such as the next code:\n\n```\nstd::unique_ptr AST(tooling::buildASTFromCode(\"int x=10;x++;\"));\nTranslationUnitDecl *DC = AST->getASTContext().getTranslationUnitDecl();\nif (DC) {\n llvm::errs() dump();\n llvm::errs() << \"---------dump end----------\\n\";\n visitor.TraverseDecl(DC);\n}\n```\n\n========================================\n\nCode:\n```text\nstd::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode(\"int x=10;x++;\"));\nTranslationUnitDecl *DC = AST->getASTContext().getTranslationUnitDecl();\nif (DC) {\n    llvm::errs() << \"---------dump begin----------\\n\";\n    DC->dump();\n    llvm::errs() << \"---------dump end----------\\n\";\n    visitor.TraverseDecl(DC);\n}\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":35,"estimatedTokens":257}}1031{"id":"stack-16440347","source":"stackoverflow","questionId":16440347,"title":"Is there a web-page containing a list of LLVM optimizations?","tags":["compiler-construction","llvm","compiler-optimization"],"text":"Title: Is there a web-page containing a list of LLVM optimizations?\nTags: compiler-construction, llvm, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nI've seen many optimization acronyms, eg.\n\n`dce, inline, constmerge, constprop, dse, licm, gvn, instcombine, mem2reg, scalarrepl`\n\nWhile I can deduce that `dce` is dead code elimination, I have trouble with many other.\n\nIs there a web-page with a list of LLVM optimizations with descriptions, what exactly each one of them does?\n\nKind regards,\nRoay\n\n========================================\n\nCode:\n```text\ndce, inline, constmerge, constprop, dse, licm, gvn, instcombine, mem2reg, scalarrepl\n```\n\n```text\ndce\n```\n\n```text\ninline\n```\n\n```text\nconstmerge\n```\n\n```text\nconstprop\n```\n\n```text\ndse\n```\n\n```text\na = 1; a = 2;\n```\n\n```text\na = 2;\n```\n\n```text\nlicm\n```\n\n```text\ninstcombine\n```\n\n```text\na + 1 + 1\n```\n\n```text\na + 2\n```\n\n```text\nmem2reg\n```\n\n```text\nalloca\n```\n\n```text\nalloca\n```\n\n```text\nscalarrepl\n```\n\n========================================\n\nComments:\n- This is exactly what I was looking for! I'll just emphasize the link.","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":16,"totalLines":87,"estimatedTokens":274}}1032{"id":"stack-6097402","source":"stackoverflow","questionId":6097402,"title":"How does the Linux C++ compiler (and linker) decide where to put typeinfo?","tags":["c++","linux","compiler-construction","linker","rtti"],"text":"Title: How does the Linux C++ compiler (and linker) decide where to put typeinfo?\nTags: c++, linux, compiler-construction, linker, rtti\nSource: Stack Overflow\n\nQuestion:\nI have a class defined in one h file and implemented in a cpp that is part of one lib (we'll call it libdef).\n\nI have two other libs that have cpp files that include this h file. One of them does dynamic_cast() to this class (we'll call it libdyn) and the other does new for this class (we'll call it libnew).\n\nIt seems that in one of these libs there's typeinfo for the type but not in the other:\n\n```\nuser@machine> ld --cref libdef.so | grep -E \"typeinfo for MyClass\"\nld: warning: cannot find entry symbol _start; not setting start address\ntypeinfo for MyClass libdef.so\n\nuser@machine> ld --cref libnew.so | grep -E \"typeinfo for MyClass\"\nld: warning: cannot find entry symbol _start; not setting start address\ntypeinfo for MyClass libdef.so\n\nuser@machine> ld --cref libdyn.so | grep -E \"typeinfo for MyClass\"\nld: warning: cannot find entry symbol _start; not setting start address\ntypeinfo for MyClass libdyn.so\n```\n\nAs you can see both libdef and libnew use the typeinfo from libdef, but libdyn uses its own typeinfo. Why is that? How does the compiler/linker decide whether to put the typeinfo in one lib or reference it from the other?\n\nI should note that both libnew and libdyn are built with -llibdef.\n\n```\nuser@machine> icpc -V\nIntel(R) C++ Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 12.0.0.084 Build 20101006\nCopyright (C) 1985-2010 Intel Corporation. All rights reserved.\n\nuser@machine> ld -V\nGNU ld version 2.17.50.0.6-14.el5 20061020\nSupported emulations:\n elf_x86_64\n elf_i386\n i386linux\n```\n\nAfter some checks I did, it depends on whether the cpp files of the lib \"see\" a virtual method definition or not.\n\nThis code will not result in the typeinfo symbol being in the library:\n\n```\nclass SomeClass { public: SomeClass(); virtual void func(); };\n```\n\nThis code will produce a typeinfo symbol in the library:\n\n```\nclass SomeClass { public: SomeClass() {} virtual void func() {} };\n```\n\nWhen present, the typeinfo symbol will have vague linkage.\n\n========================================\n\nTop Answer:\nAs others have mentioned, this depends on the C++ implementation. But here is a description of how g++ does it.\n\nBriefly, when possible, g++ defines the class vtable and type_info in the translation unit which defines the first non-inline virtual member of the class.\n\n========================================\n\nCode:\n```text\nuser@machine> ld --cref libdef.so | grep -E \"typeinfo for MyClass\"\nld: warning: cannot find entry symbol _start; not setting start address\ntypeinfo for MyClass libdef.so\n\nuser@machine> ld --cref libnew.so | grep -E \"typeinfo for MyClass\"\nld: warning: cannot find entry symbol _start; not setting start address\ntypeinfo for MyClass libdef.so\n\nuser@machine> ld --cref libdyn.so | grep -E \"typeinfo for MyClass\"\nld: warning: cannot find entry symbol _start; not setting start address\ntypeinfo for MyClass libdyn.so\n```\n\n```text\nuser@machine> icpc -V\nIntel(R) C++ Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 12.0.0.084 Build 20101006\nCopyright (C) 1985-2010 Intel Corporation.  All rights reserved.\n\nuser@machine> ld -V\nGNU ld version 2.17.50.0.6-14.el5 20061020\nSupported emulations:\n elf_x86_64\n elf_i386\n i386linux\n```\n\n```text\nclass SomeClass { public: SomeClass(); virtual void func(); };\n```\n\n```text\nclass SomeClass { public: SomeClass() {} virtual void func() {} };\n```\n\n```text\nlibdef\n```\n\n```text\nvtable\n```\n\n```text\nlibdyn\n```\n\n```text\ndynamic_cast\n```\n\n```text\nlibnew\n```\n\n```text\ndlopen\n```\n\n```text\nRTLD_LOCAL\n```\n\n```text\ndynamic_cast\n```\n\n```text\n.so\n```\n\n```text\nvirtual table\n```\n\n```text\ntype info\n```\n\n```text\ntypeinfo\n```\n\n```text\nso\n```\n\n========================================\n\nComments:\n- You seem to be assuming that there's only one typeinfo object per type... the standard does not guarantee this, it only guarantees that if there are multiple, they will compare equal.\n- @Ben The thing is that if there are multiple, they do not compare equal in Linux (because of the use of address comparison). This is not suppose to happen if you use libs only as gcc tells you to but we still have a problem with it and I'm trying to find some work around for our product.\n- by \"compare equal\", I do not mean address comparison. I mean `operator=` which is defined for objects of type `typeinfo`. That's the only mechanism the standard guarantees will work for comparing typeinfo instances.\n- @Ben I don't know if the address comparison is implemented in operator== or not, but I do know (well, read it somewhere) that the dynamic_cast() uses address comparison on Linux to determine type equality.\n- The declaration is in a header which is included in translation units in all 3 libs. If all of them had the typeinfo in them, it would make sense, but what confuses me is that one has it and the other doesn't.\n- You are probably right, but isn't there a way to make it reference it from another lib and not use its own typeinfo?\n- Not that I know of. The whole raison d'etre behind weak references is that *every* use provokes a definition (in the `.o` file), and that the linker chooses one---for all intents an purposes, at random. One of the motivations of the ODR is to allow this implementation technique. If anything in your class definitions would cause the typeinfo to be different, then you have undefined behavior.\n- I wonder if the description in the link is up-to-date. (I know that the URL says 4.6, but it's possible that the text itself is older, and hasn't been updated.) There's no mention of weak binding, and this is the technique g++ uses for things like template instantiations. (If you the link to templates on that page, it leads to a page talking about Borland model and CFront model. A very definite sign that it isn't up to date.)\n- @James: From what I can tell using nm, recent versions of g++ use both weak binding and \"key method\" techniques for vtables and type_info.\n- @ashepler I'm not sure what they do for typeinfo---my comments about weak binding there were just a guess. I do know that weak symbols are used in template instantiations, however, and it seems reasonable that they are also used here.","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":168,"estimatedTokens":1579}}1033{"id":"stack-1656119","source":"stackoverflow","questionId":1656119,"title":"Currying and compiler design","tags":["currying","compiler-construction"],"text":"Title: Currying and compiler design\nTags: currying, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThis is a homework question:\n\n Explain the transformations the type\n of a routine undergoes in partial\n parameterization.\n\nSo far I understand currying. But I cannot find any resources on how a function like this is implemented by the compiler in memory. Could I be pointed in the right direction, maybe keywords to search for or links to resources or possibly an explanation here of how the compiler generates the type and symbol table among other things thats related to the question.\n\nThanks.\n\n========================================\n\nTop Answer:\nCurrying is like fixing a parameter of the function. What you really need to modify is the prototype of the function called.. if you have for example `retn_type function(param1, param2)` and you currying it on first parameter you set it to a fixed value and you obtain a new function `retn_type(param2)` that can be called and passed in a different way from the original one.\n\nActually you can obtain it in a compiler in many ways or hacks but the core of everything to its simplicity is just to redefine a new version that is linked to first one. So when you call `retn_type(param2)` you execute the same code of first function assuming that parameter1 is specified by curryfication.\n\n========================================\n\nCode:\n```text\nf1 :: t1 -> (t2 -> (t3 -> t))\n```\n\n```text\nf2 :: t2 -> (t3 -> t)\n```\n\n```text\nf3 :: t3 -> t\n```\n\n```text\nf3(C) == c1+c2*C where c1 and c2 are constant.\nf2(B) == f3(C) where c1 is constant and c2 is replaced with B.\nf1(A) == f2(B) where c1 is replaced with A.\n```\n\n```text\nretn_type function(param1, param2)\n```\n\n```text\nretn_type(param2)\n```\n\n```text\nretn_type(param2)\n```\n\n========================================\n\nComments:\n- In reading your question, I immediately think of the ML language family, which explicitly answers your question in its very notation! It would be well worth your time to play around with OCaml.\n- Do you have any links to resource describing specific algorithms that can be used to transform the abstract syntax tree representation of a multi-parameter function into a curried functions?\n- @Richard Cook, my only source right now is some 15 year old treeware version of \"Compiler Design\" by Reinhard Wilhelm and Dieter Maurer (with emphasis on front ends for logic/functional and oo languages). If I have time during the holidays I can add some information.","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":61,"estimatedTokens":622}}1034{"id":"stack-3953016","source":"stackoverflow","questionId":3953016,"title":"In what cases does out-of-order execution result in more efficient code","tags":["compiler-construction","cpu","compiler-optimization"],"text":"Title: In what cases does out-of-order execution result in more efficient code\nTags: compiler-construction, cpu, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nI'm trying to understand how the Memory barrier works, why it is used and in what cases it should be used. However I'm not entirely sure in what cases it would be more efficient to arrange the order of instructions, can anyone give me an example of that?\n\n========================================\n\nComments:\n- Thanks, the pipelines made a lot of sense to me","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":11,"estimatedTokens":132}}1035{"id":"stack-5356786","source":"stackoverflow","questionId":5356786,"title":"JavaScript bytecode compiler?","tags":["javascript","compiler-construction","bytecode"],"text":"Title: JavaScript bytecode compiler?\nTags: javascript, compiler-construction, bytecode\nSource: Stack Overflow\n\nQuestion:\nFor a project I'm tangentially working on, I need a way to compile JavaScript into some intermediate language or bytecode so that I can single-step through it. I know that many of the JavaScript engines in modern browsers do something like this, but they don't make the resulting bytecode accessible. Is there a good off-the-shelf tool for this sort of JavaScript compilation?\n\n========================================\n\nComments:\n- Why don't you then just use a debugger of a browser? I mean, you want to step through the code and therefore you're going to translate it into some intermediate code so you can step through it...? Also interesting maybe: .NET supports JScript - maybe that helps you?\n- The idea is to have students write JavaScript code in a web browser and then to be able to run it in the browser with a debugger available. Many browsers do have good debuggers in them, but sometimes they're really hard to use or have weird side-effects, like locking up the browser except for the debugging console. For that reason, .NET won't be very useful, since this should run in a browser on any OS. Still, thanks for the suggestion!","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":12,"estimatedTokens":316}}1036{"id":"stack-5270865","source":"stackoverflow","questionId":5270865,"title":"using precedence parser not only for expressions?","tags":["parsing","compiler-construction","operator-precedence","pratt-parser"],"text":"Title: using precedence parser not only for expressions?\nTags: parsing, compiler-construction, operator-precedence, pratt-parser\nSource: Stack Overflow\n\nQuestion:\nIs it possible to use some kind of operator-precedence parser or shunting-yard algorithm for simple programming language? For example, if this language have only expressions, functions and declarations of variables.\n\nWhat are cons and pros of this way? Can it be much faster than traditional LL/LR parsers?\n\n========================================\n\nComments:\n- Not likely to be faster; all parsers have to break the input into tokens (\"lex\") and that's where most of the \"parsing\" time actually goes. A operator-precedence driven grammer might have a smaller implementation (size) footprint, but I suspect these days that the tradeoff of space for the extra convenience that LL/LR parsers bring is not worth it in most cases. Finally, it is unclear how helpful operator precedence grammars are for the non-expression part of the language; the expresssion subgrammar is usually only a modest part of the whole grammar so it is unclear what the win really is.\n- @IraBaxter Not only size footprint. Fredrik Lundh, who wrote about Pratt parsing, points out that you can save on stack depth during parsing. Whether or not this results in serious gains would depend on the particular grammar being implemented, though. effbot.org/zone/simple-top-down-parsing.htm\n- @ThomasH: How deep does the stack get in parsing? If you have really complicated expressions, I doubt the stack gets more than 10-20 levels deep. (We have an on-the-fly code generator that processes several-million-line lisp-like programs, and it never blows its stack of some 128 slots). PS: Windows gives you 1 Mb if not more of stack space for a standard program. Does saving stack space matter?\n- @IraBaxter I think this particular argument was not about the *space* consumed by the stack, but by the perfomance hit you get through pushing and poping stack frames.\n- @ThomasH: Compared to the work of processing lexemes, the cost of creating new stack frames (a few machine instructions per call) in the parser (only a few stack frames per token likely, my LALR(1) parsers have zero but simulate a state state) is vanishingly small.\n- If you want fast parsers, see dl.acm.org/citation.cfm?id=12276.13326\n- @IraBaxter Not in environments like interpreted JavaScript in the browser, where some people still try to avoid function calls for performance reasons.\n- @ThomasH: How expensive is a function call relative to an operation (e.g., integer-add or string-concatenate) in JavaScript? If its an interpreter, I'd expect these to be about the same cost. Is it different by an order of magnitude? How much? Measured how?\n- Douglas Crockford (from the article you linked) gave an updated overview on Pratt parsing later that might be easier to digest by some: youtube.com/watch?v=Nlqv6NtBXcA&t=1171s","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":21,"estimatedTokens":731}}1037{"id":"stack-46744200","source":"stackoverflow","questionId":46744200,"title":"Why specify a separate production for assignment operator with `=` literal instead of part `AssignmentOperator` symbol","tags":["javascript","ecmascript-6","context-free-grammar","compiler-construction"],"text":"Title: Why specify a separate production for assignment operator with `=` literal instead of part `AssignmentOperator` symbol\nTags: javascript, ecmascript-6, context-free-grammar, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThe EcmaScript grammar for AssignmentExpression specifies the following:\n\n```\nAssignmentExpression :\n ConditionalExpression\n YieldExpression\n ArrowFunction\n AsyncArrowFunction\n LeftHandSideExpression = AssignmentExpression >= >>>= &= ^= |= **=\n```\n\nI'm wondering what is the rationale behind specifying `LeftHandSideExpression = AssignmentExpression` as a separate production instead of simply including `=` into `AssignmentOperator`?\n\n========================================\n\nCode:\n```text\nAssignmentExpression :\n    ConditionalExpression\n    YieldExpression\n    ArrowFunction\n    AsyncArrowFunction\n    LeftHandSideExpression = AssignmentExpression                     <-------------- here\n    LeftHandSideExpression AssignmentOperator AssignmentExpression\n\nAssignmentOperator : one of\n    *= /= %= += -= <<= >>= >>>= &= ^= |= **=\n```\n\n```text\nLeftHandSideExpression = AssignmentExpression\n```\n\n```text\n=\n```\n\n```text\nAssignmentOperator\n```\n\n```text\n=\n```\n\n```text\nLeftHandSideExpression AssignmentOperator AssignmentExpression\n```\n\n```text\nAssignmentOperator\n```\n\n```text\n=\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":8,"totalLines":61,"estimatedTokens":330}}1038{"id":"stack-3245587","source":"stackoverflow","questionId":3245587,"title":"Writing atomic function","tags":["c","linux","compiler-construction","operating-system","linux-kernel"],"text":"Title: Writing atomic function\nTags: c, linux, compiler-construction, operating-system, linux-kernel\nSource: Stack Overflow\n\nQuestion:\nI want to implement an atomic function in C language, so that the process or thread will not be preempted while executing the function.\n\n========================================\n\nTop Answer:\nYou're confusing two things:\n\n- \"Atomic\" means the operation either appears to have not run or to have completed entirely.\n\n- \"Preemption\" (loosely) means that other threads can run on your core without you yielding.\n\nEven on a non-preemptive OS (e.g. classic Mac OS), things still aren't guaranteed to be atomic:\n\n- Interrupts can happen at any time (unless you disable interrupts too).\n\n- Code on other cores will run even if you disable interrupts.\n\nIt also helps to know *why* you think you need atomic operations. The easiest way to protect a shared data structure is to use a lock. If you're in the kernel, read Rusty Russell's Unreliable Guide to Locking (it might be a bit out of date). If you're in userspace, just use a pthread mutex.\n\n========================================\n\nCode:\n```text\npreempt_disable()\n```\n\n```text\npreempt_enable()\n```\n\n```text\nsched_setscheduler(2)\n```\n\n========================================\n\nComments:\n- Is this *in* the Linux kernel, or in user space?\n- Is it possible in user space ? and if not, then how it can be done in kernel space.\n- This is, generally speaking, impossible in a preemptive multitasking OS. There may be strange hooks that let you do it, but it is more important to address the question: why are you trying to do this in the first place?\n- i was curious about it, then how does the semaphore locking and releasing works?\n- @Yogesh: Mostly using inline assembly language that makes use of specific hardware support. Not by making entire functions atomic. Do you have some goal in mind? If so, tell us more about it.\n- How does the semaphore locking and releasing operations works?\n- @Yogesh, if you want to know how to use pthread semaphores, the manpages for `pthread_mutex_init(3)`, `pthread_mutex_lock(3)`, `pthread_mutex_unlock(3)` are where you should start. Perhaps `pthread_cond_signal(3)` if you want to use condition variables instead of bare mutexes, which may match your problem domain better. If you want to learn how to implement these primitives for yourself, I suggest reading Curt Schimmel's Unix Systems for Modern Architectures: Symmetric Multiprocessing and Caching for Kernel Programmers","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":49,"estimatedTokens":624}}1039{"id":"stack-61213501","source":"stackoverflow","questionId":61213501,"title":"Read and update object with Typescript compiler API","tags":["javascript","typescript","compiler-construction","typescript-compiler-api"],"text":"Title: Read and update object with Typescript compiler API\nTags: javascript, typescript, compiler-construction, typescript-compiler-api\nSource: Stack Overflow\n\nQuestion:\nThe typescript compiler API is new for me and looks like I'm missing something.\nI'm looking the way to update specific object at ts file with compiler API\n\nExisting file - *some-constant.ts*\n\n```\nexport const someConstant = {\n name: 'Jhon',\n lastName: 'Doe',\n additionalData: {\n age: 44,\n height: 145,\n someProp: 'OLD_Value'\n /**\n * Some comments that describes what's going on here\n */\n }\n};\n```\n\nAfter all, I want to get something like this:\n\n```\nexport const someConstant = {\n name: 'Jhon',\n lastName: 'Doe',\n additionalData: {\n age: 999,\n height: 3333,\n someProp: 'NEW_Value'\n eyeColor: 'brown',\n email: 'someemail@gmail.com',\n otherProp: 'with some value'\n }\n};\n```\n\n========================================\n\nCode:\n```text\nexport const someConstant = {\n    name: 'Jhon',\n    lastName: 'Doe',\n    additionalData: {\n        age: 44,\n        height: 145,\n        someProp: 'OLD_Value'\n        /**\n         * Some comments that describes what's going on here\n         */\n    }\n};\n```\n\n```text\nexport const someConstant = {\n    name: 'Jhon',\n    lastName: 'Doe',\n    additionalData: {\n        age: 999,\n        height: 3333,\n        someProp: 'NEW_Value'\n        eyeColor: 'brown',\n        email: 'someemail@gmail.com',\n        otherProp: 'with some value'\n    }\n};\n```\n\n```text\nimport { Project, PropertyAssignment, QuoteKind, Node } from \"ts-morph\";\n\n// setup\nconst project = new Project({\n    useInMemoryFileSystem: true, // this example doesn't use the real file system\n    manipulationSettings: {\n        quoteKind: QuoteKind.Single,\n    },\n});\nconst sourceFile = project.createSourceFile(\"/file.ts\", `export const someConstant = {\n    name: 'Jhon',\n    lastName: 'Doe',\n    additionalData: {\n        age: 44,\n        height: 145,\n        someProp: 'OLD_Value'\n        /**\n         * Some comments that describes what's going on here\n         */\n    }\n};`);\n\n// get the object literal\nconst additionalDataProp = sourceFile\n    .getVariableDeclarationOrThrow(\"someConstant\")\n    .getInitializerIfKindOrThrow(ts.SyntaxKind.ObjectLiteralExpression)\n    .getPropertyOrThrow(\"additionalData\") as PropertyAssignment;\nconst additionalDataObjLit = additionalDataProp\n    .getInitializerIfKindOrThrow(ts.SyntaxKind.ObjectLiteralExpression);\n\n// remove all the \"comment nodes\" if you want to... you may want to do something more specific\nadditionalDataObjLit.getPropertiesWithComments()\n    .filter(Node.isCommentNode)\n    .forEach(c => c.remove());\n\n// add the new properties\nadditionalDataObjLit.addPropertyAssignments([{\n    name: \"eyeColor\",\n    initializer: writer => writer.quote(\"brown\"),\n}, {\n    name: \"email\",\n    initializer: writer => writer.quote(\"someemail@gmail.com\"),\n}, {\n    name: \"otherProp\",\n    initializer: writer => writer.quote(\"with some value\"),\n}]);\n\n// output the new text\nconsole.log(sourceFile.getFullText());\n```\n\n========================================\n\nComments:\n- Is this something you want to do at compile time when emitting or are you wanting to make this change directly to the source file? If it's the latter then you may want to check out ts-morph.\n- I need to make it directly on the source file. And I tried ts-morph, with no success too :(","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":132,"estimatedTokens":837}}1040{"id":"stack-38200120","source":"stackoverflow","questionId":38200120,"title":"Converting Fortran to Javascript","tags":["javascript","compiler-construction","fortran","interpreter","transpiler"],"text":"Title: Converting Fortran to Javascript\nTags: javascript, compiler-construction, fortran, interpreter, transpiler\nSource: Stack Overflow\n\nQuestion:\nI'm losing my mind with this Problem. I wrote an Interpreter and then a Transpiler to convert Fortran to Javascript. BUT every time I was stuck with the Problem of passing variable by reference. \n\nAs you know in Fortran there is no syntax different between pass by reference or pass by value, all looks the same. However in Javascript pass by reference can only work, if the variable passed in an Object. \n\nFortran example code:\n\n```\nREAL FUNCTION PYTHAGORAS (A,B,K)\nREAL A,B,K\nK = 12 // E K in GEOMETRIE()\nPYTHAGORAS = SQRT(A**2+B**2)\nEND FUNCTION PYTHAGORAS\n\nLOGICAL FUNCTION GEOMETRIE (H,B,D)\nREAL H,B,D,E\nB = 12 \nH = 7\n// E is pass by reference, it's gonna be changed in PYTHAGORAS()\nD = PYTHAGORAS(B, H, E) \nGEOMETRIE = .TRUE.\nEND FUNCTION GEOMETRIE\n```\n\nI did a lot of research, but until now, I didn't find any useful result.\n\nI was wondering, if there's any library out there to do the Job (Client-side / NodeJS). I can't imagine that until now, nobody tried to lose this Problem before.\n\n========================================\n\nCode:\n```text\nREAL FUNCTION PYTHAGORAS (A,B,K)\nREAL A,B,K\nK = 12 // E <=> K in GEOMETRIE()\nPYTHAGORAS = SQRT(A**2+B**2)\nEND FUNCTION PYTHAGORAS\n\nLOGICAL FUNCTION GEOMETRIE (H,B,D)\nREAL H,B,D,E\nB = 12 \nH = 7\n// E is pass by reference, it's gonna be changed in PYTHAGORAS()\nD = PYTHAGORAS(B, H, E) \nGEOMETRIE = .TRUE.\nEND FUNCTION GEOMETRIE\n```\n\n```text\nfunction PYTHAGORAS(obj /* A B K */) {\n  obj.K = 12;\n  return Math.sqrt(obj.A * obj.A + obj.B * obj.B);\n}\n\nfunction GEOMETRIE(obj /* H B D */) {\n  var E;\n  obj.B = 12; \n  obj.H = 7;\n\n  obj.D = PYTHAGORAS({A:obj.B, B:obj.H, K:E}); \n  return true;\n}\n\nvar res = {H:0, B:0, D:0};\n\nGEOMETRIE(res);\nconsole.log(res);\n```\n\n```text\nObject { H=7,  B=12,  D=13.892443989449804 }\n```\n\n```text\nfunction PYTHAGORAS(A, B, K) {\n  K.val = 12;\n  return Math.sqrt(A.val * A.val + B.val * B.val);\n}\n\nfunction GEOMETRIE(H, B, D) {\n  var E = {}; \n  B.val = 12; \n  H.val = 7;\n\n  D.val = PYTHAGORAS(B, H, E); \n  console.log('E = ' + E.val);\n  return true;\n}\n\nvar H = {}\n, B = {}\n, D = {};\n\nGEOMETRIE(H, B, D);\nconsole.log('H = ' + H.val + ', B = ' + B.val + ', D = ' + D.val);\n```\n\n```text\nE = 12\nH = 7, B = 12, D = 13.892443989449804\n```\n\n========================================\n\nComments:\n- There IS pass by value in modern Fortran, but it is not important here. Can't you pass the variable in a list? Have you had a look at Emscripten? Asking for a library is off-topic here BTW.\n- of course there is pass by value in Fortran, as you can see in the example, the call of PYTHAGORAS(B,H,E) *// B and H are pass by value, but E is pass by reference.* I know Emscripten, but didn't try it yet. I think it will be more complicated to convert Fortran to C++ and then to Javascript.\n- No they are not, all are passed by reference. To pass by value you must use the `VALUE` keyword introduced in Fortran 2003.\n- that makes sense, I'm gonna try this way too. Thanks a lot @VladimirF I'll come back for any changes.\n- the idee is great, but still not gonna work, because as i mentioned, E should be pass by reference and it will be changed in PYTHAGORAS as K (or obj.K). So before the return true; E should be 12 and not 0.\n- You're right. Please see the alternate version in my edited answer.\n- Thanks @Arnauld, the idee of obj.val it awesome! I'm gonna try your Solution.\n- @kamal where can we see your transpiler ?","metadata":{"transformedAt":"2026-08-18T18:32:17.944Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":5,"totalLines":117,"estimatedTokens":882}}1041{"id":"stack-56160457","source":"stackoverflow","questionId":56160457,"title":"Techniques for parsing code blocks without curly braces","tags":["c#","parsing","compiler-construction","interpreter"],"text":"Title: Techniques for parsing code blocks without curly braces\nTags: c#, parsing, compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nI'm writing a simple parser/interpreter in C# from scratch (no third-party libraries). It compiles to bytecode and then I have class that runs the bytecode. I'm getting close to wrapping it up. I've just implemented `while` and `for` loops and am working on `if`|`else if`|`else` blocks.\n\nAs it stands, my parser requires all of these structures to use curly braces. I'd like to make it more C-like and have the curly braces be optional when the block contains just a single statement. This is giving me trouble.\n\n```\nif (condition)\n{\n // Make curly braces optional when there is just one statement here\n}\n```\n\nThe problem is tracking state. How does the parser know when a block without curly braces has ended. One approach would be to check if there is a block without braces in effect after each and every statement. However, there are a lot of different scenarios that would constitute a statement and so those checks would need to be in a number of places. That feels a little brittle to me.\n\nI'm just wondering if anyone has done this and knows of any slick tricks for tracking when a code block ends when there are no curly braces.\n\n========================================\n\nCode:\n```text\nif (condition)\n{\n    // Make curly braces optional when there is just one statement here\n}\n```\n\n```text\nwhile\n```\n\n```text\nfor\n```\n\n```text\nif\n```\n\n```text\nelse if\n```\n\n```text\nelse\n```\n\n```text\nstatement\n   : 'if' paren_expr ['{'] statement ['}'] \n\nparen_expr\n   : '(' expr ')'\n```\n\n```text\npublic void Statement()\n{\n    if(curToken == Token.If)\n    {\n       Eat(Token.If); // Eat is convenience method that moves token pointer on\n       if(curToken == Token.LParen)\n       {\n          Eat(Token.LParen)\n          ParenExpr();\n          Eat(Token.RParen);\n       }\n       if(curToken == Token.LBrace) // this will signify a block of statements\n       {   \n          Eat(Token.LBrace);\n          while(curToken != Token.RBrace)\n             Statement();\n          Eat(Token.RBrace);\n       }\n       else\n          Statement();              \n    }\n}\n\npublic void ParenExpr()\n{\n   // do other token checks\n}\n```\n\n========================================\n\nComments:\n- What kind of parser have you implemented? What does your grammar look like for statements and statement blocks?\n- @IanMercer: I just have a lexer that breaks the source into tokens and then a parser that figures out the bytecode from the context. I wasn't following any special techniques. The language is somewhat C-like, although that is likely to change.\n- @IanMercer: In fact, I would probably change the language if it made it easier to deal with this issue.\n- Could you give some examples where it is difficult to tell when a block ends?\n- @JimW: In every instance. As I tried to explain, it would mean I'd need to do a test after each and every statement, and not all statements are processed in the same place. So this means adding checks all around the code. Any approach like that is brittle because you could miss a spot or forget to add one when you change the code. I was just wondering if anyone knew of a slicker approach.\n- I guess I don't understand - it's unclear to me why you wouldn't know when you've read a statement. But I've never attempted to write a compiler, though it is interesting.\n- @JimW: Well, it's not like sequential lines of code that match the number of statements I'm reading. I have a loop that diverts off to different methods depending on the statements I'm parsing. For example, I call a method to parse `for` statements. So it's not quite that simple. I need state information that says I have a code block with no braces in effect. I guess it comes down to where you're going to examine that state information in your code. As I indicated, you would need to do it in a number of different places.\n- Are you doing recursive descent parsing?\n- @JohanP: Sorry, I have been reading about parsers but don't know enough about that technique. But I'm not using recursion so I wouldn't think so. I could probably still change the approach I take to parsing if it would somehow address the issue.\n- Your grammar should look like this: `statement = block | if ( expr) statement else statement | ...`, where `block` is a block of statements in curly braces.\n- I have a 99.9% delimiter-less grammar implemented. The 0.1% exception is when you want to have a block of expressions, you will always need a delimiter of some kind to indicate you are at the end of a block.\n- @FrankC.: C, C++ and C# allow blocks without delimiters. And now my code supports that too.\n- @JonathanWood - I'm was just referring to a 'block' of statements. Your comment below about the 4 statements was the impetus. I originally thought that was the question but re-reading it I see I was wrong..\n- So, is the point of this that `Statement()` would be recursive by calling itself again if the statement called for it? I did think about calling a routine to process one statement, but of course that statement could be anything, including other `if` statements. So a lot could happen. But if `Statement()` is recursive then I suppose that would be okay.\n- BTW, the code you posted doesn't seem like it would work if the block had braces and more than one statement. How would that work?\n- I'm not following you? Are you saying it wouldn't parse `if(condition){ if(condition2) doSomething(); if(condition3) doSometingElse()}`? It would parse that.\n- No, I'm saying it wouldn't parse if there were, say, four statements between the braces. You call `Statement()` once. How would it handle that?\n- Well, you have to code in the rest of the method, that will just deal with `if`, it also wouldn't deal with `while` either because it is a simple example of your `if` problem\n- I'm trying to understand the goal here. If the `if` statement is followed by curly braces, and within the curly braces there are four statements, I don't see how this would work? You only call `Statement()` once. How would `Statement()` know when to return?\n- Give me an example of a statement you want then I can add it to my example\n- Am I really being that unclear? Four statements within the curly braces: `if (condition) { statement1(); statement2(); statement3(); statement4(); }`. Your code only calls `Statement()` once. How does it know to execute four statements?\n- It's recursive. So right now, we have only handled the `if` statement. So if your `statement(1)` `statement2()` `statement3()` `statement4()` were ALL `if` statements, then it would parse it. If you need an assign statement, i.e. `x = 4`, then you need to go add it in your `Statement` function. `if(curToken == Token.Identifer) &#47;&#47; do your assignment logic`. You need to handle all of the statement cases. I can't make this any clearer.\n- Yes, you already explained that. It couldn't be more clear that your code only handles `if` statements, and that support for other statements would need to be added. What I don't get is how `Statement()`, which your code only calls once, would know to execute four statements (regardless of what those statements are). I don't see a loop in `Statement()`, so it would appear to execute only one statement. I don't understand how `Statement()` knows how to execute four statements and then return.\n- if checks the current token, `curToken`, a global variable what your next token is. The `Eat` function moves the pointer on and sets your `curToken`.\n- I get that. But it doesn't at all explain how it knows to execute four statements and then return. There is no loop in `Statement()`. It will therefore execute a single statement and return. At which time your `if` handler will expect the closing parenthesis. The remaining three statements have not been executed. I'm missing a key feature of this approach. But perhaps we're just not going to be able to discuss it.\n- @JonathanWood I have updated my answer, you were right, it needed a `while` when a `{` is encountered as this signifies a block of statements.\n- While changes are needed to the structure of what you posted, I've backed up my existing code and am going to try to refactor to employ your general concepts. A few things might be harder, but it does simplify some of the issues I'm struggling with. Thanks.","metadata":{"transformedAt":"2026-08-18T18:32:17.945Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":8,"totalLines":118,"estimatedTokens":2097}}1042{"id":"stack-43784575","source":"stackoverflow","questionId":43784575,"title":"In functional languages, how is the compiler able to translate non-tail recursion into loops to avoid stack overflows (if at all)?","tags":["recursion","functional-programming","compiler-construction","compiler-optimization","tail-recursion"],"text":"Title: In functional languages, how is the compiler able to translate non-tail recursion into loops to avoid stack overflows (if at all)?\nTags: recursion, functional-programming, compiler-construction, compiler-optimization, tail-recursion\nSource: Stack Overflow\n\nQuestion:\nI've been recently learning about functional languages and how many don't include for loops. While I don't personally view recursion as more difficult than a for loop (and often easier to reason out) I realized that many examples of recursion aren't tail recursive and therefor cannot use simple tail recursion optimization in order to avoid stack overflows. According to this question, all iterative loops can be translated into recursion, and those iterative loops can be transformed into tail recursion, so it confuses me when the answers on a question like this suggest that you have to explicitly manage the translation of your recursion into tail recursion yourself if you want to avoid stack overflows. It seems like it should be possible for a compiler to do all the translation from either recursion to tail recursion, or from recursion straight to an iterative loop with out stack overflows. \n\nAre functional compilers able to avoid stack overflows in more general recursive cases? Are you really forced to transform your recursive code in order to avoid stack overflows yourself? If they aren't able to perform general recursive stack-safe compilation, why aren't they?\n\n========================================\n\nTop Answer:\n### Tail Call Optimization:\n\nThe natural way to do arguments and calls is to sort out the cleaning up when exiting or when returning.\n\nFor tail calls to work you need to alter it so that the tail call inherits the current frame. Thus instead of making a new frame it massages the frame so that the next call returns to the current functions caller instead of this function, which really only cleans up and returns if it's a tail call.\n\nThus TCO is all about cleaning up before the last call.\n\n### Continuation Passing Style - make tail calls out of everything\n\nA compiler can change the code such that it only does primitive operations and pass it to continuations. Thus the stack usage gets moved onto the heap since the computation to be continued is made a function.\n\nAn example is:\n\n```\nfunction hypotenuse(k1, k2) {\n return sqrt(add(square(k1), square(k2)))\n}\n```\n\nbecomes\n\n```\nfunction hypotenuse(k, k1, k2) {\n (function (sk1) {\n (function (sk2) {\n (function (ar) {\n k(sqrt(ar));\n }(add(sk1,sk2));\n }(square(k2));\n }(square(k1));\n}\n```\n\nNotice every function has exactly one call now and the order of evaluation is set.\n\n========================================\n\nCode:\n```text\ndef fibc(n, cont):\n    if n <= 1:\n        return cont(n)\n    return fibc(n - 1, lambda a: fibc(n - 2, lambda b: cont(a + b)))\n```\n\n```text\nfindmax(node) {\n    if (node == null) {\n        return null\n    }\n    if (node.isLeaf) {\n        return node.value\n    } else {\n        return max(findmax(node.left), findmax(node.right))\n    }\n}\n```\n\n```text\nfact(number) {\n    if (number == 1) {\n        return 1\n    } else {\n        return number * fact(number - 1)\n    }\n}\n```\n\n```text\nfact(number, acc) {\n    if (number == 1) {\n        return acc\n    } else {\n        return fact(number - 1, number * acc)\n    }\n}\n// Helper function\nfact(number) {\n    return fact(number, 1)\n}\n```\n\n```text\nrecsub(number) {\n    if (number == 1) {\n        return 1\n    } else {\n        return number - recsub(number - 1)\n    }\n}\n```\n\n```text\nnode\n```\n\n```text\nnode\n```\n\n```text\nleft\n```\n\n```text\nright\n```\n\n```text\nvalue\n```\n\n```text\nisLeaf\n```\n\n```text\nmax\n```\n\n```text\nnull\n```\n\n```text\nmax\n```\n\n```text\nmax\n```\n\n```text\nfunction hypotenuse(k1, k2) {\n  return sqrt(add(square(k1), square(k2)))\n}\n```\n\n```text\nfunction hypotenuse(k, k1, k2) {\n  (function (sk1) {\n    (function (sk2) {\n      (function (ar) {\n         k(sqrt(ar));\n      }(add(sk1,sk2));\n    }(square(k2));\n  }(square(k1));\n}\n```\n\n========================================\n\nComments:\n- This question and its answer are definitely an interesting read: stackoverflow.com/questions/931762/&hellip;\n- your second example is trivially transformed into a similar tail recursive version as your first, and i suspect that all recursive functions in the form: `function(args) { ... .. &#47;&#47; final return always recursive return (inline operation and&#47;or function) recsub(number - 1) }` can be. Also your notion that \"compilers can't do this they just aren't smart enough!\" seems to be coming from a directly machine code perspective, you can transform your code easier if you use intermediate representations (ie Rust, which has a lot of what you would consider \"impossible\" optimizations)\n- Also my post isn't about transforming to tail recursion, its about eliminating the stack overflow. If your recursion can be transformed into an iterative version, then it doesn't matter if it can be transformed into tail recursion or not, the whole point of tail recursion is that it can be trivially transformed into yet another form where no SOs take place. Its going to be examples where recursion doesn't happen on the return line which are difficult.\n- I'd go so far as to say that writing such transpilers is possible, but not desired. Programmers don't want the compiler to mess with their algorithm. Being \"functionally equivalent\" (i.e. giving the same results for the same inputs) doesn't cut it. We also expect the compiled program to be equivalent to the code in its performance characteristics, its debuggability, its memory usage, its code size etc. The more clever the optimisations become, the more detached the output will get from the input and the *predictability* becomes lost.\n- @Bergi, That is a good point, however I'm not entirely convinced that the transformations here wouldn't have the same characteristics.\n- @snb I'm not sure if I see how the subtraction example is trivially transformed into a tail-recursive version. You need to know the result of the recursive call before it can be subtracted, so the current call must remain on the stack.It doesn't matter than the operation/function can be inlined because the length of the expansion isn't known up-front, it depends on the given argument(s).\n- Also, keep this in mind: lots of examples are a simple calculation that can be implemented using some sort of accumulator to pass some intermediate result from the current call to the next one. However, in some algorithms you won't be able to get the result until you've recursed to the base case, where recursion stops. Modeling these using iterative loops requires the implementation of a call stack analogue in the code itself. This too can run out of memory. A call stack is just another form of memory use.","metadata":{"transformedAt":"2026-08-18T18:32:17.945Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":173,"estimatedTokens":1692}}1043{"id":"stack-66489227","source":"stackoverflow","questionId":66489227,"title":"How to properly reuse symbol table in a multi-pass compiler","tags":["compiler-construction","code-generation","compiler-optimization","symbol-table"],"text":"Title: How to properly reuse symbol table in a multi-pass compiler\nTags: compiler-construction, code-generation, compiler-optimization, symbol-table\nSource: Stack Overflow\n\nQuestion:\nI'm currently building a multi-pass compiler for a block-structured language language. I have built a scope stack in semantic analysis phase. When entering a new scope, create a new table, push it to the stack and make it current scope table, then all symbols inside this scope are inserted into current table.\nWhen leaving a scope, the current table is recorded in the AST node, then pop it from the scope stack.\n\nThis way, in code generation phase, it does not have to build the symbol table all over again. Once it enters a new scope, it can simplely get the table from AST node and then push it to scope stack. I think this is the way that most of the compiler text books have recommended.\n\nIn most cases this works just fine, however, there is a corner case I don't know how to properly deal with. Consider the following code example:\n\n```\nint a = 1;\nint b = 2;\n\nvoid test()\n{\n int a = b;\n int b = 3;\n}\n```\n\nIt has two scopes: the global scope and the test()'s scope.\nSo, to do code generation for test(), we have to:\n\n- push global symbol table to the scope stack\n\n- get test()'s symbol table from AST node and push it to scope stack\n\nNow, when dealing with \"int a = b;\", it would find the local vaiable b from the scope stack, which is obviously not correct since local b is not declared yet.\n\nAny idea how to deal with this problem?\nDo I have to destroy all the symbol when leaving a scope and build the symbol table all over again in code generation phase?\n\nThanks guys!\n\n========================================\n\nCode:\n```text\nint a = 1;\nint b = 2;\n\nvoid test()\n{\n    int a = b;\n    int b = 3;\n}\n```\n\n========================================\n\nComments:\n- Depending on the semantic definition of your source language this may be a moot point. C#, for example, *defines* `test()` (in this case) to be the scope, so your code above will yield an error on the `b` reference because it hasn't been defined yet. That is, the local definition hides the global one even if it hasn't yet been defined in the code sequence.\n- Thanks for the hint about how C# would handle this. But I want the semantic to be similar with C. So I expect it to find the global variable b, until the local variable b is defined. Any ideas?\n- Why do you feel the need to construct the scope tree while parsing? Does your grammar require name resolution in order to disambiguate? (As with C/C++). If so, you might want to revisit that language design decision. :-)\n- This is known as annotating the AST. Like you said, it can be used to optimize symbol lookups, but it can also be used to store type information during semantic checking.","metadata":{"transformedAt":"2026-08-18T18:32:17.945Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":58,"estimatedTokens":700}}1044{"id":"stack-1179879","source":"stackoverflow","questionId":1179879,"title":"Where does context sensitivity get resolved in the C++ compilation process?","tags":["c++","parsing","compiler-construction"],"text":"Title: Where does context sensitivity get resolved in the C++ compilation process?\nTags: c++, parsing, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nYesterday I asked about C++ context sensitivity, see here. Among many excellent answers, here is the accepted one, by dmckee.\n\nHowever, I still think there's something to be said about this (maybe some terminological confusion?). The question amounts to: what part of compilation deals with the ambiguity?\n\nTo clarify my terminology: A CFG is a grammar that has only one non-terminal symbol on the left-hand-side of the rule (eg. `A->zC`), a CSG is one that has a terminal (plus a non-terminal) on the left-hand-side (`aAv->QT`), where uppercase letters are nonterminals and lowercase are terminals. \n\nIs any representation like the latter in the **grammar** parsing C++ source code? \n\nThank you, and sorry to push the issue.\n\n========================================\n\nTop Answer:\nFirst there is the difference between the language and the grammar. A\nlanguage is a set of strings. A grammar is a way of describing a set of\nstrings (one often say that a grammar \"generates\" the strings). A given\nlanguage may be described by several grammars.\n\nThe most well known kind of grammar are the production based one. Those\nwhere classified by Chomsky in\n\nunrestricted grammars, where there can be anything on the two sides of the\nproductions\n\nmonotonic grammar, where the left hand side is at most as long as the\nright hand side\n\ncontext-sensitive, where only one non-terminal is expanded\n\ncontext-free, where the left hand side of productions consist only of one\nnon terminal\n\nregular grammars, where the left hand side of productions consist only of\none non terminal and right hand side of production may have only one\nnon-terminal, as the latest element.\n\nMonotonic and context-sensitive grammars are also called type 1 grammars.\nThey are able to generate the same languages. They are less powerfull than\ntype 0 grammars. AFAIK, while I've seen proofs that there are languages\nwhich have a type 0 grammar but no type 1 one, I know of no example.\n\nContext-free grammars are called type 2 grammars. They are less\npowerfull than type 1 grammar. The standard example of language for which\nthere is no type 2 grammar but a type 1 grammar is the set of strings\nconsisting of an equal number of a, b and c, with the a before the b and\nthe b before the c.\n\nRegular grammar are also called type 3 grammars. They are less powerfull\nthan type 2 grammars. The standard example of a language for which there\nis no type 3 grammar but a type 2 grammar is the set of strings with\ncorrectly matching parenthesis.\n\nAmbiguity in grammars is something outside that hierarchy. A grammar is\nambiguous if a given string can generated in several ways. There are\nunambigous type 1 grammars, and there are ambiguous type 3 grammars.\n\nThen there are other kinds of grammars, which aren't part on Chomsky\nclassification (two levels grammars, attribute grammars, tree adjoining\ngrammars, ...) even if they are based on productions. Some of these are\neven able to describe the semantic of programming languages.\n\nThen there are parsing algorithms. Those often are based on CFG and impose\nmore restrictions to get better parsing speed (parsing a CSG needs\nexponential time, a CFG needs cubic time, common algorithms only linear\ntime). Those restrictions introduce other classes of grammars.\n\nCSG and monotonic grammars are in fact of little use to describe or compile\na programming language: their global behaviour isn't apparent and is\nsynthesised from local properties, so they are difficult to understand and\nattaching semantic to production is problematic, parsing them is costly --\nin general exponential -- and error handling is difficult. The non Chomsky\ngrammars were introduced to solve these issues.\n\nBack to C++. The standard describes the C++ language with a context-free\ngrammar but\n\nthere are ambiguities (the famous \"most vexing parse\"). So a compiler\nhas to recognize the ambiguities and use the right interpretation (i.e.\n`C x();` is a function declaration, not an object definition).\n\nthe grammar is not LR(1) (one of the most well known subset of CFG for\nwhich a linear parsing algorithm exist). Other algorithms (potentially\nmore costly in time or space) are used, either based on a more general\ntheory or by tweeking linear one to adapt them to C++ rules. Simplifying\nthe grammar and rejecting the incorrectly accepted programs in the\nsemantic analysis is also a possibility.\n\nthe correspondance between strings of characters and terminals is\nmodified (mainly by type and template declarations, the need to take that\ninto account in template definition has been solved with the use of\n`typename` and `template` for dependent names). This is solved by having\nthe lexing phase query the symbol table so that a given string will give\na terminal or another depending on the context.\n\nthere are additional constraints (need to declarare some identifiers,\ntype checking, ...) described in a more of less formal variant of\nenglish. This is usually considered semantic even if some more powerfull\ngrammar descriptions could handle them.\n\n========================================\n\nCode:\n```text\nA->zC\n```\n\n```text\naAv->QT\n```\n\n```text\nB = A();\n```\n\n```text\nB\n```\n\n```text\nA\n```\n\n```text\nC x();\n```\n\n```text\ntypename\n```\n\n```text\ntemplate\n```\n\n========================================\n\nComments:\n- What do you mean by ambiguity? Do you mean the parts of the language that cannot be expressed by a CFG?\n- Read dmckee's answer to my previous question. Couldn't be more explanatory than that.\n- @txwikinger: yeah, but c++ has more and worse ambiguities then that. Look at 280Z28's answer here (stackoverflow.com/questions/1172939/&hellip;) for a nasty one. That is out of my depth.\n- well. just take c and forward declarations. Forward declarations can never be checked by a context-free grammar.\n- right, but if so, the grammar *is* context-free, some processing is done before or even during the construction of the AST... Maybe I should stop speculating and go to the grammar of C++ itself. I have the feeling that I will find a CFG with some \"in-between\" processing. Thanks again, D.\n- That is how compilers work. The parser (i.e. syntactical analysis) uses a context free grammar, but a lot of checks are made due to the knowledge available from the symbol table.\n- Context sensitivity and ambiguity aren't the same thing.\n- Code example of what I meant: class Foo { public: void foo(void) { bar(); } private: void bar(void) { ++m_i; } int m_i; }; Both foo() and m_i are referenced before they are declared above, and yet this is legal C++. This can and has been dealt with by a parser alone.\n- +1 Of all the systems I've encountered, Quinn's MetaS seems positioned really well to handle the issues. In spite of his demonstrations and the theoretical power behind MetaS, AFAIK, it isn't used in any production C++ front end.","metadata":{"transformedAt":"2026-08-18T18:32:17.945Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":151,"estimatedTokens":1740}}1045{"id":"stack-29927986","source":"stackoverflow","questionId":29927986,"title":"I have a language where I need to determine if my flow control statement predicates are tautologies or contradictions","tags":["compiler-construction","compiler-errors","boolean-expression"],"text":"Title: I have a language where I need to determine if my flow control statement predicates are tautologies or contradictions\nTags: compiler-construction, compiler-errors, boolean-expression\nSource: Stack Overflow\n\nQuestion:\nHello I'm working on a domain specific language with my project group.\nWe want to report to the language user if the predicate in for example if(predicate) or while(predicate) are always true or always false. The logic operators implemented on the language are AND, OR, NEGATION as well as the comparison operators, , >=, I naturally only want to examine predicates with literals symbols such as false, true, numbers, and strings. Evaluating the values symbols of the expressions is no problem.\n\nAre all these things possible and are they existing information I can look at to achieve this maybe even some code?\n\n========================================\n\nTop Answer:\nIf the control flow is free of loops (and free of gotos and recusion; anything that can be used to simulate an unbounded loop) this is possible. You can use an SMT solver to solve pretty much arbitrary formulas and tell you whether a certain boolean expression is always true or false. The run time of that SMT solver might be impractical. Some SMT solvers are not complete, either. They sometimes answer \"unknown\". Look into Z3. It is free and of very high quality and performance.\n\nIf arbitrary control flow is involved (if this is a turing-complete language) then the Halting Problem says that you cannot solve this problem in general. You will have to make due with heuristics.\n\nI think your approach should not be part of the language design. Make this a static analysis tool that is allowed to sometimes answer \"I don't know\".\n\n========================================\n\nCode:\n```text\nWANG =\n{   sort Boolean;\n\n    true, false: Boolean;\n\n    Boolean generated by true, false;\n\n    not: Boolean ® Boolean;\n    and, or: Boolean --> Boolean ® Boolean\n               associative commutative;\n    implies, equivalent: Boolean --> Boolean ® Boolean;\n\n    axioms\n    -- 3 simplification rules for not\n        «not1»  not(true) = false;\n        «not2»  not(false) = true;\n        «not3»  not(not(x)) = x;\n    -- definition of 'implies'\n        «impl»  implies(x,y) = or(not(x),y);\n    -- definition of 'and'\n        «and»  and(x,y) = not(or(not(x),not(y)));\n    -- definition of 'equivalent'\n        «eq»    equivalent(x,y) = and(implies(x,y),implies(y,x));\n    -- simplification rules for 'or'\n        «or01»  or(true,x) = true;\n        «or02»  or(false,x) = x;\n        «or03»  or(x,not(x)) = true;\n        «or04»  or(x,or(not(x),y)) = true;\n        «or05»  or(x,x) = x;\n        «or06»  or(x,or(x,y)) = or(x,y);\n        «or07»  or(not(or(x,y)),x) = or(x,not(y));\n        «or08»  or(not(or(x,y)),or(x,z)) = or(x,or(not(y),z));\n        «or09»  or(not(or(x,y)),not(x)) = not(x);\n        «or10»  or(not(or(x,y)),or(not(x),z)) = or(not(x),z);\n        «or11»  or(not(or(not(x),y)),x) = x;\n        «or12»  or(not(or(not(x),y)),or(x,z)) = or(x,z);\n        «or13»  or(not(or(not(x),y)),not(or(x,y))) = not(y);\n        «or14»  or(not(or(not(x),y)),or(not(or(x,y)),z)) = or(not(y),z);\n endaxioms;\n}\n```\n\n========================================\n\nComments:\n- By free of loops do you mean that you cannot do this check inside of scope which is a unbounded loop or even in scopes which has a possible unbounded loop(we only have while loops)? Z3 sounds like exactly what we need, thanks.\n- There is a thing called the Goldbach Conjecture which is unproven. Can your language check that conjecture (any turing complete language can)? If yes then in all likelyhood you cannot answer all SMT queries. This is a good litmus test. The SMT solver is not better than all human researchers who have tried this conjecture. If you ask the solver \"Is the conjecture true?\" it will say \"unknown\".\n- I'm sure this ruleset does not solve the SAT problem which is even a subset of what's being asked here. You seem to claim that is solves SAT because this (allegedly) can answer the question \"is there an assignment for the ground literals such that the resulting formula is true\". That's SAT.\n- I do not claim it solves the SAT problem. It solves the problem of determining if the boolean proposition is a tautology, which is what OP asked.\n- Determining that *is* solving the SAT problem. If p(x) is UNSAT then !p(x) is a tautology. So you can determine whether p(x) or !p(x) is a tautology and you know whether the problem is SAT or UNSAT.\n- I'm going to ignore the discussion about SAT as a red herring. The only relevant question for OP is, \"is this a decision procedure for determining if a boolean equation is a tautology?\" I claim it is, and I make that claim based on paper by a \"Dr. Wang\" from the 1930s(?), in which he published this result. I cannot find the reference to this paper online; my memory of this is from the 80s, and we have used this in production tools. There is a discussion about decision procedures based on rewriting boolean equations involving another Wang; I'm sure it is different. I will keep looking.\n- In the meantime, here is a reference to the modern Wang paper: \"Towards Feasible Solutions of the Tautology Problem, Dunham and Wang, Annals of Mathematical Logic 1976. PDF: ac.els-cdn.com/0003484376900206/&hellip;\n- I'm with Ira here, although I think the proof might be non-trivial. A simpler and roughly equivalent algorithm would be mechanically reducing the expression to either disjunctive or conjunctive normal form, and then factoring using DeMorgan's law. SAT is not actually a terribly difficult problem unless you want to solve it in less than exponential time; however, in most computer programs, conditional expressions in `if` and `while` statements do not have enough terms to make a simple exponential algorithm impractical.\n- If you check out the rewrites as posted, what they do is normalize away \"and\" \"eqv\" \"imp\" leaving just \"nots\" and \"or\". The rules for simplifying the or terms is where the magic happens. No, I don't have the proof and I agree it might be complex; that's why Dr. Wang published his paper :-} But you can see the \"or\" rules read left-to-right strictly decrease term size, so it is clear it terminates. The whole reason I glommed onto this set of rules back in the 80s is because it is blindingly simple to build a tautology detector with this. (No, there's no guarantee it is lightning fast).\n- Hey thanks for the comments ill update the origin post with the solution when I eventually get around to implementing it, thanks","metadata":{"transformedAt":"2026-08-18T18:32:17.945Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":78,"estimatedTokens":1641}}1046{"id":"stack-64116558","source":"stackoverflow","questionId":64116558,"title":"How does java compiler deal with code like `public interface A extends B`","tags":["java","generics","interface","compiler-construction"],"text":"Title: How does java compiler deal with code like `public interface A extends B`\nTags: java, generics, interface, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI just started writing java and today I read some code like this:\n\n```\npublic interface A extends B{\n ...\n}\n\npublic interface B>{\n ...\n}\n```\n\nOf course I can understand the code, but it makes me really confused. It looks like ... I used myself to create myself? How the compiler deal with it?\n\n========================================\n\nTop Answer:\nTo answer your explicit question, the compiler validates generics by checking constraints against each other, validating that the specific types in question are legal/matching the generic references. After that (for the output code) generic types are erased. This is called 'type erasure'. The explicit steps are explained here: https://docs.oracle.com/javase/tutorial/java/generics/erasure.html\n\n*But more interesting are the semantics of what you apparently encountered.*\n\nGeneric types don't mean 'use to create' but rather 'regarding' (in whatever way).\nTake `List` for instance. `List` means 'List containing instances of class A'. For the code calling an instance of that, it means that all methods referencing the generic type will directly or indirectly regard object(s) of type A.\n\nAny class implementing `public interface B>` will be required to regard themselves in that respect. The point of this might be that at a later point methods or attributes that refer to `E` will then yield the type implementing them. An example could be:\n\n```\npublic interface Chainable> {\n public void append(E followup);\n\n public E getNext();\n}\n\n...\n\npublic class MyLink implements Chainable {\n public void append(MyLink l) {\n ...\n }\n\n public MyLink getNext() {\n ...\n }\n}\n```\n\nThis way, it is ensured, that any class implementing Chainable will have a method taking, and one returning an object of that class, rather than just any object.\n\nWhile the above alone is useful in terms of constraints I cannot see the better use of your example's `public interface A extends B`. The reason for this is, that any class now implementing `A` is just guaranteed to regard `A` but not the implementing class itself (which would be much more useful). Example:\n\n```\npublic class C implements A {\n public A methodDeclaredInB(A someParam) {\n ...\n }\n}\n```\n\nThe method above only knows `A` at compile time, but not `C`. If instead someone wrote:\n\n```\npublic class C implements B {\n public C methodDeclaredInB(C someParam) {\n ...\n }\n}\n```\n\nthen class `C` could be used instead of 'only' `A`.\n\nThere may be cases where it is sufficient to know type `A`, but usually you want type `C`, so making a (itself non-generic) interface `A` that extends `B` in such a way seems a fragile shortcut to the more verbose but more useful example above.\n\n========================================\n\nCode:\n```text\npublic interface A extends B<A>{\n    ...\n}\n\npublic interface B<E extends B<E>>{\n    ...\n}\n```\n\n```text\npublic interface Foo {\n    Foo ZERO = new Bar();\n}\n\npublic class Bar implements Foo {\n    ... ZERO ...\n}\n```\n\n```java\npublic interface Chainable<E extends Chainable<E>> {\n  public void append(E followup);\n\n  public E getNext();\n}\n\n...\n\npublic class MyLink implements Chainable<MyLink> {\n  public void append(MyLink l) {\n    ...\n  }\n\n  public MyLink getNext() {\n    ...\n  }\n}\n```\n\n```java\npublic class C implements A {\n  public A methodDeclaredInB(A someParam) {\n    ...\n  }\n}\n```\n\n```java\npublic class C implements B<C> {\n  public C methodDeclaredInB(C someParam) {\n    ...\n  }\n}\n```\n\n```text\nList\n```\n\n```text\nList<A>\n```\n\n```text\npublic interface B<E extends B<E>>\n```\n\n```text\nE\n```\n\n```text\npublic interface A extends B<A>\n```\n\n```text\nA\n```\n\n```text\nA\n```\n\n```text\nA\n```\n\n```text\nC\n```\n\n```text\nC\n```\n\n```text\nA\n```\n\n```text\nA\n```\n\n```text\nC\n```\n\n```text\nA\n```\n\n```text\nB\n```\n\n========================================\n\nComments:\n- Yes: it's confusing. But useful.\n- Can you elaborate Maurice Perry? I have been coding in java for over 13 years and have never seen this idiome. I don't see what this is good for.\n- What exacly is your question? The answer to 'how does the compiler deal with it' is that it compiles it, but that involves lexical and syntactic and semantic analysis, all of which is the subject of long courses and large books. Unclear what you're actually asking.\n- @TreffnonX Have a look at `Comparable`. You must have seen that.\n- I believe this should asnwer your question: stackoverflow.com/questions/7282677/&hellip;\n- @MarquisofLorne Comparable is just Comparable. I know generics in general, I just don't know what good the above example would do. How would `A extends B` be a useful constraint? What would the implication be?\n- @TreffnonX Any class that implements `Comparable` should be defined as `class X implements Comparable`. Exactly the same as in the question.\n- @MarquisofLorne *implements* not *extends*! I get how a class implements Comparable where C is the class, but this example is an interface. Taking Comparable as example, it would just guarantee that whatever implementation of that interface existed would also be comparable with any other class implementing that interface. This seems fragile at best to me. I think the implementing class should describe the generic type itself.\n- @MarquisofLorne It works the same, but it means something else, because an interface is usually extended itself. So any backreference on the interface will reference the interface, but not the class implementing it. This *is* a very material difference in semantics. All I am saying is, that this makes a lot of sens with classes, but (imho) little regarding interfaces. (And I don't mean generic interfaces in gereral, but only regarding the above examples). I can't imagine what the 'API developer' intended with this, but then again, the context is missing here.\n- @MarquisofLorne I can't claim comprehensibility for my argument, but relevance is in the eye of the beholder. Whether it is relevant for OP is for them to decide. I am just noting that I cannot imagine an applciation for above pattern regarding an interface. If you have a comprehensible example where it may be useful, I will be taught better.\n- @TreffnonX The beholder is the OP, and that's not what he asked about. You may not be able to think of a use, but the OP has clearly *already* encountered a use, and is asking how the compiler deals with it. Furthermore, you claimed something much stronger than not being able to think of a use: you claimed that you had never encountered it, when you had.\n- @MarquisofLorne I have not encountered it. And Comparable is not an applicable example. Again, this does not mean, that it makes no sense, just that I can't see it. And I don't intent to stirr dirt here, just mentioning the (to me) exotic character of the examples asked about.\n- Your example would compile with `Chainable` declared as `public interface Chainable`, so it doesn't demonstrate any purpose for the `extends Chainable`.\n- \"This way, it is ensured, that any class implementing Chainable will have a method taking, and one returning an object of that class\" This is not true. A class implementing `Chainable` will only be guaranteed to have one method taking, and one method returning, `E`, not necessarily the type of the implementing class. Only if the implementing class chooses to implement `Chainable` with itself as the type argument will it have one method taking, and one method returning, its own type.\n- @newacct \"Only if the implementing class chooses to implement Chainable with itself as the type argument will it have one method taking, and one method returning, its own type.\" - But that is my example \"That way\" referrs to the class implementing `Chainable`, which is exactly the point. And as for the earlier comment: `public interface Chainable>` binds `E` to `Chainable`. This is important and makes a difference. You can easiely verify that in an IDE: A type *not* `Chainable` cannot be the generic type `E`, which is the purpose.\n- Important here is: Fields of interfaces are static final! That should be explicit. Here is an q/a to that: stackoverflow.com/questions/9446893/fields-in-interfaces Explanation is correct though :)\n- @TreffnonX indeed, `public static final` even. Maybe I also should have mentioned: Classes Foo and Bar are loaded, On initializing Foo an initialized Bar is required. I would prefer explicit too, where it not that it is so useful for enum like constants for open value domains - without syntactic fluff (like `this.`).\n- 'Generic parameters are used by reference' is meaningless.","metadata":{"transformedAt":"2026-08-18T18:32:17.945Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":20,"totalLines":217,"estimatedTokens":2160}}1047{"id":"stack-31634461","source":"stackoverflow","questionId":31634461,"title":"How do I construct abstract syntax trees from a list of tokens using a context-free grammar?","tags":["javascript","parsing","compiler-construction","abstract-syntax-tree","context-free-grammar"],"text":"Title: How do I construct abstract syntax trees from a list of tokens using a context-free grammar?\nTags: javascript, parsing, compiler-construction, abstract-syntax-tree, context-free-grammar\nSource: Stack Overflow\n\nQuestion:\nI'm writing a C compiler in Javascript, purely for enrichment (as of yet, I expect no practical use for it, and I will likely not maintain it).\n\nI wrote a lexer that can successfully tokenize, given a list of regular expressions and the type matched by that regular expression, any string.\n\nI have been able to successfully tokenize C source code (somewhat reduced C, to be fair; I need to add more tokenizer patterns to capture everything). I now want to construct ASTs as an intermediate form between the source language and the translated assembly.\n\nTo do this, I am trying to implement a function that uses a context-free grammar, defined as an object with\n\n- key &rarr; target (expression, function declaration, cast expression, etc), and\nvalue &rarr; array of mappings\n\n- (where a mapping is an array [order is important] of the symbols that make up the target).\n\nHere is a sample CFG one might feed the parser (this is an adapted excerpt from this C grammar):\n\n```\nvar cfg = {\n \"cast_exp\": [\n [\"unary_exp\"],\n [\"(\", \"type_name\", \")\", \"cast_exp\"]\n ], \n \"unary_exp\": [\n [\"primary_exp\"],\n [\"++\", \"unary_exp\"],\n [\"--\", \"unary_exp\"]\n ],\n \"primary_exp\": [\n [\"id\"]\n ]\n};\n```\n\n`id` is one of the types my tokenizer picks up, so I suppose we can consider \"primary_exp\" a start symbol.\n\nNow, my thought is to do this recursively; that is, pick up the first token and match it against one of the starting symbols. Recurse on the remaining tokens, sending across the target we matched in the previous call, and see what production rule is composed of the target we just matched.\n\nThis isn't making too much sense to me and the way I see it, I will get lost in infinite recursion (or encounter stack overflow on really long source files).\n\n**How do I write a function that can walk my array of tokens and, using the CFG described above, construct ASTs?** Since I'm doing this for enrichment and as a personal challenge, if you'd like you can provide code, but I'm looking more for guidance and a broad-sense description of such an algorithm.\n\n========================================\n\nCode:\n```text\nvar cfg = {\n    \"cast_exp\": [\n        [\"unary_exp\"],\n        [\"(\", \"type_name\", \")\", \"cast_exp\"]\n    ],   \n    \"unary_exp\": [\n        [\"primary_exp\"],\n        [\"++\", \"unary_exp\"],\n        [\"--\", \"unary_exp\"]\n    ],\n    \"primary_exp\": [\n        [\"id\"]\n    ]\n};\n```\n\n```text\nid\n```\n\n```text\n{  I1  I2 ...  In }\n```\n\n```text\nR = A B C D;\n```\n\n```text\nR = A B <dot> C D ;\n```\n\n```text\n{  P = Q <dot> R S ;I1\n    R = A B <dot> C D ;\n    C = <dot> X Y ;\n }\n```\n\n```text\nR = A B C <dot> D;\n```\n\n```text\nR = A B C D <dot> ;\n```\n\n```text\nP = Q <dot> R S ;\n```\n\n```text\n{  primary = <dot> id ; }\n```\n\n========================================\n\nComments:\n- Closer: the question is not too broad, there are not too many answers, and one *can* be provided in format compatible with the site. (See my answer!). Your close reason is not valid.\n- Thanks for the answer! What if there are multiple *goals* to begin with? For instance, in C, an AST could be formed from `int i = 0;` as well as with `i = 1;`, however the starting symbol/goal for the first one is a type specifier, while for the second it's an id.\n- Here is an Earley Parser I wrote in Python, for reference. github.com/TheArtOfEngineering/NLPParser/blob/master/parse2.&zwnj;&#8203;py\n- @Purag: You simply put your multiple goal rules into the starting set. Parsing succeeds if *any* of the goal rules reduce, *and* the input is entirely consumed. Note the *and* condition.\n- Ah, so a *goal* isn't the first symbol matched, it's the rule for the complete statement. As in, one entire translatable unit. We read in `int` and say \"hey, move the dot forward for each rule that starts with `type_specifier`,\" if I'm understanding correctly?\n- And for a complicated language like C, would there be an issue (besides performance hit) with including all the rules in the initial set?\n- Technically it will work. Yes, you'll take quite a performance hit on the first few state transitions, but after that, the item sets will shrink to match what the input actually forces pretty fast. We do something like this to do *pattern* parsing in our DMS system (see my bio) to try to match a code fragment to the part of the language it represents (and we even do this for C!). We don't use an Earley parser because the costs are pretty high for parsing really big files; we use GLR instead, but GLR parsers are *much* harder to implement. (They pay off in performance very nicely).\n- \"goal isn't the first symbol matched\" ... Right. A goal rule is what you decide the goal rule to be. People parsing full source files think the goal rule is the \"compilation unit\", but you can choose the goal rule to be any or all of the rules in your grammar, *if* you remember to implement the *and* empty-input-stream check. (If a rule reduces and the input stream still has data, then you aren't don't parsing yet).\n- Your latest edit was gold. I was sitting here wondering \"but why can't I just replace my lexer with terminal rules in the CFG?\" My tokenizer actually sucks right now, I need to optimize it, but I'm very, very interested in parsing everything in one pass instead of two.\n- @Purag: Well, if you hate your lexer you can always write a better one. But you don't have to have two passes just because you hate it :-} In any parser there is clearly a point where a next input token is needed (either a terminal, or a character if you go nanoscopic). At that point, you can simply call the lexer to get the next token. Everything then happens in one pass.\n- \"goal isn't ...\" comment above needs a patch: *aren't don't* should have been *aren't done*. (Fastest fumble in the West).\n- Hey Ira, thought I'd update you...I was finally able to put together the Earley parser! Check it out. The only thing I haven't resolved the issue of empty productions yet, but I'm working on it. :)\n- Congratulations. Yes, empty productions are fun, but don't change the basic idea. What did you do about the lexer?\n- @IraBaxter: Haven't worked on it yet; I think I have an idea that will improve it, but we'll see. For now, my parser now accepts grammars in BNF, so all I need to do is write an `eval()` function to go through the parse tree and generate a compatible grammar for my parser, and I'll have a parser generator. :) Check it out here\n- @IraBaxter Well, turns out I don't need to change my tokenizer, it's plenty fast. After doing some CPU profiling I learned that what was really holding up the page was printing out the results. Haha.","metadata":{"transformedAt":"2026-08-18T18:32:17.945Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":10,"totalLines":122,"estimatedTokens":1695}}1048{"id":"stack-22831757","source":"stackoverflow","questionId":22831757,"title":"Syntax Directed Translation and Semantic Analysis","tags":["parsing","compiler-construction","compiler-theory"],"text":"Title: Syntax Directed Translation and Semantic Analysis\nTags: parsing, compiler-construction, compiler-theory\nSource: Stack Overflow\n\nQuestion:\nI'm having difficulties in understanding the exact differences between *Syntax Directed Translation* (SDT) and *Semantic Analysis*.\n\nBy reading the Dragon Book, I know that SDT is like an extension to CFG. You add rules or actions to the CFG that can be evaluated during parsing and will create an Abstract Syntax Tree. (Or concrete syntax tree that will then be converted to an AST)\n\nHowever, I don't understand what the purpose of Semantic Analysis will be after this part. Because it seems to me, that with SDT, you will do semantic analysis. Isn't SDT doing some type checking as well if it implements rules/actions?\n\nThank you.\n\n========================================\n\nComments:\n- I find it quite odd that a quick look at the content of the dragon book, reveals that there is no sperate section about semantic analysis. Is it something very difficult? (as a result of which it is beyond the scope of the text?)","metadata":{"transformedAt":"2026-08-18T18:32:17.945Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":17,"estimatedTokens":266}}1049{"id":"stack-24724333","source":"stackoverflow","questionId":24724333,"title":"AST manipulation in Python","tags":["python","python-3.x","compiler-construction","abstract-syntax-tree"],"text":"Title: AST manipulation in Python\nTags: python, python-3.x, compiler-construction, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI want to be able (just for fun and didactic purposes) to modify the AST of a function just to change a string that should be printed out.\n\nNow after some experiments I thought that something like this should work, however even if there are no errors the second function never prints anything.\n\nAny idea about what I'm missing?\n(The second function should just print 'world' instead of 'hello world' basically).\n\n```\ndef function_hello():\n print('hello world')\n\ndef remove_hello():\n import ast\n import inspect\n source = inspect.getsource(function_hello)\n ast_tree = ast.parse(source)\n st = ast_tree.body[0].body[0].value.args[0]\n st.s = st.s.replace('hello ', '')\n return compile(ast_tree, __file__, mode='exec')\n\nif __name__ == '__main__':\n function_hello()\n exec(remove_hello())\n```\n\n========================================\n\nCode:\n```text\ndef function_hello():\n    print('hello world')\n\n\ndef remove_hello():\n    import ast\n    import inspect\n    source = inspect.getsource(function_hello)\n    ast_tree = ast.parse(source)\n    st = ast_tree.body[0].body[0].value.args[0]\n    st.s = st.s.replace('hello ', '')\n    return compile(ast_tree, __file__, mode='exec')\n\n\nif __name__ == '__main__':\n    function_hello()\n    exec(remove_hello())\n```\n\n```text\nif __name__ == '__main__':\n    function_hello()\n    exec(remove_hello())  # This does not call the function.\n    function_hello()  # <-----------\n```\n\n```text\nfunction_hello\n```\n\n```text\nfunction_hello()\n```\n\n========================================\n\nComments:\n- You can just change the source and compile that.","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":72,"estimatedTokens":425}}1050{"id":"stack-19623234","source":"stackoverflow","questionId":19623234,"title":"Compiler codegenerator verification","tags":["testing","compiler-construction","code-generation","llvm","verification"],"text":"Title: Compiler codegenerator verification\nTags: testing, compiler-construction, code-generation, llvm, verification\nSource: Stack Overflow\n\nQuestion:\nI am working on a new backend for the LLVM compiler. The backend is coming along nicely but I am arriving at a point where i want to verify if the codegenerator is operating correctly. I have got a processor simulator available which is very accurate and which I would like to use for the verification. \n\nUsing the LLVM-lit doesn't seem like a good option for me because there is no way to integrate the simulator in the testing process.\n\nCurrently my testing strategy involves writing test programs with which i try to check as many statements as possible. Following is an example of checking arithmetic. \n\n```\nc = a + a; \nif (c != 6) return 4;\nc = a + a + a; \nif (c != 9) return 5;\n// etc\n```\n\nI am already noticing that it is difficult to find as many corner cases as possible. \n\nIs this a smart way to verify the codegenerator? If so, does anybody know of a pre existing project which includes these kind of tests?\n\n========================================\n\nTop Answer:\nYou may want to take a look at the LLVM test suite - a set of \"whole programs\" that are compiled first with a native platform compiler (e.g. `gcc`) and then with LLVM and the results are compared.\n\nSo if your CPU already has some compiler able to generate code for it, it's a good option. If not, you can rig it so the expected output is generated on your development machine and compared to the output generated by the code compiled with your backend.\n\n========================================\n\nCode:\n```text\nc = a + a;          \nif (c != 6) return 4;\nc = a + a + a;      \nif (c != 9) return 5;\n// etc\n```\n\n```text\nconfig.substitutions.append( ('%microblazeecc', ' ' + config.ecc + ' ' +\n                            '-target microblaze-ellcc-linux ') )\nconfig.substitutions.append( ('%microblazeexx', ' ' + config.ecc + '++ ' +\n                            '-target microblaze-ellcc-linux ') )\nconfig.substitutions.append( ('%microblazerun', ' ' + ellcc + '/bin/qemu-microblaze ') )\n```\n\n```text\n// Compile and run for every target.\n// RUN: %armexx -o %t %s && %armrun %t  | FileCheck -check-prefix=CHECK %s\n// RUN: %armebexx -o %t %s && %armebrun %t | FileCheck -check-prefix=CHECK %s\n// RUN: %i386exx -o %t %s && %i386run %t | FileCheck -check-prefix=CHECK %s\n// RUN: %microblazeexx -o %t %s && %microblazerun %t | FileCheck -check-prefix=CHECK %s\n// RUN: %mipsexx -o %t %s && %mipsrun %t | FileCheck -check-prefix=CHECK %s\n// RUN: %mipselexx -o %t %s && %mipselrun %t | FileCheck -check-prefix=CHECK %s\n// RUN: %ppcexx -o %t %s && %ppcrun %t | FileCheck -check-prefix=CHECK %s\n// FAIL: %ppc64exx -o %t %s && %ppc64run %t | FileCheck -check-prefix=CHECK %s\n// RUN: %x86_64exx -o %t %s && %x86_64run %t | FileCheck -check-prefix=CHECK %s\n// CHECK: foo.i = 10\n// CHECK: bye\n#include <cstdio>\n\nclass Foo {\n    int i;\npublic:\n    Foo(int i) : i(i) { }\n    int get() { return i; }\n    ~Foo() { printf(\"bye\\n\"); }\n};\n\nint main(int argc, char** argv)\n{\n    Foo foo(10);\n    printf(\"foo.i = %d\\n\", foo.get());\n}\n```\n\n```text\ngcc\n```\n\n========================================\n\nComments:\n- Take a look at this approach (Csmith): cs.utah.edu/~regehr/papers/pldi11-preprint.pdf\n- Thanks for the response! Unfortunately using the test suite is not an option because the simulator that I am using doesn't have an option for a \"clean\" output. I can only see what instructions are executed and what the status is of the register file. Until now I have been testing the code generator by manually checking the return register for the expected value.","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":88,"estimatedTokens":915}}1051{"id":"stack-28935020","source":"stackoverflow","questionId":28935020,"title":"Language class compilable to heapless runtime","tags":["compiler-construction","programming-languages","computation-theory","memory-model","turing-complete"],"text":"Title: Language class compilable to heapless runtime\nTags: compiler-construction, programming-languages, computation-theory, memory-model, turing-complete\nSource: Stack Overflow\n\nQuestion:\nSo in the general case, a program uses both memory in the stack (automatically managed) and heap (garbage collected or manually managed).\n\nWhat is the class of programs that can be compiled to use memory in a stack-like fashion only and no heap allocation? Is it still Turing-complete with some other trade off (e.g. code blow-up) or it's a weaker language class?\n\n========================================\n\nCode:\n```text\nletregion ρ in ...\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":15,"estimatedTokens":158}}1052{"id":"stack-17523814","source":"stackoverflow","questionId":17523814,"title":"How to compile for the Erlang VM","tags":["compiler-construction","erlang","leex","yecc"],"text":"Title: How to compile for the Erlang VM\nTags: compiler-construction, erlang, leex, yecc\nSource: Stack Overflow\n\nQuestion:\nI'm trying to implement a small description language for user scripting in an application. Basically users describe how objects are created from other objects. (Not really a programming language). I could use JSON as well (but it would be more verbose).\n\nI use leex and yecc and i have a decent parse tree. Say it is something like this : \n\n```\n{def,double, %% function name double\n [{x}], %% list of input names\n %% return expression, in this case {Operator, Operand1, Operand2}\n {'*',{var,x},{number,2}} \n}.\n```\n\nWith this tree, i would like to build something like this:\n\n```\ndouble(State) ->\n _Var1 = some_app:get_input(State,x),\n _Var1 * 2.\n```\n\nBut i don't know where to start. I can read the tree at runtime and build funs but i really want to learn how to compile (and hope better performance).\n\nShould i transform my parse tree to Erlang AST and then compile erlang module ?\n\nThanks.\n\n========================================\n\nCode:\n```text\n{def,double,   %% function name double\n    [{x}],     %% list of input names\n    %% return expression, in this case {Operator, Operand1, Operand2}\n    {'*',{var,x},{number,2}} \n}.\n```\n\n```text\ndouble(State) ->\n    _Var1 = some_app:get_input(State,x),\n    _Var1 * 2.\n```\n\n```text\nleex\n```\n\n```text\nyecc\n```\n\n```text\ncerl\n```\n\n```text\ncompile\n```\n\n```text\n.beam\n```\n\n```text\nleex/yecc\n```\n\n========================================\n\nComments:\n- found this cs.indiana.edu/~dyb/pubs/nano-jfp.pdf\n- You might try watching some of Robert Virding's videos. For instance, Implemting Languages on the Erlang VM. I believe he has at least 2 or 3 videos on similar topics. Also his github, has several examples of code. Additionally, you could check out Elixir or Joxa which are also implemented on the Erlang VM.\n- yes i'm taking example on elixir. I've also found this, good infos but very small code compiled. didn't know Joxa, must check this. thanks\n- Is there a way to visualize the grammar ? Perhaps something on the lines of metacpan.org/pod/GraphViz2::Parse::Yacc\n- @TheMitraBoy I think you would have to convert your grammar to a classic Yacc grammar file (or any other popular format). I think I dit that a while ago to generate railway diagrams, but I cannot remember.","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":8,"totalLines":80,"estimatedTokens":585}}1053{"id":"stack-14978233","source":"stackoverflow","questionId":14978233,"title":"Virtual table in llvm (llvm-py)","tags":["python","compiler-construction","llvm","vtable","llvm-ir"],"text":"Title: Virtual table in llvm (llvm-py)\nTags: python, compiler-construction, llvm, vtable, llvm-ir\nSource: Stack Overflow\n\nQuestion:\nI'm using llvm-py to create a DIY compiler for some artificial and need to have a virtual method table in the globe scope. My concept is to have several arrays of function pointers (one for each class). Unfortunately there's no LLVM IR Builder for a global scope and I cannot use ptrtoint in order to have uniform type of all array elements (otherwise I would store function addresses as 64-bit ints and cast them to appropriate types before calling). Do you know any reasonable solution? It can also be illustrated with C++ LLVM api, because llvm-py is very similar.\n\n========================================\n\nCode:\n```text\n@global = global [4 x i64*] [\n  i64* bitcast (void()* @f to i64*),\n  i64* bitcast (float(i32)* @g to i64*),\n  ...\n]\n```\n\n```text\nIRBuilder\n```\n\n```text\nGlobalVariable\n```\n\n```text\nConstantExpr::getBitCast()\n```\n\n```text\nFunction\n```\n\n```text\ni64\n```\n\n```text\nGlobalVariable\n```\n\n========================================\n\nComments:\n- Did you check what Clang emits for C++ vtables?\n- Yep, it does some name mangling tricks that make the code pretty hard to understand and I wanted to avoid it.\n- All right, I have totally forgotten about this bitcast method available in ConstantExpr. Thanks a lot!","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":7,"totalLines":48,"estimatedTokens":339}}1054{"id":"stack-11664693","source":"stackoverflow","questionId":11664693,"title":"Idioms for compiler development in Clojure","tags":["compiler-construction","clojure"],"text":"Title: Idioms for compiler development in Clojure\nTags: compiler-construction, clojure\nSource: Stack Overflow\n\nQuestion:\nI'd like to explore the power of Clojure for compiler development, but I cannot find example to start with. \n\nI am a total newbie (coming from Ruby), but I'm convinced that Clojure should be ideal for this purpose.\n\nLet's precise what I'm looking for :\n\n- start from a simple **AST defined in clojure** (for let say a simple sequential language : if, while, func, assign, expression)\n\n- simple **visitor for this AST** (pretty printer for example)\n\n- I am not really interested by lexing/parsing (as I consider s-expression as sufficient for my DSL syntax)\n\nWhat are the right idioms for this in Clojure ?\n\n========================================\n\nCode:\n```text\n;; functions map, can be easily extended with new functions\n;; map is of keyword -> code generating function\n(def funcs {:if \n                 (fn [cond exp1 exp2] `(if ~cond ~exp1 ~exp2))\n            :neg \n                 (fn [exp1] `(- 0 ~exp1))\n            :plus \n                 (fn [& exps] `(+ ~@exps))})\n\n;; compile directly to Clojure source code\n(defn my-compile [code]\n (cond \n   (sequential? code)   ;; if we have a list, look up the function in funcs\n     (cons (funcs (first code)) (map compile (rest code))) \n   :else                ;; treat anything else as a constant literal\n     code))\n\n;; example compilation to a Clojure expression\n(my-compile `(:if true (:neg 10) (:plus 10 20 30)))\n=> (if true (clojure.core/- 0 10) (clojure.core/+ 10 20 30))\n\n;; evaluate compiled code\n(eval (my-compile `(:if true (:neg 10) (:plus 10 20 30))))\n=> -10\n```\n\n```text\ndefrecord\n```\n\n========================================\n\nComments:\n- You do not have to limit your search to the Clojure examples. There are many examples from the other Lisps which can easily be ported to Clojure. E.g., bit.ly/3t1DX or bit.ly/I2LFdr\n- I agree. This remark could also apply for Functional languages in general, but I'd like to restrict to Clojure, as it seems to be more accessible/attractive than older lisps, for non lispians. \"The best lisp ever\" said someone ?\n- no, functional languages in general is a totally different topic. Clojure is a Lisp, in a sense that it features the most important part of a Lisp - a decent macro system. And this is what actually makes Lisp different, makes it more fit for compilers development than any other language. Take a look at my second link, it employs a specialised DSL for defining visitors. This is exactly an idiomatic Lisp way of doing things. And it is the same in all the Lisps. As for \"the best lisp ever\" I personally would not agree. Lisp with no `cons` is not quite a Lisp. And `recur` is horrible.\n- P.S., you can also take a look at Racket - the whole thing is just brilliant, and it implements many various compilers inside. It is probably the single richest source of idiomatic Lisp compilers code.","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":60,"estimatedTokens":733}}1055{"id":"stack-24793847","source":"stackoverflow","questionId":24793847,"title":"Create language with Antlr","tags":["compiler-construction","antlr","antlr4"],"text":"Title: Create language with Antlr\nTags: compiler-construction, antlr, antlr4\nSource: Stack Overflow\n\nQuestion:\nLet's say I have created a new Awesome language which is like java in syntax but instead of `package` it uses `pack`. So let's say that this new language is a template language for Java.\n\nI have created the awesome.g4 and compiled it, so Antlr gave me the BaseListener,Listener,Lexer & Parser.\n\nI want to create a program with this new Awesome language as a source code, so I can think two options:\n\nConvert it first into Java and then compile the java into binary (I\nhope I'm correct, I mean the .class files)\n\nCompile the Awesome program directly into binary.\n\nThe only relevant repo/code I have found is antlr4-csharp.\n\nSince I want to save humanity and code in my new Awesome language from now on, *is it possible to create executable code given that language as the source code, how?*\n\n========================================\n\nCode:\n```text\npackage\n```\n\n```text\npack\n```\n\n========================================\n\nComments:\n- Thanks for helping saving the world ahoffer! The link clarifies the big picture on how to create this.\n- do you have an updated link?","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":36,"estimatedTokens":294}}1056{"id":"stack-17533856","source":"stackoverflow","questionId":17533856,"title":"clang code completion - implementation design","tags":["c++","compiler-construction","clang","llvm","code-completion"],"text":"Title: clang code completion - implementation design\nTags: c++, compiler-construction, clang, llvm, code-completion\nSource: Stack Overflow\n\nQuestion:\nIs there some hidden documentation how the code-completion part of clang is implemented? What i've found out so far is that a special token (tok::code_completion) gets injected into the lexer and is handled in the parser. After observing such a token the parser can fill in possible completion strings.\n\nWhat I don't understand:\n\nIf the called functionality decides that we can insert a variable which is available in the current context. How is such a case handled?\n\n```\nstruct FooBar {\n void foo() {\n ba>\n }\n void bar() {\n }\n};\n```\n\nThe parser hasn't seen bar but it's valid to call it.\n\n========================================\n\nCode:\n```text\nstruct FooBar {\n    void foo() {\n        ba<<code completion here>>\n    }\n    void bar() {\n    }\n};\n```\n\n```text\n/// ParseCXXInlineMethodDef - We parsed and verified that the specified\n/// Declarator is a well formed C++ inline method definition. Now lex its body\n/// and store its tokens for parsing after the C++ class is complete.\nParser::DeclPtrTy\nParser::ParseCXXInlineMethodDef(...\n```\n\n```text\n/// ParseLexedMethodDefs - We finished parsing the member specification of a top\n/// (non-nested) C++ class. Now go over the stack of lexed methods that were\n/// collected during its parsing and parse them all.\nvoid Parser::ParseLexedMethodDefs(...\n```\n\n```text\nParser::ParseCXXInlineMethodDef()\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":54,"estimatedTokens":374}}1057{"id":"stack-14037090","source":"stackoverflow","questionId":14037090,"title":"What does it mean to say that the source code is always available to interpreters?","tags":["c++","compiler-construction","interpreter"],"text":"Title: What does it mean to say that the source code is always available to interpreters?\nTags: c++, compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nFrom **Thinking in C++ - Vol. 1**:\n\n Interpreters have many advantages. The transition from writing code to\n executing code is almost immediate, and the source code is always\n available so the interpreter can be much more specific when an error\n occurs.\n\nWhat does the bold line mean?\n\nDoes it mean that the interpreter cannot work unless whole of the program is in memory? Which means we cannot divide the program into modules and then have the modules interpreted as and when needed (like we do with compilers)? \n\nIf yes, then what's the reason behind this?\n\n### UPDATE:\n\nFrom **Thinking in C++ - Vol. 1**:\n\n **Most interpreters require that the complete source code be brought into the interpreter all at once.**\n\nSo, does this now indicate what I wrote above?\n\n========================================\n\nTop Answer:\nFor *compiled languages*, when you run the program, you don't have the source code — you have compiled machine/byte code and this is executed on the machine (or VM in the case of Java).\n\nInterpreters work on the *source code* and immediately interpret it and executes it using some internal mechanism. Since their working data is the source code itself, it is always available to them.\n\n========================================\n\nComments:\n- you know javascript ? Python ? Ruby ? Lua ?\n- In case of interpreted languages each line of code is interpreted and executed. While in case of compiled languages there are two stages, compilation and linking.Compilation converts code to object code. The object code is further linked by linker to produce an executable.So if you compare compiled and interpreted languages yes source code is always available to the interpreter because there are no stages involved, interpreter evaluates each line of code and executes it.\n- Source code interpreters perform their namesake: Interpret source code. In doing such, it should be obvious they need *the source code* they're interpreting and as the text implies, that source is available for reporting detailed error state. Compiled+Linked code needs no such source access once the final executable is created, so short of integrated debugging information and logging facilities, error states can be significantly more obtuse to identify with utmost clarity.\n- Seems like a perfectly real question to me...I see no reason to close it.\n- What's to see? The person writing 'Thinking in C++' isn't keen on interpreters, and has a slightly warped view view of them. Perl is an interpreted language. It reads and compiles its program at run-time and then executes what it compiled. It can load extra modules while it is running (it compiles the new modules on demand) if desired, but usually it isn't. The interpreter may not have the whole source loaded in memory; it just needs all the source available (it can read one file at time).\n- Re \"Interpreters work on the source code and immediately interpret it to the native code and execute it.\", most modern interpreters work on byte code generated from the source code. E.g. Python, JScript, Racket. Also, they don't usually interpret \"to the native code\". The interpreter is often not native code, and it's just directed by the code that it interprets, it does not translate that code to native code.\n- @Cheersandhth.-Alf: Regarding the first part, I believe the question is about theoretic aspect of interpreters - that work on source code. Regardingthe last part, you are right, let me fix it.\n- so, basically it is only a matter of \"speed\", nothing more? and why does it say that ***\"so the interpreter can be much more specific when an error occurs.\"***? Both have the source code, one has the raw other has the translated, how is it making the difference?\n- `speed` is one criteria to use interpreter. and yes, it can directly refer to source code when error occurs. but when run-time runs compiled code, it can't refer to exact line where error occured.\n- My answer still holds. also check the limitations of those interpreters where complete source code is brought into memory. And, i'm sure that here `complete source code` doesn't mean the whole project code but the required modules only.\n- Ah, well, the question wasn't about the limitations, it was about understanding the \"generic\" working of the interpreters. Also, `i'm sure that here`<- this doesn't help. Please provide the links from the standards to support your comment. No offense intended.\n- The generic working of interpreter is explained in 3 ways (see my answer). An interpreter can choose any one of them. `Wikipedia` should be a trustable source to confirm that. The line you quoted starts with `Most` i.e. not all interpreters that.\n- let us continue this discussion in chat\n- `The generic working of interpreter is explained in 3 ways (see my answer).` Which line there says that the interpreter needs all the source code in the memory at once? Third?\n- I don't think continuing in chat is needed here. We are discussing \"on topic\" stuff, not some offtopic nonsense. This discussion is related to the question and may help the future readers.\n- The point is you are asking same question again and again. and i'm just rephrasing my answer. you are talking about implementation detail while those 3 ways are about the design decision.\n- So, why don't you point which line (with links) in your answer talks about \"having the full source code in memory\" or vice versa? No need to rephrase your answer, simply point out the exact line and I'll read it.\n- None. that's the point. you can import full source code in memory by following any of the approach mentioned. but that's not mandatory. either bring all src at one go or bring as required.\n- let me give you can example and it may help you to understand more clearly. lets say you are using an undeclared variable `i` at two places. interpreter can't give errors for both the places at one time cause it will execute one line at a time. now it is varied from interpreter to interpreter whether other use of `i` is in memory or not. you can't make a generic decision about it.","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":55,"estimatedTokens":1556}}1058{"id":"stack-7785128","source":"stackoverflow","questionId":7785128,"title":"How do I get the log output of a Scala Compiler Plugin?","tags":["scala","plugins","compiler-construction"],"text":"Title: How do I get the log output of a Scala Compiler Plugin?\nTags: scala, plugins, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nFrom the scala compiler help, I would have thought that \"-Xprint:MyPhase\" would do just that, but despite the fact that the plugin does it's job, and that I know it makes log output, I still don't see any output from coming from scalac when it runs. All I see are \"[[syntax trees at end of MyPhase]] ...\" Since logging seems to simply involve calling log(\"...\"), I can't imagine what I would be doing wrong in the code that produces the log output. As an ugly work-around I can still do System.out.println(), but if there is a log() method, than I have to assume that it's actually meant to *do something* ...\n\n========================================\n\nCode:\n```text\n-Ylog:<phase>\n```\n\n========================================\n\nComments:\n- Thank you! I just looked at Borachio, and you seem to have found a way to generate Scala code in your compiler plugin. I had a question asking for that here: stackoverflow.com/questions/7625765/&hellip; to which Kevin Wright answered that it is not possible. Would you be so kind and tell us how it works?\n- Kevin is entirely correct - although he is working on a way to fix the problem, I believe. Borachio \"solves\" the problem by generating source code which is then compiled in a separate invocation of the compiler. I've hidden most of the complexity involved in this behind an sbt plugin.","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":19,"estimatedTokens":369}}1059{"id":"stack-7948940","source":"stackoverflow","questionId":7948940,"title":"Build compiler Object Oriented","tags":["oop","compiler-construction","yacc","lex"],"text":"Title: Build compiler Object Oriented\nTags: oop, compiler-construction, yacc, lex\nSource: Stack Overflow\n\nQuestion:\nI'm currently working on a project for my class. I'm building a compiler with **Flex (lex)** and **Bison (YACC)** and C. I have done just a little bit of semantic an syntax analysis but i have been thinking how im going to implement the object oriented part. That is, how can I handle classes, overloading, polymorphism and heritage. \n\nI can't seem to find something useful on google and the dragon book its just too low level. I mean too focused on building a compiler from scratch. So I was hoping that someone could point me to a good book, tutorials,example, something that can help me to clear my doubts.\n\nThanks in advance for the help, and im sorry if someone thinks this is asking to have my homework done.\n\n========================================\n\nCode:\n```text\ninstanceof\n```\n\n========================================\n\nComments:\n- Syntax is just a [nice] way to interact with the semantics of a language... this question is \"far too broad\". One \"simple\" way to do OO is to use a chained model like JavaScript. (OO does not require classes, chaining implicitly provides *a* form of overloading, and polymorphism can be obtained with duck-typing.)\n- @pst: That assumes you're willing to do dynamic typing. And even then you have to implement polymorphism *somehow*, and objects don't come for free in the first place.\n- @delnan Dynamic typing and weak typing are orthogonal. Consider Python and Objective-C. In any case, a [fixed] chained model is still applicable for statically-typed languages. There is no \"one OO\" approach.\n- @pst: Don't make me laugh. I'm well aware (and, like you, quick to point out) that weak typing != dynamic typing. But duck typing almost universally means dynamic typing (structural typing is a slightly different deal). The exception being C++-esque templates, but in that case the static type checking happens *after* the templates are instanciated, and the duck typing is limited to compiletime computations (i.e. you can't take that approach and try to type \"ordinary\" code with it).\n- @delnan Why did we ever go down this duck-typing path? Was it because I used \"JavaScript\" as a descriptor?\n- @pst: Your very first comment brought up duck typing: \"... and polymorphism can be obtained with duck-typing.\"\n- @delan Ah, my poor head melted. You are indeed correct. I started it :(","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":28,"estimatedTokens":610}}1060{"id":"stack-4113916","source":"stackoverflow","questionId":4113916,"title":"Using Mathematica to understand compiler optimization: accessing definitions created using SetDelayed?","tags":["compiler-construction","wolfram-mathematica"],"text":"Title: Using Mathematica to understand compiler optimization: accessing definitions created using SetDelayed?\nTags: compiler-construction, wolfram-mathematica\nSource: Stack Overflow\n\nQuestion:\nLately I have been interested in better understanding the optimizations done by compiler back-ends. I thought that using Mathematica to explore this might be helpful because it makes creating, displaying and manipulating syntax trees pretty easy. \n\nI thought I would start simple and look at constant propagation. So I wrote a simple function and tried to look at the syntax tree.\n\n```\nf2[x_, y_] := Module[{temp1},\n temp1 = 5;\n Return[(x + temp1)*y];\n ]\nFullForm[f2]\n```\n\nThe result of FullForm[f2], however, was just f2. I know that the right hand side of this expression has to be stored somewhere in Mathematica, so my question is where is it and is it possible to modify it after creating this rule using the SetDelayed operator \":=\"?\n\nIn the mean time I have discovered that I can use the Function symbol to achieve what I am aiming for, but I would still like to understand what Mathematica is doing a little better.\n\n```\nf1 = Function[{x, y}, Module[{temp1},\n temp1 = 5;\n Return[(x + temp1)*y]\n ]\n ]\nTreeForm[f1]\n```\n\n========================================\n\nCode:\n```text\nf2[x_, y_] := Module[{temp1},\n  temp1 = 5;\n  Return[(x + temp1)*y];\n  ]\nFullForm[f2]\n```\n\n```text\nf1 = Function[{x, y}, Module[{temp1},\n   temp1 = 5;\n   Return[(x + temp1)*y]\n   ]\n  ]\nTreeForm[f1]\n```\n\n```text\nIn[98]:= DownValues[f2]\nOut[98]= {HoldPattern[f2[x_, y_]] :> \n  Module[{temp1}, temp1 = 5; Return[(x + temp1) y];]}\n```\n\n```text\nIn[103]:= \nDownValues[\n  f2] = {HoldPattern[f2[x_, y_]] :> \n   Module[{temp1}, temp1 = 1234; Return[(x + temp1) y]]};\n\n\nIn[104]:= f2[1, 2]\nOut[104]= 2470\n```\n\n```text\nIn[107]:= g=7;\n\nIn[108]:= OwnValues[g]\nOut[108]= {HoldPattern[g]:>7}\n```\n\n```text\nSet\n```\n\n```text\nSetDelayed\n```\n\n```text\nDownValues\n```\n\n```text\nDownValues[f2]\n```\n\n```text\nOwnValues\n```\n\n========================================\n\nComments:\n- Very interesting question! Wish I had an answer for you m8, I'm gonna snoop here and see what the community has to say about it. +1 for nice Q","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":10,"totalLines":98,"estimatedTokens":542}}1061{"id":"stack-4401151","source":"stackoverflow","questionId":4401151,"title":"Writing Compilers, Lexical Analysis?","tags":["compiler-construction","linked-list","theory","analysis","lexical"],"text":"Title: Writing Compilers, Lexical Analysis?\nTags: compiler-construction, linked-list, theory, analysis, lexical\nSource: Stack Overflow\n\nQuestion:\nI'm completely new to writing compilers. So I am currently starting the project (coded in Java), and before coding, I would like to know more about the lexical analysis part. I have researched on the web, I found out that most of them use tokenizers.\n\nThe project requires that I do not use them (tokenizers), and instead use finite state automata. I am confused, do I need to implement it using linked lists? or a simple nested switch cases will do. I am not really that familiar with implementing finite automaton, what are the advantages?\n\n========================================\n\nTop Answer:\nI assume this is an assignemnt given the otherwise artificial ban on tokenizers.\n\nThere are a lot of google matches for \"lexical analysis using finite state automata\". What are these missing?\n\nAre you having problems seeing how finite state automata can be used for lexical analysis? Or writing an automaton yourself? It may help to know they're also known as finite state machines(FSM)\n\nA tokenizer may well use a FSM in its internals, so I don't understand why you say you must use an FSM but not a tokenizer - does this mean you can't use a tokenizer you've written and must write one yourself?\n\nThe implementation of a regular expression matcher is also normally a finite state machine, so that may be an area to think about.\n\nlex (and its more recent relation flex) are lexical analysers for which the source is available. You could look at those for ideas\n\n========================================\n\nComments:\n- The big compiler resources list is Learning to write a compiler. The various theory books/sites/etc therein will explain how to build a lexer with FSA (or you can probably suss out the solutions to straight forward task yourself). There are presumable multiple question on implementing FSA in Java.\n- It may be helpful to look at the code generated by some lexer generator. E.g. COCO/R for Java produces quite readable code.\n- @max taldykin, thanks for the pointer to COCO/R, it looks interesting. To the OP, scanner.java in the sources for that *is* a FSA scanner for the parser format itself, so might provide some inspiration.\n- actually, lex and flex are not lexical analyzers but lexical analyzer *generators*. so, it can be confusing to look at their source :)\n- True. I should have been more specific. The output of such tools might be also worth inspection","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":32,"estimatedTokens":631}}1062{"id":"stack-7382940","source":"stackoverflow","questionId":7382940,"title":"How to replace macros with a grammar-based parser?","tags":["parsing","compiler-construction","grammar","interpreter","yacc"],"text":"Title: How to replace macros with a grammar-based parser?\nTags: parsing, compiler-construction, grammar, interpreter, yacc\nSource: Stack Overflow\n\nQuestion:\nI need a parser for an exotic programming language. I wrote a grammar for it and used a parser generator (PEGjs) to generate the parser. That works perfectly... except for one thing: macros (that replace a placeholder with predefined text). I don't know how to integrate this into a grammar. Let me illustrate the problem:\n\nAn example program to be parsed typically looks like this:\n\n```\ninstructionA parameter1, parameter2\ninstructionB parameter1\ninstructionC parameter1, parameter2, parameter3\n```\n\nNo problem so far. But the language also supports macros:\n\n```\nDefine MacroX { foo, bar }\ninstructionD parameter1, MacroX, parameter4\n\nDefine MacroY(macroParameter1, macroParameter2) {\n instructionE parameter1, macroParameter1\n instructionF macroParameter2, MacroX\n}\n\ninstructionG parameter1, MacroX\nMacroY\n```\n\nOf course I could define a grammar to identify Macros and references to Macros. But in that case I don't know how I would parse the contents of a Macro, because it's not clear what the macro contains. It could be just one parameter (that's easiest), but it could also be several parameters in one macro (like MacroX in my example, which represents two parameters) or a whole block of instructions (like MacroY). And Macros can even contain other Macros. How do I put this into a grammar if it's not clear what the macro is semantically?\n\nThe easiest approach seems to be to run a preprocessor first to replace all the macros and only then run the parser. But in that case the line numbers get messed up. I want the parser to generate error messages containing the line number if there is a parse error. And if I preprocess the input, the line numbers do not correspond anymore.\n\nHelp very much appreciated.\n\n========================================\n\nTop Answer:\nWith PEG you have to manually define the places where you can check for macro extensions. You can add your macro to a hash and check for it in the PEG rule(s), which do allow macros (infix expr, postfix expr, unop, binop, function call, ...). It's not so easy as in lisp, but much easier than with YACC and its operator precedence hacks :)\n\nOther known PEG frameworks which allow macros, like parrot, perl6, katahdin or PFront use the trick to execute the parse at run-time, thus trading against performance.\nOr you can do both and allow pre-compiled and interpreted PEG parsing. There are several projects which thought about that, but you need a fast VM, like luajit, java, clr or friends.\n\nI use special syntax block keywords to load external shared libraries with the external pre-compiled PEG parser. E.g. to parse SQL or FFI declarations into your AST.\nBut you can also require a C compiler and compile the parse at run-time for all macros.\n\n========================================\n\nCode:\n```text\ninstructionA parameter1, parameter2\ninstructionB parameter1\ninstructionC parameter1, parameter2, parameter3\n```\n\n```text\nDefine MacroX { foo, bar }\ninstructionD parameter1, MacroX, parameter4\n\nDefine MacroY(macroParameter1, macroParameter2) {\n  instructionE parameter1, macroParameter1\n  instructionF macroParameter2, MacroX\n}\n\ninstructionG parameter1, MacroX\nMacroY\n```\n\n========================================\n\nComments:\n- Re: lines getting messed up: After reading in a macro definition, you can output an equivalent number of empty lines, or you can output something like `#line N`, and add a rule to your parser that `#line N` changes the current line number to `N`. (This is how GCC and most C preprocessors deal with `#include` directives.)","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":73,"estimatedTokens":921}}1063{"id":"stack-2779564","source":"stackoverflow","questionId":2779564,"title":"How is parsing phase in a compiler different from a rule engine?","tags":["parsing","compiler-construction","rule-engine"],"text":"Title: How is parsing phase in a compiler different from a rule engine?\nTags: parsing, compiler-construction, rule-engine\nSource: Stack Overflow\n\nQuestion:\nI have a rough understanding of how the compilers work (I mean languages, grammars, lexical analysis, parsing etc). The rule engines have various rules and associated action, just like you have rules in the grammars and you can associate actions with them in parser-generator tools like ANTLR. So I am a bit confused on how to differentiate between these two. Could anyone give a clearer, more formal explanation for the differences ?\n\nThanks,\nAbhinav.\n\n========================================\n\nComments:\n- It's unclear to me what you mean with \"rule engine\" in your question. Could you elaborate?\n- By rule engines I mean this : en.wikipedia.org/wiki/Business_rules_engine ... an example java-source.net/open-source/rule-engines\n- Thanks, Ira. That was a very comprehensive and clear answer, and it cleared my doubts :)","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":16,"estimatedTokens":245}}1064{"id":"stack-2612715","source":"stackoverflow","questionId":2612715,"title":"Compiler: Translation to assembly","tags":["compiler-construction","assembly"],"text":"Title: Compiler: Translation to assembly\nTags: compiler-construction, assembly\nSource: Stack Overflow\n\nQuestion:\nI've written an interpreter for my experimental language and know I want to move on and write a small compiler for it. It will probably take the source, go through the same steps as the interpreter (tokenizer, parser) and then translate the source to assembly.\n\nNow my questions:\n\nCan I expect that every command in my language can be 1:1 translated to a bunch of assembly instructions? What I mean is if I will have to completely throw over the whole input program or if it is just translated to assembly per line.\n\nWhich assembler should I use as output format?\n\n========================================\n\nTop Answer:\nUnless you are absolutely determined to \"reinvent the wheel\", you might as well just emit C code and then pass that to an existing C compiler, e.g. gcc. It will be much easier than writing your own back-end and you'll get all the C compiler's optimisations etc for free.\n\n========================================\n\nComments:\n- As to you second question: What's your target platform?\n- stackoverflow.com/questions/1669/learning-to-write-a-compile&zwnj;&#8203;r\n- Why, are you scared he will provide a decent system for Windows? Advising GCC is then a good way to ruin that :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":327}}1065{"id":"stack-52100795","source":"stackoverflow","questionId":52100795,"title":"What are Clojure Intrinsics","tags":["clojure","compiler-construction","clojure-java-interop"],"text":"Title: What are Clojure Intrinsics\nTags: clojure, compiler-construction, clojure-java-interop\nSource: Stack Overflow\n\nQuestion:\nBrowsing the Clojure source code I came across an `Intrinsics.java` file. It looks like it is a mapping of some clojure runtime functions to JVM opcodes.\n\nHowever, I am not sure where they get applied. The following code\n\n```\n(def ^:const pi 3.141592)\n(defn circumference [^double r] (* r 2.0 pi))\n```\n\ncompiles to\n\n```\npublic static java.lang.Object invokeStatic(double r);\n 0 dload_0 [r]\n 1 ldc2_w [14]\n 4 dmul\n 5 ldc2_w [16]\n 8 invokestatic clojure.lang.Numbers.multiply(double, double) : double [23]\n11 invokestatic java.lang.Double.valueOf(double) : java.lang.Double [29]\n14 areturn\n```\n\nand I see that `clojure.lang.Numbers.multiply(double, double) : double` did not get replaced to `DMUL`.\n\nHow exactly are intrinsics used? Thank you.\n\n========================================\n\nCode:\n```text\n(def ^:const pi 3.141592)\n(defn circumference [^double r] (* r 2.0 pi))\n```\n\n```text\npublic static java.lang.Object invokeStatic(double r);\n 0  dload_0 [r]\n 1  ldc2_w <Double 2.0> [14]\n 4  dmul\n 5  ldc2_w <Double 3.141592> [16]\n 8  invokestatic clojure.lang.Numbers.multiply(double, double) : double [23]\n11  invokestatic java.lang.Double.valueOf(double) : java.lang.Double [29]\n14  areturn\n```\n\n```text\nIntrinsics.java\n```\n\n```text\nclojure.lang.Numbers.multiply(double, double) : double\n```\n\n```text\nDMUL\n```\n\n```text\n(def ^:const pi 3.141592)\n(defn circumference ^double [^double r] (* r 2.0 pi))\n```\n\n```text\npublic static double invokeStatic(double r);\n   0  dload_0 [r]\n   1  ldc2_w <Double 2.0> [14]\n   4  dmul\n   5  ldc2_w <Double 3.141592> [16]\n   8  dmul\n   9  dreturn\n```\n\n```text\n(* r 2.0)\n```\n\n```text\ndmul\n```\n\n```text\ninvokeStatic\n```\n\n```text\n(* #<result of (* r 2.0)> 3.141592)\n```\n\n```text\nclojure.lang.Numbers.multiply(double, double) : double\n```\n\n```text\nr\n```\n\n```text\ndouble\n```\n\n========================================\n\nComments:\n- This may be a better question for the Clojure email list: groups.google.com/forum/#!forum/clojure","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":14,"totalLines":109,"estimatedTokens":520}}1066{"id":"stack-6391959","source":"stackoverflow","questionId":6391959,"title":"Expression types in a compiler written in Ocaml","tags":["compiler-construction","types","ocaml"],"text":"Title: Expression types in a compiler written in Ocaml\nTags: compiler-construction, types, ocaml\nSource: Stack Overflow\n\nQuestion:\nI am writing a small compiler in Ocaml. In `ast.mli`, i have defined 2 kinds of expressions\n\n```\ntype int_expr =\n | Integer_constant of int\n | Evar of string\n | Ebinop of binop * int_expr * int_expr\n | Ecell of int_expr * int_expr (* Sheet[ , ] *)\n\ntype bool_expr = \n | Bool_constant of bool\n | Bcmp of cmp * int_expr * int_expr\n | Band of bool_expr * bool_expr\n | Bor of bool_expr * bool_expr\n | Bnot of bool_expr\n```\n\nIn `interp.ml`, i want to define a function called `eval_expression` to evaluate any expression which could either be int_expr or bool_expr\n\n```\nlet rec eval_expression env = function\n | Integer_constant n -> Integer n\n | Ebinop (op, n, m) -> ...\n | Evar x -> ...\n | Ecell (r, c) -> ... \n | Bool_constant b -> ...\n | Bnot c -> ...\n | Bor (c1, c2) -> ...\n | Band (c1, c2) -> ...\n```\n\nBut it returns an error while compiling:\n\n```\nocamlc -c interp.ml\nFile \"interp.ml\", line 63, characters 4-19:\nError: This pattern matches values of type Ast.bool_expr\n but a pattern was expected which matches values of type Ast.int_expr\nmake: *** [interp.cmo] Error 2\n```\n\nCould anyone tell me how I could change the structure of my expression types so that `eval_expression` works? Thank you very much!\n\n========================================\n\nCode:\n```text\ntype int_expr =\n  | Integer_constant of int\n  | Evar of string\n  | Ebinop of binop * int_expr * int_expr\n  | Ecell of int_expr * int_expr (* Sheet[ , ] *)\n\ntype bool_expr = \n  | Bool_constant of bool\n  | Bcmp of cmp * int_expr * int_expr\n  | Band of bool_expr * bool_expr\n  | Bor of bool_expr * bool_expr\n  | Bnot of bool_expr\n```\n\n```text\nlet rec eval_expression env = function\n  | Integer_constant n -> Integer n\n  | Ebinop (op, n, m) -> ...\n  | Evar x -> ...\n  | Ecell (r, c) -> ... \n  | Bool_constant b -> ...\n  | Bnot c -> ...\n  | Bor (c1, c2) -> ...\n  | Band (c1, c2) -> ...\n```\n\n```text\nocamlc  -c interp.ml\nFile \"interp.ml\", line 63, characters 4-19:\nError: This pattern matches values of type Ast.bool_expr\n       but a pattern was expected which matches values of type Ast.int_expr\nmake: *** [interp.cmo] Error 2\n```\n\n```text\nast.mli\n```\n\n```text\ninterp.ml\n```\n\n```text\neval_expression\n```\n\n```text\neval_expression\n```\n\n```text\ntype expr = Iexpr of int_expr\n          | Bexpr of bool_expr\n```\n\n```text\ntype value = Ivalue of int\n           | Bvalue of bool\n```\n\n```text\nevaluate : expr -> value\n```\n\n```text\ntp\n```\n\n```text\nexpr\n```\n\n```text\nexpr -> tp -> value\n```\n\n========================================\n\nComments:\n- Can you show the exact line the failure is reported at and a few lines extra of surrounding context?\n- The exact line of failure (63) is `| Bool_constant b -> ...` in the `eval_expression` function in my post.","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":13,"totalLines":133,"estimatedTokens":709}}1067{"id":"stack-57420259","source":"stackoverflow","questionId":57420259,"title":"How to do type checking for a recursive function with no explicit return type?","tags":["function","recursion","compiler-construction","typechecking","function-call"],"text":"Title: How to do type checking for a recursive function with no explicit return type?\nTags: function, recursion, compiler-construction, typechecking, function-call\nSource: Stack Overflow\n\nQuestion:\nI am writing a language where functions are not typed. Which means I need to infer the return type of a function call in order to do type checking. However when somebody writes a recursive function the type checker goes into an infinite recursion trying to infer the type of the function call inside the function body.\n\nThe type checker does something like this:\n\n- Infer the types of the function call actual arguments.\n\n- Create a mapping of the actual argument types to the formal arguments.\n\n- Use the mapping to annotate types on the arguments used inside the function body.\n\n- Infer and return the return type of the function body.\n\nStep 4 tries to then infer the type of the function call inside the function body, which calls the same type checker function again, causing an infinite recursion.\n\nAn example of a recursive function that gives me this problem:\n\n```\nfunction factorial(n) = nHow can I solve this problem without going in to an infinite recursion loop? Is there a way to infer the type of the recursive function call without having to go into the body again? Or some clean way to infer the type from context?\n\nI know the easy solution might be to add types annotations to functions, this way the problem is trivial, but before doing that I want to know if there is a way to solve this without resorting to that.\n\nI'd also like for the solution to work for mutual recursion.\n\n========================================\n\nCode:\n```text\nfunction factorial(n) = n<1 ? 1 : n*factorial(n-1); // Function definition.\n...\nassert 24 == factorial(4); // Function call expression usage example.\n```\n\n```text\nlet empty_list = [] in\n(empty_list @ [3]), (empty_list @ [\"hello\"])\n```\n\n```text\nfunction list_of_two(x, y) = [x, y]\n```\n\n```text\nx\n```\n\n```text\ny\n```\n\n```text\nempty_list\n```\n\n```text\nempty_list\n```\n\n```text\nlet empty_list = [] in …\n```\n\n```text\nempty_list\n```\n\n```text\n…\n```\n\n```text\n…\n```\n\n========================================\n\nComments:\n- You have to assign some placeholder type for the function, and then fill it in when more information becomes available. Let's call the type of `factorial` `T`, and proceed. We know that `T` is a function. Next we see that the function takes an argument, so we update `T` to be \"function of one argument\". Then see that in the `n < 1` case, it returns `1`. So `T` must be \"function of one argument returning integer\". Now when we look at `n*factorial(n-1)`, we now know that `n` is being multiplied by an integer, and so on.\n- Note that while Stack Overflow tends to accept borderline questions about applied computer science like this one, they're more centrally on-topic on Computer Science and have a better chance of getting good answers there. Do not repost, but if you prefer your question to be on Computer Science, you can flag your question and ask a moderator to migrate it.\n- This post explains the Hindley-Milner type system very clearly and it even includes an example. It complements this answer very well. Great starting point for anyone who wants to implement the system for their own language.","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":11,"totalLines":84,"estimatedTokens":817}}1068{"id":"stack-1653284","source":"stackoverflow","questionId":1653284,"title":"C#'s compiler design - forward referencing","tags":["c#","compiler-construction","forward-reference"],"text":"Title: C#'s compiler design - forward referencing\nTags: c#, compiler-construction, forward-reference\nSource: Stack Overflow\n\nQuestion:\nIn forward referencing language such as c#, how does the compiler handle this? What are the steps in which the compiler operate?\n\n========================================\n\nTop Answer:\nIt does this by doing two passes of compilation. The first pass parses the code and collects all identifiers used. The second pass resolves all identifiers.\n\nIn a language with a single pass compiler, like Pascal, only backwards references can be used as the type of an identifier have to be known before it can be resolved.\n\n========================================\n\nComments:\n- C also uses forward reference: nacs.uci.edu/dcslib/digital_unix/digital-v40d/AQTLTBTE/&hellip;\n- You may want to rephrase your question, since forward reference and forward declaration is found in C++, so your question may need to be more general than you expected.","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":19,"estimatedTokens":241}}1069{"id":"stack-39030481","source":"stackoverflow","questionId":39030481,"title":"How Does Assembly Code Generation Work?","tags":["compilation","compiler-construction","code-generation"],"text":"Title: How Does Assembly Code Generation Work?\nTags: compilation, compiler-construction, code-generation\nSource: Stack Overflow\n\nQuestion:\nI've been studying compiler design a lot recently. I've managed to get a strong grasp of the parsing stage, but am having a bit of trouble understanding how code generation works.\n\nFrom what I've read, there seems to be 3 major steps in the code generation phase:\n\n- Instruction Selection (Greedy Tiling)\n\n- Instruction Scheduling\n\n- Register Allocation\n\nNow, instruction scheduling is a little beyond what I'm trying to do at the moment, and I think with a bit more studying and prototyping, I can probably wrap my mind around the graph coloring algorithm for register allocation.\n\nWhat stumps me is the first step, instruction selection. From what I've read about it, each instruction in a target machine language is represented by a tile; and the goal is to find the instructions that match the largest parts of the tree (hence the nickname, greedy tiling).\n\nThe thing I'm confused about is, how do you select instructions when they don't actually correspond 1:1 with the syntax tree?\n\nTake for example, accumulator-based architectures like the Z80 or MIPs single instruction architecture. Performing even 16-bit integer arithmetic on a Z80 may require the use of the accumulator or shadow registers.\n\nThere are also some instructions that can only be used on certain registers despite them being general purpose.\n\nWould I be right in assuming the following?\n\na) A tile may consist of a sequence of instructions that match a syntax tree pattern, rather than just a 1:1 match.\n\nb) The code generator generates code for a stack-based architecture (or an architecture with infinite temporary registers) first and expands and substitutes instructions as necessary somehow during the register allocation phase.\n\n========================================\n\nCode:\n```text\nmov %x, %y\nadd %x, %z\n```\n\n```text\n%x <- %y + %z\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.946Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":42,"estimatedTokens":490}}1070{"id":"stack-3507555","source":"stackoverflow","questionId":3507555,"title":"How to implement floor, ceil and round in LLVM bitcode?","tags":["math","compiler-construction","llvm","primitive"],"text":"Title: How to implement floor, ceil and round in LLVM bitcode?\nTags: math, compiler-construction, llvm, primitive\nSource: Stack Overflow\n\nQuestion:\nI'm writing the maths functions for a small LLVM-based programming language,\nand I'm currently stumped by how to implement the common rounding functions floor, ceil and round (to even). Firstly because I haven't found any algorithm descriptions for these functions, secondly because I'm not familiar with what capabilities LLVM has w. rounding.\n\nBeing able to round negative numbers correctly is a must, rounding to a specific precision is not. Rounding to an integral value will do. Simply being pointed to any existing implementations that can be used from LLVM bitcode will also work.\n\n========================================\n\nTop Answer:\nIf you look on Google Code Search, there are a few results. The linked example assumes IEEE floating point numbers. Ordinarily, compilers for common PCs just compile `floor` to floating point instructions. For example the original 387 arithmetic processor has the instruction `FPREM` which more or less does a piece of what you need for `floor`.\n\n========================================\n\nCode:\n```text\ndefine float @trunc(float %x) {\n    %rounded = fptosi float %x to i32\n    %asFloat = sitofp i32 %rounded to float\n    ret float %asFloat\n}\n```\n\n```text\ntrunc( )\n```\n\n```text\nfptosi ... to ...\n```\n\n```text\nsitofp ... to ...\n```\n\n```text\nfptosi ... to ...\n```\n\n```text\nx\n```\n\n```text\ni64\n```\n\n```text\nfloor\n```\n\n```text\nround\n```\n\n```text\ntrunc\n```\n\n```text\nx - trunc(x)\n```\n\n```text\nfloor\n```\n\n```text\nFPREM\n```\n\n```text\nfloor\n```\n\n========================================\n\nComments:\n- Not really an answer, otherwise I'd make it so. But just anecdotal information. I have found that when I'm in such a position, I will write the C code to reproduce that function, and then compile it with clang or llvm-gcc and look at the resulting llvm ir. This usually gives me a good enough idea as to how I should proceed.\n- This might be slightly offtopic, but i think it's still valuable to know and consider when implementing your own math library: coming up with an algorithm for those functions that works correctly with the quirks of floating point math is not as trivial as it might seem first, as can be seen e.g. here.\n- Calling out to my local C library is a good idea. However according to the LLVM lang. ref. fptosi rounds towards 0, not towards nearest.\n- @voxcogitatio: excellent point; shows how carefully I skimmed the reference as I was writing that snippet. I'll correct it later today.","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":14,"totalLines":85,"estimatedTokens":647}}1071{"id":"stack-21928286","source":"stackoverflow","questionId":21928286,"title":"What's the advantage of compiler instruction scheduling compared to dynamic scheduling?","tags":["compiler-construction","cpu","compiler-optimization","cpu-architecture","vliw"],"text":"Title: What's the advantage of compiler instruction scheduling compared to dynamic scheduling?\nTags: compiler-construction, cpu, compiler-optimization, cpu-architecture, vliw\nSource: Stack Overflow\n\nQuestion:\nNowadays, super-scalar RISC cpus usually support out-of-order execution, with branch prediction and speculative execution. They schedule work dynamically.\n\nWhat's the advantage of compiler instruction scheduling, compared to an out-of-order CPU's dynamic scheduling? Does compile-time static scheduling matter at all for an out-of-order CPU, or only for simple in-order CPUs?\n\nIt seems currently most software instruction scheduling work focuses on VLIW or simple CPUs. The GCC wiki's scheduling page also shows not much interest in updating gcc's scheduling algorithms.\n\n========================================\n\nTop Answer:\nFirst, I should note that current RISC architectures first **compile** then do rescheduling, cause \"high level\" assembly commands are compiled into smaller RISC commands. At least it is true for x86/x64 architectures.\n\nThen we can imagine an execution cycle as:\ncompile - optimize/reschedule - descrease scale - compile - optimize/reschedule.\n\nThat sort of answers the question, compiler has much wider scope of visibility into the application, so it mainly optimizes on macro-level (blocks of application commands), while processor mainly optimizes for micro-level (blocks of RISC commands).\n\n========================================\n\nComments:\n- This question appears to be off-topic because it is about software and hardware design. Maybe cs would be the right place?\n- Somewhat related to \"Not time bound\", compile-time scheduling is done once while dynamic scheduling work (even if cached in something like a trace cache) tends to be repeated. Static scheduling is also connected to other optimizations which can reduce work. Dynamic scheduling also costs power and area; if the cost of energy use in the compiling system is lower than in the executing system, compile-time optimization could be preferred even if the total energy used is greater. Dynamic scheduling also fits well with speculation which is more expensive statically in a RISC.\n- AFAIK, only x86 needs to decode native ISA instructions into potentially multiple different uops. RISC CPUs design their instruction set so that instructions don't need to be dynamically split up. (IDK if they usually avoid even having microcode at all to handle context-switch instructions or other rare system-management stuff that's allowed to be slow). Anyway, in a PowerPC or something, I think the machine instructions translate directly to what the out-of-order machinery tracks internally.\n- Anyway, an OOO CPU's out-of-order window (the ROB size) is maybe within an order of magnitude of 100 instructions or uops, depending on transistor and power budgets. Compilers can and do schedule and generate RISC instructions directly, for RISC ISAs, so there's nothing stopping the compiler from being able to schedule for a specific pipeline. This idea that the compiler can only schedule at a high level is nonsense. Saying that they don't schedule individual instructions doesn't answer the question. It's asking why not.","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":28,"estimatedTokens":804}}1072{"id":"stack-2143552","source":"stackoverflow","questionId":2143552,"title":"Recommend C front-end that preserves preprocessor directives","tags":["c","compiler-construction","c-preprocessor","abstract-syntax-tree"],"text":"Title: Recommend C front-end that preserves preprocessor directives\nTags: c, compiler-construction, c-preprocessor, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI'd like to start a project that involves transforming **C** code, but I'd like to *include* the preprocessor directives. I don't want to reinvent the wheel by writing my own **C** parser, so does anyone know of a front-end that can parse **C** preprocessor and **C** code, and produce an AST that can be used to re-generate (or pretty-print) the original source?\n\ne.g.,:\n\n```\n#define FILENAME \"filename\"\n#include \n\nFILE *f=0;\n...\nif (file_is_open) {\n#ifdef CAN_OPEN_IT\n f = fopen(FILENAME, \"r\");\n#else\n printf(\"Unable to open file.\\n\");\n#endif\n}\n```\n\nThe above code should be parsed into some in-memory representation that can be used to re-generate the source. In other words, it should not be processed as normal **C** in two phases, first processing the PP directives and then parsing pure **C** code. Rather it should represent the whole compile-time logic including the preprocessor variables.\n\n========================================\n\nTop Answer:\nTake the GNU gcc compiler, the flags required to pre-process the source is `gcc -E mysource.c`, see here for further information. As for pretty printing it, there's indent and this explains the usage here, this is a bit old, but nonetheless worthy of mention. There is also cflow that can produce a map of the source.\n\nSorry if I misunderstood what you're looking for...\n\n========================================\n\nCode:\n```text\n#define FILENAME \"filename\"\n#include <stdio.h>\n\nFILE *f=0;\n...\nif (file_is_open) {\n#ifdef CAN_OPEN_IT\n    f = fopen(FILENAME, \"r\");\n#else\n    printf(\"Unable to open file.\\n\");\n#endif\n}\n```\n\n```text\ngcc -E mysource.c\n```\n\n```text\n#define FILENAME \"filename\"\n#include <stdio.h>\n\nFILE *f;\nmain() {\n  f=0;\nif (file_is_open) {\n#ifdef CAN_OPEN_IT\nf = fopen(FILENAME, \"r\");\n#else\nprintf(\"Unable to open file.\\n\");\n#endif\n}\n\n}\n```\n\n```text\nC~GCC4 Domain Parser Version 3.0.1(28449)\nCopyright (C) 1996-2013 Semantic Designs, Inc; All Rights Reserved; SD Confidential\nPowered by DMS (R) Software Reengineering Toolkit\nAST Optimizations: remove constant tokens, remove unary productions, compact sequences\nUsing encoding Unicode-UTF-8?ANSI +CRLF +1 /^I\n(translation_unit@C~GCC4=2#4a7e0e0^0 Line 1 Column 1 File C:/temp/test.c\n (declaration_seq@C~GCC4=605#4a77580^1#4a7e0e0:1 {4} Line 1 Column 1 File C:/temp/test.c\n  (control_line@C~GCC4=1094#4a775c0^1#4a77580:1 Line 1 Column 1 File C:/temp/test.c\n   ('#'@C~GCC4=1548#4a771c0^1#4a775c0:1[Keyword:0] Line 1 Column 1 File C:/temp/test.c)'#'\n   (IDENTIFIER@C~GCC4=1531#4a77200^1#4a775c0:2[`FILENAME'] Line 1 Column 9 File C:/temp/test.c)IDENTIFIER\n   (<!MacroDefinition>@C~GCC4=1603#4a77180^2#4a775c0:3#4a7f300:1[`FILENAME'] Line 1 Column 18 File C:/temp/test.c\n$VOID$ [Child 1]\n   |(STRING_LITERAL@C~GCC4=1525#4a77160^2#4a77180:2#4a7f300:2[`filename'] Line 1 Column 18 File C:/temp/test.c)STRING_LITERAL\n$VOID$ [Child 3]\n   )<!MacroDefinition>#4a77180\n   (new_line@C~GCC4=1578#4a77260^1#4a775c0:4[Keyword:0] Line 1 Column 28 File C:/temp/test.c)new_line\n  )control_line#4a775c0\n  (control_line@C~GCC4=1104#4a77460^1#4a77580:2 Line 2 Column 1 File C:/temp/test.c\n   ('#'@C~GCC4=1548#4a77340^1#4a77460:1[Keyword:0] Line 2 Column 1 File C:/temp/test.c)'#'\n   (ANGLED_HEADER_NAME@C~GCC4=1589#4a77380^1#4a77460:2[`stdio.h'] Line 2 Column 10 File C:/temp/test.c)ANGLED_HEADER_NAME\n   (new_line@C~GCC4=1578#4a773c0^1#4a77460:3[Keyword:0] Line 2 Column 19 File C:/temp/test.c)new_line\n  )control_line#4a77460\n  (simple_declaration@C~GCC4=631#4a774c0^1#4a77580:3 Line 4 Column 1 File C:/temp/test.c\n   (IDENTIFIER@C~GCC4=1531#4a77360^1#4a774c0:1[`FILE'] Line 4 Column 1 File C:/temp/test.c)IDENTIFIER\n   (declarator@C~GCC4=850#4a77520^1#4a774c0:2 Line 4 Column 6 File C:/temp/test.c\n   |(ptr_operator@C~GCC4=866#4a77560^1#4a77520:1 Line 4 Column 6 File C:/temp/test.c)ptr_operator\n   |(IDENTIFIER@C~GCC4=1531#4a77480^1#4a77520:2[`f'] Line 4 Column 7 File C:/temp/test.c)IDENTIFIER\n   )declarator#4a77520\n  )simple_declaration#4a774c0\n  (function_definition@C~GCC4=966#4a77be0^1#4a77580:4 Line 5 Column 1 File C:/temp/test.c\n   (direct_declarator@C~GCC4=852#4a77440^1#4a77be0:1 Line 5 Column 1 File C:/temp/test.c\n   |(IDENTIFIER@C~GCC4=1531#4a774e0^1#4a77440:1[`main'] Line 5 Column 1 File C:/temp/test.c)IDENTIFIER\n   |(parameter_declaration_clause@C~GCC4=900#4a77220^1#4a77440:2 Line 5 Column 6 File C:/temp/test.c)parameter_declaration_clause\n   )direct_declarator#4a77440\n   (compound_statement@C~GCC4=507#4a77b20^1#4a77be0:2 Line 5 Column 8 File C:/temp/test.c\n   |(statement_seq@C~GCC4=511#4a77d20^1#4a77b20:1 {2} Line 6 Column 3 File C:/temp/test.c\n   | (AMBIGUITY<statement=358>@C~GCC4=1602#4a77680^1#4a77d20:1{2} Line 6 Column 3 File C:/temp/test.c\n   |  (expression_statement@C~GCC4=503#4a7e040^1#4a77680:1 Line 6 Column 3 File C:/temp/test.c\n   |   (assignment_expression@C~GCC4=457#4a77f00^1#4a7e040:1 Line 6 Column 3 File C:/temp/test.c\n   |   |(assignment_target@C~GCC4=470#4a77a00^1#4a77f00:1 Line 6 Column 3 File C:/temp/test.c\n   |   | (IDENTIFIER@C~GCC4=1531#4a77400^2#4a77a00:1#4a77fc0:1[`f'] Line 6 Column 3 File C:/temp/test.c)IDENTIFIER\n   |   |)assignment_target#4a77a00\n   |   |(INT_LITERAL@C~GCC4=1471#4a77a60^2#4a77f00:2#4a77f60:1[0] Line 6 Column 5 File C:/temp/test.c)INT_LITERAL\n   |   )assignment_expression#4a77f00\n   |  )expression_statement#4a7e040\n   |  (simple_declaration@C~GCC4=630#4a7e060^1#4a77680:2 Line 6 Column 3 File C:/temp/test.c\n   |   (init_declarator@C~GCC4=835#4a77fc0^1#4a7e060:1 Line 6 Column 3 File C:/temp/test.c\n   |   |(IDENTIFIER@C~GCC4=1531#4a77400^2... [ALREADY PRINTED] ...)\n   |   |(initializer@C~GCC4=983#4a77f60^1#4a77fc0:2 Line 6 Column 4 File C:/temp/test.c\n   |   | (INT_LITERAL@C~GCC4=1471#4a77a60^2... [ALREADY PRINTED] ...)\n   |   |)initializer#4a77f60\n   |   )init_declarator#4a77fc0\n   |  )simple_declaration#4a7e060\n   | )AMBIGUITY#4a77680\n   | (selection_statement@C~GCC4=527#4a77b40^1#4a77d20:2 Line 7 Column 1 File C:/temp/test.c\n   |  (IDENTIFIER@C~GCC4=1531#4a7e0c0^1#4a77b40:1[`file_is_open'] Line 7 Column 5 File C:/temp/test.c)IDENTIFIER\n   |  (compound_statement@C~GCC4=507#4a77ae0^1#4a77b40:2 Line 7 Column 19 File C:/temp/test.c\n   |   (statement@C~GCC4=490#4a7f840^1#4a77ae0:1 Line 8 Column 1 File C:/temp/test.c\n   |   |(if_directive@C~GCC4=1088#4a7f1c0^1#4a7f840:1 Line 8 Column 1 File C:/temp/test.c\n   |   | ('#'@C~GCC4=1548#4a7f240^1#4a7f1c0:1[Keyword:0] Line 8 Column 1 File C:/temp/test.c)'#'\n   |   | (IDENTIFIER@C~GCC4=1531#4a7ee60^1#4a7f1c0:2[`CAN_OPEN_IT'] Line 8 Column 8 File C:/temp/test.c)IDENTIFIER\n   |   | (new_line@C~GCC4=1578#4a7f1e0^1#4a7f1c0:3[Keyword:0] Line 8 Column 19 File C:/temp/test.c)new_line\n   |   |)if_directive#4a7f1c0\n   |   |(AMBIGUITY<statement=358>@C~GCC4=1602#4a77d40^1#4a7f840:2{2} Line 9 Column 5 File C:/temp/test.c\n   |   | (expression_statement@C~GCC4=503#4a7f4a0^1#4a77d40:1 Line 9 Column 5 File C:/temp/test.c\n   |   |  (assignment_expression@C~GCC4=457#4a7f3c0^1#4a7f4a0:1 Line 9 Column 5 File C:/temp/test.c\n   |   |   (assignment_target@C~GCC4=470#4a7eec0^1#4a7f3c0:1 Line 9 Column 5 File C:/temp/test.c\n   |   |   |(IDENTIFIER@C~GCC4=1531#4a7eee0^2#4a7eec0:1#4a7f400:1[`f'] Line 9 Column 5 File C:/temp/test.c)IDENTIFIER\n   |   |   )assignment_target#4a7eec0\n   |   |   (postfix_expression@C~GCC4=201#4a7f2e0^1#4a7f3c0:2 Line 9 Column 9 File C:/temp/test.c\n   |   |   |(IDENTIFIER@C~GCC4=1531#4a7f120^2#4a7f2e0:1#4a7f160:1[`fopen'] Line 9 Column 9 File C:/temp/test.c)IDENTIFIER\n   |   |   |(expression_list@C~GCC4=228#4a7f260^2#4a7f2e0:2#4a7f160:2 Line 9 Column 15 File C:/temp/test.c\n   |   |   | (<!MacroCall>@C~GCC4=1607#4a7f300^1#4a7f260:1[`FILENAME'] Line 9 Column 15 File C:/temp/test.c\n   |   |   |  (<!MacroDefinition>@C~GCC4=1603#4a77180^2... [ALREADY PRINTED] ...)\n   |   |   |  (STRING_LITERAL@C~GCC4=1525#4a77160^2... [ALREADY PRINTED] ...)\n   |   |   |  $VOID$ [Child 3]\n   |   |   |  (STRING_LITERAL@C~GCC4=1525#4a7f2c0^1#4a7f300:4[`filename'] Line 1 Column 18 File C:/temp/test.c)STRING_LITERAL\n   |   |   |  $VOID$ [Child 5]\n   |   |   | )<!MacroCall>#4a7f300\n   |   |   | (STRING_LITERAL@C~GCC4=1525#4a7f140^1#4a7f260:2[`r'] Line 9 Column 25 File C:/temp/test.c)STRING_LITERAL\n   |   |   |)expression_list#4a7f260\n   |   |   )postfix_expression#4a7f2e0\n   |   |  )assignment_expression#4a7f3c0\n   |   | )expression_statement#4a7f4a0\n   |   | (simple_declaration@C~GCC4=630#4a7f480^1#4a77d40:2 Line 9 Column 5 File C:/temp/test.c\n   |   |  (init_declarator@C~GCC4=835#4a7f400^1#4a7f480:1 Line 9 Column 5 File C:/temp/test.c\n   |   |   (IDENTIFIER@C~GCC4=1531#4a7eee0^2... [ALREADY PRINTED] ...)\n   |   |   (initializer@C~GCC4=983#4a7f3e0^1#4a7f400:2 Line 9 Column 7 File C:/temp/test.c\n   |   |   |(postfix_expression@C~GCC4=201#4a7f160^1#4a7f3e0:1 Line 9 Column 9 File C:/temp/test.c\n   |   |   | (IDENTIFIER@C~GCC4=1531#4a7f120^2... [ALREADY PRINTED] ...)\n   |   |   | (expression_list@C~GCC4=228#4a7f260^2... [ALREADY PRINTED] ...)\n   |   |   |)postfix_expression#4a7f160\n   |   |   )initializer#4a7f3e0\n   |   |  )init_declarator#4a7f400\n   |   | )simple_declaration#4a7f480\n   |   |)AMBIGUITY#4a77d40\n   |   |(else_directive@C~GCC4=1091#4a7f4c0^1#4a7f840:3 Line 10 Column 1 File C:/temp/test.c\n   |   | ('#'@C~GCC4=1548#4a7f500^1#4a7f4c0:1[Keyword:0] Line 10 Column 1 File C:/temp/test.c)'#'\n   |   | (new_line@C~GCC4=1578#4a7f4e0^1#4a7f4c0:2[Keyword:0] Line 10 Column 6 File C:/temp/test.c)new_line\n   |   |)else_directive#4a7f4c0\n   |   |(expression_statement@C~GCC4=503#4a7f7c0^1#4a7f840:4 Line 11 Column 5 File C:/temp/test.c\n   |   | (postfix_expression@C~GCC4=201#4a77ba0^1#4a7f7c0:1 Line 11 Column 5 File C:/temp/test.c\n   |   |  (IDENTIFIER@C~GCC4=1531#4a7f640^1#4a77ba0:1[`printf'] Line 11 Column 5 File C:/temp/test.c)IDENTIFIER\n   |   |  (STRING_LITERAL@C~GCC4=1525#4a77c20^1#4a77ba0:2[`Unable to open file.\n'] Line 11 Column 12 File C:/temp/test.c)STRING_LITERAL\n   |   | )postfix_expression#4a77ba0\n   |   |)expression_statement#4a7f7c0\n   |   |(endif_directive@C~GCC4=1092#4a7f7e0^1#4a7f840:5 Line 12 Column 1 File C:/temp/test.c\n   |   | ('#'@C~GCC4=1548#4a7f720^1#4a7f7e0:1[Keyword:0] Line 12 Column 1 File C:/temp/test.c)'#'\n   |   | (new_line@C~GCC4=1578#4a7f700^1#4a7f7e0:2[Keyword:0] Line 12 Column 7 File C:/temp/test.c)new_line\n   |   |)endif_directive#4a7f7e0\n   |   )statement#4a7f840\n   |  )compound_statement#4a77ae0\n   | )selection_statement#4a77b40\n   |)statement_seq#4a77d20\n   )compound_statement#4a77b20\n  )function_definition#4a77be0\n )declaration_seq#4a77580\n)translation_unit#4a7e0e0\n```\n\n```text\nC:\\DMS\\Domains\\C\\GCC4\\Tools\\PrettyPrinter>run domainprettyprinter \\temp\\test.c\nC~GCC4 PrettyPrinter Version 1.2.13\nCopyright (C) 2004-2013 Semantic Designs, Inc; All Rights Reserved; SD Confidential\nPowered by DMS (R) Software Reengineering Toolkit\n\n#define FILENAME \"filename\"\n#include <stdio.h>\nFILE *f;\n\nmain()\n{\n  f = 0;\n  if (file_is_open)\n    {\n      #ifdef CAN_OPEN_IT\n        f = fopen(FILENAME, \"r\");\n      #else\n        printf(\"Unable to open file.\\n\");\n      #endif\n    }\n}\n```\n\n========================================\n\nComments:\n- Why the downvote? I mentioned indent and cflow...but the question is exactly not clear as to why the AST is needed when the context of the question included 'pretty print'. It would be nice for a downvote to leave a comment explaining why instead of ignoring it which is against the spirit of SO.\n- Downvotes happen; they're a nuisance. Usually, they don't do irreparable damage to your reputation.\n- @Jonathan: Quick question, earlier I had 3 upvotes for stackoverflow.com/questions/2142796/&hellip; this, but is showing up as 5, instead of 30 why?\n- Sorry if it wasn't clear, I'm looking for something that parses C and preprocessor code, not necessarily a pretty printer, but the reason I mentioned this is that a pretty printer probably parses the CPP code. What I want is something that will generate an AST that includes the CPP logic. I don't care about pretty printing per se.\n- @Steve: Ok, the best answer I can give is to look at Antlr's grammar for parsing here... antlr.org/grammar/list...using Antlr you can generate an AST and has multiple language interface, ie C#, C, CPP, Java can use the Antlr libraries for parsing, if that's what you are looking for... :)\n- @tommieb75: regarding your '5 instead of 30'; I'd guess you reached your 200 limit for the day - after which you get a Mortarboard badge and no more points.\n- If \"thanks\" has been in your answer for 5 years, you're probably better off just leaving it there than editing it out.\n- I don't believe Clang captures preprocessor directives in its ASTs.\n- This seems to be about (ANTLR) parser generators that produce parsers implemented in C. The OP wants something that *parses* C. Did I miss something?","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":231,"estimatedTokens":3223}}1073{"id":"stack-23408984","source":"stackoverflow","questionId":23408984,"title":"Emit the llvm-ir bytecode from my llvm pass","tags":["c++","c","compiler-construction","llvm","llvm-ir"],"text":"Title: Emit the llvm-ir bytecode from my llvm pass\nTags: c++, c, compiler-construction, llvm, llvm-ir\nSource: Stack Overflow\n\nQuestion:\nIs there anyway to emit the llvm-ir bytecode from inside my pass, into a file? I want the same format that I am getting by using the opt tool, so I can pass this file to the opt later.\n\n========================================\n\nCode:\n```text\nopt\n```\n\n```text\nModule::dump()\n```\n\n```text\nModule::print\n```\n\n```text\nllvm::WriteBitcodeToFile\n```\n\n========================================\n\nComments:\n- I believe the LLVM development mail list is a more appropriate place for this question.\n- stackoverflow.com/questions/17904440/&hellip; or clang.llvm.org/hacking.html#irgen\n- So I guess I will do something like : std::string errorMessage = \"\"; raw_fd_ostream file(\"file.txt\", errorMessage); module->print(file, NULL); file.close();","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":32,"estimatedTokens":217}}1074{"id":"stack-28825807","source":"stackoverflow","questionId":28825807,"title":"How to manually manipulate precedence of special expressions in Parsec?","tags":["haskell","compiler-construction","interpreter","parsec","lambda-calculus"],"text":"Title: How to manually manipulate precedence of special expressions in Parsec?\nTags: haskell, compiler-construction, interpreter, parsec, lambda-calculus\nSource: Stack Overflow\n\nQuestion:\nI tried to write a parser for a lambda-calculus interpreter that uses the expression closures grammars of JavaScript 1.8, which means `function(x) x * x` same with `function(x) { return x * x; }`. \n\nHere is my parser code.\n\n```\nmodule Parser where\n\nimport Text.Parsec\nimport Text.Parsec.String\nimport qualified Text.Parsec.Token as P\nimport qualified Text.ParserCombinators.Parsec.Token as T\nimport Text.Parsec.Language (emptyDef)\nimport Text.Parsec.Expr\nimport Control.Applicative ((), (), (*>), ( Either ParseError [Term]\njsparse = parse prog \"\"\n\n-- The scanner.\nlexer = P.makeTokenParser emptyDef {\n T.commentStart = \"/*\",\n T.commentEnd = \"*/\",\n T.commentLine = \"//\",\n T.nestedComments = True,\n T.identStart = letter char '_' char '$',\n T.identLetter = alphaNum,\n T.reservedNames = [\"function\"],\n T.reservedOpNames = [\"=\"],\n T.caseSensitive = True\n}\n\nparens = P.parens lexer\nreserved = P.reserved lexer\nidentifier = P.identifier lexer\nwhiteSpace = P.whiteSpace lexer\nsemi = P.semi lexer\n\n-- The parser\nprog :: Parser [Term]\nprog = expr `endBy` semi\n\nterm :: Parser Term\nterm = termE expr\n\ntermE :: Parser Term -> Parser Term\ntermE e = try (parens e) try var func\n\nexpr :: Parser Term\nexpr = do whiteSpace\n e return e\n\nvar :: Parser Term\nvar = do whiteSpace\n v However, there is a trouble that `function(x) x(x)` should be parsed to `(function(x) (x(x)))`, but my parser gets `(function(x) x) (x)`.\n\n========================================\n\nCode:\n```text\nmodule Parser where\n\nimport Text.Parsec\nimport Text.Parsec.String\nimport qualified Text.Parsec.Token as P\nimport qualified Text.ParserCombinators.Parsec.Token as T\nimport Text.Parsec.Language (emptyDef)\nimport Text.Parsec.Expr\nimport Control.Applicative ((<*>), (<$>), (*>), (<*), pure, (<$))\nimport Control.Monad\n\nimport Ast\n\njsparse :: String -> Either ParseError [Term]\njsparse  = parse prog \"\"\n\n-- The scanner.\nlexer = P.makeTokenParser emptyDef {\n  T.commentStart = \"/*\",\n  T.commentEnd   = \"*/\",\n  T.commentLine  = \"//\",\n  T.nestedComments = True,\n  T.identStart = letter <|> char '_' <|> char '$',\n  T.identLetter     = alphaNum,\n  T.reservedNames   = [\"function\"],\n  T.reservedOpNames = [\"=\"],\n  T.caseSensitive   = True\n}\n\nparens = P.parens lexer\nreserved = P.reserved lexer\nidentifier = P.identifier lexer\nwhiteSpace = P.whiteSpace lexer\nsemi = P.semi lexer\n\n-- The parser\nprog :: Parser [Term]\nprog = expr `endBy` semi\n\nterm :: Parser Term\nterm = termE expr\n\ntermE :: Parser Term -> Parser Term\ntermE e = try (parens e) <|> try var <|> func\n\nexpr :: Parser Term\nexpr = do whiteSpace\n          e <- term\n          maybeAddSuffix e\n  where addSuffix e0 = do e1 <- term\n                          maybeAddSuffix $ TermApp e0 e1\n        maybeAddSuffix e = addSuffix e\n                           <|> return e\n\nvar :: Parser Term\nvar = do whiteSpace\n         v <- identifier\n         return $ TermVar v\n\nfunc :: Parser Term\nfunc  = do whiteSpace\n           reserved \"function\"\n           v  <- parens identifier\n           body <- term\n           return $ TermAbs v body\n```\n\n```text\nfunction(x) x * x\n```\n\n```text\nfunction(x) { return x * x; }\n```\n\n```text\nfunction(x) x(x)\n```\n\n```text\n(function(x) (x(x)))\n```\n\n```text\n(function(x) x) (x)\n```\n\n```text\nfunc\n```\n\n```text\nbody <- term\n```\n\n```text\nbody <- expr\n```\n\n========================================\n\nComments:\n- Thank you so much and it is solved. I learned this code framework from github.com/JakeWheat/intro_to_parsing , but I do not understand the `term` and `termE`. Why do I need the `termE e` just pass `expr` as the argument always? How can I correspond it to BNF?\n- @fiigii You don't really need `termE` in such a simple case, I would assume the tutorial uses it for more than one purpose? The rest is too big to answer in a comment, maybe you should post a new question?\n- I have posted a question about it. stackoverflow.com/questions/28828586/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":9,"totalLines":167,"estimatedTokens":1018}}1075{"id":"stack-19330026","source":"stackoverflow","questionId":19330026,"title":"Is there an overview of the nsc compiler API for Scala 2.11?","tags":["scala","compiler-construction","scala-2.11"],"text":"Title: Is there an overview of the nsc compiler API for Scala 2.11?\nTags: scala, compiler-construction, scala-2.11\nSource: Stack Overflow\n\nQuestion:\nThe `scala.tools.nsc` API has been completely changed from 2.10 to 2.11. For example, there's no `Interpreter` anymore.\n\nI'm trying to build a site similar to http://codingbat.com in Scala, so I need the ability to compile and run code that's provided by a user while my program is running.\n\nDoes anyone know of something that explains how to use the new API anywhere? (I didn't have any luck Googling what I thought were reasonable search terms.) If not, could someone who knows something about the new API provide a small working example that lets you compile code and then retrieve the result of running it?\n\n**UPDATE** 2.11.0-M5 seems to break Script Engine support, but I went back to 2.11.0-M4 and I'm able to use `ScriptEngineManager` to get an interpreter. The weird thing is, the type is `scala.tools.nsc.interpreter.IMain`. That package and class don't appear in the 2.11.0-M4 Scaladocs, so I'm wondering if they've moved out of the compiler artifact somewhere else, or if there's just an oversight in Scaladoc production and they should still be there.\n\n========================================\n\nCode:\n```text\nscala.tools.nsc\n```\n\n```text\nInterpreter\n```\n\n```text\nScriptEngineManager\n```\n\n```text\nscala.tools.nsc.interpreter.IMain\n```\n\n```text\nWelcome to Scala version 2.11.0-M4 (OpenJDK Server VM, Java 1.7.0_25).\n Type in expressions to have them evaluated.\n Type :help for more information.\n\n scala> import javax.script.ScriptEngineManager\n import javax.script.ScriptEngineManager\n\n scala> val e = new ScriptEngineManager().getEngineByName(\"scala\")\n e: javax.script.ScriptEngine = scala.tools.nsc.interpreter.IMain@1741b6d\n```\n\n========================================\n\nComments:\n- Have a look on how scala shell script runs.\n- I probably still need to compile things, especially if I'm going to run a bunch of tests against them, but M5 seems to have broken the Script Engine support: Welcome to Scala version 2.11.0-M5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_40). Type in expressions to have them evaluated. Type :help for more information. scala> import javax.script.ScriptEngineManager import javax.script.ScriptEngineManager scala> val e = new ScriptEngineManager().getEngineByName(\"scala\") e: javax.script.ScriptEngine = null\n- Yeah. It looks like they broke the nsc.interpreter package out into its own sub-project called repl. But I can't figure out what it gets included with. The docs aren't in either the compiler or library docs and there doesn't seem to be a separate repl artifact.","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":5,"totalLines":50,"estimatedTokens":667}}1076{"id":"stack-16189940","source":"stackoverflow","questionId":16189940,"title":"parsing and evaluating simple language using javacc","tags":["java","parsing","compiler-construction","javacc"],"text":"Title: parsing and evaluating simple language using javacc\nTags: java, parsing, compiler-construction, javacc\nSource: Stack Overflow\n\nQuestion:\nI have simple language like:\n\n```\nfuna X ( X+3*funb(1) ) ;\nfunb Y ( 2*Y ) ;\nmain ( 2+func(func(1)) ) ;\nfunc A ( funa(A) ) ;\n```\n\nI used CFG to parse above as:\n\n```\nprogram => (statement)+\nstatement => ( | ) expression \nexpression => T( T)*\nT => P(P)*\nP => | expression | \n```\n\nAnd token recognizing:\n\n```\n -> \"main\"\n -> ([\"a\"-\"z\"])+\n -> ([\"A\"-\"Z\"])+\n -> \"(\"\n -> \")\"\n -> \";\"\n -> ([\"0\"-\"9\"])+\n -> \"+\"\n -> \"*\"\n```\n\nI am able to parse above using javaCC, but I dont have idea, how to evaluate above such programs with parser. Above program should evaluate to 15. How to implement such in javaCC, will it be possible using augmenting parser productions? Functions can appear at any place, before call or after call.\n\nThanks\n\n========================================\n\nCode:\n```text\nfuna X ( X+3*funb(1) ) ;\nfunb Y ( 2*Y ) ;\nmain ( 2+func(func(1)) ) ;\nfunc A ( funa(A) ) ;\n```\n\n```text\nprogram    => (statement)+\nstatement  => (<main_keyword> | <idl> <idu>) <lparan> expression <rparan> <semicolon>\nexpression => T(<plus> T)*\nT          => P(<multipliation>P)*\nP          => <idu> | <idl> <lparan> expression <rparan> | <number>\n```\n\n```text\n<main_keyword>   -> \"main\"\n<idl>            -> ([\"a\"-\"z\"])+\n<idu>            -> ([\"A\"-\"Z\"])+\n<lparan>         -> \"(\"\n<rparan>         -> \")\"\n<semicolon>      -> \";\"\n<number>         -> ([\"0\"-\"9\"])+\n<plus>           -> \"+\"\n<multiplication> -> \"*\"\n```\n\n```text\neval(...)\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":75,"estimatedTokens":388}}1077{"id":"stack-28034601","source":"stackoverflow","questionId":28034601,"title":"Adding appropriate PHI Nodes to llvm-ir","tags":["c++","compiler-construction","llvm","llvm-ir","llvm-c++-api"],"text":"Title: Adding appropriate PHI Nodes to llvm-ir\nTags: c++, compiler-construction, llvm, llvm-ir, llvm-c++-api\nSource: Stack Overflow\n\nQuestion:\nI have the following scenario:\n\n```\nA\n / \\\n B C\n \\ /\n D\n / \\\n E F\n \\ /\n ...\n```\n\nWhere `A,B,C,D,E,F` are all basic blocks, `|` is a downward pointing arrow.\n\nNow, in `B` I would have some def , say `%mul = ...`, that would later be used in the corresponding left-side block `E` , like `... = %mul ...` . I have the appropriate control flow inserted - either only the left branches will be taken, or only the right ones, the problem is that I get a `Instruction does not dominate all uses` error on the verification pass.\n\nI tried to fix this by adding `PHINode` instructions, like below:\n\n```\nB: ; preds = %A\n %shl = shl ...\n br label %D\n\nC: ; preds = %A\n ...\n br label %D\n\nD: ; preds = %B , %C\n ...\n br i1 %ctrl, label %E, label %F\n\nE: ; preds = %D\n %phi_nlcs = phi i32 [ %shl, %extra_l_diff ], [ 0, %for.cond ]\n %cmp = icmp slt i32 %phi_nlcs, %1\n br label ...\n```\n\nThe ellipses are there to hide the irrelevant details, also I've renamed the blocks but the core logic should be there. As you can see, I have replaced the use of `%shl` with the `PHINode` with the appropriate `replaceAllUsesWith()` call.\n\nHowever, now I get the following new error:\n\n```\nPHINode should have one entry for each predecessor of its parent basic block! \n%phi_nlcs = phi i32 [ %shl, %extra_l_diff ], [ 0, %for.cond ]\n```\n\nHow can I fix this?\n\n========================================\n\nCode:\n```text\nA\n     /   \\\n     B   C\n      \\ /\n       D\n      / \\\n     E   F\n      \\ /\n      ...\n```\n\n```text\nB:                                     ; preds = %A\n  %shl = shl ...\n  br label %D\n\nC:                                     ; preds = %A\n ...\n br label %D\n\nD:                                      ; preds = %B , %C\n ...\n br i1 %ctrl, label %E, label %F\n\nE:                                    ; preds = %D\n  %phi_nlcs = phi i32 [ %shl, %extra_l_diff ], [ 0, %for.cond ]\n  %cmp = icmp slt i32 %phi_nlcs, %1\n  br label ...\n```\n\n```text\nPHINode should have one entry for each predecessor of its parent basic block! \n%phi_nlcs = phi i32 [ %shl, %extra_l_diff ], [ 0, %for.cond ]\n```\n\n```text\nA,B,C,D,E,F\n```\n\n```text\n|\n```\n\n```text\nB\n```\n\n```text\n%mul = ...\n```\n\n```text\nE\n```\n\n```text\n... = %mul ...\n```\n\n```text\nInstruction does not dominate all uses\n```\n\n```text\nPHINode\n```\n\n```text\n%shl\n```\n\n```text\nPHINode\n```\n\n```text\nreplaceAllUsesWith()\n```\n\n```text\nD: \n  %phi_nlcs = phi [%shl, B] [undef, C] ; if we've come from B use %shl, if from C an undef value.\n  ...\n```\n\n========================================\n\nComments:\n- I just realised it myself and was going to post the answer but you beat me to it, thanks","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":15,"totalLines":148,"estimatedTokens":682}}1078{"id":"stack-30979673","source":"stackoverflow","questionId":30979673,"title":"can removing left recursion introduce ambiguity?","tags":["parsing","compiler-construction","context-free-grammar","ll-grammar"],"text":"Title: can removing left recursion introduce ambiguity?\nTags: parsing, compiler-construction, context-free-grammar, ll-grammar\nSource: Stack Overflow\n\nQuestion:\nLet's assume we have the following CFG G:\n\n```\nA -> A b A\nA -> a\n```\n\nWhich should produce the strings\n`a`, `aba`, `ababa`, `abababa`, and so on. Now I want to remove the left recursion to make it suitable for predictive parsing. The dragon book gives the following rule to remove immediate left recursions.\nGiven\n\n```\nA -> Aa | b\n```\n\nrewrite as\n\n```\nA -> b A'\nA' -> a A'\n | ε\n```\n\nIf we simply apply the rule to the grammar from above, we get grammar G':\n\n```\nA -> a A'\nA' -> b A A'\n | ε\n```\n\nWhich looks good to me, but apparently this grammar is not LL(1), because of some ambiguity. I get the following First/ sets:\n\n```\nFirst(A) = { \"a\" }\nFirst(A') = { ε, \"b\" }\n(A) = { $, \"b\" }\n(A') = { $, \"b\" }\n```\n\nFrom which I construct the parsing table\n\n```\n| a | b | $ |\n----------------------------------------------------\nA | A -> a A' | | |\nA' | | A' -> b A A' | A' -> ε |\n | | A' -> ε | |\n```\n\nand there is a conflict in `T[A',b]`, so the grammar isn't LL(1), although there are no left recursions any more and there are also no common prefixes of the productions so that it would require left factoring.\n\nI'm not completely sure where the ambiguity comes from. I guess that during parsing the stack would fill with `S'`. And you can basically remove it (reduce to epsilon), if it isn't needed any more. I think this is the case if another `S'` is below on on the stack.\n\nI think the LL(1) grammar G'' that I try to get from the original one would be:\n\n```\nA -> a A'\nA' -> b A\n | ε\n```\n\nAm I missing something? Did I do anything wrong?\n\nIs there a more general procedure for removing left recursion that considers this edge case? If I want to automatically remove left recursions I should be able to handle this, right?\n\nIs the second grammar G' a LL(k) grammar for some k > 1?\n\n========================================\n\nTop Answer:\nRemoving left recursion cannot introduce ambiguity. This kind of transformation preserves ambiguity. If the CFG is already ambiguous, the result will be ambiguous too, and if the original is not, the resulting neither.\n\n========================================\n\nCode:\n```text\nA -> A b A\nA -> a\n```\n\n```text\nA -> Aa | b\n```\n\n```text\nA  -> b A'\nA' -> a A'\n    | ε\n```\n\n```text\nA  -> a A'\nA' -> b A A'\n    | ε\n```\n\n```text\nFirst(A) = { \"a\" }\nFirst(A') = { ε, \"b\" }\nFollow(A) = { $, \"b\" }\nFollow(A') = { $, \"b\" }\n```\n\n```text\n|   a          |   b            |  $           |\n----------------------------------------------------\nA   | A -> a A'    |                |              |\nA'  |              | A' -> b A A'   | A' -> ε      |\n    |              | A' -> ε        |              |\n```\n\n```text\nA  -> a A'\nA' -> b A\n    | ε\n```\n\n```text\na\n```\n\n```text\naba\n```\n\n```text\nababa\n```\n\n```text\nabababa\n```\n\n```text\nT[A',b]\n```\n\n```text\nS'\n```\n\n```text\nS'\n```\n\n```text\nA ⇒ A b A\n  ⇒ A b a\n  ⇒ A b A b a\n  ⇒ A b a b a\n  ⇒ a b a b a\n\nA ⇒ A b A\n  ⇒ A b A b A\n  ⇒ A b A b a\n  ⇒ A b a b a\n  ⇒ a b a b a\n```\n\n```text\nA ⇒ a              A ⇒ a\nA ⇒ a b A          A ⇒ A b a\n```\n\n```text\na b a b a\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":17,"totalLines":173,"estimatedTokens":795}}1079{"id":"stack-2694712","source":"stackoverflow","questionId":2694712,"title":"What is everything involved from typing in code to executing a program?","tags":["ide","compiler-construction"],"text":"Title: What is everything involved from typing in code to executing a program?\nTags: ide, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI realized, when just asking a question, I don't understand all the components that are part of the coding process.\n\nThis seems a silly question, but I can't find a definitive answer on Google, Wiki, nothing.\n\nWhat exactly are all the parts called, and how do they work and intertwine? I'm talking whatever you type code into, whatever checks that for errors, compiles it, and runs it.\n\nI'd appreciate any links, repeats, etc. I apologize for such a bland, stupid question.\n\nEDIT: Well, I'm trying to start Perl, so anything about Perl would help. Like, how to use Notepad++ and eventually compile Perl.\n\n========================================\n\nCode:\n```text\nperl foo.pl\n```\n\n```text\nperl myfilename.pl\n```\n\n```text\nmyfile.pl\n```\n\n========================================\n\nComments:\n- Do you have a particular language in mind? The answers are somewhat different for different languages.\n- Not a stupid question, just a day 1 question.\n- Good luck and dont be afraid to ask more questions.\n- I wish I could upvote this again, because I just learned AND lawled. Thanks for the help!","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":37,"estimatedTokens":308}}1080{"id":"stack-14710118","source":"stackoverflow","questionId":14710118,"title":"Parsing a C family language out of order","tags":["c","parsing","compiler-construction","programming-languages"],"text":"Title: Parsing a C family language out of order\nTags: c, parsing, compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nC is parsed strictly in order, i.e. everything has to be declared before it is used; in particular, types must be declared before variables of those types. This makes sense because the grammar would be ambiguous if you didn't know what was the name of a type and what wasn't, e.g. `a * b` depends on whether `a` names a type.\n\nOn the other hand, some C family languages have the desirable property of relaxing this restriction (thus eliminating manual juggling of header files). I'm writing a parser for a C-superset language which is intended to likewise have that restriction relaxed, so now I need to figure out how to do it.\n\nOne method that occurs to me would be to do two passes. The first pass goes through everything, taking advantage of the fact that everything at the top level must be a declaration, not a statement, and picks up all the types. At this stage function bodies are left unexamined, just picked up as token streams delimited by matching braces. The second pass parses function bodies. Local declarations within a function would have to be in order, but that's not really a problem.\n\nAre there any stumbling blocks in that method I haven't thought of?\n\nHow do compilers for C++, Java, C# etc. typically handle it for those parts of those languages that don't require declarations in order?\n\n========================================\n\nTop Answer:\nC++ **does** require in-order declarations.\n\nKeep in mind that C and C++ are a completely different ballgame. They are using positively ancient linker technology (C because it **is** ancient, C++ because its almost as ancient and compatible with C linkers). Both result in binaries that run directly on the CPU, with no runtime support to speak of.\n\nJava and C# have much improved linkers to rely on, and a huge runtime support system to use.\n\nEither has pros and cons. One of the cons of the C/C++ approach is that everything must be resolved at compile time, because at runtime the application is on its own. The pro is that everything is resolved at compile time, so at runtime the application can be left alone.\n\n========================================\n\nCode:\n```text\na * b\n```\n\n```text\na\n```\n\n========================================\n\nComments:\n- Do you fully preprocess the source before that?\n- Yes, preprocessing will still have to be done as a separate initial step.\n- Well, I abandoned this method because of the ambiguity presented by `a * b`. Btw, some other things to consider include incomplete types. It may not be right to first collect all types and then apply them. What if that works, but the code doesn't actually compile because at some point a type is still incomplete?\n- Then an error message would be generated in the second pass.\n- If you preserve enough information about the order of things.\n- It is common to do multiple passes, but not within the parsing stage per se. I'm not sure how you could write the grammar to disambiguate between something that is a variable declaration and something that isn't, without knowing the types beforehand; can you be more specific about what you have in mind?\n- Actually that's a good point, C++ doesn't require functions within a class to be declared before they are called, but it does still require types to be declared before they are used. I'm planning to stick to C linker technology, but I hope that doesn't preclude making a few improvements on the parsing side.","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":47,"estimatedTokens":888}}1081{"id":"stack-10307746","source":"stackoverflow","questionId":10307746,"title":"Status of machine code generation in LLVM?","tags":["compiler-construction","llvm"],"text":"Title: Status of machine code generation in LLVM?\nTags: compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nRecently I've been looking into LLVM as a back-end for a compiler project, which looks very promising.\n\nHowever, it seems that LLVM can JIT code (which is very cool!) or it can export assembly for the native platform, but it can't actually create native executables. This seems like a major limitation for the compiler back-end infrastructure that LLVM claims to be.\n\nI'm working on a project where I'd like the compiled executables to be able to run without LLVM installed (i.e. no JIT). Does LLVM have any native assemblers that can be used to generate machine code (not LLVM bitcode)?\n\n========================================\n\nCode:\n```text\nld\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":17,"estimatedTokens":193}}1082{"id":"stack-6233792","source":"stackoverflow","questionId":6233792,"title":"DLR LambdaExpressions and the System.Runtime.CompilerServices.Closure object","tags":["c#","compiler-construction","closures","dynamic-language-runtime"],"text":"Title: DLR LambdaExpressions and the System.Runtime.CompilerServices.Closure object\nTags: c#, compiler-construction, closures, dynamic-language-runtime\nSource: Stack Overflow\n\nQuestion:\nI'm working on a small programming language for the Microsoft DLR, and having a bit of a problem invoking my anonymous methods. Specifically, the code:\n\n```\nDelegate CompiledBody = Expression.Lambda(rt.Parser.ParseSingle(Body), parms).Compile();\n```\n\nSo, parms is an array containing a single ParameterExpression, and the first argument contains the appropriate Expressions to define the anonymous function. When I try to invoke my Delegate using an Expression.Call on CompiledBody.Method (a MethodInfo), I receive the error:\n\n```\nUnhandled Exception: System.ArgumentException: Expression of type 'System.Object' \ncannot be used for parameter of type 'System.Runtime.CompilerServices.Closure' \nof method 'Shiro.Runtime.ShiroAtom lambda_method(System.Runtime.CompilerServices\n.Closure, Shiro.Runtime.ShiroAtom)'\n```\n\nNow, somewhere along the way my one-argument method gained a second argument, of type System.Runtime.CompilerServices.Closure (the second one, of type ShiroAtom, is my parameter). This makes sense, except that (a) I don't really care if the method in this context is in a Closure scope and (b) I can't seem to create even an empty Closure scope to pass in this parameter.\n\nI'd appreciate any help! Thanks in advance.\n\nEDIT: Some additional information based on the awesome reply below:\n\nWhere this code occurs is deep in the bowels of my Parser. I have a stream of tokens (actually, Atoms) which get translated into an AST. This particular bit is the function call parse routine. It created a CompiledBody, then tries to invoke it using something like:\n\n```\nreturn Expression.Call(CompiledBody.Method, Expression.Constant(\"argument\"));\n```\n\nThe resulting Lambda represents a function. Based on my architecture there are only a few places I can call DynamicInvoke or just calling the Compiled Delegate, and this isn't one of them. I wish I could provide a more substantial example but this situation occurs in the midst of a hand-coded parser, and it would take way too much code to really communicate *why* the situation is this way, but I really need a way to call the compiled Lambda via Expression.Call, as shown above.\n\nThe crux of the problem is that my Compiled Lambda requires 1 additional parameter to the ones I specify, a CompilerServices.Closure, and I don't know how to make one.\n\n========================================\n\nCode:\n```text\nDelegate CompiledBody = Expression.Lambda(rt.Parser.ParseSingle(Body), parms).Compile();\n```\n\n```text\nUnhandled Exception: System.ArgumentException: Expression of type 'System.Object' \ncannot be used for parameter of type 'System.Runtime.CompilerServices.Closure' \nof method 'Shiro.Runtime.ShiroAtom lambda_method(System.Runtime.CompilerServices\n.Closure, Shiro.Runtime.ShiroAtom)'\n```\n\n```text\nreturn Expression.Call(CompiledBody.Method, Expression.Constant(\"argument\"));\n```\n\n```text\nusing System;\nusing System.Linq.Expressions;\n\nclass Program {\n    static void Main(string[] args) {\n        var outerParam = Expression.Parameter(typeof(int), \"outerParam\");\n\n        var lambda =\n            Expression.Lambda<Func<int, Action>>(\n                Expression.Lambda<Action>(\n                    Expression.Call(\n                        typeof(Console).GetMethod(\"WriteLine\", new Type[] { typeof(object) }),\n                        Expression.Convert(outerParam, typeof(object))\n                    )\n                ),\n                outerParam\n            ).Compile();\n\n        var actionParam = Expression.Parameter(typeof(Action), \"action\");\n        var lambdaInvoker =\n            Expression.Lambda<Action<Action>>(\n                Expression.Invoke(actionParam),\n                actionParam\n            ).Compile();\n\n        lambdaInvoker(lambda(100));\n        lambdaInvoker(lambda(200));\n        Console.ReadLine();\n    }\n}\n```\n\n========================================\n\nComments:\n- Thanks for the reply -- this is close to what I'm looking for, except that I need to invoke the resulting lambda in an Expression.Call, I can't just invoke it inline, since we're generating AST and I'm binding the parameters to expressions whose values can't be reliably determined until runtime. I'll try to expand what I have above, but I might be able to take the example you gave me and figure it out. Thanks again!\n- I was able to finesse this into exactly what I needed. Many thanks!","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":92,"estimatedTokens":1133}}1083{"id":"stack-3720140","source":"stackoverflow","questionId":3720140,"title":"Examples of CLR compiler optimizations","tags":["c#",".net","compiler-construction","clr","compiler-optimization"],"text":"Title: Examples of CLR compiler optimizations\nTags: c#, .net, compiler-construction, clr, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nI'm doing a presentation in few months about .Net performance and optimization, I wanted to provide some samples of unnecessary optimization, things that will be done by the compiler anyways.\n\nwhere can I find some explanation on what optimizations the compiler is actually capable of maybe some before and after code?\n\n========================================\n\nTop Answer:\nWhat I think would be even better than examples of \"things that will be done by the compiler anyways\" would be examples of scenarios where the compiler *doesn't* perform \"optimizations\" that the developer *assumes* will yield a performance improvement but which, in fact, won't.\n\nFor example sometimes a developer will assume that caching a value locally will improve performance, when actually the savings of having one less value on the stack outweighs the miniscule cost of a field access that can be inlined.\n\nOr the developer might assume that \"force-inlining\" a method call (essentially by stripping out the call itself and replacing with copied/pasted code) will be worthwhile, when in reality keeping the method call as-is would result in its getting inlined by the compiler only when it makes sense (when the benefit of inlining outweighs the growth in code size).\n\nThis is only a general idea, of course. I don't have concrete code samples that I can point to; but maybe you can scrounge some up if you look for them.\n\n========================================\n\nComments:\n- Aren't the CLR and the JVM(s with JIT) performing most optimizations at runtime/ duringJIT-compilation?","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":24,"estimatedTokens":427}}1084{"id":"stack-5237458","source":"stackoverflow","questionId":5237458,"title":"Differences between open64 versus llvm compilers","tags":["compiler-construction","comparison","llvm"],"text":"Title: Differences between open64 versus llvm compilers\nTags: compiler-construction, comparison, llvm\nSource: Stack Overflow\n\nQuestion:\nCould someone point out the differences between the open64 and llvm?\n\nI know that the open64 uses whirl IR which has 5 stages and differs a lot since each stage lowers significantly the code to the machine whereas the llvm has a single IR which is used in midlevel optimizations, later is lowered into target independent instructions (dags) and dependent ones. \n\n**1)** Is whirl also SSA,\n\n**2)** and does it uses virtual registers,\n\n**3)** does open64 has modular design like llvm,\n\n**4)** is it easy to develop pass and plug it into the toolchain?\n\nBoth compilers excel at midlevel interprocedural optimizations and transformations but\n\n**5)** does open64 have support for JITting or any kinds of dynamic translation natively built into the framework?\n\nIt looks that the quality of CG more or less is similar in both cases.\n\n**6)** What about the front ends, is it possible to easily extend ones with pragmas or bind new ones without modifying the Whirl IR (that is the problem with SUIF)?\n\nAs far as I can tell the open64 is widely used in many commercial and academical projects (UPC, AMD, Nvidia, Tensilica) but it looks that there are many branches of it (growing after 2003) and each has it's own features and limitations. Moreover, there isn't fixed developer community and environment or support, the documentation hardly exists in contrast to the llvm, and simply there isn't simple one direction in which the compiler advances.\n\nThere are also differences in licensing where the llvm matches more to BSD licensing style and the open64 is based on GPL.","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":28,"estimatedTokens":425}}1085{"id":"stack-7848521","source":"stackoverflow","questionId":7848521,"title":"How to get Scala Compiler Plugin to work in Scala IDE","tags":["eclipse","scala","plugins","compiler-construction","scala-ide"],"text":"Title: How to get Scala Compiler Plugin to work in Scala IDE\nTags: eclipse, scala, plugins, compiler-construction, scala-ide\nSource: Stack Overflow\n\nQuestion:\n*Note: This is an exact repost of the same question on the Scala-IDE mailing list, where I got zero reply and gave up waiting after 5 days.*\n\nI'm writing a Scala Compiler Plugin. It's currently called\nscalawrapper (but I might find a better name later). I have the\nTypesafe Stack installed, and I put my plugin in it under:\n\n```\nC:\\Program Files\\typesafe-stack\\misc\\scala-devel\\plugins\n```\n\nNow I can call scalac like this:\n\n```\nscalac -classpath ... -Xplugin-require:scalawrapper test\\...\n```\n\nand it finds and uses my plugin automatically.\n\nUnfortunately, I just can't get the Scala-IDE to use it. I always get\n`Missing required plugin: scalawrapper`. In the \"Project Properties\n\\Scala Compiler (Use Project Settings)\\Advanced\" tab, I have entered\nscalawrapper in the Xplugin-require field, and that works, since I get\nthe error. But nothing I can type in Xpluginsdir seem to have any\neffect.\n\nI have tried the absolute path given above, have tried with forward\nand backward slashes, have tried a temporary directory without spaces\nin it, have tried relative to project directory, have tried relative\nto workspace. It just will not use anything. I should also note that\nis is not specific to my own plugin, as I tried to use the ScalaCL\nplugin before in a previous version of the Scala-IDE, and failed for\nthe exact same reason.\n\nI have just updated today (15.10.2011) and so should have the latest\nversion (it seems I can finally explicitly set the indentation to 4,\nbut maybe I just overlooked the setting in the past).\n\nCan someone tell me what it actually expects, and what is uses as\ndefault when I don't put anything in Xpluginsdir?\n\n========================================\n\nCode:\n```text\nC:\\Program Files\\typesafe-stack\\misc\\scala-devel\\plugins\n```\n\n```text\nscalac -classpath ... -Xplugin-require:scalawrapper test\\...\n```\n\n```text\nMissing required plugin: scalawrapper\n```\n\n```text\nXplugin = C:\\code\\project\\workspace\\plugin\\xxx-0.0.1.jar\nXplugin-require = xxx\n```\n\n```text\nWindows->Preferences->Scala->Compiler\n```\n\n```text\n$workspace_loc\n```\n\n```text\n$project_loc\n```\n\n```text\n$workspace_loc\n```\n\n```text\n$workspace_loc\n```\n\n```text\nProperties->Resource->Linked Resources\n```\n\n```text\n$workspace_loc\n```\n\n```text\nWindows->Preferences->Scala->Compiler\n```\n\n```text\n$workspace_loc\n```\n\n========================================\n\nComments:\n- You have not read my question. I know where the setting is: \"In the \"Project Properties \\Scala Compiler (Use Project Settings)\\Advanced\" tab,\" The only problem is, that IT DOESN'T WORK.\n- Sorry, I did read your question, but I wasn't explicit enough about what I meant. I've edited the answer.\n- Thanks. If I do it exactly like you said, it seems to work. I think the reason I thought it did not work before was that Eclipse throws away all output (both System.out and log(...)), so it looks like it did nothing. The same options that produce the output with scalac do nothing in Eclipse. I had to throw an exception to prove that it works, but that's a different problem to be solved in a different question. Have you ever found out how to use Xpluginsdir so that you don't have to specify the jar explicitly?","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":13,"totalLines":107,"estimatedTokens":830}}1086{"id":"stack-11353555","source":"stackoverflow","questionId":11353555,"title":"What happens to casts using generics - (T)object - at run-time in Java?","tags":["java","class","generics","compiler-construction","casting"],"text":"Title: What happens to casts using generics - (T)object - at run-time in Java?\nTags: java, class, generics, compiler-construction, casting\nSource: Stack Overflow\n\nQuestion:\nAs far as I understand, Java Generics erases all the information concerning the parameter type T in the generic method (or in the generic class). That's why we can't use \n\nnew expression, such as\n\n```\nnew T()\n```\n\ninstanceof expression, such as\n\n```\nif(obj instanceof T)\n```\n\ninside a generic method.\n\nMy question is how the parameter type T works inside a generic method when it comes to casting. For example, I have 3 simple classes here:\n\n```\npublic class People {\n\n String name;\n\n public String getName() {\n return name;\n }\n\n public void setName(String name) {\n this.name = name;\n }\n\n public People(String name) {\n super();\n this.name = name;\n }\n\n @Override\n public String toString() {\n return \"People [name=\" + name + \"]\";\n }\n\n}\n\npublic class GenericClass {\n\n public T getCast(People p) {\n return (T)p;\n }\n}\n\npublic class App \n{\n public static void main( String[] args )\n {\n People p = new People(\"Kewei\");\n\n GenericClass genericClass = new GenericClass();\n Object p_object = genericClass.getCast(p);\n\n System.out.println(p_object);\n }\n}\n```\n\nWhen it executes the `(T)p` in the generic method `getCast(People p)`. Does it just take the type T as Object? or It just removes the whole `(T)` casting at compile time? I've read Bruce Eckel's «Thinking in Java» from which I roughly understand as:\n\n Java compiler checks at method's entry/leave points at compile time to\n make sure the internal consistency of generic method, and inserts\n casting code (at compile time).\n\nIs it the right understanding?\n\nThanks.\n\n========================================\n\nTop Answer:\nJava never casts anything; at run time it's simply treated \"as an object\".\nThe JRE attempts to call methods on that object, and since they've been proven to exists at compile it, it works.\n\nThere's **no reference** to what class T is at runtime. **None**. So no casting is done.\n\n========================================\n\nCode:\n```text\nnew T()\n```\n\n```text\nif(obj instanceof T)\n```\n\n```text\npublic class People {\n\n    String name;\n\n    public String getName() {\n        return name;\n    }\n\n    public void setName(String name) {\n        this.name = name;\n    }\n\n    public People(String name) {\n        super();\n        this.name = name;\n    }\n\n    @Override\n    public String toString() {\n        return \"People [name=\" + name + \"]\";\n    }\n\n}\n\npublic class GenericClass<T> {\n\n    public T getCast(People p) {\n        return (T)p;\n    }\n}\n\npublic class App \n{\n    public static void main( String[] args )\n    {\n        People p = new People(\"Kewei\");\n\n        GenericClass<Integer> genericClass = new GenericClass<Integer>();\n        Object p_object = genericClass.getCast(p);\n\n        System.out.println(p_object);\n    }\n}\n```\n\n```text\n(T)p\n```\n\n```text\ngetCast(People p)\n```\n\n```text\n(T)\n```\n\n```text\npublic class GenericClass {\n\n    public Object getCast(People p) {\n        // In this case the cast to T is removed because T's upper bound is Object.\n        // But if T's upper bound was, say, Number, then there would still be a cast\n        // to the upper bound. e.g. \"(Number)p\", and the return type would also be Number\n        return p;\n    }\n}\n```\n\n```text\n// somewhere else\nGenericClass genericClass = new GenericClass();\np_object = (Integer)genericClass.getCast(p);\n```\n\n```text\nGenericClass<Integer>\n```\n\n```text\ngetCast\n```\n\n```text\nGenericClass<S>\n```\n\n========================================\n\nComments:\n- Not true. Casts to normal classes are also checked at runtime\n- We do still need checks for the casts and assignments though. i.e. `List list; String s = list.get(0);` will have a dynamic check if the object really is a string. That's a predictable `ld&#47;cmp&#47;br` sequence though (i.e. a single cycle if we don't miss to memory).\n- Yes, I meant the Generic class itself cas no reference to T and does no casts. Very poorly written reponse on my part.\n- +1 And that's why `ClassCastException`s caused by heap pollution are harder to debug - because their stacktraces don't point to the actual bug, but the place where the compiler happened to insert a cast.\n- I think you mean upper bound, not lower bound. Also, it's the erasure of the upper bound, which is the erasure of the left most element of the upper bound.\n- Thank you very much @newacct, for your explanation. As you said, **In the places that call the method that returns T, the compiler will insert a cast into the actual type of T. In this case, in the main method, you have a GenericClass, so when getCast returns T, it is cast to Integer.** But in any case, for my method `T getCast(People p)`, an instance of `People` can't be casted into `Integer`, I don't understand how come my code works \"magically\". Why it doesn't throw an `ClassCastException`? It seems that the `p` (instance of `People`) was never ever casted into `Integer`. Why?\n- @keweishang: I'm not sure, but I'm guessing it might have to do with it being assigned to a variable of type `Object`. If you change it to `Integer p_object` does it make a difference?\n- @newacct if I change the call of the generic method to `Object p_object = (Integer)genericClass.getCast(p);`, it gives an `ClassCastException: com.foo.People cannot be cast to java.lang.Integer`. Theoretically, the compiler should itself insert automatically a cast into the actual type of `T` (in this case, `Integer`) for the method `genericClass.getCast(p)` at compile-time.\n- To this point, I still have 2 questions: **1**, inside the body a generic method who returns T, what type does it return, the upper bound of T or Object? **2**. Just right after the call to a generic method who returns T, what the compiler will add as cast? Does the compiler **always** add a the actual type of T, **or** it depends on the type of object to whom the reference is given?\n- @keweishang: 1. it returns whatever T is erased to, which in this case is Object. 2. A cast is necessary if the type that is returned (the erasure) is not assignable to the type of whatever you are assigning or passing that value to. In this case you are assigning it to a variable of type Object, in which case a cast is not *necessary*, and it seems that the compiler does not insert a cast. However, it would not be incorrect for some other compiler to insert a cast in that case too.\n- Yes, your explanation makes sense! I think you're right! Thanks a lot :)","metadata":{"transformedAt":"2026-08-18T18:32:17.947Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":200,"estimatedTokens":1623}}1087{"id":"stack-75920595","source":"stackoverflow","questionId":75920595,"title":"Trying to solve dangling else for a Mini-C grammar","tags":["c","parsing","compiler-construction","grammar","sly"],"text":"Title: Trying to solve dangling else for a Mini-C grammar\nTags: c, parsing, compiler-construction, grammar, sly\nSource: Stack Overflow\n\nQuestion:\nI have this grammar:\n\n```\ntranslation_unit \n ::= external_declaration\n | translation_unit external_declaration\n\nexternal_declaration \n ::= function_definition\n | declaration\n\nfunction_definition \n ::= type_specifier declarator compound_statement\n | STATIC type_specifier declarator compound_statement\n\ndeclaration \n ::= type_specifier declarator ';'\n | EXTERN type_specifier declarator ';'\n\ndeclaration_list_opt \n ::= declaration_list\n | empty\n \ndeclaration_list\n ::= declaration_list declaration\n | declaration\n\ntype_specifier \n ::= INT\n | CHAR\n\ndeclarator \n ::= direct_declarator\n | ASTERISK declarator\n\ndirect_declarator \n ::= ID\n | direct_declarator '(' parameter_type_list ')'\n | direct_declarator '(' ')'\n\nparameter_type_list \n ::= parameter_list ',' ELLIPSIS\n | parameter_list\n\nparameter_list \n ::= parameter_list ',' parameter_declaration\n | parameter_declaration\n\nparameter_declaration \n ::= type_specifier declarator\n\ncompound_statement \n ::= '{' declaration_list_opt statement_list '}'\n | '{' declaration_list_opt '}'\n\nexpression_statement \n ::= expression ';'\n\nexpression \n ::= equality_expression\n\nexpression \n ::= equality_expression '=' expression\n | equality_expression '+=' expression\n | equality_expression '-=' expression\n | equality_expression '*=' expression\n | equality_expression '/=' expression\n | equality_expression '%=' expression\n\nequality_expression \n ::= relational_expression\n\nequality_expression \n ::= equality_expression '==' relational_expression\n | equality_expression '!=' relational_expression\n\nrelational_expression \n ::= additive_expression\n\nrelational_expression \n ::= relational_expression '' additive_expression\n | relational_expression '=' additive_expression\n\npostfix_expression \n ::= primary_expression\n | postfix_expression '(' argument_expression_list ')'\n | postfix_expression '(' ')'\n | postfix_expression '[' expression ']'\n\nargument_expression_list \n ::= argument_expression_list ',' expression\n | expression\n\nunary_expression \n ::= postfix_expression\n | '-' unary_expression\n | '+' unary_expression\n | '!' unary_expression\n | '*' unary_expression\n | '&' unary_expression\n\nmult_expression \n ::= unary_expression\n\nmult_expression \n ::= mult_expression '*' unary_expression\n | mult_expression '/' unary_expression\n | mult_expression '%' unary_expression\n\nadditive_expression \n ::= mult_expression\n | additive_expression '+' mult_expression\n | additive_expression '-' mult_expression\n\nprimary_expression \n ::= ID\n | INUMBER\n | FNUMBER\n | CHARACTER\n | string_literal\n | '(' expression ')'\n \nstring_literal \n ::= string_literal STRING\n | STRING\n\nstatement \n ::= compound_statement\n | expression_statement\n | selection_statement\n | iteration_statement\n | jump_statement\n\njump_statement \n ::= RETURN ';'\n | RETURN expression ';'\n | BREAK ';'\n | CONTINUE ';'\n\niteration_statement \n ::= WHILE '(' expression ')' statement\n | FOR '(' expression_statement expression_statement expression ')' statement\n\nselection_statement \n ::= IF '(' expression ')' statement\n | IF '(' expression ')' statement ELSE statement\n \nstatement_list \n ::= statement_list statement\n | statement\n```\n\nWhen i implememented a parser using Sly it says that selection_statement has a shift reduce conflict.\n\nI am trying to solve it without using precedence.\n\nI tried using something like this:\n\n```\n@_(\"matched\",\n \"unmatched\")\n def selection_statement(self, p):\n pass\n \n@_(\"IF '(' expression ')' matched ELSE matched\")\n def matched(self, p):\n pass\n\n@_(\"IF '(' expression ')' matched\",\n \"IF '(' expression ')' unmatched\",\n \"IF '(' expression ')' matched ELSE unmatched\")\n def unmatched(self, p):\n pass\n```\n\nBut it has an infinite recursion on matched.\n\nI tried adding other statements on matched to solve the recursion but it only generates more conflicts.\n\nWhat should i use to elimiante the recursion?\n\n========================================\n\nCode:\n```text\ntranslation_unit \n    ::= external_declaration\n    | translation_unit external_declaration\n\nexternal_declaration \n    ::= function_definition\n    | declaration\n\nfunction_definition \n    ::= type_specifier declarator compound_statement\n    | STATIC type_specifier declarator compound_statement\n\ndeclaration \n    ::= type_specifier declarator ';'\n    | EXTERN type_specifier declarator ';'\n\ndeclaration_list_opt \n    ::= declaration_list\n    | empty\n    \ndeclaration_list\n    ::= declaration_list declaration\n    | declaration\n\ntype_specifier \n    ::= INT\n    | CHAR\n\ndeclarator \n    ::= direct_declarator\n    | ASTERISK declarator\n\ndirect_declarator \n    ::= ID\n    | direct_declarator '(' parameter_type_list ')'\n    | direct_declarator '(' ')'\n\nparameter_type_list \n    ::= parameter_list ',' ELLIPSIS\n    | parameter_list\n\nparameter_list \n    ::= parameter_list ',' parameter_declaration\n    | parameter_declaration\n\nparameter_declaration \n    ::= type_specifier declarator\n\ncompound_statement \n    ::= '{' declaration_list_opt statement_list '}'\n    | '{' declaration_list_opt '}'\n\nexpression_statement \n    ::= expression ';'\n\nexpression \n    ::= equality_expression\n\nexpression \n    ::= equality_expression '=' expression\n    | equality_expression '+=' expression\n    | equality_expression '-=' expression\n    | equality_expression '*=' expression\n    | equality_expression '/=' expression\n    | equality_expression '%=' expression\n\nequality_expression \n    ::= relational_expression\n\nequality_expression \n    ::= equality_expression '==' relational_expression\n    | equality_expression '!=' relational_expression\n\nrelational_expression \n    ::= additive_expression\n\nrelational_expression \n    ::= relational_expression '<'  additive_expression\n    | relational_expression '>'  additive_expression\n    | relational_expression '<=' additive_expression\n    | relational_expression '>=' additive_expression\n\npostfix_expression \n    ::= primary_expression\n    | postfix_expression '(' argument_expression_list ')'\n    | postfix_expression '(' ')'\n    | postfix_expression '[' expression ']'\n\n\nargument_expression_list \n    ::= argument_expression_list ',' expression\n    | expression\n\nunary_expression \n    ::= postfix_expression\n    | '-' unary_expression\n    | '+' unary_expression\n    | '!' unary_expression\n    | '*' unary_expression\n    | '&' unary_expression\n\nmult_expression \n    ::= unary_expression\n\nmult_expression \n    ::= mult_expression '*' unary_expression\n    | mult_expression '/' unary_expression\n    | mult_expression '%' unary_expression\n\nadditive_expression \n    ::= mult_expression\n    | additive_expression '+' mult_expression\n    | additive_expression '-' mult_expression\n\nprimary_expression \n    ::= ID\n    | INUMBER\n    | FNUMBER\n    | CHARACTER\n    | string_literal\n    | '(' expression ')'\n    \nstring_literal \n    ::= string_literal STRING\n    | STRING\n\nstatement \n    ::= compound_statement\n    | expression_statement\n    | selection_statement\n    | iteration_statement\n    | jump_statement\n\njump_statement \n    ::= RETURN ';'\n    | RETURN expression ';'\n    | BREAK ';'\n    | CONTINUE ';'\n\niteration_statement \n    ::= WHILE '(' expression ')' statement\n    | FOR '(' expression_statement expression_statement expression ')' statement\n\nselection_statement \n    ::= IF '(' expression ')' statement\n    | IF '(' expression ')' statement ELSE statement\n    \nstatement_list \n    ::= statement_list statement\n    | statement\n```\n\n```text\n@_(\"matched\",\n   \"unmatched\")\n   def selection_statement(self, p):\n      pass\n    \n@_(\"IF '(' expression ')' matched ELSE matched\")\n   def matched(self, p):\n      pass\n\n@_(\"IF '(' expression ')' matched\",\n   \"IF '(' expression ')' unmatched\",\n   \"IF '(' expression ')' matched ELSE unmatched\")\n   def unmatched(self, p):\n      pass\n```\n\n```text\nstatement \n    ::= matched\n    | unmatched\n\nmatched \n    ::= if_matched\n    | iteration_statement_matched\n    | compound_statement\n    | expression_statement\n    | jump_statement\n\nunmatched\n    ::= if_unmatched\n    | iteration_statement_unmatched\n\nif_matched \n    ::= IF '(' expression ')' matched ELSE matched\n\nif_unmatched \n    ::= IF '(' expression ')' statement\n    | IF '(' expression ')' matched ELSE unmatched\n\niteration_statement_matched \n    ::= WHILE '(' expression ')' matched\n\niteration_statement_matched \n    ::= FOR '(' expression_statement expression_statement expression ')' matched\n\niteration_statement_unmatched \n    ::= WHILE '(' expression ')' unmatched\n\niteration_statement_unmatched \n    ::= FOR '(' expression_statement expression_statement expression ')' unmatched\n```\n\n========================================\n\nComments:\n- This is a canonical example of a shift / reduce conflict. For example, it is the one used in the Bison manual for discussing the issue. To implement the C semantics of binding the `else` to the innermost matching `if`, you resolve the conflict by shifting, which is Bison's default for all shift / reduce conflicts. I don't know how to tell Sly to do the same.\n- Isn't this really a Python question? The grammar may be C-like, but the coding is in Python and Sly. Also, isn't Sly retired?\n- @TomKarzes: Sly is written (and used) in Python, but that probably doesn't apply to this situation.\n- @JohnBollinger: Sly automatically shifts but it displays a warning. I am trying to remove the warning.\n- @Juan_2054: You need to make your grammar unambiguous then. How would you handle a nested if statement with a single else?\n- @SeanDuggan I want to relation it to the nearest if.\n- The Sly documentation isn't bad. It contains a section on dealing with ambiguities. I'm inclined to guess that by declaring `ELSE` to have higher precedence than `IF`, you will satisfy Sly that it does not need to warn.\n- @SeanDuggan There are two types of code in the post: (1) Sly and (2) Python (embedded in the Sly code, but directly visible). There is no C code in the post. Hence, the language tags \"Sly\" and \"Python\" apply.\n- @TomKarzes: It looks like they may be trying to implement C in Sly, but I'll yield that they don't say as much.\n- @SeanDuggan The post title describes it as a \"Mini-C grammar\", so presumably it's meant to be a simplified subset of C.\n- Glad you solved it! You will be able to accept your self-answer 48 hours after the question was asked (you can accept the answers of others within 10 minutes, I think). Any chance you can post the Sly code you wound up with, to help others in the future?\n- @SeanDuggan Sure. When i complete it i will upload it.","metadata":{"transformedAt":"2026-08-18T18:32:17.948Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":410,"estimatedTokens":2634}}1088{"id":"stack-51006022","source":"stackoverflow","questionId":51006022,"title":"Why is an abstract syntax tree necessary? Why cant a recursive descent parser parse as it goes?","tags":["parsing","compiler-construction","interpreter"],"text":"Title: Why is an abstract syntax tree necessary? Why cant a recursive descent parser parse as it goes?\nTags: parsing, compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nI am a beginner when it comes to interpreter design and after learning a little about language design (grammars, language, lexers, parsers), I don't understand why it is necessary for the parser to build an abstract syntax tree as it goes. Why doesn't it just use the recursively called functions to perform the operation right there. \n\nHere is the most simple example of what I'm talking about that I found. https://www.youtube.com/watch?v=N55XNj8KjC4.\n\n========================================\n\nComments:\n- It isn't always necessary, and you can write compilers for some languages without one' especially for an interpretive target. But the moment you want to optimize you start to need one.\n- Re the title question: a recursive descent parser *does* \"parse as it goes\". What you appear to be asking is: why can't it *execute* as it goes?\n- In fact, in the bad old days, some compilers really *did* emit code as they went (using either smart assemblers or backpatching for branches to machine code that is not yet generated). The machine code quality of such compilers was generally poor.\n- And the example you're talking about isn't a compiler. Its is an expressio evaluator.\n- And it's not just the so-called 'bad old days'. There are compilers out there now that do exactly this. @torek","metadata":{"transformedAt":"2026-08-18T18:32:17.948Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":17,"estimatedTokens":370}}1089{"id":"stack-31793180","source":"stackoverflow","questionId":31793180,"title":"How to avoid undesired constant folding performed by LLVM's code generator?","tags":["compiler-construction","garbage-collection","llvm","relocation","llc"],"text":"Title: How to avoid undesired constant folding performed by LLVM's code generator?\nTags: compiler-construction, garbage-collection, llvm, relocation, llc\nSource: Stack Overflow\n\nQuestion:\nWhat I am trying to achieve is to avoid constant-folding of some constants (which represent addresses in my code) such as the `100000000` constant below. I need this because later the JIT-compiled code might get patched, which changes the constants due to object relocation.\n\nThe code below is my best effort to avoid constant-folding (at all costs). It doesn't work. I end up with constant `100011111` in the instruction stream.\n\n`llc -O0 code.ll -print-after-all` reveals that the folding happens at `Expand ISel Pseudo-instructions` pass.\n\n```\n; ModuleID = '0'\ntarget triple = \"x86_64-unknown-linux-gnu\"\n\ndefine i64 @\"0\"() {\nBlockEntry0:\n %cell = alloca i64, align 8\n store volatile i64 0, i64* %cell, align 8\n %volatile_zero3 = load volatile i64, i64* %cell, align 8\n %base = add i64 %volatile_zero3, 100000000\n %volatile_zero4 = load volatile i64, i64* %cell, align 8\n %opaque_offset = add i64 %volatile_zero4, 11111\n %casted_base = inttoptr i64 %base to i8*\n %gep = getelementptr i8, i8* %casted_base, i64 %opaque_offset\n %as_ptr = bitcast i8* %gep to i64*\n %loaded = load i64, i64* %as_ptr, align 4\n %as_function = inttoptr i64 %loaded to i64 (i64)*\n %ret_val = tail call i64 %as_function(i64 0)\n ret i64 %ret_val\n}\n\nattributes #0 = { nounwind }\n```\n\nI realize that my problem can be solved by adding some intrinsic which at codegen level would unfold to simple `movabs reg, imm64`. But I'd like to have a temporary solution for the time being. \n\n**The question:** is it possible to make an opaque constant in llvm which doesn't get constant-folded?\n\nMy llvm version is 3.7.0svn.\n\n========================================\n\nCode:\n```text\n; ModuleID = '0'\ntarget triple = \"x86_64-unknown-linux-gnu\"\n\ndefine  i64 @\"0\"() {\nBlockEntry0:\n  %cell = alloca i64, align 8\n  store volatile i64 0, i64* %cell, align 8\n  %volatile_zero3 = load volatile i64, i64* %cell, align 8\n  %base = add i64 %volatile_zero3, 100000000\n  %volatile_zero4 = load volatile i64, i64* %cell, align 8\n  %opaque_offset = add i64 %volatile_zero4, 11111\n  %casted_base = inttoptr i64 %base to i8*\n  %gep = getelementptr i8, i8* %casted_base, i64 %opaque_offset\n  %as_ptr = bitcast i8* %gep to i64*\n  %loaded = load i64, i64* %as_ptr, align 4\n  %as_function = inttoptr i64 %loaded to i64 (i64)*\n  %ret_val = tail call i64 %as_function(i64 0)\n  ret i64 %ret_val\n}\n\nattributes #0 = { nounwind }\n```\n\n```text\n100000000\n```\n\n```text\n100011111\n```\n\n```text\nllc -O0 code.ll -print-after-all\n```\n\n```text\nExpand ISel Pseudo-instructions\n```\n\n```text\nmovabs reg, imm64\n```\n\n========================================\n\nComments:\n- If the value's going to change, does it really make sense to use a constant? Maybe use a global variable with a constant initializer instead?\n- @IsmailBadawi, good thinking. That would solve it, but performance is a concern (matter of fact it is the ultimate goal) here. Though I am not sure if those extra loads gonna make a real difference if we take LICM into account. Probably I'll try the globals/constant pool if there's no simpler way.\n- Patching the code seems error prone to me. How do you find the 1000000, and how do you know that some 1000000 is something you really want to patch vs. some artifact of code generation completely independent of your constant and used for other purposes (like a bit mask)? Then again, if you knew somehow exactly what to patch then you could just let the compiler fold constants and patch the constant folded results...\n- @ErikEidt, I remember the addresses (they are always made 64-bit) I might want to patch -- no problem here. Later, when the native code has been emitted I disassemble it (with MC disassembler), look for instructions having imm64 as an operand (AFAIK there are only 2 in the x64 instruction set) and check whether the imm is one of the constants I track. Your second observation is true: if there's a bit mask equal to one of my addresses I'm doomed. I haven't encountered bit masks this large doing my lowering to LLVM IR yet, but I see how fragile the assumption is.\n- @ErikEidt, I think, an LLVM intrinsic which simply moves const to a register and remembers the offset from the function start (like stackmap/patchpoint intrinsics do) would be ideal. No problem with bitmasks, etc. and no excessive loads. I just don't know how much time it'll take me to implement, so I am looking for a temporary solution.\n- Okay.. :) The problem with global variables is they also have to be allocated somewhere. If it's the heap, the VM's GC should take control over them. The jitted code can later be discarded, so should be the relevent globals. Though it's manageable I guess. I've discovered the `llvm.experimental.gc.relocate` intrinsic and friends. Will try to go with them with globals as a backup plan. Thanks for your help!\n- Right. I was expected that they were actually just external constants that resided somewhere, otherwise you might be able to pass them in as arguments to your function. If they're truly constants you might be able to have an optimization pass that runs at jitting time on top that replaces some \"named variables\" that you have in your program with the correct values.","metadata":{"transformedAt":"2026-08-18T18:32:17.948Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":98,"estimatedTokens":1334}}1090{"id":"stack-39494636","source":"stackoverflow","questionId":39494636,"title":"Any practical example of using LLVM Alias and IFunc capabilities?","tags":["compiler-construction","llvm-ir"],"text":"Title: Any practical example of using LLVM Alias and IFunc capabilities?\nTags: compiler-construction, llvm-ir\nSource: Stack Overflow\n\nQuestion:\nI'm creating a front end (toy language for now and maybe forever) that emits LLVM IR directly (not via C++ library).\n\nI looked but could not find real and practical examples of declaring and using either of the subject capabilities. The best I have to go on is the Language Reference.\n\nQuestion is as stated in the title.\n\n========================================\n\nComments:\n- I'm thinking about using IFunc for mocking. Replace function implementations in some tests. But so far it does not look like a too much of a great fit.","metadata":{"transformedAt":"2026-08-18T18:32:17.948Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":15,"estimatedTokens":168}}1091{"id":"stack-37232680","source":"stackoverflow","questionId":37232680,"title":"How to embed metadata in object file from GCC plugin","tags":["gcc","compiler-construction","gcc-plugins"],"text":"Title: How to embed metadata in object file from GCC plugin\nTags: gcc, compiler-construction, gcc-plugins\nSource: Stack Overflow\n\nQuestion:\nI'm trying to write a GCC plugin that does some domain-specific analysis of the programs it compiles. I'm wondering about the best way to embed the analysis results as some kind of metadata (like debug information) in the generated object files.\n\nIdeally, some metadata (in my case, text) should be embedded in each object file, the linker should retain the data from all the objects it links, and finally I should have some way to access all the metadata from the final binary using `objdump`, `readelf` or similar.\n\nMy current idea is to try to add a uniquely named global string variable to each compilation unit, by adding it to the GIMPLE AST. However, I'm wondering if there is a more \"disciplined\" way; how can plugins generate debug information or other such metadata?\n\n========================================\n\nCode:\n```text\nobjdump\n```\n\n```text\nreadelf\n```\n\n```text\n// make a char array type\ntype = build_array_type_nelts(char_type_node, size);\n\n// create the variable and set its name\nvar = add_new_static_var(type);\nname = get_identifier(varname);\nDECL_NAME(var) = name;\n\n// make sure this is a definition (otherwise GCC optimizes it away!)\nTREE_PUBLIC(var) = 1;\n\n// initialize the variable to a string value\ninitializer = build_string_literal(size, my_string);\nDECL_INITIAL(var) = initializer;\n```\n\n```text\nmy_string\n```\n\n```text\nsize\n```\n\n```text\nvarname\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.948Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":50,"estimatedTokens":378}}1092{"id":"stack-36826500","source":"stackoverflow","questionId":36826500,"title":"How to eliminate this Left Recursion for LL Parser","tags":["parsing","compiler-construction","context-free-grammar","ll-grammar"],"text":"Title: How to eliminate this Left Recursion for LL Parser\nTags: parsing, compiler-construction, context-free-grammar, ll-grammar\nSource: Stack Overflow\n\nQuestion:\nHow do you eliminate a left recursion of the following type. I can't seem to be able to apply the general rule on this particular one.\n\n```\nA -> A | a | b\n```\n\nBy using the elimination rule you get:\n\n```\nA -> aA' | bA'\nA' -> A' | epsilon\n```\n\nWhich still has left recursion.\n\nDoes this say anything about the grammar being/not being LL(1)?\n\nThank you.\n\n========================================\n\nCode:\n```text\nA -> A | a | b\n```\n\n```text\nA  -> aA' | bA'\nA' -> A'  | epsilon\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.948Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":35,"estimatedTokens":160}}1093{"id":"stack-42410302","source":"stackoverflow","questionId":42410302,"title":"Converting a parse tree to AST","tags":["parsing","compiler-construction","context-free-grammar"],"text":"Title: Converting a parse tree to AST\nTags: parsing, compiler-construction, context-free-grammar\nSource: Stack Overflow\n\nQuestion:\nLet me put the question first: Can I convert a parse tree implementing this particular grammar to an AST trivially.\n\nI was given this grammar to build a parse tree:\n\n```\nliteral := INTEGER | FLOAT | TRUE | FALSE .\n\ndesignator := IDENTIFIER { \"[\" expression0 \"]\" } .\n\nop0 := \">=\" | \"\" | \"For this particular example:\n\n```\nfunc main() : void {\n let a = 1 + 2 + 3 + 4;\n}\n```\n\nMy parser will generate (part of) the parse tree as such\n\n```\nEXPRESSION1\n EXPRESSION2\n EXPRESSION3\n LITERAL\n INTEGER(1)(lineNum:2, charPos:10)\n OP1\n ADD(lineNum:2, charPos:12)\n EXPRESSION2\n EXPRESSION3\n LITERAL\n INTEGER(2)(lineNum:2, charPos:14)\n OP1\n ADD(lineNum:2, charPos:16)\n EXPRESSION2\n EXPRESSION3\n LITERAL\n INTEGER(3)(lineNum:2, charPos:18)\n OP1\n ADD(lineNum:2, charPos:20)\n EXPRESSION2\n EXPRESSION3\n LITERAL\n INTEGER(4)(lineNum:2, charPos:22)\n```\n\nJust notice how these tree branches under EXPRESSION1 go:\n\n```\nEXPRESSION2 + EXPRESSION2 + EXPRESSION2 + EXPRESSION2\n```\n\nwhich the operator + doesn't correspond to its two operands. So it seems to me that, in the AST conversion, I can't get an AST that aids 3-address IR code generation by simply pulling up the operator to replace the non-terminal EXPRESSION1.\n\nTo achieve this goal, the grammar I would have written for this language will be like this instead\n\n```\nexpression1 := expression2 | expression1 + expression2 (1)\nexpression2 := expression3 | expression2 * expression3 (2)\nexpression3 := literal (3)\n```\n\nwhich the branches under EXPRESSION1 are only \n\n```\nEXPRESSION1 + EXPRESSION2\n```\n\nHowever, this grammar is not LL(1) since |FIRST(expression2)| = |{literal, +}| > 1. \n\nIt begs the question that (1) what would be the most elegant and trivial way to convert this parse tree? (2) is my construction of the parse tree a complete waste of time for this grammar that I should have started out coding an AST instead?\n\n========================================\n\nCode:\n```text\nliteral := INTEGER | FLOAT | TRUE | FALSE .\n\ndesignator := IDENTIFIER { \"[\" expression0 \"]\" } .\n\nop0 := \">=\" | \"<=\" | \"!=\" | \"==\" | \">\" | \"<\" .\nop1 := \"+\" | \"-\" | \"or\" .\nop2 := \"*\" | \"/\" | \"and\" .\n\nexpression0 := expression1 [ op0 expression1 ] .\nexpression1 := expression2 { op1  expression2 } .\nexpression2 := expression3 { op2 expression3 } .\nexpression3 := \"not\" expression3\n       | \"(\" expression0 \")\"\n       | designator\n       | call-expression\n       | literal .\n```\n\n```text\nfunc main() : void {\n    let a = 1 + 2 + 3 + 4;\n}\n```\n\n```text\nEXPRESSION1\n                EXPRESSION2\n                  EXPRESSION3\n                    LITERAL\n                      INTEGER(1)(lineNum:2, charPos:10)\n                OP1\n                  ADD(lineNum:2, charPos:12)\n                EXPRESSION2\n                  EXPRESSION3\n                    LITERAL\n                      INTEGER(2)(lineNum:2, charPos:14)\n                OP1\n                  ADD(lineNum:2, charPos:16)\n                EXPRESSION2\n                  EXPRESSION3\n                    LITERAL\n                      INTEGER(3)(lineNum:2, charPos:18)\n                OP1\n                  ADD(lineNum:2, charPos:20)\n                EXPRESSION2\n                  EXPRESSION3\n                    LITERAL\n                      INTEGER(4)(lineNum:2, charPos:22)\n```\n\n```text\nEXPRESSION2 + EXPRESSION2 + EXPRESSION2 + EXPRESSION2\n```\n\n```text\nexpression1 := expression2 | expression1 + expression2  (1)\nexpression2 := expression3 | expression2 * expression3  (2)\nexpression3 := literal                                  (3)\n```\n\n```text\nEXPRESSION1 + EXPRESSION2\n```\n\n```text\nADD\n     /  \\\n    1   ADD\n        /  \\  \n       2   ADD\n           / \\\n          3   4\n```\n\n========================================\n\nComments:\n- You might consider the real differences between a parse tree and an abstract syntax tree. See my answer: stackoverflow.com/questions/1888854/&hellip;\n- That seems very nice. I have heard some nice properties about GLR. Now, there is compressed CST to aid. Thanks for the information.\n- Well, this is some school assignment that I wasn't able to figure out before the deadline. There is some specific guideline I have to . I was too stupid not to realize there was problem with the expression grammar that the target AST wasn't just a \"simplified\" version of the CST. The tree structure is different. I am just getting a little pissed off.\n- That's the trouble with ASTs: they aren't isomorphic to CSTs. This means you have to define a mapping from the CST produced by the parser to the AST that somebody thinks is nice, and then you have to implement that mapping by walking over the CST and/or building AST subtrees as you do reductions. That map, being defined by \"nice\" is simply ad hoc and extra work that you have to do. With big grammars, this is a royal PIA. My sympathies. Been there, done that.\n- Does this mean sticking with LL(1) grammar is in its nature a bad design because it handles operator precedence but fails at operator associativity?\n- @Davis Targeting LL(1) is fine but quite often not practical and not absolutely necessary when you can use an advanced LL(k) parser generator like ANTLR.","metadata":{"transformedAt":"2026-08-18T18:32:17.948Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":7,"totalLines":163,"estimatedTokens":1310}}1094{"id":"stack-7324402","source":"stackoverflow","questionId":7324402,"title":"Parser for user defined infix operators","tags":["parsing","language-design","compiler-construction"],"text":"Title: Parser for user defined infix operators\nTags: parsing, language-design, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am writing an interpreter for a language where functions can be used as operators. However, the functions content will only be known at runtime.\n\nFor that I considered two solutions:\n\n- Parsing is done at runtime, using the runtime information on the function\n\n- All user-defined operators use default values for precedence and associativity.\n\nI chose the latter as I see a number of advantages in parsing separately to execution.\n\nNow it comes to implementation and I am interested to see what options there are. My initial thoughts are a shift reduce parser, but I have little experience in constructing parsers.\n\nExample:\n\n```\nLHS op RHS : LHS * RHS /* define a binary operator 'op' */\nvar : 3 /* define a variable */\nprint 5 op var /* should print 15 */\n\nLHS op RHS : LHS / RHS /* Re-define op */\nprint var op var /* Should print 1 */\n```\n\nin the last case, the parser will get from the lexer: \" `id` `id` `id` `id` \". Only at runtime do I know that the 'op' id is an operator.\n\n========================================\n\nCode:\n```text\nLHS op RHS : LHS * RHS     /* define a binary operator 'op' */\nvar : 3                    /* define a variable */\nprint 5 op var             /* should print 15 */\n\nLHS op RHS : LHS / RHS     /* Re-define op */\nprint var op var           /* Should print 1 */\n```\n\n```text\nid\n```\n\n```text\nid\n```\n\n```text\nid\n```\n\n```text\nid\n```\n\n```text\ncase\n```\n\n========================================\n\nComments:\n- Why would you need runtime information to do the parsing? Also (perhaps this answers the first question), how do you intend the use of user-defined operators to look (examples)?\n- Whether it is an operator or not is the first, as the language is a single-namespace language. I'll update the post with examples on syntax.\n- I still don't see the problem. `id id id` is not valid unless it's `LHS op RHS`, right (assuming you didn't define `print` to be a context-sensetive keyword, which would be madness)? So just parse it as such and check at runtime whether there is such an operator defined. What that operator *mean* is an entirely different problem and doesn't have to be decided at parse time (in fact, it's undecidable without running the problem).\n- In which case what kind of parser would you recommend. I have read what Ullman had to say about parsers and now I have too many options :P\n- Personally, I'd use a pratt parser (effbot.org/zone/simple-top-down-parsing.htm, journal.stuffwithstuff.com/2011/03/19/&hellip;) because I simply love them (they shine at parsing expressions, but are top-down and simple to write by hand - not that I have much experience with other parsers, but they work better than recursive descent and I'm too lazy to really get into one of the parser generators). But I guess most parsers work fine.\n- I was planning on writing the parser by hand... So it's the algorithm I'm interested in. Pratts looks interesting and I haven't seen it before so thanks\n- @delnan, your approach won't allow to define different alignment and priority rules for different operators.\n- @SK-logic: Yes, but if we allow it we're back to approach #1 as in the question (if the definitions should remain truly dynamic, that is). Clearly that's neither desirable nor very practical, and also horrible language design I'd argue. Plus, OP already stated he wants to avoid it. A static, global declaration of associativity and precedence of a specific operator token (regardless of implementation) should be easy enough to allow (especially with a pratt parser).\n- @delnan, it is not a \"horrible design\", it is clean and easy - see how Haskell is doing it, for example. An alternative approach is to have different identifier syntax for possible binary operators of different precedence, something like what OCaml is doing. And, things like Pratt are obviously extremely limited - I personally prefer Packrat, which makes it very easy to use runtime information or extend the parser in compilation time with new rules.\n- @SK-logic: The important difference (which makes it a useful idea instead of a maintainer's nightmare) is that in Haskell, associativity and precedence are set in stone at parse time. If we use the same restriction, there's nothing wrong with it and it can be implemented easily (yes, also with pratt parser - but this isn't a parsing algorithm pissing match, so let's leave it at that).\n- From what I have seen of Pratt's parser, I think it compares very well to other methods, and I am working on seeing how easy it is to modify. Initial thoughts are that it is not limited at all, but easily extensible. Furthermore, you can make it work so that it executes statements when it can OR form an AST. This leaves the option perfectly open to allow runtime definition of infix operators. However, I must emphasize this is initial tests and so that may change.\n- @Mat, take a look at the Katahdin language, to see how extensible Packrat is. I doubt you can do that with Pratt.\n- @SK-logic, I don't need it to be THAT extensible in this case. operators are the only feature I intend to let the user redefine. Anything else can have a preprocessor.\n- @delnan, please add an answer for Pratt's algorithm, as I would not have found it without you mentioning it and it is most definitely awesome. Pratt is fantastic because: A) It's easy to implement B) It works with executing while parsing or parsing to an AST, C) It's easy in asm.","metadata":{"transformedAt":"2026-08-18T18:32:17.948Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":6,"totalLines":79,"estimatedTokens":1384}}1095{"id":"stack-13336544","source":"stackoverflow","questionId":13336544,"title":"Graph coloring register allocator","tags":["compiler-construction","compiler-optimization","register-allocation"],"text":"Title: Graph coloring register allocator\nTags: compiler-construction, compiler-optimization, register-allocation\nSource: Stack Overflow\n\nQuestion:\nFor my compiler course I'm building a register allocator based on graph coloring for MIPS architecture. I'm following Muchnick's treatment on the same for my implementation.\n\nMuchnick has been a little fuzzy about how to treat arguments of function in these allocators.\n\nI've made a few assumptions and thought I'd clarify the same. \n\n- There is this step to convert to lower level IR from middle level IR. Nested function calls have not been handled. My idea is to scan the function call from right to left and lay down IRs for the innermost calls outward. This way I can use the MIPS calling convention of assigning the first few arguments to argument registers and the remaining to stack with minimum amount of spills (just 1).\n\n- Register coalescing treatment in the book is not intuitive to me since it does not address how the LIR code of moving function arguments to fixed argument registers are handled. After much deliberation I've come to the conclusion that I should not do register coalescing for argument passing moves.\n\nFeedback/thoughts on these assumptions immensely appreciated.\n\n========================================\n\nCode:\n```text\nmove rx,rx\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.948Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":23,"estimatedTokens":329}}1096{"id":"stack-33465139","source":"stackoverflow","questionId":33465139,"title":"How to track the index of variables in an interpreter","tags":["compiler-construction","interpreter"],"text":"Title: How to track the index of variables in an interpreter\nTags: compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nI'm creating an interpreter (a bytecode interpreter, so also a compiler) and I've found a problem I just can't solve. I need to store variables somewhere. Storing them in a dictionary and looking them up at runtime would be way to slow, so I'd like to store them in registers and use their indexes instead of their name.\n\nSo at compile time I give every variable an index, and create an array of registers. That's fine for monolithic scoped languages. But the language I'm creating the interpreter for has nested scopes (and function calls). So another approach could be that I have a set of global registers, and a stack of register lists for function calls. So my virtual machine would have something like:\n\n```\nRegister globalRegisters[NUMBER_OF_GLOBALS];\nStack callstack;\n```\n\nBut there's another thing. My language allows functions inside functions. Example:\n\n```\nvar x = 1;\nfunction foo() {\n y = 2;\n function bar() {\n z = 3;\n y = y - 1;\n }\n}\n```\n\nFunction bar() refers to a variable that belongs to foo(). So that means that the virtual machine would have to look at the register list under the top one on the stack. But what if bar() is recursive? What if the number of recursions are defined by user input? Then the virtual machine just wouldn't know how many stack elements does it have to go under to find the set of registers containing the value of y.\n\nWhat could be an effective solution for this problem? This is the first time I'm dealing with registers, calculations happen on a value stack.\n\n========================================\n\nTop Answer:\nI think the underlying question here is very different from the one you have written, so I've written an explanation to why I think the question is malformed and an answer to what I think the underlying question is. If I am mistaken I apologize, but bear with me a bit :-)\n\n### The problem with the question\n\n I'm creating an interpreter (a bytecode interpreter, so also a compiler)\n\nAn interpreter is not a compiler, even if it's for a low-level language - unless you meant that your program both compiles some language to some bytecode interpretation, and then interprets it. In any case, unless you're jitting code, the program that is actually running is the interpreter.\n\n Storing them in a dictionary and looking them up at runtime would be way to slow, so I'd like to store them in registers and use their indexes instead of their name.\n\nIn an interpreter, forcing target language variables into registers doesn't smell right to me. For instance, say you have a method for interpreting a specific statement which uses variables. You can pull the variables quickly since you force them into registers, but then you have too few registers for running the operations in your own method efficiently. Also, saying \"yea, I'll just store those in registers\" makes me suspect you great overestimate the number of registers available to you.\n\nI'm guessing \"registers\" here is a misnomar and you just care about some efficient way of storing and accessing locals, in the presence of nested scopes and recursion. So I think your question can really be phrased as \"I want some data structure storing locals, how do I do that in the presence of nested scopes and recursive functions?\" If I'm wrong I'm sorry, but if not:\n\n### My Answer\n\nTo answer \"I want some data structure storing locals, how do I do that in the presence of nested scopes and recursive functions?\", I think it's best to first clarify the distinction between scopes and frames, in the context of locals.\n\nA *scope* is some mapping of identifiers to local variables. Inside a scope, you know all the instances of the `x` identifier refer to the same thing (roughly). A scope is something you care about when you're parsing the input language - it's what you use to understand the semantic of the code (\"oh, the `x` the coder is incrementing is the same `x` from 2 lines ago\").\n\nA *frame* is the memory allocated (typically on the stack) when calling a function. Each local usually gets a reserved place on the frame to store its value.\n\nWhen you parse the code, dealing with scopes, you don't care about recursion (since you're not running anything, just parsing). You do care about nested scopes, but those are never unbound - since the code itself (not its execution, just the code) is always finite. The standard way to deal with locals in scopes when parsing is to keep of stack of dictionaries. Create and push a new dictionary when a scope is opened, pop it when it's closed. Whenever `x` is accessed, look for it in the dictionary at the topmost dictionary in the stack - if not there, continue to the next one, and so on.\n\nThe code you generate (or straightforward perform, in an interpreter) will then know exactly to what location each instance of `x` is referring to. And those memory locations will be allocated when creating a frame. That way you also don't care about recursion - you have mapped variables to locations in the current frame, and that is valid regardless of where that frame was called from.\n\n### Finally, a word about closures\n\nIn all the languages I can recall right now, closures work by capturing enclosing variables at *definition* time. In Java, for example, every local accessed in an inner class that belongs to an outer class will, in practice, be passed to the inner class at the moment of its creation - think of it as just another argument to the construction of the inner class. C++ is more explicit about which variables it captures, but otherwise it works the same - the lambda object just gets those variables (by value or by reference, depending on the directive) passed to it on creation. In any case the captured object is distinct from the original object after it was captured (they may be both pointers to the same place, but that does not make them the same object), so it shouldn't be hard to parse.\n\n========================================\n\nCode:\n```text\nRegister globalRegisters[NUMBER_OF_GLOBALS];\nStack<Register[]> callstack;\n```\n\n```text\nvar x = 1;\nfunction foo() {\n    y = 2;\n    function bar() {\n        z = 3;\n        y = y - 1;\n    }\n}\n```\n\n```text\nfoo\n```\n\n```text\nbar\n```\n\n```text\nbar\n```\n\n```text\nfoo\n```\n\n```text\nfoo\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n========================================\n\nComments:\n- So one solution would be that the inner function's stack frame has a pointer to it's outer function's stack frame?\n- @PeterLenkefi Yes, if you disallow returning functions (and keeping in mind that you need to save the stack pointer along with the function at the time of function creation as you may otherwise not have access to it at the time of the function call - thus the struct).\n- My language can only return lambdas, so that's not gonna be a problem. This is a neat answer, thank you!\n- To help in understanding the point @Oak makes about closures and how they capture variables: I blogged about the same topic here: orangejuiceliberationfront.com/what-a-block-really-is (I call them \"blocks\" and \"lambdas\" there, as that is what Objective-C resp. C++ call them).","metadata":{"transformedAt":"2026-08-18T18:32:17.948Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":12,"totalLines":130,"estimatedTokens":1813}}1097{"id":"stack-29797866","source":"stackoverflow","questionId":29797866,"title":"Jumps for a JIT (x86_64)","tags":["assembly","compiler-construction","x86-64","jit","machine-code"],"text":"Title: Jumps for a JIT (x86_64)\nTags: assembly, compiler-construction, x86-64, jit, machine-code\nSource: Stack Overflow\n\nQuestion:\nI'm writing a JIT compiler in C for x86_64 linux.\n\nCurrently the idea is to generate some bytecode in a buffer of executable memory (e.g. obtained with an mmap call) and jump to it with a function pointer.\n\nI'd like to be able to link multiple blocks of executable memory together such that they can jump between each other using only native instructions.\n\nIdeally, the C-level pointer to an executable block can be written into another block as an absolute jump address something like this:\n\n```\nunsigned char *code_1 = { 0xAB, 0xCD, ... };\nvoid *exec_block_1 = mmap(code1, ... );\nwrite_bytecode(code_1, code_block_1);\n...\nunsigned char *code_2 = { 0xAB, 0xCD, ... , exec_block_1, ... };\nvoid *exec_block_2 = mmap(code2, ... );\nwrite_bytecode(code_2, exec_block_2); // bytecode contains code_block_1 as a jump\n // address so that the code in the second block\n // can jump to the code in the first block\n```\n\nHowever I'm finding the limitations of x86_64 quite an obstacle here. There's no way to jump to an absolute 64-bit address in x86_64 as all available 64-bit jump operations are relative to the instruction pointer. This means that I can't use the C-pointer as a jump target for generated code.\n\nIs there a solution to this problem that will allow me to link blocks together in the manner I've described? Perhaps an x86_64 instruction that I'm not aware of?\n\n========================================\n\nTop Answer:\nIf you know the addresses of the blocks at the time when you are emitting the jump instructions, you can just check to see if the distance in bytes from the address of the jump instruction to the address of the target block fits within the 32-bit signed offset of the `jXX` family of instructions.\n\nEven if you `mmap` each block separately, chances are pretty good that you won't get two neighbouring (in the control-flow sense) blocks that are more than ±2GiB apart. That being said, there are several good reasons *not* to map each block separately like that. First of all, `mmap`'s minimum unit of allocation is (almost by definition) a page, which is probably at least 4KiB. That means that the unused space after the code for each block is wasted. Secondly, packing the basic blocks more tightly increases the utilization of the instruction cache and the chances of a shorter jump encoding being valid.\n\n Perhaps an x86_64 instruction that I'm not aware of?\n\nIncidentally, there is an instruction for loading a 64-bit immediate into `rax`. The GNU toolchain refers to it as `movabs`:\n\n```\n0000000000000000 :\n 0: 49 b8 ff ff ff ff ff movabs rax,0x7fffffffffffffff\n 7: ff ff 7f\n```\n\nSo if you really want to, you can simply load the pointer into `rax` and use a jump to register.\n\n========================================\n\nCode:\n```text\nunsigned char *code_1 = { 0xAB, 0xCD, ... };\nvoid *exec_block_1 = mmap(code1, ... );\nwrite_bytecode(code_1, code_block_1);\n...\nunsigned char *code_2 = { 0xAB, 0xCD, ... , exec_block_1, ... };\nvoid *exec_block_2 = mmap(code2, ... );\nwrite_bytecode(code_2, exec_block_2); // bytecode contains code_block_1 as a jump\n                                      // address so that the code in the second block\n                                      // can jump to the code in the first block\n```\n\n```text\n;instr              ; opcodes [op size] (comment)\n    call next           ; e8 00 00 00 00 [4] (call to get current location)\nnext:\n    pop rax             ; 58 [1]  (next label address in rax)\n    add rax, 12h        ; 48 83 c0 12 [4] (adjust rax to fall on landing label)\n    push rax            ; 50 [1]  (push adjusted value)\n    mov rax, code_block ; 48 b8 XX XX XX XX XX XX XX XX [10] (load target address)\n    push rax            ; 50 [1] (push to ret to code_block)\n    ret                 ; c3 [1] (go to code_block)\nlanding:    \n    nop\n    nop\n```\n\n```text\ne8 00 00 00 00\n```\n\n```text\nrax\n```\n\n```text\nXX\n```\n\n```text\nmov rax, code_block\n```\n\n```text\ncode block\n```\n\n```text\nret\n```\n\n```text\nlanding\n```\n\n```text\n0000000000000000 <.text>:\n   0:   49 b8 ff ff ff ff ff    movabs rax,0x7fffffffffffffff\n   7:   ff ff 7f\n```\n\n```text\njXX\n```\n\n```text\nmmap\n```\n\n```text\nmmap\n```\n\n```text\nrax\n```\n\n```text\nmovabs\n```\n\n```text\nrax\n```\n\n========================================\n\nComments:\n- Hmm, maybe you are over-estimating the need to generate more than 2 gigabytes of code. The advantage of a jitter is that you can always tell that you need to fall back to an indirect jump, like `jmp rax`.\n- @HansPassant That's a good point. At the moment my goal is just to implement the simplest thing that works, and worry about performance later.\n- Also related: Handling calls to far away intrinsic functions in a JIT/ re: allocating blocks near each other with `mmap` with a hint address, so you can use a direct `call` or `jmp rel32` encoding.\n- Thanks. doing `'mov rax, code_block'`, `'push rax'`, `'ret'` has the effect I'm looking for. Ideally I'd like something that doesn't need to touch the stack, but this will suffice for now.\n- Be warned, though: Modern Intel processors keep track of return addresses for branch target prediction. Manually pushing another address and then using `ret` to jump to it interferes with the predictor and will likely result in lower performance. See the optimization manual for more information.\n- @AlexJ136: This is almost maximally inefficient compared to `call rax` or `jmp rax`. x86-64 has register-indirect call and jmp, use them! push/ret is much worse because it breaks the Return-Address predictor Stack by mismatching call and ret. (The `call next` is a special case and doesn't go on the RAS in most CPUs blog.stuffedcow.net/2018/04/ras-microbenchmarks/#call0, so this leaves it unbalanced for future returns *as well as* forcing this `ret` to mispredict.) Using `call`/`pop` to find out your own address is also insane in x86-64; that's why we have RIP-relative LEA.\n- iTLB effects would be a more relevant concern, unless you're really only talking about tiny basic blocks. i-cache locality is in 64-byte chunks (or 128-byte considering L2 adjacent-line prefetch), and short-jmp is `rel8` -128..+127 byte displacements. But yes, movabs / `jmp *rax` is the obvious choice. See also Handling calls to far away intrinsic functions in a JIT","metadata":{"transformedAt":"2026-08-18T18:32:17.948Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":146,"estimatedTokens":1593}}1098{"id":"stack-24257437","source":"stackoverflow","questionId":24257437,"title":"How do I detect and correct usless try catch blocks?","tags":["c#","compiler-construction","roslyn"],"text":"Title: How do I detect and correct usless try catch blocks?\nTags: c#, compiler-construction, roslyn\nSource: Stack Overflow\n\nQuestion:\nI've started using the .Net Complier Platform (Roslyn) to assist with enforcing coding standards.\n\nOne issue I'm struggling with is discovering and catching useless `try...catch` blocks.\n\nFor example:\n\n```\n// Would like to have this detected and offer to remove the try...catch\ntry\n{\n // Do some work\n}\ncatch(Exception ex)\n{\n throw ex;\n}\n```\n\nIt would be good to also detect the fact that the code is using `throw ex;` rather than just `throw;` such as:\n\n```\ntry\n{\n // So some work\n}\ncatch(Exception ex)\n{\n // Log the error or anything to manage the exception\n throw ex; // <-- how to detect and offer a fix for this\n}\n```\n\n========================================\n\nCode:\n```text\n// Would like to have this detected and offer to remove the try...catch\ntry\n{\n    // Do some work\n}\ncatch(Exception ex)\n{\n    throw ex;\n}\n```\n\n```text\ntry\n{\n    // So some work\n}\ncatch(Exception ex)\n{\n    // Log the error or anything to manage the exception\n    throw ex;  // <-- how to detect and offer a fix for this\n}\n```\n\n```text\ntry...catch\n```\n\n```text\nthrow ex;\n```\n\n```text\nthrow;\n```\n\n```text\nvar tree = CSharpSyntaxTree.ParseText(@\"\npublic class MyClass {\npublic void Method()\n{\n    try { }\n    catch(Exception e)\n    {\n        //useless\n        throw e;\n    }\n    try {  }\n    catch(Exception e)\n    {\n        //Some work\n        int aVariable = 4;\n        throw e;\n    }\n}\n}\n\");\n\n//Finds all catch clauses\nvar catchClauses = tree.GetRoot().DescendantNodesAndSelf().OfType<CatchClauseSyntax>();\n//Look at the catch blocks\nvar catchBlocks = catchClauses.Select(n => n.DescendantNodes().OfType<BlockSyntax>().First());\n//Filter out the clauses where statements all are only throw statements\nvar uselessClauses = catchBlocks.Where(n => n.Statements.All(m => m is ThrowStatementSyntax));\n```\n\n```text\nCatchClauseSyntax\n```\n\n```text\nStatementSyntax\n```\n\n```text\nThrowStatementSyntax\n```\n\n========================================\n\nComments:\n- Any static code analyzer can help with that stackoverflow.com/questions/38635/&hellip;\n- Jack, thanks for the comment and link however I want to use Roslyn for this as it will detect these issues directly, i.e. I don't want to have to run a build or any other tools to find these issues. I currently use Code Analysis and Gendarme, which work fine, but are not providing the instant feedback I'm looking for.\n- What specifically is tripping you up?\n- I can't figure out how to detect the code sections I mentioned in my question and then correct them. So, for example, how do I find a `try catch` that does nothing more than throw the caught exception and then, apply the fix, i.e. keep whatever `&#47;&#47;So some work` is but remove the suronding block.\n- For those of you that have put this as 'on hold as unclear' maybe it would be very helpful to SO newbies like me to explain why you have done so? I've read my question back a few times and I can't see how I can make it any more explicit. Even given example of what I'm after. Please help me to provide a better question.","metadata":{"transformedAt":"2026-08-18T18:32:17.948Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":9,"totalLines":126,"estimatedTokens":785}}1099{"id":"stack-17766992","source":"stackoverflow","questionId":17766992,"title":"Compiler construction: Handle references to unordered symbols","tags":["c++","parsing","compiler-construction","symbols","symbol-table"],"text":"Title: Compiler construction: Handle references to unordered symbols\nTags: c++, parsing, compiler-construction, symbols, symbol-table\nSource: Stack Overflow\n\nQuestion:\nI've got the dragonbook but it doesn't seem to handle that topic...\n\nIn the most modern languages it's possible to use certain variables even if their appearance in the code is unordered. \n\n**Example**\n\n```\nclass Foo {\n void bar() {\n plonk = 42;\n }\n int plonk;\n}\n```\n\nIt doesn't matter that the variable `plonk` is declared after the function. \n\n**Question**\n\nIs there any best practice/useful pattern how to implement this? There are two approaches which cam to my mind:\n\nWhile parsing add dummy symbols for unseen symbols. When the declaration is parsed those dummies get replaced by their real symbols. After the parsing we can check if there are dummies left and if so output an error.\n\nDon't do any symbol stuff while parsing but only create the AST. After parsing step through the AST and depending on the node add symbols. For e.g. a class node add symbols of the children and process them after. For e.g. statement blocks step through children and add symbols immediatly before the child is processed.\n\nI would expect approach 1. is easier and also more useful for stuff like \"importing other compilation units\". \n\nEdit:\n\nA problem i see with approach 1 is that there needs some kind of handling for ordered symbols. E.g. withing a function it's not possible to use a local symbol before it is used.\n\n========================================\n\nCode:\n```text\nclass Foo {\n    void bar() {\n        plonk = 42;\n    }\n    int plonk;\n}\n```\n\n```text\nplonk\n```\n\n```text\nfunction outer() {\n  return foo();\n\n  function inner() {\n    return foo();\n\n    function foo() {\n      return \"inner's foo\";\n    }\n  }\n\n  function foo() {\n     return \"outer's foo\";\n  }\n}\n```\n\n```text\nfoo\n```\n\n```text\nfoo\n```\n\n```text\n(x)(y)\n```\n\n```text\nx\n```\n\n```text\nx\n```\n\n```text\n(x)(y)\n```\n\n```text\ntypedef\n```\n\n========================================\n\nComments:\n- Build an AST, that's much cleaner and gives you a lot more flexibility.\n- @H2CO3: But that would make it necessary that a compilation unit which is imported is already compiled - so i know all symbols right?\n- I don't exactly . You don't need compilation - you only need the declarations. Compilation is not linkage!\n- Okay my fault - replace compilation with parsing. Those error are (as far as my knowledge goes) normally not handled by the linker but by the compiler (libraries excluded).\n- Ah, I see what you mean! Yes, if you build an AST, you have to parse the entire compilation unit. And no, most compilers don't work in one pass (there are a few exceptions, e. g. Lua's compiler). They do the entire parsing, where only syntax errors are checked, then the next phase is walking the AST, and checking for more semantics-related errors (for example, undeclared variables).\n- Hm yes makes kind of sense. Do you know some (toy-) compiler where this strategy is implemented? There are still a lot of open questions for me.\n- Huh, well, not off the top of my head. However, I am currently in the process of developing a simple scripting language. The source code will be on my GitHub shortly, you might find it interesting.\n- okay - would be nice if you could the link here if it's ready - could be useful for others too :)\n- Interesting example. There shouldn't be any completly different semantics based on the symbol-types (at least I'm trying to design the language that way). Do you have any compilers in mind which implement that strategy?\n- @Daniel: most toy languages, and many real ones, require declaration before use. Of those which don't, many (like some javascript implementations) do lookup on use, which is really inefficient. Compilers which allow declaration after use tend to be fairly complicated. However, any good compiler tutorial should explain how to build a symbol table, and the algorithm I suggest in my answer is pretty straight-forward. Good luck.","metadata":{"transformedAt":"2026-08-18T18:32:17.948Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":10,"totalLines":111,"estimatedTokens":997}}1100{"id":"stack-16607689","source":"stackoverflow","questionId":16607689,"title":"Make a Scala interpreter oblivious between interpret calls","tags":["scala","compiler-construction","classloader","interpreter"],"text":"Title: Make a Scala interpreter oblivious between interpret calls\nTags: scala, compiler-construction, classloader, interpreter\nSource: Stack Overflow\n\nQuestion:\nIs it possible to configure a Scala interpreter (`tools.nsc.IMain`) so that it **\"forgets\" the previously executed code, whenever I run the next `interpret()` call?**\n\nNormally when it compiles the sources, it wraps them in nested objects, so all the previously defined variables, functions and bindings are available.\n\nIt would suffice to not generate the nested objects (or to throw them away), although I would prefer a solution which would even remove the previously compiled classes from the class loader again.\n\nIs there a setting, or a method, or something I can overwrite, or an alternative to `IMain` that would accomplish this? I need to be able to still access the resulting objects / classes from the host VM.\n\nBasically I want to isolate subsequent `interpret()` calls without something as heavy weight as creating a new `IMain` for each iteration.\n\n========================================\n\nCode:\n```text\ntools.nsc.IMain\n```\n\n```text\ninterpret()\n```\n\n```text\nIMain\n```\n\n```text\ninterpret()\n```\n\n```text\nIMain\n```\n\n```text\nclearExecutionWrapper()\nresetClassLoader()\nresetAllCreators()\nprevRequests.clear()\nreferencedNameMap.clear()\ndefinedNameMap.clear()\nvirtualDirectory.clear()\n```\n\n```text\nimport tools.nsc._\nimport interpreter.IMain\n\nobject Test {\n  private final class Intp(cset: nsc.Settings)\n    extends IMain(cset, new NewLinePrintWriter(new ConsoleWriter, autoFlush = true)) {\n\n    override protected def parentClassLoader = Test.getClass.getClassLoader\n  }\n\n  object Foo {\n    def bar() { println(\"BAR\" )}\n  }\n\n  def run() {\n    val cset = new nsc.Settings()\n    cset.classpath.value += java.io.File.pathSeparator + sys.props(\"java.class.path\")\n    val i = new Intp(cset)\n    i.initializeSynchronous()\n    i.bind[Foo.type](\"foo\", Foo)\n    val res0 = i.interpret(\"foo.bar(); val x = 33\")\n    println(s\"res0: $res0\")\n    i.reset()\n    val res1 = i.interpret(\"println(x)\")\n    println(s\"res1: $res1\")\n    i.reset()\n    val res2 = i.interpret(\"foo.bar()\")\n    println(s\"res2: $res2\")\n  }\n}\n```\n\n```text\nfoo: Test.Foo.type = Test$Foo$@8bf223\nBAR\nx: Int = 33\nres0: Success\n<console>:8: error: not found: value x\n              println(x)\n                      ^\nres1: Error\n<console>:8: error: not found: value foo\n              foo.bar()\n              ^\nres2: Error\n```\n\n```text\nfor(j <- 0 until 3) {\n  val user  = \"foo.bar()\"\n  val synth =  \"\"\"import Test.{Foo => foo}\n               \"\"\".stripMargin + user\n  val res = i.interpret(synth)\n  println(s\"res$j: $res\")\n  i.reset()\n}\n```\n\n```text\nreset()\n```\n\n```text\nFoo\n```\n\n```text\nx\n```\n\n```text\nfoo\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":13,"totalLines":122,"estimatedTokens":683}}1101{"id":"stack-2969973","source":"stackoverflow","questionId":2969973,"title":"PHP Interpreter/Compiler","tags":["php","compiler-construction","interpreter"],"text":"Title: PHP Interpreter/Compiler\nTags: php, compiler-construction, interpreter\nSource: Stack Overflow\n\nQuestion:\nI am trying to understand how the php compiler/interpretor works. \n\nI tried to download the php source code and tried to understand how it works. I was not able to find proper documentation. WOuld be great if someone could throw light on th modules that make the php compiler and also how the apache server uses the php compiler..\n\n========================================\n\nCode:\n```text\nif ((ret = EX(opline)->handler(execute_data TSRMLS_CC)) > 0)\n```\n\n```text\nzend_execute_scripts()\n```\n\n========================================\n\nComments:\n- Well, Apache can communicate with PHP a number of ways, the most common being mod_php and FastCGI. Do you have experience with compilers/interpreters? If not, then I doubt you'll be able to just dive in and understand one as complicated as this.\n- You'll want to start by studying the basics of compilers/interpreters (parsers, lexical analyzers, tokens, etc...).\n- @ webbiedave Thanks.. I want to understand the control flow and not the way the php code is compiled. Hope I am clear. I want to understand as Xiong says the way the apache uses php compiler. Were do i get started??\n- In Rails, the cleanest approach depends on whether you want to render a 404 page or actually raise a routing error. For a controller action, you can use: `ruby raise ActionController::RoutingError, \"Not Found\"` This lets Rails handle the request as a standard 404 response. If you simply want to render the 404 page without raising an exception, you can also use: `ruby render file: Rails.root.join(\"public&#47;404.html\"), status: :not_found, layout: false` For most cases, raising `ActionController::RoutingError` is preferable because it follows Rails' normal error-handling behavior.","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":27,"estimatedTokens":457}}1102{"id":"stack-19464987","source":"stackoverflow","questionId":19464987,"title":"Object files, linkers, archives, etc: where can I find an entry-level explanation for all that?","tags":["c","gcc","compiler-construction"],"text":"Title: Object files, linkers, archives, etc: where can I find an entry-level explanation for all that?\nTags: c, gcc, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI consider myself a reasonably experienced programmer; however, I am to this day completely unable to explain what an object file is, or what it contains. I have no idea why GCC needs the `-fPIC` option to create a shared library. I don't know what a linker *really* is, or how it works. I have no idea what `ranlib`, `libtool`, `nm`, etc really do. I know how to disassemble a binary file but I'm not sure how to interpret what I see.\n\nIn short, like many programmers my understanding of the compilation process is extremely limited; the documentation that's available seems to me to be geared towards people already in the know, and so are the manpages.\n\nGoogle wasn't too helpful either, and neither was Wikipedia. Therefore, **is there an entry-level explanation of the concepts underlying the translation of a program text into an executable binary?**\n\n========================================\n\nCode:\n```text\n-fPIC\n```\n\n```text\nranlib\n```\n\n```text\nlibtool\n```\n\n```text\nnm\n```\n\n========================================\n\nComments:\n- This is a pretty wide question. For example, you say \"I know how to disassemble a binary file but I'm not sure how to interpret what I see.\" The way to resolve this is for you to learn assembly language, and how the assembly language maps to the bytes stored in the file. That's not quick and easy to explain, yet it is merely a facet required to fully answer this question. So, I think it would be unreasonable for there to exist an entry-level explanation for your entire question. It looks like you might enjoy learning assembly, though.\n- I agree; I've found his book to be extremely useful in understanding the details of all the steps post-compilation. Writing an ELF (or other object format) parser is on my list of background projects because of reading his book.","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":35,"estimatedTokens":495}}1103{"id":"stack-24275952","source":"stackoverflow","questionId":24275952,"title":"Recursive in BNF grammar","tags":["c","recursion","compiler-construction","bnf","recursive-descent"],"text":"Title: Recursive in BNF grammar\nTags: c, recursion, compiler-construction, bnf, recursive-descent\nSource: Stack Overflow\n\nQuestion:\nWell, I'm not sure how I should write a function using recursive descent parse to parse a grammer like the below. Actually, I'm not sure if I was doing right it...\n\nBNF:\n\n```\nA : B | A '!'\n B : '[' ']'\n```\n\npseudo-code:\n\n```\nf()\n{\n if(tok is B) \n parse_b();\n return somethingB\n else if(????) how will I know if it's start of A or I don't need to?\n x = f();\n parse_c();\n return somethingA\n}\n```\n\nI was doing this (no check to determine if it's an A but I feel there's something wrong with it):\n\n```\nf()\n{\n if(tok is B) \n parse_b();\n return somethingB\n else\n x = f();\n parse_c();\n return somethingA\n}\n```\n\n========================================\n\nCode:\n```text\nA : B | A '!'\n B : '[' ']'\n```\n\n```text\nf()\n{\n   if(tok is B) \n      parse_b();\n      return somethingB\n   else if(????) how will I know if it's start of A or I don't need to?\n      x = f();\n      parse_c();\n      return somethingA\n}\n```\n\n```text\nf()\n{\n   if(tok is B) \n      parse_b();\n      return somethingB\n   else\n      x = f();\n      parse_c();\n      return somethingA\n}\n```\n\n========================================\n\nComments:\n- stackoverflow.com/questions/16165352/&hellip;\n- It is not clear which of your symbols are tokens and which are non-terminals. If only B and C are tokens, then you need to either refactor the grammar or use more lookahead than you do.\n- didierc: I will check out this. @n.m.: is `A : B | A '!'` more clear?\n- For details on how to build a recursive descent parser, see stackoverflow.com/a/2336769/120163\n- ... or restructure your parser. Write down several strings that belong to the language and see how your function would process them.\n- Hm. Is B a token? If not, where are its rules? If yes... why are you calling it B?\n- Take a look at Pratt parsing: journal.stuffwithstuff.com/2011/03/19/&hellip;\n- I added B rules. I will check out this links\n- I don't understand how the restructure would be performed... could you give an example from this grammar? `foo: '(' ')' | foo '[' ']'` (now I will really check the links)\n- @IraBaxter: You answer solved my question. In special, how to deal with `L = A | L A ;` (exactly what I tried put in my question). If you post as answer here I will accept. Thanks!!","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":3,"totalLines":91,"estimatedTokens":584}}1104{"id":"stack-6550004","source":"stackoverflow","questionId":6550004,"title":"Examples of monadic effects inside a rewrite function in Hoopl?","tags":["haskell","compiler-construction","monads","dataflow","hoopl"],"text":"Title: Examples of monadic effects inside a rewrite function in Hoopl?\nTags: haskell, compiler-construction, monads, dataflow, hoopl\nSource: Stack Overflow\n\nQuestion:\nThe type of (forward) rewriting functions in Hoopl is given by the `mkFRewrite` function:\n\n```\nmkFRewrite :: (FuelMonad m) => \n (forall e x.\n n e x\n -> f\n -> m (Maybe (hoopl-3.8.6.1:Compiler.Hoopl.Dataflow.Graph n e x)))\n -> FwdRewrite m n f\n```\n\nThe `m` type implies that I can use monadic effects while rewriting. The paper \"Hoopl: A Modular, Reusable Library for Dataflow Analysis and Transformation\" says the same in Section 4.3, \"The rewrite function and the client's monad.\" \n\nCan anyone give me an example of a rewrite function that has **non-Hoopl** monadic effects embedded inside it? For example, a rewriter that uses a State monad or does some IO.\n\n========================================\n\nCode:\n```text\nmkFRewrite :: (FuelMonad m) => \n   (forall e x.\n      n e x\n      -> f\n      -> m (Maybe (hoopl-3.8.6.1:Compiler.Hoopl.Dataflow.Graph n e x)))\n   -> FwdRewrite m n f\n```\n\n```text\nmkFRewrite\n```\n\n```text\nm\n```\n\n```text\nanalyzeAndRewriteFwd ::\n  forall m n f e x entries.\n    (CheckpointMonad m,\n     NonLocal n,\n     LabelsPtr entries) =>\n  FwdPass m n f ->\n  MaybeC e entries ->\n  Graph n e x ->\n  Fact e f ->\n  m (Graph n e x, FactBase f, MaybeO x f)\n```\n\n```text\n{-# LANGUAGE TypeSynonymInstances #-}\n{-# LANGUAGE TypeFamilies #-}\n\nimport Compiler.Hoopl\nimport Control.Monad.State\n\ntype StateFuel s a = CheckingFuelMonad (State s) a\n\ninstance CheckpointMonad (State s) where\n    type Checkpoint (State s) = s\n    checkpoint = get\n    restart = put\n```\n\n```text\nFwdRewrite m n f\n```\n\n```text\nm\n```\n\n```text\nm\n```\n\n```text\nCheckpointMonad\n```\n\n```text\nm\n```\n\n```text\nSimplUniqMonad\n```\n\n========================================\n\nComments:\n- As discussed in the previous question, FuelMonad is opaque: stackoverflow.com/questions/6495320/&hellip; -- I think at this point its worth writing the authors of Hoopl directly. I suspect that they hid more than they intended, and so restricted more functionality than thy realized.\n- That was my suspicion as well.\n- The first listed author (and package maintainer) for Hoopl has a significant presence on SO, so I'm actually surprised he hasn't responded to these questions about it. Though it does look like he's been less active recently...\n- Thanks for answering, but I want to see non-Hoopl monadic effects inside the rewrite function. I think the best your example can show is using `checkpoint` and `restart,` both Hoopl-provided functions.\n- Justin: I still don't understand: are you looking for situations in which non-Hoopl monadic actions are appropriate? (Here's a super easy one: unique name generation) or how to actually, technically speaking, do it (which is pretty trivial, because the output of the rewrite function is a *monadic action*). Note that using the checkpoint functions is a bad idea, since Hoopl uses those to preserve determinacy while calculating fixpoints.\n- I would like to see how to do it. I think you can't implement any because the `FuelMonad` class' members are hidden, but I would **love** to be proved wrong. If you know of code examples, please point them out!\n- Looks like you are right. You can, technically speaking, put your own monad to be used, but you can't access any of its special features, since the only two instances of FuelMonad have no MonadTrans instance.","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":11,"totalLines":102,"estimatedTokens":860}}1105{"id":"stack-39692036","source":"stackoverflow","questionId":39692036,"title":"How do compilers manage their memory footprint?","tags":["compiler-construction","theory"],"text":"Title: How do compilers manage their memory footprint?\nTags: compiler-construction, theory\nSource: Stack Overflow\n\nQuestion:\nCompilers are able to compile huge quantities of source code. This giant code is translated in AST during the compilation phase. \nI imagine that if the code is huge, this AST will become huge as well.\n\nCan we assume that the compiler will never run out of memory on modern computer by building the AST and keeping it in memory ?\n\n========================================\n\nTop Answer:\nIt is true that the memory requirements of compilers usually grow with the input size.\n\nCompilers are no different than any other program whose memory requirement depends on the input size: You just assume that there is enough, and die gracefully otherwise.\n\nSounds too easy? Well, you can't fight against math. If one really needs that memory, there is either enough or you're out of luck. It's that simple.\n\n========================================\n\nComments:\n- Most source files are small.\n- Linkers don't need a lot of memory. Compilers do, but they can be split into passes.\n- @melpomene You'd be surprised. I used to have a compiler (Watcom) that printed the number of lines compiled, and it was typically a few hundred for the .c or .cpp file and many thousands for the .h/.hpp files.\n- A modern computer has gigabytes of RAM. Even if each byte of source code generates a separate AST node with over 1000 bytes of overhead (completely unrealistic), you could still fit the AST of a multi-megabyte source file in RAM. When was the last time you saw a source file that's a megabyte large? The largest I ever saw is `X86ISelLowering.cpp` from LLVM: about 1.2 MB or 32 thousand lines of code, plus whatever the `#include`s expand to, and that's a truly *extreme* example.","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":26,"estimatedTokens":446}}1106{"id":"stack-40634291","source":"stackoverflow","questionId":40634291,"title":"bisonc++ inheritance with polymorphic","tags":["c++","parsing","compiler-construction","abstract-syntax-tree","bisonc++"],"text":"Title: bisonc++ inheritance with polymorphic\nTags: c++, parsing, compiler-construction, abstract-syntax-tree, bisonc++\nSource: Stack Overflow\n\nQuestion:\nI'm trying to construct an AST from a bison grammar. Bison generates the parser correctly but when I try to parse an example code with some math operations the following error its printed:\n\n```\n[Fatal] calling `.get()', but Tag INT is encountered.\n```\n\nAfter debugging I notice that the issue is in the `expr` non-terminal with the following production:\n\n```\nexpr:\n ...\n | operator\n {\n $$ = $1;\n }\n```\n\nAnd `operator` has the following production itself:\n\n```\noperator:\n ...\n| INTEGER\n {\n $$ = new ast::expression::IntASTNode(std::stoi(d_scanner.matched()));\n }\n```\n\nI'm using polymorphic semantic types, `expr` and `operator` are tagged with `EXPR` that respond to `ExprASTNode` type witch is the base class for `IntASTNode` with the tag `INT`. I'm guessing that bison is getting the type from the tag and checking the tags before making any cast. Is there any way that I can resolve this?\n\n========================================\n\nCode:\n```text\n[Fatal] calling `.get<Tag__::EXPR>()', but Tag INT is encountered.\n```\n\n```text\nexpr:\n      ...\n    | operator\n      {\n         $$ = $1;\n      }\n```\n\n```text\noperator:\n   ...\n| INTEGER\n  {\n    $$ = new ast::expression::IntASTNode(std::stoi(d_scanner.matched()));\n  }\n```\n\n```text\nexpr\n```\n\n```text\noperator\n```\n\n```text\nexpr\n```\n\n```text\noperator\n```\n\n```text\nEXPR\n```\n\n```text\nExprASTNode\n```\n\n```text\nIntASTNode\n```\n\n```text\nINT\n```\n\n```text\noperator:\n     ...\n        | INTEGER\n          {\n            $$(ast::expression::IntASTNode(std::stoi(d_scanner.matched())));\n          }\n```\n\n```text\nbisonc++\n```\n\n```text\nstatic_cast\n```\n\n```text\n$$\n```\n\n```text\n$$(expr)\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":16,"totalLines":116,"estimatedTokens":443}}1107{"id":"stack-74671309","source":"stackoverflow","questionId":74671309,"title":"How does the C# compiler resolve types before applying a binary operator?","tags":["c#","compiler-construction","roslyn","expression-trees"],"text":"Title: How does the C# compiler resolve types before applying a binary operator?\nTags: c#, compiler-construction, roslyn, expression-trees\nSource: Stack Overflow\n\nQuestion:\nI'm working on a typed scripting language backed by C# Expression Trees. I'm stuck on one issue around proper type conversion with binary operators. Here is an example of the behavior I'm trying to mimic: (The conversion rules should be the same C#'s compiler)\n\n```\nvar value = \"someString\" + 10; // yields a string\nvar value = 5 + \"someString\"; // also yields a string, I don't know why\nvar x = 10f + 10; // yields a float\nvar y = 10 + 10f; // also yields a float, I don't know why\n```\n\nHow does the C# compiler know to call ToString() on the integer in the first line and to convert the integers to floats in both directions when adding with a float? Are these conversion rules hard coded?\n\nMy compiler basically works like this now for binary operators:\n\n```\nExpression Visit(Type tryToConvertTo, ASTNode node) {\n // details don't matter. If tryToConvertTo is not null\n // the resulting expression is cast to that type if not already of that type\n}\n\n// very simplified but this is the gist of it\nExpression VisitBinaryOperator(Operator operator) {\n Expression lhs = Visit(null, operator.lhs);\n Expression rhs = Visit(lhs, operator.rhs); // wrong, but mostly works unless we hit one of the example cases or something similar\n switch(operator.opType) {\n case OperatorType.Add: {\n return Expression.Add(lhs, rhs);\n }\n // other operators / error handling etc omitted\n }\n}\n```\n\nI know always accepting the left hand side's type is wrong, but I have no idea what the proper approach to resolving the example expressions might be other than hard coding the rules for primitive types.\n\nIf anyone can point me in the right direction I'd be very grateful!\n\n========================================\n\nCode:\n```text\nvar value = \"someString\" + 10; // yields a string\nvar value = 5 + \"someString\"; // also yields a string, I don't know why\nvar x = 10f + 10; // yields a float\nvar y = 10 + 10f; // also yields a float, I don't know why\n```\n\n```text\nExpression Visit(Type tryToConvertTo, ASTNode node) {\n    // details don't matter. If tryToConvertTo is not null\n    // the resulting expression is cast to that type if not already of that type\n}\n\n// very simplified but this is the gist of it\nExpression VisitBinaryOperator(Operator operator) {\n    Expression lhs = Visit(null, operator.lhs);\n    Expression rhs = Visit(lhs, operator.rhs); // wrong, but mostly works unless we hit one of the example cases or something similar\n    switch(operator.opType) {\n        case OperatorType.Add: {\n            return Expression.Add(lhs, rhs);\n        }\n        // other operators / error handling etc omitted\n    }\n}\n```\n\n```text\n+\n```\n\n```text\nstring\n```\n\n```text\nint\n```\n\n```text\nString concatenation\n```\n\n```text\n+\n```\n\n```text\nnull\n```\n\n```text\nstring\n```\n\n```text\nToString\n```\n\n```text\nobject\n```\n\n```text\nToString\n```\n\n```text\nnull\n```\n\n```text\n+\n```\n\n```text\nfloat\n```\n\n```text\nint\n```\n\n```text\nint\n```\n\n```text\nfloat\n```\n\n========================================\n\nComments:\n- stackoverflow.com/a/3398629/425871","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":18,"totalLines":138,"estimatedTokens":793}}1108{"id":"stack-51299651","source":"stackoverflow","questionId":51299651,"title":"How can a symbol table be designed to support function overloading?","tags":["c++","data-structures","compiler-construction"],"text":"Title: How can a symbol table be designed to support function overloading?\nTags: c++, data-structures, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI'm creating a compiler and really struggling with the semantic analysis phase. I'm not sure how to deal with function overloading in a symbol table. I can't seem to find any resources that describe this particular problem. I think that name mangling must be used somewhere and I'm pretty sure that types from the AST should be converted to strings.\n\nMultiple functions with the same name are allowed to be declared in the same scope as long as each declaration has a different set of parameters. The following snippet is an example in my language (it's quite similar to Swift).\n\n```\nfunc add(a: Int, b: Int) {\n return a + b;\n}\nfunc add(a: Float, b: Float) {\n return a + b;\n}\n```\n\nI don't know how to store functions in the symbol table. This is part of my symbol table data structure.\n\n```\nstruct Symbol {};\n\nstruct Var final : Symbol {\n std::string type;\n};\n\nusing FuncParams = std::vector;\nstruct Func final : Symbol {\n std::string ret;\n FuncParams params;\n};\n\nusing Table = std::unordered_map>;\nstruct Scope {\n Table table;\n Scope *parent = nullptr;\n};\nusing Scopes = std::vector>;\n```\n\nI could use a `std::unordered_multimap` and store the function name `add` as the key and store the names of the parameters in the symbol object. I could use a `std::unordered_map` and store the function name decorated with the parameters `add_Int_Int` as the key and store the names of the parameters in the symbol object as well.\n\nAlso, should `Symbol` be a base class or should I put all kinds of symbols in the one `Symbol` object? I've seen many examples of using an `enum` to differentiate between functions, variables and type declarations but a function stores return type and parameter types. Should I use a tagged union?\n\nI feel like there's a smart and simple way of solving this but I just can't find it.\n\n**Update:**\n\nI've taken a suggestion from @NeilButterworth and used unmangled function names as the symbol keys and this seems to be the way to go (but what do I know!). Answers to my other questions or some advice on this topic would be appreciated.\n\n========================================\n\nCode:\n```swift\nfunc add(a: Int, b: Int) {\n  return a + b;\n}\nfunc add(a: Float, b: Float) {\n  return a + b;\n}\n```\n\n```cpp\nstruct Symbol {};\n\nstruct Var final : Symbol {\n  std::string type;\n};\n\nusing FuncParams = std::vector<std::string>;\nstruct Func final : Symbol {\n  std::string ret;\n  FuncParams params;\n};\n\nusing Table = std::unordered_map<std::string, std::unique_ptr<Symbol>>;\nstruct Scope {\n  Table table;\n  Scope *parent = nullptr;\n};\nusing Scopes = std::vector<std::unique_ptr<Scope>>;\n```\n\n```text\nstd::unordered_multimap\n```\n\n```text\nadd\n```\n\n```text\nstd::unordered_map\n```\n\n```text\nadd_Int_Int\n```\n\n```text\nSymbol\n```\n\n```text\nSymbol\n```\n\n```text\nenum\n```\n\n```text\nisTypeCompatible(actualType,formalType)\n```\n\n```text\nf(5)\n```\n\n```text\nf(int)\n```\n\n```text\ng(5)\n```\n\n========================================\n\nComments:\n- Do you know how C++ does it? There might be other ways, but the C++ *name mangling* is relatively simple. There are other ways as well, and there are probably quite a few research papers (both freely available and paid for) about this issue.\n- @Someprogrammerdude Could you elaborate? Consider this function `func f(n: Float) {}` and this call: `f(5)`. If I lookup `f_Int` in the symbol table at the call site, I won't find anything. I want to find `f_Float` and then look for conversions from `Int` to `Float`.\n- You don't want name mangling here - look up the named functions without respect to parameters, and then walk the list looking for ones that match parameters or for which there is a conversion\n- @NeilButterworth I was beginning to come to that conclusion myself. Thank you for your input!","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":13,"totalLines":135,"estimatedTokens":974}}1109{"id":"stack-5900740","source":"stackoverflow","questionId":5900740,"title":"Conversion system for variants","tags":["compiler-construction","casting","language-design","compiler-theory","variant"],"text":"Title: Conversion system for variants\nTags: compiler-construction, casting, language-design, compiler-theory, variant\nSource: Stack Overflow\n\nQuestion:\nI have written a variant class, which will be used as the main type in a dynamic language, that will ultimately allow 256 different types of value (header is an unsigned byte, only 20 are actually used). I now want to implement casting/converting between types.\n\nMy initial thought was a lookup table, but the shear amount of memory that would need makes it impractical to implement.\n\nWhat are the alternatives? Right now I am considering a further three methods from research and suggestions from other people:\n\n- Group the types into larger subsets, such as numeric or collection or other.\n\n- Make a conversion interface that has CanCast(from, to) and Cast(Variant) methods and allow classes that implement that interface to be added to a list, that can then be checked to see if any of the conversion classes can do the cast.\n\n- Similar to (1) but make several master types, and casting is a two step process from the original type to the master type and then again to the final type.\n\nWhat would be the best system?\n\nEdit: I have added the bounty as I am still unsure on the best system, the current answer is very good, and definitely got my +1 but there must be people out there who have done this and can say what the best method is.\n\n========================================\n\nTop Answer:\nDone something similar.\n\nYou could add another byte to the \"header\", indicating the type its really storing.\n\nExample in a C-style programming language:\n\n```\ntypedef\nenum VariantInternalType {\n vtUnassigned = 0;\n vtByte = 1;\n vtCharPtr = 2; // real data\ntypedef\nstruct VariantHeader {\n void* Reserved; // hides real data\ntypedef\n byte[sizeof(VariantHeader)] Variant;\n\n// allocates & assign a byte data type to a variant\nVariant ByteToVar(byte value)\n{\n VariantHeader MyVariantHeader;\n Variant MyVariant;\n\n MyVariantHeader.VariantInternalType = VariantInternalType.vtByte;\n MyVariantHeader.Reserved = value; \n\n memcpy (&MyVariant, &MyVariantHeader, sizeof(Variant));\n\n return myVariant;\n}\n\n// allocates & assign a char array data type to a variant\nVariant CharPtrToVar(char* value)\n{\n VariantHeader MyVariantHeader;\n Variant MyVariant;\n\n MyVariantHeader.VariantInternalType = VariantInternalType.vtByte;\n MyVariantHeader.Reserved = strcpy(value); \n\n // copy exposed struct type data to hidden array data\n memcpy(&MyVariant, &MyVariantHeader, sizeof(Variant));\n\n return myVariant;\n}\n\n// deallocs memory for any internal data type\nvoid freeVar(Variant &myVariant)\n{\n VariantHeader MyVariantHeader;\n\n // copy exposed struct type data to hidden array data\n memcpy(&MyVariantHeader, &MyVariant, sizeof(VariantHeader));\n\n switch (MyVariantHeader.VariantInternalType) {\n case vtCharPtr:\n strfree(MyVariantHeader.reserved);\n break;\n\n // other types\n\n default:\n break;\n }\n\n // copy exposed struct type data to hidden array data\n memcpy(&MyVariant, &MyVariantHeader, sizeof(Variant));\n}\n\nbool isVariantType(Variant &thisVariant, VariantInternalType thisType)\n{\n VariantHeader MyVariantHeader;\n\n // copy exposed struct type data to hidden array data\n memcpy(&MyVariantHeader, &MyVariant, sizeof(VariantHeader));\n\n return (MyVariant.VariantInternalType == thisType);\n}\n\n// -------\n\nvoid main()\n{\n Variant myVariantStr = CharPtrToVar(\"Hello World\");\n Variant myVariantByte = ByteToVar(42);\n\n char* myString = null;\n byte myByte = 0;\n\n if isVariantType(myVariantStr, vtCharPtr) {\n myString = VarToCharPtr(myVariantStr);\n // print variant string into screen\n }\n\n // ... \n}\n```\n\nThis is only a suggestion, and its not tested.\n\n========================================\n\nCode:\n```text\nint my_ints[10];\nDatum d(my_ints, 10/*count*/);\nfor(long i = 0; i < d.count(); ++i)\n{\n  d[i] = i;\n}\n```\n\n```text\nMyDate my_date = MyDate::GetDateToday();\nDatum d(my_date);\ncout << d[\"DAY_OF_WEEK\"] << endl;\ncout << d[MyDate::DAY_OF_WEEK] << endl; // alternative\n```\n\n```text\nMyDate my_dates[10];\nDatum d(my_dates, 10/*count*/);\nfor(long i = 0; i < d.count(); ++i)\n{\n  cout << d[i][MyDate::DAY_OF_WEEK] << endl;\n}\n```\n\n```text\nDatumState\n```\n\n```text\nvoid*\n```\n\n```text\nvoid*\n```\n\n```text\nenum\n```\n\n```text\nVALUE_OF\n```\n\n```text\nREF_TO\n```\n\n```text\nfloat\n```\n\n```text\nfloat\n```\n\n```text\nVALUE_OF\n```\n\n```text\nREF_TO\n```\n\n```text\nPTR_TO\n```\n\n```text\nDatum\n```\n\n```text\nDatumState\n```\n\n```text\nMyDate\n```\n\n```text\nMyColor\n```\n\n```text\nMyFileName\n```\n\n```text\nvoid*\n```\n\n```text\nDatumState\n```\n\n```text\nenum\n```\n\n```text\nDatumState\n```\n\n```text\nVALUE_OF\n```\n\n```text\nREF_TO\n```\n\n```text\npointer-to-MyDate\n```\n\n```text\nvalue-of-MyDate\n```\n\n```text\nDatumStateHandle\n```\n\n```text\nMyDate\n```\n\n```text\nMyColor\n```\n\n```text\nMyFileName\n```\n\n```text\nDatum\n```\n\n```text\nMacros, helper functions, and some other supporting stuff\n```\n\n```text\nDatum\n```\n\n```text\nVariant\n```\n\n```text\nDatum\n```\n\n```text\nREF_TO\n```\n\n```text\nVALUE_OF\n```\n\n```text\nVariant\n```\n\n```text\nstatic_cast<>()\n```\n\n```text\nvoid*\n```\n\n```text\nnull\n```\n\n```text\nDatum\n```\n\n```text\nDatumState\n```\n\n```text\nenum\n```\n\n```text\nint\n```\n\n```text\nVariant\n```\n\n```text\ntypedef\nenum VariantInternalType {\n  vtUnassigned = 0;\n  vtByte = 1;\n  vtCharPtr = 2; // <-- \"plain c\" string\n  vtBool = 3;\n  // other supported data types\n}\n\n// --> real data\ntypedef\nstruct VariantHeader {\n  void* Reserved; // <-- your data (byte or void*)\n  VariantInternalType VariantInternalType;  \n}\n\n// --> hides real data\ntypedef\n  byte[sizeof(VariantHeader)] Variant;\n\n// allocates & assign a byte data type to a variant\nVariant ByteToVar(byte value)\n{\n  VariantHeader MyVariantHeader;\n  Variant MyVariant;\n\n  MyVariantHeader.VariantInternalType = VariantInternalType.vtByte;\n  MyVariantHeader.Reserved = value;  \n\n  memcpy (&MyVariant, &MyVariantHeader, sizeof(Variant));\n\n  return myVariant;\n}\n\n// allocates & assign a char array data type to a variant\nVariant CharPtrToVar(char* value)\n{\n  VariantHeader MyVariantHeader;\n  Variant MyVariant;\n\n  MyVariantHeader.VariantInternalType = VariantInternalType.vtByte;\n  MyVariantHeader.Reserved = strcpy(value);  \n\n  // copy exposed struct type data to hidden array data\n  memcpy(&MyVariant, &MyVariantHeader, sizeof(Variant));\n\n  return myVariant;\n}\n\n// deallocs memory for any internal data type\nvoid freeVar(Variant &myVariant)\n{\n  VariantHeader MyVariantHeader;\n\n  // copy exposed struct type data to hidden array data\n  memcpy(&MyVariantHeader, &MyVariant, sizeof(VariantHeader));\n\n  switch (MyVariantHeader.VariantInternalType) {\n    case vtCharPtr:\n      strfree(MyVariantHeader.reserved);\n    break;\n\n    // other types\n\n    default:\n    break;\n  }\n\n  // copy exposed struct type data to hidden array data\n  memcpy(&MyVariant, &MyVariantHeader, sizeof(Variant));\n}\n\nbool isVariantType(Variant &thisVariant, VariantInternalType thisType)\n{\n  VariantHeader MyVariantHeader;\n\n  // copy exposed struct type data to hidden array data\n  memcpy(&MyVariantHeader, &MyVariant, sizeof(VariantHeader));\n\n  return (MyVariant.VariantInternalType == thisType);\n}\n\n// -------\n\nvoid main()\n{\n  Variant myVariantStr = CharPtrToVar(\"Hello World\");\n  Variant myVariantByte = ByteToVar(42);\n\n  char* myString = null;\n  byte  myByte = 0;\n\n  if isVariantType(myVariantStr, vtCharPtr) {\n    myString = VarToCharPtr(myVariantStr);\n    // print variant string into screen\n  }\n\n  // ...    \n}\n```\n\n========================================\n\nComments:\n- Another posibility I didn't mention is using third party implementations. I've done some background reading into COM VARIANTs but I don't really want to do that for a number of reasons (mostly that I want to do it, and I'm not sure that a union of all possible types is the best way to do it).\n- That's not a bad idea, but I think I'll do some work on it to see if it's feasible to implement. I'm not going to accept this as the answer just yet though :P\n- Interestingly enough, you have implemented your variant class in a very similar way to how I have done it. I'm going to have to do some more research and possibly implement this :) Thank you very much @charley\n- I was interested mostly in the actual casting system, so the most relevent part of your post was the `static_cast<>()` as that led me on to this page on msdn which describes runtime type casting in c++. I know I never tagged my question with a language as I thought it would be irrelevant, but that is perfect for what I want. With that I cover all but a few conversions, which are easy to check for and do myself.","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":48,"totalLines":438,"estimatedTokens":2125}}1110{"id":"stack-34440614","source":"stackoverflow","questionId":34440614,"title":"How to resolve AllocaInst from a Value that was a result of a LoadInst in LLVM?","tags":["c++","compiler-construction","llvm"],"text":"Title: How to resolve AllocaInst from a Value that was a result of a LoadInst in LLVM?\nTags: c++, compiler-construction, llvm\nSource: Stack Overflow\n\nQuestion:\nConceptually what I would like to do is pretty simple. I am using the Alloca technique described in the Kaleidoscope example paired with mem2reg to reduce the need to manually create Phi nodes.\n\nI have implemented several aspects of my custom language, but I have ran into a problem with implementing post increment/decrement in a generic way. \n\nMy AST node `PostIncrDecrNode` contains a token to denote `++` or `--` and an expression AST node that is codegened to return an `llvm::Value*` much like the Kaleidoscope example. I have already noticed that I will likely need to return something other then an `llvm::Value*` as my language is very type safe and I need to know things such as signedness of integral types, but now I feel that I may also need to keep track of the `llvm::AllocaInst`.\n\nA simple example situation is code like this:\n\n```\nint myfunction(int i)\n{\n return i++;\n}\n```\n\nMy debug AST print looks like this:\n\n```\n- CompilationUnit:test.str\n - FunctionDeclarationNode\n - IdentifierNode:int\n - IdentifierNode:myfunction\n - FunctionParameterNode\n - IdentifierNode:int\n - IdentifierNode:i\n - BlockNode\n - ReturnNode\n - PostIncrDecrNode\n - IdentifierNode:i\n```\n\nThe last two lines are the relevant part here as I have a `PostIncrDecrNode` that contains an `IdentifierNode` that will be codegened like this:\n\n```\nValue* IdentifierNode::codeGenInternal(CodeGenContext& context)\n{\n Value* rtn = NULL;\n SharedSymbolEntry entry = context.getSymbolInScopeByName(*value);\n Value* val = entry ? entry->llvmVal : NULL;\n if(val)\n {\n IRBuilder<>* builder = context.getIRBuilder();\n rtn = builder->CreateLoad(val, value->c_str());\n }\n else\n {\n context.handleCodeGenError(*this, \"Unknown variable name: \" + Twine(value->c_str()));\n\n }\n return rtn;\n}\n```\n\nThe line `SharedSymbolEntry entry = context.getSymbolInScopeByName(*value);` uses the stored `shared_ptr` to `std::String` 'value' member of the `IdentifierNode` (aka the variable name) to look up my `SharedSymbolEntry` (just a wrapper type to store language specific information along with the `llvm::Value*` which is the `llvm::AllocaInst*`) in the context stack which is populated with entry block allocas by function parameters or variable AST nodes.\n\nThe problem is that the `PostIncrDecrNode` doesn't have access to the alloca, only the returned `llvm::Value*` from the load `rtn = builder->CreateLoad(val, value->c_str());`.\n\nIs there any way in LLVM to resolve the `llvm::AllocaInst*` from the `llvm::Value*` in order to use it in a store instruction (the store instruction needs a pointer and I only have the integral value in this case)?\n\nI ran into a few similar questions, but I'm not sure if they answer my question.\nThe last one seems to suggest that this may not be possible at all so I would be curious how others have solved this problem.\n\n- llvm-dependencies-alloca-load\n\n- llvm-pass-how-to-insert-a-variable-using-existing-variable-value\n\n- get-pointer-to-llvmvalue-previously-allocated-for-createload-function\n\n========================================\n\nCode:\n```text\nint myfunction(int i)\n{\n    return i++;\n}\n```\n\n```text\n- CompilationUnit:test.str\n    - FunctionDeclarationNode\n        - IdentifierNode:int\n        - IdentifierNode:myfunction\n        - FunctionParameterNode\n            - IdentifierNode:int\n            - IdentifierNode:i\n        - BlockNode\n            - ReturnNode\n                - PostIncrDecrNode\n                    - IdentifierNode:i\n```\n\n```text\nValue* IdentifierNode::codeGenInternal(CodeGenContext& context)\n{\n    Value* rtn = NULL;\n    SharedSymbolEntry entry = context.getSymbolInScopeByName(*value);\n    Value* val = entry ? entry->llvmVal : NULL;\n    if(val)\n    {\n        IRBuilder<>* builder = context.getIRBuilder();\n        rtn = builder->CreateLoad(val, value->c_str());\n    }\n    else\n    {\n        context.handleCodeGenError(*this, \"Unknown variable name: \" + Twine(value->c_str()));\n\n    }\n    return rtn;\n}\n```\n\n```text\nPostIncrDecrNode\n```\n\n```text\n++\n```\n\n```text\n--\n```\n\n```text\nllvm::Value*\n```\n\n```text\nllvm::Value*\n```\n\n```text\nllvm::AllocaInst\n```\n\n```text\nPostIncrDecrNode\n```\n\n```text\nIdentifierNode\n```\n\n```text\nSharedSymbolEntry entry = context.getSymbolInScopeByName(*value);\n```\n\n```text\nshared_ptr\n```\n\n```text\nstd::String\n```\n\n```text\nIdentifierNode\n```\n\n```text\nSharedSymbolEntry\n```\n\n```text\nllvm::Value*\n```\n\n```text\nllvm::AllocaInst*\n```\n\n```text\nPostIncrDecrNode\n```\n\n```text\nllvm::Value*\n```\n\n```text\nrtn = builder->CreateLoad(val, value->c_str());\n```\n\n```text\nllvm::AllocaInst*\n```\n\n```text\nllvm::Value*\n```\n\n```text\nValue* val = someFunction(...); \nif (llvm::LoadInst* I = dyn_cast<llvm::LoadInst>(val)\n{\n     // do something with the load instruction I\n}\nelse // not a load instruction\n```\n\n```text\nllvm::Value*\n```\n\n```text\nLoadInst*\n```\n\n```text\nllvm:Value*\n```\n\n```text\nllvm::LoadInst*\n```\n\n```text\nllvm::LoadInst*\n```\n\n```text\nAllocaInst*\n```\n\n```text\nAllocaInst\n```\n\n```text\nGetElementPtrInst\n```\n\n========================================\n\nComments:\n- Thank you! I will probably start with that approach.","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":32,"totalLines":242,"estimatedTokens":1303}}1111{"id":"stack-26008965","source":"stackoverflow","questionId":26008965,"title":"How does the code generated by JIT compiler link with the program?","tags":["compiler-construction","jit","dynamic-linking"],"text":"Title: How does the code generated by JIT compiler link with the program?\nTags: compiler-construction, jit, dynamic-linking\nSource: Stack Overflow\n\nQuestion:\nI'm trying to understand how JIT compiler works. I already understood the basic idea of dynamic code generation at runtime, but I'm not clear about how the generated code links with the program? Through dynamic-link library? It would be nice if somebody could a \"hello world\" example of JIT compiler for study.\n\n========================================\n\nComments:\n- No, through a dymically generated code segment reached essentially via a function pointer.\n- A jit typically emits machine code into memory of the program it's running, and jumps to those instructions in one manner or another. (here is a small example)\n- You should document yourself about the internals of the JVM, the java virtual machine, the docs from Sun/Oracle are really good and explanatory and Java is extremely popular. Speaking about the implementation, often times JIT compilers end up generating some C-like code using basing data structures like associative arrays, trees and stacks . Remember that whatever if the thing you start from you always need to end up running some assembly for your machine .\n- @nos Thanks! The reference is exactly what I was looking for.\n- How about when an alternative version of an already linked object is generated? How are variations stored in the data structure? Does it keep some sort of execution path signatures and select objects based on these?","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":15,"estimatedTokens":381}}1112{"id":"stack-2049569","source":"stackoverflow","questionId":2049569,"title":"Manually emit a token with ANTLR","tags":["parsing","compiler-construction","antlr"],"text":"Title: Manually emit a token with ANTLR\nTags: parsing, compiler-construction, antlr\nSource: Stack Overflow\n\nQuestion:\nI'm having a bit of trouble manually emitting a token with a lexer rule in ANTLR. I know that the emit() function needs to be used but there seems to be a distinct lack of documentation about this. Does anybody have a good example of how to do this?\n\nThe ANTLR book gives a good example of how you need to do this to parse Python's nesting. For example, if you see a certain amount of whitespace that's greater than the previous line's whitespace, emit an INDENT token but if it's less, emit a DEDENT token. Unfortunately the book glosses over the actual syntax that's required.\n\nEDIT: Here's an example of what I'm trying to parse. It's Markdown's nested blockquotes:\n\n```\nbefore blockquote\n\n> text1\n>\n> > text2\n>\n> text3\n\noutside blockquote\n```\n\nNow, my approach so far is to essentially count the > symbols per line. For example, the above seems like it should emit (roughly...) PARAGRAPH_START, CDATA, PARAGRAPH_END, BQUOTE_START, CDATA, BQUOTE_START, CDATA, BQUOTE_END, CDATA, BQUOTE_END, PARAGRAPH_START, CDATA, PARAGRAPH_END. The difficulty here is the final BQUOTE_END which I think should be an imaginary token emitted once a non-blockquote element is found (and the nesting level is >= 1)\n\n========================================\n\nTop Answer:\nWell if the token you want to emit is not defined by a lexer rule then you'll need to add a token section like so:\n\n```\ntokens\n{\n MYFAKETOKEN\n}\n```\n\nIn your lexer you will still need a rule that tells the lexer when to produce this token. A common instance is determining if something is an Integer or range or real value.\n\n```\nNUMBERS_OR_RANGE\n: INT \n ( { LA(1) == '.' && LA(2) == '.' }? { _ttype = INT; }\n | { LA(1) == '.' || LA(1) == 'e' || LA(1) == 'E' }? { _ttype = REAL; }\n )\n| PERIOD \n ( PERIOD { _ttype = RANGE; }\n INT (( 'e' | 'E' ) ( '-' | '+' )? INT )? { _ttype = REAL; }\n)\n;\n```\n\nHere you can see we match either an INT and then lookahead, if we find a double period then we know that the INT is really an int and not a real. In this case we set the variable _ttype to be INT. If we find a PERIOD and then an 'e' we know it's a real.\n\nThe second case where we match a period we know that if the next char is a period, then we've got a range otherwise we've got a real.\n\nWe could use the `MYFAKETOKEN` type we defined above to assign to _ttype if that was appropriate.\n\n========================================\n\nCode:\n```text\nbefore blockquote\n\n> text1\n>\n> > text2\n>\n> text3\n\noutside blockquote\n```\n\n```text\ntokens\n{\n    MYFAKETOKEN\n}\n```\n\n```text\nNUMBERS_OR_RANGE\n: INT \n        ( { LA(1) == '.' && LA(2) == '.' }? { _ttype = INT; }\n    | { LA(1) == '.' || LA(1) == 'e' || LA(1) == 'E' }? { _ttype = REAL; }\n    )\n| PERIOD \n    ( PERIOD { _ttype = RANGE; }\n    INT (( 'e' | 'E' ) ( '-' | '+' )? INT )? { _ttype = REAL; }\n)\n;\n```\n\n```text\nMYFAKETOKEN\n```\n\n========================================\n\nComments:\n- Scott, could you perhaps give an example of what you're trying to parse?\n- You mention Python in the question but you don't actually say what language you are using. I gave an example that does this in C++.\n- Thanks, this is pretty close to what I'm looking for... I have updated the question with a more concrete example, as requested. If you have any insights, that'd be much appreciated!","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":103,"estimatedTokens":846}}1113{"id":"stack-21535956","source":"stackoverflow","questionId":21535956,"title":"Intermediate representation for FPGA compilers","tags":["compiler-construction","fpga","intermediate-language"],"text":"Title: Intermediate representation for FPGA compilers\nTags: compiler-construction, fpga, intermediate-language\nSource: Stack Overflow\n\nQuestion:\nWhen writing compilers for standard computers, one can target an existing intermediate representation (like LLVM IR) and not have to worry about tricky architectural differences between systems. Does something like this exist for FPGAs?\n\n========================================\n\nComments:\n- One intermediary level is the purely structural RTL which can be represented e.g. as an EDIF netlist (but often this already contains architecture dependant blocks). Or do you refer to an earlier representation in the dataflow, i.e. still containing some behavioral aspects? To some extent the synthesizable subsets of Verilog/VHDL actually take this role in many toolchains.\n- I was looking for something that is not behavioral.\n- I'm intrigued to know why you would want to do this...?\n- @MartinThompson To develop an experimental HDL.\n- @Dan - ahh, fun! Answer updated...","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":15,"estimatedTokens":253}}1114{"id":"stack-9329963","source":"stackoverflow","questionId":9329963,"title":"How to get name of the bit field referenced by BIT_FIELD_REF?","tags":["gcc","compiler-construction","internal"],"text":"Title: How to get name of the bit field referenced by BIT_FIELD_REF?\nTags: gcc, compiler-construction, internal\nSource: Stack Overflow\n\nQuestion:\nI want to get the name of the field with which it was declared from GENERIC representation. I have a BIT_FIELD_REF tree and it's DECL_NAME is zero. For example,\n\n```\nstruct {\n int a;\n unsigned b:1;\n} s;\n\n...\nif (s.b)\n ...\n```\n\nFor s.b I'll get a BIT_FIELD_REF and there's no obvious way to get the “b”, which is the original name of the field. How to do it?\n\n========================================\n\nTop Answer:\nWhat exactly did I do: investigating stuff in `tree-dump.c` I ended up understanding that names of the bit fields where they're known were coming from struct's DIEs and were hard to track.\n\nThen I decided to get the name from the `BIT_FIELD_REF` argument 0 (reference to structure) type, which is `RECORD_TYPE` and it stores all the fields' sizes and offsets.\n\nProblem was to understand that `BIT_FIELD_REF` doesn't reference the bits itself: it is used like `BIT_FIELD_REF & INTEGER_CST`, where constant acts like mask. After understanding this, I quickly computed the offsets and got the name from the type.\n\n========================================\n\nCode:\n```text\nstruct {\n    int a;\n    unsigned b:1;\n} s;\n\n...\nif (s.b)\n    ...\n```\n\n```text\ncall debug_c_tree (tree_var)\n```\n\n```text\ncall debug_tree (tree_var)\n```\n\n```text\ntree-dump.c\n```\n\n```text\nBIT_FIELD_REF\n```\n\n```text\nRECORD_TYPE\n```\n\n```text\nBIT_FIELD_REF\n```\n\n```text\nBIT_FIELD_REF & INTEGER_CST\n```\n\n========================================\n\nComments:\n- Thanks for the suggestion, but it doesn't know the name. Any other way to do it?\n- Sorry, if it's knowable, I'd expect the pretty-printer to print it. Does it reappear in later dump files? tree-ssa passes maybe? I'd guess so, and they'd need to get it from somewhere.\n- problem is, they really know and print in dumps names of **some** bitfields (not all, and the particular case I was interested in initially didn't output it's name) I think I'll try to determine where does it get the name when it does, but I'm not sure where's it gets printed out.","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":77,"estimatedTokens":532}}1115{"id":"stack-17195671","source":"stackoverflow","questionId":17195671,"title":"production start constraint in yacc","tags":["parsing","compiler-construction","grammar","bison","yacc"],"text":"Title: production start constraint in yacc\nTags: parsing, compiler-construction, grammar, bison, yacc\nSource: Stack Overflow\n\nQuestion:\nI'm implementing a Javascript parser using Bison. The ECMAScript specification states that\n\n```\nExpressionStatement:\n [lookahead ∉ { '{' , 'function'}] Expression ;\n```\n\nThat breaks the ambiguity between \"{ }\" as a BlockStatement (empty statement block) and as an ExpressionStatement (empty object literal) because an ExpressionStatement just can't start with a '{' token, although an Expression could.\n\nExample of an empty statement block:\n\n```\nif (a > 5) {}\n```\n\nExample of an empty object literal:\n\n```\nvar a = {};\n```\n\nHow can I specify in a bison/yacc grammar that some production should not start with some particular tokens? I mean, something like:\n\n```\nexpressionStatement\n : %must-not-start-with('{', TOKEN_FUNCTION) expression ';'\n ;\n```\n\nI know I could duplicate all my expression rules to define an \"ExpressionNotStartingWithOpenCurlyBraceOrFunction\", but that would increase a lot the size of my grammar, so I'm trying to avoid it.\n\n========================================\n\nCode:\n```text\nExpressionStatement:\n    [lookahead ∉ { '{' , 'function'}] Expression ;\n```\n\n```text\nif (a > 5) {}\n```\n\n```text\nvar a = {};\n```\n\n```text\nexpressionStatement\n    : %must-not-start-with('{', TOKEN_FUNCTION) expression ';'\n    ;\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":57,"estimatedTokens":342}}1116{"id":"stack-26761063","source":"stackoverflow","questionId":26761063,"title":"How to check that a method always returns a value","tags":["compiler-construction","compilation"],"text":"Title: How to check that a method always returns a value\nTags: compiler-construction, compilation\nSource: Stack Overflow\n\nQuestion:\nI wrote a compiler which is already working decently. It checks that all non-void methods do have at least one return statement, but it does not check whether all code paths within a non-void method return a value. So, for example, if I have the following code:\n\n```\nint function(bool a) \n{\n if(a){\n return 5;\n }\n}\n```\n\nIt will compile \"fine\" and if a is false, control will drop off the function and keep executing whatever is defined under the function. \n\nIs there some algorithm I can use to perform this check so that I always know if a method is always returning a value? Or will I need to reinvent the wheel?\n\n========================================\n\nCode:\n```text\nint function(bool a) \n{\n     if(a){\n         return 5;\n     }\n}\n```\n\n```text\nreturn\n```\n\n```text\nreturn\n```\n\n```text\nreturn\n```\n\n```text\nreturn\n```\n\n```text\nreturn\n```\n\n```text\nvoid\n```\n\n```text\nreturn\n```\n\n```text\nreturn\n```\n\n========================================\n\nComments:\n- For quite a few compiler tricks there are already algorithms available, including recommended internal representation etc. See e.g. en.wikipedia.org/wiki/Data-flow_analysis Reading commented source code of an existing compiler for the same language (looks like `C` in your case, so pick a stackoverflow.com/questions/584714/&hellip;) is probably the best way to self-answer your otherwise unclear and broad question\n- While that does indeed fix the \"fall off the end\" issue, I don't think that was the question. The question, as I understood it, was how to produce a compile time error if not all of the paths return a value.\n- I do what you suggest for functions that don't return a value. But can't do the same for those that do, because I can't simply guess what the return value will be. Obviusly functions falling off the end shouldn't happen. What I was asking is how to detect this situation so that the compiler reports an error and halts compilation.\n- @sepp2k Exactly, that is what I meant.\n- The fall off the end issue is fixed the way @rici suggests.\n- @sepp2k: I edited the answer to make it more clear that I was trying to answer the original question. Although I don't think that it is different from the previous answer, aside from some cosmetics.\n- Thanks, now it makes more sense to me.","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":9,"totalLines":74,"estimatedTokens":598}}1117{"id":"stack-37803841","source":"stackoverflow","questionId":37803841,"title":"c# - How do self-referencing classes or circular-referencing classes within same assembly compile successfully","tags":["c#",".net","compiler-construction","circular-reference","self-reference"],"text":"Title: c# - How do self-referencing classes or circular-referencing classes within same assembly compile successfully\nTags: c#, .net, compiler-construction, circular-reference, self-reference\nSource: Stack Overflow\n\nQuestion:\nI would like to know how C# and .Net compiler is able to successfully compile a self-referenced class or circular referenced classes within same assembly.\n\nConsider the following code is present within the same assembly.\n\n```\nclass X{ X x; }\nclass Y{ Z z; }\nclass Z{ Y y; }\n```\n\nOf course, this code compiles successfully. \n\nBut How? I'd like to know is how the compiler is able to resolve the classes in these cases for the very first time. \n\nFor example, when the compiler encounters class Y, it does not know class Z yet. How is it able to resolve the child property z in Class Y?\n\nPlease explain what exactly happens in the background when the code is compiled.\nProbably some suitable articles on how compiler resolves classes and types\n\n========================================\n\nCode:\n```text\nclass X{ X x; }\nclass Y{ Z z; }\nclass Z{ Y y; }\n```\n\n========================================\n\nComments:\n- What, exactly, do you think the obstacle to compiling such code is?\n- Ah. I should have added this in the question.\n- How many passes is probably the post you need to read, where Eric Lippert describes how the C# compiler works, which is unlike e.g. a C compiler that has to have all information available when it makes a single pass over a source file.\n- Thanks. This article is exactly what i was looking for.","metadata":{"transformedAt":"2026-08-18T18:32:17.949Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":1,"totalLines":40,"estimatedTokens":386}}1118{"id":"stack-10163204","source":"stackoverflow","questionId":10163204,"title":"How does compiler handle line number in runtime error message","tags":["compiler-construction","runtime","lexical-analysis"],"text":"Title: How does compiler handle line number in runtime error message\nTags: compiler-construction, runtime, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nAlmost all compiler will return a line number along with error message. I am wondering in compiler design perspective how does compiler handle line number message in terms of following different phases? thanks. \n\n- Scanner\n\n- Parser\n\n- AST data structure\n\n- Code generation\n\nIn additional:\n\n- Run time environment\n\n- Machine interpreter\n\n========================================\n\nTop Answer:\nMost lexer and parser generators will generate code that have error reporting methods which are called when a matching error occurs. You could override that method and do what you please. \n\nAs mentioned, it's common to associate a line and character number with an identifier or string/char/integer/boolean literal in the lexer specification. Usually a lexer makes available a `yyline()` method to do this. Instead of having the lexer return a raw token (e.g. a string) value, have it return an object that contains the string value, character, and line number. For example, take a quick look at this lexer spec.\n\n```\nprivate Symbol symbol(int type) {\n return new Symbol(type, yyline, yycolumn);\n}\n```\n\nThe parser will receive the symbol during the parse and should take the location information, along with the token value, and plug it into the AST node. That information should find its way into the symbol table eventually. During type analysis, each leaf node will have location information bound to it. That should be all you need to provide fairly good error diagnostics.\n\n========================================\n\nCode:\n```text\nprivate Symbol symbol(int type) {\n    return new Symbol(type, yyline, yycolumn);\n}\n```\n\n```text\nyyline()\n```\n\n========================================\n\nComments:\n- Runtime and Machine interpreter are not the parts of a compiler.\n- Thanks. I edited the question a little bit.","metadata":{"transformedAt":"2026-08-18T18:32:17.950Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":54,"estimatedTokens":490}}1119{"id":"stack-22737086","source":"stackoverflow","questionId":22737086,"title":"Do not match if a char is between quotation marks(AKA has a programming string pattern)","tags":["c++","regex","boost","basic","compiler-construction"],"text":"Title: Do not match if a char is between quotation marks(AKA has a programming string pattern)\nTags: c++, regex, boost, basic, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have been assigned to write a compiler for Basic programming language. In basic, codes are separated with new lines or by `:` mark. e.g to following to codes are valid.\n\nModel# 1\n\n```\n10 PRINT \"Hello World 1\" : PRINT \"Hello World 2\"\n```\n\nModel# 2\n\n```\n10 PRINT \"Hello World 1\"\n 20 PRINT \"Hello World 2\"\n```\n\nYou can test those here.\n\nThe First thing i need to do, before parsing codes in my compiler is to split codes.\nI have already splited codes in lines but i am stucked with finding a regex to split The following code sample:\n\nThis following code sample should be splited in 2 `PRINT` codes. \n\n```\n10 PRINT \"Hello World 1\" : PRINT \"Hello World 2\"\n```\n\nBut DO NOT match this:\n\nThe following code sample is a single standalone command.\n\n```\n10 PRINT \"Hello World 1\" \": PRINT Hello World 2\"\n```\n\n### Question\n\nAny regex pattern to DO match the first of above code samples which `:` is outside of pair of `\"` and DO NOT match the second one?\n\nCan anybody help me out here?\n\nAny thing would help. :)\n\n========================================\n\nTop Answer:\nI believe the best option for you is tokenize your source code by using a device such as a loop, instead of trying to tokenize it by using regexps.\n\nIn pseudocode\n\n```\nstring lexeme;\ntoken t;\n\nfor char in string\n if char fits current token\n lexeme = lexeme + char;\n else\n t.lexeme = lexeme;\n t.type = type;\n lexeme = null;\n end if\n // other treatments here\nend for\n```\n\nYou can see a real-world implementation of this device in this source code, more specifically at line 86.\n\n========================================\n\nCode:\n```text\n10 PRINT \"Hello World 1\" : PRINT \"Hello World 2\"\n```\n\n```text\n10 PRINT \"Hello World 1\"\n 20 PRINT \"Hello World 2\"\n```\n\n```text\n10 PRINT \"Hello World 1\" : PRINT \"Hello World 2\"\n```\n\n```text\n10 PRINT \"Hello World 1\" \": PRINT Hello World 2\"\n```\n\n```text\n:\n```\n\n```text\nPRINT\n```\n\n```text\n:\n```\n\n```text\n\"\n```\n\n```text\n/* lines' tokens container */\n    std::string token;\n    /* Tokenize the file's content into seperate lines */\n    /* fetch and tokenizing line version of readed data  and maintain it into the container vector*/\n    for(int top = 0, bottom = 0; top < strlen(buffer) ; top++)\n    {\n        /* inline tokenizing with line breakings */\n        if(buffer[top] != '\\n' || top == bottom)\n        { /* collect current line's tokens */ token += char(buffer[top]); /* continue seeking */continue; }\n        /* if we reach here we have collected the current line's tokens */\n        /* normalize current tokens */\n        boost::algorithm::trim(token);\n        /* concurrent statements check point */\n        if(token.find(':') != std::string::npos)\n        {\n            /* a quotation mark encounter flag */\n            bool quotation_meet = false;\n            /* process entire line from beginning */\n            for(int index = 0; true ; index++)\n            {\n                /* loop's exit cond. */\n                if(!(index < token.length())) { break; }\n                /* fetch currently processing char */\n                char _char = token[index];\n                /* if encountered  a quotation mark */\n                /* we are moving into a string */\n                /* note that in basic for printing quotation mark, should use `CHR$(34)` \n                 * so there is no `\\\"` to worry about! :) */\n                if(_char == '\"')\n                {\n                    /* change quotation meeting flag */\n                    quotation_meet = !quotation_meet;\n                    /* proceed with other chars. */\n                    continue;\n                }\n                /* if we have meet the `:` char and also we are not in a pair quotation*/\n                if(_char == ':' && !quotation_meet)\n                {\n                    /* this is the first sub-token of current token */\n                    std::string subtoken(token.substr(0, index - 1));\n                    /* normalize the sub-token */\n                    boost::algorithm::trim(subtoken);\n                    /* add sub-token as new line */\n                    source_codes.push_back(subtoken);\n                    /* replace the rest of sub-token as new token */\n                    /**\n                     * Note: We keep the `:` mark intentionally, since every code line in BASIC \n                     * should start with a number; by keeping `:` while processing lines starting with `:` means \n                     * they are meant to execute semi-concurrent with previous numbered statement.\n                     * So we use following `substr` pattern instead of `token.substr(index + 1, token.length() - 1);`\n                     */\n                    token = token.substr(index, token.length() - 1);\n                    /* normalize the sub-token */\n                    boost::algorithm::trim(token);\n                    /* reset the index for new token */\n                    index = 0;\n                    /* continue with other chars */\n                    continue;\n                }\n            }\n            /* if we have any remained token and not empty one? */\n            if(token.length())\n                /* a the tokens into collection */\n                goto __ADD_TOKEN;\n        }\n__ADD_TOKEN:\n        /* if the token is not empty? */\n        if(token.length())\n            /* add fetched of token to our source code */\n            source_codes.push_back(token);\n__NEXT_TOKEN:\n        /* move pointer to next tokens' position */\n        bottom = top + 1;\n        /* clear the token buffer */\n        token.clear();\n        /* a fail safe for loop */\n        continue;\n    }\n    /* We NOW have our source code departed into lines and saved in a vector */\n```\n\n```text\nchar* buffer\n```\n\n```text\nvector<string> source_code\n```\n\n```text\n\"(?>[^\\\\\"]++|\\\\{2}|\\\\.)*\"|:\n```\n\n```text\nstring lexeme;\ntoken t;\n\nfor char in string\n    if char fits current token\n        lexeme = lexeme + char;\n    else\n        t.lexeme = lexeme;\n        t.type = type;\n        lexeme = null;\n    end if\n    // other treatments here\nend for\n```\n\n========================================\n\nComments:\n- You shouldn't be parsing this kind of construct with regexps. Regexps can only match regular languages, which doesn't fit your problem. You should instead be using a construct such as this.\n- @Mauren Yes indeed. I will eventually do that, but at first i need to tokenize the source code, and purify the codes(i.e removing comments and etc...). So i believe i need to tokenize the `:` at first.\n- I would advise you to tokenize by constructing a loop where you look at each character and decide to which token it belongs, instead of doing it by regexps. Please take a look at line 86 of the previously linked source code.\n- @Mauren Thanks, that is a huge help :)\n- @Mauren: I'm agree that a \"full\" regex solution is not the best way for this kind of task, however, don't believe that a library like boost (or other modern regex tools) is unable to match non regular languages. We are far from theorical considerations and POSIX regex engines capabilities.\n- @Dariush: What are you trying to do?\n- @Dariush: the approach is different. If you put the first part of the alternation, in a capturing group, you only need to check if the capturing group is not void or exist to know if the pattern match a colon outside any quotes.\n- The below answer is the implementation of your proposal. Thanks.","metadata":{"transformedAt":"2026-08-18T18:32:17.950Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":229,"estimatedTokens":1889}}1120{"id":"stack-5372532","source":"stackoverflow","questionId":5372532,"title":"Writing part of a compiler (written in c++) in Perl","tags":["c++","perl","compiler-construction","programming-languages","lexer"],"text":"Title: Writing part of a compiler (written in c++) in Perl\nTags: c++, perl, compiler-construction, programming-languages, lexer\nSource: Stack Overflow\n\nQuestion:\ni am trying to learn more about compilers and programming languages, **unfortunately** my university doesnt offer a course about compilers and so i have to do myself (thank you internet).\n\nAt the moment im **tryin** to understand and to implement a lexer for my language and i need regular expressions.\n\nI am used to script perl regex pretty quickly and i thought that *i could embed Perl in my C++ lexer*. Now the questions are:\n\n- Will it cause Heavy overhead?\n\n- Should i try to make peace with BOOST (or any other c++ library good gor regex) ?\n\nThank you for reading this :)\n\n========================================\n\nTop Answer:\nEmbedding Perl in your project just to do regular expressions would be like trying to stuff an elephant into a Miata to get more trunk space. (Badump!)\n\nBoost would be one way to handle regular expressions, or if you're writing in an environment that supports POSIX.2, look into the `regcomp()`, `regexec()` and `regfree()` functions.\n\nAfter you've written your own lexer, investigate a tool called `lex` which is pretty much the gold standard for developing lexical analyzers. It has a partner called `YACC` for developing parsers. Both are time tested and generate tight, bug-free code. (GNU-ish environments call these programs `flex` and `bison`.)\n\n========================================\n\nCode:\n```text\nregcomp()\n```\n\n```text\nregexec()\n```\n\n```text\nregfree()\n```\n\n```text\nlex\n```\n\n```text\nYACC\n```\n\n```text\nflex\n```\n\n```text\nbison\n```\n\n========================================\n\nComments:\n- why not the old good lex/yacc? (well, flex/bison)\n- Actually i know about them but i want to get a \"deep\" understanding of compilers internals.. i've also read about Quex that looks good too :)\n- I've seen a lexer written in Icon, which (like Perl) also has very good string processing, and the need for the power of something like regular expressions is nowhere as strong as you'd think. I'd recommend you find a book that is usually used by universities for compiler-writing courses, such as \"Crafting a Compiler in C\". I know that one gets very deep inside how a compiler works.\n- At the moment i am reading the dragon book (compilers etc etc) i will try to fetch a copy of your book asap :)\n- @fatmatto, if you want a *deep* understanding, you're bound to writing your own regular expressions compiler.\n- @SK-logic i found a lot of material about that on the web, thanks for suggesting it :D\n- Regexp::Grammars is an interesting CPAN module to learn new Perl regex capabilities.\n- this solution sounds very interesting, i could also learn how to \"organize\" a perl module. Maybe is a dumb question, but why do say \"not compiling c++\" ? Performance issues or something else?\n- @fatmatto: Writing a compiler that can parse C++ is quite hard. Besides, you really don't need regular expressions to lex C++.\n- i'm sorry i misread your comment, thanks for explaining that :D i want to compile my own defined language, not c++ anyway :)\n- @fatmatto: Although @MSalters isn't I, he did correctly explain my meaning.\n- @Ben Voigt sorry for bothering but could you explain me what do you exactly mean with \"compiler driver\" ? which part of the compiler process should it take care of?\n- @fatmatto: The \"compiler driver\" would handle command line processing and then call the other subroutines, such as \"preprocessor\", \"parser\", \"lexer\", \"call resolver\", \"optimizer\", \"output generator\"\n- I missed libpcre, but i found libPerl++ and thats why i made this post. Your suggestion looks more specific for my problem, i will add this libpcre to the todo list ;)\n- PCRE does not support many possibilities of Perl regular expressions","metadata":{"transformedAt":"2026-08-18T18:32:17.950Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":75,"estimatedTokens":953}}1121{"id":"stack-10923533","source":"stackoverflow","questionId":10923533,"title":"Why doesn't the order of methods in a class matter in C++?","tags":["c++","compiler-construction"],"text":"Title: Why doesn't the order of methods in a class matter in C++?\nTags: c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have been programming in C++ for quite some time and I never thought about this until today.\n\nConsider the following code:\n\n```\nstruct foo\n{\n // compiles fine\n void bar()\n {\n a = 1;\n my_int_type b;\n b = 5;\n }\n\n // Just a declaration, this fails to compile, which leads me to assume that\n // even though the bar() method is declared and defined all at once, the\n // compiler looks/checks-syntax-of the class interface first, and then compiles\n // the respective definitions...?\n void bar2(my_int_type); // COMPILE ERROR\n\n my_int_type b; // COMPILE ERROR because it comes before the typedef declaration\n typedef int my_int_type;\n my_int_type a;\n\n void bar3(my_int_type); // compiles fine\n};\n\nint main()\n{\n foo a;\n a.bar();\n return 0;\n}\n```\n\nIs my understanding of why the errors occur *(see `bar2()` comment above)* correct/incorrect? Either way, I would appreciate an answer with a simplistic overview of how a single-pass C++ compiler would compile the code given above.\n\n========================================\n\nTop Answer:\nCompiler just starts to go down in a block. Any symbol which is not familiar to it will be considered as a new symbol which is not defined. This is the scheme behind the function definition or header files.\n\nYou can suppose that the compiler first makes a list of definitions so the bar() method should get compiled correctly because the definitions have provided before.\n\n========================================\n\nCode:\n```text\nstruct foo\n{\n  // compiles fine\n  void bar()\n  {\n    a = 1;\n    my_int_type b;\n    b = 5;\n  }\n\n  // Just a declaration, this fails to compile, which leads me to assume that\n  // even though the bar() method is declared and defined all at once, the\n  // compiler looks/checks-syntax-of the class interface first, and then compiles\n  // the respective definitions...?\n  void bar2(my_int_type); // COMPILE ERROR\n\n  my_int_type       b; // COMPILE ERROR because it comes before the typedef declaration\n  typedef int       my_int_type;\n  my_int_type       a;\n\n  void bar3(my_int_type); // compiles fine\n};\n\nint main()\n{\n  foo a;\n  a.bar();\n  return 0;\n}\n```\n\n```text\nbar2()\n```\n\n```text\nstruct foo\n{\n    void bar()\n    {\n        my_int_type b;\n    }\n\n    typedef int my_int_type;\n};\n```\n\n```text\nstruct foo\n{\n    void bar();\n\n    typedef int my_int_type;\n};\n\ninline void foo::bar()\n{\n    my_int_type b;\n}\n```\n\n```text\nbar2\n```\n\n```text\nb\n```\n\n```text\nmy_int_type\n```\n\n```text\nmy_int_type\n```\n\n```text\nbar\n```\n\n========================================\n\nComments:\n- Pretty sure I've asked this question before... :-)\n- @Mehrdad: I searched for it, but could not find it with the keywords that came to my mind :)","metadata":{"transformedAt":"2026-08-18T18:32:17.950Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":140,"estimatedTokens":699}}1122{"id":"stack-4484328","source":"stackoverflow","questionId":4484328,"title":"Good languages to write a compiler for","tags":["compiler-construction","programming-languages"],"text":"Title: Good languages to write a compiler for\nTags: compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nI'm thinking of writing a compiler in haskell, and just to gain some knowledge and experience, I will try to implement compilers for existing languages. Could someone give me a list of languages which are suitable for this?\n\nThanks in advance\n\n========================================\n\nTop Answer:\nScheme is often used for this. There's even a tutorial called *Write Yourself a Scheme in 48 hours* for Haskell.\n\n========================================\n\nComments:\n- For an easy start, you could begin with math terms (with multiple variables inside).\n- For added fun, make your compiler output bytecode for a virtual machine, like Java's or Python's.\n- Lua is not quite compileable, it's too dynamic. Of course it's still possible to compile it efficiently, but that will involve some advanced techniques.\n- @khachik: ASM does not get compiled, it is assembled.\n- @J.M.Becker: A compiler is a program that translates a program in language **X** into a semantically equivalent program in language **Y**. An assembler most certainly does exactly that, so an assembler is a compiler, and assembling is compiling. It's just that an assembler is not a very interesting compiler, since assembler is designed to map 1:1 to object code, so an assembler is more or less a simple lookup table.\n- Forth should be implemented in Forth. It's not that interesting to write a Forth compiler in Haskell - in this case something like Cat or Joy would be more appropriate.\n- I'd agree with this except for the fact that C has all the K&R legacy support that hardly anyone uses. Because of that, I'd recommend looking elsewhere. Remember, C was designed to make the compiler easy to write, which is why you have 3 ways to add 1 to an integer.\n- Ya, it was intended as humor. But it's not that unreasonable. You could use the Small C compiler. It's small enough to be able to implement, and you can use existing implementations as a basis for unit tests.\n- Niklaus Wirth was proud of the fact that while each of Pascal's successor languages fixed some problems with the predecessors, was more expressive, safer, and in some sense \"more powerful\", it was also simpler. So, Pascal's successors (Modula-2, Oberon, the latest one being Component Pascal) might be even better languages to write a compiler for than Pascal.\n- \"Consider using LLVM as your back-end, it will save you some time on implementing boring stuff\" - I find the backend the most interesting :)\n- @xilpex, by boring stuff I mean the very end of the back-end - instruction selection, instruction scheduling, etc. Of course you can spend few years (I did...) implementing your own equivalent of the TableGen infrastructure, but in general it's a solved problem anyway and should be maintained by the hardware vendors, not compiler developers. You cannot possibly all the microarchitecture details on your own.\n- You implemented your own full backend!?\n- @xilpex yes, because I had to target a custom (and very peculiar) ISA.","metadata":{"transformedAt":"2026-08-18T18:32:17.950Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":30,"estimatedTokens":775}}1123{"id":"stack-28351654","source":"stackoverflow","questionId":28351654,"title":"What makes a value unsafe to be stored in a register?","tags":["compiler-construction","register-allocation"],"text":"Title: What makes a value unsafe to be stored in a register?\nTags: compiler-construction, register-allocation\nSource: Stack Overflow\n\nQuestion:\nWhen talking about register allocation, texts on compilation (for example, Engineering a Compiler by Cooper) often mention that values stored in registers need to be \"safe\" - otherwise they should be stored in memory.\n\nWhat makes a value unsafe to be kept in a register?\n\nEdit: Context from the book:\n\n\"In a memory-to-memory model, the allocator must determine which values can be kept safely in registers—that is, which values are unambiguous.\"\n\nThe most informative mention I could find is\n\"Register promotion uses data-flow analysis of pointer values to determine when a pointer-based value can safely be kept in a register throughout a loop nest and to rewrite the code so that the value is kept in a newly introduced temporary variable.\"\n\nSo to clarify the question: why would a pointer value be unsafe to store in a register, and is this the only case when a value could not be safe to store in a register?\n\n========================================\n\nTop Answer:\nA slightly different bit of code: \n\n```\nstatic int x;\nint* p;\nint first, second;\n\nfirst = x;\n*p = 3;\nsecond = x;\n```\n\nThe assignment to *p might have changed x, so the assignment second = x must read x from memory again and cannot use the value that was just stored in first and which is probably still in a register.\n\n========================================\n\nCode:\n```text\nint first, second, *p, *q;\n...\nfirst = *p; // store the value from the variable referred to by p in first\n*q = 3;     // assign to the variable referred to by q\nsecond = *p;     // store the value from the variable referred to by p in second\n```\n\n```text\naliases\n```\n\n```text\nfirst\n```\n\n```text\n*p\n```\n\n```text\nsecond\n```\n\n```text\np\n```\n\n```text\nq\n```\n\n```text\n*p\n```\n\n```text\n*q\n```\n\n```text\nalias analysis\n```\n\n```text\nstatic int x;\nint* p;\nint first, second;\n\nfirst = x;\n*p = 3;\nsecond = x;\n```\n\n========================================\n\nComments:\n- It would help to provide a quote from Cooper using \"safe\" in context, and/or looking up \"safe\" in the book's index.\n- I'm not sure about the first quote, but the second one is about alias analysis -- when the compiler can prove a value loaded through a pointer will not change throughout a loop, so it need not be loaded on each iteration.","metadata":{"transformedAt":"2026-08-18T18:32:17.950Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":97,"estimatedTokens":595}}1124{"id":"stack-1678591","source":"stackoverflow","questionId":1678591,"title":"Why are number suffixes necessary?","tags":["c#","compiler-construction"],"text":"Title: Why are number suffixes necessary?\nTags: c#, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThe C# language (and other languages I'm sure) require suffixes at the end of numeric literals. These suffixes indicate the type of the literal. For example, `5m` is a decimal, `5f` is a floating point number.\n\nMy question is: are these suffixes really necessary, or is it possible to infer the type of a literal from its context?\n\nFor example, the code `decimal d = 5.0` should infer that `5.0` is not a double, but a decimal. Does that kind of grammar cause problems?\n\n========================================\n\nTop Answer:\nIn general, we try to design the language so that information about types flows \"out\", not \"in\". That is, the type of an expression is determined by first analyzing each of its subexpressions, and then we see if it is consistent with its context -- we don't go the other way usually, reasoning from context to subexpressions. You can get into some very hard-to-analyze situations when type information can flow both ways, as it can with lambda expressions. \n\nNow, in the specific situation you mention, we *could* have written a rule that says \"floating point literals that can be converted to decimal without loss of precision or magnitude do not require the m suffix\", just as literal ints that fit into short convert automatically. But it's easy enough to add the \"m\", it would be confusing when some literals converted automatically and others did not, and requiring that the types be consistent makes the code easier to understand and more self-documenting.\n\n========================================\n\nCode:\n```text\n5m\n```\n\n```text\n5f\n```\n\n```text\ndecimal d = 5.0\n```\n\n```text\n5.0\n```\n\n```text\nfloat f = 7;\n```\n\n```text\nfloat f = (double)(int)(1 / 3 + 6e22 / (double)7);\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.950Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":44,"estimatedTokens":453}}1125{"id":"stack-1348476","source":"stackoverflow","questionId":1348476,"title":"Boolean expressions optimizations in Java","tags":["java","optimization","compiler-construction","boolean-logic","boolean-expression"],"text":"Title: Boolean expressions optimizations in Java\nTags: java, optimization, compiler-construction, boolean-logic, boolean-expression\nSource: Stack Overflow\n\nQuestion:\nConsider the following method in Java:\n\n```\npublic static boolean expensiveComputation() {\n for (int i = 0; i And the following main method:\n\n```\npublic static void main(String[] args) {\n boolean b = false;\n if (expensiveComputation() && b) {\n }\n}\n```\n\nLogical conjunction (same as &&) is a commutative operation. So why the compiler doesn't optimize the if-statement code to the equivalent:\n\n```\nif (b && expensiveComputation()) {\n}\n```\n\nwhich has the benefits of using short-circuit evaluation?\n\nMoreover, does the compiler try to make other logic simplifications or permutation of booleans in order to generate faster code? If not, why? Surely some optimizations would be very difficult, but my example isn't simple? Calling a method should always be slower than reading a boolean, right?\n\nThank you in advance.\n\n========================================\n\nTop Answer:\nBecause `expensiveComputation()` may have side-effects.\n\nSince Java doesn't aim to be a functionally pure language, it doesn't inhibit programmers from writing methods that have side-effects. Thus there probably isn't a lot of value in the compiler analyzing for functional purity. And then, optimizations like you posit are unlikely to be very valuable in practice, as `expensiveComputation()` would usually be required to executed anyway, to get the side effects.\n\nOf course, for a programmer, it's easy to put the `b` first if they expect it to be false and explicitly want to avoid the expensive computation.\n\n========================================\n\nCode:\n```text\npublic static boolean expensiveComputation() {\n    for (int i = 0; i < Integer.MAX_VALUE; ++i);\n    return false;\n}\n```\n\n```text\npublic static void main(String[] args) {\n    boolean b = false;\n    if (expensiveComputation() && b) {\n    }\n}\n```\n\n```text\nif (b && expensiveComputation()) {\n}\n```\n\n```text\npublic static boolean expensiveComputation() {\n        for (int i = 0; i < Integer.MAX_VALUE; ++i);\n        b = false;\n        return false;\n}\n\npublic static boolean b = true;\npublic static void main(String[] args) {\n        if (expensiveComputation() || b) {\n        // do stuff\n        }\n}\n```\n\n```text\n//do stuff\n```\n\n```text\nexpensiveComputation()\n```\n\n```text\nexpensiveComputation()\n```\n\n```text\nb\n```\n\n```text\na && b\n```\n\n========================================\n\nComments:\n- This is a silly example - its somewhat different with an ||. Why not just put the side-effect into expensiveComputation, that would illustrate the point well.\n- It's not really different with an `||`. The same kind of optimization could be done, and it has the same implications. This was just the first example I thought of that has an immediately noticable effect, and that was related to his example.\n- Does the JVM or javac actually do this? Or are you talking at some theoretical abstract layer?\n- Yes, the JVM contains an optimizing just-in-time compiler which compiles code that is used heavily. I did that benchmark, the first iteration takes 10ms, the second five, and after that it only shows zero time for each iteration.\n- @starblue: I don't think that the compiler is allowed to reorder the evaluation of the LHS & RHS expressions unless it *can be proved* that the outcome of the computation is never changed by the optimization.\n- No, the compiler doesn't need to reorder. After inlining it will see the boolean constant and optimize the && away.\n- which doesn't avoid the expensiveCall","metadata":{"transformedAt":"2026-08-18T18:32:17.950Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":109,"estimatedTokens":898}}1126{"id":"stack-254276","source":"stackoverflow","questionId":254276,"title":"Would syntax for composition be a useful addition to Java?","tags":["java","inheritance","compiler-construction","programming-languages","composition"],"text":"Title: Would syntax for composition be a useful addition to Java?\nTags: java, inheritance, compiler-construction, programming-languages, composition\nSource: Stack Overflow\n\nQuestion:\nFirst off, I know next to nothing about language theory, and I barely know any other languages except Java, but I had an idea that I think would be cool, but I need you guys to tell me:\n\na: why it sucks\n\nb: how language x has had that for years\n\nc: how my mind sucks\n\nd: all of the above\n\nThe idea would give composition the same ease of code reuse that `extends` does. \n\nSo if you had a class like this:\n\npublic interface A { \n public void methodInA(); \n}\n\nAnd then you had a class like this:\n\npublic class B { \n private composed A;\n public B() {\n // construct A within constructor\n }\n}\n\nYou would then be able to do this:\n\nB myB = new B();\nmyB.methodInA();\n\nWithout having to add in the delegation in B's class. But you could also do the same as with inheritance, ie:\n\n@Overrides\npublic void methodInA(){\n // B's own delegation method\n}\n\nDisadvantages include:\n\n- methods are hidden in the source code, making it less obvious where the call is coming from, but this is also the case with `extends`\n\n- if composed fields the same method signature there needs to be a conflict resolved (how do conflicting interfaces solve this?)\n\n- if you wanted to have several composed fields of the same type, there would be an obvious conflict for which field to delegate to\n\n- probably 100 other things I've not thought of\n\nLike I say, I'm obviously no language theorist, and I haven't spent ages thinking about it, the idea just popped in my head and I wanted to know how wrong I am. I just think it would be kind of cool.\n\n========================================\n\nTop Answer:\nIt sounds cool but I think it makes for some horrible language constructs. Obviously there is a problem if you declare more than one 'composition' of the same class, but even if you forbid that what about the case where a call matches a method in more than one of the (different) composed classes? You would have to specify which one was called in the main class, and you would need extra syntax for that. The situation becomes even worse if there are public members in the classes.\n\nComposition is used to prevent problems with multiple inheritance. Allowing composition like this is effectively permitting multiple inheritance, at least in terms of resolving which method to call. Since a key design decision with Java was to disallow multiple inheritance (for good reasons) I think it unlikely that this would ever be introduced to Java.\n\n========================================\n\nCode:\n```text\npublic interface A {  \n    public void methodInA();  \n}\n```\n\n```text\npublic class B {  \n    private composed A;\n    public B() {\n        // construct A within constructor\n    }\n}\n```\n\n```text\nB myB = new B();\nmyB.methodInA();\n```\n\n```text\n@Overrides\npublic void methodInA(){\n    // B's own delegation method\n}\n```\n\n```text\nextends\n```\n\n```text\nextends\n```\n\n```text\npublic class Foo {\n\n  @Delegate(IBar.class)\n  private Bar bar;\n\n  // initialize bar via constructor or setter\n}\n```\n\n```text\npublic Baz method1(Qux val) {\n  return bar.method1(val);\n}\n```\n\n```text\nbelongsTo\n```\n\n```text\nhas-many\n```\n\n========================================\n\nComments:\n- It's not exactly that I would want myB.getAInstance().methodInA(), I may be using the composed field to implement an interface, so the class must have the method itself.\n- \"..I think it unlikely that this would ever be introduced to Java.\" - I hear that, if I can form the idea for an addition to one of, if not the most, widely used programming language, on what was quite a slow afternoon... well, we and Sun are in trouble :-D\n- What about, if the compiler found a conflict, it would force you to override it? So you are forced to resolve it in one single way, without too much of a syntax cost, as it wouldn't look out of place. But public members... yeah hadn't thought of them at all... hmmm.\n- \"Honestly, if you really knew what you were doing...\" - and therein lies the rub my friend! :-D\n- The approach I'd favor would be to allow a class to designate two members (e.g. `first` and `second`) such that an attempt to access `foo.someMemberName` when `foo` has no such member would get rewritten as `foo.first().someMemberName` [without trying such lookup recursively on the return from `first`], and if that failed, `foo.first().someMemberName` [allowing recursive lookups on the return from `second`]. Note that no \"stacking\" of recursive lookups would be required.","metadata":{"transformedAt":"2026-08-18T18:32:17.950Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":10,"totalLines":133,"estimatedTokens":1144}}1127{"id":"stack-3038045","source":"stackoverflow","questionId":3038045,"title":"How does compiler understand the pointer type?","tags":["c++","pointers","compiler-construction","typeinfo"],"text":"Title: How does compiler understand the pointer type?\nTags: c++, pointers, compiler-construction, typeinfo\nSource: Stack Overflow\n\nQuestion:\nHow does the C++ compiler understand the pointer type? As I know pointer has a size equal to WORD of the OS (32 or 64). So does it store some info in that 32 (or 64) bits about type? Just because you can not have a pointer on one type and assign to that pointer another pointer with a different type.\n\n========================================\n\nTop Answer:\nThe compiler knows what type a pointer is because the source code says what type the pointer is:\n\n```\nint* ip; // ip is a pointer to an int\n\nfloat* fp; // fp is a pointer to a float\n\nvoid* vp; // vp is a pointer to some unknown type; need to cast it to a pointer\n // to an actual type in order to access the pointed-at object\n```\n\n========================================\n\nCode:\n```text\nreinterpret_cast<T>\n```\n\n```text\nstatic_cast<T>\n```\n\n```text\ndynamic_cast<T>\n```\n\n```text\nint* ip;   // ip is a pointer to an int\n\nfloat* fp; // fp is a pointer to a float\n\nvoid* vp;  // vp is a pointer to some unknown type; need to cast it to a pointer\n           // to an actual type in order to access the pointed-at object\n```\n\n```text\ndest = source\n// make sure that type of source == type of dest\n```\n\n```text\nint*    ip;\n// do some stuff\ndouble* dp = ip;\n```\n\n```text\nCPU word\n```\n\n```text\nword\n```\n\n========================================\n\nComments:\n- I think OP meant \"some info.\" Edited.\n- Don't confuse compile time and runtime. At compile time all the type information is known and stored within the compiler. At runtime most of the type information has been discarded and is definitely not stored as part of the pointer (the generated code is done specifically for the type it knows is rehire without reference to the type at runtime)\n- What about objects? Do they have type info runtime?\n- Sort of, some. But it is not generally used at runtime (exception is of course dynamic_cast). The code planted by the compiler is generally specific to a type deduced at compile time. There is some type information available on request but it is bad practice to use it in your code and not usually helpful. Type specific functionality is achieved at the language level via virtual functions and inheritance this functionality at runtime is accessed via method indirection (ie potentially using v-tables to call methods).\n- +1 And, of course, you *can* assign a pointer of one type to a pointer of another type, you just have to cast it.\n- It's an int! Now it's a float! Now it's an int! Now it's an float!\n- @glowcoder: Relax, man. You're two pointers! (juvenile joke from WoW)\n- @John: Looked it up, turns out your joke is funny. +1\n- Use of reinterpret_cast is not unsafe. It's meaning is well defined and if used correctly is perfectly safe. If used incorrectly it is unsafe but the same can be said for anything that is used incorrectly.\n- Also note there is no requirement for iterators to check that they from the same container when being compared or used in an algorithm and thus provide no more safety than pointers (though certain debug implementations of the STL do provide this functionality to help in debugging, but this functionality should not be relied upon)\n- Historically, \"a pointer is a memory address\" has been correct. However, on modern OSes, the closest you get is a virtual memory address, and even that is just an implementation detail.\n- @jurily: Unless we go back to 60's its always been a virtual memory address. But this is irrelevant and transparent to any application code (specifically its a hardware detail).\n- +1. Apologies if my answer sounded flippant; that wasn't my intent.\n- I didn't see it as maliciously flippant, just as a bit of a joke response.","metadata":{"transformedAt":"2026-08-18T18:32:17.950Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":81,"estimatedTokens":945}}1128{"id":"stack-62525013","source":"stackoverflow","questionId":62525013,"title":"Why do compilers construct a graph in register allocation?","tags":["assembly","compiler-construction","graph-algorithm","cpu-registers","register-allocation"],"text":"Title: Why do compilers construct a graph in register allocation?\nTags: assembly, compiler-construction, graph-algorithm, cpu-registers, register-allocation\nSource: Stack Overflow\n\nQuestion:\nI've been researching register allocation and was wondering why they all built graphs from the live registers list when there could be a better way to do it. The way I think they could do it is when the live registers crosses the number of registers available, then registers could be spilled. Here is an example (pseudo-assembly):\n\n```\n## ldi: load immediate\n## addr: add registers and store in arg 2\n## store: store memory at offset from stack pointer\n.text\n main:\n # live registers: {}\n ldi %t0, 12 # t0 = 12\n # live registers: {t0}\n ldi %t1, 8 # t1 = 8\n # live registers: {t0, t1}\n addr %t0, %t1 # t1 = t0 + t1\n # live registers: {t1}\n store -4(%sp), %t1 # -4(%sp) = t1\n # live registers: {}\n exit\n```\n\nI have laid out the live registers in the assembly code. Now, all the tutorials and texts construct interference graphs from here, etc. But instead of that (as I mentioned above), they could look at the alive registers. For example if this was a one `1` register machine, then when the live registers are `{t0, t1}`, we will have to choose a register to spill. I feel this is much simpler than constructing a graph and doing all the other stuff to check if we have to spill registers. I know ignorance is not global (somebody must have thought of this and deemed it not fit), so what am I not seeing here?\n\n========================================\n\nTop Answer:\nMerely thinking in terms of register spillage may be fine for straight-line code, but many programs contain loops. While register efficiency in loops is often more important than in straight line code, a register-spillage model makes it hard to handle situations where a value would need to be live for part of a loop near the end, and remain live until execution reaches some spot near the beginning, but wouldn't need to remain live in the middle. Under a register-spillage model, one may end up with a value being held in a register near the start of a loop, and in a different register near the end. Graph coloring would ensure that both were assigned the same \"color\" [i.e. placed in the same register].\n\n========================================\n\nCode:\n```text\n## ldi: load immediate\n## addr: add registers and store in arg 2\n## store: store memory at offset from stack pointer\n.text\n    main:\n        # live registers: {}\n        ldi    %t0, 12             # t0 = 12\n        # live registers: {t0}\n        ldi    %t1, 8              # t1 = 8\n        # live registers: {t0, t1}\n        addr   %t0, %t1            # t1 = t0 + t1\n        # live registers: {t1}\n        store  -4(%sp), %t1        # -4(%sp) = t1\n        # live registers: {}\n        exit\n```\n\n```text\n1\n```\n\n```text\n{t0, t1}\n```\n\n```text\nx++; x++;\n```\n\n```text\nx+=2\n```\n\n```text\ntcc\n```\n\n========================================\n\nComments:\n- How do you choose which register to spill? t0 or t1? suppose our live register set is {t0} and then {t0,t1} and then {t1} and then {t1,t2} and then {t2}","metadata":{"transformedAt":"2026-08-18T18:32:17.950Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":77,"estimatedTokens":781}}1129{"id":"stack-9392903","source":"stackoverflow","questionId":9392903,"title":"Parser interpreter programming","tags":["algorithm","delphi","parsing","compiler-construction","formula"],"text":"Title: Parser interpreter programming\nTags: algorithm, delphi, parsing, compiler-construction, formula\nSource: Stack Overflow\n\nQuestion:\nI want to program a package that calculates the resulting value of a certain input formula,\n\nI created the parser due to the Shunting-Yard Algorithm (Dijikstra), \nI want to create libraries of the functions that the user will be allowed to use (for ex: `sin()` and `cos()` functions)\nthen I was wondering what my next step should be; so I have some questions:\n\nWhat is more simpler to use, the Shunting Yard Algorithm, or the Recursive-Descent algorithm for parsing the formulas?\n\nwill i reach the work of the interpreter in some stage of my work, and how?\n\nThanks...\n\nPlease note that i am programming it using Delphi\n\n========================================\n\nTop Answer:\nHaving implemented both (and still maintaining systems with both), here is my pro/con list:\n\nShunting Yard:\n\n- code is short\n\n- easy when associated to simple rules/priority tables\n\n- annoying to debug when something goes wrong\n\nRecursive Descent:\n\n- code is longer\n\n- more complex when all you have are simple rules/priorities\n\n- easier to extend or add special-case syntaxes\n\n- relatively simpler to debug (follows a more \"human\" flow)\n\nOr in other words, when you're dealing only with math formulas, Shunting Yard is probably the way to go, but if you feel you may need for more complexity later on, recursive descent might be more flexible/extensible/maintainable and pay back in the long run.\n\nCompiler's compilers (Lex/Yacc, Flex/Bison etc.) would be an obvious third choice, but I don't know any maintained implementation for Delphi, and for simple maths formulas, they're overkill.\n\n========================================\n\nCode:\n```text\nsin()\n```\n\n```text\ncos()\n```\n\n========================================\n\nComments:\n- you may want to give this a try pegtop.net/delphi/components/math/download.htm\n- `[1]` - what does it mean to be \"simple\"? `[2]` - you need to elaborate. We don't have your notes from class!\n- No, it is not a homework, im not anymore in uni.\n- I meant by Simple: simpler to program using recursive methods, or in ohter words, more simple to deal with concerning the internal functions that im gonna use later.\n- \"Dealing with your internal functions\" isn't going to be the hardest part, and it'll be about the same no matter what parsing algorithm you're going to use. If you prefer a recursive implementation you'll probably prefer the Recursive Descent algorithm as it is, well, recursive!\n- may i ask an additional quest? Can the interpreter while executing a script execute a predefined script? as performing a function that is not built in it, but defined by the user?\n- I suppose this is a `how`, not a `if` question. The answer to the `if` is obviously *yes* (`Python` is an interpreted language, and Python can do anything that can be done). The \"how\" is a matter of your design, and definitively too large a subject for a comment. Feel free to ask a new question (the Ask Question button at the top-right) and ask for implementation ideas.\n- Or just look at the JVCL JvInterpreter which includes a simple pascal implementation of just such a thing.\n- dwscript and PascalScript (I like PascalScript better, but I've worked with both) are both open source, one can also look in there. To be perfectly honest for the OP, if you need to ask for implementation details, you're probably not prepared to undertake this *just yet*. Unless you do it for fun and entertainment (and I honestly *see* the entertainment value in designing something like this)\n- What should i do to be prepared? I am reading more about this Pascal Script, but is it compatible with Delphi 2010? As i saw it is only compatible till 2009... Actually it is for work, but i can see fun in this too. is it compatible also with win7,64-bits?\n- Just download the engine, install it and give it a try. There's a learning curve with any scripting engine, you'll need to learn how to make your (compiled) code available in the scripting engine. `dwscript` promises RTTI-based import, but it didn't quite work when I tried it.\n- If Pascal Script has Delphi 2009 packages, they probably work unchanged for all post-2009 32 bit versions of Delphi. They almost certainly don't work for the 64 bit version of Delphi XE2 because all 32 bit assembler code needs to be re-written for 64 bits (and scripting engines naturally contain quite a bit of assembler, to set up calls from the script to compiled code).","metadata":{"transformedAt":"2026-08-18T18:32:17.950Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":72,"estimatedTokens":1128}}1130{"id":"stack-17196113","source":"stackoverflow","questionId":17196113,"title":"Will C++ first gets converted to assembly","tags":["c++","assembly","compiler-construction","standards"],"text":"Title: Will C++ first gets converted to assembly\nTags: c++, assembly, compiler-construction, standards\nSource: Stack Overflow\n\nQuestion:\nI have confusion. I am C++ developer and heard many times that my source code will first gets converted to assembly and then assembly will get converted to machine code. \nBut in one of the video tutorial of assembly language, instructor clearly said, C/C++ code directly gets convert to machine code. (Of course there will be linking and loading there).\n\nI have seen links like this,\nDoes the C++ code compile to assembly codes?\n\nStill I am not able to clarify my doubt. \n\nIf in case, C++ does not gets converted to assembly first, how de-assembler generate assembly code from binary.\n\n========================================\n\nTop Answer:\nAs machine code maps to assembly quite directly, there it is not very practical to analyze the difference. As C++ language stands it only describes behavior, and up to the implementation what it does. It is possible to emit CLI code or java bytecode or whatever too.\n\nIn practice most implementations really go all the way and emit assembly/machine level optimized code at the end. And support emission of assembly source file (.asm, .s) or annotated code/assy/C++ source.\n\n========================================\n\nCode:\n```text\nmov rax, 42\n```\n\n========================================\n\nComments:\n- *\"how de-assembler generate assembly code from binary.\"* This can be done regardless of how the executable was generated. Given a machine code sequence and an instruction set reference you can generate a disassembly.\n- Modern compilers don't produce assembly intermediate code anymore by default. A disassembler just takes the binary and converts the code parts back to human readable assembly representation. Assembly is only a readable form for representing the machine instructions.\n- Please read the link that you provided. The answer lies there: it primarily depends on your compiler\n- That last question does not make sense. It's like asking \"if the text wasn't made by translating something from Dutch to English, then how can you translate it from English to Dutch?\". It doesn't matter where it came from.","metadata":{"transformedAt":"2026-08-18T18:32:17.950Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":36,"estimatedTokens":548}}1131{"id":"stack-62678079","source":"stackoverflow","questionId":62678079,"title":"What differentiates syntax analysis and semantic analysis?","tags":["parsing","compiler-construction","language-agnostic"],"text":"Title: What differentiates syntax analysis and semantic analysis?\nTags: parsing, compiler-construction, language-agnostic\nSource: Stack Overflow\n\nQuestion:\nTo my understanding, Parser is composed of three stages of lexical, syntactic and semantic analysis.\n\nLexical: It will split my input into tokens. Example: `123+100-0` -> `123` `+` `100` `-` `0`\n\nSyntactic: It will look into the tokens and check if they make sense with each other.\n\nThe problem i am having is understanding the final phase \"semantic parsing\" and how it differentiates to the second stage \"syntactic analysis\". To my understanding the final stage \"semantic parsing\" also validates the tokens that has been validated by \"syntactic analysis\" and then prints out the output.\n\n========================================\n\nTop Answer:\nTheoretically, syntactic analysis determines whether or not an instance of the language is \"well formed\" and analyzes its grammatical structure, while semantic analysis analyzes its meaning and whether or not it \"makes sense\".\n\nBasically, syntactic analysis may depend on the types of words, but not their meaning. Semantic analysis depends on the meanings. \"I like green tomatoes\" and \"I fill wiggly thougts\" have the same syntactic structure and are both well-formed, but the latter makes no sense semantically.\n\nThis theoretical distinction is actually a little blurry, because separating a word's \"type\" from its \"meaning\" involves some arbitrary choices.\n\nIn the practice of compiler development, however, the distinction is clear: Syntactic analysis is performed by the parser, driven by the grammar, depending on the types of the tokens. Semantic analysis starts with the actions, written in code, attached to the rules in the grammar. Basically anything that needs to look up an actual identifier or evaluate the actual value of a literal value, is semantic analysis.\n\nSome languages require the results of semantic analysis to be fed back into the parser to help with syntactic analysis. This ruins the theoretical distinction between syntactic and semantic analysis, but practically we just recognize this as a bit of a cheat and continue to refer to the parsing code as syntactic.\n\n========================================\n\nCode:\n```text\n123+100-0\n```\n\n```text\n123\n```\n\n```text\n+\n```\n\n```text\n100\n```\n\n```text\n-\n```\n\n```text\n0\n```\n\n```text\na\n```\n\n```text\n/\n```\n\n```text\nb\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\na\n```\n\n```text\n/\n```\n\n```text\nb\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\na / b\n```\n\n```text\npublic\n```\n\n```text\nclass\n```\n\n```text\nA\n```\n\n```text\nextends\n```\n\n```text\nB\n```\n\n```text\n{\n```\n\n```text\n}\n```\n\n```text\nB\n```\n\n```text\nB\n```\n\n```text\nB\n```\n\n```text\nB\n```\n\n========================================\n\nComments:\n- Welcome to StackOverflow, please read this guide to ask good questions :). What is your request here ?\n- Please read How to Ask - SO is not a discussion forum but lives to collect a Q&A as resource for devs. Your question does not seem to pose any special question.\n- cheers i have re-edited my question to fit the guidelines.\n- Thank you very much i now understand it !","metadata":{"transformedAt":"2026-08-18T18:32:17.950Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":32,"totalLines":164,"estimatedTokens":794}}1132{"id":"stack-5363267","source":"stackoverflow","questionId":5363267,"title":"Does machine-code needs an runtime environment? MoSync SDK","tags":["compiler-construction","runtime","cross-platform","interpreter","vm-implementation"],"text":"Title: Does machine-code needs an runtime environment? MoSync SDK\nTags: compiler-construction, runtime, cross-platform, interpreter, vm-implementation\nSource: Stack Overflow\n\nQuestion:\nCan anyone explain the The Runtime Architecture of MoSync?\n\nThe VM Core isn´t the Problem. I think it´s a virtual machine which is running in the java vm and interprets the code line by line. \n\nBut how is the Recompiler Core working? Is that a kind of Ahead-Of-Time compiler who compiles the app at run in native code? But then I don´t understand the pic. Or is it possible that I need the other modules of the runtime even it´s machine code?\n\nThanks\n\n========================================\n\nTop Answer:\nOne of the little know facts about MoSync is that, its not just built to be good for CPU's,\nits also good a transforming to other languages, for example, one of the MoSync engineers have the platform running in Javascript/HTML 5.0.\n\nDifferent things happen depending on what platform you using, So MoSync transforms to its intermediate language according to what is best for the target platform.\n\nThere are also 4 different flavors of MoSync IL, they are all compatible with each other,\nbut are modeled to advantage of different processor architectures.\n\nThe IL system has 128 registers, 6 of these are global, the rest are local to functions.\n\nBTW, MoSync bytecode is not the same as MoSync IL, the IL being very rich in meta data.\n\nI'd be happy to discuss the finer details or the architecture, just post your questions.","metadata":{"transformedAt":"2026-08-18T18:32:17.951Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":29,"estimatedTokens":379}}1133{"id":"stack-9055546","source":"stackoverflow","questionId":9055546,"title":"the possibility of creating a programming language without pointer aliasing","tags":["java","code-generation","parser-generator","compiler-construction"],"text":"Title: the possibility of creating a programming language without pointer aliasing\nTags: java, code-generation, parser-generator, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have a question for all the programming enthusiasts out here. \nThe pointers has always been there in the programming world. \nLike in C,C++ which has got a explicit pointer support.\nIn java explicit pointer support is not there, but internally the Java system uses pointers. \nCan there be a scope a programming language to be developed not to have pointer aliasing at all, in practical world?\n\n========================================\n\nTop Answer:\nProgramming without Pointer Variables describes using recursively defined data types instead of pointers in algorithms.\n\n The presence of pointer variables in high level\n programming languages constitutes an artifact originally\n introduced to support the representation of\n recursive data structures.\n\n========================================\n\nComments:\n- You mean a language that has pointers, but no possibility of aliasing?\n- An implementation could prohibit referencing the same object in a variety of ways. You might be more interested in purely functional languages--but anything that refers to memory in any way, or keeps a program counter, will use pointers in its implementation.\n- You would need to implement this on a machine without the use of pointers. Since all CPUs I know use use them implicitly if not explicitly, this is unlikely. e.g. the program counter is a pointer.\n- See the `restrict` keyword in C++.\n- Forth doesn't have pointers in the same sense that assembly doesn't: it can treat plain integers as memory addresses.\n- Well... technically, Forth's `pick` will grab a value at an (essentially) arbitrary stack location, which is basically an indirect reference. (Besides `fill` and `erase`; don't recall if those are in the standard or an implementation detail.)\n- @sverre, good to know. I was under the impression that PostScript did not, but am not confident of that.\n- well...i am developing a language which uses stack structure as well. But cant really feel that there can be languages which do not use pointers at all...anyways..what does this statement means? `If your stack based language allows you to interpret a value on the stack as a point on the stack, you could get problems similar to those introduced by pointer aliasing, but that need not be an allowable operation.`\n- @mad_programmer, I meant the same thing that sverre meant about assembly languages and integers but he said it better.\n- Does it keep the calculating time not that bad? or it's just turing complete but slowing something a lot?\n- @l4m2 Since we're not talking about a specific language, it's hard to say anything about performance. There's nothing about avoiding pointer aliasing that complicates performance though -- many common optimizations actually only work if there's no pointer aliasing: blog.regehr.org/archives/1307\n- This is a very interesting paper. Thank you for sharing.\n- The link is unfortunately broken, does someone have a mirror or a backup?","metadata":{"transformedAt":"2026-08-18T18:32:17.951Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":37,"estimatedTokens":777}}1134{"id":"stack-1684542","source":"stackoverflow","questionId":1684542,"title":"C++ compiler - resolving name of a class member","tags":["c++","compiler-construction"],"text":"Title: C++ compiler - resolving name of a class member\nTags: c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nWhen the compiler sees this code:\n\n```\nSomeClass foo;\nint x = foo.bar;\n```\n\nWhat is the process it goes about in retrieving the value of *bar*? I.e. does it look at some data structure representing the class definition? If so is this data structure generated at compile time or runtime?\n\n========================================\n\nTop Answer:\nThe compiler has the address of `foo`. At that address, there is enough space for member variables (`sizeof(SomeClass)`), which probably includes some padding.\n\nIt knows that `bar is at some position in the class (usually the order they were declared, plus some other magic like inheritance), and jumps to that offset.\n\nThat is:\n\n```\nstruct SomeClass\n{\n short s;\n float f;\n int bar;\n char *c;\n}\n\n// pseudo-code:\n&SomeClass.bar == (&SomeClass) + sizeof(short) + sizeof(float);\n```\n\nAt run-time, it gets that data, and assigns it to `x`\n\n========================================\n\nCode:\n```text\nSomeClass foo;\nint x = foo.bar;\n```\n\n```text\nSomeClass\n```\n\n```text\nSomeClass\n```\n\n```text\nSomeClass\n```\n\n```text\nSomeClass foo;\n```\n\n```text\nSomeClass\n```\n\n```text\nint x = foo.bar\n```\n\n```text\nint\n```\n\n```text\nSomeClass\n```\n\n```text\nbar\n```\n\n```text\nfoo\n```\n\n```text\nbar\n```\n\n```text\nx\n```\n\n```text\nSomeClass\n```\n\n```text\nSomeClass\n```\n\n```text\nfoo.bar\n```\n\n```text\nx\n```\n\n```text\nstruct SomeClass\n{\n    short s;\n    float f;\n    int bar;\n    char *c;\n}\n\n// pseudo-code:\n&SomeClass.bar == (&SomeClass) + sizeof(short) + sizeof(float);\n```\n\n```text\nfoo\n```\n\n```text\nsizeof(SomeClass)\n```\n\n```text\nx\n```\n\n```text\nclass Foo\n{\n   int x, y, z;\n   char bar[10];\n   ... etc ...\n}\n```\n\n========================================\n\nComments:\n- Just a note that all the answers below, including my own, should be taken as \"normally, in a typical compiler, this is the sort of thing which goes on\". As with almost everything to do with the implementation of C++, the real answer is, \"it's entirely up to compiler writers, who can do as they please as long as the right answer comes out when the program runs\". What's described below is the most obvious, sensible way of doing it before applying fiendish optimisations.\n- \"The compiler has the address of foo\" - well, usually it has the offset of foo relative to some stack pointer, since foo is an automatic variable.\n- This is technically inaccurate. The compiler does not (usually) assign the foo.bar to x. The compiler generates machine instructions which assign foo.bar to x at runtime.\n- That's what I meant...I'll add at run-time I suppose, though I thought that would be implied.\n- Since SomeClass is a POD, `&foo.bar == &foo + offsetof(SomeClass, bar)` would also work. @OP: Note that the above is a simplified case. When you bring polymorphic classes (with their virtual tables) and optimizing compilers playing with class layout, it gets more complex. See: stackoverflow.com/questions/1129894/&hellip;\n- @GMan: well generally at compile time, all the compiler knows is the offset. Normally the OS will provide the actual address of the stack at runtime. Obviously there exist architectures where the stack is mapped at a fixed virtual (or even physical) address, and so the compiler could know the actual address. But I don't think that should be taken as the norm.\n- Oh, I see what you're saying. I'm in the run-time mindset, but you can edit it to make it more clear if you' like.\n- I don't think I need to edit, it just depends what's understood by \"has\". At compile time the compiler knows how it will calculate the address, and it knows that by runtime, the value will have been calculated and (in unoptimised code) will be in a particular register. So it \"has\" it in that register (meaning, it knows it will be there in the future). I'm just assuming that since the question is all about what information is where during compile time and runtime, the questioner will be interested in the little details that make all the difference between compilers and interpreters.\n- Use `sizeof(int)` in place of your 4's. `int`'s aren't necessarily 4 bytes","metadata":{"transformedAt":"2026-08-18T18:32:17.951Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":22,"totalLines":156,"estimatedTokens":1039}}1135{"id":"stack-12702733","source":"stackoverflow","questionId":12702733,"title":"Writing a compiler in PHP","tags":["php","compiler-construction"],"text":"Title: Writing a compiler in PHP\nTags: php, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nA few years ago I started working on a few ideas about a programming language and as I was so excited about them I just wanted to see how they would works, so I decide to write a very simple compiler for that. As I'm more comfortable and more experienced in PHP, I just took a look at to see if it's possible to write a compiler in PHP and very soon after that I found that yes, it's possible. So I start making that and fortunately everything was okay and now it's working fine.\n\nThose days I just wanted a place to start working on my ideas, and as PHP was fast in development (no need to compile), I had MySQL on my hands also, and debugging was really easy, etc.\n\nNow I want to extend this simple compiler, and that's where I need your advise. My main question is \"Is PHP a right tool for a compiler project?\". Just suppose that I'm not gonna release it publicly, so just think about the PHP abilities to handle the task, not further problems like distribution. \n\nI believe that it has some advantages. It's fast in development, I just edit the code and press the F5 on browser and I had my binary output right after that. I also made a text box there where I could write my simple codes, press the submit and then I had my binary output again. It was also fast to keeping and working on the parsed data in MySQL. But now I'm more worried about the script timeout for example. If it's gonna compile 10,000 lines of source code, it would timeout I guess. I know I can increase the timeout, but still worried about that. Also I know that PHP as a scripting language is (as I heard) 10 times slower rather a compiled-application.\n\nSo, do you think I have to switch-off to the C? (which I'm also okay with that also) ... or do you have any ideas if I could continue with my PHP back-end, but to handle more serious things and without facing critical mistakes?\n\n**Update:**\n\nYes! the project is personal and for fun. You may consider that also!\n\nRegarding application for a PHP-based compiler, yes, it's not a real-world compiler, but imagine if you want to your ideas with others, it would be great if you gave them a web form to write their code, press the button and download the binary code. It's not my goal, however, I just wondering about that.\n\nRegarding Lex/Yacc, my ideas was more about optimizing the final binary code, so I needed something more than just generating a binary code via Lex/Yacc.\n\n========================================\n\nTop Answer:\nPHP is not the ideal environment for writing compilers. Is it doable? Sure. Should you do it? I am vehemently opposed to writing a compiler in a high-level language like PHP. I'm also opposed to unnecessarily reinventing the wheel.\n\nIf it's for fun, I say go for it, but I don't see any practical uses for a PHP-driven compiler at this time.\n\n========================================\n\nComments:\n- I can't quite get my head around why you'd think that running a compiler via a browser is easier than the command line.\n- also, this is the first compiler I've ever heard of that used a database.\n- @Spudley yeah, I stored parsed data in mysql tables, and then working on them via queries ...\n- Also lex/yacc or their GNU equivalents flex/bison\n- @nielsbot I know lex/yacc, but I wanted to experience that, however to see how much my ideas would works. they were mainly about optimization, so I didn't used lex/yacc as I needed something more deeply into generating the final binary ...\n- You can write a compiler in any language. But PHP is a poor choice indeed. Any language without a decent pattern matching support will be a poor choice unavoidably leading to tons of boilerplate code.\n- @SK-logic regex is enough, huh?\n- @Mahdi, what do you mean? Yes, you actually can write a compiler in a modern regex language. You don't need a Turing-complete language to write a compiler. But it won't be a pleasant experience. Or, you're talking about an alternative to pattern matching? Then you're wrong, regexps are not nearly a replacement.\n- @SK-logic could you please write an answer with more explanations? I really want to know more about your opinion. thanks, it would really helps ... :)\n- @SK-logic I actually do everything with regex or some other simple alternatives ... I'd face some difficulties in the future???\n- thanks for mentioning the fun ... yeah, it's somehow for fun and just experiencing new things ... :)\n- I like your comment, thanks! but I think you can write a compiler better than any compilers, no matter you're using C or PHP. I agree with you about the role and abilities of C, however I still think the logic behind the compiler is much more important. C and PHP both have the same things to deal with binary output, at the end ...\n- C is also a very poor choice for writing compilers (no pattern matching, no algebraic data types). It does not matter how fast your compiler is, but it is important how much effort you're ready to waste on implementing it. And before you start arguing, no, GCC is not written in C. GCC is written in a bunch of highly specialised DSLs (which are implemented on top of C).\n- The highly specialized DSL you are talking about ensure the architecture of GCC as a modular **collection** of compilers. The individual compilers themselves were written in C / C++. There are two dozen C compilers some of them aiming to be extremely small and/or to have no dependencies, which are all written in C. I guess you could say that the highly specialized DSL for GCC is more like a very big config file to a C application.\n- @MihaiStancu, you're wrong again. Try reading GCC code, they're using Lisp-like DSLs all the way through. Or, for a more modern approach, see LLVM and Clang, and especially their collection of DSLs implemented in `TableGen` tool. C on its own does not provide any of the specific tools that you'll need for implementing a compiler. Therefore, there is no point in using plain C (or C++) for such a task, unless you want to have some specific fun (like with `tcc`, which started as an obfuscated C contest entry).\n- thank you mentioning about performance requirements! BTW, it's not gonna do anything with PHP, it's just based on PHP :)\n- You should make note of the huge difference between a **compiler** which takes source code all the way to binary machine code versus a transcoder which takes one source code and transforms it into another. Smarty is a transcoder.\n- A compiler transforms one language into another, it's the definition of compiler. A lot of compilers do not go to binary code.\n- hey, thanks! I would dig more on these things as I thought they would never bother me if I'm going with PHP alone. Thanks again, it was really helpful! :)\n- P.S. I really meant *low*--level (in terms of a level of abstraction, not a degree of separation from hardware). PHP is not a high level enough to be a *flexible* language in which new language features can be added using the language itself.\n- I do not see why you would need a more flexible language to write a compiler. Surely you have some great advantages in a very flexible language like Ruby, but as long as you can tokenize a string you can write a compiler.","metadata":{"transformedAt":"2026-08-18T18:32:17.951Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":54,"estimatedTokens":1813}}1136{"id":"stack-1116358","source":"stackoverflow","questionId":1116358,"title":"HTML and Compilers","tags":[".net","html","theory","compiler-construction"],"text":"Title: HTML and Compilers\nTags: .net, html, theory, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nThis question is a more discussion oriented one that a simple problem specific question. \nWriting basic HTML is simple but writing fast light standards based, SEO best practices complaint, all browsers compatible HTML pages is hard and very time consuming. \n\nBut why it hard ? \n\nIn my opinion it hard because of the hundreds of different rules what need to be followed, rules what are hard to remember and even if you remember it hard to merge them together in not contradicting form and the only way to validate you work is by loading it in every browsers you support and validating every scenario.\n\nBut it really looks like a problem we had in the past in other areas of programming, previously before inventions of high level languages writing program in assembly is looks a lot like writing HTML files, you was forced to remember hundred of different rules for performance, correctness security and so on and the only way to validate them was by executing the program. \n\nIn other fields this problem was solved by compilers of high level languages what make a syntax simpler, make performance optimizations, check program syntactic correction before execution. \n\nDon’t you think we need a different simpler language for writing web pages and compiler what could produce browser specific, standard complaint size optimized HTML from this language ?\n\nDo you think it possible to create such languge and compiler ?\n\n========================================\n\nTop Answer:\nPersonally, I believe that with a decent amount of preparation, there is very little difference in writing 'basic' HTML and standards compliant HTML. Even starting from scratch, I don't think it's any harder.\n\nSimilarly, once you've developed a few sites, any rules regarding validation, etc will become second nature\n\n========================================\n\nComments:\n- I think this is a great question and something I've wanted also.\n- What is optimized HTML? Can you give an example of some non-optimized HTML then the same code optimized? Just a small sample.\n- The most simple example of not optimized HTML is a table with a single tr and a single td used as a block element by people not aware about div elements , I encounter this alot. You also can have much more complex scenario for example you could parse the page and extract the smallest subset of CSS what is needed to render the page correctly\n- Yes it is a domain specific language, but do you know if this is something that was tried already, and do you think such domain specific language could help?\n- I started to edit my answer, but I think the DSL may already exist - it's called \"HTML\".\n- HTML is the most productive thing we can achive ?\n- In general, yes. In more specific circumstances, I think we can do better. But HTML is very general and describes a very large variety of pages. I have the feeling (just a feeling, no math behind it) that such a DSL would be equivalent to HTML - one to one mapping.\n- Did you know what exactly same thing was said by assembly programmers ? :)\n- I really struggle to see any comparison between HTML and assembly. HTML is a markup language - it only describes how a page/document should appear. Any performance issues that could be directly related to the HTML are performance issues in the rendering of the content. In the case of browsers, this is down to the specific rendering engine.\n- First of all rendering performance affected by size of the DOM smaller html document will render faster , it something compiler could optimize for to produce most compact form of HTML from user markup that it self don't nesesery need to be compact it self. Also compiler reside on a server it could produce version of HTML what is best for some particalar version of the browser\n- @dxbmatt: HTML doesn't define how a document should appear, that's CSS. HTML defines what the document *means*. (That's kind of the most important rule to keep in mind when trying to write standards-compliant HTML.)\n- Sorry but i did not get why HTML and assembly compilers are different ? Compilers don't change algorithms this correct but they do help you optimize code by following best practice rules what usaly hand writen. Optimizing HTML is more like method inlining and placing variables into registers instead of main memory and not like algorithm selection. What is a best solution for this problem from the ones available ?\n- We can't solve browser inconsistences, but it possible to make the procces of fixing your HTML/CSS to compensate for them automatic\n- Well, yes and no. Unfortunately, browser vendors keep finding new and exciting ways of being inconsistent.\n- In theory, GWT provides a means for write-once, run anywhere app development where your original Java code is compiled to the best available, most compatible javascript possible on demand.\n- WIKI language is not exactly what I am after, in wiki language you can produce only wiki articles it very spefic to one purpose I would like to have something what is much more general purpose, I also don't want to transfer any logic to the client I want browser to recive just plain HTML , it just be the beter cleaner HTML.\n- I don't want just to produce standart complient HTML. I want compiler that is aware of all browser specific quirks and produce HTML that is correctly displayed in all browsers this a tedius part in HTML production and what that could be automated.\n- No, it could not be. You'd have to define a meta-language that desribes webpages and a it and the \"compilers\" that converts it to HTML would suffer from exactly the same problems that currently HTML and browsers suffer - with the additional complication of a nextra layer that makes debugging harder, and which is necessarily always out of date in catching up with browser quirks.\n- I am sorry but I probably missing something in you post, the problem with current state of HTML is what standard is not implemented equally in all browsers, but people are capable to produce HTML what is viewed correctly in all browsers relevant to them, they do it by using accumulated knowledge about different browsers behavior. Why we can't take this knowledge and make a automatic compiler from it ? I see it as following flow meta-language = > Compiler => quirksAwareHTML , why it will encounter the same problems ?\n- Apart from debugging and the catching-up-with-browser quirks problem: the HTML/CSS standard is not *interpreted* the same in all browsers. How would you ensure that this fictional meta-language is interpreted the same in all compilers (or different versions of the compiler)? Humans don't produce quirks aware HTML by following strict rules, they do it by trial-and-error and seeing if the result \"looks OK\" after each change. The fundamental problem of HTML is that \"looks OK\" is not a formally testable condition.\n- If meta-language is well defined and has a reference implementation it actually relatively easy to produce compilers what interpret it in the same way,. HTML having ill defined standard so every browser interprets it differently but other programming languages avoid this problem. Humans rely on the trial and error because set of rules is too large for any one to hold it in his head , but actually such strict rules do exist some of them listed in QuirksMode site compiler don’t need to relay on trial and error, it possible to evaluate all rules what are available and produce correct result\n- Your meta-language will have either no, or a worthless default implementation, since its target are ultimately the quirky browsers. Other programming languages are better defined than HTML+CSS because their target's behaviour is, while HTML describes layout, which is NOT well-defined because it involved aesthetic judgements.","metadata":{"transformedAt":"2026-08-18T18:32:17.951Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":52,"estimatedTokens":1966}}1137{"id":"stack-21475744","source":"stackoverflow","questionId":21475744,"title":"confusion between compiler and interpreter?","tags":["c","compiler-construction","error-handling","mingw","interpreter"],"text":"Title: confusion between compiler and interpreter?\nTags: c, compiler-construction, error-handling, mingw, interpreter\nSource: Stack Overflow\n\nQuestion:\nI read the following documentation about compiler and interpreter somewhere :-\n\n A compiler searches all the errors of a program and lists them. If the program is error\n free then it converts the code of program into machine code and then the program can be\n executed by separate commands.\n\n \n An interpreter checks the errors of a program statement by statement. After checking \n one statement, it converts that statement into machine code and then executes that\n statement. The process continues until the last statement of program occurs.\n\nMy doubt came from the following code:\n\n```\nint main()\n{\n printf(\"hello\")\n scanf(\"%d\",&j);\n return 0;\n}\n```\n\nI am using `MINGW GCC` compiler. When I compile the above code following things happen:\n\nFirstly I get the error\n\n```\nerror: expected ';' before 'scanf()'\n```\n\nAfter I correct the above error then I get the second error\n\n```\nerror: 'j' undeclared (first use in this function)\n```\n\nSo I wanted to know that why both the errors are not listed at one time?\n\n========================================\n\nTop Answer:\nThe documentation you are quoting is a bit misleading.\n\nBoth compilers and interpreters aim to report as much errors as possible but finding \"all the errors of a program\" is impossible. (cf. Halting Problem)\n\nSo, a compiler doesn't \"search for errors\", rather, it *parses* your source into a tree representation (AST) and then tries to *transform* that tree into another \"tree\" for another language (say, machine code).\n\nAn interpreter also parses your code but the transformation is done in parts at runtime.\n\nSo in your example, the missing semicolon causes the parser to fail so the compiler doesn't even get to the compilation stage (which reports the second error).\n\nAs others have said, the distinction between compilers and interpreters is not that clear anymore. Similar techniques are used, interpreters often compile to machine code, etc.\n\n========================================\n\nCode:\n```text\nint main()\n{\n    printf(\"hello\")\n    scanf(\"%d\",&j);\n    return 0;\n}\n```\n\n```text\nerror: expected ';' before 'scanf()'\n```\n\n```text\nerror: 'j' undeclared (first use in this function)\n```\n\n```text\nMINGW GCC\n```\n\n```text\nint j\nprintf(\"Enter something: \");\nscanf(\"%d\", &j);\n```\n\n```text\nscanf\n```\n\n```text\nj\n```\n\n```text\nscanf\n```\n\n```text\nj\n```\n\n```text\nscanf\n```\n\n```text\nscanf(\"%d\",&j)\n```\n\n========================================\n\nComments:\n- Not all syntax errors can be recovered from. The mistakes may seem obvious to you as a human, but to the compiler the first error makes a nonsense of the subsequent code.\n- @PaulR : you mean to say that in this case the `compiler` work as `interpreter`\n- The most logical explanation I can muster: The second line could not begin independent statement compilation (and therefore detect the bogus symbol `j`) until the first line error was addressed. The missing semicolon made the termination of the first statement undefined and therefore unable to coalesce to a logical starting point of the second. Conjecture, yes, but its probably accurate.\n- @WhozCraig : but such termination occurs in case of `interpreter`\n- That quote is complete nonsense. An interpreter doesn't convert anything to machine code, or to any other code - the interpreter itself performs the computation described by the input program. A compiler doesn't necessarily emit *machine* code. The error checking policy is orthogonal: A compiler can leave run-time checks in the code; an interpreter can perform checks before beginning to execute the very first instruction -- in both cases they are constrained by which checks the language allows.\n- Maybe this wikipedia article might help you.\n- It's good if MINGW GCC does not show you next errors until you fix error with missing \";\". Because in this case you would get lot of errors every one of which has nothing to do with real error (because scope is not closed and, for instance, in this case scant is just in wrong place syntactically)\n- Whatever you're reading, please, stop doing it now and throw the thing away, because it's obviously a pile of crap.\n- you mean to say that in this case both `compiler` and `interpreter` works simultaneously\n- @kevingomes No. GCC is a compiler, and not an interpreter (well, technically it should contain an interpreter for a small subset of C know as \"constant expressions\", but that's like 1% of its functionality). This has nothing to do with how it reports errors. Your reasoning is based on a bullshit definition.\n- @kevingomes simultaneously?? *What interpreter?* There is none in the toolchain that you ran. At this point I'm thinking that article would find better use cut up into 4\" squares and placed on the commode for emergencies.\n- Note: Grattis p&#229; 100k!\n- Typically \"errors of a program\" is taken to mean \"illegal according to the language definition\", and membership in the language is decidable for virtually all (non-pathological) languages. Not halting is legal in almost all languages, and those few languages that want to ensure halting (e.g. total functional programming) circumvent the halting problem by sacrificing Turing completeness.\n- @delnan: sure there are type checkers for all kinds of languages. It is confusing to say that a compiler \"searches for errors\". A compiler transforms from one language into another. If the language is typed the compiler may do type checking and find some errors before running the program. But an interpreter does this as well.\n- I was referring to the \"but finding all the errors of a program is impossible (cf. Halting problem)\" part. I'm completely with you that error reporting is orthogonal to the compiler/interpreter distinction.","metadata":{"transformedAt":"2026-08-18T18:32:17.951Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":130,"estimatedTokens":1458}}1138{"id":"stack-30463248","source":"stackoverflow","questionId":30463248,"title":"Can a C implementation use length-prefixed-strings \"under the hood\"?","tags":["c","compiler-construction","compiler-optimization","c-strings","null-terminated"],"text":"Title: Can a C implementation use length-prefixed-strings \"under the hood\"?\nTags: c, compiler-construction, compiler-optimization, c-strings, null-terminated\nSource: Stack Overflow\n\nQuestion:\nAfter reading this question: What are the problems of a zero-terminated string that length-prefixed strings overcome? I started to wonder, what exactly is stopping a C implementation from allocating a few extra bytes for any `char` or `wchar_t` array allocated on the stack or heap and using them as a \"string prefix\" to store the number `N` of its elements?\n\nThen, if the `N`-th character is `'\\0'`, `N - 1` would signify the string length.\n\nI believe this could mightily boost performance of functions such as `strlen` or `strcat`.\n\nThis could potentially turn to extra memory consumption if a program uses non-`0`-terminated `char` arrays extensively, but that could be remedied by a compiler flag turning on or off the regular \"count-until-you-reach-`'\\0'`\" routine for the compiled code.\n\nWhat are possible obstacles for such an implementation? Does the C Standard allow for this? What problems can this technique cause that I haven't accounted for?\n\nAnd... has this actually ever been done?\n\n========================================\n\nTop Answer:\nThen, if the `N`-th character is `'\\0'`, `N - 1` would signify the string length.\n\nActually, no, and that's why this suggestion cannot work.\n\nIf I overwrite a character in a string with a 0, I have effectively truncated the string, and a subsequent call of `strlen` on the string *must* return the truncated length. (This is commonly done by application programs, including every scanner generated by (f)lex, as well as the `strtok` standard library function. Amongst others.) \n\nMoreover, it is entirely legal to call `strlen` on an interior byte of the string.\n\nFor example (just for demonstration purposes, although I'll bet you can find code almost identical to this in common use.)\n\n```\n/* Split a string like 'key=value...' into key and value parts, and\n * return the value, and optionally its length (if the second argument\n * is not a NULL pointer). \n * On success, returns the value part and modifieds the original string\n * so that it is the key.\n * If there is no '=' in the supplied string, neither it nor the value\n * pointed to by plen are modified, and NULL is returned.\n */\nchar* keyval_split(char* keyval, int* plen) {\n char* delim = strchr(keyval, '=');\n if (delim) {\n if (plen) *plen = strlen(delim + 1)\n *delim = 0;\n return delim + 1;\n } else {\n return NULL;\n }\n}\n```\n\n========================================\n\nCode:\n```text\nchar\n```\n\n```text\nwchar_t\n```\n\n```text\nN\n```\n\n```text\nN\n```\n\n```text\n'\\0'\n```\n\n```text\nN - 1\n```\n\n```text\nstrlen\n```\n\n```text\nstrcat\n```\n\n```text\n0\n```\n\n```text\nchar\n```\n\n```text\n'\\0'\n```\n\n```text\nmalloc\n```\n\n```text\ntypedef struct {\n    size_t len;\n    char* buf;\n} String;\nsize_t my_strlen(String*);\n...\n```\n\n```text\nmy_strlen\n```\n\n```text\nmy_strcpy\n```\n\n```text\n/* Split a string like 'key=value...' into key and value parts, and\n * return the value, and optionally its length (if the second argument\n * is not a NULL pointer). \n * On success, returns the value part and modifieds the original string\n * so that it is the key.\n * If there is no '=' in the supplied string, neither it nor the value\n * pointed to by plen are modified, and NULL is returned.\n */\nchar* keyval_split(char* keyval, int* plen) {\n  char* delim = strchr(keyval, '=');\n  if (delim) {\n    if (plen) *plen = strlen(delim + 1)\n    *delim = 0;\n    return delim + 1;\n  } else {\n    return NULL;\n  }\n}\n```\n\n```text\nN\n```\n\n```text\n'\\0'\n```\n\n```text\nN - 1\n```\n\n```text\nstrlen\n```\n\n```text\nstrtok\n```\n\n```text\nstrlen\n```\n\n```text\nstruct string\n{\n  char *data;\n  size_t len;\n};\n```\n\n```text\nstruct\n```\n\n```text\nstruct\n```\n\n```text\nstrcat\n```\n\n```text\nns_output(my_file, \"This is a test\"); // ns -- new string\n```\n\n```text\nMAKE_NEW_STRING(this_is_a_test, \"This is a test\");\nns_output(my_file, this_is_a_test);\n```\n\n```text\n#define ns_output(f,s) (ns_output_func((f),(s).stringref))\n```\n\n```text\nchar\n```\n\n```text\nfopen\n```\n\n```text\nMAKE_NEW_STRING\n```\n\n```text\nthis_is_a_test\n```\n\n```text\nstringref\n```\n\n```text\nvoid*\n```\n\n```text\nstringref\n```\n\n```text\nstringref\n```\n\n========================================\n\nComments:\n- I think it is bad idea to mix data with metadata.\n- There are no strings in C. Just arrays of chars. That pretty much stops you from doing that kind of optimization.\n- @EvdzhanMustafa so where does that leave the \"end of string marker\"?\n- If you decide this implementation is better than standard one for your application - just implement it. No obstacles here.\n- @RedX: there are. That's what `'\\0'`-terminated `char` arrays are called. They even have about 5 SO tags dedicated to them =)\n- `struct StringWithExtraData { size_t curlen; size_t totalbytes; char *data };`\n- The Most Expensive One-byte Mistake queue.acm.org/detail.cfm?id=2010365\n- I personally believe the problem is that people today still stick with C, instead of moving to C++ or any other suitable higher level language. Why would you like to implement something similar to std::string in C, when there is already std::string in C++?\n- @Mints97 A char array terminated by `\\0` is just a convention. If you want to use `str*` functions your char array must that convention. But nobody is stopping you from creating your own set of functions that follows a different paradigm.\n- \"If I overwrite a character in a string with a 0, I have effectively truncated the string\"... geez, you're right! I am an idiot. Well, down the drain goes my \"brilliant\" idea XD Thanks!\n- I would favor having a variable-length prefix, a function which, given a pointer to the start of the prefix, would produce a structure that included a special header, a pointer to the text, the size of the allocation, and the length of the text within the allocation, and a function which given a pointer to the start of the prefix and a new length, would update the length of the stored string appropriately. A function like `strcat` would pass each argument to the \"get string info\" function, determine how much of the source string would fit, copy the data, compute the new length, and...\n- ...call the function to update the length stored in the destination string. If the aforementioned string-info structure's header was distinct from any other prefix, functions that would expect string pointers could just as happily accept pointers to the aforementioned structures. If code has a long string and wishes to e.g. concatenate a portion of it onto another, it could build a structure identifying the portion and pass that structure as the \"source string\" argument to the same string-concatenation function as would be used to concatenate an entire string.","metadata":{"transformedAt":"2026-08-18T18:32:17.951Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":37,"totalLines":244,"estimatedTokens":1693}}1139{"id":"stack-4112317","source":"stackoverflow","questionId":4112317,"title":"Easy way to get function prototypes?","tags":["c++","compiler-construction"],"text":"Title: Easy way to get function prototypes?\nTags: c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nA friend and I were discussing imaginary and real languages and a question that came up was if one of us wanted to generate headers for another language (perhaps D which already has a tool) what would be an easy and very good way to do this?\n\nOne of us said to scan C files and headers and ignore function bodies and only count the braces within to figure out when a function is finished. The counter to that was typedefs, defines (which braces but defines were considered as a trivial problem) and templates + specialization.\n\nAnother solution was to read binaries produce, not the actual exe but the object files the linker uses. The counter to that was the format and complexity. None of us knew anything of any object format so we couldnt estimate (we were thinking of gcc and VS c++).\n\nWhat do you guys think? Which is easier? This should be backed up with reasonable logic and fact.\n\nIf someone can link to a helpful project, one that parses C files/headers and outputs it or one that reads in elf data and displays info in an example project would be useful. I tried googling but I didnt know what it would be called. I found libelf but at this moment I couldn't get it to compile. I might be able to soon.\n\n========================================\n\nTop Answer:\nctags' output is quite easy to read/parse\n\n========================================\n\nCode:\n```text\nvoid foo() {}\n\nint main()\n{\n    foo();\n}\n```\n\n```text\n<?xml version=\"1.0\"?>\n<CLANG_XML>\n  <TranslationUnit>\n    <Function id=\"_1D\" file=\"f2\" line=\"1\" col=\"6\" context=\"_2\"\n              name=\"foo\" type=\"_12\" function_type=\"_1E\" num_args=\"0\">\n    </Function>\n    <Function id=\"_1F\" file=\"f2\" line=\"3\" col=\"5\" context=\"_2\"\n              name=\"main\" type=\"_21\" function_type=\"_22\" num_args=\"0\">\n    </Function>\n  </TranslationUnit>\n  <ReferenceSection>\n    <Types>\n      <FunctionType result_type=\"_12\" id=\"_1E\"/>\n      <FundamentalType kind=\"int\" id=\"_21\"/>\n      <FundamentalType kind=\"void\" id=\"_12\"/>\n      <FunctionType result_type=\"_21\" id=\"_22\"/>\n      <PointerType type=\"_12\" id=\"_10\"/>\n    </Types>\n    <Files>\n      <File id=\"f2\" name=\"test.cpp\"/>\n    </Files>\n  </ReferenceSection>\n</CLANG_XML>\n```\n\n```text\nclang\n```\n\n```text\ntest.cpp\n```\n\n```text\nclang++ -Xclang -ast-print-xml -fsyntax-only test.cpp\n```\n\n```text\ntest.xml\n```\n\n========================================\n\nComments:\n- @George Edison: I feel like poking to tell you i'm having some fun with the compiler-design tag ;)\n- Thanks a lot! this is very helpful! I spent the last 30mins looking into things and this blows them all out of the water. I didnt mean to get the info from an elf but from a object file which i just mention elf because it looked like it could have this info in the symbol table but i didnt know much about it (one def said name and made no mention if it was managed or not).\n- Do you know if there is a C++ or .NET way to load this data? i think i may build up a class using C# and load the data in via with XML serialization and do my dirty work in there.\n- @acidzombie24: You are welcome. If writing in C++ I would use clang libraries directly because clang is in C++ and API is clean and relatively easy to use. If you plan to use C# loading XML is definitely easier.\n- @vitaut: I am a bit nervous about the learning curve for the API. With XML at least i can see the output and it is in an understandable format\n- @vitaut: how did you find these flags? for the life of me i cant find them anywhere (man pages, website, google, etc)\n- A document of the possible XML AST produce would be helpful as well. Its way more complex then your output.\n- @acidzombie24: About `-Xclang` option I have found out from `clang++ --help`. About the rest - using `clang++ -Xclang --help test.cpp` (note that `test.cpp` should exist) but I am sure there is an easier way.\n- I'm a bit confused. Do i use IDL to generate C headers? Although useful i want it the other way around.","metadata":{"transformedAt":"2026-08-18T18:32:17.951Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":86,"estimatedTokens":1009}}1140{"id":"stack-27390654","source":"stackoverflow","questionId":27390654,"title":"Memory: how does the compiler choose where to store variables?","tags":["c++","c","memory","compiler-construction","runtime"],"text":"Title: Memory: how does the compiler choose where to store variables?\nTags: c++, c, memory, compiler-construction, runtime\nSource: Stack Overflow\n\nQuestion:\nGiven two functions, func1 and f2, with the following signatures:\n\n```\nvoid func1(){\n int baba = 12345;\n // printf the value of baba here\n}\n\nvoid f2(){\n int loo;\n //printf the value of loo here\n}\n```\n\n...if I run my int main, which only has func1 then f2:\n\n```\nint main(){\n func1();\n f2();\n}\n```\n\n...then the printed value of both baba and loo will be 12345. So my question is as follows:\n\nIs this defined behaviour, or just something erroneous that my machine does?\n\nIf this isn't some erroneous thing my computer did, can you explain why the compiler chooses to store `loo` in the same address as `baba?`\n\n**EDIT:** I guess I should ask, if I have these EXACT two functions, will baba and loo have the same value on ANY machine?\n\nI understand loo's value is the result of baba's leftover bits, and I understand that (on my machine, at least) the stacks of both are being laid out such that loo overlaps onto baba's old territory. *Is it true* that every machine would lay these two function stacks out in such a manner that baba and loo overlap? Using these two functions exactly as written, that is...\n\n========================================\n\nCode:\n```text\nvoid func1(){\n    int baba = 12345;\n    // printf the value of baba here\n}\n\nvoid f2(){\n    int loo;\n    //printf the value of loo here\n}\n```\n\n```text\nint main(){\n      func1();\n      f2();\n}\n```\n\n```text\nloo\n```\n\n```text\nbaba?\n```\n\n```text\nf2()\n```\n\n```text\nlolo\n```\n\n```text\nfunc1()\n```\n\n```text\nf2()\n```\n\n========================================\n\nComments:\n- Think about how temporary local variables can be allocated on the stack and then it should be obvious.\n- 1. Its UB. 2. Its an erroneous thing in the code, not the compiler. `loo` is indeterminate. Evaluating it invokes UB. Why the same address choice was used is implementation-dependent.\n- If an interrupt happens between calling `func1()` and `f2()` the previous `12345` on the (below stack pointer) stack would not survive.\n- I think that this is undefined behaviour. The way that it works is that when you call func1 the stack is pushed and the value 12345 is put assigned to an address in the new stack frame. When you leave func1 the stack is popped back to the calling stack frame. When you call f2 the stack is again pushed and as you have the same number of parameters in both calls (none) the location in memory of loo is now where baba was, this bit of memory has not been wiped and contains the same data.\n- @PaulR can you explain? I don't know much assembly so maybe I'm missing something.I know that during the creation/destruction of func1(), the space assigned to baba gets the int literal \"12345\" written into it. When loo is created, it's taking that same address space. I understand that's why the value is there, but I was wondering if it's merely coincidence that loo has the same address space. I guess a better question would be, is loo guaranteed to always occupy that space (in THESE given functions, on every platform)\n- Thank you all very much for the insight!!!\n- I am puzzled why this question is marked as answered and linked to a different question. If it has been answered, it should be to a similar question.\n- @user3344003 Yeah, I was wondering the same. These are two very different questions, as mines about assembly and portability, whereas the linked one is about stacks and scope\n- I use \"undefined\" as in \"Undefined Behaviour\" i.e. the c++ standard does not define a behaviour. But undefined/indeterminate does not mean that it's random. So I dare to maintain: *\"It works only in this specific case on this compiler with the same compiling switches with this operating system\"*","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":8,"totalLines":96,"estimatedTokens":950}}1141{"id":"stack-3220320","source":"stackoverflow","questionId":3220320,"title":"Question on lexical analysis","tags":["c","algorithm","lexical-analysis","compiler-construction"],"text":"Title: Question on lexical analysis\nTags: c, algorithm, lexical-analysis, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI am reading the dragon book. Quoting the text from the book (3.1.4 Lexical errors, Pno 114)\n\n It is hard for a lexical analyzer to\n tell, without the aid of other\n components, that there is a\n source-code error. For instance, if\n the string `fi` is encountered for the\n first time in a C program in the\n context:\n\n```\nfi ( a == f(x) ) ...\n```\n\n \n a lexical analyzer cannot tell whether\n `fi` is a misspelling of the keyword\n `if` or an undeclared function\n identifier. Since `fi` is a valid\n lexeme for the token id, the lexical\n analyzer must return the token `id` to\n the parser and let some other phase of\n the compiler - probably the parser in\n this case - handle an error due to\n transposition of the letters.\n\nI am bit confused after reading this. My understanding was lexical analyser starts processing the text from left to right and return tokens whenever the pattern matches. So for a language where `if` is the keyword to match, how can `fi` match? \n\nAny thoughts?\n\n========================================\n\nTop Answer:\nYou must make a distinction between syntax analysis and lexical analysis.\n\nThe task of lexical analysis is to convert a sequence of characters into a string of tokens. There can be various types of tokens, ex IDENTIFIER, ADDITION OPERATOR, END OF STATEMENT OPERATOR, etc. Lexical analysis can only fail with an error if it encounters a string of text which doesn't correspond to any token. In your case `fi ( a == f(x) ) ...` would translate to ` ` .....\n\nOnce a string of tokens have been generated, syntax analysis is performed. This typically involves constructing some sort of syntax tree from the tokens. The parser is aware of all the forms of valid statements that are allowed in the language. If the parser cannot find a syntax rule allowing the above sequence of tokens, it will fail.\n\n========================================\n\nCode:\n```text\nfi ( a == f(x) ) ...\n```\n\n```text\nfi\n```\n\n```text\nfi\n```\n\n```text\nif\n```\n\n```text\nfi\n```\n\n```text\nid\n```\n\n```text\nif\n```\n\n```text\nfi\n```\n\n```text\nfi ( a  == f(x) );\n```\n\n```text\nif\n```\n\n```text\nid\n```\n\n```text\nint a = 42;\nif (a == 42)\n    puts(\"ok\");\n```\n\n```text\nint a = 42;\nfi (a == 42)\n    puts(\"ok\");\n```\n\n```text\nint a = 42;\nfi(a == 42);\nputs(\"ok\");\n```\n\n```text\nif\n```\n\n```text\nfi\n```\n\n```text\nfi\n```\n\n```text\nint\n```\n\n```text\nvoid\n```\n\n```text\nfi ( a == f(x) ) ...\n```\n\n```text\n<IDENTIFIER> <LEFT BRACKET> <IDENTIFIER> <EQUALITY> <IDENTIFIER> <LEFT BRACKET> <IDENTIFIER> <RIGHT BRACKET> <RIGHT BRACKET>\n```\n\n========================================\n\nComments:\n- Ahh.. it makes sense now. Thanks","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":21,"totalLines":141,"estimatedTokens":679}}1142{"id":"stack-7538431","source":"stackoverflow","questionId":7538431,"title":"Compiler requirement for override and final","tags":["c++","compiler-construction","overriding","virtual","c++11"],"text":"Title: Compiler requirement for override and final\nTags: c++, compiler-construction, overriding, virtual, c++11\nSource: Stack Overflow\n\nQuestion:\nI can remember that during the discussion about general attributes which finally lead to the new contextual keywords `override` and `final` it was suggested that compiler support for these ore some **may be optional** (I guess it would read in the standard text as \"behavior is implementation-specific). But I can not find any trace about this optionality in the FDIS and the corrections afterwards.\n\nBut since not finding it is not proof, I have to ask: **Is the support** as described in 2.11p2, 9.2 and 10.3 of the FDIS **for `override` and `final` obligatory for a conforming compiler?**\n\nIs it for example required that a *conforming compiler* rejects\n\n```\nclass Foo {\n void func() override; // Error: not virtual, 9.2p9\n};\n```\n\nOr is it still conforming by ignoring *override*?\n\n========================================\n\nTop Answer:\nThe *use* of `override` and `final` is optional for the programmer, but nowhere does it say that the compiler can ignore them.\n\nThis might have been different for the earlier proposals which used attributes instead of keywords. Attributes leave a lot more freedom to the compiler.\n\n========================================\n\nCode:\n```text\nclass Foo {\n    void func() override; // Error: not virtual, 9.2p9\n};\n```\n\n```text\noverride\n```\n\n```text\nfinal\n```\n\n```text\noverride\n```\n\n```text\nfinal\n```\n\n```text\noverride\n```\n\n```text\noverride\n```\n\n```text\noverride\n```\n\n```text\nfinal\n```\n\n```text\noverride\n```\n\n```text\nfinal\n```\n\n========================================\n\nComments:\n- \"optional for the programmer\"... nicely put :-)","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":79,"estimatedTokens":427}}1143{"id":"stack-22890647","source":"stackoverflow","questionId":22890647,"title":"Is OCaml's type system static?","tags":["compiler-construction","static","ocaml","interpreter","type-systems"],"text":"Title: Is OCaml's type system static?\nTags: compiler-construction, static, ocaml, interpreter, type-systems\nSource: Stack Overflow\n\nQuestion:\nIt is mentioned in various sources that OCaml has a static and strong type system, and also that it is an interpreted language.\n\nWikepedia states that static type checking is done at compile time. Now if OCaml is not a compiled language, then how does static type checking apply to it?\n\nIt is also mentioned in some places that OCaml has a byte code compiler and a VM. Then how does OCaml figure when to behave as an interpreted language and when to behave as a compiled language?\n\n========================================\n\nTop Answer:\nStatic type checking occurs *before* any code is run. This is the case in OCaml, its typechecker runs before the interpreter or the compiler kicks in, and you get type errors if something is wrong.\n\nDynamic type checking occurs *during runtime*, every time the variable in question is used. For example - Racket or Scheme. In those, you can get a type error during program's execution.\n\n========================================\n\nComments:\n- 'Compile time' is a loose term in programing that general means 'not runtime', I.E. it is done through static analysis of the source code. More broadly speaking, a static type system simple means that every variable is declared to have a type, and the type of the variable can never change (beyond polymorphic constraints).\n- A good question would link to these “various sources”.\n- Define \"interpreted\". It's a pretty vague thing, you won't be able to distinguish compiled and interpreted language implementations easily.\n- And in order to blow up your mind a bit further, take a look at Typed Racket. It does the type checking and lowering pretty early, while still keeping a dynamically typed backend. Another similar example is TypeScript.\n- OCaml bytecode interpreter does not \"reproduce the behaviour of the compiler\". It simply executes the bytecode produced by a compiler. And the bytecode is, of course, already stripped from the type information.\n- @SK-logic you are confusing the top-level (interpreter) and the byte code virtual machine.\n- @LeoWhite, Jeffrey clearly meant something else but REPL, because REPL does not \"reproduce the behaviour of the compiler\", it is exactly the same compiler.\n- For each expression you type in REPL, the REPL does a type analysis, then (if the type is OK) it evaluates the expression. That's all I'm trying to say. My point is that the types are just as strong and static as in compiled code. It's a good point about the bytecode interpreter (or VM). It would be helpful to include in the overview I was trying to write.","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":29,"estimatedTokens":672}}1144{"id":"stack-9724476","source":"stackoverflow","questionId":9724476,"title":"Java Compiler Object Code (Byte Code?)","tags":["java","c","gcc","compiler-construction","linker"],"text":"Title: Java Compiler Object Code (Byte Code?)\nTags: java, c, gcc, compiler-construction, linker\nSource: Stack Overflow\n\nQuestion:\nA typical C program would be compiled, assembled, linked and loaded. I know that Java works with the JVM and produces byte code. Would it be possible to connect both \"object files\" using the linker? For example, a C program and a C++ program can be used together by playing with the linker and linking both object files.\n\nCan the same be done between C and Java?\n\n========================================\n\nTop Answer:\nThis might be possible if you compile the C code to JVM bytecode, not to native code, so you would 'link' at the JVM level, not via a native object linker.\n\nSee http://en.wikipedia.org/wiki/Java_virtual_machine#C_to_bytecode_compilers\n\n========================================\n\nCode:\n```text\njavac\n```\n\n```text\ngcj\n```\n\n========================================\n\nComments:\n- Blog post with a couple of implementations: depth-first.com/articles/2006/10/16/&hellip;\n- Of course, C translated to bytecodes is not really C -- many legal things in C are not legal in Java bytecode.","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":32,"estimatedTokens":281}}1145{"id":"stack-1592347","source":"stackoverflow","questionId":1592347,"title":"Compilers targeting .pyc files?","tags":["python","compiler-construction","pyc"],"text":"Title: Compilers targeting .pyc files?\nTags: python, compiler-construction, pyc\nSource: Stack Overflow\n\nQuestion:\nOut of curiosity, are there many compilers out there which target `.pyc` files?\n\nAfter a bit of Googling, the only two I can find are:\n\n- unholy: why_'s Ruby-to-pyc compiler\n\n- Python: The PSF's Python to pyc compiler\n\nSo… Are there any more?\n\n(as a side note, I got thinking about this because I want to write a Scheme-to-pyc compiler)\n\n(as a second side note, I'm not under any illusion that a Scheme-to-pyc compiler would be *useful*, but it would give me an incredible excuse to learn some internals of both Scheme and Python)\n\n========================================\n\nTop Answer:\n\"I want to write a Scheme-to-pyc compiler\".\n\nMy brain hurts! Why would you want to do that? Python byte code is an intermediate language specifically designed to meet the needs of the Python language and designed to run on Python virtual machines that, again, have been tailored to the needs of Python. Some of the most important areas of Python development these days are moving Python to other \"virtual machines\", such as Jython (JVM), IronPython (.NET), PyPy and the Unladen Swallow project (moving CPython to an LLVM-based representation). Trying to squeeze the syntax and semantics of another, very different language (Scheme) into the intermediate representation of another high-level language seems to be attacking the problem (whatever the problem is) at the wrong level. So, in general, it doesn't seem like there would be many .pyc compilers out there and there's a good reason for that.\n\n========================================\n\nCode:\n```text\n.pyc\n```\n\n```text\nnonlocal\n```\n\n========================================\n\nComments:\n- I'm fairly sure `unholy` only exists because _why is crazy. (Crazy awesome to be precise.)\n- If you want to generate code that executes in the Python runtime, by far the better approach is to generate Python code and compile it, not try to generate Python bytecode directly.\n- @Glenn agreed – a Scheme-to-Python compiler would be much more sensible… But I don't want to be sensible here, I want to learn about Python's internals (and what it takes to implement Scheme)\n- Why would I want to do that? What's *not* to like about it?! I get to learn about the internals of both Scheme AND Python (albeit at different levels… But still)!\n- But, more seriously, you've got a good point that I didn't consider: pyc, unlike general bytecode (.swf or .class?), is designed only to suit Python… So it doesn't make much sense to target it.\n- At least, it doesn't make sense if your goal is to produce something useful :P\n- Um, he already has CPython in his list: that's the PSF's Python to pyc compiler.\n- Hhhmm… Awk to Python… That's even more disgusting… But *does* have the drawback of being potentially useful…\n- The main reason I suggested it is, I answered a question about making a standalone binary from an AWK file. Python has several ways to make a standalone binary (e.g. for Windows, Py2Exe: py2exe.org). If you could translate AWK to Python you could then make a standalone binary!\n- @Ned Deily: you are only saying that because it's true... fine, I'll edit my answer.\n- Thanks for the comment, Paul! I'm glad to know that I'm not insane :) Also, welcome to Stack Overflow.","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":51,"estimatedTokens":830}}1146{"id":"stack-4156547","source":"stackoverflow","questionId":4156547,"title":"Is there a simple explanation of what a compiler is?","tags":["compiler-construction","terminology"],"text":"Title: Is there a simple explanation of what a compiler is?\nTags: compiler-construction, terminology\nSource: Stack Overflow\n\nQuestion:\nIn trying to provide a simple answer to a post about a \"C++ compiled app\" I was surprised that there weren't any questions or answers about this basic topic. Even searches on Meta didn't find anything helpful, whether on explaining compilers or beginning compiler questions in general.\n\nWe want Stackoverflow to be a good place to find answers about programming. We shouldn't leave something so fundamental to Wikipedia. A simple analogy would be asking a master carpenter what a 10-penny nail is and getting brushed off. Sure it's a simple question but a carpenter who thinks explaining something so simple is beneath him/her is just being a snob in the best case, politely, or a jerk if otherwise.\n\nAt the very least, this question provides a place to gather links to compiler-related explanations inside or outside of Stackoverflow. \n\n[Id make this CW but don't see the checkbox, has it been moved or deleted?]\n\nIn that spirit, I provide the link to the Wikipedia on compilers. Please edit this post with more links to compiler explanations.\n\nWikipedia entry on compilers.\n\n========================================\n\nTop Answer:\nI was about to provide an answer, but the first sentence on the Wikipedia article is virtually the same as what I was going to type:\n\n A compiler is a computer program *[...]* that transforms source code written in a programming language *[...]* into another computer language *[...]*\n\n========================================\n\nCode:\n```text\n.c\n```\n\n```text\n.h\n```\n\n```text\n.o\n```\n\n```text\n.a\n```\n\n```text\n.exe\n```\n\n```text\n.out\n```\n\n```text\n.a\n```\n\n```text\n.dll\n```\n\n```text\n.so\n```\n\n========================================\n\nComments:\n- Cool question! Plumbing, electrical work, or house construction might be a good area for a metaphor.\n- \"We shouldn't leave something so fundamental to Wikipedia.\" - I object. If there's already a flawless general explanation on Wikipedia (or anywhere else where it can be found via Google easily), there's no need to replicate this information here.\n- I think the wikipedia entry is pretty darn informative. It's hard to me improve on that one, unless you set a criteria such as, explain in 20 words what a compiler is (to make is a 'simple' explaination).\n- Somehow, it does seem to me that the Wikipedia page answers the question perfectly. But I guess we can give it a shot.\n- Wikipedia's a pretty good place for programming information; I go there regularly for overviews on unfamiliar concepts.\n- I'm not suggesting that we replicate the information. By providing a link to Wikipedia we both endorse the information on Wikipedia and provide a local nexus for links of a similar nature.\n- Perhaps there is a need for stack exchange site for \"self-styled basement programmers\"? I realize that this sounds more arrogant than I want it to sound.\n- I see nothing wrong with questions who's best/ideal answer is a link to Wikipedia.\n- Huzzah for Wikipedia quoting the Dragon Book. +1.\n- Hey, 2 up-votes. Actually surprised, as the more I tried to \"improve\" that stub answer, the more I felt that I would either need to detail it more to the point of reaching an essay-like answer, or need to strip it down again to go back to a one or 2 sentences answers. Interesting question to try to answer in way that is generic, synthetic but not misleading...\n- You have encountered the exact reason why I asked the question all those years ago.\n- I like your approach. Have you considered creating a part 2 and discuss liveness analysis and register allocation?","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":78,"estimatedTokens":914}}1147{"id":"stack-36955433","source":"stackoverflow","questionId":36955433,"title":"How does the JVM know when to throw a NullPointerException","tags":["java","nullpointerexception","compiler-construction","jvm"],"text":"Title: How does the JVM know when to throw a NullPointerException\nTags: java, nullpointerexception, compiler-construction, jvm\nSource: Stack Overflow\n\nQuestion:\nHow does the Java Virtual Machine know when to throw a NullPointerException? Does it check before every method I call on a an object if the object is null to check if it has to throw a NullPointerException? And if so, isnt this realy slow?\n\n========================================\n\nTop Answer:\nNULLPointerException is basically a run time exception i.e. JVM is expecting an actual object to be referenced when you are performing any operation.\n\nTo understand this better you need to know how object being created and referenced in java.Here is the syntax: \nmyClass o = new myClass();\n\nIn this case, an object is being created in the heap space with o as reference to that object i.e o is basically pointing to that object. During run time, JVM determines the actual object being pointed by o and used that to perform the operation.\n\nSo lets just say you have a method doSomething() as an instance method for the above myClass defintion, and you are calling it like this o.doSomething(), so JVM will find the actual object and called this method.\n\nBut if you set \no = null; then o is not pointing to actual object of myclass i.e. o is pointing to nothing.\nSo now during execution of o.doSomething, JVM find out that o is pointing to nothing and throws the runtime exception NULLPointerException in that case.\n\nIts not slow, because its the way JVM is being designed, to operate on the actual referenced objects which happens at runtime and if you are not handling the NULL check proeprly in your program and NULLpointerException being thrown by JVM then your program crashes and jvm terminates.\n\nsee java doc for more info:\nhttp://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html\n\n========================================\n\nCode:\n```text\nif (p != null) { p.something(); }\n```\n\n```text\np.something();\np.somethingElse();\n```\n\n```text\nthis\n```\n\n```text\np\n```\n\n```text\np.something()\n```\n\n```text\np\n```\n\n```text\nnull\n```\n\n```text\nnull\n```\n\n========================================\n\nComments:\n- In hotspot both approaches are used based on type/nullness profiling information","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":68,"estimatedTokens":563}}1148{"id":"stack-8480430","source":"stackoverflow","questionId":8480430,"title":"What is a good way of working with variables in 8 bit blocks?","tags":["c++","c","compiler-construction","programming-languages","language-design"],"text":"Title: What is a good way of working with variables in 8 bit blocks?\nTags: c++, c, compiler-construction, programming-languages, language-design\nSource: Stack Overflow\n\nQuestion:\nI'm playing around with a toy language design of mine and I have a few problems. I would like it to have variables specified in bit length. So, for example, one declares the variables like so:\n\n```\nbyte value;\ntwo_bytes value;\n```\n\nNow, here's my problem. I'm trying to make an interpreter for the language in C (or C++). My understanding of C/C++ is that their variable types are guaranteed to be at least a minimum size, but they can be larger (ie, a char will be at least 8 bits, but some implementations will have a 16 bit char).\n\nHow can I write an interpreter in C/C++ that deals only with specific lengths of bits? Is the only way to have an array of booleans or to set up bitfields for something like the char type?\n\n========================================\n\nTop Answer:\nIf you want primitive types, then most compilers should offer `` (or ``), which contains types such as `uint8_t`, `uint16_t`, `uint32_t`.\n\nIf you want obscure sizes (like e.g. `uint13_t`), you're probably best just using a larger standard size. If you need to save space, you might look into `std::bitset`, but this will probably be substantially slower for most operations.\n\n========================================\n\nCode:\n```text\nbyte value;\ntwo_bytes value;\n```\n\n```text\n#include <cstdint>\nstd::int32_t x; // 32 bits signed\nstd::uint16_t y; // 16 bits unsigned\n```\n\n```text\n<stdint.h>\n```\n\n```text\n<cstdint>\n```\n\n```text\nuint8_t\n```\n\n```text\nuint16_t\n```\n\n```text\nuint32_t\n```\n\n```text\nuint13_t\n```\n\n```text\nstd::bitset\n```\n\n```text\n#include <stdint.h>\nuint8_t my_byte_var;\nint16_t my_signed_2byte_var;\n```\n\n```text\nchar a;\nuint8_t b[4];\n\nb = a;\n```\n\n========================================\n\nComments:\n- The question is why you want to save memory. Do you target to extremeley embedded applications? The time to get one bit out of one byte is almost equal to get an 8 bit unsigned char. The char you can use directly in char bit; bit = get(3); if (bit) ... To use a bit out of a char you even have to mask it.\n- Why would it be a problem if the size of a given type is larger than what you need?\n- Which is your preference, C or C++?\n- `bool` has size 1. It's not individual bits.\n- @Pubby: Not necessarily.\n- @OliCharlesworth What do you mean? I can't imagine anything else, save for the few instances where it can be optimized out.\n- @Pubby: The standard explicitly says that `sizeof(bool)` is not required to be 1.\n- @OliCharlesworth What use would it be otherwise? `char` is always 1, and bool can always be represented as a `char`.\n- @Pubby: Perhaps by the native size of the platform, to make access faster.\n- @glglgl, I prefer C++, but am comfortable in C.\n- @Pubby: In some platforms `sizeof(bool) == 4`, you never know :)\n- If you include `cstdint`, then you should use `std::int32_t`. The language allows but does not require implementations to provide the types in the global namespace, and that makes your example non-portable.","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":93,"estimatedTokens":776}}1149{"id":"stack-2622038","source":"stackoverflow","questionId":2622038,"title":"Is there a standard lexer/parser tool for Python?","tags":["python","compiler-construction","parsing","tex","bnf"],"text":"Title: Is there a standard lexer/parser tool for Python?\nTags: python, compiler-construction, parsing, tex, bnf\nSource: Stack Overflow\n\nQuestion:\nA volunteer job requires us to convert a large number of LaTeX documents into ePub file format. It's a series of open-source fiction book which has so far only been produced only on paper via a print on demand service. We'd like to be able to offer the book to users of book-reader devices (such as Kindle) which require the ePub format for best results.\n\nFortunately, ePub is a very simple format, however there's no trivial way for LaTeX to produce the XHTML output required.\n\nWe experimented with alternative LaTeX compilers (e.g. plastex) but in the end we figured that it would probably be a lot easier to simply write our own compiler which understands a tiny subset of the LaTeX language and compiles directly to XHTML / ePub.\n\nPreviously I used a tool on Windows called GOLD. This allowed me to go directly from BNF grammars to a stub parser. It also alllowed me to implement the parser in any language I liked. (I'd choose Python).\n\nThis product has to work on Linux, so I'm wondering if there's an equivalent toolchain that works as well under Ubutnu / Eclipse / Python. The idea is that we will take the grammar of TeX and just implement a teeny subset of that, but we do not want to spend a huge amount of time worrying about grammar and parsing. A parser generator would obviously save us a great deal of time.\n\nSal\n\nUPDATE 1: Bonus marks for a solution with excellent documentation or tutorials. \n\nUPDATE 2: Extra bonus if there is grammar file for TeX already available, since all I'd have to do is implement the functions we care about.\n\n========================================\n\nTop Answer:\nTry PLY.\n\n========================================\n\nComments:\n- Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.\n- That's not a problem. We have Python and Tex people int the team of volounteers. The only issue is that we want to build on stable tools.\n- Pyparsing is no longer hosted on wikispaces.com. Go to github.com/pyparsing/pyparsing","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":32,"estimatedTokens":573}}1150{"id":"stack-48645379","source":"stackoverflow","questionId":48645379,"title":"AVR XYZ Registers","tags":["compiler-construction","microcontroller","avr","ram"],"text":"Title: AVR XYZ Registers\nTags: compiler-construction, microcontroller, avr, ram\nSource: Stack Overflow\n\nQuestion:\nWhat is the difference beatwean X, Y and Z registers in AVR microcontrollers. What for each of them suitable in C compilers? And where do compilers store heap pointer, stack pointer, frame pointer? Do this registers have the same capabilities or providing addressing in different spaces(ex. EEPROM, RAM).\n\n========================================\n\nCode:\n```text\nld r16, X\n```\n\n```text\nld r16, -Y\nst Z+, r16\n```\n\n```text\nldd r16, Y + 10\nstd Z + 5, r16\n```\n\n```text\nlpm r16, Z+\nlpm r17, Z\n```\n\n========================================\n\nComments:\n- Sorry, I meant data stack pointer not call stack. Where it stored?\n- And why do you say that AVR MCU are too small for dynamic memory allocation. Some of MEGA and XMEGA have internal RAM from 4KBytes to 16KBytes and possibility of using external RAM Memory.\n- There is only one stack. It used both for store return addresses (call/ret) and for storing the data (preserve registers push/pop and local variables). Yes it is possible to have an memory manager. But most task where those MCUs this is not required. Fast response and predictable memmory allocation thos those are better than unpredictable behavior of memory manager which may lead to heap fragmentation and stack overlap\n- Do C compilers (ex. IAR, CodeVision) have possibility to allocate block of variables, constant sized array in heap because they may be declared in if and in some cases memory will be reserved but not used, is there any annotation or modifier to say to compiler to allocate variable or array in heap? Sorry for bed English...\n- All local variables allocated in either the stack or the registers. It depends on the compiler and even on the selected optimization level. Without an explicitly enabled memory manager to have a heap, and explicitly allocated memory (e.g. malloc) no variables will be stored there.\n- @AterLux Actually, since the processor stack on the AVR only supports push and pop of single registers there are compilers that only use this stack for return addresses and sacrifices one of the pointer registers to implement a separate stack for local data.","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":38,"estimatedTokens":554}}1151{"id":"stack-41400976","source":"stackoverflow","questionId":41400976,"title":"Why does Bison (Yacc) print new lines for apparently no reason?","tags":["compiler-construction","bison","flex-lexer","yacc","lex"],"text":"Title: Why does Bison (Yacc) print new lines for apparently no reason?\nTags: compiler-construction, bison, flex-lexer, yacc, lex\nSource: Stack Overflow\n\nQuestion:\nI am working towards building a compiler for a simple formatting language using Flex and Bison.\n\nAt this stage, I still have not printed anything to `yyout` anywhere. I have some error cases where something is printed to the output file, but that is irrelevant to the example below. All my other print statements print to the console. Therefore, I expect the output file to be empty. However, with the following input file:\n\n```\n\\begin {document}\n \n\\tabsize(5)\n\\title{\"Why I Love Compiler Design\"}\n\\author{\"COMP421 Student\"}\n\\date{29/12/2016}\n\\pagesetup{30,100}\n \n\\end{document}\n```\n\nThe output file generated is:\n\nhttps://i.sstatic.net/mhVsz.png\n\nThere are 9 empty lines, corresponding to the 9 lines I had in my input file. The output I expect, however, is only 1 empty line.\n\nThis is my `.l` file:\n\n```\n%{\n #include \n #include \n #include \n #include \"y.tab.h\"\n\n void yyerror(const char *);\n int yylex(void);\n\n extern FILE *yyout;\n extern int yyparse();\n%}\n\n%option yylineno\n\n%%\n\n^\\\\ { printf(\"LEX returned token BSLASH\\n\"); return BSLASH; }\n\\{ { printf(\"LEX returned token LBRACE\\n\"); return LBRACE; }\n\\} { printf(\"LEX returned token RBRACE\\n\"); return RBRACE; }\n\\( { printf(\"LEX returned token LPAREN\\n\"); return LPAREN; }\n\\) { printf(\"LEX returned token RPAREN\\n\"); return RPAREN; }\n, { printf(\"LEX returned token COMMA\\n\"); return COMMA; }\n\nbegin { printf(\"LEX returned token BEGIN_\\n\"); return BEGIN_; }\nend { printf(\"LEX returned token END\\n\"); return END; }\ndocument { printf(\"LEX returned token DOCUMENT\\n\"); return DOCUMENT; }\n\npagesetup { printf(\"LEX returned token PAGESETUP\\n\"); return PAGESETUP; }\ntabsize { printf(\"LEX returned token TABSIZE\\n\"); return TABSIZE; }\ntitle { printf(\"LEX returned token TITLE\\n\"); return TITLE; }\nauthor { printf(\"LEX returned token AUTHOR\\n\"); return AUTHOR; }\ndate { printf(\"LEX returned token DATE\\n\"); return DATE; }\n\n(((0[1-9]|[12][0-9]|30)[-/ ]?(0[13-9]|1[012])|31[-/ ]?(0[13578]|1[02])|(0[1-9]|1[0-9]|2[0-8])[-/ ]?02)[-/ ]?[0-9]{4}|29[-/ ]?02[-/ ]?([0-9]{2}(([2468][048]|[02468][48])|[13579][26])|([13579][26]|[02468][048]|0[0-9]|1[0-6])00)) { printf(\"LEX returned token DDMMYYYYDATE\\n\"); yylval.sValue = yytext; return DDMMYYYYDATE; }\n[0-9]*[1-9][0-9]* { printf(\"LEX returned token INTEGER\\n\"); yylval.iValue = atoi(yytext); return INTEGER; }\n\\\".*\\\" { printf(\"LEX returned token STRING\\n\"); yylval.sValue = yytext; return STRING; }\n\n /* skip whitespace which is not part of a string */\n[ \\t] ;\n\n. yyerror(\"invalid character\");\n\n%%\n\nint main(int argc, char *argv[]) {\n if ( argc != 3)\n yyerror(\"ERROR You need 2 args: inputFileName outputFileName\");\n else {\n yyin = fopen(argv[1], \"r\");\n yyout = fopen(argv[2], \"w\");\n yyparse();\n fclose(yyin);\n fclose(yyout);\n }\n\n return 0;\n}\n```\n\nThis is my `.y` file:\n\n```\n%{\n #include \n #include \n #include \n #include \"y.tab.h\"\n\n void yyerror(const char *);\n int yylex(void);\n\n extern FILE *yyout;\n extern int yylineno;\n\n int docPropertyCounters[5];\n\n typedef enum {PAGE_SETUP, TAB_SIZE, DOC_TITLE, DOC_AUTHOR, DOC_DATE} document_property;\n\n static inline char *stringFromDocPropertyEnum(document_property indexOfProperty) {\n static char *strings[] = { \"\\\\pagesetup{}\", \"\\\\tabsize()\", \"\\\\title{}\", \"\\\\author{}\", \"\\\\date{}\"};\n return strings[indexOfProperty];\n }\n%}\n\n%union { \n int iValue;\n char* sValue;\n}; \n\n%start file\n\n%token BSLASH LBRACE RBRACE LPAREN RPAREN COMMA\n\n%token BEGIN_ END DOCUMENT\n\n%token PAGESETUP TABSIZE TITLE AUTHOR DATE\n\n%token INTEGER\n\n%token DDMMYYYYDATE STRING\n\n%%\n\nfile: beginDocument docProperties endDocument\n {\n for (int i = 0; i 1) \n fprintf(stderr, \"SYNTAX ERROR: Your source file contains more than one instance of the document property %s\", stringFromDocPropertyEnum(i));\n }\n |\n ;\n\nbeginDocument: BSLASH BEGIN_ LBRACE DOCUMENT RBRACE;\n\ndocProperties: docProperties docProperty\n |\n ; \n\ndocProperty: pageSetupProperty { docPropertyCounters[PAGE_SETUP]++; }\n | tabSizeProperty { docPropertyCounters[TAB_SIZE]++; }\n | titleProperty { docPropertyCounters[DOC_TITLE]++; }\n | authorProperty { docPropertyCounters[DOC_AUTHOR]++; }\n | dateProperty { docPropertyCounters[DOC_DATE]++; }\n ; \n\npageSetupProperty: BSLASH PAGESETUP LBRACE INTEGER COMMA INTEGER RBRACE;\n\ntabSizeProperty: BSLASH TABSIZE LPAREN INTEGER RPAREN;\n\ntitleProperty: BSLASH TITLE LBRACE STRING RBRACE;\n\nauthorProperty: BSLASH AUTHOR LBRACE STRING RBRACE;\n\ndateProperty: BSLASH DATE LBRACE DDMMYYYYDATE RBRACE;\n\nendDocument: BSLASH END LBRACE DOCUMENT RBRACE;\n\n%%\n\nint yywrap(void) {\n return 1;\n}\n\nvoid yyerror(const char* str) \n{\n fprintf(stderr,\"SYNTAX ERROR near line [%d]: %s\\n\",yylineno, str);\n}\n```\n\n========================================\n\nCode:\n```tex\n\\begin {document}\n    \n\\tabsize(5)\n\\title{\"Why I Love Compiler Design\"}\n\\author{\"COMP421 Student\"}\n\\date{29/12/2016}\n\\pagesetup{30,100}\n    \n\\end{document}\n```\n\n```c\n%{\n    #include <stdio.h>\n    #include <stdlib.h>\n    #include <string.h>\n    #include \"y.tab.h\"\n\n    void yyerror(const char *);\n    int yylex(void);\n\n    extern FILE *yyout;\n    extern int  yyparse();\n%}\n\n%option yylineno\n\n%%\n\n^\\\\ { printf(\"LEX returned token BSLASH\\n\"); return BSLASH; }\n\\{  { printf(\"LEX returned token LBRACE\\n\"); return LBRACE; }\n\\}  { printf(\"LEX returned token RBRACE\\n\"); return RBRACE; }\n\\(  { printf(\"LEX returned token LPAREN\\n\"); return LPAREN; }\n\\)  { printf(\"LEX returned token RPAREN\\n\"); return RPAREN; }\n,   { printf(\"LEX returned token COMMA\\n\"); return COMMA; }\n\nbegin    { printf(\"LEX returned token BEGIN_\\n\"); return BEGIN_; }\nend      { printf(\"LEX returned token END\\n\"); return END; }\ndocument { printf(\"LEX returned token DOCUMENT\\n\"); return DOCUMENT; }\n\npagesetup { printf(\"LEX returned token PAGESETUP\\n\"); return PAGESETUP; }\ntabsize   { printf(\"LEX returned token TABSIZE\\n\"); return TABSIZE; }\ntitle     { printf(\"LEX returned token TITLE\\n\"); return TITLE; }\nauthor    { printf(\"LEX returned token AUTHOR\\n\"); return AUTHOR; }\ndate      { printf(\"LEX returned token DATE\\n\"); return DATE; }\n\n(((0[1-9]|[12][0-9]|30)[-/ ]?(0[13-9]|1[012])|31[-/ ]?(0[13578]|1[02])|(0[1-9]|1[0-9]|2[0-8])[-/ ]?02)[-/ ]?[0-9]{4}|29[-/ ]?02[-/ ]?([0-9]{2}(([2468][048]|[02468][48])|[13579][26])|([13579][26]|[02468][048]|0[0-9]|1[0-6])00))  { printf(\"LEX returned token DDMMYYYYDATE\\n\"); yylval.sValue = yytext; return DDMMYYYYDATE; }\n[0-9]*[1-9][0-9]*   { printf(\"LEX returned token INTEGER\\n\"); yylval.iValue = atoi(yytext); return INTEGER; }\n\\\".*\\\"              { printf(\"LEX returned token STRING\\n\"); yylval.sValue = yytext; return STRING; }\n\n    /* skip whitespace which is not part of a string */\n[ \\t] ;\n\n. yyerror(\"invalid character\");\n\n%%\n\nint main(int argc, char *argv[]) {\n    if ( argc != 3)\n        yyerror(\"ERROR You need 2 args: inputFileName outputFileName\");\n    else {\n        yyin = fopen(argv[1], \"r\");\n        yyout = fopen(argv[2], \"w\");\n        yyparse();\n        fclose(yyin);\n        fclose(yyout);\n    }\n\n    return 0;\n}\n```\n\n```c\n%{\n    #include <stdio.h>\n    #include <stdlib.h>\n    #include <string.h>\n    #include \"y.tab.h\"\n\n    void yyerror(const char *);\n    int yylex(void);\n\n    extern FILE *yyout;\n    extern int  yylineno;\n\n    int docPropertyCounters[5];\n\n    typedef enum {PAGE_SETUP, TAB_SIZE, DOC_TITLE, DOC_AUTHOR, DOC_DATE} document_property;\n\n    static inline char *stringFromDocPropertyEnum(document_property indexOfProperty) {\n        static char *strings[] = { \"\\\\pagesetup{}\", \"\\\\tabsize()\", \"\\\\title{}\", \"\\\\author{}\", \"\\\\date{}\"};\n        return strings[indexOfProperty];\n    }\n%}\n\n%union { \n    int iValue;\n    char* sValue;\n}; \n\n%start file\n\n%token BSLASH LBRACE RBRACE LPAREN RPAREN COMMA\n\n%token BEGIN_ END DOCUMENT\n\n%token PAGESETUP TABSIZE TITLE AUTHOR DATE\n\n%token <iValue> INTEGER\n\n%token <sValue> DDMMYYYYDATE STRING\n\n%%\n\nfile: beginDocument docProperties endDocument\n            {\n                for (int i = 0; i < sizeof(docPropertyCounters)/sizeof(docPropertyCounters[0]); i++) \n                    if (docPropertyCounters[i] < 1) \n                        fprintf(stderr, \"SYNTAX ERROR: Your source file does not contain the required document property %s\", stringFromDocPropertyEnum(i)); \n                    else if (docPropertyCounters[i] > 1) \n                        fprintf(stderr, \"SYNTAX ERROR: Your source file contains more than one instance of the document property %s\", stringFromDocPropertyEnum(i));\n            }\n          |\n          ;\n\nbeginDocument: BSLASH BEGIN_ LBRACE DOCUMENT RBRACE;\n\ndocProperties: docProperties docProperty\n               |\n               ;                \n\ndocProperty:    pageSetupProperty { docPropertyCounters[PAGE_SETUP]++; }\n                | tabSizeProperty { docPropertyCounters[TAB_SIZE]++; }\n                | titleProperty   { docPropertyCounters[DOC_TITLE]++; }\n                | authorProperty  { docPropertyCounters[DOC_AUTHOR]++; }\n                | dateProperty    { docPropertyCounters[DOC_DATE]++; }\n                ;   \n\npageSetupProperty: BSLASH PAGESETUP LBRACE INTEGER COMMA INTEGER RBRACE;\n\ntabSizeProperty: BSLASH TABSIZE LPAREN INTEGER RPAREN;\n\ntitleProperty: BSLASH TITLE LBRACE STRING RBRACE;\n\nauthorProperty: BSLASH AUTHOR LBRACE STRING RBRACE;\n\ndateProperty: BSLASH DATE LBRACE DDMMYYYYDATE RBRACE;\n\nendDocument: BSLASH END LBRACE DOCUMENT RBRACE;\n\n%%\n\nint yywrap(void) {\n    return 1;\n}\n\nvoid yyerror(const char* str) \n{\n    fprintf(stderr,\"SYNTAX ERROR near line [%d]: %s\\n\",yylineno, str);\n}\n```\n\n```text\nyyout\n```\n\n```text\n.l\n```\n\n```text\n.y\n```\n\n```text\n[ \\t\\r\\n]      ;\n```\n\n```text\n[ \\t\\r\\n]      ;  /* skip whitespace which is not part of a string */\n```\n\n```text\n\\r\\n\n```\n\n========================================\n\nComments:\n- It doesn't. This is being done by *lex* or *flex*, whichever you are using.\n- I just tested your suggestion and it works like a charm\n- I would favour `[ \\t\\r\\n]+` here, for performance reasons. It only executes the generated switch statement once.\n- There is no problem putting a C comment Into your fleχ file *providing it is indented.* That is required by Posix and I believe it works with all lex implementations. Flex sometimes lets you get away with unindented comments, but I would avoid those. I also highly recommend `%option nodefault\", which will catch errors like the one which led to this question.\n- Everything after (unquoted/escaped) whitespace in the rules section is copied verbatim to the output file (C code). So a comment that is indendented by itself will just be copied.","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":9,"totalLines":377,"estimatedTokens":2641}}1152{"id":"stack-33727113","source":"stackoverflow","questionId":33727113,"title":"How to implement the execution of a language that I am writing?","tags":["c#","compiler-construction","delegates","stack-overflow"],"text":"Title: How to implement the execution of a language that I am writing?\nTags: c#, compiler-construction, delegates, stack-overflow\nSource: Stack Overflow\n\nQuestion:\nI am writing my own programming language using C# just for fun. It's called SPL. It has only 12 keywords/commands/instructions and nothing more. I know exactly what each command is going to do. This is the class diagram:\n\nhttps://i.sstatic.net/Ugq8N.png\n\nThis is not finished of course. But the idea is, we have a runtime `ISplRuntime`. All the things that the commands do, is done on the runtime. For example, the `OutputCommand` outputs the stuff in `ISplRuntime.Current` (This is just an `object`) to `ISplRuntime.Output` (This is a `TextWriter`).\n\nSo far so good. But then I need to implement the `IGotoCommand` which is just like the `goto` keyword in C#. I am going to make three kinds of gotos. But I cannot figure out how to change the flow of the program.\n\nHere's the ideas I tried:\n\nAdd a method called `RunNextCommand()` in `ISplRuntime`. And in the end of every command's `Execute()` method, call `RunNextCommand`. However, if the user writes a lot of SPL code, the stack trace will get larger and larger and result in a Stack Overflow.\n\nAdd a delegate to `ISplRuntime`. Each time a command has finished executing, `Invoke()` the delegate. The delegate will look for the next command to execute and call `Execute()` on that `ICommand`. But I am not sure if this will also result in a stack overflow. **Will it result in a stack overflow?**\n\nI am still not very sure if this is the right way to do it. I just want an general answer about how to do this kind of thing as this is the first time for me to write a language. Also please tell me whether using delegates will cause a stack overflow.\n\n========================================\n\nTop Answer:\nI am writing my own programming language using C# just for fun.\n\nAwesome!\n\n Add a method called RunNextCommand() in ISplRuntime. And in the end of every command's Execute() method, call RunNextCommand. \n\nEven before you realize that its a stack overflow to do so, you should realize that your description of `RunNextCommand` does not do what it says on the tin. What you are describing is called `RunRemainderOfProgram`. That's a sign that something is messed up in this design.\n\n Add a delegate to ISplRuntime. Each time a command has finished executing, Invoke() the delegate. The delegate will look for the next command to execute and call Execute() on that ICommand. But I am not sure if this will also result in a stack overflow. Will it result in a stack overflow?\n\nI don't see why it would. So the idea here is that `RunNextCommand` as its last action sets a delegate which, when invoked, does the next command?\n\nWhat you have invented here is called a *continuation*. The continuation of an instruction is the \"what happens next?\" of the current point of execution.\n\n I am still not very sure if this is the right way to do it. I just want an general answer about how to do this kind of thing as this is the first time for me to write a language.\n\nThere are many ways to build an interpreter, which is basically what you're doing here. I would continue to experiment and see what works and what doesn't.\n\nIt might help to study how this problem is solved in virtual and actual machines. In those machines every instruction has a unique number associated with it; a \"goto\" contains the number of the next instruction to run. There is a special variable called the \"instruction pointer\" which has the number of the currently-running instruction. If the current instruction is a goto, then the IP is set to the value indicated by the goto; if not, the IP is incremented to the next instruction, and so on. The main loop is then \"look up the instruction at the current IP, execute it, set the new IP, repeat\".\n\n Also please tell me whether using delegates will cause a stack overflow.\n\nIt's hard to predict the behaviour of a program we can't see and you haven't written. Try it and you will very quickly find out if you've written an unbounded recursion.\n\nGood luck!\n\n========================================\n\nCode:\n```text\nISplRuntime\n```\n\n```text\nOutputCommand\n```\n\n```text\nISplRuntime.Current\n```\n\n```text\nobject\n```\n\n```text\nISplRuntime.Output\n```\n\n```text\nTextWriter\n```\n\n```text\nIGotoCommand\n```\n\n```text\ngoto\n```\n\n```text\nRunNextCommand()\n```\n\n```text\nISplRuntime\n```\n\n```text\nExecute()\n```\n\n```text\nRunNextCommand\n```\n\n```text\nISplRuntime\n```\n\n```text\nInvoke()\n```\n\n```text\nExecute()\n```\n\n```text\nICommand\n```\n\n```text\nRunNextCommand\n```\n\n```text\nRunRemainderOfProgram\n```\n\n```text\nRunNextCommand\n```\n\n========================================\n\nComments:\n- Why not insert the goto operation into IL?\n- Your program is a list of instructions. When you execute you are going to increment the index to the list of instruction as you execute each instruction. A goto will cause the index to jump instead of just increment by one.\n- I would encourage anyone writing a programming language to read \"The Dragon Book\" (en.wikipedia.org/wiki/&hellip;). Even if you're not writing a full-blown compiler, and even though the book is targeted at the C programmer, there is a great deal you will learn by reading it.\n- Let me paraphrase what you mean because I am not sure if I fully understood. You want me just to leave the Execute method of the goto command blank. And when client code implements ISplRuntime, it needs to write a loop to execute the commands. In the loop, it needs to check whether the current command is a goto. If it is, then change the loop index. Am I interpreting you correctly?\n- Not quite. Let's try this: each command has `NextCommand` method. Commands are divided into `FlowControlCommand` and `SimpleCommand`. `SimpleCommand`'s `NextCommand` will return current line + 1. goto's `NextCommand` will return the line specified by the goto. Your runtime has a while loop that does `currentCommand.Execute`, then `currentLine = currentCommand.NextCommand(currentLine)`. I may have misunderstood the purpose of your `ISplRuntime` - basically, there's probably a place where the client says `myIsplProgram.Run()` - that is where you have a loop.\n- Ok I understand it now! Thanks!","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":19,"totalLines":138,"estimatedTokens":1557}}1153{"id":"stack-10065527","source":"stackoverflow","questionId":10065527,"title":"Why can't we use a CFG to scan/tokenize?","tags":["parsing","language-agnostic","compiler-construction"],"text":"Title: Why can't we use a CFG to scan/tokenize?\nTags: parsing, language-agnostic, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have a practice exam for my compilers course with the following questions that I can't get:\n\n- Why can't we use a context-free grammer (CFG) to scan/tokenize?\n\n- Why don't we use a deterministic finite automaton (DFA) for parsing?\n\nDoes anyone have any ideas?\n\n========================================\n\nCode:\n```text\n0    1\n q0  q1   q0\n q1  q0   q1\n```\n\n```text\nvector<vector<int>>\n```\n\n```text\nvector\n```\n\n```text\n<\n```\n\n```text\nvector\n```\n\n```text\n<\n```\n\n```text\nint\n```\n\n```text\n>\n```\n\n```text\n>\n```\n\n```text\n>>\n```\n\n========================================\n\nComments:\n- Actually, a CFG should be plenty powerful enough to describe tokens, but it'd be a PITA and overkill. But I'm probably nitpicking.\n- If you used a table-based bottom up parser, the parse table will be much bigger than an FSM transition table.","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":10,"totalLines":63,"estimatedTokens":240}}1154{"id":"stack-1801894","source":"stackoverflow","questionId":1801894,"title":"Dynamic languages Vs Static languages (can this two be together in the same place)","tags":["compiler-construction","programming-languages","dynamic-languages","static-language"],"text":"Title: Dynamic languages Vs Static languages (can this two be together in the same place)\nTags: compiler-construction, programming-languages, dynamic-languages, static-language\nSource: Stack Overflow\n\nQuestion:\nprogramming languages are grouped it 2 main classes \"Dynamic\" & \"Static\". \n- Is this always the case a programming language is in one of them and not in both, I mean can a language be dynamic and static at the same time ?\n\n========================================\n\nTop Answer:\nC# 4.0 is a statically typed language that supports dynamic resolution as well. The `dynamic` keyword basically tells the compiler: don't worry about this for now. If it can't resolved the type at runtime an exception is thrown.\n\n========================================\n\nCode:\n```text\ndynamic\n```\n\n```text\nSystem.Object\n```\n\n```text\ndynamic\n```\n\n```text\nNSMutableArray *a = [NSMutableArray arrayWithObject:@\"I am a string\"];\nNSNumber *n = [a objectAtIndex:0]; //assigning a string to a number!\n```\n\n```text\nid\n```\n\n```text\nid\n```\n\n```text\nNSArray\n```\n\n```text\nid\n```\n\n```text\nNSNumber *n = @\"I am a string\"\n```\n\n```text\nSystem.linq.Expression tree API\n```\n\n```text\nexpression class\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.952Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":60,"estimatedTokens":294}}1155{"id":"stack-2398183","source":"stackoverflow","questionId":2398183,"title":"Invalid lvalue in assignment error when trying to make a pointer NULL","tags":["c","compiler-construction","pointers","compiler-errors"],"text":"Title: Invalid lvalue in assignment error when trying to make a pointer NULL\nTags: c, compiler-construction, pointers, compiler-errors\nSource: Stack Overflow\n\nQuestion:\nI have a pointer of a structure type that I made. On program start it begins as NULL and I then malloc/realloc as I need to add/remove these structures and I was just gonna use my pointer to point at the first structure and move through it like an array.\n\nWhen I malloc/realloc I always make the size of the \"array\"/area in memory one larger than it needs to be. I do this so I can set the \"last index\"/area in memory to NULL so I can say something like while (pointer != NULL). \n\nI get the error: invalid lvalue in assignment when I try to assign NULL to the last position in the array/area of memory with the lines: \n\n```\n// Realloc remotelist by adding one to connrhosts\n connrhosts++;\n remotelist = realloc(remotelist, sizeof(rhost)*(connrhosts + 1));\n (remotelist + connrhosts) = NULL;\n```\n\nWhat I think I am saying is:\n\n- Its time to add a new structure to my array so I will increase connrhosts by one.\n\n- Realloc the memory that is pointed at remotelist to a new area of memory that is the size of connrhosts (how many structures I will use) as well as one additional space so I can make it NULL\n\n- Point remotelist to the new area of memory\n\n- Use my pointer remotelist and add the offset connrhosts which will now point to the last index of the area of memory and make that pointer NULL.\n\nAs far as I can tell (or feel) I did everything correctly, but I have been working on this project for sometime now and am under the impression I have tunnel vision. I would love to have a fresh set of eyes take a look at my logic/code and let me know what they think and what I did wrong. Thanks again. :D\n\nEdit -\nPart of my problem is I think I have a misunderstanding of what I can do with pointers.\n\nHere is my structure:\n\n```\ntypedef struct {\n char address[128]; // Buffer that holds our address of the remote host\n int port; // Port of the remote host\n int conn; // FD to the connection of our remote host\n int ofiles; // Open files associated with the remote host\n} rhost;\n```\n\nWhat I was hoping I could do was loop through my array/area of memory and say if its not NULL then do something with it. So my original loop statement is while (NULL != remotelist). Now I believe are reading responses and comments that this logic is wrong because I am checking if a pointer is null? I ought to be checking if the area of memory/structure that the pointer is pointing is null? If this is the case it ought to be something like while (NULL != *(remotelist + someoffset))?\n\nI am doing it this way as my teacher suggested it/talked about it in class.\n\nMy initial declaration/initialization of remotelist was: rhost *remotelist = NULL;\n\n========================================\n\nTop Answer:\nYou also need to dereference your pointer. \n\n```\n*(remotelist + connrhosts) = NULL;\n```\n\nAlthough I think \n\n```\nremotelist[connrhosts] = NULL;\n```\n\nis clearer.\n\n========================================\n\nCode:\n```text\n// Realloc remotelist by adding one to connrhosts\n  connrhosts++;\n  remotelist = realloc(remotelist, sizeof(rhost)*(connrhosts + 1));\n  (remotelist + connrhosts) = NULL;\n```\n\n```text\ntypedef struct {\n  char address[128]; // Buffer that holds our address of the remote host\n  int port; // Port of the remote host\n  int conn; // FD to the connection of our remote host\n  int ofiles; // Open files associated with the remote host\n} rhost;\n```\n\n```text\nremotelist[connrhosts] = NULL;  // array notation asuming \n                                // remotelist is an array of pointers\n```\n\n```text\nremotelist += connrhost; // pointer arithmetic\n*remotelist = NULL; // assuming remotelist is an array of pointers.\n```\n\n```text\nint\n```\n\n```text\nsize_t\n```\n\n```text\n*(remotelist + connrhosts) = NULL;\n```\n\n```text\n*\n```\n\n```text\n*(remotelist + connrhosts) = NULL;\n```\n\n```text\nremotelist[connrhosts] = NULL;\n```\n\n```text\nmemset(remotelist + connrhosts, 0, sizeof(rhost));\n```\n\n```text\npointer != NULL\n```\n\n```text\np->field == 0\n```\n\n```text\nrealloc().\n```\n\n```text\nconst rhost ZERO_HOST = { 0 };\n...\nconnrhosts++; \nremotelist = realloc(remotelist, connrhosts * sizeof *remotelist); \nremotelist[connrhosts] = ZERO_HOST;\n```\n\n```text\nif (memcmp(&remotelist[i], &ZERO_HOST, sizeof ZERO_HOST) == 0)\n  /* All zeroes */;\n```\n\n```text\nif (remotelist[i].address[0] == '\\0')\n  /* Entry is not used */;\n```\n\n```text\nNULL\n```\n\n```text\nNULL\n```\n\n```text\nNULL\n```\n\n```text\n= { 0 }\n```\n\n```text\nmemset\n```\n\n```text\nmemcmp\n```\n\n```text\nmemset\n```\n\n========================================\n\nComments:\n- BTW, why are you doing `++connrhosts` and then using `connrhosts + 1` in allocation? What is the purpose of that extra `+ 1`?\n- connrhosts is the current amount of remote hosts I can connected too. So in my array if it was two and I didn't do connrhosts + 1 I would only have two indexes, 0 and 1, and both would point to a structure. I do the + 1 so I can have three indexes, 0, 1, and 2, where 0 and 1 point to a structure and 2 can be equal to NULL. That way I can have statements like while (NULL != remotelist)\n- rhost is a structure. The allocation you make, that remotelist points to, is an array *of these structures*, not an array of pointers. You can't set a structure to NULL, so you will need another way to signal the end of the list. One way is to keep an integer hanging around with the count, and pass this along with the pointer. Another way is to use one of the fields of rhost as an \"end of data\" marker, for example say that if `ofiles` is -1, that means end of array. You could use an actual NULL-terminated array of pointers. I don't recommend it: you'd have to allocate them separately.\n- What if I said something like rhost **remotelist; Then remotelist = realloc(remotelist, sizeof(*rhost)*(connrhosts + 1)); Would I be able to set those pointers to NULL then?\n- You would, but all that would give you is an array of pointers. You could then allocate some `rhost` objects separately. It's `sizeof(rhost*)`, though, or `sizeof(*remotelist)`, not `sizeof(*rhost)`.\n- Won't remotelist += connrhost; // pointer arithmetic *remotelist = NULL; actually change the address of where my pointer is pointing at? If so that would be a problem for me. I was adding the offset or connrhosts so I could say \"This many spots away from the beginning make NULL\". I also provided more documentation on my structure and what I thought I was doing.\n- Yes, it does, it increments the pointer by connrhost blocks of the type of remotelist. You'd have to move it back again afterwards. I included both options for completeness. I gave +1 to the answer below that suggested the [] notation was cleaner - it is.\n- I went ahead and tried using the [] notation but then I got a incompatible types in assignment error. So I must be doing something wrong earlier or I just really have no idea what I am doing?\n- It sounds like you've got the types messed up somewhere, but have a look at Steve Jessop's comment on your question. It sounds like you're trying to emulate strings, which are terminated with the `\\0` character. That's actually equal to 0, so the last int is a 0 if that makes sense, which is different to NULL, which is a void * to nothing.","metadata":{"transformedAt":"2026-08-18T18:32:17.953Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":24,"totalLines":194,"estimatedTokens":1825}}1156{"id":"stack-27252003","source":"stackoverflow","questionId":27252003,"title":"(VC++) Runtime Check for Uninitialized Variables: How is the test Implemented?","tags":["visual-c++","assembly","compiler-construction","x86"],"text":"Title: (VC++) Runtime Check for Uninitialized Variables: How is the test Implemented?\nTags: visual-c++, assembly, compiler-construction, x86\nSource: Stack Overflow\n\nQuestion:\nI'm trying to understand what this test does exactly. This toy code\n\n```\nint _tmain(int argc, _TCHAR* argv[])\n{\n int i;\n printf(\"%d\", i);\n return 0;\n}\n```\n\nCompiles into this:\n\n```\nint _tmain(int argc, _TCHAR* argv[])\n```\n\n{\n012C2DF0 push ebp\n\n012C2DF1 mov ebp,esp\n\n012C2DF3 sub esp,0D8h\n\n012C2DF9 push ebx\n\n012C2DFA push esi\n\n012C2DFB push edi\n\n012C2DFC lea edi,[ebp-0D8h]\n\n012C2E02 mov ecx,36h\n\n012C2E07 mov eax,0CCCCCCCCh\n\n012C2E0C rep stos dword ptr es:[edi]\n\n**012C2E0E mov byte ptr [ebp-0D1h],0** \n\n```\nint i;\nprintf(\"%d\", i);\n```\n\n012C2E15 cmp byte ptr [ebp-0D1h],0\n\n012C2E1C jne wmain+3Bh (012C2E2Bh)\n\n012C2E1E push 12C2E5Ch\n\n012C2E23 call __RTC_UninitUse (012C10B9h)\n\n012C2E28 add esp,4\n\n012C2E2B mov esi,esp\n\n012C2E2D mov eax,dword ptr [i]\n\n012C2E30 push eax\n\n012C2E31 push 12C5858h\n\n012C2E36 call dword ptr ds:[12C9114h]\n\n012C2E3C add esp,8\n\n012C2E3F cmp esi,esp\n\n012C2E41 call __RTC_CheckEsp (012C1140h) \n\n```\nreturn 0;\n```\n\n012C2E46 xor eax,eax\n\n}\n012C2E48 pop edi\n\n012C2E49 pop esi\n\n012C2E4A pop ebx\n\n012C2E4B add esp,0D8h\n\n012C2E51 cmp ebp,esp\n\n012C2E53 call __RTC_CheckEsp (012C1140h)\n\n012C2E58 mov esp,ebp\n\n012C2E5A pop ebp\n\n012C2E5B ret \n\nThe 5 lines emphasized are the only ones removed by properly initializing the variable i. The lines 'push 12C2E5Ch, call __RTC_UninitUse' call the function that display the error box, with a pointer to a string containing the variable name (\"i\") as an argument. \n\nWhat I can't understand are the 3 lines that perform the actual test:\n\n012C2E0E mov byte ptr [ebp-0D1h],0\n\n012C2E15 cmp byte ptr [ebp-0D1h],0\n\n012C2E1C jne wmain+3Bh (012C2E2Bh) \n\nIt would have seemed the compiler is probing the stack area of i (setting a byte to zero and immediately testing whether it's zero), just to be sure it isn't initialized somewhere it couldn't see during build. However, the probed address, ebp-0D1h, has little to do with the actual address of i.\n\nEven worse, it seems if there were such an external (other thread?) initialization that did initialize the probed address but *to zero*, this test would still shout about the variable being uninitialized.\n\nWhat's going on? Maybe the probe is meant for something entirely different, say to test if a certain byte is writable?\n\n========================================\n\nTop Answer:\n`[ebp-0D1h]` is a temporary variable used by the compiler to track \"initialized\" status of variables. If we modify the source a bit, it will be more clear:\n\n```\nint _tmain(int argc, _TCHAR* argv[])\n{\n int i, j;\n printf(\"%d %d\", i, j);\n i = 1;\n printf(\"%d %d\", i, j);\n j = 2;\n return 0;\n}\n```\n\nProduces the following (irrelevant parts skipped):\n\n```\nmov DWORD PTR [ebp-12], -858993460 ; ccccccccH\nmov DWORD PTR [ebp-8], -858993460 ; ccccccccH\nmov DWORD PTR [ebp-4], -858993460 ; ccccccccH\nmov BYTE PTR $T4694[ebp], 0\nmov BYTE PTR $T4693[ebp], 0\n```\n\nIn prolog, variables are filled with 0xCC, and two tracking variables (one for `i` and one for `j`) are set to 0.\n\n```\n; 7 : printf(\"%d %d\", i, j); \n cmp BYTE PTR $T4693[ebp], 0\n jne SHORT $LN3@main\n push OFFSET $LN4@main\n call __RTC_UninitUse\n add esp, 4\n$LN3@main:\n cmp BYTE PTR $T4694[ebp], 0\n jne SHORT $LN5@main\n push OFFSET $LN6@main\n call __RTC_UninitUse\n add esp, 4\n$LN5@main:\n mov eax, DWORD PTR _j$[ebp]\n push eax\n mov ecx, DWORD PTR _i$[ebp]\n push ecx\n push OFFSET $SG4678\n call _printf\n add esp, 12 ; 0000000cH\n```\n\nThis corresponds roughly to:\n\n```\nif ( $T4693 == 0 )\n _RTC_UninitUse(\"j\");\nif ( $T4694 == 0 )\n _RTC_UninitUse(\"j\");\nprintf(\"%d %d\", i, j);\n```\n\nNext part:\n\n```\n; 8 : i = 1; \n mov BYTE PTR $T4694[ebp], 1\n mov DWORD PTR _i$[ebp], 1\n```\n\nSo, once `i` is intialized, the tracking variable is set to 1.\n\n```\n; 10 : j = 2;\nmov BYTE PTR $T4693[ebp], 1\nmov DWORD PTR _j$[ebp], 2\n```\n\nHere, the same is happening for `j`.\n\n========================================\n\nCode:\n```text\nint _tmain(int argc, _TCHAR* argv[])\n{\n    int i;\n    printf(\"%d\", i);\n    return 0;\n}\n```\n\n```text\nint _tmain(int argc, _TCHAR* argv[])\n```\n\n```text\nint i;\nprintf(\"%d\", i);\n```\n\n```text\nreturn 0;\n```\n\n```text\ni\n```\n\n```text\n[ebp-0D1h]\n```\n\n```text\ni\n```\n\n```text\ni\n```\n\n```text\nif (argc > 1) i = 1;\n```\n\n```text\nint _tmain(int argc, _TCHAR* argv[])\n{\n    int i, j;\n    printf(\"%d %d\", i, j);\n    i = 1;\n    printf(\"%d %d\", i, j);\n    j = 2;\n    return 0;\n}\n```\n\n```text\nmov DWORD PTR [ebp-12], -858993460      ; ccccccccH\nmov DWORD PTR [ebp-8], -858993460       ; ccccccccH\nmov DWORD PTR [ebp-4], -858993460       ; ccccccccH\nmov BYTE PTR $T4694[ebp], 0\nmov BYTE PTR $T4693[ebp], 0\n```\n\n```text\n; 7    :        printf(\"%d %d\", i, j);    \n    cmp BYTE PTR $T4693[ebp], 0\n    jne SHORT $LN3@main\n    push    OFFSET $LN4@main\n    call    __RTC_UninitUse\n    add esp, 4\n$LN3@main:\n    cmp BYTE PTR $T4694[ebp], 0\n    jne SHORT $LN5@main\n    push    OFFSET $LN6@main\n    call    __RTC_UninitUse\n    add esp, 4\n$LN5@main:\n    mov eax, DWORD PTR _j$[ebp]\n    push    eax\n    mov ecx, DWORD PTR _i$[ebp]\n    push    ecx\n    push    OFFSET $SG4678\n    call    _printf\n    add esp, 12                 ; 0000000cH\n```\n\n```text\nif ( $T4693 == 0 )\n  _RTC_UninitUse(\"j\");\nif ( $T4694 == 0 )\n  _RTC_UninitUse(\"j\");\nprintf(\"%d %d\", i, j);\n```\n\n```text\n; 8    :        i = 1;    \n    mov BYTE PTR $T4694[ebp], 1\n    mov DWORD PTR _i$[ebp], 1\n```\n\n```text\n; 10   :        j = 2;\nmov BYTE PTR $T4693[ebp], 1\nmov DWORD PTR _j$[ebp], 2\n```\n\n```text\n[ebp-0D1h]\n```\n\n```text\ni\n```\n\n```text\nj\n```\n\n```text\ni\n```\n\n```text\nj\n```\n\n```text\nC7060F000055    mov     dword ptr [esi],5500000Fh\nC746048BEC5151  mov     dword ptr [esi+0004],5151EC8Bh\n```\n\n```text\nBF0F000055  mov     edi,5500000Fh\n893E    mov     [esi],edi\n5F  pop     edi\n52  push    edx\nB640    mov     dh,40\nBA8BEC5151  mov     edx,5151EC8Bh\n53  push    ebx\n8BDA    mov     ebx,edx\n895E04  mov     [esi+0004],ebx\n```\n\n```text\nBB0F000055  mov     ebx,5500000Fh\n891E    mov     [esi],ebx\n5B  pop     ebx\n51  push    ecx\nB9CB00C05F  mov     ecx,5FC000CBh\n81C1C0EB91F1    add     ecx,F191EBC0h ; ecx=5151EC8Bh\n```\n\n========================================\n\nComments:\n- The code itself is zeroing the location it tests, so the branch should never be taken and thus the message printed. It doesn't make much sense to me, unless this was compiled without optimization.\n- /RTC lives only in unoptimized builds.\n- Yes! I tried to verify this previously with different code and couldn't. Even adding 'void dummy(const int& arg) { printf(\"42\"); }' and calling 'dummy(i);' removed the test code. But branching on argc as you suggested does separate the store of the flag from its test. Thanks!\n- What is the point of this answer? That code doesn't appear in the question. And it seems nonsensical, e.g. `mov dh, 40` right before overwriting the whole EDX with a mov-immediate. And `pop` / `push` replaces the bottom DWORD on the stack with some previously-untouched register, but why would you want that? This certainly doesn't look like an answer to the question about how MSVC's compiler-generated debug-mode asm works.","metadata":{"transformedAt":"2026-08-18T18:32:17.953Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":23,"totalLines":358,"estimatedTokens":1787}}1157{"id":"stack-3434786","source":"stackoverflow","questionId":3434786,"title":"8086 Assembler - Generating the object code from opcodes","tags":["compiler-construction","x86","assembly"],"text":"Title: 8086 Assembler - Generating the object code from opcodes\nTags: compiler-construction, x86, assembly\nSource: Stack Overflow\n\nQuestion:\nI'm working on an assembler for the 8086 . My question is how do you convert the hex opcodes to an executable file like .EXE,.ELF,.COM,a.out etc .Looking for links/resources for this and should the assembler do the linking process or is it done by the OS automatically?\n\n========================================\n\nTop Answer:\nIt depends a bit on the (target) platform but for the Windows PE format (EXE) your job would be to produce obj files, and let the Linker do the rest.\n\n========================================\n\nComments:\n- @Aillyn: I think he's asking for specifics of the file formats.\n- See stackoverflow.com/questions/2478142/&hellip;","metadata":{"transformedAt":"2026-08-18T18:32:17.953Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":17,"estimatedTokens":197}}1158{"id":"stack-4638122","source":"stackoverflow","questionId":4638122,"title":"Make a compiled binary run at native speed flawlessly without recompiling from source on a another system?","tags":["compiler-construction","constraints","virtualization","native","aot"],"text":"Title: Make a compiled binary run at native speed flawlessly without recompiling from source on a another system?\nTags: compiler-construction, constraints, virtualization, native, aot\nSource: Stack Overflow\n\nQuestion:\n**I know that many people, at a first glance of the question, may immediately yell out \"Java\", but no, I know Java's qualities. Allow me to elaborate my question first.**\n\nNormally, when we want our program to run at a native speed on a system, whether it be Windows, Mac OS X, or Linux, we need to compile from source codes. If you want to run a program of another system in your system, you need to use a virtual machine or an emulator. While these tools allow you to use the program you need on the non-native OS, they sometimes have problems of performance and glitches. \n\nWe also have a newer compiler called \"JIT Compiler\", where the compiler will parse the bytecode program to native machine language before execution. The performance may increase to a very good extent with JIT Compiler, but the performance is still not the same as running it on a native system.\n\nAnother program on Linux, WINE, is also a good tool for running Windows program on Linux system. I have tried running Team Fortress 2 on it, and tried experiment with some settings. I got ~40 fps on Windows at its mid-high setting on 1280 x 1024. On Linux, I need to turn everything low at 1280 x 1024 to get ~40 fps. There are 2 notable things though: \n\n- Polygon model settings do not seem to affect framerate whether I set it low or high.\n\n- When there are post-processing effects or some special effects that require manipulation of drawn pixels of the current frame, the framerate will drop to 10-20 fps.\n\nFrom this point, I can see that normal polygon rendering is just fine, but when it comes to newer rendering methods that requires graphic card to the job, it slows down to a crawl.\n\nAnyway, this question is rather theoretical. Is there anything we can do at all? I see that WINE can run STEAM and Team Fortress 2. Although there are flaws, they can run at lower setting. Or perhaps, I should also ask, \"**is it possible to translate one whole program on a system to another system without recompiling from source and get native speed?**\" I see that we also have AOT Compiler, is it possible to use it for something like this? Or there are so many constraints (such as DirectX call or differences in software architecture) that make it impossible to have a flawless and not native to the system program that runs at native speed?\n\n========================================\n\nTop Answer:\nYes, it is technically possible to translate a binary executable program written for one processor architecture and operating system into a binary executable program that will run on another processor and operating system. It's also an unholy amount of work.\n\nThere is a problem with the \"native code execution speed\" terminology. You can compile a program to native code with optimizations disabled, and the resulting code will be native executable code running at \"native code execution speed\" but it will probably run slower than the same source code compiled with optimizations enabled. Both are running \"native code execution speed\", but they are running different quantities and quality of machine code to achieve the same core algorithm.\n\nMachine instructions are much more primitive than higher level source programming languages. When compiling source code into machine code, a lot of information is lost. Data types, for example, are usually reduced by a compiler down to a handful of machine primitives - pointer, integer, float. A string is a pointer to memory. A char is an integer. An object instance is a pointer.\n\nWhen you translate one machine instruction set into another machine instruction set, you are handicapped because you don't have as much information about the data as a source code compiler has. Compiling from source code, the compiler can see relationships and optimizations in the data that would be very difficult to discover just by looking at the machine code alone. \n\nStory time: Digital Equipment Corporation created a system called FX!32 that took native compiled Win32 Intel x86 executables, decompiled them, and translated the logic into native Alpha AXP processor instructions running Windows NT AXP. In this case, the OSes were at least cut from the same cloth, but one was 32 bit and the other was 64 bit, and at the machine code level they had radically different calling conventions. Nevertheless, it worked, and it worked remarkably well. Due to the differences in hardware, an Intel x86 app running on AXP could eventually run faster than the same app running on Intel hardware. (FX!32 used profiling to reoptimize the AXP code after the Intel app was run a few times, so performance usually started out pretty bad but improved each time you ran the app)\n\nHowever, even though everything was executing native AXP instructions, the FX!32 translated app never ran as fast as taking the source code and recompiling it specifically for the AXP instruction set. The FX!32 translated native AXP instruction stream was bulked up by the necessity to fully represent the semantics of the original Intel x86 instructions even if the (unseen) higher level algorithm didn't require all aspects of those semantics.\n\nWhen doing machine instruction to machine instruction translation, you can see/hear every note in the symphony but you may have trouble picking out which ones define the melody.\n\n========================================\n\nComments:\n- There's really no way I would have thought \"Java\" upon reading the words \"native speed\"...\n- +1 for \"Virus scanners get very excited when anyone other than the host OS loads file data into memory and attempts to execute that memory.\" Oh yeah, and for a very clear, thoughtful explanation.","metadata":{"transformedAt":"2026-08-18T18:32:17.953Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":43,"estimatedTokens":1464}}1159{"id":"stack-842941","source":"stackoverflow","questionId":842941,"title":"Programmatically generate a C Win32 DLL","tags":["c","winapi","compiler-construction"],"text":"Title: Programmatically generate a C Win32 DLL\nTags: c, winapi, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI need to repeatedly generate a Win32 DLL with a registration information function. This function uses literals to return customer specific registration information, with a separate DLL being built per customer.\n\nI have a test version working correctly, with hard-coded information for one customer. The urgency for some sites dictates I generate some DLL's manually, but I would like to give the client an application that dynamically emits the C source and builds a DLL on demand.\n\nWhat would be the the best way to do this? I have VS 2008 C++ Express, and thus the cl.exe compiler. My current approach would simply be to have a C# application with a string constant for the C source, and before generation, replace tokens in that with required parameters, then build and link by shelling out and running cl.exe.\n\n========================================\n\nTop Answer:\nPut all customer information as string resources in a .rc file. Link the corresponding .res file into your DLL. All code in the DLL that depends on that customer information would call LoadString to fetch this.\n\nThen build a seperate program (or function) called \"UpdateDLL.exe\" that uses the Win32 APIs: BeginUpdateResource, UpdateResource, etc... to update the DLL with the new information.\n\nShip the following:\n\nA pre-compiled DLL that has empty (or default) strings for the customer information in the resources.\n\nYour UpdateDLL.exe tool that takes the DLL name and customer info file as command line param.\n\nYour customer runs \"UpdateDLL.exe customer.dll myinfo.txt\" to update his copy of the DLL with his information.\n\n========================================\n\nCode:\n```text\n#define\n```\n\n```text\nstring UserName = \"PLACEHOLDER UserName                     \";\nstring RegCode  = \"PLACEHOLDER RegCode                      \";\nbool CheckRegistration(string UserName, RegCode) {\n  ...\n}\n```\n\n```text\n.dll\n```\n\n```text\n.dll\n```\n\n```text\nPLACEHOLDER\n```\n\n```text\n\"MYSTART-------------MYEND\"\n```\n\n========================================\n\nComments:\n- Is cl.exe freely distributable? How will you make sure the users of your software have a copy?\n- @Neil: IIRC it isn't. But it is freely downloadable.\n- @selbie, this is a great idea. Thanks!","metadata":{"transformedAt":"2026-08-18T18:32:17.953Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":6,"totalLines":63,"estimatedTokens":582}}1160{"id":"stack-5942447","source":"stackoverflow","questionId":5942447,"title":"Intermediate code from C++","tags":["c++","optimization","compiler-construction","cross-platform","intermediate-language"],"text":"Title: Intermediate code from C++\nTags: c++, optimization, compiler-construction, cross-platform, intermediate-language\nSource: Stack Overflow\n\nQuestion:\nI want to compile a C++ program to an intermediate code. Then, I want to compile the intermediate code for the current processor with all of its resources.\n\nThe first step is to compile the C++ program with optimizations (-O2), run the linker and do most of the compilation procedure. This step must be independent of operating system and architecture.\n\nThe second step is to compile the result of the first step, without the original source code, for the operating system and processor of the current computer, with optimizations and special instructions of the processor (-march=native). The second step should be fast and with minimal software requirements.\n\nCan I do it? How to do it?\n\n**Edit**:\n\nI want to do it, because I want to distribute a platform independent program that can use all resources of the processor, without the original source code, instead of distributing a compilation for each platform and operating system. It would be good if the second step be fast and easy.\n\nProcessors of the same architecture may have different features. X86 processors may have SSE1, SSE2 or others, and they can be 32 or 64 bit. If I compile for a generic X86, it will lack of SSE optimizations. After many years, processors will have new features, and the program will need to be compiled for new processors.\n\n========================================\n\nTop Answer:\nHow much do you know about compilers? You seem to treat \"-O2\" as some magical flag.\n\nFor instance, register assignment is a typical optimization. You definitely need to now how many registers are available. No point in assigning `foo` to register 16, and then discover in phase 2 that you're targetting an x86.\n\nAnd those architecture-dependent optimizations can be quite complex. Inlining depends critically on call cost, and that in turn depends on architecture.\n\n========================================\n\nCode:\n```text\nfoo\n```\n\n```text\ncc /P MyFile.cpp\ngcc -E MyFile.cpp\n```\n\n```text\n*.i\n```\n\n```text\nMyFile.i\n```\n\n```text\n#defines\n```\n\n```text\n*.i\n```\n\n```text\ndistcc\n```\n\n========================================\n\nComments:\n- Sounds like a job for clang/LLVM ?\n- We need a little more input to give useful advice. Which processors and operating systems are we talking about? Why exactly do you want to delay the last compilation step?\n- If you want to ship platform independent program, have a look at the java virtual machine. If you want to support different versions of x86, compile binaries for each version. (Alternatively you could just provide different versions of the most time critical functions.) And shipping code today for tomorrows x86 extensions is impossible. You will have to update your code for that. If you care about your future program performance, forget about sse and make sure your code can make use of as many processor cores as possible.\n- Actually, they do quite a bit of optimization.\n- @Jerry Most of that optimization is done by the JIT at run time.\n- The immediate representation “LLVM” *can* be JIT-compiled, but you don't have to. You can also use a normal backend and get native code. That said, both JIT-compiler and native backends will do some own optimization, but most of the expensive optimizations are done on the IR, independent of the backend (so they only have to be written *once*). LLVM is really a re-usable framework of frontends, backends and optimizations, all centered around a common IR. You can combine them in quite a lot of ways.\n- That said, LLVM should in theory be able to do something like this. It is even listed on the features page as “install-time/run-time/offline”-optimization (which are all a variation of optimization on the target system). However I'm not sure how much additional work is required to get something like that working. I know, that you can run frontend, backend and optimizations separately from the command line though (if LLVM is installed).","metadata":{"transformedAt":"2026-08-18T18:32:17.953Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":70,"estimatedTokens":1014}}1161{"id":"stack-35025146","source":"stackoverflow","questionId":35025146,"title":"Flex/Bison based lexer/parser for C language","tags":["c","compiler-construction","bison","flex-lexer","ansi-c"],"text":"Title: Flex/Bison based lexer/parser for C language\nTags: c, compiler-construction, bison, flex-lexer, ansi-c\nSource: Stack Overflow\n\nQuestion:\nIs there a simple lexer/parser for C language or a subset of it which is based on Flex/Bison?\n\nI have found some open source parsers for C (TCC, LCC, ...) but none of them are based on bison.\n\n========================================\n\nTop Answer:\nThere is a C11 grammar for YACC (a predecessor of Bison), it should work with Bison (maybe some tweaks will be needed).\n\nGCC used to be based on Bison a long ago. GCC 3.4 source code contains a file with C grammar.\n\n========================================\n\nComments:\n- Adding a comment stating the reason of downvote would be much more useful for me.","metadata":{"transformedAt":"2026-08-18T18:32:17.953Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":20,"estimatedTokens":186}}1162{"id":"stack-69794988","source":"stackoverflow","questionId":69794988,"title":"How to build dominance frontier for control flow graph?","tags":["graph","compiler-construction","ssa"],"text":"Title: How to build dominance frontier for control flow graph?\nTags: graph, compiler-construction, ssa\nSource: Stack Overflow\n\nQuestion:\nI would like to understand what a common principle is used for building Ф-functions for nodes.\nI read about \"**dominance frontier (DF)**\" relationship in a graph that allow to build Ф-functions. Here is an example with control-flow graph for simple code snippet:\ncontrol-flow-graph\n\nLet's consider the definition for DF:\n\n```\nDF is a set of nodes w such that x dominates predecessor of w, but x does not strictly dominate w\n```\n\nOkay, here is my understanding of this definition. Let's consider: `DF(B1) = { B3, B5, B6, B7 }`\nbecause:\n\n```\ndom(B1, B2) & !strictly_dom(B1, B3) & is_predecessor(B2, B3);\ndom(B1, B3) & !strictly_dom(B1, B5) & is_predecessor(B3, B5);\ndom(B1, B3) & !strictly_dom(B1, B6) & is_predecessor(B3, B6); \ndom(B1, B6) & !strictly_dom(B1, B7) & is_predecessor(B6, B7);\n```\n\nIs this right understanding of DF? Could you give me more detailed explanation, please?\n\n========================================\n\nCode:\n```text\nDF is a set of nodes w such that x dominates predecessor of w, but x does not strictly dominate w\n```\n\n```text\ndom(B1, B2) & !strictly_dom(B1, B3) & is_predecessor(B2, B3);\ndom(B1, B3) & !strictly_dom(B1, B5) & is_predecessor(B3, B5);\ndom(B1, B3) & !strictly_dom(B1, B6) & is_predecessor(B3, B6); \ndom(B1, B6) & !strictly_dom(B1, B7) & is_predecessor(B6, B7);\n```\n\n```text\nDF(B1) = { B3, B5, B6, B7 }\n```\n\n```text\nB2\n```\n\n```text\nB7\n```\n\n```text\nB2\n```\n\n```text\nB7\n```\n\n```text\nB7\n```\n\n```text\nB5\n```\n\n```text\nB6\n```\n\n```text\nB7\n```\n\n```text\nB7\n```\n\n```text\nB7\n```\n\n```text\nB7\n```\n\n```text\nB5\n```\n\n```text\nB6\n```\n\n```text\n{B7}\n```\n\n```text\nj\n```\n\n```text\nk\n```\n\n```text\nB7\n```\n\n```text\nB7\n```\n\n```text\nj <- ϕ((j, B5), (j, B6))\n```\n\n```text\nk <- ϕ((k, B5), (k, B6))\n```\n\n```text\nB2\n```\n\n```text\nB7\n```\n\n```text\nB2\n```\n\n```text\nB1\n```\n\n```text\nj <- ϕ((j, B1), (j, B7))\n```\n\n```text\nk <- ϕ((k, B1), (k, B7))\n```\n\n```text\ni\n```\n\n```text\ni\n```\n\n```text\ni\n```\n\n```text\n1\n```\n\n```text\nDF(B1)\n```\n\n```text\nB3 ∈ DF(B1)\n```\n\n```text\nB3\n```\n\n```text\nB2\n```\n\n```text\nB2\n```\n\n```text\nB2\n```\n\n```text\nB7\n```\n\n```text\nB1\n```\n\n```text\nB2\n```\n\n```text\nB3\n```\n\n========================================\n\nComments:\n- What does \"dominate\" mean in that context? Do you have a problem understanding the underlying principle or with the implementation in C? I assume the language part is not really relevant for your question.\n- @Gerhardh took \"C\" tag out\n- Wow! Thank you for clear explanation!\n- \"we have that `B5` and `B6` dominate a predecessor of `B7` (namely, themselves).\". This is very important point. I have not paid attention that a node can dominate itself. Also, I got that `not strictly dominates` means that this path is not the only way to get to this node (`B7` can be reached from dominator `B5`, but also from `B6`) and thus we get \"ramification\". Thank you!","metadata":{"transformedAt":"2026-08-18T18:32:17.953Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":43,"totalLines":212,"estimatedTokens":733}}1163{"id":"stack-59560902","source":"stackoverflow","questionId":59560902,"title":"How does a compiler, say gcc, version built years ago can still compile for a processor released recently?","tags":["gcc","optimization","compiler-construction","intel","compiler-optimization"],"text":"Title: How does a compiler, say gcc, version built years ago can still compile for a processor released recently?\nTags: gcc, optimization, compiler-construction, intel, compiler-optimization\nSource: Stack Overflow\n\nQuestion:\nLet's say I take a compiler: gcc 4.8. And processor from intel, let's say skylake or some other fancy new family.\n\nchecking this question: How to see which flags -march=native will activate?; if I do `gcc -march=native -E -v - &1 | grep cc1`, this will spout out some flags for the host machine, which is the above processor, skylake. \n\nHow does gcc know what flags to enable disable... when 4.8 was released before skylake processors were out? What about other newer family of processors?\n\nConsequently, next question is upgrading the compiler to latest necessary for it **accurately and optimally** compile for target processor which is new?\n\nThe question isn't really specific to gcc/intel, I would like to know how others maintain synchronicity between processor and compiler too.\n\n========================================\n\nTop Answer:\nIt can only happen if the new processor is specifically designed to be backwards compatible with older models.\n\nForget gcc for a moment. You have a compiled X86 binary from year 2000, say, an executable built for the original Windows NT. Will a Skylake CPU run it? You betcha. Will an Itanium CPU run iit? Nope, it is not designed to do that. It is a completely different architecture\n\nNow that executable most probably wouldn't use the Skylake efficiently, but that's the whole point of evolving architectures and introducing new instructions.\n\nReturning to gcc, `-march=native` is not magic. It cannot possibly divine out the new instructions and new timings. It simply selects the \"best\" instruction set it knows that is supported by the CPU it runs on. How it's done is architecture specific. X86 CPUs can be queried about their capabilities with the the CPUID instruction. Other architectures may do it differently.\n\nTo put it another way, `-O3 -march=native` optimizes for the machine you compiled on, so it's good when you're compiling code to run on the build host. A binary built with `-march=native` on a Nehalem system is essentially the same as one built with `-march=nehalem` on *any* system. `-march=native` might detect your specific L3 cache size instead of using a default for that, if any GCC tuning decisions (like inlining or unrolling) depend on L3 size. Except if you run an old compiler on a new CPU it doesn't recognize, you get feature detection for stuff like `-mavx` but for tuning only `tune=generic`.\n\nNone of this can take advantage of new features like AVX2 or BMI2 when running on a Skylake or Ice Lake system. And some specific tuning decisions that were good on Nehalem might be sub-optimal on a different CPU. (Although this is less likely; Intel mostly maintains backwards compatibility for performance as well as correctness. Getting everyone to recompile everything for P4 didn't work out so they usually try to make existing binaries run well on new CPUs.)\n\nSome compiles *can* make binaries that do *runtime* CPU detection and dispatching so they can take advantage of whatever a CPU supports, but only for extensions that the compiler knows about when it compiled. The AVX+FMA machine-code version of a function has to be there in the executable, so a compiler from before those were even announced wouldn't have been able to create such machine code. And before real CPUs with the features were available, compiler devs hadn't had a chance to tune code-gen for those features yet, so a newer compiler might make better code for the same CPU features.\n\n========================================\n\nCode:\n```text\ngcc -march=native -E -v - </dev/null 2>&1 | grep cc1\n```\n\n```text\n-march=haswell\n```\n\n```text\n-march=native\n```\n\n```text\n-mfma\n```\n\n```text\n-mbmi2\n```\n\n```text\n-mtune=skylake\n```\n\n```text\n-mtune=generic\n```\n\n```text\n-mtune=haswell\n```\n\n```text\n/proc/cpuinfo\n```\n\n```text\nrep foo\n```\n\n```text\nfoo\n```\n\n```text\npause\n```\n\n```text\nrep nop\n```\n\n```text\nlock\n```\n\n```text\nifunc\n```\n\n```text\n-march=native\n```\n\n```text\n-O3 -march=native\n```\n\n```text\n-march=native\n```\n\n```text\n-march=nehalem\n```\n\n```text\n-march=native\n```\n\n```text\n-mavx\n```\n\n```text\ntune=generic\n```\n\n========================================\n\nComments:\n- It can only happen if the new processor is specifically designed to be backwards compatible with older models.\n- \"this will spout out some flags for the host machine, which is the above processor, skylake.\" Are you saying that if you run that command using gcc 4.8, the resulting command line includes `-march=skylake`? Are you sure you're really invoking gcc 4.8?\n- @sepp2k march=native on gcc 4.8. I don't think gcc 4.8 has march=skylake support.\n- @themagicalyang I'm not sure I'm following what you're saying. What exactly is the output you see when you run `gcc -march=native -E -v - &1 | grep cc1`? I'd expect it to contain something like `-march=haswell` or `-march=broadwell` (whichever is the latest one that 4.8 supports). I would not expect `-march=skylake` (unless the latest version of 4.8 added support for that, perhaps) and I definitely would not expect `-march=native` (`cc1` does not understand `-march=native`).\n- @sepp2k 4.8 doesn't even understand broadwell. it has `march=core-avx2` and `mtune=generic`. on the broadwell i just ran it on.\n- @themagicalyang So what makes you say that it will \"spout out some flags for the host machine, which is the above processor, skylake\"? If the output says `march=core-avx2`, then it's apparently spouting flags for core-avx2 - not skylake. Or are you asking how it knows to use `march=core-avx2` for a skylake CPU?\n- The latter. Or in fact it should have had march=skylake but it doesn't and it led me to this question. Whether my compilation is even optimal, whether upgrading compilers is necessary, how does compiler know which flags to enable\n- \"It simply selects the architecture gcc was built for. Not the architecture it currenntly runs on.\" That's not true. It will detect the CPU that you're running GCC on or the closest variant it knows.\n- Are you saying that a older compiler can compile suboptimally for new architectures?\n- Whether the compilation is \"optimal\" or \"suboptimal\" will depend on the precise code. You certainly cannot *expect* it to be optimal for a processor which the compiler doesn't explicitly support.\n- Hmm, I ended up adding quite a lot of text to expand on the `-march=native` idea in what was your last paragraph. But yes, x86's main claim to fame / reason for popularity is strict backwards compatibility. A new CPU that fails to run some existing programs would be a lot harder to sell, so vendors don't do that. They'll even bend over backwards to go beyond the on-paper ISA docs to make sure existing code keeps working. e.g. former Intel architect Andy Glew said so in Observing stale instruction fetching on x86 with self-modifying code\n- After reading the question more carefully, I think much of this is tangential to what the OP was asking; oops. I posted my own answer.","metadata":{"transformedAt":"2026-08-18T18:32:17.953Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":22,"totalLines":138,"estimatedTokens":1777}}1164{"id":"stack-27763943","source":"stackoverflow","questionId":27763943,"title":"what is target architecture in computer science?","tags":["gcc","architecture","compiler-construction","cpu-architecture"],"text":"Title: what is target architecture in computer science?\nTags: gcc, architecture, compiler-construction, cpu-architecture\nSource: Stack Overflow\n\nQuestion:\nI am a beginner in programming and wanted to download a good C compiler to practice coding. So I thought of GCC and started a small research on it. I read a Wikipedia article on it. The article mentioned something about target architecture,which I do not know. Can anyone tell me what it means, and any source I can refer for more information. Thanks in advance.\n\n========================================\n\nCode:\n```text\nx86_64-apple-darwin14.0.0\ni386-pc-mingw32\ni686-pc-linux-gnu\n```\n\n```text\nmachine-vendor-operatingsystem (not always followed though)\n```\n\n========================================\n\nComments:\n- It's what you run teh code on......","metadata":{"transformedAt":"2026-08-18T18:32:17.953Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":24,"estimatedTokens":201}}1165{"id":"stack-45755642","source":"stackoverflow","questionId":45755642,"title":"Semantics of volatile","tags":["c","compiler-construction","language-lawyer","volatile"],"text":"Title: Semantics of volatile\nTags: c, compiler-construction, language-lawyer, volatile\nSource: Stack Overflow\n\nQuestion:\nFor the sake of this question, let us look at only reads on `volatile` variables. All the discussions I have read, the only conclusion is that multiple reads on the same variable declared volatile cannot be optimized out to a single effect. \n\nBut I believe that is a bit strict. Consider two reads on a variable, which do not have any side effect between them, or do not have read of any other volatile variable between them. \n\nNow we know that the value in a volatile variable can change any time (without the compiler having a hint of it). But there is no way for the programmer to ensure that the change will happen *between* the two reads. This means that both the reads seeing the same value is a valid behavior for the program.\n\nSo can't the compiler enforce this behavior? Doing a single read and using the value twice. \n\nFor example \n\n```\nint foo(volatile int * x) {\n return *x + *x;\n}\n```\n\nCan the compiler do a single read in this case?\n\nI hope my query is clear.\n\nAlso I am assuming a system where the read itself doesn't have a side effect (like increment of a counter, or value changing with every read). Or do such systems exist?\n\nI have looked at the assembly generated from `gcc` and `clang` and they do insert two reads even with maximum optimizations. My question is are they overly conservative?\n\nEdit: To not complicate my question and avoid confusion with implementation defined order of evaluation of sub expressions, we can look at the example - \n\n```\nint foo(volatile int * x) {\n int a = *x;\n int b = *y;\n return a + b;\n}\n```\n\nBut I am also retaining the previous example because some answers and comments have referenced that.\n\n========================================\n\nTop Answer:\nNow we know that the value in a volatile variable can change any time (without the compiler having a hint of it). But there is no way for the programmer to ensure that the change will happen between the two reads. This means that both the reads seeing the same value is a valid behavior for the program.\n\nYou take a wrong conclusion from this.\nIt may happen that the value does not change. But you don't know. And the compiler doesn't know.\n\nIf the compiler doesn't know why should the compiler be allowed to assume anything about changes?\nTherefore clearly: NO! The compiler mustn't combine any read access here.\n\nThat's a weird assumption.\nIf you cannot ensure that all rabbits are white, how could it be a valid assumption that all are black?\n\nIt can also happen that the first read itself causes the value to change.\n\nIf you look at some hardware, it can be vital to do read accesses separately.\nSome timers or interrupt controllers clear some bits when they are read.\n\nAlso UARTs or Ethernet controllers might provide the whole receive buffer via one single address. You MUST read multiple times from the same address.\n\nAnd the `volatile` keyword is the means to prevent the compiler from doing tricks.\n\n========================================\n\nCode:\n```text\nint foo(volatile int * x) {\n    return *x + *x;\n}\n```\n\n```text\nint foo(volatile int * x) {\n    int a = *x;\n    int b = *y;\n    return a + b;\n}\n```\n\n```text\nvolatile\n```\n\n```text\ngcc\n```\n\n```text\nclang\n```\n\n```text\nint foo2(volatile int *x) {\n    int x1 = *x;\n    int x2 = *x;\n    return x1 + x2;\n}\n```\n\n```text\nfoo\n```\n\n```text\nfoo\n```\n\n```text\nfoo2\n```\n\n```text\nint foo(volatile int * x) { return *x + *x; }\n```\n\n```text\nint foo(volatile int * x) { int x_cp = *x; return x_cp + x_cp; }\n```\n\n```text\n-O1\n```\n\n```text\nvolatile\n```\n\n========================================\n\nComments:\n- Further more in this case, the evaluation order of operands for `+` are left implementation independent. Can the programmer make any assumption now?\n- Volatile is much like high level assembly. Store it in register on your own behalf if you really want to \"optimize\".\n- @TatsuyukiIshi I agree! But the programmer will not see any changes in observable defined behavior.\n- Well if you have a box labeled \"explosives\" , can the postman treat it as non-explosive, since it doesn't explode that often? Of course he can!111!!!\n- @joop but here it is guaranteed to not explode. It is guaranteed to behave like one of the acceptable behaviors.\n- It has to do two reads. Volatile accesses can't be optimized out. They're like IO.\n- The programmer can indeed not make any assumption about if `*x` is executed before or after `*x` :) Which obviously doesn't matter. It could have mattered if they were different variables. Example: \"clear the SPI status register by reading the data register followed by a read of the status register\". Then the code `something = *SPIDR + *SPISR` would be incorrect, since the order of evaluation is not only implementation-defined, but *unspecified*, meaning we can't know anything about it, and it may vary from case to case.\n- @Lundin to avoid confusion, I have added sequence points between the reads. And in the case of single variable what would you say?\n- Possible duplicate of Why is volatile needed in C? It is clear, variable declared as `volatile` will always execute read command when used. Even if sequence is used! Period. End of story.\n- @tilz0R unfortunately it is not a duplicate. I have a very specific case in mind. I am not asking about use of volatile in general. I am asking about a specific case that is not covered in that question.\n- @AjayBrahmakshatriya Again, they are read 2 times as variable is used twice. If you are reading from memory where there are multiple possible acceses, 2 reads must be executed.\n- It does not make any sense. Just do not declare parameter as volatile. I understand that you have done it for the reason. The compiler will complain about it as well.\n- @PeterJ It does make sense. If he wants foo to do exactly one read, for whatever reason, this is the way. Sure if this were a separately compiled function, he could simply remove the volatile qualifier, but without context, `foo` is potential candidate for inlining, in which case, without the volatile, there could be no read at all.\n- @PeterJ Exactly, and that's why if you want `foo` to always do exactly 1 read on x, the volatile qualifier isn't optional.\n- unfortunately I cant understand what you mean, as the sentence you wrote does no make any sense\n- If you do not need volatile do not use it, if you need don't abuse it. Simple\n- @PeterJ \" Just do not declare parameter as volatile. \" doesn't make sense if `foo` should always generate exactly one read on x. If `foo` is potentially inlininable, the volatile qualifier needs to be there.\n- @PSkocik: If the object at which `*x` points is declared `int volatile`, then compilers may behave arbitrarily unless `x` is an `int volatile*`, *even if the object at which `x` points is never actually modified by any outside means*.\n- Hence I added the para *Also I am assuming a system where the read itself doesn't have a side effect (like increment of a counter, or value changing with every read). Or do such systems exist?* Thank you for providing an example of such a system.\n- Now, how do these things fit into a standard? Do these all fit into some or other \"implementation defined behaviors\"? This kind of leads me to conclude that `volatile` should be used only when programming for a particular target. It does little change to the program in portable programs.\n- I agree I should have used two sequenced reads to not confuse with order of evaluation.\n- The variable could be shared among threads or processes in a portable way.\n- I'm not convinced there's a problem with `foo()` because `+` is symmetric, but if the operations using the values were not symmetric, the explicit read'n'save notation could easily be necessary.\n- @JeanBaptisteYunes if it is just a matter of threads, it should be fine to have a single read since the other thread(the one changing the value) could get scheduled after both the reads and that would have similar observable behaviour to a single read.\n- @Gilles and bunch of warnings. If parameter does not have to be volatile, do not make it volatile\n- @Jean-BaptisteYun&#232;s True, since C11. But even in C11, can you have an effectful *read* without using implementation-defined (or undefined) behavior?\n- @JonathanLeffler I don't know if there are platforms that actually do this, but there may be some kind of minimum separation between two reads that requires a sequence point. It isn't just about ordering. For example maybe the bus can issue multiple reads in the same clock cycle, but two reads from the same address need to be scheduled on different cycles or else the peripheral will only see one read, and the compiler only takes care of scheduling the reads properly if they're separated by a sequence point.\n- C can't express constraints on the time between two reads. If that's an issue, there is nothing that can be done in standard C to meet the hardware constraint.\n- @AjayBrahmakshatriya Before C11, `volatile` was only useful in one place in a strictly conforming program: for `volatile sig_atomic_t` values that are modified by a signal handler. Anything else that makes `volatile` useful required some implementation-defined way of sharing memory between multiple threads or with something outside the program. That doesn't mean that all code using `volatile` is target-specific: there are plenty of portable libraries that are designed to use in programs that are specific to environments with some form of concurrency.\n- @JonathanLeffler C can't express these constraints. That's the job of the compiler. But it would be legitimate for a compiler to take these constraints into account only at a sequence point, since the behavior of two volatile reads without an intervening sequence point is undefined.\n- We're headed on a collision course. I don't see how the compiler can know about those constraints because they can't be expressed in C; you seem to think it can know about them despite that. Let's stop the conversation here. We'll not make useful progress.\n- @Gilles I think this answer has cleared my queries to a significant extent. I will accept it.\n- My argument was that if both reads can see the value, the programmer cannot complain if they are made to see the same value. The analogy would be if it is not guaranteed that all rabbits are white, I can make on rabbit black (forcefully). Since the standard doesn't say that all rabits should be white, it should be okay.\n- About the second half I agree. And that I mentioned in the question too. My side question was what are examples of systems where read has immediate side effects? Thanks for the examples.\n- @AjayBrahmakshatriya. No, your assumption is still wrong. If I cannot ensure all rabbits to be white, I might still ensure that there is only one single black rabbit around. The compiler (or you) do not have any right to make both rabbits black ones. You make invalid assumptions. Maybe the programmer does not know exactly which results will be read, the compiler definitely does not have any clue and its not the compiler's business to make some fancy guesses.","metadata":{"transformedAt":"2026-08-18T18:32:17.957Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":13,"totalLines":167,"estimatedTokens":2791}}1166{"id":"stack-25839091","source":"stackoverflow","questionId":25839091,"title":"Bison Issues - Start Symbol does not derive any sentence","tags":["c++","compiler-construction","bison","flex-lexer"],"text":"Title: Bison Issues - Start Symbol does not derive any sentence\nTags: c++, compiler-construction, bison, flex-lexer\nSource: Stack Overflow\n\nQuestion:\nI keep getting \"Fatal Error: start symbol N_START does not derive any sentence. I am completely lost on what could be wrong with this code. \n\nI have looked at previous versions and I can't find anything that I am doing differently. \n\nHere is the code (It is long D:)\n\n```\n%error-verbose\n%{\n\n#include \n#include \n#include \n#include \n#include \n\nusing namespace std;\n\nvoid printTokenInfo(char* tokenType, char* lexeme);\nvoid printRule(const char *lhs, const char *rhs);\nint yyerror(const char *s);\n\nint numLines = 0; \n\nextern \"C\" {\n int yyparse(void);\n int yylex(void);\n int yywrap() { return 1; }\n}\n\n%}\n\n/* Token declarations */\n%token T_ASSIGN T_MULT T_PLUS T_MINUS T_DIV\n%token T_AND T_OR T_NOT T_LT T_GT\n%token T_LE T_GE T_EQ T_NE T_VAR\n%token T_ARRAY T_OF T_BOOL T_CHAR T_INT\n%token T_PROG T_PROC T_BEGIN T_END T_WHILE\n%token T_DO T_IF T_READ T_UNKNOWN\n%token T_WRITE T_TRUE T_FALSE T_LBRACK T_RBRACK\n%token T_SCOLON T_COLON T_LPAREN T_RPAREN T_COMMA\n%token T_DOT T_DOTDOT T_INTCONST T_CHARCONST T_IDENT\n\n%nonassoc T_THEN\n%nonassoc T_ELSE\n\n/* Starting point */\n%start N_START\n\n%%\nN_START : N_PROG\n {\n printRule(\"START\", \"PROG\");\n return 0;\n }\n ;\nN_PROG : N_PROGLBL T_IDENT T_SCOLON N_BLOCK T_DOT\n {\n printRule(\"PROG\", \"PROG_LBL ident ; BLOCK.\");\n }\n ;\nN_PROGLBL : T_PROG\n {\n printRule(\"PROG_LBL\", \"T_PROGTOKEN\");\n }\n ;\nN_BLOCK : N_VAR_DEC N_PROC_DEC N_STMT\n {\n printRule(\"BLOCK\", \"VAR_DEC PROC_DEC STMT\");\n }\n ;\nN_VAR_DEC_PART : //Epsilon\n {\n printRule(\"VAR_DEC_PART\", \"epsilon\");\n }\n | T_VAR N_VAR_DEC T_SCOLON N_VAR_DEC_LST\n {\n printRule(\"VAR_DEC_PART\", \"var VAR_DEC : VAR_DEC_LST\");\n }\n ;\nN_VAR_DEC_LST : //Epsilon\n {\n printRule(\"VAR_DEC_LST\", \"epsilon\");\n }\n | N_VAR_DEC T_SCOLON N_VAR_DEC_LST\n {\n printRule(\"VAR_DEC_LST\", \"VAR_DEC : VAR_DEC_LST\");\n }\n ;\nN_VAR_DEC : T_IDENT N_VAR_DEC_LST T_COLON N_TYPE\n {\n printRule(\"VAR_DEC\", \"ident VAR_DEC_LST : TYPE\");\n }\n ;\nN_IDENT : T_IDENT\n {\n printRule(\"IDENT\", \"ident\");\n }\n ;\nN_IDENT_LST : //Epsilon\n {\n printRule(\"IDENT_LST\", \"epsilon\");\n }\n | T_COMMA N_IDENT N_IDENT_LST\n {\n printRule(\"IDENT_LST\", \", IDENT IDENT_LST\");\n }\n ;\nN_TYPE : N_SIMPLE\n {\n printRule(\"TYPE\", \"SIMPLE\");\n }\n | N_ARRAY\n {\n printRule(\"TYPE\", \"ARRAY\");\n }\n ;\nN_ARRAY : T_ARRAY T_LBRACK N_IDX_RANGE T_RBRACK T_OF N_SIMPLE\n {\n printRule(\"ARRAY\", \"ARRAY ( IDX_RANGE ) of SIMPLE\");\n }\n ;\nN_IDX : N_INTCONST\n {\n printRule(\"IDX\", \"IN_CONST\");\n }\n ;\nN_IDX_RANGE : N_IDX T_DOTDOT N_IDX\n {\n printRule(\"IDX_RANGE\", \"IDX .. IDX\");\n }\n ;\nN_SIMPLE : T_INTCONST\n {\n printRule(\"SIMPLE\", \"intconst\");\n }\n | T_CHARCONST\n {\n printRule(\"SIMPLE\", \"charconst\");\n }\n | T_BOOL\n {\n printRule(\"SIMPLE\", \"bool\");\n }\n ;\nN_PROC_DEC_PART : //Epsilon\n {\n printRule(\"PROC_DEC_PART\", \"epsilon\");\n }\n | N_PROC_DEC T_SCOLON N_PROC_DEC_PART\n {\n printRule(\"PROC_DEC_PART\", \"PROC_DEC : PROC_DEC_PART\");\n }\n ;\nN_PROC_DEC : N_PROC_HDR N_BLOCK\n {\n printRule(\"PROC_DEC\", \"PROC_HDR BLOCK\");\n }\n ;\nN_PROC_HDR : T_PROC T_IDENT T_SCOLON\n {\n printRule(\"PROC_HDR\", \"proc ident :\");\n }\n ;\nN_STMT_PART : N_COMPOUND\n {\n printRule(\"STMT_PART\", \"COMPOUND\");\n }\n ;\nN_COMPOUND : T_BEGIN N_STMT N_STMT_LST T_END\n {\n printRule(\"COMPOUND\", \"begin STMT STMT_LST end\");\n }\n ;\nN_STMT_LST : //Epsilon\n {\n printRule(\"STMT_LST\", \"epsilon\");\n }\n | T_SCOLON N_STMT N_STMT_LST\n {\n printRule(\"STMT_LST\", \": STMT STMT_LST\");\n }\n ;\nN_STMT : N_ASSIGN\n {\n printRule(\"STMT\", \"ASSIGN\");\n }\n | N_PROC_STMT\n {\n printRule(\"STMT\", \"PROC_STMT\");\n }\n | N_READ\n {\n printRule(\"STMT\", \"READ\");\n }\n | N_WRITE\n {\n printRule(\"STMT\", \"WRITE\");\n }\n | N_CONDITION\n {\n printRule(\"STMT\", \"CONDITION\");\n }\n | N_WHILE\n {\n printRule(\"STMT\", \"WHILE\");\n }\n | N_COMPOUND\n {\n printRule(\"STMT\", \"COMPOUND\");\n }\n ;\nN_ASSIGN : N_VAR T_ASSIGN N_EXPR\n {\n printRule(\"ASSIGN\", \"VAR := EXPR\");\n }\n ;\nN_PROC_STMT : N_PROC_IDENT\n {\n printRule(\"PROC_STMT\", \"PROC_IDENT\");\n }\n ;\nN_PROC_IDENT : T_IDENT\n {\n printRule(\"PROC_IDENT\", \"ident\");\n }\n ;\nN_READ : T_READ T_LPAREN N_INPUT_VAR N_INPUT_LST T_RPAREN\n {\n printRule(\"READ\", \"read ( INPUT INPUT_LST )\");\n }\n ;\nN_INPUT_LST : //Epsilon\n {\n printRule(\"INPUT_LST\", \"epsilon\");\n }\n | T_COMMA N_INPUT_VAR N_INPUT_LST\n {\n printRule(\"INPUT_LST\", \", INPUT_VAR INPUT_LST\");\n }\n ;\nN_INPUT_VAR : N_VAR\n {\n printRule(\"INPUT_VAR\", \"VAR\");\n }\n ;\nN_WRITE : T_WRITE T_LPAREN N_OUTPUT N_OUTPUT_LST T_RPAREN\n {\n printRule(\"WRITE\", \"write ( OUTPUT OUTPUT_LST )\");\n }\n ;\nN_OUTPUT_LST : //Epsilon\n {\n printRule(\"OUTPUT_LST\", \"epsilon\");\n }\n | T_COMMA N_OUTPUT N_OUTPUT_LST\n {\n printRule(\"OUTPUT_LST\", \", OUTPUT OUTPUT_LST\");\n }\n ;\nN_OUTPUT : N_EXPR\n {\n printRule(\"OUTPUT\", \"EXPR\");\n }\n ;\nN_CONDITION : T_IF N_EXPR T_THEN N_STMT\n {\n printRule(\"CONDITION\", \"if EXPR then STMT\");\n }\n | T_IF N_EXPR T_THEN N_STMT T_ELSE N_STMT\n {\n printRule(\"CONDITION\", \"if EXPR then STMT else STMT\");\n }\n ;\nN_WHILE : T_WHILE N_EXPR T_DO N_STMT\n {\n printRule(\"WHILE\", \"while EXPR do STMT\");\n }\n ;\nN_EXPR : N_SIMPLE_EXPR\n {\n printRule(\"EXPR\", \"SIMPLE_EXPR\");\n }\n | N_SIMPLE_EXPR N_REL_OP N_SIMPLE_EXPR\n {\n printRule(\"EXPR\", \"SIMPLE_EXPR REL_OP SIMPLE_EXPR\");\n }\n ;\nN_SIMPLE_EXPR : N_TERM N_ADD_OP_LST\n {\n printRule(\"SIMPLE_EXPR\", \"TERM ADD_OP_LST\");\n }\n ;\nN_ADD_OP_LST : //Epsilon\n {\n printRule(\"ADD_OP_LST\", \"epsilon\");\n }\n | N_ADD_OP N_TERM N_ADD_OP_LST\n {\n printRule(\"ADD_OP_LST\", \"ADD_OP TERM ADD_OP_LST\");\n }\n ;\nN_TERM : N_FACTOR N_MULT_OP_LST\n {\n printRule(\"TERM\", \"FACTOR MULT_OP_LST\");\n }\n ;\nN_MULT_OP_LST : //Epsilon\n {\n printRule(\"MULT_OP_LST\", \"epsilon\");\n }\n | N_MULT_OP N_FACTOR N_MULT_OP_LST\n {\n printRule(\"MULT_OP_LST\", \"MULT_OP FACTOR MULT_OP_LST\");\n }\n ;\nN_FACTOR : N_SIGN N_VAR\n {\n printRule(\"FACTOR\", \"SIGN VAR\");\n }\n | N_CONST\n {\n printRule(\"FACTOR\", \"CONST\");\n }\n | T_LPAREN N_EXPR T_RPAREN\n {\n printRule(\"FACTOR\", \"( EXPR )\");\n }\n | T_NOT N_FACTOR\n {\n printRule(\"FACTOR\", \"not FACTOR\");\n }\n ;\nN_SIGN : //Epsilon\n {\n printRule(\"SIGN\", \"epsilon\");\n }\n | T_PLUS\n {\n printRule(\"SIGN\", \"+\");\n }\n | T_MINUS\n {\n printRule(\"SIGN\", \"-\");\n }\n ;\nN_ADD_OP : T_PLUS\n {\n printRule(\"ADD_OP\", \"+\");\n }\n | T_MINUS\n {\n printRule(\"ADD_OP\", \"-\");\n }\n | T_OR\n {\n printRule(\"ADD_OP\", \"or\");\n }\n ;\nN_MULT_OP : T_MULT\n {\n printRule(\"MULT_OP\", \"*\");\n }\n | T_DIV\n {\n printRule(\"MULT_OP\", \"/\");\n }\n | T_AND\n {\n printRule(\"MULT_OP\", \"and\");\n }\n ;\nN_REL_OP : T_LT\n {\n printRule(\"REL_OP\", \"\");\n }\n | T_LE\n {\n printRule(\"REL_OP\", \"=\");\n }\n | T_EQ\n {\n printRule(\"REL_OP\", \"=\");\n }\n ;\nN_VAR : N_ENTIRE_VAR\n {\n printRule(\"VAR\", \"ENTIRE_VAR\");\n }\n | N_IDX_VAR\n {\n printRule(\"VAR\", \"IDX_VAR\");\n }\n ;\nN_IDX_VAR : N_ARRAY_VAR T_LBRACK N_EXPR T_RBRACK\n {\n printRule(\"IDX_VAR\", \"ARRAY_VAR ( EXPR )\");\n }\n ;\nN_ARRAY_VAR : N_ENTIRE_VAR\n {\n printRule(\"ARRAY_VAR\", \"ENTIRE_VAR\");\n }\n ;\nN_ENTIRE_VAR : N_VAR_IDENT\n {\n printRule(\"ENTIRE_VAR\", \"VAR_IDENT\");\n }\n ;\nN_VAR_IDENT : T_IDENT\n {\n printRule(\"VAR_IDENT\", \"IDENT\");\n }\n ;\nN_CONST : N_INTCONST\n {\n printRule(\"CONST\", \"INTCONST\");\n }\n | T_CHARCONST\n {\n printRule(\"CONST\", \"CHARCONST\");\n }\n | N_BOOLCONST\n {\n printRule(\"CONST\", \"BOOLCONST\");\n }\n ;\nN_INTCONST : N_SIGN T_INTCONST\n {\n printRule(\"INTCONST\", \"SIGN intconst\");\n }\n ;\nN_BOOLCONST : T_TRUE\n {\n printRule(\"BOOLCONST\", \"true\");\n }\n | T_FALSE\n {\n printRule(\"BOOLCONST\", \"false\");\n }\n ;\n%%\n\n#include \"lex.yy.c\"\nextern FILE *yyin;\n\nvoid printRule(const char *lhs, const char *rhs) {\n printf(\"%s -> %s\\n\", lhs, rhs);\n return;\n}\n\nint yyerror(const char *s) {\n printf(\"%s\\n\", s);\n return(1);\n}\n\nvoid printTokenInfo(const char* tokenType, const char* lexeme) {\n printf(\"TOKEN: %s LEXEME: %s\\n\", tokenType, lexeme);\n}\n\nint main() {\n do {\n yyparse();\n } while (!feof(yyin));\n\n printf(\"%d lines processed\\n\", numLines);\n\n return 0;\n}\n```\n\nAny ideas?\n\n========================================\n\nCode:\n```text\n%error-verbose\n%{\n\n#include <iostream>\n#include <stdio.h>\n#include <math.h>\n#include <string>\n#include <cstring>\n\nusing namespace std;\n\nvoid printTokenInfo(char* tokenType, char* lexeme);\nvoid printRule(const char *lhs, const char *rhs);\nint yyerror(const char *s);\n\nint numLines = 0; \n\nextern \"C\" {\n int yyparse(void);\n int yylex(void);\n int yywrap() { return 1; }\n}\n\n%}\n\n/* Token declarations */\n%token T_ASSIGN T_MULT T_PLUS T_MINUS T_DIV\n%token T_AND T_OR T_NOT T_LT T_GT\n%token T_LE T_GE T_EQ T_NE T_VAR\n%token T_ARRAY T_OF T_BOOL T_CHAR T_INT\n%token T_PROG T_PROC T_BEGIN T_END T_WHILE\n%token T_DO T_IF T_READ T_UNKNOWN\n%token T_WRITE T_TRUE T_FALSE T_LBRACK T_RBRACK\n%token T_SCOLON T_COLON T_LPAREN T_RPAREN T_COMMA\n%token T_DOT T_DOTDOT T_INTCONST T_CHARCONST T_IDENT\n\n%nonassoc T_THEN\n%nonassoc T_ELSE\n\n/* Starting point */\n%start N_START\n\n%%\nN_START : N_PROG\n    {\n        printRule(\"START\", \"PROG\");\n        return 0;\n    }\n    ;\nN_PROG  : N_PROGLBL T_IDENT T_SCOLON N_BLOCK T_DOT\n    {\n        printRule(\"PROG\", \"PROG_LBL ident ; BLOCK.\");\n    }\n    ;\nN_PROGLBL : T_PROG\n    {\n        printRule(\"PROG_LBL\", \"T_PROGTOKEN\");\n    }\n    ;\nN_BLOCK : N_VAR_DEC N_PROC_DEC N_STMT\n    {\n        printRule(\"BLOCK\", \"VAR_DEC PROC_DEC STMT\");\n    }\n    ;\nN_VAR_DEC_PART  :   //Epsilon\n    {\n        printRule(\"VAR_DEC_PART\", \"epsilon\");\n    }\n            |   T_VAR N_VAR_DEC T_SCOLON N_VAR_DEC_LST\n    {\n        printRule(\"VAR_DEC_PART\", \"var VAR_DEC : VAR_DEC_LST\");\n    }\n    ;\nN_VAR_DEC_LST   :   //Epsilon\n    {\n        printRule(\"VAR_DEC_LST\", \"epsilon\");\n    }\n                |   N_VAR_DEC T_SCOLON N_VAR_DEC_LST\n    {\n        printRule(\"VAR_DEC_LST\", \"VAR_DEC : VAR_DEC_LST\");\n    }\n    ;\nN_VAR_DEC   :   T_IDENT N_VAR_DEC_LST T_COLON N_TYPE\n    {\n        printRule(\"VAR_DEC\", \"ident VAR_DEC_LST : TYPE\");\n    }\n    ;\nN_IDENT     :   T_IDENT\n    {\n        printRule(\"IDENT\", \"ident\");\n    }\n    ;\nN_IDENT_LST :   //Epsilon\n    {\n        printRule(\"IDENT_LST\", \"epsilon\");\n    }\n            |   T_COMMA N_IDENT N_IDENT_LST\n    {\n        printRule(\"IDENT_LST\", \", IDENT IDENT_LST\");\n    }\n    ;\nN_TYPE  :   N_SIMPLE\n    {\n        printRule(\"TYPE\", \"SIMPLE\");\n    }\n        |   N_ARRAY\n    {\n        printRule(\"TYPE\", \"ARRAY\");\n    }\n    ;\nN_ARRAY :   T_ARRAY T_LBRACK N_IDX_RANGE T_RBRACK T_OF N_SIMPLE\n    {\n        printRule(\"ARRAY\", \"ARRAY ( IDX_RANGE ) of SIMPLE\");\n    }\n    ;\nN_IDX   :   N_INTCONST\n    {\n        printRule(\"IDX\", \"IN_CONST\");\n    }\n    ;\nN_IDX_RANGE :   N_IDX T_DOTDOT N_IDX\n    {\n        printRule(\"IDX_RANGE\", \"IDX .. IDX\");\n    }\n    ;\nN_SIMPLE    :   T_INTCONST\n    {\n        printRule(\"SIMPLE\", \"intconst\");\n    }\n            |   T_CHARCONST\n    {\n        printRule(\"SIMPLE\", \"charconst\");\n    }\n            |   T_BOOL\n    {\n        printRule(\"SIMPLE\", \"bool\");\n    }\n    ;\nN_PROC_DEC_PART :   //Epsilon\n    {\n        printRule(\"PROC_DEC_PART\", \"epsilon\");\n    }\n                |   N_PROC_DEC T_SCOLON N_PROC_DEC_PART\n    {\n        printRule(\"PROC_DEC_PART\", \"PROC_DEC : PROC_DEC_PART\");\n    }\n    ;\nN_PROC_DEC  :   N_PROC_HDR N_BLOCK\n    {\n        printRule(\"PROC_DEC\", \"PROC_HDR BLOCK\");\n    }\n    ;\nN_PROC_HDR  :   T_PROC T_IDENT T_SCOLON\n    {\n        printRule(\"PROC_HDR\", \"proc ident :\");\n    }\n    ;\nN_STMT_PART :   N_COMPOUND\n    {\n        printRule(\"STMT_PART\", \"COMPOUND\");\n    }\n    ;\nN_COMPOUND  :   T_BEGIN N_STMT N_STMT_LST T_END\n    {\n        printRule(\"COMPOUND\", \"begin STMT STMT_LST end\");\n    }\n    ;\nN_STMT_LST  :   //Epsilon\n    {\n        printRule(\"STMT_LST\", \"epsilon\");\n    }\n            |   T_SCOLON N_STMT N_STMT_LST\n    {\n        printRule(\"STMT_LST\", \": STMT STMT_LST\");\n    }\n    ;\nN_STMT  :   N_ASSIGN\n    {\n        printRule(\"STMT\", \"ASSIGN\");\n    }\n        |   N_PROC_STMT\n    {\n        printRule(\"STMT\", \"PROC_STMT\");\n    }\n        |   N_READ\n    {\n        printRule(\"STMT\", \"READ\");\n    }\n        |   N_WRITE\n    {\n        printRule(\"STMT\", \"WRITE\");\n    }\n        |   N_CONDITION\n    {\n        printRule(\"STMT\", \"CONDITION\");\n    }\n        |   N_WHILE\n    {\n        printRule(\"STMT\", \"WHILE\");\n    }\n        |   N_COMPOUND\n    {\n        printRule(\"STMT\", \"COMPOUND\");\n    }\n    ;\nN_ASSIGN    :   N_VAR T_ASSIGN N_EXPR\n    {\n        printRule(\"ASSIGN\", \"VAR := EXPR\");\n    }\n    ;\nN_PROC_STMT :   N_PROC_IDENT\n    {\n        printRule(\"PROC_STMT\", \"PROC_IDENT\");\n    }\n    ;\nN_PROC_IDENT    :   T_IDENT\n    {\n        printRule(\"PROC_IDENT\", \"ident\");\n    }\n    ;\nN_READ  :   T_READ T_LPAREN N_INPUT_VAR N_INPUT_LST T_RPAREN\n    {\n        printRule(\"READ\", \"read ( INPUT INPUT_LST )\");\n    }\n    ;\nN_INPUT_LST :   //Epsilon\n    {\n        printRule(\"INPUT_LST\", \"epsilon\");\n    }\n            |   T_COMMA N_INPUT_VAR N_INPUT_LST\n    {\n        printRule(\"INPUT_LST\", \", INPUT_VAR INPUT_LST\");\n    }\n    ;\nN_INPUT_VAR :   N_VAR\n    {\n        printRule(\"INPUT_VAR\", \"VAR\");\n    }\n    ;\nN_WRITE :   T_WRITE T_LPAREN N_OUTPUT N_OUTPUT_LST T_RPAREN\n    {\n        printRule(\"WRITE\", \"write ( OUTPUT OUTPUT_LST )\");\n    }\n    ;\nN_OUTPUT_LST    :   //Epsilon\n    {\n        printRule(\"OUTPUT_LST\", \"epsilon\");\n    }\n                |   T_COMMA N_OUTPUT N_OUTPUT_LST\n    {\n        printRule(\"OUTPUT_LST\", \", OUTPUT OUTPUT_LST\");\n    }\n    ;\nN_OUTPUT    :   N_EXPR\n    {\n        printRule(\"OUTPUT\", \"EXPR\");\n    }\n    ;\nN_CONDITION :   T_IF N_EXPR T_THEN N_STMT\n    {\n        printRule(\"CONDITION\", \"if EXPR then STMT\");\n    }\n            |   T_IF N_EXPR T_THEN N_STMT T_ELSE N_STMT\n    {\n        printRule(\"CONDITION\", \"if EXPR then STMT else STMT\");\n    }\n    ;\nN_WHILE :   T_WHILE N_EXPR T_DO N_STMT\n    {\n        printRule(\"WHILE\", \"while EXPR do STMT\");\n    }\n    ;\nN_EXPR  :   N_SIMPLE_EXPR\n    {\n        printRule(\"EXPR\", \"SIMPLE_EXPR\");\n    }\n        |   N_SIMPLE_EXPR N_REL_OP N_SIMPLE_EXPR\n    {\n        printRule(\"EXPR\", \"SIMPLE_EXPR REL_OP SIMPLE_EXPR\");\n    }\n    ;\nN_SIMPLE_EXPR   :   N_TERM N_ADD_OP_LST\n    {\n        printRule(\"SIMPLE_EXPR\", \"TERM ADD_OP_LST\");\n    }\n    ;\nN_ADD_OP_LST    :   //Epsilon\n    {\n        printRule(\"ADD_OP_LST\", \"epsilon\");\n    }\n                |   N_ADD_OP N_TERM N_ADD_OP_LST\n    {\n        printRule(\"ADD_OP_LST\", \"ADD_OP TERM ADD_OP_LST\");\n    }\n    ;\nN_TERM  :   N_FACTOR N_MULT_OP_LST\n    {\n        printRule(\"TERM\", \"FACTOR MULT_OP_LST\");\n    }\n    ;\nN_MULT_OP_LST   :   //Epsilon\n    {\n        printRule(\"MULT_OP_LST\", \"epsilon\");\n    }\n                |   N_MULT_OP N_FACTOR N_MULT_OP_LST\n    {\n        printRule(\"MULT_OP_LST\", \"MULT_OP FACTOR MULT_OP_LST\");\n    }\n    ;\nN_FACTOR    :   N_SIGN N_VAR\n    {\n        printRule(\"FACTOR\", \"SIGN VAR\");\n    }\n            |   N_CONST\n    {\n        printRule(\"FACTOR\", \"CONST\");\n    }\n            |   T_LPAREN N_EXPR T_RPAREN\n    {\n        printRule(\"FACTOR\", \"( EXPR )\");\n    }\n            |   T_NOT N_FACTOR\n    {\n        printRule(\"FACTOR\", \"not FACTOR\");\n    }\n    ;\nN_SIGN  :   //Epsilon\n    {\n        printRule(\"SIGN\", \"epsilon\");\n    }\n        |   T_PLUS\n    {\n        printRule(\"SIGN\", \"+\");\n    }\n        |   T_MINUS\n    {\n        printRule(\"SIGN\", \"-\");\n    }\n    ;\nN_ADD_OP    :   T_PLUS\n    {\n        printRule(\"ADD_OP\", \"+\");\n    }\n            |   T_MINUS\n    {\n        printRule(\"ADD_OP\", \"-\");\n    }\n            |   T_OR\n    {\n        printRule(\"ADD_OP\", \"or\");\n    }\n    ;\nN_MULT_OP   :   T_MULT\n    {\n        printRule(\"MULT_OP\", \"*\");\n    }\n            |   T_DIV\n    {\n        printRule(\"MULT_OP\", \"/\");\n    }\n            |   T_AND\n    {\n        printRule(\"MULT_OP\", \"and\");\n    }\n    ;\nN_REL_OP    :   T_LT\n    {\n        printRule(\"REL_OP\", \"<\");\n    }\n            |   T_GT\n    {\n        printRule(\"REL_OP\", \">\");\n    }\n            |   T_LE\n    {\n        printRule(\"REL_OP\", \"<=\");\n    }\n            |   T_GE\n    {\n        printRule(\"REL_OP\", \">=\");\n    }\n            |   T_EQ\n    {\n        printRule(\"REL_OP\", \"=\");\n    }\n    ;\nN_VAR   :   N_ENTIRE_VAR\n    {\n        printRule(\"VAR\", \"ENTIRE_VAR\");\n    }\n        |   N_IDX_VAR\n    {\n        printRule(\"VAR\", \"IDX_VAR\");\n    }\n    ;\nN_IDX_VAR   :   N_ARRAY_VAR T_LBRACK N_EXPR T_RBRACK\n    {\n        printRule(\"IDX_VAR\", \"ARRAY_VAR ( EXPR )\");\n    }\n    ;\nN_ARRAY_VAR :   N_ENTIRE_VAR\n    {\n        printRule(\"ARRAY_VAR\", \"ENTIRE_VAR\");\n    }\n    ;\nN_ENTIRE_VAR    :   N_VAR_IDENT\n    {\n        printRule(\"ENTIRE_VAR\", \"VAR_IDENT\");\n    }\n    ;\nN_VAR_IDENT :   T_IDENT\n    {\n        printRule(\"VAR_IDENT\", \"IDENT\");\n    }\n    ;\nN_CONST :   N_INTCONST\n    {\n        printRule(\"CONST\", \"INTCONST\");\n    }\n        |   T_CHARCONST\n    {\n        printRule(\"CONST\", \"CHARCONST\");\n    }\n        |   N_BOOLCONST\n    {\n        printRule(\"CONST\", \"BOOLCONST\");\n    }\n    ;\nN_INTCONST  :   N_SIGN T_INTCONST\n    {\n        printRule(\"INTCONST\", \"SIGN intconst\");\n    }\n    ;\nN_BOOLCONST :   T_TRUE\n    {\n        printRule(\"BOOLCONST\", \"true\");\n    }\n            |   T_FALSE\n    {\n        printRule(\"BOOLCONST\", \"false\");\n    }\n    ;\n%%\n\n#include \"lex.yy.c\"\nextern FILE *yyin;\n\nvoid printRule(const char *lhs, const char *rhs) {\n    printf(\"%s -> %s\\n\", lhs, rhs);\n    return;\n}\n\nint yyerror(const char *s) {\n    printf(\"%s\\n\", s);\n    return(1);\n}\n\nvoid printTokenInfo(const char* tokenType, const char* lexeme) {\n    printf(\"TOKEN: %s LEXEME: %s\\n\", tokenType, lexeme);\n}\n\nint main() {\n    do {\n        yyparse();\n    } while (!feof(yyin));\n\n    printf(\"%d lines processed\\n\", numLines);\n\n    return 0;\n}\n```\n\n```text\nN_BLOCK\n```\n\n```text\nN_PROC_DEC\n```\n\n```text\nN_PROC_DEC\n```\n\n```text\nN_BLOCK\n```\n\n```text\nN_BLOCK\n```\n\n```text\nN_PROC_DEC\n```\n\n```text\nN_START\n```\n\n```text\nN_BLOCK\n```\n\n```text\nN_START\n```\n\n========================================\n\nComments:\n- Teaches me for transcribing assignments late at night. Thank you! :) I knew it had to be a recursive error because of all the other answers I had seen to this question. Figured I just needed a fresh set of eyes. Thank you again. :)","metadata":{"transformedAt":"2026-08-18T18:32:17.957Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":10,"totalLines":994,"estimatedTokens":4390}}1167{"id":"stack-41439438","source":"stackoverflow","questionId":41439438,"title":"What other tools can help me create a small language targeting JVM, besides ANTLR?","tags":["compiler-construction","antlr","formal-languages","ll-grammar"],"text":"Title: What other tools can help me create a small language targeting JVM, besides ANTLR?\nTags: compiler-construction, antlr, formal-languages, ll-grammar\nSource: Stack Overflow\n\nQuestion:\n(I started my language adventure with ANTLR several days ago. My knowledge about language theory and compiler construction is *very* limited. Excuse me if this is not a valid question.)\n\nANTLR is a *parser* generator, and specifically, an `ALL(*)` parser. According to here, a parser is:\n\n the part of a compiler that tries to make *syntactic* sense of the\n source code.\n\nAFAIK a compiler should be composed of 5 stages:\n\n- lexical analysis\n\n- syntax analysis\n\n- semantic analysis\n\n- IL representation & optimization\n\n- code generation\n\nSo ANTLR seems to cover only 1 and 2. \n\nSo if I want to write a compiler for an educational-purposed langauge which targets Java byte code on JVM. What other tools can I leverage for stages 3-5?\n\n### ADD 1\n\nAnd why ANTLR just covers as far as 1 and 2? I guess 4 and 5 are skipped because they are too specific to the target platform. But why 3 is skipped by ANTLR?\n\n========================================\n\nTop Answer:\nSince you seem to be honing in on ANTLR I suggest that you purchase a copy of\n\n\"Language Implementation Patterns - Create Your Own Domain-Specific and General Programming Languages\" and\n\n\"The Definitive ANTLR 4 Reference\" by Terrence Parr\n\n What other tools can I leverage for stages 3-5?\n\nTerrence Parr the creator of ANTLR also created String Template which can be used for AST transformations, but there are other tools that fill this niche. See: List of program transformation systems. Note that Ira already noted DMS in his answer.\n\nFor stage 3, semantic analysis, you can use ANTLR. See: ANTLR: How to replace specific nodes in a subtree using a Tree grammar? as an example. Other ways to do semantic analysis are also discussed in the book.\n\nFor stages 4-5 read chapter 10, `Building Bytecode Interpreters`.\n\nFor a beginner it is a good place to start, but it will only get you started. \n\nor \n\nFor stages 4-5 with a quick search I found this which after a quick read makes sense so I will mention it, but no guarantees. In short is uses Javac for phases 4-5. Since the blog is hosted on Oracle, I take it that only the Oracle Javac will work.\n\n A quite interesting approach is to construct AST nodes representing\n the structure of the java code, then generate byte code from that.\n Actually, this is what the javac does.\n\nGenerating java byte code by building AST trees \n\n Why ANTLR just covers as far as 1 and 2?\n\nFrom Tree rewriting in ANTLR v4 \n\nTo quote Terrence Parr \n\n Because most ANTLR users don't build compilers, I decided to focus on\n the other applications for ANTLR v4: parsing and extracting\n information and then translations. \n\nSo \nfor stages 1,2 and 3 you would use ANTLR,\n\nfor AST Transformations if necessary you use String Templates and\n\nfor stages 4 and 5 you can use Javac. \n\nThis is just a start, you have a long journey with lots of research to do. I suggest you take copious amounts of notes along the way.\n\n========================================\n\nCode:\n```text\nALL(*)\n```\n\n```text\nBuilding Bytecode Interpreters\n```\n\n========================================\n\nComments:\n- For code generation you may find useful the asm.ow2.org library\n- This question should be asked on Software Recommendations SE.\n- I tend to agree that your questions are more on the side of subjective than objective and merit the close votes; however being in your position of starting to learn about parsers, compilers, etc., over a decade ago if not here then where else should you go, thus the answers.\n- LOL `It is astonishing how often people think that the key to building a tool to process a computer (or domain-specific) language is to get a parser. Its true... in the same sense that playing poker is all about putting the ante into the pot. If you believe this, the other poker players are going to eat you alive. Yes, you have to ante. No, that's not where the game is played.`\n- `added Binary Decision Diagram support` :)\n- @IraBaxter Just read your paper about code coverage using language transformation system from here: semdesigns.com/Company/Publications/TestCoverage.pdf. It's very infomrative. Maybe I can use ANTLR to build an experimental language transformation system. Thanks. (This comment is not related to this question.)\n- @smwikipedia: Before you try to build a PTS with ANTLR, suggest you read my link to \"Life After Parsing\". I think you seriously underestimate the completeness of ANTLR as a strong foundation for a PTS.\n- @IraBaxter Thanks for the reminding. I will take a look at your link. To be honest, I only have a little experience with ANTLR.\n- Uh, how do you use String Templates for Stage 3 == \"Semantic Analysis\"???\n- Please fix your answer. Many people don't read the comments.","metadata":{"transformedAt":"2026-08-18T18:32:17.958Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":104,"estimatedTokens":1218}}1168{"id":"stack-8002529","source":"stackoverflow","questionId":8002529,"title":"Implementating functional languages for the CLR (Or, papers on the implementation of F#)","tags":["compiler-construction","f#","lambda","functional-programming","clr"],"text":"Title: Implementating functional languages for the CLR (Or, papers on the implementation of F#)\nTags: compiler-construction, f#, lambda, functional-programming, clr\nSource: Stack Overflow\n\nQuestion:\nDoes anyone know of any good papers on the implementation of the F# compiler? I'm trying to generate CIL code for a simple functional language targeting the CLR, but I am struggling with a few aspects.\n\nThe differences between functional languages and CIL are making it hard to generate well-typed CIL code. I have solutions that work via type erasure, but I'd much rather find a way to generate CIL code that reflects (to at least some extend) the Hindley-Milner type system of my source language (e.g., by generating generic classes). Judging by the generated code from the MS and Mono F# compilers, they manage to achieve exactly this!\n\n========================================\n\nComments:\n- I don't know if it goes deep enough for you, but the language spec explains how many functional forms are translated.\n- Maybe the actual F# source code will help? blogs.msdn.com/b/dsyme/archive/2010/11/04/&hellip;\n- For functional languages in general, I would also recommend Simon Peyton-Jones \"Implementing Functional Languages\": research.microsoft.com/en-us/um/people/simonpj/papers/&hellip;\n- OK, so I read Don Syme's paper and I'm sold on the idea of targeting ILX. However, I can't seem to find any resources for downloading/using the ILX assembler. Is it included with VS/Mono? Has it been dropped/is bit rotting?","metadata":{"transformedAt":"2026-08-18T18:32:17.958Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":16,"estimatedTokens":379}}1169{"id":"stack-21786069","source":"stackoverflow","questionId":21786069,"title":"Does a parser or a lexer generate a symbol table?","tags":["parsing","compiler-construction","lexical-analysis"],"text":"Title: Does a parser or a lexer generate a symbol table?\nTags: parsing, compiler-construction, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nI'm taking a compilers course and I'm recapping the introduction. It's a general overview of how the compiler process works.\n\nI'm a bit confused however.\n\nIn my course it states: \"in addition a lexical analyzer will typically access the symbol table to store/get information on certain source language concepts\". So this leads me to believe that a lexer will actually build a symbol table. The way I see it he creates tokens and stores the min a table and states what type of symbol it is. Like \"x -> VARIABLE\", for example.\n\nThen again, when reading through Google hits and I can only seem to find vague information about the fact that the parser generates this? But the parsing phase comes after the lexer phase. So I'm a bit confused.\n\nSymbol Table Population after parsing; Compiler building\n(States that the parser populates the table)\n\nhttp://www.cs.dartmouth.edu/~mckeeman/cs48/mxcom/doc/Symbols.html\nSays \"The symbol table is built by walking the syntax tree.\". The syntax tree is generated by the parser, right? (Parse tree). So how can the lexer, which runs before the parser use this symbol table?\n\nI understand that a lexer can not know the scope of a variable and other information that is contained within a symbol tabe. Therefore I understand that the parser will add this information to the table. However, a lexer does know wether a word is a variable, declaration keyword etc. Thus it should be able to build up a partial (?) symbol table. So could it perhaps be that they each build part of the symbol table?","metadata":{"transformedAt":"2026-08-18T18:32:17.958Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":20,"estimatedTokens":419}}1170{"id":"stack-20085545","source":"stackoverflow","questionId":20085545,"title":"How to provide source-level debugging info when my compiler targets C","tags":["c","compiler-construction","gdb"],"text":"Title: How to provide source-level debugging info when my compiler targets C\nTags: c, compiler-construction, gdb\nSource: Stack Overflow\n\nQuestion:\nI am working on a compiler that translate the source language into C. Then any C compiler could compile that into executable binary. \n\nThough C compiler can attach its own debugging information with the executable, however, those are C-level information. For example, the stack trace displays C functions and line numbers in C, instead of the source code functions and line numbers. \n\nI am pretty much concerned about the following debugging information available to users:\n\n- Stack trace of source-level function/line/file name, etc.\n\n- Value of variables to be printed as source-level value\n\nWhat is the common approach to this when a compiler targets C?\n\nI am looking at GDB's pretty printer. Will that be a possible solution to me?\n\n========================================\n\nTop Answer:\nThe method described by Jonathan in his answer is in fact what the C++ compiler did for the first few years of its life; the translator was called cfront.\n\nFor printing out data types that aren't native C types, customizing stack traces, and displaying info about custom threading implementations, you can go a fair distance by writing python code to extend GDB. This can be effective whether your compiler's target is C or native code. `Go` (which produces native code with its `gc` compiler) provides runtime-gdb.py, for example.\n\n========================================\n\nCode:\n```text\n#line 32 \"sourcefile.ext\"\nC code for line 32 of sourcefile.ext\n#line 33\nC code for line 33 of sourcefile.ext\n```\n\n```text\n#line 62 \"sourcefile.ext\"\nfirst line of C code - will be treated as line 62 of sourcefile.ext\nsecond line of C code - will be treated as line 63 of sourcefile.ext\nthird line of C code - will be treated as line 64 of sourcefile.ext\n#line 63 \"sourcefile.ext\"\nline of C code - will be treated as line 63 of sourcefile.ext again!\n```\n\n```text\n#line 62 \"sourcefile.ext\"\nfirst line of C code - will be treated as line 62 of sourcefile.ext\n#line 62 \"sourcefile.ext\"\nsecond line of C code - will be treated as line 62 of sourcefile.ext too\n#line 62 \"sourcefile.ext\"\nthird line of C code - will be treated as line 62 of sourcefile.ext too\n#line 63 \"sourcefile.ext\"\nline of C code - will be treated as line 63 of sourcefile.ext\n```\n\n```text\n//#line 62 \"sourcefile.ext\"\nfirst line of C code - will be treated as line 91 of sourcefile.c\n//#line 62 \"sourcefile.ext\"\nsecond line of C code - will be treated as line 93 of sourcefile.c\n//#line 62 \"sourcefile.ext\"\nthird line of C code - will be treated as line 95 of sourcefile.c\n//#line 63 \"sourcefile.ext\"\nline of C code - will be treated as line 97 of sourcefile.c\n```\n\n```text\nsourcefile.ext\n```\n\n```text\nsourcefile.c\n```\n\n```text\n#line\n```\n\n```text\nsourcefile.ext\n```\n\n```text\nsourcefile.ext\n```\n\n```text\nsourcefile.c\n```\n\n```text\nGo\n```\n\n```text\ngc\n```\n\n========================================\n\nComments:\n- Thanks a lot. Following this, I found this directive in GCC manual. It works good enough. But is there a similar approach that allows to name C functions as source-level function?\n- No: I am not of aware of a mechanism that will allow you to manipulate the names of functions similarly. You'd have to debug using the modified/transformed names. Usually, these are reasonably systematic (but C++ names take a bit of reading in the encoded format).\n- I actually noticed Go cooperates with GDB when I was doing my research on this topic. Thanks for pointing me to the actual code.","metadata":{"transformedAt":"2026-08-18T18:32:17.958Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":12,"totalLines":105,"estimatedTokens":894}}1171{"id":"stack-15025158","source":"stackoverflow","questionId":15025158,"title":"Why isn't \"if\" an expression in C","tags":["c","if-statement","compiler-construction"],"text":"Title: Why isn't \"if\" an expression in C\nTags: c, if-statement, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nWhat is the reason for 'if' statements in C not to be expressions? Why was the ternary operator added to C instead of 'if' expressions? \n\nAre there technical reasons or is it just a historical decision?\n\nEd.: to make things more clear, I've imagined an 'if' expression like this: if its result is not used, it may behave just like a statement, but if something requires the result, it must have the `else` branch of the same return type. \n\nNow I see that the result is ambiguous: indeed, it's a weird hybrid of expression and statement, so the clear separation is better.\n\n========================================\n\nTop Answer:\nAn if is defined with something like that:\n\n```\n\"if\" \"(\" \")\" \n```\n\nSo it will accept a statement if the condition is met. If it was an expression it would need to return something, hence it would only be able to execute other expressions rather than general statements. In fact the ?: can only execute conditional expressions but not statements.\n\n========================================\n\nCode:\n```text\nelse\n```\n\n```text\nint i = if (foo) { for (int x=4; x; --x) {}; 42 } else bar;\n```\n\n```text\nint foo = frob ? bar : throw \"crap\";\n```\n\n```text\nfoo = frob if bar else 42\n```\n\n```text\n?:\n```\n\n```text\nif/else\n```\n\n```text\n\"if\" <whitespace> \"(\" <whitespace> <expression> <whitespace> \")\" <whitespace> <statement>\n```\n\n========================================\n\nComments:\n- What would an `if` expression evaluate to?\n- aren't if expressions ternary operator in c? i think they are the same thing\n- @Jon I do can write some arithmetical expression in C like a statement, why not?\n- Because if `if` was an expression, each branch would have been able to be evaluated to a value of the same type. Which is a pain. Try to use the terneary expression instead of `if` while you program, and you'll see.\n- @EarlGray: Did you answer my question with that? Try answering it and see a) how difficult it is to come up with an answer that can endure criticism, b) how (not) useful the feature would be, given the restrictions that an ironclad answer will need to impose.\n- Are you in effect asking, \"why did C use the `? :` syntax for conditional expressions, instead of something involving the keyword `if`\"? The straight answer to your question is that `if` is not an expression because `if` is the syntax for conditional statements, whereas `?:` is the syntax for conditional expressions. The two have different advantages and disadvantages, so both are useful.\n- @SteveJessop Rather no. Why C has not been more functional from the start? Was it ignorance or inertia? Does the statements model fits its tasks better, than why? I think there was some way of embedding expressional 'if' into the language, if not, why?\n- @EarlGray what's the difference between expressional if and ternary operator, other than the symbols (`if` and `?:`)?\n- @EarlGray: So what's the specific difference between the conditional expressions C actually has, compared with the conditional expressions that you're saying it could have? Without knowing what you're proposing I can't venture an opinion why it wasn't the natural thing for C to have done. If the debate broadens to \"why does C have statements at all\", or \"why is it an imperative language\" then I think you lose suitable focus for SO.\n- @SteveJessop Yes, it looks like this question boils down to \"why statements are not expressions and why they're useful in C\", which is obvious for me. But still, why 'if' does not return anything (even `void`)?\n- i still don't get what's the essential difference between `int x = a < b ? 3 : 5`; and let's say if returned a value, with a hypothethical syntax `int x = if (a < b) return 3 else return 5;` @EarlGray can you tell me what's the difference?\n- @EarlGray: well, statements in general don't return anything because the grammar doesn't let you use them in contexts that require a value (or non-value in the case of `void`). The reason for that, is to reduce the \"burden\" on the language design to always supply a value. If the specific problem you face is that you're trying to write something in part 3 of a `for` loop that can't be written as an expression then we've all been there, but you just have to suck it up and write another function ;-)\n- @AliVeli Just that: why this syntax is \"hypothetical\", not an actual one.\n- @EarlGray: That part of the question *is* what I asked, isn't it, \"why doesn't the ternary operator syntax involve the keyword `if`\"? To which I speculate (a) that it was introduced before there were *any* keyword operators in C (`sizeof` initially being a macro), and/or (b) because Ritchie or someone else thought that it would appear ambiguous even if it wasn't formally so. The multiple and confusing meanings of `static` also came later ;-)\n- @SteveJessop This sounds the most convincing of the answers. That's the C, not a functional language and I have to put up with the fact that some things must be statements. I've just tried to dig into historical roots, to find a possible logical reason.\n- voids are still valid expressions. The problem comes when you want to define a loop in it.\n- Yes, it's pretty possible to return void if nobody wants the result. And even more, it's possible to cast both results (of different types, maybe) to `void` if it is not used.\n- @LtWorf: I revised my answer and removed the void-argument after thinking about it. You can even return *void* in C++-templates.\n- Sure you can, void is a type.\n- A nice point about modeling machines. \"Mandatory\" `else` can be eliminated: again, if nobody uses the result, it may be optional, so the performance argument does not work.\n- @LtWorf: ... depends on the context. In any case, you can't `return XXX`, with `XXX` being of type `void`, in non-template code in C++ (e.g. `void foo() {} void bar() { return foo(); }` is not valuid C++.\n- @EarlGray: Seems I overlooked your comment previously, sorry for that. Good point again, and thinking about it leads me to another revision of my answer.\n- Thanks, the last edit is what I've asked about.\n- You don't need to mention whitespace everywhere. The lexer would detect and throw away the whitespace (comments included) before passing the tokens over to the parser. So what the parser sees actually (or in other words, the grammar it parses) has no whitespace.\n- It's a pseudo-grammar example...\n- Even more the reason for not writing the whitespace. All I'm saying is that it clutters what you are trying to say with unnecessary information.","metadata":{"transformedAt":"2026-08-18T18:32:17.958Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":84,"estimatedTokens":1654}}1172{"id":"stack-17109262","source":"stackoverflow","questionId":17109262,"title":"F# printf compiler magic","tags":[".net","compiler-construction","f#"],"text":"Title: F# printf compiler magic\nTags: .net, compiler-construction, f#\nSource: Stack Overflow\n\nQuestion:\nIn F#, the compiler is clearly doing some magic to make this work:\n\n```\nprintfn \"%i %i\" 6 7 ;; // good\nprintfn \"%i %i\" 6 7 8;; // error\n```\n\nHow is it doing this? and is there any way to achieve a similar behavior, from within the language?\n\n========================================\n\nTop Answer:\nThe magic is in the implicit conversion from a string literal to the type `PrintfFormat`. For instance, `printf` takes an argument of type `TextWriterFormat`, which is actually just an alias for `PrintfFormat`.\n\nThe magic for this implicit conversion can't be easily emulated within the language, but there's nothing special about the `printf` family of functions - you can write your own functions that take arguments of type `PrintfFormat` and use them with string literals without any problem.\n\nWhile there's no way to extend the implicit conversion from strings, one alternative would be to use type providers. It would be quite easy to write a type provider so that \n\n```\nPrintfTypeProvider.Apply\n```\n\nreturns a value of type `int -> int -> string`, for instance, but you could also extend the logic in fairly arbitrary ways if you wanted.\n\n========================================\n\nCode:\n```text\nprintfn \"%i %i\" 6 7 ;; // good\nprintfn \"%i %i\" 6 7 8;; // error\n```\n\n```text\nand TcConstStringExpr cenv overallTy env m tpenv s  =\n\n    if (AddCxTypeEqualsTypeUndoIfFailed env.DisplayEnv cenv.css m overallTy cenv.g.string_ty) then \n      mkString cenv.g m s,tpenv\n    else \n      let aty = NewInferenceType ()\n      let bty = NewInferenceType ()\n      let cty = NewInferenceType ()\n      let dty = NewInferenceType ()\n      let ety = NewInferenceType ()\n      let ty' = mkPrintfFormatTy cenv.g aty bty cty dty ety\n      if (not (isObjTy cenv.g overallTy) && AddCxTypeMustSubsumeTypeUndoIfFailed env.DisplayEnv cenv.css m overallTy ty') then \n        // Parse the format string to work out the phantom types \n        let aty',ety' = (try Formats.ParseFormatString m cenv.g s bty cty dty with Failure s -> error (Error(FSComp.SR.tcUnableToParseFormatString(s),m)))\n        UnifyTypes cenv env m aty aty';\n        UnifyTypes cenv env m ety ety';\n        mkCallNewFormat cenv.g m aty bty cty dty ety (mkString cenv.g m s),tpenv\n      else \n        UnifyTypes cenv env m overallTy cenv.g.string_ty;\n        mkString cenv.g m s,tpenv\n```\n\n```text\nand TcConstStringExpr cenv overallTy env m tpenv s  =\n\n    if (AddCxTypeEqualsTypeUndoIfFailed env.DisplayEnv cenv.css m overallTy cenv.g.string_ty) then \n      mkString cenv.g m s,tpenv\n    elif (AddCxTypeEqualsTypeUndoIfFailed env.DisplayEnv cenv.css m overallTy cenv.g.int_ty) then \n      mkInt cenv.g m (System.Int32.Parse s),tpenv\n    elif (AddCxTypeEqualsTypeUndoIfFailed env.DisplayEnv cenv.css m overallTy cenv.g.int32_ty) then \n      mkInt32 cenv.g m (System.Int32.Parse s),tpenv\n    else \n      let aty = NewInferenceType ()\n      let bty = NewInferenceType ()\n      let cty = NewInferenceType ()\n      let dty = NewInferenceType ()\n      let ety = NewInferenceType ()\n      let ty' = mkPrintfFormatTy cenv.g aty bty cty dty ety\n      if (not (isObjTy cenv.g overallTy) && AddCxTypeMustSubsumeTypeUndoIfFailed env.DisplayEnv cenv.css m overallTy ty') then \n        // Parse the format string to work out the phantom types \n        let aty',ety' = (try Formats.ParseFormatString m cenv.g s bty cty dty with Failure s -> error (Error(FSComp.SR.tcUnableToParseFormatString(s),m)))\n        UnifyTypes cenv env m aty aty';\n        UnifyTypes cenv env m ety ety';\n        mkCallNewFormat cenv.g m aty bty cty dty ety (mkString cenv.g m s),tpenv\n      else \n        UnifyTypes cenv env m overallTy cenv.g.string_ty;\n        mkString cenv.g m s,tpenv\n```\n\n```text\nprintfn \"%i %i\" (\"4\" + 2) \"5\"\n```\n\n```text\n6 5\n```\n\n```text\nmkInt\n```\n\n```text\nmkInt32\n```\n\n```text\nPrintfTypeProvider<\"%i %i\">.Apply\n```\n\n```text\nPrintfFormat<_,_,_,_>\n```\n\n```text\nprintf\n```\n\n```text\nTextWriterFormat<'a>\n```\n\n```text\nPrintfFormat<'a,System.IO.TextWriter,unit,unit>\n```\n\n```text\nprintf\n```\n\n```text\nPrintfFormat<_,_,_,_>\n```\n\n```text\nint -> int -> string\n```\n\n```text\nsprintf\n```\n\n========================================\n\nComments:\n- could you give a link to the source?\n- @AK_ The original source can be found here: github.com/fsharp/fsharp/blob/master/src/fsharp/tc.fs#L6116\n- @AK_ stackoverflow.com/questions/88302/macro-support-in-f . Not really, but if we're already opening the F# compiler: ramon.org.il/wp/2011/12/text-based-macro-system-for-f","metadata":{"transformedAt":"2026-08-18T18:32:17.958Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":16,"totalLines":145,"estimatedTokens":1145}}1173{"id":"stack-5629397","source":"stackoverflow","questionId":5629397,"title":"Finding similar code sections using sub-trees","tags":["algorithm","parsing","language-agnostic","compiler-construction","abstract-syntax-tree"],"text":"Title: Finding similar code sections using sub-trees\nTags: algorithm, parsing, language-agnostic, compiler-construction, abstract-syntax-tree\nSource: Stack Overflow\n\nQuestion:\nI have been reading this paper titled Clone Detection using Abstract Syntax Trees by Ira D. Baxter et al. There is a paragraph from the paper that I reproduced below:\n\n In principle, finding sub-tree clones\n is easy: compare every subtree to\n every other sub-tree for equality. In\n practice, several problems arise:\n near-miss clone detection, sub-clones\n and scale. \n ... \n\n \n When locating near-miss\n clones, hashing on complete subtrees\n fails precisely because good hashing\n functions include all elements of the\n tree, and thus sorts tress with minor\n differences into different buckets. We\n solved this problem by choosing an\n **artificially bad hash function**. This function must be characterized in\n such a way that the main properties\n one wants to find on near-miss clones\n are preserved. Near miss clones are\n usually created by copy and paste\n procedures followed by small\n modifications. These modifications\n usually generate small changes to the\n shape of the tree associated with the\n copied piece of code. Therefore, we\n argue that this kind of near-miss\n clone often have only some different\n small sub-trees. Based on this\n observation, a hash function that\n ignores small sub-trees is a\n goodchoice. In the experiment\n presented here, we used a hash\n function that ignores only the\n identifier names (leaves in the tree).\n Thus our hashing function puts trees\n which are similar modulo identifiers\n into the same hash bins for\n comparison.\n\nI am trying to implement the techniques discussed in this paper but am stuck in trying to understand this one paragraph (that is unfortunately at the beginning of the paper). I understand what the paragraph is saying but the authors do not mention what hash function to choose or how to actually hash the ASTs. Can someone please explain this with a simple example from an implementation standpoint?\n\n========================================\n\nTop Answer:\nIf you know that two ASTs are \"clones\" to your human eye you want to make sure they have the same hash value also.\n\nFor example, hash every identifier to a constant and every string to another constant to avoid getting tricked by variable renaming, instead of actually using identifier name as material part of hashing.\n\nOr use commutative hashing for expression that are commutative, I.e. make sure a+b and b+a get the same hash value.\n\nExample for arithmetic expressions involving variables, integers, operators and parenthesis:\n\n```\nhash VariableName = 0x12345678\n hash IntegerConstant = 0xff77ff77\n hash x + y = (hash x) + (hash y)\n hash (x) = (hash x) Etc.\n\n========================================\n\nCode:\n```text\nhash VariableName = 0x12345678\n hash IntegerConstant = 0xff77ff77\n hash x + y = (hash x) + (hash y)\n hash (x) = (hash x) <<< 13\n hash x * y = (hash x) xor (hash y)\n```\n\n========================================\n\nComments:\n- Either the SO search engine is extremely good or I am extremely lucky to get a reply from the author. Either ways it is a win-win for me :) Your explanation clarifies my question. As a final request, would you recommend any -up works to your paper after I finish reading it? (Any state-of-the-art work in this area that you feel I should be familiar with before putting on the developer hat). By the way, a really great paper!\n- @Legend: A good index of the clone detection literation can be found at students.cis.uab.edu/tairasr/clones/literature Rather a lot has been written since my paper was published. Most of the argument is a) how do you detect clones, and b) how do you make it scale well. Elmar Juergen's papers are good on scaling, but he scales well by giving up parameterization. I'm pleased to say there are other tree-detection papers, but pretty much CloneDR has really stood the test of time. The commercial version doesn't quite work like the paper but details won't be published.\n- @Legend: If you want to hear state of the art, you need to attend the Software Clones workshop at the International Conference on Software Engineering in Hawaii in May. Besides, its a great boondoggle of a trip :-} See you there!","metadata":{"transformedAt":"2026-08-18T18:32:17.958Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":83,"estimatedTokens":1066}}1174{"id":"stack-9413893","source":"stackoverflow","questionId":9413893,"title":"Lexical analyser and parser communication","tags":["parsing","compiler-construction","lexical-analysis"],"text":"Title: Lexical analyser and parser communication\nTags: parsing, compiler-construction, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nMost of the resources on lexical analyzers and parsers illustrate use of streams to communicate between them (or so I understand).\n\nIt is explained that the parser asks for the next token, say by calling a function `getNextToken()`, and the lexer responds to it by returning the next token. Are we supposed to think of them as two objects interacting within the same program or two different programs interacting through streams?\n\nAlso, I haven't been able to understand why a serial approach isn't chosen, i.e. the lexical analyzer runs till the end of the provided source, and only then does the parser use the output of the lexical analyzer for parsing. To be precise, if the lexical analyzer reads the next lexeme only when the parser asks for the next token, how is an error handled? Especially if the error occurs towards the end of the file, all the computation done by the parser might be wasted due to the error (assuming a very basic parser without any error handling capabilities). Is recent output cached?\n\n========================================\n\nTop Answer:\nMost of the resources on lexical analyzers and parsers illustrate use\n of streams to communicate between them (or so I understand).\n\nNone of the ones I've seen do that. They rely on the lexical analyser being a single method that is called by the parser.\n\n========================================\n\nCode:\n```text\ngetNextToken()\n```\n\n```text\n.               { return -1; }\n```\n\n```text\nyylex()\n```\n\n```text\nyylex()\n```\n\n```text\ngetNextToken()\n```\n\n```text\n,\n```\n\n```text\n=\n```\n\n```text\nyylex()\n```\n\n```text\n\\n\n```\n\n```text\nyyerror(char *)\n```\n\n```text\nyyerror(char *)\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.958Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":11,"totalLines":65,"estimatedTokens":445}}1175{"id":"stack-1101267","source":"stackoverflow","questionId":1101267,"title":"Where does the compiler spend most of its time during parsing?","tags":["language-agnostic","parsing","compiler-construction","lexical-analysis"],"text":"Title: Where does the compiler spend most of its time during parsing?\nTags: language-agnostic, parsing, compiler-construction, lexical-analysis\nSource: Stack Overflow\n\nQuestion:\nI read in Sebesta book, that the compiler spends most of its time in lexing source code. So, optimizing the lexer is a necessity, unlike the syntax analyzer.\n\nIf this is true, why lexical analysis stage takes so much time compared to syntax analysis in general ?\n\nI mean by syntax analysis the the derivation process.\n\n========================================\n\nTop Answer:\nLexical analysis is the process whereby all the characters in the source code are converted to tokens. For instance\n\n```\nforeach (x in o)\n```\n\nis read character by character - \"f\", \"o\", etc.\n\nThe lexical analyser must determine the keywords being seen (\"foreach\", not \"for\" and so on.)\n\nBy the time syntactic analysis occurs the program code is \"just\" a series of tokens. That said, I agree with the answer above that lexical analysis is not necessarily the most time-consuming process, just that it has the biggest stream to work with.\n\n========================================\n\nCode:\n```text\nforeach (x in o)\n```\n\n========================================\n\nComments:\n- Why wouldn't you optimize both?\n- I'm not at all sure, but maybe it's relevent too that there's also a greater variety of character values that there is of lexed token types (e.g. 2^16 possible character values compared with e.g. only 100 token types).\n- Furthermore, given any particular token, there's quite a small number of token types (perhaps even only one token type) which can legally this type of token. I.e., the switch statement for each token in the syntactic analyzer has far fewer cases in it than the switch statements for each character in the lexer.","metadata":{"transformedAt":"2026-08-18T18:32:17.958Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":39,"estimatedTokens":447}}1176{"id":"stack-1999608","source":"stackoverflow","questionId":1999608,"title":"Assembly versus binary output","tags":["compiler-construction","assembly","code-generation"],"text":"Title: Assembly versus binary output\nTags: compiler-construction, assembly, code-generation\nSource: Stack Overflow\n\nQuestion:\nIs it better for a compiler to compile code to assembly language, or output binary machine code directly?\n\nAdvantages of assembly language that I can think of off the top of my head: avoiding the need to learn the object file format, ease of debugging the backend.\n\nAdvantage of binary: faster compile speed. How significant is this? Assuming the Gnu assembler is used (apart from anything else, it's what can reasonably be assumed to be available on most machines), does it take a significant amount of time to assemble, say, a million lines of code?\n\nAre there any differences in object file formats between various operating systems that the assembler would hide?\n\nAre there any other advantages on either side that I haven't thought of?\n\n========================================\n\nTop Answer:\nAnother advantage of assembly: Ability to use labels for jumps, loops, branches and function calls so you don't need to manually calculate memory addresses.\n\n========================================\n\nCode:\n```text\n[~/ecc/ellcc/ecc/Main] main% ../../bin/x86-elf-ecc test/sieve.c -time-actions\n===-------------------------------------------------------------------------===\n                   ... Ellcc action timing report ...\n===-------------------------------------------------------------------------===\nTotal Execution Time: 2.9006 seconds (2.9857 wall clock)\n\n---User Time---   --System Time--   --User+System--   ---Wall Time---  --- Name ---\n2.0397 ( 71.3%)   0.0250 ( 65.8%)   2.0647 ( 71.2%)   2.1174 ( 70.9%)  Bitcode linking\n0.7999 ( 27.9%)   0.0070 ( 18.4%)   0.8069 ( 27.8%)   0.8111 ( 27.2%)  Generating\n0.0000 (  0.0%)   0.0010 (  2.6%)   0.0010 (  0.0%)   0.0274 (  0.9%)  Assembly\n0.0110 (  0.4%)   0.0030 (  7.9%)   0.0140 (  0.5%)   0.0143 (  0.5%)  LLVM generation\n0.0070 (  0.2%)   0.0000 (  0.0%)   0.0070 (  0.2%)   0.0066 (  0.2%)  Type checking\n0.0000 (  0.0%)   0.0020 (  5.3%)   0.0020 (  0.1%)   0.0041 (  0.1%)  Linking\n0.0030 (  0.1%)   0.0000 (  0.0%)   0.0030 (  0.1%)   0.0031 (  0.1%)  Optimization\n0.0010 (  0.0%)   0.0000 (  0.0%)   0.0010 (  0.0%)   0.0010 (  0.0%)  Elaboration\n0.0010 (  0.0%)   0.0000 (  0.0%)   0.0010 (  0.0%)   0.0004 (  0.0%)  Integrity checking\n0.0000 (  0.0%)   0.0000 (  0.0%)   0.0000 (  0.0%)   0.0004 (  0.0%)  Parsing\n2.8626 (100.0%)   0.0380 (100.0%)   2.9006 (100.0%)   2.9857 (100.0%)  TOTAL\n\n[~/ecc/ellcc/ecc/Main] main%\n```\n\n```text\ngcc -O2 -S -c foo.c\n```\n\n========================================\n\nComments:\n- The GNU assembler works in one pass. It is linear in the size of the generated binary. Unless your compiler is very basic and does no difficult optimization at all, the time taken by the GNU assembler can be neglected in the total compilation time.","metadata":{"transformedAt":"2026-08-18T18:32:17.958Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":54,"estimatedTokens":714}}1177{"id":"stack-839564","source":"stackoverflow","questionId":839564,"title":"Stratego/XT Programming Language","tags":["compiler-construction","programming-languages"],"text":"Title: Stratego/XT Programming Language\nTags: compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nDoes anyone know something about Stratego/XT Programming Language? What is it good for? Any good and/or bad experiences with it? Any real live running example?\n\nThanks a lot.\n\n========================================\n\nTop Answer:\nTry TXL instead. It is easier to start with and does the similar jobs. There are examples on its site.","metadata":{"transformedAt":"2026-08-18T18:32:17.958Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":13,"estimatedTokens":115}}1178{"id":"stack-23639211","source":"stackoverflow","questionId":23639211,"title":"Writing a compiler - bound-checked arrays with integer expression bounds (best practice)","tags":["c++","arrays","compiler-construction","compiler-warnings"],"text":"Title: Writing a compiler - bound-checked arrays with integer expression bounds (best practice)\nTags: c++, arrays, compiler-construction, compiler-warnings\nSource: Stack Overflow\n\nQuestion:\nI am hand-coding a compiler in C++ (C++98), and am looking for feedback/ideas how the following case should be handled (see title for what the grammar is supposed to be like for arrays):\n\n```\n{\n int b;\n int m, n;\n int a[n];\n\n b = a[b*m]; (1)\n}\n```\n\nBound-checking, run-time stack extension (and shrinking when leaving scope), etc, can be handled well in my framework. \n\nMy question relates to emitting warnings in the case of using uninitialized variables, as in (1) above. If **a** is an integer variable that is used before it is initialized, I allow it, but emit a warning message. As I want to make the compiler be ready to possibly have a linker later, calculating b and m backward and actually checking if this index of a has been initialized cannot be done (eg, in the most general case, a variable could be defined in a different file). As said, I know how to emit code to do the bound-checks at run-time; but... \n\n...**what is a best-practice way to extend emitting a warning** when a variable is used uninitialized to the case of this variable being of the form a[(expr)]? As (expr) doesn't have to be an integer value (a mere number; it only needs to be of integer type), without evaluating (expr) (which I don't want to do as said just above), I cannot keep, say, a shadow array with entries marked initialized. In (gcc) C the case is simply ignored: a warning that both b and m are uninitialized is emitted, but none relating to using an uninitialised variable a[b*m]. This is obviously in line with C's view of arrays, and in C (but not the language I am working on, which has **no concept of pointers**) the expression is well-defined, other than b and m not being initialised, and the access being (probably) out of bound (an imminent stack overflow).\n\nIs it best practice to simply not check if a[(expr)] has been initialized before usage; emit code; and wait for a run-time error, if any? Or...?\n\n========================================\n\nTop Answer:\nI think trying to initialize-check everything leads down the road to pain.\n\nConsider what happens when elements of `a[b*m]` are *conditionally* initialized, i.e. the elements of `a` of which are initialized depends on the input arguments. You'd have to track not only an initialized *bit* in your \"shadow array copy\", but an entire conditional execution graph to make sure all execution paths are covered. And ultimately that's an undecidable problem on a Turing machine, even; to solve this you'd have to decide the halting problem (to tell if any particular subgraph of your execution graph even finishes executing).\n\nYou could do some heuristics to warn only over some subset of uninitialized cases, but that would just mean your compiler emits warnings sometimes and not other times. As a programmer you should know how infuriating seemingly undeterministic behavior like that is.\n\n========================================\n\nCode:\n```text\n{\n    int b;\n    int m, n;\n    int a[n];\n\n    b = a[b*m]; (1)\n}\n```\n\n```text\nvoid foo(int &x, int y)\n{\n   switch(y)\n   {\n      case 1:\n         x = 11;\n         break;\n      case 2:\n         x = 42;\n         break;\n      ...  // numbers 3-9 elided for brevity\n      case 10:\n         x = 97;\n         break;\n   }\n}\n\nint bar(int z)\n{\n    int a;\n    foo(a, z);\n}\n```\n\n```text\na\n```\n\n```text\nz\n```\n\n```text\nz\n```\n\n```text\nz\n```\n\n```text\na[b*m]\n```\n\n```text\na\n```\n\n========================================\n\nComments:\n- There is no C++99. C99?\n- @deviantfan: :) The one before C++11 (my gcc has spotty support for C++11 - I'm running and not updating an about Ubuntu 12.04 time version of gcc)\n- The one before C++11, excluding the TR, would be C++03. But: C++ has no variable length arrays (C99 has. (It&#180;s not forbidden to add them in C++ tough)). ... You should know for what language you&#180;re making a compiler ...\n- @deviantfan: (1) The target language is similar to Java; (2) the development language is C++; (3) all references to C (or any language other than the above two) are merely examples.\n- And of course the downvoter won't explain what he doesn't like about my answer.","metadata":{"transformedAt":"2026-08-18T18:32:17.958Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":8,"totalLines":104,"estimatedTokens":1072}}1179{"id":"stack-8866032","source":"stackoverflow","questionId":8866032,"title":"Flattening a control flow graph to structured code","tags":["compiler-construction","compilation","theory","control-flow","control-flow-graph"],"text":"Title: Flattening a control flow graph to structured code\nTags: compiler-construction, compilation, theory, control-flow, control-flow-graph\nSource: Stack Overflow\n\nQuestion:\nI would like to render a control flow graph (CFG) out to high-level code. Normally this is very easy; walk the tree, render each basic block in turn, glue it all together with gotos.\n\nUnfortunately, gotos are out of fashion these days, and most modern languages don't support them. So I need some way to glue my basic blocks together using only those control flow statements that exist in the language: `for`, `while`, `do`...`while`, `if`, `break` and `continue`. (I'm not willing to consider building a state machine using variables.)\n\nIt would appear that while there are algorithms to do this, they will *not* work in every case. That is, it's possible to construct a CFG that cannot be flattened to structured code using only the above limited set of control flow structures.\n\nThis seems intuitively obvious to me, but I can't prove it (and the documentation for the algorithms I've found don't go into more detail). And I haven't been able to find an example of a CFG which can't be flattened like this.\n\nI would like to know, definitively, if this is possible or not.\n\nOption (a): does anyone have a example of a CFG which cannot be flattened as described above? (Which will tell me that it's not possible.)\n\nOption (b): does anyone have a proof that CFGs *can* be flattened as described above? (Which will tell me that it *is* possible.) An algorithm to do it would be highly desirable, too, as I would then have to make it work...\n\n========================================\n\nTop Answer:\nalthough this question was asked a long time ago this actually seems to be possible. Mozilla had a similar problem when compiling LLVM to JS (or now WebAssembly). JS and WebAssembly only allow structured control flow, while LLVM allows arbitrary control flow.\n\nThey'v written a paper about this which is also used for WebAssembly:\n\n This idea is modeled on the Relooper algorithm from 2011. There is a proof there that any control flow can be represented in a structured way, using just the available control flow constructs in JavaScript, and using a helper variable like label mentioned in the Tilt semantics, without any code duplication (other approaches split nodes, and have bad worst-case code size situations). The relooper has also been implemented in Emscripten, and over the last 4 years we have gotten a lot of practical experience with it, showing that it gives good results in practice, typically with little usage of the helper variable.\n\n========================================\n\nCode:\n```text\nfor\n```\n\n```text\nwhile\n```\n\n```text\ndo\n```\n\n```text\nwhile\n```\n\n```text\nif\n```\n\n```text\nbreak\n```\n\n```text\ncontinue\n```\n\n```text\ny\n```\n\n```text\nz\n```\n\n========================================\n\nComments:\n- Why not build a state machine using variables? Just because you didn't mention it... are you aware of the structured programming theorem?\n- State machines using variables are dead slow. That's what I'm looking at right now, but some simple benchmarks show that I'm wasting about 30% of the CPU time just shuffling state. Plus, I already know how to do that, so don't need to ask about it here...\n- The computer scientist who wrote the letter suggesting that the use of **goto** be limited was Edsger Dijkstra. Donald Knuth was and afaik is still a defender of **goto** although he, too, believes it should be used with some restraint. The difference between their views was never as dramatic as it is often portrayed, and the debate is not helped by Manichean exaggerations, which are tragically prevalent in modern discourse in general.\n- I've looked at Relooper. It uses a bunch of heuristics to try to reconstruct what it can, but it's pretty easy to confuse, at which point it falls back to constructing a state machine. It's strictly a pragmatic, good-enough-for-government work solution, rather than a real solution to the fundamental problem.\n- The above graph, while irreducible, can be implemented with structured control flow with the addition of a helper variable, say \"jump\". We add a block w above x which sets jump=false and goes to x. Then we create a new block v that sets jump=true, and direct the right edge coming out of x to v instead of to z. Then we direct v, x, and z to a new block u, with a condition if jump=true, y else z. Then the first statement of z added to set jump = false. This adds a minimum of code with no duplication and turns this structure into a loop with a single entry.\n- Adding state produces a different CFG, meaning it's out of scope for me. (Also, from the practical point of view, adding state nerfs the ability of the compiler to reason about the program, resulting in much worse code generation.)\n- In that case it is impossible. Note that this situation cannot be created by structured elements either. It takes a go to. It's rare in the real world, because you need something like if x, go to y else z, y, go to z.","metadata":{"transformedAt":"2026-08-18T18:32:17.958Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":9,"totalLines":77,"estimatedTokens":1263}}1180{"id":"stack-21544859","source":"stackoverflow","questionId":21544859,"title":"Theory of interpreters, partial evaluators, and compilers","tags":["compiler-construction","interpreter","computation-theory","compiler-theory","stack-machine"],"text":"Title: Theory of interpreters, partial evaluators, and compilers\nTags: compiler-construction, interpreter, computation-theory, compiler-theory, stack-machine\nSource: Stack Overflow\n\nQuestion:\nSo I've been learning about stack machines, interpreters, compilers and a few other things related to programming languages and their general theory. Most of the stuff I find in books and online are very specialized and talk about one specific topic, e.g. interpreters, with no mention of how it relates to other topics, e.g. partial evaluators. Are there any good beginner resources for exploring the interconnections between interpreters, compilers, and partial evaluators? By good resources I mean something that explains the theory along with concrete implementations. The more I learn about this stuff the more places I see in my day to day work of how all of it can be applied but the lack of beginner friendly resources is a bit of bummer.","metadata":{"transformedAt":"2026-08-18T18:32:17.958Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":6,"estimatedTokens":235}}1181{"id":"stack-620358","source":"stackoverflow","questionId":620358,"title":"Do interpreters actually pre compile in memory?","tags":["compiler-construction","theory","interpreter"],"text":"Title: Do interpreters actually pre compile in memory?\nTags: compiler-construction, theory, interpreter\nSource: Stack Overflow\n\nQuestion:\nOk guys I thought I'd take my old CS notes and look through compiler theory a little more. I have to say I can't for the life in me remember how all this stuff works but I do have a nice sample application from my college days that helps me understand a few things. \n\nThis sample application takes the made up language and compiles it down to an intermediate assembly code like language. There is then a simple VM implementation that takes this intermediate language and executes the statements. \n\nThe thing I cant get my head around is, if I this were a straight up interpretor and not a compiler would it still be building up these intermeditary commands in memory to be executed at the end. Or does an interpretor actually \"execute\" descreet sections of the code chunks at a time?\n\n========================================\n\nTop Answer:\nIt depends on the language. Most modern interpreted languages (Perl, Python, and Ruby, to name a few) precompile the source code into some intermediary form to be executed at the end (citation).\n\n========================================\n\nCode:\n```text\n.pyc\n```\n\n========================================\n\nComments:\n- Is there an alternative? My goal is to write a very, VERY simple language parser and the thought of having to compile do to IL seems overkill somehow.\n- The problem is that you need *something* to execute your parsed commands. This is what perl, python, and java do for you. The only alternatives I know are to compile to machine code or target an existing \"interpreter\" (like the JVM).\n- Not sure I you. What I mean is that the likes of the Java VM interpritate Javas IL. But if your core language is already very simple then why would an IL even be needed?\n- My fault. There's no absolute need for the IL. You could, for example, parse and execute your source one line at a time if you wanted. I wrote a simple calculator back in college that worked like this.\n- You may not want to parse and execute at the same time. This can cause problems if you call a function that hasn't yet been defined, but is defined later on in a file. It might be best to translate it into some intermediate form, even internally, just so you get the full symbol table. Or something.\n- @Chris: Right. That all depends on the syntax rules of the language, though. In many older languages that would just be a syntax error.\n- Thanks for your help, do you know of any simple C#/Java interpretor that I could take a look at?\n- You have chosen some very complex languages. Start with something simple. May I suggest a stack based language like PostScript or Factor?\n- At the very least, a desk calculator.\n- Lol, no I did not mean an interpretor of C#/Java. I meant one written in these language. Yes, I would imagine that this is a little above me right now :-).\n- I don't use either of these much. But there are a host of others around to help you along. This looks a good start -- codeproject.com/KB/cs/csi.aspx\n- Thanks for the help, also found this one that looks interesting: codeproject.com/KB/cs/csi.aspx\n- If you want to find simple interpreters, you should try brainf*ck, but that may be too simple for practical learning applications. Look into lolcode (lolcode.com). It's a silly joke language, but there's a .NET compiler and a number of interpreters, and it has fairly complicated syntax.\n- Might I add that Parrot seems to be kind of popular and new and shiny, and is also a register-based VM? A good idea for a VM to target maybe.","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":40,"estimatedTokens":904}}1182{"id":"stack-43712554","source":"stackoverflow","questionId":43712554,"title":"Global variables in Single Static Assignment Form","tags":["compiler-construction","global-variables","ssa"],"text":"Title: Global variables in Single Static Assignment Form\nTags: compiler-construction, global-variables, ssa\nSource: Stack Overflow\n\nQuestion:\nI am working on a compiler that uses SSA for a language which contains global variables. I am wondering how i should implement uses and definitions of global variables, for example how should I convert the below code?\n\nNon SSA form:\n\n```\nx;\ny;\n\nmain () {\n x = 0;\n foo();\n y = x;\n x = 2;\n foo();\n}\n\nfoo () {\n print x;\n x = 1;\n}\n```\n\nIn SSA form there are some places where I am unsure of the subscripts to use:\n\n```\nmain () {\n x.0 = 0;\n foo()\n y.0 = x.?\n x.1 = 2;\n foo();\n}\n\nfoo () {\n print x.?;\n x.? = 1;\n}\n```\n\nI have thought about adding in phi-functions but this doesn't seem to solve the problem of what subscripts those phi functions are referring to.\n\nMany thanks,\nBen\n\n========================================\n\nCode:\n```text\nx;\ny;\n\nmain () {\n  x = 0;\n  foo();\n  y = x;\n  x = 2;\n  foo();\n}\n\nfoo () {\n  print x;\n  x = 1;\n}\n```\n\n```text\nmain () {\n  x.0 = 0;\n  foo()\n  y.0 = x.?\n  x.1 = 2;\n  foo();\n}\n\nfoo () {\n  print x.?;\n  x.? = 1;\n}\n```\n\n========================================\n\nComments:\n- This being the case does it mean that when performing data flow analysis and optimisation we ignore global variables as we might arrays?\n- @BenJacob Yes, at least for analyses based solely on SSA variables. You can still used non-SSA algorithms that reason about mutable storage locations (e.g. for two loads from the same address can be considered equivalent if there are no intervening writes between the two locations). As I said, there are also extensions of SSA to memory locations, but LLVM at least doesn't implement them (at the IR level; it has a \"memory SSA\" utility nowadays).","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":2,"totalLines":90,"estimatedTokens":432}}1183{"id":"stack-41427905","source":"stackoverflow","questionId":41427905,"title":"How many ways are there to build a parser?","tags":["parsing","compiler-construction","antlr","ll-grammar","lr-grammar"],"text":"Title: How many ways are there to build a parser?\nTags: parsing, compiler-construction, antlr, ll-grammar, lr-grammar\nSource: Stack Overflow\n\nQuestion:\nI am learning about the ANTLR v4, which is a parser generator based on so-called Adaptive LL(*) algorithm. It claims to be a big improvement over LL(*) algorithm, but I also heard about some algorithm like LR.\n\nWhat's the advantage/limitation of ANTLR's Adaptive LL(*) algorithm (over LR)?\n\n========================================\n\nCode:\n```text\nALL(*)  \nBacktracking Bottom-up  \nBacktracking LALR(1)  \nBacktracking LALR(k)  \nGLR  \nLALR(1)  \nLR(1)  \nIELR(1)  \nLALR(K)\nLR(K)  \nLL  \nLL(1)\nLL(*)  \nLL(1), Backtracking, Shunting yard\nLL(k) + syntactic and semantic predicates  \nLL, Backtracking  \nLR(0)  \nSLR  \nRecursive descent  \nRecursive descent, Backtracking  \nPEG parser interpreter, Packrat  \nPackrat (modified)  \nPackrat  \nPackrat + Cut + Left Recursion  \nPackrat (modified), mutating interpreter  \n2-phase scannerless top-down backtracking + runtime support  \nPackrat (modified to support left-recursion and resolve grammar ambiguity)  \nParsing Machine  \nEarley  \nRecursive descent + Pratt  \nPackrat (modified, partial memoization)  \nHybrid recursive descent / operator precedence  \nScannerless GLR  \nruntime-extensible GLR  \nScannerless, two phase  \nCombinators  \nEarley/combinators  \nEarley/combinators, infinitary CFGs  \nScannerless GLR  \ndelta chain\n```\n\n```text\nParsing algorithm\n```\n\n```text\nrecursive descent\n```\n\n```text\nAdaptive LL(*)\n```\n\n```text\nAdaptive LL(*)\n```\n\n```text\nAdaptive LL(*)\n```\n\n```text\nAdaptive LL(*)\n```\n\n```text\nAdaptive LL(*)\n```\n\n========================================\n\nComments:\n- Entire books have been written on this subject, I'm afraid this question is much too broad for SO.\n- Could u recommend some book on this topic? Thanks.\n- IMHO, the topic of creating various grammars and lexers/parsers is of more academic than practical interest. Most of the work done and time spent in a compiler is in the \"optimizer\".\n- The ANTLR reference doesn't go into the guts of the algorithm as far as I remember, but I'm pretty sure Terrence Parr must have published a paper on ALL(*). As for the classical LL, LR, LALR and so on look for the \"dragon book\" (a note of caution: it's very mathematical/academic in nature).\n- @LucasTrzesniewski Thanks, I will try to take the challenge.\n- Parsing Techniques: A Practical Guide. The first edition is available for free download. The literature reviee on the web page is invaluable.\n- The question is practically meaningless. Anybody can write their own parser generator or indeed invent their own (probably incorrect) parsing algorithm. There is no way to enumerate these and absolutely no point in doing so.\n- Yes, but what you really want is a parser generator engine that covers the broadest range of lanuages with the least amount of fuss. As a practical matter, GLR does extremely well on this front (arbitrary context-free grammars) and is available in a number of tools. GLL is equally good but pretty hard to find. Earley is OK but not very efficient, at least its relatively easy to code. Everything else has trouble with real grammars; you are only choosing between which parsing pit you fall into, and how much work it takes to climb out the pit for your particular grammar. Including ANTLR.\n- I like the way @IraBaxter says it, `you are only choosing between which parsing pit you fall into, and how much work it takes to climb out the pit for your particular grammar. Including ANTLR`.","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":8,"totalLines":95,"estimatedTokens":882}}1184{"id":"stack-5188639","source":"stackoverflow","questionId":5188639,"title":"Pointers to Incomplete Types","tags":["c++","oop","pointers","compiler-construction","incomplete-type"],"text":"Title: Pointers to Incomplete Types\nTags: c++, oop, pointers, compiler-construction, incomplete-type\nSource: Stack Overflow\n\nQuestion:\nConsider the following:\n\n```\nclass Incomplete;\n\nclass Complete\n{\n Incomplete* Foo; // Will only compile if Foo is a pointer.\n};\n\nclass Incomplete\n{\n Complete Bar; // Bar can be a pointer or an object.\n};\n```\n\nHow are pointers to incomplete types legal? How does the compiler know how big an object of type `Incomplete` is? And since it *can* apparently figure out the size, why are regular objects of incomplete types *illegal?*\n\n========================================\n\nTop Answer:\nThe size of a pointer is not dependent on the size of the type it points to. `int*` and `Incomplete*` are of the same size.\n\nFor regular objects, however, the size is unknown.\n\n========================================\n\nCode:\n```text\nclass Incomplete;\n\nclass Complete\n{\n    Incomplete* Foo; // Will only compile if Foo is a pointer.\n};\n\nclass Incomplete\n{\n    Complete Bar; // Bar can be a pointer or an object.\n};\n```\n\n```text\nIncomplete\n```\n\n```text\nint*\n```\n\n```text\nIncomplete*\n```\n\n```text\nIncomplete\n```\n\n========================================\n\nComments:\n- Seems this is no longer true in C++11. Pointers to incomplete types are illegal these days.\n- In addition, allocation will be done at run time, and at that point the size of the object should be known. Since pointers are always the same size, the size of the actual size of the object it is referring to can be determined when the application is executed.\n- The allocation code will not compile if the size is unknown, and it is true for the class member object (like the Incomplete::Bar in the example above), or a new dynamic allocation, or allocation on stack. Anywhere where the compiler has to put in the size of the memory chunk required by the object - the compiler will complain if the type is incomplete.","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":69,"estimatedTokens":474}}1185{"id":"stack-38281542","source":"stackoverflow","questionId":38281542,"title":"Assembly correct usage of word/byte/word ptr","tags":["assembly","compiler-construction","x86"],"text":"Title: Assembly correct usage of word/byte/word ptr\nTags: assembly, compiler-construction, x86\nSource: Stack Overflow\n\nQuestion:\nI'm writing a little machine code generator for assembly. I have a question regarding immediate to memory instructions:\n\nLet's take these 3 scenarios:\n\n```\nadd [ebx+04], 0x1\nadd [ebx+04], 0x4040\nadd [ebx+04], 0x401000\n```\n\nWhat I do is going by the immediate constant, I check what is the smallest number of bytes it can fit in then I assume whether its byte ptr, word ptr, dword ptr\n\nFirst one can fit in 1 byte so I assume it to be:\n\n```\nadd byte ptr [ebx+04], 0x1\n```\n\nSecond can fit in 2 bytes so I assume it to be:\n\n```\nadd word ptr [ebx+04], 0x4040\n```\n\nthird fits in 4 bytes so I assume it to be:\n\n```\nadd dword ptr [ebx+04], 0x401000\n```\n\nIs this correct?\n\n========================================\n\nTop Answer:\nIf you also implement a way to explicitly specify the operand size, and since it's your assembler, it's ultimately your call to allow or not such \"smart\" deduction. \n\nI strongly advice against it, programmers don't chose operand size based on the immediate values, but rather choose the immediates based on the operand size.\n\nSimply put, we consider `add DWORD [ebx+04h], 1` and `add WORD [ebx+04h], 1` two completely separate instructions.\n\n========================================\n\nCode:\n```text\nadd [ebx+04], 0x1\nadd [ebx+04], 0x4040\nadd [ebx+04], 0x401000\n```\n\n```text\nadd byte ptr [ebx+04], 0x1\n```\n\n```text\nadd word ptr [ebx+04], 0x4040\n```\n\n```text\nadd dword ptr [ebx+04], 0x401000\n```\n\n```text\nadd dword ptr [ebx+4], 1\n```\n\n```text\nadd DWORD [ebx+04h], 1\n```\n\n```text\nadd WORD [ebx+04h], 1\n```\n\n========================================\n\nComments:\n- No. How do you expect to generate a 32-bit add of 1? The PTR annotation is there for a reason.\n- there is a reason for the byte ptr, word ptr, etc nomenclature. Because the user needs to be able to select which instruction/operand. If you are making up your own you could instead use addb, addw, addd instead of x ptr.\n- if add [ebx+04], 0x00000001 produces a dword ptr, then maybe you can get away with it, if the users understand the rules for your assembly language.\n- Stating the obvious: the effect of adding 1 to a byte in memory is different from the effect of adding 1 to a dword in memory. In the latter case, carry (if any) goes over up to 4 bytes.\n- I had another question, i'm aware you can sign extend 8 bit value to 32 bits using the bit. Does it apply for 16bit to 32bit too?\n- The x86 architecture defines that this *s-bit* sign extends a byte into a word when the operand size is 16-bit, and that it extends a byte into a dword when the operand size is 32-bit. This clearly excludes sign-extending from 16-bit into 32-bit.","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":7,"totalLines":88,"estimatedTokens":686}}1186{"id":"stack-24833465","source":"stackoverflow","questionId":24833465,"title":"Bison/YACC vs. Lemon vs. standard input","tags":["parsing","compiler-construction","lemon"],"text":"Title: Bison/YACC vs. Lemon vs. standard input\nTags: parsing, compiler-construction, lemon\nSource: Stack Overflow\n\nQuestion:\nI'm trying to convert a calculator from Bison to Lemon.\n\nI ran into an unexpected problem involving standard input where\nthe two programs behave quite differently. The Bison version\nprints the result immediately after pressing [Enter]. With the\nLemon version, the result is delayed until I type a new\nexpression and press [Enter].\n\nI've created tiny Bison and Lemon grammars and Flex scanners to\nillustrate the problem. This is on Windows 7, using a July 2014\nversion of Lemon, Bison 2.41, and gcc (tdm64-2) 4.8.1.\n\n*A simple session with the Bison version*\n\nNotice how the result is returned after pressing [Enter] after\nthe simple expression.\n\n*A simple session with the Lemon version*\n\nNotice how the result is only returned after the entering of a\nsecond expression and pressing of [Enter] (ctrl Z signals\nend-of-input for cmd.exe).\n\nWhat have I done wrong?\n\n### Bison/Flex version source\n\nbadd.l:\n\n```\n%{\n #include \"y.tab.h\"\n #include \n%}\n\n%%\n[0-9]+ {yylval = atoi(yytext); return INTEGER;}\n[+] return PLUS;\n[\\n] return NL;\n[ \\t] ; /* skip whitespace */\n. {printf(\"Unknown character '%c'\\n\", yytext[0]); return 0;}\n%%\n\nint yywrap(void) {\n return 1;\n}\n```\n\nbadd.y:\n\n```\n%{\n #include \n int yylex(void);\n void yyerror(char *);\n%}\n\n%token INTEGER PLUS NL\n%left PLUS MINUS\n\n%%\n\nprog: prog expr NL { printf(\"%d\\n\", $2); }\n |\n ;\nexpr: INTEGER { $$ = $1; }\n | expr PLUS expr { $$ = $1 + $3; }\n ;\n%%\n\nvoid yyerror(char *s) {\n fprintf(stderr, \"%s\\n\", s);\n}\n\nint main(void) {\n yyparse();\n return 0;\n}\n```\n\nTo build:\n\n```\nbison -y -d badd.y\nflex badd.l\ngcc y.tab.c lex.yy.c -o badd.exe\n```\n\n### Lemon/Flex version source\n\nladd.l\n\n```\n%{\n #include \"ladd.h\"\n #include \n extern int yylval;\n%}\n\n%%\n[0-9]+ {yylval = atoi(yytext); return INTEGER;}\n[+] return PLUS;\n[\\n] return NL;\n[ \\t] ; /* skip whitespace */\n. {printf(\"Unknown character '%c'\\n\", yytext[0]); return 0;}\n%%\n\nint yywrap(void) {\n return 1;\n}\n```\n\nladd.y:\n\n```\n%include { #include }\n%syntax_error { printf(\"Lemon syntax error\\n\"); }\n%token_type {int}\n%left PLUS MINUS .\n\nstart ::= prog .\n\nprog ::= prog expr(a) NL . { printf(\"%d\\n\", a); }\nprog ::= .\n\nexpr(a) ::= INTEGER(b) . { a = b; }\nexpr(a) ::= expr(b) PLUS expr(c) . { a = b + c; }\n```\n\nmain.c:\n\n```\n#include \n#include \n\nvoid *ParseAlloc(void *(*mallocProc)(size_t));\nvoid ParseFree(void *p, void (*freeProc)(void*));\nvoid Parse(void *yyp, int yymajor, int foo);\n\nint yylex(void);\nint yylval;\n\nint main(void) {\n void *pParser;\n int tok;\n\n pParser = ParseAlloc(malloc);\n\n while ((tok = yylex()) != 0) {\n Parse(pParser, tok, yylval);\n }\n Parse(pParser, 0, 0);\n ParseFree(pParser, free );\n\n return 0;\n}\n```\n\nTo build:\n\n```\nlemon ladd.y\nflex ladd.l\ngcc main.c ladd.c lex.yy.c -o ladd.exe\n```\n\n========================================\n\nCode:\n```text\n%{\n    #include \"y.tab.h\"\n    #include <stdlib.h>\n%}\n\n%%\n[0-9]+      {yylval = atoi(yytext); return INTEGER;}\n[+]         return PLUS;\n[\\n]        return NL;\n[ \\t]       ;       /* skip whitespace */\n.           {printf(\"Unknown character '%c'\\n\", yytext[0]); return 0;}\n%%\n\nint yywrap(void) {\n    return 1;\n}\n```\n\n```text\n%{\n    #include <stdio.h>\n    int yylex(void);\n    void yyerror(char *);\n%}\n\n%token INTEGER PLUS NL\n%left PLUS MINUS\n\n%%\n\nprog:   prog expr NL                { printf(\"%d\\n\", $2); }\n        |\n        ;\nexpr:   INTEGER                     { $$ = $1; }\n        | expr PLUS expr            { $$ = $1 + $3; }\n        ;\n%%\n\nvoid yyerror(char *s) {\n    fprintf(stderr, \"%s\\n\", s);\n}\n\nint main(void) {\n    yyparse();\n    return 0;\n}\n```\n\n```text\nbison -y -d badd.y\nflex badd.l\ngcc y.tab.c lex.yy.c -o badd.exe\n```\n\n```text\n%{\n    #include \"ladd.h\"\n    #include <stdlib.h>\n    extern int yylval;\n%}\n\n%%\n[0-9]+      {yylval = atoi(yytext); return INTEGER;}\n[+]         return PLUS;\n[\\n]        return NL;\n[ \\t]       ;       /* skip whitespace */\n.           {printf(\"Unknown character '%c'\\n\", yytext[0]); return 0;}\n%%\n\nint yywrap(void) {\n    return 1;\n}\n```\n\n```text\n%include { #include <assert.h> }\n%syntax_error { printf(\"Lemon syntax error\\n\"); }\n%token_type {int}\n%left PLUS MINUS .\n\nstart   ::= prog .\n\nprog    ::= prog expr(a) NL .           { printf(\"%d\\n\", a); }\nprog    ::= .\n\nexpr(a) ::= INTEGER(b) .                { a = b; }\nexpr(a) ::= expr(b) PLUS expr(c) .      { a = b + c; }\n```\n\n```c\n#include <stdio.h>\n#include <stdlib.h>\n\nvoid *ParseAlloc(void *(*mallocProc)(size_t));\nvoid ParseFree(void *p, void (*freeProc)(void*));\nvoid Parse(void *yyp, int yymajor, int foo);\n\nint yylex(void);\nint yylval;\n\nint main(void) {\n   void *pParser;\n   int tok;\n\n   pParser = ParseAlloc(malloc);\n\n   while ((tok = yylex()) != 0) {\n      Parse(pParser, tok, yylval);\n   }\n   Parse(pParser, 0, 0);\n   ParseFree(pParser, free );\n\n   return 0;\n}\n```\n\n```text\nlemon ladd.y\nflex ladd.l\ngcc main.c ladd.c lex.yy.c -o ladd.exe\n```\n\n```text\nstart   ::= prog .\n\nprog    ::= prog print NL .\nprog    ::= .\n\nprint   ::= expr(a) .         { printf(\"%d\\n\", a); }\n```\n\n```text\nexpr\n```\n\n```text\nprint\n```\n\n========================================\n\nComments:\n- Your answer seems reasonable. But I do not quite understand the solution. Would it be possible to provide an example?\n- @SammyMitchell: Actually, the diagnosis was better than the solution. I edited the answer with a better solution.","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":10,"totalLines":319,"estimatedTokens":1343}}1187{"id":"stack-32129220","source":"stackoverflow","questionId":32129220,"title":"Is Go 1.5's bootstrapped compiler slower than the Go 1.4 compiler written in C?","tags":["go","compiler-construction","programming-languages"],"text":"Title: Is Go 1.5's bootstrapped compiler slower than the Go 1.4 compiler written in C?\nTags: go, compiler-construction, programming-languages\nSource: Stack Overflow\n\nQuestion:\nGo 1.5 managed to release a bootstrapped compiler written in Go.\nAssuming Go is slower than C, and the earlier Go compiler is written in C, is the bootstrapped compiler going to be slower in compilation time?\n\n========================================\n\nTop Answer:\nWell, PyPy is written in Python and it is known to be faster (sometimes) than CPython written in C.\n\nIn the case of Go, the language makes it easier to write more efficient code, so it should not be slower than the older C version. The writers were careful to make sure of this. Rather than faster it is easier to maintain and extend.\n\nC is fast because it is close to the CPU, but the speed of a language is mostly more about the algorithms to produce the more 'advanced' feature (simple to use but advanced compared to CPU features).\n\nA classic example is memory management. The C malloc/free is inherently slow as it reorganises free memory whenever you release it. A garbage collector sounds a lot slower because of the work it needs to do, but your program can release memory and continue on at full speed.\n\n========================================\n\nComments:\n- Bootstrapping has nothing to do with it. 'Bootstrapped compiler' and 'compiler written in C' are not mutually exclusive. What you are really asking is between 'compiler written in Go' and 'compiler written in C'.\n- Title didn't convey the specifics in the question body (is *Go 1.5's* bootstrapped compiler slower than *1.4's* compiler in C), so I edited it to be clearer, but it seems like a reasonable question, even if we don't entirely love the answer (yes, builds in 1.5 are slower).\n- Unfortunately, I don't think this is quite right--Go 1.5 compilation is slower than 1.4 (still quick compared to a lot of things, but that wasn't the question). The Go authors are hopeful they can make up some of that difference over time through performance tuning and better parallelization. Sorry not to have great cites for this, but, e.g., there's this thread starting July 5: groups.google.com/forum/#!topic/golang-nuts/uBFzGIturOA\n- I also can only go on what I heard on a podcast with one of the go team. I would expect the compiler to be slower because it can now do more. Are the compiled applications slower also?\n- No, the speed of the code it generates is independent of the speed of the compiler in this case. Programs that had long GC pauses generally have almost all of that work pushed into the background now (I answered about this elsewhere). If you have tons of goroutines and context switches the scheduler changes may help you. Otherwise many programs might see smallish changes.","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":27,"estimatedTokens":700}}1188{"id":"stack-44919087","source":"stackoverflow","questionId":44919087,"title":"How does a compiler identify its host machine's hardware? Which component?","tags":["c++","c","compiler-construction","systems-programming","intermediate-code"],"text":"Title: How does a compiler identify its host machine's hardware? Which component?\nTags: c++, c, compiler-construction, systems-programming, intermediate-code\nSource: Stack Overflow\n\nQuestion:\nMy teacher told me that the intermediate code is generic for all systems but a component of the compiler then make it different according the system/environment the code is run on. Can someone please explain it.\n\n========================================\n\nComments:\n- Are you talking about the host where the compiler is running or the host where the compiled binary is running on later?\n- Thanks for clarification. If you were asking about the compil**ed** binary, it would go in a different direction.\n- Well, rather three parts, as the major part of the backend is machine-independent as well - it'd be a hell otherwise.","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":0,"totalLines":13,"estimatedTokens":204}}1189{"id":"stack-6099415","source":"stackoverflow","questionId":6099415,"title":"Any way to parse preprocessed source through external tool before it compiles?","tags":["c++","visual-studio","visual-c++","compiler-construction","c-preprocessor"],"text":"Title: Any way to parse preprocessed source through external tool before it compiles?\nTags: c++, visual-studio, visual-c++, compiler-construction, c-preprocessor\nSource: Stack Overflow\n\nQuestion:\nI want the compiler to run preprocessing, generate all the .i files like it normally does if I just use the \"generate preprocessed file\" option, and then run an external tool, wait for it to complete, and then go on with the compilation of those .i files (which by now can be modified of course).\n\nIf that is not possible, is there a way to run an external tool on every file that is being compiled before preprocessing and compilation? (Would probably be a hell to debug in environment like that, but still).\n\nIf there is no option like that, could this even be done at all? I mean, does the compiler even use those .i files, or are they just output for the user somehow?\n\nBasically, is there any way to automatically tamper with the source before it is compiled, but without modifying the actual files?\n\nJust for refs: I am trying to think of a smart way to obfuscate all the strings with minimum modification of the source.\n\n========================================\n\nTop Answer:\nYes, you'd simply update your build system to have a preprocess step, obfuscate step, then compile-to-obj step. By default, most build systems merely merge all those to one step (and skip the obfuscate step). Should be no big deal with any \"real\" build system like Scons, waf, or even Make.\n\nIf you're using Visual Studio, then it is a bit more work. Microsoft wants you to write your build operations in MSBuild, and that's quite a bit of work, IMHO. It's not easy because MSVS is principally an IDE for iterative development, and NOT intended to be a build tool. It's not, and will never be, a build tool (even though it happens to do \"build things\", but only standard and very simple \"build things\"). But, you can still use the IDE with a different build tool. For example, we use Scons for our build, and it generates MSVS `*.sln` and `*.vcproj` files, and those files merely build with Scons (but all the files are edited in the MSVS IDE).\n\nThe simple answer: Your question is very simply a build-operations problem. It should be very straight-forward with any non-\"toy\" build system.\n\nThe distcc (distributed build tool) effectively preprocesses all files locally, then sends the `*.i` to remote compilers (that do not even need headers installed), and then ships back the `*.obj`. So, what you're talking about is pretty straight-forward.\n\n========================================\n\nCode:\n```text\n*.sln\n```\n\n```text\n*.vcproj\n```\n\n```text\n*.i\n```\n\n```text\n*.obj\n```\n\n========================================\n\nComments:\n- +1 for mentioning real intent!\n- Ic, never really used them before, but I now see the reason. Can they be incorporated into the visual studio somehow, or will I have to 1) move all the project settings to some config file, and update them only there and 2) build using the tool only, not the IDE?\n- Those are run before the build is started, aren't they? And the only files I can modify on that stage are the actual sources, which is what I don't want to do. I want to modify some intermediate files, so that the real source can stay the same, but I can still do my tricky stuff (that would reduce readability dramatically).\n- I would like to do everything in the IDE (which I might've mentioned more clearly in the question as I see now), so I gave the answer flag to ybungalobill. But your answer is perfectly suitable to my unclear question, so thx! While talking about build systems, is Boost-build any good? Seems a natural choice, since boost is used in almost any project of mine anyway...\n- I haven't worked with Boost-build directly, but the reviews are that it is very powerful, very confusing to work with, and very slow. But, YMMV. If you're looking for \"simple\", you might go with any of the \"most popular\" build systems, because they tend to have many simplified wrappers, and I don't think Boost-build is in that maturity category yet. (Good luck!)\n- Well I'll be damned, thought your solution wouldn't work cz the files would collide (the processed one and non-processed one), but turns out that when the /EP /P option is on, the generated .i file is not compiled to an .obj automatically, it is just generated, which is perfect for me. Lot's of kudos for the answer to my exact question!\n- I've encountered a small problem: it seems to work, but the problem is that the linker somwehow requests the object files of the \"source\" source files. So in the example you gave, it would give me error \"cannot open input file x.obj\". Even though x.preprocesssed.obj is compiled and included normally. Any ideas?","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":55,"estimatedTokens":1181}}1190{"id":"stack-44446720","source":"stackoverflow","questionId":44446720,"title":"NotSupportedException: The invoked member is not supported in a dynamic module in C# Unity 5","tags":["c#","compiler-construction","unity-game-engine"],"text":"Title: NotSupportedException: The invoked member is not supported in a dynamic module in C# Unity 5\nTags: c#, compiler-construction, unity-game-engine\nSource: Stack Overflow\n\nQuestion:\nThis is for my graduation project. The game idea briefly is to help students practice programming by providing some interesting missions that require a C# code solution something like codingame.com, when the user produces the correct output the result is visualised in a Unity scene.\n\nI am using a C# compiler plugin to compile the user's code in the Unity 5 run-time. Everything is working fine (each scene alone start playing the scene in the editor and stopping it) but when I advance from scene to the next one this error rises when I build the user's C# code in the run-time (NotSupportedException: The invoked member is not supported in a dynamic module) (the error always rises in the second scene or the next scene).\n\nThe Error: \n\n```\nNotSupportedException: The invoked member is not supported in a dynamic module\n```\n\nError\n\nThe Line that produces the error: \n\n```\nthis.assemblyReferences = domain.GetAssemblies().Select(a => a.Location).ToArray();\n```\n\nassemblyRefrences is an array of strings: string[] assemblyReferences;\n\nthis line is in a script called ScriptBundleLoader in its constructor\n\n========================================\n\nCode:\n```text\nNotSupportedException: The invoked member is not supported in a dynamic module\n```\n\n```text\nthis.assemblyReferences = domain.GetAssemblies().Select(a => a.Location).ToArray();\n```\n\n```text\nthis.assemblyReferences = domain.GetAssemblies().Select(a => \n{ \n    try{ return a.Location; }catch{ return null; }\n\n}).Where(s => s != null).ToArray();\n```\n\n```text\nthis.assemblyReferences = domain.GetAssemblies()\n    .Where(a => !a.IsDynamic)\n    .Select(a => a.Location)\n    .ToArray();\n```\n\n========================================\n\nComments:\n- Add the code where the error happens, also add the exception as text, not as image.\n- I added them both but I will write them again: the line which produces the error is : this.assemblyReferences = domain.GetAssemblies().Select(a => a.Location).ToArray() and the error is NotSupportedException: The invoked member is not supported in a dynamic module\n- Would using `Assembly.IsDynamic` not work? Catching an exception seems like a really nasty way of handling this, IMO.\n- @JonSkeet Yes, it will, I didn't event knew it existed :D. Updating answer.\n- is dynamic doesn't exist and produce an error @JonSkeet\n- @loaymansour: In that case I'd investigate the other properties that *are* available, to avoid catching exceptions for this.","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":0,"totalCodeBlocksIncluded":4,"totalLines":62,"estimatedTokens":655}}1191{"id":"stack-25660238","source":"stackoverflow","questionId":25660238,"title":"Why are there alignment boundaries larger than 4?","tags":["c++","compiler-construction"],"text":"Title: Why are there alignment boundaries larger than 4?\nTags: c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nWhat i don't understand, is why we have to align data in memory on boundaries larger than 4 bytes since all the other boundaries are multiples of 4. Assuming a CPU can read 4 bytes in a cycle, it will be basically no difference in performance if that data is 8 bytes large and is aligned on a 4 byte / 8 byte / 16 byte, etc.\n\n========================================\n\nTop Answer:\nWhen an x86 CPU reads a `double`, it reads 8 bytes in a cycle. When it reads an SSE vector, it reads 16 bytes. When it reads an AVX vector, it reads 32.\n\nWhen the CPU fetches a cache line from memory, it also reads *at least* 32 bytes.\n\nYour assumption that the CPU reads 4 bytes per cycle is false.\n\n========================================\n\nCode:\n```text\nld.double [48dec] // Works, 8 bytes aligned\nld.double [17dec] // Fails, not 8 bytes aligned\n```\n\n```text\ndouble\n```\n\n========================================\n\nComments:\n- What CPU/architecture are you referring to?\n- Why do you assume that the CPU reads 4 bytes in a cycle?\n- Why are there alignment boundaries larger than 1? ;-) If you can answer that, then you can answer your own question.\n- @Marco A: It was just an example. Right now i work with an x64 Intel.\n- @user3503828 you can't ask why a CPU works the way it does, without specifying which CPU you're talking about.\n- @PeterSchneider From what i understand, having only alignment of 1 would be possible for data to be stored at addresses like 3 or 5. But since 8 , 16, 32 are all multiple of 4 i don't see the reason of aligning data on 8 bytes rather than 4 bytes.\n- There are boundaries considerably larger than four for platform-specific reasons. An AS/400, for example, *requires* all pointers reside on a 128bit boundary (and you can only imagine the madness this introduces when you have arrays of structures with nested structures with pointer members). Alignment is very platform specific, so you can't just generalize that four ought to be good enough.\n- @user3503828, some x86 instructions require 16 or 32 byte alignment or they crash. Also reading a word (say a 16 byte word) split across a cache lines (64 bytes) or a virtual pages (4096 bytes) can lower performance.\n- I'm afraid i didn't state make case clearley. How much it read from the memory was just an example. But let's talk your example. either if it read 8 bytes or 16 or 32 all that data should be aligned on individual boundaries, 8, 16, 32. But since 8, 16, 32 are all multiples of 4 in my current humble understanding of things it should be the same for the cpu if the double for instance, would be aligned on a 4 byte boundary.\n- @user3503828: They're also multiples of 2. The fact that Y is a multiple of X just does not matter.\n- Is it safe to say that the alignment gives a hint to the cpu on how much data to read from memory? What i mean is, aligning an INT to 8 bytes is it safe to assume that the cpu would expect that the INT has another 4 bytes after it that are somehow meaningful for that data?\n- Again it depends on the architecture but usually it isn't for a \"hinting\" reason but in the case I posted above you will have an error 'address misaligned' if you don't honor the rule. To achieve a degree of performances and ensure consistency between caching capabilities and other architectural differences these constraints have to be met. It might be mandatory.","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":2,"totalLines":43,"estimatedTokens":869}}1192{"id":"stack-1435141","source":"stackoverflow","questionId":1435141,"title":"How does a modern compiler know an operation will not have sideffects?","tags":["compiler-construction"],"text":"Title: How does a modern compiler know an operation will not have sideffects?\nTags: compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have been reading compilers will optimize for operations without side effects.\nI assume this is done very conservatively, but how does the compiler know. \nDoes it have a look up table of side effect free operations or does it do it otherwise?\n\n========================================\n\nTop Answer:\nThe programmer might also give the compiler some hints: see for example noalias.\n\n========================================\n\nCode:\n```text\nrestrict\n```\n\n```text\nconst\n```\n\n```text\nvolatile\n```\n\n```text\nsin\n```\n\n```text\nerrno\n```\n\n```text\nglobal x\n```\n\n```text\ny->f\n```\n\n```text\n*y\n```\n\n```text\n**y\n```\n\n```text\n*x = 5;\n```\n\n```text\nconst\n```\n\n```text\nmutable\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":12,"totalLines":64,"estimatedTokens":201}}1193{"id":"stack-13714246","source":"stackoverflow","questionId":13714246,"title":"Assignment and sequence points: how is this ambiguous?","tags":["c","compiler-construction","sequence-points"],"text":"Title: Assignment and sequence points: how is this ambiguous?\nTags: c, compiler-construction, sequence-points\nSource: Stack Overflow\n\nQuestion:\nConsider the C code `a = a = a`. There's no sequence point for assignment, so this code produces a warning when compiling about an undefined operation on `a`.\n\nWhat are the possible values that `a` could have here? It seems like `a` couldn't possibly change values. Is there actually undefined behavior here or are compilers just being lazy?\n\n========================================\n\nTop Answer:\nThis is actually undefined behavior. `a` can have any value at all. \"I can't think of any way it can break\" is not the same as \"it's guaranteed to work\".\n\n========================================\n\nCode:\n```text\na = a = a\n```\n\n```text\na\n```\n\n```text\na\n```\n\n```text\na\n```\n\n```text\na\n```\n\n```text\nint a = 42;\na = a = a;\n```\n\n```text\na\n```\n\n```text\na\n```\n\n```text\na=a=a\n```\n\n```text\na\n```\n\n```text\na=a=b\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n```text\na=a=a\n```\n\n```text\na\n```\n\n```text\na=a=b\n```\n\n```text\na\n```\n\n```text\nb\n```\n\n========================================\n\nComments:\n- ... More to the point - are you just being silly? Need to put a splash more water with it.\n- Suppose I have a language that compiles down to C and an optimization step accidentally produces this....\n- Why wouldn't that behavior be defined? Just to avoid adding another sequence point to the language?\n- I guess the question may be better restated as: It is obvious what the semantics of `a=a=a` should be, so what are the technical reasons for not implementing those?\n- @Espressofa: There's an endless list of such possible cases and no good reason to bother.\n- At the time the C Standard was written, the authors expected that compiler writers would interpret Undefined Behavior to mean that compiler writers were not required to shield programmers from whatever consequences their actions might cause on the target execution platform. I've not seen any evidence that they intended compilers would go out of their way to generate useless code in cases where the behavior of straightforwardly-generated code would have been useful but the Standard imposes no requirements, but that seems to be fashionable nowadays.\n- @AnT Can you point out where in the standard it says that this has undefined behavior?\n- I can't find that text in the C99 standard. Can you point out which paragraph you're referring to?\n- @skyking: The “Between the previous and next…” text is in clause 6.5, paragraph 2, of the 1999 C standard (specifically ISO/IEC 9899:1999(E)).","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":18,"totalLines":102,"estimatedTokens":641}}1194{"id":"stack-5546076","source":"stackoverflow","questionId":5546076,"title":"Programming language design","tags":["programming-languages","compiler-construction"],"text":"Title: Programming language design\nTags: programming-languages, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI created a programming language and wrote it in my computer. It is an experimental non-professional programming language that I created for fun.\n\nA language needs the most important thing, a compiler.\n\nIs it a good idea to convert the source code to C/++ and call GCC?\n\nMy language looks like C++ and Java, it would not be difficult to convert without a parser.\n\nIt is not my goal to optimize anything neither to generate a binary for each platform. If I generate a C source, I can compile it for many platforms and use GCC optimizations.\n\nI do not know about tools that may help me, some tools that I know the name are yacc and llvm, but I do not know how they can help me.\n\nThe first part of fun is the design of programming language, the second part of fun is the implementation of runtime details. I think that a parser implementation is not a great fun.\n\nThanks\n\n========================================\n\nTop Answer:\nI'm not exactly an expert on this topic myself, but from what I know, \"converting\" your code to C or C++ is a pretty **bad** idea, *especially* when using GCC.\n\nGCC is designed to have a \"plug-in\" architecture. What you should instead do is create a *front-end* for the GCC compiler that is able to process the code from your language, and let the *back-end* of GCC take care of the code generation and optimization.\n\n(I haven't done this myself so I don't know the details of how it would work.)\n\n========================================\n\nCode:\n```text\na + b * ( c - sqrt(12 / 4) + sin(30))\n```","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":1,"totalLines":36,"estimatedTokens":410}}1195{"id":"stack-12884933","source":"stackoverflow","questionId":12884933,"title":"What is the C++ memory layout of objects/structs etc?","tags":["c++","compiler-construction"],"text":"Title: What is the C++ memory layout of objects/structs etc?\nTags: c++, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nIn C++ I presume the C++ standard has nothing to do with how data members are arranged within a class, in terms of memory layout? Would I be right in thinking this is down to the compiler in question?\n\nI'm very interested in learning how objects and other C++ entities (structs etc) are represented in physical memory (I know things like lists are node to node and arrays are continuous memory- but all the other aspects to the language).\n\n**EDIT: Would learning x86 assembler help with this and understanding C++ better?**\n\n========================================\n\nTop Answer:\nThe C++ standard does specify a few things, but far from everything.\n\nThe main rules are these:\n\n- objects in an array are laid out contiguously, with no padding between them.\n\n- class member objects *not* separated by an access specifier (`public:`/`private:`/`protected:`) are laid out in memory in the order in which they're declared, but there may be an unspecified amount of padding between member objects.\n\n- for certain types (standard-layout structs, in standardese terminology), the first base class, or if none exists, the first member, is laid out at the same address that the class itself.\n\nThere are a few more bits and pieces specified by the standard, but on the whole, the remaining details are really down to the compiler.\n\n========================================\n\nCode:\n```text\npublic:\n```\n\n```text\nprivate:\n```\n\n```text\nprotected:\n```\n\n========================================\n\nComments:\n- probably shortest answer to your question is fields of struct or classes are represented as contiguous in memory.\n- The standard does specify *some* things about how objects are to be represented in memory. But you're right that there's also a lot it doesn't specify\n- I have a small question, if I have a class with, say 10 member functions. Would it be known that accessing the 10th-declared function would be slower than the 1st? Is this down to the order they are declared in the header file?\n- The C++ standard doesn't really say anything about that, but in any sane compiler, it makes no difference. Member functions are not represented in the object at all. At compile-time, the compiler determines which function to call, so it doesn't need to look inside the object at all at runtime.\n- @NicolBolas: source, please? 9.2:12 specifies point 2, at least, and applies to any \"non-union class\". Perhaps you should check the standard before correcting and downvoting others? If you're unsure, it is actually allowed to post a comment asking for verification before you downvote. For point 3, you may be right, and I'll fix my answer to reflect that\n- @jalf +1 helping should still be rewarded\n- @OliverStutz: well, techincally, it's not much of a help if it provides incorrect information, so I appreciate being corrected. ;) But before downvoting, I think it's fair to ask that you actually ensure that you're correct, and that the person you're downvoting is not\n- By the way, I finally found it in the standard: @NicolBolas was correct on point 3, as specified by 9.2:18. I've updated the answer with this information. On point 2, my answer was, as far as I can see, correct. And I'd appreciate a citation if he has information to the contrary\n- @jalf: Fair enough. Since you can't rely on the layout for non-standard layout types, I assumed the spec defined them as unspecified.\n- @NicolBolas: technically you can't rely on the layout of standard-layout types either. Things like padding are still unspecified. But yeah, the layout of non-standard-layout types is even *less* reliable :)\n- @jalf: \"Rely on\" as in \"know that any two types with the equivalent layout are compatible\". Not as in \"know the byte offsets of\"; that's what `offsetof` is for.","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":55,"estimatedTokens":969}}1196{"id":"stack-2777811","source":"stackoverflow","questionId":2777811,"title":"How to generate a compiler error based on an attribute being missing in C#?","tags":["c#","compiler-construction","revit-api"],"text":"Title: How to generate a compiler error based on an attribute being missing in C#?\nTags: c#, compiler-construction, revit-api\nSource: Stack Overflow\n\nQuestion:\nI create a number of add-ins for the Revit Structure API. Each tool has to habe a class which implements the interface IExternalCommand. \n\nIn the latest version of Revit, for your tool to work you need to have two attributes on the class that implements that interface:\n\n[Regeneration(RegenerationOption.Manual)]\n[Transaction(TransactionMode.Automatic)] \n\nThe values in brackets can change, but there must be something there. Often I am finding myself forgetting to put the attributes on, then when it comes to runtime it crashes. Is there any way in Visual Studio 2010 to add a compiler warning or error saying that if your class implements that interface it must have those 2 attributes? I have resharper if that helps.\n\nCan anyone point me into the right direction?\n\n========================================\n\nTop Answer:\nNot during compile time, but I think it'd be easy to with reflection.\n\nI suggest a separate program that uses reflection to examine your compiled assembly, finds all classes with the specified interface, then checks attributes on those classes, returning a nice friendly error message very quickly.\n\nYou'd still have to run this program after you compile your program, but depending on your IDE, you could set it as a post-build step.\n\n========================================\n\nComments:\n- Very closely related: stackoverflow.com/questions/19454/&hellip;\n- sort of, but I'm after a compiler error, this code is not under unit testing (for various reasons)","metadata":{"transformedAt":"2026-08-18T18:32:17.959Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":30,"estimatedTokens":410}}1197{"id":"stack-11501697","source":"stackoverflow","questionId":11501697,"title":"Programmatically call C# Compiler to Compile C# code","tags":["c#",".net","compiler-construction"],"text":"Title: Programmatically call C# Compiler to Compile C# code\nTags: c#, .net, compiler-construction\nSource: Stack Overflow\n\nQuestion:\nI have designed my very own language that I, in the end, translate to C# code. Now I want to compile this C# code using the C# compiler through my Windows Forms Application.\n\nWhere is the actual compiler file located and how do I compile my code (currently placed in a string)??\n\n========================================\n\nTop Answer:\nYou can use C# CodeDom for this purpose. This link may help you\n\nhttp://www.codeproject.com/Articles/3445/Runtime-Compilation-A-NET-eval-statement\n\n========================================\n\nCode:\n```text\nCSharpCodeProvider\n```\n\n```text\nc:\\windows\\Microsoft.NET\\Framework\\v3.5\\\n```\n\n```text\nconst string outputfile = \"abc.exe\";\nconst string inputFile = \"xyz.cs\"\n\nProcessStartInfo startInfo = new ProcessStartInfo();\nstartInfo.FileName = \"c:\\\\windows\\\\Microsoft.NET\\\\Framework\\v3.5\\\\csc.exe\";\nstartInfo.Arguments = \"/out:\" + outputFile + \" \" + inputFile;\nProcess.Start(startInfo);\n```\n\n```text\ncsc.exe\n```\n\n========================================\n\nComments:\n- See -> stackoverflow.com/questions/553143/&hellip;\n- @DarkXphenomenon: What is the point of that? You did not read the question correctly.\n- He wants to compile C#. CSC is a way to do that, from the command line, and hence can be invoked from within another C# program as well.\n- @leppi the link is perfectly relevant to this question. Flagged your comment as rude/offensive.\n- @MattDavey et al. Is the command prompt programmatic? No...\n- @leppie is right .. I need to compile the program from my C# Application not via the Command line manually..!!\n- @tGilani: Perhaps the others can't read :)\n- @tGilani nobody said anything about manually typing commands into a command prompt window!\n- @MattDavey I have to concur with leppie. `csc` is not the ideal way to do that. Fortunately, the compiler is fully available via `CSharpCodeProvider`.\n- Why suggest `Reflection.Emit`?\n- -1 there is no reason to scell the csc.exe since codedom does exactly what asked.\n- While it is *valid* (I use Emit a lot), it is a bit like answering \"how do I top up the oil in my engine?\" with \"first, you'll need a few blocks of engine-grade metal, a milling machine, a ...\"\n- **(comments removed)** I've removed some unhelpful comments; basically, some people have observed that it may not be necessary or appropriate to shell to `csc` to achieve this. It is a *valid* answer (assuming the paths are correct for the machine, etc), but it has been argued that there may be more direct ways to achieve this. ``\n- I know that (CodeDom), but part of the question was \"where the csc.exe is located\"...","metadata":{"transformedAt":"2026-08-18T18:32:17.960Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":4,"totalLines":58,"estimatedTokens":676}}1198{"id":"stack-7345133","source":"stackoverflow","questionId":7345133,"title":"Why do we have alignment padding if memory is byte-addressable?","tags":["compiler-construction","memory-management","memory-alignment"],"text":"Title: Why do we have alignment padding if memory is byte-addressable?\nTags: compiler-construction, memory-management, memory-alignment\nSource: Stack Overflow\n\nQuestion:\nSince we can address every byte of memory individually, why do compilers take extra care to make sure that structs and it's members align to 32-bit borders in memory?\nI could be wrong here, but on a 32-bit system, is it not just as fast to get 4 bytes starting from say 0x0800, as it is from 0x0801?\n\n========================================\n\nTop Answer:\nTaken from wikipedia:\n\n For example, when the computer's word size is 4 bytes (a byte meaning 8 bits), the data to be read should be at a memory offset which is some multiple of 4. When this is not the case, e.g. the data starts at the 14th byte instead of the 16th byte, then the computer has to read two 4-byte chunks and do some calculation before the requested data has been read, or it may generate an alignment fault. Even though the previous data structure ends at the 14th byte, the next data structure should start at the 16th byte. Two padding bytes are inserted between the two data structures to align the next data structure to the 16th byte\n\nThe memory has to be `multiple of 4 bytes` for faster access and to `reduce computation` for `better performance`.\nso if the memory is address byte addressable usually of 4 bytes chunks in most of cases then we know where the next address is going to start e.g. as explained above also if you end up with `14 bytes` (that should be 16 bytes 4*4 = 16) then you know how much padding you have to use `16-14 = 2 bytes padding`. that is why padding is used in misaligned memory.\n\n========================================\n\nCode:\n```text\nmultiple of 4 bytes\n```\n\n```text\nreduce computation\n```\n\n```text\nbetter performance\n```\n\n```text\n14 bytes\n```\n\n```text\n16-14 = 2 bytes padding\n```\n\n========================================\n\nComments:\n- You should try google, eventhelix.com/realtimemantra/ByteAlignmentAndOrdering.htm\n- possible duplicate of Purpose of memory alignment","metadata":{"transformedAt":"2026-08-18T18:32:17.960Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":5,"totalLines":46,"estimatedTokens":512}}1199{"id":"stack-6642844","source":"stackoverflow","questionId":6642844,"title":"Is there any way to write a compiler front end without using syntax-directed translation?","tags":["parsing","compiler-construction","code-translation","intermediate-code"],"text":"Title: Is there any way to write a compiler front end without using syntax-directed translation?\nTags: parsing, compiler-construction, code-translation, intermediate-code\nSource: Stack Overflow\n\nQuestion:\nMy question is the same as the title. I just want to know if there are any other translation techniques to get the intermediate code that doesn't rely on embedding actions into the parser (that is, the parser will strictly create the abstract syntax tree, it won't generate any code). Thanks for any answers.\n\n========================================\n\nTop Answer:\nWhat you describe is in fact the standard compiler design. It is possible to write one-pass compilers, and indeed I have done so, that produce object code during parsing, but the norm is for the parser to produce an AST and for a subsequent traversal of the AST to produce the either the output or in many cases a further intermediate form such as triples, RTL, etc.\n\n========================================\n\nComments:\n- I suppose you could build a neural network, and train it, without any embedded knowledge - do you have a specific need to avoid embedding actions in the parser?\n- No, I just would like to know if there are any other options and I find it difficult to find information on the matter.\n- Thanks very much for the detailed answer. Does this mean that you can use the AST instead of the IR for generating the code? That's what I had in mind originally, but I wasn't sure if it was possible...\n- One can generate simple code directly from the AST by simply walking over it and generating code for each operator/operand as encountered. The resulting code will be pretty much what you would get if you generated directly from the parse. More complicated schemes walk over the AST and generate \"triples\" and then pass that to the rest of the compiler. If you want to really know how compilers generate code, you really ought to consult a standard text; see this SO answer: stackoverflow.com/questions/1669/learning-to-write-a-compile&zwnj;&#8203;r","metadata":{"transformedAt":"2026-08-18T18:32:17.960Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":0,"totalLines":19,"estimatedTokens":508}}1200{"id":"stack-8040408","source":"stackoverflow","questionId":8040408,"title":"How to build an Online Objective-C Compiler","tags":["php","objective-c","compiler-construction","online-compilation"],"text":"Title: How to build an Online Objective-C Compiler\nTags: php, objective-c, compiler-construction, online-compilation\nSource: Stack Overflow\n\nQuestion:\nI'm simply looking for an online objective-c compiler. At the moment I have only found ideone.com but it shows errors for any Foundation (e.g `NSObject`) or UIKit (e.g `UIView`) classes.\n\nIs there an online compiler for objective-c that can handle Foundation and UIKit classes? Failing that, how might I go about doing this myself on my own server?\n\n**EDIT:**\n\nAs it seems there is no existing online compiler. I'm now asking for a **how to guide** to build my own online objective-c compiler on a server. \n\nIt needs to be able to take a file of code, compile it and output any errors and warnings. It must also be able to handle Foundation and UIKit classes by having access to the necessary frameworks.\n\n*I don't know any web-development languages so as much help as possible would be great!*\n\n========================================\n\nTop Answer:\nhttp://ideone.com/ http://www.compileonline.com/compile_objective-c_online.php or https://compilr.com/ (note : compilr is not free) the website has a objective c compiler. it is not good just test is not problem \n\n```\nclick new code > choose objective-c > write your codes and click submit button.\n```\n\nthe problem is that many ads in there but it is not important :))\n\n **warning if you do not choose private,your code seems on the website**\n\n========================================\n\nCode:\n```text\nNSObject\n```\n\n```text\nUIView\n```\n\n```text\nclick new code > choose objective-c > write your codes and click submit button.\n```\n\n========================================\n\nComments:\n- Why do you need this? What goal are you trying to accomplish?\n- I would like to be able to compile code while I'm on the go on my iPad to check for errors or warnings.\n- You'd better read the terms of your Apple developer agreement carefully before you try this.\n- A cool idea but I suspect there's not a great solution framed like this. I'd recommend either using one of those apps that lets you screen- with your Mac back home, or just getting a MacBook Air (those guys are tiny!).\n- @Caleb It's not for an app, just for compiling code on the internet.\n- Just to make it clear: I **don't** want a iOS app, just a web service where I can compile code like ideone.com.\n- @Joshua, I'm not talking about apps. IIRC there's language in the developer agreement that basically prevents you from sharing access to Apple's developer tools. If you're going to try to build a system that lets you compile code over the web, you should make sure to read and understand that portion of the agreement.\n- @Caleb When you say 'developer tools' does that mean the Foundation and UIKit frameworks?\n- @Joshua How you interpret your agreement is between you and Apple. I'm not saying that there's no way to do what you propose, just that before you start you should be aware of the conditions that you agreed to.\n- @Joshua: Re: your comment on the bounty. If you'd take design suggestions on building your own such thing, I'd open a new question for it as this one really isn't asking that so people probably won't discuss an implementation. But it's an interesting enough project that some folks would probably happily chime in if you framed it as a how-could-I-do-this-myself.\n- @quixoto Thanks, I have taken your suggestions and have renamed the question and added some more information so it is now more focussed on how to build your own objective-c compiler on a server.\n- Back in 2010, there was a compiler that did all this stuff and worked under Windows XP/7. It was showing errors/lines and was outputing a Cydia and an .IPA project. So it shouldn't have been a problem in '11 and '12. Sad nobody has been helpful at that time.\n- FYI: pmbaty.com/iosbuildenv That's just doing everything the OP wants. Everything is done from a command-line, so you just catch the process standard output when using it as a web service. 5 minutes job.\n- @L&#233;onPelletier That looks good, but what command can be run from the command-line?\n- Sorry didn't notice your message. The compilation process is a command-line. If you create a project using this SDK in Visual Studio, you'll see somewhere in your project (from the template) that a batch of shell commands are executed. This is basically something similar to a gcc compilation on linux, but in Windows, for Objective-C. It compiles using a folder containing IOS libs. It outputs everything you need to create any offline/online IOS compiler service/application. Frighting! And existing since a while.\n- Actually, I just read carefully @Ben Zotto and my solution is the Windows-version and dirty way to achieve what he proposed. Haven't read about how all recent UI elements are compatible with pmbaty product, but you could use a Windows-based IOS development environment like Xamarin to code your stuff.\n- @L&#233;onPelletier Excellent, thanks Leon. Whereabouts in the project can I find the shell commands as I can't seem to see them for looking!\n- I've used it two years ago but don't have it anymore. I guess it still works the same. As of 2011, November, according to the website, the author started to use clang/llvm instead of GCC, so search for any command line using it in the template files. Normally, in the project property or configuration, you're supposed to see some command-line used when building the project. If so, then there you go.\n- CodeRunner's looks good but unfortunately it's for Mac. I **don't** want a iOS app, just a web service where I can compile code like ideone.com.\n- Answers to your questions. It's something for my own use and **not** something I want to productize. I don't need it to be an online Xcode just to be able to take some text and compile it. What you go onto say is very good but I am not very competent in PHP. If you could point me in the right direction with PHP especially the shell side of things as I have pretty much no knowledge of PHP.\n- You are going to need a server running Mac OS X, which you can rent for a monthly fee, but it isn't cheap. Compiling a UIKit based app cannot be done on a linux or windows server. The code to do it is relatively simple, but will be time consuming to get it exactly right. You should use @quixoto's instructions as a guide, and research/ask about each component individualy.\n- Do I really need a server running Mac OS X just to compile a single file? Why is it not possible to compile a UIKit app on a linux server?\n- @Joshua: Found this on using PHP to save a form input to a file: devshed.com/c/a/PHP/PHP-Form-to-Text-File-the-Basics Use the `system` command to shell out to gcc: php.net/manual/en/function.system.php (Whether and how you can get some form of the iOS toolchain running on a non-OS X system is beyond my knowledge.)\n- @quixoto Don't worry I've got it all sorted. I ended up saving the uploaded file to a permanent location, compiling it from there with the `shell_exec` command to shell out to `clang` whose command stores any output in a temporary log file. I then read the log file and display it. Here's a quick video: idzr.org/zjy5. Thanks very much for all your help!\n- This isn't a bad idea but I am really looking for a way to do this on a web server so I don't have to rely on a mac back home.\n- Just to add my grain of salt on another answer here, this method is working too. The answer could have been: \"I used to compile via SSH, but you could have a server with IOS SDK and call it via a web service.\" If you see it from that angle, the solution is working, and simple. Then for more users, you scale it on several machines. All machines need to do is to compile. That's not really intensive on the CPU.\n- Unfortunately, as I've mentioned in my question \"it shows errors for any Foundation (e.g NSObject) or UIKit (e.g UIView) classes\".","metadata":{"transformedAt":"2026-08-18T18:32:17.960Z","totalAnswersIncluded":1,"totalCodeBlocksIncluded":3,"totalLines":75,"estimatedTokens":1969}}

Showing the first 1,200 of 1428 lines. Download the file for the rest.