4. Syntax |
Overview
|
Note: Study 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
|
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
|
Try this: 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
|
Try this: 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
|
Try this: Write a recursive descent parser in your favorite programming language for the grammar shown above. Answer (Java): |
Example Recursive Descent Parsing
|
Exercise 1: Write a regular expression to capture the format of floating point constants in C/C++. |