Held: Friday, January 30, 1998
Exp ::= number
Exp ::= identifier
Exp ::= UnOp Exp
UnOp ::= '+' UnOp ::= '-'
Exp := Exp BinOp Exp
BinOp ::= '+' | '-' | '*' | '/'
Exp ::= '(' Exp ')'
a+b*2
is an expression? First, we observe that the
lexer converts this to identifier + identifier * number
Exp => // Exp ::= Exp BinOp Exp Exp BinOp Exp => // Exp ::= identifier identifier BinOp Exp => // BinOp ::= '+' identifier + Exp => // Exp ::= Exp BinOp Exp identifier + Exp BinOp Exp => // Exp ::= number identifier + Exp BinOp number => // Exp ::= identifier identifier + identifier BinOp number => identifier + identifier * number
Exp
rather than the
first in Exp BinOp Exp
.
Exp / | \ / | \ Exp BinOp Exp------ / | | | \ / | | \ \ identifier + Exp BinOp Exp / | | / | | identifier * number
num + num * num
.
Exp / | \ / | \ Exp + Exp | / | \ | / | \ num Exp * Exp | | | | num num
Exp / | \ / | \ Exp * Exp / | \ | / | \ | Exp + Exp num | | | | num num
num - num - num
might be parsed with
two different trees.
Term
.
Term
? Something that may include
multiplication but does not include no unparenthesized addition.
AddExp
Term
.
Term
.
Factor
.
Term
s.
Term ::= Term MulOp Term MulOp ::= '*' | '/'
Term
s? Those that don't include
multiplication. We've already called those Factor
s.
Term ::= Factor
Factor ::= '(' + Exp + ')' Factor ::= num Factor ::= id
Term
, we can separate them into those that do and
those that don't.
Exp ::= Exp AddOp Exp Exp ::= Term AddOp ::= '+' | '-'
alpha() + beta() * gamma()
beta()
is multiplied by
the result of gamma()
and that result is then added
to alpha()
.
alpha()
, beta()
and gamma()
are evaluated in? In most languages,
the answer is "it's up to the implementer."
global integer x = 1; function zebra begin x = x + 1; return x; end zebra; ... y = zebra() + zebra() - zebra();
y
?
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Thu May 7 20:29:37 1998.
This page generated on Thu May 7 20:34:36 1998 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu