The name compiler is primarily used for programs that translate source code
from a high-level programming language to a lower level language (e.g.,
assembly language, object code, or machine code) to create an executable
program.
It is computer software that transforms computer code written in one
programming language (the source language) into another programming
language (the target language).
Compilers are a type of translators that support digital devices, primarily
computers.
high-level languages is usually
r transpiler.
ogram that translates the form of
guage.
to tools used to create parsers that
un on a computer whose CPU or
om the one on which the compiler
mpiler.
n in the language that it intends to
m a low-level language to a higher
Program is free from lexical error
Program is free from semantics error
Program is free from syntax error
Even a complex program can be compiled in a small interval of time
In programs related to database accessing, many risks are reduced.
• 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.
– Most of the techniques used in compiler design can be used in
Natural Language Processing (NLP) systems.
Major parts of a compiler:
Analysis:An intermediate representation is created from the source program.
Synthesis:The equivalent target program is created from this intermediate
representation.
• Each phase transforms the source program from one representation into
another representation.
• They communicate with error handlers.
• They communicate with the symbol table.
• Lexical Analyzer reads the source program character by character and
returns the tokens of the source program.
• A token describes a pattern of characters having same meaning in the
source program. (such as identifiers, operators, keywords, numbers and so
on)
• Puts information about identifiers into the symbol table.
Ex: newval := oldval + 12 => tokens:
newval
:=
oldval
+
12
identifier
assignment operator
identifier
add operator
a number
• Regular expressions are used to describe tokens (lexical
constructs).
• A (Deterministic) Finite State Automaton can be used in the
implementation of a lexical analyzer.
• A Syntax Analyzer creates the syntactic structure (generally a parse tree)
of the given program.
• A syntax analyzer is also called as a parser.
• A parse tree describes a syntactic structure.
• In a parse tree, all terminals are at leaves.
• All inner nodes are non-terminals in
a context free grammar.
• The syntax of a language is specified by a context free grammar (CFG).
• The rules in a CFG are mostly recursive.
• A syntax analyzer checks whether a given program satisfies the rules
implied by a CFG or not.
– If it satisfies, the syntax analyzer creates a parse tree for the given
program.
• Ex: We use BNF (Backus Naur Form) to specify a CFG
assignment -> identifier := expression
expression -> identifier
expression -> number
expression -> expression + expression
• A compiler may produce an explicit intermediate codes representing the
source program.
• These intermediate codes are generally machine .But the level of
intermediate codes is close to the level of machine codes.
• Ex: newval := oldval * fact + 1
id1 := id2 * id3 + 1
MULT id2,id3,temp1 Intermediates Codes (Quadraples)
ADD temp1,#1,temp2
MOV temp2,,id1
• The code optimizer optimizes the code produced by the intermediate code
generator in the terms of time and space.
• Ex:
MULT id2,id3,temp1
ADD temp1,#1,id1
Errors by compilation:
1.The compiler highlights all the possible errors which are obstacle in
the program to provide a good meaning.
2. Logical errors could be found only at runtime of the program.
Basic error in C:
Array bound missing
Array size too large
Bad file name
Call of on function
Declaration missing
Compound statement missing
Illegal accessions
http://judge.uaizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_C
Rectangle
Write a programwhich calculates the area and perimeter of a given
rectangle.
Input:
The length a and breadth b of the rectangle are given in a line separated by a
single space.
Output:
Print the area and perimeter of the rectangle in a line.
The two integers should be separated by a single space.
Constraints:
1 ≤ a, b ≤ 100
Source code:
#include <stdio.h>
int main ()
{
int a,b,perimeter,area;
scanf ("%d %d",&a,&b);
perimeter=2*(a+b);
area=a*b;
printf ("%d %dn",area,perimeter);
}
Discussion:
The source code is correct, therefore there is no error after
we compile.
https://www.urionlinejudge.com.br/judge/en/problems/view/1036
Read 3 floating-point numbers. After, print the roots of
bhaskara’s formula. If it's impossible to calculate the roots because
a division by zero or a square root of a negative number, presents
the message “Impossivel calcular”.
Input:
Read 3 floating-point numbers A, B and C.
Output:
Print the result with 5 digits after the decimal point or the
message if it is impossible to calculate.
Sourcecode:
#include <stdio.h>
#include <math.h>
int main() {
float B,C,p,q,r;
scanf ("%f %f %f",&A,&B,&C);
p=(B*B)-(4*A*C);
if (p<0)
printf ("Impossivel calcularn");
else if (A == 0)
printf ("Impossivel calcularn");
else {
q=(-B+sqrt(p))/(A+A);
r=(-B-sqrt(p))/(A+A);
printf ("R1 = %.5fn",q);
printf ("R2 = %.5fn",r);
}
return 0;
}
DISCUSSION:
AS WE DIDN’T DECLARE AN IDENTIFIER A , THE
COMPILER IS SHOWING THAT THERE IS AN ERROR IN
THE 5TH LINE.
 http://pintokarsanda.blog.binusian.org/files/2014/03/front-and-back-end-compilers.png
 https://en.wikipedia.org/wiki/Compiler
 https://en.wikipedia.org/wiki/Preprocessor
 https://en.wikipedia.org/wiki/Lexical_analysis
 https://en.wikipedia.org/wiki/Parsing
 https://en.wikipedia.org/wiki/Semantic_analysis_(compilers)
 https://en.wikipedia.org/wiki/Intermediate_representation
 https://en.wikipedia.org/wiki/Program_optimization
 https://www.google.com/search?source=hp&ei=ZrDZWt6CKMrbvAT5nZegDg&q=co
mpiler+front+end+back+end&oq=compiler+front+end+back+end&gs_l=psy-
ab.3..0j0i22i10i30k1l2.5110134.5119852.0.5121674.34.26.0.0.0.0.1800.1800.8-
1.1.0....0...1c.1.64.psy-ab..33.1.1798.0...0.HSNGp7V62Zw
Presentation1

Presentation1

  • 2.
    The name compileris primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language, object code, or machine code) to create an executable program. It is computer software that transforms computer code written in one programming language (the source language) into another programming language (the target language). Compilers are a type of translators that support digital devices, primarily computers.
  • 4.
    high-level languages isusually r transpiler. ogram that translates the form of guage. to tools used to create parsers that un on a computer whose CPU or om the one on which the compiler mpiler. n in the language that it intends to m a low-level language to a higher
  • 5.
    Program is freefrom lexical error Program is free from semantics error Program is free from syntax error Even a complex program can be compiled in a small interval of time In programs related to database accessing, many risks are reduced.
  • 6.
    • In additionto 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.
  • 7.
    – Many softwarehaving a complex front-end may need techniques used in compiler design. – Most of the techniques used in compiler design can be used in Natural Language Processing (NLP) systems.
  • 8.
    Major parts ofa compiler: Analysis:An intermediate representation is created from the source program. Synthesis:The equivalent target program is created from this intermediate representation. • Each phase transforms the source program from one representation into another representation. • They communicate with error handlers. • They communicate with the symbol table.
  • 10.
    • Lexical Analyzerreads the source program character by character and returns the tokens of the source program. • A token describes a pattern of characters having same meaning in the source program. (such as identifiers, operators, keywords, numbers and so on) • Puts information about identifiers into the symbol table.
  • 11.
    Ex: newval :=oldval + 12 => tokens: newval := oldval + 12 identifier assignment operator identifier add operator a number • Regular expressions are used to describe tokens (lexical constructs). • A (Deterministic) Finite State Automaton can be used in the implementation of a lexical analyzer.
  • 12.
    • A SyntaxAnalyzer creates the syntactic structure (generally a parse tree) of the given program. • A syntax analyzer is also called as a parser. • A parse tree describes a syntactic structure. • In a parse tree, all terminals are at leaves. • All inner nodes are non-terminals in a context free grammar.
  • 13.
    • The syntaxof a language is specified by a context free grammar (CFG). • The rules in a CFG are mostly recursive. • A syntax analyzer checks whether a given program satisfies the rules implied by a CFG or not. – If it satisfies, the syntax analyzer creates a parse tree for the given program.
  • 14.
    • Ex: Weuse BNF (Backus Naur Form) to specify a CFG assignment -> identifier := expression expression -> identifier expression -> number expression -> expression + expression
  • 16.
    • A compilermay produce an explicit intermediate codes representing the source program. • These intermediate codes are generally machine .But the level of intermediate codes is close to the level of machine codes. • Ex: newval := oldval * fact + 1 id1 := id2 * id3 + 1 MULT id2,id3,temp1 Intermediates Codes (Quadraples) ADD temp1,#1,temp2 MOV temp2,,id1
  • 17.
    • The codeoptimizer optimizes the code produced by the intermediate code generator in the terms of time and space. • Ex: MULT id2,id3,temp1 ADD temp1,#1,id1
  • 20.
    Errors by compilation: 1.Thecompiler highlights all the possible errors which are obstacle in the program to provide a good meaning. 2. Logical errors could be found only at runtime of the program.
  • 21.
    Basic error inC: Array bound missing Array size too large Bad file name Call of on function Declaration missing Compound statement missing Illegal accessions
  • 22.
    http://judge.uaizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_C Rectangle Write a programwhichcalculates the area and perimeter of a given rectangle. Input: The length a and breadth b of the rectangle are given in a line separated by a single space.
  • 23.
    Output: Print the areaand perimeter of the rectangle in a line. The two integers should be separated by a single space. Constraints: 1 ≤ a, b ≤ 100
  • 24.
    Source code: #include <stdio.h> intmain () { int a,b,perimeter,area; scanf ("%d %d",&a,&b); perimeter=2*(a+b); area=a*b; printf ("%d %dn",area,perimeter); }
  • 25.
    Discussion: The source codeis correct, therefore there is no error after we compile.
  • 26.
    https://www.urionlinejudge.com.br/judge/en/problems/view/1036 Read 3 floating-pointnumbers. After, print the roots of bhaskara’s formula. If it's impossible to calculate the roots because a division by zero or a square root of a negative number, presents the message “Impossivel calcular”. Input: Read 3 floating-point numbers A, B and C.
  • 27.
    Output: Print the resultwith 5 digits after the decimal point or the message if it is impossible to calculate.
  • 28.
    Sourcecode: #include <stdio.h> #include <math.h> intmain() { float B,C,p,q,r; scanf ("%f %f %f",&A,&B,&C); p=(B*B)-(4*A*C); if (p<0) printf ("Impossivel calcularn"); else if (A == 0) printf ("Impossivel calcularn"); else { q=(-B+sqrt(p))/(A+A); r=(-B-sqrt(p))/(A+A); printf ("R1 = %.5fn",q); printf ("R2 = %.5fn",r); } return 0; }
  • 29.
    DISCUSSION: AS WE DIDN’TDECLARE AN IDENTIFIER A , THE COMPILER IS SHOWING THAT THERE IS AN ERROR IN THE 5TH LINE.
  • 30.
     http://pintokarsanda.blog.binusian.org/files/2014/03/front-and-back-end-compilers.png  https://en.wikipedia.org/wiki/Compiler https://en.wikipedia.org/wiki/Preprocessor  https://en.wikipedia.org/wiki/Lexical_analysis  https://en.wikipedia.org/wiki/Parsing  https://en.wikipedia.org/wiki/Semantic_analysis_(compilers)  https://en.wikipedia.org/wiki/Intermediate_representation  https://en.wikipedia.org/wiki/Program_optimization  https://www.google.com/search?source=hp&ei=ZrDZWt6CKMrbvAT5nZegDg&q=co mpiler+front+end+back+end&oq=compiler+front+end+back+end&gs_l=psy- ab.3..0j0i22i10i30k1l2.5110134.5119852.0.5121674.34.26.0.0.0.0.1800.1800.8- 1.1.0....0...1c.1.64.psy-ab..33.1.1798.0...0.HSNGp7V62Zw