SlideShare a Scribd company logo
1 of 44
5/11/2021 Saeed Parsa 1
Compiler Design
Grammars & Parsers
Saeed Parsa
Room 332,
School of Computer Engineering,
Iran University of Science & Technology
parsa@iust.ac.ir
Spring 2021
Backus-Naur Form (BNF) notation
5/11/2021 Saeed Parsa 2
 Backus-Naur form (BNF) is a formal notation for encoding grammars
intended for human consumption.
 Every rule in Backus-Naur form has the following structure:
name ::= expansion
or
name  expansion
 The symbols ::= and  mean "may expand into" or "may be replaced with”.
 A name is also called a non-terminal symbol.
 Every name in Backus-Naur form is surrounded by angle brackets, < >.
 An expansion is an expression containing terminal symbols and non-terminal
symbols, joined together by sequencing and choice.
Backus-Naur Form (BNF) notation
5/11/2021 Saeed Parsa 3
• A terminal symbol is a literal like ("+" or "function") or a class of literals
(like integer).
• Simply juxtaposing expressions indicates sequencing.
• A vertical bar | indicates choice.
Backus-Naur Form (BNF) notation
5/11/2021 Saeed Parsa 4
• For example, in BNF, the classic expression grammar is:
<expr> ::= <expr> "+" <term>
| <expr> “-" <term> | <term>
<term> ::= <term> "*" < factor>
| <term> “/" < factor>| <factor>
<factor> ::= "(" <expr> ")"
| number | identifier
Start symbol
Non-terminal symbols
Extended BNF (EBNF) notation
5/11/2021 Saeed Parsa 5
• Extended Backus-Naur form (EBNF) is a collection of extensions to Backus-
Naur form.
• Not all of these are strictly a superset, as some change the rule-definition
relation ::= to =, while others remove the angled brackets from non-terminals.
• More important than the minor syntactic differences between the forms of
EBNF are the additional operations it allows in expansions:
1. Grouping operator: (…)
2. Optional operator: […]
3. Repetition operator (zero or more): {…}
Extended BNF (EBNF) notation
5/11/2021 Saeed Parsa 6
• For example, in EBNF, the classic expression grammar is:
expr ::= <expr> ("+" | “-”) <term>
| <term>
<term> ::= <term> ( "*“ | “/”) <factor>
| <factor>
<factor> ::= "(" <expr> ")"
| <signed> | <string> | identifier
<signed> ::= [“+” | “-” ] number
<string> ::= ““” character { character } “””
Grouping
Optional
Iteration
Syntax/Parse trees
5/11/2021 Saeed Parsa 7
 Syntax trees are created to show and evaluate the grammatical structure of
statements (programs).
 To evaluate the syntax of a statement / program, the parser operates on a
stream of tokens that are generated by the lexical analyzer.
Example
5/11/2021 Saeed Parsa 8
We want to find parts of speech in this sentence.
She loves animals.
Example
5/11/2021 Saeed Parsa 9
First, you broke up the sentence into parts (words).
She loves animals
whitespace demarcates the parts
Example
5/11/2021 Saeed Parsa 10
Second, you identified each part's type.
She loves animals
pronoun verb noun
Example
5/11/2021 Saeed Parsa 11
Third, you diagrammed the sentence.
Sentence
pronoun verb noun
She loves animals
That's parsing!
Example
5/11/2021 Saeed Parsa 12
Parsing is nothing but structuring a linear sequence of parts.
She loves animals
linear sequence of parts
Sentence
pronoun verb noun
She loves animals
structured parts
parse
Example
5/11/2021 Saeed Parsa 13
Noam Chomsky (linguist)
In our brain, we automatically convert a linear sequence
of parts into a parse tree in order to understand.
How to write parser?
5/11/2021 Saeed Parsa 14
Description of
how to
structure the
parts
Parser
Parser
Generator
Description of
how to break
up the linear
sequence into
parts
lexer rules
parser rules
This tutorial teaches
you how to write these.
How to write parser?
5/11/2021 Saeed Parsa 15
<Boolean expression> ::=
<Boolean expression> or <Boolean term>
| <Boolean Term>
<Boolean term> ::=
<Boolean term> and <Boolean factor>
| <Boolean factor>
<Boolean factor > ::=
( <Boolean expression > )
| number
| identifier
Statement: A and (B or C and D)
Syntax Tree
Yield : A and ( B or C and D )
Top-down parsing
5/11/2021 Saeed Parsa 16
 Top-down parsing begins with the root of the parse tree and extends the tree
downward until leaves match the input string.
 In Top-down parsing We start from the nonterminal start symbol S and apply
every rule that can replace the nonterminal symbol with other nonterminal
symbols or terminal symbols.
 In turn, new nonterminal symbols are further replaced by their rewriting rules
from left to right, until all symbols are terminal symbols of the string.
Top-down parsing
5/11/2021 Saeed Parsa 17
Example:
S  id := E
E  E + T | E – T | E or T | T
T  T * F | T / F | T and F | F
F  id | no | (E) | not E
Statement: a := 2 + 3 – 4
Top-down parsing begins with the root
of the parse tree and extends the tree
downward until leaves match the input
string.
17
Sentential Form
5/11/2021 Saeed Parsa 18
The leftmost derivation is the one in which you always expand
the leftmost non-terminal. The rightmost derivation is the one in which you
always expand the rightmost non-terminal.
 Top-down parsing methods starts with the start symbol and tries to produce
the input from it.
 At any point in time, it replaces the leftmost nonterminal with the right hand
side of one of the rules, defining the nonterminal symbol.
 In this way, sentential forms are created.
 A sentential form is any string derivable from the start symbol.
 A sentential form is the start symbol S of a grammar or any string that can be
derived from S.
Top-down parsing: Example
5/11/2021 Saeed Parsa 19
Bottom-up parsing
5/11/2021 Saeed Parsa 20
 Also known as shift‐reduce parsing, LR family or Precedence parsing
 Shift: allow shifting input characters to the stack, waiting till a matching
production can be determined
 Reduce: once a matching production is determined, reduce
 Follow the rightmost derivation, in a reversed way
 Parse from bottom (the leaves of the parse tree) and work up to the starting
symbol
 Due to the added “shift”
- More powerful
 Can handle left recursive grammars and grammars with left factors
- Less space efficient
Bottom-up parsing
5/11/2021 Saeed Parsa 21
 Build the parse tree from leaves to root.
 Bottom-up parsing can be defined as an attempt to reduce the input string w to
the start symbol of grammar by tracing out the rightmost derivations of w in
reverse.
Eg.
S  aABe
A  Abc | b
B  d
Input: a b b c d e
Bottom-up parsing
5/11/2021 Saeed Parsa 22
Ambiguous Grammar
5/11/2021 Saeed Parsa 23
 When a grammar permits several different syntax trees for some strings, we call
the grammar ambiguous.
 How do we know when a grammar is ambiguous?
 In fact, the problem is formally undecidable.
 If we can find a string and show two alternative syntax trees for it, the grammar is
ambiguous.
 If a single production rule is both left and right recursive, the grammar is
ambiguous. For example: A → αA ∣ Aα
Ambiguous Grammar
5/11/2021 Saeed Parsa 24
 If a single production rule is both left and right recursive, the grammar is
ambiguous. For example, the following rule
A→ αA ∣ Aα
 has the following two (left-most) derivations:
1. A ⇒ αA ⇒ αAα corresponding to the grouping (α(Aα))
2. A ⇒ Aα ⇒ αAα corresponding to the grouping ((αA)α)
 So, the grammar is ambiguous!
Ambiguous Grammar: Example
5/11/2021 Saeed Parsa 25
G1:
E ::= E + T | T – E | T
T ::= T * F | T / F | F
F ::= ( E ) | id | no
Input string:
2-3+4
Ambiguous Grammar
5/11/2021 Saeed Parsa 26
 A CFG is ambiguous if there is a string in the language that is the yield of two or
more parse trees.
 Example:
S -> SS | (S) | ()
 There are two parse trees for ()()():
Ambiguous Grammar: Example
5/11/2021 Saeed Parsa 27
Statement  IfSt | WhileSt | ForSt | CaseSt | CompundSt | AssSt | CallSt
IfSt  if Condition then Statement ElsePart
ElsePart  else Statement | 
Condition  E Relop E | E
Relop  < | <= | <> | = | >= | >
E  E + T | E – T | T
T  T * F | T / F | F
F  id | no | ( E )
There are two parse trees with two different semantics for the statement:
if a > 5 then if a < 7 then writeln(‘a = 6’) else writeln(‘a >= 7‘)
Ambiguous Grammar: Example
5/11/2021 Saeed Parsa 28
Ambiguous Grammar: Example
5/11/2021 Saeed Parsa 29
Operator association in ANTLR
5/11/2021 Saeed Parsa 30
Consider this parser rule for arithmetic expressions:
expression: expression MULT expression
| INT
;
Question: How will this input be parsed: 1 * 2 * 3
Answer: By default, ANTLR associates operators left
to right, so the input is parsed this way: (1 * 2) * 3
Operator association in ANTLR
5/11/2021 Saeed Parsa 31
Suppose the arithmetic operator is exponentiation (^):
expression : expression EXPON expression
| INT;
Question: How will this input be parsed: 1 ^ 2 ^ 3
Answer: Again, the default is to associate left to right, so the
input is parsed this way:
(1 ^ 2) ^ 3
However, that's not right.
The exponentiation operator should associate right-to-left,
like this:
1 ^ (2 ^ 3)
Operator association in ANTLR
5/11/2021 Saeed Parsa 32
We can instruct ANTLR on how we want an operator
associated, using the assoc option:
expr : <assoc=right> expr '^' expr
| INT
;
Now this input 1 ^ 2 ^ 3 is parsed this way 1 ^ (2 ^ 3).
Problem
5/11/2021 Saeed Parsa 33
Create a grammar for such inputs:
2 9 10 3 1 2 3
indicates
that there
are 2
following
integers
indicates
that there
are 3
following
integers
Answer
5/11/2021 Saeed Parsa 34
lexer grammar MyLexer;
INT : [0-9]+ ;
WS : [ trn]+ -> skip ;
parser grammar MyParser;
options { tokenVocab=MyLexer; }
file: group+ ;
group: INT sequence ;
sequence: ( INT )* ;
Here's the parse tree that
is generated for the input:
2 9 10 3 1 2 3
Here is the parse tree we
desire for:
2 9 10 3 1 2 3
No restriction on the number
of INT values within
sequence.
Answer
5/11/2021 Saeed Parsa 35
This is what we get
This is what we want
Source File: 2 9 10 3 1 2 3
Assignment 3
5/11/2021 Saeed Parsa 36
Subject : Languages & Grammars
Deadline: 1399/7/28
Mark: 5 out of 100.
Assignment 3
5/11/2021 Saeed Parsa 37
Exercise 1:
Assignment 3
5/11/2021 Saeed Parsa 38
1. Write a grammar
(a) S ::= a S b | b S a | 
(b) S ::= a a S b | 
(c) S ::= a a S b | 
(d) S ::= a a S b | 
2. For statements in python:
<for statement> ::= for <var> in <list> :
<statements>
eg. for x in [2, 4, -10, “c”]:
print x, “@’
Answers Ex.1:
Assignment 3
5/11/2021 Saeed Parsa 39
1. Consider the context-free grammar:
S -> S S + | S S * | a
and the string aa + a*.
I. Draw the Syntax tree for the given string.
II. Give a leftmost derivation for the string.
III.Give a rightmost derivation for the string.
IV.Give a parse tree for the string.
V. Is the grammar ambiguous or unambiguous? Justify your answer.
VI.Describe the language generated by this grammar.
Exercise 2:
Assignment 3
5/11/2021 Saeed Parsa 40
Answers Ex.2:
Assignment 3
5/11/2021 Saeed Parsa 41
Answers Ex.2:
Assignment 3
5/11/2021 Saeed Parsa 42
Exercise 3:
Assignment 3
5/11/2021 Saeed Parsa 43
Answers Ex.3:
5/11/2021 Saeed Parsa 44

More Related Content

What's hot

What's hot (20)

Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
Parsing
ParsingParsing
Parsing
 
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
 
Declare Your Language: Syntax Definition
Declare Your Language: Syntax DefinitionDeclare Your Language: Syntax Definition
Declare Your Language: Syntax Definition
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Lexical analysis
Lexical analysisLexical analysis
Lexical analysis
 
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
 
Chapter Two(1)
Chapter Two(1)Chapter Two(1)
Chapter Two(1)
 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical Analyzer
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
 
L attribute in compiler design
L  attribute in compiler designL  attribute in compiler design
L attribute in compiler design
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical Analyzer
 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design
 
Lexical
LexicalLexical
Lexical
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 

Similar to 4. languages and grammars

Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsgadisaAdamu
 
compiler design course focus of chapter 3 and 4 part
compiler design course focus of chapter 3 and 4 partcompiler design course focus of chapter 3 and 4 part
compiler design course focus of chapter 3 and 4 partabel185080
 
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxChapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxDrIsikoIsaac
 
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...movocode
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
CH 2.pptx
CH 2.pptxCH 2.pptx
CH 2.pptxObsa2
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Keyappasami
 
4 lexical and syntax analysis
4 lexical and syntax analysis4 lexical and syntax analysis
4 lexical and syntax analysisjigeno
 
Ch3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptxCh3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptxTameneTamire
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Bhavin Darji
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bisonvip_du
 
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesEelco Visser
 
2.4 derivations and languages
2.4 derivations and languages2.4 derivations and languages
2.4 derivations and languagesSampath Kumar S
 
Ambiguous & Unambiguous Grammar
Ambiguous & Unambiguous GrammarAmbiguous & Unambiguous Grammar
Ambiguous & Unambiguous GrammarMdImamHasan1
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 

Similar to 4. languages and grammars (20)

Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
 
compiler design course focus of chapter 3 and 4 part
compiler design course focus of chapter 3 and 4 partcompiler design course focus of chapter 3 and 4 part
compiler design course focus of chapter 3 and 4 part
 
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxChapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptx
 
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
CH 2.pptx
CH 2.pptxCH 2.pptx
CH 2.pptx
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
4 lexical and syntax analysis
4 lexical and syntax analysis4 lexical and syntax analysis
4 lexical and syntax analysis
 
Parse Tree
Parse TreeParse Tree
Parse Tree
 
Syntax Definition
Syntax DefinitionSyntax Definition
Syntax Definition
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Ch3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptxCh3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptx
 
Ch06
Ch06Ch06
Ch06
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bison
 
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic Services
 
Compiler Design QA
Compiler Design QACompiler Design QA
Compiler Design QA
 
2.4 derivations and languages
2.4 derivations and languages2.4 derivations and languages
2.4 derivations and languages
 
Ambiguous & Unambiguous Grammar
Ambiguous & Unambiguous GrammarAmbiguous & Unambiguous Grammar
Ambiguous & Unambiguous Grammar
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 

Recently uploaded

Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfJerry Chew
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfElizabeth Walsh
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of PlayPooky Knightsmith
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 

Recently uploaded (20)

VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

4. languages and grammars

  • 1. 5/11/2021 Saeed Parsa 1 Compiler Design Grammars & Parsers Saeed Parsa Room 332, School of Computer Engineering, Iran University of Science & Technology parsa@iust.ac.ir Spring 2021
  • 2. Backus-Naur Form (BNF) notation 5/11/2021 Saeed Parsa 2  Backus-Naur form (BNF) is a formal notation for encoding grammars intended for human consumption.  Every rule in Backus-Naur form has the following structure: name ::= expansion or name  expansion  The symbols ::= and  mean "may expand into" or "may be replaced with”.  A name is also called a non-terminal symbol.  Every name in Backus-Naur form is surrounded by angle brackets, < >.  An expansion is an expression containing terminal symbols and non-terminal symbols, joined together by sequencing and choice.
  • 3. Backus-Naur Form (BNF) notation 5/11/2021 Saeed Parsa 3 • A terminal symbol is a literal like ("+" or "function") or a class of literals (like integer). • Simply juxtaposing expressions indicates sequencing. • A vertical bar | indicates choice.
  • 4. Backus-Naur Form (BNF) notation 5/11/2021 Saeed Parsa 4 • For example, in BNF, the classic expression grammar is: <expr> ::= <expr> "+" <term> | <expr> “-" <term> | <term> <term> ::= <term> "*" < factor> | <term> “/" < factor>| <factor> <factor> ::= "(" <expr> ")" | number | identifier Start symbol Non-terminal symbols
  • 5. Extended BNF (EBNF) notation 5/11/2021 Saeed Parsa 5 • Extended Backus-Naur form (EBNF) is a collection of extensions to Backus- Naur form. • Not all of these are strictly a superset, as some change the rule-definition relation ::= to =, while others remove the angled brackets from non-terminals. • More important than the minor syntactic differences between the forms of EBNF are the additional operations it allows in expansions: 1. Grouping operator: (…) 2. Optional operator: […] 3. Repetition operator (zero or more): {…}
  • 6. Extended BNF (EBNF) notation 5/11/2021 Saeed Parsa 6 • For example, in EBNF, the classic expression grammar is: expr ::= <expr> ("+" | “-”) <term> | <term> <term> ::= <term> ( "*“ | “/”) <factor> | <factor> <factor> ::= "(" <expr> ")" | <signed> | <string> | identifier <signed> ::= [“+” | “-” ] number <string> ::= ““” character { character } “”” Grouping Optional Iteration
  • 7. Syntax/Parse trees 5/11/2021 Saeed Parsa 7  Syntax trees are created to show and evaluate the grammatical structure of statements (programs).  To evaluate the syntax of a statement / program, the parser operates on a stream of tokens that are generated by the lexical analyzer.
  • 8. Example 5/11/2021 Saeed Parsa 8 We want to find parts of speech in this sentence. She loves animals.
  • 9. Example 5/11/2021 Saeed Parsa 9 First, you broke up the sentence into parts (words). She loves animals whitespace demarcates the parts
  • 10. Example 5/11/2021 Saeed Parsa 10 Second, you identified each part's type. She loves animals pronoun verb noun
  • 11. Example 5/11/2021 Saeed Parsa 11 Third, you diagrammed the sentence. Sentence pronoun verb noun She loves animals That's parsing!
  • 12. Example 5/11/2021 Saeed Parsa 12 Parsing is nothing but structuring a linear sequence of parts. She loves animals linear sequence of parts Sentence pronoun verb noun She loves animals structured parts parse
  • 13. Example 5/11/2021 Saeed Parsa 13 Noam Chomsky (linguist) In our brain, we automatically convert a linear sequence of parts into a parse tree in order to understand.
  • 14. How to write parser? 5/11/2021 Saeed Parsa 14 Description of how to structure the parts Parser Parser Generator Description of how to break up the linear sequence into parts lexer rules parser rules This tutorial teaches you how to write these.
  • 15. How to write parser? 5/11/2021 Saeed Parsa 15 <Boolean expression> ::= <Boolean expression> or <Boolean term> | <Boolean Term> <Boolean term> ::= <Boolean term> and <Boolean factor> | <Boolean factor> <Boolean factor > ::= ( <Boolean expression > ) | number | identifier Statement: A and (B or C and D) Syntax Tree Yield : A and ( B or C and D )
  • 16. Top-down parsing 5/11/2021 Saeed Parsa 16  Top-down parsing begins with the root of the parse tree and extends the tree downward until leaves match the input string.  In Top-down parsing We start from the nonterminal start symbol S and apply every rule that can replace the nonterminal symbol with other nonterminal symbols or terminal symbols.  In turn, new nonterminal symbols are further replaced by their rewriting rules from left to right, until all symbols are terminal symbols of the string.
  • 17. Top-down parsing 5/11/2021 Saeed Parsa 17 Example: S  id := E E  E + T | E – T | E or T | T T  T * F | T / F | T and F | F F  id | no | (E) | not E Statement: a := 2 + 3 – 4 Top-down parsing begins with the root of the parse tree and extends the tree downward until leaves match the input string. 17
  • 18. Sentential Form 5/11/2021 Saeed Parsa 18 The leftmost derivation is the one in which you always expand the leftmost non-terminal. The rightmost derivation is the one in which you always expand the rightmost non-terminal.  Top-down parsing methods starts with the start symbol and tries to produce the input from it.  At any point in time, it replaces the leftmost nonterminal with the right hand side of one of the rules, defining the nonterminal symbol.  In this way, sentential forms are created.  A sentential form is any string derivable from the start symbol.  A sentential form is the start symbol S of a grammar or any string that can be derived from S.
  • 20. Bottom-up parsing 5/11/2021 Saeed Parsa 20  Also known as shift‐reduce parsing, LR family or Precedence parsing  Shift: allow shifting input characters to the stack, waiting till a matching production can be determined  Reduce: once a matching production is determined, reduce  Follow the rightmost derivation, in a reversed way  Parse from bottom (the leaves of the parse tree) and work up to the starting symbol  Due to the added “shift” - More powerful  Can handle left recursive grammars and grammars with left factors - Less space efficient
  • 21. Bottom-up parsing 5/11/2021 Saeed Parsa 21  Build the parse tree from leaves to root.  Bottom-up parsing can be defined as an attempt to reduce the input string w to the start symbol of grammar by tracing out the rightmost derivations of w in reverse. Eg. S  aABe A  Abc | b B  d Input: a b b c d e
  • 23. Ambiguous Grammar 5/11/2021 Saeed Parsa 23  When a grammar permits several different syntax trees for some strings, we call the grammar ambiguous.  How do we know when a grammar is ambiguous?  In fact, the problem is formally undecidable.  If we can find a string and show two alternative syntax trees for it, the grammar is ambiguous.  If a single production rule is both left and right recursive, the grammar is ambiguous. For example: A → αA ∣ Aα
  • 24. Ambiguous Grammar 5/11/2021 Saeed Parsa 24  If a single production rule is both left and right recursive, the grammar is ambiguous. For example, the following rule A→ αA ∣ Aα  has the following two (left-most) derivations: 1. A ⇒ αA ⇒ αAα corresponding to the grouping (α(Aα)) 2. A ⇒ Aα ⇒ αAα corresponding to the grouping ((αA)α)  So, the grammar is ambiguous!
  • 25. Ambiguous Grammar: Example 5/11/2021 Saeed Parsa 25 G1: E ::= E + T | T – E | T T ::= T * F | T / F | F F ::= ( E ) | id | no Input string: 2-3+4
  • 26. Ambiguous Grammar 5/11/2021 Saeed Parsa 26  A CFG is ambiguous if there is a string in the language that is the yield of two or more parse trees.  Example: S -> SS | (S) | ()  There are two parse trees for ()()():
  • 27. Ambiguous Grammar: Example 5/11/2021 Saeed Parsa 27 Statement  IfSt | WhileSt | ForSt | CaseSt | CompundSt | AssSt | CallSt IfSt  if Condition then Statement ElsePart ElsePart  else Statement |  Condition  E Relop E | E Relop  < | <= | <> | = | >= | > E  E + T | E – T | T T  T * F | T / F | F F  id | no | ( E ) There are two parse trees with two different semantics for the statement: if a > 5 then if a < 7 then writeln(‘a = 6’) else writeln(‘a >= 7‘)
  • 30. Operator association in ANTLR 5/11/2021 Saeed Parsa 30 Consider this parser rule for arithmetic expressions: expression: expression MULT expression | INT ; Question: How will this input be parsed: 1 * 2 * 3 Answer: By default, ANTLR associates operators left to right, so the input is parsed this way: (1 * 2) * 3
  • 31. Operator association in ANTLR 5/11/2021 Saeed Parsa 31 Suppose the arithmetic operator is exponentiation (^): expression : expression EXPON expression | INT; Question: How will this input be parsed: 1 ^ 2 ^ 3 Answer: Again, the default is to associate left to right, so the input is parsed this way: (1 ^ 2) ^ 3 However, that's not right. The exponentiation operator should associate right-to-left, like this: 1 ^ (2 ^ 3)
  • 32. Operator association in ANTLR 5/11/2021 Saeed Parsa 32 We can instruct ANTLR on how we want an operator associated, using the assoc option: expr : <assoc=right> expr '^' expr | INT ; Now this input 1 ^ 2 ^ 3 is parsed this way 1 ^ (2 ^ 3).
  • 33. Problem 5/11/2021 Saeed Parsa 33 Create a grammar for such inputs: 2 9 10 3 1 2 3 indicates that there are 2 following integers indicates that there are 3 following integers
  • 34. Answer 5/11/2021 Saeed Parsa 34 lexer grammar MyLexer; INT : [0-9]+ ; WS : [ trn]+ -> skip ; parser grammar MyParser; options { tokenVocab=MyLexer; } file: group+ ; group: INT sequence ; sequence: ( INT )* ; Here's the parse tree that is generated for the input: 2 9 10 3 1 2 3 Here is the parse tree we desire for: 2 9 10 3 1 2 3 No restriction on the number of INT values within sequence.
  • 35. Answer 5/11/2021 Saeed Parsa 35 This is what we get This is what we want Source File: 2 9 10 3 1 2 3
  • 36. Assignment 3 5/11/2021 Saeed Parsa 36 Subject : Languages & Grammars Deadline: 1399/7/28 Mark: 5 out of 100.
  • 37. Assignment 3 5/11/2021 Saeed Parsa 37 Exercise 1:
  • 38. Assignment 3 5/11/2021 Saeed Parsa 38 1. Write a grammar (a) S ::= a S b | b S a |  (b) S ::= a a S b |  (c) S ::= a a S b |  (d) S ::= a a S b |  2. For statements in python: <for statement> ::= for <var> in <list> : <statements> eg. for x in [2, 4, -10, “c”]: print x, “@’ Answers Ex.1:
  • 39. Assignment 3 5/11/2021 Saeed Parsa 39 1. Consider the context-free grammar: S -> S S + | S S * | a and the string aa + a*. I. Draw the Syntax tree for the given string. II. Give a leftmost derivation for the string. III.Give a rightmost derivation for the string. IV.Give a parse tree for the string. V. Is the grammar ambiguous or unambiguous? Justify your answer. VI.Describe the language generated by this grammar. Exercise 2:
  • 40. Assignment 3 5/11/2021 Saeed Parsa 40 Answers Ex.2:
  • 41. Assignment 3 5/11/2021 Saeed Parsa 41 Answers Ex.2:
  • 42. Assignment 3 5/11/2021 Saeed Parsa 42 Exercise 3:
  • 43. Assignment 3 5/11/2021 Saeed Parsa 43 Answers Ex.3: