0% found this document useful (0 votes)
33 views19 pages

Compiler Lecture 10

Uploaded by

jackparker01321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views19 pages

Compiler Lecture 10

Uploaded by

jackparker01321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Parsing and Parsing Table

Course Code: CSC3220 Course Title: Compiler Design

Dept. of Computer Science


Faculty of Science and Technology

Lecturer 10 Week No: 10 Semester


No: :
Lecturer: Md Masum Billah; [email protected], Masum.Billah.net
Lecture Outline

1. Quiz
2. Parsing
3. Parsing Technique(LL1 Grammar)
4. Parsing Table Construction Technique
5. Examples
6. Exercises
Objective and Outcome

Objective:
• To provide an overview of parsing and parsing types.
• To give an overview of predictive parser
• To demonstrate the predictive parsing table construction for predictive / LL(1)
parser from a given CFG
Outcome:
• After this lecture the students will be able to understand basics of predictive
and LL (1) parser.
• The students will be capable of constructing a predictive parsing table from
given CFG
Quiz

▪Quiz
Parsing
▪ The process of determining if a string of terminals (tokens) can be generated by a
grammar.

▪Time complexity:
▪ For any CFG there is a parser that takes at most O(n3) time to parse a string of n
terminals.
▪ Linear algorithms suffice to parse essentially all languages that arise in practice.

▪ Two kinds of methods:


▪ Top-down: constructs a parse tree from root to leaves
▪ Bottom-up: constructs a parse tree from leaves to root
Types of Parsing
Parsing Table Overview

▪ A Parsing table collects information from FIRST and FOLLOW set.


▪ A Parsing table provides a direction/predictive guideline for generating a parse tree from
a grammar.
▪ A Parsing table provide information to create moves made by a predictive parser on a
specific input.
LL(k) LL(1) Parser Design
Prerequisite
• Make the grammar suitable for top-down parser. By performing the elimination
of left recursion. And by performing left factoring.
• Find the FIRST and FOLLOW of the variables.
• Create Parsing table based on the information from FIRST and FOLLOW sets.
Predictive (LL1) Parsing Table
Construction Rule
• Collect information from FIRST and FOLLOW sets into a predictive parsing Table M[A,a]
• M[A,a] is a 2D array where
• A nonterminal
• A is a terminal or the symbol $, the input endmarker
• The Production A -> a is chosen if the next input symbol a is in First (a).
• If a = ε, we should again choose A-> a, if the current input symbol is in FOLLOW (A) or if
the $ on the input has been reached and $ is in the FOLLOW(A)
Predictive (LL1) Parsing Table
Construction Rule

• From a Grammar Find out First and Follow


• Take a production; Row should be left hand side and column should be first of
right and side
• If we see epsilon in first of right hand side, place the production in follow also
• If first of right hand side terminal, directly place in table
• If the first of right hand side is epsilon, directly place in follow of left hand side
Parsing Table Construction (Example)
Predictive parsing table for the grammar (Example 1)
Predictive parsing table for the grammar (Example 2)
Predictive parsing table for the grammar (Example 3)
Parsing Table Construction (Problem)

Consider the following LL(1) grammar, which has the set of terminals T = fa; b;
ep; +; *; (; )g. This grammar generates regular expressions over fa, bg, with +
meaning the RegExp OR operator, and ep meaning the ε symbol. (Yes, this is a
context free grammar for generating regular expressions!)
Parsing Table Construction (Solution)

FIRST and FOLLOW sets


Parsing Table Construction (Solution)

LL (1) Parsing Table


Lecture References

• Carnegie Mellon University Material


https://www.cs.cmu.edu/~fp/courses/15411-f09/lectures/08-predictive.pdf
• Columbia University Material
http://www1.cs.columbia.edu/~aho/cs4115/lectures/13-02-20.htm
• Online Material
https://www.ques10.com/p/8960/construct-predictive-passing-table-for-following-2/
• Online Tutorial
https://www.tutorialspoint.com/compiler_design/compiler_design_top_down_parser.htm
References/ Books

• 1. Compilers-Principles, techniques and tools (2nd Edition) V. Aho, Sethi and D.


Ullman
• 2. Principles of Compiler Design (2nd Revised Edition 2009) A. A. Puntambekar
• 3. Basics of Compiler Design Torben Mogensen

You might also like