Docsity

parsing tree pasrer parse good very good, Lecture notes of Compilers

Toggle sidebar
bg1
Parsing
Parsing in Theory
•The “parse tree” is really the second step in the process
•Syntaxic analyser’s first job is to make sure the syntax is correct
■AS YOU KNOW, what may be syntaxically correct may not always
work!!
•The analyser’s second job is to build the tree in order to trace where
■the data and values are to be placed
■check for syntaxically correct input
•values are going to the right spot
•conditions are being checked
•data types match
•Goals of the parser, given an input program:
•Find all syntax errors; for each, produce an appropriate diagnostic message
and recover quickly
•Produce the parse tree, or at least a trace of the parse tree, for the program
•A parser constructs a parse tree for a string (which would be code)
•Two common types of parsers
•Top down - produce the parse tree, beginning at the root
■Order is that of a leftmost derivation
■Traces or builds the parse tree in preorder
•Bottom up - produce the parse tree, beginning at the leaves
■Order is that of the reverse of a rightmost derivation
•A recursive descent parser easily implements a top-down parser for simple
grammars
Where are we??
Terms & symbols used throughout the Notes
•Terms
•A recogniser simply accepts or rejects strings
•A generator produces strings
•A parser constructs a parse tree for a string
Top down vs. bottom up parsing
•Both are general techniques that can be made to work for all languages (but not all
grammars!).
•Recall that a given language can be described by several grammars
Different Grammar but same language??
Left Recursive Right Recursive
E -> E + Num
E -> Num
** this one causes problems later!
E -> Num + E
E -> Num
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download parsing tree pasrer parse good very good and more Lecture notes Compilers in PDF only on Docsity!

Parsing

Parsing in Theory

  • The “parse tree” is really the second step in the process
    • Syntaxic analyser’s first job is to make sure the syntax is correct ■ AS YOU KNOW, what may be syntaxically correct may not always work!!
    • The analyser’s second job is to build the tree in order to trace where ■ the data and values are to be placed ■ check for syntaxically correct input - values are going to the right spot - conditions are being checked - data types match
  • Goals of the parser, given an input program:
    • Find all syntax errors; for each, produce an appropriate diagnostic message and recover quickly
    • Produce the parse tree, or at least a trace of the parse tree, for the program
  • A parser constructs a parse tree for a string (which would be code)
  • Two common types of parsers
    • Top down - produce the parse tree, beginning at the root ■ Order is that of a leftmost derivation ■ Traces or builds the parse tree in preorder
    • Bottom up - produce the parse tree, beginning at the leaves ■ Order is that of the reverse of a rightmost derivation
  • A recursive descent parser easily implements a top-down parser for simple grammars

Where are we??

Terms & symbols used throughout the Notes

  • Terms
    • A recogniser simply accepts or rejects strings
    • A generator produces strings
    • A parser constructs a parse tree for a string

Top down vs. bottom up parsing

  • Both are general techniques that can be made to work for all languages (but not all grammars!).
  • Recall that a given language can be described by several grammars

Different Grammar but same language??

Left Recursive Right Recursive E -> E + Num E -> Num ** this one causes problems later!

E -> Num + E E -> Num

Both of these grammars describe the same language!!

1 + 2 + 3 = (L Recursive) = valid 1 + 2 + 3 = (R Recursive) = valid

  • (^) The parsing problem is to connect the root node S with the tree leaves, given the input (code)
  • (^) Top-down parsers
    • (^) starts constructing the parse tree at the top (root) and move down towards the leaves.
    • (^) Easy to implement by hand, but requires restricted grammars. ■ (^) Predictive parsers (e.g., LL(k)) covered later
  • (^) Bottom-up parsers
    • (^) build nodes on the bottom of the parse tree first. Suitable for automatic parser generation, handles larger class of grammars. ■ (^) shift-reduce parser (or LR( k ) parsers) covered later

Intro. to Top Down Parsing Methods

  • Simplest method is a full-backup, recursive descent parser
  • Often used for parsing simple languages
  • Write recursive recognizers (subroutines) for each grammar rule
    • If rules succeeds perform some action (i.e., build a tree node, emit code, etc.)
    • If rule fails, return failure. Caller may try another choice or fail
    • On failure it “backs up”

http://www.cs.wright.edu/~tkprasad/courses/cs780/L101TDP.pdf

Top Down Example on Paper

S -> NP VP VP -> V NP | V NP PP PP -> P NP V -> "saw" | "ate" | "walked" NP -> "John" | "Mary" | "Bob" | Det N | Det N PP Det -> "a" | "an" | "the" | "my" N -> "man" | "dog" | "cat" | "telescope" | "park" P -> "in" | "on" | "by" | "with"

notice by nature it’s Left Most!! Create another sentence that will work, and one that does not (but may look close)

  • Problems with top-down
    • When going forward, the parser consumes tokens from the input, so what happens if we have to back up? ■ What can we use??
    • Algorithms that use backup tend to be, in general, inefficient ■ There might be a large number of possibilities to try before finding the right one or giving up ■ BUT, A top down grammar can limit backtracking if - it only has one rule per non-terminal - The technique of rule factoring can be used to eliminate multiple rules for a non-terminal
    • Grammar rules which are left-recursive lead to non-termination!

The general algorithm to convert left recursion

For each rule of the form

where:

  • A is a left-recursive nonterminal
  • is a sequence of nonterminals and terminals that is not null (^ )
  • is a sequence of nonterminals and terminals that does not start with A.

replace the A-production by the production:

And create a new nonterminal

This newly created symbol is often called the "tail", or the "rest".

Left Recursion Elimination Example 1

As an example, consider the rule

This could be rewritten to avoid left recursion as

The last rule happens to be equivalent to the slightly shorter form

Left Recursion Elimination Example 2 and 3

Consider left-recursive grammar S S S

S generates strings

Rewrite using right-recursion S S’ S’ S’|

Consider left-recursive grammar exp exp addop term | term

Rewrite using right-recursion exp term exp’ exp’ addop term exp’ |

Try these:

exp --> exp + term | exp - term | term

S A | B

A ABc | AAdd | a | aa B Bee | b

Answer: (^) Answer:

Try if for the entire grammar:

exp --> exp addop term | term addop --> + | - term --> term mulop factor | factor mulop --> * factor --> ( exp ) | number

Answer:

How about these Grammars?? Good/Bad??

E T E' E' + T E' | - T E' | T F T' T' * F T' | / F T' | F num | id Consider the grammar E T + E E T T int T int * T T ( E )

Answer:

  • An important class of predictive parser only peek ahead one token into the stream
  • An LL(k) parser, does a L eft-to-right parse, a L eftmost-derivation, and k- symbol lookahead
  • Grammars where one can decide which rule to use by examining only the next token are LL(1)

Types of Parsers

LL(n) LR(n) Left to right, Leftmost derivation, look ahead at most n symbols.

Left to right, Right derivation, look ahead at most n symbols.

  • LL(1) grammars are widely used in practice
  • The syntax of a Programming Language can usually be adjusted to enable it to be described with an LL(1) grammar
  • A LR(1) or LL(1) parser never need to “look ahead” more than one input token to know what parser production rule applies

Predictive Parsing and Left Factoring

Remember the grammar?? E T + E E T T int T int * T T ( E )

  • Must left-factored grammar before use for predictive parsing
    • Left-factoring involves rewriting rules so that ■ if a non-terminal has > 1 rule, each begins with a^ terminal

LLR 1 Parsing Table Full Example

Video: (many need to adjust screen resolution) http://userpages.umbc.edu/~slupoli/notes/ProgLanguages/videos/ LL1ParingTableExample.swf

missed ( and ) as terminals in video!!

Using the same layout, try (using the same table):

  1. int * int + int
  2. (int + int) * int
  3. (int + int + int) int

Bottom up- Try www.cs.virginia.edu/kim/courses/ cs671 /lec/ cs671 -05- bottomup .ppt

Resources: Dr. Finin – UMBC Sebesta http://nltk.googlecode.com/svn/trunk/doc/book/ch08.html F 0 6 5

F 0 http://www.cs.sun.ac.za/rw711/2012term2/lectures/lec9 F 0 2010/l96 5 2010.pdf http://en.wikipedia.org/wiki/Left6 5 recursion