Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parsing Lecture 5 Fri, Jan 28, 2005. Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.

Similar presentations


Presentation on theme: "Parsing Lecture 5 Fri, Jan 28, 2005. Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A."— Presentation transcript:

1 Parsing Lecture 5 Fri, Jan 28, 2005

2 Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A   where A is a nonterminal and  is a string of terminals and nonterminals, i.e.,   (   N)*.

3 Grammar Convention Terminals are represented by lowercase letters near the beginning of the alphabet (a, b, c, …) or by non-alphabetic characters (2, +, {, …). Nonterminals are represented by uppercase letters near the beginning of the alphabet (A, B, C, …).

4 Grammar Convention Grammar symbols that may be either terminals or nonterminals are represented by uppercase letters near the end of the alphabet (Z, Y, X, …). Strings of terminals are represented by lowercase letters near the end of the alphabet (z, y, x, …).

5 Grammar Convention Strings of grammar symbols (both terminals and nonterminals) are represented by lowercase Greek letters ( , , , …).

6 Parsers A parser is a program that Is based on a context-free grammar, Receives, as its input, tokens from a lexer, Matches sequences of tokens to right-hand sides of grammar rules, and Outputs an abstract syntax tree (parse tree) representing the grammatical structure of the input.

7 Parsing Algorithms Parsing algorithms can be divided into two main categories. Top-down parsers – Begin with the start symbol and apply productions until only terminals remain. Bottom-up parsers – Begin by matching terminals to productions and continue until the string of input symbols is reduced to the start symbol.

8 Parsing Algorithms Top-down parsers traverse the parse tree from the top down. Bottom-up parsers traverse the parse tree from the bottom up.

9 Top-Down Parsers There are two basic methods of implementing a top-down parser. Recursive descent parsers (Lab 4). Table-driven parsers, also called LL and LR parsers (Labs 5 and 6).

10 Parsing Algorithms Although programming languages are described by context-free grammars, none of the known parsing algorithms is capable of parsing an arbitrary context-free grammar. Therefore, we must use caution when writing a grammar for a language; we must be able to parse it.


Download ppt "Parsing Lecture 5 Fri, Jan 28, 2005. Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A."

Similar presentations


Ads by Google