Held Wednesday, February 14, 2001
Today we consider the traditional grammar for expressions and mechanisms for making that grammar unambiguous.
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
Monday, 22 January 2001
Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.
This page was generated by Siteweaver on Mon Apr 30 10:51:51 2001.
This page may be found at
You may validate this page's HTML.
The source was last modified Mon Jan 22 10:39:10 2001.