compiler-theory

Languages and VMs: Features that are hard to optimize and why

I'm doing a survey of features in preparation for a research project. Name a mainstream language or language feature that is hard to optimize, and why the feature is or isn't worth the price paid, or instead, just debunk my theories below with anecdotal evidence. Before anyone flags this as subjective, I am asking for specific examples ...

What are modern and old compilers written in?

As a compiler, other than an interpreter, only needs to translate the input and not run it the performance of itself should be not that problematic as with an interpreter. Therefore, you wouldn't write an interpreter in, let's say Ruby or PHP because it would be far too slow. However, what about compilers? If you would write a compiler...

IR tree representation of binary operation

I have a simple operation like so: k + 1 What would be a IR tree representation of it? So far I came up with: BINOP(+, MEM(NAME(k)), CONST(1)) but I am not sure if I need the MEM for NAME or not. Any help welcome. The notation we use is exactly the same as in this pdf: http://www.computing.dcu.ie/~hamilton/teaching/CA449/notes/tr...

Example compilers

I'm searching for the source code of a compiler capable of creating Win32 programs from an input program in a programming language (It doesn't matter which, maybe the simpler the better) Yet I can't find anything right for me and huge compilers like GCC make me extremely confused as they have so many features that I don't know where to ...

How does the compiler detect duplicate definition across translation units

Hi All, How does a compiler detect duplicate definition across translation unit. Suppose there were a extern const variable declaration in an header file. If this header file was used in more than one translation unit - each having a separate definition - each TU object creation would be successful, however when the final executable is...

Indentation control while developing a small python like language

Hello, I'm developing a small python like language using flex, byacc (for lexical and parsing) and C++, but i have a few questions regarding scope control. just as python it uses white spaces (or tabs) for indentation, not only that but i want to implement index breaking like for instance if you type "break 2" inside a while loop that'...

Determining the maximum stack depth

Imagine I have a stack-based toy language that comes with the operations Push, Pop, Jump and If. I have a program and its input is the toy language. For instance I get the sequence Push 1 Push 1 Pop Pop In that case the maximum stack would be 2. A more complicated example would use branches. Push 1 Push true If .success Pop Jump ....

Removing Left Recursion in ANTLR

As is explained in http://stackoverflow.com/questions/2652060/removing-left-recursion , there are two ways to remove the left recursion. Modify the original grammar to remove the left recursion using some procedure Write the grammar originally not to have the left recursion What people normally use for removing (not having) the left ...

Getting tree construction with ANTLR

As asked and answered in http://stackoverflow.com/questions/2999755/removing-left-recursion-in-antlr , I could remove the left recursion E -> E + T|T T -> T * F|F F -> INT | ( E ) After left recursion removal, I get the following one E -> TE' E' -> null | + TE' T -> FT' T' -> null | * FT' Then, how to make the tree construction ...

Duplicate literals and hard-coding.

I see the follow pattern occurring quite frequently: b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1); Notice that the literal string is used twice. The extract is from the nginx source-base. The compiler should be able to merge these literals when it is encountered within the compilation unit. My questions a...

What are cross-checks and how to avoid them?

Hi guys, This is not a practical question like others, but I would like to understand the term cross-check very clearly. thanks in advance. ...

I need to learn basics of compiler design in the next few days.

I need to pass a "General Compiler Design" test from a potential employer and believe the best way to do so would be to get to know a little about all of compiler design. If you think another method would be better, let me know. Now then: I have intermediate to advanced knowledge of: C C++ (not as much as C) C# Java Python Ruby Perl PH...

What is an example of a lexical error and is it possible that a language has no lexical errors?

for our compiler theory class, we are tasked with creating a simple interpreter for our own designed programming language. I am using jflex and cup as my generators but i'm a bit stuck with what a lexical error is. Also, is it recommended that i use the state feature of jflex? it feels wrong as it seems like the parser is better suited t...

Formally constructing Control Flow Graph

Hi, Im writing a compiler for university project, and I would like to transform my Abstract Syntax Tree into a Control Flow Graph(CFG). Im 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 ...

how it produce multiple token for each lexeme?

I just started reading The Dragon Book and I'm finding difficulty in understanding some statements. It says: "lexical analyser produce sequence of token for each lexeme in the source program". Can you please help me to understand the above line? I know about tokens and lexemes, but what is meant by producing multiple token for each l...

How are divisions handled in the PIC18 ISA

Kind of an extension of http://stackoverflow.com/questions/3694100/converting-to-ascii-in-c , I was wondering exactly how divisions are handled on a PIC18X. If I perform a DIV operation, how many instructions does the compiler interpret that as? How many clock cycles will it take for the operation to complete? Is the number of clock cyc...

What are the key design choices to make a wicked fast compiler?

I want to know how to design a compiler that compiles very, very quickly. First, let me head off some obvious misunderstandings of my question: I am not talking about the speed of the code produced by the compiler. There are already many resources available for learning how to optimize generated code. What I'm having trouble finding i...

Why can't the compiler just compile my code as I type it?

Why can't the compiler just compile my code as I type it? From the user's point of view, it could work as smoothly as syntax colouring does today. If you stop typing for long enough (maybe a couple of seconds) the compilation (not linking) would finish, and code errors would be identified using something like syntax colouring. It's not...

What is the precise definition of a lookahead set?

I'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. ...

Scala "<-" for comprehension

Hello guys, I have found that Scala always has a "natural explanation" to anything. Always something like "ohh, but that's just a function being called on this and that object with this and that parameter". In a sense, nothing is really compiler-magic as we know it from other languages. My question is on the <- operator as used in the ...