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

Syntax analysis in CC[1]

Uploaded by

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

Syntax analysis in CC[1]

Uploaded by

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

Syntax analysis in CC

By
Attia Agha
Overview

Phases of CC
 Lexical Analysis: The first phase of a compiler is lexical analysis, also known as scanning. This
phase reads the source code and breaks it into a stream of tokens, which are the basic
units of the programming language. The tokens are then passed on to the next phase for
further processing.
 Syntax Analysis: The second phase of a compiler is syntax analysis, also known as parsing.
This phase takes the stream of tokens generated by the lexical analysis phase and checks
whether they conform to the grammar of the programming language. The output of this
phase is usually an Abstract Syntax Tree (AST).
 Semantic Analysis: The third phase of a compiler is semantic analysis. This phase checks
whether the code is semantically correct, i.e., whether it conforms to the language’s type
system and other semantic rules. In this stage, the compiler checks the meaning of the
source code to ensure that it makes sense. The compiler performs type checking, which
ensures that variables are used correctly and that operations are performed on
compatible data types. The compiler also checks for other semantic errors, such as
undeclared variables and incorrect function calls.
CONT.

 Intermediate Code Generation: The fourth phase of a compiler is


intermediate code generation. This phase generates an intermediate
representation of the source code that can be easily translated into
machine code.
 Optimization: The fifth phase of a compiler is optimization. This phase
applies various optimization techniques to the intermediate code to
improve the performance of the generated machine code.
 Code Generation: The final phase of a compiler is code generation. This
phase takes the optimized intermediate code and generates the actual
machine code that can be executed by the target hardware
Introduction

 When an input string (source code or a program in some language)


is given to a compiler, the compiler processes it in several phases,
starting from
lexical analysis (scans the input and divides it into tokens) to target
code generation
 Syntax Analysis or Parsing is the second phase, i.e. after lexical
analysis.
 It checks the syntactical structure of the given input, i.e. whether
the given input is in the correct syntax (of the language in which the
input has been written) or not. It does so by building a data
structure, called a Parse tree or Syntax tree.
Cont.

 The parse tree is constructed by using the pre-defined Grammar of the


language and the input string. If the given input string can be produced
with the help of the syntax tree (in the derivation process), the input string is
found to be in the correct syntax. if not, the error is reported by the syntax
analyzer.
 Syntax analysis, also known as parsing, is a process in compiler design
where the compiler checks if the source code follows the grammatical
rules of the programming language. This is typically the second stage of the
compilation process, following lexical analysis
 The main goal of syntax analysis is to create a parse tree or abstract syntax
tree (AST) of the source code, which is a hierarchical representation of the
source code that reflects the grammatical structure of the program
Syntax Analyzer

 It is sometimes called a parser. It


constructs the parse tree. It takes all
the tokens one by one and uses
Context-Free Grammar to construct
the parse tree
There are several types of parsing algorithms used in syntax analysis, including:
 LL parsing: This is a top-down parsing algorithm that starts with the root of
the parse tree and constructs the tree by successively expanding non-
terminals. LL parsing is known for its simplicity and ease of implementation.
 LR parsing: This is a bottom-up parsing algorithm that starts with the leaves
of the parse tree and constructs the tree by successively reducing
terminals. LR parsing is more powerful than LL parsing and can handle a
larger class of grammars.
 LR(1) parsing: This is a variant of LR parsing that uses lookahead to
disambiguate the grammar.
 LALR parsing: This is a variant of LR parsing that uses a reduced set of
lookahead symbols to reduce the number of states in the LR parser.
 Once the parse tree is constructed, the compiler can perform semantic
analysis to check if the source code makes sense and follows the semantics
of the programming language.
 The parse tree or AST can also be used in the code generation phase of the
compiler design to generate intermediate code or machine code.
Features of syntax analysis

 Syntax Trees: Syntax analysis creates a syntax tree, which is a hierarchical


representation of the code’s structure. The tree shows the relationship
between the various parts of the code, including statements, expressions,
and operators.
 Context-Free Grammar: Syntax analysis uses context-free grammar to
define the syntax of the programming language. Context-free grammar is
a formal language used to describe the structure of programming
languages.
 Top-Down and Bottom-Up Parsing: Syntax analysis can be performed using
two main approaches: top-down parsing and bottom-up parsing. Top-
down parsing starts from the highest level of the syntax tree and works its
way down, while bottom-up parsing starts from the lowest level and works
its way up.
Feature cont.

 Error Detection: Syntax analysis is responsible for detecting syntax errors in


the code. If the code does not conform to the rules of the programming
language, the parser will report an error and halt the compilation process.
 Intermediate Code Generation: Syntax analysis generates an intermediate
representation of the code, which is used by the subsequent phases of the
compiler. The intermediate representation is usually a more abstract form of
the code, which is easier to work with than the original source code.
 Optimization: Syntax analysis can perform basic optimizations on the code,
such as removing redundant code and simplifying expressions.
 The pushdown automata (PDA) is used to design the syntax analysis phase.
 The Grammar for a Language consists of Production rules.
 Example: Suppose Production rules for the Grammar of a language are:

 Now the parser attempts to construct a syntax tree from this grammar for the
given input string. It uses the given production rules and applies those as
needed to generate the string. To generate string “cad” it uses the rules as
shown in the given diagram:
 In step (iii) above, the production rule
A->bc was not a suitable one to apply
(because the string produced is
“cbcd” not “cad”), here the parser
needs to backtrack, and apply the
next production rule available with A
which is shown in step (iv), and the
string “cad” is produced.
 Thus, the given input can be produced
by the given grammar, therefore the
input is correct in syntax. But backtrack
was needed to get the correct syntax
tree, which is really a complex process
to implement.
 There can be an easier way to solve
this,“Concepts of FIRST and FOLLOW
sets in CC”.
Advantages

 Advantages of using syntax analysis in compiler design include:


 Structural validation: Syntax analysis allows the compiler to check if the
source code follows the grammatical rules of the programming language,
which helps to detect and report errors in the source code.
 Improved code generation: Syntax analysis can generate a parse tree or
abstract syntax tree (AST) of the source code, which can be used in the
code generation phase of the compiler design to generate more efficient
and optimized code.
 Easier semantic analysis: Once the parse tree or AST is constructed, the
compiler can perform semantic analysis more easily, as it can rely on the
structural information provided by the parse tree or AST
Syntax analysis

 Syntax analysis, also known as parsing, is a crucial stage in the process of compiling a
program. Its primary task is to analyze the structure of the input program and check
whether it conforms to the grammar rules of the programming language. This process
involves breaking down the input program into a series of tokens and then constructing a
parse tree or abstract syntax tree (AST) that represents the hierarchical structure of the
program.
The syntax analysis phase typically involves the following steps:
 Tokenization: The input program is divided into a sequence of tokens, which are basic
building blocks of the programming language, such as identifiers, keywords, operators,
and literals.
 Parsing: The tokens are analyzed according to the grammar rules of the programming
language, and a parse tree or AST is constructed that represents the hierarchical structure
of the program.
 Error handling: If the input program contains syntax errors, the syntax analyzer detects and
reports them to the user, along with an indication of where the error occurred.
Cont.

 Symbol table creation: The syntax analyzer creates a symbol table, which is
a data structure that stores information about the identifiers used in the
program, such as their type, scope, and location.
 The syntax analysis phase is essential for the subsequent stages of the
compiler, such as semantic analysis, code generation, and optimization. If
the syntax analysis is not performed correctly, the compiler may generate
incorrect code or fail to compile the program altogether.
 Overall, syntax analysis is a critical stage in the process of compiling a
program, as it ensures that the program is syntactically correct and ready
for further processing by the compiler.

You might also like