198 questions
2
votes
2
answers
158
views
tail call optimization with arguments on stack and callee with more arguments than caller
I have been writing a compiler and have discussed this situation with a colleague.
In the most general case, suppose you have your arguments on the stack (once argument registers are exhausted).
If ...
1
vote
1
answer
931
views
What is the point of the 4 grammars specified in Chomsky hierarchy?
I'm currently studying compiler's and am on the topic of "Chomsky Hierarchy and the 4 languages." But it beats me as to what the practical purpose of all this is?
It'd be great if I could ...
1
vote
1
answer
320
views
How does the latest ANTLR4 resolve the "dangling else" ambiguity?
I am using antlr 'org.antlr:antlr4:4.9.2' and come across the "dangling else" ambiguity problem; see the following grammar IfStat.g4.
// file: IfStat.g4
grammar IfStat;
stat : 'if' expr '...
2
votes
1
answer
552
views
Why don't most interpreted languages like ruby provide an optional compiler?
Are all interpreted languages not eventually machine code? I'm curious if the reason is because companies don't think it's worth the effort, if there is an inherit conflict that makes it impossible, ...
0
votes
0
answers
47
views
Print integers as strings in mips programs [duplicate]
This is my python code and I need to convert this to MIPS
Print (“Enter two numbers here:”)
a = int (input())
b = int (input())
sum=a+b
print(“sum is =” + str (sum))
I am having difficulty in trying ...
1
vote
1
answer
504
views
Exercise 4.2.8 from "Compilers - Principles, Techniques, & Tools" (a.k.a. Dragon Book)
I have been banging my head on this problem for a while.
This is the text of the exercise:
The grammar in Fig. 4.7 generates declarations for a single numerical identifier; these declarations ...
0
votes
0
answers
228
views
Eliminate Left Recursion in a Context Free Grammar
I understood Left Recursive Grammar (LRG) and how to remove it.
But i dont know how to remove recursive grammar which combine both left and right recursive:
A -> aAb | c
The full question is ...
1
vote
1
answer
2k
views
What is Local and Global optimization in Compiler Design?
Does the local optimization refers to one function only? And the global optimization refers to entire program?
-2
votes
1
answer
205
views
Interpreter and Compiler
Can any one help me in figuring out what compilers and interpreters are? And what their difference is ? Appreciate it if explained for Java beginner as I am one.
1
vote
1
answer
176
views
Why can't compilers automatically optimize regular recursion?
In nearly every functional programming tutorial, a large section is dedicated to teaching you how to convert algorithms to a tail-recursive format, since this can be optimized to a loop.
This is fine,...
2
votes
1
answer
266
views
Computing Liveness of Arrays and Other Non-Scalars in Low Level Intermediate Code
I am having trouble wrapping my head around the concept of calculating array liveness since they are non-scalar values. All books and courses that talk about liveness always use scalar values only but ...
0
votes
0
answers
246
views
can I reduce executable file size by defining funtions in source code?
In interpreted languages like python I can reduce the size of scripts by defining functions inside it.
In the languages like c, c++ can I use the same trick to reduce the size of executable file(not ...
0
votes
1
answer
146
views
How easy is to find a string that leads to conflict in a SLR(1) parser compared to a LR(1)
It is known that SLR(1) parsers usually have less states than LR(1). But is it easier or harder because of this to find a string that leads to conflict in a SLR(1) parser compared to a LR(1) and why?
...
0
votes
1
answer
332
views
Finding a grammar is not LL(1) without using classical methods and transforming it to LL(1)
Let's say i have this grammar:
S -> A C x | u B A
A -> z A y | S u | ε
B -> C x | y B u
C -> B w B | w A
This grammar is obviously not LL(1), which i can find constructing the parsing ...
4
votes
1
answer
1k
views
Is this an intermediate representation?
I'm looking into how the v8 compiler works. I read an article which states source code is tokenized, parsed, an AST is constructed, then bytecode is generated (https://medium.com/dailyjs/...
0
votes
2
answers
364
views
Writing a Compiler in C
I am very interested in compilers and how they work so I want to know if there are good sources or books to write a compiler from scratch. As I am a novice C programmer it would be perfect if the ...
6
votes
1
answer
3k
views
LR(1) parsing table with epsilon productions
I'm having trouble building the collection of sets of items for LR(1) parsers with a grammar containing epsilon productions. For example, given the following grammar (where eps stands for epsilon)
S -...
2
votes
1
answer
294
views
Gcc llvm backend guides to make reading source codes a little bit easier?
I begin to get acquainted with the implementation of algorithms of code-generation and optimizations in gcc and llvm. Can anyone give an advice on where to see materials, articles, lectures about how ...
-1
votes
1
answer
38
views
May multi-Pass compilers compile big programs in memory without running out of it?
I’m designing a multi-pass compiler for a language and the AST is completely kept in memory.
This means that I’m saving the entire AST in memory, no matter how many files the program is composed of.
...
1
vote
1
answer
101
views
Which stage of the compiler detects the following errornous program?
I'm having some trouble in understanding the differences between different errors in the complier phases, for instance this program:
int main()
{
int x = 5;
int x = 6;
}
My guess is that ...
0
votes
1
answer
590
views
Finding an equivalent LR grammar for the same number of "a" and "b" grammar?
I can't seem to find an equivalent LR grammar for:
S → aSbS | bSaS | ε
which I think recognize strings with the same number of 'a' than 'b'.
What would be a workaround for this? Is it possible to ...
1
vote
1
answer
1k
views
How to validate input based on CFG?
Consider this grammar:
expr ::= LP expr RP
| expr PLUS|MINUS expr
| expr STAR|SLASH expr
| term
term ::= INTEGER|FLOAT
Context-free grammar is defined as G = ( V, Σ, R, S )...
0
votes
0
answers
77
views
Building and processing a compile graph on a set of watched source files
I think this might be a quite common problem, but somehow it is hard to find suitable answers.
Background
I'm currently investigating how to speed up the node variant of Patternlab. It is basically a ...
0
votes
1
answer
238
views
Type of error in expression evaluating process
Supposing I want to write module to evaluate e simple string expression like "5+3", "(7*8/2)/6" etc...
The process that I am thinking of is:
Lexical analysis in order to convert the string to set of ...
3
votes
1
answer
496
views
How can I prove a language is context-free if I remove one character from its alphabet?
Suppose that we have a language L that is context free,and 'a' among others belongs to its alphabet. How can I prove that the language ERASEa(L),that removes all instances of the character 'a' in the ...
0
votes
0
answers
52
views
Are header files strictly necessary when the code is open source?
It was my understanding that header files were a developed practice, the header files created by copying all the externally-meaningful symbols from a source C file. Thus being able to give the linking ...
3
votes
1
answer
352
views
Moving recursive calls in "almost tail position" to true tail position
While reading Guido's reasoning for not adding tail recursion elimination to Python, I concocted this example of almost tail recursion in Haskell:
triangle :: Int -> Int
triangle 0 = 0
triangle x =...
4
votes
4
answers
1k
views
How does a compiler know if something is allocated on the stack or heap?
How does a compiler know if something is allocated on the heap or stack, for instance if I made a variable in a function and returned the address of the variable, the compiler warns me that "function ...
0
votes
3
answers
340
views
How does a function caller use a header file to determine what to do with a compiled binary?
My understanding is that C++ (and C, I guess) header files are never compiled, and simply act as an explanation of the interface of the C++ file they describe.
So if my header file describes a hello()...
192
votes
13
answers
17k
views
Why can't dead code detection be fully solved by a compiler?
The compilers I've been using in C or Java have dead code prevention (warning when a line won't ever be executed). My professor says that this problem can never be fully solved by compilers though. I ...
3
votes
2
answers
1k
views
Why use NFAs over DFAs
I'm studying some theory of computation at the moment and, as is implied, it is very theoretical.
I can convert from regex to NFAs to DFAs pretty easily, I can understand that.
But since all NFAs ...
14
votes
2
answers
18k
views
Minimum pumping length for the following regular languages
What are the minimum pumping length for the following languages ?
The empty language
(01)*
10(11*0)*0
1011
011 U 0*1*
Here are my solutions. Please correct me if I'm wrong.
p = 0 because the language ...
1
vote
2
answers
77
views
Pass Directory Context to Compiler
I'm writing a Java compiler in C just as a recreational project. But, I've noticed that when we compile files in the command line, such as "gcc example.c", the compiler is able to find example.c in ...
1
vote
1
answer
87
views
In object oriented programming, is it safe to always downcast a variable?
This is more about compiler theory, given I'm playing around with making one. I'm wondering if it is theoretically safe to always downcast a variable within a subclass.
class Car {
BodyType ...
3
votes
1
answer
607
views
Compilers: Register Allocation Against Complex Branching/Jumps
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 ...
1
vote
3
answers
716
views
Verifying data types/structs in a parser
I'm writing a recursive descent parser, and I'm at the point where I'm unsure how to validate everything. I'm not even sure if I should be doing this at the stage of the parser. What I mean is, I ...
6
votes
1
answer
3k
views
Is performance of "less/greater than than" better than "less/greater than or equal to"
Is it computationally more performant to compare less/greater than over less/greater than or equal to?
Intuitively one could think that less/greater than is marginally better.
Can a compiler use ...
2
votes
1
answer
183
views
Formal defintion of function closures in computer science
at the moment I am writing a scientific expose. A part of the content is about the definition of closures in our developed DSL. However, I was not able to find references of how to formally describe ...
4
votes
3
answers
231
views
How to recognize variables that don't affect the output of a program?
Sometimes the value of a variable accessed within the control-flow of a program cannot possibly have any effect on a its output. For example:
global var_1
global var_2
start program hello(var_3, ...
9
votes
1
answer
819
views
Code generation for expressions with fixed/preassigned register
I'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 ...
1
vote
1
answer
349
views
Reducing grammar to LL1
I have the following grammar:
A-> AB|CA
B-> Bd | ef
C-> e|f
I removed left recursion as follows and my grammar looks as below:
A->CAA'
A'-> BA'
A'-> epsilon
B-> efB'
B'->dB'
...
21
votes
1
answer
6k
views
How are Lambda Expressions Translated Into Java Byte Code
I am trying to create an example using lambda expression in java and i am using offical JDK8. My example was run successfully. But when i trying to check how the compiler translate lambda expression ...
0
votes
2
answers
1k
views
Eliminating ambiguity by left factoring
Can you eliminate ambiguity by left factoring?
For example, the dangling else.
Or is left factoring only eliminating left recursion?
Thanks.
0
votes
1
answer
635
views
SLR Parsing - with an epsilon production
Say I have:
S -> A
A -> B C A a | ϵ
B -> k | ϵ
C -> m
Now in the initial state S' -> S, I'm going to include:
S' -> .S
Then the closure of S:
A -> .B C A a , A -> .
Closure ...
4
votes
1
answer
969
views
Syntax Directed Translation and Semantic Analysis
I'm having difficulties in understanding the exact differences between Syntax Directed Translation (SDT) and Semantic Analysis.
By reading the Dragon Book, I know that SDT is like an extension to CFG....
3
votes
1
answer
352
views
Theory of interpreters, partial evaluators, and compilers
So 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 ...
1
vote
2
answers
250
views
Conditional Dependencies in Compiler Semantic Analysis Passes
Imagine that we have a been given an Excel spreadsheet with three columns, labeled COND, X and Y.
COND = TRUE or FALSE (user input)
X = if(COND == TRUE) then 0 else Y
Y = if(COND == TRUE) then X else ...
0
votes
1
answer
1k
views
Programming Language Translation
I want to translate a simple programming language to another programming language. I only need that the output be syntactically valid so the code-generation part can me ditched.
Say, a subset of ...
9
votes
2
answers
4k
views
What is the simplest way to hand-parse left recursive grammars?
I am writing a parser for a pet project, and for educational purposes, would like to do it by hand instead of using a parser generator. Unfortunately, many online resources (and the compiler course I ...
0
votes
0
answers
754
views
Nondeterministic finite automata with multiple accept/failure states
I'm having trouble making accept states for an NFA for situations that would be easy with a regular expression or an (apparently equivalent) deterministic finite automata.
For instance, what if the ...