Compiler Design
Compiler Design
Compiler Design
error messages
4
Why Study Compilers?
• General background information for good software engineer
– Increases understanding of language semantics
– Seeing the machine code generated for language constructs
helps understand performance issues for languages
– Teaches good language design
– New devices may need device-specific languages
– New business fields may need domain-specific languages
5
Applications of Compiler Technology & Tools
6
Other Applications
• In addition to the development of a compiler, the techniques used in
compiler design can be applicable to many problems in computer
science.
– Techniques used in a lexical analyzer can be used in text editors, information retrieval
system, and pattern recognition programs.
– Techniques used in a parser can be used in a query processing system such as SQL.
– Many software having a complex front-end may need techniques used in compiler design.
• A symbolic equation solver which takes an equation as input. That program should parse
the given input equation.
– Most of the techniques used in compiler design can be used in Natural Language
Processing (NLP) systems.
oldval 12
• The type of the identifier newval must match with type of the expression (oldval+12)
• Ex:
MULT id2,id3,temp1
ADD temp1,#1,id1
• Ex:
( assume that we have an architecture with instructions whose at least one of its operands is
a machine register)
MOVE id2,R1
MULT id3,R1
ADD #1,R1
MOVE R1,id1
19
Example MicroJava Program
program P main program; no separate compilation
final int size = 10;
class Table {
int[] pos; classes (without methods)
int[] neg;
}
Table val; global variables
{
void main()
int x, i; local variables
{ //---------- initialize val ----------
val = new Table;
val.pos = new int[size];
val.neg = new int[size];
i = 0;
while (i < size) {
val.pos[i] = 0; val.neg[i] = 0; i = i + 1;
}
//---------- read values ----------
read(x);
while (x != 0) {
if (x > 0) val.pos[x] = val.pos[x] + 1;
else if (x < 0) val.neg[-x] = val.neg[-x] + 1;
read(x);
}
}
}
20
Analysis of specific compilers
Programs to be discussed:
• lex – Programming utility that generates a lexical
analyzer
• yacc – Parser generator
• lcc - ANSI C compiler
Platforms:
• All three programs designed for use on Unix
• lcc runs under DOS and Unix
lex Programming Utility
General Information:
• Input is stored in a file with *.l extension
• File consists of three main sections
• lex generates C function stored in lex.yy.c
Using lex:
1) Specify words to be used as tokens (Extension of regular
expressions)
2) Run the lex utility on the source file to generate yylex(
), a C function
3) Declares global variables char* yytext and int yyleng
lex Programming Utility
Three sections of a lex input file:
Using yacc:
1) Generates a C function called yyparse()
2) yyparse() may include calls to yylex()
3) Compile this function to obtain the compiler
yacc Parser Generator
yacc source lex source
yacc lex
#include “lex.yy.c”
y.tab.c lex.yy.c
cc
a.out
• C4droid is a user-friendly (but powerful) C/C++ IDE + C/C++ compiler for Android. Note that
C4droid supports devices with ARM processors only (not devices with Intel x86 or MIPS
processor).
Basic features:
- Offline compiler: create your own applications on Android device and run them even without
Internet access
- No root required (but C4droid can use it for your programs if you want)
- Full ANSI C and ISO C99 support with TCC + uClibc
- Source code editor with syntax highlighting, tabs, code completion, code formatting, file
association and infinite undo/redo
- Export & share your programs as APKs or native executables (for terminal apps)
More features with a free GCC plugin:
- Full C++ and almost complete C++11 support with GCC + Bionic libc
- Native Activity, Qt, SDL and SDL2 support for GUI
- The most recent version of GCC always available
- Makefile support: use the same build scripts as on your PC (BusyBox is included)
- Semi-automatic open-source library porting feature for enhanced programming & education
• AIDE is an integrated development environment (IDE) for developing real Android Apps
directly on Android devices. AIDE supports the full edit-compile-run cycle: write code with the
feature rich editor offering advanced features like code completion, real-time error checking,
refactoring and smart code navigation, and run your App with a single click.AIDE will turn your
Android tablet with keyboard into a real development box. We use the Transformer Prime
running Ice Cream Sandwich to code with AIDE. AIDE will turn your Android Phone into a
small development computer to browse and touch your code on the go.
• AIDE supports building Apps with Java/Xml and the Android SDK as well as Apps with C/C++
and the Android NDK.
• AIDE is fully compatible with Eclipse projects. You can just copy the sourcecode to your
device and open the Eclipse project in AIDE to start coding. Alternatively you can keep your
sourcecode on your Dropbox - AIDE integrates with Dropbox and allows to easily download
from your Dropbox and sync back your changes. AIDE can also open Android Studio projects,
which follow the default folder structure.
• AIDE supports GIT for professional development