Compiler Design Unit-2
Compiler Design Unit-2
Compiler Design
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, which we shall see in the next article
“Concepts of FIRST and FOLLOW sets in Compiler Design”.
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.
Disadvantages:
Types of Syntax:
Simple sentences.
Complex sentences.
Compound sentences.
Compound-Complex sentences
context free grammar in compiler design:- A context-
free grammar is a set of recursive rules used to generate patterns of
strings. A context-free grammar can describe all regular
languages and more, but they cannot describe all possible languages.
Context-free grammars are studied in fields of theoretical computer
science, compiler design, and linguistics.
Context Free Grammar is formal grammar, the syntax or structure of a formal language
can be described using context-free grammar (CFG), a type of formal grammar. The
grammar has four tuples: (V,T,P,S).
V - It is the collection of variables or nonterminal symbols.
T - It is a set of terminals.
P - It is the production rules that consist of both terminals
and nonterminals.
S - It is the Starting symbol.
And the left-hand side of the G, here in the example can only be a Variable, it cannot
be a terminal.
But on the right-hand side here it can be a Variable or Terminal or both combination
of Variable and Terminal.
Above equation states that every production which contains any combination of the ‘V’
variable or ‘T’ terminal is said to be a context-free grammar.
For example the grammar A = { S, a,b, P,S} having production :
Here S is the starting symbol.
{a,b} are the terminals generally represented by small characters.
P is variable along with S.
S-> aS7-
S-> bSa
but
a->bSa, or
a->ba is not a CFG as on the left-hand side there is a variable
which does not follow the CFGs rule.
In the computer science field, context-free grammars are frequently used, especially in
the areas of formal language theory, compiler development, and natural language
processing. It is also used for explaining the syntax of programming languages and other
formal languages.
Limitations of Context-Free Grammar
Apart from all the uses and importance of Context-Free Grammar in the Compiler design
and the Computer science field, there are some limitations that are addressed, that is
CFGs are less expressive, and neither English nor programming language can be
expressed using Context-Free Grammar. Context-Free Grammar can be ambiguous
means we can generate multiple parse trees of the same input. For some grammar,
Context-Free Grammar can be less efficient because of the exponential time complexity.
And the less precise error reporting as CFGs error reporting system is not that precise that
can give more detailed error messages and information.
Type Name
0 Unrestricted Grammar
3 Regular Grammar
1. Context Free Grammar :
Language generated by Context Free Grammar is accepted by Pushdown
Automata
It is a subset of Type 0 and Type 1 grammar and a superset of Type 3
grammar.
Also called phase structured grammar.
Different context-free grammars can generate the same context-free
language.
Classification of Context Free Grammar is done on the basis of the number
of parse trees.
Only one parse tree->Unambiguous.
More than one parse tree->Ambiguous.
Example –
S –> AB
A –> a
B –> b
2. Regular Grammar :
It is accepted by Finite State Automata.
It is a subset of Type 0 ,Type 1 and Type 2 grammar.
The language it generates is called Regular Language.
Regular languages are closed under operations like Union, Intersection,
Complement etc.
They are the most restricted form of grammar.
Productions are in the form –
V –> VT / T (left-linear grammar)
(or)
V –> TV /T (right-linear grammar)
Example –
1. S –> ab.
2. S -> aS | bS | ∊
Restriction Less than Regular Grammar More than any other grammar