4. Syntax |
Overview
|
Note: These slides cover Chapter 2 of the textbook up to and including Section 2.2.3 |
Note: Java provides a class StreamTokenizer with which you can write scanners in Java to convert character streams into token streams |
Context-Free Grammars: BNF
<nonterminal> -> sequence of (non)terminals For example: <stmt> -> return | break | <id> := <expression> For example: <optional_static> -> static | e |
Extended BNF
For example: <stmt> -> for <id> := <expr> to <expr> [ step <expr> ] do <stmt> For example: <decl> -> int <id> (, <id>)* |
Example Grammar for ExpressionsContext-free grammar for a simple expression syntax with identifiers, integers, unary minus, parenthesis, and +, -, *, /
Note that identifier and signed_integer are tokens defined by a regular expression, not by the grammar. They are provided as tokens by the scanner in a compiler. |
Derivations
|
Parsing and Parse Trees
|
Note: An interactive parser demo demonstrates the parsing of a Pascal example program into a parse tree (see also textbook pp. 20-21) |
Ambiguous Grammars
|
Exercise: construct all possible left-most derivations of the string a-b+1 from the ambiguous simple expression grammar and from the unambiguous grammar. Also construct the parse trees. Answer: |
Ambiguous If-Then-Else
|
Exercise: given the above grammar, find two derivations for the program fragment(where C1 and C2 are some expressions, S1 and S2 are some statements)if C1 then if C2 then S1 else S2 |
Top-Down and Bottom-Up Parsing
|
LL Grammars and Top-Down Parsing
|
Top-Down Parsing Example
|
LR Grammars and Bottom-Up Parsing
|
Recursive Descent Parsing
|
A Recursive Descent Parser
|
Exercise: Write a recursive descent parser in Java for this grammar. Answer: |
Example Recursive Descent Parsing
|