Held Monday, September 23, 2002
Today we consider the traditional grammar for expressions and mechanisms for making that grammar unambiguous.
strings of a's and b's with equal numbers of a's and b's.)
Exp ::= NUMBER
Exp ::= ID
Exp ::= UnOp Exp
UnOp ::= PLUS UnOp ::= MINUS
Exp := Exp BinOp Exp
BinOp ::= PLUS | MINUS | TIMES | DIVIDE
Exp ::= OPEN Exp OPEN
a+b*2is an expression? First, we observe that the lexer converts this to
identifier + identifier * number
Exp => // Exp ::= Exp BinOp Exp Exp BinOp Exp => // Exp ::= ID ID BinOp Exp => // BinOp ::= PLUS ID PLUS Exp => // Exp ::= Exp BinOp Exp ID PLUS Exp BinOp Exp => // Exp ::= NUMBER ID PLUS Exp BinOp NUMBER => // Exp ::= ID ID PLUS ID BinOp NUMBER => // Binop ::= TIMES ID PLUS ID TIMES NUMBER
Exprather than the first in
Exp BinOp Exp.
Exp / | \ / | \ Exp BinOp Exp------ / | | | \ / | | \ \ ID PLUS Exp BinOp Exp / | | / | | ID TIMES NUMBER
NUMBER PLUS NUMBER TIMES NUMBER
Exp / | \ / | \ Exp + Exp | / | \ | / | \ NUM Exp * Exp | | | | NUM NUM
Exp / | \ / | \ Exp * Exp / | \ | / | \ | Exp + Exp NUM | | | | NUM NUM
NUM MINUS NUM MINUS NUMmight be parsed with two different trees.
Term? Something that may include multiplication but does not include no unparenthesized addition.
Term ::= Term MulOp Term
Terms? Those that don't include multiplication. We've already called those
Term ::= Factor
Factor ::= OPEN Exp CLOSE Factor ::= NUM Factor ::= ID
Term, we can separate them into those that do and those that don't.
Exp ::= Exp AddOp Exp Exp ::= Term
Exp ::= Exp AddOp Term
Term ::= Term MulOp Factor
Exp ::= Exp AddOp Term | Term Term ::= Term MulOp Factor | Factor Factor ::= OPEN Exp OPEN | NUM | ID
-+numis meaningless, as is
I usually create these pages
on the fly, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Fri Dec 6 10:38:06 2002.
The source to the document was last modified on Wed Sep 4 10:08:34 2002.
This document may be found at