0% found this document useful (0 votes)
11 views15 pages

Syntax Analysis: Presentation by

Uploaded by

Nirbhay verma
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)
11 views15 pages

Syntax Analysis: Presentation by

Uploaded by

Nirbhay verma
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/ 15

Presentation by

Soumyadeep Das

Syntax

A n a ly s is
Roll No:
13000121136
Reg No:
211300100110125
Compiler Design (PCC-
CS501)
Page 02 of
15

Conten
t
1. Introduction 0
7
Lexical
Analysis vs.
2. S yntax Tree
Syntax
C onstruction 0
8 Analysis
3. Top Down Parser Common
Types of
4.
0 Bottom
Syntax up Parser Syntax Trees
5 Tree
09 Conclusion
0 Example
6 10 Reference
Page 03 of
15

Introduction
Syntax analysis, also known as parsing, is a crucial phase in the
process of compiling a programming language. It plays a
fundamental role in ensuring that a given sequence of source
code follows the correct grammar specified by the language's
syntax rules. In other words, syntax analysis checks the
syntactical correctness of the source code and builds the
foundation for subsequent phases of the compiler.

Compiler Computer 202


Design Science 3
S y n t a x Tr e e
Construction:

}
To p D o w n P a r s e r :
LL(1) Parsing:
Here the 1st L represents that the scanning of the Input will be done from the Left to Right
manner and the second L shows that in this parsing technique, we are going to use the Left
most Derivation Tree.
Essential conditions to check fi rst are as follows:
1.The grammar is free from left recursion.
2.The grammar should not be ambiguous.
3.The grammar has to be left factored in so that the grammar is deterministic grammar.
Recursive Descent Parser:
It is a kind of Top-Down Parser. A top-down parser builds the parse tree from the top to down,
starting with the start non-terminal. A Predictive Parser is a special case of Recursive
Descent Parser, where no Back Tracking is required.
Example:
Before removing left After removing left
recursion E – > E + T | T recursion E – > T E’
T–>T* F|F E’ – > + T E’
F – > ( E ) | id |e T–>F
T’
T’ – > * F T’
Bottom Up
Pa r s e r:
LR Parser
The LR parser is a non-recursive, shift-reduce, bottom-up parser. It uses a wide class of
context-free grammar which makes it the most effi cient syntax analysis technique. LR
parsers are also known as LR(k) parsers, where L stands for left-to-right scanning of the
input stream; R stands for the construction of right-most derivation in reverse, and k
denotes the number of lookahead symbols to make decisions.
There are three widely used algorithms available for constructing an LR parser:

SLR(1) – Simple LR Parser:


Works on smallest class of grammar
Few number of states, hence very small
table Simple and fast construction
LR(1) – LR Parser:
Works on complete set of LR(1) Grammar
Generates large table and large number of
states Slow construction
LALR(1) – Look-Ahead LR Parser:
Works on intermediate size of
Page 07 of
What is a Syntax 15
Tree?
A syntax tree is a visual representation of the syntax structure of the source code. It breaks down the code into
separate parts, called nodes, and shows how they are related to each other.

Lexical Analysis (Tokenization):


0 The compiler starts with the lexical analysis phase, where it reads the source code and breaks it down into a sequence of
tokens. Each token represents a fundamental building block of the language, such as keywords, identifiers, constants, and
1 operators.

Syntax Analysis (Parsing):


0 Once the lexical analysis is complete, the compiler moves to the syntax analysis phase.
The primary task of this phase is to check whether the sequence of tokens follows the rules defined in the
2 language's CFG. The CFG defines the allowable structures and patterns for constructing valid statements
and expressions in the language.

0 Check Grammar Specification:


The compiler uses the language's formal context-free grammar (CFG) to specify the syntax rules.

3
The CFG consists of a set of production rules that define how valid statements and expressions can be formed using the
language's tokens.
a. Top-Down Parsing:
One common approach to syntax analysis is top-down parsing, where the compiler starts with the highest-level rule
of the CFG (the start symbol) and recursively expands it to build a parse tree or syntax tree.
The parsing process proceeds from the root to the leaves of the parse tree, attempting to match the input
sequence with the CFG rules.

Presentation by Soumyadeep Das


Page 08 of
15
b. Bottom-Up Parsing :
Another approach is bottom-up parsing, where the compiler starts with the individual tokens and tries to construct
the parse tree by reducing the tokens to higher-level language constructs.
This technique is commonly used in LR parsers (e.g., LALR, SLR), which are more powerful and efficient than some
top-down parsers.

Error Handling :
0 During syntax analysis, if the compiler encounters any syntax errors in the source code, it generates appropriate error
messages to help the programmer identify and correct the issues.
4 Error recovery mechanisms may be employed to allow the compiler to continue parsing the code and report multiple
errors in a single compilation pass.

Abstract S yntax Tree (AS T):


0 Some compilers generate an Abstract Syntax Tree (AST) during the syntax analysis phase.
The AST represents the essential semantic structure of the source code while abstracting away irrelevant details of
5 the parse tree. The AST becomes the foundation for subsequent phases like semantic analysis, optimization, and
code generation.

Presentation by
Soumyadeep Das
EX A MP L
E
Parse tree and Syntax tree
E-> E+T | T
T-> T*F |F |
id F-> id
In the parse tree, most of the leaf nodes are single
child to their parent nodes.
In the syntax tree, we can eliminate this extra
information. Synt ax t ree is a
variant of parse
t ree. In
t he synt ax
t ree, interior nodes are operators and leaves are
operands.
Syntax tree is usually used when represent a program
in a tree structure.
A sentence id + id * id would have
the following syntax tree:

Presentation by Soumyadeep TMSL |


Das 2023
EX A MP L
E
Abstract syntax tree can
be represented as:
Abstract syntax trees are important
data structures in a compiler. It
contains the least unnecessary
information.

Abstract syntax trees are more compact


than a parse tree and can be easily used
by a compiler.

Presentation by Soumyadeep TMSL |


Das 2023
LEXICAL ANALYSIS VS. Page 1
SYNTAX ANALYSIS 1

LEXICAL ANALYSIS SYNTAX ANALYSIS


Syntax analysis checks the
Lexical analysis focuses on
grammar of the source code by
analyzing the individual tokens of
making sure each token follows
the source code, such as
the specified syntax rules of the
keywords, constants, and
programming language.
identifiers.
.
Implemented using techniques
Typically implemented using
like top-down parsing or bottom-
techniques like regular expressions
up parsing to construct a parse
and finite automata to recognize
tree or syntax tree based on the
tokens in the source code
language's grammar rules.
efficiently.
Common T yp es of Synta x
T rees
Abstr act Syntax C oncr ete Syntax
Tr ee Tr ee
An AST is a t ype of synt ax t ree t hat
The CST includes all t he det ails of t he
removes unimport ant inf ormat ion, suCh as
sourCe code, suCh as whit espaCe,
semiColons or parent heses, t o represent t he
parent heses, and semiColons.
essent ial st ruCt ure of t he code.

Parse Tree:
A parse t ree is a more general t erm t hat includes bot h ASTs and CSTs. It represent s t he
hierarChiCal st ruCt ure of t he sourCe code.

Present at ion by Soumyadeep TMSL |


Das 20 23
Pag 13 of
e 15

Conclusion
Synt ax analysis is a Crit iCal phase in t he
compilat ion proCess, responsible f or ensuring
t hat t he sourCe code f ollows t he rules specif ied
by t he programming language's Cont ext - f ree
grammar (CFG). This phase is CruCial because it
est ablishes t he synt aCt iC Correct ness of t he
program, paving t he way f or subsequent
Compiler st ages like semant iC analysis,
opt imizat ion, and Code generat ion.

TMSL | 20 23
Pag 14of
e 15

Re f e re n c e
https:// s
www.tutorialspoint.com/compiler_design/compiler_design_bottom_up_parser.htm
https://www.javatpoint.com/parse-tree-and-syntax-tree https://
www.geeksforgeeks.org/bottom-up-or-shift-reduce-parsers-set-2/

TMSL | Computer Science|


2023
Presentation by SOUMYADEEP TMSL |
DAS 20 23

T ha nk
You!

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