Unit - 3 Syntax Analyzer
Unit - 3 Syntax Analyzer
Introduction to Syntax
Analyzer(Parser)
By design, every programming language has precise rules that
prescribe the syntactic structure of well-formed programs.
In C, for example, a program is made up of functions, a
function out of declarations and statements, a statement out of
expressions, and so on.
Role of Parser
In our compiler model, the parser obtains a string of tokens
from the lexical analyzer, and verifies that the string of token
names can be generated by the grammar for the source
language.
Role of Parser
Conceptually, for well-formed programs, the parser constructs
a parse tree and passes it to the rest of the compiler for further
processing.
There are three general types of parsers for grammars:
1. Top-down Parser
2. Bottom-up Parser
Grammer
Grammer is nothing but collections of rules and it contains
terminal and non-terminal symbol.
G=(N,⅀,S,P)
For example,
Every sentence must be terminated with ;
S->S;
Grammer is divided into 2 parts:
1. Restricted Grammer
2. Un-Restricted Grammer
Grammer
Grammer is nothing but collections of rules and it contains
terminal and non-terminal symbol.
1. Terminal Symbols : Terminal symbols are those which are the
components of the sentences generated using a grammar and
are represented using small case letter like a-z ,digits etc.
2. Non-Terminal Symbols :Non-Terminal Symbols are those
symbols which take part in the generation of the sentence but
are not the component of the sentence. Non-Terminal Symbols
are also called Auxiliary Symbols and Variables. These
symbols are represented using a capital letter like A, B, C, etc.
Syntax Tree and Parse Tree
Syntax Tree and Parse Tree
Derivation
The construction of a parse tree can be made by
1. Derivation
2. Reduction
Derivation
Derivation means to replace a non terminal by the body of
its production.
Derivation is used to find whether the string belongs to a
given grammar or not.
Two types of Derivation
1. Left most (Left most non terminal is replaced)
2. Right most (Right most non terminal is
replaced)
Left most Derivation
Right most Derivation
Reduction
Reduction means conversion of terminal to non terminal.
Two types of Reduction
1. Left most (Left most terminal is replaced)
2. Right most (Right most terminal is replaced)
Ambiguity
A grammar that produces more than one parse tree for
some sentence is said to be ambiguous.
S–>AA
A–>aA|b
SLR Parser
STEP1: Find augmented grammar
The augmented grammar of the given grammar is:-
FOLLOW(S)=$
FOLLOW(A)=a,b,$
SLR Parser