Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Compiler Design  >  Context-Free Grammars: Definition & Parsing

Context-Free Grammars: Definition & Parsing | Compiler Design - Computer Science Engineering (CSE) PDF Download

1. Context-free Grammars: 
Definition: Formally, a context-free grammar G is a 4-tuple G = (V, T, P, S), where:
1. V is a finite set of variables (or nonterminals). These describe sets of “related” strings.
2. T is a finite set of terminals (i.e., tokens).
3. P is a finite set of productions, each of the form
A   where A  V is a variable, and   (V  T)* is a sequence of terminals and nonterminals. S  V is the start symbol.

Example of CFG: E ==>EAE | (E) | -E | id A==> + | - | * | / |

Where E, A are the non-terminals while id, +, *, -, /,(, ) are the terminals. 2. Syntax analysis:

In syntax analysis phase the source program is analyzed to check whether if conforms to the source language’s syntax, and to determine its phase structure. This phase is often separated into two phases:

  • Lexical analysis: which produces a stream of tokens?
  • Parser: which determines the phrase structure of the program based on the context-free grammar for the language?

2.1 PARSING: Parsing is the activity of checking whether a string of symbols is in the language of some grammar, where this string is usually the stream of tokens produced by the lexical analyzer. If the string is in the grammar, we want a parse tree, and if it is not, we hope for some kind of error message explaining why not.

There are two main kinds of parsers in use, named for the way they build the parse trees:

  • Top-down: A top-down parser attempts to construct a tree from the root, applying productions forward to expand non-terminals into strings of symbols.
  • Bottom-up: A Bottom-up parser builds the tree starting with the leaves, using productions in reverse to identify strings of symbols that can be grouped together. 
    In both cases the construction of derivation is directed by scanning the input sequence from left to right, one symbol at a time.

Parse Tree:
 

                                              Context-Free Grammars: Definition & Parsing | Compiler Design - Computer Science Engineering (CSE)
 

A parse tree is the graphical representation of the structure of a sentence according to its grammar.
 

Example:
Let the production P is:
E T | E+T
T F | T*F
F V | (E)
V a | b | c |d

The parse tree may be viewed as a representation for a derivation that filters out the choice regarding the order of replacement.

Parse tree for a * b + c

 

                                                                                   Context-Free Grammars: Definition & Parsing | Compiler Design - Computer Science Engineering (CSE)
Parse tree for a + b * c is:
 

 

                                                                                       Context-Free Grammars: Definition & Parsing | Compiler Design - Computer Science Engineering (CSE)
 

Parse tree for (a * b) * (c + d)
 

 

                                                                                           Context-Free Grammars: Definition & Parsing | Compiler Design - Computer Science Engineering (CSE)

The document Context-Free Grammars: Definition & Parsing | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Context-Free Grammars: Definition & Parsing - Compiler Design - Computer Science Engineering (CSE)

1. What is a context-free grammar?
A context-free grammar is a formalism used in computer science and linguistics to describe the syntax of a language. It consists of a set of production rules that define how valid strings of symbols can be formed in the language. Each production rule consists of a non-terminal symbol on the left-hand side and a sequence of symbols (both terminals and non-terminals) on the right-hand side.
2. What is parsing in computer science?
Parsing refers to the process of analyzing a given sequence of symbols according to the rules of a formal grammar. In computer science, parsing is commonly used in programming languages, compilers, and natural language processing. It involves breaking down the input sequence into a hierarchical structure, such as a parse tree or an abstract syntax tree, based on the grammar rules.
3. How does parsing relate to context-free grammars?
Parsing is closely related to context-free grammars because context-free grammars are often used to define the syntax rules of a language. During parsing, the input sequence is checked against the production rules of the context-free grammar to determine if it is syntactically valid. If the input sequence can be derived using the grammar rules, it is considered to be in the language defined by the grammar.
4. What are some applications of context-free grammars and parsing?
Context-free grammars and parsing have various applications in computer science. They are used in programming language compilers to analyze and understand the structure of source code. Parsing is also used in natural language processing systems to parse and understand human languages. Additionally, context-free grammars and parsing are used in syntax analysis, code generation, and optimization phases of compiler design.
5. Can a context-free grammar describe all possible languages?
No, a context-free grammar cannot describe all possible languages. Context-free grammars can only describe languages that can be generated by a set of production rules that have a single non-terminal symbol on the left-hand side. Languages that require more complex rules or context-sensitive rules cannot be described by context-free grammars.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Sample Paper

,

Context-Free Grammars: Definition & Parsing | Compiler Design - Computer Science Engineering (CSE)

,

Important questions

,

study material

,

past year papers

,

Context-Free Grammars: Definition & Parsing | Compiler Design - Computer Science Engineering (CSE)

,

Context-Free Grammars: Definition & Parsing | Compiler Design - Computer Science Engineering (CSE)

,

Exam

,

practice quizzes

,

Previous Year Questions with Solutions

,

shortcuts and tricks

,

MCQs

,

ppt

,

Free

,

Semester Notes

,

Extra Questions

,

Summary

,

pdf

,

video lectures

,

mock tests for examination

,

Viva Questions

,

Objective type Questions

;