FAQ Database Discussion Community


memory layout of a multiple-inherited object in C++

c++,compiler-construction,multiple-inheritance
class B1 { virtual void f1(); int int_in_b1; }; class B2 { virtual void f2(); int int_in_b2; }; class D: B1, B2 { int int_in_d; void f1(); void f2(); }; class D1: B1, B2 { int int_in_d; virtual void f1(); virtual void f2(); }; Based on this article, the memory...

Convert 3 Address codes in Assembly

assembly,compiler-construction,code-generation
I am trying to convert 3 Address codes in ASSEMBLY code [Code Generation]. Consider, The Assembly code sequence is: If order is changed to t2 t3 t1 t4, then ADD is done with Memory operand + Register operand but SUB is never done with Memory operand. Similarly, I have seen...

Operator precedence parsing

parsing,compiler-construction,operator-precedence
I have a grammar which has the following productions: S-> if e then S else | while e do S| begin L end |s L-> S; L|S I am supposed to construct the operator precedence parsing table for the above. But I'm little confused about how to decide the precedence...

Antlr4 token existence messing up parsing

parsing,logging,compiler-construction,antlr,antlr4
first time poster so my greatest apologies if I break the rules. I'm using Antlr4 to create a log parser and I'm running into some issues that I don't understand. I'm trying to parse the following input log sequence: USA1-RR-SRX240-EDGE-01 created 10.20.30.40/50985->11.12.13.14/443 With the following grammar: grammar Juniper; WS :...

who decide actual storage of register storage class?

c,compiler-construction,compilation,compiler-optimization,storage-class-specifier
Recently I have been asked following Q in a interview : Who actually decide that where register variable will be store(in RAM or register). I have searched on google, the ans I got that compiler decide. but how can compiler decide ? It should be decided at run time as...

GVN algorithm in LLVM Infrastructure

compiler-construction,llvm
Which algorithm is used for Global Value Numbering in LLVM? I see no paper or reference in GVN.cpp file. Does anybody know the details?

Semantics of SSA and multiple assignment

compiler-construction,ssa
In SSA form, a variable assigned from a phi ends up with the appropriate value depending on which path it was reached by. But what happens if, perhaps via some unusual path, both inputs to the phi have been assigned? e.g. a = 1 ... b = 2 ... c...

How do I properly register a Rust compiler plugin?

plugins,compiler-construction,loading,rust
I've been pulling my hair out over trying to get a Rust compiler plugin to register properly. I have a separate crate called rust_to_glsl that is in a subdirectory of my main code. Inside the rust_to_glsl/src/lib.rs file I have the following #![feature(plugin_registrar)] #![feature(rustc_private)] #![crate_type="dylib"] extern crate rustc; extern crate syntax;...

How can I write in (GNU) C a proxy function to interface two different calling conventions?

c,gcc,compiler-construction,inline-assembly,calling-convention
I'm writing an interpreter/compiler hybrid where the calling convention passes parameters on the CPU stack. Functions are simply pointers to machine code (like C function pointers) potentially generated at runtime. I need a proxy function to interface with the custom calling convention. I want to write as much as possible...

Java “The blank final field may not have been initialized” Anonymous Interface vs Lambda Expression

java,constructor,lambda,compiler-construction,java-8
I've recently been encountering the error message "The blank final field obj may not have been initialized". Usually this is the case if you try to refer to a field that is possibly not assigned to a value yet. Example class: public class Foo { private final Object obj; public...

Language class compilable to heapless runtime

compiler-construction,programming-languages,computation-theory,memory-model,turing-complete
So in the general case, a program uses both memory in the stack (automatically managed) and heap (garbage collected or manually managed). What 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...

Three address code (example)

compiler-construction,compiler-optimization
How can I describe suitable codes to implement: Sum :=0; for i from 1 to n do sum+= i endfor; ...

Three-address code and symbol tables

compiler-construction,ocaml,abstract-syntax-tree,intermediate-language,symbol-table
I am working on a hobby retargetable C compiler in OCaml and I'm building it bottom up. So far I have an annotated AST type, abridged: type 'e expr = | Int of 'e * int | Var of 'e * var | Neg of 'e * 'e expr |...

Left recursion parsing

parsing,recursion,compiler-construction,recursive-descent,left-recursion
Description: While reading Compiler Design in C book I came across the following rules to describe a context-free grammar: a grammar that recognizes a list of one or more statements, each of which is an arithmetic expression followed by a semicolon. Statements are made up of a series of semicolon-delimited...

How to define association on rule without tokens?

parsing,compiler-construction,grammar,yacc,happy
I have the following minimized grammar Exp : let var '=' Exp in Exp end { App (Fn $2 $6) $4 } | Exp Exp { App $1 $2 } | Exp OpCode Exp { Op $1 Add $3 } | '(' Exp ')' { $2 } | num {...

what compiler should I use as case study for self studying compiler principles techniques [closed]

c,compiler-construction,code-generation,abstract-syntax-tree,lexical-analysis
I decided to start studying compiler theory but the problem is that I want a compiler for any language in order to track each of lexical analyzer output. syntax tree. intermediate representation. code generation. I dont care for optimization right now I am aware of some questions similar to mine...

How to make my Python code read from the second line for my lexer

python,file,compiler-construction,lexer
I am busy building a small compiler that reads the file, finds keywords and then does what the keyword specifies. I have an issue that it starts reading the file from the begining each time and icould not find a way to solve this problem with out nested if statements....

Isn't an LR(0) parser using lookaheads as well?

parsing,compiler-construction,ll,lr,shift-reduce
An 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...

Bison reduce/reduce conflict if else condition

compiler-construction,bison
I was expecting shift/reduce conlict for if else, but It gave reduce/reduce conflict on line "| IF '(' boolean_statement ')' block". Here's some information which might help explain the following code: BOOL is token for keyword used at start of each line to indicate line is boolean operation BOOLEAN is...

Tokenized output of C source code

c,compiler-construction,tokenize
I want to look at the tokenized output my c-source code. The cpp processor first process the cpp-directives and then it tokenizes the c source code. Then the this tokenized output is parsed. After that assembler does the job and process continues. I have written my tokenizer using flex. I...

Item set and SLR(1) Questions in Compiler

compiler-construction,programming-languages,grammar,ll,lalr
I ran into a Old Exam question that solved by our TA. anyone could help me? when we create SLR(1) about S--> aSb | a grammar, one of the item set LR(0) is like as: { S-->a.Sb, S-->a., S-->.aSb, S-->.a} about extracted rule from above set, which of them is...

JavaCC and Unicode issue. Why \u696d cannot be managed in JavaCC although it belong to the range “\u4e00”-“\u9fff”

java,unicode,compiler-construction,antlr,javacc
We're trying to use JavaCC as a parser to parse source code which is in UTF-8( the language is Japanese). In JavaCC, we have a declaration like: < #LETTER: [ "\u0024", "\u0041"-"\u005a", "\u005f", "\u0061"-"\u007a", "\u00c0"-"\u00d6", "\u00d8"-"\u00f6", "\u00f8"-"\u00ff", "\u0100"-"\u1fff", "\u3040"-"\u318f", "\u3300"-"\u337f", "\u3400"-"\u3d2d", "\u4e00"-"\u9fff", "\uf900"-"\ufaff" ] > If it meets a string...

Why doesn't the Julia compiler optimise this loop?

compiler-construction,compilation,julia-lang
I know very little about compiling but I was surprised to see that Julia compiler doesn't optimize several processes. Let's consider Julia (that is a Just-in-Time compiler) and let's consider these two pieces of code that do essentially the same thing. n=1 @time for i = 1:10^8 n=n+1 end elapsed...

write parsed tokens and YYTEXT of flex lexical analyser to a file

compiler-construction,flex-lexer,lexical-analysis
I need to write the token and the text parsed of that token in a file with flex analyser. Basically I want to store each parsed token in an output file. Someone has some idea?...

How do compilers store hundreds of variables in only a few registers?

compiler-construction,vm-implementation
Say you have a virtual machine that only has 4 registers, A, B, C, and D. How do compilers store so many variables with only a limited amount of space? Are there multiple ways of doing this, or is there a single solid way that this is accomplished? What's the...

Emit only long jump instructions instead of short jump instructions for LLVM

assembly,compiler-construction,llvm,compiler-optimization,instrumentation
Is there any easy way to make LLVM NOT emit short jump instructions with 1-byte-displacement, like 75 30 JNE +30, eb 1a JMP +1a, etc; instead only emit 4-byte-displacement jump instructions with 3-byte zero paddings, like 0f 85 30 00 00 00 JNE +30, e9 1a 00 00 00 JMPQ...

At what stage is error thrown?

c,compiler-construction,compilation,compiler-errors
Compilation generally occur in several stages:lexical analysis, syntax analysis, etc. Say, in C language, I wrote a=24; without declaring a as int. Now, at what stage of compilation an error is detected? At syntax analysis stage? If that is the case, then what does lexical analyzer do? Just tokenizing the...

Symbol table content after Lexical analysis

compiler-construction,lexical-analysis
Say I have a C source code file with following content: int i = 21 + 10; int blah(){ int i = 21; return i + 10; } main(){ int i; i += i + 10; } at the end of lexical analysis phase, what will be the content of...

Size of types in LLVM

java,compiler-construction,llvm
We are currently building a compiler in Java that uses LLVM as intermediate code representation. We use several types and we need to allocate memory for them, using malloc for instance. I would like to know how to compute the size that elements will need in memory: pointers, structures, ......

Not sure if this LR(1) grammar has shift/reduce conflict

parsing,compiler-construction,programming-languages,grammar,lr
I'm trying to solve question on LR(1) grammar : S->AA A->Aa A-> b I got stuck in state 4 when : S-> AA. ,$ A-> A.a ,$ A->.Aa ,$ A->.a ,$ a and A shifted to other state and A is reduced in the same state should I consider this...

C++ Compiler Output

c++,gcc,compiler-construction,metaprogramming,compile-time
I would like to know if there is a tool / GCC compilation switch one could use to probably see the trace / output of the GCC compiler before runtime. Generally , I would appreciate a tool annotating how the GCC compiler parsed through my code showing all precomputed static...

Parse a variable (recursively)

parsing,variables,compiler-construction
I am writing a compiler for my toy language for learning and curiosity purposes only and I already have accomplished some things, like math operations and stuff, I am having some trouble trying to parse a variable "within" a variable though. Take these examples in pseudo-code: numbers = [1..20] My...

LLVM IR generated code to native code

c++,compiler-construction,llvm-ir
I am learning how compilers work. For the learning I used several books and tutorials and at some point stumbled upon this issue that I cannot resolve. The complete tutorial code I followed can be found at Github repository This code produces IR code and successfully executes it. However if...

If keywords in C# are not defined in, system class then where are they in the CLR?

c#,.net,compiler-construction
I am a new leaner in C#. I know the purpose of keywords in C#, and I know where keywords in C# are used, but it not defined in system class or any class then where is this in CLR, who is access, control and verify. When execute the program...

What is common parlance for the product of applying a single production rule?

parsing,compiler-construction
I'm wondering if there is a conventional name for the result of applying a single production rule in the course of parsing? So say my grammar has this production: attr -> NAME, EQUAL, TEXT Then in the course of parsing I match that production and have a small collection of...

Transcompiling to another language [closed]

c,compiler-construction
What are the typical ways in which code can be transcompiled? Currently, I'm writing a simple programming language, and the way it's handled is recursively. A list of nodes are looped through, and say the current node is a variable node, it will call a emit_variable_node function, which will literally...

ANTLR 4 Parser Grammar

parsing,compiler-construction,antlr,antlr4
How 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...

how to write buffer for compiler?

c#,compiler-construction,buffer
I want to write compiler with C# for the first time and I somehow lost what to do about its buffering!my reference is compilers principles,techniques and tools and it is said that : Because of the amount of time taken to process characters and the large number of characters that...

Fast calculation of dominators

compiler-construction,ssa
It is said that when converting intermediate code to static single assignment form, It is necessary to calculate the dominators of basic blocks The somewhat obvious way to do this as a fixed point of equations is slow To do it fast, you need to use the fairly complicated Lengauer-Tarjan...

How to create a lexical analyzer in ANTLR 4 that can catch different types of lexical errors

java,compiler-construction,antlr,antlr4,lexical-analysis
I am using ANTLR 4 to create my lexer, but I don't how to create a lexical analyzer that catches different types of lexical errors. For example: If I have an unrecognized symbol like ^ the lexical analyzer should a report an error like this "Unrecognized symbol "^" " If...

Compile at run time exclude class that is generated by CodeDom

compiler-construction,code-generation,.net-assembly,codedom
I have used CodeDom to generate class so called "Product.cs", my class has been generated in right path but my code does not work perfect and complete because of this "Product.cs" is exclude from my solution and I have to include it at run time I searched and found way...

clojure terminating paranthesis syntax

clojure,compiler-construction,parentheses
Is there any reason why the expression (foo5 (foo4 (foo3 (foo2 (foo1 arg))))) cannot be replaced with (foo5 (foo4 (foo3 (foo2 (foo1 arg)-) or the like, and then expanded back? I know lack of reader macros means that you cannot change syntax, but can this expansion possibly be hard coded...

Java - Method picking algorithm [duplicate]

java,methods,compiler-construction
This question already has an answer here: Using null in overloaded methods in Java [duplicate] 3 answers I was messing around with methods and was looking, which Method will be executed if I make two Methods named "hello", with different objects they want and pass a "null" to the...

I have a language where I need to determine if my flow control statement predicates are tautologies or contradictions

compiler-construction,compiler-errors,boolean-expression
Hello I'm working on a domain specific language with my project group. We 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...

Register Allocation in Compilers

compiler-construction,code-generation,cpu-registers
What 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?.

Creating a grammar for function prototypes and declaration bison

compiler-construction,bison
I'm build a compiler for a school project. Everything works fine but I am having difficulty with defining a grammar for functions. I have to detect if there are zero or more parameters and if the function is either a prototype or definition. I keep getting shift/reduce and/or reduce/reduce errors....

Why my code can only run on windows xp 32bit system?[code generation]

c,visual-c++,compiler-construction,compiler-optimization
I'm trying to write a program of generate x86 machine code, but code can only running on 32bit winxp. It says "Unhandled exception at 0x776315ee in tiny.exe: 0xC0000005: Access violation." on win7 64bit, why? #include <stdio.h> #include <stdlib.h> #include <memory.h> int main() { char *code = (char *)malloc(1024); memset(code, 0,...

How are `tagbody` and `go` implemented under the hood in Common Lisp?

compiler-construction,common-lisp
How 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? I'm writing a lispy language implemented in C and would like to have something like this....

Compilers: design a grammar for all sets of strings of 0s and 1s such that every 0 is immediately followed by at least one 1

compiler-construction,context-free-grammar
Think it may be s -> (0*1)*, but I don't understand it enough to be certain. This question is from the compilers Dragon book.

Why is a defined token not recognized during syntax analysis in C using Bison?

c,syntax,compiler-construction,token,lexical
I'm currently working on a simple infix-to-postfix compiler for a given grammar. I'm currently at the stage of syntax analysis. I have already written a lexical analyzer, using Flex library, however I'm stuck on a seemingly simple problem. The information below might seem like a lot to process, but I...

associativity of operations regarding floating points

compiler-construction,programming-languages,associativity,compiler-design
I am trying to understand tthe associativity of operations when it comes to floating points. In the lecture notes i have, the following is stated: "suppose floating-point values store seven digit of accuracy. Considee the problem of adding 11 numbers together, where one of the numbers is 10^7 and the...

How to solve this Grammar through SLR?

parsing,compiler-construction,grammar,left-recursion
I want to solve this Grammar. S->SS+ S->SS* S->a I want to construct SLR sets of items and parsing table with action and goto. Can this grammar parse without eliminate left recursion. Is this Grammar SLR....

Would it be feasible to have a compiler directly/manually emit LLVM IR?

compiler-construction,llvm,llvm-ir
Would manually emitting LLVM IR instead of using the LLVM API/bindings be feasible? Few languages have LLVM bindings and often they are outdated or lack good documentation, I would really like to avoid C++, so emitting LLVM IR directly would allow me to use my language of choice (F#). Just...

INotifyPropertyChanged-Feature on Auto-Properties

c#,mvvm,compiler-construction,aop,postsharp
I have many Model-Classes which implement the INotifyPropertyChanged-Interface in order to update the UI when value changed. Sadly the properties must be written fully to support this feature. I decreased my code already by using the 'SetPropertyValue'-Method() in BaseClass. private string _title; public string Title { get { return title;...

How to create AST with ANTLR4?

java,compiler-construction,antlr,abstract-syntax-tree,antlr4
I'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...

Syntax directed translation

compiler-construction,abstract-syntax-tree,context-free-grammar,semantic-analysis
I am trying to write the semantic rules for syntax directed translation of an expression following a given cfg into 3 code representation. Consider, Here, Why is || operator required ? gen() is the only thing needed it seems as it does what is required ....

How to manually manipulate precedence of special expressions in Parsec?

haskell,compiler-construction,interpreter,parsec,lambda-calculus
I 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; }. Here is my parser code. module Parser where import Text.Parsec import Text.Parsec.String import qualified Text.Parsec.Token as...

How to traverse typed abstract syntax tree in OCaml compiler

compiler-construction,ocaml,abstract-syntax-tree,ocamlbuild
I'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...

LR(1) parser online verification [closed]

parsing,compiler-construction,lr
I'm currently studying LR parsing and in need of an online LR(1) parser to be able to verify my outcome. I've already stumbled upon this(LL(1)), where I was able to verify my first and follow set but I also want to verify my parsing diagram. Could anyone provide me with...

can a top-down parser detect ungrammaticality an input string? [closed]

parsing,compiler-construction,compiler-optimization,top-down
I read that its possible to do this , Will it require backtracking? What would be the sketch from recovering from the parsing errors ....

Can a C implementation use length-prefixed-strings “under the hood”?

c,compiler-construction,compiler-optimization,c-strings,null-terminated
After 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...

Compilers: Register Allocation Against Complex Branching/Jumps

compiler-construction,compiler-theory
I've taken a side interest to optimizers and how they work, particularly with respect to register allocation. I have somewhat of a background in writing high-level interpreters that didn't bother to generate efficient machine code, so the parts revolving around compiler construction that relate to parsing, constructing ASTs, etc. are...

Delegate caching behavior changes in Roslyn

c#,.net,compiler-construction,roslyn,c#-6.0
Given the following code: public class C { public void M() { var x = 5; Action<int> action = y => Console.WriteLine(y); } } Using VS2013, .NET 4.5. When looking at the decompiled code, we can see that the compiler is caching the delegate at the call site: public class...

Confused at transferring an ambiguous grammar to an unambiguous one

parsing,compiler-construction,grammar,context-free-grammar,ambiguous-grammar
An ambiguous grammar is given and I am asked to rewrite the grammar to make it unambiguous. In fact, I don't know why the given grammar is ambiguous, let alone rewriting it to an unambiguous one. The given grammar is S -> SS | a | b , and I...

OCaml Types not compatible - OCamlyacc

compiler-construction,functional-programming,ocaml,yacc,ocamlyacc
funcexpr: /* This is a function: arguments -> string list */ LPAREN HEAD arguments RPAREN { let head a = [List.hd (List.hd a)] in head << $3 } | LPAREN REAR arguments RPAREN { let rear b = List.tl (List.hd b) in rear << $3 } | LPAREN ERECT...

Parallel Computation Overhead

java,multithreading,compiler-construction,parallel-processing
I am using the following code as part of a CompilerPhase class. The method gets called (and benchmarked) by the main method of the compiler. ParallelCompilerPhase: private Consumer<ICompilationUnit> apply; // ... @Override public void apply(Collection<ICompilationUnit> units) { this.count = units.size(); for (ICompilationUnit unit : units) { new Thread() { @Override...