0% found this document useful (0 votes)
8 views23 pages

Lecture 3 - CSC 303

The document outlines the second lecture of a compiler construction course, focusing on syntax and semantic analysis. It explains the importance of syntax analysis in parsing source code and detecting errors, as well as the role of semantic analysis in ensuring code correctness and adherence to language semantics. Various parsing techniques, error handling strategies, and tools for syntactic and semantic analysis are also discussed.

Uploaded by

adamilola2019
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)
8 views23 pages

Lecture 3 - CSC 303

The document outlines the second lecture of a compiler construction course, focusing on syntax and semantic analysis. It explains the importance of syntax analysis in parsing source code and detecting errors, as well as the role of semantic analysis in ensuring code correctness and adherence to language semantics. Various parsing techniques, error handling strategies, and tools for syntactic and semantic analysis are also discussed.

Uploaded by

adamilola2019
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/ 23

COMPILER

CONSTRUCTIO
N
(CSC 303)
LECTURE 2

AGBEYANGI, A.O. (CSC) 1


Lecture Recap
Lecture 1: Introduction to Compilers

Lecture 2: Lexical Analysis

A.O. AGBEYANGI - CHRISLAND


2
UNIVERSITY
Today’s Lecture
Syntax Analysis
Semantic Analysis

A.O. AGBEYANGI - CHRISLAND


3
UNIVERSITY
Overview
Syntax analysis, also known as parsing, is a crucial
phase in the compilation process where the compiler
analyses the syntactic structure of the source code to
create a hierarchical structure called the parse tree.
This phase ensures the code adheres to the rules
defined by the programming language's grammar.
The syntactic structure of the source code refers to
the arrangement and organization of tokens by the
rules defined by the programming language's
grammar.

A.O. AGBEYANGI - CHRISLAND


4
UNIVERSITY
AGBEYANGI, A.O. (CSC) 5
Role of Syntax Analysis
Structure Identification
Syntax analysis identifies the structure of the source
code based on the formal grammar rules of the
programming language.

Error Detection:
It plays a vital role in detecting syntax errors in the
code, providing informative error messages to aid
developers in fixing issues.

A.O. AGBEYANGI - CHRISLAND


6
UNIVERSITY
Context-Free Grammars
(CFG)
Context-free grammars provide a formal and concise way
to describe the syntactic structure of a programming
language.

Production Rules
CFG consists of production rules that define how
different elements of the language can be combined
to form valid syntax.

A.O. AGBEYANGI - CHRISLAND


7
UNIVERSITY
Syntax Analysis: Parsing
Techniques
Parsing techniques are algorithms used in the syntax
analysis phase of a compiler to analyze the syntactic
structure of the source code based on the rules defined
by a context-free grammar (CFG).

These techniques determine whether the input string


(source code) conforms to the language syntax and, if so,
construct a parse tree or syntax tree representing the
hierarchical structure of the code.

A.O. AGBEYANGI - CHRISLAND


8
UNIVERSITY
Common Parsing
Techniques
 Top-Down Parsing (LL Parsing)
 Top-down parsing starts with the root of the parse tree and works
its way down to the leaves by recursively applying production rules
from the start symbol of the CFG. LL parsing is a common top-down
parsing technique used in compiler construction.
 Bottom-Up Parsing (LR Parsing)
 Bottom-up parsing builds the parse tree from leaves to the root. LR
parsers are more powerful and can handle a broader class of
grammars.
 Recursive Descent Parsing
 Recursive descent parsing is a top-down parsing technique where
each non-terminal in the grammar corresponds to a parsing
function.

A.O. AGBEYANGI - CHRISLAND


9
UNIVERSITY
Example
3 + 5 * (2 - 4)

A.O. AGBEYANGI - CHRISLAND


10
UNIVERSITY
Error Handling in Syntax
Analysis
Error handling in syntax analysis is a critical aspect of
compiler construction, ensuring that developers receive
meaningful feedback when their code contains syntax
errors.

It plays a vital role in enhancing the user experience by


providing clear error messages and aiding in the
debugging process.

A.O. AGBEYANGI - CHRISLAND


11
UNIVERSITY
Common Strategies for
Error Handling
 Panic Mode Recovery
 When a syntax error is encountered, the parser enters panic mode,
discarding tokens until it finds a synchronization point to resume
parsing.
 Example: In languages like C or Java, if a syntax error occurs within
a block of code, the parser may skip tokens until it finds a
semicolon or closing brace to synchronize and continue parsing.
 Phrase-Level Recovery
 Attempting to identify the smallest segment of code where the
error occurred and apply recovery strategies at that level.
 Example: If an error occurs within an expression, the parser may
try to recover by skipping to the next expression or statement
boundary.

A.O. AGBEYANGI - CHRISLAND


12
UNIVERSITY
Ambiguity in Grammars
 Ambiguity in grammar refers to a situation where a given string of
symbols can be parsed in more than one way, leading to multiple
possible parse trees or interpretations.
 This ambiguity can arise due to the presence of overlapping or
conflicting grammar rules.
 Causes of Ambiguity
 Overlapping Productions - When a grammar contains multiple
production rules that can derive the same string, ambiguity may
arise
 Left Recursion – It can lead to ambiguity because it's unclear
whether to associate the leftmost or rightmost expression first e.g.
Expr -> Expr + Expr
 Operator Precedence - when a grammar lacks explicit rules for
operator precedence and associativity
A.O. AGBEYANGI - CHRISLAND
13
UNIVERSITY
Ambiguity in Grammars
Note: Ambiguity in grammar poses challenges for
parsing and compiler design.
Understanding the causes and impacts of ambiguity is
crucial for developing effective disambiguation
techniques and ensuring the correctness and efficiency
of parsers.
By addressing ambiguity in grammar, compilers can
produce more reliable and predictable parsing results,
leading to improved software quality.

A.O. AGBEYANGI - CHRISLAND


14
UNIVERSITY
Syntactic Analysis Tools
 Syntactic analysis, also known as parsing, is a critical phase in the
compilation process where the source code is analyzed to
determine its syntactic structure based on a defined grammar.
 Several tools and frameworks facilitate syntactic analysis, offering
various features and capabilities to aid developers in building
parsers.
 Here are some commonly used syntactic analysis tools:
 ANTLR (ANother Tool for Language Recognition)
 Yacc (Yet Another Compiler Compiler)
 Bison
 PLY (Python Lex-Yacc)
 ANTLR4 and Antlr4py
A.O. AGBEYANGI - CHRISLAND
15
UNIVERSITY
Semantic Analysis

AGBEYANGI, A.O. (CSC) 16


Overview
Semantic analysis is a crucial phase in the compilation
process where the meaning of the source code is
analyzed to ensure its correctness and coherence with
respect to the language semantics.

Unlike syntactic analysis, which focuses on the structure


of the code, semantic analysis deals with the deeper
understanding of the code's intended behaviour and its
adherence to the rules of the programming language.

A.O. AGBEYANGI - CHRISLAND


17
UNIVERSITY
Key Tasks in Semantic
Analysis
 Type Checking: Verifies the compatibility and consistency of data types
used in expressions, assignments, function calls, etc.
 Scope Resolution: Determines the scope of variables and resolves
references to identifiers within the correct scope.
 Name Binding: Associates identifiers with their corresponding
declarations, ensuring that variables and functions are correctly
referenced.
 Constant Folding: Evaluates constant expressions at compile-time to
optimize performance.
 Control Flow Analysis: Analyzes the flow of control within the program to
detect unreachable code, loops, and conditional branches.
 Error Detection: Identifies semantic errors, such as type mismatches,
undeclared variables, or unreachable code, and reports them to the
developer.
A.O. AGBEYANGI - CHRISLAND
18
UNIVERSITY
Semantic Analysis
Techniques and Approaches
 Symbol Tables: Maintain information about identifiers, including
their names, types, scopes, and declarations, to aid in scope
resolution and name binding.
 Type Inference: Automatically deduces the types of expressions
and variables based on their usage context, reducing the need
for explicit type annotations.
 Constraint-Based Analysis: Formulates constraints based on
semantic rules and solves them to infer properties of the
program, such as variable lifetimes or data-flow dependencies.
 Data-flow Analysis: Tracks the flow of data and control
dependencies within the program to optimize memory usage
and identify potential security vulnerabilities.
A.O. AGBEYANGI - CHRISLAND
19
UNIVERSITY
Integration with Other
Phases
Semantic analysis is closely integrated with syntactic
analysis, where syntactic structures are annotated with
semantic information during parsing.

It precedes intermediate code generation and


optimization phases, providing a validated and
semantically correct representation of the source code
for further processing.

A.O. AGBEYANGI - CHRISLAND


20
UNIVERSITY
Tools and Frameworks
Various tools and frameworks, such as LLVM, GCC, and
Java Compiler (javac), implement semantic analysis as
part of the compilation process.

Language-specific tools, like TypeScript Compiler (tsc)


for TypeScript or Roslyn for C#, provide advanced
semantic analysis capabilities tailored to specific
languages.

A.O. AGBEYANGI - CHRISLAND


21
UNIVERSITY
Summary: Semantic
Analysis
Semantic analysis plays a critical role in ensuring the
correctness, reliability, and performance of compiled
code by analyzing its meaning and adherence to
language semantics.

By performing tasks such as type checking, scope


resolution, and error detection, semantic analysis
contributes to the overall quality and efficiency of the
compilation process.

A.O. AGBEYANGI - CHRISLAND


22
UNIVERSITY
Questions

AGBEYANGI, A.O. (CSC) 23

You might also like