Sample C Program :
//C hello world example
#include <stdio.h>
int main()
{
printf("Hello world\n");
return 0;
}
2. First Write C Program using Text Editor , such as [ Borland C/C++ 3.0 , Notpad++,Notpad ]
3. Save Program by using [.C] Extension.
4. File Saved with [.C] extension is called Source Program.
Step 2 : Compiling
1. Compiling C Program : C Source code with [.C] Extension is given as input to compiler and
compiler convert it into Equivalent Machine Instruction.
2. In Borland C/C++ 3.0 program can be compiled using key [Alt + F9 ].
3. Compiler Checks for errors . If source code is error-free then Code is converted into Object File
[.Obj ].
Compiler :
Compiler takes high level human readable program as input and convert it into the lower level
code.This conversion takes place using different phases.First phase of compiler is lexical analysis.
Must Read : [What is Compiler ?]
1.Analysis Phase :
Lexical analysis
Syntax analysis
Semantic analysis
Task of Lexical Analysis is to read the input characters and produce as output a sequence of tokens that
the
parser
uses
for
syntax
analysis.
Token
Type
sum
Identifier
Operator
num1
Identifier
Operator
num2
Identifier
Seperator
6. Lexical Analyzer is also called Linear Phase or Linear Analysis or Scanning
7. Individual Token is also Called Lexeme
8. Lexical Analyzers Output is given to Syntax Analysis.
Syntax Analysis :
Now Consider above C Programming statement. In this statement we Syntax Analyzer will create a
parse
tree
from
the
tokens.
[box]Syntax Analyzer will check only Syntax not the meaning of Statement[/box]
Syntax analyzer will just check whether plus operator has two operands or not . It does not
checks the type of operands.
Suppose One of the Operand is String and other is Integer then it does not throw error as it only
checks whether there are two operands associated with + or not .
So this Phase is also called Hierarchical Analysis as it generates Parse Tree Representation of
the Tokens generated by Lexical Analyzer
Syntax analyzer will just create parse tree. Semantic Analyzer will check actual meaning of the
statement parsed in parse tree. Semantic analysis can compare information in one part of a parse tree
to that in another part (e.g., compare reference to variable agrees with its declaration, or that
parameters to a function call match the function definition).
What is Interpreter ?
1. Interpreter Takes Single instruction as input .
2. No Intermediate Object Code is Generated
3. Conditional Control Statements are Executes slower
4. Memory Requirement is Less
5. Every time higher level program is converted into lower level program
6. Errors are displayed for every instruction interpreted (if any)
7. The interpreter can immediately execute high-level programs, thus interpreters are
sometimes used during the development of a program, when a programmer wants to add small
sections at a time and test them quickly.
8. In addition, interpreters are often used in education because they allow students to program
interactively.
BASIC
CLS
INPUT "Enter your name: ", Name$
IF Name$="Mike" THEN
PRINT "Go Away!"
ELSE
PRINT "Hello, "; Name$; ". How are you today?"
END IF
Compiler
Interpreter
Interpreter
Takes Single instruction as input .
Intermediate
isGenerated
No Intermediate
isGenerated
Conditional Control
are Executes faster
Object
Code
Statements
Memory
Requirement : More(Since Object
Object
Code
Code is Generated)
not
Program
need
be compiledevery time
Example : C Compiler
Example : BASIC
Between
Source
Source Code :
1. Source Code is In the form of Text.
2. Source Code is Human Readable.
3. Source Code is Generated by Human.
4. Source Code is Input Given to Compiler.
Object Code :
1. Object Code is in the form of Binary Numbers.
Code
and
Object
Code
: Your Title
: DD/MM/YYYY
Or
/**
* Title
: Your Title
* Date
: DD/MM/YYYY
while(i<5)
{
Statement 3;
Statement 4;
Statement 5;
} //end of while
}//end of for
}//end of if
1. If You Look at last Three Lines , At First Glance we are unable to Distinguish Corresponding
Closing Brace.
2. So Short but Sweet Comment Can be used to Avoid Your Confusion