2 Chomsky, Lexical Analysis and Pasing
2 Chomsky, Lexical Analysis and Pasing
2 Chomsky, Lexical Analysis and Pasing
x a character x
\x an escaped character, e.g., \n
{ name } a reference to a name
M|N M or N
MN M followed by N
M* zero or more occurrences of M
M+ One or more occurrences of M
M? Zero or one occurrence of M
[aeiou] the set of vowels
[0-9] the set of digits
. any single character
Production:
α→β
α Nonterminal
β (Nonterminal Terminal)*
ie, lefthand side is a single nonterminal, and righthand
side is a string of nonterminals and/or terminals
(possibly empty).
Production:
α→β |α| ≤ |β|
α, β (Nonterminal Terminal)*
ie, lefthand side can be composed of strings of
terminals and nonterminals
Additional metacharacters
{ } a series of zero or more
( ) must pick one from a list
[ ] pick none or one from a list
Example
Expression -> Term { ( + | - ) Term }
IfStatement -> if ( Expression ) Statement [ else Statement ]
EBNF is no more powerful than BNF, but its production rules are often simpler
and clearer.
Javacc EBNF
( … )* a series of zero or more
( … )+ a series of one or more
[ … ] optional
Dr. Philip Cannata
For more details, see Chapter 2 of
“Programming Language Pragmatics, Third Edition (Paperback)”
Michael L. Scott (Author)
Abstract Syntax
Parse of 5 - 4 + 3
• Example:
Expr -> Expr Op Expr | ( Expr ) | Integer
Op -> + | - | * | / | % | **
Ambiguous Parse of 5 – 4 + 3
if (x < 0)
if (y < 0) y = y - 1;
else y = 0;
Binary