0% found this document useful (0 votes)
9 views19 pages

Two's Complement Representation of Negative Numbers

The document outlines the stages of compilation, focusing on lexical analysis, syntax analysis, and code generation. Lexical analysis converts source code into tokens, while syntax analysis checks the grammatical structure and builds a parse tree. Code generation then converts this structure into object code, with an emphasis on code optimization for performance, though it may introduce complexity and compatibility issues.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views19 pages

Two's Complement Representation of Negative Numbers

The document outlines the stages of compilation, focusing on lexical analysis, syntax analysis, and code generation. Lexical analysis converts source code into tokens, while syntax analysis checks the grammatical structure and builds a parse tree. Code generation then converts this structure into object code, with an emphasis on code optimization for performance, though it may introduce complexity and compatibility issues.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Stages of

compilatio
n
• This is the first stage
• It starts by converting lexemes (words)
into a series of tokens
• As it reads the source code it reads it
letter by letter.

Lexical • When it encounters a white space,


operator symbol it decides that a lexeme

analyses (word) is complete.


• It then checks if the lexeme is valid by
using a pre-defined set of rules that
allow every lexeme to be identified as
valid token
• The tokens will be in the format
[tokenclass : token]
Token Example
class

What is Identifier
Keyword
Any function or variable name
As, If, Else, Endif, Function, End
consider Separator
Function, return, Then
() &
ed as Operator +, -, *, /, %, ^, DIV, MOD, <, <=, >,
>=
valid Literal Hello world

tokens
Number -4, 0, 3, 4
Quote “”
Boolean True, False
Datatype Integer, Decimal, String, Boolean,
Character
Function checkScore(score As Interger) Then
if score > 75 Then
return"pass"
Example else

of
return "fail"
endif

Lexical endFunction

[Keyword : Then]

analyses [Keyword : Function]


[Identifier :
checkScore]
[Keyword : Return]
[Quote : “]
[Literal : Pass]
[Separator : ( ] [Quote : “]
[Identifier : score] [Keyword : Else]
[Keyword : As] [Keyword : Return]
[Datatype : Integer] [Quote : “]
[Separator : )] [Literal : Fail]
[keyword : If] [Quote : “]
[Identifier : score] [Keyword : EndIf]
[Operator : >] [Keyword :
Side
effects
•Notice how all the
white spaces and
comments have been
removed.
•This is the side effect
of the process. The
white spaces are being
removed because they
are being passed over
or skipped by the
lexer.
Index Token Token class Data type
0 Function Keyword

What next
1 checkScore Identifier
2
• ( Separator
Index
•Token
3 score Identifier
•4Token class As Keyword
The next is that after we •5Data Integer Datatype
created the token streams
6 ) Separator
from the lexemes in the
7 If Keyword
source code, it is time to This is
8 > Operator added at
input those tokens into a
symbol table. 9 75 Literal the
10 Then Keyword syntax
11 Return Keyword analysis
From our example we will 12 “ Quote stage
assume that the lexer: 13 Pass Literal
14 “ Quote
- Only creates one symbol
15 Else Keyword
table
16 “ Quote
- Does not store duplicate 17 Fail Literal
entries 18 “ Quote

- Uses simple indexing 19 EndIf Keyword

instead of hashing 20 EndFunction Keyword


Advantages

This breaks the source code into manageable units before


passing it to the syntax analysis phase. This is done so that
the code is simplified input for further analysis

Lexical analysis can detect and report lexical errors early in


the compilation process, such as invalid characters ,
improperly formatted literals, and unrecognised tokens.

The lexer can discard unnecessary characters such as


whitespace and comments. This stage will only keep the
essential data
Disadvantages

Lexical scanning can only catch lexical errors such as


unrecognisable characters or invalid symbols. It cannot detect
syntax errors.

While lexical analysis is efficient for small programs, the time


spent scanning larger programmers can be harder

One more disadvantage is that it usually stores the entire


source code in memory and processes it into tokens. This can
consume a lot of storage
What is Syntax
analysis?
••Syntax Analysis is the second phase in
the stages of compilation.
••During this stage, the structure of the
source code is analysed, by taking the
tokens produced by lexical analyser and
put them into a grammatical hierarchy,
known as a syntax tree or parse tree.
••It checks the grammatical structure of
the input to see if it is in the correct syntax
of the programming language it has been
written in.
••If the source code fails to stick to the
correct syntax, then the syntax analyser
will report syntax errors.
Purpose of
Syntax Analysis
••Ensures that the sequence of tokens follows
the correct syntax of the programming
language.
••It organises tokens in the form of a parse tree
representing the syntactical structure of code.
This then helps developers make sense of how
different parts of the program will eventually fit
together.
••Identifies syntax errors, such as missing
semicolons, unmatched parentheses, and
incorrectly defined functions.
••Foundation for the Next Stages: The parse
tree becomes the input for the semantic
analysis stage, which includes checking the
meaning and logic of the code.
Syntax
Creating A Parse Tree
Advantages
Syntax analysis primarily allows the compiler to check whether
the source code has followed syntax rules of the programming
language. This helps a compiler in detecting and reporting errors
in the source code.

Syntax analysis enables improved code generation by


generating a parse tree of the source code. This tree is useful in
the code generation phase of the compiler design to generate
more optimized code.

Easier semantic analysis: Once the parse tree has been built, the
compiler can do semantic analysis much more easily since
semantic analysis depends on the structural information
provided by the parse tree.
Disadvantages

Parsing is a complicated process and the quality of the parser itself can
drastically affect the performance of the outputted code. It is challenging to
provide a parser for languages. In particular, it is even more complicated for
languages that have ambiguous syntaxes.

Performance degradation: Syntax analysis can bring extra overhead to the


compilation process and will reduce some performance of a compiler.

Cannot handle all languages - various languages have no formal grammars,


most of the standard languages couldn't parse.
Conclusion
•Syntax analysis is crucial in
ensuring that a program follows the
grammatical rules of a programming
language.
•By building parse trees, it forms the
basis for deeper checks (semantic
analysis) and code generation.
•Detecting syntax errors early helps
in maintaining the structure and
correctness of code, improving the
overall quality of the software
development process.
Code
generation
• This is the 3rd stage
• This is the stage where it
will use the abstract
syntax tree produced in
the syntax analysis stage
to convert it to object code
so that the instructions
can be read by the
machine and executed.
• Object code is the machine
code produced before the
final step (linker) is ran.
In these final phases, the machine code is generated
Code optimisation tries to reduce the time of the program by:

• Spotting redundant instruction and producing object code that


achieves the same effect as the source program , but not
necessarily by the same means
Code • Removing subroutines that are never called
Optimisation • Removing variables and constants that are never constants that
are never referenced
• Code optimisation can considerably increase compilation time
for a program
• Risk of bugs : Optimization may introduce errors, making it
essential for develop to understand these risks
• Compatibility issue: Optimized code might not work properly on
all platforms which results in some devices not being able to use
the code.
Advantages

Enhanced performance: This will allow to be faster,


and this will overall improve the user experience

Lower memory usage : Reduces unnecessary


instructions which can overall save memory

Efficient resource management: Better use of CPU and


system resources, making it important for software
engineering
Disadvantages

Increase Complexity - Optimized code can be harder to


maintain, impacting long term project sustainability

Risk of bugs : Optimization may introduce errors, making it


essential for develop to understand these risks

Compatibility issue: Optimized code might not work


properly on all platforms which results in some devices not
being able to use the code.

You might also like