0% found this document useful (0 votes)
11 views

Compiler Designassignment

Uploaded by

kidefresb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Compiler Designassignment

Uploaded by

kidefresb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

DEBRE MARKOS UNIVERSITY

BURIE CAMPUS

DEPARTEMENT OF COMPUTER SCIENCE


Compiler Design group Assignment

GROUP 1 MEMBERS:

STUDENT NAME STUDENT ID


1. KIDANEMARIYAM FRESBHAT --------------------- DMU1403803
2. ROBEL YISEHAK---------------------------------------- DMU1404143
3. EYASU ABEBE ------------------------------------------- DMU1406005
4. KALEAB GETNET---------------------------------------DMU1403763
5. HAYMANOT TILAHUN--------------------------------- DMU1403698

SUBMITTED TO: KASSAHUN(MSc.)


SUBMITTED DATE: 14/05/2017E.C
Contents
Introduction...........................................................................................................................................1
Part I: Basic Techniques in Compiler Construction................................................................................2
1. Lexical Analysis..............................................................................................................................2
2. Syntax Analysis..............................................................................................................................3
3. Semantic Analysis (Context-Sensitive Analysis).............................................................................4
4. Intermediate Code Generation and Optimization.............................................................................5
Part II: Basic Data Structures in Compiler Construction.........................................................................6
5. Abstract Syntax Trees (AST)...........................................................................................................6
6. Symbol Tables................................................................................................................................7
7. Three-Address Code (TAC).............................................................................................................8
8. Stack Machines..............................................................................................................................9
Part III: Basic Software Tools in Compiler Construction.......................................................................10
9. Lexical Analyzer Generators.........................................................................................................10
10. Parser Generators......................................................................................................................11
Summary.............................................................................................................................................12
References...........................................................................................................................................13
Introduction

Compiler design is a cornerstone of computer science, enabling the translation of high-level


programming languages into machine-level code for execution. This field is essential for
building reliable and efficient software, as it addresses key aspects like syntax validation,
type checking, and optimization. Understanding the inner workings of compilers is crucial for
computer science students to tackle programming challenges and develop better systems.

This document provides a detailed discussion on the fundamental techniques, data structures,
and software tools used in compiler construction. Topics covered include lexical analysis,
syntax analysis, semantic analysis, intermediate code generation, and essential data structures
like abstract syntax trees and symbol tables. Additionally, tools such as lexical analyzer and
parser generators are explored

1
Part I: Basic Techniques in Compiler Construction

1. Lexical Analysis
Lexical analysis is the process of breaking down the source code of the program into
smaller parts, called tokens, such that a computer can easily understand. These tokens can
be individual words or symbols in a sentence, such as keywords, variable names, numbers,
and punctuation.

Lexical analysis is the first phase of the compilation process, which involves scanning the
source code and breaking it into tokens. It ensures that the source code conforms to the
lexical rules of the language.
a. Breaking Input into Tokens and Handling Lexical Errors
Breaking Input into Tokens:
The source program is divided into meaningful sequences called tokens. A token can be a
keyword, identifier, operator, or punctuation. For example:
int x = 10;
Tokens: int, x, =, 10, ;
Lexical Errors:
Errors like illegal characters or incomplete tokens can occur.
Example:
Input: int x = 10$;
Error: $ is an invalid character.

2
Fix: Modify the lexer to recognize only valid characters and provide meaningful error
messages.
b. Components of Lexical Analysis
Regular Expressions: Define patterns for tokens.
Example: [a-zA-Z_][a-zA-Z0-9_]* for identifiers
Finite Automata: DFA (Deterministic Finite Automaton) is used for token recognition.
Example: A DFA can identify keywords like int or operators like +.

2. Syntax Analysis

A syntax analyzer or parser takes the input from a lexical analyzer in the form of token
streams. The parser analyzes the source code (token stream) against the production rules to
detect any errors in the code. The output of this phase is a parse tree.

This way, the parser accomplishes two tasks, i.e., parsing the code, looking for errors and
generating a parse tree as the output of the phase.

Syntax analysis is the second phase where a parser checks whether the token sequence
adheres to the grammatical structure of the programming language.

3
a. Parser Implementation
A parser uses the grammar rules of a language to validate the structure of code.
Example Grammar Rule:
E→E+T|T
T→T*F|F
F → (E) | id
Input: x = a + b * c
Output: Syntax is correct, as it adheres to the grammar.
b. Constructing Parse Trees and Parsing Techniques
Parse Tree: Represents the syntactic structure of the code.
Parsing Techniques: Top-down Parsing: Starts from the start symbol and derives the input
string (e.g., LL(1)).Bottom-up Parsing: Reduces the input string to the start symbol (e.g.,
LR(1)).Recursive Descent Parsing: A top-down method using recursive procedures for each
grammar rule.
c. Ambiguous Grammar
Ambiguity arises when a grammar produces more than one parse tree for the same string.
Example:
E → E + E | E * E | id
Solution: Rewrite the grammar to remove ambiguity. For example, define precedence and
associativity rules.

3. Semantic Analysis (Context-Sensitive Analysis)


Semantic analysis checks for context-sensitive constraints like type checking, scope rules,
etc.

Semantic analysis is the process of understanding the meaning and interpretation of words,
phrases, and sentences in a given context.

4
a. Type Checking and Type Mismatch Errors

Type Checking: Ensures operations are performed on compatible data types.


Example: Adding an integer and a string is invalid.

int x = "abc"; // Type mismatch error


Fix: Use type conversion or modify the code to correct types.

4. Intermediate Code Generation and Optimization


Intermediate code generation is a crucial phase in the compilation process where the source
program is translated into an intermediate representation. This intermediate code is machine-
independent, which enhances portability and simplifies the process of generating machine
code for different target machines.

Intermediate Code Generation: Converts the source program into an intermediate


representation like three-address code.
Example:
Input: a = b + c * d;
Intermediate Code:
t1 = c * d
t2 = b + t1
a = t2

5
Code Optimization: Improves the intermediate code for better performance without changing
its behaviour.

Example: Remove redundant computations or dead code.

Part II: Basic Data Structures in Compiler Construction


5. Abstract Syntax Trees (AST)
An Abstract Syntax Tree (AST) is a data structure used in computer science to represent the
structure of a program or code snippet. It is a tree representation of the abstract syntactic
structure of text (often source code) written in a formal language. Each node of the tree
denotes a construct occurring in the text1.
The syntax is "abstract" because it does not represent every detail appearing in the real syntax
but rather just the structural or content-related details. For instance, grouping parentheses are
implicit in the tree structure, so these do not have to be represented as separate nodes

AST is a simplified representation of the parse tree that eliminates unnecessary details.
Example:

6
6. Symbol Tables
A symbol table is a crucial data structure in compilers that stores information about
identifiers, including their names, types, scopes, and memory locations, to facilitate error
checking, code optimization, and efficient code generation during the compilation process.
Stores information about variables, functions, objects, etc., including their types, scopes, and
memory locations.

Example:

Example:2

7
For int x = 10;, the symbol table contains:

Name Type Scope Value

x int Global 10

7. Three-Address Code (TAC)

The three-address code is a sequence of statements of the form A−=B op C, where A, B, C


are either programmer-defined names, constants, or compiler-generated temporary names, the
op represents an operator that can be constant or floating point arithmetic operators or a
Boolean valued data or a logical operator. The reason for the name “three address code” is
that each statement generally includes three addresses, two for the operands, and one for the
result.

In the three-address code, at most three addresses are define any statement. Two addresses for
operand & one for the result.

Hence, op is an operator.

An only a single operation is allowed at a time at the right side of the expression.

Example− 1
Expression a = b + c + d can be converted into the following Three Address Code.

t1 = b + c

t2 = t1 + d

a = t2

8
where t1 and t2 are temporary variables generated by the compiler. Most of the time a
statement includes less than three references, but it is still known as a three address statement.

TAC represents statements as a sequence of simple instructions with at most three operands.
Example:2
Input: x = (a + b) * c
TAC:
t1 = a + b………………….//precedence rule are used
t2 = t1 * c
x = t2

8. Stack Machines
Stack Machine is a computational model like Turing Machine, Belt Machine and many
others. The central and most important component of a Stack Machine is a stack which is
used in place of registers to hold temporary variables.
Stack is a data structure with two basic operations:

 push (to add an element)


 pop (to remove an element)
Stack data structure maintains the order in which elements are inserted. Push operation adds
an element to the top of the list while pop operation removes an element from the top of the
list. It is, also, known as a Last In First Out (LIFO) data structure.

You must think of Stack as a storage device and is used in place of registers in case of Stack
Machines. Stacks are used to store temporary variables during computations in a Stack
Machine

Stack machines use a stack for intermediate calculations instead of registers.

9
Example:
Input: a + b * c
Stack Operations:
Push b
Push c
Multiply
Push a
Add

Part III: Basic Software Tools in Compiler Construction

9. Lexical Analyzer Generators


A lexical analyzer generator is a tool that allows many lexical analyzers to be created with a
simple build file. Such a build file would provide a list of declarations that provide the
generator the context it needs to develop a lexical analyzer.
Tools like Lex or Flex are used to generate lexical analyzers automatically.

10
Example: Using Lex, define token patterns in a .l file to generate a C program for token
recognition.

10. Parser Generators


A parser generator refers to a tool that can create a parser by encoding the contents of the
Action and Go to tables directly into the code. It replaces the table-driven skeleton parser
with a hard-coded implementation, where each state is represented by a small case statement
or a collection of if-then-else statements. This approach eliminates the need for a parse table
and table lookups, resulting in faster execution. However, the code size may increase due to
the inclusion of more code in each state.
Tools like Yacc or Bison generate parsers based on grammar rules.
Example:
Use Yacc to define grammar in a .y file, and it generates a parser in C.

11
Summary

Compiler design is a crucial area of study, providing insights into how programming
languages are processed and executed. This document explored the main phases of
compilation, including lexical, syntax, and semantic analysis, as well as intermediate code
generation and optimization. It also highlighted the importance of data structures like abstract
syntax trees and symbol tables in organizing and managing program information.

Furthermore, tools like Lex, Flex, Yacc, and Bison were discussed, demonstrating their role
in automating various compiler components. Mastery of these concepts equips computer
science students with the skills to build efficient, error-free software and understand the
complexities of language processing.

By applying these techniques and tools, developers can optimize performance, minimize
errors, and contribute to advancements in programming and software development.

12
References

 Aho, A. V., Lam, M. S., Sethi, R., & Ullman, J. D. (2006). Compilers: Principles,
Techniques, and Tools.

 Dragon Book

13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy