2. 003
SYNTAX ANALYSIS(PARSERING)
Syntax analysis is the second phase of the compiler
It construct the Parse Tree for checking.
A syntax analyzer or parser takes the input from a lexical analyzer in the
form of token streams
It checks whether the given input is in the correct syntax of the
programming language or not
The Parse Tree is developed with the help of context free grammar.
It helps to detect all types of Syntax errors.
3. 003
SYNTAX ANALYSIS(PARSING)
The parser must reject invalid texts by reporting syntax errors. the error
could happen in terms of expressions, statements, declarations, nested.
look at the below example
x = (2+3) ∗ 9); Mismatched Parentheses
if x>y x = 2; Missing Parentheses
while (x==3) do f1(); Invalid Keyword Do
Context-Free Grammar is a powerful tool for describing the syntax of
programming languages.
4. 004
SYNTAX ANALYSIS(PARSERING)...
A context-free grammar has four components:
Set of Non-terminals (V).
Set of Terminal symbols (Σ).
Set of Productions (P).
Start symbol (S)
Non-terminals (V): Non-terminals are set of syntactic variables that
denote sets of strings. represented with capital letters V = { A,B,S.. Z }
Grammar G=(N,T,P,S)
Terminal symbols (Σ): are set of tokens represented with small letters like
a, b, c etc. it is a basic symbols from which strings are formed
Σ = { 0, 1 } or Σ = { a, b.. z }
Productions (P): The productions of a grammar specify in which the terminals and
non-terminals can be combined to form strings.
Ex : S → AA, A → aA | b
Start symbol (S) :One of the non-terminals is designated as the start symbol
5. 005
PARSER
parser is a program that generate a parser tree for the given strings, if
the string generates from the underlying grammar
x = a+b*c;
if token string x ∈L(G), then parse tree
else error message
6. 006
PARSER TREE
A parse tree is a graphical representation of a derivation.
It is convenient to see how strings are derived from the start symbol.
The start symbol of the derivation becomes the root of the parse tree.
7. 007
DERIVATION
A derivation is basically a sequence of production rules, to get the
input string.
To decide which non-terminal to be replaced with production rule,
we can have two options.
1.Left-most Derivation: when the sentential form of an input is scanned and replaced
from left to right
2.Right-most Derivation: sentential form derived by the left-most derivation is called
the left-sentential form
During parse tree construction , we take two decisions for some
sentential form of input:
1.Deciding the non-terminal which is to be replaced.
2.Deciding the production rule, by which, the non-
terminal will be replaced.
8. S -> ID
E-> T|T
T-> T * F|F
F->ID
008
GENERATION OF PARSE TREE
eg: (S -> aABe, A-> Abc | a, B -> d ) aabcde
The generation of parser tree can be done in to two ways
eg: ((S,A), (a,b), (S->aAb, A->aAb | E)) a*n b*n
9. 009
SOME EXAMPLES OF CFG
Construct the grammer G=({S,A,B},{a,b},P,S} where the production
set p is given by, S->abSb | aAb | a A->aAAb | bS
Prove that G is a ambiguous grammar for input string abab?
Consider the grammar G=({s},{x,y,+,*},P,S) where P consists of S->S+S|S*S|x|y is
it ambiguity grammar for context-free grammar(x+x*y)
Consider the grammar G=({E},{a,b},P,S) where P consists of E->E+E|a
show that the grammar is ambiguous w=a+a+a
Derive the string "aabbabba" using leftmost derivation, rightmost
derivation and parse tree the following CFG,
S-> aB | bA
A-> a| aS | bAA
B-> b | bS |aBB
10. no
Regular Expressions Context-free grammar
1
Lexical rules are quite simple in case of Regular
Expressions.
Lexical rules are difficult in case of Context free grammar.
2
Regular Expressions are most useful for describing
the structure of lexical construct such as
identifiers, constant etc.
Context free grammars are most useful in describing the nested
chain structure or syntactic structure such as balanced
parenthesis, if else etc.
and these can’t be define by Regular Expression.
3
A set of string is defined in case of Regular
Expressions.
In Context free grammar the language is defined by the
collection of productions.
It is easy to construct efficient recognizer from
Regular Expressions.
By using the context free grammar, it is very difficult to
construct the recognizer.
There is proper procedure for lexical and
syntactical analysis in case of Regular Expressions.
There is no specific guideline for lexical and syntactic analysis
in case of Context free grammar.
010
REGULAR EXPRESSION VS CONTEXT FREE GRAMMAR
11. 011
SYNTAX DIRECTED DEFINITIONS (SDD)
EVALUATION ORDER FOR SDD
CONSTRUCTION OF SYNTAX TREES
CHAPTER-4 LISTS
SYNTAX DIRECTED TRANSLATION
12. 012
4.1 SYNTAX DIRECTED DEFINITIONS (SDD)
Syntax Directed Translation is a technique used in compiler design and
programming language theory to associate semantic actions with the production
rules of a context-free grammar.
It combines the specification of a grammar with the execution of translation or
computation tasks during the parsing process.
Grammar + Semantic rule = SDT
This can be a separate phase of a compiler or we can augment our conventional
grammar with information to control the semantic analysis and translation.
Such grammars are called attribute grammars
In other words, the parsing process and parse trees are used to direct semantic
analysis and the translation of the source program
it checks whether the program's statements and
expressions make sense in the context of the
programming language. It goes beyond the syntax
(grammar) of the code and focuses on the intended
meaning and behavior of the program.
13. 013
4.2 EVALUATION ORDER FOR SDD
The general approach to Syntax-Directed Translation is to construct a parse
tree or syntax tree and compute the values of attributes at the nodes of the
tree by visiting them in some order
E -> E+T | T
T -> T*F | F
F -> INTLIT
E -> E+T { E.val = E.val + T.val }
E -> T { E.val = T.val }
T -> T*F { T.val = T.val * F.val }
T -> F { T.val = F.val }
F -> INTLIT { F.val = INTLIT.lexval }
This is a grammar to syntactically
validate an expression having
additions and multiplications in it
Generalizing, SDT are augmented
rules to a CFG that associate
1) set of attributes to every node of
the grammar and
2) a set of translation rules to every
production rule using attributes,
constants, and lexical values.
https://www.geeksforgeeks.org/syntax-directed-translation-in-compiler-design/
Along with the grammar, we are giving semantic actions also such along with
the parsing task, some other tasks can also be done in parallel like code
generation, expression evaluation(EE) and etc...
14. 014
4.2 EVALUATION ORDER FOR SDD...
Let’s take a string to see how semantic analysis happens, S = 2+3*4. Parse tree
corresponding to S would be
Rules we should follow:
1. generate parse tree from the grammar
2. parse the tree: left to right and top to bottom
3. whenever there is a reduction, go to the production and carry out the action
15. 015
TYPES OF ATTRIBUTES
Synthesized attributes: Synthesized attributes are computed and assigned values
at a parent node based on the attributes of its child nodes. The value of a
synthesized attribute is derived from the values of attributes in the child nodes
and any additional computations performed at the parent node
They are useful for propagating information from child nodes to their parents.
Inherited attributes: Inherited attributes are passed from parent nodes to their
child nodes. The value of an inherited attribute at a child node depends on the
attribute's value at the parent node and any modifications made at the child
node
they are useful for passing information from parents to their children.
16. 016
TYPES OF SDD
L-Attributed SDD: L-Attributed SDDs are a subset of Attribute Grammars where
the attributes can be evaluated in a single left-to-right traversal of the parse tree.
In other words, the attributes in an L-Attributed SDD can be evaluated using only
information from the current node and its left siblings..
S-Attributed SDD: S-Attributed SDDs are a type of Attribute Grammar where
attributes can be evaluated in a bottom-up fashion during a depth-first traversal
of the parse tree.
S-Attributed SDDs allow for more flexible attribute dependencies, as attributes
can depend on siblings and ancestors.