Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Compiler Design: Conversion of NFA to DFA, Minimization of DFA, and Syntax Analysis, Lecture notes of Compiler Design

Lecture notes from the Compiler Design course at GIET University, covering the topics of converting NFA to DFA, minimizing DFA, and syntax analysis. The notes include explanations of the concepts, examples, and exercises for students to practice.

Typology: Lecture notes

2020/2021

Uploaded on 10/03/2021

subroto-mukherjee
subroto-mukherjee ๐Ÿ‡ฎ๐Ÿ‡ณ

2 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMPILER DESIGN
(BCSPC5010)
5th Semester CSE (R-18)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Compiler Design: Conversion of NFA to DFA, Minimization of DFA, and Syntax Analysis and more Lecture notes Compiler Design in PDF only on Docsity!

COMPILER DESIGN

(BCSPC5010)

5 th^ Semester CSE (R-18)

Conversion of NFA to DFA

Lecture โ€“ 8 LEXICAL ANALYSIS

Start/Input a b

S0 S1 S

S1 S1 S

S2 S1 S

S3 S1 S

S4 S1 S

Syntax Analysis

Lecture โ€“ 9 Syntax Analysis

Overview of the previous Lecture

Syntax Analysis

Context Free Grammar

Derivation

Syntax Analysis

Source

Code

Lexical

Analyzer

(Scanner)

Syntax

Analyzer

(Parser)

Toke

n

Request for

next Token

Lexemes

Symbol Table

Error Handler

Semantic

Analyzer

Parse

Tree

Syntax Analysis

Lecture โ€“ 9 Syntax Analysis

Syntax analysis or parsing is the second phase of a compiler. In this chapter, we shall learn the basic concepts used in the construction of a parser. We have seen that a lexical analyzer can identify tokens with the help of regular expressions and pattern rules. But a lexical analyzer cannot check the syntax of a given sentence due to the limitations of the regular expressions. Regular expressions cannot check balancing tokens, such as parenthesis.

Lecture โ€“ 9 (^) Grammar Syntax Analysis

Grammars

Therefore, this phase uses context-free grammar (CFG), which is recognized by push-down automata. CFG, on the other hand, is a superset of Regular Grammar, as depicted below:

Lecture โ€“ 9 (^) Grammar Syntax Analysis

Grammars

Construct the CFG for the language having any number of a's over the set โˆ‘= {a}. Solution : As we know the regular expression for the above language is a* Production rule for the Regular expression is as follows:

  1. S โ†’ aS rule 1
  2. S โ†’ ฮต rule 2

Example-2 Construct a CFG for the regular expression (0+1)* Solution : Production rule for the Regular expression is as follows:

  1. S โ†’ 0S | 1S rule 1
  2. S โ†’ ฮต rule 2

Lecture โ€“ 9 (^) CFG (Context-Free Grammar) Syntax Analysis

Example-

S is the start symbol and the set of terminal symbols is {0,1}. Here are the production rules:

  • S โ†’ 1S
  • S โ†’ 0S
  • S โ†’ 0 What does the above context-free grammar describe? a) Strings that end in 0 b) Strings that have an odd number of 1โ€™s c) Strings that have an equal number of 1โ€™s and 0โ€™s d) Strings that end in 1 e) Strings that have an even number of 1โ€™s and 0โ€™s

Question

Ans: a)

Lecture โ€“ 9 Syntax Analysis

Question

According to Noam Chomosky, there are four types of grammars โˆ’ Type 0, Type 1, Type 2, and Type 3. The following table shows how they differ from each other

Grammar Type Grammar Accepted^ Language Accepted^ Automaton Type 0 Unrestricted grammar

Recursively enumerable language

Turing Machine

Type 1 Context-sensitive grammar

Context-sensitive language

Linear-bounded Non-deterministic automaton Type 2 Context-free grammar

Context-free language Non-deterministic Pushdown automaton Type 3 Regular grammar Regular language Finite state automaton

Lecture โ€“ 10 (^) Types of Grammars Syntax Analysis

Types of Grammars

Type-0 grammars generate recursively enumerable languages. The productions have no restrictions. They are any phase structure grammar including all formal grammars. They generate the languages that are recognized by a Turing machine. The productions can be in the form of ฮฑAฮฒ โ†’ ฮด where ฮฑ, ฮฒ & ฮด are string of terminals and/or non-terminals. They can be empty.

Example S โ†’ ACaB Bc โ†’ acB CB โ†’ DB aD โ†’ Db

The productions can be in the form of: ฮฑ โ†’ ฮฒ where ฮฑ is (V + T)* V (V + T)* and ฮฒ is (V + T)*

V : Variables (Non-terminals) & T : Terminals.

Type โ€“ 0 (Unrestricted Grammars)

Type โ€“ 0 (Unrestricted Grammars)

Lecture โ€“ 9 Syntax Analysis

Type-2 grammars generate context-free languages. The productions must be in the form A โ†’ ฮฒ where A โˆˆ N (Non terminal) and ฮฒ โˆˆ (T โˆช N)* (String of terminals and non-terminals). These languages generated by these grammars are be recognized by a non-deterministic pushdown automaton.

Example S โ†’ X a X โ†’ a X โ†’ aX X โ†’ abc X โ†’ ฮต

  1. The productions can be in the form of ฮฑ โ†’ ฮฒ
  2. First of all it should be Type 1.
  3. Left hand side of production can have only one variable. |ฮฑ| = 1 Their is no restriction on ฮฒ.

Type โ€“ 2 (Context-Free Grammars)

Type โ€“ 2 (Context-Free Grammars)

Lecture โ€“ 9 Syntax Analysis

Type-3 grammars generate regular languages. Type-3 grammars must have a single non-terminal on the left-hand side and a right-hand side consisting of a single terminal or single terminal followed by a single non-terminal. The productions must be in the form A โ†’ a or A โ†’ aB where A, B โˆˆ N (Non terminal) and a โˆˆ T (Terminal) The rule S โ†’ ฮต is allowed if S does not appear on the right side of any rule.

Example A โ†’ ฮต A โ†’ a | aB B โ†’ b

Type 3 is most restricted form of grammar. Type 3 should be in the given form only :

V โ†’ VT* | T* (or) V โ†’ TV | T

Type โ€“ 3 (Regular Grammars)

Type โ€“ 3 (Regular Grammars)

Lecture โ€“ 9 Syntax Analysis

Left-most Derivation (Example)

Consider the following grammar and construct a Parse Tree for the string using LMD. 2 * ( 5 + 3 ) / 4 โ–ช E -> E A E โ–ช E -> ( E ) โ–ช E -> id โ–ช A -> + | - | * | / | ^ Left Most Derivation : E -> E A E E -> id A E E -> id * E E -> id * E A E E -> id * ( E ) A E

E -> id * ( E A E ) A E E -> id * ( id A E ) A E E -> id * ( id + E ) A E E -> id * ( id + id ) A E E -> id * ( id + id ) / E E -> id * ( id + id ) / id E -> 2 * ( 5 + 3) / 4

Lecture โ€“ 9 Syntax Analysis

Left-most Derivation (Example)

Parse Tree using Left-most Derivation (LMD)

Parse Tree using Left-most Derivation (LMD)

Lecture โ€“ 9 Syntax Analysis