Presentation is loading. Please wait.

Presentation is loading. Please wait.

WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.

Similar presentations


Presentation on theme: "WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University."— Presentation transcript:

1 WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University

2 2

3  CFG grammar is about categorizing the statements of a language.  Parsing using CFG means categorizing a certain statements into categories defined in the CFG.  Parsing can be expressed using a special type of graph called Trees where no cycles exist.  A parse tree is the graph representation of a derivation.  Programmatically; Parse tree can be represented as a dynamic data structure using a single root node. 3 Dr. Hussien M. Sharaf

4 4 (1)A vertex with a label which is a Non-terminal symbol is a parse tree. (2) If A → y 1 y 2 … y n is a rule in R, then the tree A y1y1 y2y2 ynyn... is a parse tree. Dr. Hussien M. Sharaf

5 Prod1:S → (S) Prod2:S → SS Prod3:S → ()  Derive the string ((())) S → (S)…..[by prod1] → ((S))…..[by prod1] → ((())) …..[by prod3] Dr. Hussien M. Sharaf 5

6  The language described by a CFG is the set of strings that can be derived from the start symbol using the rules of the grammar.  At each step, we choose a non-terminal to replace. S  (S)  (SS)  ((S)S)  (( )S)  (( )(S))  (( )((S)))  (( )(( ))) derivation sentential form This example demonstrates a leftmost derivation : one where we always expand the leftmost non-terminal in the sentential form.

7  We can describe a derivation using a graphical representation called parse tree:  the root is labeled with the start symbol, S  each internal node is labeled with a non-terminal  the children of an internal node A are the right- hand side of a production A   each leaf is labeled with a terminal  A parse tree has a unique leftmost and a unique rightmost derivation (however, we cannot tell which one was used by looking at the tree)

8  So, how can we use the grammar described earlier to verify the syntax of "(( )((( ))))"?  We must try to find a derivation for that string.  We can work top-down (starting at the root/start symbol) or bottom-up (starting at the leaves).  Careful!  There may be more than one grammar to describe the same language.  Not all grammars are suitable

9 Parsing  Top-down parser:  starts at the root of derivation tree  picks a production and tries to match the input  may require backtracking  some grammars are backtrack-free (predictive)  Bottom-up parser:  starts at the leaves  starts in a state valid for legal first tokens  as input is consumed, changes state to encode possibilities (recognize valid prefixes)  uses a stack to store both state and sentential forms 9 © Oscar Nierstrasz

10  A parser is a top-down if it discovers a parse tree top to bottom.  A top-down parse corresponds to a preorder traversal of the parse tree.  A left most derivation is applied to each step. Dr. Hussien M. Sharaf

11  Main idea:  Start at the root, grow towards leaves  Pick a production and try to match input  May need to backtrack

12 Parsing A top-down parser starts with the root of the parse tree, labeled with the start or goal symbol of the grammar. To build a parse tree, it repeats the following steps until the fringe of the parse tree matches the input string 1. At a node labeled A, select a production A  α and construct the appropriate child for each symbol of α 2. When a terminal is added to the fringe that doesn’t match the input string, backtrack 3. Find the next node to be expanded (must have a label in V n ) The key is selecting the right production in step 1  should be guided by input string 12 © Oscar Nierstrasz

13 Steps:  Elimination of Ambiguity  Elimination of Left Recursion  Left Factoring  Drawing Transition Diagram (Optional)  Applying First/Follow operators  Building the Parsing Table  Parse the given statements Dr. Mohammad Nassef

14  For some strings there exist more than one parse tree  Or more than one leftmost derivation  Or more than one rightmost derivation  Example: id+id*id Grammar: E -> E+E | E*E | id

15  Deduce CFG of addition and parse the following expression 2+3+5  1] S→S+S|N  2] N→1|2|3|4|5|6|7|8|9|0| N1|N2|N3|N4|N5|N6|N7|N8|N9|N0 15 S S+S S + S 5 S + 3 S 2 N Can u make another parsing tree ? Dr. Hussien M. Sharaf N N

16  Deduce CFG of a addition/multiplication and parse the following expression 2+3 * 5  1] S→S+S|S * S|N  2] N→1|2|3|4|5|6|7|8|9|0|NN 16 S S*S S * S 5 S + 3 S 2 N Can u make another parsing tree ? Dr. Hussien M. Sharaf N N

17  In general, we try to eliminate ambiguity by rewriting the grammar.  Example: E  E+E | E  E | id becomes: E  E+T | T T  T  F | F F  id  Can you build a parse tree for 2+3*5?

18  A grammar can generate the same string in different ways.  Ambiguity occurs when a string has two or more leftmost derivations for the same CFG.  There are ways to eliminate ambiguity such as using Chomsky Normal Form (CNF) which doesn’t use Λ.  Λ cause ambiguity. 18 Dr. Hussien M. Sharaf

19


Download ppt "WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University."

Similar presentations


Ads by Google